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.80 2008/10/18 01:13:54 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_old_link = nfs_link,
150 .vop_old_lookup = nfs_lookup,
151 .vop_old_mkdir = nfs_mkdir,
152 .vop_old_mknod = nfs_mknod,
153 .vop_mmap = nfs_mmap,
154 .vop_open = nfs_open,
155 .vop_poll = nfs_poll,
156 .vop_print = nfs_print,
157 .vop_read = nfs_read,
158 .vop_readdir = nfs_readdir,
159 .vop_readlink = nfs_readlink,
160 .vop_reclaim = nfs_reclaim,
161 .vop_old_remove = nfs_remove,
162 .vop_old_rename = nfs_rename,
163 .vop_old_rmdir = nfs_rmdir,
164 .vop_setattr = nfs_setattr,
165 .vop_strategy = nfs_strategy,
166 .vop_old_symlink = nfs_symlink,
167 .vop_write = nfs_write,
168 .vop_nresolve = nfs_nresolve
172 * Special device vnode ops
174 struct vop_ops nfsv2_spec_vops = {
175 .vop_default = spec_vnoperate,
176 .vop_access = nfsspec_access,
177 .vop_close = nfsspec_close,
178 .vop_fsync = nfs_fsync,
179 .vop_getattr = nfs_getattr,
180 .vop_inactive = nfs_inactive,
181 .vop_print = nfs_print,
182 .vop_read = nfsspec_read,
183 .vop_reclaim = nfs_reclaim,
184 .vop_setattr = nfs_setattr,
185 .vop_write = nfsspec_write
188 struct vop_ops nfsv2_fifo_vops = {
189 .vop_default = fifo_vnoperate,
190 .vop_access = nfsspec_access,
191 .vop_close = nfsfifo_close,
192 .vop_fsync = nfs_fsync,
193 .vop_getattr = nfs_getattr,
194 .vop_inactive = nfs_inactive,
195 .vop_print = nfs_print,
196 .vop_read = nfsfifo_read,
197 .vop_reclaim = nfs_reclaim,
198 .vop_setattr = nfs_setattr,
199 .vop_write = nfsfifo_write
202 static int nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
203 struct componentname *cnp,
205 static int nfs_removerpc (struct vnode *dvp, const char *name,
207 struct ucred *cred, struct thread *td);
208 static int nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
209 int fnamelen, struct vnode *tdvp,
210 const char *tnameptr, int tnamelen,
211 struct ucred *cred, struct thread *td);
212 static int nfs_renameit (struct vnode *sdvp,
213 struct componentname *scnp,
214 struct sillyrename *sp);
216 SYSCTL_DECL(_vfs_nfs);
218 static int nfs_flush_on_rename = 1;
219 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_rename, CTLFLAG_RW,
220 &nfs_flush_on_rename, 0, "flush fvp prior to rename");
221 static int nfs_flush_on_hlink = 0;
222 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_hlink, CTLFLAG_RW,
223 &nfs_flush_on_hlink, 0, "flush fvp prior to hard link");
225 static int nfsaccess_cache_timeout = NFS_DEFATTRTIMO;
226 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
227 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
229 static int nfsneg_cache_timeout = NFS_MINATTRTIMO;
230 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
231 &nfsneg_cache_timeout, 0, "NFS NEGATIVE NAMECACHE timeout");
233 static int nfspos_cache_timeout = NFS_MINATTRTIMO;
234 SYSCTL_INT(_vfs_nfs, OID_AUTO, pos_cache_timeout, CTLFLAG_RW,
235 &nfspos_cache_timeout, 0, "NFS POSITIVE NAMECACHE timeout");
237 static int nfsv3_commit_on_close = 0;
238 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
239 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
241 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
242 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
244 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
245 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
248 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
249 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
250 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
252 nfs3_access_otw(struct vnode *vp, int wmode,
253 struct thread *td, struct ucred *cred)
257 int error = 0, attrflag;
259 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
260 caddr_t bpos, dpos, cp2;
264 struct nfsnode *np = VTONFS(vp);
266 nfsstats.rpccnt[NFSPROC_ACCESS]++;
267 nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
269 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
270 *tl = txdr_unsigned(wmode);
271 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
272 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
274 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
275 rmode = fxdr_unsigned(u_int32_t, *tl);
277 np->n_modeuid = cred->cr_uid;
278 np->n_modestamp = mycpu->gd_time_seconds;
286 * nfs access vnode op.
287 * For nfs version 2, just return ok. File accesses may fail later.
288 * For nfs version 3, use the access rpc to check accessibility. If file modes
289 * are changed on the server, accesses might still fail later.
291 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
294 nfs_access(struct vop_access_args *ap)
296 struct vnode *vp = ap->a_vp;
297 thread_t td = curthread;
299 u_int32_t mode, wmode;
300 int v3 = NFS_ISV3(vp);
301 struct nfsnode *np = VTONFS(vp);
304 * Disallow write attempts on filesystems mounted read-only;
305 * unless the file is a socket, fifo, or a block or character
306 * device resident on the filesystem.
308 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
309 switch (vp->v_type) {
319 * For nfs v3, check to see if we have done this recently, and if
320 * so return our cached result instead of making an ACCESS call.
321 * If not, do an access rpc, otherwise you are stuck emulating
322 * ufs_access() locally using the vattr. This may not be correct,
323 * since the server may apply other access criteria such as
324 * client uid-->server uid mapping that we do not know about.
327 if (ap->a_mode & VREAD)
328 mode = NFSV3ACCESS_READ;
331 if (vp->v_type != VDIR) {
332 if (ap->a_mode & VWRITE)
333 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
334 if (ap->a_mode & VEXEC)
335 mode |= NFSV3ACCESS_EXECUTE;
337 if (ap->a_mode & VWRITE)
338 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
340 if (ap->a_mode & VEXEC)
341 mode |= NFSV3ACCESS_LOOKUP;
343 /* XXX safety belt, only make blanket request if caching */
344 if (nfsaccess_cache_timeout > 0) {
345 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
346 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
347 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
353 * Does our cached result allow us to give a definite yes to
356 if (np->n_modestamp &&
357 (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
358 (ap->a_cred->cr_uid == np->n_modeuid) &&
359 ((np->n_mode & mode) == mode)) {
360 nfsstats.accesscache_hits++;
363 * Either a no, or a don't know. Go to the wire.
365 nfsstats.accesscache_misses++;
366 error = nfs3_access_otw(vp, wmode, td, ap->a_cred);
368 if ((np->n_mode & mode) != mode) {
374 if ((error = nfsspec_access(ap)) != 0)
378 * Attempt to prevent a mapped root from accessing a file
379 * which it shouldn't. We try to read a byte from the file
380 * if the user is root and the file is not zero length.
381 * After calling nfsspec_access, we should have the correct
384 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
385 && VTONFS(vp)->n_size > 0) {
392 auio.uio_iov = &aiov;
396 auio.uio_segflg = UIO_SYSSPACE;
397 auio.uio_rw = UIO_READ;
400 if (vp->v_type == VREG) {
401 error = nfs_readrpc(vp, &auio);
402 } else if (vp->v_type == VDIR) {
404 bp = kmalloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
406 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
407 error = nfs_readdirrpc(vp, &auio);
409 } else if (vp->v_type == VLNK) {
410 error = nfs_readlinkrpc(vp, &auio);
417 * [re]record creds for reading and/or writing if access
418 * was granted. Assume the NFS server will grant read access
419 * for execute requests.
422 if ((ap->a_mode & (VREAD|VEXEC)) && ap->a_cred != np->n_rucred) {
425 crfree(np->n_rucred);
426 np->n_rucred = ap->a_cred;
428 if ((ap->a_mode & VWRITE) && ap->a_cred != np->n_wucred) {
431 crfree(np->n_wucred);
432 np->n_wucred = ap->a_cred;
440 * Check to see if the type is ok
441 * and that deletion is not in progress.
442 * For paged in text files, you will need to flush the page cache
443 * if consistency is lost.
445 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
450 nfs_open(struct vop_open_args *ap)
452 struct vnode *vp = ap->a_vp;
453 struct nfsnode *np = VTONFS(vp);
457 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
459 kprintf("open eacces vtyp=%d\n",vp->v_type);
465 * Save valid creds for reading and writing for later RPCs.
467 if ((ap->a_mode & FREAD) && ap->a_cred != np->n_rucred) {
470 crfree(np->n_rucred);
471 np->n_rucred = ap->a_cred;
473 if ((ap->a_mode & FWRITE) && ap->a_cred != np->n_wucred) {
476 crfree(np->n_wucred);
477 np->n_wucred = ap->a_cred;
481 * Clear the attribute cache only if opening with write access. It
482 * is unclear if we should do this at all here, but we certainly
483 * should not clear the cache unconditionally simply because a file
486 if (ap->a_mode & FWRITE)
490 * For normal NFS, reconcile changes made locally verses
491 * changes made remotely. Note that VOP_GETATTR only goes
492 * to the wire if the cached attribute has timed out or been
495 * If local modifications have been made clear the attribute
496 * cache to force an attribute and modified time check. If
497 * GETATTR detects that the file has been changed by someone
498 * other then us it will set NRMODIFIED.
500 * If we are opening a directory and local changes have been
501 * made we have to invalidate the cache in order to ensure
502 * that we get the most up-to-date information from the
505 if (np->n_flag & NLMODIFIED) {
507 if (vp->v_type == VDIR) {
508 error = nfs_vinvalbuf(vp, V_SAVE, 1);
514 error = VOP_GETATTR(vp, &vattr);
517 if (np->n_flag & NRMODIFIED) {
518 if (vp->v_type == VDIR)
520 error = nfs_vinvalbuf(vp, V_SAVE, 1);
523 np->n_flag &= ~NRMODIFIED;
526 return (vop_stdopen(ap));
531 * What an NFS client should do upon close after writing is a debatable issue.
532 * Most NFS clients push delayed writes to the server upon close, basically for
534 * 1 - So that any write errors may be reported back to the client process
535 * doing the close system call. By far the two most likely errors are
536 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
537 * 2 - To put a worst case upper bound on cache inconsistency between
538 * multiple clients for the file.
539 * There is also a consistency problem for Version 2 of the protocol w.r.t.
540 * not being able to tell if other clients are writing a file concurrently,
541 * since there is no way of knowing if the changed modify time in the reply
542 * is only due to the write for this client.
543 * (NFS Version 3 provides weak cache consistency data in the reply that
544 * should be sufficient to detect and handle this case.)
546 * The current code does the following:
547 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
548 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
549 * or commit them (this satisfies 1 and 2 except for the
550 * case where the server crashes after this close but
551 * before the commit RPC, which is felt to be "good
552 * enough". Changing the last argument to nfs_flush() to
553 * a 1 would force a commit operation, if it is felt a
554 * commit is necessary now.
555 * for NQNFS - do nothing now, since 2 is dealt with via leases and
556 * 1 should be dealt with via an fsync() system call for
557 * cases where write errors are important.
559 * nfs_close(struct vnode *a_vp, int a_fflag)
563 nfs_close(struct vop_close_args *ap)
565 struct vnode *vp = ap->a_vp;
566 struct nfsnode *np = VTONFS(vp);
568 thread_t td = curthread;
570 if (vp->v_type == VREG) {
571 if (np->n_flag & NLMODIFIED) {
574 * Under NFSv3 we have dirty buffers to dispose of. We
575 * must flush them to the NFS server. We have the option
576 * of waiting all the way through the commit rpc or just
577 * waiting for the initial write. The default is to only
578 * wait through the initial write so the data is in the
579 * server's cache, which is roughly similar to the state
580 * a standard disk subsystem leaves the file in on close().
582 * We cannot clear the NLMODIFIED bit in np->n_flag due to
583 * potential races with other processes, and certainly
584 * cannot clear it if we don't commit.
586 int cm = nfsv3_commit_on_close ? 1 : 0;
587 error = nfs_flush(vp, MNT_WAIT, td, cm);
588 /* np->n_flag &= ~NLMODIFIED; */
590 error = nfs_vinvalbuf(vp, V_SAVE, 1);
594 if (np->n_flag & NWRITEERR) {
595 np->n_flag &= ~NWRITEERR;
604 * nfs getattr call from vfs.
606 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
609 nfs_getattr(struct vop_getattr_args *ap)
611 struct vnode *vp = ap->a_vp;
612 struct nfsnode *np = VTONFS(vp);
618 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
619 int v3 = NFS_ISV3(vp);
620 thread_t td = curthread;
623 * Update local times for special files.
625 if (np->n_flag & (NACC | NUPD))
628 * First look in the cache.
630 if (nfs_getattrcache(vp, ap->a_vap) == 0)
633 if (v3 && nfsaccess_cache_timeout > 0) {
634 nfsstats.accesscache_misses++;
635 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, nfs_vpcred(vp, ND_CHECK));
636 if (nfs_getattrcache(vp, ap->a_vap) == 0)
640 nfsstats.rpccnt[NFSPROC_GETATTR]++;
641 nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
643 nfsm_request(vp, NFSPROC_GETATTR, td, nfs_vpcred(vp, ND_CHECK));
645 nfsm_loadattr(vp, ap->a_vap);
655 * nfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
658 nfs_setattr(struct vop_setattr_args *ap)
660 struct vnode *vp = ap->a_vp;
661 struct nfsnode *np = VTONFS(vp);
662 struct vattr *vap = ap->a_vap;
665 thread_t td = curthread;
672 * Setting of flags is not supported.
674 if (vap->va_flags != VNOVAL)
678 * Disallow write attempts if the filesystem is mounted read-only.
680 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
681 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
682 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
683 (vp->v_mount->mnt_flag & MNT_RDONLY))
686 if (vap->va_size != VNOVAL) {
688 * truncation requested
690 switch (vp->v_type) {
697 if (vap->va_mtime.tv_sec == VNOVAL &&
698 vap->va_atime.tv_sec == VNOVAL &&
699 vap->va_mode == (mode_t)VNOVAL &&
700 vap->va_uid == (uid_t)VNOVAL &&
701 vap->va_gid == (gid_t)VNOVAL)
703 vap->va_size = VNOVAL;
707 * Disallow write attempts if the filesystem is
710 if (vp->v_mount->mnt_flag & MNT_RDONLY)
714 * This is nasty. The RPCs we send to flush pending
715 * data often return attribute information which is
716 * cached via a callback to nfs_loadattrcache(), which
717 * has the effect of changing our notion of the file
718 * size. Due to flushed appends and other operations
719 * the file size can be set to virtually anything,
720 * including values that do not match either the old
721 * or intended file size.
723 * When this condition is detected we must loop to
724 * try the operation again. Hopefully no more
725 * flushing is required on the loop so it works the
726 * second time around. THIS CASE ALMOST ALWAYS
731 error = nfs_meta_setsize(vp, td, vap->va_size);
733 if (np->n_flag & NLMODIFIED) {
734 if (vap->va_size == 0)
735 error = nfs_vinvalbuf(vp, 0, 1);
737 error = nfs_vinvalbuf(vp, V_SAVE, 1);
740 * note: this loop case almost always happens at
741 * least once per truncation.
743 if (error == 0 && np->n_size != vap->va_size)
745 np->n_vattr.va_size = vap->va_size;
748 } else if ((np->n_flag & NLMODIFIED) && vp->v_type == VREG) {
750 * What to do. If we are modifying the mtime we lose
751 * mtime detection of changes made by the server or other
752 * clients. But programs like rsync/rdist/cpdup are going
753 * to call utimes a lot. We don't want to piecemeal sync.
755 * For now sync if any prior remote changes were detected,
756 * but allow us to lose track of remote changes made during
757 * the utimes operation.
759 if (np->n_flag & NRMODIFIED)
760 error = nfs_vinvalbuf(vp, V_SAVE, 1);
764 if (vap->va_mtime.tv_sec != VNOVAL) {
765 np->n_mtime = vap->va_mtime.tv_sec;
769 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
772 * Sanity check if a truncation was issued. This should only occur
773 * if multiple processes are racing on the same file.
775 if (error == 0 && vap->va_size != VNOVAL &&
776 np->n_size != vap->va_size) {
777 kprintf("NFS ftruncate: server disagrees on the file size: "
780 (long long)vap->va_size,
781 (long long)np->n_size);
784 if (error && vap->va_size != VNOVAL) {
785 np->n_size = np->n_vattr.va_size = tsize;
786 vnode_pager_setsize(vp, np->n_size);
792 * Do an nfs setattr rpc.
795 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
796 struct ucred *cred, struct thread *td)
798 struct nfsv2_sattr *sp;
799 struct nfsnode *np = VTONFS(vp);
802 caddr_t bpos, dpos, cp2;
804 int error = 0, wccflag = NFSV3_WCCRATTR;
805 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
806 int v3 = NFS_ISV3(vp);
808 nfsstats.rpccnt[NFSPROC_SETATTR]++;
809 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
812 nfsm_v3attrbuild(vap, TRUE);
813 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
816 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
817 if (vap->va_mode == (mode_t)VNOVAL)
818 sp->sa_mode = nfs_xdrneg1;
820 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
821 if (vap->va_uid == (uid_t)VNOVAL)
822 sp->sa_uid = nfs_xdrneg1;
824 sp->sa_uid = txdr_unsigned(vap->va_uid);
825 if (vap->va_gid == (gid_t)VNOVAL)
826 sp->sa_gid = nfs_xdrneg1;
828 sp->sa_gid = txdr_unsigned(vap->va_gid);
829 sp->sa_size = txdr_unsigned(vap->va_size);
830 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
831 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
833 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
836 nfsm_wcc_data(vp, wccflag);
838 nfsm_loadattr(vp, NULL);
846 nfs_cache_setvp(struct nchandle *nch, struct vnode *vp, int nctimeout)
852 cache_setvp(nch, vp);
853 cache_settimeout(nch, nctimeout);
857 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
858 * nfs_lookup() until all remaining new api calls are implemented.
860 * Resolve a namecache entry. This function is passed a locked ncp and
861 * must call nfs_cache_setvp() on it as appropriate to resolve the entry.
864 nfs_nresolve(struct vop_nresolve_args *ap)
866 struct thread *td = curthread;
867 struct namecache *ncp;
878 /******NFSM MACROS********/
879 struct mbuf *mb, *mrep, *mreq, *mb2, *md;
880 caddr_t bpos, dpos, cp, cp2;
887 if ((error = vget(dvp, LK_SHARED)) != 0)
892 nfsstats.lookupcache_misses++;
893 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
894 ncp = ap->a_nch->ncp;
896 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
897 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
899 nfsm_strtom(ncp->nc_name, len, NFS_MAXNAMLEN);
900 nfsm_request(dvp, NFSPROC_LOOKUP, td, ap->a_cred);
903 * Cache negatve lookups to reduce NFS traffic, but use
904 * a fast timeout. Otherwise use a timeout of 1 tick.
905 * XXX we should add a namecache flag for no-caching
906 * to uncache the negative hit as soon as possible, but
907 * we cannot simply destroy the entry because it is used
908 * as a placeholder by the caller.
911 nfs_cache_setvp(ap->a_nch, NULL, nfsneg_cache_timeout);
912 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
918 * Success, get the file handle, do various checks, and load
919 * post-operation data from the reply packet. Theoretically
920 * we should never be looking up "." so, theoretically, we
921 * should never get the same file handle as our directory. But
922 * we check anyway. XXX
924 * Note that no timeout is set for the positive cache hit. We
925 * assume, theoretically, that ESTALE returns will be dealt with
926 * properly to handle NFS races and in anycase we cannot depend
927 * on a timeout to deal with NFS open/create/excl issues so instead
928 * of a bad hack here the rest of the NFS client code needs to do
931 nfsm_getfh(fhp, fhsize, v3);
934 if (NFS_CMPFH(np, fhp, fhsize)) {
938 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
947 nfsm_postop_attr(nvp, attrflag, NFS_LATTR_NOSHRINK);
948 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
950 nfsm_loadattr(nvp, NULL);
952 nfs_cache_setvp(ap->a_nch, nvp, nfspos_cache_timeout);
966 * 'cached' nfs directory lookup
968 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
970 * nfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
971 * struct componentname *a_cnp)
974 nfs_lookup(struct vop_old_lookup_args *ap)
976 struct componentname *cnp = ap->a_cnp;
977 struct vnode *dvp = ap->a_dvp;
978 struct vnode **vpp = ap->a_vpp;
979 int flags = cnp->cn_flags;
984 struct nfsmount *nmp;
985 caddr_t bpos, dpos, cp2;
986 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
990 int lockparent, wantparent, error = 0, attrflag, fhsize;
991 int v3 = NFS_ISV3(dvp);
994 * Read-only mount check and directory check.
997 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
998 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
1001 if (dvp->v_type != VDIR)
1005 * Look it up in the cache. Note that ENOENT is only returned if we
1006 * previously entered a negative hit (see later on). The additional
1007 * nfsneg_cache_timeout check causes previously cached results to
1008 * be instantly ignored if the negative caching is turned off.
1010 lockparent = flags & CNP_LOCKPARENT;
1011 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1012 nmp = VFSTONFS(dvp->v_mount);
1020 nfsstats.lookupcache_misses++;
1021 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1022 len = cnp->cn_namelen;
1023 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
1024 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1025 nfsm_fhtom(dvp, v3);
1026 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1027 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
1029 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1033 nfsm_getfh(fhp, fhsize, v3);
1036 * Handle RENAME case...
1038 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1039 if (NFS_CMPFH(np, fhp, fhsize)) {
1043 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1050 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1051 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1053 nfsm_loadattr(newvp, NULL);
1058 cnp->cn_flags |= CNP_PDIRUNLOCK;
1063 if (flags & CNP_ISDOTDOT) {
1065 cnp->cn_flags |= CNP_PDIRUNLOCK;
1066 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1068 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1069 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1070 return (error); /* NOTE: return error from nget */
1074 error = vn_lock(dvp, LK_EXCLUSIVE);
1079 cnp->cn_flags |= CNP_PDIRUNLOCK;
1081 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1085 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1092 cnp->cn_flags |= CNP_PDIRUNLOCK;
1097 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1098 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1100 nfsm_loadattr(newvp, NULL);
1102 /* XXX MOVE TO nfs_nremove() */
1103 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1104 cnp->cn_nameiop != NAMEI_DELETE) {
1105 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1112 if (newvp != NULLVP) {
1116 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1117 cnp->cn_nameiop == NAMEI_RENAME) &&
1121 cnp->cn_flags |= CNP_PDIRUNLOCK;
1123 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1126 error = EJUSTRETURN;
1134 * Just call nfs_bioread() to do the work.
1136 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1137 * struct ucred *a_cred)
1140 nfs_read(struct vop_read_args *ap)
1142 struct vnode *vp = ap->a_vp;
1144 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1150 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1153 nfs_readlink(struct vop_readlink_args *ap)
1155 struct vnode *vp = ap->a_vp;
1157 if (vp->v_type != VLNK)
1159 return (nfs_bioread(vp, ap->a_uio, 0));
1163 * Do a readlink rpc.
1164 * Called by nfs_doio() from below the buffer cache.
1167 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1172 caddr_t bpos, dpos, cp2;
1173 int error = 0, len, attrflag;
1174 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1175 int v3 = NFS_ISV3(vp);
1177 nfsstats.rpccnt[NFSPROC_READLINK]++;
1178 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1180 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1182 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1184 nfsm_strsiz(len, NFS_MAXPATHLEN);
1185 if (len == NFS_MAXPATHLEN) {
1186 struct nfsnode *np = VTONFS(vp);
1187 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1190 nfsm_mtouio(uiop, len);
1202 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1207 caddr_t bpos, dpos, cp2;
1208 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1209 struct nfsmount *nmp;
1210 int error = 0, len, retlen, tsiz, eof, attrflag;
1211 int v3 = NFS_ISV3(vp);
1216 nmp = VFSTONFS(vp->v_mount);
1217 tsiz = uiop->uio_resid;
1218 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1221 nfsstats.rpccnt[NFSPROC_READ]++;
1222 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1223 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1225 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1227 txdr_hyper(uiop->uio_offset, tl);
1228 *(tl + 2) = txdr_unsigned(len);
1230 *tl++ = txdr_unsigned(uiop->uio_offset);
1231 *tl++ = txdr_unsigned(len);
1234 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1236 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1241 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1242 eof = fxdr_unsigned(int, *(tl + 1));
1244 nfsm_loadattr(vp, NULL);
1245 nfsm_strsiz(retlen, nmp->nm_rsize);
1246 nfsm_mtouio(uiop, retlen);
1250 if (eof || retlen == 0) {
1253 } else if (retlen < len) {
1265 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, int *must_commit)
1269 int32_t t1, t2, backup;
1270 caddr_t bpos, dpos, cp2;
1271 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1272 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1273 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1274 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1277 if (uiop->uio_iovcnt != 1)
1278 panic("nfs: writerpc iovcnt > 1");
1281 tsiz = uiop->uio_resid;
1282 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1285 nfsstats.rpccnt[NFSPROC_WRITE]++;
1286 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1287 nfsm_reqhead(vp, NFSPROC_WRITE,
1288 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1291 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1292 txdr_hyper(uiop->uio_offset, tl);
1294 *tl++ = txdr_unsigned(len);
1295 *tl++ = txdr_unsigned(*iomode);
1296 *tl = txdr_unsigned(len);
1300 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1301 /* Set both "begin" and "current" to non-garbage. */
1302 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1303 *tl++ = x; /* "begin offset" */
1304 *tl++ = x; /* "current offset" */
1305 x = txdr_unsigned(len);
1306 *tl++ = x; /* total to this offset */
1307 *tl = x; /* size of this write */
1309 nfsm_uiotom(uiop, len);
1310 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1313 * The write RPC returns a before and after mtime. The
1314 * nfsm_wcc_data() macro checks the before n_mtime
1315 * against the before time and stores the after time
1316 * in the nfsnode's cached vattr and n_mtime field.
1317 * The NRMODIFIED bit will be set if the before
1318 * time did not match the original mtime.
1320 wccflag = NFSV3_WCCCHK;
1321 nfsm_wcc_data(vp, wccflag);
1323 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1324 + NFSX_V3WRITEVERF);
1325 rlen = fxdr_unsigned(int, *tl++);
1330 } else if (rlen < len) {
1331 backup = len - rlen;
1332 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1333 uiop->uio_iov->iov_len += backup;
1334 uiop->uio_offset -= backup;
1335 uiop->uio_resid += backup;
1338 commit = fxdr_unsigned(int, *tl++);
1341 * Return the lowest committment level
1342 * obtained by any of the RPCs.
1344 if (committed == NFSV3WRITE_FILESYNC)
1346 else if (committed == NFSV3WRITE_DATASYNC &&
1347 commit == NFSV3WRITE_UNSTABLE)
1349 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1350 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1352 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1353 } else if (bcmp((caddr_t)tl,
1354 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1356 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1361 nfsm_loadattr(vp, NULL);
1369 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1370 committed = NFSV3WRITE_FILESYNC;
1371 *iomode = committed;
1373 uiop->uio_resid = tsiz;
1379 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1380 * mode set to specify the file type and the size field for rdev.
1383 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1386 struct nfsv2_sattr *sp;
1390 struct vnode *newvp = NULL;
1391 struct nfsnode *np = NULL;
1395 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1396 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1398 int v3 = NFS_ISV3(dvp);
1400 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1401 rmajor = txdr_unsigned(vap->va_rmajor);
1402 rminor = txdr_unsigned(vap->va_rminor);
1403 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) {
1404 rmajor = nfs_xdrneg1;
1405 rminor = nfs_xdrneg1;
1407 return (EOPNOTSUPP);
1409 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1412 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1413 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1414 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1415 nfsm_fhtom(dvp, v3);
1416 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1418 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1419 *tl++ = vtonfsv3_type(vap->va_type);
1420 nfsm_v3attrbuild(vap, FALSE);
1421 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1422 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1423 *tl++ = txdr_unsigned(vap->va_rmajor);
1424 *tl = txdr_unsigned(vap->va_rminor);
1427 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1428 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1429 sp->sa_uid = nfs_xdrneg1;
1430 sp->sa_gid = nfs_xdrneg1;
1431 sp->sa_size = makeudev(rmajor, rminor);
1432 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1433 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1435 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1437 nfsm_mtofh(dvp, newvp, v3, gotvp);
1443 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1444 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1450 nfsm_wcc_data(dvp, wccflag);
1459 VTONFS(dvp)->n_flag |= NLMODIFIED;
1461 VTONFS(dvp)->n_attrstamp = 0;
1467 * just call nfs_mknodrpc() to do the work.
1469 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1470 * struct componentname *a_cnp, struct vattr *a_vap)
1474 nfs_mknod(struct vop_old_mknod_args *ap)
1476 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1479 static u_long create_verf;
1481 * nfs file create call
1483 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1484 * struct componentname *a_cnp, struct vattr *a_vap)
1487 nfs_create(struct vop_old_create_args *ap)
1489 struct vnode *dvp = ap->a_dvp;
1490 struct vattr *vap = ap->a_vap;
1491 struct componentname *cnp = ap->a_cnp;
1492 struct nfsv2_sattr *sp;
1496 struct nfsnode *np = NULL;
1497 struct vnode *newvp = NULL;
1498 caddr_t bpos, dpos, cp2;
1499 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1500 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1502 int v3 = NFS_ISV3(dvp);
1505 * Oops, not for me..
1507 if (vap->va_type == VSOCK)
1508 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1510 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1513 if (vap->va_vaflags & VA_EXCLUSIVE)
1516 nfsstats.rpccnt[NFSPROC_CREATE]++;
1517 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1518 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1519 nfsm_fhtom(dvp, v3);
1520 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1522 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1523 if (fmode & O_EXCL) {
1524 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1525 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1527 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
1528 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia)->sin_addr.s_addr;
1531 *tl++ = create_verf;
1532 *tl = ++create_verf;
1534 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1535 nfsm_v3attrbuild(vap, FALSE);
1538 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1539 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1540 sp->sa_uid = nfs_xdrneg1;
1541 sp->sa_gid = nfs_xdrneg1;
1543 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1544 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1546 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1548 nfsm_mtofh(dvp, newvp, v3, gotvp);
1554 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1555 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1561 nfsm_wcc_data(dvp, wccflag);
1565 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1566 KKASSERT(newvp == NULL);
1570 } else if (v3 && (fmode & O_EXCL)) {
1572 * We are normally called with only a partially initialized
1573 * VAP. Since the NFSv3 spec says that server may use the
1574 * file attributes to store the verifier, the spec requires
1575 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1576 * in atime, but we can't really assume that all servers will
1577 * so we ensure that our SETATTR sets both atime and mtime.
1579 if (vap->va_mtime.tv_sec == VNOVAL)
1580 vfs_timestamp(&vap->va_mtime);
1581 if (vap->va_atime.tv_sec == VNOVAL)
1582 vap->va_atime = vap->va_mtime;
1583 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1587 * The new np may have enough info for access
1588 * checks, make sure rucred and wucred are
1589 * initialized for read and write rpc's.
1592 if (np->n_rucred == NULL)
1593 np->n_rucred = crhold(cnp->cn_cred);
1594 if (np->n_wucred == NULL)
1595 np->n_wucred = crhold(cnp->cn_cred);
1600 VTONFS(dvp)->n_flag |= NLMODIFIED;
1602 VTONFS(dvp)->n_attrstamp = 0;
1607 * nfs file remove call
1608 * To try and make nfs semantics closer to ufs semantics, a file that has
1609 * other processes using the vnode is renamed instead of removed and then
1610 * removed later on the last close.
1611 * - If v_sysref.refcnt > 1
1612 * If a rename is not already in the works
1613 * call nfs_sillyrename() to set it up
1617 * nfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
1618 * struct componentname *a_cnp)
1621 nfs_remove(struct vop_old_remove_args *ap)
1623 struct vnode *vp = ap->a_vp;
1624 struct vnode *dvp = ap->a_dvp;
1625 struct componentname *cnp = ap->a_cnp;
1626 struct nfsnode *np = VTONFS(vp);
1631 if (vp->v_sysref.refcnt < 1)
1632 panic("nfs_remove: bad v_sysref.refcnt");
1634 if (vp->v_type == VDIR)
1636 else if (vp->v_sysref.refcnt == 1 || (np->n_sillyrename &&
1637 VOP_GETATTR(vp, &vattr) == 0 &&
1638 vattr.va_nlink > 1)) {
1640 * throw away biocache buffers, mainly to avoid
1641 * unnecessary delayed writes later.
1643 error = nfs_vinvalbuf(vp, 0, 1);
1646 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1647 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1649 * Kludge City: If the first reply to the remove rpc is lost..
1650 * the reply to the retransmitted request will be ENOENT
1651 * since the file was in fact removed
1652 * Therefore, we cheat and return success.
1654 if (error == ENOENT)
1656 } else if (!np->n_sillyrename) {
1657 error = nfs_sillyrename(dvp, vp, cnp);
1659 np->n_attrstamp = 0;
1664 * nfs file remove rpc called from nfs_inactive
1667 nfs_removeit(struct sillyrename *sp)
1669 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1674 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1677 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1678 struct ucred *cred, struct thread *td)
1683 caddr_t bpos, dpos, cp2;
1684 int error = 0, wccflag = NFSV3_WCCRATTR;
1685 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1686 int v3 = NFS_ISV3(dvp);
1688 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1689 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1690 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1691 nfsm_fhtom(dvp, v3);
1692 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1693 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1695 nfsm_wcc_data(dvp, wccflag);
1698 VTONFS(dvp)->n_flag |= NLMODIFIED;
1700 VTONFS(dvp)->n_attrstamp = 0;
1705 * nfs file rename call
1707 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1708 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1709 * struct vnode *a_tvp, struct componentname *a_tcnp)
1712 nfs_rename(struct vop_old_rename_args *ap)
1714 struct vnode *fvp = ap->a_fvp;
1715 struct vnode *tvp = ap->a_tvp;
1716 struct vnode *fdvp = ap->a_fdvp;
1717 struct vnode *tdvp = ap->a_tdvp;
1718 struct componentname *tcnp = ap->a_tcnp;
1719 struct componentname *fcnp = ap->a_fcnp;
1722 /* Check for cross-device rename */
1723 if ((fvp->v_mount != tdvp->v_mount) ||
1724 (tvp && (fvp->v_mount != tvp->v_mount))) {
1730 * We shouldn't have to flush fvp on rename for most server-side
1731 * filesystems as the file handle should not change. Unfortunately
1732 * the inode for some filesystems (msdosfs) might be tied to the
1733 * file name or directory position so to be completely safe
1734 * vfs.nfs.flush_on_rename is set by default. Clear to improve
1737 * We must flush tvp on rename because it might become stale on the
1738 * server after the rename.
1740 if (nfs_flush_on_rename)
1741 VOP_FSYNC(fvp, MNT_WAIT);
1743 VOP_FSYNC(tvp, MNT_WAIT);
1746 * If the tvp exists and is in use, sillyrename it before doing the
1747 * rename of the new file over it.
1749 * XXX Can't sillyrename a directory.
1751 * We do not attempt to do any namecache purges in this old API
1752 * routine. The new API compat functions have access to the actual
1753 * namecache structures and will do it for us.
1755 if (tvp && tvp->v_sysref.refcnt > 1 && !VTONFS(tvp)->n_sillyrename &&
1756 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1763 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1764 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1777 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1779 if (error == ENOENT)
1785 * nfs file rename rpc called from nfs_remove() above
1788 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1789 struct sillyrename *sp)
1791 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1792 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1796 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1799 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1800 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1801 struct ucred *cred, struct thread *td)
1806 caddr_t bpos, dpos, cp2;
1807 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1808 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1809 int v3 = NFS_ISV3(fdvp);
1811 nfsstats.rpccnt[NFSPROC_RENAME]++;
1812 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1813 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1814 nfsm_rndup(tnamelen));
1815 nfsm_fhtom(fdvp, v3);
1816 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1817 nfsm_fhtom(tdvp, v3);
1818 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1819 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1821 nfsm_wcc_data(fdvp, fwccflag);
1822 nfsm_wcc_data(tdvp, twccflag);
1826 VTONFS(fdvp)->n_flag |= NLMODIFIED;
1827 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1829 VTONFS(fdvp)->n_attrstamp = 0;
1831 VTONFS(tdvp)->n_attrstamp = 0;
1836 * nfs hard link create call
1838 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1839 * struct componentname *a_cnp)
1842 nfs_link(struct vop_old_link_args *ap)
1844 struct vnode *vp = ap->a_vp;
1845 struct vnode *tdvp = ap->a_tdvp;
1846 struct componentname *cnp = ap->a_cnp;
1850 caddr_t bpos, dpos, cp2;
1851 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1852 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1855 if (vp->v_mount != tdvp->v_mount) {
1860 * The attribute cache may get out of sync with the server on link.
1861 * Pushing writes to the server before handle was inherited from
1862 * long long ago and it is unclear if we still need to do this.
1865 if (nfs_flush_on_hlink)
1866 VOP_FSYNC(vp, MNT_WAIT);
1869 nfsstats.rpccnt[NFSPROC_LINK]++;
1870 nfsm_reqhead(vp, NFSPROC_LINK,
1871 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1873 nfsm_fhtom(tdvp, v3);
1874 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1875 nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1877 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1878 nfsm_wcc_data(tdvp, wccflag);
1882 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1884 VTONFS(vp)->n_attrstamp = 0;
1886 VTONFS(tdvp)->n_attrstamp = 0;
1888 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1890 if (error == EEXIST)
1896 * nfs symbolic link create call
1898 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1899 * struct componentname *a_cnp, struct vattr *a_vap,
1903 nfs_symlink(struct vop_old_symlink_args *ap)
1905 struct vnode *dvp = ap->a_dvp;
1906 struct vattr *vap = ap->a_vap;
1907 struct componentname *cnp = ap->a_cnp;
1908 struct nfsv2_sattr *sp;
1912 caddr_t bpos, dpos, cp2;
1913 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1914 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1915 struct vnode *newvp = NULL;
1916 int v3 = NFS_ISV3(dvp);
1918 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1919 slen = strlen(ap->a_target);
1920 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1921 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1922 nfsm_fhtom(dvp, v3);
1923 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1925 nfsm_v3attrbuild(vap, FALSE);
1927 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1929 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1930 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1931 sp->sa_uid = nfs_xdrneg1;
1932 sp->sa_gid = nfs_xdrneg1;
1933 sp->sa_size = nfs_xdrneg1;
1934 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1935 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1939 * Issue the NFS request and get the rpc response.
1941 * Only NFSv3 responses returning an error of 0 actually return
1942 * a file handle that can be converted into newvp without having
1943 * to do an extra lookup rpc.
1945 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1948 nfsm_mtofh(dvp, newvp, v3, gotvp);
1949 nfsm_wcc_data(dvp, wccflag);
1953 * out code jumps -> here, mrep is also freed.
1960 * If we get an EEXIST error, silently convert it to no-error
1961 * in case of an NFS retry.
1963 if (error == EEXIST)
1967 * If we do not have (or no longer have) an error, and we could
1968 * not extract the newvp from the response due to the request being
1969 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1970 * to obtain a newvp to return.
1972 if (error == 0 && newvp == NULL) {
1973 struct nfsnode *np = NULL;
1975 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1976 cnp->cn_cred, cnp->cn_td, &np);
1986 VTONFS(dvp)->n_flag |= NLMODIFIED;
1988 VTONFS(dvp)->n_attrstamp = 0;
1995 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1996 * struct componentname *a_cnp, struct vattr *a_vap)
1999 nfs_mkdir(struct vop_old_mkdir_args *ap)
2001 struct vnode *dvp = ap->a_dvp;
2002 struct vattr *vap = ap->a_vap;
2003 struct componentname *cnp = ap->a_cnp;
2004 struct nfsv2_sattr *sp;
2009 struct nfsnode *np = NULL;
2010 struct vnode *newvp = NULL;
2011 caddr_t bpos, dpos, cp2;
2012 int error = 0, wccflag = NFSV3_WCCRATTR;
2014 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2016 int v3 = NFS_ISV3(dvp);
2018 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
2021 len = cnp->cn_namelen;
2022 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2023 nfsm_reqhead(dvp, NFSPROC_MKDIR,
2024 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2025 nfsm_fhtom(dvp, v3);
2026 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
2028 nfsm_v3attrbuild(vap, FALSE);
2030 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2031 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2032 sp->sa_uid = nfs_xdrneg1;
2033 sp->sa_gid = nfs_xdrneg1;
2034 sp->sa_size = nfs_xdrneg1;
2035 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2036 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2038 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
2040 nfsm_mtofh(dvp, newvp, v3, gotvp);
2042 nfsm_wcc_data(dvp, wccflag);
2045 VTONFS(dvp)->n_flag |= NLMODIFIED;
2047 VTONFS(dvp)->n_attrstamp = 0;
2049 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2050 * if we can succeed in looking up the directory.
2052 if (error == EEXIST || (!error && !gotvp)) {
2057 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2061 if (newvp->v_type != VDIR)
2074 * nfs remove directory call
2076 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2077 * struct componentname *a_cnp)
2080 nfs_rmdir(struct vop_old_rmdir_args *ap)
2082 struct vnode *vp = ap->a_vp;
2083 struct vnode *dvp = ap->a_dvp;
2084 struct componentname *cnp = ap->a_cnp;
2088 caddr_t bpos, dpos, cp2;
2089 int error = 0, wccflag = NFSV3_WCCRATTR;
2090 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2091 int v3 = NFS_ISV3(dvp);
2095 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2096 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2097 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2098 nfsm_fhtom(dvp, v3);
2099 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2100 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2102 nfsm_wcc_data(dvp, wccflag);
2105 VTONFS(dvp)->n_flag |= NLMODIFIED;
2107 VTONFS(dvp)->n_attrstamp = 0;
2109 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2111 if (error == ENOENT)
2119 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2122 nfs_readdir(struct vop_readdir_args *ap)
2124 struct vnode *vp = ap->a_vp;
2125 struct nfsnode *np = VTONFS(vp);
2126 struct uio *uio = ap->a_uio;
2130 if (vp->v_type != VDIR)
2133 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
2137 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2138 * and then check that is still valid, or if this is an NQNFS mount
2139 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2140 * VOP_GETATTR() does not necessarily go to the wire.
2142 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2143 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2144 if (VOP_GETATTR(vp, &vattr) == 0 &&
2145 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2147 nfsstats.direofcache_hits++;
2153 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2154 * own cache coherency checks so we do not have to.
2156 tresid = uio->uio_resid;
2157 error = nfs_bioread(vp, uio, 0);
2159 if (!error && uio->uio_resid == tresid)
2160 nfsstats.direofcache_misses++;
2167 * Readdir rpc call. nfs_bioread->nfs_doio->nfs_readdirrpc.
2169 * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2170 * offset/block and converts the nfs formatted directory entries for userland
2171 * consumption as well as deals with offsets into the middle of blocks.
2172 * nfs_doio only deals with logical blocks. In particular, uio_offset will
2173 * be block-bounded. It must convert to cookies for the actual RPC.
2176 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2179 struct nfs_dirent *dp = NULL;
2184 caddr_t bpos, dpos, cp2;
2185 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2187 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2188 struct nfsnode *dnp = VTONFS(vp);
2190 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2192 int v3 = NFS_ISV3(vp);
2195 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2196 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2197 panic("nfs readdirrpc bad uio");
2201 * If there is no cookie, assume directory was stale.
2203 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2207 return (NFSERR_BAD_COOKIE);
2209 * Loop around doing readdir rpc's of size nm_readdirsize
2210 * truncated to a multiple of DIRBLKSIZ.
2211 * The stopping criteria is EOF or buffer full.
2213 while (more_dirs && bigenough) {
2214 nfsstats.rpccnt[NFSPROC_READDIR]++;
2215 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2219 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2220 *tl++ = cookie.nfsuquad[0];
2221 *tl++ = cookie.nfsuquad[1];
2222 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2223 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2225 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2226 *tl++ = cookie.nfsuquad[0];
2228 *tl = txdr_unsigned(nmp->nm_readdirsize);
2229 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2231 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2233 nfsm_dissect(tl, u_int32_t *,
2235 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2236 dnp->n_cookieverf.nfsuquad[1] = *tl;
2242 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2243 more_dirs = fxdr_unsigned(int, *tl);
2245 /* loop thru the dir entries, converting them to std form */
2246 while (more_dirs && bigenough) {
2248 nfsm_dissect(tl, u_int32_t *,
2250 fileno = fxdr_hyper(tl);
2251 len = fxdr_unsigned(int, *(tl + 2));
2253 nfsm_dissect(tl, u_int32_t *,
2255 fileno = fxdr_unsigned(u_quad_t, *tl++);
2256 len = fxdr_unsigned(int, *tl);
2258 if (len <= 0 || len > NFS_MAXNAMLEN) {
2265 * len is the number of bytes in the path element
2266 * name, not including the \0 termination.
2268 * tlen is the number of bytes w have to reserve for
2269 * the path element name.
2271 tlen = nfsm_rndup(len);
2273 tlen += 4; /* To ensure null termination */
2276 * If the entry would cross a DIRBLKSIZ boundary,
2277 * extend the previous nfs_dirent to cover the
2280 left = DIRBLKSIZ - blksiz;
2281 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2282 dp->nfs_reclen += left;
2283 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2284 uiop->uio_iov->iov_len -= left;
2285 uiop->uio_offset += left;
2286 uiop->uio_resid -= left;
2289 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2292 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2293 dp->nfs_ino = fileno;
2294 dp->nfs_namlen = len;
2295 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2296 dp->nfs_type = DT_UNKNOWN;
2297 blksiz += dp->nfs_reclen;
2298 if (blksiz == DIRBLKSIZ)
2300 uiop->uio_offset += sizeof(struct nfs_dirent);
2301 uiop->uio_resid -= sizeof(struct nfs_dirent);
2302 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2303 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2304 nfsm_mtouio(uiop, len);
2307 * The uiop has advanced by nfs_dirent + len
2308 * but really needs to advance by
2311 cp = uiop->uio_iov->iov_base;
2313 *cp = '\0'; /* null terminate */
2314 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2315 uiop->uio_iov->iov_len -= tlen;
2316 uiop->uio_offset += tlen;
2317 uiop->uio_resid -= tlen;
2320 * NFS strings must be rounded up (nfsm_myouio
2321 * handled that in the bigenough case).
2323 nfsm_adv(nfsm_rndup(len));
2326 nfsm_dissect(tl, u_int32_t *,
2329 nfsm_dissect(tl, u_int32_t *,
2334 * If we were able to accomodate the last entry,
2335 * get the cookie for the next one. Otherwise
2336 * hold-over the cookie for the one we were not
2337 * able to accomodate.
2340 cookie.nfsuquad[0] = *tl++;
2342 cookie.nfsuquad[1] = *tl++;
2348 more_dirs = fxdr_unsigned(int, *tl);
2351 * If at end of rpc data, get the eof boolean
2354 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2355 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2360 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2361 * by increasing d_reclen for the last record.
2364 left = DIRBLKSIZ - blksiz;
2365 dp->nfs_reclen += left;
2366 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2367 uiop->uio_iov->iov_len -= left;
2368 uiop->uio_offset += left;
2369 uiop->uio_resid -= left;
2374 * We hit the end of the directory, update direofoffset.
2376 dnp->n_direofoffset = uiop->uio_offset;
2379 * There is more to go, insert the link cookie so the
2380 * next block can be read.
2382 if (uiop->uio_resid > 0)
2383 kprintf("EEK! readdirrpc resid > 0\n");
2384 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2392 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2395 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2398 struct nfs_dirent *dp;
2402 struct vnode *newvp;
2404 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2405 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2407 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2408 struct nfsnode *dnp = VTONFS(vp), *np;
2411 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2412 int attrflag, fhsize;
2413 struct nchandle nch;
2414 struct nchandle dnch;
2415 struct nlcomponent nlc;
2421 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2422 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2423 panic("nfs readdirplusrpc bad uio");
2426 * Obtain the namecache record for the directory so we have something
2427 * to use as a basis for creating the entries. This function will
2428 * return a held (but not locked) ncp. The ncp may be disconnected
2429 * from the tree and cannot be used for upward traversals, and the
2430 * ncp may be unnamed. Note that other unrelated operations may
2431 * cause the ncp to be named at any time.
2433 cache_fromdvp(vp, NULL, 0, &dnch);
2434 bzero(&nlc, sizeof(nlc));
2438 * If there is no cookie, assume directory was stale.
2440 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2444 return (NFSERR_BAD_COOKIE);
2446 * Loop around doing readdir rpc's of size nm_readdirsize
2447 * truncated to a multiple of DIRBLKSIZ.
2448 * The stopping criteria is EOF or buffer full.
2450 while (more_dirs && bigenough) {
2451 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2452 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2453 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2455 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2456 *tl++ = cookie.nfsuquad[0];
2457 *tl++ = cookie.nfsuquad[1];
2458 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2459 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2460 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2461 *tl = txdr_unsigned(nmp->nm_rsize);
2462 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2463 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2468 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2469 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2470 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2471 more_dirs = fxdr_unsigned(int, *tl);
2473 /* loop thru the dir entries, doctoring them to 4bsd form */
2474 while (more_dirs && bigenough) {
2475 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2476 fileno = fxdr_hyper(tl);
2477 len = fxdr_unsigned(int, *(tl + 2));
2478 if (len <= 0 || len > NFS_MAXNAMLEN) {
2483 tlen = nfsm_rndup(len);
2485 tlen += 4; /* To ensure null termination*/
2486 left = DIRBLKSIZ - blksiz;
2487 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2488 dp->nfs_reclen += left;
2489 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2490 uiop->uio_iov->iov_len -= left;
2491 uiop->uio_offset += left;
2492 uiop->uio_resid -= left;
2495 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2498 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2499 dp->nfs_ino = fileno;
2500 dp->nfs_namlen = len;
2501 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2502 dp->nfs_type = DT_UNKNOWN;
2503 blksiz += dp->nfs_reclen;
2504 if (blksiz == DIRBLKSIZ)
2506 uiop->uio_offset += sizeof(struct nfs_dirent);
2507 uiop->uio_resid -= sizeof(struct nfs_dirent);
2508 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2509 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2510 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2511 nlc.nlc_namelen = len;
2512 nfsm_mtouio(uiop, len);
2513 cp = uiop->uio_iov->iov_base;
2516 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2517 uiop->uio_iov->iov_len -= tlen;
2518 uiop->uio_offset += tlen;
2519 uiop->uio_resid -= tlen;
2521 nfsm_adv(nfsm_rndup(len));
2522 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2524 cookie.nfsuquad[0] = *tl++;
2525 cookie.nfsuquad[1] = *tl++;
2530 * Since the attributes are before the file handle
2531 * (sigh), we must skip over the attributes and then
2532 * come back and get them.
2534 attrflag = fxdr_unsigned(int, *tl);
2538 nfsm_adv(NFSX_V3FATTR);
2539 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2540 doit = fxdr_unsigned(int, *tl);
2542 nfsm_getfh(fhp, fhsize, 1);
2543 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2548 error = nfs_nget(vp->v_mount, fhp,
2556 if (doit && bigenough) {
2561 nfsm_loadattr(newvp, NULL);
2565 IFTODT(VTTOIF(np->n_vattr.va_type));
2567 kprintf("NFS/READDIRPLUS, ENTER %*.*s\n",
2568 nlc.nlc_namelen, nlc.nlc_namelen,
2570 nch = cache_nlookup(&dnch, &nlc);
2571 cache_setunresolved(&nch);
2572 nfs_cache_setvp(&nch, newvp,
2573 nfspos_cache_timeout);
2576 kprintf("NFS/READDIRPLUS, UNABLE TO ENTER"
2578 nlc.nlc_namelen, nlc.nlc_namelen,
2583 /* Just skip over the file handle */
2584 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2585 i = fxdr_unsigned(int, *tl);
2586 nfsm_adv(nfsm_rndup(i));
2588 if (newvp != NULLVP) {
2595 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2596 more_dirs = fxdr_unsigned(int, *tl);
2599 * If at end of rpc data, get the eof boolean
2602 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2603 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2608 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2609 * by increasing d_reclen for the last record.
2612 left = DIRBLKSIZ - blksiz;
2613 dp->nfs_reclen += left;
2614 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2615 uiop->uio_iov->iov_len -= left;
2616 uiop->uio_offset += left;
2617 uiop->uio_resid -= left;
2621 * We are now either at the end of the directory or have filled the
2625 dnp->n_direofoffset = uiop->uio_offset;
2627 if (uiop->uio_resid > 0)
2628 kprintf("EEK! readdirplusrpc resid > 0\n");
2629 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2633 if (newvp != NULLVP) {
2646 * Silly rename. To make the NFS filesystem that is stateless look a little
2647 * more like the "ufs" a remove of an active vnode is translated to a rename
2648 * to a funny looking filename that is removed by nfs_inactive on the
2649 * nfsnode. There is the potential for another process on a different client
2650 * to create the same funny name between the nfs_lookitup() fails and the
2651 * nfs_rename() completes, but...
2654 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2656 struct sillyrename *sp;
2661 * We previously purged dvp instead of vp. I don't know why, it
2662 * completely destroys performance. We can't do it anyway with the
2663 * new VFS API since we would be breaking the namecache topology.
2665 cache_purge(vp); /* XXX */
2668 if (vp->v_type == VDIR)
2669 panic("nfs: sillyrename dir");
2671 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2672 M_NFSREQ, M_WAITOK);
2673 sp->s_cred = crdup(cnp->cn_cred);
2677 /* Fudge together a funny name */
2678 sp->s_namlen = ksprintf(sp->s_name, ".nfsA%08x4.4",
2679 (int)(intptr_t)cnp->cn_td);
2681 /* Try lookitups until we get one that isn't there */
2682 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2683 cnp->cn_td, NULL) == 0) {
2685 if (sp->s_name[4] > 'z') {
2690 error = nfs_renameit(dvp, cnp, sp);
2693 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2695 np->n_sillyrename = sp;
2700 kfree((caddr_t)sp, M_NFSREQ);
2705 * Look up a file name and optionally either update the file handle or
2706 * allocate an nfsnode, depending on the value of npp.
2707 * npp == NULL --> just do the lookup
2708 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2710 * *npp != NULL --> update the file handle in the vnode
2713 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2714 struct thread *td, struct nfsnode **npp)
2719 struct vnode *newvp = NULL;
2720 struct nfsnode *np, *dnp = VTONFS(dvp);
2721 caddr_t bpos, dpos, cp2;
2722 int error = 0, fhlen, attrflag;
2723 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2725 int v3 = NFS_ISV3(dvp);
2727 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2728 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2729 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2730 nfsm_fhtom(dvp, v3);
2731 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2732 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2733 if (npp && !error) {
2734 nfsm_getfh(nfhp, fhlen, v3);
2737 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2738 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
2739 np->n_fhp = &np->n_fh;
2740 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2741 np->n_fhp =(nfsfh_t *)kmalloc(fhlen,M_NFSBIGFH,M_WAITOK);
2742 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2743 np->n_fhsize = fhlen;
2745 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2749 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2757 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
2758 if (!attrflag && *npp == NULL) {
2767 nfsm_loadattr(newvp, NULL);
2771 if (npp && *npp == NULL) {
2786 * Nfs Version 3 commit rpc
2789 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2794 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2795 caddr_t bpos, dpos, cp2;
2796 int error = 0, wccflag = NFSV3_WCCRATTR;
2797 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2799 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2801 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2802 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2804 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2805 txdr_hyper(offset, tl);
2807 *tl = txdr_unsigned(cnt);
2808 nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2809 nfsm_wcc_data(vp, wccflag);
2811 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2812 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2813 NFSX_V3WRITEVERF)) {
2814 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2816 error = NFSERR_STALEWRITEVERF;
2826 * - make nfs_bmap() essentially a no-op that does no translation
2827 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2828 * (Maybe I could use the process's page mapping, but I was concerned that
2829 * Kernel Write might not be enabled and also figured copyout() would do
2830 * a lot more work than bcopy() and also it currently happens in the
2831 * context of the swapper process (2).
2833 * nfs_bmap(struct vnode *a_vp, off_t a_loffset,
2834 * off_t *a_doffsetp, int *a_runp, int *a_runb)
2837 nfs_bmap(struct vop_bmap_args *ap)
2839 if (ap->a_doffsetp != NULL)
2840 *ap->a_doffsetp = ap->a_loffset;
2841 if (ap->a_runp != NULL)
2843 if (ap->a_runb != NULL)
2851 * For async requests when nfsiod(s) are running, queue the request by
2852 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2856 nfs_strategy(struct vop_strategy_args *ap)
2858 struct bio *bio = ap->a_bio;
2860 struct buf *bp = bio->bio_buf;
2864 KASSERT(bp->b_cmd != BUF_CMD_DONE,
2865 ("nfs_strategy: buffer %p unexpectedly marked done", bp));
2866 KASSERT(BUF_REFCNT(bp) > 0,
2867 ("nfs_strategy: buffer %p not locked", bp));
2869 if (bio->bio_flags & BIO_SYNC)
2870 td = curthread; /* XXX */
2875 * We probably don't need to push an nbio any more since no
2876 * block conversion is required due to the use of 64 bit byte
2877 * offsets, but do it anyway.
2879 nbio = push_bio(bio);
2880 nbio->bio_offset = bio->bio_offset;
2883 * If the op is asynchronous and an i/o daemon is waiting
2884 * queue the request, wake it up and wait for completion
2885 * otherwise just do it ourselves.
2887 if ((bio->bio_flags & BIO_SYNC) || nfs_asyncio(ap->a_vp, nbio, td))
2888 error = nfs_doio(ap->a_vp, nbio, td);
2895 * NB Currently unsupported.
2897 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
2901 nfs_mmap(struct vop_mmap_args *ap)
2907 * fsync vnode op. Just call nfs_flush() with commit == 1.
2909 * nfs_fsync(struct vnode *a_vp, int a_waitfor)
2913 nfs_fsync(struct vop_fsync_args *ap)
2915 return (nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1));
2919 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
2920 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
2921 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
2922 * set the buffer contains data that has already been written to the server
2923 * and which now needs a commit RPC.
2925 * If commit is 0 we only take one pass and only flush buffers containing new
2928 * If commit is 1 we take two passes, issuing a commit RPC in the second
2931 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
2932 * to completely flush all pending data.
2934 * Note that the RB_SCAN code properly handles the case where the
2935 * callback might block and directly or indirectly (another thread) cause
2936 * the RB tree to change.
2939 #ifndef NFS_COMMITBVECSIZ
2940 #define NFS_COMMITBVECSIZ 16
2943 struct nfs_flush_info {
2944 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
2951 struct buf *bvary[NFS_COMMITBVECSIZ];
2957 static int nfs_flush_bp(struct buf *bp, void *data);
2958 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
2961 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
2963 struct nfsnode *np = VTONFS(vp);
2964 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2965 struct nfs_flush_info info;
2969 bzero(&info, sizeof(info));
2972 info.waitfor = waitfor;
2973 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
2975 lwkt_gettoken(&vlock, &vp->v_token);
2981 info.mode = NFI_FLUSHNEW;
2982 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2983 nfs_flush_bp, &info);
2986 * Take a second pass if committing and no error occured.
2987 * Clean up any left over collection (whether an error
2990 if (commit && error == 0) {
2991 info.mode = NFI_COMMIT;
2992 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2993 nfs_flush_bp, &info);
2995 error = nfs_flush_docommit(&info, error);
2999 * Wait for pending I/O to complete before checking whether
3000 * any further dirty buffers exist.
3002 while (waitfor == MNT_WAIT &&
3003 bio_track_active(&vp->v_track_write)) {
3004 error = bio_track_wait(&vp->v_track_write,
3005 info.slpflag, info.slptimeo);
3008 * We have to be able to break out if this
3009 * is an 'intr' mount.
3011 if (nfs_sigintr(nmp, NULL, td)) {
3017 * Since we do not process pending signals,
3018 * once we get a PCATCH our tsleep() will no
3019 * longer sleep, switch to a fixed timeout
3022 if (info.slpflag == PCATCH) {
3024 info.slptimeo = 2 * hz;
3031 * Loop if we are flushing synchronous as well as committing,
3032 * and dirty buffers are still present. Otherwise we might livelock.
3034 } while (waitfor == MNT_WAIT && commit &&
3035 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3038 * The callbacks have to return a negative error to terminate the
3045 * Deal with any error collection
3047 if (np->n_flag & NWRITEERR) {
3048 error = np->n_error;
3049 np->n_flag &= ~NWRITEERR;
3051 lwkt_reltoken(&vlock);
3057 nfs_flush_bp(struct buf *bp, void *data)
3059 struct nfs_flush_info *info = data;
3065 switch(info->mode) {
3067 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3068 if (error && info->loops && info->waitfor == MNT_WAIT) {
3069 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3071 lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3072 if (info->slpflag & PCATCH)
3073 lkflags |= LK_PCATCH;
3074 error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3080 * Ignore locking errors
3088 * The buffer may have changed out from under us, even if
3089 * we did not block (MPSAFE). Check again now that it is
3092 if (bp->b_vp == info->vp &&
3093 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) == B_DELWRI) {
3102 * Only process buffers in need of a commit which we can
3103 * immediately lock. This may prevent a buffer from being
3104 * committed, but the normal flush loop will block on the
3105 * same buffer so we shouldn't get into an endless loop.
3107 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3108 (B_DELWRI | B_NEEDCOMMIT)) {
3111 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
3115 * We must recheck after successfully locking the buffer.
3117 if (bp->b_vp != info->vp ||
3118 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3119 (B_DELWRI | B_NEEDCOMMIT)) {
3125 * NOTE: storing the bp in the bvary[] basically sets
3126 * it up for a commit operation.
3128 * We must call vfs_busy_pages() now so the commit operation
3129 * is interlocked with user modifications to memory mapped
3132 * Note: to avoid loopback deadlocks, we do not
3133 * assign b_runningbufspace.
3136 bp->b_cmd = BUF_CMD_WRITE;
3137 vfs_busy_pages(bp->b_vp, bp);
3138 info->bvary[info->bvsize] = bp;
3139 toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3140 if (info->bvsize == 0 || toff < info->beg_off)
3141 info->beg_off = toff;
3142 toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3143 if (info->bvsize == 0 || toff > info->end_off)
3144 info->end_off = toff;
3146 if (info->bvsize == NFS_COMMITBVECSIZ) {
3147 error = nfs_flush_docommit(info, 0);
3148 KKASSERT(info->bvsize == 0);
3156 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3166 if (info->bvsize > 0) {
3168 * Commit data on the server, as required. Note that
3169 * nfs_commit will use the vnode's cred for the commit.
3170 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3172 bytes = info->end_off - info->beg_off;
3173 if (bytes > 0x40000000)
3178 retv = nfs_commit(vp, info->beg_off,
3179 (int)bytes, info->td);
3180 if (retv == NFSERR_STALEWRITEVERF)
3181 nfs_clearcommit(vp->v_mount);
3185 * Now, either mark the blocks I/O done or mark the
3186 * blocks dirty, depending on whether the commit
3189 for (i = 0; i < info->bvsize; ++i) {
3190 bp = info->bvary[i];
3191 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3194 * Error, leave B_DELWRI intact
3196 vfs_unbusy_pages(bp);
3197 bp->b_cmd = BUF_CMD_DONE;
3201 * Success, remove B_DELWRI ( bundirty() ).
3203 * b_dirtyoff/b_dirtyend seem to be NFS
3204 * specific. We should probably move that
3205 * into bundirty(). XXX
3207 * We are faking an I/O write, we have to
3208 * start the transaction in order to
3209 * immediately biodone() it.
3212 bp->b_flags &= ~B_ERROR;
3213 bp->b_dirtyoff = bp->b_dirtyend = 0;
3214 biodone(&bp->b_bio1);
3223 * NFS advisory byte-level locks.
3224 * Currently unsupported.
3226 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3230 nfs_advlock(struct vop_advlock_args *ap)
3232 struct nfsnode *np = VTONFS(ap->a_vp);
3235 * The following kludge is to allow diskless support to work
3236 * until a real NFS lockd is implemented. Basically, just pretend
3237 * that this is a local lock.
3239 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3243 * Print out the contents of an nfsnode.
3245 * nfs_print(struct vnode *a_vp)
3248 nfs_print(struct vop_print_args *ap)
3250 struct vnode *vp = ap->a_vp;
3251 struct nfsnode *np = VTONFS(vp);
3253 kprintf("tag VT_NFS, fileid %lld fsid 0x%x",
3254 (long long)np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3255 if (vp->v_type == VFIFO)
3262 * nfs special file access vnode op.
3263 * Essentially just get vattr and then imitate iaccess() since the device is
3264 * local to the client.
3266 * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
3269 nfsspec_access(struct vop_access_args *ap)
3273 struct ucred *cred = ap->a_cred;
3274 struct vnode *vp = ap->a_vp;
3275 mode_t mode = ap->a_mode;
3281 * Disallow write attempts on filesystems mounted read-only;
3282 * unless the file is a socket, fifo, or a block or character
3283 * device resident on the filesystem.
3285 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3286 switch (vp->v_type) {
3296 * If you're the super-user,
3297 * you always get access.
3299 if (cred->cr_uid == 0)
3302 error = VOP_GETATTR(vp, vap);
3306 * Access check is based on only one of owner, group, public.
3307 * If not owner, then check group. If not a member of the
3308 * group, then check public access.
3310 if (cred->cr_uid != vap->va_uid) {
3312 gp = cred->cr_groups;
3313 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3314 if (vap->va_gid == *gp)
3320 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3325 * Read wrapper for special devices.
3327 * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3328 * struct ucred *a_cred)
3331 nfsspec_read(struct vop_read_args *ap)
3333 struct nfsnode *np = VTONFS(ap->a_vp);
3339 getnanotime(&np->n_atim);
3340 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3344 * Write wrapper for special devices.
3346 * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3347 * struct ucred *a_cred)
3350 nfsspec_write(struct vop_write_args *ap)
3352 struct nfsnode *np = VTONFS(ap->a_vp);
3358 getnanotime(&np->n_mtim);
3359 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3363 * Close wrapper for special devices.
3365 * Update the times on the nfsnode then do device close.
3367 * nfsspec_close(struct vnode *a_vp, int a_fflag)
3370 nfsspec_close(struct vop_close_args *ap)
3372 struct vnode *vp = ap->a_vp;
3373 struct nfsnode *np = VTONFS(vp);
3376 if (np->n_flag & (NACC | NUPD)) {
3378 if (vp->v_sysref.refcnt == 1 &&
3379 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3381 if (np->n_flag & NACC)
3382 vattr.va_atime = np->n_atim;
3383 if (np->n_flag & NUPD)
3384 vattr.va_mtime = np->n_mtim;
3385 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3388 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3392 * Read wrapper for fifos.
3394 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3395 * struct ucred *a_cred)
3398 nfsfifo_read(struct vop_read_args *ap)
3400 struct nfsnode *np = VTONFS(ap->a_vp);
3406 getnanotime(&np->n_atim);
3407 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3411 * Write wrapper for fifos.
3413 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3414 * struct ucred *a_cred)
3417 nfsfifo_write(struct vop_write_args *ap)
3419 struct nfsnode *np = VTONFS(ap->a_vp);
3425 getnanotime(&np->n_mtim);
3426 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3430 * Close wrapper for fifos.
3432 * Update the times on the nfsnode then do fifo close.
3434 * nfsfifo_close(struct vnode *a_vp, int a_fflag)
3437 nfsfifo_close(struct vop_close_args *ap)
3439 struct vnode *vp = ap->a_vp;
3440 struct nfsnode *np = VTONFS(vp);
3444 if (np->n_flag & (NACC | NUPD)) {
3446 if (np->n_flag & NACC)
3448 if (np->n_flag & NUPD)
3451 if (vp->v_sysref.refcnt == 1 &&
3452 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3454 if (np->n_flag & NACC)
3455 vattr.va_atime = np->n_atim;
3456 if (np->n_flag & NUPD)
3457 vattr.va_mtime = np->n_mtim;
3458 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3461 return (VOCALL(&fifo_vnode_vops, &ap->a_head));