2 * Copyright (c) 2000-2015 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
)
466 if (slot
== NFS_ACCESS_CACHE_SIZE
) {
469 slot
= np
->n_access
[NFS_ACCESS_CACHE_SIZE
];
470 np
->n_access
[NFS_ACCESS_CACHE_SIZE
] = (slot
+ 1) % NFS_ACCESS_CACHE_SIZE
;
476 nfs3_access_rpc(nfsnode_t np
, u_int32_t
*access
, int rpcflags
, vfs_context_t ctx
)
478 int error
= 0, lockerror
= ENOENT
, status
, slot
;
479 uint32_t access_result
= 0;
481 struct nfsm_chain nmreq
, nmrep
;
485 nfsm_chain_null(&nmreq
);
486 nfsm_chain_null(&nmrep
);
488 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(NFS_VER3
) + NFSX_UNSIGNED
);
489 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER3
, np
->n_fhp
, np
->n_fhsize
);
490 nfsm_chain_add_32(error
, &nmreq
, *access
);
491 nfsm_chain_build_done(error
, &nmreq
);
493 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC_ACCESS
,
494 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
495 NULL
, rpcflags
, &nmrep
, &xid
, &status
);
496 if ((lockerror
= nfs_node_lock(np
)))
498 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
501 nfsm_chain_get_32(error
, &nmrep
, access_result
);
504 uid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
505 slot
= nfs_node_access_slot(np
, uid
, 1);
506 np
->n_accessuid
[slot
] = uid
;
508 np
->n_accessstamp
[slot
] = now
.tv_sec
;
509 np
->n_access
[slot
] = access_result
;
512 * If we asked for DELETE but didn't get it, the server
513 * may simply not support returning that bit (possible
514 * on UNIX systems). So, we'll assume that it is OK,
515 * and just let any subsequent delete action fail if it
516 * really isn't deletable.
518 if ((*access
& NFS_ACCESS_DELETE
) &&
519 !(np
->n_access
[slot
] & NFS_ACCESS_DELETE
))
520 np
->n_access
[slot
] |= NFS_ACCESS_DELETE
;
521 /* ".zfs" subdirectories may erroneously give a denied answer for add/remove */
522 if (nfs_access_dotzfs
&& (np
->n_flag
& NISDOTZFSCHILD
))
523 np
->n_access
[slot
] |= (NFS_ACCESS_MODIFY
|NFS_ACCESS_EXTEND
|NFS_ACCESS_DELETE
);
524 /* pass back the access returned with this request */
525 *access
= np
->n_access
[slot
];
529 nfsm_chain_cleanup(&nmreq
);
530 nfsm_chain_cleanup(&nmrep
);
535 * NFS access vnode op.
536 * For NFS version 2, just return ok. File accesses may fail later.
537 * For NFS version 3+, use the access RPC to check accessibility. If file
538 * permissions are changed on the server, accesses might still fail later.
542 struct vnop_access_args
/* {
543 struct vnodeop_desc *a_desc;
546 vfs_context_t a_context;
549 vfs_context_t ctx
= ap
->a_context
;
550 vnode_t vp
= ap
->a_vp
;
551 int error
= 0, slot
, dorpc
, rpcflags
= 0;
552 u_int32_t access
, waccess
;
553 nfsnode_t np
= VTONFS(vp
);
554 struct nfsmount
*nmp
;
560 if (nfs_mount_gone(nmp
))
562 nfsvers
= nmp
->nm_vers
;
564 if (nfsvers
== NFS_VER2
) {
565 if ((ap
->a_action
& KAUTH_VNODE_WRITE_RIGHTS
) &&
566 vfs_isrdonly(vnode_mount(vp
)))
572 * For NFS v3, do an access rpc, otherwise you are stuck emulating
573 * ufs_access() locally using the vattr. This may not be correct,
574 * since the server may apply other access criteria such as
575 * client uid-->server uid mapping that we do not know about, but
576 * this is better than just returning anything that is lying about
581 * Convert KAUTH primitives to NFS access rights.
584 if (vnode_isdir(vp
)) {
587 (KAUTH_VNODE_LIST_DIRECTORY
|
588 KAUTH_VNODE_READ_EXTATTRIBUTES
))
589 access
|= NFS_ACCESS_READ
;
590 if (ap
->a_action
& KAUTH_VNODE_SEARCH
)
591 access
|= NFS_ACCESS_LOOKUP
;
593 (KAUTH_VNODE_ADD_FILE
|
594 KAUTH_VNODE_ADD_SUBDIRECTORY
))
595 access
|= NFS_ACCESS_MODIFY
| NFS_ACCESS_EXTEND
;
596 if (ap
->a_action
& KAUTH_VNODE_DELETE_CHILD
)
597 access
|= NFS_ACCESS_MODIFY
;
601 (KAUTH_VNODE_READ_DATA
|
602 KAUTH_VNODE_READ_EXTATTRIBUTES
))
603 access
|= NFS_ACCESS_READ
;
604 if (ap
->a_action
& KAUTH_VNODE_WRITE_DATA
)
605 access
|= NFS_ACCESS_MODIFY
| NFS_ACCESS_EXTEND
;
606 if (ap
->a_action
& KAUTH_VNODE_APPEND_DATA
)
607 access
|= NFS_ACCESS_EXTEND
;
608 if (ap
->a_action
& KAUTH_VNODE_EXECUTE
)
609 access
|= NFS_ACCESS_EXECUTE
;
612 if (ap
->a_action
& KAUTH_VNODE_DELETE
)
613 access
|= NFS_ACCESS_DELETE
;
615 (KAUTH_VNODE_WRITE_ATTRIBUTES
|
616 KAUTH_VNODE_WRITE_EXTATTRIBUTES
|
617 KAUTH_VNODE_WRITE_SECURITY
))
618 access
|= NFS_ACCESS_MODIFY
;
619 /* XXX this is pretty dubious */
620 if (ap
->a_action
& KAUTH_VNODE_CHANGE_OWNER
)
621 access
|= NFS_ACCESS_MODIFY
;
623 /* if caching, always ask for every right */
624 if (nfs_access_cache_timeout
> 0) {
625 waccess
= NFS_ACCESS_READ
| NFS_ACCESS_MODIFY
|
626 NFS_ACCESS_EXTEND
| NFS_ACCESS_EXECUTE
|
627 NFS_ACCESS_DELETE
| NFS_ACCESS_LOOKUP
;
632 if ((error
= nfs_node_lock(np
)))
636 * Does our cached result allow us to give a definite yes to
639 if (auth_is_kerberized(np
->n_auth
) || auth_is_kerberized(nmp
->nm_auth
))
640 uid
= nfs_cred_getasid2uid(vfs_context_ucred(ctx
));
642 uid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
643 slot
= nfs_node_access_slot(np
, uid
, 0);
646 /* not asking for any rights understood by NFS, so don't bother doing an RPC */
647 /* OSAddAtomic(1, &nfsstats.accesscache_hits); */
650 } else if (NACCESSVALID(np
, slot
)) {
652 if (((now
.tv_sec
< (np
->n_accessstamp
[slot
] + nfs_access_cache_timeout
)) &&
653 ((np
->n_access
[slot
] & access
) == access
)) || nfs_use_cache(nmp
)) {
654 /* OSAddAtomic(1, &nfsstats.accesscache_hits); */
656 waccess
= np
->n_access
[slot
];
661 /* Either a no, or a don't know. Go to the wire. */
662 /* OSAddAtomic(1, &nfsstats.accesscache_misses); */
665 * Allow an access call to timeout if we have it cached
666 * so we won't hang if the server isn't responding.
668 if (NACCESSVALID(np
, slot
))
671 error
= nmp
->nm_funcs
->nf_access_rpc(np
, &waccess
, rpcflags
, ctx
);
674 * If the server didn't respond return the cached access.
676 if ((error
== ETIMEDOUT
) && (rpcflags
& R_SOFT
)) {
678 waccess
= np
->n_access
[slot
];
681 if (!error
&& ((waccess
& access
) != access
))
691 * Perform various update/invalidation checks and then add the
692 * open to the node. Regular files will have an open file structure
693 * on the node and, for NFSv4, perform an OPEN request on the server.
697 struct vnop_open_args
/* {
698 struct vnodeop_desc *a_desc;
701 vfs_context_t a_context;
704 vfs_context_t ctx
= ap
->a_context
;
705 vnode_t vp
= ap
->a_vp
;
706 nfsnode_t np
= VTONFS(vp
);
707 struct nfsmount
*nmp
;
708 int error
, accessMode
, denyMode
, opened
= 0;
709 struct nfs_open_owner
*noop
= NULL
;
710 struct nfs_open_file
*nofp
= NULL
;
713 if (!(ap
->a_mode
& (FREAD
|FWRITE
)))
717 if (nfs_mount_gone(nmp
))
719 if (np
->n_flag
& NREVOKE
)
722 vtype
= vnode_vtype(vp
);
723 if ((vtype
!= VREG
) && (vtype
!= VDIR
) && (vtype
!= VLNK
))
726 /* First, check if we need to update/invalidate */
727 if (ISSET(np
->n_flag
, NUPDATESIZE
))
728 nfs_data_update_size(np
, 0);
729 if ((error
= nfs_node_lock(np
)))
731 if (np
->n_flag
& NNEEDINVALIDATE
) {
732 np
->n_flag
&= ~NNEEDINVALIDATE
;
736 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, ctx
, 1);
737 if ((error
= nfs_node_lock(np
)))
741 np
->n_lastrahead
= -1;
742 if (np
->n_flag
& NMODIFIED
) {
746 if ((error
= nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, ctx
, 1)))
752 /* nfs_getattr() will check changed and purge caches */
753 if ((error
= nfs_getattr(np
, NULL
, ctx
, NGA_UNCACHED
)))
757 /* Just mark that it was opened */
758 lck_mtx_lock(&np
->n_openlock
);
760 lck_mtx_unlock(&np
->n_openlock
);
764 /* mode contains some combination of: FREAD, FWRITE, O_SHLOCK, O_EXLOCK */
766 if (ap
->a_mode
& FREAD
)
767 accessMode
|= NFS_OPEN_SHARE_ACCESS_READ
;
768 if (ap
->a_mode
& FWRITE
)
769 accessMode
|= NFS_OPEN_SHARE_ACCESS_WRITE
;
770 if (ap
->a_mode
& O_EXLOCK
)
771 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
772 else if (ap
->a_mode
& O_SHLOCK
)
773 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
775 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
776 // XXX don't do deny modes just yet (and never do it for !v4)
777 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
779 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
784 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
786 nfs_open_owner_rele(noop
);
789 if (np
->n_flag
& NREVOKE
) {
791 nfs_mount_state_in_use_end(nmp
, 0);
792 nfs_open_owner_rele(noop
);
796 error
= nfs_open_file_find(np
, noop
, &nofp
, accessMode
, denyMode
, 1);
797 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
798 NP(np
, "nfs_vnop_open: LOST %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
801 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
802 nfs_mount_state_in_use_end(nmp
, 0);
803 error
= nfs4_reopen(nofp
, vfs_context_thread(ctx
));
809 error
= nfs_open_file_set_busy(nofp
, vfs_context_thread(ctx
));
815 if (nmp
->nm_vers
< NFS_VER4
) {
817 * NFS v2/v3 opens are always allowed - so just add it.
819 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, 0);
824 * If we just created the file and the modes match, then we simply use
825 * the open performed in the create. Otherwise, send the request.
827 if ((nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) &&
828 (nofp
->nof_creator
== current_thread()) &&
829 (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
) &&
830 (denyMode
== NFS_OPEN_SHARE_DENY_NONE
)) {
831 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
832 nofp
->nof_creator
= NULL
;
835 error
= nfs4_open(np
, nofp
, accessMode
, denyMode
, ctx
);
836 if ((error
== EACCES
) && (nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) &&
837 (nofp
->nof_creator
== current_thread())) {
839 * Ugh. This can happen if we just created the file with read-only
840 * perms and we're trying to open it for real with different modes
841 * (e.g. write-only or with a deny mode) and the server decides to
842 * not allow the second open because of the read-only perms.
843 * The best we can do is to just use the create's open.
844 * We may have access we don't need or we may not have a requested
845 * deny mode. We may log complaints later, but we'll try to avoid it.
847 if (denyMode
!= NFS_OPEN_SHARE_DENY_NONE
)
848 NP(np
, "nfs_vnop_open: deny mode foregone on create, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
849 nofp
->nof_creator
= NULL
;
856 * If we had just created the file, we already had it open.
857 * If the actual open mode is less than what we grabbed at
858 * create time, then we'll downgrade the open here.
860 if ((nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) &&
861 (nofp
->nof_creator
== current_thread())) {
862 error
= nfs_close(np
, nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
864 NP(np
, "nfs_vnop_open: create close error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
865 if (!nfs_mount_state_error_should_restart(error
)) {
867 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
874 nfs_open_file_clear_busy(nofp
);
875 if (nfs_mount_state_in_use_end(nmp
, error
)) {
880 NP(np
, "nfs_vnop_open: error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
882 nfs_open_owner_rele(noop
);
883 if (!error
&& vtype
== VREG
&& (ap
->a_mode
& FWRITE
)) {
884 lck_mtx_lock(&nmp
->nm_lock
);
885 nmp
->nm_state
&= ~NFSSTA_SQUISHY
;
886 nmp
->nm_curdeadtimeout
= nmp
->nm_deadtimeout
;
887 if (nmp
->nm_curdeadtimeout
<= 0)
888 nmp
->nm_deadto_start
= 0;
890 lck_mtx_unlock(&nmp
->nm_lock
);
897 nfs_no_of_open_file_writers(nfsnode_t np
)
899 uint32_t writers
= 0;
900 struct nfs_open_file
*nofp
;
902 TAILQ_FOREACH(nofp
, &np
->n_opens
, nof_link
) {
903 writers
+= nofp
->nof_w
+ nofp
->nof_rw
+ nofp
->nof_w_dw
+ nofp
->nof_rw_dw
+
904 nofp
->nof_w_drw
+ nofp
->nof_rw_drw
+ nofp
->nof_d_w_dw
+
905 nofp
->nof_d_rw_dw
+ nofp
->nof_d_w_drw
+ nofp
->nof_d_rw_drw
+
906 nofp
->nof_d_w
+ nofp
->nof_d_rw
;
915 * What an NFS client should do upon close after writing is a debatable issue.
916 * Most NFS clients push delayed writes to the server upon close, basically for
918 * 1 - So that any write errors may be reported back to the client process
919 * doing the close system call. By far the two most likely errors are
920 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
921 * 2 - To put a worst case upper bound on cache inconsistency between
922 * multiple clients for the file.
923 * There is also a consistency problem for Version 2 of the protocol w.r.t.
924 * not being able to tell if other clients are writing a file concurrently,
925 * since there is no way of knowing if the changed modify time in the reply
926 * is only due to the write for this client.
927 * (NFS Version 3 provides weak cache consistency data in the reply that
928 * should be sufficient to detect and handle this case.)
930 * The current code does the following:
931 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
932 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate them.
933 * for NFS Version 4 - basically the same as NFSv3
937 struct vnop_close_args
/* {
938 struct vnodeop_desc *a_desc;
941 vfs_context_t a_context;
944 vfs_context_t ctx
= ap
->a_context
;
945 vnode_t vp
= ap
->a_vp
;
946 nfsnode_t np
= VTONFS(vp
);
947 struct nfsmount
*nmp
;
948 int error
= 0, error1
, nfsvers
;
949 int fflag
= ap
->a_fflag
;
951 int accessMode
, denyMode
;
952 struct nfs_open_owner
*noop
= NULL
;
953 struct nfs_open_file
*nofp
= NULL
;
958 nfsvers
= nmp
->nm_vers
;
959 vtype
= vnode_vtype(vp
);
961 /* First, check if we need to update/flush/invalidate */
962 if (ISSET(np
->n_flag
, NUPDATESIZE
))
963 nfs_data_update_size(np
, 0);
964 nfs_node_lock_force(np
);
965 if (np
->n_flag
& NNEEDINVALIDATE
) {
966 np
->n_flag
&= ~NNEEDINVALIDATE
;
968 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, ctx
, 1);
969 nfs_node_lock_force(np
);
971 if ((vtype
== VREG
) && (np
->n_flag
& NMODIFIED
) && (fflag
& FWRITE
)) {
972 /* we're closing an open for write and the file is modified, so flush it */
974 if (nfsvers
!= NFS_VER2
)
975 error
= nfs_flush(np
, MNT_WAIT
, vfs_context_thread(ctx
), 0);
977 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
978 nfs_node_lock_force(np
);
981 if (np
->n_flag
& NWRITEERR
) {
982 np
->n_flag
&= ~NWRITEERR
;
988 /* Just mark that it was closed */
989 lck_mtx_lock(&np
->n_openlock
);
990 if (np
->n_openrefcnt
== 0) {
991 if (fflag
& (FREAD
|FWRITE
)) {
992 NP(np
, "nfs_vnop_close: open reference underrun");
995 } else if (fflag
& (FREAD
|FWRITE
)) {
998 /* No FREAD/FWRITE set - probably the final close */
999 np
->n_openrefcnt
= 0;
1001 lck_mtx_unlock(&np
->n_openlock
);
1006 /* fflag should contain some combination of: FREAD, FWRITE, FHASLOCK */
1009 accessMode
|= NFS_OPEN_SHARE_ACCESS_READ
;
1011 accessMode
|= NFS_OPEN_SHARE_ACCESS_WRITE
;
1012 // XXX It would be nice if we still had the O_EXLOCK/O_SHLOCK flags that were on the open
1013 // if (fflag & O_EXLOCK)
1014 // denyMode = NFS_OPEN_SHARE_DENY_BOTH;
1015 // else if (fflag & O_SHLOCK)
1016 // denyMode = NFS_OPEN_SHARE_DENY_WRITE;
1018 // denyMode = NFS_OPEN_SHARE_DENY_NONE;
1020 if (fflag
& FHASLOCK
) {
1021 /* XXX assume FHASLOCK is for the deny mode and not flock */
1022 /* FHASLOCK flock will be unlocked in the close path, but the flag is not cleared. */
1023 if (nofp
->nof_deny
& NFS_OPEN_SHARE_DENY_READ
)
1024 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
1025 else if (nofp
->nof_deny
& NFS_OPEN_SHARE_DENY_WRITE
)
1026 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
1028 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
1030 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
1033 // XXX don't do deny modes just yet (and never do it for !v4)
1034 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
1039 * No mode given to close?
1040 * Guess this is the final close.
1041 * We should unlock all locks and close all opens.
1044 mount_t mp
= vnode_mount(vp
);
1045 int force
= (!mp
|| vfs_isforce(mp
));
1047 writers
= nfs_no_of_open_file_writers(np
);
1048 nfs_release_open_state_for_node(np
, force
);
1050 lck_mtx_lock(&nmp
->nm_lock
);
1051 if (writers
> nmp
->nm_writers
) {
1052 NP(np
, "nfs_vnop_close: number of write opens for mount underrun. Node has %d"
1053 " opens for write. Mount has total of %d opens for write\n",
1054 writers
, nmp
->nm_writers
);
1055 nmp
->nm_writers
= 0;
1057 nmp
->nm_writers
-= writers
;
1059 lck_mtx_unlock(&nmp
->nm_lock
);
1063 } else if (fflag
& FWRITE
) {
1064 lck_mtx_lock(&nmp
->nm_lock
);
1065 if (nmp
->nm_writers
== 0) {
1066 NP(np
, "nfs_vnop_close: removing open writer from mount, but mount has no files open for writing");
1070 lck_mtx_unlock(&nmp
->nm_lock
);
1074 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 0);
1076 // printf("nfs_vnop_close: can't get open owner!\n");
1081 error
= nfs_mount_state_in_use_start(nmp
, NULL
);
1083 nfs_open_owner_rele(noop
);
1087 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 0);
1088 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
1089 nfs_mount_state_in_use_end(nmp
, 0);
1090 error
= nfs4_reopen(nofp
, NULL
);
1096 NP(np
, "nfs_vnop_close: no open file for owner, error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
1100 error
= nfs_open_file_set_busy(nofp
, NULL
);
1106 error
= nfs_close(np
, nofp
, accessMode
, denyMode
, ctx
);
1108 NP(np
, "nfs_vnop_close: close error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
1112 nfs_open_file_clear_busy(nofp
);
1113 if (nfs_mount_state_in_use_end(nmp
, error
)) {
1120 NP(np
, "nfs_vnop_close: error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
1122 nfs_open_owner_rele(noop
);
1127 * nfs_close(): common function that does all the heavy lifting of file closure
1129 * Takes an open file structure and a set of access/deny modes and figures out how
1130 * to update the open file structure (and the state on the server) appropriately.
1135 struct nfs_open_file
*nofp
,
1136 uint32_t accessMode
,
1140 struct nfs_lock_owner
*nlop
;
1141 int error
= 0, changed
= 0, delegated
= 0, closed
= 0, downgrade
= 0;
1142 uint32_t newAccessMode
, newDenyMode
;
1144 /* warn if modes don't match current state */
1145 if (((accessMode
& nofp
->nof_access
) != accessMode
) || ((denyMode
& nofp
->nof_deny
) != denyMode
))
1146 NP(np
, "nfs_close: mode mismatch %d %d, current %d %d, %d",
1147 accessMode
, denyMode
, nofp
->nof_access
, nofp
->nof_deny
,
1148 kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1151 * If we're closing a write-only open, we may not have a write-only count
1152 * if we also grabbed read access. So, check the read-write count.
1154 if (denyMode
== NFS_OPEN_SHARE_DENY_NONE
) {
1155 if ((accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) &&
1156 (nofp
->nof_w
== 0) && (nofp
->nof_d_w
== 0) &&
1157 (nofp
->nof_rw
|| nofp
->nof_d_rw
))
1158 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
1159 } else if (denyMode
== NFS_OPEN_SHARE_DENY_WRITE
) {
1160 if ((accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) &&
1161 (nofp
->nof_w_dw
== 0) && (nofp
->nof_d_w_dw
== 0) &&
1162 (nofp
->nof_rw_dw
|| nofp
->nof_d_rw_dw
))
1163 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
1164 } else { /* NFS_OPEN_SHARE_DENY_BOTH */
1165 if ((accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) &&
1166 (nofp
->nof_w_drw
== 0) && (nofp
->nof_d_w_drw
== 0) &&
1167 (nofp
->nof_rw_drw
|| nofp
->nof_d_rw_drw
))
1168 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
1171 nfs_open_file_remove_open_find(nofp
, accessMode
, denyMode
, &newAccessMode
, &newDenyMode
, &delegated
);
1172 if ((newAccessMode
!= nofp
->nof_access
) || (newDenyMode
!= nofp
->nof_deny
))
1177 if (NFSTONMP(np
)->nm_vers
< NFS_VER4
) /* NFS v2/v3 closes simply need to remove the open. */
1180 if ((newAccessMode
== 0) || (nofp
->nof_opencnt
== 1)) {
1182 * No more access after this close, so clean up and close it.
1183 * Don't send a close RPC if we're closing a delegated open.
1187 if (!delegated
&& !(nofp
->nof_flags
& NFS_OPEN_FILE_LOST
))
1188 error
= nfs4_close_rpc(np
, nofp
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
), 0);
1189 if (error
== NFSERR_LOCKS_HELD
) {
1191 * Hmm... the server says we have locks we need to release first
1192 * Find the lock owner and try to unlock everything.
1194 nlop
= nfs_lock_owner_find(np
, vfs_context_proc(ctx
), 0);
1196 nfs4_unlock_rpc(np
, nlop
, F_WRLCK
, 0, UINT64_MAX
,
1197 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
1198 nfs_lock_owner_rele(nlop
);
1200 error
= nfs4_close_rpc(np
, nofp
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
), 0);
1202 } else if (changed
) {
1204 * File is still open but with less access, so downgrade the open.
1205 * Don't send a downgrade RPC if we're closing a delegated open.
1207 if (!delegated
&& !(nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
1210 * If we have delegated opens, we should probably claim them before sending
1211 * the downgrade because the server may not know the open we are downgrading to.
1213 if (nofp
->nof_d_rw_drw
|| nofp
->nof_d_w_drw
|| nofp
->nof_d_r_drw
||
1214 nofp
->nof_d_rw_dw
|| nofp
->nof_d_w_dw
|| nofp
->nof_d_r_dw
||
1215 nofp
->nof_d_rw
|| nofp
->nof_d_w
|| nofp
->nof_d_r
)
1216 nfs4_claim_delegated_state_for_open_file(nofp
, 0);
1217 /* need to remove the open before sending the downgrade */
1218 nfs_open_file_remove_open(nofp
, accessMode
, denyMode
);
1219 error
= nfs4_open_downgrade_rpc(np
, nofp
, ctx
);
1220 if (error
) /* Hmm.. that didn't work. Add the open back in. */
1221 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, delegated
);
1226 NP(np
, "nfs_close: error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1232 nfs_open_file_remove_open(nofp
, accessMode
, denyMode
);
1235 lck_mtx_lock(&nofp
->nof_lock
);
1236 if (nofp
->nof_r
|| nofp
->nof_d_r
|| nofp
->nof_w
|| nofp
->nof_d_w
|| nofp
->nof_d_rw
||
1237 (nofp
->nof_rw
&& !((nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) && !nofp
->nof_creator
&& (nofp
->nof_rw
== 1))) ||
1238 nofp
->nof_r_dw
|| nofp
->nof_d_r_dw
|| nofp
->nof_w_dw
|| nofp
->nof_d_w_dw
||
1239 nofp
->nof_rw_dw
|| nofp
->nof_d_rw_dw
|| nofp
->nof_r_drw
|| nofp
->nof_d_r_drw
||
1240 nofp
->nof_w_drw
|| nofp
->nof_d_w_drw
|| nofp
->nof_rw_drw
|| nofp
->nof_d_rw_drw
)
1241 NP(np
, "nfs_close: unexpected count: %u.%u %u.%u %u.%u dw %u.%u %u.%u %u.%u drw %u.%u %u.%u %u.%u flags 0x%x, %d",
1242 nofp
->nof_r
, nofp
->nof_d_r
, nofp
->nof_w
, nofp
->nof_d_w
,
1243 nofp
->nof_rw
, nofp
->nof_d_rw
, nofp
->nof_r_dw
, nofp
->nof_d_r_dw
,
1244 nofp
->nof_w_dw
, nofp
->nof_d_w_dw
, nofp
->nof_rw_dw
, nofp
->nof_d_rw_dw
,
1245 nofp
->nof_r_drw
, nofp
->nof_d_r_drw
, nofp
->nof_w_drw
, nofp
->nof_d_w_drw
,
1246 nofp
->nof_rw_drw
, nofp
->nof_d_rw_drw
, nofp
->nof_flags
,
1247 kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1248 /* clear out all open info, just to be safe */
1249 nofp
->nof_access
= nofp
->nof_deny
= 0;
1250 nofp
->nof_mmap_access
= nofp
->nof_mmap_deny
= 0;
1251 nofp
->nof_r
= nofp
->nof_d_r
= 0;
1252 nofp
->nof_w
= nofp
->nof_d_w
= 0;
1253 nofp
->nof_rw
= nofp
->nof_d_rw
= 0;
1254 nofp
->nof_r_dw
= nofp
->nof_d_r_dw
= 0;
1255 nofp
->nof_w_dw
= nofp
->nof_d_w_dw
= 0;
1256 nofp
->nof_rw_dw
= nofp
->nof_d_rw_dw
= 0;
1257 nofp
->nof_r_drw
= nofp
->nof_d_r_drw
= 0;
1258 nofp
->nof_w_drw
= nofp
->nof_d_w_drw
= 0;
1259 nofp
->nof_rw_drw
= nofp
->nof_d_rw_drw
= 0;
1260 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
1261 lck_mtx_unlock(&nofp
->nof_lock
);
1262 /* XXX we may potentially want to clean up idle/unused open file structures */
1264 if (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
) {
1266 NP(np
, "nfs_close: LOST%s, %d", !nofp
->nof_opencnt
? " (last)" : "",
1267 kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1282 struct nfs_vattr
*nvap
,
1285 struct nfsmount
*nmp
= mp
? VFSTONFS(mp
) : NFSTONMP(np
);
1286 int error
= 0, status
, nfsvers
, rpcflags
= 0;
1287 struct nfsm_chain nmreq
, nmrep
;
1289 if (nfs_mount_gone(nmp
))
1291 nfsvers
= nmp
->nm_vers
;
1293 if (flags
& NGA_MONITOR
) /* vnode monitor requests should be soft */
1294 rpcflags
= R_RECOVER
;
1296 if (flags
& NGA_SOFT
) /* Return ETIMEDOUT if server not responding */
1299 nfsm_chain_null(&nmreq
);
1300 nfsm_chain_null(&nmrep
);
1302 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nfsvers
));
1303 if (nfsvers
!= NFS_VER2
)
1304 nfsm_chain_add_32(error
, &nmreq
, fhsize
);
1305 nfsm_chain_add_opaque(error
, &nmreq
, fhp
, fhsize
);
1306 nfsm_chain_build_done(error
, &nmreq
);
1308 error
= nfs_request2(np
, mp
, &nmreq
, NFSPROC_GETATTR
,
1309 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
1310 NULL
, rpcflags
, &nmrep
, xidp
, &status
);
1314 error
= nfs_parsefattr(&nmrep
, nfsvers
, nvap
);
1316 nfsm_chain_cleanup(&nmreq
);
1317 nfsm_chain_cleanup(&nmrep
);
1322 * nfs_refresh_fh will attempt to update the file handle for the node.
1324 * It only does this for symbolic links and regular files that are not currently opened.
1326 * On Success returns 0 and the nodes file handle is updated, or ESTALE on failure.
1329 nfs_refresh_fh(nfsnode_t np
, vfs_context_t ctx
)
1331 vnode_t dvp
, vp
= NFSTOV(np
);
1333 const char *v_name
= vnode_getname(vp
);
1335 int namelen
, fhsize
, refreshed
;
1336 int error
, wanted
= 0;
1338 struct timespec ts
= {2, 0};
1340 NFS_VNOP_DBG("vnode is %d\n", vnode_vtype(vp
));
1342 dvp
= vnode_parent(vp
);
1343 if ((vnode_vtype(vp
) != VREG
&& vnode_vtype(vp
) != VLNK
) ||
1344 v_name
== NULL
|| *v_name
== '\0' || dvp
== NULL
) {
1346 vnode_putname(v_name
);
1351 namelen
= strlen(v_name
);
1352 MALLOC(name
, char *, namelen
+ 1, M_TEMP
, M_WAITOK
);
1354 vnode_putname(v_name
);
1357 bcopy(v_name
, name
, namelen
+1);
1358 NFS_VNOP_DBG("Trying to refresh %s : %s\n", v_name
, name
);
1359 vnode_putname(v_name
);
1361 /* Allocate the maximum size file handle */
1362 MALLOC(fhp
, uint8_t *, NFS4_FHSIZE
, M_TEMP
, M_WAITOK
);
1368 if ((error
= nfs_node_lock(np
))) {
1374 fhsize
= np
->n_fhsize
;
1375 bcopy(np
->n_fhp
, fhp
, fhsize
);
1376 while (ISSET(np
->n_flag
, NREFRESH
)) {
1377 SET(np
->n_flag
, NREFRESHWANT
);
1378 NFS_VNOP_DBG("Waiting for refresh of %s\n", name
);
1379 msleep(np
, &np
->n_lock
, PZERO
-1, "nfsrefreshwant", &ts
);
1380 if ((error
= nfs_sigintr(NFSTONMP(np
), NULL
, vfs_context_thread(ctx
), 0)))
1383 refreshed
= error
? 0 : !NFS_CMPFH(np
, fhp
, fhsize
);
1384 SET(np
->n_flag
, NREFRESH
);
1385 nfs_node_unlock(np
);
1387 NFS_VNOP_DBG("error = %d, refreshed = %d\n", error
, refreshed
);
1388 if (error
|| refreshed
)
1391 /* Check that there are no open references for this file */
1392 lck_mtx_lock(&np
->n_openlock
);
1393 if (np
->n_openrefcnt
|| !TAILQ_EMPTY(&np
->n_opens
) || !TAILQ_EMPTY(&np
->n_lock_owners
)) {
1395 struct nfs_open_file
*ofp
;
1397 TAILQ_FOREACH(ofp
, &np
->n_opens
, nof_link
) {
1398 cnt
+= ofp
->nof_opencnt
;
1401 lck_mtx_unlock(&np
->n_openlock
);
1402 NFS_VNOP_DBG("Can not refresh file handle for %s with open state\n", name
);
1403 NFS_VNOP_DBG("\topenrefcnt = %d, opens = %d lock_owners = %d\n",
1404 np
->n_openrefcnt
, cnt
, !TAILQ_EMPTY(&np
->n_lock_owners
));
1409 lck_mtx_unlock(&np
->n_openlock
);
1411 * Since the FH is currently stale we should not be able to
1412 * establish any open state until the FH is refreshed.
1415 error
= nfs_node_lock(np
);
1418 * Symlinks should never need invalidations and are holding
1419 * the one and only nfsbuf in an uncached acquired state
1420 * trying to do a readlink. So we will hang if we invalidate
1421 * in that case. Only in in the VREG case do we need to
1424 if (vnode_vtype(vp
) == VREG
) {
1425 np
->n_flag
&= ~NNEEDINVALIDATE
;
1426 nfs_node_unlock(np
);
1427 error
= nfs_vinvalbuf(vp
, V_IGNORE_WRITEERR
, ctx
, 1);
1429 NFS_VNOP_DBG("nfs_vinvalbuf returned %d\n", error
);
1432 nfs_node_unlock(np
);
1435 NFS_VNOP_DBG("Looking up %s\n", name
);
1436 error
= nfs_lookitup(dnp
, name
, namelen
, ctx
, &np
);
1438 NFS_VNOP_DBG("nfs_lookitup returned %d\n", error
);
1441 nfs_node_lock_force(np
);
1442 wanted
= ISSET(np
->n_flag
, NREFRESHWANT
);
1443 CLR(np
->n_flag
, NREFRESH
|NREFRESHWANT
);
1444 nfs_node_unlock(np
);
1449 NFS_VNOP_DBG("%s refreshed file handle\n", name
);
1454 return (error
? ESTALE
: 0);
1458 nfs_getattr(nfsnode_t np
, struct nfs_vattr
*nvap
, vfs_context_t ctx
, int flags
)
1463 error
= nfs_getattr_internal(np
, nvap
, ctx
, flags
);
1464 if (error
== ESTALE
) {
1465 error
= nfs_refresh_fh(np
, ctx
);
1473 nfs_getattr_internal(nfsnode_t np
, struct nfs_vattr
*nvap
, vfs_context_t ctx
, int flags
)
1475 struct nfsmount
*nmp
;
1476 int error
= 0, nfsvers
, inprogset
= 0, wanted
= 0, avoidfloods
;
1477 struct nfs_vattr nvattr
;
1478 struct timespec ts
= { 2, 0 };
1481 FSDBG_TOP(513, np
->n_size
, np
, np
->n_vattr
.nva_size
, np
->n_flag
);
1485 if (nfs_mount_gone(nmp
))
1487 nfsvers
= nmp
->nm_vers
;
1493 /* Update local times for special files. */
1494 if (np
->n_flag
& (NACC
| NUPD
)) {
1495 nfs_node_lock_force(np
);
1497 nfs_node_unlock(np
);
1499 /* Update size, if necessary */
1500 if (ISSET(np
->n_flag
, NUPDATESIZE
))
1501 nfs_data_update_size(np
, 0);
1503 error
= nfs_node_lock(np
);
1505 if (!(flags
& (NGA_UNCACHED
|NGA_MONITOR
)) || ((nfsvers
>= NFS_VER4
) && (np
->n_openflags
& N_DELEG_MASK
))) {
1507 * Use the cache or wait for any getattr in progress if:
1508 * - it's a cached request, or
1509 * - we have a delegation, or
1510 * - the server isn't responding
1513 error
= nfs_getattrcache(np
, nvap
, flags
);
1514 if (!error
|| (error
!= ENOENT
)) {
1515 nfs_node_unlock(np
);
1519 if (!ISSET(np
->n_flag
, NGETATTRINPROG
))
1521 if (flags
& NGA_MONITOR
) {
1522 /* no need to wait if a request is pending */
1523 error
= EINPROGRESS
;
1524 nfs_node_unlock(np
);
1527 SET(np
->n_flag
, NGETATTRWANT
);
1528 msleep(np
, &np
->n_lock
, PZERO
-1, "nfsgetattrwant", &ts
);
1529 if ((error
= nfs_sigintr(NFSTONMP(np
), NULL
, vfs_context_thread(ctx
), 0))) {
1530 nfs_node_unlock(np
);
1534 SET(np
->n_flag
, NGETATTRINPROG
);
1536 } else if (!ISSET(np
->n_flag
, NGETATTRINPROG
)) {
1537 SET(np
->n_flag
, NGETATTRINPROG
);
1539 } else if (flags
& NGA_MONITOR
) {
1540 /* no need to make a request if one is pending */
1541 error
= EINPROGRESS
;
1543 nfs_node_unlock(np
);
1546 if (nfs_mount_gone(nmp
))
1552 * Return cached attributes if they are valid,
1553 * if the server doesn't respond, and this is
1554 * some softened up style of mount.
1556 if (NATTRVALID(np
) && nfs_use_cache(nmp
))
1560 * We might want to try to get both the attributes and access info by
1561 * making an ACCESS call and seeing if it returns updated attributes.
1562 * But don't bother if we aren't caching access info or if the
1563 * attributes returned wouldn't be cached.
1565 if (!(flags
& NGA_ACL
) && (nfsvers
!= NFS_VER2
) && nfs_access_for_getattr
&& (nfs_access_cache_timeout
> 0)) {
1566 if (nfs_attrcachetimeout(np
) > 0) {
1567 /* OSAddAtomic(1, &nfsstats.accesscache_misses); */
1568 u_int32_t access
= NFS_ACCESS_ALL
;
1571 /* Return cached attrs if server doesn't respond */
1572 if (flags
& NGA_SOFT
)
1575 error
= nmp
->nm_funcs
->nf_access_rpc(np
, &access
, rpcflags
, ctx
);
1577 if (error
== ETIMEDOUT
)
1582 nfs_node_lock_force(np
);
1583 error
= nfs_getattrcache(np
, nvap
, flags
);
1584 nfs_node_unlock(np
);
1585 if (!error
|| (error
!= ENOENT
))
1587 /* Well, that didn't work... just do a getattr... */
1595 error
= nmp
->nm_funcs
->nf_getattr_rpc(np
, NULL
, np
->n_fhp
, np
->n_fhsize
, flags
, ctx
, nvap
, &xid
);
1597 nfs_node_lock_force(np
);
1598 error
= nfs_loadattrcache(np
, nvap
, &xid
, 0);
1599 nfs_node_unlock(np
);
1603 * If the server didn't respond, return cached attributes.
1606 if ((flags
& NGA_SOFT
) && (error
== ETIMEDOUT
)) {
1607 nfs_node_lock_force(np
);
1608 error
= nfs_getattrcache(np
, nvap
, flags
);
1609 if (!error
|| (error
!= ENOENT
)) {
1610 nfs_node_unlock(np
);
1613 nfs_node_unlock(np
);
1617 if (!xid
) { /* out-of-order rpc - attributes were dropped */
1618 FSDBG(513, -1, np
, np
->n_xid
>> 32, np
->n_xid
);
1619 if (avoidfloods
++ < 20)
1621 /* avoidfloods>1 is bizarre. at 20 pull the plug */
1622 /* just return the last attributes we got */
1625 nfs_node_lock_force(np
);
1627 wanted
= ISSET(np
->n_flag
, NGETATTRWANT
);
1628 CLR(np
->n_flag
, (NGETATTRINPROG
| NGETATTRWANT
));
1631 /* check if the node changed on us */
1632 vnode_t vp
= NFSTOV(np
);
1633 enum vtype vtype
= vnode_vtype(vp
);
1634 if ((vtype
== VDIR
) && NFS_CHANGED_NC(nfsvers
, np
, nvap
)) {
1635 FSDBG(513, -1, np
, 0, np
);
1636 np
->n_flag
&= ~NNEGNCENTRIES
;
1639 NFS_CHANGED_UPDATE_NC(nfsvers
, np
, nvap
);
1640 NFS_VNOP_DBG("Purge directory 0x%llx\n",
1641 (uint64_t)VM_KERNEL_ADDRPERM(vp
));
1643 if (NFS_CHANGED(nfsvers
, np
, nvap
)) {
1644 FSDBG(513, -1, np
, -1, np
);
1645 if (vtype
== VDIR
) {
1646 NFS_VNOP_DBG("Invalidate directory 0x%llx\n",
1647 (uint64_t)VM_KERNEL_ADDRPERM(vp
));
1650 nfs_node_unlock(np
);
1653 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
1654 FSDBG(513, -1, np
, -2, error
);
1656 nfs_node_lock_force(np
);
1657 NFS_CHANGED_UPDATE(nfsvers
, np
, nvap
);
1658 nfs_node_unlock(np
);
1661 nfs_node_unlock(np
);
1666 nfs_node_unlock(np
);
1671 if (nvap
== &nvattr
) {
1672 NVATTR_CLEANUP(nvap
);
1673 } else if (!(flags
& NGA_ACL
)) {
1674 /* make sure we don't return an ACL if it wasn't asked for */
1675 NFS_BITMAP_CLR(nvap
->nva_bitmap
, NFS_FATTR_ACL
);
1676 if (nvap
->nva_acl
) {
1677 kauth_acl_free(nvap
->nva_acl
);
1678 nvap
->nva_acl
= NULL
;
1681 FSDBG_BOT(513, np
->n_size
, error
, np
->n_vattr
.nva_size
, np
->n_flag
);
1686 * NFS getattr call from vfs.
1690 * The attributes we support over the wire.
1691 * We also get fsid but the vfs layer gets it out of the mount
1692 * structure after this calling us so there's no need to return it,
1693 * and Finder expects to call getattrlist just looking for the FSID
1694 * with out hanging on a non responsive server.
1696 #define NFS3_SUPPORTED_VATTRS \
1697 (VNODE_ATTR_va_rdev | \
1698 VNODE_ATTR_va_nlink | \
1699 VNODE_ATTR_va_data_size | \
1700 VNODE_ATTR_va_data_alloc | \
1701 VNODE_ATTR_va_uid | \
1702 VNODE_ATTR_va_gid | \
1703 VNODE_ATTR_va_mode | \
1704 VNODE_ATTR_va_modify_time | \
1705 VNODE_ATTR_va_change_time | \
1706 VNODE_ATTR_va_access_time | \
1707 VNODE_ATTR_va_fileid | \
1712 struct vnop_getattr_args
/* {
1713 struct vnodeop_desc *a_desc;
1715 struct vnode_attr *a_vap;
1716 vfs_context_t a_context;
1720 struct nfs_vattr nva
;
1721 struct vnode_attr
*vap
= ap
->a_vap
;
1725 * Lets don't go over the wire if we don't support any of the attributes.
1726 * Just fall through at the VFS layer and let it cons up what it needs.
1728 /* Return the io size no matter what, since we don't go over the wire for this */
1729 VATTR_RETURN(vap
, va_iosize
, nfs_iosize
);
1730 if ((vap
->va_active
& NFS3_SUPPORTED_VATTRS
) == 0)
1733 if (VATTR_IS_ACTIVE(ap
->a_vap
, va_name
))
1734 NFS_VNOP_DBG("Getting attrs for 0x%llx, vname is %s\n",
1735 (uint64_t)VM_KERNEL_ADDRPERM(ap
->a_vp
),
1736 ap
->a_vp
->v_name
? ap
->a_vp
->v_name
: "empty");
1737 error
= nfs_getattr(VTONFS(ap
->a_vp
), &nva
, ap
->a_context
, NGA_CACHED
);
1741 /* copy nva to *a_vap */
1742 VATTR_RETURN(vap
, va_type
, nva
.nva_type
);
1743 VATTR_RETURN(vap
, va_mode
, nva
.nva_mode
);
1744 rdev
= makedev(nva
.nva_rawdev
.specdata1
, nva
.nva_rawdev
.specdata2
);
1745 VATTR_RETURN(vap
, va_rdev
, rdev
);
1746 VATTR_RETURN(vap
, va_uid
, nva
.nva_uid
);
1747 VATTR_RETURN(vap
, va_gid
, nva
.nva_gid
);
1748 VATTR_RETURN(vap
, va_nlink
, nva
.nva_nlink
);
1749 VATTR_RETURN(vap
, va_fileid
, nva
.nva_fileid
);
1750 VATTR_RETURN(vap
, va_data_size
, nva
.nva_size
);
1751 VATTR_RETURN(vap
, va_data_alloc
, nva
.nva_bytes
);
1752 vap
->va_access_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_ACCESS
];
1753 vap
->va_access_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_ACCESS
];
1754 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1755 vap
->va_modify_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_MODIFY
];
1756 vap
->va_modify_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_MODIFY
];
1757 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1758 vap
->va_change_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_CHANGE
];
1759 vap
->va_change_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_CHANGE
];
1760 VATTR_SET_SUPPORTED(vap
, va_change_time
);
1762 // VATTR_RETURN(vap, va_encoding, 0xffff /* kTextEncodingUnknown */);
1771 struct vnop_setattr_args
/* {
1772 struct vnodeop_desc *a_desc;
1774 struct vnode_attr *a_vap;
1775 vfs_context_t a_context;
1778 vfs_context_t ctx
= ap
->a_context
;
1779 vnode_t vp
= ap
->a_vp
;
1780 nfsnode_t np
= VTONFS(vp
);
1781 struct nfsmount
*nmp
;
1782 struct vnode_attr
*vap
= ap
->a_vap
;
1784 int biosize
, nfsvers
, namedattrs
;
1785 u_quad_t origsize
, vapsize
;
1786 struct nfs_dulookup dul
;
1787 nfsnode_t dnp
= NULL
;
1789 const char *vname
= NULL
;
1790 struct nfs_open_owner
*noop
= NULL
;
1791 struct nfs_open_file
*nofp
= NULL
;
1794 if (nfs_mount_gone(nmp
))
1796 nfsvers
= nmp
->nm_vers
;
1797 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
1798 biosize
= nmp
->nm_biosize
;
1800 /* Disallow write attempts if the filesystem is mounted read-only. */
1801 if (vnode_vfsisrdonly(vp
))
1804 origsize
= np
->n_size
;
1805 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
1806 switch (vnode_vtype(vp
)) {
1813 if (!VATTR_IS_ACTIVE(vap
, va_modify_time
) &&
1814 !VATTR_IS_ACTIVE(vap
, va_access_time
) &&
1815 !VATTR_IS_ACTIVE(vap
, va_mode
) &&
1816 !VATTR_IS_ACTIVE(vap
, va_uid
) &&
1817 !VATTR_IS_ACTIVE(vap
, va_gid
)) {
1820 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
1824 * Disallow write attempts if the filesystem is
1825 * mounted read-only.
1827 if (vnode_vfsisrdonly(vp
))
1829 FSDBG_TOP(512, np
->n_size
, vap
->va_data_size
,
1830 np
->n_vattr
.nva_size
, np
->n_flag
);
1831 /* clear NNEEDINVALIDATE, if set */
1832 if ((error
= nfs_node_lock(np
)))
1834 if (np
->n_flag
& NNEEDINVALIDATE
)
1835 np
->n_flag
&= ~NNEEDINVALIDATE
;
1836 nfs_node_unlock(np
);
1837 /* flush everything */
1838 error
= nfs_vinvalbuf(vp
, (vap
->va_data_size
? V_SAVE
: 0) , ctx
, 1);
1840 NP(np
, "nfs_setattr: nfs_vinvalbuf %d", error
);
1841 FSDBG_BOT(512, np
->n_size
, vap
->va_data_size
, np
->n_vattr
.nva_size
, -1);
1844 if (nfsvers
>= NFS_VER4
) {
1845 /* setting file size requires having the file open for write access */
1846 if (np
->n_flag
& NREVOKE
)
1848 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
1852 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
1855 if (np
->n_flag
& NREVOKE
) {
1856 nfs_mount_state_in_use_end(nmp
, 0);
1859 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 1);
1860 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
))
1862 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
1863 nfs_mount_state_in_use_end(nmp
, 0);
1864 error
= nfs4_reopen(nofp
, vfs_context_thread(ctx
));
1870 error
= nfs_open_file_set_busy(nofp
, vfs_context_thread(ctx
));
1872 nfs_open_owner_rele(noop
);
1875 if (!(nofp
->nof_access
& NFS_OPEN_SHARE_ACCESS_WRITE
)) {
1876 /* we don't have the file open for write access, so open it */
1877 error
= nfs4_open(np
, nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
1879 nofp
->nof_flags
|= NFS_OPEN_FILE_SETATTR
;
1880 if (nfs_mount_state_error_should_restart(error
)) {
1881 nfs_open_file_clear_busy(nofp
);
1883 if (nfs_mount_state_in_use_end(nmp
, error
))
1888 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
1889 if (np
->n_size
> vap
->va_data_size
) { /* shrinking? */
1891 int neweofoff
, mustwrite
;
1894 obn
= (np
->n_size
- 1) / biosize
;
1895 bn
= vap
->va_data_size
/ biosize
;
1896 for ( ; obn
>= bn
; obn
--) {
1897 if (!nfs_buf_is_incore(np
, obn
))
1899 error
= nfs_buf_get(np
, obn
, biosize
, NULL
, NBLK_READ
, &bp
);
1903 FSDBG(512, bp
, bp
->nb_flags
, 0, obn
);
1904 SET(bp
->nb_flags
, NB_INVAL
);
1905 nfs_buf_release(bp
, 1);
1909 neweofoff
= vap
->va_data_size
- NBOFF(bp
);
1910 /* check for any dirty data before the new EOF */
1911 if ((bp
->nb_dirtyend
> 0) && (bp
->nb_dirtyoff
< neweofoff
)) {
1912 /* clip dirty range to EOF */
1913 if (bp
->nb_dirtyend
> neweofoff
) {
1914 bp
->nb_dirtyend
= neweofoff
;
1915 if (bp
->nb_dirtyoff
>= bp
->nb_dirtyend
)
1916 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
1918 if ((bp
->nb_dirtyend
> 0) && (bp
->nb_dirtyoff
< neweofoff
))
1921 bp
->nb_dirty
&= (1 << round_page_32(neweofoff
)/PAGE_SIZE
) - 1;
1925 FSDBG(512, bp
, bp
->nb_flags
, 0, obn
);
1926 SET(bp
->nb_flags
, NB_INVAL
);
1927 nfs_buf_release(bp
, 1);
1930 /* gotta write out dirty data before invalidating */
1931 /* (NB_STABLE indicates that data writes should be FILESYNC) */
1932 /* (NB_NOCACHE indicates buffer should be discarded) */
1933 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
| NB_ASYNC
| NB_READ
));
1934 SET(bp
->nb_flags
, NB_STABLE
| NB_NOCACHE
);
1935 if (!IS_VALID_CRED(bp
->nb_wcred
)) {
1936 kauth_cred_t cred
= vfs_context_ucred(ctx
);
1937 kauth_cred_ref(cred
);
1938 bp
->nb_wcred
= cred
;
1940 error
= nfs_buf_write(bp
);
1941 // Note: bp has been released
1943 FSDBG(512, bp
, 0xd00dee, 0xbad, error
);
1944 nfs_node_lock_force(np
);
1945 np
->n_error
= error
;
1946 np
->n_flag
|= NWRITEERR
;
1948 * There was a write error and we need to
1949 * invalidate attrs and flush buffers in
1950 * order to sync up with the server.
1951 * (if this write was extending the file,
1952 * we may no longer know the correct size)
1954 NATTRINVALIDATE(np
);
1955 nfs_node_unlock(np
);
1956 nfs_data_unlock(np
);
1957 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, ctx
, 1);
1958 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
1963 if (vap
->va_data_size
!= np
->n_size
)
1964 ubc_setsize(vp
, (off_t
)vap
->va_data_size
); /* XXX error? */
1965 origsize
= np
->n_size
;
1966 np
->n_size
= np
->n_vattr
.nva_size
= vap
->va_data_size
;
1967 nfs_node_lock_force(np
);
1968 CLR(np
->n_flag
, NUPDATESIZE
);
1969 nfs_node_unlock(np
);
1970 FSDBG(512, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0001);
1972 } else if (VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
1973 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
1974 (vap
->va_vaflags
& VA_UTIMES_NULL
)) {
1975 if ((error
= nfs_node_lock(np
)))
1977 if ((np
->n_flag
& NMODIFIED
) && (vnode_vtype(vp
) == VREG
)) {
1978 nfs_node_unlock(np
);
1979 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
1983 nfs_node_unlock(np
);
1986 if ((VATTR_IS_ACTIVE(vap
, va_mode
) || VATTR_IS_ACTIVE(vap
, va_uid
) || VATTR_IS_ACTIVE(vap
, va_gid
) ||
1987 VATTR_IS_ACTIVE(vap
, va_acl
) || VATTR_IS_ACTIVE(vap
, va_uuuid
) || VATTR_IS_ACTIVE(vap
, va_guuid
)) &&
1988 !(error
= nfs_node_lock(np
))) {
1989 NACCESSINVALIDATE(np
);
1990 nfs_node_unlock(np
);
1992 dvp
= vnode_getparent(vp
);
1993 vname
= vnode_getname(vp
);
1994 dnp
= (dvp
&& vname
) ? VTONFS(dvp
) : NULL
;
1996 error
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
2003 nfs_dulookup_init(&dul
, dnp
, vname
, strlen(vname
), ctx
);
2004 nfs_dulookup_start(&dul
, dnp
, ctx
);
2010 error
= nmp
->nm_funcs
->nf_setattr_rpc(np
, vap
, ctx
);
2012 if (VATTR_IS_ACTIVE(vap
, va_mode
) || VATTR_IS_ACTIVE(vap
, va_uid
) || VATTR_IS_ACTIVE(vap
, va_gid
) ||
2013 VATTR_IS_ACTIVE(vap
, va_acl
) || VATTR_IS_ACTIVE(vap
, va_uuuid
) || VATTR_IS_ACTIVE(vap
, va_guuid
)) {
2016 nfs_dulookup_finish(&dul
, dnp
, ctx
);
2017 nfs_node_clear_busy(dnp
);
2022 vnode_putname(vname
);
2026 FSDBG_BOT(512, np
->n_size
, vap
->va_data_size
, np
->n_vattr
.nva_size
, error
);
2027 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
2028 if (error
&& (origsize
!= np
->n_size
) &&
2029 ((nfsvers
< NFS_VER4
) || !nfs_mount_state_error_should_restart(error
))) {
2030 /* make every effort to resync file size w/ server... */
2031 /* (don't bother if we'll be restarting the operation) */
2032 int err
; /* preserve "error" for return */
2033 np
->n_size
= np
->n_vattr
.nva_size
= origsize
;
2034 nfs_node_lock_force(np
);
2035 CLR(np
->n_flag
, NUPDATESIZE
);
2036 nfs_node_unlock(np
);
2037 FSDBG(512, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0002);
2038 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX check error */
2039 vapsize
= vap
->va_data_size
;
2040 vap
->va_data_size
= origsize
;
2041 err
= nmp
->nm_funcs
->nf_setattr_rpc(np
, vap
, ctx
);
2043 NP(np
, "nfs_vnop_setattr: nfs%d_setattr_rpc %d %d", nfsvers
, error
, err
);
2044 vap
->va_data_size
= vapsize
;
2046 nfs_node_lock_force(np
);
2048 * The size was just set. If the size is already marked for update, don't
2049 * trust the newsize (it may have been set while the setattr was in progress).
2050 * Clear the update flag and make sure we fetch new attributes so we are sure
2051 * we have the latest size.
2053 if (ISSET(np
->n_flag
, NUPDATESIZE
)) {
2054 CLR(np
->n_flag
, NUPDATESIZE
);
2055 NATTRINVALIDATE(np
);
2056 nfs_node_unlock(np
);
2057 nfs_getattr(np
, NULL
, ctx
, NGA_UNCACHED
);
2059 nfs_node_unlock(np
);
2061 nfs_data_unlock(np
);
2062 if (nfsvers
>= NFS_VER4
) {
2064 /* don't close our setattr open if we'll be restarting... */
2065 if (!nfs_mount_state_error_should_restart(error
) &&
2066 (nofp
->nof_flags
& NFS_OPEN_FILE_SETATTR
)) {
2067 int err
= nfs_close(np
, nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
2069 NP(np
, "nfs_vnop_setattr: close error: %d", err
);
2070 nofp
->nof_flags
&= ~NFS_OPEN_FILE_SETATTR
;
2072 nfs_open_file_clear_busy(nofp
);
2075 if (nfs_mount_state_in_use_end(nmp
, error
))
2077 nfs_open_owner_rele(noop
);
2084 * Do an NFS setattr RPC.
2089 struct vnode_attr
*vap
,
2092 struct nfsmount
*nmp
= NFSTONMP(np
);
2093 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0, nfsvers
;
2094 u_int64_t xid
, nextxid
;
2095 struct nfsm_chain nmreq
, nmrep
;
2097 if (nfs_mount_gone(nmp
))
2099 nfsvers
= nmp
->nm_vers
;
2101 VATTR_SET_SUPPORTED(vap
, va_mode
);
2102 VATTR_SET_SUPPORTED(vap
, va_uid
);
2103 VATTR_SET_SUPPORTED(vap
, va_gid
);
2104 VATTR_SET_SUPPORTED(vap
, va_data_size
);
2105 VATTR_SET_SUPPORTED(vap
, va_access_time
);
2106 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
2108 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
2109 if (vap
->va_flags
) { /* we don't support setting flags */
2110 if (vap
->va_active
& ~VNODE_ATTR_va_flags
)
2111 return (EINVAL
); /* return EINVAL if other attributes also set */
2113 return (ENOTSUP
); /* return ENOTSUP for chflags(2) */
2115 /* no flags set, so we'll just ignore it */
2116 if (!(vap
->va_active
& ~VNODE_ATTR_va_flags
))
2117 return (0); /* no (other) attributes to set, so nothing to do */
2120 nfsm_chain_null(&nmreq
);
2121 nfsm_chain_null(&nmrep
);
2123 nfsm_chain_build_alloc_init(error
, &nmreq
,
2124 NFSX_FH(nfsvers
) + NFSX_SATTR(nfsvers
));
2125 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
2126 if (nfsvers
== NFS_VER3
) {
2127 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
2128 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
2129 nfsm_chain_add_32(error
, &nmreq
, vap
->va_mode
);
2131 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2133 if (VATTR_IS_ACTIVE(vap
, va_uid
)) {
2134 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
2135 nfsm_chain_add_32(error
, &nmreq
, vap
->va_uid
);
2137 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2139 if (VATTR_IS_ACTIVE(vap
, va_gid
)) {
2140 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
2141 nfsm_chain_add_32(error
, &nmreq
, vap
->va_gid
);
2143 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2145 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
2146 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
2147 nfsm_chain_add_64(error
, &nmreq
, vap
->va_data_size
);
2149 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2151 if (vap
->va_vaflags
& VA_UTIMES_NULL
) {
2152 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_SERVER
);
2153 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_SERVER
);
2155 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
2156 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_CLIENT
);
2157 nfsm_chain_add_32(error
, &nmreq
, vap
->va_access_time
.tv_sec
);
2158 nfsm_chain_add_32(error
, &nmreq
, vap
->va_access_time
.tv_nsec
);
2160 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_DONT_CHANGE
);
2162 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
2163 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_CLIENT
);
2164 nfsm_chain_add_32(error
, &nmreq
, vap
->va_modify_time
.tv_sec
);
2165 nfsm_chain_add_32(error
, &nmreq
, vap
->va_modify_time
.tv_nsec
);
2167 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_DONT_CHANGE
);
2170 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2172 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_mode
) ?
2173 vtonfsv2_mode(vnode_vtype(NFSTOV(np
)), vap
->va_mode
) : -1);
2174 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_uid
) ?
2175 vap
->va_uid
: (uint32_t)-1);
2176 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_gid
) ?
2177 vap
->va_gid
: (uint32_t)-1);
2178 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_data_size
) ?
2179 vap
->va_data_size
: (uint32_t)-1);
2180 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
2181 nfsm_chain_add_32(error
, &nmreq
, vap
->va_access_time
.tv_sec
);
2182 nfsm_chain_add_32(error
, &nmreq
, (vap
->va_access_time
.tv_nsec
!= -1) ?
2183 ((uint32_t)vap
->va_access_time
.tv_nsec
/ 1000) : 0xffffffff);
2185 nfsm_chain_add_32(error
, &nmreq
, -1);
2186 nfsm_chain_add_32(error
, &nmreq
, -1);
2188 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
2189 nfsm_chain_add_32(error
, &nmreq
, vap
->va_modify_time
.tv_sec
);
2190 nfsm_chain_add_32(error
, &nmreq
, (vap
->va_modify_time
.tv_nsec
!= -1) ?
2191 ((uint32_t)vap
->va_modify_time
.tv_nsec
/ 1000) : 0xffffffff);
2193 nfsm_chain_add_32(error
, &nmreq
, -1);
2194 nfsm_chain_add_32(error
, &nmreq
, -1);
2197 nfsm_chain_build_done(error
, &nmreq
);
2199 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_SETATTR
, ctx
, NULL
, &nmrep
, &xid
, &status
);
2200 if ((lockerror
= nfs_node_lock(np
)))
2202 if (nfsvers
== NFS_VER3
) {
2203 struct timespec premtime
= { 0, 0 };
2204 nfsm_chain_get_wcc_data(error
, &nmrep
, np
, &premtime
, &wccpostattr
, &xid
);
2206 /* if file hadn't changed, update cached mtime */
2207 if (nfstimespeccmp(&np
->n_mtime
, &premtime
, ==))
2208 NFS_CHANGED_UPDATE(nfsvers
, np
, &np
->n_vattr
);
2209 /* if directory hadn't changed, update namecache mtime */
2210 if ((vnode_vtype(NFSTOV(np
)) == VDIR
) &&
2211 nfstimespeccmp(&np
->n_ncmtime
, &premtime
, ==))
2212 NFS_CHANGED_UPDATE_NC(nfsvers
, np
, &np
->n_vattr
);
2214 NATTRINVALIDATE(np
);
2219 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
2222 * We just changed the attributes and we want to make sure that we
2223 * see the latest attributes. Get the next XID. If it's not the
2224 * next XID after the SETATTR XID, then it's possible that another
2225 * RPC was in flight at the same time and it might put stale attributes
2226 * in the cache. In that case, we invalidate the attributes and set
2227 * the attribute cache XID to guarantee that newer attributes will
2231 nfs_get_xid(&nextxid
);
2232 if (nextxid
!= (xid
+ 1)) {
2233 np
->n_xid
= nextxid
;
2234 NATTRINVALIDATE(np
);
2238 nfs_node_unlock(np
);
2239 nfsm_chain_cleanup(&nmreq
);
2240 nfsm_chain_cleanup(&nmrep
);
2245 * NFS lookup call, one step at a time...
2246 * First look in cache
2247 * If not found, unlock the directory nfsnode and do the RPC
2251 struct vnop_lookup_args
/* {
2252 struct vnodeop_desc *a_desc;
2255 struct componentname *a_cnp;
2256 vfs_context_t a_context;
2259 vfs_context_t ctx
= ap
->a_context
;
2260 struct componentname
*cnp
= ap
->a_cnp
;
2261 vnode_t dvp
= ap
->a_dvp
;
2262 vnode_t
*vpp
= ap
->a_vpp
;
2263 int flags
= cnp
->cn_flags
;
2266 struct nfsmount
*nmp
;
2268 int nfsvers
, error
, busyerror
= ENOENT
, isdot
, isdotdot
, negnamecache
;
2270 struct nfs_vattr nvattr
;
2272 struct vnop_access_args naa
;
2274 struct nfsreq rq
, *req
= &rq
;
2279 NVATTR_INIT(&nvattr
);
2281 mp
= vnode_mount(dvp
);
2283 if (nfs_mount_gone(nmp
)) {
2287 nfsvers
= nmp
->nm_vers
;
2288 negnamecache
= !NMFLAG(nmp
, NONEGNAMECACHE
);
2290 if ((error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
))))
2292 /* nfs_getattr() will check changed and purge caches */
2293 if ((error
= nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
)))
2296 error
= cache_lookup(dvp
, vpp
, cnp
);
2299 /* negative cache entry */
2303 if ((nfsvers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
)) {
2304 /* if rdirplus, try dir buf cache lookup */
2305 error
= nfs_dir_buf_cache_lookup(dnp
, &np
, cnp
, ctx
, 0);
2307 /* dir buf cache hit */
2312 if (error
!= -1) /* cache miss */
2316 /* cache hit, not really an error */
2317 OSAddAtomic64(1, &nfsstats
.lookupcache_hits
);
2319 nfs_node_clear_busy(dnp
);
2322 /* check for directory access */
2323 naa
.a_desc
= &vnop_access_desc
;
2325 naa
.a_action
= KAUTH_VNODE_SEARCH
;
2326 naa
.a_context
= ctx
;
2328 /* compute actual success/failure based on accessibility */
2329 error
= nfs_vnop_access(&naa
);
2332 /* unexpected error from cache_lookup */
2336 /* skip lookup, if we know who we are: "." or ".." */
2337 isdot
= isdotdot
= 0;
2338 if (cnp
->cn_nameptr
[0] == '.') {
2339 if (cnp
->cn_namelen
== 1)
2341 if ((cnp
->cn_namelen
== 2) && (cnp
->cn_nameptr
[1] == '.'))
2344 if (isdotdot
|| isdot
) {
2348 if ((nfsvers
>= NFS_VER4
) && (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER
)) {
2349 /* we should never be looking things up in a trigger directory, return nothing */
2354 /* do we know this name is too long? */
2356 if (nfs_mount_gone(nmp
)) {
2360 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXNAME
) &&
2361 (cnp
->cn_namelen
> (int)nmp
->nm_fsattr
.nfsa_maxname
)) {
2362 error
= ENAMETOOLONG
;
2369 OSAddAtomic64(1, &nfsstats
.lookupcache_misses
);
2371 error
= nmp
->nm_funcs
->nf_lookup_rpc_async(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &req
);
2373 error
= nmp
->nm_funcs
->nf_lookup_rpc_async_finish(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, req
, &xid
, &fh
, &nvattr
);
2376 /* is the file handle the same as this directory's file handle? */
2377 isdot
= NFS_CMPFH(dnp
, fh
.fh_data
, fh
.fh_len
);
2380 if (flags
& ISLASTCN
) {
2381 switch (cnp
->cn_nameiop
) {
2383 cnp
->cn_flags
&= ~MAKEENTRY
;
2386 cnp
->cn_flags
&= ~MAKEENTRY
;
2396 newvp
= vnode_getparent(dvp
);
2402 error
= vnode_get(dvp
);
2406 nfs_node_lock_force(dnp
);
2407 if (fh
.fh_len
&& (dnp
->n_xid
<= xid
))
2408 nfs_loadattrcache(dnp
, &nvattr
, &xid
, 0);
2409 nfs_node_unlock(dnp
);
2411 ngflags
= (cnp
->cn_flags
& MAKEENTRY
) ? NG_MAKEENTRY
: 0;
2412 error
= nfs_nget(mp
, dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, ngflags
, &np
);
2416 nfs_node_unlock(np
);
2422 if (((cnp
->cn_nameiop
== CREATE
) || (cnp
->cn_nameiop
== RENAME
)) &&
2423 (flags
& ISLASTCN
) && (error
== ENOENT
)) {
2424 if (vnode_mount(dvp
) && vnode_vfsisrdonly(dvp
))
2427 error
= EJUSTRETURN
;
2430 if ((error
== ENOENT
) && (cnp
->cn_flags
& MAKEENTRY
) &&
2431 (cnp
->cn_nameiop
!= CREATE
) && negnamecache
) {
2432 /* add a negative entry in the name cache */
2433 nfs_node_lock_force(dnp
);
2434 cache_enter(dvp
, NULL
, cnp
);
2435 dnp
->n_flag
|= NNEGNCENTRIES
;
2436 nfs_node_unlock(dnp
);
2439 NVATTR_CLEANUP(&nvattr
);
2441 nfs_node_clear_busy(dnp
);
2442 if (error
&& *vpp
) {
2449 int nfs_readlink_nocache
= DEFAULT_READLINK_NOCACHE
;
2456 struct vnop_readlink_args
/* {
2457 struct vnodeop_desc *a_desc;
2460 vfs_context_t a_context;
2463 vfs_context_t ctx
= ap
->a_context
;
2464 nfsnode_t np
= VTONFS(ap
->a_vp
);
2465 struct nfsmount
*nmp
;
2466 int error
= 0, nfsvers
;
2468 uio_t uio
= ap
->a_uio
;
2469 struct nfsbuf
*bp
= NULL
;
2473 if (vnode_vtype(ap
->a_vp
) != VLNK
)
2476 if (uio_resid(uio
) == 0)
2478 if (uio_offset(uio
) < 0)
2481 nmp
= VTONMP(ap
->a_vp
);
2482 if (nfs_mount_gone(nmp
))
2484 nfsvers
= nmp
->nm_vers
;
2487 /* nfs_getattr() will check changed and purge caches */
2488 if ((error
= nfs_getattr(np
, NULL
, ctx
, nfs_readlink_nocache
? NGA_UNCACHED
: NGA_CACHED
))) {
2489 FSDBG(531, np
, 0xd1e0001, 0, error
);
2493 if (nfs_readlink_nocache
) {
2494 timeo
= nfs_attrcachetimeout(np
);
2499 OSAddAtomic64(1, &nfsstats
.biocache_readlinks
);
2500 error
= nfs_buf_get(np
, 0, NFS_MAXPATHLEN
, vfs_context_thread(ctx
), NBLK_META
, &bp
);
2502 FSDBG(531, np
, 0xd1e0002, 0, error
);
2506 if (nfs_readlink_nocache
) {
2507 NFS_VNOP_DBG("timeo = %d ts.tv_sec = %ld need refresh = %d cached = %d\n", timeo
, ts
.tv_sec
,
2508 (np
->n_rltim
.tv_sec
+ timeo
) < ts
.tv_sec
|| nfs_readlink_nocache
> 1,
2509 ISSET(bp
->nb_flags
, NB_CACHE
) == NB_CACHE
);
2510 /* n_rltim is synchronized by the associated nfs buf */
2511 if (ISSET(bp
->nb_flags
, NB_CACHE
) && ((nfs_readlink_nocache
> 1) || ((np
->n_rltim
.tv_sec
+ timeo
) < ts
.tv_sec
))) {
2512 SET(bp
->nb_flags
, NB_INVAL
);
2513 nfs_buf_release(bp
, 0);
2517 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) {
2519 OSAddAtomic64(1, &nfsstats
.readlink_bios
);
2520 buflen
= bp
->nb_bufsize
;
2521 error
= nmp
->nm_funcs
->nf_readlink_rpc(np
, bp
->nb_data
, &buflen
, ctx
);
2523 if (error
== ESTALE
) {
2524 NFS_VNOP_DBG("Stale FH from readlink rpc\n");
2525 error
= nfs_refresh_fh(np
, ctx
);
2529 SET(bp
->nb_flags
, NB_ERROR
);
2530 bp
->nb_error
= error
;
2531 NFS_VNOP_DBG("readlink failed %d\n", error
);
2533 bp
->nb_validoff
= 0;
2534 bp
->nb_validend
= buflen
;
2536 NFS_VNOP_DBG("readlink of %.*s\n", bp
->nb_validend
, (char *)bp
->nb_data
);
2539 NFS_VNOP_DBG("got cached link of %.*s\n", bp
->nb_validend
, (char *)bp
->nb_data
);
2542 if (!error
&& (bp
->nb_validend
> 0))
2543 error
= uiomove(bp
->nb_data
, bp
->nb_validend
, uio
);
2544 FSDBG(531, np
, bp
->nb_validend
, 0, error
);
2545 nfs_buf_release(bp
, 1);
2550 * Do a readlink RPC.
2553 nfs3_readlink_rpc(nfsnode_t np
, char *buf
, uint32_t *buflenp
, vfs_context_t ctx
)
2555 struct nfsmount
*nmp
;
2556 int error
= 0, lockerror
= ENOENT
, nfsvers
, status
;
2559 struct nfsm_chain nmreq
, nmrep
;
2562 if (nfs_mount_gone(nmp
))
2564 nfsvers
= nmp
->nm_vers
;
2565 nfsm_chain_null(&nmreq
);
2566 nfsm_chain_null(&nmrep
);
2568 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nfsvers
));
2569 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
2570 nfsm_chain_build_done(error
, &nmreq
);
2572 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_READLINK
, ctx
, NULL
, &nmrep
, &xid
, &status
);
2573 if ((lockerror
= nfs_node_lock(np
)))
2575 if (nfsvers
== NFS_VER3
)
2576 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
2579 nfsm_chain_get_32(error
, &nmrep
, len
);
2581 if ((nfsvers
== NFS_VER2
) && (len
> *buflenp
)) {
2585 if (len
>= *buflenp
) {
2586 if (np
->n_size
&& (np
->n_size
< *buflenp
))
2591 nfsm_chain_get_opaque(error
, &nmrep
, len
, buf
);
2596 nfs_node_unlock(np
);
2597 nfsm_chain_cleanup(&nmreq
);
2598 nfsm_chain_cleanup(&nmrep
);
2607 nfs_read_rpc(nfsnode_t np
, uio_t uio
, vfs_context_t ctx
)
2609 struct nfsmount
*nmp
;
2610 int error
= 0, nfsvers
, eof
= 0;
2611 size_t nmrsize
, len
, retlen
;
2614 struct nfsreq rq
, *req
= &rq
;
2615 uint32_t stategenid
= 0, restart
= 0;
2617 FSDBG_TOP(536, np
, uio_offset(uio
), uio_resid(uio
), 0);
2619 if (nfs_mount_gone(nmp
))
2621 nfsvers
= nmp
->nm_vers
;
2622 nmrsize
= nmp
->nm_rsize
;
2624 txoffset
= uio_offset(uio
);
2625 tsiz
= uio_resid(uio
);
2626 if ((nfsvers
== NFS_VER2
) && ((uint64_t)(txoffset
+ tsiz
) > 0xffffffffULL
)) {
2627 FSDBG_BOT(536, np
, uio_offset(uio
), uio_resid(uio
), EFBIG
);
2632 len
= retlen
= (tsiz
> (user_ssize_t
)nmrsize
) ? nmrsize
: (size_t)tsiz
;
2633 FSDBG(536, np
, txoffset
, len
, 0);
2634 if (np
->n_flag
& NREVOKE
) {
2638 if (nmp
->nm_vers
>= NFS_VER4
)
2639 stategenid
= nmp
->nm_stategenid
;
2640 error
= nmp
->nm_funcs
->nf_read_rpc_async(np
, txoffset
, len
,
2641 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, &req
);
2643 error
= nmp
->nm_funcs
->nf_read_rpc_async_finish(np
, req
, uio
, &retlen
, &eof
);
2644 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
) &&
2645 (++restart
<= nfs_mount_state_max_restarts(nmp
))) { /* guard against no progress */
2646 lck_mtx_lock(&nmp
->nm_lock
);
2647 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
2648 NP(np
, "nfs_read_rpc: error %d, initiating recovery", error
);
2649 nfs_need_recover(nmp
, error
);
2651 lck_mtx_unlock(&nmp
->nm_lock
);
2652 if (np
->n_flag
& NREVOKE
) {
2655 if (error
== NFSERR_GRACE
)
2656 tsleep(&nmp
->nm_state
, (PZERO
-1), "nfsgrace", 2*hz
);
2657 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
)))
2665 if (nfsvers
!= NFS_VER2
) {
2666 if (eof
|| (retlen
== 0))
2668 } else if (retlen
< len
)
2672 FSDBG_BOT(536, np
, eof
, uio_resid(uio
), error
);
2677 nfs3_read_rpc_async(
2683 struct nfsreq_cbinfo
*cb
,
2684 struct nfsreq
**reqp
)
2686 struct nfsmount
*nmp
;
2687 int error
= 0, nfsvers
;
2688 struct nfsm_chain nmreq
;
2691 if (nfs_mount_gone(nmp
))
2693 nfsvers
= nmp
->nm_vers
;
2695 nfsm_chain_null(&nmreq
);
2696 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nfsvers
) + 3 * NFSX_UNSIGNED
);
2697 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
2698 if (nfsvers
== NFS_VER3
) {
2699 nfsm_chain_add_64(error
, &nmreq
, offset
);
2700 nfsm_chain_add_32(error
, &nmreq
, len
);
2702 nfsm_chain_add_32(error
, &nmreq
, offset
);
2703 nfsm_chain_add_32(error
, &nmreq
, len
);
2704 nfsm_chain_add_32(error
, &nmreq
, 0);
2706 nfsm_chain_build_done(error
, &nmreq
);
2708 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC_READ
, thd
, cred
, NULL
, 0, cb
, reqp
);
2710 nfsm_chain_cleanup(&nmreq
);
2715 nfs3_read_rpc_async_finish(
2722 int error
= 0, lockerror
, nfsvers
, status
, eof
= 0;
2725 struct nfsmount
*nmp
;
2726 struct nfsm_chain nmrep
;
2729 if (nfs_mount_gone(nmp
)) {
2730 nfs_request_async_cancel(req
);
2733 nfsvers
= nmp
->nm_vers
;
2735 nfsm_chain_null(&nmrep
);
2737 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
2738 if (error
== EINPROGRESS
) /* async request restarted */
2741 if ((lockerror
= nfs_node_lock(np
)))
2743 if (nfsvers
== NFS_VER3
)
2744 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
2747 if (nfsvers
== NFS_VER3
) {
2748 nfsm_chain_adv(error
, &nmrep
, NFSX_UNSIGNED
);
2749 nfsm_chain_get_32(error
, &nmrep
, eof
);
2751 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
2754 nfs_node_unlock(np
);
2755 nfsm_chain_get_32(error
, &nmrep
, retlen
);
2756 if ((nfsvers
== NFS_VER2
) && (retlen
> *lenp
))
2759 error
= nfsm_chain_get_uio(&nmrep
, MIN(retlen
, *lenp
), uio
);
2761 if (nfsvers
== NFS_VER3
) {
2762 if (!eof
&& !retlen
)
2764 } else if (retlen
< *lenp
) {
2769 *lenp
= MIN(retlen
, *lenp
);
2771 nfsm_chain_cleanup(&nmrep
);
2780 struct vnop_write_args
/* {
2781 struct vnodeop_desc *a_desc;
2785 vfs_context_t a_context;
2788 vfs_context_t ctx
= ap
->a_context
;
2789 uio_t uio
= ap
->a_uio
;
2790 vnode_t vp
= ap
->a_vp
;
2791 nfsnode_t np
= VTONFS(vp
);
2792 int ioflag
= ap
->a_ioflag
;
2794 struct nfsmount
*nmp
= VTONMP(vp
);
2797 int n
, on
, error
= 0;
2798 off_t boff
, start
, end
;
2800 char auio_buf
[ UIO_SIZEOF(1) ];
2804 FSDBG_TOP(515, np
, uio_offset(uio
), uio_resid(uio
), ioflag
);
2806 if (vnode_vtype(vp
) != VREG
) {
2807 FSDBG_BOT(515, np
, uio_offset(uio
), uio_resid(uio
), EIO
);
2811 thd
= vfs_context_thread(ctx
);
2812 cred
= vfs_context_ucred(ctx
);
2814 nfs_data_lock(np
, NFS_DATA_LOCK_SHARED
);
2816 if ((error
= nfs_node_lock(np
))) {
2817 nfs_data_unlock(np
);
2818 FSDBG_BOT(515, np
, uio_offset(uio
), uio_resid(uio
), error
);
2823 if (np
->n_flag
& NWRITEERR
) {
2824 error
= np
->n_error
;
2825 np
->n_flag
&= ~NWRITEERR
;
2827 if (np
->n_flag
& NNEEDINVALIDATE
) {
2828 np
->n_flag
&= ~NNEEDINVALIDATE
;
2829 nfs_node_unlock(np
);
2830 nfs_data_unlock(np
);
2831 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, ctx
, 1);
2832 nfs_data_lock(np
, NFS_DATA_LOCK_SHARED
);
2834 nfs_node_unlock(np
);
2839 biosize
= nmp
->nm_biosize
;
2841 if (ioflag
& (IO_APPEND
| IO_SYNC
)) {
2842 nfs_node_lock_force(np
);
2843 if (np
->n_flag
& NMODIFIED
) {
2844 NATTRINVALIDATE(np
);
2845 nfs_node_unlock(np
);
2846 nfs_data_unlock(np
);
2847 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
2848 nfs_data_lock(np
, NFS_DATA_LOCK_SHARED
);
2850 FSDBG(515, np
, uio_offset(uio
), 0x10bad01, error
);
2854 nfs_node_unlock(np
);
2856 if (ioflag
& IO_APPEND
) {
2857 nfs_data_unlock(np
);
2858 /* nfs_getattr() will check changed and purge caches */
2859 error
= nfs_getattr(np
, NULL
, ctx
, NGA_UNCACHED
);
2860 /* we'll be extending the file, so take the data lock exclusive */
2861 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
2863 FSDBG(515, np
, uio_offset(uio
), 0x10bad02, error
);
2866 uio_setoffset(uio
, np
->n_size
);
2869 if (uio_offset(uio
) < 0) {
2871 FSDBG_BOT(515, np
, uio_offset(uio
), 0xbad0ff, error
);
2874 if (uio_resid(uio
) == 0)
2877 if (((uio_offset(uio
) + uio_resid(uio
)) > (off_t
)np
->n_size
) && !(ioflag
& IO_APPEND
)) {
2879 * It looks like we'll be extending the file, so take the data lock exclusive.
2881 nfs_data_unlock(np
);
2882 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
2885 * Also, if the write begins after the previous EOF buffer, make sure to zero
2886 * and validate the new bytes in that buffer.
2888 struct nfsbuf
*eofbp
= NULL
;
2889 daddr64_t eofbn
= np
->n_size
/ biosize
;
2890 int eofoff
= np
->n_size
% biosize
;
2891 lbn
= uio_offset(uio
) / biosize
;
2893 if (eofoff
&& (eofbn
< lbn
)) {
2894 if ((error
= nfs_buf_get(np
, eofbn
, biosize
, thd
, NBLK_WRITE
|NBLK_ONLYVALID
, &eofbp
)))
2896 np
->n_size
+= (biosize
- eofoff
);
2897 nfs_node_lock_force(np
);
2898 CLR(np
->n_flag
, NUPDATESIZE
);
2899 np
->n_flag
|= NMODIFIED
;
2900 nfs_node_unlock(np
);
2901 FSDBG(516, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0001);
2902 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX errors */
2905 * For the old last page, don't zero bytes if there
2906 * are invalid bytes in that page (i.e. the page isn't
2908 * For pages after the old last page, zero them and
2909 * mark them as valid.
2913 if (ioflag
& IO_NOCACHE
)
2914 SET(eofbp
->nb_flags
, NB_NOCACHE
);
2916 FSDBG(516, eofbp
, eofoff
, biosize
- eofoff
, 0xe0fff01e);
2918 i
= eofoff
/PAGE_SIZE
;
2919 while (eofoff
< biosize
) {
2920 int poff
= eofoff
& PAGE_MASK
;
2921 if (!poff
|| NBPGVALID(eofbp
,i
)) {
2922 bzero(d
+ eofoff
, PAGE_SIZE
- poff
);
2923 NBPGVALID_SET(eofbp
, i
);
2925 eofoff
+= PAGE_SIZE
- poff
;
2928 nfs_buf_release(eofbp
, 1);
2934 OSAddAtomic64(1, &nfsstats
.biocache_writes
);
2935 lbn
= uio_offset(uio
) / biosize
;
2936 on
= uio_offset(uio
) % biosize
;
2938 if (uio_resid(uio
) < n
)
2942 * Get a cache block for writing. The range to be written is
2943 * (off..off+n) within the block. We ensure that the block
2944 * either has no dirty region or that the given range is
2945 * contiguous with the existing dirty region.
2947 error
= nfs_buf_get(np
, lbn
, biosize
, thd
, NBLK_WRITE
, &bp
);
2950 /* map the block because we know we're going to write to it */
2953 if (ioflag
& IO_NOCACHE
)
2954 SET(bp
->nb_flags
, NB_NOCACHE
);
2956 if (!IS_VALID_CRED(bp
->nb_wcred
)) {
2957 kauth_cred_ref(cred
);
2958 bp
->nb_wcred
= cred
;
2962 * If there's already a dirty range AND dirty pages in this block we
2963 * need to send a commit AND write the dirty pages before continuing.
2965 * If there's already a dirty range OR dirty pages in this block
2966 * and the new write range is not contiguous with the existing range,
2967 * then force the buffer to be written out now.
2968 * (We used to just extend the dirty range to cover the valid,
2969 * but unwritten, data in between also. But writing ranges
2970 * of data that weren't actually written by an application
2971 * risks overwriting some other client's data with stale data
2972 * that's just masquerading as new written data.)
2974 if (bp
->nb_dirtyend
> 0) {
2975 if (on
> bp
->nb_dirtyend
|| (on
+ n
) < bp
->nb_dirtyoff
|| bp
->nb_dirty
) {
2976 FSDBG(515, np
, uio_offset(uio
), bp
, 0xd15c001);
2977 /* write/commit buffer "synchronously" */
2978 /* (NB_STABLE indicates that data writes should be FILESYNC) */
2979 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
));
2980 SET(bp
->nb_flags
, (NB_ASYNC
| NB_STABLE
));
2981 error
= nfs_buf_write(bp
);
2986 } else if (bp
->nb_dirty
) {
2987 int firstpg
, lastpg
;
2989 /* calculate write range pagemask */
2990 firstpg
= on
/PAGE_SIZE
;
2991 lastpg
= (on
+n
-1)/PAGE_SIZE
;
2992 pagemask
= ((1 << (lastpg
+1)) - 1) & ~((1 << firstpg
) - 1);
2993 /* check if there are dirty pages outside the write range */
2994 if (bp
->nb_dirty
& ~pagemask
) {
2995 FSDBG(515, np
, uio_offset(uio
), bp
, 0xd15c002);
2996 /* write/commit buffer "synchronously" */
2997 /* (NB_STABLE indicates that data writes should be FILESYNC) */
2998 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
));
2999 SET(bp
->nb_flags
, (NB_ASYNC
| NB_STABLE
));
3000 error
= nfs_buf_write(bp
);
3005 /* if the first or last pages are already dirty */
3006 /* make sure that the dirty range encompasses those pages */
3007 if (NBPGDIRTY(bp
,firstpg
) || NBPGDIRTY(bp
,lastpg
)) {
3008 FSDBG(515, np
, uio_offset(uio
), bp
, 0xd15c003);
3009 bp
->nb_dirtyoff
= min(on
, firstpg
* PAGE_SIZE
);
3010 if (NBPGDIRTY(bp
,lastpg
)) {
3011 bp
->nb_dirtyend
= (lastpg
+1) * PAGE_SIZE
;
3013 if (NBOFF(bp
) + bp
->nb_dirtyend
> (off_t
)np
->n_size
) {
3014 bp
->nb_dirtyend
= np
->n_size
- NBOFF(bp
);
3015 if (bp
->nb_dirtyoff
>= bp
->nb_dirtyend
)
3016 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
3019 bp
->nb_dirtyend
= on
+n
;
3024 * Are we extending the size of the file with this write?
3025 * If so, update file size now that we have the block.
3026 * If there was a partial buf at the old eof, validate
3027 * and zero the new bytes.
3029 if ((uio_offset(uio
) + n
) > (off_t
)np
->n_size
) {
3030 daddr64_t eofbn
= np
->n_size
/ biosize
;
3031 int neweofoff
= (uio_offset(uio
) + n
) % biosize
;
3033 FSDBG(515, 0xb1ffa000, uio_offset(uio
) + n
, eofoff
, neweofoff
);
3035 /* if we're extending within the same last block */
3036 /* and the block is flagged as being cached... */
3037 if ((lbn
== eofbn
) && ISSET(bp
->nb_flags
, NB_CACHE
)) {
3038 /* ...check that all pages in buffer are valid */
3039 int endpg
= ((neweofoff
? neweofoff
: biosize
) - 1)/PAGE_SIZE
;
3041 /* pagemask only has to extend to last page being written to */
3042 pagemask
= (1 << (endpg
+1)) - 1;
3043 FSDBG(515, 0xb1ffa001, bp
->nb_valid
, pagemask
, 0);
3044 if ((bp
->nb_valid
& pagemask
) != pagemask
) {
3045 /* zerofill any hole */
3046 if (on
> bp
->nb_validend
) {
3048 for (i
=bp
->nb_validend
/PAGE_SIZE
; i
<= (on
- 1)/PAGE_SIZE
; i
++)
3049 NBPGVALID_SET(bp
, i
);
3051 FSDBG(516, bp
, bp
->nb_validend
, on
- bp
->nb_validend
, 0xf01e);
3052 bzero((char *)bp
->nb_data
+ bp
->nb_validend
,
3053 on
- bp
->nb_validend
);
3055 /* zerofill any trailing data in the last page */
3058 FSDBG(516, bp
, neweofoff
, PAGE_SIZE
- (neweofoff
& PAGE_MASK
), 0xe0f);
3059 bzero((char *)bp
->nb_data
+ neweofoff
,
3060 PAGE_SIZE
- (neweofoff
& PAGE_MASK
));
3064 np
->n_size
= uio_offset(uio
) + n
;
3065 nfs_node_lock_force(np
);
3066 CLR(np
->n_flag
, NUPDATESIZE
);
3067 np
->n_flag
|= NMODIFIED
;
3068 nfs_node_unlock(np
);
3069 FSDBG(516, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0001);
3070 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX errors */
3073 * If dirtyend exceeds file size, chop it down. This should
3074 * not occur unless there is a race.
3076 if (NBOFF(bp
) + bp
->nb_dirtyend
> (off_t
)np
->n_size
) {
3077 bp
->nb_dirtyend
= np
->n_size
- NBOFF(bp
);
3078 if (bp
->nb_dirtyoff
>= bp
->nb_dirtyend
)
3079 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
3082 * UBC doesn't handle partial pages, so we need to make sure
3083 * that any pages left in the page cache are completely valid.
3085 * Writes that are smaller than a block are delayed if they
3086 * don't extend to the end of the block.
3088 * If the block isn't (completely) cached, we may need to read
3089 * in some parts of pages that aren't covered by the write.
3090 * If the write offset (on) isn't page aligned, we'll need to
3091 * read the start of the first page being written to. Likewise,
3092 * if the offset of the end of the write (on+n) isn't page aligned,
3093 * we'll need to read the end of the last page being written to.
3096 * We don't want to read anything we're just going to write over.
3097 * We don't want to read anything we're just going drop when the
3098 * I/O is complete (i.e. don't do reads for NOCACHE requests).
3099 * We don't want to issue multiple I/Os if we don't have to
3100 * (because they're synchronous rpcs).
3101 * We don't want to read anything we already have modified in the
3104 if (!ISSET(bp
->nb_flags
, NB_CACHE
) && (n
< biosize
)) {
3105 int firstpg
, lastpg
, dirtypg
;
3106 int firstpgoff
, lastpgoff
;
3108 firstpg
= on
/PAGE_SIZE
;
3109 firstpgoff
= on
& PAGE_MASK
;
3110 lastpg
= (on
+n
-1)/PAGE_SIZE
;
3111 lastpgoff
= (on
+n
) & PAGE_MASK
;
3112 if (firstpgoff
&& !NBPGVALID(bp
,firstpg
)) {
3113 /* need to read start of first page */
3114 start
= firstpg
* PAGE_SIZE
;
3115 end
= start
+ firstpgoff
;
3117 if (lastpgoff
&& !NBPGVALID(bp
,lastpg
)) {
3118 /* need to read end of last page */
3120 start
= (lastpg
* PAGE_SIZE
) + lastpgoff
;
3121 end
= (lastpg
+ 1) * PAGE_SIZE
;
3123 if (ISSET(bp
->nb_flags
, NB_NOCACHE
)) {
3125 * For nocache writes, if there is any partial page at the
3126 * start or end of the write range, then we do the write
3127 * synchronously to make sure that we can drop the data
3128 * from the cache as soon as the WRITE finishes. Normally,
3129 * we would do an unstable write and not drop the data until
3130 * it was committed. But doing that here would risk allowing
3131 * invalid data to be read from the cache between the WRITE
3133 * (NB_STABLE indicates that data writes should be FILESYNC)
3136 SET(bp
->nb_flags
, NB_STABLE
);
3140 /* need to read the data in range: start...end-1 */
3142 /* first, check for dirty pages in between */
3143 /* if there are, we'll have to do two reads because */
3144 /* we don't want to overwrite the dirty pages. */
3145 for (dirtypg
=start
/PAGE_SIZE
; dirtypg
<= (end
-1)/PAGE_SIZE
; dirtypg
++)
3146 if (NBPGDIRTY(bp
,dirtypg
))
3149 /* if start is at beginning of page, try */
3150 /* to get any preceeding pages as well. */
3151 if (!(start
& PAGE_MASK
)) {
3152 /* stop at next dirty/valid page or start of block */
3153 for (; start
> 0; start
-=PAGE_SIZE
)
3154 if (NBPGVALID(bp
,((start
-1)/PAGE_SIZE
)))
3159 /* setup uio for read(s) */
3161 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
3162 &auio_buf
, sizeof(auio_buf
));
3164 if (dirtypg
<= (end
-1)/PAGE_SIZE
) {
3165 /* there's a dirty page in the way, so just do two reads */
3166 /* we'll read the preceding data here */
3167 uio_reset(auio
, boff
+ start
, UIO_SYSSPACE
, UIO_READ
);
3168 uio_addiov(auio
, CAST_USER_ADDR_T(bp
->nb_data
+ start
), on
- start
);
3169 error
= nfs_read_rpc(np
, auio
, ctx
);
3171 /* couldn't read the data, so treat buffer as synchronous NOCACHE */
3172 SET(bp
->nb_flags
, (NB_NOCACHE
|NB_STABLE
));
3175 if (uio_resid(auio
) > 0) {
3176 FSDBG(516, bp
, (caddr_t
)uio_curriovbase(auio
) - bp
->nb_data
, uio_resid(auio
), 0xd00dee01);
3177 bzero(CAST_DOWN(caddr_t
, uio_curriovbase(auio
)), uio_resid(auio
));
3180 /* update validoff/validend if necessary */
3181 if ((bp
->nb_validoff
< 0) || (bp
->nb_validoff
> start
))
3182 bp
->nb_validoff
= start
;
3183 if ((bp
->nb_validend
< 0) || (bp
->nb_validend
< on
))
3184 bp
->nb_validend
= on
;
3185 if ((off_t
)np
->n_size
> boff
+ bp
->nb_validend
)
3186 bp
->nb_validend
= min(np
->n_size
- (boff
+ start
), biosize
);
3187 /* validate any pages before the write offset */
3188 for (; start
< on
/PAGE_SIZE
; start
+=PAGE_SIZE
)
3189 NBPGVALID_SET(bp
, start
/PAGE_SIZE
);
3191 /* adjust start to read any trailing data */
3195 /* if end is at end of page, try to */
3196 /* get any following pages as well. */
3197 if (!(end
& PAGE_MASK
)) {
3198 /* stop at next valid page or end of block */
3199 for (; end
< biosize
; end
+=PAGE_SIZE
)
3200 if (NBPGVALID(bp
,end
/PAGE_SIZE
))
3204 if (((boff
+start
) >= (off_t
)np
->n_size
) ||
3205 ((start
>= on
) && ((boff
+ on
+ n
) >= (off_t
)np
->n_size
))) {
3207 * Either this entire read is beyond the current EOF
3208 * or the range that we won't be modifying (on+n...end)
3209 * is all beyond the current EOF.
3210 * No need to make a trip across the network to
3211 * read nothing. So, just zero the buffer instead.
3213 FSDBG(516, bp
, start
, end
- start
, 0xd00dee00);
3214 bzero(bp
->nb_data
+ start
, end
- start
);
3217 /* now we'll read the (rest of the) data */
3218 uio_reset(auio
, boff
+ start
, UIO_SYSSPACE
, UIO_READ
);
3219 uio_addiov(auio
, CAST_USER_ADDR_T(bp
->nb_data
+ start
), end
- start
);
3220 error
= nfs_read_rpc(np
, auio
, ctx
);
3222 /* couldn't read the data, so treat buffer as synchronous NOCACHE */
3223 SET(bp
->nb_flags
, (NB_NOCACHE
|NB_STABLE
));
3226 if (uio_resid(auio
) > 0) {
3227 FSDBG(516, bp
, (caddr_t
)uio_curriovbase(auio
) - bp
->nb_data
, uio_resid(auio
), 0xd00dee02);
3228 bzero(CAST_DOWN(caddr_t
, uio_curriovbase(auio
)), uio_resid(auio
));
3232 /* update validoff/validend if necessary */
3233 if ((bp
->nb_validoff
< 0) || (bp
->nb_validoff
> start
))
3234 bp
->nb_validoff
= start
;
3235 if ((bp
->nb_validend
< 0) || (bp
->nb_validend
< end
))
3236 bp
->nb_validend
= end
;
3237 if ((off_t
)np
->n_size
> boff
+ bp
->nb_validend
)
3238 bp
->nb_validend
= min(np
->n_size
- (boff
+ start
), biosize
);
3239 /* validate any pages before the write offset's page */
3240 for (; start
< (off_t
)trunc_page_32(on
); start
+=PAGE_SIZE
)
3241 NBPGVALID_SET(bp
, start
/PAGE_SIZE
);
3242 /* validate any pages after the range of pages being written to */
3243 for (; (end
- 1) > (off_t
)round_page_32(on
+n
-1); end
-=PAGE_SIZE
)
3244 NBPGVALID_SET(bp
, (end
-1)/PAGE_SIZE
);
3246 /* Note: pages being written to will be validated when written */
3251 if (ISSET(bp
->nb_flags
, NB_ERROR
)) {
3252 error
= bp
->nb_error
;
3253 nfs_buf_release(bp
, 1);
3257 nfs_node_lock_force(np
);
3258 np
->n_flag
|= NMODIFIED
;
3259 nfs_node_unlock(np
);
3262 error
= uiomove((char *)bp
->nb_data
+ on
, n
, uio
);
3264 SET(bp
->nb_flags
, NB_ERROR
);
3265 nfs_buf_release(bp
, 1);
3269 /* validate any pages written to */
3270 start
= on
& ~PAGE_MASK
;
3271 for (; start
< on
+n
; start
+= PAGE_SIZE
) {
3272 NBPGVALID_SET(bp
, start
/PAGE_SIZE
);
3274 * This may seem a little weird, but we don't actually set the
3275 * dirty bits for writes. This is because we keep the dirty range
3276 * in the nb_dirtyoff/nb_dirtyend fields. Also, particularly for
3277 * delayed writes, when we give the pages back to the VM we don't
3278 * want to keep them marked dirty, because when we later write the
3279 * buffer we won't be able to tell which pages were written dirty
3280 * and which pages were mmapped and dirtied.
3283 if (bp
->nb_dirtyend
> 0) {
3284 bp
->nb_dirtyoff
= min(on
, bp
->nb_dirtyoff
);
3285 bp
->nb_dirtyend
= max((on
+ n
), bp
->nb_dirtyend
);
3287 bp
->nb_dirtyoff
= on
;
3288 bp
->nb_dirtyend
= on
+ n
;
3290 if (bp
->nb_validend
<= 0 || bp
->nb_validend
< bp
->nb_dirtyoff
||
3291 bp
->nb_validoff
> bp
->nb_dirtyend
) {
3292 bp
->nb_validoff
= bp
->nb_dirtyoff
;
3293 bp
->nb_validend
= bp
->nb_dirtyend
;
3295 bp
->nb_validoff
= min(bp
->nb_validoff
, bp
->nb_dirtyoff
);
3296 bp
->nb_validend
= max(bp
->nb_validend
, bp
->nb_dirtyend
);
3298 if (!ISSET(bp
->nb_flags
, NB_CACHE
))
3299 nfs_buf_normalize_valid_range(np
, bp
);
3302 * Since this block is being modified, it must be written
3303 * again and not just committed.
3305 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
3306 nfs_node_lock_force(np
);
3307 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
3308 np
->n_needcommitcnt
--;
3309 CHECK_NEEDCOMMITCNT(np
);
3311 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
3312 nfs_node_unlock(np
);
3315 if (ioflag
& IO_SYNC
) {
3316 error
= nfs_buf_write(bp
);
3319 } else if (((n
+ on
) == biosize
) || (ioflag
& IO_APPEND
) ||
3320 (ioflag
& IO_NOCACHE
) || ISSET(bp
->nb_flags
, NB_NOCACHE
)) {
3321 SET(bp
->nb_flags
, NB_ASYNC
);
3322 error
= nfs_buf_write(bp
);
3326 /* If the block wasn't already delayed: charge for the write */
3327 if (!ISSET(bp
->nb_flags
, NB_DELWRI
)) {
3328 proc_t p
= vfs_context_proc(ctx
);
3329 if (p
&& p
->p_stats
)
3330 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_oublock
);
3332 nfs_buf_write_delayed(bp
);
3334 if (np
->n_needcommitcnt
>= NFS_A_LOT_OF_NEEDCOMMITS
)
3335 nfs_flushcommits(np
, 1);
3337 } while (uio_resid(uio
) > 0 && n
> 0);
3340 nfs_node_lock_force(np
);
3342 nfs_node_unlock(np
);
3343 nfs_data_unlock(np
);
3344 FSDBG_BOT(515, np
, uio_offset(uio
), uio_resid(uio
), error
);
3360 return nfs_write_rpc2(np
, uio
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
), iomodep
, wverfp
);
3372 struct nfsmount
*nmp
;
3373 int error
= 0, nfsvers
;
3374 int wverfset
, commit
, committed
;
3375 uint64_t wverf
= 0, wverf2
;
3376 size_t nmwsize
, totalsize
, tsiz
, len
, rlen
;
3377 struct nfsreq rq
, *req
= &rq
;
3378 uint32_t stategenid
= 0, vrestart
= 0, restart
= 0;
3379 uio_t uio_save
= NULL
;
3382 /* XXX limitation based on need to back up uio on short write */
3383 if (uio_iovcnt(uio
) != 1)
3384 panic("nfs3_write_rpc: iovcnt > 1");
3386 FSDBG_TOP(537, np
, uio_offset(uio
), uio_resid(uio
), *iomodep
);
3388 if (nfs_mount_gone(nmp
))
3390 nfsvers
= nmp
->nm_vers
;
3391 nmwsize
= nmp
->nm_wsize
;
3394 committed
= NFS_WRITE_FILESYNC
;
3396 totalsize
= tsiz
= uio_resid(uio
);
3397 if ((nfsvers
== NFS_VER2
) && ((uint64_t)(uio_offset(uio
) + tsiz
) > 0xffffffffULL
)) {
3398 FSDBG_BOT(537, np
, uio_offset(uio
), uio_resid(uio
), EFBIG
);
3402 uio_save
= uio_duplicate(uio
);
3403 if (uio_save
== NULL
) {
3408 len
= (tsiz
> nmwsize
) ? nmwsize
: tsiz
;
3409 FSDBG(537, np
, uio_offset(uio
), len
, 0);
3410 if (np
->n_flag
& NREVOKE
) {
3414 if (nmp
->nm_vers
>= NFS_VER4
)
3415 stategenid
= nmp
->nm_stategenid
;
3416 error
= nmp
->nm_funcs
->nf_write_rpc_async(np
, uio
, len
, thd
, cred
, *iomodep
, NULL
, &req
);
3418 error
= nmp
->nm_funcs
->nf_write_rpc_async_finish(np
, req
, &commit
, &rlen
, &wverf2
);
3420 if (nfs_mount_gone(nmp
))
3422 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
) &&
3423 (++restart
<= nfs_mount_state_max_restarts(nmp
))) { /* guard against no progress */
3424 lck_mtx_lock(&nmp
->nm_lock
);
3425 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
3426 NP(np
, "nfs_write_rpc: error %d, initiating recovery", error
);
3427 nfs_need_recover(nmp
, error
);
3429 lck_mtx_unlock(&nmp
->nm_lock
);
3430 if (np
->n_flag
& NREVOKE
) {
3433 if (error
== NFSERR_GRACE
)
3434 tsleep(&nmp
->nm_state
, (PZERO
-1), "nfsgrace", 2*hz
);
3435 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
)))
3441 if (nfsvers
== NFS_VER2
) {
3446 /* check for a short write */
3448 /* Reset the uio to reflect the actual transfer */
3450 uio_update(uio
, totalsize
- (tsiz
- rlen
));
3454 /* return lowest commit level returned */
3455 if (commit
< committed
)
3460 /* check write verifier */
3464 } else if (wverf
!= wverf2
) {
3465 /* verifier changed, so we need to restart all the writes */
3466 if (++vrestart
> 100) {
3467 /* give up after too many restarts */
3471 *uio
= *uio_save
; // Reset the uio back to the start
3472 committed
= NFS_WRITE_FILESYNC
;
3479 if (wverfset
&& wverfp
)
3481 *iomodep
= committed
;
3483 uio_setresid(uio
, tsiz
);
3484 FSDBG_BOT(537, np
, committed
, uio_resid(uio
), error
);
3489 nfs3_write_rpc_async(
3496 struct nfsreq_cbinfo
*cb
,
3497 struct nfsreq
**reqp
)
3499 struct nfsmount
*nmp
;
3501 int error
= 0, nfsvers
;
3502 struct nfsm_chain nmreq
;
3505 if (nfs_mount_gone(nmp
))
3507 nfsvers
= nmp
->nm_vers
;
3509 /* for async mounts, don't bother sending sync write requests */
3510 if ((iomode
!= NFS_WRITE_UNSTABLE
) && nfs_allow_async
&&
3511 ((mp
= NFSTOMP(np
))) && (vfs_flags(mp
) & MNT_ASYNC
))
3512 iomode
= NFS_WRITE_UNSTABLE
;
3514 nfsm_chain_null(&nmreq
);
3515 nfsm_chain_build_alloc_init(error
, &nmreq
,
3516 NFSX_FH(nfsvers
) + 5 * NFSX_UNSIGNED
+ nfsm_rndup(len
));
3517 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
3518 if (nfsvers
== NFS_VER3
) {
3519 nfsm_chain_add_64(error
, &nmreq
, uio_offset(uio
));
3520 nfsm_chain_add_32(error
, &nmreq
, len
);
3521 nfsm_chain_add_32(error
, &nmreq
, iomode
);
3523 nfsm_chain_add_32(error
, &nmreq
, 0);
3524 nfsm_chain_add_32(error
, &nmreq
, uio_offset(uio
));
3525 nfsm_chain_add_32(error
, &nmreq
, 0);
3527 nfsm_chain_add_32(error
, &nmreq
, len
);
3529 error
= nfsm_chain_add_uio(&nmreq
, uio
, len
);
3530 nfsm_chain_build_done(error
, &nmreq
);
3532 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC_WRITE
, thd
, cred
, NULL
, 0, cb
, reqp
);
3534 nfsm_chain_cleanup(&nmreq
);
3539 nfs3_write_rpc_async_finish(
3546 struct nfsmount
*nmp
;
3547 int error
= 0, lockerror
= ENOENT
, nfsvers
, status
;
3548 int updatemtime
= 0, wccpostattr
= 0, rlen
, committed
= NFS_WRITE_FILESYNC
;
3549 u_int64_t xid
, wverf
;
3551 struct nfsm_chain nmrep
;
3554 if (nfs_mount_gone(nmp
)) {
3555 nfs_request_async_cancel(req
);
3558 nfsvers
= nmp
->nm_vers
;
3560 nfsm_chain_null(&nmrep
);
3562 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
3563 if (error
== EINPROGRESS
) /* async request restarted */
3566 if (nfs_mount_gone(nmp
))
3568 if (!error
&& (lockerror
= nfs_node_lock(np
)))
3570 if (nfsvers
== NFS_VER3
) {
3571 struct timespec premtime
= { 0, 0 };
3572 nfsm_chain_get_wcc_data(error
, &nmrep
, np
, &premtime
, &wccpostattr
, &xid
);
3573 if (nfstimespeccmp(&np
->n_mtime
, &premtime
, ==))
3577 nfsm_chain_get_32(error
, &nmrep
, rlen
);
3582 nfsm_chain_get_32(error
, &nmrep
, committed
);
3583 nfsm_chain_get_64(error
, &nmrep
, wverf
);
3587 lck_mtx_lock(&nmp
->nm_lock
);
3588 if (!(nmp
->nm_state
& NFSSTA_HASWRITEVERF
)) {
3589 nmp
->nm_verf
= wverf
;
3590 nmp
->nm_state
|= NFSSTA_HASWRITEVERF
;
3591 } else if (nmp
->nm_verf
!= wverf
) {
3592 nmp
->nm_verf
= wverf
;
3594 lck_mtx_unlock(&nmp
->nm_lock
);
3598 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
3602 NFS_CHANGED_UPDATE(nfsvers
, np
, &np
->n_vattr
);
3605 nfs_node_unlock(np
);
3606 nfsm_chain_cleanup(&nmrep
);
3607 if ((committed
!= NFS_WRITE_FILESYNC
) && nfs_allow_async
&&
3608 ((mp
= NFSTOMP(np
))) && (vfs_flags(mp
) & MNT_ASYNC
))
3609 committed
= NFS_WRITE_FILESYNC
;
3610 *iomodep
= committed
;
3615 * NFS mknod vnode op
3617 * For NFS v2 this is a kludge. Use a create RPC but with the IFMT bits of the
3618 * mode set to specify the file type and the size field for rdev.
3622 struct vnop_mknod_args
/* {
3623 struct vnodeop_desc *a_desc;
3626 struct componentname *a_cnp;
3627 struct vnode_attr *a_vap;
3628 vfs_context_t a_context;
3631 vnode_t dvp
= ap
->a_dvp
;
3632 vnode_t
*vpp
= ap
->a_vpp
;
3633 struct componentname
*cnp
= ap
->a_cnp
;
3634 struct vnode_attr
*vap
= ap
->a_vap
;
3635 vfs_context_t ctx
= ap
->a_context
;
3636 vnode_t newvp
= NULL
;
3637 nfsnode_t np
= NULL
;
3638 struct nfsmount
*nmp
;
3639 nfsnode_t dnp
= VTONFS(dvp
);
3640 struct nfs_vattr nvattr
;
3642 int error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0;
3643 struct timespec premtime
= { 0, 0 };
3645 u_int64_t xid
= 0, dxid
;
3646 int nfsvers
, gotuid
, gotgid
;
3647 struct nfsm_chain nmreq
, nmrep
;
3648 struct nfsreq rq
, *req
= &rq
;
3651 if (nfs_mount_gone(nmp
))
3653 nfsvers
= nmp
->nm_vers
;
3655 if (!VATTR_IS_ACTIVE(vap
, va_type
))
3657 if (vap
->va_type
== VCHR
|| vap
->va_type
== VBLK
) {
3658 if (!VATTR_IS_ACTIVE(vap
, va_rdev
))
3660 rdev
= vap
->va_rdev
;
3661 } else if (vap
->va_type
== VFIFO
|| vap
->va_type
== VSOCK
)
3666 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
))
3667 return (ENAMETOOLONG
);
3669 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
3671 VATTR_SET_SUPPORTED(vap
, va_mode
);
3672 VATTR_SET_SUPPORTED(vap
, va_uid
);
3673 VATTR_SET_SUPPORTED(vap
, va_gid
);
3674 VATTR_SET_SUPPORTED(vap
, va_data_size
);
3675 VATTR_SET_SUPPORTED(vap
, va_access_time
);
3676 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
3677 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
3678 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
3680 nfsm_chain_null(&nmreq
);
3681 nfsm_chain_null(&nmrep
);
3683 nfsm_chain_build_alloc_init(error
, &nmreq
,
3684 NFSX_FH(nfsvers
) + 4 * NFSX_UNSIGNED
+
3685 nfsm_rndup(cnp
->cn_namelen
) + NFSX_SATTR(nfsvers
));
3686 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
3687 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
3688 if (nfsvers
== NFS_VER3
) {
3689 nfsm_chain_add_32(error
, &nmreq
, vtonfs_type(vap
->va_type
, nfsvers
));
3690 nfsm_chain_add_v3sattr(error
, &nmreq
, vap
);
3691 if (vap
->va_type
== VCHR
|| vap
->va_type
== VBLK
) {
3692 nfsm_chain_add_32(error
, &nmreq
, major(vap
->va_rdev
));
3693 nfsm_chain_add_32(error
, &nmreq
, minor(vap
->va_rdev
));
3696 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, rdev
);
3698 nfsm_chain_build_done(error
, &nmreq
);
3700 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
3703 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_MKNOD
,
3704 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
3706 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
3708 if ((lockerror
= nfs_node_lock(dnp
)))
3710 /* XXX no EEXIST kludge here? */
3712 if (!error
&& !status
) {
3713 if (dnp
->n_flag
& NNEGNCENTRIES
) {
3714 dnp
->n_flag
&= ~NNEGNCENTRIES
;
3715 cache_purge_negatives(dvp
);
3717 error
= nfsm_chain_get_fh_attr(&nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
3719 if (nfsvers
== NFS_VER3
)
3720 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
3724 nfsm_chain_cleanup(&nmreq
);
3725 nfsm_chain_cleanup(&nmrep
);
3728 dnp
->n_flag
|= NMODIFIED
;
3729 /* if directory hadn't changed, update namecache mtime */
3730 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==))
3731 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
3732 nfs_node_unlock(dnp
);
3733 /* nfs_getattr() will check changed and purge caches */
3734 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
3737 if (!error
&& fh
.fh_len
)
3738 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
3740 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
3744 nfs_node_clear_busy(dnp
);
3746 if (!error
&& (gotuid
|| gotgid
) &&
3747 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
3748 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
3749 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
3750 /* clear ID bits if server didn't use them (or we can't tell) */
3751 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
3752 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
3756 nfs_node_unlock(np
);
3761 nfs_node_unlock(np
);
3766 static uint32_t create_verf
;
3768 * NFS file create call
3772 struct vnop_create_args
/* {
3773 struct vnodeop_desc *a_desc;
3776 struct componentname *a_cnp;
3777 struct vnode_attr *a_vap;
3778 vfs_context_t a_context;
3781 vfs_context_t ctx
= ap
->a_context
;
3782 vnode_t dvp
= ap
->a_dvp
;
3783 struct vnode_attr
*vap
= ap
->a_vap
;
3784 struct componentname
*cnp
= ap
->a_cnp
;
3785 struct nfs_vattr nvattr
;
3787 nfsnode_t np
= NULL
;
3788 struct nfsmount
*nmp
;
3789 nfsnode_t dnp
= VTONFS(dvp
);
3790 vnode_t newvp
= NULL
;
3791 int error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0, fmode
= 0;
3792 struct timespec premtime
= { 0, 0 };
3793 int nfsvers
, gotuid
, gotgid
;
3794 u_int64_t xid
, dxid
;
3796 struct nfsm_chain nmreq
, nmrep
;
3797 struct nfsreq rq
, *req
= &rq
;
3798 struct nfs_dulookup dul
;
3801 if (nfs_mount_gone(nmp
))
3803 nfsvers
= nmp
->nm_vers
;
3805 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
))
3806 return (ENAMETOOLONG
);
3808 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
3810 VATTR_SET_SUPPORTED(vap
, va_mode
);
3811 VATTR_SET_SUPPORTED(vap
, va_uid
);
3812 VATTR_SET_SUPPORTED(vap
, va_gid
);
3813 VATTR_SET_SUPPORTED(vap
, va_data_size
);
3814 VATTR_SET_SUPPORTED(vap
, va_access_time
);
3815 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
3816 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
3817 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
3819 if (vap
->va_vaflags
& VA_EXCLUSIVE
) {
3821 if (!VATTR_IS_ACTIVE(vap
, va_access_time
) || !VATTR_IS_ACTIVE(vap
, va_modify_time
))
3822 vap
->va_vaflags
|= VA_UTIMES_NULL
;
3826 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
3827 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
3829 nfsm_chain_null(&nmreq
);
3830 nfsm_chain_null(&nmrep
);
3832 nfsm_chain_build_alloc_init(error
, &nmreq
,
3833 NFSX_FH(nfsvers
) + 2 * NFSX_UNSIGNED
+
3834 nfsm_rndup(cnp
->cn_namelen
) + NFSX_SATTR(nfsvers
));
3835 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
3836 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
3837 if (nfsvers
== NFS_VER3
) {
3838 if (fmode
& O_EXCL
) {
3839 nfsm_chain_add_32(error
, &nmreq
, NFS_CREATE_EXCLUSIVE
);
3840 lck_rw_lock_shared(in_ifaddr_rwlock
);
3841 if (!TAILQ_EMPTY(&in_ifaddrhead
))
3842 val
= IA_SIN(in_ifaddrhead
.tqh_first
)->sin_addr
.s_addr
;
3845 lck_rw_done(in_ifaddr_rwlock
);
3846 nfsm_chain_add_32(error
, &nmreq
, val
);
3848 nfsm_chain_add_32(error
, &nmreq
, create_verf
);
3850 nfsm_chain_add_32(error
, &nmreq
, NFS_CREATE_UNCHECKED
);
3851 nfsm_chain_add_v3sattr(error
, &nmreq
, vap
);
3854 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, 0);
3856 nfsm_chain_build_done(error
, &nmreq
);
3859 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_CREATE
,
3860 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
3862 nfs_dulookup_start(&dul
, dnp
, ctx
);
3863 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
3866 if ((lockerror
= nfs_node_lock(dnp
)))
3869 if (!error
&& !status
) {
3870 if (dnp
->n_flag
& NNEGNCENTRIES
) {
3871 dnp
->n_flag
&= ~NNEGNCENTRIES
;
3872 cache_purge_negatives(dvp
);
3874 error
= nfsm_chain_get_fh_attr(&nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
3876 if (nfsvers
== NFS_VER3
)
3877 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
3881 nfsm_chain_cleanup(&nmreq
);
3882 nfsm_chain_cleanup(&nmrep
);
3885 dnp
->n_flag
|= NMODIFIED
;
3886 /* if directory hadn't changed, update namecache mtime */
3887 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==))
3888 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
3889 nfs_node_unlock(dnp
);
3890 /* nfs_getattr() will check changed and purge caches */
3891 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
3894 if (!error
&& fh
.fh_len
)
3895 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
3897 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
3901 nfs_dulookup_finish(&dul
, dnp
, ctx
);
3903 nfs_node_clear_busy(dnp
);
3906 if ((nfsvers
== NFS_VER3
) && (fmode
& O_EXCL
) && (error
== NFSERR_NOTSUPP
)) {
3911 nfs_node_unlock(np
);
3914 } else if ((nfsvers
== NFS_VER3
) && (fmode
& O_EXCL
)) {
3915 nfs_node_unlock(np
);
3916 error
= nfs3_setattr_rpc(np
, vap
, ctx
);
3917 if (error
&& (gotuid
|| gotgid
)) {
3918 /* it's possible the server didn't like our attempt to set IDs. */
3919 /* so, let's try it again without those */
3920 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
3921 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
3922 error
= nfs3_setattr_rpc(np
, vap
, ctx
);
3927 nfs_node_lock_force(np
);
3931 if (!error
&& (gotuid
|| gotgid
) &&
3932 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
3933 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
3934 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
3935 /* clear ID bits if server didn't use them (or we can't tell) */
3936 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
3937 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
3940 nfs_node_unlock(np
);
3945 * NFS file remove call
3946 * To try and make NFS semantics closer to UFS semantics, a file that has
3947 * other processes using the vnode is renamed instead of removed and then
3948 * removed later on the last close.
3949 * - If vnode_isinuse()
3950 * If a rename is not already in the works
3951 * call nfs_sillyrename() to set it up
3957 struct vnop_remove_args
/* {
3958 struct vnodeop_desc *a_desc;
3961 struct componentname *a_cnp;
3963 vfs_context_t a_context;
3966 vfs_context_t ctx
= ap
->a_context
;
3967 vnode_t vp
= ap
->a_vp
;
3968 vnode_t dvp
= ap
->a_dvp
;
3969 struct componentname
*cnp
= ap
->a_cnp
;
3970 nfsnode_t dnp
= VTONFS(dvp
);
3971 nfsnode_t np
= VTONFS(vp
);
3972 int error
= 0, nfsvers
, namedattrs
, inuse
, gotattr
= 0, flushed
= 0, setsize
= 0;
3973 struct nfs_vattr nvattr
;
3974 struct nfsmount
*nmp
;
3975 struct nfs_dulookup dul
;
3977 /* XXX prevent removing a sillyrenamed file? */
3979 nmp
= NFSTONMP(dnp
);
3980 if (nfs_mount_gone(nmp
))
3982 nfsvers
= nmp
->nm_vers
;
3983 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
3986 error
= nfs_node_set_busy2(dnp
, np
, vfs_context_thread(ctx
));
3990 /* lock the node while we remove the file */
3991 lck_mtx_lock(nfs_node_hash_mutex
);
3992 while (np
->n_hflag
& NHLOCKED
) {
3993 np
->n_hflag
|= NHLOCKWANT
;
3994 msleep(np
, nfs_node_hash_mutex
, PINOD
, "nfs_remove", NULL
);
3996 np
->n_hflag
|= NHLOCKED
;
3997 lck_mtx_unlock(nfs_node_hash_mutex
);
4000 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4002 inuse
= vnode_isinuse(vp
, 0);
4003 if ((ap
->a_flags
& VNODE_REMOVE_NODELETEBUSY
) && inuse
) {
4004 /* Caller requested Carbon delete semantics, but file is busy */
4008 if (inuse
&& !gotattr
) {
4009 if (nfs_getattr(np
, &nvattr
, ctx
, NGA_CACHED
))
4010 nvattr
.nva_nlink
= 1;
4014 if (!inuse
|| (np
->n_sillyrename
&& (nvattr
.nva_nlink
> 1))) {
4016 if (!inuse
&& !flushed
) { /* flush all the buffers first */
4017 /* unlock the node */
4018 lck_mtx_lock(nfs_node_hash_mutex
);
4019 np
->n_hflag
&= ~NHLOCKED
;
4020 if (np
->n_hflag
& NHLOCKWANT
) {
4021 np
->n_hflag
&= ~NHLOCKWANT
;
4024 lck_mtx_unlock(nfs_node_hash_mutex
);
4025 nfs_node_clear_busy2(dnp
, np
);
4026 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
4027 FSDBG(260, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0011);
4029 if (error
== EINTR
) {
4030 nfs_node_lock_force(np
);
4031 NATTRINVALIDATE(np
);
4032 nfs_node_unlock(np
);
4036 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4040 if ((nmp
->nm_vers
>= NFS_VER4
) && (np
->n_openflags
& N_DELEG_MASK
))
4041 nfs4_delegation_return(np
, 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4044 * Purge the name cache so that the chance of a lookup for
4045 * the name succeeding while the remove is in progress is
4048 nfs_name_cache_purge(dnp
, np
, cnp
, ctx
);
4051 nfs_dulookup_start(&dul
, dnp
, ctx
);
4054 error
= nmp
->nm_funcs
->nf_remove_rpc(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
,
4055 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4058 * Kludge City: If the first reply to the remove rpc is lost..
4059 * the reply to the retransmitted request will be ENOENT
4060 * since the file was in fact removed
4061 * Therefore, we cheat and return success.
4063 if (error
== ENOENT
)
4066 if (!error
&& !inuse
&& !np
->n_sillyrename
) {
4068 * removal succeeded, it's not in use, and not silly renamed so
4069 * remove nfsnode from hash now so we can't accidentally find it
4070 * again if another object gets created with the same filehandle
4071 * before this vnode gets reclaimed
4073 lck_mtx_lock(nfs_node_hash_mutex
);
4074 if (np
->n_hflag
& NHHASHED
) {
4075 LIST_REMOVE(np
, n_hash
);
4076 np
->n_hflag
&= ~NHHASHED
;
4077 FSDBG(266, 0, np
, np
->n_flag
, 0xb1eb1e);
4079 lck_mtx_unlock(nfs_node_hash_mutex
);
4080 /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */
4081 /* clear all flags other than these */
4082 nfs_node_lock_force(np
);
4083 np
->n_flag
&= (NMODIFIED
);
4084 NATTRINVALIDATE(np
);
4085 nfs_node_unlock(np
);
4089 nfs_node_lock_force(np
);
4090 NATTRINVALIDATE(np
);
4091 nfs_node_unlock(np
);
4093 } else if (!np
->n_sillyrename
) {
4095 nfs_dulookup_start(&dul
, dnp
, ctx
);
4096 error
= nfs_sillyrename(dnp
, np
, cnp
, ctx
);
4097 nfs_node_lock_force(np
);
4098 NATTRINVALIDATE(np
);
4099 nfs_node_unlock(np
);
4101 nfs_node_lock_force(np
);
4102 NATTRINVALIDATE(np
);
4103 nfs_node_unlock(np
);
4105 nfs_dulookup_start(&dul
, dnp
, ctx
);
4108 /* nfs_getattr() will check changed and purge caches */
4109 nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
);
4111 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4113 /* unlock the node */
4114 lck_mtx_lock(nfs_node_hash_mutex
);
4115 np
->n_hflag
&= ~NHLOCKED
;
4116 if (np
->n_hflag
& NHLOCKWANT
) {
4117 np
->n_hflag
&= ~NHLOCKWANT
;
4120 lck_mtx_unlock(nfs_node_hash_mutex
);
4121 nfs_node_clear_busy2(dnp
, np
);
4128 * NFS silly-renamed file removal function called from nfs_vnop_inactive
4131 nfs_removeit(struct nfs_sillyrename
*nsp
)
4133 struct nfsmount
*nmp
= NFSTONMP(nsp
->nsr_dnp
);
4134 if (nfs_mount_gone(nmp
))
4136 return nmp
->nm_funcs
->nf_remove_rpc(nsp
->nsr_dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, NULL
, nsp
->nsr_cred
);
4140 * NFS remove rpc, called from nfs_remove() and nfs_removeit().
4150 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0;
4151 struct timespec premtime
= { 0, 0 };
4152 struct nfsmount
*nmp
;
4155 struct nfsm_chain nmreq
, nmrep
;
4157 nmp
= NFSTONMP(dnp
);
4158 if (nfs_mount_gone(nmp
))
4160 nfsvers
= nmp
->nm_vers
;
4161 if ((nfsvers
== NFS_VER2
) && (namelen
> NFS_MAXNAMLEN
))
4162 return (ENAMETOOLONG
);
4164 nfsm_chain_null(&nmreq
);
4165 nfsm_chain_null(&nmrep
);
4167 nfsm_chain_build_alloc_init(error
, &nmreq
,
4168 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+ nfsm_rndup(namelen
));
4169 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4170 nfsm_chain_add_name(error
, &nmreq
, name
, namelen
, nmp
);
4171 nfsm_chain_build_done(error
, &nmreq
);
4174 error
= nfs_request2(dnp
, NULL
, &nmreq
, NFSPROC_REMOVE
, thd
, cred
, NULL
, 0, &nmrep
, &xid
, &status
);
4176 if ((lockerror
= nfs_node_lock(dnp
)))
4178 if (nfsvers
== NFS_VER3
)
4179 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &xid
);
4181 dnp
->n_flag
|= NMODIFIED
;
4182 /* if directory hadn't changed, update namecache mtime */
4183 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==))
4184 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
4186 NATTRINVALIDATE(dnp
);
4191 nfs_node_unlock(dnp
);
4192 nfsm_chain_cleanup(&nmreq
);
4193 nfsm_chain_cleanup(&nmrep
);
4198 * NFS file rename call
4202 struct vnop_rename_args
/* {
4203 struct vnodeop_desc *a_desc;
4206 struct componentname *a_fcnp;
4209 struct componentname *a_tcnp;
4210 vfs_context_t a_context;
4213 vfs_context_t ctx
= ap
->a_context
;
4214 vnode_t fdvp
= ap
->a_fdvp
;
4215 vnode_t fvp
= ap
->a_fvp
;
4216 vnode_t tdvp
= ap
->a_tdvp
;
4217 vnode_t tvp
= ap
->a_tvp
;
4218 nfsnode_t fdnp
, fnp
, tdnp
, tnp
;
4219 struct componentname
*tcnp
= ap
->a_tcnp
;
4220 struct componentname
*fcnp
= ap
->a_fcnp
;
4221 int error
, nfsvers
, inuse
=0, tvprecycle
=0, locked
=0;
4222 mount_t fmp
, tdmp
, tmp
;
4223 struct nfs_vattr nvattr
;
4224 struct nfsmount
*nmp
;
4226 fdnp
= VTONFS(fdvp
);
4228 tdnp
= VTONFS(tdvp
);
4229 tnp
= tvp
? VTONFS(tvp
) : NULL
;
4231 nmp
= NFSTONMP(fdnp
);
4232 if (nfs_mount_gone(nmp
))
4234 nfsvers
= nmp
->nm_vers
;
4236 error
= nfs_node_set_busy4(fdnp
, fnp
, tdnp
, tnp
, vfs_context_thread(ctx
));
4240 if (tvp
&& (tvp
!= fvp
)) {
4241 /* lock the node while we rename over the existing file */
4242 lck_mtx_lock(nfs_node_hash_mutex
);
4243 while (tnp
->n_hflag
& NHLOCKED
) {
4244 tnp
->n_hflag
|= NHLOCKWANT
;
4245 msleep(tnp
, nfs_node_hash_mutex
, PINOD
, "nfs_rename", NULL
);
4247 tnp
->n_hflag
|= NHLOCKED
;
4248 lck_mtx_unlock(nfs_node_hash_mutex
);
4252 /* Check for cross-device rename */
4253 fmp
= vnode_mount(fvp
);
4254 tmp
= tvp
? vnode_mount(tvp
) : NULL
;
4255 tdmp
= vnode_mount(tdvp
);
4256 if ((fmp
!= tdmp
) || (tvp
&& (fmp
!= tmp
))) {
4261 /* XXX prevent renaming from/over a sillyrenamed file? */
4264 * If the tvp exists and is in use, sillyrename it before doing the
4265 * rename of the new file over it.
4266 * XXX Can't sillyrename a directory.
4267 * Don't sillyrename if source and target are same vnode (hard
4268 * links or case-variants)
4270 if (tvp
&& (tvp
!= fvp
))
4271 inuse
= vnode_isinuse(tvp
, 0);
4272 if (inuse
&& !tnp
->n_sillyrename
&& (vnode_vtype(tvp
) != VDIR
)) {
4273 error
= nfs_sillyrename(tdnp
, tnp
, tcnp
, ctx
);
4275 /* sillyrename failed. Instead of pressing on, return error */
4276 goto out
; /* should not be ENOENT. */
4278 /* sillyrename succeeded.*/
4281 } else if (tvp
&& (nmp
->nm_vers
>= NFS_VER4
) && (tnp
->n_openflags
& N_DELEG_MASK
)) {
4282 nfs4_delegation_return(tnp
, 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4285 error
= nmp
->nm_funcs
->nf_rename_rpc(fdnp
, fcnp
->cn_nameptr
, fcnp
->cn_namelen
,
4286 tdnp
, tcnp
->cn_nameptr
, tcnp
->cn_namelen
, ctx
);
4289 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
4291 if (error
== ENOENT
)
4294 if (tvp
&& (tvp
!= fvp
) && !tnp
->n_sillyrename
) {
4295 nfs_node_lock_force(tnp
);
4296 tvprecycle
= (!error
&& !vnode_isinuse(tvp
, 0) &&
4297 (nfs_getattrcache(tnp
, &nvattr
, 0) || (nvattr
.nva_nlink
== 1)));
4298 nfs_node_unlock(tnp
);
4299 lck_mtx_lock(nfs_node_hash_mutex
);
4300 if (tvprecycle
&& (tnp
->n_hflag
& NHHASHED
)) {
4302 * remove nfsnode from hash now so we can't accidentally find it
4303 * again if another object gets created with the same filehandle
4304 * before this vnode gets reclaimed
4306 LIST_REMOVE(tnp
, n_hash
);
4307 tnp
->n_hflag
&= ~NHHASHED
;
4308 FSDBG(266, 0, tnp
, tnp
->n_flag
, 0xb1eb1e);
4310 lck_mtx_unlock(nfs_node_hash_mutex
);
4313 /* purge the old name cache entries and enter the new one */
4314 nfs_name_cache_purge(fdnp
, fnp
, fcnp
, ctx
);
4316 nfs_name_cache_purge(tdnp
, tnp
, tcnp
, ctx
);
4318 /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */
4319 /* clear all flags other than these */
4320 nfs_node_lock_force(tnp
);
4321 tnp
->n_flag
&= (NMODIFIED
);
4322 nfs_node_unlock(tnp
);
4327 nfs_node_lock_force(tdnp
);
4328 if (tdnp
->n_flag
& NNEGNCENTRIES
) {
4329 tdnp
->n_flag
&= ~NNEGNCENTRIES
;
4330 cache_purge_negatives(tdvp
);
4332 nfs_node_unlock(tdnp
);
4333 nfs_node_lock_force(fnp
);
4334 cache_enter(tdvp
, fvp
, tcnp
);
4335 if (tdvp
!= fdvp
) { /* update parent pointer */
4336 if (fnp
->n_parent
&& !vnode_get(fnp
->n_parent
)) {
4337 /* remove ref from old parent */
4338 vnode_rele(fnp
->n_parent
);
4339 vnode_put(fnp
->n_parent
);
4341 fnp
->n_parent
= tdvp
;
4342 if (tdvp
&& !vnode_get(tdvp
)) {
4343 /* add ref to new parent */
4347 fnp
->n_parent
= NULL
;
4350 nfs_node_unlock(fnp
);
4353 /* nfs_getattr() will check changed and purge caches */
4354 nfs_getattr(fdnp
, NULL
, ctx
, NGA_CACHED
);
4355 nfs_getattr(tdnp
, NULL
, ctx
, NGA_CACHED
);
4358 lck_mtx_lock(nfs_node_hash_mutex
);
4359 tnp
->n_hflag
&= ~NHLOCKED
;
4360 if (tnp
->n_hflag
& NHLOCKWANT
) {
4361 tnp
->n_hflag
&= ~NHLOCKWANT
;
4364 lck_mtx_unlock(nfs_node_hash_mutex
);
4366 nfs_node_clear_busy4(fdnp
, fnp
, tdnp
, tnp
);
4371 * Do an NFS rename rpc. Called from nfs_vnop_rename() and nfs_sillyrename().
4383 int error
= 0, lockerror
= ENOENT
, status
, fwccpostattr
= 0, twccpostattr
= 0;
4384 struct timespec fpremtime
= { 0, 0 }, tpremtime
= { 0, 0 };
4385 struct nfsmount
*nmp
;
4387 u_int64_t xid
, txid
;
4388 struct nfsm_chain nmreq
, nmrep
;
4390 nmp
= NFSTONMP(fdnp
);
4391 if (nfs_mount_gone(nmp
))
4393 nfsvers
= nmp
->nm_vers
;
4394 if ((nfsvers
== NFS_VER2
) &&
4395 ((fnamelen
> NFS_MAXNAMLEN
) || (tnamelen
> NFS_MAXNAMLEN
)))
4396 return (ENAMETOOLONG
);
4398 nfsm_chain_null(&nmreq
);
4399 nfsm_chain_null(&nmrep
);
4401 nfsm_chain_build_alloc_init(error
, &nmreq
,
4402 (NFSX_FH(nfsvers
) + NFSX_UNSIGNED
) * 2 +
4403 nfsm_rndup(fnamelen
) + nfsm_rndup(tnamelen
));
4404 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, fdnp
->n_fhp
, fdnp
->n_fhsize
);
4405 nfsm_chain_add_name(error
, &nmreq
, fnameptr
, fnamelen
, nmp
);
4406 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, tdnp
->n_fhp
, tdnp
->n_fhsize
);
4407 nfsm_chain_add_name(error
, &nmreq
, tnameptr
, tnamelen
, nmp
);
4408 nfsm_chain_build_done(error
, &nmreq
);
4411 error
= nfs_request(fdnp
, NULL
, &nmreq
, NFSPROC_RENAME
, ctx
, NULL
, &nmrep
, &xid
, &status
);
4413 if ((lockerror
= nfs_node_lock2(fdnp
, tdnp
)))
4415 if (nfsvers
== NFS_VER3
) {
4417 nfsm_chain_get_wcc_data(error
, &nmrep
, fdnp
, &fpremtime
, &fwccpostattr
, &xid
);
4418 nfsm_chain_get_wcc_data(error
, &nmrep
, tdnp
, &tpremtime
, &twccpostattr
, &txid
);
4423 nfsm_chain_cleanup(&nmreq
);
4424 nfsm_chain_cleanup(&nmrep
);
4426 fdnp
->n_flag
|= NMODIFIED
;
4427 /* if directory hadn't changed, update namecache mtime */
4428 if (nfstimespeccmp(&fdnp
->n_ncmtime
, &fpremtime
, ==))
4429 NFS_CHANGED_UPDATE_NC(nfsvers
, fdnp
, &fdnp
->n_vattr
);
4431 NATTRINVALIDATE(fdnp
);
4432 tdnp
->n_flag
|= NMODIFIED
;
4433 /* if directory hadn't changed, update namecache mtime */
4434 if (nfstimespeccmp(&tdnp
->n_ncmtime
, &tpremtime
, ==))
4435 NFS_CHANGED_UPDATE_NC(nfsvers
, tdnp
, &tdnp
->n_vattr
);
4437 NATTRINVALIDATE(tdnp
);
4438 nfs_node_unlock2(fdnp
, tdnp
);
4444 * NFS hard link create call
4448 struct vnop_link_args
/* {
4449 struct vnodeop_desc *a_desc;
4452 struct componentname *a_cnp;
4453 vfs_context_t a_context;
4456 vfs_context_t ctx
= ap
->a_context
;
4457 vnode_t vp
= ap
->a_vp
;
4458 vnode_t tdvp
= ap
->a_tdvp
;
4459 struct componentname
*cnp
= ap
->a_cnp
;
4460 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0, attrflag
= 0;
4461 struct timespec premtime
= { 0, 0 };
4462 struct nfsmount
*nmp
;
4463 nfsnode_t np
= VTONFS(vp
);
4464 nfsnode_t tdnp
= VTONFS(tdvp
);
4466 u_int64_t xid
, txid
;
4467 struct nfsm_chain nmreq
, nmrep
;
4469 if (vnode_mount(vp
) != vnode_mount(tdvp
))
4473 if (nfs_mount_gone(nmp
))
4475 nfsvers
= nmp
->nm_vers
;
4476 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
))
4477 return (ENAMETOOLONG
);
4480 * Push all writes to the server, so that the attribute cache
4481 * doesn't get "out of sync" with the server.
4482 * XXX There should be a better way!
4484 nfs_flush(np
, MNT_WAIT
, vfs_context_thread(ctx
), V_IGNORE_WRITEERR
);
4486 error
= nfs_node_set_busy2(tdnp
, np
, vfs_context_thread(ctx
));
4490 nfsm_chain_null(&nmreq
);
4491 nfsm_chain_null(&nmrep
);
4493 nfsm_chain_build_alloc_init(error
, &nmreq
,
4494 NFSX_FH(nfsvers
)*2 + NFSX_UNSIGNED
+ nfsm_rndup(cnp
->cn_namelen
));
4495 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
4496 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, tdnp
->n_fhp
, tdnp
->n_fhsize
);
4497 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4498 nfsm_chain_build_done(error
, &nmreq
);
4500 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_LINK
, ctx
, NULL
, &nmrep
, &xid
, &status
);
4502 if ((lockerror
= nfs_node_lock2(tdnp
, np
))) {
4506 if (nfsvers
== NFS_VER3
) {
4508 nfsm_chain_postop_attr_update_flag(error
, &nmrep
, np
, attrflag
, &xid
);
4509 nfsm_chain_get_wcc_data(error
, &nmrep
, tdnp
, &premtime
, &wccpostattr
, &txid
);
4514 nfsm_chain_cleanup(&nmreq
);
4515 nfsm_chain_cleanup(&nmrep
);
4518 NATTRINVALIDATE(np
);
4519 tdnp
->n_flag
|= NMODIFIED
;
4520 /* if directory hadn't changed, update namecache mtime */
4521 if (nfstimespeccmp(&tdnp
->n_ncmtime
, &premtime
, ==))
4522 NFS_CHANGED_UPDATE_NC(nfsvers
, tdnp
, &tdnp
->n_vattr
);
4524 NATTRINVALIDATE(tdnp
);
4525 if (!error
&& (tdnp
->n_flag
& NNEGNCENTRIES
)) {
4526 tdnp
->n_flag
&= ~NNEGNCENTRIES
;
4527 cache_purge_negatives(tdvp
);
4529 nfs_node_unlock2(tdnp
, np
);
4531 nfs_node_clear_busy2(tdnp
, np
);
4533 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
4535 if (error
== EEXIST
)
4541 * NFS symbolic link create call
4545 struct vnop_symlink_args
/* {
4546 struct vnodeop_desc *a_desc;
4549 struct componentname *a_cnp;
4550 struct vnode_attr *a_vap;
4552 vfs_context_t a_context;
4555 vfs_context_t ctx
= ap
->a_context
;
4556 vnode_t dvp
= ap
->a_dvp
;
4557 struct vnode_attr
*vap
= ap
->a_vap
;
4558 struct componentname
*cnp
= ap
->a_cnp
;
4559 struct nfs_vattr nvattr
;
4561 int slen
, error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0;
4562 struct timespec premtime
= { 0, 0 };
4563 vnode_t newvp
= NULL
;
4564 int nfsvers
, gotuid
, gotgid
;
4565 u_int64_t xid
= 0, dxid
;
4566 nfsnode_t np
= NULL
;
4567 nfsnode_t dnp
= VTONFS(dvp
);
4568 struct nfsmount
*nmp
;
4569 struct nfsm_chain nmreq
, nmrep
;
4570 struct nfsreq rq
, *req
= &rq
;
4571 struct nfs_dulookup dul
;
4574 if (nfs_mount_gone(nmp
))
4576 nfsvers
= nmp
->nm_vers
;
4578 slen
= strlen(ap
->a_target
);
4579 if ((nfsvers
== NFS_VER2
) &&
4580 ((cnp
->cn_namelen
> NFS_MAXNAMLEN
) || (slen
> NFS_MAXPATHLEN
)))
4581 return (ENAMETOOLONG
);
4583 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
4585 VATTR_SET_SUPPORTED(vap
, va_mode
);
4586 VATTR_SET_SUPPORTED(vap
, va_uid
);
4587 VATTR_SET_SUPPORTED(vap
, va_gid
);
4588 VATTR_SET_SUPPORTED(vap
, va_data_size
);
4589 VATTR_SET_SUPPORTED(vap
, va_access_time
);
4590 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
4591 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
4592 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
4594 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
4595 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4597 nfsm_chain_null(&nmreq
);
4598 nfsm_chain_null(&nmrep
);
4600 nfsm_chain_build_alloc_init(error
, &nmreq
,
4601 NFSX_FH(nfsvers
) + 2 * NFSX_UNSIGNED
+
4602 nfsm_rndup(cnp
->cn_namelen
) + nfsm_rndup(slen
) + NFSX_SATTR(nfsvers
));
4603 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4604 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4605 if (nfsvers
== NFS_VER3
)
4606 nfsm_chain_add_v3sattr(error
, &nmreq
, vap
);
4607 nfsm_chain_add_name(error
, &nmreq
, ap
->a_target
, slen
, nmp
);
4608 if (nfsvers
== NFS_VER2
)
4609 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, -1);
4610 nfsm_chain_build_done(error
, &nmreq
);
4613 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_SYMLINK
,
4614 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
4616 nfs_dulookup_start(&dul
, dnp
, ctx
);
4617 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
4620 if ((lockerror
= nfs_node_lock(dnp
)))
4623 if (!error
&& !status
) {
4624 if (dnp
->n_flag
& NNEGNCENTRIES
) {
4625 dnp
->n_flag
&= ~NNEGNCENTRIES
;
4626 cache_purge_negatives(dvp
);
4628 if (nfsvers
== NFS_VER3
)
4629 error
= nfsm_chain_get_fh_attr(&nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
4633 if (nfsvers
== NFS_VER3
)
4634 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
4638 nfsm_chain_cleanup(&nmreq
);
4639 nfsm_chain_cleanup(&nmrep
);
4642 dnp
->n_flag
|= NMODIFIED
;
4643 /* if directory hadn't changed, update namecache mtime */
4644 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==))
4645 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
4646 nfs_node_unlock(dnp
);
4647 /* nfs_getattr() will check changed and purge caches */
4648 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
4651 if (!error
&& fh
.fh_len
)
4652 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
4656 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4659 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
4660 * if we can succeed in looking up the symlink.
4662 if ((error
== EEXIST
) || (!error
&& !newvp
)) {
4664 nfs_node_unlock(np
);
4668 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
4671 if (vnode_vtype(newvp
) != VLNK
)
4676 nfs_node_clear_busy(dnp
);
4677 if (!error
&& (gotuid
|| gotgid
) &&
4678 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
4679 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
4680 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
4681 /* clear ID bits if server didn't use them (or we can't tell) */
4682 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
4683 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
4687 nfs_node_unlock(np
);
4691 nfs_node_unlock(np
);
4702 struct vnop_mkdir_args
/* {
4703 struct vnodeop_desc *a_desc;
4706 struct componentname *a_cnp;
4707 struct vnode_attr *a_vap;
4708 vfs_context_t a_context;
4711 vfs_context_t ctx
= ap
->a_context
;
4712 vnode_t dvp
= ap
->a_dvp
;
4713 struct vnode_attr
*vap
= ap
->a_vap
;
4714 struct componentname
*cnp
= ap
->a_cnp
;
4715 struct nfs_vattr nvattr
;
4716 nfsnode_t np
= NULL
;
4717 struct nfsmount
*nmp
;
4718 nfsnode_t dnp
= VTONFS(dvp
);
4719 vnode_t newvp
= NULL
;
4720 int error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0;
4721 struct timespec premtime
= { 0, 0 };
4722 int nfsvers
, gotuid
, gotgid
;
4723 u_int64_t xid
= 0, dxid
;
4725 struct nfsm_chain nmreq
, nmrep
;
4726 struct nfsreq rq
, *req
= &rq
;
4727 struct nfs_dulookup dul
;
4730 if (nfs_mount_gone(nmp
))
4732 nfsvers
= nmp
->nm_vers
;
4733 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
))
4734 return (ENAMETOOLONG
);
4736 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
4738 VATTR_SET_SUPPORTED(vap
, va_mode
);
4739 VATTR_SET_SUPPORTED(vap
, va_uid
);
4740 VATTR_SET_SUPPORTED(vap
, va_gid
);
4741 VATTR_SET_SUPPORTED(vap
, va_data_size
);
4742 VATTR_SET_SUPPORTED(vap
, va_access_time
);
4743 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
4744 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
4745 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
4747 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
4748 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4750 nfsm_chain_null(&nmreq
);
4751 nfsm_chain_null(&nmrep
);
4753 nfsm_chain_build_alloc_init(error
, &nmreq
,
4754 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+
4755 nfsm_rndup(cnp
->cn_namelen
) + NFSX_SATTR(nfsvers
));
4756 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4757 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4758 if (nfsvers
== NFS_VER3
)
4759 nfsm_chain_add_v3sattr(error
, &nmreq
, vap
);
4761 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, -1);
4762 nfsm_chain_build_done(error
, &nmreq
);
4765 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_MKDIR
,
4766 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
4768 nfs_dulookup_start(&dul
, dnp
, ctx
);
4769 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
4772 if ((lockerror
= nfs_node_lock(dnp
)))
4775 if (!error
&& !status
) {
4776 if (dnp
->n_flag
& NNEGNCENTRIES
) {
4777 dnp
->n_flag
&= ~NNEGNCENTRIES
;
4778 cache_purge_negatives(dvp
);
4780 error
= nfsm_chain_get_fh_attr(&nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
4782 if (nfsvers
== NFS_VER3
)
4783 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
4787 nfsm_chain_cleanup(&nmreq
);
4788 nfsm_chain_cleanup(&nmrep
);
4791 dnp
->n_flag
|= NMODIFIED
;
4792 /* if directory hadn't changed, update namecache mtime */
4793 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==))
4794 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
4795 nfs_node_unlock(dnp
);
4796 /* nfs_getattr() will check changed and purge caches */
4797 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
4800 if (!error
&& fh
.fh_len
)
4801 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
4805 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4808 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
4809 * if we can succeed in looking up the directory.
4811 if ((error
== EEXIST
) || (!error
&& !newvp
)) {
4813 nfs_node_unlock(np
);
4817 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
4820 if (vnode_vtype(newvp
) != VDIR
)
4825 nfs_node_clear_busy(dnp
);
4826 if (!error
&& (gotuid
|| gotgid
) &&
4827 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
4828 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
4829 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
4830 /* clear ID bits if server didn't use them (or we can't tell) */
4831 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
4832 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
4836 nfs_node_unlock(np
);
4840 nfs_node_unlock(np
);
4847 * NFS remove directory call
4851 struct vnop_rmdir_args
/* {
4852 struct vnodeop_desc *a_desc;
4855 struct componentname *a_cnp;
4856 vfs_context_t a_context;
4859 vfs_context_t ctx
= ap
->a_context
;
4860 vnode_t vp
= ap
->a_vp
;
4861 vnode_t dvp
= ap
->a_dvp
;
4862 struct componentname
*cnp
= ap
->a_cnp
;
4863 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0;
4864 struct timespec premtime
= { 0, 0 };
4865 struct nfsmount
*nmp
;
4866 nfsnode_t np
= VTONFS(vp
);
4867 nfsnode_t dnp
= VTONFS(dvp
);
4870 struct nfsm_chain nmreq
, nmrep
;
4871 struct nfsreq rq
, *req
= &rq
;
4872 struct nfs_dulookup dul
;
4875 if (nfs_mount_gone(nmp
))
4877 nfsvers
= nmp
->nm_vers
;
4878 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
))
4879 return (ENAMETOOLONG
);
4881 if ((error
= nfs_node_set_busy2(dnp
, np
, vfs_context_thread(ctx
))))
4884 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4886 nfsm_chain_null(&nmreq
);
4887 nfsm_chain_null(&nmrep
);
4889 nfsm_chain_build_alloc_init(error
, &nmreq
,
4890 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+ nfsm_rndup(cnp
->cn_namelen
));
4891 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4892 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4893 nfsm_chain_build_done(error
, &nmreq
);
4896 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_RMDIR
,
4897 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
4899 nfs_dulookup_start(&dul
, dnp
, ctx
);
4900 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
4903 if ((lockerror
= nfs_node_lock(dnp
)))
4905 if (nfsvers
== NFS_VER3
)
4906 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &xid
);
4910 nfsm_chain_cleanup(&nmreq
);
4911 nfsm_chain_cleanup(&nmrep
);
4914 dnp
->n_flag
|= NMODIFIED
;
4915 /* if directory hadn't changed, update namecache mtime */
4916 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==))
4917 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
4918 nfs_node_unlock(dnp
);
4919 nfs_name_cache_purge(dnp
, np
, cnp
, ctx
);
4920 /* nfs_getattr() will check changed and purge caches */
4921 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
4923 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4924 nfs_node_clear_busy2(dnp
, np
);
4927 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
4929 if (error
== ENOENT
)
4933 * remove nfsnode from hash now so we can't accidentally find it
4934 * again if another object gets created with the same filehandle
4935 * before this vnode gets reclaimed
4937 lck_mtx_lock(nfs_node_hash_mutex
);
4938 if (np
->n_hflag
& NHHASHED
) {
4939 LIST_REMOVE(np
, n_hash
);
4940 np
->n_hflag
&= ~NHHASHED
;
4941 FSDBG(266, 0, np
, np
->n_flag
, 0xb1eb1e);
4943 lck_mtx_unlock(nfs_node_hash_mutex
);
4951 * The incoming "offset" is a directory cookie indicating where in the
4952 * directory entries should be read from. A zero cookie means start at
4953 * the beginning of the directory. Any other cookie will be a cookie
4954 * returned from the server.
4956 * Using that cookie, determine which buffer (and where in that buffer)
4957 * to start returning entries from. Buffer logical block numbers are
4958 * the cookies they start at. If a buffer is found that is not full,
4959 * call into the bio/RPC code to fill it. The RPC code will probably
4960 * fill several buffers (dropping the first, requiring a re-get).
4962 * When done copying entries to the buffer, set the offset to the current
4963 * entry's cookie and enter that cookie in the cookie cache.
4965 * Note: because the getdirentries(2) API returns a long-typed offset,
4966 * the incoming offset is a potentially truncated cookie (ptc).
4967 * The cookie matching code is aware of this and will fall back to
4968 * matching only 32 bits of the cookie.
4972 struct vnop_readdir_args
/* {
4973 struct vnodeop_desc *a_desc;
4979 vfs_context_t a_context;
4982 vfs_context_t ctx
= ap
->a_context
;
4983 vnode_t dvp
= ap
->a_vp
;
4984 nfsnode_t dnp
= VTONFS(dvp
);
4985 struct nfsmount
*nmp
;
4986 uio_t uio
= ap
->a_uio
;
4987 int error
, nfsvers
, extended
, numdirent
, bigcookies
, ptc
, done
;
4988 uint16_t i
, iptc
, rlen
, nlen
;
4989 uint64_t cookie
, nextcookie
, lbn
= 0;
4990 struct nfsbuf
*bp
= NULL
;
4991 struct nfs_dir_buf_header
*ndbhp
;
4992 struct direntry
*dp
, *dpptc
;
4998 if (nfs_mount_gone(nmp
))
5000 nfsvers
= nmp
->nm_vers
;
5001 bigcookies
= (nmp
->nm_state
& NFSSTA_BIGCOOKIES
);
5002 extended
= (ap
->a_flags
& VNODE_READDIR_EXTENDED
);
5004 if (vnode_vtype(dvp
) != VDIR
)
5010 if (uio_resid(uio
) == 0)
5013 if ((nfsvers
>= NFS_VER4
) && (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER
)) {
5014 /* trigger directories should never be read, return nothing */
5018 thd
= vfs_context_thread(ctx
);
5019 numdirent
= done
= 0;
5020 nextcookie
= uio_offset(uio
);
5021 ptc
= bigcookies
&& NFS_DIR_COOKIE_POTENTIALLY_TRUNCATED(nextcookie
);
5023 if ((error
= nfs_node_lock(dnp
)))
5026 if (dnp
->n_flag
& NNEEDINVALIDATE
) {
5027 dnp
->n_flag
&= ~NNEEDINVALIDATE
;
5029 nfs_node_unlock(dnp
);
5030 error
= nfs_vinvalbuf(dvp
, 0, ctx
, 1);
5032 error
= nfs_node_lock(dnp
);
5038 * check for need to invalidate when (re)starting at beginning
5041 if (dnp
->n_flag
& NMODIFIED
) {
5043 nfs_node_unlock(dnp
);
5044 if ((error
= nfs_vinvalbuf(dvp
, 0, ctx
, 1)))
5047 nfs_node_unlock(dnp
);
5049 /* nfs_getattr() will check changed and purge caches */
5050 if ((error
= nfs_getattr(dnp
, NULL
, ctx
, NGA_UNCACHED
)))
5053 nfs_node_unlock(dnp
);
5056 error
= nfs_dir_cookie_to_lbn(dnp
, nextcookie
, &ptc
, &lbn
);
5058 if (error
< 0) { /* just hit EOF cookie */
5066 while (!error
&& !done
) {
5067 OSAddAtomic64(1, &nfsstats
.biocache_readdirs
);
5068 cookie
= nextcookie
;
5070 error
= nfs_buf_get(dnp
, lbn
, NFS_DIRBLKSIZ
, thd
, NBLK_READ
, &bp
);
5073 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5074 if (!ISSET(bp
->nb_flags
, NB_CACHE
) || !ISSET(ndbhp
->ndbh_flags
, NDB_FULL
)) {
5075 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) { /* initialize the buffer */
5076 ndbhp
->ndbh_flags
= 0;
5077 ndbhp
->ndbh_count
= 0;
5078 ndbhp
->ndbh_entry_end
= sizeof(*ndbhp
);
5079 ndbhp
->ndbh_ncgen
= dnp
->n_ncgen
;
5081 error
= nfs_buf_readdir(bp
, ctx
);
5082 if (error
== NFSERR_DIRBUFDROPPED
)
5085 nfs_buf_release(bp
, 1);
5086 if (error
&& (error
!= ENXIO
) && (error
!= ETIMEDOUT
) && (error
!= EINTR
) && (error
!= ERESTART
)) {
5087 if (!nfs_node_lock(dnp
)) {
5089 nfs_node_unlock(dnp
);
5091 nfs_vinvalbuf(dvp
, 0, ctx
, 1);
5092 if (error
== NFSERR_BAD_COOKIE
)
5099 /* find next entry to return */
5100 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5102 if ((lbn
!= cookie
) && !(ptc
&& NFS_DIR_COOKIE_SAME32(lbn
, cookie
))) {
5105 for (; (i
< ndbhp
->ndbh_count
) && (cookie
!= dp
->d_seekoff
); i
++) {
5106 if (ptc
&& !dpptc
&& NFS_DIR_COOKIE_SAME32(cookie
, dp
->d_seekoff
)) {
5110 nextcookie
= dp
->d_seekoff
;
5111 dp
= NFS_DIRENTRY_NEXT(dp
);
5113 if ((i
== ndbhp
->ndbh_count
) && dpptc
) {
5117 if (i
< ndbhp
->ndbh_count
) {
5118 nextcookie
= dp
->d_seekoff
;
5119 dp
= NFS_DIRENTRY_NEXT(dp
);
5123 ptc
= 0; /* only have to deal with ptc on first cookie */
5125 /* return as many entries as we can */
5126 for (; i
< ndbhp
->ndbh_count
; i
++) {
5128 rlen
= dp
->d_reclen
;
5133 bzero(cp
, sizeof(dent
));
5135 if (dp
->d_namlen
> (sizeof(dent
.d_name
) - 1))
5136 nlen
= sizeof(dent
.d_name
) - 1;
5138 nlen
= dp
->d_namlen
;
5139 rlen
= NFS_DIRENT_LEN(nlen
);
5140 dent
.d_reclen
= rlen
;
5141 dent
.d_ino
= dp
->d_ino
;
5142 dent
.d_type
= dp
->d_type
;
5143 dent
.d_namlen
= nlen
;
5144 strlcpy(dent
.d_name
, dp
->d_name
, nlen
+ 1);
5146 /* check that the record fits */
5147 if (rlen
> uio_resid(uio
)) {
5151 if ((error
= uiomove(cp
, rlen
, uio
)))
5154 nextcookie
= dp
->d_seekoff
;
5155 dp
= NFS_DIRENTRY_NEXT(dp
);
5158 if (i
== ndbhp
->ndbh_count
) {
5159 /* hit end of buffer, move to next buffer */
5161 /* if we also hit EOF, we're done */
5162 if (ISSET(ndbhp
->ndbh_flags
, NDB_EOF
)) {
5169 uio_setoffset(uio
, nextcookie
);
5170 if (!error
&& !done
&& (nextcookie
== cookie
)) {
5171 printf("nfs readdir cookie didn't change 0x%llx, %d/%d\n", cookie
, i
, ndbhp
->ndbh_count
);
5174 nfs_buf_release(bp
, 1);
5178 nfs_dir_cookie_cache(dnp
, nextcookie
, lbn
);
5180 if (ap
->a_numdirent
)
5181 *ap
->a_numdirent
= numdirent
;
5188 * Invalidate cached directory information, except for the actual directory
5189 * blocks (which are invalidated separately).
5192 nfs_invaldir(nfsnode_t dnp
)
5194 if (vnode_vtype(NFSTOV(dnp
)) != VDIR
)
5196 dnp
->n_eofcookie
= 0;
5197 dnp
->n_cookieverf
= 0;
5198 if (!dnp
->n_cookiecache
)
5200 dnp
->n_cookiecache
->free
= 0;
5201 dnp
->n_cookiecache
->mru
= -1;
5202 memset(dnp
->n_cookiecache
->next
, -1, NFSNUMCOOKIES
);
5206 * calculate how much space is available for additional directory entries.
5209 nfs_dir_buf_freespace(struct nfsbuf
*bp
, int rdirplus
)
5211 struct nfs_dir_buf_header
*ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5216 space
= bp
->nb_bufsize
- ndbhp
->ndbh_entry_end
;
5218 space
-= ndbhp
->ndbh_count
* sizeof(struct nfs_vattr
);
5223 * add/update a cookie->lbn entry in the directory cookie cache
5226 nfs_dir_cookie_cache(nfsnode_t dnp
, uint64_t cookie
, uint64_t lbn
)
5228 struct nfsdmap
*ndcc
;
5234 if (nfs_node_lock(dnp
))
5237 if (cookie
== dnp
->n_eofcookie
) { /* EOF cookie */
5238 nfs_node_unlock(dnp
);
5242 ndcc
= dnp
->n_cookiecache
;
5244 /* allocate the cookie cache structure */
5245 MALLOC_ZONE(dnp
->n_cookiecache
, struct nfsdmap
*,
5246 sizeof(struct nfsdmap
), M_NFSDIROFF
, M_WAITOK
);
5247 if (!dnp
->n_cookiecache
) {
5248 nfs_node_unlock(dnp
);
5251 ndcc
= dnp
->n_cookiecache
;
5254 memset(ndcc
->next
, -1, NFSNUMCOOKIES
);
5258 * Search the list for this cookie.
5259 * Keep track of previous and last entries.
5263 while ((i
!= -1) && (cookie
!= ndcc
->cookies
[i
].key
)) {
5264 if (ndcc
->next
[i
] == -1) /* stop on last entry so we can reuse */
5269 if ((i
!= -1) && (cookie
== ndcc
->cookies
[i
].key
)) {
5270 /* found it, remove from list */
5272 ndcc
->next
[prev
] = ndcc
->next
[i
];
5274 ndcc
->mru
= ndcc
->next
[i
];
5276 /* not found, use next free entry or reuse last entry */
5277 if (ndcc
->free
!= NFSNUMCOOKIES
)
5280 ndcc
->next
[prev
] = -1;
5281 ndcc
->cookies
[i
].key
= cookie
;
5282 ndcc
->cookies
[i
].lbn
= lbn
;
5284 /* insert cookie at head of MRU list */
5285 ndcc
->next
[i
] = ndcc
->mru
;
5287 nfs_node_unlock(dnp
);
5291 * Try to map the given directory cookie to a directory buffer (return lbn).
5292 * If we have a possibly truncated cookie (ptc), check for 32-bit matches too.
5295 nfs_dir_cookie_to_lbn(nfsnode_t dnp
, uint64_t cookie
, int *ptc
, uint64_t *lbnp
)
5297 struct nfsdmap
*ndcc
= dnp
->n_cookiecache
;
5298 int8_t eofptc
, found
;
5300 struct nfsmount
*nmp
;
5301 struct nfsbuf
*bp
, *lastbp
;
5302 struct nfsbuflists blist
;
5303 struct direntry
*dp
, *dpptc
;
5304 struct nfs_dir_buf_header
*ndbhp
;
5306 if (!cookie
) { /* initial cookie */
5312 if (nfs_node_lock(dnp
))
5315 if (cookie
== dnp
->n_eofcookie
) { /* EOF cookie */
5316 nfs_node_unlock(dnp
);
5317 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5321 /* note if cookie is a 32-bit match with the EOF cookie */
5322 eofptc
= *ptc
? NFS_DIR_COOKIE_SAME32(cookie
, dnp
->n_eofcookie
) : 0;
5325 /* search the list for the cookie */
5326 for (i
= ndcc
? ndcc
->mru
: -1; i
>= 0; i
= ndcc
->next
[i
]) {
5327 if (ndcc
->cookies
[i
].key
== cookie
) {
5328 /* found a match for this cookie */
5329 *lbnp
= ndcc
->cookies
[i
].lbn
;
5330 nfs_node_unlock(dnp
);
5331 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5335 /* check for 32-bit match */
5336 if (*ptc
&& (iptc
== -1) && NFS_DIR_COOKIE_SAME32(ndcc
->cookies
[i
].key
, cookie
))
5339 /* exact match not found */
5341 /* but 32-bit match hit the EOF cookie */
5342 nfs_node_unlock(dnp
);
5343 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5347 /* but 32-bit match got a hit */
5348 *lbnp
= ndcc
->cookies
[iptc
].lbn
;
5349 nfs_node_unlock(dnp
);
5350 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5353 nfs_node_unlock(dnp
);
5356 * No match found in the cookie cache... hmm...
5357 * Let's search the directory's buffers for the cookie.
5359 nmp
= NFSTONMP(dnp
);
5360 if (nfs_mount_gone(nmp
))
5365 lck_mtx_lock(nfs_buf_mutex
);
5367 * Scan the list of buffers, keeping them in order.
5368 * Note that itercomplete inserts each of the remaining buffers
5369 * into the head of list (thus reversing the elements). So, we
5370 * make sure to iterate through all buffers, inserting them after
5371 * each other, to keep them in order.
5372 * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because
5373 * we don't drop nfs_buf_mutex.
5375 if (!nfs_buf_iterprepare(dnp
, &blist
, NBI_CLEAN
)) {
5377 while ((bp
= LIST_FIRST(&blist
))) {
5378 LIST_REMOVE(bp
, nb_vnbufs
);
5380 LIST_INSERT_HEAD(&dnp
->n_cleanblkhd
, bp
, nb_vnbufs
);
5382 LIST_INSERT_AFTER(lastbp
, bp
, nb_vnbufs
);
5387 if (nfs_buf_acquire(bp
, NBAC_NOWAIT
, 0, 0)) {
5388 /* just skip this buffer */
5389 nfs_buf_refrele(bp
);
5392 nfs_buf_refrele(bp
);
5394 /* scan the buffer for the cookie */
5395 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5396 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5398 for (i
=0; (i
< ndbhp
->ndbh_count
) && (cookie
!= dp
->d_seekoff
); i
++) {
5399 if (*ptc
&& !dpptc
&& NFS_DIR_COOKIE_SAME32(cookie
, dp
->d_seekoff
)) {
5403 dp
= NFS_DIRENTRY_NEXT(dp
);
5405 if ((i
== ndbhp
->ndbh_count
) && dpptc
) {
5406 /* found only a PTC match */
5409 } else if (i
< ndbhp
->ndbh_count
) {
5412 if (i
< (ndbhp
->ndbh_count
-1)) {
5413 /* next entry is *in* this buffer: return this block */
5414 *lbnp
= bp
->nb_lblkno
;
5416 } else if (i
== (ndbhp
->ndbh_count
-1)) {
5417 /* next entry refers to *next* buffer: return next block */
5418 *lbnp
= dp
->d_seekoff
;
5423 nfs_buf_itercomplete(dnp
, &blist
, NBI_CLEAN
);
5425 lck_mtx_unlock(nfs_buf_mutex
);
5427 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5431 /* still not found... oh well, just start a new block */
5433 OSAddAtomic64(1, &nfsstats
.direofcache_misses
);
5438 * scan a directory buffer for the given name
5439 * Returns: ESRCH if not found, ENOENT if found invalid, 0 if found
5440 * Note: should only be called with RDIRPLUS directory buffers
5443 #define NDBS_PURGE 1
5444 #define NDBS_UPDATE 2
5449 struct componentname
*cnp
,
5451 struct nfs_vattr
*nvap
,
5454 daddr64_t
*nextlbnp
,
5457 struct direntry
*dp
;
5458 struct nfs_dir_buf_header
*ndbhp
;
5459 struct nfs_vattr
*nvattrp
;
5460 daddr64_t nextlbn
= 0;
5461 int i
, error
= ESRCH
, fhlen
;
5463 /* scan the buffer for the name */
5464 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5465 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5466 for (i
=0; i
< ndbhp
->ndbh_count
; i
++) {
5467 nextlbn
= dp
->d_seekoff
;
5468 if ((cnp
->cn_namelen
== dp
->d_namlen
) && !strcmp(cnp
->cn_nameptr
, dp
->d_name
)) {
5469 fhlen
= dp
->d_name
[dp
->d_namlen
+1];
5470 nvattrp
= NFS_DIR_BUF_NVATTR(bp
, i
);
5471 if ((ndbhp
->ndbh_ncgen
!= bp
->nb_np
->n_ncgen
) || (fhp
->fh_len
== 0) ||
5472 (nvattrp
->nva_type
== VNON
) || (nvattrp
->nva_fileid
== 0)) {
5473 /* entry is not valid */
5477 if (flags
== NDBS_PURGE
) {
5479 bzero(nvattrp
, sizeof(*nvattrp
));
5483 if (flags
== NDBS_UPDATE
) {
5484 /* update direntry's attrs if fh matches */
5485 if ((fhp
->fh_len
== fhlen
) && !bcmp(&dp
->d_name
[dp
->d_namlen
+2], fhp
->fh_data
, fhlen
)) {
5486 bcopy(nvap
, nvattrp
, sizeof(*nvap
));
5487 dp
->d_fileno
= nvattrp
->nva_fileid
;
5488 nvattrp
->nva_fileid
= *xidp
;
5489 *(time_t*)(&dp
->d_name
[dp
->d_namlen
+2+fhp
->fh_len
]) = *attrstampp
;
5494 /* copy out fh, attrs, attrstamp, and xid */
5495 fhp
->fh_len
= fhlen
;
5496 bcopy(&dp
->d_name
[dp
->d_namlen
+2], fhp
->fh_data
, MAX(fhp
->fh_len
, (int)sizeof(fhp
->fh_data
)));
5497 *attrstampp
= *(time_t*)(&dp
->d_name
[dp
->d_namlen
+2+fhp
->fh_len
]);
5498 bcopy(nvattrp
, nvap
, sizeof(*nvap
));
5499 *xidp
= nvap
->nva_fileid
;
5500 nvap
->nva_fileid
= dp
->d_fileno
;
5504 dp
= NFS_DIRENTRY_NEXT(dp
);
5507 *nextlbnp
= nextlbn
;
5512 * Look up a name in a directory's buffers.
5513 * Note: should only be called with RDIRPLUS directory buffers
5516 nfs_dir_buf_cache_lookup(nfsnode_t dnp
, nfsnode_t
*npp
, struct componentname
*cnp
, vfs_context_t ctx
, int purge
)
5519 struct nfsmount
*nmp
;
5520 int error
= 0, i
, found
= 0, count
= 0;
5522 struct nfs_vattr nvattr
;
5524 time_t attrstamp
= 0;
5525 thread_t thd
= vfs_context_thread(ctx
);
5526 struct nfsbuf
*bp
, *lastbp
, *foundbp
;
5527 struct nfsbuflists blist
;
5528 daddr64_t lbn
, nextlbn
;
5529 int dotunder
= (cnp
->cn_namelen
> 2) && (cnp
->cn_nameptr
[0] == '.') && (cnp
->cn_nameptr
[1] == '_');
5531 nmp
= NFSTONMP(dnp
);
5532 if (nfs_mount_gone(nmp
))
5537 /* first check most recent buffer (and next one too) */
5538 lbn
= dnp
->n_lastdbl
;
5539 for (i
=0; i
< 2; i
++) {
5540 if ((error
= nfs_buf_get(dnp
, lbn
, NFS_DIRBLKSIZ
, thd
, NBLK_READ
|NBLK_ONLYVALID
, &bp
)))
5545 error
= nfs_dir_buf_search(bp
, cnp
, &fh
, &nvattr
, &xid
, &attrstamp
, &nextlbn
, purge
? NDBS_PURGE
: 0);
5546 nfs_buf_release(bp
, 0);
5547 if (error
== ESRCH
) {
5556 lck_mtx_lock(nfs_buf_mutex
);
5558 dnp
->n_lastdbl
= lbn
;
5563 * Scan the list of buffers, keeping them in order.
5564 * Note that itercomplete inserts each of the remaining buffers
5565 * into the head of list (thus reversing the elements). So, we
5566 * make sure to iterate through all buffers, inserting them after
5567 * each other, to keep them in order.
5568 * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because
5569 * we don't drop nfs_buf_mutex.
5571 if (!nfs_buf_iterprepare(dnp
, &blist
, NBI_CLEAN
)) {
5572 lastbp
= foundbp
= NULL
;
5573 while ((bp
= LIST_FIRST(&blist
))) {
5574 LIST_REMOVE(bp
, nb_vnbufs
);
5576 LIST_INSERT_HEAD(&dnp
->n_cleanblkhd
, bp
, nb_vnbufs
);
5578 LIST_INSERT_AFTER(lastbp
, bp
, nb_vnbufs
);
5582 if (!purge
&& dotunder
&& (count
> 100)) /* don't waste too much time looking for ._ files */
5585 lbn
= bp
->nb_lblkno
;
5586 if (nfs_buf_acquire(bp
, NBAC_NOWAIT
, 0, 0)) {
5587 /* just skip this buffer */
5588 nfs_buf_refrele(bp
);
5591 nfs_buf_refrele(bp
);
5593 error
= nfs_dir_buf_search(bp
, cnp
, &fh
, &nvattr
, &xid
, &attrstamp
, NULL
, purge
? NDBS_PURGE
: 0);
5594 if (error
== ESRCH
) {
5603 LIST_REMOVE(foundbp
, nb_vnbufs
);
5604 LIST_INSERT_HEAD(&dnp
->n_cleanblkhd
, foundbp
, nb_vnbufs
);
5605 dnp
->n_lastdbl
= foundbp
->nb_lblkno
;
5607 nfs_buf_itercomplete(dnp
, &blist
, NBI_CLEAN
);
5610 lck_mtx_unlock(nfs_buf_mutex
);
5612 if (!error
&& found
&& !purge
) {
5613 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
,
5614 &nvattr
, &xid
, dnp
->n_auth
, NG_MAKEENTRY
, &newnp
);
5617 newnp
->n_attrstamp
= attrstamp
;
5619 nfs_node_unlock(newnp
);
5620 /* check if the dir buffer's attrs are out of date */
5621 if (!nfs_getattr(newnp
, &nvattr
, ctx
, NGA_CACHED
) &&
5622 (newnp
->n_attrstamp
!= attrstamp
)) {
5623 /* they are, so update them */
5624 error
= nfs_buf_get(dnp
, lbn
, NFS_DIRBLKSIZ
, thd
, NBLK_READ
|NBLK_ONLYVALID
, &bp
);
5626 attrstamp
= newnp
->n_attrstamp
;
5628 nfs_dir_buf_search(bp
, cnp
, &fh
, &nvattr
, &xid
, &attrstamp
, NULL
, NDBS_UPDATE
);
5629 nfs_buf_release(bp
, 0);
5639 * Purge name cache entries for the given node.
5640 * For RDIRPLUS, also invalidate the entry in the directory's buffers.
5643 nfs_name_cache_purge(nfsnode_t dnp
, nfsnode_t np
, struct componentname
*cnp
, vfs_context_t ctx
)
5645 struct nfsmount
*nmp
= NFSTONMP(dnp
);
5647 cache_purge(NFSTOV(np
));
5648 if (nmp
&& (nmp
->nm_vers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
))
5649 nfs_dir_buf_cache_lookup(dnp
, NULL
, cnp
, ctx
, 1);
5653 * NFS V3 readdir (plus) RPC.
5656 nfs3_readdir_rpc(nfsnode_t dnp
, struct nfsbuf
*bp
, vfs_context_t ctx
)
5658 struct nfsmount
*nmp
;
5659 int error
= 0, lockerror
, nfsvers
, rdirplus
, bigcookies
;
5660 int i
, status
, attrflag
, fhflag
, more_entries
= 1, eof
, bp_dropped
= 0;
5661 uint32_t nmreaddirsize
, nmrsize
;
5662 uint32_t namlen
, skiplen
, fhlen
, xlen
, attrlen
, reclen
, space_free
, space_needed
;
5663 uint64_t cookie
, lastcookie
, xid
, savedxid
, fileno
;
5664 struct nfsm_chain nmreq
, nmrep
, nmrepsave
;
5666 struct nfs_vattr
*nvattrp
;
5667 struct nfs_dir_buf_header
*ndbhp
;
5668 struct direntry
*dp
;
5669 char *padstart
, padlen
;
5672 nmp
= NFSTONMP(dnp
);
5673 if (nfs_mount_gone(nmp
))
5675 nfsvers
= nmp
->nm_vers
;
5676 nmreaddirsize
= nmp
->nm_readdirsize
;
5677 nmrsize
= nmp
->nm_rsize
;
5678 bigcookies
= nmp
->nm_state
& NFSSTA_BIGCOOKIES
;
5680 rdirplus
= ((nfsvers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
)) ? 1 : 0;
5682 if ((lockerror
= nfs_node_lock(dnp
)))
5685 /* determine cookie to use, and move dp to the right offset */
5686 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5687 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5688 if (ndbhp
->ndbh_count
) {
5689 for (i
=0; i
< ndbhp
->ndbh_count
-1; i
++)
5690 dp
= NFS_DIRENTRY_NEXT(dp
);
5691 cookie
= dp
->d_seekoff
;
5692 dp
= NFS_DIRENTRY_NEXT(dp
);
5694 cookie
= bp
->nb_lblkno
;
5695 /* increment with every buffer read */
5696 OSAddAtomic64(1, &nfsstats
.readdir_bios
);
5698 lastcookie
= cookie
;
5701 * Loop around doing readdir(plus) RPCs of size nm_readdirsize until
5702 * the buffer is full (or we hit EOF). Then put the remainder of the
5703 * results in the next buffer(s).
5705 nfsm_chain_null(&nmreq
);
5706 nfsm_chain_null(&nmrep
);
5707 while (nfs_dir_buf_freespace(bp
, rdirplus
) && !(ndbhp
->ndbh_flags
& NDB_FULL
)) {
5708 nfsm_chain_build_alloc_init(error
, &nmreq
,
5709 NFSX_FH(nfsvers
) + NFSX_READDIR(nfsvers
) + NFSX_UNSIGNED
);
5710 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
5711 if (nfsvers
== NFS_VER3
) {
5712 /* opaque values don't need swapping, but as long */
5713 /* as we are consistent about it, it should be ok */
5714 nfsm_chain_add_64(error
, &nmreq
, cookie
);
5715 nfsm_chain_add_64(error
, &nmreq
, dnp
->n_cookieverf
);
5717 nfsm_chain_add_32(error
, &nmreq
, cookie
);
5719 nfsm_chain_add_32(error
, &nmreq
, nmreaddirsize
);
5721 nfsm_chain_add_32(error
, &nmreq
, nmrsize
);
5722 nfsm_chain_build_done(error
, &nmreq
);
5723 nfs_node_unlock(dnp
);
5727 error
= nfs_request(dnp
, NULL
, &nmreq
,
5728 rdirplus
? NFSPROC_READDIRPLUS
: NFSPROC_READDIR
,
5729 ctx
, NULL
, &nmrep
, &xid
, &status
);
5731 if ((lockerror
= nfs_node_lock(dnp
)))
5735 if (nfsvers
== NFS_VER3
)
5736 nfsm_chain_postop_attr_update(error
, &nmrep
, dnp
, &xid
);
5739 if (nfsvers
== NFS_VER3
)
5740 nfsm_chain_get_64(error
, &nmrep
, dnp
->n_cookieverf
);
5741 nfsm_chain_get_32(error
, &nmrep
, more_entries
);
5744 nfs_node_unlock(dnp
);
5747 if (error
== NFSERR_NOTSUPP
) {
5748 /* oops... it doesn't look like readdirplus is supported */
5749 lck_mtx_lock(&nmp
->nm_lock
);
5750 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_RDIRPLUS
);
5751 lck_mtx_unlock(&nmp
->nm_lock
);
5759 /* loop through the entries packing them into the buffer */
5760 while (more_entries
) {
5761 if (nfsvers
== NFS_VER3
)
5762 nfsm_chain_get_64(error
, &nmrep
, fileno
);
5764 nfsm_chain_get_32(error
, &nmrep
, fileno
);
5765 nfsm_chain_get_32(error
, &nmrep
, namlen
);
5767 /* just truncate names that don't fit in direntry.d_name */
5772 if (namlen
> (sizeof(dp
->d_name
)-1)) {
5773 skiplen
= namlen
- sizeof(dp
->d_name
) + 1;
5774 namlen
= sizeof(dp
->d_name
) - 1;
5778 /* guess that fh size will be same as parent */
5779 fhlen
= rdirplus
? (1 + dnp
->n_fhsize
) : 0;
5780 xlen
= rdirplus
? (fhlen
+ sizeof(time_t)) : 0;
5781 attrlen
= rdirplus
? sizeof(struct nfs_vattr
) : 0;
5782 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
5783 space_needed
= reclen
+ attrlen
;
5784 space_free
= nfs_dir_buf_freespace(bp
, rdirplus
);
5785 if (space_needed
> space_free
) {
5787 * We still have entries to pack, but we've
5788 * run out of room in the current buffer.
5789 * So we need to move to the next buffer.
5790 * The block# for the next buffer is the
5791 * last cookie in the current buffer.
5794 ndbhp
->ndbh_flags
|= NDB_FULL
;
5795 nfs_buf_release(bp
, 0);
5798 error
= nfs_buf_get(dnp
, lastcookie
, NFS_DIRBLKSIZ
, vfs_context_thread(ctx
), NBLK_READ
, &bp
);
5800 /* initialize buffer */
5801 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5802 ndbhp
->ndbh_flags
= 0;
5803 ndbhp
->ndbh_count
= 0;
5804 ndbhp
->ndbh_entry_end
= sizeof(*ndbhp
);
5805 ndbhp
->ndbh_ncgen
= dnp
->n_ncgen
;
5806 space_free
= nfs_dir_buf_freespace(bp
, rdirplus
);
5807 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5808 /* increment with every buffer read */
5809 OSAddAtomic64(1, &nfsstats
.readdir_bios
);
5812 dp
->d_fileno
= fileno
;
5813 dp
->d_namlen
= namlen
;
5814 dp
->d_reclen
= reclen
;
5815 dp
->d_type
= DT_UNKNOWN
;
5816 nfsm_chain_get_opaque(error
, &nmrep
, namlen
, dp
->d_name
);
5818 dp
->d_name
[namlen
] = '\0';
5820 nfsm_chain_adv(error
, &nmrep
,
5821 nfsm_rndup(namlen
+ skiplen
) - nfsm_rndup(namlen
));
5822 if (nfsvers
== NFS_VER3
)
5823 nfsm_chain_get_64(error
, &nmrep
, cookie
);
5825 nfsm_chain_get_32(error
, &nmrep
, cookie
);
5827 dp
->d_seekoff
= cookie
;
5828 if (!bigcookies
&& (cookie
>> 32) && (nmp
== NFSTONMP(dnp
))) {
5829 /* we've got a big cookie, make sure flag is set */
5830 lck_mtx_lock(&nmp
->nm_lock
);
5831 nmp
->nm_state
|= NFSSTA_BIGCOOKIES
;
5832 lck_mtx_unlock(&nmp
->nm_lock
);
5836 nvattrp
= NFS_DIR_BUF_NVATTR(bp
, ndbhp
->ndbh_count
);
5837 /* check for attributes */
5838 nfsm_chain_get_32(error
, &nmrep
, attrflag
);
5841 /* grab attributes */
5842 error
= nfs_parsefattr(&nmrep
, NFS_VER3
, nvattrp
);
5844 dp
->d_type
= IFTODT(VTTOIF(nvattrp
->nva_type
));
5845 /* fileid is already in d_fileno, so stash xid in attrs */
5846 nvattrp
->nva_fileid
= savedxid
;
5848 /* mark the attributes invalid */
5849 bzero(nvattrp
, sizeof(struct nfs_vattr
));
5851 /* check for file handle */
5852 nfsm_chain_get_32(error
, &nmrep
, fhflag
);
5855 nfsm_chain_get_fh(error
, &nmrep
, NFS_VER3
, &fh
);
5857 fhlen
= fh
.fh_len
+ 1;
5858 xlen
= fhlen
+ sizeof(time_t);
5859 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
5860 space_needed
= reclen
+ attrlen
;
5861 if (space_needed
> space_free
) {
5862 /* didn't actually have the room... move on to next buffer */
5866 /* pack the file handle into the record */
5867 dp
->d_name
[dp
->d_namlen
+1] = fh
.fh_len
;
5868 bcopy(fh
.fh_data
, &dp
->d_name
[dp
->d_namlen
+2], fh
.fh_len
);
5870 /* mark the file handle invalid */
5872 fhlen
= fh
.fh_len
+ 1;
5873 xlen
= fhlen
+ sizeof(time_t);
5874 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
5875 bzero(&dp
->d_name
[dp
->d_namlen
+1], fhlen
);
5877 *(time_t*)(&dp
->d_name
[dp
->d_namlen
+1+fhlen
]) = now
.tv_sec
;
5878 dp
->d_reclen
= reclen
;
5880 padstart
= dp
->d_name
+ dp
->d_namlen
+ 1 + xlen
;
5881 ndbhp
->ndbh_count
++;
5882 lastcookie
= cookie
;
5883 /* advance to next direntry in buffer */
5884 dp
= NFS_DIRENTRY_NEXT(dp
);
5885 ndbhp
->ndbh_entry_end
= (char*)dp
- bp
->nb_data
;
5886 /* zero out the pad bytes */
5887 padlen
= (char*)dp
- padstart
;
5889 bzero(padstart
, padlen
);
5890 /* check for more entries */
5891 nfsm_chain_get_32(error
, &nmrep
, more_entries
);
5894 /* Finally, get the eof boolean */
5895 nfsm_chain_get_32(error
, &nmrep
, eof
);
5898 ndbhp
->ndbh_flags
|= (NDB_FULL
|NDB_EOF
);
5899 nfs_node_lock_force(dnp
);
5900 dnp
->n_eofcookie
= lastcookie
;
5901 nfs_node_unlock(dnp
);
5906 nfs_buf_release(bp
, 0);
5910 if ((lockerror
= nfs_node_lock(dnp
)))
5913 nfsm_chain_cleanup(&nmrep
);
5914 nfsm_chain_null(&nmreq
);
5917 if (bp_dropped
&& bp
)
5918 nfs_buf_release(bp
, 0);
5920 nfs_node_unlock(dnp
);
5921 nfsm_chain_cleanup(&nmreq
);
5922 nfsm_chain_cleanup(&nmrep
);
5923 return (bp_dropped
? NFSERR_DIRBUFDROPPED
: error
);
5927 * Silly rename. To make the NFS filesystem that is stateless look a little
5928 * more like the "ufs" a remove of an active vnode is translated to a rename
5929 * to a funny looking filename that is removed by nfs_vnop_inactive on the
5930 * nfsnode. There is the potential for another process on a different client
5931 * to create the same funny name between when the lookitup() fails and the
5932 * rename() completes, but...
5935 /* format of "random" silly names - includes a number and pid */
5936 /* (note: shouldn't exceed size of nfs_sillyrename.nsr_name) */
5937 #define NFS_SILLYNAME_FORMAT ".nfs.%08x.%04x"
5938 /* starting from zero isn't silly enough */
5939 static uint32_t nfs_sillyrename_number
= 0x20051025;
5945 struct componentname
*cnp
,
5948 struct nfs_sillyrename
*nsp
;
5953 struct nfsmount
*nmp
;
5955 nmp
= NFSTONMP(dnp
);
5956 if (nfs_mount_gone(nmp
))
5959 nfs_name_cache_purge(dnp
, np
, cnp
, ctx
);
5961 MALLOC_ZONE(nsp
, struct nfs_sillyrename
*,
5962 sizeof (struct nfs_sillyrename
), M_NFSREQ
, M_WAITOK
);
5965 cred
= vfs_context_ucred(ctx
);
5966 kauth_cred_ref(cred
);
5967 nsp
->nsr_cred
= cred
;
5969 error
= vnode_ref(NFSTOV(dnp
));
5973 /* Fudge together a funny name */
5974 pid
= vfs_context_pid(ctx
);
5975 num
= OSAddAtomic(1, &nfs_sillyrename_number
);
5976 nsp
->nsr_namlen
= snprintf(nsp
->nsr_name
, sizeof(nsp
->nsr_name
),
5977 NFS_SILLYNAME_FORMAT
, num
, (pid
& 0xffff));
5978 if (nsp
->nsr_namlen
>= (int)sizeof(nsp
->nsr_name
))
5979 nsp
->nsr_namlen
= sizeof(nsp
->nsr_name
) - 1;
5981 /* Try lookitups until we get one that isn't there */
5982 while (nfs_lookitup(dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, ctx
, NULL
) == 0) {
5983 num
= OSAddAtomic(1, &nfs_sillyrename_number
);
5984 nsp
->nsr_namlen
= snprintf(nsp
->nsr_name
, sizeof(nsp
->nsr_name
),
5985 NFS_SILLYNAME_FORMAT
, num
, (pid
& 0xffff));
5986 if (nsp
->nsr_namlen
>= (int)sizeof(nsp
->nsr_name
))
5987 nsp
->nsr_namlen
= sizeof(nsp
->nsr_name
) - 1;
5990 /* now, do the rename */
5991 error
= nmp
->nm_funcs
->nf_rename_rpc(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
,
5992 dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, ctx
);
5994 /* Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. */
5995 if (error
== ENOENT
)
5998 nfs_node_lock_force(dnp
);
5999 if (dnp
->n_flag
& NNEGNCENTRIES
) {
6000 dnp
->n_flag
&= ~NNEGNCENTRIES
;
6001 cache_purge_negatives(NFSTOV(dnp
));
6003 nfs_node_unlock(dnp
);
6005 FSDBG(267, dnp
, np
, num
, error
);
6008 error
= nfs_lookitup(dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, ctx
, &np
);
6009 nfs_node_lock_force(np
);
6010 np
->n_sillyrename
= nsp
;
6011 nfs_node_unlock(np
);
6014 vnode_rele(NFSTOV(dnp
));
6016 nsp
->nsr_cred
= NOCRED
;
6017 kauth_cred_unref(&cred
);
6018 FREE_ZONE(nsp
, sizeof(*nsp
), M_NFSREQ
);
6023 nfs3_lookup_rpc_async(
6028 struct nfsreq
**reqp
)
6030 struct nfsmount
*nmp
;
6031 struct nfsm_chain nmreq
;
6032 int error
= 0, nfsvers
;
6034 nmp
= NFSTONMP(dnp
);
6035 if (nfs_mount_gone(nmp
))
6037 nfsvers
= nmp
->nm_vers
;
6039 nfsm_chain_null(&nmreq
);
6041 nfsm_chain_build_alloc_init(error
, &nmreq
,
6042 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+ nfsm_rndup(namelen
));
6043 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
6044 nfsm_chain_add_name(error
, &nmreq
, name
, namelen
, nmp
);
6045 nfsm_chain_build_done(error
, &nmreq
);
6047 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_LOOKUP
,
6048 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, reqp
);
6050 nfsm_chain_cleanup(&nmreq
);
6055 nfs3_lookup_rpc_async_finish(
6057 __unused
char *name
,
6058 __unused
int namelen
,
6063 struct nfs_vattr
*nvap
)
6065 int error
= 0, lockerror
= ENOENT
, status
, nfsvers
, attrflag
;
6067 struct nfsmount
*nmp
;
6068 struct nfsm_chain nmrep
;
6070 nmp
= NFSTONMP(dnp
);
6073 nfsvers
= nmp
->nm_vers
;
6075 nfsm_chain_null(&nmrep
);
6077 error
= nfs_request_async_finish(req
, &nmrep
, xidp
, &status
);
6079 if ((lockerror
= nfs_node_lock(dnp
)))
6082 if (error
|| status
) {
6083 if (nfsvers
== NFS_VER3
)
6084 nfsm_chain_postop_attr_update(error
, &nmrep
, dnp
, &xid
);
6090 nfsmout_if(error
|| !fhp
|| !nvap
);
6092 /* get the file handle */
6093 nfsm_chain_get_fh(error
, &nmrep
, nfsvers
, fhp
);
6095 /* get the attributes */
6096 if (nfsvers
== NFS_VER3
) {
6097 nfsm_chain_postop_attr_get(error
, &nmrep
, attrflag
, nvap
);
6098 nfsm_chain_postop_attr_update(error
, &nmrep
, dnp
, &xid
);
6099 if (!error
&& !attrflag
)
6100 error
= nfs3_getattr_rpc(NULL
, NFSTOMP(dnp
), fhp
->fh_data
, fhp
->fh_len
, 0, ctx
, nvap
, xidp
);
6102 error
= nfs_parsefattr(&nmrep
, nfsvers
, nvap
);
6106 nfs_node_unlock(dnp
);
6107 nfsm_chain_cleanup(&nmrep
);
6112 * Look up a file name and optionally either update the file handle or
6113 * allocate an nfsnode, depending on the value of npp.
6114 * npp == NULL --> just do the lookup
6115 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
6117 * *npp != NULL --> update the file handle in the vnode
6128 nfsnode_t np
, newnp
= NULL
;
6131 struct nfsmount
*nmp
;
6132 struct nfs_vattr nvattr
;
6133 struct nfsreq rq
, *req
= &rq
;
6135 nmp
= NFSTONMP(dnp
);
6136 if (nfs_mount_gone(nmp
))
6139 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXNAME
) &&
6140 (namelen
> (int)nmp
->nm_fsattr
.nfsa_maxname
))
6141 return (ENAMETOOLONG
);
6143 NVATTR_INIT(&nvattr
);
6145 /* check for lookup of "." */
6146 if ((name
[0] == '.') && (namelen
== 1)) {
6147 /* skip lookup, we know who we are */
6153 error
= nmp
->nm_funcs
->nf_lookup_rpc_async(dnp
, name
, namelen
, ctx
, &req
);
6155 error
= nmp
->nm_funcs
->nf_lookup_rpc_async_finish(dnp
, name
, namelen
, ctx
, req
, &xid
, &fh
, &nvattr
);
6156 nfsmout_if(!npp
|| error
);
6160 if (fh
.fh_len
!= np
->n_fhsize
) {
6161 u_char
*oldbuf
= (np
->n_fhsize
> NFS_SMALLFH
) ? np
->n_fhp
: NULL
;
6162 if (fh
.fh_len
> NFS_SMALLFH
) {
6163 MALLOC_ZONE(np
->n_fhp
, u_char
*, fh
.fh_len
, M_NFSBIGFH
, M_WAITOK
);
6170 np
->n_fhp
= &np
->n_fh
[0];
6173 FREE_ZONE(oldbuf
, np
->n_fhsize
, M_NFSBIGFH
);
6175 bcopy(fh
.fh_data
, np
->n_fhp
, fh
.fh_len
);
6176 np
->n_fhsize
= fh
.fh_len
;
6177 nfs_node_lock_force(np
);
6178 error
= nfs_loadattrcache(np
, &nvattr
, &xid
, 0);
6179 nfs_node_unlock(np
);
6182 } else if (NFS_CMPFH(dnp
, fh
.fh_data
, fh
.fh_len
)) {
6183 nfs_node_lock_force(dnp
);
6184 if (dnp
->n_xid
<= xid
)
6185 error
= nfs_loadattrcache(dnp
, &nvattr
, &xid
, 0);
6186 nfs_node_unlock(dnp
);
6190 struct componentname cn
, *cnp
= &cn
;
6191 bzero(cnp
, sizeof(*cnp
));
6192 cnp
->cn_nameptr
= name
;
6193 cnp
->cn_namelen
= namelen
;
6194 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
,
6195 &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
6201 if (npp
&& !*npp
&& !error
)
6203 NVATTR_CLEANUP(&nvattr
);
6208 * set up and initialize a "._" file lookup structure used for
6209 * performing async lookups.
6212 nfs_dulookup_init(struct nfs_dulookup
*dulp
, nfsnode_t dnp
, const char *name
, int namelen
, vfs_context_t ctx
)
6214 int error
, du_namelen
;
6216 struct nfsmount
*nmp
= NFSTONMP(dnp
);
6218 /* check for ._ file in name cache */
6220 bzero(&dulp
->du_cn
, sizeof(dulp
->du_cn
));
6221 du_namelen
= namelen
+ 2;
6222 if (!nmp
|| NMFLAG(nmp
, NONEGNAMECACHE
))
6224 if ((namelen
>= 2) && (name
[0] == '.') && (name
[1] == '_'))
6226 if (du_namelen
>= (int)sizeof(dulp
->du_smallname
))
6227 MALLOC(dulp
->du_cn
.cn_nameptr
, char *, du_namelen
+ 1, M_TEMP
, M_WAITOK
);
6229 dulp
->du_cn
.cn_nameptr
= dulp
->du_smallname
;
6230 if (!dulp
->du_cn
.cn_nameptr
)
6232 dulp
->du_cn
.cn_namelen
= du_namelen
;
6233 snprintf(dulp
->du_cn
.cn_nameptr
, du_namelen
+ 1, "._%s", name
);
6234 dulp
->du_cn
.cn_nameptr
[du_namelen
] = '\0';
6235 dulp
->du_cn
.cn_nameiop
= LOOKUP
;
6236 dulp
->du_cn
.cn_flags
= MAKEENTRY
;
6238 error
= cache_lookup(NFSTOV(dnp
), &du_vp
, &dulp
->du_cn
);
6241 } else if (!error
) {
6242 nmp
= NFSTONMP(dnp
);
6243 if (nmp
&& (nmp
->nm_vers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
)) {
6244 /* if rdirplus, try dir buf cache lookup */
6245 nfsnode_t du_np
= NULL
;
6246 if (!nfs_dir_buf_cache_lookup(dnp
, &du_np
, &dulp
->du_cn
, ctx
, 0) && du_np
) {
6247 /* dir buf cache hit */
6248 du_vp
= NFSTOV(du_np
);
6254 dulp
->du_flags
|= NFS_DULOOKUP_DOIT
;
6259 * start an async "._" file lookup request
6262 nfs_dulookup_start(struct nfs_dulookup
*dulp
, nfsnode_t dnp
, vfs_context_t ctx
)
6264 struct nfsmount
*nmp
= NFSTONMP(dnp
);
6265 struct nfsreq
*req
= &dulp
->du_req
;
6267 if (!nmp
|| !(dulp
->du_flags
& NFS_DULOOKUP_DOIT
) || (dulp
->du_flags
& NFS_DULOOKUP_INPROG
))
6269 if (!nmp
->nm_funcs
->nf_lookup_rpc_async(dnp
, dulp
->du_cn
.cn_nameptr
,
6270 dulp
->du_cn
.cn_namelen
, ctx
, &req
))
6271 dulp
->du_flags
|= NFS_DULOOKUP_INPROG
;
6275 * finish an async "._" file lookup request and clean up the structure
6278 nfs_dulookup_finish(struct nfs_dulookup
*dulp
, nfsnode_t dnp
, vfs_context_t ctx
)
6280 struct nfsmount
*nmp
= NFSTONMP(dnp
);
6285 struct nfs_vattr nvattr
;
6287 if (!nmp
|| !(dulp
->du_flags
& NFS_DULOOKUP_INPROG
))
6290 NVATTR_INIT(&nvattr
);
6291 error
= nmp
->nm_funcs
->nf_lookup_rpc_async_finish(dnp
, dulp
->du_cn
.cn_nameptr
,
6292 dulp
->du_cn
.cn_namelen
, ctx
, &dulp
->du_req
, &xid
, &fh
, &nvattr
);
6293 dulp
->du_flags
&= ~NFS_DULOOKUP_INPROG
;
6294 if (error
== ENOENT
) {
6295 /* add a negative entry in the name cache */
6296 nfs_node_lock_force(dnp
);
6297 cache_enter(NFSTOV(dnp
), NULL
, &dulp
->du_cn
);
6298 dnp
->n_flag
|= NNEGNCENTRIES
;
6299 nfs_node_unlock(dnp
);
6300 } else if (!error
) {
6301 error
= nfs_nget(NFSTOMP(dnp
), dnp
, &dulp
->du_cn
, fh
.fh_data
, fh
.fh_len
,
6302 &nvattr
, &xid
, dulp
->du_req
.r_auth
, NG_MAKEENTRY
, &du_np
);
6304 nfs_node_unlock(du_np
);
6305 vnode_put(NFSTOV(du_np
));
6308 NVATTR_CLEANUP(&nvattr
);
6310 if (dulp
->du_flags
& NFS_DULOOKUP_INPROG
)
6311 nfs_request_async_cancel(&dulp
->du_req
);
6312 if (dulp
->du_cn
.cn_nameptr
&& (dulp
->du_cn
.cn_nameptr
!= dulp
->du_smallname
))
6313 FREE(dulp
->du_cn
.cn_nameptr
, M_TEMP
);
6318 * NFS Version 3 commit RPC
6328 struct nfsmount
*nmp
;
6329 int error
= 0, lockerror
, status
, wccpostattr
= 0, nfsvers
;
6330 struct timespec premtime
= { 0, 0 };
6331 u_int64_t xid
, newwverf
;
6333 struct nfsm_chain nmreq
, nmrep
;
6336 FSDBG(521, np
, offset
, count
, nmp
? nmp
->nm_state
: 0);
6337 if (nfs_mount_gone(nmp
))
6339 if (!(nmp
->nm_state
& NFSSTA_HASWRITEVERF
))
6341 nfsvers
= nmp
->nm_vers
;
6343 if (count
> UINT32_MAX
)
6348 nfsm_chain_null(&nmreq
);
6349 nfsm_chain_null(&nmrep
);
6351 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(NFS_VER3
));
6352 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
6353 nfsm_chain_add_64(error
, &nmreq
, offset
);
6354 nfsm_chain_add_32(error
, &nmreq
, count32
);
6355 nfsm_chain_build_done(error
, &nmreq
);
6357 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC_COMMIT
,
6358 current_thread(), cred
, NULL
, 0, &nmrep
, &xid
, &status
);
6359 if ((lockerror
= nfs_node_lock(np
)))
6361 /* can we do anything useful with the wcc info? */
6362 nfsm_chain_get_wcc_data(error
, &nmrep
, np
, &premtime
, &wccpostattr
, &xid
);
6364 nfs_node_unlock(np
);
6367 nfsm_chain_get_64(error
, &nmrep
, newwverf
);
6369 lck_mtx_lock(&nmp
->nm_lock
);
6370 if (nmp
->nm_verf
!= newwverf
)
6371 nmp
->nm_verf
= newwverf
;
6372 if (wverf
!= newwverf
)
6373 error
= NFSERR_STALEWRITEVERF
;
6374 lck_mtx_unlock(&nmp
->nm_lock
);
6376 nfsm_chain_cleanup(&nmreq
);
6377 nfsm_chain_cleanup(&nmrep
);
6384 __unused
struct vnop_blockmap_args
/* {
6385 struct vnodeop_desc *a_desc;
6400 * fsync vnode op. Just call nfs_flush().
6405 struct vnop_fsync_args
/* {
6406 struct vnodeop_desc *a_desc;
6409 vfs_context_t a_context;
6412 return (nfs_flush(VTONFS(ap
->a_vp
), ap
->a_waitfor
, vfs_context_thread(ap
->a_context
), 0));
6417 * Do an NFS pathconf RPC.
6422 struct nfs_fsattr
*nfsap
,
6426 int error
= 0, lockerror
, status
, nfsvers
;
6427 struct nfsm_chain nmreq
, nmrep
;
6428 struct nfsmount
*nmp
= NFSTONMP(np
);
6431 if (nfs_mount_gone(nmp
))
6433 nfsvers
= nmp
->nm_vers
;
6435 nfsm_chain_null(&nmreq
);
6436 nfsm_chain_null(&nmrep
);
6438 /* fetch pathconf info from server */
6439 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(NFS_VER3
));
6440 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
6441 nfsm_chain_build_done(error
, &nmreq
);
6443 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_PATHCONF
, ctx
, NULL
, &nmrep
, &xid
, &status
);
6444 if ((lockerror
= nfs_node_lock(np
)))
6446 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
6448 nfs_node_unlock(np
);
6451 nfsm_chain_get_32(error
, &nmrep
, nfsap
->nfsa_maxlink
);
6452 nfsm_chain_get_32(error
, &nmrep
, nfsap
->nfsa_maxname
);
6453 nfsap
->nfsa_flags
&= ~(NFS_FSFLAG_NO_TRUNC
|NFS_FSFLAG_CHOWN_RESTRICTED
|NFS_FSFLAG_CASE_INSENSITIVE
|NFS_FSFLAG_CASE_PRESERVING
);
6454 nfsm_chain_get_32(error
, &nmrep
, val
);
6456 nfsap
->nfsa_flags
|= NFS_FSFLAG_NO_TRUNC
;
6457 nfsm_chain_get_32(error
, &nmrep
, val
);
6459 nfsap
->nfsa_flags
|= NFS_FSFLAG_CHOWN_RESTRICTED
;
6460 nfsm_chain_get_32(error
, &nmrep
, val
);
6462 nfsap
->nfsa_flags
|= NFS_FSFLAG_CASE_INSENSITIVE
;
6463 nfsm_chain_get_32(error
, &nmrep
, val
);
6465 nfsap
->nfsa_flags
|= NFS_FSFLAG_CASE_PRESERVING
;
6466 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXLINK
);
6467 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXNAME
);
6468 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_NO_TRUNC
);
6469 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_CHOWN_RESTRICTED
);
6470 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_INSENSITIVE
);
6471 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_PRESERVING
);
6473 nfsm_chain_cleanup(&nmreq
);
6474 nfsm_chain_cleanup(&nmrep
);
6478 /* save pathconf info for NFSv3 mount */
6480 nfs3_pathconf_cache(struct nfsmount
*nmp
, struct nfs_fsattr
*nfsap
)
6482 nmp
->nm_fsattr
.nfsa_maxlink
= nfsap
->nfsa_maxlink
;
6483 nmp
->nm_fsattr
.nfsa_maxname
= nfsap
->nfsa_maxname
;
6484 nmp
->nm_fsattr
.nfsa_flags
&= ~(NFS_FSFLAG_NO_TRUNC
|NFS_FSFLAG_CHOWN_RESTRICTED
|NFS_FSFLAG_CASE_INSENSITIVE
|NFS_FSFLAG_CASE_PRESERVING
);
6485 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_NO_TRUNC
;
6486 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_CHOWN_RESTRICTED
;
6487 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_INSENSITIVE
;
6488 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_PRESERVING
;
6489 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXLINK
);
6490 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXNAME
);
6491 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_NO_TRUNC
);
6492 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CHOWN_RESTRICTED
);
6493 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CASE_INSENSITIVE
);
6494 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CASE_PRESERVING
);
6495 nmp
->nm_state
|= NFSSTA_GOTPATHCONF
;
6499 * Return POSIX pathconf information applicable to nfs.
6501 * The NFS V2 protocol doesn't support this, so just return EINVAL
6507 struct vnop_pathconf_args
/* {
6508 struct vnodeop_desc *a_desc;
6512 vfs_context_t a_context;
6515 vnode_t vp
= ap
->a_vp
;
6516 nfsnode_t np
= VTONFS(vp
);
6517 struct nfsmount
*nmp
;
6518 struct nfs_fsattr nfsa
, *nfsap
;
6520 uint64_t maxFileSize
;
6524 if (nfs_mount_gone(nmp
))
6527 switch (ap
->a_name
) {
6530 case _PC_CHOWN_RESTRICTED
:
6532 case _PC_CASE_SENSITIVE
:
6533 case _PC_CASE_PRESERVING
:
6535 case _PC_FILESIZEBITS
:
6536 if (nmp
->nm_vers
== NFS_VER2
) {
6541 case _PC_XATTR_SIZE_BITS
:
6542 /* Do we support xattrs natively? */
6543 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
)
6545 /* No... so just return an error */
6548 /* don't bother contacting the server if we know the answer */
6552 if (nmp
->nm_vers
== NFS_VER2
)
6555 lck_mtx_lock(&nmp
->nm_lock
);
6556 if (nmp
->nm_vers
== NFS_VER3
) {
6557 if (!(nmp
->nm_state
& NFSSTA_GOTPATHCONF
)) {
6558 /* no pathconf info cached */
6559 lck_mtx_unlock(&nmp
->nm_lock
);
6560 NFS_CLEAR_ATTRIBUTES(nfsa
.nfsa_bitmap
);
6561 error
= nfs3_pathconf_rpc(np
, &nfsa
, ap
->a_context
);
6565 if (nfs_mount_gone(nmp
))
6567 lck_mtx_lock(&nmp
->nm_lock
);
6568 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_HOMOGENEOUS
) {
6569 /* all files have the same pathconf info, */
6570 /* so cache a copy of the results */
6571 nfs3_pathconf_cache(nmp
, &nfsa
);
6575 nfsap
= &nmp
->nm_fsattr
;
6577 } else if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_HOMOGENEOUS
)) {
6578 /* no pathconf info cached */
6579 lck_mtx_unlock(&nmp
->nm_lock
);
6580 NFS_CLEAR_ATTRIBUTES(nfsa
.nfsa_bitmap
);
6581 error
= nfs4_pathconf_rpc(np
, &nfsa
, ap
->a_context
);
6585 if (nfs_mount_gone(nmp
))
6587 lck_mtx_lock(&nmp
->nm_lock
);
6590 nfsap
= &nmp
->nm_fsattr
;
6593 switch (ap
->a_name
) {
6595 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXLINK
))
6596 *ap
->a_retval
= nfsap
->nfsa_maxlink
;
6597 else if ((nmp
->nm_vers
== NFS_VER4
) && NFS_BITMAP_ISSET(np
->n_vattr
.nva_bitmap
, NFS_FATTR_MAXLINK
))
6598 *ap
->a_retval
= np
->n_vattr
.nva_maxlink
;
6603 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXNAME
))
6604 *ap
->a_retval
= nfsap
->nfsa_maxname
;
6608 case _PC_CHOWN_RESTRICTED
:
6609 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_CHOWN_RESTRICTED
))
6610 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_CHOWN_RESTRICTED
) ? 200112 /* _POSIX_CHOWN_RESTRICTED */ : 0;
6615 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_NO_TRUNC
))
6616 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_NO_TRUNC
) ? 200112 /* _POSIX_NO_TRUNC */ : 0;
6620 case _PC_CASE_SENSITIVE
:
6621 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_INSENSITIVE
))
6622 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_INSENSITIVE
) ? 0 : 1;
6626 case _PC_CASE_PRESERVING
:
6627 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_PRESERVING
))
6628 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_PRESERVING
) ? 1 : 0;
6632 case _PC_XATTR_SIZE_BITS
: /* same as file size bits if named attrs supported */
6633 case _PC_FILESIZEBITS
:
6634 if (!NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXFILESIZE
)) {
6639 maxFileSize
= nfsap
->nfsa_maxfilesize
;
6641 if (maxFileSize
& 0xffffffff00000000ULL
) {
6645 if (maxFileSize
& 0xffff0000) {
6649 if (maxFileSize
& 0xff00) {
6653 if (maxFileSize
& 0xf0) {
6657 if (maxFileSize
& 0xc) {
6661 if (maxFileSize
& 0x2) {
6664 *ap
->a_retval
= nbits
;
6670 lck_mtx_unlock(&nmp
->nm_lock
);
6676 * Read wrapper for special devices.
6680 struct vnop_read_args
/* {
6681 struct vnodeop_desc *a_desc;
6685 vfs_context_t a_context;
6688 nfsnode_t np
= VTONFS(ap
->a_vp
);
6695 if ((error
= nfs_node_lock(np
)))
6699 np
->n_atim
.tv_sec
= now
.tv_sec
;
6700 np
->n_atim
.tv_nsec
= now
.tv_usec
* 1000;
6701 nfs_node_unlock(np
);
6702 return (VOCALL(spec_vnodeop_p
, VOFFSET(vnop_read
), ap
));
6706 * Write wrapper for special devices.
6710 struct vnop_write_args
/* {
6711 struct vnodeop_desc *a_desc;
6715 vfs_context_t a_context;
6718 nfsnode_t np
= VTONFS(ap
->a_vp
);
6725 if ((error
= nfs_node_lock(np
)))
6729 np
->n_mtim
.tv_sec
= now
.tv_sec
;
6730 np
->n_mtim
.tv_nsec
= now
.tv_usec
* 1000;
6731 nfs_node_unlock(np
);
6732 return (VOCALL(spec_vnodeop_p
, VOFFSET(vnop_write
), ap
));
6736 * Close wrapper for special devices.
6738 * Update the times on the nfsnode then do device close.
6742 struct vnop_close_args
/* {
6743 struct vnodeop_desc *a_desc;
6746 vfs_context_t a_context;
6749 vnode_t vp
= ap
->a_vp
;
6750 nfsnode_t np
= VTONFS(vp
);
6751 struct vnode_attr vattr
;
6755 if ((error
= nfs_node_lock(np
)))
6757 if (np
->n_flag
& (NACC
| NUPD
)) {
6759 if (!vnode_isinuse(vp
, 0) && (mp
= vnode_mount(vp
)) && !vfs_isrdonly(mp
)) {
6761 if (np
->n_flag
& NACC
) {
6762 vattr
.va_access_time
= np
->n_atim
;
6763 VATTR_SET_ACTIVE(&vattr
, va_access_time
);
6765 if (np
->n_flag
& NUPD
) {
6766 vattr
.va_modify_time
= np
->n_mtim
;
6767 VATTR_SET_ACTIVE(&vattr
, va_modify_time
);
6769 nfs_node_unlock(np
);
6770 vnode_setattr(vp
, &vattr
, ap
->a_context
);
6772 nfs_node_unlock(np
);
6775 nfs_node_unlock(np
);
6777 return (VOCALL(spec_vnodeop_p
, VOFFSET(vnop_close
), ap
));
6781 extern vnop_t
**fifo_vnodeop_p
;
6784 * Read wrapper for fifos.
6788 struct vnop_read_args
/* {
6789 struct vnodeop_desc *a_desc;
6793 vfs_context_t a_context;
6796 nfsnode_t np
= VTONFS(ap
->a_vp
);
6803 if ((error
= nfs_node_lock(np
)))
6807 np
->n_atim
.tv_sec
= now
.tv_sec
;
6808 np
->n_atim
.tv_nsec
= now
.tv_usec
* 1000;
6809 nfs_node_unlock(np
);
6810 return (VOCALL(fifo_vnodeop_p
, VOFFSET(vnop_read
), ap
));
6814 * Write wrapper for fifos.
6818 struct vnop_write_args
/* {
6819 struct vnodeop_desc *a_desc;
6823 vfs_context_t a_context;
6826 nfsnode_t np
= VTONFS(ap
->a_vp
);
6833 if ((error
= nfs_node_lock(np
)))
6837 np
->n_mtim
.tv_sec
= now
.tv_sec
;
6838 np
->n_mtim
.tv_nsec
= now
.tv_usec
* 1000;
6839 nfs_node_unlock(np
);
6840 return (VOCALL(fifo_vnodeop_p
, VOFFSET(vnop_write
), ap
));
6844 * Close wrapper for fifos.
6846 * Update the times on the nfsnode then do fifo close.
6850 struct vnop_close_args
/* {
6851 struct vnodeop_desc *a_desc;
6854 vfs_context_t a_context;
6857 vnode_t vp
= ap
->a_vp
;
6858 nfsnode_t np
= VTONFS(vp
);
6859 struct vnode_attr vattr
;
6864 if ((error
= nfs_node_lock(np
)))
6866 if (np
->n_flag
& (NACC
| NUPD
)) {
6868 if (np
->n_flag
& NACC
) {
6869 np
->n_atim
.tv_sec
= now
.tv_sec
;
6870 np
->n_atim
.tv_nsec
= now
.tv_usec
* 1000;
6872 if (np
->n_flag
& NUPD
) {
6873 np
->n_mtim
.tv_sec
= now
.tv_sec
;
6874 np
->n_mtim
.tv_nsec
= now
.tv_usec
* 1000;
6877 if (!vnode_isinuse(vp
, 1) && (mp
= vnode_mount(vp
)) && !vfs_isrdonly(mp
)) {
6879 if (np
->n_flag
& NACC
) {
6880 vattr
.va_access_time
= np
->n_atim
;
6881 VATTR_SET_ACTIVE(&vattr
, va_access_time
);
6883 if (np
->n_flag
& NUPD
) {
6884 vattr
.va_modify_time
= np
->n_mtim
;
6885 VATTR_SET_ACTIVE(&vattr
, va_modify_time
);
6887 nfs_node_unlock(np
);
6888 vnode_setattr(vp
, &vattr
, ap
->a_context
);
6890 nfs_node_unlock(np
);
6893 nfs_node_unlock(np
);
6895 return (VOCALL(fifo_vnodeop_p
, VOFFSET(vnop_close
), ap
));
6902 struct vnop_ioctl_args
/* {
6903 struct vnodeop_desc *a_desc;
6905 u_int32_t a_command;
6908 vfs_context_t a_context;
6911 vfs_context_t ctx
= ap
->a_context
;
6912 vnode_t vp
= ap
->a_vp
;
6913 struct nfsmount
*mp
= VTONMP(vp
);
6914 struct user_nfs_gss_principal gprinc
;
6921 switch (ap
->a_command
) {
6924 if (vnode_vfsisrdonly(vp
))
6926 error
= nfs_flush(VTONFS(vp
), MNT_WAIT
, vfs_context_thread(ctx
), 0);
6928 case NFS_FSCTL_DESTROY_CRED
:
6929 if (!auth_is_kerberized(mp
->nm_auth
))
6931 error
= nfs_gss_clnt_ctx_remove(mp
, vfs_context_ucred(ctx
));
6933 case NFS_FSCTL_SET_CRED
:
6934 if (!auth_is_kerberized(mp
->nm_auth
))
6936 NFS_DBG(NFS_FAC_GSS
, 7, "Enter NFS_FSCTL_SET_CRED (proc %d) data = %p\n", vfs_context_is64bit(ctx
), (void *)ap
->a_data
);
6937 if (vfs_context_is64bit(ctx
)) {
6938 gprinc
= *(struct user_nfs_gss_principal
*)ap
->a_data
;
6940 struct nfs_gss_principal
*tp
;
6941 tp
= (struct nfs_gss_principal
*)ap
->a_data
;
6942 gprinc
.princlen
= tp
->princlen
;
6943 gprinc
.nametype
= tp
->nametype
;
6944 gprinc
.principal
= CAST_USER_ADDR_T(tp
->principal
);
6946 if (gprinc
.princlen
> MAXPATHLEN
)
6948 NFS_DBG(NFS_FAC_GSS
, 7, "Received principal length %d name type = %d\n", gprinc
.princlen
, gprinc
.nametype
);
6950 MALLOC(p
, uint8_t *, gprinc
.princlen
+1, M_TEMP
, M_WAITOK
|M_ZERO
);
6953 error
= copyin(gprinc
.principal
, p
, gprinc
.princlen
);
6955 NFS_DBG(NFS_FAC_GSS
, 7, "NFS_FSCTL_SET_CRED could not copy in princiapl data of len %d: %d\n",
6956 gprinc
.princlen
, error
);
6960 NFS_DBG(NFS_FAC_GSS
, 7, "Seting credential to principal %s\n", p
);
6961 error
= nfs_gss_clnt_ctx_set_principal(mp
, ctx
, p
, gprinc
.princlen
, gprinc
.nametype
);
6962 NFS_DBG(NFS_FAC_GSS
, 7, "Seting credential to principal %s returned %d\n", p
, error
);
6965 case NFS_FSCTL_GET_CRED
:
6966 if (!auth_is_kerberized(mp
->nm_auth
))
6968 error
= nfs_gss_clnt_ctx_get_principal(mp
, ctx
, &gprinc
);
6971 if (vfs_context_is64bit(ctx
)) {
6972 struct user_nfs_gss_principal
*upp
= (struct user_nfs_gss_principal
*)ap
->a_data
;
6973 len
= upp
->princlen
;
6974 if (gprinc
.princlen
< len
)
6975 len
= gprinc
.princlen
;
6976 upp
->princlen
= gprinc
.princlen
;
6977 upp
->nametype
= gprinc
.nametype
;
6978 upp
->flags
= gprinc
.flags
;
6979 if (gprinc
.principal
)
6980 error
= copyout((void *)gprinc
.principal
, upp
->principal
, len
);
6982 upp
->principal
= USER_ADDR_NULL
;
6984 struct nfs_gss_principal
*u32pp
= (struct nfs_gss_principal
*)ap
->a_data
;
6985 len
= u32pp
->princlen
;
6986 if (gprinc
.princlen
< len
)
6987 len
= gprinc
.princlen
;
6988 u32pp
->princlen
= gprinc
.princlen
;
6989 u32pp
->nametype
= gprinc
.nametype
;
6990 u32pp
->flags
= gprinc
.flags
;
6991 if (gprinc
.principal
)
6992 error
= copyout((void *)gprinc
.principal
, u32pp
->principal
, len
);
6994 u32pp
->principal
= (user32_addr_t
)0;
6997 NFS_DBG(NFS_FAC_GSS
, 7, "NFS_FSCTL_GET_CRED could not copy out princiapl data of len %d: %d\n",
6998 gprinc
.princlen
, error
);
7000 FREE(gprinc
.principal
, M_TEMP
);
7009 __unused
struct vnop_select_args
/* {
7010 struct vnodeop_desc *a_desc;
7015 vfs_context_t a_context;
7020 * We were once bogusly seltrue() which returns 1. Is this right?
7026 * vnode OP for pagein using UPL
7028 * No buffer I/O, just RPCs straight into the mapped pages.
7032 struct vnop_pagein_args
/* {
7033 struct vnodeop_desc *a_desc;
7036 vm_offset_t a_pl_offset;
7040 vfs_context_t a_context;
7043 vnode_t vp
= ap
->a_vp
;
7044 upl_t pl
= ap
->a_pl
;
7045 size_t size
= ap
->a_size
;
7046 off_t f_offset
= ap
->a_f_offset
;
7047 vm_offset_t pl_offset
= ap
->a_pl_offset
;
7048 int flags
= ap
->a_flags
;
7051 nfsnode_t np
= VTONFS(vp
);
7052 size_t nmrsize
, iosize
, txsize
, rxsize
, retsize
;
7054 struct nfsmount
*nmp
;
7056 vm_offset_t ioaddr
, rxaddr
;
7058 char uio_buf
[ UIO_SIZEOF(1) ];
7059 int nofreeupl
= flags
& UPL_NOCOMMIT
;
7060 upl_page_info_t
*plinfo
;
7061 #define MAXPAGINGREQS 16 /* max outstanding RPCs for pagein/pageout */
7062 struct nfsreq
*req
[MAXPAGINGREQS
];
7063 int nextsend
, nextwait
;
7064 uint32_t stategenid
= 0, restart
= 0;
7067 FSDBG(322, np
, f_offset
, size
, flags
);
7068 if (pl
== (upl_t
)NULL
)
7069 panic("nfs_pagein: no upl");
7072 printf("nfs_pagein: invalid size %ld", size
);
7074 (void) ubc_upl_abort_range(pl
, pl_offset
, size
, 0);
7077 if (f_offset
< 0 || f_offset
>= (off_t
)np
->n_size
|| (f_offset
& PAGE_MASK_64
)) {
7079 ubc_upl_abort_range(pl
, pl_offset
, size
,
7080 UPL_ABORT_ERROR
| UPL_ABORT_FREE_ON_EMPTY
);
7084 thd
= vfs_context_thread(ap
->a_context
);
7085 cred
= ubc_getcred(vp
);
7086 if (!IS_VALID_CRED(cred
))
7087 cred
= vfs_context_ucred(ap
->a_context
);
7089 uio
= uio_createwithbuffer(1, f_offset
, UIO_SYSSPACE
, UIO_READ
,
7090 &uio_buf
, sizeof(uio_buf
));
7093 if (nfs_mount_gone(nmp
)) {
7095 ubc_upl_abort_range(pl
, pl_offset
, size
,
7096 UPL_ABORT_ERROR
| UPL_ABORT_FREE_ON_EMPTY
);
7099 nmrsize
= nmp
->nm_rsize
;
7101 plinfo
= ubc_upl_pageinfo(pl
);
7102 kret
= ubc_upl_map(pl
, &ioaddr
);
7103 if (kret
!= KERN_SUCCESS
)
7104 panic("nfs_vnop_pagein: ubc_upl_map() failed with (%d)", kret
);
7105 ioaddr
+= pl_offset
;
7108 if (nmp
->nm_vers
>= NFS_VER4
)
7109 stategenid
= nmp
->nm_stategenid
;
7110 txsize
= rxsize
= size
;
7111 txoffset
= f_offset
;
7114 bzero(req
, sizeof(req
));
7115 nextsend
= nextwait
= 0;
7117 if (np
->n_flag
& NREVOKE
) {
7121 /* send requests while we need to and have available slots */
7122 while ((txsize
> 0) && (req
[nextsend
] == NULL
)) {
7123 iosize
= MIN(nmrsize
, txsize
);
7124 if ((error
= nmp
->nm_funcs
->nf_read_rpc_async(np
, txoffset
, iosize
, thd
, cred
, NULL
, &req
[nextsend
]))) {
7125 req
[nextsend
] = NULL
;
7130 nextsend
= (nextsend
+ 1) % MAXPAGINGREQS
;
7132 /* wait while we need to and break out if more requests to send */
7133 while ((rxsize
> 0) && req
[nextwait
]) {
7134 iosize
= retsize
= MIN(nmrsize
, rxsize
);
7135 uio_reset(uio
, uio_offset(uio
), UIO_SYSSPACE
, UIO_READ
);
7136 uio_addiov(uio
, CAST_USER_ADDR_T(rxaddr
), iosize
);
7137 FSDBG(322, uio_offset(uio
), uio_resid(uio
), rxaddr
, rxsize
);
7139 upl_ubc_alias_set(pl
, (uintptr_t) current_thread(), (uintptr_t) 2);
7140 #endif /* UPL_DEBUG */
7141 OSAddAtomic64(1, &nfsstats
.pageins
);
7142 error
= nmp
->nm_funcs
->nf_read_rpc_async_finish(np
, req
[nextwait
], uio
, &retsize
, NULL
);
7143 req
[nextwait
] = NULL
;
7144 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
7145 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
)) {
7146 lck_mtx_lock(&nmp
->nm_lock
);
7147 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
7148 NP(np
, "nfs_vnop_pagein: error %d, initiating recovery", error
);
7149 nfs_need_recover(nmp
, error
);
7151 lck_mtx_unlock(&nmp
->nm_lock
);
7156 FSDBG(322, uio_offset(uio
), uio_resid(uio
), error
, -1);
7159 if (retsize
< iosize
) {
7160 /* Just zero fill the rest of the valid area. */
7161 int zcnt
= iosize
- retsize
;
7162 bzero((char *)rxaddr
+ retsize
, zcnt
);
7163 FSDBG(324, uio_offset(uio
), retsize
, zcnt
, rxaddr
);
7164 uio_update(uio
, zcnt
);
7171 } while (!error
&& (txsize
|| rxsize
));
7177 /* cancel any outstanding requests */
7178 while (req
[nextwait
]) {
7179 nfs_request_async_cancel(req
[nextwait
]);
7180 req
[nextwait
] = NULL
;
7181 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
7183 if (np
->n_flag
& NREVOKE
) {
7185 } else if (restart
) {
7186 if (restart
<= nfs_mount_state_max_restarts(nmp
)) { /* guard against no progress */
7187 if (error
== NFSERR_GRACE
)
7188 tsleep(&nmp
->nm_state
, (PZERO
-1), "nfsgrace", 2*hz
);
7189 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
)))
7192 NP(np
, "nfs_pagein: too many restarts, aborting");
7201 ubc_upl_abort_range(pl
, pl_offset
, size
,
7203 UPL_ABORT_FREE_ON_EMPTY
);
7205 ubc_upl_commit_range(pl
, pl_offset
, size
,
7206 UPL_COMMIT_CLEAR_DIRTY
|
7207 UPL_COMMIT_FREE_ON_EMPTY
);
7214 * the following are needed only by nfs_pageout to know how to handle errors
7215 * see nfs_pageout comments on explanation of actions.
7216 * the errors here are copied from errno.h and errors returned by servers
7217 * are expected to match the same numbers here. If not, our actions maybe
7220 char nfs_pageouterrorhandler(int);
7221 enum actiontype
{NOACTION
, DUMP
, DUMPANDLOG
, RETRY
, SEVER
};
7222 #define NFS_ELAST 88
7223 static u_char errorcount
[NFS_ELAST
+1]; /* better be zeros when initialized */
7224 static const char errortooutcome
[NFS_ELAST
+1] = {
7226 DUMP
, /* EPERM 1 Operation not permitted */
7227 DUMP
, /* ENOENT 2 No such file or directory */
7228 DUMPANDLOG
, /* ESRCH 3 No such process */
7229 RETRY
, /* EINTR 4 Interrupted system call */
7230 DUMP
, /* EIO 5 Input/output error */
7231 DUMP
, /* ENXIO 6 Device not configured */
7232 DUMPANDLOG
, /* E2BIG 7 Argument list too long */
7233 DUMPANDLOG
, /* ENOEXEC 8 Exec format error */
7234 DUMPANDLOG
, /* EBADF 9 Bad file descriptor */
7235 DUMPANDLOG
, /* ECHILD 10 No child processes */
7236 DUMPANDLOG
, /* EDEADLK 11 Resource deadlock avoided - was EAGAIN */
7237 RETRY
, /* ENOMEM 12 Cannot allocate memory */
7238 DUMP
, /* EACCES 13 Permission denied */
7239 DUMPANDLOG
, /* EFAULT 14 Bad address */
7240 DUMPANDLOG
, /* ENOTBLK 15 POSIX - Block device required */
7241 RETRY
, /* EBUSY 16 Device busy */
7242 DUMP
, /* EEXIST 17 File exists */
7243 DUMP
, /* EXDEV 18 Cross-device link */
7244 DUMP
, /* ENODEV 19 Operation not supported by device */
7245 DUMP
, /* ENOTDIR 20 Not a directory */
7246 DUMP
, /* EISDIR 21 Is a directory */
7247 DUMP
, /* EINVAL 22 Invalid argument */
7248 DUMPANDLOG
, /* ENFILE 23 Too many open files in system */
7249 DUMPANDLOG
, /* EMFILE 24 Too many open files */
7250 DUMPANDLOG
, /* ENOTTY 25 Inappropriate ioctl for device */
7251 DUMPANDLOG
, /* ETXTBSY 26 Text file busy - POSIX */
7252 DUMP
, /* EFBIG 27 File too large */
7253 DUMP
, /* ENOSPC 28 No space left on device */
7254 DUMPANDLOG
, /* ESPIPE 29 Illegal seek */
7255 DUMP
, /* EROFS 30 Read-only file system */
7256 DUMP
, /* EMLINK 31 Too many links */
7257 RETRY
, /* EPIPE 32 Broken pipe */
7259 DUMPANDLOG
, /* EDOM 33 Numerical argument out of domain */
7260 DUMPANDLOG
, /* ERANGE 34 Result too large */
7261 RETRY
, /* EAGAIN/EWOULDBLOCK 35 Resource temporarily unavailable */
7262 DUMPANDLOG
, /* EINPROGRESS 36 Operation now in progress */
7263 DUMPANDLOG
, /* EALREADY 37 Operation already in progress */
7264 /* ipc/network software -- argument errors */
7265 DUMPANDLOG
, /* ENOTSOC 38 Socket operation on non-socket */
7266 DUMPANDLOG
, /* EDESTADDRREQ 39 Destination address required */
7267 DUMPANDLOG
, /* EMSGSIZE 40 Message too long */
7268 DUMPANDLOG
, /* EPROTOTYPE 41 Protocol wrong type for socket */
7269 DUMPANDLOG
, /* ENOPROTOOPT 42 Protocol not available */
7270 DUMPANDLOG
, /* EPROTONOSUPPORT 43 Protocol not supported */
7271 DUMPANDLOG
, /* ESOCKTNOSUPPORT 44 Socket type not supported */
7272 DUMPANDLOG
, /* ENOTSUP 45 Operation not supported */
7273 DUMPANDLOG
, /* EPFNOSUPPORT 46 Protocol family not supported */
7274 DUMPANDLOG
, /* EAFNOSUPPORT 47 Address family not supported by protocol family */
7275 DUMPANDLOG
, /* EADDRINUSE 48 Address already in use */
7276 DUMPANDLOG
, /* EADDRNOTAVAIL 49 Can't assign requested address */
7277 /* ipc/network software -- operational errors */
7278 RETRY
, /* ENETDOWN 50 Network is down */
7279 RETRY
, /* ENETUNREACH 51 Network is unreachable */
7280 RETRY
, /* ENETRESET 52 Network dropped connection on reset */
7281 RETRY
, /* ECONNABORTED 53 Software caused connection abort */
7282 RETRY
, /* ECONNRESET 54 Connection reset by peer */
7283 RETRY
, /* ENOBUFS 55 No buffer space available */
7284 RETRY
, /* EISCONN 56 Socket is already connected */
7285 RETRY
, /* ENOTCONN 57 Socket is not connected */
7286 RETRY
, /* ESHUTDOWN 58 Can't send after socket shutdown */
7287 RETRY
, /* ETOOMANYREFS 59 Too many references: can't splice */
7288 RETRY
, /* ETIMEDOUT 60 Operation timed out */
7289 RETRY
, /* ECONNREFUSED 61 Connection refused */
7291 DUMPANDLOG
, /* ELOOP 62 Too many levels of symbolic links */
7292 DUMP
, /* ENAMETOOLONG 63 File name too long */
7293 RETRY
, /* EHOSTDOWN 64 Host is down */
7294 RETRY
, /* EHOSTUNREACH 65 No route to host */
7295 DUMP
, /* ENOTEMPTY 66 Directory not empty */
7297 DUMPANDLOG
, /* PROCLIM 67 Too many processes */
7298 DUMPANDLOG
, /* EUSERS 68 Too many users */
7299 DUMPANDLOG
, /* EDQUOT 69 Disc quota exceeded */
7300 /* Network File System */
7301 DUMP
, /* ESTALE 70 Stale NFS file handle */
7302 DUMP
, /* EREMOTE 71 Too many levels of remote in path */
7303 DUMPANDLOG
, /* EBADRPC 72 RPC struct is bad */
7304 DUMPANDLOG
, /* ERPCMISMATCH 73 RPC version wrong */
7305 DUMPANDLOG
, /* EPROGUNAVAIL 74 RPC prog. not avail */
7306 DUMPANDLOG
, /* EPROGMISMATCH 75 Program version wrong */
7307 DUMPANDLOG
, /* EPROCUNAVAIL 76 Bad procedure for program */
7309 DUMPANDLOG
, /* ENOLCK 77 No locks available */
7310 DUMPANDLOG
, /* ENOSYS 78 Function not implemented */
7311 DUMPANDLOG
, /* EFTYPE 79 Inappropriate file type or format */
7312 DUMPANDLOG
, /* EAUTH 80 Authentication error */
7313 DUMPANDLOG
, /* ENEEDAUTH 81 Need authenticator */
7314 /* Intelligent device errors */
7315 DUMPANDLOG
, /* EPWROFF 82 Device power is off */
7316 DUMPANDLOG
, /* EDEVERR 83 Device error, e.g. paper out */
7317 DUMPANDLOG
, /* EOVERFLOW 84 Value too large to be stored in data type */
7318 /* Program loading errors */
7319 DUMPANDLOG
, /* EBADEXEC 85 Bad executable */
7320 DUMPANDLOG
, /* EBADARCH 86 Bad CPU type in executable */
7321 DUMPANDLOG
, /* ESHLIBVERS 87 Shared library version mismatch */
7322 DUMPANDLOG
, /* EBADMACHO 88 Malformed Macho file */
7326 nfs_pageouterrorhandler(int error
)
7328 if (error
> NFS_ELAST
)
7331 return(errortooutcome
[error
]);
7336 * vnode OP for pageout using UPL
7338 * No buffer I/O, just RPCs straight from the mapped pages.
7339 * File size changes are not permitted in pageout.
7343 struct vnop_pageout_args
/* {
7344 struct vnodeop_desc *a_desc;
7347 vm_offset_t a_pl_offset;
7351 vfs_context_t a_context;
7354 vnode_t vp
= ap
->a_vp
;
7355 upl_t pl
= ap
->a_pl
;
7356 size_t size
= ap
->a_size
;
7357 off_t f_offset
= ap
->a_f_offset
;
7358 vm_offset_t pl_offset
= ap
->a_pl_offset
;
7359 int flags
= ap
->a_flags
;
7360 nfsnode_t np
= VTONFS(vp
);
7364 struct nfsmount
*nmp
= VTONMP(vp
);
7366 int error
= 0, iomode
;
7367 off_t off
, txoffset
, rxoffset
;
7368 vm_offset_t ioaddr
, txaddr
, rxaddr
;
7370 char uio_buf
[ UIO_SIZEOF(1) ];
7371 int nofreeupl
= flags
& UPL_NOCOMMIT
;
7372 size_t nmwsize
, biosize
, iosize
, pgsize
, txsize
, rxsize
, xsize
, remsize
;
7373 struct nfsreq
*req
[MAXPAGINGREQS
];
7374 int nextsend
, nextwait
, wverfset
, commit
;
7375 uint64_t wverf
, wverf2
;
7376 uint32_t stategenid
= 0, vrestart
= 0, restart
= 0, vrestarts
= 0, restarts
= 0;
7379 FSDBG(323, f_offset
, size
, pl
, pl_offset
);
7381 if (pl
== (upl_t
)NULL
)
7382 panic("nfs_pageout: no upl");
7385 printf("nfs_pageout: invalid size %ld", size
);
7387 ubc_upl_abort_range(pl
, pl_offset
, size
, 0);
7393 ubc_upl_abort(pl
, UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
);
7396 biosize
= nmp
->nm_biosize
;
7397 nmwsize
= nmp
->nm_wsize
;
7399 nfs_data_lock_noupdate(np
, NFS_DATA_LOCK_SHARED
);
7402 * Check to see whether the buffer is incore.
7403 * If incore and not busy, invalidate it from the cache.
7405 for (iosize
= 0; iosize
< size
; iosize
+= xsize
) {
7406 off
= f_offset
+ iosize
;
7407 /* need make sure we do things on block boundaries */
7408 xsize
= biosize
- (off
% biosize
);
7409 if (off
+ xsize
> f_offset
+ size
)
7410 xsize
= f_offset
+ size
- off
;
7411 lbn
= (daddr64_t
)(off
/ biosize
);
7412 lck_mtx_lock(nfs_buf_mutex
);
7413 if ((bp
= nfs_buf_incore(np
, lbn
))) {
7414 FSDBG(323, off
, bp
, bp
->nb_lflags
, bp
->nb_flags
);
7415 if (nfs_buf_acquire(bp
, NBAC_NOWAIT
, 0, 0)) {
7416 lck_mtx_unlock(nfs_buf_mutex
);
7417 nfs_data_unlock_noupdate(np
);
7418 /* no panic. just tell vm we are busy */
7420 ubc_upl_abort_range(pl
, pl_offset
, size
, 0);
7423 if (bp
->nb_dirtyend
> 0) {
7425 * if there's a dirty range in the buffer, check
7426 * to see if it extends beyond the pageout region
7428 * if the dirty region lies completely within the
7429 * pageout region, we just invalidate the buffer
7430 * because it's all being written out now anyway.
7432 * if any of the dirty region lies outside the
7433 * pageout region, we'll try to clip the dirty
7434 * region to eliminate the portion that's being
7435 * paged out. If that's not possible, because
7436 * the dirty region extends before and after the
7437 * pageout region, then we'll just return EBUSY.
7439 off_t boff
, start
, end
;
7443 /* clip end to EOF */
7444 if (end
> (off_t
)np
->n_size
)
7448 if ((bp
->nb_dirtyoff
< start
) &&
7449 (bp
->nb_dirtyend
> end
)) {
7451 * not gonna be able to clip the dirty region
7453 * But before returning the bad news, move the
7454 * buffer to the start of the delwri list and
7455 * give the list a push to try to flush the
7458 FSDBG(323, np
, bp
, 0xd00deebc, EBUSY
);
7459 nfs_buf_remfree(bp
);
7460 TAILQ_INSERT_HEAD(&nfsbufdelwri
, bp
, nb_free
);
7463 nfs_buf_delwri_push(1);
7464 lck_mtx_unlock(nfs_buf_mutex
);
7465 nfs_data_unlock_noupdate(np
);
7467 ubc_upl_abort_range(pl
, pl_offset
, size
, 0);
7470 if ((bp
->nb_dirtyoff
< start
) ||
7471 (bp
->nb_dirtyend
> end
)) {
7472 /* clip dirty region, if necessary */
7473 if (bp
->nb_dirtyoff
< start
)
7474 bp
->nb_dirtyend
= min(bp
->nb_dirtyend
, start
);
7475 if (bp
->nb_dirtyend
> end
)
7476 bp
->nb_dirtyoff
= max(bp
->nb_dirtyoff
, end
);
7477 FSDBG(323, bp
, bp
->nb_dirtyoff
, bp
->nb_dirtyend
, 0xd00dee00);
7478 /* we're leaving this block dirty */
7480 lck_mtx_unlock(nfs_buf_mutex
);
7484 nfs_buf_remfree(bp
);
7485 lck_mtx_unlock(nfs_buf_mutex
);
7486 SET(bp
->nb_flags
, NB_INVAL
);
7487 nfs_node_lock_force(np
);
7488 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
7489 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
7490 np
->n_needcommitcnt
--;
7491 CHECK_NEEDCOMMITCNT(np
);
7493 nfs_node_unlock(np
);
7494 nfs_buf_release(bp
, 1);
7496 lck_mtx_unlock(nfs_buf_mutex
);
7500 thd
= vfs_context_thread(ap
->a_context
);
7501 cred
= ubc_getcred(vp
);
7502 if (!IS_VALID_CRED(cred
))
7503 cred
= vfs_context_ucred(ap
->a_context
);
7505 nfs_node_lock_force(np
);
7506 if (np
->n_flag
& NWRITEERR
) {
7507 error
= np
->n_error
;
7508 nfs_node_unlock(np
);
7509 nfs_data_unlock_noupdate(np
);
7511 ubc_upl_abort_range(pl
, pl_offset
, size
,
7512 UPL_ABORT_FREE_ON_EMPTY
);
7515 nfs_node_unlock(np
);
7517 if (f_offset
< 0 || f_offset
>= (off_t
)np
->n_size
||
7518 f_offset
& PAGE_MASK_64
|| size
& PAGE_MASK_64
) {
7519 nfs_data_unlock_noupdate(np
);
7521 ubc_upl_abort_range(pl
, pl_offset
, size
,
7522 UPL_ABORT_FREE_ON_EMPTY
);
7526 kret
= ubc_upl_map(pl
, &ioaddr
);
7527 if (kret
!= KERN_SUCCESS
)
7528 panic("nfs_vnop_pageout: ubc_upl_map() failed with (%d)", kret
);
7529 ioaddr
+= pl_offset
;
7531 if ((u_quad_t
)f_offset
+ size
> np
->n_size
)
7532 xsize
= np
->n_size
- f_offset
;
7536 pgsize
= round_page_64(xsize
);
7537 if ((size
> pgsize
) && !nofreeupl
)
7538 ubc_upl_abort_range(pl
, pl_offset
+ pgsize
, size
- pgsize
,
7539 UPL_ABORT_FREE_ON_EMPTY
);
7542 * check for partial page and clear the
7543 * contents past end of the file before
7544 * releasing it in the VM page cache
7546 if ((u_quad_t
)f_offset
< np
->n_size
&& (u_quad_t
)f_offset
+ size
> np
->n_size
) {
7547 size_t io
= np
->n_size
- f_offset
;
7548 bzero((caddr_t
)(ioaddr
+ io
), size
- io
);
7549 FSDBG(321, np
->n_size
, f_offset
, f_offset
+ io
, size
- io
);
7551 nfs_data_unlock_noupdate(np
);
7553 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_WRITE
,
7554 &uio_buf
, sizeof(uio_buf
));
7557 if (nmp
->nm_vers
>= NFS_VER4
)
7558 stategenid
= nmp
->nm_stategenid
;
7559 wverf
= wverf2
= wverfset
= 0;
7560 txsize
= rxsize
= xsize
;
7561 txoffset
= rxoffset
= f_offset
;
7562 txaddr
= rxaddr
= ioaddr
;
7563 commit
= NFS_WRITE_FILESYNC
;
7565 bzero(req
, sizeof(req
));
7566 nextsend
= nextwait
= 0;
7568 if (np
->n_flag
& NREVOKE
) {
7572 /* send requests while we need to and have available slots */
7573 while ((txsize
> 0) && (req
[nextsend
] == NULL
)) {
7574 iosize
= MIN(nmwsize
, txsize
);
7575 uio_reset(auio
, txoffset
, UIO_SYSSPACE
, UIO_WRITE
);
7576 uio_addiov(auio
, CAST_USER_ADDR_T(txaddr
), iosize
);
7577 FSDBG(323, uio_offset(auio
), iosize
, txaddr
, txsize
);
7578 OSAddAtomic64(1, &nfsstats
.pageouts
);
7579 nfs_node_lock_force(np
);
7581 nfs_node_unlock(np
);
7582 vnode_startwrite(vp
);
7583 iomode
= NFS_WRITE_UNSTABLE
;
7584 if ((error
= nmp
->nm_funcs
->nf_write_rpc_async(np
, auio
, iosize
, thd
, cred
, iomode
, NULL
, &req
[nextsend
]))) {
7585 req
[nextsend
] = NULL
;
7586 vnode_writedone(vp
);
7587 nfs_node_lock_force(np
);
7589 nfs_node_unlock(np
);
7595 nextsend
= (nextsend
+ 1) % MAXPAGINGREQS
;
7597 /* wait while we need to and break out if more requests to send */
7598 while ((rxsize
> 0) && req
[nextwait
]) {
7599 iosize
= remsize
= MIN(nmwsize
, rxsize
);
7600 error
= nmp
->nm_funcs
->nf_write_rpc_async_finish(np
, req
[nextwait
], &iomode
, &iosize
, &wverf2
);
7601 req
[nextwait
] = NULL
;
7602 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
7603 vnode_writedone(vp
);
7604 nfs_node_lock_force(np
);
7606 nfs_node_unlock(np
);
7607 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
)) {
7608 lck_mtx_lock(&nmp
->nm_lock
);
7609 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
7610 NP(np
, "nfs_vnop_pageout: error %d, initiating recovery", error
);
7611 nfs_need_recover(nmp
, error
);
7613 lck_mtx_unlock(&nmp
->nm_lock
);
7618 FSDBG(323, rxoffset
, rxsize
, error
, -1);
7624 } else if (wverf
!= wverf2
) {
7625 /* verifier changed, so we need to restart all the writes */
7629 /* Retain the lowest commitment level returned. */
7630 if (iomode
< commit
)
7637 /* need to try sending the remainder */
7639 uio_reset(auio
, rxoffset
, UIO_SYSSPACE
, UIO_WRITE
);
7640 uio_addiov(auio
, CAST_USER_ADDR_T(rxaddr
), remsize
);
7641 iomode
= NFS_WRITE_UNSTABLE
;
7642 error
= nfs_write_rpc2(np
, auio
, thd
, cred
, &iomode
, &wverf2
);
7643 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
)) {
7644 NP(np
, "nfs_vnop_pageout: restart: error %d", error
);
7645 lck_mtx_lock(&nmp
->nm_lock
);
7646 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
7647 NP(np
, "nfs_vnop_pageout: error %d, initiating recovery", error
);
7648 nfs_need_recover(nmp
, error
);
7650 lck_mtx_unlock(&nmp
->nm_lock
);
7655 FSDBG(323, rxoffset
, rxsize
, error
, -1);
7658 if (wverf
!= wverf2
) {
7659 /* verifier changed, so we need to restart all the writes */
7663 if (iomode
< commit
)
7672 } while (!error
&& (txsize
|| rxsize
));
7676 if (!error
&& (commit
!= NFS_WRITE_FILESYNC
)) {
7677 error
= nmp
->nm_funcs
->nf_commit_rpc(np
, f_offset
, xsize
, cred
, wverf
);
7678 if (error
== NFSERR_STALEWRITEVERF
) {
7686 /* cancel any outstanding requests */
7687 while (req
[nextwait
]) {
7688 nfs_request_async_cancel(req
[nextwait
]);
7689 req
[nextwait
] = NULL
;
7690 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
7691 vnode_writedone(vp
);
7692 nfs_node_lock_force(np
);
7694 nfs_node_unlock(np
);
7696 if (np
->n_flag
& NREVOKE
) {
7700 if (++vrestarts
<= 100) /* guard against no progress */
7702 NP(np
, "nfs_pageout: too many restarts, aborting");
7703 FSDBG(323, f_offset
, xsize
, ERESTART
, -1);
7706 if (restarts
<= nfs_mount_state_max_restarts(nmp
)) { /* guard against no progress */
7707 if (error
== NFSERR_GRACE
)
7708 tsleep(&nmp
->nm_state
, (PZERO
-1), "nfsgrace", 2*hz
);
7709 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
)))
7712 NP(np
, "nfs_pageout: too many restarts, aborting");
7713 FSDBG(323, f_offset
, xsize
, ERESTART
, -1);
7722 * We've had several different solutions on what to do when the pageout
7723 * gets an error. If we don't handle it, and return an error to the
7724 * caller, vm, it will retry . This can end in endless looping
7725 * between vm and here doing retries of the same page. Doing a dump
7726 * back to vm, will get it out of vm's knowledge and we lose whatever
7727 * data existed. This is risky, but in some cases necessary. For
7728 * example, the initial fix here was to do that for ESTALE. In that case
7729 * the server is telling us that the file is no longer the same. We
7730 * would not want to keep paging out to that. We also saw some 151
7731 * errors from Auspex server and NFSv3 can return errors higher than
7732 * ELAST. Those along with NFS known server errors we will "dump" from
7733 * vm. Errors we don't expect to occur, we dump and log for further
7734 * analysis. Errors that could be transient, networking ones,
7735 * we let vm "retry". Lastly, errors that we retry, but may have potential
7736 * to storm the network, we "retrywithsleep". "sever" will be used in
7737 * in the future to dump all pages of object for cases like ESTALE.
7738 * All this is the basis for the states returned and first guesses on
7739 * error handling. Tweaking expected as more statistics are gathered.
7740 * Note, in the long run we may need another more robust solution to
7741 * have some kind of persistant store when the vm cannot dump nor keep
7742 * retrying as a solution, but this would be a file architectural change
7744 if (!nofreeupl
) { /* otherwise stacked file system has to handle this */
7747 char action
= nfs_pageouterrorhandler(error
);
7751 abortflags
= UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
;
7754 abortflags
= UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
;
7755 if (error
<= NFS_ELAST
) {
7756 if ((errorcount
[error
] % 100) == 0)
7757 NP(np
, "nfs_pageout: unexpected error %d. dumping vm page", error
);
7758 errorcount
[error
]++;
7762 abortflags
= UPL_ABORT_FREE_ON_EMPTY
;
7764 case SEVER
: /* not implemented */
7766 NP(np
, "nfs_pageout: action %d not expected", action
);
7770 ubc_upl_abort_range(pl
, pl_offset
, pgsize
, abortflags
);
7771 /* return error in all cases above */
7774 ubc_upl_commit_range(pl
, pl_offset
, pgsize
,
7775 UPL_COMMIT_CLEAR_DIRTY
|
7776 UPL_COMMIT_FREE_ON_EMPTY
);
7782 /* Blktooff derives file offset given a logical block number */
7785 struct vnop_blktooff_args
/* {
7786 struct vnodeop_desc *a_desc;
7793 vnode_t vp
= ap
->a_vp
;
7794 struct nfsmount
*nmp
= VTONMP(vp
);
7796 if (nfs_mount_gone(nmp
))
7798 biosize
= nmp
->nm_biosize
;
7800 *ap
->a_offset
= (off_t
)(ap
->a_lblkno
* biosize
);
7807 struct vnop_offtoblk_args
/* {
7808 struct vnodeop_desc *a_desc;
7811 daddr64_t *a_lblkno;
7815 vnode_t vp
= ap
->a_vp
;
7816 struct nfsmount
*nmp
= VTONMP(vp
);
7818 if (nfs_mount_gone(nmp
))
7820 biosize
= nmp
->nm_biosize
;
7822 *ap
->a_lblkno
= (daddr64_t
)(ap
->a_offset
/ biosize
);
7828 * vnode change monitoring
7832 struct vnop_monitor_args
/* {
7833 struct vnodeop_desc *a_desc;
7838 vfs_context_t a_context;
7841 nfsnode_t np
= VTONFS(ap
->a_vp
);
7842 struct nfsmount
*nmp
= VTONMP(ap
->a_vp
);
7845 if (nfs_mount_gone(nmp
))
7848 /* make sure that the vnode's monitoring status is up to date */
7849 lck_mtx_lock(&nmp
->nm_lock
);
7850 if (vnode_ismonitored(ap
->a_vp
)) {
7851 /* This vnode is currently being monitored, make sure we're tracking it. */
7852 if (np
->n_monlink
.le_next
== NFSNOLIST
) {
7853 LIST_INSERT_HEAD(&nmp
->nm_monlist
, np
, n_monlink
);
7854 nfs_mount_sock_thread_wake(nmp
);
7857 /* This vnode is no longer being monitored, make sure we're not tracking it. */
7858 /* Wait for any in-progress getattr to complete first. */
7859 while (np
->n_mflag
& NMMONSCANINPROG
) {
7860 struct timespec ts
= { 1, 0 };
7861 np
->n_mflag
|= NMMONSCANWANT
;
7862 msleep(&np
->n_mflag
, &nmp
->nm_lock
, PZERO
-1, "nfswaitmonscan", &ts
);
7864 if (np
->n_monlink
.le_next
!= NFSNOLIST
) {
7865 LIST_REMOVE(np
, n_monlink
);
7866 np
->n_monlink
.le_next
= NFSNOLIST
;
7869 lck_mtx_unlock(&nmp
->nm_lock
);
7875 * Send a vnode notification for the given events.
7878 nfs_vnode_notify(nfsnode_t np
, uint32_t events
)
7880 struct nfsmount
*nmp
= NFSTONMP(np
);
7881 struct nfs_vattr nvattr
;
7882 struct vnode_attr vattr
, *vap
= NULL
;
7886 if ((np
->n_evtstamp
== now
.tv_sec
) || !nmp
) {
7887 /* delay sending this notify */
7888 np
->n_events
|= events
;
7891 events
|= np
->n_events
;
7893 np
->n_evtstamp
= now
.tv_sec
;
7895 vfs_get_notify_attributes(&vattr
);
7896 if (!nfs_getattrcache(np
, &nvattr
, 0)) {
7899 VATTR_RETURN(vap
, va_fsid
, vfs_statfs(nmp
->nm_mountp
)->f_fsid
.val
[0]);
7900 VATTR_RETURN(vap
, va_fileid
, nvattr
.nva_fileid
);
7901 VATTR_RETURN(vap
, va_mode
, nvattr
.nva_mode
);
7902 VATTR_RETURN(vap
, va_uid
, nvattr
.nva_uid
);
7903 VATTR_RETURN(vap
, va_gid
, nvattr
.nva_gid
);
7904 VATTR_RETURN(vap
, va_nlink
, nvattr
.nva_nlink
);
7906 vnode_notify(NFSTOV(np
), events
, vap
);