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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * vnode op calls for Sun NFS version 2 and 3
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/systm.h>
47 #include <sys/resourcevar.h>
49 #include <sys/mount.h>
52 #include <sys/malloc.h>
54 #include <sys/namei.h>
55 #include <sys/socket.h>
56 #include <sys/vnode.h>
57 #include <sys/dirent.h>
58 #include <sys/fcntl.h>
59 #include <sys/lockf.h>
61 #include <sys/sysctl.h>
62 #include <sys/signalvar.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_extern.h>
67 #include <vm/vm_object.h>
69 #include <fs/fifofs/fifo.h>
71 #include <rpc/rpcclnt.h>
73 #include <nfs/rpcv2.h>
74 #include <nfs/nfsproto.h>
75 #include <nfsclient/nfs.h>
76 #include <nfsclient/nfsnode.h>
77 #include <nfsclient/nfsmount.h>
78 #include <nfsclient/nfs_lock.h>
79 #include <nfs/xdr_subs.h>
80 #include <nfsclient/nfsm_subs.h>
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
91 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
92 * calls are not in getblk() and brelse() so that they would not be necessary
96 #define vfs_busy_pages(bp, f)
99 static vop_read_t nfsfifo_read;
100 static vop_write_t nfsfifo_write;
101 static vop_close_t nfsfifo_close;
102 static int nfs_flush(struct vnode *, int, struct thread *,
104 static int nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
106 static vop_lookup_t nfs_lookup;
107 static vop_create_t nfs_create;
108 static vop_mknod_t nfs_mknod;
109 static vop_open_t nfs_open;
110 static vop_close_t nfs_close;
111 static vop_access_t nfs_access;
112 static vop_getattr_t nfs_getattr;
113 static vop_setattr_t nfs_setattr;
114 static vop_read_t nfs_read;
115 static vop_fsync_t nfs_fsync;
116 static vop_remove_t nfs_remove;
117 static vop_link_t nfs_link;
118 static vop_rename_t nfs_rename;
119 static vop_mkdir_t nfs_mkdir;
120 static vop_rmdir_t nfs_rmdir;
121 static vop_symlink_t nfs_symlink;
122 static vop_readdir_t nfs_readdir;
123 static vop_strategy_t nfs_strategy;
124 static int nfs_lookitup(struct vnode *, const char *, int,
125 struct ucred *, struct thread *, struct nfsnode **);
126 static int nfs_sillyrename(struct vnode *, struct vnode *,
127 struct componentname *);
128 static vop_access_t nfsspec_access;
129 static vop_readlink_t nfs_readlink;
130 static vop_print_t nfs_print;
131 static vop_advlock_t nfs_advlock;
134 * Global vfs data structures for nfs
136 struct vop_vector nfs_vnodeops = {
137 .vop_default = &default_vnodeops,
138 .vop_access = nfs_access,
139 .vop_advlock = nfs_advlock,
140 .vop_close = nfs_close,
141 .vop_create = nfs_create,
142 .vop_fsync = nfs_fsync,
143 .vop_getattr = nfs_getattr,
144 .vop_getpages = nfs_getpages,
145 .vop_putpages = nfs_putpages,
146 .vop_inactive = nfs_inactive,
147 .vop_lease = VOP_NULL,
148 .vop_link = nfs_link,
149 .vop_lookup = nfs_lookup,
150 .vop_mkdir = nfs_mkdir,
151 .vop_mknod = nfs_mknod,
152 .vop_open = nfs_open,
153 .vop_print = nfs_print,
154 .vop_read = nfs_read,
155 .vop_readdir = nfs_readdir,
156 .vop_readlink = nfs_readlink,
157 .vop_reclaim = nfs_reclaim,
158 .vop_remove = nfs_remove,
159 .vop_rename = nfs_rename,
160 .vop_rmdir = nfs_rmdir,
161 .vop_setattr = nfs_setattr,
162 .vop_strategy = nfs_strategy,
163 .vop_symlink = nfs_symlink,
164 .vop_write = nfs_write,
167 struct vop_vector nfs_fifoops = {
168 .vop_default = &fifo_specops,
169 .vop_access = nfsspec_access,
170 .vop_close = nfsfifo_close,
171 .vop_fsync = nfs_fsync,
172 .vop_getattr = nfs_getattr,
173 .vop_inactive = nfs_inactive,
174 .vop_print = nfs_print,
175 .vop_read = nfsfifo_read,
176 .vop_reclaim = nfs_reclaim,
177 .vop_setattr = nfs_setattr,
178 .vop_write = nfsfifo_write,
181 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
182 struct componentname *cnp, struct vattr *vap);
183 static int nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
184 struct ucred *cred, struct thread *td);
185 static int nfs_renamerpc(struct vnode *fdvp, const char *fnameptr,
186 int fnamelen, struct vnode *tdvp,
187 const char *tnameptr, int tnamelen,
188 struct ucred *cred, struct thread *td);
189 static int nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
190 struct sillyrename *sp);
195 struct mtx nfs_iod_mtx;
196 struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
197 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
198 int nfs_numasync = 0;
199 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
201 SYSCTL_DECL(_vfs_nfs);
203 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
204 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
205 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
207 static int nfsv3_commit_on_close = 0;
208 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
209 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
211 static int nfs_clean_pages_on_close = 1;
212 SYSCTL_INT(_vfs_nfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
213 &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
215 int nfs_directio_enable = 0;
216 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
217 &nfs_directio_enable, 0, "Enable NFS directio");
220 * This sysctl allows other processes to mmap a file that has been opened
221 * O_DIRECT by a process. In general, having processes mmap the file while
222 * Direct IO is in progress can lead to Data Inconsistencies. But, we allow
223 * this by default to prevent DoS attacks - to prevent a malicious user from
224 * opening up files O_DIRECT preventing other users from mmap'ing these
225 * files. "Protected" environments where stricter consistency guarantees are
226 * required can disable this knob. The process that opened the file O_DIRECT
227 * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
230 int nfs_directio_allow_mmap = 1;
231 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
232 &nfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
235 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
236 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
238 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
239 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
242 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
243 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
244 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
248 * The list of locks after the description of the lock is the ordering
249 * of other locks acquired with the lock held.
250 * np->n_mtx : Protects the fields in the nfsnode.
252 VI_MTX (acquired indirectly)
253 * nmp->nm_mtx : Protects the fields in the nfsmount.
255 * nfs_iod_mtx : Global lock, protects shared nfsiod state.
256 * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
259 * rep->r_mtx : Protects the fields in an nfsreq.
263 nfs3_access_otw(struct vnode *vp, int wmode, struct thread *td,
268 int error = 0, attrflag;
270 struct mbuf *mreq, *mrep, *md, *mb;
273 struct nfsnode *np = VTONFS(vp);
275 nfsstats.rpccnt[NFSPROC_ACCESS]++;
276 mreq = nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
278 bpos = mtod(mb, caddr_t);
280 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
281 *tl = txdr_unsigned(wmode);
282 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
283 nfsm_postop_attr(vp, attrflag);
285 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
286 rmode = fxdr_unsigned(u_int32_t, *tl);
287 mtx_lock(&np->n_mtx);
289 np->n_modeuid = cred->cr_uid;
290 np->n_modestamp = time_second;
291 mtx_unlock(&np->n_mtx);
299 * nfs access vnode op.
300 * For nfs version 2, just return ok. File accesses may fail later.
301 * For nfs version 3, use the access rpc to check accessibility. If file modes
302 * are changed on the server, accesses might still fail later.
305 nfs_access(struct vop_access_args *ap)
307 struct vnode *vp = ap->a_vp;
309 u_int32_t mode, wmode;
310 int v3 = NFS_ISV3(vp);
311 struct nfsnode *np = VTONFS(vp);
314 * Disallow write attempts on filesystems mounted read-only;
315 * unless the file is a socket, fifo, or a block or character
316 * device resident on the filesystem.
318 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
319 switch (vp->v_type) {
329 * For nfs v3, check to see if we have done this recently, and if
330 * so return our cached result instead of making an ACCESS call.
331 * If not, do an access rpc, otherwise you are stuck emulating
332 * ufs_access() locally using the vattr. This may not be correct,
333 * since the server may apply other access criteria such as
334 * client uid-->server uid mapping that we do not know about.
337 if (ap->a_mode & VREAD)
338 mode = NFSV3ACCESS_READ;
341 if (vp->v_type != VDIR) {
342 if (ap->a_mode & VWRITE)
343 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
344 if (ap->a_mode & VEXEC)
345 mode |= NFSV3ACCESS_EXECUTE;
347 if (ap->a_mode & VWRITE)
348 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
350 if (ap->a_mode & VEXEC)
351 mode |= NFSV3ACCESS_LOOKUP;
353 /* XXX safety belt, only make blanket request if caching */
354 if (nfsaccess_cache_timeout > 0) {
355 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
356 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
357 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
363 * Does our cached result allow us to give a definite yes to
366 mtx_lock(&np->n_mtx);
367 if ((time_second < (np->n_modestamp + nfsaccess_cache_timeout)) &&
368 (ap->a_cred->cr_uid == np->n_modeuid) &&
369 ((np->n_mode & mode) == mode)) {
370 nfsstats.accesscache_hits++;
373 * Either a no, or a don't know. Go to the wire.
375 nfsstats.accesscache_misses++;
376 mtx_unlock(&np->n_mtx);
377 error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred);
378 mtx_lock(&np->n_mtx);
380 if ((np->n_mode & mode) != mode) {
385 mtx_unlock(&np->n_mtx);
388 if ((error = nfsspec_access(ap)) != 0) {
392 * Attempt to prevent a mapped root from accessing a file
393 * which it shouldn't. We try to read a byte from the file
394 * if the user is root and the file is not zero length.
395 * After calling nfsspec_access, we should have the correct
398 mtx_lock(&np->n_mtx);
399 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
400 && VTONFS(vp)->n_size > 0) {
405 mtx_unlock(&np->n_mtx);
408 auio.uio_iov = &aiov;
412 auio.uio_segflg = UIO_SYSSPACE;
413 auio.uio_rw = UIO_READ;
414 auio.uio_td = ap->a_td;
416 if (vp->v_type == VREG)
417 error = nfs_readrpc(vp, &auio, ap->a_cred);
418 else if (vp->v_type == VDIR) {
420 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
422 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
423 error = nfs_readdirrpc(vp, &auio, ap->a_cred);
425 } else if (vp->v_type == VLNK)
426 error = nfs_readlinkrpc(vp, &auio, ap->a_cred);
430 mtx_unlock(&np->n_mtx);
437 * Check to see if the type is ok
438 * and that deletion is not in progress.
439 * For paged in text files, you will need to flush the page cache
440 * if consistency is lost.
444 nfs_open(struct vop_open_args *ap)
446 struct vnode *vp = ap->a_vp;
447 struct nfsnode *np = VTONFS(vp);
450 int fmode = ap->a_mode;
452 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
456 * Get a valid lease. If cached data is stale, flush it.
458 mtx_lock(&np->n_mtx);
459 if (np->n_flag & NMODIFIED) {
460 mtx_unlock(&np->n_mtx);
461 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
462 if (error == EINTR || error == EIO)
465 if (vp->v_type == VDIR)
466 np->n_direofoffset = 0;
467 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
470 mtx_lock(&np->n_mtx);
471 np->n_mtime = vattr.va_mtime;
472 mtx_unlock(&np->n_mtx);
475 mtx_unlock(&np->n_mtx);
476 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
479 mtx_lock(&np->n_mtx);
480 if (NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
481 if (vp->v_type == VDIR)
482 np->n_direofoffset = 0;
483 mtx_unlock(&np->n_mtx);
484 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
485 if (error == EINTR || error == EIO) {
488 mtx_lock(&np->n_mtx);
489 np->n_mtime = vattr.va_mtime;
491 mtx_unlock(&np->n_mtx);
494 * If the object has >= 1 O_DIRECT active opens, we disable caching.
496 if (nfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
497 if (np->n_directio_opens == 0) {
498 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
501 mtx_lock(&np->n_mtx);
502 np->n_flag |= NNONCACHE;
503 mtx_unlock(&np->n_mtx);
505 np->n_directio_opens++;
507 vnode_create_vobject(vp, vattr.va_size, ap->a_td);
513 * What an NFS client should do upon close after writing is a debatable issue.
514 * Most NFS clients push delayed writes to the server upon close, basically for
516 * 1 - So that any write errors may be reported back to the client process
517 * doing the close system call. By far the two most likely errors are
518 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
519 * 2 - To put a worst case upper bound on cache inconsistency between
520 * multiple clients for the file.
521 * There is also a consistency problem for Version 2 of the protocol w.r.t.
522 * not being able to tell if other clients are writing a file concurrently,
523 * since there is no way of knowing if the changed modify time in the reply
524 * is only due to the write for this client.
525 * (NFS Version 3 provides weak cache consistency data in the reply that
526 * should be sufficient to detect and handle this case.)
528 * The current code does the following:
529 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
530 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
531 * or commit them (this satisfies 1 and 2 except for the
532 * case where the server crashes after this close but
533 * before the commit RPC, which is felt to be "good
534 * enough". Changing the last argument to nfs_flush() to
535 * a 1 would force a commit operation, if it is felt a
536 * commit is necessary now.
540 nfs_close(struct vop_close_args *ap)
542 struct vnode *vp = ap->a_vp;
543 struct nfsnode *np = VTONFS(vp);
545 int fmode = ap->a_fflag;
547 if (vp->v_type == VREG) {
549 * Examine and clean dirty pages, regardless of NMODIFIED.
550 * This closes a major hole in close-to-open consistency.
551 * We want to push out all dirty pages (and buffers) on
552 * close, regardless of whether they were dirtied by
553 * mmap'ed writes or via write().
555 if (nfs_clean_pages_on_close && vp->v_object) {
556 VM_OBJECT_LOCK(vp->v_object);
557 vm_object_page_clean(vp->v_object, 0, 0, 0);
558 VM_OBJECT_UNLOCK(vp->v_object);
560 mtx_lock(&np->n_mtx);
561 if (np->n_flag & NMODIFIED) {
562 mtx_unlock(&np->n_mtx);
565 * Under NFSv3 we have dirty buffers to dispose of. We
566 * must flush them to the NFS server. We have the option
567 * of waiting all the way through the commit rpc or just
568 * waiting for the initial write. The default is to only
569 * wait through the initial write so the data is in the
570 * server's cache, which is roughly similar to the state
571 * a standard disk subsystem leaves the file in on close().
573 * We cannot clear the NMODIFIED bit in np->n_flag due to
574 * potential races with other processes, and certainly
575 * cannot clear it if we don't commit.
577 int cm = nfsv3_commit_on_close ? 1 : 0;
578 error = nfs_flush(vp, MNT_WAIT, ap->a_td, cm);
579 /* np->n_flag &= ~NMODIFIED; */
581 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
582 mtx_lock(&np->n_mtx);
585 * Invalidate the attribute cache in all cases.
586 * An open is going to fetch fresh attrs any way, other procs
587 * on this node that have file open will be forced to do an
588 * otw attr fetch, but this is safe.
591 if (np->n_flag & NWRITEERR) {
592 np->n_flag &= ~NWRITEERR;
595 mtx_unlock(&np->n_mtx);
597 if (nfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
598 mtx_lock(&np->n_mtx);
599 KASSERT((np->n_directio_opens > 0),
600 ("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
601 np->n_directio_opens--;
602 if (np->n_directio_opens == 0)
603 np->n_flag &= ~NNONCACHE;
604 mtx_unlock(&np->n_mtx);
610 * nfs getattr call from vfs.
613 nfs_getattr(struct vop_getattr_args *ap)
615 struct vnode *vp = ap->a_vp;
616 struct nfsnode *np = VTONFS(vp);
619 struct mbuf *mreq, *mrep, *md, *mb;
620 int v3 = NFS_ISV3(vp);
623 * Update local times for special files.
625 mtx_lock(&np->n_mtx);
626 if (np->n_flag & (NACC | NUPD))
628 mtx_unlock(&np->n_mtx);
630 * First look in the cache.
632 if (nfs_getattrcache(vp, ap->a_vap) == 0)
634 if (v3 && nfsaccess_cache_timeout > 0) {
635 nfsstats.accesscache_misses++;
636 nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_td, ap->a_cred);
637 if (nfs_getattrcache(vp, ap->a_vap) == 0)
640 nfsstats.rpccnt[NFSPROC_GETATTR]++;
641 mreq = nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
643 bpos = mtod(mb, caddr_t);
645 nfsm_request(vp, NFSPROC_GETATTR, ap->a_td, ap->a_cred);
647 nfsm_loadattr(vp, ap->a_vap);
658 nfs_setattr(struct vop_setattr_args *ap)
660 struct vnode *vp = ap->a_vp;
661 struct nfsnode *np = VTONFS(vp);
662 struct vattr *vap = ap->a_vap;
671 * Setting of flags and marking of atimes are not supported.
673 if (vap->va_flags != VNOVAL || (vap->va_vaflags & VA_MARK_ATIME))
677 * Disallow write attempts if the filesystem is mounted read-only.
679 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
680 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
681 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
682 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
686 if (vap->va_size != VNOVAL) {
687 switch (vp->v_type) {
694 if (vap->va_mtime.tv_sec == VNOVAL &&
695 vap->va_atime.tv_sec == VNOVAL &&
696 vap->va_mode == (mode_t)VNOVAL &&
697 vap->va_uid == (uid_t)VNOVAL &&
698 vap->va_gid == (gid_t)VNOVAL)
700 vap->va_size = VNOVAL;
704 * Disallow write attempts if the filesystem is
707 if (vp->v_mount->mnt_flag & MNT_RDONLY)
710 * We run vnode_pager_setsize() early (why?),
711 * we must set np->n_size now to avoid vinvalbuf
712 * V_SAVE races that might setsize a lower
715 mtx_lock(&np->n_mtx);
717 mtx_unlock(&np->n_mtx);
718 error = nfs_meta_setsize(vp, ap->a_cred,
719 ap->a_td, vap->va_size);
720 mtx_lock(&np->n_mtx);
721 if (np->n_flag & NMODIFIED) {
723 mtx_unlock(&np->n_mtx);
724 if (vap->va_size == 0)
725 error = nfs_vinvalbuf(vp, 0, ap->a_td, 1);
727 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
729 vnode_pager_setsize(vp, tsize);
733 mtx_unlock(&np->n_mtx);
735 * np->n_size has already been set to vap->va_size
736 * in nfs_meta_setsize(). We must set it again since
737 * nfs_loadattrcache() could be called through
738 * nfs_meta_setsize() and could modify np->n_size.
740 mtx_lock(&np->n_mtx);
741 np->n_vattr.va_size = np->n_size = vap->va_size;
742 mtx_unlock(&np->n_mtx);
745 mtx_lock(&np->n_mtx);
746 if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
747 (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
748 mtx_unlock(&np->n_mtx);
749 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) != 0 &&
750 (error == EINTR || error == EIO))
753 mtx_unlock(&np->n_mtx);
755 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
756 if (error && vap->va_size != VNOVAL) {
757 mtx_lock(&np->n_mtx);
758 np->n_size = np->n_vattr.va_size = tsize;
759 vnode_pager_setsize(vp, tsize);
760 mtx_unlock(&np->n_mtx);
767 * Do an nfs setattr rpc.
770 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
773 struct nfsv2_sattr *sp;
774 struct nfsnode *np = VTONFS(vp);
777 int error = 0, wccflag = NFSV3_WCCRATTR;
778 struct mbuf *mreq, *mrep, *md, *mb;
779 int v3 = NFS_ISV3(vp);
781 nfsstats.rpccnt[NFSPROC_SETATTR]++;
782 mreq = nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
784 bpos = mtod(mb, caddr_t);
787 nfsm_v3attrbuild(vap, TRUE);
788 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
791 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
792 if (vap->va_mode == (mode_t)VNOVAL)
793 sp->sa_mode = nfs_xdrneg1;
795 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
796 if (vap->va_uid == (uid_t)VNOVAL)
797 sp->sa_uid = nfs_xdrneg1;
799 sp->sa_uid = txdr_unsigned(vap->va_uid);
800 if (vap->va_gid == (gid_t)VNOVAL)
801 sp->sa_gid = nfs_xdrneg1;
803 sp->sa_gid = txdr_unsigned(vap->va_gid);
804 sp->sa_size = txdr_unsigned(vap->va_size);
805 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
806 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
808 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
811 nfsm_wcc_data(vp, wccflag);
813 nfsm_loadattr(vp, NULL);
820 * nfs lookup call, one step at a time...
821 * First look in cache
822 * If not found, unlock the directory nfsnode and do the rpc
825 nfs_lookup(struct vop_lookup_args *ap)
827 struct componentname *cnp = ap->a_cnp;
828 struct vnode *dvp = ap->a_dvp;
829 struct vnode **vpp = ap->a_vpp;
830 int flags = cnp->cn_flags;
832 struct nfsmount *nmp;
834 struct mbuf *mreq, *mrep, *md, *mb;
838 int error = 0, attrflag, fhsize;
839 int v3 = NFS_ISV3(dvp);
840 struct thread *td = cnp->cn_thread;
843 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
844 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
846 if (dvp->v_type != VDIR)
848 nmp = VFSTONFS(dvp->v_mount);
850 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
854 if ((error = cache_lookup(dvp, vpp, cnp)) && error != ENOENT) {
858 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, td)
859 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
860 nfsstats.lookupcache_hits++;
861 if (cnp->cn_nameiop != LOOKUP &&
863 cnp->cn_flags |= SAVENAME;
875 nfsstats.lookupcache_misses++;
876 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
877 len = cnp->cn_namelen;
878 mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP,
879 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
881 bpos = mtod(mb, caddr_t);
883 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
884 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_thread, cnp->cn_cred);
887 nfsm_postop_attr(dvp, attrflag);
892 nfsm_getfh(fhp, fhsize, v3);
895 * Handle RENAME case...
897 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
898 if (NFS_CMPFH(np, fhp, fhsize)) {
902 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, LK_EXCLUSIVE);
909 nfsm_postop_attr(newvp, attrflag);
910 nfsm_postop_attr(dvp, attrflag);
912 nfsm_loadattr(newvp, NULL);
915 cnp->cn_flags |= SAVENAME;
919 if (flags & ISDOTDOT) {
920 VOP_UNLOCK(dvp, 0, td);
921 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, cnp->cn_lkflags);
922 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
926 } else if (NFS_CMPFH(np, fhp, fhsize)) {
930 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, cnp->cn_lkflags);
938 nfsm_postop_attr(newvp, attrflag);
939 nfsm_postop_attr(dvp, attrflag);
941 nfsm_loadattr(newvp, NULL);
942 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
943 cnp->cn_flags |= SAVENAME;
944 if ((cnp->cn_flags & MAKEENTRY) &&
945 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
946 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
947 cache_enter(dvp, newvp, cnp);
953 if (newvp != NULLVP) {
957 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
958 (flags & ISLASTCN) && error == ENOENT) {
959 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
964 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
965 cnp->cn_flags |= SAVENAME;
972 * Just call nfs_bioread() to do the work.
975 nfs_read(struct vop_read_args *ap)
977 struct vnode *vp = ap->a_vp;
979 switch (vp->v_type) {
981 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
993 nfs_readlink(struct vop_readlink_args *ap)
995 struct vnode *vp = ap->a_vp;
997 if (vp->v_type != VLNK)
999 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred));
1003 * Do a readlink rpc.
1004 * Called by nfs_doio() from below the buffer cache.
1007 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1010 int error = 0, len, attrflag;
1011 struct mbuf *mreq, *mrep, *md, *mb;
1012 int v3 = NFS_ISV3(vp);
1014 nfsstats.rpccnt[NFSPROC_READLINK]++;
1015 mreq = nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1017 bpos = mtod(mb, caddr_t);
1019 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, cred);
1021 nfsm_postop_attr(vp, attrflag);
1023 nfsm_strsiz(len, NFS_MAXPATHLEN);
1024 if (len == NFS_MAXPATHLEN) {
1025 struct nfsnode *np = VTONFS(vp);
1026 mtx_lock(&np->n_mtx);
1027 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1029 mtx_unlock(&np->n_mtx);
1031 nfsm_mtouio(uiop, len);
1043 nfs_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1047 struct mbuf *mreq, *mrep, *md, *mb;
1048 struct nfsmount *nmp;
1049 int error = 0, len, retlen, tsiz, eof, attrflag;
1050 int v3 = NFS_ISV3(vp);
1056 nmp = VFSTONFS(vp->v_mount);
1057 tsiz = uiop->uio_resid;
1058 mtx_lock(&nmp->nm_mtx);
1059 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize) {
1060 mtx_unlock(&nmp->nm_mtx);
1063 rsize = nmp->nm_rsize;
1064 mtx_unlock(&nmp->nm_mtx);
1066 nfsstats.rpccnt[NFSPROC_READ]++;
1067 len = (tsiz > rsize) ? rsize : tsiz;
1068 mreq = nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1070 bpos = mtod(mb, caddr_t);
1072 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED * 3);
1074 txdr_hyper(uiop->uio_offset, tl);
1075 *(tl + 2) = txdr_unsigned(len);
1077 *tl++ = txdr_unsigned(uiop->uio_offset);
1078 *tl++ = txdr_unsigned(len);
1081 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, cred);
1083 nfsm_postop_attr(vp, attrflag);
1088 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
1089 eof = fxdr_unsigned(int, *(tl + 1));
1091 nfsm_loadattr(vp, NULL);
1093 nfsm_strsiz(retlen, rsize);
1094 nfsm_mtouio(uiop, retlen);
1098 if (eof || retlen == 0) {
1101 } else if (retlen < len) {
1113 nfs_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1114 int *iomode, int *must_commit)
1119 struct mbuf *mreq, *mrep, *md, *mb;
1120 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1121 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1122 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1126 if (uiop->uio_iovcnt != 1)
1127 panic("nfs: writerpc iovcnt > 1");
1130 tsiz = uiop->uio_resid;
1131 mtx_lock(&nmp->nm_mtx);
1132 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize) {
1133 mtx_unlock(&nmp->nm_mtx);
1136 wsize = nmp->nm_wsize;
1137 mtx_unlock(&nmp->nm_mtx);
1139 nfsstats.rpccnt[NFSPROC_WRITE]++;
1140 len = (tsiz > wsize) ? wsize : tsiz;
1141 mreq = nfsm_reqhead(vp, NFSPROC_WRITE,
1142 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1144 bpos = mtod(mb, caddr_t);
1147 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
1148 txdr_hyper(uiop->uio_offset, tl);
1150 *tl++ = txdr_unsigned(len);
1151 *tl++ = txdr_unsigned(*iomode);
1152 *tl = txdr_unsigned(len);
1156 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1157 /* Set both "begin" and "current" to non-garbage. */
1158 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1159 *tl++ = x; /* "begin offset" */
1160 *tl++ = x; /* "current offset" */
1161 x = txdr_unsigned(len);
1162 *tl++ = x; /* total to this offset */
1163 *tl = x; /* size of this write */
1165 nfsm_uiotom(uiop, len);
1166 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, cred);
1168 wccflag = NFSV3_WCCCHK;
1169 nfsm_wcc_data(vp, wccflag);
1171 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED
1172 + NFSX_V3WRITEVERF);
1173 rlen = fxdr_unsigned(int, *tl++);
1178 } else if (rlen < len) {
1179 backup = len - rlen;
1180 uiop->uio_iov->iov_base =
1181 (char *)uiop->uio_iov->iov_base -
1183 uiop->uio_iov->iov_len += backup;
1184 uiop->uio_offset -= backup;
1185 uiop->uio_resid += backup;
1188 commit = fxdr_unsigned(int, *tl++);
1191 * Return the lowest committment level
1192 * obtained by any of the RPCs.
1194 if (committed == NFSV3WRITE_FILESYNC)
1196 else if (committed == NFSV3WRITE_DATASYNC &&
1197 commit == NFSV3WRITE_UNSTABLE)
1199 mtx_lock(&nmp->nm_mtx);
1200 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1201 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1203 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1204 } else if (bcmp((caddr_t)tl,
1205 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1207 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1210 mtx_unlock(&nmp->nm_mtx);
1213 nfsm_loadattr(vp, NULL);
1216 mtx_lock(&(VTONFS(vp))->n_mtx);
1217 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime;
1218 mtx_unlock(&(VTONFS(vp))->n_mtx);
1226 if (vp->v_mount->mnt_kern_flag & MNTK_ASYNC)
1227 committed = NFSV3WRITE_FILESYNC;
1228 *iomode = committed;
1230 uiop->uio_resid = tsiz;
1236 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1237 * mode set to specify the file type and the size field for rdev.
1240 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1243 struct nfsv2_sattr *sp;
1245 struct vnode *newvp = NULL;
1246 struct nfsnode *np = NULL;
1249 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1250 struct mbuf *mreq, *mrep, *md, *mb;
1252 int v3 = NFS_ISV3(dvp);
1254 if (vap->va_type == VCHR || vap->va_type == VBLK)
1255 rdev = txdr_unsigned(vap->va_rdev);
1256 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1259 return (EOPNOTSUPP);
1261 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1264 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1265 mreq = nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1266 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1268 bpos = mtod(mb, caddr_t);
1269 nfsm_fhtom(dvp, v3);
1270 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1272 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1273 *tl++ = vtonfsv3_type(vap->va_type);
1274 nfsm_v3attrbuild(vap, FALSE);
1275 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1276 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
1277 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1278 *tl = txdr_unsigned(uminor(vap->va_rdev));
1281 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1282 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1283 sp->sa_uid = nfs_xdrneg1;
1284 sp->sa_gid = nfs_xdrneg1;
1286 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1287 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1289 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_thread, cnp->cn_cred);
1291 nfsm_mtofh(dvp, newvp, v3, gotvp);
1297 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1298 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1304 nfsm_wcc_data(dvp, wccflag);
1311 if (cnp->cn_flags & MAKEENTRY)
1312 cache_enter(dvp, newvp, cnp);
1315 mtx_lock(&(VTONFS(dvp))->n_mtx);
1316 VTONFS(dvp)->n_flag |= NMODIFIED;
1318 VTONFS(dvp)->n_attrstamp = 0;
1319 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1325 * just call nfs_mknodrpc() to do the work.
1329 nfs_mknod(struct vop_mknod_args *ap)
1331 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1334 static u_long create_verf;
1336 * nfs file create call
1339 nfs_create(struct vop_create_args *ap)
1341 struct vnode *dvp = ap->a_dvp;
1342 struct vattr *vap = ap->a_vap;
1343 struct componentname *cnp = ap->a_cnp;
1344 struct nfsv2_sattr *sp;
1346 struct nfsnode *np = NULL;
1347 struct vnode *newvp = NULL;
1349 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1350 struct mbuf *mreq, *mrep, *md, *mb;
1352 int v3 = NFS_ISV3(dvp);
1355 * Oops, not for me..
1357 if (vap->va_type == VSOCK)
1358 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1360 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1363 if (vap->va_vaflags & VA_EXCLUSIVE)
1366 nfsstats.rpccnt[NFSPROC_CREATE]++;
1367 mreq = nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1368 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1370 bpos = mtod(mb, caddr_t);
1371 nfsm_fhtom(dvp, v3);
1372 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1374 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1375 if (fmode & O_EXCL) {
1376 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1377 tl = nfsm_build(u_int32_t *, NFSX_V3CREATEVERF);
1379 if (!TAILQ_EMPTY(&in_ifaddrhead))
1380 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr;
1383 *tl++ = create_verf;
1384 *tl = ++create_verf;
1386 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1387 nfsm_v3attrbuild(vap, FALSE);
1390 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1391 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1392 sp->sa_uid = nfs_xdrneg1;
1393 sp->sa_gid = nfs_xdrneg1;
1395 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1396 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1398 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_thread, cnp->cn_cred);
1400 nfsm_mtofh(dvp, newvp, v3, gotvp);
1406 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1407 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1413 nfsm_wcc_data(dvp, wccflag);
1417 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1423 } else if (v3 && (fmode & O_EXCL)) {
1425 * We are normally called with only a partially initialized
1426 * VAP. Since the NFSv3 spec says that server may use the
1427 * file attributes to store the verifier, the spec requires
1428 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1429 * in atime, but we can't really assume that all servers will
1430 * so we ensure that our SETATTR sets both atime and mtime.
1432 if (vap->va_mtime.tv_sec == VNOVAL)
1433 vfs_timestamp(&vap->va_mtime);
1434 if (vap->va_atime.tv_sec == VNOVAL)
1435 vap->va_atime = vap->va_mtime;
1436 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_thread);
1439 if (cnp->cn_flags & MAKEENTRY)
1440 cache_enter(dvp, newvp, cnp);
1443 mtx_lock(&(VTONFS(dvp))->n_mtx);
1444 VTONFS(dvp)->n_flag |= NMODIFIED;
1446 VTONFS(dvp)->n_attrstamp = 0;
1447 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1452 * nfs file remove call
1453 * To try and make nfs semantics closer to ufs semantics, a file that has
1454 * other processes using the vnode is renamed instead of removed and then
1455 * removed later on the last close.
1456 * - If v_usecount > 1
1457 * If a rename is not already in the works
1458 * call nfs_sillyrename() to set it up
1463 nfs_remove(struct vop_remove_args *ap)
1465 struct vnode *vp = ap->a_vp;
1466 struct vnode *dvp = ap->a_dvp;
1467 struct componentname *cnp = ap->a_cnp;
1468 struct nfsnode *np = VTONFS(vp);
1473 if ((cnp->cn_flags & HASBUF) == 0)
1474 panic("nfs_remove: no name");
1475 if (vrefcnt(vp) < 1)
1476 panic("nfs_remove: bad v_usecount");
1478 if (vp->v_type == VDIR)
1480 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1481 VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_thread) == 0 &&
1482 vattr.va_nlink > 1)) {
1484 * Purge the name cache so that the chance of a lookup for
1485 * the name succeeding while the remove is in progress is
1486 * minimized. Without node locking it can still happen, such
1487 * that an I/O op returns ESTALE, but since you get this if
1488 * another host removes the file..
1492 * throw away biocache buffers, mainly to avoid
1493 * unnecessary delayed writes later.
1495 error = nfs_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1497 if (error != EINTR && error != EIO)
1498 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1499 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1501 * Kludge City: If the first reply to the remove rpc is lost..
1502 * the reply to the retransmitted request will be ENOENT
1503 * since the file was in fact removed
1504 * Therefore, we cheat and return success.
1506 if (error == ENOENT)
1508 } else if (!np->n_sillyrename)
1509 error = nfs_sillyrename(dvp, vp, cnp);
1510 np->n_attrstamp = 0;
1515 * nfs file remove rpc called from nfs_inactive
1518 nfs_removeit(struct sillyrename *sp)
1521 * Make sure that the directory vnode is still valid.
1522 * XXX we should lock sp->s_dvp here.
1524 if (sp->s_dvp->v_type == VBAD)
1526 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1531 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1534 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1535 struct ucred *cred, struct thread *td)
1538 int error = 0, wccflag = NFSV3_WCCRATTR;
1539 struct mbuf *mreq, *mrep, *md, *mb;
1540 int v3 = NFS_ISV3(dvp);
1542 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1543 mreq = nfsm_reqhead(dvp, NFSPROC_REMOVE,
1544 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1546 bpos = mtod(mb, caddr_t);
1547 nfsm_fhtom(dvp, v3);
1548 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1549 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1551 nfsm_wcc_data(dvp, wccflag);
1554 mtx_lock(&(VTONFS(dvp))->n_mtx);
1555 VTONFS(dvp)->n_flag |= NMODIFIED;
1557 VTONFS(dvp)->n_attrstamp = 0;
1558 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1563 * nfs file rename call
1566 nfs_rename(struct vop_rename_args *ap)
1568 struct vnode *fvp = ap->a_fvp;
1569 struct vnode *tvp = ap->a_tvp;
1570 struct vnode *fdvp = ap->a_fdvp;
1571 struct vnode *tdvp = ap->a_tdvp;
1572 struct componentname *tcnp = ap->a_tcnp;
1573 struct componentname *fcnp = ap->a_fcnp;
1577 if ((tcnp->cn_flags & HASBUF) == 0 ||
1578 (fcnp->cn_flags & HASBUF) == 0)
1579 panic("nfs_rename: no name");
1581 /* Check for cross-device rename */
1582 if ((fvp->v_mount != tdvp->v_mount) ||
1583 (tvp && (fvp->v_mount != tvp->v_mount))) {
1589 nfs_printf("nfs_rename: fvp == tvp (can't happen)\n");
1593 if ((error = vn_lock(fvp, LK_EXCLUSIVE, fcnp->cn_thread)) != 0)
1597 * We have to flush B_DELWRI data prior to renaming
1598 * the file. If we don't, the delayed-write buffers
1599 * can be flushed out later after the file has gone stale
1600 * under NFSV3. NFSV2 does not have this problem because
1601 * ( as far as I can tell ) it flushes dirty buffers more
1604 * Skip the rename operation if the fsync fails, this can happen
1605 * due to the server's volume being full, when we pushed out data
1606 * that was written back to our cache earlier. Not checking for
1607 * this condition can result in potential (silent) data loss.
1609 error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1610 VOP_UNLOCK(fvp, 0, fcnp->cn_thread);
1612 error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1617 * If the tvp exists and is in use, sillyrename it before doing the
1618 * rename of the new file over it.
1619 * XXX Can't sillyrename a directory.
1621 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1622 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1627 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1628 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1631 if (fvp->v_type == VDIR) {
1632 if (tvp != NULL && tvp->v_type == VDIR)
1647 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1649 if (error == ENOENT)
1655 * nfs file rename rpc called from nfs_remove() above
1658 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1659 struct sillyrename *sp)
1662 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen, sdvp,
1663 sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_thread));
1667 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1670 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1671 struct vnode *tdvp, const char *tnameptr, int tnamelen, struct ucred *cred,
1675 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1676 struct mbuf *mreq, *mrep, *md, *mb;
1677 int v3 = NFS_ISV3(fdvp);
1679 nfsstats.rpccnt[NFSPROC_RENAME]++;
1680 mreq = nfsm_reqhead(fdvp, NFSPROC_RENAME,
1681 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1682 nfsm_rndup(tnamelen));
1684 bpos = mtod(mb, caddr_t);
1685 nfsm_fhtom(fdvp, v3);
1686 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1687 nfsm_fhtom(tdvp, v3);
1688 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1689 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1691 nfsm_wcc_data(fdvp, fwccflag);
1692 nfsm_wcc_data(tdvp, twccflag);
1696 mtx_lock(&(VTONFS(fdvp))->n_mtx);
1697 VTONFS(fdvp)->n_flag |= NMODIFIED;
1698 mtx_unlock(&(VTONFS(fdvp))->n_mtx);
1699 mtx_lock(&(VTONFS(tdvp))->n_mtx);
1700 VTONFS(tdvp)->n_flag |= NMODIFIED;
1701 mtx_unlock(&(VTONFS(tdvp))->n_mtx);
1703 VTONFS(fdvp)->n_attrstamp = 0;
1705 VTONFS(tdvp)->n_attrstamp = 0;
1710 * nfs hard link create call
1713 nfs_link(struct vop_link_args *ap)
1715 struct vnode *vp = ap->a_vp;
1716 struct vnode *tdvp = ap->a_tdvp;
1717 struct componentname *cnp = ap->a_cnp;
1719 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1720 struct mbuf *mreq, *mrep, *md, *mb;
1723 if (vp->v_mount != tdvp->v_mount) {
1728 * Push all writes to the server, so that the attribute cache
1729 * doesn't get "out of sync" with the server.
1730 * XXX There should be a better way!
1732 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1735 nfsstats.rpccnt[NFSPROC_LINK]++;
1736 mreq = nfsm_reqhead(vp, NFSPROC_LINK,
1737 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1739 bpos = mtod(mb, caddr_t);
1741 nfsm_fhtom(tdvp, v3);
1742 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1743 nfsm_request(vp, NFSPROC_LINK, cnp->cn_thread, cnp->cn_cred);
1745 nfsm_postop_attr(vp, attrflag);
1746 nfsm_wcc_data(tdvp, wccflag);
1750 mtx_lock(&(VTONFS(tdvp))->n_mtx);
1751 VTONFS(tdvp)->n_flag |= NMODIFIED;
1752 mtx_unlock(&(VTONFS(tdvp))->n_mtx);
1754 VTONFS(vp)->n_attrstamp = 0;
1756 VTONFS(tdvp)->n_attrstamp = 0;
1758 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1760 if (error == EEXIST)
1766 * nfs symbolic link create call
1769 nfs_symlink(struct vop_symlink_args *ap)
1771 struct vnode *dvp = ap->a_dvp;
1772 struct vattr *vap = ap->a_vap;
1773 struct componentname *cnp = ap->a_cnp;
1774 struct nfsv2_sattr *sp;
1776 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1777 struct mbuf *mreq, *mrep, *md, *mb;
1778 struct vnode *newvp = NULL;
1779 int v3 = NFS_ISV3(dvp);
1781 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1782 slen = strlen(ap->a_target);
1783 mreq = nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1784 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1786 bpos = mtod(mb, caddr_t);
1787 nfsm_fhtom(dvp, v3);
1788 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1790 nfsm_v3attrbuild(vap, FALSE);
1792 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1794 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1795 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1796 sp->sa_uid = nfs_xdrneg1;
1797 sp->sa_gid = nfs_xdrneg1;
1798 sp->sa_size = nfs_xdrneg1;
1799 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1800 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1804 * Issue the NFS request and get the rpc response.
1806 * Only NFSv3 responses returning an error of 0 actually return
1807 * a file handle that can be converted into newvp without having
1808 * to do an extra lookup rpc.
1810 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_thread, cnp->cn_cred);
1813 nfsm_mtofh(dvp, newvp, v3, gotvp);
1814 nfsm_wcc_data(dvp, wccflag);
1818 * out code jumps -> here, mrep is also freed.
1825 * If we get an EEXIST error, silently convert it to no-error
1826 * in case of an NFS retry.
1828 if (error == EEXIST)
1832 * If we do not have (or no longer have) an error, and we could
1833 * not extract the newvp from the response due to the request being
1834 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1835 * to obtain a newvp to return.
1837 if (error == 0 && newvp == NULL) {
1838 struct nfsnode *np = NULL;
1840 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1841 cnp->cn_cred, cnp->cn_thread, &np);
1851 mtx_lock(&(VTONFS(dvp))->n_mtx);
1852 VTONFS(dvp)->n_flag |= NMODIFIED;
1853 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1855 VTONFS(dvp)->n_attrstamp = 0;
1863 nfs_mkdir(struct vop_mkdir_args *ap)
1865 struct vnode *dvp = ap->a_dvp;
1866 struct vattr *vap = ap->a_vap;
1867 struct componentname *cnp = ap->a_cnp;
1868 struct nfsv2_sattr *sp;
1870 struct nfsnode *np = NULL;
1871 struct vnode *newvp = NULL;
1873 int error = 0, wccflag = NFSV3_WCCRATTR;
1875 struct mbuf *mreq, *mrep, *md, *mb;
1877 int v3 = NFS_ISV3(dvp);
1879 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1882 len = cnp->cn_namelen;
1883 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1884 mreq = nfsm_reqhead(dvp, NFSPROC_MKDIR,
1885 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1887 bpos = mtod(mb, caddr_t);
1888 nfsm_fhtom(dvp, v3);
1889 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1891 nfsm_v3attrbuild(vap, FALSE);
1893 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1894 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1895 sp->sa_uid = nfs_xdrneg1;
1896 sp->sa_gid = nfs_xdrneg1;
1897 sp->sa_size = nfs_xdrneg1;
1898 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1899 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1901 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_thread, cnp->cn_cred);
1903 nfsm_mtofh(dvp, newvp, v3, gotvp);
1905 nfsm_wcc_data(dvp, wccflag);
1908 mtx_lock(&(VTONFS(dvp))->n_mtx);
1909 VTONFS(dvp)->n_flag |= NMODIFIED;
1910 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1912 VTONFS(dvp)->n_attrstamp = 0;
1914 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1915 * if we can succeed in looking up the directory.
1917 if (error == EEXIST || (!error && !gotvp)) {
1922 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
1923 cnp->cn_thread, &np);
1926 if (newvp->v_type != VDIR)
1939 * nfs remove directory call
1942 nfs_rmdir(struct vop_rmdir_args *ap)
1944 struct vnode *vp = ap->a_vp;
1945 struct vnode *dvp = ap->a_dvp;
1946 struct componentname *cnp = ap->a_cnp;
1948 int error = 0, wccflag = NFSV3_WCCRATTR;
1949 struct mbuf *mreq, *mrep, *md, *mb;
1950 int v3 = NFS_ISV3(dvp);
1954 nfsstats.rpccnt[NFSPROC_RMDIR]++;
1955 mreq = nfsm_reqhead(dvp, NFSPROC_RMDIR,
1956 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1958 bpos = mtod(mb, caddr_t);
1959 nfsm_fhtom(dvp, v3);
1960 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1961 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_thread, cnp->cn_cred);
1963 nfsm_wcc_data(dvp, wccflag);
1966 mtx_lock(&(VTONFS(dvp))->n_mtx);
1967 VTONFS(dvp)->n_flag |= NMODIFIED;
1968 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1970 VTONFS(dvp)->n_attrstamp = 0;
1974 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
1976 if (error == ENOENT)
1985 nfs_readdir(struct vop_readdir_args *ap)
1987 struct vnode *vp = ap->a_vp;
1988 struct nfsnode *np = VTONFS(vp);
1989 struct uio *uio = ap->a_uio;
1990 int tresid, error = 0;
1993 if (vp->v_type != VDIR)
1997 * First, check for hit on the EOF offset cache
1999 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2000 (np->n_flag & NMODIFIED) == 0) {
2001 if (VOP_GETATTR(vp, &vattr, ap->a_cred, uio->uio_td) == 0) {
2002 mtx_lock(&np->n_mtx);
2003 if (!NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2004 mtx_unlock(&np->n_mtx);
2005 nfsstats.direofcache_hits++;
2008 mtx_unlock(&np->n_mtx);
2013 * Call nfs_bioread() to do the real work.
2015 tresid = uio->uio_resid;
2016 error = nfs_bioread(vp, uio, 0, ap->a_cred);
2018 if (!error && uio->uio_resid == tresid) {
2019 nfsstats.direofcache_misses++;
2027 * Called from below the buffer cache by nfs_doio().
2030 nfs_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
2033 struct dirent *dp = NULL;
2038 struct mbuf *mreq, *mrep, *md, *mb;
2040 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2041 struct nfsnode *dnp = VTONFS(vp);
2043 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2045 int v3 = NFS_ISV3(vp);
2048 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2049 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2050 panic("nfs readdirrpc bad uio");
2054 * If there is no cookie, assume directory was stale.
2056 nfs_dircookie_lock(dnp);
2057 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2060 nfs_dircookie_unlock(dnp);
2062 nfs_dircookie_unlock(dnp);
2063 return (NFSERR_BAD_COOKIE);
2067 * Loop around doing readdir rpc's of size nm_readdirsize
2068 * truncated to a multiple of DIRBLKSIZ.
2069 * The stopping criteria is EOF or buffer full.
2071 while (more_dirs && bigenough) {
2072 nfsstats.rpccnt[NFSPROC_READDIR]++;
2073 mreq = nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2076 bpos = mtod(mb, caddr_t);
2079 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
2080 *tl++ = cookie.nfsuquad[0];
2081 *tl++ = cookie.nfsuquad[1];
2082 mtx_lock(&dnp->n_mtx);
2083 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2084 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2085 mtx_unlock(&dnp->n_mtx);
2087 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
2088 *tl++ = cookie.nfsuquad[0];
2090 *tl = txdr_unsigned(nmp->nm_readdirsize);
2091 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, cred);
2093 nfsm_postop_attr(vp, attrflag);
2095 tl = nfsm_dissect(u_int32_t *,
2097 mtx_lock(&dnp->n_mtx);
2098 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2099 dnp->n_cookieverf.nfsuquad[1] = *tl;
2100 mtx_unlock(&dnp->n_mtx);
2106 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2107 more_dirs = fxdr_unsigned(int, *tl);
2109 /* loop thru the dir entries, doctoring them to 4bsd form */
2110 while (more_dirs && bigenough) {
2112 tl = nfsm_dissect(u_int32_t *,
2114 fileno = fxdr_hyper(tl);
2115 len = fxdr_unsigned(int, *(tl + 2));
2117 tl = nfsm_dissect(u_int32_t *,
2119 fileno = fxdr_unsigned(u_quad_t, *tl++);
2120 len = fxdr_unsigned(int, *tl);
2122 if (len <= 0 || len > NFS_MAXNAMLEN) {
2127 tlen = nfsm_rndup(len);
2129 tlen += 4; /* To ensure null termination */
2130 left = DIRBLKSIZ - blksiz;
2131 if ((tlen + DIRHDSIZ) > left) {
2132 dp->d_reclen += left;
2133 uiop->uio_iov->iov_base =
2134 (char *)uiop->uio_iov->iov_base + left;
2135 uiop->uio_iov->iov_len -= left;
2136 uiop->uio_offset += left;
2137 uiop->uio_resid -= left;
2140 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2143 dp = (struct dirent *)uiop->uio_iov->iov_base;
2144 dp->d_fileno = (int)fileno;
2146 dp->d_reclen = tlen + DIRHDSIZ;
2147 dp->d_type = DT_UNKNOWN;
2148 blksiz += dp->d_reclen;
2149 if (blksiz == DIRBLKSIZ)
2151 uiop->uio_offset += DIRHDSIZ;
2152 uiop->uio_resid -= DIRHDSIZ;
2153 uiop->uio_iov->iov_base =
2154 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
2155 uiop->uio_iov->iov_len -= DIRHDSIZ;
2156 nfsm_mtouio(uiop, len);
2157 cp = uiop->uio_iov->iov_base;
2159 *cp = '\0'; /* null terminate */
2160 uiop->uio_iov->iov_base =
2161 (char *)uiop->uio_iov->iov_base + tlen;
2162 uiop->uio_iov->iov_len -= tlen;
2163 uiop->uio_offset += tlen;
2164 uiop->uio_resid -= tlen;
2166 nfsm_adv(nfsm_rndup(len));
2168 tl = nfsm_dissect(u_int32_t *,
2171 tl = nfsm_dissect(u_int32_t *,
2175 cookie.nfsuquad[0] = *tl++;
2177 cookie.nfsuquad[1] = *tl++;
2182 more_dirs = fxdr_unsigned(int, *tl);
2185 * If at end of rpc data, get the eof boolean
2188 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2189 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2194 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2195 * by increasing d_reclen for the last record.
2198 left = DIRBLKSIZ - blksiz;
2199 dp->d_reclen += left;
2200 uiop->uio_iov->iov_base =
2201 (char *)uiop->uio_iov->iov_base + left;
2202 uiop->uio_iov->iov_len -= left;
2203 uiop->uio_offset += left;
2204 uiop->uio_resid -= left;
2208 * We are now either at the end of the directory or have filled the
2212 dnp->n_direofoffset = uiop->uio_offset;
2214 if (uiop->uio_resid > 0)
2215 nfs_printf("EEK! readdirrpc resid > 0\n");
2216 nfs_dircookie_lock(dnp);
2217 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2219 nfs_dircookie_unlock(dnp);
2226 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2229 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
2235 struct vnode *newvp;
2237 caddr_t bpos, dpos, dpossav1, dpossav2;
2238 struct mbuf *mreq, *mrep, *md, *mb, *mdsav1, *mdsav2;
2239 struct nameidata nami, *ndp = &nami;
2240 struct componentname *cnp = &ndp->ni_cnd;
2242 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2243 struct nfsnode *dnp = VTONFS(vp), *np;
2246 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2247 int attrflag, fhsize;
2253 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2254 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2255 panic("nfs readdirplusrpc bad uio");
2261 * If there is no cookie, assume directory was stale.
2263 nfs_dircookie_lock(dnp);
2264 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2267 nfs_dircookie_unlock(dnp);
2269 nfs_dircookie_unlock(dnp);
2270 return (NFSERR_BAD_COOKIE);
2273 * Loop around doing readdir rpc's of size nm_readdirsize
2274 * truncated to a multiple of DIRBLKSIZ.
2275 * The stopping criteria is EOF or buffer full.
2277 while (more_dirs && bigenough) {
2278 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2279 mreq = nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2280 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2282 bpos = mtod(mb, caddr_t);
2284 tl = nfsm_build(u_int32_t *, 6 * NFSX_UNSIGNED);
2285 *tl++ = cookie.nfsuquad[0];
2286 *tl++ = cookie.nfsuquad[1];
2287 mtx_lock(&dnp->n_mtx);
2288 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2289 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2290 mtx_unlock(&dnp->n_mtx);
2291 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2292 *tl = txdr_unsigned(nmp->nm_rsize);
2293 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, cred);
2294 nfsm_postop_attr(vp, attrflag);
2299 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2300 mtx_lock(&dnp->n_mtx);
2301 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2302 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2303 mtx_unlock(&dnp->n_mtx);
2304 more_dirs = fxdr_unsigned(int, *tl);
2306 /* loop thru the dir entries, doctoring them to 4bsd form */
2307 while (more_dirs && bigenough) {
2308 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2309 fileno = fxdr_hyper(tl);
2310 len = fxdr_unsigned(int, *(tl + 2));
2311 if (len <= 0 || len > NFS_MAXNAMLEN) {
2316 tlen = nfsm_rndup(len);
2318 tlen += 4; /* To ensure null termination*/
2319 left = DIRBLKSIZ - blksiz;
2320 if ((tlen + DIRHDSIZ) > left) {
2321 dp->d_reclen += left;
2322 uiop->uio_iov->iov_base =
2323 (char *)uiop->uio_iov->iov_base + left;
2324 uiop->uio_iov->iov_len -= left;
2325 uiop->uio_offset += left;
2326 uiop->uio_resid -= left;
2329 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2332 dp = (struct dirent *)uiop->uio_iov->iov_base;
2333 dp->d_fileno = (int)fileno;
2335 dp->d_reclen = tlen + DIRHDSIZ;
2336 dp->d_type = DT_UNKNOWN;
2337 blksiz += dp->d_reclen;
2338 if (blksiz == DIRBLKSIZ)
2340 uiop->uio_offset += DIRHDSIZ;
2341 uiop->uio_resid -= DIRHDSIZ;
2342 uiop->uio_iov->iov_base =
2343 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
2344 uiop->uio_iov->iov_len -= DIRHDSIZ;
2345 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2346 cnp->cn_namelen = len;
2347 nfsm_mtouio(uiop, len);
2348 cp = uiop->uio_iov->iov_base;
2351 uiop->uio_iov->iov_base =
2352 (char *)uiop->uio_iov->iov_base + tlen;
2353 uiop->uio_iov->iov_len -= tlen;
2354 uiop->uio_offset += tlen;
2355 uiop->uio_resid -= tlen;
2357 nfsm_adv(nfsm_rndup(len));
2358 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2360 cookie.nfsuquad[0] = *tl++;
2361 cookie.nfsuquad[1] = *tl++;
2366 * Since the attributes are before the file handle
2367 * (sigh), we must skip over the attributes and then
2368 * come back and get them.
2370 attrflag = fxdr_unsigned(int, *tl);
2374 nfsm_adv(NFSX_V3FATTR);
2375 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2376 doit = fxdr_unsigned(int, *tl);
2378 * Skip loading the attrs for "..". There's a
2379 * race between loading the attrs here and
2380 * lookups that look for the directory currently
2381 * being read (in the parent). We try to acquire
2382 * the exclusive lock on ".." here, owning the
2383 * lock on the directory being read. Lookup will
2384 * hold the lock on ".." and try to acquire the
2385 * lock on the directory being read.
2387 * There are other ways of fixing this, one would
2388 * be to do a trylock on the ".." vnode and skip
2389 * loading the attrs on ".." if it happens to be
2390 * locked by another process. But skipping the
2391 * attrload on ".." seems the easiest option.
2393 if (strcmp(dp->d_name, "..") == 0) {
2396 * We've already skipped over the attrs,
2397 * skip over the filehandle. And store d_type
2400 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2401 i = fxdr_unsigned(int, *tl);
2402 nfsm_adv(nfsm_rndup(i));
2403 dp->d_type = IFTODT(VTTOIF(VDIR));
2406 nfsm_getfh(fhp, fhsize, 1);
2407 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2412 error = nfs_nget(vp->v_mount, fhp,
2413 fhsize, &np, LK_EXCLUSIVE);
2420 if (doit && bigenough) {
2425 nfsm_loadattr(newvp, NULL);
2429 IFTODT(VTTOIF(np->n_vattr.va_type));
2431 cache_enter(ndp->ni_dvp, ndp->ni_vp, cnp);
2434 /* Just skip over the file handle */
2435 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2436 i = fxdr_unsigned(int, *tl);
2438 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2439 fhsize = fxdr_unsigned(int, *tl);
2440 nfsm_adv(nfsm_rndup(fhsize));
2443 if (newvp != NULLVP) {
2450 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2451 more_dirs = fxdr_unsigned(int, *tl);
2454 * If at end of rpc data, get the eof boolean
2457 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2458 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2463 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2464 * by increasing d_reclen for the last record.
2467 left = DIRBLKSIZ - blksiz;
2468 dp->d_reclen += left;
2469 uiop->uio_iov->iov_base =
2470 (char *)uiop->uio_iov->iov_base + left;
2471 uiop->uio_iov->iov_len -= left;
2472 uiop->uio_offset += left;
2473 uiop->uio_resid -= left;
2477 * We are now either at the end of the directory or have filled the
2481 dnp->n_direofoffset = uiop->uio_offset;
2483 if (uiop->uio_resid > 0)
2484 nfs_printf("EEK! readdirplusrpc resid > 0\n");
2485 nfs_dircookie_lock(dnp);
2486 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2488 nfs_dircookie_unlock(dnp);
2491 if (newvp != NULLVP) {
2502 * Silly rename. To make the NFS filesystem that is stateless look a little
2503 * more like the "ufs" a remove of an active vnode is translated to a rename
2504 * to a funny looking filename that is removed by nfs_inactive on the
2505 * nfsnode. There is the potential for another process on a different client
2506 * to create the same funny name between the nfs_lookitup() fails and the
2507 * nfs_rename() completes, but...
2510 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2512 struct sillyrename *sp;
2516 unsigned int lticks;
2521 if (vp->v_type == VDIR)
2522 panic("nfs: sillyrename dir");
2524 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2525 M_NFSREQ, M_WAITOK);
2526 sp->s_cred = crhold(cnp->cn_cred);
2528 sp->s_removeit = nfs_removeit;
2532 * Fudge together a funny name.
2533 * Changing the format of the funny name to accomodate more
2534 * sillynames per directory.
2535 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2536 * CPU ticks since boot.
2538 pid = cnp->cn_thread->td_proc->p_pid;
2539 lticks = (unsigned int)ticks;
2541 sp->s_namlen = sprintf(sp->s_name,
2542 ".nfs.%08x.%04x4.4", lticks,
2544 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2545 cnp->cn_thread, NULL))
2549 error = nfs_renameit(dvp, cnp, sp);
2552 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2553 cnp->cn_thread, &np);
2554 np->n_sillyrename = sp;
2559 free((caddr_t)sp, M_NFSREQ);
2564 * Look up a file name and optionally either update the file handle or
2565 * allocate an nfsnode, depending on the value of npp.
2566 * npp == NULL --> just do the lookup
2567 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2569 * *npp != NULL --> update the file handle in the vnode
2572 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2573 struct thread *td, struct nfsnode **npp)
2575 struct vnode *newvp = NULL;
2576 struct nfsnode *np, *dnp = VTONFS(dvp);
2578 int error = 0, fhlen, attrflag;
2579 struct mbuf *mreq, *mrep, *md, *mb;
2581 int v3 = NFS_ISV3(dvp);
2583 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2584 mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2585 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2587 bpos = mtod(mb, caddr_t);
2588 nfsm_fhtom(dvp, v3);
2589 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2590 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2591 if (npp && !error) {
2592 nfsm_getfh(nfhp, fhlen, v3);
2595 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2596 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2597 np->n_fhp = &np->n_fh;
2598 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2599 np->n_fhp =(nfsfh_t *)malloc(fhlen, M_NFSBIGFH, M_WAITOK);
2600 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2601 np->n_fhsize = fhlen;
2603 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2607 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, LK_EXCLUSIVE);
2615 nfsm_postop_attr(newvp, attrflag);
2616 if (!attrflag && *npp == NULL) {
2625 nfsm_loadattr(newvp, NULL);
2629 if (npp && *npp == NULL) {
2644 * Nfs Version 3 commit rpc
2647 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2651 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2653 int error = 0, wccflag = NFSV3_WCCRATTR;
2654 struct mbuf *mreq, *mrep, *md, *mb;
2656 mtx_lock(&nmp->nm_mtx);
2657 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2658 mtx_unlock(&nmp->nm_mtx);
2661 mtx_unlock(&nmp->nm_mtx);
2662 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2663 mreq = nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2665 bpos = mtod(mb, caddr_t);
2667 tl = nfsm_build(u_int32_t *, 3 * NFSX_UNSIGNED);
2668 txdr_hyper(offset, tl);
2670 *tl = txdr_unsigned(cnt);
2671 nfsm_request(vp, NFSPROC_COMMIT, td, cred);
2672 nfsm_wcc_data(vp, wccflag);
2674 tl = nfsm_dissect(u_int32_t *, NFSX_V3WRITEVERF);
2675 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2676 NFSX_V3WRITEVERF)) {
2677 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2679 error = NFSERR_STALEWRITEVERF;
2689 * For async requests when nfsiod(s) are running, queue the request by
2690 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2694 nfs_strategy(struct vop_strategy_args *ap)
2696 struct buf *bp = ap->a_bp;
2699 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2700 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2702 if (bp->b_iocmd == BIO_READ)
2708 * If the op is asynchronous and an i/o daemon is waiting
2709 * queue the request, wake it up and wait for completion
2710 * otherwise just do it ourselves.
2712 if ((bp->b_flags & B_ASYNC) == 0 ||
2713 nfs_asyncio(VFSTONFS(ap->a_vp->v_mount), bp, NOCRED, curthread))
2714 (void)nfs_doio(ap->a_vp, bp, cr, curthread);
2719 * fsync vnode op. Just call nfs_flush() with commit == 1.
2723 nfs_fsync(struct vop_fsync_args *ap)
2725 return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2729 * Flush all the blocks associated with a vnode.
2730 * Walk through the buffer pool and push any dirty pages
2731 * associated with the vnode.
2734 nfs_flush(struct vnode *vp, int waitfor, struct thread *td,
2737 struct nfsnode *np = VTONFS(vp);
2741 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2742 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2744 u_quad_t off, endoff, toff;
2745 struct ucred* wcred = NULL;
2746 struct buf **bvec = NULL;
2747 #ifndef NFS_COMMITBVECSIZ
2748 #define NFS_COMMITBVECSIZ 20
2750 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2751 int bvecsize = 0, bveccount;
2753 if (nmp->nm_flag & NFSMNT_INT)
2758 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2759 * server, but has not been committed to stable storage on the server
2760 * yet. On the first pass, the byte range is worked out and the commit
2761 * rpc is done. On the second pass, nfs_writebp() is called to do the
2768 if (NFS_ISV3(vp) && commit) {
2770 if (bvec != NULL && bvec != bvec_on_stack)
2773 * Count up how many buffers waiting for a commit.
2777 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
2778 if (BUF_REFCNT(bp) == 0 &&
2779 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2780 == (B_DELWRI | B_NEEDCOMMIT))
2784 * Allocate space to remember the list of bufs to commit. It is
2785 * important to use M_NOWAIT here to avoid a race with nfs_write.
2786 * If we can't get memory (for whatever reason), we will end up
2787 * committing the buffers one-by-one in the loop below.
2789 if (bveccount > NFS_COMMITBVECSIZ) {
2791 * Release the vnode interlock to avoid a lock
2795 bvec = (struct buf **)
2796 malloc(bveccount * sizeof(struct buf *),
2800 bvec = bvec_on_stack;
2801 bvecsize = NFS_COMMITBVECSIZ;
2803 bvecsize = bveccount;
2805 bvec = bvec_on_stack;
2806 bvecsize = NFS_COMMITBVECSIZ;
2808 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
2809 if (bvecpos >= bvecsize)
2811 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2812 nbp = TAILQ_NEXT(bp, b_bobufs);
2815 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2816 (B_DELWRI | B_NEEDCOMMIT)) {
2818 nbp = TAILQ_NEXT(bp, b_bobufs);
2824 * Work out if all buffers are using the same cred
2825 * so we can deal with them all with one commit.
2827 * NOTE: we are not clearing B_DONE here, so we have
2828 * to do it later on in this routine if we intend to
2829 * initiate I/O on the bp.
2831 * Note: to avoid loopback deadlocks, we do not
2832 * assign b_runningbufspace.
2835 wcred = bp->b_wcred;
2836 else if (wcred != bp->b_wcred)
2838 vfs_busy_pages(bp, 1);
2842 * bp is protected by being locked, but nbp is not
2843 * and vfs_busy_pages() may sleep. We have to
2846 nbp = TAILQ_NEXT(bp, b_bobufs);
2849 * A list of these buffers is kept so that the
2850 * second loop knows which buffers have actually
2851 * been committed. This is necessary, since there
2852 * may be a race between the commit rpc and new
2853 * uncommitted writes on the file.
2855 bvec[bvecpos++] = bp;
2856 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2860 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2869 * Commit data on the server, as required.
2870 * If all bufs are using the same wcred, then use that with
2871 * one call for all of them, otherwise commit each one
2874 if (wcred != NOCRED)
2875 retv = nfs_commit(vp, off, (int)(endoff - off),
2879 for (i = 0; i < bvecpos; i++) {
2882 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2884 size = (u_quad_t)(bp->b_dirtyend
2886 retv = nfs_commit(vp, off, (int)size,
2892 if (retv == NFSERR_STALEWRITEVERF)
2893 nfs_clearcommit(vp->v_mount);
2896 * Now, either mark the blocks I/O done or mark the
2897 * blocks dirty, depending on whether the commit
2900 for (i = 0; i < bvecpos; i++) {
2902 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2905 * Error, leave B_DELWRI intact
2907 vfs_unbusy_pages(bp);
2911 * Success, remove B_DELWRI ( bundirty() ).
2913 * b_dirtyoff/b_dirtyend seem to be NFS
2914 * specific. We should probably move that
2915 * into bundirty(). XXX
2918 bufobj_wref(&vp->v_bufobj);
2919 bp->b_flags |= B_ASYNC;
2921 bp->b_flags &= ~B_DONE;
2922 bp->b_ioflags &= ~BIO_ERROR;
2923 bp->b_dirtyoff = bp->b_dirtyend = 0;
2931 * Start/do any write(s) that are required.
2936 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
2937 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2938 if (waitfor != MNT_WAIT || passone)
2941 error = BUF_TIMELOCK(bp,
2942 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2943 VI_MTX(vp), "nfsfsync", slpflag, slptimeo);
2949 if (error == ENOLCK)
2951 if (nfs_sigintr(nmp, NULL, td)) {
2955 if (slpflag == PCATCH) {
2961 if ((bp->b_flags & B_DELWRI) == 0)
2962 panic("nfs_fsync: not dirty");
2963 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2969 if (passone || !commit)
2970 bp->b_flags |= B_ASYNC;
2972 bp->b_flags |= B_ASYNC;
2975 if (nfs_sigintr(nmp, NULL, td)) {
2987 if (waitfor == MNT_WAIT) {
2988 while (vp->v_bufobj.bo_numoutput) {
2989 error = bufobj_wwait(&vp->v_bufobj, slpflag, slptimeo);
2992 error = nfs_sigintr(nmp, NULL, td);
2995 if (slpflag == PCATCH) {
3002 if (vp->v_bufobj.bo_dirty.bv_cnt != 0 && commit) {
3007 * Wait for all the async IO requests to drain
3010 mtx_lock(&np->n_mtx);
3011 while (np->n_directio_asyncwr > 0) {
3012 np->n_flag |= NFSYNCWAIT;
3013 error = nfs_msleep(td, (caddr_t)&np->n_directio_asyncwr,
3014 &np->n_mtx, slpflag | (PRIBIO + 1),
3017 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
3018 mtx_unlock(&np->n_mtx);
3024 mtx_unlock(&np->n_mtx);
3027 mtx_lock(&np->n_mtx);
3028 if (np->n_flag & NWRITEERR) {
3029 error = np->n_error;
3030 np->n_flag &= ~NWRITEERR;
3032 if (commit && vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
3033 vp->v_bufobj.bo_numoutput == 0 && np->n_directio_asyncwr == 0)
3034 np->n_flag &= ~NMODIFIED;
3035 mtx_unlock(&np->n_mtx);
3037 if (bvec != NULL && bvec != bvec_on_stack)
3043 * NFS advisory byte-level locks.
3046 nfs_advlock(struct vop_advlock_args *ap)
3051 if ((VFSTONFS(ap->a_vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3052 struct nfsnode *np = VTONFS(ap->a_vp);
3054 error = lf_advlock(ap, &(np->n_lockf), np->n_size);
3057 error = nfs_dolock(ap);
3064 * Print out the contents of an nfsnode.
3067 nfs_print(struct vop_print_args *ap)
3069 struct vnode *vp = ap->a_vp;
3070 struct nfsnode *np = VTONFS(vp);
3072 nfs_printf("\tfileid %ld fsid 0x%x",
3073 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3074 if (vp->v_type == VFIFO)
3081 * This is the "real" nfs::bwrite(struct buf*).
3082 * We set B_CACHE if this is a VMIO buffer.
3085 nfs_writebp(struct buf *bp, int force __unused, struct thread *td)
3088 int oldflags = bp->b_flags;
3094 if (BUF_REFCNT(bp) == 0)
3095 panic("bwrite: buffer is not locked???");
3097 if (bp->b_flags & B_INVAL) {
3102 bp->b_flags |= B_CACHE;
3105 * Undirty the bp. We will redirty it later if the I/O fails.
3110 bp->b_flags &= ~B_DONE;
3111 bp->b_ioflags &= ~BIO_ERROR;
3112 bp->b_iocmd = BIO_WRITE;
3114 bufobj_wref(bp->b_bufobj);
3115 curthread->td_proc->p_stats->p_ru.ru_oublock++;
3119 * Note: to avoid loopback deadlocks, we do not
3120 * assign b_runningbufspace.
3122 vfs_busy_pages(bp, 1);
3125 bp->b_iooffset = dbtob(bp->b_blkno);
3128 if( (oldflags & B_ASYNC) == 0) {
3129 int rtval = bufwait(bp);
3131 if (oldflags & B_DELWRI) {
3144 * nfs special file access vnode op.
3145 * Essentially just get vattr and then imitate iaccess() since the device is
3146 * local to the client.
3149 nfsspec_access(struct vop_access_args *ap)
3152 struct ucred *cred = ap->a_cred;
3153 struct vnode *vp = ap->a_vp;
3154 mode_t mode = ap->a_mode;
3159 * Disallow write attempts on filesystems mounted read-only;
3160 * unless the file is a socket, fifo, or a block or character
3161 * device resident on the filesystem.
3163 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3164 switch (vp->v_type) {
3174 error = VOP_GETATTR(vp, vap, cred, ap->a_td);
3177 error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3184 * Read wrapper for fifos.
3187 nfsfifo_read(struct vop_read_args *ap)
3189 struct nfsnode *np = VTONFS(ap->a_vp);
3195 mtx_lock(&np->n_mtx);
3197 getnanotime(&np->n_atim);
3198 mtx_unlock(&np->n_mtx);
3199 error = fifo_specops.vop_read(ap);
3204 * Write wrapper for fifos.
3207 nfsfifo_write(struct vop_write_args *ap)
3209 struct nfsnode *np = VTONFS(ap->a_vp);
3214 mtx_lock(&np->n_mtx);
3216 getnanotime(&np->n_mtim);
3217 mtx_unlock(&np->n_mtx);
3218 return(fifo_specops.vop_write(ap));
3222 * Close wrapper for fifos.
3224 * Update the times on the nfsnode then do fifo close.
3227 nfsfifo_close(struct vop_close_args *ap)
3229 struct vnode *vp = ap->a_vp;
3230 struct nfsnode *np = VTONFS(vp);
3234 mtx_lock(&np->n_mtx);
3235 if (np->n_flag & (NACC | NUPD)) {
3237 if (np->n_flag & NACC)
3239 if (np->n_flag & NUPD)
3242 if (vrefcnt(vp) == 1 &&
3243 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3245 if (np->n_flag & NACC)
3246 vattr.va_atime = np->n_atim;
3247 if (np->n_flag & NUPD)
3248 vattr.va_mtime = np->n_mtim;
3249 mtx_unlock(&np->n_mtx);
3250 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_td);
3254 mtx_unlock(&np->n_mtx);
3256 return (fifo_specops.vop_close(ap));
3260 * Just call nfs_writebp() with the force argument set to 1.
3262 * NOTE: B_DONE may or may not be set in a_bp on call.
3265 nfs_bwrite(struct buf *bp)
3268 return (nfs_writebp(bp, 1, curthread));
3271 struct buf_ops buf_ops_nfs = {
3272 .bop_name = "buf_ops_nfs",
3273 .bop_write = nfs_bwrite,
3274 .bop_strategy = bufstrategy,
3275 .bop_sync = bufsync,