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