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