2 * Copyright (c) 2000-2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95
65 * FreeBSD-Id: nfs_vnops.c,v 1.72 1997/11/07 09:20:48 phk Exp $
70 * vnode op calls for Sun NFS version 2 and 3
72 #include <sys/param.h>
73 #include <sys/kernel.h>
74 #include <sys/systm.h>
75 #include <sys/resourcevar.h>
76 #include <sys/proc_internal.h>
77 #include <sys/kauth.h>
78 #include <sys/mount_internal.h>
79 #include <sys/malloc.h>
80 #include <sys/kpi_mbuf.h>
82 #include <sys/vnode_internal.h>
83 #include <sys/dirent.h>
84 #include <sys/fcntl.h>
85 #include <sys/lockf.h>
86 #include <sys/ubc_internal.h>
88 #include <sys/signalvar.h>
89 #include <sys/uio_internal.h>
91 #include <vfs/vfs_support.h>
96 #include <kern/clock.h>
97 #include <libkern/OSAtomic.h>
99 #include <miscfs/fifofs/fifo.h>
100 #include <miscfs/specfs/specdev.h>
102 #include <nfs/rpcv2.h>
103 #include <nfs/nfsproto.h>
105 #include <nfs/nfsnode.h>
106 #include <nfs/nfs_gss.h>
107 #include <nfs/nfsmount.h>
108 #include <nfs/nfs_lock.h>
109 #include <nfs/xdr_subs.h>
110 #include <nfs/nfsm_subs.h>
113 #include <netinet/in.h>
114 #include <netinet/in_var.h>
116 #include <vm/vm_kern.h>
117 #include <vm/vm_pageout.h>
119 #include <kern/task.h>
120 #include <kern/sched_prim.h>
122 #define NFS_VNOP_DBG(...) NFS_DBG(NFS_FAC_VNOP, 7, ## __VA_ARGS__)
123 #define DEFAULT_READLINK_NOCACHE 0
128 int nfs_vnop_lookup(struct vnop_lookup_args
*);
129 int nfsspec_vnop_read(struct vnop_read_args
*);
130 int nfsspec_vnop_write(struct vnop_write_args
*);
131 int nfsspec_vnop_close(struct vnop_close_args
*);
133 int nfsfifo_vnop_read(struct vnop_read_args
*);
134 int nfsfifo_vnop_write(struct vnop_write_args
*);
135 int nfsfifo_vnop_close(struct vnop_close_args
*);
137 int nfs_vnop_ioctl(struct vnop_ioctl_args
*);
138 int nfs_vnop_select(struct vnop_select_args
*);
139 int nfs_vnop_setattr(struct vnop_setattr_args
*);
140 int nfs_vnop_fsync(struct vnop_fsync_args
*);
141 int nfs_vnop_rename(struct vnop_rename_args
*);
142 int nfs_vnop_readdir(struct vnop_readdir_args
*);
143 int nfs_vnop_readlink(struct vnop_readlink_args
*);
144 int nfs_vnop_pathconf(struct vnop_pathconf_args
*);
145 int nfs_vnop_pagein(struct vnop_pagein_args
*);
146 int nfs_vnop_pageout(struct vnop_pageout_args
*);
147 int nfs_vnop_blktooff(struct vnop_blktooff_args
*);
148 int nfs_vnop_offtoblk(struct vnop_offtoblk_args
*);
149 int nfs_vnop_blockmap(struct vnop_blockmap_args
*);
150 int nfs_vnop_monitor(struct vnop_monitor_args
*);
152 int nfs3_vnop_create(struct vnop_create_args
*);
153 int nfs3_vnop_mknod(struct vnop_mknod_args
*);
154 int nfs3_vnop_getattr(struct vnop_getattr_args
*);
155 int nfs3_vnop_link(struct vnop_link_args
*);
156 int nfs3_vnop_mkdir(struct vnop_mkdir_args
*);
157 int nfs3_vnop_rmdir(struct vnop_rmdir_args
*);
158 int nfs3_vnop_symlink(struct vnop_symlink_args
*);
160 vnop_t
**nfsv2_vnodeop_p
;
161 static struct vnodeopv_entry_desc nfsv2_vnodeop_entries
[] = {
162 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
163 { &vnop_lookup_desc
, (vnop_t
*)nfs_vnop_lookup
}, /* lookup */
164 { &vnop_create_desc
, (vnop_t
*)nfs3_vnop_create
}, /* create */
165 { &vnop_mknod_desc
, (vnop_t
*)nfs3_vnop_mknod
}, /* mknod */
166 { &vnop_open_desc
, (vnop_t
*)nfs_vnop_open
}, /* open */
167 { &vnop_close_desc
, (vnop_t
*)nfs_vnop_close
}, /* close */
168 { &vnop_access_desc
, (vnop_t
*)nfs_vnop_access
}, /* access */
169 { &vnop_getattr_desc
, (vnop_t
*)nfs3_vnop_getattr
}, /* getattr */
170 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
171 { &vnop_read_desc
, (vnop_t
*)nfs_vnop_read
}, /* read */
172 { &vnop_write_desc
, (vnop_t
*)nfs_vnop_write
}, /* write */
173 { &vnop_ioctl_desc
, (vnop_t
*)nfs_vnop_ioctl
}, /* ioctl */
174 { &vnop_select_desc
, (vnop_t
*)nfs_vnop_select
}, /* select */
175 { &vnop_revoke_desc
, (vnop_t
*)nfs_vnop_revoke
}, /* revoke */
176 { &vnop_mmap_desc
, (vnop_t
*)nfs_vnop_mmap
}, /* mmap */
177 { &vnop_mnomap_desc
, (vnop_t
*)nfs_vnop_mnomap
}, /* mnomap */
178 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
179 { &vnop_remove_desc
, (vnop_t
*)nfs_vnop_remove
}, /* remove */
180 { &vnop_link_desc
, (vnop_t
*)nfs3_vnop_link
}, /* link */
181 { &vnop_rename_desc
, (vnop_t
*)nfs_vnop_rename
}, /* rename */
182 { &vnop_mkdir_desc
, (vnop_t
*)nfs3_vnop_mkdir
}, /* mkdir */
183 { &vnop_rmdir_desc
, (vnop_t
*)nfs3_vnop_rmdir
}, /* rmdir */
184 { &vnop_symlink_desc
, (vnop_t
*)nfs3_vnop_symlink
}, /* symlink */
185 { &vnop_readdir_desc
, (vnop_t
*)nfs_vnop_readdir
}, /* readdir */
186 { &vnop_readlink_desc
, (vnop_t
*)nfs_vnop_readlink
}, /* readlink */
187 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
188 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
189 { &vnop_strategy_desc
, (vnop_t
*)err_strategy
}, /* strategy */
190 { &vnop_pathconf_desc
, (vnop_t
*)nfs_vnop_pathconf
}, /* pathconf */
191 { &vnop_advlock_desc
, (vnop_t
*)nfs_vnop_advlock
}, /* advlock */
192 { &vnop_bwrite_desc
, (vnop_t
*)err_bwrite
}, /* bwrite */
193 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
194 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
195 { &vnop_copyfile_desc
, (vnop_t
*)err_copyfile
}, /* Copyfile */
196 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
197 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
198 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
199 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
202 struct vnodeopv_desc nfsv2_vnodeop_opv_desc
=
203 { &nfsv2_vnodeop_p
, nfsv2_vnodeop_entries
};
205 vnop_t
**nfsv4_vnodeop_p
;
206 static struct vnodeopv_entry_desc nfsv4_vnodeop_entries
[] = {
207 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
208 { &vnop_lookup_desc
, (vnop_t
*)nfs_vnop_lookup
}, /* lookup */
209 { &vnop_create_desc
, (vnop_t
*)nfs4_vnop_create
}, /* create */
210 { &vnop_mknod_desc
, (vnop_t
*)nfs4_vnop_mknod
}, /* mknod */
211 { &vnop_open_desc
, (vnop_t
*)nfs_vnop_open
}, /* open */
212 { &vnop_close_desc
, (vnop_t
*)nfs_vnop_close
}, /* close */
213 { &vnop_access_desc
, (vnop_t
*)nfs_vnop_access
}, /* access */
214 { &vnop_getattr_desc
, (vnop_t
*)nfs4_vnop_getattr
}, /* getattr */
215 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
216 { &vnop_read_desc
, (vnop_t
*)nfs_vnop_read
}, /* read */
217 { &vnop_write_desc
, (vnop_t
*)nfs_vnop_write
}, /* write */
218 { &vnop_ioctl_desc
, (vnop_t
*)nfs_vnop_ioctl
}, /* ioctl */
219 { &vnop_select_desc
, (vnop_t
*)nfs_vnop_select
}, /* select */
220 { &vnop_revoke_desc
, (vnop_t
*)nfs_vnop_revoke
}, /* revoke */
221 { &vnop_mmap_desc
, (vnop_t
*)nfs_vnop_mmap
}, /* mmap */
222 { &vnop_mnomap_desc
, (vnop_t
*)nfs_vnop_mnomap
}, /* mnomap */
223 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
224 { &vnop_remove_desc
, (vnop_t
*)nfs_vnop_remove
}, /* remove */
225 { &vnop_link_desc
, (vnop_t
*)nfs4_vnop_link
}, /* link */
226 { &vnop_rename_desc
, (vnop_t
*)nfs_vnop_rename
}, /* rename */
227 { &vnop_mkdir_desc
, (vnop_t
*)nfs4_vnop_mkdir
}, /* mkdir */
228 { &vnop_rmdir_desc
, (vnop_t
*)nfs4_vnop_rmdir
}, /* rmdir */
229 { &vnop_symlink_desc
, (vnop_t
*)nfs4_vnop_symlink
}, /* symlink */
230 { &vnop_readdir_desc
, (vnop_t
*)nfs_vnop_readdir
}, /* readdir */
231 { &vnop_readlink_desc
, (vnop_t
*)nfs_vnop_readlink
}, /* readlink */
232 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
233 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
234 { &vnop_strategy_desc
, (vnop_t
*)err_strategy
}, /* strategy */
235 { &vnop_pathconf_desc
, (vnop_t
*)nfs_vnop_pathconf
}, /* pathconf */
236 { &vnop_advlock_desc
, (vnop_t
*)nfs_vnop_advlock
}, /* advlock */
237 { &vnop_bwrite_desc
, (vnop_t
*)err_bwrite
}, /* bwrite */
238 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
239 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
240 { &vnop_copyfile_desc
, (vnop_t
*)err_copyfile
}, /* Copyfile */
241 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
242 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
243 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
244 { &vnop_getxattr_desc
, (vnop_t
*)nfs4_vnop_getxattr
}, /* getxattr */
245 { &vnop_setxattr_desc
, (vnop_t
*)nfs4_vnop_setxattr
}, /* setxattr */
246 { &vnop_removexattr_desc
, (vnop_t
*)nfs4_vnop_removexattr
},/* removexattr */
247 { &vnop_listxattr_desc
, (vnop_t
*)nfs4_vnop_listxattr
},/* listxattr */
249 { &vnop_getnamedstream_desc
, (vnop_t
*)nfs4_vnop_getnamedstream
}, /* getnamedstream */
250 { &vnop_makenamedstream_desc
, (vnop_t
*)nfs4_vnop_makenamedstream
}, /* makenamedstream */
251 { &vnop_removenamedstream_desc
, (vnop_t
*)nfs4_vnop_removenamedstream
},/* removenamedstream */
253 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
256 struct vnodeopv_desc nfsv4_vnodeop_opv_desc
=
257 { &nfsv4_vnodeop_p
, nfsv4_vnodeop_entries
};
260 * Special device vnode ops
262 vnop_t
**spec_nfsv2nodeop_p
;
263 static struct vnodeopv_entry_desc spec_nfsv2nodeop_entries
[] = {
264 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
265 { &vnop_lookup_desc
, (vnop_t
*)spec_lookup
}, /* lookup */
266 { &vnop_create_desc
, (vnop_t
*)spec_create
}, /* create */
267 { &vnop_mknod_desc
, (vnop_t
*)spec_mknod
}, /* mknod */
268 { &vnop_open_desc
, (vnop_t
*)spec_open
}, /* open */
269 { &vnop_close_desc
, (vnop_t
*)nfsspec_vnop_close
}, /* close */
270 { &vnop_getattr_desc
, (vnop_t
*)nfs3_vnop_getattr
}, /* getattr */
271 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
272 { &vnop_read_desc
, (vnop_t
*)nfsspec_vnop_read
}, /* read */
273 { &vnop_write_desc
, (vnop_t
*)nfsspec_vnop_write
}, /* write */
274 { &vnop_ioctl_desc
, (vnop_t
*)spec_ioctl
}, /* ioctl */
275 { &vnop_select_desc
, (vnop_t
*)spec_select
}, /* select */
276 { &vnop_revoke_desc
, (vnop_t
*)spec_revoke
}, /* revoke */
277 { &vnop_mmap_desc
, (vnop_t
*)spec_mmap
}, /* mmap */
278 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
279 { &vnop_remove_desc
, (vnop_t
*)spec_remove
}, /* remove */
280 { &vnop_link_desc
, (vnop_t
*)spec_link
}, /* link */
281 { &vnop_rename_desc
, (vnop_t
*)spec_rename
}, /* rename */
282 { &vnop_mkdir_desc
, (vnop_t
*)spec_mkdir
}, /* mkdir */
283 { &vnop_rmdir_desc
, (vnop_t
*)spec_rmdir
}, /* rmdir */
284 { &vnop_symlink_desc
, (vnop_t
*)spec_symlink
}, /* symlink */
285 { &vnop_readdir_desc
, (vnop_t
*)spec_readdir
}, /* readdir */
286 { &vnop_readlink_desc
, (vnop_t
*)spec_readlink
}, /* readlink */
287 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
288 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
289 { &vnop_strategy_desc
, (vnop_t
*)spec_strategy
}, /* strategy */
290 { &vnop_pathconf_desc
, (vnop_t
*)spec_pathconf
}, /* pathconf */
291 { &vnop_advlock_desc
, (vnop_t
*)spec_advlock
}, /* advlock */
292 { &vnop_bwrite_desc
, (vnop_t
*)vn_bwrite
}, /* bwrite */
293 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
294 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
295 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
296 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
297 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
298 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
301 struct vnodeopv_desc spec_nfsv2nodeop_opv_desc
=
302 { &spec_nfsv2nodeop_p
, spec_nfsv2nodeop_entries
};
303 vnop_t
**spec_nfsv4nodeop_p
;
304 static struct vnodeopv_entry_desc spec_nfsv4nodeop_entries
[] = {
305 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
306 { &vnop_lookup_desc
, (vnop_t
*)spec_lookup
}, /* lookup */
307 { &vnop_create_desc
, (vnop_t
*)spec_create
}, /* create */
308 { &vnop_mknod_desc
, (vnop_t
*)spec_mknod
}, /* mknod */
309 { &vnop_open_desc
, (vnop_t
*)spec_open
}, /* open */
310 { &vnop_close_desc
, (vnop_t
*)nfsspec_vnop_close
}, /* close */
311 { &vnop_getattr_desc
, (vnop_t
*)nfs4_vnop_getattr
}, /* getattr */
312 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
313 { &vnop_read_desc
, (vnop_t
*)nfsspec_vnop_read
}, /* read */
314 { &vnop_write_desc
, (vnop_t
*)nfsspec_vnop_write
}, /* write */
315 { &vnop_ioctl_desc
, (vnop_t
*)spec_ioctl
}, /* ioctl */
316 { &vnop_select_desc
, (vnop_t
*)spec_select
}, /* select */
317 { &vnop_revoke_desc
, (vnop_t
*)spec_revoke
}, /* revoke */
318 { &vnop_mmap_desc
, (vnop_t
*)spec_mmap
}, /* mmap */
319 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
320 { &vnop_remove_desc
, (vnop_t
*)spec_remove
}, /* remove */
321 { &vnop_link_desc
, (vnop_t
*)spec_link
}, /* link */
322 { &vnop_rename_desc
, (vnop_t
*)spec_rename
}, /* rename */
323 { &vnop_mkdir_desc
, (vnop_t
*)spec_mkdir
}, /* mkdir */
324 { &vnop_rmdir_desc
, (vnop_t
*)spec_rmdir
}, /* rmdir */
325 { &vnop_symlink_desc
, (vnop_t
*)spec_symlink
}, /* symlink */
326 { &vnop_readdir_desc
, (vnop_t
*)spec_readdir
}, /* readdir */
327 { &vnop_readlink_desc
, (vnop_t
*)spec_readlink
}, /* readlink */
328 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
329 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
330 { &vnop_strategy_desc
, (vnop_t
*)spec_strategy
}, /* strategy */
331 { &vnop_pathconf_desc
, (vnop_t
*)spec_pathconf
}, /* pathconf */
332 { &vnop_advlock_desc
, (vnop_t
*)spec_advlock
}, /* advlock */
333 { &vnop_bwrite_desc
, (vnop_t
*)vn_bwrite
}, /* bwrite */
334 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
335 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
336 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
337 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
338 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
339 { &vnop_getxattr_desc
, (vnop_t
*)nfs4_vnop_getxattr
}, /* getxattr */
340 { &vnop_setxattr_desc
, (vnop_t
*)nfs4_vnop_setxattr
}, /* setxattr */
341 { &vnop_removexattr_desc
, (vnop_t
*)nfs4_vnop_removexattr
},/* removexattr */
342 { &vnop_listxattr_desc
, (vnop_t
*)nfs4_vnop_listxattr
},/* listxattr */
344 { &vnop_getnamedstream_desc
, (vnop_t
*)nfs4_vnop_getnamedstream
}, /* getnamedstream */
345 { &vnop_makenamedstream_desc
, (vnop_t
*)nfs4_vnop_makenamedstream
}, /* makenamedstream */
346 { &vnop_removenamedstream_desc
, (vnop_t
*)nfs4_vnop_removenamedstream
},/* removenamedstream */
348 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
351 struct vnodeopv_desc spec_nfsv4nodeop_opv_desc
=
352 { &spec_nfsv4nodeop_p
, spec_nfsv4nodeop_entries
};
355 vnop_t
**fifo_nfsv2nodeop_p
;
356 static struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries
[] = {
357 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
358 { &vnop_lookup_desc
, (vnop_t
*)fifo_lookup
}, /* lookup */
359 { &vnop_create_desc
, (vnop_t
*)fifo_create
}, /* create */
360 { &vnop_mknod_desc
, (vnop_t
*)fifo_mknod
}, /* mknod */
361 { &vnop_open_desc
, (vnop_t
*)fifo_open
}, /* open */
362 { &vnop_close_desc
, (vnop_t
*)nfsfifo_vnop_close
}, /* close */
363 { &vnop_getattr_desc
, (vnop_t
*)nfs3_vnop_getattr
}, /* getattr */
364 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
365 { &vnop_read_desc
, (vnop_t
*)nfsfifo_vnop_read
}, /* read */
366 { &vnop_write_desc
, (vnop_t
*)nfsfifo_vnop_write
}, /* write */
367 { &vnop_ioctl_desc
, (vnop_t
*)fifo_ioctl
}, /* ioctl */
368 { &vnop_select_desc
, (vnop_t
*)fifo_select
}, /* select */
369 { &vnop_revoke_desc
, (vnop_t
*)fifo_revoke
}, /* revoke */
370 { &vnop_mmap_desc
, (vnop_t
*)fifo_mmap
}, /* mmap */
371 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
372 { &vnop_remove_desc
, (vnop_t
*)fifo_remove
}, /* remove */
373 { &vnop_link_desc
, (vnop_t
*)fifo_link
}, /* link */
374 { &vnop_rename_desc
, (vnop_t
*)fifo_rename
}, /* rename */
375 { &vnop_mkdir_desc
, (vnop_t
*)fifo_mkdir
}, /* mkdir */
376 { &vnop_rmdir_desc
, (vnop_t
*)fifo_rmdir
}, /* rmdir */
377 { &vnop_symlink_desc
, (vnop_t
*)fifo_symlink
}, /* symlink */
378 { &vnop_readdir_desc
, (vnop_t
*)fifo_readdir
}, /* readdir */
379 { &vnop_readlink_desc
, (vnop_t
*)fifo_readlink
}, /* readlink */
380 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
381 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
382 { &vnop_strategy_desc
, (vnop_t
*)fifo_strategy
}, /* strategy */
383 { &vnop_pathconf_desc
, (vnop_t
*)fifo_pathconf
}, /* pathconf */
384 { &vnop_advlock_desc
, (vnop_t
*)fifo_advlock
}, /* advlock */
385 { &vnop_bwrite_desc
, (vnop_t
*)vn_bwrite
}, /* bwrite */
386 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
387 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
388 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
389 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
390 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
391 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
394 struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc
=
395 { &fifo_nfsv2nodeop_p
, fifo_nfsv2nodeop_entries
};
397 vnop_t
**fifo_nfsv4nodeop_p
;
398 static struct vnodeopv_entry_desc fifo_nfsv4nodeop_entries
[] = {
399 { &vnop_default_desc
, (vnop_t
*)vn_default_error
},
400 { &vnop_lookup_desc
, (vnop_t
*)fifo_lookup
}, /* lookup */
401 { &vnop_create_desc
, (vnop_t
*)fifo_create
}, /* create */
402 { &vnop_mknod_desc
, (vnop_t
*)fifo_mknod
}, /* mknod */
403 { &vnop_open_desc
, (vnop_t
*)fifo_open
}, /* open */
404 { &vnop_close_desc
, (vnop_t
*)nfsfifo_vnop_close
}, /* close */
405 { &vnop_getattr_desc
, (vnop_t
*)nfs4_vnop_getattr
}, /* getattr */
406 { &vnop_setattr_desc
, (vnop_t
*)nfs_vnop_setattr
}, /* setattr */
407 { &vnop_read_desc
, (vnop_t
*)nfsfifo_vnop_read
}, /* read */
408 { &vnop_write_desc
, (vnop_t
*)nfsfifo_vnop_write
}, /* write */
409 { &vnop_ioctl_desc
, (vnop_t
*)fifo_ioctl
}, /* ioctl */
410 { &vnop_select_desc
, (vnop_t
*)fifo_select
}, /* select */
411 { &vnop_revoke_desc
, (vnop_t
*)fifo_revoke
}, /* revoke */
412 { &vnop_mmap_desc
, (vnop_t
*)fifo_mmap
}, /* mmap */
413 { &vnop_fsync_desc
, (vnop_t
*)nfs_vnop_fsync
}, /* fsync */
414 { &vnop_remove_desc
, (vnop_t
*)fifo_remove
}, /* remove */
415 { &vnop_link_desc
, (vnop_t
*)fifo_link
}, /* link */
416 { &vnop_rename_desc
, (vnop_t
*)fifo_rename
}, /* rename */
417 { &vnop_mkdir_desc
, (vnop_t
*)fifo_mkdir
}, /* mkdir */
418 { &vnop_rmdir_desc
, (vnop_t
*)fifo_rmdir
}, /* rmdir */
419 { &vnop_symlink_desc
, (vnop_t
*)fifo_symlink
}, /* symlink */
420 { &vnop_readdir_desc
, (vnop_t
*)fifo_readdir
}, /* readdir */
421 { &vnop_readlink_desc
, (vnop_t
*)fifo_readlink
}, /* readlink */
422 { &vnop_inactive_desc
, (vnop_t
*)nfs_vnop_inactive
}, /* inactive */
423 { &vnop_reclaim_desc
, (vnop_t
*)nfs_vnop_reclaim
}, /* reclaim */
424 { &vnop_strategy_desc
, (vnop_t
*)fifo_strategy
}, /* strategy */
425 { &vnop_pathconf_desc
, (vnop_t
*)fifo_pathconf
}, /* pathconf */
426 { &vnop_advlock_desc
, (vnop_t
*)fifo_advlock
}, /* advlock */
427 { &vnop_bwrite_desc
, (vnop_t
*)vn_bwrite
}, /* bwrite */
428 { &vnop_pagein_desc
, (vnop_t
*)nfs_vnop_pagein
}, /* Pagein */
429 { &vnop_pageout_desc
, (vnop_t
*)nfs_vnop_pageout
}, /* Pageout */
430 { &vnop_blktooff_desc
, (vnop_t
*)nfs_vnop_blktooff
}, /* blktooff */
431 { &vnop_offtoblk_desc
, (vnop_t
*)nfs_vnop_offtoblk
}, /* offtoblk */
432 { &vnop_blockmap_desc
, (vnop_t
*)nfs_vnop_blockmap
}, /* blockmap */
433 { &vnop_getxattr_desc
, (vnop_t
*)nfs4_vnop_getxattr
}, /* getxattr */
434 { &vnop_setxattr_desc
, (vnop_t
*)nfs4_vnop_setxattr
}, /* setxattr */
435 { &vnop_removexattr_desc
, (vnop_t
*)nfs4_vnop_removexattr
},/* removexattr */
436 { &vnop_listxattr_desc
, (vnop_t
*)nfs4_vnop_listxattr
},/* listxattr */
438 { &vnop_getnamedstream_desc
, (vnop_t
*)nfs4_vnop_getnamedstream
}, /* getnamedstream */
439 { &vnop_makenamedstream_desc
, (vnop_t
*)nfs4_vnop_makenamedstream
}, /* makenamedstream */
440 { &vnop_removenamedstream_desc
, (vnop_t
*)nfs4_vnop_removenamedstream
},/* removenamedstream */
442 { &vnop_monitor_desc
, (vnop_t
*)nfs_vnop_monitor
}, /* monitor */
445 struct vnodeopv_desc fifo_nfsv4nodeop_opv_desc
=
446 { &fifo_nfsv4nodeop_p
, fifo_nfsv4nodeop_entries
};
449 int nfs_sillyrename(nfsnode_t
,nfsnode_t
,struct componentname
*,vfs_context_t
);
450 int nfs_getattr_internal(nfsnode_t
, struct nfs_vattr
*, vfs_context_t
, int);
451 int nfs_refresh_fh(nfsnode_t
, vfs_context_t
);
454 * Find the slot in the access cache for this UID.
455 * If adding and no existing slot is found, reuse slots in FIFO order.
456 * The index of the next slot to use is kept in the last entry of the n_access array.
459 nfs_node_access_slot(nfsnode_t np
, uid_t uid
, int add
)
463 for (slot
=0; slot
< NFS_ACCESS_CACHE_SIZE
; slot
++)
464 if (np
->n_accessuid
[slot
] == uid
)
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
;
482 struct nfsmount
*nmp
;
486 nfsm_chain_null(&nmreq
);
487 nfsm_chain_null(&nmrep
);
489 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(NFS_VER3
) + NFSX_UNSIGNED
);
490 nfsm_chain_add_fh(error
, &nmreq
, NFS_VER3
, np
->n_fhp
, np
->n_fhsize
);
491 nfsm_chain_add_32(error
, &nmreq
, *access
);
492 nfsm_chain_build_done(error
, &nmreq
);
494 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC_ACCESS
,
495 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
496 NULL
, rpcflags
, &nmrep
, &xid
, &status
);
497 if ((lockerror
= nfs_node_lock(np
)))
499 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
502 nfsm_chain_get_32(error
, &nmrep
, access_result
);
505 /* XXXab do we really need mount here, also why are we doing access cache management here? */
507 if (nfs_mount_gone(nmp
)) {
512 if (auth_is_kerberized(np
->n_auth
) || auth_is_kerberized(nmp
->nm_auth
)) {
513 uid
= nfs_cred_getasid2uid(vfs_context_ucred(ctx
));
515 uid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
517 slot
= nfs_node_access_slot(np
, uid
, 1);
518 np
->n_accessuid
[slot
] = uid
;
520 np
->n_accessstamp
[slot
] = now
.tv_sec
;
521 np
->n_access
[slot
] = access_result
;
524 * If we asked for DELETE but didn't get it, the server
525 * may simply not support returning that bit (possible
526 * on UNIX systems). So, we'll assume that it is OK,
527 * and just let any subsequent delete action fail if it
528 * really isn't deletable.
530 if ((*access
& NFS_ACCESS_DELETE
) &&
531 !(np
->n_access
[slot
] & NFS_ACCESS_DELETE
))
532 np
->n_access
[slot
] |= NFS_ACCESS_DELETE
;
533 /* ".zfs" subdirectories may erroneously give a denied answer for add/remove */
534 if (nfs_access_dotzfs
&& (np
->n_flag
& NISDOTZFSCHILD
))
535 np
->n_access
[slot
] |= (NFS_ACCESS_MODIFY
|NFS_ACCESS_EXTEND
|NFS_ACCESS_DELETE
);
536 /* pass back the access returned with this request */
537 *access
= np
->n_access
[slot
];
541 nfsm_chain_cleanup(&nmreq
);
542 nfsm_chain_cleanup(&nmrep
);
547 * NFS access vnode op.
548 * For NFS version 2, just return ok. File accesses may fail later.
549 * For NFS version 3+, use the access RPC to check accessibility. If file
550 * permissions are changed on the server, accesses might still fail later.
554 struct vnop_access_args
/* {
555 struct vnodeop_desc *a_desc;
558 vfs_context_t a_context;
561 vfs_context_t ctx
= ap
->a_context
;
562 vnode_t vp
= ap
->a_vp
;
563 int error
= 0, slot
, dorpc
, rpcflags
= 0;
564 u_int32_t access
, waccess
;
565 nfsnode_t np
= VTONFS(vp
);
566 struct nfsmount
*nmp
;
572 if (nfs_mount_gone(nmp
))
574 nfsvers
= nmp
->nm_vers
;
576 if (nfsvers
== NFS_VER2
) {
577 if ((ap
->a_action
& KAUTH_VNODE_WRITE_RIGHTS
) &&
578 vfs_isrdonly(vnode_mount(vp
)))
584 * For NFS v3, do an access rpc, otherwise you are stuck emulating
585 * ufs_access() locally using the vattr. This may not be correct,
586 * since the server may apply other access criteria such as
587 * client uid-->server uid mapping that we do not know about, but
588 * this is better than just returning anything that is lying about
593 * Convert KAUTH primitives to NFS access rights.
596 if (vnode_isdir(vp
)) {
599 (KAUTH_VNODE_LIST_DIRECTORY
|
600 KAUTH_VNODE_READ_EXTATTRIBUTES
))
601 access
|= NFS_ACCESS_READ
;
602 if (ap
->a_action
& KAUTH_VNODE_SEARCH
)
603 access
|= NFS_ACCESS_LOOKUP
;
605 (KAUTH_VNODE_ADD_FILE
|
606 KAUTH_VNODE_ADD_SUBDIRECTORY
))
607 access
|= NFS_ACCESS_MODIFY
| NFS_ACCESS_EXTEND
;
608 if (ap
->a_action
& KAUTH_VNODE_DELETE_CHILD
)
609 access
|= NFS_ACCESS_MODIFY
;
613 (KAUTH_VNODE_READ_DATA
|
614 KAUTH_VNODE_READ_EXTATTRIBUTES
))
615 access
|= NFS_ACCESS_READ
;
616 if (ap
->a_action
& KAUTH_VNODE_WRITE_DATA
)
617 access
|= NFS_ACCESS_MODIFY
| NFS_ACCESS_EXTEND
;
618 if (ap
->a_action
& KAUTH_VNODE_APPEND_DATA
)
619 access
|= NFS_ACCESS_EXTEND
;
620 if (ap
->a_action
& KAUTH_VNODE_EXECUTE
)
621 access
|= NFS_ACCESS_EXECUTE
;
624 if (ap
->a_action
& KAUTH_VNODE_DELETE
)
625 access
|= NFS_ACCESS_DELETE
;
627 (KAUTH_VNODE_WRITE_ATTRIBUTES
|
628 KAUTH_VNODE_WRITE_EXTATTRIBUTES
|
629 KAUTH_VNODE_WRITE_SECURITY
))
630 access
|= NFS_ACCESS_MODIFY
;
631 /* XXX this is pretty dubious */
632 if (ap
->a_action
& KAUTH_VNODE_CHANGE_OWNER
)
633 access
|= NFS_ACCESS_MODIFY
;
635 /* if caching, always ask for every right */
636 if (nfs_access_cache_timeout
> 0) {
637 waccess
= NFS_ACCESS_READ
| NFS_ACCESS_MODIFY
|
638 NFS_ACCESS_EXTEND
| NFS_ACCESS_EXECUTE
|
639 NFS_ACCESS_DELETE
| NFS_ACCESS_LOOKUP
;
644 if ((error
= nfs_node_lock(np
)))
648 * Does our cached result allow us to give a definite yes to
651 if (auth_is_kerberized(np
->n_auth
) || auth_is_kerberized(nmp
->nm_auth
))
652 uid
= nfs_cred_getasid2uid(vfs_context_ucred(ctx
));
654 uid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
655 slot
= nfs_node_access_slot(np
, uid
, 0);
658 /* not asking for any rights understood by NFS, so don't bother doing an RPC */
659 /* OSAddAtomic(1, &nfsstats.accesscache_hits); */
662 } else if (NACCESSVALID(np
, slot
)) {
664 if (((now
.tv_sec
< (np
->n_accessstamp
[slot
] + nfs_access_cache_timeout
)) &&
665 ((np
->n_access
[slot
] & access
) == access
)) || nfs_use_cache(nmp
)) {
666 /* OSAddAtomic(1, &nfsstats.accesscache_hits); */
668 waccess
= np
->n_access
[slot
];
673 /* Either a no, or a don't know. Go to the wire. */
674 /* OSAddAtomic(1, &nfsstats.accesscache_misses); */
677 * Allow an access call to timeout if we have it cached
678 * so we won't hang if the server isn't responding.
680 if (NACCESSVALID(np
, slot
))
683 error
= nmp
->nm_funcs
->nf_access_rpc(np
, &waccess
, rpcflags
, ctx
);
686 * If the server didn't respond return the cached access.
688 if ((error
== ETIMEDOUT
) && (rpcflags
& R_SOFT
)) {
690 waccess
= np
->n_access
[slot
];
693 if (!error
&& ((waccess
& access
) != access
))
703 * Perform various update/invalidation checks and then add the
704 * open to the node. Regular files will have an open file structure
705 * on the node and, for NFSv4, perform an OPEN request on the server.
709 struct vnop_open_args
/* {
710 struct vnodeop_desc *a_desc;
713 vfs_context_t a_context;
716 vfs_context_t ctx
= ap
->a_context
;
717 vnode_t vp
= ap
->a_vp
;
718 nfsnode_t np
= VTONFS(vp
);
719 struct nfsmount
*nmp
;
720 int error
, accessMode
, denyMode
, opened
= 0;
721 struct nfs_open_owner
*noop
= NULL
;
722 struct nfs_open_file
*nofp
= NULL
;
725 if (!(ap
->a_mode
& (FREAD
|FWRITE
)))
729 if (nfs_mount_gone(nmp
))
731 if (np
->n_flag
& NREVOKE
)
734 vtype
= vnode_vtype(vp
);
735 if ((vtype
!= VREG
) && (vtype
!= VDIR
) && (vtype
!= VLNK
))
738 /* First, check if we need to update/invalidate */
739 if (ISSET(np
->n_flag
, NUPDATESIZE
))
740 nfs_data_update_size(np
, 0);
741 if ((error
= nfs_node_lock(np
)))
743 if (np
->n_flag
& NNEEDINVALIDATE
) {
744 np
->n_flag
&= ~NNEEDINVALIDATE
;
748 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, ctx
, 1);
749 if ((error
= nfs_node_lock(np
)))
753 np
->n_lastrahead
= -1;
754 if (np
->n_flag
& NMODIFIED
) {
758 if ((error
= nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, ctx
, 1)))
764 /* nfs_getattr() will check changed and purge caches */
765 if ((error
= nfs_getattr(np
, NULL
, ctx
, NGA_UNCACHED
)))
769 /* Just mark that it was opened */
770 lck_mtx_lock(&np
->n_openlock
);
772 lck_mtx_unlock(&np
->n_openlock
);
776 /* mode contains some combination of: FREAD, FWRITE, O_SHLOCK, O_EXLOCK */
778 if (ap
->a_mode
& FREAD
)
779 accessMode
|= NFS_OPEN_SHARE_ACCESS_READ
;
780 if (ap
->a_mode
& FWRITE
)
781 accessMode
|= NFS_OPEN_SHARE_ACCESS_WRITE
;
782 if (ap
->a_mode
& O_EXLOCK
)
783 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
784 else if (ap
->a_mode
& O_SHLOCK
)
785 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
787 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
788 // XXX don't do deny modes just yet (and never do it for !v4)
789 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
791 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
796 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
798 nfs_open_owner_rele(noop
);
801 if (np
->n_flag
& NREVOKE
) {
803 nfs_mount_state_in_use_end(nmp
, 0);
804 nfs_open_owner_rele(noop
);
808 error
= nfs_open_file_find(np
, noop
, &nofp
, accessMode
, denyMode
, 1);
809 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
810 NP(np
, "nfs_vnop_open: LOST %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
813 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
814 nfs_mount_state_in_use_end(nmp
, 0);
815 error
= nfs4_reopen(nofp
, vfs_context_thread(ctx
));
821 error
= nfs_open_file_set_busy(nofp
, vfs_context_thread(ctx
));
827 if (nmp
->nm_vers
< NFS_VER4
) {
829 * NFS v2/v3 opens are always allowed - so just add it.
831 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, 0);
836 * If we just created the file and the modes match, then we simply use
837 * the open performed in the create. Otherwise, send the request.
839 if ((nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) &&
840 (nofp
->nof_creator
== current_thread()) &&
841 (accessMode
== NFS_OPEN_SHARE_ACCESS_BOTH
) &&
842 (denyMode
== NFS_OPEN_SHARE_DENY_NONE
)) {
843 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
844 nofp
->nof_creator
= NULL
;
847 error
= nfs4_open(np
, nofp
, accessMode
, denyMode
, ctx
);
848 if ((error
== EACCES
) && (nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) &&
849 (nofp
->nof_creator
== current_thread())) {
851 * Ugh. This can happen if we just created the file with read-only
852 * perms and we're trying to open it for real with different modes
853 * (e.g. write-only or with a deny mode) and the server decides to
854 * not allow the second open because of the read-only perms.
855 * The best we can do is to just use the create's open.
856 * We may have access we don't need or we may not have a requested
857 * deny mode. We may log complaints later, but we'll try to avoid it.
859 if (denyMode
!= NFS_OPEN_SHARE_DENY_NONE
)
860 NP(np
, "nfs_vnop_open: deny mode foregone on create, %d", kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
861 nofp
->nof_creator
= NULL
;
868 * If we had just created the file, we already had it open.
869 * If the actual open mode is less than what we grabbed at
870 * create time, then we'll downgrade the open here.
872 if ((nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) &&
873 (nofp
->nof_creator
== current_thread())) {
874 error
= nfs_close(np
, nofp
, NFS_OPEN_SHARE_ACCESS_BOTH
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
876 NP(np
, "nfs_vnop_open: create close error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
877 if (!nfs_mount_state_error_should_restart(error
)) {
879 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
886 nfs_open_file_clear_busy(nofp
);
887 if (nfs_mount_state_in_use_end(nmp
, error
)) {
892 NP(np
, "nfs_vnop_open: error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
894 nfs_open_owner_rele(noop
);
895 if (!error
&& vtype
== VREG
&& (ap
->a_mode
& FWRITE
)) {
896 lck_mtx_lock(&nmp
->nm_lock
);
897 nmp
->nm_state
&= ~NFSSTA_SQUISHY
;
898 nmp
->nm_curdeadtimeout
= nmp
->nm_deadtimeout
;
899 if (nmp
->nm_curdeadtimeout
<= 0)
900 nmp
->nm_deadto_start
= 0;
902 lck_mtx_unlock(&nmp
->nm_lock
);
909 nfs_no_of_open_file_writers(nfsnode_t np
)
911 uint32_t writers
= 0;
912 struct nfs_open_file
*nofp
;
914 TAILQ_FOREACH(nofp
, &np
->n_opens
, nof_link
) {
915 writers
+= nofp
->nof_w
+ nofp
->nof_rw
+ nofp
->nof_w_dw
+ nofp
->nof_rw_dw
+
916 nofp
->nof_w_drw
+ nofp
->nof_rw_drw
+ nofp
->nof_d_w_dw
+
917 nofp
->nof_d_rw_dw
+ nofp
->nof_d_w_drw
+ nofp
->nof_d_rw_drw
+
918 nofp
->nof_d_w
+ nofp
->nof_d_rw
;
927 * What an NFS client should do upon close after writing is a debatable issue.
928 * Most NFS clients push delayed writes to the server upon close, basically for
930 * 1 - So that any write errors may be reported back to the client process
931 * doing the close system call. By far the two most likely errors are
932 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
933 * 2 - To put a worst case upper bound on cache inconsistency between
934 * multiple clients for the file.
935 * There is also a consistency problem for Version 2 of the protocol w.r.t.
936 * not being able to tell if other clients are writing a file concurrently,
937 * since there is no way of knowing if the changed modify time in the reply
938 * is only due to the write for this client.
939 * (NFS Version 3 provides weak cache consistency data in the reply that
940 * should be sufficient to detect and handle this case.)
942 * The current code does the following:
943 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
944 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate them.
945 * for NFS Version 4 - basically the same as NFSv3
949 struct vnop_close_args
/* {
950 struct vnodeop_desc *a_desc;
953 vfs_context_t a_context;
956 vfs_context_t ctx
= ap
->a_context
;
957 vnode_t vp
= ap
->a_vp
;
958 nfsnode_t np
= VTONFS(vp
);
959 struct nfsmount
*nmp
;
960 int error
= 0, error1
, nfsvers
;
961 int fflag
= ap
->a_fflag
;
963 int accessMode
, denyMode
;
964 struct nfs_open_owner
*noop
= NULL
;
965 struct nfs_open_file
*nofp
= NULL
;
970 nfsvers
= nmp
->nm_vers
;
971 vtype
= vnode_vtype(vp
);
973 /* First, check if we need to update/flush/invalidate */
974 if (ISSET(np
->n_flag
, NUPDATESIZE
))
975 nfs_data_update_size(np
, 0);
976 nfs_node_lock_force(np
);
977 if (np
->n_flag
& NNEEDINVALIDATE
) {
978 np
->n_flag
&= ~NNEEDINVALIDATE
;
980 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, ctx
, 1);
981 nfs_node_lock_force(np
);
983 if ((vtype
== VREG
) && (np
->n_flag
& NMODIFIED
) && (fflag
& FWRITE
)) {
984 /* we're closing an open for write and the file is modified, so flush it */
986 if (nfsvers
!= NFS_VER2
)
987 error
= nfs_flush(np
, MNT_WAIT
, vfs_context_thread(ctx
), 0);
989 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
990 nfs_node_lock_force(np
);
993 if (np
->n_flag
& NWRITEERR
) {
994 np
->n_flag
&= ~NWRITEERR
;
1000 /* Just mark that it was closed */
1001 lck_mtx_lock(&np
->n_openlock
);
1002 if (np
->n_openrefcnt
== 0) {
1003 if (fflag
& (FREAD
|FWRITE
)) {
1004 NP(np
, "nfs_vnop_close: open reference underrun");
1007 } else if (fflag
& (FREAD
|FWRITE
)) {
1010 /* No FREAD/FWRITE set - probably the final close */
1011 np
->n_openrefcnt
= 0;
1013 lck_mtx_unlock(&np
->n_openlock
);
1018 /* fflag should contain some combination of: FREAD, FWRITE, FHASLOCK */
1021 accessMode
|= NFS_OPEN_SHARE_ACCESS_READ
;
1023 accessMode
|= NFS_OPEN_SHARE_ACCESS_WRITE
;
1024 // XXX It would be nice if we still had the O_EXLOCK/O_SHLOCK flags that were on the open
1025 // if (fflag & O_EXLOCK)
1026 // denyMode = NFS_OPEN_SHARE_DENY_BOTH;
1027 // else if (fflag & O_SHLOCK)
1028 // denyMode = NFS_OPEN_SHARE_DENY_WRITE;
1030 // denyMode = NFS_OPEN_SHARE_DENY_NONE;
1032 if (fflag
& FHASLOCK
) {
1033 /* XXX assume FHASLOCK is for the deny mode and not flock */
1034 /* FHASLOCK flock will be unlocked in the close path, but the flag is not cleared. */
1035 if (nofp
->nof_deny
& NFS_OPEN_SHARE_DENY_READ
)
1036 denyMode
= NFS_OPEN_SHARE_DENY_BOTH
;
1037 else if (nofp
->nof_deny
& NFS_OPEN_SHARE_DENY_WRITE
)
1038 denyMode
= NFS_OPEN_SHARE_DENY_WRITE
;
1040 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
1042 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
1045 // XXX don't do deny modes just yet (and never do it for !v4)
1046 denyMode
= NFS_OPEN_SHARE_DENY_NONE
;
1051 * No mode given to close?
1052 * Guess this is the final close.
1053 * We should unlock all locks and close all opens.
1056 mount_t mp
= vnode_mount(vp
);
1057 int force
= (!mp
|| vfs_isforce(mp
));
1059 writers
= nfs_no_of_open_file_writers(np
);
1060 nfs_release_open_state_for_node(np
, force
);
1062 lck_mtx_lock(&nmp
->nm_lock
);
1063 if (writers
> nmp
->nm_writers
) {
1064 NP(np
, "nfs_vnop_close: number of write opens for mount underrun. Node has %d"
1065 " opens for write. Mount has total of %d opens for write\n",
1066 writers
, nmp
->nm_writers
);
1067 nmp
->nm_writers
= 0;
1069 nmp
->nm_writers
-= writers
;
1071 lck_mtx_unlock(&nmp
->nm_lock
);
1075 } else if (fflag
& FWRITE
) {
1076 lck_mtx_lock(&nmp
->nm_lock
);
1077 if (nmp
->nm_writers
== 0) {
1078 NP(np
, "nfs_vnop_close: removing open writer from mount, but mount has no files open for writing");
1082 lck_mtx_unlock(&nmp
->nm_lock
);
1086 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 0);
1088 // printf("nfs_vnop_close: can't get open owner!\n");
1093 error
= nfs_mount_state_in_use_start(nmp
, NULL
);
1095 nfs_open_owner_rele(noop
);
1099 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 0);
1100 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
1101 nfs_mount_state_in_use_end(nmp
, 0);
1102 error
= nfs4_reopen(nofp
, NULL
);
1108 NP(np
, "nfs_vnop_close: no open file for owner, error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
1112 error
= nfs_open_file_set_busy(nofp
, NULL
);
1118 error
= nfs_close(np
, nofp
, accessMode
, denyMode
, ctx
);
1120 NP(np
, "nfs_vnop_close: close error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
1124 nfs_open_file_clear_busy(nofp
);
1125 if (nfs_mount_state_in_use_end(nmp
, error
)) {
1132 NP(np
, "nfs_vnop_close: error %d, %d", error
, kauth_cred_getuid(noop
->noo_cred
));
1134 nfs_open_owner_rele(noop
);
1139 * nfs_close(): common function that does all the heavy lifting of file closure
1141 * Takes an open file structure and a set of access/deny modes and figures out how
1142 * to update the open file structure (and the state on the server) appropriately.
1147 struct nfs_open_file
*nofp
,
1148 uint32_t accessMode
,
1152 struct nfs_lock_owner
*nlop
;
1153 int error
= 0, changed
= 0, delegated
= 0, closed
= 0, downgrade
= 0;
1154 uint32_t newAccessMode
, newDenyMode
;
1156 /* warn if modes don't match current state */
1157 if (((accessMode
& nofp
->nof_access
) != accessMode
) || ((denyMode
& nofp
->nof_deny
) != denyMode
))
1158 NP(np
, "nfs_close: mode mismatch %d %d, current %d %d, %d",
1159 accessMode
, denyMode
, nofp
->nof_access
, nofp
->nof_deny
,
1160 kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1163 * If we're closing a write-only open, we may not have a write-only count
1164 * if we also grabbed read access. So, check the read-write count.
1166 if (denyMode
== NFS_OPEN_SHARE_DENY_NONE
) {
1167 if ((accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) &&
1168 (nofp
->nof_w
== 0) && (nofp
->nof_d_w
== 0) &&
1169 (nofp
->nof_rw
|| nofp
->nof_d_rw
))
1170 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
1171 } else if (denyMode
== NFS_OPEN_SHARE_DENY_WRITE
) {
1172 if ((accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) &&
1173 (nofp
->nof_w_dw
== 0) && (nofp
->nof_d_w_dw
== 0) &&
1174 (nofp
->nof_rw_dw
|| nofp
->nof_d_rw_dw
))
1175 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
1176 } else { /* NFS_OPEN_SHARE_DENY_BOTH */
1177 if ((accessMode
== NFS_OPEN_SHARE_ACCESS_WRITE
) &&
1178 (nofp
->nof_w_drw
== 0) && (nofp
->nof_d_w_drw
== 0) &&
1179 (nofp
->nof_rw_drw
|| nofp
->nof_d_rw_drw
))
1180 accessMode
= NFS_OPEN_SHARE_ACCESS_BOTH
;
1183 nfs_open_file_remove_open_find(nofp
, accessMode
, denyMode
, &newAccessMode
, &newDenyMode
, &delegated
);
1184 if ((newAccessMode
!= nofp
->nof_access
) || (newDenyMode
!= nofp
->nof_deny
))
1189 if (NFSTONMP(np
)->nm_vers
< NFS_VER4
) /* NFS v2/v3 closes simply need to remove the open. */
1192 if ((newAccessMode
== 0) || (nofp
->nof_opencnt
== 1)) {
1194 * No more access after this close, so clean up and close it.
1195 * Don't send a close RPC if we're closing a delegated open.
1199 if (!delegated
&& !(nofp
->nof_flags
& NFS_OPEN_FILE_LOST
))
1200 error
= nfs4_close_rpc(np
, nofp
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
), 0);
1201 if (error
== NFSERR_LOCKS_HELD
) {
1203 * Hmm... the server says we have locks we need to release first
1204 * Find the lock owner and try to unlock everything.
1206 nlop
= nfs_lock_owner_find(np
, vfs_context_proc(ctx
), 0);
1208 nfs4_unlock_rpc(np
, nlop
, F_WRLCK
, 0, UINT64_MAX
,
1209 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
1210 nfs_lock_owner_rele(nlop
);
1212 error
= nfs4_close_rpc(np
, nofp
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
), 0);
1214 } else if (changed
) {
1216 * File is still open but with less access, so downgrade the open.
1217 * Don't send a downgrade RPC if we're closing a delegated open.
1219 if (!delegated
&& !(nofp
->nof_flags
& NFS_OPEN_FILE_LOST
)) {
1222 * If we have delegated opens, we should probably claim them before sending
1223 * the downgrade because the server may not know the open we are downgrading to.
1225 if (nofp
->nof_d_rw_drw
|| nofp
->nof_d_w_drw
|| nofp
->nof_d_r_drw
||
1226 nofp
->nof_d_rw_dw
|| nofp
->nof_d_w_dw
|| nofp
->nof_d_r_dw
||
1227 nofp
->nof_d_rw
|| nofp
->nof_d_w
|| nofp
->nof_d_r
)
1228 nfs4_claim_delegated_state_for_open_file(nofp
, 0);
1229 /* need to remove the open before sending the downgrade */
1230 nfs_open_file_remove_open(nofp
, accessMode
, denyMode
);
1231 error
= nfs4_open_downgrade_rpc(np
, nofp
, ctx
);
1232 if (error
) /* Hmm.. that didn't work. Add the open back in. */
1233 nfs_open_file_add_open(nofp
, accessMode
, denyMode
, delegated
);
1238 NP(np
, "nfs_close: error %d, %d", error
, kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1244 nfs_open_file_remove_open(nofp
, accessMode
, denyMode
);
1247 lck_mtx_lock(&nofp
->nof_lock
);
1248 if (nofp
->nof_r
|| nofp
->nof_d_r
|| nofp
->nof_w
|| nofp
->nof_d_w
|| nofp
->nof_d_rw
||
1249 (nofp
->nof_rw
&& !((nofp
->nof_flags
& NFS_OPEN_FILE_CREATE
) && !nofp
->nof_creator
&& (nofp
->nof_rw
== 1))) ||
1250 nofp
->nof_r_dw
|| nofp
->nof_d_r_dw
|| nofp
->nof_w_dw
|| nofp
->nof_d_w_dw
||
1251 nofp
->nof_rw_dw
|| nofp
->nof_d_rw_dw
|| nofp
->nof_r_drw
|| nofp
->nof_d_r_drw
||
1252 nofp
->nof_w_drw
|| nofp
->nof_d_w_drw
|| nofp
->nof_rw_drw
|| nofp
->nof_d_rw_drw
)
1253 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",
1254 nofp
->nof_r
, nofp
->nof_d_r
, nofp
->nof_w
, nofp
->nof_d_w
,
1255 nofp
->nof_rw
, nofp
->nof_d_rw
, nofp
->nof_r_dw
, nofp
->nof_d_r_dw
,
1256 nofp
->nof_w_dw
, nofp
->nof_d_w_dw
, nofp
->nof_rw_dw
, nofp
->nof_d_rw_dw
,
1257 nofp
->nof_r_drw
, nofp
->nof_d_r_drw
, nofp
->nof_w_drw
, nofp
->nof_d_w_drw
,
1258 nofp
->nof_rw_drw
, nofp
->nof_d_rw_drw
, nofp
->nof_flags
,
1259 kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1260 /* clear out all open info, just to be safe */
1261 nofp
->nof_access
= nofp
->nof_deny
= 0;
1262 nofp
->nof_mmap_access
= nofp
->nof_mmap_deny
= 0;
1263 nofp
->nof_r
= nofp
->nof_d_r
= 0;
1264 nofp
->nof_w
= nofp
->nof_d_w
= 0;
1265 nofp
->nof_rw
= nofp
->nof_d_rw
= 0;
1266 nofp
->nof_r_dw
= nofp
->nof_d_r_dw
= 0;
1267 nofp
->nof_w_dw
= nofp
->nof_d_w_dw
= 0;
1268 nofp
->nof_rw_dw
= nofp
->nof_d_rw_dw
= 0;
1269 nofp
->nof_r_drw
= nofp
->nof_d_r_drw
= 0;
1270 nofp
->nof_w_drw
= nofp
->nof_d_w_drw
= 0;
1271 nofp
->nof_rw_drw
= nofp
->nof_d_rw_drw
= 0;
1272 nofp
->nof_flags
&= ~NFS_OPEN_FILE_CREATE
;
1273 lck_mtx_unlock(&nofp
->nof_lock
);
1274 /* XXX we may potentially want to clean up idle/unused open file structures */
1276 if (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
) {
1278 NP(np
, "nfs_close: LOST%s, %d", !nofp
->nof_opencnt
? " (last)" : "",
1279 kauth_cred_getuid(nofp
->nof_owner
->noo_cred
));
1294 struct nfs_vattr
*nvap
,
1297 struct nfsmount
*nmp
= mp
? VFSTONFS(mp
) : NFSTONMP(np
);
1298 int error
= 0, status
, nfsvers
, rpcflags
= 0;
1299 struct nfsm_chain nmreq
, nmrep
;
1301 if (nfs_mount_gone(nmp
))
1303 nfsvers
= nmp
->nm_vers
;
1305 if (flags
& NGA_MONITOR
) /* vnode monitor requests should be soft */
1306 rpcflags
= R_RECOVER
;
1308 if (flags
& NGA_SOFT
) /* Return ETIMEDOUT if server not responding */
1311 nfsm_chain_null(&nmreq
);
1312 nfsm_chain_null(&nmrep
);
1314 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nfsvers
));
1315 if (nfsvers
!= NFS_VER2
)
1316 nfsm_chain_add_32(error
, &nmreq
, fhsize
);
1317 nfsm_chain_add_opaque(error
, &nmreq
, fhp
, fhsize
);
1318 nfsm_chain_build_done(error
, &nmreq
);
1320 error
= nfs_request2(np
, mp
, &nmreq
, NFSPROC_GETATTR
,
1321 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
1322 NULL
, rpcflags
, &nmrep
, xidp
, &status
);
1326 error
= nfs_parsefattr(&nmrep
, nfsvers
, nvap
);
1328 nfsm_chain_cleanup(&nmreq
);
1329 nfsm_chain_cleanup(&nmrep
);
1334 * nfs_refresh_fh will attempt to update the file handle for the node.
1336 * It only does this for symbolic links and regular files that are not currently opened.
1338 * On Success returns 0 and the nodes file handle is updated, or ESTALE on failure.
1341 nfs_refresh_fh(nfsnode_t np
, vfs_context_t ctx
)
1343 vnode_t dvp
, vp
= NFSTOV(np
);
1345 const char *v_name
= vnode_getname(vp
);
1347 int namelen
, fhsize
, refreshed
;
1348 int error
, wanted
= 0;
1350 struct timespec ts
= {2, 0};
1352 NFS_VNOP_DBG("vnode is %d\n", vnode_vtype(vp
));
1354 dvp
= vnode_parent(vp
);
1355 if ((vnode_vtype(vp
) != VREG
&& vnode_vtype(vp
) != VLNK
) ||
1356 v_name
== NULL
|| *v_name
== '\0' || dvp
== NULL
) {
1358 vnode_putname(v_name
);
1363 namelen
= strlen(v_name
);
1364 MALLOC(name
, char *, namelen
+ 1, M_TEMP
, M_WAITOK
);
1366 vnode_putname(v_name
);
1369 bcopy(v_name
, name
, namelen
+1);
1370 NFS_VNOP_DBG("Trying to refresh %s : %s\n", v_name
, name
);
1371 vnode_putname(v_name
);
1373 /* Allocate the maximum size file handle */
1374 MALLOC(fhp
, uint8_t *, NFS4_FHSIZE
, M_TEMP
, M_WAITOK
);
1380 if ((error
= nfs_node_lock(np
))) {
1386 fhsize
= np
->n_fhsize
;
1387 bcopy(np
->n_fhp
, fhp
, fhsize
);
1388 while (ISSET(np
->n_flag
, NREFRESH
)) {
1389 SET(np
->n_flag
, NREFRESHWANT
);
1390 NFS_VNOP_DBG("Waiting for refresh of %s\n", name
);
1391 msleep(np
, &np
->n_lock
, PZERO
-1, "nfsrefreshwant", &ts
);
1392 if ((error
= nfs_sigintr(NFSTONMP(np
), NULL
, vfs_context_thread(ctx
), 0)))
1395 refreshed
= error
? 0 : !NFS_CMPFH(np
, fhp
, fhsize
);
1396 SET(np
->n_flag
, NREFRESH
);
1397 nfs_node_unlock(np
);
1399 NFS_VNOP_DBG("error = %d, refreshed = %d\n", error
, refreshed
);
1400 if (error
|| refreshed
)
1403 /* Check that there are no open references for this file */
1404 lck_mtx_lock(&np
->n_openlock
);
1405 if (np
->n_openrefcnt
|| !TAILQ_EMPTY(&np
->n_opens
) || !TAILQ_EMPTY(&np
->n_lock_owners
)) {
1407 struct nfs_open_file
*ofp
;
1409 TAILQ_FOREACH(ofp
, &np
->n_opens
, nof_link
) {
1410 cnt
+= ofp
->nof_opencnt
;
1413 lck_mtx_unlock(&np
->n_openlock
);
1414 NFS_VNOP_DBG("Can not refresh file handle for %s with open state\n", name
);
1415 NFS_VNOP_DBG("\topenrefcnt = %d, opens = %d lock_owners = %d\n",
1416 np
->n_openrefcnt
, cnt
, !TAILQ_EMPTY(&np
->n_lock_owners
));
1421 lck_mtx_unlock(&np
->n_openlock
);
1423 * Since the FH is currently stale we should not be able to
1424 * establish any open state until the FH is refreshed.
1427 error
= nfs_node_lock(np
);
1430 * Symlinks should never need invalidations and are holding
1431 * the one and only nfsbuf in an uncached acquired state
1432 * trying to do a readlink. So we will hang if we invalidate
1433 * in that case. Only in in the VREG case do we need to
1436 if (vnode_vtype(vp
) == VREG
) {
1437 np
->n_flag
&= ~NNEEDINVALIDATE
;
1438 nfs_node_unlock(np
);
1439 error
= nfs_vinvalbuf(vp
, V_IGNORE_WRITEERR
, ctx
, 1);
1441 NFS_VNOP_DBG("nfs_vinvalbuf returned %d\n", error
);
1444 nfs_node_unlock(np
);
1447 NFS_VNOP_DBG("Looking up %s\n", name
);
1448 error
= nfs_lookitup(dnp
, name
, namelen
, ctx
, &np
);
1450 NFS_VNOP_DBG("nfs_lookitup returned %d\n", error
);
1453 nfs_node_lock_force(np
);
1454 wanted
= ISSET(np
->n_flag
, NREFRESHWANT
);
1455 CLR(np
->n_flag
, NREFRESH
|NREFRESHWANT
);
1456 nfs_node_unlock(np
);
1461 NFS_VNOP_DBG("%s refreshed file handle\n", name
);
1466 return (error
? ESTALE
: 0);
1470 nfs_getattr(nfsnode_t np
, struct nfs_vattr
*nvap
, vfs_context_t ctx
, int flags
)
1475 error
= nfs_getattr_internal(np
, nvap
, ctx
, flags
);
1476 if (error
== ESTALE
) {
1477 error
= nfs_refresh_fh(np
, ctx
);
1485 nfs_getattr_internal(nfsnode_t np
, struct nfs_vattr
*nvap
, vfs_context_t ctx
, int flags
)
1487 struct nfsmount
*nmp
;
1488 int error
= 0, nfsvers
, inprogset
= 0, wanted
= 0, avoidfloods
;
1489 struct nfs_vattr nvattr
;
1490 struct timespec ts
= { 2, 0 };
1493 FSDBG_TOP(513, np
->n_size
, np
, np
->n_vattr
.nva_size
, np
->n_flag
);
1497 if (nfs_mount_gone(nmp
))
1499 nfsvers
= nmp
->nm_vers
;
1505 /* Update local times for special files. */
1506 if (np
->n_flag
& (NACC
| NUPD
)) {
1507 nfs_node_lock_force(np
);
1509 nfs_node_unlock(np
);
1511 /* Update size, if necessary */
1512 if (ISSET(np
->n_flag
, NUPDATESIZE
))
1513 nfs_data_update_size(np
, 0);
1515 error
= nfs_node_lock(np
);
1517 if (!(flags
& (NGA_UNCACHED
|NGA_MONITOR
)) || ((nfsvers
>= NFS_VER4
) && (np
->n_openflags
& N_DELEG_MASK
))) {
1519 * Use the cache or wait for any getattr in progress if:
1520 * - it's a cached request, or
1521 * - we have a delegation, or
1522 * - the server isn't responding
1525 error
= nfs_getattrcache(np
, nvap
, flags
);
1526 if (!error
|| (error
!= ENOENT
)) {
1527 nfs_node_unlock(np
);
1531 if (!ISSET(np
->n_flag
, NGETATTRINPROG
))
1533 if (flags
& NGA_MONITOR
) {
1534 /* no need to wait if a request is pending */
1535 error
= EINPROGRESS
;
1536 nfs_node_unlock(np
);
1539 SET(np
->n_flag
, NGETATTRWANT
);
1540 msleep(np
, &np
->n_lock
, PZERO
-1, "nfsgetattrwant", &ts
);
1541 if ((error
= nfs_sigintr(NFSTONMP(np
), NULL
, vfs_context_thread(ctx
), 0))) {
1542 nfs_node_unlock(np
);
1546 SET(np
->n_flag
, NGETATTRINPROG
);
1548 } else if (!ISSET(np
->n_flag
, NGETATTRINPROG
)) {
1549 SET(np
->n_flag
, NGETATTRINPROG
);
1551 } else if (flags
& NGA_MONITOR
) {
1552 /* no need to make a request if one is pending */
1553 error
= EINPROGRESS
;
1555 nfs_node_unlock(np
);
1558 if (nfs_mount_gone(nmp
))
1564 * Return cached attributes if they are valid,
1565 * if the server doesn't respond, and this is
1566 * some softened up style of mount.
1568 if (NATTRVALID(np
) && nfs_use_cache(nmp
))
1572 * We might want to try to get both the attributes and access info by
1573 * making an ACCESS call and seeing if it returns updated attributes.
1574 * But don't bother if we aren't caching access info or if the
1575 * attributes returned wouldn't be cached.
1577 if (!(flags
& NGA_ACL
) && (nfsvers
!= NFS_VER2
) && nfs_access_for_getattr
&& (nfs_access_cache_timeout
> 0)) {
1578 if (nfs_attrcachetimeout(np
) > 0) {
1579 /* OSAddAtomic(1, &nfsstats.accesscache_misses); */
1580 u_int32_t access
= NFS_ACCESS_ALL
;
1583 /* Return cached attrs if server doesn't respond */
1584 if (flags
& NGA_SOFT
)
1587 error
= nmp
->nm_funcs
->nf_access_rpc(np
, &access
, rpcflags
, ctx
);
1589 if (error
== ETIMEDOUT
)
1594 nfs_node_lock_force(np
);
1595 error
= nfs_getattrcache(np
, nvap
, flags
);
1596 nfs_node_unlock(np
);
1597 if (!error
|| (error
!= ENOENT
))
1599 /* Well, that didn't work... just do a getattr... */
1607 error
= nmp
->nm_funcs
->nf_getattr_rpc(np
, NULL
, np
->n_fhp
, np
->n_fhsize
, flags
, ctx
, nvap
, &xid
);
1609 nfs_node_lock_force(np
);
1610 error
= nfs_loadattrcache(np
, nvap
, &xid
, 0);
1611 nfs_node_unlock(np
);
1615 * If the server didn't respond, return cached attributes.
1618 if ((flags
& NGA_SOFT
) && (error
== ETIMEDOUT
)) {
1619 nfs_node_lock_force(np
);
1620 error
= nfs_getattrcache(np
, nvap
, flags
);
1621 if (!error
|| (error
!= ENOENT
)) {
1622 nfs_node_unlock(np
);
1625 nfs_node_unlock(np
);
1629 if (!xid
) { /* out-of-order rpc - attributes were dropped */
1630 FSDBG(513, -1, np
, np
->n_xid
>> 32, np
->n_xid
);
1631 if (avoidfloods
++ < 20)
1633 /* avoidfloods>1 is bizarre. at 20 pull the plug */
1634 /* just return the last attributes we got */
1637 nfs_node_lock_force(np
);
1639 wanted
= ISSET(np
->n_flag
, NGETATTRWANT
);
1640 CLR(np
->n_flag
, (NGETATTRINPROG
| NGETATTRWANT
));
1643 /* check if the node changed on us */
1644 vnode_t vp
= NFSTOV(np
);
1645 enum vtype vtype
= vnode_vtype(vp
);
1646 if ((vtype
== VDIR
) && NFS_CHANGED_NC(nfsvers
, np
, nvap
)) {
1647 FSDBG(513, -1, np
, 0, np
);
1648 np
->n_flag
&= ~NNEGNCENTRIES
;
1651 NFS_CHANGED_UPDATE_NC(nfsvers
, np
, nvap
);
1652 NFS_VNOP_DBG("Purge directory 0x%llx\n",
1653 (uint64_t)VM_KERNEL_ADDRPERM(vp
));
1655 if (NFS_CHANGED(nfsvers
, np
, nvap
)) {
1656 FSDBG(513, -1, np
, -1, np
);
1657 if (vtype
== VDIR
) {
1658 NFS_VNOP_DBG("Invalidate directory 0x%llx\n",
1659 (uint64_t)VM_KERNEL_ADDRPERM(vp
));
1662 nfs_node_unlock(np
);
1665 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
1666 FSDBG(513, -1, np
, -2, error
);
1668 nfs_node_lock_force(np
);
1669 NFS_CHANGED_UPDATE(nfsvers
, np
, nvap
);
1670 nfs_node_unlock(np
);
1673 nfs_node_unlock(np
);
1678 nfs_node_unlock(np
);
1683 if (nvap
== &nvattr
) {
1684 NVATTR_CLEANUP(nvap
);
1685 } else if (!(flags
& NGA_ACL
)) {
1686 /* make sure we don't return an ACL if it wasn't asked for */
1687 NFS_BITMAP_CLR(nvap
->nva_bitmap
, NFS_FATTR_ACL
);
1688 if (nvap
->nva_acl
) {
1689 kauth_acl_free(nvap
->nva_acl
);
1690 nvap
->nva_acl
= NULL
;
1693 FSDBG_BOT(513, np
->n_size
, error
, np
->n_vattr
.nva_size
, np
->n_flag
);
1698 * NFS getattr call from vfs.
1702 * The attributes we support over the wire.
1703 * We also get fsid but the vfs layer gets it out of the mount
1704 * structure after this calling us so there's no need to return it,
1705 * and Finder expects to call getattrlist just looking for the FSID
1706 * with out hanging on a non responsive server.
1708 #define NFS3_SUPPORTED_VATTRS \
1709 (VNODE_ATTR_va_rdev | \
1710 VNODE_ATTR_va_nlink | \
1711 VNODE_ATTR_va_data_size | \
1712 VNODE_ATTR_va_data_alloc | \
1713 VNODE_ATTR_va_uid | \
1714 VNODE_ATTR_va_gid | \
1715 VNODE_ATTR_va_mode | \
1716 VNODE_ATTR_va_modify_time | \
1717 VNODE_ATTR_va_change_time | \
1718 VNODE_ATTR_va_access_time | \
1719 VNODE_ATTR_va_fileid | \
1724 struct vnop_getattr_args
/* {
1725 struct vnodeop_desc *a_desc;
1727 struct vnode_attr *a_vap;
1728 vfs_context_t a_context;
1732 struct nfs_vattr nva
;
1733 struct vnode_attr
*vap
= ap
->a_vap
;
1734 struct nfsmount
*nmp
;
1738 * Lets don't go over the wire if we don't support any of the attributes.
1739 * Just fall through at the VFS layer and let it cons up what it needs.
1741 /* Return the io size no matter what, since we don't go over the wire for this */
1742 VATTR_RETURN(vap
, va_iosize
, nfs_iosize
);
1743 if ((vap
->va_active
& NFS3_SUPPORTED_VATTRS
) == 0)
1746 if (VATTR_IS_ACTIVE(ap
->a_vap
, va_name
))
1747 NFS_VNOP_DBG("Getting attrs for 0x%llx, vname is %s\n",
1748 (uint64_t)VM_KERNEL_ADDRPERM(ap
->a_vp
),
1749 ap
->a_vp
->v_name
? ap
->a_vp
->v_name
: "empty");
1750 error
= nfs_getattr(VTONFS(ap
->a_vp
), &nva
, ap
->a_context
, NGA_CACHED
);
1754 /* copy nva to *a_vap */
1755 nmp
= VTONMP(ap
->a_vp
);
1756 vap
->va_flags
|= nmp
? (nmp
->nm_vers
> 2 ? VA_64BITOBJIDS
: 0) : 0;
1757 VATTR_RETURN(vap
, va_type
, nva
.nva_type
);
1758 VATTR_RETURN(vap
, va_mode
, nva
.nva_mode
);
1759 rdev
= makedev(nva
.nva_rawdev
.specdata1
, nva
.nva_rawdev
.specdata2
);
1760 VATTR_RETURN(vap
, va_rdev
, rdev
);
1761 VATTR_RETURN(vap
, va_uid
, nva
.nva_uid
);
1762 VATTR_RETURN(vap
, va_gid
, nva
.nva_gid
);
1763 VATTR_RETURN(vap
, va_nlink
, nva
.nva_nlink
);
1764 VATTR_RETURN(vap
, va_fileid
, nva
.nva_fileid
);
1765 VATTR_RETURN(vap
, va_data_size
, nva
.nva_size
);
1766 VATTR_RETURN(vap
, va_data_alloc
, nva
.nva_bytes
);
1767 vap
->va_access_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_ACCESS
];
1768 vap
->va_access_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_ACCESS
];
1769 VATTR_SET_SUPPORTED(vap
, va_access_time
);
1770 vap
->va_modify_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_MODIFY
];
1771 vap
->va_modify_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_MODIFY
];
1772 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
1773 vap
->va_change_time
.tv_sec
= nva
.nva_timesec
[NFSTIME_CHANGE
];
1774 vap
->va_change_time
.tv_nsec
= nva
.nva_timensec
[NFSTIME_CHANGE
];
1775 VATTR_SET_SUPPORTED(vap
, va_change_time
);
1777 // VATTR_RETURN(vap, va_encoding, 0xffff /* kTextEncodingUnknown */);
1786 struct vnop_setattr_args
/* {
1787 struct vnodeop_desc *a_desc;
1789 struct vnode_attr *a_vap;
1790 vfs_context_t a_context;
1793 vfs_context_t ctx
= ap
->a_context
;
1794 vnode_t vp
= ap
->a_vp
;
1795 nfsnode_t np
= VTONFS(vp
);
1796 struct nfsmount
*nmp
;
1797 struct vnode_attr
*vap
= ap
->a_vap
;
1799 int biosize
, nfsvers
, namedattrs
;
1800 u_quad_t origsize
, vapsize
;
1801 struct nfs_dulookup dul
;
1802 nfsnode_t dnp
= NULL
;
1803 int dul_in_progress
= 0;
1805 const char *vname
= NULL
;
1806 struct nfs_open_owner
*noop
= NULL
;
1807 struct nfs_open_file
*nofp
= NULL
;
1810 if (nfs_mount_gone(nmp
))
1812 nfsvers
= nmp
->nm_vers
;
1813 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
1814 biosize
= nmp
->nm_biosize
;
1816 /* Disallow write attempts if the filesystem is mounted read-only. */
1817 if (vnode_vfsisrdonly(vp
))
1820 origsize
= np
->n_size
;
1821 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
1822 switch (vnode_vtype(vp
)) {
1829 if (!VATTR_IS_ACTIVE(vap
, va_modify_time
) &&
1830 !VATTR_IS_ACTIVE(vap
, va_access_time
) &&
1831 !VATTR_IS_ACTIVE(vap
, va_mode
) &&
1832 !VATTR_IS_ACTIVE(vap
, va_uid
) &&
1833 !VATTR_IS_ACTIVE(vap
, va_gid
)) {
1836 VATTR_CLEAR_ACTIVE(vap
, va_data_size
);
1840 * Disallow write attempts if the filesystem is
1841 * mounted read-only.
1843 if (vnode_vfsisrdonly(vp
))
1845 FSDBG_TOP(512, np
->n_size
, vap
->va_data_size
,
1846 np
->n_vattr
.nva_size
, np
->n_flag
);
1847 /* clear NNEEDINVALIDATE, if set */
1848 if ((error
= nfs_node_lock(np
)))
1850 if (np
->n_flag
& NNEEDINVALIDATE
)
1851 np
->n_flag
&= ~NNEEDINVALIDATE
;
1852 nfs_node_unlock(np
);
1853 /* flush everything */
1854 error
= nfs_vinvalbuf(vp
, (vap
->va_data_size
? V_SAVE
: 0) , ctx
, 1);
1856 NP(np
, "nfs_setattr: nfs_vinvalbuf %d", error
);
1857 FSDBG_BOT(512, np
->n_size
, vap
->va_data_size
, np
->n_vattr
.nva_size
, -1);
1860 if (nfsvers
>= NFS_VER4
) {
1861 /* setting file size requires having the file open for write access */
1862 if (np
->n_flag
& NREVOKE
)
1864 noop
= nfs_open_owner_find(nmp
, vfs_context_ucred(ctx
), 1);
1868 error
= nfs_mount_state_in_use_start(nmp
, vfs_context_thread(ctx
));
1871 if (np
->n_flag
& NREVOKE
) {
1872 nfs_mount_state_in_use_end(nmp
, 0);
1875 error
= nfs_open_file_find(np
, noop
, &nofp
, 0, 0, 1);
1876 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_LOST
))
1878 if (!error
&& (nofp
->nof_flags
& NFS_OPEN_FILE_REOPEN
)) {
1879 nfs_mount_state_in_use_end(nmp
, 0);
1880 error
= nfs4_reopen(nofp
, vfs_context_thread(ctx
));
1886 error
= nfs_open_file_set_busy(nofp
, vfs_context_thread(ctx
));
1888 nfs_open_owner_rele(noop
);
1891 if (!(nofp
->nof_access
& NFS_OPEN_SHARE_ACCESS_WRITE
)) {
1892 /* we don't have the file open for write access, so open it */
1893 error
= nfs4_open(np
, nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
1895 nofp
->nof_flags
|= NFS_OPEN_FILE_SETATTR
;
1896 if (nfs_mount_state_error_should_restart(error
)) {
1897 nfs_open_file_clear_busy(nofp
);
1899 if (nfs_mount_state_in_use_end(nmp
, error
))
1904 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
1905 if (np
->n_size
> vap
->va_data_size
) { /* shrinking? */
1907 int neweofoff
, mustwrite
;
1910 obn
= (np
->n_size
- 1) / biosize
;
1911 bn
= vap
->va_data_size
/ biosize
;
1912 for ( ; obn
>= bn
; obn
--) {
1913 if (!nfs_buf_is_incore(np
, obn
))
1915 error
= nfs_buf_get(np
, obn
, biosize
, NULL
, NBLK_READ
, &bp
);
1919 FSDBG(512, bp
, bp
->nb_flags
, 0, obn
);
1920 SET(bp
->nb_flags
, NB_INVAL
);
1921 nfs_buf_release(bp
, 1);
1925 neweofoff
= vap
->va_data_size
- NBOFF(bp
);
1926 /* check for any dirty data before the new EOF */
1927 if ((bp
->nb_dirtyend
> 0) && (bp
->nb_dirtyoff
< neweofoff
)) {
1928 /* clip dirty range to EOF */
1929 if (bp
->nb_dirtyend
> neweofoff
) {
1930 bp
->nb_dirtyend
= neweofoff
;
1931 if (bp
->nb_dirtyoff
>= bp
->nb_dirtyend
)
1932 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
1934 if ((bp
->nb_dirtyend
> 0) && (bp
->nb_dirtyoff
< neweofoff
))
1937 bp
->nb_dirty
&= (1 << round_page_32(neweofoff
)/PAGE_SIZE
) - 1;
1941 FSDBG(512, bp
, bp
->nb_flags
, 0, obn
);
1942 SET(bp
->nb_flags
, NB_INVAL
);
1943 nfs_buf_release(bp
, 1);
1946 /* gotta write out dirty data before invalidating */
1947 /* (NB_STABLE indicates that data writes should be FILESYNC) */
1948 /* (NB_NOCACHE indicates buffer should be discarded) */
1949 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
| NB_ASYNC
| NB_READ
));
1950 SET(bp
->nb_flags
, NB_STABLE
| NB_NOCACHE
);
1951 if (!IS_VALID_CRED(bp
->nb_wcred
)) {
1952 kauth_cred_t cred
= vfs_context_ucred(ctx
);
1953 kauth_cred_ref(cred
);
1954 bp
->nb_wcred
= cred
;
1956 error
= nfs_buf_write(bp
);
1957 // Note: bp has been released
1959 FSDBG(512, bp
, 0xd00dee, 0xbad, error
);
1960 nfs_node_lock_force(np
);
1961 np
->n_error
= error
;
1962 np
->n_flag
|= NWRITEERR
;
1964 * There was a write error and we need to
1965 * invalidate attrs and flush buffers in
1966 * order to sync up with the server.
1967 * (if this write was extending the file,
1968 * we may no longer know the correct size)
1970 NATTRINVALIDATE(np
);
1971 nfs_node_unlock(np
);
1972 nfs_data_unlock(np
);
1973 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, ctx
, 1);
1974 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
1979 if (vap
->va_data_size
!= np
->n_size
)
1980 ubc_setsize(vp
, (off_t
)vap
->va_data_size
); /* XXX error? */
1981 origsize
= np
->n_size
;
1982 np
->n_size
= np
->n_vattr
.nva_size
= vap
->va_data_size
;
1983 nfs_node_lock_force(np
);
1984 CLR(np
->n_flag
, NUPDATESIZE
);
1985 nfs_node_unlock(np
);
1986 FSDBG(512, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0001);
1988 } else if (VATTR_IS_ACTIVE(vap
, va_modify_time
) ||
1989 VATTR_IS_ACTIVE(vap
, va_access_time
) ||
1990 (vap
->va_vaflags
& VA_UTIMES_NULL
)) {
1991 if ((error
= nfs_node_lock(np
)))
1993 if ((np
->n_flag
& NMODIFIED
) && (vnode_vtype(vp
) == VREG
)) {
1994 nfs_node_unlock(np
);
1995 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
1999 nfs_node_unlock(np
);
2002 if ((VATTR_IS_ACTIVE(vap
, va_mode
) || VATTR_IS_ACTIVE(vap
, va_uid
) || VATTR_IS_ACTIVE(vap
, va_gid
) ||
2003 VATTR_IS_ACTIVE(vap
, va_acl
) || VATTR_IS_ACTIVE(vap
, va_uuuid
) || VATTR_IS_ACTIVE(vap
, va_guuid
)) &&
2004 !(error
= nfs_node_lock(np
))) {
2005 NACCESSINVALIDATE(np
);
2006 nfs_node_unlock(np
);
2008 dvp
= vnode_getparent(vp
);
2009 vname
= vnode_getname(vp
);
2010 dnp
= (dvp
&& vname
) ? VTONFS(dvp
) : NULL
;
2012 if (nfs_node_set_busy(dnp
, vfs_context_thread(ctx
))) {
2014 vnode_putname(vname
);
2016 nfs_dulookup_init(&dul
, dnp
, vname
, strlen(vname
), ctx
);
2017 nfs_dulookup_start(&dul
, dnp
, ctx
);
2018 dul_in_progress
= 1;
2024 vnode_putname(vname
);
2030 error
= nmp
->nm_funcs
->nf_setattr_rpc(np
, vap
, ctx
);
2032 if (dul_in_progress
) {
2033 nfs_dulookup_finish(&dul
, dnp
, ctx
);
2034 nfs_node_clear_busy(dnp
);
2036 vnode_putname(vname
);
2039 FSDBG_BOT(512, np
->n_size
, vap
->va_data_size
, np
->n_vattr
.nva_size
, error
);
2040 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
2041 if (error
&& (origsize
!= np
->n_size
) &&
2042 ((nfsvers
< NFS_VER4
) || !nfs_mount_state_error_should_restart(error
))) {
2043 /* make every effort to resync file size w/ server... */
2044 /* (don't bother if we'll be restarting the operation) */
2045 int err
; /* preserve "error" for return */
2046 np
->n_size
= np
->n_vattr
.nva_size
= origsize
;
2047 nfs_node_lock_force(np
);
2048 CLR(np
->n_flag
, NUPDATESIZE
);
2049 nfs_node_unlock(np
);
2050 FSDBG(512, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0002);
2051 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX check error */
2052 vapsize
= vap
->va_data_size
;
2053 vap
->va_data_size
= origsize
;
2054 err
= nmp
->nm_funcs
->nf_setattr_rpc(np
, vap
, ctx
);
2056 NP(np
, "nfs_vnop_setattr: nfs%d_setattr_rpc %d %d", nfsvers
, error
, err
);
2057 vap
->va_data_size
= vapsize
;
2059 nfs_node_lock_force(np
);
2061 * The size was just set. If the size is already marked for update, don't
2062 * trust the newsize (it may have been set while the setattr was in progress).
2063 * Clear the update flag and make sure we fetch new attributes so we are sure
2064 * we have the latest size.
2066 if (ISSET(np
->n_flag
, NUPDATESIZE
)) {
2067 CLR(np
->n_flag
, NUPDATESIZE
);
2068 NATTRINVALIDATE(np
);
2069 nfs_node_unlock(np
);
2070 nfs_getattr(np
, NULL
, ctx
, NGA_UNCACHED
);
2072 nfs_node_unlock(np
);
2074 nfs_data_unlock(np
);
2075 if (nfsvers
>= NFS_VER4
) {
2077 /* don't close our setattr open if we'll be restarting... */
2078 if (!nfs_mount_state_error_should_restart(error
) &&
2079 (nofp
->nof_flags
& NFS_OPEN_FILE_SETATTR
)) {
2080 int err
= nfs_close(np
, nofp
, NFS_OPEN_SHARE_ACCESS_WRITE
, NFS_OPEN_SHARE_DENY_NONE
, ctx
);
2082 NP(np
, "nfs_vnop_setattr: close error: %d", err
);
2083 nofp
->nof_flags
&= ~NFS_OPEN_FILE_SETATTR
;
2085 nfs_open_file_clear_busy(nofp
);
2088 if (nfs_mount_state_in_use_end(nmp
, error
))
2090 nfs_open_owner_rele(noop
);
2097 * Do an NFS setattr RPC.
2102 struct vnode_attr
*vap
,
2105 struct nfsmount
*nmp
= NFSTONMP(np
);
2106 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0, nfsvers
;
2107 u_int64_t xid
, nextxid
;
2108 struct nfsm_chain nmreq
, nmrep
;
2110 if (nfs_mount_gone(nmp
))
2112 nfsvers
= nmp
->nm_vers
;
2114 VATTR_SET_SUPPORTED(vap
, va_mode
);
2115 VATTR_SET_SUPPORTED(vap
, va_uid
);
2116 VATTR_SET_SUPPORTED(vap
, va_gid
);
2117 VATTR_SET_SUPPORTED(vap
, va_data_size
);
2118 VATTR_SET_SUPPORTED(vap
, va_access_time
);
2119 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
2121 if (VATTR_IS_ACTIVE(vap
, va_flags
)) {
2122 if (vap
->va_flags
) { /* we don't support setting flags */
2123 if (vap
->va_active
& ~VNODE_ATTR_va_flags
)
2124 return (EINVAL
); /* return EINVAL if other attributes also set */
2126 return (ENOTSUP
); /* return ENOTSUP for chflags(2) */
2128 /* no flags set, so we'll just ignore it */
2129 if (!(vap
->va_active
& ~VNODE_ATTR_va_flags
))
2130 return (0); /* no (other) attributes to set, so nothing to do */
2133 nfsm_chain_null(&nmreq
);
2134 nfsm_chain_null(&nmrep
);
2136 nfsm_chain_build_alloc_init(error
, &nmreq
,
2137 NFSX_FH(nfsvers
) + NFSX_SATTR(nfsvers
));
2138 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
2139 if (nfsvers
== NFS_VER3
) {
2140 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
2141 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
2142 nfsm_chain_add_32(error
, &nmreq
, vap
->va_mode
);
2144 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2146 if (VATTR_IS_ACTIVE(vap
, va_uid
)) {
2147 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
2148 nfsm_chain_add_32(error
, &nmreq
, vap
->va_uid
);
2150 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2152 if (VATTR_IS_ACTIVE(vap
, va_gid
)) {
2153 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
2154 nfsm_chain_add_32(error
, &nmreq
, vap
->va_gid
);
2156 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2158 if (VATTR_IS_ACTIVE(vap
, va_data_size
)) {
2159 nfsm_chain_add_32(error
, &nmreq
, TRUE
);
2160 nfsm_chain_add_64(error
, &nmreq
, vap
->va_data_size
);
2162 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2164 if (vap
->va_vaflags
& VA_UTIMES_NULL
) {
2165 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_SERVER
);
2166 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_SERVER
);
2168 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
2169 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_CLIENT
);
2170 nfsm_chain_add_32(error
, &nmreq
, vap
->va_access_time
.tv_sec
);
2171 nfsm_chain_add_32(error
, &nmreq
, vap
->va_access_time
.tv_nsec
);
2173 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_DONT_CHANGE
);
2175 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
2176 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_SET_TO_CLIENT
);
2177 nfsm_chain_add_32(error
, &nmreq
, vap
->va_modify_time
.tv_sec
);
2178 nfsm_chain_add_32(error
, &nmreq
, vap
->va_modify_time
.tv_nsec
);
2180 nfsm_chain_add_32(error
, &nmreq
, NFS_TIME_DONT_CHANGE
);
2183 nfsm_chain_add_32(error
, &nmreq
, FALSE
);
2185 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_mode
) ?
2186 vtonfsv2_mode(vnode_vtype(NFSTOV(np
)), vap
->va_mode
) : -1);
2187 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_uid
) ?
2188 vap
->va_uid
: (uint32_t)-1);
2189 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_gid
) ?
2190 vap
->va_gid
: (uint32_t)-1);
2191 nfsm_chain_add_32(error
, &nmreq
, VATTR_IS_ACTIVE(vap
, va_data_size
) ?
2192 vap
->va_data_size
: (uint32_t)-1);
2193 if (VATTR_IS_ACTIVE(vap
, va_access_time
)) {
2194 nfsm_chain_add_32(error
, &nmreq
, vap
->va_access_time
.tv_sec
);
2195 nfsm_chain_add_32(error
, &nmreq
, (vap
->va_access_time
.tv_nsec
!= -1) ?
2196 ((uint32_t)vap
->va_access_time
.tv_nsec
/ 1000) : 0xffffffff);
2198 nfsm_chain_add_32(error
, &nmreq
, -1);
2199 nfsm_chain_add_32(error
, &nmreq
, -1);
2201 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
2202 nfsm_chain_add_32(error
, &nmreq
, vap
->va_modify_time
.tv_sec
);
2203 nfsm_chain_add_32(error
, &nmreq
, (vap
->va_modify_time
.tv_nsec
!= -1) ?
2204 ((uint32_t)vap
->va_modify_time
.tv_nsec
/ 1000) : 0xffffffff);
2206 nfsm_chain_add_32(error
, &nmreq
, -1);
2207 nfsm_chain_add_32(error
, &nmreq
, -1);
2210 nfsm_chain_build_done(error
, &nmreq
);
2212 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_SETATTR
, ctx
, NULL
, &nmrep
, &xid
, &status
);
2213 if ((lockerror
= nfs_node_lock(np
)))
2215 if (nfsvers
== NFS_VER3
) {
2216 struct timespec premtime
= { 0, 0 };
2217 nfsm_chain_get_wcc_data(error
, &nmrep
, np
, &premtime
, &wccpostattr
, &xid
);
2219 /* if file hadn't changed, update cached mtime */
2220 if (nfstimespeccmp(&np
->n_mtime
, &premtime
, ==))
2221 NFS_CHANGED_UPDATE(nfsvers
, np
, &np
->n_vattr
);
2222 /* if directory hadn't changed, update namecache mtime */
2223 if ((vnode_vtype(NFSTOV(np
)) == VDIR
) &&
2224 nfstimespeccmp(&np
->n_ncmtime
, &premtime
, ==))
2225 NFS_CHANGED_UPDATE_NC(nfsvers
, np
, &np
->n_vattr
);
2227 NATTRINVALIDATE(np
);
2232 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
2235 * We just changed the attributes and we want to make sure that we
2236 * see the latest attributes. Get the next XID. If it's not the
2237 * next XID after the SETATTR XID, then it's possible that another
2238 * RPC was in flight at the same time and it might put stale attributes
2239 * in the cache. In that case, we invalidate the attributes and set
2240 * the attribute cache XID to guarantee that newer attributes will
2244 nfs_get_xid(&nextxid
);
2245 if (nextxid
!= (xid
+ 1)) {
2246 np
->n_xid
= nextxid
;
2247 NATTRINVALIDATE(np
);
2251 nfs_node_unlock(np
);
2252 nfsm_chain_cleanup(&nmreq
);
2253 nfsm_chain_cleanup(&nmrep
);
2258 * NFS lookup call, one step at a time...
2259 * First look in cache
2260 * If not found, unlock the directory nfsnode and do the RPC
2264 struct vnop_lookup_args
/* {
2265 struct vnodeop_desc *a_desc;
2268 struct componentname *a_cnp;
2269 vfs_context_t a_context;
2272 vfs_context_t ctx
= ap
->a_context
;
2273 struct componentname
*cnp
= ap
->a_cnp
;
2274 vnode_t dvp
= ap
->a_dvp
;
2275 vnode_t
*vpp
= ap
->a_vpp
;
2276 int flags
= cnp
->cn_flags
;
2279 struct nfsmount
*nmp
;
2281 int nfsvers
, error
, busyerror
= ENOENT
, isdot
, isdotdot
, negnamecache
;
2283 struct nfs_vattr nvattr
;
2285 struct vnop_access_args naa
;
2287 struct nfsreq rq
, *req
= &rq
;
2292 NVATTR_INIT(&nvattr
);
2294 mp
= vnode_mount(dvp
);
2296 if (nfs_mount_gone(nmp
)) {
2300 nfsvers
= nmp
->nm_vers
;
2301 negnamecache
= !NMFLAG(nmp
, NONEGNAMECACHE
);
2303 if ((error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
))))
2305 /* nfs_getattr() will check changed and purge caches */
2306 if ((error
= nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
)))
2309 error
= cache_lookup(dvp
, vpp
, cnp
);
2312 /* negative cache entry */
2316 if ((nfsvers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
)) {
2317 /* if rdirplus, try dir buf cache lookup */
2318 error
= nfs_dir_buf_cache_lookup(dnp
, &np
, cnp
, ctx
, 0);
2320 /* dir buf cache hit */
2325 if (error
!= -1) /* cache miss */
2329 /* cache hit, not really an error */
2330 OSAddAtomic64(1, &nfsstats
.lookupcache_hits
);
2332 nfs_node_clear_busy(dnp
);
2335 /* check for directory access */
2336 naa
.a_desc
= &vnop_access_desc
;
2338 naa
.a_action
= KAUTH_VNODE_SEARCH
;
2339 naa
.a_context
= ctx
;
2341 /* compute actual success/failure based on accessibility */
2342 error
= nfs_vnop_access(&naa
);
2345 /* unexpected error from cache_lookup */
2349 /* skip lookup, if we know who we are: "." or ".." */
2350 isdot
= isdotdot
= 0;
2351 if (cnp
->cn_nameptr
[0] == '.') {
2352 if (cnp
->cn_namelen
== 1)
2354 if ((cnp
->cn_namelen
== 2) && (cnp
->cn_nameptr
[1] == '.'))
2357 if (isdotdot
|| isdot
) {
2361 if ((nfsvers
>= NFS_VER4
) && (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER
)) {
2362 /* we should never be looking things up in a trigger directory, return nothing */
2367 /* do we know this name is too long? */
2369 if (nfs_mount_gone(nmp
)) {
2373 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXNAME
) &&
2374 (cnp
->cn_namelen
> (int)nmp
->nm_fsattr
.nfsa_maxname
)) {
2375 error
= ENAMETOOLONG
;
2382 OSAddAtomic64(1, &nfsstats
.lookupcache_misses
);
2384 error
= nmp
->nm_funcs
->nf_lookup_rpc_async(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &req
);
2386 error
= nmp
->nm_funcs
->nf_lookup_rpc_async_finish(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, req
, &xid
, &fh
, &nvattr
);
2389 /* is the file handle the same as this directory's file handle? */
2390 isdot
= NFS_CMPFH(dnp
, fh
.fh_data
, fh
.fh_len
);
2393 if (flags
& ISLASTCN
) {
2394 switch (cnp
->cn_nameiop
) {
2396 cnp
->cn_flags
&= ~MAKEENTRY
;
2399 cnp
->cn_flags
&= ~MAKEENTRY
;
2409 newvp
= vnode_getparent(dvp
);
2415 error
= vnode_get(dvp
);
2419 nfs_node_lock_force(dnp
);
2420 if (fh
.fh_len
&& (dnp
->n_xid
<= xid
))
2421 nfs_loadattrcache(dnp
, &nvattr
, &xid
, 0);
2422 nfs_node_unlock(dnp
);
2424 ngflags
= (cnp
->cn_flags
& MAKEENTRY
) ? NG_MAKEENTRY
: 0;
2425 error
= nfs_nget(mp
, dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, ngflags
, &np
);
2429 nfs_node_unlock(np
);
2435 if (((cnp
->cn_nameiop
== CREATE
) || (cnp
->cn_nameiop
== RENAME
)) &&
2436 (flags
& ISLASTCN
) && (error
== ENOENT
)) {
2437 if (vnode_mount(dvp
) && vnode_vfsisrdonly(dvp
))
2440 error
= EJUSTRETURN
;
2443 if ((error
== ENOENT
) && (cnp
->cn_flags
& MAKEENTRY
) &&
2444 (cnp
->cn_nameiop
!= CREATE
) && negnamecache
) {
2445 /* add a negative entry in the name cache */
2446 nfs_node_lock_force(dnp
);
2447 cache_enter(dvp
, NULL
, cnp
);
2448 dnp
->n_flag
|= NNEGNCENTRIES
;
2449 nfs_node_unlock(dnp
);
2452 NVATTR_CLEANUP(&nvattr
);
2454 nfs_node_clear_busy(dnp
);
2455 if (error
&& *vpp
) {
2462 int nfs_readlink_nocache
= DEFAULT_READLINK_NOCACHE
;
2469 struct vnop_readlink_args
/* {
2470 struct vnodeop_desc *a_desc;
2473 vfs_context_t a_context;
2476 vfs_context_t ctx
= ap
->a_context
;
2477 nfsnode_t np
= VTONFS(ap
->a_vp
);
2478 struct nfsmount
*nmp
;
2479 int error
= 0, nfsvers
;
2481 uio_t uio
= ap
->a_uio
;
2482 struct nfsbuf
*bp
= NULL
;
2486 if (vnode_vtype(ap
->a_vp
) != VLNK
)
2489 if (uio_resid(uio
) == 0)
2491 if (uio_offset(uio
) < 0)
2494 nmp
= VTONMP(ap
->a_vp
);
2495 if (nfs_mount_gone(nmp
))
2497 nfsvers
= nmp
->nm_vers
;
2500 /* nfs_getattr() will check changed and purge caches */
2501 if ((error
= nfs_getattr(np
, NULL
, ctx
, nfs_readlink_nocache
? NGA_UNCACHED
: NGA_CACHED
))) {
2502 FSDBG(531, np
, 0xd1e0001, 0, error
);
2506 if (nfs_readlink_nocache
) {
2507 timeo
= nfs_attrcachetimeout(np
);
2512 OSAddAtomic64(1, &nfsstats
.biocache_readlinks
);
2513 error
= nfs_buf_get(np
, 0, NFS_MAXPATHLEN
, vfs_context_thread(ctx
), NBLK_META
, &bp
);
2515 FSDBG(531, np
, 0xd1e0002, 0, error
);
2519 if (nfs_readlink_nocache
) {
2520 NFS_VNOP_DBG("timeo = %d ts.tv_sec = %ld need refresh = %d cached = %d\n", timeo
, ts
.tv_sec
,
2521 (np
->n_rltim
.tv_sec
+ timeo
) < ts
.tv_sec
|| nfs_readlink_nocache
> 1,
2522 ISSET(bp
->nb_flags
, NB_CACHE
) == NB_CACHE
);
2523 /* n_rltim is synchronized by the associated nfs buf */
2524 if (ISSET(bp
->nb_flags
, NB_CACHE
) && ((nfs_readlink_nocache
> 1) || ((np
->n_rltim
.tv_sec
+ timeo
) < ts
.tv_sec
))) {
2525 SET(bp
->nb_flags
, NB_INVAL
);
2526 nfs_buf_release(bp
, 0);
2530 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) {
2532 OSAddAtomic64(1, &nfsstats
.readlink_bios
);
2533 buflen
= bp
->nb_bufsize
;
2534 error
= nmp
->nm_funcs
->nf_readlink_rpc(np
, bp
->nb_data
, &buflen
, ctx
);
2536 if (error
== ESTALE
) {
2537 NFS_VNOP_DBG("Stale FH from readlink rpc\n");
2538 error
= nfs_refresh_fh(np
, ctx
);
2542 SET(bp
->nb_flags
, NB_ERROR
);
2543 bp
->nb_error
= error
;
2544 NFS_VNOP_DBG("readlink failed %d\n", error
);
2546 bp
->nb_validoff
= 0;
2547 bp
->nb_validend
= buflen
;
2549 NFS_VNOP_DBG("readlink of %.*s\n", bp
->nb_validend
, (char *)bp
->nb_data
);
2552 NFS_VNOP_DBG("got cached link of %.*s\n", bp
->nb_validend
, (char *)bp
->nb_data
);
2555 if (!error
&& (bp
->nb_validend
> 0))
2556 error
= uiomove(bp
->nb_data
, bp
->nb_validend
, uio
);
2557 FSDBG(531, np
, bp
->nb_validend
, 0, error
);
2558 nfs_buf_release(bp
, 1);
2563 * Do a readlink RPC.
2566 nfs3_readlink_rpc(nfsnode_t np
, char *buf
, uint32_t *buflenp
, vfs_context_t ctx
)
2568 struct nfsmount
*nmp
;
2569 int error
= 0, lockerror
= ENOENT
, nfsvers
, status
;
2572 struct nfsm_chain nmreq
, nmrep
;
2575 if (nfs_mount_gone(nmp
))
2577 nfsvers
= nmp
->nm_vers
;
2578 nfsm_chain_null(&nmreq
);
2579 nfsm_chain_null(&nmrep
);
2581 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nfsvers
));
2582 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
2583 nfsm_chain_build_done(error
, &nmreq
);
2585 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_READLINK
, ctx
, NULL
, &nmrep
, &xid
, &status
);
2586 if ((lockerror
= nfs_node_lock(np
)))
2588 if (nfsvers
== NFS_VER3
)
2589 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
2592 nfsm_chain_get_32(error
, &nmrep
, len
);
2594 if ((nfsvers
== NFS_VER2
) && (len
> *buflenp
)) {
2598 if (len
>= *buflenp
) {
2599 if (np
->n_size
&& (np
->n_size
< *buflenp
))
2604 nfsm_chain_get_opaque(error
, &nmrep
, len
, buf
);
2609 nfs_node_unlock(np
);
2610 nfsm_chain_cleanup(&nmreq
);
2611 nfsm_chain_cleanup(&nmrep
);
2620 nfs_read_rpc(nfsnode_t np
, uio_t uio
, vfs_context_t ctx
)
2622 struct nfsmount
*nmp
;
2623 int error
= 0, nfsvers
, eof
= 0;
2624 size_t nmrsize
, len
, retlen
;
2627 struct nfsreq rq
, *req
= &rq
;
2628 uint32_t stategenid
= 0, restart
= 0;
2630 FSDBG_TOP(536, np
, uio_offset(uio
), uio_resid(uio
), 0);
2632 if (nfs_mount_gone(nmp
))
2634 nfsvers
= nmp
->nm_vers
;
2635 nmrsize
= nmp
->nm_rsize
;
2637 txoffset
= uio_offset(uio
);
2638 tsiz
= uio_resid(uio
);
2639 if ((nfsvers
== NFS_VER2
) && ((uint64_t)(txoffset
+ tsiz
) > 0xffffffffULL
)) {
2640 FSDBG_BOT(536, np
, uio_offset(uio
), uio_resid(uio
), EFBIG
);
2645 len
= retlen
= (tsiz
> (user_ssize_t
)nmrsize
) ? nmrsize
: (size_t)tsiz
;
2646 FSDBG(536, np
, txoffset
, len
, 0);
2647 if (np
->n_flag
& NREVOKE
) {
2651 if (nmp
->nm_vers
>= NFS_VER4
)
2652 stategenid
= nmp
->nm_stategenid
;
2653 error
= nmp
->nm_funcs
->nf_read_rpc_async(np
, txoffset
, len
,
2654 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, &req
);
2656 error
= nmp
->nm_funcs
->nf_read_rpc_async_finish(np
, req
, uio
, &retlen
, &eof
);
2657 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
) &&
2658 (++restart
<= nfs_mount_state_max_restarts(nmp
))) { /* guard against no progress */
2659 lck_mtx_lock(&nmp
->nm_lock
);
2660 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
2661 NP(np
, "nfs_read_rpc: error %d, initiating recovery", error
);
2662 nfs_need_recover(nmp
, error
);
2664 lck_mtx_unlock(&nmp
->nm_lock
);
2665 if (np
->n_flag
& NREVOKE
) {
2668 if (error
== NFSERR_GRACE
)
2669 tsleep(&nmp
->nm_state
, (PZERO
-1), "nfsgrace", 2*hz
);
2670 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
)))
2678 if (nfsvers
!= NFS_VER2
) {
2679 if (eof
|| (retlen
== 0))
2681 } else if (retlen
< len
)
2685 FSDBG_BOT(536, np
, eof
, uio_resid(uio
), error
);
2690 nfs3_read_rpc_async(
2696 struct nfsreq_cbinfo
*cb
,
2697 struct nfsreq
**reqp
)
2699 struct nfsmount
*nmp
;
2700 int error
= 0, nfsvers
;
2701 struct nfsm_chain nmreq
;
2704 if (nfs_mount_gone(nmp
))
2706 nfsvers
= nmp
->nm_vers
;
2708 nfsm_chain_null(&nmreq
);
2709 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(nfsvers
) + 3 * NFSX_UNSIGNED
);
2710 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
2711 if (nfsvers
== NFS_VER3
) {
2712 nfsm_chain_add_64(error
, &nmreq
, offset
);
2713 nfsm_chain_add_32(error
, &nmreq
, len
);
2715 nfsm_chain_add_32(error
, &nmreq
, offset
);
2716 nfsm_chain_add_32(error
, &nmreq
, len
);
2717 nfsm_chain_add_32(error
, &nmreq
, 0);
2719 nfsm_chain_build_done(error
, &nmreq
);
2721 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC_READ
, thd
, cred
, NULL
, 0, cb
, reqp
);
2723 nfsm_chain_cleanup(&nmreq
);
2728 nfs3_read_rpc_async_finish(
2735 int error
= 0, lockerror
, nfsvers
, status
, eof
= 0;
2738 struct nfsmount
*nmp
;
2739 struct nfsm_chain nmrep
;
2742 if (nfs_mount_gone(nmp
)) {
2743 nfs_request_async_cancel(req
);
2746 nfsvers
= nmp
->nm_vers
;
2748 nfsm_chain_null(&nmrep
);
2750 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
2751 if (error
== EINPROGRESS
) /* async request restarted */
2754 if ((lockerror
= nfs_node_lock(np
)))
2756 if (nfsvers
== NFS_VER3
)
2757 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
2760 if (nfsvers
== NFS_VER3
) {
2761 nfsm_chain_adv(error
, &nmrep
, NFSX_UNSIGNED
);
2762 nfsm_chain_get_32(error
, &nmrep
, eof
);
2764 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
2767 nfs_node_unlock(np
);
2768 nfsm_chain_get_32(error
, &nmrep
, retlen
);
2769 if ((nfsvers
== NFS_VER2
) && (retlen
> *lenp
))
2772 error
= nfsm_chain_get_uio(&nmrep
, MIN(retlen
, *lenp
), uio
);
2774 if (nfsvers
== NFS_VER3
) {
2775 if (!eof
&& !retlen
)
2777 } else if (retlen
< *lenp
) {
2782 *lenp
= MIN(retlen
, *lenp
);
2784 nfsm_chain_cleanup(&nmrep
);
2793 struct vnop_write_args
/* {
2794 struct vnodeop_desc *a_desc;
2798 vfs_context_t a_context;
2801 vfs_context_t ctx
= ap
->a_context
;
2802 uio_t uio
= ap
->a_uio
;
2803 vnode_t vp
= ap
->a_vp
;
2804 nfsnode_t np
= VTONFS(vp
);
2805 int ioflag
= ap
->a_ioflag
;
2807 struct nfsmount
*nmp
= VTONMP(vp
);
2810 int n
, on
, error
= 0;
2811 off_t boff
, start
, end
;
2813 char auio_buf
[ UIO_SIZEOF(1) ];
2817 FSDBG_TOP(515, np
, uio_offset(uio
), uio_resid(uio
), ioflag
);
2819 if (vnode_vtype(vp
) != VREG
) {
2820 FSDBG_BOT(515, np
, uio_offset(uio
), uio_resid(uio
), EIO
);
2824 thd
= vfs_context_thread(ctx
);
2825 cred
= vfs_context_ucred(ctx
);
2827 nfs_data_lock(np
, NFS_DATA_LOCK_SHARED
);
2829 if ((error
= nfs_node_lock(np
))) {
2830 nfs_data_unlock(np
);
2831 FSDBG_BOT(515, np
, uio_offset(uio
), uio_resid(uio
), error
);
2836 if (np
->n_flag
& NWRITEERR
) {
2837 error
= np
->n_error
;
2838 np
->n_flag
&= ~NWRITEERR
;
2840 if (np
->n_flag
& NNEEDINVALIDATE
) {
2841 np
->n_flag
&= ~NNEEDINVALIDATE
;
2842 nfs_node_unlock(np
);
2843 nfs_data_unlock(np
);
2844 nfs_vinvalbuf(vp
, V_SAVE
|V_IGNORE_WRITEERR
, ctx
, 1);
2845 nfs_data_lock(np
, NFS_DATA_LOCK_SHARED
);
2847 nfs_node_unlock(np
);
2852 biosize
= nmp
->nm_biosize
;
2854 if (ioflag
& (IO_APPEND
| IO_SYNC
)) {
2855 nfs_node_lock_force(np
);
2856 if (np
->n_flag
& NMODIFIED
) {
2857 NATTRINVALIDATE(np
);
2858 nfs_node_unlock(np
);
2859 nfs_data_unlock(np
);
2860 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
2861 nfs_data_lock(np
, NFS_DATA_LOCK_SHARED
);
2863 FSDBG(515, np
, uio_offset(uio
), 0x10bad01, error
);
2867 nfs_node_unlock(np
);
2869 if (ioflag
& IO_APPEND
) {
2870 nfs_data_unlock(np
);
2871 /* nfs_getattr() will check changed and purge caches */
2872 error
= nfs_getattr(np
, NULL
, ctx
, NGA_UNCACHED
);
2873 /* we'll be extending the file, so take the data lock exclusive */
2874 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
2876 FSDBG(515, np
, uio_offset(uio
), 0x10bad02, error
);
2879 uio_setoffset(uio
, np
->n_size
);
2882 if (uio_offset(uio
) < 0) {
2884 FSDBG_BOT(515, np
, uio_offset(uio
), 0xbad0ff, error
);
2887 if (uio_resid(uio
) == 0)
2890 if (((uio_offset(uio
) + uio_resid(uio
)) > (off_t
)np
->n_size
) && !(ioflag
& IO_APPEND
)) {
2892 * It looks like we'll be extending the file, so take the data lock exclusive.
2894 nfs_data_unlock(np
);
2895 nfs_data_lock(np
, NFS_DATA_LOCK_EXCLUSIVE
);
2898 * Also, if the write begins after the previous EOF buffer, make sure to zero
2899 * and validate the new bytes in that buffer.
2901 struct nfsbuf
*eofbp
= NULL
;
2902 daddr64_t eofbn
= np
->n_size
/ biosize
;
2903 int eofoff
= np
->n_size
% biosize
;
2904 lbn
= uio_offset(uio
) / biosize
;
2906 if (eofoff
&& (eofbn
< lbn
)) {
2907 if ((error
= nfs_buf_get(np
, eofbn
, biosize
, thd
, NBLK_WRITE
|NBLK_ONLYVALID
, &eofbp
)))
2909 np
->n_size
+= (biosize
- eofoff
);
2910 nfs_node_lock_force(np
);
2911 CLR(np
->n_flag
, NUPDATESIZE
);
2912 np
->n_flag
|= NMODIFIED
;
2913 nfs_node_unlock(np
);
2914 FSDBG(516, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0001);
2915 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX errors */
2918 * For the old last page, don't zero bytes if there
2919 * are invalid bytes in that page (i.e. the page isn't
2921 * For pages after the old last page, zero them and
2922 * mark them as valid.
2926 if (ioflag
& IO_NOCACHE
)
2927 SET(eofbp
->nb_flags
, NB_NOCACHE
);
2929 FSDBG(516, eofbp
, eofoff
, biosize
- eofoff
, 0xe0fff01e);
2931 i
= eofoff
/PAGE_SIZE
;
2932 while (eofoff
< biosize
) {
2933 int poff
= eofoff
& PAGE_MASK
;
2934 if (!poff
|| NBPGVALID(eofbp
,i
)) {
2935 bzero(d
+ eofoff
, PAGE_SIZE
- poff
);
2936 NBPGVALID_SET(eofbp
, i
);
2938 eofoff
+= PAGE_SIZE
- poff
;
2941 nfs_buf_release(eofbp
, 1);
2947 OSAddAtomic64(1, &nfsstats
.biocache_writes
);
2948 lbn
= uio_offset(uio
) / biosize
;
2949 on
= uio_offset(uio
) % biosize
;
2951 if (uio_resid(uio
) < n
)
2955 * Get a cache block for writing. The range to be written is
2956 * (off..off+n) within the block. We ensure that the block
2957 * either has no dirty region or that the given range is
2958 * contiguous with the existing dirty region.
2960 error
= nfs_buf_get(np
, lbn
, biosize
, thd
, NBLK_WRITE
, &bp
);
2963 /* map the block because we know we're going to write to it */
2966 if (ioflag
& IO_NOCACHE
)
2967 SET(bp
->nb_flags
, NB_NOCACHE
);
2969 if (!IS_VALID_CRED(bp
->nb_wcred
)) {
2970 kauth_cred_ref(cred
);
2971 bp
->nb_wcred
= cred
;
2975 * If there's already a dirty range AND dirty pages in this block we
2976 * need to send a commit AND write the dirty pages before continuing.
2978 * If there's already a dirty range OR dirty pages in this block
2979 * and the new write range is not contiguous with the existing range,
2980 * then force the buffer to be written out now.
2981 * (We used to just extend the dirty range to cover the valid,
2982 * but unwritten, data in between also. But writing ranges
2983 * of data that weren't actually written by an application
2984 * risks overwriting some other client's data with stale data
2985 * that's just masquerading as new written data.)
2987 if (bp
->nb_dirtyend
> 0) {
2988 if (on
> bp
->nb_dirtyend
|| (on
+ n
) < bp
->nb_dirtyoff
|| bp
->nb_dirty
) {
2989 FSDBG(515, np
, uio_offset(uio
), bp
, 0xd15c001);
2990 /* write/commit buffer "synchronously" */
2991 /* (NB_STABLE indicates that data writes should be FILESYNC) */
2992 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
));
2993 SET(bp
->nb_flags
, (NB_ASYNC
| NB_STABLE
));
2994 error
= nfs_buf_write(bp
);
2999 } else if (bp
->nb_dirty
) {
3000 int firstpg
, lastpg
;
3002 /* calculate write range pagemask */
3003 firstpg
= on
/PAGE_SIZE
;
3004 lastpg
= (on
+n
-1)/PAGE_SIZE
;
3005 pagemask
= ((1 << (lastpg
+1)) - 1) & ~((1 << firstpg
) - 1);
3006 /* check if there are dirty pages outside the write range */
3007 if (bp
->nb_dirty
& ~pagemask
) {
3008 FSDBG(515, np
, uio_offset(uio
), bp
, 0xd15c002);
3009 /* write/commit buffer "synchronously" */
3010 /* (NB_STABLE indicates that data writes should be FILESYNC) */
3011 CLR(bp
->nb_flags
, (NB_DONE
| NB_ERROR
| NB_INVAL
));
3012 SET(bp
->nb_flags
, (NB_ASYNC
| NB_STABLE
));
3013 error
= nfs_buf_write(bp
);
3018 /* if the first or last pages are already dirty */
3019 /* make sure that the dirty range encompasses those pages */
3020 if (NBPGDIRTY(bp
,firstpg
) || NBPGDIRTY(bp
,lastpg
)) {
3021 FSDBG(515, np
, uio_offset(uio
), bp
, 0xd15c003);
3022 bp
->nb_dirtyoff
= min(on
, firstpg
* PAGE_SIZE
);
3023 if (NBPGDIRTY(bp
,lastpg
)) {
3024 bp
->nb_dirtyend
= (lastpg
+1) * PAGE_SIZE
;
3026 if (NBOFF(bp
) + bp
->nb_dirtyend
> (off_t
)np
->n_size
) {
3027 bp
->nb_dirtyend
= np
->n_size
- NBOFF(bp
);
3028 if (bp
->nb_dirtyoff
>= bp
->nb_dirtyend
)
3029 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
3032 bp
->nb_dirtyend
= on
+n
;
3037 * Are we extending the size of the file with this write?
3038 * If so, update file size now that we have the block.
3039 * If there was a partial buf at the old eof, validate
3040 * and zero the new bytes.
3042 if ((uio_offset(uio
) + n
) > (off_t
)np
->n_size
) {
3043 daddr64_t eofbn
= np
->n_size
/ biosize
;
3044 int neweofoff
= (uio_offset(uio
) + n
) % biosize
;
3046 FSDBG(515, 0xb1ffa000, uio_offset(uio
) + n
, eofoff
, neweofoff
);
3048 /* if we're extending within the same last block */
3049 /* and the block is flagged as being cached... */
3050 if ((lbn
== eofbn
) && ISSET(bp
->nb_flags
, NB_CACHE
)) {
3051 /* ...check that all pages in buffer are valid */
3052 int endpg
= ((neweofoff
? neweofoff
: biosize
) - 1)/PAGE_SIZE
;
3054 /* pagemask only has to extend to last page being written to */
3055 pagemask
= (1 << (endpg
+1)) - 1;
3056 FSDBG(515, 0xb1ffa001, bp
->nb_valid
, pagemask
, 0);
3057 if ((bp
->nb_valid
& pagemask
) != pagemask
) {
3058 /* zerofill any hole */
3059 if (on
> bp
->nb_validend
) {
3061 for (i
=bp
->nb_validend
/PAGE_SIZE
; i
<= (on
- 1)/PAGE_SIZE
; i
++)
3062 NBPGVALID_SET(bp
, i
);
3064 FSDBG(516, bp
, bp
->nb_validend
, on
- bp
->nb_validend
, 0xf01e);
3065 bzero((char *)bp
->nb_data
+ bp
->nb_validend
,
3066 on
- bp
->nb_validend
);
3068 /* zerofill any trailing data in the last page */
3071 FSDBG(516, bp
, neweofoff
, PAGE_SIZE
- (neweofoff
& PAGE_MASK
), 0xe0f);
3072 bzero((char *)bp
->nb_data
+ neweofoff
,
3073 PAGE_SIZE
- (neweofoff
& PAGE_MASK
));
3077 np
->n_size
= uio_offset(uio
) + n
;
3078 nfs_node_lock_force(np
);
3079 CLR(np
->n_flag
, NUPDATESIZE
);
3080 np
->n_flag
|= NMODIFIED
;
3081 nfs_node_unlock(np
);
3082 FSDBG(516, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0001);
3083 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX errors */
3086 * If dirtyend exceeds file size, chop it down. This should
3087 * not occur unless there is a race.
3089 if (NBOFF(bp
) + bp
->nb_dirtyend
> (off_t
)np
->n_size
) {
3090 bp
->nb_dirtyend
= np
->n_size
- NBOFF(bp
);
3091 if (bp
->nb_dirtyoff
>= bp
->nb_dirtyend
)
3092 bp
->nb_dirtyoff
= bp
->nb_dirtyend
= 0;
3095 * UBC doesn't handle partial pages, so we need to make sure
3096 * that any pages left in the page cache are completely valid.
3098 * Writes that are smaller than a block are delayed if they
3099 * don't extend to the end of the block.
3101 * If the block isn't (completely) cached, we may need to read
3102 * in some parts of pages that aren't covered by the write.
3103 * If the write offset (on) isn't page aligned, we'll need to
3104 * read the start of the first page being written to. Likewise,
3105 * if the offset of the end of the write (on+n) isn't page aligned,
3106 * we'll need to read the end of the last page being written to.
3109 * We don't want to read anything we're just going to write over.
3110 * We don't want to read anything we're just going drop when the
3111 * I/O is complete (i.e. don't do reads for NOCACHE requests).
3112 * We don't want to issue multiple I/Os if we don't have to
3113 * (because they're synchronous rpcs).
3114 * We don't want to read anything we already have modified in the
3117 if (!ISSET(bp
->nb_flags
, NB_CACHE
) && (n
< biosize
)) {
3118 int firstpg
, lastpg
, dirtypg
;
3119 int firstpgoff
, lastpgoff
;
3121 firstpg
= on
/PAGE_SIZE
;
3122 firstpgoff
= on
& PAGE_MASK
;
3123 lastpg
= (on
+n
-1)/PAGE_SIZE
;
3124 lastpgoff
= (on
+n
) & PAGE_MASK
;
3125 if (firstpgoff
&& !NBPGVALID(bp
,firstpg
)) {
3126 /* need to read start of first page */
3127 start
= firstpg
* PAGE_SIZE
;
3128 end
= start
+ firstpgoff
;
3130 if (lastpgoff
&& !NBPGVALID(bp
,lastpg
)) {
3131 /* need to read end of last page */
3133 start
= (lastpg
* PAGE_SIZE
) + lastpgoff
;
3134 end
= (lastpg
+ 1) * PAGE_SIZE
;
3136 if (ISSET(bp
->nb_flags
, NB_NOCACHE
)) {
3138 * For nocache writes, if there is any partial page at the
3139 * start or end of the write range, then we do the write
3140 * synchronously to make sure that we can drop the data
3141 * from the cache as soon as the WRITE finishes. Normally,
3142 * we would do an unstable write and not drop the data until
3143 * it was committed. But doing that here would risk allowing
3144 * invalid data to be read from the cache between the WRITE
3146 * (NB_STABLE indicates that data writes should be FILESYNC)
3149 SET(bp
->nb_flags
, NB_STABLE
);
3153 /* need to read the data in range: start...end-1 */
3155 /* first, check for dirty pages in between */
3156 /* if there are, we'll have to do two reads because */
3157 /* we don't want to overwrite the dirty pages. */
3158 for (dirtypg
=start
/PAGE_SIZE
; dirtypg
<= (end
-1)/PAGE_SIZE
; dirtypg
++)
3159 if (NBPGDIRTY(bp
,dirtypg
))
3162 /* if start is at beginning of page, try */
3163 /* to get any preceeding pages as well. */
3164 if (!(start
& PAGE_MASK
)) {
3165 /* stop at next dirty/valid page or start of block */
3166 for (; start
> 0; start
-=PAGE_SIZE
)
3167 if (NBPGVALID(bp
,((start
-1)/PAGE_SIZE
)))
3172 /* setup uio for read(s) */
3174 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
3175 &auio_buf
, sizeof(auio_buf
));
3177 if (dirtypg
<= (end
-1)/PAGE_SIZE
) {
3178 /* there's a dirty page in the way, so just do two reads */
3179 /* we'll read the preceding data here */
3180 uio_reset(auio
, boff
+ start
, UIO_SYSSPACE
, UIO_READ
);
3181 uio_addiov(auio
, CAST_USER_ADDR_T(bp
->nb_data
+ start
), on
- start
);
3182 error
= nfs_read_rpc(np
, auio
, ctx
);
3184 /* couldn't read the data, so treat buffer as synchronous NOCACHE */
3185 SET(bp
->nb_flags
, (NB_NOCACHE
|NB_STABLE
));
3188 if (uio_resid(auio
) > 0) {
3189 FSDBG(516, bp
, (caddr_t
)uio_curriovbase(auio
) - bp
->nb_data
, uio_resid(auio
), 0xd00dee01);
3190 bzero(CAST_DOWN(caddr_t
, uio_curriovbase(auio
)), uio_resid(auio
));
3193 /* update validoff/validend if necessary */
3194 if ((bp
->nb_validoff
< 0) || (bp
->nb_validoff
> start
))
3195 bp
->nb_validoff
= start
;
3196 if ((bp
->nb_validend
< 0) || (bp
->nb_validend
< on
))
3197 bp
->nb_validend
= on
;
3198 if ((off_t
)np
->n_size
> boff
+ bp
->nb_validend
)
3199 bp
->nb_validend
= min(np
->n_size
- (boff
+ start
), biosize
);
3200 /* validate any pages before the write offset */
3201 for (; start
< on
/PAGE_SIZE
; start
+=PAGE_SIZE
)
3202 NBPGVALID_SET(bp
, start
/PAGE_SIZE
);
3204 /* adjust start to read any trailing data */
3208 /* if end is at end of page, try to */
3209 /* get any following pages as well. */
3210 if (!(end
& PAGE_MASK
)) {
3211 /* stop at next valid page or end of block */
3212 for (; end
< biosize
; end
+=PAGE_SIZE
)
3213 if (NBPGVALID(bp
,end
/PAGE_SIZE
))
3217 if (((boff
+start
) >= (off_t
)np
->n_size
) ||
3218 ((start
>= on
) && ((boff
+ on
+ n
) >= (off_t
)np
->n_size
))) {
3220 * Either this entire read is beyond the current EOF
3221 * or the range that we won't be modifying (on+n...end)
3222 * is all beyond the current EOF.
3223 * No need to make a trip across the network to
3224 * read nothing. So, just zero the buffer instead.
3226 FSDBG(516, bp
, start
, end
- start
, 0xd00dee00);
3227 bzero(bp
->nb_data
+ start
, end
- start
);
3230 /* now we'll read the (rest of the) data */
3231 uio_reset(auio
, boff
+ start
, UIO_SYSSPACE
, UIO_READ
);
3232 uio_addiov(auio
, CAST_USER_ADDR_T(bp
->nb_data
+ start
), end
- start
);
3233 error
= nfs_read_rpc(np
, auio
, ctx
);
3235 /* couldn't read the data, so treat buffer as synchronous NOCACHE */
3236 SET(bp
->nb_flags
, (NB_NOCACHE
|NB_STABLE
));
3239 if (uio_resid(auio
) > 0) {
3240 FSDBG(516, bp
, (caddr_t
)uio_curriovbase(auio
) - bp
->nb_data
, uio_resid(auio
), 0xd00dee02);
3241 bzero(CAST_DOWN(caddr_t
, uio_curriovbase(auio
)), uio_resid(auio
));
3245 /* update validoff/validend if necessary */
3246 if ((bp
->nb_validoff
< 0) || (bp
->nb_validoff
> start
))
3247 bp
->nb_validoff
= start
;
3248 if ((bp
->nb_validend
< 0) || (bp
->nb_validend
< end
))
3249 bp
->nb_validend
= end
;
3250 if ((off_t
)np
->n_size
> boff
+ bp
->nb_validend
)
3251 bp
->nb_validend
= min(np
->n_size
- (boff
+ start
), biosize
);
3252 /* validate any pages before the write offset's page */
3253 for (; start
< (off_t
)trunc_page_32(on
); start
+=PAGE_SIZE
)
3254 NBPGVALID_SET(bp
, start
/PAGE_SIZE
);
3255 /* validate any pages after the range of pages being written to */
3256 for (; (end
- 1) > (off_t
)round_page_32(on
+n
-1); end
-=PAGE_SIZE
)
3257 NBPGVALID_SET(bp
, (end
-1)/PAGE_SIZE
);
3259 /* Note: pages being written to will be validated when written */
3264 if (ISSET(bp
->nb_flags
, NB_ERROR
)) {
3265 error
= bp
->nb_error
;
3266 nfs_buf_release(bp
, 1);
3270 nfs_node_lock_force(np
);
3271 np
->n_flag
|= NMODIFIED
;
3272 nfs_node_unlock(np
);
3275 error
= uiomove((char *)bp
->nb_data
+ on
, n
, uio
);
3277 SET(bp
->nb_flags
, NB_ERROR
);
3278 nfs_buf_release(bp
, 1);
3282 /* validate any pages written to */
3283 start
= on
& ~PAGE_MASK
;
3284 for (; start
< on
+n
; start
+= PAGE_SIZE
) {
3285 NBPGVALID_SET(bp
, start
/PAGE_SIZE
);
3287 * This may seem a little weird, but we don't actually set the
3288 * dirty bits for writes. This is because we keep the dirty range
3289 * in the nb_dirtyoff/nb_dirtyend fields. Also, particularly for
3290 * delayed writes, when we give the pages back to the VM we don't
3291 * want to keep them marked dirty, because when we later write the
3292 * buffer we won't be able to tell which pages were written dirty
3293 * and which pages were mmapped and dirtied.
3296 if (bp
->nb_dirtyend
> 0) {
3297 bp
->nb_dirtyoff
= min(on
, bp
->nb_dirtyoff
);
3298 bp
->nb_dirtyend
= max((on
+ n
), bp
->nb_dirtyend
);
3300 bp
->nb_dirtyoff
= on
;
3301 bp
->nb_dirtyend
= on
+ n
;
3303 if (bp
->nb_validend
<= 0 || bp
->nb_validend
< bp
->nb_dirtyoff
||
3304 bp
->nb_validoff
> bp
->nb_dirtyend
) {
3305 bp
->nb_validoff
= bp
->nb_dirtyoff
;
3306 bp
->nb_validend
= bp
->nb_dirtyend
;
3308 bp
->nb_validoff
= min(bp
->nb_validoff
, bp
->nb_dirtyoff
);
3309 bp
->nb_validend
= max(bp
->nb_validend
, bp
->nb_dirtyend
);
3311 if (!ISSET(bp
->nb_flags
, NB_CACHE
))
3312 nfs_buf_normalize_valid_range(np
, bp
);
3315 * Since this block is being modified, it must be written
3316 * again and not just committed.
3318 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
3319 nfs_node_lock_force(np
);
3320 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
3321 np
->n_needcommitcnt
--;
3322 CHECK_NEEDCOMMITCNT(np
);
3324 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
3325 nfs_node_unlock(np
);
3328 if (ioflag
& IO_SYNC
) {
3329 error
= nfs_buf_write(bp
);
3332 } else if (((n
+ on
) == biosize
) || (ioflag
& IO_APPEND
) ||
3333 (ioflag
& IO_NOCACHE
) || ISSET(bp
->nb_flags
, NB_NOCACHE
)) {
3334 SET(bp
->nb_flags
, NB_ASYNC
);
3335 error
= nfs_buf_write(bp
);
3339 /* If the block wasn't already delayed: charge for the write */
3340 if (!ISSET(bp
->nb_flags
, NB_DELWRI
)) {
3341 proc_t p
= vfs_context_proc(ctx
);
3342 if (p
&& p
->p_stats
)
3343 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_oublock
);
3345 nfs_buf_write_delayed(bp
);
3347 if (np
->n_needcommitcnt
>= NFS_A_LOT_OF_NEEDCOMMITS
)
3348 nfs_flushcommits(np
, 1);
3350 } while (uio_resid(uio
) > 0 && n
> 0);
3353 nfs_node_lock_force(np
);
3355 nfs_node_unlock(np
);
3356 nfs_data_unlock(np
);
3357 FSDBG_BOT(515, np
, uio_offset(uio
), uio_resid(uio
), error
);
3373 return nfs_write_rpc2(np
, uio
, vfs_context_thread(ctx
), vfs_context_ucred(ctx
), iomodep
, wverfp
);
3385 struct nfsmount
*nmp
;
3386 int error
= 0, nfsvers
;
3387 int wverfset
, commit
, committed
;
3388 uint64_t wverf
= 0, wverf2
;
3389 size_t nmwsize
, totalsize
, tsiz
, len
, rlen
;
3390 struct nfsreq rq
, *req
= &rq
;
3391 uint32_t stategenid
= 0, vrestart
= 0, restart
= 0;
3392 uio_t uio_save
= NULL
;
3395 /* XXX limitation based on need to back up uio on short write */
3396 if (uio_iovcnt(uio
) != 1)
3397 panic("nfs3_write_rpc: iovcnt > 1");
3399 FSDBG_TOP(537, np
, uio_offset(uio
), uio_resid(uio
), *iomodep
);
3401 if (nfs_mount_gone(nmp
))
3403 nfsvers
= nmp
->nm_vers
;
3404 nmwsize
= nmp
->nm_wsize
;
3407 committed
= NFS_WRITE_FILESYNC
;
3409 totalsize
= tsiz
= uio_resid(uio
);
3410 if ((nfsvers
== NFS_VER2
) && ((uint64_t)(uio_offset(uio
) + tsiz
) > 0xffffffffULL
)) {
3411 FSDBG_BOT(537, np
, uio_offset(uio
), uio_resid(uio
), EFBIG
);
3415 uio_save
= uio_duplicate(uio
);
3416 if (uio_save
== NULL
) {
3421 len
= (tsiz
> nmwsize
) ? nmwsize
: tsiz
;
3422 FSDBG(537, np
, uio_offset(uio
), len
, 0);
3423 if (np
->n_flag
& NREVOKE
) {
3427 if (nmp
->nm_vers
>= NFS_VER4
)
3428 stategenid
= nmp
->nm_stategenid
;
3429 error
= nmp
->nm_funcs
->nf_write_rpc_async(np
, uio
, len
, thd
, cred
, *iomodep
, NULL
, &req
);
3431 error
= nmp
->nm_funcs
->nf_write_rpc_async_finish(np
, req
, &commit
, &rlen
, &wverf2
);
3433 if (nfs_mount_gone(nmp
))
3435 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
) &&
3436 (++restart
<= nfs_mount_state_max_restarts(nmp
))) { /* guard against no progress */
3437 lck_mtx_lock(&nmp
->nm_lock
);
3438 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
3439 NP(np
, "nfs_write_rpc: error %d, initiating recovery", error
);
3440 nfs_need_recover(nmp
, error
);
3442 lck_mtx_unlock(&nmp
->nm_lock
);
3443 if (np
->n_flag
& NREVOKE
) {
3446 if (error
== NFSERR_GRACE
)
3447 tsleep(&nmp
->nm_state
, (PZERO
-1), "nfsgrace", 2*hz
);
3448 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
)))
3454 if (nfsvers
== NFS_VER2
) {
3459 /* check for a short write */
3461 /* Reset the uio to reflect the actual transfer */
3463 uio_update(uio
, totalsize
- (tsiz
- rlen
));
3467 /* return lowest commit level returned */
3468 if (commit
< committed
)
3473 /* check write verifier */
3477 } else if (wverf
!= wverf2
) {
3478 /* verifier changed, so we need to restart all the writes */
3479 if (++vrestart
> 100) {
3480 /* give up after too many restarts */
3484 *uio
= *uio_save
; // Reset the uio back to the start
3485 committed
= NFS_WRITE_FILESYNC
;
3492 if (wverfset
&& wverfp
)
3494 *iomodep
= committed
;
3496 uio_setresid(uio
, tsiz
);
3497 FSDBG_BOT(537, np
, committed
, uio_resid(uio
), error
);
3502 nfs3_write_rpc_async(
3509 struct nfsreq_cbinfo
*cb
,
3510 struct nfsreq
**reqp
)
3512 struct nfsmount
*nmp
;
3514 int error
= 0, nfsvers
;
3515 struct nfsm_chain nmreq
;
3518 if (nfs_mount_gone(nmp
))
3520 nfsvers
= nmp
->nm_vers
;
3522 /* for async mounts, don't bother sending sync write requests */
3523 if ((iomode
!= NFS_WRITE_UNSTABLE
) && nfs_allow_async
&&
3524 ((mp
= NFSTOMP(np
))) && (vfs_flags(mp
) & MNT_ASYNC
))
3525 iomode
= NFS_WRITE_UNSTABLE
;
3527 nfsm_chain_null(&nmreq
);
3528 nfsm_chain_build_alloc_init(error
, &nmreq
,
3529 NFSX_FH(nfsvers
) + 5 * NFSX_UNSIGNED
+ nfsm_rndup(len
));
3530 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
3531 if (nfsvers
== NFS_VER3
) {
3532 nfsm_chain_add_64(error
, &nmreq
, uio_offset(uio
));
3533 nfsm_chain_add_32(error
, &nmreq
, len
);
3534 nfsm_chain_add_32(error
, &nmreq
, iomode
);
3536 nfsm_chain_add_32(error
, &nmreq
, 0);
3537 nfsm_chain_add_32(error
, &nmreq
, uio_offset(uio
));
3538 nfsm_chain_add_32(error
, &nmreq
, 0);
3540 nfsm_chain_add_32(error
, &nmreq
, len
);
3542 error
= nfsm_chain_add_uio(&nmreq
, uio
, len
);
3543 nfsm_chain_build_done(error
, &nmreq
);
3545 error
= nfs_request_async(np
, NULL
, &nmreq
, NFSPROC_WRITE
, thd
, cred
, NULL
, 0, cb
, reqp
);
3547 nfsm_chain_cleanup(&nmreq
);
3552 nfs3_write_rpc_async_finish(
3559 struct nfsmount
*nmp
;
3560 int error
= 0, lockerror
= ENOENT
, nfsvers
, status
;
3561 int updatemtime
= 0, wccpostattr
= 0, rlen
, committed
= NFS_WRITE_FILESYNC
;
3562 u_int64_t xid
, wverf
;
3564 struct nfsm_chain nmrep
;
3567 if (nfs_mount_gone(nmp
)) {
3568 nfs_request_async_cancel(req
);
3571 nfsvers
= nmp
->nm_vers
;
3573 nfsm_chain_null(&nmrep
);
3575 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
3576 if (error
== EINPROGRESS
) /* async request restarted */
3579 if (nfs_mount_gone(nmp
))
3581 if (!error
&& (lockerror
= nfs_node_lock(np
)))
3583 if (nfsvers
== NFS_VER3
) {
3584 struct timespec premtime
= { 0, 0 };
3585 nfsm_chain_get_wcc_data(error
, &nmrep
, np
, &premtime
, &wccpostattr
, &xid
);
3586 if (nfstimespeccmp(&np
->n_mtime
, &premtime
, ==))
3590 nfsm_chain_get_32(error
, &nmrep
, rlen
);
3595 nfsm_chain_get_32(error
, &nmrep
, committed
);
3596 nfsm_chain_get_64(error
, &nmrep
, wverf
);
3600 lck_mtx_lock(&nmp
->nm_lock
);
3601 if (!(nmp
->nm_state
& NFSSTA_HASWRITEVERF
)) {
3602 nmp
->nm_verf
= wverf
;
3603 nmp
->nm_state
|= NFSSTA_HASWRITEVERF
;
3604 } else if (nmp
->nm_verf
!= wverf
) {
3605 nmp
->nm_verf
= wverf
;
3607 lck_mtx_unlock(&nmp
->nm_lock
);
3611 nfsm_chain_loadattr(error
, &nmrep
, np
, nfsvers
, &xid
);
3615 NFS_CHANGED_UPDATE(nfsvers
, np
, &np
->n_vattr
);
3618 nfs_node_unlock(np
);
3619 nfsm_chain_cleanup(&nmrep
);
3620 if ((committed
!= NFS_WRITE_FILESYNC
) && nfs_allow_async
&&
3621 ((mp
= NFSTOMP(np
))) && (vfs_flags(mp
) & MNT_ASYNC
))
3622 committed
= NFS_WRITE_FILESYNC
;
3623 *iomodep
= committed
;
3628 * NFS mknod vnode op
3630 * For NFS v2 this is a kludge. Use a create RPC but with the IFMT bits of the
3631 * mode set to specify the file type and the size field for rdev.
3635 struct vnop_mknod_args
/* {
3636 struct vnodeop_desc *a_desc;
3639 struct componentname *a_cnp;
3640 struct vnode_attr *a_vap;
3641 vfs_context_t a_context;
3644 vnode_t dvp
= ap
->a_dvp
;
3645 vnode_t
*vpp
= ap
->a_vpp
;
3646 struct componentname
*cnp
= ap
->a_cnp
;
3647 struct vnode_attr
*vap
= ap
->a_vap
;
3648 vfs_context_t ctx
= ap
->a_context
;
3649 vnode_t newvp
= NULL
;
3650 nfsnode_t np
= NULL
;
3651 struct nfsmount
*nmp
;
3652 nfsnode_t dnp
= VTONFS(dvp
);
3653 struct nfs_vattr nvattr
;
3655 int error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0;
3656 struct timespec premtime
= { 0, 0 };
3658 u_int64_t xid
= 0, dxid
;
3659 int nfsvers
, gotuid
, gotgid
;
3660 struct nfsm_chain nmreq
, nmrep
;
3661 struct nfsreq rq
, *req
= &rq
;
3664 if (nfs_mount_gone(nmp
))
3666 nfsvers
= nmp
->nm_vers
;
3668 if (!VATTR_IS_ACTIVE(vap
, va_type
))
3670 if (vap
->va_type
== VCHR
|| vap
->va_type
== VBLK
) {
3671 if (!VATTR_IS_ACTIVE(vap
, va_rdev
))
3673 rdev
= vap
->va_rdev
;
3674 } else if (vap
->va_type
== VFIFO
|| vap
->va_type
== VSOCK
)
3679 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
))
3680 return (ENAMETOOLONG
);
3682 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
3684 VATTR_SET_SUPPORTED(vap
, va_mode
);
3685 VATTR_SET_SUPPORTED(vap
, va_uid
);
3686 VATTR_SET_SUPPORTED(vap
, va_gid
);
3687 VATTR_SET_SUPPORTED(vap
, va_data_size
);
3688 VATTR_SET_SUPPORTED(vap
, va_access_time
);
3689 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
3690 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
3691 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
3693 nfsm_chain_null(&nmreq
);
3694 nfsm_chain_null(&nmrep
);
3696 nfsm_chain_build_alloc_init(error
, &nmreq
,
3697 NFSX_FH(nfsvers
) + 4 * NFSX_UNSIGNED
+
3698 nfsm_rndup(cnp
->cn_namelen
) + NFSX_SATTR(nfsvers
));
3699 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
3700 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
3701 if (nfsvers
== NFS_VER3
) {
3702 nfsm_chain_add_32(error
, &nmreq
, vtonfs_type(vap
->va_type
, nfsvers
));
3703 nfsm_chain_add_v3sattr(error
, &nmreq
, vap
);
3704 if (vap
->va_type
== VCHR
|| vap
->va_type
== VBLK
) {
3705 nfsm_chain_add_32(error
, &nmreq
, major(vap
->va_rdev
));
3706 nfsm_chain_add_32(error
, &nmreq
, minor(vap
->va_rdev
));
3709 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, rdev
);
3711 nfsm_chain_build_done(error
, &nmreq
);
3713 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
3716 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_MKNOD
,
3717 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
3719 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
3721 if ((lockerror
= nfs_node_lock(dnp
)))
3723 /* XXX no EEXIST kludge here? */
3725 if (!error
&& !status
) {
3726 if (dnp
->n_flag
& NNEGNCENTRIES
) {
3727 dnp
->n_flag
&= ~NNEGNCENTRIES
;
3728 cache_purge_negatives(dvp
);
3730 error
= nfsm_chain_get_fh_attr(&nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
3732 if (nfsvers
== NFS_VER3
)
3733 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
3737 nfsm_chain_cleanup(&nmreq
);
3738 nfsm_chain_cleanup(&nmrep
);
3741 dnp
->n_flag
|= NMODIFIED
;
3742 /* if directory hadn't changed, update namecache mtime */
3743 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==))
3744 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
3745 nfs_node_unlock(dnp
);
3746 /* nfs_getattr() will check changed and purge caches */
3747 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
3750 if (!error
&& fh
.fh_len
)
3751 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
3753 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
3757 nfs_node_clear_busy(dnp
);
3759 if (!error
&& (gotuid
|| gotgid
) &&
3760 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
3761 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
3762 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
3763 /* clear ID bits if server didn't use them (or we can't tell) */
3764 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
3765 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
3769 nfs_node_unlock(np
);
3774 nfs_node_unlock(np
);
3779 static uint32_t create_verf
;
3781 * NFS file create call
3785 struct vnop_create_args
/* {
3786 struct vnodeop_desc *a_desc;
3789 struct componentname *a_cnp;
3790 struct vnode_attr *a_vap;
3791 vfs_context_t a_context;
3794 vfs_context_t ctx
= ap
->a_context
;
3795 vnode_t dvp
= ap
->a_dvp
;
3796 struct vnode_attr
*vap
= ap
->a_vap
;
3797 struct componentname
*cnp
= ap
->a_cnp
;
3798 struct nfs_vattr nvattr
;
3800 nfsnode_t np
= NULL
;
3801 struct nfsmount
*nmp
;
3802 nfsnode_t dnp
= VTONFS(dvp
);
3803 vnode_t newvp
= NULL
;
3804 int error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0, fmode
= 0;
3805 struct timespec premtime
= { 0, 0 };
3806 int nfsvers
, gotuid
, gotgid
;
3807 u_int64_t xid
, dxid
;
3809 struct nfsm_chain nmreq
, nmrep
;
3810 struct nfsreq rq
, *req
= &rq
;
3811 struct nfs_dulookup dul
;
3814 if (nfs_mount_gone(nmp
))
3816 nfsvers
= nmp
->nm_vers
;
3818 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
))
3819 return (ENAMETOOLONG
);
3821 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
3823 VATTR_SET_SUPPORTED(vap
, va_mode
);
3824 VATTR_SET_SUPPORTED(vap
, va_uid
);
3825 VATTR_SET_SUPPORTED(vap
, va_gid
);
3826 VATTR_SET_SUPPORTED(vap
, va_data_size
);
3827 VATTR_SET_SUPPORTED(vap
, va_access_time
);
3828 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
3829 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
3830 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
3832 if (vap
->va_vaflags
& VA_EXCLUSIVE
) {
3834 if (!VATTR_IS_ACTIVE(vap
, va_access_time
) || !VATTR_IS_ACTIVE(vap
, va_modify_time
))
3835 vap
->va_vaflags
|= VA_UTIMES_NULL
;
3839 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
3840 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
3842 nfsm_chain_null(&nmreq
);
3843 nfsm_chain_null(&nmrep
);
3845 nfsm_chain_build_alloc_init(error
, &nmreq
,
3846 NFSX_FH(nfsvers
) + 2 * NFSX_UNSIGNED
+
3847 nfsm_rndup(cnp
->cn_namelen
) + NFSX_SATTR(nfsvers
));
3848 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
3849 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
3850 if (nfsvers
== NFS_VER3
) {
3851 if (fmode
& O_EXCL
) {
3852 nfsm_chain_add_32(error
, &nmreq
, NFS_CREATE_EXCLUSIVE
);
3853 lck_rw_lock_shared(in_ifaddr_rwlock
);
3854 if (!TAILQ_EMPTY(&in_ifaddrhead
))
3855 val
= IA_SIN(in_ifaddrhead
.tqh_first
)->sin_addr
.s_addr
;
3858 lck_rw_done(in_ifaddr_rwlock
);
3859 nfsm_chain_add_32(error
, &nmreq
, val
);
3861 nfsm_chain_add_32(error
, &nmreq
, create_verf
);
3863 nfsm_chain_add_32(error
, &nmreq
, NFS_CREATE_UNCHECKED
);
3864 nfsm_chain_add_v3sattr(error
, &nmreq
, vap
);
3867 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, 0);
3869 nfsm_chain_build_done(error
, &nmreq
);
3872 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_CREATE
,
3873 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
3875 nfs_dulookup_start(&dul
, dnp
, ctx
);
3876 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
3879 if ((lockerror
= nfs_node_lock(dnp
)))
3882 if (!error
&& !status
) {
3883 if (dnp
->n_flag
& NNEGNCENTRIES
) {
3884 dnp
->n_flag
&= ~NNEGNCENTRIES
;
3885 cache_purge_negatives(dvp
);
3887 error
= nfsm_chain_get_fh_attr(&nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
3889 if (nfsvers
== NFS_VER3
)
3890 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
3894 nfsm_chain_cleanup(&nmreq
);
3895 nfsm_chain_cleanup(&nmrep
);
3898 dnp
->n_flag
|= NMODIFIED
;
3899 /* if directory hadn't changed, update namecache mtime */
3900 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==))
3901 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
3902 nfs_node_unlock(dnp
);
3903 /* nfs_getattr() will check changed and purge caches */
3904 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
3907 if (!error
&& fh
.fh_len
)
3908 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
3910 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
3914 nfs_dulookup_finish(&dul
, dnp
, ctx
);
3916 nfs_node_clear_busy(dnp
);
3919 if ((nfsvers
== NFS_VER3
) && (fmode
& O_EXCL
) && (error
== NFSERR_NOTSUPP
)) {
3924 nfs_node_unlock(np
);
3927 } else if ((nfsvers
== NFS_VER3
) && (fmode
& O_EXCL
)) {
3928 nfs_node_unlock(np
);
3929 error
= nfs3_setattr_rpc(np
, vap
, ctx
);
3930 if (error
&& (gotuid
|| gotgid
)) {
3931 /* it's possible the server didn't like our attempt to set IDs. */
3932 /* so, let's try it again without those */
3933 VATTR_CLEAR_ACTIVE(vap
, va_uid
);
3934 VATTR_CLEAR_ACTIVE(vap
, va_gid
);
3935 error
= nfs3_setattr_rpc(np
, vap
, ctx
);
3940 nfs_node_lock_force(np
);
3944 if (!error
&& (gotuid
|| gotgid
) &&
3945 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
3946 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
3947 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
3948 /* clear ID bits if server didn't use them (or we can't tell) */
3949 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
3950 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
3953 nfs_node_unlock(np
);
3958 * NFS file remove call
3959 * To try and make NFS semantics closer to UFS semantics, a file that has
3960 * other processes using the vnode is renamed instead of removed and then
3961 * removed later on the last close.
3962 * - If vnode_isinuse()
3963 * If a rename is not already in the works
3964 * call nfs_sillyrename() to set it up
3970 struct vnop_remove_args
/* {
3971 struct vnodeop_desc *a_desc;
3974 struct componentname *a_cnp;
3976 vfs_context_t a_context;
3979 vfs_context_t ctx
= ap
->a_context
;
3980 vnode_t vp
= ap
->a_vp
;
3981 vnode_t dvp
= ap
->a_dvp
;
3982 struct componentname
*cnp
= ap
->a_cnp
;
3983 nfsnode_t dnp
= VTONFS(dvp
);
3984 nfsnode_t np
= VTONFS(vp
);
3985 int error
= 0, nfsvers
, namedattrs
, inuse
, gotattr
= 0, flushed
= 0, setsize
= 0;
3986 struct nfs_vattr nvattr
;
3987 struct nfsmount
*nmp
;
3988 struct nfs_dulookup dul
;
3990 /* XXX prevent removing a sillyrenamed file? */
3992 nmp
= NFSTONMP(dnp
);
3993 if (nfs_mount_gone(nmp
))
3995 nfsvers
= nmp
->nm_vers
;
3996 namedattrs
= (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
);
3999 error
= nfs_node_set_busy2(dnp
, np
, vfs_context_thread(ctx
));
4003 /* lock the node while we remove the file */
4004 lck_mtx_lock(nfs_node_hash_mutex
);
4005 while (np
->n_hflag
& NHLOCKED
) {
4006 np
->n_hflag
|= NHLOCKWANT
;
4007 msleep(np
, nfs_node_hash_mutex
, PINOD
, "nfs_remove", NULL
);
4009 np
->n_hflag
|= NHLOCKED
;
4010 lck_mtx_unlock(nfs_node_hash_mutex
);
4013 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4015 inuse
= vnode_isinuse(vp
, 0);
4016 if ((ap
->a_flags
& VNODE_REMOVE_NODELETEBUSY
) && inuse
) {
4017 /* Caller requested Carbon delete semantics, but file is busy */
4021 if (inuse
&& !gotattr
) {
4022 if (nfs_getattr(np
, &nvattr
, ctx
, NGA_CACHED
))
4023 nvattr
.nva_nlink
= 1;
4027 if (!inuse
|| (np
->n_sillyrename
&& (nvattr
.nva_nlink
> 1))) {
4029 if (!inuse
&& !flushed
) { /* flush all the buffers first */
4030 /* unlock the node */
4031 lck_mtx_lock(nfs_node_hash_mutex
);
4032 np
->n_hflag
&= ~NHLOCKED
;
4033 if (np
->n_hflag
& NHLOCKWANT
) {
4034 np
->n_hflag
&= ~NHLOCKWANT
;
4037 lck_mtx_unlock(nfs_node_hash_mutex
);
4038 nfs_node_clear_busy2(dnp
, np
);
4039 error
= nfs_vinvalbuf(vp
, V_SAVE
, ctx
, 1);
4040 FSDBG(260, np
, np
->n_size
, np
->n_vattr
.nva_size
, 0xf00d0011);
4042 if (error
== EINTR
) {
4043 nfs_node_lock_force(np
);
4044 NATTRINVALIDATE(np
);
4045 nfs_node_unlock(np
);
4049 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4053 if ((nmp
->nm_vers
>= NFS_VER4
) && (np
->n_openflags
& N_DELEG_MASK
))
4054 nfs4_delegation_return(np
, 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4057 * Purge the name cache so that the chance of a lookup for
4058 * the name succeeding while the remove is in progress is
4061 nfs_name_cache_purge(dnp
, np
, cnp
, ctx
);
4064 nfs_dulookup_start(&dul
, dnp
, ctx
);
4067 error
= nmp
->nm_funcs
->nf_remove_rpc(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
,
4068 vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4071 * Kludge City: If the first reply to the remove rpc is lost..
4072 * the reply to the retransmitted request will be ENOENT
4073 * since the file was in fact removed
4074 * Therefore, we cheat and return success.
4076 if (error
== ENOENT
)
4079 if (!error
&& !inuse
&& !np
->n_sillyrename
) {
4081 * removal succeeded, it's not in use, and not silly renamed so
4082 * remove nfsnode from hash now so we can't accidentally find it
4083 * again if another object gets created with the same filehandle
4084 * before this vnode gets reclaimed
4086 lck_mtx_lock(nfs_node_hash_mutex
);
4087 if (np
->n_hflag
& NHHASHED
) {
4088 LIST_REMOVE(np
, n_hash
);
4089 np
->n_hflag
&= ~NHHASHED
;
4090 FSDBG(266, 0, np
, np
->n_flag
, 0xb1eb1e);
4092 lck_mtx_unlock(nfs_node_hash_mutex
);
4093 /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */
4094 /* clear all flags other than these */
4095 nfs_node_lock_force(np
);
4096 np
->n_flag
&= (NMODIFIED
);
4097 NATTRINVALIDATE(np
);
4098 nfs_node_unlock(np
);
4102 nfs_node_lock_force(np
);
4103 NATTRINVALIDATE(np
);
4104 nfs_node_unlock(np
);
4106 } else if (!np
->n_sillyrename
) {
4108 nfs_dulookup_start(&dul
, dnp
, ctx
);
4109 error
= nfs_sillyrename(dnp
, np
, cnp
, ctx
);
4110 nfs_node_lock_force(np
);
4111 NATTRINVALIDATE(np
);
4112 nfs_node_unlock(np
);
4114 nfs_node_lock_force(np
);
4115 NATTRINVALIDATE(np
);
4116 nfs_node_unlock(np
);
4118 nfs_dulookup_start(&dul
, dnp
, ctx
);
4121 /* nfs_getattr() will check changed and purge caches */
4122 nfs_getattr(dnp
, NULL
, ctx
, NGA_CACHED
);
4124 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4126 /* unlock the node */
4127 lck_mtx_lock(nfs_node_hash_mutex
);
4128 np
->n_hflag
&= ~NHLOCKED
;
4129 if (np
->n_hflag
& NHLOCKWANT
) {
4130 np
->n_hflag
&= ~NHLOCKWANT
;
4133 lck_mtx_unlock(nfs_node_hash_mutex
);
4134 nfs_node_clear_busy2(dnp
, np
);
4141 * NFS silly-renamed file removal function called from nfs_vnop_inactive
4144 nfs_removeit(struct nfs_sillyrename
*nsp
)
4146 struct nfsmount
*nmp
= NFSTONMP(nsp
->nsr_dnp
);
4147 if (nfs_mount_gone(nmp
))
4149 return nmp
->nm_funcs
->nf_remove_rpc(nsp
->nsr_dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, NULL
, nsp
->nsr_cred
);
4153 * NFS remove rpc, called from nfs_remove() and nfs_removeit().
4163 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0;
4164 struct timespec premtime
= { 0, 0 };
4165 struct nfsmount
*nmp
;
4168 struct nfsm_chain nmreq
, nmrep
;
4170 nmp
= NFSTONMP(dnp
);
4171 if (nfs_mount_gone(nmp
))
4173 nfsvers
= nmp
->nm_vers
;
4174 if ((nfsvers
== NFS_VER2
) && (namelen
> NFS_MAXNAMLEN
))
4175 return (ENAMETOOLONG
);
4177 nfsm_chain_null(&nmreq
);
4178 nfsm_chain_null(&nmrep
);
4180 nfsm_chain_build_alloc_init(error
, &nmreq
,
4181 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+ nfsm_rndup(namelen
));
4182 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4183 nfsm_chain_add_name(error
, &nmreq
, name
, namelen
, nmp
);
4184 nfsm_chain_build_done(error
, &nmreq
);
4187 error
= nfs_request2(dnp
, NULL
, &nmreq
, NFSPROC_REMOVE
, thd
, cred
, NULL
, 0, &nmrep
, &xid
, &status
);
4189 if ((lockerror
= nfs_node_lock(dnp
)))
4191 if (nfsvers
== NFS_VER3
)
4192 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &xid
);
4194 dnp
->n_flag
|= NMODIFIED
;
4195 /* if directory hadn't changed, update namecache mtime */
4196 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==))
4197 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
4199 NATTRINVALIDATE(dnp
);
4204 nfs_node_unlock(dnp
);
4205 nfsm_chain_cleanup(&nmreq
);
4206 nfsm_chain_cleanup(&nmrep
);
4211 * NFS file rename call
4215 struct vnop_rename_args
/* {
4216 struct vnodeop_desc *a_desc;
4219 struct componentname *a_fcnp;
4222 struct componentname *a_tcnp;
4223 vfs_context_t a_context;
4226 vfs_context_t ctx
= ap
->a_context
;
4227 vnode_t fdvp
= ap
->a_fdvp
;
4228 vnode_t fvp
= ap
->a_fvp
;
4229 vnode_t tdvp
= ap
->a_tdvp
;
4230 vnode_t tvp
= ap
->a_tvp
;
4231 nfsnode_t fdnp
, fnp
, tdnp
, tnp
;
4232 struct componentname
*tcnp
= ap
->a_tcnp
;
4233 struct componentname
*fcnp
= ap
->a_fcnp
;
4234 int error
, nfsvers
, inuse
=0, tvprecycle
=0, locked
=0;
4235 mount_t fmp
, tdmp
, tmp
;
4236 struct nfs_vattr nvattr
;
4237 struct nfsmount
*nmp
;
4239 fdnp
= VTONFS(fdvp
);
4241 tdnp
= VTONFS(tdvp
);
4242 tnp
= tvp
? VTONFS(tvp
) : NULL
;
4244 nmp
= NFSTONMP(fdnp
);
4245 if (nfs_mount_gone(nmp
))
4247 nfsvers
= nmp
->nm_vers
;
4249 error
= nfs_node_set_busy4(fdnp
, fnp
, tdnp
, tnp
, vfs_context_thread(ctx
));
4253 if (tvp
&& (tvp
!= fvp
)) {
4254 /* lock the node while we rename over the existing file */
4255 lck_mtx_lock(nfs_node_hash_mutex
);
4256 while (tnp
->n_hflag
& NHLOCKED
) {
4257 tnp
->n_hflag
|= NHLOCKWANT
;
4258 msleep(tnp
, nfs_node_hash_mutex
, PINOD
, "nfs_rename", NULL
);
4260 tnp
->n_hflag
|= NHLOCKED
;
4261 lck_mtx_unlock(nfs_node_hash_mutex
);
4265 /* Check for cross-device rename */
4266 fmp
= vnode_mount(fvp
);
4267 tmp
= tvp
? vnode_mount(tvp
) : NULL
;
4268 tdmp
= vnode_mount(tdvp
);
4269 if ((fmp
!= tdmp
) || (tvp
&& (fmp
!= tmp
))) {
4274 /* XXX prevent renaming from/over a sillyrenamed file? */
4277 * If the tvp exists and is in use, sillyrename it before doing the
4278 * rename of the new file over it.
4279 * XXX Can't sillyrename a directory.
4280 * Don't sillyrename if source and target are same vnode (hard
4281 * links or case-variants)
4283 if (tvp
&& (tvp
!= fvp
))
4284 inuse
= vnode_isinuse(tvp
, 0);
4285 if (inuse
&& !tnp
->n_sillyrename
&& (vnode_vtype(tvp
) != VDIR
)) {
4286 error
= nfs_sillyrename(tdnp
, tnp
, tcnp
, ctx
);
4288 /* sillyrename failed. Instead of pressing on, return error */
4289 goto out
; /* should not be ENOENT. */
4291 /* sillyrename succeeded.*/
4294 } else if (tvp
&& (nmp
->nm_vers
>= NFS_VER4
) && (tnp
->n_openflags
& N_DELEG_MASK
)) {
4295 nfs4_delegation_return(tnp
, 0, vfs_context_thread(ctx
), vfs_context_ucred(ctx
));
4298 error
= nmp
->nm_funcs
->nf_rename_rpc(fdnp
, fcnp
->cn_nameptr
, fcnp
->cn_namelen
,
4299 tdnp
, tcnp
->cn_nameptr
, tcnp
->cn_namelen
, ctx
);
4302 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
4304 if (error
== ENOENT
)
4307 if (tvp
&& (tvp
!= fvp
) && !tnp
->n_sillyrename
) {
4308 nfs_node_lock_force(tnp
);
4309 tvprecycle
= (!error
&& !vnode_isinuse(tvp
, 0) &&
4310 (nfs_getattrcache(tnp
, &nvattr
, 0) || (nvattr
.nva_nlink
== 1)));
4311 nfs_node_unlock(tnp
);
4312 lck_mtx_lock(nfs_node_hash_mutex
);
4313 if (tvprecycle
&& (tnp
->n_hflag
& NHHASHED
)) {
4315 * remove nfsnode from hash now so we can't accidentally find it
4316 * again if another object gets created with the same filehandle
4317 * before this vnode gets reclaimed
4319 LIST_REMOVE(tnp
, n_hash
);
4320 tnp
->n_hflag
&= ~NHHASHED
;
4321 FSDBG(266, 0, tnp
, tnp
->n_flag
, 0xb1eb1e);
4323 lck_mtx_unlock(nfs_node_hash_mutex
);
4326 /* purge the old name cache entries and enter the new one */
4327 nfs_name_cache_purge(fdnp
, fnp
, fcnp
, ctx
);
4329 nfs_name_cache_purge(tdnp
, tnp
, tcnp
, ctx
);
4331 /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */
4332 /* clear all flags other than these */
4333 nfs_node_lock_force(tnp
);
4334 tnp
->n_flag
&= (NMODIFIED
);
4335 nfs_node_unlock(tnp
);
4340 nfs_node_lock_force(tdnp
);
4341 if (tdnp
->n_flag
& NNEGNCENTRIES
) {
4342 tdnp
->n_flag
&= ~NNEGNCENTRIES
;
4343 cache_purge_negatives(tdvp
);
4345 nfs_node_unlock(tdnp
);
4346 nfs_node_lock_force(fnp
);
4347 cache_enter(tdvp
, fvp
, tcnp
);
4348 if (tdvp
!= fdvp
) { /* update parent pointer */
4349 if (fnp
->n_parent
&& !vnode_get(fnp
->n_parent
)) {
4350 /* remove ref from old parent */
4351 vnode_rele(fnp
->n_parent
);
4352 vnode_put(fnp
->n_parent
);
4354 fnp
->n_parent
= tdvp
;
4355 if (tdvp
&& !vnode_get(tdvp
)) {
4356 /* add ref to new parent */
4360 fnp
->n_parent
= NULL
;
4363 nfs_node_unlock(fnp
);
4366 /* nfs_getattr() will check changed and purge caches */
4367 nfs_getattr(fdnp
, NULL
, ctx
, NGA_CACHED
);
4368 nfs_getattr(tdnp
, NULL
, ctx
, NGA_CACHED
);
4371 lck_mtx_lock(nfs_node_hash_mutex
);
4372 tnp
->n_hflag
&= ~NHLOCKED
;
4373 if (tnp
->n_hflag
& NHLOCKWANT
) {
4374 tnp
->n_hflag
&= ~NHLOCKWANT
;
4377 lck_mtx_unlock(nfs_node_hash_mutex
);
4379 nfs_node_clear_busy4(fdnp
, fnp
, tdnp
, tnp
);
4384 * Do an NFS rename rpc. Called from nfs_vnop_rename() and nfs_sillyrename().
4396 int error
= 0, lockerror
= ENOENT
, status
, fwccpostattr
= 0, twccpostattr
= 0;
4397 struct timespec fpremtime
= { 0, 0 }, tpremtime
= { 0, 0 };
4398 struct nfsmount
*nmp
;
4400 u_int64_t xid
, txid
;
4401 struct nfsm_chain nmreq
, nmrep
;
4403 nmp
= NFSTONMP(fdnp
);
4404 if (nfs_mount_gone(nmp
))
4406 nfsvers
= nmp
->nm_vers
;
4407 if ((nfsvers
== NFS_VER2
) &&
4408 ((fnamelen
> NFS_MAXNAMLEN
) || (tnamelen
> NFS_MAXNAMLEN
)))
4409 return (ENAMETOOLONG
);
4411 nfsm_chain_null(&nmreq
);
4412 nfsm_chain_null(&nmrep
);
4414 nfsm_chain_build_alloc_init(error
, &nmreq
,
4415 (NFSX_FH(nfsvers
) + NFSX_UNSIGNED
) * 2 +
4416 nfsm_rndup(fnamelen
) + nfsm_rndup(tnamelen
));
4417 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, fdnp
->n_fhp
, fdnp
->n_fhsize
);
4418 nfsm_chain_add_name(error
, &nmreq
, fnameptr
, fnamelen
, nmp
);
4419 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, tdnp
->n_fhp
, tdnp
->n_fhsize
);
4420 nfsm_chain_add_name(error
, &nmreq
, tnameptr
, tnamelen
, nmp
);
4421 nfsm_chain_build_done(error
, &nmreq
);
4424 error
= nfs_request(fdnp
, NULL
, &nmreq
, NFSPROC_RENAME
, ctx
, NULL
, &nmrep
, &xid
, &status
);
4426 if ((lockerror
= nfs_node_lock2(fdnp
, tdnp
)))
4428 if (nfsvers
== NFS_VER3
) {
4430 nfsm_chain_get_wcc_data(error
, &nmrep
, fdnp
, &fpremtime
, &fwccpostattr
, &xid
);
4431 nfsm_chain_get_wcc_data(error
, &nmrep
, tdnp
, &tpremtime
, &twccpostattr
, &txid
);
4436 nfsm_chain_cleanup(&nmreq
);
4437 nfsm_chain_cleanup(&nmrep
);
4439 fdnp
->n_flag
|= NMODIFIED
;
4440 /* if directory hadn't changed, update namecache mtime */
4441 if (nfstimespeccmp(&fdnp
->n_ncmtime
, &fpremtime
, ==))
4442 NFS_CHANGED_UPDATE_NC(nfsvers
, fdnp
, &fdnp
->n_vattr
);
4444 NATTRINVALIDATE(fdnp
);
4445 tdnp
->n_flag
|= NMODIFIED
;
4446 /* if directory hadn't changed, update namecache mtime */
4447 if (nfstimespeccmp(&tdnp
->n_ncmtime
, &tpremtime
, ==))
4448 NFS_CHANGED_UPDATE_NC(nfsvers
, tdnp
, &tdnp
->n_vattr
);
4450 NATTRINVALIDATE(tdnp
);
4451 nfs_node_unlock2(fdnp
, tdnp
);
4457 * NFS hard link create call
4461 struct vnop_link_args
/* {
4462 struct vnodeop_desc *a_desc;
4465 struct componentname *a_cnp;
4466 vfs_context_t a_context;
4469 vfs_context_t ctx
= ap
->a_context
;
4470 vnode_t vp
= ap
->a_vp
;
4471 vnode_t tdvp
= ap
->a_tdvp
;
4472 struct componentname
*cnp
= ap
->a_cnp
;
4473 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0, attrflag
= 0;
4474 struct timespec premtime
= { 0, 0 };
4475 struct nfsmount
*nmp
;
4476 nfsnode_t np
= VTONFS(vp
);
4477 nfsnode_t tdnp
= VTONFS(tdvp
);
4479 u_int64_t xid
, txid
;
4480 struct nfsm_chain nmreq
, nmrep
;
4482 if (vnode_mount(vp
) != vnode_mount(tdvp
))
4486 if (nfs_mount_gone(nmp
))
4488 nfsvers
= nmp
->nm_vers
;
4489 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
))
4490 return (ENAMETOOLONG
);
4493 * Push all writes to the server, so that the attribute cache
4494 * doesn't get "out of sync" with the server.
4495 * XXX There should be a better way!
4497 nfs_flush(np
, MNT_WAIT
, vfs_context_thread(ctx
), V_IGNORE_WRITEERR
);
4499 error
= nfs_node_set_busy2(tdnp
, np
, vfs_context_thread(ctx
));
4503 nfsm_chain_null(&nmreq
);
4504 nfsm_chain_null(&nmrep
);
4506 nfsm_chain_build_alloc_init(error
, &nmreq
,
4507 NFSX_FH(nfsvers
)*2 + NFSX_UNSIGNED
+ nfsm_rndup(cnp
->cn_namelen
));
4508 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
4509 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, tdnp
->n_fhp
, tdnp
->n_fhsize
);
4510 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4511 nfsm_chain_build_done(error
, &nmreq
);
4513 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_LINK
, ctx
, NULL
, &nmrep
, &xid
, &status
);
4515 if ((lockerror
= nfs_node_lock2(tdnp
, np
))) {
4519 if (nfsvers
== NFS_VER3
) {
4521 nfsm_chain_postop_attr_update_flag(error
, &nmrep
, np
, attrflag
, &xid
);
4522 nfsm_chain_get_wcc_data(error
, &nmrep
, tdnp
, &premtime
, &wccpostattr
, &txid
);
4527 nfsm_chain_cleanup(&nmreq
);
4528 nfsm_chain_cleanup(&nmrep
);
4531 NATTRINVALIDATE(np
);
4532 tdnp
->n_flag
|= NMODIFIED
;
4533 /* if directory hadn't changed, update namecache mtime */
4534 if (nfstimespeccmp(&tdnp
->n_ncmtime
, &premtime
, ==))
4535 NFS_CHANGED_UPDATE_NC(nfsvers
, tdnp
, &tdnp
->n_vattr
);
4537 NATTRINVALIDATE(tdnp
);
4538 if (!error
&& (tdnp
->n_flag
& NNEGNCENTRIES
)) {
4539 tdnp
->n_flag
&= ~NNEGNCENTRIES
;
4540 cache_purge_negatives(tdvp
);
4542 nfs_node_unlock2(tdnp
, np
);
4544 nfs_node_clear_busy2(tdnp
, np
);
4546 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
4548 if (error
== EEXIST
)
4554 * NFS symbolic link create call
4558 struct vnop_symlink_args
/* {
4559 struct vnodeop_desc *a_desc;
4562 struct componentname *a_cnp;
4563 struct vnode_attr *a_vap;
4565 vfs_context_t a_context;
4568 vfs_context_t ctx
= ap
->a_context
;
4569 vnode_t dvp
= ap
->a_dvp
;
4570 struct vnode_attr
*vap
= ap
->a_vap
;
4571 struct componentname
*cnp
= ap
->a_cnp
;
4572 struct nfs_vattr nvattr
;
4574 int slen
, error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0;
4575 struct timespec premtime
= { 0, 0 };
4576 vnode_t newvp
= NULL
;
4577 int nfsvers
, gotuid
, gotgid
;
4578 u_int64_t xid
= 0, dxid
;
4579 nfsnode_t np
= NULL
;
4580 nfsnode_t dnp
= VTONFS(dvp
);
4581 struct nfsmount
*nmp
;
4582 struct nfsm_chain nmreq
, nmrep
;
4583 struct nfsreq rq
, *req
= &rq
;
4584 struct nfs_dulookup dul
;
4587 if (nfs_mount_gone(nmp
))
4589 nfsvers
= nmp
->nm_vers
;
4591 slen
= strlen(ap
->a_target
);
4592 if ((nfsvers
== NFS_VER2
) &&
4593 ((cnp
->cn_namelen
> NFS_MAXNAMLEN
) || (slen
> NFS_MAXPATHLEN
)))
4594 return (ENAMETOOLONG
);
4596 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
4598 VATTR_SET_SUPPORTED(vap
, va_mode
);
4599 VATTR_SET_SUPPORTED(vap
, va_uid
);
4600 VATTR_SET_SUPPORTED(vap
, va_gid
);
4601 VATTR_SET_SUPPORTED(vap
, va_data_size
);
4602 VATTR_SET_SUPPORTED(vap
, va_access_time
);
4603 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
4604 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
4605 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
4607 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
4608 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4610 nfsm_chain_null(&nmreq
);
4611 nfsm_chain_null(&nmrep
);
4613 nfsm_chain_build_alloc_init(error
, &nmreq
,
4614 NFSX_FH(nfsvers
) + 2 * NFSX_UNSIGNED
+
4615 nfsm_rndup(cnp
->cn_namelen
) + nfsm_rndup(slen
) + NFSX_SATTR(nfsvers
));
4616 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4617 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4618 if (nfsvers
== NFS_VER3
)
4619 nfsm_chain_add_v3sattr(error
, &nmreq
, vap
);
4620 nfsm_chain_add_name(error
, &nmreq
, ap
->a_target
, slen
, nmp
);
4621 if (nfsvers
== NFS_VER2
)
4622 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, -1);
4623 nfsm_chain_build_done(error
, &nmreq
);
4626 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_SYMLINK
,
4627 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
4629 nfs_dulookup_start(&dul
, dnp
, ctx
);
4630 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
4633 if ((lockerror
= nfs_node_lock(dnp
)))
4636 if (!error
&& !status
) {
4637 if (dnp
->n_flag
& NNEGNCENTRIES
) {
4638 dnp
->n_flag
&= ~NNEGNCENTRIES
;
4639 cache_purge_negatives(dvp
);
4641 if (nfsvers
== NFS_VER3
)
4642 error
= nfsm_chain_get_fh_attr(&nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
4646 if (nfsvers
== NFS_VER3
)
4647 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
4651 nfsm_chain_cleanup(&nmreq
);
4652 nfsm_chain_cleanup(&nmrep
);
4655 dnp
->n_flag
|= NMODIFIED
;
4656 /* if directory hadn't changed, update namecache mtime */
4657 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==))
4658 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
4659 nfs_node_unlock(dnp
);
4660 /* nfs_getattr() will check changed and purge caches */
4661 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
4664 if (!error
&& fh
.fh_len
)
4665 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
4669 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4672 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
4673 * if we can succeed in looking up the symlink.
4675 if ((error
== EEXIST
) || (!error
&& !newvp
)) {
4677 nfs_node_unlock(np
);
4681 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
4684 if (vnode_vtype(newvp
) != VLNK
)
4689 nfs_node_clear_busy(dnp
);
4690 if (!error
&& (gotuid
|| gotgid
) &&
4691 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
4692 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
4693 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
4694 /* clear ID bits if server didn't use them (or we can't tell) */
4695 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
4696 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
4700 nfs_node_unlock(np
);
4704 nfs_node_unlock(np
);
4715 struct vnop_mkdir_args
/* {
4716 struct vnodeop_desc *a_desc;
4719 struct componentname *a_cnp;
4720 struct vnode_attr *a_vap;
4721 vfs_context_t a_context;
4724 vfs_context_t ctx
= ap
->a_context
;
4725 vnode_t dvp
= ap
->a_dvp
;
4726 struct vnode_attr
*vap
= ap
->a_vap
;
4727 struct componentname
*cnp
= ap
->a_cnp
;
4728 struct nfs_vattr nvattr
;
4729 nfsnode_t np
= NULL
;
4730 struct nfsmount
*nmp
;
4731 nfsnode_t dnp
= VTONFS(dvp
);
4732 vnode_t newvp
= NULL
;
4733 int error
= 0, lockerror
= ENOENT
, busyerror
= ENOENT
, status
, wccpostattr
= 0;
4734 struct timespec premtime
= { 0, 0 };
4735 int nfsvers
, gotuid
, gotgid
;
4736 u_int64_t xid
= 0, dxid
;
4738 struct nfsm_chain nmreq
, nmrep
;
4739 struct nfsreq rq
, *req
= &rq
;
4740 struct nfs_dulookup dul
;
4743 if (nfs_mount_gone(nmp
))
4745 nfsvers
= nmp
->nm_vers
;
4746 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
))
4747 return (ENAMETOOLONG
);
4749 nfs_avoid_needless_id_setting_on_create(dnp
, vap
, ctx
);
4751 VATTR_SET_SUPPORTED(vap
, va_mode
);
4752 VATTR_SET_SUPPORTED(vap
, va_uid
);
4753 VATTR_SET_SUPPORTED(vap
, va_gid
);
4754 VATTR_SET_SUPPORTED(vap
, va_data_size
);
4755 VATTR_SET_SUPPORTED(vap
, va_access_time
);
4756 VATTR_SET_SUPPORTED(vap
, va_modify_time
);
4757 gotuid
= VATTR_IS_ACTIVE(vap
, va_uid
);
4758 gotgid
= VATTR_IS_ACTIVE(vap
, va_gid
);
4760 error
= busyerror
= nfs_node_set_busy(dnp
, vfs_context_thread(ctx
));
4761 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4763 nfsm_chain_null(&nmreq
);
4764 nfsm_chain_null(&nmrep
);
4766 nfsm_chain_build_alloc_init(error
, &nmreq
,
4767 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+
4768 nfsm_rndup(cnp
->cn_namelen
) + NFSX_SATTR(nfsvers
));
4769 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4770 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4771 if (nfsvers
== NFS_VER3
)
4772 nfsm_chain_add_v3sattr(error
, &nmreq
, vap
);
4774 nfsm_chain_add_v2sattr(error
, &nmreq
, vap
, -1);
4775 nfsm_chain_build_done(error
, &nmreq
);
4778 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_MKDIR
,
4779 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
4781 nfs_dulookup_start(&dul
, dnp
, ctx
);
4782 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
4785 if ((lockerror
= nfs_node_lock(dnp
)))
4788 if (!error
&& !status
) {
4789 if (dnp
->n_flag
& NNEGNCENTRIES
) {
4790 dnp
->n_flag
&= ~NNEGNCENTRIES
;
4791 cache_purge_negatives(dvp
);
4793 error
= nfsm_chain_get_fh_attr(&nmrep
, dnp
, ctx
, nfsvers
, &xid
, &fh
, &nvattr
);
4795 if (nfsvers
== NFS_VER3
)
4796 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &dxid
);
4800 nfsm_chain_cleanup(&nmreq
);
4801 nfsm_chain_cleanup(&nmrep
);
4804 dnp
->n_flag
|= NMODIFIED
;
4805 /* if directory hadn't changed, update namecache mtime */
4806 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==))
4807 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
4808 nfs_node_unlock(dnp
);
4809 /* nfs_getattr() will check changed and purge caches */
4810 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
4813 if (!error
&& fh
.fh_len
)
4814 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
, &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
4818 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4821 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
4822 * if we can succeed in looking up the directory.
4824 if ((error
== EEXIST
) || (!error
&& !newvp
)) {
4826 nfs_node_unlock(np
);
4830 error
= nfs_lookitup(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
, &np
);
4833 if (vnode_vtype(newvp
) != VDIR
)
4838 nfs_node_clear_busy(dnp
);
4839 if (!error
&& (gotuid
|| gotgid
) &&
4840 (!newvp
|| nfs_getattrcache(np
, &nvattr
, 0) ||
4841 (gotuid
&& (nvattr
.nva_uid
!= vap
->va_uid
)) ||
4842 (gotgid
&& (nvattr
.nva_gid
!= vap
->va_gid
)))) {
4843 /* clear ID bits if server didn't use them (or we can't tell) */
4844 VATTR_CLEAR_SUPPORTED(vap
, va_uid
);
4845 VATTR_CLEAR_SUPPORTED(vap
, va_gid
);
4849 nfs_node_unlock(np
);
4853 nfs_node_unlock(np
);
4860 * NFS remove directory call
4864 struct vnop_rmdir_args
/* {
4865 struct vnodeop_desc *a_desc;
4868 struct componentname *a_cnp;
4869 vfs_context_t a_context;
4872 vfs_context_t ctx
= ap
->a_context
;
4873 vnode_t vp
= ap
->a_vp
;
4874 vnode_t dvp
= ap
->a_dvp
;
4875 struct componentname
*cnp
= ap
->a_cnp
;
4876 int error
= 0, lockerror
= ENOENT
, status
, wccpostattr
= 0;
4877 struct timespec premtime
= { 0, 0 };
4878 struct nfsmount
*nmp
;
4879 nfsnode_t np
= VTONFS(vp
);
4880 nfsnode_t dnp
= VTONFS(dvp
);
4883 struct nfsm_chain nmreq
, nmrep
;
4884 struct nfsreq rq
, *req
= &rq
;
4885 struct nfs_dulookup dul
;
4888 if (nfs_mount_gone(nmp
))
4890 nfsvers
= nmp
->nm_vers
;
4891 if ((nfsvers
== NFS_VER2
) && (cnp
->cn_namelen
> NFS_MAXNAMLEN
))
4892 return (ENAMETOOLONG
);
4894 if ((error
= nfs_node_set_busy2(dnp
, np
, vfs_context_thread(ctx
))))
4897 nfs_dulookup_init(&dul
, dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, ctx
);
4899 nfsm_chain_null(&nmreq
);
4900 nfsm_chain_null(&nmrep
);
4902 nfsm_chain_build_alloc_init(error
, &nmreq
,
4903 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+ nfsm_rndup(cnp
->cn_namelen
));
4904 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
4905 nfsm_chain_add_name(error
, &nmreq
, cnp
->cn_nameptr
, cnp
->cn_namelen
, nmp
);
4906 nfsm_chain_build_done(error
, &nmreq
);
4909 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_RMDIR
,
4910 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, &req
);
4912 nfs_dulookup_start(&dul
, dnp
, ctx
);
4913 error
= nfs_request_async_finish(req
, &nmrep
, &xid
, &status
);
4916 if ((lockerror
= nfs_node_lock(dnp
)))
4918 if (nfsvers
== NFS_VER3
)
4919 nfsm_chain_get_wcc_data(error
, &nmrep
, dnp
, &premtime
, &wccpostattr
, &xid
);
4923 nfsm_chain_cleanup(&nmreq
);
4924 nfsm_chain_cleanup(&nmrep
);
4927 dnp
->n_flag
|= NMODIFIED
;
4928 /* if directory hadn't changed, update namecache mtime */
4929 if (nfstimespeccmp(&dnp
->n_ncmtime
, &premtime
, ==))
4930 NFS_CHANGED_UPDATE_NC(nfsvers
, dnp
, &dnp
->n_vattr
);
4931 nfs_node_unlock(dnp
);
4932 nfs_name_cache_purge(dnp
, np
, cnp
, ctx
);
4933 /* nfs_getattr() will check changed and purge caches */
4934 nfs_getattr(dnp
, NULL
, ctx
, wccpostattr
? NGA_CACHED
: NGA_UNCACHED
);
4936 nfs_dulookup_finish(&dul
, dnp
, ctx
);
4937 nfs_node_clear_busy2(dnp
, np
);
4940 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
4942 if (error
== ENOENT
)
4946 * remove nfsnode from hash now so we can't accidentally find it
4947 * again if another object gets created with the same filehandle
4948 * before this vnode gets reclaimed
4950 lck_mtx_lock(nfs_node_hash_mutex
);
4951 if (np
->n_hflag
& NHHASHED
) {
4952 LIST_REMOVE(np
, n_hash
);
4953 np
->n_hflag
&= ~NHHASHED
;
4954 FSDBG(266, 0, np
, np
->n_flag
, 0xb1eb1e);
4956 lck_mtx_unlock(nfs_node_hash_mutex
);
4964 * The incoming "offset" is a directory cookie indicating where in the
4965 * directory entries should be read from. A zero cookie means start at
4966 * the beginning of the directory. Any other cookie will be a cookie
4967 * returned from the server.
4969 * Using that cookie, determine which buffer (and where in that buffer)
4970 * to start returning entries from. Buffer logical block numbers are
4971 * the cookies they start at. If a buffer is found that is not full,
4972 * call into the bio/RPC code to fill it. The RPC code will probably
4973 * fill several buffers (dropping the first, requiring a re-get).
4975 * When done copying entries to the buffer, set the offset to the current
4976 * entry's cookie and enter that cookie in the cookie cache.
4978 * Note: because the getdirentries(2) API returns a long-typed offset,
4979 * the incoming offset is a potentially truncated cookie (ptc).
4980 * The cookie matching code is aware of this and will fall back to
4981 * matching only 32 bits of the cookie.
4985 struct vnop_readdir_args
/* {
4986 struct vnodeop_desc *a_desc;
4992 vfs_context_t a_context;
4995 vfs_context_t ctx
= ap
->a_context
;
4996 vnode_t dvp
= ap
->a_vp
;
4997 nfsnode_t dnp
= VTONFS(dvp
);
4998 struct nfsmount
*nmp
;
4999 uio_t uio
= ap
->a_uio
;
5000 int error
, nfsvers
, extended
, numdirent
, bigcookies
, ptc
, done
;
5001 uint16_t i
, iptc
, rlen
, nlen
;
5002 uint64_t cookie
, nextcookie
, lbn
= 0;
5003 struct nfsbuf
*bp
= NULL
;
5004 struct nfs_dir_buf_header
*ndbhp
;
5005 struct direntry
*dp
, *dpptc
;
5011 if (nfs_mount_gone(nmp
))
5013 nfsvers
= nmp
->nm_vers
;
5014 bigcookies
= (nmp
->nm_state
& NFSSTA_BIGCOOKIES
);
5015 extended
= (ap
->a_flags
& VNODE_READDIR_EXTENDED
);
5017 if (vnode_vtype(dvp
) != VDIR
)
5023 if (uio_resid(uio
) == 0)
5026 if ((nfsvers
>= NFS_VER4
) && (dnp
->n_vattr
.nva_flags
& NFS_FFLAG_TRIGGER
)) {
5027 /* trigger directories should never be read, return nothing */
5031 thd
= vfs_context_thread(ctx
);
5032 numdirent
= done
= 0;
5033 nextcookie
= uio_offset(uio
);
5034 ptc
= bigcookies
&& NFS_DIR_COOKIE_POTENTIALLY_TRUNCATED(nextcookie
);
5036 if ((error
= nfs_node_lock(dnp
)))
5039 if (dnp
->n_flag
& NNEEDINVALIDATE
) {
5040 dnp
->n_flag
&= ~NNEEDINVALIDATE
;
5042 nfs_node_unlock(dnp
);
5043 error
= nfs_vinvalbuf(dvp
, 0, ctx
, 1);
5045 error
= nfs_node_lock(dnp
);
5051 * check for need to invalidate when (re)starting at beginning
5054 if (dnp
->n_flag
& NMODIFIED
) {
5056 nfs_node_unlock(dnp
);
5057 if ((error
= nfs_vinvalbuf(dvp
, 0, ctx
, 1)))
5060 nfs_node_unlock(dnp
);
5062 /* nfs_getattr() will check changed and purge caches */
5063 if ((error
= nfs_getattr(dnp
, NULL
, ctx
, NGA_UNCACHED
)))
5066 nfs_node_unlock(dnp
);
5069 error
= nfs_dir_cookie_to_lbn(dnp
, nextcookie
, &ptc
, &lbn
);
5071 if (error
< 0) { /* just hit EOF cookie */
5079 while (!error
&& !done
) {
5080 OSAddAtomic64(1, &nfsstats
.biocache_readdirs
);
5081 cookie
= nextcookie
;
5083 error
= nfs_buf_get(dnp
, lbn
, NFS_DIRBLKSIZ
, thd
, NBLK_READ
, &bp
);
5086 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5087 if (!ISSET(bp
->nb_flags
, NB_CACHE
) || !ISSET(ndbhp
->ndbh_flags
, NDB_FULL
)) {
5088 if (!ISSET(bp
->nb_flags
, NB_CACHE
)) { /* initialize the buffer */
5089 ndbhp
->ndbh_flags
= 0;
5090 ndbhp
->ndbh_count
= 0;
5091 ndbhp
->ndbh_entry_end
= sizeof(*ndbhp
);
5092 ndbhp
->ndbh_ncgen
= dnp
->n_ncgen
;
5094 error
= nfs_buf_readdir(bp
, ctx
);
5095 if (error
== NFSERR_DIRBUFDROPPED
)
5098 nfs_buf_release(bp
, 1);
5099 if (error
&& (error
!= ENXIO
) && (error
!= ETIMEDOUT
) && (error
!= EINTR
) && (error
!= ERESTART
)) {
5100 if (!nfs_node_lock(dnp
)) {
5102 nfs_node_unlock(dnp
);
5104 nfs_vinvalbuf(dvp
, 0, ctx
, 1);
5105 if (error
== NFSERR_BAD_COOKIE
)
5112 /* find next entry to return */
5113 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5115 if ((lbn
!= cookie
) && !(ptc
&& NFS_DIR_COOKIE_SAME32(lbn
, cookie
))) {
5118 for (; (i
< ndbhp
->ndbh_count
) && (cookie
!= dp
->d_seekoff
); i
++) {
5119 if (ptc
&& !dpptc
&& NFS_DIR_COOKIE_SAME32(cookie
, dp
->d_seekoff
)) {
5123 nextcookie
= dp
->d_seekoff
;
5124 dp
= NFS_DIRENTRY_NEXT(dp
);
5126 if ((i
== ndbhp
->ndbh_count
) && dpptc
) {
5130 if (i
< ndbhp
->ndbh_count
) {
5131 nextcookie
= dp
->d_seekoff
;
5132 dp
= NFS_DIRENTRY_NEXT(dp
);
5136 ptc
= 0; /* only have to deal with ptc on first cookie */
5138 /* return as many entries as we can */
5139 for (; i
< ndbhp
->ndbh_count
; i
++) {
5141 rlen
= dp
->d_reclen
;
5146 bzero(cp
, sizeof(dent
));
5148 if (dp
->d_namlen
> (sizeof(dent
.d_name
) - 1))
5149 nlen
= sizeof(dent
.d_name
) - 1;
5151 nlen
= dp
->d_namlen
;
5152 rlen
= NFS_DIRENT_LEN(nlen
);
5153 dent
.d_reclen
= rlen
;
5154 dent
.d_ino
= dp
->d_ino
;
5155 dent
.d_type
= dp
->d_type
;
5156 dent
.d_namlen
= nlen
;
5157 strlcpy(dent
.d_name
, dp
->d_name
, nlen
+ 1);
5159 /* check that the record fits */
5160 if (rlen
> uio_resid(uio
)) {
5164 if ((error
= uiomove(cp
, rlen
, uio
)))
5167 nextcookie
= dp
->d_seekoff
;
5168 dp
= NFS_DIRENTRY_NEXT(dp
);
5171 if (i
== ndbhp
->ndbh_count
) {
5172 /* hit end of buffer, move to next buffer */
5174 /* if we also hit EOF, we're done */
5175 if (ISSET(ndbhp
->ndbh_flags
, NDB_EOF
)) {
5182 uio_setoffset(uio
, nextcookie
);
5183 if (!error
&& !done
&& (nextcookie
== cookie
)) {
5184 printf("nfs readdir cookie didn't change 0x%llx, %d/%d\n", cookie
, i
, ndbhp
->ndbh_count
);
5187 nfs_buf_release(bp
, 1);
5191 nfs_dir_cookie_cache(dnp
, nextcookie
, lbn
);
5193 if (ap
->a_numdirent
)
5194 *ap
->a_numdirent
= numdirent
;
5201 * Invalidate cached directory information, except for the actual directory
5202 * blocks (which are invalidated separately).
5205 nfs_invaldir(nfsnode_t dnp
)
5207 if (vnode_vtype(NFSTOV(dnp
)) != VDIR
)
5209 dnp
->n_eofcookie
= 0;
5210 dnp
->n_cookieverf
= 0;
5211 if (!dnp
->n_cookiecache
)
5213 dnp
->n_cookiecache
->free
= 0;
5214 dnp
->n_cookiecache
->mru
= -1;
5215 memset(dnp
->n_cookiecache
->next
, -1, NFSNUMCOOKIES
);
5219 * calculate how much space is available for additional directory entries.
5222 nfs_dir_buf_freespace(struct nfsbuf
*bp
, int rdirplus
)
5224 struct nfs_dir_buf_header
*ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5229 space
= bp
->nb_bufsize
- ndbhp
->ndbh_entry_end
;
5231 space
-= ndbhp
->ndbh_count
* sizeof(struct nfs_vattr
);
5236 * add/update a cookie->lbn entry in the directory cookie cache
5239 nfs_dir_cookie_cache(nfsnode_t dnp
, uint64_t cookie
, uint64_t lbn
)
5241 struct nfsdmap
*ndcc
;
5247 if (nfs_node_lock(dnp
))
5250 if (cookie
== dnp
->n_eofcookie
) { /* EOF cookie */
5251 nfs_node_unlock(dnp
);
5255 ndcc
= dnp
->n_cookiecache
;
5257 /* allocate the cookie cache structure */
5258 MALLOC_ZONE(dnp
->n_cookiecache
, struct nfsdmap
*,
5259 sizeof(struct nfsdmap
), M_NFSDIROFF
, M_WAITOK
);
5260 if (!dnp
->n_cookiecache
) {
5261 nfs_node_unlock(dnp
);
5264 ndcc
= dnp
->n_cookiecache
;
5267 memset(ndcc
->next
, -1, NFSNUMCOOKIES
);
5271 * Search the list for this cookie.
5272 * Keep track of previous and last entries.
5276 while ((i
!= -1) && (cookie
!= ndcc
->cookies
[i
].key
)) {
5277 if (ndcc
->next
[i
] == -1) /* stop on last entry so we can reuse */
5282 if ((i
!= -1) && (cookie
== ndcc
->cookies
[i
].key
)) {
5283 /* found it, remove from list */
5285 ndcc
->next
[prev
] = ndcc
->next
[i
];
5287 ndcc
->mru
= ndcc
->next
[i
];
5289 /* not found, use next free entry or reuse last entry */
5290 if (ndcc
->free
!= NFSNUMCOOKIES
)
5293 ndcc
->next
[prev
] = -1;
5294 ndcc
->cookies
[i
].key
= cookie
;
5295 ndcc
->cookies
[i
].lbn
= lbn
;
5297 /* insert cookie at head of MRU list */
5298 ndcc
->next
[i
] = ndcc
->mru
;
5300 nfs_node_unlock(dnp
);
5304 * Try to map the given directory cookie to a directory buffer (return lbn).
5305 * If we have a possibly truncated cookie (ptc), check for 32-bit matches too.
5308 nfs_dir_cookie_to_lbn(nfsnode_t dnp
, uint64_t cookie
, int *ptc
, uint64_t *lbnp
)
5310 struct nfsdmap
*ndcc
= dnp
->n_cookiecache
;
5311 int8_t eofptc
, found
;
5313 struct nfsmount
*nmp
;
5314 struct nfsbuf
*bp
, *lastbp
;
5315 struct nfsbuflists blist
;
5316 struct direntry
*dp
, *dpptc
;
5317 struct nfs_dir_buf_header
*ndbhp
;
5319 if (!cookie
) { /* initial cookie */
5325 if (nfs_node_lock(dnp
))
5328 if (cookie
== dnp
->n_eofcookie
) { /* EOF cookie */
5329 nfs_node_unlock(dnp
);
5330 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5334 /* note if cookie is a 32-bit match with the EOF cookie */
5335 eofptc
= *ptc
? NFS_DIR_COOKIE_SAME32(cookie
, dnp
->n_eofcookie
) : 0;
5338 /* search the list for the cookie */
5339 for (i
= ndcc
? ndcc
->mru
: -1; i
>= 0; i
= ndcc
->next
[i
]) {
5340 if (ndcc
->cookies
[i
].key
== cookie
) {
5341 /* found a match for this cookie */
5342 *lbnp
= ndcc
->cookies
[i
].lbn
;
5343 nfs_node_unlock(dnp
);
5344 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5348 /* check for 32-bit match */
5349 if (*ptc
&& (iptc
== -1) && NFS_DIR_COOKIE_SAME32(ndcc
->cookies
[i
].key
, cookie
))
5352 /* exact match not found */
5354 /* but 32-bit match hit the EOF cookie */
5355 nfs_node_unlock(dnp
);
5356 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5360 /* but 32-bit match got a hit */
5361 *lbnp
= ndcc
->cookies
[iptc
].lbn
;
5362 nfs_node_unlock(dnp
);
5363 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5366 nfs_node_unlock(dnp
);
5369 * No match found in the cookie cache... hmm...
5370 * Let's search the directory's buffers for the cookie.
5372 nmp
= NFSTONMP(dnp
);
5373 if (nfs_mount_gone(nmp
))
5378 lck_mtx_lock(nfs_buf_mutex
);
5380 * Scan the list of buffers, keeping them in order.
5381 * Note that itercomplete inserts each of the remaining buffers
5382 * into the head of list (thus reversing the elements). So, we
5383 * make sure to iterate through all buffers, inserting them after
5384 * each other, to keep them in order.
5385 * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because
5386 * we don't drop nfs_buf_mutex.
5388 if (!nfs_buf_iterprepare(dnp
, &blist
, NBI_CLEAN
)) {
5390 while ((bp
= LIST_FIRST(&blist
))) {
5391 LIST_REMOVE(bp
, nb_vnbufs
);
5393 LIST_INSERT_HEAD(&dnp
->n_cleanblkhd
, bp
, nb_vnbufs
);
5395 LIST_INSERT_AFTER(lastbp
, bp
, nb_vnbufs
);
5400 if (nfs_buf_acquire(bp
, NBAC_NOWAIT
, 0, 0)) {
5401 /* just skip this buffer */
5402 nfs_buf_refrele(bp
);
5405 nfs_buf_refrele(bp
);
5407 /* scan the buffer for the cookie */
5408 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5409 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5411 for (i
=0; (i
< ndbhp
->ndbh_count
) && (cookie
!= dp
->d_seekoff
); i
++) {
5412 if (*ptc
&& !dpptc
&& NFS_DIR_COOKIE_SAME32(cookie
, dp
->d_seekoff
)) {
5416 dp
= NFS_DIRENTRY_NEXT(dp
);
5418 if ((i
== ndbhp
->ndbh_count
) && dpptc
) {
5419 /* found only a PTC match */
5422 } else if (i
< ndbhp
->ndbh_count
) {
5425 if (i
< (ndbhp
->ndbh_count
-1)) {
5426 /* next entry is *in* this buffer: return this block */
5427 *lbnp
= bp
->nb_lblkno
;
5429 } else if (i
== (ndbhp
->ndbh_count
-1)) {
5430 /* next entry refers to *next* buffer: return next block */
5431 *lbnp
= dp
->d_seekoff
;
5436 nfs_buf_itercomplete(dnp
, &blist
, NBI_CLEAN
);
5438 lck_mtx_unlock(nfs_buf_mutex
);
5440 OSAddAtomic64(1, &nfsstats
.direofcache_hits
);
5444 /* still not found... oh well, just start a new block */
5446 OSAddAtomic64(1, &nfsstats
.direofcache_misses
);
5451 * scan a directory buffer for the given name
5452 * Returns: ESRCH if not found, ENOENT if found invalid, 0 if found
5453 * Note: should only be called with RDIRPLUS directory buffers
5456 #define NDBS_PURGE 1
5457 #define NDBS_UPDATE 2
5462 struct componentname
*cnp
,
5464 struct nfs_vattr
*nvap
,
5467 daddr64_t
*nextlbnp
,
5470 struct direntry
*dp
;
5471 struct nfs_dir_buf_header
*ndbhp
;
5472 struct nfs_vattr
*nvattrp
;
5473 daddr64_t nextlbn
= 0;
5474 int i
, error
= ESRCH
;
5477 /* scan the buffer for the name */
5478 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5479 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5480 for (i
=0; i
< ndbhp
->ndbh_count
; i
++) {
5481 nextlbn
= dp
->d_seekoff
;
5482 if ((cnp
->cn_namelen
== dp
->d_namlen
) && !strcmp(cnp
->cn_nameptr
, dp
->d_name
)) {
5483 fhlen
= dp
->d_name
[dp
->d_namlen
+1];
5484 nvattrp
= NFS_DIR_BUF_NVATTR(bp
, i
);
5485 if ((ndbhp
->ndbh_ncgen
!= bp
->nb_np
->n_ncgen
) || (fhp
->fh_len
== 0) ||
5486 (nvattrp
->nva_type
== VNON
) || (nvattrp
->nva_fileid
== 0)) {
5487 /* entry is not valid */
5491 if (flags
== NDBS_PURGE
) {
5493 bzero(nvattrp
, sizeof(*nvattrp
));
5497 if (flags
== NDBS_UPDATE
) {
5498 /* update direntry's attrs if fh matches */
5499 if ((fhp
->fh_len
== fhlen
) && !bcmp(&dp
->d_name
[dp
->d_namlen
+2], fhp
->fh_data
, fhlen
)) {
5500 bcopy(nvap
, nvattrp
, sizeof(*nvap
));
5501 dp
->d_fileno
= nvattrp
->nva_fileid
;
5502 nvattrp
->nva_fileid
= *xidp
;
5503 *(time_t*)(&dp
->d_name
[dp
->d_namlen
+2+fhp
->fh_len
]) = *attrstampp
;
5508 /* copy out fh, attrs, attrstamp, and xid */
5509 fhp
->fh_len
= fhlen
;
5510 bcopy(&dp
->d_name
[dp
->d_namlen
+2], fhp
->fh_data
, MAX(fhp
->fh_len
, (int)sizeof(fhp
->fh_data
)));
5511 *attrstampp
= *(time_t*)(&dp
->d_name
[dp
->d_namlen
+2+fhp
->fh_len
]);
5512 bcopy(nvattrp
, nvap
, sizeof(*nvap
));
5513 *xidp
= nvap
->nva_fileid
;
5514 nvap
->nva_fileid
= dp
->d_fileno
;
5518 dp
= NFS_DIRENTRY_NEXT(dp
);
5521 *nextlbnp
= nextlbn
;
5526 * Look up a name in a directory's buffers.
5527 * Note: should only be called with RDIRPLUS directory buffers
5530 nfs_dir_buf_cache_lookup(nfsnode_t dnp
, nfsnode_t
*npp
, struct componentname
*cnp
, vfs_context_t ctx
, int purge
)
5533 struct nfsmount
*nmp
;
5534 int error
= 0, i
, found
= 0, count
= 0;
5536 struct nfs_vattr nvattr
;
5538 time_t attrstamp
= 0;
5539 thread_t thd
= vfs_context_thread(ctx
);
5540 struct nfsbuf
*bp
, *lastbp
, *foundbp
;
5541 struct nfsbuflists blist
;
5542 daddr64_t lbn
, nextlbn
;
5543 int dotunder
= (cnp
->cn_namelen
> 2) && (cnp
->cn_nameptr
[0] == '.') && (cnp
->cn_nameptr
[1] == '_');
5545 nmp
= NFSTONMP(dnp
);
5546 if (nfs_mount_gone(nmp
))
5551 /* first check most recent buffer (and next one too) */
5552 lbn
= dnp
->n_lastdbl
;
5553 for (i
=0; i
< 2; i
++) {
5554 if ((error
= nfs_buf_get(dnp
, lbn
, NFS_DIRBLKSIZ
, thd
, NBLK_READ
|NBLK_ONLYVALID
, &bp
)))
5559 error
= nfs_dir_buf_search(bp
, cnp
, &fh
, &nvattr
, &xid
, &attrstamp
, &nextlbn
, purge
? NDBS_PURGE
: 0);
5560 nfs_buf_release(bp
, 0);
5561 if (error
== ESRCH
) {
5570 lck_mtx_lock(nfs_buf_mutex
);
5572 dnp
->n_lastdbl
= lbn
;
5577 * Scan the list of buffers, keeping them in order.
5578 * Note that itercomplete inserts each of the remaining buffers
5579 * into the head of list (thus reversing the elements). So, we
5580 * make sure to iterate through all buffers, inserting them after
5581 * each other, to keep them in order.
5582 * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because
5583 * we don't drop nfs_buf_mutex.
5585 if (!nfs_buf_iterprepare(dnp
, &blist
, NBI_CLEAN
)) {
5586 lastbp
= foundbp
= NULL
;
5587 while ((bp
= LIST_FIRST(&blist
))) {
5588 LIST_REMOVE(bp
, nb_vnbufs
);
5590 LIST_INSERT_HEAD(&dnp
->n_cleanblkhd
, bp
, nb_vnbufs
);
5592 LIST_INSERT_AFTER(lastbp
, bp
, nb_vnbufs
);
5596 if (!purge
&& dotunder
&& (count
> 100)) /* don't waste too much time looking for ._ files */
5599 lbn
= bp
->nb_lblkno
;
5600 if (nfs_buf_acquire(bp
, NBAC_NOWAIT
, 0, 0)) {
5601 /* just skip this buffer */
5602 nfs_buf_refrele(bp
);
5605 nfs_buf_refrele(bp
);
5607 error
= nfs_dir_buf_search(bp
, cnp
, &fh
, &nvattr
, &xid
, &attrstamp
, NULL
, purge
? NDBS_PURGE
: 0);
5608 if (error
== ESRCH
) {
5617 LIST_REMOVE(foundbp
, nb_vnbufs
);
5618 LIST_INSERT_HEAD(&dnp
->n_cleanblkhd
, foundbp
, nb_vnbufs
);
5619 dnp
->n_lastdbl
= foundbp
->nb_lblkno
;
5621 nfs_buf_itercomplete(dnp
, &blist
, NBI_CLEAN
);
5624 lck_mtx_unlock(nfs_buf_mutex
);
5626 if (!error
&& found
&& !purge
) {
5627 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
,
5628 &nvattr
, &xid
, dnp
->n_auth
, NG_MAKEENTRY
, &newnp
);
5631 newnp
->n_attrstamp
= attrstamp
;
5633 nfs_node_unlock(newnp
);
5634 /* check if the dir buffer's attrs are out of date */
5635 if (!nfs_getattr(newnp
, &nvattr
, ctx
, NGA_CACHED
) &&
5636 (newnp
->n_attrstamp
!= attrstamp
)) {
5637 /* they are, so update them */
5638 error
= nfs_buf_get(dnp
, lbn
, NFS_DIRBLKSIZ
, thd
, NBLK_READ
|NBLK_ONLYVALID
, &bp
);
5640 attrstamp
= newnp
->n_attrstamp
;
5642 nfs_dir_buf_search(bp
, cnp
, &fh
, &nvattr
, &xid
, &attrstamp
, NULL
, NDBS_UPDATE
);
5643 nfs_buf_release(bp
, 0);
5653 * Purge name cache entries for the given node.
5654 * For RDIRPLUS, also invalidate the entry in the directory's buffers.
5657 nfs_name_cache_purge(nfsnode_t dnp
, nfsnode_t np
, struct componentname
*cnp
, vfs_context_t ctx
)
5659 struct nfsmount
*nmp
= NFSTONMP(dnp
);
5661 cache_purge(NFSTOV(np
));
5662 if (nmp
&& (nmp
->nm_vers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
))
5663 nfs_dir_buf_cache_lookup(dnp
, NULL
, cnp
, ctx
, 1);
5667 * NFS V3 readdir (plus) RPC.
5670 nfs3_readdir_rpc(nfsnode_t dnp
, struct nfsbuf
*bp
, vfs_context_t ctx
)
5672 struct nfsmount
*nmp
;
5673 int error
= 0, lockerror
, nfsvers
, rdirplus
, bigcookies
;
5674 int i
, status
, attrflag
, fhflag
, more_entries
= 1, eof
, bp_dropped
= 0;
5675 uint32_t nmreaddirsize
, nmrsize
;
5676 uint32_t namlen
, skiplen
, fhlen
, xlen
, attrlen
, reclen
, space_free
, space_needed
;
5677 uint64_t cookie
, lastcookie
, xid
, savedxid
, fileno
;
5678 struct nfsm_chain nmreq
, nmrep
, nmrepsave
;
5680 struct nfs_vattr
*nvattrp
;
5681 struct nfs_dir_buf_header
*ndbhp
;
5682 struct direntry
*dp
;
5683 char *padstart
, padlen
;
5686 nmp
= NFSTONMP(dnp
);
5687 if (nfs_mount_gone(nmp
))
5689 nfsvers
= nmp
->nm_vers
;
5690 nmreaddirsize
= nmp
->nm_readdirsize
;
5691 nmrsize
= nmp
->nm_rsize
;
5692 bigcookies
= nmp
->nm_state
& NFSSTA_BIGCOOKIES
;
5694 rdirplus
= ((nfsvers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
)) ? 1 : 0;
5696 if ((lockerror
= nfs_node_lock(dnp
)))
5699 /* determine cookie to use, and move dp to the right offset */
5700 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5701 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5702 if (ndbhp
->ndbh_count
) {
5703 for (i
=0; i
< ndbhp
->ndbh_count
-1; i
++)
5704 dp
= NFS_DIRENTRY_NEXT(dp
);
5705 cookie
= dp
->d_seekoff
;
5706 dp
= NFS_DIRENTRY_NEXT(dp
);
5708 cookie
= bp
->nb_lblkno
;
5709 /* increment with every buffer read */
5710 OSAddAtomic64(1, &nfsstats
.readdir_bios
);
5712 lastcookie
= cookie
;
5715 * Loop around doing readdir(plus) RPCs of size nm_readdirsize until
5716 * the buffer is full (or we hit EOF). Then put the remainder of the
5717 * results in the next buffer(s).
5719 nfsm_chain_null(&nmreq
);
5720 nfsm_chain_null(&nmrep
);
5721 while (nfs_dir_buf_freespace(bp
, rdirplus
) && !(ndbhp
->ndbh_flags
& NDB_FULL
)) {
5722 nfsm_chain_build_alloc_init(error
, &nmreq
,
5723 NFSX_FH(nfsvers
) + NFSX_READDIR(nfsvers
) + NFSX_UNSIGNED
);
5724 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
5725 if (nfsvers
== NFS_VER3
) {
5726 /* opaque values don't need swapping, but as long */
5727 /* as we are consistent about it, it should be ok */
5728 nfsm_chain_add_64(error
, &nmreq
, cookie
);
5729 nfsm_chain_add_64(error
, &nmreq
, dnp
->n_cookieverf
);
5731 nfsm_chain_add_32(error
, &nmreq
, cookie
);
5733 nfsm_chain_add_32(error
, &nmreq
, nmreaddirsize
);
5735 nfsm_chain_add_32(error
, &nmreq
, nmrsize
);
5736 nfsm_chain_build_done(error
, &nmreq
);
5737 nfs_node_unlock(dnp
);
5741 error
= nfs_request(dnp
, NULL
, &nmreq
,
5742 rdirplus
? NFSPROC_READDIRPLUS
: NFSPROC_READDIR
,
5743 ctx
, NULL
, &nmrep
, &xid
, &status
);
5745 if ((lockerror
= nfs_node_lock(dnp
)))
5749 if (nfsvers
== NFS_VER3
)
5750 nfsm_chain_postop_attr_update(error
, &nmrep
, dnp
, &xid
);
5753 if (nfsvers
== NFS_VER3
)
5754 nfsm_chain_get_64(error
, &nmrep
, dnp
->n_cookieverf
);
5755 nfsm_chain_get_32(error
, &nmrep
, more_entries
);
5758 nfs_node_unlock(dnp
);
5761 if (error
== NFSERR_NOTSUPP
) {
5762 /* oops... it doesn't look like readdirplus is supported */
5763 lck_mtx_lock(&nmp
->nm_lock
);
5764 NFS_BITMAP_CLR(nmp
->nm_flags
, NFS_MFLAG_RDIRPLUS
);
5765 lck_mtx_unlock(&nmp
->nm_lock
);
5773 /* loop through the entries packing them into the buffer */
5774 while (more_entries
) {
5775 if (nfsvers
== NFS_VER3
)
5776 nfsm_chain_get_64(error
, &nmrep
, fileno
);
5778 nfsm_chain_get_32(error
, &nmrep
, fileno
);
5779 nfsm_chain_get_32(error
, &nmrep
, namlen
);
5781 /* just truncate names that don't fit in direntry.d_name */
5786 if (namlen
> (sizeof(dp
->d_name
)-1)) {
5787 skiplen
= namlen
- sizeof(dp
->d_name
) + 1;
5788 namlen
= sizeof(dp
->d_name
) - 1;
5792 /* guess that fh size will be same as parent */
5793 fhlen
= rdirplus
? (1 + dnp
->n_fhsize
) : 0;
5794 xlen
= rdirplus
? (fhlen
+ sizeof(time_t)) : 0;
5795 attrlen
= rdirplus
? sizeof(struct nfs_vattr
) : 0;
5796 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
5797 space_needed
= reclen
+ attrlen
;
5798 space_free
= nfs_dir_buf_freespace(bp
, rdirplus
);
5799 if (space_needed
> space_free
) {
5801 * We still have entries to pack, but we've
5802 * run out of room in the current buffer.
5803 * So we need to move to the next buffer.
5804 * The block# for the next buffer is the
5805 * last cookie in the current buffer.
5808 ndbhp
->ndbh_flags
|= NDB_FULL
;
5809 nfs_buf_release(bp
, 0);
5812 error
= nfs_buf_get(dnp
, lastcookie
, NFS_DIRBLKSIZ
, vfs_context_thread(ctx
), NBLK_READ
, &bp
);
5814 /* initialize buffer */
5815 ndbhp
= (struct nfs_dir_buf_header
*)bp
->nb_data
;
5816 ndbhp
->ndbh_flags
= 0;
5817 ndbhp
->ndbh_count
= 0;
5818 ndbhp
->ndbh_entry_end
= sizeof(*ndbhp
);
5819 ndbhp
->ndbh_ncgen
= dnp
->n_ncgen
;
5820 space_free
= nfs_dir_buf_freespace(bp
, rdirplus
);
5821 dp
= NFS_DIR_BUF_FIRST_DIRENTRY(bp
);
5822 /* increment with every buffer read */
5823 OSAddAtomic64(1, &nfsstats
.readdir_bios
);
5826 dp
->d_fileno
= fileno
;
5827 dp
->d_namlen
= namlen
;
5828 dp
->d_reclen
= reclen
;
5829 dp
->d_type
= DT_UNKNOWN
;
5830 nfsm_chain_get_opaque(error
, &nmrep
, namlen
, dp
->d_name
);
5832 dp
->d_name
[namlen
] = '\0';
5834 nfsm_chain_adv(error
, &nmrep
,
5835 nfsm_rndup(namlen
+ skiplen
) - nfsm_rndup(namlen
));
5836 if (nfsvers
== NFS_VER3
)
5837 nfsm_chain_get_64(error
, &nmrep
, cookie
);
5839 nfsm_chain_get_32(error
, &nmrep
, cookie
);
5841 dp
->d_seekoff
= cookie
;
5842 if (!bigcookies
&& (cookie
>> 32) && (nmp
== NFSTONMP(dnp
))) {
5843 /* we've got a big cookie, make sure flag is set */
5844 lck_mtx_lock(&nmp
->nm_lock
);
5845 nmp
->nm_state
|= NFSSTA_BIGCOOKIES
;
5846 lck_mtx_unlock(&nmp
->nm_lock
);
5850 nvattrp
= NFS_DIR_BUF_NVATTR(bp
, ndbhp
->ndbh_count
);
5851 /* check for attributes */
5852 nfsm_chain_get_32(error
, &nmrep
, attrflag
);
5855 /* grab attributes */
5856 error
= nfs_parsefattr(&nmrep
, NFS_VER3
, nvattrp
);
5858 dp
->d_type
= IFTODT(VTTOIF(nvattrp
->nva_type
));
5859 /* fileid is already in d_fileno, so stash xid in attrs */
5860 nvattrp
->nva_fileid
= savedxid
;
5862 /* mark the attributes invalid */
5863 bzero(nvattrp
, sizeof(struct nfs_vattr
));
5865 /* check for file handle */
5866 nfsm_chain_get_32(error
, &nmrep
, fhflag
);
5869 nfsm_chain_get_fh(error
, &nmrep
, NFS_VER3
, &fh
);
5871 fhlen
= fh
.fh_len
+ 1;
5872 xlen
= fhlen
+ sizeof(time_t);
5873 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
5874 space_needed
= reclen
+ attrlen
;
5875 if (space_needed
> space_free
) {
5876 /* didn't actually have the room... move on to next buffer */
5880 /* pack the file handle into the record */
5881 dp
->d_name
[dp
->d_namlen
+1] = fh
.fh_len
;
5882 bcopy(fh
.fh_data
, &dp
->d_name
[dp
->d_namlen
+2], fh
.fh_len
);
5884 /* mark the file handle invalid */
5886 fhlen
= fh
.fh_len
+ 1;
5887 xlen
= fhlen
+ sizeof(time_t);
5888 reclen
= NFS_DIRENTRY_LEN(namlen
+ xlen
);
5889 bzero(&dp
->d_name
[dp
->d_namlen
+1], fhlen
);
5891 *(time_t*)(&dp
->d_name
[dp
->d_namlen
+1+fhlen
]) = now
.tv_sec
;
5892 dp
->d_reclen
= reclen
;
5894 padstart
= dp
->d_name
+ dp
->d_namlen
+ 1 + xlen
;
5895 ndbhp
->ndbh_count
++;
5896 lastcookie
= cookie
;
5897 /* advance to next direntry in buffer */
5898 dp
= NFS_DIRENTRY_NEXT(dp
);
5899 ndbhp
->ndbh_entry_end
= (char*)dp
- bp
->nb_data
;
5900 /* zero out the pad bytes */
5901 padlen
= (char*)dp
- padstart
;
5903 bzero(padstart
, padlen
);
5904 /* check for more entries */
5905 nfsm_chain_get_32(error
, &nmrep
, more_entries
);
5908 /* Finally, get the eof boolean */
5909 nfsm_chain_get_32(error
, &nmrep
, eof
);
5912 ndbhp
->ndbh_flags
|= (NDB_FULL
|NDB_EOF
);
5913 nfs_node_lock_force(dnp
);
5914 dnp
->n_eofcookie
= lastcookie
;
5915 nfs_node_unlock(dnp
);
5920 nfs_buf_release(bp
, 0);
5924 if ((lockerror
= nfs_node_lock(dnp
)))
5927 nfsm_chain_cleanup(&nmrep
);
5928 nfsm_chain_null(&nmreq
);
5931 if (bp_dropped
&& bp
)
5932 nfs_buf_release(bp
, 0);
5934 nfs_node_unlock(dnp
);
5935 nfsm_chain_cleanup(&nmreq
);
5936 nfsm_chain_cleanup(&nmrep
);
5937 return (bp_dropped
? NFSERR_DIRBUFDROPPED
: error
);
5941 * Silly rename. To make the NFS filesystem that is stateless look a little
5942 * more like the "ufs" a remove of an active vnode is translated to a rename
5943 * to a funny looking filename that is removed by nfs_vnop_inactive on the
5944 * nfsnode. There is the potential for another process on a different client
5945 * to create the same funny name between when the lookitup() fails and the
5946 * rename() completes, but...
5949 /* format of "random" silly names - includes a number and pid */
5950 /* (note: shouldn't exceed size of nfs_sillyrename.nsr_name) */
5951 #define NFS_SILLYNAME_FORMAT ".nfs.%08x.%04x"
5952 /* starting from zero isn't silly enough */
5953 static uint32_t nfs_sillyrename_number
= 0x20051025;
5959 struct componentname
*cnp
,
5962 struct nfs_sillyrename
*nsp
;
5967 struct nfsmount
*nmp
;
5969 nmp
= NFSTONMP(dnp
);
5970 if (nfs_mount_gone(nmp
))
5973 nfs_name_cache_purge(dnp
, np
, cnp
, ctx
);
5975 MALLOC_ZONE(nsp
, struct nfs_sillyrename
*,
5976 sizeof (struct nfs_sillyrename
), M_NFSREQ
, M_WAITOK
);
5979 cred
= vfs_context_ucred(ctx
);
5980 kauth_cred_ref(cred
);
5981 nsp
->nsr_cred
= cred
;
5983 error
= vnode_ref(NFSTOV(dnp
));
5987 /* Fudge together a funny name */
5988 pid
= vfs_context_pid(ctx
);
5989 num
= OSAddAtomic(1, &nfs_sillyrename_number
);
5990 nsp
->nsr_namlen
= snprintf(nsp
->nsr_name
, sizeof(nsp
->nsr_name
),
5991 NFS_SILLYNAME_FORMAT
, num
, (pid
& 0xffff));
5992 if (nsp
->nsr_namlen
>= (int)sizeof(nsp
->nsr_name
))
5993 nsp
->nsr_namlen
= sizeof(nsp
->nsr_name
) - 1;
5995 /* Try lookitups until we get one that isn't there */
5996 while (nfs_lookitup(dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, ctx
, NULL
) == 0) {
5997 num
= OSAddAtomic(1, &nfs_sillyrename_number
);
5998 nsp
->nsr_namlen
= snprintf(nsp
->nsr_name
, sizeof(nsp
->nsr_name
),
5999 NFS_SILLYNAME_FORMAT
, num
, (pid
& 0xffff));
6000 if (nsp
->nsr_namlen
>= (int)sizeof(nsp
->nsr_name
))
6001 nsp
->nsr_namlen
= sizeof(nsp
->nsr_name
) - 1;
6004 /* now, do the rename */
6005 error
= nmp
->nm_funcs
->nf_rename_rpc(dnp
, cnp
->cn_nameptr
, cnp
->cn_namelen
,
6006 dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, ctx
);
6008 /* Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. */
6009 if (error
== ENOENT
)
6012 nfs_node_lock_force(dnp
);
6013 if (dnp
->n_flag
& NNEGNCENTRIES
) {
6014 dnp
->n_flag
&= ~NNEGNCENTRIES
;
6015 cache_purge_negatives(NFSTOV(dnp
));
6017 nfs_node_unlock(dnp
);
6019 FSDBG(267, dnp
, np
, num
, error
);
6022 error
= nfs_lookitup(dnp
, nsp
->nsr_name
, nsp
->nsr_namlen
, ctx
, &np
);
6023 nfs_node_lock_force(np
);
6024 np
->n_sillyrename
= nsp
;
6025 nfs_node_unlock(np
);
6028 vnode_rele(NFSTOV(dnp
));
6030 nsp
->nsr_cred
= NOCRED
;
6031 kauth_cred_unref(&cred
);
6032 FREE_ZONE(nsp
, sizeof(*nsp
), M_NFSREQ
);
6037 nfs3_lookup_rpc_async(
6042 struct nfsreq
**reqp
)
6044 struct nfsmount
*nmp
;
6045 struct nfsm_chain nmreq
;
6046 int error
= 0, nfsvers
;
6048 nmp
= NFSTONMP(dnp
);
6049 if (nfs_mount_gone(nmp
))
6051 nfsvers
= nmp
->nm_vers
;
6053 nfsm_chain_null(&nmreq
);
6055 nfsm_chain_build_alloc_init(error
, &nmreq
,
6056 NFSX_FH(nfsvers
) + NFSX_UNSIGNED
+ nfsm_rndup(namelen
));
6057 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, dnp
->n_fhp
, dnp
->n_fhsize
);
6058 nfsm_chain_add_name(error
, &nmreq
, name
, namelen
, nmp
);
6059 nfsm_chain_build_done(error
, &nmreq
);
6061 error
= nfs_request_async(dnp
, NULL
, &nmreq
, NFSPROC_LOOKUP
,
6062 vfs_context_thread(ctx
), vfs_context_ucred(ctx
), NULL
, 0, NULL
, reqp
);
6064 nfsm_chain_cleanup(&nmreq
);
6069 nfs3_lookup_rpc_async_finish(
6071 __unused
char *name
,
6072 __unused
int namelen
,
6077 struct nfs_vattr
*nvap
)
6079 int error
= 0, lockerror
= ENOENT
, status
, nfsvers
, attrflag
;
6081 struct nfsmount
*nmp
;
6082 struct nfsm_chain nmrep
;
6084 nmp
= NFSTONMP(dnp
);
6087 nfsvers
= nmp
->nm_vers
;
6089 nfsm_chain_null(&nmrep
);
6091 error
= nfs_request_async_finish(req
, &nmrep
, xidp
, &status
);
6093 if ((lockerror
= nfs_node_lock(dnp
)))
6096 if (error
|| status
) {
6097 if (nfsvers
== NFS_VER3
)
6098 nfsm_chain_postop_attr_update(error
, &nmrep
, dnp
, &xid
);
6104 nfsmout_if(error
|| !fhp
|| !nvap
);
6106 /* get the file handle */
6107 nfsm_chain_get_fh(error
, &nmrep
, nfsvers
, fhp
);
6109 /* get the attributes */
6110 if (nfsvers
== NFS_VER3
) {
6111 nfsm_chain_postop_attr_get(error
, &nmrep
, attrflag
, nvap
);
6112 nfsm_chain_postop_attr_update(error
, &nmrep
, dnp
, &xid
);
6113 if (!error
&& !attrflag
)
6114 error
= nfs3_getattr_rpc(NULL
, NFSTOMP(dnp
), fhp
->fh_data
, fhp
->fh_len
, 0, ctx
, nvap
, xidp
);
6116 error
= nfs_parsefattr(&nmrep
, nfsvers
, nvap
);
6120 nfs_node_unlock(dnp
);
6121 nfsm_chain_cleanup(&nmrep
);
6126 * Look up a file name and optionally either update the file handle or
6127 * allocate an nfsnode, depending on the value of npp.
6128 * npp == NULL --> just do the lookup
6129 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
6131 * *npp != NULL --> update the file handle in the vnode
6142 nfsnode_t np
, newnp
= NULL
;
6145 struct nfsmount
*nmp
;
6146 struct nfs_vattr nvattr
;
6147 struct nfsreq rq
, *req
= &rq
;
6149 nmp
= NFSTONMP(dnp
);
6150 if (nfs_mount_gone(nmp
))
6153 if (NFS_BITMAP_ISSET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXNAME
) &&
6154 (namelen
> (int)nmp
->nm_fsattr
.nfsa_maxname
))
6155 return (ENAMETOOLONG
);
6157 NVATTR_INIT(&nvattr
);
6159 /* check for lookup of "." */
6160 if ((name
[0] == '.') && (namelen
== 1)) {
6161 /* skip lookup, we know who we are */
6167 error
= nmp
->nm_funcs
->nf_lookup_rpc_async(dnp
, name
, namelen
, ctx
, &req
);
6169 error
= nmp
->nm_funcs
->nf_lookup_rpc_async_finish(dnp
, name
, namelen
, ctx
, req
, &xid
, &fh
, &nvattr
);
6170 nfsmout_if(!npp
|| error
);
6174 if (fh
.fh_len
!= np
->n_fhsize
) {
6175 u_char
*oldbuf
= (np
->n_fhsize
> NFS_SMALLFH
) ? np
->n_fhp
: NULL
;
6176 if (fh
.fh_len
> NFS_SMALLFH
) {
6177 MALLOC_ZONE(np
->n_fhp
, u_char
*, fh
.fh_len
, M_NFSBIGFH
, M_WAITOK
);
6184 np
->n_fhp
= &np
->n_fh
[0];
6187 FREE_ZONE(oldbuf
, np
->n_fhsize
, M_NFSBIGFH
);
6189 bcopy(fh
.fh_data
, np
->n_fhp
, fh
.fh_len
);
6190 np
->n_fhsize
= fh
.fh_len
;
6191 nfs_node_lock_force(np
);
6192 error
= nfs_loadattrcache(np
, &nvattr
, &xid
, 0);
6193 nfs_node_unlock(np
);
6196 } else if (NFS_CMPFH(dnp
, fh
.fh_data
, fh
.fh_len
)) {
6197 nfs_node_lock_force(dnp
);
6198 if (dnp
->n_xid
<= xid
)
6199 error
= nfs_loadattrcache(dnp
, &nvattr
, &xid
, 0);
6200 nfs_node_unlock(dnp
);
6204 struct componentname cn
, *cnp
= &cn
;
6205 bzero(cnp
, sizeof(*cnp
));
6206 cnp
->cn_nameptr
= name
;
6207 cnp
->cn_namelen
= namelen
;
6208 error
= nfs_nget(NFSTOMP(dnp
), dnp
, cnp
, fh
.fh_data
, fh
.fh_len
,
6209 &nvattr
, &xid
, rq
.r_auth
, NG_MAKEENTRY
, &np
);
6215 if (npp
&& !*npp
&& !error
)
6217 NVATTR_CLEANUP(&nvattr
);
6222 * set up and initialize a "._" file lookup structure used for
6223 * performing async lookups.
6226 nfs_dulookup_init(struct nfs_dulookup
*dulp
, nfsnode_t dnp
, const char *name
, int namelen
, vfs_context_t ctx
)
6228 int error
, du_namelen
;
6230 struct nfsmount
*nmp
= NFSTONMP(dnp
);
6232 /* check for ._ file in name cache */
6234 bzero(&dulp
->du_cn
, sizeof(dulp
->du_cn
));
6235 du_namelen
= namelen
+ 2;
6236 if (!nmp
|| NMFLAG(nmp
, NONEGNAMECACHE
))
6238 if ((namelen
>= 2) && (name
[0] == '.') && (name
[1] == '_'))
6240 if (du_namelen
>= (int)sizeof(dulp
->du_smallname
))
6241 MALLOC(dulp
->du_cn
.cn_nameptr
, char *, du_namelen
+ 1, M_TEMP
, M_WAITOK
);
6243 dulp
->du_cn
.cn_nameptr
= dulp
->du_smallname
;
6244 if (!dulp
->du_cn
.cn_nameptr
)
6246 dulp
->du_cn
.cn_namelen
= du_namelen
;
6247 snprintf(dulp
->du_cn
.cn_nameptr
, du_namelen
+ 1, "._%s", name
);
6248 dulp
->du_cn
.cn_nameptr
[du_namelen
] = '\0';
6249 dulp
->du_cn
.cn_nameiop
= LOOKUP
;
6250 dulp
->du_cn
.cn_flags
= MAKEENTRY
;
6252 error
= cache_lookup(NFSTOV(dnp
), &du_vp
, &dulp
->du_cn
);
6255 } else if (!error
) {
6256 nmp
= NFSTONMP(dnp
);
6257 if (nmp
&& (nmp
->nm_vers
> NFS_VER2
) && NMFLAG(nmp
, RDIRPLUS
)) {
6258 /* if rdirplus, try dir buf cache lookup */
6259 nfsnode_t du_np
= NULL
;
6260 if (!nfs_dir_buf_cache_lookup(dnp
, &du_np
, &dulp
->du_cn
, ctx
, 0) && du_np
) {
6261 /* dir buf cache hit */
6262 du_vp
= NFSTOV(du_np
);
6268 dulp
->du_flags
|= NFS_DULOOKUP_DOIT
;
6273 * start an async "._" file lookup request
6276 nfs_dulookup_start(struct nfs_dulookup
*dulp
, nfsnode_t dnp
, vfs_context_t ctx
)
6278 struct nfsmount
*nmp
= NFSTONMP(dnp
);
6279 struct nfsreq
*req
= &dulp
->du_req
;
6281 if (!nmp
|| !(dulp
->du_flags
& NFS_DULOOKUP_DOIT
) || (dulp
->du_flags
& NFS_DULOOKUP_INPROG
))
6283 if (!nmp
->nm_funcs
->nf_lookup_rpc_async(dnp
, dulp
->du_cn
.cn_nameptr
,
6284 dulp
->du_cn
.cn_namelen
, ctx
, &req
))
6285 dulp
->du_flags
|= NFS_DULOOKUP_INPROG
;
6289 * finish an async "._" file lookup request and clean up the structure
6292 nfs_dulookup_finish(struct nfs_dulookup
*dulp
, nfsnode_t dnp
, vfs_context_t ctx
)
6294 struct nfsmount
*nmp
= NFSTONMP(dnp
);
6299 struct nfs_vattr nvattr
;
6301 if (!nmp
|| !(dulp
->du_flags
& NFS_DULOOKUP_INPROG
))
6304 NVATTR_INIT(&nvattr
);
6305 error
= nmp
->nm_funcs
->nf_lookup_rpc_async_finish(dnp
, dulp
->du_cn
.cn_nameptr
,
6306 dulp
->du_cn
.cn_namelen
, ctx
, &dulp
->du_req
, &xid
, &fh
, &nvattr
);
6307 dulp
->du_flags
&= ~NFS_DULOOKUP_INPROG
;
6308 if (error
== ENOENT
) {
6309 /* add a negative entry in the name cache */
6310 nfs_node_lock_force(dnp
);
6311 cache_enter(NFSTOV(dnp
), NULL
, &dulp
->du_cn
);
6312 dnp
->n_flag
|= NNEGNCENTRIES
;
6313 nfs_node_unlock(dnp
);
6314 } else if (!error
) {
6315 error
= nfs_nget(NFSTOMP(dnp
), dnp
, &dulp
->du_cn
, fh
.fh_data
, fh
.fh_len
,
6316 &nvattr
, &xid
, dulp
->du_req
.r_auth
, NG_MAKEENTRY
, &du_np
);
6318 nfs_node_unlock(du_np
);
6319 vnode_put(NFSTOV(du_np
));
6322 NVATTR_CLEANUP(&nvattr
);
6324 if (dulp
->du_flags
& NFS_DULOOKUP_INPROG
)
6325 nfs_request_async_cancel(&dulp
->du_req
);
6326 if (dulp
->du_cn
.cn_nameptr
&& (dulp
->du_cn
.cn_nameptr
!= dulp
->du_smallname
))
6327 FREE(dulp
->du_cn
.cn_nameptr
, M_TEMP
);
6332 * NFS Version 3 commit RPC
6342 struct nfsmount
*nmp
;
6343 int error
= 0, lockerror
, status
, wccpostattr
= 0, nfsvers
;
6344 struct timespec premtime
= { 0, 0 };
6345 u_int64_t xid
, newwverf
;
6347 struct nfsm_chain nmreq
, nmrep
;
6350 FSDBG(521, np
, offset
, count
, nmp
? nmp
->nm_state
: 0);
6351 if (nfs_mount_gone(nmp
))
6353 if (!(nmp
->nm_state
& NFSSTA_HASWRITEVERF
))
6355 nfsvers
= nmp
->nm_vers
;
6357 if (count
> UINT32_MAX
)
6362 nfsm_chain_null(&nmreq
);
6363 nfsm_chain_null(&nmrep
);
6365 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(NFS_VER3
));
6366 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
6367 nfsm_chain_add_64(error
, &nmreq
, offset
);
6368 nfsm_chain_add_32(error
, &nmreq
, count32
);
6369 nfsm_chain_build_done(error
, &nmreq
);
6371 error
= nfs_request2(np
, NULL
, &nmreq
, NFSPROC_COMMIT
,
6372 current_thread(), cred
, NULL
, 0, &nmrep
, &xid
, &status
);
6373 if ((lockerror
= nfs_node_lock(np
)))
6375 /* can we do anything useful with the wcc info? */
6376 nfsm_chain_get_wcc_data(error
, &nmrep
, np
, &premtime
, &wccpostattr
, &xid
);
6378 nfs_node_unlock(np
);
6381 nfsm_chain_get_64(error
, &nmrep
, newwverf
);
6383 lck_mtx_lock(&nmp
->nm_lock
);
6384 if (nmp
->nm_verf
!= newwverf
)
6385 nmp
->nm_verf
= newwverf
;
6386 if (wverf
!= newwverf
)
6387 error
= NFSERR_STALEWRITEVERF
;
6388 lck_mtx_unlock(&nmp
->nm_lock
);
6390 nfsm_chain_cleanup(&nmreq
);
6391 nfsm_chain_cleanup(&nmrep
);
6398 __unused
struct vnop_blockmap_args
/* {
6399 struct vnodeop_desc *a_desc;
6414 * fsync vnode op. Just call nfs_flush().
6419 struct vnop_fsync_args
/* {
6420 struct vnodeop_desc *a_desc;
6423 vfs_context_t a_context;
6426 return (nfs_flush(VTONFS(ap
->a_vp
), ap
->a_waitfor
, vfs_context_thread(ap
->a_context
), 0));
6431 * Do an NFS pathconf RPC.
6436 struct nfs_fsattr
*nfsap
,
6440 int error
= 0, lockerror
, status
, nfsvers
;
6441 struct nfsm_chain nmreq
, nmrep
;
6442 struct nfsmount
*nmp
= NFSTONMP(np
);
6445 if (nfs_mount_gone(nmp
))
6447 nfsvers
= nmp
->nm_vers
;
6449 nfsm_chain_null(&nmreq
);
6450 nfsm_chain_null(&nmrep
);
6452 /* fetch pathconf info from server */
6453 nfsm_chain_build_alloc_init(error
, &nmreq
, NFSX_FH(NFS_VER3
));
6454 nfsm_chain_add_fh(error
, &nmreq
, nfsvers
, np
->n_fhp
, np
->n_fhsize
);
6455 nfsm_chain_build_done(error
, &nmreq
);
6457 error
= nfs_request(np
, NULL
, &nmreq
, NFSPROC_PATHCONF
, ctx
, NULL
, &nmrep
, &xid
, &status
);
6458 if ((lockerror
= nfs_node_lock(np
)))
6460 nfsm_chain_postop_attr_update(error
, &nmrep
, np
, &xid
);
6462 nfs_node_unlock(np
);
6465 nfsm_chain_get_32(error
, &nmrep
, nfsap
->nfsa_maxlink
);
6466 nfsm_chain_get_32(error
, &nmrep
, nfsap
->nfsa_maxname
);
6467 nfsap
->nfsa_flags
&= ~(NFS_FSFLAG_NO_TRUNC
|NFS_FSFLAG_CHOWN_RESTRICTED
|NFS_FSFLAG_CASE_INSENSITIVE
|NFS_FSFLAG_CASE_PRESERVING
);
6468 nfsm_chain_get_32(error
, &nmrep
, val
);
6470 nfsap
->nfsa_flags
|= NFS_FSFLAG_NO_TRUNC
;
6471 nfsm_chain_get_32(error
, &nmrep
, val
);
6473 nfsap
->nfsa_flags
|= NFS_FSFLAG_CHOWN_RESTRICTED
;
6474 nfsm_chain_get_32(error
, &nmrep
, val
);
6476 nfsap
->nfsa_flags
|= NFS_FSFLAG_CASE_INSENSITIVE
;
6477 nfsm_chain_get_32(error
, &nmrep
, val
);
6479 nfsap
->nfsa_flags
|= NFS_FSFLAG_CASE_PRESERVING
;
6480 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXLINK
);
6481 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXNAME
);
6482 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_NO_TRUNC
);
6483 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_CHOWN_RESTRICTED
);
6484 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_INSENSITIVE
);
6485 NFS_BITMAP_SET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_PRESERVING
);
6487 nfsm_chain_cleanup(&nmreq
);
6488 nfsm_chain_cleanup(&nmrep
);
6492 /* save pathconf info for NFSv3 mount */
6494 nfs3_pathconf_cache(struct nfsmount
*nmp
, struct nfs_fsattr
*nfsap
)
6496 nmp
->nm_fsattr
.nfsa_maxlink
= nfsap
->nfsa_maxlink
;
6497 nmp
->nm_fsattr
.nfsa_maxname
= nfsap
->nfsa_maxname
;
6498 nmp
->nm_fsattr
.nfsa_flags
&= ~(NFS_FSFLAG_NO_TRUNC
|NFS_FSFLAG_CHOWN_RESTRICTED
|NFS_FSFLAG_CASE_INSENSITIVE
|NFS_FSFLAG_CASE_PRESERVING
);
6499 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_NO_TRUNC
;
6500 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_CHOWN_RESTRICTED
;
6501 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_INSENSITIVE
;
6502 nmp
->nm_fsattr
.nfsa_flags
|= nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_PRESERVING
;
6503 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXLINK
);
6504 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_MAXNAME
);
6505 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_NO_TRUNC
);
6506 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CHOWN_RESTRICTED
);
6507 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CASE_INSENSITIVE
);
6508 NFS_BITMAP_SET(nmp
->nm_fsattr
.nfsa_bitmap
, NFS_FATTR_CASE_PRESERVING
);
6509 nmp
->nm_state
|= NFSSTA_GOTPATHCONF
;
6513 * Return POSIX pathconf information applicable to nfs.
6515 * The NFS V2 protocol doesn't support this, so just return EINVAL
6521 struct vnop_pathconf_args
/* {
6522 struct vnodeop_desc *a_desc;
6526 vfs_context_t a_context;
6529 vnode_t vp
= ap
->a_vp
;
6530 nfsnode_t np
= VTONFS(vp
);
6531 struct nfsmount
*nmp
;
6532 struct nfs_fsattr nfsa
, *nfsap
;
6534 uint64_t maxFileSize
;
6538 if (nfs_mount_gone(nmp
))
6541 switch (ap
->a_name
) {
6544 case _PC_CHOWN_RESTRICTED
:
6546 case _PC_CASE_SENSITIVE
:
6547 case _PC_CASE_PRESERVING
:
6549 case _PC_FILESIZEBITS
:
6550 if (nmp
->nm_vers
== NFS_VER2
) {
6555 case _PC_XATTR_SIZE_BITS
:
6556 /* Do we support xattrs natively? */
6557 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_NAMED_ATTR
)
6559 /* No... so just return an error */
6562 /* don't bother contacting the server if we know the answer */
6566 if (nmp
->nm_vers
== NFS_VER2
)
6569 lck_mtx_lock(&nmp
->nm_lock
);
6570 if (nmp
->nm_vers
== NFS_VER3
) {
6571 if (!(nmp
->nm_state
& NFSSTA_GOTPATHCONF
)) {
6572 /* no pathconf info cached */
6573 lck_mtx_unlock(&nmp
->nm_lock
);
6574 NFS_CLEAR_ATTRIBUTES(nfsa
.nfsa_bitmap
);
6575 error
= nfs3_pathconf_rpc(np
, &nfsa
, ap
->a_context
);
6579 if (nfs_mount_gone(nmp
))
6581 lck_mtx_lock(&nmp
->nm_lock
);
6582 if (nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_HOMOGENEOUS
) {
6583 /* all files have the same pathconf info, */
6584 /* so cache a copy of the results */
6585 nfs3_pathconf_cache(nmp
, &nfsa
);
6589 nfsap
= &nmp
->nm_fsattr
;
6591 } else if (!(nmp
->nm_fsattr
.nfsa_flags
& NFS_FSFLAG_HOMOGENEOUS
)) {
6592 /* no pathconf info cached */
6593 lck_mtx_unlock(&nmp
->nm_lock
);
6594 NFS_CLEAR_ATTRIBUTES(nfsa
.nfsa_bitmap
);
6595 error
= nfs4_pathconf_rpc(np
, &nfsa
, ap
->a_context
);
6599 if (nfs_mount_gone(nmp
))
6601 lck_mtx_lock(&nmp
->nm_lock
);
6604 nfsap
= &nmp
->nm_fsattr
;
6607 switch (ap
->a_name
) {
6609 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXLINK
))
6610 *ap
->a_retval
= nfsap
->nfsa_maxlink
;
6611 else if ((nmp
->nm_vers
== NFS_VER4
) && NFS_BITMAP_ISSET(np
->n_vattr
.nva_bitmap
, NFS_FATTR_MAXLINK
))
6612 *ap
->a_retval
= np
->n_vattr
.nva_maxlink
;
6617 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXNAME
))
6618 *ap
->a_retval
= nfsap
->nfsa_maxname
;
6622 case _PC_CHOWN_RESTRICTED
:
6623 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_CHOWN_RESTRICTED
))
6624 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_CHOWN_RESTRICTED
) ? 200112 /* _POSIX_CHOWN_RESTRICTED */ : 0;
6629 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_NO_TRUNC
))
6630 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_NO_TRUNC
) ? 200112 /* _POSIX_NO_TRUNC */ : 0;
6634 case _PC_CASE_SENSITIVE
:
6635 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_INSENSITIVE
))
6636 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_INSENSITIVE
) ? 0 : 1;
6640 case _PC_CASE_PRESERVING
:
6641 if (NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_CASE_PRESERVING
))
6642 *ap
->a_retval
= (nfsap
->nfsa_flags
& NFS_FSFLAG_CASE_PRESERVING
) ? 1 : 0;
6646 case _PC_XATTR_SIZE_BITS
: /* same as file size bits if named attrs supported */
6647 case _PC_FILESIZEBITS
:
6648 if (!NFS_BITMAP_ISSET(nfsap
->nfsa_bitmap
, NFS_FATTR_MAXFILESIZE
)) {
6653 maxFileSize
= nfsap
->nfsa_maxfilesize
;
6655 if (maxFileSize
& 0xffffffff00000000ULL
) {
6659 if (maxFileSize
& 0xffff0000) {
6663 if (maxFileSize
& 0xff00) {
6667 if (maxFileSize
& 0xf0) {
6671 if (maxFileSize
& 0xc) {
6675 if (maxFileSize
& 0x2) {
6678 *ap
->a_retval
= nbits
;
6684 lck_mtx_unlock(&nmp
->nm_lock
);
6690 * Read wrapper for special devices.
6694 struct vnop_read_args
/* {
6695 struct vnodeop_desc *a_desc;
6699 vfs_context_t a_context;
6702 nfsnode_t np
= VTONFS(ap
->a_vp
);
6703 struct timespec now
;
6709 if ((error
= nfs_node_lock(np
)))
6713 np
->n_atim
.tv_sec
= now
.tv_sec
;
6714 np
->n_atim
.tv_nsec
= now
.tv_nsec
;
6715 nfs_node_unlock(np
);
6716 return (VOCALL(spec_vnodeop_p
, VOFFSET(vnop_read
), ap
));
6720 * Write wrapper for special devices.
6724 struct vnop_write_args
/* {
6725 struct vnodeop_desc *a_desc;
6729 vfs_context_t a_context;
6732 nfsnode_t np
= VTONFS(ap
->a_vp
);
6733 struct timespec now
;
6739 if ((error
= nfs_node_lock(np
)))
6743 np
->n_mtim
.tv_sec
= now
.tv_sec
;
6744 np
->n_mtim
.tv_nsec
= now
.tv_nsec
;
6745 nfs_node_unlock(np
);
6746 return (VOCALL(spec_vnodeop_p
, VOFFSET(vnop_write
), ap
));
6750 * Close wrapper for special devices.
6752 * Update the times on the nfsnode then do device close.
6756 struct vnop_close_args
/* {
6757 struct vnodeop_desc *a_desc;
6760 vfs_context_t a_context;
6763 vnode_t vp
= ap
->a_vp
;
6764 nfsnode_t np
= VTONFS(vp
);
6765 struct vnode_attr vattr
;
6769 if ((error
= nfs_node_lock(np
)))
6771 if (np
->n_flag
& (NACC
| NUPD
)) {
6773 if (!vnode_isinuse(vp
, 0) && (mp
= vnode_mount(vp
)) && !vfs_isrdonly(mp
)) {
6775 if (np
->n_flag
& NACC
) {
6776 vattr
.va_access_time
= np
->n_atim
;
6777 VATTR_SET_ACTIVE(&vattr
, va_access_time
);
6779 if (np
->n_flag
& NUPD
) {
6780 vattr
.va_modify_time
= np
->n_mtim
;
6781 VATTR_SET_ACTIVE(&vattr
, va_modify_time
);
6783 nfs_node_unlock(np
);
6784 vnode_setattr(vp
, &vattr
, ap
->a_context
);
6786 nfs_node_unlock(np
);
6789 nfs_node_unlock(np
);
6791 return (VOCALL(spec_vnodeop_p
, VOFFSET(vnop_close
), ap
));
6795 extern vnop_t
**fifo_vnodeop_p
;
6798 * Read wrapper for fifos.
6802 struct vnop_read_args
/* {
6803 struct vnodeop_desc *a_desc;
6807 vfs_context_t a_context;
6810 nfsnode_t np
= VTONFS(ap
->a_vp
);
6811 struct timespec now
;
6817 if ((error
= nfs_node_lock(np
)))
6821 np
->n_atim
.tv_sec
= now
.tv_sec
;
6822 np
->n_atim
.tv_nsec
= now
.tv_nsec
;
6823 nfs_node_unlock(np
);
6824 return (VOCALL(fifo_vnodeop_p
, VOFFSET(vnop_read
), ap
));
6828 * Write wrapper for fifos.
6832 struct vnop_write_args
/* {
6833 struct vnodeop_desc *a_desc;
6837 vfs_context_t a_context;
6840 nfsnode_t np
= VTONFS(ap
->a_vp
);
6841 struct timespec now
;
6847 if ((error
= nfs_node_lock(np
)))
6851 np
->n_mtim
.tv_sec
= now
.tv_sec
;
6852 np
->n_mtim
.tv_nsec
= now
.tv_nsec
;
6853 nfs_node_unlock(np
);
6854 return (VOCALL(fifo_vnodeop_p
, VOFFSET(vnop_write
), ap
));
6858 * Close wrapper for fifos.
6860 * Update the times on the nfsnode then do fifo close.
6864 struct vnop_close_args
/* {
6865 struct vnodeop_desc *a_desc;
6868 vfs_context_t a_context;
6871 vnode_t vp
= ap
->a_vp
;
6872 nfsnode_t np
= VTONFS(vp
);
6873 struct vnode_attr vattr
;
6874 struct timespec now
;
6878 if ((error
= nfs_node_lock(np
)))
6880 if (np
->n_flag
& (NACC
| NUPD
)) {
6882 if (np
->n_flag
& NACC
) {
6883 np
->n_atim
.tv_sec
= now
.tv_sec
;
6884 np
->n_atim
.tv_nsec
= now
.tv_nsec
;
6886 if (np
->n_flag
& NUPD
) {
6887 np
->n_mtim
.tv_sec
= now
.tv_sec
;
6888 np
->n_mtim
.tv_nsec
= now
.tv_nsec
;
6891 if (!vnode_isinuse(vp
, 1) && (mp
= vnode_mount(vp
)) && !vfs_isrdonly(mp
)) {
6893 if (np
->n_flag
& NACC
) {
6894 vattr
.va_access_time
= np
->n_atim
;
6895 VATTR_SET_ACTIVE(&vattr
, va_access_time
);
6897 if (np
->n_flag
& NUPD
) {
6898 vattr
.va_modify_time
= np
->n_mtim
;
6899 VATTR_SET_ACTIVE(&vattr
, va_modify_time
);
6901 nfs_node_unlock(np
);
6902 vnode_setattr(vp
, &vattr
, ap
->a_context
);
6904 nfs_node_unlock(np
);
6907 nfs_node_unlock(np
);
6909 return (VOCALL(fifo_vnodeop_p
, VOFFSET(vnop_close
), ap
));
6916 struct vnop_ioctl_args
/* {
6917 struct vnodeop_desc *a_desc;
6919 u_int32_t a_command;
6922 vfs_context_t a_context;
6925 vfs_context_t ctx
= ap
->a_context
;
6926 vnode_t vp
= ap
->a_vp
;
6927 struct nfsmount
*mp
= VTONMP(vp
);
6928 struct user_nfs_gss_principal gprinc
= {};
6935 switch (ap
->a_command
) {
6938 if (vnode_vfsisrdonly(vp
))
6940 error
= nfs_flush(VTONFS(vp
), MNT_WAIT
, vfs_context_thread(ctx
), 0);
6942 case NFS_IOC_DESTROY_CRED
:
6943 if (!auth_is_kerberized(mp
->nm_auth
))
6945 error
= nfs_gss_clnt_ctx_remove(mp
, vfs_context_ucred(ctx
));
6947 case NFS_IOC_SET_CRED
:
6948 case NFS_IOC_SET_CRED64
:
6949 if (!auth_is_kerberized(mp
->nm_auth
))
6951 if ((ap
->a_command
== NFS_IOC_SET_CRED
&& vfs_context_is64bit(ctx
)) ||
6952 (ap
->a_command
== NFS_IOC_SET_CRED64
&& !vfs_context_is64bit(ctx
)))
6954 if (vfs_context_is64bit(ctx
)) {
6955 gprinc
= *(struct user_nfs_gss_principal
*)ap
->a_data
;
6957 struct nfs_gss_principal
*tp
;
6958 tp
= (struct nfs_gss_principal
*)ap
->a_data
;
6959 gprinc
.princlen
= tp
->princlen
;
6960 gprinc
.nametype
= tp
->nametype
;
6961 gprinc
.principal
= CAST_USER_ADDR_T(tp
->principal
);
6963 NFS_DBG(NFS_FAC_GSS
, 7, "Enter NFS_FSCTL_SET_CRED (64-bit=%d): principal length %d name type %d usr pointer 0x%llx\n", vfs_context_is64bit(ctx
), gprinc
.princlen
, gprinc
.nametype
, (unsigned long long)gprinc
.principal
);
6964 if (gprinc
.princlen
> MAXPATHLEN
)
6967 MALLOC(p
, uint8_t *, gprinc
.princlen
+1, M_TEMP
, M_WAITOK
|M_ZERO
);
6970 error
= copyin(gprinc
.principal
, p
, gprinc
.princlen
);
6972 NFS_DBG(NFS_FAC_GSS
, 7, "NFS_FSCTL_SET_CRED could not copy in princiapl data of len %d: %d\n",
6973 gprinc
.princlen
, error
);
6977 NFS_DBG(NFS_FAC_GSS
, 7, "Seting credential to principal %s\n", p
);
6978 error
= nfs_gss_clnt_ctx_set_principal(mp
, ctx
, p
, gprinc
.princlen
, gprinc
.nametype
);
6979 NFS_DBG(NFS_FAC_GSS
, 7, "Seting credential to principal %s returned %d\n", p
, error
);
6982 case NFS_IOC_GET_CRED
:
6983 case NFS_IOC_GET_CRED64
:
6984 if (!auth_is_kerberized(mp
->nm_auth
))
6986 if ((ap
->a_command
== NFS_IOC_GET_CRED
&& vfs_context_is64bit(ctx
)) ||
6987 (ap
->a_command
== NFS_IOC_GET_CRED64
&& !vfs_context_is64bit(ctx
)))
6989 error
= nfs_gss_clnt_ctx_get_principal(mp
, ctx
, &gprinc
);
6992 if (vfs_context_is64bit(ctx
)) {
6993 struct user_nfs_gss_principal
*upp
= (struct user_nfs_gss_principal
*)ap
->a_data
;
6994 len
= upp
->princlen
;
6995 if (gprinc
.princlen
< len
)
6996 len
= gprinc
.princlen
;
6997 upp
->princlen
= gprinc
.princlen
;
6998 upp
->nametype
= gprinc
.nametype
;
6999 upp
->flags
= gprinc
.flags
;
7000 if (gprinc
.principal
)
7001 error
= copyout((void *)gprinc
.principal
, upp
->principal
, len
);
7003 upp
->principal
= USER_ADDR_NULL
;
7005 struct nfs_gss_principal
*u32pp
= (struct nfs_gss_principal
*)ap
->a_data
;
7006 len
= u32pp
->princlen
;
7007 if (gprinc
.princlen
< len
)
7008 len
= gprinc
.princlen
;
7009 u32pp
->princlen
= gprinc
.princlen
;
7010 u32pp
->nametype
= gprinc
.nametype
;
7011 u32pp
->flags
= gprinc
.flags
;
7012 if (gprinc
.principal
)
7013 error
= copyout((void *)gprinc
.principal
, u32pp
->principal
, len
);
7015 u32pp
->principal
= (user32_addr_t
)0;
7018 NFS_DBG(NFS_FAC_GSS
, 7, "NFS_FSCTL_GET_CRED could not copy out princiapl data of len %d: %d\n",
7019 gprinc
.princlen
, error
);
7021 if (gprinc
.principal
)
7022 FREE(gprinc
.principal
, M_TEMP
);
7031 __unused
struct vnop_select_args
/* {
7032 struct vnodeop_desc *a_desc;
7037 vfs_context_t a_context;
7042 * We were once bogusly seltrue() which returns 1. Is this right?
7048 * vnode OP for pagein using UPL
7050 * No buffer I/O, just RPCs straight into the mapped pages.
7054 struct vnop_pagein_args
/* {
7055 struct vnodeop_desc *a_desc;
7058 vm_offset_t a_pl_offset;
7062 vfs_context_t a_context;
7065 vnode_t vp
= ap
->a_vp
;
7066 upl_t pl
= ap
->a_pl
;
7067 size_t size
= ap
->a_size
;
7068 off_t f_offset
= ap
->a_f_offset
;
7069 vm_offset_t pl_offset
= ap
->a_pl_offset
;
7070 int flags
= ap
->a_flags
;
7073 nfsnode_t np
= VTONFS(vp
);
7074 size_t nmrsize
, iosize
, txsize
, rxsize
, retsize
;
7076 struct nfsmount
*nmp
;
7078 vm_offset_t ioaddr
, rxaddr
;
7080 char uio_buf
[ UIO_SIZEOF(1) ];
7081 int nofreeupl
= flags
& UPL_NOCOMMIT
;
7082 upl_page_info_t
*plinfo
;
7083 #define MAXPAGINGREQS 16 /* max outstanding RPCs for pagein/pageout */
7084 struct nfsreq
*req
[MAXPAGINGREQS
];
7085 int nextsend
, nextwait
;
7086 uint32_t stategenid
= 0, restart
= 0;
7089 FSDBG(322, np
, f_offset
, size
, flags
);
7090 if (pl
== (upl_t
)NULL
)
7091 panic("nfs_pagein: no upl");
7094 printf("nfs_pagein: invalid size %ld", size
);
7096 (void) ubc_upl_abort_range(pl
, pl_offset
, size
, 0);
7099 if (f_offset
< 0 || f_offset
>= (off_t
)np
->n_size
|| (f_offset
& PAGE_MASK_64
)) {
7101 ubc_upl_abort_range(pl
, pl_offset
, size
,
7102 UPL_ABORT_ERROR
| UPL_ABORT_FREE_ON_EMPTY
);
7106 thd
= vfs_context_thread(ap
->a_context
);
7107 cred
= ubc_getcred(vp
);
7108 if (!IS_VALID_CRED(cred
))
7109 cred
= vfs_context_ucred(ap
->a_context
);
7111 uio
= uio_createwithbuffer(1, f_offset
, UIO_SYSSPACE
, UIO_READ
,
7112 &uio_buf
, sizeof(uio_buf
));
7115 if (nfs_mount_gone(nmp
)) {
7117 ubc_upl_abort_range(pl
, pl_offset
, size
,
7118 UPL_ABORT_ERROR
| UPL_ABORT_FREE_ON_EMPTY
);
7121 nmrsize
= nmp
->nm_rsize
;
7123 plinfo
= ubc_upl_pageinfo(pl
);
7124 kret
= ubc_upl_map(pl
, &ioaddr
);
7125 if (kret
!= KERN_SUCCESS
)
7126 panic("nfs_vnop_pagein: ubc_upl_map() failed with (%d)", kret
);
7127 ioaddr
+= pl_offset
;
7130 if (nmp
->nm_vers
>= NFS_VER4
)
7131 stategenid
= nmp
->nm_stategenid
;
7132 txsize
= rxsize
= size
;
7133 txoffset
= f_offset
;
7136 bzero(req
, sizeof(req
));
7137 nextsend
= nextwait
= 0;
7139 if (np
->n_flag
& NREVOKE
) {
7143 /* send requests while we need to and have available slots */
7144 while ((txsize
> 0) && (req
[nextsend
] == NULL
)) {
7145 iosize
= MIN(nmrsize
, txsize
);
7146 if ((error
= nmp
->nm_funcs
->nf_read_rpc_async(np
, txoffset
, iosize
, thd
, cred
, NULL
, &req
[nextsend
]))) {
7147 req
[nextsend
] = NULL
;
7152 nextsend
= (nextsend
+ 1) % MAXPAGINGREQS
;
7154 /* wait while we need to and break out if more requests to send */
7155 while ((rxsize
> 0) && req
[nextwait
]) {
7156 iosize
= retsize
= MIN(nmrsize
, rxsize
);
7157 uio_reset(uio
, uio_offset(uio
), UIO_SYSSPACE
, UIO_READ
);
7158 uio_addiov(uio
, CAST_USER_ADDR_T(rxaddr
), iosize
);
7159 FSDBG(322, uio_offset(uio
), uio_resid(uio
), rxaddr
, rxsize
);
7161 upl_ubc_alias_set(pl
, (uintptr_t) current_thread(), (uintptr_t) 2);
7162 #endif /* UPL_DEBUG */
7163 OSAddAtomic64(1, &nfsstats
.pageins
);
7164 error
= nmp
->nm_funcs
->nf_read_rpc_async_finish(np
, req
[nextwait
], uio
, &retsize
, NULL
);
7165 req
[nextwait
] = NULL
;
7166 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
7167 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
)) {
7168 lck_mtx_lock(&nmp
->nm_lock
);
7169 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
7170 NP(np
, "nfs_vnop_pagein: error %d, initiating recovery", error
);
7171 nfs_need_recover(nmp
, error
);
7173 lck_mtx_unlock(&nmp
->nm_lock
);
7178 FSDBG(322, uio_offset(uio
), uio_resid(uio
), error
, -1);
7181 if (retsize
< iosize
) {
7182 /* Just zero fill the rest of the valid area. */
7183 int zcnt
= iosize
- retsize
;
7184 bzero((char *)rxaddr
+ retsize
, zcnt
);
7185 FSDBG(324, uio_offset(uio
), retsize
, zcnt
, rxaddr
);
7186 uio_update(uio
, zcnt
);
7193 } while (!error
&& (txsize
|| rxsize
));
7199 /* cancel any outstanding requests */
7200 while (req
[nextwait
]) {
7201 nfs_request_async_cancel(req
[nextwait
]);
7202 req
[nextwait
] = NULL
;
7203 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
7205 if (np
->n_flag
& NREVOKE
) {
7207 } else if (restart
) {
7208 if (restart
<= nfs_mount_state_max_restarts(nmp
)) { /* guard against no progress */
7209 if (error
== NFSERR_GRACE
)
7210 tsleep(&nmp
->nm_state
, (PZERO
-1), "nfsgrace", 2*hz
);
7211 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
)))
7214 NP(np
, "nfs_pagein: too many restarts, aborting");
7223 ubc_upl_abort_range(pl
, pl_offset
, size
,
7225 UPL_ABORT_FREE_ON_EMPTY
);
7227 ubc_upl_commit_range(pl
, pl_offset
, size
,
7228 UPL_COMMIT_CLEAR_DIRTY
|
7229 UPL_COMMIT_FREE_ON_EMPTY
);
7236 * the following are needed only by nfs_pageout to know how to handle errors
7237 * see nfs_pageout comments on explanation of actions.
7238 * the errors here are copied from errno.h and errors returned by servers
7239 * are expected to match the same numbers here. If not, our actions maybe
7242 char nfs_pageouterrorhandler(int);
7243 enum actiontype
{NOACTION
, DUMP
, DUMPANDLOG
, RETRY
, SEVER
};
7244 #define NFS_ELAST 88
7245 static u_char errorcount
[NFS_ELAST
+1]; /* better be zeros when initialized */
7246 static const char errortooutcome
[NFS_ELAST
+1] = {
7248 DUMP
, /* EPERM 1 Operation not permitted */
7249 DUMP
, /* ENOENT 2 No such file or directory */
7250 DUMPANDLOG
, /* ESRCH 3 No such process */
7251 RETRY
, /* EINTR 4 Interrupted system call */
7252 DUMP
, /* EIO 5 Input/output error */
7253 DUMP
, /* ENXIO 6 Device not configured */
7254 DUMPANDLOG
, /* E2BIG 7 Argument list too long */
7255 DUMPANDLOG
, /* ENOEXEC 8 Exec format error */
7256 DUMPANDLOG
, /* EBADF 9 Bad file descriptor */
7257 DUMPANDLOG
, /* ECHILD 10 No child processes */
7258 DUMPANDLOG
, /* EDEADLK 11 Resource deadlock avoided - was EAGAIN */
7259 RETRY
, /* ENOMEM 12 Cannot allocate memory */
7260 DUMP
, /* EACCES 13 Permission denied */
7261 DUMPANDLOG
, /* EFAULT 14 Bad address */
7262 DUMPANDLOG
, /* ENOTBLK 15 POSIX - Block device required */
7263 RETRY
, /* EBUSY 16 Device busy */
7264 DUMP
, /* EEXIST 17 File exists */
7265 DUMP
, /* EXDEV 18 Cross-device link */
7266 DUMP
, /* ENODEV 19 Operation not supported by device */
7267 DUMP
, /* ENOTDIR 20 Not a directory */
7268 DUMP
, /* EISDIR 21 Is a directory */
7269 DUMP
, /* EINVAL 22 Invalid argument */
7270 DUMPANDLOG
, /* ENFILE 23 Too many open files in system */
7271 DUMPANDLOG
, /* EMFILE 24 Too many open files */
7272 DUMPANDLOG
, /* ENOTTY 25 Inappropriate ioctl for device */
7273 DUMPANDLOG
, /* ETXTBSY 26 Text file busy - POSIX */
7274 DUMP
, /* EFBIG 27 File too large */
7275 DUMP
, /* ENOSPC 28 No space left on device */
7276 DUMPANDLOG
, /* ESPIPE 29 Illegal seek */
7277 DUMP
, /* EROFS 30 Read-only file system */
7278 DUMP
, /* EMLINK 31 Too many links */
7279 RETRY
, /* EPIPE 32 Broken pipe */
7281 DUMPANDLOG
, /* EDOM 33 Numerical argument out of domain */
7282 DUMPANDLOG
, /* ERANGE 34 Result too large */
7283 RETRY
, /* EAGAIN/EWOULDBLOCK 35 Resource temporarily unavailable */
7284 DUMPANDLOG
, /* EINPROGRESS 36 Operation now in progress */
7285 DUMPANDLOG
, /* EALREADY 37 Operation already in progress */
7286 /* ipc/network software -- argument errors */
7287 DUMPANDLOG
, /* ENOTSOC 38 Socket operation on non-socket */
7288 DUMPANDLOG
, /* EDESTADDRREQ 39 Destination address required */
7289 DUMPANDLOG
, /* EMSGSIZE 40 Message too long */
7290 DUMPANDLOG
, /* EPROTOTYPE 41 Protocol wrong type for socket */
7291 DUMPANDLOG
, /* ENOPROTOOPT 42 Protocol not available */
7292 DUMPANDLOG
, /* EPROTONOSUPPORT 43 Protocol not supported */
7293 DUMPANDLOG
, /* ESOCKTNOSUPPORT 44 Socket type not supported */
7294 DUMPANDLOG
, /* ENOTSUP 45 Operation not supported */
7295 DUMPANDLOG
, /* EPFNOSUPPORT 46 Protocol family not supported */
7296 DUMPANDLOG
, /* EAFNOSUPPORT 47 Address family not supported by protocol family */
7297 DUMPANDLOG
, /* EADDRINUSE 48 Address already in use */
7298 DUMPANDLOG
, /* EADDRNOTAVAIL 49 Can't assign requested address */
7299 /* ipc/network software -- operational errors */
7300 RETRY
, /* ENETDOWN 50 Network is down */
7301 RETRY
, /* ENETUNREACH 51 Network is unreachable */
7302 RETRY
, /* ENETRESET 52 Network dropped connection on reset */
7303 RETRY
, /* ECONNABORTED 53 Software caused connection abort */
7304 RETRY
, /* ECONNRESET 54 Connection reset by peer */
7305 RETRY
, /* ENOBUFS 55 No buffer space available */
7306 RETRY
, /* EISCONN 56 Socket is already connected */
7307 RETRY
, /* ENOTCONN 57 Socket is not connected */
7308 RETRY
, /* ESHUTDOWN 58 Can't send after socket shutdown */
7309 RETRY
, /* ETOOMANYREFS 59 Too many references: can't splice */
7310 RETRY
, /* ETIMEDOUT 60 Operation timed out */
7311 RETRY
, /* ECONNREFUSED 61 Connection refused */
7313 DUMPANDLOG
, /* ELOOP 62 Too many levels of symbolic links */
7314 DUMP
, /* ENAMETOOLONG 63 File name too long */
7315 RETRY
, /* EHOSTDOWN 64 Host is down */
7316 RETRY
, /* EHOSTUNREACH 65 No route to host */
7317 DUMP
, /* ENOTEMPTY 66 Directory not empty */
7319 DUMPANDLOG
, /* PROCLIM 67 Too many processes */
7320 DUMPANDLOG
, /* EUSERS 68 Too many users */
7321 DUMPANDLOG
, /* EDQUOT 69 Disc quota exceeded */
7322 /* Network File System */
7323 DUMP
, /* ESTALE 70 Stale NFS file handle */
7324 DUMP
, /* EREMOTE 71 Too many levels of remote in path */
7325 DUMPANDLOG
, /* EBADRPC 72 RPC struct is bad */
7326 DUMPANDLOG
, /* ERPCMISMATCH 73 RPC version wrong */
7327 DUMPANDLOG
, /* EPROGUNAVAIL 74 RPC prog. not avail */
7328 DUMPANDLOG
, /* EPROGMISMATCH 75 Program version wrong */
7329 DUMPANDLOG
, /* EPROCUNAVAIL 76 Bad procedure for program */
7331 DUMPANDLOG
, /* ENOLCK 77 No locks available */
7332 DUMPANDLOG
, /* ENOSYS 78 Function not implemented */
7333 DUMPANDLOG
, /* EFTYPE 79 Inappropriate file type or format */
7334 DUMPANDLOG
, /* EAUTH 80 Authentication error */
7335 DUMPANDLOG
, /* ENEEDAUTH 81 Need authenticator */
7336 /* Intelligent device errors */
7337 DUMPANDLOG
, /* EPWROFF 82 Device power is off */
7338 DUMPANDLOG
, /* EDEVERR 83 Device error, e.g. paper out */
7339 DUMPANDLOG
, /* EOVERFLOW 84 Value too large to be stored in data type */
7340 /* Program loading errors */
7341 DUMPANDLOG
, /* EBADEXEC 85 Bad executable */
7342 DUMPANDLOG
, /* EBADARCH 86 Bad CPU type in executable */
7343 DUMPANDLOG
, /* ESHLIBVERS 87 Shared library version mismatch */
7344 DUMPANDLOG
, /* EBADMACHO 88 Malformed Macho file */
7348 nfs_pageouterrorhandler(int error
)
7350 if (error
> NFS_ELAST
)
7353 return(errortooutcome
[error
]);
7358 * vnode OP for pageout using UPL
7360 * No buffer I/O, just RPCs straight from the mapped pages.
7361 * File size changes are not permitted in pageout.
7365 struct vnop_pageout_args
/* {
7366 struct vnodeop_desc *a_desc;
7369 vm_offset_t a_pl_offset;
7373 vfs_context_t a_context;
7376 vnode_t vp
= ap
->a_vp
;
7377 upl_t pl
= ap
->a_pl
;
7378 size_t size
= ap
->a_size
;
7379 off_t f_offset
= ap
->a_f_offset
;
7380 vm_offset_t pl_offset
= ap
->a_pl_offset
;
7381 int flags
= ap
->a_flags
;
7382 nfsnode_t np
= VTONFS(vp
);
7386 struct nfsmount
*nmp
= VTONMP(vp
);
7388 int error
= 0, iomode
;
7389 off_t off
, txoffset
, rxoffset
;
7390 vm_offset_t ioaddr
, txaddr
, rxaddr
;
7392 char uio_buf
[ UIO_SIZEOF(1) ];
7393 int nofreeupl
= flags
& UPL_NOCOMMIT
;
7394 size_t nmwsize
, biosize
, iosize
, pgsize
, txsize
, rxsize
, xsize
, remsize
;
7395 struct nfsreq
*req
[MAXPAGINGREQS
];
7396 int nextsend
, nextwait
, wverfset
, commit
;
7397 uint64_t wverf
, wverf2
;
7398 uint32_t stategenid
= 0, vrestart
= 0, restart
= 0, vrestarts
= 0, restarts
= 0;
7401 FSDBG(323, f_offset
, size
, pl
, pl_offset
);
7403 if (pl
== (upl_t
)NULL
)
7404 panic("nfs_pageout: no upl");
7407 printf("nfs_pageout: invalid size %ld", size
);
7409 ubc_upl_abort_range(pl
, pl_offset
, size
, 0);
7415 ubc_upl_abort(pl
, UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
);
7418 biosize
= nmp
->nm_biosize
;
7419 nmwsize
= nmp
->nm_wsize
;
7421 nfs_data_lock_noupdate(np
, NFS_DATA_LOCK_SHARED
);
7424 * Check to see whether the buffer is incore.
7425 * If incore and not busy, invalidate it from the cache.
7427 for (iosize
= 0; iosize
< size
; iosize
+= xsize
) {
7428 off
= f_offset
+ iosize
;
7429 /* need make sure we do things on block boundaries */
7430 xsize
= biosize
- (off
% biosize
);
7431 if (off
+ xsize
> f_offset
+ size
)
7432 xsize
= f_offset
+ size
- off
;
7433 lbn
= (daddr64_t
)(off
/ biosize
);
7434 lck_mtx_lock(nfs_buf_mutex
);
7435 if ((bp
= nfs_buf_incore(np
, lbn
))) {
7436 FSDBG(323, off
, bp
, bp
->nb_lflags
, bp
->nb_flags
);
7437 if (nfs_buf_acquire(bp
, NBAC_NOWAIT
, 0, 0)) {
7438 lck_mtx_unlock(nfs_buf_mutex
);
7439 nfs_data_unlock_noupdate(np
);
7440 /* no panic. just tell vm we are busy */
7442 ubc_upl_abort_range(pl
, pl_offset
, size
, 0);
7445 if (bp
->nb_dirtyend
> 0) {
7447 * if there's a dirty range in the buffer, check
7448 * to see if it extends beyond the pageout region
7450 * if the dirty region lies completely within the
7451 * pageout region, we just invalidate the buffer
7452 * because it's all being written out now anyway.
7454 * if any of the dirty region lies outside the
7455 * pageout region, we'll try to clip the dirty
7456 * region to eliminate the portion that's being
7457 * paged out. If that's not possible, because
7458 * the dirty region extends before and after the
7459 * pageout region, then we'll just return EBUSY.
7461 off_t boff
, start
, end
;
7465 /* clip end to EOF */
7466 if (end
> (off_t
)np
->n_size
)
7470 if ((bp
->nb_dirtyoff
< start
) &&
7471 (bp
->nb_dirtyend
> end
)) {
7473 * not gonna be able to clip the dirty region
7475 * But before returning the bad news, move the
7476 * buffer to the start of the delwri list and
7477 * give the list a push to try to flush the
7480 FSDBG(323, np
, bp
, 0xd00deebc, EBUSY
);
7481 nfs_buf_remfree(bp
);
7482 TAILQ_INSERT_HEAD(&nfsbufdelwri
, bp
, nb_free
);
7485 nfs_buf_delwri_push(1);
7486 lck_mtx_unlock(nfs_buf_mutex
);
7487 nfs_data_unlock_noupdate(np
);
7489 ubc_upl_abort_range(pl
, pl_offset
, size
, 0);
7492 if ((bp
->nb_dirtyoff
< start
) ||
7493 (bp
->nb_dirtyend
> end
)) {
7494 /* clip dirty region, if necessary */
7495 if (bp
->nb_dirtyoff
< start
)
7496 bp
->nb_dirtyend
= min(bp
->nb_dirtyend
, start
);
7497 if (bp
->nb_dirtyend
> end
)
7498 bp
->nb_dirtyoff
= max(bp
->nb_dirtyoff
, end
);
7499 FSDBG(323, bp
, bp
->nb_dirtyoff
, bp
->nb_dirtyend
, 0xd00dee00);
7500 /* we're leaving this block dirty */
7502 lck_mtx_unlock(nfs_buf_mutex
);
7506 nfs_buf_remfree(bp
);
7507 lck_mtx_unlock(nfs_buf_mutex
);
7508 SET(bp
->nb_flags
, NB_INVAL
);
7509 nfs_node_lock_force(np
);
7510 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
7511 CLR(bp
->nb_flags
, NB_NEEDCOMMIT
);
7512 np
->n_needcommitcnt
--;
7513 CHECK_NEEDCOMMITCNT(np
);
7515 nfs_node_unlock(np
);
7516 nfs_buf_release(bp
, 1);
7518 lck_mtx_unlock(nfs_buf_mutex
);
7522 thd
= vfs_context_thread(ap
->a_context
);
7523 cred
= ubc_getcred(vp
);
7524 if (!IS_VALID_CRED(cred
))
7525 cred
= vfs_context_ucred(ap
->a_context
);
7527 nfs_node_lock_force(np
);
7528 if (np
->n_flag
& NWRITEERR
) {
7529 error
= np
->n_error
;
7530 nfs_node_unlock(np
);
7531 nfs_data_unlock_noupdate(np
);
7533 ubc_upl_abort_range(pl
, pl_offset
, size
,
7534 UPL_ABORT_FREE_ON_EMPTY
);
7537 nfs_node_unlock(np
);
7539 if (f_offset
< 0 || f_offset
>= (off_t
)np
->n_size
||
7540 f_offset
& PAGE_MASK_64
|| size
& PAGE_MASK_64
) {
7541 nfs_data_unlock_noupdate(np
);
7543 ubc_upl_abort_range(pl
, pl_offset
, size
,
7544 UPL_ABORT_FREE_ON_EMPTY
);
7548 kret
= ubc_upl_map(pl
, &ioaddr
);
7549 if (kret
!= KERN_SUCCESS
)
7550 panic("nfs_vnop_pageout: ubc_upl_map() failed with (%d)", kret
);
7551 ioaddr
+= pl_offset
;
7553 if ((u_quad_t
)f_offset
+ size
> np
->n_size
)
7554 xsize
= np
->n_size
- f_offset
;
7558 pgsize
= round_page_64(xsize
);
7559 if ((size
> pgsize
) && !nofreeupl
)
7560 ubc_upl_abort_range(pl
, pl_offset
+ pgsize
, size
- pgsize
,
7561 UPL_ABORT_FREE_ON_EMPTY
);
7564 * check for partial page and clear the
7565 * contents past end of the file before
7566 * releasing it in the VM page cache
7568 if ((u_quad_t
)f_offset
< np
->n_size
&& (u_quad_t
)f_offset
+ size
> np
->n_size
) {
7569 size_t io
= np
->n_size
- f_offset
;
7570 bzero((caddr_t
)(ioaddr
+ io
), size
- io
);
7571 FSDBG(321, np
->n_size
, f_offset
, f_offset
+ io
, size
- io
);
7573 nfs_data_unlock_noupdate(np
);
7575 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_WRITE
,
7576 &uio_buf
, sizeof(uio_buf
));
7579 if (nmp
->nm_vers
>= NFS_VER4
)
7580 stategenid
= nmp
->nm_stategenid
;
7581 wverf
= wverf2
= wverfset
= 0;
7582 txsize
= rxsize
= xsize
;
7583 txoffset
= rxoffset
= f_offset
;
7584 txaddr
= rxaddr
= ioaddr
;
7585 commit
= NFS_WRITE_FILESYNC
;
7587 bzero(req
, sizeof(req
));
7588 nextsend
= nextwait
= 0;
7590 if (np
->n_flag
& NREVOKE
) {
7594 /* send requests while we need to and have available slots */
7595 while ((txsize
> 0) && (req
[nextsend
] == NULL
)) {
7596 iosize
= MIN(nmwsize
, txsize
);
7597 uio_reset(auio
, txoffset
, UIO_SYSSPACE
, UIO_WRITE
);
7598 uio_addiov(auio
, CAST_USER_ADDR_T(txaddr
), iosize
);
7599 FSDBG(323, uio_offset(auio
), iosize
, txaddr
, txsize
);
7600 OSAddAtomic64(1, &nfsstats
.pageouts
);
7601 nfs_node_lock_force(np
);
7603 nfs_node_unlock(np
);
7604 vnode_startwrite(vp
);
7605 iomode
= NFS_WRITE_UNSTABLE
;
7606 if ((error
= nmp
->nm_funcs
->nf_write_rpc_async(np
, auio
, iosize
, thd
, cred
, iomode
, NULL
, &req
[nextsend
]))) {
7607 req
[nextsend
] = NULL
;
7608 vnode_writedone(vp
);
7609 nfs_node_lock_force(np
);
7611 nfs_node_unlock(np
);
7617 nextsend
= (nextsend
+ 1) % MAXPAGINGREQS
;
7619 /* wait while we need to and break out if more requests to send */
7620 while ((rxsize
> 0) && req
[nextwait
]) {
7621 iosize
= remsize
= MIN(nmwsize
, rxsize
);
7622 error
= nmp
->nm_funcs
->nf_write_rpc_async_finish(np
, req
[nextwait
], &iomode
, &iosize
, &wverf2
);
7623 req
[nextwait
] = NULL
;
7624 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
7625 vnode_writedone(vp
);
7626 nfs_node_lock_force(np
);
7628 nfs_node_unlock(np
);
7629 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
)) {
7630 lck_mtx_lock(&nmp
->nm_lock
);
7631 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
7632 NP(np
, "nfs_vnop_pageout: error %d, initiating recovery", error
);
7633 nfs_need_recover(nmp
, error
);
7635 lck_mtx_unlock(&nmp
->nm_lock
);
7640 FSDBG(323, rxoffset
, rxsize
, error
, -1);
7646 } else if (wverf
!= wverf2
) {
7647 /* verifier changed, so we need to restart all the writes */
7651 /* Retain the lowest commitment level returned. */
7652 if (iomode
< commit
)
7659 /* need to try sending the remainder */
7661 uio_reset(auio
, rxoffset
, UIO_SYSSPACE
, UIO_WRITE
);
7662 uio_addiov(auio
, CAST_USER_ADDR_T(rxaddr
), remsize
);
7663 iomode
= NFS_WRITE_UNSTABLE
;
7664 error
= nfs_write_rpc2(np
, auio
, thd
, cred
, &iomode
, &wverf2
);
7665 if ((nmp
->nm_vers
>= NFS_VER4
) && nfs_mount_state_error_should_restart(error
)) {
7666 NP(np
, "nfs_vnop_pageout: restart: error %d", error
);
7667 lck_mtx_lock(&nmp
->nm_lock
);
7668 if ((error
!= NFSERR_GRACE
) && (stategenid
== nmp
->nm_stategenid
)) {
7669 NP(np
, "nfs_vnop_pageout: error %d, initiating recovery", error
);
7670 nfs_need_recover(nmp
, error
);
7672 lck_mtx_unlock(&nmp
->nm_lock
);
7677 FSDBG(323, rxoffset
, rxsize
, error
, -1);
7680 if (wverf
!= wverf2
) {
7681 /* verifier changed, so we need to restart all the writes */
7685 if (iomode
< commit
)
7694 } while (!error
&& (txsize
|| rxsize
));
7698 if (!error
&& (commit
!= NFS_WRITE_FILESYNC
)) {
7699 error
= nmp
->nm_funcs
->nf_commit_rpc(np
, f_offset
, xsize
, cred
, wverf
);
7700 if (error
== NFSERR_STALEWRITEVERF
) {
7708 /* cancel any outstanding requests */
7709 while (req
[nextwait
]) {
7710 nfs_request_async_cancel(req
[nextwait
]);
7711 req
[nextwait
] = NULL
;
7712 nextwait
= (nextwait
+ 1) % MAXPAGINGREQS
;
7713 vnode_writedone(vp
);
7714 nfs_node_lock_force(np
);
7716 nfs_node_unlock(np
);
7718 if (np
->n_flag
& NREVOKE
) {
7722 if (++vrestarts
<= 100) /* guard against no progress */
7724 NP(np
, "nfs_pageout: too many restarts, aborting");
7725 FSDBG(323, f_offset
, xsize
, ERESTART
, -1);
7728 if (restarts
<= nfs_mount_state_max_restarts(nmp
)) { /* guard against no progress */
7729 if (error
== NFSERR_GRACE
)
7730 tsleep(&nmp
->nm_state
, (PZERO
-1), "nfsgrace", 2*hz
);
7731 if (!(error
= nfs_mount_state_wait_for_recovery(nmp
)))
7734 NP(np
, "nfs_pageout: too many restarts, aborting");
7735 FSDBG(323, f_offset
, xsize
, ERESTART
, -1);
7744 * We've had several different solutions on what to do when the pageout
7745 * gets an error. If we don't handle it, and return an error to the
7746 * caller, vm, it will retry . This can end in endless looping
7747 * between vm and here doing retries of the same page. Doing a dump
7748 * back to vm, will get it out of vm's knowledge and we lose whatever
7749 * data existed. This is risky, but in some cases necessary. For
7750 * example, the initial fix here was to do that for ESTALE. In that case
7751 * the server is telling us that the file is no longer the same. We
7752 * would not want to keep paging out to that. We also saw some 151
7753 * errors from Auspex server and NFSv3 can return errors higher than
7754 * ELAST. Those along with NFS known server errors we will "dump" from
7755 * vm. Errors we don't expect to occur, we dump and log for further
7756 * analysis. Errors that could be transient, networking ones,
7757 * we let vm "retry". Lastly, errors that we retry, but may have potential
7758 * to storm the network, we "retrywithsleep". "sever" will be used in
7759 * in the future to dump all pages of object for cases like ESTALE.
7760 * All this is the basis for the states returned and first guesses on
7761 * error handling. Tweaking expected as more statistics are gathered.
7762 * Note, in the long run we may need another more robust solution to
7763 * have some kind of persistant store when the vm cannot dump nor keep
7764 * retrying as a solution, but this would be a file architectural change
7766 if (!nofreeupl
) { /* otherwise stacked file system has to handle this */
7769 char action
= nfs_pageouterrorhandler(error
);
7773 abortflags
= UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
;
7776 abortflags
= UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
;
7777 if (error
<= NFS_ELAST
) {
7778 if ((errorcount
[error
] % 100) == 0)
7779 NP(np
, "nfs_pageout: unexpected error %d. dumping vm page", error
);
7780 errorcount
[error
]++;
7784 abortflags
= UPL_ABORT_FREE_ON_EMPTY
;
7786 case SEVER
: /* not implemented */
7788 NP(np
, "nfs_pageout: action %d not expected", action
);
7792 ubc_upl_abort_range(pl
, pl_offset
, pgsize
, abortflags
);
7793 /* return error in all cases above */
7796 ubc_upl_commit_range(pl
, pl_offset
, pgsize
,
7797 UPL_COMMIT_CLEAR_DIRTY
|
7798 UPL_COMMIT_FREE_ON_EMPTY
);
7804 /* Blktooff derives file offset given a logical block number */
7807 struct vnop_blktooff_args
/* {
7808 struct vnodeop_desc *a_desc;
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_offset
= (off_t
)(ap
->a_lblkno
* biosize
);
7829 struct vnop_offtoblk_args
/* {
7830 struct vnodeop_desc *a_desc;
7833 daddr64_t *a_lblkno;
7837 vnode_t vp
= ap
->a_vp
;
7838 struct nfsmount
*nmp
= VTONMP(vp
);
7840 if (nfs_mount_gone(nmp
))
7842 biosize
= nmp
->nm_biosize
;
7844 *ap
->a_lblkno
= (daddr64_t
)(ap
->a_offset
/ biosize
);
7850 * vnode change monitoring
7854 struct vnop_monitor_args
/* {
7855 struct vnodeop_desc *a_desc;
7860 vfs_context_t a_context;
7863 nfsnode_t np
= VTONFS(ap
->a_vp
);
7864 struct nfsmount
*nmp
= VTONMP(ap
->a_vp
);
7867 if (nfs_mount_gone(nmp
))
7870 /* make sure that the vnode's monitoring status is up to date */
7871 lck_mtx_lock(&nmp
->nm_lock
);
7872 if (vnode_ismonitored(ap
->a_vp
)) {
7873 /* This vnode is currently being monitored, make sure we're tracking it. */
7874 if (np
->n_monlink
.le_next
== NFSNOLIST
) {
7875 LIST_INSERT_HEAD(&nmp
->nm_monlist
, np
, n_monlink
);
7876 nfs_mount_sock_thread_wake(nmp
);
7879 /* This vnode is no longer being monitored, make sure we're not tracking it. */
7880 /* Wait for any in-progress getattr to complete first. */
7881 while (np
->n_mflag
& NMMONSCANINPROG
) {
7882 struct timespec ts
= { 1, 0 };
7883 np
->n_mflag
|= NMMONSCANWANT
;
7884 msleep(&np
->n_mflag
, &nmp
->nm_lock
, PZERO
-1, "nfswaitmonscan", &ts
);
7886 if (np
->n_monlink
.le_next
!= NFSNOLIST
) {
7887 LIST_REMOVE(np
, n_monlink
);
7888 np
->n_monlink
.le_next
= NFSNOLIST
;
7891 lck_mtx_unlock(&nmp
->nm_lock
);
7897 * Send a vnode notification for the given events.
7900 nfs_vnode_notify(nfsnode_t np
, uint32_t events
)
7902 struct nfsmount
*nmp
= NFSTONMP(np
);
7903 struct nfs_vattr nvattr
;
7904 struct vnode_attr vattr
, *vap
= NULL
;
7908 if ((np
->n_evtstamp
== now
.tv_sec
) || !nmp
) {
7909 /* delay sending this notify */
7910 np
->n_events
|= events
;
7913 events
|= np
->n_events
;
7915 np
->n_evtstamp
= now
.tv_sec
;
7917 vfs_get_notify_attributes(&vattr
);
7918 if (!nfs_getattrcache(np
, &nvattr
, 0)) {
7922 vap
->va_flags
|= nmp
->nm_vers
> 2 ? VA_64BITOBJIDS
: 0;
7923 VATTR_RETURN(vap
, va_fsid
, vfs_statfs(nmp
->nm_mountp
)->f_fsid
.val
[0]);
7924 VATTR_RETURN(vap
, va_fileid
, nvattr
.nva_fileid
);
7925 VATTR_RETURN(vap
, va_mode
, nvattr
.nva_mode
);
7926 VATTR_RETURN(vap
, va_uid
, nvattr
.nva_uid
);
7927 VATTR_RETURN(vap
, va_gid
, nvattr
.nva_gid
);
7928 VATTR_RETURN(vap
, va_nlink
, nvattr
.nva_nlink
);
7930 vnode_notify(NFSTOV(np
), events
, vap
);