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);
219 extern u_int32_t nfs_true, nfs_false;
220 extern u_int32_t nfs_xdrneg1;
221 extern struct nfsstats nfsstats;
222 extern nfstype nfsv3_type[9];
223 struct thread *nfs_iodwant[NFS_MAXASYNCDAEMON];
224 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
225 int nfs_numasync = 0;
227 SYSCTL_DECL(_vfs_nfs);
229 static int nfs_flush_on_rename = 1;
230 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_rename, CTLFLAG_RW,
231 &nfs_flush_on_rename, 0, "flush fvp prior to rename");
232 static int nfs_flush_on_hlink = 0;
233 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_hlink, CTLFLAG_RW,
234 &nfs_flush_on_hlink, 0, "flush fvp prior to hard link");
236 static int nfsaccess_cache_timeout = NFS_DEFATTRTIMO;
237 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
238 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
240 static int nfsneg_cache_timeout = NFS_MINATTRTIMO;
241 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
242 &nfsneg_cache_timeout, 0, "NFS NEGATIVE NAMECACHE timeout");
244 static int nfspos_cache_timeout = NFS_MINATTRTIMO;
245 SYSCTL_INT(_vfs_nfs, OID_AUTO, pos_cache_timeout, CTLFLAG_RW,
246 &nfspos_cache_timeout, 0, "NFS POSITIVE NAMECACHE timeout");
248 static int nfsv3_commit_on_close = 0;
249 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
250 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
252 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
253 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
255 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
256 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
259 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
260 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
261 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
263 nfs3_access_otw(struct vnode *vp, int wmode,
264 struct thread *td, struct ucred *cred)
268 int error = 0, attrflag;
270 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
271 caddr_t bpos, dpos, cp2;
275 struct nfsnode *np = VTONFS(vp);
277 nfsstats.rpccnt[NFSPROC_ACCESS]++;
278 nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
280 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
281 *tl = txdr_unsigned(wmode);
282 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
283 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
285 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
286 rmode = fxdr_unsigned(u_int32_t, *tl);
288 np->n_modeuid = cred->cr_uid;
289 np->n_modestamp = mycpu->gd_time_seconds;
297 * nfs access vnode op.
298 * For nfs version 2, just return ok. File accesses may fail later.
299 * For nfs version 3, use the access rpc to check accessibility. If file modes
300 * are changed on the server, accesses might still fail later.
302 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
305 nfs_access(struct vop_access_args *ap)
307 struct vnode *vp = ap->a_vp;
308 thread_t td = curthread;
310 u_int32_t mode, wmode;
311 int v3 = NFS_ISV3(vp);
312 struct nfsnode *np = VTONFS(vp);
315 * Disallow write attempts on filesystems mounted read-only;
316 * unless the file is a socket, fifo, or a block or character
317 * device resident on the filesystem.
319 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
320 switch (vp->v_type) {
330 * For nfs v3, check to see if we have done this recently, and if
331 * so return our cached result instead of making an ACCESS call.
332 * If not, do an access rpc, otherwise you are stuck emulating
333 * ufs_access() locally using the vattr. This may not be correct,
334 * since the server may apply other access criteria such as
335 * client uid-->server uid mapping that we do not know about.
338 if (ap->a_mode & VREAD)
339 mode = NFSV3ACCESS_READ;
342 if (vp->v_type != VDIR) {
343 if (ap->a_mode & VWRITE)
344 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
345 if (ap->a_mode & VEXEC)
346 mode |= NFSV3ACCESS_EXECUTE;
348 if (ap->a_mode & VWRITE)
349 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
351 if (ap->a_mode & VEXEC)
352 mode |= NFSV3ACCESS_LOOKUP;
354 /* XXX safety belt, only make blanket request if caching */
355 if (nfsaccess_cache_timeout > 0) {
356 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
357 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
358 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
364 * Does our cached result allow us to give a definite yes to
367 if (np->n_modestamp &&
368 (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
369 (ap->a_cred->cr_uid == np->n_modeuid) &&
370 ((np->n_mode & mode) == mode)) {
371 nfsstats.accesscache_hits++;
374 * Either a no, or a don't know. Go to the wire.
376 nfsstats.accesscache_misses++;
377 error = nfs3_access_otw(vp, wmode, td, ap->a_cred);
379 if ((np->n_mode & mode) != mode) {
385 if ((error = nfsspec_access(ap)) != 0)
389 * Attempt to prevent a mapped root from accessing a file
390 * which it shouldn't. We try to read a byte from the file
391 * if the user is root and the file is not zero length.
392 * After calling nfsspec_access, we should have the correct
395 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
396 && VTONFS(vp)->n_size > 0) {
403 auio.uio_iov = &aiov;
407 auio.uio_segflg = UIO_SYSSPACE;
408 auio.uio_rw = UIO_READ;
411 if (vp->v_type == VREG) {
412 error = nfs_readrpc(vp, &auio);
413 } else if (vp->v_type == VDIR) {
415 bp = kmalloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
417 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
418 error = nfs_readdirrpc(vp, &auio);
420 } else if (vp->v_type == VLNK) {
421 error = nfs_readlinkrpc(vp, &auio);
428 * [re]record creds for reading and/or writing if access
429 * was granted. Assume the NFS server will grant read access
430 * for execute requests.
433 if ((ap->a_mode & (VREAD|VEXEC)) && ap->a_cred != np->n_rucred) {
436 crfree(np->n_rucred);
437 np->n_rucred = ap->a_cred;
439 if ((ap->a_mode & VWRITE) && ap->a_cred != np->n_wucred) {
442 crfree(np->n_wucred);
443 np->n_wucred = ap->a_cred;
451 * Check to see if the type is ok
452 * and that deletion is not in progress.
453 * For paged in text files, you will need to flush the page cache
454 * if consistency is lost.
456 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
461 nfs_open(struct vop_open_args *ap)
463 struct vnode *vp = ap->a_vp;
464 struct nfsnode *np = VTONFS(vp);
468 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
470 kprintf("open eacces vtyp=%d\n",vp->v_type);
476 * Save valid creds for reading and writing for later RPCs.
478 if ((ap->a_mode & FREAD) && ap->a_cred != np->n_rucred) {
481 crfree(np->n_rucred);
482 np->n_rucred = ap->a_cred;
484 if ((ap->a_mode & FWRITE) && ap->a_cred != np->n_wucred) {
487 crfree(np->n_wucred);
488 np->n_wucred = ap->a_cred;
492 * Clear the attribute cache only if opening with write access. It
493 * is unclear if we should do this at all here, but we certainly
494 * should not clear the cache unconditionally simply because a file
497 if (ap->a_mode & FWRITE)
501 * For normal NFS, reconcile changes made locally verses
502 * changes made remotely. Note that VOP_GETATTR only goes
503 * to the wire if the cached attribute has timed out or been
506 * If local modifications have been made clear the attribute
507 * cache to force an attribute and modified time check. If
508 * GETATTR detects that the file has been changed by someone
509 * other then us it will set NRMODIFIED.
511 * If we are opening a directory and local changes have been
512 * made we have to invalidate the cache in order to ensure
513 * that we get the most up-to-date information from the
516 if (np->n_flag & NLMODIFIED) {
518 if (vp->v_type == VDIR) {
519 error = nfs_vinvalbuf(vp, V_SAVE, 1);
525 error = VOP_GETATTR(vp, &vattr);
528 if (np->n_flag & NRMODIFIED) {
529 if (vp->v_type == VDIR)
531 error = nfs_vinvalbuf(vp, V_SAVE, 1);
534 np->n_flag &= ~NRMODIFIED;
537 return (vop_stdopen(ap));
542 * What an NFS client should do upon close after writing is a debatable issue.
543 * Most NFS clients push delayed writes to the server upon close, basically for
545 * 1 - So that any write errors may be reported back to the client process
546 * doing the close system call. By far the two most likely errors are
547 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
548 * 2 - To put a worst case upper bound on cache inconsistency between
549 * multiple clients for the file.
550 * There is also a consistency problem for Version 2 of the protocol w.r.t.
551 * not being able to tell if other clients are writing a file concurrently,
552 * since there is no way of knowing if the changed modify time in the reply
553 * is only due to the write for this client.
554 * (NFS Version 3 provides weak cache consistency data in the reply that
555 * should be sufficient to detect and handle this case.)
557 * The current code does the following:
558 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
559 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
560 * or commit them (this satisfies 1 and 2 except for the
561 * case where the server crashes after this close but
562 * before the commit RPC, which is felt to be "good
563 * enough". Changing the last argument to nfs_flush() to
564 * a 1 would force a commit operation, if it is felt a
565 * commit is necessary now.
566 * for NQNFS - do nothing now, since 2 is dealt with via leases and
567 * 1 should be dealt with via an fsync() system call for
568 * cases where write errors are important.
570 * nfs_close(struct vnode *a_vp, int a_fflag)
574 nfs_close(struct vop_close_args *ap)
576 struct vnode *vp = ap->a_vp;
577 struct nfsnode *np = VTONFS(vp);
579 thread_t td = curthread;
581 if (vp->v_type == VREG) {
582 if (np->n_flag & NLMODIFIED) {
585 * Under NFSv3 we have dirty buffers to dispose of. We
586 * must flush them to the NFS server. We have the option
587 * of waiting all the way through the commit rpc or just
588 * waiting for the initial write. The default is to only
589 * wait through the initial write so the data is in the
590 * server's cache, which is roughly similar to the state
591 * a standard disk subsystem leaves the file in on close().
593 * We cannot clear the NLMODIFIED bit in np->n_flag due to
594 * potential races with other processes, and certainly
595 * cannot clear it if we don't commit.
597 int cm = nfsv3_commit_on_close ? 1 : 0;
598 error = nfs_flush(vp, MNT_WAIT, td, cm);
599 /* np->n_flag &= ~NLMODIFIED; */
601 error = nfs_vinvalbuf(vp, V_SAVE, 1);
605 if (np->n_flag & NWRITEERR) {
606 np->n_flag &= ~NWRITEERR;
615 * nfs getattr call from vfs.
617 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
620 nfs_getattr(struct vop_getattr_args *ap)
622 struct vnode *vp = ap->a_vp;
623 struct nfsnode *np = VTONFS(vp);
629 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
630 int v3 = NFS_ISV3(vp);
631 thread_t td = curthread;
634 * Update local times for special files.
636 if (np->n_flag & (NACC | NUPD))
639 * First look in the cache.
641 if (nfs_getattrcache(vp, ap->a_vap) == 0)
644 if (v3 && nfsaccess_cache_timeout > 0) {
645 nfsstats.accesscache_misses++;
646 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, nfs_vpcred(vp, ND_CHECK));
647 if (nfs_getattrcache(vp, ap->a_vap) == 0)
651 nfsstats.rpccnt[NFSPROC_GETATTR]++;
652 nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
654 nfsm_request(vp, NFSPROC_GETATTR, td, nfs_vpcred(vp, ND_CHECK));
656 nfsm_loadattr(vp, ap->a_vap);
666 * nfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
669 nfs_setattr(struct vop_setattr_args *ap)
671 struct vnode *vp = ap->a_vp;
672 struct nfsnode *np = VTONFS(vp);
673 struct vattr *vap = ap->a_vap;
676 thread_t td = curthread;
683 * Setting of flags is not supported.
685 if (vap->va_flags != VNOVAL)
689 * Disallow write attempts if the filesystem is mounted read-only.
691 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
692 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
693 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
694 (vp->v_mount->mnt_flag & MNT_RDONLY))
697 if (vap->va_size != VNOVAL) {
699 * truncation requested
701 switch (vp->v_type) {
708 if (vap->va_mtime.tv_sec == VNOVAL &&
709 vap->va_atime.tv_sec == VNOVAL &&
710 vap->va_mode == (mode_t)VNOVAL &&
711 vap->va_uid == (uid_t)VNOVAL &&
712 vap->va_gid == (gid_t)VNOVAL)
714 vap->va_size = VNOVAL;
718 * Disallow write attempts if the filesystem is
721 if (vp->v_mount->mnt_flag & MNT_RDONLY)
725 * This is nasty. The RPCs we send to flush pending
726 * data often return attribute information which is
727 * cached via a callback to nfs_loadattrcache(), which
728 * has the effect of changing our notion of the file
729 * size. Due to flushed appends and other operations
730 * the file size can be set to virtually anything,
731 * including values that do not match either the old
732 * or intended file size.
734 * When this condition is detected we must loop to
735 * try the operation again. Hopefully no more
736 * flushing is required on the loop so it works the
737 * second time around. THIS CASE ALMOST ALWAYS
742 error = nfs_meta_setsize(vp, td, vap->va_size);
744 if (np->n_flag & NLMODIFIED) {
745 if (vap->va_size == 0)
746 error = nfs_vinvalbuf(vp, 0, 1);
748 error = nfs_vinvalbuf(vp, V_SAVE, 1);
751 * note: this loop case almost always happens at
752 * least once per truncation.
754 if (error == 0 && np->n_size != vap->va_size)
756 np->n_vattr.va_size = vap->va_size;
759 } else if ((np->n_flag & NLMODIFIED) && vp->v_type == VREG) {
761 * What to do. If we are modifying the mtime we lose
762 * mtime detection of changes made by the server or other
763 * clients. But programs like rsync/rdist/cpdup are going
764 * to call utimes a lot. We don't want to piecemeal sync.
766 * For now sync if any prior remote changes were detected,
767 * but allow us to lose track of remote changes made during
768 * the utimes operation.
770 if (np->n_flag & NRMODIFIED)
771 error = nfs_vinvalbuf(vp, V_SAVE, 1);
775 if (vap->va_mtime.tv_sec != VNOVAL) {
776 np->n_mtime = vap->va_mtime.tv_sec;
780 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
783 * Sanity check if a truncation was issued. This should only occur
784 * if multiple processes are racing on the same file.
786 if (error == 0 && vap->va_size != VNOVAL &&
787 np->n_size != vap->va_size) {
788 kprintf("NFS ftruncate: server disagrees on the file size: %lld/%lld/%lld\n", tsize, vap->va_size, np->n_size);
791 if (error && vap->va_size != VNOVAL) {
792 np->n_size = np->n_vattr.va_size = tsize;
793 vnode_pager_setsize(vp, np->n_size);
799 * Do an nfs setattr rpc.
802 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
803 struct ucred *cred, struct thread *td)
805 struct nfsv2_sattr *sp;
806 struct nfsnode *np = VTONFS(vp);
809 caddr_t bpos, dpos, cp2;
811 int error = 0, wccflag = NFSV3_WCCRATTR;
812 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
813 int v3 = NFS_ISV3(vp);
815 nfsstats.rpccnt[NFSPROC_SETATTR]++;
816 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
819 nfsm_v3attrbuild(vap, TRUE);
820 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
823 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
824 if (vap->va_mode == (mode_t)VNOVAL)
825 sp->sa_mode = nfs_xdrneg1;
827 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
828 if (vap->va_uid == (uid_t)VNOVAL)
829 sp->sa_uid = nfs_xdrneg1;
831 sp->sa_uid = txdr_unsigned(vap->va_uid);
832 if (vap->va_gid == (gid_t)VNOVAL)
833 sp->sa_gid = nfs_xdrneg1;
835 sp->sa_gid = txdr_unsigned(vap->va_gid);
836 sp->sa_size = txdr_unsigned(vap->va_size);
837 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
838 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
840 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
843 nfsm_wcc_data(vp, wccflag);
845 nfsm_loadattr(vp, NULL);
853 nfs_cache_setvp(struct nchandle *nch, struct vnode *vp, int nctimeout)
859 cache_setvp(nch, vp);
860 cache_settimeout(nch, nctimeout);
864 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
865 * nfs_lookup() until all remaining new api calls are implemented.
867 * Resolve a namecache entry. This function is passed a locked ncp and
868 * must call nfs_cache_setvp() on it as appropriate to resolve the entry.
871 nfs_nresolve(struct vop_nresolve_args *ap)
873 struct thread *td = curthread;
874 struct namecache *ncp;
885 /******NFSM MACROS********/
886 struct mbuf *mb, *mrep, *mreq, *mb2, *md;
887 caddr_t bpos, dpos, cp, cp2;
894 if ((error = vget(dvp, LK_SHARED)) != 0)
899 nfsstats.lookupcache_misses++;
900 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
901 ncp = ap->a_nch->ncp;
903 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
904 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
906 nfsm_strtom(ncp->nc_name, len, NFS_MAXNAMLEN);
907 nfsm_request(dvp, NFSPROC_LOOKUP, td, ap->a_cred);
910 * Cache negatve lookups to reduce NFS traffic, but use
911 * a fast timeout. Otherwise use a timeout of 1 tick.
912 * XXX we should add a namecache flag for no-caching
913 * to uncache the negative hit as soon as possible, but
914 * we cannot simply destroy the entry because it is used
915 * as a placeholder by the caller.
918 nfs_cache_setvp(ap->a_nch, NULL, nfsneg_cache_timeout);
919 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
925 * Success, get the file handle, do various checks, and load
926 * post-operation data from the reply packet. Theoretically
927 * we should never be looking up "." so, theoretically, we
928 * should never get the same file handle as our directory. But
929 * we check anyway. XXX
931 * Note that no timeout is set for the positive cache hit. We
932 * assume, theoretically, that ESTALE returns will be dealt with
933 * properly to handle NFS races and in anycase we cannot depend
934 * on a timeout to deal with NFS open/create/excl issues so instead
935 * of a bad hack here the rest of the NFS client code needs to do
938 nfsm_getfh(fhp, fhsize, v3);
941 if (NFS_CMPFH(np, fhp, fhsize)) {
945 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
954 nfsm_postop_attr(nvp, attrflag, NFS_LATTR_NOSHRINK);
955 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
957 nfsm_loadattr(nvp, NULL);
959 nfs_cache_setvp(ap->a_nch, nvp, nfspos_cache_timeout);
973 * 'cached' nfs directory lookup
975 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
977 * nfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
978 * struct componentname *a_cnp)
981 nfs_lookup(struct vop_old_lookup_args *ap)
983 struct componentname *cnp = ap->a_cnp;
984 struct vnode *dvp = ap->a_dvp;
985 struct vnode **vpp = ap->a_vpp;
986 int flags = cnp->cn_flags;
991 struct nfsmount *nmp;
992 caddr_t bpos, dpos, cp2;
993 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
997 int lockparent, wantparent, error = 0, attrflag, fhsize;
998 int v3 = NFS_ISV3(dvp);
1001 * Read-only mount check and directory check.
1004 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1005 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
1008 if (dvp->v_type != VDIR)
1012 * Look it up in the cache. Note that ENOENT is only returned if we
1013 * previously entered a negative hit (see later on). The additional
1014 * nfsneg_cache_timeout check causes previously cached results to
1015 * be instantly ignored if the negative caching is turned off.
1017 lockparent = flags & CNP_LOCKPARENT;
1018 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1019 nmp = VFSTONFS(dvp->v_mount);
1027 nfsstats.lookupcache_misses++;
1028 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1029 len = cnp->cn_namelen;
1030 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
1031 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1032 nfsm_fhtom(dvp, v3);
1033 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1034 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
1036 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1040 nfsm_getfh(fhp, fhsize, v3);
1043 * Handle RENAME case...
1045 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1046 if (NFS_CMPFH(np, fhp, fhsize)) {
1050 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1057 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1058 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1060 nfsm_loadattr(newvp, NULL);
1065 cnp->cn_flags |= CNP_PDIRUNLOCK;
1070 if (flags & CNP_ISDOTDOT) {
1072 cnp->cn_flags |= CNP_PDIRUNLOCK;
1073 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1075 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1076 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1077 return (error); /* NOTE: return error from nget */
1081 error = vn_lock(dvp, LK_EXCLUSIVE);
1086 cnp->cn_flags |= CNP_PDIRUNLOCK;
1088 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1092 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1099 cnp->cn_flags |= CNP_PDIRUNLOCK;
1104 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1105 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1107 nfsm_loadattr(newvp, NULL);
1109 /* XXX MOVE TO nfs_nremove() */
1110 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1111 cnp->cn_nameiop != NAMEI_DELETE) {
1112 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1119 if (newvp != NULLVP) {
1123 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1124 cnp->cn_nameiop == NAMEI_RENAME) &&
1128 cnp->cn_flags |= CNP_PDIRUNLOCK;
1130 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1133 error = EJUSTRETURN;
1141 * Just call nfs_bioread() to do the work.
1143 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1144 * struct ucred *a_cred)
1147 nfs_read(struct vop_read_args *ap)
1149 struct vnode *vp = ap->a_vp;
1151 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1157 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1160 nfs_readlink(struct vop_readlink_args *ap)
1162 struct vnode *vp = ap->a_vp;
1164 if (vp->v_type != VLNK)
1166 return (nfs_bioread(vp, ap->a_uio, 0));
1170 * Do a readlink rpc.
1171 * Called by nfs_doio() from below the buffer cache.
1174 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1179 caddr_t bpos, dpos, cp2;
1180 int error = 0, len, attrflag;
1181 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1182 int v3 = NFS_ISV3(vp);
1184 nfsstats.rpccnt[NFSPROC_READLINK]++;
1185 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1187 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1189 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1191 nfsm_strsiz(len, NFS_MAXPATHLEN);
1192 if (len == NFS_MAXPATHLEN) {
1193 struct nfsnode *np = VTONFS(vp);
1194 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1197 nfsm_mtouio(uiop, len);
1209 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1214 caddr_t bpos, dpos, cp2;
1215 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1216 struct nfsmount *nmp;
1217 int error = 0, len, retlen, tsiz, eof, attrflag;
1218 int v3 = NFS_ISV3(vp);
1223 nmp = VFSTONFS(vp->v_mount);
1224 tsiz = uiop->uio_resid;
1225 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1228 nfsstats.rpccnt[NFSPROC_READ]++;
1229 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1230 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1232 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1234 txdr_hyper(uiop->uio_offset, tl);
1235 *(tl + 2) = txdr_unsigned(len);
1237 *tl++ = txdr_unsigned(uiop->uio_offset);
1238 *tl++ = txdr_unsigned(len);
1241 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1243 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1248 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1249 eof = fxdr_unsigned(int, *(tl + 1));
1251 nfsm_loadattr(vp, NULL);
1252 nfsm_strsiz(retlen, nmp->nm_rsize);
1253 nfsm_mtouio(uiop, retlen);
1257 if (eof || retlen == 0) {
1260 } else if (retlen < len) {
1272 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, int *must_commit)
1276 int32_t t1, t2, backup;
1277 caddr_t bpos, dpos, cp2;
1278 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1279 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1280 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1281 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1284 if (uiop->uio_iovcnt != 1)
1285 panic("nfs: writerpc iovcnt > 1");
1288 tsiz = uiop->uio_resid;
1289 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1292 nfsstats.rpccnt[NFSPROC_WRITE]++;
1293 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1294 nfsm_reqhead(vp, NFSPROC_WRITE,
1295 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1298 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1299 txdr_hyper(uiop->uio_offset, tl);
1301 *tl++ = txdr_unsigned(len);
1302 *tl++ = txdr_unsigned(*iomode);
1303 *tl = txdr_unsigned(len);
1307 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1308 /* Set both "begin" and "current" to non-garbage. */
1309 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1310 *tl++ = x; /* "begin offset" */
1311 *tl++ = x; /* "current offset" */
1312 x = txdr_unsigned(len);
1313 *tl++ = x; /* total to this offset */
1314 *tl = x; /* size of this write */
1316 nfsm_uiotom(uiop, len);
1317 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1320 * The write RPC returns a before and after mtime. The
1321 * nfsm_wcc_data() macro checks the before n_mtime
1322 * against the before time and stores the after time
1323 * in the nfsnode's cached vattr and n_mtime field.
1324 * The NRMODIFIED bit will be set if the before
1325 * time did not match the original mtime.
1327 wccflag = NFSV3_WCCCHK;
1328 nfsm_wcc_data(vp, wccflag);
1330 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1331 + NFSX_V3WRITEVERF);
1332 rlen = fxdr_unsigned(int, *tl++);
1337 } else if (rlen < len) {
1338 backup = len - rlen;
1339 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1340 uiop->uio_iov->iov_len += backup;
1341 uiop->uio_offset -= backup;
1342 uiop->uio_resid += backup;
1345 commit = fxdr_unsigned(int, *tl++);
1348 * Return the lowest committment level
1349 * obtained by any of the RPCs.
1351 if (committed == NFSV3WRITE_FILESYNC)
1353 else if (committed == NFSV3WRITE_DATASYNC &&
1354 commit == NFSV3WRITE_UNSTABLE)
1356 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1357 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1359 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1360 } else if (bcmp((caddr_t)tl,
1361 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1363 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1368 nfsm_loadattr(vp, NULL);
1376 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1377 committed = NFSV3WRITE_FILESYNC;
1378 *iomode = committed;
1380 uiop->uio_resid = tsiz;
1386 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1387 * mode set to specify the file type and the size field for rdev.
1390 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1393 struct nfsv2_sattr *sp;
1397 struct vnode *newvp = NULL;
1398 struct nfsnode *np = NULL;
1402 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1403 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1405 int v3 = NFS_ISV3(dvp);
1407 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1408 rmajor = txdr_unsigned(vap->va_rmajor);
1409 rminor = txdr_unsigned(vap->va_rminor);
1410 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) {
1411 rmajor = nfs_xdrneg1;
1412 rminor = nfs_xdrneg1;
1414 return (EOPNOTSUPP);
1416 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1419 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1420 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1421 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1422 nfsm_fhtom(dvp, v3);
1423 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1425 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1426 *tl++ = vtonfsv3_type(vap->va_type);
1427 nfsm_v3attrbuild(vap, FALSE);
1428 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1429 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1430 *tl++ = txdr_unsigned(vap->va_rmajor);
1431 *tl = txdr_unsigned(vap->va_rminor);
1434 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1435 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1436 sp->sa_uid = nfs_xdrneg1;
1437 sp->sa_gid = nfs_xdrneg1;
1438 sp->sa_size = makeudev(rmajor, rminor);
1439 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1440 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1442 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1444 nfsm_mtofh(dvp, newvp, v3, gotvp);
1450 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1451 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1457 nfsm_wcc_data(dvp, wccflag);
1466 VTONFS(dvp)->n_flag |= NLMODIFIED;
1468 VTONFS(dvp)->n_attrstamp = 0;
1474 * just call nfs_mknodrpc() to do the work.
1476 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1477 * struct componentname *a_cnp, struct vattr *a_vap)
1481 nfs_mknod(struct vop_old_mknod_args *ap)
1483 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1486 static u_long create_verf;
1488 * nfs file create call
1490 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1491 * struct componentname *a_cnp, struct vattr *a_vap)
1494 nfs_create(struct vop_old_create_args *ap)
1496 struct vnode *dvp = ap->a_dvp;
1497 struct vattr *vap = ap->a_vap;
1498 struct componentname *cnp = ap->a_cnp;
1499 struct nfsv2_sattr *sp;
1503 struct nfsnode *np = NULL;
1504 struct vnode *newvp = NULL;
1505 caddr_t bpos, dpos, cp2;
1506 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1507 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1509 int v3 = NFS_ISV3(dvp);
1512 * Oops, not for me..
1514 if (vap->va_type == VSOCK)
1515 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1517 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1520 if (vap->va_vaflags & VA_EXCLUSIVE)
1523 nfsstats.rpccnt[NFSPROC_CREATE]++;
1524 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1525 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1526 nfsm_fhtom(dvp, v3);
1527 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1529 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1530 if (fmode & O_EXCL) {
1531 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1532 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1534 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
1535 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia)->sin_addr.s_addr;
1538 *tl++ = create_verf;
1539 *tl = ++create_verf;
1541 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1542 nfsm_v3attrbuild(vap, FALSE);
1545 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1546 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1547 sp->sa_uid = nfs_xdrneg1;
1548 sp->sa_gid = nfs_xdrneg1;
1550 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1551 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1553 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1555 nfsm_mtofh(dvp, newvp, v3, gotvp);
1561 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1562 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1568 nfsm_wcc_data(dvp, wccflag);
1572 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1573 KKASSERT(newvp == NULL);
1577 } else if (v3 && (fmode & O_EXCL)) {
1579 * We are normally called with only a partially initialized
1580 * VAP. Since the NFSv3 spec says that server may use the
1581 * file attributes to store the verifier, the spec requires
1582 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1583 * in atime, but we can't really assume that all servers will
1584 * so we ensure that our SETATTR sets both atime and mtime.
1586 if (vap->va_mtime.tv_sec == VNOVAL)
1587 vfs_timestamp(&vap->va_mtime);
1588 if (vap->va_atime.tv_sec == VNOVAL)
1589 vap->va_atime = vap->va_mtime;
1590 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1594 * The new np may have enough info for access
1595 * checks, make sure rucred and wucred are
1596 * initialized for read and write rpc's.
1599 if (np->n_rucred == NULL)
1600 np->n_rucred = crhold(cnp->cn_cred);
1601 if (np->n_wucred == NULL)
1602 np->n_wucred = crhold(cnp->cn_cred);
1607 VTONFS(dvp)->n_flag |= NLMODIFIED;
1609 VTONFS(dvp)->n_attrstamp = 0;
1614 * nfs file remove call
1615 * To try and make nfs semantics closer to ufs semantics, a file that has
1616 * other processes using the vnode is renamed instead of removed and then
1617 * removed later on the last close.
1618 * - If v_sysref.refcnt > 1
1619 * If a rename is not already in the works
1620 * call nfs_sillyrename() to set it up
1624 * nfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
1625 * struct componentname *a_cnp)
1628 nfs_remove(struct vop_old_remove_args *ap)
1630 struct vnode *vp = ap->a_vp;
1631 struct vnode *dvp = ap->a_dvp;
1632 struct componentname *cnp = ap->a_cnp;
1633 struct nfsnode *np = VTONFS(vp);
1638 if (vp->v_sysref.refcnt < 1)
1639 panic("nfs_remove: bad v_sysref.refcnt");
1641 if (vp->v_type == VDIR)
1643 else if (vp->v_sysref.refcnt == 1 || (np->n_sillyrename &&
1644 VOP_GETATTR(vp, &vattr) == 0 &&
1645 vattr.va_nlink > 1)) {
1647 * throw away biocache buffers, mainly to avoid
1648 * unnecessary delayed writes later.
1650 error = nfs_vinvalbuf(vp, 0, 1);
1653 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1654 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1656 * Kludge City: If the first reply to the remove rpc is lost..
1657 * the reply to the retransmitted request will be ENOENT
1658 * since the file was in fact removed
1659 * Therefore, we cheat and return success.
1661 if (error == ENOENT)
1663 } else if (!np->n_sillyrename) {
1664 error = nfs_sillyrename(dvp, vp, cnp);
1666 np->n_attrstamp = 0;
1671 * nfs file remove rpc called from nfs_inactive
1674 nfs_removeit(struct sillyrename *sp)
1676 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1681 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1684 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1685 struct ucred *cred, struct thread *td)
1690 caddr_t bpos, dpos, cp2;
1691 int error = 0, wccflag = NFSV3_WCCRATTR;
1692 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1693 int v3 = NFS_ISV3(dvp);
1695 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1696 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1697 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1698 nfsm_fhtom(dvp, v3);
1699 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1700 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1702 nfsm_wcc_data(dvp, wccflag);
1705 VTONFS(dvp)->n_flag |= NLMODIFIED;
1707 VTONFS(dvp)->n_attrstamp = 0;
1712 * nfs file rename call
1714 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1715 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1716 * struct vnode *a_tvp, struct componentname *a_tcnp)
1719 nfs_rename(struct vop_old_rename_args *ap)
1721 struct vnode *fvp = ap->a_fvp;
1722 struct vnode *tvp = ap->a_tvp;
1723 struct vnode *fdvp = ap->a_fdvp;
1724 struct vnode *tdvp = ap->a_tdvp;
1725 struct componentname *tcnp = ap->a_tcnp;
1726 struct componentname *fcnp = ap->a_fcnp;
1729 /* Check for cross-device rename */
1730 if ((fvp->v_mount != tdvp->v_mount) ||
1731 (tvp && (fvp->v_mount != tvp->v_mount))) {
1737 * We shouldn't have to flush fvp on rename for most server-side
1738 * filesystems as the file handle should not change. Unfortunately
1739 * the inode for some filesystems (msdosfs) might be tied to the
1740 * file name or directory position so to be completely safe
1741 * vfs.nfs.flush_on_rename is set by default. Clear to improve
1744 * We must flush tvp on rename because it might become stale on the
1745 * server after the rename.
1747 if (nfs_flush_on_rename)
1748 VOP_FSYNC(fvp, MNT_WAIT);
1750 VOP_FSYNC(tvp, MNT_WAIT);
1753 * If the tvp exists and is in use, sillyrename it before doing the
1754 * rename of the new file over it.
1756 * XXX Can't sillyrename a directory.
1758 * We do not attempt to do any namecache purges in this old API
1759 * routine. The new API compat functions have access to the actual
1760 * namecache structures and will do it for us.
1762 if (tvp && tvp->v_sysref.refcnt > 1 && !VTONFS(tvp)->n_sillyrename &&
1763 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1770 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1771 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1784 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1786 if (error == ENOENT)
1792 * nfs file rename rpc called from nfs_remove() above
1795 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1796 struct sillyrename *sp)
1798 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1799 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1803 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1806 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1807 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1808 struct ucred *cred, struct thread *td)
1813 caddr_t bpos, dpos, cp2;
1814 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1815 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1816 int v3 = NFS_ISV3(fdvp);
1818 nfsstats.rpccnt[NFSPROC_RENAME]++;
1819 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1820 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1821 nfsm_rndup(tnamelen));
1822 nfsm_fhtom(fdvp, v3);
1823 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1824 nfsm_fhtom(tdvp, v3);
1825 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1826 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1828 nfsm_wcc_data(fdvp, fwccflag);
1829 nfsm_wcc_data(tdvp, twccflag);
1833 VTONFS(fdvp)->n_flag |= NLMODIFIED;
1834 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1836 VTONFS(fdvp)->n_attrstamp = 0;
1838 VTONFS(tdvp)->n_attrstamp = 0;
1843 * nfs hard link create call
1845 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1846 * struct componentname *a_cnp)
1849 nfs_link(struct vop_old_link_args *ap)
1851 struct vnode *vp = ap->a_vp;
1852 struct vnode *tdvp = ap->a_tdvp;
1853 struct componentname *cnp = ap->a_cnp;
1857 caddr_t bpos, dpos, cp2;
1858 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1859 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1862 if (vp->v_mount != tdvp->v_mount) {
1867 * The attribute cache may get out of sync with the server on link.
1868 * Pushing writes to the server before handle was inherited from
1869 * long long ago and it is unclear if we still need to do this.
1872 if (nfs_flush_on_hlink)
1873 VOP_FSYNC(vp, MNT_WAIT);
1876 nfsstats.rpccnt[NFSPROC_LINK]++;
1877 nfsm_reqhead(vp, NFSPROC_LINK,
1878 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1880 nfsm_fhtom(tdvp, v3);
1881 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1882 nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1884 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1885 nfsm_wcc_data(tdvp, wccflag);
1889 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1891 VTONFS(vp)->n_attrstamp = 0;
1893 VTONFS(tdvp)->n_attrstamp = 0;
1895 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1897 if (error == EEXIST)
1903 * nfs symbolic link create call
1905 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1906 * struct componentname *a_cnp, struct vattr *a_vap,
1910 nfs_symlink(struct vop_old_symlink_args *ap)
1912 struct vnode *dvp = ap->a_dvp;
1913 struct vattr *vap = ap->a_vap;
1914 struct componentname *cnp = ap->a_cnp;
1915 struct nfsv2_sattr *sp;
1919 caddr_t bpos, dpos, cp2;
1920 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1921 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1922 struct vnode *newvp = NULL;
1923 int v3 = NFS_ISV3(dvp);
1925 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1926 slen = strlen(ap->a_target);
1927 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1928 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1929 nfsm_fhtom(dvp, v3);
1930 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1932 nfsm_v3attrbuild(vap, FALSE);
1934 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1936 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1937 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1938 sp->sa_uid = nfs_xdrneg1;
1939 sp->sa_gid = nfs_xdrneg1;
1940 sp->sa_size = nfs_xdrneg1;
1941 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1942 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1946 * Issue the NFS request and get the rpc response.
1948 * Only NFSv3 responses returning an error of 0 actually return
1949 * a file handle that can be converted into newvp without having
1950 * to do an extra lookup rpc.
1952 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1955 nfsm_mtofh(dvp, newvp, v3, gotvp);
1956 nfsm_wcc_data(dvp, wccflag);
1960 * out code jumps -> here, mrep is also freed.
1967 * If we get an EEXIST error, silently convert it to no-error
1968 * in case of an NFS retry.
1970 if (error == EEXIST)
1974 * If we do not have (or no longer have) an error, and we could
1975 * not extract the newvp from the response due to the request being
1976 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1977 * to obtain a newvp to return.
1979 if (error == 0 && newvp == NULL) {
1980 struct nfsnode *np = NULL;
1982 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1983 cnp->cn_cred, cnp->cn_td, &np);
1993 VTONFS(dvp)->n_flag |= NLMODIFIED;
1995 VTONFS(dvp)->n_attrstamp = 0;
2002 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2003 * struct componentname *a_cnp, struct vattr *a_vap)
2006 nfs_mkdir(struct vop_old_mkdir_args *ap)
2008 struct vnode *dvp = ap->a_dvp;
2009 struct vattr *vap = ap->a_vap;
2010 struct componentname *cnp = ap->a_cnp;
2011 struct nfsv2_sattr *sp;
2016 struct nfsnode *np = NULL;
2017 struct vnode *newvp = NULL;
2018 caddr_t bpos, dpos, cp2;
2019 int error = 0, wccflag = NFSV3_WCCRATTR;
2021 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2023 int v3 = NFS_ISV3(dvp);
2025 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
2028 len = cnp->cn_namelen;
2029 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2030 nfsm_reqhead(dvp, NFSPROC_MKDIR,
2031 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2032 nfsm_fhtom(dvp, v3);
2033 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
2035 nfsm_v3attrbuild(vap, FALSE);
2037 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2038 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2039 sp->sa_uid = nfs_xdrneg1;
2040 sp->sa_gid = nfs_xdrneg1;
2041 sp->sa_size = nfs_xdrneg1;
2042 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2043 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2045 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
2047 nfsm_mtofh(dvp, newvp, v3, gotvp);
2049 nfsm_wcc_data(dvp, wccflag);
2052 VTONFS(dvp)->n_flag |= NLMODIFIED;
2054 VTONFS(dvp)->n_attrstamp = 0;
2056 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2057 * if we can succeed in looking up the directory.
2059 if (error == EEXIST || (!error && !gotvp)) {
2064 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2068 if (newvp->v_type != VDIR)
2081 * nfs remove directory call
2083 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2084 * struct componentname *a_cnp)
2087 nfs_rmdir(struct vop_old_rmdir_args *ap)
2089 struct vnode *vp = ap->a_vp;
2090 struct vnode *dvp = ap->a_dvp;
2091 struct componentname *cnp = ap->a_cnp;
2095 caddr_t bpos, dpos, cp2;
2096 int error = 0, wccflag = NFSV3_WCCRATTR;
2097 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2098 int v3 = NFS_ISV3(dvp);
2102 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2103 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2104 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2105 nfsm_fhtom(dvp, v3);
2106 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2107 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2109 nfsm_wcc_data(dvp, wccflag);
2112 VTONFS(dvp)->n_flag |= NLMODIFIED;
2114 VTONFS(dvp)->n_attrstamp = 0;
2116 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2118 if (error == ENOENT)
2126 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2129 nfs_readdir(struct vop_readdir_args *ap)
2131 struct vnode *vp = ap->a_vp;
2132 struct nfsnode *np = VTONFS(vp);
2133 struct uio *uio = ap->a_uio;
2137 if (vp->v_type != VDIR)
2140 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
2144 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2145 * and then check that is still valid, or if this is an NQNFS mount
2146 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2147 * VOP_GETATTR() does not necessarily go to the wire.
2149 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2150 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2151 if (VOP_GETATTR(vp, &vattr) == 0 &&
2152 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2154 nfsstats.direofcache_hits++;
2160 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2161 * own cache coherency checks so we do not have to.
2163 tresid = uio->uio_resid;
2164 error = nfs_bioread(vp, uio, 0);
2166 if (!error && uio->uio_resid == tresid)
2167 nfsstats.direofcache_misses++;
2174 * Readdir rpc call. nfs_bioread->nfs_doio->nfs_readdirrpc.
2176 * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2177 * offset/block and converts the nfs formatted directory entries for userland
2178 * consumption as well as deals with offsets into the middle of blocks.
2179 * nfs_doio only deals with logical blocks. In particular, uio_offset will
2180 * be block-bounded. It must convert to cookies for the actual RPC.
2183 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2186 struct nfs_dirent *dp = NULL;
2191 caddr_t bpos, dpos, cp2;
2192 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2194 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2195 struct nfsnode *dnp = VTONFS(vp);
2197 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2199 int v3 = NFS_ISV3(vp);
2202 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2203 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2204 panic("nfs readdirrpc bad uio");
2208 * If there is no cookie, assume directory was stale.
2210 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2214 return (NFSERR_BAD_COOKIE);
2216 * Loop around doing readdir rpc's of size nm_readdirsize
2217 * truncated to a multiple of DIRBLKSIZ.
2218 * The stopping criteria is EOF or buffer full.
2220 while (more_dirs && bigenough) {
2221 nfsstats.rpccnt[NFSPROC_READDIR]++;
2222 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2226 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2227 *tl++ = cookie.nfsuquad[0];
2228 *tl++ = cookie.nfsuquad[1];
2229 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2230 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2232 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2233 *tl++ = cookie.nfsuquad[0];
2235 *tl = txdr_unsigned(nmp->nm_readdirsize);
2236 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2238 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2240 nfsm_dissect(tl, u_int32_t *,
2242 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2243 dnp->n_cookieverf.nfsuquad[1] = *tl;
2249 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2250 more_dirs = fxdr_unsigned(int, *tl);
2252 /* loop thru the dir entries, converting them to std form */
2253 while (more_dirs && bigenough) {
2255 nfsm_dissect(tl, u_int32_t *,
2257 fileno = fxdr_hyper(tl);
2258 len = fxdr_unsigned(int, *(tl + 2));
2260 nfsm_dissect(tl, u_int32_t *,
2262 fileno = fxdr_unsigned(u_quad_t, *tl++);
2263 len = fxdr_unsigned(int, *tl);
2265 if (len <= 0 || len > NFS_MAXNAMLEN) {
2272 * len is the number of bytes in the path element
2273 * name, not including the \0 termination.
2275 * tlen is the number of bytes w have to reserve for
2276 * the path element name.
2278 tlen = nfsm_rndup(len);
2280 tlen += 4; /* To ensure null termination */
2283 * If the entry would cross a DIRBLKSIZ boundary,
2284 * extend the previous nfs_dirent to cover the
2287 left = DIRBLKSIZ - blksiz;
2288 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2289 dp->nfs_reclen += left;
2290 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2291 uiop->uio_iov->iov_len -= left;
2292 uiop->uio_offset += left;
2293 uiop->uio_resid -= left;
2296 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2299 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2300 dp->nfs_ino = fileno;
2301 dp->nfs_namlen = len;
2302 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2303 dp->nfs_type = DT_UNKNOWN;
2304 blksiz += dp->nfs_reclen;
2305 if (blksiz == DIRBLKSIZ)
2307 uiop->uio_offset += sizeof(struct nfs_dirent);
2308 uiop->uio_resid -= sizeof(struct nfs_dirent);
2309 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2310 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2311 nfsm_mtouio(uiop, len);
2314 * The uiop has advanced by nfs_dirent + len
2315 * but really needs to advance by
2318 cp = uiop->uio_iov->iov_base;
2320 *cp = '\0'; /* null terminate */
2321 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2322 uiop->uio_iov->iov_len -= tlen;
2323 uiop->uio_offset += tlen;
2324 uiop->uio_resid -= tlen;
2327 * NFS strings must be rounded up (nfsm_myouio
2328 * handled that in the bigenough case).
2330 nfsm_adv(nfsm_rndup(len));
2333 nfsm_dissect(tl, u_int32_t *,
2336 nfsm_dissect(tl, u_int32_t *,
2341 * If we were able to accomodate the last entry,
2342 * get the cookie for the next one. Otherwise
2343 * hold-over the cookie for the one we were not
2344 * able to accomodate.
2347 cookie.nfsuquad[0] = *tl++;
2349 cookie.nfsuquad[1] = *tl++;
2355 more_dirs = fxdr_unsigned(int, *tl);
2358 * If at end of rpc data, get the eof boolean
2361 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2362 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2367 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2368 * by increasing d_reclen for the last record.
2371 left = DIRBLKSIZ - blksiz;
2372 dp->nfs_reclen += left;
2373 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2374 uiop->uio_iov->iov_len -= left;
2375 uiop->uio_offset += left;
2376 uiop->uio_resid -= left;
2381 * We hit the end of the directory, update direofoffset.
2383 dnp->n_direofoffset = uiop->uio_offset;
2386 * There is more to go, insert the link cookie so the
2387 * next block can be read.
2389 if (uiop->uio_resid > 0)
2390 kprintf("EEK! readdirrpc resid > 0\n");
2391 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2399 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2402 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2405 struct nfs_dirent *dp;
2409 struct vnode *newvp;
2411 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2412 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2414 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2415 struct nfsnode *dnp = VTONFS(vp), *np;
2418 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2419 int attrflag, fhsize;
2420 struct nchandle nch;
2421 struct nchandle dnch;
2422 struct nlcomponent nlc;
2428 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2429 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2430 panic("nfs readdirplusrpc bad uio");
2433 * Obtain the namecache record for the directory so we have something
2434 * to use as a basis for creating the entries. This function will
2435 * return a held (but not locked) ncp. The ncp may be disconnected
2436 * from the tree and cannot be used for upward traversals, and the
2437 * ncp may be unnamed. Note that other unrelated operations may
2438 * cause the ncp to be named at any time.
2440 cache_fromdvp(vp, NULL, 0, &dnch);
2441 bzero(&nlc, sizeof(nlc));
2445 * If there is no cookie, assume directory was stale.
2447 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2451 return (NFSERR_BAD_COOKIE);
2453 * Loop around doing readdir rpc's of size nm_readdirsize
2454 * truncated to a multiple of DIRBLKSIZ.
2455 * The stopping criteria is EOF or buffer full.
2457 while (more_dirs && bigenough) {
2458 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2459 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2460 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2462 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2463 *tl++ = cookie.nfsuquad[0];
2464 *tl++ = cookie.nfsuquad[1];
2465 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2466 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2467 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2468 *tl = txdr_unsigned(nmp->nm_rsize);
2469 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2470 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2475 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2476 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2477 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2478 more_dirs = fxdr_unsigned(int, *tl);
2480 /* loop thru the dir entries, doctoring them to 4bsd form */
2481 while (more_dirs && bigenough) {
2482 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2483 fileno = fxdr_hyper(tl);
2484 len = fxdr_unsigned(int, *(tl + 2));
2485 if (len <= 0 || len > NFS_MAXNAMLEN) {
2490 tlen = nfsm_rndup(len);
2492 tlen += 4; /* To ensure null termination*/
2493 left = DIRBLKSIZ - blksiz;
2494 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2495 dp->nfs_reclen += left;
2496 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2497 uiop->uio_iov->iov_len -= left;
2498 uiop->uio_offset += left;
2499 uiop->uio_resid -= left;
2502 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2505 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2506 dp->nfs_ino = fileno;
2507 dp->nfs_namlen = len;
2508 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2509 dp->nfs_type = DT_UNKNOWN;
2510 blksiz += dp->nfs_reclen;
2511 if (blksiz == DIRBLKSIZ)
2513 uiop->uio_offset += sizeof(struct nfs_dirent);
2514 uiop->uio_resid -= sizeof(struct nfs_dirent);
2515 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2516 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2517 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2518 nlc.nlc_namelen = len;
2519 nfsm_mtouio(uiop, len);
2520 cp = uiop->uio_iov->iov_base;
2523 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2524 uiop->uio_iov->iov_len -= tlen;
2525 uiop->uio_offset += tlen;
2526 uiop->uio_resid -= tlen;
2528 nfsm_adv(nfsm_rndup(len));
2529 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2531 cookie.nfsuquad[0] = *tl++;
2532 cookie.nfsuquad[1] = *tl++;
2537 * Since the attributes are before the file handle
2538 * (sigh), we must skip over the attributes and then
2539 * come back and get them.
2541 attrflag = fxdr_unsigned(int, *tl);
2545 nfsm_adv(NFSX_V3FATTR);
2546 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2547 doit = fxdr_unsigned(int, *tl);
2549 nfsm_getfh(fhp, fhsize, 1);
2550 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2555 error = nfs_nget(vp->v_mount, fhp,
2563 if (doit && bigenough) {
2568 nfsm_loadattr(newvp, NULL);
2572 IFTODT(VTTOIF(np->n_vattr.va_type));
2574 kprintf("NFS/READDIRPLUS, ENTER %*.*s\n",
2575 nlc.nlc_namelen, nlc.nlc_namelen,
2577 nch = cache_nlookup(&dnch, &nlc);
2578 cache_setunresolved(&nch);
2579 nfs_cache_setvp(&nch, newvp,
2580 nfspos_cache_timeout);
2583 kprintf("NFS/READDIRPLUS, UNABLE TO ENTER"
2585 nlc.nlc_namelen, nlc.nlc_namelen,
2590 /* Just skip over the file handle */
2591 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2592 i = fxdr_unsigned(int, *tl);
2593 nfsm_adv(nfsm_rndup(i));
2595 if (newvp != NULLVP) {
2602 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2603 more_dirs = fxdr_unsigned(int, *tl);
2606 * If at end of rpc data, get the eof boolean
2609 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2610 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2615 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2616 * by increasing d_reclen for the last record.
2619 left = DIRBLKSIZ - blksiz;
2620 dp->nfs_reclen += left;
2621 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2622 uiop->uio_iov->iov_len -= left;
2623 uiop->uio_offset += left;
2624 uiop->uio_resid -= left;
2628 * We are now either at the end of the directory or have filled the
2632 dnp->n_direofoffset = uiop->uio_offset;
2634 if (uiop->uio_resid > 0)
2635 kprintf("EEK! readdirplusrpc resid > 0\n");
2636 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2640 if (newvp != NULLVP) {
2653 * Silly rename. To make the NFS filesystem that is stateless look a little
2654 * more like the "ufs" a remove of an active vnode is translated to a rename
2655 * to a funny looking filename that is removed by nfs_inactive on the
2656 * nfsnode. There is the potential for another process on a different client
2657 * to create the same funny name between the nfs_lookitup() fails and the
2658 * nfs_rename() completes, but...
2661 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2663 struct sillyrename *sp;
2668 * We previously purged dvp instead of vp. I don't know why, it
2669 * completely destroys performance. We can't do it anyway with the
2670 * new VFS API since we would be breaking the namecache topology.
2672 cache_purge(vp); /* XXX */
2675 if (vp->v_type == VDIR)
2676 panic("nfs: sillyrename dir");
2678 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2679 M_NFSREQ, M_WAITOK);
2680 sp->s_cred = crdup(cnp->cn_cred);
2684 /* Fudge together a funny name */
2685 sp->s_namlen = ksprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
2687 /* Try lookitups until we get one that isn't there */
2688 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2689 cnp->cn_td, NULL) == 0) {
2691 if (sp->s_name[4] > 'z') {
2696 error = nfs_renameit(dvp, cnp, sp);
2699 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2701 np->n_sillyrename = sp;
2706 kfree((caddr_t)sp, M_NFSREQ);
2711 * Look up a file name and optionally either update the file handle or
2712 * allocate an nfsnode, depending on the value of npp.
2713 * npp == NULL --> just do the lookup
2714 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2716 * *npp != NULL --> update the file handle in the vnode
2719 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2720 struct thread *td, struct nfsnode **npp)
2725 struct vnode *newvp = NULL;
2726 struct nfsnode *np, *dnp = VTONFS(dvp);
2727 caddr_t bpos, dpos, cp2;
2728 int error = 0, fhlen, attrflag;
2729 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2731 int v3 = NFS_ISV3(dvp);
2733 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2734 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2735 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2736 nfsm_fhtom(dvp, v3);
2737 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2738 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2739 if (npp && !error) {
2740 nfsm_getfh(nfhp, fhlen, v3);
2743 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2744 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
2745 np->n_fhp = &np->n_fh;
2746 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2747 np->n_fhp =(nfsfh_t *)kmalloc(fhlen,M_NFSBIGFH,M_WAITOK);
2748 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2749 np->n_fhsize = fhlen;
2751 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2755 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2763 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
2764 if (!attrflag && *npp == NULL) {
2773 nfsm_loadattr(newvp, NULL);
2777 if (npp && *npp == NULL) {
2792 * Nfs Version 3 commit rpc
2795 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2800 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2801 caddr_t bpos, dpos, cp2;
2802 int error = 0, wccflag = NFSV3_WCCRATTR;
2803 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2805 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2807 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2808 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2810 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2811 txdr_hyper(offset, tl);
2813 *tl = txdr_unsigned(cnt);
2814 nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2815 nfsm_wcc_data(vp, wccflag);
2817 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2818 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2819 NFSX_V3WRITEVERF)) {
2820 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2822 error = NFSERR_STALEWRITEVERF;
2832 * - make nfs_bmap() essentially a no-op that does no translation
2833 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2834 * (Maybe I could use the process's page mapping, but I was concerned that
2835 * Kernel Write might not be enabled and also figured copyout() would do
2836 * a lot more work than bcopy() and also it currently happens in the
2837 * context of the swapper process (2).
2839 * nfs_bmap(struct vnode *a_vp, off_t a_loffset,
2840 * off_t *a_doffsetp, int *a_runp, int *a_runb)
2843 nfs_bmap(struct vop_bmap_args *ap)
2845 if (ap->a_doffsetp != NULL)
2846 *ap->a_doffsetp = ap->a_loffset;
2847 if (ap->a_runp != NULL)
2849 if (ap->a_runb != NULL)
2857 * For async requests when nfsiod(s) are running, queue the request by
2858 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2862 nfs_strategy(struct vop_strategy_args *ap)
2864 struct bio *bio = ap->a_bio;
2866 struct buf *bp = bio->bio_buf;
2870 KASSERT(bp->b_cmd != BUF_CMD_DONE,
2871 ("nfs_strategy: buffer %p unexpectedly marked done", bp));
2872 KASSERT(BUF_REFCNT(bp) > 0,
2873 ("nfs_strategy: buffer %p not locked", bp));
2875 if (bp->b_flags & B_ASYNC)
2878 td = curthread; /* XXX */
2881 * We probably don't need to push an nbio any more since no
2882 * block conversion is required due to the use of 64 bit byte
2883 * offsets, but do it anyway.
2885 nbio = push_bio(bio);
2886 nbio->bio_offset = bio->bio_offset;
2889 * If the op is asynchronous and an i/o daemon is waiting
2890 * queue the request, wake it up and wait for completion
2891 * otherwise just do it ourselves.
2893 if ((bp->b_flags & B_ASYNC) == 0 || nfs_asyncio(ap->a_vp, nbio, td))
2894 error = nfs_doio(ap->a_vp, nbio, td);
2901 * NB Currently unsupported.
2903 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
2907 nfs_mmap(struct vop_mmap_args *ap)
2913 * fsync vnode op. Just call nfs_flush() with commit == 1.
2915 * nfs_fsync(struct vnode *a_vp, int a_waitfor)
2919 nfs_fsync(struct vop_fsync_args *ap)
2921 return (nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1));
2925 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
2926 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
2927 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
2928 * set the buffer contains data that has already been written to the server
2929 * and which now needs a commit RPC.
2931 * If commit is 0 we only take one pass and only flush buffers containing new
2934 * If commit is 1 we take two passes, issuing a commit RPC in the second
2937 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
2938 * to completely flush all pending data.
2940 * Note that the RB_SCAN code properly handles the case where the
2941 * callback might block and directly or indirectly (another thread) cause
2942 * the RB tree to change.
2945 #ifndef NFS_COMMITBVECSIZ
2946 #define NFS_COMMITBVECSIZ 16
2949 struct nfs_flush_info {
2950 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
2957 struct buf *bvary[NFS_COMMITBVECSIZ];
2963 static int nfs_flush_bp(struct buf *bp, void *data);
2964 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
2967 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
2969 struct nfsnode *np = VTONFS(vp);
2970 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2971 struct nfs_flush_info info;
2974 bzero(&info, sizeof(info));
2977 info.waitfor = waitfor;
2978 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
2985 info.mode = NFI_FLUSHNEW;
2986 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2987 nfs_flush_bp, &info);
2990 * Take a second pass if committing and no error occured.
2991 * Clean up any left over collection (whether an error
2994 if (commit && error == 0) {
2995 info.mode = NFI_COMMIT;
2996 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2997 nfs_flush_bp, &info);
2999 error = nfs_flush_docommit(&info, error);
3003 * Wait for pending I/O to complete before checking whether
3004 * any further dirty buffers exist.
3006 while (waitfor == MNT_WAIT && vp->v_track_write.bk_active) {
3007 vp->v_track_write.bk_waitflag = 1;
3008 error = tsleep(&vp->v_track_write,
3009 info.slpflag, "nfsfsync", info.slptimeo);
3012 * We have to be able to break out if this
3013 * is an 'intr' mount.
3015 if (nfs_sigintr(nmp, NULL, td)) {
3021 * Since we do not process pending signals,
3022 * once we get a PCATCH our tsleep() will no
3023 * longer sleep, switch to a fixed timeout
3026 if (info.slpflag == PCATCH) {
3028 info.slptimeo = 2 * hz;
3035 * Loop if we are flushing synchronous as well as committing,
3036 * and dirty buffers are still present. Otherwise we might livelock.
3038 } while (waitfor == MNT_WAIT && commit &&
3039 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3042 * The callbacks have to return a negative error to terminate the
3049 * Deal with any error collection
3051 if (np->n_flag & NWRITEERR) {
3052 error = np->n_error;
3053 np->n_flag &= ~NWRITEERR;
3061 nfs_flush_bp(struct buf *bp, void *data)
3063 struct nfs_flush_info *info = data;
3068 switch(info->mode) {
3071 if (info->loops && info->waitfor == MNT_WAIT) {
3072 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3074 int lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3075 if (info->slpflag & PCATCH)
3076 lkflags |= LK_PCATCH;
3077 error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3081 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3084 KKASSERT(bp->b_vp == info->vp);
3086 if ((bp->b_flags & B_DELWRI) == 0)
3087 panic("nfs_fsync: not dirty");
3088 if (bp->b_flags & B_NEEDCOMMIT) {
3104 * Only process buffers in need of a commit which we can
3105 * immediately lock. This may prevent a buffer from being
3106 * committed, but the normal flush loop will block on the
3107 * same buffer so we shouldn't get into an endless loop.
3110 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3111 (B_DELWRI | B_NEEDCOMMIT) ||
3112 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
3117 KKASSERT(bp->b_vp == info->vp);
3121 * NOTE: storing the bp in the bvary[] basically sets
3122 * it up for a commit operation.
3124 * We must call vfs_busy_pages() now so the commit operation
3125 * is interlocked with user modifications to memory mapped
3128 * Note: to avoid loopback deadlocks, we do not
3129 * assign b_runningbufspace.
3131 bp->b_cmd = BUF_CMD_WRITE;
3132 vfs_busy_pages(bp->b_vp, bp);
3133 info->bvary[info->bvsize] = bp;
3134 toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3135 if (info->bvsize == 0 || toff < info->beg_off)
3136 info->beg_off = toff;
3137 toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3138 if (info->bvsize == 0 || toff > info->end_off)
3139 info->end_off = toff;
3141 if (info->bvsize == NFS_COMMITBVECSIZ) {
3142 error = nfs_flush_docommit(info, 0);
3143 KKASSERT(info->bvsize == 0);
3152 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3162 if (info->bvsize > 0) {
3164 * Commit data on the server, as required. Note that
3165 * nfs_commit will use the vnode's cred for the commit.
3166 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3168 bytes = info->end_off - info->beg_off;
3169 if (bytes > 0x40000000)
3174 retv = nfs_commit(vp, info->beg_off,
3175 (int)bytes, info->td);
3176 if (retv == NFSERR_STALEWRITEVERF)
3177 nfs_clearcommit(vp->v_mount);
3181 * Now, either mark the blocks I/O done or mark the
3182 * blocks dirty, depending on whether the commit
3185 for (i = 0; i < info->bvsize; ++i) {
3186 bp = info->bvary[i];
3187 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3190 * Error, leave B_DELWRI intact
3192 vfs_unbusy_pages(bp);
3193 bp->b_cmd = BUF_CMD_DONE;
3197 * Success, remove B_DELWRI ( bundirty() ).
3199 * b_dirtyoff/b_dirtyend seem to be NFS
3200 * specific. We should probably move that
3201 * into bundirty(). XXX
3203 * We are faking an I/O write, we have to
3204 * start the transaction in order to
3205 * immediately biodone() it.
3208 bp->b_flags |= B_ASYNC;
3210 bp->b_flags &= ~B_ERROR;
3211 bp->b_dirtyoff = bp->b_dirtyend = 0;
3213 biodone(&bp->b_bio1);
3222 * NFS advisory byte-level locks.
3223 * Currently unsupported.
3225 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3229 nfs_advlock(struct vop_advlock_args *ap)
3231 struct nfsnode *np = VTONFS(ap->a_vp);
3234 * The following kludge is to allow diskless support to work
3235 * until a real NFS lockd is implemented. Basically, just pretend
3236 * that this is a local lock.
3238 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3242 * Print out the contents of an nfsnode.
3244 * nfs_print(struct vnode *a_vp)
3247 nfs_print(struct vop_print_args *ap)
3249 struct vnode *vp = ap->a_vp;
3250 struct nfsnode *np = VTONFS(vp);
3252 kprintf("tag VT_NFS, fileid %lld fsid 0x%x",
3253 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3254 if (vp->v_type == VFIFO)
3261 * nfs special file access vnode op.
3262 * Essentially just get vattr and then imitate iaccess() since the device is
3263 * local to the client.
3265 * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
3268 nfsspec_access(struct vop_access_args *ap)
3272 struct ucred *cred = ap->a_cred;
3273 struct vnode *vp = ap->a_vp;
3274 mode_t mode = ap->a_mode;
3280 * Disallow write attempts on filesystems mounted read-only;
3281 * unless the file is a socket, fifo, or a block or character
3282 * device resident on the filesystem.
3284 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3285 switch (vp->v_type) {
3295 * If you're the super-user,
3296 * you always get access.
3298 if (cred->cr_uid == 0)
3301 error = VOP_GETATTR(vp, vap);
3305 * Access check is based on only one of owner, group, public.
3306 * If not owner, then check group. If not a member of the
3307 * group, then check public access.
3309 if (cred->cr_uid != vap->va_uid) {
3311 gp = cred->cr_groups;
3312 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3313 if (vap->va_gid == *gp)
3319 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3324 * Read wrapper for special devices.
3326 * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3327 * struct ucred *a_cred)
3330 nfsspec_read(struct vop_read_args *ap)
3332 struct nfsnode *np = VTONFS(ap->a_vp);
3338 getnanotime(&np->n_atim);
3339 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3343 * Write wrapper for special devices.
3345 * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3346 * struct ucred *a_cred)
3349 nfsspec_write(struct vop_write_args *ap)
3351 struct nfsnode *np = VTONFS(ap->a_vp);
3357 getnanotime(&np->n_mtim);
3358 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3362 * Close wrapper for special devices.
3364 * Update the times on the nfsnode then do device close.
3366 * nfsspec_close(struct vnode *a_vp, int a_fflag)
3369 nfsspec_close(struct vop_close_args *ap)
3371 struct vnode *vp = ap->a_vp;
3372 struct nfsnode *np = VTONFS(vp);
3375 if (np->n_flag & (NACC | NUPD)) {
3377 if (vp->v_sysref.refcnt == 1 &&
3378 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3380 if (np->n_flag & NACC)
3381 vattr.va_atime = np->n_atim;
3382 if (np->n_flag & NUPD)
3383 vattr.va_mtime = np->n_mtim;
3384 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3387 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3391 * Read wrapper for fifos.
3393 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3394 * struct ucred *a_cred)
3397 nfsfifo_read(struct vop_read_args *ap)
3399 struct nfsnode *np = VTONFS(ap->a_vp);
3405 getnanotime(&np->n_atim);
3406 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3410 * Write wrapper for fifos.
3412 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3413 * struct ucred *a_cred)
3416 nfsfifo_write(struct vop_write_args *ap)
3418 struct nfsnode *np = VTONFS(ap->a_vp);
3424 getnanotime(&np->n_mtim);
3425 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3429 * Close wrapper for fifos.
3431 * Update the times on the nfsnode then do fifo close.
3433 * nfsfifo_close(struct vnode *a_vp, int a_fflag)
3436 nfsfifo_close(struct vop_close_args *ap)
3438 struct vnode *vp = ap->a_vp;
3439 struct nfsnode *np = VTONFS(vp);
3443 if (np->n_flag & (NACC | NUPD)) {
3445 if (np->n_flag & NACC)
3447 if (np->n_flag & NUPD)
3450 if (vp->v_sysref.refcnt == 1 &&
3451 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3453 if (np->n_flag & NACC)
3454 vattr.va_atime = np->n_atim;
3455 if (np->n_flag & NUPD)
3456 vattr.va_mtime = np->n_mtim;
3457 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3460 return (VOCALL(&fifo_vnode_vops, &ap->a_head));