2 * Copyright (c) 2000-2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
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 $
70 * vnode op calls for Sun NFS version 2 and 3
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>
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>
88 #include <sys/signalvar.h>
89 #include <sys/uio_internal.h>
91 #include <vfs/vfs_support.h>
96 #include <kern/clock.h>
97 #include <libkern/OSAtomic.h>
99 #include <miscfs/fifofs/fifo.h>
100 #include <miscfs/specfs/specdev.h>
102 #include <nfs/rpcv2.h>
103 #include <nfs/nfsproto.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>
113 #include <netinet/in.h>
114 #include <netinet/in_var.h>
116 #include <vm/vm_kern.h>
117 #include <vm/vm_pageout.h>
119 #include <kern/task.h>
120 #include <kern/sched_prim.h>
125 int nfs_vnop_lookup(struct vnop_lookup_args
*);
126 int nfsspec_vnop_read(struct vnop_read_args
*);
127 int nfsspec_vnop_write(struct vnop_write_args
*);
128 int nfsspec_vnop_close(struct vnop_close_args
*);
130 int nfsfifo_vnop_read(struct vnop_read_args
*);
131 int nfsfifo_vnop_write(struct vnop_write_args
*);
132 int nfsfifo_vnop_close(struct vnop_close_args
*);
134 int nfs_vnop_ioctl(struct vnop_ioctl_args
*);
135 int nfs_vnop_select(struct vnop_select_args
*);
136 int nfs_vnop_setattr(struct vnop_setattr_args
*);
137 int nfs_vnop_fsync(struct vnop_fsync_args
*);
138 int nfs_vnop_rename(struct vnop_rename_args
*);
139 int nfs_vnop_readdir(struct vnop_readdir_args
*);
140 int nfs_vnop_readlink(struct vnop_readlink_args
*);
141 int nfs_vnop_pathconf(struct vnop_pathconf_args
*);
142 int nfs_vnop_pagein(struct vnop_pagein_args
*);
143 int nfs_vnop_pageout(struct vnop_pageout_args
*);
144 int nfs_vnop_blktooff(struct vnop_blktooff_args
*);
145 int nfs_vnop_offtoblk(struct vnop_offtoblk_args
*);
146 int nfs_vnop_blockmap(struct vnop_blockmap_args
*);
147 int nfs_vnop_monitor(struct vnop_monitor_args
*);
149 int nfs3_vnop_create(struct vnop_create_args
*);
150 int nfs3_vnop_mknod(struct vnop_mknod_args
*);
151 int nfs3_vnop_getattr(struct vnop_getattr_args
*);
152 int nfs3_vnop_link(struct vnop_link_args
*);
153 int nfs3_vnop_mkdir(struct vnop_mkdir_args
*);
154 int nfs3_vnop_rmdir(struct vnop_rmdir_args
*);
155 int nfs3_vnop_symlink(struct vnop_symlink_args
*);
157 vnop_t
**nfsv2_vnodeop_p
;
158 static struct vnodeopv_entry_desc nfsv2_vnodeop_entries
[] = {
159 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
160 { &vnop_lookup_desc
, (vnop_t
*)nfs_vnop_lookup
}, /* lookup */
161 { &vnop_create_desc
, (vnop_t
*)nfs3_vnop_create
}, /* create */
162 { &vnop_mknod_desc
, (vnop_t
*)nfs3_vnop_mknod
}, /* mknod */
163 { &vnop_open_desc
, (vnop_t
*)nfs_vnop_open
}, /* open */
164 { &vnop_close_desc
, (vnop_t
*)nfs_vnop_close
}, /* close */
165 { &vnop_access_desc
, (vnop_t
*)nfs_vnop_access
}, /* access */
166 { &vnop_getattr_desc
, (vnop_t
*)nfs3_vnop_getattr
}, /* getattr */
167 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
168 { &vnop_read_desc
, (vnop_t
*)nfs_vnop_read
}, /* read */
169 { &vnop_write_desc
, (vnop_t
*)nfs_vnop_write
}, /* write */
170 { &vnop_ioctl_desc
, (vnop_t
*)nfs_vnop_ioctl
}, /* ioctl */
171 { &vnop_select_desc
, (vnop_t
*)nfs_vnop_select
}, /* select */
172 { &vnop_revoke_desc
, (vnop_t
*)nfs_vnop_revoke
}, /* revoke */
173 { &vnop_mmap_desc
, (vnop_t
*)nfs_vnop_mmap
}, /* mmap */
174 { &vnop_mnomap_desc
, (vnop_t
*)nfs_vnop_mnomap
}, /* mnomap */
175 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
176 { &vnop_remove_desc
, (vnop_t
*)nfs_vnop_remove
}, /* remove */
177 { &vnop_link_desc
, (vnop_t
*)nfs3_vnop_link
}, /* link */
178 { &vnop_rename_desc
, (vnop_t
*)nfs_vnop_rename
}, /* rename */
179 { &vnop_mkdir_desc
, (vnop_t
*)nfs3_vnop_mkdir
}, /* mkdir */
180 { &vnop_rmdir_desc
, (vnop_t
*)nfs3_vnop_rmdir
}, /* rmdir */
181 { &vnop_symlink_desc
, (vnop_t
*)nfs3_vnop_symlink
}, /* symlink */
182 { &vnop_readdir_desc
, (vnop_t
*)nfs_vnop_readdir
}, /* readdir */
183 { &vnop_readlink_desc
, (vnop_t
*)nfs_vnop_readlink
}, /* readlink */
184 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
185 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
186 { &vnop_strategy_desc
, (vnop_t
*)err_strategy
}, /* strategy */
187 { &vnop_pathconf_desc
, (vnop_t
*)nfs_vnop_pathconf
}, /* pathconf */
188 { &vnop_advlock_desc
, (vnop_t
*)nfs_vnop_advlock
}, /* advlock */
189 { &vnop_bwrite_desc
, (vnop_t
*)err_bwrite
}, /* bwrite */
190 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
191 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
192 { &vnop_copyfile_desc
, (vnop_t
*)err_copyfile
}, /* Copyfile */
193 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
194 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
195 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
196 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
199 struct vnodeopv_desc nfsv2_vnodeop_opv_desc
=
200 { &nfsv2_vnodeop_p
, nfsv2_vnodeop_entries
};
202 vnop_t
**nfsv4_vnodeop_p
;
203 static struct vnodeopv_entry_desc nfsv4_vnodeop_entries
[] = {
204 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
205 { &vnop_lookup_desc
, (vnop_t
*)nfs_vnop_lookup
}, /* lookup */
206 { &vnop_create_desc
, (vnop_t
*)nfs4_vnop_create
}, /* create */
207 { &vnop_mknod_desc
, (vnop_t
*)nfs4_vnop_mknod
}, /* mknod */
208 { &vnop_open_desc
, (vnop_t
*)nfs_vnop_open
}, /* open */
209 { &vnop_close_desc
, (vnop_t
*)nfs_vnop_close
}, /* close */
210 { &vnop_access_desc
, (vnop_t
*)nfs_vnop_access
}, /* access */
211 { &vnop_getattr_desc
, (vnop_t
*)nfs4_vnop_getattr
}, /* getattr */
212 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
213 { &vnop_read_desc
, (vnop_t
*)nfs_vnop_read
}, /* read */
214 { &vnop_write_desc
, (vnop_t
*)nfs_vnop_write
}, /* write */
215 { &vnop_ioctl_desc
, (vnop_t
*)nfs_vnop_ioctl
}, /* ioctl */
216 { &vnop_select_desc
, (vnop_t
*)nfs_vnop_select
}, /* select */
217 { &vnop_revoke_desc
, (vnop_t
*)nfs_vnop_revoke
}, /* revoke */
218 { &vnop_mmap_desc
, (vnop_t
*)nfs_vnop_mmap
}, /* mmap */
219 { &vnop_mnomap_desc
, (vnop_t
*)nfs_vnop_mnomap
}, /* mnomap */
220 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
221 { &vnop_remove_desc
, (vnop_t
*)nfs_vnop_remove
}, /* remove */
222 { &vnop_link_desc
, (vnop_t
*)nfs4_vnop_link
}, /* link */
223 { &vnop_rename_desc
, (vnop_t
*)nfs_vnop_rename
}, /* rename */
224 { &vnop_mkdir_desc
, (vnop_t
*)nfs4_vnop_mkdir
}, /* mkdir */
225 { &vnop_rmdir_desc
, (vnop_t
*)nfs4_vnop_rmdir
}, /* rmdir */
226 { &vnop_symlink_desc
, (vnop_t
*)nfs4_vnop_symlink
}, /* symlink */
227 { &vnop_readdir_desc
, (vnop_t
*)nfs_vnop_readdir
}, /* readdir */
228 { &vnop_readlink_desc
, (vnop_t
*)nfs_vnop_readlink
}, /* readlink */
229 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
230 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
231 { &vnop_strategy_desc
, (vnop_t
*)err_strategy
}, /* strategy */
232 { &vnop_pathconf_desc
, (vnop_t
*)nfs_vnop_pathconf
}, /* pathconf */
233 { &vnop_advlock_desc
, (vnop_t
*)nfs_vnop_advlock
}, /* advlock */
234 { &vnop_bwrite_desc
, (vnop_t
*)err_bwrite
}, /* bwrite */
235 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
236 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
237 { &vnop_copyfile_desc
, (vnop_t
*)err_copyfile
}, /* Copyfile */
238 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
239 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
240 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
241 { &vnop_getxattr_desc
, (vnop_t
*)nfs4_vnop_getxattr
}, /* getxattr */
242 { &vnop_setxattr_desc
, (vnop_t
*)nfs4_vnop_setxattr
}, /* setxattr */
243 { &vnop_removexattr_desc
, (vnop_t
*)nfs4_vnop_removexattr
},/* removexattr */
244 { &vnop_listxattr_desc
, (vnop_t
*)nfs4_vnop_listxattr
},/* listxattr */
246 { &vnop_getnamedstream_desc
, (vnop_t
*)nfs4_vnop_getnamedstream
}, /* getnamedstream */
247 { &vnop_makenamedstream_desc
, (vnop_t
*)nfs4_vnop_makenamedstream
}, /* makenamedstream */
248 { &vnop_removenamedstream_desc
, (vnop_t
*)nfs4_vnop_removenamedstream
},/* removenamedstream */
250 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
253 struct vnodeopv_desc nfsv4_vnodeop_opv_desc
=
254 { &nfsv4_vnodeop_p
, nfsv4_vnodeop_entries
};
257 * Special device vnode ops
259 vnop_t
**spec_nfsv2nodeop_p
;
260 static struct vnodeopv_entry_desc spec_nfsv2nodeop_entries
[] = {
261 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
262 { &vnop_lookup_desc
, (vnop_t
*)spec_lookup
}, /* lookup */
263 { &vnop_create_desc
, (vnop_t
*)spec_create
}, /* create */
264 { &vnop_mknod_desc
, (vnop_t
*)spec_mknod
}, /* mknod */
265 { &vnop_open_desc
, (vnop_t
*)spec_open
}, /* open */
266 { &vnop_close_desc
, (vnop_t
*)nfsspec_vnop_close
}, /* close */
267 { &vnop_getattr_desc
, (vnop_t
*)nfs3_vnop_getattr
}, /* getattr */
268 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
269 { &vnop_read_desc
, (vnop_t
*)nfsspec_vnop_read
}, /* read */
270 { &vnop_write_desc
, (vnop_t
*)nfsspec_vnop_write
}, /* write */
271 { &vnop_ioctl_desc
, (vnop_t
*)spec_ioctl
}, /* ioctl */
272 { &vnop_select_desc
, (vnop_t
*)spec_select
}, /* select */
273 { &vnop_revoke_desc
, (vnop_t
*)spec_revoke
}, /* revoke */
274 { &vnop_mmap_desc
, (vnop_t
*)spec_mmap
}, /* mmap */
275 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
276 { &vnop_remove_desc
, (vnop_t
*)spec_remove
}, /* remove */
277 { &vnop_link_desc
, (vnop_t
*)spec_link
}, /* link */
278 { &vnop_rename_desc
, (vnop_t
*)spec_rename
}, /* rename */
279 { &vnop_mkdir_desc
, (vnop_t
*)spec_mkdir
}, /* mkdir */
280 { &vnop_rmdir_desc
, (vnop_t
*)spec_rmdir
}, /* rmdir */
281 { &vnop_symlink_desc
, (vnop_t
*)spec_symlink
}, /* symlink */
282 { &vnop_readdir_desc
, (vnop_t
*)spec_readdir
}, /* readdir */
283 { &vnop_readlink_desc
, (vnop_t
*)spec_readlink
}, /* readlink */
284 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
285 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
286 { &vnop_strategy_desc
, (vnop_t
*)spec_strategy
}, /* strategy */
287 { &vnop_pathconf_desc
, (vnop_t
*)spec_pathconf
}, /* pathconf */
288 { &vnop_advlock_desc
, (vnop_t
*)spec_advlock
}, /* advlock */
289 { &vnop_bwrite_desc
, (vnop_t
*)vn_bwrite
}, /* bwrite */
290 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
291 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
292 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
293 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
294 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
295 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
298 struct vnodeopv_desc spec_nfsv2nodeop_opv_desc
=
299 { &spec_nfsv2nodeop_p
, spec_nfsv2nodeop_entries
};
300 vnop_t
**spec_nfsv4nodeop_p
;
301 static struct vnodeopv_entry_desc spec_nfsv4nodeop_entries
[] = {
302 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
303 { &vnop_lookup_desc
, (vnop_t
*)spec_lookup
}, /* lookup */
304 { &vnop_create_desc
, (vnop_t
*)spec_create
}, /* create */
305 { &vnop_mknod_desc
, (vnop_t
*)spec_mknod
}, /* mknod */
306 { &vnop_open_desc
, (vnop_t
*)spec_open
}, /* open */
307 { &vnop_close_desc
, (vnop_t
*)nfsspec_vnop_close
}, /* close */
308 { &vnop_getattr_desc
, (vnop_t
*)nfs4_vnop_getattr
}, /* getattr */
309 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
310 { &vnop_read_desc
, (vnop_t
*)nfsspec_vnop_read
}, /* read */
311 { &vnop_write_desc
, (vnop_t
*)nfsspec_vnop_write
}, /* write */
312 { &vnop_ioctl_desc
, (vnop_t
*)spec_ioctl
}, /* ioctl */
313 { &vnop_select_desc
, (vnop_t
*)spec_select
}, /* select */
314 { &vnop_revoke_desc
, (vnop_t
*)spec_revoke
}, /* revoke */
315 { &vnop_mmap_desc
, (vnop_t
*)spec_mmap
}, /* mmap */
316 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
317 { &vnop_remove_desc
, (vnop_t
*)spec_remove
}, /* remove */
318 { &vnop_link_desc
, (vnop_t
*)spec_link
}, /* link */
319 { &vnop_rename_desc
, (vnop_t
*)spec_rename
}, /* rename */
320 { &vnop_mkdir_desc
, (vnop_t
*)spec_mkdir
}, /* mkdir */
321 { &vnop_rmdir_desc
, (vnop_t
*)spec_rmdir
}, /* rmdir */
322 { &vnop_symlink_desc
, (vnop_t
*)spec_symlink
}, /* symlink */
323 { &vnop_readdir_desc
, (vnop_t
*)spec_readdir
}, /* readdir */
324 { &vnop_readlink_desc
, (vnop_t
*)spec_readlink
}, /* readlink */
325 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
326 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
327 { &vnop_strategy_desc
, (vnop_t
*)spec_strategy
}, /* strategy */
328 { &vnop_pathconf_desc
, (vnop_t
*)spec_pathconf
}, /* pathconf */
329 { &vnop_advlock_desc
, (vnop_t
*)spec_advlock
}, /* advlock */
330 { &vnop_bwrite_desc
, (vnop_t
*)vn_bwrite
}, /* bwrite */
331 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
332 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
333 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
334 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
335 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
336 { &vnop_getxattr_desc
, (vnop_t
*)nfs4_vnop_getxattr
}, /* getxattr */
337 { &vnop_setxattr_desc
, (vnop_t
*)nfs4_vnop_setxattr
}, /* setxattr */
338 { &vnop_removexattr_desc
, (vnop_t
*)nfs4_vnop_removexattr
},/* removexattr */
339 { &vnop_listxattr_desc
, (vnop_t
*)nfs4_vnop_listxattr
},/* listxattr */
341 { &vnop_getnamedstream_desc
, (vnop_t
*)nfs4_vnop_getnamedstream
}, /* getnamedstream */
342 { &vnop_makenamedstream_desc
, (vnop_t
*)nfs4_vnop_makenamedstream
}, /* makenamedstream */
343 { &vnop_removenamedstream_desc
, (vnop_t
*)nfs4_vnop_removenamedstream
},/* removenamedstream */
345 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
348 struct vnodeopv_desc spec_nfsv4nodeop_opv_desc
=
349 { &spec_nfsv4nodeop_p
, spec_nfsv4nodeop_entries
};
352 vnop_t
**fifo_nfsv2nodeop_p
;
353 static struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries
[] = {
354 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
355 { &vnop_lookup_desc
, (vnop_t
*)fifo_lookup
}, /* lookup */
356 { &vnop_create_desc
, (vnop_t
*)fifo_create
}, /* create */
357 { &vnop_mknod_desc
, (vnop_t
*)fifo_mknod
}, /* mknod */
358 { &vnop_open_desc
, (vnop_t
*)fifo_open
}, /* open */
359 { &vnop_close_desc
, (vnop_t
*)nfsfifo_vnop_close
}, /* close */
360 { &vnop_getattr_desc
, (vnop_t
*)nfs3_vnop_getattr
}, /* getattr */
361 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
362 { &vnop_read_desc
, (vnop_t
*)nfsfifo_vnop_read
}, /* read */
363 { &vnop_write_desc
, (vnop_t
*)nfsfifo_vnop_write
}, /* write */
364 { &vnop_ioctl_desc
, (vnop_t
*)fifo_ioctl
}, /* ioctl */
365 { &vnop_select_desc
, (vnop_t
*)fifo_select
}, /* select */
366 { &vnop_revoke_desc
, (vnop_t
*)fifo_revoke
}, /* revoke */
367 { &vnop_mmap_desc
, (vnop_t
*)fifo_mmap
}, /* mmap */
368 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
369 { &vnop_remove_desc
, (vnop_t
*)fifo_remove
}, /* remove */
370 { &vnop_link_desc
, (vnop_t
*)fifo_link
}, /* link */
371 { &vnop_rename_desc
, (vnop_t
*)fifo_rename
}, /* rename */
372 { &vnop_mkdir_desc
, (vnop_t
*)fifo_mkdir
}, /* mkdir */
373 { &vnop_rmdir_desc
, (vnop_t
*)fifo_rmdir
}, /* rmdir */
374 { &vnop_symlink_desc
, (vnop_t
*)fifo_symlink
}, /* symlink */
375 { &vnop_readdir_desc
, (vnop_t
*)fifo_readdir
}, /* readdir */
376 { &vnop_readlink_desc
, (vnop_t
*)fifo_readlink
}, /* readlink */
377 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
378 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
379 { &vnop_strategy_desc
, (vnop_t
*)fifo_strategy
}, /* strategy */
380 { &vnop_pathconf_desc
, (vnop_t
*)fifo_pathconf
}, /* pathconf */
381 { &vnop_advlock_desc
, (vnop_t
*)fifo_advlock
}, /* advlock */
382 { &vnop_bwrite_desc
, (vnop_t
*)vn_bwrite
}, /* bwrite */
383 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
384 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
385 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
386 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
387 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
388 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
391 struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc
=
392 { &fifo_nfsv2nodeop_p
, fifo_nfsv2nodeop_entries
};
394 vnop_t
**fifo_nfsv4nodeop_p
;
395 static struct vnodeopv_entry_desc fifo_nfsv4nodeop_entries
[] = {
396 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
397 { &vnop_lookup_desc
, (vnop_t
*)fifo_lookup
}, /* lookup */
398 { &vnop_create_desc
, (vnop_t
*)fifo_create
}, /* create */
399 { &vnop_mknod_desc
, (vnop_t
*)fifo_mknod
}, /* mknod */
400 { &vnop_open_desc
, (vnop_t
*)fifo_open
}, /* open */
401 { &vnop_close_desc
, (vnop_t
*)nfsfifo_vnop_close
}, /* close */
402 { &vnop_getattr_desc
, (vnop_t
*)nfs4_vnop_getattr
}, /* getattr */
403 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
404 { &vnop_read_desc
, (vnop_t
*)nfsfifo_vnop_read
}, /* read */
405 { &vnop_write_desc
, (vnop_t
*)nfsfifo_vnop_write
}, /* write */
406 { &vnop_ioctl_desc
, (vnop_t
*)fifo_ioctl
}, /* ioctl */
407 { &vnop_select_desc
, (vnop_t
*)fifo_select
}, /* select */
408 { &vnop_revoke_desc
, (vnop_t
*)fifo_revoke
}, /* revoke */
409 { &vnop_mmap_desc
, (vnop_t
*)fifo_mmap
}, /* mmap */
410 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
411 { &vnop_remove_desc
, (vnop_t
*)fifo_remove
}, /* remove */
412 { &vnop_link_desc
, (vnop_t
*)fifo_link
}, /* link */
413 { &vnop_rename_desc
, (vnop_t
*)fifo_rename
}, /* rename */
414 { &vnop_mkdir_desc
, (vnop_t
*)fifo_mkdir
}, /* mkdir */
415 { &vnop_rmdir_desc
, (vnop_t
*)fifo_rmdir
}, /* rmdir */
416 { &vnop_symlink_desc
, (vnop_t
*)fifo_symlink
}, /* symlink */
417 { &vnop_readdir_desc
, (vnop_t
*)fifo_readdir
}, /* readdir */
418 { &vnop_readlink_desc
, (vnop_t
*)fifo_readlink
}, /* readlink */
419 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
420 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
421 { &vnop_strategy_desc
, (vnop_t
*)fifo_strategy
}, /* strategy */
422 { &vnop_pathconf_desc
, (vnop_t
*)fifo_pathconf
}, /* pathconf */
423 { &vnop_advlock_desc
, (vnop_t
*)fifo_advlock
}, /* advlock */
424 { &vnop_bwrite_desc
, (vnop_t
*)vn_bwrite
}, /* bwrite */
425 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
426 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
427 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
428 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
429 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
430 { &vnop_getxattr_desc
, (vnop_t
*)nfs4_vnop_getxattr
}, /* getxattr */
431 { &vnop_setxattr_desc
, (vnop_t
*)nfs4_vnop_setxattr
}, /* setxattr */
432 { &vnop_removexattr_desc
, (vnop_t
*)nfs4_vnop_removexattr
},/* removexattr */
433 { &vnop_listxattr_desc
, (vnop_t
*)nfs4_vnop_listxattr
},/* listxattr */
435 { &vnop_getnamedstream_desc
, (vnop_t
*)nfs4_vnop_getnamedstream
}, /* getnamedstream */
436 { &vnop_makenamedstream_desc
, (vnop_t
*)nfs4_vnop_makenamedstream
}, /* makenamedstream */
437 { &vnop_removenamedstream_desc
, (vnop_t
*)nfs4_vnop_removenamedstream
},/* removenamedstream */
439 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
442 struct vnodeopv_desc fifo_nfsv4nodeop_opv_desc
=
443 { &fifo_nfsv4nodeop_p
, fifo_nfsv4nodeop_entries
};
447 int nfs_sillyrename(nfsnode_t
,nfsnode_t
,struct componentname
*,vfs_context_t
);
450 * Find the slot in the access cache for this UID.
451 * If adding and no existing slot is found, reuse slots in FIFO order.
452 * The index of the next slot to use is kept in the last entry of the n_access array.
455 nfs_node_access_slot(nfsnode_t np
, uid_t uid
, int add
)
459 for (slot
=0; slot
< NFS_ACCESS_CACHE_SIZE
; slot
++)
460 if (np
->n_accessuid
[slot
] == uid
)
462 if (slot
== NFS_ACCESS_CACHE_SIZE
) {
465 slot
= np
->n_access
[NFS_ACCESS_CACHE_SIZE
];
466 np
->n_access
[NFS_ACCESS_CACHE_SIZE
] = (slot
+ 1) % NFS_ACCESS_CACHE_SIZE
;
472 nfs3_access_rpc(nfsnode_t np
, u_int32_t
*access
, vfs_context_t ctx
)
474 int error
= 0, lockerror
= ENOENT
, status
, slot
;
475 uint32_t access_result
= 0;
477 struct nfsm_chain nmreq
, nmrep
;
481 nfsm_chain_null(&nmreq
);
482 nfsm_chain_null(&nmrep
);
484 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(NFS_VER3
) + NFSX_UNSIGNED
);
485 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER3
, np
->n_fhp
, np
->n_fhsize
);
486 nfsm_chain_add_32(error
, &nmreq
, *access
);
487 nfsm_chain_build_done(error
, &nmreq
);
489 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_ACCESS
, ctx
, NULL
, &nmrep
, &xid
, &status
);
490 if ((lockerror
= nfs_node_lock(np
)))
492 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
495 nfsm_chain_get_32(error
, &nmrep
, access_result
);
498 uid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
499 slot
= nfs_node_access_slot(np
, uid
, 1);
500 np
->n_accessuid
[slot
] = uid
;
502 np
->n_accessstamp
[slot
] = now
.tv_sec
;
503 np
->n_access
[slot
] = access_result
;
506 * If we asked for DELETE but didn't get it, the server
507 * may simply not support returning that bit (possible
508 * on UNIX systems). So, we'll assume that it is OK,
509 * and just let any subsequent delete action fail if it
510 * really isn't deletable.
512 if ((*access
& NFS_ACCESS_DELETE
) &&
513 !(np
->n_access
[slot
] & NFS_ACCESS_DELETE
))
514 np
->n_access
[slot
] |= NFS_ACCESS_DELETE
;
515 /* ".zfs" subdirectories may erroneously give a denied answer for add/remove */
516 if (nfs_access_dotzfs
&& (np
->n_flag
& NISDOTZFSCHILD
))
517 np
->n_access
[slot
] |= (NFS_ACCESS_MODIFY
|NFS_ACCESS_EXTEND
|NFS_ACCESS_DELETE
);
518 /* pass back the access returned with this request */
519 *access
= np
->n_access
[slot
];
523 nfsm_chain_cleanup(&nmreq
);
524 nfsm_chain_cleanup(&nmrep
);
529 * NFS access vnode op.
530 * For NFS version 2, just return ok. File accesses may fail later.
531 * For NFS version 3+, use the access RPC to check accessibility. If file
532 * permissions are changed on the server, accesses might still fail later.
536 struct vnop_access_args
/* {
537 struct vnodeop_desc *a_desc;
540 vfs_context_t a_context;
543 vfs_context_t ctx
= ap
->a_context
;
544 vnode_t vp
= ap
->a_vp
;
545 int error
= 0, slot
, dorpc
;
546 u_int32_t access
, waccess
;
547 nfsnode_t np
= VTONFS(vp
);
548 struct nfsmount
*nmp
;
556 nfsvers
= nmp
->nm_vers
;
558 if (nfsvers
== NFS_VER2
) {
559 if ((ap
->a_action
& KAUTH_VNODE_WRITE_RIGHTS
) &&
560 vfs_isrdonly(vnode_mount(vp
)))
566 * For NFS v3, do an access rpc, otherwise you are stuck emulating
567 * ufs_access() locally using the vattr. This may not be correct,
568 * since the server may apply other access criteria such as
569 * client uid-->server uid mapping that we do not know about, but
570 * this is better than just returning anything that is lying about
575 * Convert KAUTH primitives to NFS access rights.
578 if (vnode_isdir(vp
)) {
581 (KAUTH_VNODE_LIST_DIRECTORY
|
582 KAUTH_VNODE_READ_EXTATTRIBUTES
))
583 access
|= NFS_ACCESS_READ
;
584 if (ap
->a_action
& KAUTH_VNODE_SEARCH
)
585 access
|= NFS_ACCESS_LOOKUP
;
587 (KAUTH_VNODE_ADD_FILE
|
588 KAUTH_VNODE_ADD_SUBDIRECTORY
))
589 access
|= NFS_ACCESS_MODIFY
| NFS_ACCESS_EXTEND
;
590 if (ap
->a_action
& KAUTH_VNODE_DELETE_CHILD
)
591 access
|= NFS_ACCESS_MODIFY
;
595 (KAUTH_VNODE_READ_DATA
|
596 KAUTH_VNODE_READ_EXTATTRIBUTES
))
597 access
|= NFS_ACCESS_READ
;
598 if (ap
->a_action
& KAUTH_VNODE_WRITE_DATA
)
599 access
|= NFS_ACCESS_MODIFY
| NFS_ACCESS_EXTEND
;
600 if (ap
->a_action
& KAUTH_VNODE_APPEND_DATA
)
601 access
|= NFS_ACCESS_EXTEND
;
602 if (ap
->a_action
& KAUTH_VNODE_EXECUTE
)
603 access
|= NFS_ACCESS_EXECUTE
;
606 if (ap
->a_action
& KAUTH_VNODE_DELETE
)
607 access
|= NFS_ACCESS_DELETE
;
609 (KAUTH_VNODE_WRITE_ATTRIBUTES
|
610 KAUTH_VNODE_WRITE_EXTATTRIBUTES
|
611 KAUTH_VNODE_WRITE_SECURITY
))
612 access
|= NFS_ACCESS_MODIFY
;
613 /* XXX this is pretty dubious */
614 if (ap
->a_action
& KAUTH_VNODE_CHANGE_OWNER
)
615 access
|= NFS_ACCESS_MODIFY
;
617 /* if caching, always ask for every right */
618 if (nfs_access_cache_timeout
> 0) {
619 waccess
= NFS_ACCESS_READ
| NFS_ACCESS_MODIFY
|
620 NFS_ACCESS_EXTEND
| NFS_ACCESS_EXECUTE
|
621 NFS_ACCESS_DELETE
| NFS_ACCESS_LOOKUP
;
626 if ((error
= nfs_node_lock(np
)))
630 * Does our cached result allow us to give a definite yes to
633 uid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
634 slot
= nfs_node_access_slot(np
, uid
, 0);
637 /* not asking for any rights understood by NFS, so don't bother doing an RPC */
638 /* OSAddAtomic(1, &nfsstats.accesscache_hits); */
641 } else if (NACCESSVALID(np
, slot
)) {
643 if ((now
.tv_sec
< (np
->n_accessstamp
[slot
] + nfs_access_cache_timeout
)) &&
644 ((np
->n_access
[slot
] & access
) == access
)) {
645 /* OSAddAtomic(1, &nfsstats.accesscache_hits); */
647 waccess
= np
->n_access
[slot
];
652 /* Either a no, or a don't know. Go to the wire. */
653 /* OSAddAtomic(1, &nfsstats.accesscache_misses); */
654 error
= nmp
->nm_funcs
->nf_access_rpc(np
, &waccess
, ctx
);
656 if (!error
&& ((waccess
& access
) != access
))
666 * Perform various update/invalidation checks and then add the
667 * open to the node. Regular files will have an open file structure
668 * on the node and, for NFSv4, perform an OPEN request on the server.
672 struct vnop_open_args
/* {
673 struct vnodeop_desc *a_desc;
676 vfs_context_t a_context;
679 vfs_context_t ctx
= ap
->a_context
;
680 vnode_t vp
= ap
->a_vp
;
681 nfsnode_t np
= VTONFS(vp
);
682 struct nfsmount
*nmp
;
683 int error
, accessMode
, denyMode
, opened
= 0;
684 struct nfs_open_owner
*noop
= NULL
;
685 struct nfs_open_file
*nofp
= NULL
;
688 if (!(ap
->a_mode
& (FREAD
|FWRITE
)))
694 if (np
->n_flag
& NREVOKE
)
697 vtype
= vnode_vtype(vp
);
698 if ((vtype
!= VREG
) && (vtype
!= VDIR
) && (vtype
!= VLNK
))
701 /* First, check if we need to update/invalidate */
702 if (ISSET(np
->n_flag
, NUPDATESIZE
))
703 nfs_data_update_size(np
, 0);
704 if ((error
= nfs_node_lock(np
)))
706 if (np
->n_flag
& NNEEDINVALIDATE
) {
707 np
->n_flag
&= ~NNEEDINVALIDATE
;
711 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, ctx
, 1);
712 if ((error
= nfs_node_lock(np
)))
716 np
->n_lastrahead
= -1;
717 if (np
->n_flag
& NMODIFIED
) {
721 if ((error
= nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, ctx
, 1)))
727 /* nfs_getattr() will check changed and purge caches */
728 if ((error
= nfs_getattr(np
, NULL
, ctx
, NGA_UNCACHED
)))
732 /* Just mark that it was opened */
733 lck_mtx_lock(&np
->n_openlock
);
735 lck_mtx_unlock(&np
->n_openlock
);
739 /* mode contains some combination of: FREAD, FWRITE, O_SHLOCK, O_EXLOCK */
741 if (ap
->a_mode
& FREAD
)
742 accessMode
|= NFS_OPEN_SHARE_ACCESS_READ
;
743 if (ap
->a_mode
& FWRITE
)
744 accessMode
|= NFS_OPEN_SHARE_ACCESS_WRITE
;
745 if (ap
->a_mode
& O_EXLOCK
)
746 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
747 else if (ap
->a_mode
& O_SHLOCK
)
748 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
750 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
751 // XXX don't do deny modes just yet (and never do it for !v4)
752 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
754 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
759 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
761 nfs_open_owner_rele(noop
);
764 if (np
->n_flag
& NREVOKE
) {
766 nfs_mount_state_in_use_end(nmp
, 0);
767 nfs_open_owner_rele(noop
);
771 error
= nfs_open_file_find(np
, noop
, &nofp
, accessMode
, denyMode
, 1);
772 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
773 NP(np
, "nfs_vnop_open: LOST %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
776 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
777 nfs_mount_state_in_use_end(nmp
, 0);
778 error
= nfs4_reopen(nofp
, vfs_context_thread(ctx
));
784 error
= nfs_open_file_set_busy(nofp
, vfs_context_thread(ctx
));
790 if (nmp
->nm_vers
< NFS_VER4
) {
792 * NFS v2/v3 opens are always allowed - so just add it.
794 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, 0);
799 * If we just created the file and the modes match, then we simply use
800 * the open performed in the create. Otherwise, send the request.
802 if ((nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) &&
803 (nofp
->nof_creator
== current_thread()) &&
804 (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
) &&
805 (denyMode
== NFS_OPEN_SHARE_DENY_NONE
)) {
806 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
807 nofp
->nof_creator
= NULL
;
810 error
= nfs4_open(np
, nofp
, accessMode
, denyMode
, ctx
);
811 if ((error
== EACCES
) && (nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) &&
812 (nofp
->nof_creator
== current_thread())) {
814 * Ugh. This can happen if we just created the file with read-only
815 * perms and we're trying to open it for real with different modes
816 * (e.g. write-only or with a deny mode) and the server decides to
817 * not allow the second open because of the read-only perms.
818 * The best we can do is to just use the create's open.
819 * We may have access we don't need or we may not have a requested
820 * deny mode. We may log complaints later, but we'll try to avoid it.
822 if (denyMode
!= NFS_OPEN_SHARE_DENY_NONE
)
823 NP(np
, "nfs_vnop_open: deny mode foregone on create, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
824 nofp
->nof_creator
= NULL
;
831 * If we had just created the file, we already had it open.
832 * If the actual open mode is less than what we grabbed at
833 * create time, then we'll downgrade the open here.
835 if ((nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) &&
836 (nofp
->nof_creator
== current_thread())) {
837 error
= nfs_close(np
, nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
839 NP(np
, "nfs_vnop_open: create close error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
840 if (!nfs_mount_state_error_should_restart(error
)) {
842 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
849 nfs_open_file_clear_busy(nofp
);
850 if (nfs_mount_state_in_use_end(nmp
, error
)) {
855 NP(np
, "nfs_vnop_open: error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
857 nfs_open_owner_rele(noop
);
865 * What an NFS client should do upon close after writing is a debatable issue.
866 * Most NFS clients push delayed writes to the server upon close, basically for
868 * 1 - So that any write errors may be reported back to the client process
869 * doing the close system call. By far the two most likely errors are
870 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
871 * 2 - To put a worst case upper bound on cache inconsistency between
872 * multiple clients for the file.
873 * There is also a consistency problem for Version 2 of the protocol w.r.t.
874 * not being able to tell if other clients are writing a file concurrently,
875 * since there is no way of knowing if the changed modify time in the reply
876 * is only due to the write for this client.
877 * (NFS Version 3 provides weak cache consistency data in the reply that
878 * should be sufficient to detect and handle this case.)
880 * The current code does the following:
881 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
882 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate them.
883 * for NFS Version 4 - basically the same as NFSv3
887 struct vnop_close_args
/* {
888 struct vnodeop_desc *a_desc;
891 vfs_context_t a_context;
894 vfs_context_t ctx
= ap
->a_context
;
895 vnode_t vp
= ap
->a_vp
;
896 nfsnode_t np
= VTONFS(vp
);
897 struct nfsmount
*nmp
;
898 int error
= 0, error1
, nfsvers
;
899 int fflag
= ap
->a_fflag
;
901 int accessMode
, denyMode
;
902 struct nfs_open_owner
*noop
= NULL
;
903 struct nfs_open_file
*nofp
= NULL
;
908 nfsvers
= nmp
->nm_vers
;
909 vtype
= vnode_vtype(vp
);
911 /* First, check if we need to update/flush/invalidate */
912 if (ISSET(np
->n_flag
, NUPDATESIZE
))
913 nfs_data_update_size(np
, 0);
914 nfs_node_lock_force(np
);
915 if (np
->n_flag
& NNEEDINVALIDATE
) {
916 np
->n_flag
&= ~NNEEDINVALIDATE
;
918 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, ctx
, 1);
919 nfs_node_lock_force(np
);
921 if ((vtype
== VREG
) && (np
->n_flag
& NMODIFIED
) && (fflag
& FWRITE
)) {
922 /* we're closing an open for write and the file is modified, so flush it */
924 if (nfsvers
!= NFS_VER2
)
925 error
= nfs_flush(np
, MNT_WAIT
, vfs_context_thread(ctx
), 0);
927 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
928 nfs_node_lock_force(np
);
931 if (np
->n_flag
& NWRITEERR
) {
932 np
->n_flag
&= ~NWRITEERR
;
938 /* Just mark that it was closed */
939 lck_mtx_lock(&np
->n_openlock
);
940 if (np
->n_openrefcnt
== 0) {
941 if (fflag
& (FREAD
|FWRITE
)) {
942 NP(np
, "nfs_vnop_close: open reference underrun");
945 } else if (fflag
& (FREAD
|FWRITE
)) {
948 /* No FREAD/FWRITE set - probably the final close */
949 np
->n_openrefcnt
= 0;
951 lck_mtx_unlock(&np
->n_openlock
);
956 /* fflag should contain some combination of: FREAD, FWRITE, FHASLOCK */
959 accessMode
|= NFS_OPEN_SHARE_ACCESS_READ
;
961 accessMode
|= NFS_OPEN_SHARE_ACCESS_WRITE
;
962 // XXX It would be nice if we still had the O_EXLOCK/O_SHLOCK flags that were on the open
963 // if (fflag & O_EXLOCK)
964 // denyMode = NFS_OPEN_SHARE_DENY_BOTH;
965 // else if (fflag & O_SHLOCK)
966 // denyMode = NFS_OPEN_SHARE_DENY_WRITE;
968 // denyMode = NFS_OPEN_SHARE_DENY_NONE;
969 if (fflag
& FHASLOCK
) {
970 /* XXX assume FHASLOCK is for the deny mode and not flock */
971 /* FHASLOCK flock will be unlocked in the close path, but the flag is not cleared. */
972 if (nofp
->nof_deny
& NFS_OPEN_SHARE_DENY_READ
)
973 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
974 else if (nofp
->nof_deny
& NFS_OPEN_SHARE_DENY_WRITE
)
975 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
977 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
979 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
981 // XXX don't do deny modes just yet (and never do it for !v4)
982 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
986 * No mode given to close?
987 * Guess this is the final close.
988 * We should unlock all locks and close all opens.
990 mount_t mp
= vnode_mount(vp
);
991 int force
= (!mp
|| (mp
->mnt_kern_flag
& MNTK_FRCUNMOUNT
));
992 nfs_release_open_state_for_node(np
, force
);
996 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 0);
998 // printf("nfs_vnop_close: can't get open owner!\n");
1003 error
= nfs_mount_state_in_use_start(nmp
, NULL
);
1005 nfs_open_owner_rele(noop
);
1009 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 0);
1010 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
1011 nfs_mount_state_in_use_end(nmp
, 0);
1012 error
= nfs4_reopen(nofp
, NULL
);
1018 NP(np
, "nfs_vnop_close: no open file for owner, error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
1022 error
= nfs_open_file_set_busy(nofp
, NULL
);
1028 error
= nfs_close(np
, nofp
, accessMode
, denyMode
, ctx
);
1030 NP(np
, "nfs_vnop_close: close error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
1034 nfs_open_file_clear_busy(nofp
);
1035 if (nfs_mount_state_in_use_end(nmp
, error
)) {
1042 NP(np
, "nfs_vnop_close: error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
1044 nfs_open_owner_rele(noop
);
1049 * nfs_close(): common function that does all the heavy lifting of file closure
1051 * Takes an open file structure and a set of access/deny modes and figures out how
1052 * to update the open file structure (and the state on the server) appropriately.
1057 struct nfs_open_file
*nofp
,
1058 uint32_t accessMode
,
1062 struct nfs_lock_owner
*nlop
;
1063 int error
= 0, changed
= 0, delegated
= 0, closed
= 0, downgrade
= 0;
1064 uint32_t newAccessMode
, newDenyMode
;
1066 /* warn if modes don't match current state */
1067 if (((accessMode
& nofp
->nof_access
) != accessMode
) || ((denyMode
& nofp
->nof_deny
) != denyMode
))
1068 NP(np
, "nfs_close: mode mismatch %d %d, current %d %d, %d",
1069 accessMode
, denyMode
, nofp
->nof_access
, nofp
->nof_deny
,
1070 kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1073 * If we're closing a write-only open, we may not have a write-only count
1074 * if we also grabbed read access. So, check the read-write count.
1076 if (denyMode
== NFS_OPEN_SHARE_DENY_NONE
) {
1077 if ((accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) &&
1078 (nofp
->nof_w
== 0) && (nofp
->nof_d_w
== 0) &&
1079 (nofp
->nof_rw
|| nofp
->nof_d_rw
))
1080 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
1081 } else if (denyMode
== NFS_OPEN_SHARE_DENY_WRITE
) {
1082 if ((accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) &&
1083 (nofp
->nof_w_dw
== 0) && (nofp
->nof_d_w_dw
== 0) &&
1084 (nofp
->nof_rw_dw
|| nofp
->nof_d_rw_dw
))
1085 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
1086 } else { /* NFS_OPEN_SHARE_DENY_BOTH */
1087 if ((accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) &&
1088 (nofp
->nof_w_drw
== 0) && (nofp
->nof_d_w_drw
== 0) &&
1089 (nofp
->nof_rw_drw
|| nofp
->nof_d_rw_drw
))
1090 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
1093 nfs_open_file_remove_open_find(nofp
, accessMode
, denyMode
, &newAccessMode
, &newDenyMode
, &delegated
);
1094 if ((newAccessMode
!= nofp
->nof_access
) || (newDenyMode
!= nofp
->nof_deny
))
1099 if (NFSTONMP(np
)->nm_vers
< NFS_VER4
) /* NFS v2/v3 closes simply need to remove the open. */
1102 if ((newAccessMode
== 0) || (nofp
->nof_opencnt
== 1)) {
1104 * No more access after this close, so clean up and close it.
1105 * Don't send a close RPC if we're closing a delegated open.
1109 if (!delegated
&& !(nofp
->nof_flags
& NFS_OPEN_FILE_LOST
))
1110 error
= nfs4_close_rpc(np
, nofp
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
), 0);
1111 if (error
== NFSERR_LOCKS_HELD
) {
1113 * Hmm... the server says we have locks we need to release first
1114 * Find the lock owner and try to unlock everything.
1116 nlop
= nfs_lock_owner_find(np
, vfs_context_proc(ctx
), 0);
1118 nfs4_unlock_rpc(np
, nlop
, F_WRLCK
, 0, UINT64_MAX
,
1119 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
1120 nfs_lock_owner_rele(nlop
);
1122 error
= nfs4_close_rpc(np
, nofp
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
), 0);
1124 } else if (changed
) {
1126 * File is still open but with less access, so downgrade the open.
1127 * Don't send a downgrade RPC if we're closing a delegated open.
1129 if (!delegated
&& !(nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
1132 * If we have delegated opens, we should probably claim them before sending
1133 * the downgrade because the server may not know the open we are downgrading to.
1135 if (nofp
->nof_d_rw_drw
|| nofp
->nof_d_w_drw
|| nofp
->nof_d_r_drw
||
1136 nofp
->nof_d_rw_dw
|| nofp
->nof_d_w_dw
|| nofp
->nof_d_r_dw
||
1137 nofp
->nof_d_rw
|| nofp
->nof_d_w
|| nofp
->nof_d_r
)
1138 nfs4_claim_delegated_state_for_open_file(nofp
, 0);
1139 /* need to remove the open before sending the downgrade */
1140 nfs_open_file_remove_open(nofp
, accessMode
, denyMode
);
1141 error
= nfs4_open_downgrade_rpc(np
, nofp
, ctx
);
1142 if (error
) /* Hmm.. that didn't work. Add the open back in. */
1143 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, delegated
);
1148 NP(np
, "nfs_close: error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1154 nfs_open_file_remove_open(nofp
, accessMode
, denyMode
);
1157 lck_mtx_lock(&nofp
->nof_lock
);
1158 if (nofp
->nof_r
|| nofp
->nof_d_r
|| nofp
->nof_w
|| nofp
->nof_d_w
|| nofp
->nof_d_rw
||
1159 (nofp
->nof_rw
&& !((nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) && !nofp
->nof_creator
&& (nofp
->nof_rw
== 1))) ||
1160 nofp
->nof_r_dw
|| nofp
->nof_d_r_dw
|| nofp
->nof_w_dw
|| nofp
->nof_d_w_dw
||
1161 nofp
->nof_rw_dw
|| nofp
->nof_d_rw_dw
|| nofp
->nof_r_drw
|| nofp
->nof_d_r_drw
||
1162 nofp
->nof_w_drw
|| nofp
->nof_d_w_drw
|| nofp
->nof_rw_drw
|| nofp
->nof_d_rw_drw
)
1163 NP(np
, "nfs_close: unexpected count: %u.%u %u.%u %u.%u dw %u.%u %u.%u %u.%u drw %u.%u %u.%u %u.%u flags 0x%x, %d",
1164 nofp
->nof_r
, nofp
->nof_d_r
, nofp
->nof_w
, nofp
->nof_d_w
,
1165 nofp
->nof_rw
, nofp
->nof_d_rw
, nofp
->nof_r_dw
, nofp
->nof_d_r_dw
,
1166 nofp
->nof_w_dw
, nofp
->nof_d_w_dw
, nofp
->nof_rw_dw
, nofp
->nof_d_rw_dw
,
1167 nofp
->nof_r_drw
, nofp
->nof_d_r_drw
, nofp
->nof_w_drw
, nofp
->nof_d_w_drw
,
1168 nofp
->nof_rw_drw
, nofp
->nof_d_rw_drw
, nofp
->nof_flags
,
1169 kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1170 /* clear out all open info, just to be safe */
1171 nofp
->nof_access
= nofp
->nof_deny
= 0;
1172 nofp
->nof_mmap_access
= nofp
->nof_mmap_deny
= 0;
1173 nofp
->nof_r
= nofp
->nof_d_r
= 0;
1174 nofp
->nof_w
= nofp
->nof_d_w
= 0;
1175 nofp
->nof_rw
= nofp
->nof_d_rw
= 0;
1176 nofp
->nof_r_dw
= nofp
->nof_d_r_dw
= 0;
1177 nofp
->nof_w_dw
= nofp
->nof_d_w_dw
= 0;
1178 nofp
->nof_rw_dw
= nofp
->nof_d_rw_dw
= 0;
1179 nofp
->nof_r_drw
= nofp
->nof_d_r_drw
= 0;
1180 nofp
->nof_w_drw
= nofp
->nof_d_w_drw
= 0;
1181 nofp
->nof_rw_drw
= nofp
->nof_d_rw_drw
= 0;
1182 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
1183 lck_mtx_unlock(&nofp
->nof_lock
);
1184 /* XXX we may potentially want to clean up idle/unused open file structures */
1186 if (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
) {
1188 NP(np
, "nfs_close: LOST%s, %d", !nofp
->nof_opencnt
? " (last)" : "",
1189 kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1205 struct nfs_vattr
*nvap
,
1208 struct nfsmount
*nmp
= mp
? VFSTONFS(mp
) : NFSTONMP(np
);
1209 int error
= 0, status
, nfsvers
, rpcflags
= 0;
1210 struct nfsm_chain nmreq
, nmrep
;
1214 nfsvers
= nmp
->nm_vers
;
1216 if (flags
& NGA_MONITOR
) /* vnode monitor requests should be soft */
1217 rpcflags
= R_RECOVER
;
1219 nfsm_chain_null(&nmreq
);
1220 nfsm_chain_null(&nmrep
);
1222 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nfsvers
));
1223 if (nfsvers
!= NFS_VER2
)
1224 nfsm_chain_add_32(error
, &nmreq
, fhsize
);
1225 nfsm_chain_add_opaque(error
, &nmreq
, fhp
, fhsize
);
1226 nfsm_chain_build_done(error
, &nmreq
);
1228 error
= nfs_request2(np
, mp
, &nmreq
, NFSPROC_GETATTR
,
1229 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
1230 NULL
, rpcflags
, &nmrep
, xidp
, &status
);
1234 error
= nfs_parsefattr(&nmrep
, nfsvers
, nvap
);
1236 nfsm_chain_cleanup(&nmreq
);
1237 nfsm_chain_cleanup(&nmrep
);
1243 nfs_getattr(nfsnode_t np
, struct nfs_vattr
*nvap
, vfs_context_t ctx
, int flags
)
1245 struct nfsmount
*nmp
;
1246 int error
= 0, nfsvers
, inprogset
= 0, wanted
= 0, avoidfloods
;
1247 struct nfs_vattr nvattr
;
1248 struct timespec ts
= { 2, 0 };
1251 FSDBG_TOP(513, np
->n_size
, np
, np
->n_vattr
.nva_size
, np
->n_flag
);
1253 if (!(nmp
= NFSTONMP(np
)))
1255 nfsvers
= nmp
->nm_vers
;
1261 /* Update local times for special files. */
1262 if (np
->n_flag
& (NACC
| NUPD
)) {
1263 nfs_node_lock_force(np
);
1265 nfs_node_unlock(np
);
1267 /* Update size, if necessary */
1268 if (ISSET(np
->n_flag
, NUPDATESIZE
))
1269 nfs_data_update_size(np
, 0);
1271 error
= nfs_node_lock(np
);
1273 if (!(flags
& (NGA_UNCACHED
|NGA_MONITOR
)) || ((nfsvers
>= NFS_VER4
) && (np
->n_openflags
& N_DELEG_MASK
))) {
1275 * Use the cache or wait for any getattr in progress if:
1276 * - it's a cached request, or
1277 * - we have a delegation
1280 error
= nfs_getattrcache(np
, nvap
, flags
);
1281 if (!error
|| (error
!= ENOENT
)) {
1282 nfs_node_unlock(np
);
1286 if (!ISSET(np
->n_flag
, NGETATTRINPROG
))
1288 if (flags
& NGA_MONITOR
) {
1289 /* no need to wait if a request is pending */
1290 error
= EINPROGRESS
;
1291 nfs_node_unlock(np
);
1294 SET(np
->n_flag
, NGETATTRWANT
);
1295 msleep(np
, &np
->n_lock
, PZERO
-1, "nfsgetattrwant", &ts
);
1296 if ((error
= nfs_sigintr(NFSTONMP(np
), NULL
, vfs_context_thread(ctx
), 0))) {
1297 nfs_node_unlock(np
);
1301 SET(np
->n_flag
, NGETATTRINPROG
);
1303 } else if (!ISSET(np
->n_flag
, NGETATTRINPROG
)) {
1304 SET(np
->n_flag
, NGETATTRINPROG
);
1306 } else if (flags
& NGA_MONITOR
) {
1307 /* no need to make a request if one is pending */
1308 error
= EINPROGRESS
;
1310 nfs_node_unlock(np
);
1319 * We might want to try to get both the attributes and access info by
1320 * making an ACCESS call and seeing if it returns updated attributes.
1321 * But don't bother if we aren't caching access info or if the
1322 * attributes returned wouldn't be cached.
1324 if (!(flags
& NGA_ACL
) && (nfsvers
!= NFS_VER2
) && nfs_access_for_getattr
&& (nfs_access_cache_timeout
> 0)) {
1325 if (nfs_attrcachetimeout(np
) > 0) {
1326 /* OSAddAtomic(1, &nfsstats.accesscache_misses); */
1327 u_int32_t access
= NFS_ACCESS_ALL
;
1328 error
= nmp
->nm_funcs
->nf_access_rpc(np
, &access
, ctx
);
1331 nfs_node_lock_force(np
);
1332 error
= nfs_getattrcache(np
, nvap
, flags
);
1333 nfs_node_unlock(np
);
1334 if (!error
|| (error
!= ENOENT
))
1336 /* Well, that didn't work... just do a getattr... */
1343 error
= nmp
->nm_funcs
->nf_getattr_rpc(np
, NULL
, np
->n_fhp
, np
->n_fhsize
, flags
, ctx
, nvap
, &xid
);
1345 nfs_node_lock_force(np
);
1346 error
= nfs_loadattrcache(np
, nvap
, &xid
, 0);
1347 nfs_node_unlock(np
);
1350 if (!xid
) { /* out-of-order rpc - attributes were dropped */
1351 FSDBG(513, -1, np
, np
->n_xid
>> 32, np
->n_xid
);
1352 if (avoidfloods
++ < 20)
1354 /* avoidfloods>1 is bizarre. at 20 pull the plug */
1355 /* just return the last attributes we got */
1358 nfs_node_lock_force(np
);
1360 wanted
= ISSET(np
->n_flag
, NGETATTRWANT
);
1361 CLR(np
->n_flag
, (NGETATTRINPROG
| NGETATTRWANT
));
1364 /* check if the node changed on us */
1365 vnode_t vp
= NFSTOV(np
);
1366 enum vtype vtype
= vnode_vtype(vp
);
1367 if ((vtype
== VDIR
) && NFS_CHANGED_NC(nfsvers
, np
, nvap
)) {
1368 FSDBG(513, -1, np
, 0, np
);
1369 np
->n_flag
&= ~NNEGNCENTRIES
;
1372 NFS_CHANGED_UPDATE_NC(nfsvers
, np
, nvap
);
1374 if (NFS_CHANGED(nfsvers
, np
, nvap
)) {
1375 FSDBG(513, -1, np
, -1, np
);
1378 nfs_node_unlock(np
);
1381 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
1382 FSDBG(513, -1, np
, -2, error
);
1384 nfs_node_lock_force(np
);
1385 NFS_CHANGED_UPDATE(nfsvers
, np
, nvap
);
1386 nfs_node_unlock(np
);
1389 nfs_node_unlock(np
);
1394 nfs_node_unlock(np
);
1399 if (nvap
== &nvattr
) {
1400 NVATTR_CLEANUP(nvap
);
1401 } else if (!(flags
& NGA_ACL
)) {
1402 /* make sure we don't return an ACL if it wasn't asked for */
1403 NFS_BITMAP_CLR(nvap
->nva_bitmap
, NFS_FATTR_ACL
);
1404 if (nvap
->nva_acl
) {
1405 kauth_acl_free(nvap
->nva_acl
);
1406 nvap
->nva_acl
= NULL
;
1409 FSDBG_BOT(513, np
->n_size
, error
, np
->n_vattr
.nva_size
, np
->n_flag
);
1414 * NFS getattr call from vfs.
1418 struct vnop_getattr_args
/* {
1419 struct vnodeop_desc *a_desc;
1421 struct vnode_attr *a_vap;
1422 vfs_context_t a_context;
1426 struct nfs_vattr nva
;
1427 struct vnode_attr
*vap
= ap
->a_vap
;
1430 error
= nfs_getattr(VTONFS(ap
->a_vp
), &nva
, ap
->a_context
, NGA_CACHED
);
1434 /* copy nva to *a_vap */
1435 VATTR_RETURN(vap
, va_type
, nva
.nva_type
);
1436 VATTR_RETURN(vap
, va_mode
, nva
.nva_mode
);
1437 rdev
= makedev(nva
.nva_rawdev
.specdata1
, nva
.nva_rawdev
.specdata2
);
1438 VATTR_RETURN(vap
, va_rdev
, rdev
);
1439 VATTR_RETURN(vap
, va_uid
, nva
.nva_uid
);
1440 VATTR_RETURN(vap
, va_gid
, nva
.nva_gid
);
1441 VATTR_RETURN(vap
, va_nlink
, nva
.nva_nlink
);
1442 VATTR_RETURN(vap
, va_fileid
, nva
.nva_fileid
);
1443 VATTR_RETURN(vap
, va_data_size
, nva
.nva_size
);
1444 VATTR_RETURN(vap
, va_data_alloc
, nva
.nva_bytes
);
1445 VATTR_RETURN(vap
, va_iosize
, nfs_iosize
);
1446 vap
->va_access_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_ACCESS
];
1447 vap
->va_access_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_ACCESS
];
1448 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1449 vap
->va_modify_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_MODIFY
];
1450 vap
->va_modify_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_MODIFY
];
1451 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1452 vap
->va_change_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_CHANGE
];
1453 vap
->va_change_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_CHANGE
];
1454 VATTR_SET_SUPPORTED(vap
, va_change_time
);
1456 // VATTR_RETURN(vap, va_encoding, 0xffff /* kTextEncodingUnknown */);
1465 struct vnop_setattr_args
/* {
1466 struct vnodeop_desc *a_desc;
1468 struct vnode_attr *a_vap;
1469 vfs_context_t a_context;
1472 vfs_context_t ctx
= ap
->a_context
;
1473 vnode_t vp
= ap
->a_vp
;
1474 nfsnode_t np
= VTONFS(vp
);
1475 struct nfsmount
*nmp
;
1476 struct vnode_attr
*vap
= ap
->a_vap
;
1478 int biosize
, nfsvers
, namedattrs
;
1479 u_quad_t origsize
, vapsize
;
1480 struct nfs_dulookup dul
;
1481 nfsnode_t dnp
= NULL
;
1483 const char *vname
= NULL
;
1484 struct nfs_open_owner
*noop
= NULL
;
1485 struct nfs_open_file
*nofp
= NULL
;
1490 nfsvers
= nmp
->nm_vers
;
1491 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
1492 biosize
= nmp
->nm_biosize
;
1494 /* Disallow write attempts if the filesystem is mounted read-only. */
1495 if (vnode_vfsisrdonly(vp
))
1498 origsize
= np
->n_size
;
1499 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
1500 switch (vnode_vtype(vp
)) {
1507 if (!VATTR_IS_ACTIVE(vap
, va_modify_time
) &&
1508 !VATTR_IS_ACTIVE(vap
, va_access_time
) &&
1509 !VATTR_IS_ACTIVE(vap
, va_mode
) &&
1510 !VATTR_IS_ACTIVE(vap
, va_uid
) &&
1511 !VATTR_IS_ACTIVE(vap
, va_gid
)) {
1514 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
1518 * Disallow write attempts if the filesystem is
1519 * mounted read-only.
1521 if (vnode_vfsisrdonly(vp
))
1523 FSDBG_TOP(512, np
->n_size
, vap
->va_data_size
,
1524 np
->n_vattr
.nva_size
, np
->n_flag
);
1525 /* clear NNEEDINVALIDATE, if set */
1526 if ((error
= nfs_node_lock(np
)))
1528 if (np
->n_flag
& NNEEDINVALIDATE
)
1529 np
->n_flag
&= ~NNEEDINVALIDATE
;
1530 nfs_node_unlock(np
);
1531 /* flush everything */
1532 error
= nfs_vinvalbuf(vp
, (vap
->va_data_size
? V_SAVE
: 0) , ctx
, 1);
1534 NP(np
, "nfs_setattr: nfs_vinvalbuf %d", error
);
1535 FSDBG_BOT(512, np
->n_size
, vap
->va_data_size
, np
->n_vattr
.nva_size
, -1);
1538 if (nfsvers
>= NFS_VER4
) {
1539 /* setting file size requires having the file open for write access */
1540 if (np
->n_flag
& NREVOKE
)
1542 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
1546 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
1549 if (np
->n_flag
& NREVOKE
) {
1550 nfs_mount_state_in_use_end(nmp
, 0);
1553 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 1);
1554 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
))
1556 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
1557 nfs_mount_state_in_use_end(nmp
, 0);
1558 error
= nfs4_reopen(nofp
, vfs_context_thread(ctx
));
1564 error
= nfs_open_file_set_busy(nofp
, vfs_context_thread(ctx
));
1566 nfs_open_owner_rele(noop
);
1569 if (!(nofp
->nof_access
& NFS_OPEN_SHARE_ACCESS_WRITE
)) {
1570 /* we don't have the file open for write access, so open it */
1571 error
= nfs4_open(np
, nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
1573 nofp
->nof_flags
|= NFS_OPEN_FILE_SETATTR
;
1574 if (nfs_mount_state_error_should_restart(error
)) {
1575 nfs_open_file_clear_busy(nofp
);
1577 if (nfs_mount_state_in_use_end(nmp
, error
))
1582 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
1583 if (np
->n_size
> vap
->va_data_size
) { /* shrinking? */
1585 int neweofoff
, mustwrite
;
1588 obn
= (np
->n_size
- 1) / biosize
;
1589 bn
= vap
->va_data_size
/ biosize
;
1590 for ( ; obn
>= bn
; obn
--) {
1591 if (!nfs_buf_is_incore(np
, obn
))
1593 error
= nfs_buf_get(np
, obn
, biosize
, NULL
, NBLK_READ
, &bp
);
1597 FSDBG(512, bp
, bp
->nb_flags
, 0, obn
);
1598 SET(bp
->nb_flags
, NB_INVAL
);
1599 nfs_buf_release(bp
, 1);
1603 neweofoff
= vap
->va_data_size
- NBOFF(bp
);
1604 /* check for any dirty data before the new EOF */
1605 if ((bp
->nb_dirtyend
> 0) && (bp
->nb_dirtyoff
< neweofoff
)) {
1606 /* clip dirty range to EOF */
1607 if (bp
->nb_dirtyend
> neweofoff
) {
1608 bp
->nb_dirtyend
= neweofoff
;
1609 if (bp
->nb_dirtyoff
>= bp
->nb_dirtyend
)
1610 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
1612 if ((bp
->nb_dirtyend
> 0) && (bp
->nb_dirtyoff
< neweofoff
))
1615 bp
->nb_dirty
&= (1 << round_page_32(neweofoff
)/PAGE_SIZE
) - 1;
1619 FSDBG(512, bp
, bp
->nb_flags
, 0, obn
);
1620 SET(bp
->nb_flags
, NB_INVAL
);
1621 nfs_buf_release(bp
, 1);
1624 /* gotta write out dirty data before invalidating */
1625 /* (NB_STABLE indicates that data writes should be FILESYNC) */
1626 /* (NB_NOCACHE indicates buffer should be discarded) */
1627 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
| NB_ASYNC
| NB_READ
));
1628 SET(bp
->nb_flags
, NB_STABLE
| NB_NOCACHE
);
1629 if (!IS_VALID_CRED(bp
->nb_wcred
)) {
1630 kauth_cred_t cred
= vfs_context_ucred(ctx
);
1631 kauth_cred_ref(cred
);
1632 bp
->nb_wcred
= cred
;
1634 error
= nfs_buf_write(bp
);
1635 // Note: bp has been released
1637 FSDBG(512, bp
, 0xd00dee, 0xbad, error
);
1638 nfs_node_lock_force(np
);
1639 np
->n_error
= error
;
1640 np
->n_flag
|= NWRITEERR
;
1642 * There was a write error and we need to
1643 * invalidate attrs and flush buffers in
1644 * order to sync up with the server.
1645 * (if this write was extending the file,
1646 * we may no longer know the correct size)
1648 NATTRINVALIDATE(np
);
1649 nfs_node_unlock(np
);
1650 nfs_data_unlock(np
);
1651 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, ctx
, 1);
1652 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
1657 if (vap
->va_data_size
!= np
->n_size
)
1658 ubc_setsize(vp
, (off_t
)vap
->va_data_size
); /* XXX error? */
1659 origsize
= np
->n_size
;
1660 np
->n_size
= np
->n_vattr
.nva_size
= vap
->va_data_size
;
1661 nfs_node_lock_force(np
);
1662 CLR(np
->n_flag
, NUPDATESIZE
);
1663 nfs_node_unlock(np
);
1664 FSDBG(512, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0001);
1666 } else if (VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
1667 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
1668 (vap
->va_vaflags
& VA_UTIMES_NULL
)) {
1669 if ((error
= nfs_node_lock(np
)))
1671 if ((np
->n_flag
& NMODIFIED
) && (vnode_vtype(vp
) == VREG
)) {
1672 nfs_node_unlock(np
);
1673 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
1677 nfs_node_unlock(np
);
1680 if ((VATTR_IS_ACTIVE(vap
, va_mode
) || VATTR_IS_ACTIVE(vap
, va_uid
) || VATTR_IS_ACTIVE(vap
, va_gid
) ||
1681 VATTR_IS_ACTIVE(vap
, va_acl
) || VATTR_IS_ACTIVE(vap
, va_uuuid
) || VATTR_IS_ACTIVE(vap
, va_guuid
)) &&
1682 !(error
= nfs_node_lock(np
))) {
1683 NACCESSINVALIDATE(np
);
1684 nfs_node_unlock(np
);
1686 dvp
= vnode_getparent(vp
);
1687 vname
= vnode_getname(vp
);
1688 dnp
= (dvp
&& vname
) ? VTONFS(dvp
) : NULL
;
1690 error
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
1697 nfs_dulookup_init(&dul
, dnp
, vname
, strlen(vname
), ctx
);
1698 nfs_dulookup_start(&dul
, dnp
, ctx
);
1704 error
= nmp
->nm_funcs
->nf_setattr_rpc(np
, vap
, ctx
);
1706 if (VATTR_IS_ACTIVE(vap
, va_mode
) || VATTR_IS_ACTIVE(vap
, va_uid
) || VATTR_IS_ACTIVE(vap
, va_gid
) ||
1707 VATTR_IS_ACTIVE(vap
, va_acl
) || VATTR_IS_ACTIVE(vap
, va_uuuid
) || VATTR_IS_ACTIVE(vap
, va_guuid
)) {
1710 nfs_dulookup_finish(&dul
, dnp
, ctx
);
1711 nfs_node_clear_busy(dnp
);
1716 vnode_putname(vname
);
1720 FSDBG_BOT(512, np
->n_size
, vap
->va_data_size
, np
->n_vattr
.nva_size
, error
);
1721 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
1722 if (error
&& (origsize
!= np
->n_size
) &&
1723 ((nfsvers
< NFS_VER4
) || !nfs_mount_state_error_should_restart(error
))) {
1724 /* make every effort to resync file size w/ server... */
1725 /* (don't bother if we'll be restarting the operation) */
1726 int err
; /* preserve "error" for return */
1727 np
->n_size
= np
->n_vattr
.nva_size
= origsize
;
1728 nfs_node_lock_force(np
);
1729 CLR(np
->n_flag
, NUPDATESIZE
);
1730 nfs_node_unlock(np
);
1731 FSDBG(512, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0002);
1732 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX check error */
1733 vapsize
= vap
->va_data_size
;
1734 vap
->va_data_size
= origsize
;
1735 err
= nmp
->nm_funcs
->nf_setattr_rpc(np
, vap
, ctx
);
1737 NP(np
, "nfs_vnop_setattr: nfs%d_setattr_rpc %d %d", nfsvers
, error
, err
);
1738 vap
->va_data_size
= vapsize
;
1740 nfs_node_lock_force(np
);
1742 * The size was just set. If the size is already marked for update, don't
1743 * trust the newsize (it may have been set while the setattr was in progress).
1744 * Clear the update flag and make sure we fetch new attributes so we are sure
1745 * we have the latest size.
1747 if (ISSET(np
->n_flag
, NUPDATESIZE
)) {
1748 CLR(np
->n_flag
, NUPDATESIZE
);
1749 NATTRINVALIDATE(np
);
1750 nfs_node_unlock(np
);
1751 nfs_getattr(np
, NULL
, ctx
, NGA_UNCACHED
);
1753 nfs_node_unlock(np
);
1755 nfs_data_unlock(np
);
1756 if (nfsvers
>= NFS_VER4
) {
1758 /* don't close our setattr open if we'll be restarting... */
1759 if (!nfs_mount_state_error_should_restart(error
) &&
1760 (nofp
->nof_flags
& NFS_OPEN_FILE_SETATTR
)) {
1761 int err
= nfs_close(np
, nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
1763 NP(np
, "nfs_vnop_setattr: close error: %d", err
);
1764 nofp
->nof_flags
&= ~NFS_OPEN_FILE_SETATTR
;
1766 nfs_open_file_clear_busy(nofp
);
1769 if (nfs_mount_state_in_use_end(nmp
, error
))
1771 nfs_open_owner_rele(noop
);
1778 * Do an NFS setattr RPC.
1783 struct vnode_attr
*vap
,
1786 struct nfsmount
*nmp
= NFSTONMP(np
);
1787 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0, nfsvers
;
1788 u_int64_t xid
, nextxid
;
1789 struct nfsm_chain nmreq
, nmrep
;
1793 nfsvers
= nmp
->nm_vers
;
1795 VATTR_SET_SUPPORTED(vap
, va_mode
);
1796 VATTR_SET_SUPPORTED(vap
, va_uid
);
1797 VATTR_SET_SUPPORTED(vap
, va_gid
);
1798 VATTR_SET_SUPPORTED(vap
, va_data_size
);
1799 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1800 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1802 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
1803 if (vap
->va_flags
) { /* we don't support setting flags */
1804 if (vap
->va_active
& ~VNODE_ATTR_va_flags
)
1805 return (EINVAL
); /* return EINVAL if other attributes also set */
1807 return (ENOTSUP
); /* return ENOTSUP for chflags(2) */
1809 /* no flags set, so we'll just ignore it */
1810 if (!(vap
->va_active
& ~VNODE_ATTR_va_flags
))
1811 return (0); /* no (other) attributes to set, so nothing to do */
1814 nfsm_chain_null(&nmreq
);
1815 nfsm_chain_null(&nmrep
);
1817 nfsm_chain_build_alloc_init(error
, &nmreq
,
1818 NFSX_FH(nfsvers
) + NFSX_SATTR(nfsvers
));
1819 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
1820 if (nfsvers
== NFS_VER3
) {
1821 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
1822 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
1823 nfsm_chain_add_32(error
, &nmreq
, vap
->va_mode
);
1825 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
1827 if (VATTR_IS_ACTIVE(vap
, va_uid
)) {
1828 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
1829 nfsm_chain_add_32(error
, &nmreq
, vap
->va_uid
);
1831 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
1833 if (VATTR_IS_ACTIVE(vap
, va_gid
)) {
1834 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
1835 nfsm_chain_add_32(error
, &nmreq
, vap
->va_gid
);
1837 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
1839 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
1840 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
1841 nfsm_chain_add_64(error
, &nmreq
, vap
->va_data_size
);
1843 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
1845 if (vap
->va_vaflags
& VA_UTIMES_NULL
) {
1846 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_SERVER
);
1847 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_SERVER
);
1849 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
1850 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_CLIENT
);
1851 nfsm_chain_add_32(error
, &nmreq
, vap
->va_access_time
.tv_sec
);
1852 nfsm_chain_add_32(error
, &nmreq
, vap
->va_access_time
.tv_nsec
);
1854 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_DONT_CHANGE
);
1856 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
1857 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_CLIENT
);
1858 nfsm_chain_add_32(error
, &nmreq
, vap
->va_modify_time
.tv_sec
);
1859 nfsm_chain_add_32(error
, &nmreq
, vap
->va_modify_time
.tv_nsec
);
1861 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_DONT_CHANGE
);
1864 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
1866 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_mode
) ?
1867 vtonfsv2_mode(vnode_vtype(NFSTOV(np
)), vap
->va_mode
) : -1);
1868 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_uid
) ?
1869 vap
->va_uid
: (uint32_t)-1);
1870 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_gid
) ?
1871 vap
->va_gid
: (uint32_t)-1);
1872 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_data_size
) ?
1873 vap
->va_data_size
: (uint32_t)-1);
1874 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
1875 nfsm_chain_add_32(error
, &nmreq
, vap
->va_access_time
.tv_sec
);
1876 nfsm_chain_add_32(error
, &nmreq
, (vap
->va_access_time
.tv_nsec
!= -1) ?
1877 ((uint32_t)vap
->va_access_time
.tv_nsec
/ 1000) : 0xffffffff);
1879 nfsm_chain_add_32(error
, &nmreq
, -1);
1880 nfsm_chain_add_32(error
, &nmreq
, -1);
1882 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
1883 nfsm_chain_add_32(error
, &nmreq
, vap
->va_modify_time
.tv_sec
);
1884 nfsm_chain_add_32(error
, &nmreq
, (vap
->va_modify_time
.tv_nsec
!= -1) ?
1885 ((uint32_t)vap
->va_modify_time
.tv_nsec
/ 1000) : 0xffffffff);
1887 nfsm_chain_add_32(error
, &nmreq
, -1);
1888 nfsm_chain_add_32(error
, &nmreq
, -1);
1891 nfsm_chain_build_done(error
, &nmreq
);
1893 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_SETATTR
, ctx
, NULL
, &nmrep
, &xid
, &status
);
1894 if ((lockerror
= nfs_node_lock(np
)))
1896 if (nfsvers
== NFS_VER3
) {
1897 struct timespec premtime
= { 0, 0 };
1898 nfsm_chain_get_wcc_data(error
, &nmrep
, np
, &premtime
, &wccpostattr
, &xid
);
1900 /* if file hadn't changed, update cached mtime */
1901 if (nfstimespeccmp(&np
->n_mtime
, &premtime
, ==))
1902 NFS_CHANGED_UPDATE(nfsvers
, np
, &np
->n_vattr
);
1903 /* if directory hadn't changed, update namecache mtime */
1904 if ((vnode_vtype(NFSTOV(np
)) == VDIR
) &&
1905 nfstimespeccmp(&np
->n_ncmtime
, &premtime
, ==))
1906 NFS_CHANGED_UPDATE_NC(nfsvers
, np
, &np
->n_vattr
);
1908 NATTRINVALIDATE(np
);
1913 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
1916 * We just changed the attributes and we want to make sure that we
1917 * see the latest attributes. Get the next XID. If it's not the
1918 * next XID after the SETATTR XID, then it's possible that another
1919 * RPC was in flight at the same time and it might put stale attributes
1920 * in the cache. In that case, we invalidate the attributes and set
1921 * the attribute cache XID to guarantee that newer attributes will
1925 nfs_get_xid(&nextxid
);
1926 if (nextxid
!= (xid
+ 1)) {
1927 np
->n_xid
= nextxid
;
1928 NATTRINVALIDATE(np
);
1932 nfs_node_unlock(np
);
1933 nfsm_chain_cleanup(&nmreq
);
1934 nfsm_chain_cleanup(&nmrep
);
1939 * NFS lookup call, one step at a time...
1940 * First look in cache
1941 * If not found, unlock the directory nfsnode and do the RPC
1945 struct vnop_lookup_args
/* {
1946 struct vnodeop_desc *a_desc;
1949 struct componentname *a_cnp;
1950 vfs_context_t a_context;
1953 vfs_context_t ctx
= ap
->a_context
;
1954 struct componentname
*cnp
= ap
->a_cnp
;
1955 vnode_t dvp
= ap
->a_dvp
;
1956 vnode_t
*vpp
= ap
->a_vpp
;
1957 int flags
= cnp
->cn_flags
;
1960 struct nfsmount
*nmp
;
1962 int nfsvers
, error
, busyerror
= ENOENT
, isdot
, isdotdot
, negnamecache
;
1964 struct nfs_vattr nvattr
;
1966 struct vnop_access_args naa
;
1968 struct nfsreq rq
, *req
= &rq
;
1973 NVATTR_INIT(&nvattr
);
1975 mp
= vnode_mount(dvp
);
1981 nfsvers
= nmp
->nm_vers
;
1982 negnamecache
= !NMFLAG(nmp
, NONEGNAMECACHE
);
1984 if ((error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
))))
1986 /* nfs_getattr() will check changed and purge caches */
1987 if ((error
= nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
)))
1990 error
= cache_lookup(dvp
, vpp
, cnp
);
1993 /* negative cache entry */
1997 if ((nfsvers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
)) {
1998 /* if rdirplus, try dir buf cache lookup */
1999 error
= nfs_dir_buf_cache_lookup(dnp
, &np
, cnp
, ctx
, 0);
2001 /* dir buf cache hit */
2006 if (error
!= -1) /* cache miss */
2010 /* cache hit, not really an error */
2011 OSAddAtomic(1, &nfsstats
.lookupcache_hits
);
2013 nfs_node_clear_busy(dnp
);
2016 /* check for directory access */
2017 naa
.a_desc
= &vnop_access_desc
;
2019 naa
.a_action
= KAUTH_VNODE_SEARCH
;
2020 naa
.a_context
= ctx
;
2022 /* compute actual success/failure based on accessibility */
2023 error
= nfs_vnop_access(&naa
);
2026 /* unexpected error from cache_lookup */
2030 /* skip lookup, if we know who we are: "." or ".." */
2031 isdot
= isdotdot
= 0;
2032 if (cnp
->cn_nameptr
[0] == '.') {
2033 if (cnp
->cn_namelen
== 1)
2035 if ((cnp
->cn_namelen
== 2) && (cnp
->cn_nameptr
[1] == '.'))
2038 if (isdotdot
|| isdot
) {
2042 if ((nfsvers
>= NFS_VER4
) && (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER
)) {
2043 /* we should never be looking things up in a trigger directory, return nothing */
2048 /* do we know this name is too long? */
2054 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXNAME
) &&
2055 (cnp
->cn_namelen
> (int)nmp
->nm_fsattr
.nfsa_maxname
)) {
2056 error
= ENAMETOOLONG
;
2063 OSAddAtomic(1, &nfsstats
.lookupcache_misses
);
2065 error
= nmp
->nm_funcs
->nf_lookup_rpc_async(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &req
);
2067 error
= nmp
->nm_funcs
->nf_lookup_rpc_async_finish(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, req
, &xid
, &fh
, &nvattr
);
2070 /* is the file handle the same as this directory's file handle? */
2071 isdot
= NFS_CMPFH(dnp
, fh
.fh_data
, fh
.fh_len
);
2074 if (flags
& ISLASTCN
) {
2075 switch (cnp
->cn_nameiop
) {
2077 cnp
->cn_flags
&= ~MAKEENTRY
;
2080 cnp
->cn_flags
&= ~MAKEENTRY
;
2090 newvp
= vnode_getparent(dvp
);
2096 error
= vnode_get(dvp
);
2100 nfs_node_lock_force(dnp
);
2101 if (fh
.fh_len
&& (dnp
->n_xid
<= xid
))
2102 nfs_loadattrcache(dnp
, &nvattr
, &xid
, 0);
2103 nfs_node_unlock(dnp
);
2105 ngflags
= (cnp
->cn_flags
& MAKEENTRY
) ? NG_MAKEENTRY
: 0;
2106 error
= nfs_nget(mp
, dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, ngflags
, &np
);
2110 nfs_node_unlock(np
);
2116 if (((cnp
->cn_nameiop
== CREATE
) || (cnp
->cn_nameiop
== RENAME
)) &&
2117 (flags
& ISLASTCN
) && (error
== ENOENT
)) {
2118 if (vnode_mount(dvp
) && vnode_vfsisrdonly(dvp
))
2121 error
= EJUSTRETURN
;
2124 if ((error
== ENOENT
) && (cnp
->cn_flags
& MAKEENTRY
) &&
2125 (cnp
->cn_nameiop
!= CREATE
) && negnamecache
) {
2126 /* add a negative entry in the name cache */
2127 nfs_node_lock_force(dnp
);
2128 cache_enter(dvp
, NULL
, cnp
);
2129 dnp
->n_flag
|= NNEGNCENTRIES
;
2130 nfs_node_unlock(dnp
);
2133 NVATTR_CLEANUP(&nvattr
);
2135 nfs_node_clear_busy(dnp
);
2136 if (error
&& *vpp
) {
2148 struct vnop_readlink_args
/* {
2149 struct vnodeop_desc *a_desc;
2152 vfs_context_t a_context;
2155 vfs_context_t ctx
= ap
->a_context
;
2156 nfsnode_t np
= VTONFS(ap
->a_vp
);
2157 struct nfsmount
*nmp
;
2158 int error
= 0, nfsvers
;
2160 uio_t uio
= ap
->a_uio
;
2161 struct nfsbuf
*bp
= NULL
;
2163 if (vnode_vtype(ap
->a_vp
) != VLNK
)
2166 if (uio_resid(uio
) == 0)
2168 if (uio_offset(uio
) < 0)
2171 nmp
= VTONMP(ap
->a_vp
);
2174 nfsvers
= nmp
->nm_vers
;
2176 /* nfs_getattr() will check changed and purge caches */
2177 if ((error
= nfs_getattr(np
, NULL
, ctx
, NGA_CACHED
))) {
2178 FSDBG(531, np
, 0xd1e0001, 0, error
);
2182 OSAddAtomic(1, &nfsstats
.biocache_readlinks
);
2183 error
= nfs_buf_get(np
, 0, NFS_MAXPATHLEN
, vfs_context_thread(ctx
), NBLK_READ
, &bp
);
2185 FSDBG(531, np
, 0xd1e0002, 0, error
);
2188 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) {
2189 OSAddAtomic(1, &nfsstats
.readlink_bios
);
2190 buflen
= bp
->nb_bufsize
;
2191 error
= nmp
->nm_funcs
->nf_readlink_rpc(np
, bp
->nb_data
, &buflen
, ctx
);
2193 SET(bp
->nb_flags
, NB_ERROR
);
2194 bp
->nb_error
= error
;
2196 bp
->nb_validoff
= 0;
2197 bp
->nb_validend
= buflen
;
2200 if (!error
&& (bp
->nb_validend
> 0))
2201 error
= uiomove(bp
->nb_data
, bp
->nb_validend
, uio
);
2202 FSDBG(531, np
, bp
->nb_validend
, 0, error
);
2203 nfs_buf_release(bp
, 1);
2208 * Do a readlink RPC.
2211 nfs3_readlink_rpc(nfsnode_t np
, char *buf
, uint32_t *buflenp
, vfs_context_t ctx
)
2213 struct nfsmount
*nmp
;
2214 int error
= 0, lockerror
= ENOENT
, nfsvers
, status
;
2217 struct nfsm_chain nmreq
, nmrep
;
2222 nfsvers
= nmp
->nm_vers
;
2223 nfsm_chain_null(&nmreq
);
2224 nfsm_chain_null(&nmrep
);
2226 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nfsvers
));
2227 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
2228 nfsm_chain_build_done(error
, &nmreq
);
2230 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_READLINK
, ctx
, NULL
, &nmrep
, &xid
, &status
);
2231 if ((lockerror
= nfs_node_lock(np
)))
2233 if (nfsvers
== NFS_VER3
)
2234 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
2237 nfsm_chain_get_32(error
, &nmrep
, len
);
2239 if ((nfsvers
== NFS_VER2
) && (len
> *buflenp
)) {
2243 if (len
>= *buflenp
) {
2244 if (np
->n_size
&& (np
->n_size
< *buflenp
))
2249 nfsm_chain_get_opaque(error
, &nmrep
, len
, buf
);
2254 nfs_node_unlock(np
);
2255 nfsm_chain_cleanup(&nmreq
);
2256 nfsm_chain_cleanup(&nmrep
);
2265 nfs_read_rpc(nfsnode_t np
, uio_t uio
, vfs_context_t ctx
)
2267 struct nfsmount
*nmp
;
2268 int error
= 0, nfsvers
, eof
= 0;
2269 size_t nmrsize
, len
, retlen
;
2272 struct nfsreq rq
, *req
= &rq
;
2273 uint32_t stategenid
= 0, restart
= 0;
2275 FSDBG_TOP(536, np
, uio_offset(uio
), uio_resid(uio
), 0);
2279 nfsvers
= nmp
->nm_vers
;
2280 nmrsize
= nmp
->nm_rsize
;
2282 txoffset
= uio_offset(uio
);
2283 tsiz
= uio_resid(uio
);
2284 if ((nfsvers
== NFS_VER2
) && ((uint64_t)(txoffset
+ tsiz
) > 0xffffffffULL
)) {
2285 FSDBG_BOT(536, np
, uio_offset(uio
), uio_resid(uio
), EFBIG
);
2290 len
= retlen
= (tsiz
> (user_ssize_t
)nmrsize
) ? nmrsize
: (size_t)tsiz
;
2291 FSDBG(536, np
, txoffset
, len
, 0);
2292 if (np
->n_flag
& NREVOKE
) {
2296 if (nmp
->nm_vers
>= NFS_VER4
)
2297 stategenid
= nmp
->nm_stategenid
;
2298 error
= nmp
->nm_funcs
->nf_read_rpc_async(np
, txoffset
, len
,
2299 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, &req
);
2301 error
= nmp
->nm_funcs
->nf_read_rpc_async_finish(np
, req
, uio
, &retlen
, &eof
);
2302 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
) &&
2303 (++restart
<= nfs_mount_state_max_restarts(nmp
))) { /* guard against no progress */
2304 lck_mtx_lock(&nmp
->nm_lock
);
2305 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
2306 NP(np
, "nfs_read_rpc: error %d, initiating recovery", error
);
2307 nfs_need_recover(nmp
, error
);
2309 lck_mtx_unlock(&nmp
->nm_lock
);
2310 if (np
->n_flag
& NREVOKE
) {
2313 if (error
== NFSERR_GRACE
)
2314 tsleep(&nmp
->nm_state
, (PZERO
-1), "nfsgrace", 2*hz
);
2315 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
)))
2323 if (nfsvers
!= NFS_VER2
) {
2324 if (eof
|| (retlen
== 0))
2326 } else if (retlen
< len
)
2330 FSDBG_BOT(536, np
, eof
, uio_resid(uio
), error
);
2335 nfs3_read_rpc_async(
2341 struct nfsreq_cbinfo
*cb
,
2342 struct nfsreq
**reqp
)
2344 struct nfsmount
*nmp
;
2345 int error
= 0, nfsvers
;
2346 struct nfsm_chain nmreq
;
2351 nfsvers
= nmp
->nm_vers
;
2353 nfsm_chain_null(&nmreq
);
2354 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nfsvers
) + 3 * NFSX_UNSIGNED
);
2355 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
2356 if (nfsvers
== NFS_VER3
) {
2357 nfsm_chain_add_64(error
, &nmreq
, offset
);
2358 nfsm_chain_add_32(error
, &nmreq
, len
);
2360 nfsm_chain_add_32(error
, &nmreq
, offset
);
2361 nfsm_chain_add_32(error
, &nmreq
, len
);
2362 nfsm_chain_add_32(error
, &nmreq
, 0);
2364 nfsm_chain_build_done(error
, &nmreq
);
2366 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC_READ
, thd
, cred
, NULL
, 0, cb
, reqp
);
2368 nfsm_chain_cleanup(&nmreq
);
2373 nfs3_read_rpc_async_finish(
2380 int error
= 0, lockerror
, nfsvers
, status
, eof
= 0;
2383 struct nfsmount
*nmp
;
2384 struct nfsm_chain nmrep
;
2388 nfs_request_async_cancel(req
);
2391 nfsvers
= nmp
->nm_vers
;
2393 nfsm_chain_null(&nmrep
);
2395 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
2396 if (error
== EINPROGRESS
) /* async request restarted */
2399 if ((lockerror
= nfs_node_lock(np
)))
2401 if (nfsvers
== NFS_VER3
)
2402 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
2405 if (nfsvers
== NFS_VER3
) {
2406 nfsm_chain_adv(error
, &nmrep
, NFSX_UNSIGNED
);
2407 nfsm_chain_get_32(error
, &nmrep
, eof
);
2409 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
2412 nfs_node_unlock(np
);
2413 nfsm_chain_get_32(error
, &nmrep
, retlen
);
2414 if ((nfsvers
== NFS_VER2
) && (retlen
> *lenp
))
2417 error
= nfsm_chain_get_uio(&nmrep
, MIN(retlen
, *lenp
), uio
);
2419 if (nfsvers
== NFS_VER3
) {
2420 if (!eof
&& !retlen
)
2422 } else if (retlen
< *lenp
) {
2427 *lenp
= MIN(retlen
, *lenp
);
2429 nfsm_chain_cleanup(&nmrep
);
2438 struct vnop_write_args
/* {
2439 struct vnodeop_desc *a_desc;
2443 vfs_context_t a_context;
2446 vfs_context_t ctx
= ap
->a_context
;
2447 uio_t uio
= ap
->a_uio
;
2448 vnode_t vp
= ap
->a_vp
;
2449 nfsnode_t np
= VTONFS(vp
);
2450 int ioflag
= ap
->a_ioflag
;
2452 struct nfsmount
*nmp
= VTONMP(vp
);
2455 int n
, on
, error
= 0;
2456 off_t boff
, start
, end
;
2458 char auio_buf
[ UIO_SIZEOF(1) ];
2462 FSDBG_TOP(515, np
, uio_offset(uio
), uio_resid(uio
), ioflag
);
2464 if (vnode_vtype(vp
) != VREG
) {
2465 FSDBG_BOT(515, np
, uio_offset(uio
), uio_resid(uio
), EIO
);
2469 thd
= vfs_context_thread(ctx
);
2470 cred
= vfs_context_ucred(ctx
);
2472 nfs_data_lock(np
, NFS_DATA_LOCK_SHARED
);
2474 if ((error
= nfs_node_lock(np
))) {
2475 nfs_data_unlock(np
);
2476 FSDBG_BOT(515, np
, uio_offset(uio
), uio_resid(uio
), error
);
2481 if (np
->n_flag
& NWRITEERR
) {
2482 error
= np
->n_error
;
2483 np
->n_flag
&= ~NWRITEERR
;
2485 if (np
->n_flag
& NNEEDINVALIDATE
) {
2486 np
->n_flag
&= ~NNEEDINVALIDATE
;
2487 nfs_node_unlock(np
);
2488 nfs_data_unlock(np
);
2489 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, ctx
, 1);
2490 nfs_data_lock(np
, NFS_DATA_LOCK_SHARED
);
2492 nfs_node_unlock(np
);
2497 biosize
= nmp
->nm_biosize
;
2499 if (ioflag
& (IO_APPEND
| IO_SYNC
)) {
2500 nfs_node_lock_force(np
);
2501 if (np
->n_flag
& NMODIFIED
) {
2502 NATTRINVALIDATE(np
);
2503 nfs_node_unlock(np
);
2504 nfs_data_unlock(np
);
2505 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
2506 nfs_data_lock(np
, NFS_DATA_LOCK_SHARED
);
2508 FSDBG(515, np
, uio_offset(uio
), 0x10bad01, error
);
2512 nfs_node_unlock(np
);
2514 if (ioflag
& IO_APPEND
) {
2515 nfs_data_unlock(np
);
2516 /* nfs_getattr() will check changed and purge caches */
2517 error
= nfs_getattr(np
, NULL
, ctx
, NGA_UNCACHED
);
2518 /* we'll be extending the file, so take the data lock exclusive */
2519 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
2521 FSDBG(515, np
, uio_offset(uio
), 0x10bad02, error
);
2524 uio_setoffset(uio
, np
->n_size
);
2527 if (uio_offset(uio
) < 0) {
2529 FSDBG_BOT(515, np
, uio_offset(uio
), 0xbad0ff, error
);
2532 if (uio_resid(uio
) == 0)
2535 if (((uio_offset(uio
) + uio_resid(uio
)) > (off_t
)np
->n_size
) && !(ioflag
& IO_APPEND
)) {
2536 /* it looks like we'll be extending the file, so take the data lock exclusive */
2537 nfs_data_unlock(np
);
2538 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
2542 OSAddAtomic(1, &nfsstats
.biocache_writes
);
2543 lbn
= uio_offset(uio
) / biosize
;
2544 on
= uio_offset(uio
) % biosize
;
2546 if (uio_resid(uio
) < n
)
2550 * Get a cache block for writing. The range to be written is
2551 * (off..off+n) within the block. We ensure that the block
2552 * either has no dirty region or that the given range is
2553 * contiguous with the existing dirty region.
2555 error
= nfs_buf_get(np
, lbn
, biosize
, thd
, NBLK_WRITE
, &bp
);
2558 /* map the block because we know we're going to write to it */
2561 if (ioflag
& IO_NOCACHE
)
2562 SET(bp
->nb_flags
, NB_NOCACHE
);
2564 if (!IS_VALID_CRED(bp
->nb_wcred
)) {
2565 kauth_cred_ref(cred
);
2566 bp
->nb_wcred
= cred
;
2570 * If there's already a dirty range AND dirty pages in this block we
2571 * need to send a commit AND write the dirty pages before continuing.
2573 * If there's already a dirty range OR dirty pages in this block
2574 * and the new write range is not contiguous with the existing range,
2575 * then force the buffer to be written out now.
2576 * (We used to just extend the dirty range to cover the valid,
2577 * but unwritten, data in between also. But writing ranges
2578 * of data that weren't actually written by an application
2579 * risks overwriting some other client's data with stale data
2580 * that's just masquerading as new written data.)
2582 if (bp
->nb_dirtyend
> 0) {
2583 if (on
> bp
->nb_dirtyend
|| (on
+ n
) < bp
->nb_dirtyoff
|| bp
->nb_dirty
) {
2584 FSDBG(515, np
, uio_offset(uio
), bp
, 0xd15c001);
2585 /* write/commit buffer "synchronously" */
2586 /* (NB_STABLE indicates that data writes should be FILESYNC) */
2587 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
));
2588 SET(bp
->nb_flags
, (NB_ASYNC
| NB_STABLE
));
2589 error
= nfs_buf_write(bp
);
2594 } else if (bp
->nb_dirty
) {
2595 int firstpg
, lastpg
;
2597 /* calculate write range pagemask */
2598 firstpg
= on
/PAGE_SIZE
;
2599 lastpg
= (on
+n
-1)/PAGE_SIZE
;
2600 pagemask
= ((1 << (lastpg
+1)) - 1) & ~((1 << firstpg
) - 1);
2601 /* check if there are dirty pages outside the write range */
2602 if (bp
->nb_dirty
& ~pagemask
) {
2603 FSDBG(515, np
, uio_offset(uio
), bp
, 0xd15c002);
2604 /* write/commit buffer "synchronously" */
2605 /* (NB_STABLE indicates that data writes should be FILESYNC) */
2606 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
));
2607 SET(bp
->nb_flags
, (NB_ASYNC
| NB_STABLE
));
2608 error
= nfs_buf_write(bp
);
2613 /* if the first or last pages are already dirty */
2614 /* make sure that the dirty range encompasses those pages */
2615 if (NBPGDIRTY(bp
,firstpg
) || NBPGDIRTY(bp
,lastpg
)) {
2616 FSDBG(515, np
, uio_offset(uio
), bp
, 0xd15c003);
2617 bp
->nb_dirtyoff
= min(on
, firstpg
* PAGE_SIZE
);
2618 if (NBPGDIRTY(bp
,lastpg
)) {
2619 bp
->nb_dirtyend
= (lastpg
+1) * PAGE_SIZE
;
2621 if (NBOFF(bp
) + bp
->nb_dirtyend
> (off_t
)np
->n_size
) {
2622 bp
->nb_dirtyend
= np
->n_size
- NBOFF(bp
);
2623 if (bp
->nb_dirtyoff
>= bp
->nb_dirtyend
)
2624 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
2627 bp
->nb_dirtyend
= on
+n
;
2632 * Are we extending the size of the file with this write?
2633 * If so, update file size now that we have the block.
2634 * If there was a partial buf at the old eof, validate
2635 * and zero the new bytes.
2637 if ((uio_offset(uio
) + n
) > (off_t
)np
->n_size
) {
2638 struct nfsbuf
*eofbp
= NULL
;
2639 daddr64_t eofbn
= np
->n_size
/ biosize
;
2640 int eofoff
= np
->n_size
% biosize
;
2641 int neweofoff
= (uio_offset(uio
) + n
) % biosize
;
2643 FSDBG(515, 0xb1ffa000, uio_offset(uio
) + n
, eofoff
, neweofoff
);
2645 if (eofoff
&& (eofbn
< lbn
) &&
2646 ((error
= nfs_buf_get(np
, eofbn
, biosize
, thd
, NBLK_WRITE
|NBLK_ONLYVALID
, &eofbp
))))
2649 /* if we're extending within the same last block */
2650 /* and the block is flagged as being cached... */
2651 if ((lbn
== eofbn
) && ISSET(bp
->nb_flags
, NB_CACHE
)) {
2652 /* ...check that all pages in buffer are valid */
2653 int endpg
= ((neweofoff
? neweofoff
: biosize
) - 1)/PAGE_SIZE
;
2655 /* pagemask only has to extend to last page being written to */
2656 pagemask
= (1 << (endpg
+1)) - 1;
2657 FSDBG(515, 0xb1ffa001, bp
->nb_valid
, pagemask
, 0);
2658 if ((bp
->nb_valid
& pagemask
) != pagemask
) {
2659 /* zerofill any hole */
2660 if (on
> bp
->nb_validend
) {
2662 for (i
=bp
->nb_validend
/PAGE_SIZE
; i
<= (on
- 1)/PAGE_SIZE
; i
++)
2663 NBPGVALID_SET(bp
, i
);
2665 FSDBG(516, bp
, bp
->nb_validend
, on
- bp
->nb_validend
, 0xf01e);
2666 bzero((char *)bp
->nb_data
+ bp
->nb_validend
,
2667 on
- bp
->nb_validend
);
2669 /* zerofill any trailing data in the last page */
2672 FSDBG(516, bp
, neweofoff
, PAGE_SIZE
- (neweofoff
& PAGE_MASK
), 0xe0f);
2673 bzero((char *)bp
->nb_data
+ neweofoff
,
2674 PAGE_SIZE
- (neweofoff
& PAGE_MASK
));
2678 np
->n_size
= uio_offset(uio
) + n
;
2679 nfs_node_lock_force(np
);
2680 CLR(np
->n_flag
, NUPDATESIZE
);
2681 np
->n_flag
|= NMODIFIED
;
2682 nfs_node_unlock(np
);
2683 FSDBG(516, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0001);
2684 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX errors */
2687 * We may need to zero any previously invalid data
2688 * after the old EOF in the previous EOF buffer.
2690 * For the old last page, don't zero bytes if there
2691 * are invalid bytes in that page (i.e. the page isn't
2693 * For pages after the old last page, zero them and
2694 * mark them as valid.
2698 if (ioflag
& IO_NOCACHE
)
2699 SET(eofbp
->nb_flags
, NB_NOCACHE
);
2701 FSDBG(516, eofbp
, eofoff
, biosize
- eofoff
, 0xe0fff01e);
2703 i
= eofoff
/PAGE_SIZE
;
2704 while (eofoff
< biosize
) {
2705 int poff
= eofoff
& PAGE_MASK
;
2706 if (!poff
|| NBPGVALID(eofbp
,i
)) {
2707 bzero(d
+ eofoff
, PAGE_SIZE
- poff
);
2708 NBPGVALID_SET(eofbp
, i
);
2710 if (bp
->nb_validend
== eofoff
)
2711 bp
->nb_validend
+= PAGE_SIZE
- poff
;
2712 eofoff
+= PAGE_SIZE
- poff
;
2715 nfs_buf_release(eofbp
, 1);
2719 * If dirtyend exceeds file size, chop it down. This should
2720 * not occur unless there is a race.
2722 if (NBOFF(bp
) + bp
->nb_dirtyend
> (off_t
)np
->n_size
) {
2723 bp
->nb_dirtyend
= np
->n_size
- NBOFF(bp
);
2724 if (bp
->nb_dirtyoff
>= bp
->nb_dirtyend
)
2725 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
2728 * UBC doesn't handle partial pages, so we need to make sure
2729 * that any pages left in the page cache are completely valid.
2731 * Writes that are smaller than a block are delayed if they
2732 * don't extend to the end of the block.
2734 * If the block isn't (completely) cached, we may need to read
2735 * in some parts of pages that aren't covered by the write.
2736 * If the write offset (on) isn't page aligned, we'll need to
2737 * read the start of the first page being written to. Likewise,
2738 * if the offset of the end of the write (on+n) isn't page aligned,
2739 * we'll need to read the end of the last page being written to.
2742 * We don't want to read anything we're just going to write over.
2743 * We don't want to read anything we're just going drop when the
2744 * I/O is complete (i.e. don't do reads for NOCACHE requests).
2745 * We don't want to issue multiple I/Os if we don't have to
2746 * (because they're synchronous rpcs).
2747 * We don't want to read anything we already have modified in the
2750 if (!ISSET(bp
->nb_flags
, NB_CACHE
) && (n
< biosize
)) {
2751 int firstpg
, lastpg
, dirtypg
;
2752 int firstpgoff
, lastpgoff
;
2754 firstpg
= on
/PAGE_SIZE
;
2755 firstpgoff
= on
& PAGE_MASK
;
2756 lastpg
= (on
+n
-1)/PAGE_SIZE
;
2757 lastpgoff
= (on
+n
) & PAGE_MASK
;
2758 if (firstpgoff
&& !NBPGVALID(bp
,firstpg
)) {
2759 /* need to read start of first page */
2760 start
= firstpg
* PAGE_SIZE
;
2761 end
= start
+ firstpgoff
;
2763 if (lastpgoff
&& !NBPGVALID(bp
,lastpg
)) {
2764 /* need to read end of last page */
2766 start
= (lastpg
* PAGE_SIZE
) + lastpgoff
;
2767 end
= (lastpg
+ 1) * PAGE_SIZE
;
2769 if (ISSET(bp
->nb_flags
, NB_NOCACHE
)) {
2771 * For nocache writes, if there is any partial page at the
2772 * start or end of the write range, then we do the write
2773 * synchronously to make sure that we can drop the data
2774 * from the cache as soon as the WRITE finishes. Normally,
2775 * we would do an unstable write and not drop the data until
2776 * it was committed. But doing that here would risk allowing
2777 * invalid data to be read from the cache between the WRITE
2779 * (NB_STABLE indicates that data writes should be FILESYNC)
2782 SET(bp
->nb_flags
, NB_STABLE
);
2786 /* need to read the data in range: start...end-1 */
2788 /* first, check for dirty pages in between */
2789 /* if there are, we'll have to do two reads because */
2790 /* we don't want to overwrite the dirty pages. */
2791 for (dirtypg
=start
/PAGE_SIZE
; dirtypg
<= (end
-1)/PAGE_SIZE
; dirtypg
++)
2792 if (NBPGDIRTY(bp
,dirtypg
))
2795 /* if start is at beginning of page, try */
2796 /* to get any preceeding pages as well. */
2797 if (!(start
& PAGE_MASK
)) {
2798 /* stop at next dirty/valid page or start of block */
2799 for (; start
> 0; start
-=PAGE_SIZE
)
2800 if (NBPGVALID(bp
,((start
-1)/PAGE_SIZE
)))
2805 /* setup uio for read(s) */
2807 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
2808 &auio_buf
, sizeof(auio_buf
));
2810 if (dirtypg
<= (end
-1)/PAGE_SIZE
) {
2811 /* there's a dirty page in the way, so just do two reads */
2812 /* we'll read the preceding data here */
2813 uio_reset(auio
, boff
+ start
, UIO_SYSSPACE
, UIO_READ
);
2814 uio_addiov(auio
, CAST_USER_ADDR_T(bp
->nb_data
+ start
), on
- start
);
2815 error
= nfs_read_rpc(np
, auio
, ctx
);
2817 /* couldn't read the data, so treat buffer as synchronous NOCACHE */
2818 SET(bp
->nb_flags
, (NB_NOCACHE
|NB_STABLE
));
2821 if (uio_resid(auio
) > 0) {
2822 FSDBG(516, bp
, (caddr_t
)uio_curriovbase(auio
) - bp
->nb_data
, uio_resid(auio
), 0xd00dee01);
2823 bzero(CAST_DOWN(caddr_t
, uio_curriovbase(auio
)), uio_resid(auio
));
2826 /* update validoff/validend if necessary */
2827 if ((bp
->nb_validoff
< 0) || (bp
->nb_validoff
> start
))
2828 bp
->nb_validoff
= start
;
2829 if ((bp
->nb_validend
< 0) || (bp
->nb_validend
< on
))
2830 bp
->nb_validend
= on
;
2831 if ((off_t
)np
->n_size
> boff
+ bp
->nb_validend
)
2832 bp
->nb_validend
= min(np
->n_size
- (boff
+ start
), biosize
);
2833 /* validate any pages before the write offset */
2834 for (; start
< on
/PAGE_SIZE
; start
+=PAGE_SIZE
)
2835 NBPGVALID_SET(bp
, start
/PAGE_SIZE
);
2837 /* adjust start to read any trailing data */
2841 /* if end is at end of page, try to */
2842 /* get any following pages as well. */
2843 if (!(end
& PAGE_MASK
)) {
2844 /* stop at next valid page or end of block */
2845 for (; end
< biosize
; end
+=PAGE_SIZE
)
2846 if (NBPGVALID(bp
,end
/PAGE_SIZE
))
2850 if (((boff
+start
) >= (off_t
)np
->n_size
) ||
2851 ((start
>= on
) && ((boff
+ on
+ n
) >= (off_t
)np
->n_size
))) {
2853 * Either this entire read is beyond the current EOF
2854 * or the range that we won't be modifying (on+n...end)
2855 * is all beyond the current EOF.
2856 * No need to make a trip across the network to
2857 * read nothing. So, just zero the buffer instead.
2859 FSDBG(516, bp
, start
, end
- start
, 0xd00dee00);
2860 bzero(bp
->nb_data
+ start
, end
- start
);
2863 /* now we'll read the (rest of the) data */
2864 uio_reset(auio
, boff
+ start
, UIO_SYSSPACE
, UIO_READ
);
2865 uio_addiov(auio
, CAST_USER_ADDR_T(bp
->nb_data
+ start
), end
- start
);
2866 error
= nfs_read_rpc(np
, auio
, ctx
);
2868 /* couldn't read the data, so treat buffer as synchronous NOCACHE */
2869 SET(bp
->nb_flags
, (NB_NOCACHE
|NB_STABLE
));
2872 if (uio_resid(auio
) > 0) {
2873 FSDBG(516, bp
, (caddr_t
)uio_curriovbase(auio
) - bp
->nb_data
, uio_resid(auio
), 0xd00dee02);
2874 bzero(CAST_DOWN(caddr_t
, uio_curriovbase(auio
)), uio_resid(auio
));
2878 /* update validoff/validend if necessary */
2879 if ((bp
->nb_validoff
< 0) || (bp
->nb_validoff
> start
))
2880 bp
->nb_validoff
= start
;
2881 if ((bp
->nb_validend
< 0) || (bp
->nb_validend
< end
))
2882 bp
->nb_validend
= end
;
2883 if ((off_t
)np
->n_size
> boff
+ bp
->nb_validend
)
2884 bp
->nb_validend
= min(np
->n_size
- (boff
+ start
), biosize
);
2885 /* validate any pages before the write offset's page */
2886 for (; start
< (off_t
)trunc_page_32(on
); start
+=PAGE_SIZE
)
2887 NBPGVALID_SET(bp
, start
/PAGE_SIZE
);
2888 /* validate any pages after the range of pages being written to */
2889 for (; (end
- 1) > (off_t
)round_page_32(on
+n
-1); end
-=PAGE_SIZE
)
2890 NBPGVALID_SET(bp
, (end
-1)/PAGE_SIZE
);
2892 /* Note: pages being written to will be validated when written */
2897 if (ISSET(bp
->nb_flags
, NB_ERROR
)) {
2898 error
= bp
->nb_error
;
2899 nfs_buf_release(bp
, 1);
2903 nfs_node_lock_force(np
);
2904 np
->n_flag
|= NMODIFIED
;
2905 nfs_node_unlock(np
);
2908 error
= uiomove((char *)bp
->nb_data
+ on
, n
, uio
);
2910 SET(bp
->nb_flags
, NB_ERROR
);
2911 nfs_buf_release(bp
, 1);
2915 /* validate any pages written to */
2916 start
= on
& ~PAGE_MASK
;
2917 for (; start
< on
+n
; start
+= PAGE_SIZE
) {
2918 NBPGVALID_SET(bp
, start
/PAGE_SIZE
);
2920 * This may seem a little weird, but we don't actually set the
2921 * dirty bits for writes. This is because we keep the dirty range
2922 * in the nb_dirtyoff/nb_dirtyend fields. Also, particularly for
2923 * delayed writes, when we give the pages back to the VM we don't
2924 * want to keep them marked dirty, because when we later write the
2925 * buffer we won't be able to tell which pages were written dirty
2926 * and which pages were mmapped and dirtied.
2929 if (bp
->nb_dirtyend
> 0) {
2930 bp
->nb_dirtyoff
= min(on
, bp
->nb_dirtyoff
);
2931 bp
->nb_dirtyend
= max((on
+ n
), bp
->nb_dirtyend
);
2933 bp
->nb_dirtyoff
= on
;
2934 bp
->nb_dirtyend
= on
+ n
;
2936 if (bp
->nb_validend
<= 0 || bp
->nb_validend
< bp
->nb_dirtyoff
||
2937 bp
->nb_validoff
> bp
->nb_dirtyend
) {
2938 bp
->nb_validoff
= bp
->nb_dirtyoff
;
2939 bp
->nb_validend
= bp
->nb_dirtyend
;
2941 bp
->nb_validoff
= min(bp
->nb_validoff
, bp
->nb_dirtyoff
);
2942 bp
->nb_validend
= max(bp
->nb_validend
, bp
->nb_dirtyend
);
2944 if (!ISSET(bp
->nb_flags
, NB_CACHE
))
2945 nfs_buf_normalize_valid_range(np
, bp
);
2948 * Since this block is being modified, it must be written
2949 * again and not just committed.
2951 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
2952 nfs_node_lock_force(np
);
2953 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
2954 np
->n_needcommitcnt
--;
2955 CHECK_NEEDCOMMITCNT(np
);
2957 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
2958 nfs_node_unlock(np
);
2961 if (ioflag
& IO_SYNC
) {
2962 error
= nfs_buf_write(bp
);
2965 } else if (((n
+ on
) == biosize
) || (ioflag
& IO_APPEND
) ||
2966 (ioflag
& IO_NOCACHE
) || ISSET(bp
->nb_flags
, NB_NOCACHE
)) {
2967 SET(bp
->nb_flags
, NB_ASYNC
);
2968 error
= nfs_buf_write(bp
);
2972 /* If the block wasn't already delayed: charge for the write */
2973 if (!ISSET(bp
->nb_flags
, NB_DELWRI
)) {
2974 proc_t p
= vfs_context_proc(ctx
);
2975 if (p
&& p
->p_stats
)
2976 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_oublock
);
2978 nfs_buf_write_delayed(bp
);
2980 if (np
->n_needcommitcnt
>= NFS_A_LOT_OF_NEEDCOMMITS
)
2981 nfs_flushcommits(np
, 1);
2983 } while (uio_resid(uio
) > 0 && n
> 0);
2986 nfs_node_lock_force(np
);
2988 nfs_node_unlock(np
);
2989 nfs_data_unlock(np
);
2990 FSDBG_BOT(515, np
, uio_offset(uio
), uio_resid(uio
), error
);
3006 return nfs_write_rpc2(np
, uio
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
), iomodep
, wverfp
);
3018 struct nfsmount
*nmp
;
3019 int error
= 0, nfsvers
;
3020 int backup
, wverfset
, commit
, committed
;
3021 uint64_t wverf
= 0, wverf2
;
3022 size_t nmwsize
, totalsize
, tsiz
, len
, rlen
;
3023 struct nfsreq rq
, *req
= &rq
;
3024 uint32_t stategenid
= 0, vrestart
= 0, restart
= 0;
3027 /* XXX limitation based on need to back up uio on short write */
3028 if (uio_iovcnt(uio
) != 1)
3029 panic("nfs3_write_rpc: iovcnt > 1");
3031 FSDBG_TOP(537, np
, uio_offset(uio
), uio_resid(uio
), *iomodep
);
3035 nfsvers
= nmp
->nm_vers
;
3036 nmwsize
= nmp
->nm_wsize
;
3039 committed
= NFS_WRITE_FILESYNC
;
3041 totalsize
= tsiz
= uio_resid(uio
);
3042 if ((nfsvers
== NFS_VER2
) && ((uint64_t)(uio_offset(uio
) + tsiz
) > 0xffffffffULL
)) {
3043 FSDBG_BOT(537, np
, uio_offset(uio
), uio_resid(uio
), EFBIG
);
3048 len
= (tsiz
> nmwsize
) ? nmwsize
: tsiz
;
3049 FSDBG(537, np
, uio_offset(uio
), len
, 0);
3050 if (np
->n_flag
& NREVOKE
) {
3054 if (nmp
->nm_vers
>= NFS_VER4
)
3055 stategenid
= nmp
->nm_stategenid
;
3056 error
= nmp
->nm_funcs
->nf_write_rpc_async(np
, uio
, len
, thd
, cred
, *iomodep
, NULL
, &req
);
3058 error
= nmp
->nm_funcs
->nf_write_rpc_async_finish(np
, req
, &commit
, &rlen
, &wverf2
);
3062 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
) &&
3063 (++restart
<= nfs_mount_state_max_restarts(nmp
))) { /* guard against no progress */
3064 lck_mtx_lock(&nmp
->nm_lock
);
3065 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
3066 NP(np
, "nfs_write_rpc: error %d, initiating recovery", error
);
3067 nfs_need_recover(nmp
, error
);
3069 lck_mtx_unlock(&nmp
->nm_lock
);
3070 if (np
->n_flag
& NREVOKE
) {
3073 if (error
== NFSERR_GRACE
)
3074 tsleep(&nmp
->nm_state
, (PZERO
-1), "nfsgrace", 2*hz
);
3075 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
)))
3081 if (nfsvers
== NFS_VER2
) {
3086 /* check for a short write */
3088 backup
= len
- rlen
;
3089 uio_pushback(uio
, backup
);
3093 /* return lowest commit level returned */
3094 if (commit
< committed
)
3099 /* check write verifier */
3103 } else if (wverf
!= wverf2
) {
3104 /* verifier changed, so we need to restart all the writes */
3105 if (++vrestart
> 100) {
3106 /* give up after too many restarts */
3110 backup
= totalsize
- tsiz
;
3111 uio_pushback(uio
, backup
);
3112 committed
= NFS_WRITE_FILESYNC
;
3117 if (wverfset
&& wverfp
)
3119 *iomodep
= committed
;
3121 uio_setresid(uio
, tsiz
);
3122 FSDBG_BOT(537, np
, committed
, uio_resid(uio
), error
);
3127 nfs3_write_rpc_async(
3134 struct nfsreq_cbinfo
*cb
,
3135 struct nfsreq
**reqp
)
3137 struct nfsmount
*nmp
;
3139 int error
= 0, nfsvers
;
3140 struct nfsm_chain nmreq
;
3145 nfsvers
= nmp
->nm_vers
;
3147 /* for async mounts, don't bother sending sync write requests */
3148 if ((iomode
!= NFS_WRITE_UNSTABLE
) && nfs_allow_async
&&
3149 ((mp
= NFSTOMP(np
))) && (vfs_flags(mp
) & MNT_ASYNC
))
3150 iomode
= NFS_WRITE_UNSTABLE
;
3152 nfsm_chain_null(&nmreq
);
3153 nfsm_chain_build_alloc_init(error
, &nmreq
,
3154 NFSX_FH(nfsvers
) + 5 * NFSX_UNSIGNED
+ nfsm_rndup(len
));
3155 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
3156 if (nfsvers
== NFS_VER3
) {
3157 nfsm_chain_add_64(error
, &nmreq
, uio_offset(uio
));
3158 nfsm_chain_add_32(error
, &nmreq
, len
);
3159 nfsm_chain_add_32(error
, &nmreq
, iomode
);
3161 nfsm_chain_add_32(error
, &nmreq
, 0);
3162 nfsm_chain_add_32(error
, &nmreq
, uio_offset(uio
));
3163 nfsm_chain_add_32(error
, &nmreq
, 0);
3165 nfsm_chain_add_32(error
, &nmreq
, len
);
3167 error
= nfsm_chain_add_uio(&nmreq
, uio
, len
);
3168 nfsm_chain_build_done(error
, &nmreq
);
3170 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC_WRITE
, thd
, cred
, NULL
, 0, cb
, reqp
);
3172 nfsm_chain_cleanup(&nmreq
);
3177 nfs3_write_rpc_async_finish(
3184 struct nfsmount
*nmp
;
3185 int error
= 0, lockerror
= ENOENT
, nfsvers
, status
;
3186 int updatemtime
= 0, wccpostattr
= 0, rlen
, committed
= NFS_WRITE_FILESYNC
;
3187 u_int64_t xid
, wverf
;
3189 struct nfsm_chain nmrep
;
3193 nfs_request_async_cancel(req
);
3196 nfsvers
= nmp
->nm_vers
;
3198 nfsm_chain_null(&nmrep
);
3200 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
3201 if (error
== EINPROGRESS
) /* async request restarted */
3206 if (!error
&& (lockerror
= nfs_node_lock(np
)))
3208 if (nfsvers
== NFS_VER3
) {
3209 struct timespec premtime
= { 0, 0 };
3210 nfsm_chain_get_wcc_data(error
, &nmrep
, np
, &premtime
, &wccpostattr
, &xid
);
3211 if (nfstimespeccmp(&np
->n_mtime
, &premtime
, ==))
3215 nfsm_chain_get_32(error
, &nmrep
, rlen
);
3220 nfsm_chain_get_32(error
, &nmrep
, committed
);
3221 nfsm_chain_get_64(error
, &nmrep
, wverf
);
3225 lck_mtx_lock(&nmp
->nm_lock
);
3226 if (!(nmp
->nm_state
& NFSSTA_HASWRITEVERF
)) {
3227 nmp
->nm_verf
= wverf
;
3228 nmp
->nm_state
|= NFSSTA_HASWRITEVERF
;
3229 } else if (nmp
->nm_verf
!= wverf
) {
3230 nmp
->nm_verf
= wverf
;
3232 lck_mtx_unlock(&nmp
->nm_lock
);
3236 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
3240 NFS_CHANGED_UPDATE(nfsvers
, np
, &np
->n_vattr
);
3243 nfs_node_unlock(np
);
3244 nfsm_chain_cleanup(&nmrep
);
3245 if ((committed
!= NFS_WRITE_FILESYNC
) && nfs_allow_async
&&
3246 ((mp
= NFSTOMP(np
))) && (vfs_flags(mp
) & MNT_ASYNC
))
3247 committed
= NFS_WRITE_FILESYNC
;
3248 *iomodep
= committed
;
3253 * NFS mknod vnode op
3255 * For NFS v2 this is a kludge. Use a create RPC but with the IFMT bits of the
3256 * mode set to specify the file type and the size field for rdev.
3260 struct vnop_mknod_args
/* {
3261 struct vnodeop_desc *a_desc;
3264 struct componentname *a_cnp;
3265 struct vnode_attr *a_vap;
3266 vfs_context_t a_context;
3269 vnode_t dvp
= ap
->a_dvp
;
3270 vnode_t
*vpp
= ap
->a_vpp
;
3271 struct componentname
*cnp
= ap
->a_cnp
;
3272 struct vnode_attr
*vap
= ap
->a_vap
;
3273 vfs_context_t ctx
= ap
->a_context
;
3274 vnode_t newvp
= NULL
;
3275 nfsnode_t np
= NULL
;
3276 struct nfsmount
*nmp
;
3277 nfsnode_t dnp
= VTONFS(dvp
);
3278 struct nfs_vattr nvattr
;
3280 int error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0;
3281 struct timespec premtime
= { 0, 0 };
3283 u_int64_t xid
, dxid
;
3284 int nfsvers
, gotuid
, gotgid
;
3285 struct nfsm_chain nmreq
, nmrep
;
3286 struct nfsreq rq
, *req
= &rq
;
3291 nfsvers
= nmp
->nm_vers
;
3293 if (!VATTR_IS_ACTIVE(vap
, va_type
))
3295 if (vap
->va_type
== VCHR
|| vap
->va_type
== VBLK
) {
3296 if (!VATTR_IS_ACTIVE(vap
, va_rdev
))
3298 rdev
= vap
->va_rdev
;
3299 } else if (vap
->va_type
== VFIFO
|| vap
->va_type
== VSOCK
)
3304 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
))
3305 return (ENAMETOOLONG
);
3307 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
3309 VATTR_SET_SUPPORTED(vap
, va_mode
);
3310 VATTR_SET_SUPPORTED(vap
, va_uid
);
3311 VATTR_SET_SUPPORTED(vap
, va_gid
);
3312 VATTR_SET_SUPPORTED(vap
, va_data_size
);
3313 VATTR_SET_SUPPORTED(vap
, va_access_time
);
3314 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
3315 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
3316 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
3318 nfsm_chain_null(&nmreq
);
3319 nfsm_chain_null(&nmrep
);
3321 nfsm_chain_build_alloc_init(error
, &nmreq
,
3322 NFSX_FH(nfsvers
) + 4 * NFSX_UNSIGNED
+
3323 nfsm_rndup(cnp
->cn_namelen
) + NFSX_SATTR(nfsvers
));
3324 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
3325 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
3326 if (nfsvers
== NFS_VER3
) {
3327 nfsm_chain_add_32(error
, &nmreq
, vtonfs_type(vap
->va_type
, nfsvers
));
3328 nfsm_chain_add_v3sattr(error
, &nmreq
, vap
);
3329 if (vap
->va_type
== VCHR
|| vap
->va_type
== VBLK
) {
3330 nfsm_chain_add_32(error
, &nmreq
, major(vap
->va_rdev
));
3331 nfsm_chain_add_32(error
, &nmreq
, minor(vap
->va_rdev
));
3334 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, rdev
);
3336 nfsm_chain_build_done(error
, &nmreq
);
3338 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
3341 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_MKNOD
,
3342 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
3344 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
3346 if ((lockerror
= nfs_node_lock(dnp
)))
3348 /* XXX no EEXIST kludge here? */
3350 if (!error
&& !status
) {
3351 if (dnp
->n_flag
& NNEGNCENTRIES
) {
3352 dnp
->n_flag
&= ~NNEGNCENTRIES
;
3353 cache_purge_negatives(dvp
);
3355 error
= nfsm_chain_get_fh_attr(&nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
3357 if (nfsvers
== NFS_VER3
)
3358 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
3362 nfsm_chain_cleanup(&nmreq
);
3363 nfsm_chain_cleanup(&nmrep
);
3366 dnp
->n_flag
|= NMODIFIED
;
3367 /* if directory hadn't changed, update namecache mtime */
3368 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==))
3369 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
3370 nfs_node_unlock(dnp
);
3371 /* nfs_getattr() will check changed and purge caches */
3372 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
3375 if (!error
&& fh
.fh_len
)
3376 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
3378 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
3382 nfs_node_clear_busy(dnp
);
3384 if (!error
&& (gotuid
|| gotgid
) &&
3385 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
3386 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
3387 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
3388 /* clear ID bits if server didn't use them (or we can't tell) */
3389 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
3390 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
3394 nfs_node_unlock(np
);
3399 nfs_node_unlock(np
);
3404 static uint32_t create_verf
;
3406 * NFS file create call
3410 struct vnop_create_args
/* {
3411 struct vnodeop_desc *a_desc;
3414 struct componentname *a_cnp;
3415 struct vnode_attr *a_vap;
3416 vfs_context_t a_context;
3419 vfs_context_t ctx
= ap
->a_context
;
3420 vnode_t dvp
= ap
->a_dvp
;
3421 struct vnode_attr
*vap
= ap
->a_vap
;
3422 struct componentname
*cnp
= ap
->a_cnp
;
3423 struct nfs_vattr nvattr
;
3425 nfsnode_t np
= NULL
;
3426 struct nfsmount
*nmp
;
3427 nfsnode_t dnp
= VTONFS(dvp
);
3428 vnode_t newvp
= NULL
;
3429 int error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0, fmode
= 0;
3430 struct timespec premtime
= { 0, 0 };
3431 int nfsvers
, gotuid
, gotgid
;
3432 u_int64_t xid
, dxid
;
3434 struct nfsm_chain nmreq
, nmrep
;
3435 struct nfsreq rq
, *req
= &rq
;
3436 struct nfs_dulookup dul
;
3441 nfsvers
= nmp
->nm_vers
;
3443 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
))
3444 return (ENAMETOOLONG
);
3446 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
3448 VATTR_SET_SUPPORTED(vap
, va_mode
);
3449 VATTR_SET_SUPPORTED(vap
, va_uid
);
3450 VATTR_SET_SUPPORTED(vap
, va_gid
);
3451 VATTR_SET_SUPPORTED(vap
, va_data_size
);
3452 VATTR_SET_SUPPORTED(vap
, va_access_time
);
3453 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
3454 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
3455 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
3457 if (vap
->va_vaflags
& VA_EXCLUSIVE
) {
3459 if (!VATTR_IS_ACTIVE(vap
, va_access_time
) || !VATTR_IS_ACTIVE(vap
, va_modify_time
))
3460 vap
->va_vaflags
|= VA_UTIMES_NULL
;
3464 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
3465 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
3467 nfsm_chain_null(&nmreq
);
3468 nfsm_chain_null(&nmrep
);
3470 nfsm_chain_build_alloc_init(error
, &nmreq
,
3471 NFSX_FH(nfsvers
) + 2 * NFSX_UNSIGNED
+
3472 nfsm_rndup(cnp
->cn_namelen
) + NFSX_SATTR(nfsvers
));
3473 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
3474 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
3475 if (nfsvers
== NFS_VER3
) {
3476 if (fmode
& O_EXCL
) {
3477 nfsm_chain_add_32(error
, &nmreq
, NFS_CREATE_EXCLUSIVE
);
3478 lck_rw_lock_shared(in_ifaddr_rwlock
);
3479 if (!TAILQ_EMPTY(&in_ifaddrhead
))
3480 val
= IA_SIN(in_ifaddrhead
.tqh_first
)->sin_addr
.s_addr
;
3483 lck_rw_done(in_ifaddr_rwlock
);
3484 nfsm_chain_add_32(error
, &nmreq
, val
);
3486 nfsm_chain_add_32(error
, &nmreq
, create_verf
);
3488 nfsm_chain_add_32(error
, &nmreq
, NFS_CREATE_UNCHECKED
);
3489 nfsm_chain_add_v3sattr(error
, &nmreq
, vap
);
3492 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, 0);
3494 nfsm_chain_build_done(error
, &nmreq
);
3497 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_CREATE
,
3498 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
3500 nfs_dulookup_start(&dul
, dnp
, ctx
);
3501 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
3504 if ((lockerror
= nfs_node_lock(dnp
)))
3507 if (!error
&& !status
) {
3508 if (dnp
->n_flag
& NNEGNCENTRIES
) {
3509 dnp
->n_flag
&= ~NNEGNCENTRIES
;
3510 cache_purge_negatives(dvp
);
3512 error
= nfsm_chain_get_fh_attr(&nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
3514 if (nfsvers
== NFS_VER3
)
3515 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
3519 nfsm_chain_cleanup(&nmreq
);
3520 nfsm_chain_cleanup(&nmrep
);
3523 dnp
->n_flag
|= NMODIFIED
;
3524 /* if directory hadn't changed, update namecache mtime */
3525 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==))
3526 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
3527 nfs_node_unlock(dnp
);
3528 /* nfs_getattr() will check changed and purge caches */
3529 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
3532 if (!error
&& fh
.fh_len
)
3533 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
3535 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
3539 nfs_dulookup_finish(&dul
, dnp
, ctx
);
3541 nfs_node_clear_busy(dnp
);
3544 if ((nfsvers
== NFS_VER3
) && (fmode
& O_EXCL
) && (error
== NFSERR_NOTSUPP
)) {
3549 nfs_node_unlock(np
);
3552 } else if ((nfsvers
== NFS_VER3
) && (fmode
& O_EXCL
)) {
3553 nfs_node_unlock(np
);
3554 error
= nfs3_setattr_rpc(np
, vap
, ctx
);
3555 if (error
&& (gotuid
|| gotgid
)) {
3556 /* it's possible the server didn't like our attempt to set IDs. */
3557 /* so, let's try it again without those */
3558 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
3559 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
3560 error
= nfs3_setattr_rpc(np
, vap
, ctx
);
3565 nfs_node_lock_force(np
);
3569 if (!error
&& (gotuid
|| gotgid
) &&
3570 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
3571 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
3572 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
3573 /* clear ID bits if server didn't use them (or we can't tell) */
3574 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
3575 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
3578 nfs_node_unlock(np
);
3583 * NFS file remove call
3584 * To try and make NFS semantics closer to UFS semantics, a file that has
3585 * other processes using the vnode is renamed instead of removed and then
3586 * removed later on the last close.
3587 * - If vnode_isinuse()
3588 * If a rename is not already in the works
3589 * call nfs_sillyrename() to set it up
3595 struct vnop_remove_args
/* {
3596 struct vnodeop_desc *a_desc;
3599 struct componentname *a_cnp;
3601 vfs_context_t a_context;
3604 vfs_context_t ctx
= ap
->a_context
;
3605 vnode_t vp
= ap
->a_vp
;
3606 vnode_t dvp
= ap
->a_dvp
;
3607 struct componentname
*cnp
= ap
->a_cnp
;
3608 nfsnode_t dnp
= VTONFS(dvp
);
3609 nfsnode_t np
= VTONFS(vp
);
3610 int error
= 0, nfsvers
, namedattrs
, inuse
, gotattr
= 0, flushed
= 0, setsize
= 0;
3611 struct nfs_vattr nvattr
;
3612 struct nfsmount
*nmp
;
3613 struct nfs_dulookup dul
;
3615 /* XXX prevent removing a sillyrenamed file? */
3617 nmp
= NFSTONMP(dnp
);
3620 nfsvers
= nmp
->nm_vers
;
3621 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
3624 error
= nfs_node_set_busy2(dnp
, np
, vfs_context_thread(ctx
));
3628 /* lock the node while we remove the file */
3629 lck_mtx_lock(nfs_node_hash_mutex
);
3630 while (np
->n_hflag
& NHLOCKED
) {
3631 np
->n_hflag
|= NHLOCKWANT
;
3632 msleep(np
, nfs_node_hash_mutex
, PINOD
, "nfs_remove", NULL
);
3634 np
->n_hflag
|= NHLOCKED
;
3635 lck_mtx_unlock(nfs_node_hash_mutex
);
3638 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
3640 inuse
= vnode_isinuse(vp
, 0);
3641 if ((ap
->a_flags
& VNODE_REMOVE_NODELETEBUSY
) && inuse
) {
3642 /* Caller requested Carbon delete semantics, but file is busy */
3646 if (inuse
&& !gotattr
) {
3647 if (nfs_getattr(np
, &nvattr
, ctx
, NGA_CACHED
))
3648 nvattr
.nva_nlink
= 1;
3652 if (!inuse
|| (np
->n_sillyrename
&& (nvattr
.nva_nlink
> 1))) {
3654 if (!inuse
&& !flushed
) { /* flush all the buffers first */
3655 /* unlock the node */
3656 lck_mtx_lock(nfs_node_hash_mutex
);
3657 np
->n_hflag
&= ~NHLOCKED
;
3658 if (np
->n_hflag
& NHLOCKWANT
) {
3659 np
->n_hflag
&= ~NHLOCKWANT
;
3662 lck_mtx_unlock(nfs_node_hash_mutex
);
3663 nfs_node_clear_busy2(dnp
, np
);
3664 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
3665 FSDBG(260, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0011);
3667 if (error
== EINTR
) {
3668 nfs_node_lock_force(np
);
3669 NATTRINVALIDATE(np
);
3670 nfs_node_unlock(np
);
3674 nfs_dulookup_finish(&dul
, dnp
, ctx
);
3678 if ((nmp
->nm_vers
>= NFS_VER4
) && (np
->n_openflags
& N_DELEG_MASK
))
3679 nfs4_delegation_return(np
, 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
3682 * Purge the name cache so that the chance of a lookup for
3683 * the name succeeding while the remove is in progress is
3686 nfs_name_cache_purge(dnp
, np
, cnp
, ctx
);
3689 nfs_dulookup_start(&dul
, dnp
, ctx
);
3692 error
= nmp
->nm_funcs
->nf_remove_rpc(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
,
3693 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
3696 * Kludge City: If the first reply to the remove rpc is lost..
3697 * the reply to the retransmitted request will be ENOENT
3698 * since the file was in fact removed
3699 * Therefore, we cheat and return success.
3701 if (error
== ENOENT
)
3704 if (!error
&& !inuse
&& !np
->n_sillyrename
) {
3706 * removal succeeded, it's not in use, and not silly renamed so
3707 * remove nfsnode from hash now so we can't accidentally find it
3708 * again if another object gets created with the same filehandle
3709 * before this vnode gets reclaimed
3711 lck_mtx_lock(nfs_node_hash_mutex
);
3712 if (np
->n_hflag
& NHHASHED
) {
3713 LIST_REMOVE(np
, n_hash
);
3714 np
->n_hflag
&= ~NHHASHED
;
3715 FSDBG(266, 0, np
, np
->n_flag
, 0xb1eb1e);
3717 lck_mtx_unlock(nfs_node_hash_mutex
);
3718 /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */
3719 /* clear all flags other than these */
3720 nfs_node_lock_force(np
);
3721 np
->n_flag
&= (NMODIFIED
);
3722 NATTRINVALIDATE(np
);
3723 nfs_node_unlock(np
);
3727 nfs_node_lock_force(np
);
3728 NATTRINVALIDATE(np
);
3729 nfs_node_unlock(np
);
3731 } else if (!np
->n_sillyrename
) {
3733 nfs_dulookup_start(&dul
, dnp
, ctx
);
3734 error
= nfs_sillyrename(dnp
, np
, cnp
, ctx
);
3735 nfs_node_lock_force(np
);
3736 NATTRINVALIDATE(np
);
3737 nfs_node_unlock(np
);
3739 nfs_node_lock_force(np
);
3740 NATTRINVALIDATE(np
);
3741 nfs_node_unlock(np
);
3743 nfs_dulookup_start(&dul
, dnp
, ctx
);
3746 /* nfs_getattr() will check changed and purge caches */
3747 nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
);
3749 nfs_dulookup_finish(&dul
, dnp
, ctx
);
3751 /* unlock the node */
3752 lck_mtx_lock(nfs_node_hash_mutex
);
3753 np
->n_hflag
&= ~NHLOCKED
;
3754 if (np
->n_hflag
& NHLOCKWANT
) {
3755 np
->n_hflag
&= ~NHLOCKWANT
;
3758 lck_mtx_unlock(nfs_node_hash_mutex
);
3759 nfs_node_clear_busy2(dnp
, np
);
3766 * NFS silly-renamed file removal function called from nfs_vnop_inactive
3769 nfs_removeit(struct nfs_sillyrename
*nsp
)
3771 struct nfsmount
*nmp
= NFSTONMP(nsp
->nsr_dnp
);
3774 return nmp
->nm_funcs
->nf_remove_rpc(nsp
->nsr_dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, NULL
, nsp
->nsr_cred
);
3778 * NFS remove rpc, called from nfs_remove() and nfs_removeit().
3788 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0;
3789 struct timespec premtime
= { 0, 0 };
3790 struct nfsmount
*nmp
;
3793 struct nfsm_chain nmreq
, nmrep
;
3795 nmp
= NFSTONMP(dnp
);
3798 nfsvers
= nmp
->nm_vers
;
3799 if ((nfsvers
== NFS_VER2
) && (namelen
> NFS_MAXNAMLEN
))
3800 return (ENAMETOOLONG
);
3802 nfsm_chain_null(&nmreq
);
3803 nfsm_chain_null(&nmrep
);
3805 nfsm_chain_build_alloc_init(error
, &nmreq
,
3806 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+ nfsm_rndup(namelen
));
3807 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
3808 nfsm_chain_add_name(error
, &nmreq
, name
, namelen
, nmp
);
3809 nfsm_chain_build_done(error
, &nmreq
);
3812 error
= nfs_request2(dnp
, NULL
, &nmreq
, NFSPROC_REMOVE
, thd
, cred
, NULL
, 0, &nmrep
, &xid
, &status
);
3814 if ((lockerror
= nfs_node_lock(dnp
)))
3816 if (nfsvers
== NFS_VER3
)
3817 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &xid
);
3819 dnp
->n_flag
|= NMODIFIED
;
3820 /* if directory hadn't changed, update namecache mtime */
3821 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==))
3822 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
3824 NATTRINVALIDATE(dnp
);
3829 nfs_node_unlock(dnp
);
3830 nfsm_chain_cleanup(&nmreq
);
3831 nfsm_chain_cleanup(&nmrep
);
3836 * NFS file rename call
3840 struct vnop_rename_args
/* {
3841 struct vnodeop_desc *a_desc;
3844 struct componentname *a_fcnp;
3847 struct componentname *a_tcnp;
3848 vfs_context_t a_context;
3851 vfs_context_t ctx
= ap
->a_context
;
3852 vnode_t fdvp
= ap
->a_fdvp
;
3853 vnode_t fvp
= ap
->a_fvp
;
3854 vnode_t tdvp
= ap
->a_tdvp
;
3855 vnode_t tvp
= ap
->a_tvp
;
3856 nfsnode_t fdnp
, fnp
, tdnp
, tnp
;
3857 struct componentname
*tcnp
= ap
->a_tcnp
;
3858 struct componentname
*fcnp
= ap
->a_fcnp
;
3859 int error
, nfsvers
, inuse
=0, tvprecycle
=0, locked
=0;
3860 mount_t fmp
, tdmp
, tmp
;
3861 struct nfs_vattr nvattr
;
3862 struct nfsmount
*nmp
;
3864 fdnp
= VTONFS(fdvp
);
3866 tdnp
= VTONFS(tdvp
);
3867 tnp
= tvp
? VTONFS(tvp
) : NULL
;
3869 nmp
= NFSTONMP(fdnp
);
3872 nfsvers
= nmp
->nm_vers
;
3874 error
= nfs_node_set_busy4(fdnp
, fnp
, tdnp
, tnp
, vfs_context_thread(ctx
));
3878 if (tvp
&& (tvp
!= fvp
)) {
3879 /* lock the node while we rename over the existing file */
3880 lck_mtx_lock(nfs_node_hash_mutex
);
3881 while (tnp
->n_hflag
& NHLOCKED
) {
3882 tnp
->n_hflag
|= NHLOCKWANT
;
3883 msleep(tnp
, nfs_node_hash_mutex
, PINOD
, "nfs_rename", NULL
);
3885 tnp
->n_hflag
|= NHLOCKED
;
3886 lck_mtx_unlock(nfs_node_hash_mutex
);
3890 /* Check for cross-device rename */
3891 fmp
= vnode_mount(fvp
);
3892 tmp
= tvp
? vnode_mount(tvp
) : NULL
;
3893 tdmp
= vnode_mount(tdvp
);
3894 if ((fmp
!= tdmp
) || (tvp
&& (fmp
!= tmp
))) {
3899 /* XXX prevent renaming from/over a sillyrenamed file? */
3902 * If the tvp exists and is in use, sillyrename it before doing the
3903 * rename of the new file over it.
3904 * XXX Can't sillyrename a directory.
3905 * Don't sillyrename if source and target are same vnode (hard
3906 * links or case-variants)
3908 if (tvp
&& (tvp
!= fvp
))
3909 inuse
= vnode_isinuse(tvp
, 0);
3910 if (inuse
&& !tnp
->n_sillyrename
&& (vnode_vtype(tvp
) != VDIR
)) {
3911 error
= nfs_sillyrename(tdnp
, tnp
, tcnp
, ctx
);
3913 /* sillyrename failed. Instead of pressing on, return error */
3914 goto out
; /* should not be ENOENT. */
3916 /* sillyrename succeeded.*/
3919 } else if (tvp
&& (nmp
->nm_vers
>= NFS_VER4
) && (tnp
->n_openflags
& N_DELEG_MASK
)) {
3920 nfs4_delegation_return(tnp
, 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
3923 error
= nmp
->nm_funcs
->nf_rename_rpc(fdnp
, fcnp
->cn_nameptr
, fcnp
->cn_namelen
,
3924 tdnp
, tcnp
->cn_nameptr
, tcnp
->cn_namelen
, ctx
);
3927 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
3929 if (error
== ENOENT
)
3932 if (tvp
&& (tvp
!= fvp
) && !tnp
->n_sillyrename
) {
3933 nfs_node_lock_force(tnp
);
3934 tvprecycle
= (!error
&& !vnode_isinuse(tvp
, 0) &&
3935 (nfs_getattrcache(tnp
, &nvattr
, 0) || (nvattr
.nva_nlink
== 1)));
3936 nfs_node_unlock(tnp
);
3937 lck_mtx_lock(nfs_node_hash_mutex
);
3938 if (tvprecycle
&& (tnp
->n_hflag
& NHHASHED
)) {
3940 * remove nfsnode from hash now so we can't accidentally find it
3941 * again if another object gets created with the same filehandle
3942 * before this vnode gets reclaimed
3944 LIST_REMOVE(tnp
, n_hash
);
3945 tnp
->n_hflag
&= ~NHHASHED
;
3946 FSDBG(266, 0, tnp
, tnp
->n_flag
, 0xb1eb1e);
3948 lck_mtx_unlock(nfs_node_hash_mutex
);
3951 /* purge the old name cache entries and enter the new one */
3952 nfs_name_cache_purge(fdnp
, fnp
, fcnp
, ctx
);
3954 nfs_name_cache_purge(tdnp
, tnp
, tcnp
, ctx
);
3956 /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */
3957 /* clear all flags other than these */
3958 nfs_node_lock_force(tnp
);
3959 tnp
->n_flag
&= (NMODIFIED
);
3960 nfs_node_unlock(tnp
);
3965 nfs_node_lock_force(tdnp
);
3966 if (tdnp
->n_flag
& NNEGNCENTRIES
) {
3967 tdnp
->n_flag
&= ~NNEGNCENTRIES
;
3968 cache_purge_negatives(tdvp
);
3970 nfs_node_unlock(tdnp
);
3971 nfs_node_lock_force(fnp
);
3972 cache_enter(tdvp
, fvp
, tcnp
);
3973 if (tdvp
!= fdvp
) { /* update parent pointer */
3974 if (fnp
->n_parent
&& !vnode_get(fnp
->n_parent
)) {
3975 /* remove ref from old parent */
3976 vnode_rele(fnp
->n_parent
);
3977 vnode_put(fnp
->n_parent
);
3979 fnp
->n_parent
= tdvp
;
3980 if (tdvp
&& !vnode_get(tdvp
)) {
3981 /* add ref to new parent */
3985 fnp
->n_parent
= NULL
;
3988 nfs_node_unlock(fnp
);
3991 /* nfs_getattr() will check changed and purge caches */
3992 nfs_getattr(fdnp
, NULL
, ctx
, NGA_CACHED
);
3993 nfs_getattr(tdnp
, NULL
, ctx
, NGA_CACHED
);
3996 lck_mtx_lock(nfs_node_hash_mutex
);
3997 tnp
->n_hflag
&= ~NHLOCKED
;
3998 if (tnp
->n_hflag
& NHLOCKWANT
) {
3999 tnp
->n_hflag
&= ~NHLOCKWANT
;
4002 lck_mtx_unlock(nfs_node_hash_mutex
);
4004 nfs_node_clear_busy4(fdnp
, fnp
, tdnp
, tnp
);
4009 * Do an NFS rename rpc. Called from nfs_vnop_rename() and nfs_sillyrename().
4021 int error
= 0, lockerror
= ENOENT
, status
, fwccpostattr
= 0, twccpostattr
= 0;
4022 struct timespec fpremtime
= { 0, 0 }, tpremtime
= { 0, 0 };
4023 struct nfsmount
*nmp
;
4025 u_int64_t xid
, txid
;
4026 struct nfsm_chain nmreq
, nmrep
;
4028 nmp
= NFSTONMP(fdnp
);
4031 nfsvers
= nmp
->nm_vers
;
4032 if ((nfsvers
== NFS_VER2
) &&
4033 ((fnamelen
> NFS_MAXNAMLEN
) || (tnamelen
> NFS_MAXNAMLEN
)))
4034 return (ENAMETOOLONG
);
4036 nfsm_chain_null(&nmreq
);
4037 nfsm_chain_null(&nmrep
);
4039 nfsm_chain_build_alloc_init(error
, &nmreq
,
4040 (NFSX_FH(nfsvers
) + NFSX_UNSIGNED
) * 2 +
4041 nfsm_rndup(fnamelen
) + nfsm_rndup(tnamelen
));
4042 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, fdnp
->n_fhp
, fdnp
->n_fhsize
);
4043 nfsm_chain_add_name(error
, &nmreq
, fnameptr
, fnamelen
, nmp
);
4044 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, tdnp
->n_fhp
, tdnp
->n_fhsize
);
4045 nfsm_chain_add_name(error
, &nmreq
, tnameptr
, tnamelen
, nmp
);
4046 nfsm_chain_build_done(error
, &nmreq
);
4049 error
= nfs_request(fdnp
, NULL
, &nmreq
, NFSPROC_RENAME
, ctx
, NULL
, &nmrep
, &xid
, &status
);
4051 if ((lockerror
= nfs_node_lock2(fdnp
, tdnp
)))
4053 if (nfsvers
== NFS_VER3
) {
4055 nfsm_chain_get_wcc_data(error
, &nmrep
, fdnp
, &fpremtime
, &fwccpostattr
, &xid
);
4056 nfsm_chain_get_wcc_data(error
, &nmrep
, tdnp
, &tpremtime
, &twccpostattr
, &txid
);
4061 nfsm_chain_cleanup(&nmreq
);
4062 nfsm_chain_cleanup(&nmrep
);
4064 fdnp
->n_flag
|= NMODIFIED
;
4065 /* if directory hadn't changed, update namecache mtime */
4066 if (nfstimespeccmp(&fdnp
->n_ncmtime
, &fpremtime
, ==))
4067 NFS_CHANGED_UPDATE_NC(nfsvers
, fdnp
, &fdnp
->n_vattr
);
4069 NATTRINVALIDATE(fdnp
);
4070 tdnp
->n_flag
|= NMODIFIED
;
4071 /* if directory hadn't changed, update namecache mtime */
4072 if (nfstimespeccmp(&tdnp
->n_ncmtime
, &tpremtime
, ==))
4073 NFS_CHANGED_UPDATE_NC(nfsvers
, tdnp
, &tdnp
->n_vattr
);
4075 NATTRINVALIDATE(tdnp
);
4076 nfs_node_unlock2(fdnp
, tdnp
);
4082 * NFS hard link create call
4086 struct vnop_link_args
/* {
4087 struct vnodeop_desc *a_desc;
4090 struct componentname *a_cnp;
4091 vfs_context_t a_context;
4094 vfs_context_t ctx
= ap
->a_context
;
4095 vnode_t vp
= ap
->a_vp
;
4096 vnode_t tdvp
= ap
->a_tdvp
;
4097 struct componentname
*cnp
= ap
->a_cnp
;
4098 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0, attrflag
= 0;
4099 struct timespec premtime
= { 0, 0 };
4100 struct nfsmount
*nmp
;
4101 nfsnode_t np
= VTONFS(vp
);
4102 nfsnode_t tdnp
= VTONFS(tdvp
);
4104 u_int64_t xid
, txid
;
4105 struct nfsm_chain nmreq
, nmrep
;
4107 if (vnode_mount(vp
) != vnode_mount(tdvp
))
4113 nfsvers
= nmp
->nm_vers
;
4114 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
))
4115 return (ENAMETOOLONG
);
4118 * Push all writes to the server, so that the attribute cache
4119 * doesn't get "out of sync" with the server.
4120 * XXX There should be a better way!
4122 nfs_flush(np
, MNT_WAIT
, vfs_context_thread(ctx
), V_IGNORE_WRITEERR
);
4124 error
= nfs_node_set_busy2(tdnp
, np
, vfs_context_thread(ctx
));
4128 nfsm_chain_null(&nmreq
);
4129 nfsm_chain_null(&nmrep
);
4131 nfsm_chain_build_alloc_init(error
, &nmreq
,
4132 NFSX_FH(nfsvers
)*2 + NFSX_UNSIGNED
+ nfsm_rndup(cnp
->cn_namelen
));
4133 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
4134 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, tdnp
->n_fhp
, tdnp
->n_fhsize
);
4135 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4136 nfsm_chain_build_done(error
, &nmreq
);
4138 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_LINK
, ctx
, NULL
, &nmrep
, &xid
, &status
);
4140 if ((lockerror
= nfs_node_lock2(tdnp
, np
))) {
4144 if (nfsvers
== NFS_VER3
) {
4146 nfsm_chain_postop_attr_update_flag(error
, &nmrep
, np
, attrflag
, &xid
);
4147 nfsm_chain_get_wcc_data(error
, &nmrep
, tdnp
, &premtime
, &wccpostattr
, &txid
);
4152 nfsm_chain_cleanup(&nmreq
);
4153 nfsm_chain_cleanup(&nmrep
);
4156 NATTRINVALIDATE(np
);
4157 tdnp
->n_flag
|= NMODIFIED
;
4158 /* if directory hadn't changed, update namecache mtime */
4159 if (nfstimespeccmp(&tdnp
->n_ncmtime
, &premtime
, ==))
4160 NFS_CHANGED_UPDATE_NC(nfsvers
, tdnp
, &tdnp
->n_vattr
);
4162 NATTRINVALIDATE(tdnp
);
4163 if (!error
&& (tdnp
->n_flag
& NNEGNCENTRIES
)) {
4164 tdnp
->n_flag
&= ~NNEGNCENTRIES
;
4165 cache_purge_negatives(tdvp
);
4167 nfs_node_unlock2(tdnp
, np
);
4169 nfs_node_clear_busy2(tdnp
, np
);
4171 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
4173 if (error
== EEXIST
)
4179 * NFS symbolic link create call
4183 struct vnop_symlink_args
/* {
4184 struct vnodeop_desc *a_desc;
4187 struct componentname *a_cnp;
4188 struct vnode_attr *a_vap;
4190 vfs_context_t a_context;
4193 vfs_context_t ctx
= ap
->a_context
;
4194 vnode_t dvp
= ap
->a_dvp
;
4195 struct vnode_attr
*vap
= ap
->a_vap
;
4196 struct componentname
*cnp
= ap
->a_cnp
;
4197 struct nfs_vattr nvattr
;
4199 int slen
, error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0;
4200 struct timespec premtime
= { 0, 0 };
4201 vnode_t newvp
= NULL
;
4202 int nfsvers
, gotuid
, gotgid
;
4203 u_int64_t xid
, dxid
;
4204 nfsnode_t np
= NULL
;
4205 nfsnode_t dnp
= VTONFS(dvp
);
4206 struct nfsmount
*nmp
;
4207 struct nfsm_chain nmreq
, nmrep
;
4208 struct nfsreq rq
, *req
= &rq
;
4209 struct nfs_dulookup dul
;
4214 nfsvers
= nmp
->nm_vers
;
4216 slen
= strlen(ap
->a_target
);
4217 if ((nfsvers
== NFS_VER2
) &&
4218 ((cnp
->cn_namelen
> NFS_MAXNAMLEN
) || (slen
> NFS_MAXPATHLEN
)))
4219 return (ENAMETOOLONG
);
4221 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
4223 VATTR_SET_SUPPORTED(vap
, va_mode
);
4224 VATTR_SET_SUPPORTED(vap
, va_uid
);
4225 VATTR_SET_SUPPORTED(vap
, va_gid
);
4226 VATTR_SET_SUPPORTED(vap
, va_data_size
);
4227 VATTR_SET_SUPPORTED(vap
, va_access_time
);
4228 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
4229 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
4230 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
4232 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
4233 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4235 nfsm_chain_null(&nmreq
);
4236 nfsm_chain_null(&nmrep
);
4238 nfsm_chain_build_alloc_init(error
, &nmreq
,
4239 NFSX_FH(nfsvers
) + 2 * NFSX_UNSIGNED
+
4240 nfsm_rndup(cnp
->cn_namelen
) + nfsm_rndup(slen
) + NFSX_SATTR(nfsvers
));
4241 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4242 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4243 if (nfsvers
== NFS_VER3
)
4244 nfsm_chain_add_v3sattr(error
, &nmreq
, vap
);
4245 nfsm_chain_add_name(error
, &nmreq
, ap
->a_target
, slen
, nmp
);
4246 if (nfsvers
== NFS_VER2
)
4247 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, -1);
4248 nfsm_chain_build_done(error
, &nmreq
);
4251 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_SYMLINK
,
4252 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
4254 nfs_dulookup_start(&dul
, dnp
, ctx
);
4255 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
4258 if ((lockerror
= nfs_node_lock(dnp
)))
4261 if (!error
&& !status
) {
4262 if (dnp
->n_flag
& NNEGNCENTRIES
) {
4263 dnp
->n_flag
&= ~NNEGNCENTRIES
;
4264 cache_purge_negatives(dvp
);
4266 if (nfsvers
== NFS_VER3
)
4267 error
= nfsm_chain_get_fh_attr(&nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
4271 if (nfsvers
== NFS_VER3
)
4272 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
4276 nfsm_chain_cleanup(&nmreq
);
4277 nfsm_chain_cleanup(&nmrep
);
4280 dnp
->n_flag
|= NMODIFIED
;
4281 /* if directory hadn't changed, update namecache mtime */
4282 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==))
4283 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
4284 nfs_node_unlock(dnp
);
4285 /* nfs_getattr() will check changed and purge caches */
4286 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
4289 if (!error
&& fh
.fh_len
)
4290 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
4294 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4297 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
4298 * if we can succeed in looking up the symlink.
4300 if ((error
== EEXIST
) || (!error
&& !newvp
)) {
4302 nfs_node_unlock(np
);
4306 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
4309 if (vnode_vtype(newvp
) != VLNK
)
4314 nfs_node_clear_busy(dnp
);
4315 if (!error
&& (gotuid
|| gotgid
) &&
4316 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
4317 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
4318 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
4319 /* clear ID bits if server didn't use them (or we can't tell) */
4320 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
4321 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
4325 nfs_node_unlock(np
);
4329 nfs_node_unlock(np
);
4340 struct vnop_mkdir_args
/* {
4341 struct vnodeop_desc *a_desc;
4344 struct componentname *a_cnp;
4345 struct vnode_attr *a_vap;
4346 vfs_context_t a_context;
4349 vfs_context_t ctx
= ap
->a_context
;
4350 vnode_t dvp
= ap
->a_dvp
;
4351 struct vnode_attr
*vap
= ap
->a_vap
;
4352 struct componentname
*cnp
= ap
->a_cnp
;
4353 struct nfs_vattr nvattr
;
4354 nfsnode_t np
= NULL
;
4355 struct nfsmount
*nmp
;
4356 nfsnode_t dnp
= VTONFS(dvp
);
4357 vnode_t newvp
= NULL
;
4358 int error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0;
4359 struct timespec premtime
= { 0, 0 };
4360 int nfsvers
, gotuid
, gotgid
;
4361 u_int64_t xid
, dxid
;
4363 struct nfsm_chain nmreq
, nmrep
;
4364 struct nfsreq rq
, *req
= &rq
;
4365 struct nfs_dulookup dul
;
4370 nfsvers
= nmp
->nm_vers
;
4371 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
))
4372 return (ENAMETOOLONG
);
4374 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
4376 VATTR_SET_SUPPORTED(vap
, va_mode
);
4377 VATTR_SET_SUPPORTED(vap
, va_uid
);
4378 VATTR_SET_SUPPORTED(vap
, va_gid
);
4379 VATTR_SET_SUPPORTED(vap
, va_data_size
);
4380 VATTR_SET_SUPPORTED(vap
, va_access_time
);
4381 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
4382 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
4383 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
4385 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
4386 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4388 nfsm_chain_null(&nmreq
);
4389 nfsm_chain_null(&nmrep
);
4391 nfsm_chain_build_alloc_init(error
, &nmreq
,
4392 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+
4393 nfsm_rndup(cnp
->cn_namelen
) + NFSX_SATTR(nfsvers
));
4394 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4395 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4396 if (nfsvers
== NFS_VER3
)
4397 nfsm_chain_add_v3sattr(error
, &nmreq
, vap
);
4399 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, -1);
4400 nfsm_chain_build_done(error
, &nmreq
);
4403 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_MKDIR
,
4404 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
4406 nfs_dulookup_start(&dul
, dnp
, ctx
);
4407 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
4410 if ((lockerror
= nfs_node_lock(dnp
)))
4413 if (!error
&& !status
) {
4414 if (dnp
->n_flag
& NNEGNCENTRIES
) {
4415 dnp
->n_flag
&= ~NNEGNCENTRIES
;
4416 cache_purge_negatives(dvp
);
4418 error
= nfsm_chain_get_fh_attr(&nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
4420 if (nfsvers
== NFS_VER3
)
4421 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
4425 nfsm_chain_cleanup(&nmreq
);
4426 nfsm_chain_cleanup(&nmrep
);
4429 dnp
->n_flag
|= NMODIFIED
;
4430 /* if directory hadn't changed, update namecache mtime */
4431 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==))
4432 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
4433 nfs_node_unlock(dnp
);
4434 /* nfs_getattr() will check changed and purge caches */
4435 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
4438 if (!error
&& fh
.fh_len
)
4439 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
4443 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4446 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
4447 * if we can succeed in looking up the directory.
4449 if ((error
== EEXIST
) || (!error
&& !newvp
)) {
4451 nfs_node_unlock(np
);
4455 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
4458 if (vnode_vtype(newvp
) != VDIR
)
4463 nfs_node_clear_busy(dnp
);
4464 if (!error
&& (gotuid
|| gotgid
) &&
4465 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
4466 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
4467 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
4468 /* clear ID bits if server didn't use them (or we can't tell) */
4469 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
4470 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
4474 nfs_node_unlock(np
);
4478 nfs_node_unlock(np
);
4485 * NFS remove directory call
4489 struct vnop_rmdir_args
/* {
4490 struct vnodeop_desc *a_desc;
4493 struct componentname *a_cnp;
4494 vfs_context_t a_context;
4497 vfs_context_t ctx
= ap
->a_context
;
4498 vnode_t vp
= ap
->a_vp
;
4499 vnode_t dvp
= ap
->a_dvp
;
4500 struct componentname
*cnp
= ap
->a_cnp
;
4501 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0;
4502 struct timespec premtime
= { 0, 0 };
4503 struct nfsmount
*nmp
;
4504 nfsnode_t np
= VTONFS(vp
);
4505 nfsnode_t dnp
= VTONFS(dvp
);
4508 struct nfsm_chain nmreq
, nmrep
;
4509 struct nfsreq rq
, *req
= &rq
;
4510 struct nfs_dulookup dul
;
4515 nfsvers
= nmp
->nm_vers
;
4516 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
))
4517 return (ENAMETOOLONG
);
4519 if ((error
= nfs_node_set_busy2(dnp
, np
, vfs_context_thread(ctx
))))
4522 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4524 nfsm_chain_null(&nmreq
);
4525 nfsm_chain_null(&nmrep
);
4527 nfsm_chain_build_alloc_init(error
, &nmreq
,
4528 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+ nfsm_rndup(cnp
->cn_namelen
));
4529 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4530 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4531 nfsm_chain_build_done(error
, &nmreq
);
4534 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_RMDIR
,
4535 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
4537 nfs_dulookup_start(&dul
, dnp
, ctx
);
4538 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
4541 if ((lockerror
= nfs_node_lock(dnp
)))
4543 if (nfsvers
== NFS_VER3
)
4544 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &xid
);
4548 nfsm_chain_cleanup(&nmreq
);
4549 nfsm_chain_cleanup(&nmrep
);
4552 dnp
->n_flag
|= NMODIFIED
;
4553 /* if directory hadn't changed, update namecache mtime */
4554 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==))
4555 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
4556 nfs_node_unlock(dnp
);
4557 nfs_name_cache_purge(dnp
, np
, cnp
, ctx
);
4558 /* nfs_getattr() will check changed and purge caches */
4559 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
4561 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4562 nfs_node_clear_busy2(dnp
, np
);
4565 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
4567 if (error
== ENOENT
)
4571 * remove nfsnode from hash now so we can't accidentally find it
4572 * again if another object gets created with the same filehandle
4573 * before this vnode gets reclaimed
4575 lck_mtx_lock(nfs_node_hash_mutex
);
4576 if (np
->n_hflag
& NHHASHED
) {
4577 LIST_REMOVE(np
, n_hash
);
4578 np
->n_hflag
&= ~NHHASHED
;
4579 FSDBG(266, 0, np
, np
->n_flag
, 0xb1eb1e);
4581 lck_mtx_unlock(nfs_node_hash_mutex
);
4589 * The incoming "offset" is a directory cookie indicating where in the
4590 * directory entries should be read from. A zero cookie means start at
4591 * the beginning of the directory. Any other cookie will be a cookie
4592 * returned from the server.
4594 * Using that cookie, determine which buffer (and where in that buffer)
4595 * to start returning entries from. Buffer logical block numbers are
4596 * the cookies they start at. If a buffer is found that is not full,
4597 * call into the bio/RPC code to fill it. The RPC code will probably
4598 * fill several buffers (dropping the first, requiring a re-get).
4600 * When done copying entries to the buffer, set the offset to the current
4601 * entry's cookie and enter that cookie in the cookie cache.
4603 * Note: because the getdirentries(2) API returns a long-typed offset,
4604 * the incoming offset is a potentially truncated cookie (ptc).
4605 * The cookie matching code is aware of this and will fall back to
4606 * matching only 32 bits of the cookie.
4610 struct vnop_readdir_args
/* {
4611 struct vnodeop_desc *a_desc;
4617 vfs_context_t a_context;
4620 vfs_context_t ctx
= ap
->a_context
;
4621 vnode_t dvp
= ap
->a_vp
;
4622 nfsnode_t dnp
= VTONFS(dvp
);
4623 struct nfsmount
*nmp
;
4624 uio_t uio
= ap
->a_uio
;
4625 int error
, nfsvers
, extended
, numdirent
, bigcookies
, ptc
, done
;
4626 uint16_t i
, iptc
, rlen
, nlen
;
4627 uint64_t cookie
, nextcookie
, lbn
= 0;
4628 struct nfsbuf
*bp
= NULL
;
4629 struct nfs_dir_buf_header
*ndbhp
;
4630 struct direntry
*dp
, *dpptc
;
4638 nfsvers
= nmp
->nm_vers
;
4639 bigcookies
= (nmp
->nm_state
& NFSSTA_BIGCOOKIES
);
4640 extended
= (ap
->a_flags
& VNODE_READDIR_EXTENDED
);
4642 if (vnode_vtype(dvp
) != VDIR
)
4648 if (uio_resid(uio
) == 0)
4651 if ((nfsvers
>= NFS_VER4
) && (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER
)) {
4652 /* trigger directories should never be read, return nothing */
4656 thd
= vfs_context_thread(ctx
);
4657 numdirent
= done
= 0;
4658 nextcookie
= uio_offset(uio
);
4659 ptc
= bigcookies
&& NFS_DIR_COOKIE_POTENTIALLY_TRUNCATED(nextcookie
);
4661 if ((error
= nfs_node_lock(dnp
)))
4664 if (dnp
->n_flag
& NNEEDINVALIDATE
) {
4665 dnp
->n_flag
&= ~NNEEDINVALIDATE
;
4667 nfs_node_unlock(dnp
);
4668 error
= nfs_vinvalbuf(dvp
, 0, ctx
, 1);
4670 error
= nfs_node_lock(dnp
);
4676 * check for need to invalidate when (re)starting at beginning
4679 if (dnp
->n_flag
& NMODIFIED
) {
4681 nfs_node_unlock(dnp
);
4682 if ((error
= nfs_vinvalbuf(dvp
, 0, ctx
, 1)))
4685 nfs_node_unlock(dnp
);
4687 /* nfs_getattr() will check changed and purge caches */
4688 if ((error
= nfs_getattr(dnp
, NULL
, ctx
, NGA_UNCACHED
)))
4691 nfs_node_unlock(dnp
);
4694 error
= nfs_dir_cookie_to_lbn(dnp
, nextcookie
, &ptc
, &lbn
);
4696 if (error
< 0) { /* just hit EOF cookie */
4704 while (!error
&& !done
) {
4705 OSAddAtomic(1, &nfsstats
.biocache_readdirs
);
4706 cookie
= nextcookie
;
4708 error
= nfs_buf_get(dnp
, lbn
, NFS_DIRBLKSIZ
, thd
, NBLK_READ
, &bp
);
4711 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
4712 if (!ISSET(bp
->nb_flags
, NB_CACHE
) || !ISSET(ndbhp
->ndbh_flags
, NDB_FULL
)) {
4713 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) { /* initialize the buffer */
4714 ndbhp
->ndbh_flags
= 0;
4715 ndbhp
->ndbh_count
= 0;
4716 ndbhp
->ndbh_entry_end
= sizeof(*ndbhp
);
4717 ndbhp
->ndbh_ncgen
= dnp
->n_ncgen
;
4719 error
= nfs_buf_readdir(bp
, ctx
);
4720 if (error
== NFSERR_DIRBUFDROPPED
)
4723 nfs_buf_release(bp
, 1);
4724 if (error
&& (error
!= ENXIO
) && (error
!= ETIMEDOUT
) && (error
!= EINTR
) && (error
!= ERESTART
)) {
4725 if (!nfs_node_lock(dnp
)) {
4727 nfs_node_unlock(dnp
);
4729 nfs_vinvalbuf(dvp
, 0, ctx
, 1);
4730 if (error
== NFSERR_BAD_COOKIE
)
4737 /* find next entry to return */
4738 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
4740 if ((lbn
!= cookie
) && !(ptc
&& NFS_DIR_COOKIE_SAME32(lbn
, cookie
))) {
4743 for (; (i
< ndbhp
->ndbh_count
) && (cookie
!= dp
->d_seekoff
); i
++) {
4744 if (ptc
&& !dpptc
&& NFS_DIR_COOKIE_SAME32(cookie
, dp
->d_seekoff
)) {
4748 nextcookie
= dp
->d_seekoff
;
4749 dp
= NFS_DIRENTRY_NEXT(dp
);
4751 if ((i
== ndbhp
->ndbh_count
) && dpptc
) {
4755 if (i
< ndbhp
->ndbh_count
) {
4756 nextcookie
= dp
->d_seekoff
;
4757 dp
= NFS_DIRENTRY_NEXT(dp
);
4761 ptc
= 0; /* only have to deal with ptc on first cookie */
4763 /* return as many entries as we can */
4764 for (; i
< ndbhp
->ndbh_count
; i
++) {
4766 rlen
= dp
->d_reclen
;
4771 bzero(cp
, sizeof(dent
));
4773 if (dp
->d_namlen
> (sizeof(dent
.d_name
) - 1))
4774 nlen
= sizeof(dent
.d_name
) - 1;
4776 nlen
= dp
->d_namlen
;
4777 rlen
= NFS_DIRENT_LEN(nlen
);
4778 dent
.d_reclen
= rlen
;
4779 dent
.d_ino
= dp
->d_ino
;
4780 dent
.d_type
= dp
->d_type
;
4781 dent
.d_namlen
= nlen
;
4782 strlcpy(dent
.d_name
, dp
->d_name
, nlen
+ 1);
4784 /* check that the record fits */
4785 if (rlen
> uio_resid(uio
)) {
4789 if ((error
= uiomove(cp
, rlen
, uio
)))
4792 nextcookie
= dp
->d_seekoff
;
4793 dp
= NFS_DIRENTRY_NEXT(dp
);
4796 if (i
== ndbhp
->ndbh_count
) {
4797 /* hit end of buffer, move to next buffer */
4799 /* if we also hit EOF, we're done */
4800 if (ISSET(ndbhp
->ndbh_flags
, NDB_EOF
)) {
4807 uio_setoffset(uio
, nextcookie
);
4808 if (!error
&& !done
&& (nextcookie
== cookie
)) {
4809 printf("nfs readdir cookie didn't change 0x%llx, %d/%d\n", cookie
, i
, ndbhp
->ndbh_count
);
4812 nfs_buf_release(bp
, 1);
4816 nfs_dir_cookie_cache(dnp
, nextcookie
, lbn
);
4818 if (ap
->a_numdirent
)
4819 *ap
->a_numdirent
= numdirent
;
4826 * Invalidate cached directory information, except for the actual directory
4827 * blocks (which are invalidated separately).
4830 nfs_invaldir(nfsnode_t dnp
)
4832 if (vnode_vtype(NFSTOV(dnp
)) != VDIR
)
4834 dnp
->n_eofcookie
= 0;
4835 dnp
->n_cookieverf
= 0;
4836 if (!dnp
->n_cookiecache
)
4838 dnp
->n_cookiecache
->free
= 0;
4839 dnp
->n_cookiecache
->mru
= -1;
4840 memset(dnp
->n_cookiecache
->next
, -1, NFSNUMCOOKIES
);
4844 * calculate how much space is available for additional directory entries.
4847 nfs_dir_buf_freespace(struct nfsbuf
*bp
, int rdirplus
)
4849 struct nfs_dir_buf_header
*ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
4854 space
= bp
->nb_bufsize
- ndbhp
->ndbh_entry_end
;
4856 space
-= ndbhp
->ndbh_count
* sizeof(struct nfs_vattr
);
4861 * add/update a cookie->lbn entry in the directory cookie cache
4864 nfs_dir_cookie_cache(nfsnode_t dnp
, uint64_t cookie
, uint64_t lbn
)
4866 struct nfsdmap
*ndcc
;
4872 if (nfs_node_lock(dnp
))
4875 if (cookie
== dnp
->n_eofcookie
) { /* EOF cookie */
4876 nfs_node_unlock(dnp
);
4880 ndcc
= dnp
->n_cookiecache
;
4882 /* allocate the cookie cache structure */
4883 MALLOC_ZONE(dnp
->n_cookiecache
, struct nfsdmap
*,
4884 sizeof(struct nfsdmap
), M_NFSDIROFF
, M_WAITOK
);
4885 if (!dnp
->n_cookiecache
) {
4886 nfs_node_unlock(dnp
);
4889 ndcc
= dnp
->n_cookiecache
;
4892 memset(ndcc
->next
, -1, NFSNUMCOOKIES
);
4896 * Search the list for this cookie.
4897 * Keep track of previous and last entries.
4901 while ((i
!= -1) && (cookie
!= ndcc
->cookies
[i
].key
)) {
4902 if (ndcc
->next
[i
] == -1) /* stop on last entry so we can reuse */
4907 if ((i
!= -1) && (cookie
== ndcc
->cookies
[i
].key
)) {
4908 /* found it, remove from list */
4910 ndcc
->next
[prev
] = ndcc
->next
[i
];
4912 ndcc
->mru
= ndcc
->next
[i
];
4914 /* not found, use next free entry or reuse last entry */
4915 if (ndcc
->free
!= NFSNUMCOOKIES
)
4918 ndcc
->next
[prev
] = -1;
4919 ndcc
->cookies
[i
].key
= cookie
;
4920 ndcc
->cookies
[i
].lbn
= lbn
;
4922 /* insert cookie at head of MRU list */
4923 ndcc
->next
[i
] = ndcc
->mru
;
4925 nfs_node_unlock(dnp
);
4929 * Try to map the given directory cookie to a directory buffer (return lbn).
4930 * If we have a possibly truncated cookie (ptc), check for 32-bit matches too.
4933 nfs_dir_cookie_to_lbn(nfsnode_t dnp
, uint64_t cookie
, int *ptc
, uint64_t *lbnp
)
4935 struct nfsdmap
*ndcc
= dnp
->n_cookiecache
;
4936 int8_t eofptc
, found
;
4938 struct nfsmount
*nmp
;
4939 struct nfsbuf
*bp
, *lastbp
;
4940 struct nfsbuflists blist
;
4941 struct direntry
*dp
, *dpptc
;
4942 struct nfs_dir_buf_header
*ndbhp
;
4944 if (!cookie
) { /* initial cookie */
4950 if (nfs_node_lock(dnp
))
4953 if (cookie
== dnp
->n_eofcookie
) { /* EOF cookie */
4954 nfs_node_unlock(dnp
);
4955 OSAddAtomic(1, &nfsstats
.direofcache_hits
);
4959 /* note if cookie is a 32-bit match with the EOF cookie */
4960 eofptc
= *ptc
? NFS_DIR_COOKIE_SAME32(cookie
, dnp
->n_eofcookie
) : 0;
4963 /* search the list for the cookie */
4964 for (i
= ndcc
? ndcc
->mru
: -1; i
>= 0; i
= ndcc
->next
[i
]) {
4965 if (ndcc
->cookies
[i
].key
== cookie
) {
4966 /* found a match for this cookie */
4967 *lbnp
= ndcc
->cookies
[i
].lbn
;
4968 nfs_node_unlock(dnp
);
4969 OSAddAtomic(1, &nfsstats
.direofcache_hits
);
4973 /* check for 32-bit match */
4974 if (*ptc
&& (iptc
== -1) && NFS_DIR_COOKIE_SAME32(ndcc
->cookies
[i
].key
, cookie
))
4977 /* exact match not found */
4979 /* but 32-bit match hit the EOF cookie */
4980 nfs_node_unlock(dnp
);
4981 OSAddAtomic(1, &nfsstats
.direofcache_hits
);
4985 /* but 32-bit match got a hit */
4986 *lbnp
= ndcc
->cookies
[iptc
].lbn
;
4987 nfs_node_unlock(dnp
);
4988 OSAddAtomic(1, &nfsstats
.direofcache_hits
);
4991 nfs_node_unlock(dnp
);
4994 * No match found in the cookie cache... hmm...
4995 * Let's search the directory's buffers for the cookie.
4997 nmp
= NFSTONMP(dnp
);
5003 lck_mtx_lock(nfs_buf_mutex
);
5005 * Scan the list of buffers, keeping them in order.
5006 * Note that itercomplete inserts each of the remaining buffers
5007 * into the head of list (thus reversing the elements). So, we
5008 * make sure to iterate through all buffers, inserting them after
5009 * each other, to keep them in order.
5010 * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because
5011 * we don't drop nfs_buf_mutex.
5013 if (!nfs_buf_iterprepare(dnp
, &blist
, NBI_CLEAN
)) {
5015 while ((bp
= LIST_FIRST(&blist
))) {
5016 LIST_REMOVE(bp
, nb_vnbufs
);
5018 LIST_INSERT_HEAD(&dnp
->n_cleanblkhd
, bp
, nb_vnbufs
);
5020 LIST_INSERT_AFTER(lastbp
, bp
, nb_vnbufs
);
5025 if (nfs_buf_acquire(bp
, NBAC_NOWAIT
, 0, 0)) {
5026 /* just skip this buffer */
5027 nfs_buf_refrele(bp
);
5030 nfs_buf_refrele(bp
);
5032 /* scan the buffer for the cookie */
5033 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5034 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5036 for (i
=0; (i
< ndbhp
->ndbh_count
) && (cookie
!= dp
->d_seekoff
); i
++) {
5037 if (*ptc
&& !dpptc
&& NFS_DIR_COOKIE_SAME32(cookie
, dp
->d_seekoff
)) {
5041 dp
= NFS_DIRENTRY_NEXT(dp
);
5043 if ((i
== ndbhp
->ndbh_count
) && dpptc
) {
5044 /* found only a PTC match */
5047 } else if (i
< ndbhp
->ndbh_count
) {
5050 if (i
< (ndbhp
->ndbh_count
-1)) {
5051 /* next entry is *in* this buffer: return this block */
5052 *lbnp
= bp
->nb_lblkno
;
5054 } else if (i
== (ndbhp
->ndbh_count
-1)) {
5055 /* next entry refers to *next* buffer: return next block */
5056 *lbnp
= dp
->d_seekoff
;
5061 nfs_buf_itercomplete(dnp
, &blist
, NBI_CLEAN
);
5063 lck_mtx_unlock(nfs_buf_mutex
);
5065 OSAddAtomic(1, &nfsstats
.direofcache_hits
);
5069 /* still not found... oh well, just start a new block */
5071 OSAddAtomic(1, &nfsstats
.direofcache_misses
);
5076 * scan a directory buffer for the given name
5077 * Returns: ESRCH if not found, ENOENT if found invalid, 0 if found
5078 * Note: should only be called with RDIRPLUS directory buffers
5081 #define NDBS_PURGE 1
5082 #define NDBS_UPDATE 2
5087 struct componentname
*cnp
,
5089 struct nfs_vattr
*nvap
,
5092 daddr64_t
*nextlbnp
,
5095 struct direntry
*dp
;
5096 struct nfs_dir_buf_header
*ndbhp
;
5097 struct nfs_vattr
*nvattrp
;
5098 daddr64_t nextlbn
= 0;
5099 int i
, error
= ESRCH
, fhlen
;
5101 /* scan the buffer for the name */
5102 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5103 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5104 for (i
=0; i
< ndbhp
->ndbh_count
; i
++) {
5105 nextlbn
= dp
->d_seekoff
;
5106 if ((cnp
->cn_namelen
== dp
->d_namlen
) && !strcmp(cnp
->cn_nameptr
, dp
->d_name
)) {
5107 fhlen
= dp
->d_name
[dp
->d_namlen
+1];
5108 nvattrp
= NFS_DIR_BUF_NVATTR(bp
, i
);
5109 if ((ndbhp
->ndbh_ncgen
!= bp
->nb_np
->n_ncgen
) || (fhp
->fh_len
== 0) ||
5110 (nvattrp
->nva_type
== VNON
) || (nvattrp
->nva_fileid
== 0)) {
5111 /* entry is not valid */
5115 if (flags
== NDBS_PURGE
) {
5117 bzero(nvattrp
, sizeof(*nvattrp
));
5121 if (flags
== NDBS_UPDATE
) {
5122 /* update direntry's attrs if fh matches */
5123 if ((fhp
->fh_len
== fhlen
) && !bcmp(&dp
->d_name
[dp
->d_namlen
+2], fhp
->fh_data
, fhlen
)) {
5124 bcopy(nvap
, nvattrp
, sizeof(*nvap
));
5125 dp
->d_fileno
= nvattrp
->nva_fileid
;
5126 nvattrp
->nva_fileid
= *xidp
;
5127 *(time_t*)(&dp
->d_name
[dp
->d_namlen
+2+fhp
->fh_len
]) = *attrstampp
;
5132 /* copy out fh, attrs, attrstamp, and xid */
5133 fhp
->fh_len
= fhlen
;
5134 bcopy(&dp
->d_name
[dp
->d_namlen
+2], fhp
->fh_data
, MAX(fhp
->fh_len
, (int)sizeof(fhp
->fh_data
)));
5135 *attrstampp
= *(time_t*)(&dp
->d_name
[dp
->d_namlen
+2+fhp
->fh_len
]);
5136 bcopy(nvattrp
, nvap
, sizeof(*nvap
));
5137 *xidp
= nvap
->nva_fileid
;
5138 nvap
->nva_fileid
= dp
->d_fileno
;
5142 dp
= NFS_DIRENTRY_NEXT(dp
);
5145 *nextlbnp
= nextlbn
;
5150 * Look up a name in a directory's buffers.
5151 * Note: should only be called with RDIRPLUS directory buffers
5154 nfs_dir_buf_cache_lookup(nfsnode_t dnp
, nfsnode_t
*npp
, struct componentname
*cnp
, vfs_context_t ctx
, int purge
)
5157 struct nfsmount
*nmp
;
5158 int error
= 0, i
, found
= 0, count
= 0;
5160 struct nfs_vattr nvattr
;
5162 time_t attrstamp
= 0;
5163 thread_t thd
= vfs_context_thread(ctx
);
5164 struct nfsbuf
*bp
, *lastbp
, *foundbp
;
5165 struct nfsbuflists blist
;
5166 daddr64_t lbn
, nextlbn
;
5167 int dotunder
= (cnp
->cn_namelen
> 2) && (cnp
->cn_nameptr
[0] == '.') && (cnp
->cn_nameptr
[1] == '_');
5169 if (!(nmp
= NFSTONMP(dnp
)))
5174 /* first check most recent buffer (and next one too) */
5175 lbn
= dnp
->n_lastdbl
;
5176 for (i
=0; i
< 2; i
++) {
5177 if ((error
= nfs_buf_get(dnp
, lbn
, NFS_DIRBLKSIZ
, thd
, NBLK_READ
|NBLK_ONLYVALID
, &bp
)))
5182 error
= nfs_dir_buf_search(bp
, cnp
, &fh
, &nvattr
, &xid
, &attrstamp
, &nextlbn
, purge
? NDBS_PURGE
: 0);
5183 nfs_buf_release(bp
, 0);
5184 if (error
== ESRCH
) {
5193 lck_mtx_lock(nfs_buf_mutex
);
5195 dnp
->n_lastdbl
= lbn
;
5200 * Scan the list of buffers, keeping them in order.
5201 * Note that itercomplete inserts each of the remaining buffers
5202 * into the head of list (thus reversing the elements). So, we
5203 * make sure to iterate through all buffers, inserting them after
5204 * each other, to keep them in order.
5205 * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because
5206 * we don't drop nfs_buf_mutex.
5208 if (!nfs_buf_iterprepare(dnp
, &blist
, NBI_CLEAN
)) {
5209 lastbp
= foundbp
= NULL
;
5210 while ((bp
= LIST_FIRST(&blist
))) {
5211 LIST_REMOVE(bp
, nb_vnbufs
);
5213 LIST_INSERT_HEAD(&dnp
->n_cleanblkhd
, bp
, nb_vnbufs
);
5215 LIST_INSERT_AFTER(lastbp
, bp
, nb_vnbufs
);
5219 if (!purge
&& dotunder
&& (count
> 100)) /* don't waste too much time looking for ._ files */
5222 lbn
= bp
->nb_lblkno
;
5223 if (nfs_buf_acquire(bp
, NBAC_NOWAIT
, 0, 0)) {
5224 /* just skip this buffer */
5225 nfs_buf_refrele(bp
);
5228 nfs_buf_refrele(bp
);
5230 error
= nfs_dir_buf_search(bp
, cnp
, &fh
, &nvattr
, &xid
, &attrstamp
, NULL
, purge
? NDBS_PURGE
: 0);
5231 if (error
== ESRCH
) {
5240 LIST_REMOVE(foundbp
, nb_vnbufs
);
5241 LIST_INSERT_HEAD(&dnp
->n_cleanblkhd
, foundbp
, nb_vnbufs
);
5242 dnp
->n_lastdbl
= foundbp
->nb_lblkno
;
5244 nfs_buf_itercomplete(dnp
, &blist
, NBI_CLEAN
);
5247 lck_mtx_unlock(nfs_buf_mutex
);
5249 if (!error
&& found
&& !purge
) {
5250 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
,
5251 &nvattr
, &xid
, dnp
->n_auth
, NG_MAKEENTRY
, &newnp
);
5254 newnp
->n_attrstamp
= attrstamp
;
5256 nfs_node_unlock(newnp
);
5257 /* check if the dir buffer's attrs are out of date */
5258 if (!nfs_getattr(newnp
, &nvattr
, ctx
, NGA_CACHED
) &&
5259 (newnp
->n_attrstamp
!= attrstamp
)) {
5260 /* they are, so update them */
5261 error
= nfs_buf_get(dnp
, lbn
, NFS_DIRBLKSIZ
, thd
, NBLK_READ
|NBLK_ONLYVALID
, &bp
);
5263 attrstamp
= newnp
->n_attrstamp
;
5265 nfs_dir_buf_search(bp
, cnp
, &fh
, &nvattr
, &xid
, &attrstamp
, NULL
, NDBS_UPDATE
);
5266 nfs_buf_release(bp
, 0);
5276 * Purge name cache entries for the given node.
5277 * For RDIRPLUS, also invalidate the entry in the directory's buffers.
5280 nfs_name_cache_purge(nfsnode_t dnp
, nfsnode_t np
, struct componentname
*cnp
, vfs_context_t ctx
)
5282 struct nfsmount
*nmp
= NFSTONMP(dnp
);
5284 cache_purge(NFSTOV(np
));
5285 if (nmp
&& (nmp
->nm_vers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
))
5286 nfs_dir_buf_cache_lookup(dnp
, NULL
, cnp
, ctx
, 1);
5290 * NFS V3 readdir (plus) RPC.
5293 nfs3_readdir_rpc(nfsnode_t dnp
, struct nfsbuf
*bp
, vfs_context_t ctx
)
5295 struct nfsmount
*nmp
;
5296 int error
= 0, lockerror
, nfsvers
, rdirplus
, bigcookies
;
5297 int i
, status
, attrflag
, fhflag
, more_entries
= 1, eof
, bp_dropped
= 0;
5298 uint32_t nmreaddirsize
, nmrsize
;
5299 uint32_t namlen
, skiplen
, fhlen
, xlen
, attrlen
, reclen
, space_free
, space_needed
;
5300 uint64_t cookie
, lastcookie
, xid
, savedxid
, fileno
;
5301 struct nfsm_chain nmreq
, nmrep
, nmrepsave
;
5303 struct nfs_vattr
*nvattrp
;
5304 struct nfs_dir_buf_header
*ndbhp
;
5305 struct direntry
*dp
;
5306 char *padstart
, padlen
;
5309 nmp
= NFSTONMP(dnp
);
5312 nfsvers
= nmp
->nm_vers
;
5313 nmreaddirsize
= nmp
->nm_readdirsize
;
5314 nmrsize
= nmp
->nm_rsize
;
5315 bigcookies
= nmp
->nm_state
& NFSSTA_BIGCOOKIES
;
5317 rdirplus
= ((nfsvers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
)) ? 1 : 0;
5319 if ((lockerror
= nfs_node_lock(dnp
)))
5322 /* determine cookie to use, and move dp to the right offset */
5323 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5324 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5325 if (ndbhp
->ndbh_count
) {
5326 for (i
=0; i
< ndbhp
->ndbh_count
-1; i
++)
5327 dp
= NFS_DIRENTRY_NEXT(dp
);
5328 cookie
= dp
->d_seekoff
;
5329 dp
= NFS_DIRENTRY_NEXT(dp
);
5331 cookie
= bp
->nb_lblkno
;
5332 /* increment with every buffer read */
5333 OSAddAtomic(1, &nfsstats
.readdir_bios
);
5335 lastcookie
= cookie
;
5338 * Loop around doing readdir(plus) RPCs of size nm_readdirsize until
5339 * the buffer is full (or we hit EOF). Then put the remainder of the
5340 * results in the next buffer(s).
5342 nfsm_chain_null(&nmreq
);
5343 nfsm_chain_null(&nmrep
);
5344 while (nfs_dir_buf_freespace(bp
, rdirplus
) && !(ndbhp
->ndbh_flags
& NDB_FULL
)) {
5345 nfsm_chain_build_alloc_init(error
, &nmreq
,
5346 NFSX_FH(nfsvers
) + NFSX_READDIR(nfsvers
) + NFSX_UNSIGNED
);
5347 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
5348 if (nfsvers
== NFS_VER3
) {
5349 /* opaque values don't need swapping, but as long */
5350 /* as we are consistent about it, it should be ok */
5351 nfsm_chain_add_64(error
, &nmreq
, cookie
);
5352 nfsm_chain_add_64(error
, &nmreq
, dnp
->n_cookieverf
);
5354 nfsm_chain_add_32(error
, &nmreq
, cookie
);
5356 nfsm_chain_add_32(error
, &nmreq
, nmreaddirsize
);
5358 nfsm_chain_add_32(error
, &nmreq
, nmrsize
);
5359 nfsm_chain_build_done(error
, &nmreq
);
5360 nfs_node_unlock(dnp
);
5364 error
= nfs_request(dnp
, NULL
, &nmreq
,
5365 rdirplus
? NFSPROC_READDIRPLUS
: NFSPROC_READDIR
,
5366 ctx
, NULL
, &nmrep
, &xid
, &status
);
5368 if ((lockerror
= nfs_node_lock(dnp
)))
5372 if (nfsvers
== NFS_VER3
)
5373 nfsm_chain_postop_attr_update(error
, &nmrep
, dnp
, &xid
);
5376 if (nfsvers
== NFS_VER3
)
5377 nfsm_chain_get_64(error
, &nmrep
, dnp
->n_cookieverf
);
5378 nfsm_chain_get_32(error
, &nmrep
, more_entries
);
5381 nfs_node_unlock(dnp
);
5384 if (error
== NFSERR_NOTSUPP
) {
5385 /* oops... it doesn't look like readdirplus is supported */
5386 lck_mtx_lock(&nmp
->nm_lock
);
5387 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_RDIRPLUS
);
5388 lck_mtx_unlock(&nmp
->nm_lock
);
5396 /* loop through the entries packing them into the buffer */
5397 while (more_entries
) {
5398 if (nfsvers
== NFS_VER3
)
5399 nfsm_chain_get_64(error
, &nmrep
, fileno
);
5401 nfsm_chain_get_32(error
, &nmrep
, fileno
);
5402 nfsm_chain_get_32(error
, &nmrep
, namlen
);
5404 /* just truncate names that don't fit in direntry.d_name */
5409 if (namlen
> (sizeof(dp
->d_name
)-1)) {
5410 skiplen
= namlen
- sizeof(dp
->d_name
) + 1;
5411 namlen
= sizeof(dp
->d_name
) - 1;
5415 /* guess that fh size will be same as parent */
5416 fhlen
= rdirplus
? (1 + dnp
->n_fhsize
) : 0;
5417 xlen
= rdirplus
? (fhlen
+ sizeof(time_t)) : 0;
5418 attrlen
= rdirplus
? sizeof(struct nfs_vattr
) : 0;
5419 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
5420 space_needed
= reclen
+ attrlen
;
5421 space_free
= nfs_dir_buf_freespace(bp
, rdirplus
);
5422 if (space_needed
> space_free
) {
5424 * We still have entries to pack, but we've
5425 * run out of room in the current buffer.
5426 * So we need to move to the next buffer.
5427 * The block# for the next buffer is the
5428 * last cookie in the current buffer.
5431 ndbhp
->ndbh_flags
|= NDB_FULL
;
5432 nfs_buf_release(bp
, 0);
5435 error
= nfs_buf_get(dnp
, lastcookie
, NFS_DIRBLKSIZ
, vfs_context_thread(ctx
), NBLK_READ
, &bp
);
5437 /* initialize buffer */
5438 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5439 ndbhp
->ndbh_flags
= 0;
5440 ndbhp
->ndbh_count
= 0;
5441 ndbhp
->ndbh_entry_end
= sizeof(*ndbhp
);
5442 ndbhp
->ndbh_ncgen
= dnp
->n_ncgen
;
5443 space_free
= nfs_dir_buf_freespace(bp
, rdirplus
);
5444 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5445 /* increment with every buffer read */
5446 OSAddAtomic(1, &nfsstats
.readdir_bios
);
5449 dp
->d_fileno
= fileno
;
5450 dp
->d_namlen
= namlen
;
5451 dp
->d_reclen
= reclen
;
5452 dp
->d_type
= DT_UNKNOWN
;
5453 nfsm_chain_get_opaque(error
, &nmrep
, namlen
, dp
->d_name
);
5455 dp
->d_name
[namlen
] = '\0';
5457 nfsm_chain_adv(error
, &nmrep
,
5458 nfsm_rndup(namlen
+ skiplen
) - nfsm_rndup(namlen
));
5459 if (nfsvers
== NFS_VER3
)
5460 nfsm_chain_get_64(error
, &nmrep
, cookie
);
5462 nfsm_chain_get_32(error
, &nmrep
, cookie
);
5464 dp
->d_seekoff
= cookie
;
5465 if (!bigcookies
&& (cookie
>> 32) && (nmp
== NFSTONMP(dnp
))) {
5466 /* we've got a big cookie, make sure flag is set */
5467 lck_mtx_lock(&nmp
->nm_lock
);
5468 nmp
->nm_state
|= NFSSTA_BIGCOOKIES
;
5469 lck_mtx_unlock(&nmp
->nm_lock
);
5473 nvattrp
= NFS_DIR_BUF_NVATTR(bp
, ndbhp
->ndbh_count
);
5474 /* check for attributes */
5475 nfsm_chain_get_32(error
, &nmrep
, attrflag
);
5478 /* grab attributes */
5479 error
= nfs_parsefattr(&nmrep
, NFS_VER3
, nvattrp
);
5481 dp
->d_type
= IFTODT(VTTOIF(nvattrp
->nva_type
));
5482 /* fileid is already in d_fileno, so stash xid in attrs */
5483 nvattrp
->nva_fileid
= savedxid
;
5485 /* mark the attributes invalid */
5486 bzero(nvattrp
, sizeof(struct nfs_vattr
));
5488 /* check for file handle */
5489 nfsm_chain_get_32(error
, &nmrep
, fhflag
);
5492 nfsm_chain_get_fh(error
, &nmrep
, NFS_VER3
, &fh
);
5494 fhlen
= fh
.fh_len
+ 1;
5495 xlen
= fhlen
+ sizeof(time_t);
5496 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
5497 space_needed
= reclen
+ attrlen
;
5498 if (space_needed
> space_free
) {
5499 /* didn't actually have the room... move on to next buffer */
5503 /* pack the file handle into the record */
5504 dp
->d_name
[dp
->d_namlen
+1] = fh
.fh_len
;
5505 bcopy(fh
.fh_data
, &dp
->d_name
[dp
->d_namlen
+2], fh
.fh_len
);
5507 /* mark the file handle invalid */
5509 fhlen
= fh
.fh_len
+ 1;
5510 xlen
= fhlen
+ sizeof(time_t);
5511 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
5512 bzero(&dp
->d_name
[dp
->d_namlen
+1], fhlen
);
5514 *(time_t*)(&dp
->d_name
[dp
->d_namlen
+1+fhlen
]) = now
.tv_sec
;
5515 dp
->d_reclen
= reclen
;
5517 padstart
= dp
->d_name
+ dp
->d_namlen
+ 1 + xlen
;
5518 ndbhp
->ndbh_count
++;
5519 lastcookie
= cookie
;
5520 /* advance to next direntry in buffer */
5521 dp
= NFS_DIRENTRY_NEXT(dp
);
5522 ndbhp
->ndbh_entry_end
= (char*)dp
- bp
->nb_data
;
5523 /* zero out the pad bytes */
5524 padlen
= (char*)dp
- padstart
;
5526 bzero(padstart
, padlen
);
5527 /* check for more entries */
5528 nfsm_chain_get_32(error
, &nmrep
, more_entries
);
5531 /* Finally, get the eof boolean */
5532 nfsm_chain_get_32(error
, &nmrep
, eof
);
5535 ndbhp
->ndbh_flags
|= (NDB_FULL
|NDB_EOF
);
5536 nfs_node_lock_force(dnp
);
5537 dnp
->n_eofcookie
= lastcookie
;
5538 nfs_node_unlock(dnp
);
5543 nfs_buf_release(bp
, 0);
5547 if ((lockerror
= nfs_node_lock(dnp
)))
5550 nfsm_chain_cleanup(&nmrep
);
5551 nfsm_chain_null(&nmreq
);
5554 if (bp_dropped
&& bp
)
5555 nfs_buf_release(bp
, 0);
5557 nfs_node_unlock(dnp
);
5558 nfsm_chain_cleanup(&nmreq
);
5559 nfsm_chain_cleanup(&nmrep
);
5560 return (bp_dropped
? NFSERR_DIRBUFDROPPED
: error
);
5564 * Silly rename. To make the NFS filesystem that is stateless look a little
5565 * more like the "ufs" a remove of an active vnode is translated to a rename
5566 * to a funny looking filename that is removed by nfs_vnop_inactive on the
5567 * nfsnode. There is the potential for another process on a different client
5568 * to create the same funny name between when the lookitup() fails and the
5569 * rename() completes, but...
5572 /* format of "random" silly names - includes a number and pid */
5573 /* (note: shouldn't exceed size of nfs_sillyrename.nsr_name) */
5574 #define NFS_SILLYNAME_FORMAT ".nfs.%08x.%04x"
5575 /* starting from zero isn't silly enough */
5576 static uint32_t nfs_sillyrename_number
= 0x20051025;
5582 struct componentname
*cnp
,
5585 struct nfs_sillyrename
*nsp
;
5590 struct nfsmount
*nmp
;
5592 nmp
= NFSTONMP(dnp
);
5596 nfs_name_cache_purge(dnp
, np
, cnp
, ctx
);
5598 MALLOC_ZONE(nsp
, struct nfs_sillyrename
*,
5599 sizeof (struct nfs_sillyrename
), M_NFSREQ
, M_WAITOK
);
5602 cred
= vfs_context_ucred(ctx
);
5603 kauth_cred_ref(cred
);
5604 nsp
->nsr_cred
= cred
;
5606 error
= vnode_ref(NFSTOV(dnp
));
5610 /* Fudge together a funny name */
5611 pid
= vfs_context_pid(ctx
);
5612 num
= OSAddAtomic(1, &nfs_sillyrename_number
);
5613 nsp
->nsr_namlen
= snprintf(nsp
->nsr_name
, sizeof(nsp
->nsr_name
),
5614 NFS_SILLYNAME_FORMAT
, num
, (pid
& 0xffff));
5615 if (nsp
->nsr_namlen
>= (int)sizeof(nsp
->nsr_name
))
5616 nsp
->nsr_namlen
= sizeof(nsp
->nsr_name
) - 1;
5618 /* Try lookitups until we get one that isn't there */
5619 while (nfs_lookitup(dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, ctx
, NULL
) == 0) {
5620 num
= OSAddAtomic(1, &nfs_sillyrename_number
);
5621 nsp
->nsr_namlen
= snprintf(nsp
->nsr_name
, sizeof(nsp
->nsr_name
),
5622 NFS_SILLYNAME_FORMAT
, num
, (pid
& 0xffff));
5623 if (nsp
->nsr_namlen
>= (int)sizeof(nsp
->nsr_name
))
5624 nsp
->nsr_namlen
= sizeof(nsp
->nsr_name
) - 1;
5627 /* now, do the rename */
5628 error
= nmp
->nm_funcs
->nf_rename_rpc(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
,
5629 dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, ctx
);
5631 /* Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. */
5632 if (error
== ENOENT
)
5635 nfs_node_lock_force(dnp
);
5636 if (dnp
->n_flag
& NNEGNCENTRIES
) {
5637 dnp
->n_flag
&= ~NNEGNCENTRIES
;
5638 cache_purge_negatives(NFSTOV(dnp
));
5640 nfs_node_unlock(dnp
);
5642 FSDBG(267, dnp
, np
, num
, error
);
5645 error
= nfs_lookitup(dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, ctx
, &np
);
5646 nfs_node_lock_force(np
);
5647 np
->n_sillyrename
= nsp
;
5648 nfs_node_unlock(np
);
5651 vnode_rele(NFSTOV(dnp
));
5653 nsp
->nsr_cred
= NOCRED
;
5654 kauth_cred_unref(&cred
);
5655 FREE_ZONE(nsp
, sizeof(*nsp
), M_NFSREQ
);
5660 nfs3_lookup_rpc_async(
5665 struct nfsreq
**reqp
)
5667 struct nfsmount
*nmp
;
5668 struct nfsm_chain nmreq
;
5669 int error
= 0, nfsvers
;
5671 nmp
= NFSTONMP(dnp
);
5674 nfsvers
= nmp
->nm_vers
;
5676 nfsm_chain_null(&nmreq
);
5678 nfsm_chain_build_alloc_init(error
, &nmreq
,
5679 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+ nfsm_rndup(namelen
));
5680 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
5681 nfsm_chain_add_name(error
, &nmreq
, name
, namelen
, nmp
);
5682 nfsm_chain_build_done(error
, &nmreq
);
5684 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_LOOKUP
,
5685 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, reqp
);
5687 nfsm_chain_cleanup(&nmreq
);
5692 nfs3_lookup_rpc_async_finish(
5694 __unused
char *name
,
5695 __unused
int namelen
,
5700 struct nfs_vattr
*nvap
)
5702 int error
= 0, lockerror
= ENOENT
, status
, nfsvers
, attrflag
;
5704 struct nfsmount
*nmp
;
5705 struct nfsm_chain nmrep
;
5707 nmp
= NFSTONMP(dnp
);
5708 nfsvers
= nmp
->nm_vers
;
5710 nfsm_chain_null(&nmrep
);
5712 error
= nfs_request_async_finish(req
, &nmrep
, xidp
, &status
);
5714 if ((lockerror
= nfs_node_lock(dnp
)))
5717 if (error
|| status
) {
5718 if (nfsvers
== NFS_VER3
)
5719 nfsm_chain_postop_attr_update(error
, &nmrep
, dnp
, &xid
);
5725 nfsmout_if(error
|| !fhp
|| !nvap
);
5727 /* get the file handle */
5728 nfsm_chain_get_fh(error
, &nmrep
, nfsvers
, fhp
);
5730 /* get the attributes */
5731 if (nfsvers
== NFS_VER3
) {
5732 nfsm_chain_postop_attr_get(error
, &nmrep
, attrflag
, nvap
);
5733 nfsm_chain_postop_attr_update(error
, &nmrep
, dnp
, &xid
);
5734 if (!error
&& !attrflag
)
5735 error
= nfs3_getattr_rpc(NULL
, NFSTOMP(dnp
), fhp
->fh_data
, fhp
->fh_len
, 0, ctx
, nvap
, xidp
);
5737 error
= nfs_parsefattr(&nmrep
, nfsvers
, nvap
);
5741 nfs_node_unlock(dnp
);
5742 nfsm_chain_cleanup(&nmrep
);
5747 * Look up a file name and optionally either update the file handle or
5748 * allocate an nfsnode, depending on the value of npp.
5749 * npp == NULL --> just do the lookup
5750 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
5752 * *npp != NULL --> update the file handle in the vnode
5763 nfsnode_t np
, newnp
= NULL
;
5766 struct nfsmount
*nmp
;
5767 struct nfs_vattr nvattr
;
5768 struct nfsreq rq
, *req
= &rq
;
5770 nmp
= NFSTONMP(dnp
);
5774 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXNAME
) &&
5775 (namelen
> (int)nmp
->nm_fsattr
.nfsa_maxname
))
5776 return (ENAMETOOLONG
);
5778 NVATTR_INIT(&nvattr
);
5780 /* check for lookup of "." */
5781 if ((name
[0] == '.') && (namelen
== 1)) {
5782 /* skip lookup, we know who we are */
5788 error
= nmp
->nm_funcs
->nf_lookup_rpc_async(dnp
, name
, namelen
, ctx
, &req
);
5790 error
= nmp
->nm_funcs
->nf_lookup_rpc_async_finish(dnp
, name
, namelen
, ctx
, req
, &xid
, &fh
, &nvattr
);
5791 nfsmout_if(!npp
|| error
);
5795 if (fh
.fh_len
!= np
->n_fhsize
) {
5796 u_char
*oldbuf
= (np
->n_fhsize
> NFS_SMALLFH
) ? np
->n_fhp
: NULL
;
5797 if (fh
.fh_len
> NFS_SMALLFH
) {
5798 MALLOC_ZONE(np
->n_fhp
, u_char
*, fh
.fh_len
, M_NFSBIGFH
, M_WAITOK
);
5805 np
->n_fhp
= &np
->n_fh
[0];
5808 FREE_ZONE(oldbuf
, np
->n_fhsize
, M_NFSBIGFH
);
5810 bcopy(fh
.fh_data
, np
->n_fhp
, fh
.fh_len
);
5811 np
->n_fhsize
= fh
.fh_len
;
5812 nfs_node_lock_force(np
);
5813 error
= nfs_loadattrcache(np
, &nvattr
, &xid
, 0);
5814 nfs_node_unlock(np
);
5817 } else if (NFS_CMPFH(dnp
, fh
.fh_data
, fh
.fh_len
)) {
5818 nfs_node_lock_force(dnp
);
5819 if (dnp
->n_xid
<= xid
)
5820 error
= nfs_loadattrcache(dnp
, &nvattr
, &xid
, 0);
5821 nfs_node_unlock(dnp
);
5825 struct componentname cn
, *cnp
= &cn
;
5826 bzero(cnp
, sizeof(*cnp
));
5827 cnp
->cn_nameptr
= name
;
5828 cnp
->cn_namelen
= namelen
;
5829 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
,
5830 &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
5836 if (npp
&& !*npp
&& !error
)
5838 NVATTR_CLEANUP(&nvattr
);
5843 * set up and initialize a "._" file lookup structure used for
5844 * performing async lookups.
5847 nfs_dulookup_init(struct nfs_dulookup
*dulp
, nfsnode_t dnp
, const char *name
, int namelen
, vfs_context_t ctx
)
5849 int error
, du_namelen
;
5851 struct nfsmount
*nmp
= NFSTONMP(dnp
);
5853 /* check for ._ file in name cache */
5855 bzero(&dulp
->du_cn
, sizeof(dulp
->du_cn
));
5856 du_namelen
= namelen
+ 2;
5857 if (!nmp
|| NMFLAG(nmp
, NONEGNAMECACHE
))
5859 if ((namelen
>= 2) && (name
[0] == '.') && (name
[1] == '_'))
5861 if (du_namelen
>= (int)sizeof(dulp
->du_smallname
))
5862 MALLOC(dulp
->du_cn
.cn_nameptr
, char *, du_namelen
+ 1, M_TEMP
, M_WAITOK
);
5864 dulp
->du_cn
.cn_nameptr
= dulp
->du_smallname
;
5865 if (!dulp
->du_cn
.cn_nameptr
)
5867 dulp
->du_cn
.cn_namelen
= du_namelen
;
5868 snprintf(dulp
->du_cn
.cn_nameptr
, du_namelen
+ 1, "._%s", name
);
5869 dulp
->du_cn
.cn_nameptr
[du_namelen
] = '\0';
5870 dulp
->du_cn
.cn_nameiop
= LOOKUP
;
5871 dulp
->du_cn
.cn_flags
= MAKEENTRY
;
5873 error
= cache_lookup(NFSTOV(dnp
), &du_vp
, &dulp
->du_cn
);
5876 } else if (!error
) {
5877 nmp
= NFSTONMP(dnp
);
5878 if (nmp
&& (nmp
->nm_vers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
)) {
5879 /* if rdirplus, try dir buf cache lookup */
5880 nfsnode_t du_np
= NULL
;
5881 if (!nfs_dir_buf_cache_lookup(dnp
, &du_np
, &dulp
->du_cn
, ctx
, 0) && du_np
) {
5882 /* dir buf cache hit */
5883 du_vp
= NFSTOV(du_np
);
5889 dulp
->du_flags
|= NFS_DULOOKUP_DOIT
;
5894 * start an async "._" file lookup request
5897 nfs_dulookup_start(struct nfs_dulookup
*dulp
, nfsnode_t dnp
, vfs_context_t ctx
)
5899 struct nfsmount
*nmp
= NFSTONMP(dnp
);
5900 struct nfsreq
*req
= &dulp
->du_req
;
5902 if (!nmp
|| !(dulp
->du_flags
& NFS_DULOOKUP_DOIT
) || (dulp
->du_flags
& NFS_DULOOKUP_INPROG
))
5904 if (!nmp
->nm_funcs
->nf_lookup_rpc_async(dnp
, dulp
->du_cn
.cn_nameptr
,
5905 dulp
->du_cn
.cn_namelen
, ctx
, &req
))
5906 dulp
->du_flags
|= NFS_DULOOKUP_INPROG
;
5910 * finish an async "._" file lookup request and clean up the structure
5913 nfs_dulookup_finish(struct nfs_dulookup
*dulp
, nfsnode_t dnp
, vfs_context_t ctx
)
5915 struct nfsmount
*nmp
= NFSTONMP(dnp
);
5920 struct nfs_vattr nvattr
;
5922 if (!nmp
|| !(dulp
->du_flags
& NFS_DULOOKUP_INPROG
))
5925 NVATTR_INIT(&nvattr
);
5926 error
= nmp
->nm_funcs
->nf_lookup_rpc_async_finish(dnp
, dulp
->du_cn
.cn_nameptr
,
5927 dulp
->du_cn
.cn_namelen
, ctx
, &dulp
->du_req
, &xid
, &fh
, &nvattr
);
5928 dulp
->du_flags
&= ~NFS_DULOOKUP_INPROG
;
5929 if (error
== ENOENT
) {
5930 /* add a negative entry in the name cache */
5931 nfs_node_lock_force(dnp
);
5932 cache_enter(NFSTOV(dnp
), NULL
, &dulp
->du_cn
);
5933 dnp
->n_flag
|= NNEGNCENTRIES
;
5934 nfs_node_unlock(dnp
);
5935 } else if (!error
) {
5936 error
= nfs_nget(NFSTOMP(dnp
), dnp
, &dulp
->du_cn
, fh
.fh_data
, fh
.fh_len
,
5937 &nvattr
, &xid
, dulp
->du_req
.r_auth
, NG_MAKEENTRY
, &du_np
);
5939 nfs_node_unlock(du_np
);
5940 vnode_put(NFSTOV(du_np
));
5943 NVATTR_CLEANUP(&nvattr
);
5945 if (dulp
->du_flags
& NFS_DULOOKUP_INPROG
)
5946 nfs_request_async_cancel(&dulp
->du_req
);
5947 if (dulp
->du_cn
.cn_nameptr
&& (dulp
->du_cn
.cn_nameptr
!= dulp
->du_smallname
))
5948 FREE(dulp
->du_cn
.cn_nameptr
, M_TEMP
);
5953 * NFS Version 3 commit RPC
5963 struct nfsmount
*nmp
;
5964 int error
= 0, lockerror
, status
, wccpostattr
= 0, nfsvers
;
5965 struct timespec premtime
= { 0, 0 };
5966 u_int64_t xid
, newwverf
;
5968 struct nfsm_chain nmreq
, nmrep
;
5971 FSDBG(521, np
, offset
, count
, nmp
? nmp
->nm_state
: 0);
5974 if (!(nmp
->nm_state
& NFSSTA_HASWRITEVERF
))
5976 nfsvers
= nmp
->nm_vers
;
5978 if (count
> UINT32_MAX
)
5983 nfsm_chain_null(&nmreq
);
5984 nfsm_chain_null(&nmrep
);
5986 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(NFS_VER3
));
5987 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
5988 nfsm_chain_add_64(error
, &nmreq
, offset
);
5989 nfsm_chain_add_32(error
, &nmreq
, count32
);
5990 nfsm_chain_build_done(error
, &nmreq
);
5992 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC_COMMIT
,
5993 current_thread(), cred
, NULL
, 0, &nmrep
, &xid
, &status
);
5994 if ((lockerror
= nfs_node_lock(np
)))
5996 /* can we do anything useful with the wcc info? */
5997 nfsm_chain_get_wcc_data(error
, &nmrep
, np
, &premtime
, &wccpostattr
, &xid
);
5999 nfs_node_unlock(np
);
6002 nfsm_chain_get_64(error
, &nmrep
, newwverf
);
6004 lck_mtx_lock(&nmp
->nm_lock
);
6005 if (nmp
->nm_verf
!= newwverf
)
6006 nmp
->nm_verf
= newwverf
;
6007 if (wverf
!= newwverf
)
6008 error
= NFSERR_STALEWRITEVERF
;
6009 lck_mtx_unlock(&nmp
->nm_lock
);
6011 nfsm_chain_cleanup(&nmreq
);
6012 nfsm_chain_cleanup(&nmrep
);
6019 __unused
struct vnop_blockmap_args
/* {
6020 struct vnodeop_desc *a_desc;
6035 * fsync vnode op. Just call nfs_flush().
6040 struct vnop_fsync_args
/* {
6041 struct vnodeop_desc *a_desc;
6044 vfs_context_t a_context;
6047 return (nfs_flush(VTONFS(ap
->a_vp
), ap
->a_waitfor
, vfs_context_thread(ap
->a_context
), 0));
6052 * Do an NFS pathconf RPC.
6057 struct nfs_fsattr
*nfsap
,
6061 int error
= 0, lockerror
, status
, nfsvers
;
6062 struct nfsm_chain nmreq
, nmrep
;
6063 struct nfsmount
*nmp
= NFSTONMP(np
);
6068 nfsvers
= nmp
->nm_vers
;
6070 nfsm_chain_null(&nmreq
);
6071 nfsm_chain_null(&nmrep
);
6073 /* fetch pathconf info from server */
6074 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(NFS_VER3
));
6075 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
6076 nfsm_chain_build_done(error
, &nmreq
);
6078 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_PATHCONF
, ctx
, NULL
, &nmrep
, &xid
, &status
);
6079 if ((lockerror
= nfs_node_lock(np
)))
6081 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
6083 nfs_node_unlock(np
);
6086 nfsm_chain_get_32(error
, &nmrep
, nfsap
->nfsa_maxlink
);
6087 nfsm_chain_get_32(error
, &nmrep
, nfsap
->nfsa_maxname
);
6088 nfsm_chain_get_32(error
, &nmrep
, val
);
6090 nfsap
->nfsa_flags
|= NFS_FSFLAG_NO_TRUNC
;
6091 nfsm_chain_get_32(error
, &nmrep
, val
);
6093 nfsap
->nfsa_flags
|= NFS_FSFLAG_CHOWN_RESTRICTED
;
6094 nfsm_chain_get_32(error
, &nmrep
, val
);
6096 nfsap
->nfsa_flags
|= NFS_FSFLAG_CASE_INSENSITIVE
;
6097 nfsm_chain_get_32(error
, &nmrep
, val
);
6099 nfsap
->nfsa_flags
|= NFS_FSFLAG_CASE_PRESERVING
;
6100 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXLINK
);
6101 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXNAME
);
6102 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_NO_TRUNC
);
6103 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_CHOWN_RESTRICTED
);
6104 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_INSENSITIVE
);
6105 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_PRESERVING
);
6107 nfsm_chain_cleanup(&nmreq
);
6108 nfsm_chain_cleanup(&nmrep
);
6112 /* save pathconf info for NFSv3 mount */
6114 nfs3_pathconf_cache(struct nfsmount
*nmp
, struct nfs_fsattr
*nfsap
)
6116 nmp
->nm_fsattr
.nfsa_maxlink
= nfsap
->nfsa_maxlink
;
6117 nmp
->nm_fsattr
.nfsa_maxname
= nfsap
->nfsa_maxname
;
6118 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_NO_TRUNC
;
6119 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_CHOWN_RESTRICTED
;
6120 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_INSENSITIVE
;
6121 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_PRESERVING
;
6122 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXLINK
);
6123 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXNAME
);
6124 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_NO_TRUNC
);
6125 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CHOWN_RESTRICTED
);
6126 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CASE_INSENSITIVE
);
6127 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CASE_PRESERVING
);
6128 nmp
->nm_state
|= NFSSTA_GOTPATHCONF
;
6132 * Return POSIX pathconf information applicable to nfs.
6134 * The NFS V2 protocol doesn't support this, so just return EINVAL
6140 struct vnop_pathconf_args
/* {
6141 struct vnodeop_desc *a_desc;
6145 vfs_context_t a_context;
6148 vnode_t vp
= ap
->a_vp
;
6149 nfsnode_t np
= VTONFS(vp
);
6150 struct nfsmount
*nmp
;
6151 struct nfs_fsattr nfsa
, *nfsap
;
6153 uint64_t maxFileSize
;
6160 switch (ap
->a_name
) {
6163 case _PC_CHOWN_RESTRICTED
:
6165 case _PC_CASE_SENSITIVE
:
6166 case _PC_CASE_PRESERVING
:
6168 case _PC_FILESIZEBITS
:
6169 if (nmp
->nm_vers
== NFS_VER2
) {
6174 case _PC_XATTR_SIZE_BITS
:
6175 /* Do we support xattrs natively? */
6176 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
)
6178 /* No... so just return an error */
6181 /* don't bother contacting the server if we know the answer */
6185 if (nmp
->nm_vers
== NFS_VER2
)
6188 lck_mtx_lock(&nmp
->nm_lock
);
6189 if (nmp
->nm_vers
== NFS_VER3
) {
6190 if (!(nmp
->nm_state
& NFSSTA_GOTPATHCONF
)) {
6191 /* no pathconf info cached */
6192 lck_mtx_unlock(&nmp
->nm_lock
);
6193 NFS_CLEAR_ATTRIBUTES(nfsa
.nfsa_bitmap
);
6194 error
= nfs3_pathconf_rpc(np
, &nfsa
, ap
->a_context
);
6200 lck_mtx_lock(&nmp
->nm_lock
);
6201 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_HOMOGENEOUS
) {
6202 /* all files have the same pathconf info, */
6203 /* so cache a copy of the results */
6204 nfs3_pathconf_cache(nmp
, &nfsa
);
6208 nfsap
= &nmp
->nm_fsattr
;
6210 } else if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_HOMOGENEOUS
)) {
6211 /* no pathconf info cached */
6212 lck_mtx_unlock(&nmp
->nm_lock
);
6213 NFS_CLEAR_ATTRIBUTES(nfsa
.nfsa_bitmap
);
6214 error
= nfs4_pathconf_rpc(np
, &nfsa
, ap
->a_context
);
6220 lck_mtx_lock(&nmp
->nm_lock
);
6223 nfsap
= &nmp
->nm_fsattr
;
6226 switch (ap
->a_name
) {
6228 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXLINK
))
6229 *ap
->a_retval
= nfsap
->nfsa_maxlink
;
6230 else if ((nmp
->nm_vers
== NFS_VER4
) && NFS_BITMAP_ISSET(np
->n_vattr
.nva_bitmap
, NFS_FATTR_MAXLINK
))
6231 *ap
->a_retval
= np
->n_vattr
.nva_maxlink
;
6236 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXNAME
))
6237 *ap
->a_retval
= nfsap
->nfsa_maxname
;
6241 case _PC_CHOWN_RESTRICTED
:
6242 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_CHOWN_RESTRICTED
))
6243 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_CHOWN_RESTRICTED
) ? 200112 /* _POSIX_CHOWN_RESTRICTED */ : 0;
6248 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_NO_TRUNC
))
6249 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_NO_TRUNC
) ? 200112 /* _POSIX_NO_TRUNC */ : 0;
6253 case _PC_CASE_SENSITIVE
:
6254 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_INSENSITIVE
))
6255 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_INSENSITIVE
) ? 0 : 1;
6259 case _PC_CASE_PRESERVING
:
6260 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_PRESERVING
))
6261 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_PRESERVING
) ? 1 : 0;
6265 case _PC_XATTR_SIZE_BITS
: /* same as file size bits if named attrs supported */
6266 case _PC_FILESIZEBITS
:
6267 if (!NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXFILESIZE
)) {
6272 maxFileSize
= nfsap
->nfsa_maxfilesize
;
6274 if (maxFileSize
& 0xffffffff00000000ULL
) {
6278 if (maxFileSize
& 0xffff0000) {
6282 if (maxFileSize
& 0xff00) {
6286 if (maxFileSize
& 0xf0) {
6290 if (maxFileSize
& 0xc) {
6294 if (maxFileSize
& 0x2) {
6297 *ap
->a_retval
= nbits
;
6303 lck_mtx_unlock(&nmp
->nm_lock
);
6309 * Read wrapper for special devices.
6313 struct vnop_read_args
/* {
6314 struct vnodeop_desc *a_desc;
6318 vfs_context_t a_context;
6321 nfsnode_t np
= VTONFS(ap
->a_vp
);
6328 if ((error
= nfs_node_lock(np
)))
6332 np
->n_atim
.tv_sec
= now
.tv_sec
;
6333 np
->n_atim
.tv_nsec
= now
.tv_usec
* 1000;
6334 nfs_node_unlock(np
);
6335 return (VOCALL(spec_vnodeop_p
, VOFFSET(vnop_read
), ap
));
6339 * Write wrapper for special devices.
6343 struct vnop_write_args
/* {
6344 struct vnodeop_desc *a_desc;
6348 vfs_context_t a_context;
6351 nfsnode_t np
= VTONFS(ap
->a_vp
);
6358 if ((error
= nfs_node_lock(np
)))
6362 np
->n_mtim
.tv_sec
= now
.tv_sec
;
6363 np
->n_mtim
.tv_nsec
= now
.tv_usec
* 1000;
6364 nfs_node_unlock(np
);
6365 return (VOCALL(spec_vnodeop_p
, VOFFSET(vnop_write
), ap
));
6369 * Close wrapper for special devices.
6371 * Update the times on the nfsnode then do device close.
6375 struct vnop_close_args
/* {
6376 struct vnodeop_desc *a_desc;
6379 vfs_context_t a_context;
6382 vnode_t vp
= ap
->a_vp
;
6383 nfsnode_t np
= VTONFS(vp
);
6384 struct vnode_attr vattr
;
6388 if ((error
= nfs_node_lock(np
)))
6390 if (np
->n_flag
& (NACC
| NUPD
)) {
6392 if (!vnode_isinuse(vp
, 0) && (mp
= vnode_mount(vp
)) && !vfs_isrdonly(mp
)) {
6394 if (np
->n_flag
& NACC
) {
6395 vattr
.va_access_time
= np
->n_atim
;
6396 VATTR_SET_ACTIVE(&vattr
, va_access_time
);
6398 if (np
->n_flag
& NUPD
) {
6399 vattr
.va_modify_time
= np
->n_mtim
;
6400 VATTR_SET_ACTIVE(&vattr
, va_modify_time
);
6402 nfs_node_unlock(np
);
6403 vnode_setattr(vp
, &vattr
, ap
->a_context
);
6405 nfs_node_unlock(np
);
6408 nfs_node_unlock(np
);
6410 return (VOCALL(spec_vnodeop_p
, VOFFSET(vnop_close
), ap
));
6414 extern vnop_t
**fifo_vnodeop_p
;
6417 * Read wrapper for fifos.
6421 struct vnop_read_args
/* {
6422 struct vnodeop_desc *a_desc;
6426 vfs_context_t a_context;
6429 nfsnode_t np
= VTONFS(ap
->a_vp
);
6436 if ((error
= nfs_node_lock(np
)))
6440 np
->n_atim
.tv_sec
= now
.tv_sec
;
6441 np
->n_atim
.tv_nsec
= now
.tv_usec
* 1000;
6442 nfs_node_unlock(np
);
6443 return (VOCALL(fifo_vnodeop_p
, VOFFSET(vnop_read
), ap
));
6447 * Write wrapper for fifos.
6451 struct vnop_write_args
/* {
6452 struct vnodeop_desc *a_desc;
6456 vfs_context_t a_context;
6459 nfsnode_t np
= VTONFS(ap
->a_vp
);
6466 if ((error
= nfs_node_lock(np
)))
6470 np
->n_mtim
.tv_sec
= now
.tv_sec
;
6471 np
->n_mtim
.tv_nsec
= now
.tv_usec
* 1000;
6472 nfs_node_unlock(np
);
6473 return (VOCALL(fifo_vnodeop_p
, VOFFSET(vnop_write
), ap
));
6477 * Close wrapper for fifos.
6479 * Update the times on the nfsnode then do fifo close.
6483 struct vnop_close_args
/* {
6484 struct vnodeop_desc *a_desc;
6487 vfs_context_t a_context;
6490 vnode_t vp
= ap
->a_vp
;
6491 nfsnode_t np
= VTONFS(vp
);
6492 struct vnode_attr vattr
;
6497 if ((error
= nfs_node_lock(np
)))
6499 if (np
->n_flag
& (NACC
| NUPD
)) {
6501 if (np
->n_flag
& NACC
) {
6502 np
->n_atim
.tv_sec
= now
.tv_sec
;
6503 np
->n_atim
.tv_nsec
= now
.tv_usec
* 1000;
6505 if (np
->n_flag
& NUPD
) {
6506 np
->n_mtim
.tv_sec
= now
.tv_sec
;
6507 np
->n_mtim
.tv_nsec
= now
.tv_usec
* 1000;
6510 if (!vnode_isinuse(vp
, 1) && (mp
= vnode_mount(vp
)) && !vfs_isrdonly(mp
)) {
6512 if (np
->n_flag
& NACC
) {
6513 vattr
.va_access_time
= np
->n_atim
;
6514 VATTR_SET_ACTIVE(&vattr
, va_access_time
);
6516 if (np
->n_flag
& NUPD
) {
6517 vattr
.va_modify_time
= np
->n_mtim
;
6518 VATTR_SET_ACTIVE(&vattr
, va_modify_time
);
6520 nfs_node_unlock(np
);
6521 vnode_setattr(vp
, &vattr
, ap
->a_context
);
6523 nfs_node_unlock(np
);
6526 nfs_node_unlock(np
);
6528 return (VOCALL(fifo_vnodeop_p
, VOFFSET(vnop_close
), ap
));
6535 struct vnop_ioctl_args
/* {
6536 struct vnodeop_desc *a_desc;
6538 u_int32_t a_command;
6541 vfs_context_t a_context;
6544 vfs_context_t ctx
= ap
->a_context
;
6545 vnode_t vp
= ap
->a_vp
;
6548 switch (ap
->a_command
) {
6551 if (vnode_vfsisrdonly(vp
))
6555 error
= nfs_flush(VTONFS(vp
), MNT_WAIT
, vfs_context_thread(ctx
), 0);
6566 __unused
struct vnop_select_args
/* {
6567 struct vnodeop_desc *a_desc;
6572 vfs_context_t a_context;
6577 * We were once bogusly seltrue() which returns 1. Is this right?
6583 * vnode OP for pagein using UPL
6585 * No buffer I/O, just RPCs straight into the mapped pages.
6589 struct vnop_pagein_args
/* {
6590 struct vnodeop_desc *a_desc;
6593 vm_offset_t a_pl_offset;
6597 vfs_context_t a_context;
6600 vnode_t vp
= ap
->a_vp
;
6601 upl_t pl
= ap
->a_pl
;
6602 size_t size
= ap
->a_size
;
6603 off_t f_offset
= ap
->a_f_offset
;
6604 vm_offset_t pl_offset
= ap
->a_pl_offset
;
6605 int flags
= ap
->a_flags
;
6608 nfsnode_t np
= VTONFS(vp
);
6609 size_t nmrsize
, iosize
, txsize
, rxsize
, retsize
;
6611 struct nfsmount
*nmp
;
6613 vm_offset_t ioaddr
, rxaddr
;
6615 char uio_buf
[ UIO_SIZEOF(1) ];
6616 int nofreeupl
= flags
& UPL_NOCOMMIT
;
6617 upl_page_info_t
*plinfo
;
6618 #define MAXPAGINGREQS 16 /* max outstanding RPCs for pagein/pageout */
6619 struct nfsreq
*req
[MAXPAGINGREQS
];
6620 int nextsend
, nextwait
;
6621 uint32_t stategenid
= 0, restart
= 0;
6624 FSDBG(322, np
, f_offset
, size
, flags
);
6625 if (pl
== (upl_t
)NULL
)
6626 panic("nfs_pagein: no upl");
6629 printf("nfs_pagein: invalid size %ld", size
);
6631 (void) ubc_upl_abort(pl
, 0);
6634 if (f_offset
< 0 || f_offset
>= (off_t
)np
->n_size
|| (f_offset
& PAGE_MASK_64
)) {
6636 ubc_upl_abort_range(pl
, pl_offset
, size
,
6637 UPL_ABORT_ERROR
| UPL_ABORT_FREE_ON_EMPTY
);
6641 thd
= vfs_context_thread(ap
->a_context
);
6642 cred
= ubc_getcred(vp
);
6643 if (!IS_VALID_CRED(cred
))
6644 cred
= vfs_context_ucred(ap
->a_context
);
6646 uio
= uio_createwithbuffer(1, f_offset
, UIO_SYSSPACE
, UIO_READ
,
6647 &uio_buf
, sizeof(uio_buf
));
6652 ubc_upl_abort_range(pl
, pl_offset
, size
,
6653 UPL_ABORT_ERROR
| UPL_ABORT_FREE_ON_EMPTY
);
6656 nmrsize
= nmp
->nm_rsize
;
6658 plinfo
= ubc_upl_pageinfo(pl
);
6659 kret
= ubc_upl_map(pl
, &ioaddr
);
6660 if (kret
!= KERN_SUCCESS
)
6661 panic("nfs_vnop_pagein: ubc_upl_map() failed with (%d)", kret
);
6662 ioaddr
+= pl_offset
;
6665 if (nmp
->nm_vers
>= NFS_VER4
)
6666 stategenid
= nmp
->nm_stategenid
;
6667 txsize
= rxsize
= size
;
6668 txoffset
= f_offset
;
6671 bzero(req
, sizeof(req
));
6672 nextsend
= nextwait
= 0;
6674 if (np
->n_flag
& NREVOKE
) {
6678 /* send requests while we need to and have available slots */
6679 while ((txsize
> 0) && (req
[nextsend
] == NULL
)) {
6680 iosize
= MIN(nmrsize
, txsize
);
6681 if ((error
= nmp
->nm_funcs
->nf_read_rpc_async(np
, txoffset
, iosize
, thd
, cred
, NULL
, &req
[nextsend
]))) {
6682 req
[nextsend
] = NULL
;
6687 nextsend
= (nextsend
+ 1) % MAXPAGINGREQS
;
6689 /* wait while we need to and break out if more requests to send */
6690 while ((rxsize
> 0) && req
[nextwait
]) {
6691 iosize
= retsize
= MIN(nmrsize
, rxsize
);
6692 uio_reset(uio
, uio_offset(uio
), UIO_SYSSPACE
, UIO_READ
);
6693 uio_addiov(uio
, CAST_USER_ADDR_T(rxaddr
), iosize
);
6694 FSDBG(322, uio_offset(uio
), uio_resid(uio
), rxaddr
, rxsize
);
6696 upl_ubc_alias_set(pl
, (uintptr_t) current_thread(), (uintptr_t) 2);
6697 #endif /* UPL_DEBUG */
6698 OSAddAtomic(1, &nfsstats
.pageins
);
6699 error
= nmp
->nm_funcs
->nf_read_rpc_async_finish(np
, req
[nextwait
], uio
, &retsize
, NULL
);
6700 req
[nextwait
] = NULL
;
6701 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
6702 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
)) {
6703 lck_mtx_lock(&nmp
->nm_lock
);
6704 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
6705 NP(np
, "nfs_vnop_pagein: error %d, initiating recovery", error
);
6706 nfs_need_recover(nmp
, error
);
6708 lck_mtx_unlock(&nmp
->nm_lock
);
6713 FSDBG(322, uio_offset(uio
), uio_resid(uio
), error
, -1);
6716 if (retsize
< iosize
) {
6717 /* Just zero fill the rest of the valid area. */
6718 int zcnt
= iosize
- retsize
;
6719 bzero((char *)rxaddr
+ retsize
, zcnt
);
6720 FSDBG(324, uio_offset(uio
), retsize
, zcnt
, rxaddr
);
6721 uio_update(uio
, zcnt
);
6728 } while (!error
&& (txsize
|| rxsize
));
6734 /* cancel any outstanding requests */
6735 while (req
[nextwait
]) {
6736 nfs_request_async_cancel(req
[nextwait
]);
6737 req
[nextwait
] = NULL
;
6738 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
6740 if (np
->n_flag
& NREVOKE
) {
6742 } else if (restart
) {
6743 if (restart
<= nfs_mount_state_max_restarts(nmp
)) { /* guard against no progress */
6744 if (error
== NFSERR_GRACE
)
6745 tsleep(&nmp
->nm_state
, (PZERO
-1), "nfsgrace", 2*hz
);
6746 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
)))
6749 NP(np
, "nfs_pagein: too many restarts, aborting");
6758 ubc_upl_abort_range(pl
, pl_offset
, size
,
6760 UPL_ABORT_FREE_ON_EMPTY
);
6762 ubc_upl_commit_range(pl
, pl_offset
, size
,
6763 UPL_COMMIT_CLEAR_DIRTY
|
6764 UPL_COMMIT_FREE_ON_EMPTY
);
6771 * the following are needed only by nfs_pageout to know how to handle errors
6772 * see nfs_pageout comments on explanation of actions.
6773 * the errors here are copied from errno.h and errors returned by servers
6774 * are expected to match the same numbers here. If not, our actions maybe
6777 char nfs_pageouterrorhandler(int);
6778 enum actiontype
{NOACTION
, DUMP
, DUMPANDLOG
, RETRY
, RETRYWITHSLEEP
, SEVER
};
6779 #define NFS_ELAST 88
6780 static u_char errorcount
[NFS_ELAST
+1]; /* better be zeros when initialized */
6781 static const char errortooutcome
[NFS_ELAST
+1] = {
6783 DUMP
, /* EPERM 1 Operation not permitted */
6784 DUMP
, /* ENOENT 2 No such file or directory */
6785 DUMPANDLOG
, /* ESRCH 3 No such process */
6786 RETRY
, /* EINTR 4 Interrupted system call */
6787 DUMP
, /* EIO 5 Input/output error */
6788 DUMP
, /* ENXIO 6 Device not configured */
6789 DUMPANDLOG
, /* E2BIG 7 Argument list too long */
6790 DUMPANDLOG
, /* ENOEXEC 8 Exec format error */
6791 DUMPANDLOG
, /* EBADF 9 Bad file descriptor */
6792 DUMPANDLOG
, /* ECHILD 10 No child processes */
6793 DUMPANDLOG
, /* EDEADLK 11 Resource deadlock avoided - was EAGAIN */
6794 RETRY
, /* ENOMEM 12 Cannot allocate memory */
6795 DUMP
, /* EACCES 13 Permission denied */
6796 DUMPANDLOG
, /* EFAULT 14 Bad address */
6797 DUMPANDLOG
, /* ENOTBLK 15 POSIX - Block device required */
6798 RETRY
, /* EBUSY 16 Device busy */
6799 DUMP
, /* EEXIST 17 File exists */
6800 DUMP
, /* EXDEV 18 Cross-device link */
6801 DUMP
, /* ENODEV 19 Operation not supported by device */
6802 DUMP
, /* ENOTDIR 20 Not a directory */
6803 DUMP
, /* EISDIR 21 Is a directory */
6804 DUMP
, /* EINVAL 22 Invalid argument */
6805 DUMPANDLOG
, /* ENFILE 23 Too many open files in system */
6806 DUMPANDLOG
, /* EMFILE 24 Too many open files */
6807 DUMPANDLOG
, /* ENOTTY 25 Inappropriate ioctl for device */
6808 DUMPANDLOG
, /* ETXTBSY 26 Text file busy - POSIX */
6809 DUMP
, /* EFBIG 27 File too large */
6810 DUMP
, /* ENOSPC 28 No space left on device */
6811 DUMPANDLOG
, /* ESPIPE 29 Illegal seek */
6812 DUMP
, /* EROFS 30 Read-only file system */
6813 DUMP
, /* EMLINK 31 Too many links */
6814 RETRY
, /* EPIPE 32 Broken pipe */
6816 DUMPANDLOG
, /* EDOM 33 Numerical argument out of domain */
6817 DUMPANDLOG
, /* ERANGE 34 Result too large */
6818 RETRY
, /* EAGAIN/EWOULDBLOCK 35 Resource temporarily unavailable */
6819 DUMPANDLOG
, /* EINPROGRESS 36 Operation now in progress */
6820 DUMPANDLOG
, /* EALREADY 37 Operation already in progress */
6821 /* ipc/network software -- argument errors */
6822 DUMPANDLOG
, /* ENOTSOC 38 Socket operation on non-socket */
6823 DUMPANDLOG
, /* EDESTADDRREQ 39 Destination address required */
6824 DUMPANDLOG
, /* EMSGSIZE 40 Message too long */
6825 DUMPANDLOG
, /* EPROTOTYPE 41 Protocol wrong type for socket */
6826 DUMPANDLOG
, /* ENOPROTOOPT 42 Protocol not available */
6827 DUMPANDLOG
, /* EPROTONOSUPPORT 43 Protocol not supported */
6828 DUMPANDLOG
, /* ESOCKTNOSUPPORT 44 Socket type not supported */
6829 DUMPANDLOG
, /* ENOTSUP 45 Operation not supported */
6830 DUMPANDLOG
, /* EPFNOSUPPORT 46 Protocol family not supported */
6831 DUMPANDLOG
, /* EAFNOSUPPORT 47 Address family not supported by protocol family */
6832 DUMPANDLOG
, /* EADDRINUSE 48 Address already in use */
6833 DUMPANDLOG
, /* EADDRNOTAVAIL 49 Can't assign requested address */
6834 /* ipc/network software -- operational errors */
6835 RETRY
, /* ENETDOWN 50 Network is down */
6836 RETRY
, /* ENETUNREACH 51 Network is unreachable */
6837 RETRY
, /* ENETRESET 52 Network dropped connection on reset */
6838 RETRY
, /* ECONNABORTED 53 Software caused connection abort */
6839 RETRY
, /* ECONNRESET 54 Connection reset by peer */
6840 RETRY
, /* ENOBUFS 55 No buffer space available */
6841 RETRY
, /* EISCONN 56 Socket is already connected */
6842 RETRY
, /* ENOTCONN 57 Socket is not connected */
6843 RETRY
, /* ESHUTDOWN 58 Can't send after socket shutdown */
6844 RETRY
, /* ETOOMANYREFS 59 Too many references: can't splice */
6845 RETRY
, /* ETIMEDOUT 60 Operation timed out */
6846 RETRY
, /* ECONNREFUSED 61 Connection refused */
6848 DUMPANDLOG
, /* ELOOP 62 Too many levels of symbolic links */
6849 DUMP
, /* ENAMETOOLONG 63 File name too long */
6850 RETRY
, /* EHOSTDOWN 64 Host is down */
6851 RETRY
, /* EHOSTUNREACH 65 No route to host */
6852 DUMP
, /* ENOTEMPTY 66 Directory not empty */
6854 DUMPANDLOG
, /* PROCLIM 67 Too many processes */
6855 DUMPANDLOG
, /* EUSERS 68 Too many users */
6856 DUMPANDLOG
, /* EDQUOT 69 Disc quota exceeded */
6857 /* Network File System */
6858 DUMP
, /* ESTALE 70 Stale NFS file handle */
6859 DUMP
, /* EREMOTE 71 Too many levels of remote in path */
6860 DUMPANDLOG
, /* EBADRPC 72 RPC struct is bad */
6861 DUMPANDLOG
, /* ERPCMISMATCH 73 RPC version wrong */
6862 DUMPANDLOG
, /* EPROGUNAVAIL 74 RPC prog. not avail */
6863 DUMPANDLOG
, /* EPROGMISMATCH 75 Program version wrong */
6864 DUMPANDLOG
, /* EPROCUNAVAIL 76 Bad procedure for program */
6866 DUMPANDLOG
, /* ENOLCK 77 No locks available */
6867 DUMPANDLOG
, /* ENOSYS 78 Function not implemented */
6868 DUMPANDLOG
, /* EFTYPE 79 Inappropriate file type or format */
6869 DUMPANDLOG
, /* EAUTH 80 Authentication error */
6870 DUMPANDLOG
, /* ENEEDAUTH 81 Need authenticator */
6871 /* Intelligent device errors */
6872 DUMPANDLOG
, /* EPWROFF 82 Device power is off */
6873 DUMPANDLOG
, /* EDEVERR 83 Device error, e.g. paper out */
6874 DUMPANDLOG
, /* EOVERFLOW 84 Value too large to be stored in data type */
6875 /* Program loading errors */
6876 DUMPANDLOG
, /* EBADEXEC 85 Bad executable */
6877 DUMPANDLOG
, /* EBADARCH 86 Bad CPU type in executable */
6878 DUMPANDLOG
, /* ESHLIBVERS 87 Shared library version mismatch */
6879 DUMPANDLOG
, /* EBADMACHO 88 Malformed Macho file */
6883 nfs_pageouterrorhandler(int error
)
6885 if (error
> NFS_ELAST
)
6888 return(errortooutcome
[error
]);
6893 * vnode OP for pageout using UPL
6895 * No buffer I/O, just RPCs straight from the mapped pages.
6896 * File size changes are not permitted in pageout.
6900 struct vnop_pageout_args
/* {
6901 struct vnodeop_desc *a_desc;
6904 vm_offset_t a_pl_offset;
6908 vfs_context_t a_context;
6911 vnode_t vp
= ap
->a_vp
;
6912 upl_t pl
= ap
->a_pl
;
6913 size_t size
= ap
->a_size
;
6914 off_t f_offset
= ap
->a_f_offset
;
6915 vm_offset_t pl_offset
= ap
->a_pl_offset
;
6916 int flags
= ap
->a_flags
;
6917 nfsnode_t np
= VTONFS(vp
);
6921 struct nfsmount
*nmp
= VTONMP(vp
);
6923 int error
= 0, iomode
;
6924 off_t off
, txoffset
, rxoffset
;
6925 vm_offset_t ioaddr
, txaddr
, rxaddr
;
6927 char uio_buf
[ UIO_SIZEOF(1) ];
6928 int nofreeupl
= flags
& UPL_NOCOMMIT
;
6929 size_t nmwsize
, biosize
, iosize
, pgsize
, txsize
, rxsize
, xsize
, remsize
;
6930 struct nfsreq
*req
[MAXPAGINGREQS
];
6931 int nextsend
, nextwait
, wverfset
, commit
;
6932 uint64_t wverf
, wverf2
;
6933 uint32_t stategenid
= 0, vrestart
= 0, restart
= 0, vrestarts
= 0, restarts
= 0;
6936 FSDBG(323, f_offset
, size
, pl
, pl_offset
);
6938 if (pl
== (upl_t
)NULL
)
6939 panic("nfs_pageout: no upl");
6942 printf("nfs_pageout: invalid size %ld", size
);
6944 ubc_upl_abort(pl
, 0);
6950 ubc_upl_abort(pl
, UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
);
6953 biosize
= nmp
->nm_biosize
;
6954 nmwsize
= nmp
->nm_wsize
;
6956 nfs_data_lock_noupdate(np
, NFS_DATA_LOCK_SHARED
);
6959 * Check to see whether the buffer is incore.
6960 * If incore and not busy, invalidate it from the cache.
6962 for (iosize
= 0; iosize
< size
; iosize
+= xsize
) {
6963 off
= f_offset
+ iosize
;
6964 /* need make sure we do things on block boundaries */
6965 xsize
= biosize
- (off
% biosize
);
6966 if (off
+ xsize
> f_offset
+ size
)
6967 xsize
= f_offset
+ size
- off
;
6968 lbn
= (daddr64_t
)(off
/ biosize
);
6969 lck_mtx_lock(nfs_buf_mutex
);
6970 if ((bp
= nfs_buf_incore(np
, lbn
))) {
6971 FSDBG(323, off
, bp
, bp
->nb_lflags
, bp
->nb_flags
);
6972 if (nfs_buf_acquire(bp
, NBAC_NOWAIT
, 0, 0)) {
6973 lck_mtx_unlock(nfs_buf_mutex
);
6974 nfs_data_unlock_noupdate(np
);
6975 /* no panic. just tell vm we are busy */
6977 ubc_upl_abort(pl
, 0);
6980 if (bp
->nb_dirtyend
> 0) {
6982 * if there's a dirty range in the buffer, check
6983 * to see if it extends beyond the pageout region
6985 * if the dirty region lies completely within the
6986 * pageout region, we just invalidate the buffer
6987 * because it's all being written out now anyway.
6989 * if any of the dirty region lies outside the
6990 * pageout region, we'll try to clip the dirty
6991 * region to eliminate the portion that's being
6992 * paged out. If that's not possible, because
6993 * the dirty region extends before and after the
6994 * pageout region, then we'll just return EBUSY.
6996 off_t boff
, start
, end
;
7000 /* clip end to EOF */
7001 if (end
> (off_t
)np
->n_size
)
7005 if ((bp
->nb_dirtyoff
< start
) &&
7006 (bp
->nb_dirtyend
> end
)) {
7008 * not gonna be able to clip the dirty region
7010 * But before returning the bad news, move the
7011 * buffer to the start of the delwri list and
7012 * give the list a push to try to flush the
7015 FSDBG(323, np
, bp
, 0xd00deebc, EBUSY
);
7016 nfs_buf_remfree(bp
);
7017 TAILQ_INSERT_HEAD(&nfsbufdelwri
, bp
, nb_free
);
7020 nfs_buf_delwri_push(1);
7021 lck_mtx_unlock(nfs_buf_mutex
);
7022 nfs_data_unlock_noupdate(np
);
7024 ubc_upl_abort(pl
, 0);
7027 if ((bp
->nb_dirtyoff
< start
) ||
7028 (bp
->nb_dirtyend
> end
)) {
7029 /* clip dirty region, if necessary */
7030 if (bp
->nb_dirtyoff
< start
)
7031 bp
->nb_dirtyend
= min(bp
->nb_dirtyend
, start
);
7032 if (bp
->nb_dirtyend
> end
)
7033 bp
->nb_dirtyoff
= max(bp
->nb_dirtyoff
, end
);
7034 FSDBG(323, bp
, bp
->nb_dirtyoff
, bp
->nb_dirtyend
, 0xd00dee00);
7035 /* we're leaving this block dirty */
7037 lck_mtx_unlock(nfs_buf_mutex
);
7041 nfs_buf_remfree(bp
);
7042 lck_mtx_unlock(nfs_buf_mutex
);
7043 SET(bp
->nb_flags
, NB_INVAL
);
7044 nfs_node_lock_force(np
);
7045 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
7046 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
7047 np
->n_needcommitcnt
--;
7048 CHECK_NEEDCOMMITCNT(np
);
7050 nfs_node_unlock(np
);
7051 nfs_buf_release(bp
, 1);
7053 lck_mtx_unlock(nfs_buf_mutex
);
7057 thd
= vfs_context_thread(ap
->a_context
);
7058 cred
= ubc_getcred(vp
);
7059 if (!IS_VALID_CRED(cred
))
7060 cred
= vfs_context_ucred(ap
->a_context
);
7062 nfs_node_lock_force(np
);
7063 if (np
->n_flag
& NWRITEERR
) {
7064 error
= np
->n_error
;
7065 nfs_node_unlock(np
);
7066 nfs_data_unlock_noupdate(np
);
7068 ubc_upl_abort_range(pl
, pl_offset
, size
,
7069 UPL_ABORT_FREE_ON_EMPTY
);
7072 nfs_node_unlock(np
);
7074 if (f_offset
< 0 || f_offset
>= (off_t
)np
->n_size
||
7075 f_offset
& PAGE_MASK_64
|| size
& PAGE_MASK_64
) {
7076 nfs_data_unlock_noupdate(np
);
7078 ubc_upl_abort_range(pl
, pl_offset
, size
,
7079 UPL_ABORT_FREE_ON_EMPTY
);
7083 kret
= ubc_upl_map(pl
, &ioaddr
);
7084 if (kret
!= KERN_SUCCESS
)
7085 panic("nfs_vnop_pageout: ubc_upl_map() failed with (%d)", kret
);
7086 ioaddr
+= pl_offset
;
7088 if ((u_quad_t
)f_offset
+ size
> np
->n_size
)
7089 xsize
= np
->n_size
- f_offset
;
7093 pgsize
= round_page_64(xsize
);
7094 if ((size
> pgsize
) && !nofreeupl
)
7095 ubc_upl_abort_range(pl
, pl_offset
+ pgsize
, size
- pgsize
,
7096 UPL_ABORT_FREE_ON_EMPTY
);
7099 * check for partial page and clear the
7100 * contents past end of the file before
7101 * releasing it in the VM page cache
7103 if ((u_quad_t
)f_offset
< np
->n_size
&& (u_quad_t
)f_offset
+ size
> np
->n_size
) {
7104 size_t io
= np
->n_size
- f_offset
;
7105 bzero((caddr_t
)(ioaddr
+ io
), size
- io
);
7106 FSDBG(321, np
->n_size
, f_offset
, f_offset
+ io
, size
- io
);
7108 nfs_data_unlock_noupdate(np
);
7110 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_WRITE
,
7111 &uio_buf
, sizeof(uio_buf
));
7114 if (nmp
->nm_vers
>= NFS_VER4
)
7115 stategenid
= nmp
->nm_stategenid
;
7116 wverf
= wverf2
= wverfset
= 0;
7117 txsize
= rxsize
= xsize
;
7118 txoffset
= rxoffset
= f_offset
;
7119 txaddr
= rxaddr
= ioaddr
;
7120 commit
= NFS_WRITE_FILESYNC
;
7122 bzero(req
, sizeof(req
));
7123 nextsend
= nextwait
= 0;
7125 if (np
->n_flag
& NREVOKE
) {
7129 /* send requests while we need to and have available slots */
7130 while ((txsize
> 0) && (req
[nextsend
] == NULL
)) {
7131 iosize
= MIN(nmwsize
, txsize
);
7132 uio_reset(auio
, txoffset
, UIO_SYSSPACE
, UIO_WRITE
);
7133 uio_addiov(auio
, CAST_USER_ADDR_T(txaddr
), iosize
);
7134 FSDBG(323, uio_offset(auio
), iosize
, txaddr
, txsize
);
7135 OSAddAtomic(1, &nfsstats
.pageouts
);
7136 nfs_node_lock_force(np
);
7138 nfs_node_unlock(np
);
7139 vnode_startwrite(vp
);
7140 iomode
= NFS_WRITE_UNSTABLE
;
7141 if ((error
= nmp
->nm_funcs
->nf_write_rpc_async(np
, auio
, iosize
, thd
, cred
, iomode
, NULL
, &req
[nextsend
]))) {
7142 req
[nextsend
] = NULL
;
7143 vnode_writedone(vp
);
7144 nfs_node_lock_force(np
);
7146 nfs_node_unlock(np
);
7152 nextsend
= (nextsend
+ 1) % MAXPAGINGREQS
;
7154 /* wait while we need to and break out if more requests to send */
7155 while ((rxsize
> 0) && req
[nextwait
]) {
7156 iosize
= remsize
= MIN(nmwsize
, rxsize
);
7157 error
= nmp
->nm_funcs
->nf_write_rpc_async_finish(np
, req
[nextwait
], &iomode
, &iosize
, &wverf2
);
7158 req
[nextwait
] = NULL
;
7159 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
7160 vnode_writedone(vp
);
7161 nfs_node_lock_force(np
);
7163 nfs_node_unlock(np
);
7164 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
)) {
7165 lck_mtx_lock(&nmp
->nm_lock
);
7166 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
7167 NP(np
, "nfs_vnop_pageout: error %d, initiating recovery", error
);
7168 nfs_need_recover(nmp
, error
);
7170 lck_mtx_unlock(&nmp
->nm_lock
);
7175 FSDBG(323, rxoffset
, rxsize
, error
, -1);
7181 } else if (wverf
!= wverf2
) {
7182 /* verifier changed, so we need to restart all the writes */
7186 /* Retain the lowest commitment level returned. */
7187 if (iomode
< commit
)
7194 /* need to try sending the remainder */
7196 uio_reset(auio
, rxoffset
, UIO_SYSSPACE
, UIO_WRITE
);
7197 uio_addiov(auio
, CAST_USER_ADDR_T(rxaddr
), remsize
);
7198 iomode
= NFS_WRITE_UNSTABLE
;
7199 error
= nfs_write_rpc2(np
, auio
, thd
, cred
, &iomode
, &wverf2
);
7200 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
)) {
7201 NP(np
, "nfs_vnop_pageout: restart: error %d", error
);
7202 lck_mtx_lock(&nmp
->nm_lock
);
7203 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
7204 NP(np
, "nfs_vnop_pageout: error %d, initiating recovery", error
);
7205 nfs_need_recover(nmp
, error
);
7207 lck_mtx_unlock(&nmp
->nm_lock
);
7212 FSDBG(323, rxoffset
, rxsize
, error
, -1);
7215 if (wverf
!= wverf2
) {
7216 /* verifier changed, so we need to restart all the writes */
7220 if (iomode
< commit
)
7229 } while (!error
&& (txsize
|| rxsize
));
7233 if (!error
&& (commit
!= NFS_WRITE_FILESYNC
)) {
7234 error
= nmp
->nm_funcs
->nf_commit_rpc(np
, f_offset
, xsize
, cred
, wverf
);
7235 if (error
== NFSERR_STALEWRITEVERF
) {
7243 /* cancel any outstanding requests */
7244 while (req
[nextwait
]) {
7245 nfs_request_async_cancel(req
[nextwait
]);
7246 req
[nextwait
] = NULL
;
7247 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
7248 vnode_writedone(vp
);
7249 nfs_node_lock_force(np
);
7251 nfs_node_unlock(np
);
7253 if (np
->n_flag
& NREVOKE
) {
7257 if (++vrestarts
<= 100) /* guard against no progress */
7259 NP(np
, "nfs_pageout: too many restarts, aborting");
7260 FSDBG(323, f_offset
, xsize
, ERESTART
, -1);
7263 if (restarts
<= nfs_mount_state_max_restarts(nmp
)) { /* guard against no progress */
7264 if (error
== NFSERR_GRACE
)
7265 tsleep(&nmp
->nm_state
, (PZERO
-1), "nfsgrace", 2*hz
);
7266 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
)))
7269 NP(np
, "nfs_pageout: too many restarts, aborting");
7270 FSDBG(323, f_offset
, xsize
, ERESTART
, -1);
7279 * We've had several different solutions on what to do when the pageout
7280 * gets an error. If we don't handle it, and return an error to the
7281 * caller, vm, it will retry . This can end in endless looping
7282 * between vm and here doing retries of the same page. Doing a dump
7283 * back to vm, will get it out of vm's knowledge and we lose whatever
7284 * data existed. This is risky, but in some cases necessary. For
7285 * example, the initial fix here was to do that for ESTALE. In that case
7286 * the server is telling us that the file is no longer the same. We
7287 * would not want to keep paging out to that. We also saw some 151
7288 * errors from Auspex server and NFSv3 can return errors higher than
7289 * ELAST. Those along with NFS known server errors we will "dump" from
7290 * vm. Errors we don't expect to occur, we dump and log for further
7291 * analysis. Errors that could be transient, networking ones,
7292 * we let vm "retry". Lastly, errors that we retry, but may have potential
7293 * to storm the network, we "retrywithsleep". "sever" will be used in
7294 * in the future to dump all pages of object for cases like ESTALE.
7295 * All this is the basis for the states returned and first guesses on
7296 * error handling. Tweaking expected as more statistics are gathered.
7297 * Note, in the long run we may need another more robust solution to
7298 * have some kind of persistant store when the vm cannot dump nor keep
7299 * retrying as a solution, but this would be a file architectural change
7301 if (!nofreeupl
) { /* otherwise stacked file system has to handle this */
7304 char action
= nfs_pageouterrorhandler(error
);
7308 abortflags
= UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
;
7311 abortflags
= UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
;
7312 if (error
<= NFS_ELAST
) {
7313 if ((errorcount
[error
] % 100) == 0)
7314 NP(np
, "nfs_pageout: unexpected error %d. dumping vm page", error
);
7315 errorcount
[error
]++;
7319 abortflags
= UPL_ABORT_FREE_ON_EMPTY
;
7321 case RETRYWITHSLEEP
:
7322 abortflags
= UPL_ABORT_FREE_ON_EMPTY
;
7323 /* pri unused. PSOCK for placeholder. */
7324 tsleep(&lbolt
, PSOCK
, "nfspageout", 0);
7326 case SEVER
: /* not implemented */
7328 NP(np
, "nfs_pageout: action %d not expected", action
);
7332 ubc_upl_abort_range(pl
, pl_offset
, pgsize
, abortflags
);
7333 /* return error in all cases above */
7336 ubc_upl_commit_range(pl
, pl_offset
, pgsize
,
7337 UPL_COMMIT_CLEAR_DIRTY
|
7338 UPL_COMMIT_FREE_ON_EMPTY
);
7344 /* Blktooff derives file offset given a logical block number */
7347 struct vnop_blktooff_args
/* {
7348 struct vnodeop_desc *a_desc;
7355 vnode_t vp
= ap
->a_vp
;
7356 struct nfsmount
*nmp
= VTONMP(vp
);
7360 biosize
= nmp
->nm_biosize
;
7362 *ap
->a_offset
= (off_t
)(ap
->a_lblkno
* biosize
);
7369 struct vnop_offtoblk_args
/* {
7370 struct vnodeop_desc *a_desc;
7373 daddr64_t *a_lblkno;
7377 vnode_t vp
= ap
->a_vp
;
7378 struct nfsmount
*nmp
= VTONMP(vp
);
7382 biosize
= nmp
->nm_biosize
;
7384 *ap
->a_lblkno
= (daddr64_t
)(ap
->a_offset
/ biosize
);
7390 * vnode change monitoring
7394 struct vnop_monitor_args
/* {
7395 struct vnodeop_desc *a_desc;
7400 vfs_context_t a_context;
7403 nfsnode_t np
= VTONFS(ap
->a_vp
);
7404 struct nfsmount
*nmp
= VTONMP(ap
->a_vp
);
7410 /* make sure that the vnode's monitoring status is up to date */
7411 lck_mtx_lock(&nmp
->nm_lock
);
7412 if (vnode_ismonitored(ap
->a_vp
)) {
7413 /* This vnode is currently being monitored, make sure we're tracking it. */
7414 if (np
->n_monlink
.le_next
== NFSNOLIST
) {
7415 LIST_INSERT_HEAD(&nmp
->nm_monlist
, np
, n_monlink
);
7416 nfs_mount_sock_thread_wake(nmp
);
7419 /* This vnode is no longer being monitored, make sure we're not tracking it. */
7420 /* Wait for any in-progress getattr to complete first. */
7421 while (np
->n_mflag
& NMMONSCANINPROG
) {
7422 struct timespec ts
= { 1, 0 };
7423 np
->n_mflag
|= NMMONSCANWANT
;
7424 msleep(&np
->n_mflag
, &nmp
->nm_lock
, PZERO
-1, "nfswaitmonscan", &ts
);
7426 if (np
->n_monlink
.le_next
!= NFSNOLIST
) {
7427 LIST_REMOVE(np
, n_monlink
);
7428 np
->n_monlink
.le_next
= NFSNOLIST
;
7431 lck_mtx_unlock(&nmp
->nm_lock
);
7437 * Send a vnode notification for the given events.
7440 nfs_vnode_notify(nfsnode_t np
, uint32_t events
)
7442 struct nfsmount
*nmp
= NFSTONMP(np
);
7443 struct nfs_vattr nvattr
;
7444 struct vnode_attr vattr
, *vap
= NULL
;
7448 if ((np
->n_evtstamp
== now
.tv_sec
) || !nmp
) {
7449 /* delay sending this notify */
7450 np
->n_events
|= events
;
7453 events
|= np
->n_events
;
7455 np
->n_evtstamp
= now
.tv_sec
;
7457 vfs_get_notify_attributes(&vattr
);
7458 if (!nfs_getattrcache(np
, &nvattr
, 0)) {
7461 VATTR_RETURN(vap
, va_fsid
, vfs_statfs(nmp
->nm_mountp
)->f_fsid
.val
[0]);
7462 VATTR_RETURN(vap
, va_fileid
, nvattr
.nva_fileid
);
7463 VATTR_RETURN(vap
, va_mode
, nvattr
.nva_mode
);
7464 VATTR_RETURN(vap
, va_uid
, nvattr
.nva_uid
);
7465 VATTR_RETURN(vap
, va_gid
, nvattr
.nva_gid
);
7466 VATTR_RETURN(vap
, va_nlink
, nvattr
.nva_nlink
);
7468 vnode_notify(NFSTOV(np
), events
, vap
);