]> git.saurik.com Git - apple/xnu.git/blame - bsd/nfs/nfs_vnops.c
xnu-4570.61.1.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_vnops.c
CommitLineData
1c79356b 1/*
5ba3f43e 2 * Copyright (c) 2000-2017 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29/*
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
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.
51 *
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
62 * SUCH DAMAGE.
63 *
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 $
66 */
67
68
69/*
70 * vnode op calls for Sun NFS version 2 and 3
71 */
1c79356b
A
72#include <sys/param.h>
73#include <sys/kernel.h>
74#include <sys/systm.h>
75#include <sys/resourcevar.h>
91447636
A
76#include <sys/proc_internal.h>
77#include <sys/kauth.h>
78#include <sys/mount_internal.h>
1c79356b 79#include <sys/malloc.h>
91447636 80#include <sys/kpi_mbuf.h>
1c79356b 81#include <sys/conf.h>
91447636 82#include <sys/vnode_internal.h>
1c79356b
A
83#include <sys/dirent.h>
84#include <sys/fcntl.h>
85#include <sys/lockf.h>
91447636
A
86#include <sys/ubc_internal.h>
87#include <sys/attr.h>
88#include <sys/signalvar.h>
89#include <sys/uio_internal.h>
1c79356b 90
1c79356b
A
91#include <vfs/vfs_support.h>
92
93#include <sys/vm.h>
1c79356b 94
2d21ac55 95#include <sys/time.h>
1c79356b 96#include <kern/clock.h>
91447636 97#include <libkern/OSAtomic.h>
1c79356b
A
98
99#include <miscfs/fifofs/fifo.h>
100#include <miscfs/specfs/specdev.h>
101
102#include <nfs/rpcv2.h>
103#include <nfs/nfsproto.h>
104#include <nfs/nfs.h>
105#include <nfs/nfsnode.h>
2d21ac55 106#include <nfs/nfs_gss.h>
1c79356b 107#include <nfs/nfsmount.h>
55e303ae 108#include <nfs/nfs_lock.h>
1c79356b
A
109#include <nfs/xdr_subs.h>
110#include <nfs/nfsm_subs.h>
1c79356b
A
111
112#include <net/if.h>
113#include <netinet/in.h>
114#include <netinet/in_var.h>
b0d623f7 115
1c79356b 116#include <vm/vm_kern.h>
b0d623f7 117#include <vm/vm_pageout.h>
1c79356b 118
9bccf70c
A
119#include <kern/task.h>
120#include <kern/sched_prim.h>
1c79356b 121
39236c6e 122#define NFS_VNOP_DBG(...) NFS_DBG(NFS_FAC_VNOP, 7, ## __VA_ARGS__)
fe8ab488 123#define DEFAULT_READLINK_NOCACHE 0
39236c6e 124
1c79356b 125/*
2d21ac55 126 * NFS vnode ops
1c79356b 127 */
b0d623f7
A
128int nfs_vnop_lookup(struct vnop_lookup_args *);
129int nfsspec_vnop_read(struct vnop_read_args *);
130int nfsspec_vnop_write(struct vnop_write_args *);
131int nfsspec_vnop_close(struct vnop_close_args *);
2d21ac55 132#if FIFO
b0d623f7
A
133int nfsfifo_vnop_read(struct vnop_read_args *);
134int nfsfifo_vnop_write(struct vnop_write_args *);
135int nfsfifo_vnop_close(struct vnop_close_args *);
2d21ac55 136#endif
b0d623f7
A
137int nfs_vnop_ioctl(struct vnop_ioctl_args *);
138int nfs_vnop_select(struct vnop_select_args *);
139int nfs_vnop_setattr(struct vnop_setattr_args *);
b0d623f7 140int nfs_vnop_fsync(struct vnop_fsync_args *);
b0d623f7
A
141int nfs_vnop_rename(struct vnop_rename_args *);
142int nfs_vnop_readdir(struct vnop_readdir_args *);
143int nfs_vnop_readlink(struct vnop_readlink_args *);
144int nfs_vnop_pathconf(struct vnop_pathconf_args *);
145int nfs_vnop_pagein(struct vnop_pagein_args *);
146int nfs_vnop_pageout(struct vnop_pageout_args *);
147int nfs_vnop_blktooff(struct vnop_blktooff_args *);
148int nfs_vnop_offtoblk(struct vnop_offtoblk_args *);
149int nfs_vnop_blockmap(struct vnop_blockmap_args *);
6d2010ae 150int nfs_vnop_monitor(struct vnop_monitor_args *);
b0d623f7
A
151
152int nfs3_vnop_create(struct vnop_create_args *);
153int nfs3_vnop_mknod(struct vnop_mknod_args *);
154int nfs3_vnop_getattr(struct vnop_getattr_args *);
155int nfs3_vnop_link(struct vnop_link_args *);
156int nfs3_vnop_mkdir(struct vnop_mkdir_args *);
157int nfs3_vnop_rmdir(struct vnop_rmdir_args *);
158int nfs3_vnop_symlink(struct vnop_symlink_args *);
2d21ac55 159
91447636 160vnop_t **nfsv2_vnodeop_p;
1c79356b 161static struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
91447636 162 { &vnop_default_desc, (vnop_t *)vn_default_error },
2d21ac55
A
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 */
6d2010ae
A
166 { &vnop_open_desc, (vnop_t *)nfs_vnop_open }, /* open */
167 { &vnop_close_desc, (vnop_t *)nfs_vnop_close }, /* close */
2d21ac55
A
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 */
6d2010ae 177 { &vnop_mnomap_desc, (vnop_t *)nfs_vnop_mnomap }, /* mnomap */
2d21ac55
A
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 */
91447636 189 { &vnop_strategy_desc, (vnop_t *)err_strategy }, /* strategy */
2d21ac55 190 { &vnop_pathconf_desc, (vnop_t *)nfs_vnop_pathconf }, /* pathconf */
6d2010ae 191 { &vnop_advlock_desc, (vnop_t *)nfs_vnop_advlock }, /* advlock */
91447636 192 { &vnop_bwrite_desc, (vnop_t *)err_bwrite }, /* bwrite */
2d21ac55
A
193 { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein }, /* Pagein */
194 { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout }, /* Pageout */
91447636 195 { &vnop_copyfile_desc, (vnop_t *)err_copyfile }, /* Copyfile */
2d21ac55
A
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 */
6d2010ae 199 { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */
1c79356b
A
200 { NULL, NULL }
201};
202struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
203 { &nfsv2_vnodeop_p, nfsv2_vnodeop_entries };
2d21ac55
A
204
205vnop_t **nfsv4_vnodeop_p;
206static 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 */
6d2010ae
A
211 { &vnop_open_desc, (vnop_t *)nfs_vnop_open }, /* open */
212 { &vnop_close_desc, (vnop_t *)nfs_vnop_close }, /* close */
2d21ac55
A
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 */
6d2010ae 216 { &vnop_read_desc, (vnop_t *)nfs_vnop_read }, /* read */
2d21ac55
A
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 */
6d2010ae
A
221 { &vnop_mmap_desc, (vnop_t *)nfs_vnop_mmap }, /* mmap */
222 { &vnop_mnomap_desc, (vnop_t *)nfs_vnop_mnomap }, /* mnomap */
2d21ac55
A
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 */
6d2010ae 236 { &vnop_advlock_desc, (vnop_t *)nfs_vnop_advlock }, /* advlock */
2d21ac55
A
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 */
6d2010ae
A
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 */
248#if NAMEDSTREAMS
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 */
252#endif
253 { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */
2d21ac55
A
254 { NULL, NULL }
255};
256struct vnodeopv_desc nfsv4_vnodeop_opv_desc =
257 { &nfsv4_vnodeop_p, nfsv4_vnodeop_entries };
1c79356b
A
258
259/*
260 * Special device vnode ops
261 */
91447636 262vnop_t **spec_nfsv2nodeop_p;
1c79356b 263static struct vnodeopv_entry_desc spec_nfsv2nodeop_entries[] = {
91447636
A
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 */
2d21ac55
A
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 */
91447636
A
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 */
2d21ac55 278 { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */
91447636
A
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 */
2d21ac55
A
287 { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */
288 { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */
91447636
A
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 */
2d21ac55
A
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 */
6d2010ae 298 { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */
1c79356b
A
299 { NULL, NULL }
300};
301struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
302 { &spec_nfsv2nodeop_p, spec_nfsv2nodeop_entries };
2d21ac55
A
303vnop_t **spec_nfsv4nodeop_p;
304static 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 */
6d2010ae
A
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 */
343#if NAMEDSTREAMS
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 */
347#endif
348 { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */
2d21ac55
A
349 { NULL, NULL }
350};
351struct vnodeopv_desc spec_nfsv4nodeop_opv_desc =
352 { &spec_nfsv4nodeop_p, spec_nfsv4nodeop_entries };
1c79356b 353
2d21ac55 354#if FIFO
91447636 355vnop_t **fifo_nfsv2nodeop_p;
1c79356b 356static struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries[] = {
91447636
A
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 */
2d21ac55
A
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 */
91447636
A
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 */
2d21ac55 371 { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync }, /* fsync */
91447636
A
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 */
2d21ac55
A
380 { &vnop_inactive_desc, (vnop_t *)nfs_vnop_inactive }, /* inactive */
381 { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim }, /* reclaim */
91447636
A
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 */
2d21ac55
A
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 */
6d2010ae 391 { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */
1c79356b
A
392 { NULL, NULL }
393};
394struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
395 { &fifo_nfsv2nodeop_p, fifo_nfsv2nodeop_entries };
0b4e3aa0 396
2d21ac55
A
397vnop_t **fifo_nfsv4nodeop_p;
398static 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 */
6d2010ae
A
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 */
437#if NAMEDSTREAMS
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 */
441#endif
442 { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor }, /* monitor */
2d21ac55 443 { NULL, NULL }
0b4e3aa0 444};
2d21ac55
A
445struct vnodeopv_desc fifo_nfsv4nodeop_opv_desc =
446 { &fifo_nfsv4nodeop_p, fifo_nfsv4nodeop_entries };
447#endif /* FIFO */
0b4e3aa0 448
b0d623f7 449int nfs_sillyrename(nfsnode_t,nfsnode_t,struct componentname *,vfs_context_t);
fe8ab488
A
450int nfs_getattr_internal(nfsnode_t, struct nfs_vattr *, vfs_context_t, int);
451int nfs_refresh_fh(nfsnode_t, vfs_context_t);
2d21ac55
A
452
453/*
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.
6d2010ae 456 * The index of the next slot to use is kept in the last entry of the n_access array.
2d21ac55
A
457 */
458int
6d2010ae 459nfs_node_access_slot(nfsnode_t np, uid_t uid, int add)
0b4e3aa0 460{
2d21ac55
A
461 int slot;
462
463 for (slot=0; slot < NFS_ACCESS_CACHE_SIZE; slot++)
6d2010ae 464 if (np->n_accessuid[slot] == uid)
2d21ac55
A
465 break;
466 if (slot == NFS_ACCESS_CACHE_SIZE) {
467 if (!add)
468 return (-1);
6d2010ae
A
469 slot = np->n_access[NFS_ACCESS_CACHE_SIZE];
470 np->n_access[NFS_ACCESS_CACHE_SIZE] = (slot + 1) % NFS_ACCESS_CACHE_SIZE;
2d21ac55
A
471 }
472 return (slot);
0b4e3aa0 473}
1c79356b 474
2d21ac55 475int
fe8ab488 476nfs3_access_rpc(nfsnode_t np, u_int32_t *access, int rpcflags, vfs_context_t ctx)
1c79356b 477{
b0d623f7 478 int error = 0, lockerror = ENOENT, status, slot;
6d2010ae 479 uint32_t access_result = 0;
fa4905b1 480 u_int64_t xid;
2d21ac55 481 struct nfsm_chain nmreq, nmrep;
813fb2f6 482 struct nfsmount *nmp;
55e303ae 483 struct timeval now;
2d21ac55
A
484 uid_t uid;
485
486 nfsm_chain_null(&nmreq);
487 nfsm_chain_null(&nmrep);
488
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);
6d2010ae 491 nfsm_chain_add_32(error, &nmreq, *access);
2d21ac55
A
492 nfsm_chain_build_done(error, &nmreq);
493 nfsmout_if(error);
fe8ab488
A
494 error = nfs_request2(np, NULL, &nmreq, NFSPROC_ACCESS,
495 vfs_context_thread(ctx), vfs_context_ucred(ctx),
496 NULL, rpcflags, &nmrep, &xid, &status);
b0d623f7
A
497 if ((lockerror = nfs_node_lock(np)))
498 error = lockerror;
2d21ac55
A
499 nfsm_chain_postop_attr_update(error, &nmrep, np, &xid);
500 if (!error)
501 error = status;
6d2010ae 502 nfsm_chain_get_32(error, &nmrep, access_result);
2d21ac55 503 nfsmout_if(error);
0b4e3aa0 504
813fb2f6
A
505 /* XXXab do we really need mount here, also why are we doing access cache management here? */
506 nmp = NFSTONMP(np);
507 if (nfs_mount_gone(nmp)) {
508 error = ENXIO;
509 }
510 nfsmout_if(error);
511
512 if (auth_is_kerberized(np->n_auth) || auth_is_kerberized(nmp->nm_auth)) {
513 uid = nfs_cred_getasid2uid(vfs_context_ucred(ctx));
514 } else {
515 uid = kauth_cred_getuid(vfs_context_ucred(ctx));
516 }
6d2010ae
A
517 slot = nfs_node_access_slot(np, uid, 1);
518 np->n_accessuid[slot] = uid;
2d21ac55 519 microuptime(&now);
6d2010ae
A
520 np->n_accessstamp[slot] = now.tv_sec;
521 np->n_access[slot] = access_result;
2d21ac55
A
522
523 /*
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.
529 */
6d2010ae
A
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];
2d21ac55 538nfsmout:
b0d623f7
A
539 if (!lockerror)
540 nfs_node_unlock(np);
2d21ac55
A
541 nfsm_chain_cleanup(&nmreq);
542 nfsm_chain_cleanup(&nmrep);
543 return (error);
1c79356b
A
544}
545
546/*
2d21ac55
A
547 * NFS access vnode op.
548 * For NFS version 2, just return ok. File accesses may fail later.
6d2010ae
A
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.
1c79356b 551 */
2d21ac55
A
552int
553nfs_vnop_access(
91447636
A
554 struct vnop_access_args /* {
555 struct vnodeop_desc *a_desc;
556 vnode_t a_vp;
2d21ac55 557 int a_action;
91447636 558 vfs_context_t a_context;
2d21ac55 559 } */ *ap)
1c79356b 560{
2d21ac55 561 vfs_context_t ctx = ap->a_context;
91447636 562 vnode_t vp = ap->a_vp;
fe8ab488 563 int error = 0, slot, dorpc, rpcflags = 0;
6d2010ae 564 u_int32_t access, waccess;
2d21ac55
A
565 nfsnode_t np = VTONFS(vp);
566 struct nfsmount *nmp;
567 int nfsvers;
55e303ae 568 struct timeval now;
2d21ac55
A
569 uid_t uid;
570
571 nmp = VTONMP(vp);
fe8ab488 572 if (nfs_mount_gone(nmp))
2d21ac55
A
573 return (ENXIO);
574 nfsvers = nmp->nm_vers;
575
576 if (nfsvers == NFS_VER2) {
577 if ((ap->a_action & KAUTH_VNODE_WRITE_RIGHTS) &&
578 vfs_isrdonly(vnode_mount(vp)))
579 return (EROFS);
580 return (0);
581 }
1c79356b
A
582
583 /*
2d21ac55 584 * For NFS v3, do an access rpc, otherwise you are stuck emulating
1c79356b
A
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
589 * in the cache.
590 */
91447636 591
2d21ac55
A
592 /*
593 * Convert KAUTH primitives to NFS access rights.
594 */
6d2010ae 595 access = 0;
2d21ac55
A
596 if (vnode_isdir(vp)) {
597 /* directory */
598 if (ap->a_action &
599 (KAUTH_VNODE_LIST_DIRECTORY |
600 KAUTH_VNODE_READ_EXTATTRIBUTES))
6d2010ae 601 access |= NFS_ACCESS_READ;
2d21ac55 602 if (ap->a_action & KAUTH_VNODE_SEARCH)
6d2010ae 603 access |= NFS_ACCESS_LOOKUP;
2d21ac55
A
604 if (ap->a_action &
605 (KAUTH_VNODE_ADD_FILE |
606 KAUTH_VNODE_ADD_SUBDIRECTORY))
6d2010ae 607 access |= NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
2d21ac55 608 if (ap->a_action & KAUTH_VNODE_DELETE_CHILD)
6d2010ae 609 access |= NFS_ACCESS_MODIFY;
91447636 610 } else {
2d21ac55
A
611 /* file */
612 if (ap->a_action &
613 (KAUTH_VNODE_READ_DATA |
614 KAUTH_VNODE_READ_EXTATTRIBUTES))
6d2010ae 615 access |= NFS_ACCESS_READ;
2d21ac55 616 if (ap->a_action & KAUTH_VNODE_WRITE_DATA)
6d2010ae 617 access |= NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
2d21ac55 618 if (ap->a_action & KAUTH_VNODE_APPEND_DATA)
6d2010ae 619 access |= NFS_ACCESS_EXTEND;
2d21ac55 620 if (ap->a_action & KAUTH_VNODE_EXECUTE)
6d2010ae 621 access |= NFS_ACCESS_EXECUTE;
2d21ac55
A
622 }
623 /* common */
624 if (ap->a_action & KAUTH_VNODE_DELETE)
6d2010ae 625 access |= NFS_ACCESS_DELETE;
2d21ac55
A
626 if (ap->a_action &
627 (KAUTH_VNODE_WRITE_ATTRIBUTES |
628 KAUTH_VNODE_WRITE_EXTATTRIBUTES |
629 KAUTH_VNODE_WRITE_SECURITY))
6d2010ae 630 access |= NFS_ACCESS_MODIFY;
2d21ac55
A
631 /* XXX this is pretty dubious */
632 if (ap->a_action & KAUTH_VNODE_CHANGE_OWNER)
6d2010ae 633 access |= NFS_ACCESS_MODIFY;
2d21ac55
A
634
635 /* if caching, always ask for every right */
636 if (nfs_access_cache_timeout > 0) {
6d2010ae 637 waccess = NFS_ACCESS_READ | NFS_ACCESS_MODIFY |
2d21ac55
A
638 NFS_ACCESS_EXTEND | NFS_ACCESS_EXECUTE |
639 NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
640 } else {
6d2010ae 641 waccess = access;
2d21ac55
A
642 }
643
b0d623f7 644 if ((error = nfs_node_lock(np)))
2d21ac55
A
645 return (error);
646
647 /*
648 * Does our cached result allow us to give a definite yes to
649 * this request?
650 */
3e170ce0
A
651 if (auth_is_kerberized(np->n_auth) || auth_is_kerberized(nmp->nm_auth))
652 uid = nfs_cred_getasid2uid(vfs_context_ucred(ctx));
653 else
654 uid = kauth_cred_getuid(vfs_context_ucred(ctx));
6d2010ae 655 slot = nfs_node_access_slot(np, uid, 0);
2d21ac55 656 dorpc = 1;
6d2010ae 657 if (access == 0) {
b0d623f7
A
658 /* not asking for any rights understood by NFS, so don't bother doing an RPC */
659 /* OSAddAtomic(1, &nfsstats.accesscache_hits); */
660 dorpc = 0;
6d2010ae
A
661 waccess = 0;
662 } else if (NACCESSVALID(np, slot)) {
fe8ab488
A
663 microuptime(&now);
664 if (((now.tv_sec < (np->n_accessstamp[slot] + nfs_access_cache_timeout)) &&
665 ((np->n_access[slot] & access) == access)) || nfs_use_cache(nmp)) {
b0d623f7 666 /* OSAddAtomic(1, &nfsstats.accesscache_hits); */
fe8ab488
A
667 dorpc = 0;
668 waccess = np->n_access[slot];
1c79356b 669 }
fa4905b1 670 }
b0d623f7 671 nfs_node_unlock(np);
2d21ac55
A
672 if (dorpc) {
673 /* Either a no, or a don't know. Go to the wire. */
b0d623f7 674 /* OSAddAtomic(1, &nfsstats.accesscache_misses); */
fe8ab488
A
675
676 /*
677 * Allow an access call to timeout if we have it cached
678 * so we won't hang if the server isn't responding.
679 */
680 if (NACCESSVALID(np, slot))
681 rpcflags |= R_SOFT;
682
683 error = nmp->nm_funcs->nf_access_rpc(np, &waccess, rpcflags, ctx);
684
685 /*
686 * If the server didn't respond return the cached access.
687 */
688 if ((error == ETIMEDOUT) && (rpcflags & R_SOFT)) {
689 error = 0;
690 waccess = np->n_access[slot];
691 }
2d21ac55 692 }
6d2010ae 693 if (!error && ((waccess & access) != access))
2d21ac55 694 error = EACCES;
91447636 695
0b4e3aa0 696 return (error);
1c79356b
A
697}
698
6d2010ae 699
1c79356b 700/*
2d21ac55 701 * NFS open vnode op
6d2010ae
A
702 *
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.
1c79356b 706 */
2d21ac55 707int
6d2010ae 708nfs_vnop_open(
91447636
A
709 struct vnop_open_args /* {
710 struct vnodeop_desc *a_desc;
711 vnode_t a_vp;
712 int a_mode;
713 vfs_context_t a_context;
2d21ac55 714 } */ *ap)
1c79356b 715{
2d21ac55 716 vfs_context_t ctx = ap->a_context;
91447636 717 vnode_t vp = ap->a_vp;
2d21ac55
A
718 nfsnode_t np = VTONFS(vp);
719 struct nfsmount *nmp;
6d2010ae
A
720 int error, accessMode, denyMode, opened = 0;
721 struct nfs_open_owner *noop = NULL;
722 struct nfs_open_file *nofp = NULL;
91447636 723 enum vtype vtype;
6d2010ae
A
724
725 if (!(ap->a_mode & (FREAD|FWRITE)))
726 return (EINVAL);
2d21ac55
A
727
728 nmp = VTONMP(vp);
fe8ab488 729 if (nfs_mount_gone(nmp))
2d21ac55 730 return (ENXIO);
6d2010ae
A
731 if (np->n_flag & NREVOKE)
732 return (EIO);
1c79356b 733
91447636 734 vtype = vnode_vtype(vp);
2d21ac55 735 if ((vtype != VREG) && (vtype != VDIR) && (vtype != VLNK))
1c79356b 736 return (EACCES);
6d2010ae
A
737
738 /* First, check if we need to update/invalidate */
2d21ac55
A
739 if (ISSET(np->n_flag, NUPDATESIZE))
740 nfs_data_update_size(np, 0);
b0d623f7 741 if ((error = nfs_node_lock(np)))
2d21ac55 742 return (error);
91447636
A
743 if (np->n_flag & NNEEDINVALIDATE) {
744 np->n_flag &= ~NNEEDINVALIDATE;
b0d623f7
A
745 if (vtype == VDIR)
746 nfs_invaldir(np);
747 nfs_node_unlock(np);
2d21ac55 748 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1);
b0d623f7 749 if ((error = nfs_node_lock(np)))
2d21ac55 750 return (error);
91447636 751 }
6d2010ae 752 if (vtype == VREG)
b0d623f7 753 np->n_lastrahead = -1;
91447636 754 if (np->n_flag & NMODIFIED) {
91447636 755 if (vtype == VDIR)
b0d623f7
A
756 nfs_invaldir(np);
757 nfs_node_unlock(np);
758 if ((error = nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1)))
91447636 759 return (error);
1c79356b 760 } else {
b0d623f7 761 nfs_node_unlock(np);
1c79356b 762 }
6d2010ae 763
b0d623f7 764 /* nfs_getattr() will check changed and purge caches */
6d2010ae
A
765 if ((error = nfs_getattr(np, NULL, ctx, NGA_UNCACHED)))
766 return (error);
767
768 if (vtype != VREG) {
769 /* Just mark that it was opened */
770 lck_mtx_lock(&np->n_openlock);
771 np->n_openrefcnt++;
772 lck_mtx_unlock(&np->n_openlock);
773 return (0);
774 }
775
776 /* mode contains some combination of: FREAD, FWRITE, O_SHLOCK, O_EXLOCK */
777 accessMode = 0;
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;
786 else
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;
790
791 noop = nfs_open_owner_find(nmp, vfs_context_ucred(ctx), 1);
792 if (!noop)
793 return (ENOMEM);
794
795restart:
796 error = nfs_mount_state_in_use_start(nmp, vfs_context_thread(ctx));
797 if (error) {
798 nfs_open_owner_rele(noop);
799 return (error);
800 }
801 if (np->n_flag & NREVOKE) {
802 error = EIO;
803 nfs_mount_state_in_use_end(nmp, 0);
804 nfs_open_owner_rele(noop);
805 return (error);
806 }
807
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));
811 error = EIO;
812 }
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));
816 nofp = NULL;
817 if (!error)
818 goto restart;
819 }
820 if (!error)
821 error = nfs_open_file_set_busy(nofp, vfs_context_thread(ctx));
822 if (error) {
823 nofp = NULL;
824 goto out;
825 }
826
827 if (nmp->nm_vers < NFS_VER4) {
828 /*
829 * NFS v2/v3 opens are always allowed - so just add it.
830 */
831 nfs_open_file_add_open(nofp, accessMode, denyMode, 0);
832 goto out;
833 }
834
835 /*
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.
838 */
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;
845 } else {
846 if (!opened)
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())) {
850 /*
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.
858 */
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;
862 error = 0;
863 }
864 if (error)
865 goto out;
866 opened = 1;
867 /*
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.
871 */
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);
875 if (error)
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)) {
878 error = 0;
879 nofp->nof_flags &= ~NFS_OPEN_FILE_CREATE;
880 }
881 }
882 }
883
884out:
885 if (nofp)
886 nfs_open_file_clear_busy(nofp);
887 if (nfs_mount_state_in_use_end(nmp, error)) {
888 nofp = NULL;
889 goto restart;
890 }
891 if (error)
892 NP(np, "nfs_vnop_open: error %d, %d", error, kauth_cred_getuid(noop->noo_cred));
893 if (noop)
894 nfs_open_owner_rele(noop);
316670eb
A
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;
901 nmp->nm_writers++;
902 lck_mtx_unlock(&nmp->nm_lock);
903 }
904
6d2010ae 905 return (error);
1c79356b
A
906}
907
316670eb
A
908static uint32_t
909nfs_no_of_open_file_writers(nfsnode_t np)
910{
911 uint32_t writers = 0;
912 struct nfs_open_file *nofp;
913
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;
919 }
920
921 return (writers);
922}
6d2010ae 923
1c79356b 924/*
2d21ac55 925 * NFS close vnode op
6d2010ae 926 *
1c79356b
A
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
929 * two reasons:
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.)
941 *
942 * The current code does the following:
943 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
6d2010ae
A
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
1c79356b 946 */
2d21ac55 947int
6d2010ae 948nfs_vnop_close(
91447636 949 struct vnop_close_args /* {
1c79356b 950 struct vnodeop_desc *a_desc;
91447636
A
951 vnode_t a_vp;
952 int a_fflag;
953 vfs_context_t a_context;
2d21ac55 954 } */ *ap)
1c79356b 955{
2d21ac55 956 vfs_context_t ctx = ap->a_context;
91447636 957 vnode_t vp = ap->a_vp;
2d21ac55 958 nfsnode_t np = VTONFS(vp);
55e303ae 959 struct nfsmount *nmp;
6d2010ae
A
960 int error = 0, error1, nfsvers;
961 int fflag = ap->a_fflag;
962 enum vtype vtype;
963 int accessMode, denyMode;
964 struct nfs_open_owner *noop = NULL;
965 struct nfs_open_file *nofp = NULL;
1c79356b 966
2d21ac55
A
967 nmp = VTONMP(vp);
968 if (!nmp)
969 return (ENXIO);
970 nfsvers = nmp->nm_vers;
6d2010ae 971 vtype = vnode_vtype(vp);
91447636 972
6d2010ae 973 /* First, check if we need to update/flush/invalidate */
2d21ac55
A
974 if (ISSET(np->n_flag, NUPDATESIZE))
975 nfs_data_update_size(np, 0);
6d2010ae 976 nfs_node_lock_force(np);
2d21ac55 977 if (np->n_flag & NNEEDINVALIDATE) {
91447636 978 np->n_flag &= ~NNEEDINVALIDATE;
b0d623f7 979 nfs_node_unlock(np);
2d21ac55 980 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1);
6d2010ae 981 nfs_node_lock_force(np);
2d21ac55 982 }
6d2010ae
A
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 */
b0d623f7 985 nfs_node_unlock(np);
2d21ac55
A
986 if (nfsvers != NFS_VER2)
987 error = nfs_flush(np, MNT_WAIT, vfs_context_thread(ctx), 0);
988 else
989 error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1);
b0d623f7 990 nfs_node_lock_force(np);
91447636 991 NATTRINVALIDATE(np);
2d21ac55
A
992 }
993 if (np->n_flag & NWRITEERR) {
1c79356b
A
994 np->n_flag &= ~NWRITEERR;
995 error = np->n_error;
1c79356b 996 }
b0d623f7 997 nfs_node_unlock(np);
6d2010ae
A
998
999 if (vtype != VREG) {
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");
1005 error = EINVAL;
1006 }
1007 } else if (fflag & (FREAD|FWRITE)) {
1008 np->n_openrefcnt--;
1009 } else {
1010 /* No FREAD/FWRITE set - probably the final close */
1011 np->n_openrefcnt = 0;
1012 }
1013 lck_mtx_unlock(&np->n_openlock);
1014 return (error);
1015 }
1016 error1 = error;
1017
1018 /* fflag should contain some combination of: FREAD, FWRITE, FHASLOCK */
1019 accessMode = 0;
1020 if (fflag & FREAD)
1021 accessMode |= NFS_OPEN_SHARE_ACCESS_READ;
1022 if (fflag & FWRITE)
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;
1029// else
1030// denyMode = NFS_OPEN_SHARE_DENY_NONE;
ebb1b9f4 1031#if 0 // Not yet
6d2010ae
A
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;
1039 else
1040 denyMode = NFS_OPEN_SHARE_DENY_NONE;
1041 } else {
1042 denyMode = NFS_OPEN_SHARE_DENY_NONE;
1043 }
ebb1b9f4 1044#else
6d2010ae
A
1045 // XXX don't do deny modes just yet (and never do it for !v4)
1046 denyMode = NFS_OPEN_SHARE_DENY_NONE;
ebb1b9f4 1047#endif
6d2010ae
A
1048
1049 if (!accessMode) {
1050 /*
1051 * No mode given to close?
1052 * Guess this is the final close.
1053 * We should unlock all locks and close all opens.
1054 */
316670eb 1055 uint32_t writers;
6d2010ae 1056 mount_t mp = vnode_mount(vp);
fe8ab488 1057 int force = (!mp || vfs_isforce(mp));
316670eb
A
1058
1059 writers = nfs_no_of_open_file_writers(np);
6d2010ae 1060 nfs_release_open_state_for_node(np, force);
316670eb
A
1061 if (writers) {
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;
1068 } else {
1069 nmp->nm_writers -= writers;
1070 }
1071 lck_mtx_unlock(&nmp->nm_lock);
1072 }
1073
6d2010ae 1074 return (error);
316670eb
A
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");
1079 } else {
1080 nmp->nm_writers--;
1081 }
1082 lck_mtx_unlock(&nmp->nm_lock);
6d2010ae 1083 }
316670eb 1084
6d2010ae
A
1085
1086 noop = nfs_open_owner_find(nmp, vfs_context_ucred(ctx), 0);
1087 if (!noop) {
1088 // printf("nfs_vnop_close: can't get open owner!\n");
1089 return (EIO);
1090 }
1091
1092restart:
1093 error = nfs_mount_state_in_use_start(nmp, NULL);
1094 if (error) {
1095 nfs_open_owner_rele(noop);
1096 return (error);
1097 }
1098
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);
1103 nofp = NULL;
1104 if (!error)
1105 goto restart;
1106 }
1107 if (error) {
1108 NP(np, "nfs_vnop_close: no open file for owner, error %d, %d", error, kauth_cred_getuid(noop->noo_cred));
1109 error = EBADF;
1110 goto out;
1111 }
1112 error = nfs_open_file_set_busy(nofp, NULL);
1113 if (error) {
1114 nofp = NULL;
1115 goto out;
1116 }
1117
1118 error = nfs_close(np, nofp, accessMode, denyMode, ctx);
1119 if (error)
1120 NP(np, "nfs_vnop_close: close error %d, %d", error, kauth_cred_getuid(noop->noo_cred));
1121
1122out:
1123 if (nofp)
1124 nfs_open_file_clear_busy(nofp);
1125 if (nfs_mount_state_in_use_end(nmp, error)) {
1126 nofp = NULL;
1127 goto restart;
1128 }
1129 if (!error)
1130 error = error1;
1131 if (error)
1132 NP(np, "nfs_vnop_close: error %d, %d", error, kauth_cred_getuid(noop->noo_cred));
1133 if (noop)
1134 nfs_open_owner_rele(noop);
1c79356b
A
1135 return (error);
1136}
1137
6d2010ae
A
1138/*
1139 * nfs_close(): common function that does all the heavy lifting of file closure
1140 *
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.
1143 */
1144int
1145nfs_close(
1146 nfsnode_t np,
1147 struct nfs_open_file *nofp,
1148 uint32_t accessMode,
1149 uint32_t denyMode,
1150 vfs_context_t ctx)
1151{
1152 struct nfs_lock_owner *nlop;
1153 int error = 0, changed = 0, delegated = 0, closed = 0, downgrade = 0;
1154 uint32_t newAccessMode, newDenyMode;
316670eb 1155
6d2010ae
A
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));
1161
1162 /*
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.
1165 */
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;
1181 }
1182
1183 nfs_open_file_remove_open_find(nofp, accessMode, denyMode, &newAccessMode, &newDenyMode, &delegated);
1184 if ((newAccessMode != nofp->nof_access) || (newDenyMode != nofp->nof_deny))
1185 changed = 1;
1186 else
1187 changed = 0;
1188
1189 if (NFSTONMP(np)->nm_vers < NFS_VER4) /* NFS v2/v3 closes simply need to remove the open. */
1190 goto v3close;
1191
1192 if ((newAccessMode == 0) || (nofp->nof_opencnt == 1)) {
1193 /*
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.
1196 */
1197 nfs_wait_bufs(np);
1198 closed = 1;
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) {
1202 /*
1203 * Hmm... the server says we have locks we need to release first
1204 * Find the lock owner and try to unlock everything.
1205 */
1206 nlop = nfs_lock_owner_find(np, vfs_context_proc(ctx), 0);
1207 if (nlop) {
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);
1211 }
1212 error = nfs4_close_rpc(np, nofp, vfs_context_thread(ctx), vfs_context_ucred(ctx), 0);
1213 }
1214 } else if (changed) {
1215 /*
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.
1218 */
1219 if (!delegated && !(nofp->nof_flags & NFS_OPEN_FILE_LOST)) {
1220 downgrade = 1;
1221 /*
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.
1224 */
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);
1234 }
1235 }
1236
1237 if (error) {
1238 NP(np, "nfs_close: error %d, %d", error, kauth_cred_getuid(nofp->nof_owner->noo_cred));
1239 return (error);
1240 }
1241
1242v3close:
1243 if (!downgrade)
1244 nfs_open_file_remove_open(nofp, accessMode, denyMode);
1245
1246 if (closed) {
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 */
1275 }
1276 if (nofp->nof_flags & NFS_OPEN_FILE_LOST) {
1277 error = EIO;
1278 NP(np, "nfs_close: LOST%s, %d", !nofp->nof_opencnt ? " (last)" : "",
1279 kauth_cred_getuid(nofp->nof_owner->noo_cred));
1280 }
316670eb 1281
6d2010ae
A
1282 return (error);
1283}
1284
1285
91447636 1286int
2d21ac55
A
1287nfs3_getattr_rpc(
1288 nfsnode_t np,
91447636
A
1289 mount_t mp,
1290 u_char *fhp,
2d21ac55 1291 size_t fhsize,
6d2010ae 1292 int flags,
2d21ac55 1293 vfs_context_t ctx,
91447636
A
1294 struct nfs_vattr *nvap,
1295 u_int64_t *xidp)
1296{
2d21ac55 1297 struct nfsmount *nmp = mp ? VFSTONFS(mp) : NFSTONMP(np);
6d2010ae 1298 int error = 0, status, nfsvers, rpcflags = 0;
2d21ac55 1299 struct nfsm_chain nmreq, nmrep;
91447636 1300
fe8ab488 1301 if (nfs_mount_gone(nmp))
2d21ac55
A
1302 return (ENXIO);
1303 nfsvers = nmp->nm_vers;
1304
6d2010ae
A
1305 if (flags & NGA_MONITOR) /* vnode monitor requests should be soft */
1306 rpcflags = R_RECOVER;
1307
fe8ab488
A
1308 if (flags & NGA_SOFT) /* Return ETIMEDOUT if server not responding */
1309 rpcflags |= R_SOFT;
1310
2d21ac55
A
1311 nfsm_chain_null(&nmreq);
1312 nfsm_chain_null(&nmrep);
1313
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);
1319 nfsmout_if(error);
6d2010ae
A
1320 error = nfs_request2(np, mp, &nmreq, NFSPROC_GETATTR,
1321 vfs_context_thread(ctx), vfs_context_ucred(ctx),
1322 NULL, rpcflags, &nmrep, xidp, &status);
2d21ac55
A
1323 if (!error)
1324 error = status;
1325 nfsmout_if(error);
1326 error = nfs_parsefattr(&nmrep, nfsvers, nvap);
1327nfsmout:
1328 nfsm_chain_cleanup(&nmreq);
1329 nfsm_chain_cleanup(&nmrep);
91447636
A
1330 return (error);
1331}
1332
fe8ab488
A
1333/*
1334 * nfs_refresh_fh will attempt to update the file handle for the node.
1335 *
1336 * It only does this for symbolic links and regular files that are not currently opened.
1337 *
1338 * On Success returns 0 and the nodes file handle is updated, or ESTALE on failure.
1339 */
1340int
1341nfs_refresh_fh(nfsnode_t np, vfs_context_t ctx)
1342{
1343 vnode_t dvp, vp = NFSTOV(np);
1344 nfsnode_t dnp;
1345 const char *v_name = vnode_getname(vp);
1346 char *name;
1347 int namelen, fhsize, refreshed;
1348 int error, wanted = 0;
1349 uint8_t *fhp;
1350 struct timespec ts = {2, 0};
1351
1352 NFS_VNOP_DBG("vnode is %d\n", vnode_vtype(vp));
1353
1354 dvp = vnode_parent(vp);
1355 if ((vnode_vtype(vp) != VREG && vnode_vtype(vp) != VLNK) ||
1356 v_name == NULL || *v_name == '\0' || dvp == NULL) {
1357 if (v_name != NULL)
1358 vnode_putname(v_name);
1359 return (ESTALE);
1360 }
1361 dnp = VTONFS(dvp);
1362
1363 namelen = strlen(v_name);
1364 MALLOC(name, char *, namelen + 1, M_TEMP, M_WAITOK);
1365 if (name == NULL) {
1366 vnode_putname(v_name);
1367 return (ESTALE);
1368 }
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);
1372
1373 /* Allocate the maximum size file handle */
1374 MALLOC(fhp, uint8_t *, NFS4_FHSIZE, M_TEMP, M_WAITOK);
1375 if (fhp == NULL) {
1376 FREE(name, M_TEMP);
1377 return (ESTALE);
1378 }
1379
1380 if ((error = nfs_node_lock(np))) {
1381 FREE(name, M_TEMP);
1382 FREE(fhp, M_TEMP);
1383 return (ESTALE);
1384 }
1385
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)))
1393 break;
1394 }
1395 refreshed = error ? 0 : !NFS_CMPFH(np, fhp, fhsize);
1396 SET(np->n_flag, NREFRESH);
1397 nfs_node_unlock(np);
1398
1399 NFS_VNOP_DBG("error = %d, refreshed = %d\n", error, refreshed);
1400 if (error || refreshed)
1401 goto nfsmout;
1402
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)) {
1406 int cnt = 0;
1407 struct nfs_open_file *ofp;
1408
1409 TAILQ_FOREACH(ofp, &np->n_opens, nof_link) {
1410 cnt += ofp->nof_opencnt;
1411 }
1412 if (cnt) {
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));
1417 error = ESTALE;
1418 goto nfsmout;
1419 }
1420 }
1421 lck_mtx_unlock(&np->n_openlock);
1422 /*
1423 * Since the FH is currently stale we should not be able to
1424 * establish any open state until the FH is refreshed.
1425 */
1426
1427 error = nfs_node_lock(np);
1428 nfsmout_if(error);
1429 /*
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
1434 * invalidate.
1435 */
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);
1440 if (error)
1441 NFS_VNOP_DBG("nfs_vinvalbuf returned %d\n", error);
1442 nfsmout_if(error);
1443 } else {
1444 nfs_node_unlock(np);
1445 }
1446
1447 NFS_VNOP_DBG("Looking up %s\n", name);
1448 error = nfs_lookitup(dnp, name, namelen, ctx, &np);
1449 if (error)
1450 NFS_VNOP_DBG("nfs_lookitup returned %d\n", error);
1451
1452nfsmout:
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);
1457 if (wanted)
1458 wakeup(np);
1459
1460 if (error == 0)
1461 NFS_VNOP_DBG("%s refreshed file handle\n", name);
1462
1463 FREE(name, M_TEMP);
1464 FREE(fhp, M_TEMP);
1465
1466 return (error ? ESTALE : 0);
1467}
2d21ac55 1468
91447636 1469int
6d2010ae 1470nfs_getattr(nfsnode_t np, struct nfs_vattr *nvap, vfs_context_t ctx, int flags)
fe8ab488
A
1471{
1472 int error;
1473
1474retry:
1475 error = nfs_getattr_internal(np, nvap, ctx, flags);
1476 if (error == ESTALE) {
1477 error = nfs_refresh_fh(np, ctx);
1478 if (!error)
1479 goto retry;
1480 }
1481 return (error);
1482}
1483
1484int
1485nfs_getattr_internal(nfsnode_t np, struct nfs_vattr *nvap, vfs_context_t ctx, int flags)
1c79356b 1486{
2d21ac55 1487 struct nfsmount *nmp;
b0d623f7 1488 int error = 0, nfsvers, inprogset = 0, wanted = 0, avoidfloods;
6d2010ae 1489 struct nfs_vattr nvattr;
b0d623f7 1490 struct timespec ts = { 2, 0 };
fa4905b1 1491 u_int64_t xid;
91447636
A
1492
1493 FSDBG_TOP(513, np->n_size, np, np->n_vattr.nva_size, np->n_flag);
1494
fe8ab488
A
1495 nmp = NFSTONMP(np);
1496
1497 if (nfs_mount_gone(nmp))
b0d623f7
A
1498 return (ENXIO);
1499 nfsvers = nmp->nm_vers;
1500
6d2010ae
A
1501 if (!nvap)
1502 nvap = &nvattr;
1503 NVATTR_INIT(nvap);
1504
2d21ac55
A
1505 /* Update local times for special files. */
1506 if (np->n_flag & (NACC | NUPD)) {
b0d623f7 1507 nfs_node_lock_force(np);
1c79356b 1508 np->n_flag |= NCHG;
b0d623f7 1509 nfs_node_unlock(np);
2d21ac55
A
1510 }
1511 /* Update size, if necessary */
b0d623f7 1512 if (ISSET(np->n_flag, NUPDATESIZE))
2d21ac55
A
1513 nfs_data_update_size(np, 0);
1514
b0d623f7
A
1515 error = nfs_node_lock(np);
1516 nfsmout_if(error);
6d2010ae
A
1517 if (!(flags & (NGA_UNCACHED|NGA_MONITOR)) || ((nfsvers >= NFS_VER4) && (np->n_openflags & N_DELEG_MASK))) {
1518 /*
1519 * Use the cache or wait for any getattr in progress if:
1520 * - it's a cached request, or
fe8ab488
A
1521 * - we have a delegation, or
1522 * - the server isn't responding
6d2010ae 1523 */
b0d623f7 1524 while (1) {
6d2010ae 1525 error = nfs_getattrcache(np, nvap, flags);
b0d623f7
A
1526 if (!error || (error != ENOENT)) {
1527 nfs_node_unlock(np);
1528 goto nfsmout;
1529 }
6d2010ae 1530 error = 0;
b0d623f7
A
1531 if (!ISSET(np->n_flag, NGETATTRINPROG))
1532 break;
6d2010ae
A
1533 if (flags & NGA_MONITOR) {
1534 /* no need to wait if a request is pending */
1535 error = EINPROGRESS;
1536 nfs_node_unlock(np);
1537 goto nfsmout;
1538 }
b0d623f7
A
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);
1543 goto nfsmout;
1544 }
1545 }
1546 SET(np->n_flag, NGETATTRINPROG);
1547 inprogset = 1;
1548 } else if (!ISSET(np->n_flag, NGETATTRINPROG)) {
1549 SET(np->n_flag, NGETATTRINPROG);
1550 inprogset = 1;
6d2010ae
A
1551 } else if (flags & NGA_MONITOR) {
1552 /* no need to make a request if one is pending */
1553 error = EINPROGRESS;
b0d623f7
A
1554 }
1555 nfs_node_unlock(np);
55e303ae 1556
2d21ac55 1557 nmp = NFSTONMP(np);
fe8ab488 1558 if (nfs_mount_gone(nmp))
2d21ac55 1559 error = ENXIO;
6d2010ae 1560 if (error)
2d21ac55 1561 goto nfsmout;
fa4905b1 1562
fe8ab488
A
1563 /*
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.
1567 */
1568 if (NATTRVALID(np) && nfs_use_cache(nmp))
1569 flags |= NGA_SOFT;
1570
91447636 1571 /*
6d2010ae
A
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.
91447636
A
1574 * But don't bother if we aren't caching access info or if the
1575 * attributes returned wouldn't be cached.
1576 */
6d2010ae 1577 if (!(flags & NGA_ACL) && (nfsvers != NFS_VER2) && nfs_access_for_getattr && (nfs_access_cache_timeout > 0)) {
2d21ac55 1578 if (nfs_attrcachetimeout(np) > 0) {
b0d623f7 1579 /* OSAddAtomic(1, &nfsstats.accesscache_misses); */
6d2010ae 1580 u_int32_t access = NFS_ACCESS_ALL;
fe8ab488
A
1581 int rpcflags = 0;
1582
1583 /* Return cached attrs if server doesn't respond */
1584 if (flags & NGA_SOFT)
1585 rpcflags |= R_SOFT;
1586
1587 error = nmp->nm_funcs->nf_access_rpc(np, &access, rpcflags, ctx);
1588
1589 if (error == ETIMEDOUT)
1590 goto returncached;
1591
2d21ac55
A
1592 if (error)
1593 goto nfsmout;
b0d623f7 1594 nfs_node_lock_force(np);
6d2010ae 1595 error = nfs_getattrcache(np, nvap, flags);
b0d623f7
A
1596 nfs_node_unlock(np);
1597 if (!error || (error != ENOENT))
2d21ac55 1598 goto nfsmout;
b0d623f7 1599 /* Well, that didn't work... just do a getattr... */
2d21ac55
A
1600 error = 0;
1601 }
1c79356b 1602 }
b0d623f7 1603
fa4905b1 1604 avoidfloods = 0;
fe8ab488 1605
fa4905b1 1606tryagain:
6d2010ae 1607 error = nmp->nm_funcs->nf_getattr_rpc(np, NULL, np->n_fhp, np->n_fhsize, flags, ctx, nvap, &xid);
b0d623f7
A
1608 if (!error) {
1609 nfs_node_lock_force(np);
1610 error = nfs_loadattrcache(np, nvap, &xid, 0);
1611 nfs_node_unlock(np);
1612 }
fe8ab488
A
1613
1614 /*
1615 * If the server didn't respond, return cached attributes.
1616 */
1617returncached:
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);
1623 goto nfsmout;
1624 }
1625 nfs_node_unlock(np);
1626 }
2d21ac55 1627 nfsmout_if(error);
fe8ab488 1628
2d21ac55
A
1629 if (!xid) { /* out-of-order rpc - attributes were dropped */
1630 FSDBG(513, -1, np, np->n_xid >> 32, np->n_xid);
b0d623f7 1631 if (avoidfloods++ < 20)
2d21ac55 1632 goto tryagain;
b0d623f7
A
1633 /* avoidfloods>1 is bizarre. at 20 pull the plug */
1634 /* just return the last attributes we got */
1635 }
1636nfsmout:
1637 nfs_node_lock_force(np);
1638 if (inprogset) {
1639 wanted = ISSET(np->n_flag, NGETATTRWANT);
1640 CLR(np->n_flag, (NGETATTRINPROG | NGETATTRWANT));
91447636 1641 }
b0d623f7
A
1642 if (!error) {
1643 /* check if the node changed on us */
2d21ac55
A
1644 vnode_t vp = NFSTOV(np);
1645 enum vtype vtype = vnode_vtype(vp);
b0d623f7
A
1646 if ((vtype == VDIR) && NFS_CHANGED_NC(nfsvers, np, nvap)) {
1647 FSDBG(513, -1, np, 0, np);
1648 np->n_flag &= ~NNEGNCENTRIES;
1649 cache_purge(vp);
1650 np->n_ncgen++;
1651 NFS_CHANGED_UPDATE_NC(nfsvers, np, nvap);
39236c6e
A
1652 NFS_VNOP_DBG("Purge directory 0x%llx\n",
1653 (uint64_t)VM_KERNEL_ADDRPERM(vp));
2d21ac55 1654 }
b0d623f7
A
1655 if (NFS_CHANGED(nfsvers, np, nvap)) {
1656 FSDBG(513, -1, np, -1, np);
39236c6e
A
1657 if (vtype == VDIR) {
1658 NFS_VNOP_DBG("Invalidate directory 0x%llx\n",
1659 (uint64_t)VM_KERNEL_ADDRPERM(vp));
b0d623f7 1660 nfs_invaldir(np);
39236c6e 1661 }
b0d623f7
A
1662 nfs_node_unlock(np);
1663 if (wanted)
1664 wakeup(np);
2d21ac55 1665 error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1);
fa4905b1 1666 FSDBG(513, -1, np, -2, error);
55e303ae 1667 if (!error) {
b0d623f7 1668 nfs_node_lock_force(np);
2d21ac55 1669 NFS_CHANGED_UPDATE(nfsvers, np, nvap);
b0d623f7 1670 nfs_node_unlock(np);
55e303ae 1671 }
2d21ac55 1672 } else {
b0d623f7
A
1673 nfs_node_unlock(np);
1674 if (wanted)
1675 wakeup(np);
1c79356b 1676 }
b0d623f7
A
1677 } else {
1678 nfs_node_unlock(np);
1679 if (wanted)
1680 wakeup(np);
1c79356b 1681 }
6d2010ae
A
1682
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;
1691 }
1692 }
2d21ac55 1693 FSDBG_BOT(513, np->n_size, error, np->n_vattr.nva_size, np->n_flag);
91447636
A
1694 return (error);
1695}
1696
2d21ac55
A
1697/*
1698 * NFS getattr call from vfs.
1699 */
39236c6e
A
1700
1701/*
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.
1707 */
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 | \
1720 VNODE_ATTR_va_type)
1721
b0d623f7 1722int
2d21ac55 1723nfs3_vnop_getattr(
91447636
A
1724 struct vnop_getattr_args /* {
1725 struct vnodeop_desc *a_desc;
1726 vnode_t a_vp;
1727 struct vnode_attr *a_vap;
1728 vfs_context_t a_context;
1729 } */ *ap)
1730{
1731 int error;
1732 struct nfs_vattr nva;
1733 struct vnode_attr *vap = ap->a_vap;
5ba3f43e 1734 struct nfsmount *nmp;
2d21ac55 1735 dev_t rdev;
91447636 1736
39236c6e
A
1737 /*
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.
1740 */
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)
1744 return (0);
1745
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");
b0d623f7 1750 error = nfs_getattr(VTONFS(ap->a_vp), &nva, ap->a_context, NGA_CACHED);
91447636
A
1751 if (error)
1752 return (error);
1753
1754 /* copy nva to *a_vap */
5ba3f43e
A
1755 nmp = VTONMP(ap->a_vp);
1756 vap->va_flags |= nmp ? (nmp->nm_vers > 2 ? VA_64BITOBJIDS : 0) : 0;
2d21ac55
A
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);
2d21ac55
A
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);
91447636 1776
2d21ac55 1777 // VATTR_RETURN(vap, va_encoding, 0xffff /* kTextEncodingUnknown */);
1c79356b
A
1778 return (error);
1779}
1780
1781/*
2d21ac55 1782 * NFS setattr call.
1c79356b 1783 */
b0d623f7 1784int
2d21ac55 1785nfs_vnop_setattr(
91447636 1786 struct vnop_setattr_args /* {
1c79356b 1787 struct vnodeop_desc *a_desc;
91447636
A
1788 vnode_t a_vp;
1789 struct vnode_attr *a_vap;
1790 vfs_context_t a_context;
2d21ac55 1791 } */ *ap)
1c79356b 1792{
2d21ac55 1793 vfs_context_t ctx = ap->a_context;
91447636 1794 vnode_t vp = ap->a_vp;
2d21ac55 1795 nfsnode_t np = VTONFS(vp);
0c530ab8 1796 struct nfsmount *nmp;
91447636 1797 struct vnode_attr *vap = ap->a_vap;
1c79356b 1798 int error = 0;
6d2010ae
A
1799 int biosize, nfsvers, namedattrs;
1800 u_quad_t origsize, vapsize;
2d21ac55
A
1801 struct nfs_dulookup dul;
1802 nfsnode_t dnp = NULL;
813fb2f6 1803 int dul_in_progress = 0;
2d21ac55
A
1804 vnode_t dvp = NULL;
1805 const char *vname = NULL;
b0d623f7
A
1806 struct nfs_open_owner *noop = NULL;
1807 struct nfs_open_file *nofp = NULL;
2d21ac55
A
1808
1809 nmp = VTONMP(vp);
fe8ab488 1810 if (nfs_mount_gone(nmp))
0c530ab8 1811 return (ENXIO);
2d21ac55 1812 nfsvers = nmp->nm_vers;
6d2010ae 1813 namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR);
0c530ab8 1814 biosize = nmp->nm_biosize;
fa4905b1 1815
91447636 1816 /* Disallow write attempts if the filesystem is mounted read-only. */
2d21ac55 1817 if (vnode_vfsisrdonly(vp))
1c79356b 1818 return (EROFS);
91447636 1819
2d21ac55 1820 origsize = np->n_size;
91447636 1821 if (VATTR_IS_ACTIVE(vap, va_data_size)) {
2d21ac55
A
1822 switch (vnode_vtype(vp)) {
1823 case VDIR:
1824 return (EISDIR);
1825 case VCHR:
1826 case VBLK:
1827 case VSOCK:
1828 case VFIFO:
91447636
A
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) &&
2d21ac55 1833 !VATTR_IS_ACTIVE(vap, va_gid)) {
1c79356b 1834 return (0);
2d21ac55 1835 }
91447636 1836 VATTR_CLEAR_ACTIVE(vap, va_data_size);
2d21ac55
A
1837 break;
1838 default:
1c79356b
A
1839 /*
1840 * Disallow write attempts if the filesystem is
1841 * mounted read-only.
1842 */
91447636 1843 if (vnode_vfsisrdonly(vp))
1c79356b 1844 return (EROFS);
91447636
A
1845 FSDBG_TOP(512, np->n_size, vap->va_data_size,
1846 np->n_vattr.nva_size, np->n_flag);
2d21ac55 1847 /* clear NNEEDINVALIDATE, if set */
b0d623f7 1848 if ((error = nfs_node_lock(np)))
2d21ac55
A
1849 return (error);
1850 if (np->n_flag & NNEEDINVALIDATE)
1851 np->n_flag &= ~NNEEDINVALIDATE;
b0d623f7 1852 nfs_node_unlock(np);
2d21ac55
A
1853 /* flush everything */
1854 error = nfs_vinvalbuf(vp, (vap->va_data_size ? V_SAVE : 0) , ctx, 1);
1855 if (error) {
6d2010ae 1856 NP(np, "nfs_setattr: nfs_vinvalbuf %d", error);
2d21ac55
A
1857 FSDBG_BOT(512, np->n_size, vap->va_data_size, np->n_vattr.nva_size, -1);
1858 return (error);
1859 }
b0d623f7
A
1860 if (nfsvers >= NFS_VER4) {
1861 /* setting file size requires having the file open for write access */
6d2010ae
A
1862 if (np->n_flag & NREVOKE)
1863 return (EIO);
b0d623f7
A
1864 noop = nfs_open_owner_find(nmp, vfs_context_ucred(ctx), 1);
1865 if (!noop)
1866 return (ENOMEM);
6d2010ae
A
1867restart:
1868 error = nfs_mount_state_in_use_start(nmp, vfs_context_thread(ctx));
1869 if (error)
1870 return (error);
1871 if (np->n_flag & NREVOKE) {
1872 nfs_mount_state_in_use_end(nmp, 0);
1873 return (EIO);
1874 }
b0d623f7
A
1875 error = nfs_open_file_find(np, noop, &nofp, 0, 0, 1);
1876 if (!error && (nofp->nof_flags & NFS_OPEN_FILE_LOST))
1877 error = EIO;
1878 if (!error && (nofp->nof_flags & NFS_OPEN_FILE_REOPEN)) {
6d2010ae
A
1879 nfs_mount_state_in_use_end(nmp, 0);
1880 error = nfs4_reopen(nofp, vfs_context_thread(ctx));
b0d623f7 1881 nofp = NULL;
6d2010ae
A
1882 if (!error)
1883 goto restart;
b0d623f7 1884 }
6d2010ae
A
1885 if (!error)
1886 error = nfs_open_file_set_busy(nofp, vfs_context_thread(ctx));
b0d623f7
A
1887 if (error) {
1888 nfs_open_owner_rele(noop);
1889 return (error);
1890 }
1891 if (!(nofp->nof_access & NFS_OPEN_SHARE_ACCESS_WRITE)) {
1892 /* we don't have the file open for write access, so open it */
b0d623f7
A
1893 error = nfs4_open(np, nofp, NFS_OPEN_SHARE_ACCESS_WRITE, NFS_OPEN_SHARE_DENY_NONE, ctx);
1894 if (!error)
1895 nofp->nof_flags |= NFS_OPEN_FILE_SETATTR;
1896 if (nfs_mount_state_error_should_restart(error)) {
1897 nfs_open_file_clear_busy(nofp);
1898 nofp = NULL;
6d2010ae
A
1899 if (nfs_mount_state_in_use_end(nmp, error))
1900 goto restart;
b0d623f7 1901 }
b0d623f7
A
1902 }
1903 }
1904 nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE);
2d21ac55 1905 if (np->n_size > vap->va_data_size) { /* shrinking? */
91447636 1906 daddr64_t obn, bn;
0c530ab8 1907 int neweofoff, mustwrite;
55e303ae 1908 struct nfsbuf *bp;
fa4905b1 1909
fa4905b1 1910 obn = (np->n_size - 1) / biosize;
2d21ac55 1911 bn = vap->va_data_size / biosize;
91447636 1912 for ( ; obn >= bn; obn--) {
2d21ac55 1913 if (!nfs_buf_is_incore(np, obn))
91447636 1914 continue;
2d21ac55 1915 error = nfs_buf_get(np, obn, biosize, NULL, NBLK_READ, &bp);
91447636 1916 if (error)
55e303ae 1917 continue;
91447636
A
1918 if (obn != bn) {
1919 FSDBG(512, bp, bp->nb_flags, 0, obn);
1920 SET(bp->nb_flags, NB_INVAL);
1921 nfs_buf_release(bp, 1);
1922 continue;
1923 }
1924 mustwrite = 0;
1925 neweofoff = vap->va_data_size - NBOFF(bp);
1926 /* check for any dirty data before the new EOF */
2d21ac55 1927 if ((bp->nb_dirtyend > 0) && (bp->nb_dirtyoff < neweofoff)) {
55e303ae 1928 /* clip dirty range to EOF */
2d21ac55 1929 if (bp->nb_dirtyend > neweofoff) {
91447636 1930 bp->nb_dirtyend = neweofoff;
2d21ac55
A
1931 if (bp->nb_dirtyoff >= bp->nb_dirtyend)
1932 bp->nb_dirtyoff = bp->nb_dirtyend = 0;
1933 }
1934 if ((bp->nb_dirtyend > 0) && (bp->nb_dirtyoff < neweofoff))
1935 mustwrite++;
91447636
A
1936 }
1937 bp->nb_dirty &= (1 << round_page_32(neweofoff)/PAGE_SIZE) - 1;
1938 if (bp->nb_dirty)
55e303ae 1939 mustwrite++;
91447636
A
1940 if (!mustwrite) {
1941 FSDBG(512, bp, bp->nb_flags, 0, obn);
1942 SET(bp->nb_flags, NB_INVAL);
1943 nfs_buf_release(bp, 1);
1944 continue;
1945 }
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);
0c530ab8 1951 if (!IS_VALID_CRED(bp->nb_wcred)) {
2d21ac55 1952 kauth_cred_t cred = vfs_context_ucred(ctx);
91447636
A
1953 kauth_cred_ref(cred);
1954 bp->nb_wcred = cred;
1955 }
1956 error = nfs_buf_write(bp);
1957 // Note: bp has been released
1958 if (error) {
1959 FSDBG(512, bp, 0xd00dee, 0xbad, error);
b0d623f7 1960 nfs_node_lock_force(np);
91447636
A
1961 np->n_error = error;
1962 np->n_flag |= NWRITEERR;
55e303ae 1963 /*
91447636
A
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)
55e303ae 1969 */
91447636 1970 NATTRINVALIDATE(np);
b0d623f7 1971 nfs_node_unlock(np);
2d21ac55
A
1972 nfs_data_unlock(np);
1973 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1);
b0d623f7 1974 nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE);
91447636 1975 error = 0;
fa4905b1 1976 }
91447636 1977 }
1c79356b 1978 }
2d21ac55
A
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;
91447636 1982 np->n_size = np->n_vattr.nva_size = vap->va_data_size;
b0d623f7 1983 nfs_node_lock_force(np);
2d21ac55 1984 CLR(np->n_flag, NUPDATESIZE);
b0d623f7 1985 nfs_node_unlock(np);
2d21ac55
A
1986 FSDBG(512, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001);
1987 }
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)) {
b0d623f7 1991 if ((error = nfs_node_lock(np)))
55e303ae 1992 return (error);
2d21ac55 1993 if ((np->n_flag & NMODIFIED) && (vnode_vtype(vp) == VREG)) {
b0d623f7 1994 nfs_node_unlock(np);
2d21ac55
A
1995 error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1);
1996 if (error == EINTR)
1997 return (error);
1998 } else {
b0d623f7 1999 nfs_node_unlock(np);
2d21ac55 2000 }
55e303ae 2001 }
6d2010ae
A
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);
b0d623f7 2006 nfs_node_unlock(np);
6d2010ae
A
2007 if (!namedattrs) {
2008 dvp = vnode_getparent(vp);
2009 vname = vnode_getname(vp);
2010 dnp = (dvp && vname) ? VTONFS(dvp) : NULL;
2011 if (dnp) {
813fb2f6
A
2012 if (nfs_node_set_busy(dnp, vfs_context_thread(ctx))) {
2013 vnode_put(dvp);
2014 vnode_putname(vname);
2015 } else {
2016 nfs_dulookup_init(&dul, dnp, vname, strlen(vname), ctx);
2017 nfs_dulookup_start(&dul, dnp, ctx);
2018 dul_in_progress = 1;
6d2010ae 2019 }
813fb2f6
A
2020 } else {
2021 if (dvp)
2022 vnode_put(dvp);
2023 if (vname)
2024 vnode_putname(vname);
2d21ac55 2025 }
2d21ac55
A
2026 }
2027 }
2028
6d2010ae 2029 if (!error)
b0d623f7
A
2030 error = nmp->nm_funcs->nf_setattr_rpc(np, vap, ctx);
2031
813fb2f6
A
2032 if (dul_in_progress) {
2033 nfs_dulookup_finish(&dul, dnp, ctx);
2034 nfs_node_clear_busy(dnp);
2035 vnode_put(dvp);
2036 vnode_putname(vname);
91447636 2037 }
1c79356b 2038
2d21ac55
A
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)) {
6d2010ae
A
2041 if (error && (origsize != np->n_size) &&
2042 ((nfsvers < NFS_VER4) || !nfs_mount_state_error_should_restart(error))) {
2d21ac55 2043 /* make every effort to resync file size w/ server... */
6d2010ae 2044 /* (don't bother if we'll be restarting the operation) */
2d21ac55
A
2045 int err; /* preserve "error" for return */
2046 np->n_size = np->n_vattr.nva_size = origsize;
b0d623f7 2047 nfs_node_lock_force(np);
2d21ac55 2048 CLR(np->n_flag, NUPDATESIZE);
b0d623f7 2049 nfs_node_unlock(np);
2d21ac55
A
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 */
6d2010ae 2052 vapsize = vap->va_data_size;
2d21ac55 2053 vap->va_data_size = origsize;
b0d623f7 2054 err = nmp->nm_funcs->nf_setattr_rpc(np, vap, ctx);
2d21ac55 2055 if (err)
6d2010ae
A
2056 NP(np, "nfs_vnop_setattr: nfs%d_setattr_rpc %d %d", nfsvers, error, err);
2057 vap->va_data_size = vapsize;
2d21ac55 2058 }
b0d623f7
A
2059 nfs_node_lock_force(np);
2060 /*
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.
2065 */
2066 if (ISSET(np->n_flag, NUPDATESIZE)) {
2067 CLR(np->n_flag, NUPDATESIZE);
2068 NATTRINVALIDATE(np);
2069 nfs_node_unlock(np);
6d2010ae 2070 nfs_getattr(np, NULL, ctx, NGA_UNCACHED);
b0d623f7
A
2071 } else {
2072 nfs_node_unlock(np);
2073 }
2d21ac55 2074 nfs_data_unlock(np);
b0d623f7 2075 if (nfsvers >= NFS_VER4) {
6d2010ae
A
2076 if (nofp) {
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);
2081 if (err)
2082 NP(np, "nfs_vnop_setattr: close error: %d", err);
2083 nofp->nof_flags &= ~NFS_OPEN_FILE_SETATTR;
b0d623f7 2084 }
b0d623f7 2085 nfs_open_file_clear_busy(nofp);
6d2010ae 2086 nofp = NULL;
b0d623f7 2087 }
6d2010ae
A
2088 if (nfs_mount_state_in_use_end(nmp, error))
2089 goto restart;
b0d623f7
A
2090 nfs_open_owner_rele(noop);
2091 }
1c79356b
A
2092 }
2093 return (error);
2094}
2095
2096/*
2d21ac55 2097 * Do an NFS setattr RPC.
1c79356b 2098 */
2d21ac55
A
2099int
2100nfs3_setattr_rpc(
2101 nfsnode_t np,
2102 struct vnode_attr *vap,
b0d623f7 2103 vfs_context_t ctx)
1c79356b 2104{
2d21ac55
A
2105 struct nfsmount *nmp = NFSTONMP(np);
2106 int error = 0, lockerror = ENOENT, status, wccpostattr = 0, nfsvers;
b0d623f7 2107 u_int64_t xid, nextxid;
2d21ac55 2108 struct nfsm_chain nmreq, nmrep;
55e303ae 2109
fe8ab488 2110 if (nfs_mount_gone(nmp))
55e303ae 2111 return (ENXIO);
2d21ac55 2112 nfsvers = nmp->nm_vers;
1c79356b 2113
2d21ac55
A
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);
2120
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 */
2125 else
2126 return (ENOTSUP); /* return ENOTSUP for chflags(2) */
2127 }
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 */
2131 }
2132
2133 nfsm_chain_null(&nmreq);
2134 nfsm_chain_null(&nmrep);
2135
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) {
91447636 2140 if (VATTR_IS_ACTIVE(vap, va_mode)) {
2d21ac55
A
2141 nfsm_chain_add_32(error, &nmreq, TRUE);
2142 nfsm_chain_add_32(error, &nmreq, vap->va_mode);
1c79356b 2143 } else {
2d21ac55 2144 nfsm_chain_add_32(error, &nmreq, FALSE);
1c79356b 2145 }
91447636 2146 if (VATTR_IS_ACTIVE(vap, va_uid)) {
2d21ac55
A
2147 nfsm_chain_add_32(error, &nmreq, TRUE);
2148 nfsm_chain_add_32(error, &nmreq, vap->va_uid);
1c79356b 2149 } else {
2d21ac55 2150 nfsm_chain_add_32(error, &nmreq, FALSE);
1c79356b 2151 }
91447636 2152 if (VATTR_IS_ACTIVE(vap, va_gid)) {
2d21ac55
A
2153 nfsm_chain_add_32(error, &nmreq, TRUE);
2154 nfsm_chain_add_32(error, &nmreq, vap->va_gid);
1c79356b 2155 } else {
2d21ac55 2156 nfsm_chain_add_32(error, &nmreq, FALSE);
1c79356b 2157 }
91447636 2158 if (VATTR_IS_ACTIVE(vap, va_data_size)) {
2d21ac55
A
2159 nfsm_chain_add_32(error, &nmreq, TRUE);
2160 nfsm_chain_add_64(error, &nmreq, vap->va_data_size);
1c79356b 2161 } else {
2d21ac55 2162 nfsm_chain_add_32(error, &nmreq, FALSE);
1c79356b 2163 }
2d21ac55
A
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);
2167 } else {
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);
1c79356b 2172 } else {
2d21ac55 2173 nfsm_chain_add_32(error, &nmreq, NFS_TIME_DONT_CHANGE);
1c79356b 2174 }
2d21ac55
A
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);
1c79356b 2179 } else {
2d21ac55 2180 nfsm_chain_add_32(error, &nmreq, NFS_TIME_DONT_CHANGE);
1c79356b 2181 }
1c79356b 2182 }
2d21ac55 2183 nfsm_chain_add_32(error, &nmreq, FALSE);
1c79356b 2184 } else {
2d21ac55
A
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);
91447636 2193 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
2d21ac55
A
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);
91447636 2197 } else {
2d21ac55
A
2198 nfsm_chain_add_32(error, &nmreq, -1);
2199 nfsm_chain_add_32(error, &nmreq, -1);
91447636
A
2200 }
2201 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
2d21ac55
A
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);
91447636 2205 } else {
2d21ac55
A
2206 nfsm_chain_add_32(error, &nmreq, -1);
2207 nfsm_chain_add_32(error, &nmreq, -1);
91447636 2208 }
1c79356b 2209 }
2d21ac55
A
2210 nfsm_chain_build_done(error, &nmreq);
2211 nfsmout_if(error);
6d2010ae 2212 error = nfs_request(np, NULL, &nmreq, NFSPROC_SETATTR, ctx, NULL, &nmrep, &xid, &status);
b0d623f7 2213 if ((lockerror = nfs_node_lock(np)))
2d21ac55
A
2214 error = lockerror;
2215 if (nfsvers == NFS_VER3) {
91447636 2216 struct timespec premtime = { 0, 0 };
2d21ac55
A
2217 nfsm_chain_get_wcc_data(error, &nmrep, np, &premtime, &wccpostattr, &xid);
2218 nfsmout_if(error);
483a1d10 2219 /* if file hadn't changed, update cached mtime */
2d21ac55
A
2220 if (nfstimespeccmp(&np->n_mtime, &premtime, ==))
2221 NFS_CHANGED_UPDATE(nfsvers, np, &np->n_vattr);
483a1d10 2222 /* if directory hadn't changed, update namecache mtime */
2d21ac55
A
2223 if ((vnode_vtype(NFSTOV(np)) == VDIR) &&
2224 nfstimespeccmp(&np->n_ncmtime, &premtime, ==))
2225 NFS_CHANGED_UPDATE_NC(nfsvers, np, &np->n_vattr);
483a1d10 2226 if (!wccpostattr)
2d21ac55
A
2227 NATTRINVALIDATE(np);
2228 error = status;
e5568f75 2229 } else {
2d21ac55
A
2230 if (!error)
2231 error = status;
6d2010ae 2232 nfsm_chain_loadattr(error, &nmrep, np, nfsvers, &xid);
e5568f75 2233 }
b0d623f7
A
2234 /*
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
2241 * get loaded next.
2242 */
2243 nextxid = 0;
2244 nfs_get_xid(&nextxid);
2245 if (nextxid != (xid + 1)) {
2246 np->n_xid = nextxid;
2247 NATTRINVALIDATE(np);
2248 }
2d21ac55 2249nfsmout:
b0d623f7
A
2250 if (!lockerror)
2251 nfs_node_unlock(np);
2d21ac55
A
2252 nfsm_chain_cleanup(&nmreq);
2253 nfsm_chain_cleanup(&nmrep);
1c79356b
A
2254 return (error);
2255}
2256
2257/*
2d21ac55 2258 * NFS lookup call, one step at a time...
1c79356b 2259 * First look in cache
2d21ac55 2260 * If not found, unlock the directory nfsnode and do the RPC
1c79356b 2261 */
b0d623f7 2262int
2d21ac55 2263nfs_vnop_lookup(
91447636 2264 struct vnop_lookup_args /* {
1c79356b 2265 struct vnodeop_desc *a_desc;
91447636
A
2266 vnode_t a_dvp;
2267 vnode_t *a_vpp;
1c79356b 2268 struct componentname *a_cnp;
91447636 2269 vfs_context_t a_context;
2d21ac55 2270 } */ *ap)
1c79356b 2271{
2d21ac55 2272 vfs_context_t ctx = ap->a_context;
91447636
A
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;
2277 vnode_t newvp;
2d21ac55
A
2278 nfsnode_t dnp, np;
2279 struct nfsmount *nmp;
2280 mount_t mp;
b0d623f7 2281 int nfsvers, error, busyerror = ENOENT, isdot, isdotdot, negnamecache;
2d21ac55 2282 u_int64_t xid;
91447636 2283 struct nfs_vattr nvattr;
91447636 2284 int ngflags;
2d21ac55
A
2285 struct vnop_access_args naa;
2286 fhandle_t fh;
2287 struct nfsreq rq, *req = &rq;
1c79356b 2288
1c79356b 2289 *vpp = NULLVP;
55e303ae 2290
91447636 2291 dnp = VTONFS(dvp);
6d2010ae 2292 NVATTR_INIT(&nvattr);
fa4905b1 2293
2d21ac55
A
2294 mp = vnode_mount(dvp);
2295 nmp = VFSTONFS(mp);
fe8ab488 2296 if (nfs_mount_gone(nmp)) {
2d21ac55
A
2297 error = ENXIO;
2298 goto error_return;
2299 }
2300 nfsvers = nmp->nm_vers;
6d2010ae 2301 negnamecache = !NMFLAG(nmp, NONEGNAMECACHE);
2d21ac55 2302
b0d623f7
A
2303 if ((error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx))))
2304 goto error_return;
2305 /* nfs_getattr() will check changed and purge caches */
6d2010ae 2306 if ((error = nfs_getattr(dnp, NULL, ctx, NGA_CACHED)))
91447636 2307 goto error_return;
1c79356b 2308
91447636
A
2309 error = cache_lookup(dvp, vpp, cnp);
2310 switch (error) {
2311 case ENOENT:
2d21ac55
A
2312 /* negative cache entry */
2313 goto error_return;
91447636
A
2314 case 0:
2315 /* cache miss */
6d2010ae 2316 if ((nfsvers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) {
b0d623f7
A
2317 /* if rdirplus, try dir buf cache lookup */
2318 error = nfs_dir_buf_cache_lookup(dnp, &np, cnp, ctx, 0);
2319 if (!error && np) {
2320 /* dir buf cache hit */
2321 *vpp = NFSTOV(np);
2322 error = -1;
2323 }
2324 }
2325 if (error != -1) /* cache miss */
2326 break;
2327 /* FALLTHROUGH */
91447636
A
2328 case -1:
2329 /* cache hit, not really an error */
316670eb 2330 OSAddAtomic64(1, &nfsstats.lookupcache_hits);
91447636 2331
b0d623f7 2332 nfs_node_clear_busy(dnp);
6d2010ae 2333 busyerror = ENOENT;
2d21ac55 2334
91447636 2335 /* check for directory access */
6d2010ae 2336 naa.a_desc = &vnop_access_desc;
91447636
A
2337 naa.a_vp = dvp;
2338 naa.a_action = KAUTH_VNODE_SEARCH;
2d21ac55 2339 naa.a_context = ctx;
91447636
A
2340
2341 /* compute actual success/failure based on accessibility */
2d21ac55 2342 error = nfs_vnop_access(&naa);
91447636
A
2343 /* FALLTHROUGH */
2344 default:
2345 /* unexpected error from cache_lookup */
2346 goto error_return;
2347 }
2d21ac55
A
2348
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)
2353 isdot = 1;
2354 if ((cnp->cn_namelen == 2) && (cnp->cn_nameptr[1] == '.'))
2355 isdotdot = 1;
2356 }
2357 if (isdotdot || isdot) {
2358 fh.fh_len = 0;
91447636
A
2359 goto found;
2360 }
6d2010ae
A
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 */
2363 error = ENOENT;
2364 goto error_return;
2365 }
55e303ae 2366
91447636 2367 /* do we know this name is too long? */
2d21ac55 2368 nmp = VTONMP(dvp);
fe8ab488 2369 if (nfs_mount_gone(nmp)) {
2d21ac55
A
2370 error = ENXIO;
2371 goto error_return;
2372 }
2373 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME) &&
b0d623f7 2374 (cnp->cn_namelen > (int)nmp->nm_fsattr.nfsa_maxname)) {
91447636
A
2375 error = ENAMETOOLONG;
2376 goto error_return;
1c79356b 2377 }
fa4905b1 2378
1c79356b
A
2379 error = 0;
2380 newvp = NULLVP;
91447636 2381
316670eb 2382 OSAddAtomic64(1, &nfsstats.lookupcache_misses);
1c79356b 2383
2d21ac55
A
2384 error = nmp->nm_funcs->nf_lookup_rpc_async(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &req);
2385 nfsmout_if(error);
6d2010ae 2386 error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, req, &xid, &fh, &nvattr);
2d21ac55 2387 nfsmout_if(error);
91447636 2388
91447636 2389 /* is the file handle the same as this directory's file handle? */
2d21ac55
A
2390 isdot = NFS_CMPFH(dnp, fh.fh_data, fh.fh_len);
2391
2392found:
2d21ac55
A
2393 if (flags & ISLASTCN) {
2394 switch (cnp->cn_nameiop) {
2395 case DELETE:
2396 cnp->cn_flags &= ~MAKEENTRY;
2397 break;
2398 case RENAME:
2399 cnp->cn_flags &= ~MAKEENTRY;
2400 if (isdot) {
2401 error = EISDIR;
91447636
A
2402 goto error_return;
2403 }
2d21ac55 2404 break;
91447636 2405 }
91447636
A
2406 }
2407
2d21ac55 2408 if (isdotdot) {
2d21ac55
A
2409 newvp = vnode_getparent(dvp);
2410 if (!newvp) {
2411 error = ENOENT;
0b4e3aa0 2412 goto error_return;
1c79356b 2413 }
2d21ac55 2414 } else if (isdot) {
91447636 2415 error = vnode_get(dvp);
2d21ac55 2416 if (error)
0b4e3aa0 2417 goto error_return;
91447636 2418 newvp = dvp;
b0d623f7 2419 nfs_node_lock_force(dnp);
2d21ac55
A
2420 if (fh.fh_len && (dnp->n_xid <= xid))
2421 nfs_loadattrcache(dnp, &nvattr, &xid, 0);
b0d623f7 2422 nfs_node_unlock(dnp);
1c79356b 2423 } else {
2d21ac55 2424 ngflags = (cnp->cn_flags & MAKEENTRY) ? NG_MAKEENTRY : 0;
6d2010ae 2425 error = nfs_nget(mp, dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, ngflags, &np);
2d21ac55 2426 if (error)
0b4e3aa0 2427 goto error_return;
1c79356b 2428 newvp = NFSTOV(np);
b0d623f7 2429 nfs_node_unlock(np);
1c79356b 2430 }
1c79356b 2431 *vpp = newvp;
91447636 2432
2d21ac55 2433nfsmout:
1c79356b 2434 if (error) {
2d21ac55
A
2435 if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME)) &&
2436 (flags & ISLASTCN) && (error == ENOENT)) {
91447636 2437 if (vnode_mount(dvp) && vnode_vfsisrdonly(dvp))
1c79356b
A
2438 error = EROFS;
2439 else
2440 error = EJUSTRETURN;
2441 }
1c79356b 2442 }
2d21ac55
A
2443 if ((error == ENOENT) && (cnp->cn_flags & MAKEENTRY) &&
2444 (cnp->cn_nameiop != CREATE) && negnamecache) {
2445 /* add a negative entry in the name cache */
b0d623f7 2446 nfs_node_lock_force(dnp);
2d21ac55
A
2447 cache_enter(dvp, NULL, cnp);
2448 dnp->n_flag |= NNEGNCENTRIES;
b0d623f7 2449 nfs_node_unlock(dnp);
2d21ac55 2450 }
0b4e3aa0 2451error_return:
6d2010ae 2452 NVATTR_CLEANUP(&nvattr);
b0d623f7
A
2453 if (!busyerror)
2454 nfs_node_clear_busy(dnp);
91447636
A
2455 if (error && *vpp) {
2456 vnode_put(*vpp);
2457 *vpp = NULLVP;
2458 }
1c79356b
A
2459 return (error);
2460}
2461
fe8ab488
A
2462int nfs_readlink_nocache = DEFAULT_READLINK_NOCACHE;
2463
1c79356b 2464/*
2d21ac55 2465 * NFS readlink call
1c79356b 2466 */
b0d623f7 2467int
2d21ac55 2468nfs_vnop_readlink(
91447636
A
2469 struct vnop_readlink_args /* {
2470 struct vnodeop_desc *a_desc;
2471 vnode_t a_vp;
1c79356b 2472 struct uio *a_uio;
91447636 2473 vfs_context_t a_context;
2d21ac55 2474 } */ *ap)
1c79356b 2475{
2d21ac55
A
2476 vfs_context_t ctx = ap->a_context;
2477 nfsnode_t np = VTONFS(ap->a_vp);
2478 struct nfsmount *nmp;
b0d623f7 2479 int error = 0, nfsvers;
2d21ac55 2480 uint32_t buflen;
b0d623f7 2481 uio_t uio = ap->a_uio;
2d21ac55 2482 struct nfsbuf *bp = NULL;
fe8ab488
A
2483 struct timespec ts;
2484 int timeo;
2d21ac55
A
2485
2486 if (vnode_vtype(ap->a_vp) != VLNK)
1c79356b 2487 return (EPERM);
2d21ac55 2488
b0d623f7 2489 if (uio_resid(uio) == 0)
2d21ac55 2490 return (0);
b0d623f7 2491 if (uio_offset(uio) < 0)
2d21ac55
A
2492 return (EINVAL);
2493
2494 nmp = VTONMP(ap->a_vp);
fe8ab488 2495 if (nfs_mount_gone(nmp))
2d21ac55
A
2496 return (ENXIO);
2497 nfsvers = nmp->nm_vers;
2498
fe8ab488 2499
b0d623f7 2500 /* nfs_getattr() will check changed and purge caches */
fe8ab488 2501 if ((error = nfs_getattr(np, NULL, ctx, nfs_readlink_nocache ? NGA_UNCACHED : NGA_CACHED))) {
2d21ac55
A
2502 FSDBG(531, np, 0xd1e0001, 0, error);
2503 return (error);
2504 }
2d21ac55 2505
fe8ab488
A
2506 if (nfs_readlink_nocache) {
2507 timeo = nfs_attrcachetimeout(np);
2508 nanouptime(&ts);
2509 }
2510
2511retry:
316670eb 2512 OSAddAtomic64(1, &nfsstats.biocache_readlinks);
fe8ab488 2513 error = nfs_buf_get(np, 0, NFS_MAXPATHLEN, vfs_context_thread(ctx), NBLK_META, &bp);
2d21ac55
A
2514 if (error) {
2515 FSDBG(531, np, 0xd1e0002, 0, error);
2516 return (error);
2517 }
fe8ab488
A
2518
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);
2527 goto retry;
2528 }
2529 }
2d21ac55 2530 if (!ISSET(bp->nb_flags, NB_CACHE)) {
fe8ab488 2531readagain:
316670eb 2532 OSAddAtomic64(1, &nfsstats.readlink_bios);
2d21ac55
A
2533 buflen = bp->nb_bufsize;
2534 error = nmp->nm_funcs->nf_readlink_rpc(np, bp->nb_data, &buflen, ctx);
2535 if (error) {
fe8ab488
A
2536 if (error == ESTALE) {
2537 NFS_VNOP_DBG("Stale FH from readlink rpc\n");
2538 error = nfs_refresh_fh(np, ctx);
2539 if (error == 0)
2540 goto readagain;
2541 }
2d21ac55
A
2542 SET(bp->nb_flags, NB_ERROR);
2543 bp->nb_error = error;
fe8ab488 2544 NFS_VNOP_DBG("readlink failed %d\n", error);
2d21ac55
A
2545 } else {
2546 bp->nb_validoff = 0;
2547 bp->nb_validend = buflen;
fe8ab488
A
2548 np->n_rltim = ts;
2549 NFS_VNOP_DBG("readlink of %.*s\n", bp->nb_validend, (char *)bp->nb_data);
2d21ac55 2550 }
fe8ab488
A
2551 } else {
2552 NFS_VNOP_DBG("got cached link of %.*s\n", bp->nb_validend, (char *)bp->nb_data);
2d21ac55 2553 }
fe8ab488 2554
b0d623f7
A
2555 if (!error && (bp->nb_validend > 0))
2556 error = uiomove(bp->nb_data, bp->nb_validend, uio);
2d21ac55
A
2557 FSDBG(531, np, bp->nb_validend, 0, error);
2558 nfs_buf_release(bp, 1);
2559 return (error);
1c79356b
A
2560}
2561
2562/*
2d21ac55 2563 * Do a readlink RPC.
1c79356b
A
2564 */
2565int
2d21ac55 2566nfs3_readlink_rpc(nfsnode_t np, char *buf, uint32_t *buflenp, vfs_context_t ctx)
1c79356b 2567{
2d21ac55
A
2568 struct nfsmount *nmp;
2569 int error = 0, lockerror = ENOENT, nfsvers, status;
2570 uint32_t len;
fa4905b1 2571 u_int64_t xid;
2d21ac55 2572 struct nfsm_chain nmreq, nmrep;
1c79356b 2573
2d21ac55 2574 nmp = NFSTONMP(np);
fe8ab488 2575 if (nfs_mount_gone(nmp))
55e303ae 2576 return (ENXIO);
2d21ac55
A
2577 nfsvers = nmp->nm_vers;
2578 nfsm_chain_null(&nmreq);
2579 nfsm_chain_null(&nmrep);
2580
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);
2584 nfsmout_if(error);
6d2010ae 2585 error = nfs_request(np, NULL, &nmreq, NFSPROC_READLINK, ctx, NULL, &nmrep, &xid, &status);
b0d623f7 2586 if ((lockerror = nfs_node_lock(np)))
2d21ac55
A
2587 error = lockerror;
2588 if (nfsvers == NFS_VER3)
2589 nfsm_chain_postop_attr_update(error, &nmrep, np, &xid);
2590 if (!error)
2591 error = status;
2592 nfsm_chain_get_32(error, &nmrep, len);
2593 nfsmout_if(error);
2594 if ((nfsvers == NFS_VER2) && (len > *buflenp)) {
2595 error = EBADRPC;
2596 goto nfsmout;
2597 }
2598 if (len >= *buflenp) {
2599 if (np->n_size && (np->n_size < *buflenp))
2600 len = np->n_size;
2601 else
2602 len = *buflenp - 1;
2603 }
2604 nfsm_chain_get_opaque(error, &nmrep, len, buf);
2605 if (!error)
2606 *buflenp = len;
2607nfsmout:
2608 if (!lockerror)
b0d623f7 2609 nfs_node_unlock(np);
2d21ac55
A
2610 nfsm_chain_cleanup(&nmreq);
2611 nfsm_chain_cleanup(&nmrep);
1c79356b
A
2612 return (error);
2613}
2614
2615/*
2d21ac55 2616 * NFS read RPC call
1c79356b
A
2617 * Ditto above
2618 */
2619int
b0d623f7 2620nfs_read_rpc(nfsnode_t np, uio_t uio, vfs_context_t ctx)
1c79356b 2621{
1c79356b 2622 struct nfsmount *nmp;
2d21ac55 2623 int error = 0, nfsvers, eof = 0;
b0d623f7
A
2624 size_t nmrsize, len, retlen;
2625 user_ssize_t tsiz;
2d21ac55
A
2626 off_t txoffset;
2627 struct nfsreq rq, *req = &rq;
b0d623f7 2628 uint32_t stategenid = 0, restart = 0;
1c79356b 2629
b0d623f7 2630 FSDBG_TOP(536, np, uio_offset(uio), uio_resid(uio), 0);
2d21ac55 2631 nmp = NFSTONMP(np);
fe8ab488 2632 if (nfs_mount_gone(nmp))
55e303ae 2633 return (ENXIO);
2d21ac55 2634 nfsvers = nmp->nm_vers;
55e303ae
A
2635 nmrsize = nmp->nm_rsize;
2636
b0d623f7
A
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);
1c79356b 2641 return (EFBIG);
55e303ae 2642 }
2d21ac55 2643
1c79356b 2644 while (tsiz > 0) {
b0d623f7 2645 len = retlen = (tsiz > (user_ssize_t)nmrsize) ? nmrsize : (size_t)tsiz;
2d21ac55 2646 FSDBG(536, np, txoffset, len, 0);
6d2010ae
A
2647 if (np->n_flag & NREVOKE) {
2648 error = EIO;
2649 break;
2650 }
b0d623f7
A
2651 if (nmp->nm_vers >= NFS_VER4)
2652 stategenid = nmp->nm_stategenid;
2d21ac55
A
2653 error = nmp->nm_funcs->nf_read_rpc_async(np, txoffset, len,
2654 vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, &req);
2655 if (!error)
b0d623f7
A
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);
6d2010ae
A
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);
b0d623f7
A
2663 }
2664 lck_mtx_unlock(&nmp->nm_lock);
6d2010ae
A
2665 if (np->n_flag & NREVOKE) {
2666 error = EIO;
2667 } else {
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)))
2671 continue;
2672 }
b0d623f7 2673 }
91447636
A
2674 if (error)
2675 break;
2d21ac55
A
2676 txoffset += retlen;
2677 tsiz -= retlen;
2678 if (nfsvers != NFS_VER2) {
2679 if (eof || (retlen == 0))
2680 tsiz = 0;
2681 } else if (retlen < len)
2682 tsiz = 0;
2683 }
2684
b0d623f7 2685 FSDBG_BOT(536, np, eof, uio_resid(uio), error);
2d21ac55
A
2686 return (error);
2687}
2688
2689int
2690nfs3_read_rpc_async(
2691 nfsnode_t np,
2692 off_t offset,
2693 size_t len,
2694 thread_t thd,
2695 kauth_cred_t cred,
2696 struct nfsreq_cbinfo *cb,
2697 struct nfsreq **reqp)
2698{
2699 struct nfsmount *nmp;
2700 int error = 0, nfsvers;
2701 struct nfsm_chain nmreq;
2702
2703 nmp = NFSTONMP(np);
fe8ab488 2704 if (nfs_mount_gone(nmp))
2d21ac55
A
2705 return (ENXIO);
2706 nfsvers = nmp->nm_vers;
2707
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);
2714 } else {
2715 nfsm_chain_add_32(error, &nmreq, offset);
2716 nfsm_chain_add_32(error, &nmreq, len);
2717 nfsm_chain_add_32(error, &nmreq, 0);
2718 }
2719 nfsm_chain_build_done(error, &nmreq);
2720 nfsmout_if(error);
6d2010ae 2721 error = nfs_request_async(np, NULL, &nmreq, NFSPROC_READ, thd, cred, NULL, 0, cb, reqp);
2d21ac55
A
2722nfsmout:
2723 nfsm_chain_cleanup(&nmreq);
2724 return (error);
2725}
2726
2727int
2728nfs3_read_rpc_async_finish(
2729 nfsnode_t np,
2730 struct nfsreq *req,
b0d623f7 2731 uio_t uio,
2d21ac55
A
2732 size_t *lenp,
2733 int *eofp)
2734{
2735 int error = 0, lockerror, nfsvers, status, eof = 0;
2736 size_t retlen = 0;
2737 uint64_t xid;
2738 struct nfsmount *nmp;
2739 struct nfsm_chain nmrep;
2740
2741 nmp = NFSTONMP(np);
fe8ab488 2742 if (nfs_mount_gone(nmp)) {
2d21ac55
A
2743 nfs_request_async_cancel(req);
2744 return (ENXIO);
2745 }
2746 nfsvers = nmp->nm_vers;
2747
2748 nfsm_chain_null(&nmrep);
2749
2750 error = nfs_request_async_finish(req, &nmrep, &xid, &status);
2751 if (error == EINPROGRESS) /* async request restarted */
2752 return (error);
2753
b0d623f7 2754 if ((lockerror = nfs_node_lock(np)))
2d21ac55
A
2755 error = lockerror;
2756 if (nfsvers == NFS_VER3)
2757 nfsm_chain_postop_attr_update(error, &nmrep, np, &xid);
2758 if (!error)
2759 error = status;
2760 if (nfsvers == NFS_VER3) {
2761 nfsm_chain_adv(error, &nmrep, NFSX_UNSIGNED);
2762 nfsm_chain_get_32(error, &nmrep, eof);
2763 } else {
6d2010ae 2764 nfsm_chain_loadattr(error, &nmrep, np, nfsvers, &xid);
2d21ac55
A
2765 }
2766 if (!lockerror)
b0d623f7 2767 nfs_node_unlock(np);
2d21ac55
A
2768 nfsm_chain_get_32(error, &nmrep, retlen);
2769 if ((nfsvers == NFS_VER2) && (retlen > *lenp))
2770 error = EBADRPC;
2771 nfsmout_if(error);
b0d623f7 2772 error = nfsm_chain_get_uio(&nmrep, MIN(retlen, *lenp), uio);
2d21ac55
A
2773 if (eofp) {
2774 if (nfsvers == NFS_VER3) {
2775 if (!eof && !retlen)
2776 eof = 1;
2777 } else if (retlen < *lenp) {
2778 eof = 1;
1c79356b 2779 }
2d21ac55
A
2780 *eofp = eof;
2781 }
2782 *lenp = MIN(retlen, *lenp);
2783nfsmout:
2784 nfsm_chain_cleanup(&nmrep);
2785 return (error);
2786}
2787
2788/*
2789 * NFS write call
2790 */
2791int
2792nfs_vnop_write(
2793 struct vnop_write_args /* {
2794 struct vnodeop_desc *a_desc;
2795 vnode_t a_vp;
2796 struct uio *a_uio;
2797 int a_ioflag;
2798 vfs_context_t a_context;
2799 } */ *ap)
2800{
2801 vfs_context_t ctx = ap->a_context;
b0d623f7 2802 uio_t uio = ap->a_uio;
2d21ac55
A
2803 vnode_t vp = ap->a_vp;
2804 nfsnode_t np = VTONFS(vp);
2805 int ioflag = ap->a_ioflag;
2806 struct nfsbuf *bp;
2d21ac55
A
2807 struct nfsmount *nmp = VTONMP(vp);
2808 daddr64_t lbn;
2809 int biosize;
2810 int n, on, error = 0;
2811 off_t boff, start, end;
b0d623f7
A
2812 uio_t auio;
2813 char auio_buf [ UIO_SIZEOF(1) ];
2d21ac55
A
2814 thread_t thd;
2815 kauth_cred_t cred;
2816
b0d623f7 2817 FSDBG_TOP(515, np, uio_offset(uio), uio_resid(uio), ioflag);
2d21ac55
A
2818
2819 if (vnode_vtype(vp) != VREG) {
b0d623f7 2820 FSDBG_BOT(515, np, uio_offset(uio), uio_resid(uio), EIO);
2d21ac55
A
2821 return (EIO);
2822 }
2823
2824 thd = vfs_context_thread(ctx);
2825 cred = vfs_context_ucred(ctx);
2826
b0d623f7 2827 nfs_data_lock(np, NFS_DATA_LOCK_SHARED);
2d21ac55 2828
b0d623f7 2829 if ((error = nfs_node_lock(np))) {
2d21ac55 2830 nfs_data_unlock(np);
b0d623f7 2831 FSDBG_BOT(515, np, uio_offset(uio), uio_resid(uio), error);
2d21ac55
A
2832 return (error);
2833 }
2834 np->n_wrbusy++;
2835
2836 if (np->n_flag & NWRITEERR) {
2837 error = np->n_error;
2838 np->n_flag &= ~NWRITEERR;
2839 }
2840 if (np->n_flag & NNEEDINVALIDATE) {
2841 np->n_flag &= ~NNEEDINVALIDATE;
b0d623f7 2842 nfs_node_unlock(np);
2d21ac55
A
2843 nfs_data_unlock(np);
2844 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1);
b0d623f7
A
2845 nfs_data_lock(np, NFS_DATA_LOCK_SHARED);
2846 } else {
2847 nfs_node_unlock(np);
2d21ac55 2848 }
b0d623f7 2849 if (error)
2d21ac55 2850 goto out;
2d21ac55
A
2851
2852 biosize = nmp->nm_biosize;
2853
2854 if (ioflag & (IO_APPEND | IO_SYNC)) {
b0d623f7 2855 nfs_node_lock_force(np);
2d21ac55
A
2856 if (np->n_flag & NMODIFIED) {
2857 NATTRINVALIDATE(np);
b0d623f7 2858 nfs_node_unlock(np);
2d21ac55
A
2859 nfs_data_unlock(np);
2860 error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1);
b0d623f7
A
2861 nfs_data_lock(np, NFS_DATA_LOCK_SHARED);
2862 if (error) {
2863 FSDBG(515, np, uio_offset(uio), 0x10bad01, error);
2d21ac55 2864 goto out;
e5568f75 2865 }
b0d623f7
A
2866 } else {
2867 nfs_node_unlock(np);
2d21ac55
A
2868 }
2869 if (ioflag & IO_APPEND) {
2d21ac55 2870 nfs_data_unlock(np);
b0d623f7 2871 /* nfs_getattr() will check changed and purge caches */
6d2010ae 2872 error = nfs_getattr(np, NULL, ctx, NGA_UNCACHED);
2d21ac55 2873 /* we'll be extending the file, so take the data lock exclusive */
b0d623f7
A
2874 nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE);
2875 if (error) {
2876 FSDBG(515, np, uio_offset(uio), 0x10bad02, error);
2d21ac55
A
2877 goto out;
2878 }
b0d623f7 2879 uio_setoffset(uio, np->n_size);
2d21ac55
A
2880 }
2881 }
b0d623f7 2882 if (uio_offset(uio) < 0) {
2d21ac55 2883 error = EINVAL;
b0d623f7 2884 FSDBG_BOT(515, np, uio_offset(uio), 0xbad0ff, error);
2d21ac55
A
2885 goto out;
2886 }
b0d623f7 2887 if (uio_resid(uio) == 0)
2d21ac55 2888 goto out;
2d21ac55 2889
b0d623f7 2890 if (((uio_offset(uio) + uio_resid(uio)) > (off_t)np->n_size) && !(ioflag & IO_APPEND)) {
fe8ab488
A
2891 /*
2892 * It looks like we'll be extending the file, so take the data lock exclusive.
2893 */
2d21ac55 2894 nfs_data_unlock(np);
b0d623f7 2895 nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE);
fe8ab488
A
2896
2897 /*
2898 * Also, if the write begins after the previous EOF buffer, make sure to zero
2899 * and validate the new bytes in that buffer.
2900 */
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;
2905
2906 if (eofoff && (eofbn < lbn)) {
2907 if ((error = nfs_buf_get(np, eofbn, biosize, thd, NBLK_WRITE|NBLK_ONLYVALID, &eofbp)))
2908 goto out;
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 */
2916 if (eofbp) {
2917 /*
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
2920 * currently valid).
2921 * For pages after the old last page, zero them and
2922 * mark them as valid.
2923 */
2924 char *d;
2925 int i;
2926 if (ioflag & IO_NOCACHE)
2927 SET(eofbp->nb_flags, NB_NOCACHE);
2928 NFS_BUF_MAP(eofbp);
2929 FSDBG(516, eofbp, eofoff, biosize - eofoff, 0xe0fff01e);
2930 d = eofbp->nb_data;
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);
2937 }
2938 eofoff += PAGE_SIZE - poff;
2939 i++;
2940 }
2941 nfs_buf_release(eofbp, 1);
2942 }
2943 }
2d21ac55
A
2944 }
2945
2946 do {
316670eb 2947 OSAddAtomic64(1, &nfsstats.biocache_writes);
b0d623f7
A
2948 lbn = uio_offset(uio) / biosize;
2949 on = uio_offset(uio) % biosize;
2950 n = biosize - on;
2951 if (uio_resid(uio) < n)
2952 n = uio_resid(uio);
2d21ac55
A
2953again:
2954 /*
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.
2959 */
2960 error = nfs_buf_get(np, lbn, biosize, thd, NBLK_WRITE, &bp);
2961 if (error)
2962 goto out;
2963 /* map the block because we know we're going to write to it */
2964 NFS_BUF_MAP(bp);
2965
2966 if (ioflag & IO_NOCACHE)
2967 SET(bp->nb_flags, NB_NOCACHE);
2968
2969 if (!IS_VALID_CRED(bp->nb_wcred)) {
2970 kauth_cred_ref(cred);
2971 bp->nb_wcred = cred;
2972 }
2973
2974 /*
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.
2977 *
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.)
2986 */
2987 if (bp->nb_dirtyend > 0) {
2988 if (on > bp->nb_dirtyend || (on + n) < bp->nb_dirtyoff || bp->nb_dirty) {
b0d623f7 2989 FSDBG(515, np, uio_offset(uio), bp, 0xd15c001);
2d21ac55
A
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);
2995 if (error)
2996 goto out;
2997 goto again;
2998 }
2999 } else if (bp->nb_dirty) {
3000 int firstpg, lastpg;
3001 u_int32_t pagemask;
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) {
b0d623f7 3008 FSDBG(515, np, uio_offset(uio), bp, 0xd15c002);
2d21ac55
A
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);
3014 if (error)
3015 goto out;
3016 goto again;
3017 }
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)) {
b0d623f7 3021 FSDBG(515, np, uio_offset(uio), bp, 0xd15c003);
2d21ac55
A
3022 bp->nb_dirtyoff = min(on, firstpg * PAGE_SIZE);
3023 if (NBPGDIRTY(bp,lastpg)) {
3024 bp->nb_dirtyend = (lastpg+1) * PAGE_SIZE;
3025 /* clip to EOF */
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;
3030 }
3031 } else
3032 bp->nb_dirtyend = on+n;
3033 }
3034 }
3035
3036 /*
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.
3041 */
b0d623f7 3042 if ((uio_offset(uio) + n) > (off_t)np->n_size) {
2d21ac55 3043 daddr64_t eofbn = np->n_size / biosize;
b0d623f7 3044 int neweofoff = (uio_offset(uio) + n) % biosize;
2d21ac55 3045
b0d623f7 3046 FSDBG(515, 0xb1ffa000, uio_offset(uio) + n, eofoff, neweofoff);
2d21ac55 3047
2d21ac55
A
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;
3053 u_int32_t pagemask;
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) {
3060 int i;
3061 for (i=bp->nb_validend/PAGE_SIZE; i <= (on - 1)/PAGE_SIZE; i++)
3062 NBPGVALID_SET(bp, i);
3063 NFS_BUF_MAP(bp);
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);
3067 }
3068 /* zerofill any trailing data in the last page */
3069 if (neweofoff) {
3070 NFS_BUF_MAP(bp);
3071 FSDBG(516, bp, neweofoff, PAGE_SIZE - (neweofoff & PAGE_MASK), 0xe0f);
3072 bzero((char *)bp->nb_data + neweofoff,
3073 PAGE_SIZE - (neweofoff & PAGE_MASK));
3074 }
3075 }
3076 }
b0d623f7
A
3077 np->n_size = uio_offset(uio) + n;
3078 nfs_node_lock_force(np);
2d21ac55
A
3079 CLR(np->n_flag, NUPDATESIZE);
3080 np->n_flag |= NMODIFIED;
b0d623f7 3081 nfs_node_unlock(np);
2d21ac55
A
3082 FSDBG(516, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001);
3083 ubc_setsize(vp, (off_t)np->n_size); /* XXX errors */
2d21ac55
A
3084 }
3085 /*
3086 * If dirtyend exceeds file size, chop it down. This should
3087 * not occur unless there is a race.
3088 */
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;
3093 }
3094 /*
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.
3097 *
3098 * Writes that are smaller than a block are delayed if they
3099 * don't extend to the end of the block.
3100 *
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.
3107 *
3108 * Notes:
3109 * We don't want to read anything we're just going to write over.
6d2010ae
A
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).
2d21ac55
A
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
3115 * page cache.
3116 */
6d2010ae 3117 if (!ISSET(bp->nb_flags, NB_CACHE) && (n < biosize)) {
2d21ac55
A
3118 int firstpg, lastpg, dirtypg;
3119 int firstpgoff, lastpgoff;
3120 start = end = -1;
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;
3129 }
3130 if (lastpgoff && !NBPGVALID(bp,lastpg)) {
3131 /* need to read end of last page */
3132 if (start < 0)
3133 start = (lastpg * PAGE_SIZE) + lastpgoff;
3134 end = (lastpg + 1) * PAGE_SIZE;
3135 }
6d2010ae
A
3136 if (ISSET(bp->nb_flags, NB_NOCACHE)) {
3137 /*
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
3145 * and the COMMIT.
3146 * (NB_STABLE indicates that data writes should be FILESYNC)
3147 */
3148 if (end > start)
3149 SET(bp->nb_flags, NB_STABLE);
3150 goto skipread;
3151 }
2d21ac55
A
3152 if (end > start) {
3153 /* need to read the data in range: start...end-1 */
3154
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))
3160 break;
3161
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)))
3168 break;
3169 }
3170
3171 NFS_BUF_MAP(bp);
3172 /* setup uio for read(s) */
3173 boff = NBOFF(bp);
b0d623f7
A
3174 auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ,
3175 &auio_buf, sizeof(auio_buf));
2d21ac55
A
3176
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 */
b0d623f7
A
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);
6d2010ae
A
3183 if (error) {
3184 /* couldn't read the data, so treat buffer as synchronous NOCACHE */
2d21ac55 3185 SET(bp->nb_flags, (NB_NOCACHE|NB_STABLE));
6d2010ae
A
3186 goto skipread;
3187 }
b0d623f7
A
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));
2d21ac55
A
3191 }
3192 if (!error) {
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);
3203 }
3204 /* adjust start to read any trailing data */
3205 start = on+n;
3206 }
3207
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))
3214 break;
3215 }
3216
3217 if (((boff+start) >= (off_t)np->n_size) ||
3218 ((start >= on) && ((boff + on + n) >= (off_t)np->n_size))) {
3219 /*
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.
3225 */
3226 FSDBG(516, bp, start, end - start, 0xd00dee00);
3227 bzero(bp->nb_data + start, end - start);
3228 error = 0;
6d2010ae 3229 } else {
2d21ac55 3230 /* now we'll read the (rest of the) data */
b0d623f7
A
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);
6d2010ae
A
3234 if (error) {
3235 /* couldn't read the data, so treat buffer as synchronous NOCACHE */
2d21ac55 3236 SET(bp->nb_flags, (NB_NOCACHE|NB_STABLE));
6d2010ae
A
3237 goto skipread;
3238 }
b0d623f7
A
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));
2d21ac55
A
3242 }
3243 }
3244 if (!error) {
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 */
b0d623f7 3253 for (; start < (off_t)trunc_page_32(on); start+=PAGE_SIZE)
2d21ac55
A
3254 NBPGVALID_SET(bp, start/PAGE_SIZE);
3255 /* validate any pages after the range of pages being written to */
b0d623f7 3256 for (; (end - 1) > (off_t)round_page_32(on+n-1); end-=PAGE_SIZE)
2d21ac55
A
3257 NBPGVALID_SET(bp, (end-1)/PAGE_SIZE);
3258 }
3259 /* Note: pages being written to will be validated when written */
3260 }
3261 }
6d2010ae 3262skipread:
2d21ac55
A
3263
3264 if (ISSET(bp->nb_flags, NB_ERROR)) {
3265 error = bp->nb_error;
3266 nfs_buf_release(bp, 1);
3267 goto out;
3268 }
3269
b0d623f7 3270 nfs_node_lock_force(np);
2d21ac55 3271 np->n_flag |= NMODIFIED;
b0d623f7 3272 nfs_node_unlock(np);
2d21ac55
A
3273
3274 NFS_BUF_MAP(bp);
3275 error = uiomove((char *)bp->nb_data + on, n, uio);
3276 if (error) {
3277 SET(bp->nb_flags, NB_ERROR);
3278 nfs_buf_release(bp, 1);
3279 goto out;
3280 }
3281
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);
3286 /*
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.
3294 */
3295 }
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);
3299 } else {
3300 bp->nb_dirtyoff = on;
3301 bp->nb_dirtyend = on + n;
3302 }
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;
e5568f75 3307 } else {
2d21ac55
A
3308 bp->nb_validoff = min(bp->nb_validoff, bp->nb_dirtyoff);
3309 bp->nb_validend = max(bp->nb_validend, bp->nb_dirtyend);
3310 }
3311 if (!ISSET(bp->nb_flags, NB_CACHE))
3312 nfs_buf_normalize_valid_range(np, bp);
3313
3314 /*
3315 * Since this block is being modified, it must be written
3316 * again and not just committed.
3317 */
3318 if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
b0d623f7 3319 nfs_node_lock_force(np);
2d21ac55
A
3320 if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
3321 np->n_needcommitcnt--;
3322 CHECK_NEEDCOMMITCNT(np);
e5568f75 3323 }
2d21ac55 3324 CLR(bp->nb_flags, NB_NEEDCOMMIT);
b0d623f7 3325 nfs_node_unlock(np);
e5568f75 3326 }
2d21ac55
A
3327
3328 if (ioflag & IO_SYNC) {
3329 error = nfs_buf_write(bp);
3330 if (error)
3331 goto out;
b0d623f7
A
3332 } else if (((n + on) == biosize) || (ioflag & IO_APPEND) ||
3333 (ioflag & IO_NOCACHE) || ISSET(bp->nb_flags, NB_NOCACHE)) {
2d21ac55
A
3334 SET(bp->nb_flags, NB_ASYNC);
3335 error = nfs_buf_write(bp);
3336 if (error)
3337 goto out;
e5568f75 3338 } else {
2d21ac55
A
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)
b0d623f7 3343 OSIncrementAtomicLong(&p->p_stats->p_ru.ru_oublock);
2d21ac55
A
3344 }
3345 nfs_buf_write_delayed(bp);
e5568f75 3346 }
2d21ac55
A
3347 if (np->n_needcommitcnt >= NFS_A_LOT_OF_NEEDCOMMITS)
3348 nfs_flushcommits(np, 1);
3349
b0d623f7 3350 } while (uio_resid(uio) > 0 && n > 0);
2d21ac55
A
3351
3352out:
b0d623f7 3353 nfs_node_lock_force(np);
2d21ac55 3354 np->n_wrbusy--;
b0d623f7 3355 nfs_node_unlock(np);
2d21ac55 3356 nfs_data_unlock(np);
b0d623f7 3357 FSDBG_BOT(515, np, uio_offset(uio), uio_resid(uio), error);
1c79356b
A
3358 return (error);
3359}
3360
2d21ac55 3361
1c79356b 3362/*
2d21ac55 3363 * NFS write call
1c79356b
A
3364 */
3365int
2d21ac55
A
3366nfs_write_rpc(
3367 nfsnode_t np,
b0d623f7 3368 uio_t uio,
2d21ac55
A
3369 vfs_context_t ctx,
3370 int *iomodep,
3371 uint64_t *wverfp)
3372{
b0d623f7 3373 return nfs_write_rpc2(np, uio, vfs_context_thread(ctx), vfs_context_ucred(ctx), iomodep, wverfp);
2d21ac55
A
3374}
3375
3376int
3377nfs_write_rpc2(
3378 nfsnode_t np,
b0d623f7 3379 uio_t uio,
2d21ac55 3380 thread_t thd,
91447636 3381 kauth_cred_t cred,
2d21ac55 3382 int *iomodep,
8f6c56a5 3383 uint64_t *wverfp)
1c79356b 3384{
55e303ae 3385 struct nfsmount *nmp;
b0d623f7 3386 int error = 0, nfsvers;
39236c6e 3387 int wverfset, commit, committed;
2d21ac55
A
3388 uint64_t wverf = 0, wverf2;
3389 size_t nmwsize, totalsize, tsiz, len, rlen;
3390 struct nfsreq rq, *req = &rq;
b0d623f7 3391 uint32_t stategenid = 0, vrestart = 0, restart = 0;
39236c6e 3392 uio_t uio_save = NULL;
1c79356b
A
3393
3394#if DIAGNOSTIC
2d21ac55 3395 /* XXX limitation based on need to back up uio on short write */
b0d623f7 3396 if (uio_iovcnt(uio) != 1)
2d21ac55 3397 panic("nfs3_write_rpc: iovcnt > 1");
1c79356b 3398#endif
b0d623f7 3399 FSDBG_TOP(537, np, uio_offset(uio), uio_resid(uio), *iomodep);
2d21ac55 3400 nmp = NFSTONMP(np);
fe8ab488 3401 if (nfs_mount_gone(nmp))
55e303ae 3402 return (ENXIO);
2d21ac55
A
3403 nfsvers = nmp->nm_vers;
3404 nmwsize = nmp->nm_wsize;
3405
b0d623f7 3406 wverfset = 0;
2d21ac55
A
3407 committed = NFS_WRITE_FILESYNC;
3408
b0d623f7
A
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);
1c79356b 3412 return (EFBIG);
55e303ae 3413 }
2d21ac55 3414
39236c6e
A
3415 uio_save = uio_duplicate(uio);
3416 if (uio_save == NULL) {
3417 return (EIO);
3418 }
3419
1c79356b 3420 while (tsiz > 0) {
2d21ac55 3421 len = (tsiz > nmwsize) ? nmwsize : tsiz;
b0d623f7 3422 FSDBG(537, np, uio_offset(uio), len, 0);
6d2010ae
A
3423 if (np->n_flag & NREVOKE) {
3424 error = EIO;
3425 break;
3426 }
b0d623f7
A
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);
2d21ac55
A
3430 if (!error)
3431 error = nmp->nm_funcs->nf_write_rpc_async_finish(np, req, &commit, &rlen, &wverf2);
3432 nmp = NFSTONMP(np);
fe8ab488 3433 if (nfs_mount_gone(nmp))
55e303ae 3434 error = ENXIO;
b0d623f7
A
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);
6d2010ae
A
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);
b0d623f7
A
3441 }
3442 lck_mtx_unlock(&nmp->nm_lock);
6d2010ae
A
3443 if (np->n_flag & NREVOKE) {
3444 error = EIO;
3445 } else {
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)))
3449 continue;
3450 }
b0d623f7 3451 }
91447636
A
3452 if (error)
3453 break;
2d21ac55
A
3454 if (nfsvers == NFS_VER2) {
3455 tsiz -= len;
3456 continue;
1c79356b 3457 }
1c79356b 3458
2d21ac55
A
3459 /* check for a short write */
3460 if (rlen < len) {
39236c6e
A
3461 /* Reset the uio to reflect the actual transfer */
3462 *uio = *uio_save;
3463 uio_update(uio, totalsize - (tsiz - rlen));
2d21ac55 3464 len = rlen;
e5568f75 3465 }
fa4905b1 3466
2d21ac55
A
3467 /* return lowest commit level returned */
3468 if (commit < committed)
3469 committed = commit;
3470
fa4905b1 3471 tsiz -= len;
2d21ac55
A
3472
3473 /* check write verifier */
3474 if (!wverfset) {
3475 wverf = wverf2;
3476 wverfset = 1;
3477 } else if (wverf != wverf2) {
3478 /* verifier changed, so we need to restart all the writes */
b0d623f7 3479 if (++vrestart > 100) {
2d21ac55
A
3480 /* give up after too many restarts */
3481 error = EIO;
3482 break;
3483 }
39236c6e 3484 *uio = *uio_save; // Reset the uio back to the start
2d21ac55
A
3485 committed = NFS_WRITE_FILESYNC;
3486 wverfset = 0;
3487 tsiz = totalsize;
3488 }
3489 }
39236c6e
A
3490 if (uio_save)
3491 uio_free(uio_save);
2d21ac55
A
3492 if (wverfset && wverfp)
3493 *wverfp = wverf;
3494 *iomodep = committed;
3495 if (error)
b0d623f7
A
3496 uio_setresid(uio, tsiz);
3497 FSDBG_BOT(537, np, committed, uio_resid(uio), error);
2d21ac55
A
3498 return (error);
3499}
3500
3501int
3502nfs3_write_rpc_async(
3503 nfsnode_t np,
b0d623f7 3504 uio_t uio,
2d21ac55
A
3505 size_t len,
3506 thread_t thd,
3507 kauth_cred_t cred,
3508 int iomode,
3509 struct nfsreq_cbinfo *cb,
3510 struct nfsreq **reqp)
3511{
3512 struct nfsmount *nmp;
6d2010ae 3513 mount_t mp;
2d21ac55 3514 int error = 0, nfsvers;
2d21ac55
A
3515 struct nfsm_chain nmreq;
3516
3517 nmp = NFSTONMP(np);
fe8ab488 3518 if (nfs_mount_gone(nmp))
2d21ac55
A
3519 return (ENXIO);
3520 nfsvers = nmp->nm_vers;
3521
6d2010ae
A
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;
3526
2d21ac55
A
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) {
b0d623f7 3532 nfsm_chain_add_64(error, &nmreq, uio_offset(uio));
2d21ac55
A
3533 nfsm_chain_add_32(error, &nmreq, len);
3534 nfsm_chain_add_32(error, &nmreq, iomode);
3535 } else {
3536 nfsm_chain_add_32(error, &nmreq, 0);
b0d623f7 3537 nfsm_chain_add_32(error, &nmreq, uio_offset(uio));
2d21ac55
A
3538 nfsm_chain_add_32(error, &nmreq, 0);
3539 }
3540 nfsm_chain_add_32(error, &nmreq, len);
3541 nfsmout_if(error);
b0d623f7 3542 error = nfsm_chain_add_uio(&nmreq, uio, len);
2d21ac55
A
3543 nfsm_chain_build_done(error, &nmreq);
3544 nfsmout_if(error);
6d2010ae 3545 error = nfs_request_async(np, NULL, &nmreq, NFSPROC_WRITE, thd, cred, NULL, 0, cb, reqp);
2d21ac55
A
3546nfsmout:
3547 nfsm_chain_cleanup(&nmreq);
3548 return (error);
3549}
3550
3551int
3552nfs3_write_rpc_async_finish(
3553 nfsnode_t np,
3554 struct nfsreq *req,
3555 int *iomodep,
3556 size_t *rlenp,
3557 uint64_t *wverfp)
3558{
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;
3563 mount_t mp;
3564 struct nfsm_chain nmrep;
3565
3566 nmp = NFSTONMP(np);
fe8ab488 3567 if (nfs_mount_gone(nmp)) {
2d21ac55
A
3568 nfs_request_async_cancel(req);
3569 return (ENXIO);
3570 }
3571 nfsvers = nmp->nm_vers;
3572
3573 nfsm_chain_null(&nmrep);
3574
3575 error = nfs_request_async_finish(req, &nmrep, &xid, &status);
3576 if (error == EINPROGRESS) /* async request restarted */
3577 return (error);
3578 nmp = NFSTONMP(np);
fe8ab488 3579 if (nfs_mount_gone(nmp))
2d21ac55 3580 error = ENXIO;
b0d623f7 3581 if (!error && (lockerror = nfs_node_lock(np)))
2d21ac55
A
3582 error = lockerror;
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, ==))
3587 updatemtime = 1;
3588 if (!error)
3589 error = status;
3590 nfsm_chain_get_32(error, &nmrep, rlen);
3591 nfsmout_if(error);
3592 *rlenp = rlen;
3593 if (rlen <= 0)
3594 error = NFSERR_IO;
3595 nfsm_chain_get_32(error, &nmrep, committed);
3596 nfsm_chain_get_64(error, &nmrep, wverf);
3597 nfsmout_if(error);
3598 if (wverfp)
3599 *wverfp = 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;
3606 }
3607 lck_mtx_unlock(&nmp->nm_lock);
3608 } else {
3609 if (!error)
3610 error = status;
6d2010ae 3611 nfsm_chain_loadattr(error, &nmrep, np, nfsvers, &xid);
2d21ac55 3612 nfsmout_if(error);
1c79356b 3613 }
2d21ac55
A
3614 if (updatemtime)
3615 NFS_CHANGED_UPDATE(nfsvers, np, &np->n_vattr);
1c79356b 3616nfsmout:
2d21ac55 3617 if (!lockerror)
b0d623f7 3618 nfs_node_unlock(np);
2d21ac55
A
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;
1c79356b
A
3624 return (error);
3625}
3626
3627/*
2d21ac55
A
3628 * NFS mknod vnode op
3629 *
3630 * For NFS v2 this is a kludge. Use a create RPC but with the IFMT bits of the
1c79356b
A
3631 * mode set to specify the file type and the size field for rdev.
3632 */
b0d623f7 3633int
2d21ac55
A
3634nfs3_vnop_mknod(
3635 struct vnop_mknod_args /* {
3636 struct vnodeop_desc *a_desc;
3637 vnode_t a_dvp;
3638 vnode_t *a_vpp;
3639 struct componentname *a_cnp;
3640 struct vnode_attr *a_vap;
3641 vfs_context_t a_context;
3642 } */ *ap)
1c79356b 3643{
2d21ac55
A
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);
6d2010ae 3653 struct nfs_vattr nvattr;
2d21ac55 3654 fhandle_t fh;
b0d623f7 3655 int error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0;
91447636 3656 struct timespec premtime = { 0, 0 };
b0d623f7 3657 u_int32_t rdev;
39236c6e 3658 u_int64_t xid = 0, dxid;
2d21ac55
A
3659 int nfsvers, gotuid, gotgid;
3660 struct nfsm_chain nmreq, nmrep;
6d2010ae 3661 struct nfsreq rq, *req = &rq;
2d21ac55
A
3662
3663 nmp = VTONMP(dvp);
fe8ab488 3664 if (nfs_mount_gone(nmp))
2d21ac55
A
3665 return (ENXIO);
3666 nfsvers = nmp->nm_vers;
1c79356b 3667
91447636
A
3668 if (!VATTR_IS_ACTIVE(vap, va_type))
3669 return (EINVAL);
3670 if (vap->va_type == VCHR || vap->va_type == VBLK) {
3671 if (!VATTR_IS_ACTIVE(vap, va_rdev))
3672 return (EINVAL);
2d21ac55 3673 rdev = vap->va_rdev;
91447636 3674 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1c79356b
A
3675 rdev = 0xffffffff;
3676 else {
91447636 3677 return (ENOTSUP);
1c79356b 3678 }
2d21ac55
A
3679 if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN))
3680 return (ENAMETOOLONG);
91447636 3681
6d2010ae
A
3682 nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx);
3683
91447636
A
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);
3692
2d21ac55
A
3693 nfsm_chain_null(&nmreq);
3694 nfsm_chain_null(&nmrep);
3695
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);
6d2010ae 3700 nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp);
2d21ac55
A
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);
1c79356b 3704 if (vap->va_type == VCHR || vap->va_type == VBLK) {
2d21ac55
A
3705 nfsm_chain_add_32(error, &nmreq, major(vap->va_rdev));
3706 nfsm_chain_add_32(error, &nmreq, minor(vap->va_rdev));
1c79356b
A
3707 }
3708 } else {
2d21ac55 3709 nfsm_chain_add_v2sattr(error, &nmreq, vap, rdev);
91447636 3710 }
2d21ac55 3711 nfsm_chain_build_done(error, &nmreq);
b0d623f7
A
3712 if (!error)
3713 error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx));
2d21ac55
A
3714 nfsmout_if(error);
3715
6d2010ae
A
3716 error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_MKNOD,
3717 vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req);
3718 if (!error)
3719 error = nfs_request_async_finish(req, &nmrep, &xid, &status);
2d21ac55 3720
b0d623f7
A
3721 if ((lockerror = nfs_node_lock(dnp)))
3722 error = lockerror;
91447636 3723 /* XXX no EEXIST kludge here? */
2d21ac55
A
3724 dxid = xid;
3725 if (!error && !status) {
3726 if (dnp->n_flag & NNEGNCENTRIES) {
3727 dnp->n_flag &= ~NNEGNCENTRIES;
3728 cache_purge_negatives(dvp);
3729 }
3730 error = nfsm_chain_get_fh_attr(&nmrep, dnp, ctx, nfsvers, &xid, &fh, &nvattr);
3731 }
3732 if (nfsvers == NFS_VER3)
3733 nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid);
3734 if (!error)
3735 error = status;
3736nfsmout:
3737 nfsm_chain_cleanup(&nmreq);
3738 nfsm_chain_cleanup(&nmrep);
3739
3740 if (!lockerror) {
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);
b0d623f7
A
3745 nfs_node_unlock(dnp);
3746 /* nfs_getattr() will check changed and purge caches */
6d2010ae 3747 nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED);
1c79356b 3748 }
2d21ac55
A
3749
3750 if (!error && fh.fh_len)
6d2010ae 3751 error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np);
2d21ac55
A
3752 if (!error && !np)
3753 error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np);
3754 if (!error && np)
3755 newvp = NFSTOV(np);
b0d623f7
A
3756 if (!busyerror)
3757 nfs_node_clear_busy(dnp);
2d21ac55 3758
91447636 3759 if (!error && (gotuid || gotgid) &&
6d2010ae 3760 (!newvp || nfs_getattrcache(np, &nvattr, 0) ||
91447636
A
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);
3766 }
1c79356b 3767 if (error) {
2d21ac55 3768 if (newvp) {
b0d623f7 3769 nfs_node_unlock(np);
91447636 3770 vnode_put(newvp);
2d21ac55 3771 }
1c79356b 3772 } else {
1c79356b 3773 *vpp = newvp;
b0d623f7 3774 nfs_node_unlock(np);
1c79356b 3775 }
1c79356b
A
3776 return (error);
3777}
3778
b0d623f7 3779static uint32_t create_verf;
1c79356b 3780/*
2d21ac55 3781 * NFS file create call
1c79356b 3782 */
b0d623f7 3783int
2d21ac55 3784nfs3_vnop_create(
91447636
A
3785 struct vnop_create_args /* {
3786 struct vnodeop_desc *a_desc;
3787 vnode_t a_dvp;
3788 vnode_t *a_vpp;
1c79356b 3789 struct componentname *a_cnp;
91447636
A
3790 struct vnode_attr *a_vap;
3791 vfs_context_t a_context;
2d21ac55 3792 } */ *ap)
1c79356b 3793{
2d21ac55 3794 vfs_context_t ctx = ap->a_context;
91447636
A
3795 vnode_t dvp = ap->a_dvp;
3796 struct vnode_attr *vap = ap->a_vap;
3797 struct componentname *cnp = ap->a_cnp;
6d2010ae 3798 struct nfs_vattr nvattr;
2d21ac55
A
3799 fhandle_t fh;
3800 nfsnode_t np = NULL;
3801 struct nfsmount *nmp;
3802 nfsnode_t dnp = VTONFS(dvp);
3803 vnode_t newvp = NULL;
b0d623f7 3804 int error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0, fmode = 0;
91447636 3805 struct timespec premtime = { 0, 0 };
2d21ac55
A
3806 int nfsvers, gotuid, gotgid;
3807 u_int64_t xid, dxid;
3808 uint32_t val;
3809 struct nfsm_chain nmreq, nmrep;
6d2010ae 3810 struct nfsreq rq, *req = &rq;
2d21ac55 3811 struct nfs_dulookup dul;
91447636 3812
2d21ac55 3813 nmp = VTONMP(dvp);
fe8ab488 3814 if (nfs_mount_gone(nmp))
2d21ac55
A
3815 return (ENXIO);
3816 nfsvers = nmp->nm_vers;
1c79356b 3817
2d21ac55
A
3818 if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN))
3819 return (ENAMETOOLONG);
91447636 3820
6d2010ae
A
3821 nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx);
3822
91447636
A
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);
1c79356b 3831
6d2010ae 3832 if (vap->va_vaflags & VA_EXCLUSIVE) {
1c79356b 3833 fmode |= O_EXCL;
6d2010ae
A
3834 if (!VATTR_IS_ACTIVE(vap, va_access_time) || !VATTR_IS_ACTIVE(vap, va_modify_time))
3835 vap->va_vaflags |= VA_UTIMES_NULL;
3836 }
2d21ac55 3837
1c79356b 3838again:
b0d623f7
A
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);
2d21ac55
A
3841
3842 nfsm_chain_null(&nmreq);
3843 nfsm_chain_null(&nmrep);
3844
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);
6d2010ae 3849 nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp);
2d21ac55 3850 if (nfsvers == NFS_VER3) {
1c79356b 3851 if (fmode & O_EXCL) {
2d21ac55 3852 nfsm_chain_add_32(error, &nmreq, NFS_CREATE_EXCLUSIVE);
b0d623f7 3853 lck_rw_lock_shared(in_ifaddr_rwlock);
2d21ac55
A
3854 if (!TAILQ_EMPTY(&in_ifaddrhead))
3855 val = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr.s_addr;
3856 else
3857 val = create_verf;
b0d623f7 3858 lck_rw_done(in_ifaddr_rwlock);
2d21ac55
A
3859 nfsm_chain_add_32(error, &nmreq, val);
3860 ++create_verf;
3861 nfsm_chain_add_32(error, &nmreq, create_verf);
1c79356b 3862 } else {
2d21ac55
A
3863 nfsm_chain_add_32(error, &nmreq, NFS_CREATE_UNCHECKED);
3864 nfsm_chain_add_v3sattr(error, &nmreq, vap);
1c79356b
A
3865 }
3866 } else {
2d21ac55 3867 nfsm_chain_add_v2sattr(error, &nmreq, vap, 0);
1c79356b 3868 }
2d21ac55
A
3869 nfsm_chain_build_done(error, &nmreq);
3870 nfsmout_if(error);
2d21ac55
A
3871
3872 error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_CREATE,
6d2010ae 3873 vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req);
1c79356b 3874 if (!error) {
2d21ac55
A
3875 nfs_dulookup_start(&dul, dnp, ctx);
3876 error = nfs_request_async_finish(req, &nmrep, &xid, &status);
3877 }
3878
b0d623f7
A
3879 if ((lockerror = nfs_node_lock(dnp)))
3880 error = lockerror;
2d21ac55
A
3881 dxid = xid;
3882 if (!error && !status) {
3883 if (dnp->n_flag & NNEGNCENTRIES) {
3884 dnp->n_flag &= ~NNEGNCENTRIES;
3885 cache_purge_negatives(dvp);
3886 }
3887 error = nfsm_chain_get_fh_attr(&nmrep, dnp, ctx, nfsvers, &xid, &fh, &nvattr);
3888 }
3889 if (nfsvers == NFS_VER3)
3890 nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid);
3891 if (!error)
3892 error = status;
3893nfsmout:
3894 nfsm_chain_cleanup(&nmreq);
3895 nfsm_chain_cleanup(&nmrep);
3896
3897 if (!lockerror) {
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);
b0d623f7
A
3902 nfs_node_unlock(dnp);
3903 /* nfs_getattr() will check changed and purge caches */
6d2010ae 3904 nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED);
1c79356b 3905 }
2d21ac55
A
3906
3907 if (!error && fh.fh_len)
6d2010ae 3908 error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np);
2d21ac55
A
3909 if (!error && !np)
3910 error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np);
3911 if (!error && np)
3912 newvp = NFSTOV(np);
3913
3914 nfs_dulookup_finish(&dul, dnp, ctx);
b0d623f7
A
3915 if (!busyerror)
3916 nfs_node_clear_busy(dnp);
2d21ac55 3917
1c79356b 3918 if (error) {
2d21ac55 3919 if ((nfsvers == NFS_VER3) && (fmode & O_EXCL) && (error == NFSERR_NOTSUPP)) {
1c79356b
A
3920 fmode &= ~O_EXCL;
3921 goto again;
3922 }
2d21ac55 3923 if (newvp) {
b0d623f7 3924 nfs_node_unlock(np);
91447636 3925 vnode_put(newvp);
2d21ac55
A
3926 }
3927 } else if ((nfsvers == NFS_VER3) && (fmode & O_EXCL)) {
b0d623f7
A
3928 nfs_node_unlock(np);
3929 error = nfs3_setattr_rpc(np, vap, ctx);
91447636
A
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);
b0d623f7 3935 error = nfs3_setattr_rpc(np, vap, ctx);
91447636 3936 }
b0d623f7 3937 if (error)
91447636 3938 vnode_put(newvp);
b0d623f7
A
3939 else
3940 nfs_node_lock_force(np);
ccc36f2f 3941 }
2d21ac55 3942 if (!error)
1c79356b 3943 *ap->a_vpp = newvp;
91447636 3944 if (!error && (gotuid || gotgid) &&
6d2010ae 3945 (!newvp || nfs_getattrcache(np, &nvattr, 0) ||
91447636
A
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);
3951 }
2d21ac55 3952 if (!error)
b0d623f7 3953 nfs_node_unlock(np);
1c79356b
A
3954 return (error);
3955}
3956
3957/*
2d21ac55
A
3958 * NFS file remove call
3959 * To try and make NFS semantics closer to UFS semantics, a file that has
1c79356b
A
3960 * other processes using the vnode is renamed instead of removed and then
3961 * removed later on the last close.
91447636 3962 * - If vnode_isinuse()
1c79356b
A
3963 * If a rename is not already in the works
3964 * call nfs_sillyrename() to set it up
3965 * else
2d21ac55 3966 * do the remove RPC
1c79356b 3967 */
b0d623f7 3968int
2d21ac55 3969nfs_vnop_remove(
91447636 3970 struct vnop_remove_args /* {
1c79356b 3971 struct vnodeop_desc *a_desc;
91447636
A
3972 vnode_t a_dvp;
3973 vnode_t a_vp;
3974 struct componentname *a_cnp;
3975 int a_flags;
3976 vfs_context_t a_context;
2d21ac55 3977 } */ *ap)
1c79356b 3978{
2d21ac55 3979 vfs_context_t ctx = ap->a_context;
91447636
A
3980 vnode_t vp = ap->a_vp;
3981 vnode_t dvp = ap->a_dvp;
3982 struct componentname *cnp = ap->a_cnp;
2d21ac55
A
3983 nfsnode_t dnp = VTONFS(dvp);
3984 nfsnode_t np = VTONFS(vp);
6d2010ae 3985 int error = 0, nfsvers, namedattrs, inuse, gotattr = 0, flushed = 0, setsize = 0;
91447636 3986 struct nfs_vattr nvattr;
2d21ac55
A
3987 struct nfsmount *nmp;
3988 struct nfs_dulookup dul;
3989
3990 /* XXX prevent removing a sillyrenamed file? */
3991
3992 nmp = NFSTONMP(dnp);
fe8ab488 3993 if (nfs_mount_gone(nmp))
2d21ac55
A
3994 return (ENXIO);
3995 nfsvers = nmp->nm_vers;
6d2010ae 3996 namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR);
2d21ac55
A
3997
3998again_relock:
b0d623f7 3999 error = nfs_node_set_busy2(dnp, np, vfs_context_thread(ctx));
2d21ac55
A
4000 if (error)
4001 return (error);
1c79356b 4002
2d21ac55
A
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);
4008 }
4009 np->n_hflag |= NHLOCKED;
4010 lck_mtx_unlock(nfs_node_hash_mutex);
9bccf70c 4011
6d2010ae
A
4012 if (!namedattrs)
4013 nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx);
2d21ac55
A
4014again:
4015 inuse = vnode_isinuse(vp, 0);
4016 if ((ap->a_flags & VNODE_REMOVE_NODELETEBUSY) && inuse) {
55e303ae 4017 /* Caller requested Carbon delete semantics, but file is busy */
2d21ac55
A
4018 error = EBUSY;
4019 goto out;
55e303ae 4020 }
2d21ac55 4021 if (inuse && !gotattr) {
b0d623f7 4022 if (nfs_getattr(np, &nvattr, ctx, NGA_CACHED))
2d21ac55
A
4023 nvattr.nva_nlink = 1;
4024 gotattr = 1;
4025 goto again;
4026 }
4027 if (!inuse || (np->n_sillyrename && (nvattr.nva_nlink > 1))) {
4028
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;
4035 wakeup(np);
4036 }
4037 lck_mtx_unlock(nfs_node_hash_mutex);
b0d623f7 4038 nfs_node_clear_busy2(dnp, np);
2d21ac55
A
4039 error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1);
4040 FSDBG(260, np, np->n_size, np->n_vattr.nva_size, 0xf00d0011);
4041 flushed = 1;
4042 if (error == EINTR) {
b0d623f7 4043 nfs_node_lock_force(np);
2d21ac55 4044 NATTRINVALIDATE(np);
b0d623f7 4045 nfs_node_unlock(np);
2d21ac55
A
4046 return (error);
4047 }
6d2010ae
A
4048 if (!namedattrs)
4049 nfs_dulookup_finish(&dul, dnp, ctx);
2d21ac55
A
4050 goto again_relock;
4051 }
4052
6d2010ae
A
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));
b0d623f7 4055
1c79356b
A
4056 /*
4057 * Purge the name cache so that the chance of a lookup for
4058 * the name succeeding while the remove is in progress is
483a1d10 4059 * minimized.
1c79356b 4060 */
b0d623f7 4061 nfs_name_cache_purge(dnp, np, cnp, ctx);
2d21ac55 4062
6d2010ae
A
4063 if (!namedattrs)
4064 nfs_dulookup_start(&dul, dnp, ctx);
2d21ac55 4065
1c79356b 4066 /* Do the rpc */
2d21ac55
A
4067 error = nmp->nm_funcs->nf_remove_rpc(dnp, cnp->cn_nameptr, cnp->cn_namelen,
4068 vfs_context_thread(ctx), vfs_context_ucred(ctx));
4069
1c79356b
A
4070 /*
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.
4075 */
4076 if (error == ENOENT)
4077 error = 0;
2d21ac55
A
4078
4079 if (!error && !inuse && !np->n_sillyrename) {
55e303ae 4080 /*
2d21ac55 4081 * removal succeeded, it's not in use, and not silly renamed so
55e303ae
A
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
4085 */
91447636 4086 lck_mtx_lock(nfs_node_hash_mutex);
2d21ac55
A
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);
4091 }
91447636 4092 lck_mtx_unlock(nfs_node_hash_mutex);
2d21ac55 4093 /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */
91447636 4094 /* clear all flags other than these */
b0d623f7 4095 nfs_node_lock_force(np);
2d21ac55 4096 np->n_flag &= (NMODIFIED);
2d21ac55 4097 NATTRINVALIDATE(np);
b0d623f7
A
4098 nfs_node_unlock(np);
4099 vnode_recycle(vp);
2d21ac55
A
4100 setsize = 1;
4101 } else {
b0d623f7 4102 nfs_node_lock_force(np);
2d21ac55 4103 NATTRINVALIDATE(np);
b0d623f7 4104 nfs_node_unlock(np);
55e303ae 4105 }
1c79356b 4106 } else if (!np->n_sillyrename) {
6d2010ae
A
4107 if (!namedattrs)
4108 nfs_dulookup_start(&dul, dnp, ctx);
2d21ac55 4109 error = nfs_sillyrename(dnp, np, cnp, ctx);
b0d623f7 4110 nfs_node_lock_force(np);
2d21ac55 4111 NATTRINVALIDATE(np);
b0d623f7 4112 nfs_node_unlock(np);
2d21ac55 4113 } else {
b0d623f7 4114 nfs_node_lock_force(np);
2d21ac55 4115 NATTRINVALIDATE(np);
b0d623f7 4116 nfs_node_unlock(np);
6d2010ae
A
4117 if (!namedattrs)
4118 nfs_dulookup_start(&dul, dnp, ctx);
1c79356b 4119 }
1c79356b 4120
b0d623f7 4121 /* nfs_getattr() will check changed and purge caches */
6d2010ae
A
4122 nfs_getattr(dnp, NULL, ctx, NGA_CACHED);
4123 if (!namedattrs)
4124 nfs_dulookup_finish(&dul, dnp, ctx);
2d21ac55
A
4125out:
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;
4131 wakeup(np);
4132 }
4133 lck_mtx_unlock(nfs_node_hash_mutex);
b0d623f7 4134 nfs_node_clear_busy2(dnp, np);
2d21ac55
A
4135 if (setsize)
4136 ubc_setsize(vp, 0);
1c79356b
A
4137 return (error);
4138}
4139
4140/*
2d21ac55 4141 * NFS silly-renamed file removal function called from nfs_vnop_inactive
1c79356b
A
4142 */
4143int
2d21ac55 4144nfs_removeit(struct nfs_sillyrename *nsp)
1c79356b 4145{
2d21ac55 4146 struct nfsmount *nmp = NFSTONMP(nsp->nsr_dnp);
fe8ab488 4147 if (nfs_mount_gone(nmp))
2d21ac55
A
4148 return (ENXIO);
4149 return nmp->nm_funcs->nf_remove_rpc(nsp->nsr_dnp, nsp->nsr_name, nsp->nsr_namlen, NULL, nsp->nsr_cred);
1c79356b
A
4150}
4151
4152/*
2d21ac55 4153 * NFS remove rpc, called from nfs_remove() and nfs_removeit().
1c79356b 4154 */
2d21ac55
A
4155int
4156nfs3_remove_rpc(
4157 nfsnode_t dnp,
4158 char *name,
4159 int namelen,
4160 thread_t thd,
4161 kauth_cred_t cred)
1c79356b 4162{
b0d623f7 4163 int error = 0, lockerror = ENOENT, status, wccpostattr = 0;
91447636 4164 struct timespec premtime = { 0, 0 };
2d21ac55
A
4165 struct nfsmount *nmp;
4166 int nfsvers;
fa4905b1 4167 u_int64_t xid;
2d21ac55 4168 struct nfsm_chain nmreq, nmrep;
1c79356b 4169
2d21ac55 4170 nmp = NFSTONMP(dnp);
fe8ab488 4171 if (nfs_mount_gone(nmp))
55e303ae 4172 return (ENXIO);
2d21ac55
A
4173 nfsvers = nmp->nm_vers;
4174 if ((nfsvers == NFS_VER2) && (namelen > NFS_MAXNAMLEN))
4175 return (ENAMETOOLONG);
55e303ae 4176
2d21ac55
A
4177 nfsm_chain_null(&nmreq);
4178 nfsm_chain_null(&nmrep);
4179
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);
6d2010ae 4183 nfsm_chain_add_name(error, &nmreq, name, namelen, nmp);
2d21ac55
A
4184 nfsm_chain_build_done(error, &nmreq);
4185 nfsmout_if(error);
4186
6d2010ae 4187 error = nfs_request2(dnp, NULL, &nmreq, NFSPROC_REMOVE, thd, cred, NULL, 0, &nmrep, &xid, &status);
2d21ac55 4188
b0d623f7
A
4189 if ((lockerror = nfs_node_lock(dnp)))
4190 error = lockerror;
2d21ac55
A
4191 if (nfsvers == NFS_VER3)
4192 nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &xid);
b0d623f7 4193 nfsmout_if(error);
2d21ac55 4194 dnp->n_flag |= NMODIFIED;
483a1d10 4195 /* if directory hadn't changed, update namecache mtime */
2d21ac55
A
4196 if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==))
4197 NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr);
483a1d10 4198 if (!wccpostattr)
2d21ac55
A
4199 NATTRINVALIDATE(dnp);
4200 if (!error)
4201 error = status;
4202nfsmout:
b0d623f7
A
4203 if (!lockerror)
4204 nfs_node_unlock(dnp);
2d21ac55
A
4205 nfsm_chain_cleanup(&nmreq);
4206 nfsm_chain_cleanup(&nmrep);
1c79356b
A
4207 return (error);
4208}
4209
4210/*
2d21ac55 4211 * NFS file rename call
1c79356b 4212 */
b0d623f7 4213int
2d21ac55 4214nfs_vnop_rename(
91447636
A
4215 struct vnop_rename_args /* {
4216 struct vnodeop_desc *a_desc;
4217 vnode_t a_fdvp;
4218 vnode_t a_fvp;
1c79356b 4219 struct componentname *a_fcnp;
91447636
A
4220 vnode_t a_tdvp;
4221 vnode_t a_tvp;
1c79356b 4222 struct componentname *a_tcnp;
91447636 4223 vfs_context_t a_context;
2d21ac55 4224 } */ *ap)
1c79356b 4225{
2d21ac55 4226 vfs_context_t ctx = ap->a_context;
91447636 4227 vnode_t fdvp = ap->a_fdvp;
2d21ac55 4228 vnode_t fvp = ap->a_fvp;
91447636 4229 vnode_t tdvp = ap->a_tdvp;
2d21ac55
A
4230 vnode_t tvp = ap->a_tvp;
4231 nfsnode_t fdnp, fnp, tdnp, tnp;
91447636
A
4232 struct componentname *tcnp = ap->a_tcnp;
4233 struct componentname *fcnp = ap->a_fcnp;
2d21ac55 4234 int error, nfsvers, inuse=0, tvprecycle=0, locked=0;
91447636 4235 mount_t fmp, tdmp, tmp;
2d21ac55
A
4236 struct nfs_vattr nvattr;
4237 struct nfsmount *nmp;
91447636 4238
2d21ac55
A
4239 fdnp = VTONFS(fdvp);
4240 fnp = VTONFS(fvp);
4241 tdnp = VTONFS(tdvp);
91447636 4242 tnp = tvp ? VTONFS(tvp) : NULL;
1c79356b 4243
2d21ac55 4244 nmp = NFSTONMP(fdnp);
fe8ab488 4245 if (nfs_mount_gone(nmp))
2d21ac55
A
4246 return (ENXIO);
4247 nfsvers = nmp->nm_vers;
4248
b0d623f7 4249 error = nfs_node_set_busy4(fdnp, fnp, tdnp, tnp, vfs_context_thread(ctx));
2d21ac55
A
4250 if (error)
4251 return (error);
4252
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);
4259 }
4260 tnp->n_hflag |= NHLOCKED;
4261 lck_mtx_unlock(nfs_node_hash_mutex);
4262 locked = 1;
4263 }
4264
1c79356b 4265 /* Check for cross-device rename */
91447636
A
4266 fmp = vnode_mount(fvp);
4267 tmp = tvp ? vnode_mount(tvp) : NULL;
4268 tdmp = vnode_mount(tdvp);
4269 if ((fmp != tdmp) || (tvp && (fmp != tmp))) {
1c79356b
A
4270 error = EXDEV;
4271 goto out;
4272 }
4273
2d21ac55
A
4274 /* XXX prevent renaming from/over a sillyrenamed file? */
4275
1c79356b
A
4276 /*
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.
9bccf70c
A
4280 * Don't sillyrename if source and target are same vnode (hard
4281 * links or case-variants)
1c79356b 4282 */
2d21ac55 4283 if (tvp && (tvp != fvp))
91447636 4284 inuse = vnode_isinuse(tvp, 0);
2d21ac55
A
4285 if (inuse && !tnp->n_sillyrename && (vnode_vtype(tvp) != VDIR)) {
4286 error = nfs_sillyrename(tdnp, tnp, tcnp, ctx);
4287 if (error) {
9bccf70c
A
4288 /* sillyrename failed. Instead of pressing on, return error */
4289 goto out; /* should not be ENOENT. */
4290 } else {
4291 /* sillyrename succeeded.*/
9bccf70c
A
4292 tvp = NULL;
4293 }
b0d623f7 4294 } else if (tvp && (nmp->nm_vers >= NFS_VER4) && (tnp->n_openflags & N_DELEG_MASK)) {
6d2010ae 4295 nfs4_delegation_return(tnp, 0, vfs_context_thread(ctx), vfs_context_ucred(ctx));
1c79356b
A
4296 }
4297
2d21ac55
A
4298 error = nmp->nm_funcs->nf_rename_rpc(fdnp, fcnp->cn_nameptr, fcnp->cn_namelen,
4299 tdnp, tcnp->cn_nameptr, tcnp->cn_namelen, ctx);
1c79356b 4300
91447636
A
4301 /*
4302 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
4303 */
4304 if (error == ENOENT)
4305 error = 0;
4306
2d21ac55 4307 if (tvp && (tvp != fvp) && !tnp->n_sillyrename) {
b0d623f7 4308 nfs_node_lock_force(tnp);
2d21ac55 4309 tvprecycle = (!error && !vnode_isinuse(tvp, 0) &&
6d2010ae 4310 (nfs_getattrcache(tnp, &nvattr, 0) || (nvattr.nva_nlink == 1)));
b0d623f7 4311 nfs_node_unlock(tnp);
91447636 4312 lck_mtx_lock(nfs_node_hash_mutex);
2d21ac55
A
4313 if (tvprecycle && (tnp->n_hflag & NHHASHED)) {
4314 /*
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
4318 */
4319 LIST_REMOVE(tnp, n_hash);
4320 tnp->n_hflag &= ~NHHASHED;
4321 FSDBG(266, 0, tnp, tnp->n_flag, 0xb1eb1e);
4322 }
91447636 4323 lck_mtx_unlock(nfs_node_hash_mutex);
55e303ae 4324 }
2d21ac55 4325
91447636 4326 /* purge the old name cache entries and enter the new one */
b0d623f7 4327 nfs_name_cache_purge(fdnp, fnp, fcnp, ctx);
9bccf70c 4328 if (tvp) {
b0d623f7 4329 nfs_name_cache_purge(tdnp, tnp, tcnp, ctx);
2d21ac55
A
4330 if (tvprecycle) {
4331 /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */
91447636 4332 /* clear all flags other than these */
b0d623f7 4333 nfs_node_lock_force(tnp);
2d21ac55 4334 tnp->n_flag &= (NMODIFIED);
b0d623f7 4335 nfs_node_unlock(tnp);
91447636
A
4336 vnode_recycle(tvp);
4337 }
9bccf70c 4338 }
2d21ac55 4339 if (!error) {
b0d623f7 4340 nfs_node_lock_force(tdnp);
2d21ac55
A
4341 if (tdnp->n_flag & NNEGNCENTRIES) {
4342 tdnp->n_flag &= ~NNEGNCENTRIES;
4343 cache_purge_negatives(tdvp);
4344 }
b0d623f7
A
4345 nfs_node_unlock(tdnp);
4346 nfs_node_lock_force(fnp);
91447636 4347 cache_enter(tdvp, fvp, tcnp);
2d21ac55
A
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);
4353 }
4354 fnp->n_parent = tdvp;
4355 if (tdvp && !vnode_get(tdvp)) {
4356 /* add ref to new parent */
4357 vnode_ref(tdvp);
4358 vnode_put(tdvp);
4359 } else {
4360 fnp->n_parent = NULL;
4361 }
4362 }
b0d623f7 4363 nfs_node_unlock(fnp);
2d21ac55 4364 }
1c79356b 4365out:
b0d623f7 4366 /* nfs_getattr() will check changed and purge caches */
6d2010ae
A
4367 nfs_getattr(fdnp, NULL, ctx, NGA_CACHED);
4368 nfs_getattr(tdnp, NULL, ctx, NGA_CACHED);
2d21ac55
A
4369 if (locked) {
4370 /* unlock node */
4371 lck_mtx_lock(nfs_node_hash_mutex);
4372 tnp->n_hflag &= ~NHLOCKED;
4373 if (tnp->n_hflag & NHLOCKWANT) {
4374 tnp->n_hflag &= ~NHLOCKWANT;
4375 wakeup(tnp);
4376 }
4377 lck_mtx_unlock(nfs_node_hash_mutex);
4378 }
b0d623f7 4379 nfs_node_clear_busy4(fdnp, fnp, tdnp, tnp);
1c79356b
A
4380 return (error);
4381}
4382
4383/*
2d21ac55 4384 * Do an NFS rename rpc. Called from nfs_vnop_rename() and nfs_sillyrename().
1c79356b 4385 */
2d21ac55
A
4386int
4387nfs3_rename_rpc(
4388 nfsnode_t fdnp,
4389 char *fnameptr,
4390 int fnamelen,
4391 nfsnode_t tdnp,
4392 char *tnameptr,
4393 int tnamelen,
4394 vfs_context_t ctx)
1c79356b 4395{
b0d623f7 4396 int error = 0, lockerror = ENOENT, status, fwccpostattr = 0, twccpostattr = 0;
91447636 4397 struct timespec fpremtime = { 0, 0 }, tpremtime = { 0, 0 };
2d21ac55
A
4398 struct nfsmount *nmp;
4399 int nfsvers;
4400 u_int64_t xid, txid;
4401 struct nfsm_chain nmreq, nmrep;
1c79356b 4402
2d21ac55 4403 nmp = NFSTONMP(fdnp);
fe8ab488 4404 if (nfs_mount_gone(nmp))
55e303ae 4405 return (ENXIO);
2d21ac55
A
4406 nfsvers = nmp->nm_vers;
4407 if ((nfsvers == NFS_VER2) &&
4408 ((fnamelen > NFS_MAXNAMLEN) || (tnamelen > NFS_MAXNAMLEN)))
4409 return (ENAMETOOLONG);
4410
4411 nfsm_chain_null(&nmreq);
4412 nfsm_chain_null(&nmrep);
4413
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);
6d2010ae 4418 nfsm_chain_add_name(error, &nmreq, fnameptr, fnamelen, nmp);
2d21ac55 4419 nfsm_chain_add_fh(error, &nmreq, nfsvers, tdnp->n_fhp, tdnp->n_fhsize);
6d2010ae 4420 nfsm_chain_add_name(error, &nmreq, tnameptr, tnamelen, nmp);
2d21ac55
A
4421 nfsm_chain_build_done(error, &nmreq);
4422 nfsmout_if(error);
4423
6d2010ae 4424 error = nfs_request(fdnp, NULL, &nmreq, NFSPROC_RENAME, ctx, NULL, &nmrep, &xid, &status);
2d21ac55 4425
b0d623f7
A
4426 if ((lockerror = nfs_node_lock2(fdnp, tdnp)))
4427 error = lockerror;
2d21ac55
A
4428 if (nfsvers == NFS_VER3) {
4429 txid = xid;
4430 nfsm_chain_get_wcc_data(error, &nmrep, fdnp, &fpremtime, &fwccpostattr, &xid);
4431 nfsm_chain_get_wcc_data(error, &nmrep, tdnp, &tpremtime, &twccpostattr, &txid);
4432 }
4433 if (!error)
4434 error = status;
4435nfsmout:
4436 nfsm_chain_cleanup(&nmreq);
4437 nfsm_chain_cleanup(&nmrep);
b0d623f7
A
4438 if (!lockerror) {
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);
4443 if (!fwccpostattr)
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);
4449 if (!twccpostattr)
4450 NATTRINVALIDATE(tdnp);
4451 nfs_node_unlock2(fdnp, tdnp);
4452 }
1c79356b
A
4453 return (error);
4454}
4455
4456/*
2d21ac55 4457 * NFS hard link create call
1c79356b 4458 */
b0d623f7 4459int
2d21ac55 4460nfs3_vnop_link(
91447636
A
4461 struct vnop_link_args /* {
4462 struct vnodeop_desc *a_desc;
4463 vnode_t a_vp;
4464 vnode_t a_tdvp;
1c79356b 4465 struct componentname *a_cnp;
91447636 4466 vfs_context_t a_context;
2d21ac55 4467 } */ *ap)
1c79356b 4468{
2d21ac55 4469 vfs_context_t ctx = ap->a_context;
91447636
A
4470 vnode_t vp = ap->a_vp;
4471 vnode_t tdvp = ap->a_tdvp;
4472 struct componentname *cnp = ap->a_cnp;
b0d623f7 4473 int error = 0, lockerror = ENOENT, status, wccpostattr = 0, attrflag = 0;
91447636 4474 struct timespec premtime = { 0, 0 };
2d21ac55
A
4475 struct nfsmount *nmp;
4476 nfsnode_t np = VTONFS(vp);
4477 nfsnode_t tdnp = VTONFS(tdvp);
4478 int nfsvers;
4479 u_int64_t xid, txid;
4480 struct nfsm_chain nmreq, nmrep;
1c79356b 4481
2d21ac55 4482 if (vnode_mount(vp) != vnode_mount(tdvp))
1c79356b 4483 return (EXDEV);
1c79356b 4484
2d21ac55 4485 nmp = VTONMP(vp);
fe8ab488 4486 if (nfs_mount_gone(nmp))
2d21ac55
A
4487 return (ENXIO);
4488 nfsvers = nmp->nm_vers;
4489 if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN))
4490 return (ENAMETOOLONG);
55e303ae 4491
1c79356b
A
4492 /*
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!
4496 */
2d21ac55 4497 nfs_flush(np, MNT_WAIT, vfs_context_thread(ctx), V_IGNORE_WRITEERR);
1c79356b 4498
b0d623f7 4499 error = nfs_node_set_busy2(tdnp, np, vfs_context_thread(ctx));
91447636
A
4500 if (error)
4501 return (error);
fa4905b1 4502
2d21ac55
A
4503 nfsm_chain_null(&nmreq);
4504 nfsm_chain_null(&nmrep);
4505
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);
6d2010ae 4510 nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp);
2d21ac55
A
4511 nfsm_chain_build_done(error, &nmreq);
4512 nfsmout_if(error);
6d2010ae 4513 error = nfs_request(np, NULL, &nmreq, NFSPROC_LINK, ctx, NULL, &nmrep, &xid, &status);
b0d623f7
A
4514
4515 if ((lockerror = nfs_node_lock2(tdnp, np))) {
4516 error = lockerror;
4517 goto nfsmout;
4518 }
2d21ac55
A
4519 if (nfsvers == NFS_VER3) {
4520 txid = xid;
4521 nfsm_chain_postop_attr_update_flag(error, &nmrep, np, attrflag, &xid);
4522 nfsm_chain_get_wcc_data(error, &nmrep, tdnp, &premtime, &wccpostattr, &txid);
1c79356b 4523 }
2d21ac55
A
4524 if (!error)
4525 error = status;
4526nfsmout:
4527 nfsm_chain_cleanup(&nmreq);
4528 nfsm_chain_cleanup(&nmrep);
b0d623f7
A
4529 if (!lockerror) {
4530 if (!attrflag)
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);
4536 if (!wccpostattr)
4537 NATTRINVALIDATE(tdnp);
4538 if (!error && (tdnp->n_flag & NNEGNCENTRIES)) {
4539 tdnp->n_flag &= ~NNEGNCENTRIES;
4540 cache_purge_negatives(tdvp);
4541 }
4542 nfs_node_unlock2(tdnp, np);
2d21ac55 4543 }
b0d623f7 4544 nfs_node_clear_busy2(tdnp, np);
1c79356b
A
4545 /*
4546 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
4547 */
4548 if (error == EEXIST)
4549 error = 0;
4550 return (error);
4551}
4552
4553/*
2d21ac55 4554 * NFS symbolic link create call
1c79356b 4555 */
b0d623f7 4556int
2d21ac55 4557nfs3_vnop_symlink(
91447636
A
4558 struct vnop_symlink_args /* {
4559 struct vnodeop_desc *a_desc;
4560 vnode_t a_dvp;
4561 vnode_t *a_vpp;
1c79356b 4562 struct componentname *a_cnp;
91447636 4563 struct vnode_attr *a_vap;
1c79356b 4564 char *a_target;
91447636 4565 vfs_context_t a_context;
2d21ac55 4566 } */ *ap)
1c79356b 4567{
2d21ac55 4568 vfs_context_t ctx = ap->a_context;
91447636
A
4569 vnode_t dvp = ap->a_dvp;
4570 struct vnode_attr *vap = ap->a_vap;
4571 struct componentname *cnp = ap->a_cnp;
6d2010ae 4572 struct nfs_vattr nvattr;
2d21ac55 4573 fhandle_t fh;
b0d623f7 4574 int slen, error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0;
91447636 4575 struct timespec premtime = { 0, 0 };
2d21ac55
A
4576 vnode_t newvp = NULL;
4577 int nfsvers, gotuid, gotgid;
39236c6e 4578 u_int64_t xid = 0, dxid;
2d21ac55
A
4579 nfsnode_t np = NULL;
4580 nfsnode_t dnp = VTONFS(dvp);
4581 struct nfsmount *nmp;
4582 struct nfsm_chain nmreq, nmrep;
6d2010ae 4583 struct nfsreq rq, *req = &rq;
2d21ac55 4584 struct nfs_dulookup dul;
91447636 4585
2d21ac55 4586 nmp = VTONMP(dvp);
fe8ab488 4587 if (nfs_mount_gone(nmp))
2d21ac55
A
4588 return (ENXIO);
4589 nfsvers = nmp->nm_vers;
1c79356b 4590
1c79356b 4591 slen = strlen(ap->a_target);
2d21ac55
A
4592 if ((nfsvers == NFS_VER2) &&
4593 ((cnp->cn_namelen > NFS_MAXNAMLEN) || (slen > NFS_MAXPATHLEN)))
4594 return (ENAMETOOLONG);
91447636 4595
6d2010ae
A
4596 nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx);
4597
91447636
A
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);
4606
b0d623f7
A
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);
2d21ac55
A
4609
4610 nfsm_chain_null(&nmreq);
4611 nfsm_chain_null(&nmrep);
4612
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);
6d2010ae 4617 nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp);
2d21ac55
A
4618 if (nfsvers == NFS_VER3)
4619 nfsm_chain_add_v3sattr(error, &nmreq, vap);
6d2010ae 4620 nfsm_chain_add_name(error, &nmreq, ap->a_target, slen, nmp);
2d21ac55
A
4621 if (nfsvers == NFS_VER2)
4622 nfsm_chain_add_v2sattr(error, &nmreq, vap, -1);
4623 nfsm_chain_build_done(error, &nmreq);
4624 nfsmout_if(error);
2d21ac55
A
4625
4626 error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_SYMLINK,
6d2010ae 4627 vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req);
2d21ac55
A
4628 if (!error) {
4629 nfs_dulookup_start(&dul, dnp, ctx);
4630 error = nfs_request_async_finish(req, &nmrep, &xid, &status);
4631 }
4632
b0d623f7
A
4633 if ((lockerror = nfs_node_lock(dnp)))
4634 error = lockerror;
2d21ac55
A
4635 dxid = xid;
4636 if (!error && !status) {
4637 if (dnp->n_flag & NNEGNCENTRIES) {
4638 dnp->n_flag &= ~NNEGNCENTRIES;
4639 cache_purge_negatives(dvp);
91447636 4640 }
2d21ac55
A
4641 if (nfsvers == NFS_VER3)
4642 error = nfsm_chain_get_fh_attr(&nmrep, dnp, ctx, nfsvers, &xid, &fh, &nvattr);
4643 else
4644 fh.fh_len = 0;
1c79356b 4645 }
2d21ac55
A
4646 if (nfsvers == NFS_VER3)
4647 nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid);
4648 if (!error)
4649 error = status;
4650nfsmout:
4651 nfsm_chain_cleanup(&nmreq);
4652 nfsm_chain_cleanup(&nmrep);
fa4905b1 4653
2d21ac55
A
4654 if (!lockerror) {
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);
b0d623f7
A
4659 nfs_node_unlock(dnp);
4660 /* nfs_getattr() will check changed and purge caches */
6d2010ae 4661 nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED);
1c79356b 4662 }
55e303ae 4663
2d21ac55 4664 if (!error && fh.fh_len)
6d2010ae 4665 error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np);
2d21ac55
A
4666 if (!error && np)
4667 newvp = NFSTOV(np);
4668
4669 nfs_dulookup_finish(&dul, dnp, ctx);
91447636 4670
1c79356b 4671 /*
91447636
A
4672 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
4673 * if we can succeed in looking up the symlink.
1c79356b 4674 */
2d21ac55 4675 if ((error == EEXIST) || (!error && !newvp)) {
91447636 4676 if (newvp) {
b0d623f7 4677 nfs_node_unlock(np);
91447636
A
4678 vnode_put(newvp);
4679 newvp = NULL;
4680 }
2d21ac55 4681 error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np);
91447636
A
4682 if (!error) {
4683 newvp = NFSTOV(np);
4684 if (vnode_vtype(newvp) != VLNK)
4685 error = EEXIST;
4686 }
4687 }
b0d623f7
A
4688 if (!busyerror)
4689 nfs_node_clear_busy(dnp);
91447636 4690 if (!error && (gotuid || gotgid) &&
6d2010ae 4691 (!newvp || nfs_getattrcache(np, &nvattr, 0) ||
91447636
A
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);
4697 }
4698 if (error) {
2d21ac55 4699 if (newvp) {
b0d623f7 4700 nfs_node_unlock(np);
91447636 4701 vnode_put(newvp);
2d21ac55 4702 }
91447636 4703 } else {
b0d623f7 4704 nfs_node_unlock(np);
91447636
A
4705 *ap->a_vpp = newvp;
4706 }
1c79356b
A
4707 return (error);
4708}
4709
4710/*
2d21ac55 4711 * NFS make dir call
1c79356b 4712 */
b0d623f7 4713int
2d21ac55 4714nfs3_vnop_mkdir(
91447636
A
4715 struct vnop_mkdir_args /* {
4716 struct vnodeop_desc *a_desc;
4717 vnode_t a_dvp;
4718 vnode_t *a_vpp;
1c79356b 4719 struct componentname *a_cnp;
91447636
A
4720 struct vnode_attr *a_vap;
4721 vfs_context_t a_context;
2d21ac55 4722 } */ *ap)
1c79356b 4723{
2d21ac55 4724 vfs_context_t ctx = ap->a_context;
91447636
A
4725 vnode_t dvp = ap->a_dvp;
4726 struct vnode_attr *vap = ap->a_vap;
4727 struct componentname *cnp = ap->a_cnp;
6d2010ae 4728 struct nfs_vattr nvattr;
2d21ac55
A
4729 nfsnode_t np = NULL;
4730 struct nfsmount *nmp;
4731 nfsnode_t dnp = VTONFS(dvp);
4732 vnode_t newvp = NULL;
b0d623f7 4733 int error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0;
91447636 4734 struct timespec premtime = { 0, 0 };
2d21ac55 4735 int nfsvers, gotuid, gotgid;
39236c6e 4736 u_int64_t xid= 0, dxid;
2d21ac55
A
4737 fhandle_t fh;
4738 struct nfsm_chain nmreq, nmrep;
6d2010ae 4739 struct nfsreq rq, *req = &rq;
2d21ac55 4740 struct nfs_dulookup dul;
1c79356b 4741
2d21ac55 4742 nmp = VTONMP(dvp);
fe8ab488 4743 if (nfs_mount_gone(nmp))
2d21ac55
A
4744 return (ENXIO);
4745 nfsvers = nmp->nm_vers;
4746 if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN))
4747 return (ENAMETOOLONG);
91447636 4748
6d2010ae
A
4749 nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx);
4750
91447636
A
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);
4759
b0d623f7
A
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);
2d21ac55
A
4762
4763 nfsm_chain_null(&nmreq);
4764 nfsm_chain_null(&nmrep);
4765
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);
6d2010ae 4770 nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp);
2d21ac55
A
4771 if (nfsvers == NFS_VER3)
4772 nfsm_chain_add_v3sattr(error, &nmreq, vap);
4773 else
4774 nfsm_chain_add_v2sattr(error, &nmreq, vap, -1);
4775 nfsm_chain_build_done(error, &nmreq);
4776 nfsmout_if(error);
2d21ac55
A
4777
4778 error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_MKDIR,
6d2010ae 4779 vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req);
2d21ac55
A
4780 if (!error) {
4781 nfs_dulookup_start(&dul, dnp, ctx);
4782 error = nfs_request_async_finish(req, &nmrep, &xid, &status);
1c79356b 4783 }
2d21ac55 4784
b0d623f7
A
4785 if ((lockerror = nfs_node_lock(dnp)))
4786 error = lockerror;
fa4905b1 4787 dxid = xid;
2d21ac55
A
4788 if (!error && !status) {
4789 if (dnp->n_flag & NNEGNCENTRIES) {
4790 dnp->n_flag &= ~NNEGNCENTRIES;
4791 cache_purge_negatives(dvp);
4792 }
4793 error = nfsm_chain_get_fh_attr(&nmrep, dnp, ctx, nfsvers, &xid, &fh, &nvattr);
4794 }
4795 if (nfsvers == NFS_VER3)
4796 nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &dxid);
1c79356b 4797 if (!error)
2d21ac55
A
4798 error = status;
4799nfsmout:
4800 nfsm_chain_cleanup(&nmreq);
4801 nfsm_chain_cleanup(&nmrep);
4802
4803 if (!lockerror) {
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);
b0d623f7
A
4808 nfs_node_unlock(dnp);
4809 /* nfs_getattr() will check changed and purge caches */
6d2010ae 4810 nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED);
2d21ac55
A
4811 }
4812
4813 if (!error && fh.fh_len)
6d2010ae 4814 error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np);
2d21ac55
A
4815 if (!error && np)
4816 newvp = NFSTOV(np);
4817
4818 nfs_dulookup_finish(&dul, dnp, ctx);
4819
1c79356b
A
4820 /*
4821 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
4822 * if we can succeed in looking up the directory.
4823 */
b0d623f7 4824 if ((error == EEXIST) || (!error && !newvp)) {
1c79356b 4825 if (newvp) {
b0d623f7 4826 nfs_node_unlock(np);
91447636
A
4827 vnode_put(newvp);
4828 newvp = NULL;
1c79356b 4829 }
2d21ac55 4830 error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np);
1c79356b
A
4831 if (!error) {
4832 newvp = NFSTOV(np);
91447636 4833 if (vnode_vtype(newvp) != VDIR)
1c79356b
A
4834 error = EEXIST;
4835 }
4836 }
b0d623f7
A
4837 if (!busyerror)
4838 nfs_node_clear_busy(dnp);
91447636 4839 if (!error && (gotuid || gotgid) &&
6d2010ae 4840 (!newvp || nfs_getattrcache(np, &nvattr, 0) ||
91447636
A
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);
4846 }
1c79356b 4847 if (error) {
2d21ac55 4848 if (newvp) {
b0d623f7 4849 nfs_node_unlock(np);
91447636 4850 vnode_put(newvp);
2d21ac55 4851 }
483a1d10 4852 } else {
b0d623f7 4853 nfs_node_unlock(np);
1c79356b 4854 *ap->a_vpp = newvp;
483a1d10 4855 }
1c79356b
A
4856 return (error);
4857}
4858
4859/*
2d21ac55 4860 * NFS remove directory call
1c79356b 4861 */
b0d623f7 4862int
2d21ac55 4863nfs3_vnop_rmdir(
91447636
A
4864 struct vnop_rmdir_args /* {
4865 struct vnodeop_desc *a_desc;
4866 vnode_t a_dvp;
4867 vnode_t a_vp;
1c79356b 4868 struct componentname *a_cnp;
91447636 4869 vfs_context_t a_context;
2d21ac55 4870 } */ *ap)
1c79356b 4871{
2d21ac55 4872 vfs_context_t ctx = ap->a_context;
91447636
A
4873 vnode_t vp = ap->a_vp;
4874 vnode_t dvp = ap->a_dvp;
4875 struct componentname *cnp = ap->a_cnp;
b0d623f7 4876 int error = 0, lockerror = ENOENT, status, wccpostattr = 0;
91447636 4877 struct timespec premtime = { 0, 0 };
2d21ac55
A
4878 struct nfsmount *nmp;
4879 nfsnode_t np = VTONFS(vp);
4880 nfsnode_t dnp = VTONFS(dvp);
2d21ac55 4881 int nfsvers;
fa4905b1 4882 u_int64_t xid;
2d21ac55 4883 struct nfsm_chain nmreq, nmrep;
6d2010ae 4884 struct nfsreq rq, *req = &rq;
2d21ac55 4885 struct nfs_dulookup dul;
1c79356b 4886
2d21ac55 4887 nmp = VTONMP(vp);
fe8ab488 4888 if (nfs_mount_gone(nmp))
2d21ac55
A
4889 return (ENXIO);
4890 nfsvers = nmp->nm_vers;
4891 if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN))
4892 return (ENAMETOOLONG);
91447636 4893
b0d623f7 4894 if ((error = nfs_node_set_busy2(dnp, np, vfs_context_thread(ctx))))
91447636 4895 return (error);
2d21ac55 4896
b0d623f7
A
4897 nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx);
4898
2d21ac55
A
4899 nfsm_chain_null(&nmreq);
4900 nfsm_chain_null(&nmrep);
4901
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);
6d2010ae 4905 nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp);
2d21ac55
A
4906 nfsm_chain_build_done(error, &nmreq);
4907 nfsmout_if(error);
4908
4909 error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_RMDIR,
6d2010ae 4910 vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req);
2d21ac55
A
4911 if (!error) {
4912 nfs_dulookup_start(&dul, dnp, ctx);
4913 error = nfs_request_async_finish(req, &nmrep, &xid, &status);
4914 }
4915
b0d623f7
A
4916 if ((lockerror = nfs_node_lock(dnp)))
4917 error = lockerror;
2d21ac55
A
4918 if (nfsvers == NFS_VER3)
4919 nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &xid);
4920 if (!error)
4921 error = status;
4922nfsmout:
4923 nfsm_chain_cleanup(&nmreq);
4924 nfsm_chain_cleanup(&nmrep);
4925
b0d623f7
A
4926 if (!lockerror) {
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 */
6d2010ae 4934 nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED);
2d21ac55
A
4935 }
4936 nfs_dulookup_finish(&dul, dnp, ctx);
b0d623f7 4937 nfs_node_clear_busy2(dnp, np);
2d21ac55 4938
1c79356b
A
4939 /*
4940 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
4941 */
4942 if (error == ENOENT)
4943 error = 0;
483a1d10
A
4944 if (!error) {
4945 /*
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
4949 */
91447636 4950 lck_mtx_lock(nfs_node_hash_mutex);
2d21ac55
A
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);
4955 }
91447636 4956 lck_mtx_unlock(nfs_node_hash_mutex);
483a1d10 4957 }
1c79356b
A
4958 return (error);
4959}
4960
4961/*
2d21ac55 4962 * NFS readdir call
b0d623f7
A
4963 *
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.
4968 *
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).
4974 *
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.
4977 *
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.
1c79356b 4982 */
b0d623f7 4983int
2d21ac55 4984nfs_vnop_readdir(
91447636
A
4985 struct vnop_readdir_args /* {
4986 struct vnodeop_desc *a_desc;
4987 vnode_t a_vp;
1c79356b 4988 struct uio *a_uio;
b0d623f7 4989 int a_flags;
91447636 4990 int *a_eofflag;
b0d623f7 4991 int *a_numdirent;
91447636 4992 vfs_context_t a_context;
2d21ac55 4993 } */ *ap)
1c79356b 4994{
2d21ac55 4995 vfs_context_t ctx = ap->a_context;
b0d623f7
A
4996 vnode_t dvp = ap->a_vp;
4997 nfsnode_t dnp = VTONFS(dvp);
2d21ac55 4998 struct nfsmount *nmp;
b0d623f7
A
4999 uio_t uio = ap->a_uio;
5000 int error, nfsvers, extended, numdirent, bigcookies, ptc, done;
b0d623f7
A
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;
5006 struct dirent dent;
5007 char *cp = NULL;
5008 thread_t thd;
1c79356b 5009
b0d623f7 5010 nmp = VTONMP(dvp);
fe8ab488 5011 if (nfs_mount_gone(nmp))
2d21ac55
A
5012 return (ENXIO);
5013 nfsvers = nmp->nm_vers;
b0d623f7
A
5014 bigcookies = (nmp->nm_state & NFSSTA_BIGCOOKIES);
5015 extended = (ap->a_flags & VNODE_READDIR_EXTENDED);
2d21ac55 5016
b0d623f7
A
5017 if (vnode_vtype(dvp) != VDIR)
5018 return (EPERM);
5019
5020 if (ap->a_eofflag)
5021 *ap->a_eofflag = 0;
5022
5023 if (uio_resid(uio) == 0)
5024 return (0);
5025
6d2010ae
A
5026 if ((nfsvers >= NFS_VER4) && (dnp->n_vattr.nva_flags & NFS_FFLAG_TRIGGER)) {
5027 /* trigger directories should never be read, return nothing */
5028 return (0);
5029 }
5030
b0d623f7
A
5031 thd = vfs_context_thread(ctx);
5032 numdirent = done = 0;
5033 nextcookie = uio_offset(uio);
5034 ptc = bigcookies && NFS_DIR_COOKIE_POTENTIALLY_TRUNCATED(nextcookie);
5035
5036 if ((error = nfs_node_lock(dnp)))
5037 goto out;
5038
5039 if (dnp->n_flag & NNEEDINVALIDATE) {
5040 dnp->n_flag &= ~NNEEDINVALIDATE;
5041 nfs_invaldir(dnp);
5042 nfs_node_unlock(dnp);
5043 error = nfs_vinvalbuf(dvp, 0, ctx, 1);
5044 if (!error)
5045 error = nfs_node_lock(dnp);
5046 if (error)
5047 goto out;
5048 }
91447636 5049
1c79356b 5050 /*
b0d623f7 5051 * check for need to invalidate when (re)starting at beginning
1c79356b 5052 */
b0d623f7
A
5053 if (!nextcookie) {
5054 if (dnp->n_flag & NMODIFIED) {
5055 nfs_invaldir(dnp);
5056 nfs_node_unlock(dnp);
5057 if ((error = nfs_vinvalbuf(dvp, 0, ctx, 1)))
5058 goto out;
5059 } else {
5060 nfs_node_unlock(dnp);
5061 }
5062 /* nfs_getattr() will check changed and purge caches */
6d2010ae 5063 if ((error = nfs_getattr(dnp, NULL, ctx, NGA_UNCACHED)))
b0d623f7
A
5064 goto out;
5065 } else {
5066 nfs_node_unlock(dnp);
5067 }
5068
5069 error = nfs_dir_cookie_to_lbn(dnp, nextcookie, &ptc, &lbn);
5070 if (error) {
5071 if (error < 0) { /* just hit EOF cookie */
5072 done = 1;
5073 error = 0;
5074 }
5075 if (ap->a_eofflag)
5076 *ap->a_eofflag = 1;
5077 }
5078
5079 while (!error && !done) {
316670eb 5080 OSAddAtomic64(1, &nfsstats.biocache_readdirs);
b0d623f7
A
5081 cookie = nextcookie;
5082getbuffer:
5083 error = nfs_buf_get(dnp, lbn, NFS_DIRBLKSIZ, thd, NBLK_READ, &bp);
5084 if (error)
5085 goto out;
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;
5093 }
5094 error = nfs_buf_readdir(bp, ctx);
5095 if (error == NFSERR_DIRBUFDROPPED)
5096 goto getbuffer;
5097 if (error)
5098 nfs_buf_release(bp, 1);
5099 if (error && (error != ENXIO) && (error != ETIMEDOUT) && (error != EINTR) && (error != ERESTART)) {
5100 if (!nfs_node_lock(dnp)) {
5101 nfs_invaldir(dnp);
5102 nfs_node_unlock(dnp);
5103 }
5104 nfs_vinvalbuf(dvp, 0, ctx, 1);
5105 if (error == NFSERR_BAD_COOKIE)
5106 error = ENOENT;
5107 }
5108 if (error)
5109 goto out;
5110 }
5111
5112 /* find next entry to return */
5113 dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp);
5114 i = 0;
5115 if ((lbn != cookie) && !(ptc && NFS_DIR_COOKIE_SAME32(lbn, cookie))) {
5116 dpptc = NULL;
5117 iptc = 0;
5118 for (; (i < ndbhp->ndbh_count) && (cookie != dp->d_seekoff); i++) {
5119 if (ptc && !dpptc && NFS_DIR_COOKIE_SAME32(cookie, dp->d_seekoff)) {
5120 iptc = i;
5121 dpptc = dp;
5122 }
5123 nextcookie = dp->d_seekoff;
5124 dp = NFS_DIRENTRY_NEXT(dp);
5125 }
5126 if ((i == ndbhp->ndbh_count) && dpptc) {
5127 i = iptc;
5128 dp = dpptc;
5129 }
5130 if (i < ndbhp->ndbh_count) {
5131 nextcookie = dp->d_seekoff;
5132 dp = NFS_DIRENTRY_NEXT(dp);
5133 i++;
5134 }
5135 }
5136 ptc = 0; /* only have to deal with ptc on first cookie */
5137
5138 /* return as many entries as we can */
5139 for (; i < ndbhp->ndbh_count; i++) {
5140 if (extended) {
5141 rlen = dp->d_reclen;
5142 cp = (char*)dp;
5143 } else {
5144 if (!cp) {
5145 cp = (char*)&dent;
5146 bzero(cp, sizeof(dent));
5147 }
5148 if (dp->d_namlen > (sizeof(dent.d_name) - 1))
5149 nlen = sizeof(dent.d_name) - 1;
5150 else
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);
5158 }
5159 /* check that the record fits */
5160 if (rlen > uio_resid(uio)) {
5161 done = 1;
5162 break;
5163 }
5164 if ((error = uiomove(cp, rlen, uio)))
5165 break;
5166 numdirent++;
5167 nextcookie = dp->d_seekoff;
5168 dp = NFS_DIRENTRY_NEXT(dp);
5169 }
5170
5171 if (i == ndbhp->ndbh_count) {
5172 /* hit end of buffer, move to next buffer */
5173 lbn = nextcookie;
5174 /* if we also hit EOF, we're done */
5175 if (ISSET(ndbhp->ndbh_flags, NDB_EOF)) {
5176 done = 1;
2d21ac55
A
5177 if (ap->a_eofflag)
5178 *ap->a_eofflag = 1;
483a1d10 5179 }
1c79356b 5180 }
b0d623f7
A
5181 if (!error)
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);
5185 error = EIO;
5186 }
5187 nfs_buf_release(bp, 1);
1c79356b
A
5188 }
5189
b0d623f7
A
5190 if (!error)
5191 nfs_dir_cookie_cache(dnp, nextcookie, lbn);
1c79356b 5192
b0d623f7
A
5193 if (ap->a_numdirent)
5194 *ap->a_numdirent = numdirent;
5195out:
1c79356b
A
5196 return (error);
5197}
5198
b0d623f7 5199
1c79356b 5200/*
b0d623f7
A
5201 * Invalidate cached directory information, except for the actual directory
5202 * blocks (which are invalidated separately).
1c79356b 5203 */
b0d623f7
A
5204void
5205nfs_invaldir(nfsnode_t dnp)
1c79356b 5206{
b0d623f7
A
5207 if (vnode_vtype(NFSTOV(dnp)) != VDIR)
5208 return;
5209 dnp->n_eofcookie = 0;
5210 dnp->n_cookieverf = 0;
5211 if (!dnp->n_cookiecache)
5212 return;
5213 dnp->n_cookiecache->free = 0;
5214 dnp->n_cookiecache->mru = -1;
5215 memset(dnp->n_cookiecache->next, -1, NFSNUMCOOKIES);
5216}
1c79356b 5217
b0d623f7
A
5218/*
5219 * calculate how much space is available for additional directory entries.
5220 */
5221uint32_t
5222nfs_dir_buf_freespace(struct nfsbuf *bp, int rdirplus)
5223{
5224 struct nfs_dir_buf_header *ndbhp = (struct nfs_dir_buf_header*)bp->nb_data;
5225 uint32_t space;
1c79356b 5226
b0d623f7
A
5227 if (!ndbhp)
5228 return (0);
5229 space = bp->nb_bufsize - ndbhp->ndbh_entry_end;
5230 if (rdirplus)
5231 space -= ndbhp->ndbh_count * sizeof(struct nfs_vattr);
5232 return (space);
5233}
2d21ac55 5234
b0d623f7
A
5235/*
5236 * add/update a cookie->lbn entry in the directory cookie cache
5237 */
5238void
5239nfs_dir_cookie_cache(nfsnode_t dnp, uint64_t cookie, uint64_t lbn)
5240{
5241 struct nfsdmap *ndcc;
5242 int8_t i, prev;
5243
5244 if (!cookie)
5245 return;
5246
5247 if (nfs_node_lock(dnp))
5248 return;
5249
5250 if (cookie == dnp->n_eofcookie) { /* EOF cookie */
5251 nfs_node_unlock(dnp);
5252 return;
5253 }
5254
5255 ndcc = dnp->n_cookiecache;
5256 if (!ndcc) {
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);
5262 return;
5263 }
5264 ndcc = dnp->n_cookiecache;
5265 ndcc->free = 0;
5266 ndcc->mru = -1;
5267 memset(ndcc->next, -1, NFSNUMCOOKIES);
2d21ac55
A
5268 }
5269
1c79356b 5270 /*
b0d623f7
A
5271 * Search the list for this cookie.
5272 * Keep track of previous and last entries.
1c79356b 5273 */
b0d623f7
A
5274 prev = -1;
5275 i = ndcc->mru;
5276 while ((i != -1) && (cookie != ndcc->cookies[i].key)) {
5277 if (ndcc->next[i] == -1) /* stop on last entry so we can reuse */
5278 break;
5279 prev = i;
5280 i = ndcc->next[i];
5281 }
5282 if ((i != -1) && (cookie == ndcc->cookies[i].key)) {
5283 /* found it, remove from list */
5284 if (prev != -1)
5285 ndcc->next[prev] = ndcc->next[i];
5286 else
5287 ndcc->mru = ndcc->next[i];
5288 } else {
5289 /* not found, use next free entry or reuse last entry */
5290 if (ndcc->free != NFSNUMCOOKIES)
5291 i = ndcc->free++;
5292 else
5293 ndcc->next[prev] = -1;
5294 ndcc->cookies[i].key = cookie;
5295 ndcc->cookies[i].lbn = lbn;
5296 }
5297 /* insert cookie at head of MRU list */
5298 ndcc->next[i] = ndcc->mru;
5299 ndcc->mru = i;
5300 nfs_node_unlock(dnp);
5301}
2d21ac55 5302
b0d623f7
A
5303/*
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.
5306 */
5307int
5308nfs_dir_cookie_to_lbn(nfsnode_t dnp, uint64_t cookie, int *ptc, uint64_t *lbnp)
5309{
5310 struct nfsdmap *ndcc = dnp->n_cookiecache;
6d2010ae
A
5311 int8_t eofptc, found;
5312 int i, iptc;
b0d623f7
A
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;
5318
5319 if (!cookie) { /* initial cookie */
5320 *lbnp = 0;
5321 *ptc = 0;
5322 return (0);
5323 }
2d21ac55 5324
b0d623f7
A
5325 if (nfs_node_lock(dnp))
5326 return (ENOENT);
2d21ac55 5327
b0d623f7
A
5328 if (cookie == dnp->n_eofcookie) { /* EOF cookie */
5329 nfs_node_unlock(dnp);
316670eb 5330 OSAddAtomic64(1, &nfsstats.direofcache_hits);
b0d623f7
A
5331 *ptc = 0;
5332 return (-1);
5333 }
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;
5336 iptc = -1;
2d21ac55 5337
b0d623f7
A
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);
316670eb 5344 OSAddAtomic64(1, &nfsstats.direofcache_hits);
b0d623f7
A
5345 *ptc = 0;
5346 return (0);
1c79356b 5347 }
b0d623f7
A
5348 /* check for 32-bit match */
5349 if (*ptc && (iptc == -1) && NFS_DIR_COOKIE_SAME32(ndcc->cookies[i].key, cookie))
5350 iptc = i;
5351 }
5352 /* exact match not found */
5353 if (eofptc) {
5354 /* but 32-bit match hit the EOF cookie */
5355 nfs_node_unlock(dnp);
316670eb 5356 OSAddAtomic64(1, &nfsstats.direofcache_hits);
b0d623f7
A
5357 return (-1);
5358 }
5359 if (iptc >= 0) {
5360 /* but 32-bit match got a hit */
5361 *lbnp = ndcc->cookies[iptc].lbn;
5362 nfs_node_unlock(dnp);
316670eb 5363 OSAddAtomic64(1, &nfsstats.direofcache_hits);
b0d623f7
A
5364 return (0);
5365 }
5366 nfs_node_unlock(dnp);
2d21ac55 5367
b0d623f7
A
5368 /*
5369 * No match found in the cookie cache... hmm...
5370 * Let's search the directory's buffers for the cookie.
5371 */
5372 nmp = NFSTONMP(dnp);
fe8ab488 5373 if (nfs_mount_gone(nmp))
b0d623f7
A
5374 return (ENXIO);
5375 dpptc = NULL;
5376 found = 0;
5377
5378 lck_mtx_lock(nfs_buf_mutex);
5379 /*
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.
5387 */
5388 if (!nfs_buf_iterprepare(dnp, &blist, NBI_CLEAN)) {
5389 lastbp = NULL;
5390 while ((bp = LIST_FIRST(&blist))) {
5391 LIST_REMOVE(bp, nb_vnbufs);
5392 if (!lastbp)
5393 LIST_INSERT_HEAD(&dnp->n_cleanblkhd, bp, nb_vnbufs);
2d21ac55 5394 else
b0d623f7
A
5395 LIST_INSERT_AFTER(lastbp, bp, nb_vnbufs);
5396 lastbp = bp;
5397 if (found)
5398 continue;
5399 nfs_buf_refget(bp);
5400 if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) {
5401 /* just skip this buffer */
5402 nfs_buf_refrele(bp);
5403 continue;
1c79356b 5404 }
b0d623f7
A
5405 nfs_buf_refrele(bp);
5406
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);
5410 dpptc = NULL;
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)) {
5413 dpptc = dp;
5414 iptc = i;
5415 }
5416 dp = NFS_DIRENTRY_NEXT(dp);
91447636 5417 }
b0d623f7
A
5418 if ((i == ndbhp->ndbh_count) && dpptc) {
5419 /* found only a PTC match */
5420 dp = dpptc;
5421 i = iptc;
5422 } else if (i < ndbhp->ndbh_count) {
5423 *ptc = 0;
1c79356b 5424 }
b0d623f7
A
5425 if (i < (ndbhp->ndbh_count-1)) {
5426 /* next entry is *in* this buffer: return this block */
5427 *lbnp = bp->nb_lblkno;
5428 found = 1;
5429 } else if (i == (ndbhp->ndbh_count-1)) {
5430 /* next entry refers to *next* buffer: return next block */
5431 *lbnp = dp->d_seekoff;
5432 found = 1;
1c79356b 5433 }
b0d623f7 5434 nfs_buf_drop(bp);
1c79356b 5435 }
b0d623f7 5436 nfs_buf_itercomplete(dnp, &blist, NBI_CLEAN);
2d21ac55 5437 }
b0d623f7
A
5438 lck_mtx_unlock(nfs_buf_mutex);
5439 if (found) {
316670eb 5440 OSAddAtomic64(1, &nfsstats.direofcache_hits);
b0d623f7 5441 return (0);
1c79356b 5442 }
b0d623f7
A
5443
5444 /* still not found... oh well, just start a new block */
5445 *lbnp = cookie;
316670eb 5446 OSAddAtomic64(1, &nfsstats.direofcache_misses);
b0d623f7
A
5447 return (0);
5448}
5449
5450/*
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
5454 */
5455
5456#define NDBS_PURGE 1
5457#define NDBS_UPDATE 2
5458
5459int
5460nfs_dir_buf_search(
5461 struct nfsbuf *bp,
5462 struct componentname *cnp,
5463 fhandle_t *fhp,
5464 struct nfs_vattr *nvap,
5465 uint64_t *xidp,
5466 time_t *attrstampp,
5467 daddr64_t *nextlbnp,
5468 int flags)
5469{
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, fhlen;
5475
5476 /* scan the buffer for the name */
5477 ndbhp = (struct nfs_dir_buf_header*)bp->nb_data;
5478 dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp);
5479 for (i=0; i < ndbhp->ndbh_count; i++) {
5480 nextlbn = dp->d_seekoff;
5481 if ((cnp->cn_namelen == dp->d_namlen) && !strcmp(cnp->cn_nameptr, dp->d_name)) {
5482 fhlen = dp->d_name[dp->d_namlen+1];
5483 nvattrp = NFS_DIR_BUF_NVATTR(bp, i);
5484 if ((ndbhp->ndbh_ncgen != bp->nb_np->n_ncgen) || (fhp->fh_len == 0) ||
5485 (nvattrp->nva_type == VNON) || (nvattrp->nva_fileid == 0)) {
6d2010ae 5486 /* entry is not valid */
b0d623f7
A
5487 error = ENOENT;
5488 break;
5489 }
5490 if (flags == NDBS_PURGE) {
5491 dp->d_fileno = 0;
5492 bzero(nvattrp, sizeof(*nvattrp));
5493 error = ENOENT;
5494 break;
5495 }
5496 if (flags == NDBS_UPDATE) {
5497 /* update direntry's attrs if fh matches */
5498 if ((fhp->fh_len == fhlen) && !bcmp(&dp->d_name[dp->d_namlen+2], fhp->fh_data, fhlen)) {
5499 bcopy(nvap, nvattrp, sizeof(*nvap));
5500 dp->d_fileno = nvattrp->nva_fileid;
5501 nvattrp->nva_fileid = *xidp;
5502 *(time_t*)(&dp->d_name[dp->d_namlen+2+fhp->fh_len]) = *attrstampp;
5503 }
5504 error = 0;
5505 break;
5506 }
5507 /* copy out fh, attrs, attrstamp, and xid */
5508 fhp->fh_len = fhlen;
5509 bcopy(&dp->d_name[dp->d_namlen+2], fhp->fh_data, MAX(fhp->fh_len, (int)sizeof(fhp->fh_data)));
5510 *attrstampp = *(time_t*)(&dp->d_name[dp->d_namlen+2+fhp->fh_len]);
5511 bcopy(nvattrp, nvap, sizeof(*nvap));
5512 *xidp = nvap->nva_fileid;
5513 nvap->nva_fileid = dp->d_fileno;
5514 error = 0;
5515 break;
5516 }
5517 dp = NFS_DIRENTRY_NEXT(dp);
1c79356b 5518 }
b0d623f7
A
5519 if (nextlbnp)
5520 *nextlbnp = nextlbn;
5521 return (error);
5522}
1c79356b 5523
b0d623f7
A
5524/*
5525 * Look up a name in a directory's buffers.
5526 * Note: should only be called with RDIRPLUS directory buffers
5527 */
5528int
5529nfs_dir_buf_cache_lookup(nfsnode_t dnp, nfsnode_t *npp, struct componentname *cnp, vfs_context_t ctx, int purge)
5530{
5531 nfsnode_t newnp;
5532 struct nfsmount *nmp;
6d2010ae 5533 int error = 0, i, found = 0, count = 0;
b0d623f7
A
5534 u_int64_t xid;
5535 struct nfs_vattr nvattr;
5536 fhandle_t fh;
5537 time_t attrstamp = 0;
5538 thread_t thd = vfs_context_thread(ctx);
5539 struct nfsbuf *bp, *lastbp, *foundbp;
5540 struct nfsbuflists blist;
5541 daddr64_t lbn, nextlbn;
5542 int dotunder = (cnp->cn_namelen > 2) && (cnp->cn_nameptr[0] == '.') && (cnp->cn_nameptr[1] == '_');
5543
fe8ab488
A
5544 nmp = NFSTONMP(dnp);
5545 if (nfs_mount_gone(nmp))
b0d623f7 5546 return (ENXIO);
b0d623f7
A
5547 if (!purge)
5548 *npp = NULL;
5549
5550 /* first check most recent buffer (and next one too) */
5551 lbn = dnp->n_lastdbl;
5552 for (i=0; i < 2; i++) {
5553 if ((error = nfs_buf_get(dnp, lbn, NFS_DIRBLKSIZ, thd, NBLK_READ|NBLK_ONLYVALID, &bp)))
5554 return (error);
5555 if (!bp)
5556 break;
5557 count++;
5558 error = nfs_dir_buf_search(bp, cnp, &fh, &nvattr, &xid, &attrstamp, &nextlbn, purge ? NDBS_PURGE : 0);
5559 nfs_buf_release(bp, 0);
5560 if (error == ESRCH) {
5561 error = 0;
5562 } else {
5563 found = 1;
5564 break;
5565 }
5566 lbn = nextlbn;
5567 }
5568
5569 lck_mtx_lock(nfs_buf_mutex);
5570 if (found) {
5571 dnp->n_lastdbl = lbn;
5572 goto done;
5573 }
2d21ac55 5574
1c79356b 5575 /*
b0d623f7
A
5576 * Scan the list of buffers, keeping them in order.
5577 * Note that itercomplete inserts each of the remaining buffers
5578 * into the head of list (thus reversing the elements). So, we
5579 * make sure to iterate through all buffers, inserting them after
5580 * each other, to keep them in order.
5581 * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because
5582 * we don't drop nfs_buf_mutex.
1c79356b 5583 */
b0d623f7
A
5584 if (!nfs_buf_iterprepare(dnp, &blist, NBI_CLEAN)) {
5585 lastbp = foundbp = NULL;
5586 while ((bp = LIST_FIRST(&blist))) {
5587 LIST_REMOVE(bp, nb_vnbufs);
5588 if (!lastbp)
5589 LIST_INSERT_HEAD(&dnp->n_cleanblkhd, bp, nb_vnbufs);
5590 else
5591 LIST_INSERT_AFTER(lastbp, bp, nb_vnbufs);
5592 lastbp = bp;
5593 if (error || found)
5594 continue;
5595 if (!purge && dotunder && (count > 100)) /* don't waste too much time looking for ._ files */
5596 continue;
5597 nfs_buf_refget(bp);
5598 lbn = bp->nb_lblkno;
5599 if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) {
5600 /* just skip this buffer */
5601 nfs_buf_refrele(bp);
5602 continue;
5603 }
5604 nfs_buf_refrele(bp);
5605 count++;
5606 error = nfs_dir_buf_search(bp, cnp, &fh, &nvattr, &xid, &attrstamp, NULL, purge ? NDBS_PURGE : 0);
5607 if (error == ESRCH) {
5608 error = 0;
5609 } else {
5610 found = 1;
5611 foundbp = bp;
5612 }
5613 nfs_buf_drop(bp);
5614 }
5615 if (found) {
5616 LIST_REMOVE(foundbp, nb_vnbufs);
5617 LIST_INSERT_HEAD(&dnp->n_cleanblkhd, foundbp, nb_vnbufs);
5618 dnp->n_lastdbl = foundbp->nb_lblkno;
5619 }
5620 nfs_buf_itercomplete(dnp, &blist, NBI_CLEAN);
5621 }
5622done:
5623 lck_mtx_unlock(nfs_buf_mutex);
5624
5625 if (!error && found && !purge) {
5626 error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len,
6d2010ae 5627 &nvattr, &xid, dnp->n_auth, NG_MAKEENTRY, &newnp);
b0d623f7
A
5628 if (error)
5629 return (error);
5630 newnp->n_attrstamp = attrstamp;
5631 *npp = newnp;
5632 nfs_node_unlock(newnp);
5633 /* check if the dir buffer's attrs are out of date */
5634 if (!nfs_getattr(newnp, &nvattr, ctx, NGA_CACHED) &&
5635 (newnp->n_attrstamp != attrstamp)) {
5636 /* they are, so update them */
5637 error = nfs_buf_get(dnp, lbn, NFS_DIRBLKSIZ, thd, NBLK_READ|NBLK_ONLYVALID, &bp);
5638 if (!error && bp) {
5639 attrstamp = newnp->n_attrstamp;
5640 xid = newnp->n_xid;
5641 nfs_dir_buf_search(bp, cnp, &fh, &nvattr, &xid, &attrstamp, NULL, NDBS_UPDATE);
5642 nfs_buf_release(bp, 0);
5643 }
5644 error = 0;
5645 }
1c79356b 5646 }
2d21ac55 5647
1c79356b
A
5648 return (error);
5649}
5650
5651/*
b0d623f7
A
5652 * Purge name cache entries for the given node.
5653 * For RDIRPLUS, also invalidate the entry in the directory's buffers.
5654 */
5655void
5656nfs_name_cache_purge(nfsnode_t dnp, nfsnode_t np, struct componentname *cnp, vfs_context_t ctx)
5657{
5658 struct nfsmount *nmp = NFSTONMP(dnp);
5659
5660 cache_purge(NFSTOV(np));
6d2010ae 5661 if (nmp && (nmp->nm_vers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS))
b0d623f7
A
5662 nfs_dir_buf_cache_lookup(dnp, NULL, cnp, ctx, 1);
5663}
5664
5665/*
5666 * NFS V3 readdir (plus) RPC.
1c79356b
A
5667 */
5668int
b0d623f7 5669nfs3_readdir_rpc(nfsnode_t dnp, struct nfsbuf *bp, vfs_context_t ctx)
1c79356b 5670{
55e303ae 5671 struct nfsmount *nmp;
b0d623f7
A
5672 int error = 0, lockerror, nfsvers, rdirplus, bigcookies;
5673 int i, status, attrflag, fhflag, more_entries = 1, eof, bp_dropped = 0;
5674 uint32_t nmreaddirsize, nmrsize;
5675 uint32_t namlen, skiplen, fhlen, xlen, attrlen, reclen, space_free, space_needed;
5676 uint64_t cookie, lastcookie, xid, savedxid, fileno;
5677 struct nfsm_chain nmreq, nmrep, nmrepsave;
5678 fhandle_t fh;
5679 struct nfs_vattr *nvattrp;
5680 struct nfs_dir_buf_header *ndbhp;
5681 struct direntry *dp;
5682 char *padstart, padlen;
5683 struct timeval now;
1c79356b 5684
2d21ac55 5685 nmp = NFSTONMP(dnp);
fe8ab488 5686 if (nfs_mount_gone(nmp))
55e303ae 5687 return (ENXIO);
2d21ac55 5688 nfsvers = nmp->nm_vers;
55e303ae
A
5689 nmreaddirsize = nmp->nm_readdirsize;
5690 nmrsize = nmp->nm_rsize;
b0d623f7
A
5691 bigcookies = nmp->nm_state & NFSSTA_BIGCOOKIES;
5692noplus:
6d2010ae 5693 rdirplus = ((nfsvers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) ? 1 : 0;
55e303ae 5694
b0d623f7 5695 if ((lockerror = nfs_node_lock(dnp)))
2d21ac55
A
5696 return (lockerror);
5697
b0d623f7
A
5698 /* determine cookie to use, and move dp to the right offset */
5699 ndbhp = (struct nfs_dir_buf_header*)bp->nb_data;
5700 dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp);
5701 if (ndbhp->ndbh_count) {
5702 for (i=0; i < ndbhp->ndbh_count-1; i++)
5703 dp = NFS_DIRENTRY_NEXT(dp);
5704 cookie = dp->d_seekoff;
5705 dp = NFS_DIRENTRY_NEXT(dp);
5706 } else {
5707 cookie = bp->nb_lblkno;
5708 /* increment with every buffer read */
316670eb 5709 OSAddAtomic64(1, &nfsstats.readdir_bios);
2d21ac55 5710 }
b0d623f7 5711 lastcookie = cookie;
2d21ac55 5712
1c79356b 5713 /*
b0d623f7
A
5714 * Loop around doing readdir(plus) RPCs of size nm_readdirsize until
5715 * the buffer is full (or we hit EOF). Then put the remainder of the
5716 * results in the next buffer(s).
1c79356b 5717 */
2d21ac55
A
5718 nfsm_chain_null(&nmreq);
5719 nfsm_chain_null(&nmrep);
b0d623f7 5720 while (nfs_dir_buf_freespace(bp, rdirplus) && !(ndbhp->ndbh_flags & NDB_FULL)) {
2d21ac55 5721 nfsm_chain_build_alloc_init(error, &nmreq,
b0d623f7 5722 NFSX_FH(nfsvers) + NFSX_READDIR(nfsvers) + NFSX_UNSIGNED);
2d21ac55 5723 nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
b0d623f7
A
5724 if (nfsvers == NFS_VER3) {
5725 /* opaque values don't need swapping, but as long */
5726 /* as we are consistent about it, it should be ok */
5727 nfsm_chain_add_64(error, &nmreq, cookie);
5728 nfsm_chain_add_64(error, &nmreq, dnp->n_cookieverf);
5729 } else {
5730 nfsm_chain_add_32(error, &nmreq, cookie);
5731 }
2d21ac55 5732 nfsm_chain_add_32(error, &nmreq, nmreaddirsize);
b0d623f7
A
5733 if (rdirplus)
5734 nfsm_chain_add_32(error, &nmreq, nmrsize);
2d21ac55 5735 nfsm_chain_build_done(error, &nmreq);
b0d623f7 5736 nfs_node_unlock(dnp);
2d21ac55
A
5737 lockerror = ENOENT;
5738 nfsmout_if(error);
5739
b0d623f7
A
5740 error = nfs_request(dnp, NULL, &nmreq,
5741 rdirplus ? NFSPROC_READDIRPLUS : NFSPROC_READDIR,
6d2010ae 5742 ctx, NULL, &nmrep, &xid, &status);
2d21ac55 5743
b0d623f7 5744 if ((lockerror = nfs_node_lock(dnp)))
2d21ac55
A
5745 error = lockerror;
5746
b0d623f7
A
5747 savedxid = xid;
5748 if (nfsvers == NFS_VER3)
5749 nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid);
2d21ac55
A
5750 if (!error)
5751 error = status;
b0d623f7
A
5752 if (nfsvers == NFS_VER3)
5753 nfsm_chain_get_64(error, &nmrep, dnp->n_cookieverf);
5754 nfsm_chain_get_32(error, &nmrep, more_entries);
2d21ac55
A
5755
5756 if (!lockerror) {
b0d623f7 5757 nfs_node_unlock(dnp);
2d21ac55 5758 lockerror = ENOENT;
1c79356b 5759 }
b0d623f7
A
5760 if (error == NFSERR_NOTSUPP) {
5761 /* oops... it doesn't look like readdirplus is supported */
5762 lck_mtx_lock(&nmp->nm_lock);
6d2010ae 5763 NFS_BITMAP_CLR(nmp->nm_flags, NFS_MFLAG_RDIRPLUS);
b0d623f7
A
5764 lck_mtx_unlock(&nmp->nm_lock);
5765 goto noplus;
5766 }
2d21ac55 5767 nfsmout_if(error);
1c79356b 5768
b0d623f7
A
5769 if (rdirplus)
5770 microuptime(&now);
5771
5772 /* loop through the entries packing them into the buffer */
5773 while (more_entries) {
5774 if (nfsvers == NFS_VER3)
5775 nfsm_chain_get_64(error, &nmrep, fileno);
5776 else
5777 nfsm_chain_get_32(error, &nmrep, fileno);
5778 nfsm_chain_get_32(error, &nmrep, namlen);
2d21ac55 5779 nfsmout_if(error);
b0d623f7
A
5780 /* just truncate names that don't fit in direntry.d_name */
5781 if (namlen <= 0) {
1c79356b 5782 error = EBADRPC;
1c79356b
A
5783 goto nfsmout;
5784 }
b0d623f7
A
5785 if (namlen > (sizeof(dp->d_name)-1)) {
5786 skiplen = namlen - sizeof(dp->d_name) + 1;
5787 namlen = sizeof(dp->d_name) - 1;
91447636
A
5788 } else {
5789 skiplen = 0;
5790 }
b0d623f7
A
5791 /* guess that fh size will be same as parent */
5792 fhlen = rdirplus ? (1 + dnp->n_fhsize) : 0;
5793 xlen = rdirplus ? (fhlen + sizeof(time_t)) : 0;
5794 attrlen = rdirplus ? sizeof(struct nfs_vattr) : 0;
5795 reclen = NFS_DIRENTRY_LEN(namlen + xlen);
5796 space_needed = reclen + attrlen;
5797 space_free = nfs_dir_buf_freespace(bp, rdirplus);
5798 if (space_needed > space_free) {
5799 /*
5800 * We still have entries to pack, but we've
5801 * run out of room in the current buffer.
5802 * So we need to move to the next buffer.
5803 * The block# for the next buffer is the
5804 * last cookie in the current buffer.
5805 */
5806nextbuffer:
5807 ndbhp->ndbh_flags |= NDB_FULL;
5808 nfs_buf_release(bp, 0);
5809 bp_dropped = 1;
5810 bp = NULL;
5811 error = nfs_buf_get(dnp, lastcookie, NFS_DIRBLKSIZ, vfs_context_thread(ctx), NBLK_READ, &bp);
2d21ac55 5812 nfsmout_if(error);
b0d623f7
A
5813 /* initialize buffer */
5814 ndbhp = (struct nfs_dir_buf_header*)bp->nb_data;
5815 ndbhp->ndbh_flags = 0;
5816 ndbhp->ndbh_count = 0;
5817 ndbhp->ndbh_entry_end = sizeof(*ndbhp);
5818 ndbhp->ndbh_ncgen = dnp->n_ncgen;
5819 space_free = nfs_dir_buf_freespace(bp, rdirplus);
5820 dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp);
5821 /* increment with every buffer read */
316670eb 5822 OSAddAtomic64(1, &nfsstats.readdir_bios);
91447636 5823 }
b0d623f7
A
5824 nmrepsave = nmrep;
5825 dp->d_fileno = fileno;
5826 dp->d_namlen = namlen;
5827 dp->d_reclen = reclen;
5828 dp->d_type = DT_UNKNOWN;
5829 nfsm_chain_get_opaque(error, &nmrep, namlen, dp->d_name);
2d21ac55 5830 nfsmout_if(error);
b0d623f7
A
5831 dp->d_name[namlen] = '\0';
5832 if (skiplen)
5833 nfsm_chain_adv(error, &nmrep,
5834 nfsm_rndup(namlen + skiplen) - nfsm_rndup(namlen));
5835 if (nfsvers == NFS_VER3)
5836 nfsm_chain_get_64(error, &nmrep, cookie);
5837 else
5838 nfsm_chain_get_32(error, &nmrep, cookie);
5839 nfsmout_if(error);
5840 dp->d_seekoff = cookie;
5841 if (!bigcookies && (cookie >> 32) && (nmp == NFSTONMP(dnp))) {
5842 /* we've got a big cookie, make sure flag is set */
5843 lck_mtx_lock(&nmp->nm_lock);
5844 nmp->nm_state |= NFSSTA_BIGCOOKIES;
5845 lck_mtx_unlock(&nmp->nm_lock);
5846 bigcookies = 1;
5847 }
5848 if (rdirplus) {
5849 nvattrp = NFS_DIR_BUF_NVATTR(bp, ndbhp->ndbh_count);
5850 /* check for attributes */
5851 nfsm_chain_get_32(error, &nmrep, attrflag);
2d21ac55 5852 nfsmout_if(error);
b0d623f7
A
5853 if (attrflag) {
5854 /* grab attributes */
5855 error = nfs_parsefattr(&nmrep, NFS_VER3, nvattrp);
5856 nfsmout_if(error);
5857 dp->d_type = IFTODT(VTTOIF(nvattrp->nva_type));
5858 /* fileid is already in d_fileno, so stash xid in attrs */
5859 nvattrp->nva_fileid = savedxid;
5860 } else {
5861 /* mark the attributes invalid */
5862 bzero(nvattrp, sizeof(struct nfs_vattr));
5863 }
5864 /* check for file handle */
5865 nfsm_chain_get_32(error, &nmrep, fhflag);
5866 nfsmout_if(error);
5867 if (fhflag) {
5868 nfsm_chain_get_fh(error, &nmrep, NFS_VER3, &fh);
5869 nfsmout_if(error);
5870 fhlen = fh.fh_len + 1;
5871 xlen = fhlen + sizeof(time_t);
5872 reclen = NFS_DIRENTRY_LEN(namlen + xlen);
5873 space_needed = reclen + attrlen;
5874 if (space_needed > space_free) {
5875 /* didn't actually have the room... move on to next buffer */
5876 nmrep = nmrepsave;
5877 goto nextbuffer;
2d21ac55 5878 }
b0d623f7
A
5879 /* pack the file handle into the record */
5880 dp->d_name[dp->d_namlen+1] = fh.fh_len;
5881 bcopy(fh.fh_data, &dp->d_name[dp->d_namlen+2], fh.fh_len);
1c79356b 5882 } else {
b0d623f7
A
5883 /* mark the file handle invalid */
5884 fh.fh_len = 0;
5885 fhlen = fh.fh_len + 1;
5886 xlen = fhlen + sizeof(time_t);
5887 reclen = NFS_DIRENTRY_LEN(namlen + xlen);
5888 bzero(&dp->d_name[dp->d_namlen+1], fhlen);
1c79356b 5889 }
b0d623f7
A
5890 *(time_t*)(&dp->d_name[dp->d_namlen+1+fhlen]) = now.tv_sec;
5891 dp->d_reclen = reclen;
1c79356b 5892 }
b0d623f7
A
5893 padstart = dp->d_name + dp->d_namlen + 1 + xlen;
5894 ndbhp->ndbh_count++;
5895 lastcookie = cookie;
5896 /* advance to next direntry in buffer */
5897 dp = NFS_DIRENTRY_NEXT(dp);
5898 ndbhp->ndbh_entry_end = (char*)dp - bp->nb_data;
5899 /* zero out the pad bytes */
5900 padlen = (char*)dp - padstart;
5901 if (padlen > 0)
5902 bzero(padstart, padlen);
5903 /* check for more entries */
5904 nfsm_chain_get_32(error, &nmrep, more_entries);
2d21ac55 5905 nfsmout_if(error);
1c79356b 5906 }
b0d623f7
A
5907 /* Finally, get the eof boolean */
5908 nfsm_chain_get_32(error, &nmrep, eof);
5909 nfsmout_if(error);
5910 if (eof) {
5911 ndbhp->ndbh_flags |= (NDB_FULL|NDB_EOF);
5912 nfs_node_lock_force(dnp);
5913 dnp->n_eofcookie = lastcookie;
5914 nfs_node_unlock(dnp);
5915 } else {
5916 more_entries = 1;
5917 }
5918 if (bp_dropped) {
5919 nfs_buf_release(bp, 0);
5920 bp = NULL;
5921 break;
1c79356b 5922 }
b0d623f7 5923 if ((lockerror = nfs_node_lock(dnp)))
2d21ac55
A
5924 error = lockerror;
5925 nfsmout_if(error);
5926 nfsm_chain_cleanup(&nmrep);
5927 nfsm_chain_null(&nmreq);
5928 }
1c79356b 5929nfsmout:
b0d623f7
A
5930 if (bp_dropped && bp)
5931 nfs_buf_release(bp, 0);
2d21ac55 5932 if (!lockerror)
b0d623f7 5933 nfs_node_unlock(dnp);
2d21ac55
A
5934 nfsm_chain_cleanup(&nmreq);
5935 nfsm_chain_cleanup(&nmrep);
b0d623f7 5936 return (bp_dropped ? NFSERR_DIRBUFDROPPED : error);
1c79356b
A
5937}
5938
5939/*
5940 * Silly rename. To make the NFS filesystem that is stateless look a little
5941 * more like the "ufs" a remove of an active vnode is translated to a rename
2d21ac55 5942 * to a funny looking filename that is removed by nfs_vnop_inactive on the
1c79356b 5943 * nfsnode. There is the potential for another process on a different client
2d21ac55
A
5944 * to create the same funny name between when the lookitup() fails and the
5945 * rename() completes, but...
1c79356b 5946 */
55e303ae 5947
2d21ac55
A
5948/* format of "random" silly names - includes a number and pid */
5949/* (note: shouldn't exceed size of nfs_sillyrename.nsr_name) */
5950#define NFS_SILLYNAME_FORMAT ".nfs.%08x.%04x"
5951/* starting from zero isn't silly enough */
5952static uint32_t nfs_sillyrename_number = 0x20051025;
55e303ae 5953
b0d623f7 5954int
91447636 5955nfs_sillyrename(
2d21ac55
A
5956 nfsnode_t dnp,
5957 nfsnode_t np,
91447636 5958 struct componentname *cnp,
2d21ac55 5959 vfs_context_t ctx)
1c79356b 5960{
2d21ac55 5961 struct nfs_sillyrename *nsp;
1c79356b
A
5962 int error;
5963 short pid;
2d21ac55
A
5964 kauth_cred_t cred;
5965 uint32_t num;
5966 struct nfsmount *nmp;
1c79356b 5967
2d21ac55 5968 nmp = NFSTONMP(dnp);
fe8ab488 5969 if (nfs_mount_gone(nmp))
2d21ac55
A
5970 return (ENXIO);
5971
b0d623f7 5972 nfs_name_cache_purge(dnp, np, cnp, ctx);
2d21ac55
A
5973
5974 MALLOC_ZONE(nsp, struct nfs_sillyrename *,
5975 sizeof (struct nfs_sillyrename), M_NFSREQ, M_WAITOK);
5976 if (!nsp)
91447636 5977 return (ENOMEM);
2d21ac55 5978 cred = vfs_context_ucred(ctx);
91447636 5979 kauth_cred_ref(cred);
2d21ac55
A
5980 nsp->nsr_cred = cred;
5981 nsp->nsr_dnp = dnp;
5982 error = vnode_ref(NFSTOV(dnp));
91447636
A
5983 if (error)
5984 goto bad_norele;
1c79356b
A
5985
5986 /* Fudge together a funny name */
2d21ac55 5987 pid = vfs_context_pid(ctx);
b0d623f7 5988 num = OSAddAtomic(1, &nfs_sillyrename_number);
2d21ac55
A
5989 nsp->nsr_namlen = snprintf(nsp->nsr_name, sizeof(nsp->nsr_name),
5990 NFS_SILLYNAME_FORMAT, num, (pid & 0xffff));
5991 if (nsp->nsr_namlen >= (int)sizeof(nsp->nsr_name))
5992 nsp->nsr_namlen = sizeof(nsp->nsr_name) - 1;
1c79356b
A
5993
5994 /* Try lookitups until we get one that isn't there */
2d21ac55 5995 while (nfs_lookitup(dnp, nsp->nsr_name, nsp->nsr_namlen, ctx, NULL) == 0) {
b0d623f7 5996 num = OSAddAtomic(1, &nfs_sillyrename_number);
2d21ac55
A
5997 nsp->nsr_namlen = snprintf(nsp->nsr_name, sizeof(nsp->nsr_name),
5998 NFS_SILLYNAME_FORMAT, num, (pid & 0xffff));
5999 if (nsp->nsr_namlen >= (int)sizeof(nsp->nsr_name))
6000 nsp->nsr_namlen = sizeof(nsp->nsr_name) - 1;
55e303ae 6001 }
2d21ac55 6002
55e303ae 6003 /* now, do the rename */
2d21ac55
A
6004 error = nmp->nm_funcs->nf_rename_rpc(dnp, cnp->cn_nameptr, cnp->cn_namelen,
6005 dnp, nsp->nsr_name, nsp->nsr_namlen, ctx);
6d2010ae
A
6006
6007 /* Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. */
6008 if (error == ENOENT)
6009 error = 0;
b0d623f7
A
6010 if (!error) {
6011 nfs_node_lock_force(dnp);
6012 if (dnp->n_flag & NNEGNCENTRIES) {
6013 dnp->n_flag &= ~NNEGNCENTRIES;
6014 cache_purge_negatives(NFSTOV(dnp));
6015 }
6016 nfs_node_unlock(dnp);
2d21ac55
A
6017 }
6018 FSDBG(267, dnp, np, num, error);
91447636 6019 if (error)
1c79356b 6020 goto bad;
2d21ac55 6021 error = nfs_lookitup(dnp, nsp->nsr_name, nsp->nsr_namlen, ctx, &np);
b0d623f7 6022 nfs_node_lock_force(np);
2d21ac55 6023 np->n_sillyrename = nsp;
b0d623f7 6024 nfs_node_unlock(np);
1c79356b
A
6025 return (0);
6026bad:
2d21ac55 6027 vnode_rele(NFSTOV(dnp));
91447636 6028bad_norele:
2d21ac55
A
6029 nsp->nsr_cred = NOCRED;
6030 kauth_cred_unref(&cred);
6031 FREE_ZONE(nsp, sizeof(*nsp), M_NFSREQ);
6032 return (error);
6033}
6034
6035int
6036nfs3_lookup_rpc_async(
6037 nfsnode_t dnp,
6038 char *name,
6039 int namelen,
6040 vfs_context_t ctx,
6041 struct nfsreq **reqp)
6042{
6043 struct nfsmount *nmp;
6044 struct nfsm_chain nmreq;
6045 int error = 0, nfsvers;
6046
6047 nmp = NFSTONMP(dnp);
fe8ab488 6048 if (nfs_mount_gone(nmp))
2d21ac55
A
6049 return (ENXIO);
6050 nfsvers = nmp->nm_vers;
6051
6052 nfsm_chain_null(&nmreq);
6053
6054 nfsm_chain_build_alloc_init(error, &nmreq,
6055 NFSX_FH(nfsvers) + NFSX_UNSIGNED + nfsm_rndup(namelen));
6056 nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
6d2010ae 6057 nfsm_chain_add_name(error, &nmreq, name, namelen, nmp);
2d21ac55
A
6058 nfsm_chain_build_done(error, &nmreq);
6059 nfsmout_if(error);
6060 error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_LOOKUP,
6d2010ae 6061 vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, reqp);
2d21ac55
A
6062nfsmout:
6063 nfsm_chain_cleanup(&nmreq);
6064 return (error);
6065}
6066
6067int
6068nfs3_lookup_rpc_async_finish(
6069 nfsnode_t dnp,
6d2010ae
A
6070 __unused char *name,
6071 __unused int namelen,
2d21ac55
A
6072 vfs_context_t ctx,
6073 struct nfsreq *req,
6074 u_int64_t *xidp,
6075 fhandle_t *fhp,
6076 struct nfs_vattr *nvap)
6077{
b0d623f7 6078 int error = 0, lockerror = ENOENT, status, nfsvers, attrflag;
2d21ac55
A
6079 u_int64_t xid;
6080 struct nfsmount *nmp;
6081 struct nfsm_chain nmrep;
6082
6083 nmp = NFSTONMP(dnp);
3e170ce0
A
6084 if (nmp == NULL)
6085 return (ENXIO);
2d21ac55
A
6086 nfsvers = nmp->nm_vers;
6087
6088 nfsm_chain_null(&nmrep);
6089
6090 error = nfs_request_async_finish(req, &nmrep, xidp, &status);
6091
b0d623f7
A
6092 if ((lockerror = nfs_node_lock(dnp)))
6093 error = lockerror;
2d21ac55
A
6094 xid = *xidp;
6095 if (error || status) {
6096 if (nfsvers == NFS_VER3)
6097 nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid);
6098 if (!error)
6099 error = status;
6100 goto nfsmout;
6101 }
6102
6103 nfsmout_if(error || !fhp || !nvap);
6104
6105 /* get the file handle */
6106 nfsm_chain_get_fh(error, &nmrep, nfsvers, fhp);
6107
6108 /* get the attributes */
6109 if (nfsvers == NFS_VER3) {
6110 nfsm_chain_postop_attr_get(error, &nmrep, attrflag, nvap);
6111 nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid);
6112 if (!error && !attrflag)
6d2010ae 6113 error = nfs3_getattr_rpc(NULL, NFSTOMP(dnp), fhp->fh_data, fhp->fh_len, 0, ctx, nvap, xidp);
2d21ac55
A
6114 } else {
6115 error = nfs_parsefattr(&nmrep, nfsvers, nvap);
6116 }
6117nfsmout:
b0d623f7
A
6118 if (!lockerror)
6119 nfs_node_unlock(dnp);
2d21ac55 6120 nfsm_chain_cleanup(&nmrep);
1c79356b
A
6121 return (error);
6122}
6123
6124/*
6125 * Look up a file name and optionally either update the file handle or
6126 * allocate an nfsnode, depending on the value of npp.
6127 * npp == NULL --> just do the lookup
6128 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
6129 * handled too
6130 * *npp != NULL --> update the file handle in the vnode
6131 */
2d21ac55
A
6132int
6133nfs_lookitup(
6134 nfsnode_t dnp,
6135 char *name,
6136 int namelen,
6137 vfs_context_t ctx,
6138 nfsnode_t *npp)
6139{
6140 int error = 0;
6141 nfsnode_t np, newnp = NULL;
6142 u_int64_t xid;
6143 fhandle_t fh;
6144 struct nfsmount *nmp;
6145 struct nfs_vattr nvattr;
6146 struct nfsreq rq, *req = &rq;
6147
6148 nmp = NFSTONMP(dnp);
fe8ab488 6149 if (nfs_mount_gone(nmp))
2d21ac55
A
6150 return (ENXIO);
6151
6152 if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME) &&
b0d623f7 6153 (namelen > (int)nmp->nm_fsattr.nfsa_maxname))
2d21ac55
A
6154 return (ENAMETOOLONG);
6155
6d2010ae
A
6156 NVATTR_INIT(&nvattr);
6157
2d21ac55
A
6158 /* check for lookup of "." */
6159 if ((name[0] == '.') && (namelen == 1)) {
6160 /* skip lookup, we know who we are */
6161 fh.fh_len = 0;
6162 newnp = dnp;
6163 goto nfsmout;
6164 }
6165
6166 error = nmp->nm_funcs->nf_lookup_rpc_async(dnp, name, namelen, ctx, &req);
6167 nfsmout_if(error);
6d2010ae 6168 error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, name, namelen, ctx, req, &xid, &fh, &nvattr);
2d21ac55
A
6169 nfsmout_if(!npp || error);
6170
6171 if (*npp) {
6172 np = *npp;
6173 if (fh.fh_len != np->n_fhsize) {
6174 u_char *oldbuf = (np->n_fhsize > NFS_SMALLFH) ? np->n_fhp : NULL;
6175 if (fh.fh_len > NFS_SMALLFH) {
6176 MALLOC_ZONE(np->n_fhp, u_char *, fh.fh_len, M_NFSBIGFH, M_WAITOK);
6177 if (!np->n_fhp) {
6178 np->n_fhp = oldbuf;
6179 error = ENOMEM;
6180 goto nfsmout;
6181 }
6182 } else {
6183 np->n_fhp = &np->n_fh[0];
6184 }
6185 if (oldbuf)
6186 FREE_ZONE(oldbuf, np->n_fhsize, M_NFSBIGFH);
6187 }
6188 bcopy(fh.fh_data, np->n_fhp, fh.fh_len);
6189 np->n_fhsize = fh.fh_len;
b0d623f7 6190 nfs_node_lock_force(np);
2d21ac55 6191 error = nfs_loadattrcache(np, &nvattr, &xid, 0);
b0d623f7 6192 nfs_node_unlock(np);
2d21ac55
A
6193 nfsmout_if(error);
6194 newnp = np;
6195 } else if (NFS_CMPFH(dnp, fh.fh_data, fh.fh_len)) {
b0d623f7 6196 nfs_node_lock_force(dnp);
2d21ac55
A
6197 if (dnp->n_xid <= xid)
6198 error = nfs_loadattrcache(dnp, &nvattr, &xid, 0);
b0d623f7 6199 nfs_node_unlock(dnp);
2d21ac55
A
6200 nfsmout_if(error);
6201 newnp = dnp;
6202 } else {
6203 struct componentname cn, *cnp = &cn;
6204 bzero(cnp, sizeof(*cnp));
6205 cnp->cn_nameptr = name;
6206 cnp->cn_namelen = namelen;
6207 error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len,
6d2010ae 6208 &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np);
2d21ac55
A
6209 nfsmout_if(error);
6210 newnp = np;
6211 }
6212
6213nfsmout:
6214 if (npp && !*npp && !error)
6215 *npp = newnp;
6d2010ae 6216 NVATTR_CLEANUP(&nvattr);
2d21ac55
A
6217 return (error);
6218}
6219
6220/*
6221 * set up and initialize a "._" file lookup structure used for
6222 * performing async lookups.
6223 */
6224void
b0d623f7 6225nfs_dulookup_init(struct nfs_dulookup *dulp, nfsnode_t dnp, const char *name, int namelen, vfs_context_t ctx)
2d21ac55
A
6226{
6227 int error, du_namelen;
6228 vnode_t du_vp;
6d2010ae 6229 struct nfsmount *nmp = NFSTONMP(dnp);
2d21ac55
A
6230
6231 /* check for ._ file in name cache */
6232 dulp->du_flags = 0;
6233 bzero(&dulp->du_cn, sizeof(dulp->du_cn));
6234 du_namelen = namelen + 2;
6d2010ae
A
6235 if (!nmp || NMFLAG(nmp, NONEGNAMECACHE))
6236 return;
2d21ac55
A
6237 if ((namelen >= 2) && (name[0] == '.') && (name[1] == '_'))
6238 return;
6239 if (du_namelen >= (int)sizeof(dulp->du_smallname))
6240 MALLOC(dulp->du_cn.cn_nameptr, char *, du_namelen + 1, M_TEMP, M_WAITOK);
6241 else
6242 dulp->du_cn.cn_nameptr = dulp->du_smallname;
6243 if (!dulp->du_cn.cn_nameptr)
6244 return;
6245 dulp->du_cn.cn_namelen = du_namelen;
6246 snprintf(dulp->du_cn.cn_nameptr, du_namelen + 1, "._%s", name);
6247 dulp->du_cn.cn_nameptr[du_namelen] = '\0';
b0d623f7
A
6248 dulp->du_cn.cn_nameiop = LOOKUP;
6249 dulp->du_cn.cn_flags = MAKEENTRY;
2d21ac55
A
6250
6251 error = cache_lookup(NFSTOV(dnp), &du_vp, &dulp->du_cn);
b0d623f7 6252 if (error == -1) {
2d21ac55 6253 vnode_put(du_vp);
b0d623f7 6254 } else if (!error) {
6d2010ae
A
6255 nmp = NFSTONMP(dnp);
6256 if (nmp && (nmp->nm_vers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) {
b0d623f7
A
6257 /* if rdirplus, try dir buf cache lookup */
6258 nfsnode_t du_np = NULL;
6259 if (!nfs_dir_buf_cache_lookup(dnp, &du_np, &dulp->du_cn, ctx, 0) && du_np) {
6260 /* dir buf cache hit */
6261 du_vp = NFSTOV(du_np);
6262 vnode_put(du_vp);
6263 error = -1;
6264 }
6265 }
6266 if (!error)
6267 dulp->du_flags |= NFS_DULOOKUP_DOIT;
6268 }
2d21ac55
A
6269}
6270
6271/*
6272 * start an async "._" file lookup request
6273 */
6274void
6275nfs_dulookup_start(struct nfs_dulookup *dulp, nfsnode_t dnp, vfs_context_t ctx)
6276{
6277 struct nfsmount *nmp = NFSTONMP(dnp);
6278 struct nfsreq *req = &dulp->du_req;
6279
6d2010ae 6280 if (!nmp || !(dulp->du_flags & NFS_DULOOKUP_DOIT) || (dulp->du_flags & NFS_DULOOKUP_INPROG))
2d21ac55
A
6281 return;
6282 if (!nmp->nm_funcs->nf_lookup_rpc_async(dnp, dulp->du_cn.cn_nameptr,
6283 dulp->du_cn.cn_namelen, ctx, &req))
6284 dulp->du_flags |= NFS_DULOOKUP_INPROG;
6285}
6286
6287/*
6288 * finish an async "._" file lookup request and clean up the structure
6289 */
6290void
6291nfs_dulookup_finish(struct nfs_dulookup *dulp, nfsnode_t dnp, vfs_context_t ctx)
1c79356b 6292{
2d21ac55
A
6293 struct nfsmount *nmp = NFSTONMP(dnp);
6294 int error;
6295 nfsnode_t du_np;
6296 u_int64_t xid;
6297 fhandle_t fh;
91447636 6298 struct nfs_vattr nvattr;
1c79356b 6299
2d21ac55
A
6300 if (!nmp || !(dulp->du_flags & NFS_DULOOKUP_INPROG))
6301 goto out;
55e303ae 6302
6d2010ae
A
6303 NVATTR_INIT(&nvattr);
6304 error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, dulp->du_cn.cn_nameptr,
6305 dulp->du_cn.cn_namelen, ctx, &dulp->du_req, &xid, &fh, &nvattr);
2d21ac55
A
6306 dulp->du_flags &= ~NFS_DULOOKUP_INPROG;
6307 if (error == ENOENT) {
6308 /* add a negative entry in the name cache */
b0d623f7 6309 nfs_node_lock_force(dnp);
2d21ac55
A
6310 cache_enter(NFSTOV(dnp), NULL, &dulp->du_cn);
6311 dnp->n_flag |= NNEGNCENTRIES;
b0d623f7 6312 nfs_node_unlock(dnp);
2d21ac55
A
6313 } else if (!error) {
6314 error = nfs_nget(NFSTOMP(dnp), dnp, &dulp->du_cn, fh.fh_data, fh.fh_len,
6d2010ae 6315 &nvattr, &xid, dulp->du_req.r_auth, NG_MAKEENTRY, &du_np);
2d21ac55 6316 if (!error) {
b0d623f7 6317 nfs_node_unlock(du_np);
2d21ac55 6318 vnode_put(NFSTOV(du_np));
1c79356b 6319 }
1c79356b 6320 }
6d2010ae 6321 NVATTR_CLEANUP(&nvattr);
2d21ac55
A
6322out:
6323 if (dulp->du_flags & NFS_DULOOKUP_INPROG)
6324 nfs_request_async_cancel(&dulp->du_req);
6325 if (dulp->du_cn.cn_nameptr && (dulp->du_cn.cn_nameptr != dulp->du_smallname))
6326 FREE(dulp->du_cn.cn_nameptr, M_TEMP);
1c79356b
A
6327}
6328
2d21ac55 6329
1c79356b 6330/*
2d21ac55 6331 * NFS Version 3 commit RPC
1c79356b 6332 */
55e303ae 6333int
2d21ac55
A
6334nfs3_commit_rpc(
6335 nfsnode_t np,
6d2010ae
A
6336 uint64_t offset,
6337 uint64_t count,
6338 kauth_cred_t cred,
6339 uint64_t wverf)
1c79356b 6340{
2d21ac55
A
6341 struct nfsmount *nmp;
6342 int error = 0, lockerror, status, wccpostattr = 0, nfsvers;
91447636 6343 struct timespec premtime = { 0, 0 };
6d2010ae 6344 u_int64_t xid, newwverf;
2d21ac55
A
6345 uint32_t count32;
6346 struct nfsm_chain nmreq, nmrep;
8f6c56a5 6347
2d21ac55
A
6348 nmp = NFSTONMP(np);
6349 FSDBG(521, np, offset, count, nmp ? nmp->nm_state : 0);
fe8ab488 6350 if (nfs_mount_gone(nmp))
55e303ae 6351 return (ENXIO);
2d21ac55 6352 if (!(nmp->nm_state & NFSSTA_HASWRITEVERF))
1c79356b 6353 return (0);
2d21ac55
A
6354 nfsvers = nmp->nm_vers;
6355
6356 if (count > UINT32_MAX)
6357 count32 = 0;
6358 else
6359 count32 = count;
6360
6361 nfsm_chain_null(&nmreq);
6362 nfsm_chain_null(&nmrep);
6363
6364 nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(NFS_VER3));
6365 nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
6366 nfsm_chain_add_64(error, &nmreq, offset);
6367 nfsm_chain_add_32(error, &nmreq, count32);
6368 nfsm_chain_build_done(error, &nmreq);
6369 nfsmout_if(error);
6370 error = nfs_request2(np, NULL, &nmreq, NFSPROC_COMMIT,
6d2010ae 6371 current_thread(), cred, NULL, 0, &nmrep, &xid, &status);
b0d623f7 6372 if ((lockerror = nfs_node_lock(np)))
2d21ac55
A
6373 error = lockerror;
6374 /* can we do anything useful with the wcc info? */
6375 nfsm_chain_get_wcc_data(error, &nmrep, np, &premtime, &wccpostattr, &xid);
6376 if (!lockerror)
b0d623f7 6377 nfs_node_unlock(np);
2d21ac55
A
6378 if (!error)
6379 error = status;
6d2010ae 6380 nfsm_chain_get_64(error, &nmrep, newwverf);
2d21ac55
A
6381 nfsmout_if(error);
6382 lck_mtx_lock(&nmp->nm_lock);
6d2010ae
A
6383 if (nmp->nm_verf != newwverf)
6384 nmp->nm_verf = newwverf;
6385 if (wverf != newwverf)
2d21ac55 6386 error = NFSERR_STALEWRITEVERF;
2d21ac55
A
6387 lck_mtx_unlock(&nmp->nm_lock);
6388nfsmout:
6389 nfsm_chain_cleanup(&nmreq);
6390 nfsm_chain_cleanup(&nmrep);
1c79356b
A
6391 return (error);
6392}
6393
2d21ac55 6394
b0d623f7 6395int
2d21ac55 6396nfs_vnop_blockmap(
91447636
A
6397 __unused struct vnop_blockmap_args /* {
6398 struct vnodeop_desc *a_desc;
6399 vnode_t a_vp;
6400 off_t a_foffset;
6401 size_t a_size;
6402 daddr64_t *a_bpn;
6403 size_t *a_run;
6404 void *a_poff;
6405 int a_flags;
6406 } */ *ap)
1c79356b 6407{
91447636 6408 return (ENOTSUP);
1c79356b
A
6409}
6410
1c79356b
A
6411
6412/*
2d21ac55 6413 * fsync vnode op. Just call nfs_flush().
1c79356b
A
6414 */
6415/* ARGSUSED */
b0d623f7 6416int
2d21ac55 6417nfs_vnop_fsync(
91447636 6418 struct vnop_fsync_args /* {
1c79356b 6419 struct vnodeop_desc *a_desc;
91447636
A
6420 vnode_t a_vp;
6421 int a_waitfor;
6422 vfs_context_t a_context;
2d21ac55 6423 } */ *ap)
1c79356b 6424{
2d21ac55 6425 return (nfs_flush(VTONFS(ap->a_vp), ap->a_waitfor, vfs_context_thread(ap->a_context), 0));
1c79356b 6426}
1c79356b 6427
1c79356b 6428
55e303ae 6429/*
2d21ac55 6430 * Do an NFS pathconf RPC.
55e303ae 6431 */
91447636 6432int
2d21ac55
A
6433nfs3_pathconf_rpc(
6434 nfsnode_t np,
6435 struct nfs_fsattr *nfsap,
6436 vfs_context_t ctx)
55e303ae 6437{
2d21ac55
A
6438 u_int64_t xid;
6439 int error = 0, lockerror, status, nfsvers;
6440 struct nfsm_chain nmreq, nmrep;
6441 struct nfsmount *nmp = NFSTONMP(np);
6442 uint32_t val = 0;
483a1d10 6443
fe8ab488 6444 if (nfs_mount_gone(nmp))
2d21ac55
A
6445 return (ENXIO);
6446 nfsvers = nmp->nm_vers;
1c79356b 6447
2d21ac55
A
6448 nfsm_chain_null(&nmreq);
6449 nfsm_chain_null(&nmrep);
1c79356b 6450
91447636 6451 /* fetch pathconf info from server */
2d21ac55
A
6452 nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(NFS_VER3));
6453 nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
6454 nfsm_chain_build_done(error, &nmreq);
6455 nfsmout_if(error);
6d2010ae 6456 error = nfs_request(np, NULL, &nmreq, NFSPROC_PATHCONF, ctx, NULL, &nmrep, &xid, &status);
b0d623f7 6457 if ((lockerror = nfs_node_lock(np)))
2d21ac55
A
6458 error = lockerror;
6459 nfsm_chain_postop_attr_update(error, &nmrep, np, &xid);
6460 if (!lockerror)
b0d623f7 6461 nfs_node_unlock(np);
2d21ac55
A
6462 if (!error)
6463 error = status;
6464 nfsm_chain_get_32(error, &nmrep, nfsap->nfsa_maxlink);
6465 nfsm_chain_get_32(error, &nmrep, nfsap->nfsa_maxname);
fe8ab488 6466 nfsap->nfsa_flags &= ~(NFS_FSFLAG_NO_TRUNC|NFS_FSFLAG_CHOWN_RESTRICTED|NFS_FSFLAG_CASE_INSENSITIVE|NFS_FSFLAG_CASE_PRESERVING);
2d21ac55
A
6467 nfsm_chain_get_32(error, &nmrep, val);
6468 if (val)
6469 nfsap->nfsa_flags |= NFS_FSFLAG_NO_TRUNC;
6470 nfsm_chain_get_32(error, &nmrep, val);
6471 if (val)
6472 nfsap->nfsa_flags |= NFS_FSFLAG_CHOWN_RESTRICTED;
6473 nfsm_chain_get_32(error, &nmrep, val);
6474 if (val)
6475 nfsap->nfsa_flags |= NFS_FSFLAG_CASE_INSENSITIVE;
6476 nfsm_chain_get_32(error, &nmrep, val);
6477 if (val)
6478 nfsap->nfsa_flags |= NFS_FSFLAG_CASE_PRESERVING;
6479 NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_MAXLINK);
6480 NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_MAXNAME);
6481 NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_NO_TRUNC);
6482 NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_CHOWN_RESTRICTED);
6483 NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_INSENSITIVE);
6484 NFS_BITMAP_SET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_PRESERVING);
6485nfsmout:
6486 nfsm_chain_cleanup(&nmreq);
6487 nfsm_chain_cleanup(&nmrep);
91447636 6488 return (error);
1c79356b
A
6489}
6490
2d21ac55 6491/* save pathconf info for NFSv3 mount */
91447636 6492void
2d21ac55 6493nfs3_pathconf_cache(struct nfsmount *nmp, struct nfs_fsattr *nfsap)
1c79356b 6494{
2d21ac55
A
6495 nmp->nm_fsattr.nfsa_maxlink = nfsap->nfsa_maxlink;
6496 nmp->nm_fsattr.nfsa_maxname = nfsap->nfsa_maxname;
fe8ab488 6497 nmp->nm_fsattr.nfsa_flags &= ~(NFS_FSFLAG_NO_TRUNC|NFS_FSFLAG_CHOWN_RESTRICTED|NFS_FSFLAG_CASE_INSENSITIVE|NFS_FSFLAG_CASE_PRESERVING);
2d21ac55
A
6498 nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_NO_TRUNC;
6499 nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_CHOWN_RESTRICTED;
6500 nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_CASE_INSENSITIVE;
6501 nmp->nm_fsattr.nfsa_flags |= nfsap->nfsa_flags & NFS_FSFLAG_CASE_PRESERVING;
6502 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXLINK);
6503 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME);
6504 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_NO_TRUNC);
6505 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CHOWN_RESTRICTED);
6506 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CASE_INSENSITIVE);
6507 NFS_BITMAP_SET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_CASE_PRESERVING);
91447636 6508 nmp->nm_state |= NFSSTA_GOTPATHCONF;
1c79356b
A
6509}
6510
6511/*
91447636
A
6512 * Return POSIX pathconf information applicable to nfs.
6513 *
6514 * The NFS V2 protocol doesn't support this, so just return EINVAL
6515 * for V2.
1c79356b 6516 */
91447636 6517/* ARGSUSED */
b0d623f7 6518int
2d21ac55 6519nfs_vnop_pathconf(
91447636
A
6520 struct vnop_pathconf_args /* {
6521 struct vnodeop_desc *a_desc;
6522 vnode_t a_vp;
6523 int a_name;
b0d623f7 6524 int32_t *a_retval;
91447636 6525 vfs_context_t a_context;
2d21ac55 6526 } */ *ap)
1c79356b 6527{
91447636 6528 vnode_t vp = ap->a_vp;
2d21ac55 6529 nfsnode_t np = VTONFS(vp);
91447636 6530 struct nfsmount *nmp;
2d21ac55
A
6531 struct nfs_fsattr nfsa, *nfsap;
6532 int error = 0;
6533 uint64_t maxFileSize;
6534 uint nbits;
1c79356b 6535
2d21ac55 6536 nmp = VTONMP(vp);
fe8ab488 6537 if (nfs_mount_gone(nmp))
91447636 6538 return (ENXIO);
1c79356b 6539
91447636
A
6540 switch (ap->a_name) {
6541 case _PC_LINK_MAX:
6542 case _PC_NAME_MAX:
6543 case _PC_CHOWN_RESTRICTED:
6544 case _PC_NO_TRUNC:
6545 case _PC_CASE_SENSITIVE:
6546 case _PC_CASE_PRESERVING:
6547 break;
2d21ac55
A
6548 case _PC_FILESIZEBITS:
6549 if (nmp->nm_vers == NFS_VER2) {
6550 *ap->a_retval = 32;
6551 return (0);
6552 }
6553 break;
6d2010ae
A
6554 case _PC_XATTR_SIZE_BITS:
6555 /* Do we support xattrs natively? */
6556 if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR)
6557 break; /* Yes */
6558 /* No... so just return an error */
6559 /* FALLTHROUGH */
91447636
A
6560 default:
6561 /* don't bother contacting the server if we know the answer */
6562 return (EINVAL);
6563 }
1c79356b 6564
2d21ac55
A
6565 if (nmp->nm_vers == NFS_VER2)
6566 return (EINVAL);
6567
6568 lck_mtx_lock(&nmp->nm_lock);
6569 if (nmp->nm_vers == NFS_VER3) {
6570 if (!(nmp->nm_state & NFSSTA_GOTPATHCONF)) {
6571 /* no pathconf info cached */
6572 lck_mtx_unlock(&nmp->nm_lock);
6573 NFS_CLEAR_ATTRIBUTES(nfsa.nfsa_bitmap);
6574 error = nfs3_pathconf_rpc(np, &nfsa, ap->a_context);
6575 if (error)
6576 return (error);
6577 nmp = VTONMP(vp);
fe8ab488 6578 if (nfs_mount_gone(nmp))
2d21ac55
A
6579 return (ENXIO);
6580 lck_mtx_lock(&nmp->nm_lock);
6581 if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_HOMOGENEOUS) {
6582 /* all files have the same pathconf info, */
6583 /* so cache a copy of the results */
6584 nfs3_pathconf_cache(nmp, &nfsa);
6585 }
6586 nfsap = &nfsa;
6587 } else {
6588 nfsap = &nmp->nm_fsattr;
6589 }
6590 } else if (!(nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_HOMOGENEOUS)) {
91447636 6591 /* no pathconf info cached */
2d21ac55
A
6592 lck_mtx_unlock(&nmp->nm_lock);
6593 NFS_CLEAR_ATTRIBUTES(nfsa.nfsa_bitmap);
6594 error = nfs4_pathconf_rpc(np, &nfsa, ap->a_context);
91447636
A
6595 if (error)
6596 return (error);
2d21ac55 6597 nmp = VTONMP(vp);
fe8ab488 6598 if (nfs_mount_gone(nmp))
91447636 6599 return (ENXIO);
2d21ac55
A
6600 lck_mtx_lock(&nmp->nm_lock);
6601 nfsap = &nfsa;
6602 } else {
6603 nfsap = &nmp->nm_fsattr;
91447636 6604 }
1c79356b 6605
91447636
A
6606 switch (ap->a_name) {
6607 case _PC_LINK_MAX:
2d21ac55
A
6608 if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_MAXLINK))
6609 *ap->a_retval = nfsap->nfsa_maxlink;
6610 else if ((nmp->nm_vers == NFS_VER4) && NFS_BITMAP_ISSET(np->n_vattr.nva_bitmap, NFS_FATTR_MAXLINK))
6611 *ap->a_retval = np->n_vattr.nva_maxlink;
91447636 6612 else
2d21ac55 6613 error = EINVAL;
91447636 6614 break;
2d21ac55
A
6615 case _PC_NAME_MAX:
6616 if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_MAXNAME))
6617 *ap->a_retval = nfsap->nfsa_maxname;
6618 else
6619 error = EINVAL;
6620 break;
6621 case _PC_CHOWN_RESTRICTED:
6622 if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_CHOWN_RESTRICTED))
b0d623f7 6623 *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_CHOWN_RESTRICTED) ? 200112 /* _POSIX_CHOWN_RESTRICTED */ : 0;
2d21ac55
A
6624 else
6625 error = EINVAL;
6626 break;
6627 case _PC_NO_TRUNC:
6628 if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_NO_TRUNC))
b0d623f7 6629 *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_NO_TRUNC) ? 200112 /* _POSIX_NO_TRUNC */ : 0;
2d21ac55
A
6630 else
6631 error = EINVAL;
6632 break;
6633 case _PC_CASE_SENSITIVE:
6634 if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_INSENSITIVE))
b0d623f7 6635 *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_CASE_INSENSITIVE) ? 0 : 1;
2d21ac55
A
6636 else
6637 error = EINVAL;
6638 break;
6639 case _PC_CASE_PRESERVING:
6640 if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_PRESERVING))
b0d623f7 6641 *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_CASE_PRESERVING) ? 1 : 0;
2d21ac55
A
6642 else
6643 error = EINVAL;
6644 break;
6d2010ae 6645 case _PC_XATTR_SIZE_BITS: /* same as file size bits if named attrs supported */
2d21ac55 6646 case _PC_FILESIZEBITS:
b0d623f7 6647 if (!NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_MAXFILESIZE)) {
2d21ac55
A
6648 *ap->a_retval = 64;
6649 error = 0;
6650 break;
1c79356b 6651 }
b0d623f7 6652 maxFileSize = nfsap->nfsa_maxfilesize;
2d21ac55
A
6653 nbits = 1;
6654 if (maxFileSize & 0xffffffff00000000ULL) {
6655 nbits += 32;
6656 maxFileSize >>= 32;
91447636 6657 }
2d21ac55
A
6658 if (maxFileSize & 0xffff0000) {
6659 nbits += 16;
6660 maxFileSize >>= 16;
6661 }
6662 if (maxFileSize & 0xff00) {
6663 nbits += 8;
6664 maxFileSize >>= 8;
6665 }
6666 if (maxFileSize & 0xf0) {
6667 nbits += 4;
6668 maxFileSize >>= 4;
91447636 6669 }
2d21ac55
A
6670 if (maxFileSize & 0xc) {
6671 nbits += 2;
6672 maxFileSize >>= 2;
6673 }
6674 if (maxFileSize & 0x2) {
6675 nbits += 1;
6676 }
6677 *ap->a_retval = nbits;
6678 break;
6679 default:
6680 error = EINVAL;
6681 }
1c79356b 6682
2d21ac55
A
6683 lck_mtx_unlock(&nmp->nm_lock);
6684
6685 return (error);
1c79356b
A
6686}
6687
1c79356b
A
6688/*
6689 * Read wrapper for special devices.
6690 */
b0d623f7 6691int
2d21ac55 6692nfsspec_vnop_read(
91447636
A
6693 struct vnop_read_args /* {
6694 struct vnodeop_desc *a_desc;
6695 vnode_t a_vp;
1c79356b 6696 struct uio *a_uio;
91447636
A
6697 int a_ioflag;
6698 vfs_context_t a_context;
2d21ac55 6699 } */ *ap)
1c79356b 6700{
2d21ac55 6701 nfsnode_t np = VTONFS(ap->a_vp);
5ba3f43e 6702 struct timespec now;
2d21ac55 6703 int error;
1c79356b
A
6704
6705 /*
6706 * Set access flag.
6707 */
b0d623f7 6708 if ((error = nfs_node_lock(np)))
2d21ac55 6709 return (error);
1c79356b 6710 np->n_flag |= NACC;
5ba3f43e 6711 nanotime(&now);
55e303ae 6712 np->n_atim.tv_sec = now.tv_sec;
5ba3f43e 6713 np->n_atim.tv_nsec = now.tv_nsec;
b0d623f7 6714 nfs_node_unlock(np);
91447636 6715 return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_read), ap));
1c79356b
A
6716}
6717
6718/*
6719 * Write wrapper for special devices.
6720 */
b0d623f7 6721int
2d21ac55 6722nfsspec_vnop_write(
91447636
A
6723 struct vnop_write_args /* {
6724 struct vnodeop_desc *a_desc;
6725 vnode_t a_vp;
1c79356b 6726 struct uio *a_uio;
91447636
A
6727 int a_ioflag;
6728 vfs_context_t a_context;
2d21ac55 6729 } */ *ap)
1c79356b 6730{
2d21ac55 6731 nfsnode_t np = VTONFS(ap->a_vp);
5ba3f43e 6732 struct timespec now;
2d21ac55 6733 int error;
1c79356b
A
6734
6735 /*
6736 * Set update flag.
6737 */
b0d623f7 6738 if ((error = nfs_node_lock(np)))
2d21ac55 6739 return (error);
1c79356b 6740 np->n_flag |= NUPD;
5ba3f43e 6741 nanotime(&now);
55e303ae 6742 np->n_mtim.tv_sec = now.tv_sec;
5ba3f43e 6743 np->n_mtim.tv_nsec = now.tv_nsec;
b0d623f7 6744 nfs_node_unlock(np);
91447636 6745 return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_write), ap));
1c79356b
A
6746}
6747
6748/*
6749 * Close wrapper for special devices.
6750 *
6751 * Update the times on the nfsnode then do device close.
6752 */
b0d623f7 6753int
2d21ac55 6754nfsspec_vnop_close(
91447636
A
6755 struct vnop_close_args /* {
6756 struct vnodeop_desc *a_desc;
6757 vnode_t a_vp;
6758 int a_fflag;
6759 vfs_context_t a_context;
2d21ac55 6760 } */ *ap)
1c79356b 6761{
91447636 6762 vnode_t vp = ap->a_vp;
2d21ac55 6763 nfsnode_t np = VTONFS(vp);
91447636
A
6764 struct vnode_attr vattr;
6765 mount_t mp;
2d21ac55 6766 int error;
1c79356b 6767
b0d623f7 6768 if ((error = nfs_node_lock(np)))
2d21ac55 6769 return (error);
1c79356b
A
6770 if (np->n_flag & (NACC | NUPD)) {
6771 np->n_flag |= NCHG;
b0d623f7 6772 if (!vnode_isinuse(vp, 0) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) {
91447636
A
6773 VATTR_INIT(&vattr);
6774 if (np->n_flag & NACC) {
6775 vattr.va_access_time = np->n_atim;
6776 VATTR_SET_ACTIVE(&vattr, va_access_time);
6777 }
6778 if (np->n_flag & NUPD) {
6779 vattr.va_modify_time = np->n_mtim;
6780 VATTR_SET_ACTIVE(&vattr, va_modify_time);
6781 }
b0d623f7 6782 nfs_node_unlock(np);
91447636 6783 vnode_setattr(vp, &vattr, ap->a_context);
2d21ac55 6784 } else {
b0d623f7 6785 nfs_node_unlock(np);
1c79356b 6786 }
2d21ac55 6787 } else {
b0d623f7 6788 nfs_node_unlock(np);
1c79356b 6789 }
91447636 6790 return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_close), ap));
1c79356b
A
6791}
6792
2d21ac55 6793#if FIFO
91447636
A
6794extern vnop_t **fifo_vnodeop_p;
6795
1c79356b
A
6796/*
6797 * Read wrapper for fifos.
6798 */
b0d623f7 6799int
2d21ac55 6800nfsfifo_vnop_read(
91447636
A
6801 struct vnop_read_args /* {
6802 struct vnodeop_desc *a_desc;
6803 vnode_t a_vp;
1c79356b 6804 struct uio *a_uio;
91447636
A
6805 int a_ioflag;
6806 vfs_context_t a_context;
2d21ac55 6807 } */ *ap)
1c79356b 6808{
2d21ac55 6809 nfsnode_t np = VTONFS(ap->a_vp);
5ba3f43e 6810 struct timespec now;
2d21ac55 6811 int error;
1c79356b
A
6812
6813 /*
6814 * Set access flag.
6815 */
b0d623f7 6816 if ((error = nfs_node_lock(np)))
2d21ac55 6817 return (error);
1c79356b 6818 np->n_flag |= NACC;
5ba3f43e 6819 nanotime(&now);
55e303ae 6820 np->n_atim.tv_sec = now.tv_sec;
5ba3f43e 6821 np->n_atim.tv_nsec = now.tv_nsec;
b0d623f7 6822 nfs_node_unlock(np);
91447636 6823 return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_read), ap));
1c79356b
A
6824}
6825
6826/*
6827 * Write wrapper for fifos.
6828 */
b0d623f7 6829int
2d21ac55 6830nfsfifo_vnop_write(
91447636
A
6831 struct vnop_write_args /* {
6832 struct vnodeop_desc *a_desc;
6833 vnode_t a_vp;
1c79356b 6834 struct uio *a_uio;
91447636
A
6835 int a_ioflag;
6836 vfs_context_t a_context;
2d21ac55 6837 } */ *ap)
1c79356b 6838{
2d21ac55 6839 nfsnode_t np = VTONFS(ap->a_vp);
5ba3f43e 6840 struct timespec now;
2d21ac55 6841 int error;
1c79356b
A
6842
6843 /*
6844 * Set update flag.
6845 */
b0d623f7 6846 if ((error = nfs_node_lock(np)))
2d21ac55 6847 return (error);
1c79356b 6848 np->n_flag |= NUPD;
5ba3f43e 6849 nanotime(&now);
55e303ae 6850 np->n_mtim.tv_sec = now.tv_sec;
5ba3f43e 6851 np->n_mtim.tv_nsec = now.tv_nsec;
b0d623f7 6852 nfs_node_unlock(np);
91447636 6853 return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_write), ap));
1c79356b
A
6854}
6855
6856/*
6857 * Close wrapper for fifos.
6858 *
6859 * Update the times on the nfsnode then do fifo close.
6860 */
b0d623f7 6861int
2d21ac55 6862nfsfifo_vnop_close(
91447636
A
6863 struct vnop_close_args /* {
6864 struct vnodeop_desc *a_desc;
6865 vnode_t a_vp;
6866 int a_fflag;
6867 vfs_context_t a_context;
2d21ac55 6868 } */ *ap)
1c79356b 6869{
91447636 6870 vnode_t vp = ap->a_vp;
2d21ac55 6871 nfsnode_t np = VTONFS(vp);
91447636 6872 struct vnode_attr vattr;
5ba3f43e 6873 struct timespec now;
91447636 6874 mount_t mp;
2d21ac55 6875 int error;
1c79356b 6876
b0d623f7 6877 if ((error = nfs_node_lock(np)))
2d21ac55 6878 return (error);
1c79356b 6879 if (np->n_flag & (NACC | NUPD)) {
5ba3f43e 6880 nanotime(&now);
1c79356b 6881 if (np->n_flag & NACC) {
55e303ae 6882 np->n_atim.tv_sec = now.tv_sec;
5ba3f43e 6883 np->n_atim.tv_nsec = now.tv_nsec;
1c79356b
A
6884 }
6885 if (np->n_flag & NUPD) {
55e303ae 6886 np->n_mtim.tv_sec = now.tv_sec;
5ba3f43e 6887 np->n_mtim.tv_nsec = now.tv_nsec;
1c79356b
A
6888 }
6889 np->n_flag |= NCHG;
91447636
A
6890 if (!vnode_isinuse(vp, 1) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) {
6891 VATTR_INIT(&vattr);
6892 if (np->n_flag & NACC) {
6893 vattr.va_access_time = np->n_atim;
6894 VATTR_SET_ACTIVE(&vattr, va_access_time);
6895 }
6896 if (np->n_flag & NUPD) {
6897 vattr.va_modify_time = np->n_mtim;
6898 VATTR_SET_ACTIVE(&vattr, va_modify_time);
6899 }
b0d623f7 6900 nfs_node_unlock(np);
91447636 6901 vnode_setattr(vp, &vattr, ap->a_context);
2d21ac55 6902 } else {
b0d623f7 6903 nfs_node_unlock(np);
1c79356b 6904 }
2d21ac55 6905 } else {
b0d623f7 6906 nfs_node_unlock(np);
1c79356b 6907 }
91447636 6908 return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_close), ap));
1c79356b 6909}
2d21ac55 6910#endif /* FIFO */
1c79356b 6911
91447636 6912/*ARGSUSED*/
b0d623f7 6913int
2d21ac55 6914nfs_vnop_ioctl(
6d2010ae 6915 struct vnop_ioctl_args /* {
91447636
A
6916 struct vnodeop_desc *a_desc;
6917 vnode_t a_vp;
b0d623f7 6918 u_int32_t a_command;
91447636
A
6919 caddr_t a_data;
6920 int a_fflag;
2d21ac55 6921 vfs_context_t a_context;
91447636 6922 } */ *ap)
1c79356b 6923{
6d2010ae
A
6924 vfs_context_t ctx = ap->a_context;
6925 vnode_t vp = ap->a_vp;
39236c6e 6926 struct nfsmount *mp = VTONMP(vp);
3e170ce0
A
6927 struct user_nfs_gss_principal gprinc;
6928 uint32_t len;
6d2010ae 6929 int error = ENOTTY;
1c79356b 6930
39236c6e
A
6931 if (mp == NULL)
6932 return (ENXIO);
6933
6d2010ae
A
6934 switch (ap->a_command) {
6935
6936 case F_FULLFSYNC:
6937 if (vnode_vfsisrdonly(vp))
6938 return (EROFS);
6d2010ae
A
6939 error = nfs_flush(VTONFS(vp), MNT_WAIT, vfs_context_thread(ctx), 0);
6940 break;
5ba3f43e 6941 case NFS_IOC_DESTROY_CRED:
3e170ce0
A
6942 if (!auth_is_kerberized(mp->nm_auth))
6943 return (ENOTSUP);
fe8ab488 6944 error = nfs_gss_clnt_ctx_remove(mp, vfs_context_ucred(ctx));
39236c6e 6945 break;
5ba3f43e
A
6946 case NFS_IOC_SET_CRED:
6947 case NFS_IOC_SET_CRED64:
3e170ce0
A
6948 if (!auth_is_kerberized(mp->nm_auth))
6949 return (ENOTSUP);
5ba3f43e
A
6950 if ((ap->a_command == NFS_IOC_SET_CRED && vfs_context_is64bit(ctx)) ||
6951 (ap->a_command == NFS_IOC_SET_CRED64 && !vfs_context_is64bit(ctx)))
6952 return (EINVAL);
3e170ce0
A
6953 if (vfs_context_is64bit(ctx)) {
6954 gprinc = *(struct user_nfs_gss_principal *)ap->a_data;
6955 } else {
6956 struct nfs_gss_principal *tp;
6957 tp = (struct nfs_gss_principal *)ap->a_data;
6958 gprinc.princlen = tp->princlen;
6959 gprinc.nametype = tp->nametype;
6960 gprinc.principal = CAST_USER_ADDR_T(tp->principal);
6961 }
39037602 6962 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);
3e170ce0
A
6963 if (gprinc.princlen > MAXPATHLEN)
6964 return (EINVAL);
3e170ce0
A
6965 uint8_t *p;
6966 MALLOC(p, uint8_t *, gprinc.princlen+1, M_TEMP, M_WAITOK|M_ZERO);
6967 if (p == NULL)
6968 return (ENOMEM);
6969 error = copyin(gprinc.principal, p, gprinc.princlen);
6970 if (error) {
6971 NFS_DBG(NFS_FAC_GSS, 7, "NFS_FSCTL_SET_CRED could not copy in princiapl data of len %d: %d\n",
6972 gprinc.princlen, error);
6973 FREE(p, M_TEMP);
6974 return (error);
6975 }
6976 NFS_DBG(NFS_FAC_GSS, 7, "Seting credential to principal %s\n", p);
6977 error = nfs_gss_clnt_ctx_set_principal(mp, ctx, p, gprinc.princlen, gprinc.nametype);
6978 NFS_DBG(NFS_FAC_GSS, 7, "Seting credential to principal %s returned %d\n", p, error);
6979 FREE(p, M_TEMP);
6980 break;
5ba3f43e
A
6981 case NFS_IOC_GET_CRED:
6982 case NFS_IOC_GET_CRED64:
3e170ce0
A
6983 if (!auth_is_kerberized(mp->nm_auth))
6984 return (ENOTSUP);
5ba3f43e
A
6985 if ((ap->a_command == NFS_IOC_GET_CRED && vfs_context_is64bit(ctx)) ||
6986 (ap->a_command == NFS_IOC_GET_CRED64 && !vfs_context_is64bit(ctx)))
6987 return (EINVAL);
3e170ce0
A
6988 error = nfs_gss_clnt_ctx_get_principal(mp, ctx, &gprinc);
6989 if (error)
6990 break;
6991 if (vfs_context_is64bit(ctx)) {
6992 struct user_nfs_gss_principal *upp = (struct user_nfs_gss_principal *)ap->a_data;
6993 len = upp->princlen;
6994 if (gprinc.princlen < len)
6995 len = gprinc.princlen;
6996 upp->princlen = gprinc.princlen;
6997 upp->nametype = gprinc.nametype;
6998 upp->flags = gprinc.flags;
6999 if (gprinc.principal)
7000 error = copyout((void *)gprinc.principal, upp->principal, len);
7001 else
7002 upp->principal = USER_ADDR_NULL;
7003 } else {
7004 struct nfs_gss_principal *u32pp = (struct nfs_gss_principal *)ap->a_data;
7005 len = u32pp->princlen;
7006 if (gprinc.princlen < len)
7007 len = gprinc.princlen;
7008 u32pp->princlen = gprinc.princlen;
7009 u32pp->nametype = gprinc.nametype;
7010 u32pp->flags = gprinc.flags;
7011 if (gprinc.principal)
7012 error = copyout((void *)gprinc.principal, u32pp->principal, len);
7013 else
7014 u32pp->principal = (user32_addr_t)0;
7015 }
7016 if (error) {
7017 NFS_DBG(NFS_FAC_GSS, 7, "NFS_FSCTL_GET_CRED could not copy out princiapl data of len %d: %d\n",
7018 gprinc.princlen, error);
7019 }
39037602
A
7020 if (gprinc.principal)
7021 FREE(gprinc.principal, M_TEMP);
6d2010ae
A
7022 }
7023
7024 return (error);
1c79356b
A
7025}
7026
91447636 7027/*ARGSUSED*/
b0d623f7 7028int
2d21ac55 7029nfs_vnop_select(
91447636
A
7030 __unused struct vnop_select_args /* {
7031 struct vnodeop_desc *a_desc;
7032 vnode_t a_vp;
7033 int a_which;
7034 int a_fflags;
91447636 7035 void *a_wql;
2d21ac55 7036 vfs_context_t a_context;
91447636 7037 } */ *ap)
1c79356b
A
7038{
7039
7040 /*
7041 * We were once bogusly seltrue() which returns 1. Is this right?
7042 */
7043 return (1);
7044}
7045
1c79356b 7046/*
2d21ac55
A
7047 * vnode OP for pagein using UPL
7048 *
7049 * No buffer I/O, just RPCs straight into the mapped pages.
1c79356b 7050 */
b0d623f7 7051int
2d21ac55 7052nfs_vnop_pagein(
91447636
A
7053 struct vnop_pagein_args /* {
7054 struct vnodeop_desc *a_desc;
7055 vnode_t a_vp;
7056 upl_t a_pl;
7057 vm_offset_t a_pl_offset;
7058 off_t a_f_offset;
7059 size_t a_size;
7060 int a_flags;
7061 vfs_context_t a_context;
2d21ac55 7062 } */ *ap)
1c79356b 7063{
91447636 7064 vnode_t vp = ap->a_vp;
1c79356b 7065 upl_t pl = ap->a_pl;
2d21ac55 7066 size_t size = ap->a_size;
1c79356b
A
7067 off_t f_offset = ap->a_f_offset;
7068 vm_offset_t pl_offset = ap->a_pl_offset;
2d21ac55
A
7069 int flags = ap->a_flags;
7070 thread_t thd;
91447636 7071 kauth_cred_t cred;
2d21ac55
A
7072 nfsnode_t np = VTONFS(vp);
7073 size_t nmrsize, iosize, txsize, rxsize, retsize;
7074 off_t txoffset;
55e303ae 7075 struct nfsmount *nmp;
1c79356b 7076 int error = 0;
b0d623f7
A
7077 vm_offset_t ioaddr, rxaddr;
7078 uio_t uio;
7079 char uio_buf [ UIO_SIZEOF(1) ];
fa4905b1 7080 int nofreeupl = flags & UPL_NOCOMMIT;
55e303ae 7081 upl_page_info_t *plinfo;
2d21ac55
A
7082#define MAXPAGINGREQS 16 /* max outstanding RPCs for pagein/pageout */
7083 struct nfsreq *req[MAXPAGINGREQS];
7084 int nextsend, nextwait;
b0d623f7
A
7085 uint32_t stategenid = 0, restart = 0;
7086 kern_return_t kret;
1c79356b 7087
2d21ac55 7088 FSDBG(322, np, f_offset, size, flags);
fa4905b1
A
7089 if (pl == (upl_t)NULL)
7090 panic("nfs_pagein: no upl");
1c79356b 7091
fa4905b1 7092 if (size <= 0) {
2d21ac55 7093 printf("nfs_pagein: invalid size %ld", size);
fa4905b1 7094 if (!nofreeupl)
316670eb 7095 (void) ubc_upl_abort_range(pl, pl_offset, size, 0);
1c79356b 7096 return (EINVAL);
fa4905b1 7097 }
91447636 7098 if (f_offset < 0 || f_offset >= (off_t)np->n_size || (f_offset & PAGE_MASK_64)) {
fa4905b1 7099 if (!nofreeupl)
2d21ac55 7100 ubc_upl_abort_range(pl, pl_offset, size,
1c79356b
A
7101 UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY);
7102 return (EINVAL);
7103 }
91447636 7104
2d21ac55 7105 thd = vfs_context_thread(ap->a_context);
fa4905b1 7106 cred = ubc_getcred(vp);
0c530ab8 7107 if (!IS_VALID_CRED(cred))
91447636 7108 cred = vfs_context_ucred(ap->a_context);
1c79356b 7109
b0d623f7
A
7110 uio = uio_createwithbuffer(1, f_offset, UIO_SYSSPACE, UIO_READ,
7111 &uio_buf, sizeof(uio_buf));
1c79356b 7112
2d21ac55 7113 nmp = VTONMP(vp);
fe8ab488 7114 if (nfs_mount_gone(nmp)) {
55e303ae 7115 if (!nofreeupl)
2d21ac55 7116 ubc_upl_abort_range(pl, pl_offset, size,
55e303ae
A
7117 UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY);
7118 return (ENXIO);
7119 }
2d21ac55 7120 nmrsize = nmp->nm_rsize;
1c79356b 7121
55e303ae 7122 plinfo = ubc_upl_pageinfo(pl);
b0d623f7
A
7123 kret = ubc_upl_map(pl, &ioaddr);
7124 if (kret != KERN_SUCCESS)
7125 panic("nfs_vnop_pagein: ubc_upl_map() failed with (%d)", kret);
1c79356b 7126 ioaddr += pl_offset;
b0d623f7
A
7127
7128tryagain:
7129 if (nmp->nm_vers >= NFS_VER4)
7130 stategenid = nmp->nm_stategenid;
2d21ac55
A
7131 txsize = rxsize = size;
7132 txoffset = f_offset;
b0d623f7 7133 rxaddr = ioaddr;
1c79356b 7134
2d21ac55
A
7135 bzero(req, sizeof(req));
7136 nextsend = nextwait = 0;
1c79356b 7137 do {
6d2010ae
A
7138 if (np->n_flag & NREVOKE) {
7139 error = EIO;
7140 break;
7141 }
2d21ac55
A
7142 /* send requests while we need to and have available slots */
7143 while ((txsize > 0) && (req[nextsend] == NULL)) {
7144 iosize = MIN(nmrsize, txsize);
7145 if ((error = nmp->nm_funcs->nf_read_rpc_async(np, txoffset, iosize, thd, cred, NULL, &req[nextsend]))) {
7146 req[nextsend] = NULL;
7147 break;
7148 }
7149 txoffset += iosize;
7150 txsize -= iosize;
7151 nextsend = (nextsend + 1) % MAXPAGINGREQS;
7152 }
7153 /* wait while we need to and break out if more requests to send */
7154 while ((rxsize > 0) && req[nextwait]) {
7155 iosize = retsize = MIN(nmrsize, rxsize);
b0d623f7
A
7156 uio_reset(uio, uio_offset(uio), UIO_SYSSPACE, UIO_READ);
7157 uio_addiov(uio, CAST_USER_ADDR_T(rxaddr), iosize);
7158 FSDBG(322, uio_offset(uio), uio_resid(uio), rxaddr, rxsize);
7159#if UPL_DEBUG
7160 upl_ubc_alias_set(pl, (uintptr_t) current_thread(), (uintptr_t) 2);
91447636 7161#endif /* UPL_DEBUG */
316670eb 7162 OSAddAtomic64(1, &nfsstats.pageins);
2d21ac55
A
7163 error = nmp->nm_funcs->nf_read_rpc_async_finish(np, req[nextwait], uio, &retsize, NULL);
7164 req[nextwait] = NULL;
7165 nextwait = (nextwait + 1) % MAXPAGINGREQS;
b0d623f7
A
7166 if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error)) {
7167 lck_mtx_lock(&nmp->nm_lock);
6d2010ae
A
7168 if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) {
7169 NP(np, "nfs_vnop_pagein: error %d, initiating recovery", error);
7170 nfs_need_recover(nmp, error);
b0d623f7
A
7171 }
7172 lck_mtx_unlock(&nmp->nm_lock);
b0d623f7
A
7173 restart++;
7174 goto cancel;
7175 }
2d21ac55 7176 if (error) {
b0d623f7 7177 FSDBG(322, uio_offset(uio), uio_resid(uio), error, -1);
2d21ac55
A
7178 break;
7179 }
7180 if (retsize < iosize) {
7181 /* Just zero fill the rest of the valid area. */
2d21ac55 7182 int zcnt = iosize - retsize;
b0d623f7
A
7183 bzero((char *)rxaddr + retsize, zcnt);
7184 FSDBG(324, uio_offset(uio), retsize, zcnt, rxaddr);
7185 uio_update(uio, zcnt);
1c79356b 7186 }
b0d623f7 7187 rxaddr += iosize;
2d21ac55
A
7188 rxsize -= iosize;
7189 if (txsize)
7190 break;
1c79356b 7191 }
2d21ac55 7192 } while (!error && (txsize || rxsize));
1c79356b 7193
b0d623f7 7194 restart = 0;
1c79356b 7195
2d21ac55 7196 if (error) {
b0d623f7 7197cancel:
2d21ac55
A
7198 /* cancel any outstanding requests */
7199 while (req[nextwait]) {
7200 nfs_request_async_cancel(req[nextwait]);
7201 req[nextwait] = NULL;
7202 nextwait = (nextwait + 1) % MAXPAGINGREQS;
7203 }
6d2010ae
A
7204 if (np->n_flag & NREVOKE) {
7205 error = EIO;
7206 } else if (restart) {
7207 if (restart <= nfs_mount_state_max_restarts(nmp)) { /* guard against no progress */
7208 if (error == NFSERR_GRACE)
7209 tsleep(&nmp->nm_state, (PZERO-1), "nfsgrace", 2*hz);
7210 if (!(error = nfs_mount_state_wait_for_recovery(nmp)))
7211 goto tryagain;
7212 } else {
7213 NP(np, "nfs_pagein: too many restarts, aborting");
7214 }
b0d623f7 7215 }
2d21ac55
A
7216 }
7217
b0d623f7
A
7218 ubc_upl_unmap(pl);
7219
fa4905b1 7220 if (!nofreeupl) {
2d21ac55
A
7221 if (error)
7222 ubc_upl_abort_range(pl, pl_offset, size,
fa4905b1
A
7223 UPL_ABORT_ERROR |
7224 UPL_ABORT_FREE_ON_EMPTY);
1c79356b 7225 else
0b4e3aa0 7226 ubc_upl_commit_range(pl, pl_offset, size,
fa4905b1
A
7227 UPL_COMMIT_CLEAR_DIRTY |
7228 UPL_COMMIT_FREE_ON_EMPTY);
1c79356b 7229 }
1c79356b
A
7230 return (error);
7231}
7232
0b4e3aa0 7233
1c79356b 7234/*
2d21ac55
A
7235 * the following are needed only by nfs_pageout to know how to handle errors
7236 * see nfs_pageout comments on explanation of actions.
7237 * the errors here are copied from errno.h and errors returned by servers
7238 * are expected to match the same numbers here. If not, our actions maybe
7239 * erroneous.
7240 */
b0d623f7 7241char nfs_pageouterrorhandler(int);
39236c6e 7242enum actiontype {NOACTION, DUMP, DUMPANDLOG, RETRY, SEVER};
2d21ac55
A
7243#define NFS_ELAST 88
7244static u_char errorcount[NFS_ELAST+1]; /* better be zeros when initialized */
7245static const char errortooutcome[NFS_ELAST+1] = {
7246 NOACTION,
7247 DUMP, /* EPERM 1 Operation not permitted */
7248 DUMP, /* ENOENT 2 No such file or directory */
7249 DUMPANDLOG, /* ESRCH 3 No such process */
7250 RETRY, /* EINTR 4 Interrupted system call */
7251 DUMP, /* EIO 5 Input/output error */
7252 DUMP, /* ENXIO 6 Device not configured */
7253 DUMPANDLOG, /* E2BIG 7 Argument list too long */
7254 DUMPANDLOG, /* ENOEXEC 8 Exec format error */
7255 DUMPANDLOG, /* EBADF 9 Bad file descriptor */
7256 DUMPANDLOG, /* ECHILD 10 No child processes */
7257 DUMPANDLOG, /* EDEADLK 11 Resource deadlock avoided - was EAGAIN */
7258 RETRY, /* ENOMEM 12 Cannot allocate memory */
7259 DUMP, /* EACCES 13 Permission denied */
7260 DUMPANDLOG, /* EFAULT 14 Bad address */
7261 DUMPANDLOG, /* ENOTBLK 15 POSIX - Block device required */
7262 RETRY, /* EBUSY 16 Device busy */
7263 DUMP, /* EEXIST 17 File exists */
7264 DUMP, /* EXDEV 18 Cross-device link */
7265 DUMP, /* ENODEV 19 Operation not supported by device */
7266 DUMP, /* ENOTDIR 20 Not a directory */
7267 DUMP, /* EISDIR 21 Is a directory */
7268 DUMP, /* EINVAL 22 Invalid argument */
7269 DUMPANDLOG, /* ENFILE 23 Too many open files in system */
7270 DUMPANDLOG, /* EMFILE 24 Too many open files */
7271 DUMPANDLOG, /* ENOTTY 25 Inappropriate ioctl for device */
7272 DUMPANDLOG, /* ETXTBSY 26 Text file busy - POSIX */
7273 DUMP, /* EFBIG 27 File too large */
7274 DUMP, /* ENOSPC 28 No space left on device */
7275 DUMPANDLOG, /* ESPIPE 29 Illegal seek */
7276 DUMP, /* EROFS 30 Read-only file system */
7277 DUMP, /* EMLINK 31 Too many links */
7278 RETRY, /* EPIPE 32 Broken pipe */
7279 /* math software */
7280 DUMPANDLOG, /* EDOM 33 Numerical argument out of domain */
7281 DUMPANDLOG, /* ERANGE 34 Result too large */
7282 RETRY, /* EAGAIN/EWOULDBLOCK 35 Resource temporarily unavailable */
7283 DUMPANDLOG, /* EINPROGRESS 36 Operation now in progress */
7284 DUMPANDLOG, /* EALREADY 37 Operation already in progress */
7285 /* ipc/network software -- argument errors */
7286 DUMPANDLOG, /* ENOTSOC 38 Socket operation on non-socket */
7287 DUMPANDLOG, /* EDESTADDRREQ 39 Destination address required */
7288 DUMPANDLOG, /* EMSGSIZE 40 Message too long */
7289 DUMPANDLOG, /* EPROTOTYPE 41 Protocol wrong type for socket */
7290 DUMPANDLOG, /* ENOPROTOOPT 42 Protocol not available */
7291 DUMPANDLOG, /* EPROTONOSUPPORT 43 Protocol not supported */
7292 DUMPANDLOG, /* ESOCKTNOSUPPORT 44 Socket type not supported */
7293 DUMPANDLOG, /* ENOTSUP 45 Operation not supported */
7294 DUMPANDLOG, /* EPFNOSUPPORT 46 Protocol family not supported */
7295 DUMPANDLOG, /* EAFNOSUPPORT 47 Address family not supported by protocol family */
7296 DUMPANDLOG, /* EADDRINUSE 48 Address already in use */
7297 DUMPANDLOG, /* EADDRNOTAVAIL 49 Can't assign requested address */
7298 /* ipc/network software -- operational errors */
7299 RETRY, /* ENETDOWN 50 Network is down */
7300 RETRY, /* ENETUNREACH 51 Network is unreachable */
7301 RETRY, /* ENETRESET 52 Network dropped connection on reset */
7302 RETRY, /* ECONNABORTED 53 Software caused connection abort */
7303 RETRY, /* ECONNRESET 54 Connection reset by peer */
7304 RETRY, /* ENOBUFS 55 No buffer space available */
7305 RETRY, /* EISCONN 56 Socket is already connected */
7306 RETRY, /* ENOTCONN 57 Socket is not connected */
7307 RETRY, /* ESHUTDOWN 58 Can't send after socket shutdown */
7308 RETRY, /* ETOOMANYREFS 59 Too many references: can't splice */
7309 RETRY, /* ETIMEDOUT 60 Operation timed out */
7310 RETRY, /* ECONNREFUSED 61 Connection refused */
7311
7312 DUMPANDLOG, /* ELOOP 62 Too many levels of symbolic links */
7313 DUMP, /* ENAMETOOLONG 63 File name too long */
7314 RETRY, /* EHOSTDOWN 64 Host is down */
7315 RETRY, /* EHOSTUNREACH 65 No route to host */
7316 DUMP, /* ENOTEMPTY 66 Directory not empty */
7317 /* quotas & mush */
7318 DUMPANDLOG, /* PROCLIM 67 Too many processes */
7319 DUMPANDLOG, /* EUSERS 68 Too many users */
7320 DUMPANDLOG, /* EDQUOT 69 Disc quota exceeded */
7321 /* Network File System */
7322 DUMP, /* ESTALE 70 Stale NFS file handle */
7323 DUMP, /* EREMOTE 71 Too many levels of remote in path */
7324 DUMPANDLOG, /* EBADRPC 72 RPC struct is bad */
7325 DUMPANDLOG, /* ERPCMISMATCH 73 RPC version wrong */
7326 DUMPANDLOG, /* EPROGUNAVAIL 74 RPC prog. not avail */
7327 DUMPANDLOG, /* EPROGMISMATCH 75 Program version wrong */
7328 DUMPANDLOG, /* EPROCUNAVAIL 76 Bad procedure for program */
7329
7330 DUMPANDLOG, /* ENOLCK 77 No locks available */
7331 DUMPANDLOG, /* ENOSYS 78 Function not implemented */
7332 DUMPANDLOG, /* EFTYPE 79 Inappropriate file type or format */
7333 DUMPANDLOG, /* EAUTH 80 Authentication error */
7334 DUMPANDLOG, /* ENEEDAUTH 81 Need authenticator */
7335 /* Intelligent device errors */
7336 DUMPANDLOG, /* EPWROFF 82 Device power is off */
7337 DUMPANDLOG, /* EDEVERR 83 Device error, e.g. paper out */
7338 DUMPANDLOG, /* EOVERFLOW 84 Value too large to be stored in data type */
7339 /* Program loading errors */
7340 DUMPANDLOG, /* EBADEXEC 85 Bad executable */
7341 DUMPANDLOG, /* EBADARCH 86 Bad CPU type in executable */
7342 DUMPANDLOG, /* ESHLIBVERS 87 Shared library version mismatch */
7343 DUMPANDLOG, /* EBADMACHO 88 Malformed Macho file */
7344};
7345
b0d623f7 7346char
2d21ac55
A
7347nfs_pageouterrorhandler(int error)
7348{
7349 if (error > NFS_ELAST)
7350 return(DUMP);
7351 else
7352 return(errortooutcome[error]);
7353}
7354
7355
7356/*
7357 * vnode OP for pageout using UPL
7358 *
7359 * No buffer I/O, just RPCs straight from the mapped pages.
1c79356b
A
7360 * File size changes are not permitted in pageout.
7361 */
b0d623f7 7362int
2d21ac55 7363nfs_vnop_pageout(
91447636
A
7364 struct vnop_pageout_args /* {
7365 struct vnodeop_desc *a_desc;
7366 vnode_t a_vp;
7367 upl_t a_pl;
7368 vm_offset_t a_pl_offset;
7369 off_t a_f_offset;
7370 size_t a_size;
7371 int a_flags;
7372 vfs_context_t a_context;
2d21ac55 7373 } */ *ap)
1c79356b 7374{
91447636 7375 vnode_t vp = ap->a_vp;
1c79356b 7376 upl_t pl = ap->a_pl;
2d21ac55 7377 size_t size = ap->a_size;
1c79356b
A
7378 off_t f_offset = ap->a_f_offset;
7379 vm_offset_t pl_offset = ap->a_pl_offset;
2d21ac55
A
7380 int flags = ap->a_flags;
7381 nfsnode_t np = VTONFS(vp);
7382 thread_t thd;
91447636 7383 kauth_cred_t cred;
55e303ae 7384 struct nfsbuf *bp;
2d21ac55 7385 struct nfsmount *nmp = VTONMP(vp);
91447636 7386 daddr64_t lbn;
8f6c56a5 7387 int error = 0, iomode;
2d21ac55
A
7388 off_t off, txoffset, rxoffset;
7389 vm_offset_t ioaddr, txaddr, rxaddr;
b0d623f7
A
7390 uio_t auio;
7391 char uio_buf [ UIO_SIZEOF(1) ];
fa4905b1 7392 int nofreeupl = flags & UPL_NOCOMMIT;
2d21ac55
A
7393 size_t nmwsize, biosize, iosize, pgsize, txsize, rxsize, xsize, remsize;
7394 struct nfsreq *req[MAXPAGINGREQS];
b0d623f7 7395 int nextsend, nextwait, wverfset, commit;
2d21ac55 7396 uint64_t wverf, wverf2;
b0d623f7
A
7397 uint32_t stategenid = 0, vrestart = 0, restart = 0, vrestarts = 0, restarts = 0;
7398 kern_return_t kret;
1c79356b 7399
fa4905b1
A
7400 FSDBG(323, f_offset, size, pl, pl_offset);
7401
7402 if (pl == (upl_t)NULL)
7403 panic("nfs_pageout: no upl");
1c79356b 7404
fa4905b1 7405 if (size <= 0) {
2d21ac55 7406 printf("nfs_pageout: invalid size %ld", size);
fa4905b1 7407 if (!nofreeupl)
316670eb 7408 ubc_upl_abort_range(pl, pl_offset, size, 0);
1c79356b 7409 return (EINVAL);
1c79356b
A
7410 }
7411
55e303ae
A
7412 if (!nmp) {
7413 if (!nofreeupl)
7414 ubc_upl_abort(pl, UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY);
7415 return (ENXIO);
7416 }
0c530ab8 7417 biosize = nmp->nm_biosize;
2d21ac55
A
7418 nmwsize = nmp->nm_wsize;
7419
b0d623f7 7420 nfs_data_lock_noupdate(np, NFS_DATA_LOCK_SHARED);
1c79356b 7421
1c79356b 7422 /*
55e303ae
A
7423 * Check to see whether the buffer is incore.
7424 * If incore and not busy, invalidate it from the cache.
1c79356b 7425 */
55e303ae
A
7426 for (iosize = 0; iosize < size; iosize += xsize) {
7427 off = f_offset + iosize;
7428 /* need make sure we do things on block boundaries */
7429 xsize = biosize - (off % biosize);
7430 if (off + xsize > f_offset + size)
7431 xsize = f_offset + size - off;
2d21ac55 7432 lbn = (daddr64_t)(off / biosize);
91447636 7433 lck_mtx_lock(nfs_buf_mutex);
2d21ac55 7434 if ((bp = nfs_buf_incore(np, lbn))) {
91447636
A
7435 FSDBG(323, off, bp, bp->nb_lflags, bp->nb_flags);
7436 if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) {
7437 lck_mtx_unlock(nfs_buf_mutex);
b0d623f7 7438 nfs_data_unlock_noupdate(np);
fa4905b1
A
7439 /* no panic. just tell vm we are busy */
7440 if (!nofreeupl)
316670eb 7441 ubc_upl_abort_range(pl, pl_offset, size, 0);
55e303ae
A
7442 return (EBUSY);
7443 }
7444 if (bp->nb_dirtyend > 0) {
7445 /*
91447636
A
7446 * if there's a dirty range in the buffer, check
7447 * to see if it extends beyond the pageout region
55e303ae
A
7448 *
7449 * if the dirty region lies completely within the
7450 * pageout region, we just invalidate the buffer
7451 * because it's all being written out now anyway.
7452 *
7453 * if any of the dirty region lies outside the
7454 * pageout region, we'll try to clip the dirty
7455 * region to eliminate the portion that's being
7456 * paged out. If that's not possible, because
7457 * the dirty region extends before and after the
7458 * pageout region, then we'll just return EBUSY.
7459 */
7460 off_t boff, start, end;
7461 boff = NBOFF(bp);
7462 start = off;
7463 end = off + xsize;
7464 /* clip end to EOF */
91447636 7465 if (end > (off_t)np->n_size)
55e303ae
A
7466 end = np->n_size;
7467 start -= boff;
7468 end -= boff;
7469 if ((bp->nb_dirtyoff < start) &&
7470 (bp->nb_dirtyend > end)) {
b0d623f7
A
7471 /*
7472 * not gonna be able to clip the dirty region
7473 *
7474 * But before returning the bad news, move the
7475 * buffer to the start of the delwri list and
7476 * give the list a push to try to flush the
7477 * buffer out.
7478 */
2d21ac55 7479 FSDBG(323, np, bp, 0xd00deebc, EBUSY);
b0d623f7
A
7480 nfs_buf_remfree(bp);
7481 TAILQ_INSERT_HEAD(&nfsbufdelwri, bp, nb_free);
7482 nfsbufdelwricnt++;
91447636 7483 nfs_buf_drop(bp);
b0d623f7 7484 nfs_buf_delwri_push(1);
91447636 7485 lck_mtx_unlock(nfs_buf_mutex);
b0d623f7 7486 nfs_data_unlock_noupdate(np);
55e303ae 7487 if (!nofreeupl)
316670eb 7488 ubc_upl_abort_range(pl, pl_offset, size, 0);
55e303ae
A
7489 return (EBUSY);
7490 }
7491 if ((bp->nb_dirtyoff < start) ||
7492 (bp->nb_dirtyend > end)) {
7493 /* clip dirty region, if necessary */
7494 if (bp->nb_dirtyoff < start)
7495 bp->nb_dirtyend = min(bp->nb_dirtyend, start);
7496 if (bp->nb_dirtyend > end)
7497 bp->nb_dirtyoff = max(bp->nb_dirtyoff, end);
7498 FSDBG(323, bp, bp->nb_dirtyoff, bp->nb_dirtyend, 0xd00dee00);
7499 /* we're leaving this block dirty */
91447636
A
7500 nfs_buf_drop(bp);
7501 lck_mtx_unlock(nfs_buf_mutex);
55e303ae
A
7502 continue;
7503 }
7504 }
7505 nfs_buf_remfree(bp);
91447636
A
7506 lck_mtx_unlock(nfs_buf_mutex);
7507 SET(bp->nb_flags, NB_INVAL);
b0d623f7 7508 nfs_node_lock_force(np);
55e303ae
A
7509 if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
7510 CLR(bp->nb_flags, NB_NEEDCOMMIT);
7511 np->n_needcommitcnt--;
7512 CHECK_NEEDCOMMITCNT(np);
fa4905b1 7513 }
b0d623f7 7514 nfs_node_unlock(np);
483a1d10 7515 nfs_buf_release(bp, 1);
91447636
A
7516 } else {
7517 lck_mtx_unlock(nfs_buf_mutex);
1c79356b 7518 }
1c79356b
A
7519 }
7520
2d21ac55 7521 thd = vfs_context_thread(ap->a_context);
1c79356b 7522 cred = ubc_getcred(vp);
0c530ab8 7523 if (!IS_VALID_CRED(cred))
91447636 7524 cred = vfs_context_ucred(ap->a_context);
1c79356b 7525
b0d623f7 7526 nfs_node_lock_force(np);
1c79356b 7527 if (np->n_flag & NWRITEERR) {
2d21ac55 7528 error = np->n_error;
b0d623f7
A
7529 nfs_node_unlock(np);
7530 nfs_data_unlock_noupdate(np);
fa4905b1
A
7531 if (!nofreeupl)
7532 ubc_upl_abort_range(pl, pl_offset, size,
7533 UPL_ABORT_FREE_ON_EMPTY);
2d21ac55 7534 return (error);
1c79356b 7535 }
b0d623f7 7536 nfs_node_unlock(np);
1c79356b 7537
91447636 7538 if (f_offset < 0 || f_offset >= (off_t)np->n_size ||
55e303ae 7539 f_offset & PAGE_MASK_64 || size & PAGE_MASK_64) {
b0d623f7 7540 nfs_data_unlock_noupdate(np);
fa4905b1
A
7541 if (!nofreeupl)
7542 ubc_upl_abort_range(pl, pl_offset, size,
7543 UPL_ABORT_FREE_ON_EMPTY);
1c79356b
A
7544 return (EINVAL);
7545 }
7546
b0d623f7
A
7547 kret = ubc_upl_map(pl, &ioaddr);
7548 if (kret != KERN_SUCCESS)
7549 panic("nfs_vnop_pageout: ubc_upl_map() failed with (%d)", kret);
55e303ae 7550 ioaddr += pl_offset;
1c79356b 7551
91447636 7552 if ((u_quad_t)f_offset + size > np->n_size)
55e303ae 7553 xsize = np->n_size - f_offset;
1c79356b 7554 else
55e303ae 7555 xsize = size;
1c79356b 7556
55e303ae 7557 pgsize = round_page_64(xsize);
2d21ac55
A
7558 if ((size > pgsize) && !nofreeupl)
7559 ubc_upl_abort_range(pl, pl_offset + pgsize, size - pgsize,
7560 UPL_ABORT_FREE_ON_EMPTY);
1c79356b 7561
2d21ac55 7562 /*
1c79356b
A
7563 * check for partial page and clear the
7564 * contents past end of the file before
7565 * releasing it in the VM page cache
7566 */
91447636 7567 if ((u_quad_t)f_offset < np->n_size && (u_quad_t)f_offset + size > np->n_size) {
1c79356b 7568 size_t io = np->n_size - f_offset;
55e303ae 7569 bzero((caddr_t)(ioaddr + io), size - io);
fa4905b1 7570 FSDBG(321, np->n_size, f_offset, f_offset + io, size - io);
1c79356b 7571 }
b0d623f7 7572 nfs_data_unlock_noupdate(np);
1c79356b 7573
b0d623f7
A
7574 auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_WRITE,
7575 &uio_buf, sizeof(uio_buf));
55e303ae 7576
2d21ac55 7577tryagain:
b0d623f7
A
7578 if (nmp->nm_vers >= NFS_VER4)
7579 stategenid = nmp->nm_stategenid;
2d21ac55
A
7580 wverf = wverf2 = wverfset = 0;
7581 txsize = rxsize = xsize;
7582 txoffset = rxoffset = f_offset;
7583 txaddr = rxaddr = ioaddr;
7584 commit = NFS_WRITE_FILESYNC;
7585
7586 bzero(req, sizeof(req));
7587 nextsend = nextwait = 0;
1c79356b 7588 do {
6d2010ae
A
7589 if (np->n_flag & NREVOKE) {
7590 error = EIO;
7591 break;
7592 }
2d21ac55
A
7593 /* send requests while we need to and have available slots */
7594 while ((txsize > 0) && (req[nextsend] == NULL)) {
7595 iosize = MIN(nmwsize, txsize);
b0d623f7
A
7596 uio_reset(auio, txoffset, UIO_SYSSPACE, UIO_WRITE);
7597 uio_addiov(auio, CAST_USER_ADDR_T(txaddr), iosize);
7598 FSDBG(323, uio_offset(auio), iosize, txaddr, txsize);
316670eb 7599 OSAddAtomic64(1, &nfsstats.pageouts);
b0d623f7
A
7600 nfs_node_lock_force(np);
7601 np->n_numoutput++;
7602 nfs_node_unlock(np);
2d21ac55
A
7603 vnode_startwrite(vp);
7604 iomode = NFS_WRITE_UNSTABLE;
b0d623f7 7605 if ((error = nmp->nm_funcs->nf_write_rpc_async(np, auio, iosize, thd, cred, iomode, NULL, &req[nextsend]))) {
2d21ac55
A
7606 req[nextsend] = NULL;
7607 vnode_writedone(vp);
b0d623f7
A
7608 nfs_node_lock_force(np);
7609 np->n_numoutput--;
7610 nfs_node_unlock(np);
2d21ac55
A
7611 break;
7612 }
7613 txaddr += iosize;
7614 txoffset += iosize;
7615 txsize -= iosize;
7616 nextsend = (nextsend + 1) % MAXPAGINGREQS;
7617 }
7618 /* wait while we need to and break out if more requests to send */
7619 while ((rxsize > 0) && req[nextwait]) {
7620 iosize = remsize = MIN(nmwsize, rxsize);
7621 error = nmp->nm_funcs->nf_write_rpc_async_finish(np, req[nextwait], &iomode, &iosize, &wverf2);
7622 req[nextwait] = NULL;
7623 nextwait = (nextwait + 1) % MAXPAGINGREQS;
7624 vnode_writedone(vp);
b0d623f7
A
7625 nfs_node_lock_force(np);
7626 np->n_numoutput--;
7627 nfs_node_unlock(np);
7628 if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error)) {
7629 lck_mtx_lock(&nmp->nm_lock);
6d2010ae
A
7630 if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) {
7631 NP(np, "nfs_vnop_pageout: error %d, initiating recovery", error);
7632 nfs_need_recover(nmp, error);
b0d623f7
A
7633 }
7634 lck_mtx_unlock(&nmp->nm_lock);
b0d623f7
A
7635 restart = 1;
7636 goto cancel;
7637 }
2d21ac55
A
7638 if (error) {
7639 FSDBG(323, rxoffset, rxsize, error, -1);
7640 break;
7641 }
7642 if (!wverfset) {
7643 wverf = wverf2;
7644 wverfset = 1;
7645 } else if (wverf != wverf2) {
7646 /* verifier changed, so we need to restart all the writes */
b0d623f7 7647 vrestart = 1;
2d21ac55
A
7648 goto cancel;
7649 }
7650 /* Retain the lowest commitment level returned. */
7651 if (iomode < commit)
7652 commit = iomode;
7653 rxaddr += iosize;
7654 rxoffset += iosize;
7655 rxsize -= iosize;
7656 remsize -= iosize;
7657 if (remsize > 0) {
7658 /* need to try sending the remainder */
7659 iosize = remsize;
b0d623f7
A
7660 uio_reset(auio, rxoffset, UIO_SYSSPACE, UIO_WRITE);
7661 uio_addiov(auio, CAST_USER_ADDR_T(rxaddr), remsize);
2d21ac55 7662 iomode = NFS_WRITE_UNSTABLE;
b0d623f7
A
7663 error = nfs_write_rpc2(np, auio, thd, cred, &iomode, &wverf2);
7664 if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error)) {
6d2010ae 7665 NP(np, "nfs_vnop_pageout: restart: error %d", error);
b0d623f7 7666 lck_mtx_lock(&nmp->nm_lock);
6d2010ae
A
7667 if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) {
7668 NP(np, "nfs_vnop_pageout: error %d, initiating recovery", error);
7669 nfs_need_recover(nmp, error);
b0d623f7
A
7670 }
7671 lck_mtx_unlock(&nmp->nm_lock);
b0d623f7
A
7672 restart = 1;
7673 goto cancel;
7674 }
2d21ac55
A
7675 if (error) {
7676 FSDBG(323, rxoffset, rxsize, error, -1);
7677 break;
7678 }
7679 if (wverf != wverf2) {
7680 /* verifier changed, so we need to restart all the writes */
b0d623f7 7681 vrestart = 1;
2d21ac55
A
7682 goto cancel;
7683 }
7684 if (iomode < commit)
7685 commit = iomode;
7686 rxaddr += iosize;
7687 rxoffset += iosize;
7688 rxsize -= iosize;
7689 }
7690 if (txsize)
7691 break;
7692 }
7693 } while (!error && (txsize || rxsize));
7694
b0d623f7 7695 vrestart = 0;
2d21ac55
A
7696
7697 if (!error && (commit != NFS_WRITE_FILESYNC)) {
6d2010ae 7698 error = nmp->nm_funcs->nf_commit_rpc(np, f_offset, xsize, cred, wverf);
2d21ac55 7699 if (error == NFSERR_STALEWRITEVERF) {
b0d623f7 7700 vrestart = 1;
2d21ac55
A
7701 error = EIO;
7702 }
7703 }
7704
7705 if (error) {
7706cancel:
7707 /* cancel any outstanding requests */
7708 while (req[nextwait]) {
7709 nfs_request_async_cancel(req[nextwait]);
7710 req[nextwait] = NULL;
7711 nextwait = (nextwait + 1) % MAXPAGINGREQS;
7712 vnode_writedone(vp);
b0d623f7
A
7713 nfs_node_lock_force(np);
7714 np->n_numoutput--;
7715 nfs_node_unlock(np);
7716 }
6d2010ae
A
7717 if (np->n_flag & NREVOKE) {
7718 error = EIO;
7719 } else {
7720 if (vrestart) {
7721 if (++vrestarts <= 100) /* guard against no progress */
7722 goto tryagain;
7723 NP(np, "nfs_pageout: too many restarts, aborting");
7724 FSDBG(323, f_offset, xsize, ERESTART, -1);
7725 }
7726 if (restart) {
7727 if (restarts <= nfs_mount_state_max_restarts(nmp)) { /* guard against no progress */
7728 if (error == NFSERR_GRACE)
7729 tsleep(&nmp->nm_state, (PZERO-1), "nfsgrace", 2*hz);
7730 if (!(error = nfs_mount_state_wait_for_recovery(nmp)))
7731 goto tryagain;
7732 } else {
7733 NP(np, "nfs_pageout: too many restarts, aborting");
7734 FSDBG(323, f_offset, xsize, ERESTART, -1);
7735 }
7736 }
2d21ac55
A
7737 }
7738 }
7739
0b4e3aa0 7740 ubc_upl_unmap(pl);
2d21ac55 7741
0b4e3aa0
A
7742 /*
7743 * We've had several different solutions on what to do when the pageout
2d21ac55
A
7744 * gets an error. If we don't handle it, and return an error to the
7745 * caller, vm, it will retry . This can end in endless looping
0b4e3aa0
A
7746 * between vm and here doing retries of the same page. Doing a dump
7747 * back to vm, will get it out of vm's knowledge and we lose whatever
7748 * data existed. This is risky, but in some cases necessary. For
7749 * example, the initial fix here was to do that for ESTALE. In that case
2d21ac55
A
7750 * the server is telling us that the file is no longer the same. We
7751 * would not want to keep paging out to that. We also saw some 151
0b4e3aa0 7752 * errors from Auspex server and NFSv3 can return errors higher than
fa4905b1
A
7753 * ELAST. Those along with NFS known server errors we will "dump" from
7754 * vm. Errors we don't expect to occur, we dump and log for further
0b4e3aa0
A
7755 * analysis. Errors that could be transient, networking ones,
7756 * we let vm "retry". Lastly, errors that we retry, but may have potential
7757 * to storm the network, we "retrywithsleep". "sever" will be used in
7758 * in the future to dump all pages of object for cases like ESTALE.
7759 * All this is the basis for the states returned and first guesses on
7760 * error handling. Tweaking expected as more statistics are gathered.
7761 * Note, in the long run we may need another more robust solution to
7762 * have some kind of persistant store when the vm cannot dump nor keep
fa4905b1 7763 * retrying as a solution, but this would be a file architectural change
0b4e3aa0 7764 */
fa4905b1 7765 if (!nofreeupl) { /* otherwise stacked file system has to handle this */
0b4e3aa0 7766 if (error) {
2d21ac55
A
7767 int abortflags = 0;
7768 char action = nfs_pageouterrorhandler(error);
0b4e3aa0
A
7769
7770 switch (action) {
7771 case DUMP:
7772 abortflags = UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY;
7773 break;
7774 case DUMPANDLOG:
7775 abortflags = UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY;
2d21ac55
A
7776 if (error <= NFS_ELAST) {
7777 if ((errorcount[error] % 100) == 0)
6d2010ae 7778 NP(np, "nfs_pageout: unexpected error %d. dumping vm page", error);
2d21ac55
A
7779 errorcount[error]++;
7780 }
0b4e3aa0
A
7781 break;
7782 case RETRY:
7783 abortflags = UPL_ABORT_FREE_ON_EMPTY;
7784 break;
0b4e3aa0
A
7785 case SEVER: /* not implemented */
7786 default:
6d2010ae 7787 NP(np, "nfs_pageout: action %d not expected", action);
0b4e3aa0
A
7788 break;
7789 }
2d21ac55
A
7790
7791 ubc_upl_abort_range(pl, pl_offset, pgsize, abortflags);
0b4e3aa0 7792 /* return error in all cases above */
2d21ac55
A
7793
7794 } else {
0b4e3aa0 7795 ubc_upl_commit_range(pl, pl_offset, pgsize,
fa4905b1
A
7796 UPL_COMMIT_CLEAR_DIRTY |
7797 UPL_COMMIT_FREE_ON_EMPTY);
2d21ac55 7798 }
1c79356b 7799 }
1c79356b
A
7800 return (error);
7801}
7802
7803/* Blktooff derives file offset given a logical block number */
b0d623f7 7804int
2d21ac55 7805nfs_vnop_blktooff(
91447636
A
7806 struct vnop_blktooff_args /* {
7807 struct vnodeop_desc *a_desc;
7808 vnode_t a_vp;
7809 daddr64_t a_lblkno;
7810 off_t *a_offset;
2d21ac55 7811 } */ *ap)
1c79356b
A
7812{
7813 int biosize;
91447636 7814 vnode_t vp = ap->a_vp;
2d21ac55 7815 struct nfsmount *nmp = VTONMP(vp);
1c79356b 7816
fe8ab488 7817 if (nfs_mount_gone(nmp))
55e303ae 7818 return (ENXIO);
0c530ab8 7819 biosize = nmp->nm_biosize;
1c79356b 7820
91447636 7821 *ap->a_offset = (off_t)(ap->a_lblkno * biosize);
1c79356b
A
7822
7823 return (0);
7824}
7825
b0d623f7 7826int
2d21ac55 7827nfs_vnop_offtoblk(
91447636
A
7828 struct vnop_offtoblk_args /* {
7829 struct vnodeop_desc *a_desc;
7830 vnode_t a_vp;
7831 off_t a_offset;
7832 daddr64_t *a_lblkno;
2d21ac55 7833 } */ *ap)
1c79356b
A
7834{
7835 int biosize;
91447636 7836 vnode_t vp = ap->a_vp;
2d21ac55 7837 struct nfsmount *nmp = VTONMP(vp);
1c79356b 7838
fe8ab488 7839 if (nfs_mount_gone(nmp))
55e303ae 7840 return (ENXIO);
0c530ab8 7841 biosize = nmp->nm_biosize;
1c79356b 7842
91447636 7843 *ap->a_lblkno = (daddr64_t)(ap->a_offset / biosize);
1c79356b
A
7844
7845 return (0);
7846}
91447636 7847
6d2010ae
A
7848/*
7849 * vnode change monitoring
7850 */
7851int
7852nfs_vnop_monitor(
7853 struct vnop_monitor_args /* {
7854 struct vnodeop_desc *a_desc;
7855 vnode_t a_vp;
7856 uint32_t a_events;
7857 uint32_t a_flags;
7858 void *a_handle;
7859 vfs_context_t a_context;
7860 } */ *ap)
7861{
7862 nfsnode_t np = VTONFS(ap->a_vp);
7863 struct nfsmount *nmp = VTONMP(ap->a_vp);
7864 int error = 0;
7865
fe8ab488 7866 if (nfs_mount_gone(nmp))
6d2010ae
A
7867 return (ENXIO);
7868
7869 /* make sure that the vnode's monitoring status is up to date */
7870 lck_mtx_lock(&nmp->nm_lock);
7871 if (vnode_ismonitored(ap->a_vp)) {
7872 /* This vnode is currently being monitored, make sure we're tracking it. */
7873 if (np->n_monlink.le_next == NFSNOLIST) {
7874 LIST_INSERT_HEAD(&nmp->nm_monlist, np, n_monlink);
7875 nfs_mount_sock_thread_wake(nmp);
7876 }
7877 } else {
7878 /* This vnode is no longer being monitored, make sure we're not tracking it. */
7879 /* Wait for any in-progress getattr to complete first. */
7880 while (np->n_mflag & NMMONSCANINPROG) {
7881 struct timespec ts = { 1, 0 };
7882 np->n_mflag |= NMMONSCANWANT;
7883 msleep(&np->n_mflag, &nmp->nm_lock, PZERO-1, "nfswaitmonscan", &ts);
7884 }
7885 if (np->n_monlink.le_next != NFSNOLIST) {
7886 LIST_REMOVE(np, n_monlink);
7887 np->n_monlink.le_next = NFSNOLIST;
7888 }
7889 }
7890 lck_mtx_unlock(&nmp->nm_lock);
7891
7892 return (error);
7893}
7894
7895/*
7896 * Send a vnode notification for the given events.
7897 */
7898void
7899nfs_vnode_notify(nfsnode_t np, uint32_t events)
7900{
7901 struct nfsmount *nmp = NFSTONMP(np);
7902 struct nfs_vattr nvattr;
7903 struct vnode_attr vattr, *vap = NULL;
7904 struct timeval now;
7905
7906 microuptime(&now);
7907 if ((np->n_evtstamp == now.tv_sec) || !nmp) {
7908 /* delay sending this notify */
7909 np->n_events |= events;
7910 return;
7911 }
7912 events |= np->n_events;
7913 np->n_events = 0;
7914 np->n_evtstamp = now.tv_sec;
7915
7916 vfs_get_notify_attributes(&vattr);
7917 if (!nfs_getattrcache(np, &nvattr, 0)) {
7918 vap = &vattr;
7919 VATTR_INIT(vap);
5ba3f43e
A
7920
7921 vap->va_flags |= nmp->nm_vers > 2 ? VA_64BITOBJIDS : 0;
6d2010ae
A
7922 VATTR_RETURN(vap, va_fsid, vfs_statfs(nmp->nm_mountp)->f_fsid.val[0]);
7923 VATTR_RETURN(vap, va_fileid, nvattr.nva_fileid);
7924 VATTR_RETURN(vap, va_mode, nvattr.nva_mode);
7925 VATTR_RETURN(vap, va_uid, nvattr.nva_uid);
7926 VATTR_RETURN(vap, va_gid, nvattr.nva_gid);
7927 VATTR_RETURN(vap, va_nlink, nvattr.nva_nlink);
7928 }
7929 vnode_notify(NFSTOV(np), events, vap);
7930}