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