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