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.63 2006/07/19 06:08:13 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 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 vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
653 nfs_setattr(struct vop_setattr_args *ap)
655 struct vnode *vp = ap->a_vp;
656 struct nfsnode *np = VTONFS(vp);
657 struct vattr *vap = ap->a_vap;
660 thread_t td = curthread;
667 * Setting of flags is not supported.
669 if (vap->va_flags != VNOVAL)
673 * Disallow write attempts if the filesystem is mounted read-only.
675 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
676 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
677 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
678 (vp->v_mount->mnt_flag & MNT_RDONLY))
680 if (vap->va_size != VNOVAL) {
681 switch (vp->v_type) {
688 if (vap->va_mtime.tv_sec == VNOVAL &&
689 vap->va_atime.tv_sec == VNOVAL &&
690 vap->va_mode == (mode_t)VNOVAL &&
691 vap->va_uid == (uid_t)VNOVAL &&
692 vap->va_gid == (gid_t)VNOVAL)
694 vap->va_size = VNOVAL;
698 * Disallow write attempts if the filesystem is
701 if (vp->v_mount->mnt_flag & MNT_RDONLY)
705 * This is nasty. The RPCs we send to flush pending
706 * data often return attribute information which is
707 * cached via a callback to nfs_loadattrcache(), which
708 * has the effect of changing our notion of the file
709 * size. Due to flushed appends and other operations
710 * the file size can be set to virtually anything,
711 * including values that do not match either the old
712 * or intended file size.
714 * When this condition is detected we must loop to
715 * try the operation again. Hopefully no more
716 * flushing is required on the loop so it works the
717 * second time around. THIS CASE ALMOST ALWAYS
722 error = nfs_meta_setsize(vp, td, vap->va_size);
724 if (np->n_flag & NLMODIFIED) {
725 if (vap->va_size == 0)
726 error = nfs_vinvalbuf(vp, 0, 1);
728 error = nfs_vinvalbuf(vp, V_SAVE, 1);
731 * note: this loop case almost always happens at
732 * least once per truncation.
734 if (error == 0 && np->n_size != vap->va_size)
736 np->n_vattr.va_size = vap->va_size;
739 } else if ((vap->va_mtime.tv_sec != VNOVAL ||
740 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NLMODIFIED) &&
741 vp->v_type == VREG &&
742 (error = nfs_vinvalbuf(vp, V_SAVE, 1)) == EINTR
746 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
749 * Sanity check if a truncation was issued. This should only occur
750 * if multiple processes are racing on the same file.
752 if (error == 0 && vap->va_size != VNOVAL &&
753 np->n_size != vap->va_size) {
754 printf("NFS ftruncate: server disagrees on the file size: %lld/%lld/%lld\n", tsize, vap->va_size, np->n_size);
757 if (error && vap->va_size != VNOVAL) {
758 np->n_size = np->n_vattr.va_size = tsize;
759 vnode_pager_setsize(vp, np->n_size);
765 * Do an nfs setattr rpc.
768 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
769 struct ucred *cred, struct thread *td)
771 struct nfsv2_sattr *sp;
772 struct nfsnode *np = VTONFS(vp);
775 caddr_t bpos, dpos, cp2;
777 int error = 0, wccflag = NFSV3_WCCRATTR;
778 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
779 int v3 = NFS_ISV3(vp);
781 nfsstats.rpccnt[NFSPROC_SETATTR]++;
782 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
785 nfsm_v3attrbuild(vap, TRUE);
786 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
789 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
790 if (vap->va_mode == (mode_t)VNOVAL)
791 sp->sa_mode = nfs_xdrneg1;
793 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
794 if (vap->va_uid == (uid_t)VNOVAL)
795 sp->sa_uid = nfs_xdrneg1;
797 sp->sa_uid = txdr_unsigned(vap->va_uid);
798 if (vap->va_gid == (gid_t)VNOVAL)
799 sp->sa_gid = nfs_xdrneg1;
801 sp->sa_gid = txdr_unsigned(vap->va_gid);
802 sp->sa_size = txdr_unsigned(vap->va_size);
803 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
804 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
806 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
809 nfsm_wcc_data(vp, wccflag);
811 nfsm_loadattr(vp, (struct vattr *)0);
818 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
819 * nfs_lookup() until all remaining new api calls are implemented.
821 * Resolve a namecache entry. This function is passed a locked ncp and
822 * must call cache_setvp() on it as appropriate to resolve the entry.
825 nfs_nresolve(struct vop_nresolve_args *ap)
827 struct thread *td = curthread;
828 struct namecache *ncp;
839 /******NFSM MACROS********/
840 struct mbuf *mb, *mrep, *mreq, *mb2, *md;
841 caddr_t bpos, dpos, cp, cp2;
848 KKASSERT(ncp->nc_parent && ncp->nc_parent->nc_vp);
849 dvp = ncp->nc_parent->nc_vp;
850 if ((error = vget(dvp, LK_SHARED)) != 0)
855 nfsstats.lookupcache_misses++;
856 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
858 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
859 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
861 nfsm_strtom(ncp->nc_name, len, NFS_MAXNAMLEN);
862 nfsm_request(dvp, NFSPROC_LOOKUP, td, ap->a_cred);
865 * Cache negatve lookups to reduce NFS traffic, but use
866 * a fast timeout. Otherwise use a timeout of 1 tick.
867 * XXX we should add a namecache flag for no-caching
868 * to uncache the negative hit as soon as possible, but
869 * we cannot simply destroy the entry because it is used
870 * as a placeholder by the caller.
872 if (error == ENOENT) {
875 if (nfsneg_cache_timeout)
876 nticks = nfsneg_cache_timeout * hz;
879 cache_setvp(ncp, NULL);
880 cache_settimeout(ncp, nticks);
882 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
888 * Success, get the file handle, do various checks, and load
889 * post-operation data from the reply packet. Theoretically
890 * we should never be looking up "." so, theoretically, we
891 * should never get the same file handle as our directory. But
892 * we check anyway. XXX
894 * Note that no timeout is set for the positive cache hit. We
895 * assume, theoretically, that ESTALE returns will be dealt with
896 * properly to handle NFS races and in anycase we cannot depend
897 * on a timeout to deal with NFS open/create/excl issues so instead
898 * of a bad hack here the rest of the NFS client code needs to do
901 nfsm_getfh(fhp, fhsize, v3);
904 if (NFS_CMPFH(np, fhp, fhsize)) {
908 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
917 nfsm_postop_attr(nvp, attrflag, NFS_LATTR_NOSHRINK);
918 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
920 nfsm_loadattr(nvp, NULL);
922 cache_setvp(ncp, nvp);
936 * 'cached' nfs directory lookup
938 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
940 * nfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
941 * struct componentname *a_cnp)
944 nfs_lookup(struct vop_old_lookup_args *ap)
946 struct componentname *cnp = ap->a_cnp;
947 struct vnode *dvp = ap->a_dvp;
948 struct vnode **vpp = ap->a_vpp;
949 int flags = cnp->cn_flags;
954 struct nfsmount *nmp;
955 caddr_t bpos, dpos, cp2;
956 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
960 int lockparent, wantparent, error = 0, attrflag, fhsize;
961 int v3 = NFS_ISV3(dvp);
964 * Read-only mount check and directory check.
967 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
968 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
971 if (dvp->v_type != VDIR)
975 * Look it up in the cache. Note that ENOENT is only returned if we
976 * previously entered a negative hit (see later on). The additional
977 * nfsneg_cache_timeout check causes previously cached results to
978 * be instantly ignored if the negative caching is turned off.
980 lockparent = flags & CNP_LOCKPARENT;
981 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
982 nmp = VFSTONFS(dvp->v_mount);
990 nfsstats.lookupcache_misses++;
991 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
992 len = cnp->cn_namelen;
993 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
994 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
996 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
997 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
999 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1003 nfsm_getfh(fhp, fhsize, v3);
1006 * Handle RENAME case...
1008 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1009 if (NFS_CMPFH(np, fhp, fhsize)) {
1013 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1020 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1021 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1023 nfsm_loadattr(newvp, (struct vattr *)0);
1028 cnp->cn_flags |= CNP_PDIRUNLOCK;
1033 if (flags & CNP_ISDOTDOT) {
1035 cnp->cn_flags |= CNP_PDIRUNLOCK;
1036 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1038 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1039 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1040 return (error); /* NOTE: return error from nget */
1044 error = vn_lock(dvp, LK_EXCLUSIVE);
1049 cnp->cn_flags |= CNP_PDIRUNLOCK;
1051 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1055 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1062 cnp->cn_flags |= CNP_PDIRUNLOCK;
1067 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1068 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1070 nfsm_loadattr(newvp, (struct vattr *)0);
1072 /* XXX MOVE TO nfs_nremove() */
1073 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1074 cnp->cn_nameiop != NAMEI_DELETE) {
1075 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1082 if (newvp != NULLVP) {
1086 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1087 cnp->cn_nameiop == NAMEI_RENAME) &&
1091 cnp->cn_flags |= CNP_PDIRUNLOCK;
1093 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1096 error = EJUSTRETURN;
1104 * Just call nfs_bioread() to do the work.
1106 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1107 * struct ucred *a_cred)
1110 nfs_read(struct vop_read_args *ap)
1112 struct vnode *vp = ap->a_vp;
1114 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1115 switch (vp->v_type) {
1117 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1128 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1131 nfs_readlink(struct vop_readlink_args *ap)
1133 struct vnode *vp = ap->a_vp;
1135 if (vp->v_type != VLNK)
1137 return (nfs_bioread(vp, ap->a_uio, 0));
1141 * Do a readlink rpc.
1142 * Called by nfs_doio() from below the buffer cache.
1145 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1150 caddr_t bpos, dpos, cp2;
1151 int error = 0, len, attrflag;
1152 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1153 int v3 = NFS_ISV3(vp);
1155 nfsstats.rpccnt[NFSPROC_READLINK]++;
1156 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1158 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1160 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1162 nfsm_strsiz(len, NFS_MAXPATHLEN);
1163 if (len == NFS_MAXPATHLEN) {
1164 struct nfsnode *np = VTONFS(vp);
1165 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1168 nfsm_mtouio(uiop, len);
1180 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1185 caddr_t bpos, dpos, cp2;
1186 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1187 struct nfsmount *nmp;
1188 int error = 0, len, retlen, tsiz, eof, attrflag;
1189 int v3 = NFS_ISV3(vp);
1194 nmp = VFSTONFS(vp->v_mount);
1195 tsiz = uiop->uio_resid;
1196 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1199 nfsstats.rpccnt[NFSPROC_READ]++;
1200 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1201 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1203 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1205 txdr_hyper(uiop->uio_offset, tl);
1206 *(tl + 2) = txdr_unsigned(len);
1208 *tl++ = txdr_unsigned(uiop->uio_offset);
1209 *tl++ = txdr_unsigned(len);
1212 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1214 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1219 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1220 eof = fxdr_unsigned(int, *(tl + 1));
1222 nfsm_loadattr(vp, (struct vattr *)0);
1223 nfsm_strsiz(retlen, nmp->nm_rsize);
1224 nfsm_mtouio(uiop, retlen);
1228 if (eof || retlen == 0) {
1231 } else if (retlen < len) {
1243 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, int *must_commit)
1247 int32_t t1, t2, backup;
1248 caddr_t bpos, dpos, cp2;
1249 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1250 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1251 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1252 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1255 if (uiop->uio_iovcnt != 1)
1256 panic("nfs: writerpc iovcnt > 1");
1259 tsiz = uiop->uio_resid;
1260 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1263 nfsstats.rpccnt[NFSPROC_WRITE]++;
1264 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1265 nfsm_reqhead(vp, NFSPROC_WRITE,
1266 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1269 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1270 txdr_hyper(uiop->uio_offset, tl);
1272 *tl++ = txdr_unsigned(len);
1273 *tl++ = txdr_unsigned(*iomode);
1274 *tl = txdr_unsigned(len);
1278 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1279 /* Set both "begin" and "current" to non-garbage. */
1280 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1281 *tl++ = x; /* "begin offset" */
1282 *tl++ = x; /* "current offset" */
1283 x = txdr_unsigned(len);
1284 *tl++ = x; /* total to this offset */
1285 *tl = x; /* size of this write */
1287 nfsm_uiotom(uiop, len);
1288 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1291 * The write RPC returns a before and after mtime. The
1292 * nfsm_wcc_data() macro checks the before n_mtime
1293 * against the before time and stores the after time
1294 * in the nfsnode's cached vattr and n_mtime field.
1295 * The NRMODIFIED bit will be set if the before
1296 * time did not match the original mtime.
1298 wccflag = NFSV3_WCCCHK;
1299 nfsm_wcc_data(vp, wccflag);
1301 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1302 + NFSX_V3WRITEVERF);
1303 rlen = fxdr_unsigned(int, *tl++);
1308 } else if (rlen < len) {
1309 backup = len - rlen;
1310 uiop->uio_iov->iov_base -= backup;
1311 uiop->uio_iov->iov_len += backup;
1312 uiop->uio_offset -= backup;
1313 uiop->uio_resid += backup;
1316 commit = fxdr_unsigned(int, *tl++);
1319 * Return the lowest committment level
1320 * obtained by any of the RPCs.
1322 if (committed == NFSV3WRITE_FILESYNC)
1324 else if (committed == NFSV3WRITE_DATASYNC &&
1325 commit == NFSV3WRITE_UNSTABLE)
1327 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1328 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1330 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1331 } else if (bcmp((caddr_t)tl,
1332 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1334 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1339 nfsm_loadattr(vp, (struct vattr *)0);
1347 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1348 committed = NFSV3WRITE_FILESYNC;
1349 *iomode = committed;
1351 uiop->uio_resid = tsiz;
1357 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1358 * mode set to specify the file type and the size field for rdev.
1361 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1364 struct nfsv2_sattr *sp;
1368 struct vnode *newvp = (struct vnode *)0;
1369 struct nfsnode *np = (struct nfsnode *)0;
1373 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1374 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1376 int v3 = NFS_ISV3(dvp);
1378 if (vap->va_type == VCHR || vap->va_type == VBLK)
1379 rdev = txdr_unsigned(vap->va_rdev);
1380 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1383 return (EOPNOTSUPP);
1385 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1388 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1389 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1390 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1391 nfsm_fhtom(dvp, v3);
1392 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1394 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1395 *tl++ = vtonfsv3_type(vap->va_type);
1396 nfsm_v3attrbuild(vap, FALSE);
1397 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1398 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1399 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1400 *tl = txdr_unsigned(uminor(vap->va_rdev));
1403 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1404 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1405 sp->sa_uid = nfs_xdrneg1;
1406 sp->sa_gid = nfs_xdrneg1;
1408 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1409 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1411 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1413 nfsm_mtofh(dvp, newvp, v3, gotvp);
1417 newvp = (struct vnode *)0;
1419 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1420 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1426 nfsm_wcc_data(dvp, wccflag);
1435 VTONFS(dvp)->n_flag |= NLMODIFIED;
1437 VTONFS(dvp)->n_attrstamp = 0;
1443 * just call nfs_mknodrpc() to do the work.
1445 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1446 * struct componentname *a_cnp, struct vattr *a_vap)
1450 nfs_mknod(struct vop_old_mknod_args *ap)
1452 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1455 static u_long create_verf;
1457 * nfs file create call
1459 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1460 * struct componentname *a_cnp, struct vattr *a_vap)
1463 nfs_create(struct vop_old_create_args *ap)
1465 struct vnode *dvp = ap->a_dvp;
1466 struct vattr *vap = ap->a_vap;
1467 struct componentname *cnp = ap->a_cnp;
1468 struct nfsv2_sattr *sp;
1472 struct nfsnode *np = (struct nfsnode *)0;
1473 struct vnode *newvp = (struct vnode *)0;
1474 caddr_t bpos, dpos, cp2;
1475 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1476 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1478 int v3 = NFS_ISV3(dvp);
1481 * Oops, not for me..
1483 if (vap->va_type == VSOCK)
1484 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1486 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1489 if (vap->va_vaflags & VA_EXCLUSIVE)
1492 nfsstats.rpccnt[NFSPROC_CREATE]++;
1493 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1494 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1495 nfsm_fhtom(dvp, v3);
1496 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1498 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1499 if (fmode & O_EXCL) {
1500 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1501 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1503 if (!TAILQ_EMPTY(&in_ifaddrhead))
1504 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr;
1507 *tl++ = create_verf;
1508 *tl = ++create_verf;
1510 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1511 nfsm_v3attrbuild(vap, FALSE);
1514 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1515 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1516 sp->sa_uid = nfs_xdrneg1;
1517 sp->sa_gid = nfs_xdrneg1;
1519 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1520 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1522 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1524 nfsm_mtofh(dvp, newvp, v3, gotvp);
1528 newvp = (struct vnode *)0;
1530 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1531 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1537 nfsm_wcc_data(dvp, wccflag);
1541 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1547 } else if (v3 && (fmode & O_EXCL)) {
1549 * We are normally called with only a partially initialized
1550 * VAP. Since the NFSv3 spec says that server may use the
1551 * file attributes to store the verifier, the spec requires
1552 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1553 * in atime, but we can't really assume that all servers will
1554 * so we ensure that our SETATTR sets both atime and mtime.
1556 if (vap->va_mtime.tv_sec == VNOVAL)
1557 vfs_timestamp(&vap->va_mtime);
1558 if (vap->va_atime.tv_sec == VNOVAL)
1559 vap->va_atime = vap->va_mtime;
1560 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1564 * The new np may have enough info for access
1565 * checks, make sure rucred and wucred are
1566 * initialized for read and write rpc's.
1569 if (np->n_rucred == NULL)
1570 np->n_rucred = crhold(cnp->cn_cred);
1571 if (np->n_wucred == NULL)
1572 np->n_wucred = crhold(cnp->cn_cred);
1575 VTONFS(dvp)->n_flag |= NLMODIFIED;
1577 VTONFS(dvp)->n_attrstamp = 0;
1582 * nfs file remove call
1583 * To try and make nfs semantics closer to ufs semantics, a file that has
1584 * other processes using the vnode is renamed instead of removed and then
1585 * removed later on the last close.
1586 * - If v_usecount > 1
1587 * If a rename is not already in the works
1588 * call nfs_sillyrename() to set it up
1592 * nfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
1593 * struct componentname *a_cnp)
1596 nfs_remove(struct vop_old_remove_args *ap)
1598 struct vnode *vp = ap->a_vp;
1599 struct vnode *dvp = ap->a_dvp;
1600 struct componentname *cnp = ap->a_cnp;
1601 struct nfsnode *np = VTONFS(vp);
1606 if (vp->v_usecount < 1)
1607 panic("nfs_remove: bad v_usecount");
1609 if (vp->v_type == VDIR)
1611 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1612 VOP_GETATTR(vp, &vattr) == 0 &&
1613 vattr.va_nlink > 1)) {
1615 * throw away biocache buffers, mainly to avoid
1616 * unnecessary delayed writes later.
1618 error = nfs_vinvalbuf(vp, 0, 1);
1621 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1622 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1624 * Kludge City: If the first reply to the remove rpc is lost..
1625 * the reply to the retransmitted request will be ENOENT
1626 * since the file was in fact removed
1627 * Therefore, we cheat and return success.
1629 if (error == ENOENT)
1631 } else if (!np->n_sillyrename) {
1632 error = nfs_sillyrename(dvp, vp, cnp);
1634 np->n_attrstamp = 0;
1639 * nfs file remove rpc called from nfs_inactive
1642 nfs_removeit(struct sillyrename *sp)
1644 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1649 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1652 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1653 struct ucred *cred, struct thread *td)
1658 caddr_t bpos, dpos, cp2;
1659 int error = 0, wccflag = NFSV3_WCCRATTR;
1660 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1661 int v3 = NFS_ISV3(dvp);
1663 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1664 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1665 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1666 nfsm_fhtom(dvp, v3);
1667 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1668 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1670 nfsm_wcc_data(dvp, wccflag);
1673 VTONFS(dvp)->n_flag |= NLMODIFIED;
1675 VTONFS(dvp)->n_attrstamp = 0;
1680 * nfs file rename call
1682 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1683 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1684 * struct vnode *a_tvp, struct componentname *a_tcnp)
1687 nfs_rename(struct vop_old_rename_args *ap)
1689 struct vnode *fvp = ap->a_fvp;
1690 struct vnode *tvp = ap->a_tvp;
1691 struct vnode *fdvp = ap->a_fdvp;
1692 struct vnode *tdvp = ap->a_tdvp;
1693 struct componentname *tcnp = ap->a_tcnp;
1694 struct componentname *fcnp = ap->a_fcnp;
1697 /* Check for cross-device rename */
1698 if ((fvp->v_mount != tdvp->v_mount) ||
1699 (tvp && (fvp->v_mount != tvp->v_mount))) {
1705 * We have to flush B_DELWRI data prior to renaming
1706 * the file. If we don't, the delayed-write buffers
1707 * can be flushed out later after the file has gone stale
1708 * under NFSV3. NFSV2 does not have this problem because
1709 * ( as far as I can tell ) it flushes dirty buffers more
1713 VOP_FSYNC(fvp, MNT_WAIT);
1715 VOP_FSYNC(tvp, MNT_WAIT);
1718 * If the tvp exists and is in use, sillyrename it before doing the
1719 * rename of the new file over it.
1721 * XXX Can't sillyrename a directory.
1723 * We do not attempt to do any namecache purges in this old API
1724 * routine. The new API compat functions have access to the actual
1725 * namecache structures and will do it for us.
1727 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1728 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1735 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1736 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1749 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1751 if (error == ENOENT)
1757 * nfs file rename rpc called from nfs_remove() above
1760 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1761 struct sillyrename *sp)
1763 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1764 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1768 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1771 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1772 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1773 struct ucred *cred, struct thread *td)
1778 caddr_t bpos, dpos, cp2;
1779 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1780 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1781 int v3 = NFS_ISV3(fdvp);
1783 nfsstats.rpccnt[NFSPROC_RENAME]++;
1784 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1785 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1786 nfsm_rndup(tnamelen));
1787 nfsm_fhtom(fdvp, v3);
1788 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1789 nfsm_fhtom(tdvp, v3);
1790 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1791 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1793 nfsm_wcc_data(fdvp, fwccflag);
1794 nfsm_wcc_data(tdvp, twccflag);
1798 VTONFS(fdvp)->n_flag |= NLMODIFIED;
1799 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1801 VTONFS(fdvp)->n_attrstamp = 0;
1803 VTONFS(tdvp)->n_attrstamp = 0;
1808 * nfs hard link create call
1810 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1811 * struct componentname *a_cnp)
1814 nfs_link(struct vop_old_link_args *ap)
1816 struct vnode *vp = ap->a_vp;
1817 struct vnode *tdvp = ap->a_tdvp;
1818 struct componentname *cnp = ap->a_cnp;
1822 caddr_t bpos, dpos, cp2;
1823 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1824 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1827 if (vp->v_mount != tdvp->v_mount) {
1832 * Push all writes to the server, so that the attribute cache
1833 * doesn't get "out of sync" with the server.
1834 * XXX There should be a better way!
1836 VOP_FSYNC(vp, MNT_WAIT);
1839 nfsstats.rpccnt[NFSPROC_LINK]++;
1840 nfsm_reqhead(vp, NFSPROC_LINK,
1841 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1843 nfsm_fhtom(tdvp, v3);
1844 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1845 nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1847 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1848 nfsm_wcc_data(tdvp, wccflag);
1852 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1854 VTONFS(vp)->n_attrstamp = 0;
1856 VTONFS(tdvp)->n_attrstamp = 0;
1858 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1860 if (error == EEXIST)
1866 * nfs symbolic link create call
1868 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1869 * struct componentname *a_cnp, struct vattr *a_vap,
1873 nfs_symlink(struct vop_old_symlink_args *ap)
1875 struct vnode *dvp = ap->a_dvp;
1876 struct vattr *vap = ap->a_vap;
1877 struct componentname *cnp = ap->a_cnp;
1878 struct nfsv2_sattr *sp;
1882 caddr_t bpos, dpos, cp2;
1883 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1884 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1885 struct vnode *newvp = (struct vnode *)0;
1886 int v3 = NFS_ISV3(dvp);
1888 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1889 slen = strlen(ap->a_target);
1890 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1891 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1892 nfsm_fhtom(dvp, v3);
1893 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1895 nfsm_v3attrbuild(vap, FALSE);
1897 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1899 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1900 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1901 sp->sa_uid = nfs_xdrneg1;
1902 sp->sa_gid = nfs_xdrneg1;
1903 sp->sa_size = nfs_xdrneg1;
1904 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1905 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1909 * Issue the NFS request and get the rpc response.
1911 * Only NFSv3 responses returning an error of 0 actually return
1912 * a file handle that can be converted into newvp without having
1913 * to do an extra lookup rpc.
1915 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1918 nfsm_mtofh(dvp, newvp, v3, gotvp);
1919 nfsm_wcc_data(dvp, wccflag);
1923 * out code jumps -> here, mrep is also freed.
1930 * If we get an EEXIST error, silently convert it to no-error
1931 * in case of an NFS retry.
1933 if (error == EEXIST)
1937 * If we do not have (or no longer have) an error, and we could
1938 * not extract the newvp from the response due to the request being
1939 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1940 * to obtain a newvp to return.
1942 if (error == 0 && newvp == NULL) {
1943 struct nfsnode *np = NULL;
1945 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1946 cnp->cn_cred, cnp->cn_td, &np);
1956 VTONFS(dvp)->n_flag |= NLMODIFIED;
1958 VTONFS(dvp)->n_attrstamp = 0;
1965 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1966 * struct componentname *a_cnp, struct vattr *a_vap)
1969 nfs_mkdir(struct vop_old_mkdir_args *ap)
1971 struct vnode *dvp = ap->a_dvp;
1972 struct vattr *vap = ap->a_vap;
1973 struct componentname *cnp = ap->a_cnp;
1974 struct nfsv2_sattr *sp;
1979 struct nfsnode *np = (struct nfsnode *)0;
1980 struct vnode *newvp = (struct vnode *)0;
1981 caddr_t bpos, dpos, cp2;
1982 int error = 0, wccflag = NFSV3_WCCRATTR;
1984 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1986 int v3 = NFS_ISV3(dvp);
1988 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1991 len = cnp->cn_namelen;
1992 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1993 nfsm_reqhead(dvp, NFSPROC_MKDIR,
1994 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1995 nfsm_fhtom(dvp, v3);
1996 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1998 nfsm_v3attrbuild(vap, FALSE);
2000 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2001 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2002 sp->sa_uid = nfs_xdrneg1;
2003 sp->sa_gid = nfs_xdrneg1;
2004 sp->sa_size = nfs_xdrneg1;
2005 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2006 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2008 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
2010 nfsm_mtofh(dvp, newvp, v3, gotvp);
2012 nfsm_wcc_data(dvp, wccflag);
2015 VTONFS(dvp)->n_flag |= NLMODIFIED;
2017 VTONFS(dvp)->n_attrstamp = 0;
2019 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2020 * if we can succeed in looking up the directory.
2022 if (error == EEXIST || (!error && !gotvp)) {
2025 newvp = (struct vnode *)0;
2027 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2031 if (newvp->v_type != VDIR)
2044 * nfs remove directory call
2046 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2047 * struct componentname *a_cnp)
2050 nfs_rmdir(struct vop_old_rmdir_args *ap)
2052 struct vnode *vp = ap->a_vp;
2053 struct vnode *dvp = ap->a_dvp;
2054 struct componentname *cnp = ap->a_cnp;
2058 caddr_t bpos, dpos, cp2;
2059 int error = 0, wccflag = NFSV3_WCCRATTR;
2060 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2061 int v3 = NFS_ISV3(dvp);
2065 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2066 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2067 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2068 nfsm_fhtom(dvp, v3);
2069 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2070 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2072 nfsm_wcc_data(dvp, wccflag);
2075 VTONFS(dvp)->n_flag |= NLMODIFIED;
2077 VTONFS(dvp)->n_attrstamp = 0;
2079 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2081 if (error == ENOENT)
2089 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2092 nfs_readdir(struct vop_readdir_args *ap)
2094 struct vnode *vp = ap->a_vp;
2095 struct nfsnode *np = VTONFS(vp);
2096 struct uio *uio = ap->a_uio;
2100 if (vp->v_type != VDIR)
2104 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2105 * and then check that is still valid, or if this is an NQNFS mount
2106 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2107 * VOP_GETATTR() does not necessarily go to the wire.
2109 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2110 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2111 if (VOP_GETATTR(vp, &vattr) == 0 &&
2112 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2114 nfsstats.direofcache_hits++;
2120 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2121 * own cache coherency checks so we do not have to.
2123 tresid = uio->uio_resid;
2124 error = nfs_bioread(vp, uio, 0);
2126 if (!error && uio->uio_resid == tresid)
2127 nfsstats.direofcache_misses++;
2132 * Readdir rpc call. nfs_bioread->nfs_doio->nfs_readdirrpc.
2134 * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2135 * offset/block and converts the nfs formatted directory entries for userland
2136 * consumption as well as deals with offsets into the middle of blocks.
2137 * nfs_doio only deals with logical blocks. In particular, uio_offset will
2138 * be block-bounded. It must convert to cookies for the actual RPC.
2141 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2144 struct nfs_dirent *dp = NULL;
2149 caddr_t bpos, dpos, cp2;
2150 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2152 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2153 struct nfsnode *dnp = VTONFS(vp);
2155 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2157 int v3 = NFS_ISV3(vp);
2160 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2161 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2162 panic("nfs readdirrpc bad uio");
2166 * If there is no cookie, assume directory was stale.
2168 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2172 return (NFSERR_BAD_COOKIE);
2174 * Loop around doing readdir rpc's of size nm_readdirsize
2175 * truncated to a multiple of DIRBLKSIZ.
2176 * The stopping criteria is EOF or buffer full.
2178 while (more_dirs && bigenough) {
2179 nfsstats.rpccnt[NFSPROC_READDIR]++;
2180 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2184 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2185 *tl++ = cookie.nfsuquad[0];
2186 *tl++ = cookie.nfsuquad[1];
2187 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2188 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2190 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2191 *tl++ = cookie.nfsuquad[0];
2193 *tl = txdr_unsigned(nmp->nm_readdirsize);
2194 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2196 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2198 nfsm_dissect(tl, u_int32_t *,
2200 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2201 dnp->n_cookieverf.nfsuquad[1] = *tl;
2207 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2208 more_dirs = fxdr_unsigned(int, *tl);
2210 /* loop thru the dir entries, converting them to std form */
2211 while (more_dirs && bigenough) {
2213 nfsm_dissect(tl, u_int32_t *,
2215 fileno = fxdr_hyper(tl);
2216 len = fxdr_unsigned(int, *(tl + 2));
2218 nfsm_dissect(tl, u_int32_t *,
2220 fileno = fxdr_unsigned(u_quad_t, *tl++);
2221 len = fxdr_unsigned(int, *tl);
2223 if (len <= 0 || len > NFS_MAXNAMLEN) {
2230 * len is the number of bytes in the path element
2231 * name, not including the \0 termination.
2233 * tlen is the number of bytes w have to reserve for
2234 * the path element name.
2236 tlen = nfsm_rndup(len);
2238 tlen += 4; /* To ensure null termination */
2241 * If the entry would cross a DIRBLKSIZ boundary,
2242 * extend the previous nfs_dirent to cover the
2245 left = DIRBLKSIZ - blksiz;
2246 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2247 dp->nfs_reclen += left;
2248 uiop->uio_iov->iov_base += left;
2249 uiop->uio_iov->iov_len -= left;
2250 uiop->uio_offset += left;
2251 uiop->uio_resid -= left;
2254 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2257 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2258 dp->nfs_ino = fileno;
2259 dp->nfs_namlen = len;
2260 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2261 dp->nfs_type = DT_UNKNOWN;
2262 blksiz += dp->nfs_reclen;
2263 if (blksiz == DIRBLKSIZ)
2265 uiop->uio_offset += sizeof(struct nfs_dirent);
2266 uiop->uio_resid -= sizeof(struct nfs_dirent);
2267 uiop->uio_iov->iov_base += sizeof(struct nfs_dirent);
2268 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2269 nfsm_mtouio(uiop, len);
2272 * The uiop has advanced by nfs_dirent + len
2273 * but really needs to advance by
2276 cp = uiop->uio_iov->iov_base;
2278 *cp = '\0'; /* null terminate */
2279 uiop->uio_iov->iov_base += tlen;
2280 uiop->uio_iov->iov_len -= tlen;
2281 uiop->uio_offset += tlen;
2282 uiop->uio_resid -= tlen;
2285 * NFS strings must be rounded up (nfsm_myouio
2286 * handled that in the bigenough case).
2288 nfsm_adv(nfsm_rndup(len));
2291 nfsm_dissect(tl, u_int32_t *,
2294 nfsm_dissect(tl, u_int32_t *,
2299 * If we were able to accomodate the last entry,
2300 * get the cookie for the next one. Otherwise
2301 * hold-over the cookie for the one we were not
2302 * able to accomodate.
2305 cookie.nfsuquad[0] = *tl++;
2307 cookie.nfsuquad[1] = *tl++;
2313 more_dirs = fxdr_unsigned(int, *tl);
2316 * If at end of rpc data, get the eof boolean
2319 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2320 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2325 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2326 * by increasing d_reclen for the last record.
2329 left = DIRBLKSIZ - blksiz;
2330 dp->nfs_reclen += left;
2331 uiop->uio_iov->iov_base += left;
2332 uiop->uio_iov->iov_len -= left;
2333 uiop->uio_offset += left;
2334 uiop->uio_resid -= left;
2339 * We hit the end of the directory, update direofoffset.
2341 dnp->n_direofoffset = uiop->uio_offset;
2344 * There is more to go, insert the link cookie so the
2345 * next block can be read.
2347 if (uiop->uio_resid > 0)
2348 printf("EEK! readdirrpc resid > 0\n");
2349 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2357 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2360 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2363 struct nfs_dirent *dp;
2367 struct vnode *newvp;
2369 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2370 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2372 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2373 struct nfsnode *dnp = VTONFS(vp), *np;
2376 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2377 int attrflag, fhsize;
2378 struct namecache *ncp;
2379 struct namecache *dncp;
2380 struct nlcomponent nlc;
2386 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2387 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2388 panic("nfs readdirplusrpc bad uio");
2391 * Obtain the namecache record for the directory so we have something
2392 * to use as a basis for creating the entries. This function will
2393 * return a held (but not locked) ncp. The ncp may be disconnected
2394 * from the tree and cannot be used for upward traversals, and the
2395 * ncp may be unnamed. Note that other unrelated operations may
2396 * cause the ncp to be named at any time.
2398 dncp = cache_fromdvp(vp, NULL, 0);
2399 bzero(&nlc, sizeof(nlc));
2403 * If there is no cookie, assume directory was stale.
2405 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2409 return (NFSERR_BAD_COOKIE);
2411 * Loop around doing readdir rpc's of size nm_readdirsize
2412 * truncated to a multiple of DIRBLKSIZ.
2413 * The stopping criteria is EOF or buffer full.
2415 while (more_dirs && bigenough) {
2416 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2417 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2418 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2420 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2421 *tl++ = cookie.nfsuquad[0];
2422 *tl++ = cookie.nfsuquad[1];
2423 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2424 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2425 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2426 *tl = txdr_unsigned(nmp->nm_rsize);
2427 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2428 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2433 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2434 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2435 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2436 more_dirs = fxdr_unsigned(int, *tl);
2438 /* loop thru the dir entries, doctoring them to 4bsd form */
2439 while (more_dirs && bigenough) {
2440 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2441 fileno = fxdr_hyper(tl);
2442 len = fxdr_unsigned(int, *(tl + 2));
2443 if (len <= 0 || len > NFS_MAXNAMLEN) {
2448 tlen = nfsm_rndup(len);
2450 tlen += 4; /* To ensure null termination*/
2451 left = DIRBLKSIZ - blksiz;
2452 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2453 dp->nfs_reclen += left;
2454 uiop->uio_iov->iov_base += left;
2455 uiop->uio_iov->iov_len -= left;
2456 uiop->uio_offset += left;
2457 uiop->uio_resid -= left;
2460 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2463 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2464 dp->nfs_ino = fileno;
2465 dp->nfs_namlen = len;
2466 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2467 dp->nfs_type = DT_UNKNOWN;
2468 blksiz += dp->nfs_reclen;
2469 if (blksiz == DIRBLKSIZ)
2471 uiop->uio_offset += sizeof(struct nfs_dirent);
2472 uiop->uio_resid -= sizeof(struct nfs_dirent);
2473 uiop->uio_iov->iov_base += sizeof(struct nfs_dirent);
2474 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2475 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2476 nlc.nlc_namelen = len;
2477 nfsm_mtouio(uiop, len);
2478 cp = uiop->uio_iov->iov_base;
2481 uiop->uio_iov->iov_base += tlen;
2482 uiop->uio_iov->iov_len -= tlen;
2483 uiop->uio_offset += tlen;
2484 uiop->uio_resid -= tlen;
2486 nfsm_adv(nfsm_rndup(len));
2487 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2489 cookie.nfsuquad[0] = *tl++;
2490 cookie.nfsuquad[1] = *tl++;
2495 * Since the attributes are before the file handle
2496 * (sigh), we must skip over the attributes and then
2497 * come back and get them.
2499 attrflag = fxdr_unsigned(int, *tl);
2503 nfsm_adv(NFSX_V3FATTR);
2504 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2505 doit = fxdr_unsigned(int, *tl);
2507 nfsm_getfh(fhp, fhsize, 1);
2508 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2513 error = nfs_nget(vp->v_mount, fhp,
2521 if (doit && bigenough) {
2526 nfsm_loadattr(newvp, (struct vattr *)0);
2530 IFTODT(VTTOIF(np->n_vattr.va_type));
2532 printf("NFS/READDIRPLUS, ENTER %*.*s\n",
2533 nlc.nlc_namelen, nlc.nlc_namelen,
2535 ncp = cache_nlookup(dncp, &nlc);
2536 cache_setunresolved(ncp);
2537 cache_setvp(ncp, newvp);
2540 printf("NFS/READDIRPLUS, UNABLE TO ENTER"
2542 nlc.nlc_namelen, nlc.nlc_namelen,
2547 /* Just skip over the file handle */
2548 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2549 i = fxdr_unsigned(int, *tl);
2550 nfsm_adv(nfsm_rndup(i));
2552 if (newvp != NULLVP) {
2559 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2560 more_dirs = fxdr_unsigned(int, *tl);
2563 * If at end of rpc data, get the eof boolean
2566 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2567 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2572 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2573 * by increasing d_reclen for the last record.
2576 left = DIRBLKSIZ - blksiz;
2577 dp->nfs_reclen += left;
2578 uiop->uio_iov->iov_base += left;
2579 uiop->uio_iov->iov_len -= left;
2580 uiop->uio_offset += left;
2581 uiop->uio_resid -= left;
2585 * We are now either at the end of the directory or have filled the
2589 dnp->n_direofoffset = uiop->uio_offset;
2591 if (uiop->uio_resid > 0)
2592 printf("EEK! readdirplusrpc resid > 0\n");
2593 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2597 if (newvp != NULLVP) {
2610 * Silly rename. To make the NFS filesystem that is stateless look a little
2611 * more like the "ufs" a remove of an active vnode is translated to a rename
2612 * to a funny looking filename that is removed by nfs_inactive on the
2613 * nfsnode. There is the potential for another process on a different client
2614 * to create the same funny name between the nfs_lookitup() fails and the
2615 * nfs_rename() completes, but...
2618 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2620 struct sillyrename *sp;
2625 * We previously purged dvp instead of vp. I don't know why, it
2626 * completely destroys performance. We can't do it anyway with the
2627 * new VFS API since we would be breaking the namecache topology.
2629 cache_purge(vp); /* XXX */
2632 if (vp->v_type == VDIR)
2633 panic("nfs: sillyrename dir");
2635 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2636 M_NFSREQ, M_WAITOK);
2637 sp->s_cred = crdup(cnp->cn_cred);
2641 /* Fudge together a funny name */
2642 sp->s_namlen = sprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
2644 /* Try lookitups until we get one that isn't there */
2645 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2646 cnp->cn_td, (struct nfsnode **)0) == 0) {
2648 if (sp->s_name[4] > 'z') {
2653 error = nfs_renameit(dvp, cnp, sp);
2656 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2658 np->n_sillyrename = sp;
2663 free((caddr_t)sp, M_NFSREQ);
2668 * Look up a file name and optionally either update the file handle or
2669 * allocate an nfsnode, depending on the value of npp.
2670 * npp == NULL --> just do the lookup
2671 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2673 * *npp != NULL --> update the file handle in the vnode
2676 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2677 struct thread *td, struct nfsnode **npp)
2682 struct vnode *newvp = (struct vnode *)0;
2683 struct nfsnode *np, *dnp = VTONFS(dvp);
2684 caddr_t bpos, dpos, cp2;
2685 int error = 0, fhlen, attrflag;
2686 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2688 int v3 = NFS_ISV3(dvp);
2690 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2691 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2692 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2693 nfsm_fhtom(dvp, v3);
2694 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2695 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2696 if (npp && !error) {
2697 nfsm_getfh(nfhp, fhlen, v3);
2700 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2701 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2702 np->n_fhp = &np->n_fh;
2703 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2704 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2705 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2706 np->n_fhsize = fhlen;
2708 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2712 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2720 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
2721 if (!attrflag && *npp == NULL) {
2730 nfsm_loadattr(newvp, (struct vattr *)0);
2734 if (npp && *npp == NULL) {
2749 * Nfs Version 3 commit rpc
2752 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2757 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2758 caddr_t bpos, dpos, cp2;
2759 int error = 0, wccflag = NFSV3_WCCRATTR;
2760 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2762 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2764 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2765 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2767 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2768 txdr_hyper(offset, tl);
2770 *tl = txdr_unsigned(cnt);
2771 nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2772 nfsm_wcc_data(vp, wccflag);
2774 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2775 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2776 NFSX_V3WRITEVERF)) {
2777 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2779 error = NFSERR_STALEWRITEVERF;
2789 * - make nfs_bmap() essentially a no-op that does no translation
2790 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2791 * (Maybe I could use the process's page mapping, but I was concerned that
2792 * Kernel Write might not be enabled and also figured copyout() would do
2793 * a lot more work than bcopy() and also it currently happens in the
2794 * context of the swapper process (2).
2796 * nfs_bmap(struct vnode *a_vp, off_t a_loffset, struct vnode **a_vpp,
2797 * off_t *a_doffsetp, int *a_runp, int *a_runb)
2800 nfs_bmap(struct vop_bmap_args *ap)
2802 struct vnode *vp = ap->a_vp;
2804 if (ap->a_vpp != NULL)
2806 if (ap->a_doffsetp != NULL)
2807 *ap->a_doffsetp = ap->a_loffset;
2808 if (ap->a_runp != NULL)
2810 if (ap->a_runb != NULL)
2818 * For async requests when nfsiod(s) are running, queue the request by
2819 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2823 nfs_strategy(struct vop_strategy_args *ap)
2825 struct bio *bio = ap->a_bio;
2827 struct buf *bp = bio->bio_buf;
2831 KASSERT(bp->b_cmd != BUF_CMD_DONE,
2832 ("nfs_strategy: buffer %p unexpectedly marked done", bp));
2833 KASSERT(BUF_REFCNT(bp) > 0,
2834 ("nfs_strategy: buffer %p not locked", bp));
2836 if (bp->b_flags & B_ASYNC)
2839 td = curthread; /* XXX */
2842 * We probably don't need to push an nbio any more since no
2843 * block conversion is required due to the use of 64 bit byte
2844 * offsets, but do it anyway.
2846 nbio = push_bio(bio);
2847 nbio->bio_offset = bio->bio_offset;
2850 * If the op is asynchronous and an i/o daemon is waiting
2851 * queue the request, wake it up and wait for completion
2852 * otherwise just do it ourselves.
2854 if ((bp->b_flags & B_ASYNC) == 0 || nfs_asyncio(ap->a_vp, nbio, td))
2855 error = nfs_doio(ap->a_vp, nbio, td);
2862 * NB Currently unsupported.
2864 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
2865 * struct thread *a_td)
2869 nfs_mmap(struct vop_mmap_args *ap)
2875 * fsync vnode op. Just call nfs_flush() with commit == 1.
2877 * nfs_fsync(struct vnode *a_vp, struct ucred * a_cred, int a_waitfor,
2878 * struct thread *a_td)
2882 nfs_fsync(struct vop_fsync_args *ap)
2884 return (nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1));
2888 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
2889 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
2890 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
2891 * set the buffer contains data that has already been written to the server
2892 * and which now needs a commit RPC.
2894 * If commit is 0 we only take one pass and only flush buffers containing new
2897 * If commit is 1 we take two passes, issuing a commit RPC in the second
2900 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
2901 * to completely flush all pending data.
2903 * Note that the RB_SCAN code properly handles the case where the
2904 * callback might block and directly or indirectly (another thread) cause
2905 * the RB tree to change.
2908 #ifndef NFS_COMMITBVECSIZ
2909 #define NFS_COMMITBVECSIZ 16
2912 struct nfs_flush_info {
2913 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
2920 struct buf *bvary[NFS_COMMITBVECSIZ];
2926 static int nfs_flush_bp(struct buf *bp, void *data);
2927 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
2930 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
2932 struct nfsnode *np = VTONFS(vp);
2933 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2934 struct nfs_flush_info info;
2937 bzero(&info, sizeof(info));
2940 info.waitfor = waitfor;
2941 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
2948 info.mode = NFI_FLUSHNEW;
2949 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2950 nfs_flush_bp, &info);
2953 * Take a second pass if committing and no error occured.
2954 * Clean up any left over collection (whether an error
2957 if (commit && error == 0) {
2958 info.mode = NFI_COMMIT;
2959 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2960 nfs_flush_bp, &info);
2962 error = nfs_flush_docommit(&info, error);
2966 * Wait for pending I/O to complete before checking whether
2967 * any further dirty buffers exist.
2969 while (waitfor == MNT_WAIT && vp->v_track_write.bk_active) {
2970 vp->v_track_write.bk_waitflag = 1;
2971 error = tsleep(&vp->v_track_write,
2972 info.slpflag, "nfsfsync", info.slptimeo);
2975 * We have to be able to break out if this
2976 * is an 'intr' mount.
2978 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
2984 * Since we do not process pending signals,
2985 * once we get a PCATCH our tsleep() will no
2986 * longer sleep, switch to a fixed timeout
2989 if (info.slpflag == PCATCH) {
2991 info.slptimeo = 2 * hz;
2998 * Loop if we are flushing synchronous as well as committing,
2999 * and dirty buffers are still present. Otherwise we might livelock.
3001 } while (waitfor == MNT_WAIT && commit &&
3002 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3005 * The callbacks have to return a negative error to terminate the
3012 * Deal with any error collection
3014 if (np->n_flag & NWRITEERR) {
3015 error = np->n_error;
3016 np->n_flag &= ~NWRITEERR;
3024 nfs_flush_bp(struct buf *bp, void *data)
3026 struct nfs_flush_info *info = data;
3031 switch(info->mode) {
3034 if (info->loops && info->waitfor == MNT_WAIT) {
3035 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3037 int lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3038 if (info->slpflag & PCATCH)
3039 lkflags |= LK_PCATCH;
3040 error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3044 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3047 KKASSERT(bp->b_vp == info->vp);
3049 if ((bp->b_flags & B_DELWRI) == 0)
3050 panic("nfs_fsync: not dirty");
3051 if (bp->b_flags & B_NEEDCOMMIT) {
3058 bp->b_flags |= B_ASYNC;
3068 * Only process buffers in need of a commit which we can
3069 * immediately lock. This may prevent a buffer from being
3070 * committed, but the normal flush loop will block on the
3071 * same buffer so we shouldn't get into an endless loop.
3074 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3075 (B_DELWRI | B_NEEDCOMMIT) ||
3076 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
3081 KKASSERT(bp->b_vp == info->vp);
3085 * NOTE: storing the bp in the bvary[] basically sets
3086 * it up for a commit operation.
3088 * We must call vfs_busy_pages() now so the commit operation
3089 * is interlocked with user modifications to memory mapped
3092 * Note: to avoid loopback deadlocks, we do not
3093 * assign b_runningbufspace.
3095 bp->b_cmd = BUF_CMD_WRITE;
3096 vfs_busy_pages(bp->b_vp, bp);
3097 info->bvary[info->bvsize] = bp;
3098 toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3099 if (info->bvsize == 0 || toff < info->beg_off)
3100 info->beg_off = toff;
3101 toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3102 if (info->bvsize == 0 || toff > info->end_off)
3103 info->end_off = toff;
3105 if (info->bvsize == NFS_COMMITBVECSIZ) {
3106 error = nfs_flush_docommit(info, 0);
3107 KKASSERT(info->bvsize == 0);
3116 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3126 if (info->bvsize > 0) {
3128 * Commit data on the server, as required. Note that
3129 * nfs_commit will use the vnode's cred for the commit.
3130 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3132 bytes = info->end_off - info->beg_off;
3133 if (bytes > 0x40000000)
3138 retv = nfs_commit(vp, info->beg_off,
3139 (int)bytes, info->td);
3140 if (retv == NFSERR_STALEWRITEVERF)
3141 nfs_clearcommit(vp->v_mount);
3145 * Now, either mark the blocks I/O done or mark the
3146 * blocks dirty, depending on whether the commit
3149 for (i = 0; i < info->bvsize; ++i) {
3150 bp = info->bvary[i];
3151 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3154 * Error, leave B_DELWRI intact
3156 vfs_unbusy_pages(bp);
3157 bp->b_cmd = BUF_CMD_DONE;
3161 * Success, remove B_DELWRI ( bundirty() ).
3163 * b_dirtyoff/b_dirtyend seem to be NFS
3164 * specific. We should probably move that
3165 * into bundirty(). XXX
3167 * We are faking an I/O write, we have to
3168 * start the transaction in order to
3169 * immediately biodone() it.
3172 bp->b_flags |= B_ASYNC;
3174 bp->b_flags &= ~B_ERROR;
3175 bp->b_dirtyoff = bp->b_dirtyend = 0;
3177 biodone(&bp->b_bio1);
3186 * NFS advisory byte-level locks.
3187 * Currently unsupported.
3189 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3193 nfs_advlock(struct vop_advlock_args *ap)
3195 struct nfsnode *np = VTONFS(ap->a_vp);
3198 * The following kludge is to allow diskless support to work
3199 * until a real NFS lockd is implemented. Basically, just pretend
3200 * that this is a local lock.
3202 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3206 * Print out the contents of an nfsnode.
3208 * nfs_print(struct vnode *a_vp)
3211 nfs_print(struct vop_print_args *ap)
3213 struct vnode *vp = ap->a_vp;
3214 struct nfsnode *np = VTONFS(vp);
3216 printf("tag VT_NFS, fileid %ld fsid 0x%x",
3217 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3218 if (vp->v_type == VFIFO)
3225 * nfs special file access vnode op.
3226 * Essentially just get vattr and then imitate iaccess() since the device is
3227 * local to the client.
3229 * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
3230 * struct thread *a_td)
3233 nfsspec_access(struct vop_access_args *ap)
3237 struct ucred *cred = ap->a_cred;
3238 struct vnode *vp = ap->a_vp;
3239 mode_t mode = ap->a_mode;
3245 * Disallow write attempts on filesystems mounted read-only;
3246 * unless the file is a socket, fifo, or a block or character
3247 * device resident on the filesystem.
3249 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3250 switch (vp->v_type) {
3260 * If you're the super-user,
3261 * you always get access.
3263 if (cred->cr_uid == 0)
3266 error = VOP_GETATTR(vp, vap);
3270 * Access check is based on only one of owner, group, public.
3271 * If not owner, then check group. If not a member of the
3272 * group, then check public access.
3274 if (cred->cr_uid != vap->va_uid) {
3276 gp = cred->cr_groups;
3277 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3278 if (vap->va_gid == *gp)
3284 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3289 * Read wrapper for special devices.
3291 * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3292 * struct ucred *a_cred)
3295 nfsspec_read(struct vop_read_args *ap)
3297 struct nfsnode *np = VTONFS(ap->a_vp);
3303 getnanotime(&np->n_atim);
3304 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3308 * Write wrapper for special devices.
3310 * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3311 * struct ucred *a_cred)
3314 nfsspec_write(struct vop_write_args *ap)
3316 struct nfsnode *np = VTONFS(ap->a_vp);
3322 getnanotime(&np->n_mtim);
3323 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3327 * Close wrapper for special devices.
3329 * Update the times on the nfsnode then do device close.
3331 * nfsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
3332 * struct thread *a_td)
3335 nfsspec_close(struct vop_close_args *ap)
3337 struct vnode *vp = ap->a_vp;
3338 struct nfsnode *np = VTONFS(vp);
3341 if (np->n_flag & (NACC | NUPD)) {
3343 if (vp->v_usecount == 1 &&
3344 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3346 if (np->n_flag & NACC)
3347 vattr.va_atime = np->n_atim;
3348 if (np->n_flag & NUPD)
3349 vattr.va_mtime = np->n_mtim;
3350 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3353 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3357 * Read wrapper for fifos.
3359 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3360 * struct ucred *a_cred)
3363 nfsfifo_read(struct vop_read_args *ap)
3365 struct nfsnode *np = VTONFS(ap->a_vp);
3371 getnanotime(&np->n_atim);
3372 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3376 * Write wrapper for fifos.
3378 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3379 * struct ucred *a_cred)
3382 nfsfifo_write(struct vop_write_args *ap)
3384 struct nfsnode *np = VTONFS(ap->a_vp);
3390 getnanotime(&np->n_mtim);
3391 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3395 * Close wrapper for fifos.
3397 * Update the times on the nfsnode then do fifo close.
3399 * nfsfifo_close(struct vnode *a_vp, int a_fflag, struct thread *a_td)
3402 nfsfifo_close(struct vop_close_args *ap)
3404 struct vnode *vp = ap->a_vp;
3405 struct nfsnode *np = VTONFS(vp);
3409 if (np->n_flag & (NACC | NUPD)) {
3411 if (np->n_flag & NACC)
3413 if (np->n_flag & NUPD)
3416 if (vp->v_usecount == 1 &&
3417 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3419 if (np->n_flag & NACC)
3420 vattr.va_atime = np->n_atim;
3421 if (np->n_flag & NUPD)
3422 vattr.va_mtime = np->n_mtim;
3423 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3426 return (VOCALL(&fifo_vnode_vops, &ap->a_head));