2 * Copyright (c) 2000-2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95
65 * FreeBSD-Id: nfs_vnops.c,v 1.72 1997/11/07 09:20:48 phk Exp $
70 * vnode op calls for Sun NFS version 2 and 3
72 #include <sys/param.h>
73 #include <sys/kernel.h>
74 #include <sys/systm.h>
75 #include <sys/resourcevar.h>
76 #include <sys/proc_internal.h>
77 #include <sys/kauth.h>
78 #include <sys/mount_internal.h>
79 #include <sys/malloc.h>
80 #include <sys/kpi_mbuf.h>
82 #include <sys/vnode_internal.h>
83 #include <sys/dirent.h>
84 #include <sys/fcntl.h>
85 #include <sys/lockf.h>
86 #include <sys/ubc_internal.h>
88 #include <sys/signalvar.h>
89 #include <sys/uio_internal.h>
91 #include <vfs/vfs_support.h>
96 #include <kern/clock.h>
97 #include <libkern/OSAtomic.h>
99 #include <miscfs/fifofs/fifo.h>
100 #include <miscfs/specfs/specdev.h>
102 #include <nfs/rpcv2.h>
103 #include <nfs/nfsproto.h>
105 #include <nfs/nfsnode.h>
106 #include <nfs/nfs_gss.h>
107 #include <nfs/nfsmount.h>
108 #include <nfs/nfs_lock.h>
109 #include <nfs/xdr_subs.h>
110 #include <nfs/nfsm_subs.h>
113 #include <netinet/in.h>
114 #include <netinet/in_var.h>
116 #include <vm/vm_kern.h>
117 #include <vm/vm_pageout.h>
119 #include <kern/task.h>
120 #include <kern/sched_prim.h>
122 #define NFS_VNOP_DBG(...) NFS_DBG(NFS_FAC_VNOP, 7, ## __VA_ARGS__)
123 #define DEFAULT_READLINK_NOCACHE 0
128 int nfs_vnop_lookup(struct vnop_lookup_args
*);
129 int nfsspec_vnop_read(struct vnop_read_args
*);
130 int nfsspec_vnop_write(struct vnop_write_args
*);
131 int nfsspec_vnop_close(struct vnop_close_args
*);
133 int nfsfifo_vnop_read(struct vnop_read_args
*);
134 int nfsfifo_vnop_write(struct vnop_write_args
*);
135 int nfsfifo_vnop_close(struct vnop_close_args
*);
137 int nfs_vnop_ioctl(struct vnop_ioctl_args
*);
138 int nfs_vnop_select(struct vnop_select_args
*);
139 int nfs_vnop_setattr(struct vnop_setattr_args
*);
140 int nfs_vnop_fsync(struct vnop_fsync_args
*);
141 int nfs_vnop_rename(struct vnop_rename_args
*);
142 int nfs_vnop_readdir(struct vnop_readdir_args
*);
143 int nfs_vnop_readlink(struct vnop_readlink_args
*);
144 int nfs_vnop_pathconf(struct vnop_pathconf_args
*);
145 int nfs_vnop_pagein(struct vnop_pagein_args
*);
146 int nfs_vnop_pageout(struct vnop_pageout_args
*);
147 int nfs_vnop_blktooff(struct vnop_blktooff_args
*);
148 int nfs_vnop_offtoblk(struct vnop_offtoblk_args
*);
149 int nfs_vnop_blockmap(struct vnop_blockmap_args
*);
150 int nfs_vnop_monitor(struct vnop_monitor_args
*);
152 int nfs3_vnop_create(struct vnop_create_args
*);
153 int nfs3_vnop_mknod(struct vnop_mknod_args
*);
154 int nfs3_vnop_getattr(struct vnop_getattr_args
*);
155 int nfs3_vnop_link(struct vnop_link_args
*);
156 int nfs3_vnop_mkdir(struct vnop_mkdir_args
*);
157 int nfs3_vnop_rmdir(struct vnop_rmdir_args
*);
158 int nfs3_vnop_symlink(struct vnop_symlink_args
*);
160 vnop_t
**nfsv2_vnodeop_p
;
161 static struct vnodeopv_entry_desc nfsv2_vnodeop_entries
[] = {
162 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
163 { &vnop_lookup_desc
, (vnop_t
*)nfs_vnop_lookup
}, /* lookup */
164 { &vnop_create_desc
, (vnop_t
*)nfs3_vnop_create
}, /* create */
165 { &vnop_mknod_desc
, (vnop_t
*)nfs3_vnop_mknod
}, /* mknod */
166 { &vnop_open_desc
, (vnop_t
*)nfs_vnop_open
}, /* open */
167 { &vnop_close_desc
, (vnop_t
*)nfs_vnop_close
}, /* close */
168 { &vnop_access_desc
, (vnop_t
*)nfs_vnop_access
}, /* access */
169 { &vnop_getattr_desc
, (vnop_t
*)nfs3_vnop_getattr
}, /* getattr */
170 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
171 { &vnop_read_desc
, (vnop_t
*)nfs_vnop_read
}, /* read */
172 { &vnop_write_desc
, (vnop_t
*)nfs_vnop_write
}, /* write */
173 { &vnop_ioctl_desc
, (vnop_t
*)nfs_vnop_ioctl
}, /* ioctl */
174 { &vnop_select_desc
, (vnop_t
*)nfs_vnop_select
}, /* select */
175 { &vnop_revoke_desc
, (vnop_t
*)nfs_vnop_revoke
}, /* revoke */
176 { &vnop_mmap_desc
, (vnop_t
*)nfs_vnop_mmap
}, /* mmap */
177 { &vnop_mnomap_desc
, (vnop_t
*)nfs_vnop_mnomap
}, /* mnomap */
178 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
179 { &vnop_remove_desc
, (vnop_t
*)nfs_vnop_remove
}, /* remove */
180 { &vnop_link_desc
, (vnop_t
*)nfs3_vnop_link
}, /* link */
181 { &vnop_rename_desc
, (vnop_t
*)nfs_vnop_rename
}, /* rename */
182 { &vnop_mkdir_desc
, (vnop_t
*)nfs3_vnop_mkdir
}, /* mkdir */
183 { &vnop_rmdir_desc
, (vnop_t
*)nfs3_vnop_rmdir
}, /* rmdir */
184 { &vnop_symlink_desc
, (vnop_t
*)nfs3_vnop_symlink
}, /* symlink */
185 { &vnop_readdir_desc
, (vnop_t
*)nfs_vnop_readdir
}, /* readdir */
186 { &vnop_readlink_desc
, (vnop_t
*)nfs_vnop_readlink
}, /* readlink */
187 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
188 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
189 { &vnop_strategy_desc
, (vnop_t
*)err_strategy
}, /* strategy */
190 { &vnop_pathconf_desc
, (vnop_t
*)nfs_vnop_pathconf
}, /* pathconf */
191 { &vnop_advlock_desc
, (vnop_t
*)nfs_vnop_advlock
}, /* advlock */
192 { &vnop_bwrite_desc
, (vnop_t
*)err_bwrite
}, /* bwrite */
193 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
194 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
195 { &vnop_copyfile_desc
, (vnop_t
*)err_copyfile
}, /* Copyfile */
196 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
197 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
198 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
199 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
202 struct vnodeopv_desc nfsv2_vnodeop_opv_desc
=
203 { &nfsv2_vnodeop_p
, nfsv2_vnodeop_entries
};
205 vnop_t
**nfsv4_vnodeop_p
;
206 static struct vnodeopv_entry_desc nfsv4_vnodeop_entries
[] = {
207 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
208 { &vnop_lookup_desc
, (vnop_t
*)nfs_vnop_lookup
}, /* lookup */
209 { &vnop_create_desc
, (vnop_t
*)nfs4_vnop_create
}, /* create */
210 { &vnop_mknod_desc
, (vnop_t
*)nfs4_vnop_mknod
}, /* mknod */
211 { &vnop_open_desc
, (vnop_t
*)nfs_vnop_open
}, /* open */
212 { &vnop_close_desc
, (vnop_t
*)nfs_vnop_close
}, /* close */
213 { &vnop_access_desc
, (vnop_t
*)nfs_vnop_access
}, /* access */
214 { &vnop_getattr_desc
, (vnop_t
*)nfs4_vnop_getattr
}, /* getattr */
215 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
216 { &vnop_read_desc
, (vnop_t
*)nfs_vnop_read
}, /* read */
217 { &vnop_write_desc
, (vnop_t
*)nfs_vnop_write
}, /* write */
218 { &vnop_ioctl_desc
, (vnop_t
*)nfs_vnop_ioctl
}, /* ioctl */
219 { &vnop_select_desc
, (vnop_t
*)nfs_vnop_select
}, /* select */
220 { &vnop_revoke_desc
, (vnop_t
*)nfs_vnop_revoke
}, /* revoke */
221 { &vnop_mmap_desc
, (vnop_t
*)nfs_vnop_mmap
}, /* mmap */
222 { &vnop_mnomap_desc
, (vnop_t
*)nfs_vnop_mnomap
}, /* mnomap */
223 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
224 { &vnop_remove_desc
, (vnop_t
*)nfs_vnop_remove
}, /* remove */
225 { &vnop_link_desc
, (vnop_t
*)nfs4_vnop_link
}, /* link */
226 { &vnop_rename_desc
, (vnop_t
*)nfs_vnop_rename
}, /* rename */
227 { &vnop_mkdir_desc
, (vnop_t
*)nfs4_vnop_mkdir
}, /* mkdir */
228 { &vnop_rmdir_desc
, (vnop_t
*)nfs4_vnop_rmdir
}, /* rmdir */
229 { &vnop_symlink_desc
, (vnop_t
*)nfs4_vnop_symlink
}, /* symlink */
230 { &vnop_readdir_desc
, (vnop_t
*)nfs_vnop_readdir
}, /* readdir */
231 { &vnop_readlink_desc
, (vnop_t
*)nfs_vnop_readlink
}, /* readlink */
232 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
233 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
234 { &vnop_strategy_desc
, (vnop_t
*)err_strategy
}, /* strategy */
235 { &vnop_pathconf_desc
, (vnop_t
*)nfs_vnop_pathconf
}, /* pathconf */
236 { &vnop_advlock_desc
, (vnop_t
*)nfs_vnop_advlock
}, /* advlock */
237 { &vnop_bwrite_desc
, (vnop_t
*)err_bwrite
}, /* bwrite */
238 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
239 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
240 { &vnop_copyfile_desc
, (vnop_t
*)err_copyfile
}, /* Copyfile */
241 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
242 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
243 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
244 { &vnop_getxattr_desc
, (vnop_t
*)nfs4_vnop_getxattr
}, /* getxattr */
245 { &vnop_setxattr_desc
, (vnop_t
*)nfs4_vnop_setxattr
}, /* setxattr */
246 { &vnop_removexattr_desc
, (vnop_t
*)nfs4_vnop_removexattr
},/* removexattr */
247 { &vnop_listxattr_desc
, (vnop_t
*)nfs4_vnop_listxattr
},/* listxattr */
249 { &vnop_getnamedstream_desc
, (vnop_t
*)nfs4_vnop_getnamedstream
}, /* getnamedstream */
250 { &vnop_makenamedstream_desc
, (vnop_t
*)nfs4_vnop_makenamedstream
}, /* makenamedstream */
251 { &vnop_removenamedstream_desc
, (vnop_t
*)nfs4_vnop_removenamedstream
},/* removenamedstream */
253 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
256 struct vnodeopv_desc nfsv4_vnodeop_opv_desc
=
257 { &nfsv4_vnodeop_p
, nfsv4_vnodeop_entries
};
260 * Special device vnode ops
262 vnop_t
**spec_nfsv2nodeop_p
;
263 static struct vnodeopv_entry_desc spec_nfsv2nodeop_entries
[] = {
264 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
265 { &vnop_lookup_desc
, (vnop_t
*)spec_lookup
}, /* lookup */
266 { &vnop_create_desc
, (vnop_t
*)spec_create
}, /* create */
267 { &vnop_mknod_desc
, (vnop_t
*)spec_mknod
}, /* mknod */
268 { &vnop_open_desc
, (vnop_t
*)spec_open
}, /* open */
269 { &vnop_close_desc
, (vnop_t
*)nfsspec_vnop_close
}, /* close */
270 { &vnop_getattr_desc
, (vnop_t
*)nfs3_vnop_getattr
}, /* getattr */
271 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
272 { &vnop_read_desc
, (vnop_t
*)nfsspec_vnop_read
}, /* read */
273 { &vnop_write_desc
, (vnop_t
*)nfsspec_vnop_write
}, /* write */
274 { &vnop_ioctl_desc
, (vnop_t
*)spec_ioctl
}, /* ioctl */
275 { &vnop_select_desc
, (vnop_t
*)spec_select
}, /* select */
276 { &vnop_revoke_desc
, (vnop_t
*)spec_revoke
}, /* revoke */
277 { &vnop_mmap_desc
, (vnop_t
*)spec_mmap
}, /* mmap */
278 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
279 { &vnop_remove_desc
, (vnop_t
*)spec_remove
}, /* remove */
280 { &vnop_link_desc
, (vnop_t
*)spec_link
}, /* link */
281 { &vnop_rename_desc
, (vnop_t
*)spec_rename
}, /* rename */
282 { &vnop_mkdir_desc
, (vnop_t
*)spec_mkdir
}, /* mkdir */
283 { &vnop_rmdir_desc
, (vnop_t
*)spec_rmdir
}, /* rmdir */
284 { &vnop_symlink_desc
, (vnop_t
*)spec_symlink
}, /* symlink */
285 { &vnop_readdir_desc
, (vnop_t
*)spec_readdir
}, /* readdir */
286 { &vnop_readlink_desc
, (vnop_t
*)spec_readlink
}, /* readlink */
287 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
288 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
289 { &vnop_strategy_desc
, (vnop_t
*)spec_strategy
}, /* strategy */
290 { &vnop_pathconf_desc
, (vnop_t
*)spec_pathconf
}, /* pathconf */
291 { &vnop_advlock_desc
, (vnop_t
*)spec_advlock
}, /* advlock */
292 { &vnop_bwrite_desc
, (vnop_t
*)vn_bwrite
}, /* bwrite */
293 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
294 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
295 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
296 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
297 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
298 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
301 struct vnodeopv_desc spec_nfsv2nodeop_opv_desc
=
302 { &spec_nfsv2nodeop_p
, spec_nfsv2nodeop_entries
};
303 vnop_t
**spec_nfsv4nodeop_p
;
304 static struct vnodeopv_entry_desc spec_nfsv4nodeop_entries
[] = {
305 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
306 { &vnop_lookup_desc
, (vnop_t
*)spec_lookup
}, /* lookup */
307 { &vnop_create_desc
, (vnop_t
*)spec_create
}, /* create */
308 { &vnop_mknod_desc
, (vnop_t
*)spec_mknod
}, /* mknod */
309 { &vnop_open_desc
, (vnop_t
*)spec_open
}, /* open */
310 { &vnop_close_desc
, (vnop_t
*)nfsspec_vnop_close
}, /* close */
311 { &vnop_getattr_desc
, (vnop_t
*)nfs4_vnop_getattr
}, /* getattr */
312 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
313 { &vnop_read_desc
, (vnop_t
*)nfsspec_vnop_read
}, /* read */
314 { &vnop_write_desc
, (vnop_t
*)nfsspec_vnop_write
}, /* write */
315 { &vnop_ioctl_desc
, (vnop_t
*)spec_ioctl
}, /* ioctl */
316 { &vnop_select_desc
, (vnop_t
*)spec_select
}, /* select */
317 { &vnop_revoke_desc
, (vnop_t
*)spec_revoke
}, /* revoke */
318 { &vnop_mmap_desc
, (vnop_t
*)spec_mmap
}, /* mmap */
319 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
320 { &vnop_remove_desc
, (vnop_t
*)spec_remove
}, /* remove */
321 { &vnop_link_desc
, (vnop_t
*)spec_link
}, /* link */
322 { &vnop_rename_desc
, (vnop_t
*)spec_rename
}, /* rename */
323 { &vnop_mkdir_desc
, (vnop_t
*)spec_mkdir
}, /* mkdir */
324 { &vnop_rmdir_desc
, (vnop_t
*)spec_rmdir
}, /* rmdir */
325 { &vnop_symlink_desc
, (vnop_t
*)spec_symlink
}, /* symlink */
326 { &vnop_readdir_desc
, (vnop_t
*)spec_readdir
}, /* readdir */
327 { &vnop_readlink_desc
, (vnop_t
*)spec_readlink
}, /* readlink */
328 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
329 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
330 { &vnop_strategy_desc
, (vnop_t
*)spec_strategy
}, /* strategy */
331 { &vnop_pathconf_desc
, (vnop_t
*)spec_pathconf
}, /* pathconf */
332 { &vnop_advlock_desc
, (vnop_t
*)spec_advlock
}, /* advlock */
333 { &vnop_bwrite_desc
, (vnop_t
*)vn_bwrite
}, /* bwrite */
334 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
335 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
336 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
337 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
338 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
339 { &vnop_getxattr_desc
, (vnop_t
*)nfs4_vnop_getxattr
}, /* getxattr */
340 { &vnop_setxattr_desc
, (vnop_t
*)nfs4_vnop_setxattr
}, /* setxattr */
341 { &vnop_removexattr_desc
, (vnop_t
*)nfs4_vnop_removexattr
},/* removexattr */
342 { &vnop_listxattr_desc
, (vnop_t
*)nfs4_vnop_listxattr
},/* listxattr */
344 { &vnop_getnamedstream_desc
, (vnop_t
*)nfs4_vnop_getnamedstream
}, /* getnamedstream */
345 { &vnop_makenamedstream_desc
, (vnop_t
*)nfs4_vnop_makenamedstream
}, /* makenamedstream */
346 { &vnop_removenamedstream_desc
, (vnop_t
*)nfs4_vnop_removenamedstream
},/* removenamedstream */
348 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
351 struct vnodeopv_desc spec_nfsv4nodeop_opv_desc
=
352 { &spec_nfsv4nodeop_p
, spec_nfsv4nodeop_entries
};
355 vnop_t
**fifo_nfsv2nodeop_p
;
356 static struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries
[] = {
357 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
358 { &vnop_lookup_desc
, (vnop_t
*)fifo_lookup
}, /* lookup */
359 { &vnop_create_desc
, (vnop_t
*)fifo_create
}, /* create */
360 { &vnop_mknod_desc
, (vnop_t
*)fifo_mknod
}, /* mknod */
361 { &vnop_open_desc
, (vnop_t
*)fifo_open
}, /* open */
362 { &vnop_close_desc
, (vnop_t
*)nfsfifo_vnop_close
}, /* close */
363 { &vnop_getattr_desc
, (vnop_t
*)nfs3_vnop_getattr
}, /* getattr */
364 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
365 { &vnop_read_desc
, (vnop_t
*)nfsfifo_vnop_read
}, /* read */
366 { &vnop_write_desc
, (vnop_t
*)nfsfifo_vnop_write
}, /* write */
367 { &vnop_ioctl_desc
, (vnop_t
*)fifo_ioctl
}, /* ioctl */
368 { &vnop_select_desc
, (vnop_t
*)fifo_select
}, /* select */
369 { &vnop_revoke_desc
, (vnop_t
*)fifo_revoke
}, /* revoke */
370 { &vnop_mmap_desc
, (vnop_t
*)fifo_mmap
}, /* mmap */
371 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
372 { &vnop_remove_desc
, (vnop_t
*)fifo_remove
}, /* remove */
373 { &vnop_link_desc
, (vnop_t
*)fifo_link
}, /* link */
374 { &vnop_rename_desc
, (vnop_t
*)fifo_rename
}, /* rename */
375 { &vnop_mkdir_desc
, (vnop_t
*)fifo_mkdir
}, /* mkdir */
376 { &vnop_rmdir_desc
, (vnop_t
*)fifo_rmdir
}, /* rmdir */
377 { &vnop_symlink_desc
, (vnop_t
*)fifo_symlink
}, /* symlink */
378 { &vnop_readdir_desc
, (vnop_t
*)fifo_readdir
}, /* readdir */
379 { &vnop_readlink_desc
, (vnop_t
*)fifo_readlink
}, /* readlink */
380 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
381 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
382 { &vnop_strategy_desc
, (vnop_t
*)fifo_strategy
}, /* strategy */
383 { &vnop_pathconf_desc
, (vnop_t
*)fifo_pathconf
}, /* pathconf */
384 { &vnop_advlock_desc
, (vnop_t
*)fifo_advlock
}, /* advlock */
385 { &vnop_bwrite_desc
, (vnop_t
*)vn_bwrite
}, /* bwrite */
386 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
387 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
388 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
389 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
390 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
391 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
394 struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc
=
395 { &fifo_nfsv2nodeop_p
, fifo_nfsv2nodeop_entries
};
397 vnop_t
**fifo_nfsv4nodeop_p
;
398 static struct vnodeopv_entry_desc fifo_nfsv4nodeop_entries
[] = {
399 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
400 { &vnop_lookup_desc
, (vnop_t
*)fifo_lookup
}, /* lookup */
401 { &vnop_create_desc
, (vnop_t
*)fifo_create
}, /* create */
402 { &vnop_mknod_desc
, (vnop_t
*)fifo_mknod
}, /* mknod */
403 { &vnop_open_desc
, (vnop_t
*)fifo_open
}, /* open */
404 { &vnop_close_desc
, (vnop_t
*)nfsfifo_vnop_close
}, /* close */
405 { &vnop_getattr_desc
, (vnop_t
*)nfs4_vnop_getattr
}, /* getattr */
406 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
407 { &vnop_read_desc
, (vnop_t
*)nfsfifo_vnop_read
}, /* read */
408 { &vnop_write_desc
, (vnop_t
*)nfsfifo_vnop_write
}, /* write */
409 { &vnop_ioctl_desc
, (vnop_t
*)fifo_ioctl
}, /* ioctl */
410 { &vnop_select_desc
, (vnop_t
*)fifo_select
}, /* select */
411 { &vnop_revoke_desc
, (vnop_t
*)fifo_revoke
}, /* revoke */
412 { &vnop_mmap_desc
, (vnop_t
*)fifo_mmap
}, /* mmap */
413 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
414 { &vnop_remove_desc
, (vnop_t
*)fifo_remove
}, /* remove */
415 { &vnop_link_desc
, (vnop_t
*)fifo_link
}, /* link */
416 { &vnop_rename_desc
, (vnop_t
*)fifo_rename
}, /* rename */
417 { &vnop_mkdir_desc
, (vnop_t
*)fifo_mkdir
}, /* mkdir */
418 { &vnop_rmdir_desc
, (vnop_t
*)fifo_rmdir
}, /* rmdir */
419 { &vnop_symlink_desc
, (vnop_t
*)fifo_symlink
}, /* symlink */
420 { &vnop_readdir_desc
, (vnop_t
*)fifo_readdir
}, /* readdir */
421 { &vnop_readlink_desc
, (vnop_t
*)fifo_readlink
}, /* readlink */
422 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
423 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
424 { &vnop_strategy_desc
, (vnop_t
*)fifo_strategy
}, /* strategy */
425 { &vnop_pathconf_desc
, (vnop_t
*)fifo_pathconf
}, /* pathconf */
426 { &vnop_advlock_desc
, (vnop_t
*)fifo_advlock
}, /* advlock */
427 { &vnop_bwrite_desc
, (vnop_t
*)vn_bwrite
}, /* bwrite */
428 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
429 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
430 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
431 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
432 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
433 { &vnop_getxattr_desc
, (vnop_t
*)nfs4_vnop_getxattr
}, /* getxattr */
434 { &vnop_setxattr_desc
, (vnop_t
*)nfs4_vnop_setxattr
}, /* setxattr */
435 { &vnop_removexattr_desc
, (vnop_t
*)nfs4_vnop_removexattr
},/* removexattr */
436 { &vnop_listxattr_desc
, (vnop_t
*)nfs4_vnop_listxattr
},/* listxattr */
438 { &vnop_getnamedstream_desc
, (vnop_t
*)nfs4_vnop_getnamedstream
}, /* getnamedstream */
439 { &vnop_makenamedstream_desc
, (vnop_t
*)nfs4_vnop_makenamedstream
}, /* makenamedstream */
440 { &vnop_removenamedstream_desc
, (vnop_t
*)nfs4_vnop_removenamedstream
},/* removenamedstream */
442 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
445 struct vnodeopv_desc fifo_nfsv4nodeop_opv_desc
=
446 { &fifo_nfsv4nodeop_p
, fifo_nfsv4nodeop_entries
};
449 int nfs_sillyrename(nfsnode_t
, nfsnode_t
, struct componentname
*, vfs_context_t
);
450 int nfs_getattr_internal(nfsnode_t
, struct nfs_vattr
*, vfs_context_t
, int);
451 int nfs_refresh_fh(nfsnode_t
, vfs_context_t
);
454 * Find the slot in the access cache for this UID.
455 * If adding and no existing slot is found, reuse slots in FIFO order.
456 * The index of the next slot to use is kept in the last entry of the n_access array.
459 nfs_node_access_slot(nfsnode_t np
, uid_t uid
, int add
)
463 for (slot
= 0; slot
< NFS_ACCESS_CACHE_SIZE
; slot
++) {
464 if (np
->n_accessuid
[slot
] == uid
) {
468 if (slot
== NFS_ACCESS_CACHE_SIZE
) {
472 slot
= np
->n_access
[NFS_ACCESS_CACHE_SIZE
];
473 np
->n_access
[NFS_ACCESS_CACHE_SIZE
] = (slot
+ 1) % NFS_ACCESS_CACHE_SIZE
;
479 nfs3_access_rpc(nfsnode_t np
, u_int32_t
*access
, int rpcflags
, vfs_context_t ctx
)
481 int error
= 0, lockerror
= ENOENT
, status
, slot
;
482 uint32_t access_result
= 0;
484 struct nfsm_chain nmreq
, nmrep
;
485 struct nfsmount
*nmp
;
489 nfsm_chain_null(&nmreq
);
490 nfsm_chain_null(&nmrep
);
492 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(NFS_VER3
) + NFSX_UNSIGNED
);
493 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER3
, np
->n_fhp
, np
->n_fhsize
);
494 nfsm_chain_add_32(error
, &nmreq
, *access
);
495 nfsm_chain_build_done(error
, &nmreq
);
497 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC_ACCESS
,
498 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
499 NULL
, rpcflags
, &nmrep
, &xid
, &status
);
500 if ((lockerror
= nfs_node_lock(np
))) {
503 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
507 nfsm_chain_get_32(error
, &nmrep
, access_result
);
510 /* XXXab do we really need mount here, also why are we doing access cache management here? */
512 if (nfs_mount_gone(nmp
)) {
517 if (auth_is_kerberized(np
->n_auth
) || auth_is_kerberized(nmp
->nm_auth
)) {
518 uid
= nfs_cred_getasid2uid(vfs_context_ucred(ctx
));
520 uid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
522 slot
= nfs_node_access_slot(np
, uid
, 1);
523 np
->n_accessuid
[slot
] = uid
;
525 np
->n_accessstamp
[slot
] = now
.tv_sec
;
526 np
->n_access
[slot
] = access_result
;
529 * If we asked for DELETE but didn't get it, the server
530 * may simply not support returning that bit (possible
531 * on UNIX systems). So, we'll assume that it is OK,
532 * and just let any subsequent delete action fail if it
533 * really isn't deletable.
535 if ((*access
& NFS_ACCESS_DELETE
) &&
536 !(np
->n_access
[slot
] & NFS_ACCESS_DELETE
)) {
537 np
->n_access
[slot
] |= NFS_ACCESS_DELETE
;
539 /* ".zfs" subdirectories may erroneously give a denied answer for add/remove */
540 if (nfs_access_dotzfs
&& (np
->n_flag
& NISDOTZFSCHILD
)) {
541 np
->n_access
[slot
] |= (NFS_ACCESS_MODIFY
| NFS_ACCESS_EXTEND
| NFS_ACCESS_DELETE
);
543 /* pass back the access returned with this request */
544 *access
= np
->n_access
[slot
];
549 nfsm_chain_cleanup(&nmreq
);
550 nfsm_chain_cleanup(&nmrep
);
555 * NFS access vnode op.
556 * For NFS version 2, just return ok. File accesses may fail later.
557 * For NFS version 3+, use the access RPC to check accessibility. If file
558 * permissions are changed on the server, accesses might still fail later.
562 struct vnop_access_args
/* {
563 * struct vnodeop_desc *a_desc;
566 * vfs_context_t a_context;
569 vfs_context_t ctx
= ap
->a_context
;
570 vnode_t vp
= ap
->a_vp
;
571 int error
= 0, slot
, dorpc
, rpcflags
= 0;
572 u_int32_t access
, waccess
;
573 nfsnode_t np
= VTONFS(vp
);
574 struct nfsmount
*nmp
;
580 if (nfs_mount_gone(nmp
)) {
583 nfsvers
= nmp
->nm_vers
;
585 if (nfsvers
== NFS_VER2
) {
586 if ((ap
->a_action
& KAUTH_VNODE_WRITE_RIGHTS
) &&
587 vfs_isrdonly(vnode_mount(vp
))) {
594 * For NFS v3, do an access rpc, otherwise you are stuck emulating
595 * ufs_access() locally using the vattr. This may not be correct,
596 * since the server may apply other access criteria such as
597 * client uid-->server uid mapping that we do not know about, but
598 * this is better than just returning anything that is lying about
603 * Convert KAUTH primitives to NFS access rights.
606 if (vnode_isdir(vp
)) {
609 (KAUTH_VNODE_LIST_DIRECTORY
|
610 KAUTH_VNODE_READ_EXTATTRIBUTES
)) {
611 access
|= NFS_ACCESS_READ
;
613 if (ap
->a_action
& KAUTH_VNODE_SEARCH
) {
614 access
|= NFS_ACCESS_LOOKUP
;
617 (KAUTH_VNODE_ADD_FILE
|
618 KAUTH_VNODE_ADD_SUBDIRECTORY
)) {
619 access
|= NFS_ACCESS_MODIFY
| NFS_ACCESS_EXTEND
;
621 if (ap
->a_action
& KAUTH_VNODE_DELETE_CHILD
) {
622 access
|= NFS_ACCESS_MODIFY
;
627 (KAUTH_VNODE_READ_DATA
|
628 KAUTH_VNODE_READ_EXTATTRIBUTES
)) {
629 access
|= NFS_ACCESS_READ
;
631 if (ap
->a_action
& KAUTH_VNODE_WRITE_DATA
) {
632 access
|= NFS_ACCESS_MODIFY
| NFS_ACCESS_EXTEND
;
634 if (ap
->a_action
& KAUTH_VNODE_APPEND_DATA
) {
635 access
|= NFS_ACCESS_EXTEND
;
637 if (ap
->a_action
& KAUTH_VNODE_EXECUTE
) {
638 access
|= NFS_ACCESS_EXECUTE
;
642 if (ap
->a_action
& KAUTH_VNODE_DELETE
) {
643 access
|= NFS_ACCESS_DELETE
;
646 (KAUTH_VNODE_WRITE_ATTRIBUTES
|
647 KAUTH_VNODE_WRITE_EXTATTRIBUTES
|
648 KAUTH_VNODE_WRITE_SECURITY
)) {
649 access
|= NFS_ACCESS_MODIFY
;
651 /* XXX this is pretty dubious */
652 if (ap
->a_action
& KAUTH_VNODE_CHANGE_OWNER
) {
653 access
|= NFS_ACCESS_MODIFY
;
656 /* if caching, always ask for every right */
657 if (nfs_access_cache_timeout
> 0) {
658 waccess
= NFS_ACCESS_READ
| NFS_ACCESS_MODIFY
|
659 NFS_ACCESS_EXTEND
| NFS_ACCESS_EXECUTE
|
660 NFS_ACCESS_DELETE
| NFS_ACCESS_LOOKUP
;
665 if ((error
= nfs_node_lock(np
))) {
670 * Does our cached result allow us to give a definite yes to
673 if (auth_is_kerberized(np
->n_auth
) || auth_is_kerberized(nmp
->nm_auth
)) {
674 uid
= nfs_cred_getasid2uid(vfs_context_ucred(ctx
));
676 uid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
678 slot
= nfs_node_access_slot(np
, uid
, 0);
681 /* not asking for any rights understood by NFS, so don't bother doing an RPC */
682 /* OSAddAtomic(1, &nfsstats.accesscache_hits); */
685 } else if (NACCESSVALID(np
, slot
)) {
687 if (((now
.tv_sec
< (np
->n_accessstamp
[slot
] + nfs_access_cache_timeout
)) &&
688 ((np
->n_access
[slot
] & access
) == access
)) || nfs_use_cache(nmp
)) {
689 /* OSAddAtomic(1, &nfsstats.accesscache_hits); */
691 waccess
= np
->n_access
[slot
];
696 /* Either a no, or a don't know. Go to the wire. */
697 /* OSAddAtomic(1, &nfsstats.accesscache_misses); */
700 * Allow an access call to timeout if we have it cached
701 * so we won't hang if the server isn't responding.
703 if (NACCESSVALID(np
, slot
)) {
707 error
= nmp
->nm_funcs
->nf_access_rpc(np
, &waccess
, rpcflags
, ctx
);
710 * If the server didn't respond return the cached access.
712 if ((error
== ETIMEDOUT
) && (rpcflags
& R_SOFT
)) {
714 waccess
= np
->n_access
[slot
];
717 if (!error
&& ((waccess
& access
) != access
)) {
728 * Perform various update/invalidation checks and then add the
729 * open to the node. Regular files will have an open file structure
730 * on the node and, for NFSv4, perform an OPEN request on the server.
734 struct vnop_open_args
/* {
735 * struct vnodeop_desc *a_desc;
738 * vfs_context_t a_context;
741 vfs_context_t ctx
= ap
->a_context
;
742 vnode_t vp
= ap
->a_vp
;
743 nfsnode_t np
= VTONFS(vp
);
744 struct nfsmount
*nmp
;
745 int error
, accessMode
, denyMode
, opened
= 0;
746 struct nfs_open_owner
*noop
= NULL
;
747 struct nfs_open_file
*nofp
= NULL
;
750 if (!(ap
->a_mode
& (FREAD
| FWRITE
))) {
755 if (nfs_mount_gone(nmp
)) {
758 if (np
->n_flag
& NREVOKE
) {
762 vtype
= vnode_vtype(vp
);
763 if ((vtype
!= VREG
) && (vtype
!= VDIR
) && (vtype
!= VLNK
)) {
767 /* First, check if we need to update/invalidate */
768 if (ISSET(np
->n_flag
, NUPDATESIZE
)) {
769 nfs_data_update_size(np
, 0);
771 if ((error
= nfs_node_lock(np
))) {
774 if (np
->n_flag
& NNEEDINVALIDATE
) {
775 np
->n_flag
&= ~NNEEDINVALIDATE
;
780 nfs_vinvalbuf(vp
, V_SAVE
| V_IGNORE_WRITEERR
, ctx
, 1);
781 if ((error
= nfs_node_lock(np
))) {
786 np
->n_lastrahead
= -1;
788 if (np
->n_flag
& NMODIFIED
) {
793 if ((error
= nfs_vinvalbuf(vp
, V_SAVE
| V_IGNORE_WRITEERR
, ctx
, 1))) {
800 /* nfs_getattr() will check changed and purge caches */
801 if ((error
= nfs_getattr(np
, NULL
, ctx
, NGA_UNCACHED
))) {
806 /* Just mark that it was opened */
807 lck_mtx_lock(&np
->n_openlock
);
809 lck_mtx_unlock(&np
->n_openlock
);
813 /* mode contains some combination of: FREAD, FWRITE, O_SHLOCK, O_EXLOCK */
815 if (ap
->a_mode
& FREAD
) {
816 accessMode
|= NFS_OPEN_SHARE_ACCESS_READ
;
818 if (ap
->a_mode
& FWRITE
) {
819 accessMode
|= NFS_OPEN_SHARE_ACCESS_WRITE
;
821 if (ap
->a_mode
& O_EXLOCK
) {
822 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
823 } else if (ap
->a_mode
& O_SHLOCK
) {
824 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
826 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
828 // XXX don't do deny modes just yet (and never do it for !v4)
829 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
831 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
837 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
839 nfs_open_owner_rele(noop
);
842 if (np
->n_flag
& NREVOKE
) {
844 nfs_mount_state_in_use_end(nmp
, 0);
845 nfs_open_owner_rele(noop
);
849 error
= nfs_open_file_find(np
, noop
, &nofp
, accessMode
, denyMode
, 1);
850 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
851 NP(np
, "nfs_vnop_open: LOST %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
854 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
855 nfs_mount_state_in_use_end(nmp
, 0);
856 error
= nfs4_reopen(nofp
, vfs_context_thread(ctx
));
863 error
= nfs_open_file_set_busy(nofp
, vfs_context_thread(ctx
));
870 if (nmp
->nm_vers
< NFS_VER4
) {
872 * NFS v2/v3 opens are always allowed - so just add it.
874 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, 0);
879 * If we just created the file and the modes match, then we simply use
880 * the open performed in the create. Otherwise, send the request.
882 if ((nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) &&
883 (nofp
->nof_creator
== current_thread()) &&
884 (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
) &&
885 (denyMode
== NFS_OPEN_SHARE_DENY_NONE
)) {
886 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
887 nofp
->nof_creator
= NULL
;
890 error
= nfs4_open(np
, nofp
, accessMode
, denyMode
, ctx
);
892 if ((error
== EACCES
) && (nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) &&
893 (nofp
->nof_creator
== current_thread())) {
895 * Ugh. This can happen if we just created the file with read-only
896 * perms and we're trying to open it for real with different modes
897 * (e.g. write-only or with a deny mode) and the server decides to
898 * not allow the second open because of the read-only perms.
899 * The best we can do is to just use the create's open.
900 * We may have access we don't need or we may not have a requested
901 * deny mode. We may log complaints later, but we'll try to avoid it.
903 if (denyMode
!= NFS_OPEN_SHARE_DENY_NONE
) {
904 NP(np
, "nfs_vnop_open: deny mode foregone on create, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
906 nofp
->nof_creator
= NULL
;
914 * If we had just created the file, we already had it open.
915 * If the actual open mode is less than what we grabbed at
916 * create time, then we'll downgrade the open here.
918 if ((nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) &&
919 (nofp
->nof_creator
== current_thread())) {
920 error
= nfs_close(np
, nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
922 NP(np
, "nfs_vnop_open: create close error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
924 if (!nfs_mount_state_error_should_restart(error
)) {
926 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
933 nfs_open_file_clear_busy(nofp
);
935 if (nfs_mount_state_in_use_end(nmp
, error
)) {
940 NP(np
, "nfs_vnop_open: error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
943 nfs_open_owner_rele(noop
);
945 if (!error
&& vtype
== VREG
&& (ap
->a_mode
& FWRITE
)) {
946 lck_mtx_lock(&nmp
->nm_lock
);
947 nmp
->nm_state
&= ~NFSSTA_SQUISHY
;
948 nmp
->nm_curdeadtimeout
= nmp
->nm_deadtimeout
;
949 if (nmp
->nm_curdeadtimeout
<= 0) {
950 nmp
->nm_deadto_start
= 0;
953 lck_mtx_unlock(&nmp
->nm_lock
);
960 nfs_no_of_open_file_writers(nfsnode_t np
)
962 uint32_t writers
= 0;
963 struct nfs_open_file
*nofp
;
965 TAILQ_FOREACH(nofp
, &np
->n_opens
, nof_link
) {
966 writers
+= nofp
->nof_w
+ nofp
->nof_rw
+ nofp
->nof_w_dw
+ nofp
->nof_rw_dw
+
967 nofp
->nof_w_drw
+ nofp
->nof_rw_drw
+ nofp
->nof_d_w_dw
+
968 nofp
->nof_d_rw_dw
+ nofp
->nof_d_w_drw
+ nofp
->nof_d_rw_drw
+
969 nofp
->nof_d_w
+ nofp
->nof_d_rw
;
978 * What an NFS client should do upon close after writing is a debatable issue.
979 * Most NFS clients push delayed writes to the server upon close, basically for
981 * 1 - So that any write errors may be reported back to the client process
982 * doing the close system call. By far the two most likely errors are
983 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
984 * 2 - To put a worst case upper bound on cache inconsistency between
985 * multiple clients for the file.
986 * There is also a consistency problem for Version 2 of the protocol w.r.t.
987 * not being able to tell if other clients are writing a file concurrently,
988 * since there is no way of knowing if the changed modify time in the reply
989 * is only due to the write for this client.
990 * (NFS Version 3 provides weak cache consistency data in the reply that
991 * should be sufficient to detect and handle this case.)
993 * The current code does the following:
994 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
995 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate them.
996 * for NFS Version 4 - basically the same as NFSv3
1000 struct vnop_close_args
/* {
1001 * struct vnodeop_desc *a_desc;
1004 * vfs_context_t a_context;
1007 vfs_context_t ctx
= ap
->a_context
;
1008 vnode_t vp
= ap
->a_vp
;
1009 nfsnode_t np
= VTONFS(vp
);
1010 struct nfsmount
*nmp
;
1011 int error
= 0, error1
, nfsvers
;
1012 int fflag
= ap
->a_fflag
;
1014 int accessMode
, denyMode
;
1015 struct nfs_open_owner
*noop
= NULL
;
1016 struct nfs_open_file
*nofp
= NULL
;
1022 nfsvers
= nmp
->nm_vers
;
1023 vtype
= vnode_vtype(vp
);
1025 /* First, check if we need to update/flush/invalidate */
1026 if (ISSET(np
->n_flag
, NUPDATESIZE
)) {
1027 nfs_data_update_size(np
, 0);
1029 nfs_node_lock_force(np
);
1030 if (np
->n_flag
& NNEEDINVALIDATE
) {
1031 np
->n_flag
&= ~NNEEDINVALIDATE
;
1032 nfs_node_unlock(np
);
1033 nfs_vinvalbuf(vp
, V_SAVE
| V_IGNORE_WRITEERR
, ctx
, 1);
1034 nfs_node_lock_force(np
);
1036 if ((vtype
== VREG
) && (np
->n_flag
& NMODIFIED
) && (fflag
& FWRITE
)) {
1037 /* we're closing an open for write and the file is modified, so flush it */
1038 nfs_node_unlock(np
);
1039 if (nfsvers
!= NFS_VER2
) {
1040 error
= nfs_flush(np
, MNT_WAIT
, vfs_context_thread(ctx
), 0);
1042 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
1044 nfs_node_lock_force(np
);
1045 NATTRINVALIDATE(np
);
1047 if (np
->n_flag
& NWRITEERR
) {
1048 np
->n_flag
&= ~NWRITEERR
;
1049 error
= np
->n_error
;
1051 nfs_node_unlock(np
);
1053 if (vtype
!= VREG
) {
1054 /* Just mark that it was closed */
1055 lck_mtx_lock(&np
->n_openlock
);
1056 if (np
->n_openrefcnt
== 0) {
1057 if (fflag
& (FREAD
| FWRITE
)) {
1058 NP(np
, "nfs_vnop_close: open reference underrun");
1061 } else if (fflag
& (FREAD
| FWRITE
)) {
1064 /* No FREAD/FWRITE set - probably the final close */
1065 np
->n_openrefcnt
= 0;
1067 lck_mtx_unlock(&np
->n_openlock
);
1072 /* fflag should contain some combination of: FREAD, FWRITE, FHASLOCK */
1074 if (fflag
& FREAD
) {
1075 accessMode
|= NFS_OPEN_SHARE_ACCESS_READ
;
1077 if (fflag
& FWRITE
) {
1078 accessMode
|= NFS_OPEN_SHARE_ACCESS_WRITE
;
1080 // XXX It would be nice if we still had the O_EXLOCK/O_SHLOCK flags that were on the open
1081 // if (fflag & O_EXLOCK)
1082 // denyMode = NFS_OPEN_SHARE_DENY_BOTH;
1083 // else if (fflag & O_SHLOCK)
1084 // denyMode = NFS_OPEN_SHARE_DENY_WRITE;
1086 // denyMode = NFS_OPEN_SHARE_DENY_NONE;
1088 if (fflag
& FHASLOCK
) {
1089 /* XXX assume FHASLOCK is for the deny mode and not flock */
1090 /* FHASLOCK flock will be unlocked in the close path, but the flag is not cleared. */
1091 if (nofp
->nof_deny
& NFS_OPEN_SHARE_DENY_READ
) {
1092 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
1093 } else if (nofp
->nof_deny
& NFS_OPEN_SHARE_DENY_WRITE
) {
1094 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
1096 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
1099 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
1102 // XXX don't do deny modes just yet (and never do it for !v4)
1103 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
1108 * No mode given to close?
1109 * Guess this is the final close.
1110 * We should unlock all locks and close all opens.
1113 mount_t mp
= vnode_mount(vp
);
1114 int force
= (!mp
|| vfs_isforce(mp
));
1116 writers
= nfs_no_of_open_file_writers(np
);
1117 nfs_release_open_state_for_node(np
, force
);
1119 lck_mtx_lock(&nmp
->nm_lock
);
1120 if (writers
> nmp
->nm_writers
) {
1121 NP(np
, "nfs_vnop_close: number of write opens for mount underrun. Node has %d"
1122 " opens for write. Mount has total of %d opens for write\n",
1123 writers
, nmp
->nm_writers
);
1124 nmp
->nm_writers
= 0;
1126 nmp
->nm_writers
-= writers
;
1128 lck_mtx_unlock(&nmp
->nm_lock
);
1132 } else if (fflag
& FWRITE
) {
1133 lck_mtx_lock(&nmp
->nm_lock
);
1134 if (nmp
->nm_writers
== 0) {
1135 NP(np
, "nfs_vnop_close: removing open writer from mount, but mount has no files open for writing");
1139 lck_mtx_unlock(&nmp
->nm_lock
);
1143 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 0);
1145 // printf("nfs_vnop_close: can't get open owner!\n");
1150 error
= nfs_mount_state_in_use_start(nmp
, NULL
);
1152 nfs_open_owner_rele(noop
);
1156 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 0);
1157 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
1158 nfs_mount_state_in_use_end(nmp
, 0);
1159 error
= nfs4_reopen(nofp
, NULL
);
1166 NP(np
, "nfs_vnop_close: no open file for owner, error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
1170 error
= nfs_open_file_set_busy(nofp
, NULL
);
1176 error
= nfs_close(np
, nofp
, accessMode
, denyMode
, ctx
);
1178 NP(np
, "nfs_vnop_close: close error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
1183 nfs_open_file_clear_busy(nofp
);
1185 if (nfs_mount_state_in_use_end(nmp
, error
)) {
1193 NP(np
, "nfs_vnop_close: error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
1196 nfs_open_owner_rele(noop
);
1202 * nfs_close(): common function that does all the heavy lifting of file closure
1204 * Takes an open file structure and a set of access/deny modes and figures out how
1205 * to update the open file structure (and the state on the server) appropriately.
1210 struct nfs_open_file
*nofp
,
1211 uint32_t accessMode
,
1215 struct nfs_lock_owner
*nlop
;
1216 int error
= 0, changed
= 0, delegated
= 0, closed
= 0, downgrade
= 0;
1217 uint32_t newAccessMode
, newDenyMode
;
1219 /* warn if modes don't match current state */
1220 if (((accessMode
& nofp
->nof_access
) != accessMode
) || ((denyMode
& nofp
->nof_deny
) != denyMode
)) {
1221 NP(np
, "nfs_close: mode mismatch %d %d, current %d %d, %d",
1222 accessMode
, denyMode
, nofp
->nof_access
, nofp
->nof_deny
,
1223 kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1227 * If we're closing a write-only open, we may not have a write-only count
1228 * if we also grabbed read access. So, check the read-write count.
1230 if (denyMode
== NFS_OPEN_SHARE_DENY_NONE
) {
1231 if ((accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) &&
1232 (nofp
->nof_w
== 0) && (nofp
->nof_d_w
== 0) &&
1233 (nofp
->nof_rw
|| nofp
->nof_d_rw
)) {
1234 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
1236 } else if (denyMode
== NFS_OPEN_SHARE_DENY_WRITE
) {
1237 if ((accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) &&
1238 (nofp
->nof_w_dw
== 0) && (nofp
->nof_d_w_dw
== 0) &&
1239 (nofp
->nof_rw_dw
|| nofp
->nof_d_rw_dw
)) {
1240 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
1242 } else { /* NFS_OPEN_SHARE_DENY_BOTH */
1243 if ((accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) &&
1244 (nofp
->nof_w_drw
== 0) && (nofp
->nof_d_w_drw
== 0) &&
1245 (nofp
->nof_rw_drw
|| nofp
->nof_d_rw_drw
)) {
1246 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
1250 nfs_open_file_remove_open_find(nofp
, accessMode
, denyMode
, &newAccessMode
, &newDenyMode
, &delegated
);
1251 if ((newAccessMode
!= nofp
->nof_access
) || (newDenyMode
!= nofp
->nof_deny
)) {
1257 if (NFSTONMP(np
)->nm_vers
< NFS_VER4
) { /* NFS v2/v3 closes simply need to remove the open. */
1261 if ((newAccessMode
== 0) || (nofp
->nof_opencnt
== 1)) {
1263 * No more access after this close, so clean up and close it.
1264 * Don't send a close RPC if we're closing a delegated open.
1268 if (!delegated
&& !(nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
1269 error
= nfs4_close_rpc(np
, nofp
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
), 0);
1271 if (error
== NFSERR_LOCKS_HELD
) {
1273 * Hmm... the server says we have locks we need to release first
1274 * Find the lock owner and try to unlock everything.
1276 nlop
= nfs_lock_owner_find(np
, vfs_context_proc(ctx
), 0);
1278 nfs4_unlock_rpc(np
, nlop
, F_WRLCK
, 0, UINT64_MAX
,
1279 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
1280 nfs_lock_owner_rele(nlop
);
1282 error
= nfs4_close_rpc(np
, nofp
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
), 0);
1284 } else if (changed
) {
1286 * File is still open but with less access, so downgrade the open.
1287 * Don't send a downgrade RPC if we're closing a delegated open.
1289 if (!delegated
&& !(nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
1292 * If we have delegated opens, we should probably claim them before sending
1293 * the downgrade because the server may not know the open we are downgrading to.
1295 if (nofp
->nof_d_rw_drw
|| nofp
->nof_d_w_drw
|| nofp
->nof_d_r_drw
||
1296 nofp
->nof_d_rw_dw
|| nofp
->nof_d_w_dw
|| nofp
->nof_d_r_dw
||
1297 nofp
->nof_d_rw
|| nofp
->nof_d_w
|| nofp
->nof_d_r
) {
1298 nfs4_claim_delegated_state_for_open_file(nofp
, 0);
1300 /* need to remove the open before sending the downgrade */
1301 nfs_open_file_remove_open(nofp
, accessMode
, denyMode
);
1302 error
= nfs4_open_downgrade_rpc(np
, nofp
, ctx
);
1303 if (error
) { /* Hmm.. that didn't work. Add the open back in. */
1304 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, delegated
);
1310 NP(np
, "nfs_close: error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1316 nfs_open_file_remove_open(nofp
, accessMode
, denyMode
);
1320 lck_mtx_lock(&nofp
->nof_lock
);
1321 if (nofp
->nof_r
|| nofp
->nof_d_r
|| nofp
->nof_w
|| nofp
->nof_d_w
|| nofp
->nof_d_rw
||
1322 (nofp
->nof_rw
&& !((nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) && !nofp
->nof_creator
&& (nofp
->nof_rw
== 1))) ||
1323 nofp
->nof_r_dw
|| nofp
->nof_d_r_dw
|| nofp
->nof_w_dw
|| nofp
->nof_d_w_dw
||
1324 nofp
->nof_rw_dw
|| nofp
->nof_d_rw_dw
|| nofp
->nof_r_drw
|| nofp
->nof_d_r_drw
||
1325 nofp
->nof_w_drw
|| nofp
->nof_d_w_drw
|| nofp
->nof_rw_drw
|| nofp
->nof_d_rw_drw
) {
1326 NP(np
, "nfs_close: unexpected count: %u.%u %u.%u %u.%u dw %u.%u %u.%u %u.%u drw %u.%u %u.%u %u.%u flags 0x%x, %d",
1327 nofp
->nof_r
, nofp
->nof_d_r
, nofp
->nof_w
, nofp
->nof_d_w
,
1328 nofp
->nof_rw
, nofp
->nof_d_rw
, nofp
->nof_r_dw
, nofp
->nof_d_r_dw
,
1329 nofp
->nof_w_dw
, nofp
->nof_d_w_dw
, nofp
->nof_rw_dw
, nofp
->nof_d_rw_dw
,
1330 nofp
->nof_r_drw
, nofp
->nof_d_r_drw
, nofp
->nof_w_drw
, nofp
->nof_d_w_drw
,
1331 nofp
->nof_rw_drw
, nofp
->nof_d_rw_drw
, nofp
->nof_flags
,
1332 kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1334 /* clear out all open info, just to be safe */
1335 nofp
->nof_access
= nofp
->nof_deny
= 0;
1336 nofp
->nof_mmap_access
= nofp
->nof_mmap_deny
= 0;
1337 nofp
->nof_r
= nofp
->nof_d_r
= 0;
1338 nofp
->nof_w
= nofp
->nof_d_w
= 0;
1339 nofp
->nof_rw
= nofp
->nof_d_rw
= 0;
1340 nofp
->nof_r_dw
= nofp
->nof_d_r_dw
= 0;
1341 nofp
->nof_w_dw
= nofp
->nof_d_w_dw
= 0;
1342 nofp
->nof_rw_dw
= nofp
->nof_d_rw_dw
= 0;
1343 nofp
->nof_r_drw
= nofp
->nof_d_r_drw
= 0;
1344 nofp
->nof_w_drw
= nofp
->nof_d_w_drw
= 0;
1345 nofp
->nof_rw_drw
= nofp
->nof_d_rw_drw
= 0;
1346 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
1347 lck_mtx_unlock(&nofp
->nof_lock
);
1348 /* XXX we may potentially want to clean up idle/unused open file structures */
1350 if (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
) {
1352 NP(np
, "nfs_close: LOST%s, %d", !nofp
->nof_opencnt
? " (last)" : "",
1353 kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1368 struct nfs_vattr
*nvap
,
1371 struct nfsmount
*nmp
= mp
? VFSTONFS(mp
) : NFSTONMP(np
);
1372 int error
= 0, status
, nfsvers
, rpcflags
= 0;
1373 struct nfsm_chain nmreq
, nmrep
;
1375 if (nfs_mount_gone(nmp
)) {
1378 nfsvers
= nmp
->nm_vers
;
1380 if (flags
& NGA_MONITOR
) { /* vnode monitor requests should be soft */
1381 rpcflags
= R_RECOVER
;
1384 if (flags
& NGA_SOFT
) { /* Return ETIMEDOUT if server not responding */
1388 nfsm_chain_null(&nmreq
);
1389 nfsm_chain_null(&nmrep
);
1391 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nfsvers
));
1392 if (nfsvers
!= NFS_VER2
) {
1393 nfsm_chain_add_32(error
, &nmreq
, fhsize
);
1395 nfsm_chain_add_opaque(error
, &nmreq
, fhp
, fhsize
);
1396 nfsm_chain_build_done(error
, &nmreq
);
1398 error
= nfs_request2(np
, mp
, &nmreq
, NFSPROC_GETATTR
,
1399 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
1400 NULL
, rpcflags
, &nmrep
, xidp
, &status
);
1405 error
= nfs_parsefattr(&nmrep
, nfsvers
, nvap
);
1407 nfsm_chain_cleanup(&nmreq
);
1408 nfsm_chain_cleanup(&nmrep
);
1413 * nfs_refresh_fh will attempt to update the file handle for the node.
1415 * It only does this for symbolic links and regular files that are not currently opened.
1417 * On Success returns 0 and the nodes file handle is updated, or ESTALE on failure.
1420 nfs_refresh_fh(nfsnode_t np
, vfs_context_t ctx
)
1422 vnode_t dvp
, vp
= NFSTOV(np
);
1424 const char *v_name
= vnode_getname(vp
);
1426 int namelen
, fhsize
, refreshed
;
1427 int error
, wanted
= 0;
1429 struct timespec ts
= {2, 0};
1431 NFS_VNOP_DBG("vnode is %d\n", vnode_vtype(vp
));
1433 dvp
= vnode_parent(vp
);
1434 if ((vnode_vtype(vp
) != VREG
&& vnode_vtype(vp
) != VLNK
) ||
1435 v_name
== NULL
|| *v_name
== '\0' || dvp
== NULL
) {
1436 if (v_name
!= NULL
) {
1437 vnode_putname(v_name
);
1443 namelen
= strlen(v_name
);
1444 MALLOC(name
, char *, namelen
+ 1, M_TEMP
, M_WAITOK
);
1446 vnode_putname(v_name
);
1449 bcopy(v_name
, name
, namelen
+ 1);
1450 NFS_VNOP_DBG("Trying to refresh %s : %s\n", v_name
, name
);
1451 vnode_putname(v_name
);
1453 /* Allocate the maximum size file handle */
1454 MALLOC(fhp
, uint8_t *, NFS4_FHSIZE
, M_TEMP
, M_WAITOK
);
1460 if ((error
= nfs_node_lock(np
))) {
1466 fhsize
= np
->n_fhsize
;
1467 bcopy(np
->n_fhp
, fhp
, fhsize
);
1468 while (ISSET(np
->n_flag
, NREFRESH
)) {
1469 SET(np
->n_flag
, NREFRESHWANT
);
1470 NFS_VNOP_DBG("Waiting for refresh of %s\n", name
);
1471 msleep(np
, &np
->n_lock
, PZERO
- 1, "nfsrefreshwant", &ts
);
1472 if ((error
= nfs_sigintr(NFSTONMP(np
), NULL
, vfs_context_thread(ctx
), 0))) {
1476 refreshed
= error
? 0 : !NFS_CMPFH(np
, fhp
, fhsize
);
1477 SET(np
->n_flag
, NREFRESH
);
1478 nfs_node_unlock(np
);
1480 NFS_VNOP_DBG("error = %d, refreshed = %d\n", error
, refreshed
);
1481 if (error
|| refreshed
) {
1485 /* Check that there are no open references for this file */
1486 lck_mtx_lock(&np
->n_openlock
);
1487 if (np
->n_openrefcnt
|| !TAILQ_EMPTY(&np
->n_opens
) || !TAILQ_EMPTY(&np
->n_lock_owners
)) {
1489 struct nfs_open_file
*ofp
;
1491 TAILQ_FOREACH(ofp
, &np
->n_opens
, nof_link
) {
1492 cnt
+= ofp
->nof_opencnt
;
1495 lck_mtx_unlock(&np
->n_openlock
);
1496 NFS_VNOP_DBG("Can not refresh file handle for %s with open state\n", name
);
1497 NFS_VNOP_DBG("\topenrefcnt = %d, opens = %d lock_owners = %d\n",
1498 np
->n_openrefcnt
, cnt
, !TAILQ_EMPTY(&np
->n_lock_owners
));
1503 lck_mtx_unlock(&np
->n_openlock
);
1505 * Since the FH is currently stale we should not be able to
1506 * establish any open state until the FH is refreshed.
1509 error
= nfs_node_lock(np
);
1512 * Symlinks should never need invalidations and are holding
1513 * the one and only nfsbuf in an uncached acquired state
1514 * trying to do a readlink. So we will hang if we invalidate
1515 * in that case. Only in in the VREG case do we need to
1518 if (vnode_vtype(vp
) == VREG
) {
1519 np
->n_flag
&= ~NNEEDINVALIDATE
;
1520 nfs_node_unlock(np
);
1521 error
= nfs_vinvalbuf(vp
, V_IGNORE_WRITEERR
, ctx
, 1);
1523 NFS_VNOP_DBG("nfs_vinvalbuf returned %d\n", error
);
1527 nfs_node_unlock(np
);
1530 NFS_VNOP_DBG("Looking up %s\n", name
);
1531 error
= nfs_lookitup(dnp
, name
, namelen
, ctx
, &np
);
1533 NFS_VNOP_DBG("nfs_lookitup returned %d\n", error
);
1537 nfs_node_lock_force(np
);
1538 wanted
= ISSET(np
->n_flag
, NREFRESHWANT
);
1539 CLR(np
->n_flag
, NREFRESH
| NREFRESHWANT
);
1540 nfs_node_unlock(np
);
1546 NFS_VNOP_DBG("%s refreshed file handle\n", name
);
1552 return error
? ESTALE
: 0;
1556 nfs_getattr(nfsnode_t np
, struct nfs_vattr
*nvap
, vfs_context_t ctx
, int flags
)
1561 error
= nfs_getattr_internal(np
, nvap
, ctx
, flags
);
1562 if (error
== ESTALE
) {
1563 error
= nfs_refresh_fh(np
, ctx
);
1572 nfs_getattr_internal(nfsnode_t np
, struct nfs_vattr
*nvap
, vfs_context_t ctx
, int flags
)
1574 struct nfsmount
*nmp
;
1575 int error
= 0, nfsvers
, inprogset
= 0, wanted
= 0, avoidfloods
;
1576 struct nfs_vattr nvattr
;
1577 struct timespec ts
= { 2, 0 };
1580 FSDBG_TOP(513, np
->n_size
, np
, np
->n_vattr
.nva_size
, np
->n_flag
);
1584 if (nfs_mount_gone(nmp
)) {
1587 nfsvers
= nmp
->nm_vers
;
1594 /* Update local times for special files. */
1595 if (np
->n_flag
& (NACC
| NUPD
)) {
1596 nfs_node_lock_force(np
);
1598 nfs_node_unlock(np
);
1600 /* Update size, if necessary */
1601 if (ISSET(np
->n_flag
, NUPDATESIZE
)) {
1602 nfs_data_update_size(np
, 0);
1605 error
= nfs_node_lock(np
);
1607 if (!(flags
& (NGA_UNCACHED
| NGA_MONITOR
)) || ((nfsvers
>= NFS_VER4
) && (np
->n_openflags
& N_DELEG_MASK
))) {
1609 * Use the cache or wait for any getattr in progress if:
1610 * - it's a cached request, or
1611 * - we have a delegation, or
1612 * - the server isn't responding
1615 error
= nfs_getattrcache(np
, nvap
, flags
);
1616 if (!error
|| (error
!= ENOENT
)) {
1617 nfs_node_unlock(np
);
1621 if (!ISSET(np
->n_flag
, NGETATTRINPROG
)) {
1624 if (flags
& NGA_MONITOR
) {
1625 /* no need to wait if a request is pending */
1626 error
= EINPROGRESS
;
1627 nfs_node_unlock(np
);
1630 SET(np
->n_flag
, NGETATTRWANT
);
1631 msleep(np
, &np
->n_lock
, PZERO
- 1, "nfsgetattrwant", &ts
);
1632 if ((error
= nfs_sigintr(NFSTONMP(np
), NULL
, vfs_context_thread(ctx
), 0))) {
1633 nfs_node_unlock(np
);
1637 SET(np
->n_flag
, NGETATTRINPROG
);
1639 } else if (!ISSET(np
->n_flag
, NGETATTRINPROG
)) {
1640 SET(np
->n_flag
, NGETATTRINPROG
);
1642 } else if (flags
& NGA_MONITOR
) {
1643 /* no need to make a request if one is pending */
1644 error
= EINPROGRESS
;
1646 nfs_node_unlock(np
);
1649 if (nfs_mount_gone(nmp
)) {
1657 * Return cached attributes if they are valid,
1658 * if the server doesn't respond, and this is
1659 * some softened up style of mount.
1661 if (NATTRVALID(np
) && nfs_use_cache(nmp
)) {
1666 * We might want to try to get both the attributes and access info by
1667 * making an ACCESS call and seeing if it returns updated attributes.
1668 * But don't bother if we aren't caching access info or if the
1669 * attributes returned wouldn't be cached.
1671 if (!(flags
& NGA_ACL
) && (nfsvers
!= NFS_VER2
) && nfs_access_for_getattr
&& (nfs_access_cache_timeout
> 0)) {
1672 if (nfs_attrcachetimeout(np
) > 0) {
1673 /* OSAddAtomic(1, &nfsstats.accesscache_misses); */
1674 u_int32_t access
= NFS_ACCESS_ALL
;
1677 /* Return cached attrs if server doesn't respond */
1678 if (flags
& NGA_SOFT
) {
1682 error
= nmp
->nm_funcs
->nf_access_rpc(np
, &access
, rpcflags
, ctx
);
1684 if (error
== ETIMEDOUT
) {
1691 nfs_node_lock_force(np
);
1692 error
= nfs_getattrcache(np
, nvap
, flags
);
1693 nfs_node_unlock(np
);
1694 if (!error
|| (error
!= ENOENT
)) {
1697 /* Well, that didn't work... just do a getattr... */
1705 error
= nmp
->nm_funcs
->nf_getattr_rpc(np
, NULL
, np
->n_fhp
, np
->n_fhsize
, flags
, ctx
, nvap
, &xid
);
1707 nfs_node_lock_force(np
);
1708 error
= nfs_loadattrcache(np
, nvap
, &xid
, 0);
1709 nfs_node_unlock(np
);
1713 * If the server didn't respond, return cached attributes.
1716 if ((flags
& NGA_SOFT
) && (error
== ETIMEDOUT
)) {
1717 nfs_node_lock_force(np
);
1718 error
= nfs_getattrcache(np
, nvap
, flags
);
1719 if (!error
|| (error
!= ENOENT
)) {
1720 nfs_node_unlock(np
);
1723 nfs_node_unlock(np
);
1727 if (!xid
) { /* out-of-order rpc - attributes were dropped */
1728 FSDBG(513, -1, np
, np
->n_xid
>> 32, np
->n_xid
);
1729 if (avoidfloods
++ < 20) {
1732 /* avoidfloods>1 is bizarre. at 20 pull the plug */
1733 /* just return the last attributes we got */
1736 nfs_node_lock_force(np
);
1738 wanted
= ISSET(np
->n_flag
, NGETATTRWANT
);
1739 CLR(np
->n_flag
, (NGETATTRINPROG
| NGETATTRWANT
));
1742 /* check if the node changed on us */
1743 vnode_t vp
= NFSTOV(np
);
1744 enum vtype vtype
= vnode_vtype(vp
);
1745 if ((vtype
== VDIR
) && NFS_CHANGED_NC(nfsvers
, np
, nvap
)) {
1746 FSDBG(513, -1, np
, 0, np
);
1747 np
->n_flag
&= ~NNEGNCENTRIES
;
1750 NFS_CHANGED_UPDATE_NC(nfsvers
, np
, nvap
);
1751 NFS_VNOP_DBG("Purge directory 0x%llx\n",
1752 (uint64_t)VM_KERNEL_ADDRPERM(vp
));
1754 if (NFS_CHANGED(nfsvers
, np
, nvap
)) {
1755 FSDBG(513, -1, np
, -1, np
);
1756 if (vtype
== VDIR
) {
1757 NFS_VNOP_DBG("Invalidate directory 0x%llx\n",
1758 (uint64_t)VM_KERNEL_ADDRPERM(vp
));
1761 nfs_node_unlock(np
);
1765 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
1766 FSDBG(513, -1, np
, -2, error
);
1768 nfs_node_lock_force(np
);
1769 NFS_CHANGED_UPDATE(nfsvers
, np
, nvap
);
1770 nfs_node_unlock(np
);
1773 nfs_node_unlock(np
);
1779 nfs_node_unlock(np
);
1785 if (nvap
== &nvattr
) {
1786 NVATTR_CLEANUP(nvap
);
1787 } else if (!(flags
& NGA_ACL
)) {
1788 /* make sure we don't return an ACL if it wasn't asked for */
1789 NFS_BITMAP_CLR(nvap
->nva_bitmap
, NFS_FATTR_ACL
);
1790 if (nvap
->nva_acl
) {
1791 kauth_acl_free(nvap
->nva_acl
);
1792 nvap
->nva_acl
= NULL
;
1795 FSDBG_BOT(513, np
->n_size
, error
, np
->n_vattr
.nva_size
, np
->n_flag
);
1800 * NFS getattr call from vfs.
1804 * The attributes we support over the wire.
1805 * We also get fsid but the vfs layer gets it out of the mount
1806 * structure after this calling us so there's no need to return it,
1807 * and Finder expects to call getattrlist just looking for the FSID
1808 * with out hanging on a non responsive server.
1810 #define NFS3_SUPPORTED_VATTRS \
1811 (VNODE_ATTR_va_rdev | \
1812 VNODE_ATTR_va_nlink | \
1813 VNODE_ATTR_va_data_size | \
1814 VNODE_ATTR_va_data_alloc | \
1815 VNODE_ATTR_va_uid | \
1816 VNODE_ATTR_va_gid | \
1817 VNODE_ATTR_va_mode | \
1818 VNODE_ATTR_va_modify_time | \
1819 VNODE_ATTR_va_change_time | \
1820 VNODE_ATTR_va_access_time | \
1821 VNODE_ATTR_va_fileid | \
1826 struct vnop_getattr_args
/* {
1827 * struct vnodeop_desc *a_desc;
1829 * struct vnode_attr *a_vap;
1830 * vfs_context_t a_context;
1834 struct nfs_vattr nva
;
1835 struct vnode_attr
*vap
= ap
->a_vap
;
1836 struct nfsmount
*nmp
;
1840 * Lets don't go over the wire if we don't support any of the attributes.
1841 * Just fall through at the VFS layer and let it cons up what it needs.
1843 /* Return the io size no matter what, since we don't go over the wire for this */
1844 VATTR_RETURN(vap
, va_iosize
, nfs_iosize
);
1845 if ((vap
->va_active
& NFS3_SUPPORTED_VATTRS
) == 0) {
1849 if (VATTR_IS_ACTIVE(ap
->a_vap
, va_name
)) {
1850 NFS_VNOP_DBG("Getting attrs for 0x%llx, vname is %s\n",
1851 (uint64_t)VM_KERNEL_ADDRPERM(ap
->a_vp
),
1852 ap
->a_vp
->v_name
? ap
->a_vp
->v_name
: "empty");
1854 error
= nfs_getattr(VTONFS(ap
->a_vp
), &nva
, ap
->a_context
, NGA_CACHED
);
1859 /* copy nva to *a_vap */
1860 nmp
= VTONMP(ap
->a_vp
);
1861 VATTR_RETURN(vap
, va_type
, nva
.nva_type
);
1862 VATTR_RETURN(vap
, va_mode
, nva
.nva_mode
);
1863 rdev
= makedev(nva
.nva_rawdev
.specdata1
, nva
.nva_rawdev
.specdata2
);
1864 VATTR_RETURN(vap
, va_rdev
, rdev
);
1865 VATTR_RETURN(vap
, va_uid
, nva
.nva_uid
);
1866 VATTR_RETURN(vap
, va_gid
, nva
.nva_gid
);
1867 VATTR_RETURN(vap
, va_nlink
, nva
.nva_nlink
);
1868 VATTR_RETURN(vap
, va_fileid
, nva
.nva_fileid
);
1869 VATTR_RETURN(vap
, va_data_size
, nva
.nva_size
);
1870 VATTR_RETURN(vap
, va_data_alloc
, nva
.nva_bytes
);
1871 vap
->va_access_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_ACCESS
];
1872 vap
->va_access_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_ACCESS
];
1873 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1874 vap
->va_modify_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_MODIFY
];
1875 vap
->va_modify_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_MODIFY
];
1876 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1877 vap
->va_change_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_CHANGE
];
1878 vap
->va_change_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_CHANGE
];
1879 VATTR_SET_SUPPORTED(vap
, va_change_time
);
1881 // VATTR_RETURN(vap, va_encoding, 0xffff /* kTextEncodingUnknown */);
1890 struct vnop_setattr_args
/* {
1891 * struct vnodeop_desc *a_desc;
1893 * struct vnode_attr *a_vap;
1894 * vfs_context_t a_context;
1897 vfs_context_t ctx
= ap
->a_context
;
1898 vnode_t vp
= ap
->a_vp
;
1899 nfsnode_t np
= VTONFS(vp
);
1900 struct nfsmount
*nmp
;
1901 struct vnode_attr
*vap
= ap
->a_vap
;
1903 int biosize
, nfsvers
, namedattrs
;
1904 u_quad_t origsize
, vapsize
;
1905 struct nfs_dulookup dul
;
1906 nfsnode_t dnp
= NULL
;
1907 int dul_in_progress
= 0;
1909 const char *vname
= NULL
;
1910 struct nfs_open_owner
*noop
= NULL
;
1911 struct nfs_open_file
*nofp
= NULL
;
1914 if (nfs_mount_gone(nmp
)) {
1917 nfsvers
= nmp
->nm_vers
;
1918 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
1919 biosize
= nmp
->nm_biosize
;
1921 /* Disallow write attempts if the filesystem is mounted read-only. */
1922 if (vnode_vfsisrdonly(vp
)) {
1926 origsize
= np
->n_size
;
1927 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
1928 switch (vnode_vtype(vp
)) {
1935 if (!VATTR_IS_ACTIVE(vap
, va_modify_time
) &&
1936 !VATTR_IS_ACTIVE(vap
, va_access_time
) &&
1937 !VATTR_IS_ACTIVE(vap
, va_mode
) &&
1938 !VATTR_IS_ACTIVE(vap
, va_uid
) &&
1939 !VATTR_IS_ACTIVE(vap
, va_gid
)) {
1942 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
1946 * Disallow write attempts if the filesystem is
1947 * mounted read-only.
1949 if (vnode_vfsisrdonly(vp
)) {
1952 FSDBG_TOP(512, np
->n_size
, vap
->va_data_size
,
1953 np
->n_vattr
.nva_size
, np
->n_flag
);
1954 /* clear NNEEDINVALIDATE, if set */
1955 if ((error
= nfs_node_lock(np
))) {
1958 if (np
->n_flag
& NNEEDINVALIDATE
) {
1959 np
->n_flag
&= ~NNEEDINVALIDATE
;
1961 nfs_node_unlock(np
);
1962 /* flush everything */
1963 error
= nfs_vinvalbuf(vp
, (vap
->va_data_size
? V_SAVE
: 0), ctx
, 1);
1965 NP(np
, "nfs_setattr: nfs_vinvalbuf %d", error
);
1966 FSDBG_BOT(512, np
->n_size
, vap
->va_data_size
, np
->n_vattr
.nva_size
, -1);
1969 if (nfsvers
>= NFS_VER4
) {
1970 /* setting file size requires having the file open for write access */
1971 if (np
->n_flag
& NREVOKE
) {
1974 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
1979 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
1983 if (np
->n_flag
& NREVOKE
) {
1984 nfs_mount_state_in_use_end(nmp
, 0);
1987 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 1);
1988 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
1991 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
1992 nfs_mount_state_in_use_end(nmp
, 0);
1993 error
= nfs4_reopen(nofp
, vfs_context_thread(ctx
));
2000 error
= nfs_open_file_set_busy(nofp
, vfs_context_thread(ctx
));
2003 nfs_open_owner_rele(noop
);
2006 if (!(nofp
->nof_access
& NFS_OPEN_SHARE_ACCESS_WRITE
)) {
2007 /* we don't have the file open for write access, so open it */
2008 error
= nfs4_open(np
, nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
2010 nofp
->nof_flags
|= NFS_OPEN_FILE_SETATTR
;
2012 if (nfs_mount_state_error_should_restart(error
)) {
2013 nfs_open_file_clear_busy(nofp
);
2015 if (nfs_mount_state_in_use_end(nmp
, error
)) {
2021 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
2022 if (np
->n_size
> vap
->va_data_size
) { /* shrinking? */
2024 int neweofoff
, mustwrite
;
2027 obn
= (np
->n_size
- 1) / biosize
;
2028 bn
= vap
->va_data_size
/ biosize
;
2029 for (; obn
>= bn
; obn
--) {
2030 if (!nfs_buf_is_incore(np
, obn
)) {
2033 error
= nfs_buf_get(np
, obn
, biosize
, NULL
, NBLK_READ
, &bp
);
2038 FSDBG(512, bp
, bp
->nb_flags
, 0, obn
);
2039 SET(bp
->nb_flags
, NB_INVAL
);
2040 nfs_buf_release(bp
, 1);
2044 neweofoff
= vap
->va_data_size
- NBOFF(bp
);
2045 /* check for any dirty data before the new EOF */
2046 if ((bp
->nb_dirtyend
> 0) && (bp
->nb_dirtyoff
< neweofoff
)) {
2047 /* clip dirty range to EOF */
2048 if (bp
->nb_dirtyend
> neweofoff
) {
2049 bp
->nb_dirtyend
= neweofoff
;
2050 if (bp
->nb_dirtyoff
>= bp
->nb_dirtyend
) {
2051 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
2054 if ((bp
->nb_dirtyend
> 0) && (bp
->nb_dirtyoff
< neweofoff
)) {
2058 bp
->nb_dirty
&= (1 << round_page_32(neweofoff
) / PAGE_SIZE
) - 1;
2063 FSDBG(512, bp
, bp
->nb_flags
, 0, obn
);
2064 SET(bp
->nb_flags
, NB_INVAL
);
2065 nfs_buf_release(bp
, 1);
2068 /* gotta write out dirty data before invalidating */
2069 /* (NB_STABLE indicates that data writes should be FILESYNC) */
2070 /* (NB_NOCACHE indicates buffer should be discarded) */
2071 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
| NB_ASYNC
| NB_READ
));
2072 SET(bp
->nb_flags
, NB_STABLE
| NB_NOCACHE
);
2073 if (!IS_VALID_CRED(bp
->nb_wcred
)) {
2074 kauth_cred_t cred
= vfs_context_ucred(ctx
);
2075 kauth_cred_ref(cred
);
2076 bp
->nb_wcred
= cred
;
2078 error
= nfs_buf_write(bp
);
2079 // Note: bp has been released
2081 FSDBG(512, bp
, 0xd00dee, 0xbad, error
);
2082 nfs_node_lock_force(np
);
2083 np
->n_error
= error
;
2084 np
->n_flag
|= NWRITEERR
;
2086 * There was a write error and we need to
2087 * invalidate attrs and flush buffers in
2088 * order to sync up with the server.
2089 * (if this write was extending the file,
2090 * we may no longer know the correct size)
2092 NATTRINVALIDATE(np
);
2093 nfs_node_unlock(np
);
2094 nfs_data_unlock(np
);
2095 nfs_vinvalbuf(vp
, V_SAVE
| V_IGNORE_WRITEERR
, ctx
, 1);
2096 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
2101 if (vap
->va_data_size
!= np
->n_size
) {
2102 ubc_setsize(vp
, (off_t
)vap
->va_data_size
); /* XXX error? */
2104 origsize
= np
->n_size
;
2105 np
->n_size
= np
->n_vattr
.nva_size
= vap
->va_data_size
;
2106 nfs_node_lock_force(np
);
2107 CLR(np
->n_flag
, NUPDATESIZE
);
2108 nfs_node_unlock(np
);
2109 FSDBG(512, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0001);
2111 } else if (VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
2112 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
2113 (vap
->va_vaflags
& VA_UTIMES_NULL
)) {
2114 if ((error
= nfs_node_lock(np
))) {
2117 if ((np
->n_flag
& NMODIFIED
) && (vnode_vtype(vp
) == VREG
)) {
2118 nfs_node_unlock(np
);
2119 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
2120 if (error
== EINTR
) {
2124 nfs_node_unlock(np
);
2127 if ((VATTR_IS_ACTIVE(vap
, va_mode
) || VATTR_IS_ACTIVE(vap
, va_uid
) || VATTR_IS_ACTIVE(vap
, va_gid
) ||
2128 VATTR_IS_ACTIVE(vap
, va_acl
) || VATTR_IS_ACTIVE(vap
, va_uuuid
) || VATTR_IS_ACTIVE(vap
, va_guuid
)) &&
2129 !(error
= nfs_node_lock(np
))) {
2130 NACCESSINVALIDATE(np
);
2131 nfs_node_unlock(np
);
2133 dvp
= vnode_getparent(vp
);
2134 vname
= vnode_getname(vp
);
2135 dnp
= (dvp
&& vname
) ? VTONFS(dvp
) : NULL
;
2137 if (nfs_node_set_busy(dnp
, vfs_context_thread(ctx
))) {
2139 vnode_putname(vname
);
2141 nfs_dulookup_init(&dul
, dnp
, vname
, strlen(vname
), ctx
);
2142 nfs_dulookup_start(&dul
, dnp
, ctx
);
2143 dul_in_progress
= 1;
2150 vnode_putname(vname
);
2157 error
= nmp
->nm_funcs
->nf_setattr_rpc(np
, vap
, ctx
);
2160 if (dul_in_progress
) {
2161 nfs_dulookup_finish(&dul
, dnp
, ctx
);
2162 nfs_node_clear_busy(dnp
);
2164 vnode_putname(vname
);
2167 FSDBG_BOT(512, np
->n_size
, vap
->va_data_size
, np
->n_vattr
.nva_size
, error
);
2168 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
2169 if (error
&& (origsize
!= np
->n_size
) &&
2170 ((nfsvers
< NFS_VER4
) || !nfs_mount_state_error_should_restart(error
))) {
2171 /* make every effort to resync file size w/ server... */
2172 /* (don't bother if we'll be restarting the operation) */
2173 int err
; /* preserve "error" for return */
2174 np
->n_size
= np
->n_vattr
.nva_size
= origsize
;
2175 nfs_node_lock_force(np
);
2176 CLR(np
->n_flag
, NUPDATESIZE
);
2177 nfs_node_unlock(np
);
2178 FSDBG(512, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0002);
2179 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX check error */
2180 vapsize
= vap
->va_data_size
;
2181 vap
->va_data_size
= origsize
;
2182 err
= nmp
->nm_funcs
->nf_setattr_rpc(np
, vap
, ctx
);
2184 NP(np
, "nfs_vnop_setattr: nfs%d_setattr_rpc %d %d", nfsvers
, error
, err
);
2186 vap
->va_data_size
= vapsize
;
2188 nfs_node_lock_force(np
);
2190 * The size was just set. If the size is already marked for update, don't
2191 * trust the newsize (it may have been set while the setattr was in progress).
2192 * Clear the update flag and make sure we fetch new attributes so we are sure
2193 * we have the latest size.
2195 if (ISSET(np
->n_flag
, NUPDATESIZE
)) {
2196 CLR(np
->n_flag
, NUPDATESIZE
);
2197 NATTRINVALIDATE(np
);
2198 nfs_node_unlock(np
);
2199 nfs_getattr(np
, NULL
, ctx
, NGA_UNCACHED
);
2201 nfs_node_unlock(np
);
2203 nfs_data_unlock(np
);
2204 if (nfsvers
>= NFS_VER4
) {
2206 /* don't close our setattr open if we'll be restarting... */
2207 if (!nfs_mount_state_error_should_restart(error
) &&
2208 (nofp
->nof_flags
& NFS_OPEN_FILE_SETATTR
)) {
2209 int err
= nfs_close(np
, nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
2211 NP(np
, "nfs_vnop_setattr: close error: %d", err
);
2213 nofp
->nof_flags
&= ~NFS_OPEN_FILE_SETATTR
;
2215 nfs_open_file_clear_busy(nofp
);
2218 if (nfs_mount_state_in_use_end(nmp
, error
)) {
2221 nfs_open_owner_rele(noop
);
2228 * Do an NFS setattr RPC.
2233 struct vnode_attr
*vap
,
2236 struct nfsmount
*nmp
= NFSTONMP(np
);
2237 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0, nfsvers
;
2238 u_int64_t xid
, nextxid
;
2239 struct nfsm_chain nmreq
, nmrep
;
2241 if (nfs_mount_gone(nmp
)) {
2244 nfsvers
= nmp
->nm_vers
;
2246 VATTR_SET_SUPPORTED(vap
, va_mode
);
2247 VATTR_SET_SUPPORTED(vap
, va_uid
);
2248 VATTR_SET_SUPPORTED(vap
, va_gid
);
2249 VATTR_SET_SUPPORTED(vap
, va_data_size
);
2250 VATTR_SET_SUPPORTED(vap
, va_access_time
);
2251 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
2253 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
2254 if (vap
->va_flags
) { /* we don't support setting flags */
2255 if (vap
->va_active
& ~VNODE_ATTR_va_flags
) {
2256 return EINVAL
; /* return EINVAL if other attributes also set */
2258 return ENOTSUP
; /* return ENOTSUP for chflags(2) */
2261 /* no flags set, so we'll just ignore it */
2262 if (!(vap
->va_active
& ~VNODE_ATTR_va_flags
)) {
2263 return 0; /* no (other) attributes to set, so nothing to do */
2267 nfsm_chain_null(&nmreq
);
2268 nfsm_chain_null(&nmrep
);
2270 nfsm_chain_build_alloc_init(error
, &nmreq
,
2271 NFSX_FH(nfsvers
) + NFSX_SATTR(nfsvers
));
2272 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
2273 if (nfsvers
== NFS_VER3
) {
2274 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
2275 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
2276 nfsm_chain_add_32(error
, &nmreq
, vap
->va_mode
);
2278 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2280 if (VATTR_IS_ACTIVE(vap
, va_uid
)) {
2281 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
2282 nfsm_chain_add_32(error
, &nmreq
, vap
->va_uid
);
2284 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2286 if (VATTR_IS_ACTIVE(vap
, va_gid
)) {
2287 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
2288 nfsm_chain_add_32(error
, &nmreq
, vap
->va_gid
);
2290 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2292 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
2293 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
2294 nfsm_chain_add_64(error
, &nmreq
, vap
->va_data_size
);
2296 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2298 if (vap
->va_vaflags
& VA_UTIMES_NULL
) {
2299 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_SERVER
);
2300 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_SERVER
);
2302 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
2303 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_CLIENT
);
2304 nfsm_chain_add_32(error
, &nmreq
, vap
->va_access_time
.tv_sec
);
2305 nfsm_chain_add_32(error
, &nmreq
, vap
->va_access_time
.tv_nsec
);
2307 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_DONT_CHANGE
);
2309 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
2310 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_CLIENT
);
2311 nfsm_chain_add_32(error
, &nmreq
, vap
->va_modify_time
.tv_sec
);
2312 nfsm_chain_add_32(error
, &nmreq
, vap
->va_modify_time
.tv_nsec
);
2314 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_DONT_CHANGE
);
2317 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2319 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_mode
) ?
2320 vtonfsv2_mode(vnode_vtype(NFSTOV(np
)), vap
->va_mode
) : -1);
2321 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_uid
) ?
2322 vap
->va_uid
: (uint32_t)-1);
2323 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_gid
) ?
2324 vap
->va_gid
: (uint32_t)-1);
2325 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_data_size
) ?
2326 vap
->va_data_size
: (uint32_t)-1);
2327 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
2328 nfsm_chain_add_32(error
, &nmreq
, vap
->va_access_time
.tv_sec
);
2329 nfsm_chain_add_32(error
, &nmreq
, (vap
->va_access_time
.tv_nsec
!= -1) ?
2330 ((uint32_t)vap
->va_access_time
.tv_nsec
/ 1000) : 0xffffffff);
2332 nfsm_chain_add_32(error
, &nmreq
, -1);
2333 nfsm_chain_add_32(error
, &nmreq
, -1);
2335 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
2336 nfsm_chain_add_32(error
, &nmreq
, vap
->va_modify_time
.tv_sec
);
2337 nfsm_chain_add_32(error
, &nmreq
, (vap
->va_modify_time
.tv_nsec
!= -1) ?
2338 ((uint32_t)vap
->va_modify_time
.tv_nsec
/ 1000) : 0xffffffff);
2340 nfsm_chain_add_32(error
, &nmreq
, -1);
2341 nfsm_chain_add_32(error
, &nmreq
, -1);
2344 nfsm_chain_build_done(error
, &nmreq
);
2346 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_SETATTR
, ctx
, NULL
, &nmrep
, &xid
, &status
);
2347 if ((lockerror
= nfs_node_lock(np
))) {
2350 if (nfsvers
== NFS_VER3
) {
2351 struct timespec premtime
= { 0, 0 };
2352 nfsm_chain_get_wcc_data(error
, &nmrep
, np
, &premtime
, &wccpostattr
, &xid
);
2354 /* if file hadn't changed, update cached mtime */
2355 if (nfstimespeccmp(&np
->n_mtime
, &premtime
, ==)) {
2356 NFS_CHANGED_UPDATE(nfsvers
, np
, &np
->n_vattr
);
2358 /* if directory hadn't changed, update namecache mtime */
2359 if ((vnode_vtype(NFSTOV(np
)) == VDIR
) &&
2360 nfstimespeccmp(&np
->n_ncmtime
, &premtime
, ==)) {
2361 NFS_CHANGED_UPDATE_NC(nfsvers
, np
, &np
->n_vattr
);
2364 NATTRINVALIDATE(np
);
2371 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
2374 * We just changed the attributes and we want to make sure that we
2375 * see the latest attributes. Get the next XID. If it's not the
2376 * next XID after the SETATTR XID, then it's possible that another
2377 * RPC was in flight at the same time and it might put stale attributes
2378 * in the cache. In that case, we invalidate the attributes and set
2379 * the attribute cache XID to guarantee that newer attributes will
2383 nfs_get_xid(&nextxid
);
2384 if (nextxid
!= (xid
+ 1)) {
2385 np
->n_xid
= nextxid
;
2386 NATTRINVALIDATE(np
);
2390 nfs_node_unlock(np
);
2392 nfsm_chain_cleanup(&nmreq
);
2393 nfsm_chain_cleanup(&nmrep
);
2398 * NFS lookup call, one step at a time...
2399 * First look in cache
2400 * If not found, unlock the directory nfsnode and do the RPC
2404 struct vnop_lookup_args
/* {
2405 * struct vnodeop_desc *a_desc;
2408 * struct componentname *a_cnp;
2409 * vfs_context_t a_context;
2412 vfs_context_t ctx
= ap
->a_context
;
2413 struct componentname
*cnp
= ap
->a_cnp
;
2414 vnode_t dvp
= ap
->a_dvp
;
2415 vnode_t
*vpp
= ap
->a_vpp
;
2416 int flags
= cnp
->cn_flags
;
2419 struct nfsmount
*nmp
;
2421 int nfsvers
, error
, busyerror
= ENOENT
, isdot
, isdotdot
, negnamecache
;
2423 struct nfs_vattr nvattr
;
2425 struct vnop_access_args naa
;
2427 struct nfsreq rq
, *req
= &rq
;
2432 NVATTR_INIT(&nvattr
);
2434 mp
= vnode_mount(dvp
);
2436 if (nfs_mount_gone(nmp
)) {
2440 nfsvers
= nmp
->nm_vers
;
2441 negnamecache
= !NMFLAG(nmp
, NONEGNAMECACHE
);
2443 if ((error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
)))) {
2446 /* nfs_getattr() will check changed and purge caches */
2447 if ((error
= nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
))) {
2451 error
= cache_lookup(dvp
, vpp
, cnp
);
2454 /* negative cache entry */
2458 if ((nfsvers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
)) {
2459 /* if rdirplus, try dir buf cache lookup */
2460 error
= nfs_dir_buf_cache_lookup(dnp
, &np
, cnp
, ctx
, 0);
2462 /* dir buf cache hit */
2467 if (error
!= -1) { /* cache miss */
2472 /* cache hit, not really an error */
2473 OSAddAtomic64(1, &nfsstats
.lookupcache_hits
);
2475 nfs_node_clear_busy(dnp
);
2478 /* check for directory access */
2479 naa
.a_desc
= &vnop_access_desc
;
2481 naa
.a_action
= KAUTH_VNODE_SEARCH
;
2482 naa
.a_context
= ctx
;
2484 /* compute actual success/failure based on accessibility */
2485 error
= nfs_vnop_access(&naa
);
2488 /* unexpected error from cache_lookup */
2492 /* skip lookup, if we know who we are: "." or ".." */
2493 isdot
= isdotdot
= 0;
2494 if (cnp
->cn_nameptr
[0] == '.') {
2495 if (cnp
->cn_namelen
== 1) {
2498 if ((cnp
->cn_namelen
== 2) && (cnp
->cn_nameptr
[1] == '.')) {
2502 if (isdotdot
|| isdot
) {
2506 if ((nfsvers
>= NFS_VER4
) && (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER
)) {
2507 /* we should never be looking things up in a trigger directory, return nothing */
2512 /* do we know this name is too long? */
2514 if (nfs_mount_gone(nmp
)) {
2518 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXNAME
) &&
2519 (cnp
->cn_namelen
> (int)nmp
->nm_fsattr
.nfsa_maxname
)) {
2520 error
= ENAMETOOLONG
;
2527 OSAddAtomic64(1, &nfsstats
.lookupcache_misses
);
2529 error
= nmp
->nm_funcs
->nf_lookup_rpc_async(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &req
);
2531 error
= nmp
->nm_funcs
->nf_lookup_rpc_async_finish(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, req
, &xid
, &fh
, &nvattr
);
2534 /* is the file handle the same as this directory's file handle? */
2535 isdot
= NFS_CMPFH(dnp
, fh
.fh_data
, fh
.fh_len
);
2538 if (flags
& ISLASTCN
) {
2539 switch (cnp
->cn_nameiop
) {
2541 cnp
->cn_flags
&= ~MAKEENTRY
;
2544 cnp
->cn_flags
&= ~MAKEENTRY
;
2554 newvp
= vnode_getparent(dvp
);
2560 error
= vnode_get(dvp
);
2565 nfs_node_lock_force(dnp
);
2566 if (fh
.fh_len
&& (dnp
->n_xid
<= xid
)) {
2567 nfs_loadattrcache(dnp
, &nvattr
, &xid
, 0);
2569 nfs_node_unlock(dnp
);
2571 ngflags
= (cnp
->cn_flags
& MAKEENTRY
) ? NG_MAKEENTRY
: 0;
2572 error
= nfs_nget(mp
, dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, ngflags
, &np
);
2577 nfs_node_unlock(np
);
2583 if (((cnp
->cn_nameiop
== CREATE
) || (cnp
->cn_nameiop
== RENAME
)) &&
2584 (flags
& ISLASTCN
) && (error
== ENOENT
)) {
2585 if (vnode_mount(dvp
) && vnode_vfsisrdonly(dvp
)) {
2588 error
= EJUSTRETURN
;
2592 if ((error
== ENOENT
) && (cnp
->cn_flags
& MAKEENTRY
) &&
2593 (cnp
->cn_nameiop
!= CREATE
) && negnamecache
) {
2594 /* add a negative entry in the name cache */
2595 nfs_node_lock_force(dnp
);
2596 cache_enter(dvp
, NULL
, cnp
);
2597 dnp
->n_flag
|= NNEGNCENTRIES
;
2598 nfs_node_unlock(dnp
);
2601 NVATTR_CLEANUP(&nvattr
);
2603 nfs_node_clear_busy(dnp
);
2605 if (error
&& *vpp
) {
2612 int nfs_readlink_nocache
= DEFAULT_READLINK_NOCACHE
;
2619 struct vnop_readlink_args
/* {
2620 * struct vnodeop_desc *a_desc;
2622 * struct uio *a_uio;
2623 * vfs_context_t a_context;
2626 vfs_context_t ctx
= ap
->a_context
;
2627 nfsnode_t np
= VTONFS(ap
->a_vp
);
2628 struct nfsmount
*nmp
;
2629 int error
= 0, nfsvers
;
2631 uio_t uio
= ap
->a_uio
;
2632 struct nfsbuf
*bp
= NULL
;
2636 if (vnode_vtype(ap
->a_vp
) != VLNK
) {
2640 if (uio_resid(uio
) == 0) {
2643 if (uio_offset(uio
) < 0) {
2647 nmp
= VTONMP(ap
->a_vp
);
2648 if (nfs_mount_gone(nmp
)) {
2651 nfsvers
= nmp
->nm_vers
;
2654 /* nfs_getattr() will check changed and purge caches */
2655 if ((error
= nfs_getattr(np
, NULL
, ctx
, nfs_readlink_nocache
? NGA_UNCACHED
: NGA_CACHED
))) {
2656 FSDBG(531, np
, 0xd1e0001, 0, error
);
2660 if (nfs_readlink_nocache
) {
2661 timeo
= nfs_attrcachetimeout(np
);
2666 OSAddAtomic64(1, &nfsstats
.biocache_readlinks
);
2667 error
= nfs_buf_get(np
, 0, NFS_MAXPATHLEN
, vfs_context_thread(ctx
), NBLK_META
, &bp
);
2669 FSDBG(531, np
, 0xd1e0002, 0, error
);
2673 if (nfs_readlink_nocache
) {
2674 NFS_VNOP_DBG("timeo = %d ts.tv_sec = %ld need refresh = %d cached = %d\n", timeo
, ts
.tv_sec
,
2675 (np
->n_rltim
.tv_sec
+ timeo
) < ts
.tv_sec
|| nfs_readlink_nocache
> 1,
2676 ISSET(bp
->nb_flags
, NB_CACHE
) == NB_CACHE
);
2677 /* n_rltim is synchronized by the associated nfs buf */
2678 if (ISSET(bp
->nb_flags
, NB_CACHE
) && ((nfs_readlink_nocache
> 1) || ((np
->n_rltim
.tv_sec
+ timeo
) < ts
.tv_sec
))) {
2679 SET(bp
->nb_flags
, NB_INVAL
);
2680 nfs_buf_release(bp
, 0);
2684 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) {
2686 OSAddAtomic64(1, &nfsstats
.readlink_bios
);
2687 buflen
= bp
->nb_bufsize
;
2688 error
= nmp
->nm_funcs
->nf_readlink_rpc(np
, bp
->nb_data
, &buflen
, ctx
);
2690 if (error
== ESTALE
) {
2691 NFS_VNOP_DBG("Stale FH from readlink rpc\n");
2692 error
= nfs_refresh_fh(np
, ctx
);
2697 SET(bp
->nb_flags
, NB_ERROR
);
2698 bp
->nb_error
= error
;
2699 NFS_VNOP_DBG("readlink failed %d\n", error
);
2701 bp
->nb_validoff
= 0;
2702 bp
->nb_validend
= buflen
;
2704 NFS_VNOP_DBG("readlink of %.*s\n", bp
->nb_validend
, (char *)bp
->nb_data
);
2707 NFS_VNOP_DBG("got cached link of %.*s\n", bp
->nb_validend
, (char *)bp
->nb_data
);
2710 if (!error
&& (bp
->nb_validend
> 0)) {
2711 error
= uiomove(bp
->nb_data
, bp
->nb_validend
, uio
);
2713 FSDBG(531, np
, bp
->nb_validend
, 0, error
);
2714 nfs_buf_release(bp
, 1);
2719 * Do a readlink RPC.
2722 nfs3_readlink_rpc(nfsnode_t np
, char *buf
, uint32_t *buflenp
, vfs_context_t ctx
)
2724 struct nfsmount
*nmp
;
2725 int error
= 0, lockerror
= ENOENT
, nfsvers
, status
;
2728 struct nfsm_chain nmreq
, nmrep
;
2731 if (nfs_mount_gone(nmp
)) {
2734 nfsvers
= nmp
->nm_vers
;
2735 nfsm_chain_null(&nmreq
);
2736 nfsm_chain_null(&nmrep
);
2738 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nfsvers
));
2739 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
2740 nfsm_chain_build_done(error
, &nmreq
);
2742 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_READLINK
, ctx
, NULL
, &nmrep
, &xid
, &status
);
2743 if ((lockerror
= nfs_node_lock(np
))) {
2746 if (nfsvers
== NFS_VER3
) {
2747 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
2752 nfsm_chain_get_32(error
, &nmrep
, len
);
2754 if ((nfsvers
== NFS_VER2
) && (len
> *buflenp
)) {
2758 if (len
>= *buflenp
) {
2759 if (np
->n_size
&& (np
->n_size
< *buflenp
)) {
2765 nfsm_chain_get_opaque(error
, &nmrep
, len
, buf
);
2771 nfs_node_unlock(np
);
2773 nfsm_chain_cleanup(&nmreq
);
2774 nfsm_chain_cleanup(&nmrep
);
2783 nfs_read_rpc(nfsnode_t np
, uio_t uio
, vfs_context_t ctx
)
2785 struct nfsmount
*nmp
;
2786 int error
= 0, nfsvers
, eof
= 0;
2787 size_t nmrsize
, len
, retlen
;
2790 struct nfsreq rq
, *req
= &rq
;
2791 uint32_t stategenid
= 0, restart
= 0;
2793 FSDBG_TOP(536, np
, uio_offset(uio
), uio_resid(uio
), 0);
2795 if (nfs_mount_gone(nmp
)) {
2798 nfsvers
= nmp
->nm_vers
;
2799 nmrsize
= nmp
->nm_rsize
;
2801 txoffset
= uio_offset(uio
);
2802 tsiz
= uio_resid(uio
);
2803 if ((nfsvers
== NFS_VER2
) && ((uint64_t)(txoffset
+ tsiz
) > 0xffffffffULL
)) {
2804 FSDBG_BOT(536, np
, uio_offset(uio
), uio_resid(uio
), EFBIG
);
2809 len
= retlen
= (tsiz
> (user_ssize_t
)nmrsize
) ? nmrsize
: (size_t)tsiz
;
2810 FSDBG(536, np
, txoffset
, len
, 0);
2811 if (np
->n_flag
& NREVOKE
) {
2815 if (nmp
->nm_vers
>= NFS_VER4
) {
2816 stategenid
= nmp
->nm_stategenid
;
2818 error
= nmp
->nm_funcs
->nf_read_rpc_async(np
, txoffset
, len
,
2819 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, &req
);
2821 error
= nmp
->nm_funcs
->nf_read_rpc_async_finish(np
, req
, uio
, &retlen
, &eof
);
2823 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
) &&
2824 (++restart
<= nfs_mount_state_max_restarts(nmp
))) { /* guard against no progress */
2825 lck_mtx_lock(&nmp
->nm_lock
);
2826 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
2827 NP(np
, "nfs_read_rpc: error %d, initiating recovery", error
);
2828 nfs_need_recover(nmp
, error
);
2830 lck_mtx_unlock(&nmp
->nm_lock
);
2831 if (np
->n_flag
& NREVOKE
) {
2834 if (error
== NFSERR_GRACE
) {
2835 tsleep(&nmp
->nm_state
, (PZERO
- 1), "nfsgrace", 2 * hz
);
2837 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
))) {
2847 if (nfsvers
!= NFS_VER2
) {
2848 if (eof
|| (retlen
== 0)) {
2851 } else if (retlen
< len
) {
2856 FSDBG_BOT(536, np
, eof
, uio_resid(uio
), error
);
2861 nfs3_read_rpc_async(
2867 struct nfsreq_cbinfo
*cb
,
2868 struct nfsreq
**reqp
)
2870 struct nfsmount
*nmp
;
2871 int error
= 0, nfsvers
;
2872 struct nfsm_chain nmreq
;
2875 if (nfs_mount_gone(nmp
)) {
2878 nfsvers
= nmp
->nm_vers
;
2880 nfsm_chain_null(&nmreq
);
2881 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nfsvers
) + 3 * NFSX_UNSIGNED
);
2882 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
2883 if (nfsvers
== NFS_VER3
) {
2884 nfsm_chain_add_64(error
, &nmreq
, offset
);
2885 nfsm_chain_add_32(error
, &nmreq
, len
);
2887 nfsm_chain_add_32(error
, &nmreq
, offset
);
2888 nfsm_chain_add_32(error
, &nmreq
, len
);
2889 nfsm_chain_add_32(error
, &nmreq
, 0);
2891 nfsm_chain_build_done(error
, &nmreq
);
2893 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC_READ
, thd
, cred
, NULL
, 0, cb
, reqp
);
2895 nfsm_chain_cleanup(&nmreq
);
2900 nfs3_read_rpc_async_finish(
2907 int error
= 0, lockerror
, nfsvers
, status
, eof
= 0;
2910 struct nfsmount
*nmp
;
2911 struct nfsm_chain nmrep
;
2914 if (nfs_mount_gone(nmp
)) {
2915 nfs_request_async_cancel(req
);
2918 nfsvers
= nmp
->nm_vers
;
2920 nfsm_chain_null(&nmrep
);
2922 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
2923 if (error
== EINPROGRESS
) { /* async request restarted */
2927 if ((lockerror
= nfs_node_lock(np
))) {
2930 if (nfsvers
== NFS_VER3
) {
2931 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
2936 if (nfsvers
== NFS_VER3
) {
2937 nfsm_chain_adv(error
, &nmrep
, NFSX_UNSIGNED
);
2938 nfsm_chain_get_32(error
, &nmrep
, eof
);
2940 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
2943 nfs_node_unlock(np
);
2945 nfsm_chain_get_32(error
, &nmrep
, retlen
);
2946 if ((nfsvers
== NFS_VER2
) && (retlen
> *lenp
)) {
2950 error
= nfsm_chain_get_uio(&nmrep
, MIN(retlen
, *lenp
), uio
);
2952 if (nfsvers
== NFS_VER3
) {
2953 if (!eof
&& !retlen
) {
2956 } else if (retlen
< *lenp
) {
2961 *lenp
= MIN(retlen
, *lenp
);
2963 nfsm_chain_cleanup(&nmrep
);
2972 struct vnop_write_args
/* {
2973 * struct vnodeop_desc *a_desc;
2975 * struct uio *a_uio;
2977 * vfs_context_t a_context;
2980 vfs_context_t ctx
= ap
->a_context
;
2981 uio_t uio
= ap
->a_uio
;
2982 vnode_t vp
= ap
->a_vp
;
2983 nfsnode_t np
= VTONFS(vp
);
2984 int ioflag
= ap
->a_ioflag
;
2986 struct nfsmount
*nmp
= VTONMP(vp
);
2989 int n
, on
, error
= 0;
2990 off_t boff
, start
, end
;
2992 char auio_buf
[UIO_SIZEOF(1)];
2996 FSDBG_TOP(515, np
, uio_offset(uio
), uio_resid(uio
), ioflag
);
2998 if (vnode_vtype(vp
) != VREG
) {
2999 FSDBG_BOT(515, np
, uio_offset(uio
), uio_resid(uio
), EIO
);
3003 thd
= vfs_context_thread(ctx
);
3004 cred
= vfs_context_ucred(ctx
);
3006 nfs_data_lock(np
, NFS_DATA_LOCK_SHARED
);
3008 if ((error
= nfs_node_lock(np
))) {
3009 nfs_data_unlock(np
);
3010 FSDBG_BOT(515, np
, uio_offset(uio
), uio_resid(uio
), error
);
3015 if (np
->n_flag
& NWRITEERR
) {
3016 error
= np
->n_error
;
3017 np
->n_flag
&= ~NWRITEERR
;
3019 if (np
->n_flag
& NNEEDINVALIDATE
) {
3020 np
->n_flag
&= ~NNEEDINVALIDATE
;
3021 nfs_node_unlock(np
);
3022 nfs_data_unlock(np
);
3023 nfs_vinvalbuf(vp
, V_SAVE
| V_IGNORE_WRITEERR
, ctx
, 1);
3024 nfs_data_lock(np
, NFS_DATA_LOCK_SHARED
);
3026 nfs_node_unlock(np
);
3032 biosize
= nmp
->nm_biosize
;
3034 if (ioflag
& (IO_APPEND
| IO_SYNC
)) {
3035 nfs_node_lock_force(np
);
3036 if (np
->n_flag
& NMODIFIED
) {
3037 NATTRINVALIDATE(np
);
3038 nfs_node_unlock(np
);
3039 nfs_data_unlock(np
);
3040 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
3041 nfs_data_lock(np
, NFS_DATA_LOCK_SHARED
);
3043 FSDBG(515, np
, uio_offset(uio
), 0x10bad01, error
);
3047 nfs_node_unlock(np
);
3049 if (ioflag
& IO_APPEND
) {
3050 nfs_data_unlock(np
);
3051 /* nfs_getattr() will check changed and purge caches */
3052 error
= nfs_getattr(np
, NULL
, ctx
, NGA_UNCACHED
);
3053 /* we'll be extending the file, so take the data lock exclusive */
3054 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
3056 FSDBG(515, np
, uio_offset(uio
), 0x10bad02, error
);
3059 uio_setoffset(uio
, np
->n_size
);
3062 if (uio_offset(uio
) < 0) {
3064 FSDBG_BOT(515, np
, uio_offset(uio
), 0xbad0ff, error
);
3067 if (uio_resid(uio
) == 0) {
3071 if (((uio_offset(uio
) + uio_resid(uio
)) > (off_t
)np
->n_size
) && !(ioflag
& IO_APPEND
)) {
3073 * It looks like we'll be extending the file, so take the data lock exclusive.
3075 nfs_data_unlock(np
);
3076 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
3079 * Also, if the write begins after the previous EOF buffer, make sure to zero
3080 * and validate the new bytes in that buffer.
3082 struct nfsbuf
*eofbp
= NULL
;
3083 daddr64_t eofbn
= np
->n_size
/ biosize
;
3084 int eofoff
= np
->n_size
% biosize
;
3085 lbn
= uio_offset(uio
) / biosize
;
3087 if (eofoff
&& (eofbn
< lbn
)) {
3088 if ((error
= nfs_buf_get(np
, eofbn
, biosize
, thd
, NBLK_WRITE
| NBLK_ONLYVALID
, &eofbp
))) {
3091 np
->n_size
+= (biosize
- eofoff
);
3092 nfs_node_lock_force(np
);
3093 CLR(np
->n_flag
, NUPDATESIZE
);
3094 np
->n_flag
|= NMODIFIED
;
3095 nfs_node_unlock(np
);
3096 FSDBG(516, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0001);
3097 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX errors */
3100 * For the old last page, don't zero bytes if there
3101 * are invalid bytes in that page (i.e. the page isn't
3103 * For pages after the old last page, zero them and
3104 * mark them as valid.
3108 if (ioflag
& IO_NOCACHE
) {
3109 SET(eofbp
->nb_flags
, NB_NOCACHE
);
3112 FSDBG(516, eofbp
, eofoff
, biosize
- eofoff
, 0xe0fff01e);
3114 i
= eofoff
/ PAGE_SIZE
;
3115 while (eofoff
< biosize
) {
3116 int poff
= eofoff
& PAGE_MASK
;
3117 if (!poff
|| NBPGVALID(eofbp
, i
)) {
3118 bzero(d
+ eofoff
, PAGE_SIZE
- poff
);
3119 NBPGVALID_SET(eofbp
, i
);
3121 eofoff
+= PAGE_SIZE
- poff
;
3124 nfs_buf_release(eofbp
, 1);
3130 OSAddAtomic64(1, &nfsstats
.biocache_writes
);
3131 lbn
= uio_offset(uio
) / biosize
;
3132 on
= uio_offset(uio
) % biosize
;
3134 if (uio_resid(uio
) < n
) {
3139 * Get a cache block for writing. The range to be written is
3140 * (off..off+n) within the block. We ensure that the block
3141 * either has no dirty region or that the given range is
3142 * contiguous with the existing dirty region.
3144 error
= nfs_buf_get(np
, lbn
, biosize
, thd
, NBLK_WRITE
, &bp
);
3148 /* map the block because we know we're going to write to it */
3151 if (ioflag
& IO_NOCACHE
) {
3152 SET(bp
->nb_flags
, NB_NOCACHE
);
3155 if (!IS_VALID_CRED(bp
->nb_wcred
)) {
3156 kauth_cred_ref(cred
);
3157 bp
->nb_wcred
= cred
;
3161 * If there's already a dirty range AND dirty pages in this block we
3162 * need to send a commit AND write the dirty pages before continuing.
3164 * If there's already a dirty range OR dirty pages in this block
3165 * and the new write range is not contiguous with the existing range,
3166 * then force the buffer to be written out now.
3167 * (We used to just extend the dirty range to cover the valid,
3168 * but unwritten, data in between also. But writing ranges
3169 * of data that weren't actually written by an application
3170 * risks overwriting some other client's data with stale data
3171 * that's just masquerading as new written data.)
3173 if (bp
->nb_dirtyend
> 0) {
3174 if (on
> bp
->nb_dirtyend
|| (on
+ n
) < bp
->nb_dirtyoff
|| bp
->nb_dirty
) {
3175 FSDBG(515, np
, uio_offset(uio
), bp
, 0xd15c001);
3176 /* write/commit buffer "synchronously" */
3177 /* (NB_STABLE indicates that data writes should be FILESYNC) */
3178 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
));
3179 SET(bp
->nb_flags
, (NB_ASYNC
| NB_STABLE
));
3180 error
= nfs_buf_write(bp
);
3186 } else if (bp
->nb_dirty
) {
3187 int firstpg
, lastpg
;
3189 /* calculate write range pagemask */
3190 firstpg
= on
/ PAGE_SIZE
;
3191 lastpg
= (on
+ n
- 1) / PAGE_SIZE
;
3192 pagemask
= ((1 << (lastpg
+ 1)) - 1) & ~((1 << firstpg
) - 1);
3193 /* check if there are dirty pages outside the write range */
3194 if (bp
->nb_dirty
& ~pagemask
) {
3195 FSDBG(515, np
, uio_offset(uio
), bp
, 0xd15c002);
3196 /* write/commit buffer "synchronously" */
3197 /* (NB_STABLE indicates that data writes should be FILESYNC) */
3198 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
));
3199 SET(bp
->nb_flags
, (NB_ASYNC
| NB_STABLE
));
3200 error
= nfs_buf_write(bp
);
3206 /* if the first or last pages are already dirty */
3207 /* make sure that the dirty range encompasses those pages */
3208 if (NBPGDIRTY(bp
, firstpg
) || NBPGDIRTY(bp
, lastpg
)) {
3209 FSDBG(515, np
, uio_offset(uio
), bp
, 0xd15c003);
3210 bp
->nb_dirtyoff
= min(on
, firstpg
* PAGE_SIZE
);
3211 if (NBPGDIRTY(bp
, lastpg
)) {
3212 bp
->nb_dirtyend
= (lastpg
+ 1) * PAGE_SIZE
;
3214 if (NBOFF(bp
) + bp
->nb_dirtyend
> (off_t
)np
->n_size
) {
3215 bp
->nb_dirtyend
= np
->n_size
- NBOFF(bp
);
3216 if (bp
->nb_dirtyoff
>= bp
->nb_dirtyend
) {
3217 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
3221 bp
->nb_dirtyend
= on
+ n
;
3227 * Are we extending the size of the file with this write?
3228 * If so, update file size now that we have the block.
3229 * If there was a partial buf at the old eof, validate
3230 * and zero the new bytes.
3232 if ((uio_offset(uio
) + n
) > (off_t
)np
->n_size
) {
3233 daddr64_t eofbn
= np
->n_size
/ biosize
;
3234 int neweofoff
= (uio_offset(uio
) + n
) % biosize
;
3236 FSDBG(515, 0xb1ffa000, uio_offset(uio
) + n
, eofoff
, neweofoff
);
3238 /* if we're extending within the same last block */
3239 /* and the block is flagged as being cached... */
3240 if ((lbn
== eofbn
) && ISSET(bp
->nb_flags
, NB_CACHE
)) {
3241 /* ...check that all pages in buffer are valid */
3242 int endpg
= ((neweofoff
? neweofoff
: biosize
) - 1) / PAGE_SIZE
;
3244 /* pagemask only has to extend to last page being written to */
3245 pagemask
= (1 << (endpg
+ 1)) - 1;
3246 FSDBG(515, 0xb1ffa001, bp
->nb_valid
, pagemask
, 0);
3247 if ((bp
->nb_valid
& pagemask
) != pagemask
) {
3248 /* zerofill any hole */
3249 if (on
> bp
->nb_validend
) {
3251 for (i
= bp
->nb_validend
/ PAGE_SIZE
; i
<= (on
- 1) / PAGE_SIZE
; i
++) {
3252 NBPGVALID_SET(bp
, i
);
3255 FSDBG(516, bp
, bp
->nb_validend
, on
- bp
->nb_validend
, 0xf01e);
3256 bzero((char *)bp
->nb_data
+ bp
->nb_validend
,
3257 on
- bp
->nb_validend
);
3259 /* zerofill any trailing data in the last page */
3262 FSDBG(516, bp
, neweofoff
, PAGE_SIZE
- (neweofoff
& PAGE_MASK
), 0xe0f);
3263 bzero((char *)bp
->nb_data
+ neweofoff
,
3264 PAGE_SIZE
- (neweofoff
& PAGE_MASK
));
3268 np
->n_size
= uio_offset(uio
) + n
;
3269 nfs_node_lock_force(np
);
3270 CLR(np
->n_flag
, NUPDATESIZE
);
3271 np
->n_flag
|= NMODIFIED
;
3272 nfs_node_unlock(np
);
3273 FSDBG(516, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0001);
3274 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX errors */
3277 * If dirtyend exceeds file size, chop it down. This should
3278 * not occur unless there is a race.
3280 if (NBOFF(bp
) + bp
->nb_dirtyend
> (off_t
)np
->n_size
) {
3281 bp
->nb_dirtyend
= np
->n_size
- NBOFF(bp
);
3282 if (bp
->nb_dirtyoff
>= bp
->nb_dirtyend
) {
3283 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
3287 * UBC doesn't handle partial pages, so we need to make sure
3288 * that any pages left in the page cache are completely valid.
3290 * Writes that are smaller than a block are delayed if they
3291 * don't extend to the end of the block.
3293 * If the block isn't (completely) cached, we may need to read
3294 * in some parts of pages that aren't covered by the write.
3295 * If the write offset (on) isn't page aligned, we'll need to
3296 * read the start of the first page being written to. Likewise,
3297 * if the offset of the end of the write (on+n) isn't page aligned,
3298 * we'll need to read the end of the last page being written to.
3301 * We don't want to read anything we're just going to write over.
3302 * We don't want to read anything we're just going drop when the
3303 * I/O is complete (i.e. don't do reads for NOCACHE requests).
3304 * We don't want to issue multiple I/Os if we don't have to
3305 * (because they're synchronous rpcs).
3306 * We don't want to read anything we already have modified in the
3309 if (!ISSET(bp
->nb_flags
, NB_CACHE
) && (n
< biosize
)) {
3310 int firstpg
, lastpg
, dirtypg
;
3311 int firstpgoff
, lastpgoff
;
3313 firstpg
= on
/ PAGE_SIZE
;
3314 firstpgoff
= on
& PAGE_MASK
;
3315 lastpg
= (on
+ n
- 1) / PAGE_SIZE
;
3316 lastpgoff
= (on
+ n
) & PAGE_MASK
;
3317 if (firstpgoff
&& !NBPGVALID(bp
, firstpg
)) {
3318 /* need to read start of first page */
3319 start
= firstpg
* PAGE_SIZE
;
3320 end
= start
+ firstpgoff
;
3322 if (lastpgoff
&& !NBPGVALID(bp
, lastpg
)) {
3323 /* need to read end of last page */
3325 start
= (lastpg
* PAGE_SIZE
) + lastpgoff
;
3327 end
= (lastpg
+ 1) * PAGE_SIZE
;
3329 if (ISSET(bp
->nb_flags
, NB_NOCACHE
)) {
3331 * For nocache writes, if there is any partial page at the
3332 * start or end of the write range, then we do the write
3333 * synchronously to make sure that we can drop the data
3334 * from the cache as soon as the WRITE finishes. Normally,
3335 * we would do an unstable write and not drop the data until
3336 * it was committed. But doing that here would risk allowing
3337 * invalid data to be read from the cache between the WRITE
3339 * (NB_STABLE indicates that data writes should be FILESYNC)
3342 SET(bp
->nb_flags
, NB_STABLE
);
3347 /* need to read the data in range: start...end-1 */
3349 /* first, check for dirty pages in between */
3350 /* if there are, we'll have to do two reads because */
3351 /* we don't want to overwrite the dirty pages. */
3352 for (dirtypg
= start
/ PAGE_SIZE
; dirtypg
<= (end
- 1) / PAGE_SIZE
; dirtypg
++) {
3353 if (NBPGDIRTY(bp
, dirtypg
)) {
3358 /* if start is at beginning of page, try */
3359 /* to get any preceeding pages as well. */
3360 if (!(start
& PAGE_MASK
)) {
3361 /* stop at next dirty/valid page or start of block */
3362 for (; start
> 0; start
-= PAGE_SIZE
) {
3363 if (NBPGVALID(bp
, ((start
- 1) / PAGE_SIZE
))) {
3370 /* setup uio for read(s) */
3372 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
3373 &auio_buf
, sizeof(auio_buf
));
3375 if (dirtypg
<= (end
- 1) / PAGE_SIZE
) {
3376 /* there's a dirty page in the way, so just do two reads */
3377 /* we'll read the preceding data here */
3378 uio_reset(auio
, boff
+ start
, UIO_SYSSPACE
, UIO_READ
);
3379 uio_addiov(auio
, CAST_USER_ADDR_T(bp
->nb_data
+ start
), on
- start
);
3380 error
= nfs_read_rpc(np
, auio
, ctx
);
3382 /* couldn't read the data, so treat buffer as synchronous NOCACHE */
3383 SET(bp
->nb_flags
, (NB_NOCACHE
| NB_STABLE
));
3386 if (uio_resid(auio
) > 0) {
3387 FSDBG(516, bp
, (caddr_t
)uio_curriovbase(auio
) - bp
->nb_data
, uio_resid(auio
), 0xd00dee01);
3388 bzero(CAST_DOWN(caddr_t
, uio_curriovbase(auio
)), uio_resid(auio
));
3391 /* update validoff/validend if necessary */
3392 if ((bp
->nb_validoff
< 0) || (bp
->nb_validoff
> start
)) {
3393 bp
->nb_validoff
= start
;
3395 if ((bp
->nb_validend
< 0) || (bp
->nb_validend
< on
)) {
3396 bp
->nb_validend
= on
;
3398 if ((off_t
)np
->n_size
> boff
+ bp
->nb_validend
) {
3399 bp
->nb_validend
= min(np
->n_size
- (boff
+ start
), biosize
);
3401 /* validate any pages before the write offset */
3402 for (; start
< on
/ PAGE_SIZE
; start
+= PAGE_SIZE
) {
3403 NBPGVALID_SET(bp
, start
/ PAGE_SIZE
);
3406 /* adjust start to read any trailing data */
3410 /* if end is at end of page, try to */
3411 /* get any following pages as well. */
3412 if (!(end
& PAGE_MASK
)) {
3413 /* stop at next valid page or end of block */
3414 for (; end
< biosize
; end
+= PAGE_SIZE
) {
3415 if (NBPGVALID(bp
, end
/ PAGE_SIZE
)) {
3421 if (((boff
+ start
) >= (off_t
)np
->n_size
) ||
3422 ((start
>= on
) && ((boff
+ on
+ n
) >= (off_t
)np
->n_size
))) {
3424 * Either this entire read is beyond the current EOF
3425 * or the range that we won't be modifying (on+n...end)
3426 * is all beyond the current EOF.
3427 * No need to make a trip across the network to
3428 * read nothing. So, just zero the buffer instead.
3430 FSDBG(516, bp
, start
, end
- start
, 0xd00dee00);
3431 bzero(bp
->nb_data
+ start
, end
- start
);
3434 /* now we'll read the (rest of the) data */
3435 uio_reset(auio
, boff
+ start
, UIO_SYSSPACE
, UIO_READ
);
3436 uio_addiov(auio
, CAST_USER_ADDR_T(bp
->nb_data
+ start
), end
- start
);
3437 error
= nfs_read_rpc(np
, auio
, ctx
);
3439 /* couldn't read the data, so treat buffer as synchronous NOCACHE */
3440 SET(bp
->nb_flags
, (NB_NOCACHE
| NB_STABLE
));
3443 if (uio_resid(auio
) > 0) {
3444 FSDBG(516, bp
, (caddr_t
)uio_curriovbase(auio
) - bp
->nb_data
, uio_resid(auio
), 0xd00dee02);
3445 bzero(CAST_DOWN(caddr_t
, uio_curriovbase(auio
)), uio_resid(auio
));
3449 /* update validoff/validend if necessary */
3450 if ((bp
->nb_validoff
< 0) || (bp
->nb_validoff
> start
)) {
3451 bp
->nb_validoff
= start
;
3453 if ((bp
->nb_validend
< 0) || (bp
->nb_validend
< end
)) {
3454 bp
->nb_validend
= end
;
3456 if ((off_t
)np
->n_size
> boff
+ bp
->nb_validend
) {
3457 bp
->nb_validend
= min(np
->n_size
- (boff
+ start
), biosize
);
3459 /* validate any pages before the write offset's page */
3460 for (; start
< (off_t
)trunc_page_32(on
); start
+= PAGE_SIZE
) {
3461 NBPGVALID_SET(bp
, start
/ PAGE_SIZE
);
3463 /* validate any pages after the range of pages being written to */
3464 for (; (end
- 1) > (off_t
)round_page_32(on
+ n
- 1); end
-= PAGE_SIZE
) {
3465 NBPGVALID_SET(bp
, (end
- 1) / PAGE_SIZE
);
3468 /* Note: pages being written to will be validated when written */
3473 if (ISSET(bp
->nb_flags
, NB_ERROR
)) {
3474 error
= bp
->nb_error
;
3475 nfs_buf_release(bp
, 1);
3479 nfs_node_lock_force(np
);
3480 np
->n_flag
|= NMODIFIED
;
3481 nfs_node_unlock(np
);
3484 error
= uiomove((char *)bp
->nb_data
+ on
, n
, uio
);
3486 SET(bp
->nb_flags
, NB_ERROR
);
3487 nfs_buf_release(bp
, 1);
3491 /* validate any pages written to */
3492 start
= on
& ~PAGE_MASK
;
3493 for (; start
< on
+ n
; start
+= PAGE_SIZE
) {
3494 NBPGVALID_SET(bp
, start
/ PAGE_SIZE
);
3496 * This may seem a little weird, but we don't actually set the
3497 * dirty bits for writes. This is because we keep the dirty range
3498 * in the nb_dirtyoff/nb_dirtyend fields. Also, particularly for
3499 * delayed writes, when we give the pages back to the VM we don't
3500 * want to keep them marked dirty, because when we later write the
3501 * buffer we won't be able to tell which pages were written dirty
3502 * and which pages were mmapped and dirtied.
3505 if (bp
->nb_dirtyend
> 0) {
3506 bp
->nb_dirtyoff
= min(on
, bp
->nb_dirtyoff
);
3507 bp
->nb_dirtyend
= max((on
+ n
), bp
->nb_dirtyend
);
3509 bp
->nb_dirtyoff
= on
;
3510 bp
->nb_dirtyend
= on
+ n
;
3512 if (bp
->nb_validend
<= 0 || bp
->nb_validend
< bp
->nb_dirtyoff
||
3513 bp
->nb_validoff
> bp
->nb_dirtyend
) {
3514 bp
->nb_validoff
= bp
->nb_dirtyoff
;
3515 bp
->nb_validend
= bp
->nb_dirtyend
;
3517 bp
->nb_validoff
= min(bp
->nb_validoff
, bp
->nb_dirtyoff
);
3518 bp
->nb_validend
= max(bp
->nb_validend
, bp
->nb_dirtyend
);
3520 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) {
3521 nfs_buf_normalize_valid_range(np
, bp
);
3525 * Since this block is being modified, it must be written
3526 * again and not just committed.
3528 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
3529 nfs_node_lock_force(np
);
3530 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
3531 np
->n_needcommitcnt
--;
3532 CHECK_NEEDCOMMITCNT(np
);
3534 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
3535 nfs_node_unlock(np
);
3538 if (ioflag
& IO_SYNC
) {
3539 error
= nfs_buf_write(bp
);
3543 } else if (((n
+ on
) == biosize
) || (ioflag
& IO_APPEND
) ||
3544 (ioflag
& IO_NOCACHE
) || ISSET(bp
->nb_flags
, NB_NOCACHE
)) {
3545 SET(bp
->nb_flags
, NB_ASYNC
);
3546 error
= nfs_buf_write(bp
);
3551 /* If the block wasn't already delayed: charge for the write */
3552 if (!ISSET(bp
->nb_flags
, NB_DELWRI
)) {
3553 proc_t p
= vfs_context_proc(ctx
);
3554 if (p
&& p
->p_stats
) {
3555 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_oublock
);
3558 nfs_buf_write_delayed(bp
);
3560 if (np
->n_needcommitcnt
>= NFS_A_LOT_OF_NEEDCOMMITS
) {
3561 nfs_flushcommits(np
, 1);
3563 } while (uio_resid(uio
) > 0 && n
> 0);
3566 nfs_node_lock_force(np
);
3568 nfs_node_unlock(np
);
3569 nfs_data_unlock(np
);
3570 FSDBG_BOT(515, np
, uio_offset(uio
), uio_resid(uio
), error
);
3586 return nfs_write_rpc2(np
, uio
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
), iomodep
, wverfp
);
3598 struct nfsmount
*nmp
;
3599 int error
= 0, nfsvers
;
3600 int wverfset
, commit
, committed
;
3601 uint64_t wverf
= 0, wverf2
;
3602 size_t nmwsize
, totalsize
, tsiz
, len
, rlen
;
3603 struct nfsreq rq
, *req
= &rq
;
3604 uint32_t stategenid
= 0, vrestart
= 0, restart
= 0;
3605 uio_t uio_save
= NULL
;
3608 /* XXX limitation based on need to back up uio on short write */
3609 if (uio_iovcnt(uio
) != 1) {
3610 panic("nfs3_write_rpc: iovcnt > 1");
3613 FSDBG_TOP(537, np
, uio_offset(uio
), uio_resid(uio
), *iomodep
);
3615 if (nfs_mount_gone(nmp
)) {
3618 nfsvers
= nmp
->nm_vers
;
3619 nmwsize
= nmp
->nm_wsize
;
3622 committed
= NFS_WRITE_FILESYNC
;
3624 totalsize
= tsiz
= uio_resid(uio
);
3625 if ((nfsvers
== NFS_VER2
) && ((uint64_t)(uio_offset(uio
) + tsiz
) > 0xffffffffULL
)) {
3626 FSDBG_BOT(537, np
, uio_offset(uio
), uio_resid(uio
), EFBIG
);
3630 uio_save
= uio_duplicate(uio
);
3631 if (uio_save
== NULL
) {
3636 len
= (tsiz
> nmwsize
) ? nmwsize
: tsiz
;
3637 FSDBG(537, np
, uio_offset(uio
), len
, 0);
3638 if (np
->n_flag
& NREVOKE
) {
3642 if (nmp
->nm_vers
>= NFS_VER4
) {
3643 stategenid
= nmp
->nm_stategenid
;
3645 error
= nmp
->nm_funcs
->nf_write_rpc_async(np
, uio
, len
, thd
, cred
, *iomodep
, NULL
, &req
);
3647 error
= nmp
->nm_funcs
->nf_write_rpc_async_finish(np
, req
, &commit
, &rlen
, &wverf2
);
3650 if (nfs_mount_gone(nmp
)) {
3653 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
) &&
3654 (++restart
<= nfs_mount_state_max_restarts(nmp
))) { /* guard against no progress */
3655 lck_mtx_lock(&nmp
->nm_lock
);
3656 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
3657 NP(np
, "nfs_write_rpc: error %d, initiating recovery", error
);
3658 nfs_need_recover(nmp
, error
);
3660 lck_mtx_unlock(&nmp
->nm_lock
);
3661 if (np
->n_flag
& NREVOKE
) {
3664 if (error
== NFSERR_GRACE
) {
3665 tsleep(&nmp
->nm_state
, (PZERO
- 1), "nfsgrace", 2 * hz
);
3667 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
))) {
3675 if (nfsvers
== NFS_VER2
) {
3680 /* check for a short write */
3682 /* Reset the uio to reflect the actual transfer */
3684 uio_update(uio
, totalsize
- (tsiz
- rlen
));
3688 /* return lowest commit level returned */
3689 if (commit
< committed
) {
3695 /* check write verifier */
3699 } else if (wverf
!= wverf2
) {
3700 /* verifier changed, so we need to restart all the writes */
3701 if (++vrestart
> 100) {
3702 /* give up after too many restarts */
3706 *uio
= *uio_save
; // Reset the uio back to the start
3707 committed
= NFS_WRITE_FILESYNC
;
3715 if (wverfset
&& wverfp
) {
3718 *iomodep
= committed
;
3720 uio_setresid(uio
, tsiz
);
3722 FSDBG_BOT(537, np
, committed
, uio_resid(uio
), error
);
3727 nfs3_write_rpc_async(
3734 struct nfsreq_cbinfo
*cb
,
3735 struct nfsreq
**reqp
)
3737 struct nfsmount
*nmp
;
3739 int error
= 0, nfsvers
;
3740 struct nfsm_chain nmreq
;
3743 if (nfs_mount_gone(nmp
)) {
3746 nfsvers
= nmp
->nm_vers
;
3748 /* for async mounts, don't bother sending sync write requests */
3749 if ((iomode
!= NFS_WRITE_UNSTABLE
) && nfs_allow_async
&&
3750 ((mp
= NFSTOMP(np
))) && (vfs_flags(mp
) & MNT_ASYNC
)) {
3751 iomode
= NFS_WRITE_UNSTABLE
;
3754 nfsm_chain_null(&nmreq
);
3755 nfsm_chain_build_alloc_init(error
, &nmreq
,
3756 NFSX_FH(nfsvers
) + 5 * NFSX_UNSIGNED
+ nfsm_rndup(len
));
3757 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
3758 if (nfsvers
== NFS_VER3
) {
3759 nfsm_chain_add_64(error
, &nmreq
, uio_offset(uio
));
3760 nfsm_chain_add_32(error
, &nmreq
, len
);
3761 nfsm_chain_add_32(error
, &nmreq
, iomode
);
3763 nfsm_chain_add_32(error
, &nmreq
, 0);
3764 nfsm_chain_add_32(error
, &nmreq
, uio_offset(uio
));
3765 nfsm_chain_add_32(error
, &nmreq
, 0);
3767 nfsm_chain_add_32(error
, &nmreq
, len
);
3769 error
= nfsm_chain_add_uio(&nmreq
, uio
, len
);
3770 nfsm_chain_build_done(error
, &nmreq
);
3772 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC_WRITE
, thd
, cred
, NULL
, 0, cb
, reqp
);
3774 nfsm_chain_cleanup(&nmreq
);
3779 nfs3_write_rpc_async_finish(
3786 struct nfsmount
*nmp
;
3787 int error
= 0, lockerror
= ENOENT
, nfsvers
, status
;
3788 int updatemtime
= 0, wccpostattr
= 0, rlen
, committed
= NFS_WRITE_FILESYNC
;
3789 u_int64_t xid
, wverf
;
3791 struct nfsm_chain nmrep
;
3794 if (nfs_mount_gone(nmp
)) {
3795 nfs_request_async_cancel(req
);
3798 nfsvers
= nmp
->nm_vers
;
3800 nfsm_chain_null(&nmrep
);
3802 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
3803 if (error
== EINPROGRESS
) { /* async request restarted */
3807 if (nfs_mount_gone(nmp
)) {
3810 if (!error
&& (lockerror
= nfs_node_lock(np
))) {
3813 if (nfsvers
== NFS_VER3
) {
3814 struct timespec premtime
= { 0, 0 };
3815 nfsm_chain_get_wcc_data(error
, &nmrep
, np
, &premtime
, &wccpostattr
, &xid
);
3816 if (nfstimespeccmp(&np
->n_mtime
, &premtime
, ==)) {
3822 nfsm_chain_get_32(error
, &nmrep
, rlen
);
3828 nfsm_chain_get_32(error
, &nmrep
, committed
);
3829 nfsm_chain_get_64(error
, &nmrep
, wverf
);
3834 lck_mtx_lock(&nmp
->nm_lock
);
3835 if (!(nmp
->nm_state
& NFSSTA_HASWRITEVERF
)) {
3836 nmp
->nm_verf
= wverf
;
3837 nmp
->nm_state
|= NFSSTA_HASWRITEVERF
;
3838 } else if (nmp
->nm_verf
!= wverf
) {
3839 nmp
->nm_verf
= wverf
;
3841 lck_mtx_unlock(&nmp
->nm_lock
);
3846 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
3850 NFS_CHANGED_UPDATE(nfsvers
, np
, &np
->n_vattr
);
3854 nfs_node_unlock(np
);
3856 nfsm_chain_cleanup(&nmrep
);
3857 if ((committed
!= NFS_WRITE_FILESYNC
) && nfs_allow_async
&&
3858 ((mp
= NFSTOMP(np
))) && (vfs_flags(mp
) & MNT_ASYNC
)) {
3859 committed
= NFS_WRITE_FILESYNC
;
3861 *iomodep
= committed
;
3866 * NFS mknod vnode op
3868 * For NFS v2 this is a kludge. Use a create RPC but with the IFMT bits of the
3869 * mode set to specify the file type and the size field for rdev.
3873 struct vnop_mknod_args
/* {
3874 * struct vnodeop_desc *a_desc;
3877 * struct componentname *a_cnp;
3878 * struct vnode_attr *a_vap;
3879 * vfs_context_t a_context;
3882 vnode_t dvp
= ap
->a_dvp
;
3883 vnode_t
*vpp
= ap
->a_vpp
;
3884 struct componentname
*cnp
= ap
->a_cnp
;
3885 struct vnode_attr
*vap
= ap
->a_vap
;
3886 vfs_context_t ctx
= ap
->a_context
;
3887 vnode_t newvp
= NULL
;
3888 nfsnode_t np
= NULL
;
3889 struct nfsmount
*nmp
;
3890 nfsnode_t dnp
= VTONFS(dvp
);
3891 struct nfs_vattr nvattr
;
3893 int error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0;
3894 struct timespec premtime
= { 0, 0 };
3896 u_int64_t xid
= 0, dxid
;
3897 int nfsvers
, gotuid
, gotgid
;
3898 struct nfsm_chain nmreq
, nmrep
;
3899 struct nfsreq rq
, *req
= &rq
;
3902 if (nfs_mount_gone(nmp
)) {
3905 nfsvers
= nmp
->nm_vers
;
3907 if (!VATTR_IS_ACTIVE(vap
, va_type
)) {
3910 if (vap
->va_type
== VCHR
|| vap
->va_type
== VBLK
) {
3911 if (!VATTR_IS_ACTIVE(vap
, va_rdev
)) {
3914 rdev
= vap
->va_rdev
;
3915 } else if (vap
->va_type
== VFIFO
|| vap
->va_type
== VSOCK
) {
3920 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
)) {
3921 return ENAMETOOLONG
;
3924 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
3926 VATTR_SET_SUPPORTED(vap
, va_mode
);
3927 VATTR_SET_SUPPORTED(vap
, va_uid
);
3928 VATTR_SET_SUPPORTED(vap
, va_gid
);
3929 VATTR_SET_SUPPORTED(vap
, va_data_size
);
3930 VATTR_SET_SUPPORTED(vap
, va_access_time
);
3931 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
3932 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
3933 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
3935 nfsm_chain_null(&nmreq
);
3936 nfsm_chain_null(&nmrep
);
3938 nfsm_chain_build_alloc_init(error
, &nmreq
,
3939 NFSX_FH(nfsvers
) + 4 * NFSX_UNSIGNED
+
3940 nfsm_rndup(cnp
->cn_namelen
) + NFSX_SATTR(nfsvers
));
3941 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
3942 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
3943 if (nfsvers
== NFS_VER3
) {
3944 nfsm_chain_add_32(error
, &nmreq
, vtonfs_type(vap
->va_type
, nfsvers
));
3945 nfsm_chain_add_v3sattr(error
, &nmreq
, vap
);
3946 if (vap
->va_type
== VCHR
|| vap
->va_type
== VBLK
) {
3947 nfsm_chain_add_32(error
, &nmreq
, major(vap
->va_rdev
));
3948 nfsm_chain_add_32(error
, &nmreq
, minor(vap
->va_rdev
));
3951 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, rdev
);
3953 nfsm_chain_build_done(error
, &nmreq
);
3955 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
3959 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_MKNOD
,
3960 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
3962 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
3965 if ((lockerror
= nfs_node_lock(dnp
))) {
3968 /* XXX no EEXIST kludge here? */
3970 if (!error
&& !status
) {
3971 if (dnp
->n_flag
& NNEGNCENTRIES
) {
3972 dnp
->n_flag
&= ~NNEGNCENTRIES
;
3973 cache_purge_negatives(dvp
);
3975 error
= nfsm_chain_get_fh_attr(&nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
3977 if (nfsvers
== NFS_VER3
) {
3978 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
3984 nfsm_chain_cleanup(&nmreq
);
3985 nfsm_chain_cleanup(&nmrep
);
3988 dnp
->n_flag
|= NMODIFIED
;
3989 /* if directory hadn't changed, update namecache mtime */
3990 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==)) {
3991 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
3993 nfs_node_unlock(dnp
);
3994 /* nfs_getattr() will check changed and purge caches */
3995 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
3998 if (!error
&& fh
.fh_len
) {
3999 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
4001 if (!error
&& !np
) {
4002 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
4008 nfs_node_clear_busy(dnp
);
4011 if (!error
&& (gotuid
|| gotgid
) &&
4012 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
4013 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
4014 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
4015 /* clear ID bits if server didn't use them (or we can't tell) */
4016 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
4017 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
4021 nfs_node_unlock(np
);
4026 nfs_node_unlock(np
);
4031 static uint32_t create_verf
;
4033 * NFS file create call
4037 struct vnop_create_args
/* {
4038 * struct vnodeop_desc *a_desc;
4041 * struct componentname *a_cnp;
4042 * struct vnode_attr *a_vap;
4043 * vfs_context_t a_context;
4046 vfs_context_t ctx
= ap
->a_context
;
4047 vnode_t dvp
= ap
->a_dvp
;
4048 struct vnode_attr
*vap
= ap
->a_vap
;
4049 struct componentname
*cnp
= ap
->a_cnp
;
4050 struct nfs_vattr nvattr
;
4052 nfsnode_t np
= NULL
;
4053 struct nfsmount
*nmp
;
4054 nfsnode_t dnp
= VTONFS(dvp
);
4055 vnode_t newvp
= NULL
;
4056 int error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0, fmode
= 0;
4057 struct timespec premtime
= { 0, 0 };
4058 int nfsvers
, gotuid
, gotgid
;
4059 u_int64_t xid
, dxid
;
4061 struct nfsm_chain nmreq
, nmrep
;
4062 struct nfsreq rq
, *req
= &rq
;
4063 struct nfs_dulookup dul
;
4066 if (nfs_mount_gone(nmp
)) {
4069 nfsvers
= nmp
->nm_vers
;
4071 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
)) {
4072 return ENAMETOOLONG
;
4075 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
4077 VATTR_SET_SUPPORTED(vap
, va_mode
);
4078 VATTR_SET_SUPPORTED(vap
, va_uid
);
4079 VATTR_SET_SUPPORTED(vap
, va_gid
);
4080 VATTR_SET_SUPPORTED(vap
, va_data_size
);
4081 VATTR_SET_SUPPORTED(vap
, va_access_time
);
4082 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
4083 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
4084 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
4086 if (vap
->va_vaflags
& VA_EXCLUSIVE
) {
4088 if (!VATTR_IS_ACTIVE(vap
, va_access_time
) || !VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
4089 vap
->va_vaflags
|= VA_UTIMES_NULL
;
4094 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
4095 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4097 nfsm_chain_null(&nmreq
);
4098 nfsm_chain_null(&nmrep
);
4100 nfsm_chain_build_alloc_init(error
, &nmreq
,
4101 NFSX_FH(nfsvers
) + 2 * NFSX_UNSIGNED
+
4102 nfsm_rndup(cnp
->cn_namelen
) + NFSX_SATTR(nfsvers
));
4103 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4104 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4105 if (nfsvers
== NFS_VER3
) {
4106 if (fmode
& O_EXCL
) {
4107 nfsm_chain_add_32(error
, &nmreq
, NFS_CREATE_EXCLUSIVE
);
4108 lck_rw_lock_shared(in_ifaddr_rwlock
);
4109 if (!TAILQ_EMPTY(&in_ifaddrhead
)) {
4110 val
= IA_SIN(in_ifaddrhead
.tqh_first
)->sin_addr
.s_addr
;
4114 lck_rw_done(in_ifaddr_rwlock
);
4115 nfsm_chain_add_32(error
, &nmreq
, val
);
4117 nfsm_chain_add_32(error
, &nmreq
, create_verf
);
4119 nfsm_chain_add_32(error
, &nmreq
, NFS_CREATE_UNCHECKED
);
4120 nfsm_chain_add_v3sattr(error
, &nmreq
, vap
);
4123 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, 0);
4125 nfsm_chain_build_done(error
, &nmreq
);
4128 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_CREATE
,
4129 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
4131 nfs_dulookup_start(&dul
, dnp
, ctx
);
4132 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
4135 if ((lockerror
= nfs_node_lock(dnp
))) {
4139 if (!error
&& !status
) {
4140 if (dnp
->n_flag
& NNEGNCENTRIES
) {
4141 dnp
->n_flag
&= ~NNEGNCENTRIES
;
4142 cache_purge_negatives(dvp
);
4144 error
= nfsm_chain_get_fh_attr(&nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
4146 if (nfsvers
== NFS_VER3
) {
4147 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
4153 nfsm_chain_cleanup(&nmreq
);
4154 nfsm_chain_cleanup(&nmrep
);
4157 dnp
->n_flag
|= NMODIFIED
;
4158 /* if directory hadn't changed, update namecache mtime */
4159 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==)) {
4160 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
4162 nfs_node_unlock(dnp
);
4163 /* nfs_getattr() will check changed and purge caches */
4164 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
4167 if (!error
&& fh
.fh_len
) {
4168 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
4170 if (!error
&& !np
) {
4171 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
4177 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4179 nfs_node_clear_busy(dnp
);
4183 if ((nfsvers
== NFS_VER3
) && (fmode
& O_EXCL
) && (error
== NFSERR_NOTSUPP
)) {
4188 nfs_node_unlock(np
);
4191 } else if ((nfsvers
== NFS_VER3
) && (fmode
& O_EXCL
)) {
4192 nfs_node_unlock(np
);
4193 error
= nfs3_setattr_rpc(np
, vap
, ctx
);
4194 if (error
&& (gotuid
|| gotgid
)) {
4195 /* it's possible the server didn't like our attempt to set IDs. */
4196 /* so, let's try it again without those */
4197 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
4198 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
4199 error
= nfs3_setattr_rpc(np
, vap
, ctx
);
4204 nfs_node_lock_force(np
);
4210 if (!error
&& (gotuid
|| gotgid
) &&
4211 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
4212 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
4213 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
4214 /* clear ID bits if server didn't use them (or we can't tell) */
4215 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
4216 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
4219 nfs_node_unlock(np
);
4225 * NFS file remove call
4226 * To try and make NFS semantics closer to UFS semantics, a file that has
4227 * other processes using the vnode is renamed instead of removed and then
4228 * removed later on the last close.
4229 * - If vnode_isinuse()
4230 * If a rename is not already in the works
4231 * call nfs_sillyrename() to set it up
4237 struct vnop_remove_args
/* {
4238 * struct vnodeop_desc *a_desc;
4241 * struct componentname *a_cnp;
4243 * vfs_context_t a_context;
4246 vfs_context_t ctx
= ap
->a_context
;
4247 vnode_t vp
= ap
->a_vp
;
4248 vnode_t dvp
= ap
->a_dvp
;
4249 struct componentname
*cnp
= ap
->a_cnp
;
4250 nfsnode_t dnp
= VTONFS(dvp
);
4251 nfsnode_t np
= VTONFS(vp
);
4252 int error
= 0, nfsvers
, namedattrs
, inuse
, gotattr
= 0, flushed
= 0, setsize
= 0;
4253 struct nfs_vattr nvattr
;
4254 struct nfsmount
*nmp
;
4255 struct nfs_dulookup dul
;
4257 /* XXX prevent removing a sillyrenamed file? */
4259 nmp
= NFSTONMP(dnp
);
4260 if (nfs_mount_gone(nmp
)) {
4263 nfsvers
= nmp
->nm_vers
;
4264 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
4267 error
= nfs_node_set_busy2(dnp
, np
, vfs_context_thread(ctx
));
4272 /* lock the node while we remove the file */
4273 lck_mtx_lock(nfs_node_hash_mutex
);
4274 while (np
->n_hflag
& NHLOCKED
) {
4275 np
->n_hflag
|= NHLOCKWANT
;
4276 msleep(np
, nfs_node_hash_mutex
, PINOD
, "nfs_remove", NULL
);
4278 np
->n_hflag
|= NHLOCKED
;
4279 lck_mtx_unlock(nfs_node_hash_mutex
);
4282 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4285 inuse
= vnode_isinuse(vp
, 0);
4286 if ((ap
->a_flags
& VNODE_REMOVE_NODELETEBUSY
) && inuse
) {
4287 /* Caller requested Carbon delete semantics, but file is busy */
4291 if (inuse
&& !gotattr
) {
4292 if (nfs_getattr(np
, &nvattr
, ctx
, NGA_CACHED
)) {
4293 nvattr
.nva_nlink
= 1;
4298 if (!inuse
|| (np
->n_sillyrename
&& (nvattr
.nva_nlink
> 1))) {
4299 if (!inuse
&& !flushed
) { /* flush all the buffers first */
4300 /* unlock the node */
4301 lck_mtx_lock(nfs_node_hash_mutex
);
4302 np
->n_hflag
&= ~NHLOCKED
;
4303 if (np
->n_hflag
& NHLOCKWANT
) {
4304 np
->n_hflag
&= ~NHLOCKWANT
;
4307 lck_mtx_unlock(nfs_node_hash_mutex
);
4308 nfs_node_clear_busy2(dnp
, np
);
4309 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
4310 FSDBG(260, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0011);
4312 if (error
== EINTR
) {
4313 nfs_node_lock_force(np
);
4314 NATTRINVALIDATE(np
);
4315 nfs_node_unlock(np
);
4319 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4324 if ((nmp
->nm_vers
>= NFS_VER4
) && (np
->n_openflags
& N_DELEG_MASK
)) {
4325 nfs4_delegation_return(np
, 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4329 * Purge the name cache so that the chance of a lookup for
4330 * the name succeeding while the remove is in progress is
4333 nfs_name_cache_purge(dnp
, np
, cnp
, ctx
);
4336 nfs_dulookup_start(&dul
, dnp
, ctx
);
4340 error
= nmp
->nm_funcs
->nf_remove_rpc(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
,
4341 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4344 * Kludge City: If the first reply to the remove rpc is lost..
4345 * the reply to the retransmitted request will be ENOENT
4346 * since the file was in fact removed
4347 * Therefore, we cheat and return success.
4349 if (error
== ENOENT
) {
4353 if (!error
&& !inuse
&& !np
->n_sillyrename
) {
4355 * removal succeeded, it's not in use, and not silly renamed so
4356 * remove nfsnode from hash now so we can't accidentally find it
4357 * again if another object gets created with the same filehandle
4358 * before this vnode gets reclaimed
4360 lck_mtx_lock(nfs_node_hash_mutex
);
4361 if (np
->n_hflag
& NHHASHED
) {
4362 LIST_REMOVE(np
, n_hash
);
4363 np
->n_hflag
&= ~NHHASHED
;
4364 FSDBG(266, 0, np
, np
->n_flag
, 0xb1eb1e);
4366 lck_mtx_unlock(nfs_node_hash_mutex
);
4367 /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */
4368 /* clear all flags other than these */
4369 nfs_node_lock_force(np
);
4370 np
->n_flag
&= (NMODIFIED
);
4371 NATTRINVALIDATE(np
);
4372 nfs_node_unlock(np
);
4376 nfs_node_lock_force(np
);
4377 NATTRINVALIDATE(np
);
4378 nfs_node_unlock(np
);
4380 } else if (!np
->n_sillyrename
) {
4382 nfs_dulookup_start(&dul
, dnp
, ctx
);
4384 error
= nfs_sillyrename(dnp
, np
, cnp
, ctx
);
4385 nfs_node_lock_force(np
);
4386 NATTRINVALIDATE(np
);
4387 nfs_node_unlock(np
);
4389 nfs_node_lock_force(np
);
4390 NATTRINVALIDATE(np
);
4391 nfs_node_unlock(np
);
4393 nfs_dulookup_start(&dul
, dnp
, ctx
);
4397 /* nfs_getattr() will check changed and purge caches */
4398 nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
);
4400 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4403 /* unlock the node */
4404 lck_mtx_lock(nfs_node_hash_mutex
);
4405 np
->n_hflag
&= ~NHLOCKED
;
4406 if (np
->n_hflag
& NHLOCKWANT
) {
4407 np
->n_hflag
&= ~NHLOCKWANT
;
4410 lck_mtx_unlock(nfs_node_hash_mutex
);
4411 nfs_node_clear_busy2(dnp
, np
);
4419 * NFS silly-renamed file removal function called from nfs_vnop_inactive
4422 nfs_removeit(struct nfs_sillyrename
*nsp
)
4424 struct nfsmount
*nmp
= NFSTONMP(nsp
->nsr_dnp
);
4425 if (nfs_mount_gone(nmp
)) {
4428 return nmp
->nm_funcs
->nf_remove_rpc(nsp
->nsr_dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, NULL
, nsp
->nsr_cred
);
4432 * NFS remove rpc, called from nfs_remove() and nfs_removeit().
4442 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0;
4443 struct timespec premtime
= { 0, 0 };
4444 struct nfsmount
*nmp
;
4447 struct nfsm_chain nmreq
, nmrep
;
4449 nmp
= NFSTONMP(dnp
);
4450 if (nfs_mount_gone(nmp
)) {
4453 nfsvers
= nmp
->nm_vers
;
4454 if ((nfsvers
== NFS_VER2
) && (namelen
> NFS_MAXNAMLEN
)) {
4455 return ENAMETOOLONG
;
4458 nfsm_chain_null(&nmreq
);
4459 nfsm_chain_null(&nmrep
);
4461 nfsm_chain_build_alloc_init(error
, &nmreq
,
4462 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+ nfsm_rndup(namelen
));
4463 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4464 nfsm_chain_add_name(error
, &nmreq
, name
, namelen
, nmp
);
4465 nfsm_chain_build_done(error
, &nmreq
);
4468 error
= nfs_request2(dnp
, NULL
, &nmreq
, NFSPROC_REMOVE
, thd
, cred
, NULL
, 0, &nmrep
, &xid
, &status
);
4470 if ((lockerror
= nfs_node_lock(dnp
))) {
4473 if (nfsvers
== NFS_VER3
) {
4474 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &xid
);
4477 dnp
->n_flag
|= NMODIFIED
;
4478 /* if directory hadn't changed, update namecache mtime */
4479 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==)) {
4480 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
4483 NATTRINVALIDATE(dnp
);
4490 nfs_node_unlock(dnp
);
4492 nfsm_chain_cleanup(&nmreq
);
4493 nfsm_chain_cleanup(&nmrep
);
4498 * NFS file rename call
4502 struct vnop_rename_args
/* {
4503 * struct vnodeop_desc *a_desc;
4506 * struct componentname *a_fcnp;
4509 * struct componentname *a_tcnp;
4510 * vfs_context_t a_context;
4513 vfs_context_t ctx
= ap
->a_context
;
4514 vnode_t fdvp
= ap
->a_fdvp
;
4515 vnode_t fvp
= ap
->a_fvp
;
4516 vnode_t tdvp
= ap
->a_tdvp
;
4517 vnode_t tvp
= ap
->a_tvp
;
4518 nfsnode_t fdnp
, fnp
, tdnp
, tnp
;
4519 struct componentname
*tcnp
= ap
->a_tcnp
;
4520 struct componentname
*fcnp
= ap
->a_fcnp
;
4521 int error
, nfsvers
, inuse
= 0, tvprecycle
= 0, locked
= 0;
4522 mount_t fmp
, tdmp
, tmp
;
4523 struct nfs_vattr nvattr
;
4524 struct nfsmount
*nmp
;
4526 fdnp
= VTONFS(fdvp
);
4528 tdnp
= VTONFS(tdvp
);
4529 tnp
= tvp
? VTONFS(tvp
) : NULL
;
4531 nmp
= NFSTONMP(fdnp
);
4532 if (nfs_mount_gone(nmp
)) {
4535 nfsvers
= nmp
->nm_vers
;
4537 error
= nfs_node_set_busy4(fdnp
, fnp
, tdnp
, tnp
, vfs_context_thread(ctx
));
4542 if (tvp
&& (tvp
!= fvp
)) {
4543 /* lock the node while we rename over the existing file */
4544 lck_mtx_lock(nfs_node_hash_mutex
);
4545 while (tnp
->n_hflag
& NHLOCKED
) {
4546 tnp
->n_hflag
|= NHLOCKWANT
;
4547 msleep(tnp
, nfs_node_hash_mutex
, PINOD
, "nfs_rename", NULL
);
4549 tnp
->n_hflag
|= NHLOCKED
;
4550 lck_mtx_unlock(nfs_node_hash_mutex
);
4554 /* Check for cross-device rename */
4555 fmp
= vnode_mount(fvp
);
4556 tmp
= tvp
? vnode_mount(tvp
) : NULL
;
4557 tdmp
= vnode_mount(tdvp
);
4558 if ((fmp
!= tdmp
) || (tvp
&& (fmp
!= tmp
))) {
4563 /* XXX prevent renaming from/over a sillyrenamed file? */
4566 * If the tvp exists and is in use, sillyrename it before doing the
4567 * rename of the new file over it.
4568 * XXX Can't sillyrename a directory.
4569 * Don't sillyrename if source and target are same vnode (hard
4570 * links or case-variants)
4572 if (tvp
&& (tvp
!= fvp
)) {
4573 inuse
= vnode_isinuse(tvp
, 0);
4575 if (inuse
&& !tnp
->n_sillyrename
&& (vnode_vtype(tvp
) != VDIR
)) {
4576 error
= nfs_sillyrename(tdnp
, tnp
, tcnp
, ctx
);
4578 /* sillyrename failed. Instead of pressing on, return error */
4579 goto out
; /* should not be ENOENT. */
4581 /* sillyrename succeeded.*/
4584 } else if (tvp
&& (nmp
->nm_vers
>= NFS_VER4
) && (tnp
->n_openflags
& N_DELEG_MASK
)) {
4585 nfs4_delegation_return(tnp
, 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4588 error
= nmp
->nm_funcs
->nf_rename_rpc(fdnp
, fcnp
->cn_nameptr
, fcnp
->cn_namelen
,
4589 tdnp
, tcnp
->cn_nameptr
, tcnp
->cn_namelen
, ctx
);
4592 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
4594 if (error
== ENOENT
) {
4598 if (tvp
&& (tvp
!= fvp
) && !tnp
->n_sillyrename
) {
4599 nfs_node_lock_force(tnp
);
4600 tvprecycle
= (!error
&& !vnode_isinuse(tvp
, 0) &&
4601 (nfs_getattrcache(tnp
, &nvattr
, 0) || (nvattr
.nva_nlink
== 1)));
4602 nfs_node_unlock(tnp
);
4603 lck_mtx_lock(nfs_node_hash_mutex
);
4604 if (tvprecycle
&& (tnp
->n_hflag
& NHHASHED
)) {
4606 * remove nfsnode from hash now so we can't accidentally find it
4607 * again if another object gets created with the same filehandle
4608 * before this vnode gets reclaimed
4610 LIST_REMOVE(tnp
, n_hash
);
4611 tnp
->n_hflag
&= ~NHHASHED
;
4612 FSDBG(266, 0, tnp
, tnp
->n_flag
, 0xb1eb1e);
4614 lck_mtx_unlock(nfs_node_hash_mutex
);
4617 /* purge the old name cache entries and enter the new one */
4618 nfs_name_cache_purge(fdnp
, fnp
, fcnp
, ctx
);
4620 nfs_name_cache_purge(tdnp
, tnp
, tcnp
, ctx
);
4622 /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */
4623 /* clear all flags other than these */
4624 nfs_node_lock_force(tnp
);
4625 tnp
->n_flag
&= (NMODIFIED
);
4626 nfs_node_unlock(tnp
);
4631 nfs_node_lock_force(tdnp
);
4632 if (tdnp
->n_flag
& NNEGNCENTRIES
) {
4633 tdnp
->n_flag
&= ~NNEGNCENTRIES
;
4634 cache_purge_negatives(tdvp
);
4636 nfs_node_unlock(tdnp
);
4637 nfs_node_lock_force(fnp
);
4638 cache_enter(tdvp
, fvp
, tcnp
);
4639 if (tdvp
!= fdvp
) { /* update parent pointer */
4640 if (fnp
->n_parent
&& !vnode_get(fnp
->n_parent
)) {
4641 /* remove ref from old parent */
4642 vnode_rele(fnp
->n_parent
);
4643 vnode_put(fnp
->n_parent
);
4645 fnp
->n_parent
= tdvp
;
4646 if (tdvp
&& !vnode_get(tdvp
)) {
4647 /* add ref to new parent */
4651 fnp
->n_parent
= NULL
;
4654 nfs_node_unlock(fnp
);
4657 /* nfs_getattr() will check changed and purge caches */
4658 nfs_getattr(fdnp
, NULL
, ctx
, NGA_CACHED
);
4659 nfs_getattr(tdnp
, NULL
, ctx
, NGA_CACHED
);
4662 lck_mtx_lock(nfs_node_hash_mutex
);
4663 tnp
->n_hflag
&= ~NHLOCKED
;
4664 if (tnp
->n_hflag
& NHLOCKWANT
) {
4665 tnp
->n_hflag
&= ~NHLOCKWANT
;
4668 lck_mtx_unlock(nfs_node_hash_mutex
);
4670 nfs_node_clear_busy4(fdnp
, fnp
, tdnp
, tnp
);
4675 * Do an NFS rename rpc. Called from nfs_vnop_rename() and nfs_sillyrename().
4687 int error
= 0, lockerror
= ENOENT
, status
, fwccpostattr
= 0, twccpostattr
= 0;
4688 struct timespec fpremtime
= { 0, 0 }, tpremtime
= { 0, 0 };
4689 struct nfsmount
*nmp
;
4691 u_int64_t xid
, txid
;
4692 struct nfsm_chain nmreq
, nmrep
;
4694 nmp
= NFSTONMP(fdnp
);
4695 if (nfs_mount_gone(nmp
)) {
4698 nfsvers
= nmp
->nm_vers
;
4699 if ((nfsvers
== NFS_VER2
) &&
4700 ((fnamelen
> NFS_MAXNAMLEN
) || (tnamelen
> NFS_MAXNAMLEN
))) {
4701 return ENAMETOOLONG
;
4704 nfsm_chain_null(&nmreq
);
4705 nfsm_chain_null(&nmrep
);
4707 nfsm_chain_build_alloc_init(error
, &nmreq
,
4708 (NFSX_FH(nfsvers
) + NFSX_UNSIGNED
) * 2 +
4709 nfsm_rndup(fnamelen
) + nfsm_rndup(tnamelen
));
4710 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, fdnp
->n_fhp
, fdnp
->n_fhsize
);
4711 nfsm_chain_add_name(error
, &nmreq
, fnameptr
, fnamelen
, nmp
);
4712 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, tdnp
->n_fhp
, tdnp
->n_fhsize
);
4713 nfsm_chain_add_name(error
, &nmreq
, tnameptr
, tnamelen
, nmp
);
4714 nfsm_chain_build_done(error
, &nmreq
);
4717 error
= nfs_request(fdnp
, NULL
, &nmreq
, NFSPROC_RENAME
, ctx
, NULL
, &nmrep
, &xid
, &status
);
4719 if ((lockerror
= nfs_node_lock2(fdnp
, tdnp
))) {
4722 if (nfsvers
== NFS_VER3
) {
4724 nfsm_chain_get_wcc_data(error
, &nmrep
, fdnp
, &fpremtime
, &fwccpostattr
, &xid
);
4725 nfsm_chain_get_wcc_data(error
, &nmrep
, tdnp
, &tpremtime
, &twccpostattr
, &txid
);
4731 nfsm_chain_cleanup(&nmreq
);
4732 nfsm_chain_cleanup(&nmrep
);
4734 fdnp
->n_flag
|= NMODIFIED
;
4735 /* if directory hadn't changed, update namecache mtime */
4736 if (nfstimespeccmp(&fdnp
->n_ncmtime
, &fpremtime
, ==)) {
4737 NFS_CHANGED_UPDATE_NC(nfsvers
, fdnp
, &fdnp
->n_vattr
);
4739 if (!fwccpostattr
) {
4740 NATTRINVALIDATE(fdnp
);
4742 tdnp
->n_flag
|= NMODIFIED
;
4743 /* if directory hadn't changed, update namecache mtime */
4744 if (nfstimespeccmp(&tdnp
->n_ncmtime
, &tpremtime
, ==)) {
4745 NFS_CHANGED_UPDATE_NC(nfsvers
, tdnp
, &tdnp
->n_vattr
);
4747 if (!twccpostattr
) {
4748 NATTRINVALIDATE(tdnp
);
4750 nfs_node_unlock2(fdnp
, tdnp
);
4756 * NFS hard link create call
4760 struct vnop_link_args
/* {
4761 * struct vnodeop_desc *a_desc;
4764 * struct componentname *a_cnp;
4765 * vfs_context_t a_context;
4768 vfs_context_t ctx
= ap
->a_context
;
4769 vnode_t vp
= ap
->a_vp
;
4770 vnode_t tdvp
= ap
->a_tdvp
;
4771 struct componentname
*cnp
= ap
->a_cnp
;
4772 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0, attrflag
= 0;
4773 struct timespec premtime
= { 0, 0 };
4774 struct nfsmount
*nmp
;
4775 nfsnode_t np
= VTONFS(vp
);
4776 nfsnode_t tdnp
= VTONFS(tdvp
);
4778 u_int64_t xid
, txid
;
4779 struct nfsm_chain nmreq
, nmrep
;
4781 if (vnode_mount(vp
) != vnode_mount(tdvp
)) {
4786 if (nfs_mount_gone(nmp
)) {
4789 nfsvers
= nmp
->nm_vers
;
4790 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
)) {
4791 return ENAMETOOLONG
;
4795 * Push all writes to the server, so that the attribute cache
4796 * doesn't get "out of sync" with the server.
4797 * XXX There should be a better way!
4799 nfs_flush(np
, MNT_WAIT
, vfs_context_thread(ctx
), V_IGNORE_WRITEERR
);
4801 error
= nfs_node_set_busy2(tdnp
, np
, vfs_context_thread(ctx
));
4806 nfsm_chain_null(&nmreq
);
4807 nfsm_chain_null(&nmrep
);
4809 nfsm_chain_build_alloc_init(error
, &nmreq
,
4810 NFSX_FH(nfsvers
) * 2 + NFSX_UNSIGNED
+ nfsm_rndup(cnp
->cn_namelen
));
4811 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
4812 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, tdnp
->n_fhp
, tdnp
->n_fhsize
);
4813 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4814 nfsm_chain_build_done(error
, &nmreq
);
4816 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_LINK
, ctx
, NULL
, &nmrep
, &xid
, &status
);
4818 if ((lockerror
= nfs_node_lock2(tdnp
, np
))) {
4822 if (nfsvers
== NFS_VER3
) {
4824 nfsm_chain_postop_attr_update_flag(error
, &nmrep
, np
, attrflag
, &xid
);
4825 nfsm_chain_get_wcc_data(error
, &nmrep
, tdnp
, &premtime
, &wccpostattr
, &txid
);
4831 nfsm_chain_cleanup(&nmreq
);
4832 nfsm_chain_cleanup(&nmrep
);
4835 NATTRINVALIDATE(np
);
4837 tdnp
->n_flag
|= NMODIFIED
;
4838 /* if directory hadn't changed, update namecache mtime */
4839 if (nfstimespeccmp(&tdnp
->n_ncmtime
, &premtime
, ==)) {
4840 NFS_CHANGED_UPDATE_NC(nfsvers
, tdnp
, &tdnp
->n_vattr
);
4843 NATTRINVALIDATE(tdnp
);
4845 if (!error
&& (tdnp
->n_flag
& NNEGNCENTRIES
)) {
4846 tdnp
->n_flag
&= ~NNEGNCENTRIES
;
4847 cache_purge_negatives(tdvp
);
4849 nfs_node_unlock2(tdnp
, np
);
4851 nfs_node_clear_busy2(tdnp
, np
);
4853 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
4855 if (error
== EEXIST
) {
4862 * NFS symbolic link create call
4866 struct vnop_symlink_args
/* {
4867 * struct vnodeop_desc *a_desc;
4870 * struct componentname *a_cnp;
4871 * struct vnode_attr *a_vap;
4873 * vfs_context_t a_context;
4876 vfs_context_t ctx
= ap
->a_context
;
4877 vnode_t dvp
= ap
->a_dvp
;
4878 struct vnode_attr
*vap
= ap
->a_vap
;
4879 struct componentname
*cnp
= ap
->a_cnp
;
4880 struct nfs_vattr nvattr
;
4882 int slen
, error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0;
4883 struct timespec premtime
= { 0, 0 };
4884 vnode_t newvp
= NULL
;
4885 int nfsvers
, gotuid
, gotgid
;
4886 u_int64_t xid
= 0, dxid
;
4887 nfsnode_t np
= NULL
;
4888 nfsnode_t dnp
= VTONFS(dvp
);
4889 struct nfsmount
*nmp
;
4890 struct nfsm_chain nmreq
, nmrep
;
4891 struct nfsreq rq
, *req
= &rq
;
4892 struct nfs_dulookup dul
;
4895 if (nfs_mount_gone(nmp
)) {
4898 nfsvers
= nmp
->nm_vers
;
4900 slen
= strlen(ap
->a_target
);
4901 if ((nfsvers
== NFS_VER2
) &&
4902 ((cnp
->cn_namelen
> NFS_MAXNAMLEN
) || (slen
> NFS_MAXPATHLEN
))) {
4903 return ENAMETOOLONG
;
4906 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
4908 VATTR_SET_SUPPORTED(vap
, va_mode
);
4909 VATTR_SET_SUPPORTED(vap
, va_uid
);
4910 VATTR_SET_SUPPORTED(vap
, va_gid
);
4911 VATTR_SET_SUPPORTED(vap
, va_data_size
);
4912 VATTR_SET_SUPPORTED(vap
, va_access_time
);
4913 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
4914 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
4915 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
4917 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
4918 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4920 nfsm_chain_null(&nmreq
);
4921 nfsm_chain_null(&nmrep
);
4923 nfsm_chain_build_alloc_init(error
, &nmreq
,
4924 NFSX_FH(nfsvers
) + 2 * NFSX_UNSIGNED
+
4925 nfsm_rndup(cnp
->cn_namelen
) + nfsm_rndup(slen
) + NFSX_SATTR(nfsvers
));
4926 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4927 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4928 if (nfsvers
== NFS_VER3
) {
4929 nfsm_chain_add_v3sattr(error
, &nmreq
, vap
);
4931 nfsm_chain_add_name(error
, &nmreq
, ap
->a_target
, slen
, nmp
);
4932 if (nfsvers
== NFS_VER2
) {
4933 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, -1);
4935 nfsm_chain_build_done(error
, &nmreq
);
4938 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_SYMLINK
,
4939 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
4941 nfs_dulookup_start(&dul
, dnp
, ctx
);
4942 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
4945 if ((lockerror
= nfs_node_lock(dnp
))) {
4949 if (!error
&& !status
) {
4950 if (dnp
->n_flag
& NNEGNCENTRIES
) {
4951 dnp
->n_flag
&= ~NNEGNCENTRIES
;
4952 cache_purge_negatives(dvp
);
4954 if (nfsvers
== NFS_VER3
) {
4955 error
= nfsm_chain_get_fh_attr(&nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
4960 if (nfsvers
== NFS_VER3
) {
4961 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
4967 nfsm_chain_cleanup(&nmreq
);
4968 nfsm_chain_cleanup(&nmrep
);
4971 dnp
->n_flag
|= NMODIFIED
;
4972 /* if directory hadn't changed, update namecache mtime */
4973 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==)) {
4974 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
4976 nfs_node_unlock(dnp
);
4977 /* nfs_getattr() will check changed and purge caches */
4978 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
4981 if (!error
&& fh
.fh_len
) {
4982 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
4988 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4991 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
4992 * if we can succeed in looking up the symlink.
4994 if ((error
== EEXIST
) || (!error
&& !newvp
)) {
4996 nfs_node_unlock(np
);
5000 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
5003 if (vnode_vtype(newvp
) != VLNK
) {
5009 nfs_node_clear_busy(dnp
);
5011 if (!error
&& (gotuid
|| gotgid
) &&
5012 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
5013 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
5014 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
5015 /* clear ID bits if server didn't use them (or we can't tell) */
5016 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
5017 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
5021 nfs_node_unlock(np
);
5025 nfs_node_unlock(np
);
5036 struct vnop_mkdir_args
/* {
5037 * struct vnodeop_desc *a_desc;
5040 * struct componentname *a_cnp;
5041 * struct vnode_attr *a_vap;
5042 * vfs_context_t a_context;
5045 vfs_context_t ctx
= ap
->a_context
;
5046 vnode_t dvp
= ap
->a_dvp
;
5047 struct vnode_attr
*vap
= ap
->a_vap
;
5048 struct componentname
*cnp
= ap
->a_cnp
;
5049 struct nfs_vattr nvattr
;
5050 nfsnode_t np
= NULL
;
5051 struct nfsmount
*nmp
;
5052 nfsnode_t dnp
= VTONFS(dvp
);
5053 vnode_t newvp
= NULL
;
5054 int error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0;
5055 struct timespec premtime
= { 0, 0 };
5056 int nfsvers
, gotuid
, gotgid
;
5057 u_int64_t xid
= 0, dxid
;
5059 struct nfsm_chain nmreq
, nmrep
;
5060 struct nfsreq rq
, *req
= &rq
;
5061 struct nfs_dulookup dul
;
5064 if (nfs_mount_gone(nmp
)) {
5067 nfsvers
= nmp
->nm_vers
;
5068 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
)) {
5069 return ENAMETOOLONG
;
5072 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
5074 VATTR_SET_SUPPORTED(vap
, va_mode
);
5075 VATTR_SET_SUPPORTED(vap
, va_uid
);
5076 VATTR_SET_SUPPORTED(vap
, va_gid
);
5077 VATTR_SET_SUPPORTED(vap
, va_data_size
);
5078 VATTR_SET_SUPPORTED(vap
, va_access_time
);
5079 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
5080 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
5081 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
5083 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
5084 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
5086 nfsm_chain_null(&nmreq
);
5087 nfsm_chain_null(&nmrep
);
5089 nfsm_chain_build_alloc_init(error
, &nmreq
,
5090 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+
5091 nfsm_rndup(cnp
->cn_namelen
) + NFSX_SATTR(nfsvers
));
5092 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
5093 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
5094 if (nfsvers
== NFS_VER3
) {
5095 nfsm_chain_add_v3sattr(error
, &nmreq
, vap
);
5097 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, -1);
5099 nfsm_chain_build_done(error
, &nmreq
);
5102 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_MKDIR
,
5103 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
5105 nfs_dulookup_start(&dul
, dnp
, ctx
);
5106 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
5109 if ((lockerror
= nfs_node_lock(dnp
))) {
5113 if (!error
&& !status
) {
5114 if (dnp
->n_flag
& NNEGNCENTRIES
) {
5115 dnp
->n_flag
&= ~NNEGNCENTRIES
;
5116 cache_purge_negatives(dvp
);
5118 error
= nfsm_chain_get_fh_attr(&nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
5120 if (nfsvers
== NFS_VER3
) {
5121 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
5127 nfsm_chain_cleanup(&nmreq
);
5128 nfsm_chain_cleanup(&nmrep
);
5131 dnp
->n_flag
|= NMODIFIED
;
5132 /* if directory hadn't changed, update namecache mtime */
5133 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==)) {
5134 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
5136 nfs_node_unlock(dnp
);
5137 /* nfs_getattr() will check changed and purge caches */
5138 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
5141 if (!error
&& fh
.fh_len
) {
5142 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
5148 nfs_dulookup_finish(&dul
, dnp
, ctx
);
5151 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
5152 * if we can succeed in looking up the directory.
5154 if ((error
== EEXIST
) || (!error
&& !newvp
)) {
5156 nfs_node_unlock(np
);
5160 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
5163 if (vnode_vtype(newvp
) != VDIR
) {
5169 nfs_node_clear_busy(dnp
);
5171 if (!error
&& (gotuid
|| gotgid
) &&
5172 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
5173 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
5174 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
5175 /* clear ID bits if server didn't use them (or we can't tell) */
5176 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
5177 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
5181 nfs_node_unlock(np
);
5185 nfs_node_unlock(np
);
5192 * NFS remove directory call
5196 struct vnop_rmdir_args
/* {
5197 * struct vnodeop_desc *a_desc;
5200 * struct componentname *a_cnp;
5201 * vfs_context_t a_context;
5204 vfs_context_t ctx
= ap
->a_context
;
5205 vnode_t vp
= ap
->a_vp
;
5206 vnode_t dvp
= ap
->a_dvp
;
5207 struct componentname
*cnp
= ap
->a_cnp
;
5208 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0;
5209 struct timespec premtime
= { 0, 0 };
5210 struct nfsmount
*nmp
;
5211 nfsnode_t np
= VTONFS(vp
);
5212 nfsnode_t dnp
= VTONFS(dvp
);
5215 struct nfsm_chain nmreq
, nmrep
;
5216 struct nfsreq rq
, *req
= &rq
;
5217 struct nfs_dulookup dul
;
5220 if (nfs_mount_gone(nmp
)) {
5223 nfsvers
= nmp
->nm_vers
;
5224 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
)) {
5225 return ENAMETOOLONG
;
5228 if ((error
= nfs_node_set_busy2(dnp
, np
, vfs_context_thread(ctx
)))) {
5232 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
5234 nfsm_chain_null(&nmreq
);
5235 nfsm_chain_null(&nmrep
);
5237 nfsm_chain_build_alloc_init(error
, &nmreq
,
5238 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+ nfsm_rndup(cnp
->cn_namelen
));
5239 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
5240 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
5241 nfsm_chain_build_done(error
, &nmreq
);
5244 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_RMDIR
,
5245 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
5247 nfs_dulookup_start(&dul
, dnp
, ctx
);
5248 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
5251 if ((lockerror
= nfs_node_lock(dnp
))) {
5254 if (nfsvers
== NFS_VER3
) {
5255 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &xid
);
5261 nfsm_chain_cleanup(&nmreq
);
5262 nfsm_chain_cleanup(&nmrep
);
5265 dnp
->n_flag
|= NMODIFIED
;
5266 /* if directory hadn't changed, update namecache mtime */
5267 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==)) {
5268 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
5270 nfs_node_unlock(dnp
);
5271 nfs_name_cache_purge(dnp
, np
, cnp
, ctx
);
5272 /* nfs_getattr() will check changed and purge caches */
5273 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
5275 nfs_dulookup_finish(&dul
, dnp
, ctx
);
5276 nfs_node_clear_busy2(dnp
, np
);
5279 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
5281 if (error
== ENOENT
) {
5286 * remove nfsnode from hash now so we can't accidentally find it
5287 * again if another object gets created with the same filehandle
5288 * before this vnode gets reclaimed
5290 lck_mtx_lock(nfs_node_hash_mutex
);
5291 if (np
->n_hflag
& NHHASHED
) {
5292 LIST_REMOVE(np
, n_hash
);
5293 np
->n_hflag
&= ~NHHASHED
;
5294 FSDBG(266, 0, np
, np
->n_flag
, 0xb1eb1e);
5296 lck_mtx_unlock(nfs_node_hash_mutex
);
5304 * The incoming "offset" is a directory cookie indicating where in the
5305 * directory entries should be read from. A zero cookie means start at
5306 * the beginning of the directory. Any other cookie will be a cookie
5307 * returned from the server.
5309 * Using that cookie, determine which buffer (and where in that buffer)
5310 * to start returning entries from. Buffer logical block numbers are
5311 * the cookies they start at. If a buffer is found that is not full,
5312 * call into the bio/RPC code to fill it. The RPC code will probably
5313 * fill several buffers (dropping the first, requiring a re-get).
5315 * When done copying entries to the buffer, set the offset to the current
5316 * entry's cookie and enter that cookie in the cookie cache.
5318 * Note: because the getdirentries(2) API returns a long-typed offset,
5319 * the incoming offset is a potentially truncated cookie (ptc).
5320 * The cookie matching code is aware of this and will fall back to
5321 * matching only 32 bits of the cookie.
5325 struct vnop_readdir_args
/* {
5326 * struct vnodeop_desc *a_desc;
5328 * struct uio *a_uio;
5332 * vfs_context_t a_context;
5335 vfs_context_t ctx
= ap
->a_context
;
5336 vnode_t dvp
= ap
->a_vp
;
5337 nfsnode_t dnp
= VTONFS(dvp
);
5338 struct nfsmount
*nmp
;
5339 uio_t uio
= ap
->a_uio
;
5340 int error
, nfsvers
, extended
, numdirent
, bigcookies
, ptc
, done
;
5341 uint16_t i
, iptc
, rlen
, nlen
;
5342 uint64_t cookie
, nextcookie
, lbn
= 0;
5343 struct nfsbuf
*bp
= NULL
;
5344 struct nfs_dir_buf_header
*ndbhp
;
5345 struct direntry
*dp
, *dpptc
;
5351 if (nfs_mount_gone(nmp
)) {
5354 nfsvers
= nmp
->nm_vers
;
5355 bigcookies
= (nmp
->nm_state
& NFSSTA_BIGCOOKIES
);
5356 extended
= (ap
->a_flags
& VNODE_READDIR_EXTENDED
);
5358 if (vnode_vtype(dvp
) != VDIR
) {
5362 if (ap
->a_eofflag
) {
5366 if (uio_resid(uio
) == 0) {
5370 if ((nfsvers
>= NFS_VER4
) && (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER
)) {
5371 /* trigger directories should never be read, return nothing */
5375 thd
= vfs_context_thread(ctx
);
5376 numdirent
= done
= 0;
5377 nextcookie
= uio_offset(uio
);
5378 ptc
= bigcookies
&& NFS_DIR_COOKIE_POTENTIALLY_TRUNCATED(nextcookie
);
5380 if ((error
= nfs_node_lock(dnp
))) {
5384 if (dnp
->n_flag
& NNEEDINVALIDATE
) {
5385 dnp
->n_flag
&= ~NNEEDINVALIDATE
;
5387 nfs_node_unlock(dnp
);
5388 error
= nfs_vinvalbuf(dvp
, 0, ctx
, 1);
5390 error
= nfs_node_lock(dnp
);
5398 * check for need to invalidate when (re)starting at beginning
5401 if (dnp
->n_flag
& NMODIFIED
) {
5403 nfs_node_unlock(dnp
);
5404 if ((error
= nfs_vinvalbuf(dvp
, 0, ctx
, 1))) {
5408 nfs_node_unlock(dnp
);
5410 /* nfs_getattr() will check changed and purge caches */
5411 if ((error
= nfs_getattr(dnp
, NULL
, ctx
, NGA_UNCACHED
))) {
5415 nfs_node_unlock(dnp
);
5418 error
= nfs_dir_cookie_to_lbn(dnp
, nextcookie
, &ptc
, &lbn
);
5420 if (error
< 0) { /* just hit EOF cookie */
5424 if (ap
->a_eofflag
) {
5429 while (!error
&& !done
) {
5430 OSAddAtomic64(1, &nfsstats
.biocache_readdirs
);
5431 cookie
= nextcookie
;
5433 error
= nfs_buf_get(dnp
, lbn
, NFS_DIRBLKSIZ
, thd
, NBLK_READ
, &bp
);
5437 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5438 if (!ISSET(bp
->nb_flags
, NB_CACHE
) || !ISSET(ndbhp
->ndbh_flags
, NDB_FULL
)) {
5439 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) { /* initialize the buffer */
5440 ndbhp
->ndbh_flags
= 0;
5441 ndbhp
->ndbh_count
= 0;
5442 ndbhp
->ndbh_entry_end
= sizeof(*ndbhp
);
5443 ndbhp
->ndbh_ncgen
= dnp
->n_ncgen
;
5445 error
= nfs_buf_readdir(bp
, ctx
);
5446 if (error
== NFSERR_DIRBUFDROPPED
) {
5450 nfs_buf_release(bp
, 1);
5452 if (error
&& (error
!= ENXIO
) && (error
!= ETIMEDOUT
) && (error
!= EINTR
) && (error
!= ERESTART
)) {
5453 if (!nfs_node_lock(dnp
)) {
5455 nfs_node_unlock(dnp
);
5457 nfs_vinvalbuf(dvp
, 0, ctx
, 1);
5458 if (error
== NFSERR_BAD_COOKIE
) {
5467 /* find next entry to return */
5468 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5470 if ((lbn
!= cookie
) && !(ptc
&& NFS_DIR_COOKIE_SAME32(lbn
, cookie
))) {
5473 for (; (i
< ndbhp
->ndbh_count
) && (cookie
!= dp
->d_seekoff
); i
++) {
5474 if (ptc
&& !dpptc
&& NFS_DIR_COOKIE_SAME32(cookie
, dp
->d_seekoff
)) {
5478 nextcookie
= dp
->d_seekoff
;
5479 dp
= NFS_DIRENTRY_NEXT(dp
);
5481 if ((i
== ndbhp
->ndbh_count
) && dpptc
) {
5485 if (i
< ndbhp
->ndbh_count
) {
5486 nextcookie
= dp
->d_seekoff
;
5487 dp
= NFS_DIRENTRY_NEXT(dp
);
5491 ptc
= 0; /* only have to deal with ptc on first cookie */
5493 /* return as many entries as we can */
5494 for (; i
< ndbhp
->ndbh_count
; i
++) {
5496 rlen
= dp
->d_reclen
;
5501 bzero(cp
, sizeof(dent
));
5503 if (dp
->d_namlen
> (sizeof(dent
.d_name
) - 1)) {
5504 nlen
= sizeof(dent
.d_name
) - 1;
5506 nlen
= dp
->d_namlen
;
5508 rlen
= NFS_DIRENT_LEN(nlen
);
5509 dent
.d_reclen
= rlen
;
5510 dent
.d_ino
= dp
->d_ino
;
5511 dent
.d_type
= dp
->d_type
;
5512 dent
.d_namlen
= nlen
;
5513 strlcpy(dent
.d_name
, dp
->d_name
, nlen
+ 1);
5515 /* check that the record fits */
5516 if (rlen
> uio_resid(uio
)) {
5520 if ((error
= uiomove(cp
, rlen
, uio
))) {
5524 nextcookie
= dp
->d_seekoff
;
5525 dp
= NFS_DIRENTRY_NEXT(dp
);
5528 if (i
== ndbhp
->ndbh_count
) {
5529 /* hit end of buffer, move to next buffer */
5531 /* if we also hit EOF, we're done */
5532 if (ISSET(ndbhp
->ndbh_flags
, NDB_EOF
)) {
5534 if (ap
->a_eofflag
) {
5540 uio_setoffset(uio
, nextcookie
);
5542 if (!error
&& !done
&& (nextcookie
== cookie
)) {
5543 printf("nfs readdir cookie didn't change 0x%llx, %d/%d\n", cookie
, i
, ndbhp
->ndbh_count
);
5546 nfs_buf_release(bp
, 1);
5550 nfs_dir_cookie_cache(dnp
, nextcookie
, lbn
);
5553 if (ap
->a_numdirent
) {
5554 *ap
->a_numdirent
= numdirent
;
5562 * Invalidate cached directory information, except for the actual directory
5563 * blocks (which are invalidated separately).
5566 nfs_invaldir(nfsnode_t dnp
)
5568 if (vnode_vtype(NFSTOV(dnp
)) != VDIR
) {
5571 dnp
->n_eofcookie
= 0;
5572 dnp
->n_cookieverf
= 0;
5573 if (!dnp
->n_cookiecache
) {
5576 dnp
->n_cookiecache
->free
= 0;
5577 dnp
->n_cookiecache
->mru
= -1;
5578 memset(dnp
->n_cookiecache
->next
, -1, NFSNUMCOOKIES
);
5582 * calculate how much space is available for additional directory entries.
5585 nfs_dir_buf_freespace(struct nfsbuf
*bp
, int rdirplus
)
5587 struct nfs_dir_buf_header
*ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5593 space
= bp
->nb_bufsize
- ndbhp
->ndbh_entry_end
;
5595 space
-= ndbhp
->ndbh_count
* sizeof(struct nfs_vattr
);
5601 * add/update a cookie->lbn entry in the directory cookie cache
5604 nfs_dir_cookie_cache(nfsnode_t dnp
, uint64_t cookie
, uint64_t lbn
)
5606 struct nfsdmap
*ndcc
;
5613 if (nfs_node_lock(dnp
)) {
5617 if (cookie
== dnp
->n_eofcookie
) { /* EOF cookie */
5618 nfs_node_unlock(dnp
);
5622 ndcc
= dnp
->n_cookiecache
;
5624 /* allocate the cookie cache structure */
5625 MALLOC_ZONE(dnp
->n_cookiecache
, struct nfsdmap
*,
5626 sizeof(struct nfsdmap
), M_NFSDIROFF
, M_WAITOK
);
5627 if (!dnp
->n_cookiecache
) {
5628 nfs_node_unlock(dnp
);
5631 ndcc
= dnp
->n_cookiecache
;
5634 memset(ndcc
->next
, -1, NFSNUMCOOKIES
);
5638 * Search the list for this cookie.
5639 * Keep track of previous and last entries.
5643 while ((i
!= -1) && (cookie
!= ndcc
->cookies
[i
].key
)) {
5644 if (ndcc
->next
[i
] == -1) { /* stop on last entry so we can reuse */
5650 if ((i
!= -1) && (cookie
== ndcc
->cookies
[i
].key
)) {
5651 /* found it, remove from list */
5653 ndcc
->next
[prev
] = ndcc
->next
[i
];
5655 ndcc
->mru
= ndcc
->next
[i
];
5658 /* not found, use next free entry or reuse last entry */
5659 if (ndcc
->free
!= NFSNUMCOOKIES
) {
5662 ndcc
->next
[prev
] = -1;
5664 ndcc
->cookies
[i
].key
= cookie
;
5665 ndcc
->cookies
[i
].lbn
= lbn
;
5667 /* insert cookie at head of MRU list */
5668 ndcc
->next
[i
] = ndcc
->mru
;
5670 nfs_node_unlock(dnp
);
5674 * Try to map the given directory cookie to a directory buffer (return lbn).
5675 * If we have a possibly truncated cookie (ptc), check for 32-bit matches too.
5678 nfs_dir_cookie_to_lbn(nfsnode_t dnp
, uint64_t cookie
, int *ptc
, uint64_t *lbnp
)
5680 struct nfsdmap
*ndcc
= dnp
->n_cookiecache
;
5681 int8_t eofptc
, found
;
5683 struct nfsmount
*nmp
;
5684 struct nfsbuf
*bp
, *lastbp
;
5685 struct nfsbuflists blist
;
5686 struct direntry
*dp
, *dpptc
;
5687 struct nfs_dir_buf_header
*ndbhp
;
5689 if (!cookie
) { /* initial cookie */
5695 if (nfs_node_lock(dnp
)) {
5699 if (cookie
== dnp
->n_eofcookie
) { /* EOF cookie */
5700 nfs_node_unlock(dnp
);
5701 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5705 /* note if cookie is a 32-bit match with the EOF cookie */
5706 eofptc
= *ptc
? NFS_DIR_COOKIE_SAME32(cookie
, dnp
->n_eofcookie
) : 0;
5709 /* search the list for the cookie */
5710 for (i
= ndcc
? ndcc
->mru
: -1; i
>= 0; i
= ndcc
->next
[i
]) {
5711 if (ndcc
->cookies
[i
].key
== cookie
) {
5712 /* found a match for this cookie */
5713 *lbnp
= ndcc
->cookies
[i
].lbn
;
5714 nfs_node_unlock(dnp
);
5715 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5719 /* check for 32-bit match */
5720 if (*ptc
&& (iptc
== -1) && NFS_DIR_COOKIE_SAME32(ndcc
->cookies
[i
].key
, cookie
)) {
5724 /* exact match not found */
5726 /* but 32-bit match hit the EOF cookie */
5727 nfs_node_unlock(dnp
);
5728 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5732 /* but 32-bit match got a hit */
5733 *lbnp
= ndcc
->cookies
[iptc
].lbn
;
5734 nfs_node_unlock(dnp
);
5735 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5738 nfs_node_unlock(dnp
);
5741 * No match found in the cookie cache... hmm...
5742 * Let's search the directory's buffers for the cookie.
5744 nmp
= NFSTONMP(dnp
);
5745 if (nfs_mount_gone(nmp
)) {
5751 lck_mtx_lock(nfs_buf_mutex
);
5753 * Scan the list of buffers, keeping them in order.
5754 * Note that itercomplete inserts each of the remaining buffers
5755 * into the head of list (thus reversing the elements). So, we
5756 * make sure to iterate through all buffers, inserting them after
5757 * each other, to keep them in order.
5758 * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because
5759 * we don't drop nfs_buf_mutex.
5761 if (!nfs_buf_iterprepare(dnp
, &blist
, NBI_CLEAN
)) {
5763 while ((bp
= LIST_FIRST(&blist
))) {
5764 LIST_REMOVE(bp
, nb_vnbufs
);
5766 LIST_INSERT_HEAD(&dnp
->n_cleanblkhd
, bp
, nb_vnbufs
);
5768 LIST_INSERT_AFTER(lastbp
, bp
, nb_vnbufs
);
5775 if (nfs_buf_acquire(bp
, NBAC_NOWAIT
, 0, 0)) {
5776 /* just skip this buffer */
5777 nfs_buf_refrele(bp
);
5780 nfs_buf_refrele(bp
);
5782 /* scan the buffer for the cookie */
5783 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5784 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5786 for (i
= 0; (i
< ndbhp
->ndbh_count
) && (cookie
!= dp
->d_seekoff
); i
++) {
5787 if (*ptc
&& !dpptc
&& NFS_DIR_COOKIE_SAME32(cookie
, dp
->d_seekoff
)) {
5791 dp
= NFS_DIRENTRY_NEXT(dp
);
5793 if ((i
== ndbhp
->ndbh_count
) && dpptc
) {
5794 /* found only a PTC match */
5797 } else if (i
< ndbhp
->ndbh_count
) {
5800 if (i
< (ndbhp
->ndbh_count
- 1)) {
5801 /* next entry is *in* this buffer: return this block */
5802 *lbnp
= bp
->nb_lblkno
;
5804 } else if (i
== (ndbhp
->ndbh_count
- 1)) {
5805 /* next entry refers to *next* buffer: return next block */
5806 *lbnp
= dp
->d_seekoff
;
5811 nfs_buf_itercomplete(dnp
, &blist
, NBI_CLEAN
);
5813 lck_mtx_unlock(nfs_buf_mutex
);
5815 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5819 /* still not found... oh well, just start a new block */
5821 OSAddAtomic64(1, &nfsstats
.direofcache_misses
);
5826 * scan a directory buffer for the given name
5827 * Returns: ESRCH if not found, ENOENT if found invalid, 0 if found
5828 * Note: should only be called with RDIRPLUS directory buffers
5831 #define NDBS_PURGE 1
5832 #define NDBS_UPDATE 2
5837 struct componentname
*cnp
,
5839 struct nfs_vattr
*nvap
,
5842 daddr64_t
*nextlbnp
,
5845 struct direntry
*dp
;
5846 struct nfs_dir_buf_header
*ndbhp
;
5847 struct nfs_vattr
*nvattrp
;
5848 daddr64_t nextlbn
= 0;
5849 int i
, error
= ESRCH
;
5852 /* scan the buffer for the name */
5853 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5854 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5855 for (i
= 0; i
< ndbhp
->ndbh_count
; i
++) {
5856 nextlbn
= dp
->d_seekoff
;
5857 if ((cnp
->cn_namelen
== dp
->d_namlen
) && !strcmp(cnp
->cn_nameptr
, dp
->d_name
)) {
5858 fhlen
= dp
->d_name
[dp
->d_namlen
+ 1];
5859 nvattrp
= NFS_DIR_BUF_NVATTR(bp
, i
);
5860 if ((ndbhp
->ndbh_ncgen
!= bp
->nb_np
->n_ncgen
) || (fhp
->fh_len
== 0) ||
5861 (nvattrp
->nva_type
== VNON
) || (nvattrp
->nva_fileid
== 0)) {
5862 /* entry is not valid */
5866 if (flags
== NDBS_PURGE
) {
5868 bzero(nvattrp
, sizeof(*nvattrp
));
5872 if (flags
== NDBS_UPDATE
) {
5873 /* update direntry's attrs if fh matches */
5874 if ((fhp
->fh_len
== fhlen
) && !bcmp(&dp
->d_name
[dp
->d_namlen
+ 2], fhp
->fh_data
, fhlen
)) {
5875 bcopy(nvap
, nvattrp
, sizeof(*nvap
));
5876 dp
->d_fileno
= nvattrp
->nva_fileid
;
5877 nvattrp
->nva_fileid
= *xidp
;
5878 *(time_t*)(&dp
->d_name
[dp
->d_namlen
+ 2 + fhp
->fh_len
]) = *attrstampp
;
5883 /* copy out fh, attrs, attrstamp, and xid */
5884 fhp
->fh_len
= fhlen
;
5885 bcopy(&dp
->d_name
[dp
->d_namlen
+ 2], fhp
->fh_data
, MAX(fhp
->fh_len
, (int)sizeof(fhp
->fh_data
)));
5886 *attrstampp
= *(time_t*)(&dp
->d_name
[dp
->d_namlen
+ 2 + fhp
->fh_len
]);
5887 bcopy(nvattrp
, nvap
, sizeof(*nvap
));
5888 *xidp
= nvap
->nva_fileid
;
5889 nvap
->nva_fileid
= dp
->d_fileno
;
5893 dp
= NFS_DIRENTRY_NEXT(dp
);
5896 *nextlbnp
= nextlbn
;
5902 * Look up a name in a directory's buffers.
5903 * Note: should only be called with RDIRPLUS directory buffers
5906 nfs_dir_buf_cache_lookup(nfsnode_t dnp
, nfsnode_t
*npp
, struct componentname
*cnp
, vfs_context_t ctx
, int purge
)
5909 struct nfsmount
*nmp
;
5910 int error
= 0, i
, found
= 0, count
= 0;
5912 struct nfs_vattr nvattr
;
5914 time_t attrstamp
= 0;
5915 thread_t thd
= vfs_context_thread(ctx
);
5916 struct nfsbuf
*bp
, *lastbp
, *foundbp
;
5917 struct nfsbuflists blist
;
5918 daddr64_t lbn
, nextlbn
;
5919 int dotunder
= (cnp
->cn_namelen
> 2) && (cnp
->cn_nameptr
[0] == '.') && (cnp
->cn_nameptr
[1] == '_');
5921 nmp
= NFSTONMP(dnp
);
5922 if (nfs_mount_gone(nmp
)) {
5929 /* first check most recent buffer (and next one too) */
5930 lbn
= dnp
->n_lastdbl
;
5931 for (i
= 0; i
< 2; i
++) {
5932 if ((error
= nfs_buf_get(dnp
, lbn
, NFS_DIRBLKSIZ
, thd
, NBLK_READ
| NBLK_ONLYVALID
, &bp
))) {
5939 error
= nfs_dir_buf_search(bp
, cnp
, &fh
, &nvattr
, &xid
, &attrstamp
, &nextlbn
, purge
? NDBS_PURGE
: 0);
5940 nfs_buf_release(bp
, 0);
5941 if (error
== ESRCH
) {
5950 lck_mtx_lock(nfs_buf_mutex
);
5952 dnp
->n_lastdbl
= lbn
;
5957 * Scan the list of buffers, keeping them in order.
5958 * Note that itercomplete inserts each of the remaining buffers
5959 * into the head of list (thus reversing the elements). So, we
5960 * make sure to iterate through all buffers, inserting them after
5961 * each other, to keep them in order.
5962 * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because
5963 * we don't drop nfs_buf_mutex.
5965 if (!nfs_buf_iterprepare(dnp
, &blist
, NBI_CLEAN
)) {
5966 lastbp
= foundbp
= NULL
;
5967 while ((bp
= LIST_FIRST(&blist
))) {
5968 LIST_REMOVE(bp
, nb_vnbufs
);
5970 LIST_INSERT_HEAD(&dnp
->n_cleanblkhd
, bp
, nb_vnbufs
);
5972 LIST_INSERT_AFTER(lastbp
, bp
, nb_vnbufs
);
5975 if (error
|| found
) {
5978 if (!purge
&& dotunder
&& (count
> 100)) { /* don't waste too much time looking for ._ files */
5982 lbn
= bp
->nb_lblkno
;
5983 if (nfs_buf_acquire(bp
, NBAC_NOWAIT
, 0, 0)) {
5984 /* just skip this buffer */
5985 nfs_buf_refrele(bp
);
5988 nfs_buf_refrele(bp
);
5990 error
= nfs_dir_buf_search(bp
, cnp
, &fh
, &nvattr
, &xid
, &attrstamp
, NULL
, purge
? NDBS_PURGE
: 0);
5991 if (error
== ESRCH
) {
6000 LIST_REMOVE(foundbp
, nb_vnbufs
);
6001 LIST_INSERT_HEAD(&dnp
->n_cleanblkhd
, foundbp
, nb_vnbufs
);
6002 dnp
->n_lastdbl
= foundbp
->nb_lblkno
;
6004 nfs_buf_itercomplete(dnp
, &blist
, NBI_CLEAN
);
6007 lck_mtx_unlock(nfs_buf_mutex
);
6009 if (!error
&& found
&& !purge
) {
6010 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
,
6011 &nvattr
, &xid
, dnp
->n_auth
, NG_MAKEENTRY
, &newnp
);
6015 newnp
->n_attrstamp
= attrstamp
;
6017 nfs_node_unlock(newnp
);
6018 /* check if the dir buffer's attrs are out of date */
6019 if (!nfs_getattr(newnp
, &nvattr
, ctx
, NGA_CACHED
) &&
6020 (newnp
->n_attrstamp
!= attrstamp
)) {
6021 /* they are, so update them */
6022 error
= nfs_buf_get(dnp
, lbn
, NFS_DIRBLKSIZ
, thd
, NBLK_READ
| NBLK_ONLYVALID
, &bp
);
6024 attrstamp
= newnp
->n_attrstamp
;
6026 nfs_dir_buf_search(bp
, cnp
, &fh
, &nvattr
, &xid
, &attrstamp
, NULL
, NDBS_UPDATE
);
6027 nfs_buf_release(bp
, 0);
6037 * Purge name cache entries for the given node.
6038 * For RDIRPLUS, also invalidate the entry in the directory's buffers.
6041 nfs_name_cache_purge(nfsnode_t dnp
, nfsnode_t np
, struct componentname
*cnp
, vfs_context_t ctx
)
6043 struct nfsmount
*nmp
= NFSTONMP(dnp
);
6045 cache_purge(NFSTOV(np
));
6046 if (nmp
&& (nmp
->nm_vers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
)) {
6047 nfs_dir_buf_cache_lookup(dnp
, NULL
, cnp
, ctx
, 1);
6052 * NFS V3 readdir (plus) RPC.
6055 nfs3_readdir_rpc(nfsnode_t dnp
, struct nfsbuf
*bp
, vfs_context_t ctx
)
6057 struct nfsmount
*nmp
;
6058 int error
= 0, lockerror
, nfsvers
, rdirplus
, bigcookies
;
6059 int i
, status
, attrflag
, fhflag
, more_entries
= 1, eof
, bp_dropped
= 0;
6060 uint32_t nmreaddirsize
, nmrsize
;
6061 uint32_t namlen
, skiplen
, fhlen
, xlen
, attrlen
, reclen
, space_free
, space_needed
;
6062 uint64_t cookie
, lastcookie
, xid
, savedxid
, fileno
;
6063 struct nfsm_chain nmreq
, nmrep
, nmrepsave
;
6065 struct nfs_vattr
*nvattrp
;
6066 struct nfs_dir_buf_header
*ndbhp
;
6067 struct direntry
*dp
;
6068 char *padstart
, padlen
;
6071 nmp
= NFSTONMP(dnp
);
6072 if (nfs_mount_gone(nmp
)) {
6075 nfsvers
= nmp
->nm_vers
;
6076 nmreaddirsize
= nmp
->nm_readdirsize
;
6077 nmrsize
= nmp
->nm_rsize
;
6078 bigcookies
= nmp
->nm_state
& NFSSTA_BIGCOOKIES
;
6080 rdirplus
= ((nfsvers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
)) ? 1 : 0;
6082 if ((lockerror
= nfs_node_lock(dnp
))) {
6086 /* determine cookie to use, and move dp to the right offset */
6087 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
6088 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
6089 if (ndbhp
->ndbh_count
) {
6090 for (i
= 0; i
< ndbhp
->ndbh_count
- 1; i
++) {
6091 dp
= NFS_DIRENTRY_NEXT(dp
);
6093 cookie
= dp
->d_seekoff
;
6094 dp
= NFS_DIRENTRY_NEXT(dp
);
6096 cookie
= bp
->nb_lblkno
;
6097 /* increment with every buffer read */
6098 OSAddAtomic64(1, &nfsstats
.readdir_bios
);
6100 lastcookie
= cookie
;
6103 * Loop around doing readdir(plus) RPCs of size nm_readdirsize until
6104 * the buffer is full (or we hit EOF). Then put the remainder of the
6105 * results in the next buffer(s).
6107 nfsm_chain_null(&nmreq
);
6108 nfsm_chain_null(&nmrep
);
6109 while (nfs_dir_buf_freespace(bp
, rdirplus
) && !(ndbhp
->ndbh_flags
& NDB_FULL
)) {
6110 nfsm_chain_build_alloc_init(error
, &nmreq
,
6111 NFSX_FH(nfsvers
) + NFSX_READDIR(nfsvers
) + NFSX_UNSIGNED
);
6112 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
6113 if (nfsvers
== NFS_VER3
) {
6114 /* opaque values don't need swapping, but as long */
6115 /* as we are consistent about it, it should be ok */
6116 nfsm_chain_add_64(error
, &nmreq
, cookie
);
6117 nfsm_chain_add_64(error
, &nmreq
, dnp
->n_cookieverf
);
6119 nfsm_chain_add_32(error
, &nmreq
, cookie
);
6121 nfsm_chain_add_32(error
, &nmreq
, nmreaddirsize
);
6123 nfsm_chain_add_32(error
, &nmreq
, nmrsize
);
6125 nfsm_chain_build_done(error
, &nmreq
);
6126 nfs_node_unlock(dnp
);
6130 error
= nfs_request(dnp
, NULL
, &nmreq
,
6131 rdirplus
? NFSPROC_READDIRPLUS
: NFSPROC_READDIR
,
6132 ctx
, NULL
, &nmrep
, &xid
, &status
);
6134 if ((lockerror
= nfs_node_lock(dnp
))) {
6139 if (nfsvers
== NFS_VER3
) {
6140 nfsm_chain_postop_attr_update(error
, &nmrep
, dnp
, &xid
);
6145 if (nfsvers
== NFS_VER3
) {
6146 nfsm_chain_get_64(error
, &nmrep
, dnp
->n_cookieverf
);
6148 nfsm_chain_get_32(error
, &nmrep
, more_entries
);
6151 nfs_node_unlock(dnp
);
6154 if (error
== NFSERR_NOTSUPP
) {
6155 /* oops... it doesn't look like readdirplus is supported */
6156 lck_mtx_lock(&nmp
->nm_lock
);
6157 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_RDIRPLUS
);
6158 lck_mtx_unlock(&nmp
->nm_lock
);
6167 /* loop through the entries packing them into the buffer */
6168 while (more_entries
) {
6169 if (nfsvers
== NFS_VER3
) {
6170 nfsm_chain_get_64(error
, &nmrep
, fileno
);
6172 nfsm_chain_get_32(error
, &nmrep
, fileno
);
6174 nfsm_chain_get_32(error
, &nmrep
, namlen
);
6176 /* just truncate names that don't fit in direntry.d_name */
6181 if (namlen
> (sizeof(dp
->d_name
) - 1)) {
6182 skiplen
= namlen
- sizeof(dp
->d_name
) + 1;
6183 namlen
= sizeof(dp
->d_name
) - 1;
6187 /* guess that fh size will be same as parent */
6188 fhlen
= rdirplus
? (1 + dnp
->n_fhsize
) : 0;
6189 xlen
= rdirplus
? (fhlen
+ sizeof(time_t)) : 0;
6190 attrlen
= rdirplus
? sizeof(struct nfs_vattr
) : 0;
6191 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
6192 space_needed
= reclen
+ attrlen
;
6193 space_free
= nfs_dir_buf_freespace(bp
, rdirplus
);
6194 if (space_needed
> space_free
) {
6196 * We still have entries to pack, but we've
6197 * run out of room in the current buffer.
6198 * So we need to move to the next buffer.
6199 * The block# for the next buffer is the
6200 * last cookie in the current buffer.
6203 ndbhp
->ndbh_flags
|= NDB_FULL
;
6204 nfs_buf_release(bp
, 0);
6207 error
= nfs_buf_get(dnp
, lastcookie
, NFS_DIRBLKSIZ
, vfs_context_thread(ctx
), NBLK_READ
, &bp
);
6209 /* initialize buffer */
6210 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
6211 ndbhp
->ndbh_flags
= 0;
6212 ndbhp
->ndbh_count
= 0;
6213 ndbhp
->ndbh_entry_end
= sizeof(*ndbhp
);
6214 ndbhp
->ndbh_ncgen
= dnp
->n_ncgen
;
6215 space_free
= nfs_dir_buf_freespace(bp
, rdirplus
);
6216 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
6217 /* increment with every buffer read */
6218 OSAddAtomic64(1, &nfsstats
.readdir_bios
);
6221 dp
->d_fileno
= fileno
;
6222 dp
->d_namlen
= namlen
;
6223 dp
->d_reclen
= reclen
;
6224 dp
->d_type
= DT_UNKNOWN
;
6225 nfsm_chain_get_opaque(error
, &nmrep
, namlen
, dp
->d_name
);
6227 dp
->d_name
[namlen
] = '\0';
6229 nfsm_chain_adv(error
, &nmrep
,
6230 nfsm_rndup(namlen
+ skiplen
) - nfsm_rndup(namlen
));
6232 if (nfsvers
== NFS_VER3
) {
6233 nfsm_chain_get_64(error
, &nmrep
, cookie
);
6235 nfsm_chain_get_32(error
, &nmrep
, cookie
);
6238 dp
->d_seekoff
= cookie
;
6239 if (!bigcookies
&& (cookie
>> 32) && (nmp
== NFSTONMP(dnp
))) {
6240 /* we've got a big cookie, make sure flag is set */
6241 lck_mtx_lock(&nmp
->nm_lock
);
6242 nmp
->nm_state
|= NFSSTA_BIGCOOKIES
;
6243 lck_mtx_unlock(&nmp
->nm_lock
);
6247 nvattrp
= NFS_DIR_BUF_NVATTR(bp
, ndbhp
->ndbh_count
);
6248 /* check for attributes */
6249 nfsm_chain_get_32(error
, &nmrep
, attrflag
);
6252 /* grab attributes */
6253 error
= nfs_parsefattr(&nmrep
, NFS_VER3
, nvattrp
);
6255 dp
->d_type
= IFTODT(VTTOIF(nvattrp
->nva_type
));
6256 /* fileid is already in d_fileno, so stash xid in attrs */
6257 nvattrp
->nva_fileid
= savedxid
;
6259 /* mark the attributes invalid */
6260 bzero(nvattrp
, sizeof(struct nfs_vattr
));
6262 /* check for file handle */
6263 nfsm_chain_get_32(error
, &nmrep
, fhflag
);
6266 nfsm_chain_get_fh(error
, &nmrep
, NFS_VER3
, &fh
);
6268 fhlen
= fh
.fh_len
+ 1;
6269 xlen
= fhlen
+ sizeof(time_t);
6270 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
6271 space_needed
= reclen
+ attrlen
;
6272 if (space_needed
> space_free
) {
6273 /* didn't actually have the room... move on to next buffer */
6277 /* pack the file handle into the record */
6278 dp
->d_name
[dp
->d_namlen
+ 1] = fh
.fh_len
;
6279 bcopy(fh
.fh_data
, &dp
->d_name
[dp
->d_namlen
+ 2], fh
.fh_len
);
6281 /* mark the file handle invalid */
6283 fhlen
= fh
.fh_len
+ 1;
6284 xlen
= fhlen
+ sizeof(time_t);
6285 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
6286 bzero(&dp
->d_name
[dp
->d_namlen
+ 1], fhlen
);
6288 *(time_t*)(&dp
->d_name
[dp
->d_namlen
+ 1 + fhlen
]) = now
.tv_sec
;
6289 dp
->d_reclen
= reclen
;
6291 padstart
= dp
->d_name
+ dp
->d_namlen
+ 1 + xlen
;
6292 ndbhp
->ndbh_count
++;
6293 lastcookie
= cookie
;
6294 /* advance to next direntry in buffer */
6295 dp
= NFS_DIRENTRY_NEXT(dp
);
6296 ndbhp
->ndbh_entry_end
= (char*)dp
- bp
->nb_data
;
6297 /* zero out the pad bytes */
6298 padlen
= (char*)dp
- padstart
;
6300 bzero(padstart
, padlen
);
6302 /* check for more entries */
6303 nfsm_chain_get_32(error
, &nmrep
, more_entries
);
6306 /* Finally, get the eof boolean */
6307 nfsm_chain_get_32(error
, &nmrep
, eof
);
6310 ndbhp
->ndbh_flags
|= (NDB_FULL
| NDB_EOF
);
6311 nfs_node_lock_force(dnp
);
6312 dnp
->n_eofcookie
= lastcookie
;
6313 nfs_node_unlock(dnp
);
6318 nfs_buf_release(bp
, 0);
6322 if ((lockerror
= nfs_node_lock(dnp
))) {
6326 nfsm_chain_cleanup(&nmrep
);
6327 nfsm_chain_null(&nmreq
);
6330 if (bp_dropped
&& bp
) {
6331 nfs_buf_release(bp
, 0);
6334 nfs_node_unlock(dnp
);
6336 nfsm_chain_cleanup(&nmreq
);
6337 nfsm_chain_cleanup(&nmrep
);
6338 return bp_dropped
? NFSERR_DIRBUFDROPPED
: error
;
6342 * Silly rename. To make the NFS filesystem that is stateless look a little
6343 * more like the "ufs" a remove of an active vnode is translated to a rename
6344 * to a funny looking filename that is removed by nfs_vnop_inactive on the
6345 * nfsnode. There is the potential for another process on a different client
6346 * to create the same funny name between when the lookitup() fails and the
6347 * rename() completes, but...
6350 /* format of "random" silly names - includes a number and pid */
6351 /* (note: shouldn't exceed size of nfs_sillyrename.nsr_name) */
6352 #define NFS_SILLYNAME_FORMAT ".nfs.%08x.%04x"
6353 /* starting from zero isn't silly enough */
6354 static uint32_t nfs_sillyrename_number
= 0x20051025;
6360 struct componentname
*cnp
,
6363 struct nfs_sillyrename
*nsp
;
6368 struct nfsmount
*nmp
;
6370 nmp
= NFSTONMP(dnp
);
6371 if (nfs_mount_gone(nmp
)) {
6375 nfs_name_cache_purge(dnp
, np
, cnp
, ctx
);
6377 MALLOC_ZONE(nsp
, struct nfs_sillyrename
*,
6378 sizeof(struct nfs_sillyrename
), M_NFSREQ
, M_WAITOK
);
6382 cred
= vfs_context_ucred(ctx
);
6383 kauth_cred_ref(cred
);
6384 nsp
->nsr_cred
= cred
;
6386 error
= vnode_ref(NFSTOV(dnp
));
6391 /* Fudge together a funny name */
6392 pid
= vfs_context_pid(ctx
);
6393 num
= OSAddAtomic(1, &nfs_sillyrename_number
);
6394 nsp
->nsr_namlen
= snprintf(nsp
->nsr_name
, sizeof(nsp
->nsr_name
),
6395 NFS_SILLYNAME_FORMAT
, num
, (pid
& 0xffff));
6396 if (nsp
->nsr_namlen
>= (int)sizeof(nsp
->nsr_name
)) {
6397 nsp
->nsr_namlen
= sizeof(nsp
->nsr_name
) - 1;
6400 /* Try lookitups until we get one that isn't there */
6401 while (nfs_lookitup(dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, ctx
, NULL
) == 0) {
6402 num
= OSAddAtomic(1, &nfs_sillyrename_number
);
6403 nsp
->nsr_namlen
= snprintf(nsp
->nsr_name
, sizeof(nsp
->nsr_name
),
6404 NFS_SILLYNAME_FORMAT
, num
, (pid
& 0xffff));
6405 if (nsp
->nsr_namlen
>= (int)sizeof(nsp
->nsr_name
)) {
6406 nsp
->nsr_namlen
= sizeof(nsp
->nsr_name
) - 1;
6410 /* now, do the rename */
6411 error
= nmp
->nm_funcs
->nf_rename_rpc(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
,
6412 dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, ctx
);
6414 /* Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. */
6415 if (error
== ENOENT
) {
6419 nfs_node_lock_force(dnp
);
6420 if (dnp
->n_flag
& NNEGNCENTRIES
) {
6421 dnp
->n_flag
&= ~NNEGNCENTRIES
;
6422 cache_purge_negatives(NFSTOV(dnp
));
6424 nfs_node_unlock(dnp
);
6426 FSDBG(267, dnp
, np
, num
, error
);
6430 error
= nfs_lookitup(dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, ctx
, &np
);
6431 nfs_node_lock_force(np
);
6432 np
->n_sillyrename
= nsp
;
6433 nfs_node_unlock(np
);
6436 vnode_rele(NFSTOV(dnp
));
6438 nsp
->nsr_cred
= NOCRED
;
6439 kauth_cred_unref(&cred
);
6440 FREE_ZONE(nsp
, sizeof(*nsp
), M_NFSREQ
);
6445 nfs3_lookup_rpc_async(
6450 struct nfsreq
**reqp
)
6452 struct nfsmount
*nmp
;
6453 struct nfsm_chain nmreq
;
6454 int error
= 0, nfsvers
;
6456 nmp
= NFSTONMP(dnp
);
6457 if (nfs_mount_gone(nmp
)) {
6460 nfsvers
= nmp
->nm_vers
;
6462 nfsm_chain_null(&nmreq
);
6464 nfsm_chain_build_alloc_init(error
, &nmreq
,
6465 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+ nfsm_rndup(namelen
));
6466 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
6467 nfsm_chain_add_name(error
, &nmreq
, name
, namelen
, nmp
);
6468 nfsm_chain_build_done(error
, &nmreq
);
6470 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_LOOKUP
,
6471 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, reqp
);
6473 nfsm_chain_cleanup(&nmreq
);
6478 nfs3_lookup_rpc_async_finish(
6480 __unused
char *name
,
6481 __unused
int namelen
,
6486 struct nfs_vattr
*nvap
)
6488 int error
= 0, lockerror
= ENOENT
, status
, nfsvers
, attrflag
;
6490 struct nfsmount
*nmp
;
6491 struct nfsm_chain nmrep
;
6493 nmp
= NFSTONMP(dnp
);
6497 nfsvers
= nmp
->nm_vers
;
6499 nfsm_chain_null(&nmrep
);
6501 error
= nfs_request_async_finish(req
, &nmrep
, xidp
, &status
);
6503 if ((lockerror
= nfs_node_lock(dnp
))) {
6507 if (error
|| status
) {
6508 if (nfsvers
== NFS_VER3
) {
6509 nfsm_chain_postop_attr_update(error
, &nmrep
, dnp
, &xid
);
6517 nfsmout_if(error
|| !fhp
|| !nvap
);
6519 /* get the file handle */
6520 nfsm_chain_get_fh(error
, &nmrep
, nfsvers
, fhp
);
6522 /* get the attributes */
6523 if (nfsvers
== NFS_VER3
) {
6524 nfsm_chain_postop_attr_get(error
, &nmrep
, attrflag
, nvap
);
6525 nfsm_chain_postop_attr_update(error
, &nmrep
, dnp
, &xid
);
6526 if (!error
&& !attrflag
) {
6527 error
= nfs3_getattr_rpc(NULL
, NFSTOMP(dnp
), fhp
->fh_data
, fhp
->fh_len
, 0, ctx
, nvap
, xidp
);
6530 error
= nfs_parsefattr(&nmrep
, nfsvers
, nvap
);
6534 nfs_node_unlock(dnp
);
6536 nfsm_chain_cleanup(&nmrep
);
6541 * Look up a file name and optionally either update the file handle or
6542 * allocate an nfsnode, depending on the value of npp.
6543 * npp == NULL --> just do the lookup
6544 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
6546 * *npp != NULL --> update the file handle in the vnode
6557 nfsnode_t np
, newnp
= NULL
;
6560 struct nfsmount
*nmp
;
6561 struct nfs_vattr nvattr
;
6562 struct nfsreq rq
, *req
= &rq
;
6564 nmp
= NFSTONMP(dnp
);
6565 if (nfs_mount_gone(nmp
)) {
6569 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXNAME
) &&
6570 (namelen
> (int)nmp
->nm_fsattr
.nfsa_maxname
)) {
6571 return ENAMETOOLONG
;
6574 NVATTR_INIT(&nvattr
);
6576 /* check for lookup of "." */
6577 if ((name
[0] == '.') && (namelen
== 1)) {
6578 /* skip lookup, we know who we are */
6584 error
= nmp
->nm_funcs
->nf_lookup_rpc_async(dnp
, name
, namelen
, ctx
, &req
);
6586 error
= nmp
->nm_funcs
->nf_lookup_rpc_async_finish(dnp
, name
, namelen
, ctx
, req
, &xid
, &fh
, &nvattr
);
6587 nfsmout_if(!npp
|| error
);
6591 if (fh
.fh_len
!= np
->n_fhsize
) {
6592 u_char
*oldbuf
= (np
->n_fhsize
> NFS_SMALLFH
) ? np
->n_fhp
: NULL
;
6593 if (fh
.fh_len
> NFS_SMALLFH
) {
6594 MALLOC_ZONE(np
->n_fhp
, u_char
*, fh
.fh_len
, M_NFSBIGFH
, M_WAITOK
);
6601 np
->n_fhp
= &np
->n_fh
[0];
6604 FREE_ZONE(oldbuf
, np
->n_fhsize
, M_NFSBIGFH
);
6607 bcopy(fh
.fh_data
, np
->n_fhp
, fh
.fh_len
);
6608 np
->n_fhsize
= fh
.fh_len
;
6609 nfs_node_lock_force(np
);
6610 error
= nfs_loadattrcache(np
, &nvattr
, &xid
, 0);
6611 nfs_node_unlock(np
);
6614 } else if (NFS_CMPFH(dnp
, fh
.fh_data
, fh
.fh_len
)) {
6615 nfs_node_lock_force(dnp
);
6616 if (dnp
->n_xid
<= xid
) {
6617 error
= nfs_loadattrcache(dnp
, &nvattr
, &xid
, 0);
6619 nfs_node_unlock(dnp
);
6623 struct componentname cn
, *cnp
= &cn
;
6624 bzero(cnp
, sizeof(*cnp
));
6625 cnp
->cn_nameptr
= name
;
6626 cnp
->cn_namelen
= namelen
;
6627 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
,
6628 &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
6634 if (npp
&& !*npp
&& !error
) {
6637 NVATTR_CLEANUP(&nvattr
);
6642 * set up and initialize a "._" file lookup structure used for
6643 * performing async lookups.
6646 nfs_dulookup_init(struct nfs_dulookup
*dulp
, nfsnode_t dnp
, const char *name
, int namelen
, vfs_context_t ctx
)
6648 int error
, du_namelen
;
6650 struct nfsmount
*nmp
= NFSTONMP(dnp
);
6652 /* check for ._ file in name cache */
6654 bzero(&dulp
->du_cn
, sizeof(dulp
->du_cn
));
6655 du_namelen
= namelen
+ 2;
6656 if (!nmp
|| NMFLAG(nmp
, NONEGNAMECACHE
)) {
6659 if ((namelen
>= 2) && (name
[0] == '.') && (name
[1] == '_')) {
6662 if (du_namelen
>= (int)sizeof(dulp
->du_smallname
)) {
6663 MALLOC(dulp
->du_cn
.cn_nameptr
, char *, du_namelen
+ 1, M_TEMP
, M_WAITOK
);
6665 dulp
->du_cn
.cn_nameptr
= dulp
->du_smallname
;
6667 if (!dulp
->du_cn
.cn_nameptr
) {
6670 dulp
->du_cn
.cn_namelen
= du_namelen
;
6671 snprintf(dulp
->du_cn
.cn_nameptr
, du_namelen
+ 1, "._%s", name
);
6672 dulp
->du_cn
.cn_nameptr
[du_namelen
] = '\0';
6673 dulp
->du_cn
.cn_nameiop
= LOOKUP
;
6674 dulp
->du_cn
.cn_flags
= MAKEENTRY
;
6676 error
= cache_lookup(NFSTOV(dnp
), &du_vp
, &dulp
->du_cn
);
6679 } else if (!error
) {
6680 nmp
= NFSTONMP(dnp
);
6681 if (nmp
&& (nmp
->nm_vers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
)) {
6682 /* if rdirplus, try dir buf cache lookup */
6683 nfsnode_t du_np
= NULL
;
6684 if (!nfs_dir_buf_cache_lookup(dnp
, &du_np
, &dulp
->du_cn
, ctx
, 0) && du_np
) {
6685 /* dir buf cache hit */
6686 du_vp
= NFSTOV(du_np
);
6692 dulp
->du_flags
|= NFS_DULOOKUP_DOIT
;
6698 * start an async "._" file lookup request
6701 nfs_dulookup_start(struct nfs_dulookup
*dulp
, nfsnode_t dnp
, vfs_context_t ctx
)
6703 struct nfsmount
*nmp
= NFSTONMP(dnp
);
6704 struct nfsreq
*req
= &dulp
->du_req
;
6706 if (!nmp
|| !(dulp
->du_flags
& NFS_DULOOKUP_DOIT
) || (dulp
->du_flags
& NFS_DULOOKUP_INPROG
)) {
6709 if (!nmp
->nm_funcs
->nf_lookup_rpc_async(dnp
, dulp
->du_cn
.cn_nameptr
,
6710 dulp
->du_cn
.cn_namelen
, ctx
, &req
)) {
6711 dulp
->du_flags
|= NFS_DULOOKUP_INPROG
;
6716 * finish an async "._" file lookup request and clean up the structure
6719 nfs_dulookup_finish(struct nfs_dulookup
*dulp
, nfsnode_t dnp
, vfs_context_t ctx
)
6721 struct nfsmount
*nmp
= NFSTONMP(dnp
);
6726 struct nfs_vattr nvattr
;
6728 if (!nmp
|| !(dulp
->du_flags
& NFS_DULOOKUP_INPROG
)) {
6732 NVATTR_INIT(&nvattr
);
6733 error
= nmp
->nm_funcs
->nf_lookup_rpc_async_finish(dnp
, dulp
->du_cn
.cn_nameptr
,
6734 dulp
->du_cn
.cn_namelen
, ctx
, &dulp
->du_req
, &xid
, &fh
, &nvattr
);
6735 dulp
->du_flags
&= ~NFS_DULOOKUP_INPROG
;
6736 if (error
== ENOENT
) {
6737 /* add a negative entry in the name cache */
6738 nfs_node_lock_force(dnp
);
6739 cache_enter(NFSTOV(dnp
), NULL
, &dulp
->du_cn
);
6740 dnp
->n_flag
|= NNEGNCENTRIES
;
6741 nfs_node_unlock(dnp
);
6742 } else if (!error
) {
6743 error
= nfs_nget(NFSTOMP(dnp
), dnp
, &dulp
->du_cn
, fh
.fh_data
, fh
.fh_len
,
6744 &nvattr
, &xid
, dulp
->du_req
.r_auth
, NG_MAKEENTRY
, &du_np
);
6746 nfs_node_unlock(du_np
);
6747 vnode_put(NFSTOV(du_np
));
6750 NVATTR_CLEANUP(&nvattr
);
6752 if (dulp
->du_flags
& NFS_DULOOKUP_INPROG
) {
6753 nfs_request_async_cancel(&dulp
->du_req
);
6755 if (dulp
->du_cn
.cn_nameptr
&& (dulp
->du_cn
.cn_nameptr
!= dulp
->du_smallname
)) {
6756 FREE(dulp
->du_cn
.cn_nameptr
, M_TEMP
);
6762 * NFS Version 3 commit RPC
6772 struct nfsmount
*nmp
;
6773 int error
= 0, lockerror
, status
, wccpostattr
= 0, nfsvers
;
6774 struct timespec premtime
= { 0, 0 };
6775 u_int64_t xid
, newwverf
;
6777 struct nfsm_chain nmreq
, nmrep
;
6780 FSDBG(521, np
, offset
, count
, nmp
? nmp
->nm_state
: 0);
6781 if (nfs_mount_gone(nmp
)) {
6784 if (!(nmp
->nm_state
& NFSSTA_HASWRITEVERF
)) {
6787 nfsvers
= nmp
->nm_vers
;
6789 if (count
> UINT32_MAX
) {
6795 nfsm_chain_null(&nmreq
);
6796 nfsm_chain_null(&nmrep
);
6798 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(NFS_VER3
));
6799 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
6800 nfsm_chain_add_64(error
, &nmreq
, offset
);
6801 nfsm_chain_add_32(error
, &nmreq
, count32
);
6802 nfsm_chain_build_done(error
, &nmreq
);
6804 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC_COMMIT
,
6805 current_thread(), cred
, NULL
, 0, &nmrep
, &xid
, &status
);
6806 if ((lockerror
= nfs_node_lock(np
))) {
6809 /* can we do anything useful with the wcc info? */
6810 nfsm_chain_get_wcc_data(error
, &nmrep
, np
, &premtime
, &wccpostattr
, &xid
);
6812 nfs_node_unlock(np
);
6817 nfsm_chain_get_64(error
, &nmrep
, newwverf
);
6819 lck_mtx_lock(&nmp
->nm_lock
);
6820 if (nmp
->nm_verf
!= newwverf
) {
6821 nmp
->nm_verf
= newwverf
;
6823 if (wverf
!= newwverf
) {
6824 error
= NFSERR_STALEWRITEVERF
;
6826 lck_mtx_unlock(&nmp
->nm_lock
);
6828 nfsm_chain_cleanup(&nmreq
);
6829 nfsm_chain_cleanup(&nmrep
);
6836 __unused
struct vnop_blockmap_args
/* {
6837 * struct vnodeop_desc *a_desc;
6852 * fsync vnode op. Just call nfs_flush().
6857 struct vnop_fsync_args
/* {
6858 * struct vnodeop_desc *a_desc;
6861 * vfs_context_t a_context;
6864 return nfs_flush(VTONFS(ap
->a_vp
), ap
->a_waitfor
, vfs_context_thread(ap
->a_context
), 0);
6869 * Do an NFS pathconf RPC.
6874 struct nfs_fsattr
*nfsap
,
6878 int error
= 0, lockerror
, status
, nfsvers
;
6879 struct nfsm_chain nmreq
, nmrep
;
6880 struct nfsmount
*nmp
= NFSTONMP(np
);
6883 if (nfs_mount_gone(nmp
)) {
6886 nfsvers
= nmp
->nm_vers
;
6888 nfsm_chain_null(&nmreq
);
6889 nfsm_chain_null(&nmrep
);
6891 /* fetch pathconf info from server */
6892 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(NFS_VER3
));
6893 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
6894 nfsm_chain_build_done(error
, &nmreq
);
6896 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_PATHCONF
, ctx
, NULL
, &nmrep
, &xid
, &status
);
6897 if ((lockerror
= nfs_node_lock(np
))) {
6900 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
6902 nfs_node_unlock(np
);
6907 nfsm_chain_get_32(error
, &nmrep
, nfsap
->nfsa_maxlink
);
6908 nfsm_chain_get_32(error
, &nmrep
, nfsap
->nfsa_maxname
);
6909 nfsap
->nfsa_flags
&= ~(NFS_FSFLAG_NO_TRUNC
| NFS_FSFLAG_CHOWN_RESTRICTED
| NFS_FSFLAG_CASE_INSENSITIVE
| NFS_FSFLAG_CASE_PRESERVING
);
6910 nfsm_chain_get_32(error
, &nmrep
, val
);
6912 nfsap
->nfsa_flags
|= NFS_FSFLAG_NO_TRUNC
;
6914 nfsm_chain_get_32(error
, &nmrep
, val
);
6916 nfsap
->nfsa_flags
|= NFS_FSFLAG_CHOWN_RESTRICTED
;
6918 nfsm_chain_get_32(error
, &nmrep
, val
);
6920 nfsap
->nfsa_flags
|= NFS_FSFLAG_CASE_INSENSITIVE
;
6922 nfsm_chain_get_32(error
, &nmrep
, val
);
6924 nfsap
->nfsa_flags
|= NFS_FSFLAG_CASE_PRESERVING
;
6926 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXLINK
);
6927 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXNAME
);
6928 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_NO_TRUNC
);
6929 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_CHOWN_RESTRICTED
);
6930 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_INSENSITIVE
);
6931 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_PRESERVING
);
6933 nfsm_chain_cleanup(&nmreq
);
6934 nfsm_chain_cleanup(&nmrep
);
6938 /* save pathconf info for NFSv3 mount */
6940 nfs3_pathconf_cache(struct nfsmount
*nmp
, struct nfs_fsattr
*nfsap
)
6942 nmp
->nm_fsattr
.nfsa_maxlink
= nfsap
->nfsa_maxlink
;
6943 nmp
->nm_fsattr
.nfsa_maxname
= nfsap
->nfsa_maxname
;
6944 nmp
->nm_fsattr
.nfsa_flags
&= ~(NFS_FSFLAG_NO_TRUNC
| NFS_FSFLAG_CHOWN_RESTRICTED
| NFS_FSFLAG_CASE_INSENSITIVE
| NFS_FSFLAG_CASE_PRESERVING
);
6945 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_NO_TRUNC
;
6946 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_CHOWN_RESTRICTED
;
6947 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_INSENSITIVE
;
6948 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_PRESERVING
;
6949 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXLINK
);
6950 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXNAME
);
6951 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_NO_TRUNC
);
6952 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CHOWN_RESTRICTED
);
6953 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CASE_INSENSITIVE
);
6954 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CASE_PRESERVING
);
6955 nmp
->nm_state
|= NFSSTA_GOTPATHCONF
;
6959 * Return POSIX pathconf information applicable to nfs.
6961 * The NFS V2 protocol doesn't support this, so just return EINVAL
6967 struct vnop_pathconf_args
/* {
6968 * struct vnodeop_desc *a_desc;
6971 * int32_t *a_retval;
6972 * vfs_context_t a_context;
6975 vnode_t vp
= ap
->a_vp
;
6976 nfsnode_t np
= VTONFS(vp
);
6977 struct nfsmount
*nmp
;
6978 struct nfs_fsattr nfsa
, *nfsap
;
6980 uint64_t maxFileSize
;
6984 if (nfs_mount_gone(nmp
)) {
6988 switch (ap
->a_name
) {
6991 case _PC_CHOWN_RESTRICTED
:
6993 case _PC_CASE_SENSITIVE
:
6994 case _PC_CASE_PRESERVING
:
6996 case _PC_FILESIZEBITS
:
6997 if (nmp
->nm_vers
== NFS_VER2
) {
7002 case _PC_XATTR_SIZE_BITS
:
7003 /* Do we support xattrs natively? */
7004 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
) {
7007 /* No... so just return an error */
7010 /* don't bother contacting the server if we know the answer */
7014 if (nmp
->nm_vers
== NFS_VER2
) {
7018 lck_mtx_lock(&nmp
->nm_lock
);
7019 if (nmp
->nm_vers
== NFS_VER3
) {
7020 if (!(nmp
->nm_state
& NFSSTA_GOTPATHCONF
)) {
7021 /* no pathconf info cached */
7022 lck_mtx_unlock(&nmp
->nm_lock
);
7023 NFS_CLEAR_ATTRIBUTES(nfsa
.nfsa_bitmap
);
7024 error
= nfs3_pathconf_rpc(np
, &nfsa
, ap
->a_context
);
7029 if (nfs_mount_gone(nmp
)) {
7032 lck_mtx_lock(&nmp
->nm_lock
);
7033 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_HOMOGENEOUS
) {
7034 /* all files have the same pathconf info, */
7035 /* so cache a copy of the results */
7036 nfs3_pathconf_cache(nmp
, &nfsa
);
7040 nfsap
= &nmp
->nm_fsattr
;
7042 } else if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_HOMOGENEOUS
)) {
7043 /* no pathconf info cached */
7044 lck_mtx_unlock(&nmp
->nm_lock
);
7045 NFS_CLEAR_ATTRIBUTES(nfsa
.nfsa_bitmap
);
7046 error
= nfs4_pathconf_rpc(np
, &nfsa
, ap
->a_context
);
7051 if (nfs_mount_gone(nmp
)) {
7054 lck_mtx_lock(&nmp
->nm_lock
);
7057 nfsap
= &nmp
->nm_fsattr
;
7060 switch (ap
->a_name
) {
7062 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXLINK
)) {
7063 *ap
->a_retval
= nfsap
->nfsa_maxlink
;
7064 } else if ((nmp
->nm_vers
== NFS_VER4
) && NFS_BITMAP_ISSET(np
->n_vattr
.nva_bitmap
, NFS_FATTR_MAXLINK
)) {
7065 *ap
->a_retval
= np
->n_vattr
.nva_maxlink
;
7071 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXNAME
)) {
7072 *ap
->a_retval
= nfsap
->nfsa_maxname
;
7077 case _PC_CHOWN_RESTRICTED
:
7078 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_CHOWN_RESTRICTED
)) {
7079 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_CHOWN_RESTRICTED
) ? 200112 /* _POSIX_CHOWN_RESTRICTED */ : 0;
7085 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_NO_TRUNC
)) {
7086 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_NO_TRUNC
) ? 200112 /* _POSIX_NO_TRUNC */ : 0;
7091 case _PC_CASE_SENSITIVE
:
7092 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_INSENSITIVE
)) {
7093 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_INSENSITIVE
) ? 0 : 1;
7098 case _PC_CASE_PRESERVING
:
7099 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_PRESERVING
)) {
7100 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_PRESERVING
) ? 1 : 0;
7105 case _PC_XATTR_SIZE_BITS
: /* same as file size bits if named attrs supported */
7106 case _PC_FILESIZEBITS
:
7107 if (!NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXFILESIZE
)) {
7112 maxFileSize
= nfsap
->nfsa_maxfilesize
;
7114 if (maxFileSize
& 0xffffffff00000000ULL
) {
7118 if (maxFileSize
& 0xffff0000) {
7122 if (maxFileSize
& 0xff00) {
7126 if (maxFileSize
& 0xf0) {
7130 if (maxFileSize
& 0xc) {
7134 if (maxFileSize
& 0x2) {
7137 *ap
->a_retval
= nbits
;
7143 lck_mtx_unlock(&nmp
->nm_lock
);
7149 * Read wrapper for special devices.
7153 struct vnop_read_args
/* {
7154 * struct vnodeop_desc *a_desc;
7156 * struct uio *a_uio;
7158 * vfs_context_t a_context;
7161 nfsnode_t np
= VTONFS(ap
->a_vp
);
7162 struct timespec now
;
7168 if ((error
= nfs_node_lock(np
))) {
7173 np
->n_atim
.tv_sec
= now
.tv_sec
;
7174 np
->n_atim
.tv_nsec
= now
.tv_nsec
;
7175 nfs_node_unlock(np
);
7176 return VOCALL(spec_vnodeop_p
, VOFFSET(vnop_read
), ap
);
7180 * Write wrapper for special devices.
7184 struct vnop_write_args
/* {
7185 * struct vnodeop_desc *a_desc;
7187 * struct uio *a_uio;
7189 * vfs_context_t a_context;
7192 nfsnode_t np
= VTONFS(ap
->a_vp
);
7193 struct timespec now
;
7199 if ((error
= nfs_node_lock(np
))) {
7204 np
->n_mtim
.tv_sec
= now
.tv_sec
;
7205 np
->n_mtim
.tv_nsec
= now
.tv_nsec
;
7206 nfs_node_unlock(np
);
7207 return VOCALL(spec_vnodeop_p
, VOFFSET(vnop_write
), ap
);
7211 * Close wrapper for special devices.
7213 * Update the times on the nfsnode then do device close.
7217 struct vnop_close_args
/* {
7218 * struct vnodeop_desc *a_desc;
7221 * vfs_context_t a_context;
7224 vnode_t vp
= ap
->a_vp
;
7225 nfsnode_t np
= VTONFS(vp
);
7226 struct vnode_attr vattr
;
7230 if ((error
= nfs_node_lock(np
))) {
7233 if (np
->n_flag
& (NACC
| NUPD
)) {
7235 if (!vnode_isinuse(vp
, 0) && (mp
= vnode_mount(vp
)) && !vfs_isrdonly(mp
)) {
7237 if (np
->n_flag
& NACC
) {
7238 vattr
.va_access_time
= np
->n_atim
;
7239 VATTR_SET_ACTIVE(&vattr
, va_access_time
);
7241 if (np
->n_flag
& NUPD
) {
7242 vattr
.va_modify_time
= np
->n_mtim
;
7243 VATTR_SET_ACTIVE(&vattr
, va_modify_time
);
7245 nfs_node_unlock(np
);
7246 vnode_setattr(vp
, &vattr
, ap
->a_context
);
7248 nfs_node_unlock(np
);
7251 nfs_node_unlock(np
);
7253 return VOCALL(spec_vnodeop_p
, VOFFSET(vnop_close
), ap
);
7257 extern vnop_t
**fifo_vnodeop_p
;
7260 * Read wrapper for fifos.
7264 struct vnop_read_args
/* {
7265 * struct vnodeop_desc *a_desc;
7267 * struct uio *a_uio;
7269 * vfs_context_t a_context;
7272 nfsnode_t np
= VTONFS(ap
->a_vp
);
7273 struct timespec now
;
7279 if ((error
= nfs_node_lock(np
))) {
7284 np
->n_atim
.tv_sec
= now
.tv_sec
;
7285 np
->n_atim
.tv_nsec
= now
.tv_nsec
;
7286 nfs_node_unlock(np
);
7287 return VOCALL(fifo_vnodeop_p
, VOFFSET(vnop_read
), ap
);
7291 * Write wrapper for fifos.
7295 struct vnop_write_args
/* {
7296 * struct vnodeop_desc *a_desc;
7298 * struct uio *a_uio;
7300 * vfs_context_t a_context;
7303 nfsnode_t np
= VTONFS(ap
->a_vp
);
7304 struct timespec now
;
7310 if ((error
= nfs_node_lock(np
))) {
7315 np
->n_mtim
.tv_sec
= now
.tv_sec
;
7316 np
->n_mtim
.tv_nsec
= now
.tv_nsec
;
7317 nfs_node_unlock(np
);
7318 return VOCALL(fifo_vnodeop_p
, VOFFSET(vnop_write
), ap
);
7322 * Close wrapper for fifos.
7324 * Update the times on the nfsnode then do fifo close.
7328 struct vnop_close_args
/* {
7329 * struct vnodeop_desc *a_desc;
7332 * vfs_context_t a_context;
7335 vnode_t vp
= ap
->a_vp
;
7336 nfsnode_t np
= VTONFS(vp
);
7337 struct vnode_attr vattr
;
7338 struct timespec now
;
7342 if ((error
= nfs_node_lock(np
))) {
7345 if (np
->n_flag
& (NACC
| NUPD
)) {
7347 if (np
->n_flag
& NACC
) {
7348 np
->n_atim
.tv_sec
= now
.tv_sec
;
7349 np
->n_atim
.tv_nsec
= now
.tv_nsec
;
7351 if (np
->n_flag
& NUPD
) {
7352 np
->n_mtim
.tv_sec
= now
.tv_sec
;
7353 np
->n_mtim
.tv_nsec
= now
.tv_nsec
;
7356 if (!vnode_isinuse(vp
, 1) && (mp
= vnode_mount(vp
)) && !vfs_isrdonly(mp
)) {
7358 if (np
->n_flag
& NACC
) {
7359 vattr
.va_access_time
= np
->n_atim
;
7360 VATTR_SET_ACTIVE(&vattr
, va_access_time
);
7362 if (np
->n_flag
& NUPD
) {
7363 vattr
.va_modify_time
= np
->n_mtim
;
7364 VATTR_SET_ACTIVE(&vattr
, va_modify_time
);
7366 nfs_node_unlock(np
);
7367 vnode_setattr(vp
, &vattr
, ap
->a_context
);
7369 nfs_node_unlock(np
);
7372 nfs_node_unlock(np
);
7374 return VOCALL(fifo_vnodeop_p
, VOFFSET(vnop_close
), ap
);
7381 struct vnop_ioctl_args
/* {
7382 * struct vnodeop_desc *a_desc;
7384 * u_int32_t a_command;
7387 * vfs_context_t a_context;
7390 vfs_context_t ctx
= ap
->a_context
;
7391 vnode_t vp
= ap
->a_vp
;
7392 struct nfsmount
*mp
= VTONMP(vp
);
7393 struct user_nfs_gss_principal gprinc
= {};
7401 switch (ap
->a_command
) {
7403 if (vnode_vfsisrdonly(vp
)) {
7406 error
= nfs_flush(VTONFS(vp
), MNT_WAIT
, vfs_context_thread(ctx
), 0);
7408 case NFS_IOC_DESTROY_CRED
:
7409 if (!auth_is_kerberized(mp
->nm_auth
)) {
7412 error
= nfs_gss_clnt_ctx_remove(mp
, vfs_context_ucred(ctx
));
7414 case NFS_IOC_SET_CRED
:
7415 case NFS_IOC_SET_CRED64
:
7416 if (!auth_is_kerberized(mp
->nm_auth
)) {
7419 if ((ap
->a_command
== NFS_IOC_SET_CRED
&& vfs_context_is64bit(ctx
)) ||
7420 (ap
->a_command
== NFS_IOC_SET_CRED64
&& !vfs_context_is64bit(ctx
))) {
7423 if (vfs_context_is64bit(ctx
)) {
7424 gprinc
= *(struct user_nfs_gss_principal
*)ap
->a_data
;
7426 struct nfs_gss_principal
*tp
;
7427 tp
= (struct nfs_gss_principal
*)ap
->a_data
;
7428 gprinc
.princlen
= tp
->princlen
;
7429 gprinc
.nametype
= tp
->nametype
;
7430 gprinc
.principal
= CAST_USER_ADDR_T(tp
->principal
);
7432 NFS_DBG(NFS_FAC_GSS
, 7, "Enter NFS_FSCTL_SET_CRED (64-bit=%d): principal length %d name type %d usr pointer 0x%llx\n", vfs_context_is64bit(ctx
), gprinc
.princlen
, gprinc
.nametype
, (unsigned long long)gprinc
.principal
);
7433 if (gprinc
.princlen
> MAXPATHLEN
) {
7437 MALLOC(p
, uint8_t *, gprinc
.princlen
+ 1, M_TEMP
, M_WAITOK
| M_ZERO
);
7441 error
= copyin(gprinc
.principal
, p
, gprinc
.princlen
);
7443 NFS_DBG(NFS_FAC_GSS
, 7, "NFS_FSCTL_SET_CRED could not copy in princiapl data of len %d: %d\n",
7444 gprinc
.princlen
, error
);
7448 NFS_DBG(NFS_FAC_GSS
, 7, "Seting credential to principal %s\n", p
);
7449 error
= nfs_gss_clnt_ctx_set_principal(mp
, ctx
, p
, gprinc
.princlen
, gprinc
.nametype
);
7450 NFS_DBG(NFS_FAC_GSS
, 7, "Seting credential to principal %s returned %d\n", p
, error
);
7453 case NFS_IOC_GET_CRED
:
7454 case NFS_IOC_GET_CRED64
:
7455 if (!auth_is_kerberized(mp
->nm_auth
)) {
7458 if ((ap
->a_command
== NFS_IOC_GET_CRED
&& vfs_context_is64bit(ctx
)) ||
7459 (ap
->a_command
== NFS_IOC_GET_CRED64
&& !vfs_context_is64bit(ctx
))) {
7462 error
= nfs_gss_clnt_ctx_get_principal(mp
, ctx
, &gprinc
);
7466 if (vfs_context_is64bit(ctx
)) {
7467 struct user_nfs_gss_principal
*upp
= (struct user_nfs_gss_principal
*)ap
->a_data
;
7468 len
= upp
->princlen
;
7469 if (gprinc
.princlen
< len
) {
7470 len
= gprinc
.princlen
;
7472 upp
->princlen
= gprinc
.princlen
;
7473 upp
->nametype
= gprinc
.nametype
;
7474 upp
->flags
= gprinc
.flags
;
7475 if (gprinc
.principal
) {
7476 error
= copyout((void *)gprinc
.principal
, upp
->principal
, len
);
7478 upp
->principal
= USER_ADDR_NULL
;
7481 struct nfs_gss_principal
*u32pp
= (struct nfs_gss_principal
*)ap
->a_data
;
7482 len
= u32pp
->princlen
;
7483 if (gprinc
.princlen
< len
) {
7484 len
= gprinc
.princlen
;
7486 u32pp
->princlen
= gprinc
.princlen
;
7487 u32pp
->nametype
= gprinc
.nametype
;
7488 u32pp
->flags
= gprinc
.flags
;
7489 if (gprinc
.principal
) {
7490 error
= copyout((void *)gprinc
.principal
, u32pp
->principal
, len
);
7492 u32pp
->principal
= (user32_addr_t
)0;
7496 NFS_DBG(NFS_FAC_GSS
, 7, "NFS_FSCTL_GET_CRED could not copy out princiapl data of len %d: %d\n",
7497 gprinc
.princlen
, error
);
7499 if (gprinc
.principal
) {
7500 FREE(gprinc
.principal
, M_TEMP
);
7510 __unused
struct vnop_select_args
/* {
7511 * struct vnodeop_desc *a_desc;
7516 * vfs_context_t a_context;
7520 * We were once bogusly seltrue() which returns 1. Is this right?
7526 * vnode OP for pagein using UPL
7528 * No buffer I/O, just RPCs straight into the mapped pages.
7532 struct vnop_pagein_args
/* {
7533 * struct vnodeop_desc *a_desc;
7536 * vm_offset_t a_pl_offset;
7540 * vfs_context_t a_context;
7543 vnode_t vp
= ap
->a_vp
;
7544 upl_t pl
= ap
->a_pl
;
7545 size_t size
= ap
->a_size
;
7546 off_t f_offset
= ap
->a_f_offset
;
7547 vm_offset_t pl_offset
= ap
->a_pl_offset
;
7548 int flags
= ap
->a_flags
;
7551 nfsnode_t np
= VTONFS(vp
);
7552 size_t nmrsize
, iosize
, txsize
, rxsize
, retsize
;
7554 struct nfsmount
*nmp
;
7556 vm_offset_t ioaddr
, rxaddr
;
7558 char uio_buf
[UIO_SIZEOF(1)];
7559 int nofreeupl
= flags
& UPL_NOCOMMIT
;
7560 upl_page_info_t
*plinfo
;
7561 #define MAXPAGINGREQS 16 /* max outstanding RPCs for pagein/pageout */
7562 struct nfsreq
*req
[MAXPAGINGREQS
];
7563 int nextsend
, nextwait
;
7564 uint32_t stategenid
= 0, restart
= 0;
7567 FSDBG(322, np
, f_offset
, size
, flags
);
7568 if (pl
== (upl_t
)NULL
) {
7569 panic("nfs_pagein: no upl");
7573 printf("nfs_pagein: invalid size %ld", size
);
7575 (void) ubc_upl_abort_range(pl
, pl_offset
, size
, 0);
7579 if (f_offset
< 0 || f_offset
>= (off_t
)np
->n_size
|| (f_offset
& PAGE_MASK_64
)) {
7581 ubc_upl_abort_range(pl
, pl_offset
, size
,
7582 UPL_ABORT_ERROR
| UPL_ABORT_FREE_ON_EMPTY
);
7587 thd
= vfs_context_thread(ap
->a_context
);
7588 cred
= ubc_getcred(vp
);
7589 if (!IS_VALID_CRED(cred
)) {
7590 cred
= vfs_context_ucred(ap
->a_context
);
7593 uio
= uio_createwithbuffer(1, f_offset
, UIO_SYSSPACE
, UIO_READ
,
7594 &uio_buf
, sizeof(uio_buf
));
7597 if (nfs_mount_gone(nmp
)) {
7599 ubc_upl_abort_range(pl
, pl_offset
, size
,
7600 UPL_ABORT_ERROR
| UPL_ABORT_FREE_ON_EMPTY
);
7604 nmrsize
= nmp
->nm_rsize
;
7606 plinfo
= ubc_upl_pageinfo(pl
);
7607 kret
= ubc_upl_map(pl
, &ioaddr
);
7608 if (kret
!= KERN_SUCCESS
) {
7609 panic("nfs_vnop_pagein: ubc_upl_map() failed with (%d)", kret
);
7611 ioaddr
+= pl_offset
;
7614 if (nmp
->nm_vers
>= NFS_VER4
) {
7615 stategenid
= nmp
->nm_stategenid
;
7617 txsize
= rxsize
= size
;
7618 txoffset
= f_offset
;
7621 bzero(req
, sizeof(req
));
7622 nextsend
= nextwait
= 0;
7624 if (np
->n_flag
& NREVOKE
) {
7628 /* send requests while we need to and have available slots */
7629 while ((txsize
> 0) && (req
[nextsend
] == NULL
)) {
7630 iosize
= MIN(nmrsize
, txsize
);
7631 if ((error
= nmp
->nm_funcs
->nf_read_rpc_async(np
, txoffset
, iosize
, thd
, cred
, NULL
, &req
[nextsend
]))) {
7632 req
[nextsend
] = NULL
;
7637 nextsend
= (nextsend
+ 1) % MAXPAGINGREQS
;
7639 /* wait while we need to and break out if more requests to send */
7640 while ((rxsize
> 0) && req
[nextwait
]) {
7641 iosize
= retsize
= MIN(nmrsize
, rxsize
);
7642 uio_reset(uio
, uio_offset(uio
), UIO_SYSSPACE
, UIO_READ
);
7643 uio_addiov(uio
, CAST_USER_ADDR_T(rxaddr
), iosize
);
7644 FSDBG(322, uio_offset(uio
), uio_resid(uio
), rxaddr
, rxsize
);
7646 upl_ubc_alias_set(pl
, (uintptr_t) current_thread(), (uintptr_t) 2);
7647 #endif /* UPL_DEBUG */
7648 OSAddAtomic64(1, &nfsstats
.pageins
);
7649 error
= nmp
->nm_funcs
->nf_read_rpc_async_finish(np
, req
[nextwait
], uio
, &retsize
, NULL
);
7650 req
[nextwait
] = NULL
;
7651 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
7652 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
)) {
7653 lck_mtx_lock(&nmp
->nm_lock
);
7654 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
7655 NP(np
, "nfs_vnop_pagein: error %d, initiating recovery", error
);
7656 nfs_need_recover(nmp
, error
);
7658 lck_mtx_unlock(&nmp
->nm_lock
);
7663 FSDBG(322, uio_offset(uio
), uio_resid(uio
), error
, -1);
7666 if (retsize
< iosize
) {
7667 /* Just zero fill the rest of the valid area. */
7668 int zcnt
= iosize
- retsize
;
7669 bzero((char *)rxaddr
+ retsize
, zcnt
);
7670 FSDBG(324, uio_offset(uio
), retsize
, zcnt
, rxaddr
);
7671 uio_update(uio
, zcnt
);
7679 } while (!error
&& (txsize
|| rxsize
));
7685 /* cancel any outstanding requests */
7686 while (req
[nextwait
]) {
7687 nfs_request_async_cancel(req
[nextwait
]);
7688 req
[nextwait
] = NULL
;
7689 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
7691 if (np
->n_flag
& NREVOKE
) {
7693 } else if (restart
) {
7694 if (restart
<= nfs_mount_state_max_restarts(nmp
)) { /* guard against no progress */
7695 if (error
== NFSERR_GRACE
) {
7696 tsleep(&nmp
->nm_state
, (PZERO
- 1), "nfsgrace", 2 * hz
);
7698 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
))) {
7702 NP(np
, "nfs_pagein: too many restarts, aborting");
7711 ubc_upl_abort_range(pl
, pl_offset
, size
,
7713 UPL_ABORT_FREE_ON_EMPTY
);
7715 ubc_upl_commit_range(pl
, pl_offset
, size
,
7716 UPL_COMMIT_CLEAR_DIRTY
|
7717 UPL_COMMIT_FREE_ON_EMPTY
);
7725 * the following are needed only by nfs_pageout to know how to handle errors
7726 * see nfs_pageout comments on explanation of actions.
7727 * the errors here are copied from errno.h and errors returned by servers
7728 * are expected to match the same numbers here. If not, our actions maybe
7731 char nfs_pageouterrorhandler(int);
7732 enum actiontype
{NOACTION
, DUMP
, DUMPANDLOG
, RETRY
, SEVER
};
7733 #define NFS_ELAST 88
7734 static u_char errorcount
[NFS_ELAST
+ 1]; /* better be zeros when initialized */
7735 static const char errortooutcome
[NFS_ELAST
+ 1] = {
7737 DUMP
, /* EPERM 1 Operation not permitted */
7738 DUMP
, /* ENOENT 2 No such file or directory */
7739 DUMPANDLOG
, /* ESRCH 3 No such process */
7740 RETRY
, /* EINTR 4 Interrupted system call */
7741 DUMP
, /* EIO 5 Input/output error */
7742 DUMP
, /* ENXIO 6 Device not configured */
7743 DUMPANDLOG
, /* E2BIG 7 Argument list too long */
7744 DUMPANDLOG
, /* ENOEXEC 8 Exec format error */
7745 DUMPANDLOG
, /* EBADF 9 Bad file descriptor */
7746 DUMPANDLOG
, /* ECHILD 10 No child processes */
7747 DUMPANDLOG
, /* EDEADLK 11 Resource deadlock avoided - was EAGAIN */
7748 RETRY
, /* ENOMEM 12 Cannot allocate memory */
7749 DUMP
, /* EACCES 13 Permission denied */
7750 DUMPANDLOG
, /* EFAULT 14 Bad address */
7751 DUMPANDLOG
, /* ENOTBLK 15 POSIX - Block device required */
7752 RETRY
, /* EBUSY 16 Device busy */
7753 DUMP
, /* EEXIST 17 File exists */
7754 DUMP
, /* EXDEV 18 Cross-device link */
7755 DUMP
, /* ENODEV 19 Operation not supported by device */
7756 DUMP
, /* ENOTDIR 20 Not a directory */
7757 DUMP
, /* EISDIR 21 Is a directory */
7758 DUMP
, /* EINVAL 22 Invalid argument */
7759 DUMPANDLOG
, /* ENFILE 23 Too many open files in system */
7760 DUMPANDLOG
, /* EMFILE 24 Too many open files */
7761 DUMPANDLOG
, /* ENOTTY 25 Inappropriate ioctl for device */
7762 DUMPANDLOG
, /* ETXTBSY 26 Text file busy - POSIX */
7763 DUMP
, /* EFBIG 27 File too large */
7764 DUMP
, /* ENOSPC 28 No space left on device */
7765 DUMPANDLOG
, /* ESPIPE 29 Illegal seek */
7766 DUMP
, /* EROFS 30 Read-only file system */
7767 DUMP
, /* EMLINK 31 Too many links */
7768 RETRY
, /* EPIPE 32 Broken pipe */
7770 DUMPANDLOG
, /* EDOM 33 Numerical argument out of domain */
7771 DUMPANDLOG
, /* ERANGE 34 Result too large */
7772 RETRY
, /* EAGAIN/EWOULDBLOCK 35 Resource temporarily unavailable */
7773 DUMPANDLOG
, /* EINPROGRESS 36 Operation now in progress */
7774 DUMPANDLOG
, /* EALREADY 37 Operation already in progress */
7775 /* ipc/network software -- argument errors */
7776 DUMPANDLOG
, /* ENOTSOC 38 Socket operation on non-socket */
7777 DUMPANDLOG
, /* EDESTADDRREQ 39 Destination address required */
7778 DUMPANDLOG
, /* EMSGSIZE 40 Message too long */
7779 DUMPANDLOG
, /* EPROTOTYPE 41 Protocol wrong type for socket */
7780 DUMPANDLOG
, /* ENOPROTOOPT 42 Protocol not available */
7781 DUMPANDLOG
, /* EPROTONOSUPPORT 43 Protocol not supported */
7782 DUMPANDLOG
, /* ESOCKTNOSUPPORT 44 Socket type not supported */
7783 DUMPANDLOG
, /* ENOTSUP 45 Operation not supported */
7784 DUMPANDLOG
, /* EPFNOSUPPORT 46 Protocol family not supported */
7785 DUMPANDLOG
, /* EAFNOSUPPORT 47 Address family not supported by protocol family */
7786 DUMPANDLOG
, /* EADDRINUSE 48 Address already in use */
7787 DUMPANDLOG
, /* EADDRNOTAVAIL 49 Can't assign requested address */
7788 /* ipc/network software -- operational errors */
7789 RETRY
, /* ENETDOWN 50 Network is down */
7790 RETRY
, /* ENETUNREACH 51 Network is unreachable */
7791 RETRY
, /* ENETRESET 52 Network dropped connection on reset */
7792 RETRY
, /* ECONNABORTED 53 Software caused connection abort */
7793 RETRY
, /* ECONNRESET 54 Connection reset by peer */
7794 RETRY
, /* ENOBUFS 55 No buffer space available */
7795 RETRY
, /* EISCONN 56 Socket is already connected */
7796 RETRY
, /* ENOTCONN 57 Socket is not connected */
7797 RETRY
, /* ESHUTDOWN 58 Can't send after socket shutdown */
7798 RETRY
, /* ETOOMANYREFS 59 Too many references: can't splice */
7799 RETRY
, /* ETIMEDOUT 60 Operation timed out */
7800 RETRY
, /* ECONNREFUSED 61 Connection refused */
7802 DUMPANDLOG
, /* ELOOP 62 Too many levels of symbolic links */
7803 DUMP
, /* ENAMETOOLONG 63 File name too long */
7804 RETRY
, /* EHOSTDOWN 64 Host is down */
7805 RETRY
, /* EHOSTUNREACH 65 No route to host */
7806 DUMP
, /* ENOTEMPTY 66 Directory not empty */
7808 DUMPANDLOG
, /* PROCLIM 67 Too many processes */
7809 DUMPANDLOG
, /* EUSERS 68 Too many users */
7810 DUMPANDLOG
, /* EDQUOT 69 Disc quota exceeded */
7811 /* Network File System */
7812 DUMP
, /* ESTALE 70 Stale NFS file handle */
7813 DUMP
, /* EREMOTE 71 Too many levels of remote in path */
7814 DUMPANDLOG
, /* EBADRPC 72 RPC struct is bad */
7815 DUMPANDLOG
, /* ERPCMISMATCH 73 RPC version wrong */
7816 DUMPANDLOG
, /* EPROGUNAVAIL 74 RPC prog. not avail */
7817 DUMPANDLOG
, /* EPROGMISMATCH 75 Program version wrong */
7818 DUMPANDLOG
, /* EPROCUNAVAIL 76 Bad procedure for program */
7820 DUMPANDLOG
, /* ENOLCK 77 No locks available */
7821 DUMPANDLOG
, /* ENOSYS 78 Function not implemented */
7822 DUMPANDLOG
, /* EFTYPE 79 Inappropriate file type or format */
7823 DUMPANDLOG
, /* EAUTH 80 Authentication error */
7824 DUMPANDLOG
, /* ENEEDAUTH 81 Need authenticator */
7825 /* Intelligent device errors */
7826 DUMPANDLOG
, /* EPWROFF 82 Device power is off */
7827 DUMPANDLOG
, /* EDEVERR 83 Device error, e.g. paper out */
7828 DUMPANDLOG
, /* EOVERFLOW 84 Value too large to be stored in data type */
7829 /* Program loading errors */
7830 DUMPANDLOG
, /* EBADEXEC 85 Bad executable */
7831 DUMPANDLOG
, /* EBADARCH 86 Bad CPU type in executable */
7832 DUMPANDLOG
, /* ESHLIBVERS 87 Shared library version mismatch */
7833 DUMPANDLOG
, /* EBADMACHO 88 Malformed Macho file */
7837 nfs_pageouterrorhandler(int error
)
7839 if (error
> NFS_ELAST
) {
7842 return errortooutcome
[error
];
7848 * vnode OP for pageout using UPL
7850 * No buffer I/O, just RPCs straight from the mapped pages.
7851 * File size changes are not permitted in pageout.
7855 struct vnop_pageout_args
/* {
7856 * struct vnodeop_desc *a_desc;
7859 * vm_offset_t a_pl_offset;
7863 * vfs_context_t a_context;
7866 vnode_t vp
= ap
->a_vp
;
7867 upl_t pl
= ap
->a_pl
;
7868 size_t size
= ap
->a_size
;
7869 off_t f_offset
= ap
->a_f_offset
;
7870 vm_offset_t pl_offset
= ap
->a_pl_offset
;
7871 int flags
= ap
->a_flags
;
7872 nfsnode_t np
= VTONFS(vp
);
7876 struct nfsmount
*nmp
= VTONMP(vp
);
7878 int error
= 0, iomode
;
7879 off_t off
, txoffset
, rxoffset
;
7880 vm_offset_t ioaddr
, txaddr
, rxaddr
;
7882 char uio_buf
[UIO_SIZEOF(1)];
7883 int nofreeupl
= flags
& UPL_NOCOMMIT
;
7884 size_t nmwsize
, biosize
, iosize
, pgsize
, txsize
, rxsize
, xsize
, remsize
;
7885 struct nfsreq
*req
[MAXPAGINGREQS
];
7886 int nextsend
, nextwait
, wverfset
, commit
;
7887 uint64_t wverf
, wverf2
;
7888 uint32_t stategenid
= 0, vrestart
= 0, restart
= 0, vrestarts
= 0, restarts
= 0;
7891 FSDBG(323, f_offset
, size
, pl
, pl_offset
);
7893 if (pl
== (upl_t
)NULL
) {
7894 panic("nfs_pageout: no upl");
7898 printf("nfs_pageout: invalid size %ld", size
);
7900 ubc_upl_abort_range(pl
, pl_offset
, size
, 0);
7907 ubc_upl_abort(pl
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
7911 biosize
= nmp
->nm_biosize
;
7912 nmwsize
= nmp
->nm_wsize
;
7914 nfs_data_lock_noupdate(np
, NFS_DATA_LOCK_SHARED
);
7917 * Check to see whether the buffer is incore.
7918 * If incore and not busy, invalidate it from the cache.
7920 for (iosize
= 0; iosize
< size
; iosize
+= xsize
) {
7921 off
= f_offset
+ iosize
;
7922 /* need make sure we do things on block boundaries */
7923 xsize
= biosize
- (off
% biosize
);
7924 if (off
+ xsize
> f_offset
+ size
) {
7925 xsize
= f_offset
+ size
- off
;
7927 lbn
= (daddr64_t
)(off
/ biosize
);
7928 lck_mtx_lock(nfs_buf_mutex
);
7929 if ((bp
= nfs_buf_incore(np
, lbn
))) {
7930 FSDBG(323, off
, bp
, bp
->nb_lflags
, bp
->nb_flags
);
7931 if (nfs_buf_acquire(bp
, NBAC_NOWAIT
, 0, 0)) {
7932 lck_mtx_unlock(nfs_buf_mutex
);
7933 nfs_data_unlock_noupdate(np
);
7934 /* no panic. just tell vm we are busy */
7936 ubc_upl_abort_range(pl
, pl_offset
, size
, 0);
7940 if (bp
->nb_dirtyend
> 0) {
7942 * if there's a dirty range in the buffer, check
7943 * to see if it extends beyond the pageout region
7945 * if the dirty region lies completely within the
7946 * pageout region, we just invalidate the buffer
7947 * because it's all being written out now anyway.
7949 * if any of the dirty region lies outside the
7950 * pageout region, we'll try to clip the dirty
7951 * region to eliminate the portion that's being
7952 * paged out. If that's not possible, because
7953 * the dirty region extends before and after the
7954 * pageout region, then we'll just return EBUSY.
7956 off_t boff
, start
, end
;
7960 /* clip end to EOF */
7961 if (end
> (off_t
)np
->n_size
) {
7966 if ((bp
->nb_dirtyoff
< start
) &&
7967 (bp
->nb_dirtyend
> end
)) {
7969 * not gonna be able to clip the dirty region
7971 * But before returning the bad news, move the
7972 * buffer to the start of the delwri list and
7973 * give the list a push to try to flush the
7976 FSDBG(323, np
, bp
, 0xd00deebc, EBUSY
);
7977 nfs_buf_remfree(bp
);
7978 TAILQ_INSERT_HEAD(&nfsbufdelwri
, bp
, nb_free
);
7981 nfs_buf_delwri_push(1);
7982 lck_mtx_unlock(nfs_buf_mutex
);
7983 nfs_data_unlock_noupdate(np
);
7985 ubc_upl_abort_range(pl
, pl_offset
, size
, 0);
7989 if ((bp
->nb_dirtyoff
< start
) ||
7990 (bp
->nb_dirtyend
> end
)) {
7991 /* clip dirty region, if necessary */
7992 if (bp
->nb_dirtyoff
< start
) {
7993 bp
->nb_dirtyend
= min(bp
->nb_dirtyend
, start
);
7995 if (bp
->nb_dirtyend
> end
) {
7996 bp
->nb_dirtyoff
= max(bp
->nb_dirtyoff
, end
);
7998 FSDBG(323, bp
, bp
->nb_dirtyoff
, bp
->nb_dirtyend
, 0xd00dee00);
7999 /* we're leaving this block dirty */
8001 lck_mtx_unlock(nfs_buf_mutex
);
8005 nfs_buf_remfree(bp
);
8006 lck_mtx_unlock(nfs_buf_mutex
);
8007 SET(bp
->nb_flags
, NB_INVAL
);
8008 nfs_node_lock_force(np
);
8009 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
8010 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
8011 np
->n_needcommitcnt
--;
8012 CHECK_NEEDCOMMITCNT(np
);
8014 nfs_node_unlock(np
);
8015 nfs_buf_release(bp
, 1);
8017 lck_mtx_unlock(nfs_buf_mutex
);
8021 thd
= vfs_context_thread(ap
->a_context
);
8022 cred
= ubc_getcred(vp
);
8023 if (!IS_VALID_CRED(cred
)) {
8024 cred
= vfs_context_ucred(ap
->a_context
);
8027 nfs_node_lock_force(np
);
8028 if (np
->n_flag
& NWRITEERR
) {
8029 error
= np
->n_error
;
8030 nfs_node_unlock(np
);
8031 nfs_data_unlock_noupdate(np
);
8033 ubc_upl_abort_range(pl
, pl_offset
, size
,
8034 UPL_ABORT_FREE_ON_EMPTY
);
8038 nfs_node_unlock(np
);
8040 if (f_offset
< 0 || f_offset
>= (off_t
)np
->n_size
||
8041 f_offset
& PAGE_MASK_64
|| size
& PAGE_MASK_64
) {
8042 nfs_data_unlock_noupdate(np
);
8044 ubc_upl_abort_range(pl
, pl_offset
, size
,
8045 UPL_ABORT_FREE_ON_EMPTY
);
8050 kret
= ubc_upl_map(pl
, &ioaddr
);
8051 if (kret
!= KERN_SUCCESS
) {
8052 panic("nfs_vnop_pageout: ubc_upl_map() failed with (%d)", kret
);
8054 ioaddr
+= pl_offset
;
8056 if ((u_quad_t
)f_offset
+ size
> np
->n_size
) {
8057 xsize
= np
->n_size
- f_offset
;
8062 pgsize
= round_page_64(xsize
);
8063 if ((size
> pgsize
) && !nofreeupl
) {
8064 ubc_upl_abort_range(pl
, pl_offset
+ pgsize
, size
- pgsize
,
8065 UPL_ABORT_FREE_ON_EMPTY
);
8069 * check for partial page and clear the
8070 * contents past end of the file before
8071 * releasing it in the VM page cache
8073 if ((u_quad_t
)f_offset
< np
->n_size
&& (u_quad_t
)f_offset
+ size
> np
->n_size
) {
8074 size_t io
= np
->n_size
- f_offset
;
8075 bzero((caddr_t
)(ioaddr
+ io
), size
- io
);
8076 FSDBG(321, np
->n_size
, f_offset
, f_offset
+ io
, size
- io
);
8078 nfs_data_unlock_noupdate(np
);
8080 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_WRITE
,
8081 &uio_buf
, sizeof(uio_buf
));
8084 if (nmp
->nm_vers
>= NFS_VER4
) {
8085 stategenid
= nmp
->nm_stategenid
;
8087 wverf
= wverf2
= wverfset
= 0;
8088 txsize
= rxsize
= xsize
;
8089 txoffset
= rxoffset
= f_offset
;
8090 txaddr
= rxaddr
= ioaddr
;
8091 commit
= NFS_WRITE_FILESYNC
;
8093 bzero(req
, sizeof(req
));
8094 nextsend
= nextwait
= 0;
8096 if (np
->n_flag
& NREVOKE
) {
8100 /* send requests while we need to and have available slots */
8101 while ((txsize
> 0) && (req
[nextsend
] == NULL
)) {
8102 iosize
= MIN(nmwsize
, txsize
);
8103 uio_reset(auio
, txoffset
, UIO_SYSSPACE
, UIO_WRITE
);
8104 uio_addiov(auio
, CAST_USER_ADDR_T(txaddr
), iosize
);
8105 FSDBG(323, uio_offset(auio
), iosize
, txaddr
, txsize
);
8106 OSAddAtomic64(1, &nfsstats
.pageouts
);
8107 nfs_node_lock_force(np
);
8109 nfs_node_unlock(np
);
8110 vnode_startwrite(vp
);
8111 iomode
= NFS_WRITE_UNSTABLE
;
8112 if ((error
= nmp
->nm_funcs
->nf_write_rpc_async(np
, auio
, iosize
, thd
, cred
, iomode
, NULL
, &req
[nextsend
]))) {
8113 req
[nextsend
] = NULL
;
8114 vnode_writedone(vp
);
8115 nfs_node_lock_force(np
);
8117 nfs_node_unlock(np
);
8123 nextsend
= (nextsend
+ 1) % MAXPAGINGREQS
;
8125 /* wait while we need to and break out if more requests to send */
8126 while ((rxsize
> 0) && req
[nextwait
]) {
8127 iosize
= remsize
= MIN(nmwsize
, rxsize
);
8128 error
= nmp
->nm_funcs
->nf_write_rpc_async_finish(np
, req
[nextwait
], &iomode
, &iosize
, &wverf2
);
8129 req
[nextwait
] = NULL
;
8130 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
8131 vnode_writedone(vp
);
8132 nfs_node_lock_force(np
);
8134 nfs_node_unlock(np
);
8135 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
)) {
8136 lck_mtx_lock(&nmp
->nm_lock
);
8137 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
8138 NP(np
, "nfs_vnop_pageout: error %d, initiating recovery", error
);
8139 nfs_need_recover(nmp
, error
);
8141 lck_mtx_unlock(&nmp
->nm_lock
);
8146 FSDBG(323, rxoffset
, rxsize
, error
, -1);
8152 } else if (wverf
!= wverf2
) {
8153 /* verifier changed, so we need to restart all the writes */
8157 /* Retain the lowest commitment level returned. */
8158 if (iomode
< commit
) {
8166 /* need to try sending the remainder */
8168 uio_reset(auio
, rxoffset
, UIO_SYSSPACE
, UIO_WRITE
);
8169 uio_addiov(auio
, CAST_USER_ADDR_T(rxaddr
), remsize
);
8170 iomode
= NFS_WRITE_UNSTABLE
;
8171 error
= nfs_write_rpc2(np
, auio
, thd
, cred
, &iomode
, &wverf2
);
8172 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
)) {
8173 NP(np
, "nfs_vnop_pageout: restart: error %d", error
);
8174 lck_mtx_lock(&nmp
->nm_lock
);
8175 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
8176 NP(np
, "nfs_vnop_pageout: error %d, initiating recovery", error
);
8177 nfs_need_recover(nmp
, error
);
8179 lck_mtx_unlock(&nmp
->nm_lock
);
8184 FSDBG(323, rxoffset
, rxsize
, error
, -1);
8187 if (wverf
!= wverf2
) {
8188 /* verifier changed, so we need to restart all the writes */
8192 if (iomode
< commit
) {
8203 } while (!error
&& (txsize
|| rxsize
));
8207 if (!error
&& (commit
!= NFS_WRITE_FILESYNC
)) {
8208 error
= nmp
->nm_funcs
->nf_commit_rpc(np
, f_offset
, xsize
, cred
, wverf
);
8209 if (error
== NFSERR_STALEWRITEVERF
) {
8217 /* cancel any outstanding requests */
8218 while (req
[nextwait
]) {
8219 nfs_request_async_cancel(req
[nextwait
]);
8220 req
[nextwait
] = NULL
;
8221 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
8222 vnode_writedone(vp
);
8223 nfs_node_lock_force(np
);
8225 nfs_node_unlock(np
);
8227 if (np
->n_flag
& NREVOKE
) {
8231 if (++vrestarts
<= 100) { /* guard against no progress */
8234 NP(np
, "nfs_pageout: too many restarts, aborting");
8235 FSDBG(323, f_offset
, xsize
, ERESTART
, -1);
8238 if (restarts
<= nfs_mount_state_max_restarts(nmp
)) { /* guard against no progress */
8239 if (error
== NFSERR_GRACE
) {
8240 tsleep(&nmp
->nm_state
, (PZERO
- 1), "nfsgrace", 2 * hz
);
8242 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
))) {
8246 NP(np
, "nfs_pageout: too many restarts, aborting");
8247 FSDBG(323, f_offset
, xsize
, ERESTART
, -1);
8256 * We've had several different solutions on what to do when the pageout
8257 * gets an error. If we don't handle it, and return an error to the
8258 * caller, vm, it will retry . This can end in endless looping
8259 * between vm and here doing retries of the same page. Doing a dump
8260 * back to vm, will get it out of vm's knowledge and we lose whatever
8261 * data existed. This is risky, but in some cases necessary. For
8262 * example, the initial fix here was to do that for ESTALE. In that case
8263 * the server is telling us that the file is no longer the same. We
8264 * would not want to keep paging out to that. We also saw some 151
8265 * errors from Auspex server and NFSv3 can return errors higher than
8266 * ELAST. Those along with NFS known server errors we will "dump" from
8267 * vm. Errors we don't expect to occur, we dump and log for further
8268 * analysis. Errors that could be transient, networking ones,
8269 * we let vm "retry". Lastly, errors that we retry, but may have potential
8270 * to storm the network, we "retrywithsleep". "sever" will be used in
8271 * in the future to dump all pages of object for cases like ESTALE.
8272 * All this is the basis for the states returned and first guesses on
8273 * error handling. Tweaking expected as more statistics are gathered.
8274 * Note, in the long run we may need another more robust solution to
8275 * have some kind of persistant store when the vm cannot dump nor keep
8276 * retrying as a solution, but this would be a file architectural change
8278 if (!nofreeupl
) { /* otherwise stacked file system has to handle this */
8281 char action
= nfs_pageouterrorhandler(error
);
8285 abortflags
= UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
;
8288 abortflags
= UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
;
8289 if (error
<= NFS_ELAST
) {
8290 if ((errorcount
[error
] % 100) == 0) {
8291 NP(np
, "nfs_pageout: unexpected error %d. dumping vm page", error
);
8293 errorcount
[error
]++;
8297 abortflags
= UPL_ABORT_FREE_ON_EMPTY
;
8299 case SEVER
: /* not implemented */
8301 NP(np
, "nfs_pageout: action %d not expected", action
);
8305 ubc_upl_abort_range(pl
, pl_offset
, pgsize
, abortflags
);
8306 /* return error in all cases above */
8308 ubc_upl_commit_range(pl
, pl_offset
, pgsize
,
8309 UPL_COMMIT_CLEAR_DIRTY
|
8310 UPL_COMMIT_FREE_ON_EMPTY
);
8316 /* Blktooff derives file offset given a logical block number */
8319 struct vnop_blktooff_args
/* {
8320 * struct vnodeop_desc *a_desc;
8322 * daddr64_t a_lblkno;
8327 vnode_t vp
= ap
->a_vp
;
8328 struct nfsmount
*nmp
= VTONMP(vp
);
8330 if (nfs_mount_gone(nmp
)) {
8333 biosize
= nmp
->nm_biosize
;
8335 *ap
->a_offset
= (off_t
)(ap
->a_lblkno
* biosize
);
8342 struct vnop_offtoblk_args
/* {
8343 * struct vnodeop_desc *a_desc;
8346 * daddr64_t *a_lblkno;
8350 vnode_t vp
= ap
->a_vp
;
8351 struct nfsmount
*nmp
= VTONMP(vp
);
8353 if (nfs_mount_gone(nmp
)) {
8356 biosize
= nmp
->nm_biosize
;
8358 *ap
->a_lblkno
= (daddr64_t
)(ap
->a_offset
/ biosize
);
8364 * vnode change monitoring
8368 struct vnop_monitor_args
/* {
8369 * struct vnodeop_desc *a_desc;
8371 * uint32_t a_events;
8374 * vfs_context_t a_context;
8377 nfsnode_t np
= VTONFS(ap
->a_vp
);
8378 struct nfsmount
*nmp
= VTONMP(ap
->a_vp
);
8381 if (nfs_mount_gone(nmp
)) {
8385 /* make sure that the vnode's monitoring status is up to date */
8386 lck_mtx_lock(&nmp
->nm_lock
);
8387 if (vnode_ismonitored(ap
->a_vp
)) {
8388 /* This vnode is currently being monitored, make sure we're tracking it. */
8389 if (np
->n_monlink
.le_next
== NFSNOLIST
) {
8390 LIST_INSERT_HEAD(&nmp
->nm_monlist
, np
, n_monlink
);
8391 nfs_mount_sock_thread_wake(nmp
);
8394 /* This vnode is no longer being monitored, make sure we're not tracking it. */
8395 /* Wait for any in-progress getattr to complete first. */
8396 while (np
->n_mflag
& NMMONSCANINPROG
) {
8397 struct timespec ts
= { 1, 0 };
8398 np
->n_mflag
|= NMMONSCANWANT
;
8399 msleep(&np
->n_mflag
, &nmp
->nm_lock
, PZERO
- 1, "nfswaitmonscan", &ts
);
8401 if (np
->n_monlink
.le_next
!= NFSNOLIST
) {
8402 LIST_REMOVE(np
, n_monlink
);
8403 np
->n_monlink
.le_next
= NFSNOLIST
;
8406 lck_mtx_unlock(&nmp
->nm_lock
);
8412 * Send a vnode notification for the given events.
8415 nfs_vnode_notify(nfsnode_t np
, uint32_t events
)
8417 struct nfsmount
*nmp
= NFSTONMP(np
);
8418 struct nfs_vattr nvattr
;
8419 struct vnode_attr vattr
, *vap
= NULL
;
8423 if ((np
->n_evtstamp
== now
.tv_sec
) || !nmp
) {
8424 /* delay sending this notify */
8425 np
->n_events
|= events
;
8428 events
|= np
->n_events
;
8430 np
->n_evtstamp
= now
.tv_sec
;
8432 vfs_get_notify_attributes(&vattr
);
8433 if (!nfs_getattrcache(np
, &nvattr
, 0)) {
8437 VATTR_RETURN(vap
, va_fsid
, vfs_statfs(nmp
->nm_mountp
)->f_fsid
.val
[0]);
8438 VATTR_RETURN(vap
, va_fileid
, nvattr
.nva_fileid
);
8439 VATTR_RETURN(vap
, va_mode
, nvattr
.nva_mode
);
8440 VATTR_RETURN(vap
, va_uid
, nvattr
.nva_uid
);
8441 VATTR_RETURN(vap
, va_gid
, nvattr
.nva_gid
);
8442 VATTR_RETURN(vap
, va_nlink
, nvattr
.nva_nlink
);
8444 vnode_notify(NFSTOV(np
), events
, vap
);