]> git.saurik.com Git - apple/xnu.git/blame - bsd/nfs/nfs_vnops.c
xnu-792.17.14.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_vnops.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
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
A
94
95#include <sys/time.h>
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>
106#include <nfs/nfsmount.h>
55e303ae 107#include <nfs/nfs_lock.h>
1c79356b
A
108#include <nfs/xdr_subs.h>
109#include <nfs/nfsm_subs.h>
1c79356b
A
110
111#include <net/if.h>
112#include <netinet/in.h>
113#include <netinet/in_var.h>
1c79356b
A
114#include <vm/vm_kern.h>
115
9bccf70c
A
116#include <kern/task.h>
117#include <kern/sched_prim.h>
118
1c79356b
A
119#include <sys/kdebug.h>
120
fa4905b1
A
121#define FSDBG(A, B, C, D, E) \
122 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_NONE, \
123 (int)(B), (int)(C), (int)(D), (int)(E), 0)
124#define FSDBG_TOP(A, B, C, D, E) \
125 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_START, \
126 (int)(B), (int)(C), (int)(D), (int)(E), 0)
127#define FSDBG_BOT(A, B, C, D, E) \
128 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_END, \
129 (int)(B), (int)(C), (int)(D), (int)(E), 0)
130
91447636
A
131static int nfsspec_read(struct vnop_read_args *);
132static int nfsspec_write(struct vnop_write_args *);
133static int nfsfifo_read(struct vnop_read_args *);
134static int nfsfifo_write(struct vnop_write_args *);
135static int nfsspec_close(struct vnop_close_args *);
136static int nfsfifo_close(struct vnop_close_args *);
137static int nfs_ioctl(struct vnop_ioctl_args *);
138static int nfs_select(struct vnop_select_args *);
139static int nfs_setattrrpc(vnode_t,struct vnode_attr *,kauth_cred_t,proc_t);
140static int nfs_lookup(struct vnop_lookup_args *);
141static int nfs_create(struct vnop_create_args *);
142static int nfs_mknod(struct vnop_mknod_args *);
143static int nfs_open(struct vnop_open_args *);
144static int nfs_close(struct vnop_close_args *);
145static int nfs_access(struct vnop_access_args *);
146static int nfs_vnop_getattr(struct vnop_getattr_args *);
147static int nfs_setattr(struct vnop_setattr_args *);
148static int nfs_read(struct vnop_read_args *);
149static int nfs_mmap(struct vnop_mmap_args *);
150static int nfs_fsync(struct vnop_fsync_args *);
151static int nfs_remove(struct vnop_remove_args *);
152static int nfs_link(struct vnop_link_args *);
153static int nfs_rename(struct vnop_rename_args *);
154static int nfs_mkdir(struct vnop_mkdir_args *);
155static int nfs_rmdir(struct vnop_rmdir_args *);
156static int nfs_symlink(struct vnop_symlink_args *);
157static int nfs_readdir(struct vnop_readdir_args *);
158static int nfs_lookitup(vnode_t,char *,int,kauth_cred_t,proc_t,struct nfsnode **);
159static int nfs_sillyrename(vnode_t,vnode_t,struct componentname *,kauth_cred_t,proc_t);
160static int nfs_readlink(struct vnop_readlink_args *);
161static int nfs_pathconf(struct vnop_pathconf_args *);
162static int nfs_advlock(struct vnop_advlock_args *);
163static int nfs_pagein(struct vnop_pagein_args *);
164static int nfs_pageout(struct vnop_pageout_args *);
165static int nfs_blktooff(struct vnop_blktooff_args *);
166static int nfs_offtoblk(struct vnop_offtoblk_args *);
167static int nfs_blockmap(struct vnop_blockmap_args *);
1c79356b
A
168
169/*
170 * Global vfs data structures for nfs
171 */
91447636 172vnop_t **nfsv2_vnodeop_p;
1c79356b 173static struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
91447636
A
174 { &vnop_default_desc, (vnop_t *)vn_default_error },
175 { &vnop_lookup_desc, (vnop_t *)nfs_lookup }, /* lookup */
176 { &vnop_create_desc, (vnop_t *)nfs_create }, /* create */
177 { &vnop_mknod_desc, (vnop_t *)nfs_mknod }, /* mknod */
178 { &vnop_open_desc, (vnop_t *)nfs_open }, /* open */
179 { &vnop_close_desc, (vnop_t *)nfs_close }, /* close */
180 { &vnop_access_desc, (vnop_t *)nfs_access }, /* access */
181 { &vnop_getattr_desc, (vnop_t *)nfs_vnop_getattr }, /* getattr */
182 { &vnop_setattr_desc, (vnop_t *)nfs_setattr }, /* setattr */
183 { &vnop_read_desc, (vnop_t *)nfs_read }, /* read */
184 { &vnop_write_desc, (vnop_t *)nfs_write }, /* write */
185 { &vnop_ioctl_desc, (vnop_t *)nfs_ioctl }, /* ioctl */
186 { &vnop_select_desc, (vnop_t *)nfs_select }, /* select */
187 { &vnop_revoke_desc, (vnop_t *)nfs_revoke }, /* revoke */
188 { &vnop_mmap_desc, (vnop_t *)nfs_mmap }, /* mmap */
189 { &vnop_fsync_desc, (vnop_t *)nfs_fsync }, /* fsync */
190 { &vnop_remove_desc, (vnop_t *)nfs_remove }, /* remove */
191 { &vnop_link_desc, (vnop_t *)nfs_link }, /* link */
192 { &vnop_rename_desc, (vnop_t *)nfs_rename }, /* rename */
193 { &vnop_mkdir_desc, (vnop_t *)nfs_mkdir }, /* mkdir */
194 { &vnop_rmdir_desc, (vnop_t *)nfs_rmdir }, /* rmdir */
195 { &vnop_symlink_desc, (vnop_t *)nfs_symlink }, /* symlink */
196 { &vnop_readdir_desc, (vnop_t *)nfs_readdir }, /* readdir */
197 { &vnop_readlink_desc, (vnop_t *)nfs_readlink }, /* readlink */
198 { &vnop_inactive_desc, (vnop_t *)nfs_inactive }, /* inactive */
199 { &vnop_reclaim_desc, (vnop_t *)nfs_reclaim }, /* reclaim */
200 { &vnop_strategy_desc, (vnop_t *)err_strategy }, /* strategy */
201 { &vnop_pathconf_desc, (vnop_t *)nfs_pathconf }, /* pathconf */
202 { &vnop_advlock_desc, (vnop_t *)nfs_advlock }, /* advlock */
203 { &vnop_bwrite_desc, (vnop_t *)err_bwrite }, /* bwrite */
204 { &vnop_pagein_desc, (vnop_t *)nfs_pagein }, /* Pagein */
205 { &vnop_pageout_desc, (vnop_t *)nfs_pageout }, /* Pageout */
206 { &vnop_copyfile_desc, (vnop_t *)err_copyfile }, /* Copyfile */
207 { &vnop_blktooff_desc, (vnop_t *)nfs_blktooff }, /* blktooff */
208 { &vnop_offtoblk_desc, (vnop_t *)nfs_offtoblk }, /* offtoblk */
209 { &vnop_blockmap_desc, (vnop_t *)nfs_blockmap }, /* blockmap */
1c79356b
A
210 { NULL, NULL }
211};
212struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
213 { &nfsv2_vnodeop_p, nfsv2_vnodeop_entries };
214#ifdef __FreeBSD__
215VNODEOP_SET(nfsv2_vnodeop_opv_desc);
216#endif
217
218/*
219 * Special device vnode ops
220 */
91447636 221vnop_t **spec_nfsv2nodeop_p;
1c79356b 222static struct vnodeopv_entry_desc spec_nfsv2nodeop_entries[] = {
91447636
A
223 { &vnop_default_desc, (vnop_t *)vn_default_error },
224 { &vnop_lookup_desc, (vnop_t *)spec_lookup }, /* lookup */
225 { &vnop_create_desc, (vnop_t *)spec_create }, /* create */
226 { &vnop_mknod_desc, (vnop_t *)spec_mknod }, /* mknod */
227 { &vnop_open_desc, (vnop_t *)spec_open }, /* open */
228 { &vnop_close_desc, (vnop_t *)nfsspec_close }, /* close */
229 { &vnop_getattr_desc, (vnop_t *)nfs_vnop_getattr }, /* getattr */
230 { &vnop_setattr_desc, (vnop_t *)nfs_setattr }, /* setattr */
231 { &vnop_read_desc, (vnop_t *)nfsspec_read }, /* read */
232 { &vnop_write_desc, (vnop_t *)nfsspec_write }, /* write */
233 { &vnop_ioctl_desc, (vnop_t *)spec_ioctl }, /* ioctl */
234 { &vnop_select_desc, (vnop_t *)spec_select }, /* select */
235 { &vnop_revoke_desc, (vnop_t *)spec_revoke }, /* revoke */
236 { &vnop_mmap_desc, (vnop_t *)spec_mmap }, /* mmap */
237 { &vnop_fsync_desc, (vnop_t *)nfs_fsync }, /* fsync */
238 { &vnop_remove_desc, (vnop_t *)spec_remove }, /* remove */
239 { &vnop_link_desc, (vnop_t *)spec_link }, /* link */
240 { &vnop_rename_desc, (vnop_t *)spec_rename }, /* rename */
241 { &vnop_mkdir_desc, (vnop_t *)spec_mkdir }, /* mkdir */
242 { &vnop_rmdir_desc, (vnop_t *)spec_rmdir }, /* rmdir */
243 { &vnop_symlink_desc, (vnop_t *)spec_symlink }, /* symlink */
244 { &vnop_readdir_desc, (vnop_t *)spec_readdir }, /* readdir */
245 { &vnop_readlink_desc, (vnop_t *)spec_readlink }, /* readlink */
246 { &vnop_inactive_desc, (vnop_t *)nfs_inactive }, /* inactive */
247 { &vnop_reclaim_desc, (vnop_t *)nfs_reclaim }, /* reclaim */
248 { &vnop_strategy_desc, (vnop_t *)spec_strategy }, /* strategy */
249 { &vnop_pathconf_desc, (vnop_t *)spec_pathconf }, /* pathconf */
250 { &vnop_advlock_desc, (vnop_t *)spec_advlock }, /* advlock */
251 { &vnop_bwrite_desc, (vnop_t *)vn_bwrite }, /* bwrite */
252 { &vnop_pagein_desc, (vnop_t *)nfs_pagein }, /* Pagein */
253 { &vnop_pageout_desc, (vnop_t *)nfs_pageout }, /* Pageout */
254 { &vnop_blktooff_desc, (vnop_t *)nfs_blktooff }, /* blktooff */
255 { &vnop_offtoblk_desc, (vnop_t *)nfs_offtoblk }, /* offtoblk */
256 { &vnop_blockmap_desc, (vnop_t *)nfs_blockmap }, /* blockmap */
1c79356b
A
257 { NULL, NULL }
258};
259struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
260 { &spec_nfsv2nodeop_p, spec_nfsv2nodeop_entries };
261#ifdef __FreeBSD__
262VNODEOP_SET(spec_nfsv2nodeop_opv_desc);
263#endif
264
91447636 265vnop_t **fifo_nfsv2nodeop_p;
1c79356b 266static struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries[] = {
91447636
A
267 { &vnop_default_desc, (vnop_t *)vn_default_error },
268 { &vnop_lookup_desc, (vnop_t *)fifo_lookup }, /* lookup */
269 { &vnop_create_desc, (vnop_t *)fifo_create }, /* create */
270 { &vnop_mknod_desc, (vnop_t *)fifo_mknod }, /* mknod */
271 { &vnop_open_desc, (vnop_t *)fifo_open }, /* open */
272 { &vnop_close_desc, (vnop_t *)nfsfifo_close }, /* close */
273 { &vnop_getattr_desc, (vnop_t *)nfs_vnop_getattr }, /* getattr */
274 { &vnop_setattr_desc, (vnop_t *)nfs_setattr }, /* setattr */
275 { &vnop_read_desc, (vnop_t *)nfsfifo_read }, /* read */
276 { &vnop_write_desc, (vnop_t *)nfsfifo_write }, /* write */
277 { &vnop_ioctl_desc, (vnop_t *)fifo_ioctl }, /* ioctl */
278 { &vnop_select_desc, (vnop_t *)fifo_select }, /* select */
279 { &vnop_revoke_desc, (vnop_t *)fifo_revoke }, /* revoke */
280 { &vnop_mmap_desc, (vnop_t *)fifo_mmap }, /* mmap */
281 { &vnop_fsync_desc, (vnop_t *)nfs_fsync }, /* fsync */
282 { &vnop_remove_desc, (vnop_t *)fifo_remove }, /* remove */
283 { &vnop_link_desc, (vnop_t *)fifo_link }, /* link */
284 { &vnop_rename_desc, (vnop_t *)fifo_rename }, /* rename */
285 { &vnop_mkdir_desc, (vnop_t *)fifo_mkdir }, /* mkdir */
286 { &vnop_rmdir_desc, (vnop_t *)fifo_rmdir }, /* rmdir */
287 { &vnop_symlink_desc, (vnop_t *)fifo_symlink }, /* symlink */
288 { &vnop_readdir_desc, (vnop_t *)fifo_readdir }, /* readdir */
289 { &vnop_readlink_desc, (vnop_t *)fifo_readlink }, /* readlink */
290 { &vnop_inactive_desc, (vnop_t *)nfs_inactive }, /* inactive */
291 { &vnop_reclaim_desc, (vnop_t *)nfs_reclaim }, /* reclaim */
292 { &vnop_strategy_desc, (vnop_t *)fifo_strategy }, /* strategy */
293 { &vnop_pathconf_desc, (vnop_t *)fifo_pathconf }, /* pathconf */
294 { &vnop_advlock_desc, (vnop_t *)fifo_advlock }, /* advlock */
295 { &vnop_bwrite_desc, (vnop_t *)vn_bwrite }, /* bwrite */
296 { &vnop_pagein_desc, (vnop_t *)nfs_pagein }, /* Pagein */
297 { &vnop_pageout_desc, (vnop_t *)nfs_pageout }, /* Pageout */
298 { &vnop_blktooff_desc, (vnop_t *)nfs_blktooff }, /* blktooff */
299 { &vnop_offtoblk_desc, (vnop_t *)nfs_offtoblk }, /* offtoblk */
300 { &vnop_blockmap_desc, (vnop_t *)nfs_blockmap }, /* blockmap */
1c79356b
A
301 { NULL, NULL }
302};
303struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
304 { &fifo_nfsv2nodeop_p, fifo_nfsv2nodeop_entries };
305#ifdef __FreeBSD__
306VNODEOP_SET(fifo_nfsv2nodeop_opv_desc);
307#endif
308
91447636
A
309static int nfs_mknodrpc(vnode_t dvp, vnode_t *vpp,
310 struct componentname *cnp,
311 struct vnode_attr *vap,
312 kauth_cred_t cred, proc_t p);
313static int nfs_removerpc(vnode_t dvp, char *name, int namelen,
314 kauth_cred_t cred, proc_t proc);
315static int nfs_renamerpc(vnode_t fdvp, char *fnameptr,
316 int fnamelen, vnode_t tdvp,
317 char *tnameptr, int tnamelen,
318 kauth_cred_t cred, proc_t proc);
1c79356b
A
319
320/*
321 * Global variables
322 */
91447636 323extern u_long nfs_xdrneg1;
1c79356b
A
324extern u_long nfs_true, nfs_false;
325extern struct nfsstats nfsstats;
326extern nfstype nfsv3_type[9];
91447636 327proc_t nfs_iodwant[NFS_MAXASYNCDAEMON];
1c79356b 328struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
91447636
A
329
330lck_grp_t *nfs_iod_lck_grp;
331lck_grp_attr_t *nfs_iod_lck_grp_attr;
332lck_attr_t *nfs_iod_lck_attr;
333lck_mtx_t *nfs_iod_mutex;
334
1c79356b 335int nfs_numasync = 0;
55e303ae 336int nfs_ioddelwri = 0;
91447636 337
1c79356b
A
338#define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
339
340static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
341/* SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
342 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
343*/
344#define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
345 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
346 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
347
0b4e3aa0
A
348
349/*
350 * the following are needed only by nfs_pageout to know how to handle errors
351 * see nfs_pageout comments on explanation of actions.
352 * the errors here are copied from errno.h and errors returned by servers
353 * are expected to match the same numbers here. If not, our actions maybe
354 * erroneous.
355 */
356enum actiontype {NOACTION, DUMP, DUMPANDLOG, RETRY, RETRYWITHSLEEP, SEVER};
357
358static int errorcount[ELAST+1]; /* better be zeros when initialized */
359
360static const short errortooutcome[ELAST+1] = {
361 NOACTION,
362 DUMP, /* EPERM 1 Operation not permitted */
363 DUMP, /* ENOENT 2 No such file or directory */
364 DUMPANDLOG, /* ESRCH 3 No such process */
365 RETRY, /* EINTR 4 Interrupted system call */
366 DUMP, /* EIO 5 Input/output error */
367 DUMP, /* ENXIO 6 Device not configured */
368 DUMPANDLOG, /* E2BIG 7 Argument list too long */
369 DUMPANDLOG, /* ENOEXEC 8 Exec format error */
370 DUMPANDLOG, /* EBADF 9 Bad file descriptor */
371 DUMPANDLOG, /* ECHILD 10 No child processes */
372 DUMPANDLOG, /* EDEADLK 11 Resource deadlock avoided - was EAGAIN */
373 RETRY, /* ENOMEM 12 Cannot allocate memory */
374 DUMP, /* EACCES 13 Permission denied */
375 DUMPANDLOG, /* EFAULT 14 Bad address */
376 DUMPANDLOG, /* ENOTBLK 15 POSIX - Block device required */
377 RETRY, /* EBUSY 16 Device busy */
378 DUMP, /* EEXIST 17 File exists */
379 DUMP, /* EXDEV 18 Cross-device link */
380 DUMP, /* ENODEV 19 Operation not supported by device */
381 DUMP, /* ENOTDIR 20 Not a directory */
382 DUMP, /* EISDIR 21 Is a directory */
383 DUMP, /* EINVAL 22 Invalid argument */
384 DUMPANDLOG, /* ENFILE 23 Too many open files in system */
385 DUMPANDLOG, /* EMFILE 24 Too many open files */
386 DUMPANDLOG, /* ENOTTY 25 Inappropriate ioctl for device */
387 DUMPANDLOG, /* ETXTBSY 26 Text file busy - POSIX */
388 DUMP, /* EFBIG 27 File too large */
389 DUMP, /* ENOSPC 28 No space left on device */
390 DUMPANDLOG, /* ESPIPE 29 Illegal seek */
391 DUMP, /* EROFS 30 Read-only file system */
392 DUMP, /* EMLINK 31 Too many links */
393 RETRY, /* EPIPE 32 Broken pipe */
394 /* math software */
395 DUMPANDLOG, /* EDOM 33 Numerical argument out of domain */
396 DUMPANDLOG, /* ERANGE 34 Result too large */
397 RETRY, /* EAGAIN/EWOULDBLOCK 35 Resource temporarily unavailable */
398 DUMPANDLOG, /* EINPROGRESS 36 Operation now in progress */
399 DUMPANDLOG, /* EALREADY 37 Operation already in progress */
400 /* ipc/network software -- argument errors */
401 DUMPANDLOG, /* ENOTSOC 38 Socket operation on non-socket */
402 DUMPANDLOG, /* EDESTADDRREQ 39 Destination address required */
403 DUMPANDLOG, /* EMSGSIZE 40 Message too long */
404 DUMPANDLOG, /* EPROTOTYPE 41 Protocol wrong type for socket */
405 DUMPANDLOG, /* ENOPROTOOPT 42 Protocol not available */
406 DUMPANDLOG, /* EPROTONOSUPPORT 43 Protocol not supported */
407 DUMPANDLOG, /* ESOCKTNOSUPPORT 44 Socket type not supported */
408 DUMPANDLOG, /* ENOTSUP 45 Operation not supported */
409 DUMPANDLOG, /* EPFNOSUPPORT 46 Protocol family not supported */
410 DUMPANDLOG, /* EAFNOSUPPORT 47 Address family not supported by protocol family */
411 DUMPANDLOG, /* EADDRINUSE 48 Address already in use */
412 DUMPANDLOG, /* EADDRNOTAVAIL 49 Can't assign requested address */
413 /* ipc/network software -- operational errors */
414 RETRY, /* ENETDOWN 50 Network is down */
415 RETRY, /* ENETUNREACH 51 Network is unreachable */
416 RETRY, /* ENETRESET 52 Network dropped connection on reset */
417 RETRY, /* ECONNABORTED 53 Software caused connection abort */
418 RETRY, /* ECONNRESET 54 Connection reset by peer */
419 RETRY, /* ENOBUFS 55 No buffer space available */
420 RETRY, /* EISCONN 56 Socket is already connected */
421 RETRY, /* ENOTCONN 57 Socket is not connected */
422 RETRY, /* ESHUTDOWN 58 Can't send after socket shutdown */
423 RETRY, /* ETOOMANYREFS 59 Too many references: can't splice */
424 RETRY, /* ETIMEDOUT 60 Operation timed out */
425 RETRY, /* ECONNREFUSED 61 Connection refused */
426
427 DUMPANDLOG, /* ELOOP 62 Too many levels of symbolic links */
428 DUMP, /* ENAMETOOLONG 63 File name too long */
429 RETRY, /* EHOSTDOWN 64 Host is down */
430 RETRY, /* EHOSTUNREACH 65 No route to host */
431 DUMP, /* ENOTEMPTY 66 Directory not empty */
432 /* quotas & mush */
433 DUMPANDLOG, /* PROCLIM 67 Too many processes */
434 DUMPANDLOG, /* EUSERS 68 Too many users */
435 DUMPANDLOG, /* EDQUOT 69 Disc quota exceeded */
436 /* Network File System */
437 DUMP, /* ESTALE 70 Stale NFS file handle */
438 DUMP, /* EREMOTE 71 Too many levels of remote in path */
439 DUMPANDLOG, /* EBADRPC 72 RPC struct is bad */
440 DUMPANDLOG, /* ERPCMISMATCH 73 RPC version wrong */
441 DUMPANDLOG, /* EPROGUNAVAIL 74 RPC prog. not avail */
442 DUMPANDLOG, /* EPROGMISMATCH 75 Program version wrong */
443 DUMPANDLOG, /* EPROCUNAVAIL 76 Bad procedure for program */
444
445 DUMPANDLOG, /* ENOLCK 77 No locks available */
446 DUMPANDLOG, /* ENOSYS 78 Function not implemented */
447 DUMPANDLOG, /* EFTYPE 79 Inappropriate file type or format */
448 DUMPANDLOG, /* EAUTH 80 Authentication error */
449 DUMPANDLOG, /* ENEEDAUTH 81 Need authenticator */
450 /* Intelligent device errors */
451 DUMPANDLOG, /* EPWROFF 82 Device power is off */
452 DUMPANDLOG, /* EDEVERR 83 Device error, e.g. paper out */
453 DUMPANDLOG, /* EOVERFLOW 84 Value too large to be stored in data type */
454 /* Program loading errors */
455 DUMPANDLOG, /* EBADEXEC 85 Bad executable */
456 DUMPANDLOG, /* EBADARCH 86 Bad CPU type in executable */
457 DUMPANDLOG, /* ESHLIBVERS 87 Shared library version mismatch */
458 DUMPANDLOG, /* EBADMACHO 88 Malformed Macho file */
459};
460
461
462static short
91447636 463nfs_pageouterrorhandler(int error)
0b4e3aa0
A
464{
465 if (error > ELAST)
466 return(DUMP);
467 else
468 return(errortooutcome[error]);
469}
1c79356b
A
470
471static int
91447636 472nfs3_access_otw(vnode_t vp,
0b4e3aa0 473 int wmode,
91447636
A
474 proc_t p,
475 kauth_cred_t cred)
1c79356b 476{
0b4e3aa0 477 const int v3 = 1;
55e303ae 478 u_long *tl;
0b4e3aa0
A
479 int error = 0, attrflag;
480
91447636 481 mbuf_t mreq, mrep, md, mb, mb2;
0b4e3aa0 482 caddr_t bpos, dpos, cp2;
55e303ae 483 register long t1, t2;
0b4e3aa0
A
484 register caddr_t cp;
485 u_int32_t rmode;
486 struct nfsnode *np = VTONFS(vp);
fa4905b1 487 u_int64_t xid;
55e303ae 488 struct timeval now;
0b4e3aa0 489
91447636
A
490 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED);
491 if (error)
492 return (error);
493 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_ACCESS]);
0b4e3aa0 494 nfsm_fhtom(vp, v3);
55e303ae 495 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
0b4e3aa0 496 *tl = txdr_unsigned(wmode);
fa4905b1 497 nfsm_request(vp, NFSPROC_ACCESS, p, cred, &xid);
e5568f75 498 if (mrep) {
91447636 499 nfsm_postop_attr_update(vp, 1, attrflag, &xid);
e5568f75 500 }
0b4e3aa0 501 if (!error) {
55e303ae 502 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
0b4e3aa0
A
503 rmode = fxdr_unsigned(u_int32_t, *tl);
504 np->n_mode = rmode;
91447636 505 np->n_modeuid = kauth_cred_getuid(cred);
55e303ae
A
506 microuptime(&now);
507 np->n_modestamp = now.tv_sec;
fa4905b1 508 }
0b4e3aa0
A
509 nfsm_reqdone;
510 return error;
1c79356b
A
511}
512
513/*
514 * nfs access vnode op.
515 * For nfs version 2, just return ok. File accesses may fail later.
516 * For nfs version 3, use the access rpc to check accessibility. If file modes
517 * are changed on the server, accesses might still fail later.
518 */
519static int
520nfs_access(ap)
91447636
A
521 struct vnop_access_args /* {
522 struct vnodeop_desc *a_desc;
523 vnode_t a_vp;
524 int a_mode;
525 vfs_context_t a_context;
1c79356b
A
526 } */ *ap;
527{
91447636
A
528 vnode_t vp = ap->a_vp;
529 int error = 0, dorpc;
0b4e3aa0 530 u_long mode, wmode;
1c79356b 531 int v3 = NFS_ISV3(vp);
0b4e3aa0 532 struct nfsnode *np = VTONFS(vp);
55e303ae 533 struct timeval now;
91447636 534 kauth_cred_t cred;
1c79356b
A
535
536 /*
537 * For nfs v3, do an access rpc, otherwise you are stuck emulating
538 * ufs_access() locally using the vattr. This may not be correct,
539 * since the server may apply other access criteria such as
540 * client uid-->server uid mapping that we do not know about, but
541 * this is better than just returning anything that is lying about
542 * in the cache.
543 */
544 if (v3) {
91447636
A
545 /*
546 * Convert KAUTH primitives to NFS access rights.
547 */
548 mode = 0;
549 if (vnode_isdir(vp)) {
550 /* directory */
551 if (ap->a_action &
552 (KAUTH_VNODE_LIST_DIRECTORY |
553 KAUTH_VNODE_READ_EXTATTRIBUTES))
554 mode |= NFSV3ACCESS_READ;
555 if (ap->a_action & KAUTH_VNODE_SEARCH)
1c79356b 556 mode |= NFSV3ACCESS_LOOKUP;
91447636
A
557 if (ap->a_action &
558 (KAUTH_VNODE_ADD_FILE |
559 KAUTH_VNODE_ADD_SUBDIRECTORY))
560 mode |= NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND;
561 if (ap->a_action & KAUTH_VNODE_DELETE_CHILD)
562 mode |= NFSV3ACCESS_MODIFY;
1c79356b 563 } else {
91447636
A
564 /* file */
565 if (ap->a_action &
566 (KAUTH_VNODE_READ_DATA |
567 KAUTH_VNODE_READ_EXTATTRIBUTES))
568 mode |= NFSV3ACCESS_READ;
569 if (ap->a_action & KAUTH_VNODE_WRITE_DATA)
fa4905b1 570 mode |= NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND;
91447636
A
571 if (ap->a_action & KAUTH_VNODE_APPEND_DATA)
572 mode |= NFSV3ACCESS_EXTEND;
573 if (ap->a_action & KAUTH_VNODE_EXECUTE)
1c79356b
A
574 mode |= NFSV3ACCESS_EXECUTE;
575 }
91447636
A
576 /* common */
577 if (ap->a_action & KAUTH_VNODE_DELETE)
578 mode |= NFSV3ACCESS_DELETE;
579 if (ap->a_action &
580 (KAUTH_VNODE_WRITE_ATTRIBUTES |
581 KAUTH_VNODE_WRITE_EXTATTRIBUTES |
582 KAUTH_VNODE_WRITE_SECURITY))
583 mode |= NFSV3ACCESS_MODIFY;
584 /* XXX this is pretty dubious */
585 if (ap->a_action & KAUTH_VNODE_CHANGE_OWNER)
586 mode |= NFSV3ACCESS_MODIFY;
587
588 /* if caching, always ask for every right */
0b4e3aa0
A
589 if (nfsaccess_cache_timeout > 0) {
590 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
fa4905b1
A
591 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
592 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
0b4e3aa0
A
593 } else
594 wmode = mode;
1c79356b 595
91447636
A
596 cred = vfs_context_ucred(ap->a_context);
597
0b4e3aa0
A
598 /*
599 * Does our cached result allow us to give a definite yes to
600 * this request?
601 */
91447636
A
602 dorpc = 1;
603 if (NMODEVALID(np)) {
604 microuptime(&now);
605 if ((now.tv_sec < (np->n_modestamp + nfsaccess_cache_timeout)) &&
606 (kauth_cred_getuid(cred) == np->n_modeuid) &&
607 ((np->n_mode & mode) == mode)) {
608 /* OSAddAtomic(1, (SInt32*)&nfsstats.accesscache_hits); */
609 dorpc = 0;
610 }
611 }
612 if (dorpc) {
613 /* Either a no, or a don't know. Go to the wire. */
614 /* OSAddAtomic(1, (SInt32*)&nfsstats.accesscache_misses); */
615 error = nfs3_access_otw(vp, wmode, vfs_context_proc(ap->a_context), cred);
616 }
617 if (!error) {
0b4e3aa0 618 /*
91447636
A
619 * If we asked for DELETE but didn't get it, the server
620 * may simply not support returning that bit (possible
621 * on UNIX systems). So, we'll assume that it is OK,
622 * and just let any subsequent delete action fail if it
623 * really isn't deletable.
0b4e3aa0 624 */
91447636
A
625 if ((mode & NFSV3ACCESS_DELETE) &&
626 !(np->n_mode & NFSV3ACCESS_DELETE))
627 np->n_mode |= NFSV3ACCESS_DELETE;
628 if ((np->n_mode & mode) != mode)
629 error = EACCES;
fa4905b1 630 }
91447636
A
631 } else {
632 /* v2 */
633 if ((ap->a_action & KAUTH_VNODE_WRITE_RIGHTS) && vfs_isrdonly(vnode_mount(vp))) {
634 error = EROFS;
635 } else {
636 error = 0;
1c79356b 637 }
fa4905b1 638 }
91447636 639
0b4e3aa0 640 return (error);
1c79356b
A
641}
642
643/*
644 * nfs open vnode op
645 * Check to see if the type is ok
646 * and that deletion is not in progress.
647 * For paged in text files, you will need to flush the page cache
648 * if consistency is lost.
649 */
650/* ARGSUSED */
fa4905b1 651
1c79356b
A
652static int
653nfs_open(ap)
91447636
A
654 struct vnop_open_args /* {
655 struct vnodeop_desc *a_desc;
656 vnode_t a_vp;
657 int a_mode;
658 vfs_context_t a_context;
1c79356b
A
659 } */ *ap;
660{
91447636 661 vnode_t vp = ap->a_vp;
1c79356b 662 struct nfsnode *np = VTONFS(vp);
91447636
A
663 struct nfs_vattr nvattr;
664 kauth_cred_t cred;
665 proc_t p;
666 enum vtype vtype;
1c79356b
A
667 int error;
668
91447636
A
669 vtype = vnode_vtype(vp);
670 if (vtype != VREG && vtype != VDIR && vtype != VLNK) {
1c79356b 671 return (EACCES);
fa4905b1 672 }
91447636
A
673
674 cred = vfs_context_ucred(ap->a_context);
675 p = vfs_context_proc(ap->a_context);
676
677 if (np->n_flag & NNEEDINVALIDATE) {
678 np->n_flag &= ~NNEEDINVALIDATE;
679 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, cred, p, 1);
680 }
681 if (np->n_flag & NMODIFIED) {
682 if ((error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1)) == EINTR)
1c79356b 683 return (error);
91447636
A
684 NATTRINVALIDATE(np);
685 if (vtype == VDIR)
686 np->n_direofoffset = 0;
687 error = nfs_getattr(vp, &nvattr, cred, p);
688 if (error)
689 return (error);
690 if (vtype == VDIR) {
691 /* if directory changed, purge any name cache entries */
692 if (nfstimespeccmp(&np->n_ncmtime, &nvattr.nva_mtime, !=))
693 cache_purge(vp);
694 np->n_ncmtime = nvattr.nva_mtime;
1c79356b 695 }
91447636 696 np->n_mtime = nvattr.nva_mtime;
1c79356b 697 } else {
91447636
A
698 error = nfs_getattr(vp, &nvattr, cred, p);
699 if (error)
700 return (error);
701 if (nfstimespeccmp(&np->n_mtime, &nvattr.nva_mtime, !=)) {
702 if (vtype == VDIR) {
1c79356b 703 np->n_direofoffset = 0;
91447636
A
704 nfs_invaldir(vp);
705 /* purge name cache entries */
706 if (nfstimespeccmp(&np->n_ncmtime, &nvattr.nva_mtime, !=))
483a1d10 707 cache_purge(vp);
483a1d10 708 }
91447636 709 if ((error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1)) == EINTR)
1c79356b 710 return (error);
91447636
A
711 if (vtype == VDIR)
712 np->n_ncmtime = nvattr.nva_mtime;
713 np->n_mtime = nvattr.nva_mtime;
1c79356b
A
714 }
715 }
91447636 716 NATTRINVALIDATE(np); /* For Open/Close consistency */
1c79356b
A
717 return (0);
718}
719
720/*
721 * nfs close vnode op
722 * What an NFS client should do upon close after writing is a debatable issue.
723 * Most NFS clients push delayed writes to the server upon close, basically for
724 * two reasons:
725 * 1 - So that any write errors may be reported back to the client process
726 * doing the close system call. By far the two most likely errors are
727 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
728 * 2 - To put a worst case upper bound on cache inconsistency between
729 * multiple clients for the file.
730 * There is also a consistency problem for Version 2 of the protocol w.r.t.
731 * not being able to tell if other clients are writing a file concurrently,
732 * since there is no way of knowing if the changed modify time in the reply
733 * is only due to the write for this client.
734 * (NFS Version 3 provides weak cache consistency data in the reply that
735 * should be sufficient to detect and handle this case.)
736 *
737 * The current code does the following:
738 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
739 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
483a1d10 740 * them.
1c79356b
A
741 */
742/* ARGSUSED */
743static int
744nfs_close(ap)
91447636 745 struct vnop_close_args /* {
1c79356b 746 struct vnodeop_desc *a_desc;
91447636
A
747 vnode_t a_vp;
748 int a_fflag;
749 vfs_context_t a_context;
1c79356b
A
750 } */ *ap;
751{
91447636
A
752 vnode_t vp = ap->a_vp;
753 struct nfsnode *np = VTONFS(vp);
55e303ae 754 struct nfsmount *nmp;
91447636
A
755 kauth_cred_t cred;
756 proc_t p;
1c79356b
A
757 int error = 0;
758
91447636
A
759 cred = vfs_context_ucred(ap->a_context);
760 p = vfs_context_proc(ap->a_context);
761
762 if (vnode_vtype(vp) == VREG) {
1c79356b
A
763#if DIAGNOSTIC
764 register struct sillyrename *sp = np->n_sillyrename;
765 if (sp)
766 kprintf("nfs_close: %s, dvp=%x, vp=%x, ap=%x, np=%x, sp=%x\n",
767 &sp->s_name[0], (unsigned)(sp->s_dvp), (unsigned)vp,
768 (unsigned)ap, (unsigned)np, (unsigned)sp);
769#endif
91447636 770 nmp = VFSTONFS(vnode_mount(vp));
55e303ae
A
771 if (!nmp)
772 return (ENXIO);
91447636
A
773 if (np->n_flag & NNEEDINVALIDATE) {
774 np->n_flag &= ~NNEEDINVALIDATE;
775 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, cred, p, 1);
776 }
777 if (np->n_flag & NMODIFIED) {
1c79356b 778 if (NFS_ISV3(vp)) {
91447636 779 error = nfs_flush(vp, MNT_WAIT, cred, p, 0);
fa4905b1
A
780 /*
781 * We cannot clear the NMODIFIED bit in np->n_flag due to
9bccf70c 782 * potential races with other processes
fa4905b1
A
783 * NMODIFIED is a hint
784 */
785 /* np->n_flag &= ~NMODIFIED; */
55e303ae 786 } else {
91447636 787 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
55e303ae 788 }
91447636 789 NATTRINVALIDATE(np);
1c79356b
A
790 }
791 if (np->n_flag & NWRITEERR) {
792 np->n_flag &= ~NWRITEERR;
793 error = np->n_error;
794 }
795 }
796 return (error);
797}
798
91447636
A
799
800int
801nfs_getattr_no_vnode(
802 mount_t mp,
803 u_char *fhp,
804 int fhsize,
805 kauth_cred_t cred,
806 proc_t p,
807 struct nfs_vattr *nvap,
808 u_int64_t *xidp)
809{
810 mbuf_t mreq, mrep, md, mb, mb2;
811 caddr_t bpos, dpos;
812 int t2;
813 u_long *tl;
814 caddr_t cp;
815 struct nfsmount *nmp = VFSTONFS(mp);
816 int v3 = (nmp->nm_flag & NFSMNT_NFSV3);
817 int hsiz;
818 int error = 0;
819
820 // XXX fix this to use macros once the macros get cleaned up
821 //nfsm_reqhead(NFSX_FH(v3));
822 hsiz = NFSX_FH(v3);
823 mb = NULL;
824 if (hsiz >= nfs_mbuf_minclsize)
825 error = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, &mb);
826 else
827 error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, &mb);
828 if (error)
829 return (error);
830 bpos = mbuf_data(mb);
831 mreq = mb;
832 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_GETATTR]);
833 //nfsm_fhtom(vp, v3);
834 if (v3) {
835 t2 = nfsm_rndup(fhsize) + NFSX_UNSIGNED;
836 if (t2 <= mbuf_trailingspace(mb)) {
837 nfsm_build(tl, u_long *, t2);
838 *tl++ = txdr_unsigned(fhsize);
839 *(tl + ((t2>>2) - 2)) = 0;
840 bcopy((caddr_t)fhp,(caddr_t)tl, fhsize);
841 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (caddr_t)fhp, fhsize))) {
842 error = t2;
843 mbuf_freem(mreq);
844 goto nfsmout;
845 }
846 } else {
847 nfsm_build(cp, caddr_t, NFSX_V2FH);
848 bcopy((caddr_t)fhp, cp, NFSX_V2FH);
849 }
850 //nfsm_request(vp, NFSPROC_GETATTR, p, cred, xidp);
851 if ((error = nfs_request(NULL, mp, mreq, NFSPROC_GETATTR, p, cred, &mrep, &md, &dpos, xidp))) {
852 if (error & NFSERR_RETERR)
853 error &= ~NFSERR_RETERR;
854 else
855 goto nfsmout;
856 }
857 if (!error) {
858 //nfsm_loadattr(vp, nvap, xidp);
859 error = nfs_parsefattr(&md, &dpos, v3, nvap);
860 if (error) {
861 mbuf_freem(mrep);
862 goto nfsmout;
863 }
864 }
865 nfsm_reqdone;
866 return (error);
867}
868
1c79356b
A
869/*
870 * nfs getattr call from vfs.
871 */
91447636
A
872int
873nfs_getattr(
874 vnode_t vp,
875 struct nfs_vattr *nvap,
876 kauth_cred_t cred,
877 proc_t p)
1c79356b 878{
91447636
A
879 struct nfsnode *np = VTONFS(vp);
880 caddr_t cp;
881 u_long *tl;
882 int t1, t2;
1c79356b
A
883 caddr_t bpos, dpos;
884 int error = 0;
91447636 885 mbuf_t mreq, mrep, md, mb, mb2;
55e303ae 886 int v3;
fa4905b1
A
887 u_int64_t xid;
888 int avoidfloods;
91447636
A
889
890 FSDBG_TOP(513, np->n_size, np, np->n_vattr.nva_size, np->n_flag);
891
1c79356b
A
892 /*
893 * Update local times for special files.
894 */
895 if (np->n_flag & (NACC | NUPD))
896 np->n_flag |= NCHG;
1c79356b
A
897 /*
898 * First look in the cache.
899 */
91447636
A
900 if ((error = nfs_getattrcache(vp, nvap)) == 0) {
901 FSDBG_BOT(513, np->n_size, 0, np->n_vattr.nva_size, np->n_flag);
1c79356b
A
902 return (0);
903 }
fa4905b1 904 if (error != ENOENT) {
91447636 905 FSDBG_BOT(513, np->n_size, error, np->n_vattr.nva_size,
fa4905b1 906 np->n_flag);
1c79356b 907 return (error);
fa4905b1 908 }
55e303ae 909
91447636
A
910 if (!VFSTONFS(vnode_mount(vp))) {
911 FSDBG_BOT(513, np->n_size, ENXIO, np->n_vattr.nva_size, np->n_flag);
55e303ae
A
912 return (ENXIO);
913 }
914 v3 = NFS_ISV3(vp);
1c79356b 915 error = 0;
fa4905b1 916
91447636
A
917 /*
918 * Try to get both the attributes and access info by making an
919 * ACCESS call and seeing if it returns updated attributes.
920 * But don't bother if we aren't caching access info or if the
921 * attributes returned wouldn't be cached.
922 */
923 if (v3 && (nfsaccess_cache_timeout > 0) &&
924 (nfs_attrcachetimeout(vp) > 0)) {
925 /* OSAddAtomic(1, (SInt32*)&nfsstats.accesscache_misses); */
926 if ((error = nfs3_access_otw(vp, NFSV3ACCESS_ALL, p, cred)))
fa4905b1 927 return (error);
91447636 928 if ((error = nfs_getattrcache(vp, nvap)) == 0)
1c79356b
A
929 return (0);
930 if (error != ENOENT)
931 return (error);
932 error = 0;
933 }
fa4905b1
A
934 avoidfloods = 0;
935tryagain:
91447636
A
936 nfsm_reqhead(NFSX_FH(v3));
937 if (error) {
938 FSDBG_BOT(513, np->n_size, error, np->n_vattr.nva_size, np->n_flag);
939 return (error);
940 }
941 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_GETATTR]);
1c79356b 942 nfsm_fhtom(vp, v3);
91447636 943 nfsm_request(vp, NFSPROC_GETATTR, p, cred, &xid);
1c79356b 944 if (!error) {
91447636 945 nfsm_loadattr(vp, v3, nvap, &xid);
fa4905b1 946 if (!xid) { /* out-of-order rpc - attributes were dropped */
91447636 947 mbuf_freem(mrep);
e5568f75 948 mrep = NULL;
fa4905b1
A
949 FSDBG(513, -1, np, np->n_xid << 32, np->n_xid);
950 if (avoidfloods++ < 100)
951 goto tryagain;
952 /*
953 * avoidfloods>1 is bizarre. at 100 pull the plug
954 */
955 panic("nfs_getattr: getattr flood\n");
956 }
91447636
A
957 if (nfstimespeccmp(&np->n_mtime, &nvap->nva_mtime, !=)) {
958 enum vtype vtype = vnode_vtype(vp);
fa4905b1 959 FSDBG(513, -1, np, -1, vp);
91447636 960 if (vtype == VDIR) {
1c79356b 961 nfs_invaldir(vp);
55e303ae 962 /* purge name cache entries */
91447636 963 if (nfstimespeccmp(&np->n_ncmtime, &nvap->nva_mtime, !=))
483a1d10 964 cache_purge(vp);
55e303ae 965 }
91447636 966 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
fa4905b1 967 FSDBG(513, -1, np, -2, error);
55e303ae 968 if (!error) {
91447636
A
969 if (vtype == VDIR)
970 np->n_ncmtime = nvap->nva_mtime;
971 np->n_mtime = nvap->nva_mtime;
55e303ae 972 }
1c79356b
A
973 }
974 }
975 nfsm_reqdone;
976
91447636
A
977 FSDBG_BOT(513, np->n_size, -1, np->n_vattr.nva_size, error);
978 return (error);
979}
980
981
982static int
983nfs_vnop_getattr(
984 struct vnop_getattr_args /* {
985 struct vnodeop_desc *a_desc;
986 vnode_t a_vp;
987 struct vnode_attr *a_vap;
988 vfs_context_t a_context;
989 } */ *ap)
990{
991 int error;
992 struct nfs_vattr nva;
993 struct vnode_attr *vap = ap->a_vap;
994
995 error = nfs_getattr(ap->a_vp, &nva,
996 vfs_context_ucred(ap->a_context),
997 vfs_context_proc(ap->a_context));
998 if (error)
999 return (error);
1000
1001 /* copy nva to *a_vap */
1002 VATTR_RETURN(vap, va_type, nva.nva_type);
1003 VATTR_RETURN(vap, va_mode, nva.nva_mode);
1004 VATTR_RETURN(vap, va_rdev, nva.nva_rdev);
1005 VATTR_RETURN(vap, va_uid, nva.nva_uid);
1006 VATTR_RETURN(vap, va_gid, nva.nva_gid);
1007 VATTR_RETURN(vap, va_nlink, nva.nva_nlink);
1008 VATTR_RETURN(vap, va_fileid, nva.nva_fileid);
1009 VATTR_RETURN(vap, va_data_size, nva.nva_size);
1010 VATTR_RETURN(vap, va_data_alloc, nva.nva_bytes);
1011 VATTR_RETURN(vap, va_iosize, nva.nva_blocksize); /* should this just be f_iosize? */
1012 VATTR_RETURN(vap, va_fsid, nva.nva_fsid);
1013 vap->va_access_time.tv_sec = nva.nva_atime.tv_sec;
1014 vap->va_access_time.tv_nsec = nva.nva_atime.tv_nsec;
1015 VATTR_SET_SUPPORTED(vap, va_access_time);
1016 vap->va_modify_time.tv_sec = nva.nva_mtime.tv_sec;
1017 vap->va_modify_time.tv_nsec = nva.nva_mtime.tv_nsec;
1018 VATTR_SET_SUPPORTED(vap, va_modify_time);
1019 vap->va_change_time.tv_sec = nva.nva_ctime.tv_sec;
1020 vap->va_change_time.tv_nsec = nva.nva_ctime.tv_nsec;
1021 VATTR_SET_SUPPORTED(vap, va_change_time);
1022
1c79356b
A
1023 return (error);
1024}
1025
1026/*
1027 * nfs setattr call.
1028 */
1029static int
1030nfs_setattr(ap)
91447636 1031 struct vnop_setattr_args /* {
1c79356b 1032 struct vnodeop_desc *a_desc;
91447636
A
1033 vnode_t a_vp;
1034 struct vnode_attr *a_vap;
1035 vfs_context_t a_context;
1c79356b
A
1036 } */ *ap;
1037{
91447636
A
1038 vnode_t vp = ap->a_vp;
1039 struct nfsnode *np = VTONFS(vp);
1040 struct vnode_attr *vap = ap->a_vap;
1c79356b
A
1041 int error = 0;
1042 u_quad_t tsize;
91447636
A
1043 kauth_cred_t cred;
1044 proc_t p;
1c79356b
A
1045
1046#ifndef nolint
1047 tsize = (u_quad_t)0;
1048#endif
fa4905b1 1049
91447636
A
1050 /* Setting of flags is not supported. */
1051 if (VATTR_IS_ACTIVE(vap, va_flags))
1052 return (ENOTSUP);
1053
1054 cred = vfs_context_ucred(ap->a_context);
1055 p = vfs_context_proc(ap->a_context);
1056
1057 VATTR_SET_SUPPORTED(vap, va_mode);
1058 VATTR_SET_SUPPORTED(vap, va_uid);
1059 VATTR_SET_SUPPORTED(vap, va_gid);
1060 VATTR_SET_SUPPORTED(vap, va_data_size);
1061 VATTR_SET_SUPPORTED(vap, va_access_time);
1062 VATTR_SET_SUPPORTED(vap, va_modify_time);
1063
1064 /* Disallow write attempts if the filesystem is mounted read-only. */
1065 if ((VATTR_IS_ACTIVE(vap, va_flags) || VATTR_IS_ACTIVE(vap, va_mode) ||
1066 VATTR_IS_ACTIVE(vap, va_uid) || VATTR_IS_ACTIVE(vap, va_gid) ||
1067 VATTR_IS_ACTIVE(vap, va_access_time) ||
1068 VATTR_IS_ACTIVE(vap, va_modify_time)) &&
1069 vnode_vfsisrdonly(vp))
1c79356b 1070 return (EROFS);
91447636
A
1071
1072 if (VATTR_IS_ACTIVE(vap, va_data_size)) {
1073 switch (vnode_vtype(vp)) {
1c79356b
A
1074 case VDIR:
1075 return (EISDIR);
1076 case VCHR:
1077 case VBLK:
1078 case VSOCK:
1079 case VFIFO:
91447636
A
1080 if (!VATTR_IS_ACTIVE(vap, va_modify_time) &&
1081 !VATTR_IS_ACTIVE(vap, va_access_time) &&
1082 !VATTR_IS_ACTIVE(vap, va_mode) &&
1083 !VATTR_IS_ACTIVE(vap, va_uid) &&
1084 !VATTR_IS_ACTIVE(vap, va_gid))
1c79356b 1085 return (0);
91447636 1086 VATTR_CLEAR_ACTIVE(vap, va_data_size);
1c79356b
A
1087 break;
1088 default:
1089 /*
1090 * Disallow write attempts if the filesystem is
1091 * mounted read-only.
1092 */
91447636 1093 if (vnode_vfsisrdonly(vp))
1c79356b 1094 return (EROFS);
91447636
A
1095 FSDBG_TOP(512, np->n_size, vap->va_data_size,
1096 np->n_vattr.nva_size, np->n_flag);
fa4905b1 1097 if (np->n_flag & NMODIFIED) {
91447636
A
1098 if (vap->va_data_size == 0)
1099 error = nfs_vinvalbuf(vp, 0, cred, p, 1);
fa4905b1 1100 else
91447636 1101 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
fa4905b1
A
1102 if (error) {
1103 printf("nfs_setattr: nfs_vinvalbuf %d\n", error);
91447636
A
1104 FSDBG_BOT(512, np->n_size, vap->va_data_size,
1105 np->n_vattr.nva_size, -1);
fa4905b1
A
1106 return (error);
1107 }
91447636
A
1108 } else if (np->n_size > vap->va_data_size) { /* shrinking? */
1109 daddr64_t obn, bn;
8f6c56a5 1110 int biosize, neweofoff, mustwrite;
55e303ae 1111 struct nfsbuf *bp;
fa4905b1 1112
8f6c56a5 1113 biosize = vfs_statfs(vnode_mount(vp))->f_iosize;
fa4905b1 1114 obn = (np->n_size - 1) / biosize;
91447636
A
1115 bn = vap->va_data_size / biosize;
1116 for ( ; obn >= bn; obn--) {
1117 if (!nfs_buf_is_incore(vp, obn))
1118 continue;
1119 error = nfs_buf_get(vp, obn, biosize, 0, NBLK_READ, &bp);
1120 if (error)
55e303ae 1121 continue;
91447636
A
1122 if (obn != bn) {
1123 FSDBG(512, bp, bp->nb_flags, 0, obn);
1124 SET(bp->nb_flags, NB_INVAL);
1125 nfs_buf_release(bp, 1);
1126 continue;
1127 }
1128 mustwrite = 0;
1129 neweofoff = vap->va_data_size - NBOFF(bp);
1130 /* check for any dirty data before the new EOF */
1131 if (bp->nb_dirtyend && bp->nb_dirtyoff < neweofoff) {
55e303ae
A
1132 /* clip dirty range to EOF */
1133 if (bp->nb_dirtyend > neweofoff)
91447636 1134 bp->nb_dirtyend = neweofoff;
55e303ae 1135 mustwrite++;
91447636
A
1136 }
1137 bp->nb_dirty &= (1 << round_page_32(neweofoff)/PAGE_SIZE) - 1;
1138 if (bp->nb_dirty)
55e303ae 1139 mustwrite++;
91447636
A
1140 if (!mustwrite) {
1141 FSDBG(512, bp, bp->nb_flags, 0, obn);
1142 SET(bp->nb_flags, NB_INVAL);
1143 nfs_buf_release(bp, 1);
1144 continue;
1145 }
1146 /* gotta write out dirty data before invalidating */
1147 /* (NB_STABLE indicates that data writes should be FILESYNC) */
1148 /* (NB_NOCACHE indicates buffer should be discarded) */
1149 CLR(bp->nb_flags, (NB_DONE | NB_ERROR | NB_INVAL | NB_ASYNC | NB_READ));
1150 SET(bp->nb_flags, NB_STABLE | NB_NOCACHE);
1151 if (bp->nb_wcred == NOCRED) {
1152 kauth_cred_ref(cred);
1153 bp->nb_wcred = cred;
1154 }
1155 error = nfs_buf_write(bp);
1156 // Note: bp has been released
1157 if (error) {
1158 FSDBG(512, bp, 0xd00dee, 0xbad, error);
1159 np->n_error = error;
1160 np->n_flag |= NWRITEERR;
55e303ae 1161 /*
91447636
A
1162 * There was a write error and we need to
1163 * invalidate attrs and flush buffers in
1164 * order to sync up with the server.
1165 * (if this write was extending the file,
1166 * we may no longer know the correct size)
55e303ae 1167 */
91447636
A
1168 NATTRINVALIDATE(np);
1169 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, cred, p, 1);
1170 error = 0;
fa4905b1 1171 }
91447636 1172 }
1c79356b 1173 }
fa4905b1 1174 tsize = np->n_size;
91447636
A
1175 np->n_size = np->n_vattr.nva_size = vap->va_data_size;
1176 ubc_setsize(vp, (off_t)vap->va_data_size); /* XXX error? */
1177 }
1178 } else if ((VATTR_IS_ACTIVE(vap, va_modify_time) ||
1179 VATTR_IS_ACTIVE(vap, va_access_time)) &&
1180 (np->n_flag & NMODIFIED) && (vnode_vtype(vp) == VREG)) {
1181 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
55e303ae
A
1182 if (error == EINTR)
1183 return (error);
1184 }
91447636
A
1185 if (VATTR_IS_ACTIVE(vap, va_mode)) {
1186 NMODEINVALIDATE(np);
1187 }
1188 error = nfs_setattrrpc(vp, vap, cred, p);
1189 FSDBG_BOT(512, np->n_size, vap->va_data_size, np->n_vattr.nva_size, error);
1190 if (error && VATTR_IS_ACTIVE(vap, va_data_size)) {
1c79356b 1191 /* make every effort to resync file size w/ server... */
91447636 1192 int err; /* preserve "error" for return */
1c79356b 1193
91447636 1194 np->n_size = np->n_vattr.nva_size = tsize;
fa4905b1 1195 ubc_setsize(vp, (off_t)np->n_size); /* XXX check error */
91447636
A
1196 vap->va_data_size = tsize;
1197 err = nfs_setattrrpc(vp, vap, cred, p);
1198 printf("nfs_setattr: nfs_setattrrpc %d %d\n", error, err);
1c79356b
A
1199 }
1200 return (error);
1201}
1202
1203/*
1204 * Do an nfs setattr rpc.
1205 */
1206static int
1207nfs_setattrrpc(vp, vap, cred, procp)
91447636
A
1208 vnode_t vp;
1209 struct vnode_attr *vap;
1210 kauth_cred_t cred;
1211 proc_t procp;
1c79356b
A
1212{
1213 register struct nfsv2_sattr *sp;
1214 register caddr_t cp;
1215 register long t1, t2;
1216 caddr_t bpos, dpos, cp2;
1217 u_long *tl;
483a1d10 1218 int error = 0, wccpostattr = 0;
91447636 1219 mbuf_t mreq, mrep, md, mb, mb2;
55e303ae 1220 int v3;
fa4905b1 1221 u_int64_t xid;
55e303ae
A
1222 struct timeval now;
1223
91447636 1224 if (!VFSTONFS(vnode_mount(vp)))
55e303ae
A
1225 return (ENXIO);
1226 v3 = NFS_ISV3(vp);
1c79356b 1227
91447636
A
1228 nfsm_reqhead(NFSX_FH(v3) + NFSX_SATTR(v3));
1229 if (error)
1230 return (error);
1231 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_SETATTR]);
1c79356b
A
1232 nfsm_fhtom(vp, v3);
1233 if (v3) {
91447636 1234 if (VATTR_IS_ACTIVE(vap, va_mode)) {
1c79356b
A
1235 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
1236 *tl++ = nfs_true;
1237 *tl = txdr_unsigned(vap->va_mode);
1238 } else {
1239 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1240 *tl = nfs_false;
1241 }
91447636 1242 if (VATTR_IS_ACTIVE(vap, va_uid)) {
1c79356b
A
1243 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
1244 *tl++ = nfs_true;
1245 *tl = txdr_unsigned(vap->va_uid);
1246 } else {
1247 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1248 *tl = nfs_false;
1249 }
91447636 1250 if (VATTR_IS_ACTIVE(vap, va_gid)) {
1c79356b
A
1251 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
1252 *tl++ = nfs_true;
1253 *tl = txdr_unsigned(vap->va_gid);
1254 } else {
1255 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1256 *tl = nfs_false;
1257 }
91447636 1258 if (VATTR_IS_ACTIVE(vap, va_data_size)) {
1c79356b
A
1259 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED);
1260 *tl++ = nfs_true;
91447636 1261 txdr_hyper(&vap->va_data_size, tl);
1c79356b
A
1262 } else {
1263 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1264 *tl = nfs_false;
1265 }
55e303ae 1266 microtime(&now);
91447636
A
1267 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
1268 if (vap->va_access_time.tv_sec != now.tv_sec) {
1c79356b
A
1269 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED);
1270 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
91447636 1271 txdr_nfsv3time(&vap->va_access_time, tl);
1c79356b
A
1272 } else {
1273 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1274 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1275 }
1276 } else {
1277 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1278 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
1279 }
91447636
A
1280 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
1281 if (vap->va_modify_time.tv_sec != now.tv_sec) {
1c79356b
A
1282 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED);
1283 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
91447636 1284 txdr_nfsv3time(&vap->va_modify_time, tl);
1c79356b
A
1285 } else {
1286 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1287 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1288 }
1289 } else {
1290 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1291 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
1292 }
1293 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1294 *tl = nfs_false;
1295 } else {
91447636 1296 struct timespec neg1time = { -1, -1 };
1c79356b 1297 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
91447636
A
1298 if (VATTR_IS_ACTIVE(vap, va_mode))
1299 sp->sa_mode = vtonfsv2_mode(vnode_vtype(vp), vap->va_mode);
1c79356b 1300 else
91447636
A
1301 sp->sa_mode = nfs_xdrneg1;
1302 if (VATTR_IS_ACTIVE(vap, va_uid))
1c79356b 1303 sp->sa_uid = txdr_unsigned(vap->va_uid);
1c79356b 1304 else
91447636
A
1305 sp->sa_uid = nfs_xdrneg1;
1306 if (VATTR_IS_ACTIVE(vap, va_gid))
1c79356b 1307 sp->sa_gid = txdr_unsigned(vap->va_gid);
91447636
A
1308 else
1309 sp->sa_gid = nfs_xdrneg1;
1310 if (VATTR_IS_ACTIVE(vap, va_data_size))
1311 sp->sa_size = txdr_unsigned(vap->va_data_size);
1312 else
1313 sp->sa_size = nfs_xdrneg1;
1314 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
1315 txdr_nfsv2time(&vap->va_access_time, &sp->sa_atime);
1316 } else {
1317 txdr_nfsv2time(&neg1time, &sp->sa_atime);
1318 }
1319 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
1320 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
1321 } else {
1322 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
1323 }
1c79356b 1324 }
fa4905b1 1325 nfsm_request(vp, NFSPROC_SETATTR, procp, cred, &xid);
1c79356b 1326 if (v3) {
91447636 1327 struct timespec premtime = { 0, 0 };
e5568f75 1328 if (mrep) {
91447636 1329 nfsm_wcc_data(vp, &premtime, wccpostattr, &xid);
483a1d10
A
1330 }
1331 /* if file hadn't changed, update cached mtime */
91447636
A
1332 if (nfstimespeccmp(&VTONFS(vp)->n_mtime, &premtime, ==)) {
1333 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.nva_mtime;
483a1d10
A
1334 }
1335 /* if directory hadn't changed, update namecache mtime */
91447636
A
1336 if ((vnode_vtype(vp) == VDIR) &&
1337 nfstimespeccmp(&VTONFS(vp)->n_ncmtime, &premtime, ==)) {
1338 VTONFS(vp)->n_ncmtime = VTONFS(vp)->n_vattr.nva_mtime;
e5568f75 1339 }
483a1d10 1340 if (!wccpostattr)
91447636 1341 NATTRINVALIDATE(VTONFS(vp));
e5568f75
A
1342 } else {
1343 if (mrep) {
91447636 1344 nfsm_loadattr(vp, v3, NULL, &xid);
e5568f75
A
1345 }
1346 }
1c79356b
A
1347 nfsm_reqdone;
1348 return (error);
1349}
1350
1351/*
1352 * nfs lookup call, one step at a time...
1353 * First look in cache
1354 * If not found, unlock the directory nfsnode and do the rpc
1355 */
1356static int
1357nfs_lookup(ap)
91447636 1358 struct vnop_lookup_args /* {
1c79356b 1359 struct vnodeop_desc *a_desc;
91447636
A
1360 vnode_t a_dvp;
1361 vnode_t *a_vpp;
1c79356b 1362 struct componentname *a_cnp;
91447636 1363 vfs_context_t a_context;
1c79356b
A
1364 } */ *ap;
1365{
91447636
A
1366 struct componentname *cnp = ap->a_cnp;
1367 vnode_t dvp = ap->a_dvp;
1368 vnode_t *vpp = ap->a_vpp;
1369 int flags = cnp->cn_flags;
1370 vnode_t newvp;
1371 u_long *tl;
1372 caddr_t cp;
1373 long t1, t2;
1c79356b 1374 caddr_t bpos, dpos, cp2;
91447636 1375 mbuf_t mreq, mrep, md, mb, mb2;
1c79356b 1376 long len;
91447636
A
1377 u_char *fhp;
1378 struct nfsnode *dnp, *np;
1379 int wantparent, error, attrflag, dattrflag, fhsize, fhisdvp;
1c79356b 1380 int v3 = NFS_ISV3(dvp);
91447636
A
1381 u_int64_t xid, dxid;
1382 struct nfs_vattr nvattr;
1383 kauth_cred_t cred;
1384 proc_t p;
1385 int ngflags;
1c79356b 1386
1c79356b 1387 *vpp = NULLVP;
55e303ae 1388
91447636
A
1389 cred = vfs_context_ucred(ap->a_context);
1390 p = vfs_context_proc(ap->a_context);
1391
1c79356b 1392 wantparent = flags & (LOCKPARENT|WANTPARENT);
91447636 1393 dnp = VTONFS(dvp);
fa4905b1 1394
91447636
A
1395 error = nfs_getattr(dvp, &nvattr, cred, p);
1396 if (error)
1397 goto error_return;
1398 if (nfstimespeccmp(&dnp->n_ncmtime, &nvattr.nva_mtime, !=)) {
483a1d10
A
1399 /*
1400 * This directory has changed on us.
1401 * Purge any name cache entries.
1402 */
55e303ae 1403 cache_purge(dvp);
91447636 1404 dnp->n_ncmtime = nvattr.nva_mtime;
483a1d10 1405 }
1c79356b 1406
91447636
A
1407 error = cache_lookup(dvp, vpp, cnp);
1408 switch (error) {
1409 case ENOENT:
1410 /* negative cache entry same as cache miss */
1411 error = 0;
1412 /* FALLTHROUGH */
1413 case 0:
1414 /* cache miss */
1415 break;
1416 case -1:
1417 /* cache hit, not really an error */
1418 {
1419 struct vnop_access_args naa;
1420
1421 OSAddAtomic(1, (SInt32*)&nfsstats.lookupcache_hits);
1422
1423 /* check for directory access */
1424 naa.a_vp = dvp;
1425 naa.a_action = KAUTH_VNODE_SEARCH;
1426 naa.a_context = ap->a_context;
1427
1428 /* compute actual success/failure based on accessibility */
1429 error = nfs_access(&naa);
1430 }
1431 /* FALLTHROUGH */
1432 default:
1433 /* unexpected error from cache_lookup */
1434 goto error_return;
1435 }
1436
1437 /* check for lookup of "." */
1438 if ((cnp->cn_nameptr[0] == '.') && (cnp->cn_namelen == 1)) {
1439 /* skip lookup, we know who we are */
1440 fhisdvp = 1;
1441 fhp = NULL;
1442 fhsize = 0;
1443 mrep = NULL;
1444 goto found;
1445 }
55e303ae 1446
91447636
A
1447 /* do we know this name is too long? */
1448 if (v3) {
1449 /* For NFSv3: need uniform pathconf info to test pc_namemax */
1450 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp));
1451 if (!nmp) {
1452 error = ENXIO;
483a1d10 1453 goto error_return;
1c79356b 1454 }
91447636
A
1455 if (((nmp->nm_state & (NFSSTA_GOTFSINFO|NFSSTA_GOTPATHCONF)) ==
1456 (NFSSTA_GOTFSINFO|NFSSTA_GOTPATHCONF)) &&
1457 (nmp->nm_fsinfo.fsproperties & NFSV3FSINFO_HOMOGENEOUS) &&
1458 (cnp->cn_namelen > (long)nmp->nm_fsinfo.namemax)) {
1459 error = ENAMETOOLONG;
0b4e3aa0 1460 goto error_return;
91447636
A
1461 }
1462 } else if (cnp->cn_namelen > NFS_MAXNAMLEN) {
1463 error = ENAMETOOLONG;
1464 goto error_return;
1c79356b 1465 }
fa4905b1 1466
1c79356b
A
1467 error = 0;
1468 newvp = NULLVP;
91447636
A
1469
1470 OSAddAtomic(1, (SInt32*)&nfsstats.lookupcache_misses);
1c79356b 1471 len = cnp->cn_namelen;
91447636
A
1472 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1473 if (error)
1474 goto error_return;
1475 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_LOOKUP]);
1c79356b 1476 nfsm_fhtom(dvp, v3);
91447636 1477 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN, v3);
0b4e3aa0 1478 /* nfsm_request for NFSv2 causes you to goto to nfsmout upon errors */
91447636 1479 nfsm_request(dvp, NFSPROC_LOOKUP, p, cred, &xid);
1c79356b
A
1480
1481 if (error) {
e5568f75 1482 if (mrep) {
91447636
A
1483 nfsm_postop_attr_update(dvp, v3, dattrflag, &xid);
1484 mbuf_freem(mrep);
e5568f75 1485 }
1c79356b
A
1486 goto nfsmout;
1487 }
91447636
A
1488
1489 /* get the filehandle */
1c79356b 1490 nfsm_getfh(fhp, fhsize, v3);
91447636
A
1491 /* is the file handle the same as this directory's file handle? */
1492 fhisdvp = NFS_CMPFH(dnp, fhp, fhsize);
1493
1494 /* get attributes */
1495 if (v3) {
1496 dxid = xid;
1497 nfsm_postop_attr_get(v3, attrflag, &nvattr);
1498 nfsm_postop_attr_update(dvp, v3, dattrflag, &dxid);
1499 if (!attrflag && (!fhisdvp || !dattrflag)) {
1500 /* We need valid attributes in order */
1501 /* to call nfs_nget/vnode_create(). */
1502 error = nfs_getattr_no_vnode(vnode_mount(dvp),
1503 fhp, fhsize, cred, p, &nvattr, &xid);
1504 if (error) {
1505 mbuf_freem(mrep);
1506 goto error_return;
1507 }
1508 }
1509 } else {
1510 nfsm_attr_get(v3, &nvattr);
1511 }
1512
1513found:
1c79356b
A
1514
1515 /*
1516 * Handle RENAME case...
1517 */
1518 if (cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
91447636
A
1519 if (fhisdvp) {
1520 mbuf_freem(mrep);
0b4e3aa0
A
1521 error = EISDIR;
1522 goto error_return;
1c79356b 1523 }
91447636
A
1524 error = nfs_nget(vnode_mount(dvp), dvp, cnp, fhp, fhsize,
1525 &nvattr, &xid, 0, &np);
1526 if (error) {
1527 mbuf_freem(mrep);
0b4e3aa0 1528 goto error_return;
1c79356b 1529 }
91447636
A
1530 *vpp = NFSTOV(np);
1531 mbuf_freem(mrep);
fa4905b1 1532
0b4e3aa0 1533 goto error_return;
1c79356b
A
1534 }
1535
91447636
A
1536 if ((cnp->cn_flags & MAKEENTRY) &&
1537 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)))
1538 ngflags = NG_MAKEENTRY;
1539 else
1540 ngflags = 0;
1541
1542 if (fhisdvp) {
1543 error = vnode_get(dvp);
1c79356b 1544 if (error) {
91447636 1545 mbuf_freem(mrep);
0b4e3aa0 1546 goto error_return;
1c79356b 1547 }
91447636
A
1548 newvp = dvp;
1549 /* test fhp to see if we have valid attributes in nvattr */
1550 if (fhp && (dnp->n_xid <= xid)) {
1551 error = nfs_loadattrcache(dnp, &nvattr, &xid, 0);
1552 if (error) {
1553 vnode_put(dvp);
1554 mbuf_freem(mrep);
1555 goto error_return;
1556 }
1c79356b 1557 }
1c79356b 1558 } else {
91447636
A
1559 error = nfs_nget(vnode_mount(dvp), dvp, cnp, fhp, fhsize,
1560 &nvattr, &xid, ngflags, &np);
1561 if (error) {
1562 mbuf_freem(mrep);
0b4e3aa0 1563 goto error_return;
1c79356b 1564 }
1c79356b
A
1565 newvp = NFSTOV(np);
1566 }
1c79356b 1567 *vpp = newvp;
91447636
A
1568// if (error == 0 && *vpp != NULL && *vpp != dvp)
1569// nfs_unlock(VTONFS(*vpp));
1570
1c79356b
A
1571 nfsm_reqdone;
1572 if (error) {
1c79356b
A
1573 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1574 (flags & ISLASTCN) && error == ENOENT) {
91447636 1575 if (vnode_mount(dvp) && vnode_vfsisrdonly(dvp))
1c79356b
A
1576 error = EROFS;
1577 else
1578 error = EJUSTRETURN;
1579 }
1c79356b 1580 }
0b4e3aa0 1581error_return:
91447636
A
1582 if (error && *vpp) {
1583 vnode_put(*vpp);
1584 *vpp = NULLVP;
1585 }
1c79356b
A
1586 return (error);
1587}
1588
1589/*
1590 * nfs read call.
1591 * Just call nfs_bioread() to do the work.
1592 */
1593static int
1594nfs_read(ap)
91447636
A
1595 struct vnop_read_args /* {
1596 struct vnodeop_desc *a_desc;
1597 vnode_t a_vp;
1c79356b 1598 struct uio *a_uio;
91447636
A
1599 int a_ioflag;
1600 vfs_context_t a_context;
1c79356b
A
1601 } */ *ap;
1602{
91447636 1603 if (vnode_vtype(ap->a_vp) != VREG)
1c79356b 1604 return (EPERM);
91447636
A
1605 return (nfs_bioread(ap->a_vp, ap->a_uio, ap->a_ioflag,
1606 vfs_context_ucred(ap->a_context),
1607 vfs_context_proc(ap->a_context)));
1c79356b
A
1608}
1609
fa4905b1 1610
1c79356b
A
1611/*
1612 * nfs readlink call
1613 */
1614static int
1615nfs_readlink(ap)
91447636
A
1616 struct vnop_readlink_args /* {
1617 struct vnodeop_desc *a_desc;
1618 vnode_t a_vp;
1c79356b 1619 struct uio *a_uio;
91447636 1620 vfs_context_t a_context;
1c79356b
A
1621 } */ *ap;
1622{
91447636 1623 if (vnode_vtype(ap->a_vp) != VLNK)
1c79356b 1624 return (EPERM);
91447636
A
1625 return (nfs_bioread(ap->a_vp, ap->a_uio, 0,
1626 vfs_context_ucred(ap->a_context),
1627 vfs_context_proc(ap->a_context)));
1c79356b
A
1628}
1629
1630/*
1631 * Do a readlink rpc.
1632 * Called by nfs_doio() from below the buffer cache.
1633 */
1634int
91447636
A
1635nfs_readlinkrpc(
1636 vnode_t vp,
1637 struct uio *uiop,
1638 kauth_cred_t cred,
1639 proc_t p)
1c79356b
A
1640{
1641 register u_long *tl;
1642 register caddr_t cp;
1643 register long t1, t2;
1644 caddr_t bpos, dpos, cp2;
1645 int error = 0, len, attrflag;
91447636 1646 mbuf_t mreq, mrep, md, mb, mb2;
55e303ae 1647 int v3;
fa4905b1 1648 u_int64_t xid;
1c79356b 1649
91447636 1650 if (!VFSTONFS(vnode_mount(vp)))
55e303ae
A
1651 return (ENXIO);
1652 v3 = NFS_ISV3(vp);
1653
91447636
A
1654 nfsm_reqhead(NFSX_FH(v3));
1655 if (error)
1656 return (error);
1657 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_READLINK]);
1c79356b 1658 nfsm_fhtom(vp, v3);
91447636 1659 nfsm_request(vp, NFSPROC_READLINK, p, cred, &xid);
e5568f75 1660 if (v3 && mrep)
91447636 1661 nfsm_postop_attr_update(vp, v3, attrflag, &xid);
1c79356b 1662 if (!error) {
91447636
A
1663 nfsm_strsiz(len, NFS_MAXPATHLEN, v3);
1664 if (len >= NFS_MAXPATHLEN) {
fa4905b1 1665 struct nfsnode *np = VTONFS(vp);
1c79356b
A
1666#if DIAGNOSTIC
1667 if (!np)
1668 panic("nfs_readlinkrpc: null np");
1669#endif
1670 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1671 len = np->n_size;
1672 }
1673 nfsm_mtouio(uiop, len);
1674 }
1675 nfsm_reqdone;
1676 return (error);
1677}
1678
1679/*
1680 * nfs read rpc call
1681 * Ditto above
1682 */
1683int
91447636
A
1684nfs_readrpc(
1685 vnode_t vp,
1686 struct uio *uiop,
1687 kauth_cred_t cred,
1688 proc_t p)
1c79356b
A
1689{
1690 register u_long *tl;
1691 register caddr_t cp;
1692 register long t1, t2;
1693 caddr_t bpos, dpos, cp2;
91447636 1694 mbuf_t mreq, mrep, md, mb, mb2;
1c79356b 1695 struct nfsmount *nmp;
55e303ae
A
1696 int error = 0, len, retlen, tsiz, eof = 0, attrflag;
1697 int v3, nmrsize;
fa4905b1 1698 u_int64_t xid;
1c79356b 1699
91447636
A
1700 FSDBG_TOP(536, vp, uiop->uio_offset, uio_uio_resid(uiop), 0);
1701 nmp = VFSTONFS(vnode_mount(vp));
55e303ae
A
1702 if (!nmp)
1703 return (ENXIO);
1704 v3 = NFS_ISV3(vp);
1705 nmrsize = nmp->nm_rsize;
1706
91447636
A
1707 // LP64todo - fix this
1708 tsiz = uio_uio_resid(uiop);
55e303ae 1709 if (((u_int64_t)uiop->uio_offset + (unsigned int)tsiz > 0xffffffff) && !v3) {
91447636 1710 FSDBG_BOT(536, vp, uiop->uio_offset, uio_uio_resid(uiop), EFBIG);
1c79356b 1711 return (EFBIG);
55e303ae 1712 }
1c79356b 1713 while (tsiz > 0) {
55e303ae 1714 len = (tsiz > nmrsize) ? nmrsize : tsiz;
91447636
A
1715 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1716 if (error)
1717 break;
1718 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_READ]);
1c79356b
A
1719 nfsm_fhtom(vp, v3);
1720 nfsm_build(tl, u_long *, NFSX_UNSIGNED * 3);
1721 if (v3) {
1722 txdr_hyper(&uiop->uio_offset, tl);
1723 *(tl + 2) = txdr_unsigned(len);
1724 } else {
1725 *tl++ = txdr_unsigned(uiop->uio_offset);
1726 *tl++ = txdr_unsigned(len);
1727 *tl = 0;
1728 }
55e303ae 1729 FSDBG(536, vp, uiop->uio_offset, len, 0);
91447636 1730 nfsm_request(vp, NFSPROC_READ, p, cred, &xid);
1c79356b 1731 if (v3) {
e5568f75 1732 if (mrep) {
91447636 1733 nfsm_postop_attr_update(vp, v3, attrflag, &xid);
e5568f75 1734 }
1c79356b 1735 if (error) {
91447636 1736 mbuf_freem(mrep);
1c79356b
A
1737 goto nfsmout;
1738 }
1739 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
1740 eof = fxdr_unsigned(int, *(tl + 1));
e5568f75
A
1741 } else {
1742 if (mrep) {
91447636 1743 nfsm_loadattr(vp, v3, NULL, &xid);
e5568f75
A
1744 }
1745 }
1746 if (mrep) {
91447636 1747 nfsm_strsiz(retlen, nmrsize, 0);
e5568f75 1748 nfsm_mtouio(uiop, retlen);
91447636 1749 mbuf_freem(mrep);
e5568f75
A
1750 } else {
1751 retlen = 0;
1752 }
1c79356b
A
1753 tsiz -= retlen;
1754 if (v3) {
1755 if (eof || retlen == 0)
1756 tsiz = 0;
1757 } else if (retlen < len)
1758 tsiz = 0;
1759 }
1760nfsmout:
91447636 1761 FSDBG_BOT(536, vp, eof, uio_uio_resid(uiop), error);
1c79356b
A
1762 return (error);
1763}
1764
1765/*
1766 * nfs write call
1767 */
1768int
91447636
A
1769nfs_writerpc(
1770 vnode_t vp,
1771 struct uio *uiop,
1772 kauth_cred_t cred,
1773 proc_t p,
1774 int *iomode,
8f6c56a5 1775 uint64_t *wverfp)
1c79356b
A
1776{
1777 register u_long *tl;
1778 register caddr_t cp;
1779 register int t1, t2, backup;
1780 caddr_t bpos, dpos, cp2;
91447636 1781 mbuf_t mreq, mrep, md, mb, mb2;
55e303ae 1782 struct nfsmount *nmp;
483a1d10 1783 int error = 0, len, tsiz, updatemtime = 0, wccpostattr = 0, rlen, commit;
55e303ae 1784 int v3, committed = NFSV3WRITE_FILESYNC;
8f6c56a5 1785 u_int64_t xid, wverf;
91447636 1786 mount_t mp;
1c79356b
A
1787
1788#if DIAGNOSTIC
1789 if (uiop->uio_iovcnt != 1)
1790 panic("nfs_writerpc: iovcnt > 1");
1791#endif
91447636
A
1792 FSDBG_TOP(537, vp, uiop->uio_offset, uio_uio_resid(uiop), *iomode);
1793 nmp = VFSTONFS(vnode_mount(vp));
55e303ae
A
1794 if (!nmp)
1795 return (ENXIO);
1796 v3 = NFS_ISV3(vp);
91447636
A
1797 // LP64todo - fix this
1798 tsiz = uio_uio_resid(uiop);
55e303ae 1799 if (((u_int64_t)uiop->uio_offset + (unsigned int)tsiz > 0xffffffff) && !v3) {
91447636 1800 FSDBG_BOT(537, vp, uiop->uio_offset, uio_uio_resid(uiop), EFBIG);
1c79356b 1801 return (EFBIG);
55e303ae 1802 }
1c79356b 1803 while (tsiz > 0) {
91447636 1804 nmp = VFSTONFS(vnode_mount(vp));
55e303ae
A
1805 if (!nmp) {
1806 error = ENXIO;
1807 break;
1808 }
1c79356b 1809 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
91447636
A
1810 nfsm_reqhead(NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1811 if (error)
1812 break;
1813 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_WRITE]);
1c79356b
A
1814 nfsm_fhtom(vp, v3);
1815 if (v3) {
1816 nfsm_build(tl, u_long *, 5 * NFSX_UNSIGNED);
1817 txdr_hyper(&uiop->uio_offset, tl);
1818 tl += 2;
1819 *tl++ = txdr_unsigned(len);
1820 *tl++ = txdr_unsigned(*iomode);
1821 } else {
1822 nfsm_build(tl, u_long *, 4 * NFSX_UNSIGNED);
1823 *++tl = txdr_unsigned(uiop->uio_offset);
1824 tl += 2;
1825 }
1826 *tl = txdr_unsigned(len);
55e303ae 1827 FSDBG(537, vp, uiop->uio_offset, len, 0);
1c79356b 1828 nfsm_uiotom(uiop, len);
91447636
A
1829 nfsm_request(vp, NFSPROC_WRITE, p, cred, &xid);
1830 nmp = VFSTONFS(vnode_mount(vp));
55e303ae
A
1831 if (!nmp)
1832 error = ENXIO;
1c79356b 1833 if (v3) {
e5568f75 1834 if (mrep) {
91447636
A
1835 struct timespec premtime;
1836 nfsm_wcc_data(vp, &premtime, wccpostattr, &xid);
1837 if (nfstimespeccmp(&VTONFS(vp)->n_mtime, &premtime, ==))
483a1d10 1838 updatemtime = 1;
e5568f75 1839 }
1c79356b
A
1840 if (!error) {
1841 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED +
1842 NFSX_V3WRITEVERF);
1843 rlen = fxdr_unsigned(int, *tl++);
1844 if (rlen <= 0) {
1845 error = NFSERR_IO;
1846 break;
1847 } else if (rlen < len) {
1848 backup = len - rlen;
91447636
A
1849 uio_iov_base_add(uiop, -backup);
1850 uio_iov_len_add(uiop, backup);
1c79356b 1851 uiop->uio_offset -= backup;
91447636 1852 uio_uio_resid_add(uiop, backup);
1c79356b
A
1853 len = rlen;
1854 }
1855 commit = fxdr_unsigned(int, *tl++);
1856
1857 /*
1858 * Return the lowest committment level
1859 * obtained by any of the RPCs.
1860 */
1861 if (committed == NFSV3WRITE_FILESYNC)
1862 committed = commit;
1863 else if (committed == NFSV3WRITE_DATASYNC &&
1864 commit == NFSV3WRITE_UNSTABLE)
1865 committed = commit;
8f6c56a5
A
1866 fxdr_hyper(tl, &wverf);
1867 if (wverfp)
1868 *wverfp = wverf;
55e303ae 1869 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
8f6c56a5 1870 nmp->nm_verf = wverf;
55e303ae 1871 nmp->nm_state |= NFSSTA_HASWRITEVERF;
8f6c56a5
A
1872 } else if (wverf != nmp->nm_verf) {
1873 nmp->nm_verf = wverf;
1c79356b
A
1874 }
1875 }
e5568f75
A
1876 } else {
1877 if (mrep) {
91447636 1878 nfsm_loadattr(vp, v3, NULL, &xid);
e5568f75
A
1879 }
1880 }
fa4905b1 1881
483a1d10 1882 if (updatemtime)
91447636
A
1883 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.nva_mtime;
1884 mbuf_freem(mrep);
fa4905b1
A
1885 /*
1886 * we seem to have a case where we end up looping on shutdown
1887 * and taking down nfs servers. For V3, error cases, there is
1888 * no way to terminate loop, if the len was 0, meaning,
1889 * nmp->nm_wsize was trashed. FreeBSD has this fix in it.
1890 * Let's try it.
1891 */
1892 if (error)
1893 break;
1894 tsiz -= len;
1c79356b
A
1895 }
1896nfsmout:
91447636 1897 if ((mp = vnode_mount(vp)) && (vfs_flags(mp) & MNT_ASYNC))
1c79356b
A
1898 committed = NFSV3WRITE_FILESYNC;
1899 *iomode = committed;
1900 if (error)
91447636
A
1901 uio_uio_resid_set(uiop, tsiz);
1902 FSDBG_BOT(537, vp, committed, uio_uio_resid(uiop), error);
1c79356b
A
1903 return (error);
1904}
1905
1906/*
1907 * nfs mknod rpc
1908 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1909 * mode set to specify the file type and the size field for rdev.
1910 */
1911static int
91447636
A
1912nfs_mknodrpc(
1913 vnode_t dvp,
1914 vnode_t *vpp,
1915 struct componentname *cnp,
1916 struct vnode_attr *vap,
1917 kauth_cred_t cred,
1918 proc_t p)
1c79356b
A
1919{
1920 register struct nfsv2_sattr *sp;
1c79356b
A
1921 register u_long *tl;
1922 register caddr_t cp;
1923 register long t1, t2;
91447636 1924 vnode_t newvp = (vnode_t)0;
1c79356b 1925 struct nfsnode *np = (struct nfsnode *)0;
91447636 1926 struct nfs_vattr nvattr;
1c79356b
A
1927 char *cp2;
1928 caddr_t bpos, dpos;
483a1d10 1929 int error = 0, wccpostattr = 0, gotvp = 0;
91447636
A
1930 struct timespec premtime = { 0, 0 };
1931 mbuf_t mreq, mrep, md, mb, mb2;
1c79356b 1932 u_long rdev;
fa4905b1 1933 u_int64_t xid;
1c79356b 1934 int v3 = NFS_ISV3(dvp);
91447636 1935 int gotuid, gotgid;
1c79356b 1936
91447636
A
1937 if (!VATTR_IS_ACTIVE(vap, va_type))
1938 return (EINVAL);
1939 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1940 if (!VATTR_IS_ACTIVE(vap, va_rdev))
1941 return (EINVAL);
1c79356b 1942 rdev = txdr_unsigned(vap->va_rdev);
91447636 1943 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1c79356b
A
1944 rdev = 0xffffffff;
1945 else {
91447636 1946 return (ENOTSUP);
1c79356b 1947 }
91447636
A
1948 nfsm_reqhead(NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1949 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1950 if (error)
1c79356b 1951 return (error);
91447636
A
1952
1953 VATTR_SET_SUPPORTED(vap, va_mode);
1954 VATTR_SET_SUPPORTED(vap, va_uid);
1955 VATTR_SET_SUPPORTED(vap, va_gid);
1956 VATTR_SET_SUPPORTED(vap, va_data_size);
1957 VATTR_SET_SUPPORTED(vap, va_access_time);
1958 VATTR_SET_SUPPORTED(vap, va_modify_time);
1959 gotuid = VATTR_IS_ACTIVE(vap, va_uid);
1960 gotgid = VATTR_IS_ACTIVE(vap, va_gid);
1961
1962 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_MKNOD]);
1c79356b 1963 nfsm_fhtom(dvp, v3);
91447636 1964 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN, v3);
1c79356b 1965 if (v3) {
91447636 1966 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1c79356b 1967 *tl++ = vtonfsv3_type(vap->va_type);
91447636 1968 nfsm_v3sattr(vap);
1c79356b
A
1969 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1970 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
1971 *tl++ = txdr_unsigned(major(vap->va_rdev));
1972 *tl = txdr_unsigned(minor(vap->va_rdev));
1973 }
1974 } else {
91447636 1975 struct timespec neg1time = { -1, -1 };
1c79356b 1976 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
91447636
A
1977 sp->sa_mode = vtonfsv2_mode(vap->va_type,
1978 (VATTR_IS_ACTIVE(vap, va_mode) ? vap->va_mode : 0600));
1979 sp->sa_uid = gotuid ? (u_long)txdr_unsigned(vap->va_uid) : nfs_xdrneg1;
1980 sp->sa_gid = gotgid ? (u_long)txdr_unsigned(vap->va_gid) : nfs_xdrneg1;
1c79356b 1981 sp->sa_size = rdev;
91447636
A
1982 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
1983 txdr_nfsv2time(&vap->va_access_time, &sp->sa_atime);
1984 } else {
1985 txdr_nfsv2time(&neg1time, &sp->sa_atime);
1986 }
1987 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
1988 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
1989 } else {
1990 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
1991 }
1992 }
1993 nfsm_request(dvp, NFSPROC_MKNOD, p, cred, &xid);
1994 /* XXX no EEXIST kludge here? */
1995 if (!error) {
1996 nfsm_mtofh(dvp, cnp, newvp, v3, &xid, gotvp);
1997 if (!gotvp) {
1c79356b 1998 error = nfs_lookitup(dvp, cnp->cn_nameptr,
91447636 1999 cnp->cn_namelen, cred, p, &np);
1c79356b
A
2000 if (!error)
2001 newvp = NFSTOV(np);
2002 }
2003 }
e5568f75 2004 if (v3 && mrep)
91447636
A
2005 nfsm_wcc_data(dvp, &premtime, wccpostattr, &xid);
2006 if (!error && (gotuid || gotgid) &&
2007 (!newvp || nfs_getattrcache(newvp, &nvattr) ||
2008 (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
2009 (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
2010 /* clear ID bits if server didn't use them (or we can't tell) */
2011 VATTR_CLEAR_SUPPORTED(vap, va_uid);
2012 VATTR_CLEAR_SUPPORTED(vap, va_gid);
2013 }
1c79356b
A
2014 nfsm_reqdone;
2015 if (error) {
2016 if (newvp)
91447636 2017 vnode_put(newvp);
1c79356b 2018 } else {
1c79356b
A
2019 *vpp = newvp;
2020 }
55e303ae 2021 VTONFS(dvp)->n_flag |= NMODIFIED;
483a1d10 2022 /* if directory hadn't changed, update namecache mtime */
91447636
A
2023 if (nfstimespeccmp(&VTONFS(dvp)->n_ncmtime, &premtime, ==))
2024 VTONFS(dvp)->n_ncmtime = VTONFS(dvp)->n_vattr.nva_mtime;
483a1d10 2025 if (!wccpostattr)
91447636 2026 NATTRINVALIDATE(VTONFS(dvp));
1c79356b
A
2027 return (error);
2028}
2029
2030/*
2031 * nfs mknod vop
2032 * just call nfs_mknodrpc() to do the work.
2033 */
2034/* ARGSUSED */
2035static int
2036nfs_mknod(ap)
91447636
A
2037 struct vnop_mknod_args /* {
2038 struct vnodeop_desc *a_desc;
2039 vnode_t a_dvp;
2040 vnode_t *a_vpp;
1c79356b 2041 struct componentname *a_cnp;
91447636
A
2042 struct vnode_attr *a_vap;
2043 vfs_context_t a_context;
1c79356b
A
2044 } */ *ap;
2045{
1c79356b
A
2046 int error;
2047
91447636
A
2048 error = nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap,
2049 vfs_context_ucred(ap->a_context),
2050 vfs_context_proc(ap->a_context));
2051
1c79356b
A
2052 return (error);
2053}
2054
2055static u_long create_verf;
2056/*
2057 * nfs file create call
2058 */
2059static int
2060nfs_create(ap)
91447636
A
2061 struct vnop_create_args /* {
2062 struct vnodeop_desc *a_desc;
2063 vnode_t a_dvp;
2064 vnode_t *a_vpp;
1c79356b 2065 struct componentname *a_cnp;
91447636
A
2066 struct vnode_attr *a_vap;
2067 vfs_context_t a_context;
1c79356b
A
2068 } */ *ap;
2069{
91447636
A
2070 vnode_t dvp = ap->a_dvp;
2071 struct vnode_attr *vap = ap->a_vap;
2072 struct componentname *cnp = ap->a_cnp;
2073 struct nfs_vattr nvattr;
2074 struct nfsv2_sattr *sp;
2075 u_long *tl;
2076 caddr_t cp;
2077 long t1, t2;
1c79356b 2078 struct nfsnode *np = (struct nfsnode *)0;
91447636 2079 vnode_t newvp = (vnode_t)0;
1c79356b 2080 caddr_t bpos, dpos, cp2;
483a1d10 2081 int error = 0, wccpostattr = 0, gotvp = 0, fmode = 0;
91447636
A
2082 struct timespec premtime = { 0, 0 };
2083 mbuf_t mreq, mrep, md, mb, mb2;
1c79356b 2084 int v3 = NFS_ISV3(dvp);
91447636 2085 int gotuid, gotgid;
fa4905b1 2086 u_int64_t xid;
91447636
A
2087 kauth_cred_t cred;
2088 proc_t p;
2089
2090 cred = vfs_context_ucred(ap->a_context);
2091 p = vfs_context_proc(ap->a_context);
2092
2093 if (!VATTR_IS_ACTIVE(vap, va_type))
2094 return (EINVAL);
1c79356b
A
2095
2096 /*
2097 * Oops, not for me..
2098 */
2099 if (vap->va_type == VSOCK)
91447636
A
2100 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap, cred, p));
2101
2102 VATTR_SET_SUPPORTED(vap, va_mode);
2103 VATTR_SET_SUPPORTED(vap, va_uid);
2104 VATTR_SET_SUPPORTED(vap, va_gid);
2105 VATTR_SET_SUPPORTED(vap, va_data_size);
2106 VATTR_SET_SUPPORTED(vap, va_access_time);
2107 VATTR_SET_SUPPORTED(vap, va_modify_time);
2108 gotuid = VATTR_IS_ACTIVE(vap, va_uid);
2109 gotgid = VATTR_IS_ACTIVE(vap, va_gid);
1c79356b 2110
1c79356b
A
2111 if (vap->va_vaflags & VA_EXCLUSIVE)
2112 fmode |= O_EXCL;
2113again:
91447636 2114 nfsm_reqhead(NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1c79356b 2115 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
91447636
A
2116 if (error)
2117 return (error);
2118 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_CREATE]);
1c79356b 2119 nfsm_fhtom(dvp, v3);
91447636 2120 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN, v3);
1c79356b
A
2121 if (v3) {
2122 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
2123 if (fmode & O_EXCL) {
2124 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
2125 nfsm_build(tl, u_long *, NFSX_V3CREATEVERF);
2126 if (!TAILQ_EMPTY(&in_ifaddrhead))
2127 *tl++ = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr.s_addr;
2128 else
2129 *tl++ = create_verf;
2130 *tl = ++create_verf;
2131 } else {
2132 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
91447636 2133 nfsm_v3sattr(vap);
1c79356b
A
2134 }
2135 } else {
91447636 2136 struct timespec neg1time = { -1, -1 };
1c79356b 2137 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
91447636
A
2138 sp->sa_mode = vtonfsv2_mode(vap->va_type,
2139 (VATTR_IS_ACTIVE(vap, va_mode) ? vap->va_mode : 0600));
2140 sp->sa_uid = gotuid ? (u_long)txdr_unsigned(vap->va_uid) : nfs_xdrneg1;
2141 sp->sa_gid = gotgid ? (u_long)txdr_unsigned(vap->va_gid) : nfs_xdrneg1;
1c79356b 2142 sp->sa_size = 0;
91447636
A
2143 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
2144 txdr_nfsv2time(&vap->va_access_time, &sp->sa_atime);
2145 } else {
2146 txdr_nfsv2time(&neg1time, &sp->sa_atime);
2147 }
2148 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
2149 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
2150 } else {
2151 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
2152 }
1c79356b 2153 }
91447636 2154 nfsm_request(dvp, NFSPROC_CREATE, p, cred, &xid);
1c79356b 2155 if (!error) {
91447636 2156 nfsm_mtofh(dvp, cnp, newvp, v3, &xid, gotvp);
1c79356b 2157 if (!gotvp) {
1c79356b 2158 error = nfs_lookitup(dvp, cnp->cn_nameptr,
91447636 2159 cnp->cn_namelen, cred, p, &np);
1c79356b
A
2160 if (!error)
2161 newvp = NFSTOV(np);
2162 }
2163 }
e5568f75 2164 if (v3 && mrep)
91447636 2165 nfsm_wcc_data(dvp, &premtime, wccpostattr, &xid);
1c79356b
A
2166 nfsm_reqdone;
2167 if (error) {
2168 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
2169 fmode &= ~O_EXCL;
2170 goto again;
2171 }
2172 if (newvp)
91447636 2173 vnode_put(newvp);
ccc36f2f 2174 } else if (v3 && (fmode & O_EXCL)) {
91447636
A
2175 error = nfs_setattrrpc(newvp, vap, cred, p);
2176 if (error && (gotuid || gotgid)) {
2177 /* it's possible the server didn't like our attempt to set IDs. */
2178 /* so, let's try it again without those */
2179 VATTR_CLEAR_ACTIVE(vap, va_uid);
2180 VATTR_CLEAR_ACTIVE(vap, va_gid);
2181 error = nfs_setattrrpc(newvp, vap, cred, p);
2182 }
ccc36f2f 2183 if (error)
91447636 2184 vnode_put(newvp);
ccc36f2f 2185 }
1c79356b 2186 if (!error) {
1c79356b
A
2187 *ap->a_vpp = newvp;
2188 }
55e303ae 2189 VTONFS(dvp)->n_flag |= NMODIFIED;
483a1d10 2190 /* if directory hadn't changed, update namecache mtime */
91447636
A
2191 if (nfstimespeccmp(&VTONFS(dvp)->n_ncmtime, &premtime, ==))
2192 VTONFS(dvp)->n_ncmtime = VTONFS(dvp)->n_vattr.nva_mtime;
483a1d10 2193 if (!wccpostattr)
91447636
A
2194 NATTRINVALIDATE(VTONFS(dvp));
2195 if (!error && (gotuid || gotgid) &&
2196 (!newvp || nfs_getattrcache(newvp, &nvattr) ||
2197 (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
2198 (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
2199 /* clear ID bits if server didn't use them (or we can't tell) */
2200 VATTR_CLEAR_SUPPORTED(vap, va_uid);
2201 VATTR_CLEAR_SUPPORTED(vap, va_gid);
2202 }
1c79356b
A
2203 return (error);
2204}
2205
2206/*
2207 * nfs file remove call
2208 * To try and make nfs semantics closer to ufs semantics, a file that has
2209 * other processes using the vnode is renamed instead of removed and then
2210 * removed later on the last close.
91447636 2211 * - If vnode_isinuse()
1c79356b
A
2212 * If a rename is not already in the works
2213 * call nfs_sillyrename() to set it up
2214 * else
2215 * do the remove rpc
2216 */
2217static int
2218nfs_remove(ap)
91447636 2219 struct vnop_remove_args /* {
1c79356b 2220 struct vnodeop_desc *a_desc;
91447636
A
2221 vnode_t a_dvp;
2222 vnode_t a_vp;
2223 struct componentname *a_cnp;
2224 int a_flags;
2225 vfs_context_t a_context;
1c79356b
A
2226 } */ *ap;
2227{
91447636
A
2228 vnode_t vp = ap->a_vp;
2229 vnode_t dvp = ap->a_dvp;
2230 struct componentname *cnp = ap->a_cnp;
2231 struct nfsnode *np = VTONFS(vp);
9bccf70c 2232 int error = 0, gofree = 0;
91447636
A
2233 struct nfs_vattr nvattr;
2234 kauth_cred_t cred;
2235 proc_t p;
1c79356b 2236
91447636
A
2237 cred = vfs_context_ucred(ap->a_context);
2238 p = vfs_context_proc(ap->a_context);
9bccf70c 2239
91447636
A
2240 gofree = vnode_isinuse(vp, 0) ? 0 : 1;
2241 if ((ap->a_flags & VNODE_REMOVE_NODELETEBUSY) && !gofree) {
55e303ae 2242 /* Caller requested Carbon delete semantics, but file is busy */
55e303ae
A
2243 return (EBUSY);
2244 }
9bccf70c 2245 if (gofree || (np->n_sillyrename &&
91447636
A
2246 nfs_getattr(vp, &nvattr, cred, p) == 0 &&
2247 nvattr.nva_nlink > 1)) {
1c79356b
A
2248 /*
2249 * Purge the name cache so that the chance of a lookup for
2250 * the name succeeding while the remove is in progress is
483a1d10 2251 * minimized.
1c79356b
A
2252 */
2253 cache_purge(vp);
2254 /*
2255 * throw away biocache buffers, mainly to avoid
2256 * unnecessary delayed writes later.
2257 */
91447636 2258 error = nfs_vinvalbuf(vp, 0, cred, p, 1);
fa4905b1
A
2259 np->n_size = 0;
2260 ubc_setsize(vp, (off_t)0); /* XXX check error */
1c79356b
A
2261 /* Do the rpc */
2262 if (error != EINTR)
2263 error = nfs_removerpc(dvp, cnp->cn_nameptr,
91447636 2264 cnp->cn_namelen, cred, p);
1c79356b
A
2265 /*
2266 * Kludge City: If the first reply to the remove rpc is lost..
2267 * the reply to the retransmitted request will be ENOENT
2268 * since the file was in fact removed
2269 * Therefore, we cheat and return success.
2270 */
2271 if (error == ENOENT)
2272 error = 0;
55e303ae
A
2273 if (!error) {
2274 /*
2275 * remove nfsnode from hash now so we can't accidentally find it
2276 * again if another object gets created with the same filehandle
2277 * before this vnode gets reclaimed
2278 */
91447636 2279 lck_mtx_lock(nfs_node_hash_mutex);
55e303ae
A
2280 LIST_REMOVE(np, n_hash);
2281 np->n_flag &= ~NHASHED;
91447636
A
2282 lck_mtx_unlock(nfs_node_hash_mutex);
2283 }
2284 if (!error && !np->n_sillyrename) {
2285 /* clear flags now: won't get nfs_inactive for recycled vnode */
2286 /* clear all flags other than these */
2287 np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NHASHED);
2288 vnode_recycle(vp);
55e303ae 2289 }
1c79356b 2290 } else if (!np->n_sillyrename) {
91447636 2291 error = nfs_sillyrename(dvp, vp, cnp, cred, p);
1c79356b 2292 }
91447636 2293 NATTRINVALIDATE(np);
1c79356b
A
2294
2295 return (error);
2296}
2297
2298/*
2299 * nfs file remove rpc called from nfs_inactive
2300 */
2301int
91447636 2302nfs_removeit(struct sillyrename *sp)
1c79356b 2303{
91447636 2304 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred, NULL));
1c79356b
A
2305}
2306
2307/*
2308 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
2309 */
2310static int
2311nfs_removerpc(dvp, name, namelen, cred, proc)
91447636 2312 vnode_t dvp;
1c79356b
A
2313 char *name;
2314 int namelen;
91447636
A
2315 kauth_cred_t cred;
2316 proc_t proc;
1c79356b
A
2317{
2318 register u_long *tl;
2319 register caddr_t cp;
2320 register long t1, t2;
2321 caddr_t bpos, dpos, cp2;
483a1d10 2322 int error = 0, wccpostattr = 0;
91447636
A
2323 struct timespec premtime = { 0, 0 };
2324 mbuf_t mreq, mrep, md, mb, mb2;
55e303ae 2325 int v3;
fa4905b1 2326 u_int64_t xid;
1c79356b 2327
91447636 2328 if (!VFSTONFS(vnode_mount(dvp)))
55e303ae
A
2329 return (ENXIO);
2330 v3 = NFS_ISV3(dvp);
2331
91447636
A
2332 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
2333 if (error)
2334 return (error);
2335 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_REMOVE]);
1c79356b 2336 nfsm_fhtom(dvp, v3);
91447636 2337 nfsm_strtom(name, namelen, NFS_MAXNAMLEN, v3);
fa4905b1 2338 nfsm_request(dvp, NFSPROC_REMOVE, proc, cred, &xid);
e5568f75 2339 if (v3 && mrep)
91447636 2340 nfsm_wcc_data(dvp, &premtime, wccpostattr, &xid);
1c79356b 2341 nfsm_reqdone;
55e303ae 2342 VTONFS(dvp)->n_flag |= NMODIFIED;
483a1d10 2343 /* if directory hadn't changed, update namecache mtime */
91447636
A
2344 if (nfstimespeccmp(&VTONFS(dvp)->n_ncmtime, &premtime, ==))
2345 VTONFS(dvp)->n_ncmtime = VTONFS(dvp)->n_vattr.nva_mtime;
483a1d10 2346 if (!wccpostattr)
91447636 2347 NATTRINVALIDATE(VTONFS(dvp));
1c79356b
A
2348 return (error);
2349}
2350
2351/*
2352 * nfs file rename call
2353 */
2354static int
2355nfs_rename(ap)
91447636
A
2356 struct vnop_rename_args /* {
2357 struct vnodeop_desc *a_desc;
2358 vnode_t a_fdvp;
2359 vnode_t a_fvp;
1c79356b 2360 struct componentname *a_fcnp;
91447636
A
2361 vnode_t a_tdvp;
2362 vnode_t a_tvp;
1c79356b 2363 struct componentname *a_tcnp;
91447636 2364 vfs_context_t a_context;
1c79356b
A
2365 } */ *ap;
2366{
91447636
A
2367 vnode_t fvp = ap->a_fvp;
2368 vnode_t tvp = ap->a_tvp;
2369 vnode_t fdvp = ap->a_fdvp;
2370 vnode_t tdvp = ap->a_tdvp;
2371 struct componentname *tcnp = ap->a_tcnp;
2372 struct componentname *fcnp = ap->a_fcnp;
483a1d10 2373 int error, inuse=0;
91447636
A
2374 mount_t fmp, tdmp, tmp;
2375 struct nfsnode *tnp;
2376 kauth_cred_t cred;
2377 proc_t p;
2378
2379 cred = vfs_context_ucred(ap->a_context);
2380 p = vfs_context_proc(ap->a_context);
2381
2382 tnp = tvp ? VTONFS(tvp) : NULL;
1c79356b 2383
1c79356b 2384 /* Check for cross-device rename */
91447636
A
2385 fmp = vnode_mount(fvp);
2386 tmp = tvp ? vnode_mount(tvp) : NULL;
2387 tdmp = vnode_mount(tdvp);
2388 if ((fmp != tdmp) || (tvp && (fmp != tmp))) {
1c79356b
A
2389 error = EXDEV;
2390 goto out;
2391 }
2392
2393 /*
2394 * If the tvp exists and is in use, sillyrename it before doing the
2395 * rename of the new file over it.
2396 * XXX Can't sillyrename a directory.
9bccf70c
A
2397 * Don't sillyrename if source and target are same vnode (hard
2398 * links or case-variants)
1c79356b 2399 */
9bccf70c 2400 if (tvp && tvp != fvp) {
91447636 2401 inuse = vnode_isinuse(tvp, 0);
9bccf70c 2402 }
91447636
A
2403 if (inuse && !tnp->n_sillyrename && vnode_vtype(tvp) != VDIR) {
2404 if ((error = nfs_sillyrename(tdvp, tvp, tcnp, cred, p))) {
9bccf70c
A
2405 /* sillyrename failed. Instead of pressing on, return error */
2406 goto out; /* should not be ENOENT. */
2407 } else {
2408 /* sillyrename succeeded.*/
9bccf70c
A
2409 tvp = NULL;
2410 }
1c79356b
A
2411 }
2412
2413 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
91447636 2414 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, cred, p);
1c79356b 2415
91447636
A
2416 /*
2417 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2418 */
2419 if (error == ENOENT)
2420 error = 0;
2421
2422 if (!error && tvp && tvp != fvp && !tnp->n_sillyrename) {
55e303ae
A
2423 /*
2424 * remove nfsnode from hash now so we can't accidentally find it
2425 * again if another object gets created with the same filehandle
2426 * before this vnode gets reclaimed
2427 */
91447636
A
2428 lck_mtx_lock(nfs_node_hash_mutex);
2429 LIST_REMOVE(tnp, n_hash);
2430 tnp->n_flag &= ~NHASHED;
2431 lck_mtx_unlock(nfs_node_hash_mutex);
55e303ae 2432 }
9bccf70c 2433
91447636 2434 /* purge the old name cache entries and enter the new one */
9bccf70c
A
2435 cache_purge(fvp);
2436 if (tvp) {
483a1d10 2437 cache_purge(tvp);
91447636
A
2438 if (!error && !tnp->n_sillyrename) {
2439 /* clear flags now: won't get nfs_inactive for recycled vnode */
2440 /* clear all flags other than these */
2441 tnp->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NHASHED);
2442 vnode_recycle(tvp);
2443 }
9bccf70c 2444 }
91447636
A
2445 if (!error)
2446 cache_enter(tdvp, fvp, tcnp);
2447
1c79356b 2448out:
1c79356b
A
2449 /*
2450 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2451 */
2452 if (error == ENOENT)
2453 error = 0;
2454 return (error);
2455}
2456
2457/*
91447636 2458 * Do an nfs rename rpc. Called from nfs_rename() and nfs_sillyrename().
1c79356b
A
2459 */
2460static int
2461nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, proc)
91447636 2462 vnode_t fdvp;
1c79356b
A
2463 char *fnameptr;
2464 int fnamelen;
91447636 2465 vnode_t tdvp;
1c79356b
A
2466 char *tnameptr;
2467 int tnamelen;
91447636
A
2468 kauth_cred_t cred;
2469 proc_t proc;
1c79356b
A
2470{
2471 register u_long *tl;
2472 register caddr_t cp;
2473 register long t1, t2;
2474 caddr_t bpos, dpos, cp2;
483a1d10 2475 int error = 0, fwccpostattr = 0, twccpostattr = 0;
91447636
A
2476 struct timespec fpremtime = { 0, 0 }, tpremtime = { 0, 0 };
2477 mbuf_t mreq, mrep, md, mb, mb2;
55e303ae 2478 int v3;
fa4905b1 2479 u_int64_t xid;
1c79356b 2480
91447636 2481 if (!VFSTONFS(vnode_mount(fdvp)))
55e303ae
A
2482 return (ENXIO);
2483 v3 = NFS_ISV3(fdvp);
2484
91447636 2485 nfsm_reqhead((NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
fa4905b1 2486 nfsm_rndup(tnamelen));
91447636
A
2487 if (error)
2488 return (error);
2489 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_RENAME]);
1c79356b 2490 nfsm_fhtom(fdvp, v3);
91447636 2491 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN, v3);
1c79356b 2492 nfsm_fhtom(tdvp, v3);
91447636 2493 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN, v3);
fa4905b1 2494 nfsm_request(fdvp, NFSPROC_RENAME, proc, cred, &xid);
e5568f75 2495 if (v3 && mrep) {
fa4905b1
A
2496 u_int64_t txid = xid;
2497
91447636
A
2498 nfsm_wcc_data(fdvp, &fpremtime, fwccpostattr, &xid);
2499 nfsm_wcc_data(tdvp, &tpremtime, twccpostattr, &txid);
1c79356b
A
2500 }
2501 nfsm_reqdone;
55e303ae 2502 VTONFS(fdvp)->n_flag |= NMODIFIED;
483a1d10 2503 /* if directory hadn't changed, update namecache mtime */
91447636
A
2504 if (nfstimespeccmp(&VTONFS(fdvp)->n_ncmtime, &fpremtime, ==))
2505 VTONFS(fdvp)->n_ncmtime = VTONFS(fdvp)->n_vattr.nva_mtime;
483a1d10 2506 if (!fwccpostattr)
91447636 2507 NATTRINVALIDATE(VTONFS(fdvp));
55e303ae 2508 VTONFS(tdvp)->n_flag |= NMODIFIED;
483a1d10 2509 /* if directory hadn't changed, update namecache mtime */
91447636
A
2510 if (nfstimespeccmp(&VTONFS(tdvp)->n_ncmtime, &tpremtime, ==))
2511 VTONFS(tdvp)->n_ncmtime = VTONFS(tdvp)->n_vattr.nva_mtime;
483a1d10 2512 if (!twccpostattr)
91447636 2513 NATTRINVALIDATE(VTONFS(tdvp));
1c79356b
A
2514 return (error);
2515}
2516
2517/*
2518 * nfs hard link create call
2519 */
2520static int
2521nfs_link(ap)
91447636
A
2522 struct vnop_link_args /* {
2523 struct vnodeop_desc *a_desc;
2524 vnode_t a_vp;
2525 vnode_t a_tdvp;
1c79356b 2526 struct componentname *a_cnp;
91447636 2527 vfs_context_t a_context;
1c79356b
A
2528 } */ *ap;
2529{
91447636
A
2530 vnode_t vp = ap->a_vp;
2531 vnode_t tdvp = ap->a_tdvp;
2532 struct componentname *cnp = ap->a_cnp;
2533 u_long *tl;
2534 caddr_t cp;
2535 long t1, t2;
1c79356b 2536 caddr_t bpos, dpos, cp2;
483a1d10 2537 int error = 0, wccpostattr = 0, attrflag = 0;
91447636
A
2538 struct timespec premtime = { 0, 0 };
2539 mbuf_t mreq, mrep, md, mb, mb2;
2540 int v3;
fa4905b1 2541 u_int64_t xid;
91447636
A
2542 kauth_cred_t cred;
2543 proc_t p;
1c79356b 2544
91447636 2545 if (vnode_mount(vp) != vnode_mount(tdvp)) {
1c79356b
A
2546 return (EXDEV);
2547 }
2548
91447636
A
2549 cred = vfs_context_ucred(ap->a_context);
2550 p = vfs_context_proc(ap->a_context);
55e303ae 2551
55e303ae
A
2552 v3 = NFS_ISV3(vp);
2553
1c79356b
A
2554 /*
2555 * Push all writes to the server, so that the attribute cache
2556 * doesn't get "out of sync" with the server.
2557 * XXX There should be a better way!
2558 */
91447636 2559 nfs_flush(vp, MNT_WAIT, cred, p, 0);
1c79356b 2560
91447636
A
2561 nfsm_reqhead(NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2562 if (error)
2563 return (error);
2564 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_LINK]);
1c79356b
A
2565 nfsm_fhtom(vp, v3);
2566 nfsm_fhtom(tdvp, v3);
91447636
A
2567 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN, v3);
2568 nfsm_request(vp, NFSPROC_LINK, p, cred, &xid);
e5568f75 2569 if (v3 && mrep) {
fa4905b1
A
2570 u_int64_t txid = xid;
2571
91447636
A
2572 nfsm_postop_attr_update(vp, v3, attrflag, &xid);
2573 nfsm_wcc_data(tdvp, &premtime, wccpostattr, &txid);
1c79356b
A
2574 }
2575 nfsm_reqdone;
1c79356b
A
2576
2577 VTONFS(tdvp)->n_flag |= NMODIFIED;
55e303ae 2578 if (!attrflag)
91447636 2579 NATTRINVALIDATE(VTONFS(vp));
483a1d10 2580 /* if directory hadn't changed, update namecache mtime */
91447636
A
2581 if (nfstimespeccmp(&VTONFS(tdvp)->n_ncmtime, &premtime, ==))
2582 VTONFS(tdvp)->n_ncmtime = VTONFS(tdvp)->n_vattr.nva_mtime;
483a1d10 2583 if (!wccpostattr)
91447636 2584 NATTRINVALIDATE(VTONFS(tdvp));
1c79356b
A
2585 /*
2586 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2587 */
2588 if (error == EEXIST)
2589 error = 0;
2590 return (error);
2591}
2592
2593/*
2594 * nfs symbolic link create call
2595 */
2596static int
2597nfs_symlink(ap)
91447636
A
2598 struct vnop_symlink_args /* {
2599 struct vnodeop_desc *a_desc;
2600 vnode_t a_dvp;
2601 vnode_t *a_vpp;
1c79356b 2602 struct componentname *a_cnp;
91447636 2603 struct vnode_attr *a_vap;
1c79356b 2604 char *a_target;
91447636 2605 vfs_context_t a_context;
1c79356b
A
2606 } */ *ap;
2607{
91447636
A
2608 vnode_t dvp = ap->a_dvp;
2609 struct vnode_attr *vap = ap->a_vap;
2610 struct componentname *cnp = ap->a_cnp;
2611 struct nfs_vattr nvattr;
2612 struct nfsv2_sattr *sp;
2613 u_long *tl;
2614 caddr_t cp;
2615 long t1, t2;
1c79356b 2616 caddr_t bpos, dpos, cp2;
91447636
A
2617 int slen, error = 0, wccpostattr = 0, gotvp = 0;
2618 struct timespec premtime = { 0, 0 };
2619 mbuf_t mreq, mrep, md, mb, mb2;
2620 vnode_t newvp = (vnode_t)0;
1c79356b 2621 int v3 = NFS_ISV3(dvp);
91447636 2622 int gotuid, gotgid;
fa4905b1 2623 u_int64_t xid;
91447636
A
2624 kauth_cred_t cred;
2625 proc_t p;
2626 struct nfsnode *np = NULL;
2627
2628 cred = vfs_context_ucred(ap->a_context);
2629 p = vfs_context_proc(ap->a_context);
1c79356b 2630
1c79356b 2631 slen = strlen(ap->a_target);
91447636 2632 nfsm_reqhead(NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1c79356b 2633 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
91447636
A
2634 if (error)
2635 return (error);
2636
2637 VATTR_SET_SUPPORTED(vap, va_mode);
2638 VATTR_SET_SUPPORTED(vap, va_uid);
2639 VATTR_SET_SUPPORTED(vap, va_gid);
2640 VATTR_SET_SUPPORTED(vap, va_data_size);
2641 VATTR_SET_SUPPORTED(vap, va_access_time);
2642 VATTR_SET_SUPPORTED(vap, va_modify_time);
2643 gotuid = VATTR_IS_ACTIVE(vap, va_uid);
2644 gotgid = VATTR_IS_ACTIVE(vap, va_gid);
2645
2646 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_SYMLINK]);
1c79356b 2647 nfsm_fhtom(dvp, v3);
91447636 2648 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN, v3);
1c79356b 2649 if (v3) {
91447636 2650 nfsm_v3sattr(vap);
1c79356b 2651 }
91447636 2652 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN, v3);
1c79356b 2653 if (!v3) {
91447636 2654 struct timespec neg1time = { -1, -1 };
1c79356b 2655 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
91447636
A
2656 sp->sa_mode = vtonfsv2_mode(VLNK,
2657 (VATTR_IS_ACTIVE(vap, va_mode) ? vap->va_mode : 0600));
2658 sp->sa_uid = gotuid ? (u_long)txdr_unsigned(vap->va_uid) : nfs_xdrneg1;
2659 sp->sa_gid = gotgid ? (u_long)txdr_unsigned(vap->va_gid) : nfs_xdrneg1;
2660 sp->sa_size = nfs_xdrneg1;
2661 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
2662 txdr_nfsv2time(&vap->va_access_time, &sp->sa_atime);
2663 } else {
2664 txdr_nfsv2time(&neg1time, &sp->sa_atime);
2665 }
2666 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
2667 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
2668 } else {
2669 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
2670 }
1c79356b 2671 }
91447636 2672 nfsm_request(dvp, NFSPROC_SYMLINK, p, cred, &xid);
e5568f75 2673 if (v3 && mrep) {
fa4905b1
A
2674 u_int64_t dxid = xid;
2675
1c79356b 2676 if (!error)
91447636
A
2677 nfsm_mtofh(dvp, cnp, newvp, v3, &xid, gotvp);
2678 nfsm_wcc_data(dvp, &premtime, wccpostattr, &dxid);
1c79356b
A
2679 }
2680 nfsm_reqdone;
55e303ae
A
2681
2682 VTONFS(dvp)->n_flag |= NMODIFIED;
483a1d10 2683 /* if directory hadn't changed, update namecache mtime */
91447636
A
2684 if (nfstimespeccmp(&VTONFS(dvp)->n_ncmtime, &premtime, ==))
2685 VTONFS(dvp)->n_ncmtime = VTONFS(dvp)->n_vattr.nva_mtime;
483a1d10 2686 if (!wccpostattr)
91447636
A
2687 NATTRINVALIDATE(VTONFS(dvp));
2688
1c79356b 2689 /*
91447636
A
2690 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2691 * if we can succeed in looking up the symlink.
1c79356b 2692 */
91447636
A
2693 if ((error == EEXIST) || (!error && !gotvp)) {
2694 if (newvp) {
2695 vnode_put(newvp);
2696 newvp = NULL;
2697 }
2698 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen, cred, p, &np);
2699 if (!error) {
2700 newvp = NFSTOV(np);
2701 if (vnode_vtype(newvp) != VLNK)
2702 error = EEXIST;
2703 }
2704 }
2705 if (!error && (gotuid || gotgid) &&
2706 (!newvp || nfs_getattrcache(newvp, &nvattr) ||
2707 (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
2708 (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
2709 /* clear ID bits if server didn't use them (or we can't tell) */
2710 VATTR_CLEAR_SUPPORTED(vap, va_uid);
2711 VATTR_CLEAR_SUPPORTED(vap, va_gid);
2712 }
2713 if (error) {
2714 if (newvp)
2715 vnode_put(newvp);
2716 } else {
2717 *ap->a_vpp = newvp;
2718 }
1c79356b
A
2719 return (error);
2720}
2721
2722/*
2723 * nfs make dir call
2724 */
2725static int
2726nfs_mkdir(ap)
91447636
A
2727 struct vnop_mkdir_args /* {
2728 struct vnodeop_desc *a_desc;
2729 vnode_t a_dvp;
2730 vnode_t *a_vpp;
1c79356b 2731 struct componentname *a_cnp;
91447636
A
2732 struct vnode_attr *a_vap;
2733 vfs_context_t a_context;
1c79356b
A
2734 } */ *ap;
2735{
91447636
A
2736 vnode_t dvp = ap->a_dvp;
2737 struct vnode_attr *vap = ap->a_vap;
2738 struct componentname *cnp = ap->a_cnp;
2739 struct nfs_vattr nvattr;
2740 struct nfsv2_sattr *sp;
2741 u_long *tl;
2742 caddr_t cp;
2743 long t1, t2;
2744 int len;
1c79356b 2745 struct nfsnode *np = (struct nfsnode *)0;
91447636 2746 vnode_t newvp = (vnode_t)0;
1c79356b 2747 caddr_t bpos, dpos, cp2;
483a1d10 2748 int error = 0, wccpostattr = 0;
91447636 2749 struct timespec premtime = { 0, 0 };
1c79356b 2750 int gotvp = 0;
91447636 2751 mbuf_t mreq, mrep, md, mb, mb2;
1c79356b 2752 int v3 = NFS_ISV3(dvp);
91447636 2753 int gotuid, gotgid;
fa4905b1 2754 u_int64_t xid, dxid;
91447636
A
2755 kauth_cred_t cred;
2756 proc_t p;
2757
2758 cred = vfs_context_ucred(ap->a_context);
2759 p = vfs_context_proc(ap->a_context);
1c79356b 2760
1c79356b 2761 len = cnp->cn_namelen;
91447636
A
2762 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2763 if (error)
2764 return (error);
2765
2766 VATTR_SET_SUPPORTED(vap, va_mode);
2767 VATTR_SET_SUPPORTED(vap, va_uid);
2768 VATTR_SET_SUPPORTED(vap, va_gid);
2769 VATTR_SET_SUPPORTED(vap, va_data_size);
2770 VATTR_SET_SUPPORTED(vap, va_access_time);
2771 VATTR_SET_SUPPORTED(vap, va_modify_time);
2772 gotuid = VATTR_IS_ACTIVE(vap, va_uid);
2773 gotgid = VATTR_IS_ACTIVE(vap, va_gid);
2774
2775 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_MKDIR]);
1c79356b 2776 nfsm_fhtom(dvp, v3);
91447636 2777 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN, v3);
1c79356b 2778 if (v3) {
91447636 2779 nfsm_v3sattr(vap);
1c79356b 2780 } else {
91447636 2781 struct timespec neg1time = { -1, -1 };
1c79356b 2782 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
91447636
A
2783 sp->sa_mode = vtonfsv2_mode(VDIR,
2784 (VATTR_IS_ACTIVE(vap, va_mode) ? vap->va_mode : 0600));
2785 sp->sa_uid = gotuid ? (u_long)txdr_unsigned(vap->va_uid) : nfs_xdrneg1;
2786 sp->sa_gid = gotgid ? (u_long)txdr_unsigned(vap->va_gid) : nfs_xdrneg1;
2787 sp->sa_size = nfs_xdrneg1;
2788 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
2789 txdr_nfsv2time(&vap->va_access_time, &sp->sa_atime);
2790 } else {
2791 txdr_nfsv2time(&neg1time, &sp->sa_atime);
2792 }
2793 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
2794 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
2795 } else {
2796 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
2797 }
1c79356b 2798 }
91447636 2799 nfsm_request(dvp, NFSPROC_MKDIR, p, cred, &xid);
fa4905b1 2800 dxid = xid;
1c79356b 2801 if (!error)
91447636 2802 nfsm_mtofh(dvp, cnp, newvp, v3, &xid, gotvp);
e5568f75 2803 if (v3 && mrep)
91447636 2804 nfsm_wcc_data(dvp, &premtime, wccpostattr, &dxid);
1c79356b 2805 nfsm_reqdone;
55e303ae 2806 VTONFS(dvp)->n_flag |= NMODIFIED;
483a1d10 2807 /* if directory hadn't changed, update namecache mtime */
91447636
A
2808 if (nfstimespeccmp(&VTONFS(dvp)->n_ncmtime, &premtime, ==))
2809 VTONFS(dvp)->n_ncmtime = VTONFS(dvp)->n_vattr.nva_mtime;
483a1d10 2810 if (!wccpostattr)
91447636 2811 NATTRINVALIDATE(VTONFS(dvp));
1c79356b
A
2812 /*
2813 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2814 * if we can succeed in looking up the directory.
2815 */
2816 if (error == EEXIST || (!error && !gotvp)) {
2817 if (newvp) {
91447636
A
2818 vnode_put(newvp);
2819 newvp = NULL;
1c79356b 2820 }
91447636 2821 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cred, p, &np);
1c79356b
A
2822 if (!error) {
2823 newvp = NFSTOV(np);
91447636 2824 if (vnode_vtype(newvp) != VDIR)
1c79356b
A
2825 error = EEXIST;
2826 }
2827 }
91447636
A
2828 if (!error && (gotuid || gotgid) &&
2829 (!newvp || nfs_getattrcache(newvp, &nvattr) ||
2830 (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
2831 (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
2832 /* clear ID bits if server didn't use them (or we can't tell) */
2833 VATTR_CLEAR_SUPPORTED(vap, va_uid);
2834 VATTR_CLEAR_SUPPORTED(vap, va_gid);
2835 }
1c79356b
A
2836 if (error) {
2837 if (newvp)
91447636 2838 vnode_put(newvp);
483a1d10 2839 } else {
1c79356b 2840 *ap->a_vpp = newvp;
483a1d10 2841 }
1c79356b
A
2842 return (error);
2843}
2844
2845/*
2846 * nfs remove directory call
2847 */
2848static int
2849nfs_rmdir(ap)
91447636
A
2850 struct vnop_rmdir_args /* {
2851 struct vnodeop_desc *a_desc;
2852 vnode_t a_dvp;
2853 vnode_t a_vp;
1c79356b 2854 struct componentname *a_cnp;
91447636 2855 vfs_context_t a_context;
1c79356b
A
2856 } */ *ap;
2857{
91447636
A
2858 vnode_t vp = ap->a_vp;
2859 vnode_t dvp = ap->a_dvp;
2860 struct componentname *cnp = ap->a_cnp;
2861 u_long *tl;
2862 caddr_t cp;
2863 long t1, t2;
1c79356b 2864 caddr_t bpos, dpos, cp2;
483a1d10 2865 int error = 0, wccpostattr = 0;
91447636
A
2866 struct timespec premtime = { 0, 0 };
2867 mbuf_t mreq, mrep, md, mb, mb2;
1c79356b 2868 int v3 = NFS_ISV3(dvp);
fa4905b1 2869 u_int64_t xid;
91447636
A
2870 kauth_cred_t cred;
2871 proc_t p;
1c79356b 2872
91447636
A
2873 cred = vfs_context_ucred(ap->a_context);
2874 p = vfs_context_proc(ap->a_context);
2875
2876 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2877 if (error)
2878 return (error);
2879 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_RMDIR]);
1c79356b 2880 nfsm_fhtom(dvp, v3);
91447636
A
2881 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN, v3);
2882 nfsm_request(dvp, NFSPROC_RMDIR, p, cred, &xid);
e5568f75 2883 if (v3 && mrep)
91447636 2884 nfsm_wcc_data(dvp, &premtime, wccpostattr, &xid);
1c79356b 2885 nfsm_reqdone;
55e303ae 2886 VTONFS(dvp)->n_flag |= NMODIFIED;
483a1d10 2887 /* if directory hadn't changed, update namecache mtime */
91447636
A
2888 if (nfstimespeccmp(&VTONFS(dvp)->n_ncmtime, &premtime, ==))
2889 VTONFS(dvp)->n_ncmtime = VTONFS(dvp)->n_vattr.nva_mtime;
483a1d10 2890 if (!wccpostattr)
91447636 2891 NATTRINVALIDATE(VTONFS(dvp));
1c79356b 2892 cache_purge(vp);
1c79356b
A
2893 /*
2894 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2895 */
2896 if (error == ENOENT)
2897 error = 0;
483a1d10
A
2898 if (!error) {
2899 /*
2900 * remove nfsnode from hash now so we can't accidentally find it
2901 * again if another object gets created with the same filehandle
2902 * before this vnode gets reclaimed
2903 */
91447636 2904 lck_mtx_lock(nfs_node_hash_mutex);
483a1d10
A
2905 LIST_REMOVE(VTONFS(vp), n_hash);
2906 VTONFS(vp)->n_flag &= ~NHASHED;
91447636 2907 lck_mtx_unlock(nfs_node_hash_mutex);
483a1d10 2908 }
1c79356b
A
2909 return (error);
2910}
2911
2912/*
2913 * nfs readdir call
2914 */
2915static int
2916nfs_readdir(ap)
91447636
A
2917 struct vnop_readdir_args /* {
2918 struct vnodeop_desc *a_desc;
2919 vnode_t a_vp;
1c79356b 2920 struct uio *a_uio;
91447636
A
2921 int *a_eofflag;
2922 int *a_ncookies;
2923 u_long **a_cookies;
2924 vfs_context_t a_context;
1c79356b
A
2925 } */ *ap;
2926{
91447636
A
2927 vnode_t vp = ap->a_vp;
2928 struct nfsnode *np = VTONFS(vp);
2929 struct uio *uio = ap->a_uio;
1c79356b 2930 int tresid, error;
91447636
A
2931 struct nfs_vattr nvattr;
2932 kauth_cred_t cred;
2933 proc_t p;
1c79356b 2934
91447636 2935 if (vnode_vtype(vp) != VDIR)
1c79356b 2936 return (EPERM);
91447636
A
2937
2938 cred = vfs_context_ucred(ap->a_context);
2939 p = vfs_context_proc(ap->a_context);
2940
1c79356b
A
2941 /*
2942 * First, check for hit on the EOF offset cache
2943 */
2944 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2945 (np->n_flag & NMODIFIED) == 0) {
91447636
A
2946 if (!nfs_getattr(vp, &nvattr, cred, p)) {
2947 if (nfstimespeccmp(&np->n_mtime, &nvattr.nva_mtime, ==)) {
2948 OSAddAtomic(1, (SInt32*)&nfsstats.direofcache_hits);
55e303ae
A
2949 return (0);
2950 }
91447636 2951 if (nfstimespeccmp(&np->n_ncmtime, &nvattr.nva_mtime, !=)) {
483a1d10
A
2952 /* directory changed, purge any name cache entries */
2953 cache_purge(vp);
2954 }
1c79356b
A
2955 }
2956 }
2957
2958 /*
2959 * Call nfs_bioread() to do the real work.
2960 */
91447636
A
2961 // LP64todo - fix this
2962 tresid = uio_uio_resid(uio);
2963 error = nfs_bioread(vp, uio, 0, cred, p);
1c79356b 2964
91447636
A
2965 if (!error && uio_uio_resid(uio) == tresid)
2966 OSAddAtomic(1, (SInt32*)&nfsstats.direofcache_misses);
1c79356b
A
2967 return (error);
2968}
2969
2970/*
2971 * Readdir rpc call.
2972 * Called from below the buffer cache by nfs_doio().
2973 */
2974int
91447636
A
2975nfs_readdirrpc(
2976 vnode_t vp,
2977 struct uio *uiop,
2978 kauth_cred_t cred,
2979 proc_t p)
1c79356b 2980{
91447636 2981 register int len, skiplen, left;
1c79356b
A
2982 register struct dirent *dp;
2983 register u_long *tl;
2984 register caddr_t cp;
2985 register long t1, t2;
2986 register nfsuint64 *cookiep;
2987 caddr_t bpos, dpos, cp2;
91447636 2988 mbuf_t mreq, mrep, md, mb, mb2;
1c79356b 2989 nfsuint64 cookie;
55e303ae 2990 struct nfsmount *nmp;
1c79356b
A
2991 struct nfsnode *dnp = VTONFS(vp);
2992 u_quad_t fileno;
2993 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2994 int attrflag;
55e303ae 2995 int v3, nmreaddirsize;
fa4905b1 2996 u_int64_t xid;
1c79356b
A
2997
2998#ifndef nolint
2999 dp = (struct dirent *)0;
3000#endif
3001#if DIAGNOSTIC
3002 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (NFS_DIRBLKSIZ - 1)) ||
91447636 3003 (uio_uio_resid(uiop) & (NFS_DIRBLKSIZ - 1)))
1c79356b
A
3004 panic("nfs_readdirrpc: bad uio");
3005#endif
91447636 3006 nmp = VFSTONFS(vnode_mount(vp));
55e303ae
A
3007 if (!nmp)
3008 return (ENXIO);
3009 v3 = NFS_ISV3(vp);
3010 nmreaddirsize = nmp->nm_readdirsize;
1c79356b
A
3011
3012 /*
3013 * If there is no cookie, assume directory was stale.
3014 */
3015 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
3016 if (cookiep)
3017 cookie = *cookiep;
3018 else
3019 return (NFSERR_BAD_COOKIE);
3020 /*
3021 * Loop around doing readdir rpc's of size nm_readdirsize
3022 * truncated to a multiple of DIRBLKSIZ.
3023 * The stopping criteria is EOF or buffer full.
3024 */
3025 while (more_dirs && bigenough) {
91447636
A
3026 nfsm_reqhead(NFSX_FH(v3) + NFSX_READDIR(v3));
3027 if (error)
3028 goto nfsmout;
3029 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_READDIR]);
1c79356b
A
3030 nfsm_fhtom(vp, v3);
3031 if (v3) {
3032 nfsm_build(tl, u_long *, 5 * NFSX_UNSIGNED);
3033 *tl++ = cookie.nfsuquad[0];
3034 *tl++ = cookie.nfsuquad[1];
3035 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3036 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3037 } else {
3038 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
3039 *tl++ = cookie.nfsuquad[0];
3040 }
55e303ae 3041 *tl = txdr_unsigned(nmreaddirsize);
91447636 3042 nfsm_request(vp, NFSPROC_READDIR, p, cred, &xid);
1c79356b 3043 if (v3) {
e5568f75 3044 if (mrep) {
91447636 3045 nfsm_postop_attr_update(vp, v3, attrflag, &xid);
e5568f75 3046 }
1c79356b
A
3047 if (!error) {
3048 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
3049 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3050 dnp->n_cookieverf.nfsuquad[1] = *tl;
3051 } else {
91447636 3052 mbuf_freem(mrep);
1c79356b
A
3053 goto nfsmout;
3054 }
e5568f75
A
3055 } else if (!mrep) {
3056 // XXX assert error?
3057 goto nfsmout;
1c79356b
A
3058 }
3059 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3060 more_dirs = fxdr_unsigned(int, *tl);
3061
3062 /* loop thru the dir entries, doctoring them to 4bsd form */
3063 while (more_dirs && bigenough) {
3064 if (v3) {
3065 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3066 fxdr_hyper(tl, &fileno);
3067 len = fxdr_unsigned(int, *(tl + 2));
3068 } else {
3069 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
3070 fileno = fxdr_unsigned(u_quad_t, *tl++);
3071 len = fxdr_unsigned(int, *tl);
3072 }
91447636
A
3073 /* Note: v3 supports longer names, but struct dirent doesn't */
3074 /* so we just truncate the names to fit */
3075 if (len <= 0) {
1c79356b 3076 error = EBADRPC;
91447636 3077 mbuf_freem(mrep);
1c79356b
A
3078 goto nfsmout;
3079 }
91447636
A
3080 if (len > MAXNAMLEN) {
3081 skiplen = len - MAXNAMLEN;
3082 len = MAXNAMLEN;
3083 } else {
3084 skiplen = 0;
3085 }
1c79356b
A
3086 tlen = nfsm_rndup(len);
3087 if (tlen == len)
3088 tlen += 4; /* To ensure null termination */
3089 left = DIRBLKSIZ - blksiz;
91447636 3090 if ((tlen + (int)DIRHDSIZ) > left) {
1c79356b 3091 dp->d_reclen += left;
91447636
A
3092 uio_iov_base_add(uiop, left);
3093 uio_iov_len_add(uiop, -left);
1c79356b 3094 uiop->uio_offset += left;
91447636 3095 uio_uio_resid_add(uiop, -left);
1c79356b
A
3096 blksiz = 0;
3097 }
91447636 3098 if ((tlen + (int)DIRHDSIZ) > uio_uio_resid(uiop))
1c79356b
A
3099 bigenough = 0;
3100 if (bigenough) {
91447636
A
3101 // LP64todo - fix this!
3102 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
1c79356b
A
3103 dp->d_fileno = (int)fileno;
3104 dp->d_namlen = len;
3105 dp->d_reclen = tlen + DIRHDSIZ;
3106 dp->d_type = DT_UNKNOWN;
3107 blksiz += dp->d_reclen;
3108 if (blksiz == DIRBLKSIZ)
3109 blksiz = 0;
3110 uiop->uio_offset += DIRHDSIZ;
91447636
A
3111#if LP64KERN
3112 uio_uio_resid_add(uiop, -((int64_t)DIRHDSIZ));
3113 uio_iov_len_add(uiop, -((int64_t)DIRHDSIZ));
3114#else
3115 uio_uio_resid_add(uiop, -((int)DIRHDSIZ));
3116 uio_iov_len_add(uiop, -((int)DIRHDSIZ));
3117#endif
3118 uio_iov_base_add(uiop, DIRHDSIZ);
1c79356b 3119 nfsm_mtouio(uiop, len);
91447636
A
3120 // LP64todo - fix this!
3121 cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
1c79356b
A
3122 tlen -= len;
3123 *cp = '\0'; /* null terminate */
91447636
A
3124 uio_iov_base_add(uiop, tlen);
3125 uio_iov_len_add(uiop, -tlen);
1c79356b 3126 uiop->uio_offset += tlen;
91447636
A
3127 uio_uio_resid_add(uiop, -tlen);
3128 } else {
1c79356b 3129 nfsm_adv(nfsm_rndup(len));
91447636
A
3130 }
3131 if (skiplen)
3132 nfsm_adv(nfsm_rndup(skiplen));
1c79356b
A
3133 if (v3) {
3134 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3135 } else {
3136 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
3137 }
3138 if (bigenough) {
3139 cookie.nfsuquad[0] = *tl++;
3140 if (v3)
3141 cookie.nfsuquad[1] = *tl++;
3142 } else if (v3)
3143 tl += 2;
3144 else
3145 tl++;
3146 more_dirs = fxdr_unsigned(int, *tl);
3147 }
3148 /*
3149 * If at end of rpc data, get the eof boolean
3150 */
3151 if (!more_dirs) {
3152 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3153 more_dirs = (fxdr_unsigned(int, *tl) == 0);
3154 }
91447636 3155 mbuf_freem(mrep);
1c79356b
A
3156 }
3157 /*
3158 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3159 * by increasing d_reclen for the last record.
3160 */
3161 if (blksiz > 0) {
3162 left = DIRBLKSIZ - blksiz;
3163 dp->d_reclen += left;
91447636
A
3164 uio_iov_base_add(uiop, left);
3165 uio_iov_len_add(uiop, -left);
1c79356b 3166 uiop->uio_offset += left;
91447636 3167 uio_uio_resid_add(uiop, -left);
1c79356b
A
3168 }
3169
3170 /*
3171 * We are now either at the end of the directory or have filled the
3172 * block.
3173 */
3174 if (bigenough)
3175 dnp->n_direofoffset = uiop->uio_offset;
3176 else {
91447636 3177 if (uio_uio_resid(uiop) > 0)
1c79356b
A
3178 printf("EEK! readdirrpc resid > 0\n");
3179 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
91447636
A
3180 if (cookiep)
3181 *cookiep = cookie;
1c79356b
A
3182 }
3183nfsmout:
3184 return (error);
3185}
3186
3187/*
3188 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
3189 */
3190int
91447636
A
3191nfs_readdirplusrpc(
3192 vnode_t vp,
3193 struct uio *uiop,
3194 kauth_cred_t cred,
3195 proc_t p)
1c79356b 3196{
91447636
A
3197 int len, skiplen, left;
3198 struct dirent *dp;
3199 u_long *tl;
3200 caddr_t cp;
3201 long t1, t2;
3202 vnode_t newvp;
3203 nfsuint64 *cookiep;
3204 caddr_t bpos, dpos, cp2;
3205 mbuf_t mreq, mrep, md, mb, mb2;
3206 struct componentname cn, *cnp = &cn;
1c79356b 3207 nfsuint64 cookie;
55e303ae 3208 struct nfsmount *nmp;
1c79356b 3209 struct nfsnode *dnp = VTONFS(vp), *np;
91447636 3210 u_char *fhp;
1c79356b
A
3211 u_quad_t fileno;
3212 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
55e303ae 3213 int attrflag, fhsize, nmreaddirsize, nmrsize;
fa4905b1 3214 u_int64_t xid, savexid;
91447636 3215 struct nfs_vattr nvattr;
1c79356b
A
3216
3217#ifndef nolint
3218 dp = (struct dirent *)0;
3219#endif
3220#if DIAGNOSTIC
3221 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
91447636 3222 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)))
1c79356b
A
3223 panic("nfs_readdirplusrpc: bad uio");
3224#endif
91447636 3225 nmp = VFSTONFS(vnode_mount(vp));
55e303ae
A
3226 if (!nmp)
3227 return (ENXIO);
3228 nmreaddirsize = nmp->nm_readdirsize;
3229 nmrsize = nmp->nm_rsize;
3230
91447636 3231 bzero(cnp, sizeof(*cnp));
1c79356b
A
3232 newvp = NULLVP;
3233
3234 /*
3235 * If there is no cookie, assume directory was stale.
3236 */
3237 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
3238 if (cookiep)
3239 cookie = *cookiep;
3240 else
3241 return (NFSERR_BAD_COOKIE);
3242 /*
3243 * Loop around doing readdir rpc's of size nm_readdirsize
3244 * truncated to a multiple of DIRBLKSIZ.
3245 * The stopping criteria is EOF or buffer full.
3246 */
3247 while (more_dirs && bigenough) {
91447636
A
3248 nfsm_reqhead(NFSX_FH(1) + 6 * NFSX_UNSIGNED);
3249 if (error)
3250 goto nfsmout;
3251 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_READDIRPLUS]);
1c79356b
A
3252 nfsm_fhtom(vp, 1);
3253 nfsm_build(tl, u_long *, 6 * NFSX_UNSIGNED);
3254 *tl++ = cookie.nfsuquad[0];
3255 *tl++ = cookie.nfsuquad[1];
3256 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3257 *tl++ = dnp->n_cookieverf.nfsuquad[1];
55e303ae
A
3258 *tl++ = txdr_unsigned(nmreaddirsize);
3259 *tl = txdr_unsigned(nmrsize);
91447636 3260 nfsm_request(vp, NFSPROC_READDIRPLUS, p, cred, &xid);
fa4905b1 3261 savexid = xid;
e5568f75 3262 if (mrep) {
91447636 3263 nfsm_postop_attr_update(vp, 1, attrflag, &xid);
e5568f75 3264 }
1c79356b 3265 if (error) {
91447636 3266 mbuf_freem(mrep);
1c79356b
A
3267 goto nfsmout;
3268 }
3269 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3270 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3271 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3272 more_dirs = fxdr_unsigned(int, *tl);
3273
3274 /* loop thru the dir entries, doctoring them to 4bsd form */
3275 while (more_dirs && bigenough) {
3276 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3277 fxdr_hyper(tl, &fileno);
3278 len = fxdr_unsigned(int, *(tl + 2));
91447636
A
3279 /* Note: v3 supports longer names, but struct dirent doesn't */
3280 /* so we just truncate the names to fit */
3281 if (len <= 0) {
1c79356b 3282 error = EBADRPC;
91447636 3283 mbuf_freem(mrep);
1c79356b
A
3284 goto nfsmout;
3285 }
91447636
A
3286 if (len > MAXNAMLEN) {
3287 skiplen = len - MAXNAMLEN;
3288 len = MAXNAMLEN;
3289 } else {
3290 skiplen = 0;
3291 }
1c79356b
A
3292 tlen = nfsm_rndup(len);
3293 if (tlen == len)
3294 tlen += 4; /* To ensure null termination*/
3295 left = DIRBLKSIZ - blksiz;
91447636 3296 if ((tlen + (int)DIRHDSIZ) > left) {
1c79356b 3297 dp->d_reclen += left;
91447636
A
3298 uio_iov_base_add(uiop, left);
3299 uio_iov_len_add(uiop, -left);
1c79356b 3300 uiop->uio_offset += left;
91447636 3301 uio_uio_resid_add(uiop, -left);
1c79356b
A
3302 blksiz = 0;
3303 }
91447636 3304 if ((tlen + (int)DIRHDSIZ) > uio_uio_resid(uiop))
1c79356b
A
3305 bigenough = 0;
3306 if (bigenough) {
91447636
A
3307 // LP64todo - fix this!
3308 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
1c79356b
A
3309 dp->d_fileno = (int)fileno;
3310 dp->d_namlen = len;
3311 dp->d_reclen = tlen + DIRHDSIZ;
3312 dp->d_type = DT_UNKNOWN;
3313 blksiz += dp->d_reclen;
3314 if (blksiz == DIRBLKSIZ)
3315 blksiz = 0;
3316 uiop->uio_offset += DIRHDSIZ;
91447636
A
3317#if LP64KERN
3318 uio_uio_resid_add(uiop, -((int64_t)DIRHDSIZ));
3319 uio_iov_len_add(uiop, -((int64_t)DIRHDSIZ));
3320#else
3321 uio_uio_resid_add(uiop, -((int)DIRHDSIZ));
3322 uio_iov_len_add(uiop, -((int)DIRHDSIZ));
3323#endif
3324 uio_iov_base_add(uiop, DIRHDSIZ);
3325 // LP64todo - fix this!
3326 cnp->cn_nameptr = CAST_DOWN(caddr_t, uio_iov_base(uiop));
1c79356b
A
3327 cnp->cn_namelen = len;
3328 nfsm_mtouio(uiop, len);
91447636 3329 cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
1c79356b
A
3330 tlen -= len;
3331 *cp = '\0';
91447636
A
3332 uio_iov_base_add(uiop, tlen);
3333 uio_iov_len_add(uiop, -tlen);
1c79356b 3334 uiop->uio_offset += tlen;
91447636
A
3335 uio_uio_resid_add(uiop, -tlen);
3336 } else {
1c79356b 3337 nfsm_adv(nfsm_rndup(len));
91447636
A
3338 }
3339 if (skiplen)
3340 nfsm_adv(nfsm_rndup(skiplen));
1c79356b
A
3341 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3342 if (bigenough) {
3343 cookie.nfsuquad[0] = *tl++;
3344 cookie.nfsuquad[1] = *tl++;
3345 } else
3346 tl += 2;
3347
3348 /*
3349 * Since the attributes are before the file handle
3350 * (sigh), we must skip over the attributes and then
3351 * come back and get them.
3352 */
3353 attrflag = fxdr_unsigned(int, *tl);
3354 if (attrflag) {
91447636
A
3355 /* grab attributes */
3356 nfsm_attr_get(1, &nvattr);
3357 dp->d_type = IFTODT(VTTOIF(nvattr.nva_type));
3358 /* check for file handle */
1c79356b
A
3359 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3360 doit = fxdr_unsigned(int, *tl);
3361 if (doit) {
3362 nfsm_getfh(fhp, fhsize, 1);
3363 if (NFS_CMPFH(dnp, fhp, fhsize)) {
91447636
A
3364 error = vnode_ref(vp);
3365 if (error) {
3366 doit = 0;
3367 } else {
3368 newvp = vp;
3369 np = dnp;
3370 }
55e303ae
A
3371 } else if (!bigenough ||
3372 (cnp->cn_namelen == 2 &&
3373 cnp->cn_nameptr[1] == '.' &&
3374 cnp->cn_nameptr[0] == '.')) {
3375 /*
91447636 3376 * XXXmacko I don't think this ".." thing is a problem anymore.
55e303ae
A
3377 * don't doit if we can't guarantee
3378 * that this entry is NOT ".." because
3379 * we would have to drop the lock on
3380 * the directory before getting the
91447636 3381 * lock on the ".." vnode... and we
55e303ae
A
3382 * don't want to drop the dvp lock in
3383 * the middle of a readdirplus.
3384 */
3385 doit = 0;
1c79356b 3386 } else {
91447636
A
3387 cnp->cn_hash = 0;
3388
3389 error = nfs_nget(vnode_mount(vp), vp, cnp,
3390 fhp, fhsize, &nvattr, &xid,
3391 NG_MAKEENTRY, &np);
3392 if (error)
1c79356b
A
3393 doit = 0;
3394 else
3395 newvp = NFSTOV(np);
3396 }
3397 }
91447636
A
3398 /* update attributes if not already updated */
3399 if (doit && bigenough && (np->n_xid <= savexid)) {
fa4905b1 3400 xid = savexid;
91447636
A
3401 nfs_loadattrcache(np, &nvattr, &xid, 0);
3402 /* any error can be ignored */
1c79356b
A
3403 }
3404 } else {
3405 /* Just skip over the file handle */
3406 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3407 i = fxdr_unsigned(int, *tl);
3408 nfsm_adv(nfsm_rndup(i));
3409 }
3410 if (newvp != NULLVP) {
55e303ae 3411 if (newvp == vp)
91447636 3412 vnode_rele(newvp);
55e303ae 3413 else
91447636 3414 vnode_put(newvp);
1c79356b
A
3415 newvp = NULLVP;
3416 }
3417 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3418 more_dirs = fxdr_unsigned(int, *tl);
3419 }
3420 /*
3421 * If at end of rpc data, get the eof boolean
3422 */
3423 if (!more_dirs) {
3424 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3425 more_dirs = (fxdr_unsigned(int, *tl) == 0);
3426 }
91447636 3427 mbuf_freem(mrep);
1c79356b
A
3428 }
3429 /*
3430 * Fill last record, iff any, out to a multiple of NFS_DIRBLKSIZ
3431 * by increasing d_reclen for the last record.
3432 */
3433 if (blksiz > 0) {
3434 left = DIRBLKSIZ - blksiz;
3435 dp->d_reclen += left;
91447636
A
3436 uio_iov_base_add(uiop, left);
3437 uio_iov_len_add(uiop, -left);
1c79356b 3438 uiop->uio_offset += left;
91447636 3439 uio_uio_resid_add(uiop, -left);
1c79356b
A
3440 }
3441
3442 /*
3443 * We are now either at the end of the directory or have filled the
3444 * block.
3445 */
3446 if (bigenough)
3447 dnp->n_direofoffset = uiop->uio_offset;
3448 else {
91447636 3449 if (uio_uio_resid(uiop) > 0)
1c79356b
A
3450 printf("EEK! readdirplusrpc resid > 0\n");
3451 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
91447636
A
3452 if (cookiep)
3453 *cookiep = cookie;
1c79356b
A
3454 }
3455nfsmout:
1c79356b
A
3456 return (error);
3457}
3458
3459/*
3460 * Silly rename. To make the NFS filesystem that is stateless look a little
3461 * more like the "ufs" a remove of an active vnode is translated to a rename
3462 * to a funny looking filename that is removed by nfs_inactive on the
3463 * nfsnode. There is the potential for another process on a different client
3464 * to create the same funny name between the nfs_lookitup() fails and the
3465 * nfs_rename() completes, but...
3466 */
55e303ae
A
3467
3468/* format of "random" names and next name to try */
3469/* (note: shouldn't exceed size of sillyrename.s_name) */
3470static char sillyrename_name[] = ".nfsAAA%04x4.4";
3471
1c79356b 3472static int
91447636
A
3473nfs_sillyrename(
3474 vnode_t dvp,
3475 vnode_t vp,
3476 struct componentname *cnp,
3477 kauth_cred_t cred,
3478 proc_t p)
1c79356b
A
3479{
3480 register struct sillyrename *sp;
3481 struct nfsnode *np;
3482 int error;
3483 short pid;
91447636 3484 kauth_cred_t tmpcred;
55e303ae 3485 int i, j, k;
1c79356b 3486
483a1d10 3487 cache_purge(vp);
1c79356b
A
3488 np = VTONFS(vp);
3489#if DIAGNOSTIC
91447636 3490 if (vnode_vtype(vp) == VDIR)
1c79356b
A
3491 panic("nfs_sillyrename: dir");
3492#endif
3493 MALLOC_ZONE(sp, struct sillyrename *,
3494 sizeof (struct sillyrename), M_NFSREQ, M_WAITOK);
91447636
A
3495 if (!sp)
3496 return (ENOMEM);
3497 kauth_cred_ref(cred);
3498 sp->s_cred = cred;
1c79356b 3499 sp->s_dvp = dvp;
91447636
A
3500 error = vnode_ref(dvp);
3501 if (error)
3502 goto bad_norele;
1c79356b
A
3503
3504 /* Fudge together a funny name */
91447636 3505 pid = proc_pid(p);
55e303ae 3506 sp->s_namlen = sprintf(sp->s_name, sillyrename_name, pid);
1c79356b
A
3507
3508 /* Try lookitups until we get one that isn't there */
55e303ae 3509 i = j = k = 0;
91447636 3510 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred, p, NULL) == 0) {
55e303ae
A
3511 if (sp->s_name[4]++ >= 'z')
3512 sp->s_name[4] = 'A';
3513 if (++i > ('z' - 'A' + 1)) {
3514 i = 0;
3515 if (sp->s_name[5]++ >= 'z')
3516 sp->s_name[5] = 'A';
3517 if (++j > ('z' - 'A' + 1)) {
3518 j = 0;
3519 if (sp->s_name[6]++ >= 'z')
3520 sp->s_name[6] = 'A';
3521 if (++k > ('z' - 'A' + 1)) {
3522 error = EINVAL;
3523 goto bad;
3524 }
3525 }
1c79356b
A
3526 }
3527 }
55e303ae
A
3528 /* make note of next "random" name to try */
3529 if ((sillyrename_name[4] = (sp->s_name[4] + 1)) > 'z') {
3530 sillyrename_name[4] = 'A';
3531 if ((sillyrename_name[5] = (sp->s_name[5] + 1)) > 'z') {
3532 sillyrename_name[5] = 'A';
3533 if ((sillyrename_name[6] = (sp->s_name[6] + 1)) > 'z')
3534 sillyrename_name[6] = 'A';
3535 }
3536 }
3537 /* now, do the rename */
91447636
A
3538 error = nfs_renamerpc(dvp, cnp->cn_nameptr, cnp->cn_namelen,
3539 dvp, sp->s_name, sp->s_namlen, sp->s_cred, p);
3540 if (error)
1c79356b 3541 goto bad;
91447636 3542 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred, p, &np);
1c79356b
A
3543#if DIAGNOSTIC
3544 kprintf("sillyrename: %s, vp=%x, np=%x, dvp=%x\n",
3545 &sp->s_name[0], (unsigned)vp, (unsigned)np, (unsigned)dvp);
3546#endif
3547 np->n_sillyrename = sp;
3548 return (0);
3549bad:
91447636
A
3550 vnode_rele(sp->s_dvp);
3551bad_norele:
3552 tmpcred = sp->s_cred;
1c79356b 3553 sp->s_cred = NOCRED;
91447636 3554 kauth_cred_rele(tmpcred);
55e303ae 3555 FREE_ZONE((caddr_t)sp, sizeof (struct sillyrename), M_NFSREQ);
1c79356b
A
3556 return (error);
3557}
3558
3559/*
3560 * Look up a file name and optionally either update the file handle or
3561 * allocate an nfsnode, depending on the value of npp.
3562 * npp == NULL --> just do the lookup
3563 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
3564 * handled too
3565 * *npp != NULL --> update the file handle in the vnode
3566 */
3567static int
3568nfs_lookitup(dvp, name, len, cred, procp, npp)
91447636 3569 vnode_t dvp;
1c79356b
A
3570 char *name;
3571 int len;
91447636
A
3572 kauth_cred_t cred;
3573 proc_t procp;
1c79356b
A
3574 struct nfsnode **npp;
3575{
91447636
A
3576 u_long *tl;
3577 caddr_t cp;
3578 long t1, t2;
3579 vnode_t newvp = (vnode_t)0;
1c79356b
A
3580 struct nfsnode *np, *dnp = VTONFS(dvp);
3581 caddr_t bpos, dpos, cp2;
3582 int error = 0, fhlen, attrflag;
91447636
A
3583 mbuf_t mreq, mrep, md, mb, mb2;
3584 u_char *nfhp;
55e303ae 3585 int v3;
91447636
A
3586 u_int64_t xid, dxid, savedxid;
3587 struct nfs_vattr nvattr;
1c79356b 3588
91447636 3589 if (!VFSTONFS(vnode_mount(dvp)))
55e303ae
A
3590 return (ENXIO);
3591 v3 = NFS_ISV3(dvp);
3592
91447636
A
3593 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
3594 if (error)
3595 return (error);
3596 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_LOOKUP]);
1c79356b 3597 nfsm_fhtom(dvp, v3);
91447636 3598 nfsm_strtom(name, len, NFS_MAXNAMLEN, v3);
fa4905b1 3599 nfsm_request(dvp, NFSPROC_LOOKUP, procp, cred, &xid);
1c79356b 3600 if (npp && !error) {
91447636 3601 savedxid = xid;
1c79356b 3602 nfsm_getfh(nfhp, fhlen, v3);
91447636
A
3603 /* get attributes */
3604 if (v3) {
3605 nfsm_postop_attr_get(v3, attrflag, &nvattr);
3606 if (!attrflag) {
3607 /* We need valid attributes in order */
3608 /* to call nfs_nget/vnode_create(). */
3609 error = nfs_getattr_no_vnode(vnode_mount(dvp),
3610 nfhp, fhlen, cred, procp, &nvattr, &xid);
3611 if (error) {
3612 mbuf_freem(mrep);
3613 goto nfsmout;
3614 }
3615 }
3616 dxid = savedxid;
3617 nfsm_postop_attr_update(dvp, v3, attrflag, &dxid);
3618 } else {
3619 nfsm_attr_get(v3, &nvattr);
3620 }
1c79356b
A
3621 if (*npp) {
3622 np = *npp;
91447636
A
3623 if (fhlen != np->n_fhsize) {
3624 u_char *oldbuf = (np->n_fhsize > NFS_SMALLFH) ? np->n_fhp : NULL;
3625 if (fhlen > NFS_SMALLFH) {
3626 MALLOC_ZONE(np->n_fhp, u_char *, fhlen, M_NFSBIGFH, M_WAITOK);
3627 if (!np->n_fhp) {
3628 np->n_fhp = oldbuf;
3629 error = ENOMEM;
3630 mbuf_freem(mrep);
3631 goto nfsmout;
3632 }
3633 } else {
3634 np->n_fhp = &np->n_fh[0];
3635 }
3636 if (oldbuf) {
3637 FREE_ZONE(oldbuf, np->n_fhsize, M_NFSBIGFH);
3638 }
3639 }
3640 bcopy(nfhp, np->n_fhp, fhlen);
1c79356b
A
3641 np->n_fhsize = fhlen;
3642 newvp = NFSTOV(np);
91447636
A
3643 error = nfs_loadattrcache(np, &nvattr, &xid, 0);
3644 if (error) {
3645 mbuf_freem(mrep);
3646 goto nfsmout;
3647 }
1c79356b 3648 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
1c79356b 3649 newvp = dvp;
91447636
A
3650 if (dnp->n_xid <= savedxid) {
3651 dxid = savedxid;
3652 error = nfs_loadattrcache(dnp, &nvattr, &dxid, 0);
3653 if (error) {
3654 mbuf_freem(mrep);
3655 goto nfsmout;
3656 }
3657 }
1c79356b 3658 } else {
91447636
A
3659 struct componentname cn, *cnp = &cn;
3660 bzero(cnp, sizeof(*cnp));
3661 cnp->cn_nameptr = name;
3662 cnp->cn_namelen = len;
3663
3664 error = nfs_nget(vnode_mount(dvp), dvp, cnp, nfhp, fhlen,
3665 &nvattr, &xid, NG_MAKEENTRY, &np);
1c79356b 3666 if (error) {
91447636 3667 mbuf_freem(mrep);
1c79356b
A
3668 return (error);
3669 }
3670 newvp = NFSTOV(np);
3671 }
1c79356b
A
3672 }
3673 nfsm_reqdone;
3674 if (npp && *npp == NULL) {
3675 if (error) {
91447636 3676 if (newvp) {
1c79356b 3677 if (newvp == dvp)
91447636 3678 vnode_rele(newvp);
1c79356b 3679 else
91447636
A
3680 vnode_put(newvp);
3681 }
1c79356b
A
3682 } else
3683 *npp = np;
3684 }
3685 return (error);
3686}
3687
3688/*
3689 * Nfs Version 3 commit rpc
3690 */
55e303ae 3691int
91447636
A
3692nfs_commit(vp, offset, count, cred, procp)
3693 vnode_t vp;
1c79356b 3694 u_quad_t offset;
91447636
A
3695 u_int32_t count;
3696 kauth_cred_t cred;
3697 proc_t procp;
1c79356b 3698{
91447636
A
3699 caddr_t cp;
3700 u_long *tl;
3701 int t1, t2;
3702 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1c79356b 3703 caddr_t bpos, dpos, cp2;
483a1d10 3704 int error = 0, wccpostattr = 0;
91447636
A
3705 struct timespec premtime = { 0, 0 };
3706 mbuf_t mreq, mrep, md, mb, mb2;
8f6c56a5
A
3707 u_int64_t xid, wverf;
3708
91447636 3709 FSDBG(521, vp, offset, count, nmp->nm_state);
55e303ae
A
3710 if (!nmp)
3711 return (ENXIO);
3712 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
1c79356b 3713 return (0);
91447636
A
3714 nfsm_reqhead(NFSX_FH(1));
3715 if (error)
3716 return (error);
3717 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_COMMIT]);
1c79356b
A
3718 nfsm_fhtom(vp, 1);
3719 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED);
3720 txdr_hyper(&offset, tl);
3721 tl += 2;
91447636 3722 *tl = txdr_unsigned(count);
fa4905b1 3723 nfsm_request(vp, NFSPROC_COMMIT, procp, cred, &xid);
e5568f75 3724 if (mrep) {
91447636 3725 nfsm_wcc_data(vp, &premtime, wccpostattr, &xid);
483a1d10 3726 /* XXX can we do anything useful with the wcc info? */
e5568f75 3727 }
1c79356b
A
3728 if (!error) {
3729 nfsm_dissect(tl, u_long *, NFSX_V3WRITEVERF);
8f6c56a5
A
3730 fxdr_hyper(tl, &wverf);
3731 if (wverf != nmp->nm_verf) {
3732 nmp->nm_verf = wverf;
1c79356b
A
3733 error = NFSERR_STALEWRITEVERF;
3734 }
3735 }
3736 nfsm_reqdone;
3737 return (error);
3738}
3739
1c79356b 3740static int
91447636
A
3741nfs_blockmap(
3742 __unused struct vnop_blockmap_args /* {
3743 struct vnodeop_desc *a_desc;
3744 vnode_t a_vp;
3745 off_t a_foffset;
3746 size_t a_size;
3747 daddr64_t *a_bpn;
3748 size_t *a_run;
3749 void *a_poff;
3750 int a_flags;
3751 } */ *ap)
1c79356b 3752{
91447636 3753 return (ENOTSUP);
1c79356b
A
3754}
3755
1c79356b
A
3756/*
3757 * Mmap a file
3758 *
3759 * NB Currently unsupported.
3760 */
91447636 3761/*ARGSUSED*/
1c79356b 3762static int
91447636
A
3763nfs_mmap(
3764 __unused struct vnop_mmap_args /* {
3765 struct vnodeop_desc *a_desc;
3766 vnode_t a_vp;
3767 int a_fflags;
3768 kauth_cred_t a_cred;
3769 proc_t a_p;
3770 } */ *ap)
1c79356b
A
3771{
3772
3773 return (EINVAL);
3774}
3775
3776/*
91447636 3777 * fsync vnode op. Just call nfs_flush() with commit == 1.
1c79356b
A
3778 */
3779/* ARGSUSED */
3780static int
3781nfs_fsync(ap)
91447636 3782 struct vnop_fsync_args /* {
1c79356b 3783 struct vnodeop_desc *a_desc;
91447636
A
3784 vnode_t a_vp;
3785 int a_waitfor;
3786 vfs_context_t a_context;
1c79356b
A
3787 } */ *ap;
3788{
91447636
A
3789 kauth_cred_t cred = vfs_context_ucred(ap->a_context);
3790 proc_t p = vfs_context_proc(ap->a_context);
3791 struct nfsnode *np = VTONFS(ap->a_vp);
3792 int error;
3793
3794 np->n_flag |= NWRBUSY;
3795 error = nfs_flush(ap->a_vp, ap->a_waitfor, cred, p, 0);
3796 np->n_flag &= ~NWRBUSY;
3797 return (error);
1c79356b 3798}
55e303ae
A
3799
3800int
91447636 3801nfs_flushcommits(vnode_t vp, proc_t p, int nowait)
1c79356b 3802{
55e303ae 3803 struct nfsnode *np = VTONFS(vp);
91447636
A
3804 struct nfsbuf *bp;
3805 struct nfsbuflists blist, commitlist;
3806 int error = 0, retv, wcred_set, flags;
1c79356b 3807 u_quad_t off, endoff, toff;
91447636
A
3808 u_int32_t count;
3809 kauth_cred_t wcred = NULL;
1c79356b 3810
55e303ae 3811 FSDBG_TOP(557, vp, np, 0, 0);
1c79356b
A
3812
3813 /*
55e303ae 3814 * A nb_flags == (NB_DELWRI | NB_NEEDCOMMIT) block has been written to the
1c79356b 3815 * server, but nas not been committed to stable storage on the server
55e303ae
A
3816 * yet. The byte range is worked out for as many nfsbufs as we can handle
3817 * and the commit rpc is done.
1c79356b 3818 */
91447636 3819 if (!LIST_EMPTY(&np->n_dirtyblkhd))
1c79356b 3820 np->n_flag |= NMODIFIED;
55e303ae 3821
1c79356b
A
3822 off = (u_quad_t)-1;
3823 endoff = 0;
55e303ae 3824 wcred_set = 0;
91447636 3825 LIST_INIT(&commitlist);
1c79356b 3826
91447636 3827 if (!VFSTONFS(vnode_mount(vp))) {
55e303ae
A
3828 error = ENXIO;
3829 goto done;
3830 }
3831 if (!NFS_ISV3(vp)) {
3832 error = EINVAL;
3833 goto done;
3834 }
9bccf70c 3835
91447636
A
3836 flags = NBI_DIRTY;
3837 if (nowait)
3838 flags |= NBI_NOWAIT;
3839 lck_mtx_lock(nfs_buf_mutex);
3840 if (!nfs_buf_iterprepare(np, &blist, flags)) {
3841 while ((bp = LIST_FIRST(&blist))) {
3842 LIST_REMOVE(bp, nb_vnbufs);
3843 LIST_INSERT_HEAD(&np->n_dirtyblkhd, bp, nb_vnbufs);
3844 error = nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0);
3845 if (error)
3846 continue;
8f6c56a5
A
3847 if (ISSET(bp->nb_flags, NB_NEEDCOMMIT))
3848 nfs_buf_check_write_verifier(np, bp);
91447636
A
3849 if (((bp->nb_flags & (NB_DELWRI | NB_NEEDCOMMIT))
3850 != (NB_DELWRI | NB_NEEDCOMMIT))) {
3851 nfs_buf_drop(bp);
3852 continue;
fa4905b1 3853 }
91447636
A
3854 nfs_buf_remfree(bp);
3855 lck_mtx_unlock(nfs_buf_mutex);
3856 /*
3857 * we need a upl to see if the page has been
3858 * dirtied (think mmap) since the unstable write, and
3859 * also to prevent vm from paging it during our commit rpc
3860 */
3861 if (!ISSET(bp->nb_flags, NB_PAGELIST)) {
3862 retv = nfs_buf_upl_setup(bp);
3863 if (retv) {
3864 /* unable to create upl */
3865 /* vm object must no longer exist */
3866 /* this could be fatal if we need */
3867 /* to write the data again, we'll see... */
3868 printf("nfs_flushcommits: upl create failed %d\n", retv);
3869 bp->nb_valid = bp->nb_dirty = 0;
3870 }
3871 }
3872 nfs_buf_upl_check(bp);
3873 lck_mtx_lock(nfs_buf_mutex);
9bccf70c 3874
91447636
A
3875 FSDBG(557, bp, bp->nb_flags, bp->nb_valid, bp->nb_dirty);
3876 FSDBG(557, bp->nb_validoff, bp->nb_validend,
3877 bp->nb_dirtyoff, bp->nb_dirtyend);
9bccf70c 3878
91447636
A
3879 /*
3880 * We used to check for dirty pages here; if there were any
3881 * we'd abort the commit and force the entire buffer to be
3882 * written again.
3883 *
3884 * Instead of doing that, we now go ahead and commit the dirty
3885 * range, and then leave the buffer around with dirty pages
3886 * that will be written out later.
3887 */
55e303ae 3888
91447636
A
3889 /*
3890 * Work out if all buffers are using the same cred
3891 * so we can deal with them all with one commit.
3892 *
3893 * XXX creds in bp's must be obtained by kauth_cred_ref on
3894 * the same original cred in order for them to be equal.
3895 */
3896 if (wcred_set == 0) {
3897 wcred = bp->nb_wcred;
3898 if (wcred == NOCRED)
3899 panic("nfs: needcommit w/out wcred");
3900 wcred_set = 1;
3901 } else if ((wcred_set == 1) && wcred != bp->nb_wcred) {
3902 wcred_set = -1;
3903 }
3904 SET(bp->nb_flags, NB_WRITEINPROG);
1c79356b 3905
91447636
A
3906 /*
3907 * A list of these buffers is kept so that the
3908 * second loop knows which buffers have actually
3909 * been committed. This is necessary, since there
3910 * may be a race between the commit rpc and new
3911 * uncommitted writes on the file.
3912 */
3913 LIST_REMOVE(bp, nb_vnbufs);
3914 LIST_INSERT_HEAD(&commitlist, bp, nb_vnbufs);
3915 toff = NBOFF(bp) + bp->nb_dirtyoff;
3916 if (toff < off)
3917 off = toff;
3918 toff += (u_quad_t)(bp->nb_dirtyend - bp->nb_dirtyoff);
3919 if (toff > endoff)
3920 endoff = toff;
1c79356b 3921 }
91447636 3922 nfs_buf_itercomplete(np, &blist, NBI_DIRTY);
55e303ae 3923 }
91447636 3924 lck_mtx_unlock(nfs_buf_mutex);
55e303ae 3925
91447636 3926 if (LIST_EMPTY(&commitlist)) {
55e303ae
A
3927 error = ENOBUFS;
3928 goto done;
3929 }
3930
3931 /*
3932 * Commit data on the server, as required.
3933 * If all bufs are using the same wcred, then use that with
3934 * one call for all of them, otherwise commit each one
3935 * separately.
3936 */
91447636
A
3937 if (wcred_set == 1) {
3938 /*
3939 * Note, it's possible the commit range could be >2^32-1.
3940 * If it is, we'll send one commit that covers the whole file.
3941 */
3942 if ((endoff - off) > 0xffffffff)
3943 count = 0;
3944 else
3945 count = (endoff - off);
3946 retv = nfs_commit(vp, off, count, wcred, p);
3947 } else {
55e303ae 3948 retv = 0;
91447636
A
3949 LIST_FOREACH(bp, &commitlist, nb_vnbufs) {
3950 toff = NBOFF(bp) + bp->nb_dirtyoff;
3951 count = bp->nb_dirtyend - bp->nb_dirtyoff;
3952 retv = nfs_commit(vp, toff, count, bp->nb_wcred, p);
3953 if (retv)
3954 break;
55e303ae
A
3955 }
3956 }
55e303ae
A
3957
3958 /*
3959 * Now, either mark the blocks I/O done or mark the
3960 * blocks dirty, depending on whether the commit
3961 * succeeded.
3962 */
91447636
A
3963 while ((bp = LIST_FIRST(&commitlist))) {
3964 LIST_REMOVE(bp, nb_vnbufs);
55e303ae 3965 FSDBG(557, bp, retv, bp->nb_flags, bp->nb_dirty);
55e303ae 3966 CLR(bp->nb_flags, (NB_NEEDCOMMIT | NB_WRITEINPROG));
55e303ae
A
3967 np->n_needcommitcnt--;
3968 CHECK_NEEDCOMMITCNT(np);
3969
3970 if (retv) {
91447636
A
3971 /* move back to dirty list */
3972 lck_mtx_lock(nfs_buf_mutex);
3973 LIST_INSERT_HEAD(&VTONFS(vp)->n_dirtyblkhd, bp, nb_vnbufs);
3974 lck_mtx_unlock(nfs_buf_mutex);
483a1d10 3975 nfs_buf_release(bp, 1);
91447636
A
3976 continue;
3977 }
55e303ae 3978
91447636
A
3979 vnode_startwrite(vp);
3980 if (ISSET(bp->nb_flags, NB_DELWRI)) {
3981 OSAddAtomic(-1, (SInt32*)&nfs_nbdwrite);
3982 NFSBUFCNTCHK(0);
3983 wakeup(&nfs_nbdwrite);
3984 }
3985 CLR(bp->nb_flags, (NB_READ|NB_DONE|NB_ERROR|NB_DELWRI));
3986 /* if block still has dirty pages, we don't want it to */
3987 /* be released in nfs_buf_iodone(). So, don't set NB_ASYNC. */
3988 if (!bp->nb_dirty)
3989 SET(bp->nb_flags, NB_ASYNC);
55e303ae 3990
91447636
A
3991 /* move to clean list */
3992 lck_mtx_lock(nfs_buf_mutex);
3993 LIST_INSERT_HEAD(&VTONFS(vp)->n_cleanblkhd, bp, nb_vnbufs);
3994 lck_mtx_unlock(nfs_buf_mutex);
55e303ae 3995
91447636 3996 bp->nb_dirtyoff = bp->nb_dirtyend = 0;
55e303ae 3997
91447636
A
3998 nfs_buf_iodone(bp);
3999 if (bp->nb_dirty) {
4000 /* throw it back in as a delayed write buffer */
4001 CLR(bp->nb_flags, NB_DONE);
4002 nfs_buf_write_delayed(bp, p);
1c79356b 4003 }
55e303ae
A
4004 }
4005
4006done:
55e303ae
A
4007 FSDBG_BOT(557, vp, np, 0, error);
4008 return (error);
4009}
1c79356b 4010
55e303ae
A
4011/*
4012 * Flush all the blocks associated with a vnode.
4013 * Walk through the buffer pool and push any dirty pages
4014 * associated with the vnode.
4015 */
91447636
A
4016int
4017nfs_flush(
4018 vnode_t vp,
4019 int waitfor,
4020 __unused kauth_cred_t cred,
4021 proc_t p,
4022 int ignore_writeerr)
55e303ae
A
4023{
4024 struct nfsnode *np = VTONFS(vp);
91447636
A
4025 struct nfsbuf *bp;
4026 struct nfsbuflists blist;
4027 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4028 int error = 0, error2, slptimeo = 0, slpflag = 0;
4029 int flags, passone = 1;
55e303ae 4030
483a1d10 4031 FSDBG_TOP(517, vp, np, waitfor, 0);
55e303ae
A
4032
4033 if (!nmp) {
4034 error = ENXIO;
4035 goto done;
1c79356b 4036 }
55e303ae
A
4037 if (nmp->nm_flag & NFSMNT_INT)
4038 slpflag = PCATCH;
55e303ae 4039
1c79356b 4040 /*
483a1d10
A
4041 * On the first pass, start async/unstable writes on all
4042 * delayed write buffers. Then wait for all writes to complete
4043 * and call nfs_flushcommits() to commit any uncommitted buffers.
4044 * On all subsequent passes, start STABLE writes on any remaining
4045 * dirty buffers. Then wait for all writes to complete.
1c79356b 4046 */
55e303ae 4047again:
91447636
A
4048 lck_mtx_lock(nfs_buf_mutex);
4049 FSDBG(518, LIST_FIRST(&np->n_dirtyblkhd), np->n_flag, 0, 0);
4050 if (!LIST_EMPTY(&np->n_dirtyblkhd))
55e303ae 4051 np->n_flag |= NMODIFIED;
91447636
A
4052 if (!VFSTONFS(vnode_mount(vp))) {
4053 lck_mtx_unlock(nfs_buf_mutex);
55e303ae 4054 error = ENXIO;
1c79356b
A
4055 goto done;
4056 }
55e303ae
A
4057
4058 /* Start/do any write(s) that are required. */
91447636
A
4059 if (!nfs_buf_iterprepare(np, &blist, NBI_DIRTY)) {
4060 while ((bp = LIST_FIRST(&blist))) {
4061 LIST_REMOVE(bp, nb_vnbufs);
4062 LIST_INSERT_HEAD(&np->n_dirtyblkhd, bp, nb_vnbufs);
4063 flags = (passone || (waitfor != MNT_WAIT)) ? NBAC_NOWAIT : 0;
4064 if (flags != NBAC_NOWAIT)
4065 nfs_buf_refget(bp);
4066 while ((error = nfs_buf_acquire(bp, flags, slpflag, slptimeo))) {
4067 FSDBG(524, bp, flags, bp->nb_lflags, bp->nb_flags);
4068 if (error == EBUSY)
4069 break;
4070 if (error) {
4071 error2 = nfs_sigintr(VFSTONFS(vnode_mount(vp)), NULL, p);
4072 if (error2) {
4073 if (flags != NBAC_NOWAIT)
4074 nfs_buf_refrele(bp);
4075 nfs_buf_itercomplete(np, &blist, NBI_DIRTY);
4076 lck_mtx_unlock(nfs_buf_mutex);
4077 error = error2;
4078 goto done;
4079 }
4080 if (slpflag == PCATCH) {
4081 slpflag = 0;
4082 slptimeo = 2 * hz;
4083 }
fa4905b1 4084 }
1c79356b 4085 }
91447636
A
4086 if (flags != NBAC_NOWAIT)
4087 nfs_buf_refrele(bp);
4088 if (error == EBUSY)
4089 continue;
4090 if (!bp->nb_vp) {
4091 /* buffer is no longer valid */
4092 nfs_buf_drop(bp);
4093 continue;
4094 }
8f6c56a5
A
4095 if (ISSET(bp->nb_flags, NB_NEEDCOMMIT))
4096 nfs_buf_check_write_verifier(np, bp);
91447636
A
4097 if (!ISSET(bp->nb_flags, NB_DELWRI))
4098 panic("nfs_flush: not dirty");
4099 FSDBG(525, bp, passone, bp->nb_lflags, bp->nb_flags);
4100 if ((passone || (waitfor != MNT_WAIT)) &&
4101 ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
4102 nfs_buf_drop(bp);
4103 continue;
4104 }
4105 nfs_buf_remfree(bp);
4106 lck_mtx_unlock(nfs_buf_mutex);
4107 if (ISSET(bp->nb_flags, NB_ERROR)) {
4108 np->n_error = bp->nb_error ? bp->nb_error : EIO;
4109 np->n_flag |= NWRITEERR;
4110 nfs_buf_release(bp, 1);
4111 lck_mtx_lock(nfs_buf_mutex);
4112 continue;
4113 }
4114 SET(bp->nb_flags, NB_ASYNC);
4115 if (!passone) {
4116 /* NB_STABLE forces this to be written FILESYNC */
4117 SET(bp->nb_flags, NB_STABLE);
4118 }
4119 nfs_buf_write(bp);
4120 lck_mtx_lock(nfs_buf_mutex);
55e303ae 4121 }
91447636 4122 nfs_buf_itercomplete(np, &blist, NBI_DIRTY);
1c79356b 4123 }
91447636 4124 lck_mtx_unlock(nfs_buf_mutex);
55e303ae 4125
1c79356b 4126 if (waitfor == MNT_WAIT) {
91447636
A
4127 while ((error = vnode_waitforwrites(vp, 0, slpflag, slptimeo, "nfsflush"))) {
4128 error2 = nfs_sigintr(VFSTONFS(vnode_mount(vp)), NULL, p);
4129 if (error2) {
4130 error = error2;
1c79356b 4131 goto done;
91447636
A
4132 }
4133 if (slpflag == PCATCH) {
1c79356b
A
4134 slpflag = 0;
4135 slptimeo = 2 * hz;
1c79356b
A
4136 }
4137 }
483a1d10
A
4138 }
4139
4140 if (NFS_ISV3(vp)) {
4141 /* loop while it looks like there are still buffers to be */
4142 /* commited and nfs_flushcommits() seems to be handling them. */
4143 while (np->n_needcommitcnt)
91447636 4144 if (nfs_flushcommits(vp, p, 0))
483a1d10
A
4145 break;
4146 }
4147
4148 if (passone) {
4149 passone = 0;
4150 goto again;
4151 }
4152
13fec989
A
4153 if (waitfor == MNT_WAIT) {
4154 if (!LIST_EMPTY(&np->n_dirtyblkhd))
4155 goto again;
4156 /* if we have no dirty blocks, we can clear the modified flag */
3a60a9f5 4157 np->n_flag &= ~NMODIFIED;
13fec989 4158 }
483a1d10 4159
fa4905b1 4160 FSDBG(526, np->n_flag, np->n_error, 0, 0);
91447636 4161 if (!ignore_writeerr && (np->n_flag & NWRITEERR)) {
1c79356b
A
4162 error = np->n_error;
4163 np->n_flag &= ~NWRITEERR;
4164 }
4165done:
fa4905b1 4166 FSDBG_BOT(517, vp, np, error, 0);
1c79356b
A
4167 return (error);
4168}
4169
4170/*
91447636 4171 * Do an nfs pathconf rpc.
1c79356b 4172 */
91447636
A
4173int
4174nfs_pathconfrpc(
4175 vnode_t vp,
4176 struct nfsv3_pathconf *pc,
4177 kauth_cred_t cred,
4178 proc_t procp)
1c79356b 4179{
91447636
A
4180 mbuf_t mreq, mrep, md, mb, mb2;
4181 caddr_t bpos, dpos, cp, cp2;
4182 int32_t t1, t2;
4183 u_long *tl;
4184 u_int64_t xid;
4185 int attrflag, error = 0;
4186 struct nfsv3_pathconf *mpc;
1c79356b 4187
91447636
A
4188 /* fetch pathconf info from server */
4189 nfsm_reqhead(NFSX_FH(1));
4190 if (error)
4191 return (error);
4192 nfsm_fhtom(vp, 1);
4193 nfsm_request(vp, NFSPROC_PATHCONF, procp, cred, &xid);
4194 nfsm_postop_attr_update(vp, 1, attrflag, &xid);
4195 if (!error) {
4196 nfsm_dissect(mpc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
4197 pc->pc_linkmax = fxdr_unsigned(long, mpc->pc_linkmax);
4198 pc->pc_namemax = fxdr_unsigned(long, mpc->pc_namemax);
4199 pc->pc_chownrestricted = fxdr_unsigned(long, mpc->pc_chownrestricted);
4200 pc->pc_notrunc = fxdr_unsigned(long, mpc->pc_notrunc);
4201 pc->pc_caseinsensitive = fxdr_unsigned(long, mpc->pc_caseinsensitive);
4202 pc->pc_casepreserving = fxdr_unsigned(long, mpc->pc_casepreserving);
4203 }
4204 nfsm_reqdone;
1c79356b 4205
91447636 4206 return (error);
1c79356b
A
4207}
4208
91447636
A
4209void
4210nfs_pathconf_cache(struct nfsmount *nmp, struct nfsv3_pathconf *pc)
1c79356b 4211{
91447636
A
4212 nmp->nm_state |= NFSSTA_GOTPATHCONF;
4213 nmp->nm_fsinfo.linkmax = pc->pc_linkmax;
4214 nmp->nm_fsinfo.namemax = pc->pc_namemax;
4215 nmp->nm_fsinfo.pcflags = 0;
4216 if (pc->pc_notrunc)
4217 nmp->nm_fsinfo.pcflags |= NFSPCINFO_NOTRUNC;
4218 if (pc->pc_chownrestricted)
4219 nmp->nm_fsinfo.pcflags |= NFSPCINFO_CHOWN_RESTRICTED;
4220 if (pc->pc_caseinsensitive)
4221 nmp->nm_fsinfo.pcflags |= NFSPCINFO_CASE_INSENSITIVE;
4222 if (pc->pc_casepreserving)
4223 nmp->nm_fsinfo.pcflags |= NFSPCINFO_CASE_PRESERVING;
1c79356b
A
4224}
4225
4226/*
91447636
A
4227 * Return POSIX pathconf information applicable to nfs.
4228 *
4229 * The NFS V2 protocol doesn't support this, so just return EINVAL
4230 * for V2.
1c79356b 4231 */
91447636 4232/* ARGSUSED */
1c79356b 4233static int
91447636
A
4234nfs_pathconf(ap)
4235 struct vnop_pathconf_args /* {
4236 struct vnodeop_desc *a_desc;
4237 vnode_t a_vp;
4238 int a_name;
4239 register_t *a_retval;
4240 vfs_context_t a_context;
1c79356b
A
4241 } */ *ap;
4242{
91447636
A
4243 vnode_t vp = ap->a_vp;
4244 struct nfsmount *nmp;
4245 struct nfsv3_pathconf pc;
4246 int error = 0, cached;
1c79356b 4247
91447636
A
4248 nmp = VFSTONFS(vnode_mount(vp));
4249 if (!nmp)
4250 return (ENXIO);
4251 if (!NFS_ISV3(vp))
4252 return (EINVAL);
1c79356b 4253
91447636
A
4254 switch (ap->a_name) {
4255 case _PC_LINK_MAX:
4256 case _PC_NAME_MAX:
4257 case _PC_CHOWN_RESTRICTED:
4258 case _PC_NO_TRUNC:
4259 case _PC_CASE_SENSITIVE:
4260 case _PC_CASE_PRESERVING:
4261 break;
4262 default:
4263 /* don't bother contacting the server if we know the answer */
4264 return (EINVAL);
4265 }
1c79356b 4266
91447636
A
4267 if (!(nmp->nm_state & NFSSTA_GOTPATHCONF)) {
4268 /* no pathconf info cached */
4269 kauth_cred_t cred = vfs_context_ucred(ap->a_context);
4270 proc_t p = vfs_context_proc(ap->a_context);
4271 error = nfs_pathconfrpc(vp, &pc, cred, p);
4272 if (error)
4273 return (error);
4274 nmp = VFSTONFS(vnode_mount(vp));
4275 if (!nmp)
4276 return (ENXIO);
4277 if (!(nmp->nm_state & NFSSTA_GOTFSINFO)) {
4278 nfs_fsinfo(nmp, vp, cred, p);
4279 nmp = VFSTONFS(vnode_mount(vp));
4280 if (!nmp)
4281 return (ENXIO);
4282 }
4283 if ((nmp->nm_state & NFSSTA_GOTFSINFO) &&
4284 (nmp->nm_fsinfo.fsproperties & NFSV3FSINFO_HOMOGENEOUS)) {
4285 /* all files have the same pathconf info, */
4286 /* so cache a copy of the results */
4287 nfs_pathconf_cache(nmp, &pc);
4288 }
4289 }
1c79356b 4290
91447636
A
4291 cached = (nmp->nm_state & NFSSTA_GOTPATHCONF);
4292
4293 switch (ap->a_name) {
4294 case _PC_LINK_MAX:
4295 *ap->a_retval = cached ? nmp->nm_fsinfo.linkmax : pc.pc_linkmax;
4296 break;
4297 case _PC_NAME_MAX:
4298 *ap->a_retval = cached ? nmp->nm_fsinfo.namemax : pc.pc_namemax;
4299 break;
4300 case _PC_CHOWN_RESTRICTED:
4301 if (cached)
4302 *ap->a_retval = (nmp->nm_fsinfo.pcflags & NFSPCINFO_CHOWN_RESTRICTED) ? 1 : 0;
4303 else
4304 *ap->a_retval = pc.pc_chownrestricted;
4305 break;
4306 case _PC_NO_TRUNC:
4307 if (cached)
4308 *ap->a_retval = (nmp->nm_fsinfo.pcflags & NFSPCINFO_NOTRUNC) ? 1 : 0;
4309 else
4310 *ap->a_retval = pc.pc_notrunc;
4311 break;
4312 case _PC_CASE_SENSITIVE:
4313 if (cached)
4314 *ap->a_retval = (nmp->nm_fsinfo.pcflags & NFSPCINFO_CASE_INSENSITIVE) ? 0 : 1;
4315 else
4316 *ap->a_retval = !pc.pc_caseinsensitive;
4317 break;
4318 case _PC_CASE_PRESERVING:
4319 if (cached)
4320 *ap->a_retval = (nmp->nm_fsinfo.pcflags & NFSPCINFO_CASE_PRESERVING) ? 1 : 0;
4321 else
4322 *ap->a_retval = pc.pc_casepreserving;
4323 break;
4324 default:
4325 error = EINVAL;
4326 }
1c79356b 4327
91447636 4328 return (error);
1c79356b
A
4329}
4330
4331/*
91447636 4332 * NFS advisory byte-level locks (client)
1c79356b
A
4333 */
4334static int
91447636
A
4335nfs_advlock(ap)
4336 struct vnop_advlock_args /* {
4337 struct vnodeop_desc *a_desc;
4338 vnode_t a_vp;
4339 caddr_t a_id;
4340 int a_op;
4341 struct flock *a_fl;
1c79356b 4342 int a_flags;
91447636 4343 vfs_context_t a_context;
1c79356b
A
4344 } */ *ap;
4345{
91447636 4346 return (nfs_dolock(ap));
1c79356b
A
4347}
4348
1c79356b 4349/*
55e303ae 4350 * write (or commit) the given NFS buffer
1c79356b
A
4351 */
4352int
55e303ae 4353nfs_buf_write(struct nfsbuf *bp)
1c79356b 4354{
55e303ae 4355 int oldflags = bp->nb_flags, rv = 0;
91447636
A
4356 vnode_t vp = bp->nb_vp;
4357 struct nfsnode *np = VTONFS(vp);
4358 kauth_cred_t cr;
4359 proc_t p = current_proc(); // XXX
55e303ae
A
4360
4361 FSDBG_TOP(553, bp, NBOFF(bp), bp->nb_flags, 0);
1c79356b 4362
91447636 4363 if (!ISSET(bp->nb_lflags, NBL_BUSY))
55e303ae 4364 panic("nfs_buf_write: buffer is not busy???");
1c79356b 4365
55e303ae
A
4366 CLR(bp->nb_flags, (NB_READ|NB_DONE|NB_ERROR|NB_DELWRI));
4367 if (ISSET(oldflags, NB_DELWRI)) {
91447636
A
4368 OSAddAtomic(-1, (SInt32*)&nfs_nbdwrite);
4369 NFSBUFCNTCHK(0);
4370 wakeup(&nfs_nbdwrite);
d52fe63f 4371 }
1c79356b 4372
55e303ae
A
4373 /* move to clean list */
4374 if (ISSET(oldflags, (NB_ASYNC|NB_DELWRI))) {
91447636 4375 lck_mtx_lock(nfs_buf_mutex);
55e303ae
A
4376 if (bp->nb_vnbufs.le_next != NFSNOLIST)
4377 LIST_REMOVE(bp, nb_vnbufs);
4378 LIST_INSERT_HEAD(&VTONFS(vp)->n_cleanblkhd, bp, nb_vnbufs);
91447636 4379 lck_mtx_unlock(nfs_buf_mutex);
1c79356b 4380 }
91447636 4381 vnode_startwrite(vp);
1c79356b 4382
55e303ae
A
4383 if (p && p->p_stats)
4384 p->p_stats->p_ru.ru_oublock++;
1c79356b
A
4385
4386 /*
55e303ae
A
4387 * For async requests when nfsiod(s) are running, queue the request by
4388 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the request.
1c79356b 4389 */
55e303ae 4390 if (ISSET(bp->nb_flags, NB_ASYNC))
91447636 4391 p = NULL;
55e303ae
A
4392 if (ISSET(bp->nb_flags, NB_READ))
4393 cr = bp->nb_rcred;
4394 else
4395 cr = bp->nb_wcred;
4396 if (!ISSET(bp->nb_flags, NB_ASYNC) || nfs_asyncio(bp, NOCRED))
4397 rv = nfs_doio(bp, cr, p);
4398
4399 if ((oldflags & NB_ASYNC) == 0) {
4400 rv = nfs_buf_iowait(bp);
4401 /* move to clean list */
4402 if (oldflags & NB_DELWRI) {
91447636 4403 lck_mtx_lock(nfs_buf_mutex);
55e303ae
A
4404 if (bp->nb_vnbufs.le_next != NFSNOLIST)
4405 LIST_REMOVE(bp, nb_vnbufs);
4406 LIST_INSERT_HEAD(&VTONFS(vp)->n_cleanblkhd, bp, nb_vnbufs);
91447636 4407 lck_mtx_unlock(nfs_buf_mutex);
1c79356b 4408 }
91447636 4409 oldflags = bp->nb_flags;
55e303ae 4410 FSDBG_BOT(553, bp, NBOFF(bp), bp->nb_flags, rv);
91447636
A
4411 if (cr) {
4412 kauth_cred_ref(cr);
4413 }
483a1d10 4414 nfs_buf_release(bp, 1);
91447636
A
4415 if (ISSET(oldflags, NB_ERROR) && !(np->n_flag & NFLUSHINPROG)) {
4416 /*
4417 * There was a write error and we need to
4418 * invalidate attrs and flush buffers in
4419 * order to sync up with the server.
4420 * (if this write was extending the file,
4421 * we may no longer know the correct size)
4422 *
4423 * But we couldn't call vinvalbuf while holding
4424 * the buffer busy. So we call vinvalbuf() after
4425 * releasing the buffer.
4426 */
4427 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, cr, p, 1);
4428 }
4429 if (cr)
4430 kauth_cred_rele(cr);
55e303ae 4431 return (rv);
1c79356b
A
4432 }
4433
55e303ae
A
4434 FSDBG_BOT(553, bp, NBOFF(bp), bp->nb_flags, rv);
4435 return (rv);
1c79356b
A
4436}
4437
1c79356b
A
4438/*
4439 * Read wrapper for special devices.
4440 */
4441static int
4442nfsspec_read(ap)
91447636
A
4443 struct vnop_read_args /* {
4444 struct vnodeop_desc *a_desc;
4445 vnode_t a_vp;
1c79356b 4446 struct uio *a_uio;
91447636
A
4447 int a_ioflag;
4448 vfs_context_t a_context;
1c79356b
A
4449 } */ *ap;
4450{
4451 register struct nfsnode *np = VTONFS(ap->a_vp);
55e303ae 4452 struct timeval now;
1c79356b
A
4453
4454 /*
4455 * Set access flag.
4456 */
4457 np->n_flag |= NACC;
55e303ae
A
4458 microtime(&now);
4459 np->n_atim.tv_sec = now.tv_sec;
4460 np->n_atim.tv_nsec = now.tv_usec * 1000;
91447636 4461 return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_read), ap));
1c79356b
A
4462}
4463
4464/*
4465 * Write wrapper for special devices.
4466 */
4467static int
4468nfsspec_write(ap)
91447636
A
4469 struct vnop_write_args /* {
4470 struct vnodeop_desc *a_desc;
4471 vnode_t a_vp;
1c79356b 4472 struct uio *a_uio;
91447636
A
4473 int a_ioflag;
4474 vfs_context_t a_context;
1c79356b
A
4475 } */ *ap;
4476{
4477 register struct nfsnode *np = VTONFS(ap->a_vp);
55e303ae 4478 struct timeval now;
1c79356b
A
4479
4480 /*
4481 * Set update flag.
4482 */
4483 np->n_flag |= NUPD;
55e303ae
A
4484 microtime(&now);
4485 np->n_mtim.tv_sec = now.tv_sec;
4486 np->n_mtim.tv_nsec = now.tv_usec * 1000;
91447636 4487 return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_write), ap));
1c79356b
A
4488}
4489
4490/*
4491 * Close wrapper for special devices.
4492 *
4493 * Update the times on the nfsnode then do device close.
4494 */
4495static int
4496nfsspec_close(ap)
91447636
A
4497 struct vnop_close_args /* {
4498 struct vnodeop_desc *a_desc;
4499 vnode_t a_vp;
4500 int a_fflag;
4501 vfs_context_t a_context;
1c79356b
A
4502 } */ *ap;
4503{
91447636
A
4504 vnode_t vp = ap->a_vp;
4505 struct nfsnode *np = VTONFS(vp);
4506 struct vnode_attr vattr;
4507 mount_t mp;
1c79356b
A
4508
4509 if (np->n_flag & (NACC | NUPD)) {
4510 np->n_flag |= NCHG;
91447636
A
4511 if (!vnode_isinuse(vp, 1) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) {
4512 VATTR_INIT(&vattr);
4513 if (np->n_flag & NACC) {
4514 vattr.va_access_time = np->n_atim;
4515 VATTR_SET_ACTIVE(&vattr, va_access_time);
4516 }
4517 if (np->n_flag & NUPD) {
4518 vattr.va_modify_time = np->n_mtim;
4519 VATTR_SET_ACTIVE(&vattr, va_modify_time);
4520 }
4521 vnode_setattr(vp, &vattr, ap->a_context);
1c79356b
A
4522 }
4523 }
91447636 4524 return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_close), ap));
1c79356b
A
4525}
4526
91447636
A
4527extern vnop_t **fifo_vnodeop_p;
4528
1c79356b
A
4529/*
4530 * Read wrapper for fifos.
4531 */
4532static int
4533nfsfifo_read(ap)
91447636
A
4534 struct vnop_read_args /* {
4535 struct vnodeop_desc *a_desc;
4536 vnode_t a_vp;
1c79356b 4537 struct uio *a_uio;
91447636
A
4538 int a_ioflag;
4539 vfs_context_t a_context;
1c79356b
A
4540 } */ *ap;
4541{
1c79356b 4542 register struct nfsnode *np = VTONFS(ap->a_vp);
55e303ae 4543 struct timeval now;
1c79356b
A
4544
4545 /*
4546 * Set access flag.
4547 */
4548 np->n_flag |= NACC;
55e303ae
A
4549 microtime(&now);
4550 np->n_atim.tv_sec = now.tv_sec;
4551 np->n_atim.tv_nsec = now.tv_usec * 1000;
91447636 4552 return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_read), ap));
1c79356b
A
4553}
4554
4555/*
4556 * Write wrapper for fifos.
4557 */
4558static int
4559nfsfifo_write(ap)
91447636
A
4560 struct vnop_write_args /* {
4561 struct vnodeop_desc *a_desc;
4562 vnode_t a_vp;
1c79356b 4563 struct uio *a_uio;
91447636
A
4564 int a_ioflag;
4565 vfs_context_t a_context;
1c79356b
A
4566 } */ *ap;
4567{
1c79356b 4568 register struct nfsnode *np = VTONFS(ap->a_vp);
55e303ae 4569 struct timeval now;
1c79356b
A
4570
4571 /*
4572 * Set update flag.
4573 */
4574 np->n_flag |= NUPD;
55e303ae
A
4575 microtime(&now);
4576 np->n_mtim.tv_sec = now.tv_sec;
4577 np->n_mtim.tv_nsec = now.tv_usec * 1000;
91447636 4578 return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_write), ap));
1c79356b
A
4579}
4580
4581/*
4582 * Close wrapper for fifos.
4583 *
4584 * Update the times on the nfsnode then do fifo close.
4585 */
4586static int
4587nfsfifo_close(ap)
91447636
A
4588 struct vnop_close_args /* {
4589 struct vnodeop_desc *a_desc;
4590 vnode_t a_vp;
4591 int a_fflag;
4592 vfs_context_t a_context;
1c79356b
A
4593 } */ *ap;
4594{
91447636
A
4595 vnode_t vp = ap->a_vp;
4596 struct nfsnode *np = VTONFS(vp);
4597 struct vnode_attr vattr;
55e303ae 4598 struct timeval now;
91447636 4599 mount_t mp;
1c79356b
A
4600
4601 if (np->n_flag & (NACC | NUPD)) {
55e303ae 4602 microtime(&now);
1c79356b 4603 if (np->n_flag & NACC) {
55e303ae
A
4604 np->n_atim.tv_sec = now.tv_sec;
4605 np->n_atim.tv_nsec = now.tv_usec * 1000;
1c79356b
A
4606 }
4607 if (np->n_flag & NUPD) {
55e303ae
A
4608 np->n_mtim.tv_sec = now.tv_sec;
4609 np->n_mtim.tv_nsec = now.tv_usec * 1000;
1c79356b
A
4610 }
4611 np->n_flag |= NCHG;
91447636
A
4612 if (!vnode_isinuse(vp, 1) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) {
4613 VATTR_INIT(&vattr);
4614 if (np->n_flag & NACC) {
4615 vattr.va_access_time = np->n_atim;
4616 VATTR_SET_ACTIVE(&vattr, va_access_time);
4617 }
4618 if (np->n_flag & NUPD) {
4619 vattr.va_modify_time = np->n_mtim;
4620 VATTR_SET_ACTIVE(&vattr, va_modify_time);
4621 }
4622 vnode_setattr(vp, &vattr, ap->a_context);
1c79356b
A
4623 }
4624 }
91447636 4625 return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_close), ap));
1c79356b
A
4626}
4627
91447636 4628/*ARGSUSED*/
1c79356b 4629static int
91447636
A
4630nfs_ioctl(
4631 __unused struct vnop_ioctl_args /* {
4632 struct vnodeop_desc *a_desc;
4633 vnode_t a_vp;
4634 u_long a_command;
4635 caddr_t a_data;
4636 int a_fflag;
4637 kauth_cred_t a_cred;
4638 proc_t a_p;
4639 } */ *ap)
1c79356b
A
4640{
4641
4642 /*
4643 * XXX we were once bogusly enoictl() which returned this (ENOTTY).
4644 * Probably we should return ENODEV.
4645 */
4646 return (ENOTTY);
4647}
4648
91447636 4649/*ARGSUSED*/
1c79356b 4650static int
91447636
A
4651nfs_select(
4652 __unused struct vnop_select_args /* {
4653 struct vnodeop_desc *a_desc;
4654 vnode_t a_vp;
4655 int a_which;
4656 int a_fflags;
4657 kauth_cred_t a_cred;
4658 void *a_wql;
4659 proc_t a_p;
4660 } */ *ap)
1c79356b
A
4661{
4662
4663 /*
4664 * We were once bogusly seltrue() which returns 1. Is this right?
4665 */
4666 return (1);
4667}
4668
1c79356b
A
4669/*
4670 * Vnode op for pagein using getblk_pages
4671 * derived from nfs_bioread()
4672 * No read aheads are started from pagein operation
4673 */
4674static int
4675nfs_pagein(ap)
91447636
A
4676 struct vnop_pagein_args /* {
4677 struct vnodeop_desc *a_desc;
4678 vnode_t a_vp;
4679 upl_t a_pl;
4680 vm_offset_t a_pl_offset;
4681 off_t a_f_offset;
4682 size_t a_size;
4683 int a_flags;
4684 vfs_context_t a_context;
1c79356b
A
4685 } */ *ap;
4686{
91447636 4687 vnode_t vp = ap->a_vp;
1c79356b
A
4688 upl_t pl = ap->a_pl;
4689 size_t size= ap->a_size;
4690 off_t f_offset = ap->a_f_offset;
4691 vm_offset_t pl_offset = ap->a_pl_offset;
4692 int flags = ap->a_flags;
91447636
A
4693 kauth_cred_t cred;
4694 proc_t p;
55e303ae
A
4695 struct nfsnode *np = VTONFS(vp);
4696 int biosize, xsize, iosize;
55e303ae 4697 struct nfsmount *nmp;
1c79356b
A
4698 int error = 0;
4699 vm_offset_t ioaddr;
4700 struct uio auio;
91447636 4701 struct iovec_32 aiov;
1c79356b 4702 struct uio * uio = &auio;
fa4905b1 4703 int nofreeupl = flags & UPL_NOCOMMIT;
55e303ae 4704 upl_page_info_t *plinfo;
1c79356b 4705
55e303ae 4706 FSDBG(322, vp, f_offset, size, flags);
fa4905b1
A
4707 if (pl == (upl_t)NULL)
4708 panic("nfs_pagein: no upl");
1c79356b
A
4709
4710 if (UBCINVALID(vp)) {
fa4905b1
A
4711 printf("nfs_pagein: invalid vnode 0x%x", (int)vp);
4712 if (!nofreeupl)
91447636 4713 (void) ubc_upl_abort(pl, 0);
1c79356b
A
4714 return (EPERM);
4715 }
1c79356b 4716 UBCINFOCHECK("nfs_pagein", vp);
1c79356b 4717
fa4905b1
A
4718 if (size <= 0) {
4719 printf("nfs_pagein: invalid size %d", size);
4720 if (!nofreeupl)
91447636 4721 (void) ubc_upl_abort(pl, 0);
1c79356b 4722 return (EINVAL);
fa4905b1 4723 }
91447636 4724 if (f_offset < 0 || f_offset >= (off_t)np->n_size || (f_offset & PAGE_MASK_64)) {
fa4905b1 4725 if (!nofreeupl)
0b4e3aa0 4726 ubc_upl_abort_range(pl, pl_offset, size,
1c79356b
A
4727 UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY);
4728 return (EINVAL);
4729 }
91447636 4730
fa4905b1
A
4731 cred = ubc_getcred(vp);
4732 if (cred == NOCRED)
91447636
A
4733 cred = vfs_context_ucred(ap->a_context);
4734 p = vfs_context_proc(ap->a_context);
1c79356b 4735
1c79356b 4736 auio.uio_offset = f_offset;
91447636 4737#if 1 /* LP64todo - can't use new segment flags until the drivers are ready */
1c79356b 4738 auio.uio_segflg = UIO_SYSSPACE;
91447636
A
4739#else
4740 auio.uio_segflg = UIO_SYSSPACE32;
4741#endif
1c79356b 4742 auio.uio_rw = UIO_READ;
91447636 4743 auio.uio_procp = p;
1c79356b 4744
91447636 4745 nmp = VFSTONFS(vnode_mount(vp));
55e303ae
A
4746 if (!nmp) {
4747 if (!nofreeupl)
4748 ubc_upl_abort_range(pl, pl_offset, size,
4749 UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY);
4750 return (ENXIO);
4751 }
4752 if ((nmp->nm_flag & NFSMNT_NFSV3) && !(nmp->nm_state & NFSSTA_GOTFSINFO))
8f6c56a5
A
4753 (void)nfs_fsinfo(nmp, vp, cred, p);
4754 biosize = vfs_statfs(vnode_mount(vp))->f_iosize;
1c79356b 4755
55e303ae 4756 plinfo = ubc_upl_pageinfo(pl);
0b4e3aa0 4757 ubc_upl_map(pl, &ioaddr);
1c79356b
A
4758 ioaddr += pl_offset;
4759 xsize = size;
4760
4761 do {
55e303ae
A
4762 /*
4763 * It would be nice to be able to issue all these requests
4764 * in parallel instead of waiting for each one to complete
4765 * before sending the next one.
4766 * XXX Should we align these requests to block boundaries?
4767 */
91447636 4768 iosize = min(biosize, xsize);
9bccf70c 4769 aiov.iov_len = iosize;
91447636
A
4770 aiov.iov_base = (uintptr_t)ioaddr;
4771 auio.uio_iovs.iov32p = &aiov;
55e303ae 4772 auio.uio_iovcnt = 1;
91447636 4773 uio_uio_resid_set(&auio, iosize);
1c79356b 4774
91447636 4775 FSDBG(322, uio->uio_offset, uio_uio_resid(uio), ioaddr, xsize);
1c79356b
A
4776 /*
4777 * With UBC we get here only when the file data is not in the VM
4778 * page cache, so go ahead and read in.
4779 */
91447636
A
4780#ifdef UPL_DEBUG
4781 upl_ubc_alias_set(pl, current_thread(), 2);
4782#endif /* UPL_DEBUG */
4783 OSAddAtomic(1, (SInt32*)&nfsstats.pageins);
9bccf70c 4784
91447636 4785 error = nfs_readrpc(vp, uio, cred, p);
1c79356b
A
4786
4787 if (!error) {
91447636 4788 if (uio_uio_resid(uio)) {
1c79356b 4789 /*
fa4905b1
A
4790 * If uio_resid > 0, there is a hole in the file
4791 * and no writes after the hole have been pushed
4792 * to the server yet... or we're at the EOF
1c79356b
A
4793 * Just zero fill the rest of the valid area.
4794 */
91447636
A
4795 // LP64todo - fix this
4796 int zcnt = uio_uio_resid(uio);
9bccf70c 4797 int zoff = iosize - zcnt;
1c79356b
A
4798 bzero((char *)ioaddr + zoff, zcnt);
4799
fa4905b1 4800 FSDBG(324, uio->uio_offset, zoff, zcnt, ioaddr);
1c79356b
A
4801 uio->uio_offset += zcnt;
4802 }
9bccf70c
A
4803 ioaddr += iosize;
4804 xsize -= iosize;
91447636
A
4805 } else {
4806 FSDBG(322, uio->uio_offset, uio_uio_resid(uio), error, -1);
1c79356b
A
4807 }
4808
91447636 4809 nmp = VFSTONFS(vnode_mount(vp));
1c79356b
A
4810 } while (error == 0 && xsize > 0);
4811
0b4e3aa0 4812 ubc_upl_unmap(pl);
1c79356b 4813
fa4905b1 4814 if (!nofreeupl) {
1c79356b 4815 if (error)
0b4e3aa0 4816 ubc_upl_abort_range(pl, pl_offset, size,
fa4905b1
A
4817 UPL_ABORT_ERROR |
4818 UPL_ABORT_FREE_ON_EMPTY);
1c79356b 4819 else
0b4e3aa0 4820 ubc_upl_commit_range(pl, pl_offset, size,
fa4905b1
A
4821 UPL_COMMIT_CLEAR_DIRTY |
4822 UPL_COMMIT_FREE_ON_EMPTY);
1c79356b 4823 }
1c79356b
A
4824 return (error);
4825}
4826
0b4e3aa0 4827
1c79356b
A
4828/*
4829 * Vnode op for pageout using UPL
4830 * Derived from nfs_write()
4831 * File size changes are not permitted in pageout.
4832 */
4833static int
4834nfs_pageout(ap)
91447636
A
4835 struct vnop_pageout_args /* {
4836 struct vnodeop_desc *a_desc;
4837 vnode_t a_vp;
4838 upl_t a_pl;
4839 vm_offset_t a_pl_offset;
4840 off_t a_f_offset;
4841 size_t a_size;
4842 int a_flags;
4843 vfs_context_t a_context;
1c79356b
A
4844 } */ *ap;
4845{
91447636 4846 vnode_t vp = ap->a_vp;
1c79356b
A
4847 upl_t pl = ap->a_pl;
4848 size_t size= ap->a_size;
4849 off_t f_offset = ap->a_f_offset;
4850 vm_offset_t pl_offset = ap->a_pl_offset;
4851 int flags = ap->a_flags;
1c79356b 4852 struct nfsnode *np = VTONFS(vp);
91447636
A
4853 kauth_cred_t cred;
4854 proc_t p;
55e303ae 4855 struct nfsbuf *bp;
91447636
A
4856 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4857 daddr64_t lbn;
8f6c56a5 4858 int error = 0, iomode;
1c79356b
A
4859 off_t off;
4860 vm_offset_t ioaddr;
4861 struct uio auio;
91447636 4862 struct iovec_32 aiov;
fa4905b1 4863 int nofreeupl = flags & UPL_NOCOMMIT;
91447636 4864 size_t biosize, iosize, pgsize, xsize;
1c79356b 4865
fa4905b1
A
4866 FSDBG(323, f_offset, size, pl, pl_offset);
4867
4868 if (pl == (upl_t)NULL)
4869 panic("nfs_pageout: no upl");
1c79356b
A
4870
4871 if (UBCINVALID(vp)) {
fa4905b1
A
4872 printf("nfs_pageout: invalid vnode 0x%x", (int)vp);
4873 if (!nofreeupl)
55e303ae 4874 ubc_upl_abort(pl, 0);
1c79356b
A
4875 return (EIO);
4876 }
4877 UBCINFOCHECK("nfs_pageout", vp);
4878
fa4905b1
A
4879 if (size <= 0) {
4880 printf("nfs_pageout: invalid size %d", size);
4881 if (!nofreeupl)
55e303ae 4882 ubc_upl_abort(pl, 0);
1c79356b 4883 return (EINVAL);
1c79356b
A
4884 }
4885
55e303ae
A
4886 if (!nmp) {
4887 if (!nofreeupl)
4888 ubc_upl_abort(pl, UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY);
4889 return (ENXIO);
4890 }
8f6c56a5 4891 biosize = vfs_statfs(vnode_mount(vp))->f_iosize;
1c79356b 4892
1c79356b 4893 /*
55e303ae
A
4894 * Check to see whether the buffer is incore.
4895 * If incore and not busy, invalidate it from the cache.
1c79356b 4896 */
55e303ae
A
4897 for (iosize = 0; iosize < size; iosize += xsize) {
4898 off = f_offset + iosize;
4899 /* need make sure we do things on block boundaries */
4900 xsize = biosize - (off % biosize);
4901 if (off + xsize > f_offset + size)
4902 xsize = f_offset + size - off;
4903 lbn = ubc_offtoblk(vp, off);
91447636
A
4904 lck_mtx_lock(nfs_buf_mutex);
4905 if ((bp = nfs_buf_incore(vp, lbn))) {
4906 FSDBG(323, off, bp, bp->nb_lflags, bp->nb_flags);
4907 if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) {
4908 lck_mtx_unlock(nfs_buf_mutex);
fa4905b1
A
4909 /* no panic. just tell vm we are busy */
4910 if (!nofreeupl)
55e303ae
A
4911 ubc_upl_abort(pl, 0);
4912 return (EBUSY);
4913 }
4914 if (bp->nb_dirtyend > 0) {
4915 /*
91447636
A
4916 * if there's a dirty range in the buffer, check
4917 * to see if it extends beyond the pageout region
55e303ae
A
4918 *
4919 * if the dirty region lies completely within the
4920 * pageout region, we just invalidate the buffer
4921 * because it's all being written out now anyway.
4922 *
4923 * if any of the dirty region lies outside the
4924 * pageout region, we'll try to clip the dirty
4925 * region to eliminate the portion that's being
4926 * paged out. If that's not possible, because
4927 * the dirty region extends before and after the
4928 * pageout region, then we'll just return EBUSY.
4929 */
4930 off_t boff, start, end;
4931 boff = NBOFF(bp);
4932 start = off;
4933 end = off + xsize;
4934 /* clip end to EOF */
91447636 4935 if (end > (off_t)np->n_size)
55e303ae
A
4936 end = np->n_size;
4937 start -= boff;
4938 end -= boff;
4939 if ((bp->nb_dirtyoff < start) &&
4940 (bp->nb_dirtyend > end)) {
4941 /* not gonna be able to clip the dirty region */
4942 FSDBG(323, vp, bp, 0xd00deebc, EBUSY);
91447636
A
4943 nfs_buf_drop(bp);
4944 lck_mtx_unlock(nfs_buf_mutex);
55e303ae
A
4945 if (!nofreeupl)
4946 ubc_upl_abort(pl, 0);
4947 return (EBUSY);
4948 }
4949 if ((bp->nb_dirtyoff < start) ||
4950 (bp->nb_dirtyend > end)) {
4951 /* clip dirty region, if necessary */
4952 if (bp->nb_dirtyoff < start)
4953 bp->nb_dirtyend = min(bp->nb_dirtyend, start);
4954 if (bp->nb_dirtyend > end)
4955 bp->nb_dirtyoff = max(bp->nb_dirtyoff, end);
4956 FSDBG(323, bp, bp->nb_dirtyoff, bp->nb_dirtyend, 0xd00dee00);
4957 /* we're leaving this block dirty */
91447636
A
4958 nfs_buf_drop(bp);
4959 lck_mtx_unlock(nfs_buf_mutex);
55e303ae
A
4960 continue;
4961 }
4962 }
4963 nfs_buf_remfree(bp);
91447636
A
4964 lck_mtx_unlock(nfs_buf_mutex);
4965 SET(bp->nb_flags, NB_INVAL);
55e303ae
A
4966 if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
4967 CLR(bp->nb_flags, NB_NEEDCOMMIT);
4968 np->n_needcommitcnt--;
4969 CHECK_NEEDCOMMITCNT(np);
fa4905b1 4970 }
483a1d10 4971 nfs_buf_release(bp, 1);
91447636
A
4972 } else {
4973 lck_mtx_unlock(nfs_buf_mutex);
1c79356b 4974 }
1c79356b
A
4975 }
4976
4977 cred = ubc_getcred(vp);
4978 if (cred == NOCRED)
91447636
A
4979 cred = vfs_context_ucred(ap->a_context);
4980 p = vfs_context_proc(ap->a_context);
1c79356b
A
4981
4982 if (np->n_flag & NWRITEERR) {
4983 np->n_flag &= ~NWRITEERR;
fa4905b1
A
4984 if (!nofreeupl)
4985 ubc_upl_abort_range(pl, pl_offset, size,
4986 UPL_ABORT_FREE_ON_EMPTY);
1c79356b
A
4987 return (np->n_error);
4988 }
91447636
A
4989 if ((nmp->nm_flag & NFSMNT_NFSV3) && !(nmp->nm_state & NFSSTA_GOTFSINFO))
4990 nfs_fsinfo(nmp, vp, cred, p);
1c79356b 4991
91447636 4992 if (f_offset < 0 || f_offset >= (off_t)np->n_size ||
55e303ae 4993 f_offset & PAGE_MASK_64 || size & PAGE_MASK_64) {
fa4905b1
A
4994 if (!nofreeupl)
4995 ubc_upl_abort_range(pl, pl_offset, size,
4996 UPL_ABORT_FREE_ON_EMPTY);
1c79356b
A
4997 return (EINVAL);
4998 }
4999
0b4e3aa0 5000 ubc_upl_map(pl, &ioaddr);
55e303ae 5001 ioaddr += pl_offset;
1c79356b 5002
91447636 5003 if ((u_quad_t)f_offset + size > np->n_size)
55e303ae 5004 xsize = np->n_size - f_offset;
1c79356b 5005 else
55e303ae 5006 xsize = size;
1c79356b 5007
55e303ae 5008 pgsize = round_page_64(xsize);
1c79356b 5009 if (size > pgsize) {
fa4905b1
A
5010 if (!nofreeupl)
5011 ubc_upl_abort_range(pl, pl_offset + pgsize,
5012 size - pgsize,
5013 UPL_ABORT_FREE_ON_EMPTY);
1c79356b 5014 }
1c79356b 5015
1c79356b
A
5016 /*
5017 * check for partial page and clear the
5018 * contents past end of the file before
5019 * releasing it in the VM page cache
5020 */
91447636 5021 if ((u_quad_t)f_offset < np->n_size && (u_quad_t)f_offset + size > np->n_size) {
1c79356b 5022 size_t io = np->n_size - f_offset;
55e303ae 5023 bzero((caddr_t)(ioaddr + io), size - io);
fa4905b1 5024 FSDBG(321, np->n_size, f_offset, f_offset + io, size - io);
1c79356b
A
5025 }
5026
55e303ae 5027 auio.uio_offset = f_offset;
91447636 5028#if 1 /* LP64todo - can't use new segment flags until the drivers are ready */
55e303ae 5029 auio.uio_segflg = UIO_SYSSPACE;
91447636
A
5030#else
5031 auio.uio_segflg = UIO_SYSSPACE32;
5032#endif
55e303ae 5033 auio.uio_rw = UIO_READ;
91447636 5034 auio.uio_procp = p;
55e303ae 5035
1c79356b 5036 do {
55e303ae
A
5037 /*
5038 * It would be nice to be able to issue all these requests
5039 * in parallel instead of waiting for each one to complete
5040 * before sending the next one.
5041 * XXX Should we align these requests to block boundaries?
5042 */
5043 iosize = min(biosize, xsize);
91447636 5044 uio_uio_resid_set(&auio, iosize);
55e303ae 5045 aiov.iov_len = iosize;
91447636
A
5046 aiov.iov_base = (uintptr_t)ioaddr;
5047 auio.uio_iovs.iov32p = &aiov;
55e303ae
A
5048 auio.uio_iovcnt = 1;
5049
91447636
A
5050 FSDBG(323, auio.uio_offset, uio_uio_resid(&auio), ioaddr, xsize);
5051 OSAddAtomic(1, (SInt32*)&nfsstats.pageouts);
5052
5053 vnode_startwrite(vp);
55e303ae 5054
fa4905b1 5055 /* NMODIFIED would be set here if doing unstable writes */
1c79356b 5056 iomode = NFSV3WRITE_FILESYNC;
8f6c56a5 5057 error = nfs_writerpc(vp, &auio, cred, p, &iomode, NULL);
91447636 5058 vnode_writedone(vp);
1c79356b
A
5059 if (error)
5060 goto cleanup;
55e303ae
A
5061 /* Note: no need to check uio_resid, because */
5062 /* it'll only be set if there was an error. */
5063 ioaddr += iosize;
5064 xsize -= iosize;
5065 } while (xsize > 0);
1c79356b
A
5066
5067cleanup:
0b4e3aa0
A
5068 ubc_upl_unmap(pl);
5069 /*
5070 * We've had several different solutions on what to do when the pageout
5071 * gets an error. If we don't handle it, and return an error to the
5072 * caller, vm, it will retry . This can end in endless looping
5073 * between vm and here doing retries of the same page. Doing a dump
5074 * back to vm, will get it out of vm's knowledge and we lose whatever
5075 * data existed. This is risky, but in some cases necessary. For
5076 * example, the initial fix here was to do that for ESTALE. In that case
5077 * the server is telling us that the file is no longer the same. We
5078 * would not want to keep paging out to that. We also saw some 151
5079 * errors from Auspex server and NFSv3 can return errors higher than
fa4905b1
A
5080 * ELAST. Those along with NFS known server errors we will "dump" from
5081 * vm. Errors we don't expect to occur, we dump and log for further
0b4e3aa0
A
5082 * analysis. Errors that could be transient, networking ones,
5083 * we let vm "retry". Lastly, errors that we retry, but may have potential
5084 * to storm the network, we "retrywithsleep". "sever" will be used in
5085 * in the future to dump all pages of object for cases like ESTALE.
5086 * All this is the basis for the states returned and first guesses on
5087 * error handling. Tweaking expected as more statistics are gathered.
5088 * Note, in the long run we may need another more robust solution to
5089 * have some kind of persistant store when the vm cannot dump nor keep
fa4905b1 5090 * retrying as a solution, but this would be a file architectural change
0b4e3aa0
A
5091 */
5092
fa4905b1 5093 if (!nofreeupl) { /* otherwise stacked file system has to handle this */
0b4e3aa0 5094 if (error) {
91447636 5095 int abortflags = 0;
0b4e3aa0
A
5096 short action = nfs_pageouterrorhandler(error);
5097
5098 switch (action) {
5099 case DUMP:
5100 abortflags = UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY;
5101 break;
5102 case DUMPANDLOG:
5103 abortflags = UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY;
fa4905b1
A
5104 if (error <= ELAST &&
5105 (errorcount[error] % 100 == 0))
0b4e3aa0
A
5106 printf("nfs_pageout: unexpected error %d. dumping vm page\n", error);
5107 errorcount[error]++;
5108 break;
5109 case RETRY:
5110 abortflags = UPL_ABORT_FREE_ON_EMPTY;
5111 break;
5112 case RETRYWITHSLEEP:
5113 abortflags = UPL_ABORT_FREE_ON_EMPTY;
fa4905b1 5114 /* pri unused. PSOCK for placeholder. */
91447636 5115 tsleep(&lbolt, PSOCK, "nfspageout", 0);
0b4e3aa0
A
5116 break;
5117 case SEVER: /* not implemented */
5118 default:
5119 printf("nfs_pageout: action %d not expected\n", action);
5120 break;
5121 }
5122
5123 ubc_upl_abort_range(pl, pl_offset, size, abortflags);
5124 /* return error in all cases above */
5125
5126 } else
5127 ubc_upl_commit_range(pl, pl_offset, pgsize,
fa4905b1
A
5128 UPL_COMMIT_CLEAR_DIRTY |
5129 UPL_COMMIT_FREE_ON_EMPTY);
1c79356b 5130 }
1c79356b
A
5131 return (error);
5132}
5133
5134/* Blktooff derives file offset given a logical block number */
5135static int
5136nfs_blktooff(ap)
91447636
A
5137 struct vnop_blktooff_args /* {
5138 struct vnodeop_desc *a_desc;
5139 vnode_t a_vp;
5140 daddr64_t a_lblkno;
5141 off_t *a_offset;
1c79356b
A
5142 } */ *ap;
5143{
5144 int biosize;
91447636 5145 vnode_t vp = ap->a_vp;
8f6c56a5 5146 mount_t mp = vnode_mount(vp);
1c79356b 5147
8f6c56a5 5148 if (!mp)
55e303ae 5149 return (ENXIO);
8f6c56a5
A
5150
5151 biosize = vfs_statfs(mp)->f_iosize;
1c79356b 5152
91447636 5153 *ap->a_offset = (off_t)(ap->a_lblkno * biosize);
1c79356b
A
5154
5155 return (0);
5156}
5157
1c79356b
A
5158static int
5159nfs_offtoblk(ap)
91447636
A
5160 struct vnop_offtoblk_args /* {
5161 struct vnodeop_desc *a_desc;
5162 vnode_t a_vp;
5163 off_t a_offset;
5164 daddr64_t *a_lblkno;
1c79356b
A
5165 } */ *ap;
5166{
5167 int biosize;
91447636 5168 vnode_t vp = ap->a_vp;
8f6c56a5 5169 mount_t mp = vnode_mount(vp);
1c79356b 5170
8f6c56a5 5171 if (!mp)
55e303ae 5172 return (ENXIO);
8f6c56a5
A
5173
5174 biosize = vfs_statfs(mp)->f_iosize;
1c79356b 5175
91447636 5176 *ap->a_lblkno = (daddr64_t)(ap->a_offset / biosize);
1c79356b
A
5177
5178 return (0);
5179}
91447636 5180