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)
255 struct nfsnode *np = VTONFS(vp);
260 struct nfsm_info info;
265 nfsstats.rpccnt[NFSPROC_ACCESS]++;
266 nfsm_reqhead(&info, vp, NFSPROC_ACCESS,
267 NFSX_FH(info.v3) + NFSX_UNSIGNED);
268 ERROROUT(nfsm_fhtom(&info, vp));
269 tl = nfsm_build(&info, NFSX_UNSIGNED);
270 *tl = txdr_unsigned(wmode);
271 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_ACCESS, td, cred, &error));
272 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag, NFS_LATTR_NOSHRINK));
274 NULLOUT(tl = nfsm_dissect(&info, 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;
287 * nfs access vnode op.
288 * For nfs version 2, just return ok. File accesses may fail later.
289 * For nfs version 3, use the access rpc to check accessibility. If file modes
290 * are changed on the server, accesses might still fail later.
292 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
295 nfs_access(struct vop_access_args *ap)
297 struct vnode *vp = ap->a_vp;
298 thread_t td = curthread;
300 u_int32_t mode, wmode;
301 struct nfsnode *np = VTONFS(vp);
302 int v3 = NFS_ISV3(vp);
305 * Disallow write attempts on filesystems mounted read-only;
306 * unless the file is a socket, fifo, or a block or character
307 * device resident on the filesystem.
309 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
310 switch (vp->v_type) {
320 * For nfs v3, check to see if we have done this recently, and if
321 * so return our cached result instead of making an ACCESS call.
322 * If not, do an access rpc, otherwise you are stuck emulating
323 * ufs_access() locally using the vattr. This may not be correct,
324 * since the server may apply other access criteria such as
325 * client uid-->server uid mapping that we do not know about.
328 if (ap->a_mode & VREAD)
329 mode = NFSV3ACCESS_READ;
332 if (vp->v_type != VDIR) {
333 if (ap->a_mode & VWRITE)
334 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
335 if (ap->a_mode & VEXEC)
336 mode |= NFSV3ACCESS_EXECUTE;
338 if (ap->a_mode & VWRITE)
339 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
341 if (ap->a_mode & VEXEC)
342 mode |= NFSV3ACCESS_LOOKUP;
344 /* XXX safety belt, only make blanket request if caching */
345 if (nfsaccess_cache_timeout > 0) {
346 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
347 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
348 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
354 * Does our cached result allow us to give a definite yes to
357 if (np->n_modestamp &&
358 (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
359 (ap->a_cred->cr_uid == np->n_modeuid) &&
360 ((np->n_mode & mode) == mode)) {
361 nfsstats.accesscache_hits++;
364 * Either a no, or a don't know. Go to the wire.
366 nfsstats.accesscache_misses++;
367 error = nfs3_access_otw(vp, wmode, td, ap->a_cred);
369 if ((np->n_mode & mode) != mode) {
375 if ((error = nfsspec_access(ap)) != 0)
379 * Attempt to prevent a mapped root from accessing a file
380 * which it shouldn't. We try to read a byte from the file
381 * if the user is root and the file is not zero length.
382 * After calling nfsspec_access, we should have the correct
385 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
386 && VTONFS(vp)->n_size > 0) {
393 auio.uio_iov = &aiov;
397 auio.uio_segflg = UIO_SYSSPACE;
398 auio.uio_rw = UIO_READ;
401 if (vp->v_type == VREG) {
402 error = nfs_readrpc(vp, &auio);
403 } else if (vp->v_type == VDIR) {
405 bp = kmalloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
407 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
408 error = nfs_readdirrpc(vp, &auio);
410 } else if (vp->v_type == VLNK) {
411 error = nfs_readlinkrpc(vp, &auio);
418 * [re]record creds for reading and/or writing if access
419 * was granted. Assume the NFS server will grant read access
420 * for execute requests.
423 if ((ap->a_mode & (VREAD|VEXEC)) && ap->a_cred != np->n_rucred) {
426 crfree(np->n_rucred);
427 np->n_rucred = ap->a_cred;
429 if ((ap->a_mode & VWRITE) && ap->a_cred != np->n_wucred) {
432 crfree(np->n_wucred);
433 np->n_wucred = ap->a_cred;
441 * Check to see if the type is ok
442 * and that deletion is not in progress.
443 * For paged in text files, you will need to flush the page cache
444 * if consistency is lost.
446 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
451 nfs_open(struct vop_open_args *ap)
453 struct vnode *vp = ap->a_vp;
454 struct nfsnode *np = VTONFS(vp);
458 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
460 kprintf("open eacces vtyp=%d\n",vp->v_type);
466 * Save valid creds for reading and writing for later RPCs.
468 if ((ap->a_mode & FREAD) && ap->a_cred != np->n_rucred) {
471 crfree(np->n_rucred);
472 np->n_rucred = ap->a_cred;
474 if ((ap->a_mode & FWRITE) && ap->a_cred != np->n_wucred) {
477 crfree(np->n_wucred);
478 np->n_wucred = ap->a_cred;
482 * Clear the attribute cache only if opening with write access. It
483 * is unclear if we should do this at all here, but we certainly
484 * should not clear the cache unconditionally simply because a file
487 if (ap->a_mode & FWRITE)
491 * For normal NFS, reconcile changes made locally verses
492 * changes made remotely. Note that VOP_GETATTR only goes
493 * to the wire if the cached attribute has timed out or been
496 * If local modifications have been made clear the attribute
497 * cache to force an attribute and modified time check. If
498 * GETATTR detects that the file has been changed by someone
499 * other then us it will set NRMODIFIED.
501 * If we are opening a directory and local changes have been
502 * made we have to invalidate the cache in order to ensure
503 * that we get the most up-to-date information from the
506 if (np->n_flag & NLMODIFIED) {
508 if (vp->v_type == VDIR) {
509 error = nfs_vinvalbuf(vp, V_SAVE, 1);
515 error = VOP_GETATTR(vp, &vattr);
518 if (np->n_flag & NRMODIFIED) {
519 if (vp->v_type == VDIR)
521 error = nfs_vinvalbuf(vp, V_SAVE, 1);
524 np->n_flag &= ~NRMODIFIED;
527 return (vop_stdopen(ap));
532 * What an NFS client should do upon close after writing is a debatable issue.
533 * Most NFS clients push delayed writes to the server upon close, basically for
535 * 1 - So that any write errors may be reported back to the client process
536 * doing the close system call. By far the two most likely errors are
537 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
538 * 2 - To put a worst case upper bound on cache inconsistency between
539 * multiple clients for the file.
540 * There is also a consistency problem for Version 2 of the protocol w.r.t.
541 * not being able to tell if other clients are writing a file concurrently,
542 * since there is no way of knowing if the changed modify time in the reply
543 * is only due to the write for this client.
544 * (NFS Version 3 provides weak cache consistency data in the reply that
545 * should be sufficient to detect and handle this case.)
547 * The current code does the following:
548 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
549 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
550 * or commit them (this satisfies 1 and 2 except for the
551 * case where the server crashes after this close but
552 * before the commit RPC, which is felt to be "good
553 * enough". Changing the last argument to nfs_flush() to
554 * a 1 would force a commit operation, if it is felt a
555 * commit is necessary now.
556 * for NQNFS - do nothing now, since 2 is dealt with via leases and
557 * 1 should be dealt with via an fsync() system call for
558 * cases where write errors are important.
560 * nfs_close(struct vnode *a_vp, int a_fflag)
564 nfs_close(struct vop_close_args *ap)
566 struct vnode *vp = ap->a_vp;
567 struct nfsnode *np = VTONFS(vp);
569 thread_t td = curthread;
571 if (vp->v_type == VREG) {
572 if (np->n_flag & NLMODIFIED) {
575 * Under NFSv3 we have dirty buffers to dispose of. We
576 * must flush them to the NFS server. We have the option
577 * of waiting all the way through the commit rpc or just
578 * waiting for the initial write. The default is to only
579 * wait through the initial write so the data is in the
580 * server's cache, which is roughly similar to the state
581 * a standard disk subsystem leaves the file in on close().
583 * We cannot clear the NLMODIFIED bit in np->n_flag due to
584 * potential races with other processes, and certainly
585 * cannot clear it if we don't commit.
587 int cm = nfsv3_commit_on_close ? 1 : 0;
588 error = nfs_flush(vp, MNT_WAIT, td, cm);
589 /* np->n_flag &= ~NLMODIFIED; */
591 error = nfs_vinvalbuf(vp, V_SAVE, 1);
595 if (np->n_flag & NWRITEERR) {
596 np->n_flag &= ~NWRITEERR;
605 * nfs getattr call from vfs.
607 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
610 nfs_getattr(struct vop_getattr_args *ap)
612 struct vnode *vp = ap->a_vp;
613 struct nfsnode *np = VTONFS(vp);
615 thread_t td = curthread;
616 struct nfsm_info info;
619 info.v3 = NFS_ISV3(vp);
622 * Update local times for special files.
624 if (np->n_flag & (NACC | NUPD))
627 * First look in the cache.
629 if (nfs_getattrcache(vp, ap->a_vap) == 0)
632 if (info.v3 && nfsaccess_cache_timeout > 0) {
633 nfsstats.accesscache_misses++;
634 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, nfs_vpcred(vp, ND_CHECK));
635 if (nfs_getattrcache(vp, ap->a_vap) == 0)
639 nfsstats.rpccnt[NFSPROC_GETATTR]++;
640 nfsm_reqhead(&info, vp, NFSPROC_GETATTR, NFSX_FH(info.v3));
641 ERROROUT(nfsm_fhtom(&info, vp));
642 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_GETATTR, td,
643 nfs_vpcred(vp, ND_CHECK), &error));
645 ERROROUT(nfsm_loadattr(&info, vp, ap->a_vap));
656 * nfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
659 nfs_setattr(struct vop_setattr_args *ap)
661 struct vnode *vp = ap->a_vp;
662 struct nfsnode *np = VTONFS(vp);
663 struct vattr *vap = ap->a_vap;
666 thread_t td = curthread;
673 * Setting of flags is not supported.
675 if (vap->va_flags != VNOVAL)
679 * Disallow write attempts if the filesystem is mounted read-only.
681 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
682 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
683 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
684 (vp->v_mount->mnt_flag & MNT_RDONLY))
687 if (vap->va_size != VNOVAL) {
689 * truncation requested
691 switch (vp->v_type) {
698 if (vap->va_mtime.tv_sec == VNOVAL &&
699 vap->va_atime.tv_sec == VNOVAL &&
700 vap->va_mode == (mode_t)VNOVAL &&
701 vap->va_uid == (uid_t)VNOVAL &&
702 vap->va_gid == (gid_t)VNOVAL)
704 vap->va_size = VNOVAL;
708 * Disallow write attempts if the filesystem is
711 if (vp->v_mount->mnt_flag & MNT_RDONLY)
715 * This is nasty. The RPCs we send to flush pending
716 * data often return attribute information which is
717 * cached via a callback to nfs_loadattrcache(), which
718 * has the effect of changing our notion of the file
719 * size. Due to flushed appends and other operations
720 * the file size can be set to virtually anything,
721 * including values that do not match either the old
722 * or intended file size.
724 * When this condition is detected we must loop to
725 * try the operation again. Hopefully no more
726 * flushing is required on the loop so it works the
727 * second time around. THIS CASE ALMOST ALWAYS
732 error = nfs_meta_setsize(vp, td, vap->va_size);
734 if (np->n_flag & NLMODIFIED) {
735 if (vap->va_size == 0)
736 error = nfs_vinvalbuf(vp, 0, 1);
738 error = nfs_vinvalbuf(vp, V_SAVE, 1);
741 * note: this loop case almost always happens at
742 * least once per truncation.
744 if (error == 0 && np->n_size != vap->va_size)
746 np->n_vattr.va_size = vap->va_size;
749 } else if ((np->n_flag & NLMODIFIED) && vp->v_type == VREG) {
751 * What to do. If we are modifying the mtime we lose
752 * mtime detection of changes made by the server or other
753 * clients. But programs like rsync/rdist/cpdup are going
754 * to call utimes a lot. We don't want to piecemeal sync.
756 * For now sync if any prior remote changes were detected,
757 * but allow us to lose track of remote changes made during
758 * the utimes operation.
760 if (np->n_flag & NRMODIFIED)
761 error = nfs_vinvalbuf(vp, V_SAVE, 1);
765 if (vap->va_mtime.tv_sec != VNOVAL) {
766 np->n_mtime = vap->va_mtime.tv_sec;
770 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
773 * Sanity check if a truncation was issued. This should only occur
774 * if multiple processes are racing on the same file.
776 if (error == 0 && vap->va_size != VNOVAL &&
777 np->n_size != vap->va_size) {
778 kprintf("NFS ftruncate: server disagrees on the file size: "
781 (long long)vap->va_size,
782 (long long)np->n_size);
785 if (error && vap->va_size != VNOVAL) {
786 np->n_size = np->n_vattr.va_size = tsize;
787 vnode_pager_setsize(vp, np->n_size);
793 * Do an nfs setattr rpc.
796 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
797 struct ucred *cred, struct thread *td)
799 struct nfsv2_sattr *sp;
800 struct nfsnode *np = VTONFS(vp);
802 int error = 0, wccflag = NFSV3_WCCRATTR;
803 struct nfsm_info info;
806 info.v3 = NFS_ISV3(vp);
808 nfsstats.rpccnt[NFSPROC_SETATTR]++;
809 nfsm_reqhead(&info, vp, NFSPROC_SETATTR,
810 NFSX_FH(info.v3) + NFSX_SATTR(info.v3));
811 ERROROUT(nfsm_fhtom(&info, vp));
813 nfsm_v3attrbuild(&info, vap, TRUE);
814 tl = nfsm_build(&info, NFSX_UNSIGNED);
817 sp = nfsm_build(&info, NFSX_V2SATTR);
818 if (vap->va_mode == (mode_t)VNOVAL)
819 sp->sa_mode = nfs_xdrneg1;
821 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
822 if (vap->va_uid == (uid_t)VNOVAL)
823 sp->sa_uid = nfs_xdrneg1;
825 sp->sa_uid = txdr_unsigned(vap->va_uid);
826 if (vap->va_gid == (gid_t)VNOVAL)
827 sp->sa_gid = nfs_xdrneg1;
829 sp->sa_gid = txdr_unsigned(vap->va_gid);
830 sp->sa_size = txdr_unsigned(vap->va_size);
831 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
832 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
834 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_SETATTR, td, cred, &error));
837 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
839 ERROROUT(nfsm_loadattr(&info, vp, NULL));
849 nfs_cache_setvp(struct nchandle *nch, struct vnode *vp, int nctimeout)
855 cache_setvp(nch, vp);
856 cache_settimeout(nch, nctimeout);
860 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
861 * nfs_lookup() until all remaining new api calls are implemented.
863 * Resolve a namecache entry. This function is passed a locked ncp and
864 * must call nfs_cache_setvp() on it as appropriate to resolve the entry.
867 nfs_nresolve(struct vop_nresolve_args *ap)
869 struct thread *td = curthread;
870 struct namecache *ncp;
881 struct nfsm_info info;
886 if ((error = vget(dvp, LK_SHARED)) != 0)
890 info.v3 = NFS_ISV3(dvp);
893 nfsstats.lookupcache_misses++;
894 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
895 ncp = ap->a_nch->ncp;
897 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
898 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
899 ERROROUT(nfsm_fhtom(&info, dvp));
900 ERROROUT(nfsm_strtom(&info, ncp->nc_name, len, NFS_MAXNAMLEN));
901 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td,
902 ap->a_cred, &error));
905 * Cache negatve lookups to reduce NFS traffic, but use
906 * a fast timeout. Otherwise use a timeout of 1 tick.
907 * XXX we should add a namecache flag for no-caching
908 * to uncache the negative hit as soon as possible, but
909 * we cannot simply destroy the entry because it is used
910 * as a placeholder by the caller.
912 * The refactored nfs code will overwrite a non-zero error
913 * with 0 when we use ERROROUT(), so don't here.
916 nfs_cache_setvp(ap->a_nch, NULL, nfsneg_cache_timeout);
917 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
929 * Success, get the file handle, do various checks, and load
930 * post-operation data from the reply packet. Theoretically
931 * we should never be looking up "." so, theoretically, we
932 * should never get the same file handle as our directory. But
933 * we check anyway. XXX
935 * Note that no timeout is set for the positive cache hit. We
936 * assume, theoretically, that ESTALE returns will be dealt with
937 * properly to handle NFS races and in anycase we cannot depend
938 * on a timeout to deal with NFS open/create/excl issues so instead
939 * of a bad hack here the rest of the NFS client code needs to do
942 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
945 if (NFS_CMPFH(np, fhp, fhsize)) {
949 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
959 ERROROUT(nfsm_postop_attr(&info, nvp, &attrflag,
960 NFS_LATTR_NOSHRINK));
961 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
962 NFS_LATTR_NOSHRINK));
964 ERROROUT(nfsm_loadattr(&info, nvp, NULL));
966 nfs_cache_setvp(ap->a_nch, nvp, nfspos_cache_timeout);
981 * 'cached' nfs directory lookup
983 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
985 * nfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
986 * struct componentname *a_cnp)
989 nfs_lookup(struct vop_old_lookup_args *ap)
991 struct componentname *cnp = ap->a_cnp;
992 struct vnode *dvp = ap->a_dvp;
993 struct vnode **vpp = ap->a_vpp;
994 int flags = cnp->cn_flags;
996 struct nfsmount *nmp;
1000 int lockparent, wantparent, attrflag, fhsize;
1003 struct nfsm_info info;
1006 info.v3 = NFS_ISV3(dvp);
1010 * Read-only mount check and directory check.
1013 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1014 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
1017 if (dvp->v_type != VDIR)
1021 * Look it up in the cache. Note that ENOENT is only returned if we
1022 * previously entered a negative hit (see later on). The additional
1023 * nfsneg_cache_timeout check causes previously cached results to
1024 * be instantly ignored if the negative caching is turned off.
1026 lockparent = flags & CNP_LOCKPARENT;
1027 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1028 nmp = VFSTONFS(dvp->v_mount);
1036 nfsstats.lookupcache_misses++;
1037 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1038 len = cnp->cn_namelen;
1039 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
1040 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1041 ERROROUT(nfsm_fhtom(&info, dvp));
1042 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
1043 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, cnp->cn_td,
1044 cnp->cn_cred, &error));
1046 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
1047 NFS_LATTR_NOSHRINK);
1057 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1060 * Handle RENAME case...
1062 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1063 if (NFS_CMPFH(np, fhp, fhsize)) {
1068 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1076 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1077 NFS_LATTR_NOSHRINK));
1078 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1079 NFS_LATTR_NOSHRINK));
1081 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1088 cnp->cn_flags |= CNP_PDIRUNLOCK;
1093 if (flags & CNP_ISDOTDOT) {
1095 cnp->cn_flags |= CNP_PDIRUNLOCK;
1096 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1098 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1099 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1100 return (error); /* NOTE: return error from nget */
1104 error = vn_lock(dvp, LK_EXCLUSIVE);
1109 cnp->cn_flags |= CNP_PDIRUNLOCK;
1111 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1115 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1123 cnp->cn_flags |= CNP_PDIRUNLOCK;
1128 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1129 NFS_LATTR_NOSHRINK));
1130 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1131 NFS_LATTR_NOSHRINK));
1133 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1136 /* XXX MOVE TO nfs_nremove() */
1137 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1138 cnp->cn_nameiop != NAMEI_DELETE) {
1139 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1147 if (newvp != NULLVP) {
1151 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1152 cnp->cn_nameiop == NAMEI_RENAME) &&
1156 cnp->cn_flags |= CNP_PDIRUNLOCK;
1158 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1161 error = EJUSTRETURN;
1169 * Just call nfs_bioread() to do the work.
1171 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1172 * struct ucred *a_cred)
1175 nfs_read(struct vop_read_args *ap)
1177 struct vnode *vp = ap->a_vp;
1179 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1185 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1188 nfs_readlink(struct vop_readlink_args *ap)
1190 struct vnode *vp = ap->a_vp;
1192 if (vp->v_type != VLNK)
1194 return (nfs_bioread(vp, ap->a_uio, 0));
1198 * Do a readlink rpc.
1199 * Called by nfs_doio() from below the buffer cache.
1202 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1204 int error = 0, len, attrflag;
1205 struct nfsm_info info;
1208 info.v3 = NFS_ISV3(vp);
1210 nfsstats.rpccnt[NFSPROC_READLINK]++;
1211 nfsm_reqhead(&info, vp, NFSPROC_READLINK, NFSX_FH(info.v3));
1212 ERROROUT(nfsm_fhtom(&info, vp));
1213 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READLINK, uiop->uio_td,
1214 nfs_vpcred(vp, ND_CHECK), &error));
1216 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1217 NFS_LATTR_NOSHRINK));
1220 NEGATIVEOUT(len = nfsm_strsiz(&info, NFS_MAXPATHLEN));
1221 if (len == NFS_MAXPATHLEN) {
1222 struct nfsnode *np = VTONFS(vp);
1223 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1226 ERROROUT(nfsm_mtouio(&info, uiop, len));
1239 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1242 struct nfsmount *nmp;
1243 int error = 0, len, retlen, tsiz, eof, attrflag;
1244 struct nfsm_info info;
1247 info.v3 = NFS_ISV3(vp);
1252 nmp = VFSTONFS(vp->v_mount);
1253 tsiz = uiop->uio_resid;
1254 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1257 nfsstats.rpccnt[NFSPROC_READ]++;
1258 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1259 nfsm_reqhead(&info, vp, NFSPROC_READ,
1260 NFSX_FH(info.v3) + NFSX_UNSIGNED * 3);
1261 ERROROUT(nfsm_fhtom(&info, vp));
1262 tl = nfsm_build(&info, NFSX_UNSIGNED * 3);
1264 txdr_hyper(uiop->uio_offset, tl);
1265 *(tl + 2) = txdr_unsigned(len);
1267 *tl++ = txdr_unsigned(uiop->uio_offset);
1268 *tl++ = txdr_unsigned(len);
1271 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READ, uiop->uio_td,
1272 nfs_vpcred(vp, ND_READ), &error));
1274 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1275 NFS_LATTR_NOSHRINK));
1276 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1277 eof = fxdr_unsigned(int, *(tl + 1));
1279 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1281 NEGATIVEOUT(retlen = nfsm_strsiz(&info, nmp->nm_rsize));
1282 ERROROUT(nfsm_mtouio(&info, uiop, retlen));
1287 if (eof || retlen == 0) {
1290 } else if (retlen < len) {
1302 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, int *must_commit)
1306 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1307 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1308 int committed = NFSV3WRITE_FILESYNC;
1309 struct nfsm_info info;
1312 info.v3 = NFS_ISV3(vp);
1315 if (uiop->uio_iovcnt != 1)
1316 panic("nfs: writerpc iovcnt > 1");
1319 tsiz = uiop->uio_resid;
1320 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1323 nfsstats.rpccnt[NFSPROC_WRITE]++;
1324 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1325 nfsm_reqhead(&info, vp, NFSPROC_WRITE,
1326 NFSX_FH(info.v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1327 ERROROUT(nfsm_fhtom(&info, vp));
1329 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
1330 txdr_hyper(uiop->uio_offset, tl);
1332 *tl++ = txdr_unsigned(len);
1333 *tl++ = txdr_unsigned(*iomode);
1334 *tl = txdr_unsigned(len);
1338 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1339 /* Set both "begin" and "current" to non-garbage. */
1340 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1341 *tl++ = x; /* "begin offset" */
1342 *tl++ = x; /* "current offset" */
1343 x = txdr_unsigned(len);
1344 *tl++ = x; /* total to this offset */
1345 *tl = x; /* size of this write */
1347 ERROROUT(nfsm_uiotom(&info, uiop, len));
1348 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_WRITE, uiop->uio_td,
1349 nfs_vpcred(vp, ND_WRITE), &error));
1352 * The write RPC returns a before and after mtime. The
1353 * nfsm_wcc_data() macro checks the before n_mtime
1354 * against the before time and stores the after time
1355 * in the nfsnode's cached vattr and n_mtime field.
1356 * The NRMODIFIED bit will be set if the before
1357 * time did not match the original mtime.
1359 wccflag = NFSV3_WCCCHK;
1360 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
1362 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1363 rlen = fxdr_unsigned(int, *tl++);
1369 } else if (rlen < len) {
1370 backup = len - rlen;
1371 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1372 uiop->uio_iov->iov_len += backup;
1373 uiop->uio_offset -= backup;
1374 uiop->uio_resid += backup;
1377 commit = fxdr_unsigned(int, *tl++);
1380 * Return the lowest committment level
1381 * obtained by any of the RPCs.
1383 if (committed == NFSV3WRITE_FILESYNC)
1385 else if (committed == NFSV3WRITE_DATASYNC &&
1386 commit == NFSV3WRITE_UNSTABLE)
1388 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1389 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1391 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1392 } else if (bcmp((caddr_t)tl,
1393 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1395 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1400 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1409 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1410 committed = NFSV3WRITE_FILESYNC;
1411 *iomode = committed;
1413 uiop->uio_resid = tsiz;
1419 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1420 * mode set to specify the file type and the size field for rdev.
1423 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1426 struct nfsv2_sattr *sp;
1428 struct vnode *newvp = NULL;
1429 struct nfsnode *np = NULL;
1431 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1433 struct nfsm_info info;
1436 info.v3 = NFS_ISV3(dvp);
1438 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1439 rmajor = txdr_unsigned(vap->va_rmajor);
1440 rminor = txdr_unsigned(vap->va_rminor);
1441 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) {
1442 rmajor = nfs_xdrneg1;
1443 rminor = nfs_xdrneg1;
1445 return (EOPNOTSUPP);
1447 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1450 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1451 nfsm_reqhead(&info, dvp, NFSPROC_MKNOD,
1452 NFSX_FH(info.v3) + 4 * NFSX_UNSIGNED +
1453 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1454 ERROROUT(nfsm_fhtom(&info, dvp));
1455 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1458 tl = nfsm_build(&info, NFSX_UNSIGNED);
1459 *tl++ = vtonfsv3_type(vap->va_type);
1460 nfsm_v3attrbuild(&info, vap, FALSE);
1461 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1462 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
1463 *tl++ = txdr_unsigned(vap->va_rmajor);
1464 *tl = txdr_unsigned(vap->va_rminor);
1467 sp = nfsm_build(&info, NFSX_V2SATTR);
1468 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1469 sp->sa_uid = nfs_xdrneg1;
1470 sp->sa_gid = nfs_xdrneg1;
1471 sp->sa_size = makeudev(rmajor, rminor);
1472 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1473 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1475 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKNOD, cnp->cn_td,
1476 cnp->cn_cred, &error));
1478 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1484 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1485 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1491 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1502 VTONFS(dvp)->n_flag |= NLMODIFIED;
1504 VTONFS(dvp)->n_attrstamp = 0;
1510 * just call nfs_mknodrpc() to do the work.
1512 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1513 * struct componentname *a_cnp, struct vattr *a_vap)
1517 nfs_mknod(struct vop_old_mknod_args *ap)
1519 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1522 static u_long create_verf;
1524 * nfs file create call
1526 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1527 * struct componentname *a_cnp, struct vattr *a_vap)
1530 nfs_create(struct vop_old_create_args *ap)
1532 struct vnode *dvp = ap->a_dvp;
1533 struct vattr *vap = ap->a_vap;
1534 struct componentname *cnp = ap->a_cnp;
1535 struct nfsv2_sattr *sp;
1537 struct nfsnode *np = NULL;
1538 struct vnode *newvp = NULL;
1539 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1541 struct nfsm_info info;
1544 info.v3 = NFS_ISV3(dvp);
1547 * Oops, not for me..
1549 if (vap->va_type == VSOCK)
1550 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1552 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1555 if (vap->va_vaflags & VA_EXCLUSIVE)
1558 nfsstats.rpccnt[NFSPROC_CREATE]++;
1559 nfsm_reqhead(&info, dvp, NFSPROC_CREATE,
1560 NFSX_FH(info.v3) + 2 * NFSX_UNSIGNED +
1561 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1562 ERROROUT(nfsm_fhtom(&info, dvp));
1563 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1566 tl = nfsm_build(&info, NFSX_UNSIGNED);
1567 if (fmode & O_EXCL) {
1568 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1569 tl = nfsm_build(&info, NFSX_V3CREATEVERF);
1571 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
1572 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia)->sin_addr.s_addr;
1575 *tl++ = create_verf;
1576 *tl = ++create_verf;
1578 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1579 nfsm_v3attrbuild(&info, vap, FALSE);
1582 sp = nfsm_build(&info, NFSX_V2SATTR);
1583 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1584 sp->sa_uid = nfs_xdrneg1;
1585 sp->sa_gid = nfs_xdrneg1;
1587 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1588 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1590 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_CREATE, cnp->cn_td,
1591 cnp->cn_cred, &error));
1593 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1599 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1600 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1606 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1612 if (info.v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1613 KKASSERT(newvp == NULL);
1617 } else if (info.v3 && (fmode & O_EXCL)) {
1619 * We are normally called with only a partially initialized
1620 * VAP. Since the NFSv3 spec says that server may use the
1621 * file attributes to store the verifier, the spec requires
1622 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1623 * in atime, but we can't really assume that all servers will
1624 * so we ensure that our SETATTR sets both atime and mtime.
1626 if (vap->va_mtime.tv_sec == VNOVAL)
1627 vfs_timestamp(&vap->va_mtime);
1628 if (vap->va_atime.tv_sec == VNOVAL)
1629 vap->va_atime = vap->va_mtime;
1630 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1634 * The new np may have enough info for access
1635 * checks, make sure rucred and wucred are
1636 * initialized for read and write rpc's.
1639 if (np->n_rucred == NULL)
1640 np->n_rucred = crhold(cnp->cn_cred);
1641 if (np->n_wucred == NULL)
1642 np->n_wucred = crhold(cnp->cn_cred);
1647 VTONFS(dvp)->n_flag |= NLMODIFIED;
1649 VTONFS(dvp)->n_attrstamp = 0;
1654 * nfs file remove call
1655 * To try and make nfs semantics closer to ufs semantics, a file that has
1656 * other processes using the vnode is renamed instead of removed and then
1657 * removed later on the last close.
1658 * - If v_sysref.refcnt > 1
1659 * If a rename is not already in the works
1660 * call nfs_sillyrename() to set it up
1664 * nfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
1665 * struct componentname *a_cnp)
1668 nfs_remove(struct vop_old_remove_args *ap)
1670 struct vnode *vp = ap->a_vp;
1671 struct vnode *dvp = ap->a_dvp;
1672 struct componentname *cnp = ap->a_cnp;
1673 struct nfsnode *np = VTONFS(vp);
1678 if (vp->v_sysref.refcnt < 1)
1679 panic("nfs_remove: bad v_sysref.refcnt");
1681 if (vp->v_type == VDIR)
1683 else if (vp->v_sysref.refcnt == 1 || (np->n_sillyrename &&
1684 VOP_GETATTR(vp, &vattr) == 0 &&
1685 vattr.va_nlink > 1)) {
1687 * throw away biocache buffers, mainly to avoid
1688 * unnecessary delayed writes later.
1690 error = nfs_vinvalbuf(vp, 0, 1);
1693 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1694 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1696 * Kludge City: If the first reply to the remove rpc is lost..
1697 * the reply to the retransmitted request will be ENOENT
1698 * since the file was in fact removed
1699 * Therefore, we cheat and return success.
1701 if (error == ENOENT)
1703 } else if (!np->n_sillyrename) {
1704 error = nfs_sillyrename(dvp, vp, cnp);
1706 np->n_attrstamp = 0;
1711 * nfs file remove rpc called from nfs_inactive
1714 nfs_removeit(struct sillyrename *sp)
1716 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1721 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1724 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1725 struct ucred *cred, struct thread *td)
1727 int error = 0, wccflag = NFSV3_WCCRATTR;
1728 struct nfsm_info info;
1731 info.v3 = NFS_ISV3(dvp);
1733 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1734 nfsm_reqhead(&info, dvp, NFSPROC_REMOVE,
1735 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1736 ERROROUT(nfsm_fhtom(&info, dvp));
1737 ERROROUT(nfsm_strtom(&info, name, namelen, NFS_MAXNAMLEN));
1738 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_REMOVE, td, cred, &error));
1740 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1745 VTONFS(dvp)->n_flag |= NLMODIFIED;
1747 VTONFS(dvp)->n_attrstamp = 0;
1752 * nfs file rename call
1754 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1755 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1756 * struct vnode *a_tvp, struct componentname *a_tcnp)
1759 nfs_rename(struct vop_old_rename_args *ap)
1761 struct vnode *fvp = ap->a_fvp;
1762 struct vnode *tvp = ap->a_tvp;
1763 struct vnode *fdvp = ap->a_fdvp;
1764 struct vnode *tdvp = ap->a_tdvp;
1765 struct componentname *tcnp = ap->a_tcnp;
1766 struct componentname *fcnp = ap->a_fcnp;
1769 /* Check for cross-device rename */
1770 if ((fvp->v_mount != tdvp->v_mount) ||
1771 (tvp && (fvp->v_mount != tvp->v_mount))) {
1777 * We shouldn't have to flush fvp on rename for most server-side
1778 * filesystems as the file handle should not change. Unfortunately
1779 * the inode for some filesystems (msdosfs) might be tied to the
1780 * file name or directory position so to be completely safe
1781 * vfs.nfs.flush_on_rename is set by default. Clear to improve
1784 * We must flush tvp on rename because it might become stale on the
1785 * server after the rename.
1787 if (nfs_flush_on_rename)
1788 VOP_FSYNC(fvp, MNT_WAIT);
1790 VOP_FSYNC(tvp, MNT_WAIT);
1793 * If the tvp exists and is in use, sillyrename it before doing the
1794 * rename of the new file over it.
1796 * XXX Can't sillyrename a directory.
1798 * We do not attempt to do any namecache purges in this old API
1799 * routine. The new API compat functions have access to the actual
1800 * namecache structures and will do it for us.
1802 if (tvp && tvp->v_sysref.refcnt > 1 && !VTONFS(tvp)->n_sillyrename &&
1803 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1810 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1811 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1824 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1826 if (error == ENOENT)
1832 * nfs file rename rpc called from nfs_remove() above
1835 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1836 struct sillyrename *sp)
1838 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1839 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1843 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1846 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1847 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1848 struct ucred *cred, struct thread *td)
1850 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1851 struct nfsm_info info;
1854 info.v3 = NFS_ISV3(fdvp);
1856 nfsstats.rpccnt[NFSPROC_RENAME]++;
1857 nfsm_reqhead(&info, fdvp, NFSPROC_RENAME,
1858 (NFSX_FH(info.v3) + NFSX_UNSIGNED)*2 +
1859 nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen));
1860 ERROROUT(nfsm_fhtom(&info, fdvp));
1861 ERROROUT(nfsm_strtom(&info, fnameptr, fnamelen, NFS_MAXNAMLEN));
1862 ERROROUT(nfsm_fhtom(&info, tdvp));
1863 ERROROUT(nfsm_strtom(&info, tnameptr, tnamelen, NFS_MAXNAMLEN));
1864 NEGKEEPOUT(nfsm_request(&info, fdvp, NFSPROC_RENAME, td, cred, &error));
1866 ERROROUT(nfsm_wcc_data(&info, fdvp, &fwccflag));
1867 ERROROUT(nfsm_wcc_data(&info, tdvp, &twccflag));
1872 VTONFS(fdvp)->n_flag |= NLMODIFIED;
1873 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1875 VTONFS(fdvp)->n_attrstamp = 0;
1877 VTONFS(tdvp)->n_attrstamp = 0;
1882 * nfs hard link create call
1884 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1885 * struct componentname *a_cnp)
1888 nfs_link(struct vop_old_link_args *ap)
1890 struct vnode *vp = ap->a_vp;
1891 struct vnode *tdvp = ap->a_tdvp;
1892 struct componentname *cnp = ap->a_cnp;
1893 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1894 struct nfsm_info info;
1896 if (vp->v_mount != tdvp->v_mount) {
1901 * The attribute cache may get out of sync with the server on link.
1902 * Pushing writes to the server before handle was inherited from
1903 * long long ago and it is unclear if we still need to do this.
1906 if (nfs_flush_on_hlink)
1907 VOP_FSYNC(vp, MNT_WAIT);
1910 info.v3 = NFS_ISV3(vp);
1912 nfsstats.rpccnt[NFSPROC_LINK]++;
1913 nfsm_reqhead(&info, vp, NFSPROC_LINK,
1914 NFSX_FH(info.v3) * 2 + NFSX_UNSIGNED +
1915 nfsm_rndup(cnp->cn_namelen));
1916 ERROROUT(nfsm_fhtom(&info, vp));
1917 ERROROUT(nfsm_fhtom(&info, tdvp));
1918 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1920 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_LINK, cnp->cn_td,
1921 cnp->cn_cred, &error));
1923 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1924 NFS_LATTR_NOSHRINK));
1925 ERROROUT(nfsm_wcc_data(&info, tdvp, &wccflag));
1930 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1932 VTONFS(vp)->n_attrstamp = 0;
1934 VTONFS(tdvp)->n_attrstamp = 0;
1936 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1938 if (error == EEXIST)
1944 * nfs symbolic link create call
1946 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1947 * struct componentname *a_cnp, struct vattr *a_vap,
1951 nfs_symlink(struct vop_old_symlink_args *ap)
1953 struct vnode *dvp = ap->a_dvp;
1954 struct vattr *vap = ap->a_vap;
1955 struct componentname *cnp = ap->a_cnp;
1956 struct nfsv2_sattr *sp;
1957 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1958 struct vnode *newvp = NULL;
1959 struct nfsm_info info;
1962 info.v3 = NFS_ISV3(dvp);
1964 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1965 slen = strlen(ap->a_target);
1966 nfsm_reqhead(&info, dvp, NFSPROC_SYMLINK,
1967 NFSX_FH(info.v3) + 2*NFSX_UNSIGNED +
1968 nfsm_rndup(cnp->cn_namelen) +
1969 nfsm_rndup(slen) + NFSX_SATTR(info.v3));
1970 ERROROUT(nfsm_fhtom(&info, dvp));
1971 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1974 nfsm_v3attrbuild(&info, vap, FALSE);
1976 ERROROUT(nfsm_strtom(&info, ap->a_target, slen, NFS_MAXPATHLEN));
1978 sp = nfsm_build(&info, NFSX_V2SATTR);
1979 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1980 sp->sa_uid = nfs_xdrneg1;
1981 sp->sa_gid = nfs_xdrneg1;
1982 sp->sa_size = nfs_xdrneg1;
1983 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1984 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1988 * Issue the NFS request and get the rpc response.
1990 * Only NFSv3 responses returning an error of 0 actually return
1991 * a file handle that can be converted into newvp without having
1992 * to do an extra lookup rpc.
1994 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_SYMLINK, cnp->cn_td,
1995 cnp->cn_cred, &error));
1998 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2000 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2004 * out code jumps -> here, mrep is also freed.
2012 * If we get an EEXIST error, silently convert it to no-error
2013 * in case of an NFS retry.
2015 if (error == EEXIST)
2019 * If we do not have (or no longer have) an error, and we could
2020 * not extract the newvp from the response due to the request being
2021 * NFSv2 or the error being EEXIST. We have to do a lookup in order
2022 * to obtain a newvp to return.
2024 if (error == 0 && newvp == NULL) {
2025 struct nfsnode *np = NULL;
2027 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2028 cnp->cn_cred, cnp->cn_td, &np);
2038 VTONFS(dvp)->n_flag |= NLMODIFIED;
2040 VTONFS(dvp)->n_attrstamp = 0;
2047 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2048 * struct componentname *a_cnp, struct vattr *a_vap)
2051 nfs_mkdir(struct vop_old_mkdir_args *ap)
2053 struct vnode *dvp = ap->a_dvp;
2054 struct vattr *vap = ap->a_vap;
2055 struct componentname *cnp = ap->a_cnp;
2056 struct nfsv2_sattr *sp;
2057 struct nfsnode *np = NULL;
2058 struct vnode *newvp = NULL;
2060 int error = 0, wccflag = NFSV3_WCCRATTR;
2063 struct nfsm_info info;
2066 info.v3 = NFS_ISV3(dvp);
2068 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
2071 len = cnp->cn_namelen;
2072 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2073 nfsm_reqhead(&info, dvp, NFSPROC_MKDIR,
2074 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2075 nfsm_rndup(len) + NFSX_SATTR(info.v3));
2076 ERROROUT(nfsm_fhtom(&info, dvp));
2077 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
2079 nfsm_v3attrbuild(&info, vap, FALSE);
2081 sp = nfsm_build(&info, NFSX_V2SATTR);
2082 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2083 sp->sa_uid = nfs_xdrneg1;
2084 sp->sa_gid = nfs_xdrneg1;
2085 sp->sa_size = nfs_xdrneg1;
2086 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2087 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2089 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKDIR, cnp->cn_td,
2090 cnp->cn_cred, &error));
2092 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2095 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2100 VTONFS(dvp)->n_flag |= NLMODIFIED;
2102 VTONFS(dvp)->n_attrstamp = 0;
2104 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2105 * if we can succeed in looking up the directory.
2107 if (error == EEXIST || (!error && !gotvp)) {
2112 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2116 if (newvp->v_type != VDIR)
2129 * nfs remove directory call
2131 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2132 * struct componentname *a_cnp)
2135 nfs_rmdir(struct vop_old_rmdir_args *ap)
2137 struct vnode *vp = ap->a_vp;
2138 struct vnode *dvp = ap->a_dvp;
2139 struct componentname *cnp = ap->a_cnp;
2140 int error = 0, wccflag = NFSV3_WCCRATTR;
2141 struct nfsm_info info;
2144 info.v3 = NFS_ISV3(dvp);
2148 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2149 nfsm_reqhead(&info, dvp, NFSPROC_RMDIR,
2150 NFSX_FH(info.v3) + NFSX_UNSIGNED +
2151 nfsm_rndup(cnp->cn_namelen));
2152 ERROROUT(nfsm_fhtom(&info, dvp));
2153 ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2155 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_RMDIR, cnp->cn_td,
2156 cnp->cn_cred, &error));
2158 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2163 VTONFS(dvp)->n_flag |= NLMODIFIED;
2165 VTONFS(dvp)->n_attrstamp = 0;
2167 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2169 if (error == ENOENT)
2177 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2180 nfs_readdir(struct vop_readdir_args *ap)
2182 struct vnode *vp = ap->a_vp;
2183 struct nfsnode *np = VTONFS(vp);
2184 struct uio *uio = ap->a_uio;
2188 if (vp->v_type != VDIR)
2191 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
2195 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2196 * and then check that is still valid, or if this is an NQNFS mount
2197 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2198 * VOP_GETATTR() does not necessarily go to the wire.
2200 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2201 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2202 if (VOP_GETATTR(vp, &vattr) == 0 &&
2203 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2205 nfsstats.direofcache_hits++;
2211 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2212 * own cache coherency checks so we do not have to.
2214 tresid = uio->uio_resid;
2215 error = nfs_bioread(vp, uio, 0);
2217 if (!error && uio->uio_resid == tresid)
2218 nfsstats.direofcache_misses++;
2225 * Readdir rpc call. nfs_bioread->nfs_doio->nfs_readdirrpc.
2227 * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2228 * offset/block and converts the nfs formatted directory entries for userland
2229 * consumption as well as deals with offsets into the middle of blocks.
2230 * nfs_doio only deals with logical blocks. In particular, uio_offset will
2231 * be block-bounded. It must convert to cookies for the actual RPC.
2234 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2237 struct nfs_dirent *dp = NULL;
2242 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2243 struct nfsnode *dnp = VTONFS(vp);
2245 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2247 struct nfsm_info info;
2250 info.v3 = NFS_ISV3(vp);
2253 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2254 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2255 panic("nfs readdirrpc bad uio");
2259 * If there is no cookie, assume directory was stale.
2261 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2265 return (NFSERR_BAD_COOKIE);
2267 * Loop around doing readdir rpc's of size nm_readdirsize
2268 * truncated to a multiple of DIRBLKSIZ.
2269 * The stopping criteria is EOF or buffer full.
2271 while (more_dirs && bigenough) {
2272 nfsstats.rpccnt[NFSPROC_READDIR]++;
2273 nfsm_reqhead(&info, vp, NFSPROC_READDIR,
2274 NFSX_FH(info.v3) + NFSX_READDIR(info.v3));
2275 ERROROUT(nfsm_fhtom(&info, vp));
2277 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
2278 *tl++ = cookie.nfsuquad[0];
2279 *tl++ = cookie.nfsuquad[1];
2280 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2281 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2283 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
2284 *tl++ = cookie.nfsuquad[0];
2286 *tl = txdr_unsigned(nmp->nm_readdirsize);
2287 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIR,
2289 nfs_vpcred(vp, ND_READ), &error));
2291 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2292 NFS_LATTR_NOSHRINK));
2293 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2294 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2295 dnp->n_cookieverf.nfsuquad[1] = *tl;
2297 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2298 more_dirs = fxdr_unsigned(int, *tl);
2300 /* loop thru the dir entries, converting them to std form */
2301 while (more_dirs && bigenough) {
2303 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2304 fileno = fxdr_hyper(tl);
2305 len = fxdr_unsigned(int, *(tl + 2));
2307 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2308 fileno = fxdr_unsigned(u_quad_t, *tl++);
2309 len = fxdr_unsigned(int, *tl);
2311 if (len <= 0 || len > NFS_MAXNAMLEN) {
2319 * len is the number of bytes in the path element
2320 * name, not including the \0 termination.
2322 * tlen is the number of bytes w have to reserve for
2323 * the path element name.
2325 tlen = nfsm_rndup(len);
2327 tlen += 4; /* To ensure null termination */
2330 * If the entry would cross a DIRBLKSIZ boundary,
2331 * extend the previous nfs_dirent to cover the
2334 left = DIRBLKSIZ - blksiz;
2335 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2336 dp->nfs_reclen += left;
2337 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2338 uiop->uio_iov->iov_len -= left;
2339 uiop->uio_offset += left;
2340 uiop->uio_resid -= left;
2343 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2346 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2347 dp->nfs_ino = fileno;
2348 dp->nfs_namlen = len;
2349 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2350 dp->nfs_type = DT_UNKNOWN;
2351 blksiz += dp->nfs_reclen;
2352 if (blksiz == DIRBLKSIZ)
2354 uiop->uio_offset += sizeof(struct nfs_dirent);
2355 uiop->uio_resid -= sizeof(struct nfs_dirent);
2356 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2357 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2358 ERROROUT(nfsm_mtouio(&info, uiop, len));
2361 * The uiop has advanced by nfs_dirent + len
2362 * but really needs to advance by
2365 cp = uiop->uio_iov->iov_base;
2367 *cp = '\0'; /* null terminate */
2368 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2369 uiop->uio_iov->iov_len -= tlen;
2370 uiop->uio_offset += tlen;
2371 uiop->uio_resid -= tlen;
2374 * NFS strings must be rounded up (nfsm_myouio
2375 * handled that in the bigenough case).
2377 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2380 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2382 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2386 * If we were able to accomodate the last entry,
2387 * get the cookie for the next one. Otherwise
2388 * hold-over the cookie for the one we were not
2389 * able to accomodate.
2392 cookie.nfsuquad[0] = *tl++;
2394 cookie.nfsuquad[1] = *tl++;
2395 } else if (info.v3) {
2400 more_dirs = fxdr_unsigned(int, *tl);
2403 * If at end of rpc data, get the eof boolean
2406 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2407 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2413 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2414 * by increasing d_reclen for the last record.
2417 left = DIRBLKSIZ - blksiz;
2418 dp->nfs_reclen += left;
2419 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2420 uiop->uio_iov->iov_len -= left;
2421 uiop->uio_offset += left;
2422 uiop->uio_resid -= left;
2427 * We hit the end of the directory, update direofoffset.
2429 dnp->n_direofoffset = uiop->uio_offset;
2432 * There is more to go, insert the link cookie so the
2433 * next block can be read.
2435 if (uiop->uio_resid > 0)
2436 kprintf("EEK! readdirrpc resid > 0\n");
2437 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2445 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2448 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2451 struct nfs_dirent *dp;
2453 struct vnode *newvp;
2455 caddr_t dpossav1, dpossav2;
2457 struct mbuf *mdsav1, *mdsav2;
2459 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2460 struct nfsnode *dnp = VTONFS(vp), *np;
2463 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2464 int attrflag, fhsize;
2465 struct nchandle nch;
2466 struct nchandle dnch;
2467 struct nlcomponent nlc;
2468 struct nfsm_info info;
2477 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2478 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2479 panic("nfs readdirplusrpc bad uio");
2482 * Obtain the namecache record for the directory so we have something
2483 * to use as a basis for creating the entries. This function will
2484 * return a held (but not locked) ncp. The ncp may be disconnected
2485 * from the tree and cannot be used for upward traversals, and the
2486 * ncp may be unnamed. Note that other unrelated operations may
2487 * cause the ncp to be named at any time.
2489 cache_fromdvp(vp, NULL, 0, &dnch);
2490 bzero(&nlc, sizeof(nlc));
2494 * If there is no cookie, assume directory was stale.
2496 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2500 return (NFSERR_BAD_COOKIE);
2502 * Loop around doing readdir rpc's of size nm_readdirsize
2503 * truncated to a multiple of DIRBLKSIZ.
2504 * The stopping criteria is EOF or buffer full.
2506 while (more_dirs && bigenough) {
2507 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2508 nfsm_reqhead(&info, vp, NFSPROC_READDIRPLUS,
2509 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2510 ERROROUT(nfsm_fhtom(&info, vp));
2511 tl = nfsm_build(&info, 6 * NFSX_UNSIGNED);
2512 *tl++ = cookie.nfsuquad[0];
2513 *tl++ = cookie.nfsuquad[1];
2514 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2515 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2516 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2517 *tl = txdr_unsigned(nmp->nm_rsize);
2518 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIRPLUS,
2520 nfs_vpcred(vp, ND_READ), &error));
2521 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2522 NFS_LATTR_NOSHRINK));
2523 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2524 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2525 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2526 more_dirs = fxdr_unsigned(int, *tl);
2528 /* loop thru the dir entries, doctoring them to 4bsd form */
2529 while (more_dirs && bigenough) {
2530 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2531 fileno = fxdr_hyper(tl);
2532 len = fxdr_unsigned(int, *(tl + 2));
2533 if (len <= 0 || len > NFS_MAXNAMLEN) {
2539 tlen = nfsm_rndup(len);
2541 tlen += 4; /* To ensure null termination*/
2542 left = DIRBLKSIZ - blksiz;
2543 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2544 dp->nfs_reclen += left;
2545 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2546 uiop->uio_iov->iov_len -= left;
2547 uiop->uio_offset += left;
2548 uiop->uio_resid -= left;
2551 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2554 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2555 dp->nfs_ino = fileno;
2556 dp->nfs_namlen = len;
2557 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2558 dp->nfs_type = DT_UNKNOWN;
2559 blksiz += dp->nfs_reclen;
2560 if (blksiz == DIRBLKSIZ)
2562 uiop->uio_offset += sizeof(struct nfs_dirent);
2563 uiop->uio_resid -= sizeof(struct nfs_dirent);
2564 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2565 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2566 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2567 nlc.nlc_namelen = len;
2568 ERROROUT(nfsm_mtouio(&info, uiop, len));
2569 cp = uiop->uio_iov->iov_base;
2572 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2573 uiop->uio_iov->iov_len -= tlen;
2574 uiop->uio_offset += tlen;
2575 uiop->uio_resid -= tlen;
2577 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2579 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2581 cookie.nfsuquad[0] = *tl++;
2582 cookie.nfsuquad[1] = *tl++;
2587 * Since the attributes are before the file handle
2588 * (sigh), we must skip over the attributes and then
2589 * come back and get them.
2591 attrflag = fxdr_unsigned(int, *tl);
2593 dpossav1 = info.dpos;
2595 ERROROUT(nfsm_adv(&info, NFSX_V3FATTR));
2596 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2597 doit = fxdr_unsigned(int, *tl);
2599 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
2600 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2605 error = nfs_nget(vp->v_mount, fhp,
2613 if (doit && bigenough) {
2614 dpossav2 = info.dpos;
2615 info.dpos = dpossav1;
2618 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
2619 info.dpos = dpossav2;
2622 IFTODT(VTTOIF(np->n_vattr.va_type));
2624 kprintf("NFS/READDIRPLUS, ENTER %*.*s\n",
2625 nlc.nlc_namelen, nlc.nlc_namelen,
2627 nch = cache_nlookup(&dnch, &nlc);
2628 cache_setunresolved(&nch);
2629 nfs_cache_setvp(&nch, newvp,
2630 nfspos_cache_timeout);
2633 kprintf("NFS/READDIRPLUS, UNABLE TO ENTER"
2635 nlc.nlc_namelen, nlc.nlc_namelen,
2640 /* Just skip over the file handle */
2641 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2642 i = fxdr_unsigned(int, *tl);
2643 ERROROUT(nfsm_adv(&info, nfsm_rndup(i)));
2645 if (newvp != NULLVP) {
2652 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2653 more_dirs = fxdr_unsigned(int, *tl);
2656 * If at end of rpc data, get the eof boolean
2659 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2660 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2666 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2667 * by increasing d_reclen for the last record.
2670 left = DIRBLKSIZ - blksiz;
2671 dp->nfs_reclen += left;
2672 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2673 uiop->uio_iov->iov_len -= left;
2674 uiop->uio_offset += left;
2675 uiop->uio_resid -= left;
2679 * We are now either at the end of the directory or have filled the
2683 dnp->n_direofoffset = uiop->uio_offset;
2685 if (uiop->uio_resid > 0)
2686 kprintf("EEK! readdirplusrpc resid > 0\n");
2687 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2691 if (newvp != NULLVP) {
2704 * Silly rename. To make the NFS filesystem that is stateless look a little
2705 * more like the "ufs" a remove of an active vnode is translated to a rename
2706 * to a funny looking filename that is removed by nfs_inactive on the
2707 * nfsnode. There is the potential for another process on a different client
2708 * to create the same funny name between the nfs_lookitup() fails and the
2709 * nfs_rename() completes, but...
2712 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2714 struct sillyrename *sp;
2719 * We previously purged dvp instead of vp. I don't know why, it
2720 * completely destroys performance. We can't do it anyway with the
2721 * new VFS API since we would be breaking the namecache topology.
2723 cache_purge(vp); /* XXX */
2726 if (vp->v_type == VDIR)
2727 panic("nfs: sillyrename dir");
2729 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2730 M_NFSREQ, M_WAITOK);
2731 sp->s_cred = crdup(cnp->cn_cred);
2735 /* Fudge together a funny name */
2736 sp->s_namlen = ksprintf(sp->s_name, ".nfsA%08x4.4",
2737 (int)(intptr_t)cnp->cn_td);
2739 /* Try lookitups until we get one that isn't there */
2740 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2741 cnp->cn_td, NULL) == 0) {
2743 if (sp->s_name[4] > 'z') {
2748 error = nfs_renameit(dvp, cnp, sp);
2751 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2753 np->n_sillyrename = sp;
2758 kfree((caddr_t)sp, M_NFSREQ);
2763 * Look up a file name and optionally either update the file handle or
2764 * allocate an nfsnode, depending on the value of npp.
2765 * npp == NULL --> just do the lookup
2766 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2768 * *npp != NULL --> update the file handle in the vnode
2771 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2772 struct thread *td, struct nfsnode **npp)
2774 struct vnode *newvp = NULL;
2775 struct nfsnode *np, *dnp = VTONFS(dvp);
2776 int error = 0, fhlen, attrflag;
2778 struct nfsm_info info;
2781 info.v3 = NFS_ISV3(dvp);
2783 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2784 nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
2785 NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2786 ERROROUT(nfsm_fhtom(&info, dvp));
2787 ERROROUT(nfsm_strtom(&info, name, len, NFS_MAXNAMLEN));
2788 NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td, cred, &error));
2789 if (npp && !error) {
2790 NEGATIVEOUT(fhlen = nfsm_getfh(&info, &nfhp));
2793 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2794 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
2795 np->n_fhp = &np->n_fh;
2796 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2797 np->n_fhp =(nfsfh_t *)kmalloc(fhlen,M_NFSBIGFH,M_WAITOK);
2798 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2799 np->n_fhsize = fhlen;
2801 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2805 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2814 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
2815 NFS_LATTR_NOSHRINK));
2816 if (!attrflag && *npp == NULL) {
2826 ERROROUT(error = nfsm_loadattr(&info, newvp, NULL));
2832 if (npp && *npp == NULL) {
2847 * Nfs Version 3 commit rpc
2850 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2852 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2853 int error = 0, wccflag = NFSV3_WCCRATTR;
2854 struct nfsm_info info;
2860 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2862 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2863 nfsm_reqhead(&info, vp, NFSPROC_COMMIT, NFSX_FH(1));
2864 ERROROUT(nfsm_fhtom(&info, vp));
2865 tl = nfsm_build(&info, 3 * NFSX_UNSIGNED);
2866 txdr_hyper(offset, tl);
2868 *tl = txdr_unsigned(cnt);
2869 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_COMMIT, td,
2870 nfs_vpcred(vp, ND_WRITE), &error));
2871 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
2873 NULLOUT(tl = nfsm_dissect(&info, NFSX_V3WRITEVERF));
2874 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2875 NFSX_V3WRITEVERF)) {
2876 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2878 error = NFSERR_STALEWRITEVERF;
2889 * - make nfs_bmap() essentially a no-op that does no translation
2890 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2891 * (Maybe I could use the process's page mapping, but I was concerned that
2892 * Kernel Write might not be enabled and also figured copyout() would do
2893 * a lot more work than bcopy() and also it currently happens in the
2894 * context of the swapper process (2).
2896 * nfs_bmap(struct vnode *a_vp, off_t a_loffset,
2897 * off_t *a_doffsetp, int *a_runp, int *a_runb)
2900 nfs_bmap(struct vop_bmap_args *ap)
2902 if (ap->a_doffsetp != NULL)
2903 *ap->a_doffsetp = ap->a_loffset;
2904 if (ap->a_runp != NULL)
2906 if (ap->a_runb != NULL)
2914 * For async requests when nfsiod(s) are running, queue the request by
2915 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2919 nfs_strategy(struct vop_strategy_args *ap)
2921 struct bio *bio = ap->a_bio;
2923 struct buf *bp = bio->bio_buf;
2927 KASSERT(bp->b_cmd != BUF_CMD_DONE,
2928 ("nfs_strategy: buffer %p unexpectedly marked done", bp));
2929 KASSERT(BUF_REFCNT(bp) > 0,
2930 ("nfs_strategy: buffer %p not locked", bp));
2932 if (bio->bio_flags & BIO_SYNC)
2933 td = curthread; /* XXX */
2938 * We probably don't need to push an nbio any more since no
2939 * block conversion is required due to the use of 64 bit byte
2940 * offsets, but do it anyway.
2942 nbio = push_bio(bio);
2943 nbio->bio_offset = bio->bio_offset;
2946 * If the op is asynchronous and an i/o daemon is waiting
2947 * queue the request, wake it up and wait for completion
2948 * otherwise just do it ourselves.
2950 if ((bio->bio_flags & BIO_SYNC) || nfs_asyncio(ap->a_vp, nbio, td))
2951 error = nfs_doio(ap->a_vp, nbio, td);
2958 * NB Currently unsupported.
2960 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
2964 nfs_mmap(struct vop_mmap_args *ap)
2970 * fsync vnode op. Just call nfs_flush() with commit == 1.
2972 * nfs_fsync(struct vnode *a_vp, int a_waitfor)
2976 nfs_fsync(struct vop_fsync_args *ap)
2978 return (nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1));
2982 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
2983 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
2984 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
2985 * set the buffer contains data that has already been written to the server
2986 * and which now needs a commit RPC.
2988 * If commit is 0 we only take one pass and only flush buffers containing new
2991 * If commit is 1 we take two passes, issuing a commit RPC in the second
2994 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
2995 * to completely flush all pending data.
2997 * Note that the RB_SCAN code properly handles the case where the
2998 * callback might block and directly or indirectly (another thread) cause
2999 * the RB tree to change.
3002 #ifndef NFS_COMMITBVECSIZ
3003 #define NFS_COMMITBVECSIZ 16
3006 struct nfs_flush_info {
3007 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
3014 struct buf *bvary[NFS_COMMITBVECSIZ];
3020 static int nfs_flush_bp(struct buf *bp, void *data);
3021 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
3024 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
3026 struct nfsnode *np = VTONFS(vp);
3027 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3028 struct nfs_flush_info info;
3032 bzero(&info, sizeof(info));
3035 info.waitfor = waitfor;
3036 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
3038 lwkt_gettoken(&vlock, &vp->v_token);
3044 info.mode = NFI_FLUSHNEW;
3045 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3046 nfs_flush_bp, &info);
3049 * Take a second pass if committing and no error occured.
3050 * Clean up any left over collection (whether an error
3053 if (commit && error == 0) {
3054 info.mode = NFI_COMMIT;
3055 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3056 nfs_flush_bp, &info);
3058 error = nfs_flush_docommit(&info, error);
3062 * Wait for pending I/O to complete before checking whether
3063 * any further dirty buffers exist.
3065 while (waitfor == MNT_WAIT &&
3066 bio_track_active(&vp->v_track_write)) {
3067 error = bio_track_wait(&vp->v_track_write,
3068 info.slpflag, info.slptimeo);
3071 * We have to be able to break out if this
3072 * is an 'intr' mount.
3074 if (nfs_sigintr(nmp, NULL, td)) {
3080 * Since we do not process pending signals,
3081 * once we get a PCATCH our tsleep() will no
3082 * longer sleep, switch to a fixed timeout
3085 if (info.slpflag == PCATCH) {
3087 info.slptimeo = 2 * hz;
3094 * Loop if we are flushing synchronous as well as committing,
3095 * and dirty buffers are still present. Otherwise we might livelock.
3097 } while (waitfor == MNT_WAIT && commit &&
3098 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3101 * The callbacks have to return a negative error to terminate the
3108 * Deal with any error collection
3110 if (np->n_flag & NWRITEERR) {
3111 error = np->n_error;
3112 np->n_flag &= ~NWRITEERR;
3114 lwkt_reltoken(&vlock);
3120 nfs_flush_bp(struct buf *bp, void *data)
3122 struct nfs_flush_info *info = data;
3128 switch(info->mode) {
3130 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3131 if (error && info->loops && info->waitfor == MNT_WAIT) {
3132 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3134 lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3135 if (info->slpflag & PCATCH)
3136 lkflags |= LK_PCATCH;
3137 error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3143 * Ignore locking errors
3151 * The buffer may have changed out from under us, even if
3152 * we did not block (MPSAFE). Check again now that it is
3155 if (bp->b_vp == info->vp &&
3156 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) == B_DELWRI) {
3165 * Only process buffers in need of a commit which we can
3166 * immediately lock. This may prevent a buffer from being
3167 * committed, but the normal flush loop will block on the
3168 * same buffer so we shouldn't get into an endless loop.
3170 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3171 (B_DELWRI | B_NEEDCOMMIT)) {
3174 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
3178 * We must recheck after successfully locking the buffer.
3180 if (bp->b_vp != info->vp ||
3181 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3182 (B_DELWRI | B_NEEDCOMMIT)) {
3188 * NOTE: storing the bp in the bvary[] basically sets
3189 * it up for a commit operation.
3191 * We must call vfs_busy_pages() now so the commit operation
3192 * is interlocked with user modifications to memory mapped
3195 * Note: to avoid loopback deadlocks, we do not
3196 * assign b_runningbufspace.
3199 bp->b_cmd = BUF_CMD_WRITE;
3200 vfs_busy_pages(bp->b_vp, bp);
3201 info->bvary[info->bvsize] = bp;
3202 toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3203 if (info->bvsize == 0 || toff < info->beg_off)
3204 info->beg_off = toff;
3205 toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3206 if (info->bvsize == 0 || toff > info->end_off)
3207 info->end_off = toff;
3209 if (info->bvsize == NFS_COMMITBVECSIZ) {
3210 error = nfs_flush_docommit(info, 0);
3211 KKASSERT(info->bvsize == 0);
3219 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3229 if (info->bvsize > 0) {
3231 * Commit data on the server, as required. Note that
3232 * nfs_commit will use the vnode's cred for the commit.
3233 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3235 bytes = info->end_off - info->beg_off;
3236 if (bytes > 0x40000000)
3241 retv = nfs_commit(vp, info->beg_off,
3242 (int)bytes, info->td);
3243 if (retv == NFSERR_STALEWRITEVERF)
3244 nfs_clearcommit(vp->v_mount);
3248 * Now, either mark the blocks I/O done or mark the
3249 * blocks dirty, depending on whether the commit
3252 for (i = 0; i < info->bvsize; ++i) {
3253 bp = info->bvary[i];
3254 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3257 * Error, leave B_DELWRI intact
3259 vfs_unbusy_pages(bp);
3260 bp->b_cmd = BUF_CMD_DONE;
3264 * Success, remove B_DELWRI ( bundirty() ).
3266 * b_dirtyoff/b_dirtyend seem to be NFS
3267 * specific. We should probably move that
3268 * into bundirty(). XXX
3270 * We are faking an I/O write, we have to
3271 * start the transaction in order to
3272 * immediately biodone() it.
3275 bp->b_flags &= ~B_ERROR;
3276 bp->b_dirtyoff = bp->b_dirtyend = 0;
3277 biodone(&bp->b_bio1);
3286 * NFS advisory byte-level locks.
3287 * Currently unsupported.
3289 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3293 nfs_advlock(struct vop_advlock_args *ap)
3295 struct nfsnode *np = VTONFS(ap->a_vp);
3298 * The following kludge is to allow diskless support to work
3299 * until a real NFS lockd is implemented. Basically, just pretend
3300 * that this is a local lock.
3302 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3306 * Print out the contents of an nfsnode.
3308 * nfs_print(struct vnode *a_vp)
3311 nfs_print(struct vop_print_args *ap)
3313 struct vnode *vp = ap->a_vp;
3314 struct nfsnode *np = VTONFS(vp);
3316 kprintf("tag VT_NFS, fileid %lld fsid 0x%x",
3317 (long long)np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3318 if (vp->v_type == VFIFO)
3325 * nfs special file access vnode op.
3326 * Essentially just get vattr and then imitate iaccess() since the device is
3327 * local to the client.
3329 * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
3332 nfsspec_access(struct vop_access_args *ap)
3336 struct ucred *cred = ap->a_cred;
3337 struct vnode *vp = ap->a_vp;
3338 mode_t mode = ap->a_mode;
3344 * Disallow write attempts on filesystems mounted read-only;
3345 * unless the file is a socket, fifo, or a block or character
3346 * device resident on the filesystem.
3348 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3349 switch (vp->v_type) {
3359 * If you're the super-user,
3360 * you always get access.
3362 if (cred->cr_uid == 0)
3365 error = VOP_GETATTR(vp, vap);
3369 * Access check is based on only one of owner, group, public.
3370 * If not owner, then check group. If not a member of the
3371 * group, then check public access.
3373 if (cred->cr_uid != vap->va_uid) {
3375 gp = cred->cr_groups;
3376 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3377 if (vap->va_gid == *gp)
3383 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3388 * Read wrapper for special devices.
3390 * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3391 * struct ucred *a_cred)
3394 nfsspec_read(struct vop_read_args *ap)
3396 struct nfsnode *np = VTONFS(ap->a_vp);
3402 getnanotime(&np->n_atim);
3403 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3407 * Write wrapper for special devices.
3409 * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3410 * struct ucred *a_cred)
3413 nfsspec_write(struct vop_write_args *ap)
3415 struct nfsnode *np = VTONFS(ap->a_vp);
3421 getnanotime(&np->n_mtim);
3422 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3426 * Close wrapper for special devices.
3428 * Update the times on the nfsnode then do device close.
3430 * nfsspec_close(struct vnode *a_vp, int a_fflag)
3433 nfsspec_close(struct vop_close_args *ap)
3435 struct vnode *vp = ap->a_vp;
3436 struct nfsnode *np = VTONFS(vp);
3439 if (np->n_flag & (NACC | NUPD)) {
3441 if (vp->v_sysref.refcnt == 1 &&
3442 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3444 if (np->n_flag & NACC)
3445 vattr.va_atime = np->n_atim;
3446 if (np->n_flag & NUPD)
3447 vattr.va_mtime = np->n_mtim;
3448 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3451 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3455 * Read wrapper for fifos.
3457 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3458 * struct ucred *a_cred)
3461 nfsfifo_read(struct vop_read_args *ap)
3463 struct nfsnode *np = VTONFS(ap->a_vp);
3469 getnanotime(&np->n_atim);
3470 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3474 * Write wrapper for fifos.
3476 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3477 * struct ucred *a_cred)
3480 nfsfifo_write(struct vop_write_args *ap)
3482 struct nfsnode *np = VTONFS(ap->a_vp);
3488 getnanotime(&np->n_mtim);
3489 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3493 * Close wrapper for fifos.
3495 * Update the times on the nfsnode then do fifo close.
3497 * nfsfifo_close(struct vnode *a_vp, int a_fflag)
3500 nfsfifo_close(struct vop_close_args *ap)
3502 struct vnode *vp = ap->a_vp;
3503 struct nfsnode *np = VTONFS(vp);
3507 if (np->n_flag & (NACC | NUPD)) {
3509 if (np->n_flag & NACC)
3511 if (np->n_flag & NUPD)
3514 if (vp->v_sysref.refcnt == 1 &&
3515 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3517 if (np->n_flag & NACC)
3518 vattr.va_atime = np->n_atim;
3519 if (np->n_flag & NUPD)
3520 vattr.va_mtime = np->n_mtim;
3521 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3524 return (VOCALL(&fifo_vnode_vops, &ap->a_head));