nfs_vnops.c revision 1.266.10.5 1 /* $NetBSD: nfs_vnops.c,v 1.266.10.5 2009/07/18 14:53:26 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)nfs_vnops.c 8.19 (Berkeley) 7/31/95
35 */
36
37 /*
38 * vnode op calls for Sun NFS version 2 and 3
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: nfs_vnops.c,v 1.266.10.5 2009/07/18 14:53:26 yamt Exp $");
43
44 #ifdef _KERNEL_OPT
45 #include "opt_nfs.h"
46 #include "opt_uvmhist.h"
47 #endif
48
49 #include <sys/param.h>
50 #include <sys/proc.h>
51 #include <sys/kernel.h>
52 #include <sys/systm.h>
53 #include <sys/resourcevar.h>
54 #include <sys/mount.h>
55 #include <sys/buf.h>
56 #include <sys/condvar.h>
57 #include <sys/disk.h>
58 #include <sys/malloc.h>
59 #include <sys/kmem.h>
60 #include <sys/mbuf.h>
61 #include <sys/mutex.h>
62 #include <sys/namei.h>
63 #include <sys/vnode.h>
64 #include <sys/dirent.h>
65 #include <sys/fcntl.h>
66 #include <sys/hash.h>
67 #include <sys/lockf.h>
68 #include <sys/stat.h>
69 #include <sys/unistd.h>
70 #include <sys/kauth.h>
71
72 #include <uvm/uvm_extern.h>
73 #include <uvm/uvm.h>
74
75 #include <miscfs/fifofs/fifo.h>
76 #include <miscfs/genfs/genfs.h>
77 #include <miscfs/genfs/genfs_node.h>
78 #include <miscfs/specfs/specdev.h>
79
80 #include <nfs/rpcv2.h>
81 #include <nfs/nfsproto.h>
82 #include <nfs/nfs.h>
83 #include <nfs/nfsnode.h>
84 #include <nfs/nfsmount.h>
85 #include <nfs/xdr_subs.h>
86 #include <nfs/nfsm_subs.h>
87 #include <nfs/nfs_var.h>
88
89 #include <net/if.h>
90 #include <netinet/in.h>
91 #include <netinet/in_var.h>
92
93 /*
94 * Global vfs data structures for nfs
95 */
96 int (**nfsv2_vnodeop_p)(void *);
97 const struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
98 { &vop_default_desc, vn_default_error },
99 { &vop_lookup_desc, nfs_lookup }, /* lookup */
100 { &vop_create_desc, nfs_create }, /* create */
101 { &vop_mknod_desc, nfs_mknod }, /* mknod */
102 { &vop_open_desc, nfs_open }, /* open */
103 { &vop_close_desc, nfs_close }, /* close */
104 { &vop_access_desc, nfs_access }, /* access */
105 { &vop_getattr_desc, nfs_getattr }, /* getattr */
106 { &vop_setattr_desc, nfs_setattr }, /* setattr */
107 { &vop_read_desc, nfs_read }, /* read */
108 { &vop_write_desc, nfs_write }, /* write */
109 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
110 { &vop_ioctl_desc, nfs_ioctl }, /* ioctl */
111 { &vop_poll_desc, nfs_poll }, /* poll */
112 { &vop_kqfilter_desc, nfs_kqfilter }, /* kqfilter */
113 { &vop_revoke_desc, nfs_revoke }, /* revoke */
114 { &vop_mmap_desc, nfs_mmap }, /* mmap */
115 { &vop_fsync_desc, nfs_fsync }, /* fsync */
116 { &vop_seek_desc, nfs_seek }, /* seek */
117 { &vop_remove_desc, nfs_remove }, /* remove */
118 { &vop_link_desc, nfs_link }, /* link */
119 { &vop_rename_desc, nfs_rename }, /* rename */
120 { &vop_mkdir_desc, nfs_mkdir }, /* mkdir */
121 { &vop_rmdir_desc, nfs_rmdir }, /* rmdir */
122 { &vop_symlink_desc, nfs_symlink }, /* symlink */
123 { &vop_readdir_desc, nfs_readdir }, /* readdir */
124 { &vop_readlink_desc, nfs_readlink }, /* readlink */
125 { &vop_abortop_desc, nfs_abortop }, /* abortop */
126 { &vop_inactive_desc, nfs_inactive }, /* inactive */
127 { &vop_reclaim_desc, nfs_reclaim }, /* reclaim */
128 { &vop_lock_desc, nfs_lock }, /* lock */
129 { &vop_unlock_desc, nfs_unlock }, /* unlock */
130 { &vop_bmap_desc, nfs_bmap }, /* bmap */
131 { &vop_strategy_desc, nfs_strategy }, /* strategy */
132 { &vop_print_desc, nfs_print }, /* print */
133 { &vop_islocked_desc, nfs_islocked }, /* islocked */
134 { &vop_pathconf_desc, nfs_pathconf }, /* pathconf */
135 { &vop_advlock_desc, nfs_advlock }, /* advlock */
136 { &vop_bwrite_desc, genfs_badop }, /* bwrite */
137 { &vop_getpages_desc, nfs_getpages }, /* getpages */
138 { &vop_putpages_desc, genfs_putpages }, /* putpages */
139 { NULL, NULL }
140 };
141 const struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
142 { &nfsv2_vnodeop_p, nfsv2_vnodeop_entries };
143
144 /*
145 * Special device vnode ops
146 */
147 int (**spec_nfsv2nodeop_p)(void *);
148 const struct vnodeopv_entry_desc spec_nfsv2nodeop_entries[] = {
149 { &vop_default_desc, vn_default_error },
150 { &vop_lookup_desc, spec_lookup }, /* lookup */
151 { &vop_create_desc, spec_create }, /* create */
152 { &vop_mknod_desc, spec_mknod }, /* mknod */
153 { &vop_open_desc, spec_open }, /* open */
154 { &vop_close_desc, nfsspec_close }, /* close */
155 { &vop_access_desc, nfsspec_access }, /* access */
156 { &vop_getattr_desc, nfs_getattr }, /* getattr */
157 { &vop_setattr_desc, nfs_setattr }, /* setattr */
158 { &vop_read_desc, nfsspec_read }, /* read */
159 { &vop_write_desc, nfsspec_write }, /* write */
160 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
161 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
162 { &vop_poll_desc, spec_poll }, /* poll */
163 { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
164 { &vop_revoke_desc, spec_revoke }, /* revoke */
165 { &vop_mmap_desc, spec_mmap }, /* mmap */
166 { &vop_fsync_desc, spec_fsync }, /* fsync */
167 { &vop_seek_desc, spec_seek }, /* seek */
168 { &vop_remove_desc, spec_remove }, /* remove */
169 { &vop_link_desc, spec_link }, /* link */
170 { &vop_rename_desc, spec_rename }, /* rename */
171 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
172 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
173 { &vop_symlink_desc, spec_symlink }, /* symlink */
174 { &vop_readdir_desc, spec_readdir }, /* readdir */
175 { &vop_readlink_desc, spec_readlink }, /* readlink */
176 { &vop_abortop_desc, spec_abortop }, /* abortop */
177 { &vop_inactive_desc, nfs_inactive }, /* inactive */
178 { &vop_reclaim_desc, nfs_reclaim }, /* reclaim */
179 { &vop_lock_desc, nfs_lock }, /* lock */
180 { &vop_unlock_desc, nfs_unlock }, /* unlock */
181 { &vop_bmap_desc, spec_bmap }, /* bmap */
182 { &vop_strategy_desc, spec_strategy }, /* strategy */
183 { &vop_print_desc, nfs_print }, /* print */
184 { &vop_islocked_desc, nfs_islocked }, /* islocked */
185 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
186 { &vop_advlock_desc, spec_advlock }, /* advlock */
187 { &vop_bwrite_desc, spec_bwrite }, /* bwrite */
188 { &vop_getpages_desc, spec_getpages }, /* getpages */
189 { &vop_putpages_desc, spec_putpages }, /* putpages */
190 { NULL, NULL }
191 };
192 const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
193 { &spec_nfsv2nodeop_p, spec_nfsv2nodeop_entries };
194
195 int (**fifo_nfsv2nodeop_p)(void *);
196 const struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries[] = {
197 { &vop_default_desc, vn_default_error },
198 { &vop_lookup_desc, fifo_lookup }, /* lookup */
199 { &vop_create_desc, fifo_create }, /* create */
200 { &vop_mknod_desc, fifo_mknod }, /* mknod */
201 { &vop_open_desc, fifo_open }, /* open */
202 { &vop_close_desc, nfsfifo_close }, /* close */
203 { &vop_access_desc, nfsspec_access }, /* access */
204 { &vop_getattr_desc, nfs_getattr }, /* getattr */
205 { &vop_setattr_desc, nfs_setattr }, /* setattr */
206 { &vop_read_desc, nfsfifo_read }, /* read */
207 { &vop_write_desc, nfsfifo_write }, /* write */
208 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
209 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
210 { &vop_poll_desc, fifo_poll }, /* poll */
211 { &vop_kqfilter_desc, fifo_kqfilter }, /* kqfilter */
212 { &vop_revoke_desc, fifo_revoke }, /* revoke */
213 { &vop_mmap_desc, fifo_mmap }, /* mmap */
214 { &vop_fsync_desc, nfs_fsync }, /* fsync */
215 { &vop_seek_desc, fifo_seek }, /* seek */
216 { &vop_remove_desc, fifo_remove }, /* remove */
217 { &vop_link_desc, fifo_link }, /* link */
218 { &vop_rename_desc, fifo_rename }, /* rename */
219 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
220 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
221 { &vop_symlink_desc, fifo_symlink }, /* symlink */
222 { &vop_readdir_desc, fifo_readdir }, /* readdir */
223 { &vop_readlink_desc, fifo_readlink }, /* readlink */
224 { &vop_abortop_desc, fifo_abortop }, /* abortop */
225 { &vop_inactive_desc, nfs_inactive }, /* inactive */
226 { &vop_reclaim_desc, nfs_reclaim }, /* reclaim */
227 { &vop_lock_desc, nfs_lock }, /* lock */
228 { &vop_unlock_desc, nfs_unlock }, /* unlock */
229 { &vop_bmap_desc, fifo_bmap }, /* bmap */
230 { &vop_strategy_desc, genfs_badop }, /* strategy */
231 { &vop_print_desc, nfs_print }, /* print */
232 { &vop_islocked_desc, nfs_islocked }, /* islocked */
233 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
234 { &vop_advlock_desc, fifo_advlock }, /* advlock */
235 { &vop_bwrite_desc, genfs_badop }, /* bwrite */
236 { &vop_putpages_desc, fifo_putpages }, /* putpages */
237 { NULL, NULL }
238 };
239 const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
240 { &fifo_nfsv2nodeop_p, fifo_nfsv2nodeop_entries };
241
242 static int nfs_linkrpc(struct vnode *, struct vnode *, const char *,
243 size_t, kauth_cred_t, struct lwp *);
244 static void nfs_writerpc_extfree(struct mbuf *, void *, size_t, void *);
245
246 /*
247 * Global variables
248 */
249 extern u_int32_t nfs_true, nfs_false;
250 extern u_int32_t nfs_xdrneg1;
251 extern const nfstype nfsv3_type[9];
252
253 int nfs_numasync = 0;
254 #define DIRHDSIZ _DIRENT_NAMEOFF(dp)
255 #define UIO_ADVANCE(uio, siz) \
256 (void)((uio)->uio_resid -= (siz), \
257 (uio)->uio_iov->iov_base = (char *)(uio)->uio_iov->iov_base + (siz), \
258 (uio)->uio_iov->iov_len -= (siz))
259
260 static void nfs_cache_enter(struct vnode *, struct vnode *,
261 struct componentname *);
262
263 static void
264 nfs_cache_enter(struct vnode *dvp, struct vnode *vp,
265 struct componentname *cnp)
266 {
267 struct nfsnode *dnp = VTONFS(dvp);
268
269 if (vp != NULL) {
270 struct nfsnode *np = VTONFS(vp);
271
272 np->n_ctime = np->n_vattr->va_ctime.tv_sec;
273 }
274
275 if (!timespecisset(&dnp->n_nctime))
276 dnp->n_nctime = dnp->n_vattr->va_mtime;
277
278 cache_enter(dvp, vp, cnp);
279 }
280
281 /*
282 * nfs null call from vfs.
283 */
284 int
285 nfs_null(struct vnode *vp, kauth_cred_t cred, struct lwp *l)
286 {
287 char *bpos, *dpos;
288 int error = 0;
289 struct mbuf *mreq, *mrep, *md, *mb;
290 struct nfsnode *np = VTONFS(vp);
291
292 nfsm_reqhead(np, NFSPROC_NULL, 0);
293 nfsm_request(np, NFSPROC_NULL, l, cred);
294 nfsm_reqdone;
295 return (error);
296 }
297
298 /*
299 * nfs access vnode op.
300 * For nfs version 2, just return ok. File accesses may fail later.
301 * For nfs version 3, use the access rpc to check accessibility. If file modes
302 * are changed on the server, accesses might still fail later.
303 */
304 int
305 nfs_access(void *v)
306 {
307 struct vop_access_args /* {
308 struct vnode *a_vp;
309 int a_mode;
310 kauth_cred_t a_cred;
311 } */ *ap = v;
312 struct vnode *vp = ap->a_vp;
313 #ifndef NFS_V2_ONLY
314 u_int32_t *tl;
315 char *cp;
316 int32_t t1, t2;
317 char *bpos, *dpos, *cp2;
318 int error = 0, attrflag;
319 struct mbuf *mreq, *mrep, *md, *mb;
320 u_int32_t mode, rmode;
321 const int v3 = NFS_ISV3(vp);
322 #endif
323 int cachevalid;
324 struct nfsnode *np = VTONFS(vp);
325 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
326 uid_t uid = kauth_cred_geteuid(ap->a_cred);
327 int result;
328
329 mutex_enter(&np->n_attrlock);
330 cachevalid = np->n_accstamp != -1 &&
331 (time_uptime - np->n_accstamp) < nfs_attrtimeo(nmp, np) &&
332 np->n_accuid == uid;
333
334 /*
335 * Check access cache first. If this request has been made for this
336 * uid shortly before, use the cached result.
337 */
338 if (cachevalid) {
339 if (!np->n_accerror) {
340 if ((np->n_accmode & ap->a_mode) == ap->a_mode) {
341 result = np->n_accerror;
342 mutex_exit(&np->n_attrlock);
343 return result;
344 }
345 } else if ((np->n_accmode & ap->a_mode) == np->n_accmode) {
346 result = np->n_accerror;
347 mutex_exit(&np->n_attrlock);
348 return result;
349 }
350 }
351 mutex_exit(&np->n_attrlock);
352
353 #ifndef NFS_V2_ONLY
354 /*
355 * For nfs v3, do an access rpc, otherwise you are stuck emulating
356 * ufs_access() locally using the vattr. This may not be correct,
357 * since the server may apply other access criteria such as
358 * client uid-->server uid mapping that we do not know about, but
359 * this is better than just returning anything that is lying about
360 * in the cache.
361 */
362 if (v3) {
363 nfsstats.rpccnt[NFSPROC_ACCESS]++;
364 nfsm_reqhead(np, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
365 nfsm_fhtom(np, v3);
366 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
367 if (ap->a_mode & VREAD)
368 mode = NFSV3ACCESS_READ;
369 else
370 mode = 0;
371 if (vp->v_type != VDIR) {
372 if (ap->a_mode & VWRITE)
373 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
374 if (ap->a_mode & VEXEC)
375 mode |= NFSV3ACCESS_EXECUTE;
376 } else {
377 if (ap->a_mode & VWRITE)
378 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
379 NFSV3ACCESS_DELETE);
380 if (ap->a_mode & VEXEC)
381 mode |= NFSV3ACCESS_LOOKUP;
382 }
383 *tl = txdr_unsigned(mode);
384 nfsm_request(np, NFSPROC_ACCESS, curlwp, ap->a_cred);
385 nfsm_postop_attr(vp, attrflag, 0);
386 if (!error) {
387 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
388 rmode = fxdr_unsigned(u_int32_t, *tl);
389 /*
390 * The NFS V3 spec does not clarify whether or not
391 * the returned access bits can be a superset of
392 * the ones requested, so...
393 */
394 if ((rmode & mode) != mode)
395 error = EACCES;
396 }
397 nfsm_reqdone;
398 } else
399 #endif
400 return (nfsspec_access(ap));
401 #ifndef NFS_V2_ONLY
402 /*
403 * Disallow write attempts on filesystems mounted read-only;
404 * unless the file is a socket, fifo, or a block or character
405 * device resident on the filesystem.
406 */
407 if (!error && (ap->a_mode & VWRITE) &&
408 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
409 switch (vp->v_type) {
410 case VREG:
411 case VDIR:
412 case VLNK:
413 error = EROFS;
414 default:
415 break;
416 }
417 }
418
419 if (!error || error == EACCES) {
420 mutex_enter(&np->n_attrlock);
421 /*
422 * If we got the same result as for a previous,
423 * different request, OR it in. Don't update
424 * the timestamp in that case.
425 */
426 if (cachevalid && np->n_accstamp != -1 &&
427 error == np->n_accerror) {
428 if (!error)
429 np->n_accmode |= ap->a_mode;
430 else if ((np->n_accmode & ap->a_mode) == ap->a_mode)
431 np->n_accmode = ap->a_mode;
432 } else {
433 np->n_accstamp = time_uptime;
434 np->n_accuid = kauth_cred_geteuid(ap->a_cred);
435 np->n_accmode = ap->a_mode;
436 np->n_accerror = error;
437 }
438 mutex_exit(&np->n_attrlock);
439 }
440
441 return (error);
442 #endif
443 }
444
445 /*
446 * nfs open vnode op
447 * Check to see if the type is ok
448 * and that deletion is not in progress.
449 * For paged in text files, you will need to flush the page cache
450 * if consistency is lost.
451 */
452 /* ARGSUSED */
453 int
454 nfs_open(void *v)
455 {
456 struct vop_open_args /* {
457 struct vnode *a_vp;
458 int a_mode;
459 kauth_cred_t a_cred;
460 } */ *ap = v;
461 struct vnode *vp = ap->a_vp;
462 struct nfsnode *np = VTONFS(vp);
463 int error;
464
465 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
466 return (EACCES);
467 }
468
469 if (ap->a_mode & FREAD) {
470 if (np->n_rcred != NULL)
471 kauth_cred_free(np->n_rcred);
472 np->n_rcred = ap->a_cred;
473 kauth_cred_hold(np->n_rcred);
474 }
475 if (ap->a_mode & FWRITE) {
476 if (np->n_wcred != NULL)
477 kauth_cred_free(np->n_wcred);
478 np->n_wcred = ap->a_cred;
479 kauth_cred_hold(np->n_wcred);
480 }
481
482 error = nfs_flushstalebuf(vp, ap->a_cred, curlwp, 0);
483 if (error)
484 return error;
485
486 NFS_INVALIDATE_ATTRCACHE(np); /* For Open/Close consistency */
487
488 return (0);
489 }
490
491 /*
492 * nfs close vnode op
493 * What an NFS client should do upon close after writing is a debatable issue.
494 * Most NFS clients push delayed writes to the server upon close, basically for
495 * two reasons:
496 * 1 - So that any write errors may be reported back to the client process
497 * doing the close system call. By far the two most likely errors are
498 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
499 * 2 - To put a worst case upper bound on cache inconsistency between
500 * multiple clients for the file.
501 * There is also a consistency problem for Version 2 of the protocol w.r.t.
502 * not being able to tell if other clients are writing a file concurrently,
503 * since there is no way of knowing if the changed modify time in the reply
504 * is only due to the write for this client.
505 * (NFS Version 3 provides weak cache consistency data in the reply that
506 * should be sufficient to detect and handle this case.)
507 *
508 * The current code does the following:
509 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
510 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
511 * or commit them (this satisfies 1 and 2 except for the
512 * case where the server crashes after this close but
513 * before the commit RPC, which is felt to be "good
514 * enough". Changing the last argument to nfs_flush() to
515 * a 1 would force a commit operation, if it is felt a
516 * commit is necessary now.
517 */
518 /* ARGSUSED */
519 int
520 nfs_close(void *v)
521 {
522 struct vop_close_args /* {
523 struct vnodeop_desc *a_desc;
524 struct vnode *a_vp;
525 int a_fflag;
526 kauth_cred_t a_cred;
527 } */ *ap = v;
528 struct vnode *vp = ap->a_vp;
529 struct nfsnode *np = VTONFS(vp);
530 int error = 0;
531 UVMHIST_FUNC("nfs_close"); UVMHIST_CALLED(ubchist);
532
533 if (vp->v_type == VREG) {
534 if (np->n_flag & NMODIFIED) {
535 #ifndef NFS_V2_ONLY
536 if (NFS_ISV3(vp)) {
537 error = nfs_flush(vp, ap->a_cred, MNT_WAIT, curlwp, 0);
538 np->n_flag &= ~NMODIFIED;
539 } else
540 #endif
541 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, curlwp, 1);
542 NFS_INVALIDATE_ATTRCACHE(np);
543 }
544 if (np->n_flag & NWRITEERR) {
545 np->n_flag &= ~NWRITEERR;
546 error = np->n_error;
547 }
548 }
549 UVMHIST_LOG(ubchist, "returning %d", error,0,0,0);
550 return (error);
551 }
552
553 /*
554 * nfs getattr call from vfs.
555 */
556 int
557 nfs_getattr(void *v)
558 {
559 struct vop_getattr_args /* {
560 struct vnode *a_vp;
561 struct vattr *a_vap;
562 kauth_cred_t a_cred;
563 } */ *ap = v;
564 struct vnode *vp = ap->a_vp;
565 struct nfsnode *np = VTONFS(vp);
566 char *cp;
567 u_int32_t *tl;
568 int32_t t1, t2;
569 char *bpos, *dpos;
570 int error = 0;
571 struct mbuf *mreq, *mrep, *md, *mb;
572 const int v3 = NFS_ISV3(vp);
573
574 KASSERT(VOP_ISLOCKED(vp));
575
576 /*
577 * Update local times for special files.
578 */
579 if (np->n_flag & (NACC | NUPD))
580 np->n_flag |= NCHG;
581
582 /*
583 * if we have delayed truncation, do it now.
584 */
585 nfs_delayedtruncate(vp);
586
587 /*
588 * First look in the cache.
589 */
590 if (nfs_getattrcache(vp, ap->a_vap) == 0)
591 return (0);
592 nfsstats.rpccnt[NFSPROC_GETATTR]++;
593 nfsm_reqhead(np, NFSPROC_GETATTR, NFSX_FH(v3));
594 nfsm_fhtom(np, v3);
595 nfsm_request(np, NFSPROC_GETATTR, curlwp, ap->a_cred);
596 if (!error) {
597 nfsm_loadattr(vp, ap->a_vap, 0);
598 if (vp->v_type == VDIR &&
599 ap->a_vap->va_blocksize < NFS_DIRFRAGSIZ)
600 ap->a_vap->va_blocksize = NFS_DIRFRAGSIZ;
601 }
602 nfsm_reqdone;
603 return (error);
604 }
605
606 /*
607 * nfs setattr call.
608 */
609 int
610 nfs_setattr(void *v)
611 {
612 struct vop_setattr_args /* {
613 struct vnodeop_desc *a_desc;
614 struct vnode *a_vp;
615 struct vattr *a_vap;
616 kauth_cred_t a_cred;
617 } */ *ap = v;
618 struct vnode *vp = ap->a_vp;
619 struct nfsnode *np = VTONFS(vp);
620 struct vattr *vap = ap->a_vap;
621 int error = 0;
622 u_quad_t tsize = 0;
623
624 /*
625 * Setting of flags is not supported.
626 */
627 if (vap->va_flags != VNOVAL)
628 return (EOPNOTSUPP);
629
630 /*
631 * Disallow write attempts if the filesystem is mounted read-only.
632 */
633 if ((vap->va_uid != (uid_t)VNOVAL ||
634 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
635 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
636 (vp->v_mount->mnt_flag & MNT_RDONLY))
637 return (EROFS);
638 if (vap->va_size != VNOVAL) {
639 if (vap->va_size > VFSTONFS(vp->v_mount)->nm_maxfilesize) {
640 return EFBIG;
641 }
642 switch (vp->v_type) {
643 case VDIR:
644 return (EISDIR);
645 case VCHR:
646 case VBLK:
647 case VSOCK:
648 case VFIFO:
649 if (vap->va_mtime.tv_sec == VNOVAL &&
650 vap->va_atime.tv_sec == VNOVAL &&
651 vap->va_mode == (mode_t)VNOVAL &&
652 vap->va_uid == (uid_t)VNOVAL &&
653 vap->va_gid == (gid_t)VNOVAL)
654 return (0);
655 vap->va_size = VNOVAL;
656 break;
657 default:
658 /*
659 * Disallow write attempts if the filesystem is
660 * mounted read-only.
661 */
662 if (vp->v_mount->mnt_flag & MNT_RDONLY)
663 return (EROFS);
664 genfs_node_wrlock(vp);
665 uvm_vnp_setsize(vp, vap->va_size);
666 tsize = np->n_size;
667 np->n_size = vap->va_size;
668 if (vap->va_size == 0)
669 error = nfs_vinvalbuf(vp, 0,
670 ap->a_cred, curlwp, 1);
671 else
672 error = nfs_vinvalbuf(vp, V_SAVE,
673 ap->a_cred, curlwp, 1);
674 if (error) {
675 uvm_vnp_setsize(vp, tsize);
676 genfs_node_unlock(vp);
677 return (error);
678 }
679 np->n_vattr->va_size = vap->va_size;
680 }
681 } else {
682 /*
683 * flush files before setattr because a later write of
684 * cached data might change timestamps or reset sugid bits
685 */
686 if ((vap->va_mtime.tv_sec != VNOVAL ||
687 vap->va_atime.tv_sec != VNOVAL ||
688 vap->va_mode != VNOVAL) &&
689 vp->v_type == VREG &&
690 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
691 curlwp, 1)) == EINTR)
692 return (error);
693 }
694 error = nfs_setattrrpc(vp, vap, ap->a_cred, curlwp);
695 if (vap->va_size != VNOVAL) {
696 if (error) {
697 np->n_size = np->n_vattr->va_size = tsize;
698 uvm_vnp_setsize(vp, np->n_size);
699 }
700 genfs_node_unlock(vp);
701 }
702 VN_KNOTE(vp, NOTE_ATTRIB);
703 return (error);
704 }
705
706 /*
707 * Do an nfs setattr rpc.
708 */
709 int
710 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, kauth_cred_t cred, struct lwp *l)
711 {
712 struct nfsv2_sattr *sp;
713 char *cp;
714 int32_t t1, t2;
715 char *bpos, *dpos;
716 u_int32_t *tl;
717 int error = 0;
718 struct mbuf *mreq, *mrep, *md, *mb;
719 const int v3 = NFS_ISV3(vp);
720 struct nfsnode *np = VTONFS(vp);
721 #ifndef NFS_V2_ONLY
722 int wccflag = NFSV3_WCCRATTR;
723 char *cp2;
724 #endif
725
726 nfsstats.rpccnt[NFSPROC_SETATTR]++;
727 nfsm_reqhead(np, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
728 nfsm_fhtom(np, v3);
729 #ifndef NFS_V2_ONLY
730 if (v3) {
731 nfsm_v3attrbuild(vap, true);
732 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
733 *tl = nfs_false;
734 } else {
735 #endif
736 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
737 if (vap->va_mode == (mode_t)VNOVAL)
738 sp->sa_mode = nfs_xdrneg1;
739 else
740 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
741 if (vap->va_uid == (uid_t)VNOVAL)
742 sp->sa_uid = nfs_xdrneg1;
743 else
744 sp->sa_uid = txdr_unsigned(vap->va_uid);
745 if (vap->va_gid == (gid_t)VNOVAL)
746 sp->sa_gid = nfs_xdrneg1;
747 else
748 sp->sa_gid = txdr_unsigned(vap->va_gid);
749 sp->sa_size = txdr_unsigned(vap->va_size);
750 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
751 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
752 #ifndef NFS_V2_ONLY
753 }
754 #endif
755 nfsm_request(np, NFSPROC_SETATTR, l, cred);
756 #ifndef NFS_V2_ONLY
757 if (v3) {
758 nfsm_wcc_data(vp, wccflag, NAC_NOTRUNC, false);
759 } else
760 #endif
761 nfsm_loadattr(vp, (struct vattr *)0, NAC_NOTRUNC);
762 nfsm_reqdone;
763 return (error);
764 }
765
766 /*
767 * nfs lookup call, one step at a time...
768 * First look in cache
769 * If not found, do the rpc.
770 */
771 int
772 nfs_lookup(void *v)
773 {
774 struct vop_lookup_args /* {
775 struct vnodeop_desc *a_desc;
776 struct vnode *a_dvp;
777 struct vnode **a_vpp;
778 struct componentname *a_cnp;
779 } */ *ap = v;
780 struct componentname *cnp = ap->a_cnp;
781 struct vnode *dvp = ap->a_dvp;
782 struct vnode **vpp = ap->a_vpp;
783 int flags;
784 struct vnode *newvp;
785 u_int32_t *tl;
786 char *cp;
787 int32_t t1, t2;
788 char *bpos, *dpos, *cp2;
789 struct mbuf *mreq, *mrep, *md, *mb;
790 long len;
791 nfsfh_t *fhp;
792 struct nfsnode *np;
793 int error = 0, attrflag, fhsize;
794 const int v3 = NFS_ISV3(dvp);
795
796 flags = cnp->cn_flags;
797
798 *vpp = NULLVP;
799 newvp = NULLVP;
800 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
801 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
802 return (EROFS);
803 if (dvp->v_type != VDIR)
804 return (ENOTDIR);
805
806 /*
807 * RFC1813(nfsv3) 3.2 says clients should handle "." by themselves.
808 */
809 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
810 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
811 if (error)
812 return error;
813 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN))
814 return EISDIR;
815 VREF(dvp);
816 *vpp = dvp;
817 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
818 cnp->cn_flags |= SAVENAME;
819 return 0;
820 }
821
822 np = VTONFS(dvp);
823
824 /*
825 * Before performing an RPC, check the name cache to see if
826 * the directory/name pair we are looking for is known already.
827 * If the directory/name pair is found in the name cache,
828 * we have to ensure the directory has not changed from
829 * the time the cache entry has been created. If it has,
830 * the cache entry has to be ignored.
831 */
832 error = cache_lookup_raw(dvp, vpp, cnp);
833 KASSERT(dvp != *vpp);
834 KASSERT((cnp->cn_flags & ISWHITEOUT) == 0);
835 if (error >= 0) {
836 struct vattr vattr;
837 int err2;
838
839 if (error && error != ENOENT) {
840 *vpp = NULLVP;
841 return error;
842 }
843
844 err2 = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
845 if (err2 != 0) {
846 if (error == 0)
847 vrele(*vpp);
848 *vpp = NULLVP;
849 return err2;
850 }
851
852 if (VOP_GETATTR(dvp, &vattr, cnp->cn_cred)
853 || timespeccmp(&vattr.va_mtime,
854 &VTONFS(dvp)->n_nctime, !=)) {
855 if (error == 0) {
856 vrele(*vpp);
857 *vpp = NULLVP;
858 }
859 cache_purge1(dvp, NULL, PURGE_CHILDREN);
860 timespecclear(&np->n_nctime);
861 goto dorpc;
862 }
863
864 if (error == ENOENT) {
865 goto noentry;
866 }
867
868 /*
869 * investigate the vnode returned by cache_lookup_raw.
870 * if it isn't appropriate, do an rpc.
871 */
872 newvp = *vpp;
873 if ((flags & ISDOTDOT) != 0) {
874 VOP_UNLOCK(dvp, 0);
875 }
876 error = vn_lock(newvp, LK_EXCLUSIVE);
877 if ((flags & ISDOTDOT) != 0) {
878 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
879 }
880 if (error != 0) {
881 /* newvp has been reclaimed. */
882 vrele(newvp);
883 *vpp = NULLVP;
884 goto dorpc;
885 }
886 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred)
887 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
888 nfsstats.lookupcache_hits++;
889 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
890 cnp->cn_flags |= SAVENAME;
891 KASSERT(newvp->v_type != VNON);
892 return (0);
893 }
894 cache_purge1(newvp, NULL, PURGE_PARENTS);
895 vput(newvp);
896 *vpp = NULLVP;
897 }
898 dorpc:
899 #if 0
900 /*
901 * because nfsv3 has the same CREATE semantics as ours,
902 * we don't have to perform LOOKUPs beforehand.
903 *
904 * XXX ideally we can do the same for nfsv2 in the case of !O_EXCL.
905 * XXX although we have no way to know if O_EXCL is requested or not.
906 */
907
908 if (v3 && cnp->cn_nameiop == CREATE &&
909 (flags & (ISLASTCN|ISDOTDOT)) == ISLASTCN &&
910 (dvp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
911 cnp->cn_flags |= SAVENAME;
912 return (EJUSTRETURN);
913 }
914 #endif /* 0 */
915
916 error = 0;
917 newvp = NULLVP;
918 nfsstats.lookupcache_misses++;
919 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
920 len = cnp->cn_namelen;
921 nfsm_reqhead(np, NFSPROC_LOOKUP,
922 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
923 nfsm_fhtom(np, v3);
924 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
925 nfsm_request(np, NFSPROC_LOOKUP, curlwp, cnp->cn_cred);
926 if (error) {
927 nfsm_postop_attr(dvp, attrflag, 0);
928 m_freem(mrep);
929 goto nfsmout;
930 }
931 nfsm_getfh(fhp, fhsize, v3);
932
933 /*
934 * Handle RENAME case...
935 */
936 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
937 if (NFS_CMPFH(np, fhp, fhsize)) {
938 m_freem(mrep);
939 return (EISDIR);
940 }
941 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
942 if (error) {
943 m_freem(mrep);
944 return error;
945 }
946 newvp = NFSTOV(np);
947 #ifndef NFS_V2_ONLY
948 if (v3) {
949 nfsm_postop_attr(newvp, attrflag, 0);
950 nfsm_postop_attr(dvp, attrflag, 0);
951 } else
952 #endif
953 nfsm_loadattr(newvp, (struct vattr *)0, 0);
954 *vpp = newvp;
955 m_freem(mrep);
956 cnp->cn_flags |= SAVENAME;
957 goto validate;
958 }
959
960 /*
961 * The postop attr handling is duplicated for each if case,
962 * because it should be done while dvp is locked (unlocking
963 * dvp is different for each case).
964 */
965
966 if (NFS_CMPFH(np, fhp, fhsize)) {
967 /*
968 * as we handle "." lookup locally, this should be
969 * a broken server.
970 */
971 VREF(dvp);
972 newvp = dvp;
973 #ifndef NFS_V2_ONLY
974 if (v3) {
975 nfsm_postop_attr(newvp, attrflag, 0);
976 nfsm_postop_attr(dvp, attrflag, 0);
977 } else
978 #endif
979 nfsm_loadattr(newvp, (struct vattr *)0, 0);
980 } else if (flags & ISDOTDOT) {
981 /*
982 * ".." lookup
983 */
984 VOP_UNLOCK(dvp, 0);
985 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
986 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
987 if (error) {
988 m_freem(mrep);
989 return error;
990 }
991 newvp = NFSTOV(np);
992
993 #ifndef NFS_V2_ONLY
994 if (v3) {
995 nfsm_postop_attr(newvp, attrflag, 0);
996 nfsm_postop_attr(dvp, attrflag, 0);
997 } else
998 #endif
999 nfsm_loadattr(newvp, (struct vattr *)0, 0);
1000 } else {
1001 /*
1002 * Other lookups.
1003 */
1004 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1005 if (error) {
1006 m_freem(mrep);
1007 return error;
1008 }
1009 newvp = NFSTOV(np);
1010 #ifndef NFS_V2_ONLY
1011 if (v3) {
1012 nfsm_postop_attr(newvp, attrflag, 0);
1013 nfsm_postop_attr(dvp, attrflag, 0);
1014 } else
1015 #endif
1016 nfsm_loadattr(newvp, (struct vattr *)0, 0);
1017 }
1018 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1019 cnp->cn_flags |= SAVENAME;
1020 if ((cnp->cn_flags & MAKEENTRY) &&
1021 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
1022 nfs_cache_enter(dvp, newvp, cnp);
1023 }
1024 *vpp = newvp;
1025 nfsm_reqdone;
1026 if (error) {
1027 /*
1028 * We get here only because of errors returned by
1029 * the RPC. Otherwise we'll have returned above
1030 * (the nfsm_* macros will jump to nfsm_reqdone
1031 * on error).
1032 */
1033 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) &&
1034 cnp->cn_nameiop != CREATE) {
1035 nfs_cache_enter(dvp, NULL, cnp);
1036 }
1037 if (newvp != NULLVP) {
1038 if (newvp == dvp) {
1039 vrele(newvp);
1040 } else {
1041 vput(newvp);
1042 }
1043 }
1044 noentry:
1045 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1046 (flags & ISLASTCN) && error == ENOENT) {
1047 if (dvp->v_mount->mnt_flag & MNT_RDONLY) {
1048 error = EROFS;
1049 } else {
1050 error = EJUSTRETURN;
1051 cnp->cn_flags |= SAVENAME;
1052 }
1053 }
1054 *vpp = NULL;
1055 return error;
1056 }
1057
1058 validate:
1059 /*
1060 * make sure we have valid type and size.
1061 */
1062
1063 newvp = *vpp;
1064 if (newvp->v_type == VNON) {
1065 struct vattr vattr; /* dummy */
1066
1067 KASSERT(VTONFS(newvp)->n_attrstamp == 0);
1068 error = VOP_GETATTR(newvp, &vattr, cnp->cn_cred);
1069 if (error) {
1070 vput(newvp);
1071 *vpp = NULL;
1072 }
1073 }
1074
1075 return error;
1076 }
1077
1078 /*
1079 * nfs read call.
1080 * Just call nfs_bioread() to do the work.
1081 */
1082 int
1083 nfs_read(void *v)
1084 {
1085 struct vop_read_args /* {
1086 struct vnode *a_vp;
1087 struct uio *a_uio;
1088 int a_ioflag;
1089 kauth_cred_t a_cred;
1090 } */ *ap = v;
1091 struct vnode *vp = ap->a_vp;
1092
1093 if (vp->v_type != VREG)
1094 return EISDIR;
1095 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred, 0));
1096 }
1097
1098 /*
1099 * nfs readlink call
1100 */
1101 int
1102 nfs_readlink(void *v)
1103 {
1104 struct vop_readlink_args /* {
1105 struct vnode *a_vp;
1106 struct uio *a_uio;
1107 kauth_cred_t a_cred;
1108 } */ *ap = v;
1109 struct vnode *vp = ap->a_vp;
1110 struct nfsnode *np = VTONFS(vp);
1111
1112 if (vp->v_type != VLNK)
1113 return (EPERM);
1114
1115 if (np->n_rcred != NULL) {
1116 kauth_cred_free(np->n_rcred);
1117 }
1118 np->n_rcred = ap->a_cred;
1119 kauth_cred_hold(np->n_rcred);
1120
1121 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred, 0));
1122 }
1123
1124 /*
1125 * Do a readlink rpc.
1126 * Called by nfs_doio() from below the buffer cache.
1127 */
1128 int
1129 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop, kauth_cred_t cred)
1130 {
1131 u_int32_t *tl;
1132 char *cp;
1133 int32_t t1, t2;
1134 char *bpos, *dpos, *cp2;
1135 int error = 0;
1136 uint32_t len;
1137 struct mbuf *mreq, *mrep, *md, *mb;
1138 const int v3 = NFS_ISV3(vp);
1139 struct nfsnode *np = VTONFS(vp);
1140 #ifndef NFS_V2_ONLY
1141 int attrflag;
1142 #endif
1143
1144 nfsstats.rpccnt[NFSPROC_READLINK]++;
1145 nfsm_reqhead(np, NFSPROC_READLINK, NFSX_FH(v3));
1146 nfsm_fhtom(np, v3);
1147 nfsm_request(np, NFSPROC_READLINK, curlwp, cred);
1148 #ifndef NFS_V2_ONLY
1149 if (v3)
1150 nfsm_postop_attr(vp, attrflag, 0);
1151 #endif
1152 if (!error) {
1153 #ifndef NFS_V2_ONLY
1154 if (v3) {
1155 nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
1156 len = fxdr_unsigned(uint32_t, *tl);
1157 if (len > MAXPATHLEN) {
1158 /*
1159 * this pathname is too long for us.
1160 */
1161 m_freem(mrep);
1162 /* Solaris returns EINVAL. should we follow? */
1163 error = ENAMETOOLONG;
1164 goto nfsmout;
1165 }
1166 } else
1167 #endif
1168 {
1169 nfsm_strsiz(len, NFS_MAXPATHLEN);
1170 }
1171 nfsm_mtouio(uiop, len);
1172 }
1173 nfsm_reqdone;
1174 return (error);
1175 }
1176
1177 /*
1178 * nfs read rpc call
1179 * Ditto above
1180 */
1181 int
1182 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1183 {
1184 u_int32_t *tl;
1185 char *cp;
1186 int32_t t1, t2;
1187 char *bpos, *dpos, *cp2;
1188 struct mbuf *mreq, *mrep, *md, *mb;
1189 struct nfsmount *nmp;
1190 int error = 0, len, retlen, tsiz, eof, byte_count;
1191 const int v3 = NFS_ISV3(vp);
1192 struct nfsnode *np = VTONFS(vp);
1193 #ifndef NFS_V2_ONLY
1194 int attrflag;
1195 #endif
1196
1197 #ifndef nolint
1198 eof = 0;
1199 #endif
1200 nmp = VFSTONFS(vp->v_mount);
1201 tsiz = uiop->uio_resid;
1202 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1203 return (EFBIG);
1204 mutex_enter(&nmp->nm_lock);
1205 iostat_busy(nmp->nm_stats);
1206 mutex_exit(&nmp->nm_lock);
1207 byte_count = 0; /* count bytes actually transferred */
1208 while (tsiz > 0) {
1209 nfsstats.rpccnt[NFSPROC_READ]++;
1210 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1211 nfsm_reqhead(np, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1212 nfsm_fhtom(np, v3);
1213 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1214 #ifndef NFS_V2_ONLY
1215 if (v3) {
1216 txdr_hyper(uiop->uio_offset, tl);
1217 *(tl + 2) = txdr_unsigned(len);
1218 } else
1219 #endif
1220 {
1221 *tl++ = txdr_unsigned(uiop->uio_offset);
1222 *tl++ = txdr_unsigned(len);
1223 *tl = 0;
1224 }
1225 nfsm_request(np, NFSPROC_READ, curlwp, np->n_rcred);
1226 #ifndef NFS_V2_ONLY
1227 if (v3) {
1228 nfsm_postop_attr(vp, attrflag, NAC_NOTRUNC);
1229 if (error) {
1230 m_freem(mrep);
1231 goto nfsmout;
1232 }
1233 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1234 eof = fxdr_unsigned(int, *(tl + 1));
1235 } else
1236 #endif
1237 nfsm_loadattr(vp, (struct vattr *)0, NAC_NOTRUNC);
1238 nfsm_strsiz(retlen, nmp->nm_rsize);
1239 nfsm_mtouio(uiop, retlen);
1240 m_freem(mrep);
1241 tsiz -= retlen;
1242 byte_count += retlen;
1243 #ifndef NFS_V2_ONLY
1244 if (v3) {
1245 if (eof || retlen == 0)
1246 tsiz = 0;
1247 } else
1248 #endif
1249 if (retlen < len)
1250 tsiz = 0;
1251 }
1252 nfsmout:
1253 mutex_enter(&nmp->nm_lock);
1254 iostat_unbusy(nmp->nm_stats, byte_count, 1);
1255 mutex_exit(&nmp->nm_lock);
1256 return (error);
1257 }
1258
1259 struct nfs_writerpc_context {
1260 kmutex_t nwc_lock;
1261 kcondvar_t nwc_cv;
1262 int nwc_mbufcount;
1263 };
1264
1265 /*
1266 * free mbuf used to refer protected pages while write rpc call.
1267 * called at splvm.
1268 */
1269 static void
1270 nfs_writerpc_extfree(struct mbuf *m, void *tbuf, size_t size, void *arg)
1271 {
1272 struct nfs_writerpc_context *ctx = arg;
1273
1274 KASSERT(m != NULL);
1275 KASSERT(ctx != NULL);
1276 pool_cache_put(mb_cache, m);
1277 mutex_enter(&ctx->nwc_lock);
1278 if (--ctx->nwc_mbufcount == 0) {
1279 cv_signal(&ctx->nwc_cv);
1280 }
1281 mutex_exit(&ctx->nwc_lock);
1282 }
1283
1284 /*
1285 * nfs write call
1286 */
1287 int
1288 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, bool pageprotected, bool *stalewriteverfp)
1289 {
1290 u_int32_t *tl;
1291 char *cp;
1292 int32_t t1, t2;
1293 char *bpos, *dpos;
1294 struct mbuf *mreq, *mrep, *md, *mb;
1295 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1296 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR;
1297 const int v3 = NFS_ISV3(vp);
1298 int committed = NFSV3WRITE_FILESYNC;
1299 struct nfsnode *np = VTONFS(vp);
1300 struct nfs_writerpc_context ctx;
1301 int byte_count;
1302 struct lwp *l = NULL;
1303 size_t origresid;
1304 #ifndef NFS_V2_ONLY
1305 char *cp2;
1306 int rlen, commit;
1307 #endif
1308
1309 mutex_init(&ctx.nwc_lock, MUTEX_DRIVER, IPL_VM);
1310 cv_init(&ctx.nwc_cv, "nfsmblk");
1311 ctx.nwc_mbufcount = 1;
1312
1313 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
1314 panic("writerpc readonly vp %p", vp);
1315 }
1316
1317 #ifdef DIAGNOSTIC
1318 if (uiop->uio_iovcnt != 1)
1319 panic("nfs: writerpc iovcnt > 1");
1320 #endif
1321 tsiz = uiop->uio_resid;
1322 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1323 return (EFBIG);
1324 if (pageprotected) {
1325 l = curlwp;
1326 uvm_lwp_hold(l);
1327 }
1328 retry:
1329 origresid = uiop->uio_resid;
1330 KASSERT(origresid == uiop->uio_iov->iov_len);
1331 mutex_enter(&nmp->nm_lock);
1332 iostat_busy(nmp->nm_stats);
1333 mutex_exit(&nmp->nm_lock);
1334 byte_count = 0; /* count of bytes actually written */
1335 while (tsiz > 0) {
1336 uint32_t datalen; /* data bytes need to be allocated in mbuf */
1337 uint32_t backup;
1338 bool stalewriteverf = false;
1339
1340 nfsstats.rpccnt[NFSPROC_WRITE]++;
1341 len = min(tsiz, nmp->nm_wsize);
1342 datalen = pageprotected ? 0 : nfsm_rndup(len);
1343 nfsm_reqhead(np, NFSPROC_WRITE,
1344 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + datalen);
1345 nfsm_fhtom(np, v3);
1346 #ifndef NFS_V2_ONLY
1347 if (v3) {
1348 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1349 txdr_hyper(uiop->uio_offset, tl);
1350 tl += 2;
1351 *tl++ = txdr_unsigned(len);
1352 *tl++ = txdr_unsigned(*iomode);
1353 *tl = txdr_unsigned(len);
1354 } else
1355 #endif
1356 {
1357 u_int32_t x;
1358
1359 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1360 /* Set both "begin" and "current" to non-garbage. */
1361 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1362 *tl++ = x; /* "begin offset" */
1363 *tl++ = x; /* "current offset" */
1364 x = txdr_unsigned(len);
1365 *tl++ = x; /* total to this offset */
1366 *tl = x; /* size of this write */
1367
1368 }
1369 if (pageprotected) {
1370 /*
1371 * since we know pages can't be modified during i/o,
1372 * no need to copy them for us.
1373 */
1374 struct mbuf *m;
1375 struct iovec *iovp = uiop->uio_iov;
1376
1377 m = m_get(M_WAIT, MT_DATA);
1378 MCLAIM(m, &nfs_mowner);
1379 MEXTADD(m, iovp->iov_base, len, M_MBUF,
1380 nfs_writerpc_extfree, &ctx);
1381 m->m_flags |= M_EXT_ROMAP;
1382 m->m_len = len;
1383 mb->m_next = m;
1384 /*
1385 * no need to maintain mb and bpos here
1386 * because no one care them later.
1387 */
1388 #if 0
1389 mb = m;
1390 bpos = mtod(void *, mb) + mb->m_len;
1391 #endif
1392 UIO_ADVANCE(uiop, len);
1393 uiop->uio_offset += len;
1394 mutex_enter(&ctx.nwc_lock);
1395 ctx.nwc_mbufcount++;
1396 mutex_exit(&ctx.nwc_lock);
1397 nfs_zeropad(mb, 0, nfsm_padlen(len));
1398 } else {
1399 nfsm_uiotom(uiop, len);
1400 }
1401 nfsm_request(np, NFSPROC_WRITE, curlwp, np->n_wcred);
1402 #ifndef NFS_V2_ONLY
1403 if (v3) {
1404 wccflag = NFSV3_WCCCHK;
1405 nfsm_wcc_data(vp, wccflag, NAC_NOTRUNC, !error);
1406 if (!error) {
1407 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1408 + NFSX_V3WRITEVERF);
1409 rlen = fxdr_unsigned(int, *tl++);
1410 if (rlen == 0) {
1411 error = NFSERR_IO;
1412 m_freem(mrep);
1413 break;
1414 } else if (rlen < len) {
1415 backup = len - rlen;
1416 UIO_ADVANCE(uiop, -backup);
1417 uiop->uio_offset -= backup;
1418 len = rlen;
1419 }
1420 commit = fxdr_unsigned(int, *tl++);
1421
1422 /*
1423 * Return the lowest committment level
1424 * obtained by any of the RPCs.
1425 */
1426 if (committed == NFSV3WRITE_FILESYNC)
1427 committed = commit;
1428 else if (committed == NFSV3WRITE_DATASYNC &&
1429 commit == NFSV3WRITE_UNSTABLE)
1430 committed = commit;
1431 mutex_enter(&nmp->nm_lock);
1432 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0){
1433 memcpy(nmp->nm_writeverf, tl,
1434 NFSX_V3WRITEVERF);
1435 nmp->nm_iflag |= NFSMNT_HASWRITEVERF;
1436 } else if ((nmp->nm_iflag &
1437 NFSMNT_STALEWRITEVERF) ||
1438 memcmp(tl, nmp->nm_writeverf,
1439 NFSX_V3WRITEVERF)) {
1440 memcpy(nmp->nm_writeverf, tl,
1441 NFSX_V3WRITEVERF);
1442 /*
1443 * note NFSMNT_STALEWRITEVERF
1444 * if we're the first thread to
1445 * notice it.
1446 */
1447 if ((nmp->nm_iflag &
1448 NFSMNT_STALEWRITEVERF) == 0) {
1449 stalewriteverf = true;
1450 nmp->nm_iflag |=
1451 NFSMNT_STALEWRITEVERF;
1452 }
1453 }
1454 mutex_exit(&nmp->nm_lock);
1455 }
1456 } else
1457 #endif
1458 nfsm_loadattr(vp, (struct vattr *)0, NAC_NOTRUNC);
1459 if (wccflag)
1460 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr->va_mtime;
1461 m_freem(mrep);
1462 if (error)
1463 break;
1464 tsiz -= len;
1465 byte_count += len;
1466 if (stalewriteverf) {
1467 *stalewriteverfp = true;
1468 stalewriteverf = false;
1469 if (committed == NFSV3WRITE_UNSTABLE &&
1470 len != origresid) {
1471 /*
1472 * if our write requests weren't atomic but
1473 * unstable, datas in previous iterations
1474 * might have already been lost now.
1475 * then, we should resend them to nfsd.
1476 */
1477 backup = origresid - tsiz;
1478 UIO_ADVANCE(uiop, -backup);
1479 uiop->uio_offset -= backup;
1480 tsiz = origresid;
1481 goto retry;
1482 }
1483 }
1484 }
1485 nfsmout:
1486 mutex_enter(&nmp->nm_lock);
1487 iostat_unbusy(nmp->nm_stats, byte_count, 0);
1488 mutex_exit(&nmp->nm_lock);
1489 if (pageprotected) {
1490 /*
1491 * wait until mbufs go away.
1492 * retransmitted mbufs can survive longer than rpc requests
1493 * themselves.
1494 */
1495 mutex_enter(&ctx.nwc_lock);
1496 ctx.nwc_mbufcount--;
1497 while (ctx.nwc_mbufcount > 0) {
1498 cv_wait(&ctx.nwc_cv, &ctx.nwc_lock);
1499 }
1500 mutex_exit(&ctx.nwc_lock);
1501 uvm_lwp_rele(l);
1502 }
1503 mutex_destroy(&ctx.nwc_lock);
1504 cv_destroy(&ctx.nwc_cv);
1505 *iomode = committed;
1506 if (error)
1507 uiop->uio_resid = tsiz;
1508 return (error);
1509 }
1510
1511 /*
1512 * nfs mknod rpc
1513 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1514 * mode set to specify the file type and the size field for rdev.
1515 */
1516 int
1517 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, struct vattr *vap)
1518 {
1519 struct nfsv2_sattr *sp;
1520 u_int32_t *tl;
1521 char *cp;
1522 int32_t t1, t2;
1523 struct vnode *newvp = (struct vnode *)0;
1524 struct nfsnode *dnp, *np;
1525 char *cp2;
1526 char *bpos, *dpos;
1527 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1528 struct mbuf *mreq, *mrep, *md, *mb;
1529 u_int32_t rdev;
1530 const int v3 = NFS_ISV3(dvp);
1531
1532 if (vap->va_type == VCHR || vap->va_type == VBLK)
1533 rdev = txdr_unsigned(vap->va_rdev);
1534 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1535 rdev = nfs_xdrneg1;
1536 else {
1537 VOP_ABORTOP(dvp, cnp);
1538 vput(dvp);
1539 return (EOPNOTSUPP);
1540 }
1541 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1542 dnp = VTONFS(dvp);
1543 nfsm_reqhead(dnp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1544 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1545 nfsm_fhtom(dnp, v3);
1546 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1547 #ifndef NFS_V2_ONLY
1548 if (v3) {
1549 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1550 *tl++ = vtonfsv3_type(vap->va_type);
1551 nfsm_v3attrbuild(vap, false);
1552 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1553 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1554 *tl++ = txdr_unsigned(major(vap->va_rdev));
1555 *tl = txdr_unsigned(minor(vap->va_rdev));
1556 }
1557 } else
1558 #endif
1559 {
1560 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1561 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1562 sp->sa_uid = nfs_xdrneg1;
1563 sp->sa_gid = nfs_xdrneg1;
1564 sp->sa_size = rdev;
1565 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1566 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1567 }
1568 nfsm_request(dnp, NFSPROC_MKNOD, curlwp, cnp->cn_cred);
1569 if (!error) {
1570 nfsm_mtofh(dvp, newvp, v3, gotvp);
1571 if (!gotvp) {
1572 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1573 cnp->cn_namelen, cnp->cn_cred, curlwp, &np);
1574 if (!error)
1575 newvp = NFSTOV(np);
1576 }
1577 }
1578 #ifndef NFS_V2_ONLY
1579 if (v3)
1580 nfsm_wcc_data(dvp, wccflag, 0, !error);
1581 #endif
1582 nfsm_reqdone;
1583 if (error) {
1584 if (newvp)
1585 vput(newvp);
1586 } else {
1587 if (cnp->cn_flags & MAKEENTRY)
1588 nfs_cache_enter(dvp, newvp, cnp);
1589 *vpp = newvp;
1590 }
1591 PNBUF_PUT(cnp->cn_pnbuf);
1592 VTONFS(dvp)->n_flag |= NMODIFIED;
1593 if (!wccflag)
1594 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
1595 vput(dvp);
1596 return (error);
1597 }
1598
1599 /*
1600 * nfs mknod vop
1601 * just call nfs_mknodrpc() to do the work.
1602 */
1603 /* ARGSUSED */
1604 int
1605 nfs_mknod(void *v)
1606 {
1607 struct vop_mknod_args /* {
1608 struct vnode *a_dvp;
1609 struct vnode **a_vpp;
1610 struct componentname *a_cnp;
1611 struct vattr *a_vap;
1612 } */ *ap = v;
1613 struct vnode *dvp = ap->a_dvp;
1614 struct componentname *cnp = ap->a_cnp;
1615 int error;
1616
1617 error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, ap->a_vap);
1618 VN_KNOTE(dvp, NOTE_WRITE);
1619 if (error == 0 || error == EEXIST)
1620 cache_purge1(dvp, cnp, 0);
1621 return (error);
1622 }
1623
1624 /*
1625 * nfs file create call
1626 */
1627 int
1628 nfs_create(void *v)
1629 {
1630 struct vop_create_args /* {
1631 struct vnode *a_dvp;
1632 struct vnode **a_vpp;
1633 struct componentname *a_cnp;
1634 struct vattr *a_vap;
1635 } */ *ap = v;
1636 struct vnode *dvp = ap->a_dvp;
1637 struct vattr *vap = ap->a_vap;
1638 struct componentname *cnp = ap->a_cnp;
1639 struct nfsv2_sattr *sp;
1640 u_int32_t *tl;
1641 char *cp;
1642 int32_t t1, t2;
1643 struct nfsnode *dnp, *np = (struct nfsnode *)0;
1644 struct vnode *newvp = (struct vnode *)0;
1645 char *bpos, *dpos, *cp2;
1646 int error, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1647 struct mbuf *mreq, *mrep, *md, *mb;
1648 const int v3 = NFS_ISV3(dvp);
1649 u_int32_t excl_mode = NFSV3CREATE_UNCHECKED;
1650
1651 /*
1652 * Oops, not for me..
1653 */
1654 if (vap->va_type == VSOCK)
1655 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1656
1657 KASSERT(vap->va_type == VREG);
1658
1659 #ifdef VA_EXCLUSIVE
1660 if (vap->va_vaflags & VA_EXCLUSIVE) {
1661 excl_mode = NFSV3CREATE_EXCLUSIVE;
1662 }
1663 #endif
1664 again:
1665 error = 0;
1666 nfsstats.rpccnt[NFSPROC_CREATE]++;
1667 dnp = VTONFS(dvp);
1668 nfsm_reqhead(dnp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1669 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1670 nfsm_fhtom(dnp, v3);
1671 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1672 #ifndef NFS_V2_ONLY
1673 if (v3) {
1674 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1675 if (excl_mode == NFSV3CREATE_EXCLUSIVE) {
1676 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1677 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1678 *tl++ = arc4random();
1679 *tl = arc4random();
1680 } else {
1681 *tl = txdr_unsigned(excl_mode);
1682 nfsm_v3attrbuild(vap, false);
1683 }
1684 } else
1685 #endif
1686 {
1687 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1688 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1689 sp->sa_uid = nfs_xdrneg1;
1690 sp->sa_gid = nfs_xdrneg1;
1691 sp->sa_size = 0;
1692 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1693 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1694 }
1695 nfsm_request(dnp, NFSPROC_CREATE, curlwp, cnp->cn_cred);
1696 if (!error) {
1697 nfsm_mtofh(dvp, newvp, v3, gotvp);
1698 if (!gotvp) {
1699 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1700 cnp->cn_namelen, cnp->cn_cred, curlwp, &np);
1701 if (!error)
1702 newvp = NFSTOV(np);
1703 }
1704 }
1705 #ifndef NFS_V2_ONLY
1706 if (v3)
1707 nfsm_wcc_data(dvp, wccflag, 0, !error);
1708 #endif
1709 nfsm_reqdone;
1710 if (error) {
1711 /*
1712 * nfs_request maps NFSERR_NOTSUPP to ENOTSUP.
1713 */
1714 if (v3 && error == ENOTSUP) {
1715 if (excl_mode == NFSV3CREATE_EXCLUSIVE) {
1716 excl_mode = NFSV3CREATE_GUARDED;
1717 goto again;
1718 } else if (excl_mode == NFSV3CREATE_GUARDED) {
1719 excl_mode = NFSV3CREATE_UNCHECKED;
1720 goto again;
1721 }
1722 }
1723 } else if (v3 && (excl_mode == NFSV3CREATE_EXCLUSIVE)) {
1724 struct timespec ts;
1725
1726 getnanotime(&ts);
1727
1728 /*
1729 * make sure that we'll update timestamps as
1730 * most server implementations use them to store
1731 * the create verifier.
1732 *
1733 * XXX it's better to use TOSERVER always.
1734 */
1735
1736 if (vap->va_atime.tv_sec == VNOVAL)
1737 vap->va_atime = ts;
1738 if (vap->va_mtime.tv_sec == VNOVAL)
1739 vap->va_mtime = ts;
1740
1741 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, curlwp);
1742 }
1743 if (error == 0) {
1744 if (cnp->cn_flags & MAKEENTRY)
1745 nfs_cache_enter(dvp, newvp, cnp);
1746 else
1747 cache_purge1(dvp, cnp, 0);
1748 *ap->a_vpp = newvp;
1749 } else {
1750 if (newvp)
1751 vput(newvp);
1752 if (error == EEXIST)
1753 cache_purge1(dvp, cnp, 0);
1754 }
1755 PNBUF_PUT(cnp->cn_pnbuf);
1756 VTONFS(dvp)->n_flag |= NMODIFIED;
1757 if (!wccflag)
1758 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
1759 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1760 vput(dvp);
1761 return (error);
1762 }
1763
1764 /*
1765 * nfs file remove call
1766 * To try and make nfs semantics closer to ufs semantics, a file that has
1767 * other processes using the vnode is renamed instead of removed and then
1768 * removed later on the last close.
1769 * - If v_usecount > 1
1770 * If a rename is not already in the works
1771 * call nfs_sillyrename() to set it up
1772 * else
1773 * do the remove rpc
1774 */
1775 int
1776 nfs_remove(void *v)
1777 {
1778 struct vop_remove_args /* {
1779 struct vnodeop_desc *a_desc;
1780 struct vnode * a_dvp;
1781 struct vnode * a_vp;
1782 struct componentname * a_cnp;
1783 } */ *ap = v;
1784 struct vnode *vp = ap->a_vp;
1785 struct vnode *dvp = ap->a_dvp;
1786 struct componentname *cnp = ap->a_cnp;
1787 struct nfsnode *np = VTONFS(vp);
1788 int error = 0;
1789 struct vattr vattr;
1790
1791 #ifndef DIAGNOSTIC
1792 if ((cnp->cn_flags & HASBUF) == 0)
1793 panic("nfs_remove: no name");
1794 if (vp->v_usecount < 1)
1795 panic("nfs_remove: bad v_usecount");
1796 #endif
1797 if (vp->v_type == VDIR)
1798 error = EPERM;
1799 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1800 VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1801 vattr.va_nlink > 1)) {
1802 /*
1803 * Purge the name cache so that the chance of a lookup for
1804 * the name succeeding while the remove is in progress is
1805 * minimized. Without node locking it can still happen, such
1806 * that an I/O op returns ESTALE, but since you get this if
1807 * another host removes the file..
1808 */
1809 cache_purge(vp);
1810 /*
1811 * throw away biocache buffers, mainly to avoid
1812 * unnecessary delayed writes later.
1813 */
1814 error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, curlwp, 1);
1815 /* Do the rpc */
1816 if (error != EINTR)
1817 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1818 cnp->cn_namelen, cnp->cn_cred, curlwp);
1819 } else if (!np->n_sillyrename)
1820 error = nfs_sillyrename(dvp, vp, cnp, false);
1821 PNBUF_PUT(cnp->cn_pnbuf);
1822 if (!error && nfs_getattrcache(vp, &vattr) == 0 &&
1823 vattr.va_nlink == 1) {
1824 np->n_flag |= NREMOVED;
1825 }
1826 NFS_INVALIDATE_ATTRCACHE(np);
1827 VN_KNOTE(vp, NOTE_DELETE);
1828 VN_KNOTE(dvp, NOTE_WRITE);
1829 if (dvp == vp)
1830 vrele(vp);
1831 else
1832 vput(vp);
1833 vput(dvp);
1834 return (error);
1835 }
1836
1837 /*
1838 * nfs file remove rpc called from nfs_inactive
1839 */
1840 int
1841 nfs_removeit(struct sillyrename *sp)
1842 {
1843
1844 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1845 (struct lwp *)0));
1846 }
1847
1848 /*
1849 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1850 */
1851 int
1852 nfs_removerpc(struct vnode *dvp, const char *name, int namelen, kauth_cred_t cred, struct lwp *l)
1853 {
1854 u_int32_t *tl;
1855 char *cp;
1856 #ifndef NFS_V2_ONLY
1857 int32_t t1;
1858 char *cp2;
1859 #endif
1860 int32_t t2;
1861 char *bpos, *dpos;
1862 int error = 0, wccflag = NFSV3_WCCRATTR;
1863 struct mbuf *mreq, *mrep, *md, *mb;
1864 const int v3 = NFS_ISV3(dvp);
1865 int rexmit = 0;
1866 struct nfsnode *dnp = VTONFS(dvp);
1867
1868 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1869 nfsm_reqhead(dnp, NFSPROC_REMOVE,
1870 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1871 nfsm_fhtom(dnp, v3);
1872 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1873 nfsm_request1(dnp, NFSPROC_REMOVE, l, cred, &rexmit);
1874 #ifndef NFS_V2_ONLY
1875 if (v3)
1876 nfsm_wcc_data(dvp, wccflag, 0, !error);
1877 #endif
1878 nfsm_reqdone;
1879 VTONFS(dvp)->n_flag |= NMODIFIED;
1880 if (!wccflag)
1881 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
1882 /*
1883 * Kludge City: If the first reply to the remove rpc is lost..
1884 * the reply to the retransmitted request will be ENOENT
1885 * since the file was in fact removed
1886 * Therefore, we cheat and return success.
1887 */
1888 if (rexmit && error == ENOENT)
1889 error = 0;
1890 return (error);
1891 }
1892
1893 /*
1894 * nfs file rename call
1895 */
1896 int
1897 nfs_rename(void *v)
1898 {
1899 struct vop_rename_args /* {
1900 struct vnode *a_fdvp;
1901 struct vnode *a_fvp;
1902 struct componentname *a_fcnp;
1903 struct vnode *a_tdvp;
1904 struct vnode *a_tvp;
1905 struct componentname *a_tcnp;
1906 } */ *ap = v;
1907 struct vnode *fvp = ap->a_fvp;
1908 struct vnode *tvp = ap->a_tvp;
1909 struct vnode *fdvp = ap->a_fdvp;
1910 struct vnode *tdvp = ap->a_tdvp;
1911 struct componentname *tcnp = ap->a_tcnp;
1912 struct componentname *fcnp = ap->a_fcnp;
1913 int error;
1914
1915 #ifndef DIAGNOSTIC
1916 if ((tcnp->cn_flags & HASBUF) == 0 ||
1917 (fcnp->cn_flags & HASBUF) == 0)
1918 panic("nfs_rename: no name");
1919 #endif
1920 /* Check for cross-device rename */
1921 if ((fvp->v_mount != tdvp->v_mount) ||
1922 (tvp && (fvp->v_mount != tvp->v_mount))) {
1923 error = EXDEV;
1924 goto out;
1925 }
1926
1927 /*
1928 * If the tvp exists and is in use, sillyrename it before doing the
1929 * rename of the new file over it.
1930 *
1931 * Have sillyrename use link instead of rename if possible,
1932 * so that we don't lose the file if the rename fails, and so
1933 * that there's no window when the "to" file doesn't exist.
1934 */
1935 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1936 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp, true)) {
1937 VN_KNOTE(tvp, NOTE_DELETE);
1938 vput(tvp);
1939 tvp = NULL;
1940 }
1941
1942 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1943 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1944 curlwp);
1945
1946 VN_KNOTE(fdvp, NOTE_WRITE);
1947 VN_KNOTE(tdvp, NOTE_WRITE);
1948 if (error == 0 || error == EEXIST) {
1949 if (fvp->v_type == VDIR)
1950 cache_purge(fvp);
1951 else
1952 cache_purge1(fdvp, fcnp, 0);
1953 if (tvp != NULL && tvp->v_type == VDIR)
1954 cache_purge(tvp);
1955 else
1956 cache_purge1(tdvp, tcnp, 0);
1957 }
1958 out:
1959 if (tdvp == tvp)
1960 vrele(tdvp);
1961 else
1962 vput(tdvp);
1963 if (tvp)
1964 vput(tvp);
1965 vrele(fdvp);
1966 vrele(fvp);
1967 return (error);
1968 }
1969
1970 /*
1971 * nfs file rename rpc called from nfs_remove() above
1972 */
1973 int
1974 nfs_renameit(struct vnode *sdvp, struct componentname *scnp, struct sillyrename *sp)
1975 {
1976 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1977 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, curlwp));
1978 }
1979
1980 /*
1981 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1982 */
1983 int
1984 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen, struct vnode *tdvp, const char *tnameptr, int tnamelen, kauth_cred_t cred, struct lwp *l)
1985 {
1986 u_int32_t *tl;
1987 char *cp;
1988 #ifndef NFS_V2_ONLY
1989 int32_t t1;
1990 char *cp2;
1991 #endif
1992 int32_t t2;
1993 char *bpos, *dpos;
1994 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1995 struct mbuf *mreq, *mrep, *md, *mb;
1996 const int v3 = NFS_ISV3(fdvp);
1997 int rexmit = 0;
1998 struct nfsnode *fdnp = VTONFS(fdvp);
1999
2000 nfsstats.rpccnt[NFSPROC_RENAME]++;
2001 nfsm_reqhead(fdnp, NFSPROC_RENAME,
2002 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
2003 nfsm_rndup(tnamelen));
2004 nfsm_fhtom(fdnp, v3);
2005 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
2006 nfsm_fhtom(VTONFS(tdvp), v3);
2007 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
2008 nfsm_request1(fdnp, NFSPROC_RENAME, l, cred, &rexmit);
2009 #ifndef NFS_V2_ONLY
2010 if (v3) {
2011 nfsm_wcc_data(fdvp, fwccflag, 0, !error);
2012 nfsm_wcc_data(tdvp, twccflag, 0, !error);
2013 }
2014 #endif
2015 nfsm_reqdone;
2016 VTONFS(fdvp)->n_flag |= NMODIFIED;
2017 VTONFS(tdvp)->n_flag |= NMODIFIED;
2018 if (!fwccflag)
2019 NFS_INVALIDATE_ATTRCACHE(VTONFS(fdvp));
2020 if (!twccflag)
2021 NFS_INVALIDATE_ATTRCACHE(VTONFS(tdvp));
2022 /*
2023 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2024 */
2025 if (rexmit && error == ENOENT)
2026 error = 0;
2027 return (error);
2028 }
2029
2030 /*
2031 * NFS link RPC, called from nfs_link.
2032 * Assumes dvp and vp locked, and leaves them that way.
2033 */
2034
2035 static int
2036 nfs_linkrpc(struct vnode *dvp, struct vnode *vp, const char *name,
2037 size_t namelen, kauth_cred_t cred, struct lwp *l)
2038 {
2039 u_int32_t *tl;
2040 char *cp;
2041 #ifndef NFS_V2_ONLY
2042 int32_t t1;
2043 char *cp2;
2044 #endif
2045 int32_t t2;
2046 char *bpos, *dpos;
2047 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
2048 struct mbuf *mreq, *mrep, *md, *mb;
2049 const int v3 = NFS_ISV3(dvp);
2050 int rexmit = 0;
2051 struct nfsnode *np = VTONFS(vp);
2052
2053 nfsstats.rpccnt[NFSPROC_LINK]++;
2054 nfsm_reqhead(np, NFSPROC_LINK,
2055 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(namelen));
2056 nfsm_fhtom(np, v3);
2057 nfsm_fhtom(VTONFS(dvp), v3);
2058 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
2059 nfsm_request1(np, NFSPROC_LINK, l, cred, &rexmit);
2060 #ifndef NFS_V2_ONLY
2061 if (v3) {
2062 nfsm_postop_attr(vp, attrflag, 0);
2063 nfsm_wcc_data(dvp, wccflag, 0, !error);
2064 }
2065 #endif
2066 nfsm_reqdone;
2067
2068 VTONFS(dvp)->n_flag |= NMODIFIED;
2069 if (!attrflag)
2070 NFS_INVALIDATE_ATTRCACHE(VTONFS(vp));
2071 if (!wccflag)
2072 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2073
2074 /*
2075 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2076 */
2077 if (rexmit && error == EEXIST)
2078 error = 0;
2079
2080 return error;
2081 }
2082
2083 /*
2084 * nfs hard link create call
2085 */
2086 int
2087 nfs_link(void *v)
2088 {
2089 struct vop_link_args /* {
2090 struct vnode *a_dvp;
2091 struct vnode *a_vp;
2092 struct componentname *a_cnp;
2093 } */ *ap = v;
2094 struct vnode *vp = ap->a_vp;
2095 struct vnode *dvp = ap->a_dvp;
2096 struct componentname *cnp = ap->a_cnp;
2097 int error = 0;
2098
2099 if (dvp->v_mount != vp->v_mount) {
2100 VOP_ABORTOP(dvp, cnp);
2101 vput(dvp);
2102 return (EXDEV);
2103 }
2104 if (dvp != vp) {
2105 error = vn_lock(vp, LK_EXCLUSIVE);
2106 if (error != 0) {
2107 VOP_ABORTOP(dvp, cnp);
2108 vput(dvp);
2109 return error;
2110 }
2111 }
2112
2113 /*
2114 * Push all writes to the server, so that the attribute cache
2115 * doesn't get "out of sync" with the server.
2116 * XXX There should be a better way!
2117 */
2118 VOP_FSYNC(vp, cnp->cn_cred, FSYNC_WAIT, 0, 0);
2119
2120 error = nfs_linkrpc(dvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
2121 cnp->cn_cred, curlwp);
2122
2123 if (error == 0)
2124 cache_purge1(dvp, cnp, 0);
2125 PNBUF_PUT(cnp->cn_pnbuf);
2126 if (dvp != vp)
2127 VOP_UNLOCK(vp, 0);
2128 VN_KNOTE(vp, NOTE_LINK);
2129 VN_KNOTE(dvp, NOTE_WRITE);
2130 vput(dvp);
2131 return (error);
2132 }
2133
2134 /*
2135 * nfs symbolic link create call
2136 */
2137 int
2138 nfs_symlink(void *v)
2139 {
2140 struct vop_symlink_args /* {
2141 struct vnode *a_dvp;
2142 struct vnode **a_vpp;
2143 struct componentname *a_cnp;
2144 struct vattr *a_vap;
2145 char *a_target;
2146 } */ *ap = v;
2147 struct vnode *dvp = ap->a_dvp;
2148 struct vattr *vap = ap->a_vap;
2149 struct componentname *cnp = ap->a_cnp;
2150 struct nfsv2_sattr *sp;
2151 u_int32_t *tl;
2152 char *cp;
2153 int32_t t1, t2;
2154 char *bpos, *dpos, *cp2;
2155 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2156 struct mbuf *mreq, *mrep, *md, *mb;
2157 struct vnode *newvp = (struct vnode *)0;
2158 const int v3 = NFS_ISV3(dvp);
2159 int rexmit = 0;
2160 struct nfsnode *dnp = VTONFS(dvp);
2161
2162 *ap->a_vpp = NULL;
2163 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2164 slen = strlen(ap->a_target);
2165 nfsm_reqhead(dnp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
2166 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
2167 nfsm_fhtom(dnp, v3);
2168 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2169 #ifndef NFS_V2_ONlY
2170 if (v3)
2171 nfsm_v3attrbuild(vap, false);
2172 #endif
2173 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
2174 #ifndef NFS_V2_ONlY
2175 if (!v3) {
2176 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2177 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2178 sp->sa_uid = nfs_xdrneg1;
2179 sp->sa_gid = nfs_xdrneg1;
2180 sp->sa_size = nfs_xdrneg1;
2181 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2182 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2183 }
2184 #endif
2185 nfsm_request1(dnp, NFSPROC_SYMLINK, curlwp, cnp->cn_cred,
2186 &rexmit);
2187 #ifndef NFS_V2_ONlY
2188 if (v3) {
2189 if (!error)
2190 nfsm_mtofh(dvp, newvp, v3, gotvp);
2191 nfsm_wcc_data(dvp, wccflag, 0, !error);
2192 }
2193 #endif
2194 nfsm_reqdone;
2195 /*
2196 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2197 */
2198 if (rexmit && error == EEXIST)
2199 error = 0;
2200 if (error == 0 || error == EEXIST)
2201 cache_purge1(dvp, cnp, 0);
2202 if (error == 0 && newvp == NULL) {
2203 struct nfsnode *np = NULL;
2204
2205 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2206 cnp->cn_cred, curlwp, &np);
2207 if (error == 0)
2208 newvp = NFSTOV(np);
2209 }
2210 if (error) {
2211 if (newvp != NULL)
2212 vput(newvp);
2213 } else {
2214 *ap->a_vpp = newvp;
2215 }
2216 PNBUF_PUT(cnp->cn_pnbuf);
2217 VTONFS(dvp)->n_flag |= NMODIFIED;
2218 if (!wccflag)
2219 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2220 VN_KNOTE(dvp, NOTE_WRITE);
2221 vput(dvp);
2222 return (error);
2223 }
2224
2225 /*
2226 * nfs make dir call
2227 */
2228 int
2229 nfs_mkdir(void *v)
2230 {
2231 struct vop_mkdir_args /* {
2232 struct vnode *a_dvp;
2233 struct vnode **a_vpp;
2234 struct componentname *a_cnp;
2235 struct vattr *a_vap;
2236 } */ *ap = v;
2237 struct vnode *dvp = ap->a_dvp;
2238 struct vattr *vap = ap->a_vap;
2239 struct componentname *cnp = ap->a_cnp;
2240 struct nfsv2_sattr *sp;
2241 u_int32_t *tl;
2242 char *cp;
2243 int32_t t1, t2;
2244 int len;
2245 struct nfsnode *dnp = VTONFS(dvp), *np = (struct nfsnode *)0;
2246 struct vnode *newvp = (struct vnode *)0;
2247 char *bpos, *dpos, *cp2;
2248 int error = 0, wccflag = NFSV3_WCCRATTR;
2249 int gotvp = 0;
2250 int rexmit = 0;
2251 struct mbuf *mreq, *mrep, *md, *mb;
2252 const int v3 = NFS_ISV3(dvp);
2253
2254 len = cnp->cn_namelen;
2255 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2256 nfsm_reqhead(dnp, NFSPROC_MKDIR,
2257 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2258 nfsm_fhtom(dnp, v3);
2259 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
2260 #ifndef NFS_V2_ONLY
2261 if (v3) {
2262 nfsm_v3attrbuild(vap, false);
2263 } else
2264 #endif
2265 {
2266 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2267 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2268 sp->sa_uid = nfs_xdrneg1;
2269 sp->sa_gid = nfs_xdrneg1;
2270 sp->sa_size = nfs_xdrneg1;
2271 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2272 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2273 }
2274 nfsm_request1(dnp, NFSPROC_MKDIR, curlwp, cnp->cn_cred, &rexmit);
2275 if (!error)
2276 nfsm_mtofh(dvp, newvp, v3, gotvp);
2277 if (v3)
2278 nfsm_wcc_data(dvp, wccflag, 0, !error);
2279 nfsm_reqdone;
2280 VTONFS(dvp)->n_flag |= NMODIFIED;
2281 if (!wccflag)
2282 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2283 /*
2284 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2285 * if we can succeed in looking up the directory.
2286 */
2287 if ((rexmit && error == EEXIST) || (!error && !gotvp)) {
2288 if (newvp) {
2289 vput(newvp);
2290 newvp = (struct vnode *)0;
2291 }
2292 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2293 curlwp, &np);
2294 if (!error) {
2295 newvp = NFSTOV(np);
2296 if (newvp->v_type != VDIR || newvp == dvp)
2297 error = EEXIST;
2298 }
2299 }
2300 if (error) {
2301 if (newvp) {
2302 if (dvp != newvp)
2303 vput(newvp);
2304 else
2305 vrele(newvp);
2306 }
2307 } else {
2308 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
2309 if (cnp->cn_flags & MAKEENTRY)
2310 nfs_cache_enter(dvp, newvp, cnp);
2311 *ap->a_vpp = newvp;
2312 }
2313 PNBUF_PUT(cnp->cn_pnbuf);
2314 vput(dvp);
2315 return (error);
2316 }
2317
2318 /*
2319 * nfs remove directory call
2320 */
2321 int
2322 nfs_rmdir(void *v)
2323 {
2324 struct vop_rmdir_args /* {
2325 struct vnode *a_dvp;
2326 struct vnode *a_vp;
2327 struct componentname *a_cnp;
2328 } */ *ap = v;
2329 struct vnode *vp = ap->a_vp;
2330 struct vnode *dvp = ap->a_dvp;
2331 struct componentname *cnp = ap->a_cnp;
2332 u_int32_t *tl;
2333 char *cp;
2334 #ifndef NFS_V2_ONLY
2335 int32_t t1;
2336 char *cp2;
2337 #endif
2338 int32_t t2;
2339 char *bpos, *dpos;
2340 int error = 0, wccflag = NFSV3_WCCRATTR;
2341 int rexmit = 0;
2342 struct mbuf *mreq, *mrep, *md, *mb;
2343 const int v3 = NFS_ISV3(dvp);
2344 struct nfsnode *dnp;
2345
2346 if (dvp == vp) {
2347 vrele(dvp);
2348 vput(dvp);
2349 PNBUF_PUT(cnp->cn_pnbuf);
2350 return (EINVAL);
2351 }
2352 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2353 dnp = VTONFS(dvp);
2354 nfsm_reqhead(dnp, NFSPROC_RMDIR,
2355 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2356 nfsm_fhtom(dnp, v3);
2357 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2358 nfsm_request1(dnp, NFSPROC_RMDIR, curlwp, cnp->cn_cred, &rexmit);
2359 #ifndef NFS_V2_ONLY
2360 if (v3)
2361 nfsm_wcc_data(dvp, wccflag, 0, !error);
2362 #endif
2363 nfsm_reqdone;
2364 PNBUF_PUT(cnp->cn_pnbuf);
2365 VTONFS(dvp)->n_flag |= NMODIFIED;
2366 if (!wccflag)
2367 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2368 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
2369 VN_KNOTE(vp, NOTE_DELETE);
2370 cache_purge(vp);
2371 vput(vp);
2372 vput(dvp);
2373 /*
2374 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2375 */
2376 if (rexmit && error == ENOENT)
2377 error = 0;
2378 return (error);
2379 }
2380
2381 /*
2382 * nfs readdir call
2383 */
2384 int
2385 nfs_readdir(void *v)
2386 {
2387 struct vop_readdir_args /* {
2388 struct vnode *a_vp;
2389 struct uio *a_uio;
2390 kauth_cred_t a_cred;
2391 int *a_eofflag;
2392 off_t **a_cookies;
2393 int *a_ncookies;
2394 } */ *ap = v;
2395 struct vnode *vp = ap->a_vp;
2396 struct uio *uio = ap->a_uio;
2397 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2398 char *base = uio->uio_iov->iov_base;
2399 int tresid, error;
2400 size_t count, lost;
2401 struct dirent *dp;
2402 off_t *cookies = NULL;
2403 int ncookies = 0, nc;
2404
2405 if (vp->v_type != VDIR)
2406 return (EPERM);
2407
2408 lost = uio->uio_resid & (NFS_DIRFRAGSIZ - 1);
2409 count = uio->uio_resid - lost;
2410 if (count <= 0)
2411 return (EINVAL);
2412
2413 /*
2414 * Call nfs_bioread() to do the real work.
2415 */
2416 tresid = uio->uio_resid = count;
2417 error = nfs_bioread(vp, uio, 0, ap->a_cred,
2418 ap->a_cookies ? NFSBIO_CACHECOOKIES : 0);
2419
2420 if (!error && ap->a_cookies) {
2421 ncookies = count / 16;
2422 cookies = malloc(sizeof (off_t) * ncookies, M_TEMP, M_WAITOK);
2423 *ap->a_cookies = cookies;
2424 }
2425
2426 if (!error && uio->uio_resid == tresid) {
2427 uio->uio_resid += lost;
2428 nfsstats.direofcache_misses++;
2429 if (ap->a_cookies)
2430 *ap->a_ncookies = 0;
2431 *ap->a_eofflag = 1;
2432 return (0);
2433 }
2434
2435 if (!error && ap->a_cookies) {
2436 /*
2437 * Only the NFS server and emulations use cookies, and they
2438 * load the directory block into system space, so we can
2439 * just look at it directly.
2440 */
2441 if (!VMSPACE_IS_KERNEL_P(uio->uio_vmspace) ||
2442 uio->uio_iovcnt != 1)
2443 panic("nfs_readdir: lost in space");
2444 for (nc = 0; ncookies-- &&
2445 base < (char *)uio->uio_iov->iov_base; nc++){
2446 dp = (struct dirent *) base;
2447 if (dp->d_reclen == 0)
2448 break;
2449 if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
2450 *(cookies++) = (off_t)NFS_GETCOOKIE32(dp);
2451 else
2452 *(cookies++) = NFS_GETCOOKIE(dp);
2453 base += dp->d_reclen;
2454 }
2455 uio->uio_resid +=
2456 ((char *)uio->uio_iov->iov_base - base);
2457 uio->uio_iov->iov_len +=
2458 ((char *)uio->uio_iov->iov_base - base);
2459 uio->uio_iov->iov_base = base;
2460 *ap->a_ncookies = nc;
2461 }
2462
2463 uio->uio_resid += lost;
2464 *ap->a_eofflag = 0;
2465 return (error);
2466 }
2467
2468 /*
2469 * Readdir rpc call.
2470 * Called from below the buffer cache by nfs_doio().
2471 */
2472 int
2473 nfs_readdirrpc(struct vnode *vp, struct uio *uiop, kauth_cred_t cred)
2474 {
2475 int len, left;
2476 struct dirent *dp = NULL;
2477 u_int32_t *tl;
2478 char *cp;
2479 int32_t t1, t2;
2480 char *bpos, *dpos, *cp2;
2481 struct mbuf *mreq, *mrep, *md, *mb;
2482 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2483 struct nfsnode *dnp = VTONFS(vp);
2484 u_quad_t fileno;
2485 int error = 0, more_dirs = 1, blksiz = 0, bigenough = 1;
2486 #ifndef NFS_V2_ONLY
2487 int attrflag;
2488 #endif
2489 int nrpcs = 0, reclen;
2490 const int v3 = NFS_ISV3(vp);
2491
2492 #ifdef DIAGNOSTIC
2493 /*
2494 * Should be called from buffer cache, so only amount of
2495 * NFS_DIRBLKSIZ will be requested.
2496 */
2497 if (uiop->uio_iovcnt != 1 || uiop->uio_resid != NFS_DIRBLKSIZ)
2498 panic("nfs readdirrpc bad uio");
2499 #endif
2500
2501 /*
2502 * Loop around doing readdir rpc's of size nm_readdirsize
2503 * truncated to a multiple of NFS_DIRFRAGSIZ.
2504 * The stopping criteria is EOF or buffer full.
2505 */
2506 while (more_dirs && bigenough) {
2507 /*
2508 * Heuristic: don't bother to do another RPC to further
2509 * fill up this block if there is not much room left. (< 50%
2510 * of the readdir RPC size). This wastes some buffer space
2511 * but can save up to 50% in RPC calls.
2512 */
2513 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
2514 bigenough = 0;
2515 break;
2516 }
2517 nfsstats.rpccnt[NFSPROC_READDIR]++;
2518 nfsm_reqhead(dnp, NFSPROC_READDIR, NFSX_FH(v3) +
2519 NFSX_READDIR(v3));
2520 nfsm_fhtom(dnp, v3);
2521 #ifndef NFS_V2_ONLY
2522 if (v3) {
2523 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2524 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2525 txdr_swapcookie3(uiop->uio_offset, tl);
2526 } else {
2527 txdr_cookie3(uiop->uio_offset, tl);
2528 }
2529 tl += 2;
2530 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2531 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2532 } else
2533 #endif
2534 {
2535 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2536 *tl++ = txdr_unsigned(uiop->uio_offset);
2537 }
2538 *tl = txdr_unsigned(nmp->nm_readdirsize);
2539 nfsm_request(dnp, NFSPROC_READDIR, curlwp, cred);
2540 nrpcs++;
2541 #ifndef NFS_V2_ONLY
2542 if (v3) {
2543 nfsm_postop_attr(vp, attrflag, 0);
2544 if (!error) {
2545 nfsm_dissect(tl, u_int32_t *,
2546 2 * NFSX_UNSIGNED);
2547 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2548 dnp->n_cookieverf.nfsuquad[1] = *tl;
2549 } else {
2550 m_freem(mrep);
2551 goto nfsmout;
2552 }
2553 }
2554 #endif
2555 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2556 more_dirs = fxdr_unsigned(int, *tl);
2557
2558 /* loop thru the dir entries, doctoring them to 4bsd form */
2559 while (more_dirs && bigenough) {
2560 #ifndef NFS_V2_ONLY
2561 if (v3) {
2562 nfsm_dissect(tl, u_int32_t *,
2563 3 * NFSX_UNSIGNED);
2564 fileno = fxdr_hyper(tl);
2565 len = fxdr_unsigned(int, *(tl + 2));
2566 } else
2567 #endif
2568 {
2569 nfsm_dissect(tl, u_int32_t *,
2570 2 * NFSX_UNSIGNED);
2571 fileno = fxdr_unsigned(u_quad_t, *tl++);
2572 len = fxdr_unsigned(int, *tl);
2573 }
2574 if (len <= 0 || len > NFS_MAXNAMLEN) {
2575 error = EBADRPC;
2576 m_freem(mrep);
2577 goto nfsmout;
2578 }
2579 /* for cookie stashing */
2580 reclen = _DIRENT_RECLEN(dp, len) + 2 * sizeof(off_t);
2581 left = NFS_DIRFRAGSIZ - blksiz;
2582 if (reclen > left) {
2583 memset(uiop->uio_iov->iov_base, 0, left);
2584 dp->d_reclen += left;
2585 UIO_ADVANCE(uiop, left);
2586 blksiz = 0;
2587 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2588 }
2589 if (reclen > uiop->uio_resid)
2590 bigenough = 0;
2591 if (bigenough) {
2592 int tlen;
2593
2594 dp = (struct dirent *)uiop->uio_iov->iov_base;
2595 dp->d_fileno = fileno;
2596 dp->d_namlen = len;
2597 dp->d_reclen = reclen;
2598 dp->d_type = DT_UNKNOWN;
2599 blksiz += reclen;
2600 if (blksiz == NFS_DIRFRAGSIZ)
2601 blksiz = 0;
2602 UIO_ADVANCE(uiop, DIRHDSIZ);
2603 nfsm_mtouio(uiop, len);
2604 tlen = reclen - (DIRHDSIZ + len);
2605 (void)memset(uiop->uio_iov->iov_base, 0, tlen);
2606 UIO_ADVANCE(uiop, tlen);
2607 } else
2608 nfsm_adv(nfsm_rndup(len));
2609 #ifndef NFS_V2_ONLY
2610 if (v3) {
2611 nfsm_dissect(tl, u_int32_t *,
2612 3 * NFSX_UNSIGNED);
2613 } else
2614 #endif
2615 {
2616 nfsm_dissect(tl, u_int32_t *,
2617 2 * NFSX_UNSIGNED);
2618 }
2619 if (bigenough) {
2620 #ifndef NFS_V2_ONLY
2621 if (v3) {
2622 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2623 uiop->uio_offset =
2624 fxdr_swapcookie3(tl);
2625 else
2626 uiop->uio_offset =
2627 fxdr_cookie3(tl);
2628 }
2629 else
2630 #endif
2631 {
2632 uiop->uio_offset =
2633 fxdr_unsigned(off_t, *tl);
2634 }
2635 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2636 }
2637 if (v3)
2638 tl += 2;
2639 else
2640 tl++;
2641 more_dirs = fxdr_unsigned(int, *tl);
2642 }
2643 /*
2644 * If at end of rpc data, get the eof boolean
2645 */
2646 if (!more_dirs) {
2647 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2648 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2649
2650 /*
2651 * kludge: if we got no entries, treat it as EOF.
2652 * some server sometimes send a reply without any
2653 * entries or EOF.
2654 * although it might mean the server has very long name,
2655 * we can't handle such entries anyway.
2656 */
2657
2658 if (uiop->uio_resid >= NFS_DIRBLKSIZ)
2659 more_dirs = 0;
2660 }
2661 m_freem(mrep);
2662 }
2663 /*
2664 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2665 * by increasing d_reclen for the last record.
2666 */
2667 if (blksiz > 0) {
2668 left = NFS_DIRFRAGSIZ - blksiz;
2669 memset(uiop->uio_iov->iov_base, 0, left);
2670 dp->d_reclen += left;
2671 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2672 UIO_ADVANCE(uiop, left);
2673 }
2674
2675 /*
2676 * We are now either at the end of the directory or have filled the
2677 * block.
2678 */
2679 if (bigenough) {
2680 dnp->n_direofoffset = uiop->uio_offset;
2681 dnp->n_flag |= NEOFVALID;
2682 }
2683 nfsmout:
2684 return (error);
2685 }
2686
2687 #ifndef NFS_V2_ONLY
2688 /*
2689 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2690 */
2691 int
2692 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop, kauth_cred_t cred)
2693 {
2694 int len, left;
2695 struct dirent *dp = NULL;
2696 u_int32_t *tl;
2697 char *cp;
2698 int32_t t1, t2;
2699 struct vnode *newvp;
2700 char *bpos, *dpos, *cp2;
2701 struct mbuf *mreq, *mrep, *md, *mb;
2702 struct nameidata nami, *ndp = &nami;
2703 struct componentname *cnp = &ndp->ni_cnd;
2704 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2705 struct nfsnode *dnp = VTONFS(vp), *np;
2706 nfsfh_t *fhp;
2707 u_quad_t fileno;
2708 int error = 0, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2709 int attrflag, fhsize, nrpcs = 0, reclen;
2710 struct nfs_fattr fattr, *fp;
2711
2712 #ifdef DIAGNOSTIC
2713 if (uiop->uio_iovcnt != 1 || uiop->uio_resid != NFS_DIRBLKSIZ)
2714 panic("nfs readdirplusrpc bad uio");
2715 #endif
2716 ndp->ni_dvp = vp;
2717 newvp = NULLVP;
2718
2719 /*
2720 * Loop around doing readdir rpc's of size nm_readdirsize
2721 * truncated to a multiple of NFS_DIRFRAGSIZ.
2722 * The stopping criteria is EOF or buffer full.
2723 */
2724 while (more_dirs && bigenough) {
2725 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
2726 bigenough = 0;
2727 break;
2728 }
2729 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2730 nfsm_reqhead(dnp, NFSPROC_READDIRPLUS,
2731 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2732 nfsm_fhtom(dnp, 1);
2733 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2734 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2735 txdr_swapcookie3(uiop->uio_offset, tl);
2736 } else {
2737 txdr_cookie3(uiop->uio_offset, tl);
2738 }
2739 tl += 2;
2740 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2741 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2742 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2743 *tl = txdr_unsigned(nmp->nm_rsize);
2744 nfsm_request(dnp, NFSPROC_READDIRPLUS, curlwp, cred);
2745 nfsm_postop_attr(vp, attrflag, 0);
2746 if (error) {
2747 m_freem(mrep);
2748 goto nfsmout;
2749 }
2750 nrpcs++;
2751 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2752 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2753 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2754 more_dirs = fxdr_unsigned(int, *tl);
2755
2756 /* loop thru the dir entries, doctoring them to 4bsd form */
2757 while (more_dirs && bigenough) {
2758 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2759 fileno = fxdr_hyper(tl);
2760 len = fxdr_unsigned(int, *(tl + 2));
2761 if (len <= 0 || len > NFS_MAXNAMLEN) {
2762 error = EBADRPC;
2763 m_freem(mrep);
2764 goto nfsmout;
2765 }
2766 /* for cookie stashing */
2767 reclen = _DIRENT_RECLEN(dp, len) + 2 * sizeof(off_t);
2768 left = NFS_DIRFRAGSIZ - blksiz;
2769 if (reclen > left) {
2770 /*
2771 * DIRFRAGSIZ is aligned, no need to align
2772 * again here.
2773 */
2774 memset(uiop->uio_iov->iov_base, 0, left);
2775 dp->d_reclen += left;
2776 UIO_ADVANCE(uiop, left);
2777 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2778 blksiz = 0;
2779 }
2780 if (reclen > uiop->uio_resid)
2781 bigenough = 0;
2782 if (bigenough) {
2783 int tlen;
2784
2785 dp = (struct dirent *)uiop->uio_iov->iov_base;
2786 dp->d_fileno = fileno;
2787 dp->d_namlen = len;
2788 dp->d_reclen = reclen;
2789 dp->d_type = DT_UNKNOWN;
2790 blksiz += reclen;
2791 if (blksiz == NFS_DIRFRAGSIZ)
2792 blksiz = 0;
2793 UIO_ADVANCE(uiop, DIRHDSIZ);
2794 nfsm_mtouio(uiop, len);
2795 tlen = reclen - (DIRHDSIZ + len);
2796 (void)memset(uiop->uio_iov->iov_base, 0, tlen);
2797 UIO_ADVANCE(uiop, tlen);
2798 cnp->cn_nameptr = dp->d_name;
2799 cnp->cn_namelen = dp->d_namlen;
2800 } else
2801 nfsm_adv(nfsm_rndup(len));
2802 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2803 if (bigenough) {
2804 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2805 uiop->uio_offset =
2806 fxdr_swapcookie3(tl);
2807 else
2808 uiop->uio_offset =
2809 fxdr_cookie3(tl);
2810 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2811 }
2812 tl += 2;
2813
2814 /*
2815 * Since the attributes are before the file handle
2816 * (sigh), we must skip over the attributes and then
2817 * come back and get them.
2818 */
2819 attrflag = fxdr_unsigned(int, *tl);
2820 if (attrflag) {
2821 nfsm_dissect(fp, struct nfs_fattr *, NFSX_V3FATTR);
2822 memcpy(&fattr, fp, NFSX_V3FATTR);
2823 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2824 doit = fxdr_unsigned(int, *tl);
2825 if (doit) {
2826 nfsm_getfh(fhp, fhsize, 1);
2827 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2828 VREF(vp);
2829 newvp = vp;
2830 np = dnp;
2831 } else {
2832 error = nfs_nget1(vp->v_mount, fhp,
2833 fhsize, &np, LK_NOWAIT);
2834 if (!error)
2835 newvp = NFSTOV(np);
2836 }
2837 if (!error) {
2838 const char *xcp;
2839
2840 nfs_loadattrcache(&newvp, &fattr, 0, 0);
2841 if (bigenough) {
2842 dp->d_type =
2843 IFTODT(VTTOIF(np->n_vattr->va_type));
2844 if (cnp->cn_namelen <= NCHNAMLEN) {
2845 ndp->ni_vp = newvp;
2846 xcp = cnp->cn_nameptr +
2847 cnp->cn_namelen;
2848 cnp->cn_hash =
2849 namei_hash(cnp->cn_nameptr, &xcp);
2850 nfs_cache_enter(ndp->ni_dvp,
2851 ndp->ni_vp, cnp);
2852 }
2853 }
2854 }
2855 error = 0;
2856 }
2857 } else {
2858 /* Just skip over the file handle */
2859 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2860 i = fxdr_unsigned(int, *tl);
2861 nfsm_adv(nfsm_rndup(i));
2862 }
2863 if (newvp != NULLVP) {
2864 if (newvp == vp)
2865 vrele(newvp);
2866 else
2867 vput(newvp);
2868 newvp = NULLVP;
2869 }
2870 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2871 more_dirs = fxdr_unsigned(int, *tl);
2872 }
2873 /*
2874 * If at end of rpc data, get the eof boolean
2875 */
2876 if (!more_dirs) {
2877 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2878 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2879
2880 /*
2881 * kludge: see a comment in nfs_readdirrpc.
2882 */
2883
2884 if (uiop->uio_resid >= NFS_DIRBLKSIZ)
2885 more_dirs = 0;
2886 }
2887 m_freem(mrep);
2888 }
2889 /*
2890 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2891 * by increasing d_reclen for the last record.
2892 */
2893 if (blksiz > 0) {
2894 left = NFS_DIRFRAGSIZ - blksiz;
2895 memset(uiop->uio_iov->iov_base, 0, left);
2896 dp->d_reclen += left;
2897 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2898 UIO_ADVANCE(uiop, left);
2899 }
2900
2901 /*
2902 * We are now either at the end of the directory or have filled the
2903 * block.
2904 */
2905 if (bigenough) {
2906 dnp->n_direofoffset = uiop->uio_offset;
2907 dnp->n_flag |= NEOFVALID;
2908 }
2909 nfsmout:
2910 if (newvp != NULLVP) {
2911 if(newvp == vp)
2912 vrele(newvp);
2913 else
2914 vput(newvp);
2915 }
2916 return (error);
2917 }
2918 #endif
2919
2920 /*
2921 * Silly rename. To make the NFS filesystem that is stateless look a little
2922 * more like the "ufs" a remove of an active vnode is translated to a rename
2923 * to a funny looking filename that is removed by nfs_inactive on the
2924 * nfsnode. There is the potential for another process on a different client
2925 * to create the same funny name between the nfs_lookitup() fails and the
2926 * nfs_rename() completes, but...
2927 */
2928 int
2929 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp, bool dolink)
2930 {
2931 struct sillyrename *sp;
2932 struct nfsnode *np;
2933 int error;
2934 pid_t pid;
2935
2936 cache_purge(dvp);
2937 np = VTONFS(vp);
2938 #ifndef DIAGNOSTIC
2939 if (vp->v_type == VDIR)
2940 panic("nfs: sillyrename dir");
2941 #endif
2942 sp = kmem_alloc(sizeof(*sp), KM_SLEEP);
2943 sp->s_cred = kauth_cred_dup(cnp->cn_cred);
2944 sp->s_dvp = dvp;
2945 VREF(dvp);
2946
2947 /* Fudge together a funny name */
2948 pid = curlwp->l_proc->p_pid;
2949 memcpy(sp->s_name, ".nfsAxxxx4.4", 13);
2950 sp->s_namlen = 12;
2951 sp->s_name[8] = hexdigits[pid & 0xf];
2952 sp->s_name[7] = hexdigits[(pid >> 4) & 0xf];
2953 sp->s_name[6] = hexdigits[(pid >> 8) & 0xf];
2954 sp->s_name[5] = hexdigits[(pid >> 12) & 0xf];
2955
2956 /* Try lookitups until we get one that isn't there */
2957 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2958 curlwp, (struct nfsnode **)0) == 0) {
2959 sp->s_name[4]++;
2960 if (sp->s_name[4] > 'z') {
2961 error = EINVAL;
2962 goto bad;
2963 }
2964 }
2965 if (dolink) {
2966 error = nfs_linkrpc(dvp, vp, sp->s_name, sp->s_namlen,
2967 sp->s_cred, curlwp);
2968 /*
2969 * nfs_request maps NFSERR_NOTSUPP to ENOTSUP.
2970 */
2971 if (error == ENOTSUP) {
2972 error = nfs_renameit(dvp, cnp, sp);
2973 }
2974 } else {
2975 error = nfs_renameit(dvp, cnp, sp);
2976 }
2977 if (error)
2978 goto bad;
2979 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2980 curlwp, &np);
2981 np->n_sillyrename = sp;
2982 return (0);
2983 bad:
2984 vrele(sp->s_dvp);
2985 kauth_cred_free(sp->s_cred);
2986 kmem_free(sp, sizeof(*sp));
2987 return (error);
2988 }
2989
2990 /*
2991 * Look up a file name and optionally either update the file handle or
2992 * allocate an nfsnode, depending on the value of npp.
2993 * npp == NULL --> just do the lookup
2994 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2995 * handled too
2996 * *npp != NULL --> update the file handle in the vnode
2997 */
2998 int
2999 nfs_lookitup(struct vnode *dvp, const char *name, int len, kauth_cred_t cred, struct lwp *l, struct nfsnode **npp)
3000 {
3001 u_int32_t *tl;
3002 char *cp;
3003 int32_t t1, t2;
3004 struct vnode *newvp = (struct vnode *)0;
3005 struct nfsnode *np, *dnp = VTONFS(dvp);
3006 char *bpos, *dpos, *cp2;
3007 int error = 0, fhlen;
3008 #ifndef NFS_V2_ONLY
3009 int attrflag;
3010 #endif
3011 struct mbuf *mreq, *mrep, *md, *mb;
3012 nfsfh_t *nfhp;
3013 const int v3 = NFS_ISV3(dvp);
3014
3015 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
3016 nfsm_reqhead(dnp, NFSPROC_LOOKUP,
3017 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
3018 nfsm_fhtom(dnp, v3);
3019 nfsm_strtom(name, len, NFS_MAXNAMLEN);
3020 nfsm_request(dnp, NFSPROC_LOOKUP, l, cred);
3021 if (npp && !error) {
3022 nfsm_getfh(nfhp, fhlen, v3);
3023 if (*npp) {
3024 np = *npp;
3025 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
3026 kmem_free(np->n_fhp, np->n_fhsize);
3027 np->n_fhp = &np->n_fh;
3028 }
3029 #if NFS_SMALLFH < NFSX_V3FHMAX
3030 else if (np->n_fhsize <= NFS_SMALLFH && fhlen > NFS_SMALLFH)
3031 np->n_fhp = kmem_alloc(fhlen, KM_SLEEP);
3032 #endif
3033 memcpy(np->n_fhp, nfhp, fhlen);
3034 np->n_fhsize = fhlen;
3035 newvp = NFSTOV(np);
3036 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
3037 VREF(dvp);
3038 newvp = dvp;
3039 np = dnp;
3040 } else {
3041 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
3042 if (error) {
3043 m_freem(mrep);
3044 return (error);
3045 }
3046 newvp = NFSTOV(np);
3047 }
3048 #ifndef NFS_V2_ONLY
3049 if (v3) {
3050 nfsm_postop_attr(newvp, attrflag, 0);
3051 if (!attrflag && *npp == NULL) {
3052 m_freem(mrep);
3053 vput(newvp);
3054 return (ENOENT);
3055 }
3056 } else
3057 #endif
3058 nfsm_loadattr(newvp, (struct vattr *)0, 0);
3059 }
3060 nfsm_reqdone;
3061 if (npp && *npp == NULL) {
3062 if (error) {
3063 if (newvp)
3064 vput(newvp);
3065 } else
3066 *npp = np;
3067 }
3068 return (error);
3069 }
3070
3071 #ifndef NFS_V2_ONLY
3072 /*
3073 * Nfs Version 3 commit rpc
3074 */
3075 int
3076 nfs_commit(struct vnode *vp, off_t offset, uint32_t cnt, struct lwp *l)
3077 {
3078 char *cp;
3079 u_int32_t *tl;
3080 int32_t t1, t2;
3081 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3082 char *bpos, *dpos, *cp2;
3083 int error = 0, wccflag = NFSV3_WCCRATTR;
3084 struct mbuf *mreq, *mrep, *md, *mb;
3085 struct nfsnode *np;
3086
3087 KASSERT(NFS_ISV3(vp));
3088
3089 #ifdef NFS_DEBUG_COMMIT
3090 printf("commit %lu - %lu\n", (unsigned long)offset,
3091 (unsigned long)(offset + cnt));
3092 #endif
3093
3094 mutex_enter(&nmp->nm_lock);
3095 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0) {
3096 mutex_exit(&nmp->nm_lock);
3097 return (0);
3098 }
3099 mutex_exit(&nmp->nm_lock);
3100 nfsstats.rpccnt[NFSPROC_COMMIT]++;
3101 np = VTONFS(vp);
3102 nfsm_reqhead(np, NFSPROC_COMMIT, NFSX_FH(1));
3103 nfsm_fhtom(np, 1);
3104 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3105 txdr_hyper(offset, tl);
3106 tl += 2;
3107 *tl = txdr_unsigned(cnt);
3108 nfsm_request(np, NFSPROC_COMMIT, l, np->n_wcred);
3109 nfsm_wcc_data(vp, wccflag, NAC_NOTRUNC, false);
3110 if (!error) {
3111 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
3112 mutex_enter(&nmp->nm_lock);
3113 if ((nmp->nm_iflag & NFSMNT_STALEWRITEVERF) ||
3114 memcmp(nmp->nm_writeverf, tl, NFSX_V3WRITEVERF)) {
3115 memcpy(nmp->nm_writeverf, tl, NFSX_V3WRITEVERF);
3116 error = NFSERR_STALEWRITEVERF;
3117 nmp->nm_iflag |= NFSMNT_STALEWRITEVERF;
3118 }
3119 mutex_exit(&nmp->nm_lock);
3120 }
3121 nfsm_reqdone;
3122 return (error);
3123 }
3124 #endif
3125
3126 /*
3127 * Kludge City..
3128 * - make nfs_bmap() essentially a no-op that does no translation
3129 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
3130 * (Maybe I could use the process's page mapping, but I was concerned that
3131 * Kernel Write might not be enabled and also figured copyout() would do
3132 * a lot more work than memcpy() and also it currently happens in the
3133 * context of the swapper process (2).
3134 */
3135 int
3136 nfs_bmap(void *v)
3137 {
3138 struct vop_bmap_args /* {
3139 struct vnode *a_vp;
3140 daddr_t a_bn;
3141 struct vnode **a_vpp;
3142 daddr_t *a_bnp;
3143 int *a_runp;
3144 } */ *ap = v;
3145 struct vnode *vp = ap->a_vp;
3146 int bshift = vp->v_mount->mnt_fs_bshift - vp->v_mount->mnt_dev_bshift;
3147
3148 if (ap->a_vpp != NULL)
3149 *ap->a_vpp = vp;
3150 if (ap->a_bnp != NULL)
3151 *ap->a_bnp = ap->a_bn << bshift;
3152 if (ap->a_runp != NULL)
3153 *ap->a_runp = 1024 * 1024; /* XXX */
3154 return (0);
3155 }
3156
3157 /*
3158 * Strategy routine.
3159 * For async requests when nfsiod(s) are running, queue the request by
3160 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
3161 * request.
3162 */
3163 int
3164 nfs_strategy(void *v)
3165 {
3166 struct vop_strategy_args *ap = v;
3167 struct buf *bp = ap->a_bp;
3168 int error = 0;
3169
3170 if ((bp->b_flags & (B_PHYS|B_ASYNC)) == (B_PHYS|B_ASYNC))
3171 panic("nfs physio/async");
3172
3173 /*
3174 * If the op is asynchronous and an i/o daemon is waiting
3175 * queue the request, wake it up and wait for completion
3176 * otherwise just do it ourselves.
3177 */
3178 if ((bp->b_flags & B_ASYNC) == 0 || nfs_asyncio(bp))
3179 error = nfs_doio(bp);
3180 return (error);
3181 }
3182
3183 /*
3184 * fsync vnode op. Just call nfs_flush() with commit == 1.
3185 */
3186 /* ARGSUSED */
3187 int
3188 nfs_fsync(void *v)
3189 {
3190 struct vop_fsync_args /* {
3191 struct vnodeop_desc *a_desc;
3192 struct vnode * a_vp;
3193 kauth_cred_t a_cred;
3194 int a_flags;
3195 off_t offlo;
3196 off_t offhi;
3197 struct lwp * a_l;
3198 } */ *ap = v;
3199
3200 struct vnode *vp = ap->a_vp;
3201
3202 if (vp->v_type != VREG)
3203 return 0;
3204
3205 return (nfs_flush(vp, ap->a_cred,
3206 (ap->a_flags & FSYNC_WAIT) != 0 ? MNT_WAIT : 0, curlwp, 1));
3207 }
3208
3209 /*
3210 * Flush all the data associated with a vnode.
3211 */
3212 int
3213 nfs_flush(struct vnode *vp, kauth_cred_t cred, int waitfor, struct lwp *l,
3214 int commit)
3215 {
3216 struct nfsnode *np = VTONFS(vp);
3217 int error;
3218 int flushflags = PGO_ALLPAGES|PGO_CLEANIT|PGO_SYNCIO;
3219 UVMHIST_FUNC("nfs_flush"); UVMHIST_CALLED(ubchist);
3220
3221 mutex_enter(&vp->v_interlock);
3222 error = VOP_PUTPAGES(vp, 0, 0, flushflags);
3223 if (np->n_flag & NWRITEERR) {
3224 error = np->n_error;
3225 np->n_flag &= ~NWRITEERR;
3226 }
3227 UVMHIST_LOG(ubchist, "returning %d", error,0,0,0);
3228 return (error);
3229 }
3230
3231 /*
3232 * Return POSIX pathconf information applicable to nfs.
3233 *
3234 * N.B. The NFS V2 protocol doesn't support this RPC.
3235 */
3236 /* ARGSUSED */
3237 int
3238 nfs_pathconf(void *v)
3239 {
3240 struct vop_pathconf_args /* {
3241 struct vnode *a_vp;
3242 int a_name;
3243 register_t *a_retval;
3244 } */ *ap = v;
3245 struct nfsv3_pathconf *pcp;
3246 struct vnode *vp = ap->a_vp;
3247 struct mbuf *mreq, *mrep, *md, *mb;
3248 int32_t t1, t2;
3249 u_int32_t *tl;
3250 char *bpos, *dpos, *cp, *cp2;
3251 int error = 0, attrflag;
3252 #ifndef NFS_V2_ONLY
3253 struct nfsmount *nmp;
3254 unsigned int l;
3255 u_int64_t maxsize;
3256 #endif
3257 const int v3 = NFS_ISV3(vp);
3258 struct nfsnode *np = VTONFS(vp);
3259
3260 switch (ap->a_name) {
3261 /* Names that can be resolved locally. */
3262 case _PC_PIPE_BUF:
3263 *ap->a_retval = PIPE_BUF;
3264 break;
3265 case _PC_SYNC_IO:
3266 *ap->a_retval = 1;
3267 break;
3268 /* Names that cannot be resolved locally; do an RPC, if possible. */
3269 case _PC_LINK_MAX:
3270 case _PC_NAME_MAX:
3271 case _PC_CHOWN_RESTRICTED:
3272 case _PC_NO_TRUNC:
3273 if (!v3) {
3274 error = EINVAL;
3275 break;
3276 }
3277 nfsstats.rpccnt[NFSPROC_PATHCONF]++;
3278 nfsm_reqhead(np, NFSPROC_PATHCONF, NFSX_FH(1));
3279 nfsm_fhtom(np, 1);
3280 nfsm_request(np, NFSPROC_PATHCONF,
3281 curlwp, curlwp->l_cred); /* XXX */
3282 nfsm_postop_attr(vp, attrflag, 0);
3283 if (!error) {
3284 nfsm_dissect(pcp, struct nfsv3_pathconf *,
3285 NFSX_V3PATHCONF);
3286 switch (ap->a_name) {
3287 case _PC_LINK_MAX:
3288 *ap->a_retval =
3289 fxdr_unsigned(register_t, pcp->pc_linkmax);
3290 break;
3291 case _PC_NAME_MAX:
3292 *ap->a_retval =
3293 fxdr_unsigned(register_t, pcp->pc_namemax);
3294 break;
3295 case _PC_CHOWN_RESTRICTED:
3296 *ap->a_retval =
3297 (pcp->pc_chownrestricted == nfs_true);
3298 break;
3299 case _PC_NO_TRUNC:
3300 *ap->a_retval =
3301 (pcp->pc_notrunc == nfs_true);
3302 break;
3303 }
3304 }
3305 nfsm_reqdone;
3306 break;
3307 case _PC_FILESIZEBITS:
3308 #ifndef NFS_V2_ONLY
3309 if (v3) {
3310 nmp = VFSTONFS(vp->v_mount);
3311 if ((nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
3312 if ((error = nfs_fsinfo(nmp, vp,
3313 curlwp->l_cred, curlwp)) != 0) /* XXX */
3314 break;
3315 for (l = 0, maxsize = nmp->nm_maxfilesize;
3316 (maxsize >> l) > 0; l++)
3317 ;
3318 *ap->a_retval = l + 1;
3319 } else
3320 #endif
3321 {
3322 *ap->a_retval = 32; /* NFS V2 limitation */
3323 }
3324 break;
3325 default:
3326 error = EINVAL;
3327 break;
3328 }
3329
3330 return (error);
3331 }
3332
3333 /*
3334 * NFS advisory byte-level locks.
3335 */
3336 int
3337 nfs_advlock(void *v)
3338 {
3339 struct vop_advlock_args /* {
3340 struct vnode *a_vp;
3341 void *a_id;
3342 int a_op;
3343 struct flock *a_fl;
3344 int a_flags;
3345 } */ *ap = v;
3346 struct nfsnode *np = VTONFS(ap->a_vp);
3347
3348 return lf_advlock(ap, &np->n_lockf, np->n_size);
3349 }
3350
3351 /*
3352 * Print out the contents of an nfsnode.
3353 */
3354 int
3355 nfs_print(void *v)
3356 {
3357 struct vop_print_args /* {
3358 struct vnode *a_vp;
3359 } */ *ap = v;
3360 struct vnode *vp = ap->a_vp;
3361 struct nfsnode *np = VTONFS(vp);
3362
3363 printf("tag VT_NFS, fileid %lld fsid 0x%llx",
3364 (unsigned long long)np->n_vattr->va_fileid,
3365 (unsigned long long)np->n_vattr->va_fsid);
3366 if (vp->v_type == VFIFO)
3367 fifo_printinfo(vp);
3368 printf("\n");
3369 return (0);
3370 }
3371
3372 /*
3373 * nfs unlock wrapper.
3374 */
3375 int
3376 nfs_unlock(void *v)
3377 {
3378 struct vop_unlock_args /* {
3379 struct vnode *a_vp;
3380 int a_flags;
3381 } */ *ap = v;
3382 struct vnode *vp = ap->a_vp;
3383
3384 /*
3385 * VOP_UNLOCK can be called by nfs_loadattrcache
3386 * with v_data == 0.
3387 */
3388 if (VTONFS(vp)) {
3389 nfs_delayedtruncate(vp);
3390 }
3391
3392 return genfs_unlock(v);
3393 }
3394
3395 /*
3396 * nfs special file access vnode op.
3397 * Essentially just get vattr and then imitate iaccess() since the device is
3398 * local to the client.
3399 */
3400 int
3401 nfsspec_access(void *v)
3402 {
3403 struct vop_access_args /* {
3404 struct vnode *a_vp;
3405 int a_mode;
3406 kauth_cred_t a_cred;
3407 struct lwp *a_l;
3408 } */ *ap = v;
3409 struct vattr va;
3410 struct vnode *vp = ap->a_vp;
3411 int error;
3412
3413 error = VOP_GETATTR(vp, &va, ap->a_cred);
3414 if (error)
3415 return (error);
3416
3417 /*
3418 * Disallow write attempts on filesystems mounted read-only;
3419 * unless the file is a socket, fifo, or a block or character
3420 * device resident on the filesystem.
3421 */
3422 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3423 switch (vp->v_type) {
3424 case VREG:
3425 case VDIR:
3426 case VLNK:
3427 return (EROFS);
3428 default:
3429 break;
3430 }
3431 }
3432
3433 return (genfs_can_access(va.va_type, va.va_mode,
3434 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
3435 }
3436
3437 /*
3438 * Read wrapper for special devices.
3439 */
3440 int
3441 nfsspec_read(void *v)
3442 {
3443 struct vop_read_args /* {
3444 struct vnode *a_vp;
3445 struct uio *a_uio;
3446 int a_ioflag;
3447 kauth_cred_t a_cred;
3448 } */ *ap = v;
3449 struct nfsnode *np = VTONFS(ap->a_vp);
3450
3451 /*
3452 * Set access flag.
3453 */
3454 np->n_flag |= NACC;
3455 getnanotime(&np->n_atim);
3456 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3457 }
3458
3459 /*
3460 * Write wrapper for special devices.
3461 */
3462 int
3463 nfsspec_write(void *v)
3464 {
3465 struct vop_write_args /* {
3466 struct vnode *a_vp;
3467 struct uio *a_uio;
3468 int a_ioflag;
3469 kauth_cred_t a_cred;
3470 } */ *ap = v;
3471 struct nfsnode *np = VTONFS(ap->a_vp);
3472
3473 /*
3474 * Set update flag.
3475 */
3476 np->n_flag |= NUPD;
3477 getnanotime(&np->n_mtim);
3478 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3479 }
3480
3481 /*
3482 * Close wrapper for special devices.
3483 *
3484 * Update the times on the nfsnode then do device close.
3485 */
3486 int
3487 nfsspec_close(void *v)
3488 {
3489 struct vop_close_args /* {
3490 struct vnode *a_vp;
3491 int a_fflag;
3492 kauth_cred_t a_cred;
3493 struct lwp *a_l;
3494 } */ *ap = v;
3495 struct vnode *vp = ap->a_vp;
3496 struct nfsnode *np = VTONFS(vp);
3497 struct vattr vattr;
3498
3499 if (np->n_flag & (NACC | NUPD)) {
3500 np->n_flag |= NCHG;
3501 if (vp->v_usecount == 1 &&
3502 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3503 VATTR_NULL(&vattr);
3504 if (np->n_flag & NACC)
3505 vattr.va_atime = np->n_atim;
3506 if (np->n_flag & NUPD)
3507 vattr.va_mtime = np->n_mtim;
3508 (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3509 }
3510 }
3511 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3512 }
3513
3514 /*
3515 * Read wrapper for fifos.
3516 */
3517 int
3518 nfsfifo_read(void *v)
3519 {
3520 struct vop_read_args /* {
3521 struct vnode *a_vp;
3522 struct uio *a_uio;
3523 int a_ioflag;
3524 kauth_cred_t a_cred;
3525 } */ *ap = v;
3526 struct nfsnode *np = VTONFS(ap->a_vp);
3527
3528 /*
3529 * Set access flag.
3530 */
3531 np->n_flag |= NACC;
3532 getnanotime(&np->n_atim);
3533 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3534 }
3535
3536 /*
3537 * Write wrapper for fifos.
3538 */
3539 int
3540 nfsfifo_write(void *v)
3541 {
3542 struct vop_write_args /* {
3543 struct vnode *a_vp;
3544 struct uio *a_uio;
3545 int a_ioflag;
3546 kauth_cred_t a_cred;
3547 } */ *ap = v;
3548 struct nfsnode *np = VTONFS(ap->a_vp);
3549
3550 /*
3551 * Set update flag.
3552 */
3553 np->n_flag |= NUPD;
3554 getnanotime(&np->n_mtim);
3555 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3556 }
3557
3558 /*
3559 * Close wrapper for fifos.
3560 *
3561 * Update the times on the nfsnode then do fifo close.
3562 */
3563 int
3564 nfsfifo_close(void *v)
3565 {
3566 struct vop_close_args /* {
3567 struct vnode *a_vp;
3568 int a_fflag;
3569 kauth_cred_t a_cred;
3570 struct lwp *a_l;
3571 } */ *ap = v;
3572 struct vnode *vp = ap->a_vp;
3573 struct nfsnode *np = VTONFS(vp);
3574 struct vattr vattr;
3575
3576 if (np->n_flag & (NACC | NUPD)) {
3577 struct timespec ts;
3578
3579 getnanotime(&ts);
3580 if (np->n_flag & NACC)
3581 np->n_atim = ts;
3582 if (np->n_flag & NUPD)
3583 np->n_mtim = ts;
3584 np->n_flag |= NCHG;
3585 if (vp->v_usecount == 1 &&
3586 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3587 VATTR_NULL(&vattr);
3588 if (np->n_flag & NACC)
3589 vattr.va_atime = np->n_atim;
3590 if (np->n_flag & NUPD)
3591 vattr.va_mtime = np->n_mtim;
3592 (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3593 }
3594 }
3595 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
3596 }
3597