2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95
37 * $FreeBSD: src/sys/nfs/nfs_vnops.c,v 1.150.2.5 2001/12/20 19:56:28 dillon Exp $
38 * $DragonFly: src/sys/vfs/nfs/nfs_vnops.c,v 1.62 2006/07/18 22:22:15 dillon Exp $
43 * vnode op calls for Sun NFS version 2 and 3
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/resourcevar.h>
53 #include <sys/mount.h>
55 #include <sys/malloc.h>
57 #include <sys/namei.h>
58 #include <sys/nlookup.h>
59 #include <sys/socket.h>
60 #include <sys/vnode.h>
61 #include <sys/dirent.h>
62 #include <sys/fcntl.h>
63 #include <sys/lockf.h>
65 #include <sys/sysctl.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_zone.h>
74 #include <vfs/fifofs/fifo.h>
75 #include <vfs/ufs/dir.h>
85 #include "nfsm_subs.h"
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
91 #include <sys/thread2.h>
97 static int nfsspec_read (struct vop_read_args *);
98 static int nfsspec_write (struct vop_write_args *);
99 static int nfsfifo_read (struct vop_read_args *);
100 static int nfsfifo_write (struct vop_write_args *);
101 static int nfsspec_close (struct vop_close_args *);
102 static int nfsfifo_close (struct vop_close_args *);
103 #define nfs_poll vop_nopoll
104 static int nfs_setattrrpc (struct vnode *,struct vattr *,struct ucred *,struct thread *);
105 static int nfs_lookup (struct vop_old_lookup_args *);
106 static int nfs_create (struct vop_old_create_args *);
107 static int nfs_mknod (struct vop_old_mknod_args *);
108 static int nfs_open (struct vop_open_args *);
109 static int nfs_close (struct vop_close_args *);
110 static int nfs_access (struct vop_access_args *);
111 static int nfs_getattr (struct vop_getattr_args *);
112 static int nfs_setattr (struct vop_setattr_args *);
113 static int nfs_read (struct vop_read_args *);
114 static int nfs_mmap (struct vop_mmap_args *);
115 static int nfs_fsync (struct vop_fsync_args *);
116 static int nfs_remove (struct vop_old_remove_args *);
117 static int nfs_link (struct vop_old_link_args *);
118 static int nfs_rename (struct vop_old_rename_args *);
119 static int nfs_mkdir (struct vop_old_mkdir_args *);
120 static int nfs_rmdir (struct vop_old_rmdir_args *);
121 static int nfs_symlink (struct vop_old_symlink_args *);
122 static int nfs_readdir (struct vop_readdir_args *);
123 static int nfs_bmap (struct vop_bmap_args *);
124 static int nfs_strategy (struct vop_strategy_args *);
125 static int nfs_lookitup (struct vnode *, const char *, int,
126 struct ucred *, struct thread *, struct nfsnode **);
127 static int nfs_sillyrename (struct vnode *,struct vnode *,struct componentname *);
128 static int nfsspec_access (struct vop_access_args *);
129 static int nfs_readlink (struct vop_readlink_args *);
130 static int nfs_print (struct vop_print_args *);
131 static int nfs_advlock (struct vop_advlock_args *);
133 static int nfs_nresolve (struct vop_nresolve_args *);
135 * Global vfs data structures for nfs
137 struct vop_ops nfsv2_vnode_vops = {
138 .vop_default = vop_defaultop,
139 .vop_access = nfs_access,
140 .vop_advlock = nfs_advlock,
141 .vop_bmap = nfs_bmap,
142 .vop_close = nfs_close,
143 .vop_old_create = nfs_create,
144 .vop_fsync = nfs_fsync,
145 .vop_getattr = nfs_getattr,
146 .vop_getpages = nfs_getpages,
147 .vop_putpages = nfs_putpages,
148 .vop_inactive = nfs_inactive,
149 .vop_islocked = vop_stdislocked,
150 .vop_old_link = nfs_link,
151 .vop_lock = vop_stdlock,
152 .vop_old_lookup = nfs_lookup,
153 .vop_old_mkdir = nfs_mkdir,
154 .vop_old_mknod = nfs_mknod,
155 .vop_mmap = nfs_mmap,
156 .vop_open = nfs_open,
157 .vop_poll = nfs_poll,
158 .vop_print = nfs_print,
159 .vop_read = nfs_read,
160 .vop_readdir = nfs_readdir,
161 .vop_readlink = nfs_readlink,
162 .vop_reclaim = nfs_reclaim,
163 .vop_old_remove = nfs_remove,
164 .vop_old_rename = nfs_rename,
165 .vop_old_rmdir = nfs_rmdir,
166 .vop_setattr = nfs_setattr,
167 .vop_strategy = nfs_strategy,
168 .vop_old_symlink = nfs_symlink,
169 .vop_unlock = vop_stdunlock,
170 .vop_write = nfs_write,
171 .vop_nresolve = nfs_nresolve
175 * Special device vnode ops
177 struct vop_ops nfsv2_spec_vops = {
178 .vop_default = spec_vnoperate,
179 .vop_access = nfsspec_access,
180 .vop_close = nfsspec_close,
181 .vop_fsync = nfs_fsync,
182 .vop_getattr = nfs_getattr,
183 .vop_inactive = nfs_inactive,
184 .vop_islocked = vop_stdislocked,
185 .vop_lock = vop_stdlock,
186 .vop_print = nfs_print,
187 .vop_read = nfsspec_read,
188 .vop_reclaim = nfs_reclaim,
189 .vop_setattr = nfs_setattr,
190 .vop_unlock = vop_stdunlock,
191 .vop_write = nfsspec_write
194 struct vop_ops nfsv2_fifo_vops = {
195 .vop_default = fifo_vnoperate,
196 .vop_access = nfsspec_access,
197 .vop_close = nfsfifo_close,
198 .vop_fsync = nfs_fsync,
199 .vop_getattr = nfs_getattr,
200 .vop_inactive = nfs_inactive,
201 .vop_islocked = vop_stdislocked,
202 .vop_lock = vop_stdlock,
203 .vop_print = nfs_print,
204 .vop_read = nfsfifo_read,
205 .vop_reclaim = nfs_reclaim,
206 .vop_setattr = nfs_setattr,
207 .vop_unlock = vop_stdunlock,
208 .vop_write = nfsfifo_write
211 static int nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
212 struct componentname *cnp,
214 static int nfs_removerpc (struct vnode *dvp, const char *name,
216 struct ucred *cred, struct thread *td);
217 static int nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
218 int fnamelen, struct vnode *tdvp,
219 const char *tnameptr, int tnamelen,
220 struct ucred *cred, struct thread *td);
221 static int nfs_renameit (struct vnode *sdvp,
222 struct componentname *scnp,
223 struct sillyrename *sp);
228 extern u_int32_t nfs_true, nfs_false;
229 extern u_int32_t nfs_xdrneg1;
230 extern struct nfsstats nfsstats;
231 extern nfstype nfsv3_type[9];
232 struct thread *nfs_iodwant[NFS_MAXASYNCDAEMON];
233 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
234 int nfs_numasync = 0;
236 SYSCTL_DECL(_vfs_nfs);
238 static int nfsaccess_cache_timeout = NFS_DEFATTRTIMO;
239 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
240 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
242 static int nfsneg_cache_timeout = NFS_MINATTRTIMO;
243 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
244 &nfsneg_cache_timeout, 0, "NFS NEGATIVE ACCESS cache timeout");
246 static int nfsv3_commit_on_close = 0;
247 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
248 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
250 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
251 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
253 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
254 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
257 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
258 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
259 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
261 nfs3_access_otw(struct vnode *vp, int wmode,
262 struct thread *td, struct ucred *cred)
266 int error = 0, attrflag;
268 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
269 caddr_t bpos, dpos, cp2;
273 struct nfsnode *np = VTONFS(vp);
275 nfsstats.rpccnt[NFSPROC_ACCESS]++;
276 nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
278 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
279 *tl = txdr_unsigned(wmode);
280 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
281 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
283 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
284 rmode = fxdr_unsigned(u_int32_t, *tl);
286 np->n_modeuid = cred->cr_uid;
287 np->n_modestamp = mycpu->gd_time_seconds;
295 * nfs access vnode op.
296 * For nfs version 2, just return ok. File accesses may fail later.
297 * For nfs version 3, use the access rpc to check accessibility. If file modes
298 * are changed on the server, accesses might still fail later.
300 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
301 * struct thread *a_td)
304 nfs_access(struct vop_access_args *ap)
306 struct vnode *vp = ap->a_vp;
307 thread_t td = curthread;
309 u_int32_t mode, wmode;
310 int v3 = NFS_ISV3(vp);
311 struct nfsnode *np = VTONFS(vp);
314 * Disallow write attempts on filesystems mounted read-only;
315 * unless the file is a socket, fifo, or a block or character
316 * device resident on the filesystem.
318 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
319 switch (vp->v_type) {
329 * For nfs v3, check to see if we have done this recently, and if
330 * so return our cached result instead of making an ACCESS call.
331 * If not, do an access rpc, otherwise you are stuck emulating
332 * ufs_access() locally using the vattr. This may not be correct,
333 * since the server may apply other access criteria such as
334 * client uid-->server uid mapping that we do not know about.
337 if (ap->a_mode & VREAD)
338 mode = NFSV3ACCESS_READ;
341 if (vp->v_type != VDIR) {
342 if (ap->a_mode & VWRITE)
343 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
344 if (ap->a_mode & VEXEC)
345 mode |= NFSV3ACCESS_EXECUTE;
347 if (ap->a_mode & VWRITE)
348 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
350 if (ap->a_mode & VEXEC)
351 mode |= NFSV3ACCESS_LOOKUP;
353 /* XXX safety belt, only make blanket request if caching */
354 if (nfsaccess_cache_timeout > 0) {
355 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
356 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
357 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
363 * Does our cached result allow us to give a definite yes to
366 if (np->n_modestamp &&
367 (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
368 (ap->a_cred->cr_uid == np->n_modeuid) &&
369 ((np->n_mode & mode) == mode)) {
370 nfsstats.accesscache_hits++;
373 * Either a no, or a don't know. Go to the wire.
375 nfsstats.accesscache_misses++;
376 error = nfs3_access_otw(vp, wmode, td, ap->a_cred);
378 if ((np->n_mode & mode) != mode) {
384 if ((error = nfsspec_access(ap)) != 0)
388 * Attempt to prevent a mapped root from accessing a file
389 * which it shouldn't. We try to read a byte from the file
390 * if the user is root and the file is not zero length.
391 * After calling nfsspec_access, we should have the correct
394 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
395 && VTONFS(vp)->n_size > 0) {
402 auio.uio_iov = &aiov;
406 auio.uio_segflg = UIO_SYSSPACE;
407 auio.uio_rw = UIO_READ;
410 if (vp->v_type == VREG) {
411 error = nfs_readrpc(vp, &auio);
412 } else if (vp->v_type == VDIR) {
414 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
416 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
417 error = nfs_readdirrpc(vp, &auio);
419 } else if (vp->v_type == VLNK) {
420 error = nfs_readlinkrpc(vp, &auio);
427 * [re]record creds for reading and/or writing if access
428 * was granted. Assume the NFS server will grant read access
429 * for execute requests.
432 if ((ap->a_mode & (VREAD|VEXEC)) && ap->a_cred != np->n_rucred) {
435 crfree(np->n_rucred);
436 np->n_rucred = ap->a_cred;
438 if ((ap->a_mode & VWRITE) && ap->a_cred != np->n_wucred) {
441 crfree(np->n_wucred);
442 np->n_wucred = ap->a_cred;
450 * Check to see if the type is ok
451 * and that deletion is not in progress.
452 * For paged in text files, you will need to flush the page cache
453 * if consistency is lost.
455 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
459 nfs_open(struct vop_open_args *ap)
461 struct vnode *vp = ap->a_vp;
462 struct nfsnode *np = VTONFS(vp);
466 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
468 printf("open eacces vtyp=%d\n",vp->v_type);
474 * Clear the attribute cache only if opening with write access. It
475 * is unclear if we should do this at all here, but we certainly
476 * should not clear the cache unconditionally simply because a file
479 if (ap->a_mode & FWRITE)
483 * For normal NFS, reconcile changes made locally verses
484 * changes made remotely. Note that VOP_GETATTR only goes
485 * to the wire if the cached attribute has timed out or been
488 * If local modifications have been made clear the attribute
489 * cache to force an attribute and modified time check. If
490 * GETATTR detects that the file has been changed by someone
491 * other then us it will set NRMODIFIED.
493 * If we are opening a directory and local changes have been
494 * made we have to invalidate the cache in order to ensure
495 * that we get the most up-to-date information from the
498 if (np->n_flag & NLMODIFIED) {
500 if (vp->v_type == VDIR) {
501 error = nfs_vinvalbuf(vp, V_SAVE, 1);
507 error = VOP_GETATTR(vp, &vattr);
510 if (np->n_flag & NRMODIFIED) {
511 if (vp->v_type == VDIR)
513 error = nfs_vinvalbuf(vp, V_SAVE, 1);
516 np->n_flag &= ~NRMODIFIED;
519 return (vop_stdopen(ap));
524 * What an NFS client should do upon close after writing is a debatable issue.
525 * Most NFS clients push delayed writes to the server upon close, basically for
527 * 1 - So that any write errors may be reported back to the client process
528 * doing the close system call. By far the two most likely errors are
529 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
530 * 2 - To put a worst case upper bound on cache inconsistency between
531 * multiple clients for the file.
532 * There is also a consistency problem for Version 2 of the protocol w.r.t.
533 * not being able to tell if other clients are writing a file concurrently,
534 * since there is no way of knowing if the changed modify time in the reply
535 * is only due to the write for this client.
536 * (NFS Version 3 provides weak cache consistency data in the reply that
537 * should be sufficient to detect and handle this case.)
539 * The current code does the following:
540 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
541 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
542 * or commit them (this satisfies 1 and 2 except for the
543 * case where the server crashes after this close but
544 * before the commit RPC, which is felt to be "good
545 * enough". Changing the last argument to nfs_flush() to
546 * a 1 would force a commit operation, if it is felt a
547 * commit is necessary now.
548 * for NQNFS - do nothing now, since 2 is dealt with via leases and
549 * 1 should be dealt with via an fsync() system call for
550 * cases where write errors are important.
552 * nfs_close(struct vnodeop_desc *a_desc, struct vnode *a_vp, int a_fflag,
553 * struct ucred *a_cred, struct thread *a_td)
557 nfs_close(struct vop_close_args *ap)
559 struct vnode *vp = ap->a_vp;
560 struct nfsnode *np = VTONFS(vp);
562 thread_t td = curthread;
564 if (vp->v_type == VREG) {
565 if (np->n_flag & NLMODIFIED) {
568 * Under NFSv3 we have dirty buffers to dispose of. We
569 * must flush them to the NFS server. We have the option
570 * of waiting all the way through the commit rpc or just
571 * waiting for the initial write. The default is to only
572 * wait through the initial write so the data is in the
573 * server's cache, which is roughly similar to the state
574 * a standard disk subsystem leaves the file in on close().
576 * We cannot clear the NLMODIFIED bit in np->n_flag due to
577 * potential races with other processes, and certainly
578 * cannot clear it if we don't commit.
580 int cm = nfsv3_commit_on_close ? 1 : 0;
581 error = nfs_flush(vp, MNT_WAIT, td, cm);
582 /* np->n_flag &= ~NLMODIFIED; */
584 error = nfs_vinvalbuf(vp, V_SAVE, 1);
588 if (np->n_flag & NWRITEERR) {
589 np->n_flag &= ~NWRITEERR;
598 * nfs getattr call from vfs.
600 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred,
601 * struct thread *a_td)
604 nfs_getattr(struct vop_getattr_args *ap)
606 struct vnode *vp = ap->a_vp;
607 struct nfsnode *np = VTONFS(vp);
613 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
614 int v3 = NFS_ISV3(vp);
615 thread_t td = curthread;
618 * Update local times for special files.
620 if (np->n_flag & (NACC | NUPD))
623 * First look in the cache.
625 if (nfs_getattrcache(vp, ap->a_vap) == 0)
628 if (v3 && nfsaccess_cache_timeout > 0) {
629 nfsstats.accesscache_misses++;
630 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, nfs_vpcred(vp, ND_CHECK));
631 if (nfs_getattrcache(vp, ap->a_vap) == 0)
635 nfsstats.rpccnt[NFSPROC_GETATTR]++;
636 nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
638 nfsm_request(vp, NFSPROC_GETATTR, td, nfs_vpcred(vp, ND_CHECK));
640 nfsm_loadattr(vp, ap->a_vap);
650 * nfs_setattr(struct vnodeop_desc *a_desc, struct vnode *a_vp,
651 * struct vattr *a_vap, struct ucred *a_cred)
654 nfs_setattr(struct vop_setattr_args *ap)
656 struct vnode *vp = ap->a_vp;
657 struct nfsnode *np = VTONFS(vp);
658 struct vattr *vap = ap->a_vap;
661 thread_t td = curthread;
668 * Setting of flags is not supported.
670 if (vap->va_flags != VNOVAL)
674 * Disallow write attempts if the filesystem is mounted read-only.
676 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
677 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
678 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
679 (vp->v_mount->mnt_flag & MNT_RDONLY))
681 if (vap->va_size != VNOVAL) {
682 switch (vp->v_type) {
689 if (vap->va_mtime.tv_sec == VNOVAL &&
690 vap->va_atime.tv_sec == VNOVAL &&
691 vap->va_mode == (mode_t)VNOVAL &&
692 vap->va_uid == (uid_t)VNOVAL &&
693 vap->va_gid == (gid_t)VNOVAL)
695 vap->va_size = VNOVAL;
699 * Disallow write attempts if the filesystem is
702 if (vp->v_mount->mnt_flag & MNT_RDONLY)
706 * This is nasty. The RPCs we send to flush pending
707 * data often return attribute information which is
708 * cached via a callback to nfs_loadattrcache(), which
709 * has the effect of changing our notion of the file
710 * size. Due to flushed appends and other operations
711 * the file size can be set to virtually anything,
712 * including values that do not match either the old
713 * or intended file size.
715 * When this condition is detected we must loop to
716 * try the operation again. Hopefully no more
717 * flushing is required on the loop so it works the
718 * second time around. THIS CASE ALMOST ALWAYS
723 error = nfs_meta_setsize(vp, td, vap->va_size);
725 if (np->n_flag & NLMODIFIED) {
726 if (vap->va_size == 0)
727 error = nfs_vinvalbuf(vp, 0, 1);
729 error = nfs_vinvalbuf(vp, V_SAVE, 1);
732 * note: this loop case almost always happens at
733 * least once per truncation.
735 if (error == 0 && np->n_size != vap->va_size)
737 np->n_vattr.va_size = vap->va_size;
740 } else if ((vap->va_mtime.tv_sec != VNOVAL ||
741 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NLMODIFIED) &&
742 vp->v_type == VREG &&
743 (error = nfs_vinvalbuf(vp, V_SAVE, 1)) == EINTR
747 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
750 * Sanity check if a truncation was issued. This should only occur
751 * if multiple processes are racing on the same file.
753 if (error == 0 && vap->va_size != VNOVAL &&
754 np->n_size != vap->va_size) {
755 printf("NFS ftruncate: server disagrees on the file size: %lld/%lld/%lld\n", tsize, vap->va_size, np->n_size);
758 if (error && vap->va_size != VNOVAL) {
759 np->n_size = np->n_vattr.va_size = tsize;
760 vnode_pager_setsize(vp, np->n_size);
766 * Do an nfs setattr rpc.
769 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
770 struct ucred *cred, struct thread *td)
772 struct nfsv2_sattr *sp;
773 struct nfsnode *np = VTONFS(vp);
776 caddr_t bpos, dpos, cp2;
778 int error = 0, wccflag = NFSV3_WCCRATTR;
779 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
780 int v3 = NFS_ISV3(vp);
782 nfsstats.rpccnt[NFSPROC_SETATTR]++;
783 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
786 nfsm_v3attrbuild(vap, TRUE);
787 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
790 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
791 if (vap->va_mode == (mode_t)VNOVAL)
792 sp->sa_mode = nfs_xdrneg1;
794 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
795 if (vap->va_uid == (uid_t)VNOVAL)
796 sp->sa_uid = nfs_xdrneg1;
798 sp->sa_uid = txdr_unsigned(vap->va_uid);
799 if (vap->va_gid == (gid_t)VNOVAL)
800 sp->sa_gid = nfs_xdrneg1;
802 sp->sa_gid = txdr_unsigned(vap->va_gid);
803 sp->sa_size = txdr_unsigned(vap->va_size);
804 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
805 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
807 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
810 nfsm_wcc_data(vp, wccflag);
812 nfsm_loadattr(vp, (struct vattr *)0);
819 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
820 * nfs_lookup() until all remaining new api calls are implemented.
822 * Resolve a namecache entry. This function is passed a locked ncp and
823 * must call cache_setvp() on it as appropriate to resolve the entry.
826 nfs_nresolve(struct vop_nresolve_args *ap)
828 struct thread *td = curthread;
829 struct namecache *ncp;
840 /******NFSM MACROS********/
841 struct mbuf *mb, *mrep, *mreq, *mb2, *md;
842 caddr_t bpos, dpos, cp, cp2;
849 KKASSERT(ncp->nc_parent && ncp->nc_parent->nc_vp);
850 dvp = ncp->nc_parent->nc_vp;
851 if ((error = vget(dvp, LK_SHARED)) != 0)
856 nfsstats.lookupcache_misses++;
857 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
859 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
860 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
862 nfsm_strtom(ncp->nc_name, len, NFS_MAXNAMLEN);
863 nfsm_request(dvp, NFSPROC_LOOKUP, td, ap->a_cred);
866 * Cache negatve lookups to reduce NFS traffic, but use
867 * a fast timeout. Otherwise use a timeout of 1 tick.
868 * XXX we should add a namecache flag for no-caching
869 * to uncache the negative hit as soon as possible, but
870 * we cannot simply destroy the entry because it is used
871 * as a placeholder by the caller.
873 if (error == ENOENT) {
876 if (nfsneg_cache_timeout)
877 nticks = nfsneg_cache_timeout * hz;
880 cache_setvp(ncp, NULL);
881 cache_settimeout(ncp, nticks);
883 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
889 * Success, get the file handle, do various checks, and load
890 * post-operation data from the reply packet. Theoretically
891 * we should never be looking up "." so, theoretically, we
892 * should never get the same file handle as our directory. But
893 * we check anyway. XXX
895 * Note that no timeout is set for the positive cache hit. We
896 * assume, theoretically, that ESTALE returns will be dealt with
897 * properly to handle NFS races and in anycase we cannot depend
898 * on a timeout to deal with NFS open/create/excl issues so instead
899 * of a bad hack here the rest of the NFS client code needs to do
902 nfsm_getfh(fhp, fhsize, v3);
905 if (NFS_CMPFH(np, fhp, fhsize)) {
909 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
918 nfsm_postop_attr(nvp, attrflag, NFS_LATTR_NOSHRINK);
919 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
921 nfsm_loadattr(nvp, NULL);
923 cache_setvp(ncp, nvp);
937 * 'cached' nfs directory lookup
939 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
941 * nfs_lookup(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
942 * struct vnode **a_vpp, struct componentname *a_cnp)
945 nfs_lookup(struct vop_old_lookup_args *ap)
947 struct componentname *cnp = ap->a_cnp;
948 struct vnode *dvp = ap->a_dvp;
949 struct vnode **vpp = ap->a_vpp;
950 int flags = cnp->cn_flags;
955 struct nfsmount *nmp;
956 caddr_t bpos, dpos, cp2;
957 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
961 int lockparent, wantparent, error = 0, attrflag, fhsize;
962 int v3 = NFS_ISV3(dvp);
965 * Read-only mount check and directory check.
968 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
969 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
972 if (dvp->v_type != VDIR)
976 * Look it up in the cache. Note that ENOENT is only returned if we
977 * previously entered a negative hit (see later on). The additional
978 * nfsneg_cache_timeout check causes previously cached results to
979 * be instantly ignored if the negative caching is turned off.
981 lockparent = flags & CNP_LOCKPARENT;
982 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
983 nmp = VFSTONFS(dvp->v_mount);
991 nfsstats.lookupcache_misses++;
992 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
993 len = cnp->cn_namelen;
994 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
995 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
997 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
998 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
1000 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1004 nfsm_getfh(fhp, fhsize, v3);
1007 * Handle RENAME case...
1009 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1010 if (NFS_CMPFH(np, fhp, fhsize)) {
1014 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1021 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1022 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1024 nfsm_loadattr(newvp, (struct vattr *)0);
1029 cnp->cn_flags |= CNP_PDIRUNLOCK;
1034 if (flags & CNP_ISDOTDOT) {
1036 cnp->cn_flags |= CNP_PDIRUNLOCK;
1037 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1039 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1040 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1041 return (error); /* NOTE: return error from nget */
1045 error = vn_lock(dvp, LK_EXCLUSIVE);
1050 cnp->cn_flags |= CNP_PDIRUNLOCK;
1052 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1056 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1063 cnp->cn_flags |= CNP_PDIRUNLOCK;
1068 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1069 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1071 nfsm_loadattr(newvp, (struct vattr *)0);
1073 /* XXX MOVE TO nfs_nremove() */
1074 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1075 cnp->cn_nameiop != NAMEI_DELETE) {
1076 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1083 if (newvp != NULLVP) {
1087 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1088 cnp->cn_nameiop == NAMEI_RENAME) &&
1092 cnp->cn_flags |= CNP_PDIRUNLOCK;
1094 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1097 error = EJUSTRETURN;
1105 * Just call nfs_bioread() to do the work.
1107 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1108 * struct ucred *a_cred)
1111 nfs_read(struct vop_read_args *ap)
1113 struct vnode *vp = ap->a_vp;
1115 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1116 switch (vp->v_type) {
1118 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1129 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1132 nfs_readlink(struct vop_readlink_args *ap)
1134 struct vnode *vp = ap->a_vp;
1136 if (vp->v_type != VLNK)
1138 return (nfs_bioread(vp, ap->a_uio, 0));
1142 * Do a readlink rpc.
1143 * Called by nfs_doio() from below the buffer cache.
1146 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1151 caddr_t bpos, dpos, cp2;
1152 int error = 0, len, attrflag;
1153 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1154 int v3 = NFS_ISV3(vp);
1156 nfsstats.rpccnt[NFSPROC_READLINK]++;
1157 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1159 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1161 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1163 nfsm_strsiz(len, NFS_MAXPATHLEN);
1164 if (len == NFS_MAXPATHLEN) {
1165 struct nfsnode *np = VTONFS(vp);
1166 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1169 nfsm_mtouio(uiop, len);
1181 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1186 caddr_t bpos, dpos, cp2;
1187 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1188 struct nfsmount *nmp;
1189 int error = 0, len, retlen, tsiz, eof, attrflag;
1190 int v3 = NFS_ISV3(vp);
1195 nmp = VFSTONFS(vp->v_mount);
1196 tsiz = uiop->uio_resid;
1197 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1200 nfsstats.rpccnt[NFSPROC_READ]++;
1201 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1202 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1204 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1206 txdr_hyper(uiop->uio_offset, tl);
1207 *(tl + 2) = txdr_unsigned(len);
1209 *tl++ = txdr_unsigned(uiop->uio_offset);
1210 *tl++ = txdr_unsigned(len);
1213 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1215 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1220 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1221 eof = fxdr_unsigned(int, *(tl + 1));
1223 nfsm_loadattr(vp, (struct vattr *)0);
1224 nfsm_strsiz(retlen, nmp->nm_rsize);
1225 nfsm_mtouio(uiop, retlen);
1229 if (eof || retlen == 0) {
1232 } else if (retlen < len) {
1244 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, int *must_commit)
1248 int32_t t1, t2, backup;
1249 caddr_t bpos, dpos, cp2;
1250 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1251 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1252 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1253 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1256 if (uiop->uio_iovcnt != 1)
1257 panic("nfs: writerpc iovcnt > 1");
1260 tsiz = uiop->uio_resid;
1261 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1264 nfsstats.rpccnt[NFSPROC_WRITE]++;
1265 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1266 nfsm_reqhead(vp, NFSPROC_WRITE,
1267 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1270 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1271 txdr_hyper(uiop->uio_offset, tl);
1273 *tl++ = txdr_unsigned(len);
1274 *tl++ = txdr_unsigned(*iomode);
1275 *tl = txdr_unsigned(len);
1279 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1280 /* Set both "begin" and "current" to non-garbage. */
1281 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1282 *tl++ = x; /* "begin offset" */
1283 *tl++ = x; /* "current offset" */
1284 x = txdr_unsigned(len);
1285 *tl++ = x; /* total to this offset */
1286 *tl = x; /* size of this write */
1288 nfsm_uiotom(uiop, len);
1289 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1292 * The write RPC returns a before and after mtime. The
1293 * nfsm_wcc_data() macro checks the before n_mtime
1294 * against the before time and stores the after time
1295 * in the nfsnode's cached vattr and n_mtime field.
1296 * The NRMODIFIED bit will be set if the before
1297 * time did not match the original mtime.
1299 wccflag = NFSV3_WCCCHK;
1300 nfsm_wcc_data(vp, wccflag);
1302 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1303 + NFSX_V3WRITEVERF);
1304 rlen = fxdr_unsigned(int, *tl++);
1309 } else if (rlen < len) {
1310 backup = len - rlen;
1311 uiop->uio_iov->iov_base -= backup;
1312 uiop->uio_iov->iov_len += backup;
1313 uiop->uio_offset -= backup;
1314 uiop->uio_resid += backup;
1317 commit = fxdr_unsigned(int, *tl++);
1320 * Return the lowest committment level
1321 * obtained by any of the RPCs.
1323 if (committed == NFSV3WRITE_FILESYNC)
1325 else if (committed == NFSV3WRITE_DATASYNC &&
1326 commit == NFSV3WRITE_UNSTABLE)
1328 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1329 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1331 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1332 } else if (bcmp((caddr_t)tl,
1333 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1335 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1340 nfsm_loadattr(vp, (struct vattr *)0);
1348 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1349 committed = NFSV3WRITE_FILESYNC;
1350 *iomode = committed;
1352 uiop->uio_resid = tsiz;
1358 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1359 * mode set to specify the file type and the size field for rdev.
1362 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1365 struct nfsv2_sattr *sp;
1369 struct vnode *newvp = (struct vnode *)0;
1370 struct nfsnode *np = (struct nfsnode *)0;
1374 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1375 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1377 int v3 = NFS_ISV3(dvp);
1379 if (vap->va_type == VCHR || vap->va_type == VBLK)
1380 rdev = txdr_unsigned(vap->va_rdev);
1381 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1384 return (EOPNOTSUPP);
1386 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1389 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1390 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1391 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1392 nfsm_fhtom(dvp, v3);
1393 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1395 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1396 *tl++ = vtonfsv3_type(vap->va_type);
1397 nfsm_v3attrbuild(vap, FALSE);
1398 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1399 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1400 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1401 *tl = txdr_unsigned(uminor(vap->va_rdev));
1404 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1405 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1406 sp->sa_uid = nfs_xdrneg1;
1407 sp->sa_gid = nfs_xdrneg1;
1409 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1410 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1412 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1414 nfsm_mtofh(dvp, newvp, v3, gotvp);
1418 newvp = (struct vnode *)0;
1420 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1421 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1427 nfsm_wcc_data(dvp, wccflag);
1436 VTONFS(dvp)->n_flag |= NLMODIFIED;
1438 VTONFS(dvp)->n_attrstamp = 0;
1444 * just call nfs_mknodrpc() to do the work.
1446 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1447 * struct componentname *a_cnp, struct vattr *a_vap)
1451 nfs_mknod(struct vop_old_mknod_args *ap)
1453 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1456 static u_long create_verf;
1458 * nfs file create call
1460 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1461 * struct componentname *a_cnp, struct vattr *a_vap)
1464 nfs_create(struct vop_old_create_args *ap)
1466 struct vnode *dvp = ap->a_dvp;
1467 struct vattr *vap = ap->a_vap;
1468 struct componentname *cnp = ap->a_cnp;
1469 struct nfsv2_sattr *sp;
1473 struct nfsnode *np = (struct nfsnode *)0;
1474 struct vnode *newvp = (struct vnode *)0;
1475 caddr_t bpos, dpos, cp2;
1476 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1477 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1479 int v3 = NFS_ISV3(dvp);
1482 * Oops, not for me..
1484 if (vap->va_type == VSOCK)
1485 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1487 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1490 if (vap->va_vaflags & VA_EXCLUSIVE)
1493 nfsstats.rpccnt[NFSPROC_CREATE]++;
1494 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1495 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1496 nfsm_fhtom(dvp, v3);
1497 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1499 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1500 if (fmode & O_EXCL) {
1501 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1502 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1504 if (!TAILQ_EMPTY(&in_ifaddrhead))
1505 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr;
1508 *tl++ = create_verf;
1509 *tl = ++create_verf;
1511 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1512 nfsm_v3attrbuild(vap, FALSE);
1515 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1516 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1517 sp->sa_uid = nfs_xdrneg1;
1518 sp->sa_gid = nfs_xdrneg1;
1520 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1521 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1523 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1525 nfsm_mtofh(dvp, newvp, v3, gotvp);
1529 newvp = (struct vnode *)0;
1531 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1532 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1538 nfsm_wcc_data(dvp, wccflag);
1542 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1548 } else if (v3 && (fmode & O_EXCL)) {
1550 * We are normally called with only a partially initialized
1551 * VAP. Since the NFSv3 spec says that server may use the
1552 * file attributes to store the verifier, the spec requires
1553 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1554 * in atime, but we can't really assume that all servers will
1555 * so we ensure that our SETATTR sets both atime and mtime.
1557 if (vap->va_mtime.tv_sec == VNOVAL)
1558 vfs_timestamp(&vap->va_mtime);
1559 if (vap->va_atime.tv_sec == VNOVAL)
1560 vap->va_atime = vap->va_mtime;
1561 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1565 * The new np may have enough info for access
1566 * checks, make sure rucred and wucred are
1567 * initialized for read and write rpc's.
1570 if (np->n_rucred == NULL)
1571 np->n_rucred = crhold(cnp->cn_cred);
1572 if (np->n_wucred == NULL)
1573 np->n_wucred = crhold(cnp->cn_cred);
1576 VTONFS(dvp)->n_flag |= NLMODIFIED;
1578 VTONFS(dvp)->n_attrstamp = 0;
1583 * nfs file remove call
1584 * To try and make nfs semantics closer to ufs semantics, a file that has
1585 * other processes using the vnode is renamed instead of removed and then
1586 * removed later on the last close.
1587 * - If v_usecount > 1
1588 * If a rename is not already in the works
1589 * call nfs_sillyrename() to set it up
1593 * nfs_remove(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
1594 * struct vnode *a_vp, struct componentname *a_cnp)
1597 nfs_remove(struct vop_old_remove_args *ap)
1599 struct vnode *vp = ap->a_vp;
1600 struct vnode *dvp = ap->a_dvp;
1601 struct componentname *cnp = ap->a_cnp;
1602 struct nfsnode *np = VTONFS(vp);
1607 if (vp->v_usecount < 1)
1608 panic("nfs_remove: bad v_usecount");
1610 if (vp->v_type == VDIR)
1612 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1613 VOP_GETATTR(vp, &vattr) == 0 &&
1614 vattr.va_nlink > 1)) {
1616 * throw away biocache buffers, mainly to avoid
1617 * unnecessary delayed writes later.
1619 error = nfs_vinvalbuf(vp, 0, 1);
1622 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1623 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1625 * Kludge City: If the first reply to the remove rpc is lost..
1626 * the reply to the retransmitted request will be ENOENT
1627 * since the file was in fact removed
1628 * Therefore, we cheat and return success.
1630 if (error == ENOENT)
1632 } else if (!np->n_sillyrename) {
1633 error = nfs_sillyrename(dvp, vp, cnp);
1635 np->n_attrstamp = 0;
1640 * nfs file remove rpc called from nfs_inactive
1643 nfs_removeit(struct sillyrename *sp)
1645 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1650 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1653 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1654 struct ucred *cred, struct thread *td)
1659 caddr_t bpos, dpos, cp2;
1660 int error = 0, wccflag = NFSV3_WCCRATTR;
1661 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1662 int v3 = NFS_ISV3(dvp);
1664 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1665 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1666 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1667 nfsm_fhtom(dvp, v3);
1668 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1669 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1671 nfsm_wcc_data(dvp, wccflag);
1674 VTONFS(dvp)->n_flag |= NLMODIFIED;
1676 VTONFS(dvp)->n_attrstamp = 0;
1681 * nfs file rename call
1683 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1684 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1685 * struct vnode *a_tvp, struct componentname *a_tcnp)
1688 nfs_rename(struct vop_old_rename_args *ap)
1690 struct vnode *fvp = ap->a_fvp;
1691 struct vnode *tvp = ap->a_tvp;
1692 struct vnode *fdvp = ap->a_fdvp;
1693 struct vnode *tdvp = ap->a_tdvp;
1694 struct componentname *tcnp = ap->a_tcnp;
1695 struct componentname *fcnp = ap->a_fcnp;
1698 /* Check for cross-device rename */
1699 if ((fvp->v_mount != tdvp->v_mount) ||
1700 (tvp && (fvp->v_mount != tvp->v_mount))) {
1706 * We have to flush B_DELWRI data prior to renaming
1707 * the file. If we don't, the delayed-write buffers
1708 * can be flushed out later after the file has gone stale
1709 * under NFSV3. NFSV2 does not have this problem because
1710 * ( as far as I can tell ) it flushes dirty buffers more
1714 VOP_FSYNC(fvp, MNT_WAIT);
1716 VOP_FSYNC(tvp, MNT_WAIT);
1719 * If the tvp exists and is in use, sillyrename it before doing the
1720 * rename of the new file over it.
1722 * XXX Can't sillyrename a directory.
1724 * We do not attempt to do any namecache purges in this old API
1725 * routine. The new API compat functions have access to the actual
1726 * namecache structures and will do it for us.
1728 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1729 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1736 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1737 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1750 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1752 if (error == ENOENT)
1758 * nfs file rename rpc called from nfs_remove() above
1761 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1762 struct sillyrename *sp)
1764 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1765 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1769 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1772 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1773 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1774 struct ucred *cred, struct thread *td)
1779 caddr_t bpos, dpos, cp2;
1780 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1781 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1782 int v3 = NFS_ISV3(fdvp);
1784 nfsstats.rpccnt[NFSPROC_RENAME]++;
1785 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1786 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1787 nfsm_rndup(tnamelen));
1788 nfsm_fhtom(fdvp, v3);
1789 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1790 nfsm_fhtom(tdvp, v3);
1791 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1792 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1794 nfsm_wcc_data(fdvp, fwccflag);
1795 nfsm_wcc_data(tdvp, twccflag);
1799 VTONFS(fdvp)->n_flag |= NLMODIFIED;
1800 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1802 VTONFS(fdvp)->n_attrstamp = 0;
1804 VTONFS(tdvp)->n_attrstamp = 0;
1809 * nfs hard link create call
1811 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1812 * struct componentname *a_cnp)
1815 nfs_link(struct vop_old_link_args *ap)
1817 struct vnode *vp = ap->a_vp;
1818 struct vnode *tdvp = ap->a_tdvp;
1819 struct componentname *cnp = ap->a_cnp;
1823 caddr_t bpos, dpos, cp2;
1824 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1825 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1828 if (vp->v_mount != tdvp->v_mount) {
1833 * Push all writes to the server, so that the attribute cache
1834 * doesn't get "out of sync" with the server.
1835 * XXX There should be a better way!
1837 VOP_FSYNC(vp, MNT_WAIT);
1840 nfsstats.rpccnt[NFSPROC_LINK]++;
1841 nfsm_reqhead(vp, NFSPROC_LINK,
1842 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1844 nfsm_fhtom(tdvp, v3);
1845 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1846 nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1848 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1849 nfsm_wcc_data(tdvp, wccflag);
1853 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1855 VTONFS(vp)->n_attrstamp = 0;
1857 VTONFS(tdvp)->n_attrstamp = 0;
1859 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1861 if (error == EEXIST)
1867 * nfs symbolic link create call
1869 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1870 * struct componentname *a_cnp, struct vattr *a_vap,
1874 nfs_symlink(struct vop_old_symlink_args *ap)
1876 struct vnode *dvp = ap->a_dvp;
1877 struct vattr *vap = ap->a_vap;
1878 struct componentname *cnp = ap->a_cnp;
1879 struct nfsv2_sattr *sp;
1883 caddr_t bpos, dpos, cp2;
1884 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1885 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1886 struct vnode *newvp = (struct vnode *)0;
1887 int v3 = NFS_ISV3(dvp);
1889 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1890 slen = strlen(ap->a_target);
1891 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1892 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1893 nfsm_fhtom(dvp, v3);
1894 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1896 nfsm_v3attrbuild(vap, FALSE);
1898 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1900 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1901 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1902 sp->sa_uid = nfs_xdrneg1;
1903 sp->sa_gid = nfs_xdrneg1;
1904 sp->sa_size = nfs_xdrneg1;
1905 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1906 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1910 * Issue the NFS request and get the rpc response.
1912 * Only NFSv3 responses returning an error of 0 actually return
1913 * a file handle that can be converted into newvp without having
1914 * to do an extra lookup rpc.
1916 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1919 nfsm_mtofh(dvp, newvp, v3, gotvp);
1920 nfsm_wcc_data(dvp, wccflag);
1924 * out code jumps -> here, mrep is also freed.
1931 * If we get an EEXIST error, silently convert it to no-error
1932 * in case of an NFS retry.
1934 if (error == EEXIST)
1938 * If we do not have (or no longer have) an error, and we could
1939 * not extract the newvp from the response due to the request being
1940 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1941 * to obtain a newvp to return.
1943 if (error == 0 && newvp == NULL) {
1944 struct nfsnode *np = NULL;
1946 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1947 cnp->cn_cred, cnp->cn_td, &np);
1957 VTONFS(dvp)->n_flag |= NLMODIFIED;
1959 VTONFS(dvp)->n_attrstamp = 0;
1966 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1967 * struct componentname *a_cnp, struct vattr *a_vap)
1970 nfs_mkdir(struct vop_old_mkdir_args *ap)
1972 struct vnode *dvp = ap->a_dvp;
1973 struct vattr *vap = ap->a_vap;
1974 struct componentname *cnp = ap->a_cnp;
1975 struct nfsv2_sattr *sp;
1980 struct nfsnode *np = (struct nfsnode *)0;
1981 struct vnode *newvp = (struct vnode *)0;
1982 caddr_t bpos, dpos, cp2;
1983 int error = 0, wccflag = NFSV3_WCCRATTR;
1985 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1987 int v3 = NFS_ISV3(dvp);
1989 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1992 len = cnp->cn_namelen;
1993 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1994 nfsm_reqhead(dvp, NFSPROC_MKDIR,
1995 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1996 nfsm_fhtom(dvp, v3);
1997 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1999 nfsm_v3attrbuild(vap, FALSE);
2001 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2002 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2003 sp->sa_uid = nfs_xdrneg1;
2004 sp->sa_gid = nfs_xdrneg1;
2005 sp->sa_size = nfs_xdrneg1;
2006 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2007 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2009 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
2011 nfsm_mtofh(dvp, newvp, v3, gotvp);
2013 nfsm_wcc_data(dvp, wccflag);
2016 VTONFS(dvp)->n_flag |= NLMODIFIED;
2018 VTONFS(dvp)->n_attrstamp = 0;
2020 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2021 * if we can succeed in looking up the directory.
2023 if (error == EEXIST || (!error && !gotvp)) {
2026 newvp = (struct vnode *)0;
2028 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2032 if (newvp->v_type != VDIR)
2045 * nfs remove directory call
2047 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2048 * struct componentname *a_cnp)
2051 nfs_rmdir(struct vop_old_rmdir_args *ap)
2053 struct vnode *vp = ap->a_vp;
2054 struct vnode *dvp = ap->a_dvp;
2055 struct componentname *cnp = ap->a_cnp;
2059 caddr_t bpos, dpos, cp2;
2060 int error = 0, wccflag = NFSV3_WCCRATTR;
2061 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2062 int v3 = NFS_ISV3(dvp);
2066 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2067 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2068 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2069 nfsm_fhtom(dvp, v3);
2070 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2071 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2073 nfsm_wcc_data(dvp, wccflag);
2076 VTONFS(dvp)->n_flag |= NLMODIFIED;
2078 VTONFS(dvp)->n_attrstamp = 0;
2080 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2082 if (error == ENOENT)
2090 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2093 nfs_readdir(struct vop_readdir_args *ap)
2095 struct vnode *vp = ap->a_vp;
2096 struct nfsnode *np = VTONFS(vp);
2097 struct uio *uio = ap->a_uio;
2101 if (vp->v_type != VDIR)
2105 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2106 * and then check that is still valid, or if this is an NQNFS mount
2107 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2108 * VOP_GETATTR() does not necessarily go to the wire.
2110 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2111 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2112 if (VOP_GETATTR(vp, &vattr) == 0 &&
2113 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2115 nfsstats.direofcache_hits++;
2121 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2122 * own cache coherency checks so we do not have to.
2124 tresid = uio->uio_resid;
2125 error = nfs_bioread(vp, uio, 0);
2127 if (!error && uio->uio_resid == tresid)
2128 nfsstats.direofcache_misses++;
2133 * Readdir rpc call. nfs_bioread->nfs_doio->nfs_readdirrpc.
2135 * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2136 * offset/block and converts the nfs formatted directory entries for userland
2137 * consumption as well as deals with offsets into the middle of blocks.
2138 * nfs_doio only deals with logical blocks. In particular, uio_offset will
2139 * be block-bounded. It must convert to cookies for the actual RPC.
2142 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2145 struct nfs_dirent *dp = NULL;
2150 caddr_t bpos, dpos, cp2;
2151 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2153 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2154 struct nfsnode *dnp = VTONFS(vp);
2156 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2158 int v3 = NFS_ISV3(vp);
2161 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2162 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2163 panic("nfs readdirrpc bad uio");
2167 * If there is no cookie, assume directory was stale.
2169 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2173 return (NFSERR_BAD_COOKIE);
2175 * Loop around doing readdir rpc's of size nm_readdirsize
2176 * truncated to a multiple of DIRBLKSIZ.
2177 * The stopping criteria is EOF or buffer full.
2179 while (more_dirs && bigenough) {
2180 nfsstats.rpccnt[NFSPROC_READDIR]++;
2181 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2185 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2186 *tl++ = cookie.nfsuquad[0];
2187 *tl++ = cookie.nfsuquad[1];
2188 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2189 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2191 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2192 *tl++ = cookie.nfsuquad[0];
2194 *tl = txdr_unsigned(nmp->nm_readdirsize);
2195 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2197 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2199 nfsm_dissect(tl, u_int32_t *,
2201 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2202 dnp->n_cookieverf.nfsuquad[1] = *tl;
2208 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2209 more_dirs = fxdr_unsigned(int, *tl);
2211 /* loop thru the dir entries, converting them to std form */
2212 while (more_dirs && bigenough) {
2214 nfsm_dissect(tl, u_int32_t *,
2216 fileno = fxdr_hyper(tl);
2217 len = fxdr_unsigned(int, *(tl + 2));
2219 nfsm_dissect(tl, u_int32_t *,
2221 fileno = fxdr_unsigned(u_quad_t, *tl++);
2222 len = fxdr_unsigned(int, *tl);
2224 if (len <= 0 || len > NFS_MAXNAMLEN) {
2231 * len is the number of bytes in the path element
2232 * name, not including the \0 termination.
2234 * tlen is the number of bytes w have to reserve for
2235 * the path element name.
2237 tlen = nfsm_rndup(len);
2239 tlen += 4; /* To ensure null termination */
2242 * If the entry would cross a DIRBLKSIZ boundary,
2243 * extend the previous nfs_dirent to cover the
2246 left = DIRBLKSIZ - blksiz;
2247 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2248 dp->nfs_reclen += left;
2249 uiop->uio_iov->iov_base += left;
2250 uiop->uio_iov->iov_len -= left;
2251 uiop->uio_offset += left;
2252 uiop->uio_resid -= left;
2255 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2258 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2259 dp->nfs_ino = fileno;
2260 dp->nfs_namlen = len;
2261 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2262 dp->nfs_type = DT_UNKNOWN;
2263 blksiz += dp->nfs_reclen;
2264 if (blksiz == DIRBLKSIZ)
2266 uiop->uio_offset += sizeof(struct nfs_dirent);
2267 uiop->uio_resid -= sizeof(struct nfs_dirent);
2268 uiop->uio_iov->iov_base += sizeof(struct nfs_dirent);
2269 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2270 nfsm_mtouio(uiop, len);
2273 * The uiop has advanced by nfs_dirent + len
2274 * but really needs to advance by
2277 cp = uiop->uio_iov->iov_base;
2279 *cp = '\0'; /* null terminate */
2280 uiop->uio_iov->iov_base += tlen;
2281 uiop->uio_iov->iov_len -= tlen;
2282 uiop->uio_offset += tlen;
2283 uiop->uio_resid -= tlen;
2286 * NFS strings must be rounded up (nfsm_myouio
2287 * handled that in the bigenough case).
2289 nfsm_adv(nfsm_rndup(len));
2292 nfsm_dissect(tl, u_int32_t *,
2295 nfsm_dissect(tl, u_int32_t *,
2300 * If we were able to accomodate the last entry,
2301 * get the cookie for the next one. Otherwise
2302 * hold-over the cookie for the one we were not
2303 * able to accomodate.
2306 cookie.nfsuquad[0] = *tl++;
2308 cookie.nfsuquad[1] = *tl++;
2314 more_dirs = fxdr_unsigned(int, *tl);
2317 * If at end of rpc data, get the eof boolean
2320 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2321 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2326 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2327 * by increasing d_reclen for the last record.
2330 left = DIRBLKSIZ - blksiz;
2331 dp->nfs_reclen += left;
2332 uiop->uio_iov->iov_base += left;
2333 uiop->uio_iov->iov_len -= left;
2334 uiop->uio_offset += left;
2335 uiop->uio_resid -= left;
2340 * We hit the end of the directory, update direofoffset.
2342 dnp->n_direofoffset = uiop->uio_offset;
2345 * There is more to go, insert the link cookie so the
2346 * next block can be read.
2348 if (uiop->uio_resid > 0)
2349 printf("EEK! readdirrpc resid > 0\n");
2350 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2358 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2361 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2364 struct nfs_dirent *dp;
2368 struct vnode *newvp;
2370 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2371 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2373 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2374 struct nfsnode *dnp = VTONFS(vp), *np;
2377 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2378 int attrflag, fhsize;
2379 struct namecache *ncp;
2380 struct namecache *dncp;
2381 struct nlcomponent nlc;
2387 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2388 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2389 panic("nfs readdirplusrpc bad uio");
2392 * Obtain the namecache record for the directory so we have something
2393 * to use as a basis for creating the entries. This function will
2394 * return a held (but not locked) ncp. The ncp may be disconnected
2395 * from the tree and cannot be used for upward traversals, and the
2396 * ncp may be unnamed. Note that other unrelated operations may
2397 * cause the ncp to be named at any time.
2399 dncp = cache_fromdvp(vp, NULL, 0);
2400 bzero(&nlc, sizeof(nlc));
2404 * If there is no cookie, assume directory was stale.
2406 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2410 return (NFSERR_BAD_COOKIE);
2412 * Loop around doing readdir rpc's of size nm_readdirsize
2413 * truncated to a multiple of DIRBLKSIZ.
2414 * The stopping criteria is EOF or buffer full.
2416 while (more_dirs && bigenough) {
2417 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2418 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2419 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2421 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2422 *tl++ = cookie.nfsuquad[0];
2423 *tl++ = cookie.nfsuquad[1];
2424 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2425 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2426 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2427 *tl = txdr_unsigned(nmp->nm_rsize);
2428 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2429 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2434 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2435 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2436 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2437 more_dirs = fxdr_unsigned(int, *tl);
2439 /* loop thru the dir entries, doctoring them to 4bsd form */
2440 while (more_dirs && bigenough) {
2441 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2442 fileno = fxdr_hyper(tl);
2443 len = fxdr_unsigned(int, *(tl + 2));
2444 if (len <= 0 || len > NFS_MAXNAMLEN) {
2449 tlen = nfsm_rndup(len);
2451 tlen += 4; /* To ensure null termination*/
2452 left = DIRBLKSIZ - blksiz;
2453 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2454 dp->nfs_reclen += left;
2455 uiop->uio_iov->iov_base += left;
2456 uiop->uio_iov->iov_len -= left;
2457 uiop->uio_offset += left;
2458 uiop->uio_resid -= left;
2461 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2464 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2465 dp->nfs_ino = fileno;
2466 dp->nfs_namlen = len;
2467 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2468 dp->nfs_type = DT_UNKNOWN;
2469 blksiz += dp->nfs_reclen;
2470 if (blksiz == DIRBLKSIZ)
2472 uiop->uio_offset += sizeof(struct nfs_dirent);
2473 uiop->uio_resid -= sizeof(struct nfs_dirent);
2474 uiop->uio_iov->iov_base += sizeof(struct nfs_dirent);
2475 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2476 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2477 nlc.nlc_namelen = len;
2478 nfsm_mtouio(uiop, len);
2479 cp = uiop->uio_iov->iov_base;
2482 uiop->uio_iov->iov_base += tlen;
2483 uiop->uio_iov->iov_len -= tlen;
2484 uiop->uio_offset += tlen;
2485 uiop->uio_resid -= tlen;
2487 nfsm_adv(nfsm_rndup(len));
2488 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2490 cookie.nfsuquad[0] = *tl++;
2491 cookie.nfsuquad[1] = *tl++;
2496 * Since the attributes are before the file handle
2497 * (sigh), we must skip over the attributes and then
2498 * come back and get them.
2500 attrflag = fxdr_unsigned(int, *tl);
2504 nfsm_adv(NFSX_V3FATTR);
2505 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2506 doit = fxdr_unsigned(int, *tl);
2508 nfsm_getfh(fhp, fhsize, 1);
2509 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2514 error = nfs_nget(vp->v_mount, fhp,
2522 if (doit && bigenough) {
2527 nfsm_loadattr(newvp, (struct vattr *)0);
2531 IFTODT(VTTOIF(np->n_vattr.va_type));
2533 printf("NFS/READDIRPLUS, ENTER %*.*s\n",
2534 nlc.nlc_namelen, nlc.nlc_namelen,
2536 ncp = cache_nlookup(dncp, &nlc);
2537 cache_setunresolved(ncp);
2538 cache_setvp(ncp, newvp);
2541 printf("NFS/READDIRPLUS, UNABLE TO ENTER"
2543 nlc.nlc_namelen, nlc.nlc_namelen,
2548 /* Just skip over the file handle */
2549 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2550 i = fxdr_unsigned(int, *tl);
2551 nfsm_adv(nfsm_rndup(i));
2553 if (newvp != NULLVP) {
2560 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2561 more_dirs = fxdr_unsigned(int, *tl);
2564 * If at end of rpc data, get the eof boolean
2567 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2568 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2573 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2574 * by increasing d_reclen for the last record.
2577 left = DIRBLKSIZ - blksiz;
2578 dp->nfs_reclen += left;
2579 uiop->uio_iov->iov_base += left;
2580 uiop->uio_iov->iov_len -= left;
2581 uiop->uio_offset += left;
2582 uiop->uio_resid -= left;
2586 * We are now either at the end of the directory or have filled the
2590 dnp->n_direofoffset = uiop->uio_offset;
2592 if (uiop->uio_resid > 0)
2593 printf("EEK! readdirplusrpc resid > 0\n");
2594 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2598 if (newvp != NULLVP) {
2611 * Silly rename. To make the NFS filesystem that is stateless look a little
2612 * more like the "ufs" a remove of an active vnode is translated to a rename
2613 * to a funny looking filename that is removed by nfs_inactive on the
2614 * nfsnode. There is the potential for another process on a different client
2615 * to create the same funny name between the nfs_lookitup() fails and the
2616 * nfs_rename() completes, but...
2619 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2621 struct sillyrename *sp;
2626 * We previously purged dvp instead of vp. I don't know why, it
2627 * completely destroys performance. We can't do it anyway with the
2628 * new VFS API since we would be breaking the namecache topology.
2630 cache_purge(vp); /* XXX */
2633 if (vp->v_type == VDIR)
2634 panic("nfs: sillyrename dir");
2636 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2637 M_NFSREQ, M_WAITOK);
2638 sp->s_cred = crdup(cnp->cn_cred);
2642 /* Fudge together a funny name */
2643 sp->s_namlen = sprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
2645 /* Try lookitups until we get one that isn't there */
2646 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2647 cnp->cn_td, (struct nfsnode **)0) == 0) {
2649 if (sp->s_name[4] > 'z') {
2654 error = nfs_renameit(dvp, cnp, sp);
2657 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2659 np->n_sillyrename = sp;
2664 free((caddr_t)sp, M_NFSREQ);
2669 * Look up a file name and optionally either update the file handle or
2670 * allocate an nfsnode, depending on the value of npp.
2671 * npp == NULL --> just do the lookup
2672 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2674 * *npp != NULL --> update the file handle in the vnode
2677 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2678 struct thread *td, struct nfsnode **npp)
2683 struct vnode *newvp = (struct vnode *)0;
2684 struct nfsnode *np, *dnp = VTONFS(dvp);
2685 caddr_t bpos, dpos, cp2;
2686 int error = 0, fhlen, attrflag;
2687 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2689 int v3 = NFS_ISV3(dvp);
2691 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2692 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2693 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2694 nfsm_fhtom(dvp, v3);
2695 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2696 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2697 if (npp && !error) {
2698 nfsm_getfh(nfhp, fhlen, v3);
2701 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2702 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2703 np->n_fhp = &np->n_fh;
2704 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2705 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2706 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2707 np->n_fhsize = fhlen;
2709 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2713 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2721 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
2722 if (!attrflag && *npp == NULL) {
2731 nfsm_loadattr(newvp, (struct vattr *)0);
2735 if (npp && *npp == NULL) {
2750 * Nfs Version 3 commit rpc
2753 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2758 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2759 caddr_t bpos, dpos, cp2;
2760 int error = 0, wccflag = NFSV3_WCCRATTR;
2761 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2763 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2765 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2766 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2768 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2769 txdr_hyper(offset, tl);
2771 *tl = txdr_unsigned(cnt);
2772 nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2773 nfsm_wcc_data(vp, wccflag);
2775 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2776 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2777 NFSX_V3WRITEVERF)) {
2778 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2780 error = NFSERR_STALEWRITEVERF;
2790 * - make nfs_bmap() essentially a no-op that does no translation
2791 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2792 * (Maybe I could use the process's page mapping, but I was concerned that
2793 * Kernel Write might not be enabled and also figured copyout() would do
2794 * a lot more work than bcopy() and also it currently happens in the
2795 * context of the swapper process (2).
2797 * nfs_bmap(struct vnode *a_vp, off_t a_loffset, struct vnode **a_vpp,
2798 * off_t *a_doffsetp, int *a_runp, int *a_runb)
2801 nfs_bmap(struct vop_bmap_args *ap)
2803 struct vnode *vp = ap->a_vp;
2805 if (ap->a_vpp != NULL)
2807 if (ap->a_doffsetp != NULL)
2808 *ap->a_doffsetp = ap->a_loffset;
2809 if (ap->a_runp != NULL)
2811 if (ap->a_runb != NULL)
2819 * For async requests when nfsiod(s) are running, queue the request by
2820 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2824 nfs_strategy(struct vop_strategy_args *ap)
2826 struct bio *bio = ap->a_bio;
2828 struct buf *bp = bio->bio_buf;
2832 KASSERT(bp->b_cmd != BUF_CMD_DONE,
2833 ("nfs_strategy: buffer %p unexpectedly marked done", bp));
2834 KASSERT(BUF_REFCNT(bp) > 0,
2835 ("nfs_strategy: buffer %p not locked", bp));
2837 if (bp->b_flags & B_ASYNC)
2840 td = curthread; /* XXX */
2843 * We probably don't need to push an nbio any more since no
2844 * block conversion is required due to the use of 64 bit byte
2845 * offsets, but do it anyway.
2847 nbio = push_bio(bio);
2848 nbio->bio_offset = bio->bio_offset;
2851 * If the op is asynchronous and an i/o daemon is waiting
2852 * queue the request, wake it up and wait for completion
2853 * otherwise just do it ourselves.
2855 if ((bp->b_flags & B_ASYNC) == 0 || nfs_asyncio(ap->a_vp, nbio, td))
2856 error = nfs_doio(ap->a_vp, nbio, td);
2863 * NB Currently unsupported.
2865 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
2866 * struct thread *a_td)
2870 nfs_mmap(struct vop_mmap_args *ap)
2876 * fsync vnode op. Just call nfs_flush() with commit == 1.
2878 * nfs_fsync(struct vnodeop_desc *a_desc, struct vnode *a_vp,
2879 * struct ucred * a_cred, int a_waitfor, struct thread *a_td)
2883 nfs_fsync(struct vop_fsync_args *ap)
2885 return (nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1));
2889 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
2890 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
2891 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
2892 * set the buffer contains data that has already been written to the server
2893 * and which now needs a commit RPC.
2895 * If commit is 0 we only take one pass and only flush buffers containing new
2898 * If commit is 1 we take two passes, issuing a commit RPC in the second
2901 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
2902 * to completely flush all pending data.
2904 * Note that the RB_SCAN code properly handles the case where the
2905 * callback might block and directly or indirectly (another thread) cause
2906 * the RB tree to change.
2909 #ifndef NFS_COMMITBVECSIZ
2910 #define NFS_COMMITBVECSIZ 16
2913 struct nfs_flush_info {
2914 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
2921 struct buf *bvary[NFS_COMMITBVECSIZ];
2927 static int nfs_flush_bp(struct buf *bp, void *data);
2928 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
2931 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
2933 struct nfsnode *np = VTONFS(vp);
2934 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2935 struct nfs_flush_info info;
2938 bzero(&info, sizeof(info));
2941 info.waitfor = waitfor;
2942 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
2949 info.mode = NFI_FLUSHNEW;
2950 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2951 nfs_flush_bp, &info);
2954 * Take a second pass if committing and no error occured.
2955 * Clean up any left over collection (whether an error
2958 if (commit && error == 0) {
2959 info.mode = NFI_COMMIT;
2960 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2961 nfs_flush_bp, &info);
2963 error = nfs_flush_docommit(&info, error);
2967 * Wait for pending I/O to complete before checking whether
2968 * any further dirty buffers exist.
2970 while (waitfor == MNT_WAIT && vp->v_track_write.bk_active) {
2971 vp->v_track_write.bk_waitflag = 1;
2972 error = tsleep(&vp->v_track_write,
2973 info.slpflag, "nfsfsync", info.slptimeo);
2976 * We have to be able to break out if this
2977 * is an 'intr' mount.
2979 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
2985 * Since we do not process pending signals,
2986 * once we get a PCATCH our tsleep() will no
2987 * longer sleep, switch to a fixed timeout
2990 if (info.slpflag == PCATCH) {
2992 info.slptimeo = 2 * hz;
2999 * Loop if we are flushing synchronous as well as committing,
3000 * and dirty buffers are still present. Otherwise we might livelock.
3002 } while (waitfor == MNT_WAIT && commit &&
3003 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3006 * The callbacks have to return a negative error to terminate the
3013 * Deal with any error collection
3015 if (np->n_flag & NWRITEERR) {
3016 error = np->n_error;
3017 np->n_flag &= ~NWRITEERR;
3025 nfs_flush_bp(struct buf *bp, void *data)
3027 struct nfs_flush_info *info = data;
3032 switch(info->mode) {
3035 if (info->loops && info->waitfor == MNT_WAIT) {
3036 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3038 int lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3039 if (info->slpflag & PCATCH)
3040 lkflags |= LK_PCATCH;
3041 error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3045 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3048 KKASSERT(bp->b_vp == info->vp);
3050 if ((bp->b_flags & B_DELWRI) == 0)
3051 panic("nfs_fsync: not dirty");
3052 if (bp->b_flags & B_NEEDCOMMIT) {
3059 bp->b_flags |= B_ASYNC;
3069 * Only process buffers in need of a commit which we can
3070 * immediately lock. This may prevent a buffer from being
3071 * committed, but the normal flush loop will block on the
3072 * same buffer so we shouldn't get into an endless loop.
3075 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3076 (B_DELWRI | B_NEEDCOMMIT) ||
3077 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
3082 KKASSERT(bp->b_vp == info->vp);
3086 * NOTE: storing the bp in the bvary[] basically sets
3087 * it up for a commit operation.
3089 * We must call vfs_busy_pages() now so the commit operation
3090 * is interlocked with user modifications to memory mapped
3093 * Note: to avoid loopback deadlocks, we do not
3094 * assign b_runningbufspace.
3096 bp->b_cmd = BUF_CMD_WRITE;
3097 vfs_busy_pages(bp->b_vp, bp);
3098 info->bvary[info->bvsize] = bp;
3099 toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3100 if (info->bvsize == 0 || toff < info->beg_off)
3101 info->beg_off = toff;
3102 toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3103 if (info->bvsize == 0 || toff > info->end_off)
3104 info->end_off = toff;
3106 if (info->bvsize == NFS_COMMITBVECSIZ) {
3107 error = nfs_flush_docommit(info, 0);
3108 KKASSERT(info->bvsize == 0);
3117 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3127 if (info->bvsize > 0) {
3129 * Commit data on the server, as required. Note that
3130 * nfs_commit will use the vnode's cred for the commit.
3131 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3133 bytes = info->end_off - info->beg_off;
3134 if (bytes > 0x40000000)
3139 retv = nfs_commit(vp, info->beg_off,
3140 (int)bytes, info->td);
3141 if (retv == NFSERR_STALEWRITEVERF)
3142 nfs_clearcommit(vp->v_mount);
3146 * Now, either mark the blocks I/O done or mark the
3147 * blocks dirty, depending on whether the commit
3150 for (i = 0; i < info->bvsize; ++i) {
3151 bp = info->bvary[i];
3152 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3155 * Error, leave B_DELWRI intact
3157 vfs_unbusy_pages(bp);
3158 bp->b_cmd = BUF_CMD_DONE;
3162 * Success, remove B_DELWRI ( bundirty() ).
3164 * b_dirtyoff/b_dirtyend seem to be NFS
3165 * specific. We should probably move that
3166 * into bundirty(). XXX
3168 * We are faking an I/O write, we have to
3169 * start the transaction in order to
3170 * immediately biodone() it.
3173 bp->b_flags |= B_ASYNC;
3175 bp->b_flags &= ~B_ERROR;
3176 bp->b_dirtyoff = bp->b_dirtyend = 0;
3178 biodone(&bp->b_bio1);
3187 * NFS advisory byte-level locks.
3188 * Currently unsupported.
3190 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3194 nfs_advlock(struct vop_advlock_args *ap)
3196 struct nfsnode *np = VTONFS(ap->a_vp);
3199 * The following kludge is to allow diskless support to work
3200 * until a real NFS lockd is implemented. Basically, just pretend
3201 * that this is a local lock.
3203 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3207 * Print out the contents of an nfsnode.
3209 * nfs_print(struct vnode *a_vp)
3212 nfs_print(struct vop_print_args *ap)
3214 struct vnode *vp = ap->a_vp;
3215 struct nfsnode *np = VTONFS(vp);
3217 printf("tag VT_NFS, fileid %ld fsid 0x%x",
3218 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3219 if (vp->v_type == VFIFO)
3226 * nfs special file access vnode op.
3227 * Essentially just get vattr and then imitate iaccess() since the device is
3228 * local to the client.
3230 * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
3231 * struct thread *a_td)
3234 nfsspec_access(struct vop_access_args *ap)
3238 struct ucred *cred = ap->a_cred;
3239 struct vnode *vp = ap->a_vp;
3240 mode_t mode = ap->a_mode;
3246 * Disallow write attempts on filesystems mounted read-only;
3247 * unless the file is a socket, fifo, or a block or character
3248 * device resident on the filesystem.
3250 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3251 switch (vp->v_type) {
3261 * If you're the super-user,
3262 * you always get access.
3264 if (cred->cr_uid == 0)
3267 error = VOP_GETATTR(vp, vap);
3271 * Access check is based on only one of owner, group, public.
3272 * If not owner, then check group. If not a member of the
3273 * group, then check public access.
3275 if (cred->cr_uid != vap->va_uid) {
3277 gp = cred->cr_groups;
3278 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3279 if (vap->va_gid == *gp)
3285 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3290 * Read wrapper for special devices.
3292 * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3293 * struct ucred *a_cred)
3296 nfsspec_read(struct vop_read_args *ap)
3298 struct nfsnode *np = VTONFS(ap->a_vp);
3304 getnanotime(&np->n_atim);
3305 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3309 * Write wrapper for special devices.
3311 * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3312 * struct ucred *a_cred)
3315 nfsspec_write(struct vop_write_args *ap)
3317 struct nfsnode *np = VTONFS(ap->a_vp);
3323 getnanotime(&np->n_mtim);
3324 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3328 * Close wrapper for special devices.
3330 * Update the times on the nfsnode then do device close.
3332 * nfsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
3333 * struct thread *a_td)
3336 nfsspec_close(struct vop_close_args *ap)
3338 struct vnode *vp = ap->a_vp;
3339 struct nfsnode *np = VTONFS(vp);
3342 if (np->n_flag & (NACC | NUPD)) {
3344 if (vp->v_usecount == 1 &&
3345 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3347 if (np->n_flag & NACC)
3348 vattr.va_atime = np->n_atim;
3349 if (np->n_flag & NUPD)
3350 vattr.va_mtime = np->n_mtim;
3351 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3354 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3358 * Read wrapper for fifos.
3360 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3361 * struct ucred *a_cred)
3364 nfsfifo_read(struct vop_read_args *ap)
3366 struct nfsnode *np = VTONFS(ap->a_vp);
3372 getnanotime(&np->n_atim);
3373 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3377 * Write wrapper for fifos.
3379 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3380 * struct ucred *a_cred)
3383 nfsfifo_write(struct vop_write_args *ap)
3385 struct nfsnode *np = VTONFS(ap->a_vp);
3391 getnanotime(&np->n_mtim);
3392 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3396 * Close wrapper for fifos.
3398 * Update the times on the nfsnode then do fifo close.
3400 * nfsfifo_close(struct vnode *a_vp, int a_fflag, struct thread *a_td)
3403 nfsfifo_close(struct vop_close_args *ap)
3405 struct vnode *vp = ap->a_vp;
3406 struct nfsnode *np = VTONFS(vp);
3410 if (np->n_flag & (NACC | NUPD)) {
3412 if (np->n_flag & NACC)
3414 if (np->n_flag & NUPD)
3417 if (vp->v_usecount == 1 &&
3418 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3420 if (np->n_flag & NACC)
3421 vattr.va_atime = np->n_atim;
3422 if (np->n_flag & NUPD)
3423 vattr.va_mtime = np->n_mtim;
3424 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3427 return (VOCALL(&fifo_vnode_vops, &ap->a_head));