]> git.saurik.com Git - apple/xnu.git/blame - bsd/nfs/nfs_vnops.c
xnu-792.24.17.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_vnops.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
6601e61a 4 * @APPLE_LICENSE_HEADER_START@
1c79356b 5 *
6601e61a
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.
8f6c56a5 11 *
6601e61a
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
8f6c56a5
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
6601e61a
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.
8f6c56a5 19 *
6601e61a 20 * @APPLE_LICENSE_HEADER_END@
1c79356b
A
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;
6601e61a 1104 int biosize, neweofoff, mustwrite;
55e303ae 1105 struct nfsbuf *bp;
fa4905b1 1106
6601e61a 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);
6601e61a 1145 if (bp->nb_wcred == NOCRED) {
91447636
A
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,
8f6c56a5 1769 uint64_t *wverfp)
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;
8f6c56a5 1779 u_int64_t xid, wverf;
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);
91447636
A
1791 // LP64todo - fix this
1792 tsiz = uio_uio_resid(uiop);
55e303ae 1793 if (((u_int64_t)uiop->uio_offset + (unsigned int)tsiz > 0xffffffff) && !v3) {
91447636 1794 FSDBG_BOT(537, vp, uiop->uio_offset, uio_uio_resid(uiop), EFBIG);
1c79356b 1795 return (EFBIG);
55e303ae 1796 }
1c79356b 1797 while (tsiz > 0) {
91447636 1798 nmp = VFSTONFS(vnode_mount(vp));
55e303ae
A
1799 if (!nmp) {
1800 error = ENXIO;
1801 break;
1802 }
1c79356b 1803 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
91447636
A
1804 nfsm_reqhead(NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1805 if (error)
1806 break;
1807 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_WRITE]);
1c79356b
A
1808 nfsm_fhtom(vp, v3);
1809 if (v3) {
1810 nfsm_build(tl, u_long *, 5 * NFSX_UNSIGNED);
1811 txdr_hyper(&uiop->uio_offset, tl);
1812 tl += 2;
1813 *tl++ = txdr_unsigned(len);
1814 *tl++ = txdr_unsigned(*iomode);
1815 } else {
1816 nfsm_build(tl, u_long *, 4 * NFSX_UNSIGNED);
1817 *++tl = txdr_unsigned(uiop->uio_offset);
1818 tl += 2;
1819 }
1820 *tl = txdr_unsigned(len);
55e303ae 1821 FSDBG(537, vp, uiop->uio_offset, len, 0);
1c79356b 1822 nfsm_uiotom(uiop, len);
91447636
A
1823 nfsm_request(vp, NFSPROC_WRITE, p, cred, &xid);
1824 nmp = VFSTONFS(vnode_mount(vp));
55e303ae
A
1825 if (!nmp)
1826 error = ENXIO;
1c79356b 1827 if (v3) {
e5568f75 1828 if (mrep) {
91447636
A
1829 struct timespec premtime;
1830 nfsm_wcc_data(vp, &premtime, wccpostattr, &xid);
1831 if (nfstimespeccmp(&VTONFS(vp)->n_mtime, &premtime, ==))
483a1d10 1832 updatemtime = 1;
e5568f75 1833 }
1c79356b
A
1834 if (!error) {
1835 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED +
1836 NFSX_V3WRITEVERF);
1837 rlen = fxdr_unsigned(int, *tl++);
1838 if (rlen <= 0) {
1839 error = NFSERR_IO;
1840 break;
1841 } else if (rlen < len) {
1842 backup = len - rlen;
91447636
A
1843 uio_iov_base_add(uiop, -backup);
1844 uio_iov_len_add(uiop, backup);
1c79356b 1845 uiop->uio_offset -= backup;
91447636 1846 uio_uio_resid_add(uiop, backup);
1c79356b
A
1847 len = rlen;
1848 }
1849 commit = fxdr_unsigned(int, *tl++);
1850
1851 /*
1852 * Return the lowest committment level
1853 * obtained by any of the RPCs.
1854 */
1855 if (committed == NFSV3WRITE_FILESYNC)
1856 committed = commit;
1857 else if (committed == NFSV3WRITE_DATASYNC &&
1858 commit == NFSV3WRITE_UNSTABLE)
1859 committed = commit;
8f6c56a5
A
1860 fxdr_hyper(tl, &wverf);
1861 if (wverfp)
1862 *wverfp = wverf;
55e303ae 1863 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
8f6c56a5 1864 nmp->nm_verf = wverf;
55e303ae 1865 nmp->nm_state |= NFSSTA_HASWRITEVERF;
8f6c56a5
A
1866 } else if (wverf != nmp->nm_verf) {
1867 nmp->nm_verf = wverf;
1c79356b
A
1868 }
1869 }
e5568f75
A
1870 } else {
1871 if (mrep) {
91447636 1872 nfsm_loadattr(vp, v3, NULL, &xid);
e5568f75
A
1873 }
1874 }
fa4905b1 1875
483a1d10 1876 if (updatemtime)
91447636
A
1877 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.nva_mtime;
1878 mbuf_freem(mrep);
fa4905b1
A
1879 /*
1880 * we seem to have a case where we end up looping on shutdown
1881 * and taking down nfs servers. For V3, error cases, there is
1882 * no way to terminate loop, if the len was 0, meaning,
1883 * nmp->nm_wsize was trashed. FreeBSD has this fix in it.
1884 * Let's try it.
1885 */
1886 if (error)
1887 break;
1888 tsiz -= len;
1c79356b
A
1889 }
1890nfsmout:
91447636 1891 if ((mp = vnode_mount(vp)) && (vfs_flags(mp) & MNT_ASYNC))
1c79356b
A
1892 committed = NFSV3WRITE_FILESYNC;
1893 *iomode = committed;
1894 if (error)
91447636
A
1895 uio_uio_resid_set(uiop, tsiz);
1896 FSDBG_BOT(537, vp, committed, uio_uio_resid(uiop), error);
1c79356b
A
1897 return (error);
1898}
1899
1900/*
1901 * nfs mknod rpc
1902 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1903 * mode set to specify the file type and the size field for rdev.
1904 */
1905static int
91447636
A
1906nfs_mknodrpc(
1907 vnode_t dvp,
1908 vnode_t *vpp,
1909 struct componentname *cnp,
1910 struct vnode_attr *vap,
1911 kauth_cred_t cred,
1912 proc_t p)
1c79356b
A
1913{
1914 register struct nfsv2_sattr *sp;
1c79356b
A
1915 register u_long *tl;
1916 register caddr_t cp;
1917 register long t1, t2;
91447636 1918 vnode_t newvp = (vnode_t)0;
1c79356b 1919 struct nfsnode *np = (struct nfsnode *)0;
91447636 1920 struct nfs_vattr nvattr;
1c79356b
A
1921 char *cp2;
1922 caddr_t bpos, dpos;
483a1d10 1923 int error = 0, wccpostattr = 0, gotvp = 0;
91447636
A
1924 struct timespec premtime = { 0, 0 };
1925 mbuf_t mreq, mrep, md, mb, mb2;
1c79356b 1926 u_long rdev;
fa4905b1 1927 u_int64_t xid;
1c79356b 1928 int v3 = NFS_ISV3(dvp);
91447636 1929 int gotuid, gotgid;
1c79356b 1930
91447636
A
1931 if (!VATTR_IS_ACTIVE(vap, va_type))
1932 return (EINVAL);
1933 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1934 if (!VATTR_IS_ACTIVE(vap, va_rdev))
1935 return (EINVAL);
1c79356b 1936 rdev = txdr_unsigned(vap->va_rdev);
91447636 1937 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1c79356b
A
1938 rdev = 0xffffffff;
1939 else {
91447636 1940 return (ENOTSUP);
1c79356b 1941 }
91447636
A
1942 nfsm_reqhead(NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1943 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1944 if (error)
1c79356b 1945 return (error);
91447636
A
1946
1947 VATTR_SET_SUPPORTED(vap, va_mode);
1948 VATTR_SET_SUPPORTED(vap, va_uid);
1949 VATTR_SET_SUPPORTED(vap, va_gid);
1950 VATTR_SET_SUPPORTED(vap, va_data_size);
1951 VATTR_SET_SUPPORTED(vap, va_access_time);
1952 VATTR_SET_SUPPORTED(vap, va_modify_time);
1953 gotuid = VATTR_IS_ACTIVE(vap, va_uid);
1954 gotgid = VATTR_IS_ACTIVE(vap, va_gid);
1955
1956 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_MKNOD]);
1c79356b 1957 nfsm_fhtom(dvp, v3);
91447636 1958 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN, v3);
1c79356b 1959 if (v3) {
91447636 1960 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1c79356b 1961 *tl++ = vtonfsv3_type(vap->va_type);
91447636 1962 nfsm_v3sattr(vap);
1c79356b
A
1963 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1964 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
1965 *tl++ = txdr_unsigned(major(vap->va_rdev));
1966 *tl = txdr_unsigned(minor(vap->va_rdev));
1967 }
1968 } else {
91447636 1969 struct timespec neg1time = { -1, -1 };
1c79356b 1970 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
91447636
A
1971 sp->sa_mode = vtonfsv2_mode(vap->va_type,
1972 (VATTR_IS_ACTIVE(vap, va_mode) ? vap->va_mode : 0600));
1973 sp->sa_uid = gotuid ? (u_long)txdr_unsigned(vap->va_uid) : nfs_xdrneg1;
1974 sp->sa_gid = gotgid ? (u_long)txdr_unsigned(vap->va_gid) : nfs_xdrneg1;
1c79356b 1975 sp->sa_size = rdev;
91447636
A
1976 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
1977 txdr_nfsv2time(&vap->va_access_time, &sp->sa_atime);
1978 } else {
1979 txdr_nfsv2time(&neg1time, &sp->sa_atime);
1980 }
1981 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
1982 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
1983 } else {
1984 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
1985 }
1986 }
1987 nfsm_request(dvp, NFSPROC_MKNOD, p, cred, &xid);
1988 /* XXX no EEXIST kludge here? */
1989 if (!error) {
1990 nfsm_mtofh(dvp, cnp, newvp, v3, &xid, gotvp);
1991 if (!gotvp) {
1c79356b 1992 error = nfs_lookitup(dvp, cnp->cn_nameptr,
91447636 1993 cnp->cn_namelen, cred, p, &np);
1c79356b
A
1994 if (!error)
1995 newvp = NFSTOV(np);
1996 }
1997 }
e5568f75 1998 if (v3 && mrep)
91447636
A
1999 nfsm_wcc_data(dvp, &premtime, wccpostattr, &xid);
2000 if (!error && (gotuid || gotgid) &&
2001 (!newvp || nfs_getattrcache(newvp, &nvattr) ||
2002 (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
2003 (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
2004 /* clear ID bits if server didn't use them (or we can't tell) */
2005 VATTR_CLEAR_SUPPORTED(vap, va_uid);
2006 VATTR_CLEAR_SUPPORTED(vap, va_gid);
2007 }
1c79356b
A
2008 nfsm_reqdone;
2009 if (error) {
2010 if (newvp)
91447636 2011 vnode_put(newvp);
1c79356b 2012 } else {
1c79356b
A
2013 *vpp = newvp;
2014 }
55e303ae 2015 VTONFS(dvp)->n_flag |= NMODIFIED;
483a1d10 2016 /* if directory hadn't changed, update namecache mtime */
91447636
A
2017 if (nfstimespeccmp(&VTONFS(dvp)->n_ncmtime, &premtime, ==))
2018 VTONFS(dvp)->n_ncmtime = VTONFS(dvp)->n_vattr.nva_mtime;
483a1d10 2019 if (!wccpostattr)
91447636 2020 NATTRINVALIDATE(VTONFS(dvp));
1c79356b
A
2021 return (error);
2022}
2023
2024/*
2025 * nfs mknod vop
2026 * just call nfs_mknodrpc() to do the work.
2027 */
2028/* ARGSUSED */
2029static int
2030nfs_mknod(ap)
91447636
A
2031 struct vnop_mknod_args /* {
2032 struct vnodeop_desc *a_desc;
2033 vnode_t a_dvp;
2034 vnode_t *a_vpp;
1c79356b 2035 struct componentname *a_cnp;
91447636
A
2036 struct vnode_attr *a_vap;
2037 vfs_context_t a_context;
1c79356b
A
2038 } */ *ap;
2039{
1c79356b
A
2040 int error;
2041
91447636
A
2042 error = nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap,
2043 vfs_context_ucred(ap->a_context),
2044 vfs_context_proc(ap->a_context));
2045
1c79356b
A
2046 return (error);
2047}
2048
2049static u_long create_verf;
2050/*
2051 * nfs file create call
2052 */
2053static int
2054nfs_create(ap)
91447636
A
2055 struct vnop_create_args /* {
2056 struct vnodeop_desc *a_desc;
2057 vnode_t a_dvp;
2058 vnode_t *a_vpp;
1c79356b 2059 struct componentname *a_cnp;
91447636
A
2060 struct vnode_attr *a_vap;
2061 vfs_context_t a_context;
1c79356b
A
2062 } */ *ap;
2063{
91447636
A
2064 vnode_t dvp = ap->a_dvp;
2065 struct vnode_attr *vap = ap->a_vap;
2066 struct componentname *cnp = ap->a_cnp;
2067 struct nfs_vattr nvattr;
2068 struct nfsv2_sattr *sp;
2069 u_long *tl;
2070 caddr_t cp;
2071 long t1, t2;
1c79356b 2072 struct nfsnode *np = (struct nfsnode *)0;
91447636 2073 vnode_t newvp = (vnode_t)0;
1c79356b 2074 caddr_t bpos, dpos, cp2;
483a1d10 2075 int error = 0, wccpostattr = 0, gotvp = 0, fmode = 0;
91447636
A
2076 struct timespec premtime = { 0, 0 };
2077 mbuf_t mreq, mrep, md, mb, mb2;
1c79356b 2078 int v3 = NFS_ISV3(dvp);
91447636 2079 int gotuid, gotgid;
fa4905b1 2080 u_int64_t xid;
91447636
A
2081 kauth_cred_t cred;
2082 proc_t p;
2083
2084 cred = vfs_context_ucred(ap->a_context);
2085 p = vfs_context_proc(ap->a_context);
2086
2087 if (!VATTR_IS_ACTIVE(vap, va_type))
2088 return (EINVAL);
1c79356b
A
2089
2090 /*
2091 * Oops, not for me..
2092 */
2093 if (vap->va_type == VSOCK)
91447636
A
2094 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap, cred, p));
2095
2096 VATTR_SET_SUPPORTED(vap, va_mode);
2097 VATTR_SET_SUPPORTED(vap, va_uid);
2098 VATTR_SET_SUPPORTED(vap, va_gid);
2099 VATTR_SET_SUPPORTED(vap, va_data_size);
2100 VATTR_SET_SUPPORTED(vap, va_access_time);
2101 VATTR_SET_SUPPORTED(vap, va_modify_time);
2102 gotuid = VATTR_IS_ACTIVE(vap, va_uid);
2103 gotgid = VATTR_IS_ACTIVE(vap, va_gid);
1c79356b 2104
1c79356b
A
2105 if (vap->va_vaflags & VA_EXCLUSIVE)
2106 fmode |= O_EXCL;
2107again:
91447636 2108 nfsm_reqhead(NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1c79356b 2109 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
91447636
A
2110 if (error)
2111 return (error);
2112 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_CREATE]);
1c79356b 2113 nfsm_fhtom(dvp, v3);
91447636 2114 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN, v3);
1c79356b
A
2115 if (v3) {
2116 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
2117 if (fmode & O_EXCL) {
2118 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
2119 nfsm_build(tl, u_long *, NFSX_V3CREATEVERF);
2120 if (!TAILQ_EMPTY(&in_ifaddrhead))
2121 *tl++ = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr.s_addr;
2122 else
2123 *tl++ = create_verf;
2124 *tl = ++create_verf;
2125 } else {
2126 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
91447636 2127 nfsm_v3sattr(vap);
1c79356b
A
2128 }
2129 } else {
91447636 2130 struct timespec neg1time = { -1, -1 };
1c79356b 2131 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
91447636
A
2132 sp->sa_mode = vtonfsv2_mode(vap->va_type,
2133 (VATTR_IS_ACTIVE(vap, va_mode) ? vap->va_mode : 0600));
2134 sp->sa_uid = gotuid ? (u_long)txdr_unsigned(vap->va_uid) : nfs_xdrneg1;
2135 sp->sa_gid = gotgid ? (u_long)txdr_unsigned(vap->va_gid) : nfs_xdrneg1;
1c79356b 2136 sp->sa_size = 0;
91447636
A
2137 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
2138 txdr_nfsv2time(&vap->va_access_time, &sp->sa_atime);
2139 } else {
2140 txdr_nfsv2time(&neg1time, &sp->sa_atime);
2141 }
2142 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
2143 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
2144 } else {
2145 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
2146 }
1c79356b 2147 }
91447636 2148 nfsm_request(dvp, NFSPROC_CREATE, p, cred, &xid);
1c79356b 2149 if (!error) {
91447636 2150 nfsm_mtofh(dvp, cnp, newvp, v3, &xid, gotvp);
1c79356b 2151 if (!gotvp) {
1c79356b 2152 error = nfs_lookitup(dvp, cnp->cn_nameptr,
91447636 2153 cnp->cn_namelen, cred, p, &np);
1c79356b
A
2154 if (!error)
2155 newvp = NFSTOV(np);
2156 }
2157 }
e5568f75 2158 if (v3 && mrep)
91447636 2159 nfsm_wcc_data(dvp, &premtime, wccpostattr, &xid);
1c79356b
A
2160 nfsm_reqdone;
2161 if (error) {
2162 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
2163 fmode &= ~O_EXCL;
2164 goto again;
2165 }
2166 if (newvp)
91447636 2167 vnode_put(newvp);
ccc36f2f 2168 } else if (v3 && (fmode & O_EXCL)) {
91447636
A
2169 error = nfs_setattrrpc(newvp, vap, cred, p);
2170 if (error && (gotuid || gotgid)) {
2171 /* it's possible the server didn't like our attempt to set IDs. */
2172 /* so, let's try it again without those */
2173 VATTR_CLEAR_ACTIVE(vap, va_uid);
2174 VATTR_CLEAR_ACTIVE(vap, va_gid);
2175 error = nfs_setattrrpc(newvp, vap, cred, p);
2176 }
ccc36f2f 2177 if (error)
91447636 2178 vnode_put(newvp);
ccc36f2f 2179 }
1c79356b 2180 if (!error) {
1c79356b
A
2181 *ap->a_vpp = newvp;
2182 }
55e303ae 2183 VTONFS(dvp)->n_flag |= NMODIFIED;
483a1d10 2184 /* if directory hadn't changed, update namecache mtime */
91447636
A
2185 if (nfstimespeccmp(&VTONFS(dvp)->n_ncmtime, &premtime, ==))
2186 VTONFS(dvp)->n_ncmtime = VTONFS(dvp)->n_vattr.nva_mtime;
483a1d10 2187 if (!wccpostattr)
91447636
A
2188 NATTRINVALIDATE(VTONFS(dvp));
2189 if (!error && (gotuid || gotgid) &&
2190 (!newvp || nfs_getattrcache(newvp, &nvattr) ||
2191 (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
2192 (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
2193 /* clear ID bits if server didn't use them (or we can't tell) */
2194 VATTR_CLEAR_SUPPORTED(vap, va_uid);
2195 VATTR_CLEAR_SUPPORTED(vap, va_gid);
2196 }
1c79356b
A
2197 return (error);
2198}
2199
2200/*
2201 * nfs file remove call
2202 * To try and make nfs semantics closer to ufs semantics, a file that has
2203 * other processes using the vnode is renamed instead of removed and then
2204 * removed later on the last close.
91447636 2205 * - If vnode_isinuse()
1c79356b
A
2206 * If a rename is not already in the works
2207 * call nfs_sillyrename() to set it up
2208 * else
2209 * do the remove rpc
2210 */
2211static int
2212nfs_remove(ap)
91447636 2213 struct vnop_remove_args /* {
1c79356b 2214 struct vnodeop_desc *a_desc;
91447636
A
2215 vnode_t a_dvp;
2216 vnode_t a_vp;
2217 struct componentname *a_cnp;
2218 int a_flags;
2219 vfs_context_t a_context;
1c79356b
A
2220 } */ *ap;
2221{
91447636
A
2222 vnode_t vp = ap->a_vp;
2223 vnode_t dvp = ap->a_dvp;
2224 struct componentname *cnp = ap->a_cnp;
2225 struct nfsnode *np = VTONFS(vp);
9bccf70c 2226 int error = 0, gofree = 0;
91447636
A
2227 struct nfs_vattr nvattr;
2228 kauth_cred_t cred;
2229 proc_t p;
1c79356b 2230
91447636
A
2231 cred = vfs_context_ucred(ap->a_context);
2232 p = vfs_context_proc(ap->a_context);
9bccf70c 2233
91447636
A
2234 gofree = vnode_isinuse(vp, 0) ? 0 : 1;
2235 if ((ap->a_flags & VNODE_REMOVE_NODELETEBUSY) && !gofree) {
55e303ae 2236 /* Caller requested Carbon delete semantics, but file is busy */
55e303ae
A
2237 return (EBUSY);
2238 }
9bccf70c 2239 if (gofree || (np->n_sillyrename &&
91447636
A
2240 nfs_getattr(vp, &nvattr, cred, p) == 0 &&
2241 nvattr.nva_nlink > 1)) {
1c79356b
A
2242 /*
2243 * Purge the name cache so that the chance of a lookup for
2244 * the name succeeding while the remove is in progress is
483a1d10 2245 * minimized.
1c79356b
A
2246 */
2247 cache_purge(vp);
2248 /*
2249 * throw away biocache buffers, mainly to avoid
2250 * unnecessary delayed writes later.
2251 */
91447636 2252 error = nfs_vinvalbuf(vp, 0, cred, p, 1);
fa4905b1
A
2253 np->n_size = 0;
2254 ubc_setsize(vp, (off_t)0); /* XXX check error */
1c79356b
A
2255 /* Do the rpc */
2256 if (error != EINTR)
2257 error = nfs_removerpc(dvp, cnp->cn_nameptr,
91447636 2258 cnp->cn_namelen, cred, p);
1c79356b
A
2259 /*
2260 * Kludge City: If the first reply to the remove rpc is lost..
2261 * the reply to the retransmitted request will be ENOENT
2262 * since the file was in fact removed
2263 * Therefore, we cheat and return success.
2264 */
2265 if (error == ENOENT)
2266 error = 0;
55e303ae
A
2267 if (!error) {
2268 /*
2269 * remove nfsnode from hash now so we can't accidentally find it
2270 * again if another object gets created with the same filehandle
2271 * before this vnode gets reclaimed
2272 */
91447636 2273 lck_mtx_lock(nfs_node_hash_mutex);
55e303ae
A
2274 LIST_REMOVE(np, n_hash);
2275 np->n_flag &= ~NHASHED;
91447636
A
2276 lck_mtx_unlock(nfs_node_hash_mutex);
2277 }
2278 if (!error && !np->n_sillyrename) {
2279 /* clear flags now: won't get nfs_inactive for recycled vnode */
2280 /* clear all flags other than these */
2281 np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NHASHED);
2282 vnode_recycle(vp);
55e303ae 2283 }
1c79356b 2284 } else if (!np->n_sillyrename) {
91447636 2285 error = nfs_sillyrename(dvp, vp, cnp, cred, p);
1c79356b 2286 }
91447636 2287 NATTRINVALIDATE(np);
1c79356b
A
2288
2289 return (error);
2290}
2291
2292/*
2293 * nfs file remove rpc called from nfs_inactive
2294 */
2295int
91447636 2296nfs_removeit(struct sillyrename *sp)
1c79356b 2297{
91447636 2298 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred, NULL));
1c79356b
A
2299}
2300
2301/*
2302 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
2303 */
2304static int
2305nfs_removerpc(dvp, name, namelen, cred, proc)
91447636 2306 vnode_t dvp;
1c79356b
A
2307 char *name;
2308 int namelen;
91447636
A
2309 kauth_cred_t cred;
2310 proc_t proc;
1c79356b
A
2311{
2312 register u_long *tl;
2313 register caddr_t cp;
2314 register long t1, t2;
2315 caddr_t bpos, dpos, cp2;
483a1d10 2316 int error = 0, wccpostattr = 0;
91447636
A
2317 struct timespec premtime = { 0, 0 };
2318 mbuf_t mreq, mrep, md, mb, mb2;
55e303ae 2319 int v3;
fa4905b1 2320 u_int64_t xid;
1c79356b 2321
91447636 2322 if (!VFSTONFS(vnode_mount(dvp)))
55e303ae
A
2323 return (ENXIO);
2324 v3 = NFS_ISV3(dvp);
2325
91447636
A
2326 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
2327 if (error)
2328 return (error);
2329 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_REMOVE]);
1c79356b 2330 nfsm_fhtom(dvp, v3);
91447636 2331 nfsm_strtom(name, namelen, NFS_MAXNAMLEN, v3);
fa4905b1 2332 nfsm_request(dvp, NFSPROC_REMOVE, proc, cred, &xid);
e5568f75 2333 if (v3 && mrep)
91447636 2334 nfsm_wcc_data(dvp, &premtime, wccpostattr, &xid);
1c79356b 2335 nfsm_reqdone;
55e303ae 2336 VTONFS(dvp)->n_flag |= NMODIFIED;
483a1d10 2337 /* if directory hadn't changed, update namecache mtime */
91447636
A
2338 if (nfstimespeccmp(&VTONFS(dvp)->n_ncmtime, &premtime, ==))
2339 VTONFS(dvp)->n_ncmtime = VTONFS(dvp)->n_vattr.nva_mtime;
483a1d10 2340 if (!wccpostattr)
91447636 2341 NATTRINVALIDATE(VTONFS(dvp));
1c79356b
A
2342 return (error);
2343}
2344
2345/*
2346 * nfs file rename call
2347 */
2348static int
2349nfs_rename(ap)
91447636
A
2350 struct vnop_rename_args /* {
2351 struct vnodeop_desc *a_desc;
2352 vnode_t a_fdvp;
2353 vnode_t a_fvp;
1c79356b 2354 struct componentname *a_fcnp;
91447636
A
2355 vnode_t a_tdvp;
2356 vnode_t a_tvp;
1c79356b 2357 struct componentname *a_tcnp;
91447636 2358 vfs_context_t a_context;
1c79356b
A
2359 } */ *ap;
2360{
91447636
A
2361 vnode_t fvp = ap->a_fvp;
2362 vnode_t tvp = ap->a_tvp;
2363 vnode_t fdvp = ap->a_fdvp;
2364 vnode_t tdvp = ap->a_tdvp;
2365 struct componentname *tcnp = ap->a_tcnp;
2366 struct componentname *fcnp = ap->a_fcnp;
483a1d10 2367 int error, inuse=0;
91447636
A
2368 mount_t fmp, tdmp, tmp;
2369 struct nfsnode *tnp;
2370 kauth_cred_t cred;
2371 proc_t p;
2372
2373 cred = vfs_context_ucred(ap->a_context);
2374 p = vfs_context_proc(ap->a_context);
2375
2376 tnp = tvp ? VTONFS(tvp) : NULL;
1c79356b 2377
1c79356b 2378 /* Check for cross-device rename */
91447636
A
2379 fmp = vnode_mount(fvp);
2380 tmp = tvp ? vnode_mount(tvp) : NULL;
2381 tdmp = vnode_mount(tdvp);
2382 if ((fmp != tdmp) || (tvp && (fmp != tmp))) {
1c79356b
A
2383 error = EXDEV;
2384 goto out;
2385 }
2386
2387 /*
2388 * If the tvp exists and is in use, sillyrename it before doing the
2389 * rename of the new file over it.
2390 * XXX Can't sillyrename a directory.
9bccf70c
A
2391 * Don't sillyrename if source and target are same vnode (hard
2392 * links or case-variants)
1c79356b 2393 */
9bccf70c 2394 if (tvp && tvp != fvp) {
91447636 2395 inuse = vnode_isinuse(tvp, 0);
9bccf70c 2396 }
91447636
A
2397 if (inuse && !tnp->n_sillyrename && vnode_vtype(tvp) != VDIR) {
2398 if ((error = nfs_sillyrename(tdvp, tvp, tcnp, cred, p))) {
9bccf70c
A
2399 /* sillyrename failed. Instead of pressing on, return error */
2400 goto out; /* should not be ENOENT. */
2401 } else {
2402 /* sillyrename succeeded.*/
9bccf70c
A
2403 tvp = NULL;
2404 }
1c79356b
A
2405 }
2406
2407 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
91447636 2408 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, cred, p);
1c79356b 2409
91447636
A
2410 /*
2411 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2412 */
2413 if (error == ENOENT)
2414 error = 0;
2415
2416 if (!error && tvp && tvp != fvp && !tnp->n_sillyrename) {
55e303ae
A
2417 /*
2418 * remove nfsnode from hash now so we can't accidentally find it
2419 * again if another object gets created with the same filehandle
2420 * before this vnode gets reclaimed
2421 */
91447636
A
2422 lck_mtx_lock(nfs_node_hash_mutex);
2423 LIST_REMOVE(tnp, n_hash);
2424 tnp->n_flag &= ~NHASHED;
2425 lck_mtx_unlock(nfs_node_hash_mutex);
55e303ae 2426 }
9bccf70c 2427
91447636 2428 /* purge the old name cache entries and enter the new one */
9bccf70c
A
2429 cache_purge(fvp);
2430 if (tvp) {
483a1d10 2431 cache_purge(tvp);
91447636
A
2432 if (!error && !tnp->n_sillyrename) {
2433 /* clear flags now: won't get nfs_inactive for recycled vnode */
2434 /* clear all flags other than these */
2435 tnp->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NHASHED);
2436 vnode_recycle(tvp);
2437 }
9bccf70c 2438 }
91447636
A
2439 if (!error)
2440 cache_enter(tdvp, fvp, tcnp);
2441
1c79356b 2442out:
1c79356b
A
2443 /*
2444 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2445 */
2446 if (error == ENOENT)
2447 error = 0;
2448 return (error);
2449}
2450
2451/*
91447636 2452 * Do an nfs rename rpc. Called from nfs_rename() and nfs_sillyrename().
1c79356b
A
2453 */
2454static int
2455nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, proc)
91447636 2456 vnode_t fdvp;
1c79356b
A
2457 char *fnameptr;
2458 int fnamelen;
91447636 2459 vnode_t tdvp;
1c79356b
A
2460 char *tnameptr;
2461 int tnamelen;
91447636
A
2462 kauth_cred_t cred;
2463 proc_t proc;
1c79356b
A
2464{
2465 register u_long *tl;
2466 register caddr_t cp;
2467 register long t1, t2;
2468 caddr_t bpos, dpos, cp2;
483a1d10 2469 int error = 0, fwccpostattr = 0, twccpostattr = 0;
91447636
A
2470 struct timespec fpremtime = { 0, 0 }, tpremtime = { 0, 0 };
2471 mbuf_t mreq, mrep, md, mb, mb2;
55e303ae 2472 int v3;
fa4905b1 2473 u_int64_t xid;
1c79356b 2474
91447636 2475 if (!VFSTONFS(vnode_mount(fdvp)))
55e303ae
A
2476 return (ENXIO);
2477 v3 = NFS_ISV3(fdvp);
2478
91447636 2479 nfsm_reqhead((NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
fa4905b1 2480 nfsm_rndup(tnamelen));
91447636
A
2481 if (error)
2482 return (error);
2483 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_RENAME]);
1c79356b 2484 nfsm_fhtom(fdvp, v3);
91447636 2485 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN, v3);
1c79356b 2486 nfsm_fhtom(tdvp, v3);
91447636 2487 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN, v3);
fa4905b1 2488 nfsm_request(fdvp, NFSPROC_RENAME, proc, cred, &xid);
e5568f75 2489 if (v3 && mrep) {
fa4905b1
A
2490 u_int64_t txid = xid;
2491
91447636
A
2492 nfsm_wcc_data(fdvp, &fpremtime, fwccpostattr, &xid);
2493 nfsm_wcc_data(tdvp, &tpremtime, twccpostattr, &txid);
1c79356b
A
2494 }
2495 nfsm_reqdone;
55e303ae 2496 VTONFS(fdvp)->n_flag |= NMODIFIED;
483a1d10 2497 /* if directory hadn't changed, update namecache mtime */
91447636
A
2498 if (nfstimespeccmp(&VTONFS(fdvp)->n_ncmtime, &fpremtime, ==))
2499 VTONFS(fdvp)->n_ncmtime = VTONFS(fdvp)->n_vattr.nva_mtime;
483a1d10 2500 if (!fwccpostattr)
91447636 2501 NATTRINVALIDATE(VTONFS(fdvp));
55e303ae 2502 VTONFS(tdvp)->n_flag |= NMODIFIED;
483a1d10 2503 /* if directory hadn't changed, update namecache mtime */
91447636
A
2504 if (nfstimespeccmp(&VTONFS(tdvp)->n_ncmtime, &tpremtime, ==))
2505 VTONFS(tdvp)->n_ncmtime = VTONFS(tdvp)->n_vattr.nva_mtime;
483a1d10 2506 if (!twccpostattr)
91447636 2507 NATTRINVALIDATE(VTONFS(tdvp));
1c79356b
A
2508 return (error);
2509}
2510
2511/*
2512 * nfs hard link create call
2513 */
2514static int
2515nfs_link(ap)
91447636
A
2516 struct vnop_link_args /* {
2517 struct vnodeop_desc *a_desc;
2518 vnode_t a_vp;
2519 vnode_t a_tdvp;
1c79356b 2520 struct componentname *a_cnp;
91447636 2521 vfs_context_t a_context;
1c79356b
A
2522 } */ *ap;
2523{
91447636
A
2524 vnode_t vp = ap->a_vp;
2525 vnode_t tdvp = ap->a_tdvp;
2526 struct componentname *cnp = ap->a_cnp;
2527 u_long *tl;
2528 caddr_t cp;
2529 long t1, t2;
1c79356b 2530 caddr_t bpos, dpos, cp2;
483a1d10 2531 int error = 0, wccpostattr = 0, attrflag = 0;
91447636
A
2532 struct timespec premtime = { 0, 0 };
2533 mbuf_t mreq, mrep, md, mb, mb2;
2534 int v3;
fa4905b1 2535 u_int64_t xid;
91447636
A
2536 kauth_cred_t cred;
2537 proc_t p;
1c79356b 2538
91447636 2539 if (vnode_mount(vp) != vnode_mount(tdvp)) {
1c79356b
A
2540 return (EXDEV);
2541 }
2542
91447636
A
2543 cred = vfs_context_ucred(ap->a_context);
2544 p = vfs_context_proc(ap->a_context);
55e303ae 2545
55e303ae
A
2546 v3 = NFS_ISV3(vp);
2547
1c79356b
A
2548 /*
2549 * Push all writes to the server, so that the attribute cache
2550 * doesn't get "out of sync" with the server.
2551 * XXX There should be a better way!
2552 */
91447636 2553 nfs_flush(vp, MNT_WAIT, cred, p, 0);
1c79356b 2554
91447636
A
2555 nfsm_reqhead(NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2556 if (error)
2557 return (error);
2558 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_LINK]);
1c79356b
A
2559 nfsm_fhtom(vp, v3);
2560 nfsm_fhtom(tdvp, v3);
91447636
A
2561 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN, v3);
2562 nfsm_request(vp, NFSPROC_LINK, p, cred, &xid);
e5568f75 2563 if (v3 && mrep) {
fa4905b1
A
2564 u_int64_t txid = xid;
2565
91447636
A
2566 nfsm_postop_attr_update(vp, v3, attrflag, &xid);
2567 nfsm_wcc_data(tdvp, &premtime, wccpostattr, &txid);
1c79356b
A
2568 }
2569 nfsm_reqdone;
1c79356b
A
2570
2571 VTONFS(tdvp)->n_flag |= NMODIFIED;
55e303ae 2572 if (!attrflag)
91447636 2573 NATTRINVALIDATE(VTONFS(vp));
483a1d10 2574 /* if directory hadn't changed, update namecache mtime */
91447636
A
2575 if (nfstimespeccmp(&VTONFS(tdvp)->n_ncmtime, &premtime, ==))
2576 VTONFS(tdvp)->n_ncmtime = VTONFS(tdvp)->n_vattr.nva_mtime;
483a1d10 2577 if (!wccpostattr)
91447636 2578 NATTRINVALIDATE(VTONFS(tdvp));
1c79356b
A
2579 /*
2580 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2581 */
2582 if (error == EEXIST)
2583 error = 0;
2584 return (error);
2585}
2586
2587/*
2588 * nfs symbolic link create call
2589 */
2590static int
2591nfs_symlink(ap)
91447636
A
2592 struct vnop_symlink_args /* {
2593 struct vnodeop_desc *a_desc;
2594 vnode_t a_dvp;
2595 vnode_t *a_vpp;
1c79356b 2596 struct componentname *a_cnp;
91447636 2597 struct vnode_attr *a_vap;
1c79356b 2598 char *a_target;
91447636 2599 vfs_context_t a_context;
1c79356b
A
2600 } */ *ap;
2601{
91447636
A
2602 vnode_t dvp = ap->a_dvp;
2603 struct vnode_attr *vap = ap->a_vap;
2604 struct componentname *cnp = ap->a_cnp;
2605 struct nfs_vattr nvattr;
2606 struct nfsv2_sattr *sp;
2607 u_long *tl;
2608 caddr_t cp;
2609 long t1, t2;
1c79356b 2610 caddr_t bpos, dpos, cp2;
91447636
A
2611 int slen, error = 0, wccpostattr = 0, gotvp = 0;
2612 struct timespec premtime = { 0, 0 };
2613 mbuf_t mreq, mrep, md, mb, mb2;
2614 vnode_t newvp = (vnode_t)0;
1c79356b 2615 int v3 = NFS_ISV3(dvp);
91447636 2616 int gotuid, gotgid;
fa4905b1 2617 u_int64_t xid;
91447636
A
2618 kauth_cred_t cred;
2619 proc_t p;
2620 struct nfsnode *np = NULL;
2621
2622 cred = vfs_context_ucred(ap->a_context);
2623 p = vfs_context_proc(ap->a_context);
1c79356b 2624
1c79356b 2625 slen = strlen(ap->a_target);
91447636 2626 nfsm_reqhead(NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1c79356b 2627 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
91447636
A
2628 if (error)
2629 return (error);
2630
2631 VATTR_SET_SUPPORTED(vap, va_mode);
2632 VATTR_SET_SUPPORTED(vap, va_uid);
2633 VATTR_SET_SUPPORTED(vap, va_gid);
2634 VATTR_SET_SUPPORTED(vap, va_data_size);
2635 VATTR_SET_SUPPORTED(vap, va_access_time);
2636 VATTR_SET_SUPPORTED(vap, va_modify_time);
2637 gotuid = VATTR_IS_ACTIVE(vap, va_uid);
2638 gotgid = VATTR_IS_ACTIVE(vap, va_gid);
2639
2640 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_SYMLINK]);
1c79356b 2641 nfsm_fhtom(dvp, v3);
91447636 2642 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN, v3);
1c79356b 2643 if (v3) {
91447636 2644 nfsm_v3sattr(vap);
1c79356b 2645 }
91447636 2646 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN, v3);
1c79356b 2647 if (!v3) {
91447636 2648 struct timespec neg1time = { -1, -1 };
1c79356b 2649 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
91447636
A
2650 sp->sa_mode = vtonfsv2_mode(VLNK,
2651 (VATTR_IS_ACTIVE(vap, va_mode) ? vap->va_mode : 0600));
2652 sp->sa_uid = gotuid ? (u_long)txdr_unsigned(vap->va_uid) : nfs_xdrneg1;
2653 sp->sa_gid = gotgid ? (u_long)txdr_unsigned(vap->va_gid) : nfs_xdrneg1;
2654 sp->sa_size = nfs_xdrneg1;
2655 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
2656 txdr_nfsv2time(&vap->va_access_time, &sp->sa_atime);
2657 } else {
2658 txdr_nfsv2time(&neg1time, &sp->sa_atime);
2659 }
2660 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
2661 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
2662 } else {
2663 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
2664 }
1c79356b 2665 }
91447636 2666 nfsm_request(dvp, NFSPROC_SYMLINK, p, cred, &xid);
e5568f75 2667 if (v3 && mrep) {
fa4905b1
A
2668 u_int64_t dxid = xid;
2669
1c79356b 2670 if (!error)
91447636
A
2671 nfsm_mtofh(dvp, cnp, newvp, v3, &xid, gotvp);
2672 nfsm_wcc_data(dvp, &premtime, wccpostattr, &dxid);
1c79356b
A
2673 }
2674 nfsm_reqdone;
55e303ae
A
2675
2676 VTONFS(dvp)->n_flag |= NMODIFIED;
483a1d10 2677 /* if directory hadn't changed, update namecache mtime */
91447636
A
2678 if (nfstimespeccmp(&VTONFS(dvp)->n_ncmtime, &premtime, ==))
2679 VTONFS(dvp)->n_ncmtime = VTONFS(dvp)->n_vattr.nva_mtime;
483a1d10 2680 if (!wccpostattr)
91447636
A
2681 NATTRINVALIDATE(VTONFS(dvp));
2682
1c79356b 2683 /*
91447636
A
2684 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2685 * if we can succeed in looking up the symlink.
1c79356b 2686 */
91447636
A
2687 if ((error == EEXIST) || (!error && !gotvp)) {
2688 if (newvp) {
2689 vnode_put(newvp);
2690 newvp = NULL;
2691 }
2692 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen, cred, p, &np);
2693 if (!error) {
2694 newvp = NFSTOV(np);
2695 if (vnode_vtype(newvp) != VLNK)
2696 error = EEXIST;
2697 }
2698 }
2699 if (!error && (gotuid || gotgid) &&
2700 (!newvp || nfs_getattrcache(newvp, &nvattr) ||
2701 (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
2702 (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
2703 /* clear ID bits if server didn't use them (or we can't tell) */
2704 VATTR_CLEAR_SUPPORTED(vap, va_uid);
2705 VATTR_CLEAR_SUPPORTED(vap, va_gid);
2706 }
2707 if (error) {
2708 if (newvp)
2709 vnode_put(newvp);
2710 } else {
2711 *ap->a_vpp = newvp;
2712 }
1c79356b
A
2713 return (error);
2714}
2715
2716/*
2717 * nfs make dir call
2718 */
2719static int
2720nfs_mkdir(ap)
91447636
A
2721 struct vnop_mkdir_args /* {
2722 struct vnodeop_desc *a_desc;
2723 vnode_t a_dvp;
2724 vnode_t *a_vpp;
1c79356b 2725 struct componentname *a_cnp;
91447636
A
2726 struct vnode_attr *a_vap;
2727 vfs_context_t a_context;
1c79356b
A
2728 } */ *ap;
2729{
91447636
A
2730 vnode_t dvp = ap->a_dvp;
2731 struct vnode_attr *vap = ap->a_vap;
2732 struct componentname *cnp = ap->a_cnp;
2733 struct nfs_vattr nvattr;
2734 struct nfsv2_sattr *sp;
2735 u_long *tl;
2736 caddr_t cp;
2737 long t1, t2;
2738 int len;
1c79356b 2739 struct nfsnode *np = (struct nfsnode *)0;
91447636 2740 vnode_t newvp = (vnode_t)0;
1c79356b 2741 caddr_t bpos, dpos, cp2;
483a1d10 2742 int error = 0, wccpostattr = 0;
91447636 2743 struct timespec premtime = { 0, 0 };
1c79356b 2744 int gotvp = 0;
91447636 2745 mbuf_t mreq, mrep, md, mb, mb2;
1c79356b 2746 int v3 = NFS_ISV3(dvp);
91447636 2747 int gotuid, gotgid;
fa4905b1 2748 u_int64_t xid, dxid;
91447636
A
2749 kauth_cred_t cred;
2750 proc_t p;
2751
2752 cred = vfs_context_ucred(ap->a_context);
2753 p = vfs_context_proc(ap->a_context);
1c79356b 2754
1c79356b 2755 len = cnp->cn_namelen;
91447636
A
2756 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2757 if (error)
2758 return (error);
2759
2760 VATTR_SET_SUPPORTED(vap, va_mode);
2761 VATTR_SET_SUPPORTED(vap, va_uid);
2762 VATTR_SET_SUPPORTED(vap, va_gid);
2763 VATTR_SET_SUPPORTED(vap, va_data_size);
2764 VATTR_SET_SUPPORTED(vap, va_access_time);
2765 VATTR_SET_SUPPORTED(vap, va_modify_time);
2766 gotuid = VATTR_IS_ACTIVE(vap, va_uid);
2767 gotgid = VATTR_IS_ACTIVE(vap, va_gid);
2768
2769 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_MKDIR]);
1c79356b 2770 nfsm_fhtom(dvp, v3);
91447636 2771 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN, v3);
1c79356b 2772 if (v3) {
91447636 2773 nfsm_v3sattr(vap);
1c79356b 2774 } else {
91447636 2775 struct timespec neg1time = { -1, -1 };
1c79356b 2776 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
91447636
A
2777 sp->sa_mode = vtonfsv2_mode(VDIR,
2778 (VATTR_IS_ACTIVE(vap, va_mode) ? vap->va_mode : 0600));
2779 sp->sa_uid = gotuid ? (u_long)txdr_unsigned(vap->va_uid) : nfs_xdrneg1;
2780 sp->sa_gid = gotgid ? (u_long)txdr_unsigned(vap->va_gid) : nfs_xdrneg1;
2781 sp->sa_size = nfs_xdrneg1;
2782 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
2783 txdr_nfsv2time(&vap->va_access_time, &sp->sa_atime);
2784 } else {
2785 txdr_nfsv2time(&neg1time, &sp->sa_atime);
2786 }
2787 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
2788 txdr_nfsv2time(&vap->va_modify_time, &sp->sa_mtime);
2789 } else {
2790 txdr_nfsv2time(&neg1time, &sp->sa_mtime);
2791 }
1c79356b 2792 }
91447636 2793 nfsm_request(dvp, NFSPROC_MKDIR, p, cred, &xid);
fa4905b1 2794 dxid = xid;
1c79356b 2795 if (!error)
91447636 2796 nfsm_mtofh(dvp, cnp, newvp, v3, &xid, gotvp);
e5568f75 2797 if (v3 && mrep)
91447636 2798 nfsm_wcc_data(dvp, &premtime, wccpostattr, &dxid);
1c79356b 2799 nfsm_reqdone;
55e303ae 2800 VTONFS(dvp)->n_flag |= NMODIFIED;
483a1d10 2801 /* if directory hadn't changed, update namecache mtime */
91447636
A
2802 if (nfstimespeccmp(&VTONFS(dvp)->n_ncmtime, &premtime, ==))
2803 VTONFS(dvp)->n_ncmtime = VTONFS(dvp)->n_vattr.nva_mtime;
483a1d10 2804 if (!wccpostattr)
91447636 2805 NATTRINVALIDATE(VTONFS(dvp));
1c79356b
A
2806 /*
2807 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2808 * if we can succeed in looking up the directory.
2809 */
2810 if (error == EEXIST || (!error && !gotvp)) {
2811 if (newvp) {
91447636
A
2812 vnode_put(newvp);
2813 newvp = NULL;
1c79356b 2814 }
91447636 2815 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cred, p, &np);
1c79356b
A
2816 if (!error) {
2817 newvp = NFSTOV(np);
91447636 2818 if (vnode_vtype(newvp) != VDIR)
1c79356b
A
2819 error = EEXIST;
2820 }
2821 }
91447636
A
2822 if (!error && (gotuid || gotgid) &&
2823 (!newvp || nfs_getattrcache(newvp, &nvattr) ||
2824 (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
2825 (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
2826 /* clear ID bits if server didn't use them (or we can't tell) */
2827 VATTR_CLEAR_SUPPORTED(vap, va_uid);
2828 VATTR_CLEAR_SUPPORTED(vap, va_gid);
2829 }
1c79356b
A
2830 if (error) {
2831 if (newvp)
91447636 2832 vnode_put(newvp);
483a1d10 2833 } else {
1c79356b 2834 *ap->a_vpp = newvp;
483a1d10 2835 }
1c79356b
A
2836 return (error);
2837}
2838
2839/*
2840 * nfs remove directory call
2841 */
2842static int
2843nfs_rmdir(ap)
91447636
A
2844 struct vnop_rmdir_args /* {
2845 struct vnodeop_desc *a_desc;
2846 vnode_t a_dvp;
2847 vnode_t a_vp;
1c79356b 2848 struct componentname *a_cnp;
91447636 2849 vfs_context_t a_context;
1c79356b
A
2850 } */ *ap;
2851{
91447636
A
2852 vnode_t vp = ap->a_vp;
2853 vnode_t dvp = ap->a_dvp;
2854 struct componentname *cnp = ap->a_cnp;
2855 u_long *tl;
2856 caddr_t cp;
2857 long t1, t2;
1c79356b 2858 caddr_t bpos, dpos, cp2;
483a1d10 2859 int error = 0, wccpostattr = 0;
91447636
A
2860 struct timespec premtime = { 0, 0 };
2861 mbuf_t mreq, mrep, md, mb, mb2;
1c79356b 2862 int v3 = NFS_ISV3(dvp);
fa4905b1 2863 u_int64_t xid;
91447636
A
2864 kauth_cred_t cred;
2865 proc_t p;
1c79356b 2866
91447636
A
2867 cred = vfs_context_ucred(ap->a_context);
2868 p = vfs_context_proc(ap->a_context);
2869
2870 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2871 if (error)
2872 return (error);
2873 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_RMDIR]);
1c79356b 2874 nfsm_fhtom(dvp, v3);
91447636
A
2875 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN, v3);
2876 nfsm_request(dvp, NFSPROC_RMDIR, p, cred, &xid);
e5568f75 2877 if (v3 && mrep)
91447636 2878 nfsm_wcc_data(dvp, &premtime, wccpostattr, &xid);
1c79356b 2879 nfsm_reqdone;
55e303ae 2880 VTONFS(dvp)->n_flag |= NMODIFIED;
483a1d10 2881 /* if directory hadn't changed, update namecache mtime */
91447636
A
2882 if (nfstimespeccmp(&VTONFS(dvp)->n_ncmtime, &premtime, ==))
2883 VTONFS(dvp)->n_ncmtime = VTONFS(dvp)->n_vattr.nva_mtime;
483a1d10 2884 if (!wccpostattr)
91447636 2885 NATTRINVALIDATE(VTONFS(dvp));
1c79356b 2886 cache_purge(vp);
1c79356b
A
2887 /*
2888 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2889 */
2890 if (error == ENOENT)
2891 error = 0;
483a1d10
A
2892 if (!error) {
2893 /*
2894 * remove nfsnode from hash now so we can't accidentally find it
2895 * again if another object gets created with the same filehandle
2896 * before this vnode gets reclaimed
2897 */
91447636 2898 lck_mtx_lock(nfs_node_hash_mutex);
483a1d10
A
2899 LIST_REMOVE(VTONFS(vp), n_hash);
2900 VTONFS(vp)->n_flag &= ~NHASHED;
91447636 2901 lck_mtx_unlock(nfs_node_hash_mutex);
483a1d10 2902 }
1c79356b
A
2903 return (error);
2904}
2905
2906/*
2907 * nfs readdir call
2908 */
2909static int
2910nfs_readdir(ap)
91447636
A
2911 struct vnop_readdir_args /* {
2912 struct vnodeop_desc *a_desc;
2913 vnode_t a_vp;
1c79356b 2914 struct uio *a_uio;
91447636
A
2915 int *a_eofflag;
2916 int *a_ncookies;
2917 u_long **a_cookies;
2918 vfs_context_t a_context;
1c79356b
A
2919 } */ *ap;
2920{
91447636
A
2921 vnode_t vp = ap->a_vp;
2922 struct nfsnode *np = VTONFS(vp);
2923 struct uio *uio = ap->a_uio;
1c79356b 2924 int tresid, error;
91447636
A
2925 struct nfs_vattr nvattr;
2926 kauth_cred_t cred;
2927 proc_t p;
1c79356b 2928
91447636 2929 if (vnode_vtype(vp) != VDIR)
1c79356b 2930 return (EPERM);
91447636
A
2931
2932 cred = vfs_context_ucred(ap->a_context);
2933 p = vfs_context_proc(ap->a_context);
2934
1c79356b
A
2935 /*
2936 * First, check for hit on the EOF offset cache
2937 */
2938 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2939 (np->n_flag & NMODIFIED) == 0) {
91447636
A
2940 if (!nfs_getattr(vp, &nvattr, cred, p)) {
2941 if (nfstimespeccmp(&np->n_mtime, &nvattr.nva_mtime, ==)) {
2942 OSAddAtomic(1, (SInt32*)&nfsstats.direofcache_hits);
55e303ae
A
2943 return (0);
2944 }
91447636 2945 if (nfstimespeccmp(&np->n_ncmtime, &nvattr.nva_mtime, !=)) {
483a1d10
A
2946 /* directory changed, purge any name cache entries */
2947 cache_purge(vp);
2948 }
1c79356b
A
2949 }
2950 }
2951
2952 /*
2953 * Call nfs_bioread() to do the real work.
2954 */
91447636
A
2955 // LP64todo - fix this
2956 tresid = uio_uio_resid(uio);
2957 error = nfs_bioread(vp, uio, 0, cred, p);
1c79356b 2958
91447636
A
2959 if (!error && uio_uio_resid(uio) == tresid)
2960 OSAddAtomic(1, (SInt32*)&nfsstats.direofcache_misses);
1c79356b
A
2961 return (error);
2962}
2963
2964/*
2965 * Readdir rpc call.
2966 * Called from below the buffer cache by nfs_doio().
2967 */
2968int
91447636
A
2969nfs_readdirrpc(
2970 vnode_t vp,
2971 struct uio *uiop,
2972 kauth_cred_t cred,
2973 proc_t p)
1c79356b 2974{
91447636 2975 register int len, skiplen, left;
1c79356b
A
2976 register struct dirent *dp;
2977 register u_long *tl;
2978 register caddr_t cp;
2979 register long t1, t2;
2980 register nfsuint64 *cookiep;
2981 caddr_t bpos, dpos, cp2;
91447636 2982 mbuf_t mreq, mrep, md, mb, mb2;
1c79356b 2983 nfsuint64 cookie;
55e303ae 2984 struct nfsmount *nmp;
1c79356b
A
2985 struct nfsnode *dnp = VTONFS(vp);
2986 u_quad_t fileno;
2987 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2988 int attrflag;
55e303ae 2989 int v3, nmreaddirsize;
fa4905b1 2990 u_int64_t xid;
1c79356b
A
2991
2992#ifndef nolint
2993 dp = (struct dirent *)0;
2994#endif
2995#if DIAGNOSTIC
2996 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (NFS_DIRBLKSIZ - 1)) ||
91447636 2997 (uio_uio_resid(uiop) & (NFS_DIRBLKSIZ - 1)))
1c79356b
A
2998 panic("nfs_readdirrpc: bad uio");
2999#endif
91447636 3000 nmp = VFSTONFS(vnode_mount(vp));
55e303ae
A
3001 if (!nmp)
3002 return (ENXIO);
3003 v3 = NFS_ISV3(vp);
3004 nmreaddirsize = nmp->nm_readdirsize;
1c79356b
A
3005
3006 /*
3007 * If there is no cookie, assume directory was stale.
3008 */
3009 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
3010 if (cookiep)
3011 cookie = *cookiep;
3012 else
3013 return (NFSERR_BAD_COOKIE);
3014 /*
3015 * Loop around doing readdir rpc's of size nm_readdirsize
3016 * truncated to a multiple of DIRBLKSIZ.
3017 * The stopping criteria is EOF or buffer full.
3018 */
3019 while (more_dirs && bigenough) {
91447636
A
3020 nfsm_reqhead(NFSX_FH(v3) + NFSX_READDIR(v3));
3021 if (error)
3022 goto nfsmout;
3023 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_READDIR]);
1c79356b
A
3024 nfsm_fhtom(vp, v3);
3025 if (v3) {
3026 nfsm_build(tl, u_long *, 5 * NFSX_UNSIGNED);
3027 *tl++ = cookie.nfsuquad[0];
3028 *tl++ = cookie.nfsuquad[1];
3029 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3030 *tl++ = dnp->n_cookieverf.nfsuquad[1];
3031 } else {
3032 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
3033 *tl++ = cookie.nfsuquad[0];
3034 }
55e303ae 3035 *tl = txdr_unsigned(nmreaddirsize);
91447636 3036 nfsm_request(vp, NFSPROC_READDIR, p, cred, &xid);
1c79356b 3037 if (v3) {
e5568f75 3038 if (mrep) {
91447636 3039 nfsm_postop_attr_update(vp, v3, attrflag, &xid);
e5568f75 3040 }
1c79356b
A
3041 if (!error) {
3042 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
3043 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3044 dnp->n_cookieverf.nfsuquad[1] = *tl;
3045 } else {
91447636 3046 mbuf_freem(mrep);
1c79356b
A
3047 goto nfsmout;
3048 }
e5568f75
A
3049 } else if (!mrep) {
3050 // XXX assert error?
3051 goto nfsmout;
1c79356b
A
3052 }
3053 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3054 more_dirs = fxdr_unsigned(int, *tl);
3055
3056 /* loop thru the dir entries, doctoring them to 4bsd form */
3057 while (more_dirs && bigenough) {
3058 if (v3) {
3059 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3060 fxdr_hyper(tl, &fileno);
3061 len = fxdr_unsigned(int, *(tl + 2));
3062 } else {
3063 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
3064 fileno = fxdr_unsigned(u_quad_t, *tl++);
3065 len = fxdr_unsigned(int, *tl);
3066 }
91447636
A
3067 /* Note: v3 supports longer names, but struct dirent doesn't */
3068 /* so we just truncate the names to fit */
3069 if (len <= 0) {
1c79356b 3070 error = EBADRPC;
91447636 3071 mbuf_freem(mrep);
1c79356b
A
3072 goto nfsmout;
3073 }
91447636
A
3074 if (len > MAXNAMLEN) {
3075 skiplen = len - MAXNAMLEN;
3076 len = MAXNAMLEN;
3077 } else {
3078 skiplen = 0;
3079 }
1c79356b
A
3080 tlen = nfsm_rndup(len);
3081 if (tlen == len)
3082 tlen += 4; /* To ensure null termination */
3083 left = DIRBLKSIZ - blksiz;
91447636 3084 if ((tlen + (int)DIRHDSIZ) > left) {
1c79356b 3085 dp->d_reclen += left;
91447636
A
3086 uio_iov_base_add(uiop, left);
3087 uio_iov_len_add(uiop, -left);
1c79356b 3088 uiop->uio_offset += left;
91447636 3089 uio_uio_resid_add(uiop, -left);
1c79356b
A
3090 blksiz = 0;
3091 }
91447636 3092 if ((tlen + (int)DIRHDSIZ) > uio_uio_resid(uiop))
1c79356b
A
3093 bigenough = 0;
3094 if (bigenough) {
91447636
A
3095 // LP64todo - fix this!
3096 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
1c79356b
A
3097 dp->d_fileno = (int)fileno;
3098 dp->d_namlen = len;
3099 dp->d_reclen = tlen + DIRHDSIZ;
3100 dp->d_type = DT_UNKNOWN;
3101 blksiz += dp->d_reclen;
3102 if (blksiz == DIRBLKSIZ)
3103 blksiz = 0;
3104 uiop->uio_offset += DIRHDSIZ;
91447636
A
3105#if LP64KERN
3106 uio_uio_resid_add(uiop, -((int64_t)DIRHDSIZ));
3107 uio_iov_len_add(uiop, -((int64_t)DIRHDSIZ));
3108#else
3109 uio_uio_resid_add(uiop, -((int)DIRHDSIZ));
3110 uio_iov_len_add(uiop, -((int)DIRHDSIZ));
3111#endif
3112 uio_iov_base_add(uiop, DIRHDSIZ);
1c79356b 3113 nfsm_mtouio(uiop, len);
91447636
A
3114 // LP64todo - fix this!
3115 cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
1c79356b
A
3116 tlen -= len;
3117 *cp = '\0'; /* null terminate */
91447636
A
3118 uio_iov_base_add(uiop, tlen);
3119 uio_iov_len_add(uiop, -tlen);
1c79356b 3120 uiop->uio_offset += tlen;
91447636
A
3121 uio_uio_resid_add(uiop, -tlen);
3122 } else {
1c79356b 3123 nfsm_adv(nfsm_rndup(len));
91447636
A
3124 }
3125 if (skiplen)
3126 nfsm_adv(nfsm_rndup(skiplen));
1c79356b
A
3127 if (v3) {
3128 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3129 } else {
3130 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
3131 }
3132 if (bigenough) {
3133 cookie.nfsuquad[0] = *tl++;
3134 if (v3)
3135 cookie.nfsuquad[1] = *tl++;
3136 } else if (v3)
3137 tl += 2;
3138 else
3139 tl++;
3140 more_dirs = fxdr_unsigned(int, *tl);
3141 }
3142 /*
3143 * If at end of rpc data, get the eof boolean
3144 */
3145 if (!more_dirs) {
3146 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3147 more_dirs = (fxdr_unsigned(int, *tl) == 0);
3148 }
91447636 3149 mbuf_freem(mrep);
1c79356b
A
3150 }
3151 /*
3152 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
3153 * by increasing d_reclen for the last record.
3154 */
3155 if (blksiz > 0) {
3156 left = DIRBLKSIZ - blksiz;
3157 dp->d_reclen += left;
91447636
A
3158 uio_iov_base_add(uiop, left);
3159 uio_iov_len_add(uiop, -left);
1c79356b 3160 uiop->uio_offset += left;
91447636 3161 uio_uio_resid_add(uiop, -left);
1c79356b
A
3162 }
3163
3164 /*
3165 * We are now either at the end of the directory or have filled the
3166 * block.
3167 */
3168 if (bigenough)
3169 dnp->n_direofoffset = uiop->uio_offset;
3170 else {
91447636 3171 if (uio_uio_resid(uiop) > 0)
1c79356b
A
3172 printf("EEK! readdirrpc resid > 0\n");
3173 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
91447636
A
3174 if (cookiep)
3175 *cookiep = cookie;
1c79356b
A
3176 }
3177nfsmout:
3178 return (error);
3179}
3180
3181/*
3182 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
3183 */
3184int
91447636
A
3185nfs_readdirplusrpc(
3186 vnode_t vp,
3187 struct uio *uiop,
3188 kauth_cred_t cred,
3189 proc_t p)
1c79356b 3190{
91447636
A
3191 int len, skiplen, left;
3192 struct dirent *dp;
3193 u_long *tl;
3194 caddr_t cp;
3195 long t1, t2;
3196 vnode_t newvp;
3197 nfsuint64 *cookiep;
3198 caddr_t bpos, dpos, cp2;
3199 mbuf_t mreq, mrep, md, mb, mb2;
3200 struct componentname cn, *cnp = &cn;
1c79356b 3201 nfsuint64 cookie;
55e303ae 3202 struct nfsmount *nmp;
1c79356b 3203 struct nfsnode *dnp = VTONFS(vp), *np;
91447636 3204 u_char *fhp;
1c79356b
A
3205 u_quad_t fileno;
3206 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
55e303ae 3207 int attrflag, fhsize, nmreaddirsize, nmrsize;
fa4905b1 3208 u_int64_t xid, savexid;
91447636 3209 struct nfs_vattr nvattr;
1c79356b
A
3210
3211#ifndef nolint
3212 dp = (struct dirent *)0;
3213#endif
3214#if DIAGNOSTIC
3215 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
91447636 3216 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)))
1c79356b
A
3217 panic("nfs_readdirplusrpc: bad uio");
3218#endif
91447636 3219 nmp = VFSTONFS(vnode_mount(vp));
55e303ae
A
3220 if (!nmp)
3221 return (ENXIO);
3222 nmreaddirsize = nmp->nm_readdirsize;
3223 nmrsize = nmp->nm_rsize;
3224
91447636 3225 bzero(cnp, sizeof(*cnp));
1c79356b
A
3226 newvp = NULLVP;
3227
3228 /*
3229 * If there is no cookie, assume directory was stale.
3230 */
3231 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
3232 if (cookiep)
3233 cookie = *cookiep;
3234 else
3235 return (NFSERR_BAD_COOKIE);
3236 /*
3237 * Loop around doing readdir rpc's of size nm_readdirsize
3238 * truncated to a multiple of DIRBLKSIZ.
3239 * The stopping criteria is EOF or buffer full.
3240 */
3241 while (more_dirs && bigenough) {
91447636
A
3242 nfsm_reqhead(NFSX_FH(1) + 6 * NFSX_UNSIGNED);
3243 if (error)
3244 goto nfsmout;
3245 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_READDIRPLUS]);
1c79356b
A
3246 nfsm_fhtom(vp, 1);
3247 nfsm_build(tl, u_long *, 6 * NFSX_UNSIGNED);
3248 *tl++ = cookie.nfsuquad[0];
3249 *tl++ = cookie.nfsuquad[1];
3250 *tl++ = dnp->n_cookieverf.nfsuquad[0];
3251 *tl++ = dnp->n_cookieverf.nfsuquad[1];
55e303ae
A
3252 *tl++ = txdr_unsigned(nmreaddirsize);
3253 *tl = txdr_unsigned(nmrsize);
91447636 3254 nfsm_request(vp, NFSPROC_READDIRPLUS, p, cred, &xid);
fa4905b1 3255 savexid = xid;
e5568f75 3256 if (mrep) {
91447636 3257 nfsm_postop_attr_update(vp, 1, attrflag, &xid);
e5568f75 3258 }
1c79356b 3259 if (error) {
91447636 3260 mbuf_freem(mrep);
1c79356b
A
3261 goto nfsmout;
3262 }
3263 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3264 dnp->n_cookieverf.nfsuquad[0] = *tl++;
3265 dnp->n_cookieverf.nfsuquad[1] = *tl++;
3266 more_dirs = fxdr_unsigned(int, *tl);
3267
3268 /* loop thru the dir entries, doctoring them to 4bsd form */
3269 while (more_dirs && bigenough) {
3270 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3271 fxdr_hyper(tl, &fileno);
3272 len = fxdr_unsigned(int, *(tl + 2));
91447636
A
3273 /* Note: v3 supports longer names, but struct dirent doesn't */
3274 /* so we just truncate the names to fit */
3275 if (len <= 0) {
1c79356b 3276 error = EBADRPC;
91447636 3277 mbuf_freem(mrep);
1c79356b
A
3278 goto nfsmout;
3279 }
91447636
A
3280 if (len > MAXNAMLEN) {
3281 skiplen = len - MAXNAMLEN;
3282 len = MAXNAMLEN;
3283 } else {
3284 skiplen = 0;
3285 }
1c79356b
A
3286 tlen = nfsm_rndup(len);
3287 if (tlen == len)
3288 tlen += 4; /* To ensure null termination*/
3289 left = DIRBLKSIZ - blksiz;
91447636 3290 if ((tlen + (int)DIRHDSIZ) > left) {
1c79356b 3291 dp->d_reclen += left;
91447636
A
3292 uio_iov_base_add(uiop, left);
3293 uio_iov_len_add(uiop, -left);
1c79356b 3294 uiop->uio_offset += left;
91447636 3295 uio_uio_resid_add(uiop, -left);
1c79356b
A
3296 blksiz = 0;
3297 }
91447636 3298 if ((tlen + (int)DIRHDSIZ) > uio_uio_resid(uiop))
1c79356b
A
3299 bigenough = 0;
3300 if (bigenough) {
91447636
A
3301 // LP64todo - fix this!
3302 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
1c79356b
A
3303 dp->d_fileno = (int)fileno;
3304 dp->d_namlen = len;
3305 dp->d_reclen = tlen + DIRHDSIZ;
3306 dp->d_type = DT_UNKNOWN;
3307 blksiz += dp->d_reclen;
3308 if (blksiz == DIRBLKSIZ)
3309 blksiz = 0;
3310 uiop->uio_offset += DIRHDSIZ;
91447636
A
3311#if LP64KERN
3312 uio_uio_resid_add(uiop, -((int64_t)DIRHDSIZ));
3313 uio_iov_len_add(uiop, -((int64_t)DIRHDSIZ));
3314#else
3315 uio_uio_resid_add(uiop, -((int)DIRHDSIZ));
3316 uio_iov_len_add(uiop, -((int)DIRHDSIZ));
3317#endif
3318 uio_iov_base_add(uiop, DIRHDSIZ);
3319 // LP64todo - fix this!
3320 cnp->cn_nameptr = CAST_DOWN(caddr_t, uio_iov_base(uiop));
1c79356b
A
3321 cnp->cn_namelen = len;
3322 nfsm_mtouio(uiop, len);
91447636 3323 cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
1c79356b
A
3324 tlen -= len;
3325 *cp = '\0';
91447636
A
3326 uio_iov_base_add(uiop, tlen);
3327 uio_iov_len_add(uiop, -tlen);
1c79356b 3328 uiop->uio_offset += tlen;
91447636
A
3329 uio_uio_resid_add(uiop, -tlen);
3330 } else {
1c79356b 3331 nfsm_adv(nfsm_rndup(len));
91447636
A
3332 }
3333 if (skiplen)
3334 nfsm_adv(nfsm_rndup(skiplen));
1c79356b
A
3335 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3336 if (bigenough) {
3337 cookie.nfsuquad[0] = *tl++;
3338 cookie.nfsuquad[1] = *tl++;
3339 } else
3340 tl += 2;
3341
3342 /*
3343 * Since the attributes are before the file handle
3344 * (sigh), we must skip over the attributes and then
3345 * come back and get them.
3346 */
3347 attrflag = fxdr_unsigned(int, *tl);
3348 if (attrflag) {
91447636
A
3349 /* grab attributes */
3350 nfsm_attr_get(1, &nvattr);
3351 dp->d_type = IFTODT(VTTOIF(nvattr.nva_type));
3352 /* check for file handle */
1c79356b
A
3353 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3354 doit = fxdr_unsigned(int, *tl);
3355 if (doit) {
3356 nfsm_getfh(fhp, fhsize, 1);
3357 if (NFS_CMPFH(dnp, fhp, fhsize)) {
91447636
A
3358 error = vnode_ref(vp);
3359 if (error) {
3360 doit = 0;
3361 } else {
3362 newvp = vp;
3363 np = dnp;
3364 }
55e303ae
A
3365 } else if (!bigenough ||
3366 (cnp->cn_namelen == 2 &&
3367 cnp->cn_nameptr[1] == '.' &&
3368 cnp->cn_nameptr[0] == '.')) {
3369 /*
91447636 3370 * XXXmacko I don't think this ".." thing is a problem anymore.
55e303ae
A
3371 * don't doit if we can't guarantee
3372 * that this entry is NOT ".." because
3373 * we would have to drop the lock on
3374 * the directory before getting the
91447636 3375 * lock on the ".." vnode... and we
55e303ae
A
3376 * don't want to drop the dvp lock in
3377 * the middle of a readdirplus.
3378 */
3379 doit = 0;
1c79356b 3380 } else {
91447636
A
3381 cnp->cn_hash = 0;
3382
3383 error = nfs_nget(vnode_mount(vp), vp, cnp,
3384 fhp, fhsize, &nvattr, &xid,
3385 NG_MAKEENTRY, &np);
3386 if (error)
1c79356b
A
3387 doit = 0;
3388 else
3389 newvp = NFSTOV(np);
3390 }
3391 }
91447636
A
3392 /* update attributes if not already updated */
3393 if (doit && bigenough && (np->n_xid <= savexid)) {
fa4905b1 3394 xid = savexid;
91447636
A
3395 nfs_loadattrcache(np, &nvattr, &xid, 0);
3396 /* any error can be ignored */
1c79356b
A
3397 }
3398 } else {
3399 /* Just skip over the file handle */
3400 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3401 i = fxdr_unsigned(int, *tl);
3402 nfsm_adv(nfsm_rndup(i));
3403 }
3404 if (newvp != NULLVP) {
55e303ae 3405 if (newvp == vp)
91447636 3406 vnode_rele(newvp);
55e303ae 3407 else
91447636 3408 vnode_put(newvp);
1c79356b
A
3409 newvp = NULLVP;
3410 }
3411 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3412 more_dirs = fxdr_unsigned(int, *tl);
3413 }
3414 /*
3415 * If at end of rpc data, get the eof boolean
3416 */
3417 if (!more_dirs) {
3418 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
3419 more_dirs = (fxdr_unsigned(int, *tl) == 0);
3420 }
91447636 3421 mbuf_freem(mrep);
1c79356b
A
3422 }
3423 /*
3424 * Fill last record, iff any, out to a multiple of NFS_DIRBLKSIZ
3425 * by increasing d_reclen for the last record.
3426 */
3427 if (blksiz > 0) {
3428 left = DIRBLKSIZ - blksiz;
3429 dp->d_reclen += left;
91447636
A
3430 uio_iov_base_add(uiop, left);
3431 uio_iov_len_add(uiop, -left);
1c79356b 3432 uiop->uio_offset += left;
91447636 3433 uio_uio_resid_add(uiop, -left);
1c79356b
A
3434 }
3435
3436 /*
3437 * We are now either at the end of the directory or have filled the
3438 * block.
3439 */
3440 if (bigenough)
3441 dnp->n_direofoffset = uiop->uio_offset;
3442 else {
91447636 3443 if (uio_uio_resid(uiop) > 0)
1c79356b
A
3444 printf("EEK! readdirplusrpc resid > 0\n");
3445 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
91447636
A
3446 if (cookiep)
3447 *cookiep = cookie;
1c79356b
A
3448 }
3449nfsmout:
1c79356b
A
3450 return (error);
3451}
3452
3453/*
3454 * Silly rename. To make the NFS filesystem that is stateless look a little
3455 * more like the "ufs" a remove of an active vnode is translated to a rename
3456 * to a funny looking filename that is removed by nfs_inactive on the
3457 * nfsnode. There is the potential for another process on a different client
3458 * to create the same funny name between the nfs_lookitup() fails and the
3459 * nfs_rename() completes, but...
3460 */
55e303ae
A
3461
3462/* format of "random" names and next name to try */
3463/* (note: shouldn't exceed size of sillyrename.s_name) */
3464static char sillyrename_name[] = ".nfsAAA%04x4.4";
3465
1c79356b 3466static int
91447636
A
3467nfs_sillyrename(
3468 vnode_t dvp,
3469 vnode_t vp,
3470 struct componentname *cnp,
3471 kauth_cred_t cred,
3472 proc_t p)
1c79356b
A
3473{
3474 register struct sillyrename *sp;
3475 struct nfsnode *np;
3476 int error;
3477 short pid;
91447636 3478 kauth_cred_t tmpcred;
55e303ae 3479 int i, j, k;
1c79356b 3480
483a1d10 3481 cache_purge(vp);
1c79356b
A
3482 np = VTONFS(vp);
3483#if DIAGNOSTIC
91447636 3484 if (vnode_vtype(vp) == VDIR)
1c79356b
A
3485 panic("nfs_sillyrename: dir");
3486#endif
3487 MALLOC_ZONE(sp, struct sillyrename *,
3488 sizeof (struct sillyrename), M_NFSREQ, M_WAITOK);
91447636
A
3489 if (!sp)
3490 return (ENOMEM);
3491 kauth_cred_ref(cred);
3492 sp->s_cred = cred;
1c79356b 3493 sp->s_dvp = dvp;
91447636
A
3494 error = vnode_ref(dvp);
3495 if (error)
3496 goto bad_norele;
1c79356b
A
3497
3498 /* Fudge together a funny name */
91447636 3499 pid = proc_pid(p);
55e303ae 3500 sp->s_namlen = sprintf(sp->s_name, sillyrename_name, pid);
1c79356b
A
3501
3502 /* Try lookitups until we get one that isn't there */
55e303ae 3503 i = j = k = 0;
91447636 3504 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred, p, NULL) == 0) {
55e303ae
A
3505 if (sp->s_name[4]++ >= 'z')
3506 sp->s_name[4] = 'A';
3507 if (++i > ('z' - 'A' + 1)) {
3508 i = 0;
3509 if (sp->s_name[5]++ >= 'z')
3510 sp->s_name[5] = 'A';
3511 if (++j > ('z' - 'A' + 1)) {
3512 j = 0;
3513 if (sp->s_name[6]++ >= 'z')
3514 sp->s_name[6] = 'A';
3515 if (++k > ('z' - 'A' + 1)) {
3516 error = EINVAL;
3517 goto bad;
3518 }
3519 }
1c79356b
A
3520 }
3521 }
55e303ae
A
3522 /* make note of next "random" name to try */
3523 if ((sillyrename_name[4] = (sp->s_name[4] + 1)) > 'z') {
3524 sillyrename_name[4] = 'A';
3525 if ((sillyrename_name[5] = (sp->s_name[5] + 1)) > 'z') {
3526 sillyrename_name[5] = 'A';
3527 if ((sillyrename_name[6] = (sp->s_name[6] + 1)) > 'z')
3528 sillyrename_name[6] = 'A';
3529 }
3530 }
3531 /* now, do the rename */
91447636
A
3532 error = nfs_renamerpc(dvp, cnp->cn_nameptr, cnp->cn_namelen,
3533 dvp, sp->s_name, sp->s_namlen, sp->s_cred, p);
3534 if (error)
1c79356b 3535 goto bad;
91447636 3536 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred, p, &np);
1c79356b
A
3537#if DIAGNOSTIC
3538 kprintf("sillyrename: %s, vp=%x, np=%x, dvp=%x\n",
3539 &sp->s_name[0], (unsigned)vp, (unsigned)np, (unsigned)dvp);
3540#endif
3541 np->n_sillyrename = sp;
3542 return (0);
3543bad:
91447636
A
3544 vnode_rele(sp->s_dvp);
3545bad_norele:
6601e61a
A
3546 tmpcred = sp->s_cred;
3547 sp->s_cred = NOCRED;
3548 kauth_cred_rele(tmpcred);
55e303ae 3549 FREE_ZONE((caddr_t)sp, sizeof (struct sillyrename), M_NFSREQ);
1c79356b
A
3550 return (error);
3551}
3552
3553/*
3554 * Look up a file name and optionally either update the file handle or
3555 * allocate an nfsnode, depending on the value of npp.
3556 * npp == NULL --> just do the lookup
3557 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
3558 * handled too
3559 * *npp != NULL --> update the file handle in the vnode
3560 */
3561static int
3562nfs_lookitup(dvp, name, len, cred, procp, npp)
91447636 3563 vnode_t dvp;
1c79356b
A
3564 char *name;
3565 int len;
91447636
A
3566 kauth_cred_t cred;
3567 proc_t procp;
1c79356b
A
3568 struct nfsnode **npp;
3569{
91447636
A
3570 u_long *tl;
3571 caddr_t cp;
3572 long t1, t2;
3573 vnode_t newvp = (vnode_t)0;
1c79356b
A
3574 struct nfsnode *np, *dnp = VTONFS(dvp);
3575 caddr_t bpos, dpos, cp2;
3576 int error = 0, fhlen, attrflag;
91447636
A
3577 mbuf_t mreq, mrep, md, mb, mb2;
3578 u_char *nfhp;
55e303ae 3579 int v3;
91447636
A
3580 u_int64_t xid, dxid, savedxid;
3581 struct nfs_vattr nvattr;
1c79356b 3582
91447636 3583 if (!VFSTONFS(vnode_mount(dvp)))
55e303ae
A
3584 return (ENXIO);
3585 v3 = NFS_ISV3(dvp);
3586
91447636
A
3587 nfsm_reqhead(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
3588 if (error)
3589 return (error);
3590 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_LOOKUP]);
1c79356b 3591 nfsm_fhtom(dvp, v3);
91447636 3592 nfsm_strtom(name, len, NFS_MAXNAMLEN, v3);
fa4905b1 3593 nfsm_request(dvp, NFSPROC_LOOKUP, procp, cred, &xid);
1c79356b 3594 if (npp && !error) {
91447636 3595 savedxid = xid;
1c79356b 3596 nfsm_getfh(nfhp, fhlen, v3);
91447636
A
3597 /* get attributes */
3598 if (v3) {
3599 nfsm_postop_attr_get(v3, attrflag, &nvattr);
3600 if (!attrflag) {
3601 /* We need valid attributes in order */
3602 /* to call nfs_nget/vnode_create(). */
3603 error = nfs_getattr_no_vnode(vnode_mount(dvp),
3604 nfhp, fhlen, cred, procp, &nvattr, &xid);
3605 if (error) {
3606 mbuf_freem(mrep);
3607 goto nfsmout;
3608 }
3609 }
3610 dxid = savedxid;
3611 nfsm_postop_attr_update(dvp, v3, attrflag, &dxid);
3612 } else {
3613 nfsm_attr_get(v3, &nvattr);
3614 }
1c79356b
A
3615 if (*npp) {
3616 np = *npp;
91447636
A
3617 if (fhlen != np->n_fhsize) {
3618 u_char *oldbuf = (np->n_fhsize > NFS_SMALLFH) ? np->n_fhp : NULL;
3619 if (fhlen > NFS_SMALLFH) {
3620 MALLOC_ZONE(np->n_fhp, u_char *, fhlen, M_NFSBIGFH, M_WAITOK);
3621 if (!np->n_fhp) {
3622 np->n_fhp = oldbuf;
3623 error = ENOMEM;
3624 mbuf_freem(mrep);
3625 goto nfsmout;
3626 }
3627 } else {
3628 np->n_fhp = &np->n_fh[0];
3629 }
3630 if (oldbuf) {
3631 FREE_ZONE(oldbuf, np->n_fhsize, M_NFSBIGFH);
3632 }
3633 }
3634 bcopy(nfhp, np->n_fhp, fhlen);
1c79356b
A
3635 np->n_fhsize = fhlen;
3636 newvp = NFSTOV(np);
91447636
A
3637 error = nfs_loadattrcache(np, &nvattr, &xid, 0);
3638 if (error) {
3639 mbuf_freem(mrep);
3640 goto nfsmout;
3641 }
1c79356b 3642 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
1c79356b 3643 newvp = dvp;
91447636
A
3644 if (dnp->n_xid <= savedxid) {
3645 dxid = savedxid;
3646 error = nfs_loadattrcache(dnp, &nvattr, &dxid, 0);
3647 if (error) {
3648 mbuf_freem(mrep);
3649 goto nfsmout;
3650 }
3651 }
1c79356b 3652 } else {
91447636
A
3653 struct componentname cn, *cnp = &cn;
3654 bzero(cnp, sizeof(*cnp));
3655 cnp->cn_nameptr = name;
3656 cnp->cn_namelen = len;
3657
3658 error = nfs_nget(vnode_mount(dvp), dvp, cnp, nfhp, fhlen,
3659 &nvattr, &xid, NG_MAKEENTRY, &np);
1c79356b 3660 if (error) {
91447636 3661 mbuf_freem(mrep);
1c79356b
A
3662 return (error);
3663 }
3664 newvp = NFSTOV(np);
3665 }
1c79356b
A
3666 }
3667 nfsm_reqdone;
3668 if (npp && *npp == NULL) {
3669 if (error) {
91447636 3670 if (newvp) {
1c79356b 3671 if (newvp == dvp)
91447636 3672 vnode_rele(newvp);
1c79356b 3673 else
91447636
A
3674 vnode_put(newvp);
3675 }
1c79356b
A
3676 } else
3677 *npp = np;
3678 }
3679 return (error);
3680}
3681
3682/*
3683 * Nfs Version 3 commit rpc
3684 */
55e303ae 3685int
91447636
A
3686nfs_commit(vp, offset, count, cred, procp)
3687 vnode_t vp;
1c79356b 3688 u_quad_t offset;
91447636
A
3689 u_int32_t count;
3690 kauth_cred_t cred;
3691 proc_t procp;
1c79356b 3692{
91447636
A
3693 caddr_t cp;
3694 u_long *tl;
3695 int t1, t2;
3696 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
1c79356b 3697 caddr_t bpos, dpos, cp2;
483a1d10 3698 int error = 0, wccpostattr = 0;
91447636
A
3699 struct timespec premtime = { 0, 0 };
3700 mbuf_t mreq, mrep, md, mb, mb2;
8f6c56a5
A
3701 u_int64_t xid, wverf;
3702
91447636 3703 FSDBG(521, vp, offset, count, nmp->nm_state);
55e303ae
A
3704 if (!nmp)
3705 return (ENXIO);
3706 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
1c79356b 3707 return (0);
91447636
A
3708 nfsm_reqhead(NFSX_FH(1));
3709 if (error)
3710 return (error);
3711 OSAddAtomic(1, (SInt32*)&nfsstats.rpccnt[NFSPROC_COMMIT]);
1c79356b
A
3712 nfsm_fhtom(vp, 1);
3713 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED);
3714 txdr_hyper(&offset, tl);
3715 tl += 2;
91447636 3716 *tl = txdr_unsigned(count);
fa4905b1 3717 nfsm_request(vp, NFSPROC_COMMIT, procp, cred, &xid);
e5568f75 3718 if (mrep) {
91447636 3719 nfsm_wcc_data(vp, &premtime, wccpostattr, &xid);
483a1d10 3720 /* XXX can we do anything useful with the wcc info? */
e5568f75 3721 }
1c79356b
A
3722 if (!error) {
3723 nfsm_dissect(tl, u_long *, NFSX_V3WRITEVERF);
8f6c56a5
A
3724 fxdr_hyper(tl, &wverf);
3725 if (wverf != nmp->nm_verf) {
3726 nmp->nm_verf = wverf;
1c79356b
A
3727 error = NFSERR_STALEWRITEVERF;
3728 }
3729 }
3730 nfsm_reqdone;
3731 return (error);
3732}
3733
1c79356b 3734static int
91447636
A
3735nfs_blockmap(
3736 __unused struct vnop_blockmap_args /* {
3737 struct vnodeop_desc *a_desc;
3738 vnode_t a_vp;
3739 off_t a_foffset;
3740 size_t a_size;
3741 daddr64_t *a_bpn;
3742 size_t *a_run;
3743 void *a_poff;
3744 int a_flags;
3745 } */ *ap)
1c79356b 3746{
91447636 3747 return (ENOTSUP);
1c79356b
A
3748}
3749
1c79356b
A
3750/*
3751 * Mmap a file
3752 *
3753 * NB Currently unsupported.
3754 */
91447636 3755/*ARGSUSED*/
1c79356b 3756static int
91447636
A
3757nfs_mmap(
3758 __unused struct vnop_mmap_args /* {
3759 struct vnodeop_desc *a_desc;
3760 vnode_t a_vp;
3761 int a_fflags;
3762 kauth_cred_t a_cred;
3763 proc_t a_p;
3764 } */ *ap)
1c79356b
A
3765{
3766
3767 return (EINVAL);
3768}
3769
3770/*
91447636 3771 * fsync vnode op. Just call nfs_flush() with commit == 1.
1c79356b
A
3772 */
3773/* ARGSUSED */
3774static int
3775nfs_fsync(ap)
91447636 3776 struct vnop_fsync_args /* {
1c79356b 3777 struct vnodeop_desc *a_desc;
91447636
A
3778 vnode_t a_vp;
3779 int a_waitfor;
3780 vfs_context_t a_context;
1c79356b
A
3781 } */ *ap;
3782{
91447636
A
3783 kauth_cred_t cred = vfs_context_ucred(ap->a_context);
3784 proc_t p = vfs_context_proc(ap->a_context);
3785 struct nfsnode *np = VTONFS(ap->a_vp);
3786 int error;
3787
3788 np->n_flag |= NWRBUSY;
3789 error = nfs_flush(ap->a_vp, ap->a_waitfor, cred, p, 0);
3790 np->n_flag &= ~NWRBUSY;
3791 return (error);
1c79356b 3792}
55e303ae
A
3793
3794int
91447636 3795nfs_flushcommits(vnode_t vp, proc_t p, int nowait)
1c79356b 3796{
55e303ae 3797 struct nfsnode *np = VTONFS(vp);
91447636
A
3798 struct nfsbuf *bp;
3799 struct nfsbuflists blist, commitlist;
3800 int error = 0, retv, wcred_set, flags;
1c79356b 3801 u_quad_t off, endoff, toff;
91447636
A
3802 u_int32_t count;
3803 kauth_cred_t wcred = NULL;
1c79356b 3804
55e303ae 3805 FSDBG_TOP(557, vp, np, 0, 0);
1c79356b
A
3806
3807 /*
55e303ae 3808 * A nb_flags == (NB_DELWRI | NB_NEEDCOMMIT) block has been written to the
1c79356b 3809 * server, but nas not been committed to stable storage on the server
55e303ae
A
3810 * yet. The byte range is worked out for as many nfsbufs as we can handle
3811 * and the commit rpc is done.
1c79356b 3812 */
91447636 3813 if (!LIST_EMPTY(&np->n_dirtyblkhd))
1c79356b 3814 np->n_flag |= NMODIFIED;
55e303ae 3815
1c79356b
A
3816 off = (u_quad_t)-1;
3817 endoff = 0;
55e303ae 3818 wcred_set = 0;
91447636 3819 LIST_INIT(&commitlist);
1c79356b 3820
91447636 3821 if (!VFSTONFS(vnode_mount(vp))) {
55e303ae
A
3822 error = ENXIO;
3823 goto done;
3824 }
3825 if (!NFS_ISV3(vp)) {
3826 error = EINVAL;
3827 goto done;
3828 }
9bccf70c 3829
91447636
A
3830 flags = NBI_DIRTY;
3831 if (nowait)
3832 flags |= NBI_NOWAIT;
3833 lck_mtx_lock(nfs_buf_mutex);
3834 if (!nfs_buf_iterprepare(np, &blist, flags)) {
3835 while ((bp = LIST_FIRST(&blist))) {
3836 LIST_REMOVE(bp, nb_vnbufs);
3837 LIST_INSERT_HEAD(&np->n_dirtyblkhd, bp, nb_vnbufs);
3838 error = nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0);
3839 if (error)
3840 continue;
8f6c56a5
A
3841 if (ISSET(bp->nb_flags, NB_NEEDCOMMIT))
3842 nfs_buf_check_write_verifier(np, bp);
91447636
A
3843 if (((bp->nb_flags & (NB_DELWRI | NB_NEEDCOMMIT))
3844 != (NB_DELWRI | NB_NEEDCOMMIT))) {
3845 nfs_buf_drop(bp);
3846 continue;
fa4905b1 3847 }
91447636
A
3848 nfs_buf_remfree(bp);
3849 lck_mtx_unlock(nfs_buf_mutex);
3850 /*
3851 * we need a upl to see if the page has been
3852 * dirtied (think mmap) since the unstable write, and
3853 * also to prevent vm from paging it during our commit rpc
3854 */
3855 if (!ISSET(bp->nb_flags, NB_PAGELIST)) {
3856 retv = nfs_buf_upl_setup(bp);
3857 if (retv) {
3858 /* unable to create upl */
3859 /* vm object must no longer exist */
3860 /* this could be fatal if we need */
3861 /* to write the data again, we'll see... */
3862 printf("nfs_flushcommits: upl create failed %d\n", retv);
3863 bp->nb_valid = bp->nb_dirty = 0;
3864 }
3865 }
3866 nfs_buf_upl_check(bp);
3867 lck_mtx_lock(nfs_buf_mutex);
9bccf70c 3868
91447636
A
3869 FSDBG(557, bp, bp->nb_flags, bp->nb_valid, bp->nb_dirty);
3870 FSDBG(557, bp->nb_validoff, bp->nb_validend,
3871 bp->nb_dirtyoff, bp->nb_dirtyend);
9bccf70c 3872
91447636
A
3873 /*
3874 * We used to check for dirty pages here; if there were any
3875 * we'd abort the commit and force the entire buffer to be
3876 * written again.
3877 *
3878 * Instead of doing that, we now go ahead and commit the dirty
3879 * range, and then leave the buffer around with dirty pages
3880 * that will be written out later.
3881 */
55e303ae 3882
91447636
A
3883 /*
3884 * Work out if all buffers are using the same cred
3885 * so we can deal with them all with one commit.
3886 *
3887 * XXX creds in bp's must be obtained by kauth_cred_ref on
3888 * the same original cred in order for them to be equal.
3889 */
3890 if (wcred_set == 0) {
3891 wcred = bp->nb_wcred;
6601e61a 3892 if (wcred == NOCRED)
91447636
A
3893 panic("nfs: needcommit w/out wcred");
3894 wcred_set = 1;
3895 } else if ((wcred_set == 1) && wcred != bp->nb_wcred) {
3896 wcred_set = -1;
3897 }
3898 SET(bp->nb_flags, NB_WRITEINPROG);
1c79356b 3899
91447636
A
3900 /*
3901 * A list of these buffers is kept so that the
3902 * second loop knows which buffers have actually
3903 * been committed. This is necessary, since there
3904 * may be a race between the commit rpc and new
3905 * uncommitted writes on the file.
3906 */
3907 LIST_REMOVE(bp, nb_vnbufs);
3908 LIST_INSERT_HEAD(&commitlist, bp, nb_vnbufs);
3909 toff = NBOFF(bp) + bp->nb_dirtyoff;
3910 if (toff < off)
3911 off = toff;
3912 toff += (u_quad_t)(bp->nb_dirtyend - bp->nb_dirtyoff);
3913 if (toff > endoff)
3914 endoff = toff;
1c79356b 3915 }
91447636 3916 nfs_buf_itercomplete(np, &blist, NBI_DIRTY);
55e303ae 3917 }
91447636 3918 lck_mtx_unlock(nfs_buf_mutex);
55e303ae 3919
91447636 3920 if (LIST_EMPTY(&commitlist)) {
55e303ae
A
3921 error = ENOBUFS;
3922 goto done;
3923 }
3924
3925 /*
3926 * Commit data on the server, as required.
3927 * If all bufs are using the same wcred, then use that with
3928 * one call for all of them, otherwise commit each one
3929 * separately.
3930 */
91447636
A
3931 if (wcred_set == 1) {
3932 /*
3933 * Note, it's possible the commit range could be >2^32-1.
3934 * If it is, we'll send one commit that covers the whole file.
3935 */
3936 if ((endoff - off) > 0xffffffff)
3937 count = 0;
3938 else
3939 count = (endoff - off);
3940 retv = nfs_commit(vp, off, count, wcred, p);
3941 } else {
55e303ae 3942 retv = 0;
91447636
A
3943 LIST_FOREACH(bp, &commitlist, nb_vnbufs) {
3944 toff = NBOFF(bp) + bp->nb_dirtyoff;
3945 count = bp->nb_dirtyend - bp->nb_dirtyoff;
3946 retv = nfs_commit(vp, toff, count, bp->nb_wcred, p);
3947 if (retv)
3948 break;
55e303ae
A
3949 }
3950 }
55e303ae
A
3951
3952 /*
3953 * Now, either mark the blocks I/O done or mark the
3954 * blocks dirty, depending on whether the commit
3955 * succeeded.
3956 */
91447636
A
3957 while ((bp = LIST_FIRST(&commitlist))) {
3958 LIST_REMOVE(bp, nb_vnbufs);
55e303ae 3959 FSDBG(557, bp, retv, bp->nb_flags, bp->nb_dirty);
55e303ae 3960 CLR(bp->nb_flags, (NB_NEEDCOMMIT | NB_WRITEINPROG));
55e303ae
A
3961 np->n_needcommitcnt--;
3962 CHECK_NEEDCOMMITCNT(np);
3963
3964 if (retv) {
91447636
A
3965 /* move back to dirty list */
3966 lck_mtx_lock(nfs_buf_mutex);
3967 LIST_INSERT_HEAD(&VTONFS(vp)->n_dirtyblkhd, bp, nb_vnbufs);
3968 lck_mtx_unlock(nfs_buf_mutex);
483a1d10 3969 nfs_buf_release(bp, 1);
91447636
A
3970 continue;
3971 }
55e303ae 3972
91447636
A
3973 vnode_startwrite(vp);
3974 if (ISSET(bp->nb_flags, NB_DELWRI)) {
3975 OSAddAtomic(-1, (SInt32*)&nfs_nbdwrite);
3976 NFSBUFCNTCHK(0);
3977 wakeup(&nfs_nbdwrite);
3978 }
3979 CLR(bp->nb_flags, (NB_READ|NB_DONE|NB_ERROR|NB_DELWRI));
3980 /* if block still has dirty pages, we don't want it to */
3981 /* be released in nfs_buf_iodone(). So, don't set NB_ASYNC. */
3982 if (!bp->nb_dirty)
3983 SET(bp->nb_flags, NB_ASYNC);
55e303ae 3984
91447636
A
3985 /* move to clean list */
3986 lck_mtx_lock(nfs_buf_mutex);
3987 LIST_INSERT_HEAD(&VTONFS(vp)->n_cleanblkhd, bp, nb_vnbufs);
3988 lck_mtx_unlock(nfs_buf_mutex);
55e303ae 3989
91447636 3990 bp->nb_dirtyoff = bp->nb_dirtyend = 0;
55e303ae 3991
91447636
A
3992 nfs_buf_iodone(bp);
3993 if (bp->nb_dirty) {
3994 /* throw it back in as a delayed write buffer */
3995 CLR(bp->nb_flags, NB_DONE);
3996 nfs_buf_write_delayed(bp, p);
1c79356b 3997 }
55e303ae
A
3998 }
3999
4000done:
55e303ae
A
4001 FSDBG_BOT(557, vp, np, 0, error);
4002 return (error);
4003}
1c79356b 4004
55e303ae
A
4005/*
4006 * Flush all the blocks associated with a vnode.
4007 * Walk through the buffer pool and push any dirty pages
4008 * associated with the vnode.
4009 */
91447636
A
4010int
4011nfs_flush(
4012 vnode_t vp,
4013 int waitfor,
4014 __unused kauth_cred_t cred,
4015 proc_t p,
4016 int ignore_writeerr)
55e303ae
A
4017{
4018 struct nfsnode *np = VTONFS(vp);
91447636
A
4019 struct nfsbuf *bp;
4020 struct nfsbuflists blist;
4021 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4022 int error = 0, error2, slptimeo = 0, slpflag = 0;
4023 int flags, passone = 1;
55e303ae 4024
483a1d10 4025 FSDBG_TOP(517, vp, np, waitfor, 0);
55e303ae
A
4026
4027 if (!nmp) {
4028 error = ENXIO;
4029 goto done;
1c79356b 4030 }
55e303ae
A
4031 if (nmp->nm_flag & NFSMNT_INT)
4032 slpflag = PCATCH;
55e303ae 4033
1c79356b 4034 /*
483a1d10
A
4035 * On the first pass, start async/unstable writes on all
4036 * delayed write buffers. Then wait for all writes to complete
4037 * and call nfs_flushcommits() to commit any uncommitted buffers.
4038 * On all subsequent passes, start STABLE writes on any remaining
4039 * dirty buffers. Then wait for all writes to complete.
1c79356b 4040 */
55e303ae 4041again:
91447636
A
4042 lck_mtx_lock(nfs_buf_mutex);
4043 FSDBG(518, LIST_FIRST(&np->n_dirtyblkhd), np->n_flag, 0, 0);
4044 if (!LIST_EMPTY(&np->n_dirtyblkhd))
55e303ae 4045 np->n_flag |= NMODIFIED;
91447636
A
4046 if (!VFSTONFS(vnode_mount(vp))) {
4047 lck_mtx_unlock(nfs_buf_mutex);
55e303ae 4048 error = ENXIO;
1c79356b
A
4049 goto done;
4050 }
55e303ae
A
4051
4052 /* Start/do any write(s) that are required. */
91447636
A
4053 if (!nfs_buf_iterprepare(np, &blist, NBI_DIRTY)) {
4054 while ((bp = LIST_FIRST(&blist))) {
4055 LIST_REMOVE(bp, nb_vnbufs);
4056 LIST_INSERT_HEAD(&np->n_dirtyblkhd, bp, nb_vnbufs);
4057 flags = (passone || (waitfor != MNT_WAIT)) ? NBAC_NOWAIT : 0;
4058 if (flags != NBAC_NOWAIT)
4059 nfs_buf_refget(bp);
4060 while ((error = nfs_buf_acquire(bp, flags, slpflag, slptimeo))) {
4061 FSDBG(524, bp, flags, bp->nb_lflags, bp->nb_flags);
4062 if (error == EBUSY)
4063 break;
4064 if (error) {
4065 error2 = nfs_sigintr(VFSTONFS(vnode_mount(vp)), NULL, p);
4066 if (error2) {
4067 if (flags != NBAC_NOWAIT)
4068 nfs_buf_refrele(bp);
4069 nfs_buf_itercomplete(np, &blist, NBI_DIRTY);
4070 lck_mtx_unlock(nfs_buf_mutex);
4071 error = error2;
4072 goto done;
4073 }
4074 if (slpflag == PCATCH) {
4075 slpflag = 0;
4076 slptimeo = 2 * hz;
4077 }
fa4905b1 4078 }
1c79356b 4079 }
91447636
A
4080 if (flags != NBAC_NOWAIT)
4081 nfs_buf_refrele(bp);
4082 if (error == EBUSY)
4083 continue;
4084 if (!bp->nb_vp) {
4085 /* buffer is no longer valid */
4086 nfs_buf_drop(bp);
4087 continue;
4088 }
8f6c56a5
A
4089 if (ISSET(bp->nb_flags, NB_NEEDCOMMIT))
4090 nfs_buf_check_write_verifier(np, bp);
91447636
A
4091 if (!ISSET(bp->nb_flags, NB_DELWRI))
4092 panic("nfs_flush: not dirty");
4093 FSDBG(525, bp, passone, bp->nb_lflags, bp->nb_flags);
4094 if ((passone || (waitfor != MNT_WAIT)) &&
4095 ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
4096 nfs_buf_drop(bp);
4097 continue;
4098 }
4099 nfs_buf_remfree(bp);
4100 lck_mtx_unlock(nfs_buf_mutex);
4101 if (ISSET(bp->nb_flags, NB_ERROR)) {
4102 np->n_error = bp->nb_error ? bp->nb_error : EIO;
4103 np->n_flag |= NWRITEERR;
4104 nfs_buf_release(bp, 1);
4105 lck_mtx_lock(nfs_buf_mutex);
4106 continue;
4107 }
4108 SET(bp->nb_flags, NB_ASYNC);
4109 if (!passone) {
4110 /* NB_STABLE forces this to be written FILESYNC */
4111 SET(bp->nb_flags, NB_STABLE);
4112 }
4113 nfs_buf_write(bp);
4114 lck_mtx_lock(nfs_buf_mutex);
55e303ae 4115 }
91447636 4116 nfs_buf_itercomplete(np, &blist, NBI_DIRTY);
1c79356b 4117 }
91447636 4118 lck_mtx_unlock(nfs_buf_mutex);
55e303ae 4119
1c79356b 4120 if (waitfor == MNT_WAIT) {
91447636
A
4121 while ((error = vnode_waitforwrites(vp, 0, slpflag, slptimeo, "nfsflush"))) {
4122 error2 = nfs_sigintr(VFSTONFS(vnode_mount(vp)), NULL, p);
4123 if (error2) {
4124 error = error2;
1c79356b 4125 goto done;
91447636
A
4126 }
4127 if (slpflag == PCATCH) {
1c79356b
A
4128 slpflag = 0;
4129 slptimeo = 2 * hz;
1c79356b
A
4130 }
4131 }
483a1d10
A
4132 }
4133
4134 if (NFS_ISV3(vp)) {
4135 /* loop while it looks like there are still buffers to be */
4136 /* commited and nfs_flushcommits() seems to be handling them. */
4137 while (np->n_needcommitcnt)
91447636 4138 if (nfs_flushcommits(vp, p, 0))
483a1d10
A
4139 break;
4140 }
4141
4142 if (passone) {
4143 passone = 0;
4144 goto again;
4145 }
4146
13fec989
A
4147 if (waitfor == MNT_WAIT) {
4148 if (!LIST_EMPTY(&np->n_dirtyblkhd))
4149 goto again;
4150 /* if we have no dirty blocks, we can clear the modified flag */
3a60a9f5 4151 np->n_flag &= ~NMODIFIED;
13fec989 4152 }
483a1d10 4153
fa4905b1 4154 FSDBG(526, np->n_flag, np->n_error, 0, 0);
91447636 4155 if (!ignore_writeerr && (np->n_flag & NWRITEERR)) {
1c79356b
A
4156 error = np->n_error;
4157 np->n_flag &= ~NWRITEERR;
4158 }
4159done:
fa4905b1 4160 FSDBG_BOT(517, vp, np, error, 0);
1c79356b
A
4161 return (error);
4162}
4163
4164/*
91447636 4165 * Do an nfs pathconf rpc.
1c79356b 4166 */
91447636
A
4167int
4168nfs_pathconfrpc(
4169 vnode_t vp,
4170 struct nfsv3_pathconf *pc,
4171 kauth_cred_t cred,
4172 proc_t procp)
1c79356b 4173{
91447636
A
4174 mbuf_t mreq, mrep, md, mb, mb2;
4175 caddr_t bpos, dpos, cp, cp2;
4176 int32_t t1, t2;
4177 u_long *tl;
4178 u_int64_t xid;
4179 int attrflag, error = 0;
4180 struct nfsv3_pathconf *mpc;
1c79356b 4181
91447636
A
4182 /* fetch pathconf info from server */
4183 nfsm_reqhead(NFSX_FH(1));
4184 if (error)
4185 return (error);
4186 nfsm_fhtom(vp, 1);
4187 nfsm_request(vp, NFSPROC_PATHCONF, procp, cred, &xid);
4188 nfsm_postop_attr_update(vp, 1, attrflag, &xid);
4189 if (!error) {
4190 nfsm_dissect(mpc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
4191 pc->pc_linkmax = fxdr_unsigned(long, mpc->pc_linkmax);
4192 pc->pc_namemax = fxdr_unsigned(long, mpc->pc_namemax);
4193 pc->pc_chownrestricted = fxdr_unsigned(long, mpc->pc_chownrestricted);
4194 pc->pc_notrunc = fxdr_unsigned(long, mpc->pc_notrunc);
4195 pc->pc_caseinsensitive = fxdr_unsigned(long, mpc->pc_caseinsensitive);
4196 pc->pc_casepreserving = fxdr_unsigned(long, mpc->pc_casepreserving);
4197 }
4198 nfsm_reqdone;
1c79356b 4199
91447636 4200 return (error);
1c79356b
A
4201}
4202
91447636
A
4203void
4204nfs_pathconf_cache(struct nfsmount *nmp, struct nfsv3_pathconf *pc)
1c79356b 4205{
91447636
A
4206 nmp->nm_state |= NFSSTA_GOTPATHCONF;
4207 nmp->nm_fsinfo.linkmax = pc->pc_linkmax;
4208 nmp->nm_fsinfo.namemax = pc->pc_namemax;
4209 nmp->nm_fsinfo.pcflags = 0;
4210 if (pc->pc_notrunc)
4211 nmp->nm_fsinfo.pcflags |= NFSPCINFO_NOTRUNC;
4212 if (pc->pc_chownrestricted)
4213 nmp->nm_fsinfo.pcflags |= NFSPCINFO_CHOWN_RESTRICTED;
4214 if (pc->pc_caseinsensitive)
4215 nmp->nm_fsinfo.pcflags |= NFSPCINFO_CASE_INSENSITIVE;
4216 if (pc->pc_casepreserving)
4217 nmp->nm_fsinfo.pcflags |= NFSPCINFO_CASE_PRESERVING;
1c79356b
A
4218}
4219
4220/*
91447636
A
4221 * Return POSIX pathconf information applicable to nfs.
4222 *
4223 * The NFS V2 protocol doesn't support this, so just return EINVAL
4224 * for V2.
1c79356b 4225 */
91447636 4226/* ARGSUSED */
1c79356b 4227static int
91447636
A
4228nfs_pathconf(ap)
4229 struct vnop_pathconf_args /* {
4230 struct vnodeop_desc *a_desc;
4231 vnode_t a_vp;
4232 int a_name;
4233 register_t *a_retval;
4234 vfs_context_t a_context;
1c79356b
A
4235 } */ *ap;
4236{
91447636
A
4237 vnode_t vp = ap->a_vp;
4238 struct nfsmount *nmp;
4239 struct nfsv3_pathconf pc;
4240 int error = 0, cached;
1c79356b 4241
91447636
A
4242 nmp = VFSTONFS(vnode_mount(vp));
4243 if (!nmp)
4244 return (ENXIO);
4245 if (!NFS_ISV3(vp))
4246 return (EINVAL);
1c79356b 4247
91447636
A
4248 switch (ap->a_name) {
4249 case _PC_LINK_MAX:
4250 case _PC_NAME_MAX:
4251 case _PC_CHOWN_RESTRICTED:
4252 case _PC_NO_TRUNC:
4253 case _PC_CASE_SENSITIVE:
4254 case _PC_CASE_PRESERVING:
4255 break;
4256 default:
4257 /* don't bother contacting the server if we know the answer */
4258 return (EINVAL);
4259 }
1c79356b 4260
91447636
A
4261 if (!(nmp->nm_state & NFSSTA_GOTPATHCONF)) {
4262 /* no pathconf info cached */
4263 kauth_cred_t cred = vfs_context_ucred(ap->a_context);
4264 proc_t p = vfs_context_proc(ap->a_context);
4265 error = nfs_pathconfrpc(vp, &pc, cred, p);
4266 if (error)
4267 return (error);
4268 nmp = VFSTONFS(vnode_mount(vp));
4269 if (!nmp)
4270 return (ENXIO);
4271 if (!(nmp->nm_state & NFSSTA_GOTFSINFO)) {
4272 nfs_fsinfo(nmp, vp, cred, p);
4273 nmp = VFSTONFS(vnode_mount(vp));
4274 if (!nmp)
4275 return (ENXIO);
4276 }
4277 if ((nmp->nm_state & NFSSTA_GOTFSINFO) &&
4278 (nmp->nm_fsinfo.fsproperties & NFSV3FSINFO_HOMOGENEOUS)) {
4279 /* all files have the same pathconf info, */
4280 /* so cache a copy of the results */
4281 nfs_pathconf_cache(nmp, &pc);
4282 }
4283 }
1c79356b 4284
91447636
A
4285 cached = (nmp->nm_state & NFSSTA_GOTPATHCONF);
4286
4287 switch (ap->a_name) {
4288 case _PC_LINK_MAX:
4289 *ap->a_retval = cached ? nmp->nm_fsinfo.linkmax : pc.pc_linkmax;
4290 break;
4291 case _PC_NAME_MAX:
4292 *ap->a_retval = cached ? nmp->nm_fsinfo.namemax : pc.pc_namemax;
4293 break;
4294 case _PC_CHOWN_RESTRICTED:
4295 if (cached)
4296 *ap->a_retval = (nmp->nm_fsinfo.pcflags & NFSPCINFO_CHOWN_RESTRICTED) ? 1 : 0;
4297 else
4298 *ap->a_retval = pc.pc_chownrestricted;
4299 break;
4300 case _PC_NO_TRUNC:
4301 if (cached)
4302 *ap->a_retval = (nmp->nm_fsinfo.pcflags & NFSPCINFO_NOTRUNC) ? 1 : 0;
4303 else
4304 *ap->a_retval = pc.pc_notrunc;
4305 break;
4306 case _PC_CASE_SENSITIVE:
4307 if (cached)
4308 *ap->a_retval = (nmp->nm_fsinfo.pcflags & NFSPCINFO_CASE_INSENSITIVE) ? 0 : 1;
4309 else
4310 *ap->a_retval = !pc.pc_caseinsensitive;
4311 break;
4312 case _PC_CASE_PRESERVING:
4313 if (cached)
4314 *ap->a_retval = (nmp->nm_fsinfo.pcflags & NFSPCINFO_CASE_PRESERVING) ? 1 : 0;
4315 else
4316 *ap->a_retval = pc.pc_casepreserving;
4317 break;
4318 default:
4319 error = EINVAL;
4320 }
1c79356b 4321
91447636 4322 return (error);
1c79356b
A
4323}
4324
4325/*
91447636 4326 * NFS advisory byte-level locks (client)
1c79356b
A
4327 */
4328static int
91447636
A
4329nfs_advlock(ap)
4330 struct vnop_advlock_args /* {
4331 struct vnodeop_desc *a_desc;
4332 vnode_t a_vp;
4333 caddr_t a_id;
4334 int a_op;
4335 struct flock *a_fl;
1c79356b 4336 int a_flags;
91447636 4337 vfs_context_t a_context;
1c79356b
A
4338 } */ *ap;
4339{
91447636 4340 return (nfs_dolock(ap));
1c79356b
A
4341}
4342
1c79356b 4343/*
55e303ae 4344 * write (or commit) the given NFS buffer
1c79356b
A
4345 */
4346int
55e303ae 4347nfs_buf_write(struct nfsbuf *bp)
1c79356b 4348{
55e303ae 4349 int oldflags = bp->nb_flags, rv = 0;
91447636
A
4350 vnode_t vp = bp->nb_vp;
4351 struct nfsnode *np = VTONFS(vp);
4352 kauth_cred_t cr;
4353 proc_t p = current_proc(); // XXX
55e303ae
A
4354
4355 FSDBG_TOP(553, bp, NBOFF(bp), bp->nb_flags, 0);
1c79356b 4356
91447636 4357 if (!ISSET(bp->nb_lflags, NBL_BUSY))
55e303ae 4358 panic("nfs_buf_write: buffer is not busy???");
1c79356b 4359
55e303ae
A
4360 CLR(bp->nb_flags, (NB_READ|NB_DONE|NB_ERROR|NB_DELWRI));
4361 if (ISSET(oldflags, NB_DELWRI)) {
91447636
A
4362 OSAddAtomic(-1, (SInt32*)&nfs_nbdwrite);
4363 NFSBUFCNTCHK(0);
4364 wakeup(&nfs_nbdwrite);
d52fe63f 4365 }
1c79356b 4366
55e303ae
A
4367 /* move to clean list */
4368 if (ISSET(oldflags, (NB_ASYNC|NB_DELWRI))) {
91447636 4369 lck_mtx_lock(nfs_buf_mutex);
55e303ae
A
4370 if (bp->nb_vnbufs.le_next != NFSNOLIST)
4371 LIST_REMOVE(bp, nb_vnbufs);
4372 LIST_INSERT_HEAD(&VTONFS(vp)->n_cleanblkhd, bp, nb_vnbufs);
91447636 4373 lck_mtx_unlock(nfs_buf_mutex);
1c79356b 4374 }
91447636 4375 vnode_startwrite(vp);
1c79356b 4376
55e303ae
A
4377 if (p && p->p_stats)
4378 p->p_stats->p_ru.ru_oublock++;
1c79356b
A
4379
4380 /*
55e303ae
A
4381 * For async requests when nfsiod(s) are running, queue the request by
4382 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the request.
1c79356b 4383 */
55e303ae 4384 if (ISSET(bp->nb_flags, NB_ASYNC))
91447636 4385 p = NULL;
55e303ae
A
4386 if (ISSET(bp->nb_flags, NB_READ))
4387 cr = bp->nb_rcred;
4388 else
4389 cr = bp->nb_wcred;
4390 if (!ISSET(bp->nb_flags, NB_ASYNC) || nfs_asyncio(bp, NOCRED))
4391 rv = nfs_doio(bp, cr, p);
4392
4393 if ((oldflags & NB_ASYNC) == 0) {
4394 rv = nfs_buf_iowait(bp);
4395 /* move to clean list */
4396 if (oldflags & NB_DELWRI) {
91447636 4397 lck_mtx_lock(nfs_buf_mutex);
55e303ae
A
4398 if (bp->nb_vnbufs.le_next != NFSNOLIST)
4399 LIST_REMOVE(bp, nb_vnbufs);
4400 LIST_INSERT_HEAD(&VTONFS(vp)->n_cleanblkhd, bp, nb_vnbufs);
91447636 4401 lck_mtx_unlock(nfs_buf_mutex);
1c79356b 4402 }
91447636 4403 oldflags = bp->nb_flags;
55e303ae 4404 FSDBG_BOT(553, bp, NBOFF(bp), bp->nb_flags, rv);
6601e61a 4405 if (cr) {
91447636
A
4406 kauth_cred_ref(cr);
4407 }
483a1d10 4408 nfs_buf_release(bp, 1);
91447636
A
4409 if (ISSET(oldflags, NB_ERROR) && !(np->n_flag & NFLUSHINPROG)) {
4410 /*
4411 * There was a write error and we need to
4412 * invalidate attrs and flush buffers in
4413 * order to sync up with the server.
4414 * (if this write was extending the file,
4415 * we may no longer know the correct size)
4416 *
4417 * But we couldn't call vinvalbuf while holding
4418 * the buffer busy. So we call vinvalbuf() after
4419 * releasing the buffer.
4420 */
4421 nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, cr, p, 1);
4422 }
6601e61a
A
4423 if (cr)
4424 kauth_cred_rele(cr);
55e303ae 4425 return (rv);
1c79356b
A
4426 }
4427
55e303ae
A
4428 FSDBG_BOT(553, bp, NBOFF(bp), bp->nb_flags, rv);
4429 return (rv);
1c79356b
A
4430}
4431
1c79356b
A
4432/*
4433 * Read wrapper for special devices.
4434 */
4435static int
4436nfsspec_read(ap)
91447636
A
4437 struct vnop_read_args /* {
4438 struct vnodeop_desc *a_desc;
4439 vnode_t a_vp;
1c79356b 4440 struct uio *a_uio;
91447636
A
4441 int a_ioflag;
4442 vfs_context_t a_context;
1c79356b
A
4443 } */ *ap;
4444{
4445 register struct nfsnode *np = VTONFS(ap->a_vp);
55e303ae 4446 struct timeval now;
1c79356b
A
4447
4448 /*
4449 * Set access flag.
4450 */
4451 np->n_flag |= NACC;
55e303ae
A
4452 microtime(&now);
4453 np->n_atim.tv_sec = now.tv_sec;
4454 np->n_atim.tv_nsec = now.tv_usec * 1000;
91447636 4455 return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_read), ap));
1c79356b
A
4456}
4457
4458/*
4459 * Write wrapper for special devices.
4460 */
4461static int
4462nfsspec_write(ap)
91447636
A
4463 struct vnop_write_args /* {
4464 struct vnodeop_desc *a_desc;
4465 vnode_t a_vp;
1c79356b 4466 struct uio *a_uio;
91447636
A
4467 int a_ioflag;
4468 vfs_context_t a_context;
1c79356b
A
4469 } */ *ap;
4470{
4471 register struct nfsnode *np = VTONFS(ap->a_vp);
55e303ae 4472 struct timeval now;
1c79356b
A
4473
4474 /*
4475 * Set update flag.
4476 */
4477 np->n_flag |= NUPD;
55e303ae
A
4478 microtime(&now);
4479 np->n_mtim.tv_sec = now.tv_sec;
4480 np->n_mtim.tv_nsec = now.tv_usec * 1000;
91447636 4481 return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_write), ap));
1c79356b
A
4482}
4483
4484/*
4485 * Close wrapper for special devices.
4486 *
4487 * Update the times on the nfsnode then do device close.
4488 */
4489static int
4490nfsspec_close(ap)
91447636
A
4491 struct vnop_close_args /* {
4492 struct vnodeop_desc *a_desc;
4493 vnode_t a_vp;
4494 int a_fflag;
4495 vfs_context_t a_context;
1c79356b
A
4496 } */ *ap;
4497{
91447636
A
4498 vnode_t vp = ap->a_vp;
4499 struct nfsnode *np = VTONFS(vp);
4500 struct vnode_attr vattr;
4501 mount_t mp;
1c79356b
A
4502
4503 if (np->n_flag & (NACC | NUPD)) {
4504 np->n_flag |= NCHG;
91447636
A
4505 if (!vnode_isinuse(vp, 1) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) {
4506 VATTR_INIT(&vattr);
4507 if (np->n_flag & NACC) {
4508 vattr.va_access_time = np->n_atim;
4509 VATTR_SET_ACTIVE(&vattr, va_access_time);
4510 }
4511 if (np->n_flag & NUPD) {
4512 vattr.va_modify_time = np->n_mtim;
4513 VATTR_SET_ACTIVE(&vattr, va_modify_time);
4514 }
4515 vnode_setattr(vp, &vattr, ap->a_context);
1c79356b
A
4516 }
4517 }
91447636 4518 return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_close), ap));
1c79356b
A
4519}
4520
91447636
A
4521extern vnop_t **fifo_vnodeop_p;
4522
1c79356b
A
4523/*
4524 * Read wrapper for fifos.
4525 */
4526static int
4527nfsfifo_read(ap)
91447636
A
4528 struct vnop_read_args /* {
4529 struct vnodeop_desc *a_desc;
4530 vnode_t a_vp;
1c79356b 4531 struct uio *a_uio;
91447636
A
4532 int a_ioflag;
4533 vfs_context_t a_context;
1c79356b
A
4534 } */ *ap;
4535{
1c79356b 4536 register struct nfsnode *np = VTONFS(ap->a_vp);
55e303ae 4537 struct timeval now;
1c79356b
A
4538
4539 /*
4540 * Set access flag.
4541 */
4542 np->n_flag |= NACC;
55e303ae
A
4543 microtime(&now);
4544 np->n_atim.tv_sec = now.tv_sec;
4545 np->n_atim.tv_nsec = now.tv_usec * 1000;
91447636 4546 return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_read), ap));
1c79356b
A
4547}
4548
4549/*
4550 * Write wrapper for fifos.
4551 */
4552static int
4553nfsfifo_write(ap)
91447636
A
4554 struct vnop_write_args /* {
4555 struct vnodeop_desc *a_desc;
4556 vnode_t a_vp;
1c79356b 4557 struct uio *a_uio;
91447636
A
4558 int a_ioflag;
4559 vfs_context_t a_context;
1c79356b
A
4560 } */ *ap;
4561{
1c79356b 4562 register struct nfsnode *np = VTONFS(ap->a_vp);
55e303ae 4563 struct timeval now;
1c79356b
A
4564
4565 /*
4566 * Set update flag.
4567 */
4568 np->n_flag |= NUPD;
55e303ae
A
4569 microtime(&now);
4570 np->n_mtim.tv_sec = now.tv_sec;
4571 np->n_mtim.tv_nsec = now.tv_usec * 1000;
91447636 4572 return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_write), ap));
1c79356b
A
4573}
4574
4575/*
4576 * Close wrapper for fifos.
4577 *
4578 * Update the times on the nfsnode then do fifo close.
4579 */
4580static int
4581nfsfifo_close(ap)
91447636
A
4582 struct vnop_close_args /* {
4583 struct vnodeop_desc *a_desc;
4584 vnode_t a_vp;
4585 int a_fflag;
4586 vfs_context_t a_context;
1c79356b
A
4587 } */ *ap;
4588{
91447636
A
4589 vnode_t vp = ap->a_vp;
4590 struct nfsnode *np = VTONFS(vp);
4591 struct vnode_attr vattr;
55e303ae 4592 struct timeval now;
91447636 4593 mount_t mp;
1c79356b
A
4594
4595 if (np->n_flag & (NACC | NUPD)) {
55e303ae 4596 microtime(&now);
1c79356b 4597 if (np->n_flag & NACC) {
55e303ae
A
4598 np->n_atim.tv_sec = now.tv_sec;
4599 np->n_atim.tv_nsec = now.tv_usec * 1000;
1c79356b
A
4600 }
4601 if (np->n_flag & NUPD) {
55e303ae
A
4602 np->n_mtim.tv_sec = now.tv_sec;
4603 np->n_mtim.tv_nsec = now.tv_usec * 1000;
1c79356b
A
4604 }
4605 np->n_flag |= NCHG;
91447636
A
4606 if (!vnode_isinuse(vp, 1) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) {
4607 VATTR_INIT(&vattr);
4608 if (np->n_flag & NACC) {
4609 vattr.va_access_time = np->n_atim;
4610 VATTR_SET_ACTIVE(&vattr, va_access_time);
4611 }
4612 if (np->n_flag & NUPD) {
4613 vattr.va_modify_time = np->n_mtim;
4614 VATTR_SET_ACTIVE(&vattr, va_modify_time);
4615 }
4616 vnode_setattr(vp, &vattr, ap->a_context);
1c79356b
A
4617 }
4618 }
91447636 4619 return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_close), ap));
1c79356b
A
4620}
4621
91447636 4622/*ARGSUSED*/
1c79356b 4623static int
91447636
A
4624nfs_ioctl(
4625 __unused struct vnop_ioctl_args /* {
4626 struct vnodeop_desc *a_desc;
4627 vnode_t a_vp;
4628 u_long a_command;
4629 caddr_t a_data;
4630 int a_fflag;
4631 kauth_cred_t a_cred;
4632 proc_t a_p;
4633 } */ *ap)
1c79356b
A
4634{
4635
4636 /*
4637 * XXX we were once bogusly enoictl() which returned this (ENOTTY).
4638 * Probably we should return ENODEV.
4639 */
4640 return (ENOTTY);
4641}
4642
91447636 4643/*ARGSUSED*/
1c79356b 4644static int
91447636
A
4645nfs_select(
4646 __unused struct vnop_select_args /* {
4647 struct vnodeop_desc *a_desc;
4648 vnode_t a_vp;
4649 int a_which;
4650 int a_fflags;
4651 kauth_cred_t a_cred;
4652 void *a_wql;
4653 proc_t a_p;
4654 } */ *ap)
1c79356b
A
4655{
4656
4657 /*
4658 * We were once bogusly seltrue() which returns 1. Is this right?
4659 */
4660 return (1);
4661}
4662
1c79356b
A
4663/*
4664 * Vnode op for pagein using getblk_pages
4665 * derived from nfs_bioread()
4666 * No read aheads are started from pagein operation
4667 */
4668static int
4669nfs_pagein(ap)
91447636
A
4670 struct vnop_pagein_args /* {
4671 struct vnodeop_desc *a_desc;
4672 vnode_t a_vp;
4673 upl_t a_pl;
4674 vm_offset_t a_pl_offset;
4675 off_t a_f_offset;
4676 size_t a_size;
4677 int a_flags;
4678 vfs_context_t a_context;
1c79356b
A
4679 } */ *ap;
4680{
91447636 4681 vnode_t vp = ap->a_vp;
1c79356b
A
4682 upl_t pl = ap->a_pl;
4683 size_t size= ap->a_size;
4684 off_t f_offset = ap->a_f_offset;
4685 vm_offset_t pl_offset = ap->a_pl_offset;
4686 int flags = ap->a_flags;
91447636
A
4687 kauth_cred_t cred;
4688 proc_t p;
55e303ae
A
4689 struct nfsnode *np = VTONFS(vp);
4690 int biosize, xsize, iosize;
55e303ae 4691 struct nfsmount *nmp;
1c79356b
A
4692 int error = 0;
4693 vm_offset_t ioaddr;
4694 struct uio auio;
91447636 4695 struct iovec_32 aiov;
1c79356b 4696 struct uio * uio = &auio;
fa4905b1 4697 int nofreeupl = flags & UPL_NOCOMMIT;
55e303ae 4698 upl_page_info_t *plinfo;
1c79356b 4699
55e303ae 4700 FSDBG(322, vp, f_offset, size, flags);
fa4905b1
A
4701 if (pl == (upl_t)NULL)
4702 panic("nfs_pagein: no upl");
1c79356b
A
4703
4704 if (UBCINVALID(vp)) {
fa4905b1
A
4705 printf("nfs_pagein: invalid vnode 0x%x", (int)vp);
4706 if (!nofreeupl)
91447636 4707 (void) ubc_upl_abort(pl, 0);
1c79356b
A
4708 return (EPERM);
4709 }
1c79356b 4710 UBCINFOCHECK("nfs_pagein", vp);
1c79356b 4711
fa4905b1
A
4712 if (size <= 0) {
4713 printf("nfs_pagein: invalid size %d", size);
4714 if (!nofreeupl)
91447636 4715 (void) ubc_upl_abort(pl, 0);
1c79356b 4716 return (EINVAL);
fa4905b1 4717 }
91447636 4718 if (f_offset < 0 || f_offset >= (off_t)np->n_size || (f_offset & PAGE_MASK_64)) {
fa4905b1 4719 if (!nofreeupl)
0b4e3aa0 4720 ubc_upl_abort_range(pl, pl_offset, size,
1c79356b
A
4721 UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY);
4722 return (EINVAL);
4723 }
91447636 4724
fa4905b1 4725 cred = ubc_getcred(vp);
6601e61a 4726 if (cred == NOCRED)
91447636
A
4727 cred = vfs_context_ucred(ap->a_context);
4728 p = vfs_context_proc(ap->a_context);
1c79356b 4729
1c79356b 4730 auio.uio_offset = f_offset;
91447636 4731#if 1 /* LP64todo - can't use new segment flags until the drivers are ready */
1c79356b 4732 auio.uio_segflg = UIO_SYSSPACE;
91447636
A
4733#else
4734 auio.uio_segflg = UIO_SYSSPACE32;
4735#endif
1c79356b 4736 auio.uio_rw = UIO_READ;
91447636 4737 auio.uio_procp = p;
1c79356b 4738
91447636 4739 nmp = VFSTONFS(vnode_mount(vp));
55e303ae
A
4740 if (!nmp) {
4741 if (!nofreeupl)
4742 ubc_upl_abort_range(pl, pl_offset, size,
4743 UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY);
4744 return (ENXIO);
4745 }
4746 if ((nmp->nm_flag & NFSMNT_NFSV3) && !(nmp->nm_state & NFSSTA_GOTFSINFO))
6601e61a
A
4747 (void)nfs_fsinfo(nmp, vp, cred, p);
4748 biosize = vfs_statfs(vnode_mount(vp))->f_iosize;
1c79356b 4749
55e303ae 4750 plinfo = ubc_upl_pageinfo(pl);
0b4e3aa0 4751 ubc_upl_map(pl, &ioaddr);
1c79356b
A
4752 ioaddr += pl_offset;
4753 xsize = size;
4754
4755 do {
55e303ae
A
4756 /*
4757 * It would be nice to be able to issue all these requests
4758 * in parallel instead of waiting for each one to complete
4759 * before sending the next one.
4760 * XXX Should we align these requests to block boundaries?
4761 */
91447636 4762 iosize = min(biosize, xsize);
9bccf70c 4763 aiov.iov_len = iosize;
91447636
A
4764 aiov.iov_base = (uintptr_t)ioaddr;
4765 auio.uio_iovs.iov32p = &aiov;
55e303ae 4766 auio.uio_iovcnt = 1;
91447636 4767 uio_uio_resid_set(&auio, iosize);
1c79356b 4768
91447636 4769 FSDBG(322, uio->uio_offset, uio_uio_resid(uio), ioaddr, xsize);
1c79356b
A
4770 /*
4771 * With UBC we get here only when the file data is not in the VM
4772 * page cache, so go ahead and read in.
4773 */
91447636
A
4774#ifdef UPL_DEBUG
4775 upl_ubc_alias_set(pl, current_thread(), 2);
4776#endif /* UPL_DEBUG */
4777 OSAddAtomic(1, (SInt32*)&nfsstats.pageins);
9bccf70c 4778
91447636 4779 error = nfs_readrpc(vp, uio, cred, p);
1c79356b
A
4780
4781 if (!error) {
91447636 4782 if (uio_uio_resid(uio)) {
1c79356b 4783 /*
fa4905b1
A
4784 * If uio_resid > 0, there is a hole in the file
4785 * and no writes after the hole have been pushed
4786 * to the server yet... or we're at the EOF
1c79356b
A
4787 * Just zero fill the rest of the valid area.
4788 */
91447636
A
4789 // LP64todo - fix this
4790 int zcnt = uio_uio_resid(uio);
9bccf70c 4791 int zoff = iosize - zcnt;
1c79356b
A
4792 bzero((char *)ioaddr + zoff, zcnt);
4793
fa4905b1 4794 FSDBG(324, uio->uio_offset, zoff, zcnt, ioaddr);
1c79356b
A
4795 uio->uio_offset += zcnt;
4796 }
9bccf70c
A
4797 ioaddr += iosize;
4798 xsize -= iosize;
91447636
A
4799 } else {
4800 FSDBG(322, uio->uio_offset, uio_uio_resid(uio), error, -1);
1c79356b
A
4801 }
4802
91447636 4803 nmp = VFSTONFS(vnode_mount(vp));
1c79356b
A
4804 } while (error == 0 && xsize > 0);
4805
0b4e3aa0 4806 ubc_upl_unmap(pl);
1c79356b 4807
fa4905b1 4808 if (!nofreeupl) {
1c79356b 4809 if (error)
0b4e3aa0 4810 ubc_upl_abort_range(pl, pl_offset, size,
fa4905b1
A
4811 UPL_ABORT_ERROR |
4812 UPL_ABORT_FREE_ON_EMPTY);
1c79356b 4813 else
0b4e3aa0 4814 ubc_upl_commit_range(pl, pl_offset, size,
fa4905b1
A
4815 UPL_COMMIT_CLEAR_DIRTY |
4816 UPL_COMMIT_FREE_ON_EMPTY);
1c79356b 4817 }
1c79356b
A
4818 return (error);
4819}
4820
0b4e3aa0 4821
1c79356b
A
4822/*
4823 * Vnode op for pageout using UPL
4824 * Derived from nfs_write()
4825 * File size changes are not permitted in pageout.
4826 */
4827static int
4828nfs_pageout(ap)
91447636
A
4829 struct vnop_pageout_args /* {
4830 struct vnodeop_desc *a_desc;
4831 vnode_t a_vp;
4832 upl_t a_pl;
4833 vm_offset_t a_pl_offset;
4834 off_t a_f_offset;
4835 size_t a_size;
4836 int a_flags;
4837 vfs_context_t a_context;
1c79356b
A
4838 } */ *ap;
4839{
91447636 4840 vnode_t vp = ap->a_vp;
1c79356b
A
4841 upl_t pl = ap->a_pl;
4842 size_t size= ap->a_size;
4843 off_t f_offset = ap->a_f_offset;
4844 vm_offset_t pl_offset = ap->a_pl_offset;
4845 int flags = ap->a_flags;
1c79356b 4846 struct nfsnode *np = VTONFS(vp);
91447636
A
4847 kauth_cred_t cred;
4848 proc_t p;
55e303ae 4849 struct nfsbuf *bp;
91447636
A
4850 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp));
4851 daddr64_t lbn;
8f6c56a5 4852 int error = 0, iomode;
1c79356b
A
4853 off_t off;
4854 vm_offset_t ioaddr;
4855 struct uio auio;
91447636 4856 struct iovec_32 aiov;
fa4905b1 4857 int nofreeupl = flags & UPL_NOCOMMIT;
91447636 4858 size_t biosize, iosize, pgsize, xsize;
1c79356b 4859
fa4905b1
A
4860 FSDBG(323, f_offset, size, pl, pl_offset);
4861
4862 if (pl == (upl_t)NULL)
4863 panic("nfs_pageout: no upl");
1c79356b
A
4864
4865 if (UBCINVALID(vp)) {
fa4905b1
A
4866 printf("nfs_pageout: invalid vnode 0x%x", (int)vp);
4867 if (!nofreeupl)
55e303ae 4868 ubc_upl_abort(pl, 0);
1c79356b
A
4869 return (EIO);
4870 }
4871 UBCINFOCHECK("nfs_pageout", vp);
4872
fa4905b1
A
4873 if (size <= 0) {
4874 printf("nfs_pageout: invalid size %d", size);
4875 if (!nofreeupl)
55e303ae 4876 ubc_upl_abort(pl, 0);
1c79356b 4877 return (EINVAL);
1c79356b
A
4878 }
4879
55e303ae
A
4880 if (!nmp) {
4881 if (!nofreeupl)
4882 ubc_upl_abort(pl, UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY);
4883 return (ENXIO);
4884 }
6601e61a 4885 biosize = vfs_statfs(vnode_mount(vp))->f_iosize;
1c79356b 4886
1c79356b 4887 /*
55e303ae
A
4888 * Check to see whether the buffer is incore.
4889 * If incore and not busy, invalidate it from the cache.
1c79356b 4890 */
55e303ae
A
4891 for (iosize = 0; iosize < size; iosize += xsize) {
4892 off = f_offset + iosize;
4893 /* need make sure we do things on block boundaries */
4894 xsize = biosize - (off % biosize);
4895 if (off + xsize > f_offset + size)
4896 xsize = f_offset + size - off;
4897 lbn = ubc_offtoblk(vp, off);
91447636
A
4898 lck_mtx_lock(nfs_buf_mutex);
4899 if ((bp = nfs_buf_incore(vp, lbn))) {
4900 FSDBG(323, off, bp, bp->nb_lflags, bp->nb_flags);
4901 if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) {
4902 lck_mtx_unlock(nfs_buf_mutex);
fa4905b1
A
4903 /* no panic. just tell vm we are busy */
4904 if (!nofreeupl)
55e303ae
A
4905 ubc_upl_abort(pl, 0);
4906 return (EBUSY);
4907 }
4908 if (bp->nb_dirtyend > 0) {
4909 /*
91447636
A
4910 * if there's a dirty range in the buffer, check
4911 * to see if it extends beyond the pageout region
55e303ae
A
4912 *
4913 * if the dirty region lies completely within the
4914 * pageout region, we just invalidate the buffer
4915 * because it's all being written out now anyway.
4916 *
4917 * if any of the dirty region lies outside the
4918 * pageout region, we'll try to clip the dirty
4919 * region to eliminate the portion that's being
4920 * paged out. If that's not possible, because
4921 * the dirty region extends before and after the
4922 * pageout region, then we'll just return EBUSY.
4923 */
4924 off_t boff, start, end;
4925 boff = NBOFF(bp);
4926 start = off;
4927 end = off + xsize;
4928 /* clip end to EOF */
91447636 4929 if (end > (off_t)np->n_size)
55e303ae
A
4930 end = np->n_size;
4931 start -= boff;
4932 end -= boff;
4933 if ((bp->nb_dirtyoff < start) &&
4934 (bp->nb_dirtyend > end)) {
4935 /* not gonna be able to clip the dirty region */
4936 FSDBG(323, vp, bp, 0xd00deebc, EBUSY);
91447636
A
4937 nfs_buf_drop(bp);
4938 lck_mtx_unlock(nfs_buf_mutex);
55e303ae
A
4939 if (!nofreeupl)
4940 ubc_upl_abort(pl, 0);
4941 return (EBUSY);
4942 }
4943 if ((bp->nb_dirtyoff < start) ||
4944 (bp->nb_dirtyend > end)) {
4945 /* clip dirty region, if necessary */
4946 if (bp->nb_dirtyoff < start)
4947 bp->nb_dirtyend = min(bp->nb_dirtyend, start);
4948 if (bp->nb_dirtyend > end)
4949 bp->nb_dirtyoff = max(bp->nb_dirtyoff, end);
4950 FSDBG(323, bp, bp->nb_dirtyoff, bp->nb_dirtyend, 0xd00dee00);
4951 /* we're leaving this block dirty */
91447636
A
4952 nfs_buf_drop(bp);
4953 lck_mtx_unlock(nfs_buf_mutex);
55e303ae
A
4954 continue;
4955 }
4956 }
4957 nfs_buf_remfree(bp);
91447636
A
4958 lck_mtx_unlock(nfs_buf_mutex);
4959 SET(bp->nb_flags, NB_INVAL);
55e303ae
A
4960 if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
4961 CLR(bp->nb_flags, NB_NEEDCOMMIT);
4962 np->n_needcommitcnt--;
4963 CHECK_NEEDCOMMITCNT(np);
fa4905b1 4964 }
483a1d10 4965 nfs_buf_release(bp, 1);
91447636
A
4966 } else {
4967 lck_mtx_unlock(nfs_buf_mutex);
1c79356b 4968 }
1c79356b
A
4969 }
4970
4971 cred = ubc_getcred(vp);
6601e61a 4972 if (cred == NOCRED)
91447636
A
4973 cred = vfs_context_ucred(ap->a_context);
4974 p = vfs_context_proc(ap->a_context);
1c79356b
A
4975
4976 if (np->n_flag & NWRITEERR) {
4977 np->n_flag &= ~NWRITEERR;
fa4905b1
A
4978 if (!nofreeupl)
4979 ubc_upl_abort_range(pl, pl_offset, size,
4980 UPL_ABORT_FREE_ON_EMPTY);
1c79356b
A
4981 return (np->n_error);
4982 }
91447636
A
4983 if ((nmp->nm_flag & NFSMNT_NFSV3) && !(nmp->nm_state & NFSSTA_GOTFSINFO))
4984 nfs_fsinfo(nmp, vp, cred, p);
1c79356b 4985
91447636 4986 if (f_offset < 0 || f_offset >= (off_t)np->n_size ||
55e303ae 4987 f_offset & PAGE_MASK_64 || size & PAGE_MASK_64) {
fa4905b1
A
4988 if (!nofreeupl)
4989 ubc_upl_abort_range(pl, pl_offset, size,
4990 UPL_ABORT_FREE_ON_EMPTY);
1c79356b
A
4991 return (EINVAL);
4992 }
4993
0b4e3aa0 4994 ubc_upl_map(pl, &ioaddr);
55e303ae 4995 ioaddr += pl_offset;
1c79356b 4996
91447636 4997 if ((u_quad_t)f_offset + size > np->n_size)
55e303ae 4998 xsize = np->n_size - f_offset;
1c79356b 4999 else
55e303ae 5000 xsize = size;
1c79356b 5001
55e303ae 5002 pgsize = round_page_64(xsize);
1c79356b 5003 if (size > pgsize) {
fa4905b1
A
5004 if (!nofreeupl)
5005 ubc_upl_abort_range(pl, pl_offset + pgsize,
5006 size - pgsize,
5007 UPL_ABORT_FREE_ON_EMPTY);
1c79356b 5008 }
1c79356b 5009
1c79356b
A
5010 /*
5011 * check for partial page and clear the
5012 * contents past end of the file before
5013 * releasing it in the VM page cache
5014 */
91447636 5015 if ((u_quad_t)f_offset < np->n_size && (u_quad_t)f_offset + size > np->n_size) {
1c79356b 5016 size_t io = np->n_size - f_offset;
55e303ae 5017 bzero((caddr_t)(ioaddr + io), size - io);
fa4905b1 5018 FSDBG(321, np->n_size, f_offset, f_offset + io, size - io);
1c79356b
A
5019 }
5020
55e303ae 5021 auio.uio_offset = f_offset;
91447636 5022#if 1 /* LP64todo - can't use new segment flags until the drivers are ready */
55e303ae 5023 auio.uio_segflg = UIO_SYSSPACE;
91447636
A
5024#else
5025 auio.uio_segflg = UIO_SYSSPACE32;
5026#endif
55e303ae 5027 auio.uio_rw = UIO_READ;
91447636 5028 auio.uio_procp = p;
55e303ae 5029
1c79356b 5030 do {
55e303ae
A
5031 /*
5032 * It would be nice to be able to issue all these requests
5033 * in parallel instead of waiting for each one to complete
5034 * before sending the next one.
5035 * XXX Should we align these requests to block boundaries?
5036 */
5037 iosize = min(biosize, xsize);
91447636 5038 uio_uio_resid_set(&auio, iosize);
55e303ae 5039 aiov.iov_len = iosize;
91447636
A
5040 aiov.iov_base = (uintptr_t)ioaddr;
5041 auio.uio_iovs.iov32p = &aiov;
55e303ae
A
5042 auio.uio_iovcnt = 1;
5043
91447636
A
5044 FSDBG(323, auio.uio_offset, uio_uio_resid(&auio), ioaddr, xsize);
5045 OSAddAtomic(1, (SInt32*)&nfsstats.pageouts);
5046
5047 vnode_startwrite(vp);
55e303ae 5048
fa4905b1 5049 /* NMODIFIED would be set here if doing unstable writes */
1c79356b 5050 iomode = NFSV3WRITE_FILESYNC;
8f6c56a5 5051 error = nfs_writerpc(vp, &auio, cred, p, &iomode, NULL);
91447636 5052 vnode_writedone(vp);
1c79356b
A
5053 if (error)
5054 goto cleanup;
55e303ae
A
5055 /* Note: no need to check uio_resid, because */
5056 /* it'll only be set if there was an error. */
5057 ioaddr += iosize;
5058 xsize -= iosize;
5059 } while (xsize > 0);
1c79356b
A
5060
5061cleanup:
0b4e3aa0
A
5062 ubc_upl_unmap(pl);
5063 /*
5064 * We've had several different solutions on what to do when the pageout
5065 * gets an error. If we don't handle it, and return an error to the
5066 * caller, vm, it will retry . This can end in endless looping
5067 * between vm and here doing retries of the same page. Doing a dump
5068 * back to vm, will get it out of vm's knowledge and we lose whatever
5069 * data existed. This is risky, but in some cases necessary. For
5070 * example, the initial fix here was to do that for ESTALE. In that case
5071 * the server is telling us that the file is no longer the same. We
5072 * would not want to keep paging out to that. We also saw some 151
5073 * errors from Auspex server and NFSv3 can return errors higher than
fa4905b1
A
5074 * ELAST. Those along with NFS known server errors we will "dump" from
5075 * vm. Errors we don't expect to occur, we dump and log for further
0b4e3aa0
A
5076 * analysis. Errors that could be transient, networking ones,
5077 * we let vm "retry". Lastly, errors that we retry, but may have potential
5078 * to storm the network, we "retrywithsleep". "sever" will be used in
5079 * in the future to dump all pages of object for cases like ESTALE.
5080 * All this is the basis for the states returned and first guesses on
5081 * error handling. Tweaking expected as more statistics are gathered.
5082 * Note, in the long run we may need another more robust solution to
5083 * have some kind of persistant store when the vm cannot dump nor keep
fa4905b1 5084 * retrying as a solution, but this would be a file architectural change
0b4e3aa0
A
5085 */
5086
fa4905b1 5087 if (!nofreeupl) { /* otherwise stacked file system has to handle this */
0b4e3aa0 5088 if (error) {
91447636 5089 int abortflags = 0;
0b4e3aa0
A
5090 short action = nfs_pageouterrorhandler(error);
5091
5092 switch (action) {
5093 case DUMP:
5094 abortflags = UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY;
5095 break;
5096 case DUMPANDLOG:
5097 abortflags = UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY;
fa4905b1
A
5098 if (error <= ELAST &&
5099 (errorcount[error] % 100 == 0))
0b4e3aa0
A
5100 printf("nfs_pageout: unexpected error %d. dumping vm page\n", error);
5101 errorcount[error]++;
5102 break;
5103 case RETRY:
5104 abortflags = UPL_ABORT_FREE_ON_EMPTY;
5105 break;
5106 case RETRYWITHSLEEP:
5107 abortflags = UPL_ABORT_FREE_ON_EMPTY;
fa4905b1 5108 /* pri unused. PSOCK for placeholder. */
91447636 5109 tsleep(&lbolt, PSOCK, "nfspageout", 0);
0b4e3aa0
A
5110 break;
5111 case SEVER: /* not implemented */
5112 default:
5113 printf("nfs_pageout: action %d not expected\n", action);
5114 break;
5115 }
5116
5117 ubc_upl_abort_range(pl, pl_offset, size, abortflags);
5118 /* return error in all cases above */
5119
5120 } else
5121 ubc_upl_commit_range(pl, pl_offset, pgsize,
fa4905b1
A
5122 UPL_COMMIT_CLEAR_DIRTY |
5123 UPL_COMMIT_FREE_ON_EMPTY);
1c79356b 5124 }
1c79356b
A
5125 return (error);
5126}
5127
5128/* Blktooff derives file offset given a logical block number */
5129static int
5130nfs_blktooff(ap)
91447636
A
5131 struct vnop_blktooff_args /* {
5132 struct vnodeop_desc *a_desc;
5133 vnode_t a_vp;
5134 daddr64_t a_lblkno;
5135 off_t *a_offset;
1c79356b
A
5136 } */ *ap;
5137{
5138 int biosize;
91447636 5139 vnode_t vp = ap->a_vp;
6601e61a 5140 mount_t mp = vnode_mount(vp);
1c79356b 5141
6601e61a 5142 if (!mp)
55e303ae 5143 return (ENXIO);
6601e61a
A
5144
5145 biosize = vfs_statfs(mp)->f_iosize;
1c79356b 5146
91447636 5147 *ap->a_offset = (off_t)(ap->a_lblkno * biosize);
1c79356b
A
5148
5149 return (0);
5150}
5151
1c79356b
A
5152static int
5153nfs_offtoblk(ap)
91447636
A
5154 struct vnop_offtoblk_args /* {
5155 struct vnodeop_desc *a_desc;
5156 vnode_t a_vp;
5157 off_t a_offset;
5158 daddr64_t *a_lblkno;
1c79356b
A
5159 } */ *ap;
5160{
5161 int biosize;
91447636 5162 vnode_t vp = ap->a_vp;
6601e61a 5163 mount_t mp = vnode_mount(vp);
1c79356b 5164
6601e61a 5165 if (!mp)
55e303ae 5166 return (ENXIO);
6601e61a
A
5167
5168 biosize = vfs_statfs(mp)->f_iosize;
1c79356b 5169
91447636 5170 *ap->a_lblkno = (daddr64_t)(ap->a_offset / biosize);
1c79356b
A
5171
5172 return (0);
5173}
91447636 5174