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