2 * Copyright (c) 2000-2019 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>
90 #include <sys/xattr.h>
92 #include <vfs/vfs_support.h>
97 #include <kern/clock.h>
98 #include <libkern/OSAtomic.h>
100 #include <miscfs/fifofs/fifo.h>
101 #include <miscfs/specfs/specdev.h>
103 #include <nfs/rpcv2.h>
104 #include <nfs/nfsproto.h>
106 #include <nfs/nfsnode.h>
107 #include <nfs/nfs_gss.h>
108 #include <nfs/nfsmount.h>
109 #include <nfs/nfs_lock.h>
110 #include <nfs/xdr_subs.h>
111 #include <nfs/nfsm_subs.h>
114 #include <netinet/in.h>
115 #include <netinet/in_var.h>
117 #include <vm/vm_kern.h>
118 #include <vm/vm_pageout.h>
120 #include <kern/task.h>
121 #include <kern/sched_prim.h>
123 #define NFS_VNOP_DBG(...) NFS_DBG(NFS_FAC_VNOP, 7, ## __VA_ARGS__)
124 #define DEFAULT_READLINK_NOCACHE 0
129 int nfs_vnop_lookup(struct vnop_lookup_args
*);
130 int nfsspec_vnop_read(struct vnop_read_args
*);
131 int nfsspec_vnop_write(struct vnop_write_args
*);
132 int nfsspec_vnop_close(struct vnop_close_args
*);
134 int nfsfifo_vnop_read(struct vnop_read_args
*);
135 int nfsfifo_vnop_write(struct vnop_write_args
*);
136 int nfsfifo_vnop_close(struct vnop_close_args
*);
138 int nfs_vnop_ioctl(struct vnop_ioctl_args
*);
139 int nfs_vnop_select(struct vnop_select_args
*);
140 int nfs_vnop_setattr(struct vnop_setattr_args
*);
141 int nfs_vnop_fsync(struct vnop_fsync_args
*);
142 int nfs_vnop_rename(struct vnop_rename_args
*);
143 int nfs_vnop_readdir(struct vnop_readdir_args
*);
144 int nfs_vnop_readlink(struct vnop_readlink_args
*);
145 int nfs_vnop_pathconf(struct vnop_pathconf_args
*);
146 int nfs_vnop_pagein(struct vnop_pagein_args
*);
147 int nfs_vnop_pageout(struct vnop_pageout_args
*);
148 int nfs_vnop_blktooff(struct vnop_blktooff_args
*);
149 int nfs_vnop_offtoblk(struct vnop_offtoblk_args
*);
150 int nfs_vnop_blockmap(struct vnop_blockmap_args
*);
151 int nfs_vnop_monitor(struct vnop_monitor_args
*);
153 int nfs3_vnop_create(struct vnop_create_args
*);
154 int nfs3_vnop_mknod(struct vnop_mknod_args
*);
155 int nfs3_vnop_getattr(struct vnop_getattr_args
*);
156 int nfs3_vnop_link(struct vnop_link_args
*);
157 int nfs3_vnop_mkdir(struct vnop_mkdir_args
*);
158 int nfs3_vnop_rmdir(struct vnop_rmdir_args
*);
159 int nfs3_vnop_symlink(struct vnop_symlink_args
*);
162 vnop_t
**nfsv2_vnodeop_p
;
163 static const struct vnodeopv_entry_desc nfsv2_vnodeop_entries
[] = {
164 { .opve_op
= &vnop_default_desc
, .opve_impl
= (vnop_t
*)vn_default_error
},
165 { .opve_op
= &vnop_lookup_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_lookup
}, /* lookup */
166 { .opve_op
= &vnop_create_desc
, .opve_impl
= (vnop_t
*)nfs3_vnop_create
}, /* create */
167 { .opve_op
= &vnop_mknod_desc
, .opve_impl
= (vnop_t
*)nfs3_vnop_mknod
}, /* mknod */
168 { .opve_op
= &vnop_open_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_open
}, /* open */
169 { .opve_op
= &vnop_close_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_close
}, /* close */
170 { .opve_op
= &vnop_access_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_access
}, /* access */
171 { .opve_op
= &vnop_getattr_desc
, .opve_impl
= (vnop_t
*)nfs3_vnop_getattr
}, /* getattr */
172 { .opve_op
= &vnop_setattr_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
173 { .opve_op
= &vnop_read_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_read
}, /* read */
174 { .opve_op
= &vnop_write_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_write
}, /* write */
175 { .opve_op
= &vnop_ioctl_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_ioctl
}, /* ioctl */
176 { .opve_op
= &vnop_select_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_select
}, /* select */
177 { .opve_op
= &vnop_revoke_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_revoke
}, /* revoke */
178 { .opve_op
= &vnop_mmap_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_mmap
}, /* mmap */
179 { .opve_op
= &vnop_mnomap_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_mnomap
}, /* mnomap */
180 { .opve_op
= &vnop_fsync_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
181 { .opve_op
= &vnop_remove_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_remove
}, /* remove */
182 { .opve_op
= &vnop_link_desc
, .opve_impl
= (vnop_t
*)nfs3_vnop_link
}, /* link */
183 { .opve_op
= &vnop_rename_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_rename
}, /* rename */
184 { .opve_op
= &vnop_mkdir_desc
, .opve_impl
= (vnop_t
*)nfs3_vnop_mkdir
}, /* mkdir */
185 { .opve_op
= &vnop_rmdir_desc
, .opve_impl
= (vnop_t
*)nfs3_vnop_rmdir
}, /* rmdir */
186 { .opve_op
= &vnop_symlink_desc
, .opve_impl
= (vnop_t
*)nfs3_vnop_symlink
}, /* symlink */
187 { .opve_op
= &vnop_readdir_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_readdir
}, /* readdir */
188 { .opve_op
= &vnop_readlink_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_readlink
}, /* readlink */
189 { .opve_op
= &vnop_inactive_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
190 { .opve_op
= &vnop_reclaim_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
191 { .opve_op
= &vnop_strategy_desc
, .opve_impl
= (vnop_t
*)err_strategy
}, /* strategy */
192 { .opve_op
= &vnop_pathconf_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_pathconf
}, /* pathconf */
193 { .opve_op
= &vnop_advlock_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_advlock
}, /* advlock */
194 { .opve_op
= &vnop_bwrite_desc
, .opve_impl
= (vnop_t
*)err_bwrite
}, /* bwrite */
195 { .opve_op
= &vnop_pagein_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
196 { .opve_op
= &vnop_pageout_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
197 { .opve_op
= &vnop_copyfile_desc
, .opve_impl
= (vnop_t
*)err_copyfile
}, /* Copyfile */
198 { .opve_op
= &vnop_blktooff_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
199 { .opve_op
= &vnop_offtoblk_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
200 { .opve_op
= &vnop_blockmap_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
201 { .opve_op
= &vnop_monitor_desc
, .opve_impl
= (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
202 { .opve_op
= NULL
, .opve_impl
= NULL
}
204 const struct vnodeopv_desc nfsv2_vnodeop_opv_desc
=
205 { &nfsv2_vnodeop_p
, nfsv2_vnodeop_entries
};
209 vnop_t
**nfsv4_vnodeop_p
;
210 static const struct vnodeopv_entry_desc nfsv4_vnodeop_entries
[] = {
211 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
212 { &vnop_lookup_desc
, (vnop_t
*)nfs_vnop_lookup
}, /* lookup */
213 { &vnop_create_desc
, (vnop_t
*)nfs4_vnop_create
}, /* create */
214 { &vnop_mknod_desc
, (vnop_t
*)nfs4_vnop_mknod
}, /* mknod */
215 { &vnop_open_desc
, (vnop_t
*)nfs_vnop_open
}, /* open */
216 { &vnop_close_desc
, (vnop_t
*)nfs_vnop_close
}, /* close */
217 { &vnop_access_desc
, (vnop_t
*)nfs_vnop_access
}, /* access */
218 { &vnop_getattr_desc
, (vnop_t
*)nfs4_vnop_getattr
}, /* getattr */
219 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
220 { &vnop_read_desc
, (vnop_t
*)nfs_vnop_read
}, /* read */
221 { &vnop_write_desc
, (vnop_t
*)nfs_vnop_write
}, /* write */
222 { &vnop_ioctl_desc
, (vnop_t
*)nfs_vnop_ioctl
}, /* ioctl */
223 { &vnop_select_desc
, (vnop_t
*)nfs_vnop_select
}, /* select */
224 { &vnop_revoke_desc
, (vnop_t
*)nfs_vnop_revoke
}, /* revoke */
225 { &vnop_mmap_desc
, (vnop_t
*)nfs_vnop_mmap
}, /* mmap */
226 { &vnop_mnomap_desc
, (vnop_t
*)nfs_vnop_mnomap
}, /* mnomap */
227 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
228 { &vnop_remove_desc
, (vnop_t
*)nfs_vnop_remove
}, /* remove */
229 { &vnop_link_desc
, (vnop_t
*)nfs4_vnop_link
}, /* link */
230 { &vnop_rename_desc
, (vnop_t
*)nfs_vnop_rename
}, /* rename */
231 { &vnop_mkdir_desc
, (vnop_t
*)nfs4_vnop_mkdir
}, /* mkdir */
232 { &vnop_rmdir_desc
, (vnop_t
*)nfs4_vnop_rmdir
}, /* rmdir */
233 { &vnop_symlink_desc
, (vnop_t
*)nfs4_vnop_symlink
}, /* symlink */
234 { &vnop_readdir_desc
, (vnop_t
*)nfs_vnop_readdir
}, /* readdir */
235 { &vnop_readlink_desc
, (vnop_t
*)nfs_vnop_readlink
}, /* readlink */
236 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
237 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
238 { &vnop_strategy_desc
, (vnop_t
*)err_strategy
}, /* strategy */
239 { &vnop_pathconf_desc
, (vnop_t
*)nfs_vnop_pathconf
}, /* pathconf */
240 { &vnop_advlock_desc
, (vnop_t
*)nfs_vnop_advlock
}, /* advlock */
241 { &vnop_bwrite_desc
, (vnop_t
*)err_bwrite
}, /* bwrite */
242 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
243 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
244 { &vnop_copyfile_desc
, (vnop_t
*)err_copyfile
}, /* Copyfile */
245 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
246 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
247 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
248 { &vnop_getxattr_desc
, (vnop_t
*)nfs4_vnop_getxattr
}, /* getxattr */
249 { &vnop_setxattr_desc
, (vnop_t
*)nfs4_vnop_setxattr
}, /* setxattr */
250 { &vnop_removexattr_desc
, (vnop_t
*)nfs4_vnop_removexattr
},/* removexattr */
251 { &vnop_listxattr_desc
, (vnop_t
*)nfs4_vnop_listxattr
},/* listxattr */
253 { &vnop_getnamedstream_desc
, (vnop_t
*)nfs4_vnop_getnamedstream
}, /* getnamedstream */
254 { &vnop_makenamedstream_desc
, (vnop_t
*)nfs4_vnop_makenamedstream
}, /* makenamedstream */
255 { &vnop_removenamedstream_desc
, (vnop_t
*)nfs4_vnop_removenamedstream
},/* removenamedstream */
257 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
260 const struct vnodeopv_desc nfsv4_vnodeop_opv_desc
=
261 { &nfsv4_vnodeop_p
, nfsv4_vnodeop_entries
};
265 * Special device vnode ops
267 vnop_t
**spec_nfsv2nodeop_p
;
268 static const struct vnodeopv_entry_desc spec_nfsv2nodeop_entries
[] = {
269 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
270 { &vnop_lookup_desc
, (vnop_t
*)spec_lookup
}, /* lookup */
271 { &vnop_create_desc
, (vnop_t
*)spec_create
}, /* create */
272 { &vnop_mknod_desc
, (vnop_t
*)spec_mknod
}, /* mknod */
273 { &vnop_open_desc
, (vnop_t
*)spec_open
}, /* open */
274 { &vnop_close_desc
, (vnop_t
*)nfsspec_vnop_close
}, /* close */
275 { &vnop_getattr_desc
, (vnop_t
*)nfs3_vnop_getattr
}, /* getattr */
276 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
277 { &vnop_read_desc
, (vnop_t
*)nfsspec_vnop_read
}, /* read */
278 { &vnop_write_desc
, (vnop_t
*)nfsspec_vnop_write
}, /* write */
279 { &vnop_ioctl_desc
, (vnop_t
*)spec_ioctl
}, /* ioctl */
280 { &vnop_select_desc
, (vnop_t
*)spec_select
}, /* select */
281 { &vnop_revoke_desc
, (vnop_t
*)spec_revoke
}, /* revoke */
282 { &vnop_mmap_desc
, (vnop_t
*)spec_mmap
}, /* mmap */
283 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
284 { &vnop_remove_desc
, (vnop_t
*)spec_remove
}, /* remove */
285 { &vnop_link_desc
, (vnop_t
*)spec_link
}, /* link */
286 { &vnop_rename_desc
, (vnop_t
*)spec_rename
}, /* rename */
287 { &vnop_mkdir_desc
, (vnop_t
*)spec_mkdir
}, /* mkdir */
288 { &vnop_rmdir_desc
, (vnop_t
*)spec_rmdir
}, /* rmdir */
289 { &vnop_symlink_desc
, (vnop_t
*)spec_symlink
}, /* symlink */
290 { &vnop_readdir_desc
, (vnop_t
*)spec_readdir
}, /* readdir */
291 { &vnop_readlink_desc
, (vnop_t
*)spec_readlink
}, /* readlink */
292 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
293 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
294 { &vnop_strategy_desc
, (vnop_t
*)spec_strategy
}, /* strategy */
295 { &vnop_pathconf_desc
, (vnop_t
*)spec_pathconf
}, /* pathconf */
296 { &vnop_advlock_desc
, (vnop_t
*)spec_advlock
}, /* advlock */
297 { &vnop_bwrite_desc
, (vnop_t
*)vn_bwrite
}, /* bwrite */
298 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
299 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
300 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
301 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
302 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
303 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
306 const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc
=
307 { &spec_nfsv2nodeop_p
, spec_nfsv2nodeop_entries
};
309 vnop_t
**spec_nfsv4nodeop_p
;
310 static const struct vnodeopv_entry_desc spec_nfsv4nodeop_entries
[] = {
311 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
312 { &vnop_lookup_desc
, (vnop_t
*)spec_lookup
}, /* lookup */
313 { &vnop_create_desc
, (vnop_t
*)spec_create
}, /* create */
314 { &vnop_mknod_desc
, (vnop_t
*)spec_mknod
}, /* mknod */
315 { &vnop_open_desc
, (vnop_t
*)spec_open
}, /* open */
316 { &vnop_close_desc
, (vnop_t
*)nfsspec_vnop_close
}, /* close */
317 { &vnop_getattr_desc
, (vnop_t
*)nfs4_vnop_getattr
}, /* getattr */
318 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
319 { &vnop_read_desc
, (vnop_t
*)nfsspec_vnop_read
}, /* read */
320 { &vnop_write_desc
, (vnop_t
*)nfsspec_vnop_write
}, /* write */
321 { &vnop_ioctl_desc
, (vnop_t
*)spec_ioctl
}, /* ioctl */
322 { &vnop_select_desc
, (vnop_t
*)spec_select
}, /* select */
323 { &vnop_revoke_desc
, (vnop_t
*)spec_revoke
}, /* revoke */
324 { &vnop_mmap_desc
, (vnop_t
*)spec_mmap
}, /* mmap */
325 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
326 { &vnop_remove_desc
, (vnop_t
*)spec_remove
}, /* remove */
327 { &vnop_link_desc
, (vnop_t
*)spec_link
}, /* link */
328 { &vnop_rename_desc
, (vnop_t
*)spec_rename
}, /* rename */
329 { &vnop_mkdir_desc
, (vnop_t
*)spec_mkdir
}, /* mkdir */
330 { &vnop_rmdir_desc
, (vnop_t
*)spec_rmdir
}, /* rmdir */
331 { &vnop_symlink_desc
, (vnop_t
*)spec_symlink
}, /* symlink */
332 { &vnop_readdir_desc
, (vnop_t
*)spec_readdir
}, /* readdir */
333 { &vnop_readlink_desc
, (vnop_t
*)spec_readlink
}, /* readlink */
334 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
335 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
336 { &vnop_strategy_desc
, (vnop_t
*)spec_strategy
}, /* strategy */
337 { &vnop_pathconf_desc
, (vnop_t
*)spec_pathconf
}, /* pathconf */
338 { &vnop_advlock_desc
, (vnop_t
*)spec_advlock
}, /* advlock */
339 { &vnop_bwrite_desc
, (vnop_t
*)vn_bwrite
}, /* bwrite */
340 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
341 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
342 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
343 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
344 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
345 { &vnop_getxattr_desc
, (vnop_t
*)nfs4_vnop_getxattr
}, /* getxattr */
346 { &vnop_setxattr_desc
, (vnop_t
*)nfs4_vnop_setxattr
}, /* setxattr */
347 { &vnop_removexattr_desc
, (vnop_t
*)nfs4_vnop_removexattr
},/* removexattr */
348 { &vnop_listxattr_desc
, (vnop_t
*)nfs4_vnop_listxattr
},/* listxattr */
350 { &vnop_getnamedstream_desc
, (vnop_t
*)nfs4_vnop_getnamedstream
}, /* getnamedstream */
351 { &vnop_makenamedstream_desc
, (vnop_t
*)nfs4_vnop_makenamedstream
}, /* makenamedstream */
352 { &vnop_removenamedstream_desc
, (vnop_t
*)nfs4_vnop_removenamedstream
},/* removenamedstream */
354 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
357 const struct vnodeopv_desc spec_nfsv4nodeop_opv_desc
=
358 { &spec_nfsv4nodeop_p
, spec_nfsv4nodeop_entries
};
359 #endif /* CONFIG_NFS4 */
362 vnop_t
**fifo_nfsv2nodeop_p
;
363 static const struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries
[] = {
364 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
365 { &vnop_lookup_desc
, (vnop_t
*)fifo_lookup
}, /* lookup */
366 { &vnop_create_desc
, (vnop_t
*)fifo_create
}, /* create */
367 { &vnop_mknod_desc
, (vnop_t
*)fifo_mknod
}, /* mknod */
368 { &vnop_open_desc
, (vnop_t
*)fifo_open
}, /* open */
369 { &vnop_close_desc
, (vnop_t
*)nfsfifo_vnop_close
}, /* close */
370 { &vnop_getattr_desc
, (vnop_t
*)nfs3_vnop_getattr
}, /* getattr */
371 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
372 { &vnop_read_desc
, (vnop_t
*)nfsfifo_vnop_read
}, /* read */
373 { &vnop_write_desc
, (vnop_t
*)nfsfifo_vnop_write
}, /* write */
374 { &vnop_ioctl_desc
, (vnop_t
*)fifo_ioctl
}, /* ioctl */
375 { &vnop_select_desc
, (vnop_t
*)fifo_select
}, /* select */
376 { &vnop_revoke_desc
, (vnop_t
*)fifo_revoke
}, /* revoke */
377 { &vnop_mmap_desc
, (vnop_t
*)fifo_mmap
}, /* mmap */
378 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
379 { &vnop_remove_desc
, (vnop_t
*)fifo_remove
}, /* remove */
380 { &vnop_link_desc
, (vnop_t
*)fifo_link
}, /* link */
381 { &vnop_rename_desc
, (vnop_t
*)fifo_rename
}, /* rename */
382 { &vnop_mkdir_desc
, (vnop_t
*)fifo_mkdir
}, /* mkdir */
383 { &vnop_rmdir_desc
, (vnop_t
*)fifo_rmdir
}, /* rmdir */
384 { &vnop_symlink_desc
, (vnop_t
*)fifo_symlink
}, /* symlink */
385 { &vnop_readdir_desc
, (vnop_t
*)fifo_readdir
}, /* readdir */
386 { &vnop_readlink_desc
, (vnop_t
*)fifo_readlink
}, /* readlink */
387 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
388 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
389 { &vnop_strategy_desc
, (vnop_t
*)fifo_strategy
}, /* strategy */
390 { &vnop_pathconf_desc
, (vnop_t
*)fifo_pathconf
}, /* pathconf */
391 { &vnop_advlock_desc
, (vnop_t
*)fifo_advlock
}, /* advlock */
392 { &vnop_bwrite_desc
, (vnop_t
*)vn_bwrite
}, /* bwrite */
393 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
394 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
395 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
396 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
397 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
398 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
401 const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc
=
402 { &fifo_nfsv2nodeop_p
, fifo_nfsv2nodeop_entries
};
407 vnop_t
**fifo_nfsv4nodeop_p
;
408 static const struct vnodeopv_entry_desc fifo_nfsv4nodeop_entries
[] = {
409 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
410 { &vnop_lookup_desc
, (vnop_t
*)fifo_lookup
}, /* lookup */
411 { &vnop_create_desc
, (vnop_t
*)fifo_create
}, /* create */
412 { &vnop_mknod_desc
, (vnop_t
*)fifo_mknod
}, /* mknod */
413 { &vnop_open_desc
, (vnop_t
*)fifo_open
}, /* open */
414 { &vnop_close_desc
, (vnop_t
*)nfsfifo_vnop_close
}, /* close */
415 { &vnop_getattr_desc
, (vnop_t
*)nfs4_vnop_getattr
}, /* getattr */
416 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
417 { &vnop_read_desc
, (vnop_t
*)nfsfifo_vnop_read
}, /* read */
418 { &vnop_write_desc
, (vnop_t
*)nfsfifo_vnop_write
}, /* write */
419 { &vnop_ioctl_desc
, (vnop_t
*)fifo_ioctl
}, /* ioctl */
420 { &vnop_select_desc
, (vnop_t
*)fifo_select
}, /* select */
421 { &vnop_revoke_desc
, (vnop_t
*)fifo_revoke
}, /* revoke */
422 { &vnop_mmap_desc
, (vnop_t
*)fifo_mmap
}, /* mmap */
423 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
424 { &vnop_remove_desc
, (vnop_t
*)fifo_remove
}, /* remove */
425 { &vnop_link_desc
, (vnop_t
*)fifo_link
}, /* link */
426 { &vnop_rename_desc
, (vnop_t
*)fifo_rename
}, /* rename */
427 { &vnop_mkdir_desc
, (vnop_t
*)fifo_mkdir
}, /* mkdir */
428 { &vnop_rmdir_desc
, (vnop_t
*)fifo_rmdir
}, /* rmdir */
429 { &vnop_symlink_desc
, (vnop_t
*)fifo_symlink
}, /* symlink */
430 { &vnop_readdir_desc
, (vnop_t
*)fifo_readdir
}, /* readdir */
431 { &vnop_readlink_desc
, (vnop_t
*)fifo_readlink
}, /* readlink */
432 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
433 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
434 { &vnop_strategy_desc
, (vnop_t
*)fifo_strategy
}, /* strategy */
435 { &vnop_pathconf_desc
, (vnop_t
*)fifo_pathconf
}, /* pathconf */
436 { &vnop_advlock_desc
, (vnop_t
*)fifo_advlock
}, /* advlock */
437 { &vnop_bwrite_desc
, (vnop_t
*)vn_bwrite
}, /* bwrite */
438 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
439 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
440 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
441 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
442 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
443 { &vnop_getxattr_desc
, (vnop_t
*)nfs4_vnop_getxattr
}, /* getxattr */
444 { &vnop_setxattr_desc
, (vnop_t
*)nfs4_vnop_setxattr
}, /* setxattr */
445 { &vnop_removexattr_desc
, (vnop_t
*)nfs4_vnop_removexattr
},/* removexattr */
446 { &vnop_listxattr_desc
, (vnop_t
*)nfs4_vnop_listxattr
},/* listxattr */
448 { &vnop_getnamedstream_desc
, (vnop_t
*)nfs4_vnop_getnamedstream
}, /* getnamedstream */
449 { &vnop_makenamedstream_desc
, (vnop_t
*)nfs4_vnop_makenamedstream
}, /* makenamedstream */
450 { &vnop_removenamedstream_desc
, (vnop_t
*)nfs4_vnop_removenamedstream
},/* removenamedstream */
452 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
455 const struct vnodeopv_desc fifo_nfsv4nodeop_opv_desc
=
456 { &fifo_nfsv4nodeop_p
, fifo_nfsv4nodeop_entries
};
458 #endif /* CONFIG_NFS4 */
460 int nfs_sillyrename(nfsnode_t
, nfsnode_t
, struct componentname
*, vfs_context_t
);
461 int nfs_getattr_internal(nfsnode_t
, struct nfs_vattr
*, vfs_context_t
, int);
462 int nfs_refresh_fh(nfsnode_t
, vfs_context_t
);
466 * Find the slot in the access cache for this UID.
467 * If adding and no existing slot is found, reuse slots in FIFO order.
468 * The index of the next slot to use is kept in the last entry of the n_access array.
471 nfs_node_access_slot(nfsnode_t np
, uid_t uid
, int add
)
475 for (slot
= 0; slot
< NFS_ACCESS_CACHE_SIZE
; slot
++) {
476 if (np
->n_accessuid
[slot
] == uid
) {
480 if (slot
== NFS_ACCESS_CACHE_SIZE
) {
484 slot
= np
->n_access
[NFS_ACCESS_CACHE_SIZE
];
485 np
->n_access
[NFS_ACCESS_CACHE_SIZE
] = (slot
+ 1) % NFS_ACCESS_CACHE_SIZE
;
491 nfs3_access_rpc(nfsnode_t np
, u_int32_t
*access
, int rpcflags
, vfs_context_t ctx
)
493 int error
= 0, lockerror
= ENOENT
, status
, slot
;
494 uint32_t access_result
= 0;
496 struct nfsm_chain nmreq
, nmrep
;
497 struct nfsmount
*nmp
;
501 nfsm_chain_null(&nmreq
);
502 nfsm_chain_null(&nmrep
);
504 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(NFS_VER3
) + NFSX_UNSIGNED
);
505 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER3
, np
->n_fhp
, np
->n_fhsize
);
506 nfsm_chain_add_32(error
, &nmreq
, *access
);
507 nfsm_chain_build_done(error
, &nmreq
);
509 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC_ACCESS
,
510 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
511 NULL
, rpcflags
, &nmrep
, &xid
, &status
);
512 if ((lockerror
= nfs_node_lock(np
))) {
515 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
519 nfsm_chain_get_32(error
, &nmrep
, access_result
);
522 /* XXXab do we really need mount here, also why are we doing access cache management here? */
524 if (nfs_mount_gone(nmp
)) {
530 if (auth_is_kerberized(np
->n_auth
) || auth_is_kerberized(nmp
->nm_auth
)) {
531 uid
= nfs_cred_getasid2uid(vfs_context_ucred(ctx
));
533 uid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
536 uid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
537 #endif /* CONFIG_NFS_GSS */
538 slot
= nfs_node_access_slot(np
, uid
, 1);
539 np
->n_accessuid
[slot
] = uid
;
541 np
->n_accessstamp
[slot
] = now
.tv_sec
;
542 np
->n_access
[slot
] = access_result
;
545 * If we asked for DELETE but didn't get it, the server
546 * may simply not support returning that bit (possible
547 * on UNIX systems). So, we'll assume that it is OK,
548 * and just let any subsequent delete action fail if it
549 * really isn't deletable.
551 if ((*access
& NFS_ACCESS_DELETE
) &&
552 !(np
->n_access
[slot
] & NFS_ACCESS_DELETE
)) {
553 np
->n_access
[slot
] |= NFS_ACCESS_DELETE
;
555 /* ".zfs" subdirectories may erroneously give a denied answer for add/remove */
556 if (nfs_access_dotzfs
&& (np
->n_flag
& NISDOTZFSCHILD
)) {
557 np
->n_access
[slot
] |= (NFS_ACCESS_MODIFY
| NFS_ACCESS_EXTEND
| NFS_ACCESS_DELETE
);
559 /* pass back the access returned with this request */
560 *access
= np
->n_access
[slot
];
565 nfsm_chain_cleanup(&nmreq
);
566 nfsm_chain_cleanup(&nmrep
);
572 * NFS access vnode op.
573 * For NFS version 2, just return ok. File accesses may fail later.
574 * For NFS version 3+, use the access RPC to check accessibility. If file
575 * permissions are changed on the server, accesses might still fail later.
579 struct vnop_access_args
/* {
580 * struct vnodeop_desc *a_desc;
583 * vfs_context_t a_context;
586 vfs_context_t ctx
= ap
->a_context
;
587 vnode_t vp
= ap
->a_vp
;
588 int error
= 0, slot
, dorpc
, rpcflags
= 0;
589 u_int32_t access
, waccess
;
590 nfsnode_t np
= VTONFS(vp
);
591 struct nfsmount
*nmp
;
597 if (nfs_mount_gone(nmp
)) {
600 nfsvers
= nmp
->nm_vers
;
603 if (nfsvers
== NFS_VER2
|| NMFLAG(nmp
, NOOPAQUE_AUTH
)) {
604 if ((ap
->a_action
& KAUTH_VNODE_WRITE_RIGHTS
) &&
605 vfs_isrdonly(vnode_mount(vp
))) {
612 * For NFS v3, do an access rpc, otherwise you are stuck emulating
613 * ufs_access() locally using the vattr. This may not be correct,
614 * since the server may apply other access criteria such as
615 * client uid-->server uid mapping that we do not know about, but
616 * this is better than just returning anything that is lying about
621 * Convert KAUTH primitives to NFS access rights.
624 if (vnode_isdir(vp
)) {
627 (KAUTH_VNODE_LIST_DIRECTORY
|
628 KAUTH_VNODE_READ_EXTATTRIBUTES
)) {
629 access
|= NFS_ACCESS_READ
;
631 if (ap
->a_action
& KAUTH_VNODE_SEARCH
) {
632 access
|= NFS_ACCESS_LOOKUP
;
635 (KAUTH_VNODE_ADD_FILE
|
636 KAUTH_VNODE_ADD_SUBDIRECTORY
)) {
637 access
|= NFS_ACCESS_MODIFY
| NFS_ACCESS_EXTEND
;
639 if (ap
->a_action
& KAUTH_VNODE_DELETE_CHILD
) {
640 access
|= NFS_ACCESS_MODIFY
;
645 (KAUTH_VNODE_READ_DATA
|
646 KAUTH_VNODE_READ_EXTATTRIBUTES
)) {
647 access
|= NFS_ACCESS_READ
;
649 if (ap
->a_action
& KAUTH_VNODE_WRITE_DATA
) {
650 access
|= NFS_ACCESS_MODIFY
| NFS_ACCESS_EXTEND
;
652 if (ap
->a_action
& KAUTH_VNODE_APPEND_DATA
) {
653 access
|= NFS_ACCESS_EXTEND
;
655 if (ap
->a_action
& KAUTH_VNODE_EXECUTE
) {
656 access
|= NFS_ACCESS_EXECUTE
;
660 if (ap
->a_action
& KAUTH_VNODE_DELETE
) {
661 access
|= NFS_ACCESS_DELETE
;
664 (KAUTH_VNODE_WRITE_ATTRIBUTES
|
665 KAUTH_VNODE_WRITE_EXTATTRIBUTES
|
666 KAUTH_VNODE_WRITE_SECURITY
)) {
667 access
|= NFS_ACCESS_MODIFY
;
669 /* XXX this is pretty dubious */
670 if (ap
->a_action
& KAUTH_VNODE_CHANGE_OWNER
) {
671 access
|= NFS_ACCESS_MODIFY
;
674 /* if caching, always ask for every right */
675 if (nfs_access_cache_timeout
> 0) {
676 waccess
= NFS_ACCESS_READ
| NFS_ACCESS_MODIFY
|
677 NFS_ACCESS_EXTEND
| NFS_ACCESS_EXECUTE
|
678 NFS_ACCESS_DELETE
| NFS_ACCESS_LOOKUP
;
683 if ((error
= nfs_node_lock(np
))) {
688 * Does our cached result allow us to give a definite yes to
692 if (auth_is_kerberized(np
->n_auth
) || auth_is_kerberized(nmp
->nm_auth
)) {
693 uid
= nfs_cred_getasid2uid(vfs_context_ucred(ctx
));
695 uid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
698 uid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
699 #endif /* CONFIG_NFS_GSS */
700 slot
= nfs_node_access_slot(np
, uid
, 0);
703 /* not asking for any rights understood by NFS, so don't bother doing an RPC */
704 /* OSAddAtomic(1, &nfsstats.accesscache_hits); */
707 } else if (NACCESSVALID(np
, slot
)) {
709 if (((now
.tv_sec
< (np
->n_accessstamp
[slot
] + nfs_access_cache_timeout
)) &&
710 ((np
->n_access
[slot
] & access
) == access
)) || nfs_use_cache(nmp
)) {
711 /* OSAddAtomic(1, &nfsstats.accesscache_hits); */
713 waccess
= np
->n_access
[slot
];
718 /* Either a no, or a don't know. Go to the wire. */
719 /* OSAddAtomic(1, &nfsstats.accesscache_misses); */
722 * Allow an access call to timeout if we have it cached
723 * so we won't hang if the server isn't responding.
725 if (NACCESSVALID(np
, slot
)) {
729 error
= nmp
->nm_funcs
->nf_access_rpc(np
, &waccess
, rpcflags
, ctx
);
732 * If the server didn't respond return the cached access.
734 if ((error
== ETIMEDOUT
) && (rpcflags
& R_SOFT
)) {
736 waccess
= np
->n_access
[slot
];
739 if (!error
&& ((waccess
& access
) != access
)) {
750 * Perform various update/invalidation checks and then add the
751 * open to the node. Regular files will have an open file structure
752 * on the node and, for NFSv4, perform an OPEN request on the server.
756 struct vnop_open_args
/* {
757 * struct vnodeop_desc *a_desc;
760 * vfs_context_t a_context;
763 vfs_context_t ctx
= ap
->a_context
;
764 vnode_t vp
= ap
->a_vp
;
765 nfsnode_t np
= VTONFS(vp
);
766 struct nfsmount
*nmp
;
767 int error
, accessMode
, denyMode
, opened
= 0;
768 struct nfs_open_owner
*noop
= NULL
;
769 struct nfs_open_file
*nofp
= NULL
;
772 if (!(ap
->a_mode
& (FREAD
| FWRITE
))) {
777 if (nfs_mount_gone(nmp
)) {
780 if (np
->n_flag
& NREVOKE
) {
784 vtype
= vnode_vtype(vp
);
785 if ((vtype
!= VREG
) && (vtype
!= VDIR
) && (vtype
!= VLNK
)) {
789 /* First, check if we need to update/invalidate */
790 if (ISSET(np
->n_flag
, NUPDATESIZE
)) {
791 nfs_data_update_size(np
, 0);
793 if ((error
= nfs_node_lock(np
))) {
796 if (np
->n_flag
& NNEEDINVALIDATE
) {
797 np
->n_flag
&= ~NNEEDINVALIDATE
;
802 nfs_vinvalbuf(vp
, V_SAVE
| V_IGNORE_WRITEERR
, ctx
, 1);
803 if ((error
= nfs_node_lock(np
))) {
808 np
->n_lastrahead
= -1;
810 if (np
->n_flag
& NMODIFIED
) {
815 if ((error
= nfs_vinvalbuf(vp
, V_SAVE
| V_IGNORE_WRITEERR
, ctx
, 1))) {
822 /* nfs_getattr() will check changed and purge caches */
823 if ((error
= nfs_getattr(np
, NULL
, ctx
, NGA_UNCACHED
))) {
828 /* Just mark that it was opened */
829 lck_mtx_lock(&np
->n_openlock
);
831 lck_mtx_unlock(&np
->n_openlock
);
835 /* mode contains some combination of: FREAD, FWRITE, O_SHLOCK, O_EXLOCK */
837 if (ap
->a_mode
& FREAD
) {
838 accessMode
|= NFS_OPEN_SHARE_ACCESS_READ
;
840 if (ap
->a_mode
& FWRITE
) {
841 accessMode
|= NFS_OPEN_SHARE_ACCESS_WRITE
;
843 if (ap
->a_mode
& O_EXLOCK
) {
844 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
845 } else if (ap
->a_mode
& O_SHLOCK
) {
846 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
848 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
850 // XXX don't do deny modes just yet (and never do it for !v4)
851 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
853 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
859 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
861 nfs_open_owner_rele(noop
);
864 if (np
->n_flag
& NREVOKE
) {
866 nfs_mount_state_in_use_end(nmp
, 0);
867 nfs_open_owner_rele(noop
);
871 error
= nfs_open_file_find(np
, noop
, &nofp
, accessMode
, denyMode
, 1);
872 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
873 NP(np
, "nfs_vnop_open: LOST %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
877 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
878 nfs_mount_state_in_use_end(nmp
, 0);
879 error
= nfs4_reopen(nofp
, vfs_context_thread(ctx
));
887 error
= nfs_open_file_set_busy(nofp
, vfs_context_thread(ctx
));
894 if (nmp
->nm_vers
< NFS_VER4
) {
896 * NFS v2/v3 opens are always allowed - so just add it.
898 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, 0);
903 * If we just created the file and the modes match, then we simply use
904 * the open performed in the create. Otherwise, send the request.
906 if ((nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) &&
907 (nofp
->nof_creator
== current_thread()) &&
908 (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
) &&
909 (denyMode
== NFS_OPEN_SHARE_DENY_NONE
)) {
910 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
911 nofp
->nof_creator
= NULL
;
915 error
= nfs4_open(np
, nofp
, accessMode
, denyMode
, ctx
);
918 if ((error
== EACCES
) && (nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) &&
919 (nofp
->nof_creator
== current_thread())) {
921 * Ugh. This can happen if we just created the file with read-only
922 * perms and we're trying to open it for real with different modes
923 * (e.g. write-only or with a deny mode) and the server decides to
924 * not allow the second open because of the read-only perms.
925 * The best we can do is to just use the create's open.
926 * We may have access we don't need or we may not have a requested
927 * deny mode. We may log complaints later, but we'll try to avoid it.
929 if (denyMode
!= NFS_OPEN_SHARE_DENY_NONE
) {
930 NP(np
, "nfs_vnop_open: deny mode foregone on create, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
932 nofp
->nof_creator
= NULL
;
940 * If we had just created the file, we already had it open.
941 * If the actual open mode is less than what we grabbed at
942 * create time, then we'll downgrade the open here.
944 if ((nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) &&
945 (nofp
->nof_creator
== current_thread())) {
946 error
= nfs_close(np
, nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
948 NP(np
, "nfs_vnop_open: create close error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
950 if (!nfs_mount_state_error_should_restart(error
)) {
952 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
959 nfs_open_file_clear_busy(nofp
);
961 if (nfs_mount_state_in_use_end(nmp
, error
)) {
966 NP(np
, "nfs_vnop_open: error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
969 nfs_open_owner_rele(noop
);
971 if (!error
&& vtype
== VREG
&& (ap
->a_mode
& FWRITE
)) {
972 lck_mtx_lock(&nmp
->nm_lock
);
973 nmp
->nm_state
&= ~NFSSTA_SQUISHY
;
974 nmp
->nm_curdeadtimeout
= nmp
->nm_deadtimeout
;
975 if (nmp
->nm_curdeadtimeout
<= 0) {
976 nmp
->nm_deadto_start
= 0;
979 lck_mtx_unlock(&nmp
->nm_lock
);
986 nfs_no_of_open_file_writers(nfsnode_t np
)
988 uint32_t writers
= 0;
989 struct nfs_open_file
*nofp
;
991 TAILQ_FOREACH(nofp
, &np
->n_opens
, nof_link
) {
992 writers
+= nofp
->nof_w
+ nofp
->nof_rw
+ nofp
->nof_w_dw
+ nofp
->nof_rw_dw
+
993 nofp
->nof_w_drw
+ nofp
->nof_rw_drw
+ nofp
->nof_d_w_dw
+
994 nofp
->nof_d_rw_dw
+ nofp
->nof_d_w_drw
+ nofp
->nof_d_rw_drw
+
995 nofp
->nof_d_w
+ nofp
->nof_d_rw
;
1002 * NFS close vnode op
1004 * What an NFS client should do upon close after writing is a debatable issue.
1005 * Most NFS clients push delayed writes to the server upon close, basically for
1007 * 1 - So that any write errors may be reported back to the client process
1008 * doing the close system call. By far the two most likely errors are
1009 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
1010 * 2 - To put a worst case upper bound on cache inconsistency between
1011 * multiple clients for the file.
1012 * There is also a consistency problem for Version 2 of the protocol w.r.t.
1013 * not being able to tell if other clients are writing a file concurrently,
1014 * since there is no way of knowing if the changed modify time in the reply
1015 * is only due to the write for this client.
1016 * (NFS Version 3 provides weak cache consistency data in the reply that
1017 * should be sufficient to detect and handle this case.)
1019 * The current code does the following:
1020 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
1021 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate them.
1022 * for NFS Version 4 - basically the same as NFSv3
1026 struct vnop_close_args
/* {
1027 * struct vnodeop_desc *a_desc;
1030 * vfs_context_t a_context;
1033 vfs_context_t ctx
= ap
->a_context
;
1034 vnode_t vp
= ap
->a_vp
;
1035 nfsnode_t np
= VTONFS(vp
);
1036 struct nfsmount
*nmp
;
1037 int error
= 0, error1
, nfsvers
;
1038 int fflag
= ap
->a_fflag
;
1040 int accessMode
, denyMode
;
1041 struct nfs_open_owner
*noop
= NULL
;
1042 struct nfs_open_file
*nofp
= NULL
;
1048 nfsvers
= nmp
->nm_vers
;
1049 vtype
= vnode_vtype(vp
);
1051 /* First, check if we need to update/flush/invalidate */
1052 if (ISSET(np
->n_flag
, NUPDATESIZE
)) {
1053 nfs_data_update_size(np
, 0);
1055 nfs_node_lock_force(np
);
1056 if (np
->n_flag
& NNEEDINVALIDATE
) {
1057 np
->n_flag
&= ~NNEEDINVALIDATE
;
1058 nfs_node_unlock(np
);
1059 nfs_vinvalbuf(vp
, V_SAVE
| V_IGNORE_WRITEERR
, ctx
, 1);
1060 nfs_node_lock_force(np
);
1062 if ((vtype
== VREG
) && (np
->n_flag
& NMODIFIED
) && (fflag
& FWRITE
)) {
1063 /* we're closing an open for write and the file is modified, so flush it */
1064 nfs_node_unlock(np
);
1065 if (nfsvers
!= NFS_VER2
) {
1066 error
= nfs_flush(np
, MNT_WAIT
, vfs_context_thread(ctx
), 0);
1068 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
1070 nfs_node_lock_force(np
);
1071 NATTRINVALIDATE(np
);
1073 if (np
->n_flag
& NWRITEERR
) {
1074 np
->n_flag
&= ~NWRITEERR
;
1075 error
= np
->n_error
;
1077 nfs_node_unlock(np
);
1079 if (vtype
!= VREG
) {
1080 /* Just mark that it was closed */
1081 lck_mtx_lock(&np
->n_openlock
);
1082 if (np
->n_openrefcnt
== 0) {
1083 if (fflag
& (FREAD
| FWRITE
)) {
1084 NP(np
, "nfs_vnop_close: open reference underrun");
1087 } else if (fflag
& (FREAD
| FWRITE
)) {
1090 /* No FREAD/FWRITE set - probably the final close */
1091 np
->n_openrefcnt
= 0;
1093 lck_mtx_unlock(&np
->n_openlock
);
1098 /* fflag should contain some combination of: FREAD, FWRITE, FHASLOCK */
1100 if (fflag
& FREAD
) {
1101 accessMode
|= NFS_OPEN_SHARE_ACCESS_READ
;
1103 if (fflag
& FWRITE
) {
1104 accessMode
|= NFS_OPEN_SHARE_ACCESS_WRITE
;
1106 // XXX It would be nice if we still had the O_EXLOCK/O_SHLOCK flags that were on the open
1107 // if (fflag & O_EXLOCK)
1108 // denyMode = NFS_OPEN_SHARE_DENY_BOTH;
1109 // else if (fflag & O_SHLOCK)
1110 // denyMode = NFS_OPEN_SHARE_DENY_WRITE;
1112 // denyMode = NFS_OPEN_SHARE_DENY_NONE;
1114 if (fflag
& FHASLOCK
) {
1115 /* XXX assume FHASLOCK is for the deny mode and not flock */
1116 /* FHASLOCK flock will be unlocked in the close path, but the flag is not cleared. */
1117 if (nofp
->nof_deny
& NFS_OPEN_SHARE_DENY_READ
) {
1118 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
1119 } else if (nofp
->nof_deny
& NFS_OPEN_SHARE_DENY_WRITE
) {
1120 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
1122 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
1125 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
1128 // XXX don't do deny modes just yet (and never do it for !v4)
1129 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
1134 * No mode given to close?
1135 * Guess this is the final close.
1136 * We should unlock all locks and close all opens.
1139 mount_t mp
= vnode_mount(vp
);
1140 int force
= (!mp
|| vfs_isforce(mp
));
1142 writers
= nfs_no_of_open_file_writers(np
);
1143 nfs_release_open_state_for_node(np
, force
);
1145 lck_mtx_lock(&nmp
->nm_lock
);
1146 if (writers
> nmp
->nm_writers
) {
1147 NP(np
, "nfs_vnop_close: number of write opens for mount underrun. Node has %d"
1148 " opens for write. Mount has total of %d opens for write\n",
1149 writers
, nmp
->nm_writers
);
1150 nmp
->nm_writers
= 0;
1152 nmp
->nm_writers
-= writers
;
1154 lck_mtx_unlock(&nmp
->nm_lock
);
1158 } else if (fflag
& FWRITE
) {
1159 lck_mtx_lock(&nmp
->nm_lock
);
1160 if (nmp
->nm_writers
== 0) {
1161 NP(np
, "nfs_vnop_close: removing open writer from mount, but mount has no files open for writing");
1165 lck_mtx_unlock(&nmp
->nm_lock
);
1169 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 0);
1171 // printf("nfs_vnop_close: can't get open owner!\n");
1176 error
= nfs_mount_state_in_use_start(nmp
, NULL
);
1178 nfs_open_owner_rele(noop
);
1182 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 0);
1184 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
1185 nfs_mount_state_in_use_end(nmp
, 0);
1186 error
= nfs4_reopen(nofp
, NULL
);
1194 NP(np
, "nfs_vnop_close: no open file for owner, error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
1198 error
= nfs_open_file_set_busy(nofp
, NULL
);
1204 error
= nfs_close(np
, nofp
, accessMode
, denyMode
, ctx
);
1206 NP(np
, "nfs_vnop_close: close error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
1211 nfs_open_file_clear_busy(nofp
);
1213 if (nfs_mount_state_in_use_end(nmp
, error
)) {
1221 NP(np
, "nfs_vnop_close: error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
1224 nfs_open_owner_rele(noop
);
1230 * nfs_close(): common function that does all the heavy lifting of file closure
1232 * Takes an open file structure and a set of access/deny modes and figures out how
1233 * to update the open file structure (and the state on the server) appropriately.
1238 struct nfs_open_file
*nofp
,
1239 uint32_t accessMode
,
1244 struct nfs_lock_owner
*nlop
;
1246 int error
= 0, changed
= 0, delegated
= 0, closed
= 0, downgrade
= 0;
1247 uint32_t newAccessMode
, newDenyMode
;
1249 /* warn if modes don't match current state */
1250 if (((accessMode
& nofp
->nof_access
) != accessMode
) || ((denyMode
& nofp
->nof_deny
) != denyMode
)) {
1251 NP(np
, "nfs_close: mode mismatch %d %d, current %d %d, %d",
1252 accessMode
, denyMode
, nofp
->nof_access
, nofp
->nof_deny
,
1253 kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1257 * If we're closing a write-only open, we may not have a write-only count
1258 * if we also grabbed read access. So, check the read-write count.
1260 if (denyMode
== NFS_OPEN_SHARE_DENY_NONE
) {
1261 if ((accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) &&
1262 (nofp
->nof_w
== 0) && (nofp
->nof_d_w
== 0) &&
1263 (nofp
->nof_rw
|| nofp
->nof_d_rw
)) {
1264 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
1266 } else if (denyMode
== NFS_OPEN_SHARE_DENY_WRITE
) {
1267 if ((accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) &&
1268 (nofp
->nof_w_dw
== 0) && (nofp
->nof_d_w_dw
== 0) &&
1269 (nofp
->nof_rw_dw
|| nofp
->nof_d_rw_dw
)) {
1270 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
1272 } else { /* NFS_OPEN_SHARE_DENY_BOTH */
1273 if ((accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) &&
1274 (nofp
->nof_w_drw
== 0) && (nofp
->nof_d_w_drw
== 0) &&
1275 (nofp
->nof_rw_drw
|| nofp
->nof_d_rw_drw
)) {
1276 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
1280 nfs_open_file_remove_open_find(nofp
, accessMode
, denyMode
, &newAccessMode
, &newDenyMode
, &delegated
);
1281 if ((newAccessMode
!= nofp
->nof_access
) || (newDenyMode
!= nofp
->nof_deny
)) {
1287 if (NFSTONMP(np
)->nm_vers
< NFS_VER4
) {
1288 /* NFS v2/v3 closes simply need to remove the open. */
1292 if ((newAccessMode
== 0) || (nofp
->nof_opencnt
== 1)) {
1294 * No more access after this close, so clean up and close it.
1295 * Don't send a close RPC if we're closing a delegated open.
1299 if (!delegated
&& !(nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
1300 error
= nfs4_close_rpc(np
, nofp
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
), 0);
1302 if (error
== NFSERR_LOCKS_HELD
) {
1304 * Hmm... the server says we have locks we need to release first
1305 * Find the lock owner and try to unlock everything.
1307 nlop
= nfs_lock_owner_find(np
, vfs_context_proc(ctx
), 0);
1309 nfs4_unlock_rpc(np
, nlop
, F_WRLCK
, 0, UINT64_MAX
,
1310 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
1311 nfs_lock_owner_rele(nlop
);
1313 error
= nfs4_close_rpc(np
, nofp
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
), 0);
1315 } else if (changed
) {
1317 * File is still open but with less access, so downgrade the open.
1318 * Don't send a downgrade RPC if we're closing a delegated open.
1320 if (!delegated
&& !(nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
1323 * If we have delegated opens, we should probably claim them before sending
1324 * the downgrade because the server may not know the open we are downgrading to.
1326 if (nofp
->nof_d_rw_drw
|| nofp
->nof_d_w_drw
|| nofp
->nof_d_r_drw
||
1327 nofp
->nof_d_rw_dw
|| nofp
->nof_d_w_dw
|| nofp
->nof_d_r_dw
||
1328 nofp
->nof_d_rw
|| nofp
->nof_d_w
|| nofp
->nof_d_r
) {
1329 nfs4_claim_delegated_state_for_open_file(nofp
, 0);
1331 /* need to remove the open before sending the downgrade */
1332 nfs_open_file_remove_open(nofp
, accessMode
, denyMode
);
1333 error
= nfs4_open_downgrade_rpc(np
, nofp
, ctx
);
1334 if (error
) { /* Hmm.. that didn't work. Add the open back in. */
1335 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, delegated
);
1342 NP(np
, "nfs_close: error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1347 nfs_open_file_remove_open(nofp
, accessMode
, denyMode
);
1351 lck_mtx_lock(&nofp
->nof_lock
);
1352 if (nofp
->nof_r
|| nofp
->nof_d_r
|| nofp
->nof_w
|| nofp
->nof_d_w
|| nofp
->nof_d_rw
||
1353 (nofp
->nof_rw
&& !((nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) && !nofp
->nof_creator
&& (nofp
->nof_rw
== 1))) ||
1354 nofp
->nof_r_dw
|| nofp
->nof_d_r_dw
|| nofp
->nof_w_dw
|| nofp
->nof_d_w_dw
||
1355 nofp
->nof_rw_dw
|| nofp
->nof_d_rw_dw
|| nofp
->nof_r_drw
|| nofp
->nof_d_r_drw
||
1356 nofp
->nof_w_drw
|| nofp
->nof_d_w_drw
|| nofp
->nof_rw_drw
|| nofp
->nof_d_rw_drw
) {
1357 NP(np
, "nfs_close: unexpected count: %u.%u %u.%u %u.%u dw %u.%u %u.%u %u.%u drw %u.%u %u.%u %u.%u flags 0x%x, %d",
1358 nofp
->nof_r
, nofp
->nof_d_r
, nofp
->nof_w
, nofp
->nof_d_w
,
1359 nofp
->nof_rw
, nofp
->nof_d_rw
, nofp
->nof_r_dw
, nofp
->nof_d_r_dw
,
1360 nofp
->nof_w_dw
, nofp
->nof_d_w_dw
, nofp
->nof_rw_dw
, nofp
->nof_d_rw_dw
,
1361 nofp
->nof_r_drw
, nofp
->nof_d_r_drw
, nofp
->nof_w_drw
, nofp
->nof_d_w_drw
,
1362 nofp
->nof_rw_drw
, nofp
->nof_d_rw_drw
, nofp
->nof_flags
,
1363 kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1365 /* clear out all open info, just to be safe */
1366 nofp
->nof_access
= nofp
->nof_deny
= 0;
1367 nofp
->nof_mmap_access
= nofp
->nof_mmap_deny
= 0;
1368 nofp
->nof_r
= nofp
->nof_d_r
= 0;
1369 nofp
->nof_w
= nofp
->nof_d_w
= 0;
1370 nofp
->nof_rw
= nofp
->nof_d_rw
= 0;
1371 nofp
->nof_r_dw
= nofp
->nof_d_r_dw
= 0;
1372 nofp
->nof_w_dw
= nofp
->nof_d_w_dw
= 0;
1373 nofp
->nof_rw_dw
= nofp
->nof_d_rw_dw
= 0;
1374 nofp
->nof_r_drw
= nofp
->nof_d_r_drw
= 0;
1375 nofp
->nof_w_drw
= nofp
->nof_d_w_drw
= 0;
1376 nofp
->nof_rw_drw
= nofp
->nof_d_rw_drw
= 0;
1377 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
1378 lck_mtx_unlock(&nofp
->nof_lock
);
1379 /* XXX we may potentially want to clean up idle/unused open file structures */
1381 if (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
) {
1383 NP(np
, "nfs_close: LOST%s, %d", !nofp
->nof_opencnt
? " (last)" : "",
1384 kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1399 struct nfs_vattr
*nvap
,
1402 struct nfsmount
*nmp
= mp
? VFSTONFS(mp
) : NFSTONMP(np
);
1403 int error
= 0, status
, nfsvers
, rpcflags
= 0;
1404 struct nfsm_chain nmreq
, nmrep
;
1406 if (nfs_mount_gone(nmp
)) {
1409 nfsvers
= nmp
->nm_vers
;
1411 if (flags
& NGA_MONITOR
) { /* vnode monitor requests should be soft */
1412 rpcflags
= R_RECOVER
;
1415 if (flags
& NGA_SOFT
) { /* Return ETIMEDOUT if server not responding */
1419 nfsm_chain_null(&nmreq
);
1420 nfsm_chain_null(&nmrep
);
1422 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nfsvers
));
1423 if (nfsvers
!= NFS_VER2
) {
1424 nfsm_chain_add_32(error
, &nmreq
, fhsize
);
1426 nfsm_chain_add_opaque(error
, &nmreq
, fhp
, fhsize
);
1427 nfsm_chain_build_done(error
, &nmreq
);
1429 error
= nfs_request2(np
, mp
, &nmreq
, NFSPROC_GETATTR
,
1430 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
1431 NULL
, rpcflags
, &nmrep
, xidp
, &status
);
1436 error
= nfs_parsefattr(nmp
, &nmrep
, nfsvers
, nvap
);
1438 nfsm_chain_cleanup(&nmreq
);
1439 nfsm_chain_cleanup(&nmrep
);
1444 * nfs_refresh_fh will attempt to update the file handle for the node.
1446 * It only does this for symbolic links and regular files that are not currently opened.
1448 * On Success returns 0 and the nodes file handle is updated, or ESTALE on failure.
1451 nfs_refresh_fh(nfsnode_t np
, vfs_context_t ctx
)
1453 vnode_t dvp
, vp
= NFSTOV(np
);
1455 const char *v_name
= vnode_getname(vp
);
1457 int namelen
, fhsize
, refreshed
;
1458 int error
, wanted
= 0;
1460 struct timespec ts
= {.tv_sec
= 2, .tv_nsec
= 0};
1462 NFS_VNOP_DBG("vnode is %d\n", vnode_vtype(vp
));
1464 dvp
= vnode_parent(vp
);
1465 if ((vnode_vtype(vp
) != VREG
&& vnode_vtype(vp
) != VLNK
) ||
1466 v_name
== NULL
|| *v_name
== '\0' || dvp
== NULL
) {
1467 if (v_name
!= NULL
) {
1468 vnode_putname(v_name
);
1474 namelen
= strlen(v_name
);
1475 MALLOC(name
, char *, namelen
+ 1, M_TEMP
, M_WAITOK
);
1477 vnode_putname(v_name
);
1480 bcopy(v_name
, name
, namelen
+ 1);
1481 NFS_VNOP_DBG("Trying to refresh %s : %s\n", v_name
, name
);
1482 vnode_putname(v_name
);
1484 /* Allocate the maximum size file handle */
1485 MALLOC(fhp
, uint8_t *, NFS4_FHSIZE
, M_TEMP
, M_WAITOK
);
1491 if ((error
= nfs_node_lock(np
))) {
1497 fhsize
= np
->n_fhsize
;
1498 bcopy(np
->n_fhp
, fhp
, fhsize
);
1499 while (ISSET(np
->n_flag
, NREFRESH
)) {
1500 SET(np
->n_flag
, NREFRESHWANT
);
1501 NFS_VNOP_DBG("Waiting for refresh of %s\n", name
);
1502 msleep(np
, &np
->n_lock
, PZERO
- 1, "nfsrefreshwant", &ts
);
1503 if ((error
= nfs_sigintr(NFSTONMP(np
), NULL
, vfs_context_thread(ctx
), 0))) {
1507 refreshed
= error
? 0 : !NFS_CMPFH(np
, fhp
, fhsize
);
1508 SET(np
->n_flag
, NREFRESH
);
1509 nfs_node_unlock(np
);
1511 NFS_VNOP_DBG("error = %d, refreshed = %d\n", error
, refreshed
);
1512 if (error
|| refreshed
) {
1516 /* Check that there are no open references for this file */
1517 lck_mtx_lock(&np
->n_openlock
);
1518 if (np
->n_openrefcnt
|| !TAILQ_EMPTY(&np
->n_opens
) || !TAILQ_EMPTY(&np
->n_lock_owners
)) {
1520 struct nfs_open_file
*ofp
;
1522 TAILQ_FOREACH(ofp
, &np
->n_opens
, nof_link
) {
1523 cnt
+= ofp
->nof_opencnt
;
1526 lck_mtx_unlock(&np
->n_openlock
);
1527 NFS_VNOP_DBG("Can not refresh file handle for %s with open state\n", name
);
1528 NFS_VNOP_DBG("\topenrefcnt = %d, opens = %d lock_owners = %d\n",
1529 np
->n_openrefcnt
, cnt
, !TAILQ_EMPTY(&np
->n_lock_owners
));
1534 lck_mtx_unlock(&np
->n_openlock
);
1536 * Since the FH is currently stale we should not be able to
1537 * establish any open state until the FH is refreshed.
1540 error
= nfs_node_lock(np
);
1543 * Symlinks should never need invalidations and are holding
1544 * the one and only nfsbuf in an uncached acquired state
1545 * trying to do a readlink. So we will hang if we invalidate
1546 * in that case. Only in in the VREG case do we need to
1549 if (vnode_vtype(vp
) == VREG
) {
1550 np
->n_flag
&= ~NNEEDINVALIDATE
;
1551 nfs_node_unlock(np
);
1552 error
= nfs_vinvalbuf(vp
, V_IGNORE_WRITEERR
, ctx
, 1);
1554 NFS_VNOP_DBG("nfs_vinvalbuf returned %d\n", error
);
1558 nfs_node_unlock(np
);
1561 NFS_VNOP_DBG("Looking up %s\n", name
);
1562 error
= nfs_lookitup(dnp
, name
, namelen
, ctx
, &np
);
1564 NFS_VNOP_DBG("nfs_lookitup returned %d\n", error
);
1568 nfs_node_lock_force(np
);
1569 wanted
= ISSET(np
->n_flag
, NREFRESHWANT
);
1570 CLR(np
->n_flag
, NREFRESH
| NREFRESHWANT
);
1571 nfs_node_unlock(np
);
1577 NFS_VNOP_DBG("%s refreshed file handle\n", name
);
1583 return error
? ESTALE
: 0;
1587 nfs_getattr(nfsnode_t np
, struct nfs_vattr
*nvap
, vfs_context_t ctx
, int flags
)
1592 error
= nfs_getattr_internal(np
, nvap
, ctx
, flags
);
1593 if (error
== ESTALE
) {
1594 error
= nfs_refresh_fh(np
, ctx
);
1603 nfs_getattr_internal(nfsnode_t np
, struct nfs_vattr
*nvap
, vfs_context_t ctx
, int flags
)
1605 struct nfsmount
*nmp
;
1606 int error
= 0, nfsvers
, inprogset
= 0, wanted
= 0, avoidfloods
;
1607 struct nfs_vattr nvattr
;
1608 struct timespec ts
= { .tv_sec
= 2, .tv_nsec
= 0 };
1611 FSDBG_TOP(513, np
->n_size
, np
, np
->n_vattr
.nva_size
, np
->n_flag
);
1615 if (nfs_mount_gone(nmp
)) {
1618 nfsvers
= nmp
->nm_vers
;
1625 /* Update local times for special files. */
1626 if (np
->n_flag
& (NACC
| NUPD
)) {
1627 nfs_node_lock_force(np
);
1629 nfs_node_unlock(np
);
1631 /* Update size, if necessary */
1632 if (ISSET(np
->n_flag
, NUPDATESIZE
)) {
1633 nfs_data_update_size(np
, 0);
1636 error
= nfs_node_lock(np
);
1638 if (!(flags
& (NGA_UNCACHED
| NGA_MONITOR
)) || ((nfsvers
>= NFS_VER4
) && (np
->n_openflags
& N_DELEG_MASK
))) {
1640 * Use the cache or wait for any getattr in progress if:
1641 * - it's a cached request, or
1642 * - we have a delegation, or
1643 * - the server isn't responding
1646 error
= nfs_getattrcache(np
, nvap
, flags
);
1647 if (!error
|| (error
!= ENOENT
)) {
1648 nfs_node_unlock(np
);
1652 if (!ISSET(np
->n_flag
, NGETATTRINPROG
)) {
1655 if (flags
& NGA_MONITOR
) {
1656 /* no need to wait if a request is pending */
1657 error
= EINPROGRESS
;
1658 nfs_node_unlock(np
);
1661 SET(np
->n_flag
, NGETATTRWANT
);
1662 msleep(np
, &np
->n_lock
, PZERO
- 1, "nfsgetattrwant", &ts
);
1663 if ((error
= nfs_sigintr(NFSTONMP(np
), NULL
, vfs_context_thread(ctx
), 0))) {
1664 nfs_node_unlock(np
);
1668 SET(np
->n_flag
, NGETATTRINPROG
);
1670 } else if (!ISSET(np
->n_flag
, NGETATTRINPROG
)) {
1671 SET(np
->n_flag
, NGETATTRINPROG
);
1673 } else if (flags
& NGA_MONITOR
) {
1674 /* no need to make a request if one is pending */
1675 error
= EINPROGRESS
;
1677 nfs_node_unlock(np
);
1680 if (nfs_mount_gone(nmp
)) {
1688 * Return cached attributes if they are valid,
1689 * if the server doesn't respond, and this is
1690 * some softened up style of mount.
1692 if (NATTRVALID(np
) && nfs_use_cache(nmp
)) {
1697 * We might want to try to get both the attributes and access info by
1698 * making an ACCESS call and seeing if it returns updated attributes.
1699 * But don't bother if we aren't caching access info or if the
1700 * attributes returned wouldn't be cached.
1702 if (!(flags
& NGA_ACL
) && (nfsvers
!= NFS_VER2
) && nfs_access_for_getattr
&& (nfs_access_cache_timeout
> 0)) {
1703 if (nfs_attrcachetimeout(np
) > 0) {
1704 /* OSAddAtomic(1, &nfsstats.accesscache_misses); */
1705 u_int32_t access
= NFS_ACCESS_ALL
;
1708 /* Return cached attrs if server doesn't respond */
1709 if (flags
& NGA_SOFT
) {
1713 error
= nmp
->nm_funcs
->nf_access_rpc(np
, &access
, rpcflags
, ctx
);
1715 if (error
== ETIMEDOUT
) {
1722 nfs_node_lock_force(np
);
1723 error
= nfs_getattrcache(np
, nvap
, flags
);
1724 nfs_node_unlock(np
);
1725 if (!error
|| (error
!= ENOENT
)) {
1728 /* Well, that didn't work... just do a getattr... */
1736 error
= nmp
->nm_funcs
->nf_getattr_rpc(np
, NULL
, np
->n_fhp
, np
->n_fhsize
, flags
, ctx
, nvap
, &xid
);
1738 nfs_node_lock_force(np
);
1739 error
= nfs_loadattrcache(np
, nvap
, &xid
, 0);
1740 nfs_node_unlock(np
);
1744 * If the server didn't respond, return cached attributes.
1747 if ((flags
& NGA_SOFT
) && (error
== ETIMEDOUT
)) {
1748 nfs_node_lock_force(np
);
1749 error
= nfs_getattrcache(np
, nvap
, flags
);
1750 if (!error
|| (error
!= ENOENT
)) {
1751 nfs_node_unlock(np
);
1754 nfs_node_unlock(np
);
1758 if (!xid
) { /* out-of-order rpc - attributes were dropped */
1759 FSDBG(513, -1, np
, np
->n_xid
>> 32, np
->n_xid
);
1760 if (avoidfloods
++ < 20) {
1763 /* avoidfloods>1 is bizarre. at 20 pull the plug */
1764 /* just return the last attributes we got */
1767 nfs_node_lock_force(np
);
1769 wanted
= ISSET(np
->n_flag
, NGETATTRWANT
);
1770 CLR(np
->n_flag
, (NGETATTRINPROG
| NGETATTRWANT
));
1773 /* check if the node changed on us */
1774 vnode_t vp
= NFSTOV(np
);
1775 enum vtype vtype
= vnode_vtype(vp
);
1776 if ((vtype
== VDIR
) && NFS_CHANGED_NC(nfsvers
, np
, nvap
)) {
1777 FSDBG(513, -1, np
, 0, np
);
1778 np
->n_flag
&= ~NNEGNCENTRIES
;
1781 NFS_CHANGED_UPDATE_NC(nfsvers
, np
, nvap
);
1782 NFS_VNOP_DBG("Purge directory 0x%llx\n",
1783 (uint64_t)VM_KERNEL_ADDRPERM(vp
));
1785 if (NFS_CHANGED(nfsvers
, np
, nvap
)) {
1786 FSDBG(513, -1, np
, -1, np
);
1787 if (vtype
== VDIR
) {
1788 NFS_VNOP_DBG("Invalidate directory 0x%llx\n",
1789 (uint64_t)VM_KERNEL_ADDRPERM(vp
));
1792 nfs_node_unlock(np
);
1796 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
1797 FSDBG(513, -1, np
, -2, error
);
1799 nfs_node_lock_force(np
);
1800 NFS_CHANGED_UPDATE(nfsvers
, np
, nvap
);
1801 nfs_node_unlock(np
);
1804 nfs_node_unlock(np
);
1810 nfs_node_unlock(np
);
1816 if (nvap
== &nvattr
) {
1817 NVATTR_CLEANUP(nvap
);
1818 } else if (!(flags
& NGA_ACL
)) {
1819 /* make sure we don't return an ACL if it wasn't asked for */
1820 NFS_BITMAP_CLR(nvap
->nva_bitmap
, NFS_FATTR_ACL
);
1821 if (nvap
->nva_acl
) {
1822 kauth_acl_free(nvap
->nva_acl
);
1823 nvap
->nva_acl
= NULL
;
1826 FSDBG_BOT(513, np
->n_size
, error
, np
->n_vattr
.nva_size
, np
->n_flag
);
1831 nfs_parse_user_access(
1835 int user_access
= R_OK
;
1836 if ((vfs_flags(mp
) & MNT_RDONLY
) == 0) {
1837 user_access
|= W_OK
;
1840 user_access
|= X_OK
;
1846 * NFS getattr call from vfs.
1850 * The attributes we support over the wire.
1851 * We also get fsid but the vfs layer gets it out of the mount
1852 * structure after this calling us so there's no need to return it,
1853 * and Finder expects to call getattrlist just looking for the FSID
1854 * with out hanging on a non responsive server.
1856 #define NFS3_SUPPORTED_VATTRS \
1857 (VNODE_ATTR_va_rdev | \
1858 VNODE_ATTR_va_nlink | \
1859 VNODE_ATTR_va_data_size | \
1860 VNODE_ATTR_va_data_alloc | \
1861 VNODE_ATTR_va_uid | \
1862 VNODE_ATTR_va_gid | \
1863 VNODE_ATTR_va_mode | \
1864 VNODE_ATTR_va_modify_time | \
1865 VNODE_ATTR_va_change_time | \
1866 VNODE_ATTR_va_access_time | \
1867 VNODE_ATTR_va_fileid | \
1873 struct vnop_getattr_args
/* {
1874 * struct vnodeop_desc *a_desc;
1876 * struct vnode_attr *a_vap;
1877 * vfs_context_t a_context;
1881 struct nfs_vattr nva
;
1882 struct vnode_attr
*vap
= ap
->a_vap
;
1883 struct nfsmount
*nmp
;
1886 nmp
= VTONMP(ap
->a_vp
);
1889 * Lets don't go over the wire if we don't support any of the attributes.
1890 * Just fall through at the VFS layer and let it cons up what it needs.
1892 /* Return the io size no matter what, since we don't go over the wire for this */
1893 VATTR_RETURN(vap
, va_iosize
, nfs_iosize
);
1895 if ((vap
->va_active
& NFS3_SUPPORTED_VATTRS
) == 0) {
1899 if (VATTR_IS_ACTIVE(ap
->a_vap
, va_name
)) {
1900 NFS_VNOP_DBG("Getting attrs for 0x%llx, vname is %s\n",
1901 (uint64_t)VM_KERNEL_ADDRPERM(ap
->a_vp
),
1902 ap
->a_vp
->v_name
? ap
->a_vp
->v_name
: "empty");
1904 error
= nfs_getattr(VTONFS(ap
->a_vp
), &nva
, ap
->a_context
, NGA_CACHED
);
1909 /* copy nva to *a_vap */
1910 VATTR_RETURN(vap
, va_type
, nva
.nva_type
);
1911 VATTR_RETURN(vap
, va_mode
, nva
.nva_mode
);
1912 rdev
= makedev(nva
.nva_rawdev
.specdata1
, nva
.nva_rawdev
.specdata2
);
1913 VATTR_RETURN(vap
, va_rdev
, rdev
);
1914 VATTR_RETURN(vap
, va_uid
, nva
.nva_uid
);
1915 VATTR_RETURN(vap
, va_gid
, nva
.nva_gid
);
1916 VATTR_RETURN(vap
, va_nlink
, nva
.nva_nlink
);
1917 VATTR_RETURN(vap
, va_fileid
, nva
.nva_fileid
);
1918 VATTR_RETURN(vap
, va_data_size
, nva
.nva_size
);
1919 VATTR_RETURN(vap
, va_data_alloc
, nva
.nva_bytes
);
1920 vap
->va_access_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_ACCESS
];
1921 vap
->va_access_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_ACCESS
];
1922 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1923 vap
->va_modify_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_MODIFY
];
1924 vap
->va_modify_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_MODIFY
];
1925 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1926 vap
->va_change_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_CHANGE
];
1927 vap
->va_change_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_CHANGE
];
1928 VATTR_SET_SUPPORTED(vap
, va_change_time
);
1931 // VATTR_RETURN(vap, va_encoding, 0xffff /* kTextEncodingUnknown */);
1940 struct vnop_setattr_args
/* {
1941 * struct vnodeop_desc *a_desc;
1943 * struct vnode_attr *a_vap;
1944 * vfs_context_t a_context;
1947 vfs_context_t ctx
= ap
->a_context
;
1948 vnode_t vp
= ap
->a_vp
;
1949 nfsnode_t np
= VTONFS(vp
);
1950 struct nfsmount
*nmp
;
1951 struct vnode_attr
*vap
= ap
->a_vap
;
1953 int biosize
, nfsvers
, namedattrs
;
1954 u_quad_t origsize
, vapsize
;
1955 struct nfs_dulookup dul
;
1956 nfsnode_t dnp
= NULL
;
1957 int dul_in_progress
= 0;
1959 const char *vname
= NULL
;
1961 struct nfs_open_owner
*noop
= NULL
;
1962 struct nfs_open_file
*nofp
= NULL
;
1965 if (nfs_mount_gone(nmp
)) {
1968 nfsvers
= nmp
->nm_vers
;
1969 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
1970 biosize
= nmp
->nm_biosize
;
1972 /* Disallow write attempts if the filesystem is mounted read-only. */
1973 if (vnode_vfsisrdonly(vp
)) {
1977 origsize
= np
->n_size
;
1978 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
1979 switch (vnode_vtype(vp
)) {
1986 if (!VATTR_IS_ACTIVE(vap
, va_modify_time
) &&
1987 !VATTR_IS_ACTIVE(vap
, va_access_time
) &&
1988 !VATTR_IS_ACTIVE(vap
, va_mode
) &&
1989 !VATTR_IS_ACTIVE(vap
, va_uid
) &&
1990 !VATTR_IS_ACTIVE(vap
, va_gid
)) {
1993 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
1997 * Disallow write attempts if the filesystem is
1998 * mounted read-only.
2000 if (vnode_vfsisrdonly(vp
)) {
2003 FSDBG_TOP(512, np
->n_size
, vap
->va_data_size
,
2004 np
->n_vattr
.nva_size
, np
->n_flag
);
2005 /* clear NNEEDINVALIDATE, if set */
2006 if ((error
= nfs_node_lock(np
))) {
2009 if (np
->n_flag
& NNEEDINVALIDATE
) {
2010 np
->n_flag
&= ~NNEEDINVALIDATE
;
2012 nfs_node_unlock(np
);
2013 /* flush everything */
2014 error
= nfs_vinvalbuf(vp
, (vap
->va_data_size
? V_SAVE
: 0), ctx
, 1);
2016 NP(np
, "nfs_setattr: nfs_vinvalbuf %d", error
);
2017 FSDBG_BOT(512, np
->n_size
, vap
->va_data_size
, np
->n_vattr
.nva_size
, -1);
2021 if (nfsvers
>= NFS_VER4
) {
2022 /* setting file size requires having the file open for write access */
2023 if (np
->n_flag
& NREVOKE
) {
2026 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
2031 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
2035 if (np
->n_flag
& NREVOKE
) {
2036 nfs_mount_state_in_use_end(nmp
, 0);
2039 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 1);
2040 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
2043 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
2044 nfs_mount_state_in_use_end(nmp
, 0);
2045 error
= nfs4_reopen(nofp
, vfs_context_thread(ctx
));
2052 error
= nfs_open_file_set_busy(nofp
, vfs_context_thread(ctx
));
2055 nfs_open_owner_rele(noop
);
2058 if (!(nofp
->nof_access
& NFS_OPEN_SHARE_ACCESS_WRITE
)) {
2059 /* we don't have the file open for write access, so open it */
2060 error
= nfs4_open(np
, nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
2062 nofp
->nof_flags
|= NFS_OPEN_FILE_SETATTR
;
2064 if (nfs_mount_state_error_should_restart(error
)) {
2065 nfs_open_file_clear_busy(nofp
);
2067 if (nfs_mount_state_in_use_end(nmp
, error
)) {
2074 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
2075 if (np
->n_size
> vap
->va_data_size
) { /* shrinking? */
2077 int neweofoff
, mustwrite
;
2080 obn
= (np
->n_size
- 1) / biosize
;
2081 bn
= vap
->va_data_size
/ biosize
;
2082 for (; obn
>= bn
; obn
--) {
2083 if (!nfs_buf_is_incore(np
, obn
)) {
2086 error
= nfs_buf_get(np
, obn
, biosize
, NULL
, NBLK_READ
, &bp
);
2091 FSDBG(512, bp
, bp
->nb_flags
, 0, obn
);
2092 SET(bp
->nb_flags
, NB_INVAL
);
2093 nfs_buf_release(bp
, 1);
2097 neweofoff
= vap
->va_data_size
- NBOFF(bp
);
2098 /* check for any dirty data before the new EOF */
2099 if ((bp
->nb_dirtyend
> 0) && (bp
->nb_dirtyoff
< neweofoff
)) {
2100 /* clip dirty range to EOF */
2101 if (bp
->nb_dirtyend
> neweofoff
) {
2102 bp
->nb_dirtyend
= neweofoff
;
2103 if (bp
->nb_dirtyoff
>= bp
->nb_dirtyend
) {
2104 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
2107 if ((bp
->nb_dirtyend
> 0) && (bp
->nb_dirtyoff
< neweofoff
)) {
2111 bp
->nb_dirty
&= (1 << round_page_32(neweofoff
) / PAGE_SIZE
) - 1;
2116 FSDBG(512, bp
, bp
->nb_flags
, 0, obn
);
2117 SET(bp
->nb_flags
, NB_INVAL
);
2118 nfs_buf_release(bp
, 1);
2121 /* gotta write out dirty data before invalidating */
2122 /* (NB_STABLE indicates that data writes should be FILESYNC) */
2123 /* (NB_NOCACHE indicates buffer should be discarded) */
2124 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
| NB_ASYNC
| NB_READ
));
2125 SET(bp
->nb_flags
, NB_STABLE
| NB_NOCACHE
);
2126 if (!IS_VALID_CRED(bp
->nb_wcred
)) {
2127 kauth_cred_t cred
= vfs_context_ucred(ctx
);
2128 kauth_cred_ref(cred
);
2129 bp
->nb_wcred
= cred
;
2131 error
= nfs_buf_write(bp
);
2132 // Note: bp has been released
2134 FSDBG(512, bp
, 0xd00dee, 0xbad, error
);
2135 nfs_node_lock_force(np
);
2136 np
->n_error
= error
;
2137 np
->n_flag
|= NWRITEERR
;
2139 * There was a write error and we need to
2140 * invalidate attrs and flush buffers in
2141 * order to sync up with the server.
2142 * (if this write was extending the file,
2143 * we may no longer know the correct size)
2145 NATTRINVALIDATE(np
);
2146 nfs_node_unlock(np
);
2147 nfs_data_unlock(np
);
2148 nfs_vinvalbuf(vp
, V_SAVE
| V_IGNORE_WRITEERR
, ctx
, 1);
2149 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
2154 if (vap
->va_data_size
!= np
->n_size
) {
2155 ubc_setsize(vp
, (off_t
)vap
->va_data_size
); /* XXX error? */
2157 origsize
= np
->n_size
;
2158 np
->n_size
= np
->n_vattr
.nva_size
= vap
->va_data_size
;
2159 nfs_node_lock_force(np
);
2160 CLR(np
->n_flag
, NUPDATESIZE
);
2161 nfs_node_unlock(np
);
2162 FSDBG(512, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0001);
2164 } else if (VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
2165 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
2166 (vap
->va_vaflags
& VA_UTIMES_NULL
)) {
2167 if ((error
= nfs_node_lock(np
))) {
2170 if ((np
->n_flag
& NMODIFIED
) && (vnode_vtype(vp
) == VREG
)) {
2171 nfs_node_unlock(np
);
2172 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
2173 if (error
== EINTR
) {
2177 nfs_node_unlock(np
);
2180 if ((VATTR_IS_ACTIVE(vap
, va_mode
) || VATTR_IS_ACTIVE(vap
, va_uid
) || VATTR_IS_ACTIVE(vap
, va_gid
) ||
2181 VATTR_IS_ACTIVE(vap
, va_acl
) || VATTR_IS_ACTIVE(vap
, va_uuuid
) || VATTR_IS_ACTIVE(vap
, va_guuid
)) &&
2182 !(error
= nfs_node_lock(np
))) {
2183 NACCESSINVALIDATE(np
);
2184 nfs_node_unlock(np
);
2186 dvp
= vnode_getparent(vp
);
2187 vname
= vnode_getname(vp
);
2188 dnp
= (dvp
&& vname
) ? VTONFS(dvp
) : NULL
;
2190 if (nfs_node_set_busy(dnp
, vfs_context_thread(ctx
))) {
2192 vnode_putname(vname
);
2194 nfs_dulookup_init(&dul
, dnp
, vname
, strlen(vname
), ctx
);
2195 nfs_dulookup_start(&dul
, dnp
, ctx
);
2196 dul_in_progress
= 1;
2203 vnode_putname(vname
);
2210 error
= nmp
->nm_funcs
->nf_setattr_rpc(np
, vap
, ctx
);
2213 if (dul_in_progress
) {
2214 nfs_dulookup_finish(&dul
, dnp
, ctx
);
2215 nfs_node_clear_busy(dnp
);
2217 vnode_putname(vname
);
2220 FSDBG_BOT(512, np
->n_size
, vap
->va_data_size
, np
->n_vattr
.nva_size
, error
);
2221 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
2222 if (error
&& (origsize
!= np
->n_size
) &&
2223 ((nfsvers
< NFS_VER4
) || !nfs_mount_state_error_should_restart(error
))) {
2224 /* make every effort to resync file size w/ server... */
2225 /* (don't bother if we'll be restarting the operation) */
2226 int err
; /* preserve "error" for return */
2227 np
->n_size
= np
->n_vattr
.nva_size
= origsize
;
2228 nfs_node_lock_force(np
);
2229 CLR(np
->n_flag
, NUPDATESIZE
);
2230 nfs_node_unlock(np
);
2231 FSDBG(512, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0002);
2232 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX check error */
2233 vapsize
= vap
->va_data_size
;
2234 vap
->va_data_size
= origsize
;
2235 err
= nmp
->nm_funcs
->nf_setattr_rpc(np
, vap
, ctx
);
2237 NP(np
, "nfs_vnop_setattr: nfs%d_setattr_rpc %d %d", nfsvers
, error
, err
);
2239 vap
->va_data_size
= vapsize
;
2241 nfs_node_lock_force(np
);
2243 * The size was just set. If the size is already marked for update, don't
2244 * trust the newsize (it may have been set while the setattr was in progress).
2245 * Clear the update flag and make sure we fetch new attributes so we are sure
2246 * we have the latest size.
2248 if (ISSET(np
->n_flag
, NUPDATESIZE
)) {
2249 CLR(np
->n_flag
, NUPDATESIZE
);
2250 NATTRINVALIDATE(np
);
2251 nfs_node_unlock(np
);
2252 nfs_getattr(np
, NULL
, ctx
, NGA_UNCACHED
);
2254 nfs_node_unlock(np
);
2256 nfs_data_unlock(np
);
2258 if (nfsvers
>= NFS_VER4
) {
2260 /* don't close our setattr open if we'll be restarting... */
2261 if (!nfs_mount_state_error_should_restart(error
) &&
2262 (nofp
->nof_flags
& NFS_OPEN_FILE_SETATTR
)) {
2263 int err
= nfs_close(np
, nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
2265 NP(np
, "nfs_vnop_setattr: close error: %d", err
);
2267 nofp
->nof_flags
&= ~NFS_OPEN_FILE_SETATTR
;
2269 nfs_open_file_clear_busy(nofp
);
2272 if (nfs_mount_state_in_use_end(nmp
, error
)) {
2275 nfs_open_owner_rele(noop
);
2283 * Do an NFS setattr RPC.
2288 struct vnode_attr
*vap
,
2291 struct nfsmount
*nmp
= NFSTONMP(np
);
2292 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0, nfsvers
;
2293 u_int64_t xid
, nextxid
;
2294 struct nfsm_chain nmreq
, nmrep
;
2296 if (nfs_mount_gone(nmp
)) {
2299 nfsvers
= nmp
->nm_vers
;
2301 VATTR_SET_SUPPORTED(vap
, va_mode
);
2302 VATTR_SET_SUPPORTED(vap
, va_uid
);
2303 VATTR_SET_SUPPORTED(vap
, va_gid
);
2304 VATTR_SET_SUPPORTED(vap
, va_data_size
);
2305 VATTR_SET_SUPPORTED(vap
, va_access_time
);
2306 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
2309 if (VATTR_IS_ACTIVE(vap
, va_flags
)
2311 if (vap
->va_flags
) { /* we don't support setting flags */
2312 if (vap
->va_active
& ~VNODE_ATTR_va_flags
) {
2313 return EINVAL
; /* return EINVAL if other attributes also set */
2315 return ENOTSUP
; /* return ENOTSUP for chflags(2) */
2318 /* no flags set, so we'll just ignore it */
2319 if (!(vap
->va_active
& ~VNODE_ATTR_va_flags
)) {
2320 return 0; /* no (other) attributes to set, so nothing to do */
2324 nfsm_chain_null(&nmreq
);
2325 nfsm_chain_null(&nmrep
);
2327 nfsm_chain_build_alloc_init(error
, &nmreq
,
2328 NFSX_FH(nfsvers
) + NFSX_SATTR(nfsvers
));
2329 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
2330 if (nfsvers
== NFS_VER3
) {
2331 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
2332 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
2333 nfsm_chain_add_32(error
, &nmreq
, vap
->va_mode
);
2335 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2337 if (VATTR_IS_ACTIVE(vap
, va_uid
)) {
2338 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
2339 nfsm_chain_add_32(error
, &nmreq
, vap
->va_uid
);
2341 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2343 if (VATTR_IS_ACTIVE(vap
, va_gid
)) {
2344 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
2345 nfsm_chain_add_32(error
, &nmreq
, vap
->va_gid
);
2347 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2349 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
2350 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
2351 nfsm_chain_add_64(error
, &nmreq
, vap
->va_data_size
);
2353 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2355 if (vap
->va_vaflags
& VA_UTIMES_NULL
) {
2356 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_SERVER
);
2357 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_SERVER
);
2359 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
2360 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_CLIENT
);
2361 nfsm_chain_add_32(error
, &nmreq
, vap
->va_access_time
.tv_sec
);
2362 nfsm_chain_add_32(error
, &nmreq
, vap
->va_access_time
.tv_nsec
);
2364 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_DONT_CHANGE
);
2366 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
2367 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_CLIENT
);
2368 nfsm_chain_add_32(error
, &nmreq
, vap
->va_modify_time
.tv_sec
);
2369 nfsm_chain_add_32(error
, &nmreq
, vap
->va_modify_time
.tv_nsec
);
2371 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_DONT_CHANGE
);
2374 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2376 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_mode
) ?
2377 vtonfsv2_mode(vnode_vtype(NFSTOV(np
)), vap
->va_mode
) : -1);
2378 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_uid
) ?
2379 vap
->va_uid
: (uint32_t)-1);
2380 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_gid
) ?
2381 vap
->va_gid
: (uint32_t)-1);
2382 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_data_size
) ?
2383 vap
->va_data_size
: (uint32_t)-1);
2384 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
2385 nfsm_chain_add_32(error
, &nmreq
, vap
->va_access_time
.tv_sec
);
2386 nfsm_chain_add_32(error
, &nmreq
, (vap
->va_access_time
.tv_nsec
!= -1) ?
2387 ((uint32_t)vap
->va_access_time
.tv_nsec
/ 1000) : 0xffffffff);
2389 nfsm_chain_add_32(error
, &nmreq
, -1);
2390 nfsm_chain_add_32(error
, &nmreq
, -1);
2392 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
2393 nfsm_chain_add_32(error
, &nmreq
, vap
->va_modify_time
.tv_sec
);
2394 nfsm_chain_add_32(error
, &nmreq
, (vap
->va_modify_time
.tv_nsec
!= -1) ?
2395 ((uint32_t)vap
->va_modify_time
.tv_nsec
/ 1000) : 0xffffffff);
2397 nfsm_chain_add_32(error
, &nmreq
, -1);
2398 nfsm_chain_add_32(error
, &nmreq
, -1);
2401 nfsm_chain_build_done(error
, &nmreq
);
2403 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_SETATTR
, ctx
, NULL
, &nmrep
, &xid
, &status
);
2404 if ((lockerror
= nfs_node_lock(np
))) {
2407 if (nfsvers
== NFS_VER3
) {
2408 struct timespec premtime
= { .tv_sec
= 0, .tv_nsec
= 0 };
2409 nfsm_chain_get_wcc_data(error
, &nmrep
, np
, &premtime
, &wccpostattr
, &xid
);
2411 /* if file hadn't changed, update cached mtime */
2412 if (nfstimespeccmp(&np
->n_mtime
, &premtime
, ==)) {
2413 NFS_CHANGED_UPDATE(nfsvers
, np
, &np
->n_vattr
);
2415 /* if directory hadn't changed, update namecache mtime */
2416 if ((vnode_vtype(NFSTOV(np
)) == VDIR
) &&
2417 nfstimespeccmp(&np
->n_ncmtime
, &premtime
, ==)) {
2418 NFS_CHANGED_UPDATE_NC(nfsvers
, np
, &np
->n_vattr
);
2421 NATTRINVALIDATE(np
);
2428 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
2431 * We just changed the attributes and we want to make sure that we
2432 * see the latest attributes. Get the next XID. If it's not the
2433 * next XID after the SETATTR XID, then it's possible that another
2434 * RPC was in flight at the same time and it might put stale attributes
2435 * in the cache. In that case, we invalidate the attributes and set
2436 * the attribute cache XID to guarantee that newer attributes will
2440 nfs_get_xid(&nextxid
);
2441 if (nextxid
!= (xid
+ 1)) {
2442 np
->n_xid
= nextxid
;
2443 NATTRINVALIDATE(np
);
2447 nfs_node_unlock(np
);
2449 nfsm_chain_cleanup(&nmreq
);
2450 nfsm_chain_cleanup(&nmrep
);
2455 * NFS lookup call, one step at a time...
2456 * First look in cache
2457 * If not found, unlock the directory nfsnode and do the RPC
2461 struct vnop_lookup_args
/* {
2462 * struct vnodeop_desc *a_desc;
2465 * struct componentname *a_cnp;
2466 * vfs_context_t a_context;
2469 vfs_context_t ctx
= ap
->a_context
;
2470 struct componentname
*cnp
= ap
->a_cnp
;
2471 vnode_t dvp
= ap
->a_dvp
;
2472 vnode_t
*vpp
= ap
->a_vpp
;
2473 int flags
= cnp
->cn_flags
;
2476 struct nfsmount
*nmp
;
2478 int nfsvers
, error
, busyerror
= ENOENT
, isdot
, isdotdot
, negnamecache
;
2480 struct nfs_vattr nvattr
;
2482 struct vnop_access_args naa
;
2484 struct nfsreq rq
, *req
= &rq
;
2489 NVATTR_INIT(&nvattr
);
2491 mp
= vnode_mount(dvp
);
2493 if (nfs_mount_gone(nmp
)) {
2497 nfsvers
= nmp
->nm_vers
;
2498 negnamecache
= !NMFLAG(nmp
, NONEGNAMECACHE
);
2500 if ((error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
)))) {
2503 /* nfs_getattr() will check changed and purge caches */
2504 if ((error
= nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
))) {
2508 error
= cache_lookup(dvp
, vpp
, cnp
);
2511 /* negative cache entry */
2515 if ((nfsvers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
)) {
2516 /* if rdirplus, try dir buf cache lookup */
2517 error
= nfs_dir_buf_cache_lookup(dnp
, &np
, cnp
, ctx
, 0);
2519 /* dir buf cache hit */
2524 if (error
!= -1) { /* cache miss */
2529 /* cache hit, not really an error */
2530 OSAddAtomic64(1, &nfsstats
.lookupcache_hits
);
2532 nfs_node_clear_busy(dnp
);
2535 /* check for directory access */
2536 naa
.a_desc
= &vnop_access_desc
;
2538 naa
.a_action
= KAUTH_VNODE_SEARCH
;
2539 naa
.a_context
= ctx
;
2541 /* compute actual success/failure based on accessibility */
2542 error
= nfs_vnop_access(&naa
);
2545 /* unexpected error from cache_lookup */
2549 /* skip lookup, if we know who we are: "." or ".." */
2550 isdot
= isdotdot
= 0;
2551 if (cnp
->cn_nameptr
[0] == '.') {
2552 if (cnp
->cn_namelen
== 1) {
2555 if ((cnp
->cn_namelen
== 2) && (cnp
->cn_nameptr
[1] == '.')) {
2559 if (isdotdot
|| isdot
) {
2564 if ((nfsvers
>= NFS_VER4
) && (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER
)) {
2565 /* we should never be looking things up in a trigger directory, return nothing */
2571 /* do we know this name is too long? */
2573 if (nfs_mount_gone(nmp
)) {
2577 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXNAME
) &&
2578 (cnp
->cn_namelen
> (int)nmp
->nm_fsattr
.nfsa_maxname
)) {
2579 error
= ENAMETOOLONG
;
2586 OSAddAtomic64(1, &nfsstats
.lookupcache_misses
);
2588 error
= nmp
->nm_funcs
->nf_lookup_rpc_async(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &req
);
2590 error
= nmp
->nm_funcs
->nf_lookup_rpc_async_finish(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, req
, &xid
, &fh
, &nvattr
);
2593 /* is the file handle the same as this directory's file handle? */
2594 isdot
= NFS_CMPFH(dnp
, fh
.fh_data
, fh
.fh_len
);
2597 if (flags
& ISLASTCN
) {
2598 switch (cnp
->cn_nameiop
) {
2600 cnp
->cn_flags
&= ~MAKEENTRY
;
2603 cnp
->cn_flags
&= ~MAKEENTRY
;
2613 newvp
= vnode_getparent(dvp
);
2619 error
= vnode_get(dvp
);
2624 nfs_node_lock_force(dnp
);
2625 if (fh
.fh_len
&& (dnp
->n_xid
<= xid
)) {
2626 nfs_loadattrcache(dnp
, &nvattr
, &xid
, 0);
2628 nfs_node_unlock(dnp
);
2630 ngflags
= (cnp
->cn_flags
& MAKEENTRY
) ? NG_MAKEENTRY
: 0;
2631 error
= nfs_nget(mp
, dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, ngflags
, &np
);
2636 nfs_node_unlock(np
);
2642 if (((cnp
->cn_nameiop
== CREATE
) || (cnp
->cn_nameiop
== RENAME
)) &&
2643 (flags
& ISLASTCN
) && (error
== ENOENT
)) {
2644 if (vnode_mount(dvp
) && vnode_vfsisrdonly(dvp
)) {
2647 error
= EJUSTRETURN
;
2651 if ((error
== ENOENT
) && (cnp
->cn_flags
& MAKEENTRY
) &&
2652 (cnp
->cn_nameiop
!= CREATE
) && negnamecache
) {
2653 /* add a negative entry in the name cache */
2654 nfs_node_lock_force(dnp
);
2655 cache_enter(dvp
, NULL
, cnp
);
2656 dnp
->n_flag
|= NNEGNCENTRIES
;
2657 nfs_node_unlock(dnp
);
2660 NVATTR_CLEANUP(&nvattr
);
2662 nfs_node_clear_busy(dnp
);
2664 if (error
&& *vpp
) {
2671 int nfs_readlink_nocache
= DEFAULT_READLINK_NOCACHE
;
2678 struct vnop_readlink_args
/* {
2679 * struct vnodeop_desc *a_desc;
2681 * struct uio *a_uio;
2682 * vfs_context_t a_context;
2685 vfs_context_t ctx
= ap
->a_context
;
2686 nfsnode_t np
= VTONFS(ap
->a_vp
);
2687 struct nfsmount
*nmp
;
2688 int error
= 0, nfsvers
;
2690 uio_t uio
= ap
->a_uio
;
2691 struct nfsbuf
*bp
= NULL
;
2695 if (vnode_vtype(ap
->a_vp
) != VLNK
) {
2699 if (uio_resid(uio
) == 0) {
2702 if (uio_offset(uio
) < 0) {
2706 nmp
= VTONMP(ap
->a_vp
);
2707 if (nfs_mount_gone(nmp
)) {
2710 nfsvers
= nmp
->nm_vers
;
2713 /* nfs_getattr() will check changed and purge caches */
2714 if ((error
= nfs_getattr(np
, NULL
, ctx
, nfs_readlink_nocache
? NGA_UNCACHED
: NGA_CACHED
))) {
2715 FSDBG(531, np
, 0xd1e0001, 0, error
);
2719 if (nfs_readlink_nocache
) {
2720 timeo
= nfs_attrcachetimeout(np
);
2725 OSAddAtomic64(1, &nfsstats
.biocache_readlinks
);
2726 error
= nfs_buf_get(np
, 0, NFS_MAXPATHLEN
, vfs_context_thread(ctx
), NBLK_META
, &bp
);
2728 FSDBG(531, np
, 0xd1e0002, 0, error
);
2732 if (nfs_readlink_nocache
) {
2733 NFS_VNOP_DBG("timeo = %d ts.tv_sec = %ld need refresh = %d cached = %d\n", timeo
, ts
.tv_sec
,
2734 (np
->n_rltim
.tv_sec
+ timeo
) < ts
.tv_sec
|| nfs_readlink_nocache
> 1,
2735 ISSET(bp
->nb_flags
, NB_CACHE
) == NB_CACHE
);
2736 /* n_rltim is synchronized by the associated nfs buf */
2737 if (ISSET(bp
->nb_flags
, NB_CACHE
) && ((nfs_readlink_nocache
> 1) || ((np
->n_rltim
.tv_sec
+ timeo
) < ts
.tv_sec
))) {
2738 SET(bp
->nb_flags
, NB_INVAL
);
2739 nfs_buf_release(bp
, 0);
2743 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) {
2745 OSAddAtomic64(1, &nfsstats
.readlink_bios
);
2746 buflen
= bp
->nb_bufsize
;
2747 error
= nmp
->nm_funcs
->nf_readlink_rpc(np
, bp
->nb_data
, &buflen
, ctx
);
2749 if (error
== ESTALE
) {
2750 NFS_VNOP_DBG("Stale FH from readlink rpc\n");
2751 error
= nfs_refresh_fh(np
, ctx
);
2756 SET(bp
->nb_flags
, NB_ERROR
);
2757 bp
->nb_error
= error
;
2758 NFS_VNOP_DBG("readlink failed %d\n", error
);
2760 bp
->nb_validoff
= 0;
2761 bp
->nb_validend
= buflen
;
2763 NFS_VNOP_DBG("readlink of %.*s\n", bp
->nb_validend
, (char *)bp
->nb_data
);
2766 NFS_VNOP_DBG("got cached link of %.*s\n", bp
->nb_validend
, (char *)bp
->nb_data
);
2769 if (!error
&& (bp
->nb_validend
> 0)) {
2770 error
= uiomove(bp
->nb_data
, bp
->nb_validend
, uio
);
2772 FSDBG(531, np
, bp
->nb_validend
, 0, error
);
2773 nfs_buf_release(bp
, 1);
2778 * Do a readlink RPC.
2781 nfs3_readlink_rpc(nfsnode_t np
, char *buf
, uint32_t *buflenp
, vfs_context_t ctx
)
2783 struct nfsmount
*nmp
;
2784 int error
= 0, lockerror
= ENOENT
, nfsvers
, status
;
2787 struct nfsm_chain nmreq
, nmrep
;
2790 if (nfs_mount_gone(nmp
)) {
2793 nfsvers
= nmp
->nm_vers
;
2794 nfsm_chain_null(&nmreq
);
2795 nfsm_chain_null(&nmrep
);
2797 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nfsvers
));
2798 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
2799 nfsm_chain_build_done(error
, &nmreq
);
2801 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_READLINK
, ctx
, NULL
, &nmrep
, &xid
, &status
);
2802 if ((lockerror
= nfs_node_lock(np
))) {
2805 if (nfsvers
== NFS_VER3
) {
2806 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
2811 nfsm_chain_get_32(error
, &nmrep
, len
);
2813 if ((nfsvers
== NFS_VER2
) && (len
> *buflenp
)) {
2817 if (len
>= *buflenp
) {
2818 if (np
->n_size
&& (np
->n_size
< *buflenp
)) {
2824 nfsm_chain_get_opaque(error
, &nmrep
, len
, buf
);
2830 nfs_node_unlock(np
);
2832 nfsm_chain_cleanup(&nmreq
);
2833 nfsm_chain_cleanup(&nmrep
);
2842 nfs_read_rpc(nfsnode_t np
, uio_t uio
, vfs_context_t ctx
)
2844 struct nfsmount
*nmp
;
2845 int error
= 0, nfsvers
, eof
= 0;
2846 size_t nmrsize
, len
, retlen
;
2849 struct nfsreq rq
, *req
= &rq
;
2851 uint32_t stategenid
= 0, restart
= 0;
2853 FSDBG_TOP(536, np
, uio_offset(uio
), uio_resid(uio
), 0);
2855 if (nfs_mount_gone(nmp
)) {
2858 nfsvers
= nmp
->nm_vers
;
2859 nmrsize
= nmp
->nm_rsize
;
2861 txoffset
= uio_offset(uio
);
2862 tsiz
= uio_resid(uio
);
2863 if ((nfsvers
== NFS_VER2
) && ((uint64_t)(txoffset
+ tsiz
) > 0xffffffffULL
)) {
2864 FSDBG_BOT(536, np
, uio_offset(uio
), uio_resid(uio
), EFBIG
);
2869 len
= retlen
= (tsiz
> (user_ssize_t
)nmrsize
) ? nmrsize
: (size_t)tsiz
;
2870 FSDBG(536, np
, txoffset
, len
, 0);
2871 if (np
->n_flag
& NREVOKE
) {
2876 if (nmp
->nm_vers
>= NFS_VER4
) {
2877 stategenid
= nmp
->nm_stategenid
;
2880 error
= nmp
->nm_funcs
->nf_read_rpc_async(np
, txoffset
, len
,
2881 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, &req
);
2883 error
= nmp
->nm_funcs
->nf_read_rpc_async_finish(np
, req
, uio
, &retlen
, &eof
);
2886 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
) &&
2887 (++restart
<= nfs_mount_state_max_restarts(nmp
))) { /* guard against no progress */
2888 lck_mtx_lock(&nmp
->nm_lock
);
2889 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
2890 NP(np
, "nfs_read_rpc: error %d, initiating recovery", error
);
2891 nfs_need_recover(nmp
, error
);
2893 lck_mtx_unlock(&nmp
->nm_lock
);
2894 if (np
->n_flag
& NREVOKE
) {
2897 if (error
== NFSERR_GRACE
) {
2898 tsleep(&nmp
->nm_state
, (PZERO
- 1), "nfsgrace", 2 * hz
);
2900 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
))) {
2911 if (nfsvers
!= NFS_VER2
) {
2912 if (eof
|| (retlen
== 0)) {
2915 } else if (retlen
< len
) {
2920 FSDBG_BOT(536, np
, eof
, uio_resid(uio
), error
);
2925 nfs3_read_rpc_async(
2931 struct nfsreq_cbinfo
*cb
,
2932 struct nfsreq
**reqp
)
2934 struct nfsmount
*nmp
;
2935 int error
= 0, nfsvers
;
2936 struct nfsm_chain nmreq
;
2939 if (nfs_mount_gone(nmp
)) {
2942 nfsvers
= nmp
->nm_vers
;
2944 nfsm_chain_null(&nmreq
);
2945 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nfsvers
) + 3 * NFSX_UNSIGNED
);
2946 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
2947 if (nfsvers
== NFS_VER3
) {
2948 nfsm_chain_add_64(error
, &nmreq
, offset
);
2949 nfsm_chain_add_32(error
, &nmreq
, len
);
2951 nfsm_chain_add_32(error
, &nmreq
, offset
);
2952 nfsm_chain_add_32(error
, &nmreq
, len
);
2953 nfsm_chain_add_32(error
, &nmreq
, 0);
2955 nfsm_chain_build_done(error
, &nmreq
);
2957 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC_READ
, thd
, cred
, NULL
, 0, cb
, reqp
);
2959 nfsm_chain_cleanup(&nmreq
);
2964 nfs3_read_rpc_async_finish(
2971 int error
= 0, lockerror
, nfsvers
, status
, eof
= 0;
2974 struct nfsmount
*nmp
;
2975 struct nfsm_chain nmrep
;
2978 if (nfs_mount_gone(nmp
)) {
2979 nfs_request_async_cancel(req
);
2982 nfsvers
= nmp
->nm_vers
;
2984 nfsm_chain_null(&nmrep
);
2986 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
2987 if (error
== EINPROGRESS
) { /* async request restarted */
2991 if ((lockerror
= nfs_node_lock(np
))) {
2994 if (nfsvers
== NFS_VER3
) {
2995 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
3000 if (nfsvers
== NFS_VER3
) {
3001 nfsm_chain_adv(error
, &nmrep
, NFSX_UNSIGNED
);
3002 nfsm_chain_get_32(error
, &nmrep
, eof
);
3004 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
3007 nfs_node_unlock(np
);
3009 nfsm_chain_get_32(error
, &nmrep
, retlen
);
3010 if ((nfsvers
== NFS_VER2
) && (retlen
> *lenp
)) {
3014 error
= nfsm_chain_get_uio(&nmrep
, MIN(retlen
, *lenp
), uio
);
3016 if (nfsvers
== NFS_VER3
) {
3017 if (!eof
&& !retlen
) {
3020 } else if (retlen
< *lenp
) {
3025 *lenp
= MIN(retlen
, *lenp
);
3027 nfsm_chain_cleanup(&nmrep
);
3036 struct vnop_write_args
/* {
3037 * struct vnodeop_desc *a_desc;
3039 * struct uio *a_uio;
3041 * vfs_context_t a_context;
3044 vfs_context_t ctx
= ap
->a_context
;
3045 uio_t uio
= ap
->a_uio
;
3046 vnode_t vp
= ap
->a_vp
;
3047 nfsnode_t np
= VTONFS(vp
);
3048 int ioflag
= ap
->a_ioflag
;
3050 struct nfsmount
*nmp
= VTONMP(vp
);
3053 int n
, on
, error
= 0;
3054 off_t boff
, start
, end
;
3056 char auio_buf
[UIO_SIZEOF(1)];
3060 FSDBG_TOP(515, np
, uio_offset(uio
), uio_resid(uio
), ioflag
);
3062 if (vnode_vtype(vp
) != VREG
) {
3063 FSDBG_BOT(515, np
, uio_offset(uio
), uio_resid(uio
), EIO
);
3067 thd
= vfs_context_thread(ctx
);
3068 cred
= vfs_context_ucred(ctx
);
3070 nfs_data_lock(np
, NFS_DATA_LOCK_SHARED
);
3072 if ((error
= nfs_node_lock(np
))) {
3073 nfs_data_unlock(np
);
3074 FSDBG_BOT(515, np
, uio_offset(uio
), uio_resid(uio
), error
);
3079 if (np
->n_flag
& NWRITEERR
) {
3080 error
= np
->n_error
;
3081 np
->n_flag
&= ~NWRITEERR
;
3083 if (np
->n_flag
& NNEEDINVALIDATE
) {
3084 np
->n_flag
&= ~NNEEDINVALIDATE
;
3085 nfs_node_unlock(np
);
3086 nfs_data_unlock(np
);
3087 nfs_vinvalbuf(vp
, V_SAVE
| V_IGNORE_WRITEERR
, ctx
, 1);
3088 nfs_data_lock(np
, NFS_DATA_LOCK_SHARED
);
3090 nfs_node_unlock(np
);
3096 biosize
= nmp
->nm_biosize
;
3098 if (ioflag
& (IO_APPEND
| IO_SYNC
)) {
3099 nfs_node_lock_force(np
);
3100 if (np
->n_flag
& NMODIFIED
) {
3101 NATTRINVALIDATE(np
);
3102 nfs_node_unlock(np
);
3103 nfs_data_unlock(np
);
3104 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
3105 nfs_data_lock(np
, NFS_DATA_LOCK_SHARED
);
3107 FSDBG(515, np
, uio_offset(uio
), 0x10bad01, error
);
3111 nfs_node_unlock(np
);
3113 if (ioflag
& IO_APPEND
) {
3114 nfs_data_unlock(np
);
3115 /* nfs_getattr() will check changed and purge caches */
3116 error
= nfs_getattr(np
, NULL
, ctx
, NGA_UNCACHED
);
3117 /* we'll be extending the file, so take the data lock exclusive */
3118 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
3120 FSDBG(515, np
, uio_offset(uio
), 0x10bad02, error
);
3123 uio_setoffset(uio
, np
->n_size
);
3126 if (uio_offset(uio
) < 0) {
3128 FSDBG_BOT(515, np
, uio_offset(uio
), 0xbad0ff, error
);
3131 if (uio_resid(uio
) == 0) {
3135 if (((uio_offset(uio
) + uio_resid(uio
)) > (off_t
)np
->n_size
) && !(ioflag
& IO_APPEND
)) {
3137 * It looks like we'll be extending the file, so take the data lock exclusive.
3139 nfs_data_unlock(np
);
3140 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
3143 * Also, if the write begins after the previous EOF buffer, make sure to zero
3144 * and validate the new bytes in that buffer.
3146 struct nfsbuf
*eofbp
= NULL
;
3147 daddr64_t eofbn
= np
->n_size
/ biosize
;
3148 int eofoff
= np
->n_size
% biosize
;
3149 lbn
= uio_offset(uio
) / biosize
;
3151 if (eofoff
&& (eofbn
< lbn
)) {
3152 if ((error
= nfs_buf_get(np
, eofbn
, biosize
, thd
, NBLK_WRITE
| NBLK_ONLYVALID
, &eofbp
))) {
3155 np
->n_size
+= (biosize
- eofoff
);
3156 nfs_node_lock_force(np
);
3157 CLR(np
->n_flag
, NUPDATESIZE
);
3158 np
->n_flag
|= NMODIFIED
;
3159 nfs_node_unlock(np
);
3160 FSDBG(516, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0001);
3161 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX errors */
3164 * For the old last page, don't zero bytes if there
3165 * are invalid bytes in that page (i.e. the page isn't
3167 * For pages after the old last page, zero them and
3168 * mark them as valid.
3172 if (ioflag
& IO_NOCACHE
) {
3173 SET(eofbp
->nb_flags
, NB_NOCACHE
);
3176 FSDBG(516, eofbp
, eofoff
, biosize
- eofoff
, 0xe0fff01e);
3178 i
= eofoff
/ PAGE_SIZE
;
3179 while (eofoff
< biosize
) {
3180 int poff
= eofoff
& PAGE_MASK
;
3181 if (!poff
|| NBPGVALID(eofbp
, i
)) {
3182 bzero(d
+ eofoff
, PAGE_SIZE
- poff
);
3183 NBPGVALID_SET(eofbp
, i
);
3185 eofoff
+= PAGE_SIZE
- poff
;
3188 nfs_buf_release(eofbp
, 1);
3194 OSAddAtomic64(1, &nfsstats
.biocache_writes
);
3195 lbn
= uio_offset(uio
) / biosize
;
3196 on
= uio_offset(uio
) % biosize
;
3198 if (uio_resid(uio
) < n
) {
3203 * Get a cache block for writing. The range to be written is
3204 * (off..off+n) within the block. We ensure that the block
3205 * either has no dirty region or that the given range is
3206 * contiguous with the existing dirty region.
3208 error
= nfs_buf_get(np
, lbn
, biosize
, thd
, NBLK_WRITE
, &bp
);
3212 /* map the block because we know we're going to write to it */
3215 if (ioflag
& IO_NOCACHE
) {
3216 SET(bp
->nb_flags
, NB_NOCACHE
);
3219 if (!IS_VALID_CRED(bp
->nb_wcred
)) {
3220 kauth_cred_ref(cred
);
3221 bp
->nb_wcred
= cred
;
3225 * If there's already a dirty range AND dirty pages in this block we
3226 * need to send a commit AND write the dirty pages before continuing.
3228 * If there's already a dirty range OR dirty pages in this block
3229 * and the new write range is not contiguous with the existing range,
3230 * then force the buffer to be written out now.
3231 * (We used to just extend the dirty range to cover the valid,
3232 * but unwritten, data in between also. But writing ranges
3233 * of data that weren't actually written by an application
3234 * risks overwriting some other client's data with stale data
3235 * that's just masquerading as new written data.)
3237 if (bp
->nb_dirtyend
> 0) {
3238 if (on
> bp
->nb_dirtyend
|| (on
+ n
) < bp
->nb_dirtyoff
|| bp
->nb_dirty
) {
3239 FSDBG(515, np
, uio_offset(uio
), bp
, 0xd15c001);
3240 /* write/commit buffer "synchronously" */
3241 /* (NB_STABLE indicates that data writes should be FILESYNC) */
3242 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
));
3243 SET(bp
->nb_flags
, (NB_ASYNC
| NB_STABLE
));
3244 error
= nfs_buf_write(bp
);
3250 } else if (bp
->nb_dirty
) {
3251 int firstpg
, lastpg
;
3253 /* calculate write range pagemask */
3254 firstpg
= on
/ PAGE_SIZE
;
3255 lastpg
= (on
+ n
- 1) / PAGE_SIZE
;
3256 pagemask
= ((1 << (lastpg
+ 1)) - 1) & ~((1 << firstpg
) - 1);
3257 /* check if there are dirty pages outside the write range */
3258 if (bp
->nb_dirty
& ~pagemask
) {
3259 FSDBG(515, np
, uio_offset(uio
), bp
, 0xd15c002);
3260 /* write/commit buffer "synchronously" */
3261 /* (NB_STABLE indicates that data writes should be FILESYNC) */
3262 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
));
3263 SET(bp
->nb_flags
, (NB_ASYNC
| NB_STABLE
));
3264 error
= nfs_buf_write(bp
);
3270 /* if the first or last pages are already dirty */
3271 /* make sure that the dirty range encompasses those pages */
3272 if (NBPGDIRTY(bp
, firstpg
) || NBPGDIRTY(bp
, lastpg
)) {
3273 FSDBG(515, np
, uio_offset(uio
), bp
, 0xd15c003);
3274 bp
->nb_dirtyoff
= min(on
, firstpg
* PAGE_SIZE
);
3275 if (NBPGDIRTY(bp
, lastpg
)) {
3276 bp
->nb_dirtyend
= (lastpg
+ 1) * PAGE_SIZE
;
3278 if (NBOFF(bp
) + bp
->nb_dirtyend
> (off_t
)np
->n_size
) {
3279 bp
->nb_dirtyend
= np
->n_size
- NBOFF(bp
);
3280 if (bp
->nb_dirtyoff
>= bp
->nb_dirtyend
) {
3281 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
3285 bp
->nb_dirtyend
= on
+ n
;
3291 * Are we extending the size of the file with this write?
3292 * If so, update file size now that we have the block.
3293 * If there was a partial buf at the old eof, validate
3294 * and zero the new bytes.
3296 if ((uio_offset(uio
) + n
) > (off_t
)np
->n_size
) {
3297 daddr64_t eofbn
= np
->n_size
/ biosize
;
3298 int neweofoff
= (uio_offset(uio
) + n
) % biosize
;
3300 FSDBG(515, 0xb1ffa000, uio_offset(uio
) + n
, eofoff
, neweofoff
);
3302 /* if we're extending within the same last block */
3303 /* and the block is flagged as being cached... */
3304 if ((lbn
== eofbn
) && ISSET(bp
->nb_flags
, NB_CACHE
)) {
3305 /* ...check that all pages in buffer are valid */
3306 int endpg
= ((neweofoff
? neweofoff
: biosize
) - 1) / PAGE_SIZE
;
3308 /* pagemask only has to extend to last page being written to */
3309 pagemask
= (1 << (endpg
+ 1)) - 1;
3310 FSDBG(515, 0xb1ffa001, bp
->nb_valid
, pagemask
, 0);
3311 if ((bp
->nb_valid
& pagemask
) != pagemask
) {
3312 /* zerofill any hole */
3313 if (on
> bp
->nb_validend
) {
3315 for (i
= bp
->nb_validend
/ PAGE_SIZE
; i
<= (on
- 1) / PAGE_SIZE
; i
++) {
3316 NBPGVALID_SET(bp
, i
);
3319 FSDBG(516, bp
, bp
->nb_validend
, on
- bp
->nb_validend
, 0xf01e);
3320 bzero((char *)bp
->nb_data
+ bp
->nb_validend
,
3321 on
- bp
->nb_validend
);
3323 /* zerofill any trailing data in the last page */
3326 FSDBG(516, bp
, neweofoff
, PAGE_SIZE
- (neweofoff
& PAGE_MASK
), 0xe0f);
3327 bzero((char *)bp
->nb_data
+ neweofoff
,
3328 PAGE_SIZE
- (neweofoff
& PAGE_MASK
));
3332 np
->n_size
= uio_offset(uio
) + n
;
3333 nfs_node_lock_force(np
);
3334 CLR(np
->n_flag
, NUPDATESIZE
);
3335 np
->n_flag
|= NMODIFIED
;
3336 nfs_node_unlock(np
);
3337 FSDBG(516, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0001);
3338 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX errors */
3341 * If dirtyend exceeds file size, chop it down. This should
3342 * not occur unless there is a race.
3344 if (NBOFF(bp
) + bp
->nb_dirtyend
> (off_t
)np
->n_size
) {
3345 bp
->nb_dirtyend
= np
->n_size
- NBOFF(bp
);
3346 if (bp
->nb_dirtyoff
>= bp
->nb_dirtyend
) {
3347 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
3351 * UBC doesn't handle partial pages, so we need to make sure
3352 * that any pages left in the page cache are completely valid.
3354 * Writes that are smaller than a block are delayed if they
3355 * don't extend to the end of the block.
3357 * If the block isn't (completely) cached, we may need to read
3358 * in some parts of pages that aren't covered by the write.
3359 * If the write offset (on) isn't page aligned, we'll need to
3360 * read the start of the first page being written to. Likewise,
3361 * if the offset of the end of the write (on+n) isn't page aligned,
3362 * we'll need to read the end of the last page being written to.
3365 * We don't want to read anything we're just going to write over.
3366 * We don't want to read anything we're just going drop when the
3367 * I/O is complete (i.e. don't do reads for NOCACHE requests).
3368 * We don't want to issue multiple I/Os if we don't have to
3369 * (because they're synchronous rpcs).
3370 * We don't want to read anything we already have modified in the
3373 if (!ISSET(bp
->nb_flags
, NB_CACHE
) && (n
< biosize
)) {
3374 int firstpg
, lastpg
, dirtypg
;
3375 int firstpgoff
, lastpgoff
;
3377 firstpg
= on
/ PAGE_SIZE
;
3378 firstpgoff
= on
& PAGE_MASK
;
3379 lastpg
= (on
+ n
- 1) / PAGE_SIZE
;
3380 lastpgoff
= (on
+ n
) & PAGE_MASK
;
3381 if (firstpgoff
&& !NBPGVALID(bp
, firstpg
)) {
3382 /* need to read start of first page */
3383 start
= firstpg
* PAGE_SIZE
;
3384 end
= start
+ firstpgoff
;
3386 if (lastpgoff
&& !NBPGVALID(bp
, lastpg
)) {
3387 /* need to read end of last page */
3389 start
= (lastpg
* PAGE_SIZE
) + lastpgoff
;
3391 end
= (lastpg
+ 1) * PAGE_SIZE
;
3393 if (ISSET(bp
->nb_flags
, NB_NOCACHE
)) {
3395 * For nocache writes, if there is any partial page at the
3396 * start or end of the write range, then we do the write
3397 * synchronously to make sure that we can drop the data
3398 * from the cache as soon as the WRITE finishes. Normally,
3399 * we would do an unstable write and not drop the data until
3400 * it was committed. But doing that here would risk allowing
3401 * invalid data to be read from the cache between the WRITE
3403 * (NB_STABLE indicates that data writes should be FILESYNC)
3406 SET(bp
->nb_flags
, NB_STABLE
);
3411 /* need to read the data in range: start...end-1 */
3413 /* first, check for dirty pages in between */
3414 /* if there are, we'll have to do two reads because */
3415 /* we don't want to overwrite the dirty pages. */
3416 for (dirtypg
= start
/ PAGE_SIZE
; dirtypg
<= (end
- 1) / PAGE_SIZE
; dirtypg
++) {
3417 if (NBPGDIRTY(bp
, dirtypg
)) {
3422 /* if start is at beginning of page, try */
3423 /* to get any preceeding pages as well. */
3424 if (!(start
& PAGE_MASK
)) {
3425 /* stop at next dirty/valid page or start of block */
3426 for (; start
> 0; start
-= PAGE_SIZE
) {
3427 if (NBPGVALID(bp
, ((start
- 1) / PAGE_SIZE
))) {
3434 /* setup uio for read(s) */
3436 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
3437 &auio_buf
, sizeof(auio_buf
));
3439 if (dirtypg
<= (end
- 1) / PAGE_SIZE
) {
3440 /* there's a dirty page in the way, so just do two reads */
3441 /* we'll read the preceding data here */
3442 uio_reset(auio
, boff
+ start
, UIO_SYSSPACE
, UIO_READ
);
3443 uio_addiov(auio
, CAST_USER_ADDR_T(bp
->nb_data
+ start
), on
- start
);
3444 error
= nfs_read_rpc(np
, auio
, ctx
);
3446 /* couldn't read the data, so treat buffer as synchronous NOCACHE */
3447 SET(bp
->nb_flags
, (NB_NOCACHE
| NB_STABLE
));
3450 if (uio_resid(auio
) > 0) {
3451 FSDBG(516, bp
, (caddr_t
)uio_curriovbase(auio
) - bp
->nb_data
, uio_resid(auio
), 0xd00dee01);
3452 bzero(CAST_DOWN(caddr_t
, uio_curriovbase(auio
)), uio_resid(auio
));
3455 /* update validoff/validend if necessary */
3456 if ((bp
->nb_validoff
< 0) || (bp
->nb_validoff
> start
)) {
3457 bp
->nb_validoff
= start
;
3459 if ((bp
->nb_validend
< 0) || (bp
->nb_validend
< on
)) {
3460 bp
->nb_validend
= on
;
3462 if ((off_t
)np
->n_size
> boff
+ bp
->nb_validend
) {
3463 bp
->nb_validend
= min(np
->n_size
- (boff
+ start
), biosize
);
3465 /* validate any pages before the write offset */
3466 for (; start
< on
/ PAGE_SIZE
; start
+= PAGE_SIZE
) {
3467 NBPGVALID_SET(bp
, start
/ PAGE_SIZE
);
3470 /* adjust start to read any trailing data */
3474 /* if end is at end of page, try to */
3475 /* get any following pages as well. */
3476 if (!(end
& PAGE_MASK
)) {
3477 /* stop at next valid page or end of block */
3478 for (; end
< biosize
; end
+= PAGE_SIZE
) {
3479 if (NBPGVALID(bp
, end
/ PAGE_SIZE
)) {
3485 if (((boff
+ start
) >= (off_t
)np
->n_size
) ||
3486 ((start
>= on
) && ((boff
+ on
+ n
) >= (off_t
)np
->n_size
))) {
3488 * Either this entire read is beyond the current EOF
3489 * or the range that we won't be modifying (on+n...end)
3490 * is all beyond the current EOF.
3491 * No need to make a trip across the network to
3492 * read nothing. So, just zero the buffer instead.
3494 FSDBG(516, bp
, start
, end
- start
, 0xd00dee00);
3495 bzero(bp
->nb_data
+ start
, end
- start
);
3498 /* now we'll read the (rest of the) data */
3499 uio_reset(auio
, boff
+ start
, UIO_SYSSPACE
, UIO_READ
);
3500 uio_addiov(auio
, CAST_USER_ADDR_T(bp
->nb_data
+ start
), end
- start
);
3501 error
= nfs_read_rpc(np
, auio
, ctx
);
3503 /* couldn't read the data, so treat buffer as synchronous NOCACHE */
3504 SET(bp
->nb_flags
, (NB_NOCACHE
| NB_STABLE
));
3507 if (uio_resid(auio
) > 0) {
3508 FSDBG(516, bp
, (caddr_t
)uio_curriovbase(auio
) - bp
->nb_data
, uio_resid(auio
), 0xd00dee02);
3509 bzero(CAST_DOWN(caddr_t
, uio_curriovbase(auio
)), uio_resid(auio
));
3513 /* update validoff/validend if necessary */
3514 if ((bp
->nb_validoff
< 0) || (bp
->nb_validoff
> start
)) {
3515 bp
->nb_validoff
= start
;
3517 if ((bp
->nb_validend
< 0) || (bp
->nb_validend
< end
)) {
3518 bp
->nb_validend
= end
;
3520 if ((off_t
)np
->n_size
> boff
+ bp
->nb_validend
) {
3521 bp
->nb_validend
= min(np
->n_size
- (boff
+ start
), biosize
);
3523 /* validate any pages before the write offset's page */
3524 for (; start
< (off_t
)trunc_page_32(on
); start
+= PAGE_SIZE
) {
3525 NBPGVALID_SET(bp
, start
/ PAGE_SIZE
);
3527 /* validate any pages after the range of pages being written to */
3528 for (; (end
- 1) > (off_t
)round_page_32(on
+ n
- 1); end
-= PAGE_SIZE
) {
3529 NBPGVALID_SET(bp
, (end
- 1) / PAGE_SIZE
);
3532 /* Note: pages being written to will be validated when written */
3537 if (ISSET(bp
->nb_flags
, NB_ERROR
)) {
3538 error
= bp
->nb_error
;
3539 nfs_buf_release(bp
, 1);
3543 nfs_node_lock_force(np
);
3544 np
->n_flag
|= NMODIFIED
;
3545 nfs_node_unlock(np
);
3548 error
= uiomove((char *)bp
->nb_data
+ on
, n
, uio
);
3550 SET(bp
->nb_flags
, NB_ERROR
);
3551 nfs_buf_release(bp
, 1);
3555 /* validate any pages written to */
3556 start
= on
& ~PAGE_MASK
;
3557 for (; start
< on
+ n
; start
+= PAGE_SIZE
) {
3558 NBPGVALID_SET(bp
, start
/ PAGE_SIZE
);
3560 * This may seem a little weird, but we don't actually set the
3561 * dirty bits for writes. This is because we keep the dirty range
3562 * in the nb_dirtyoff/nb_dirtyend fields. Also, particularly for
3563 * delayed writes, when we give the pages back to the VM we don't
3564 * want to keep them marked dirty, because when we later write the
3565 * buffer we won't be able to tell which pages were written dirty
3566 * and which pages were mmapped and dirtied.
3569 if (bp
->nb_dirtyend
> 0) {
3570 bp
->nb_dirtyoff
= min(on
, bp
->nb_dirtyoff
);
3571 bp
->nb_dirtyend
= max((on
+ n
), bp
->nb_dirtyend
);
3573 bp
->nb_dirtyoff
= on
;
3574 bp
->nb_dirtyend
= on
+ n
;
3576 if (bp
->nb_validend
<= 0 || bp
->nb_validend
< bp
->nb_dirtyoff
||
3577 bp
->nb_validoff
> bp
->nb_dirtyend
) {
3578 bp
->nb_validoff
= bp
->nb_dirtyoff
;
3579 bp
->nb_validend
= bp
->nb_dirtyend
;
3581 bp
->nb_validoff
= min(bp
->nb_validoff
, bp
->nb_dirtyoff
);
3582 bp
->nb_validend
= max(bp
->nb_validend
, bp
->nb_dirtyend
);
3584 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) {
3585 nfs_buf_normalize_valid_range(np
, bp
);
3589 * Since this block is being modified, it must be written
3590 * again and not just committed.
3592 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
3593 nfs_node_lock_force(np
);
3594 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
3595 np
->n_needcommitcnt
--;
3596 CHECK_NEEDCOMMITCNT(np
);
3598 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
3599 nfs_node_unlock(np
);
3602 if (ioflag
& IO_SYNC
) {
3603 error
= nfs_buf_write(bp
);
3607 } else if (((n
+ on
) == biosize
) || (ioflag
& IO_APPEND
) ||
3608 (ioflag
& IO_NOCACHE
) || ISSET(bp
->nb_flags
, NB_NOCACHE
)) {
3609 SET(bp
->nb_flags
, NB_ASYNC
);
3610 error
= nfs_buf_write(bp
);
3615 /* If the block wasn't already delayed: charge for the write */
3616 if (!ISSET(bp
->nb_flags
, NB_DELWRI
)) {
3617 proc_t p
= vfs_context_proc(ctx
);
3618 if (p
&& p
->p_stats
) {
3619 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_oublock
);
3622 nfs_buf_write_delayed(bp
);
3626 if (np
->n_needcommitcnt
>= NFS_A_LOT_OF_NEEDCOMMITS
) {
3627 nfs_flushcommits(np
, 1);
3629 } while (uio_resid(uio
) > 0 && n
> 0);
3632 nfs_node_lock_force(np
);
3634 nfs_node_unlock(np
);
3635 nfs_data_unlock(np
);
3636 FSDBG_BOT(515, np
, uio_offset(uio
), uio_resid(uio
), error
);
3652 return nfs_write_rpc2(np
, uio
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
), iomodep
, wverfp
);
3664 struct nfsmount
*nmp
;
3665 int error
= 0, nfsvers
;
3666 int wverfset
, commit
, committed
;
3667 uint64_t wverf
= 0, wverf2
;
3668 size_t nmwsize
, totalsize
, tsiz
, len
, rlen
;
3669 struct nfsreq rq
, *req
= &rq
;
3671 uint32_t stategenid
= 0, restart
= 0;
3673 uint32_t vrestart
= 0;
3674 uio_t uio_save
= NULL
;
3677 /* XXX limitation based on need to back up uio on short write */
3678 if (uio_iovcnt(uio
) != 1) {
3679 panic("nfs3_write_rpc: iovcnt > 1");
3682 FSDBG_TOP(537, np
, uio_offset(uio
), uio_resid(uio
), *iomodep
);
3684 if (nfs_mount_gone(nmp
)) {
3687 nfsvers
= nmp
->nm_vers
;
3688 nmwsize
= nmp
->nm_wsize
;
3691 committed
= NFS_WRITE_FILESYNC
;
3693 totalsize
= tsiz
= uio_resid(uio
);
3694 if ((nfsvers
== NFS_VER2
) && ((uint64_t)(uio_offset(uio
) + tsiz
) > 0xffffffffULL
)) {
3695 FSDBG_BOT(537, np
, uio_offset(uio
), uio_resid(uio
), EFBIG
);
3699 uio_save
= uio_duplicate(uio
);
3700 if (uio_save
== NULL
) {
3705 len
= (tsiz
> nmwsize
) ? nmwsize
: tsiz
;
3706 FSDBG(537, np
, uio_offset(uio
), len
, 0);
3707 if (np
->n_flag
& NREVOKE
) {
3712 if (nmp
->nm_vers
>= NFS_VER4
) {
3713 stategenid
= nmp
->nm_stategenid
;
3716 error
= nmp
->nm_funcs
->nf_write_rpc_async(np
, uio
, len
, thd
, cred
, *iomodep
, NULL
, &req
);
3718 error
= nmp
->nm_funcs
->nf_write_rpc_async_finish(np
, req
, &commit
, &rlen
, &wverf2
);
3721 if (nfs_mount_gone(nmp
)) {
3725 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
) &&
3726 (++restart
<= nfs_mount_state_max_restarts(nmp
))) { /* guard against no progress */
3727 lck_mtx_lock(&nmp
->nm_lock
);
3728 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
3729 NP(np
, "nfs_write_rpc: error %d, initiating recovery", error
);
3730 nfs_need_recover(nmp
, error
);
3732 lck_mtx_unlock(&nmp
->nm_lock
);
3733 if (np
->n_flag
& NREVOKE
) {
3736 if (error
== NFSERR_GRACE
) {
3737 tsleep(&nmp
->nm_state
, (PZERO
- 1), "nfsgrace", 2 * hz
);
3739 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
))) {
3748 if (nfsvers
== NFS_VER2
) {
3753 /* check for a short write */
3755 /* Reset the uio to reflect the actual transfer */
3757 uio_update(uio
, totalsize
- (tsiz
- rlen
));
3761 /* return lowest commit level returned */
3762 if (commit
< committed
) {
3768 /* check write verifier */
3772 } else if (wverf
!= wverf2
) {
3773 /* verifier changed, so we need to restart all the writes */
3774 if (++vrestart
> 100) {
3775 /* give up after too many restarts */
3779 *uio
= *uio_save
; // Reset the uio back to the start
3780 committed
= NFS_WRITE_FILESYNC
;
3788 if (wverfset
&& wverfp
) {
3791 *iomodep
= committed
;
3793 uio_setresid(uio
, tsiz
);
3795 FSDBG_BOT(537, np
, committed
, uio_resid(uio
), error
);
3800 nfs3_write_rpc_async(
3807 struct nfsreq_cbinfo
*cb
,
3808 struct nfsreq
**reqp
)
3810 struct nfsmount
*nmp
;
3812 int error
= 0, nfsvers
;
3813 struct nfsm_chain nmreq
;
3816 if (nfs_mount_gone(nmp
)) {
3819 nfsvers
= nmp
->nm_vers
;
3821 /* for async mounts, don't bother sending sync write requests */
3822 if ((iomode
!= NFS_WRITE_UNSTABLE
) && nfs_allow_async
&&
3823 ((mp
= NFSTOMP(np
))) && (vfs_flags(mp
) & MNT_ASYNC
)) {
3824 iomode
= NFS_WRITE_UNSTABLE
;
3827 nfsm_chain_null(&nmreq
);
3828 nfsm_chain_build_alloc_init(error
, &nmreq
,
3829 NFSX_FH(nfsvers
) + 5 * NFSX_UNSIGNED
+ nfsm_rndup(len
));
3830 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
3831 if (nfsvers
== NFS_VER3
) {
3832 nfsm_chain_add_64(error
, &nmreq
, uio_offset(uio
));
3833 nfsm_chain_add_32(error
, &nmreq
, len
);
3834 nfsm_chain_add_32(error
, &nmreq
, iomode
);
3836 nfsm_chain_add_32(error
, &nmreq
, 0);
3837 nfsm_chain_add_32(error
, &nmreq
, uio_offset(uio
));
3838 nfsm_chain_add_32(error
, &nmreq
, 0);
3840 nfsm_chain_add_32(error
, &nmreq
, len
);
3842 error
= nfsm_chain_add_uio(&nmreq
, uio
, len
);
3843 nfsm_chain_build_done(error
, &nmreq
);
3845 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC_WRITE
, thd
, cred
, NULL
, 0, cb
, reqp
);
3847 nfsm_chain_cleanup(&nmreq
);
3852 nfs3_write_rpc_async_finish(
3859 struct nfsmount
*nmp
;
3860 int error
= 0, lockerror
= ENOENT
, nfsvers
, status
;
3861 int updatemtime
= 0, wccpostattr
= 0, rlen
, committed
= NFS_WRITE_FILESYNC
;
3862 u_int64_t xid
, wverf
;
3864 struct nfsm_chain nmrep
;
3867 if (nfs_mount_gone(nmp
)) {
3868 nfs_request_async_cancel(req
);
3871 nfsvers
= nmp
->nm_vers
;
3873 nfsm_chain_null(&nmrep
);
3875 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
3876 if (error
== EINPROGRESS
) { /* async request restarted */
3880 if (nfs_mount_gone(nmp
)) {
3883 if (!error
&& (lockerror
= nfs_node_lock(np
))) {
3886 if (nfsvers
== NFS_VER3
) {
3887 struct timespec premtime
= { .tv_sec
= 0, .tv_nsec
= 0 };
3888 nfsm_chain_get_wcc_data(error
, &nmrep
, np
, &premtime
, &wccpostattr
, &xid
);
3889 if (nfstimespeccmp(&np
->n_mtime
, &premtime
, ==)) {
3895 nfsm_chain_get_32(error
, &nmrep
, rlen
);
3901 nfsm_chain_get_32(error
, &nmrep
, committed
);
3902 nfsm_chain_get_64(error
, &nmrep
, wverf
);
3907 lck_mtx_lock(&nmp
->nm_lock
);
3908 if (!(nmp
->nm_state
& NFSSTA_HASWRITEVERF
)) {
3909 nmp
->nm_verf
= wverf
;
3910 nmp
->nm_state
|= NFSSTA_HASWRITEVERF
;
3911 } else if (nmp
->nm_verf
!= wverf
) {
3912 nmp
->nm_verf
= wverf
;
3914 lck_mtx_unlock(&nmp
->nm_lock
);
3919 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
3923 NFS_CHANGED_UPDATE(nfsvers
, np
, &np
->n_vattr
);
3927 nfs_node_unlock(np
);
3929 nfsm_chain_cleanup(&nmrep
);
3930 if ((committed
!= NFS_WRITE_FILESYNC
) && nfs_allow_async
&&
3931 ((mp
= NFSTOMP(np
))) && (vfs_flags(mp
) & MNT_ASYNC
)) {
3932 committed
= NFS_WRITE_FILESYNC
;
3934 *iomodep
= committed
;
3939 * NFS mknod vnode op
3941 * For NFS v2 this is a kludge. Use a create RPC but with the IFMT bits of the
3942 * mode set to specify the file type and the size field for rdev.
3946 struct vnop_mknod_args
/* {
3947 * struct vnodeop_desc *a_desc;
3950 * struct componentname *a_cnp;
3951 * struct vnode_attr *a_vap;
3952 * vfs_context_t a_context;
3955 vnode_t dvp
= ap
->a_dvp
;
3956 vnode_t
*vpp
= ap
->a_vpp
;
3957 struct componentname
*cnp
= ap
->a_cnp
;
3958 struct vnode_attr
*vap
= ap
->a_vap
;
3959 vfs_context_t ctx
= ap
->a_context
;
3960 vnode_t newvp
= NULL
;
3961 nfsnode_t np
= NULL
;
3962 struct nfsmount
*nmp
;
3963 nfsnode_t dnp
= VTONFS(dvp
);
3964 struct nfs_vattr nvattr
;
3966 int error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0;
3967 struct timespec premtime
= { .tv_sec
= 0, .tv_nsec
= 0 };
3969 u_int64_t xid
= 0, dxid
;
3970 int nfsvers
, gotuid
, gotgid
;
3971 struct nfsm_chain nmreq
, nmrep
;
3972 struct nfsreq rq
, *req
= &rq
;
3975 if (nfs_mount_gone(nmp
)) {
3978 nfsvers
= nmp
->nm_vers
;
3980 if (!VATTR_IS_ACTIVE(vap
, va_type
)) {
3983 if (vap
->va_type
== VCHR
|| vap
->va_type
== VBLK
) {
3984 if (!VATTR_IS_ACTIVE(vap
, va_rdev
)) {
3987 rdev
= vap
->va_rdev
;
3988 } else if (vap
->va_type
== VFIFO
|| vap
->va_type
== VSOCK
) {
3993 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
)) {
3994 return ENAMETOOLONG
;
3997 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
3999 VATTR_SET_SUPPORTED(vap
, va_mode
);
4000 VATTR_SET_SUPPORTED(vap
, va_uid
);
4001 VATTR_SET_SUPPORTED(vap
, va_gid
);
4002 VATTR_SET_SUPPORTED(vap
, va_data_size
);
4003 VATTR_SET_SUPPORTED(vap
, va_access_time
);
4004 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
4005 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
4006 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
4008 nfsm_chain_null(&nmreq
);
4009 nfsm_chain_null(&nmrep
);
4011 nfsm_chain_build_alloc_init(error
, &nmreq
,
4012 NFSX_FH(nfsvers
) + 4 * NFSX_UNSIGNED
+
4013 nfsm_rndup(cnp
->cn_namelen
) + NFSX_SATTR(nfsvers
));
4014 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4015 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4016 if (nfsvers
== NFS_VER3
) {
4017 nfsm_chain_add_32(error
, &nmreq
, vtonfs_type(vap
->va_type
, nfsvers
));
4018 nfsm_chain_add_v3sattr(nmp
, error
, &nmreq
, vap
);
4019 if (vap
->va_type
== VCHR
|| vap
->va_type
== VBLK
) {
4020 nfsm_chain_add_32(error
, &nmreq
, major(vap
->va_rdev
));
4021 nfsm_chain_add_32(error
, &nmreq
, minor(vap
->va_rdev
));
4024 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, rdev
);
4026 nfsm_chain_build_done(error
, &nmreq
);
4028 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
4032 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_MKNOD
,
4033 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
4035 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
4038 if ((lockerror
= nfs_node_lock(dnp
))) {
4041 /* XXX no EEXIST kludge here? */
4043 if (!error
&& !status
) {
4044 if (dnp
->n_flag
& NNEGNCENTRIES
) {
4045 dnp
->n_flag
&= ~NNEGNCENTRIES
;
4046 cache_purge_negatives(dvp
);
4048 error
= nfsm_chain_get_fh_attr(nmp
, &nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
4050 if (nfsvers
== NFS_VER3
) {
4051 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
4057 nfsm_chain_cleanup(&nmreq
);
4058 nfsm_chain_cleanup(&nmrep
);
4061 dnp
->n_flag
|= NMODIFIED
;
4062 /* if directory hadn't changed, update namecache mtime */
4063 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==)) {
4064 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
4066 nfs_node_unlock(dnp
);
4067 /* nfs_getattr() will check changed and purge caches */
4068 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
4071 if (!error
&& fh
.fh_len
) {
4072 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
4074 if (!error
&& !np
) {
4075 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
4081 nfs_node_clear_busy(dnp
);
4084 if (!error
&& (gotuid
|| gotgid
) &&
4085 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
4086 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
4087 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
4088 /* clear ID bits if server didn't use them (or we can't tell) */
4089 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
4090 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
4094 nfs_node_unlock(np
);
4099 nfs_node_unlock(np
);
4104 static uint32_t create_verf
;
4106 * NFS file create call
4110 struct vnop_create_args
/* {
4111 * struct vnodeop_desc *a_desc;
4114 * struct componentname *a_cnp;
4115 * struct vnode_attr *a_vap;
4116 * vfs_context_t a_context;
4119 vfs_context_t ctx
= ap
->a_context
;
4120 vnode_t dvp
= ap
->a_dvp
;
4121 struct vnode_attr
*vap
= ap
->a_vap
;
4122 struct componentname
*cnp
= ap
->a_cnp
;
4123 struct nfs_vattr nvattr
;
4125 nfsnode_t np
= NULL
;
4126 struct nfsmount
*nmp
;
4127 nfsnode_t dnp
= VTONFS(dvp
);
4128 vnode_t newvp
= NULL
;
4129 int error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0, fmode
= 0;
4130 struct timespec premtime
= { .tv_sec
= 0, .tv_nsec
= 0 };
4131 int nfsvers
, gotuid
, gotgid
;
4132 u_int64_t xid
, dxid
;
4134 struct nfsm_chain nmreq
, nmrep
;
4135 struct nfsreq rq
, *req
= &rq
;
4136 struct nfs_dulookup dul
;
4137 int dul_in_progress
= 0;
4141 if (nfs_mount_gone(nmp
)) {
4144 nfsvers
= nmp
->nm_vers
;
4145 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
4147 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
)) {
4148 return ENAMETOOLONG
;
4151 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
4153 VATTR_SET_SUPPORTED(vap
, va_mode
);
4154 VATTR_SET_SUPPORTED(vap
, va_uid
);
4155 VATTR_SET_SUPPORTED(vap
, va_gid
);
4156 VATTR_SET_SUPPORTED(vap
, va_data_size
);
4157 VATTR_SET_SUPPORTED(vap
, va_access_time
);
4158 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
4159 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
4160 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
4162 if ((vap
->va_vaflags
& VA_EXCLUSIVE
)
4165 if (!VATTR_IS_ACTIVE(vap
, va_access_time
) || !VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
4166 vap
->va_vaflags
|= VA_UTIMES_NULL
;
4171 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
4173 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4176 nfsm_chain_null(&nmreq
);
4177 nfsm_chain_null(&nmrep
);
4179 nfsm_chain_build_alloc_init(error
, &nmreq
,
4180 NFSX_FH(nfsvers
) + 2 * NFSX_UNSIGNED
+
4181 nfsm_rndup(cnp
->cn_namelen
) + NFSX_SATTR(nfsvers
));
4182 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4183 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4184 if (nfsvers
== NFS_VER3
) {
4185 if (fmode
& O_EXCL
) {
4186 nfsm_chain_add_32(error
, &nmreq
, NFS_CREATE_EXCLUSIVE
);
4187 lck_rw_lock_shared(in_ifaddr_rwlock
);
4188 if (!TAILQ_EMPTY(&in_ifaddrhead
)) {
4189 val
= IA_SIN(in_ifaddrhead
.tqh_first
)->sin_addr
.s_addr
;
4193 lck_rw_done(in_ifaddr_rwlock
);
4194 nfsm_chain_add_32(error
, &nmreq
, val
);
4196 nfsm_chain_add_32(error
, &nmreq
, create_verf
);
4198 nfsm_chain_add_32(error
, &nmreq
, NFS_CREATE_UNCHECKED
);
4199 nfsm_chain_add_v3sattr(nmp
, error
, &nmreq
, vap
);
4202 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, 0);
4204 nfsm_chain_build_done(error
, &nmreq
);
4207 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_CREATE
,
4208 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
4211 nfs_dulookup_start(&dul
, dnp
, ctx
);
4212 dul_in_progress
= 1;
4214 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
4217 if ((lockerror
= nfs_node_lock(dnp
))) {
4221 if (!error
&& !status
) {
4222 if (dnp
->n_flag
& NNEGNCENTRIES
) {
4223 dnp
->n_flag
&= ~NNEGNCENTRIES
;
4224 cache_purge_negatives(dvp
);
4226 error
= nfsm_chain_get_fh_attr(nmp
, &nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
4228 if (nfsvers
== NFS_VER3
) {
4229 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
4235 nfsm_chain_cleanup(&nmreq
);
4236 nfsm_chain_cleanup(&nmrep
);
4239 dnp
->n_flag
|= NMODIFIED
;
4240 /* if directory hadn't changed, update namecache mtime */
4241 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==)) {
4242 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
4244 nfs_node_unlock(dnp
);
4245 /* nfs_getattr() will check changed and purge caches */
4246 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
4249 if (!error
&& fh
.fh_len
) {
4250 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
4252 if (!error
&& !np
) {
4253 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
4259 if (dul_in_progress
) {
4260 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4263 nfs_node_clear_busy(dnp
);
4267 if ((nfsvers
== NFS_VER3
) && (fmode
& O_EXCL
) && (error
== NFSERR_NOTSUPP
)) {
4272 nfs_node_unlock(np
);
4275 } else if ((nfsvers
== NFS_VER3
) && (fmode
& O_EXCL
)) {
4276 nfs_node_unlock(np
);
4277 error
= nfs3_setattr_rpc(np
, vap
, ctx
);
4278 if (error
&& (gotuid
|| gotgid
)) {
4279 /* it's possible the server didn't like our attempt to set IDs. */
4280 /* so, let's try it again without those */
4281 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
4282 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
4283 error
= nfs3_setattr_rpc(np
, vap
, ctx
);
4288 nfs_node_lock_force(np
);
4294 if (!error
&& (gotuid
|| gotgid
) &&
4295 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
4296 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
4297 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
4298 /* clear ID bits if server didn't use them (or we can't tell) */
4299 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
4300 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
4303 nfs_node_unlock(np
);
4309 * NFS file remove call
4310 * To try and make NFS semantics closer to UFS semantics, a file that has
4311 * other processes using the vnode is renamed instead of removed and then
4312 * removed later on the last close.
4313 * - If vnode_isinuse()
4314 * If a rename is not already in the works
4315 * call nfs_sillyrename() to set it up
4321 struct vnop_remove_args
/* {
4322 * struct vnodeop_desc *a_desc;
4325 * struct componentname *a_cnp;
4327 * vfs_context_t a_context;
4330 vfs_context_t ctx
= ap
->a_context
;
4331 vnode_t vp
= ap
->a_vp
;
4332 vnode_t dvp
= ap
->a_dvp
;
4333 struct componentname
*cnp
= ap
->a_cnp
;
4334 nfsnode_t dnp
= VTONFS(dvp
);
4335 nfsnode_t np
= VTONFS(vp
);
4336 int error
= 0, nfsvers
, namedattrs
, inuse
, gotattr
= 0, flushed
= 0, setsize
= 0;
4337 struct nfs_vattr nvattr
;
4338 struct nfsmount
*nmp
;
4339 struct nfs_dulookup dul
;
4341 /* XXX prevent removing a sillyrenamed file? */
4343 nmp
= NFSTONMP(dnp
);
4344 if (nfs_mount_gone(nmp
)) {
4347 nfsvers
= nmp
->nm_vers
;
4348 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
4351 error
= nfs_node_set_busy2(dnp
, np
, vfs_context_thread(ctx
));
4356 /* lock the node while we remove the file */
4357 lck_mtx_lock(nfs_node_hash_mutex
);
4358 while (np
->n_hflag
& NHLOCKED
) {
4359 np
->n_hflag
|= NHLOCKWANT
;
4360 msleep(np
, nfs_node_hash_mutex
, PINOD
, "nfs_remove", NULL
);
4362 np
->n_hflag
|= NHLOCKED
;
4363 lck_mtx_unlock(nfs_node_hash_mutex
);
4366 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4369 inuse
= vnode_isinuse(vp
, 0);
4370 if ((ap
->a_flags
& VNODE_REMOVE_NODELETEBUSY
) && inuse
) {
4371 /* Caller requested Carbon delete semantics, but file is busy */
4375 if (inuse
&& !gotattr
) {
4376 if (nfs_getattr(np
, &nvattr
, ctx
, NGA_CACHED
)) {
4377 nvattr
.nva_nlink
= 1;
4382 if (!inuse
|| (np
->n_sillyrename
&& (nvattr
.nva_nlink
> 1))) {
4383 if (!inuse
&& !flushed
) { /* flush all the buffers first */
4384 /* unlock the node */
4385 lck_mtx_lock(nfs_node_hash_mutex
);
4386 np
->n_hflag
&= ~NHLOCKED
;
4387 if (np
->n_hflag
& NHLOCKWANT
) {
4388 np
->n_hflag
&= ~NHLOCKWANT
;
4391 lck_mtx_unlock(nfs_node_hash_mutex
);
4392 nfs_node_clear_busy2(dnp
, np
);
4393 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
4394 FSDBG(260, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0011);
4396 if (error
== EINTR
) {
4397 nfs_node_lock_force(np
);
4398 NATTRINVALIDATE(np
);
4399 nfs_node_unlock(np
);
4403 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4408 if ((nmp
->nm_vers
>= NFS_VER4
) && (np
->n_openflags
& N_DELEG_MASK
)) {
4409 nfs4_delegation_return(np
, 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4413 * Purge the name cache so that the chance of a lookup for
4414 * the name succeeding while the remove is in progress is
4417 nfs_name_cache_purge(dnp
, np
, cnp
, ctx
);
4420 nfs_dulookup_start(&dul
, dnp
, ctx
);
4424 error
= nmp
->nm_funcs
->nf_remove_rpc(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
,
4425 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4428 * Kludge City: If the first reply to the remove rpc is lost..
4429 * the reply to the retransmitted request will be ENOENT
4430 * since the file was in fact removed
4431 * Therefore, we cheat and return success.
4433 if (error
== ENOENT
) {
4437 if (!error
&& !inuse
&& !np
->n_sillyrename
) {
4439 * removal succeeded, it's not in use, and not silly renamed so
4440 * remove nfsnode from hash now so we can't accidentally find it
4441 * again if another object gets created with the same filehandle
4442 * before this vnode gets reclaimed
4444 lck_mtx_lock(nfs_node_hash_mutex
);
4445 if (np
->n_hflag
& NHHASHED
) {
4446 LIST_REMOVE(np
, n_hash
);
4447 np
->n_hflag
&= ~NHHASHED
;
4448 FSDBG(266, 0, np
, np
->n_flag
, 0xb1eb1e);
4450 lck_mtx_unlock(nfs_node_hash_mutex
);
4451 /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */
4452 /* clear all flags other than these */
4453 nfs_node_lock_force(np
);
4454 np
->n_flag
&= (NMODIFIED
);
4455 NATTRINVALIDATE(np
);
4456 nfs_node_unlock(np
);
4460 nfs_node_lock_force(np
);
4461 NATTRINVALIDATE(np
);
4462 nfs_node_unlock(np
);
4464 } else if (!np
->n_sillyrename
) {
4466 nfs_dulookup_start(&dul
, dnp
, ctx
);
4468 error
= nfs_sillyrename(dnp
, np
, cnp
, ctx
);
4469 nfs_node_lock_force(np
);
4470 NATTRINVALIDATE(np
);
4471 nfs_node_unlock(np
);
4473 nfs_node_lock_force(np
);
4474 NATTRINVALIDATE(np
);
4475 nfs_node_unlock(np
);
4477 nfs_dulookup_start(&dul
, dnp
, ctx
);
4481 /* nfs_getattr() will check changed and purge caches */
4482 nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
);
4484 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4487 /* unlock the node */
4488 lck_mtx_lock(nfs_node_hash_mutex
);
4489 np
->n_hflag
&= ~NHLOCKED
;
4490 if (np
->n_hflag
& NHLOCKWANT
) {
4491 np
->n_hflag
&= ~NHLOCKWANT
;
4494 lck_mtx_unlock(nfs_node_hash_mutex
);
4495 nfs_node_clear_busy2(dnp
, np
);
4503 * NFS silly-renamed file removal function called from nfs_vnop_inactive
4506 nfs_removeit(struct nfs_sillyrename
*nsp
)
4508 struct nfsmount
*nmp
= NFSTONMP(nsp
->nsr_dnp
);
4509 if (nfs_mount_gone(nmp
)) {
4512 return nmp
->nm_funcs
->nf_remove_rpc(nsp
->nsr_dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, NULL
, nsp
->nsr_cred
);
4516 * NFS remove rpc, called from nfs_remove() and nfs_removeit().
4526 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0;
4527 struct timespec premtime
= { .tv_sec
= 0, .tv_nsec
= 0 };
4528 struct nfsmount
*nmp
;
4531 struct nfsm_chain nmreq
, nmrep
;
4533 nmp
= NFSTONMP(dnp
);
4534 if (nfs_mount_gone(nmp
)) {
4537 nfsvers
= nmp
->nm_vers
;
4538 if ((nfsvers
== NFS_VER2
) && (namelen
> NFS_MAXNAMLEN
)) {
4539 return ENAMETOOLONG
;
4542 nfsm_chain_null(&nmreq
);
4543 nfsm_chain_null(&nmrep
);
4545 nfsm_chain_build_alloc_init(error
, &nmreq
,
4546 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+ nfsm_rndup(namelen
));
4547 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4548 nfsm_chain_add_name(error
, &nmreq
, name
, namelen
, nmp
);
4549 nfsm_chain_build_done(error
, &nmreq
);
4552 error
= nfs_request2(dnp
, NULL
, &nmreq
, NFSPROC_REMOVE
, thd
, cred
, NULL
, 0, &nmrep
, &xid
, &status
);
4554 if ((lockerror
= nfs_node_lock(dnp
))) {
4557 if (nfsvers
== NFS_VER3
) {
4558 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &xid
);
4561 dnp
->n_flag
|= NMODIFIED
;
4562 /* if directory hadn't changed, update namecache mtime */
4563 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==)) {
4564 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
4567 NATTRINVALIDATE(dnp
);
4574 nfs_node_unlock(dnp
);
4576 nfsm_chain_cleanup(&nmreq
);
4577 nfsm_chain_cleanup(&nmrep
);
4582 * NFS file rename call
4586 struct vnop_rename_args
/* {
4587 * struct vnodeop_desc *a_desc;
4590 * struct componentname *a_fcnp;
4593 * struct componentname *a_tcnp;
4594 * vfs_context_t a_context;
4597 vfs_context_t ctx
= ap
->a_context
;
4598 vnode_t fdvp
= ap
->a_fdvp
;
4599 vnode_t fvp
= ap
->a_fvp
;
4600 vnode_t tdvp
= ap
->a_tdvp
;
4601 vnode_t tvp
= ap
->a_tvp
;
4602 nfsnode_t fdnp
, fnp
, tdnp
, tnp
;
4603 struct componentname
*tcnp
= ap
->a_tcnp
;
4604 struct componentname
*fcnp
= ap
->a_fcnp
;
4605 int error
, nfsvers
, inuse
= 0, tvprecycle
= 0, locked
= 0;
4606 mount_t fmp
, tdmp
, tmp
;
4607 struct nfs_vattr nvattr
;
4608 struct nfsmount
*nmp
;
4610 fdnp
= VTONFS(fdvp
);
4612 tdnp
= VTONFS(tdvp
);
4613 tnp
= tvp
? VTONFS(tvp
) : NULL
;
4615 nmp
= NFSTONMP(fdnp
);
4616 if (nfs_mount_gone(nmp
)) {
4619 nfsvers
= nmp
->nm_vers
;
4621 error
= nfs_node_set_busy4(fdnp
, fnp
, tdnp
, tnp
, vfs_context_thread(ctx
));
4626 if (tvp
&& (tvp
!= fvp
)) {
4627 /* lock the node while we rename over the existing file */
4628 lck_mtx_lock(nfs_node_hash_mutex
);
4629 while (tnp
->n_hflag
& NHLOCKED
) {
4630 tnp
->n_hflag
|= NHLOCKWANT
;
4631 msleep(tnp
, nfs_node_hash_mutex
, PINOD
, "nfs_rename", NULL
);
4633 tnp
->n_hflag
|= NHLOCKED
;
4634 lck_mtx_unlock(nfs_node_hash_mutex
);
4638 /* Check for cross-device rename */
4639 fmp
= vnode_mount(fvp
);
4640 tmp
= tvp
? vnode_mount(tvp
) : NULL
;
4641 tdmp
= vnode_mount(tdvp
);
4642 if ((fmp
!= tdmp
) || (tvp
&& (fmp
!= tmp
))) {
4647 /* XXX prevent renaming from/over a sillyrenamed file? */
4650 * If the tvp exists and is in use, sillyrename it before doing the
4651 * rename of the new file over it.
4652 * XXX Can't sillyrename a directory.
4653 * Don't sillyrename if source and target are same vnode (hard
4654 * links or case-variants)
4656 if (tvp
&& (tvp
!= fvp
)) {
4657 inuse
= vnode_isinuse(tvp
, 0);
4659 if (inuse
&& !tnp
->n_sillyrename
&& (vnode_vtype(tvp
) != VDIR
)) {
4660 error
= nfs_sillyrename(tdnp
, tnp
, tcnp
, ctx
);
4662 /* sillyrename failed. Instead of pressing on, return error */
4663 goto out
; /* should not be ENOENT. */
4665 /* sillyrename succeeded.*/
4670 else if (tvp
&& (nmp
->nm_vers
>= NFS_VER4
) && (tnp
->n_openflags
& N_DELEG_MASK
)) {
4671 nfs4_delegation_return(tnp
, 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4674 error
= nmp
->nm_funcs
->nf_rename_rpc(fdnp
, fcnp
->cn_nameptr
, fcnp
->cn_namelen
,
4675 tdnp
, tcnp
->cn_nameptr
, tcnp
->cn_namelen
, ctx
);
4678 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
4680 if (error
== ENOENT
) {
4684 if (tvp
&& (tvp
!= fvp
) && !tnp
->n_sillyrename
) {
4685 nfs_node_lock_force(tnp
);
4686 tvprecycle
= (!error
&& !vnode_isinuse(tvp
, 0) &&
4687 (nfs_getattrcache(tnp
, &nvattr
, 0) || (nvattr
.nva_nlink
== 1)));
4688 nfs_node_unlock(tnp
);
4689 lck_mtx_lock(nfs_node_hash_mutex
);
4690 if (tvprecycle
&& (tnp
->n_hflag
& NHHASHED
)) {
4692 * remove nfsnode from hash now so we can't accidentally find it
4693 * again if another object gets created with the same filehandle
4694 * before this vnode gets reclaimed
4696 LIST_REMOVE(tnp
, n_hash
);
4697 tnp
->n_hflag
&= ~NHHASHED
;
4698 FSDBG(266, 0, tnp
, tnp
->n_flag
, 0xb1eb1e);
4700 lck_mtx_unlock(nfs_node_hash_mutex
);
4703 /* purge the old name cache entries and enter the new one */
4704 nfs_name_cache_purge(fdnp
, fnp
, fcnp
, ctx
);
4706 nfs_name_cache_purge(tdnp
, tnp
, tcnp
, ctx
);
4708 /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */
4709 /* clear all flags other than these */
4710 nfs_node_lock_force(tnp
);
4711 tnp
->n_flag
&= (NMODIFIED
);
4712 nfs_node_unlock(tnp
);
4717 nfs_node_lock_force(tdnp
);
4718 if (tdnp
->n_flag
& NNEGNCENTRIES
) {
4719 tdnp
->n_flag
&= ~NNEGNCENTRIES
;
4720 cache_purge_negatives(tdvp
);
4722 nfs_node_unlock(tdnp
);
4723 nfs_node_lock_force(fnp
);
4724 cache_enter(tdvp
, fvp
, tcnp
);
4725 if (tdvp
!= fdvp
) { /* update parent pointer */
4726 if (fnp
->n_parent
&& !vnode_get(fnp
->n_parent
)) {
4727 /* remove ref from old parent */
4728 vnode_rele(fnp
->n_parent
);
4729 vnode_put(fnp
->n_parent
);
4731 fnp
->n_parent
= tdvp
;
4732 if (tdvp
&& !vnode_get(tdvp
)) {
4733 /* add ref to new parent */
4737 fnp
->n_parent
= NULL
;
4740 nfs_node_unlock(fnp
);
4743 /* nfs_getattr() will check changed and purge caches */
4744 nfs_getattr(fdnp
, NULL
, ctx
, NGA_CACHED
);
4745 nfs_getattr(tdnp
, NULL
, ctx
, NGA_CACHED
);
4748 lck_mtx_lock(nfs_node_hash_mutex
);
4749 tnp
->n_hflag
&= ~NHLOCKED
;
4750 if (tnp
->n_hflag
& NHLOCKWANT
) {
4751 tnp
->n_hflag
&= ~NHLOCKWANT
;
4754 lck_mtx_unlock(nfs_node_hash_mutex
);
4756 nfs_node_clear_busy4(fdnp
, fnp
, tdnp
, tnp
);
4761 * Do an NFS rename rpc. Called from nfs_vnop_rename() and nfs_sillyrename().
4773 int error
= 0, lockerror
= ENOENT
, status
, fwccpostattr
= 0, twccpostattr
= 0;
4774 struct timespec fpremtime
= { .tv_sec
= 0, .tv_nsec
= 0 }, tpremtime
= { .tv_sec
= 0, .tv_nsec
= 0 };
4775 struct nfsmount
*nmp
;
4777 u_int64_t xid
, txid
;
4778 struct nfsm_chain nmreq
, nmrep
;
4780 nmp
= NFSTONMP(fdnp
);
4781 if (nfs_mount_gone(nmp
)) {
4784 nfsvers
= nmp
->nm_vers
;
4785 if ((nfsvers
== NFS_VER2
) &&
4786 ((fnamelen
> NFS_MAXNAMLEN
) || (tnamelen
> NFS_MAXNAMLEN
))) {
4787 return ENAMETOOLONG
;
4790 nfsm_chain_null(&nmreq
);
4791 nfsm_chain_null(&nmrep
);
4793 nfsm_chain_build_alloc_init(error
, &nmreq
,
4794 (NFSX_FH(nfsvers
) + NFSX_UNSIGNED
) * 2 +
4795 nfsm_rndup(fnamelen
) + nfsm_rndup(tnamelen
));
4796 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, fdnp
->n_fhp
, fdnp
->n_fhsize
);
4797 nfsm_chain_add_name(error
, &nmreq
, fnameptr
, fnamelen
, nmp
);
4798 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, tdnp
->n_fhp
, tdnp
->n_fhsize
);
4799 nfsm_chain_add_name(error
, &nmreq
, tnameptr
, tnamelen
, nmp
);
4800 nfsm_chain_build_done(error
, &nmreq
);
4803 error
= nfs_request(fdnp
, NULL
, &nmreq
, NFSPROC_RENAME
, ctx
, NULL
, &nmrep
, &xid
, &status
);
4805 if ((lockerror
= nfs_node_lock2(fdnp
, tdnp
))) {
4808 if (nfsvers
== NFS_VER3
) {
4810 nfsm_chain_get_wcc_data(error
, &nmrep
, fdnp
, &fpremtime
, &fwccpostattr
, &xid
);
4811 nfsm_chain_get_wcc_data(error
, &nmrep
, tdnp
, &tpremtime
, &twccpostattr
, &txid
);
4817 nfsm_chain_cleanup(&nmreq
);
4818 nfsm_chain_cleanup(&nmrep
);
4820 fdnp
->n_flag
|= NMODIFIED
;
4821 /* if directory hadn't changed, update namecache mtime */
4822 if (nfstimespeccmp(&fdnp
->n_ncmtime
, &fpremtime
, ==)) {
4823 NFS_CHANGED_UPDATE_NC(nfsvers
, fdnp
, &fdnp
->n_vattr
);
4825 if (!fwccpostattr
) {
4826 NATTRINVALIDATE(fdnp
);
4828 tdnp
->n_flag
|= NMODIFIED
;
4829 /* if directory hadn't changed, update namecache mtime */
4830 if (nfstimespeccmp(&tdnp
->n_ncmtime
, &tpremtime
, ==)) {
4831 NFS_CHANGED_UPDATE_NC(nfsvers
, tdnp
, &tdnp
->n_vattr
);
4833 if (!twccpostattr
) {
4834 NATTRINVALIDATE(tdnp
);
4836 nfs_node_unlock2(fdnp
, tdnp
);
4842 * NFS hard link create call
4846 struct vnop_link_args
/* {
4847 * struct vnodeop_desc *a_desc;
4850 * struct componentname *a_cnp;
4851 * vfs_context_t a_context;
4854 vfs_context_t ctx
= ap
->a_context
;
4855 vnode_t vp
= ap
->a_vp
;
4856 vnode_t tdvp
= ap
->a_tdvp
;
4857 struct componentname
*cnp
= ap
->a_cnp
;
4858 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0, attrflag
= 0;
4859 struct timespec premtime
= { .tv_sec
= 0, .tv_nsec
= 0 };
4860 struct nfsmount
*nmp
;
4861 nfsnode_t np
= VTONFS(vp
);
4862 nfsnode_t tdnp
= VTONFS(tdvp
);
4864 u_int64_t xid
, txid
;
4865 struct nfsm_chain nmreq
, nmrep
;
4867 if (vnode_mount(vp
) != vnode_mount(tdvp
)) {
4872 if (nfs_mount_gone(nmp
)) {
4875 nfsvers
= nmp
->nm_vers
;
4876 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
)) {
4877 return ENAMETOOLONG
;
4881 * Push all writes to the server, so that the attribute cache
4882 * doesn't get "out of sync" with the server.
4883 * XXX There should be a better way!
4885 nfs_flush(np
, MNT_WAIT
, vfs_context_thread(ctx
), V_IGNORE_WRITEERR
);
4887 error
= nfs_node_set_busy2(tdnp
, np
, vfs_context_thread(ctx
));
4892 nfsm_chain_null(&nmreq
);
4893 nfsm_chain_null(&nmrep
);
4895 nfsm_chain_build_alloc_init(error
, &nmreq
,
4896 NFSX_FH(nfsvers
) * 2 + NFSX_UNSIGNED
+ nfsm_rndup(cnp
->cn_namelen
));
4897 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
4898 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, tdnp
->n_fhp
, tdnp
->n_fhsize
);
4899 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4900 nfsm_chain_build_done(error
, &nmreq
);
4902 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_LINK
, ctx
, NULL
, &nmrep
, &xid
, &status
);
4904 if ((lockerror
= nfs_node_lock2(tdnp
, np
))) {
4908 if (nfsvers
== NFS_VER3
) {
4910 nfsm_chain_postop_attr_update_flag(error
, &nmrep
, np
, attrflag
, &xid
);
4911 nfsm_chain_get_wcc_data(error
, &nmrep
, tdnp
, &premtime
, &wccpostattr
, &txid
);
4917 nfsm_chain_cleanup(&nmreq
);
4918 nfsm_chain_cleanup(&nmrep
);
4921 NATTRINVALIDATE(np
);
4923 tdnp
->n_flag
|= NMODIFIED
;
4924 /* if directory hadn't changed, update namecache mtime */
4925 if (nfstimespeccmp(&tdnp
->n_ncmtime
, &premtime
, ==)) {
4926 NFS_CHANGED_UPDATE_NC(nfsvers
, tdnp
, &tdnp
->n_vattr
);
4929 NATTRINVALIDATE(tdnp
);
4931 if (!error
&& (tdnp
->n_flag
& NNEGNCENTRIES
)) {
4932 tdnp
->n_flag
&= ~NNEGNCENTRIES
;
4933 cache_purge_negatives(tdvp
);
4935 nfs_node_unlock2(tdnp
, np
);
4937 nfs_node_clear_busy2(tdnp
, np
);
4939 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
4941 if (error
== EEXIST
) {
4948 * NFS symbolic link create call
4952 struct vnop_symlink_args
/* {
4953 * struct vnodeop_desc *a_desc;
4956 * struct componentname *a_cnp;
4957 * struct vnode_attr *a_vap;
4959 * vfs_context_t a_context;
4962 vfs_context_t ctx
= ap
->a_context
;
4963 vnode_t dvp
= ap
->a_dvp
;
4964 struct vnode_attr
*vap
= ap
->a_vap
;
4965 struct componentname
*cnp
= ap
->a_cnp
;
4966 struct nfs_vattr nvattr
;
4968 int slen
, error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0;
4969 struct timespec premtime
= { .tv_sec
= 0, .tv_nsec
= 0 };
4970 vnode_t newvp
= NULL
;
4971 int nfsvers
, gotuid
, gotgid
;
4972 u_int64_t xid
= 0, dxid
;
4973 nfsnode_t np
= NULL
;
4974 nfsnode_t dnp
= VTONFS(dvp
);
4975 struct nfsmount
*nmp
;
4976 struct nfsm_chain nmreq
, nmrep
;
4977 struct nfsreq rq
, *req
= &rq
;
4978 struct nfs_dulookup dul
;
4980 int dul_in_progress
= 0;
4983 if (nfs_mount_gone(nmp
)) {
4986 nfsvers
= nmp
->nm_vers
;
4987 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
4989 slen
= strlen(ap
->a_target
);
4990 if ((nfsvers
== NFS_VER2
) &&
4991 ((cnp
->cn_namelen
> NFS_MAXNAMLEN
) || (slen
> NFS_MAXPATHLEN
))) {
4992 return ENAMETOOLONG
;
4995 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
4997 VATTR_SET_SUPPORTED(vap
, va_mode
);
4998 VATTR_SET_SUPPORTED(vap
, va_uid
);
4999 VATTR_SET_SUPPORTED(vap
, va_gid
);
5000 VATTR_SET_SUPPORTED(vap
, va_data_size
);
5001 VATTR_SET_SUPPORTED(vap
, va_access_time
);
5002 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
5003 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
5004 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
5006 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
5008 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
5011 nfsm_chain_null(&nmreq
);
5012 nfsm_chain_null(&nmrep
);
5014 nfsm_chain_build_alloc_init(error
, &nmreq
,
5015 NFSX_FH(nfsvers
) + 2 * NFSX_UNSIGNED
+
5016 nfsm_rndup(cnp
->cn_namelen
) + nfsm_rndup(slen
) + NFSX_SATTR(nfsvers
));
5017 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
5018 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
5019 if (nfsvers
== NFS_VER3
) {
5020 nfsm_chain_add_v3sattr(nmp
, error
, &nmreq
, vap
);
5022 nfsm_chain_add_name(error
, &nmreq
, ap
->a_target
, slen
, nmp
);
5023 if (nfsvers
== NFS_VER2
) {
5024 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, -1);
5026 nfsm_chain_build_done(error
, &nmreq
);
5029 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_SYMLINK
,
5030 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
5033 nfs_dulookup_start(&dul
, dnp
, ctx
);
5034 dul_in_progress
= 1;
5036 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
5039 if ((lockerror
= nfs_node_lock(dnp
))) {
5043 if (!error
&& !status
) {
5044 if (dnp
->n_flag
& NNEGNCENTRIES
) {
5045 dnp
->n_flag
&= ~NNEGNCENTRIES
;
5046 cache_purge_negatives(dvp
);
5048 if (nfsvers
== NFS_VER3
) {
5049 error
= nfsm_chain_get_fh_attr(nmp
, &nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
5054 if (nfsvers
== NFS_VER3
) {
5055 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
5061 nfsm_chain_cleanup(&nmreq
);
5062 nfsm_chain_cleanup(&nmrep
);
5065 dnp
->n_flag
|= NMODIFIED
;
5066 /* if directory hadn't changed, update namecache mtime */
5067 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==)) {
5068 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
5070 nfs_node_unlock(dnp
);
5071 /* nfs_getattr() will check changed and purge caches */
5072 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
5075 if (!error
&& fh
.fh_len
) {
5076 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
5082 if (dul_in_progress
) {
5083 nfs_dulookup_finish(&dul
, dnp
, ctx
);
5087 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
5088 * if we can succeed in looking up the symlink.
5090 if ((error
== EEXIST
) || (!error
&& !newvp
)) {
5092 nfs_node_unlock(np
);
5096 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
5099 if (vnode_vtype(newvp
) != VLNK
) {
5105 nfs_node_clear_busy(dnp
);
5107 if (!error
&& (gotuid
|| gotgid
) &&
5108 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
5109 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
5110 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
5111 /* clear ID bits if server didn't use them (or we can't tell) */
5112 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
5113 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
5117 nfs_node_unlock(np
);
5121 nfs_node_unlock(np
);
5132 struct vnop_mkdir_args
/* {
5133 * struct vnodeop_desc *a_desc;
5136 * struct componentname *a_cnp;
5137 * struct vnode_attr *a_vap;
5138 * vfs_context_t a_context;
5141 vfs_context_t ctx
= ap
->a_context
;
5142 vnode_t dvp
= ap
->a_dvp
;
5143 struct vnode_attr
*vap
= ap
->a_vap
;
5144 struct componentname
*cnp
= ap
->a_cnp
;
5145 struct nfs_vattr nvattr
;
5146 nfsnode_t np
= NULL
;
5147 struct nfsmount
*nmp
;
5148 nfsnode_t dnp
= VTONFS(dvp
);
5149 vnode_t newvp
= NULL
;
5150 int error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0;
5151 struct timespec premtime
= { .tv_sec
= 0, .tv_nsec
= 0 };
5152 int nfsvers
, gotuid
, gotgid
;
5153 u_int64_t xid
= 0, dxid
;
5155 struct nfsm_chain nmreq
, nmrep
;
5156 struct nfsreq rq
, *req
= &rq
;
5157 struct nfs_dulookup dul
;
5159 int dul_in_progress
= 0;
5162 if (nfs_mount_gone(nmp
)) {
5165 nfsvers
= nmp
->nm_vers
;
5166 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
5168 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
)) {
5169 return ENAMETOOLONG
;
5172 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
5174 VATTR_SET_SUPPORTED(vap
, va_mode
);
5175 VATTR_SET_SUPPORTED(vap
, va_uid
);
5176 VATTR_SET_SUPPORTED(vap
, va_gid
);
5177 VATTR_SET_SUPPORTED(vap
, va_data_size
);
5178 VATTR_SET_SUPPORTED(vap
, va_access_time
);
5179 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
5180 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
5181 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
5183 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
5185 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
5188 nfsm_chain_null(&nmreq
);
5189 nfsm_chain_null(&nmrep
);
5191 nfsm_chain_build_alloc_init(error
, &nmreq
,
5192 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+
5193 nfsm_rndup(cnp
->cn_namelen
) + NFSX_SATTR(nfsvers
));
5194 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
5195 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
5196 if (nfsvers
== NFS_VER3
) {
5197 nfsm_chain_add_v3sattr(nmp
, error
, &nmreq
, vap
);
5199 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, -1);
5201 nfsm_chain_build_done(error
, &nmreq
);
5204 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_MKDIR
,
5205 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
5208 nfs_dulookup_start(&dul
, dnp
, ctx
);
5209 dul_in_progress
= 1;
5211 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
5214 if ((lockerror
= nfs_node_lock(dnp
))) {
5218 if (!error
&& !status
) {
5219 if (dnp
->n_flag
& NNEGNCENTRIES
) {
5220 dnp
->n_flag
&= ~NNEGNCENTRIES
;
5221 cache_purge_negatives(dvp
);
5223 error
= nfsm_chain_get_fh_attr(nmp
, &nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
5225 if (nfsvers
== NFS_VER3
) {
5226 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
5232 nfsm_chain_cleanup(&nmreq
);
5233 nfsm_chain_cleanup(&nmrep
);
5236 dnp
->n_flag
|= NMODIFIED
;
5237 /* if directory hadn't changed, update namecache mtime */
5238 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==)) {
5239 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
5241 nfs_node_unlock(dnp
);
5242 /* nfs_getattr() will check changed and purge caches */
5243 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
5246 if (!error
&& fh
.fh_len
) {
5247 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
5253 if (dul_in_progress
) {
5254 nfs_dulookup_finish(&dul
, dnp
, ctx
);
5258 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
5259 * if we can succeed in looking up the directory.
5261 if ((error
== EEXIST
) || (!error
&& !newvp
)) {
5263 nfs_node_unlock(np
);
5267 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
5270 if (vnode_vtype(newvp
) != VDIR
) {
5276 nfs_node_clear_busy(dnp
);
5278 if (!error
&& (gotuid
|| gotgid
) &&
5279 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
5280 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
5281 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
5282 /* clear ID bits if server didn't use them (or we can't tell) */
5283 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
5284 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
5288 nfs_node_unlock(np
);
5292 nfs_node_unlock(np
);
5299 * NFS remove directory call
5303 struct vnop_rmdir_args
/* {
5304 * struct vnodeop_desc *a_desc;
5307 * struct componentname *a_cnp;
5308 * vfs_context_t a_context;
5311 vfs_context_t ctx
= ap
->a_context
;
5312 vnode_t vp
= ap
->a_vp
;
5313 vnode_t dvp
= ap
->a_dvp
;
5314 struct componentname
*cnp
= ap
->a_cnp
;
5315 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0;
5316 struct timespec premtime
= { .tv_sec
= 0, .tv_nsec
= 0 };
5317 struct nfsmount
*nmp
;
5318 nfsnode_t np
= VTONFS(vp
);
5319 nfsnode_t dnp
= VTONFS(dvp
);
5322 struct nfsm_chain nmreq
, nmrep
;
5323 struct nfsreq rq
, *req
= &rq
;
5324 struct nfs_dulookup dul
;
5326 int dul_in_progress
= 0;
5329 if (nfs_mount_gone(nmp
)) {
5332 nfsvers
= nmp
->nm_vers
;
5333 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
5335 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
)) {
5336 return ENAMETOOLONG
;
5339 if ((error
= nfs_node_set_busy2(dnp
, np
, vfs_context_thread(ctx
)))) {
5344 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
5347 nfsm_chain_null(&nmreq
);
5348 nfsm_chain_null(&nmrep
);
5350 nfsm_chain_build_alloc_init(error
, &nmreq
,
5351 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+ nfsm_rndup(cnp
->cn_namelen
));
5352 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
5353 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
5354 nfsm_chain_build_done(error
, &nmreq
);
5357 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_RMDIR
,
5358 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
5361 nfs_dulookup_start(&dul
, dnp
, ctx
);
5362 dul_in_progress
= 1;
5364 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
5367 if ((lockerror
= nfs_node_lock(dnp
))) {
5370 if (nfsvers
== NFS_VER3
) {
5371 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &xid
);
5377 nfsm_chain_cleanup(&nmreq
);
5378 nfsm_chain_cleanup(&nmrep
);
5381 dnp
->n_flag
|= NMODIFIED
;
5382 /* if directory hadn't changed, update namecache mtime */
5383 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==)) {
5384 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
5386 nfs_node_unlock(dnp
);
5387 nfs_name_cache_purge(dnp
, np
, cnp
, ctx
);
5388 /* nfs_getattr() will check changed and purge caches */
5389 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
5391 if (dul_in_progress
) {
5392 nfs_dulookup_finish(&dul
, dnp
, ctx
);
5394 nfs_node_clear_busy2(dnp
, np
);
5397 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
5399 if (error
== ENOENT
) {
5404 * remove nfsnode from hash now so we can't accidentally find it
5405 * again if another object gets created with the same filehandle
5406 * before this vnode gets reclaimed
5408 lck_mtx_lock(nfs_node_hash_mutex
);
5409 if (np
->n_hflag
& NHHASHED
) {
5410 LIST_REMOVE(np
, n_hash
);
5411 np
->n_hflag
&= ~NHHASHED
;
5412 FSDBG(266, 0, np
, np
->n_flag
, 0xb1eb1e);
5414 lck_mtx_unlock(nfs_node_hash_mutex
);
5422 * The incoming "offset" is a directory cookie indicating where in the
5423 * directory entries should be read from. A zero cookie means start at
5424 * the beginning of the directory. Any other cookie will be a cookie
5425 * returned from the server.
5427 * Using that cookie, determine which buffer (and where in that buffer)
5428 * to start returning entries from. Buffer logical block numbers are
5429 * the cookies they start at. If a buffer is found that is not full,
5430 * call into the bio/RPC code to fill it. The RPC code will probably
5431 * fill several buffers (dropping the first, requiring a re-get).
5433 * When done copying entries to the buffer, set the offset to the current
5434 * entry's cookie and enter that cookie in the cookie cache.
5436 * Note: because the getdirentries(2) API returns a long-typed offset,
5437 * the incoming offset is a potentially truncated cookie (ptc).
5438 * The cookie matching code is aware of this and will fall back to
5439 * matching only 32 bits of the cookie.
5443 struct vnop_readdir_args
/* {
5444 * struct vnodeop_desc *a_desc;
5446 * struct uio *a_uio;
5450 * vfs_context_t a_context;
5453 vfs_context_t ctx
= ap
->a_context
;
5454 vnode_t dvp
= ap
->a_vp
;
5455 nfsnode_t dnp
= VTONFS(dvp
);
5456 struct nfsmount
*nmp
;
5457 uio_t uio
= ap
->a_uio
;
5458 int error
, nfsvers
, extended
, numdirent
, bigcookies
, ptc
, done
;
5459 uint16_t i
, iptc
, rlen
, nlen
;
5460 uint64_t cookie
, nextcookie
, lbn
= 0;
5461 struct nfsbuf
*bp
= NULL
;
5462 struct nfs_dir_buf_header
*ndbhp
;
5463 struct direntry
*dp
, *dpptc
;
5469 if (nfs_mount_gone(nmp
)) {
5472 nfsvers
= nmp
->nm_vers
;
5473 bigcookies
= (nmp
->nm_state
& NFSSTA_BIGCOOKIES
);
5474 extended
= (ap
->a_flags
& VNODE_READDIR_EXTENDED
);
5476 if (vnode_vtype(dvp
) != VDIR
) {
5480 if (ap
->a_eofflag
) {
5484 if (uio_resid(uio
) == 0) {
5488 if ((nfsvers
>= NFS_VER4
) && (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER
)) {
5489 /* trigger directories should never be read, return nothing */
5493 thd
= vfs_context_thread(ctx
);
5494 numdirent
= done
= 0;
5495 nextcookie
= uio_offset(uio
);
5496 ptc
= bigcookies
&& NFS_DIR_COOKIE_POTENTIALLY_TRUNCATED(nextcookie
);
5498 if ((error
= nfs_node_lock(dnp
))) {
5502 if (dnp
->n_flag
& NNEEDINVALIDATE
) {
5503 dnp
->n_flag
&= ~NNEEDINVALIDATE
;
5505 nfs_node_unlock(dnp
);
5506 error
= nfs_vinvalbuf(dvp
, 0, ctx
, 1);
5508 error
= nfs_node_lock(dnp
);
5516 * check for need to invalidate when (re)starting at beginning
5519 if (dnp
->n_flag
& NMODIFIED
) {
5521 nfs_node_unlock(dnp
);
5522 if ((error
= nfs_vinvalbuf(dvp
, 0, ctx
, 1))) {
5526 nfs_node_unlock(dnp
);
5528 /* nfs_getattr() will check changed and purge caches */
5529 if ((error
= nfs_getattr(dnp
, NULL
, ctx
, NGA_UNCACHED
))) {
5533 nfs_node_unlock(dnp
);
5536 error
= nfs_dir_cookie_to_lbn(dnp
, nextcookie
, &ptc
, &lbn
);
5538 if (error
< 0) { /* just hit EOF cookie */
5542 if (ap
->a_eofflag
) {
5547 while (!error
&& !done
) {
5548 OSAddAtomic64(1, &nfsstats
.biocache_readdirs
);
5549 cookie
= nextcookie
;
5551 error
= nfs_buf_get(dnp
, lbn
, NFS_DIRBLKSIZ
, thd
, NBLK_READ
, &bp
);
5555 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5556 if (!ISSET(bp
->nb_flags
, NB_CACHE
) || !ISSET(ndbhp
->ndbh_flags
, NDB_FULL
)) {
5557 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) { /* initialize the buffer */
5558 ndbhp
->ndbh_flags
= 0;
5559 ndbhp
->ndbh_count
= 0;
5560 ndbhp
->ndbh_entry_end
= sizeof(*ndbhp
);
5561 ndbhp
->ndbh_ncgen
= dnp
->n_ncgen
;
5563 error
= nfs_buf_readdir(bp
, ctx
);
5564 if (error
== NFSERR_DIRBUFDROPPED
) {
5568 nfs_buf_release(bp
, 1);
5570 if (error
&& (error
!= ENXIO
) && (error
!= ETIMEDOUT
) && (error
!= EINTR
) && (error
!= ERESTART
)) {
5571 if (!nfs_node_lock(dnp
)) {
5573 nfs_node_unlock(dnp
);
5575 nfs_vinvalbuf(dvp
, 0, ctx
, 1);
5576 if (error
== NFSERR_BAD_COOKIE
) {
5585 /* find next entry to return */
5586 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5588 if ((lbn
!= cookie
) && !(ptc
&& NFS_DIR_COOKIE_SAME32(lbn
, cookie
))) {
5591 for (; (i
< ndbhp
->ndbh_count
) && (cookie
!= dp
->d_seekoff
); i
++) {
5592 if (ptc
&& !dpptc
&& NFS_DIR_COOKIE_SAME32(cookie
, dp
->d_seekoff
)) {
5596 nextcookie
= dp
->d_seekoff
;
5597 dp
= NFS_DIRENTRY_NEXT(dp
);
5599 if ((i
== ndbhp
->ndbh_count
) && dpptc
) {
5603 if (i
< ndbhp
->ndbh_count
) {
5604 nextcookie
= dp
->d_seekoff
;
5605 dp
= NFS_DIRENTRY_NEXT(dp
);
5609 ptc
= 0; /* only have to deal with ptc on first cookie */
5611 /* return as many entries as we can */
5612 for (; i
< ndbhp
->ndbh_count
; i
++) {
5614 rlen
= dp
->d_reclen
;
5619 bzero(cp
, sizeof(dent
));
5621 if (dp
->d_namlen
> (sizeof(dent
.d_name
) - 1)) {
5622 nlen
= sizeof(dent
.d_name
) - 1;
5624 nlen
= dp
->d_namlen
;
5626 rlen
= NFS_DIRENT_LEN(nlen
);
5627 dent
.d_reclen
= rlen
;
5628 dent
.d_ino
= dp
->d_ino
;
5629 dent
.d_type
= dp
->d_type
;
5630 dent
.d_namlen
= nlen
;
5631 strlcpy(dent
.d_name
, dp
->d_name
, nlen
+ 1);
5633 /* check that the record fits */
5634 if (rlen
> uio_resid(uio
)) {
5638 if ((error
= uiomove(cp
, rlen
, uio
))) {
5642 nextcookie
= dp
->d_seekoff
;
5643 dp
= NFS_DIRENTRY_NEXT(dp
);
5646 if (i
== ndbhp
->ndbh_count
) {
5647 /* hit end of buffer, move to next buffer */
5649 /* if we also hit EOF, we're done */
5650 if (ISSET(ndbhp
->ndbh_flags
, NDB_EOF
)) {
5652 if (ap
->a_eofflag
) {
5658 uio_setoffset(uio
, nextcookie
);
5660 if (!error
&& !done
&& (nextcookie
== cookie
)) {
5661 printf("nfs readdir cookie didn't change 0x%llx, %d/%d\n", cookie
, i
, ndbhp
->ndbh_count
);
5664 nfs_buf_release(bp
, 1);
5668 nfs_dir_cookie_cache(dnp
, nextcookie
, lbn
);
5671 if (ap
->a_numdirent
) {
5672 *ap
->a_numdirent
= numdirent
;
5680 * Invalidate cached directory information, except for the actual directory
5681 * blocks (which are invalidated separately).
5684 nfs_invaldir(nfsnode_t dnp
)
5686 if (vnode_vtype(NFSTOV(dnp
)) != VDIR
) {
5689 dnp
->n_eofcookie
= 0;
5690 dnp
->n_cookieverf
= 0;
5691 if (!dnp
->n_cookiecache
) {
5694 dnp
->n_cookiecache
->free
= 0;
5695 dnp
->n_cookiecache
->mru
= -1;
5696 memset(dnp
->n_cookiecache
->next
, -1, NFSNUMCOOKIES
);
5700 * calculate how much space is available for additional directory entries.
5703 nfs_dir_buf_freespace(struct nfsbuf
*bp
, int rdirplus
)
5705 struct nfs_dir_buf_header
*ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5711 space
= bp
->nb_bufsize
- ndbhp
->ndbh_entry_end
;
5713 space
-= ndbhp
->ndbh_count
* sizeof(struct nfs_vattr
);
5719 * add/update a cookie->lbn entry in the directory cookie cache
5722 nfs_dir_cookie_cache(nfsnode_t dnp
, uint64_t cookie
, uint64_t lbn
)
5724 struct nfsdmap
*ndcc
;
5731 if (nfs_node_lock(dnp
)) {
5735 if (cookie
== dnp
->n_eofcookie
) { /* EOF cookie */
5736 nfs_node_unlock(dnp
);
5740 ndcc
= dnp
->n_cookiecache
;
5742 /* allocate the cookie cache structure */
5743 MALLOC_ZONE(dnp
->n_cookiecache
, struct nfsdmap
*,
5744 sizeof(struct nfsdmap
), M_NFSDIROFF
, M_WAITOK
);
5745 if (!dnp
->n_cookiecache
) {
5746 nfs_node_unlock(dnp
);
5749 ndcc
= dnp
->n_cookiecache
;
5752 memset(ndcc
->next
, -1, NFSNUMCOOKIES
);
5756 * Search the list for this cookie.
5757 * Keep track of previous and last entries.
5761 while ((i
!= -1) && (cookie
!= ndcc
->cookies
[i
].key
)) {
5762 if (ndcc
->next
[i
] == -1) { /* stop on last entry so we can reuse */
5768 if ((i
!= -1) && (cookie
== ndcc
->cookies
[i
].key
)) {
5769 /* found it, remove from list */
5771 ndcc
->next
[prev
] = ndcc
->next
[i
];
5773 ndcc
->mru
= ndcc
->next
[i
];
5776 /* not found, use next free entry or reuse last entry */
5777 if (ndcc
->free
!= NFSNUMCOOKIES
) {
5780 ndcc
->next
[prev
] = -1;
5782 ndcc
->cookies
[i
].key
= cookie
;
5783 ndcc
->cookies
[i
].lbn
= lbn
;
5785 /* insert cookie at head of MRU list */
5786 ndcc
->next
[i
] = ndcc
->mru
;
5788 nfs_node_unlock(dnp
);
5792 * Try to map the given directory cookie to a directory buffer (return lbn).
5793 * If we have a possibly truncated cookie (ptc), check for 32-bit matches too.
5796 nfs_dir_cookie_to_lbn(nfsnode_t dnp
, uint64_t cookie
, int *ptc
, uint64_t *lbnp
)
5798 struct nfsdmap
*ndcc
= dnp
->n_cookiecache
;
5799 int8_t eofptc
, found
;
5801 struct nfsmount
*nmp
;
5802 struct nfsbuf
*bp
, *lastbp
;
5803 struct nfsbuflists blist
;
5804 struct direntry
*dp
, *dpptc
;
5805 struct nfs_dir_buf_header
*ndbhp
;
5807 if (!cookie
) { /* initial cookie */
5813 if (nfs_node_lock(dnp
)) {
5817 if (cookie
== dnp
->n_eofcookie
) { /* EOF cookie */
5818 nfs_node_unlock(dnp
);
5819 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5823 /* note if cookie is a 32-bit match with the EOF cookie */
5824 eofptc
= *ptc
? NFS_DIR_COOKIE_SAME32(cookie
, dnp
->n_eofcookie
) : 0;
5827 /* search the list for the cookie */
5828 for (i
= ndcc
? ndcc
->mru
: -1; i
>= 0; i
= ndcc
->next
[i
]) {
5829 if (ndcc
->cookies
[i
].key
== cookie
) {
5830 /* found a match for this cookie */
5831 *lbnp
= ndcc
->cookies
[i
].lbn
;
5832 nfs_node_unlock(dnp
);
5833 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5837 /* check for 32-bit match */
5838 if (*ptc
&& (iptc
== -1) && NFS_DIR_COOKIE_SAME32(ndcc
->cookies
[i
].key
, cookie
)) {
5842 /* exact match not found */
5844 /* but 32-bit match hit the EOF cookie */
5845 nfs_node_unlock(dnp
);
5846 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5850 /* but 32-bit match got a hit */
5851 *lbnp
= ndcc
->cookies
[iptc
].lbn
;
5852 nfs_node_unlock(dnp
);
5853 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5856 nfs_node_unlock(dnp
);
5859 * No match found in the cookie cache... hmm...
5860 * Let's search the directory's buffers for the cookie.
5862 nmp
= NFSTONMP(dnp
);
5863 if (nfs_mount_gone(nmp
)) {
5869 lck_mtx_lock(nfs_buf_mutex
);
5871 * Scan the list of buffers, keeping them in order.
5872 * Note that itercomplete inserts each of the remaining buffers
5873 * into the head of list (thus reversing the elements). So, we
5874 * make sure to iterate through all buffers, inserting them after
5875 * each other, to keep them in order.
5876 * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because
5877 * we don't drop nfs_buf_mutex.
5879 if (!nfs_buf_iterprepare(dnp
, &blist
, NBI_CLEAN
)) {
5881 while ((bp
= LIST_FIRST(&blist
))) {
5882 LIST_REMOVE(bp
, nb_vnbufs
);
5884 LIST_INSERT_HEAD(&dnp
->n_cleanblkhd
, bp
, nb_vnbufs
);
5886 LIST_INSERT_AFTER(lastbp
, bp
, nb_vnbufs
);
5893 if (nfs_buf_acquire(bp
, NBAC_NOWAIT
, 0, 0)) {
5894 /* just skip this buffer */
5895 nfs_buf_refrele(bp
);
5898 nfs_buf_refrele(bp
);
5900 /* scan the buffer for the cookie */
5901 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5902 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5904 for (i
= 0; (i
< ndbhp
->ndbh_count
) && (cookie
!= dp
->d_seekoff
); i
++) {
5905 if (*ptc
&& !dpptc
&& NFS_DIR_COOKIE_SAME32(cookie
, dp
->d_seekoff
)) {
5909 dp
= NFS_DIRENTRY_NEXT(dp
);
5911 if ((i
== ndbhp
->ndbh_count
) && dpptc
) {
5912 /* found only a PTC match */
5915 } else if (i
< ndbhp
->ndbh_count
) {
5918 if (i
< (ndbhp
->ndbh_count
- 1)) {
5919 /* next entry is *in* this buffer: return this block */
5920 *lbnp
= bp
->nb_lblkno
;
5922 } else if (i
== (ndbhp
->ndbh_count
- 1)) {
5923 /* next entry refers to *next* buffer: return next block */
5924 *lbnp
= dp
->d_seekoff
;
5929 nfs_buf_itercomplete(dnp
, &blist
, NBI_CLEAN
);
5931 lck_mtx_unlock(nfs_buf_mutex
);
5933 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5937 /* still not found... oh well, just start a new block */
5939 OSAddAtomic64(1, &nfsstats
.direofcache_misses
);
5944 * scan a directory buffer for the given name
5945 * Returns: ESRCH if not found, ENOENT if found invalid, 0 if found
5946 * Note: should only be called with RDIRPLUS directory buffers
5949 #define NDBS_PURGE 1
5950 #define NDBS_UPDATE 2
5955 struct componentname
*cnp
,
5957 struct nfs_vattr
*nvap
,
5960 daddr64_t
*nextlbnp
,
5963 struct direntry
*dp
;
5964 struct nfs_dir_buf_header
*ndbhp
;
5965 struct nfs_vattr
*nvattrp
;
5966 daddr64_t nextlbn
= 0;
5967 int i
, error
= ESRCH
;
5970 /* scan the buffer for the name */
5971 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5972 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5973 for (i
= 0; i
< ndbhp
->ndbh_count
; i
++) {
5974 nextlbn
= dp
->d_seekoff
;
5975 if ((cnp
->cn_namelen
== dp
->d_namlen
) && !strcmp(cnp
->cn_nameptr
, dp
->d_name
)) {
5976 fhlen
= dp
->d_name
[dp
->d_namlen
+ 1];
5977 nvattrp
= NFS_DIR_BUF_NVATTR(bp
, i
);
5978 if ((ndbhp
->ndbh_ncgen
!= bp
->nb_np
->n_ncgen
) || (fhlen
== 0) ||
5979 (nvattrp
->nva_type
== VNON
) || (nvattrp
->nva_fileid
== 0)) {
5980 /* entry is not valid */
5984 if (flags
== NDBS_PURGE
) {
5986 bzero(nvattrp
, sizeof(*nvattrp
));
5990 if (flags
== NDBS_UPDATE
) {
5991 /* update direntry's attrs if fh matches */
5992 if ((fhp
->fh_len
== fhlen
) && !bcmp(&dp
->d_name
[dp
->d_namlen
+ 2], fhp
->fh_data
, fhlen
)) {
5993 bcopy(nvap
, nvattrp
, sizeof(*nvap
));
5994 dp
->d_fileno
= nvattrp
->nva_fileid
;
5995 nvattrp
->nva_fileid
= *xidp
;
5996 *(time_t*)(&dp
->d_name
[dp
->d_namlen
+ 2 + fhp
->fh_len
]) = *attrstampp
;
6001 /* copy out fh, attrs, attrstamp, and xid */
6002 fhp
->fh_len
= fhlen
;
6003 bcopy(&dp
->d_name
[dp
->d_namlen
+ 2], fhp
->fh_data
, MAX(fhp
->fh_len
, (int)sizeof(fhp
->fh_data
)));
6004 *attrstampp
= *(time_t*)(&dp
->d_name
[dp
->d_namlen
+ 2 + fhp
->fh_len
]);
6005 bcopy(nvattrp
, nvap
, sizeof(*nvap
));
6006 *xidp
= nvap
->nva_fileid
;
6007 nvap
->nva_fileid
= dp
->d_fileno
;
6011 dp
= NFS_DIRENTRY_NEXT(dp
);
6014 *nextlbnp
= nextlbn
;
6020 * Look up a name in a directory's buffers.
6021 * Note: should only be called with RDIRPLUS directory buffers
6024 nfs_dir_buf_cache_lookup(nfsnode_t dnp
, nfsnode_t
*npp
, struct componentname
*cnp
, vfs_context_t ctx
, int purge
)
6027 struct nfsmount
*nmp
;
6028 int error
= 0, i
, found
= 0, count
= 0;
6030 struct nfs_vattr nvattr
;
6032 time_t attrstamp
= 0;
6033 thread_t thd
= vfs_context_thread(ctx
);
6034 struct nfsbuf
*bp
, *lastbp
, *foundbp
;
6035 struct nfsbuflists blist
;
6036 daddr64_t lbn
, nextlbn
;
6037 int dotunder
= (cnp
->cn_namelen
> 2) && (cnp
->cn_nameptr
[0] == '.') && (cnp
->cn_nameptr
[1] == '_');
6039 nmp
= NFSTONMP(dnp
);
6040 if (nfs_mount_gone(nmp
)) {
6047 /* first check most recent buffer (and next one too) */
6048 lbn
= dnp
->n_lastdbl
;
6049 for (i
= 0; i
< 2; i
++) {
6050 if ((error
= nfs_buf_get(dnp
, lbn
, NFS_DIRBLKSIZ
, thd
, NBLK_READ
| NBLK_ONLYVALID
, &bp
))) {
6057 error
= nfs_dir_buf_search(bp
, cnp
, &fh
, &nvattr
, &xid
, &attrstamp
, &nextlbn
, purge
? NDBS_PURGE
: 0);
6058 nfs_buf_release(bp
, 0);
6059 if (error
== ESRCH
) {
6068 lck_mtx_lock(nfs_buf_mutex
);
6070 dnp
->n_lastdbl
= lbn
;
6075 * Scan the list of buffers, keeping them in order.
6076 * Note that itercomplete inserts each of the remaining buffers
6077 * into the head of list (thus reversing the elements). So, we
6078 * make sure to iterate through all buffers, inserting them after
6079 * each other, to keep them in order.
6080 * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because
6081 * we don't drop nfs_buf_mutex.
6083 if (!nfs_buf_iterprepare(dnp
, &blist
, NBI_CLEAN
)) {
6084 lastbp
= foundbp
= NULL
;
6085 while ((bp
= LIST_FIRST(&blist
))) {
6086 LIST_REMOVE(bp
, nb_vnbufs
);
6088 LIST_INSERT_HEAD(&dnp
->n_cleanblkhd
, bp
, nb_vnbufs
);
6090 LIST_INSERT_AFTER(lastbp
, bp
, nb_vnbufs
);
6093 if (error
|| found
) {
6096 if (!purge
&& dotunder
&& (count
> 100)) { /* don't waste too much time looking for ._ files */
6100 lbn
= bp
->nb_lblkno
;
6101 if (nfs_buf_acquire(bp
, NBAC_NOWAIT
, 0, 0)) {
6102 /* just skip this buffer */
6103 nfs_buf_refrele(bp
);
6106 nfs_buf_refrele(bp
);
6108 error
= nfs_dir_buf_search(bp
, cnp
, &fh
, &nvattr
, &xid
, &attrstamp
, NULL
, purge
? NDBS_PURGE
: 0);
6109 if (error
== ESRCH
) {
6118 LIST_REMOVE(foundbp
, nb_vnbufs
);
6119 LIST_INSERT_HEAD(&dnp
->n_cleanblkhd
, foundbp
, nb_vnbufs
);
6120 dnp
->n_lastdbl
= foundbp
->nb_lblkno
;
6122 nfs_buf_itercomplete(dnp
, &blist
, NBI_CLEAN
);
6125 lck_mtx_unlock(nfs_buf_mutex
);
6127 if (!error
&& found
&& !purge
) {
6128 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
,
6129 &nvattr
, &xid
, dnp
->n_auth
, NG_MAKEENTRY
, &newnp
);
6133 newnp
->n_attrstamp
= attrstamp
;
6135 nfs_node_unlock(newnp
);
6136 /* check if the dir buffer's attrs are out of date */
6137 if (!nfs_getattr(newnp
, &nvattr
, ctx
, NGA_CACHED
) &&
6138 (newnp
->n_attrstamp
!= attrstamp
)) {
6139 /* they are, so update them */
6140 error
= nfs_buf_get(dnp
, lbn
, NFS_DIRBLKSIZ
, thd
, NBLK_READ
| NBLK_ONLYVALID
, &bp
);
6142 attrstamp
= newnp
->n_attrstamp
;
6144 nfs_dir_buf_search(bp
, cnp
, &fh
, &nvattr
, &xid
, &attrstamp
, NULL
, NDBS_UPDATE
);
6145 nfs_buf_release(bp
, 0);
6155 * Purge name cache entries for the given node.
6156 * For RDIRPLUS, also invalidate the entry in the directory's buffers.
6159 nfs_name_cache_purge(nfsnode_t dnp
, nfsnode_t np
, struct componentname
*cnp
, vfs_context_t ctx
)
6161 struct nfsmount
*nmp
= NFSTONMP(dnp
);
6163 cache_purge(NFSTOV(np
));
6164 if (nmp
&& (nmp
->nm_vers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
)) {
6165 nfs_dir_buf_cache_lookup(dnp
, NULL
, cnp
, ctx
, 1);
6170 * NFS V3 readdir (plus) RPC.
6173 nfs3_readdir_rpc(nfsnode_t dnp
, struct nfsbuf
*bp
, vfs_context_t ctx
)
6175 struct nfsmount
*nmp
;
6176 int error
= 0, lockerror
, nfsvers
, rdirplus
, bigcookies
;
6177 int i
, status
, attrflag
, fhflag
, more_entries
= 1, eof
, bp_dropped
= 0;
6178 uint32_t nmreaddirsize
, nmrsize
;
6179 uint32_t namlen
, skiplen
, fhlen
, xlen
, attrlen
, reclen
, space_free
, space_needed
;
6180 uint64_t cookie
, lastcookie
, xid
, savedxid
, fileno
;
6181 struct nfsm_chain nmreq
, nmrep
, nmrepsave
;
6183 struct nfs_vattr
*nvattrp
;
6184 struct nfs_dir_buf_header
*ndbhp
;
6185 struct direntry
*dp
;
6186 char *padstart
, padlen
;
6189 nmp
= NFSTONMP(dnp
);
6190 if (nfs_mount_gone(nmp
)) {
6193 nfsvers
= nmp
->nm_vers
;
6194 nmreaddirsize
= nmp
->nm_readdirsize
;
6195 nmrsize
= nmp
->nm_rsize
;
6196 bigcookies
= nmp
->nm_state
& NFSSTA_BIGCOOKIES
;
6198 rdirplus
= ((nfsvers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
)) ? 1 : 0;
6200 if ((lockerror
= nfs_node_lock(dnp
))) {
6204 /* determine cookie to use, and move dp to the right offset */
6205 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
6206 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
6207 if (ndbhp
->ndbh_count
) {
6208 for (i
= 0; i
< ndbhp
->ndbh_count
- 1; i
++) {
6209 dp
= NFS_DIRENTRY_NEXT(dp
);
6211 cookie
= dp
->d_seekoff
;
6212 dp
= NFS_DIRENTRY_NEXT(dp
);
6214 cookie
= bp
->nb_lblkno
;
6215 /* increment with every buffer read */
6216 OSAddAtomic64(1, &nfsstats
.readdir_bios
);
6218 lastcookie
= cookie
;
6221 * Loop around doing readdir(plus) RPCs of size nm_readdirsize until
6222 * the buffer is full (or we hit EOF). Then put the remainder of the
6223 * results in the next buffer(s).
6225 nfsm_chain_null(&nmreq
);
6226 nfsm_chain_null(&nmrep
);
6227 while (nfs_dir_buf_freespace(bp
, rdirplus
) && !(ndbhp
->ndbh_flags
& NDB_FULL
)) {
6228 nfsm_chain_build_alloc_init(error
, &nmreq
,
6229 NFSX_FH(nfsvers
) + NFSX_READDIR(nfsvers
) + NFSX_UNSIGNED
);
6230 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
6231 if (nfsvers
== NFS_VER3
) {
6232 /* opaque values don't need swapping, but as long */
6233 /* as we are consistent about it, it should be ok */
6234 nfsm_chain_add_64(error
, &nmreq
, cookie
);
6235 nfsm_chain_add_64(error
, &nmreq
, dnp
->n_cookieverf
);
6237 nfsm_chain_add_32(error
, &nmreq
, cookie
);
6239 nfsm_chain_add_32(error
, &nmreq
, nmreaddirsize
);
6241 nfsm_chain_add_32(error
, &nmreq
, nmrsize
);
6243 nfsm_chain_build_done(error
, &nmreq
);
6244 nfs_node_unlock(dnp
);
6248 error
= nfs_request(dnp
, NULL
, &nmreq
,
6249 rdirplus
? NFSPROC_READDIRPLUS
: NFSPROC_READDIR
,
6250 ctx
, NULL
, &nmrep
, &xid
, &status
);
6252 if ((lockerror
= nfs_node_lock(dnp
))) {
6257 if (nfsvers
== NFS_VER3
) {
6258 nfsm_chain_postop_attr_update(error
, &nmrep
, dnp
, &xid
);
6263 if (nfsvers
== NFS_VER3
) {
6264 nfsm_chain_get_64(error
, &nmrep
, dnp
->n_cookieverf
);
6266 nfsm_chain_get_32(error
, &nmrep
, more_entries
);
6269 nfs_node_unlock(dnp
);
6272 if (error
== NFSERR_NOTSUPP
) {
6273 /* oops... it doesn't look like readdirplus is supported */
6274 lck_mtx_lock(&nmp
->nm_lock
);
6275 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_RDIRPLUS
);
6276 lck_mtx_unlock(&nmp
->nm_lock
);
6285 /* loop through the entries packing them into the buffer */
6286 while (more_entries
) {
6287 if (nfsvers
== NFS_VER3
) {
6288 nfsm_chain_get_64(error
, &nmrep
, fileno
);
6290 nfsm_chain_get_32(error
, &nmrep
, fileno
);
6292 nfsm_chain_get_32(error
, &nmrep
, namlen
);
6294 /* just truncate names that don't fit in direntry.d_name */
6299 if (namlen
> (sizeof(dp
->d_name
) - 1)) {
6300 skiplen
= namlen
- sizeof(dp
->d_name
) + 1;
6301 namlen
= sizeof(dp
->d_name
) - 1;
6305 /* guess that fh size will be same as parent */
6306 fhlen
= rdirplus
? (1 + dnp
->n_fhsize
) : 0;
6307 xlen
= rdirplus
? (fhlen
+ sizeof(time_t)) : 0;
6308 attrlen
= rdirplus
? sizeof(struct nfs_vattr
) : 0;
6309 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
6310 space_needed
= reclen
+ attrlen
;
6311 space_free
= nfs_dir_buf_freespace(bp
, rdirplus
);
6312 if (space_needed
> space_free
) {
6314 * We still have entries to pack, but we've
6315 * run out of room in the current buffer.
6316 * So we need to move to the next buffer.
6317 * The block# for the next buffer is the
6318 * last cookie in the current buffer.
6321 ndbhp
->ndbh_flags
|= NDB_FULL
;
6322 nfs_buf_release(bp
, 0);
6325 error
= nfs_buf_get(dnp
, lastcookie
, NFS_DIRBLKSIZ
, vfs_context_thread(ctx
), NBLK_READ
, &bp
);
6327 /* initialize buffer */
6328 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
6329 ndbhp
->ndbh_flags
= 0;
6330 ndbhp
->ndbh_count
= 0;
6331 ndbhp
->ndbh_entry_end
= sizeof(*ndbhp
);
6332 ndbhp
->ndbh_ncgen
= dnp
->n_ncgen
;
6333 space_free
= nfs_dir_buf_freespace(bp
, rdirplus
);
6334 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
6335 /* increment with every buffer read */
6336 OSAddAtomic64(1, &nfsstats
.readdir_bios
);
6339 dp
->d_fileno
= fileno
;
6340 dp
->d_namlen
= namlen
;
6341 dp
->d_reclen
= reclen
;
6342 dp
->d_type
= DT_UNKNOWN
;
6343 nfsm_chain_get_opaque(error
, &nmrep
, namlen
, dp
->d_name
);
6345 dp
->d_name
[namlen
] = '\0';
6347 nfsm_chain_adv(error
, &nmrep
,
6348 nfsm_rndup(namlen
+ skiplen
) - nfsm_rndup(namlen
));
6350 if (nfsvers
== NFS_VER3
) {
6351 nfsm_chain_get_64(error
, &nmrep
, cookie
);
6353 nfsm_chain_get_32(error
, &nmrep
, cookie
);
6356 dp
->d_seekoff
= cookie
;
6357 if (!bigcookies
&& (cookie
>> 32) && (nmp
== NFSTONMP(dnp
))) {
6358 /* we've got a big cookie, make sure flag is set */
6359 lck_mtx_lock(&nmp
->nm_lock
);
6360 nmp
->nm_state
|= NFSSTA_BIGCOOKIES
;
6361 lck_mtx_unlock(&nmp
->nm_lock
);
6365 nvattrp
= NFS_DIR_BUF_NVATTR(bp
, ndbhp
->ndbh_count
);
6366 /* check for attributes */
6367 nfsm_chain_get_32(error
, &nmrep
, attrflag
);
6370 /* grab attributes */
6371 error
= nfs_parsefattr(nmp
, &nmrep
, NFS_VER3
, nvattrp
);
6373 dp
->d_type
= IFTODT(VTTOIF(nvattrp
->nva_type
));
6374 /* fileid is already in d_fileno, so stash xid in attrs */
6375 nvattrp
->nva_fileid
= savedxid
;
6377 /* mark the attributes invalid */
6378 bzero(nvattrp
, sizeof(struct nfs_vattr
));
6380 /* check for file handle */
6381 nfsm_chain_get_32(error
, &nmrep
, fhflag
);
6384 nfsm_chain_get_fh(error
, &nmrep
, NFS_VER3
, &fh
);
6386 fhlen
= fh
.fh_len
+ 1;
6387 xlen
= fhlen
+ sizeof(time_t);
6388 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
6389 space_needed
= reclen
+ attrlen
;
6390 if (space_needed
> space_free
) {
6391 /* didn't actually have the room... move on to next buffer */
6395 /* pack the file handle into the record */
6396 dp
->d_name
[dp
->d_namlen
+ 1] = fh
.fh_len
;
6397 bcopy(fh
.fh_data
, &dp
->d_name
[dp
->d_namlen
+ 2], fh
.fh_len
);
6399 /* mark the file handle invalid */
6401 fhlen
= fh
.fh_len
+ 1;
6402 xlen
= fhlen
+ sizeof(time_t);
6403 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
6404 bzero(&dp
->d_name
[dp
->d_namlen
+ 1], fhlen
);
6406 *(time_t*)(&dp
->d_name
[dp
->d_namlen
+ 1 + fhlen
]) = now
.tv_sec
;
6407 dp
->d_reclen
= reclen
;
6409 padstart
= dp
->d_name
+ dp
->d_namlen
+ 1 + xlen
;
6410 ndbhp
->ndbh_count
++;
6411 lastcookie
= cookie
;
6412 /* advance to next direntry in buffer */
6413 dp
= NFS_DIRENTRY_NEXT(dp
);
6414 ndbhp
->ndbh_entry_end
= (char*)dp
- bp
->nb_data
;
6415 /* zero out the pad bytes */
6416 padlen
= (char*)dp
- padstart
;
6418 bzero(padstart
, padlen
);
6420 /* check for more entries */
6421 nfsm_chain_get_32(error
, &nmrep
, more_entries
);
6424 /* Finally, get the eof boolean */
6425 nfsm_chain_get_32(error
, &nmrep
, eof
);
6428 ndbhp
->ndbh_flags
|= (NDB_FULL
| NDB_EOF
);
6429 nfs_node_lock_force(dnp
);
6430 dnp
->n_eofcookie
= lastcookie
;
6431 nfs_node_unlock(dnp
);
6436 nfs_buf_release(bp
, 0);
6440 if ((lockerror
= nfs_node_lock(dnp
))) {
6444 nfsm_chain_cleanup(&nmrep
);
6445 nfsm_chain_null(&nmreq
);
6448 if (bp_dropped
&& bp
) {
6449 nfs_buf_release(bp
, 0);
6452 nfs_node_unlock(dnp
);
6454 nfsm_chain_cleanup(&nmreq
);
6455 nfsm_chain_cleanup(&nmrep
);
6456 return bp_dropped
? NFSERR_DIRBUFDROPPED
: error
;
6460 * Silly rename. To make the NFS filesystem that is stateless look a little
6461 * more like the "ufs" a remove of an active vnode is translated to a rename
6462 * to a funny looking filename that is removed by nfs_vnop_inactive on the
6463 * nfsnode. There is the potential for another process on a different client
6464 * to create the same funny name between when the lookitup() fails and the
6465 * rename() completes, but...
6468 /* format of "random" silly names - includes a number and pid */
6469 /* (note: shouldn't exceed size of nfs_sillyrename.nsr_name) */
6470 #define NFS_SILLYNAME_FORMAT ".nfs.%08x.%04x"
6471 /* starting from zero isn't silly enough */
6472 static uint32_t nfs_sillyrename_number
= 0x20051025;
6478 struct componentname
*cnp
,
6481 struct nfs_sillyrename
*nsp
;
6486 struct nfsmount
*nmp
;
6488 nmp
= NFSTONMP(dnp
);
6489 if (nfs_mount_gone(nmp
)) {
6493 nfs_name_cache_purge(dnp
, np
, cnp
, ctx
);
6495 MALLOC_ZONE(nsp
, struct nfs_sillyrename
*,
6496 sizeof(struct nfs_sillyrename
), M_NFSREQ
, M_WAITOK
);
6500 cred
= vfs_context_ucred(ctx
);
6501 kauth_cred_ref(cred
);
6502 nsp
->nsr_cred
= cred
;
6504 error
= vnode_ref(NFSTOV(dnp
));
6509 /* Fudge together a funny name */
6510 pid
= vfs_context_pid(ctx
);
6511 num
= OSAddAtomic(1, &nfs_sillyrename_number
);
6512 nsp
->nsr_namlen
= snprintf(nsp
->nsr_name
, sizeof(nsp
->nsr_name
),
6513 NFS_SILLYNAME_FORMAT
, num
, (pid
& 0xffff));
6514 if (nsp
->nsr_namlen
>= (int)sizeof(nsp
->nsr_name
)) {
6515 nsp
->nsr_namlen
= sizeof(nsp
->nsr_name
) - 1;
6518 /* Try lookitups until we get one that isn't there */
6519 while (nfs_lookitup(dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, ctx
, NULL
) == 0) {
6520 num
= OSAddAtomic(1, &nfs_sillyrename_number
);
6521 nsp
->nsr_namlen
= snprintf(nsp
->nsr_name
, sizeof(nsp
->nsr_name
),
6522 NFS_SILLYNAME_FORMAT
, num
, (pid
& 0xffff));
6523 if (nsp
->nsr_namlen
>= (int)sizeof(nsp
->nsr_name
)) {
6524 nsp
->nsr_namlen
= sizeof(nsp
->nsr_name
) - 1;
6528 /* now, do the rename */
6529 error
= nmp
->nm_funcs
->nf_rename_rpc(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
,
6530 dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, ctx
);
6532 /* Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. */
6533 if (error
== ENOENT
) {
6537 nfs_node_lock_force(dnp
);
6538 if (dnp
->n_flag
& NNEGNCENTRIES
) {
6539 dnp
->n_flag
&= ~NNEGNCENTRIES
;
6540 cache_purge_negatives(NFSTOV(dnp
));
6542 nfs_node_unlock(dnp
);
6544 FSDBG(267, dnp
, np
, num
, error
);
6548 error
= nfs_lookitup(dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, ctx
, &np
);
6549 nfs_node_lock_force(np
);
6550 np
->n_sillyrename
= nsp
;
6551 nfs_node_unlock(np
);
6554 vnode_rele(NFSTOV(dnp
));
6556 nsp
->nsr_cred
= NOCRED
;
6557 kauth_cred_unref(&cred
);
6558 FREE_ZONE(nsp
, sizeof(*nsp
), M_NFSREQ
);
6563 nfs3_lookup_rpc_async(
6568 struct nfsreq
**reqp
)
6570 struct nfsmount
*nmp
;
6571 struct nfsm_chain nmreq
;
6572 int error
= 0, nfsvers
;
6574 nmp
= NFSTONMP(dnp
);
6575 if (nfs_mount_gone(nmp
)) {
6578 nfsvers
= nmp
->nm_vers
;
6580 nfsm_chain_null(&nmreq
);
6582 nfsm_chain_build_alloc_init(error
, &nmreq
,
6583 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+ nfsm_rndup(namelen
));
6584 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
6585 nfsm_chain_add_name(error
, &nmreq
, name
, namelen
, nmp
);
6586 nfsm_chain_build_done(error
, &nmreq
);
6588 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_LOOKUP
,
6589 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, reqp
);
6591 nfsm_chain_cleanup(&nmreq
);
6596 nfs3_lookup_rpc_async_finish(
6598 __unused
char *name
,
6599 __unused
int namelen
,
6604 struct nfs_vattr
*nvap
)
6606 int error
= 0, lockerror
= ENOENT
, status
, nfsvers
, attrflag
;
6608 struct nfsmount
*nmp
;
6609 struct nfsm_chain nmrep
;
6611 nmp
= NFSTONMP(dnp
);
6615 nfsvers
= nmp
->nm_vers
;
6617 nfsm_chain_null(&nmrep
);
6619 error
= nfs_request_async_finish(req
, &nmrep
, xidp
, &status
);
6621 if ((lockerror
= nfs_node_lock(dnp
))) {
6625 if (error
|| status
) {
6626 if (nfsvers
== NFS_VER3
) {
6627 nfsm_chain_postop_attr_update(error
, &nmrep
, dnp
, &xid
);
6635 nfsmout_if(error
|| !fhp
|| !nvap
);
6637 /* get the file handle */
6638 nfsm_chain_get_fh(error
, &nmrep
, nfsvers
, fhp
);
6640 /* get the attributes */
6641 if (nfsvers
== NFS_VER3
) {
6642 nfsm_chain_postop_attr_get(nmp
, error
, &nmrep
, attrflag
, nvap
);
6643 nfsm_chain_postop_attr_update(error
, &nmrep
, dnp
, &xid
);
6644 if (!error
&& !attrflag
) {
6645 error
= nfs3_getattr_rpc(NULL
, NFSTOMP(dnp
), fhp
->fh_data
, fhp
->fh_len
, 0, ctx
, nvap
, xidp
);
6648 error
= nfs_parsefattr(nmp
, &nmrep
, nfsvers
, nvap
);
6652 nfs_node_unlock(dnp
);
6654 nfsm_chain_cleanup(&nmrep
);
6659 * Look up a file name and optionally either update the file handle or
6660 * allocate an nfsnode, depending on the value of npp.
6661 * npp == NULL --> just do the lookup
6662 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
6664 * *npp != NULL --> update the file handle in the vnode
6675 nfsnode_t np
, newnp
= NULL
;
6678 struct nfsmount
*nmp
;
6679 struct nfs_vattr nvattr
;
6680 struct nfsreq rq
, *req
= &rq
;
6682 nmp
= NFSTONMP(dnp
);
6683 if (nfs_mount_gone(nmp
)) {
6687 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXNAME
) &&
6688 (namelen
> (int)nmp
->nm_fsattr
.nfsa_maxname
)) {
6689 return ENAMETOOLONG
;
6692 NVATTR_INIT(&nvattr
);
6694 /* check for lookup of "." */
6695 if ((name
[0] == '.') && (namelen
== 1)) {
6696 /* skip lookup, we know who we are */
6702 error
= nmp
->nm_funcs
->nf_lookup_rpc_async(dnp
, name
, namelen
, ctx
, &req
);
6704 error
= nmp
->nm_funcs
->nf_lookup_rpc_async_finish(dnp
, name
, namelen
, ctx
, req
, &xid
, &fh
, &nvattr
);
6705 nfsmout_if(!npp
|| error
);
6709 if (fh
.fh_len
!= np
->n_fhsize
) {
6710 u_char
*oldbuf
= (np
->n_fhsize
> NFS_SMALLFH
) ? np
->n_fhp
: NULL
;
6711 if (fh
.fh_len
> NFS_SMALLFH
) {
6712 MALLOC_ZONE(np
->n_fhp
, u_char
*, fh
.fh_len
, M_NFSBIGFH
, M_WAITOK
);
6719 np
->n_fhp
= &np
->n_fh
[0];
6722 FREE_ZONE(oldbuf
, np
->n_fhsize
, M_NFSBIGFH
);
6725 bcopy(fh
.fh_data
, np
->n_fhp
, fh
.fh_len
);
6726 np
->n_fhsize
= fh
.fh_len
;
6727 nfs_node_lock_force(np
);
6728 error
= nfs_loadattrcache(np
, &nvattr
, &xid
, 0);
6729 nfs_node_unlock(np
);
6732 } else if (NFS_CMPFH(dnp
, fh
.fh_data
, fh
.fh_len
)) {
6733 nfs_node_lock_force(dnp
);
6734 if (dnp
->n_xid
<= xid
) {
6735 error
= nfs_loadattrcache(dnp
, &nvattr
, &xid
, 0);
6737 nfs_node_unlock(dnp
);
6741 struct componentname cn
, *cnp
= &cn
;
6742 bzero(cnp
, sizeof(*cnp
));
6743 cnp
->cn_nameptr
= name
;
6744 cnp
->cn_namelen
= namelen
;
6745 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
,
6746 &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
6752 if (npp
&& !*npp
&& !error
) {
6755 NVATTR_CLEANUP(&nvattr
);
6760 * set up and initialize a "._" file lookup structure used for
6761 * performing async lookups.
6764 nfs_dulookup_init(struct nfs_dulookup
*dulp
, nfsnode_t dnp
, const char *name
, int namelen
, vfs_context_t ctx
)
6766 int error
, du_namelen
;
6768 struct nfsmount
*nmp
= NFSTONMP(dnp
);
6770 /* check for ._ file in name cache */
6772 bzero(&dulp
->du_cn
, sizeof(dulp
->du_cn
));
6773 du_namelen
= namelen
+ 2;
6774 if (!nmp
|| NMFLAG(nmp
, NONEGNAMECACHE
)) {
6777 if ((namelen
>= 2) && (name
[0] == '.') && (name
[1] == '_')) {
6780 if (du_namelen
>= (int)sizeof(dulp
->du_smallname
)) {
6781 MALLOC(dulp
->du_cn
.cn_nameptr
, char *, du_namelen
+ 1, M_TEMP
, M_WAITOK
);
6783 dulp
->du_cn
.cn_nameptr
= dulp
->du_smallname
;
6785 if (!dulp
->du_cn
.cn_nameptr
) {
6788 dulp
->du_cn
.cn_namelen
= du_namelen
;
6789 snprintf(dulp
->du_cn
.cn_nameptr
, du_namelen
+ 1, "._%s", name
);
6790 dulp
->du_cn
.cn_nameptr
[du_namelen
] = '\0';
6791 dulp
->du_cn
.cn_nameiop
= LOOKUP
;
6792 dulp
->du_cn
.cn_flags
= MAKEENTRY
;
6794 error
= cache_lookup(NFSTOV(dnp
), &du_vp
, &dulp
->du_cn
);
6797 } else if (!error
) {
6798 nmp
= NFSTONMP(dnp
);
6799 if (nmp
&& (nmp
->nm_vers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
)) {
6800 /* if rdirplus, try dir buf cache lookup */
6801 nfsnode_t du_np
= NULL
;
6802 if (!nfs_dir_buf_cache_lookup(dnp
, &du_np
, &dulp
->du_cn
, ctx
, 0) && du_np
) {
6803 /* dir buf cache hit */
6804 du_vp
= NFSTOV(du_np
);
6810 dulp
->du_flags
|= NFS_DULOOKUP_DOIT
;
6816 * start an async "._" file lookup request
6819 nfs_dulookup_start(struct nfs_dulookup
*dulp
, nfsnode_t dnp
, vfs_context_t ctx
)
6821 struct nfsmount
*nmp
= NFSTONMP(dnp
);
6822 struct nfsreq
*req
= &dulp
->du_req
;
6824 if (!nmp
|| !(dulp
->du_flags
& NFS_DULOOKUP_DOIT
) || (dulp
->du_flags
& NFS_DULOOKUP_INPROG
)) {
6827 if (!nmp
->nm_funcs
->nf_lookup_rpc_async(dnp
, dulp
->du_cn
.cn_nameptr
,
6828 dulp
->du_cn
.cn_namelen
, ctx
, &req
)) {
6829 dulp
->du_flags
|= NFS_DULOOKUP_INPROG
;
6834 * finish an async "._" file lookup request and clean up the structure
6837 nfs_dulookup_finish(struct nfs_dulookup
*dulp
, nfsnode_t dnp
, vfs_context_t ctx
)
6839 struct nfsmount
*nmp
= NFSTONMP(dnp
);
6844 struct nfs_vattr nvattr
;
6846 if (!nmp
|| !(dulp
->du_flags
& NFS_DULOOKUP_INPROG
)) {
6850 NVATTR_INIT(&nvattr
);
6851 error
= nmp
->nm_funcs
->nf_lookup_rpc_async_finish(dnp
, dulp
->du_cn
.cn_nameptr
,
6852 dulp
->du_cn
.cn_namelen
, ctx
, &dulp
->du_req
, &xid
, &fh
, &nvattr
);
6853 dulp
->du_flags
&= ~NFS_DULOOKUP_INPROG
;
6854 if (error
== ENOENT
) {
6855 /* add a negative entry in the name cache */
6856 nfs_node_lock_force(dnp
);
6857 cache_enter(NFSTOV(dnp
), NULL
, &dulp
->du_cn
);
6858 dnp
->n_flag
|= NNEGNCENTRIES
;
6859 nfs_node_unlock(dnp
);
6860 } else if (!error
) {
6861 error
= nfs_nget(NFSTOMP(dnp
), dnp
, &dulp
->du_cn
, fh
.fh_data
, fh
.fh_len
,
6862 &nvattr
, &xid
, dulp
->du_req
.r_auth
, NG_MAKEENTRY
, &du_np
);
6864 nfs_node_unlock(du_np
);
6865 vnode_put(NFSTOV(du_np
));
6868 NVATTR_CLEANUP(&nvattr
);
6870 if (dulp
->du_flags
& NFS_DULOOKUP_INPROG
) {
6871 nfs_request_async_cancel(&dulp
->du_req
);
6873 if (dulp
->du_cn
.cn_nameptr
&& (dulp
->du_cn
.cn_nameptr
!= dulp
->du_smallname
)) {
6874 FREE(dulp
->du_cn
.cn_nameptr
, M_TEMP
);
6880 * NFS Version 3 commit RPC
6890 struct nfsmount
*nmp
;
6891 int error
= 0, lockerror
, status
, wccpostattr
= 0, nfsvers
;
6892 struct timespec premtime
= { .tv_sec
= 0, .tv_nsec
= 0 };
6893 u_int64_t xid
, newwverf
;
6895 struct nfsm_chain nmreq
, nmrep
;
6898 FSDBG(521, np
, offset
, count
, nmp
? nmp
->nm_state
: 0);
6899 if (nfs_mount_gone(nmp
)) {
6902 if (!(nmp
->nm_state
& NFSSTA_HASWRITEVERF
)) {
6905 nfsvers
= nmp
->nm_vers
;
6907 if (count
> UINT32_MAX
) {
6913 nfsm_chain_null(&nmreq
);
6914 nfsm_chain_null(&nmrep
);
6916 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(NFS_VER3
));
6917 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
6918 nfsm_chain_add_64(error
, &nmreq
, offset
);
6919 nfsm_chain_add_32(error
, &nmreq
, count32
);
6920 nfsm_chain_build_done(error
, &nmreq
);
6922 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC_COMMIT
,
6923 current_thread(), cred
, NULL
, 0, &nmrep
, &xid
, &status
);
6924 if ((lockerror
= nfs_node_lock(np
))) {
6927 /* can we do anything useful with the wcc info? */
6928 nfsm_chain_get_wcc_data(error
, &nmrep
, np
, &premtime
, &wccpostattr
, &xid
);
6930 nfs_node_unlock(np
);
6935 nfsm_chain_get_64(error
, &nmrep
, newwverf
);
6937 lck_mtx_lock(&nmp
->nm_lock
);
6938 if (nmp
->nm_verf
!= newwverf
) {
6939 nmp
->nm_verf
= newwverf
;
6941 if (wverf
!= newwverf
) {
6942 error
= NFSERR_STALEWRITEVERF
;
6944 lck_mtx_unlock(&nmp
->nm_lock
);
6946 nfsm_chain_cleanup(&nmreq
);
6947 nfsm_chain_cleanup(&nmrep
);
6954 __unused
struct vnop_blockmap_args
/* {
6955 * struct vnodeop_desc *a_desc;
6970 * fsync vnode op. Just call nfs_flush().
6975 struct vnop_fsync_args
/* {
6976 * struct vnodeop_desc *a_desc;
6979 * vfs_context_t a_context;
6982 return nfs_flush(VTONFS(ap
->a_vp
), ap
->a_waitfor
, vfs_context_thread(ap
->a_context
), 0);
6987 * Do an NFS pathconf RPC.
6992 struct nfs_fsattr
*nfsap
,
6996 int error
= 0, lockerror
, status
, nfsvers
;
6997 struct nfsm_chain nmreq
, nmrep
;
6998 struct nfsmount
*nmp
= NFSTONMP(np
);
7001 if (nfs_mount_gone(nmp
)) {
7004 nfsvers
= nmp
->nm_vers
;
7006 nfsm_chain_null(&nmreq
);
7007 nfsm_chain_null(&nmrep
);
7009 /* fetch pathconf info from server */
7010 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(NFS_VER3
));
7011 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
7012 nfsm_chain_build_done(error
, &nmreq
);
7014 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_PATHCONF
, ctx
, NULL
, &nmrep
, &xid
, &status
);
7015 if ((lockerror
= nfs_node_lock(np
))) {
7018 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
7020 nfs_node_unlock(np
);
7025 nfsm_chain_get_32(error
, &nmrep
, nfsap
->nfsa_maxlink
);
7026 nfsm_chain_get_32(error
, &nmrep
, nfsap
->nfsa_maxname
);
7027 nfsap
->nfsa_flags
&= ~(NFS_FSFLAG_NO_TRUNC
| NFS_FSFLAG_CHOWN_RESTRICTED
| NFS_FSFLAG_CASE_INSENSITIVE
| NFS_FSFLAG_CASE_PRESERVING
);
7028 nfsm_chain_get_32(error
, &nmrep
, val
);
7030 nfsap
->nfsa_flags
|= NFS_FSFLAG_NO_TRUNC
;
7032 nfsm_chain_get_32(error
, &nmrep
, val
);
7034 nfsap
->nfsa_flags
|= NFS_FSFLAG_CHOWN_RESTRICTED
;
7036 nfsm_chain_get_32(error
, &nmrep
, val
);
7038 nfsap
->nfsa_flags
|= NFS_FSFLAG_CASE_INSENSITIVE
;
7040 nfsm_chain_get_32(error
, &nmrep
, val
);
7042 nfsap
->nfsa_flags
|= NFS_FSFLAG_CASE_PRESERVING
;
7044 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXLINK
);
7045 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXNAME
);
7046 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_NO_TRUNC
);
7047 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_CHOWN_RESTRICTED
);
7048 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_INSENSITIVE
);
7049 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_PRESERVING
);
7051 nfsm_chain_cleanup(&nmreq
);
7052 nfsm_chain_cleanup(&nmrep
);
7056 /* save pathconf info for NFSv3 mount */
7058 nfs3_pathconf_cache(struct nfsmount
*nmp
, struct nfs_fsattr
*nfsap
)
7060 nmp
->nm_fsattr
.nfsa_maxlink
= nfsap
->nfsa_maxlink
;
7061 nmp
->nm_fsattr
.nfsa_maxname
= nfsap
->nfsa_maxname
;
7062 nmp
->nm_fsattr
.nfsa_flags
&= ~(NFS_FSFLAG_NO_TRUNC
| NFS_FSFLAG_CHOWN_RESTRICTED
| NFS_FSFLAG_CASE_INSENSITIVE
| NFS_FSFLAG_CASE_PRESERVING
);
7063 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_NO_TRUNC
;
7064 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_CHOWN_RESTRICTED
;
7065 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_INSENSITIVE
;
7066 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_PRESERVING
;
7067 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXLINK
);
7068 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXNAME
);
7069 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_NO_TRUNC
);
7070 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CHOWN_RESTRICTED
);
7071 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CASE_INSENSITIVE
);
7072 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CASE_PRESERVING
);
7073 nmp
->nm_state
|= NFSSTA_GOTPATHCONF
;
7077 * Return POSIX pathconf information applicable to nfs.
7079 * The NFS V2 protocol doesn't support this, so just return EINVAL
7085 struct vnop_pathconf_args
/* {
7086 * struct vnodeop_desc *a_desc;
7089 * int32_t *a_retval;
7090 * vfs_context_t a_context;
7093 vnode_t vp
= ap
->a_vp
;
7094 nfsnode_t np
= VTONFS(vp
);
7095 struct nfsmount
*nmp
;
7096 struct nfs_fsattr nfsa
, *nfsap
;
7098 uint64_t maxFileSize
;
7102 if (nfs_mount_gone(nmp
)) {
7106 switch (ap
->a_name
) {
7109 case _PC_CHOWN_RESTRICTED
:
7111 case _PC_CASE_SENSITIVE
:
7112 case _PC_CASE_PRESERVING
:
7114 case _PC_FILESIZEBITS
:
7115 if (nmp
->nm_vers
== NFS_VER2
) {
7120 case _PC_XATTR_SIZE_BITS
:
7121 /* Do we support xattrs natively? */
7122 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
) {
7125 /* No... so just return an error */
7128 /* don't bother contacting the server if we know the answer */
7132 if (nmp
->nm_vers
== NFS_VER2
) {
7136 lck_mtx_lock(&nmp
->nm_lock
);
7137 if (nmp
->nm_vers
== NFS_VER3
) {
7138 if (!(nmp
->nm_state
& NFSSTA_GOTPATHCONF
)) {
7139 /* no pathconf info cached */
7140 lck_mtx_unlock(&nmp
->nm_lock
);
7141 NFS_CLEAR_ATTRIBUTES(nfsa
.nfsa_bitmap
);
7142 error
= nfs3_pathconf_rpc(np
, &nfsa
, ap
->a_context
);
7147 if (nfs_mount_gone(nmp
)) {
7150 lck_mtx_lock(&nmp
->nm_lock
);
7151 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_HOMOGENEOUS
) {
7152 /* all files have the same pathconf info, */
7153 /* so cache a copy of the results */
7154 nfs3_pathconf_cache(nmp
, &nfsa
);
7158 nfsap
= &nmp
->nm_fsattr
;
7162 else if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_HOMOGENEOUS
)) {
7163 /* no pathconf info cached */
7164 lck_mtx_unlock(&nmp
->nm_lock
);
7165 NFS_CLEAR_ATTRIBUTES(nfsa
.nfsa_bitmap
);
7166 error
= nfs4_pathconf_rpc(np
, &nfsa
, ap
->a_context
);
7171 if (nfs_mount_gone(nmp
)) {
7174 lck_mtx_lock(&nmp
->nm_lock
);
7179 nfsap
= &nmp
->nm_fsattr
;
7181 switch (ap
->a_name
) {
7183 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXLINK
)) {
7184 *ap
->a_retval
= nfsap
->nfsa_maxlink
;
7186 } else if ((nmp
->nm_vers
== NFS_VER4
) && NFS_BITMAP_ISSET(np
->n_vattr
.nva_bitmap
, NFS_FATTR_MAXLINK
)) {
7187 *ap
->a_retval
= np
->n_vattr
.nva_maxlink
;
7194 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXNAME
)) {
7195 *ap
->a_retval
= nfsap
->nfsa_maxname
;
7200 case _PC_CHOWN_RESTRICTED
:
7201 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_CHOWN_RESTRICTED
)) {
7202 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_CHOWN_RESTRICTED
) ? 200112 /* _POSIX_CHOWN_RESTRICTED */ : 0;
7208 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_NO_TRUNC
)) {
7209 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_NO_TRUNC
) ? 200112 /* _POSIX_NO_TRUNC */ : 0;
7214 case _PC_CASE_SENSITIVE
:
7215 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_INSENSITIVE
)) {
7216 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_INSENSITIVE
) ? 0 : 1;
7221 case _PC_CASE_PRESERVING
:
7222 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_PRESERVING
)) {
7223 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_PRESERVING
) ? 1 : 0;
7228 case _PC_XATTR_SIZE_BITS
: /* same as file size bits if named attrs supported */
7229 case _PC_FILESIZEBITS
:
7230 if (!NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXFILESIZE
)) {
7235 maxFileSize
= nfsap
->nfsa_maxfilesize
;
7237 if (maxFileSize
& 0xffffffff00000000ULL
) {
7241 if (maxFileSize
& 0xffff0000) {
7245 if (maxFileSize
& 0xff00) {
7249 if (maxFileSize
& 0xf0) {
7253 if (maxFileSize
& 0xc) {
7257 if (maxFileSize
& 0x2) {
7260 *ap
->a_retval
= nbits
;
7266 lck_mtx_unlock(&nmp
->nm_lock
);
7272 * Read wrapper for special devices.
7276 struct vnop_read_args
/* {
7277 * struct vnodeop_desc *a_desc;
7279 * struct uio *a_uio;
7281 * vfs_context_t a_context;
7284 nfsnode_t np
= VTONFS(ap
->a_vp
);
7285 struct timespec now
;
7291 if ((error
= nfs_node_lock(np
))) {
7296 np
->n_atim
.tv_sec
= now
.tv_sec
;
7297 np
->n_atim
.tv_nsec
= now
.tv_nsec
;
7298 nfs_node_unlock(np
);
7299 return VOCALL(spec_vnodeop_p
, VOFFSET(vnop_read
), ap
);
7303 * Write wrapper for special devices.
7307 struct vnop_write_args
/* {
7308 * struct vnodeop_desc *a_desc;
7310 * struct uio *a_uio;
7312 * vfs_context_t a_context;
7315 nfsnode_t np
= VTONFS(ap
->a_vp
);
7316 struct timespec now
;
7322 if ((error
= nfs_node_lock(np
))) {
7327 np
->n_mtim
.tv_sec
= now
.tv_sec
;
7328 np
->n_mtim
.tv_nsec
= now
.tv_nsec
;
7329 nfs_node_unlock(np
);
7330 return VOCALL(spec_vnodeop_p
, VOFFSET(vnop_write
), ap
);
7334 * Close wrapper for special devices.
7336 * Update the times on the nfsnode then do device close.
7340 struct vnop_close_args
/* {
7341 * struct vnodeop_desc *a_desc;
7344 * vfs_context_t a_context;
7347 vnode_t vp
= ap
->a_vp
;
7348 nfsnode_t np
= VTONFS(vp
);
7349 struct vnode_attr vattr
;
7353 if ((error
= nfs_node_lock(np
))) {
7356 if (np
->n_flag
& (NACC
| NUPD
)) {
7358 if (!vnode_isinuse(vp
, 0) && (mp
= vnode_mount(vp
)) && !vfs_isrdonly(mp
)) {
7360 if (np
->n_flag
& NACC
) {
7361 vattr
.va_access_time
= np
->n_atim
;
7362 VATTR_SET_ACTIVE(&vattr
, va_access_time
);
7364 if (np
->n_flag
& NUPD
) {
7365 vattr
.va_modify_time
= np
->n_mtim
;
7366 VATTR_SET_ACTIVE(&vattr
, va_modify_time
);
7368 nfs_node_unlock(np
);
7369 vnode_setattr(vp
, &vattr
, ap
->a_context
);
7371 nfs_node_unlock(np
);
7374 nfs_node_unlock(np
);
7376 return VOCALL(spec_vnodeop_p
, VOFFSET(vnop_close
), ap
);
7380 extern vnop_t
**fifo_vnodeop_p
;
7383 * Read wrapper for fifos.
7387 struct vnop_read_args
/* {
7388 * struct vnodeop_desc *a_desc;
7390 * struct uio *a_uio;
7392 * vfs_context_t a_context;
7395 nfsnode_t np
= VTONFS(ap
->a_vp
);
7396 struct timespec now
;
7402 if ((error
= nfs_node_lock(np
))) {
7407 np
->n_atim
.tv_sec
= now
.tv_sec
;
7408 np
->n_atim
.tv_nsec
= now
.tv_nsec
;
7409 nfs_node_unlock(np
);
7410 return VOCALL(fifo_vnodeop_p
, VOFFSET(vnop_read
), ap
);
7414 * Write wrapper for fifos.
7418 struct vnop_write_args
/* {
7419 * struct vnodeop_desc *a_desc;
7421 * struct uio *a_uio;
7423 * vfs_context_t a_context;
7426 nfsnode_t np
= VTONFS(ap
->a_vp
);
7427 struct timespec now
;
7433 if ((error
= nfs_node_lock(np
))) {
7438 np
->n_mtim
.tv_sec
= now
.tv_sec
;
7439 np
->n_mtim
.tv_nsec
= now
.tv_nsec
;
7440 nfs_node_unlock(np
);
7441 return VOCALL(fifo_vnodeop_p
, VOFFSET(vnop_write
), ap
);
7445 * Close wrapper for fifos.
7447 * Update the times on the nfsnode then do fifo close.
7451 struct vnop_close_args
/* {
7452 * struct vnodeop_desc *a_desc;
7455 * vfs_context_t a_context;
7458 vnode_t vp
= ap
->a_vp
;
7459 nfsnode_t np
= VTONFS(vp
);
7460 struct vnode_attr vattr
;
7461 struct timespec now
;
7465 if ((error
= nfs_node_lock(np
))) {
7468 if (np
->n_flag
& (NACC
| NUPD
)) {
7470 if (np
->n_flag
& NACC
) {
7471 np
->n_atim
.tv_sec
= now
.tv_sec
;
7472 np
->n_atim
.tv_nsec
= now
.tv_nsec
;
7474 if (np
->n_flag
& NUPD
) {
7475 np
->n_mtim
.tv_sec
= now
.tv_sec
;
7476 np
->n_mtim
.tv_nsec
= now
.tv_nsec
;
7479 if (!vnode_isinuse(vp
, 1) && (mp
= vnode_mount(vp
)) && !vfs_isrdonly(mp
)) {
7481 if (np
->n_flag
& NACC
) {
7482 vattr
.va_access_time
= np
->n_atim
;
7483 VATTR_SET_ACTIVE(&vattr
, va_access_time
);
7485 if (np
->n_flag
& NUPD
) {
7486 vattr
.va_modify_time
= np
->n_mtim
;
7487 VATTR_SET_ACTIVE(&vattr
, va_modify_time
);
7489 nfs_node_unlock(np
);
7490 vnode_setattr(vp
, &vattr
, ap
->a_context
);
7492 nfs_node_unlock(np
);
7495 nfs_node_unlock(np
);
7497 return VOCALL(fifo_vnodeop_p
, VOFFSET(vnop_close
), ap
);
7504 struct vnop_ioctl_args
/* {
7505 * struct vnodeop_desc *a_desc;
7507 * u_int32_t a_command;
7510 * vfs_context_t a_context;
7513 vfs_context_t ctx
= ap
->a_context
;
7514 vnode_t vp
= ap
->a_vp
;
7515 struct nfsmount
*mp
= VTONMP(vp
);
7518 struct user_nfs_gss_principal gprinc
= {};
7525 switch (ap
->a_command
) {
7527 if (vnode_vfsisrdonly(vp
)) {
7530 error
= nfs_flush(VTONFS(vp
), MNT_WAIT
, vfs_context_thread(ctx
), 0);
7533 case NFS_IOC_DESTROY_CRED
:
7534 if (!auth_is_kerberized(mp
->nm_auth
)) {
7537 error
= nfs_gss_clnt_ctx_remove(mp
, vfs_context_ucred(ctx
));
7539 case NFS_IOC_SET_CRED
:
7540 case NFS_IOC_SET_CRED64
:
7541 if (!auth_is_kerberized(mp
->nm_auth
)) {
7544 if ((ap
->a_command
== NFS_IOC_SET_CRED
&& vfs_context_is64bit(ctx
)) ||
7545 (ap
->a_command
== NFS_IOC_SET_CRED64
&& !vfs_context_is64bit(ctx
))) {
7548 if (vfs_context_is64bit(ctx
)) {
7549 gprinc
= *(struct user_nfs_gss_principal
*)ap
->a_data
;
7551 struct nfs_gss_principal
*tp
;
7552 tp
= (struct nfs_gss_principal
*)ap
->a_data
;
7553 gprinc
.princlen
= tp
->princlen
;
7554 gprinc
.nametype
= tp
->nametype
;
7555 gprinc
.principal
= CAST_USER_ADDR_T(tp
->principal
);
7557 NFS_DBG(NFS_FAC_GSS
, 7, "Enter NFS_FSCTL_SET_CRED (64-bit=%d): principal length %d name type %d usr pointer 0x%llx\n", vfs_context_is64bit(ctx
), gprinc
.princlen
, gprinc
.nametype
, (unsigned long long)gprinc
.principal
);
7558 if (gprinc
.princlen
> MAXPATHLEN
) {
7562 MALLOC(p
, uint8_t *, gprinc
.princlen
+ 1, M_TEMP
, M_WAITOK
| M_ZERO
);
7566 error
= copyin(gprinc
.principal
, p
, gprinc
.princlen
);
7568 NFS_DBG(NFS_FAC_GSS
, 7, "NFS_FSCTL_SET_CRED could not copy in princiapl data of len %d: %d\n",
7569 gprinc
.princlen
, error
);
7573 NFS_DBG(NFS_FAC_GSS
, 7, "Seting credential to principal %s\n", p
);
7574 error
= nfs_gss_clnt_ctx_set_principal(mp
, ctx
, p
, gprinc
.princlen
, gprinc
.nametype
);
7575 NFS_DBG(NFS_FAC_GSS
, 7, "Seting credential to principal %s returned %d\n", p
, error
);
7578 case NFS_IOC_GET_CRED
:
7579 case NFS_IOC_GET_CRED64
:
7580 if (!auth_is_kerberized(mp
->nm_auth
)) {
7583 if ((ap
->a_command
== NFS_IOC_GET_CRED
&& vfs_context_is64bit(ctx
)) ||
7584 (ap
->a_command
== NFS_IOC_GET_CRED64
&& !vfs_context_is64bit(ctx
))) {
7587 error
= nfs_gss_clnt_ctx_get_principal(mp
, ctx
, &gprinc
);
7591 if (vfs_context_is64bit(ctx
)) {
7592 struct user_nfs_gss_principal
*upp
= (struct user_nfs_gss_principal
*)ap
->a_data
;
7593 len
= upp
->princlen
;
7594 if (gprinc
.princlen
< len
) {
7595 len
= gprinc
.princlen
;
7597 upp
->princlen
= gprinc
.princlen
;
7598 upp
->nametype
= gprinc
.nametype
;
7599 upp
->flags
= gprinc
.flags
;
7600 if (gprinc
.principal
) {
7601 error
= copyout((void *)gprinc
.principal
, upp
->principal
, len
);
7603 upp
->principal
= USER_ADDR_NULL
;
7606 struct nfs_gss_principal
*u32pp
= (struct nfs_gss_principal
*)ap
->a_data
;
7607 len
= u32pp
->princlen
;
7608 if (gprinc
.princlen
< len
) {
7609 len
= gprinc
.princlen
;
7611 u32pp
->princlen
= gprinc
.princlen
;
7612 u32pp
->nametype
= gprinc
.nametype
;
7613 u32pp
->flags
= gprinc
.flags
;
7614 if (gprinc
.principal
) {
7615 error
= copyout((void *)gprinc
.principal
, u32pp
->principal
, len
);
7617 u32pp
->principal
= (user32_addr_t
)0;
7621 NFS_DBG(NFS_FAC_GSS
, 7, "NFS_FSCTL_GET_CRED could not copy out princiapl data of len %d: %d\n",
7622 gprinc
.princlen
, error
);
7624 if (gprinc
.principal
) {
7625 FREE(gprinc
.principal
, M_TEMP
);
7627 #endif /* CONFIG_NFS_GSS */
7636 __unused
struct vnop_select_args
/* {
7637 * struct vnodeop_desc *a_desc;
7642 * vfs_context_t a_context;
7646 * We were once bogusly seltrue() which returns 1. Is this right?
7652 * vnode OP for pagein using UPL
7654 * No buffer I/O, just RPCs straight into the mapped pages.
7658 struct vnop_pagein_args
/* {
7659 * struct vnodeop_desc *a_desc;
7662 * vm_offset_t a_pl_offset;
7666 * vfs_context_t a_context;
7669 vnode_t vp
= ap
->a_vp
;
7670 upl_t pl
= ap
->a_pl
;
7671 size_t size
= ap
->a_size
;
7672 off_t f_offset
= ap
->a_f_offset
;
7673 vm_offset_t pl_offset
= ap
->a_pl_offset
;
7674 int flags
= ap
->a_flags
;
7677 nfsnode_t np
= VTONFS(vp
);
7678 size_t nmrsize
, iosize
, txsize
, rxsize
, retsize
;
7680 struct nfsmount
*nmp
;
7682 vm_offset_t ioaddr
, rxaddr
;
7684 char uio_buf
[UIO_SIZEOF(1)];
7685 int nofreeupl
= flags
& UPL_NOCOMMIT
;
7686 upl_page_info_t
*plinfo
;
7687 #define MAXPAGINGREQS 16 /* max outstanding RPCs for pagein/pageout */
7688 struct nfsreq
*req
[MAXPAGINGREQS
];
7689 int nextsend
, nextwait
;
7691 uint32_t stategenid
= 0;
7693 uint32_t restart
= 0;
7696 FSDBG(322, np
, f_offset
, size
, flags
);
7697 if (pl
== (upl_t
)NULL
) {
7698 panic("nfs_pagein: no upl");
7702 printf("nfs_pagein: invalid size %ld", size
);
7704 (void) ubc_upl_abort_range(pl
, pl_offset
, size
, 0);
7708 if (f_offset
< 0 || f_offset
>= (off_t
)np
->n_size
|| (f_offset
& PAGE_MASK_64
)) {
7710 ubc_upl_abort_range(pl
, pl_offset
, size
,
7711 UPL_ABORT_ERROR
| UPL_ABORT_FREE_ON_EMPTY
);
7716 thd
= vfs_context_thread(ap
->a_context
);
7717 cred
= ubc_getcred(vp
);
7718 if (!IS_VALID_CRED(cred
)) {
7719 cred
= vfs_context_ucred(ap
->a_context
);
7722 uio
= uio_createwithbuffer(1, f_offset
, UIO_SYSSPACE
, UIO_READ
,
7723 &uio_buf
, sizeof(uio_buf
));
7726 if (nfs_mount_gone(nmp
)) {
7728 ubc_upl_abort_range(pl
, pl_offset
, size
,
7729 UPL_ABORT_ERROR
| UPL_ABORT_FREE_ON_EMPTY
);
7733 nmrsize
= nmp
->nm_rsize
;
7735 plinfo
= ubc_upl_pageinfo(pl
);
7736 kret
= ubc_upl_map(pl
, &ioaddr
);
7737 if (kret
!= KERN_SUCCESS
) {
7738 panic("nfs_vnop_pagein: ubc_upl_map() failed with (%d)", kret
);
7740 ioaddr
+= pl_offset
;
7744 if (nmp
->nm_vers
>= NFS_VER4
) {
7745 stategenid
= nmp
->nm_stategenid
;
7748 txsize
= rxsize
= size
;
7749 txoffset
= f_offset
;
7752 bzero(req
, sizeof(req
));
7753 nextsend
= nextwait
= 0;
7755 if (np
->n_flag
& NREVOKE
) {
7759 /* send requests while we need to and have available slots */
7760 while ((txsize
> 0) && (req
[nextsend
] == NULL
)) {
7761 iosize
= MIN(nmrsize
, txsize
);
7762 if ((error
= nmp
->nm_funcs
->nf_read_rpc_async(np
, txoffset
, iosize
, thd
, cred
, NULL
, &req
[nextsend
]))) {
7763 req
[nextsend
] = NULL
;
7768 nextsend
= (nextsend
+ 1) % MAXPAGINGREQS
;
7770 /* wait while we need to and break out if more requests to send */
7771 while ((rxsize
> 0) && req
[nextwait
]) {
7772 iosize
= retsize
= MIN(nmrsize
, rxsize
);
7773 uio_reset(uio
, uio_offset(uio
), UIO_SYSSPACE
, UIO_READ
);
7774 uio_addiov(uio
, CAST_USER_ADDR_T(rxaddr
), iosize
);
7775 FSDBG(322, uio_offset(uio
), uio_resid(uio
), rxaddr
, rxsize
);
7777 upl_ubc_alias_set(pl
, (uintptr_t) current_thread(), (uintptr_t) 2);
7778 #endif /* UPL_DEBUG */
7779 OSAddAtomic64(1, &nfsstats
.pageins
);
7780 error
= nmp
->nm_funcs
->nf_read_rpc_async_finish(np
, req
[nextwait
], uio
, &retsize
, NULL
);
7781 req
[nextwait
] = NULL
;
7782 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
7784 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
)) {
7785 lck_mtx_lock(&nmp
->nm_lock
);
7786 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
7787 NP(np
, "nfs_vnop_pagein: error %d, initiating recovery", error
);
7788 nfs_need_recover(nmp
, error
);
7790 lck_mtx_unlock(&nmp
->nm_lock
);
7796 FSDBG(322, uio_offset(uio
), uio_resid(uio
), error
, -1);
7799 if (retsize
< iosize
) {
7800 /* Just zero fill the rest of the valid area. */
7801 int zcnt
= iosize
- retsize
;
7802 bzero((char *)rxaddr
+ retsize
, zcnt
);
7803 FSDBG(324, uio_offset(uio
), retsize
, zcnt
, rxaddr
);
7804 uio_update(uio
, zcnt
);
7812 } while (!error
&& (txsize
|| rxsize
));
7820 /* cancel any outstanding requests */
7821 while (req
[nextwait
]) {
7822 nfs_request_async_cancel(req
[nextwait
]);
7823 req
[nextwait
] = NULL
;
7824 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
7826 if (np
->n_flag
& NREVOKE
) {
7828 } else if (restart
) {
7829 if (restart
<= nfs_mount_state_max_restarts(nmp
)) { /* guard against no progress */
7830 if (error
== NFSERR_GRACE
) {
7831 tsleep(&nmp
->nm_state
, (PZERO
- 1), "nfsgrace", 2 * hz
);
7833 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
))) {
7837 NP(np
, "nfs_pagein: too many restarts, aborting");
7846 ubc_upl_abort_range(pl
, pl_offset
, size
,
7848 UPL_ABORT_FREE_ON_EMPTY
);
7850 ubc_upl_commit_range(pl
, pl_offset
, size
,
7851 UPL_COMMIT_CLEAR_DIRTY
|
7852 UPL_COMMIT_FREE_ON_EMPTY
);
7860 * the following are needed only by nfs_pageout to know how to handle errors
7861 * see nfs_pageout comments on explanation of actions.
7862 * the errors here are copied from errno.h and errors returned by servers
7863 * are expected to match the same numbers here. If not, our actions maybe
7866 char nfs_pageouterrorhandler(int);
7867 enum actiontype
{NOACTION
, DUMP
, DUMPANDLOG
, RETRY
, SEVER
};
7868 #define NFS_ELAST 88
7869 static u_char errorcount
[NFS_ELAST
+ 1]; /* better be zeros when initialized */
7870 static const char errortooutcome
[NFS_ELAST
+ 1] = {
7872 DUMP
, /* EPERM 1 Operation not permitted */
7873 DUMP
, /* ENOENT 2 No such file or directory */
7874 DUMPANDLOG
, /* ESRCH 3 No such process */
7875 RETRY
, /* EINTR 4 Interrupted system call */
7876 DUMP
, /* EIO 5 Input/output error */
7877 DUMP
, /* ENXIO 6 Device not configured */
7878 DUMPANDLOG
, /* E2BIG 7 Argument list too long */
7879 DUMPANDLOG
, /* ENOEXEC 8 Exec format error */
7880 DUMPANDLOG
, /* EBADF 9 Bad file descriptor */
7881 DUMPANDLOG
, /* ECHILD 10 No child processes */
7882 DUMPANDLOG
, /* EDEADLK 11 Resource deadlock avoided - was EAGAIN */
7883 RETRY
, /* ENOMEM 12 Cannot allocate memory */
7884 DUMP
, /* EACCES 13 Permission denied */
7885 DUMPANDLOG
, /* EFAULT 14 Bad address */
7886 DUMPANDLOG
, /* ENOTBLK 15 POSIX - Block device required */
7887 RETRY
, /* EBUSY 16 Device busy */
7888 DUMP
, /* EEXIST 17 File exists */
7889 DUMP
, /* EXDEV 18 Cross-device link */
7890 DUMP
, /* ENODEV 19 Operation not supported by device */
7891 DUMP
, /* ENOTDIR 20 Not a directory */
7892 DUMP
, /* EISDIR 21 Is a directory */
7893 DUMP
, /* EINVAL 22 Invalid argument */
7894 DUMPANDLOG
, /* ENFILE 23 Too many open files in system */
7895 DUMPANDLOG
, /* EMFILE 24 Too many open files */
7896 DUMPANDLOG
, /* ENOTTY 25 Inappropriate ioctl for device */
7897 DUMPANDLOG
, /* ETXTBSY 26 Text file busy - POSIX */
7898 DUMP
, /* EFBIG 27 File too large */
7899 DUMP
, /* ENOSPC 28 No space left on device */
7900 DUMPANDLOG
, /* ESPIPE 29 Illegal seek */
7901 DUMP
, /* EROFS 30 Read-only file system */
7902 DUMP
, /* EMLINK 31 Too many links */
7903 RETRY
, /* EPIPE 32 Broken pipe */
7905 DUMPANDLOG
, /* EDOM 33 Numerical argument out of domain */
7906 DUMPANDLOG
, /* ERANGE 34 Result too large */
7907 RETRY
, /* EAGAIN/EWOULDBLOCK 35 Resource temporarily unavailable */
7908 DUMPANDLOG
, /* EINPROGRESS 36 Operation now in progress */
7909 DUMPANDLOG
, /* EALREADY 37 Operation already in progress */
7910 /* ipc/network software -- argument errors */
7911 DUMPANDLOG
, /* ENOTSOC 38 Socket operation on non-socket */
7912 DUMPANDLOG
, /* EDESTADDRREQ 39 Destination address required */
7913 DUMPANDLOG
, /* EMSGSIZE 40 Message too long */
7914 DUMPANDLOG
, /* EPROTOTYPE 41 Protocol wrong type for socket */
7915 DUMPANDLOG
, /* ENOPROTOOPT 42 Protocol not available */
7916 DUMPANDLOG
, /* EPROTONOSUPPORT 43 Protocol not supported */
7917 DUMPANDLOG
, /* ESOCKTNOSUPPORT 44 Socket type not supported */
7918 DUMPANDLOG
, /* ENOTSUP 45 Operation not supported */
7919 DUMPANDLOG
, /* EPFNOSUPPORT 46 Protocol family not supported */
7920 DUMPANDLOG
, /* EAFNOSUPPORT 47 Address family not supported by protocol family */
7921 DUMPANDLOG
, /* EADDRINUSE 48 Address already in use */
7922 DUMPANDLOG
, /* EADDRNOTAVAIL 49 Can't assign requested address */
7923 /* ipc/network software -- operational errors */
7924 RETRY
, /* ENETDOWN 50 Network is down */
7925 RETRY
, /* ENETUNREACH 51 Network is unreachable */
7926 RETRY
, /* ENETRESET 52 Network dropped connection on reset */
7927 RETRY
, /* ECONNABORTED 53 Software caused connection abort */
7928 RETRY
, /* ECONNRESET 54 Connection reset by peer */
7929 RETRY
, /* ENOBUFS 55 No buffer space available */
7930 RETRY
, /* EISCONN 56 Socket is already connected */
7931 RETRY
, /* ENOTCONN 57 Socket is not connected */
7932 RETRY
, /* ESHUTDOWN 58 Can't send after socket shutdown */
7933 RETRY
, /* ETOOMANYREFS 59 Too many references: can't splice */
7934 RETRY
, /* ETIMEDOUT 60 Operation timed out */
7935 RETRY
, /* ECONNREFUSED 61 Connection refused */
7937 DUMPANDLOG
, /* ELOOP 62 Too many levels of symbolic links */
7938 DUMP
, /* ENAMETOOLONG 63 File name too long */
7939 RETRY
, /* EHOSTDOWN 64 Host is down */
7940 RETRY
, /* EHOSTUNREACH 65 No route to host */
7941 DUMP
, /* ENOTEMPTY 66 Directory not empty */
7943 DUMPANDLOG
, /* PROCLIM 67 Too many processes */
7944 DUMPANDLOG
, /* EUSERS 68 Too many users */
7945 DUMPANDLOG
, /* EDQUOT 69 Disc quota exceeded */
7946 /* Network File System */
7947 DUMP
, /* ESTALE 70 Stale NFS file handle */
7948 DUMP
, /* EREMOTE 71 Too many levels of remote in path */
7949 DUMPANDLOG
, /* EBADRPC 72 RPC struct is bad */
7950 DUMPANDLOG
, /* ERPCMISMATCH 73 RPC version wrong */
7951 DUMPANDLOG
, /* EPROGUNAVAIL 74 RPC prog. not avail */
7952 DUMPANDLOG
, /* EPROGMISMATCH 75 Program version wrong */
7953 DUMPANDLOG
, /* EPROCUNAVAIL 76 Bad procedure for program */
7955 DUMPANDLOG
, /* ENOLCK 77 No locks available */
7956 DUMPANDLOG
, /* ENOSYS 78 Function not implemented */
7957 DUMPANDLOG
, /* EFTYPE 79 Inappropriate file type or format */
7958 DUMPANDLOG
, /* EAUTH 80 Authentication error */
7959 DUMPANDLOG
, /* ENEEDAUTH 81 Need authenticator */
7960 /* Intelligent device errors */
7961 DUMPANDLOG
, /* EPWROFF 82 Device power is off */
7962 DUMPANDLOG
, /* EDEVERR 83 Device error, e.g. paper out */
7963 DUMPANDLOG
, /* EOVERFLOW 84 Value too large to be stored in data type */
7964 /* Program loading errors */
7965 DUMPANDLOG
, /* EBADEXEC 85 Bad executable */
7966 DUMPANDLOG
, /* EBADARCH 86 Bad CPU type in executable */
7967 DUMPANDLOG
, /* ESHLIBVERS 87 Shared library version mismatch */
7968 DUMPANDLOG
, /* EBADMACHO 88 Malformed Macho file */
7972 nfs_pageouterrorhandler(int error
)
7974 if (error
> NFS_ELAST
) {
7977 return errortooutcome
[error
];
7983 * vnode OP for pageout using UPL
7985 * No buffer I/O, just RPCs straight from the mapped pages.
7986 * File size changes are not permitted in pageout.
7990 struct vnop_pageout_args
/* {
7991 * struct vnodeop_desc *a_desc;
7994 * vm_offset_t a_pl_offset;
7998 * vfs_context_t a_context;
8001 vnode_t vp
= ap
->a_vp
;
8002 upl_t pl
= ap
->a_pl
;
8003 size_t size
= ap
->a_size
;
8004 off_t f_offset
= ap
->a_f_offset
;
8005 vm_offset_t pl_offset
= ap
->a_pl_offset
;
8006 int flags
= ap
->a_flags
;
8007 nfsnode_t np
= VTONFS(vp
);
8011 struct nfsmount
*nmp
= VTONMP(vp
);
8013 int error
= 0, iomode
;
8014 off_t off
, txoffset
, rxoffset
;
8015 vm_offset_t ioaddr
, txaddr
, rxaddr
;
8017 char uio_buf
[UIO_SIZEOF(1)];
8018 int nofreeupl
= flags
& UPL_NOCOMMIT
;
8019 size_t nmwsize
, biosize
, iosize
, pgsize
, txsize
, rxsize
, xsize
, remsize
;
8020 struct nfsreq
*req
[MAXPAGINGREQS
];
8021 int nextsend
, nextwait
, wverfset
, commit
;
8022 uint64_t wverf
, wverf2
;
8024 uint32_t stategenid
= 0;
8026 uint32_t vrestart
= 0, restart
= 0, vrestarts
= 0, restarts
= 0;
8029 FSDBG(323, f_offset
, size
, pl
, pl_offset
);
8031 if (pl
== (upl_t
)NULL
) {
8032 panic("nfs_pageout: no upl");
8036 printf("nfs_pageout: invalid size %ld", size
);
8038 ubc_upl_abort_range(pl
, pl_offset
, size
, 0);
8045 ubc_upl_abort(pl
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
8049 biosize
= nmp
->nm_biosize
;
8050 nmwsize
= nmp
->nm_wsize
;
8052 nfs_data_lock_noupdate(np
, NFS_DATA_LOCK_SHARED
);
8055 * Check to see whether the buffer is incore.
8056 * If incore and not busy, invalidate it from the cache.
8058 for (iosize
= 0; iosize
< size
; iosize
+= xsize
) {
8059 off
= f_offset
+ iosize
;
8060 /* need make sure we do things on block boundaries */
8061 xsize
= biosize
- (off
% biosize
);
8062 if (off
+ xsize
> f_offset
+ size
) {
8063 xsize
= f_offset
+ size
- off
;
8065 lbn
= (daddr64_t
)(off
/ biosize
);
8066 lck_mtx_lock(nfs_buf_mutex
);
8067 if ((bp
= nfs_buf_incore(np
, lbn
))) {
8068 FSDBG(323, off
, bp
, bp
->nb_lflags
, bp
->nb_flags
);
8069 if (nfs_buf_acquire(bp
, NBAC_NOWAIT
, 0, 0)) {
8070 lck_mtx_unlock(nfs_buf_mutex
);
8071 nfs_data_unlock_noupdate(np
);
8072 /* no panic. just tell vm we are busy */
8074 ubc_upl_abort_range(pl
, pl_offset
, size
, 0);
8078 if (bp
->nb_dirtyend
> 0) {
8080 * if there's a dirty range in the buffer, check
8081 * to see if it extends beyond the pageout region
8083 * if the dirty region lies completely within the
8084 * pageout region, we just invalidate the buffer
8085 * because it's all being written out now anyway.
8087 * if any of the dirty region lies outside the
8088 * pageout region, we'll try to clip the dirty
8089 * region to eliminate the portion that's being
8090 * paged out. If that's not possible, because
8091 * the dirty region extends before and after the
8092 * pageout region, then we'll just return EBUSY.
8094 off_t boff
, start
, end
;
8098 /* clip end to EOF */
8099 if (end
> (off_t
)np
->n_size
) {
8104 if ((bp
->nb_dirtyoff
< start
) &&
8105 (bp
->nb_dirtyend
> end
)) {
8107 * not gonna be able to clip the dirty region
8109 * But before returning the bad news, move the
8110 * buffer to the start of the delwri list and
8111 * give the list a push to try to flush the
8114 FSDBG(323, np
, bp
, 0xd00deebc, EBUSY
);
8115 nfs_buf_remfree(bp
);
8116 TAILQ_INSERT_HEAD(&nfsbufdelwri
, bp
, nb_free
);
8119 nfs_buf_delwri_push(1);
8120 lck_mtx_unlock(nfs_buf_mutex
);
8121 nfs_data_unlock_noupdate(np
);
8123 ubc_upl_abort_range(pl
, pl_offset
, size
, 0);
8127 if ((bp
->nb_dirtyoff
< start
) ||
8128 (bp
->nb_dirtyend
> end
)) {
8129 /* clip dirty region, if necessary */
8130 if (bp
->nb_dirtyoff
< start
) {
8131 bp
->nb_dirtyend
= min(bp
->nb_dirtyend
, start
);
8133 if (bp
->nb_dirtyend
> end
) {
8134 bp
->nb_dirtyoff
= max(bp
->nb_dirtyoff
, end
);
8136 FSDBG(323, bp
, bp
->nb_dirtyoff
, bp
->nb_dirtyend
, 0xd00dee00);
8137 /* we're leaving this block dirty */
8139 lck_mtx_unlock(nfs_buf_mutex
);
8143 nfs_buf_remfree(bp
);
8144 lck_mtx_unlock(nfs_buf_mutex
);
8145 SET(bp
->nb_flags
, NB_INVAL
);
8146 nfs_node_lock_force(np
);
8147 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
8148 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
8149 np
->n_needcommitcnt
--;
8150 CHECK_NEEDCOMMITCNT(np
);
8152 nfs_node_unlock(np
);
8153 nfs_buf_release(bp
, 1);
8155 lck_mtx_unlock(nfs_buf_mutex
);
8159 thd
= vfs_context_thread(ap
->a_context
);
8160 cred
= ubc_getcred(vp
);
8161 if (!IS_VALID_CRED(cred
)) {
8162 cred
= vfs_context_ucred(ap
->a_context
);
8165 nfs_node_lock_force(np
);
8166 if (np
->n_flag
& NWRITEERR
) {
8167 error
= np
->n_error
;
8168 nfs_node_unlock(np
);
8169 nfs_data_unlock_noupdate(np
);
8171 ubc_upl_abort_range(pl
, pl_offset
, size
,
8172 UPL_ABORT_FREE_ON_EMPTY
);
8176 nfs_node_unlock(np
);
8178 if (f_offset
< 0 || f_offset
>= (off_t
)np
->n_size
||
8179 f_offset
& PAGE_MASK_64
|| size
& PAGE_MASK_64
) {
8180 nfs_data_unlock_noupdate(np
);
8182 ubc_upl_abort_range(pl
, pl_offset
, size
,
8183 UPL_ABORT_FREE_ON_EMPTY
);
8188 kret
= ubc_upl_map(pl
, &ioaddr
);
8189 if (kret
!= KERN_SUCCESS
) {
8190 panic("nfs_vnop_pageout: ubc_upl_map() failed with (%d)", kret
);
8192 ioaddr
+= pl_offset
;
8194 if ((u_quad_t
)f_offset
+ size
> np
->n_size
) {
8195 xsize
= np
->n_size
- f_offset
;
8200 pgsize
= round_page_64(xsize
);
8201 if ((size
> pgsize
) && !nofreeupl
) {
8202 ubc_upl_abort_range(pl
, pl_offset
+ pgsize
, size
- pgsize
,
8203 UPL_ABORT_FREE_ON_EMPTY
);
8207 * check for partial page and clear the
8208 * contents past end of the file before
8209 * releasing it in the VM page cache
8211 if ((u_quad_t
)f_offset
< np
->n_size
&& (u_quad_t
)f_offset
+ size
> np
->n_size
) {
8212 size_t io
= np
->n_size
- f_offset
;
8213 bzero((caddr_t
)(ioaddr
+ io
), size
- io
);
8214 FSDBG(321, np
->n_size
, f_offset
, f_offset
+ io
, size
- io
);
8216 nfs_data_unlock_noupdate(np
);
8218 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_WRITE
,
8219 &uio_buf
, sizeof(uio_buf
));
8223 if (nmp
->nm_vers
>= NFS_VER4
) {
8224 stategenid
= nmp
->nm_stategenid
;
8227 wverf
= wverf2
= wverfset
= 0;
8228 txsize
= rxsize
= xsize
;
8229 txoffset
= rxoffset
= f_offset
;
8230 txaddr
= rxaddr
= ioaddr
;
8231 commit
= NFS_WRITE_FILESYNC
;
8233 bzero(req
, sizeof(req
));
8234 nextsend
= nextwait
= 0;
8236 if (np
->n_flag
& NREVOKE
) {
8240 /* send requests while we need to and have available slots */
8241 while ((txsize
> 0) && (req
[nextsend
] == NULL
)) {
8242 iosize
= MIN(nmwsize
, txsize
);
8243 uio_reset(auio
, txoffset
, UIO_SYSSPACE
, UIO_WRITE
);
8244 uio_addiov(auio
, CAST_USER_ADDR_T(txaddr
), iosize
);
8245 FSDBG(323, uio_offset(auio
), iosize
, txaddr
, txsize
);
8246 OSAddAtomic64(1, &nfsstats
.pageouts
);
8247 nfs_node_lock_force(np
);
8249 nfs_node_unlock(np
);
8250 vnode_startwrite(vp
);
8251 iomode
= NFS_WRITE_UNSTABLE
;
8252 if ((error
= nmp
->nm_funcs
->nf_write_rpc_async(np
, auio
, iosize
, thd
, cred
, iomode
, NULL
, &req
[nextsend
]))) {
8253 req
[nextsend
] = NULL
;
8254 vnode_writedone(vp
);
8255 nfs_node_lock_force(np
);
8257 nfs_node_unlock(np
);
8263 nextsend
= (nextsend
+ 1) % MAXPAGINGREQS
;
8265 /* wait while we need to and break out if more requests to send */
8266 while ((rxsize
> 0) && req
[nextwait
]) {
8267 iosize
= remsize
= MIN(nmwsize
, rxsize
);
8268 error
= nmp
->nm_funcs
->nf_write_rpc_async_finish(np
, req
[nextwait
], &iomode
, &iosize
, &wverf2
);
8269 req
[nextwait
] = NULL
;
8270 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
8271 vnode_writedone(vp
);
8272 nfs_node_lock_force(np
);
8274 nfs_node_unlock(np
);
8276 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
)) {
8277 lck_mtx_lock(&nmp
->nm_lock
);
8278 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
8279 NP(np
, "nfs_vnop_pageout: error %d, initiating recovery", error
);
8280 nfs_need_recover(nmp
, error
);
8282 lck_mtx_unlock(&nmp
->nm_lock
);
8288 FSDBG(323, rxoffset
, rxsize
, error
, -1);
8294 } else if (wverf
!= wverf2
) {
8295 /* verifier changed, so we need to restart all the writes */
8299 /* Retain the lowest commitment level returned. */
8300 if (iomode
< commit
) {
8308 /* need to try sending the remainder */
8310 uio_reset(auio
, rxoffset
, UIO_SYSSPACE
, UIO_WRITE
);
8311 uio_addiov(auio
, CAST_USER_ADDR_T(rxaddr
), remsize
);
8312 iomode
= NFS_WRITE_UNSTABLE
;
8313 error
= nfs_write_rpc2(np
, auio
, thd
, cred
, &iomode
, &wverf2
);
8315 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
)) {
8316 NP(np
, "nfs_vnop_pageout: restart: error %d", error
);
8317 lck_mtx_lock(&nmp
->nm_lock
);
8318 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
8319 NP(np
, "nfs_vnop_pageout: error %d, initiating recovery", error
);
8320 nfs_need_recover(nmp
, error
);
8322 lck_mtx_unlock(&nmp
->nm_lock
);
8328 FSDBG(323, rxoffset
, rxsize
, error
, -1);
8331 if (wverf
!= wverf2
) {
8332 /* verifier changed, so we need to restart all the writes */
8336 if (iomode
< commit
) {
8347 } while (!error
&& (txsize
|| rxsize
));
8351 if (!error
&& (commit
!= NFS_WRITE_FILESYNC
)) {
8352 error
= nmp
->nm_funcs
->nf_commit_rpc(np
, f_offset
, xsize
, cred
, wverf
);
8353 if (error
== NFSERR_STALEWRITEVERF
) {
8361 /* cancel any outstanding requests */
8362 while (req
[nextwait
]) {
8363 nfs_request_async_cancel(req
[nextwait
]);
8364 req
[nextwait
] = NULL
;
8365 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
8366 vnode_writedone(vp
);
8367 nfs_node_lock_force(np
);
8369 nfs_node_unlock(np
);
8371 if (np
->n_flag
& NREVOKE
) {
8375 if (++vrestarts
<= 100) { /* guard against no progress */
8378 NP(np
, "nfs_pageout: too many restarts, aborting");
8379 FSDBG(323, f_offset
, xsize
, ERESTART
, -1);
8382 if (restarts
<= nfs_mount_state_max_restarts(nmp
)) { /* guard against no progress */
8383 if (error
== NFSERR_GRACE
) {
8384 tsleep(&nmp
->nm_state
, (PZERO
- 1), "nfsgrace", 2 * hz
);
8386 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
))) {
8390 NP(np
, "nfs_pageout: too many restarts, aborting");
8391 FSDBG(323, f_offset
, xsize
, ERESTART
, -1);
8400 * We've had several different solutions on what to do when the pageout
8401 * gets an error. If we don't handle it, and return an error to the
8402 * caller, vm, it will retry . This can end in endless looping
8403 * between vm and here doing retries of the same page. Doing a dump
8404 * back to vm, will get it out of vm's knowledge and we lose whatever
8405 * data existed. This is risky, but in some cases necessary. For
8406 * example, the initial fix here was to do that for ESTALE. In that case
8407 * the server is telling us that the file is no longer the same. We
8408 * would not want to keep paging out to that. We also saw some 151
8409 * errors from Auspex server and NFSv3 can return errors higher than
8410 * ELAST. Those along with NFS known server errors we will "dump" from
8411 * vm. Errors we don't expect to occur, we dump and log for further
8412 * analysis. Errors that could be transient, networking ones,
8413 * we let vm "retry". Lastly, errors that we retry, but may have potential
8414 * to storm the network, we "retrywithsleep". "sever" will be used in
8415 * in the future to dump all pages of object for cases like ESTALE.
8416 * All this is the basis for the states returned and first guesses on
8417 * error handling. Tweaking expected as more statistics are gathered.
8418 * Note, in the long run we may need another more robust solution to
8419 * have some kind of persistant store when the vm cannot dump nor keep
8420 * retrying as a solution, but this would be a file architectural change
8422 if (!nofreeupl
) { /* otherwise stacked file system has to handle this */
8425 char action
= nfs_pageouterrorhandler(error
);
8429 abortflags
= UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
;
8432 abortflags
= UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
;
8433 if (error
<= NFS_ELAST
) {
8434 if ((errorcount
[error
] % 100) == 0) {
8435 NP(np
, "nfs_pageout: unexpected error %d. dumping vm page", error
);
8437 errorcount
[error
]++;
8441 abortflags
= UPL_ABORT_FREE_ON_EMPTY
;
8443 case SEVER
: /* not implemented */
8445 NP(np
, "nfs_pageout: action %d not expected", action
);
8449 ubc_upl_abort_range(pl
, pl_offset
, pgsize
, abortflags
);
8450 /* return error in all cases above */
8452 ubc_upl_commit_range(pl
, pl_offset
, pgsize
,
8453 UPL_COMMIT_CLEAR_DIRTY
|
8454 UPL_COMMIT_FREE_ON_EMPTY
);
8460 /* Blktooff derives file offset given a logical block number */
8463 struct vnop_blktooff_args
/* {
8464 * struct vnodeop_desc *a_desc;
8466 * daddr64_t a_lblkno;
8471 vnode_t vp
= ap
->a_vp
;
8472 struct nfsmount
*nmp
= VTONMP(vp
);
8474 if (nfs_mount_gone(nmp
)) {
8477 biosize
= nmp
->nm_biosize
;
8479 *ap
->a_offset
= (off_t
)(ap
->a_lblkno
* biosize
);
8486 struct vnop_offtoblk_args
/* {
8487 * struct vnodeop_desc *a_desc;
8490 * daddr64_t *a_lblkno;
8494 vnode_t vp
= ap
->a_vp
;
8495 struct nfsmount
*nmp
= VTONMP(vp
);
8497 if (nfs_mount_gone(nmp
)) {
8500 biosize
= nmp
->nm_biosize
;
8502 *ap
->a_lblkno
= (daddr64_t
)(ap
->a_offset
/ biosize
);
8508 * vnode change monitoring
8512 struct vnop_monitor_args
/* {
8513 * struct vnodeop_desc *a_desc;
8515 * uint32_t a_events;
8518 * vfs_context_t a_context;
8521 nfsnode_t np
= VTONFS(ap
->a_vp
);
8522 struct nfsmount
*nmp
= VTONMP(ap
->a_vp
);
8525 if (nfs_mount_gone(nmp
)) {
8529 /* make sure that the vnode's monitoring status is up to date */
8530 lck_mtx_lock(&nmp
->nm_lock
);
8531 if (vnode_ismonitored(ap
->a_vp
)) {
8532 /* This vnode is currently being monitored, make sure we're tracking it. */
8533 if (np
->n_monlink
.le_next
== NFSNOLIST
) {
8534 LIST_INSERT_HEAD(&nmp
->nm_monlist
, np
, n_monlink
);
8535 nfs_mount_sock_thread_wake(nmp
);
8538 /* This vnode is no longer being monitored, make sure we're not tracking it. */
8539 /* Wait for any in-progress getattr to complete first. */
8540 while (np
->n_mflag
& NMMONSCANINPROG
) {
8541 struct timespec ts
= { .tv_sec
= 1, .tv_nsec
= 0 };
8542 np
->n_mflag
|= NMMONSCANWANT
;
8543 msleep(&np
->n_mflag
, &nmp
->nm_lock
, PZERO
- 1, "nfswaitmonscan", &ts
);
8545 if (np
->n_monlink
.le_next
!= NFSNOLIST
) {
8546 LIST_REMOVE(np
, n_monlink
);
8547 np
->n_monlink
.le_next
= NFSNOLIST
;
8550 lck_mtx_unlock(&nmp
->nm_lock
);
8556 * Send a vnode notification for the given events.
8559 nfs_vnode_notify(nfsnode_t np
, uint32_t events
)
8561 struct nfsmount
*nmp
= NFSTONMP(np
);
8562 struct nfs_vattr nvattr
;
8563 struct vnode_attr vattr
, *vap
= NULL
;
8567 if ((np
->n_evtstamp
== now
.tv_sec
) || !nmp
) {
8568 /* delay sending this notify */
8569 np
->n_events
|= events
;
8572 events
|= np
->n_events
;
8574 np
->n_evtstamp
= now
.tv_sec
;
8576 vfs_get_notify_attributes(&vattr
);
8577 if (!nfs_getattrcache(np
, &nvattr
, 0)) {
8581 VATTR_RETURN(vap
, va_fsid
, vfs_statfs(nmp
->nm_mountp
)->f_fsid
.val
[0]);
8582 VATTR_RETURN(vap
, va_fileid
, nvattr
.nva_fileid
);
8583 VATTR_RETURN(vap
, va_mode
, nvattr
.nva_mode
);
8584 VATTR_RETURN(vap
, va_uid
, nvattr
.nva_uid
);
8585 VATTR_RETURN(vap
, va_gid
, nvattr
.nva_gid
);
8586 VATTR_RETURN(vap
, va_nlink
, nvattr
.nva_nlink
);
8588 vnode_notify(NFSTOV(np
), events
, vap
);