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