]> git.saurik.com Git - apple/xnu.git/blame - bsd/nfs/nfs_vnops.c
xnu-3789.70.16.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_vnops.c
CommitLineData
1c79356b 1/*
3e170ce0 2 * Copyright (c) 2000-2015 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29/*
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95
65 * FreeBSD-Id: nfs_vnops.c,v 1.72 1997/11/07 09:20:48 phk Exp $
66 */
67
68
69/*
70 * vnode op calls for Sun NFS version 2 and 3
71 */
1c79356b
A
72#include <sys/param.h>
73#include <sys/kernel.h>
74#include <sys/systm.h>
75#include <sys/resourcevar.h>
91447636
A
76#include <sys/proc_internal.h>
77#include <sys/kauth.h>
78#include <sys/mount_internal.h>
1c79356b 79#include <sys/malloc.h>
91447636 80#include <sys/kpi_mbuf.h>
1c79356b 81#include <sys/conf.h>
91447636 82#include <sys/vnode_internal.h>
1c79356b
A
83#include <sys/dirent.h>
84#include <sys/fcntl.h>
85#include <sys/lockf.h>
91447636
A
86#include <sys/ubc_internal.h>
87#include <sys/attr.h>
88#include <sys/signalvar.h>
89#include <sys/uio_internal.h>
1c79356b 90
1c79356b
A
91#include <vfs/vfs_support.h>
92
93#include <sys/vm.h>
1c79356b 94
2d21ac55 95#include <sys/time.h>
1c79356b 96#include <kern/clock.h>
91447636 97#include <libkern/OSAtomic.h>
1c79356b
A
98
99#include <miscfs/fifofs/fifo.h>
100#include <miscfs/specfs/specdev.h>
101
102#include <nfs/rpcv2.h>
103#include <nfs/nfsproto.h>
104#include <nfs/nfs.h>
105#include <nfs/nfsnode.h>
2d21ac55 106#include <nfs/nfs_gss.h>
1c79356b 107#include <nfs/nfsmount.h>
55e303ae 108#include <nfs/nfs_lock.h>
1c79356b
A
109#include <nfs/xdr_subs.h>
110#include <nfs/nfsm_subs.h>
1c79356b
A
111
112#include <net/if.h>
113#include <netinet/in.h>
114#include <netinet/in_var.h>
b0d623f7 115
1c79356b 116#include <vm/vm_kern.h>
b0d623f7 117#include <vm/vm_pageout.h>
1c79356b 118
9bccf70c
A
119#include <kern/task.h>
120#include <kern/sched_prim.h>
1c79356b 121
39236c6e 122#define NFS_VNOP_DBG(...) NFS_DBG(NFS_FAC_VNOP, 7, ## __VA_ARGS__)
fe8ab488 123#define DEFAULT_READLINK_NOCACHE 0
39236c6e 124
1c79356b 125/*
2d21ac55 126 * NFS vnode ops
1c79356b 127 */
b0d623f7
A
128int nfs_vnop_lookup(struct vnop_lookup_args *);
129int nfsspec_vnop_read(struct vnop_read_args *);
130int nfsspec_vnop_write(struct vnop_write_args *);
131int nfsspec_vnop_close(struct vnop_close_args *);
2d21ac55 132#if FIFO
b0d623f7
A
133int nfsfifo_vnop_read(struct vnop_read_args *);
134int nfsfifo_vnop_write(struct vnop_write_args *);
135int nfsfifo_vnop_close(struct vnop_close_args *);
2d21ac55 136#endif
b0d623f7
A
137int nfs_vnop_ioctl(struct vnop_ioctl_args *);
138int nfs_vnop_select(struct vnop_select_args *);
139int nfs_vnop_setattr(struct vnop_setattr_args *);
b0d623f7 140int nfs_vnop_fsync(struct vnop_fsync_args *);
b0d623f7
A
141int nfs_vnop_rename(struct vnop_rename_args *);
142int nfs_vnop_readdir(struct vnop_readdir_args *);
143int nfs_vnop_readlink(struct vnop_readlink_args *);
144int nfs_vnop_pathconf(struct vnop_pathconf_args *);
145int nfs_vnop_pagein(struct vnop_pagein_args *);
146int nfs_vnop_pageout(struct vnop_pageout_args *);
147int nfs_vnop_blktooff(struct vnop_blktooff_args *);
148int nfs_vnop_offtoblk(struct vnop_offtoblk_args *);
149int nfs_vnop_blockmap(struct vnop_blockmap_args *);
6d2010ae 150int nfs_vnop_monitor(struct vnop_monitor_args *);
b0d623f7
A
151
152int nfs3_vnop_create(struct vnop_create_args *);
153int nfs3_vnop_mknod(struct vnop_mknod_args *);
154int nfs3_vnop_getattr(struct vnop_getattr_args *);
155int nfs3_vnop_link(struct vnop_link_args *);
156int nfs3_vnop_mkdir(struct vnop_mkdir_args *);
157int nfs3_vnop_rmdir(struct vnop_rmdir_args *);
158int nfs3_vnop_symlink(struct vnop_symlink_args *);
2d21ac55 159
91447636 160vnop_t **nfsv2_vnodeop_p;
1c79356b 161static struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
91447636 162 { &vnop_default_desc, (vnop_t *)vn_default_error },
2d21ac55
A
163 { &vnop_lookup_desc, (vnop_t *)nfs_vnop_lookup }, /* lookup */
164 { &vnop_create_desc, (vnop_t *)nfs3_vnop_create }, /* create */
165 { &vnop_mknod_desc, (vnop_t *)nfs3_vnop_mknod }, /* mknod */
6d2010ae
A
166 { &vnop_open_desc, (vnop_t *)nfs_vnop_open }, /* open */
167 { &vnop_close_desc, (vnop_t *)nfs_vnop_close }, /* close */
2d21ac55
A
168 { &vnop_access_desc, (vnop_t *)nfs_vnop_access }, /* access */
169 { &vnop_getattr_desc, (vnop_t *)nfs3_vnop_getattr }, /* getattr */
170 { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */
171 { &vnop_read_desc, (vnop_t *)nfs_vnop_read }, /* read */
172 { &vnop_write_desc, (vnop_t *)nfs_vnop_write }, /* write */
173 { &vnop_ioctl_desc, (vnop_t *)nfs_vnop_ioctl }, /* ioctl */
174 { &vnop_select_desc, (vnop_t *)nfs_vnop_select }, /* select */
175 { &vnop_revoke_desc, (vnop_t *)nfs_vnop_revoke }, /* revoke */
176 { &vnop_mmap_desc, (vnop_t *)nfs_vnop_mmap }, /* mmap */
6d2010ae 177 { &vnop_mnomap_desc, (vnop_t *)nfs_vnop_mnomap }, /* mnomap */
2d21ac55
A
178 { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */
179 { &vnop_remove_desc, (vnop_t *)nfs_vnop_remove }, /* remove */
180 { &vnop_link_desc, (vnop_t *)nfs3_vnop_link }, /* link */
181 { &vnop_rename_desc, (vnop_t *)nfs_vnop_rename }, /* rename */
182 { &vnop_mkdir_desc, (vnop_t *)nfs3_vnop_mkdir }, /* mkdir */
183 { &vnop_rmdir_desc, (vnop_t *)nfs3_vnop_rmdir }, /* rmdir */
184 { &vnop_symlink_desc, (vnop_t *)nfs3_vnop_symlink }, /* symlink */
185 { &vnop_readdir_desc, (vnop_t *)nfs_vnop_readdir }, /* readdir */
186 { &vnop_readlink_desc, (vnop_t *)nfs_vnop_readlink }, /* readlink */
187 { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */
188 { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */
91447636 189 { &vnop_strategy_desc, (vnop_t *)err_strategy }, /* strategy */
2d21ac55 190 { &vnop_pathconf_desc, (vnop_t *)nfs_vnop_pathconf }, /* pathconf */
6d2010ae 191 { &vnop_advlock_desc, (vnop_t *)nfs_vnop_advlock }, /* advlock */
91447636 192 { &vnop_bwrite_desc, (vnop_t *)err_bwrite }, /* bwrite */
2d21ac55
A
193 { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */
194 { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */
91447636 195 { &vnop_copyfile_desc, (vnop_t *)err_copyfile }, /* Copyfile */
2d21ac55
A
196 { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */
197 { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */
198 { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */
6d2010ae 199 { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */
1c79356b
A
200 { NULL, NULL }
201};
202struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
203 { &nfsv2_vnodeop_p, nfsv2_vnodeop_entries };
2d21ac55
A
204
205vnop_t **nfsv4_vnodeop_p;
206static struct vnodeopv_entry_desc nfsv4_vnodeop_entries[] = {
207 { &vnop_default_desc, (vnop_t *)vn_default_error },
208 { &vnop_lookup_desc, (vnop_t *)nfs_vnop_lookup }, /* lookup */
209 { &vnop_create_desc, (vnop_t *)nfs4_vnop_create }, /* create */
210 { &vnop_mknod_desc, (vnop_t *)nfs4_vnop_mknod }, /* mknod */
6d2010ae
A
211 { &vnop_open_desc, (vnop_t *)nfs_vnop_open }, /* open */
212 { &vnop_close_desc, (vnop_t *)nfs_vnop_close }, /* close */
2d21ac55
A
213 { &vnop_access_desc, (vnop_t *)nfs_vnop_access }, /* access */
214 { &vnop_getattr_desc, (vnop_t *)nfs4_vnop_getattr }, /* getattr */
215 { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */
6d2010ae 216 { &vnop_read_desc, (vnop_t *)nfs_vnop_read }, /* read */
2d21ac55
A
217 { &vnop_write_desc, (vnop_t *)nfs_vnop_write }, /* write */
218 { &vnop_ioctl_desc, (vnop_t *)nfs_vnop_ioctl }, /* ioctl */
219 { &vnop_select_desc, (vnop_t *)nfs_vnop_select }, /* select */
220 { &vnop_revoke_desc, (vnop_t *)nfs_vnop_revoke }, /* revoke */
6d2010ae
A
221 { &vnop_mmap_desc, (vnop_t *)nfs_vnop_mmap }, /* mmap */
222 { &vnop_mnomap_desc, (vnop_t *)nfs_vnop_mnomap }, /* mnomap */
2d21ac55
A
223 { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */
224 { &vnop_remove_desc, (vnop_t *)nfs_vnop_remove }, /* remove */
225 { &vnop_link_desc, (vnop_t *)nfs4_vnop_link }, /* link */
226 { &vnop_rename_desc, (vnop_t *)nfs_vnop_rename }, /* rename */
227 { &vnop_mkdir_desc, (vnop_t *)nfs4_vnop_mkdir }, /* mkdir */
228 { &vnop_rmdir_desc, (vnop_t *)nfs4_vnop_rmdir }, /* rmdir */
229 { &vnop_symlink_desc, (vnop_t *)nfs4_vnop_symlink }, /* symlink */
230 { &vnop_readdir_desc, (vnop_t *)nfs_vnop_readdir }, /* readdir */
231 { &vnop_readlink_desc, (vnop_t *)nfs_vnop_readlink }, /* readlink */
232 { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */
233 { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */
234 { &vnop_strategy_desc, (vnop_t *)err_strategy }, /* strategy */
235 { &vnop_pathconf_desc, (vnop_t *)nfs_vnop_pathconf }, /* pathconf */
6d2010ae 236 { &vnop_advlock_desc, (vnop_t *)nfs_vnop_advlock }, /* advlock */
2d21ac55
A
237 { &vnop_bwrite_desc, (vnop_t *)err_bwrite }, /* bwrite */
238 { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */
239 { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */
240 { &vnop_copyfile_desc, (vnop_t *)err_copyfile }, /* Copyfile */
241 { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */
242 { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */
243 { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */
6d2010ae
A
244 { &vnop_getxattr_desc, (vnop_t *)nfs4_vnop_getxattr }, /* getxattr */
245 { &vnop_setxattr_desc, (vnop_t *)nfs4_vnop_setxattr }, /* setxattr */
246 { &vnop_removexattr_desc, (vnop_t *)nfs4_vnop_removexattr },/* removexattr */
247 { &vnop_listxattr_desc, (vnop_t *)nfs4_vnop_listxattr },/* listxattr */
248#if NAMEDSTREAMS
249 { &vnop_getnamedstream_desc, (vnop_t *)nfs4_vnop_getnamedstream }, /* getnamedstream */
250 { &vnop_makenamedstream_desc, (vnop_t *)nfs4_vnop_makenamedstream }, /* makenamedstream */
251 { &vnop_removenamedstream_desc, (vnop_t *)nfs4_vnop_removenamedstream },/* removenamedstream */
252#endif
253 { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */
2d21ac55
A
254 { NULL, NULL }
255};
256struct vnodeopv_desc nfsv4_vnodeop_opv_desc =
257 { &nfsv4_vnodeop_p, nfsv4_vnodeop_entries };
1c79356b
A
258
259/*
260 * Special device vnode ops
261 */
91447636 262vnop_t **spec_nfsv2nodeop_p;
1c79356b 263static struct vnodeopv_entry_desc spec_nfsv2nodeop_entries[] = {
91447636
A
264 { &vnop_default_desc, (vnop_t *)vn_default_error },
265 { &vnop_lookup_desc, (vnop_t *)spec_lookup }, /* lookup */
266 { &vnop_create_desc, (vnop_t *)spec_create }, /* create */
267 { &vnop_mknod_desc, (vnop_t *)spec_mknod }, /* mknod */
268 { &vnop_open_desc, (vnop_t *)spec_open }, /* open */
2d21ac55
A
269 { &vnop_close_desc, (vnop_t *)nfsspec_vnop_close }, /* close */
270 { &vnop_getattr_desc, (vnop_t *)nfs3_vnop_getattr }, /* getattr */
271 { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */
272 { &vnop_read_desc, (vnop_t *)nfsspec_vnop_read }, /* read */
273 { &vnop_write_desc, (vnop_t *)nfsspec_vnop_write }, /* write */
91447636
A
274 { &vnop_ioctl_desc, (vnop_t *)spec_ioctl }, /* ioctl */
275 { &vnop_select_desc, (vnop_t *)spec_select }, /* select */
276 { &vnop_revoke_desc, (vnop_t *)spec_revoke }, /* revoke */
277 { &vnop_mmap_desc, (vnop_t *)spec_mmap }, /* mmap */
2d21ac55 278 { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */
91447636
A
279 { &vnop_remove_desc, (vnop_t *)spec_remove }, /* remove */
280 { &vnop_link_desc, (vnop_t *)spec_link }, /* link */
281 { &vnop_rename_desc, (vnop_t *)spec_rename }, /* rename */
282 { &vnop_mkdir_desc, (vnop_t *)spec_mkdir }, /* mkdir */
283 { &vnop_rmdir_desc, (vnop_t *)spec_rmdir }, /* rmdir */
284 { &vnop_symlink_desc, (vnop_t *)spec_symlink }, /* symlink */
285 { &vnop_readdir_desc, (vnop_t *)spec_readdir }, /* readdir */
286 { &vnop_readlink_desc, (vnop_t *)spec_readlink }, /* readlink */
2d21ac55
A
287 { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */
288 { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */
91447636
A
289 { &vnop_strategy_desc, (vnop_t *)spec_strategy }, /* strategy */
290 { &vnop_pathconf_desc, (vnop_t *)spec_pathconf }, /* pathconf */
291 { &vnop_advlock_desc, (vnop_t *)spec_advlock }, /* advlock */
292 { &vnop_bwrite_desc, (vnop_t *)vn_bwrite }, /* bwrite */
2d21ac55
A
293 { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */
294 { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */
295 { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */
296 { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */
297 { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */
6d2010ae 298 { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */
1c79356b
A
299 { NULL, NULL }
300};
301struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
302 { &spec_nfsv2nodeop_p, spec_nfsv2nodeop_entries };
2d21ac55
A
303vnop_t **spec_nfsv4nodeop_p;
304static struct vnodeopv_entry_desc spec_nfsv4nodeop_entries[] = {
305 { &vnop_default_desc, (vnop_t *)vn_default_error },
306 { &vnop_lookup_desc, (vnop_t *)spec_lookup }, /* lookup */
307 { &vnop_create_desc, (vnop_t *)spec_create }, /* create */
308 { &vnop_mknod_desc, (vnop_t *)spec_mknod }, /* mknod */
309 { &vnop_open_desc, (vnop_t *)spec_open }, /* open */
310 { &vnop_close_desc, (vnop_t *)nfsspec_vnop_close }, /* close */
311 { &vnop_getattr_desc, (vnop_t *)nfs4_vnop_getattr }, /* getattr */
312 { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */
313 { &vnop_read_desc, (vnop_t *)nfsspec_vnop_read }, /* read */
314 { &vnop_write_desc, (vnop_t *)nfsspec_vnop_write }, /* write */
315 { &vnop_ioctl_desc, (vnop_t *)spec_ioctl }, /* ioctl */
316 { &vnop_select_desc, (vnop_t *)spec_select }, /* select */
317 { &vnop_revoke_desc, (vnop_t *)spec_revoke }, /* revoke */
318 { &vnop_mmap_desc, (vnop_t *)spec_mmap }, /* mmap */
319 { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */
320 { &vnop_remove_desc, (vnop_t *)spec_remove }, /* remove */
321 { &vnop_link_desc, (vnop_t *)spec_link }, /* link */
322 { &vnop_rename_desc, (vnop_t *)spec_rename }, /* rename */
323 { &vnop_mkdir_desc, (vnop_t *)spec_mkdir }, /* mkdir */
324 { &vnop_rmdir_desc, (vnop_t *)spec_rmdir }, /* rmdir */
325 { &vnop_symlink_desc, (vnop_t *)spec_symlink }, /* symlink */
326 { &vnop_readdir_desc, (vnop_t *)spec_readdir }, /* readdir */
327 { &vnop_readlink_desc, (vnop_t *)spec_readlink }, /* readlink */
328 { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */
329 { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */
330 { &vnop_strategy_desc, (vnop_t *)spec_strategy }, /* strategy */
331 { &vnop_pathconf_desc, (vnop_t *)spec_pathconf }, /* pathconf */
332 { &vnop_advlock_desc, (vnop_t *)spec_advlock }, /* advlock */
333 { &vnop_bwrite_desc, (vnop_t *)vn_bwrite }, /* bwrite */
334 { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */
335 { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */
336 { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */
337 { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */
338 { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */
6d2010ae
A
339 { &vnop_getxattr_desc, (vnop_t *)nfs4_vnop_getxattr }, /* getxattr */
340 { &vnop_setxattr_desc, (vnop_t *)nfs4_vnop_setxattr }, /* setxattr */
341 { &vnop_removexattr_desc, (vnop_t *)nfs4_vnop_removexattr },/* removexattr */
342 { &vnop_listxattr_desc, (vnop_t *)nfs4_vnop_listxattr },/* listxattr */
343#if NAMEDSTREAMS
344 { &vnop_getnamedstream_desc, (vnop_t *)nfs4_vnop_getnamedstream }, /* getnamedstream */
345 { &vnop_makenamedstream_desc, (vnop_t *)nfs4_vnop_makenamedstream }, /* makenamedstream */
346 { &vnop_removenamedstream_desc, (vnop_t *)nfs4_vnop_removenamedstream },/* removenamedstream */
347#endif
348 { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */
2d21ac55
A
349 { NULL, NULL }
350};
351struct vnodeopv_desc spec_nfsv4nodeop_opv_desc =
352 { &spec_nfsv4nodeop_p, spec_nfsv4nodeop_entries };
1c79356b 353
2d21ac55 354#if FIFO
91447636 355vnop_t **fifo_nfsv2nodeop_p;
1c79356b 356static struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries[] = {
91447636
A
357 { &vnop_default_desc, (vnop_t *)vn_default_error },
358 { &vnop_lookup_desc, (vnop_t *)fifo_lookup }, /* lookup */
359 { &vnop_create_desc, (vnop_t *)fifo_create }, /* create */
360 { &vnop_mknod_desc, (vnop_t *)fifo_mknod }, /* mknod */
361 { &vnop_open_desc, (vnop_t *)fifo_open }, /* open */
2d21ac55
A
362 { &vnop_close_desc, (vnop_t *)nfsfifo_vnop_close }, /* close */
363 { &vnop_getattr_desc, (vnop_t *)nfs3_vnop_getattr }, /* getattr */
364 { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */
365 { &vnop_read_desc, (vnop_t *)nfsfifo_vnop_read }, /* read */
366 { &vnop_write_desc, (vnop_t *)nfsfifo_vnop_write }, /* write */
91447636
A
367 { &vnop_ioctl_desc, (vnop_t *)fifo_ioctl }, /* ioctl */
368 { &vnop_select_desc, (vnop_t *)fifo_select }, /* select */
369 { &vnop_revoke_desc, (vnop_t *)fifo_revoke }, /* revoke */
370 { &vnop_mmap_desc, (vnop_t *)fifo_mmap }, /* mmap */
2d21ac55 371 { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */
91447636
A
372 { &vnop_remove_desc, (vnop_t *)fifo_remove }, /* remove */
373 { &vnop_link_desc, (vnop_t *)fifo_link }, /* link */
374 { &vnop_rename_desc, (vnop_t *)fifo_rename }, /* rename */
375 { &vnop_mkdir_desc, (vnop_t *)fifo_mkdir }, /* mkdir */
376 { &vnop_rmdir_desc, (vnop_t *)fifo_rmdir }, /* rmdir */
377 { &vnop_symlink_desc, (vnop_t *)fifo_symlink }, /* symlink */
378 { &vnop_readdir_desc, (vnop_t *)fifo_readdir }, /* readdir */
379 { &vnop_readlink_desc, (vnop_t *)fifo_readlink }, /* readlink */
2d21ac55
A
380 { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */
381 { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */
91447636
A
382 { &vnop_strategy_desc, (vnop_t *)fifo_strategy }, /* strategy */
383 { &vnop_pathconf_desc, (vnop_t *)fifo_pathconf }, /* pathconf */
384 { &vnop_advlock_desc, (vnop_t *)fifo_advlock }, /* advlock */
385 { &vnop_bwrite_desc, (vnop_t *)vn_bwrite }, /* bwrite */
2d21ac55
A
386 { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */
387 { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */
388 { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */
389 { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */
390 { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */
6d2010ae 391 { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */
1c79356b
A
392 { NULL, NULL }
393};
394struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
395 { &fifo_nfsv2nodeop_p, fifo_nfsv2nodeop_entries };
0b4e3aa0 396
2d21ac55
A
397vnop_t **fifo_nfsv4nodeop_p;
398static struct vnodeopv_entry_desc fifo_nfsv4nodeop_entries[] = {
399 { &vnop_default_desc, (vnop_t *)vn_default_error },
400 { &vnop_lookup_desc, (vnop_t *)fifo_lookup }, /* lookup */
401 { &vnop_create_desc, (vnop_t *)fifo_create }, /* create */
402 { &vnop_mknod_desc, (vnop_t *)fifo_mknod }, /* mknod */
403 { &vnop_open_desc, (vnop_t *)fifo_open }, /* open */
404 { &vnop_close_desc, (vnop_t *)nfsfifo_vnop_close }, /* close */
405 { &vnop_getattr_desc, (vnop_t *)nfs4_vnop_getattr }, /* getattr */
406 { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr }, /* setattr */
407 { &vnop_read_desc, (vnop_t *)nfsfifo_vnop_read }, /* read */
408 { &vnop_write_desc, (vnop_t *)nfsfifo_vnop_write }, /* write */
409 { &vnop_ioctl_desc, (vnop_t *)fifo_ioctl }, /* ioctl */
410 { &vnop_select_desc, (vnop_t *)fifo_select }, /* select */
411 { &vnop_revoke_desc, (vnop_t *)fifo_revoke }, /* revoke */
412 { &vnop_mmap_desc, (vnop_t *)fifo_mmap }, /* mmap */
413 { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */
414 { &vnop_remove_desc, (vnop_t *)fifo_remove }, /* remove */
415 { &vnop_link_desc, (vnop_t *)fifo_link }, /* link */
416 { &vnop_rename_desc, (vnop_t *)fifo_rename }, /* rename */
417 { &vnop_mkdir_desc, (vnop_t *)fifo_mkdir }, /* mkdir */
418 { &vnop_rmdir_desc, (vnop_t *)fifo_rmdir }, /* rmdir */
419 { &vnop_symlink_desc, (vnop_t *)fifo_symlink }, /* symlink */
420 { &vnop_readdir_desc, (vnop_t *)fifo_readdir }, /* readdir */
421 { &vnop_readlink_desc, (vnop_t *)fifo_readlink }, /* readlink */
422 { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */
423 { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */
424 { &vnop_strategy_desc, (vnop_t *)fifo_strategy }, /* strategy */
425 { &vnop_pathconf_desc, (vnop_t *)fifo_pathconf }, /* pathconf */
426 { &vnop_advlock_desc, (vnop_t *)fifo_advlock }, /* advlock */
427 { &vnop_bwrite_desc, (vnop_t *)vn_bwrite }, /* bwrite */
428 { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */
429 { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */
430 { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff }, /* blktooff */
431 { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk }, /* offtoblk */
432 { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap }, /* blockmap */
6d2010ae
A
433 { &vnop_getxattr_desc, (vnop_t *)nfs4_vnop_getxattr }, /* getxattr */
434 { &vnop_setxattr_desc, (vnop_t *)nfs4_vnop_setxattr }, /* setxattr */
435 { &vnop_removexattr_desc, (vnop_t *)nfs4_vnop_removexattr },/* removexattr */
436 { &vnop_listxattr_desc, (vnop_t *)nfs4_vnop_listxattr },/* listxattr */
437#if NAMEDSTREAMS
438 { &vnop_getnamedstream_desc, (vnop_t *)nfs4_vnop_getnamedstream }, /* getnamedstream */
439 { &vnop_makenamedstream_desc, (vnop_t *)nfs4_vnop_makenamedstream }, /* makenamedstream */
440 { &vnop_removenamedstream_desc, (vnop_t *)nfs4_vnop_removenamedstream },/* removenamedstream */
441#endif
442 { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */
2d21ac55 443 { NULL, NULL }
0b4e3aa0 444};
2d21ac55
A
445struct vnodeopv_desc fifo_nfsv4nodeop_opv_desc =
446 { &fifo_nfsv4nodeop_p, fifo_nfsv4nodeop_entries };
447#endif /* FIFO */
0b4e3aa0 448
b0d623f7 449int nfs_sillyrename(nfsnode_t,nfsnode_t,struct componentname *,vfs_context_t);
fe8ab488
A
450int nfs_getattr_internal(nfsnode_t, struct nfs_vattr *, vfs_context_t, int);
451int nfs_refresh_fh(nfsnode_t, vfs_context_t);
2d21ac55
A
452
453/*
454 * Find the slot in the access cache for this UID.
455 * If adding and no existing slot is found, reuse slots in FIFO order.
6d2010ae 456 * The index of the next slot to use is kept in the last entry of the n_access array.
2d21ac55
A
457 */
458int
6d2010ae 459nfs_node_access_slot(nfsnode_t np, uid_t uid, int add)
0b4e3aa0 460{
2d21ac55
A
461 int slot;
462
463 for (slot=0; slot < NFS_ACCESS_CACHE_SIZE; slot++)
6d2010ae 464 if (np->n_accessuid[slot] == uid)
2d21ac55
A
465 break;
466 if (slot == NFS_ACCESS_CACHE_SIZE) {
467 if (!add)
468 return (-1);
6d2010ae
A
469 slot = np->n_access[NFS_ACCESS_CACHE_SIZE];
470 np->n_access[NFS_ACCESS_CACHE_SIZE] = (slot + 1) % NFS_ACCESS_CACHE_SIZE;
2d21ac55
A
471 }
472 return (slot);
0b4e3aa0 473}
1c79356b 474
2d21ac55 475int
fe8ab488 476nfs3_access_rpc(nfsnode_t np, u_int32_t *access, int rpcflags, vfs_context_t ctx)
1c79356b 477{
b0d623f7 478 int error = 0, lockerror = ENOENT, status, slot;
6d2010ae 479 uint32_t access_result = 0;
fa4905b1 480 u_int64_t xid;
2d21ac55 481 struct nfsm_chain nmreq, nmrep;
813fb2f6 482 struct nfsmount *nmp;
55e303ae 483 struct timeval now;
2d21ac55
A
484 uid_t uid;
485
486 nfsm_chain_null(&nmreq);
487 nfsm_chain_null(&nmrep);
488
489 nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(NFS_VER3) + NFSX_UNSIGNED);
490 nfsm_chain_add_fh(error, &nmreq, NFS_VER3, np->n_fhp, np->n_fhsize);
6d2010ae 491 nfsm_chain_add_32(error, &nmreq, *access);
2d21ac55
A
492 nfsm_chain_build_done(error, &nmreq);
493 nfsmout_if(error);
fe8ab488
A
494 error = nfs_request2(np, NULL, &nmreq, NFSPROC_ACCESS,
495 vfs_context_thread(ctx), vfs_context_ucred(ctx),
496 NULL, rpcflags, &nmrep, &xid, &status);
b0d623f7
A
497 if ((lockerror = nfs_node_lock(np)))
498 error = lockerror;
2d21ac55
A
499 nfsm_chain_postop_attr_update(error, &nmrep, np, &xid);
500 if (!error)
501 error = status;
6d2010ae 502 nfsm_chain_get_32(error, &nmrep, access_result);
2d21ac55 503 nfsmout_if(error);
0b4e3aa0 504
813fb2f6
A
505 /* XXXab do we really need mount here, also why are we doing access cache management here? */
506 nmp = NFSTONMP(np);
507 if (nfs_mount_gone(nmp)) {
508 error = ENXIO;
509 }
510 nfsmout_if(error);
511
512 if (auth_is_kerberized(np->n_auth) || auth_is_kerberized(nmp->nm_auth)) {
513 uid = nfs_cred_getasid2uid(vfs_context_ucred(ctx));
514 } else {
515 uid = kauth_cred_getuid(vfs_context_ucred(ctx));
516 }
6d2010ae
A
517 slot = nfs_node_access_slot(np, uid, 1);
518 np->n_accessuid[slot] = uid;
2d21ac55 519 microuptime(&now);
6d2010ae
A
520 np->n_accessstamp[slot] = now.tv_sec;
521 np->n_access[slot] = access_result;
2d21ac55
A
522
523 /*
524 * If we asked for DELETE but didn't get it, the server
525 * may simply not support returning that bit (possible
526 * on UNIX systems). So, we'll assume that it is OK,
527 * and just let any subsequent delete action fail if it
528 * really isn't deletable.
529 */
6d2010ae
A
530 if ((*access & NFS_ACCESS_DELETE) &&
531 !(np->n_access[slot] & NFS_ACCESS_DELETE))
532 np->n_access[slot] |= NFS_ACCESS_DELETE;
533 /* ".zfs" subdirectories may erroneously give a denied answer for add/remove */
534 if (nfs_access_dotzfs && (np->n_flag & NISDOTZFSCHILD))
535 np->n_access[slot] |= (NFS_ACCESS_MODIFY|NFS_ACCESS_EXTEND|NFS_ACCESS_DELETE);
536 /* pass back the access returned with this request */
537 *access = np->n_access[slot];
2d21ac55 538nfsmout:
b0d623f7
A
539 if (!lockerror)
540 nfs_node_unlock(np);
2d21ac55
A
541 nfsm_chain_cleanup(&nmreq);
542 nfsm_chain_cleanup(&nmrep);
543 return (error);
1c79356b
A
544}
545
546/*
2d21ac55
A
547 * NFS access vnode op.
548 * For NFS version 2, just return ok. File accesses may fail later.
6d2010ae
A
549 * For NFS version 3+, use the access RPC to check accessibility. If file
550 * permissions are changed on the server, accesses might still fail later.
1c79356b 551 */
2d21ac55
A
552int
553nfs_vnop_access(
91447636
A
554 struct vnop_access_args /* {
555 struct vnodeop_desc *a_desc;
556 vnode_t a_vp;
2d21ac55 557 int a_action;
91447636 558 vfs_context_t a_context;
2d21ac55 559 } */ *ap)
1c79356b 560{
2d21ac55 561 vfs_context_t ctx = ap->a_context;
91447636 562 vnode_t vp = ap->a_vp;
fe8ab488 563 int error = 0, slot, dorpc, rpcflags = 0;
6d2010ae 564 u_int32_t access, waccess;
2d21ac55
A
565 nfsnode_t np = VTONFS(vp);
566 struct nfsmount *nmp;
567 int nfsvers;
55e303ae 568 struct timeval now;
2d21ac55
A
569 uid_t uid;
570
571 nmp = VTONMP(vp);
fe8ab488 572 if (nfs_mount_gone(nmp))
2d21ac55
A
573 return (ENXIO);
574 nfsvers = nmp->nm_vers;
575
576 if (nfsvers == NFS_VER2) {
577 if ((ap->a_action & KAUTH_VNODE_WRITE_RIGHTS) &&
578 vfs_isrdonly(vnode_mount(vp)))
579 return (EROFS);
580 return (0);
581 }
1c79356b
A
582
583 /*
2d21ac55 584 * For NFS v3, do an access rpc, otherwise you are stuck emulating
1c79356b
A
585 * ufs_access() locally using the vattr. This may not be correct,
586 * since the server may apply other access criteria such as
587 * client uid-->server uid mapping that we do not know about, but
588 * this is better than just returning anything that is lying about
589 * in the cache.
590 */
91447636 591
2d21ac55
A
592 /*
593 * Convert KAUTH primitives to NFS access rights.
594 */
6d2010ae 595 access = 0;
2d21ac55
A
596 if (vnode_isdir(vp)) {
597 /* directory */
598 if (ap->a_action &
599 (KAUTH_VNODE_LIST_DIRECTORY |
600 KAUTH_VNODE_READ_EXTATTRIBUTES))
6d2010ae 601 access |= NFS_ACCESS_READ;
2d21ac55 602 if (ap->a_action & KAUTH_VNODE_SEARCH)
6d2010ae 603 access |= NFS_ACCESS_LOOKUP;
2d21ac55
A
604 if (ap->a_action &
605 (KAUTH_VNODE_ADD_FILE |
606 KAUTH_VNODE_ADD_SUBDIRECTORY))
6d2010ae 607 access |= NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
2d21ac55 608 if (ap->a_action & KAUTH_VNODE_DELETE_CHILD)
6d2010ae 609 access |= NFS_ACCESS_MODIFY;
91447636 610 } else {
2d21ac55
A
611 /* file */
612 if (ap->a_action &
613 (KAUTH_VNODE_READ_DATA |
614 KAUTH_VNODE_READ_EXTATTRIBUTES))
6d2010ae 615 access |= NFS_ACCESS_READ;
2d21ac55 616 if (ap->a_action & KAUTH_VNODE_WRITE_DATA)
6d2010ae 617 access |= NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
2d21ac55 618 if (ap->a_action & KAUTH_VNODE_APPEND_DATA)
6d2010ae 619 access |= NFS_ACCESS_EXTEND;
2d21ac55 620 if (ap->a_action & KAUTH_VNODE_EXECUTE)
6d2010ae 621 access |= NFS_ACCESS_EXECUTE;
2d21ac55
A
622 }
623 /* common */
624 if (ap->a_action & KAUTH_VNODE_DELETE)
6d2010ae 625 access |= NFS_ACCESS_DELETE;
2d21ac55
A
626 if (ap->a_action &
627 (KAUTH_VNODE_WRITE_ATTRIBUTES |
628 KAUTH_VNODE_WRITE_EXTATTRIBUTES |
629 KAUTH_VNODE_WRITE_SECURITY))
6d2010ae 630 access |= NFS_ACCESS_MODIFY;
2d21ac55
A
631 /* XXX this is pretty dubious */
632 if (ap->a_action & KAUTH_VNODE_CHANGE_OWNER)
6d2010ae 633 access |= NFS_ACCESS_MODIFY;
2d21ac55
A
634
635 /* if caching, always ask for every right */
636 if (nfs_access_cache_timeout > 0) {
6d2010ae 637 waccess = NFS_ACCESS_READ | NFS_ACCESS_MODIFY |
2d21ac55
A
638 NFS_ACCESS_EXTEND | NFS_ACCESS_EXECUTE |
639 NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
640 } else {
6d2010ae 641 waccess = access;
2d21ac55
A
642 }
643
b0d623f7 644 if ((error = nfs_node_lock(np)))
2d21ac55
A
645 return (error);
646
647 /*
648 * Does our cached result allow us to give a definite yes to
649 * this request?
650 */
3e170ce0
A
651 if (auth_is_kerberized(np->n_auth) || auth_is_kerberized(nmp->nm_auth))
652 uid = nfs_cred_getasid2uid(vfs_context_ucred(ctx));
653 else
654 uid = kauth_cred_getuid(vfs_context_ucred(ctx));
6d2010ae 655 slot = nfs_node_access_slot(np, uid, 0);
2d21ac55 656 dorpc = 1;
6d2010ae 657 if (access == 0) {
b0d623f7
A
658 /* not asking for any rights understood by NFS, so don't bother doing an RPC */
659 /* OSAddAtomic(1, &nfsstats.accesscache_hits); */
660 dorpc = 0;
6d2010ae
A
661 waccess = 0;
662 } else if (NACCESSVALID(np, slot)) {
fe8ab488
A
663 microuptime(&now);
664 if (((now.tv_sec < (np->n_accessstamp[slot] + nfs_access_cache_timeout)) &&
665 ((np->n_access[slot] & access) == access)) || nfs_use_cache(nmp)) {
b0d623f7 666 /* OSAddAtomic(1, &nfsstats.accesscache_hits); */
fe8ab488
A
667 dorpc = 0;
668 waccess = np->n_access[slot];
1c79356b 669 }
fa4905b1 670 }
b0d623f7 671 nfs_node_unlock(np);
2d21ac55
A
672 if (dorpc) {
673 /* Either a no, or a don't know. Go to the wire. */
b0d623f7 674 /* OSAddAtomic(1, &nfsstats.accesscache_misses); */
fe8ab488
A
675
676 /*
677 * Allow an access call to timeout if we have it cached
678 * so we won't hang if the server isn't responding.
679 */
680 if (NACCESSVALID(np, slot))
681 rpcflags |= R_SOFT;
682
683 error = nmp->nm_funcs->nf_access_rpc(np, &waccess, rpcflags, ctx);
684
685 /*
686 * If the server didn't respond return the cached access.
687 */
688 if ((error == ETIMEDOUT) && (rpcflags & R_SOFT)) {
689 error = 0;
690 waccess = np->n_access[slot];
691 }
2d21ac55 692 }
6d2010ae 693 if (!error && ((waccess & access) != access))
2d21ac55 694 error = EACCES;
91447636 695
0b4e3aa0 696 return (error);
1c79356b
A
697}
698
6d2010ae 699
1c79356b 700/*
2d21ac55 701 * NFS open vnode op
6d2010ae
A
702 *
703 * Perform various update/invalidation checks and then add the
704 * open to the node. Regular files will have an open file structure
705 * on the node and, for NFSv4, perform an OPEN request on the server.
1c79356b 706 */
2d21ac55 707int
6d2010ae 708nfs_vnop_open(
91447636
A
709 struct vnop_open_args /* {
710 struct vnodeop_desc *a_desc;
711 vnode_t a_vp;
712 int a_mode;
713 vfs_context_t a_context;
2d21ac55 714 } */ *ap)
1c79356b 715{
2d21ac55 716 vfs_context_t ctx = ap->a_context;
91447636 717 vnode_t vp = ap->a_vp;
2d21ac55
A
718 nfsnode_t np = VTONFS(vp);
719 struct nfsmount *nmp;
6d2010ae
A
720 int error, accessMode, denyMode, opened = 0;
721 struct nfs_open_owner *noop = NULL;
722 struct nfs_open_file *nofp = NULL;
91447636 723 enum vtype vtype;
6d2010ae
A
724
725 if (!(ap->a_mode & (FREAD|FWRITE)))
726 return (EINVAL);
2d21ac55
A
727
728 nmp = VTONMP(vp);
fe8ab488 729 if (nfs_mount_gone(nmp))
2d21ac55 730 return (ENXIO);
6d2010ae
A
731 if (np->n_flag & NREVOKE)
732 return (EIO);
1c79356b 733
91447636 734 vtype = vnode_vtype(vp);
2d21ac55 735 if ((vtype != VREG) && (vtype != VDIR) && (vtype != VLNK))
1c79356b 736 return (EACCES);
6d2010ae
A
737
738 /* First, check if we need to update/invalidate */
2d21ac55
A
739 if (ISSET(np->n_flag, NUPDATESIZE))
740 nfs_data_update_size(np, 0);
b0d623f7 741 if ((error = nfs_node_lock(np)))
2d21ac55 742 return (error);
91447636
A
743 if (np->n_flag & NNEEDINVALIDATE) {
744 np->n_flag &= ~NNEEDINVALIDATE;
b0d623f7
A
745 if (vtype == VDIR)
746 nfs_invaldir(np);
747 nfs_node_unlock(np);
2d21ac55 748 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1);
b0d623f7 749 if ((error = nfs_node_lock(np)))
2d21ac55 750 return (error);
91447636 751 }
6d2010ae 752 if (vtype == VREG)
b0d623f7 753 np->n_lastrahead = -1;
91447636 754 if (np->n_flag & NMODIFIED) {
91447636 755 if (vtype == VDIR)
b0d623f7
A
756 nfs_invaldir(np);
757 nfs_node_unlock(np);
758 if ((error = nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1)))
91447636 759 return (error);
1c79356b 760 } else {
b0d623f7 761 nfs_node_unlock(np);
1c79356b 762 }
6d2010ae 763
b0d623f7 764 /* nfs_getattr() will check changed and purge caches */
6d2010ae
A
765 if ((error = nfs_getattr(np, NULL, ctx, NGA_UNCACHED)))
766 return (error);
767
768 if (vtype != VREG) {
769 /* Just mark that it was opened */
770 lck_mtx_lock(&np->n_openlock);
771 np->n_openrefcnt++;
772 lck_mtx_unlock(&np->n_openlock);
773 return (0);
774 }
775
776 /* mode contains some combination of: FREAD, FWRITE, O_SHLOCK, O_EXLOCK */
777 accessMode = 0;
778 if (ap->a_mode & FREAD)
779 accessMode |= NFS_OPEN_SHARE_ACCESS_READ;
780 if (ap->a_mode & FWRITE)
781 accessMode |= NFS_OPEN_SHARE_ACCESS_WRITE;
782 if (ap->a_mode & O_EXLOCK)
783 denyMode = NFS_OPEN_SHARE_DENY_BOTH;
784 else if (ap->a_mode & O_SHLOCK)
785 denyMode = NFS_OPEN_SHARE_DENY_WRITE;
786 else
787 denyMode = NFS_OPEN_SHARE_DENY_NONE;
788 // XXX don't do deny modes just yet (and never do it for !v4)
789 denyMode = NFS_OPEN_SHARE_DENY_NONE;
790
791 noop = nfs_open_owner_find(nmp, vfs_context_ucred(ctx), 1);
792 if (!noop)
793 return (ENOMEM);
794
795restart:
796 error = nfs_mount_state_in_use_start(nmp, vfs_context_thread(ctx));
797 if (error) {
798 nfs_open_owner_rele(noop);
799 return (error);
800 }
801 if (np->n_flag & NREVOKE) {
802 error = EIO;
803 nfs_mount_state_in_use_end(nmp, 0);
804 nfs_open_owner_rele(noop);
805 return (error);
806 }
807
808 error = nfs_open_file_find(np, noop, &nofp, accessMode, denyMode, 1);
809 if (!error && (nofp->nof_flags & NFS_OPEN_FILE_LOST)) {
810 NP(np, "nfs_vnop_open: LOST %d", kauth_cred_getuid(nofp->nof_owner->noo_cred));
811 error = EIO;
812 }
813 if (!error && (nofp->nof_flags & NFS_OPEN_FILE_REOPEN)) {
814 nfs_mount_state_in_use_end(nmp, 0);
815 error = nfs4_reopen(nofp, vfs_context_thread(ctx));
816 nofp = NULL;
817 if (!error)
818 goto restart;
819 }
820 if (!error)
821 error = nfs_open_file_set_busy(nofp, vfs_context_thread(ctx));
822 if (error) {
823 nofp = NULL;
824 goto out;
825 }
826
827 if (nmp->nm_vers < NFS_VER4) {
828 /*
829 * NFS v2/v3 opens are always allowed - so just add it.
830 */
831 nfs_open_file_add_open(nofp, accessMode, denyMode, 0);
832 goto out;
833 }
834
835 /*
836 * If we just created the file and the modes match, then we simply use
837 * the open performed in the create. Otherwise, send the request.
838 */
839 if ((nofp->nof_flags & NFS_OPEN_FILE_CREATE) &&
840 (nofp->nof_creator == current_thread()) &&
841 (accessMode == NFS_OPEN_SHARE_ACCESS_BOTH) &&
842 (denyMode == NFS_OPEN_SHARE_DENY_NONE)) {
843 nofp->nof_flags &= ~NFS_OPEN_FILE_CREATE;
844 nofp->nof_creator = NULL;
845 } else {
846 if (!opened)
847 error = nfs4_open(np, nofp, accessMode, denyMode, ctx);
848 if ((error == EACCES) && (nofp->nof_flags & NFS_OPEN_FILE_CREATE) &&
849 (nofp->nof_creator == current_thread())) {
850 /*
851 * Ugh. This can happen if we just created the file with read-only
852 * perms and we're trying to open it for real with different modes
853 * (e.g. write-only or with a deny mode) and the server decides to
854 * not allow the second open because of the read-only perms.
855 * The best we can do is to just use the create's open.
856 * We may have access we don't need or we may not have a requested
857 * deny mode. We may log complaints later, but we'll try to avoid it.
858 */
859 if (denyMode != NFS_OPEN_SHARE_DENY_NONE)
860 NP(np, "nfs_vnop_open: deny mode foregone on create, %d", kauth_cred_getuid(nofp->nof_owner->noo_cred));
861 nofp->nof_creator = NULL;
862 error = 0;
863 }
864 if (error)
865 goto out;
866 opened = 1;
867 /*
868 * If we had just created the file, we already had it open.
869 * If the actual open mode is less than what we grabbed at
870 * create time, then we'll downgrade the open here.
871 */
872 if ((nofp->nof_flags & NFS_OPEN_FILE_CREATE) &&
873 (nofp->nof_creator == current_thread())) {
874 error = nfs_close(np, nofp, NFS_OPEN_SHARE_ACCESS_BOTH, NFS_OPEN_SHARE_DENY_NONE, ctx);
875 if (error)
876 NP(np, "nfs_vnop_open: create close error %d, %d", error, kauth_cred_getuid(nofp->nof_owner->noo_cred));
877 if (!nfs_mount_state_error_should_restart(error)) {
878 error = 0;
879 nofp->nof_flags &= ~NFS_OPEN_FILE_CREATE;
880 }
881 }
882 }
883
884out:
885 if (nofp)
886 nfs_open_file_clear_busy(nofp);
887 if (nfs_mount_state_in_use_end(nmp, error)) {
888 nofp = NULL;
889 goto restart;
890 }
891 if (error)
892 NP(np, "nfs_vnop_open: error %d, %d", error, kauth_cred_getuid(noop->noo_cred));
893 if (noop)
894 nfs_open_owner_rele(noop);
316670eb
A
895 if (!error && vtype == VREG && (ap->a_mode & FWRITE)) {
896 lck_mtx_lock(&nmp->nm_lock);
897 nmp->nm_state &= ~NFSSTA_SQUISHY;
898 nmp->nm_curdeadtimeout = nmp->nm_deadtimeout;
899 if (nmp->nm_curdeadtimeout <= 0)
900 nmp->nm_deadto_start = 0;
901 nmp->nm_writers++;
902 lck_mtx_unlock(&nmp->nm_lock);
903 }
904
6d2010ae 905 return (error);
1c79356b
A
906}
907
316670eb
A
908static uint32_t
909nfs_no_of_open_file_writers(nfsnode_t np)
910{
911 uint32_t writers = 0;
912 struct nfs_open_file *nofp;
913
914 TAILQ_FOREACH(nofp, &np->n_opens, nof_link) {
915 writers += nofp->nof_w + nofp->nof_rw + nofp->nof_w_dw + nofp->nof_rw_dw +
916 nofp->nof_w_drw + nofp->nof_rw_drw + nofp->nof_d_w_dw +
917 nofp->nof_d_rw_dw + nofp->nof_d_w_drw + nofp->nof_d_rw_drw +
918 nofp->nof_d_w + nofp->nof_d_rw;
919 }
920
921 return (writers);
922}
6d2010ae 923
1c79356b 924/*
2d21ac55 925 * NFS close vnode op
6d2010ae 926 *
1c79356b
A
927 * What an NFS client should do upon close after writing is a debatable issue.
928 * Most NFS clients push delayed writes to the server upon close, basically for
929 * two reasons:
930 * 1 - So that any write errors may be reported back to the client process
931 * doing the close system call. By far the two most likely errors are
932 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
933 * 2 - To put a worst case upper bound on cache inconsistency between
934 * multiple clients for the file.
935 * There is also a consistency problem for Version 2 of the protocol w.r.t.
936 * not being able to tell if other clients are writing a file concurrently,
937 * since there is no way of knowing if the changed modify time in the reply
938 * is only due to the write for this client.
939 * (NFS Version 3 provides weak cache consistency data in the reply that
940 * should be sufficient to detect and handle this case.)
941 *
942 * The current code does the following:
943 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
6d2010ae
A
944 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate them.
945 * for NFS Version 4 - basically the same as NFSv3
1c79356b 946 */
2d21ac55 947int
6d2010ae 948nfs_vnop_close(
91447636 949 struct vnop_close_args /* {
1c79356b 950 struct vnodeop_desc *a_desc;
91447636
A
951 vnode_t a_vp;
952 int a_fflag;
953 vfs_context_t a_context;
2d21ac55 954 } */ *ap)
1c79356b 955{
2d21ac55 956 vfs_context_t ctx = ap->a_context;
91447636 957 vnode_t vp = ap->a_vp;
2d21ac55 958 nfsnode_t np = VTONFS(vp);
55e303ae 959 struct nfsmount *nmp;
6d2010ae
A
960 int error = 0, error1, nfsvers;
961 int fflag = ap->a_fflag;
962 enum vtype vtype;
963 int accessMode, denyMode;
964 struct nfs_open_owner *noop = NULL;
965 struct nfs_open_file *nofp = NULL;
1c79356b 966
2d21ac55
A
967 nmp = VTONMP(vp);
968 if (!nmp)
969 return (ENXIO);
970 nfsvers = nmp->nm_vers;
6d2010ae 971 vtype = vnode_vtype(vp);
91447636 972
6d2010ae 973 /* First, check if we need to update/flush/invalidate */
2d21ac55
A
974 if (ISSET(np->n_flag, NUPDATESIZE))
975 nfs_data_update_size(np, 0);
6d2010ae 976 nfs_node_lock_force(np);
2d21ac55 977 if (np->n_flag & NNEEDINVALIDATE) {
91447636 978 np->n_flag &= ~NNEEDINVALIDATE;
b0d623f7 979 nfs_node_unlock(np);
2d21ac55 980 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1);
6d2010ae 981 nfs_node_lock_force(np);
2d21ac55 982 }
6d2010ae
A
983 if ((vtype == VREG) && (np->n_flag & NMODIFIED) && (fflag & FWRITE)) {
984 /* we're closing an open for write and the file is modified, so flush it */
b0d623f7 985 nfs_node_unlock(np);
2d21ac55
A
986 if (nfsvers != NFS_VER2)
987 error = nfs_flush(np, MNT_WAIT, vfs_context_thread(ctx), 0);
988 else
989 error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1);
b0d623f7 990 nfs_node_lock_force(np);
91447636 991 NATTRINVALIDATE(np);
2d21ac55
A
992 }
993 if (np->n_flag & NWRITEERR) {
1c79356b
A
994 np->n_flag &= ~NWRITEERR;
995 error = np->n_error;
1c79356b 996 }
b0d623f7 997 nfs_node_unlock(np);
6d2010ae
A
998
999 if (vtype != VREG) {
1000 /* Just mark that it was closed */
1001 lck_mtx_lock(&np->n_openlock);
1002 if (np->n_openrefcnt == 0) {
1003 if (fflag & (FREAD|FWRITE)) {
1004 NP(np, "nfs_vnop_close: open reference underrun");
1005 error = EINVAL;
1006 }
1007 } else if (fflag & (FREAD|FWRITE)) {
1008 np->n_openrefcnt--;
1009 } else {
1010 /* No FREAD/FWRITE set - probably the final close */
1011 np->n_openrefcnt = 0;
1012 }
1013 lck_mtx_unlock(&np->n_openlock);
1014 return (error);
1015 }
1016 error1 = error;
1017
1018 /* fflag should contain some combination of: FREAD, FWRITE, FHASLOCK */
1019 accessMode = 0;
1020 if (fflag & FREAD)
1021 accessMode |= NFS_OPEN_SHARE_ACCESS_READ;
1022 if (fflag & FWRITE)
1023 accessMode |= NFS_OPEN_SHARE_ACCESS_WRITE;
1024// XXX It would be nice if we still had the O_EXLOCK/O_SHLOCK flags that were on the open
1025// if (fflag & O_EXLOCK)
1026// denyMode = NFS_OPEN_SHARE_DENY_BOTH;
1027// else if (fflag & O_SHLOCK)
1028// denyMode = NFS_OPEN_SHARE_DENY_WRITE;
1029// else
1030// denyMode = NFS_OPEN_SHARE_DENY_NONE;
ebb1b9f4 1031#if 0 // Not yet
6d2010ae
A
1032 if (fflag & FHASLOCK) {
1033 /* XXX assume FHASLOCK is for the deny mode and not flock */
1034 /* FHASLOCK flock will be unlocked in the close path, but the flag is not cleared. */
1035 if (nofp->nof_deny & NFS_OPEN_SHARE_DENY_READ)
1036 denyMode = NFS_OPEN_SHARE_DENY_BOTH;
1037 else if (nofp->nof_deny & NFS_OPEN_SHARE_DENY_WRITE)
1038 denyMode = NFS_OPEN_SHARE_DENY_WRITE;
1039 else
1040 denyMode = NFS_OPEN_SHARE_DENY_NONE;
1041 } else {
1042 denyMode = NFS_OPEN_SHARE_DENY_NONE;
1043 }
ebb1b9f4 1044#else
6d2010ae
A
1045 // XXX don't do deny modes just yet (and never do it for !v4)
1046 denyMode = NFS_OPEN_SHARE_DENY_NONE;
ebb1b9f4 1047#endif
6d2010ae
A
1048
1049 if (!accessMode) {
1050 /*
1051 * No mode given to close?
1052 * Guess this is the final close.
1053 * We should unlock all locks and close all opens.
1054 */
316670eb 1055 uint32_t writers;
6d2010ae 1056 mount_t mp = vnode_mount(vp);
fe8ab488 1057 int force = (!mp || vfs_isforce(mp));
316670eb
A
1058
1059 writers = nfs_no_of_open_file_writers(np);
6d2010ae 1060 nfs_release_open_state_for_node(np, force);
316670eb
A
1061 if (writers) {
1062 lck_mtx_lock(&nmp->nm_lock);
1063 if (writers > nmp->nm_writers) {
1064 NP(np, "nfs_vnop_close: number of write opens for mount underrun. Node has %d"
1065 " opens for write. Mount has total of %d opens for write\n",
1066 writers, nmp->nm_writers);
1067 nmp->nm_writers = 0;
1068 } else {
1069 nmp->nm_writers -= writers;
1070 }
1071 lck_mtx_unlock(&nmp->nm_lock);
1072 }
1073
6d2010ae 1074 return (error);
316670eb
A
1075 } else if (fflag & FWRITE) {
1076 lck_mtx_lock(&nmp->nm_lock);
1077 if (nmp->nm_writers == 0) {
1078 NP(np, "nfs_vnop_close: removing open writer from mount, but mount has no files open for writing");
1079 } else {
1080 nmp->nm_writers--;
1081 }
1082 lck_mtx_unlock(&nmp->nm_lock);
6d2010ae 1083 }
316670eb 1084
6d2010ae
A
1085
1086 noop = nfs_open_owner_find(nmp, vfs_context_ucred(ctx), 0);
1087 if (!noop) {
1088 // printf("nfs_vnop_close: can't get open owner!\n");
1089 return (EIO);
1090 }
1091
1092restart:
1093 error = nfs_mount_state_in_use_start(nmp, NULL);
1094 if (error) {
1095 nfs_open_owner_rele(noop);
1096 return (error);
1097 }
1098
1099 error = nfs_open_file_find(np, noop, &nofp, 0, 0, 0);
1100 if (!error && (nofp->nof_flags & NFS_OPEN_FILE_REOPEN)) {
1101 nfs_mount_state_in_use_end(nmp, 0);
1102 error = nfs4_reopen(nofp, NULL);
1103 nofp = NULL;
1104 if (!error)
1105 goto restart;
1106 }
1107 if (error) {
1108 NP(np, "nfs_vnop_close: no open file for owner, error %d, %d", error, kauth_cred_getuid(noop->noo_cred));
1109 error = EBADF;
1110 goto out;
1111 }
1112 error = nfs_open_file_set_busy(nofp, NULL);
1113 if (error) {
1114 nofp = NULL;
1115 goto out;
1116 }
1117
1118 error = nfs_close(np, nofp, accessMode, denyMode, ctx);
1119 if (error)
1120 NP(np, "nfs_vnop_close: close error %d, %d", error, kauth_cred_getuid(noop->noo_cred));
1121
1122out:
1123 if (nofp)
1124 nfs_open_file_clear_busy(nofp);
1125 if (nfs_mount_state_in_use_end(nmp, error)) {
1126 nofp = NULL;
1127 goto restart;
1128 }
1129 if (!error)
1130 error = error1;
1131 if (error)
1132 NP(np, "nfs_vnop_close: error %d, %d", error, kauth_cred_getuid(noop->noo_cred));
1133 if (noop)
1134 nfs_open_owner_rele(noop);
1c79356b
A
1135 return (error);
1136}
1137
6d2010ae
A
1138/*
1139 * nfs_close(): common function that does all the heavy lifting of file closure
1140 *
1141 * Takes an open file structure and a set of access/deny modes and figures out how
1142 * to update the open file structure (and the state on the server) appropriately.
1143 */
1144int
1145nfs_close(
1146 nfsnode_t np,
1147 struct nfs_open_file *nofp,
1148 uint32_t accessMode,
1149 uint32_t denyMode,
1150 vfs_context_t ctx)
1151{
1152 struct nfs_lock_owner *nlop;
1153 int error = 0, changed = 0, delegated = 0, closed = 0, downgrade = 0;
1154 uint32_t newAccessMode, newDenyMode;
316670eb 1155
6d2010ae
A
1156 /* warn if modes don't match current state */
1157 if (((accessMode & nofp->nof_access) != accessMode) || ((denyMode & nofp->nof_deny) != denyMode))
1158 NP(np, "nfs_close: mode mismatch %d %d, current %d %d, %d",
1159 accessMode, denyMode, nofp->nof_access, nofp->nof_deny,
1160 kauth_cred_getuid(nofp->nof_owner->noo_cred));
1161
1162 /*
1163 * If we're closing a write-only open, we may not have a write-only count
1164 * if we also grabbed read access. So, check the read-write count.
1165 */
1166 if (denyMode == NFS_OPEN_SHARE_DENY_NONE) {
1167 if ((accessMode == NFS_OPEN_SHARE_ACCESS_WRITE) &&
1168 (nofp->nof_w == 0) && (nofp->nof_d_w == 0) &&
1169 (nofp->nof_rw || nofp->nof_d_rw))
1170 accessMode = NFS_OPEN_SHARE_ACCESS_BOTH;
1171 } else if (denyMode == NFS_OPEN_SHARE_DENY_WRITE) {
1172 if ((accessMode == NFS_OPEN_SHARE_ACCESS_WRITE) &&
1173 (nofp->nof_w_dw == 0) && (nofp->nof_d_w_dw == 0) &&
1174 (nofp->nof_rw_dw || nofp->nof_d_rw_dw))
1175 accessMode = NFS_OPEN_SHARE_ACCESS_BOTH;
1176 } else { /* NFS_OPEN_SHARE_DENY_BOTH */
1177 if ((accessMode == NFS_OPEN_SHARE_ACCESS_WRITE) &&
1178 (nofp->nof_w_drw == 0) && (nofp->nof_d_w_drw == 0) &&
1179 (nofp->nof_rw_drw || nofp->nof_d_rw_drw))
1180 accessMode = NFS_OPEN_SHARE_ACCESS_BOTH;
1181 }
1182
1183 nfs_open_file_remove_open_find(nofp, accessMode, denyMode, &newAccessMode, &newDenyMode, &delegated);
1184 if ((newAccessMode != nofp->nof_access) || (newDenyMode != nofp->nof_deny))
1185 changed = 1;
1186 else
1187 changed = 0;
1188
1189 if (NFSTONMP(np)->nm_vers < NFS_VER4) /* NFS v2/v3 closes simply need to remove the open. */
1190 goto v3close;
1191
1192 if ((newAccessMode == 0) || (nofp->nof_opencnt == 1)) {
1193 /*
1194 * No more access after this close, so clean up and close it.
1195 * Don't send a close RPC if we're closing a delegated open.
1196 */
1197 nfs_wait_bufs(np);
1198 closed = 1;
1199 if (!delegated && !(nofp->nof_flags & NFS_OPEN_FILE_LOST))
1200 error = nfs4_close_rpc(np, nofp, vfs_context_thread(ctx), vfs_context_ucred(ctx), 0);
1201 if (error == NFSERR_LOCKS_HELD) {
1202 /*
1203 * Hmm... the server says we have locks we need to release first
1204 * Find the lock owner and try to unlock everything.
1205 */
1206 nlop = nfs_lock_owner_find(np, vfs_context_proc(ctx), 0);
1207 if (nlop) {
1208 nfs4_unlock_rpc(np, nlop, F_WRLCK, 0, UINT64_MAX,
1209 0, vfs_context_thread(ctx), vfs_context_ucred(ctx));
1210 nfs_lock_owner_rele(nlop);
1211 }
1212 error = nfs4_close_rpc(np, nofp, vfs_context_thread(ctx), vfs_context_ucred(ctx), 0);
1213 }
1214 } else if (changed) {
1215 /*
1216 * File is still open but with less access, so downgrade the open.
1217 * Don't send a downgrade RPC if we're closing a delegated open.
1218 */
1219 if (!delegated && !(nofp->nof_flags & NFS_OPEN_FILE_LOST)) {
1220 downgrade = 1;
1221 /*
1222 * If we have delegated opens, we should probably claim them before sending
1223 * the downgrade because the server may not know the open we are downgrading to.
1224 */
1225 if (nofp->nof_d_rw_drw || nofp->nof_d_w_drw || nofp->nof_d_r_drw ||
1226 nofp->nof_d_rw_dw || nofp->nof_d_w_dw || nofp->nof_d_r_dw ||
1227 nofp->nof_d_rw || nofp->nof_d_w || nofp->nof_d_r)
1228 nfs4_claim_delegated_state_for_open_file(nofp, 0);
1229 /* need to remove the open before sending the downgrade */
1230 nfs_open_file_remove_open(nofp, accessMode, denyMode);
1231 error = nfs4_open_downgrade_rpc(np, nofp, ctx);
1232 if (error) /* Hmm.. that didn't work. Add the open back in. */
1233 nfs_open_file_add_open(nofp, accessMode, denyMode, delegated);
1234 }
1235 }
1236
1237 if (error) {
1238 NP(np, "nfs_close: error %d, %d", error, kauth_cred_getuid(nofp->nof_owner->noo_cred));
1239 return (error);
1240 }
1241
1242v3close:
1243 if (!downgrade)
1244 nfs_open_file_remove_open(nofp, accessMode, denyMode);
1245
1246 if (closed) {
1247 lck_mtx_lock(&nofp->nof_lock);
1248 if (nofp->nof_r || nofp->nof_d_r || nofp->nof_w || nofp->nof_d_w || nofp->nof_d_rw ||
1249 (nofp->nof_rw && !((nofp->nof_flags & NFS_OPEN_FILE_CREATE) && !nofp->nof_creator && (nofp->nof_rw == 1))) ||
1250 nofp->nof_r_dw || nofp->nof_d_r_dw || nofp->nof_w_dw || nofp->nof_d_w_dw ||
1251 nofp->nof_rw_dw || nofp->nof_d_rw_dw || nofp->nof_r_drw || nofp->nof_d_r_drw ||
1252 nofp->nof_w_drw || nofp->nof_d_w_drw || nofp->nof_rw_drw || nofp->nof_d_rw_drw)
1253 NP(np, "nfs_close: unexpected count: %u.%u %u.%u %u.%u dw %u.%u %u.%u %u.%u drw %u.%u %u.%u %u.%u flags 0x%x, %d",
1254 nofp->nof_r, nofp->nof_d_r, nofp->nof_w, nofp->nof_d_w,
1255 nofp->nof_rw, nofp->nof_d_rw, nofp->nof_r_dw, nofp->nof_d_r_dw,
1256 nofp->nof_w_dw, nofp->nof_d_w_dw, nofp->nof_rw_dw, nofp->nof_d_rw_dw,
1257 nofp->nof_r_drw, nofp->nof_d_r_drw, nofp->nof_w_drw, nofp->nof_d_w_drw,
1258 nofp->nof_rw_drw, nofp->nof_d_rw_drw, nofp->nof_flags,
1259 kauth_cred_getuid(nofp->nof_owner->noo_cred));
1260 /* clear out all open info, just to be safe */
1261 nofp->nof_access = nofp->nof_deny = 0;
1262 nofp->nof_mmap_access = nofp->nof_mmap_deny = 0;
1263 nofp->nof_r = nofp->nof_d_r = 0;
1264 nofp->nof_w = nofp->nof_d_w = 0;
1265 nofp->nof_rw = nofp->nof_d_rw = 0;
1266 nofp->nof_r_dw = nofp->nof_d_r_dw = 0;
1267 nofp->nof_w_dw = nofp->nof_d_w_dw = 0;
1268 nofp->nof_rw_dw = nofp->nof_d_rw_dw = 0;
1269 nofp->nof_r_drw = nofp->nof_d_r_drw = 0;
1270 nofp->nof_w_drw = nofp->nof_d_w_drw = 0;
1271 nofp->nof_rw_drw = nofp->nof_d_rw_drw = 0;
1272 nofp->nof_flags &= ~NFS_OPEN_FILE_CREATE;
1273 lck_mtx_unlock(&nofp->nof_lock);
1274 /* XXX we may potentially want to clean up idle/unused open file structures */
1275 }
1276 if (nofp->nof_flags & NFS_OPEN_FILE_LOST) {
1277 error = EIO;
1278 NP(np, "nfs_close: LOST%s, %d", !nofp->nof_opencnt ? " (last)" : "",
1279 kauth_cred_getuid(nofp->nof_owner->noo_cred));
1280 }
316670eb 1281
6d2010ae
A
1282 return (error);
1283}
1284
1285
91447636 1286int
2d21ac55
A
1287nfs3_getattr_rpc(
1288 nfsnode_t np,
91447636
A
1289 mount_t mp,
1290 u_char *fhp,
2d21ac55 1291 size_t fhsize,
6d2010ae 1292 int flags,
2d21ac55 1293 vfs_context_t ctx,
91447636
A
1294 struct nfs_vattr *nvap,
1295 u_int64_t *xidp)
1296{
2d21ac55 1297 struct nfsmount *nmp = mp ? VFSTONFS(mp) : NFSTONMP(np);
6d2010ae 1298 int error = 0, status, nfsvers, rpcflags = 0;
2d21ac55 1299 struct nfsm_chain nmreq, nmrep;
91447636 1300
fe8ab488 1301 if (nfs_mount_gone(nmp))
2d21ac55
A
1302 return (ENXIO);
1303 nfsvers = nmp->nm_vers;
1304
6d2010ae
A
1305 if (flags & NGA_MONITOR) /* vnode monitor requests should be soft */
1306 rpcflags = R_RECOVER;
1307
fe8ab488
A
1308 if (flags & NGA_SOFT) /* Return ETIMEDOUT if server not responding */
1309 rpcflags |= R_SOFT;
1310
2d21ac55
A
1311 nfsm_chain_null(&nmreq);
1312 nfsm_chain_null(&nmrep);
1313
1314 nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(nfsvers));
1315 if (nfsvers != NFS_VER2)
1316 nfsm_chain_add_32(error, &nmreq, fhsize);
1317 nfsm_chain_add_opaque(error, &nmreq, fhp, fhsize);
1318 nfsm_chain_build_done(error, &nmreq);
1319 nfsmout_if(error);
6d2010ae
A
1320 error = nfs_request2(np, mp, &nmreq, NFSPROC_GETATTR,
1321 vfs_context_thread(ctx), vfs_context_ucred(ctx),
1322 NULL, rpcflags, &nmrep, xidp, &status);
2d21ac55
A
1323 if (!error)
1324 error = status;
1325 nfsmout_if(error);
1326 error = nfs_parsefattr(&nmrep, nfsvers, nvap);
1327nfsmout:
1328 nfsm_chain_cleanup(&nmreq);
1329 nfsm_chain_cleanup(&nmrep);
91447636
A
1330 return (error);
1331}
1332
fe8ab488
A
1333/*
1334 * nfs_refresh_fh will attempt to update the file handle for the node.
1335 *
1336 * It only does this for symbolic links and regular files that are not currently opened.
1337 *
1338 * On Success returns 0 and the nodes file handle is updated, or ESTALE on failure.
1339 */
1340int
1341nfs_refresh_fh(nfsnode_t np, vfs_context_t ctx)
1342{
1343 vnode_t dvp, vp = NFSTOV(np);
1344 nfsnode_t dnp;
1345 const char *v_name = vnode_getname(vp);
1346 char *name;
1347 int namelen, fhsize, refreshed;
1348 int error, wanted = 0;
1349 uint8_t *fhp;
1350 struct timespec ts = {2, 0};
1351
1352 NFS_VNOP_DBG("vnode is %d\n", vnode_vtype(vp));
1353
1354 dvp = vnode_parent(vp);
1355 if ((vnode_vtype(vp) != VREG && vnode_vtype(vp) != VLNK) ||
1356 v_name == NULL || *v_name == '\0' || dvp == NULL) {
1357 if (v_name != NULL)
1358 vnode_putname(v_name);
1359 return (ESTALE);
1360 }
1361 dnp = VTONFS(dvp);
1362
1363 namelen = strlen(v_name);
1364 MALLOC(name, char *, namelen + 1, M_TEMP, M_WAITOK);
1365 if (name == NULL) {
1366 vnode_putname(v_name);
1367 return (ESTALE);
1368 }
1369 bcopy(v_name, name, namelen+1);
1370 NFS_VNOP_DBG("Trying to refresh %s : %s\n", v_name, name);
1371 vnode_putname(v_name);
1372
1373 /* Allocate the maximum size file handle */
1374 MALLOC(fhp, uint8_t *, NFS4_FHSIZE, M_TEMP, M_WAITOK);
1375 if (fhp == NULL) {
1376 FREE(name, M_TEMP);
1377 return (ESTALE);
1378 }
1379
1380 if ((error = nfs_node_lock(np))) {
1381 FREE(name, M_TEMP);
1382 FREE(fhp, M_TEMP);
1383 return (ESTALE);
1384 }
1385
1386 fhsize = np->n_fhsize;
1387 bcopy(np->n_fhp, fhp, fhsize);
1388 while (ISSET(np->n_flag, NREFRESH)) {
1389 SET(np->n_flag, NREFRESHWANT);
1390 NFS_VNOP_DBG("Waiting for refresh of %s\n", name);
1391 msleep(np, &np->n_lock, PZERO-1, "nfsrefreshwant", &ts);
1392 if ((error = nfs_sigintr(NFSTONMP(np), NULL, vfs_context_thread(ctx), 0)))
1393 break;
1394 }
1395 refreshed = error ? 0 : !NFS_CMPFH(np, fhp, fhsize);
1396 SET(np->n_flag, NREFRESH);
1397 nfs_node_unlock(np);
1398
1399 NFS_VNOP_DBG("error = %d, refreshed = %d\n", error, refreshed);
1400 if (error || refreshed)
1401 goto nfsmout;
1402
1403 /* Check that there are no open references for this file */
1404 lck_mtx_lock(&np->n_openlock);
1405 if (np->n_openrefcnt || !TAILQ_EMPTY(&np->n_opens) || !TAILQ_EMPTY(&np->n_lock_owners)) {
1406 int cnt = 0;
1407 struct nfs_open_file *ofp;
1408
1409 TAILQ_FOREACH(ofp, &np->n_opens, nof_link) {
1410 cnt += ofp->nof_opencnt;
1411 }
1412 if (cnt) {
1413 lck_mtx_unlock(&np->n_openlock);
1414 NFS_VNOP_DBG("Can not refresh file handle for %s with open state\n", name);
1415 NFS_VNOP_DBG("\topenrefcnt = %d, opens = %d lock_owners = %d\n",
1416 np->n_openrefcnt, cnt, !TAILQ_EMPTY(&np->n_lock_owners));
1417 error = ESTALE;
1418 goto nfsmout;
1419 }
1420 }
1421 lck_mtx_unlock(&np->n_openlock);
1422 /*
1423 * Since the FH is currently stale we should not be able to
1424 * establish any open state until the FH is refreshed.
1425 */
1426
1427 error = nfs_node_lock(np);
1428 nfsmout_if(error);
1429 /*
1430 * Symlinks should never need invalidations and are holding
1431 * the one and only nfsbuf in an uncached acquired state
1432 * trying to do a readlink. So we will hang if we invalidate
1433 * in that case. Only in in the VREG case do we need to
1434 * invalidate.
1435 */
1436 if (vnode_vtype(vp) == VREG) {
1437 np->n_flag &= ~NNEEDINVALIDATE;
1438 nfs_node_unlock(np);
1439 error = nfs_vinvalbuf(vp, V_IGNORE_WRITEERR, ctx, 1);
1440 if (error)
1441 NFS_VNOP_DBG("nfs_vinvalbuf returned %d\n", error);
1442 nfsmout_if(error);
1443 } else {
1444 nfs_node_unlock(np);
1445 }
1446
1447 NFS_VNOP_DBG("Looking up %s\n", name);
1448 error = nfs_lookitup(dnp, name, namelen, ctx, &np);
1449 if (error)
1450 NFS_VNOP_DBG("nfs_lookitup returned %d\n", error);
1451
1452nfsmout:
1453 nfs_node_lock_force(np);
1454 wanted = ISSET(np->n_flag, NREFRESHWANT);
1455 CLR(np->n_flag, NREFRESH|NREFRESHWANT);
1456 nfs_node_unlock(np);
1457 if (wanted)
1458 wakeup(np);
1459
1460 if (error == 0)
1461 NFS_VNOP_DBG("%s refreshed file handle\n", name);
1462
1463 FREE(name, M_TEMP);
1464 FREE(fhp, M_TEMP);
1465
1466 return (error ? ESTALE : 0);
1467}
2d21ac55 1468
91447636 1469int
6d2010ae 1470nfs_getattr(nfsnode_t np, struct nfs_vattr *nvap, vfs_context_t ctx, int flags)
fe8ab488
A
1471{
1472 int error;
1473
1474retry:
1475 error = nfs_getattr_internal(np, nvap, ctx, flags);
1476 if (error == ESTALE) {
1477 error = nfs_refresh_fh(np, ctx);
1478 if (!error)
1479 goto retry;
1480 }
1481 return (error);
1482}
1483
1484int
1485nfs_getattr_internal(nfsnode_t np, struct nfs_vattr *nvap, vfs_context_t ctx, int flags)
1c79356b 1486{
2d21ac55 1487 struct nfsmount *nmp;
b0d623f7 1488 int error = 0, nfsvers, inprogset = 0, wanted = 0, avoidfloods;
6d2010ae 1489 struct nfs_vattr nvattr;
b0d623f7 1490 struct timespec ts = { 2, 0 };
fa4905b1 1491 u_int64_t xid;
91447636
A
1492
1493 FSDBG_TOP(513, np->n_size, np, np->n_vattr.nva_size, np->n_flag);
1494
fe8ab488
A
1495 nmp = NFSTONMP(np);
1496
1497 if (nfs_mount_gone(nmp))
b0d623f7
A
1498 return (ENXIO);
1499 nfsvers = nmp->nm_vers;
1500
6d2010ae
A
1501 if (!nvap)
1502 nvap = &nvattr;
1503 NVATTR_INIT(nvap);
1504
2d21ac55
A
1505 /* Update local times for special files. */
1506 if (np->n_flag & (NACC | NUPD)) {
b0d623f7 1507 nfs_node_lock_force(np);
1c79356b 1508 np->n_flag |= NCHG;
b0d623f7 1509 nfs_node_unlock(np);
2d21ac55
A
1510 }
1511 /* Update size, if necessary */
b0d623f7 1512 if (ISSET(np->n_flag, NUPDATESIZE))
2d21ac55
A
1513 nfs_data_update_size(np, 0);
1514
b0d623f7
A
1515 error = nfs_node_lock(np);
1516 nfsmout_if(error);
6d2010ae
A
1517 if (!(flags & (NGA_UNCACHED|NGA_MONITOR)) || ((nfsvers >= NFS_VER4) && (np->n_openflags & N_DELEG_MASK))) {
1518 /*
1519 * Use the cache or wait for any getattr in progress if:
1520 * - it's a cached request, or
fe8ab488
A
1521 * - we have a delegation, or
1522 * - the server isn't responding
6d2010ae 1523 */
b0d623f7 1524 while (1) {
6d2010ae 1525 error = nfs_getattrcache(np, nvap, flags);
b0d623f7
A
1526 if (!error || (error != ENOENT)) {
1527 nfs_node_unlock(np);
1528 goto nfsmout;
1529 }
6d2010ae 1530 error = 0;
b0d623f7
A
1531 if (!ISSET(np->n_flag, NGETATTRINPROG))
1532 break;
6d2010ae
A
1533 if (flags & NGA_MONITOR) {
1534 /* no need to wait if a request is pending */
1535 error = EINPROGRESS;
1536 nfs_node_unlock(np);
1537 goto nfsmout;
1538 }
b0d623f7
A
1539 SET(np->n_flag, NGETATTRWANT);
1540 msleep(np, &np->n_lock, PZERO-1, "nfsgetattrwant", &ts);
1541 if ((error = nfs_sigintr(NFSTONMP(np), NULL, vfs_context_thread(ctx), 0))) {
1542 nfs_node_unlock(np);
1543 goto nfsmout;
1544 }
1545 }
1546 SET(np->n_flag, NGETATTRINPROG);
1547 inprogset = 1;
1548 } else if (!ISSET(np->n_flag, NGETATTRINPROG)) {
1549 SET(np->n_flag, NGETATTRINPROG);
1550 inprogset = 1;
6d2010ae
A
1551 } else if (flags & NGA_MONITOR) {
1552 /* no need to make a request if one is pending */
1553 error = EINPROGRESS;
b0d623f7
A
1554 }
1555 nfs_node_unlock(np);
55e303ae 1556
2d21ac55 1557 nmp = NFSTONMP(np);
fe8ab488 1558 if (nfs_mount_gone(nmp))
2d21ac55 1559 error = ENXIO;
6d2010ae 1560 if (error)
2d21ac55 1561 goto nfsmout;
fa4905b1 1562
fe8ab488
A
1563 /*
1564 * Return cached attributes if they are valid,
1565 * if the server doesn't respond, and this is
1566 * some softened up style of mount.
1567 */
1568 if (NATTRVALID(np) && nfs_use_cache(nmp))
1569 flags |= NGA_SOFT;
1570
91447636 1571 /*
6d2010ae
A
1572 * We might want to try to get both the attributes and access info by
1573 * making an ACCESS call and seeing if it returns updated attributes.
91447636
A
1574 * But don't bother if we aren't caching access info or if the
1575 * attributes returned wouldn't be cached.
1576 */
6d2010ae 1577 if (!(flags & NGA_ACL) && (nfsvers != NFS_VER2) && nfs_access_for_getattr && (nfs_access_cache_timeout > 0)) {
2d21ac55 1578 if (nfs_attrcachetimeout(np) > 0) {
b0d623f7 1579 /* OSAddAtomic(1, &nfsstats.accesscache_misses); */
6d2010ae 1580 u_int32_t access = NFS_ACCESS_ALL;
fe8ab488
A
1581 int rpcflags = 0;
1582
1583 /* Return cached attrs if server doesn't respond */
1584 if (flags & NGA_SOFT)
1585 rpcflags |= R_SOFT;
1586
1587 error = nmp->nm_funcs->nf_access_rpc(np, &access, rpcflags, ctx);
1588
1589 if (error == ETIMEDOUT)
1590 goto returncached;
1591
2d21ac55
A
1592 if (error)
1593 goto nfsmout;
b0d623f7 1594 nfs_node_lock_force(np);
6d2010ae 1595 error = nfs_getattrcache(np, nvap, flags);
b0d623f7
A
1596 nfs_node_unlock(np);
1597 if (!error || (error != ENOENT))
2d21ac55 1598 goto nfsmout;
b0d623f7 1599 /* Well, that didn't work... just do a getattr... */
2d21ac55
A
1600 error = 0;
1601 }
1c79356b 1602 }
b0d623f7 1603
fa4905b1 1604 avoidfloods = 0;
fe8ab488 1605
fa4905b1 1606tryagain:
6d2010ae 1607 error = nmp->nm_funcs->nf_getattr_rpc(np, NULL, np->n_fhp, np->n_fhsize, flags, ctx, nvap, &xid);
b0d623f7
A
1608 if (!error) {
1609 nfs_node_lock_force(np);
1610 error = nfs_loadattrcache(np, nvap, &xid, 0);
1611 nfs_node_unlock(np);
1612 }
fe8ab488
A
1613
1614 /*
1615 * If the server didn't respond, return cached attributes.
1616 */
1617returncached:
1618 if ((flags & NGA_SOFT) && (error == ETIMEDOUT)) {
1619 nfs_node_lock_force(np);
1620 error = nfs_getattrcache(np, nvap, flags);
1621 if (!error || (error != ENOENT)) {
1622 nfs_node_unlock(np);
1623 goto nfsmout;
1624 }
1625 nfs_node_unlock(np);
1626 }
2d21ac55 1627 nfsmout_if(error);
fe8ab488 1628
2d21ac55
A
1629 if (!xid) { /* out-of-order rpc - attributes were dropped */
1630 FSDBG(513, -1, np, np->n_xid >> 32, np->n_xid);
b0d623f7 1631 if (avoidfloods++ < 20)
2d21ac55 1632 goto tryagain;
b0d623f7
A
1633 /* avoidfloods>1 is bizarre. at 20 pull the plug */
1634 /* just return the last attributes we got */
1635 }
1636nfsmout:
1637 nfs_node_lock_force(np);
1638 if (inprogset) {
1639 wanted = ISSET(np->n_flag, NGETATTRWANT);
1640 CLR(np->n_flag, (NGETATTRINPROG | NGETATTRWANT));
91447636 1641 }
b0d623f7
A
1642 if (!error) {
1643 /* check if the node changed on us */
2d21ac55
A
1644 vnode_t vp = NFSTOV(np);
1645 enum vtype vtype = vnode_vtype(vp);
b0d623f7
A
1646 if ((vtype == VDIR) && NFS_CHANGED_NC(nfsvers, np, nvap)) {
1647 FSDBG(513, -1, np, 0, np);
1648 np->n_flag &= ~NNEGNCENTRIES;
1649 cache_purge(vp);
1650 np->n_ncgen++;
1651 NFS_CHANGED_UPDATE_NC(nfsvers, np, nvap);
39236c6e
A
1652 NFS_VNOP_DBG("Purge directory 0x%llx\n",
1653 (uint64_t)VM_KERNEL_ADDRPERM(vp));
2d21ac55 1654 }
b0d623f7
A
1655 if (NFS_CHANGED(nfsvers, np, nvap)) {
1656 FSDBG(513, -1, np, -1, np);
39236c6e
A
1657 if (vtype == VDIR) {
1658 NFS_VNOP_DBG("Invalidate directory 0x%llx\n",
1659 (uint64_t)VM_KERNEL_ADDRPERM(vp));
b0d623f7 1660 nfs_invaldir(np);
39236c6e 1661 }
b0d623f7
A
1662 nfs_node_unlock(np);
1663 if (wanted)
1664 wakeup(np);
2d21ac55 1665 error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1);
fa4905b1 1666 FSDBG(513, -1, np, -2, error);
55e303ae 1667 if (!error) {
b0d623f7 1668 nfs_node_lock_force(np);
2d21ac55 1669 NFS_CHANGED_UPDATE(nfsvers, np, nvap);
b0d623f7 1670 nfs_node_unlock(np);
55e303ae 1671 }
2d21ac55 1672 } else {
b0d623f7
A
1673 nfs_node_unlock(np);
1674 if (wanted)
1675 wakeup(np);
1c79356b 1676 }
b0d623f7
A
1677 } else {
1678 nfs_node_unlock(np);
1679 if (wanted)
1680 wakeup(np);
1c79356b 1681 }
6d2010ae
A
1682
1683 if (nvap == &nvattr) {
1684 NVATTR_CLEANUP(nvap);
1685 } else if (!(flags & NGA_ACL)) {
1686 /* make sure we don't return an ACL if it wasn't asked for */
1687 NFS_BITMAP_CLR(nvap->nva_bitmap, NFS_FATTR_ACL);
1688 if (nvap->nva_acl) {
1689 kauth_acl_free(nvap->nva_acl);
1690 nvap->nva_acl = NULL;
1691 }
1692 }
2d21ac55 1693 FSDBG_BOT(513, np->n_size, error, np->n_vattr.nva_size, np->n_flag);
91447636
A
1694 return (error);
1695}
1696
2d21ac55
A
1697/*
1698 * NFS getattr call from vfs.
1699 */
39236c6e
A
1700
1701/*
1702 * The attributes we support over the wire.
1703 * We also get fsid but the vfs layer gets it out of the mount
1704 * structure after this calling us so there's no need to return it,
1705 * and Finder expects to call getattrlist just looking for the FSID
1706 * with out hanging on a non responsive server.
1707 */
1708#define NFS3_SUPPORTED_VATTRS \
1709 (VNODE_ATTR_va_rdev | \
1710 VNODE_ATTR_va_nlink | \
1711 VNODE_ATTR_va_data_size | \
1712 VNODE_ATTR_va_data_alloc | \
1713 VNODE_ATTR_va_uid | \
1714 VNODE_ATTR_va_gid | \
1715 VNODE_ATTR_va_mode | \
1716 VNODE_ATTR_va_modify_time | \
1717 VNODE_ATTR_va_change_time | \
1718 VNODE_ATTR_va_access_time | \
1719 VNODE_ATTR_va_fileid | \
1720 VNODE_ATTR_va_type)
1721
b0d623f7 1722int
2d21ac55 1723nfs3_vnop_getattr(
91447636
A
1724 struct vnop_getattr_args /* {
1725 struct vnodeop_desc *a_desc;
1726 vnode_t a_vp;
1727 struct vnode_attr *a_vap;
1728 vfs_context_t a_context;
1729 } */ *ap)
1730{
1731 int error;
1732 struct nfs_vattr nva;
1733 struct vnode_attr *vap = ap->a_vap;
2d21ac55 1734 dev_t rdev;
91447636 1735
39236c6e
A
1736 /*
1737 * Lets don't go over the wire if we don't support any of the attributes.
1738 * Just fall through at the VFS layer and let it cons up what it needs.
1739 */
1740 /* Return the io size no matter what, since we don't go over the wire for this */
1741 VATTR_RETURN(vap, va_iosize, nfs_iosize);
1742 if ((vap->va_active & NFS3_SUPPORTED_VATTRS) == 0)
1743 return (0);
1744
1745 if (VATTR_IS_ACTIVE(ap->a_vap, va_name))
1746 NFS_VNOP_DBG("Getting attrs for 0x%llx, vname is %s\n",
1747 (uint64_t)VM_KERNEL_ADDRPERM(ap->a_vp),
1748 ap->a_vp->v_name ? ap->a_vp->v_name : "empty");
b0d623f7 1749 error = nfs_getattr(VTONFS(ap->a_vp), &nva, ap->a_context, NGA_CACHED);
91447636
A
1750 if (error)
1751 return (error);
1752
1753 /* copy nva to *a_vap */
2d21ac55
A
1754 VATTR_RETURN(vap, va_type, nva.nva_type);
1755 VATTR_RETURN(vap, va_mode, nva.nva_mode);
1756 rdev = makedev(nva.nva_rawdev.specdata1, nva.nva_rawdev.specdata2);
1757 VATTR_RETURN(vap, va_rdev, rdev);
1758 VATTR_RETURN(vap, va_uid, nva.nva_uid);
1759 VATTR_RETURN(vap, va_gid, nva.nva_gid);
1760 VATTR_RETURN(vap, va_nlink, nva.nva_nlink);
1761 VATTR_RETURN(vap, va_fileid, nva.nva_fileid);
1762 VATTR_RETURN(vap, va_data_size, nva.nva_size);
1763 VATTR_RETURN(vap, va_data_alloc, nva.nva_bytes);
2d21ac55
A
1764 vap->va_access_time.tv_sec = nva.nva_timesec[NFSTIME_ACCESS];
1765 vap->va_access_time.tv_nsec = nva.nva_timensec[NFSTIME_ACCESS];
1766 VATTR_SET_SUPPORTED(vap, va_access_time);
1767 vap->va_modify_time.tv_sec = nva.nva_timesec[NFSTIME_MODIFY];
1768 vap->va_modify_time.tv_nsec = nva.nva_timensec[NFSTIME_MODIFY];
1769 VATTR_SET_SUPPORTED(vap, va_modify_time);
1770 vap->va_change_time.tv_sec = nva.nva_timesec[NFSTIME_CHANGE];
1771 vap->va_change_time.tv_nsec = nva.nva_timensec[NFSTIME_CHANGE];
1772 VATTR_SET_SUPPORTED(vap, va_change_time);
91447636 1773
2d21ac55 1774 // VATTR_RETURN(vap, va_encoding, 0xffff /* kTextEncodingUnknown */);
1c79356b
A
1775 return (error);
1776}
1777
1778/*
2d21ac55 1779 * NFS setattr call.
1c79356b 1780 */
b0d623f7 1781int
2d21ac55 1782nfs_vnop_setattr(
91447636 1783 struct vnop_setattr_args /* {
1c79356b 1784 struct vnodeop_desc *a_desc;
91447636
A
1785 vnode_t a_vp;
1786 struct vnode_attr *a_vap;
1787 vfs_context_t a_context;
2d21ac55 1788 } */ *ap)
1c79356b 1789{
2d21ac55 1790 vfs_context_t ctx = ap->a_context;
91447636 1791 vnode_t vp = ap->a_vp;
2d21ac55 1792 nfsnode_t np = VTONFS(vp);
0c530ab8 1793 struct nfsmount *nmp;
91447636 1794 struct vnode_attr *vap = ap->a_vap;
1c79356b 1795 int error = 0;
6d2010ae
A
1796 int biosize, nfsvers, namedattrs;
1797 u_quad_t origsize, vapsize;
2d21ac55
A
1798 struct nfs_dulookup dul;
1799 nfsnode_t dnp = NULL;
813fb2f6 1800 int dul_in_progress = 0;
2d21ac55
A
1801 vnode_t dvp = NULL;
1802 const char *vname = NULL;
b0d623f7
A
1803 struct nfs_open_owner *noop = NULL;
1804 struct nfs_open_file *nofp = NULL;
2d21ac55
A
1805
1806 nmp = VTONMP(vp);
fe8ab488 1807 if (nfs_mount_gone(nmp))
0c530ab8 1808 return (ENXIO);
2d21ac55 1809 nfsvers = nmp->nm_vers;
6d2010ae 1810 namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR);
0c530ab8 1811 biosize = nmp->nm_biosize;
fa4905b1 1812
91447636 1813 /* Disallow write attempts if the filesystem is mounted read-only. */
2d21ac55 1814 if (vnode_vfsisrdonly(vp))
1c79356b 1815 return (EROFS);
91447636 1816
2d21ac55 1817 origsize = np->n_size;
91447636 1818 if (VATTR_IS_ACTIVE(vap, va_data_size)) {
2d21ac55
A
1819 switch (vnode_vtype(vp)) {
1820 case VDIR:
1821 return (EISDIR);
1822 case VCHR:
1823 case VBLK:
1824 case VSOCK:
1825 case VFIFO:
91447636
A
1826 if (!VATTR_IS_ACTIVE(vap, va_modify_time) &&
1827 !VATTR_IS_ACTIVE(vap, va_access_time) &&
1828 !VATTR_IS_ACTIVE(vap, va_mode) &&
1829 !VATTR_IS_ACTIVE(vap, va_uid) &&
2d21ac55 1830 !VATTR_IS_ACTIVE(vap, va_gid)) {
1c79356b 1831 return (0);
2d21ac55 1832 }
91447636 1833 VATTR_CLEAR_ACTIVE(vap, va_data_size);
2d21ac55
A
1834 break;
1835 default:
1c79356b
A
1836 /*
1837 * Disallow write attempts if the filesystem is
1838 * mounted read-only.
1839 */
91447636 1840 if (vnode_vfsisrdonly(vp))
1c79356b 1841 return (EROFS);
91447636
A
1842 FSDBG_TOP(512, np->n_size, vap->va_data_size,
1843 np->n_vattr.nva_size, np->n_flag);
2d21ac55 1844 /* clear NNEEDINVALIDATE, if set */
b0d623f7 1845 if ((error = nfs_node_lock(np)))
2d21ac55
A
1846 return (error);
1847 if (np->n_flag & NNEEDINVALIDATE)
1848 np->n_flag &= ~NNEEDINVALIDATE;
b0d623f7 1849 nfs_node_unlock(np);
2d21ac55
A
1850 /* flush everything */
1851 error = nfs_vinvalbuf(vp, (vap->va_data_size ? V_SAVE : 0) , ctx, 1);
1852 if (error) {
6d2010ae 1853 NP(np, "nfs_setattr: nfs_vinvalbuf %d", error);
2d21ac55
A
1854 FSDBG_BOT(512, np->n_size, vap->va_data_size, np->n_vattr.nva_size, -1);
1855 return (error);
1856 }
b0d623f7
A
1857 if (nfsvers >= NFS_VER4) {
1858 /* setting file size requires having the file open for write access */
6d2010ae
A
1859 if (np->n_flag & NREVOKE)
1860 return (EIO);
b0d623f7
A
1861 noop = nfs_open_owner_find(nmp, vfs_context_ucred(ctx), 1);
1862 if (!noop)
1863 return (ENOMEM);
6d2010ae
A
1864restart:
1865 error = nfs_mount_state_in_use_start(nmp, vfs_context_thread(ctx));
1866 if (error)
1867 return (error);
1868 if (np->n_flag & NREVOKE) {
1869 nfs_mount_state_in_use_end(nmp, 0);
1870 return (EIO);
1871 }
b0d623f7
A
1872 error = nfs_open_file_find(np, noop, &nofp, 0, 0, 1);
1873 if (!error && (nofp->nof_flags & NFS_OPEN_FILE_LOST))
1874 error = EIO;
1875 if (!error && (nofp->nof_flags & NFS_OPEN_FILE_REOPEN)) {
6d2010ae
A
1876 nfs_mount_state_in_use_end(nmp, 0);
1877 error = nfs4_reopen(nofp, vfs_context_thread(ctx));
b0d623f7 1878 nofp = NULL;
6d2010ae
A
1879 if (!error)
1880 goto restart;
b0d623f7 1881 }
6d2010ae
A
1882 if (!error)
1883 error = nfs_open_file_set_busy(nofp, vfs_context_thread(ctx));
b0d623f7
A
1884 if (error) {
1885 nfs_open_owner_rele(noop);
1886 return (error);
1887 }
1888 if (!(nofp->nof_access & NFS_OPEN_SHARE_ACCESS_WRITE)) {
1889 /* we don't have the file open for write access, so open it */
b0d623f7
A
1890 error = nfs4_open(np, nofp, NFS_OPEN_SHARE_ACCESS_WRITE, NFS_OPEN_SHARE_DENY_NONE, ctx);
1891 if (!error)
1892 nofp->nof_flags |= NFS_OPEN_FILE_SETATTR;
1893 if (nfs_mount_state_error_should_restart(error)) {
1894 nfs_open_file_clear_busy(nofp);
1895 nofp = NULL;
6d2010ae
A
1896 if (nfs_mount_state_in_use_end(nmp, error))
1897 goto restart;
b0d623f7 1898 }
b0d623f7
A
1899 }
1900 }
1901 nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE);
2d21ac55 1902 if (np->n_size > vap->va_data_size) { /* shrinking? */
91447636 1903 daddr64_t obn, bn;
0c530ab8 1904 int neweofoff, mustwrite;
55e303ae 1905 struct nfsbuf *bp;
fa4905b1 1906
fa4905b1 1907 obn = (np->n_size - 1) / biosize;
2d21ac55 1908 bn = vap->va_data_size / biosize;
91447636 1909 for ( ; obn >= bn; obn--) {
2d21ac55 1910 if (!nfs_buf_is_incore(np, obn))
91447636 1911 continue;
2d21ac55 1912 error = nfs_buf_get(np, obn, biosize, NULL, NBLK_READ, &bp);
91447636 1913 if (error)
55e303ae 1914 continue;
91447636
A
1915 if (obn != bn) {
1916 FSDBG(512, bp, bp->nb_flags, 0, obn);
1917 SET(bp->nb_flags, NB_INVAL);
1918 nfs_buf_release(bp, 1);
1919 continue;
1920 }
1921 mustwrite = 0;
1922 neweofoff = vap->va_data_size - NBOFF(bp);
1923 /* check for any dirty data before the new EOF */
2d21ac55 1924 if ((bp->nb_dirtyend > 0) && (bp->nb_dirtyoff < neweofoff)) {
55e303ae 1925 /* clip dirty range to EOF */
2d21ac55 1926 if (bp->nb_dirtyend > neweofoff) {
91447636 1927 bp->nb_dirtyend = neweofoff;
2d21ac55
A
1928 if (bp->nb_dirtyoff >= bp->nb_dirtyend)
1929 bp->nb_dirtyoff = bp->nb_dirtyend = 0;
1930 }
1931 if ((bp->nb_dirtyend > 0) && (bp->nb_dirtyoff < neweofoff))
1932 mustwrite++;
91447636
A
1933 }
1934 bp->nb_dirty &= (1 << round_page_32(neweofoff)/PAGE_SIZE) - 1;
1935 if (bp->nb_dirty)
55e303ae 1936 mustwrite++;
91447636
A
1937 if (!mustwrite) {
1938 FSDBG(512, bp, bp->nb_flags, 0, obn);
1939 SET(bp->nb_flags, NB_INVAL);
1940 nfs_buf_release(bp, 1);
1941 continue;
1942 }
1943 /* gotta write out dirty data before invalidating */
1944 /* (NB_STABLE indicates that data writes should be FILESYNC) */
1945 /* (NB_NOCACHE indicates buffer should be discarded) */
1946 CLR(bp->nb_flags, (NB_DONE | NB_ERROR | NB_INVAL | NB_ASYNC | NB_READ));
1947 SET(bp->nb_flags, NB_STABLE | NB_NOCACHE);
0c530ab8 1948 if (!IS_VALID_CRED(bp->nb_wcred)) {
2d21ac55 1949 kauth_cred_t cred = vfs_context_ucred(ctx);
91447636
A
1950 kauth_cred_ref(cred);
1951 bp->nb_wcred = cred;
1952 }
1953 error = nfs_buf_write(bp);
1954 // Note: bp has been released
1955 if (error) {
1956 FSDBG(512, bp, 0xd00dee, 0xbad, error);
b0d623f7 1957 nfs_node_lock_force(np);
91447636
A
1958 np->n_error = error;
1959 np->n_flag |= NWRITEERR;
55e303ae 1960 /*
91447636
A
1961 * There was a write error and we need to
1962 * invalidate attrs and flush buffers in
1963 * order to sync up with the server.
1964 * (if this write was extending the file,
1965 * we may no longer know the correct size)
55e303ae 1966 */
91447636 1967 NATTRINVALIDATE(np);
b0d623f7 1968 nfs_node_unlock(np);
2d21ac55
A
1969 nfs_data_unlock(np);
1970 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1);
b0d623f7 1971 nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE);
91447636 1972 error = 0;
fa4905b1 1973 }
91447636 1974 }
1c79356b 1975 }
2d21ac55
A
1976 if (vap->va_data_size != np->n_size)
1977 ubc_setsize(vp, (off_t)vap->va_data_size); /* XXX error? */
1978 origsize = np->n_size;
91447636 1979 np->n_size = np->n_vattr.nva_size = vap->va_data_size;
b0d623f7 1980 nfs_node_lock_force(np);
2d21ac55 1981 CLR(np->n_flag, NUPDATESIZE);
b0d623f7 1982 nfs_node_unlock(np);
2d21ac55
A
1983 FSDBG(512, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001);
1984 }
1985 } else if (VATTR_IS_ACTIVE(vap, va_modify_time) ||
1986 VATTR_IS_ACTIVE(vap, va_access_time) ||
1987 (vap->va_vaflags & VA_UTIMES_NULL)) {
b0d623f7 1988 if ((error = nfs_node_lock(np)))
55e303ae 1989 return (error);
2d21ac55 1990 if ((np->n_flag & NMODIFIED) && (vnode_vtype(vp) == VREG)) {
b0d623f7 1991 nfs_node_unlock(np);
2d21ac55
A
1992 error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1);
1993 if (error == EINTR)
1994 return (error);
1995 } else {
b0d623f7 1996 nfs_node_unlock(np);
2d21ac55 1997 }
55e303ae 1998 }
6d2010ae
A
1999 if ((VATTR_IS_ACTIVE(vap, va_mode) || VATTR_IS_ACTIVE(vap, va_uid) || VATTR_IS_ACTIVE(vap, va_gid) ||
2000 VATTR_IS_ACTIVE(vap, va_acl) || VATTR_IS_ACTIVE(vap, va_uuuid) || VATTR_IS_ACTIVE(vap, va_guuid)) &&
2001 !(error = nfs_node_lock(np))) {
2002 NACCESSINVALIDATE(np);
b0d623f7 2003 nfs_node_unlock(np);
6d2010ae
A
2004 if (!namedattrs) {
2005 dvp = vnode_getparent(vp);
2006 vname = vnode_getname(vp);
2007 dnp = (dvp && vname) ? VTONFS(dvp) : NULL;
2008 if (dnp) {
813fb2f6
A
2009 if (nfs_node_set_busy(dnp, vfs_context_thread(ctx))) {
2010 vnode_put(dvp);
2011 vnode_putname(vname);
2012 } else {
2013 nfs_dulookup_init(&dul, dnp, vname, strlen(vname), ctx);
2014 nfs_dulookup_start(&dul, dnp, ctx);
2015 dul_in_progress = 1;
6d2010ae 2016 }
813fb2f6
A
2017 } else {
2018 if (dvp)
2019 vnode_put(dvp);
2020 if (vname)
2021 vnode_putname(vname);
2d21ac55 2022 }
2d21ac55
A
2023 }
2024 }
2025
6d2010ae 2026 if (!error)
b0d623f7
A
2027 error = nmp->nm_funcs->nf_setattr_rpc(np, vap, ctx);
2028
813fb2f6
A
2029 if (dul_in_progress) {
2030 nfs_dulookup_finish(&dul, dnp, ctx);
2031 nfs_node_clear_busy(dnp);
2032 vnode_put(dvp);
2033 vnode_putname(vname);
91447636 2034 }
1c79356b 2035
2d21ac55
A
2036 FSDBG_BOT(512, np->n_size, vap->va_data_size, np->n_vattr.nva_size, error);
2037 if (VATTR_IS_ACTIVE(vap, va_data_size)) {
6d2010ae
A
2038 if (error && (origsize != np->n_size) &&
2039 ((nfsvers < NFS_VER4) || !nfs_mount_state_error_should_restart(error))) {
2d21ac55 2040 /* make every effort to resync file size w/ server... */
6d2010ae 2041 /* (don't bother if we'll be restarting the operation) */
2d21ac55
A
2042 int err; /* preserve "error" for return */
2043 np->n_size = np->n_vattr.nva_size = origsize;
b0d623f7 2044 nfs_node_lock_force(np);
2d21ac55 2045 CLR(np->n_flag, NUPDATESIZE);
b0d623f7 2046 nfs_node_unlock(np);
2d21ac55
A
2047 FSDBG(512, np, np->n_size, np->n_vattr.nva_size, 0xf00d0002);
2048 ubc_setsize(vp, (off_t)np->n_size); /* XXX check error */
6d2010ae 2049 vapsize = vap->va_data_size;
2d21ac55 2050 vap->va_data_size = origsize;
b0d623f7 2051 err = nmp->nm_funcs->nf_setattr_rpc(np, vap, ctx);
2d21ac55 2052 if (err)
6d2010ae
A
2053 NP(np, "nfs_vnop_setattr: nfs%d_setattr_rpc %d %d", nfsvers, error, err);
2054 vap->va_data_size = vapsize;
2d21ac55 2055 }
b0d623f7
A
2056 nfs_node_lock_force(np);
2057 /*
2058 * The size was just set. If the size is already marked for update, don't
2059 * trust the newsize (it may have been set while the setattr was in progress).
2060 * Clear the update flag and make sure we fetch new attributes so we are sure
2061 * we have the latest size.
2062 */
2063 if (ISSET(np->n_flag, NUPDATESIZE)) {
2064 CLR(np->n_flag, NUPDATESIZE);
2065 NATTRINVALIDATE(np);
2066 nfs_node_unlock(np);
6d2010ae 2067 nfs_getattr(np, NULL, ctx, NGA_UNCACHED);
b0d623f7
A
2068 } else {
2069 nfs_node_unlock(np);
2070 }
2d21ac55 2071 nfs_data_unlock(np);
b0d623f7 2072 if (nfsvers >= NFS_VER4) {
6d2010ae
A
2073 if (nofp) {
2074 /* don't close our setattr open if we'll be restarting... */
2075 if (!nfs_mount_state_error_should_restart(error) &&
2076 (nofp->nof_flags & NFS_OPEN_FILE_SETATTR)) {
2077 int err = nfs_close(np, nofp, NFS_OPEN_SHARE_ACCESS_WRITE, NFS_OPEN_SHARE_DENY_NONE, ctx);
2078 if (err)
2079 NP(np, "nfs_vnop_setattr: close error: %d", err);
2080 nofp->nof_flags &= ~NFS_OPEN_FILE_SETATTR;
b0d623f7 2081 }
b0d623f7 2082 nfs_open_file_clear_busy(nofp);
6d2010ae 2083 nofp = NULL;
b0d623f7 2084 }
6d2010ae
A
2085 if (nfs_mount_state_in_use_end(nmp, error))
2086 goto restart;
b0d623f7
A
2087 nfs_open_owner_rele(noop);
2088 }
1c79356b
A
2089 }
2090 return (error);
2091}
2092
2093/*
2d21ac55 2094 * Do an NFS setattr RPC.
1c79356b 2095 */
2d21ac55
A
2096int
2097nfs3_setattr_rpc(
2098 nfsnode_t np,
2099 struct vnode_attr *vap,
b0d623f7 2100 vfs_context_t ctx)
1c79356b 2101{
2d21ac55
A
2102 struct nfsmount *nmp = NFSTONMP(np);
2103 int error = 0, lockerror = ENOENT, status, wccpostattr = 0, nfsvers;
b0d623f7 2104 u_int64_t xid, nextxid;
2d21ac55 2105 struct nfsm_chain nmreq, nmrep;
55e303ae 2106
fe8ab488 2107 if (nfs_mount_gone(nmp))
55e303ae 2108 return (ENXIO);
2d21ac55 2109 nfsvers = nmp->nm_vers;
1c79356b 2110
2d21ac55
A
2111 VATTR_SET_SUPPORTED(vap, va_mode);
2112 VATTR_SET_SUPPORTED(vap, va_uid);
2113 VATTR_SET_SUPPORTED(vap, va_gid);
2114 VATTR_SET_SUPPORTED(vap, va_data_size);
2115 VATTR_SET_SUPPORTED(vap, va_access_time);
2116 VATTR_SET_SUPPORTED(vap, va_modify_time);
2117
2118 if (VATTR_IS_ACTIVE(vap, va_flags)) {
2119 if (vap->va_flags) { /* we don't support setting flags */
2120 if (vap->va_active & ~VNODE_ATTR_va_flags)
2121 return (EINVAL); /* return EINVAL if other attributes also set */
2122 else
2123 return (ENOTSUP); /* return ENOTSUP for chflags(2) */
2124 }
2125 /* no flags set, so we'll just ignore it */
2126 if (!(vap->va_active & ~VNODE_ATTR_va_flags))
2127 return (0); /* no (other) attributes to set, so nothing to do */
2128 }
2129
2130 nfsm_chain_null(&nmreq);
2131 nfsm_chain_null(&nmrep);
2132
2133 nfsm_chain_build_alloc_init(error, &nmreq,
2134 NFSX_FH(nfsvers) + NFSX_SATTR(nfsvers));
2135 nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
2136 if (nfsvers == NFS_VER3) {
91447636 2137 if (VATTR_IS_ACTIVE(vap, va_mode)) {
2d21ac55
A
2138 nfsm_chain_add_32(error, &nmreq, TRUE);
2139 nfsm_chain_add_32(error, &nmreq, vap->va_mode);
1c79356b 2140 } else {
2d21ac55 2141 nfsm_chain_add_32(error, &nmreq, FALSE);
1c79356b 2142 }
91447636 2143 if (VATTR_IS_ACTIVE(vap, va_uid)) {
2d21ac55
A
2144 nfsm_chain_add_32(error, &nmreq, TRUE);
2145 nfsm_chain_add_32(error, &nmreq, vap->va_uid);
1c79356b 2146 } else {
2d21ac55 2147 nfsm_chain_add_32(error, &nmreq, FALSE);
1c79356b 2148 }
91447636 2149 if (VATTR_IS_ACTIVE(vap, va_gid)) {
2d21ac55
A
2150 nfsm_chain_add_32(error, &nmreq, TRUE);
2151 nfsm_chain_add_32(error, &nmreq, vap->va_gid);
1c79356b 2152 } else {
2d21ac55 2153 nfsm_chain_add_32(error, &nmreq, FALSE);
1c79356b 2154 }
91447636 2155 if (VATTR_IS_ACTIVE(vap, va_data_size)) {
2d21ac55
A
2156 nfsm_chain_add_32(error, &nmreq, TRUE);
2157 nfsm_chain_add_64(error, &nmreq, vap->va_data_size);
1c79356b 2158 } else {
2d21ac55 2159 nfsm_chain_add_32(error, &nmreq, FALSE);
1c79356b 2160 }
2d21ac55
A
2161 if (vap->va_vaflags & VA_UTIMES_NULL) {
2162 nfsm_chain_add_32(error, &nmreq, NFS_TIME_SET_TO_SERVER);
2163 nfsm_chain_add_32(error, &nmreq, NFS_TIME_SET_TO_SERVER);
2164 } else {
2165 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
2166 nfsm_chain_add_32(error, &nmreq, NFS_TIME_SET_TO_CLIENT);
2167 nfsm_chain_add_32(error, &nmreq, vap->va_access_time.tv_sec);
2168 nfsm_chain_add_32(error, &nmreq, vap->va_access_time.tv_nsec);
1c79356b 2169 } else {
2d21ac55 2170 nfsm_chain_add_32(error, &nmreq, NFS_TIME_DONT_CHANGE);
1c79356b 2171 }
2d21ac55
A
2172 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
2173 nfsm_chain_add_32(error, &nmreq, NFS_TIME_SET_TO_CLIENT);
2174 nfsm_chain_add_32(error, &nmreq, vap->va_modify_time.tv_sec);
2175 nfsm_chain_add_32(error, &nmreq, vap->va_modify_time.tv_nsec);
1c79356b 2176 } else {
2d21ac55 2177 nfsm_chain_add_32(error, &nmreq, NFS_TIME_DONT_CHANGE);
1c79356b 2178 }
1c79356b 2179 }
2d21ac55 2180 nfsm_chain_add_32(error, &nmreq, FALSE);
1c79356b 2181 } else {
2d21ac55
A
2182 nfsm_chain_add_32(error, &nmreq, VATTR_IS_ACTIVE(vap, va_mode) ?
2183 vtonfsv2_mode(vnode_vtype(NFSTOV(np)), vap->va_mode) : -1);
2184 nfsm_chain_add_32(error, &nmreq, VATTR_IS_ACTIVE(vap, va_uid) ?
2185 vap->va_uid : (uint32_t)-1);
2186 nfsm_chain_add_32(error, &nmreq, VATTR_IS_ACTIVE(vap, va_gid) ?
2187 vap->va_gid : (uint32_t)-1);
2188 nfsm_chain_add_32(error, &nmreq, VATTR_IS_ACTIVE(vap, va_data_size) ?
2189 vap->va_data_size : (uint32_t)-1);
91447636 2190 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
2d21ac55
A
2191 nfsm_chain_add_32(error, &nmreq, vap->va_access_time.tv_sec);
2192 nfsm_chain_add_32(error, &nmreq, (vap->va_access_time.tv_nsec != -1) ?
2193 ((uint32_t)vap->va_access_time.tv_nsec / 1000) : 0xffffffff);
91447636 2194 } else {
2d21ac55
A
2195 nfsm_chain_add_32(error, &nmreq, -1);
2196 nfsm_chain_add_32(error, &nmreq, -1);
91447636
A
2197 }
2198 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
2d21ac55
A
2199 nfsm_chain_add_32(error, &nmreq, vap->va_modify_time.tv_sec);
2200 nfsm_chain_add_32(error, &nmreq, (vap->va_modify_time.tv_nsec != -1) ?
2201 ((uint32_t)vap->va_modify_time.tv_nsec / 1000) : 0xffffffff);
91447636 2202 } else {
2d21ac55
A
2203 nfsm_chain_add_32(error, &nmreq, -1);
2204 nfsm_chain_add_32(error, &nmreq, -1);
91447636 2205 }
1c79356b 2206 }
2d21ac55
A
2207 nfsm_chain_build_done(error, &nmreq);
2208 nfsmout_if(error);
6d2010ae 2209 error = nfs_request(np, NULL, &nmreq, NFSPROC_SETATTR, ctx, NULL, &nmrep, &xid, &status);
b0d623f7 2210 if ((lockerror = nfs_node_lock(np)))
2d21ac55
A
2211 error = lockerror;
2212 if (nfsvers == NFS_VER3) {
91447636 2213 struct timespec premtime = { 0, 0 };
2d21ac55
A
2214 nfsm_chain_get_wcc_data(error, &nmrep, np, &premtime, &wccpostattr, &xid);
2215 nfsmout_if(error);
483a1d10 2216 /* if file hadn't changed, update cached mtime */
2d21ac55
A
2217 if (nfstimespeccmp(&np->n_mtime, &premtime, ==))
2218 NFS_CHANGED_UPDATE(nfsvers, np, &np->n_vattr);
483a1d10 2219 /* if directory hadn't changed, update namecache mtime */
2d21ac55
A
2220 if ((vnode_vtype(NFSTOV(np)) == VDIR) &&
2221 nfstimespeccmp(&np->n_ncmtime, &premtime, ==))
2222 NFS_CHANGED_UPDATE_NC(nfsvers, np, &np->n_vattr);
483a1d10 2223 if (!wccpostattr)
2d21ac55
A
2224 NATTRINVALIDATE(np);
2225 error = status;
e5568f75 2226 } else {
2d21ac55
A
2227 if (!error)
2228 error = status;
6d2010ae 2229 nfsm_chain_loadattr(error, &nmrep, np, nfsvers, &xid);
e5568f75 2230 }
b0d623f7
A
2231 /*
2232 * We just changed the attributes and we want to make sure that we
2233 * see the latest attributes. Get the next XID. If it's not the
2234 * next XID after the SETATTR XID, then it's possible that another
2235 * RPC was in flight at the same time and it might put stale attributes
2236 * in the cache. In that case, we invalidate the attributes and set
2237 * the attribute cache XID to guarantee that newer attributes will
2238 * get loaded next.
2239 */
2240 nextxid = 0;
2241 nfs_get_xid(&nextxid);
2242 if (nextxid != (xid + 1)) {
2243 np->n_xid = nextxid;
2244 NATTRINVALIDATE(np);
2245 }
2d21ac55 2246nfsmout:
b0d623f7
A
2247 if (!lockerror)
2248 nfs_node_unlock(np);
2d21ac55
A
2249 nfsm_chain_cleanup(&nmreq);
2250 nfsm_chain_cleanup(&nmrep);
1c79356b
A
2251 return (error);
2252}
2253
2254/*
2d21ac55 2255 * NFS lookup call, one step at a time...
1c79356b 2256 * First look in cache
2d21ac55 2257 * If not found, unlock the directory nfsnode and do the RPC
1c79356b 2258 */
b0d623f7 2259int
2d21ac55 2260nfs_vnop_lookup(
91447636 2261 struct vnop_lookup_args /* {
1c79356b 2262 struct vnodeop_desc *a_desc;
91447636
A
2263 vnode_t a_dvp;
2264 vnode_t *a_vpp;
1c79356b 2265 struct componentname *a_cnp;
91447636 2266 vfs_context_t a_context;
2d21ac55 2267 } */ *ap)
1c79356b 2268{
2d21ac55 2269 vfs_context_t ctx = ap->a_context;
91447636
A
2270 struct componentname *cnp = ap->a_cnp;
2271 vnode_t dvp = ap->a_dvp;
2272 vnode_t *vpp = ap->a_vpp;
2273 int flags = cnp->cn_flags;
2274 vnode_t newvp;
2d21ac55
A
2275 nfsnode_t dnp, np;
2276 struct nfsmount *nmp;
2277 mount_t mp;
b0d623f7 2278 int nfsvers, error, busyerror = ENOENT, isdot, isdotdot, negnamecache;
2d21ac55 2279 u_int64_t xid;
91447636 2280 struct nfs_vattr nvattr;
91447636 2281 int ngflags;
2d21ac55
A
2282 struct vnop_access_args naa;
2283 fhandle_t fh;
2284 struct nfsreq rq, *req = &rq;
1c79356b 2285
1c79356b 2286 *vpp = NULLVP;
55e303ae 2287
91447636 2288 dnp = VTONFS(dvp);
6d2010ae 2289 NVATTR_INIT(&nvattr);
fa4905b1 2290
2d21ac55
A
2291 mp = vnode_mount(dvp);
2292 nmp = VFSTONFS(mp);
fe8ab488 2293 if (nfs_mount_gone(nmp)) {
2d21ac55
A
2294 error = ENXIO;
2295 goto error_return;
2296 }
2297 nfsvers = nmp->nm_vers;
6d2010ae 2298 negnamecache = !NMFLAG(nmp, NONEGNAMECACHE);
2d21ac55 2299
b0d623f7
A
2300 if ((error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx))))
2301 goto error_return;
2302 /* nfs_getattr() will check changed and purge caches */
6d2010ae 2303 if ((error = nfs_getattr(dnp, NULL, ctx, NGA_CACHED)))
91447636 2304 goto error_return;
1c79356b 2305
91447636
A
2306 error = cache_lookup(dvp, vpp, cnp);
2307 switch (error) {
2308 case ENOENT:
2d21ac55
A
2309 /* negative cache entry */
2310 goto error_return;
91447636
A
2311 case 0:
2312 /* cache miss */
6d2010ae 2313 if ((nfsvers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) {
b0d623f7
A
2314 /* if rdirplus, try dir buf cache lookup */
2315 error = nfs_dir_buf_cache_lookup(dnp, &np, cnp, ctx, 0);
2316 if (!error && np) {
2317 /* dir buf cache hit */
2318 *vpp = NFSTOV(np);
2319 error = -1;
2320 }
2321 }
2322 if (error != -1) /* cache miss */
2323 break;
2324 /* FALLTHROUGH */
91447636
A
2325 case -1:
2326 /* cache hit, not really an error */
316670eb 2327 OSAddAtomic64(1, &nfsstats.lookupcache_hits);
91447636 2328
b0d623f7 2329 nfs_node_clear_busy(dnp);
6d2010ae 2330 busyerror = ENOENT;
2d21ac55 2331
91447636 2332 /* check for directory access */
6d2010ae 2333 naa.a_desc = &vnop_access_desc;
91447636
A
2334 naa.a_vp = dvp;
2335 naa.a_action = KAUTH_VNODE_SEARCH;
2d21ac55 2336 naa.a_context = ctx;
91447636
A
2337
2338 /* compute actual success/failure based on accessibility */
2d21ac55 2339 error = nfs_vnop_access(&naa);
91447636
A
2340 /* FALLTHROUGH */
2341 default:
2342 /* unexpected error from cache_lookup */
2343 goto error_return;
2344 }
2d21ac55
A
2345
2346 /* skip lookup, if we know who we are: "." or ".." */
2347 isdot = isdotdot = 0;
2348 if (cnp->cn_nameptr[0] == '.') {
2349 if (cnp->cn_namelen == 1)
2350 isdot = 1;
2351 if ((cnp->cn_namelen == 2) && (cnp->cn_nameptr[1] == '.'))
2352 isdotdot = 1;
2353 }
2354 if (isdotdot || isdot) {
2355 fh.fh_len = 0;
91447636
A
2356 goto found;
2357 }
6d2010ae
A
2358 if ((nfsvers >= NFS_VER4) && (dnp->n_vattr.nva_flags & NFS_FFLAG_TRIGGER)) {
2359 /* we should never be looking things up in a trigger directory, return nothing */
2360 error = ENOENT;
2361 goto error_return;
2362 }
55e303ae 2363
91447636 2364 /* do we know this name is too long? */
2d21ac55 2365 nmp = VTONMP(dvp);
fe8ab488 2366 if (nfs_mount_gone(nmp)) {
2d21ac55
A
2367 error = ENXIO;
2368 goto error_return;
2369 }
2370 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME) &&
b0d623f7 2371 (cnp->cn_namelen > (int)nmp->nm_fsattr.nfsa_maxname)) {
91447636
A
2372 error = ENAMETOOLONG;
2373 goto error_return;
1c79356b 2374 }
fa4905b1 2375
1c79356b
A
2376 error = 0;
2377 newvp = NULLVP;
91447636 2378
316670eb 2379 OSAddAtomic64(1, &nfsstats.lookupcache_misses);
1c79356b 2380
2d21ac55
A
2381 error = nmp->nm_funcs->nf_lookup_rpc_async(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &req);
2382 nfsmout_if(error);
6d2010ae 2383 error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, req, &xid, &fh, &nvattr);
2d21ac55 2384 nfsmout_if(error);
91447636 2385
91447636 2386 /* is the file handle the same as this directory's file handle? */
2d21ac55
A
2387 isdot = NFS_CMPFH(dnp, fh.fh_data, fh.fh_len);
2388
2389found:
2d21ac55
A
2390 if (flags & ISLASTCN) {
2391 switch (cnp->cn_nameiop) {
2392 case DELETE:
2393 cnp->cn_flags &= ~MAKEENTRY;
2394 break;
2395 case RENAME:
2396 cnp->cn_flags &= ~MAKEENTRY;
2397 if (isdot) {
2398 error = EISDIR;
91447636
A
2399 goto error_return;
2400 }
2d21ac55 2401 break;
91447636 2402 }
91447636
A
2403 }
2404
2d21ac55 2405 if (isdotdot) {
2d21ac55
A
2406 newvp = vnode_getparent(dvp);
2407 if (!newvp) {
2408 error = ENOENT;
0b4e3aa0 2409 goto error_return;
1c79356b 2410 }
2d21ac55 2411 } else if (isdot) {
91447636 2412 error = vnode_get(dvp);
2d21ac55 2413 if (error)
0b4e3aa0 2414 goto error_return;
91447636 2415 newvp = dvp;
b0d623f7 2416 nfs_node_lock_force(dnp);
2d21ac55
A
2417 if (fh.fh_len && (dnp->n_xid <= xid))
2418 nfs_loadattrcache(dnp, &nvattr, &xid, 0);
b0d623f7 2419 nfs_node_unlock(dnp);
1c79356b 2420 } else {
2d21ac55 2421 ngflags = (cnp->cn_flags & MAKEENTRY) ? NG_MAKEENTRY : 0;
6d2010ae 2422 error = nfs_nget(mp, dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, ngflags, &np);
2d21ac55 2423 if (error)
0b4e3aa0 2424 goto error_return;
1c79356b 2425 newvp = NFSTOV(np);
b0d623f7 2426 nfs_node_unlock(np);
1c79356b 2427 }
1c79356b 2428 *vpp = newvp;
91447636 2429
2d21ac55 2430nfsmout:
1c79356b 2431 if (error) {
2d21ac55
A
2432 if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME)) &&
2433 (flags & ISLASTCN) && (error == ENOENT)) {
91447636 2434 if (vnode_mount(dvp) && vnode_vfsisrdonly(dvp))
1c79356b
A
2435 error = EROFS;
2436 else
2437 error = EJUSTRETURN;
2438 }
1c79356b 2439 }
2d21ac55
A
2440 if ((error == ENOENT) && (cnp->cn_flags & MAKEENTRY) &&
2441 (cnp->cn_nameiop != CREATE) && negnamecache) {
2442 /* add a negative entry in the name cache */
b0d623f7 2443 nfs_node_lock_force(dnp);
2d21ac55
A
2444 cache_enter(dvp, NULL, cnp);
2445 dnp->n_flag |= NNEGNCENTRIES;
b0d623f7 2446 nfs_node_unlock(dnp);
2d21ac55 2447 }
0b4e3aa0 2448error_return:
6d2010ae 2449 NVATTR_CLEANUP(&nvattr);
b0d623f7
A
2450 if (!busyerror)
2451 nfs_node_clear_busy(dnp);
91447636
A
2452 if (error && *vpp) {
2453 vnode_put(*vpp);
2454 *vpp = NULLVP;
2455 }
1c79356b
A
2456 return (error);
2457}
2458
fe8ab488
A
2459int nfs_readlink_nocache = DEFAULT_READLINK_NOCACHE;
2460
1c79356b 2461/*
2d21ac55 2462 * NFS readlink call
1c79356b 2463 */
b0d623f7 2464int
2d21ac55 2465nfs_vnop_readlink(
91447636
A
2466 struct vnop_readlink_args /* {
2467 struct vnodeop_desc *a_desc;
2468 vnode_t a_vp;
1c79356b 2469 struct uio *a_uio;
91447636 2470 vfs_context_t a_context;
2d21ac55 2471 } */ *ap)
1c79356b 2472{
2d21ac55
A
2473 vfs_context_t ctx = ap->a_context;
2474 nfsnode_t np = VTONFS(ap->a_vp);
2475 struct nfsmount *nmp;
b0d623f7 2476 int error = 0, nfsvers;
2d21ac55 2477 uint32_t buflen;
b0d623f7 2478 uio_t uio = ap->a_uio;
2d21ac55 2479 struct nfsbuf *bp = NULL;
fe8ab488
A
2480 struct timespec ts;
2481 int timeo;
2d21ac55
A
2482
2483 if (vnode_vtype(ap->a_vp) != VLNK)
1c79356b 2484 return (EPERM);
2d21ac55 2485
b0d623f7 2486 if (uio_resid(uio) == 0)
2d21ac55 2487 return (0);
b0d623f7 2488 if (uio_offset(uio) < 0)
2d21ac55
A
2489 return (EINVAL);
2490
2491 nmp = VTONMP(ap->a_vp);
fe8ab488 2492 if (nfs_mount_gone(nmp))
2d21ac55
A
2493 return (ENXIO);
2494 nfsvers = nmp->nm_vers;
2495
fe8ab488 2496
b0d623f7 2497 /* nfs_getattr() will check changed and purge caches */
fe8ab488 2498 if ((error = nfs_getattr(np, NULL, ctx, nfs_readlink_nocache ? NGA_UNCACHED : NGA_CACHED))) {
2d21ac55
A
2499 FSDBG(531, np, 0xd1e0001, 0, error);
2500 return (error);
2501 }
2d21ac55 2502
fe8ab488
A
2503 if (nfs_readlink_nocache) {
2504 timeo = nfs_attrcachetimeout(np);
2505 nanouptime(&ts);
2506 }
2507
2508retry:
316670eb 2509 OSAddAtomic64(1, &nfsstats.biocache_readlinks);
fe8ab488 2510 error = nfs_buf_get(np, 0, NFS_MAXPATHLEN, vfs_context_thread(ctx), NBLK_META, &bp);
2d21ac55
A
2511 if (error) {
2512 FSDBG(531, np, 0xd1e0002, 0, error);
2513 return (error);
2514 }
fe8ab488
A
2515
2516 if (nfs_readlink_nocache) {
2517 NFS_VNOP_DBG("timeo = %d ts.tv_sec = %ld need refresh = %d cached = %d\n", timeo, ts.tv_sec,
2518 (np->n_rltim.tv_sec + timeo) < ts.tv_sec || nfs_readlink_nocache > 1,
2519 ISSET(bp->nb_flags, NB_CACHE) == NB_CACHE);
2520 /* n_rltim is synchronized by the associated nfs buf */
2521 if (ISSET(bp->nb_flags, NB_CACHE) && ((nfs_readlink_nocache > 1) || ((np->n_rltim.tv_sec + timeo) < ts.tv_sec))) {
2522 SET(bp->nb_flags, NB_INVAL);
2523 nfs_buf_release(bp, 0);
2524 goto retry;
2525 }
2526 }
2d21ac55 2527 if (!ISSET(bp->nb_flags, NB_CACHE)) {
fe8ab488 2528readagain:
316670eb 2529 OSAddAtomic64(1, &nfsstats.readlink_bios);
2d21ac55
A
2530 buflen = bp->nb_bufsize;
2531 error = nmp->nm_funcs->nf_readlink_rpc(np, bp->nb_data, &buflen, ctx);
2532 if (error) {
fe8ab488
A
2533 if (error == ESTALE) {
2534 NFS_VNOP_DBG("Stale FH from readlink rpc\n");
2535 error = nfs_refresh_fh(np, ctx);
2536 if (error == 0)
2537 goto readagain;
2538 }
2d21ac55
A
2539 SET(bp->nb_flags, NB_ERROR);
2540 bp->nb_error = error;
fe8ab488 2541 NFS_VNOP_DBG("readlink failed %d\n", error);
2d21ac55
A
2542 } else {
2543 bp->nb_validoff = 0;
2544 bp->nb_validend = buflen;
fe8ab488
A
2545 np->n_rltim = ts;
2546 NFS_VNOP_DBG("readlink of %.*s\n", bp->nb_validend, (char *)bp->nb_data);
2d21ac55 2547 }
fe8ab488
A
2548 } else {
2549 NFS_VNOP_DBG("got cached link of %.*s\n", bp->nb_validend, (char *)bp->nb_data);
2d21ac55 2550 }
fe8ab488 2551
b0d623f7
A
2552 if (!error && (bp->nb_validend > 0))
2553 error = uiomove(bp->nb_data, bp->nb_validend, uio);
2d21ac55
A
2554 FSDBG(531, np, bp->nb_validend, 0, error);
2555 nfs_buf_release(bp, 1);
2556 return (error);
1c79356b
A
2557}
2558
2559/*
2d21ac55 2560 * Do a readlink RPC.
1c79356b
A
2561 */
2562int
2d21ac55 2563nfs3_readlink_rpc(nfsnode_t np, char *buf, uint32_t *buflenp, vfs_context_t ctx)
1c79356b 2564{
2d21ac55
A
2565 struct nfsmount *nmp;
2566 int error = 0, lockerror = ENOENT, nfsvers, status;
2567 uint32_t len;
fa4905b1 2568 u_int64_t xid;
2d21ac55 2569 struct nfsm_chain nmreq, nmrep;
1c79356b 2570
2d21ac55 2571 nmp = NFSTONMP(np);
fe8ab488 2572 if (nfs_mount_gone(nmp))
55e303ae 2573 return (ENXIO);
2d21ac55
A
2574 nfsvers = nmp->nm_vers;
2575 nfsm_chain_null(&nmreq);
2576 nfsm_chain_null(&nmrep);
2577
2578 nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(nfsvers));
2579 nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
2580 nfsm_chain_build_done(error, &nmreq);
2581 nfsmout_if(error);
6d2010ae 2582 error = nfs_request(np, NULL, &nmreq, NFSPROC_READLINK, ctx, NULL, &nmrep, &xid, &status);
b0d623f7 2583 if ((lockerror = nfs_node_lock(np)))
2d21ac55
A
2584 error = lockerror;
2585 if (nfsvers == NFS_VER3)
2586 nfsm_chain_postop_attr_update(error, &nmrep, np, &xid);
2587 if (!error)
2588 error = status;
2589 nfsm_chain_get_32(error, &nmrep, len);
2590 nfsmout_if(error);
2591 if ((nfsvers == NFS_VER2) && (len > *buflenp)) {
2592 error = EBADRPC;
2593 goto nfsmout;
2594 }
2595 if (len >= *buflenp) {
2596 if (np->n_size && (np->n_size < *buflenp))
2597 len = np->n_size;
2598 else
2599 len = *buflenp - 1;
2600 }
2601 nfsm_chain_get_opaque(error, &nmrep, len, buf);
2602 if (!error)
2603 *buflenp = len;
2604nfsmout:
2605 if (!lockerror)
b0d623f7 2606 nfs_node_unlock(np);
2d21ac55
A
2607 nfsm_chain_cleanup(&nmreq);
2608 nfsm_chain_cleanup(&nmrep);
1c79356b
A
2609 return (error);
2610}
2611
2612/*
2d21ac55 2613 * NFS read RPC call
1c79356b
A
2614 * Ditto above
2615 */
2616int
b0d623f7 2617nfs_read_rpc(nfsnode_t np, uio_t uio, vfs_context_t ctx)
1c79356b 2618{
1c79356b 2619 struct nfsmount *nmp;
2d21ac55 2620 int error = 0, nfsvers, eof = 0;
b0d623f7
A
2621 size_t nmrsize, len, retlen;
2622 user_ssize_t tsiz;
2d21ac55
A
2623 off_t txoffset;
2624 struct nfsreq rq, *req = &rq;
b0d623f7 2625 uint32_t stategenid = 0, restart = 0;
1c79356b 2626
b0d623f7 2627 FSDBG_TOP(536, np, uio_offset(uio), uio_resid(uio), 0);
2d21ac55 2628 nmp = NFSTONMP(np);
fe8ab488 2629 if (nfs_mount_gone(nmp))
55e303ae 2630 return (ENXIO);
2d21ac55 2631 nfsvers = nmp->nm_vers;
55e303ae
A
2632 nmrsize = nmp->nm_rsize;
2633
b0d623f7
A
2634 txoffset = uio_offset(uio);
2635 tsiz = uio_resid(uio);
2636 if ((nfsvers == NFS_VER2) && ((uint64_t)(txoffset + tsiz) > 0xffffffffULL)) {
2637 FSDBG_BOT(536, np, uio_offset(uio), uio_resid(uio), EFBIG);
1c79356b 2638 return (EFBIG);
55e303ae 2639 }
2d21ac55 2640
1c79356b 2641 while (tsiz > 0) {
b0d623f7 2642 len = retlen = (tsiz > (user_ssize_t)nmrsize) ? nmrsize : (size_t)tsiz;
2d21ac55 2643 FSDBG(536, np, txoffset, len, 0);
6d2010ae
A
2644 if (np->n_flag & NREVOKE) {
2645 error = EIO;
2646 break;
2647 }
b0d623f7
A
2648 if (nmp->nm_vers >= NFS_VER4)
2649 stategenid = nmp->nm_stategenid;
2d21ac55
A
2650 error = nmp->nm_funcs->nf_read_rpc_async(np, txoffset, len,
2651 vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, &req);
2652 if (!error)
b0d623f7
A
2653 error = nmp->nm_funcs->nf_read_rpc_async_finish(np, req, uio, &retlen, &eof);
2654 if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error) &&
2655 (++restart <= nfs_mount_state_max_restarts(nmp))) { /* guard against no progress */
2656 lck_mtx_lock(&nmp->nm_lock);
6d2010ae
A
2657 if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) {
2658 NP(np, "nfs_read_rpc: error %d, initiating recovery", error);
2659 nfs_need_recover(nmp, error);
b0d623f7
A
2660 }
2661 lck_mtx_unlock(&nmp->nm_lock);
6d2010ae
A
2662 if (np->n_flag & NREVOKE) {
2663 error = EIO;
2664 } else {
2665 if (error == NFSERR_GRACE)
2666 tsleep(&nmp->nm_state, (PZERO-1), "nfsgrace", 2*hz);
2667 if (!(error = nfs_mount_state_wait_for_recovery(nmp)))
2668 continue;
2669 }
b0d623f7 2670 }
91447636
A
2671 if (error)
2672 break;
2d21ac55
A
2673 txoffset += retlen;
2674 tsiz -= retlen;
2675 if (nfsvers != NFS_VER2) {
2676 if (eof || (retlen == 0))
2677 tsiz = 0;
2678 } else if (retlen < len)
2679 tsiz = 0;
2680 }
2681
b0d623f7 2682 FSDBG_BOT(536, np, eof, uio_resid(uio), error);
2d21ac55
A
2683 return (error);
2684}
2685
2686int
2687nfs3_read_rpc_async(
2688 nfsnode_t np,
2689 off_t offset,
2690 size_t len,
2691 thread_t thd,
2692 kauth_cred_t cred,
2693 struct nfsreq_cbinfo *cb,
2694 struct nfsreq **reqp)
2695{
2696 struct nfsmount *nmp;
2697 int error = 0, nfsvers;
2698 struct nfsm_chain nmreq;
2699
2700 nmp = NFSTONMP(np);
fe8ab488 2701 if (nfs_mount_gone(nmp))
2d21ac55
A
2702 return (ENXIO);
2703 nfsvers = nmp->nm_vers;
2704
2705 nfsm_chain_null(&nmreq);
2706 nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(nfsvers) + 3 * NFSX_UNSIGNED);
2707 nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
2708 if (nfsvers == NFS_VER3) {
2709 nfsm_chain_add_64(error, &nmreq, offset);
2710 nfsm_chain_add_32(error, &nmreq, len);
2711 } else {
2712 nfsm_chain_add_32(error, &nmreq, offset);
2713 nfsm_chain_add_32(error, &nmreq, len);
2714 nfsm_chain_add_32(error, &nmreq, 0);
2715 }
2716 nfsm_chain_build_done(error, &nmreq);
2717 nfsmout_if(error);
6d2010ae 2718 error = nfs_request_async(np, NULL, &nmreq, NFSPROC_READ, thd, cred, NULL, 0, cb, reqp);
2d21ac55
A
2719nfsmout:
2720 nfsm_chain_cleanup(&nmreq);
2721 return (error);
2722}
2723
2724int
2725nfs3_read_rpc_async_finish(
2726 nfsnode_t np,
2727 struct nfsreq *req,
b0d623f7 2728 uio_t uio,
2d21ac55
A
2729 size_t *lenp,
2730 int *eofp)
2731{
2732 int error = 0, lockerror, nfsvers, status, eof = 0;
2733 size_t retlen = 0;
2734 uint64_t xid;
2735 struct nfsmount *nmp;
2736 struct nfsm_chain nmrep;
2737
2738 nmp = NFSTONMP(np);
fe8ab488 2739 if (nfs_mount_gone(nmp)) {
2d21ac55
A
2740 nfs_request_async_cancel(req);
2741 return (ENXIO);
2742 }
2743 nfsvers = nmp->nm_vers;
2744
2745 nfsm_chain_null(&nmrep);
2746
2747 error = nfs_request_async_finish(req, &nmrep, &xid, &status);
2748 if (error == EINPROGRESS) /* async request restarted */
2749 return (error);
2750
b0d623f7 2751 if ((lockerror = nfs_node_lock(np)))
2d21ac55
A
2752 error = lockerror;
2753 if (nfsvers == NFS_VER3)
2754 nfsm_chain_postop_attr_update(error, &nmrep, np, &xid);
2755 if (!error)
2756 error = status;
2757 if (nfsvers == NFS_VER3) {
2758 nfsm_chain_adv(error, &nmrep, NFSX_UNSIGNED);
2759 nfsm_chain_get_32(error, &nmrep, eof);
2760 } else {
6d2010ae 2761 nfsm_chain_loadattr(error, &nmrep, np, nfsvers, &xid);
2d21ac55
A
2762 }
2763 if (!lockerror)
b0d623f7 2764 nfs_node_unlock(np);
2d21ac55
A
2765 nfsm_chain_get_32(error, &nmrep, retlen);
2766 if ((nfsvers == NFS_VER2) && (retlen > *lenp))
2767 error = EBADRPC;
2768 nfsmout_if(error);
b0d623f7 2769 error = nfsm_chain_get_uio(&nmrep, MIN(retlen, *lenp), uio);
2d21ac55
A
2770 if (eofp) {
2771 if (nfsvers == NFS_VER3) {
2772 if (!eof && !retlen)
2773 eof = 1;
2774 } else if (retlen < *lenp) {
2775 eof = 1;
1c79356b 2776 }
2d21ac55
A
2777 *eofp = eof;
2778 }
2779 *lenp = MIN(retlen, *lenp);
2780nfsmout:
2781 nfsm_chain_cleanup(&nmrep);
2782 return (error);
2783}
2784
2785/*
2786 * NFS write call
2787 */
2788int
2789nfs_vnop_write(
2790 struct vnop_write_args /* {
2791 struct vnodeop_desc *a_desc;
2792 vnode_t a_vp;
2793 struct uio *a_uio;
2794 int a_ioflag;
2795 vfs_context_t a_context;
2796 } */ *ap)
2797{
2798 vfs_context_t ctx = ap->a_context;
b0d623f7 2799 uio_t uio = ap->a_uio;
2d21ac55
A
2800 vnode_t vp = ap->a_vp;
2801 nfsnode_t np = VTONFS(vp);
2802 int ioflag = ap->a_ioflag;
2803 struct nfsbuf *bp;
2d21ac55
A
2804 struct nfsmount *nmp = VTONMP(vp);
2805 daddr64_t lbn;
2806 int biosize;
2807 int n, on, error = 0;
2808 off_t boff, start, end;
b0d623f7
A
2809 uio_t auio;
2810 char auio_buf [ UIO_SIZEOF(1) ];
2d21ac55
A
2811 thread_t thd;
2812 kauth_cred_t cred;
2813
b0d623f7 2814 FSDBG_TOP(515, np, uio_offset(uio), uio_resid(uio), ioflag);
2d21ac55
A
2815
2816 if (vnode_vtype(vp) != VREG) {
b0d623f7 2817 FSDBG_BOT(515, np, uio_offset(uio), uio_resid(uio), EIO);
2d21ac55
A
2818 return (EIO);
2819 }
2820
2821 thd = vfs_context_thread(ctx);
2822 cred = vfs_context_ucred(ctx);
2823
b0d623f7 2824 nfs_data_lock(np, NFS_DATA_LOCK_SHARED);
2d21ac55 2825
b0d623f7 2826 if ((error = nfs_node_lock(np))) {
2d21ac55 2827 nfs_data_unlock(np);
b0d623f7 2828 FSDBG_BOT(515, np, uio_offset(uio), uio_resid(uio), error);
2d21ac55
A
2829 return (error);
2830 }
2831 np->n_wrbusy++;
2832
2833 if (np->n_flag & NWRITEERR) {
2834 error = np->n_error;
2835 np->n_flag &= ~NWRITEERR;
2836 }
2837 if (np->n_flag & NNEEDINVALIDATE) {
2838 np->n_flag &= ~NNEEDINVALIDATE;
b0d623f7 2839 nfs_node_unlock(np);
2d21ac55
A
2840 nfs_data_unlock(np);
2841 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1);
b0d623f7
A
2842 nfs_data_lock(np, NFS_DATA_LOCK_SHARED);
2843 } else {
2844 nfs_node_unlock(np);
2d21ac55 2845 }
b0d623f7 2846 if (error)
2d21ac55 2847 goto out;
2d21ac55
A
2848
2849 biosize = nmp->nm_biosize;
2850
2851 if (ioflag & (IO_APPEND | IO_SYNC)) {
b0d623f7 2852 nfs_node_lock_force(np);
2d21ac55
A
2853 if (np->n_flag & NMODIFIED) {
2854 NATTRINVALIDATE(np);
b0d623f7 2855 nfs_node_unlock(np);
2d21ac55
A
2856 nfs_data_unlock(np);
2857 error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1);
b0d623f7
A
2858 nfs_data_lock(np, NFS_DATA_LOCK_SHARED);
2859 if (error) {
2860 FSDBG(515, np, uio_offset(uio), 0x10bad01, error);
2d21ac55 2861 goto out;
e5568f75 2862 }
b0d623f7
A
2863 } else {
2864 nfs_node_unlock(np);
2d21ac55
A
2865 }
2866 if (ioflag & IO_APPEND) {
2d21ac55 2867 nfs_data_unlock(np);
b0d623f7 2868 /* nfs_getattr() will check changed and purge caches */
6d2010ae 2869 error = nfs_getattr(np, NULL, ctx, NGA_UNCACHED);
2d21ac55 2870 /* we'll be extending the file, so take the data lock exclusive */
b0d623f7
A
2871 nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE);
2872 if (error) {
2873 FSDBG(515, np, uio_offset(uio), 0x10bad02, error);
2d21ac55
A
2874 goto out;
2875 }
b0d623f7 2876 uio_setoffset(uio, np->n_size);
2d21ac55
A
2877 }
2878 }
b0d623f7 2879 if (uio_offset(uio) < 0) {
2d21ac55 2880 error = EINVAL;
b0d623f7 2881 FSDBG_BOT(515, np, uio_offset(uio), 0xbad0ff, error);
2d21ac55
A
2882 goto out;
2883 }
b0d623f7 2884 if (uio_resid(uio) == 0)
2d21ac55 2885 goto out;
2d21ac55 2886
b0d623f7 2887 if (((uio_offset(uio) + uio_resid(uio)) > (off_t)np->n_size) && !(ioflag & IO_APPEND)) {
fe8ab488
A
2888 /*
2889 * It looks like we'll be extending the file, so take the data lock exclusive.
2890 */
2d21ac55 2891 nfs_data_unlock(np);
b0d623f7 2892 nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE);
fe8ab488
A
2893
2894 /*
2895 * Also, if the write begins after the previous EOF buffer, make sure to zero
2896 * and validate the new bytes in that buffer.
2897 */
2898 struct nfsbuf *eofbp = NULL;
2899 daddr64_t eofbn = np->n_size / biosize;
2900 int eofoff = np->n_size % biosize;
2901 lbn = uio_offset(uio) / biosize;
2902
2903 if (eofoff && (eofbn < lbn)) {
2904 if ((error = nfs_buf_get(np, eofbn, biosize, thd, NBLK_WRITE|NBLK_ONLYVALID, &eofbp)))
2905 goto out;
2906 np->n_size += (biosize - eofoff);
2907 nfs_node_lock_force(np);
2908 CLR(np->n_flag, NUPDATESIZE);
2909 np->n_flag |= NMODIFIED;
2910 nfs_node_unlock(np);
2911 FSDBG(516, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001);
2912 ubc_setsize(vp, (off_t)np->n_size); /* XXX errors */
2913 if (eofbp) {
2914 /*
2915 * For the old last page, don't zero bytes if there
2916 * are invalid bytes in that page (i.e. the page isn't
2917 * currently valid).
2918 * For pages after the old last page, zero them and
2919 * mark them as valid.
2920 */
2921 char *d;
2922 int i;
2923 if (ioflag & IO_NOCACHE)
2924 SET(eofbp->nb_flags, NB_NOCACHE);
2925 NFS_BUF_MAP(eofbp);
2926 FSDBG(516, eofbp, eofoff, biosize - eofoff, 0xe0fff01e);
2927 d = eofbp->nb_data;
2928 i = eofoff/PAGE_SIZE;
2929 while (eofoff < biosize) {
2930 int poff = eofoff & PAGE_MASK;
2931 if (!poff || NBPGVALID(eofbp,i)) {
2932 bzero(d + eofoff, PAGE_SIZE - poff);
2933 NBPGVALID_SET(eofbp, i);
2934 }
2935 eofoff += PAGE_SIZE - poff;
2936 i++;
2937 }
2938 nfs_buf_release(eofbp, 1);
2939 }
2940 }
2d21ac55
A
2941 }
2942
2943 do {
316670eb 2944 OSAddAtomic64(1, &nfsstats.biocache_writes);
b0d623f7
A
2945 lbn = uio_offset(uio) / biosize;
2946 on = uio_offset(uio) % biosize;
2947 n = biosize - on;
2948 if (uio_resid(uio) < n)
2949 n = uio_resid(uio);
2d21ac55
A
2950again:
2951 /*
2952 * Get a cache block for writing. The range to be written is
2953 * (off..off+n) within the block. We ensure that the block
2954 * either has no dirty region or that the given range is
2955 * contiguous with the existing dirty region.
2956 */
2957 error = nfs_buf_get(np, lbn, biosize, thd, NBLK_WRITE, &bp);
2958 if (error)
2959 goto out;
2960 /* map the block because we know we're going to write to it */
2961 NFS_BUF_MAP(bp);
2962
2963 if (ioflag & IO_NOCACHE)
2964 SET(bp->nb_flags, NB_NOCACHE);
2965
2966 if (!IS_VALID_CRED(bp->nb_wcred)) {
2967 kauth_cred_ref(cred);
2968 bp->nb_wcred = cred;
2969 }
2970
2971 /*
2972 * If there's already a dirty range AND dirty pages in this block we
2973 * need to send a commit AND write the dirty pages before continuing.
2974 *
2975 * If there's already a dirty range OR dirty pages in this block
2976 * and the new write range is not contiguous with the existing range,
2977 * then force the buffer to be written out now.
2978 * (We used to just extend the dirty range to cover the valid,
2979 * but unwritten, data in between also. But writing ranges
2980 * of data that weren't actually written by an application
2981 * risks overwriting some other client's data with stale data
2982 * that's just masquerading as new written data.)
2983 */
2984 if (bp->nb_dirtyend > 0) {
2985 if (on > bp->nb_dirtyend || (on + n) < bp->nb_dirtyoff || bp->nb_dirty) {
b0d623f7 2986 FSDBG(515, np, uio_offset(uio), bp, 0xd15c001);
2d21ac55
A
2987 /* write/commit buffer "synchronously" */
2988 /* (NB_STABLE indicates that data writes should be FILESYNC) */
2989 CLR(bp->nb_flags, (NB_DONE | NB_ERROR | NB_INVAL));
2990 SET(bp->nb_flags, (NB_ASYNC | NB_STABLE));
2991 error = nfs_buf_write(bp);
2992 if (error)
2993 goto out;
2994 goto again;
2995 }
2996 } else if (bp->nb_dirty) {
2997 int firstpg, lastpg;
2998 u_int32_t pagemask;
2999 /* calculate write range pagemask */
3000 firstpg = on/PAGE_SIZE;
3001 lastpg = (on+n-1)/PAGE_SIZE;
3002 pagemask = ((1 << (lastpg+1)) - 1) & ~((1 << firstpg) - 1);
3003 /* check if there are dirty pages outside the write range */
3004 if (bp->nb_dirty & ~pagemask) {
b0d623f7 3005 FSDBG(515, np, uio_offset(uio), bp, 0xd15c002);
2d21ac55
A
3006 /* write/commit buffer "synchronously" */
3007 /* (NB_STABLE indicates that data writes should be FILESYNC) */
3008 CLR(bp->nb_flags, (NB_DONE | NB_ERROR | NB_INVAL));
3009 SET(bp->nb_flags, (NB_ASYNC | NB_STABLE));
3010 error = nfs_buf_write(bp);
3011 if (error)
3012 goto out;
3013 goto again;
3014 }
3015 /* if the first or last pages are already dirty */
3016 /* make sure that the dirty range encompasses those pages */
3017 if (NBPGDIRTY(bp,firstpg) || NBPGDIRTY(bp,lastpg)) {
b0d623f7 3018 FSDBG(515, np, uio_offset(uio), bp, 0xd15c003);
2d21ac55
A
3019 bp->nb_dirtyoff = min(on, firstpg * PAGE_SIZE);
3020 if (NBPGDIRTY(bp,lastpg)) {
3021 bp->nb_dirtyend = (lastpg+1) * PAGE_SIZE;
3022 /* clip to EOF */
3023 if (NBOFF(bp) + bp->nb_dirtyend > (off_t)np->n_size) {
3024 bp->nb_dirtyend = np->n_size - NBOFF(bp);
3025 if (bp->nb_dirtyoff >= bp->nb_dirtyend)
3026 bp->nb_dirtyoff = bp->nb_dirtyend = 0;
3027 }
3028 } else
3029 bp->nb_dirtyend = on+n;
3030 }
3031 }
3032
3033 /*
3034 * Are we extending the size of the file with this write?
3035 * If so, update file size now that we have the block.
3036 * If there was a partial buf at the old eof, validate
3037 * and zero the new bytes.
3038 */
b0d623f7 3039 if ((uio_offset(uio) + n) > (off_t)np->n_size) {
2d21ac55 3040 daddr64_t eofbn = np->n_size / biosize;
b0d623f7 3041 int neweofoff = (uio_offset(uio) + n) % biosize;
2d21ac55 3042
b0d623f7 3043 FSDBG(515, 0xb1ffa000, uio_offset(uio) + n, eofoff, neweofoff);
2d21ac55 3044
2d21ac55
A
3045 /* if we're extending within the same last block */
3046 /* and the block is flagged as being cached... */
3047 if ((lbn == eofbn) && ISSET(bp->nb_flags, NB_CACHE)) {
3048 /* ...check that all pages in buffer are valid */
3049 int endpg = ((neweofoff ? neweofoff : biosize) - 1)/PAGE_SIZE;
3050 u_int32_t pagemask;
3051 /* pagemask only has to extend to last page being written to */
3052 pagemask = (1 << (endpg+1)) - 1;
3053 FSDBG(515, 0xb1ffa001, bp->nb_valid, pagemask, 0);
3054 if ((bp->nb_valid & pagemask) != pagemask) {
3055 /* zerofill any hole */
3056 if (on > bp->nb_validend) {
3057 int i;
3058 for (i=bp->nb_validend/PAGE_SIZE; i <= (on - 1)/PAGE_SIZE; i++)
3059 NBPGVALID_SET(bp, i);
3060 NFS_BUF_MAP(bp);
3061 FSDBG(516, bp, bp->nb_validend, on - bp->nb_validend, 0xf01e);
3062 bzero((char *)bp->nb_data + bp->nb_validend,
3063 on - bp->nb_validend);
3064 }
3065 /* zerofill any trailing data in the last page */
3066 if (neweofoff) {
3067 NFS_BUF_MAP(bp);
3068 FSDBG(516, bp, neweofoff, PAGE_SIZE - (neweofoff & PAGE_MASK), 0xe0f);
3069 bzero((char *)bp->nb_data + neweofoff,
3070 PAGE_SIZE - (neweofoff & PAGE_MASK));
3071 }
3072 }
3073 }
b0d623f7
A
3074 np->n_size = uio_offset(uio) + n;
3075 nfs_node_lock_force(np);
2d21ac55
A
3076 CLR(np->n_flag, NUPDATESIZE);
3077 np->n_flag |= NMODIFIED;
b0d623f7 3078 nfs_node_unlock(np);
2d21ac55
A
3079 FSDBG(516, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001);
3080 ubc_setsize(vp, (off_t)np->n_size); /* XXX errors */
2d21ac55
A
3081 }
3082 /*
3083 * If dirtyend exceeds file size, chop it down. This should
3084 * not occur unless there is a race.
3085 */
3086 if (NBOFF(bp) + bp->nb_dirtyend > (off_t)np->n_size) {
3087 bp->nb_dirtyend = np->n_size - NBOFF(bp);
3088 if (bp->nb_dirtyoff >= bp->nb_dirtyend)
3089 bp->nb_dirtyoff = bp->nb_dirtyend = 0;
3090 }
3091 /*
3092 * UBC doesn't handle partial pages, so we need to make sure
3093 * that any pages left in the page cache are completely valid.
3094 *
3095 * Writes that are smaller than a block are delayed if they
3096 * don't extend to the end of the block.
3097 *
3098 * If the block isn't (completely) cached, we may need to read
3099 * in some parts of pages that aren't covered by the write.
3100 * If the write offset (on) isn't page aligned, we'll need to
3101 * read the start of the first page being written to. Likewise,
3102 * if the offset of the end of the write (on+n) isn't page aligned,
3103 * we'll need to read the end of the last page being written to.
3104 *
3105 * Notes:
3106 * We don't want to read anything we're just going to write over.
6d2010ae
A
3107 * We don't want to read anything we're just going drop when the
3108 * I/O is complete (i.e. don't do reads for NOCACHE requests).
2d21ac55
A
3109 * We don't want to issue multiple I/Os if we don't have to
3110 * (because they're synchronous rpcs).
3111 * We don't want to read anything we already have modified in the
3112 * page cache.
3113 */
6d2010ae 3114 if (!ISSET(bp->nb_flags, NB_CACHE) && (n < biosize)) {
2d21ac55
A
3115 int firstpg, lastpg, dirtypg;
3116 int firstpgoff, lastpgoff;
3117 start = end = -1;
3118 firstpg = on/PAGE_SIZE;
3119 firstpgoff = on & PAGE_MASK;
3120 lastpg = (on+n-1)/PAGE_SIZE;
3121 lastpgoff = (on+n) & PAGE_MASK;
3122 if (firstpgoff && !NBPGVALID(bp,firstpg)) {
3123 /* need to read start of first page */
3124 start = firstpg * PAGE_SIZE;
3125 end = start + firstpgoff;
3126 }
3127 if (lastpgoff && !NBPGVALID(bp,lastpg)) {
3128 /* need to read end of last page */
3129 if (start < 0)
3130 start = (lastpg * PAGE_SIZE) + lastpgoff;
3131 end = (lastpg + 1) * PAGE_SIZE;
3132 }
6d2010ae
A
3133 if (ISSET(bp->nb_flags, NB_NOCACHE)) {
3134 /*
3135 * For nocache writes, if there is any partial page at the
3136 * start or end of the write range, then we do the write
3137 * synchronously to make sure that we can drop the data
3138 * from the cache as soon as the WRITE finishes. Normally,
3139 * we would do an unstable write and not drop the data until
3140 * it was committed. But doing that here would risk allowing
3141 * invalid data to be read from the cache between the WRITE
3142 * and the COMMIT.
3143 * (NB_STABLE indicates that data writes should be FILESYNC)
3144 */
3145 if (end > start)
3146 SET(bp->nb_flags, NB_STABLE);
3147 goto skipread;
3148 }
2d21ac55
A
3149 if (end > start) {
3150 /* need to read the data in range: start...end-1 */
3151
3152 /* first, check for dirty pages in between */
3153 /* if there are, we'll have to do two reads because */
3154 /* we don't want to overwrite the dirty pages. */
3155 for (dirtypg=start/PAGE_SIZE; dirtypg <= (end-1)/PAGE_SIZE; dirtypg++)
3156 if (NBPGDIRTY(bp,dirtypg))
3157 break;
3158
3159 /* if start is at beginning of page, try */
3160 /* to get any preceeding pages as well. */
3161 if (!(start & PAGE_MASK)) {
3162 /* stop at next dirty/valid page or start of block */
3163 for (; start > 0; start-=PAGE_SIZE)
3164 if (NBPGVALID(bp,((start-1)/PAGE_SIZE)))
3165 break;
3166 }
3167
3168 NFS_BUF_MAP(bp);
3169 /* setup uio for read(s) */
3170 boff = NBOFF(bp);
b0d623f7
A
3171 auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ,
3172 &auio_buf, sizeof(auio_buf));
2d21ac55
A
3173
3174 if (dirtypg <= (end-1)/PAGE_SIZE) {
3175 /* there's a dirty page in the way, so just do two reads */
3176 /* we'll read the preceding data here */
b0d623f7
A
3177 uio_reset(auio, boff + start, UIO_SYSSPACE, UIO_READ);
3178 uio_addiov(auio, CAST_USER_ADDR_T(bp->nb_data + start), on - start);
3179 error = nfs_read_rpc(np, auio, ctx);
6d2010ae
A
3180 if (error) {
3181 /* couldn't read the data, so treat buffer as synchronous NOCACHE */
2d21ac55 3182 SET(bp->nb_flags, (NB_NOCACHE|NB_STABLE));
6d2010ae
A
3183 goto skipread;
3184 }
b0d623f7
A
3185 if (uio_resid(auio) > 0) {
3186 FSDBG(516, bp, (caddr_t)uio_curriovbase(auio) - bp->nb_data, uio_resid(auio), 0xd00dee01);
3187 bzero(CAST_DOWN(caddr_t, uio_curriovbase(auio)), uio_resid(auio));
2d21ac55
A
3188 }
3189 if (!error) {
3190 /* update validoff/validend if necessary */
3191 if ((bp->nb_validoff < 0) || (bp->nb_validoff > start))
3192 bp->nb_validoff = start;
3193 if ((bp->nb_validend < 0) || (bp->nb_validend < on))
3194 bp->nb_validend = on;
3195 if ((off_t)np->n_size > boff + bp->nb_validend)
3196 bp->nb_validend = min(np->n_size - (boff + start), biosize);
3197 /* validate any pages before the write offset */
3198 for (; start < on/PAGE_SIZE; start+=PAGE_SIZE)
3199 NBPGVALID_SET(bp, start/PAGE_SIZE);
3200 }
3201 /* adjust start to read any trailing data */
3202 start = on+n;
3203 }
3204
3205 /* if end is at end of page, try to */
3206 /* get any following pages as well. */
3207 if (!(end & PAGE_MASK)) {
3208 /* stop at next valid page or end of block */
3209 for (; end < biosize; end+=PAGE_SIZE)
3210 if (NBPGVALID(bp,end/PAGE_SIZE))
3211 break;
3212 }
3213
3214 if (((boff+start) >= (off_t)np->n_size) ||
3215 ((start >= on) && ((boff + on + n) >= (off_t)np->n_size))) {
3216 /*
3217 * Either this entire read is beyond the current EOF
3218 * or the range that we won't be modifying (on+n...end)
3219 * is all beyond the current EOF.
3220 * No need to make a trip across the network to
3221 * read nothing. So, just zero the buffer instead.
3222 */
3223 FSDBG(516, bp, start, end - start, 0xd00dee00);
3224 bzero(bp->nb_data + start, end - start);
3225 error = 0;
6d2010ae 3226 } else {
2d21ac55 3227 /* now we'll read the (rest of the) data */
b0d623f7
A
3228 uio_reset(auio, boff + start, UIO_SYSSPACE, UIO_READ);
3229 uio_addiov(auio, CAST_USER_ADDR_T(bp->nb_data + start), end - start);
3230 error = nfs_read_rpc(np, auio, ctx);
6d2010ae
A
3231 if (error) {
3232 /* couldn't read the data, so treat buffer as synchronous NOCACHE */
2d21ac55 3233 SET(bp->nb_flags, (NB_NOCACHE|NB_STABLE));
6d2010ae
A
3234 goto skipread;
3235 }
b0d623f7
A
3236 if (uio_resid(auio) > 0) {
3237 FSDBG(516, bp, (caddr_t)uio_curriovbase(auio) - bp->nb_data, uio_resid(auio), 0xd00dee02);
3238 bzero(CAST_DOWN(caddr_t, uio_curriovbase(auio)), uio_resid(auio));
2d21ac55
A
3239 }
3240 }
3241 if (!error) {
3242 /* update validoff/validend if necessary */
3243 if ((bp->nb_validoff < 0) || (bp->nb_validoff > start))
3244 bp->nb_validoff = start;
3245 if ((bp->nb_validend < 0) || (bp->nb_validend < end))
3246 bp->nb_validend = end;
3247 if ((off_t)np->n_size > boff + bp->nb_validend)
3248 bp->nb_validend = min(np->n_size - (boff + start), biosize);
3249 /* validate any pages before the write offset's page */
b0d623f7 3250 for (; start < (off_t)trunc_page_32(on); start+=PAGE_SIZE)
2d21ac55
A
3251 NBPGVALID_SET(bp, start/PAGE_SIZE);
3252 /* validate any pages after the range of pages being written to */
b0d623f7 3253 for (; (end - 1) > (off_t)round_page_32(on+n-1); end-=PAGE_SIZE)
2d21ac55
A
3254 NBPGVALID_SET(bp, (end-1)/PAGE_SIZE);
3255 }
3256 /* Note: pages being written to will be validated when written */
3257 }
3258 }
6d2010ae 3259skipread:
2d21ac55
A
3260
3261 if (ISSET(bp->nb_flags, NB_ERROR)) {
3262 error = bp->nb_error;
3263 nfs_buf_release(bp, 1);
3264 goto out;
3265 }
3266
b0d623f7 3267 nfs_node_lock_force(np);
2d21ac55 3268 np->n_flag |= NMODIFIED;
b0d623f7 3269 nfs_node_unlock(np);
2d21ac55
A
3270
3271 NFS_BUF_MAP(bp);
3272 error = uiomove((char *)bp->nb_data + on, n, uio);
3273 if (error) {
3274 SET(bp->nb_flags, NB_ERROR);
3275 nfs_buf_release(bp, 1);
3276 goto out;
3277 }
3278
3279 /* validate any pages written to */
3280 start = on & ~PAGE_MASK;
3281 for (; start < on+n; start += PAGE_SIZE) {
3282 NBPGVALID_SET(bp, start/PAGE_SIZE);
3283 /*
3284 * This may seem a little weird, but we don't actually set the
3285 * dirty bits for writes. This is because we keep the dirty range
3286 * in the nb_dirtyoff/nb_dirtyend fields. Also, particularly for
3287 * delayed writes, when we give the pages back to the VM we don't
3288 * want to keep them marked dirty, because when we later write the
3289 * buffer we won't be able to tell which pages were written dirty
3290 * and which pages were mmapped and dirtied.
3291 */
3292 }
3293 if (bp->nb_dirtyend > 0) {
3294 bp->nb_dirtyoff = min(on, bp->nb_dirtyoff);
3295 bp->nb_dirtyend = max((on + n), bp->nb_dirtyend);
3296 } else {
3297 bp->nb_dirtyoff = on;
3298 bp->nb_dirtyend = on + n;
3299 }
3300 if (bp->nb_validend <= 0 || bp->nb_validend < bp->nb_dirtyoff ||
3301 bp->nb_validoff > bp->nb_dirtyend) {
3302 bp->nb_validoff = bp->nb_dirtyoff;
3303 bp->nb_validend = bp->nb_dirtyend;
e5568f75 3304 } else {
2d21ac55
A
3305 bp->nb_validoff = min(bp->nb_validoff, bp->nb_dirtyoff);
3306 bp->nb_validend = max(bp->nb_validend, bp->nb_dirtyend);
3307 }
3308 if (!ISSET(bp->nb_flags, NB_CACHE))
3309 nfs_buf_normalize_valid_range(np, bp);
3310
3311 /*
3312 * Since this block is being modified, it must be written
3313 * again and not just committed.
3314 */
3315 if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
b0d623f7 3316 nfs_node_lock_force(np);
2d21ac55
A
3317 if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
3318 np->n_needcommitcnt--;
3319 CHECK_NEEDCOMMITCNT(np);
e5568f75 3320 }
2d21ac55 3321 CLR(bp->nb_flags, NB_NEEDCOMMIT);
b0d623f7 3322 nfs_node_unlock(np);
e5568f75 3323 }
2d21ac55
A
3324
3325 if (ioflag & IO_SYNC) {
3326 error = nfs_buf_write(bp);
3327 if (error)
3328 goto out;
b0d623f7
A
3329 } else if (((n + on) == biosize) || (ioflag & IO_APPEND) ||
3330 (ioflag & IO_NOCACHE) || ISSET(bp->nb_flags, NB_NOCACHE)) {
2d21ac55
A
3331 SET(bp->nb_flags, NB_ASYNC);
3332 error = nfs_buf_write(bp);
3333 if (error)
3334 goto out;
e5568f75 3335 } else {
2d21ac55
A
3336 /* If the block wasn't already delayed: charge for the write */
3337 if (!ISSET(bp->nb_flags, NB_DELWRI)) {
3338 proc_t p = vfs_context_proc(ctx);
3339 if (p && p->p_stats)
b0d623f7 3340 OSIncrementAtomicLong(&p->p_stats->p_ru.ru_oublock);
2d21ac55
A
3341 }
3342 nfs_buf_write_delayed(bp);
e5568f75 3343 }
2d21ac55
A
3344 if (np->n_needcommitcnt >= NFS_A_LOT_OF_NEEDCOMMITS)
3345 nfs_flushcommits(np, 1);
3346
b0d623f7 3347 } while (uio_resid(uio) > 0 && n > 0);
2d21ac55
A
3348
3349out:
b0d623f7 3350 nfs_node_lock_force(np);
2d21ac55 3351 np->n_wrbusy--;
b0d623f7 3352 nfs_node_unlock(np);
2d21ac55 3353 nfs_data_unlock(np);
b0d623f7 3354 FSDBG_BOT(515, np, uio_offset(uio), uio_resid(uio), error);
1c79356b
A
3355 return (error);
3356}
3357
2d21ac55 3358
1c79356b 3359/*
2d21ac55 3360 * NFS write call
1c79356b
A
3361 */
3362int
2d21ac55
A
3363nfs_write_rpc(
3364 nfsnode_t np,
b0d623f7 3365 uio_t uio,
2d21ac55
A
3366 vfs_context_t ctx,
3367 int *iomodep,
3368 uint64_t *wverfp)
3369{
b0d623f7 3370 return nfs_write_rpc2(np, uio, vfs_context_thread(ctx), vfs_context_ucred(ctx), iomodep, wverfp);
2d21ac55
A
3371}
3372
3373int
3374nfs_write_rpc2(
3375 nfsnode_t np,
b0d623f7 3376 uio_t uio,
2d21ac55 3377 thread_t thd,
91447636 3378 kauth_cred_t cred,
2d21ac55 3379 int *iomodep,
8f6c56a5 3380 uint64_t *wverfp)
1c79356b 3381{
55e303ae 3382 struct nfsmount *nmp;
b0d623f7 3383 int error = 0, nfsvers;
39236c6e 3384 int wverfset, commit, committed;
2d21ac55
A
3385 uint64_t wverf = 0, wverf2;
3386 size_t nmwsize, totalsize, tsiz, len, rlen;
3387 struct nfsreq rq, *req = &rq;
b0d623f7 3388 uint32_t stategenid = 0, vrestart = 0, restart = 0;
39236c6e 3389 uio_t uio_save = NULL;
1c79356b
A
3390
3391#if DIAGNOSTIC
2d21ac55 3392 /* XXX limitation based on need to back up uio on short write */
b0d623f7 3393 if (uio_iovcnt(uio) != 1)
2d21ac55 3394 panic("nfs3_write_rpc: iovcnt > 1");
1c79356b 3395#endif
b0d623f7 3396 FSDBG_TOP(537, np, uio_offset(uio), uio_resid(uio), *iomodep);
2d21ac55 3397 nmp = NFSTONMP(np);
fe8ab488 3398 if (nfs_mount_gone(nmp))
55e303ae 3399 return (ENXIO);
2d21ac55
A
3400 nfsvers = nmp->nm_vers;
3401 nmwsize = nmp->nm_wsize;
3402
b0d623f7 3403 wverfset = 0;
2d21ac55
A
3404 committed = NFS_WRITE_FILESYNC;
3405
b0d623f7
A
3406 totalsize = tsiz = uio_resid(uio);
3407 if ((nfsvers == NFS_VER2) && ((uint64_t)(uio_offset(uio) + tsiz) > 0xffffffffULL)) {
3408 FSDBG_BOT(537, np, uio_offset(uio), uio_resid(uio), EFBIG);
1c79356b 3409 return (EFBIG);
55e303ae 3410 }
2d21ac55 3411
39236c6e
A
3412 uio_save = uio_duplicate(uio);
3413 if (uio_save == NULL) {
3414 return (EIO);
3415 }
3416
1c79356b 3417 while (tsiz > 0) {
2d21ac55 3418 len = (tsiz > nmwsize) ? nmwsize : tsiz;
b0d623f7 3419 FSDBG(537, np, uio_offset(uio), len, 0);
6d2010ae
A
3420 if (np->n_flag & NREVOKE) {
3421 error = EIO;
3422 break;
3423 }
b0d623f7
A
3424 if (nmp->nm_vers >= NFS_VER4)
3425 stategenid = nmp->nm_stategenid;
3426 error = nmp->nm_funcs->nf_write_rpc_async(np, uio, len, thd, cred, *iomodep, NULL, &req);
2d21ac55
A
3427 if (!error)
3428 error = nmp->nm_funcs->nf_write_rpc_async_finish(np, req, &commit, &rlen, &wverf2);
3429 nmp = NFSTONMP(np);
fe8ab488 3430 if (nfs_mount_gone(nmp))
55e303ae 3431 error = ENXIO;
b0d623f7
A
3432 if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error) &&
3433 (++restart <= nfs_mount_state_max_restarts(nmp))) { /* guard against no progress */
3434 lck_mtx_lock(&nmp->nm_lock);
6d2010ae
A
3435 if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) {
3436 NP(np, "nfs_write_rpc: error %d, initiating recovery", error);
3437 nfs_need_recover(nmp, error);
b0d623f7
A
3438 }
3439 lck_mtx_unlock(&nmp->nm_lock);
6d2010ae
A
3440 if (np->n_flag & NREVOKE) {
3441 error = EIO;
3442 } else {
3443 if (error == NFSERR_GRACE)
3444 tsleep(&nmp->nm_state, (PZERO-1), "nfsgrace", 2*hz);
3445 if (!(error = nfs_mount_state_wait_for_recovery(nmp)))
3446 continue;
3447 }
b0d623f7 3448 }
91447636
A
3449 if (error)
3450 break;
2d21ac55
A
3451 if (nfsvers == NFS_VER2) {
3452 tsiz -= len;
3453 continue;
1c79356b 3454 }
1c79356b 3455
2d21ac55
A
3456 /* check for a short write */
3457 if (rlen < len) {
39236c6e
A
3458 /* Reset the uio to reflect the actual transfer */
3459 *uio = *uio_save;
3460 uio_update(uio, totalsize - (tsiz - rlen));
2d21ac55 3461 len = rlen;
e5568f75 3462 }
fa4905b1 3463
2d21ac55
A
3464 /* return lowest commit level returned */
3465 if (commit < committed)
3466 committed = commit;
3467
fa4905b1 3468 tsiz -= len;
2d21ac55
A
3469
3470 /* check write verifier */
3471 if (!wverfset) {
3472 wverf = wverf2;
3473 wverfset = 1;
3474 } else if (wverf != wverf2) {
3475 /* verifier changed, so we need to restart all the writes */
b0d623f7 3476 if (++vrestart > 100) {
2d21ac55
A
3477 /* give up after too many restarts */
3478 error = EIO;
3479 break;
3480 }
39236c6e 3481 *uio = *uio_save; // Reset the uio back to the start
2d21ac55
A
3482 committed = NFS_WRITE_FILESYNC;
3483 wverfset = 0;
3484 tsiz = totalsize;
3485 }
3486 }
39236c6e
A
3487 if (uio_save)
3488 uio_free(uio_save);
2d21ac55
A
3489 if (wverfset && wverfp)
3490 *wverfp = wverf;
3491 *iomodep = committed;
3492 if (error)
b0d623f7
A
3493 uio_setresid(uio, tsiz);
3494 FSDBG_BOT(537, np, committed, uio_resid(uio), error);
2d21ac55
A
3495 return (error);
3496}
3497
3498int
3499nfs3_write_rpc_async(
3500 nfsnode_t np,
b0d623f7 3501 uio_t uio,
2d21ac55
A
3502 size_t len,
3503 thread_t thd,
3504 kauth_cred_t cred,
3505 int iomode,
3506 struct nfsreq_cbinfo *cb,
3507 struct nfsreq **reqp)
3508{
3509 struct nfsmount *nmp;
6d2010ae 3510 mount_t mp;
2d21ac55 3511 int error = 0, nfsvers;
2d21ac55
A
3512 struct nfsm_chain nmreq;
3513
3514 nmp = NFSTONMP(np);
fe8ab488 3515 if (nfs_mount_gone(nmp))
2d21ac55
A
3516 return (ENXIO);
3517 nfsvers = nmp->nm_vers;
3518
6d2010ae
A
3519 /* for async mounts, don't bother sending sync write requests */
3520 if ((iomode != NFS_WRITE_UNSTABLE) && nfs_allow_async &&
3521 ((mp = NFSTOMP(np))) && (vfs_flags(mp) & MNT_ASYNC))
3522 iomode = NFS_WRITE_UNSTABLE;
3523
2d21ac55
A
3524 nfsm_chain_null(&nmreq);
3525 nfsm_chain_build_alloc_init(error, &nmreq,
3526 NFSX_FH(nfsvers) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
3527 nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
3528 if (nfsvers == NFS_VER3) {
b0d623f7 3529 nfsm_chain_add_64(error, &nmreq, uio_offset(uio));
2d21ac55
A
3530 nfsm_chain_add_32(error, &nmreq, len);
3531 nfsm_chain_add_32(error, &nmreq, iomode);
3532 } else {
3533 nfsm_chain_add_32(error, &nmreq, 0);
b0d623f7 3534 nfsm_chain_add_32(error, &nmreq, uio_offset(uio));
2d21ac55
A
3535 nfsm_chain_add_32(error, &nmreq, 0);
3536 }
3537 nfsm_chain_add_32(error, &nmreq, len);
3538 nfsmout_if(error);
b0d623f7 3539 error = nfsm_chain_add_uio(&nmreq, uio, len);
2d21ac55
A
3540 nfsm_chain_build_done(error, &nmreq);
3541 nfsmout_if(error);
6d2010ae 3542 error = nfs_request_async(np, NULL, &nmreq, NFSPROC_WRITE, thd, cred, NULL, 0, cb, reqp);
2d21ac55
A
3543nfsmout:
3544 nfsm_chain_cleanup(&nmreq);
3545 return (error);
3546}
3547
3548int
3549nfs3_write_rpc_async_finish(
3550 nfsnode_t np,
3551 struct nfsreq *req,
3552 int *iomodep,
3553 size_t *rlenp,
3554 uint64_t *wverfp)
3555{
3556 struct nfsmount *nmp;
3557 int error = 0, lockerror = ENOENT, nfsvers, status;
3558 int updatemtime = 0, wccpostattr = 0, rlen, committed = NFS_WRITE_FILESYNC;
3559 u_int64_t xid, wverf;
3560 mount_t mp;
3561 struct nfsm_chain nmrep;
3562
3563 nmp = NFSTONMP(np);
fe8ab488 3564 if (nfs_mount_gone(nmp)) {
2d21ac55
A
3565 nfs_request_async_cancel(req);
3566 return (ENXIO);
3567 }
3568 nfsvers = nmp->nm_vers;
3569
3570 nfsm_chain_null(&nmrep);
3571
3572 error = nfs_request_async_finish(req, &nmrep, &xid, &status);
3573 if (error == EINPROGRESS) /* async request restarted */
3574 return (error);
3575 nmp = NFSTONMP(np);
fe8ab488 3576 if (nfs_mount_gone(nmp))
2d21ac55 3577 error = ENXIO;
b0d623f7 3578 if (!error && (lockerror = nfs_node_lock(np)))
2d21ac55
A
3579 error = lockerror;
3580 if (nfsvers == NFS_VER3) {
3581 struct timespec premtime = { 0, 0 };
3582 nfsm_chain_get_wcc_data(error, &nmrep, np, &premtime, &wccpostattr, &xid);
3583 if (nfstimespeccmp(&np->n_mtime, &premtime, ==))
3584 updatemtime = 1;
3585 if (!error)
3586 error = status;
3587 nfsm_chain_get_32(error, &nmrep, rlen);
3588 nfsmout_if(error);
3589 *rlenp = rlen;
3590 if (rlen <= 0)
3591 error = NFSERR_IO;
3592 nfsm_chain_get_32(error, &nmrep, committed);
3593 nfsm_chain_get_64(error, &nmrep, wverf);
3594 nfsmout_if(error);
3595 if (wverfp)
3596 *wverfp = wverf;
3597 lck_mtx_lock(&nmp->nm_lock);
3598 if (!(nmp->nm_state & NFSSTA_HASWRITEVERF)) {
3599 nmp->nm_verf = wverf;
3600 nmp->nm_state |= NFSSTA_HASWRITEVERF;
3601 } else if (nmp->nm_verf != wverf) {
3602 nmp->nm_verf = wverf;
3603 }
3604 lck_mtx_unlock(&nmp->nm_lock);
3605 } else {
3606 if (!error)
3607 error = status;
6d2010ae 3608 nfsm_chain_loadattr(error, &nmrep, np, nfsvers, &xid);
2d21ac55 3609 nfsmout_if(error);
1c79356b 3610 }
2d21ac55
A
3611 if (updatemtime)
3612 NFS_CHANGED_UPDATE(nfsvers, np, &np->n_vattr);
1c79356b 3613nfsmout:
2d21ac55 3614 if (!lockerror)
b0d623f7 3615 nfs_node_unlock(np);
2d21ac55
A
3616 nfsm_chain_cleanup(&nmrep);
3617 if ((committed != NFS_WRITE_FILESYNC) && nfs_allow_async &&
3618 ((mp = NFSTOMP(np))) && (vfs_flags(mp) & MNT_ASYNC))
3619 committed = NFS_WRITE_FILESYNC;
3620 *iomodep = committed;
1c79356b
A
3621 return (error);
3622}
3623
3624/*
2d21ac55
A
3625 * NFS mknod vnode op
3626 *
3627 * For NFS v2 this is a kludge. Use a create RPC but with the IFMT bits of the
1c79356b
A
3628 * mode set to specify the file type and the size field for rdev.
3629 */
b0d623f7 3630int
2d21ac55
A
3631nfs3_vnop_mknod(
3632 struct vnop_mknod_args /* {
3633 struct vnodeop_desc *a_desc;
3634 vnode_t a_dvp;
3635 vnode_t *a_vpp;
3636 struct componentname *a_cnp;
3637 struct vnode_attr *a_vap;
3638 vfs_context_t a_context;
3639 } */ *ap)
1c79356b 3640{
2d21ac55
A
3641 vnode_t dvp = ap->a_dvp;
3642 vnode_t *vpp = ap->a_vpp;
3643 struct componentname *cnp = ap->a_cnp;
3644 struct vnode_attr *vap = ap->a_vap;
3645 vfs_context_t ctx = ap->a_context;
3646 vnode_t newvp = NULL;
3647 nfsnode_t np = NULL;
3648 struct nfsmount *nmp;
3649 nfsnode_t dnp = VTONFS(dvp);
6d2010ae 3650 struct nfs_vattr nvattr;
2d21ac55 3651 fhandle_t fh;
b0d623f7 3652 int error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0;
91447636 3653 struct timespec premtime = { 0, 0 };
b0d623f7 3654 u_int32_t rdev;
39236c6e 3655 u_int64_t xid = 0, dxid;
2d21ac55
A
3656 int nfsvers, gotuid, gotgid;
3657 struct nfsm_chain nmreq, nmrep;
6d2010ae 3658 struct nfsreq rq, *req = &rq;
2d21ac55
A
3659
3660 nmp = VTONMP(dvp);
fe8ab488 3661 if (nfs_mount_gone(nmp))
2d21ac55
A
3662 return (ENXIO);
3663 nfsvers = nmp->nm_vers;
1c79356b 3664
91447636
A
3665 if (!VATTR_IS_ACTIVE(vap, va_type))
3666 return (EINVAL);
3667 if (vap->va_type == VCHR || vap->va_type == VBLK) {
3668 if (!VATTR_IS_ACTIVE(vap, va_rdev))
3669 return (EINVAL);
2d21ac55 3670 rdev = vap->va_rdev;
91447636 3671 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1c79356b
A
3672 rdev = 0xffffffff;
3673 else {
91447636 3674 return (ENOTSUP);
1c79356b 3675 }
2d21ac55
A
3676 if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN))
3677 return (ENAMETOOLONG);
91447636 3678
6d2010ae
A
3679 nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx);
3680
91447636
A
3681 VATTR_SET_SUPPORTED(vap, va_mode);
3682 VATTR_SET_SUPPORTED(vap, va_uid);
3683 VATTR_SET_SUPPORTED(vap, va_gid);
3684 VATTR_SET_SUPPORTED(vap, va_data_size);
3685 VATTR_SET_SUPPORTED(vap, va_access_time);
3686 VATTR_SET_SUPPORTED(vap, va_modify_time);
3687 gotuid = VATTR_IS_ACTIVE(vap, va_uid);
3688 gotgid = VATTR_IS_ACTIVE(vap, va_gid);
3689
2d21ac55
A
3690 nfsm_chain_null(&nmreq);
3691 nfsm_chain_null(&nmrep);
3692
3693 nfsm_chain_build_alloc_init(error, &nmreq,
3694 NFSX_FH(nfsvers) + 4 * NFSX_UNSIGNED +
3695 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(nfsvers));
3696 nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
6d2010ae 3697 nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp);
2d21ac55
A
3698 if (nfsvers == NFS_VER3) {
3699 nfsm_chain_add_32(error, &nmreq, vtonfs_type(vap->va_type, nfsvers));
3700 nfsm_chain_add_v3sattr(error, &nmreq, vap);
1c79356b 3701 if (vap->va_type == VCHR || vap->va_type == VBLK) {
2d21ac55
A
3702 nfsm_chain_add_32(error, &nmreq, major(vap->va_rdev));
3703 nfsm_chain_add_32(error, &nmreq, minor(vap->va_rdev));
1c79356b
A
3704 }
3705 } else {
2d21ac55 3706 nfsm_chain_add_v2sattr(error, &nmreq, vap, rdev);
91447636 3707 }
2d21ac55 3708 nfsm_chain_build_done(error, &nmreq);
b0d623f7
A
3709 if (!error)
3710 error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx));
2d21ac55
A
3711 nfsmout_if(error);
3712
6d2010ae
A
3713 error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_MKNOD,
3714 vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req);
3715 if (!error)
3716 error = nfs_request_async_finish(req, &nmrep, &xid, &status);
2d21ac55 3717
b0d623f7
A
3718 if ((lockerror = nfs_node_lock(dnp)))
3719 error = lockerror;
91447636 3720 /* XXX no EEXIST kludge here? */
2d21ac55
A
3721 dxid = xid;
3722 if (!error && !status) {
3723 if (dnp->n_flag & NNEGNCENTRIES) {
3724 dnp->n_flag &= ~NNEGNCENTRIES;
3725 cache_purge_negatives(dvp);
3726 }
3727 error = nfsm_chain_get_fh_attr(&nmrep, dnp, ctx, nfsvers, &xid, &fh, &nvattr);
3728 }
3729 if (nfsvers == NFS_VER3)
3730 nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid);
3731 if (!error)
3732 error = status;
3733nfsmout:
3734 nfsm_chain_cleanup(&nmreq);
3735 nfsm_chain_cleanup(&nmrep);
3736
3737 if (!lockerror) {
3738 dnp->n_flag |= NMODIFIED;
3739 /* if directory hadn't changed, update namecache mtime */
3740 if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==))
3741 NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr);
b0d623f7
A
3742 nfs_node_unlock(dnp);
3743 /* nfs_getattr() will check changed and purge caches */
6d2010ae 3744 nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED);
1c79356b 3745 }
2d21ac55
A
3746
3747 if (!error && fh.fh_len)
6d2010ae 3748 error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np);
2d21ac55
A
3749 if (!error && !np)
3750 error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np);
3751 if (!error && np)
3752 newvp = NFSTOV(np);
b0d623f7
A
3753 if (!busyerror)
3754 nfs_node_clear_busy(dnp);
2d21ac55 3755
91447636 3756 if (!error && (gotuid || gotgid) &&
6d2010ae 3757 (!newvp || nfs_getattrcache(np, &nvattr, 0) ||
91447636
A
3758 (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
3759 (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
3760 /* clear ID bits if server didn't use them (or we can't tell) */
3761 VATTR_CLEAR_SUPPORTED(vap, va_uid);
3762 VATTR_CLEAR_SUPPORTED(vap, va_gid);
3763 }
1c79356b 3764 if (error) {
2d21ac55 3765 if (newvp) {
b0d623f7 3766 nfs_node_unlock(np);
91447636 3767 vnode_put(newvp);
2d21ac55 3768 }
1c79356b 3769 } else {
1c79356b 3770 *vpp = newvp;
b0d623f7 3771 nfs_node_unlock(np);
1c79356b 3772 }
1c79356b
A
3773 return (error);
3774}
3775
b0d623f7 3776static uint32_t create_verf;
1c79356b 3777/*
2d21ac55 3778 * NFS file create call
1c79356b 3779 */
b0d623f7 3780int
2d21ac55 3781nfs3_vnop_create(
91447636
A
3782 struct vnop_create_args /* {
3783 struct vnodeop_desc *a_desc;
3784 vnode_t a_dvp;
3785 vnode_t *a_vpp;
1c79356b 3786 struct componentname *a_cnp;
91447636
A
3787 struct vnode_attr *a_vap;
3788 vfs_context_t a_context;
2d21ac55 3789 } */ *ap)
1c79356b 3790{
2d21ac55 3791 vfs_context_t ctx = ap->a_context;
91447636
A
3792 vnode_t dvp = ap->a_dvp;
3793 struct vnode_attr *vap = ap->a_vap;
3794 struct componentname *cnp = ap->a_cnp;
6d2010ae 3795 struct nfs_vattr nvattr;
2d21ac55
A
3796 fhandle_t fh;
3797 nfsnode_t np = NULL;
3798 struct nfsmount *nmp;
3799 nfsnode_t dnp = VTONFS(dvp);
3800 vnode_t newvp = NULL;
b0d623f7 3801 int error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0, fmode = 0;
91447636 3802 struct timespec premtime = { 0, 0 };
2d21ac55
A
3803 int nfsvers, gotuid, gotgid;
3804 u_int64_t xid, dxid;
3805 uint32_t val;
3806 struct nfsm_chain nmreq, nmrep;
6d2010ae 3807 struct nfsreq rq, *req = &rq;
2d21ac55 3808 struct nfs_dulookup dul;
91447636 3809
2d21ac55 3810 nmp = VTONMP(dvp);
fe8ab488 3811 if (nfs_mount_gone(nmp))
2d21ac55
A
3812 return (ENXIO);
3813 nfsvers = nmp->nm_vers;
1c79356b 3814
2d21ac55
A
3815 if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN))
3816 return (ENAMETOOLONG);
91447636 3817
6d2010ae
A
3818 nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx);
3819
91447636
A
3820 VATTR_SET_SUPPORTED(vap, va_mode);
3821 VATTR_SET_SUPPORTED(vap, va_uid);
3822 VATTR_SET_SUPPORTED(vap, va_gid);
3823 VATTR_SET_SUPPORTED(vap, va_data_size);
3824 VATTR_SET_SUPPORTED(vap, va_access_time);
3825 VATTR_SET_SUPPORTED(vap, va_modify_time);
3826 gotuid = VATTR_IS_ACTIVE(vap, va_uid);
3827 gotgid = VATTR_IS_ACTIVE(vap, va_gid);
1c79356b 3828
6d2010ae 3829 if (vap->va_vaflags & VA_EXCLUSIVE) {
1c79356b 3830 fmode |= O_EXCL;
6d2010ae
A
3831 if (!VATTR_IS_ACTIVE(vap, va_access_time) || !VATTR_IS_ACTIVE(vap, va_modify_time))
3832 vap->va_vaflags |= VA_UTIMES_NULL;
3833 }
2d21ac55 3834
1c79356b 3835again:
b0d623f7
A
3836 error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx));
3837 nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx);
2d21ac55
A
3838
3839 nfsm_chain_null(&nmreq);
3840 nfsm_chain_null(&nmrep);
3841
3842 nfsm_chain_build_alloc_init(error, &nmreq,
3843 NFSX_FH(nfsvers) + 2 * NFSX_UNSIGNED +
3844 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(nfsvers));
3845 nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
6d2010ae 3846 nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp);
2d21ac55 3847 if (nfsvers == NFS_VER3) {
1c79356b 3848 if (fmode & O_EXCL) {
2d21ac55 3849 nfsm_chain_add_32(error, &nmreq, NFS_CREATE_EXCLUSIVE);
b0d623f7 3850 lck_rw_lock_shared(in_ifaddr_rwlock);
2d21ac55
A
3851 if (!TAILQ_EMPTY(&in_ifaddrhead))
3852 val = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr.s_addr;
3853 else
3854 val = create_verf;
b0d623f7 3855 lck_rw_done(in_ifaddr_rwlock);
2d21ac55
A
3856 nfsm_chain_add_32(error, &nmreq, val);
3857 ++create_verf;
3858 nfsm_chain_add_32(error, &nmreq, create_verf);
1c79356b 3859 } else {
2d21ac55
A
3860 nfsm_chain_add_32(error, &nmreq, NFS_CREATE_UNCHECKED);
3861 nfsm_chain_add_v3sattr(error, &nmreq, vap);
1c79356b
A
3862 }
3863 } else {
2d21ac55 3864 nfsm_chain_add_v2sattr(error, &nmreq, vap, 0);
1c79356b 3865 }
2d21ac55
A
3866 nfsm_chain_build_done(error, &nmreq);
3867 nfsmout_if(error);
2d21ac55
A
3868
3869 error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_CREATE,
6d2010ae 3870 vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req);
1c79356b 3871 if (!error) {
2d21ac55
A
3872 nfs_dulookup_start(&dul, dnp, ctx);
3873 error = nfs_request_async_finish(req, &nmrep, &xid, &status);
3874 }
3875
b0d623f7
A
3876 if ((lockerror = nfs_node_lock(dnp)))
3877 error = lockerror;
2d21ac55
A
3878 dxid = xid;
3879 if (!error && !status) {
3880 if (dnp->n_flag & NNEGNCENTRIES) {
3881 dnp->n_flag &= ~NNEGNCENTRIES;
3882 cache_purge_negatives(dvp);
3883 }
3884 error = nfsm_chain_get_fh_attr(&nmrep, dnp, ctx, nfsvers, &xid, &fh, &nvattr);
3885 }
3886 if (nfsvers == NFS_VER3)
3887 nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid);
3888 if (!error)
3889 error = status;
3890nfsmout:
3891 nfsm_chain_cleanup(&nmreq);
3892 nfsm_chain_cleanup(&nmrep);
3893
3894 if (!lockerror) {
3895 dnp->n_flag |= NMODIFIED;
3896 /* if directory hadn't changed, update namecache mtime */
3897 if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==))
3898 NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr);
b0d623f7
A
3899 nfs_node_unlock(dnp);
3900 /* nfs_getattr() will check changed and purge caches */
6d2010ae 3901 nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED);
1c79356b 3902 }
2d21ac55
A
3903
3904 if (!error && fh.fh_len)
6d2010ae 3905 error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np);
2d21ac55
A
3906 if (!error && !np)
3907 error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np);
3908 if (!error && np)
3909 newvp = NFSTOV(np);
3910
3911 nfs_dulookup_finish(&dul, dnp, ctx);
b0d623f7
A
3912 if (!busyerror)
3913 nfs_node_clear_busy(dnp);
2d21ac55 3914
1c79356b 3915 if (error) {
2d21ac55 3916 if ((nfsvers == NFS_VER3) && (fmode & O_EXCL) && (error == NFSERR_NOTSUPP)) {
1c79356b
A
3917 fmode &= ~O_EXCL;
3918 goto again;
3919 }
2d21ac55 3920 if (newvp) {
b0d623f7 3921 nfs_node_unlock(np);
91447636 3922 vnode_put(newvp);
2d21ac55
A
3923 }
3924 } else if ((nfsvers == NFS_VER3) && (fmode & O_EXCL)) {
b0d623f7
A
3925 nfs_node_unlock(np);
3926 error = nfs3_setattr_rpc(np, vap, ctx);
91447636
A
3927 if (error && (gotuid || gotgid)) {
3928 /* it's possible the server didn't like our attempt to set IDs. */
3929 /* so, let's try it again without those */
3930 VATTR_CLEAR_ACTIVE(vap, va_uid);
3931 VATTR_CLEAR_ACTIVE(vap, va_gid);
b0d623f7 3932 error = nfs3_setattr_rpc(np, vap, ctx);
91447636 3933 }
b0d623f7 3934 if (error)
91447636 3935 vnode_put(newvp);
b0d623f7
A
3936 else
3937 nfs_node_lock_force(np);
ccc36f2f 3938 }
2d21ac55 3939 if (!error)
1c79356b 3940 *ap->a_vpp = newvp;
91447636 3941 if (!error && (gotuid || gotgid) &&
6d2010ae 3942 (!newvp || nfs_getattrcache(np, &nvattr, 0) ||
91447636
A
3943 (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
3944 (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
3945 /* clear ID bits if server didn't use them (or we can't tell) */
3946 VATTR_CLEAR_SUPPORTED(vap, va_uid);
3947 VATTR_CLEAR_SUPPORTED(vap, va_gid);
3948 }
2d21ac55 3949 if (!error)
b0d623f7 3950 nfs_node_unlock(np);
1c79356b
A
3951 return (error);
3952}
3953
3954/*
2d21ac55
A
3955 * NFS file remove call
3956 * To try and make NFS semantics closer to UFS semantics, a file that has
1c79356b
A
3957 * other processes using the vnode is renamed instead of removed and then
3958 * removed later on the last close.
91447636 3959 * - If vnode_isinuse()
1c79356b
A
3960 * If a rename is not already in the works
3961 * call nfs_sillyrename() to set it up
3962 * else
2d21ac55 3963 * do the remove RPC
1c79356b 3964 */
b0d623f7 3965int
2d21ac55 3966nfs_vnop_remove(
91447636 3967 struct vnop_remove_args /* {
1c79356b 3968 struct vnodeop_desc *a_desc;
91447636
A
3969 vnode_t a_dvp;
3970 vnode_t a_vp;
3971 struct componentname *a_cnp;
3972 int a_flags;
3973 vfs_context_t a_context;
2d21ac55 3974 } */ *ap)
1c79356b 3975{
2d21ac55 3976 vfs_context_t ctx = ap->a_context;
91447636
A
3977 vnode_t vp = ap->a_vp;
3978 vnode_t dvp = ap->a_dvp;
3979 struct componentname *cnp = ap->a_cnp;
2d21ac55
A
3980 nfsnode_t dnp = VTONFS(dvp);
3981 nfsnode_t np = VTONFS(vp);
6d2010ae 3982 int error = 0, nfsvers, namedattrs, inuse, gotattr = 0, flushed = 0, setsize = 0;
91447636 3983 struct nfs_vattr nvattr;
2d21ac55
A
3984 struct nfsmount *nmp;
3985 struct nfs_dulookup dul;
3986
3987 /* XXX prevent removing a sillyrenamed file? */
3988
3989 nmp = NFSTONMP(dnp);
fe8ab488 3990 if (nfs_mount_gone(nmp))
2d21ac55
A
3991 return (ENXIO);
3992 nfsvers = nmp->nm_vers;
6d2010ae 3993 namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR);
2d21ac55
A
3994
3995again_relock:
b0d623f7 3996 error = nfs_node_set_busy2(dnp, np, vfs_context_thread(ctx));
2d21ac55
A
3997 if (error)
3998 return (error);
1c79356b 3999
2d21ac55
A
4000 /* lock the node while we remove the file */
4001 lck_mtx_lock(nfs_node_hash_mutex);
4002 while (np->n_hflag & NHLOCKED) {
4003 np->n_hflag |= NHLOCKWANT;
4004 msleep(np, nfs_node_hash_mutex, PINOD, "nfs_remove", NULL);
4005 }
4006 np->n_hflag |= NHLOCKED;
4007 lck_mtx_unlock(nfs_node_hash_mutex);
9bccf70c 4008
6d2010ae
A
4009 if (!namedattrs)
4010 nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx);
2d21ac55
A
4011again:
4012 inuse = vnode_isinuse(vp, 0);
4013 if ((ap->a_flags & VNODE_REMOVE_NODELETEBUSY) && inuse) {
55e303ae 4014 /* Caller requested Carbon delete semantics, but file is busy */
2d21ac55
A
4015 error = EBUSY;
4016 goto out;
55e303ae 4017 }
2d21ac55 4018 if (inuse && !gotattr) {
b0d623f7 4019 if (nfs_getattr(np, &nvattr, ctx, NGA_CACHED))
2d21ac55
A
4020 nvattr.nva_nlink = 1;
4021 gotattr = 1;
4022 goto again;
4023 }
4024 if (!inuse || (np->n_sillyrename && (nvattr.nva_nlink > 1))) {
4025
4026 if (!inuse && !flushed) { /* flush all the buffers first */
4027 /* unlock the node */
4028 lck_mtx_lock(nfs_node_hash_mutex);
4029 np->n_hflag &= ~NHLOCKED;
4030 if (np->n_hflag & NHLOCKWANT) {
4031 np->n_hflag &= ~NHLOCKWANT;
4032 wakeup(np);
4033 }
4034 lck_mtx_unlock(nfs_node_hash_mutex);
b0d623f7 4035 nfs_node_clear_busy2(dnp, np);
2d21ac55
A
4036 error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1);
4037 FSDBG(260, np, np->n_size, np->n_vattr.nva_size, 0xf00d0011);
4038 flushed = 1;
4039 if (error == EINTR) {
b0d623f7 4040 nfs_node_lock_force(np);
2d21ac55 4041 NATTRINVALIDATE(np);
b0d623f7 4042 nfs_node_unlock(np);
2d21ac55
A
4043 return (error);
4044 }
6d2010ae
A
4045 if (!namedattrs)
4046 nfs_dulookup_finish(&dul, dnp, ctx);
2d21ac55
A
4047 goto again_relock;
4048 }
4049
6d2010ae
A
4050 if ((nmp->nm_vers >= NFS_VER4) && (np->n_openflags & N_DELEG_MASK))
4051 nfs4_delegation_return(np, 0, vfs_context_thread(ctx), vfs_context_ucred(ctx));
b0d623f7 4052
1c79356b
A
4053 /*
4054 * Purge the name cache so that the chance of a lookup for
4055 * the name succeeding while the remove is in progress is
483a1d10 4056 * minimized.
1c79356b 4057 */
b0d623f7 4058 nfs_name_cache_purge(dnp, np, cnp, ctx);
2d21ac55 4059
6d2010ae
A
4060 if (!namedattrs)
4061 nfs_dulookup_start(&dul, dnp, ctx);
2d21ac55 4062
1c79356b 4063 /* Do the rpc */
2d21ac55
A
4064 error = nmp->nm_funcs->nf_remove_rpc(dnp, cnp->cn_nameptr, cnp->cn_namelen,
4065 vfs_context_thread(ctx), vfs_context_ucred(ctx));
4066
1c79356b
A
4067 /*
4068 * Kludge City: If the first reply to the remove rpc is lost..
4069 * the reply to the retransmitted request will be ENOENT
4070 * since the file was in fact removed
4071 * Therefore, we cheat and return success.
4072 */
4073 if (error == ENOENT)
4074 error = 0;
2d21ac55
A
4075
4076 if (!error && !inuse && !np->n_sillyrename) {
55e303ae 4077 /*
2d21ac55 4078 * removal succeeded, it's not in use, and not silly renamed so
55e303ae
A
4079 * remove nfsnode from hash now so we can't accidentally find it
4080 * again if another object gets created with the same filehandle
4081 * before this vnode gets reclaimed
4082 */
91447636 4083 lck_mtx_lock(nfs_node_hash_mutex);
2d21ac55
A
4084 if (np->n_hflag & NHHASHED) {
4085 LIST_REMOVE(np, n_hash);
4086 np->n_hflag &= ~NHHASHED;
4087 FSDBG(266, 0, np, np->n_flag, 0xb1eb1e);
4088 }
91447636 4089 lck_mtx_unlock(nfs_node_hash_mutex);
2d21ac55 4090 /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */
91447636 4091 /* clear all flags other than these */
b0d623f7 4092 nfs_node_lock_force(np);
2d21ac55 4093 np->n_flag &= (NMODIFIED);
2d21ac55 4094 NATTRINVALIDATE(np);
b0d623f7
A
4095 nfs_node_unlock(np);
4096 vnode_recycle(vp);
2d21ac55
A
4097 setsize = 1;
4098 } else {
b0d623f7 4099 nfs_node_lock_force(np);
2d21ac55 4100 NATTRINVALIDATE(np);
b0d623f7 4101 nfs_node_unlock(np);
55e303ae 4102 }
1c79356b 4103 } else if (!np->n_sillyrename) {
6d2010ae
A
4104 if (!namedattrs)
4105 nfs_dulookup_start(&dul, dnp, ctx);
2d21ac55 4106 error = nfs_sillyrename(dnp, np, cnp, ctx);
b0d623f7 4107 nfs_node_lock_force(np);
2d21ac55 4108 NATTRINVALIDATE(np);
b0d623f7 4109 nfs_node_unlock(np);
2d21ac55 4110 } else {
b0d623f7 4111 nfs_node_lock_force(np);
2d21ac55 4112 NATTRINVALIDATE(np);
b0d623f7 4113 nfs_node_unlock(np);
6d2010ae
A
4114 if (!namedattrs)
4115 nfs_dulookup_start(&dul, dnp, ctx);
1c79356b 4116 }
1c79356b 4117
b0d623f7 4118 /* nfs_getattr() will check changed and purge caches */
6d2010ae
A
4119 nfs_getattr(dnp, NULL, ctx, NGA_CACHED);
4120 if (!namedattrs)
4121 nfs_dulookup_finish(&dul, dnp, ctx);
2d21ac55
A
4122out:
4123 /* unlock the node */
4124 lck_mtx_lock(nfs_node_hash_mutex);
4125 np->n_hflag &= ~NHLOCKED;
4126 if (np->n_hflag & NHLOCKWANT) {
4127 np->n_hflag &= ~NHLOCKWANT;
4128 wakeup(np);
4129 }
4130 lck_mtx_unlock(nfs_node_hash_mutex);
b0d623f7 4131 nfs_node_clear_busy2(dnp, np);
2d21ac55
A
4132 if (setsize)
4133 ubc_setsize(vp, 0);
1c79356b
A
4134 return (error);
4135}
4136
4137/*
2d21ac55 4138 * NFS silly-renamed file removal function called from nfs_vnop_inactive
1c79356b
A
4139 */
4140int
2d21ac55 4141nfs_removeit(struct nfs_sillyrename *nsp)
1c79356b 4142{
2d21ac55 4143 struct nfsmount *nmp = NFSTONMP(nsp->nsr_dnp);
fe8ab488 4144 if (nfs_mount_gone(nmp))
2d21ac55
A
4145 return (ENXIO);
4146 return nmp->nm_funcs->nf_remove_rpc(nsp->nsr_dnp, nsp->nsr_name, nsp->nsr_namlen, NULL, nsp->nsr_cred);
1c79356b
A
4147}
4148
4149/*
2d21ac55 4150 * NFS remove rpc, called from nfs_remove() and nfs_removeit().
1c79356b 4151 */
2d21ac55
A
4152int
4153nfs3_remove_rpc(
4154 nfsnode_t dnp,
4155 char *name,
4156 int namelen,
4157 thread_t thd,
4158 kauth_cred_t cred)
1c79356b 4159{
b0d623f7 4160 int error = 0, lockerror = ENOENT, status, wccpostattr = 0;
91447636 4161 struct timespec premtime = { 0, 0 };
2d21ac55
A
4162 struct nfsmount *nmp;
4163 int nfsvers;
fa4905b1 4164 u_int64_t xid;
2d21ac55 4165 struct nfsm_chain nmreq, nmrep;
1c79356b 4166
2d21ac55 4167 nmp = NFSTONMP(dnp);
fe8ab488 4168 if (nfs_mount_gone(nmp))
55e303ae 4169 return (ENXIO);
2d21ac55
A
4170 nfsvers = nmp->nm_vers;
4171 if ((nfsvers == NFS_VER2) && (namelen > NFS_MAXNAMLEN))
4172 return (ENAMETOOLONG);
55e303ae 4173
2d21ac55
A
4174 nfsm_chain_null(&nmreq);
4175 nfsm_chain_null(&nmrep);
4176
4177 nfsm_chain_build_alloc_init(error, &nmreq,
4178 NFSX_FH(nfsvers) + NFSX_UNSIGNED + nfsm_rndup(namelen));
4179 nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
6d2010ae 4180 nfsm_chain_add_name(error, &nmreq, name, namelen, nmp);
2d21ac55
A
4181 nfsm_chain_build_done(error, &nmreq);
4182 nfsmout_if(error);
4183
6d2010ae 4184 error = nfs_request2(dnp, NULL, &nmreq, NFSPROC_REMOVE, thd, cred, NULL, 0, &nmrep, &xid, &status);
2d21ac55 4185
b0d623f7
A
4186 if ((lockerror = nfs_node_lock(dnp)))
4187 error = lockerror;
2d21ac55
A
4188 if (nfsvers == NFS_VER3)
4189 nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &xid);
b0d623f7 4190 nfsmout_if(error);
2d21ac55 4191 dnp->n_flag |= NMODIFIED;
483a1d10 4192 /* if directory hadn't changed, update namecache mtime */
2d21ac55
A
4193 if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==))
4194 NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr);
483a1d10 4195 if (!wccpostattr)
2d21ac55
A
4196 NATTRINVALIDATE(dnp);
4197 if (!error)
4198 error = status;
4199nfsmout:
b0d623f7
A
4200 if (!lockerror)
4201 nfs_node_unlock(dnp);
2d21ac55
A
4202 nfsm_chain_cleanup(&nmreq);
4203 nfsm_chain_cleanup(&nmrep);
1c79356b
A
4204 return (error);
4205}
4206
4207/*
2d21ac55 4208 * NFS file rename call
1c79356b 4209 */
b0d623f7 4210int
2d21ac55 4211nfs_vnop_rename(
91447636
A
4212 struct vnop_rename_args /* {
4213 struct vnodeop_desc *a_desc;
4214 vnode_t a_fdvp;
4215 vnode_t a_fvp;
1c79356b 4216 struct componentname *a_fcnp;
91447636
A
4217 vnode_t a_tdvp;
4218 vnode_t a_tvp;
1c79356b 4219 struct componentname *a_tcnp;
91447636 4220 vfs_context_t a_context;
2d21ac55 4221 } */ *ap)
1c79356b 4222{
2d21ac55 4223 vfs_context_t ctx = ap->a_context;
91447636 4224 vnode_t fdvp = ap->a_fdvp;
2d21ac55 4225 vnode_t fvp = ap->a_fvp;
91447636 4226 vnode_t tdvp = ap->a_tdvp;
2d21ac55
A
4227 vnode_t tvp = ap->a_tvp;
4228 nfsnode_t fdnp, fnp, tdnp, tnp;
91447636
A
4229 struct componentname *tcnp = ap->a_tcnp;
4230 struct componentname *fcnp = ap->a_fcnp;
2d21ac55 4231 int error, nfsvers, inuse=0, tvprecycle=0, locked=0;
91447636 4232 mount_t fmp, tdmp, tmp;
2d21ac55
A
4233 struct nfs_vattr nvattr;
4234 struct nfsmount *nmp;
91447636 4235
2d21ac55
A
4236 fdnp = VTONFS(fdvp);
4237 fnp = VTONFS(fvp);
4238 tdnp = VTONFS(tdvp);
91447636 4239 tnp = tvp ? VTONFS(tvp) : NULL;
1c79356b 4240
2d21ac55 4241 nmp = NFSTONMP(fdnp);
fe8ab488 4242 if (nfs_mount_gone(nmp))
2d21ac55
A
4243 return (ENXIO);
4244 nfsvers = nmp->nm_vers;
4245
b0d623f7 4246 error = nfs_node_set_busy4(fdnp, fnp, tdnp, tnp, vfs_context_thread(ctx));
2d21ac55
A
4247 if (error)
4248 return (error);
4249
4250 if (tvp && (tvp != fvp)) {
4251 /* lock the node while we rename over the existing file */
4252 lck_mtx_lock(nfs_node_hash_mutex);
4253 while (tnp->n_hflag & NHLOCKED) {
4254 tnp->n_hflag |= NHLOCKWANT;
4255 msleep(tnp, nfs_node_hash_mutex, PINOD, "nfs_rename", NULL);
4256 }
4257 tnp->n_hflag |= NHLOCKED;
4258 lck_mtx_unlock(nfs_node_hash_mutex);
4259 locked = 1;
4260 }
4261
1c79356b 4262 /* Check for cross-device rename */
91447636
A
4263 fmp = vnode_mount(fvp);
4264 tmp = tvp ? vnode_mount(tvp) : NULL;
4265 tdmp = vnode_mount(tdvp);
4266 if ((fmp != tdmp) || (tvp && (fmp != tmp))) {
1c79356b
A
4267 error = EXDEV;
4268 goto out;
4269 }
4270
2d21ac55
A
4271 /* XXX prevent renaming from/over a sillyrenamed file? */
4272
1c79356b
A
4273 /*
4274 * If the tvp exists and is in use, sillyrename it before doing the
4275 * rename of the new file over it.
4276 * XXX Can't sillyrename a directory.
9bccf70c
A
4277 * Don't sillyrename if source and target are same vnode (hard
4278 * links or case-variants)
1c79356b 4279 */
2d21ac55 4280 if (tvp && (tvp != fvp))
91447636 4281 inuse = vnode_isinuse(tvp, 0);
2d21ac55
A
4282 if (inuse && !tnp->n_sillyrename && (vnode_vtype(tvp) != VDIR)) {
4283 error = nfs_sillyrename(tdnp, tnp, tcnp, ctx);
4284 if (error) {
9bccf70c
A
4285 /* sillyrename failed. Instead of pressing on, return error */
4286 goto out; /* should not be ENOENT. */
4287 } else {
4288 /* sillyrename succeeded.*/
9bccf70c
A
4289 tvp = NULL;
4290 }
b0d623f7 4291 } else if (tvp && (nmp->nm_vers >= NFS_VER4) && (tnp->n_openflags & N_DELEG_MASK)) {
6d2010ae 4292 nfs4_delegation_return(tnp, 0, vfs_context_thread(ctx), vfs_context_ucred(ctx));
1c79356b
A
4293 }
4294
2d21ac55
A
4295 error = nmp->nm_funcs->nf_rename_rpc(fdnp, fcnp->cn_nameptr, fcnp->cn_namelen,
4296 tdnp, tcnp->cn_nameptr, tcnp->cn_namelen, ctx);
1c79356b 4297
91447636
A
4298 /*
4299 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
4300 */
4301 if (error == ENOENT)
4302 error = 0;
4303
2d21ac55 4304 if (tvp && (tvp != fvp) && !tnp->n_sillyrename) {
b0d623f7 4305 nfs_node_lock_force(tnp);
2d21ac55 4306 tvprecycle = (!error && !vnode_isinuse(tvp, 0) &&
6d2010ae 4307 (nfs_getattrcache(tnp, &nvattr, 0) || (nvattr.nva_nlink == 1)));
b0d623f7 4308 nfs_node_unlock(tnp);
91447636 4309 lck_mtx_lock(nfs_node_hash_mutex);
2d21ac55
A
4310 if (tvprecycle && (tnp->n_hflag & NHHASHED)) {
4311 /*
4312 * remove nfsnode from hash now so we can't accidentally find it
4313 * again if another object gets created with the same filehandle
4314 * before this vnode gets reclaimed
4315 */
4316 LIST_REMOVE(tnp, n_hash);
4317 tnp->n_hflag &= ~NHHASHED;
4318 FSDBG(266, 0, tnp, tnp->n_flag, 0xb1eb1e);
4319 }
91447636 4320 lck_mtx_unlock(nfs_node_hash_mutex);
55e303ae 4321 }
2d21ac55 4322
91447636 4323 /* purge the old name cache entries and enter the new one */
b0d623f7 4324 nfs_name_cache_purge(fdnp, fnp, fcnp, ctx);
9bccf70c 4325 if (tvp) {
b0d623f7 4326 nfs_name_cache_purge(tdnp, tnp, tcnp, ctx);
2d21ac55
A
4327 if (tvprecycle) {
4328 /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */
91447636 4329 /* clear all flags other than these */
b0d623f7 4330 nfs_node_lock_force(tnp);
2d21ac55 4331 tnp->n_flag &= (NMODIFIED);
b0d623f7 4332 nfs_node_unlock(tnp);
91447636
A
4333 vnode_recycle(tvp);
4334 }
9bccf70c 4335 }
2d21ac55 4336 if (!error) {
b0d623f7 4337 nfs_node_lock_force(tdnp);
2d21ac55
A
4338 if (tdnp->n_flag & NNEGNCENTRIES) {
4339 tdnp->n_flag &= ~NNEGNCENTRIES;
4340 cache_purge_negatives(tdvp);
4341 }
b0d623f7
A
4342 nfs_node_unlock(tdnp);
4343 nfs_node_lock_force(fnp);
91447636 4344 cache_enter(tdvp, fvp, tcnp);
2d21ac55
A
4345 if (tdvp != fdvp) { /* update parent pointer */
4346 if (fnp->n_parent && !vnode_get(fnp->n_parent)) {
4347 /* remove ref from old parent */
4348 vnode_rele(fnp->n_parent);
4349 vnode_put(fnp->n_parent);
4350 }
4351 fnp->n_parent = tdvp;
4352 if (tdvp && !vnode_get(tdvp)) {
4353 /* add ref to new parent */
4354 vnode_ref(tdvp);
4355 vnode_put(tdvp);
4356 } else {
4357 fnp->n_parent = NULL;
4358 }
4359 }
b0d623f7 4360 nfs_node_unlock(fnp);
2d21ac55 4361 }
1c79356b 4362out:
b0d623f7 4363 /* nfs_getattr() will check changed and purge caches */
6d2010ae
A
4364 nfs_getattr(fdnp, NULL, ctx, NGA_CACHED);
4365 nfs_getattr(tdnp, NULL, ctx, NGA_CACHED);
2d21ac55
A
4366 if (locked) {
4367 /* unlock node */
4368 lck_mtx_lock(nfs_node_hash_mutex);
4369 tnp->n_hflag &= ~NHLOCKED;
4370 if (tnp->n_hflag & NHLOCKWANT) {
4371 tnp->n_hflag &= ~NHLOCKWANT;
4372 wakeup(tnp);
4373 }
4374 lck_mtx_unlock(nfs_node_hash_mutex);
4375 }
b0d623f7 4376 nfs_node_clear_busy4(fdnp, fnp, tdnp, tnp);
1c79356b
A
4377 return (error);
4378}
4379
4380/*
2d21ac55 4381 * Do an NFS rename rpc. Called from nfs_vnop_rename() and nfs_sillyrename().
1c79356b 4382 */
2d21ac55
A
4383int
4384nfs3_rename_rpc(
4385 nfsnode_t fdnp,
4386 char *fnameptr,
4387 int fnamelen,
4388 nfsnode_t tdnp,
4389 char *tnameptr,
4390 int tnamelen,
4391 vfs_context_t ctx)
1c79356b 4392{
b0d623f7 4393 int error = 0, lockerror = ENOENT, status, fwccpostattr = 0, twccpostattr = 0;
91447636 4394 struct timespec fpremtime = { 0, 0 }, tpremtime = { 0, 0 };
2d21ac55
A
4395 struct nfsmount *nmp;
4396 int nfsvers;
4397 u_int64_t xid, txid;
4398 struct nfsm_chain nmreq, nmrep;
1c79356b 4399
2d21ac55 4400 nmp = NFSTONMP(fdnp);
fe8ab488 4401 if (nfs_mount_gone(nmp))
55e303ae 4402 return (ENXIO);
2d21ac55
A
4403 nfsvers = nmp->nm_vers;
4404 if ((nfsvers == NFS_VER2) &&
4405 ((fnamelen > NFS_MAXNAMLEN) || (tnamelen > NFS_MAXNAMLEN)))
4406 return (ENAMETOOLONG);
4407
4408 nfsm_chain_null(&nmreq);
4409 nfsm_chain_null(&nmrep);
4410
4411 nfsm_chain_build_alloc_init(error, &nmreq,
4412 (NFSX_FH(nfsvers) + NFSX_UNSIGNED) * 2 +
4413 nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen));
4414 nfsm_chain_add_fh(error, &nmreq, nfsvers, fdnp->n_fhp, fdnp->n_fhsize);
6d2010ae 4415 nfsm_chain_add_name(error, &nmreq, fnameptr, fnamelen, nmp);
2d21ac55 4416 nfsm_chain_add_fh(error, &nmreq, nfsvers, tdnp->n_fhp, tdnp->n_fhsize);
6d2010ae 4417 nfsm_chain_add_name(error, &nmreq, tnameptr, tnamelen, nmp);
2d21ac55
A
4418 nfsm_chain_build_done(error, &nmreq);
4419 nfsmout_if(error);
4420
6d2010ae 4421 error = nfs_request(fdnp, NULL, &nmreq, NFSPROC_RENAME, ctx, NULL, &nmrep, &xid, &status);
2d21ac55 4422
b0d623f7
A
4423 if ((lockerror = nfs_node_lock2(fdnp, tdnp)))
4424 error = lockerror;
2d21ac55
A
4425 if (nfsvers == NFS_VER3) {
4426 txid = xid;
4427 nfsm_chain_get_wcc_data(error, &nmrep, fdnp, &fpremtime, &fwccpostattr, &xid);
4428 nfsm_chain_get_wcc_data(error, &nmrep, tdnp, &tpremtime, &twccpostattr, &txid);
4429 }
4430 if (!error)
4431 error = status;
4432nfsmout:
4433 nfsm_chain_cleanup(&nmreq);
4434 nfsm_chain_cleanup(&nmrep);
b0d623f7
A
4435 if (!lockerror) {
4436 fdnp->n_flag |= NMODIFIED;
4437 /* if directory hadn't changed, update namecache mtime */
4438 if (nfstimespeccmp(&fdnp->n_ncmtime, &fpremtime, ==))
4439 NFS_CHANGED_UPDATE_NC(nfsvers, fdnp, &fdnp->n_vattr);
4440 if (!fwccpostattr)
4441 NATTRINVALIDATE(fdnp);
4442 tdnp->n_flag |= NMODIFIED;
4443 /* if directory hadn't changed, update namecache mtime */
4444 if (nfstimespeccmp(&tdnp->n_ncmtime, &tpremtime, ==))
4445 NFS_CHANGED_UPDATE_NC(nfsvers, tdnp, &tdnp->n_vattr);
4446 if (!twccpostattr)
4447 NATTRINVALIDATE(tdnp);
4448 nfs_node_unlock2(fdnp, tdnp);
4449 }
1c79356b
A
4450 return (error);
4451}
4452
4453/*
2d21ac55 4454 * NFS hard link create call
1c79356b 4455 */
b0d623f7 4456int
2d21ac55 4457nfs3_vnop_link(
91447636
A
4458 struct vnop_link_args /* {
4459 struct vnodeop_desc *a_desc;
4460 vnode_t a_vp;
4461 vnode_t a_tdvp;
1c79356b 4462 struct componentname *a_cnp;
91447636 4463 vfs_context_t a_context;
2d21ac55 4464 } */ *ap)
1c79356b 4465{
2d21ac55 4466 vfs_context_t ctx = ap->a_context;
91447636
A
4467 vnode_t vp = ap->a_vp;
4468 vnode_t tdvp = ap->a_tdvp;
4469 struct componentname *cnp = ap->a_cnp;
b0d623f7 4470 int error = 0, lockerror = ENOENT, status, wccpostattr = 0, attrflag = 0;
91447636 4471 struct timespec premtime = { 0, 0 };
2d21ac55
A
4472 struct nfsmount *nmp;
4473 nfsnode_t np = VTONFS(vp);
4474 nfsnode_t tdnp = VTONFS(tdvp);
4475 int nfsvers;
4476 u_int64_t xid, txid;
4477 struct nfsm_chain nmreq, nmrep;
1c79356b 4478
2d21ac55 4479 if (vnode_mount(vp) != vnode_mount(tdvp))
1c79356b 4480 return (EXDEV);
1c79356b 4481
2d21ac55 4482 nmp = VTONMP(vp);
fe8ab488 4483 if (nfs_mount_gone(nmp))
2d21ac55
A
4484 return (ENXIO);
4485 nfsvers = nmp->nm_vers;
4486 if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN))
4487 return (ENAMETOOLONG);
55e303ae 4488
1c79356b
A
4489 /*
4490 * Push all writes to the server, so that the attribute cache
4491 * doesn't get "out of sync" with the server.
4492 * XXX There should be a better way!
4493 */
2d21ac55 4494 nfs_flush(np, MNT_WAIT, vfs_context_thread(ctx), V_IGNORE_WRITEERR);
1c79356b 4495
b0d623f7 4496 error = nfs_node_set_busy2(tdnp, np, vfs_context_thread(ctx));
91447636
A
4497 if (error)
4498 return (error);
fa4905b1 4499
2d21ac55
A
4500 nfsm_chain_null(&nmreq);
4501 nfsm_chain_null(&nmrep);
4502
4503 nfsm_chain_build_alloc_init(error, &nmreq,
4504 NFSX_FH(nfsvers)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
4505 nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
4506 nfsm_chain_add_fh(error, &nmreq, nfsvers, tdnp->n_fhp, tdnp->n_fhsize);
6d2010ae 4507 nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp);
2d21ac55
A
4508 nfsm_chain_build_done(error, &nmreq);
4509 nfsmout_if(error);
6d2010ae 4510 error = nfs_request(np, NULL, &nmreq, NFSPROC_LINK, ctx, NULL, &nmrep, &xid, &status);
b0d623f7
A
4511
4512 if ((lockerror = nfs_node_lock2(tdnp, np))) {
4513 error = lockerror;
4514 goto nfsmout;
4515 }
2d21ac55
A
4516 if (nfsvers == NFS_VER3) {
4517 txid = xid;
4518 nfsm_chain_postop_attr_update_flag(error, &nmrep, np, attrflag, &xid);
4519 nfsm_chain_get_wcc_data(error, &nmrep, tdnp, &premtime, &wccpostattr, &txid);
1c79356b 4520 }
2d21ac55
A
4521 if (!error)
4522 error = status;
4523nfsmout:
4524 nfsm_chain_cleanup(&nmreq);
4525 nfsm_chain_cleanup(&nmrep);
b0d623f7
A
4526 if (!lockerror) {
4527 if (!attrflag)
4528 NATTRINVALIDATE(np);
4529 tdnp->n_flag |= NMODIFIED;
4530 /* if directory hadn't changed, update namecache mtime */
4531 if (nfstimespeccmp(&tdnp->n_ncmtime, &premtime, ==))
4532 NFS_CHANGED_UPDATE_NC(nfsvers, tdnp, &tdnp->n_vattr);
4533 if (!wccpostattr)
4534 NATTRINVALIDATE(tdnp);
4535 if (!error && (tdnp->n_flag & NNEGNCENTRIES)) {
4536 tdnp->n_flag &= ~NNEGNCENTRIES;
4537 cache_purge_negatives(tdvp);
4538 }
4539 nfs_node_unlock2(tdnp, np);
2d21ac55 4540 }
b0d623f7 4541 nfs_node_clear_busy2(tdnp, np);
1c79356b
A
4542 /*
4543 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
4544 */
4545 if (error == EEXIST)
4546 error = 0;
4547 return (error);
4548}
4549
4550/*
2d21ac55 4551 * NFS symbolic link create call
1c79356b 4552 */
b0d623f7 4553int
2d21ac55 4554nfs3_vnop_symlink(
91447636
A
4555 struct vnop_symlink_args /* {
4556 struct vnodeop_desc *a_desc;
4557 vnode_t a_dvp;
4558 vnode_t *a_vpp;
1c79356b 4559 struct componentname *a_cnp;
91447636 4560 struct vnode_attr *a_vap;
1c79356b 4561 char *a_target;
91447636 4562 vfs_context_t a_context;
2d21ac55 4563 } */ *ap)
1c79356b 4564{
2d21ac55 4565 vfs_context_t ctx = ap->a_context;
91447636
A
4566 vnode_t dvp = ap->a_dvp;
4567 struct vnode_attr *vap = ap->a_vap;
4568 struct componentname *cnp = ap->a_cnp;
6d2010ae 4569 struct nfs_vattr nvattr;
2d21ac55 4570 fhandle_t fh;
b0d623f7 4571 int slen, error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0;
91447636 4572 struct timespec premtime = { 0, 0 };
2d21ac55
A
4573 vnode_t newvp = NULL;
4574 int nfsvers, gotuid, gotgid;
39236c6e 4575 u_int64_t xid = 0, dxid;
2d21ac55
A
4576 nfsnode_t np = NULL;
4577 nfsnode_t dnp = VTONFS(dvp);
4578 struct nfsmount *nmp;
4579 struct nfsm_chain nmreq, nmrep;
6d2010ae 4580 struct nfsreq rq, *req = &rq;
2d21ac55 4581 struct nfs_dulookup dul;
91447636 4582
2d21ac55 4583 nmp = VTONMP(dvp);
fe8ab488 4584 if (nfs_mount_gone(nmp))
2d21ac55
A
4585 return (ENXIO);
4586 nfsvers = nmp->nm_vers;
1c79356b 4587
1c79356b 4588 slen = strlen(ap->a_target);
2d21ac55
A
4589 if ((nfsvers == NFS_VER2) &&
4590 ((cnp->cn_namelen > NFS_MAXNAMLEN) || (slen > NFS_MAXPATHLEN)))
4591 return (ENAMETOOLONG);
91447636 4592
6d2010ae
A
4593 nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx);
4594
91447636
A
4595 VATTR_SET_SUPPORTED(vap, va_mode);
4596 VATTR_SET_SUPPORTED(vap, va_uid);
4597 VATTR_SET_SUPPORTED(vap, va_gid);
4598 VATTR_SET_SUPPORTED(vap, va_data_size);
4599 VATTR_SET_SUPPORTED(vap, va_access_time);
4600 VATTR_SET_SUPPORTED(vap, va_modify_time);
4601 gotuid = VATTR_IS_ACTIVE(vap, va_uid);
4602 gotgid = VATTR_IS_ACTIVE(vap, va_gid);
4603
b0d623f7
A
4604 error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx));
4605 nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx);
2d21ac55
A
4606
4607 nfsm_chain_null(&nmreq);
4608 nfsm_chain_null(&nmrep);
4609
4610 nfsm_chain_build_alloc_init(error, &nmreq,
4611 NFSX_FH(nfsvers) + 2 * NFSX_UNSIGNED +
4612 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(nfsvers));
4613 nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
6d2010ae 4614 nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp);
2d21ac55
A
4615 if (nfsvers == NFS_VER3)
4616 nfsm_chain_add_v3sattr(error, &nmreq, vap);
6d2010ae 4617 nfsm_chain_add_name(error, &nmreq, ap->a_target, slen, nmp);
2d21ac55
A
4618 if (nfsvers == NFS_VER2)
4619 nfsm_chain_add_v2sattr(error, &nmreq, vap, -1);
4620 nfsm_chain_build_done(error, &nmreq);
4621 nfsmout_if(error);
2d21ac55
A
4622
4623 error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_SYMLINK,
6d2010ae 4624 vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req);
2d21ac55
A
4625 if (!error) {
4626 nfs_dulookup_start(&dul, dnp, ctx);
4627 error = nfs_request_async_finish(req, &nmrep, &xid, &status);
4628 }
4629
b0d623f7
A
4630 if ((lockerror = nfs_node_lock(dnp)))
4631 error = lockerror;
2d21ac55
A
4632 dxid = xid;
4633 if (!error && !status) {
4634 if (dnp->n_flag & NNEGNCENTRIES) {
4635 dnp->n_flag &= ~NNEGNCENTRIES;
4636 cache_purge_negatives(dvp);
91447636 4637 }
2d21ac55
A
4638 if (nfsvers == NFS_VER3)
4639 error = nfsm_chain_get_fh_attr(&nmrep, dnp, ctx, nfsvers, &xid, &fh, &nvattr);
4640 else
4641 fh.fh_len = 0;
1c79356b 4642 }
2d21ac55
A
4643 if (nfsvers == NFS_VER3)
4644 nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid);
4645 if (!error)
4646 error = status;
4647nfsmout:
4648 nfsm_chain_cleanup(&nmreq);
4649 nfsm_chain_cleanup(&nmrep);
fa4905b1 4650
2d21ac55
A
4651 if (!lockerror) {
4652 dnp->n_flag |= NMODIFIED;
4653 /* if directory hadn't changed, update namecache mtime */
4654 if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==))
4655 NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr);
b0d623f7
A
4656 nfs_node_unlock(dnp);
4657 /* nfs_getattr() will check changed and purge caches */
6d2010ae 4658 nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED);
1c79356b 4659 }
55e303ae 4660
2d21ac55 4661 if (!error && fh.fh_len)
6d2010ae 4662 error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np);
2d21ac55
A
4663 if (!error && np)
4664 newvp = NFSTOV(np);
4665
4666 nfs_dulookup_finish(&dul, dnp, ctx);
91447636 4667
1c79356b 4668 /*
91447636
A
4669 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
4670 * if we can succeed in looking up the symlink.
1c79356b 4671 */
2d21ac55 4672 if ((error == EEXIST) || (!error && !newvp)) {
91447636 4673 if (newvp) {
b0d623f7 4674 nfs_node_unlock(np);
91447636
A
4675 vnode_put(newvp);
4676 newvp = NULL;
4677 }
2d21ac55 4678 error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np);
91447636
A
4679 if (!error) {
4680 newvp = NFSTOV(np);
4681 if (vnode_vtype(newvp) != VLNK)
4682 error = EEXIST;
4683 }
4684 }
b0d623f7
A
4685 if (!busyerror)
4686 nfs_node_clear_busy(dnp);
91447636 4687 if (!error && (gotuid || gotgid) &&
6d2010ae 4688 (!newvp || nfs_getattrcache(np, &nvattr, 0) ||
91447636
A
4689 (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
4690 (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
4691 /* clear ID bits if server didn't use them (or we can't tell) */
4692 VATTR_CLEAR_SUPPORTED(vap, va_uid);
4693 VATTR_CLEAR_SUPPORTED(vap, va_gid);
4694 }
4695 if (error) {
2d21ac55 4696 if (newvp) {
b0d623f7 4697 nfs_node_unlock(np);
91447636 4698 vnode_put(newvp);
2d21ac55 4699 }
91447636 4700 } else {
b0d623f7 4701 nfs_node_unlock(np);
91447636
A
4702 *ap->a_vpp = newvp;
4703 }
1c79356b
A
4704 return (error);
4705}
4706
4707/*
2d21ac55 4708 * NFS make dir call
1c79356b 4709 */
b0d623f7 4710int
2d21ac55 4711nfs3_vnop_mkdir(
91447636
A
4712 struct vnop_mkdir_args /* {
4713 struct vnodeop_desc *a_desc;
4714 vnode_t a_dvp;
4715 vnode_t *a_vpp;
1c79356b 4716 struct componentname *a_cnp;
91447636
A
4717 struct vnode_attr *a_vap;
4718 vfs_context_t a_context;
2d21ac55 4719 } */ *ap)
1c79356b 4720{
2d21ac55 4721 vfs_context_t ctx = ap->a_context;
91447636
A
4722 vnode_t dvp = ap->a_dvp;
4723 struct vnode_attr *vap = ap->a_vap;
4724 struct componentname *cnp = ap->a_cnp;
6d2010ae 4725 struct nfs_vattr nvattr;
2d21ac55
A
4726 nfsnode_t np = NULL;
4727 struct nfsmount *nmp;
4728 nfsnode_t dnp = VTONFS(dvp);
4729 vnode_t newvp = NULL;
b0d623f7 4730 int error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0;
91447636 4731 struct timespec premtime = { 0, 0 };
2d21ac55 4732 int nfsvers, gotuid, gotgid;
39236c6e 4733 u_int64_t xid= 0, dxid;
2d21ac55
A
4734 fhandle_t fh;
4735 struct nfsm_chain nmreq, nmrep;
6d2010ae 4736 struct nfsreq rq, *req = &rq;
2d21ac55 4737 struct nfs_dulookup dul;
1c79356b 4738
2d21ac55 4739 nmp = VTONMP(dvp);
fe8ab488 4740 if (nfs_mount_gone(nmp))
2d21ac55
A
4741 return (ENXIO);
4742 nfsvers = nmp->nm_vers;
4743 if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN))
4744 return (ENAMETOOLONG);
91447636 4745
6d2010ae
A
4746 nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx);
4747
91447636
A
4748 VATTR_SET_SUPPORTED(vap, va_mode);
4749 VATTR_SET_SUPPORTED(vap, va_uid);
4750 VATTR_SET_SUPPORTED(vap, va_gid);
4751 VATTR_SET_SUPPORTED(vap, va_data_size);
4752 VATTR_SET_SUPPORTED(vap, va_access_time);
4753 VATTR_SET_SUPPORTED(vap, va_modify_time);
4754 gotuid = VATTR_IS_ACTIVE(vap, va_uid);
4755 gotgid = VATTR_IS_ACTIVE(vap, va_gid);
4756
b0d623f7
A
4757 error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx));
4758 nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx);
2d21ac55
A
4759
4760 nfsm_chain_null(&nmreq);
4761 nfsm_chain_null(&nmrep);
4762
4763 nfsm_chain_build_alloc_init(error, &nmreq,
4764 NFSX_FH(nfsvers) + NFSX_UNSIGNED +
4765 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(nfsvers));
4766 nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
6d2010ae 4767 nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp);
2d21ac55
A
4768 if (nfsvers == NFS_VER3)
4769 nfsm_chain_add_v3sattr(error, &nmreq, vap);
4770 else
4771 nfsm_chain_add_v2sattr(error, &nmreq, vap, -1);
4772 nfsm_chain_build_done(error, &nmreq);
4773 nfsmout_if(error);
2d21ac55
A
4774
4775 error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_MKDIR,
6d2010ae 4776 vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req);
2d21ac55
A
4777 if (!error) {
4778 nfs_dulookup_start(&dul, dnp, ctx);
4779 error = nfs_request_async_finish(req, &nmrep, &xid, &status);
1c79356b 4780 }
2d21ac55 4781
b0d623f7
A
4782 if ((lockerror = nfs_node_lock(dnp)))
4783 error = lockerror;
fa4905b1 4784 dxid = xid;
2d21ac55
A
4785 if (!error && !status) {
4786 if (dnp->n_flag & NNEGNCENTRIES) {
4787 dnp->n_flag &= ~NNEGNCENTRIES;
4788 cache_purge_negatives(dvp);
4789 }
4790 error = nfsm_chain_get_fh_attr(&nmrep, dnp, ctx, nfsvers, &xid, &fh, &nvattr);
4791 }
4792 if (nfsvers == NFS_VER3)
4793 nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid);
1c79356b 4794 if (!error)
2d21ac55
A
4795 error = status;
4796nfsmout:
4797 nfsm_chain_cleanup(&nmreq);
4798 nfsm_chain_cleanup(&nmrep);
4799
4800 if (!lockerror) {
4801 dnp->n_flag |= NMODIFIED;
4802 /* if directory hadn't changed, update namecache mtime */
4803 if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==))
4804 NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr);
b0d623f7
A
4805 nfs_node_unlock(dnp);
4806 /* nfs_getattr() will check changed and purge caches */
6d2010ae 4807 nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED);
2d21ac55
A
4808 }
4809
4810 if (!error && fh.fh_len)
6d2010ae 4811 error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np);
2d21ac55
A
4812 if (!error && np)
4813 newvp = NFSTOV(np);
4814
4815 nfs_dulookup_finish(&dul, dnp, ctx);
4816
1c79356b
A
4817 /*
4818 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
4819 * if we can succeed in looking up the directory.
4820 */
b0d623f7 4821 if ((error == EEXIST) || (!error && !newvp)) {
1c79356b 4822 if (newvp) {
b0d623f7 4823 nfs_node_unlock(np);
91447636
A
4824 vnode_put(newvp);
4825 newvp = NULL;
1c79356b 4826 }
2d21ac55 4827 error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np);
1c79356b
A
4828 if (!error) {
4829 newvp = NFSTOV(np);
91447636 4830 if (vnode_vtype(newvp) != VDIR)
1c79356b
A
4831 error = EEXIST;
4832 }
4833 }
b0d623f7
A
4834 if (!busyerror)
4835 nfs_node_clear_busy(dnp);
91447636 4836 if (!error && (gotuid || gotgid) &&
6d2010ae 4837 (!newvp || nfs_getattrcache(np, &nvattr, 0) ||
91447636
A
4838 (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
4839 (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
4840 /* clear ID bits if server didn't use them (or we can't tell) */
4841 VATTR_CLEAR_SUPPORTED(vap, va_uid);
4842 VATTR_CLEAR_SUPPORTED(vap, va_gid);
4843 }
1c79356b 4844 if (error) {
2d21ac55 4845 if (newvp) {
b0d623f7 4846 nfs_node_unlock(np);
91447636 4847 vnode_put(newvp);
2d21ac55 4848 }
483a1d10 4849 } else {
b0d623f7 4850 nfs_node_unlock(np);
1c79356b 4851 *ap->a_vpp = newvp;
483a1d10 4852 }
1c79356b
A
4853 return (error);
4854}
4855
4856/*
2d21ac55 4857 * NFS remove directory call
1c79356b 4858 */
b0d623f7 4859int
2d21ac55 4860nfs3_vnop_rmdir(
91447636
A
4861 struct vnop_rmdir_args /* {
4862 struct vnodeop_desc *a_desc;
4863 vnode_t a_dvp;
4864 vnode_t a_vp;
1c79356b 4865 struct componentname *a_cnp;
91447636 4866 vfs_context_t a_context;
2d21ac55 4867 } */ *ap)
1c79356b 4868{
2d21ac55 4869 vfs_context_t ctx = ap->a_context;
91447636
A
4870 vnode_t vp = ap->a_vp;
4871 vnode_t dvp = ap->a_dvp;
4872 struct componentname *cnp = ap->a_cnp;
b0d623f7 4873 int error = 0, lockerror = ENOENT, status, wccpostattr = 0;
91447636 4874 struct timespec premtime = { 0, 0 };
2d21ac55
A
4875 struct nfsmount *nmp;
4876 nfsnode_t np = VTONFS(vp);
4877 nfsnode_t dnp = VTONFS(dvp);
2d21ac55 4878 int nfsvers;
fa4905b1 4879 u_int64_t xid;
2d21ac55 4880 struct nfsm_chain nmreq, nmrep;
6d2010ae 4881 struct nfsreq rq, *req = &rq;
2d21ac55 4882 struct nfs_dulookup dul;
1c79356b 4883
2d21ac55 4884 nmp = VTONMP(vp);
fe8ab488 4885 if (nfs_mount_gone(nmp))
2d21ac55
A
4886 return (ENXIO);
4887 nfsvers = nmp->nm_vers;
4888 if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN))
4889 return (ENAMETOOLONG);
91447636 4890
b0d623f7 4891 if ((error = nfs_node_set_busy2(dnp, np, vfs_context_thread(ctx))))
91447636 4892 return (error);
2d21ac55 4893
b0d623f7
A
4894 nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx);
4895
2d21ac55
A
4896 nfsm_chain_null(&nmreq);
4897 nfsm_chain_null(&nmrep);
4898
4899 nfsm_chain_build_alloc_init(error, &nmreq,
4900 NFSX_FH(nfsvers) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
4901 nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
6d2010ae 4902 nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp);
2d21ac55
A
4903 nfsm_chain_build_done(error, &nmreq);
4904 nfsmout_if(error);
4905
4906 error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_RMDIR,
6d2010ae 4907 vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req);
2d21ac55
A
4908 if (!error) {
4909 nfs_dulookup_start(&dul, dnp, ctx);
4910 error = nfs_request_async_finish(req, &nmrep, &xid, &status);
4911 }
4912
b0d623f7
A
4913 if ((lockerror = nfs_node_lock(dnp)))
4914 error = lockerror;
2d21ac55
A
4915 if (nfsvers == NFS_VER3)
4916 nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &xid);
4917 if (!error)
4918 error = status;
4919nfsmout:
4920 nfsm_chain_cleanup(&nmreq);
4921 nfsm_chain_cleanup(&nmrep);
4922
b0d623f7
A
4923 if (!lockerror) {
4924 dnp->n_flag |= NMODIFIED;
4925 /* if directory hadn't changed, update namecache mtime */
4926 if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==))
4927 NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr);
4928 nfs_node_unlock(dnp);
4929 nfs_name_cache_purge(dnp, np, cnp, ctx);
4930 /* nfs_getattr() will check changed and purge caches */
6d2010ae 4931 nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED);
2d21ac55
A
4932 }
4933 nfs_dulookup_finish(&dul, dnp, ctx);
b0d623f7 4934 nfs_node_clear_busy2(dnp, np);
2d21ac55 4935
1c79356b
A
4936 /*
4937 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
4938 */
4939 if (error == ENOENT)
4940 error = 0;
483a1d10
A
4941 if (!error) {
4942 /*
4943 * remove nfsnode from hash now so we can't accidentally find it
4944 * again if another object gets created with the same filehandle
4945 * before this vnode gets reclaimed
4946 */
91447636 4947 lck_mtx_lock(nfs_node_hash_mutex);
2d21ac55
A
4948 if (np->n_hflag & NHHASHED) {
4949 LIST_REMOVE(np, n_hash);
4950 np->n_hflag &= ~NHHASHED;
4951 FSDBG(266, 0, np, np->n_flag, 0xb1eb1e);
4952 }
91447636 4953 lck_mtx_unlock(nfs_node_hash_mutex);
483a1d10 4954 }
1c79356b
A
4955 return (error);
4956}
4957
4958/*
2d21ac55 4959 * NFS readdir call
b0d623f7
A
4960 *
4961 * The incoming "offset" is a directory cookie indicating where in the
4962 * directory entries should be read from. A zero cookie means start at
4963 * the beginning of the directory. Any other cookie will be a cookie
4964 * returned from the server.
4965 *
4966 * Using that cookie, determine which buffer (and where in that buffer)
4967 * to start returning entries from. Buffer logical block numbers are
4968 * the cookies they start at. If a buffer is found that is not full,
4969 * call into the bio/RPC code to fill it. The RPC code will probably
4970 * fill several buffers (dropping the first, requiring a re-get).
4971 *
4972 * When done copying entries to the buffer, set the offset to the current
4973 * entry's cookie and enter that cookie in the cookie cache.
4974 *
4975 * Note: because the getdirentries(2) API returns a long-typed offset,
4976 * the incoming offset is a potentially truncated cookie (ptc).
4977 * The cookie matching code is aware of this and will fall back to
4978 * matching only 32 bits of the cookie.
1c79356b 4979 */
b0d623f7 4980int
2d21ac55 4981nfs_vnop_readdir(
91447636
A
4982 struct vnop_readdir_args /* {
4983 struct vnodeop_desc *a_desc;
4984 vnode_t a_vp;
1c79356b 4985 struct uio *a_uio;
b0d623f7 4986 int a_flags;
91447636 4987 int *a_eofflag;
b0d623f7 4988 int *a_numdirent;
91447636 4989 vfs_context_t a_context;
2d21ac55 4990 } */ *ap)
1c79356b 4991{
2d21ac55 4992 vfs_context_t ctx = ap->a_context;
b0d623f7
A
4993 vnode_t dvp = ap->a_vp;
4994 nfsnode_t dnp = VTONFS(dvp);
2d21ac55 4995 struct nfsmount *nmp;
b0d623f7
A
4996 uio_t uio = ap->a_uio;
4997 int error, nfsvers, extended, numdirent, bigcookies, ptc, done;
b0d623f7
A
4998 uint16_t i, iptc, rlen, nlen;
4999 uint64_t cookie, nextcookie, lbn = 0;
5000 struct nfsbuf *bp = NULL;
5001 struct nfs_dir_buf_header *ndbhp;
5002 struct direntry *dp, *dpptc;
5003 struct dirent dent;
5004 char *cp = NULL;
5005 thread_t thd;
1c79356b 5006
b0d623f7 5007 nmp = VTONMP(dvp);
fe8ab488 5008 if (nfs_mount_gone(nmp))
2d21ac55
A
5009 return (ENXIO);
5010 nfsvers = nmp->nm_vers;
b0d623f7
A
5011 bigcookies = (nmp->nm_state & NFSSTA_BIGCOOKIES);
5012 extended = (ap->a_flags & VNODE_READDIR_EXTENDED);
2d21ac55 5013
b0d623f7
A
5014 if (vnode_vtype(dvp) != VDIR)
5015 return (EPERM);
5016
5017 if (ap->a_eofflag)
5018 *ap->a_eofflag = 0;
5019
5020 if (uio_resid(uio) == 0)
5021 return (0);
5022
6d2010ae
A
5023 if ((nfsvers >= NFS_VER4) && (dnp->n_vattr.nva_flags & NFS_FFLAG_TRIGGER)) {
5024 /* trigger directories should never be read, return nothing */
5025 return (0);
5026 }
5027
b0d623f7
A
5028 thd = vfs_context_thread(ctx);
5029 numdirent = done = 0;
5030 nextcookie = uio_offset(uio);
5031 ptc = bigcookies && NFS_DIR_COOKIE_POTENTIALLY_TRUNCATED(nextcookie);
5032
5033 if ((error = nfs_node_lock(dnp)))
5034 goto out;
5035
5036 if (dnp->n_flag & NNEEDINVALIDATE) {
5037 dnp->n_flag &= ~NNEEDINVALIDATE;
5038 nfs_invaldir(dnp);
5039 nfs_node_unlock(dnp);
5040 error = nfs_vinvalbuf(dvp, 0, ctx, 1);
5041 if (!error)
5042 error = nfs_node_lock(dnp);
5043 if (error)
5044 goto out;
5045 }
91447636 5046
1c79356b 5047 /*
b0d623f7 5048 * check for need to invalidate when (re)starting at beginning
1c79356b 5049 */
b0d623f7
A
5050 if (!nextcookie) {
5051 if (dnp->n_flag & NMODIFIED) {
5052 nfs_invaldir(dnp);
5053 nfs_node_unlock(dnp);
5054 if ((error = nfs_vinvalbuf(dvp, 0, ctx, 1)))
5055 goto out;
5056 } else {
5057 nfs_node_unlock(dnp);
5058 }
5059 /* nfs_getattr() will check changed and purge caches */
6d2010ae 5060 if ((error = nfs_getattr(dnp, NULL, ctx, NGA_UNCACHED)))
b0d623f7
A
5061 goto out;
5062 } else {
5063 nfs_node_unlock(dnp);
5064 }
5065
5066 error = nfs_dir_cookie_to_lbn(dnp, nextcookie, &ptc, &lbn);
5067 if (error) {
5068 if (error < 0) { /* just hit EOF cookie */
5069 done = 1;
5070 error = 0;
5071 }
5072 if (ap->a_eofflag)
5073 *ap->a_eofflag = 1;
5074 }
5075
5076 while (!error && !done) {
316670eb 5077 OSAddAtomic64(1, &nfsstats.biocache_readdirs);
b0d623f7
A
5078 cookie = nextcookie;
5079getbuffer:
5080 error = nfs_buf_get(dnp, lbn, NFS_DIRBLKSIZ, thd, NBLK_READ, &bp);
5081 if (error)
5082 goto out;
5083 ndbhp = (struct nfs_dir_buf_header*)bp->nb_data;
5084 if (!ISSET(bp->nb_flags, NB_CACHE) || !ISSET(ndbhp->ndbh_flags, NDB_FULL)) {
5085 if (!ISSET(bp->nb_flags, NB_CACHE)) { /* initialize the buffer */
5086 ndbhp->ndbh_flags = 0;
5087 ndbhp->ndbh_count = 0;
5088 ndbhp->ndbh_entry_end = sizeof(*ndbhp);
5089 ndbhp->ndbh_ncgen = dnp->n_ncgen;
5090 }
5091 error = nfs_buf_readdir(bp, ctx);
5092 if (error == NFSERR_DIRBUFDROPPED)
5093 goto getbuffer;
5094 if (error)
5095 nfs_buf_release(bp, 1);
5096 if (error && (error != ENXIO) && (error != ETIMEDOUT) && (error != EINTR) && (error != ERESTART)) {
5097 if (!nfs_node_lock(dnp)) {
5098 nfs_invaldir(dnp);
5099 nfs_node_unlock(dnp);
5100 }
5101 nfs_vinvalbuf(dvp, 0, ctx, 1);
5102 if (error == NFSERR_BAD_COOKIE)
5103 error = ENOENT;
5104 }
5105 if (error)
5106 goto out;
5107 }
5108
5109 /* find next entry to return */
5110 dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp);
5111 i = 0;
5112 if ((lbn != cookie) && !(ptc && NFS_DIR_COOKIE_SAME32(lbn, cookie))) {
5113 dpptc = NULL;
5114 iptc = 0;
5115 for (; (i < ndbhp->ndbh_count) && (cookie != dp->d_seekoff); i++) {
5116 if (ptc && !dpptc && NFS_DIR_COOKIE_SAME32(cookie, dp->d_seekoff)) {
5117 iptc = i;
5118 dpptc = dp;
5119 }
5120 nextcookie = dp->d_seekoff;
5121 dp = NFS_DIRENTRY_NEXT(dp);
5122 }
5123 if ((i == ndbhp->ndbh_count) && dpptc) {
5124 i = iptc;
5125 dp = dpptc;
5126 }
5127 if (i < ndbhp->ndbh_count) {
5128 nextcookie = dp->d_seekoff;
5129 dp = NFS_DIRENTRY_NEXT(dp);
5130 i++;
5131 }
5132 }
5133 ptc = 0; /* only have to deal with ptc on first cookie */
5134
5135 /* return as many entries as we can */
5136 for (; i < ndbhp->ndbh_count; i++) {
5137 if (extended) {
5138 rlen = dp->d_reclen;
5139 cp = (char*)dp;
5140 } else {
5141 if (!cp) {
5142 cp = (char*)&dent;
5143 bzero(cp, sizeof(dent));
5144 }
5145 if (dp->d_namlen > (sizeof(dent.d_name) - 1))
5146 nlen = sizeof(dent.d_name) - 1;
5147 else
5148 nlen = dp->d_namlen;
5149 rlen = NFS_DIRENT_LEN(nlen);
5150 dent.d_reclen = rlen;
5151 dent.d_ino = dp->d_ino;
5152 dent.d_type = dp->d_type;
5153 dent.d_namlen = nlen;
5154 strlcpy(dent.d_name, dp->d_name, nlen + 1);
5155 }
5156 /* check that the record fits */
5157 if (rlen > uio_resid(uio)) {
5158 done = 1;
5159 break;
5160 }
5161 if ((error = uiomove(cp, rlen, uio)))
5162 break;
5163 numdirent++;
5164 nextcookie = dp->d_seekoff;
5165 dp = NFS_DIRENTRY_NEXT(dp);
5166 }
5167
5168 if (i == ndbhp->ndbh_count) {
5169 /* hit end of buffer, move to next buffer */
5170 lbn = nextcookie;
5171 /* if we also hit EOF, we're done */
5172 if (ISSET(ndbhp->ndbh_flags, NDB_EOF)) {
5173 done = 1;
2d21ac55
A
5174 if (ap->a_eofflag)
5175 *ap->a_eofflag = 1;
483a1d10 5176 }
1c79356b 5177 }
b0d623f7
A
5178 if (!error)
5179 uio_setoffset(uio, nextcookie);
5180 if (!error && !done && (nextcookie == cookie)) {
5181 printf("nfs readdir cookie didn't change 0x%llx, %d/%d\n", cookie, i, ndbhp->ndbh_count);
5182 error = EIO;
5183 }
5184 nfs_buf_release(bp, 1);
1c79356b
A
5185 }
5186
b0d623f7
A
5187 if (!error)
5188 nfs_dir_cookie_cache(dnp, nextcookie, lbn);
1c79356b 5189
b0d623f7
A
5190 if (ap->a_numdirent)
5191 *ap->a_numdirent = numdirent;
5192out:
1c79356b
A
5193 return (error);
5194}
5195
b0d623f7 5196
1c79356b 5197/*
b0d623f7
A
5198 * Invalidate cached directory information, except for the actual directory
5199 * blocks (which are invalidated separately).
1c79356b 5200 */
b0d623f7
A
5201void
5202nfs_invaldir(nfsnode_t dnp)
1c79356b 5203{
b0d623f7
A
5204 if (vnode_vtype(NFSTOV(dnp)) != VDIR)
5205 return;
5206 dnp->n_eofcookie = 0;
5207 dnp->n_cookieverf = 0;
5208 if (!dnp->n_cookiecache)
5209 return;
5210 dnp->n_cookiecache->free = 0;
5211 dnp->n_cookiecache->mru = -1;
5212 memset(dnp->n_cookiecache->next, -1, NFSNUMCOOKIES);
5213}
1c79356b 5214
b0d623f7
A
5215/*
5216 * calculate how much space is available for additional directory entries.
5217 */
5218uint32_t
5219nfs_dir_buf_freespace(struct nfsbuf *bp, int rdirplus)
5220{
5221 struct nfs_dir_buf_header *ndbhp = (struct nfs_dir_buf_header*)bp->nb_data;
5222 uint32_t space;
1c79356b 5223
b0d623f7
A
5224 if (!ndbhp)
5225 return (0);
5226 space = bp->nb_bufsize - ndbhp->ndbh_entry_end;
5227 if (rdirplus)
5228 space -= ndbhp->ndbh_count * sizeof(struct nfs_vattr);
5229 return (space);
5230}
2d21ac55 5231
b0d623f7
A
5232/*
5233 * add/update a cookie->lbn entry in the directory cookie cache
5234 */
5235void
5236nfs_dir_cookie_cache(nfsnode_t dnp, uint64_t cookie, uint64_t lbn)
5237{
5238 struct nfsdmap *ndcc;
5239 int8_t i, prev;
5240
5241 if (!cookie)
5242 return;
5243
5244 if (nfs_node_lock(dnp))
5245 return;
5246
5247 if (cookie == dnp->n_eofcookie) { /* EOF cookie */
5248 nfs_node_unlock(dnp);
5249 return;
5250 }
5251
5252 ndcc = dnp->n_cookiecache;
5253 if (!ndcc) {
5254 /* allocate the cookie cache structure */
5255 MALLOC_ZONE(dnp->n_cookiecache, struct nfsdmap *,
5256 sizeof(struct nfsdmap), M_NFSDIROFF, M_WAITOK);
5257 if (!dnp->n_cookiecache) {
5258 nfs_node_unlock(dnp);
5259 return;
5260 }
5261 ndcc = dnp->n_cookiecache;
5262 ndcc->free = 0;
5263 ndcc->mru = -1;
5264 memset(ndcc->next, -1, NFSNUMCOOKIES);
2d21ac55
A
5265 }
5266
1c79356b 5267 /*
b0d623f7
A
5268 * Search the list for this cookie.
5269 * Keep track of previous and last entries.
1c79356b 5270 */
b0d623f7
A
5271 prev = -1;
5272 i = ndcc->mru;
5273 while ((i != -1) && (cookie != ndcc->cookies[i].key)) {
5274 if (ndcc->next[i] == -1) /* stop on last entry so we can reuse */
5275 break;
5276 prev = i;
5277 i = ndcc->next[i];
5278 }
5279 if ((i != -1) && (cookie == ndcc->cookies[i].key)) {
5280 /* found it, remove from list */
5281 if (prev != -1)
5282 ndcc->next[prev] = ndcc->next[i];
5283 else
5284 ndcc->mru = ndcc->next[i];
5285 } else {
5286 /* not found, use next free entry or reuse last entry */
5287 if (ndcc->free != NFSNUMCOOKIES)
5288 i = ndcc->free++;
5289 else
5290 ndcc->next[prev] = -1;
5291 ndcc->cookies[i].key = cookie;
5292 ndcc->cookies[i].lbn = lbn;
5293 }
5294 /* insert cookie at head of MRU list */
5295 ndcc->next[i] = ndcc->mru;
5296 ndcc->mru = i;
5297 nfs_node_unlock(dnp);
5298}
2d21ac55 5299
b0d623f7
A
5300/*
5301 * Try to map the given directory cookie to a directory buffer (return lbn).
5302 * If we have a possibly truncated cookie (ptc), check for 32-bit matches too.
5303 */
5304int
5305nfs_dir_cookie_to_lbn(nfsnode_t dnp, uint64_t cookie, int *ptc, uint64_t *lbnp)
5306{
5307 struct nfsdmap *ndcc = dnp->n_cookiecache;
6d2010ae
A
5308 int8_t eofptc, found;
5309 int i, iptc;
b0d623f7
A
5310 struct nfsmount *nmp;
5311 struct nfsbuf *bp, *lastbp;
5312 struct nfsbuflists blist;
5313 struct direntry *dp, *dpptc;
5314 struct nfs_dir_buf_header *ndbhp;
5315
5316 if (!cookie) { /* initial cookie */
5317 *lbnp = 0;
5318 *ptc = 0;
5319 return (0);
5320 }
2d21ac55 5321
b0d623f7
A
5322 if (nfs_node_lock(dnp))
5323 return (ENOENT);
2d21ac55 5324
b0d623f7
A
5325 if (cookie == dnp->n_eofcookie) { /* EOF cookie */
5326 nfs_node_unlock(dnp);
316670eb 5327 OSAddAtomic64(1, &nfsstats.direofcache_hits);
b0d623f7
A
5328 *ptc = 0;
5329 return (-1);
5330 }
5331 /* note if cookie is a 32-bit match with the EOF cookie */
5332 eofptc = *ptc ? NFS_DIR_COOKIE_SAME32(cookie, dnp->n_eofcookie) : 0;
5333 iptc = -1;
2d21ac55 5334
b0d623f7
A
5335 /* search the list for the cookie */
5336 for (i = ndcc ? ndcc->mru : -1; i >= 0; i = ndcc->next[i]) {
5337 if (ndcc->cookies[i].key == cookie) {
5338 /* found a match for this cookie */
5339 *lbnp = ndcc->cookies[i].lbn;
5340 nfs_node_unlock(dnp);
316670eb 5341 OSAddAtomic64(1, &nfsstats.direofcache_hits);
b0d623f7
A
5342 *ptc = 0;
5343 return (0);
1c79356b 5344 }
b0d623f7
A
5345 /* check for 32-bit match */
5346 if (*ptc && (iptc == -1) && NFS_DIR_COOKIE_SAME32(ndcc->cookies[i].key, cookie))
5347 iptc = i;
5348 }
5349 /* exact match not found */
5350 if (eofptc) {
5351 /* but 32-bit match hit the EOF cookie */
5352 nfs_node_unlock(dnp);
316670eb 5353 OSAddAtomic64(1, &nfsstats.direofcache_hits);
b0d623f7
A
5354 return (-1);
5355 }
5356 if (iptc >= 0) {
5357 /* but 32-bit match got a hit */
5358 *lbnp = ndcc->cookies[iptc].lbn;
5359 nfs_node_unlock(dnp);
316670eb 5360 OSAddAtomic64(1, &nfsstats.direofcache_hits);
b0d623f7
A
5361 return (0);
5362 }
5363 nfs_node_unlock(dnp);
2d21ac55 5364
b0d623f7
A
5365 /*
5366 * No match found in the cookie cache... hmm...
5367 * Let's search the directory's buffers for the cookie.
5368 */
5369 nmp = NFSTONMP(dnp);
fe8ab488 5370 if (nfs_mount_gone(nmp))
b0d623f7
A
5371 return (ENXIO);
5372 dpptc = NULL;
5373 found = 0;
5374
5375 lck_mtx_lock(nfs_buf_mutex);
5376 /*
5377 * Scan the list of buffers, keeping them in order.
5378 * Note that itercomplete inserts each of the remaining buffers
5379 * into the head of list (thus reversing the elements). So, we
5380 * make sure to iterate through all buffers, inserting them after
5381 * each other, to keep them in order.
5382 * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because
5383 * we don't drop nfs_buf_mutex.
5384 */
5385 if (!nfs_buf_iterprepare(dnp, &blist, NBI_CLEAN)) {
5386 lastbp = NULL;
5387 while ((bp = LIST_FIRST(&blist))) {
5388 LIST_REMOVE(bp, nb_vnbufs);
5389 if (!lastbp)
5390 LIST_INSERT_HEAD(&dnp->n_cleanblkhd, bp, nb_vnbufs);
2d21ac55 5391 else
b0d623f7
A
5392 LIST_INSERT_AFTER(lastbp, bp, nb_vnbufs);
5393 lastbp = bp;
5394 if (found)
5395 continue;
5396 nfs_buf_refget(bp);
5397 if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) {
5398 /* just skip this buffer */
5399 nfs_buf_refrele(bp);
5400 continue;
1c79356b 5401 }
b0d623f7
A
5402 nfs_buf_refrele(bp);
5403
5404 /* scan the buffer for the cookie */
5405 ndbhp = (struct nfs_dir_buf_header*)bp->nb_data;
5406 dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp);
5407 dpptc = NULL;
5408 for (i=0; (i < ndbhp->ndbh_count) && (cookie != dp->d_seekoff); i++) {
5409 if (*ptc && !dpptc && NFS_DIR_COOKIE_SAME32(cookie, dp->d_seekoff)) {
5410 dpptc = dp;
5411 iptc = i;
5412 }
5413 dp = NFS_DIRENTRY_NEXT(dp);
91447636 5414 }
b0d623f7
A
5415 if ((i == ndbhp->ndbh_count) && dpptc) {
5416 /* found only a PTC match */
5417 dp = dpptc;
5418 i = iptc;
5419 } else if (i < ndbhp->ndbh_count) {
5420 *ptc = 0;
1c79356b 5421 }
b0d623f7
A
5422 if (i < (ndbhp->ndbh_count-1)) {
5423 /* next entry is *in* this buffer: return this block */
5424 *lbnp = bp->nb_lblkno;
5425 found = 1;
5426 } else if (i == (ndbhp->ndbh_count-1)) {
5427 /* next entry refers to *next* buffer: return next block */
5428 *lbnp = dp->d_seekoff;
5429 found = 1;
1c79356b 5430 }
b0d623f7 5431 nfs_buf_drop(bp);
1c79356b 5432 }
b0d623f7 5433 nfs_buf_itercomplete(dnp, &blist, NBI_CLEAN);
2d21ac55 5434 }
b0d623f7
A
5435 lck_mtx_unlock(nfs_buf_mutex);
5436 if (found) {
316670eb 5437 OSAddAtomic64(1, &nfsstats.direofcache_hits);
b0d623f7 5438 return (0);
1c79356b 5439 }
b0d623f7
A
5440
5441 /* still not found... oh well, just start a new block */
5442 *lbnp = cookie;
316670eb 5443 OSAddAtomic64(1, &nfsstats.direofcache_misses);
b0d623f7
A
5444 return (0);
5445}
5446
5447/*
5448 * scan a directory buffer for the given name
5449 * Returns: ESRCH if not found, ENOENT if found invalid, 0 if found
5450 * Note: should only be called with RDIRPLUS directory buffers
5451 */
5452
5453#define NDBS_PURGE 1
5454#define NDBS_UPDATE 2
5455
5456int
5457nfs_dir_buf_search(
5458 struct nfsbuf *bp,
5459 struct componentname *cnp,
5460 fhandle_t *fhp,
5461 struct nfs_vattr *nvap,
5462 uint64_t *xidp,
5463 time_t *attrstampp,
5464 daddr64_t *nextlbnp,
5465 int flags)
5466{
5467 struct direntry *dp;
5468 struct nfs_dir_buf_header *ndbhp;
5469 struct nfs_vattr *nvattrp;
5470 daddr64_t nextlbn = 0;
5471 int i, error = ESRCH, fhlen;
5472
5473 /* scan the buffer for the name */
5474 ndbhp = (struct nfs_dir_buf_header*)bp->nb_data;
5475 dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp);
5476 for (i=0; i < ndbhp->ndbh_count; i++) {
5477 nextlbn = dp->d_seekoff;
5478 if ((cnp->cn_namelen == dp->d_namlen) && !strcmp(cnp->cn_nameptr, dp->d_name)) {
5479 fhlen = dp->d_name[dp->d_namlen+1];
5480 nvattrp = NFS_DIR_BUF_NVATTR(bp, i);
5481 if ((ndbhp->ndbh_ncgen != bp->nb_np->n_ncgen) || (fhp->fh_len == 0) ||
5482 (nvattrp->nva_type == VNON) || (nvattrp->nva_fileid == 0)) {
6d2010ae 5483 /* entry is not valid */
b0d623f7
A
5484 error = ENOENT;
5485 break;
5486 }
5487 if (flags == NDBS_PURGE) {
5488 dp->d_fileno = 0;
5489 bzero(nvattrp, sizeof(*nvattrp));
5490 error = ENOENT;
5491 break;
5492 }
5493 if (flags == NDBS_UPDATE) {
5494 /* update direntry's attrs if fh matches */
5495 if ((fhp->fh_len == fhlen) && !bcmp(&dp->d_name[dp->d_namlen+2], fhp->fh_data, fhlen)) {
5496 bcopy(nvap, nvattrp, sizeof(*nvap));
5497 dp->d_fileno = nvattrp->nva_fileid;
5498 nvattrp->nva_fileid = *xidp;
5499 *(time_t*)(&dp->d_name[dp->d_namlen+2+fhp->fh_len]) = *attrstampp;
5500 }
5501 error = 0;
5502 break;
5503 }
5504 /* copy out fh, attrs, attrstamp, and xid */
5505 fhp->fh_len = fhlen;
5506 bcopy(&dp->d_name[dp->d_namlen+2], fhp->fh_data, MAX(fhp->fh_len, (int)sizeof(fhp->fh_data)));
5507 *attrstampp = *(time_t*)(&dp->d_name[dp->d_namlen+2+fhp->fh_len]);
5508 bcopy(nvattrp, nvap, sizeof(*nvap));
5509 *xidp = nvap->nva_fileid;
5510 nvap->nva_fileid = dp->d_fileno;
5511 error = 0;
5512 break;
5513 }
5514 dp = NFS_DIRENTRY_NEXT(dp);
1c79356b 5515 }
b0d623f7
A
5516 if (nextlbnp)
5517 *nextlbnp = nextlbn;
5518 return (error);
5519}
1c79356b 5520
b0d623f7
A
5521/*
5522 * Look up a name in a directory's buffers.
5523 * Note: should only be called with RDIRPLUS directory buffers
5524 */
5525int
5526nfs_dir_buf_cache_lookup(nfsnode_t dnp, nfsnode_t *npp, struct componentname *cnp, vfs_context_t ctx, int purge)
5527{
5528 nfsnode_t newnp;
5529 struct nfsmount *nmp;
6d2010ae 5530 int error = 0, i, found = 0, count = 0;
b0d623f7
A
5531 u_int64_t xid;
5532 struct nfs_vattr nvattr;
5533 fhandle_t fh;
5534 time_t attrstamp = 0;
5535 thread_t thd = vfs_context_thread(ctx);
5536 struct nfsbuf *bp, *lastbp, *foundbp;
5537 struct nfsbuflists blist;
5538 daddr64_t lbn, nextlbn;
5539 int dotunder = (cnp->cn_namelen > 2) && (cnp->cn_nameptr[0] == '.') && (cnp->cn_nameptr[1] == '_');
5540
fe8ab488
A
5541 nmp = NFSTONMP(dnp);
5542 if (nfs_mount_gone(nmp))
b0d623f7 5543 return (ENXIO);
b0d623f7
A
5544 if (!purge)
5545 *npp = NULL;
5546
5547 /* first check most recent buffer (and next one too) */
5548 lbn = dnp->n_lastdbl;
5549 for (i=0; i < 2; i++) {
5550 if ((error = nfs_buf_get(dnp, lbn, NFS_DIRBLKSIZ, thd, NBLK_READ|NBLK_ONLYVALID, &bp)))
5551 return (error);
5552 if (!bp)
5553 break;
5554 count++;
5555 error = nfs_dir_buf_search(bp, cnp, &fh, &nvattr, &xid, &attrstamp, &nextlbn, purge ? NDBS_PURGE : 0);
5556 nfs_buf_release(bp, 0);
5557 if (error == ESRCH) {
5558 error = 0;
5559 } else {
5560 found = 1;
5561 break;
5562 }
5563 lbn = nextlbn;
5564 }
5565
5566 lck_mtx_lock(nfs_buf_mutex);
5567 if (found) {
5568 dnp->n_lastdbl = lbn;
5569 goto done;
5570 }
2d21ac55 5571
1c79356b 5572 /*
b0d623f7
A
5573 * Scan the list of buffers, keeping them in order.
5574 * Note that itercomplete inserts each of the remaining buffers
5575 * into the head of list (thus reversing the elements). So, we
5576 * make sure to iterate through all buffers, inserting them after
5577 * each other, to keep them in order.
5578 * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because
5579 * we don't drop nfs_buf_mutex.
1c79356b 5580 */
b0d623f7
A
5581 if (!nfs_buf_iterprepare(dnp, &blist, NBI_CLEAN)) {
5582 lastbp = foundbp = NULL;
5583 while ((bp = LIST_FIRST(&blist))) {
5584 LIST_REMOVE(bp, nb_vnbufs);
5585 if (!lastbp)
5586 LIST_INSERT_HEAD(&dnp->n_cleanblkhd, bp, nb_vnbufs);
5587 else
5588 LIST_INSERT_AFTER(lastbp, bp, nb_vnbufs);
5589 lastbp = bp;
5590 if (error || found)
5591 continue;
5592 if (!purge && dotunder && (count > 100)) /* don't waste too much time looking for ._ files */
5593 continue;
5594 nfs_buf_refget(bp);
5595 lbn = bp->nb_lblkno;
5596 if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) {
5597 /* just skip this buffer */
5598 nfs_buf_refrele(bp);
5599 continue;
5600 }
5601 nfs_buf_refrele(bp);
5602 count++;
5603 error = nfs_dir_buf_search(bp, cnp, &fh, &nvattr, &xid, &attrstamp, NULL, purge ? NDBS_PURGE : 0);
5604 if (error == ESRCH) {
5605 error = 0;
5606 } else {
5607 found = 1;
5608 foundbp = bp;
5609 }
5610 nfs_buf_drop(bp);
5611 }
5612 if (found) {
5613 LIST_REMOVE(foundbp, nb_vnbufs);
5614 LIST_INSERT_HEAD(&dnp->n_cleanblkhd, foundbp, nb_vnbufs);
5615 dnp->n_lastdbl = foundbp->nb_lblkno;
5616 }
5617 nfs_buf_itercomplete(dnp, &blist, NBI_CLEAN);
5618 }
5619done:
5620 lck_mtx_unlock(nfs_buf_mutex);
5621
5622 if (!error && found && !purge) {
5623 error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len,
6d2010ae 5624 &nvattr, &xid, dnp->n_auth, NG_MAKEENTRY, &newnp);
b0d623f7
A
5625 if (error)
5626 return (error);
5627 newnp->n_attrstamp = attrstamp;
5628 *npp = newnp;
5629 nfs_node_unlock(newnp);
5630 /* check if the dir buffer's attrs are out of date */
5631 if (!nfs_getattr(newnp, &nvattr, ctx, NGA_CACHED) &&
5632 (newnp->n_attrstamp != attrstamp)) {
5633 /* they are, so update them */
5634 error = nfs_buf_get(dnp, lbn, NFS_DIRBLKSIZ, thd, NBLK_READ|NBLK_ONLYVALID, &bp);
5635 if (!error && bp) {
5636 attrstamp = newnp->n_attrstamp;
5637 xid = newnp->n_xid;
5638 nfs_dir_buf_search(bp, cnp, &fh, &nvattr, &xid, &attrstamp, NULL, NDBS_UPDATE);
5639 nfs_buf_release(bp, 0);
5640 }
5641 error = 0;
5642 }
1c79356b 5643 }
2d21ac55 5644
1c79356b
A
5645 return (error);
5646}
5647
5648/*
b0d623f7
A
5649 * Purge name cache entries for the given node.
5650 * For RDIRPLUS, also invalidate the entry in the directory's buffers.
5651 */
5652void
5653nfs_name_cache_purge(nfsnode_t dnp, nfsnode_t np, struct componentname *cnp, vfs_context_t ctx)
5654{
5655 struct nfsmount *nmp = NFSTONMP(dnp);
5656
5657 cache_purge(NFSTOV(np));
6d2010ae 5658 if (nmp && (nmp->nm_vers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS))
b0d623f7
A
5659 nfs_dir_buf_cache_lookup(dnp, NULL, cnp, ctx, 1);
5660}
5661
5662/*
5663 * NFS V3 readdir (plus) RPC.
1c79356b
A
5664 */
5665int
b0d623f7 5666nfs3_readdir_rpc(nfsnode_t dnp, struct nfsbuf *bp, vfs_context_t ctx)
1c79356b 5667{
55e303ae 5668 struct nfsmount *nmp;
b0d623f7
A
5669 int error = 0, lockerror, nfsvers, rdirplus, bigcookies;
5670 int i, status, attrflag, fhflag, more_entries = 1, eof, bp_dropped = 0;
5671 uint32_t nmreaddirsize, nmrsize;
5672 uint32_t namlen, skiplen, fhlen, xlen, attrlen, reclen, space_free, space_needed;
5673 uint64_t cookie, lastcookie, xid, savedxid, fileno;
5674 struct nfsm_chain nmreq, nmrep, nmrepsave;
5675 fhandle_t fh;
5676 struct nfs_vattr *nvattrp;
5677 struct nfs_dir_buf_header *ndbhp;
5678 struct direntry *dp;
5679 char *padstart, padlen;
5680 struct timeval now;
1c79356b 5681
2d21ac55 5682 nmp = NFSTONMP(dnp);
fe8ab488 5683 if (nfs_mount_gone(nmp))
55e303ae 5684 return (ENXIO);
2d21ac55 5685 nfsvers = nmp->nm_vers;
55e303ae
A
5686 nmreaddirsize = nmp->nm_readdirsize;
5687 nmrsize = nmp->nm_rsize;
b0d623f7
A
5688 bigcookies = nmp->nm_state & NFSSTA_BIGCOOKIES;
5689noplus:
6d2010ae 5690 rdirplus = ((nfsvers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) ? 1 : 0;
55e303ae 5691
b0d623f7 5692 if ((lockerror = nfs_node_lock(dnp)))
2d21ac55
A
5693 return (lockerror);
5694
b0d623f7
A
5695 /* determine cookie to use, and move dp to the right offset */
5696 ndbhp = (struct nfs_dir_buf_header*)bp->nb_data;
5697 dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp);
5698 if (ndbhp->ndbh_count) {
5699 for (i=0; i < ndbhp->ndbh_count-1; i++)
5700 dp = NFS_DIRENTRY_NEXT(dp);
5701 cookie = dp->d_seekoff;
5702 dp = NFS_DIRENTRY_NEXT(dp);
5703 } else {
5704 cookie = bp->nb_lblkno;
5705 /* increment with every buffer read */
316670eb 5706 OSAddAtomic64(1, &nfsstats.readdir_bios);
2d21ac55 5707 }
b0d623f7 5708 lastcookie = cookie;
2d21ac55 5709
1c79356b 5710 /*
b0d623f7
A
5711 * Loop around doing readdir(plus) RPCs of size nm_readdirsize until
5712 * the buffer is full (or we hit EOF). Then put the remainder of the
5713 * results in the next buffer(s).
1c79356b 5714 */
2d21ac55
A
5715 nfsm_chain_null(&nmreq);
5716 nfsm_chain_null(&nmrep);
b0d623f7 5717 while (nfs_dir_buf_freespace(bp, rdirplus) && !(ndbhp->ndbh_flags & NDB_FULL)) {
2d21ac55 5718 nfsm_chain_build_alloc_init(error, &nmreq,
b0d623f7 5719 NFSX_FH(nfsvers) + NFSX_READDIR(nfsvers) + NFSX_UNSIGNED);
2d21ac55 5720 nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
b0d623f7
A
5721 if (nfsvers == NFS_VER3) {
5722 /* opaque values don't need swapping, but as long */
5723 /* as we are consistent about it, it should be ok */
5724 nfsm_chain_add_64(error, &nmreq, cookie);
5725 nfsm_chain_add_64(error, &nmreq, dnp->n_cookieverf);
5726 } else {
5727 nfsm_chain_add_32(error, &nmreq, cookie);
5728 }
2d21ac55 5729 nfsm_chain_add_32(error, &nmreq, nmreaddirsize);
b0d623f7
A
5730 if (rdirplus)
5731 nfsm_chain_add_32(error, &nmreq, nmrsize);
2d21ac55 5732 nfsm_chain_build_done(error, &nmreq);
b0d623f7 5733 nfs_node_unlock(dnp);
2d21ac55
A
5734 lockerror = ENOENT;
5735 nfsmout_if(error);
5736
b0d623f7
A
5737 error = nfs_request(dnp, NULL, &nmreq,
5738 rdirplus ? NFSPROC_READDIRPLUS : NFSPROC_READDIR,
6d2010ae 5739 ctx, NULL, &nmrep, &xid, &status);
2d21ac55 5740
b0d623f7 5741 if ((lockerror = nfs_node_lock(dnp)))
2d21ac55
A
5742 error = lockerror;
5743
b0d623f7
A
5744 savedxid = xid;
5745 if (nfsvers == NFS_VER3)
5746 nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid);
2d21ac55
A
5747 if (!error)
5748 error = status;
b0d623f7
A
5749 if (nfsvers == NFS_VER3)
5750 nfsm_chain_get_64(error, &nmrep, dnp->n_cookieverf);
5751 nfsm_chain_get_32(error, &nmrep, more_entries);
2d21ac55
A
5752
5753 if (!lockerror) {
b0d623f7 5754 nfs_node_unlock(dnp);
2d21ac55 5755 lockerror = ENOENT;
1c79356b 5756 }
b0d623f7
A
5757 if (error == NFSERR_NOTSUPP) {
5758 /* oops... it doesn't look like readdirplus is supported */
5759 lck_mtx_lock(&nmp->nm_lock);
6d2010ae 5760 NFS_BITMAP_CLR(nmp->nm_flags, NFS_MFLAG_RDIRPLUS);
b0d623f7
A
5761 lck_mtx_unlock(&nmp->nm_lock);
5762 goto noplus;
5763 }
2d21ac55 5764 nfsmout_if(error);
1c79356b 5765
b0d623f7
A
5766 if (rdirplus)
5767 microuptime(&now);
5768
5769 /* loop through the entries packing them into the buffer */
5770 while (more_entries) {
5771 if (nfsvers == NFS_VER3)
5772 nfsm_chain_get_64(error, &nmrep, fileno);
5773 else
5774 nfsm_chain_get_32(error, &nmrep, fileno);
5775 nfsm_chain_get_32(error, &nmrep, namlen);
2d21ac55 5776 nfsmout_if(error);
b0d623f7
A
5777 /* just truncate names that don't fit in direntry.d_name */
5778 if (namlen <= 0) {
1c79356b 5779 error = EBADRPC;
1c79356b
A
5780 goto nfsmout;
5781 }
b0d623f7
A
5782 if (namlen > (sizeof(dp->d_name)-1)) {
5783 skiplen = namlen - sizeof(dp->d_name) + 1;
5784 namlen = sizeof(dp->d_name) - 1;
91447636
A
5785 } else {
5786 skiplen = 0;
5787 }
b0d623f7
A
5788 /* guess that fh size will be same as parent */
5789 fhlen = rdirplus ? (1 + dnp->n_fhsize) : 0;
5790 xlen = rdirplus ? (fhlen + sizeof(time_t)) : 0;
5791 attrlen = rdirplus ? sizeof(struct nfs_vattr) : 0;
5792 reclen = NFS_DIRENTRY_LEN(namlen + xlen);
5793 space_needed = reclen + attrlen;
5794 space_free = nfs_dir_buf_freespace(bp, rdirplus);
5795 if (space_needed > space_free) {
5796 /*
5797 * We still have entries to pack, but we've
5798 * run out of room in the current buffer.
5799 * So we need to move to the next buffer.
5800 * The block# for the next buffer is the
5801 * last cookie in the current buffer.
5802 */
5803nextbuffer:
5804 ndbhp->ndbh_flags |= NDB_FULL;
5805 nfs_buf_release(bp, 0);
5806 bp_dropped = 1;
5807 bp = NULL;
5808 error = nfs_buf_get(dnp, lastcookie, NFS_DIRBLKSIZ, vfs_context_thread(ctx), NBLK_READ, &bp);
2d21ac55 5809 nfsmout_if(error);
b0d623f7
A
5810 /* initialize buffer */
5811 ndbhp = (struct nfs_dir_buf_header*)bp->nb_data;
5812 ndbhp->ndbh_flags = 0;
5813 ndbhp->ndbh_count = 0;
5814 ndbhp->ndbh_entry_end = sizeof(*ndbhp);
5815 ndbhp->ndbh_ncgen = dnp->n_ncgen;
5816 space_free = nfs_dir_buf_freespace(bp, rdirplus);
5817 dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp);
5818 /* increment with every buffer read */
316670eb 5819 OSAddAtomic64(1, &nfsstats.readdir_bios);
91447636 5820 }
b0d623f7
A
5821 nmrepsave = nmrep;
5822 dp->d_fileno = fileno;
5823 dp->d_namlen = namlen;
5824 dp->d_reclen = reclen;
5825 dp->d_type = DT_UNKNOWN;
5826 nfsm_chain_get_opaque(error, &nmrep, namlen, dp->d_name);
2d21ac55 5827 nfsmout_if(error);
b0d623f7
A
5828 dp->d_name[namlen] = '\0';
5829 if (skiplen)
5830 nfsm_chain_adv(error, &nmrep,
5831 nfsm_rndup(namlen + skiplen) - nfsm_rndup(namlen));
5832 if (nfsvers == NFS_VER3)
5833 nfsm_chain_get_64(error, &nmrep, cookie);
5834 else
5835 nfsm_chain_get_32(error, &nmrep, cookie);
5836 nfsmout_if(error);
5837 dp->d_seekoff = cookie;
5838 if (!bigcookies && (cookie >> 32) && (nmp == NFSTONMP(dnp))) {
5839 /* we've got a big cookie, make sure flag is set */
5840 lck_mtx_lock(&nmp->nm_lock);
5841 nmp->nm_state |= NFSSTA_BIGCOOKIES;
5842 lck_mtx_unlock(&nmp->nm_lock);
5843 bigcookies = 1;
5844 }
5845 if (rdirplus) {
5846 nvattrp = NFS_DIR_BUF_NVATTR(bp, ndbhp->ndbh_count);
5847 /* check for attributes */
5848 nfsm_chain_get_32(error, &nmrep, attrflag);
2d21ac55 5849 nfsmout_if(error);
b0d623f7
A
5850 if (attrflag) {
5851 /* grab attributes */
5852 error = nfs_parsefattr(&nmrep, NFS_VER3, nvattrp);
5853 nfsmout_if(error);
5854 dp->d_type = IFTODT(VTTOIF(nvattrp->nva_type));
5855 /* fileid is already in d_fileno, so stash xid in attrs */
5856 nvattrp->nva_fileid = savedxid;
5857 } else {
5858 /* mark the attributes invalid */
5859 bzero(nvattrp, sizeof(struct nfs_vattr));
5860 }
5861 /* check for file handle */
5862 nfsm_chain_get_32(error, &nmrep, fhflag);
5863 nfsmout_if(error);
5864 if (fhflag) {
5865 nfsm_chain_get_fh(error, &nmrep, NFS_VER3, &fh);
5866 nfsmout_if(error);
5867 fhlen = fh.fh_len + 1;
5868 xlen = fhlen + sizeof(time_t);
5869 reclen = NFS_DIRENTRY_LEN(namlen + xlen);
5870 space_needed = reclen + attrlen;
5871 if (space_needed > space_free) {
5872 /* didn't actually have the room... move on to next buffer */
5873 nmrep = nmrepsave;
5874 goto nextbuffer;
2d21ac55 5875 }
b0d623f7
A
5876 /* pack the file handle into the record */
5877 dp->d_name[dp->d_namlen+1] = fh.fh_len;
5878 bcopy(fh.fh_data, &dp->d_name[dp->d_namlen+2], fh.fh_len);
1c79356b 5879 } else {
b0d623f7
A
5880 /* mark the file handle invalid */
5881 fh.fh_len = 0;
5882 fhlen = fh.fh_len + 1;
5883 xlen = fhlen + sizeof(time_t);
5884 reclen = NFS_DIRENTRY_LEN(namlen + xlen);
5885 bzero(&dp->d_name[dp->d_namlen+1], fhlen);
1c79356b 5886 }
b0d623f7
A
5887 *(time_t*)(&dp->d_name[dp->d_namlen+1+fhlen]) = now.tv_sec;
5888 dp->d_reclen = reclen;
1c79356b 5889 }
b0d623f7
A
5890 padstart = dp->d_name + dp->d_namlen + 1 + xlen;
5891 ndbhp->ndbh_count++;
5892 lastcookie = cookie;
5893 /* advance to next direntry in buffer */
5894 dp = NFS_DIRENTRY_NEXT(dp);
5895 ndbhp->ndbh_entry_end = (char*)dp - bp->nb_data;
5896 /* zero out the pad bytes */
5897 padlen = (char*)dp - padstart;
5898 if (padlen > 0)
5899 bzero(padstart, padlen);
5900 /* check for more entries */
5901 nfsm_chain_get_32(error, &nmrep, more_entries);
2d21ac55 5902 nfsmout_if(error);
1c79356b 5903 }
b0d623f7
A
5904 /* Finally, get the eof boolean */
5905 nfsm_chain_get_32(error, &nmrep, eof);
5906 nfsmout_if(error);
5907 if (eof) {
5908 ndbhp->ndbh_flags |= (NDB_FULL|NDB_EOF);
5909 nfs_node_lock_force(dnp);
5910 dnp->n_eofcookie = lastcookie;
5911 nfs_node_unlock(dnp);
5912 } else {
5913 more_entries = 1;
5914 }
5915 if (bp_dropped) {
5916 nfs_buf_release(bp, 0);
5917 bp = NULL;
5918 break;
1c79356b 5919 }
b0d623f7 5920 if ((lockerror = nfs_node_lock(dnp)))
2d21ac55
A
5921 error = lockerror;
5922 nfsmout_if(error);
5923 nfsm_chain_cleanup(&nmrep);
5924 nfsm_chain_null(&nmreq);
5925 }
1c79356b 5926nfsmout:
b0d623f7
A
5927 if (bp_dropped && bp)
5928 nfs_buf_release(bp, 0);
2d21ac55 5929 if (!lockerror)
b0d623f7 5930 nfs_node_unlock(dnp);
2d21ac55
A
5931 nfsm_chain_cleanup(&nmreq);
5932 nfsm_chain_cleanup(&nmrep);
b0d623f7 5933 return (bp_dropped ? NFSERR_DIRBUFDROPPED : error);
1c79356b
A
5934}
5935
5936/*
5937 * Silly rename. To make the NFS filesystem that is stateless look a little
5938 * more like the "ufs" a remove of an active vnode is translated to a rename
2d21ac55 5939 * to a funny looking filename that is removed by nfs_vnop_inactive on the
1c79356b 5940 * nfsnode. There is the potential for another process on a different client
2d21ac55
A
5941 * to create the same funny name between when the lookitup() fails and the
5942 * rename() completes, but...
1c79356b 5943 */
55e303ae 5944
2d21ac55
A
5945/* format of "random" silly names - includes a number and pid */
5946/* (note: shouldn't exceed size of nfs_sillyrename.nsr_name) */
5947#define NFS_SILLYNAME_FORMAT ".nfs.%08x.%04x"
5948/* starting from zero isn't silly enough */
5949static uint32_t nfs_sillyrename_number = 0x20051025;
55e303ae 5950
b0d623f7 5951int
91447636 5952nfs_sillyrename(
2d21ac55
A
5953 nfsnode_t dnp,
5954 nfsnode_t np,
91447636 5955 struct componentname *cnp,
2d21ac55 5956 vfs_context_t ctx)
1c79356b 5957{
2d21ac55 5958 struct nfs_sillyrename *nsp;
1c79356b
A
5959 int error;
5960 short pid;
2d21ac55
A
5961 kauth_cred_t cred;
5962 uint32_t num;
5963 struct nfsmount *nmp;
1c79356b 5964
2d21ac55 5965 nmp = NFSTONMP(dnp);
fe8ab488 5966 if (nfs_mount_gone(nmp))
2d21ac55
A
5967 return (ENXIO);
5968
b0d623f7 5969 nfs_name_cache_purge(dnp, np, cnp, ctx);
2d21ac55
A
5970
5971 MALLOC_ZONE(nsp, struct nfs_sillyrename *,
5972 sizeof (struct nfs_sillyrename), M_NFSREQ, M_WAITOK);
5973 if (!nsp)
91447636 5974 return (ENOMEM);
2d21ac55 5975 cred = vfs_context_ucred(ctx);
91447636 5976 kauth_cred_ref(cred);
2d21ac55
A
5977 nsp->nsr_cred = cred;
5978 nsp->nsr_dnp = dnp;
5979 error = vnode_ref(NFSTOV(dnp));
91447636
A
5980 if (error)
5981 goto bad_norele;
1c79356b
A
5982
5983 /* Fudge together a funny name */
2d21ac55 5984 pid = vfs_context_pid(ctx);
b0d623f7 5985 num = OSAddAtomic(1, &nfs_sillyrename_number);
2d21ac55
A
5986 nsp->nsr_namlen = snprintf(nsp->nsr_name, sizeof(nsp->nsr_name),
5987 NFS_SILLYNAME_FORMAT, num, (pid & 0xffff));
5988 if (nsp->nsr_namlen >= (int)sizeof(nsp->nsr_name))
5989 nsp->nsr_namlen = sizeof(nsp->nsr_name) - 1;
1c79356b
A
5990
5991 /* Try lookitups until we get one that isn't there */
2d21ac55 5992 while (nfs_lookitup(dnp, nsp->nsr_name, nsp->nsr_namlen, ctx, NULL) == 0) {
b0d623f7 5993 num = OSAddAtomic(1, &nfs_sillyrename_number);
2d21ac55
A
5994 nsp->nsr_namlen = snprintf(nsp->nsr_name, sizeof(nsp->nsr_name),
5995 NFS_SILLYNAME_FORMAT, num, (pid & 0xffff));
5996 if (nsp->nsr_namlen >= (int)sizeof(nsp->nsr_name))
5997 nsp->nsr_namlen = sizeof(nsp->nsr_name) - 1;
55e303ae 5998 }
2d21ac55 5999
55e303ae 6000 /* now, do the rename */
2d21ac55
A
6001 error = nmp->nm_funcs->nf_rename_rpc(dnp, cnp->cn_nameptr, cnp->cn_namelen,
6002 dnp, nsp->nsr_name, nsp->nsr_namlen, ctx);
6d2010ae
A
6003
6004 /* Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. */
6005 if (error == ENOENT)
6006 error = 0;
b0d623f7
A
6007 if (!error) {
6008 nfs_node_lock_force(dnp);
6009 if (dnp->n_flag & NNEGNCENTRIES) {
6010 dnp->n_flag &= ~NNEGNCENTRIES;
6011 cache_purge_negatives(NFSTOV(dnp));
6012 }
6013 nfs_node_unlock(dnp);
2d21ac55
A
6014 }
6015 FSDBG(267, dnp, np, num, error);
91447636 6016 if (error)
1c79356b 6017 goto bad;
2d21ac55 6018 error = nfs_lookitup(dnp, nsp->nsr_name, nsp->nsr_namlen, ctx, &np);
b0d623f7 6019 nfs_node_lock_force(np);
2d21ac55 6020 np->n_sillyrename = nsp;
b0d623f7 6021 nfs_node_unlock(np);
1c79356b
A
6022 return (0);
6023bad:
2d21ac55 6024 vnode_rele(NFSTOV(dnp));
91447636 6025bad_norele:
2d21ac55
A
6026 nsp->nsr_cred = NOCRED;
6027 kauth_cred_unref(&cred);
6028 FREE_ZONE(nsp, sizeof(*nsp), M_NFSREQ);
6029 return (error);
6030}
6031
6032int
6033nfs3_lookup_rpc_async(
6034 nfsnode_t dnp,
6035 char *name,
6036 int namelen,
6037 vfs_context_t ctx,
6038 struct nfsreq **reqp)
6039{
6040 struct nfsmount *nmp;
6041 struct nfsm_chain nmreq;
6042 int error = 0, nfsvers;
6043
6044 nmp = NFSTONMP(dnp);
fe8ab488 6045 if (nfs_mount_gone(nmp))
2d21ac55
A
6046 return (ENXIO);
6047 nfsvers = nmp->nm_vers;
6048
6049 nfsm_chain_null(&nmreq);
6050
6051 nfsm_chain_build_alloc_init(error, &nmreq,
6052 NFSX_FH(nfsvers) + NFSX_UNSIGNED + nfsm_rndup(namelen));
6053 nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
6d2010ae 6054 nfsm_chain_add_name(error, &nmreq, name, namelen, nmp);
2d21ac55
A
6055 nfsm_chain_build_done(error, &nmreq);
6056 nfsmout_if(error);
6057 error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_LOOKUP,
6d2010ae 6058 vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, reqp);
2d21ac55
A
6059nfsmout:
6060 nfsm_chain_cleanup(&nmreq);
6061 return (error);
6062}
6063
6064int
6065nfs3_lookup_rpc_async_finish(
6066 nfsnode_t dnp,
6d2010ae
A
6067 __unused char *name,
6068 __unused int namelen,
2d21ac55
A
6069 vfs_context_t ctx,
6070 struct nfsreq *req,
6071 u_int64_t *xidp,
6072 fhandle_t *fhp,
6073 struct nfs_vattr *nvap)
6074{
b0d623f7 6075 int error = 0, lockerror = ENOENT, status, nfsvers, attrflag;
2d21ac55
A
6076 u_int64_t xid;
6077 struct nfsmount *nmp;
6078 struct nfsm_chain nmrep;
6079
6080 nmp = NFSTONMP(dnp);
3e170ce0
A
6081 if (nmp == NULL)
6082 return (ENXIO);
2d21ac55
A
6083 nfsvers = nmp->nm_vers;
6084
6085 nfsm_chain_null(&nmrep);
6086
6087 error = nfs_request_async_finish(req, &nmrep, xidp, &status);
6088
b0d623f7
A
6089 if ((lockerror = nfs_node_lock(dnp)))
6090 error = lockerror;
2d21ac55
A
6091 xid = *xidp;
6092 if (error || status) {
6093 if (nfsvers == NFS_VER3)
6094 nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid);
6095 if (!error)
6096 error = status;
6097 goto nfsmout;
6098 }
6099
6100 nfsmout_if(error || !fhp || !nvap);
6101
6102 /* get the file handle */
6103 nfsm_chain_get_fh(error, &nmrep, nfsvers, fhp);
6104
6105 /* get the attributes */
6106 if (nfsvers == NFS_VER3) {
6107 nfsm_chain_postop_attr_get(error, &nmrep, attrflag, nvap);
6108 nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid);
6109 if (!error && !attrflag)
6d2010ae 6110 error = nfs3_getattr_rpc(NULL, NFSTOMP(dnp), fhp->fh_data, fhp->fh_len, 0, ctx, nvap, xidp);
2d21ac55
A
6111 } else {
6112 error = nfs_parsefattr(&nmrep, nfsvers, nvap);
6113 }
6114nfsmout:
b0d623f7
A
6115 if (!lockerror)
6116 nfs_node_unlock(dnp);
2d21ac55 6117 nfsm_chain_cleanup(&nmrep);
1c79356b
A
6118 return (error);
6119}
6120
6121/*
6122 * Look up a file name and optionally either update the file handle or
6123 * allocate an nfsnode, depending on the value of npp.
6124 * npp == NULL --> just do the lookup
6125 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
6126 * handled too
6127 * *npp != NULL --> update the file handle in the vnode
6128 */
2d21ac55
A
6129int
6130nfs_lookitup(
6131 nfsnode_t dnp,
6132 char *name,
6133 int namelen,
6134 vfs_context_t ctx,
6135 nfsnode_t *npp)
6136{
6137 int error = 0;
6138 nfsnode_t np, newnp = NULL;
6139 u_int64_t xid;
6140 fhandle_t fh;
6141 struct nfsmount *nmp;
6142 struct nfs_vattr nvattr;
6143 struct nfsreq rq, *req = &rq;
6144
6145 nmp = NFSTONMP(dnp);
fe8ab488 6146 if (nfs_mount_gone(nmp))
2d21ac55
A
6147 return (ENXIO);
6148
6149 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME) &&
b0d623f7 6150 (namelen > (int)nmp->nm_fsattr.nfsa_maxname))
2d21ac55
A
6151 return (ENAMETOOLONG);
6152
6d2010ae
A
6153 NVATTR_INIT(&nvattr);
6154
2d21ac55
A
6155 /* check for lookup of "." */
6156 if ((name[0] == '.') && (namelen == 1)) {
6157 /* skip lookup, we know who we are */
6158 fh.fh_len = 0;
6159 newnp = dnp;
6160 goto nfsmout;
6161 }
6162
6163 error = nmp->nm_funcs->nf_lookup_rpc_async(dnp, name, namelen, ctx, &req);
6164 nfsmout_if(error);
6d2010ae 6165 error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, name, namelen, ctx, req, &xid, &fh, &nvattr);
2d21ac55
A
6166 nfsmout_if(!npp || error);
6167
6168 if (*npp) {
6169 np = *npp;
6170 if (fh.fh_len != np->n_fhsize) {
6171 u_char *oldbuf = (np->n_fhsize > NFS_SMALLFH) ? np->n_fhp : NULL;
6172 if (fh.fh_len > NFS_SMALLFH) {
6173 MALLOC_ZONE(np->n_fhp, u_char *, fh.fh_len, M_NFSBIGFH, M_WAITOK);
6174 if (!np->n_fhp) {
6175 np->n_fhp = oldbuf;
6176 error = ENOMEM;
6177 goto nfsmout;
6178 }
6179 } else {
6180 np->n_fhp = &np->n_fh[0];
6181 }
6182 if (oldbuf)
6183 FREE_ZONE(oldbuf, np->n_fhsize, M_NFSBIGFH);
6184 }
6185 bcopy(fh.fh_data, np->n_fhp, fh.fh_len);
6186 np->n_fhsize = fh.fh_len;
b0d623f7 6187 nfs_node_lock_force(np);
2d21ac55 6188 error = nfs_loadattrcache(np, &nvattr, &xid, 0);
b0d623f7 6189 nfs_node_unlock(np);
2d21ac55
A
6190 nfsmout_if(error);
6191 newnp = np;
6192 } else if (NFS_CMPFH(dnp, fh.fh_data, fh.fh_len)) {
b0d623f7 6193 nfs_node_lock_force(dnp);
2d21ac55
A
6194 if (dnp->n_xid <= xid)
6195 error = nfs_loadattrcache(dnp, &nvattr, &xid, 0);
b0d623f7 6196 nfs_node_unlock(dnp);
2d21ac55
A
6197 nfsmout_if(error);
6198 newnp = dnp;
6199 } else {
6200 struct componentname cn, *cnp = &cn;
6201 bzero(cnp, sizeof(*cnp));
6202 cnp->cn_nameptr = name;
6203 cnp->cn_namelen = namelen;
6204 error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len,
6d2010ae 6205 &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np);
2d21ac55
A
6206 nfsmout_if(error);
6207 newnp = np;
6208 }
6209
6210nfsmout:
6211 if (npp && !*npp && !error)
6212 *npp = newnp;
6d2010ae 6213 NVATTR_CLEANUP(&nvattr);
2d21ac55
A
6214 return (error);
6215}
6216
6217/*
6218 * set up and initialize a "._" file lookup structure used for
6219 * performing async lookups.
6220 */
6221void
b0d623f7 6222nfs_dulookup_init(struct nfs_dulookup *dulp, nfsnode_t dnp, const char *name, int namelen, vfs_context_t ctx)
2d21ac55
A
6223{
6224 int error, du_namelen;
6225 vnode_t du_vp;
6d2010ae 6226 struct nfsmount *nmp = NFSTONMP(dnp);
2d21ac55
A
6227
6228 /* check for ._ file in name cache */
6229 dulp->du_flags = 0;
6230 bzero(&dulp->du_cn, sizeof(dulp->du_cn));
6231 du_namelen = namelen + 2;
6d2010ae
A
6232 if (!nmp || NMFLAG(nmp, NONEGNAMECACHE))
6233 return;
2d21ac55
A
6234 if ((namelen >= 2) && (name[0] == '.') && (name[1] == '_'))
6235 return;
6236 if (du_namelen >= (int)sizeof(dulp->du_smallname))
6237 MALLOC(dulp->du_cn.cn_nameptr, char *, du_namelen + 1, M_TEMP, M_WAITOK);
6238 else
6239 dulp->du_cn.cn_nameptr = dulp->du_smallname;
6240 if (!dulp->du_cn.cn_nameptr)
6241 return;
6242 dulp->du_cn.cn_namelen = du_namelen;
6243 snprintf(dulp->du_cn.cn_nameptr, du_namelen + 1, "._%s", name);
6244 dulp->du_cn.cn_nameptr[du_namelen] = '\0';
b0d623f7
A
6245 dulp->du_cn.cn_nameiop = LOOKUP;
6246 dulp->du_cn.cn_flags = MAKEENTRY;
2d21ac55
A
6247
6248 error = cache_lookup(NFSTOV(dnp), &du_vp, &dulp->du_cn);
b0d623f7 6249 if (error == -1) {
2d21ac55 6250 vnode_put(du_vp);
b0d623f7 6251 } else if (!error) {
6d2010ae
A
6252 nmp = NFSTONMP(dnp);
6253 if (nmp && (nmp->nm_vers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) {
b0d623f7
A
6254 /* if rdirplus, try dir buf cache lookup */
6255 nfsnode_t du_np = NULL;
6256 if (!nfs_dir_buf_cache_lookup(dnp, &du_np, &dulp->du_cn, ctx, 0) && du_np) {
6257 /* dir buf cache hit */
6258 du_vp = NFSTOV(du_np);
6259 vnode_put(du_vp);
6260 error = -1;
6261 }
6262 }
6263 if (!error)
6264 dulp->du_flags |= NFS_DULOOKUP_DOIT;
6265 }
2d21ac55
A
6266}
6267
6268/*
6269 * start an async "._" file lookup request
6270 */
6271void
6272nfs_dulookup_start(struct nfs_dulookup *dulp, nfsnode_t dnp, vfs_context_t ctx)
6273{
6274 struct nfsmount *nmp = NFSTONMP(dnp);
6275 struct nfsreq *req = &dulp->du_req;
6276
6d2010ae 6277 if (!nmp || !(dulp->du_flags & NFS_DULOOKUP_DOIT) || (dulp->du_flags & NFS_DULOOKUP_INPROG))
2d21ac55
A
6278 return;
6279 if (!nmp->nm_funcs->nf_lookup_rpc_async(dnp, dulp->du_cn.cn_nameptr,
6280 dulp->du_cn.cn_namelen, ctx, &req))
6281 dulp->du_flags |= NFS_DULOOKUP_INPROG;
6282}
6283
6284/*
6285 * finish an async "._" file lookup request and clean up the structure
6286 */
6287void
6288nfs_dulookup_finish(struct nfs_dulookup *dulp, nfsnode_t dnp, vfs_context_t ctx)
1c79356b 6289{
2d21ac55
A
6290 struct nfsmount *nmp = NFSTONMP(dnp);
6291 int error;
6292 nfsnode_t du_np;
6293 u_int64_t xid;
6294 fhandle_t fh;
91447636 6295 struct nfs_vattr nvattr;
1c79356b 6296
2d21ac55
A
6297 if (!nmp || !(dulp->du_flags & NFS_DULOOKUP_INPROG))
6298 goto out;
55e303ae 6299
6d2010ae
A
6300 NVATTR_INIT(&nvattr);
6301 error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, dulp->du_cn.cn_nameptr,
6302 dulp->du_cn.cn_namelen, ctx, &dulp->du_req, &xid, &fh, &nvattr);
2d21ac55
A
6303 dulp->du_flags &= ~NFS_DULOOKUP_INPROG;
6304 if (error == ENOENT) {
6305 /* add a negative entry in the name cache */
b0d623f7 6306 nfs_node_lock_force(dnp);
2d21ac55
A
6307 cache_enter(NFSTOV(dnp), NULL, &dulp->du_cn);
6308 dnp->n_flag |= NNEGNCENTRIES;
b0d623f7 6309 nfs_node_unlock(dnp);
2d21ac55
A
6310 } else if (!error) {
6311 error = nfs_nget(NFSTOMP(dnp), dnp, &dulp->du_cn, fh.fh_data, fh.fh_len,
6d2010ae 6312 &nvattr, &xid, dulp->du_req.r_auth, NG_MAKEENTRY, &du_np);
2d21ac55 6313 if (!error) {
b0d623f7 6314 nfs_node_unlock(du_np);
2d21ac55 6315 vnode_put(NFSTOV(du_np));
1c79356b 6316 }
1c79356b 6317 }
6d2010ae 6318 NVATTR_CLEANUP(&nvattr);
2d21ac55
A
6319out:
6320 if (dulp->du_flags & NFS_DULOOKUP_INPROG)
6321 nfs_request_async_cancel(&dulp->du_req);
6322 if (dulp->du_cn.cn_nameptr && (dulp->du_cn.cn_nameptr != dulp->du_smallname))
6323 FREE(dulp->du_cn.cn_nameptr, M_TEMP);
1c79356b
A
6324}
6325
2d21ac55 6326
1c79356b 6327/*
2d21ac55 6328 * NFS Version 3 commit RPC
1c79356b 6329 */
55e303ae 6330int
2d21ac55
A
6331nfs3_commit_rpc(
6332 nfsnode_t np,
6d2010ae
A
6333 uint64_t offset,
6334 uint64_t count,
6335 kauth_cred_t cred,
6336 uint64_t wverf)
1c79356b 6337{
2d21ac55
A
6338 struct nfsmount *nmp;
6339 int error = 0, lockerror, status, wccpostattr = 0, nfsvers;
91447636 6340 struct timespec premtime = { 0, 0 };
6d2010ae 6341 u_int64_t xid, newwverf;
2d21ac55
A
6342 uint32_t count32;
6343 struct nfsm_chain nmreq, nmrep;
8f6c56a5 6344
2d21ac55
A
6345 nmp = NFSTONMP(np);
6346 FSDBG(521, np, offset, count, nmp ? nmp->nm_state : 0);
fe8ab488 6347 if (nfs_mount_gone(nmp))
55e303ae 6348 return (ENXIO);
2d21ac55 6349 if (!(nmp->nm_state & NFSSTA_HASWRITEVERF))
1c79356b 6350 return (0);
2d21ac55
A
6351 nfsvers = nmp->nm_vers;
6352
6353 if (count > UINT32_MAX)
6354 count32 = 0;
6355 else
6356 count32 = count;
6357
6358 nfsm_chain_null(&nmreq);
6359 nfsm_chain_null(&nmrep);
6360
6361 nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(NFS_VER3));
6362 nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
6363 nfsm_chain_add_64(error, &nmreq, offset);
6364 nfsm_chain_add_32(error, &nmreq, count32);
6365 nfsm_chain_build_done(error, &nmreq);
6366 nfsmout_if(error);
6367 error = nfs_request2(np, NULL, &nmreq, NFSPROC_COMMIT,
6d2010ae 6368 current_thread(), cred, NULL, 0, &nmrep, &xid, &status);
b0d623f7 6369 if ((lockerror = nfs_node_lock(np)))
2d21ac55
A
6370 error = lockerror;
6371 /* can we do anything useful with the wcc info? */
6372 nfsm_chain_get_wcc_data(error, &nmrep, np, &premtime, &wccpostattr, &xid);
6373 if (!lockerror)
b0d623f7 6374 nfs_node_unlock(np);
2d21ac55
A
6375 if (!error)
6376 error = status;
6d2010ae 6377 nfsm_chain_get_64(error, &nmrep, newwverf);
2d21ac55
A
6378 nfsmout_if(error);
6379 lck_mtx_lock(&nmp->nm_lock);
6d2010ae
A
6380 if (nmp->nm_verf != newwverf)
6381 nmp->nm_verf = newwverf;
6382 if (wverf != newwverf)
2d21ac55 6383 error = NFSERR_STALEWRITEVERF;
2d21ac55
A
6384 lck_mtx_unlock(&nmp->nm_lock);
6385nfsmout:
6386 nfsm_chain_cleanup(&nmreq);
6387 nfsm_chain_cleanup(&nmrep);
1c79356b
A
6388 return (error);
6389}
6390
2d21ac55 6391
b0d623f7 6392int
2d21ac55 6393nfs_vnop_blockmap(
91447636
A
6394 __unused struct vnop_blockmap_args /* {
6395 struct vnodeop_desc *a_desc;
6396 vnode_t a_vp;
6397 off_t a_foffset;
6398 size_t a_size;
6399 daddr64_t *a_bpn;
6400 size_t *a_run;
6401 void *a_poff;
6402 int a_flags;
6403 } */ *ap)
1c79356b 6404{
91447636 6405 return (ENOTSUP);
1c79356b
A
6406}
6407
1c79356b
A
6408
6409/*
2d21ac55 6410 * fsync vnode op. Just call nfs_flush().
1c79356b
A
6411 */
6412/* ARGSUSED */
b0d623f7 6413int
2d21ac55 6414nfs_vnop_fsync(
91447636 6415 struct vnop_fsync_args /* {
1c79356b 6416 struct vnodeop_desc *a_desc;
91447636
A
6417 vnode_t a_vp;
6418 int a_waitfor;
6419 vfs_context_t a_context;
2d21ac55 6420 } */ *ap)
1c79356b 6421{
2d21ac55 6422 return (nfs_flush(VTONFS(ap->a_vp), ap->a_waitfor, vfs_context_thread(ap->a_context), 0));
1c79356b 6423}
1c79356b 6424
1c79356b 6425
55e303ae 6426/*
2d21ac55 6427 * Do an NFS pathconf RPC.
55e303ae 6428 */
91447636 6429int
2d21ac55
A
6430nfs3_pathconf_rpc(
6431 nfsnode_t np,
6432 struct nfs_fsattr *nfsap,
6433 vfs_context_t ctx)
55e303ae 6434{
2d21ac55
A
6435 u_int64_t xid;
6436 int error = 0, lockerror, status, nfsvers;
6437 struct nfsm_chain nmreq, nmrep;
6438 struct nfsmount *nmp = NFSTONMP(np);
6439 uint32_t val = 0;
483a1d10 6440
fe8ab488 6441 if (nfs_mount_gone(nmp))
2d21ac55
A
6442 return (ENXIO);
6443 nfsvers = nmp->nm_vers;
1c79356b 6444
2d21ac55
A
6445 nfsm_chain_null(&nmreq);
6446 nfsm_chain_null(&nmrep);
1c79356b 6447
91447636 6448 /* fetch pathconf info from server */
2d21ac55
A
6449 nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(NFS_VER3));
6450 nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
6451 nfsm_chain_build_done(error, &nmreq);
6452 nfsmout_if(error);
6d2010ae 6453 error = nfs_request(np, NULL, &nmreq, NFSPROC_PATHCONF, ctx, NULL, &nmrep, &xid, &status);
b0d623f7 6454 if ((lockerror = nfs_node_lock(np)))
2d21ac55
A
6455 error = lockerror;
6456 nfsm_chain_postop_attr_update(error, &nmrep, np, &xid);
6457 if (!lockerror)
b0d623f7 6458 nfs_node_unlock(np);
2d21ac55
A
6459 if (!error)
6460 error = status;
6461 nfsm_chain_get_32(error, &nmrep, nfsap->nfsa_maxlink);
6462 nfsm_chain_get_32(error, &nmrep, nfsap->nfsa_maxname);
fe8ab488 6463 nfsap->nfsa_flags &= ~(NFS_FSFLAG_NO_TRUNC|NFS_FSFLAG_CHOWN_RESTRICTED|NFS_FSFLAG_CASE_INSENSITIVE|NFS_FSFLAG_CASE_PRESERVING);
2d21ac55
A
6464 nfsm_chain_get_32(error, &nmrep, val);
6465 if (val)
6466 nfsap->nfsa_flags |= NFS_FSFLAG_NO_TRUNC;
6467 nfsm_chain_get_32(error, &nmrep, val);
6468 if (val)
6469 nfsap->nfsa_flags |= NFS_FSFLAG_CHOWN_RESTRICTED;
6470 nfsm_chain_get_32(error, &nmrep, val);
6471 if (val)
6472 nfsap->nfsa_flags |= NFS_FSFLAG_CASE_INSENSITIVE;
6473 nfsm_chain_get_32(error, &nmrep, val);
6474 if (val)
6475 nfsap->nfsa_flags |= NFS_FSFLAG_CASE_PRESERVING;
6476 NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_MAXLINK);
6477 NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_MAXNAME);
6478 NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_NO_TRUNC);
6479 NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_CHOWN_RESTRICTED);
6480 NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_INSENSITIVE);
6481 NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_PRESERVING);
6482nfsmout:
6483 nfsm_chain_cleanup(&nmreq);
6484 nfsm_chain_cleanup(&nmrep);
91447636 6485 return (error);
1c79356b
A
6486}
6487
2d21ac55 6488/* save pathconf info for NFSv3 mount */
91447636 6489void
2d21ac55 6490nfs3_pathconf_cache(struct nfsmount *nmp, struct nfs_fsattr *nfsap)
1c79356b 6491{
2d21ac55
A
6492 nmp->nm_fsattr.nfsa_maxlink = nfsap->nfsa_maxlink;
6493 nmp->nm_fsattr.nfsa_maxname = nfsap->nfsa_maxname;
fe8ab488 6494 nmp->nm_fsattr.nfsa_flags &= ~(NFS_FSFLAG_NO_TRUNC|NFS_FSFLAG_CHOWN_RESTRICTED|NFS_FSFLAG_CASE_INSENSITIVE|NFS_FSFLAG_CASE_PRESERVING);
2d21ac55
A
6495 nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_NO_TRUNC;
6496 nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_CHOWN_RESTRICTED;
6497 nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_CASE_INSENSITIVE;
6498 nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_CASE_PRESERVING;
6499 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXLINK);
6500 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME);
6501 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_NO_TRUNC);
6502 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CHOWN_RESTRICTED);
6503 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CASE_INSENSITIVE);
6504 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CASE_PRESERVING);
91447636 6505 nmp->nm_state |= NFSSTA_GOTPATHCONF;
1c79356b
A
6506}
6507
6508/*
91447636
A
6509 * Return POSIX pathconf information applicable to nfs.
6510 *
6511 * The NFS V2 protocol doesn't support this, so just return EINVAL
6512 * for V2.
1c79356b 6513 */
91447636 6514/* ARGSUSED */
b0d623f7 6515int
2d21ac55 6516nfs_vnop_pathconf(
91447636
A
6517 struct vnop_pathconf_args /* {
6518 struct vnodeop_desc *a_desc;
6519 vnode_t a_vp;
6520 int a_name;
b0d623f7 6521 int32_t *a_retval;
91447636 6522 vfs_context_t a_context;
2d21ac55 6523 } */ *ap)
1c79356b 6524{
91447636 6525 vnode_t vp = ap->a_vp;
2d21ac55 6526 nfsnode_t np = VTONFS(vp);
91447636 6527 struct nfsmount *nmp;
2d21ac55
A
6528 struct nfs_fsattr nfsa, *nfsap;
6529 int error = 0;
6530 uint64_t maxFileSize;
6531 uint nbits;
1c79356b 6532
2d21ac55 6533 nmp = VTONMP(vp);
fe8ab488 6534 if (nfs_mount_gone(nmp))
91447636 6535 return (ENXIO);
1c79356b 6536
91447636
A
6537 switch (ap->a_name) {
6538 case _PC_LINK_MAX:
6539 case _PC_NAME_MAX:
6540 case _PC_CHOWN_RESTRICTED:
6541 case _PC_NO_TRUNC:
6542 case _PC_CASE_SENSITIVE:
6543 case _PC_CASE_PRESERVING:
6544 break;
2d21ac55
A
6545 case _PC_FILESIZEBITS:
6546 if (nmp->nm_vers == NFS_VER2) {
6547 *ap->a_retval = 32;
6548 return (0);
6549 }
6550 break;
6d2010ae
A
6551 case _PC_XATTR_SIZE_BITS:
6552 /* Do we support xattrs natively? */
6553 if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR)
6554 break; /* Yes */
6555 /* No... so just return an error */
6556 /* FALLTHROUGH */
91447636
A
6557 default:
6558 /* don't bother contacting the server if we know the answer */
6559 return (EINVAL);
6560 }
1c79356b 6561
2d21ac55
A
6562 if (nmp->nm_vers == NFS_VER2)
6563 return (EINVAL);
6564
6565 lck_mtx_lock(&nmp->nm_lock);
6566 if (nmp->nm_vers == NFS_VER3) {
6567 if (!(nmp->nm_state & NFSSTA_GOTPATHCONF)) {
6568 /* no pathconf info cached */
6569 lck_mtx_unlock(&nmp->nm_lock);
6570 NFS_CLEAR_ATTRIBUTES(nfsa.nfsa_bitmap);
6571 error = nfs3_pathconf_rpc(np, &nfsa, ap->a_context);
6572 if (error)
6573 return (error);
6574 nmp = VTONMP(vp);
fe8ab488 6575 if (nfs_mount_gone(nmp))
2d21ac55
A
6576 return (ENXIO);
6577 lck_mtx_lock(&nmp->nm_lock);
6578 if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_HOMOGENEOUS) {
6579 /* all files have the same pathconf info, */
6580 /* so cache a copy of the results */
6581 nfs3_pathconf_cache(nmp, &nfsa);
6582 }
6583 nfsap = &nfsa;
6584 } else {
6585 nfsap = &nmp->nm_fsattr;
6586 }
6587 } else if (!(nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_HOMOGENEOUS)) {
91447636 6588 /* no pathconf info cached */
2d21ac55
A
6589 lck_mtx_unlock(&nmp->nm_lock);
6590 NFS_CLEAR_ATTRIBUTES(nfsa.nfsa_bitmap);
6591 error = nfs4_pathconf_rpc(np, &nfsa, ap->a_context);
91447636
A
6592 if (error)
6593 return (error);
2d21ac55 6594 nmp = VTONMP(vp);
fe8ab488 6595 if (nfs_mount_gone(nmp))
91447636 6596 return (ENXIO);
2d21ac55
A
6597 lck_mtx_lock(&nmp->nm_lock);
6598 nfsap = &nfsa;
6599 } else {
6600 nfsap = &nmp->nm_fsattr;
91447636 6601 }
1c79356b 6602
91447636
A
6603 switch (ap->a_name) {
6604 case _PC_LINK_MAX:
2d21ac55
A
6605 if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_MAXLINK))
6606 *ap->a_retval = nfsap->nfsa_maxlink;
6607 else if ((nmp->nm_vers == NFS_VER4) && NFS_BITMAP_ISSET(np->n_vattr.nva_bitmap, NFS_FATTR_MAXLINK))
6608 *ap->a_retval = np->n_vattr.nva_maxlink;
91447636 6609 else
2d21ac55 6610 error = EINVAL;
91447636 6611 break;
2d21ac55
A
6612 case _PC_NAME_MAX:
6613 if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_MAXNAME))
6614 *ap->a_retval = nfsap->nfsa_maxname;
6615 else
6616 error = EINVAL;
6617 break;
6618 case _PC_CHOWN_RESTRICTED:
6619 if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_CHOWN_RESTRICTED))
b0d623f7 6620 *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_CHOWN_RESTRICTED) ? 200112 /* _POSIX_CHOWN_RESTRICTED */ : 0;
2d21ac55
A
6621 else
6622 error = EINVAL;
6623 break;
6624 case _PC_NO_TRUNC:
6625 if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_NO_TRUNC))
b0d623f7 6626 *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_NO_TRUNC) ? 200112 /* _POSIX_NO_TRUNC */ : 0;
2d21ac55
A
6627 else
6628 error = EINVAL;
6629 break;
6630 case _PC_CASE_SENSITIVE:
6631 if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_INSENSITIVE))
b0d623f7 6632 *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_CASE_INSENSITIVE) ? 0 : 1;
2d21ac55
A
6633 else
6634 error = EINVAL;
6635 break;
6636 case _PC_CASE_PRESERVING:
6637 if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_PRESERVING))
b0d623f7 6638 *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_CASE_PRESERVING) ? 1 : 0;
2d21ac55
A
6639 else
6640 error = EINVAL;
6641 break;
6d2010ae 6642 case _PC_XATTR_SIZE_BITS: /* same as file size bits if named attrs supported */
2d21ac55 6643 case _PC_FILESIZEBITS:
b0d623f7 6644 if (!NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_MAXFILESIZE)) {
2d21ac55
A
6645 *ap->a_retval = 64;
6646 error = 0;
6647 break;
1c79356b 6648 }
b0d623f7 6649 maxFileSize = nfsap->nfsa_maxfilesize;
2d21ac55
A
6650 nbits = 1;
6651 if (maxFileSize & 0xffffffff00000000ULL) {
6652 nbits += 32;
6653 maxFileSize >>= 32;
91447636 6654 }
2d21ac55
A
6655 if (maxFileSize & 0xffff0000) {
6656 nbits += 16;
6657 maxFileSize >>= 16;
6658 }
6659 if (maxFileSize & 0xff00) {
6660 nbits += 8;
6661 maxFileSize >>= 8;
6662 }
6663 if (maxFileSize & 0xf0) {
6664 nbits += 4;
6665 maxFileSize >>= 4;
91447636 6666 }
2d21ac55
A
6667 if (maxFileSize & 0xc) {
6668 nbits += 2;
6669 maxFileSize >>= 2;
6670 }
6671 if (maxFileSize & 0x2) {
6672 nbits += 1;
6673 }
6674 *ap->a_retval = nbits;
6675 break;
6676 default:
6677 error = EINVAL;
6678 }
1c79356b 6679
2d21ac55
A
6680 lck_mtx_unlock(&nmp->nm_lock);
6681
6682 return (error);
1c79356b
A
6683}
6684
1c79356b
A
6685/*
6686 * Read wrapper for special devices.
6687 */
b0d623f7 6688int
2d21ac55 6689nfsspec_vnop_read(
91447636
A
6690 struct vnop_read_args /* {
6691 struct vnodeop_desc *a_desc;
6692 vnode_t a_vp;
1c79356b 6693 struct uio *a_uio;
91447636
A
6694 int a_ioflag;
6695 vfs_context_t a_context;
2d21ac55 6696 } */ *ap)
1c79356b 6697{
2d21ac55 6698 nfsnode_t np = VTONFS(ap->a_vp);
55e303ae 6699 struct timeval now;
2d21ac55 6700 int error;
1c79356b
A
6701
6702 /*
6703 * Set access flag.
6704 */
b0d623f7 6705 if ((error = nfs_node_lock(np)))
2d21ac55 6706 return (error);
1c79356b 6707 np->n_flag |= NACC;
55e303ae
A
6708 microtime(&now);
6709 np->n_atim.tv_sec = now.tv_sec;
6710 np->n_atim.tv_nsec = now.tv_usec * 1000;
b0d623f7 6711 nfs_node_unlock(np);
91447636 6712 return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_read), ap));
1c79356b
A
6713}
6714
6715/*
6716 * Write wrapper for special devices.
6717 */
b0d623f7 6718int
2d21ac55 6719nfsspec_vnop_write(
91447636
A
6720 struct vnop_write_args /* {
6721 struct vnodeop_desc *a_desc;
6722 vnode_t a_vp;
1c79356b 6723 struct uio *a_uio;
91447636
A
6724 int a_ioflag;
6725 vfs_context_t a_context;
2d21ac55 6726 } */ *ap)
1c79356b 6727{
2d21ac55 6728 nfsnode_t np = VTONFS(ap->a_vp);
55e303ae 6729 struct timeval now;
2d21ac55 6730 int error;
1c79356b
A
6731
6732 /*
6733 * Set update flag.
6734 */
b0d623f7 6735 if ((error = nfs_node_lock(np)))
2d21ac55 6736 return (error);
1c79356b 6737 np->n_flag |= NUPD;
55e303ae
A
6738 microtime(&now);
6739 np->n_mtim.tv_sec = now.tv_sec;
6740 np->n_mtim.tv_nsec = now.tv_usec * 1000;
b0d623f7 6741 nfs_node_unlock(np);
91447636 6742 return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_write), ap));
1c79356b
A
6743}
6744
6745/*
6746 * Close wrapper for special devices.
6747 *
6748 * Update the times on the nfsnode then do device close.
6749 */
b0d623f7 6750int
2d21ac55 6751nfsspec_vnop_close(
91447636
A
6752 struct vnop_close_args /* {
6753 struct vnodeop_desc *a_desc;
6754 vnode_t a_vp;
6755 int a_fflag;
6756 vfs_context_t a_context;
2d21ac55 6757 } */ *ap)
1c79356b 6758{
91447636 6759 vnode_t vp = ap->a_vp;
2d21ac55 6760 nfsnode_t np = VTONFS(vp);
91447636
A
6761 struct vnode_attr vattr;
6762 mount_t mp;
2d21ac55 6763 int error;
1c79356b 6764
b0d623f7 6765 if ((error = nfs_node_lock(np)))
2d21ac55 6766 return (error);
1c79356b
A
6767 if (np->n_flag & (NACC | NUPD)) {
6768 np->n_flag |= NCHG;
b0d623f7 6769 if (!vnode_isinuse(vp, 0) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) {
91447636
A
6770 VATTR_INIT(&vattr);
6771 if (np->n_flag & NACC) {
6772 vattr.va_access_time = np->n_atim;
6773 VATTR_SET_ACTIVE(&vattr, va_access_time);
6774 }
6775 if (np->n_flag & NUPD) {
6776 vattr.va_modify_time = np->n_mtim;
6777 VATTR_SET_ACTIVE(&vattr, va_modify_time);
6778 }
b0d623f7 6779 nfs_node_unlock(np);
91447636 6780 vnode_setattr(vp, &vattr, ap->a_context);
2d21ac55 6781 } else {
b0d623f7 6782 nfs_node_unlock(np);
1c79356b 6783 }
2d21ac55 6784 } else {
b0d623f7 6785 nfs_node_unlock(np);
1c79356b 6786 }
91447636 6787 return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_close), ap));
1c79356b
A
6788}
6789
2d21ac55 6790#if FIFO
91447636
A
6791extern vnop_t **fifo_vnodeop_p;
6792
1c79356b
A
6793/*
6794 * Read wrapper for fifos.
6795 */
b0d623f7 6796int
2d21ac55 6797nfsfifo_vnop_read(
91447636
A
6798 struct vnop_read_args /* {
6799 struct vnodeop_desc *a_desc;
6800 vnode_t a_vp;
1c79356b 6801 struct uio *a_uio;
91447636
A
6802 int a_ioflag;
6803 vfs_context_t a_context;
2d21ac55 6804 } */ *ap)
1c79356b 6805{
2d21ac55 6806 nfsnode_t np = VTONFS(ap->a_vp);
55e303ae 6807 struct timeval now;
2d21ac55 6808 int error;
1c79356b
A
6809
6810 /*
6811 * Set access flag.
6812 */
b0d623f7 6813 if ((error = nfs_node_lock(np)))
2d21ac55 6814 return (error);
1c79356b 6815 np->n_flag |= NACC;
55e303ae
A
6816 microtime(&now);
6817 np->n_atim.tv_sec = now.tv_sec;
6818 np->n_atim.tv_nsec = now.tv_usec * 1000;
b0d623f7 6819 nfs_node_unlock(np);
91447636 6820 return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_read), ap));
1c79356b
A
6821}
6822
6823/*
6824 * Write wrapper for fifos.
6825 */
b0d623f7 6826int
2d21ac55 6827nfsfifo_vnop_write(
91447636
A
6828 struct vnop_write_args /* {
6829 struct vnodeop_desc *a_desc;
6830 vnode_t a_vp;
1c79356b 6831 struct uio *a_uio;
91447636
A
6832 int a_ioflag;
6833 vfs_context_t a_context;
2d21ac55 6834 } */ *ap)
1c79356b 6835{
2d21ac55 6836 nfsnode_t np = VTONFS(ap->a_vp);
55e303ae 6837 struct timeval now;
2d21ac55 6838 int error;
1c79356b
A
6839
6840 /*
6841 * Set update flag.
6842 */
b0d623f7 6843 if ((error = nfs_node_lock(np)))
2d21ac55 6844 return (error);
1c79356b 6845 np->n_flag |= NUPD;
55e303ae
A
6846 microtime(&now);
6847 np->n_mtim.tv_sec = now.tv_sec;
6848 np->n_mtim.tv_nsec = now.tv_usec * 1000;
b0d623f7 6849 nfs_node_unlock(np);
91447636 6850 return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_write), ap));
1c79356b
A
6851}
6852
6853/*
6854 * Close wrapper for fifos.
6855 *
6856 * Update the times on the nfsnode then do fifo close.
6857 */
b0d623f7 6858int
2d21ac55 6859nfsfifo_vnop_close(
91447636
A
6860 struct vnop_close_args /* {
6861 struct vnodeop_desc *a_desc;
6862 vnode_t a_vp;
6863 int a_fflag;
6864 vfs_context_t a_context;
2d21ac55 6865 } */ *ap)
1c79356b 6866{
91447636 6867 vnode_t vp = ap->a_vp;
2d21ac55 6868 nfsnode_t np = VTONFS(vp);
91447636 6869 struct vnode_attr vattr;
55e303ae 6870 struct timeval now;
91447636 6871 mount_t mp;
2d21ac55 6872 int error;
1c79356b 6873
b0d623f7 6874 if ((error = nfs_node_lock(np)))
2d21ac55 6875 return (error);
1c79356b 6876 if (np->n_flag & (NACC | NUPD)) {
55e303ae 6877 microtime(&now);
1c79356b 6878 if (np->n_flag & NACC) {
55e303ae
A
6879 np->n_atim.tv_sec = now.tv_sec;
6880 np->n_atim.tv_nsec = now.tv_usec * 1000;
1c79356b
A
6881 }
6882 if (np->n_flag & NUPD) {
55e303ae
A
6883 np->n_mtim.tv_sec = now.tv_sec;
6884 np->n_mtim.tv_nsec = now.tv_usec * 1000;
1c79356b
A
6885 }
6886 np->n_flag |= NCHG;
91447636
A
6887 if (!vnode_isinuse(vp, 1) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) {
6888 VATTR_INIT(&vattr);
6889 if (np->n_flag & NACC) {
6890 vattr.va_access_time = np->n_atim;
6891 VATTR_SET_ACTIVE(&vattr, va_access_time);
6892 }
6893 if (np->n_flag & NUPD) {
6894 vattr.va_modify_time = np->n_mtim;
6895 VATTR_SET_ACTIVE(&vattr, va_modify_time);
6896 }
b0d623f7 6897 nfs_node_unlock(np);
91447636 6898 vnode_setattr(vp, &vattr, ap->a_context);
2d21ac55 6899 } else {
b0d623f7 6900 nfs_node_unlock(np);
1c79356b 6901 }
2d21ac55 6902 } else {
b0d623f7 6903 nfs_node_unlock(np);
1c79356b 6904 }
91447636 6905 return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_close), ap));
1c79356b 6906}
2d21ac55 6907#endif /* FIFO */
1c79356b 6908
91447636 6909/*ARGSUSED*/
b0d623f7 6910int
2d21ac55 6911nfs_vnop_ioctl(
6d2010ae 6912 struct vnop_ioctl_args /* {
91447636
A
6913 struct vnodeop_desc *a_desc;
6914 vnode_t a_vp;
b0d623f7 6915 u_int32_t a_command;
91447636
A
6916 caddr_t a_data;
6917 int a_fflag;
2d21ac55 6918 vfs_context_t a_context;
91447636 6919 } */ *ap)
1c79356b 6920{
6d2010ae
A
6921 vfs_context_t ctx = ap->a_context;
6922 vnode_t vp = ap->a_vp;
39236c6e 6923 struct nfsmount *mp = VTONMP(vp);
3e170ce0
A
6924 struct user_nfs_gss_principal gprinc;
6925 uint32_t len;
6d2010ae 6926 int error = ENOTTY;
1c79356b 6927
39236c6e
A
6928 if (mp == NULL)
6929 return (ENXIO);
6930
6d2010ae
A
6931 switch (ap->a_command) {
6932
6933 case F_FULLFSYNC:
6934 if (vnode_vfsisrdonly(vp))
6935 return (EROFS);
6d2010ae
A
6936 error = nfs_flush(VTONFS(vp), MNT_WAIT, vfs_context_thread(ctx), 0);
6937 break;
39236c6e 6938 case NFS_FSCTL_DESTROY_CRED:
3e170ce0
A
6939 if (!auth_is_kerberized(mp->nm_auth))
6940 return (ENOTSUP);
fe8ab488 6941 error = nfs_gss_clnt_ctx_remove(mp, vfs_context_ucred(ctx));
39236c6e 6942 break;
3e170ce0
A
6943 case NFS_FSCTL_SET_CRED:
6944 if (!auth_is_kerberized(mp->nm_auth))
6945 return (ENOTSUP);
3e170ce0
A
6946 if (vfs_context_is64bit(ctx)) {
6947 gprinc = *(struct user_nfs_gss_principal *)ap->a_data;
6948 } else {
6949 struct nfs_gss_principal *tp;
6950 tp = (struct nfs_gss_principal *)ap->a_data;
6951 gprinc.princlen = tp->princlen;
6952 gprinc.nametype = tp->nametype;
6953 gprinc.principal = CAST_USER_ADDR_T(tp->principal);
6954 }
39037602 6955 NFS_DBG(NFS_FAC_GSS, 7, "Enter NFS_FSCTL_SET_CRED (64-bit=%d): principal length %d name type %d usr pointer 0x%llx\n", vfs_context_is64bit(ctx), gprinc.princlen, gprinc.nametype, (unsigned long long)gprinc.principal);
3e170ce0
A
6956 if (gprinc.princlen > MAXPATHLEN)
6957 return (EINVAL);
3e170ce0
A
6958 uint8_t *p;
6959 MALLOC(p, uint8_t *, gprinc.princlen+1, M_TEMP, M_WAITOK|M_ZERO);
6960 if (p == NULL)
6961 return (ENOMEM);
6962 error = copyin(gprinc.principal, p, gprinc.princlen);
6963 if (error) {
6964 NFS_DBG(NFS_FAC_GSS, 7, "NFS_FSCTL_SET_CRED could not copy in princiapl data of len %d: %d\n",
6965 gprinc.princlen, error);
6966 FREE(p, M_TEMP);
6967 return (error);
6968 }
6969 NFS_DBG(NFS_FAC_GSS, 7, "Seting credential to principal %s\n", p);
6970 error = nfs_gss_clnt_ctx_set_principal(mp, ctx, p, gprinc.princlen, gprinc.nametype);
6971 NFS_DBG(NFS_FAC_GSS, 7, "Seting credential to principal %s returned %d\n", p, error);
6972 FREE(p, M_TEMP);
6973 break;
6974 case NFS_FSCTL_GET_CRED:
6975 if (!auth_is_kerberized(mp->nm_auth))
6976 return (ENOTSUP);
6977 error = nfs_gss_clnt_ctx_get_principal(mp, ctx, &gprinc);
6978 if (error)
6979 break;
6980 if (vfs_context_is64bit(ctx)) {
6981 struct user_nfs_gss_principal *upp = (struct user_nfs_gss_principal *)ap->a_data;
6982 len = upp->princlen;
6983 if (gprinc.princlen < len)
6984 len = gprinc.princlen;
6985 upp->princlen = gprinc.princlen;
6986 upp->nametype = gprinc.nametype;
6987 upp->flags = gprinc.flags;
6988 if (gprinc.principal)
6989 error = copyout((void *)gprinc.principal, upp->principal, len);
6990 else
6991 upp->principal = USER_ADDR_NULL;
6992 } else {
6993 struct nfs_gss_principal *u32pp = (struct nfs_gss_principal *)ap->a_data;
6994 len = u32pp->princlen;
6995 if (gprinc.princlen < len)
6996 len = gprinc.princlen;
6997 u32pp->princlen = gprinc.princlen;
6998 u32pp->nametype = gprinc.nametype;
6999 u32pp->flags = gprinc.flags;
7000 if (gprinc.principal)
7001 error = copyout((void *)gprinc.principal, u32pp->principal, len);
7002 else
7003 u32pp->principal = (user32_addr_t)0;
7004 }
7005 if (error) {
7006 NFS_DBG(NFS_FAC_GSS, 7, "NFS_FSCTL_GET_CRED could not copy out princiapl data of len %d: %d\n",
7007 gprinc.princlen, error);
7008 }
39037602
A
7009 if (gprinc.principal)
7010 FREE(gprinc.principal, M_TEMP);
6d2010ae
A
7011 }
7012
7013 return (error);
1c79356b
A
7014}
7015
91447636 7016/*ARGSUSED*/
b0d623f7 7017int
2d21ac55 7018nfs_vnop_select(
91447636
A
7019 __unused struct vnop_select_args /* {
7020 struct vnodeop_desc *a_desc;
7021 vnode_t a_vp;
7022 int a_which;
7023 int a_fflags;
91447636 7024 void *a_wql;
2d21ac55 7025 vfs_context_t a_context;
91447636 7026 } */ *ap)
1c79356b
A
7027{
7028
7029 /*
7030 * We were once bogusly seltrue() which returns 1. Is this right?
7031 */
7032 return (1);
7033}
7034
1c79356b 7035/*
2d21ac55
A
7036 * vnode OP for pagein using UPL
7037 *
7038 * No buffer I/O, just RPCs straight into the mapped pages.
1c79356b 7039 */
b0d623f7 7040int
2d21ac55 7041nfs_vnop_pagein(
91447636
A
7042 struct vnop_pagein_args /* {
7043 struct vnodeop_desc *a_desc;
7044 vnode_t a_vp;
7045 upl_t a_pl;
7046 vm_offset_t a_pl_offset;
7047 off_t a_f_offset;
7048 size_t a_size;
7049 int a_flags;
7050 vfs_context_t a_context;
2d21ac55 7051 } */ *ap)
1c79356b 7052{
91447636 7053 vnode_t vp = ap->a_vp;
1c79356b 7054 upl_t pl = ap->a_pl;
2d21ac55 7055 size_t size = ap->a_size;
1c79356b
A
7056 off_t f_offset = ap->a_f_offset;
7057 vm_offset_t pl_offset = ap->a_pl_offset;
2d21ac55
A
7058 int flags = ap->a_flags;
7059 thread_t thd;
91447636 7060 kauth_cred_t cred;
2d21ac55
A
7061 nfsnode_t np = VTONFS(vp);
7062 size_t nmrsize, iosize, txsize, rxsize, retsize;
7063 off_t txoffset;
55e303ae 7064 struct nfsmount *nmp;
1c79356b 7065 int error = 0;
b0d623f7
A
7066 vm_offset_t ioaddr, rxaddr;
7067 uio_t uio;
7068 char uio_buf [ UIO_SIZEOF(1) ];
fa4905b1 7069 int nofreeupl = flags & UPL_NOCOMMIT;
55e303ae 7070 upl_page_info_t *plinfo;
2d21ac55
A
7071#define MAXPAGINGREQS 16 /* max outstanding RPCs for pagein/pageout */
7072 struct nfsreq *req[MAXPAGINGREQS];
7073 int nextsend, nextwait;
b0d623f7
A
7074 uint32_t stategenid = 0, restart = 0;
7075 kern_return_t kret;
1c79356b 7076
2d21ac55 7077 FSDBG(322, np, f_offset, size, flags);
fa4905b1
A
7078 if (pl == (upl_t)NULL)
7079 panic("nfs_pagein: no upl");
1c79356b 7080
fa4905b1 7081 if (size <= 0) {
2d21ac55 7082 printf("nfs_pagein: invalid size %ld", size);
fa4905b1 7083 if (!nofreeupl)
316670eb 7084 (void) ubc_upl_abort_range(pl, pl_offset, size, 0);
1c79356b 7085 return (EINVAL);
fa4905b1 7086 }
91447636 7087 if (f_offset < 0 || f_offset >= (off_t)np->n_size || (f_offset & PAGE_MASK_64)) {
fa4905b1 7088 if (!nofreeupl)
2d21ac55 7089 ubc_upl_abort_range(pl, pl_offset, size,
1c79356b
A
7090 UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY);
7091 return (EINVAL);
7092 }
91447636 7093
2d21ac55 7094 thd = vfs_context_thread(ap->a_context);
fa4905b1 7095 cred = ubc_getcred(vp);
0c530ab8 7096 if (!IS_VALID_CRED(cred))
91447636 7097 cred = vfs_context_ucred(ap->a_context);
1c79356b 7098
b0d623f7
A
7099 uio = uio_createwithbuffer(1, f_offset, UIO_SYSSPACE, UIO_READ,
7100 &uio_buf, sizeof(uio_buf));
1c79356b 7101
2d21ac55 7102 nmp = VTONMP(vp);
fe8ab488 7103 if (nfs_mount_gone(nmp)) {
55e303ae 7104 if (!nofreeupl)
2d21ac55 7105 ubc_upl_abort_range(pl, pl_offset, size,
55e303ae
A
7106 UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY);
7107 return (ENXIO);
7108 }
2d21ac55 7109 nmrsize = nmp->nm_rsize;
1c79356b 7110
55e303ae 7111 plinfo = ubc_upl_pageinfo(pl);
b0d623f7
A
7112 kret = ubc_upl_map(pl, &ioaddr);
7113 if (kret != KERN_SUCCESS)
7114 panic("nfs_vnop_pagein: ubc_upl_map() failed with (%d)", kret);
1c79356b 7115 ioaddr += pl_offset;
b0d623f7
A
7116
7117tryagain:
7118 if (nmp->nm_vers >= NFS_VER4)
7119 stategenid = nmp->nm_stategenid;
2d21ac55
A
7120 txsize = rxsize = size;
7121 txoffset = f_offset;
b0d623f7 7122 rxaddr = ioaddr;
1c79356b 7123
2d21ac55
A
7124 bzero(req, sizeof(req));
7125 nextsend = nextwait = 0;
1c79356b 7126 do {
6d2010ae
A
7127 if (np->n_flag & NREVOKE) {
7128 error = EIO;
7129 break;
7130 }
2d21ac55
A
7131 /* send requests while we need to and have available slots */
7132 while ((txsize > 0) && (req[nextsend] == NULL)) {
7133 iosize = MIN(nmrsize, txsize);
7134 if ((error = nmp->nm_funcs->nf_read_rpc_async(np, txoffset, iosize, thd, cred, NULL, &req[nextsend]))) {
7135 req[nextsend] = NULL;
7136 break;
7137 }
7138 txoffset += iosize;
7139 txsize -= iosize;
7140 nextsend = (nextsend + 1) % MAXPAGINGREQS;
7141 }
7142 /* wait while we need to and break out if more requests to send */
7143 while ((rxsize > 0) && req[nextwait]) {
7144 iosize = retsize = MIN(nmrsize, rxsize);
b0d623f7
A
7145 uio_reset(uio, uio_offset(uio), UIO_SYSSPACE, UIO_READ);
7146 uio_addiov(uio, CAST_USER_ADDR_T(rxaddr), iosize);
7147 FSDBG(322, uio_offset(uio), uio_resid(uio), rxaddr, rxsize);
7148#if UPL_DEBUG
7149 upl_ubc_alias_set(pl, (uintptr_t) current_thread(), (uintptr_t) 2);
91447636 7150#endif /* UPL_DEBUG */
316670eb 7151 OSAddAtomic64(1, &nfsstats.pageins);
2d21ac55
A
7152 error = nmp->nm_funcs->nf_read_rpc_async_finish(np, req[nextwait], uio, &retsize, NULL);
7153 req[nextwait] = NULL;
7154 nextwait = (nextwait + 1) % MAXPAGINGREQS;
b0d623f7
A
7155 if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error)) {
7156 lck_mtx_lock(&nmp->nm_lock);
6d2010ae
A
7157 if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) {
7158 NP(np, "nfs_vnop_pagein: error %d, initiating recovery", error);
7159 nfs_need_recover(nmp, error);
b0d623f7
A
7160 }
7161 lck_mtx_unlock(&nmp->nm_lock);
b0d623f7
A
7162 restart++;
7163 goto cancel;
7164 }
2d21ac55 7165 if (error) {
b0d623f7 7166 FSDBG(322, uio_offset(uio), uio_resid(uio), error, -1);
2d21ac55
A
7167 break;
7168 }
7169 if (retsize < iosize) {
7170 /* Just zero fill the rest of the valid area. */
2d21ac55 7171 int zcnt = iosize - retsize;
b0d623f7
A
7172 bzero((char *)rxaddr + retsize, zcnt);
7173 FSDBG(324, uio_offset(uio), retsize, zcnt, rxaddr);
7174 uio_update(uio, zcnt);
1c79356b 7175 }
b0d623f7 7176 rxaddr += iosize;
2d21ac55
A
7177 rxsize -= iosize;
7178 if (txsize)
7179 break;
1c79356b 7180 }
2d21ac55 7181 } while (!error && (txsize || rxsize));
1c79356b 7182
b0d623f7 7183 restart = 0;
1c79356b 7184
2d21ac55 7185 if (error) {
b0d623f7 7186cancel:
2d21ac55
A
7187 /* cancel any outstanding requests */
7188 while (req[nextwait]) {
7189 nfs_request_async_cancel(req[nextwait]);
7190 req[nextwait] = NULL;
7191 nextwait = (nextwait + 1) % MAXPAGINGREQS;
7192 }
6d2010ae
A
7193 if (np->n_flag & NREVOKE) {
7194 error = EIO;
7195 } else if (restart) {
7196 if (restart <= nfs_mount_state_max_restarts(nmp)) { /* guard against no progress */
7197 if (error == NFSERR_GRACE)
7198 tsleep(&nmp->nm_state, (PZERO-1), "nfsgrace", 2*hz);
7199 if (!(error = nfs_mount_state_wait_for_recovery(nmp)))
7200 goto tryagain;
7201 } else {
7202 NP(np, "nfs_pagein: too many restarts, aborting");
7203 }
b0d623f7 7204 }
2d21ac55
A
7205 }
7206
b0d623f7
A
7207 ubc_upl_unmap(pl);
7208
fa4905b1 7209 if (!nofreeupl) {
2d21ac55
A
7210 if (error)
7211 ubc_upl_abort_range(pl, pl_offset, size,
fa4905b1
A
7212 UPL_ABORT_ERROR |
7213 UPL_ABORT_FREE_ON_EMPTY);
1c79356b 7214 else
0b4e3aa0 7215 ubc_upl_commit_range(pl, pl_offset, size,
fa4905b1
A
7216 UPL_COMMIT_CLEAR_DIRTY |
7217 UPL_COMMIT_FREE_ON_EMPTY);
1c79356b 7218 }
1c79356b
A
7219 return (error);
7220}
7221
0b4e3aa0 7222
1c79356b 7223/*
2d21ac55
A
7224 * the following are needed only by nfs_pageout to know how to handle errors
7225 * see nfs_pageout comments on explanation of actions.
7226 * the errors here are copied from errno.h and errors returned by servers
7227 * are expected to match the same numbers here. If not, our actions maybe
7228 * erroneous.
7229 */
b0d623f7 7230char nfs_pageouterrorhandler(int);
39236c6e 7231enum actiontype {NOACTION, DUMP, DUMPANDLOG, RETRY, SEVER};
2d21ac55
A
7232#define NFS_ELAST 88
7233static u_char errorcount[NFS_ELAST+1]; /* better be zeros when initialized */
7234static const char errortooutcome[NFS_ELAST+1] = {
7235 NOACTION,
7236 DUMP, /* EPERM 1 Operation not permitted */
7237 DUMP, /* ENOENT 2 No such file or directory */
7238 DUMPANDLOG, /* ESRCH 3 No such process */
7239 RETRY, /* EINTR 4 Interrupted system call */
7240 DUMP, /* EIO 5 Input/output error */
7241 DUMP, /* ENXIO 6 Device not configured */
7242 DUMPANDLOG, /* E2BIG 7 Argument list too long */
7243 DUMPANDLOG, /* ENOEXEC 8 Exec format error */
7244 DUMPANDLOG, /* EBADF 9 Bad file descriptor */
7245 DUMPANDLOG, /* ECHILD 10 No child processes */
7246 DUMPANDLOG, /* EDEADLK 11 Resource deadlock avoided - was EAGAIN */
7247 RETRY, /* ENOMEM 12 Cannot allocate memory */
7248 DUMP, /* EACCES 13 Permission denied */
7249 DUMPANDLOG, /* EFAULT 14 Bad address */
7250 DUMPANDLOG, /* ENOTBLK 15 POSIX - Block device required */
7251 RETRY, /* EBUSY 16 Device busy */
7252 DUMP, /* EEXIST 17 File exists */
7253 DUMP, /* EXDEV 18 Cross-device link */
7254 DUMP, /* ENODEV 19 Operation not supported by device */
7255 DUMP, /* ENOTDIR 20 Not a directory */
7256 DUMP, /* EISDIR 21 Is a directory */
7257 DUMP, /* EINVAL 22 Invalid argument */
7258 DUMPANDLOG, /* ENFILE 23 Too many open files in system */
7259 DUMPANDLOG, /* EMFILE 24 Too many open files */
7260 DUMPANDLOG, /* ENOTTY 25 Inappropriate ioctl for device */
7261 DUMPANDLOG, /* ETXTBSY 26 Text file busy - POSIX */
7262 DUMP, /* EFBIG 27 File too large */
7263 DUMP, /* ENOSPC 28 No space left on device */
7264 DUMPANDLOG, /* ESPIPE 29 Illegal seek */
7265 DUMP, /* EROFS 30 Read-only file system */
7266 DUMP, /* EMLINK 31 Too many links */
7267 RETRY, /* EPIPE 32 Broken pipe */
7268 /* math software */
7269 DUMPANDLOG, /* EDOM 33 Numerical argument out of domain */
7270 DUMPANDLOG, /* ERANGE 34 Result too large */
7271 RETRY, /* EAGAIN/EWOULDBLOCK 35 Resource temporarily unavailable */
7272 DUMPANDLOG, /* EINPROGRESS 36 Operation now in progress */
7273 DUMPANDLOG, /* EALREADY 37 Operation already in progress */
7274 /* ipc/network software -- argument errors */
7275 DUMPANDLOG, /* ENOTSOC 38 Socket operation on non-socket */
7276 DUMPANDLOG, /* EDESTADDRREQ 39 Destination address required */
7277 DUMPANDLOG, /* EMSGSIZE 40 Message too long */
7278 DUMPANDLOG, /* EPROTOTYPE 41 Protocol wrong type for socket */
7279 DUMPANDLOG, /* ENOPROTOOPT 42 Protocol not available */
7280 DUMPANDLOG, /* EPROTONOSUPPORT 43 Protocol not supported */
7281 DUMPANDLOG, /* ESOCKTNOSUPPORT 44 Socket type not supported */
7282 DUMPANDLOG, /* ENOTSUP 45 Operation not supported */
7283 DUMPANDLOG, /* EPFNOSUPPORT 46 Protocol family not supported */
7284 DUMPANDLOG, /* EAFNOSUPPORT 47 Address family not supported by protocol family */
7285 DUMPANDLOG, /* EADDRINUSE 48 Address already in use */
7286 DUMPANDLOG, /* EADDRNOTAVAIL 49 Can't assign requested address */
7287 /* ipc/network software -- operational errors */
7288 RETRY, /* ENETDOWN 50 Network is down */
7289 RETRY, /* ENETUNREACH 51 Network is unreachable */
7290 RETRY, /* ENETRESET 52 Network dropped connection on reset */
7291 RETRY, /* ECONNABORTED 53 Software caused connection abort */
7292 RETRY, /* ECONNRESET 54 Connection reset by peer */
7293 RETRY, /* ENOBUFS 55 No buffer space available */
7294 RETRY, /* EISCONN 56 Socket is already connected */
7295 RETRY, /* ENOTCONN 57 Socket is not connected */
7296 RETRY, /* ESHUTDOWN 58 Can't send after socket shutdown */
7297 RETRY, /* ETOOMANYREFS 59 Too many references: can't splice */
7298 RETRY, /* ETIMEDOUT 60 Operation timed out */
7299 RETRY, /* ECONNREFUSED 61 Connection refused */
7300
7301 DUMPANDLOG, /* ELOOP 62 Too many levels of symbolic links */
7302 DUMP, /* ENAMETOOLONG 63 File name too long */
7303 RETRY, /* EHOSTDOWN 64 Host is down */
7304 RETRY, /* EHOSTUNREACH 65 No route to host */
7305 DUMP, /* ENOTEMPTY 66 Directory not empty */
7306 /* quotas & mush */
7307 DUMPANDLOG, /* PROCLIM 67 Too many processes */
7308 DUMPANDLOG, /* EUSERS 68 Too many users */
7309 DUMPANDLOG, /* EDQUOT 69 Disc quota exceeded */
7310 /* Network File System */
7311 DUMP, /* ESTALE 70 Stale NFS file handle */
7312 DUMP, /* EREMOTE 71 Too many levels of remote in path */
7313 DUMPANDLOG, /* EBADRPC 72 RPC struct is bad */
7314 DUMPANDLOG, /* ERPCMISMATCH 73 RPC version wrong */
7315 DUMPANDLOG, /* EPROGUNAVAIL 74 RPC prog. not avail */
7316 DUMPANDLOG, /* EPROGMISMATCH 75 Program version wrong */
7317 DUMPANDLOG, /* EPROCUNAVAIL 76 Bad procedure for program */
7318
7319 DUMPANDLOG, /* ENOLCK 77 No locks available */
7320 DUMPANDLOG, /* ENOSYS 78 Function not implemented */
7321 DUMPANDLOG, /* EFTYPE 79 Inappropriate file type or format */
7322 DUMPANDLOG, /* EAUTH 80 Authentication error */
7323 DUMPANDLOG, /* ENEEDAUTH 81 Need authenticator */
7324 /* Intelligent device errors */
7325 DUMPANDLOG, /* EPWROFF 82 Device power is off */
7326 DUMPANDLOG, /* EDEVERR 83 Device error, e.g. paper out */
7327 DUMPANDLOG, /* EOVERFLOW 84 Value too large to be stored in data type */
7328 /* Program loading errors */
7329 DUMPANDLOG, /* EBADEXEC 85 Bad executable */
7330 DUMPANDLOG, /* EBADARCH 86 Bad CPU type in executable */
7331 DUMPANDLOG, /* ESHLIBVERS 87 Shared library version mismatch */
7332 DUMPANDLOG, /* EBADMACHO 88 Malformed Macho file */
7333};
7334
b0d623f7 7335char
2d21ac55
A
7336nfs_pageouterrorhandler(int error)
7337{
7338 if (error > NFS_ELAST)
7339 return(DUMP);
7340 else
7341 return(errortooutcome[error]);
7342}
7343
7344
7345/*
7346 * vnode OP for pageout using UPL
7347 *
7348 * No buffer I/O, just RPCs straight from the mapped pages.
1c79356b
A
7349 * File size changes are not permitted in pageout.
7350 */
b0d623f7 7351int
2d21ac55 7352nfs_vnop_pageout(
91447636
A
7353 struct vnop_pageout_args /* {
7354 struct vnodeop_desc *a_desc;
7355 vnode_t a_vp;
7356 upl_t a_pl;
7357 vm_offset_t a_pl_offset;
7358 off_t a_f_offset;
7359 size_t a_size;
7360 int a_flags;
7361 vfs_context_t a_context;
2d21ac55 7362 } */ *ap)
1c79356b 7363{
91447636 7364 vnode_t vp = ap->a_vp;
1c79356b 7365 upl_t pl = ap->a_pl;
2d21ac55 7366 size_t size = ap->a_size;
1c79356b
A
7367 off_t f_offset = ap->a_f_offset;
7368 vm_offset_t pl_offset = ap->a_pl_offset;
2d21ac55
A
7369 int flags = ap->a_flags;
7370 nfsnode_t np = VTONFS(vp);
7371 thread_t thd;
91447636 7372 kauth_cred_t cred;
55e303ae 7373 struct nfsbuf *bp;
2d21ac55 7374 struct nfsmount *nmp = VTONMP(vp);
91447636 7375 daddr64_t lbn;
8f6c56a5 7376 int error = 0, iomode;
2d21ac55
A
7377 off_t off, txoffset, rxoffset;
7378 vm_offset_t ioaddr, txaddr, rxaddr;
b0d623f7
A
7379 uio_t auio;
7380 char uio_buf [ UIO_SIZEOF(1) ];
fa4905b1 7381 int nofreeupl = flags & UPL_NOCOMMIT;
2d21ac55
A
7382 size_t nmwsize, biosize, iosize, pgsize, txsize, rxsize, xsize, remsize;
7383 struct nfsreq *req[MAXPAGINGREQS];
b0d623f7 7384 int nextsend, nextwait, wverfset, commit;
2d21ac55 7385 uint64_t wverf, wverf2;
b0d623f7
A
7386 uint32_t stategenid = 0, vrestart = 0, restart = 0, vrestarts = 0, restarts = 0;
7387 kern_return_t kret;
1c79356b 7388
fa4905b1
A
7389 FSDBG(323, f_offset, size, pl, pl_offset);
7390
7391 if (pl == (upl_t)NULL)
7392 panic("nfs_pageout: no upl");
1c79356b 7393
fa4905b1 7394 if (size <= 0) {
2d21ac55 7395 printf("nfs_pageout: invalid size %ld", size);
fa4905b1 7396 if (!nofreeupl)
316670eb 7397 ubc_upl_abort_range(pl, pl_offset, size, 0);
1c79356b 7398 return (EINVAL);
1c79356b
A
7399 }
7400
55e303ae
A
7401 if (!nmp) {
7402 if (!nofreeupl)
7403 ubc_upl_abort(pl, UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY);
7404 return (ENXIO);
7405 }
0c530ab8 7406 biosize = nmp->nm_biosize;
2d21ac55
A
7407 nmwsize = nmp->nm_wsize;
7408
b0d623f7 7409 nfs_data_lock_noupdate(np, NFS_DATA_LOCK_SHARED);
1c79356b 7410
1c79356b 7411 /*
55e303ae
A
7412 * Check to see whether the buffer is incore.
7413 * If incore and not busy, invalidate it from the cache.
1c79356b 7414 */
55e303ae
A
7415 for (iosize = 0; iosize < size; iosize += xsize) {
7416 off = f_offset + iosize;
7417 /* need make sure we do things on block boundaries */
7418 xsize = biosize - (off % biosize);
7419 if (off + xsize > f_offset + size)
7420 xsize = f_offset + size - off;
2d21ac55 7421 lbn = (daddr64_t)(off / biosize);
91447636 7422 lck_mtx_lock(nfs_buf_mutex);
2d21ac55 7423 if ((bp = nfs_buf_incore(np, lbn))) {
91447636
A
7424 FSDBG(323, off, bp, bp->nb_lflags, bp->nb_flags);
7425 if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) {
7426 lck_mtx_unlock(nfs_buf_mutex);
b0d623f7 7427 nfs_data_unlock_noupdate(np);
fa4905b1
A
7428 /* no panic. just tell vm we are busy */
7429 if (!nofreeupl)
316670eb 7430 ubc_upl_abort_range(pl, pl_offset, size, 0);
55e303ae
A
7431 return (EBUSY);
7432 }
7433 if (bp->nb_dirtyend > 0) {
7434 /*
91447636
A
7435 * if there's a dirty range in the buffer, check
7436 * to see if it extends beyond the pageout region
55e303ae
A
7437 *
7438 * if the dirty region lies completely within the
7439 * pageout region, we just invalidate the buffer
7440 * because it's all being written out now anyway.
7441 *
7442 * if any of the dirty region lies outside the
7443 * pageout region, we'll try to clip the dirty
7444 * region to eliminate the portion that's being
7445 * paged out. If that's not possible, because
7446 * the dirty region extends before and after the
7447 * pageout region, then we'll just return EBUSY.
7448 */
7449 off_t boff, start, end;
7450 boff = NBOFF(bp);
7451 start = off;
7452 end = off + xsize;
7453 /* clip end to EOF */
91447636 7454 if (end > (off_t)np->n_size)
55e303ae
A
7455 end = np->n_size;
7456 start -= boff;
7457 end -= boff;
7458 if ((bp->nb_dirtyoff < start) &&
7459 (bp->nb_dirtyend > end)) {
b0d623f7
A
7460 /*
7461 * not gonna be able to clip the dirty region
7462 *
7463 * But before returning the bad news, move the
7464 * buffer to the start of the delwri list and
7465 * give the list a push to try to flush the
7466 * buffer out.
7467 */
2d21ac55 7468 FSDBG(323, np, bp, 0xd00deebc, EBUSY);
b0d623f7
A
7469 nfs_buf_remfree(bp);
7470 TAILQ_INSERT_HEAD(&nfsbufdelwri, bp, nb_free);
7471 nfsbufdelwricnt++;
91447636 7472 nfs_buf_drop(bp);
b0d623f7 7473 nfs_buf_delwri_push(1);
91447636 7474 lck_mtx_unlock(nfs_buf_mutex);
b0d623f7 7475 nfs_data_unlock_noupdate(np);
55e303ae 7476 if (!nofreeupl)
316670eb 7477 ubc_upl_abort_range(pl, pl_offset, size, 0);
55e303ae
A
7478 return (EBUSY);
7479 }
7480 if ((bp->nb_dirtyoff < start) ||
7481 (bp->nb_dirtyend > end)) {
7482 /* clip dirty region, if necessary */
7483 if (bp->nb_dirtyoff < start)
7484 bp->nb_dirtyend = min(bp->nb_dirtyend, start);
7485 if (bp->nb_dirtyend > end)
7486 bp->nb_dirtyoff = max(bp->nb_dirtyoff, end);
7487 FSDBG(323, bp, bp->nb_dirtyoff, bp->nb_dirtyend, 0xd00dee00);
7488 /* we're leaving this block dirty */
91447636
A
7489 nfs_buf_drop(bp);
7490 lck_mtx_unlock(nfs_buf_mutex);
55e303ae
A
7491 continue;
7492 }
7493 }
7494 nfs_buf_remfree(bp);
91447636
A
7495 lck_mtx_unlock(nfs_buf_mutex);
7496 SET(bp->nb_flags, NB_INVAL);
b0d623f7 7497 nfs_node_lock_force(np);
55e303ae
A
7498 if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
7499 CLR(bp->nb_flags, NB_NEEDCOMMIT);
7500 np->n_needcommitcnt--;
7501 CHECK_NEEDCOMMITCNT(np);
fa4905b1 7502 }
b0d623f7 7503 nfs_node_unlock(np);
483a1d10 7504 nfs_buf_release(bp, 1);
91447636
A
7505 } else {
7506 lck_mtx_unlock(nfs_buf_mutex);
1c79356b 7507 }
1c79356b
A
7508 }
7509
2d21ac55 7510 thd = vfs_context_thread(ap->a_context);
1c79356b 7511 cred = ubc_getcred(vp);
0c530ab8 7512 if (!IS_VALID_CRED(cred))
91447636 7513 cred = vfs_context_ucred(ap->a_context);
1c79356b 7514
b0d623f7 7515 nfs_node_lock_force(np);
1c79356b 7516 if (np->n_flag & NWRITEERR) {
2d21ac55 7517 error = np->n_error;
b0d623f7
A
7518 nfs_node_unlock(np);
7519 nfs_data_unlock_noupdate(np);
fa4905b1
A
7520 if (!nofreeupl)
7521 ubc_upl_abort_range(pl, pl_offset, size,
7522 UPL_ABORT_FREE_ON_EMPTY);
2d21ac55 7523 return (error);
1c79356b 7524 }
b0d623f7 7525 nfs_node_unlock(np);
1c79356b 7526
91447636 7527 if (f_offset < 0 || f_offset >= (off_t)np->n_size ||
55e303ae 7528 f_offset & PAGE_MASK_64 || size & PAGE_MASK_64) {
b0d623f7 7529 nfs_data_unlock_noupdate(np);
fa4905b1
A
7530 if (!nofreeupl)
7531 ubc_upl_abort_range(pl, pl_offset, size,
7532 UPL_ABORT_FREE_ON_EMPTY);
1c79356b
A
7533 return (EINVAL);
7534 }
7535
b0d623f7
A
7536 kret = ubc_upl_map(pl, &ioaddr);
7537 if (kret != KERN_SUCCESS)
7538 panic("nfs_vnop_pageout: ubc_upl_map() failed with (%d)", kret);
55e303ae 7539 ioaddr += pl_offset;
1c79356b 7540
91447636 7541 if ((u_quad_t)f_offset + size > np->n_size)
55e303ae 7542 xsize = np->n_size - f_offset;
1c79356b 7543 else
55e303ae 7544 xsize = size;
1c79356b 7545
55e303ae 7546 pgsize = round_page_64(xsize);
2d21ac55
A
7547 if ((size > pgsize) && !nofreeupl)
7548 ubc_upl_abort_range(pl, pl_offset + pgsize, size - pgsize,
7549 UPL_ABORT_FREE_ON_EMPTY);
1c79356b 7550
2d21ac55 7551 /*
1c79356b
A
7552 * check for partial page and clear the
7553 * contents past end of the file before
7554 * releasing it in the VM page cache
7555 */
91447636 7556 if ((u_quad_t)f_offset < np->n_size && (u_quad_t)f_offset + size > np->n_size) {
1c79356b 7557 size_t io = np->n_size - f_offset;
55e303ae 7558 bzero((caddr_t)(ioaddr + io), size - io);
fa4905b1 7559 FSDBG(321, np->n_size, f_offset, f_offset + io, size - io);
1c79356b 7560 }
b0d623f7 7561 nfs_data_unlock_noupdate(np);
1c79356b 7562
b0d623f7
A
7563 auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_WRITE,
7564 &uio_buf, sizeof(uio_buf));
55e303ae 7565
2d21ac55 7566tryagain:
b0d623f7
A
7567 if (nmp->nm_vers >= NFS_VER4)
7568 stategenid = nmp->nm_stategenid;
2d21ac55
A
7569 wverf = wverf2 = wverfset = 0;
7570 txsize = rxsize = xsize;
7571 txoffset = rxoffset = f_offset;
7572 txaddr = rxaddr = ioaddr;
7573 commit = NFS_WRITE_FILESYNC;
7574
7575 bzero(req, sizeof(req));
7576 nextsend = nextwait = 0;
1c79356b 7577 do {
6d2010ae
A
7578 if (np->n_flag & NREVOKE) {
7579 error = EIO;
7580 break;
7581 }
2d21ac55
A
7582 /* send requests while we need to and have available slots */
7583 while ((txsize > 0) && (req[nextsend] == NULL)) {
7584 iosize = MIN(nmwsize, txsize);
b0d623f7
A
7585 uio_reset(auio, txoffset, UIO_SYSSPACE, UIO_WRITE);
7586 uio_addiov(auio, CAST_USER_ADDR_T(txaddr), iosize);
7587 FSDBG(323, uio_offset(auio), iosize, txaddr, txsize);
316670eb 7588 OSAddAtomic64(1, &nfsstats.pageouts);
b0d623f7
A
7589 nfs_node_lock_force(np);
7590 np->n_numoutput++;
7591 nfs_node_unlock(np);
2d21ac55
A
7592 vnode_startwrite(vp);
7593 iomode = NFS_WRITE_UNSTABLE;
b0d623f7 7594 if ((error = nmp->nm_funcs->nf_write_rpc_async(np, auio, iosize, thd, cred, iomode, NULL, &req[nextsend]))) {
2d21ac55
A
7595 req[nextsend] = NULL;
7596 vnode_writedone(vp);
b0d623f7
A
7597 nfs_node_lock_force(np);
7598 np->n_numoutput--;
7599 nfs_node_unlock(np);
2d21ac55
A
7600 break;
7601 }
7602 txaddr += iosize;
7603 txoffset += iosize;
7604 txsize -= iosize;
7605 nextsend = (nextsend + 1) % MAXPAGINGREQS;
7606 }
7607 /* wait while we need to and break out if more requests to send */
7608 while ((rxsize > 0) && req[nextwait]) {
7609 iosize = remsize = MIN(nmwsize, rxsize);
7610 error = nmp->nm_funcs->nf_write_rpc_async_finish(np, req[nextwait], &iomode, &iosize, &wverf2);
7611 req[nextwait] = NULL;
7612 nextwait = (nextwait + 1) % MAXPAGINGREQS;
7613 vnode_writedone(vp);
b0d623f7
A
7614 nfs_node_lock_force(np);
7615 np->n_numoutput--;
7616 nfs_node_unlock(np);
7617 if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error)) {
7618 lck_mtx_lock(&nmp->nm_lock);
6d2010ae
A
7619 if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) {
7620 NP(np, "nfs_vnop_pageout: error %d, initiating recovery", error);
7621 nfs_need_recover(nmp, error);
b0d623f7
A
7622 }
7623 lck_mtx_unlock(&nmp->nm_lock);
b0d623f7
A
7624 restart = 1;
7625 goto cancel;
7626 }
2d21ac55
A
7627 if (error) {
7628 FSDBG(323, rxoffset, rxsize, error, -1);
7629 break;
7630 }
7631 if (!wverfset) {
7632 wverf = wverf2;
7633 wverfset = 1;
7634 } else if (wverf != wverf2) {
7635 /* verifier changed, so we need to restart all the writes */
b0d623f7 7636 vrestart = 1;
2d21ac55
A
7637 goto cancel;
7638 }
7639 /* Retain the lowest commitment level returned. */
7640 if (iomode < commit)
7641 commit = iomode;
7642 rxaddr += iosize;
7643 rxoffset += iosize;
7644 rxsize -= iosize;
7645 remsize -= iosize;
7646 if (remsize > 0) {
7647 /* need to try sending the remainder */
7648 iosize = remsize;
b0d623f7
A
7649 uio_reset(auio, rxoffset, UIO_SYSSPACE, UIO_WRITE);
7650 uio_addiov(auio, CAST_USER_ADDR_T(rxaddr), remsize);
2d21ac55 7651 iomode = NFS_WRITE_UNSTABLE;
b0d623f7
A
7652 error = nfs_write_rpc2(np, auio, thd, cred, &iomode, &wverf2);
7653 if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error)) {
6d2010ae 7654 NP(np, "nfs_vnop_pageout: restart: error %d", error);
b0d623f7 7655 lck_mtx_lock(&nmp->nm_lock);
6d2010ae
A
7656 if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) {
7657 NP(np, "nfs_vnop_pageout: error %d, initiating recovery", error);
7658 nfs_need_recover(nmp, error);
b0d623f7
A
7659 }
7660 lck_mtx_unlock(&nmp->nm_lock);
b0d623f7
A
7661 restart = 1;
7662 goto cancel;
7663 }
2d21ac55
A
7664 if (error) {
7665 FSDBG(323, rxoffset, rxsize, error, -1);
7666 break;
7667 }
7668 if (wverf != wverf2) {
7669 /* verifier changed, so we need to restart all the writes */
b0d623f7 7670 vrestart = 1;
2d21ac55
A
7671 goto cancel;
7672 }
7673 if (iomode < commit)
7674 commit = iomode;
7675 rxaddr += iosize;
7676 rxoffset += iosize;
7677 rxsize -= iosize;
7678 }
7679 if (txsize)
7680 break;
7681 }
7682 } while (!error && (txsize || rxsize));
7683
b0d623f7 7684 vrestart = 0;
2d21ac55
A
7685
7686 if (!error && (commit != NFS_WRITE_FILESYNC)) {
6d2010ae 7687 error = nmp->nm_funcs->nf_commit_rpc(np, f_offset, xsize, cred, wverf);
2d21ac55 7688 if (error == NFSERR_STALEWRITEVERF) {
b0d623f7 7689 vrestart = 1;
2d21ac55
A
7690 error = EIO;
7691 }
7692 }
7693
7694 if (error) {
7695cancel:
7696 /* cancel any outstanding requests */
7697 while (req[nextwait]) {
7698 nfs_request_async_cancel(req[nextwait]);
7699 req[nextwait] = NULL;
7700 nextwait = (nextwait + 1) % MAXPAGINGREQS;
7701 vnode_writedone(vp);
b0d623f7
A
7702 nfs_node_lock_force(np);
7703 np->n_numoutput--;
7704 nfs_node_unlock(np);
7705 }
6d2010ae
A
7706 if (np->n_flag & NREVOKE) {
7707 error = EIO;
7708 } else {
7709 if (vrestart) {
7710 if (++vrestarts <= 100) /* guard against no progress */
7711 goto tryagain;
7712 NP(np, "nfs_pageout: too many restarts, aborting");
7713 FSDBG(323, f_offset, xsize, ERESTART, -1);
7714 }
7715 if (restart) {
7716 if (restarts <= nfs_mount_state_max_restarts(nmp)) { /* guard against no progress */
7717 if (error == NFSERR_GRACE)
7718 tsleep(&nmp->nm_state, (PZERO-1), "nfsgrace", 2*hz);
7719 if (!(error = nfs_mount_state_wait_for_recovery(nmp)))
7720 goto tryagain;
7721 } else {
7722 NP(np, "nfs_pageout: too many restarts, aborting");
7723 FSDBG(323, f_offset, xsize, ERESTART, -1);
7724 }
7725 }
2d21ac55
A
7726 }
7727 }
7728
0b4e3aa0 7729 ubc_upl_unmap(pl);
2d21ac55 7730
0b4e3aa0
A
7731 /*
7732 * We've had several different solutions on what to do when the pageout
2d21ac55
A
7733 * gets an error. If we don't handle it, and return an error to the
7734 * caller, vm, it will retry . This can end in endless looping
0b4e3aa0
A
7735 * between vm and here doing retries of the same page. Doing a dump
7736 * back to vm, will get it out of vm's knowledge and we lose whatever
7737 * data existed. This is risky, but in some cases necessary. For
7738 * example, the initial fix here was to do that for ESTALE. In that case
2d21ac55
A
7739 * the server is telling us that the file is no longer the same. We
7740 * would not want to keep paging out to that. We also saw some 151
0b4e3aa0 7741 * errors from Auspex server and NFSv3 can return errors higher than
fa4905b1
A
7742 * ELAST. Those along with NFS known server errors we will "dump" from
7743 * vm. Errors we don't expect to occur, we dump and log for further
0b4e3aa0
A
7744 * analysis. Errors that could be transient, networking ones,
7745 * we let vm "retry". Lastly, errors that we retry, but may have potential
7746 * to storm the network, we "retrywithsleep". "sever" will be used in
7747 * in the future to dump all pages of object for cases like ESTALE.
7748 * All this is the basis for the states returned and first guesses on
7749 * error handling. Tweaking expected as more statistics are gathered.
7750 * Note, in the long run we may need another more robust solution to
7751 * have some kind of persistant store when the vm cannot dump nor keep
fa4905b1 7752 * retrying as a solution, but this would be a file architectural change
0b4e3aa0 7753 */
fa4905b1 7754 if (!nofreeupl) { /* otherwise stacked file system has to handle this */
0b4e3aa0 7755 if (error) {
2d21ac55
A
7756 int abortflags = 0;
7757 char action = nfs_pageouterrorhandler(error);
0b4e3aa0
A
7758
7759 switch (action) {
7760 case DUMP:
7761 abortflags = UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY;
7762 break;
7763 case DUMPANDLOG:
7764 abortflags = UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY;
2d21ac55
A
7765 if (error <= NFS_ELAST) {
7766 if ((errorcount[error] % 100) == 0)
6d2010ae 7767 NP(np, "nfs_pageout: unexpected error %d. dumping vm page", error);
2d21ac55
A
7768 errorcount[error]++;
7769 }
0b4e3aa0
A
7770 break;
7771 case RETRY:
7772 abortflags = UPL_ABORT_FREE_ON_EMPTY;
7773 break;
0b4e3aa0
A
7774 case SEVER: /* not implemented */
7775 default:
6d2010ae 7776 NP(np, "nfs_pageout: action %d not expected", action);
0b4e3aa0
A
7777 break;
7778 }
2d21ac55
A
7779
7780 ubc_upl_abort_range(pl, pl_offset, pgsize, abortflags);
0b4e3aa0 7781 /* return error in all cases above */
2d21ac55
A
7782
7783 } else {
0b4e3aa0 7784 ubc_upl_commit_range(pl, pl_offset, pgsize,
fa4905b1
A
7785 UPL_COMMIT_CLEAR_DIRTY |
7786 UPL_COMMIT_FREE_ON_EMPTY);
2d21ac55 7787 }
1c79356b 7788 }
1c79356b
A
7789 return (error);
7790}
7791
7792/* Blktooff derives file offset given a logical block number */
b0d623f7 7793int
2d21ac55 7794nfs_vnop_blktooff(
91447636
A
7795 struct vnop_blktooff_args /* {
7796 struct vnodeop_desc *a_desc;
7797 vnode_t a_vp;
7798 daddr64_t a_lblkno;
7799 off_t *a_offset;
2d21ac55 7800 } */ *ap)
1c79356b
A
7801{
7802 int biosize;
91447636 7803 vnode_t vp = ap->a_vp;
2d21ac55 7804 struct nfsmount *nmp = VTONMP(vp);
1c79356b 7805
fe8ab488 7806 if (nfs_mount_gone(nmp))
55e303ae 7807 return (ENXIO);
0c530ab8 7808 biosize = nmp->nm_biosize;
1c79356b 7809
91447636 7810 *ap->a_offset = (off_t)(ap->a_lblkno * biosize);
1c79356b
A
7811
7812 return (0);
7813}
7814
b0d623f7 7815int
2d21ac55 7816nfs_vnop_offtoblk(
91447636
A
7817 struct vnop_offtoblk_args /* {
7818 struct vnodeop_desc *a_desc;
7819 vnode_t a_vp;
7820 off_t a_offset;
7821 daddr64_t *a_lblkno;
2d21ac55 7822 } */ *ap)
1c79356b
A
7823{
7824 int biosize;
91447636 7825 vnode_t vp = ap->a_vp;
2d21ac55 7826 struct nfsmount *nmp = VTONMP(vp);
1c79356b 7827
fe8ab488 7828 if (nfs_mount_gone(nmp))
55e303ae 7829 return (ENXIO);
0c530ab8 7830 biosize = nmp->nm_biosize;
1c79356b 7831
91447636 7832 *ap->a_lblkno = (daddr64_t)(ap->a_offset / biosize);
1c79356b
A
7833
7834 return (0);
7835}
91447636 7836
6d2010ae
A
7837/*
7838 * vnode change monitoring
7839 */
7840int
7841nfs_vnop_monitor(
7842 struct vnop_monitor_args /* {
7843 struct vnodeop_desc *a_desc;
7844 vnode_t a_vp;
7845 uint32_t a_events;
7846 uint32_t a_flags;
7847 void *a_handle;
7848 vfs_context_t a_context;
7849 } */ *ap)
7850{
7851 nfsnode_t np = VTONFS(ap->a_vp);
7852 struct nfsmount *nmp = VTONMP(ap->a_vp);
7853 int error = 0;
7854
fe8ab488 7855 if (nfs_mount_gone(nmp))
6d2010ae
A
7856 return (ENXIO);
7857
7858 /* make sure that the vnode's monitoring status is up to date */
7859 lck_mtx_lock(&nmp->nm_lock);
7860 if (vnode_ismonitored(ap->a_vp)) {
7861 /* This vnode is currently being monitored, make sure we're tracking it. */
7862 if (np->n_monlink.le_next == NFSNOLIST) {
7863 LIST_INSERT_HEAD(&nmp->nm_monlist, np, n_monlink);
7864 nfs_mount_sock_thread_wake(nmp);
7865 }
7866 } else {
7867 /* This vnode is no longer being monitored, make sure we're not tracking it. */
7868 /* Wait for any in-progress getattr to complete first. */
7869 while (np->n_mflag & NMMONSCANINPROG) {
7870 struct timespec ts = { 1, 0 };
7871 np->n_mflag |= NMMONSCANWANT;
7872 msleep(&np->n_mflag, &nmp->nm_lock, PZERO-1, "nfswaitmonscan", &ts);
7873 }
7874 if (np->n_monlink.le_next != NFSNOLIST) {
7875 LIST_REMOVE(np, n_monlink);
7876 np->n_monlink.le_next = NFSNOLIST;
7877 }
7878 }
7879 lck_mtx_unlock(&nmp->nm_lock);
7880
7881 return (error);
7882}
7883
7884/*
7885 * Send a vnode notification for the given events.
7886 */
7887void
7888nfs_vnode_notify(nfsnode_t np, uint32_t events)
7889{
7890 struct nfsmount *nmp = NFSTONMP(np);
7891 struct nfs_vattr nvattr;
7892 struct vnode_attr vattr, *vap = NULL;
7893 struct timeval now;
7894
7895 microuptime(&now);
7896 if ((np->n_evtstamp == now.tv_sec) || !nmp) {
7897 /* delay sending this notify */
7898 np->n_events |= events;
7899 return;
7900 }
7901 events |= np->n_events;
7902 np->n_events = 0;
7903 np->n_evtstamp = now.tv_sec;
7904
7905 vfs_get_notify_attributes(&vattr);
7906 if (!nfs_getattrcache(np, &nvattr, 0)) {
7907 vap = &vattr;
7908 VATTR_INIT(vap);
7909 VATTR_RETURN(vap, va_fsid, vfs_statfs(nmp->nm_mountp)->f_fsid.val[0]);
7910 VATTR_RETURN(vap, va_fileid, nvattr.nva_fileid);
7911 VATTR_RETURN(vap, va_mode, nvattr.nva_mode);
7912 VATTR_RETURN(vap, va_uid, nvattr.nva_uid);
7913 VATTR_RETURN(vap, va_gid, nvattr.nva_gid);
7914 VATTR_RETURN(vap, va_nlink, nvattr.nva_nlink);
7915 }
7916 vnode_notify(NFSTOV(np), events, vap);
7917}