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>
54 #include <sys/malloc.h>
56 #include <sys/namei.h>
57 #include <sys/socket.h>
58 #include <sys/vnode.h>
59 #include <sys/dirent.h>
60 #include <sys/fcntl.h>
61 #include <sys/lockf.h>
62 #include <sys/rwlock.h>
64 #include <sys/sysctl.h>
65 #include <sys/signalvar.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_object.h>
71 #include <nfs/nfsproto.h>
72 #include <nfsclient/nfs.h>
73 #include <nfsclient/nfsnode.h>
74 #include <nfsclient/nfsmount.h>
75 #include <nfs/nfs_kdtrace.h>
76 #include <nfs/nfs_lock.h>
77 #include <nfs/xdr_subs.h>
78 #include <nfsclient/nfsm_subs.h>
81 #include <net/if_var.h>
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
86 #include <machine/stdarg.h>
89 #include <sys/dtrace_bsd.h>
91 dtrace_nfsclient_accesscache_flush_probe_func_t
92 dtrace_nfsclient_accesscache_flush_done_probe;
93 uint32_t nfsclient_accesscache_flush_done_id;
95 dtrace_nfsclient_accesscache_get_probe_func_t
96 dtrace_nfsclient_accesscache_get_hit_probe,
97 dtrace_nfsclient_accesscache_get_miss_probe;
98 uint32_t nfsclient_accesscache_get_hit_id;
99 uint32_t nfsclient_accesscache_get_miss_id;
101 dtrace_nfsclient_accesscache_load_probe_func_t
102 dtrace_nfsclient_accesscache_load_done_probe;
103 uint32_t nfsclient_accesscache_load_done_id;
104 #endif /* !KDTRACE_HOOKS */
111 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
112 * calls are not in getblk() and brelse() so that they would not be necessary
116 #define vfs_busy_pages(bp, f)
119 static vop_read_t nfsfifo_read;
120 static vop_write_t nfsfifo_write;
121 static vop_close_t nfsfifo_close;
122 static int nfs_flush(struct vnode *, int, int);
123 static int nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *);
124 static vop_lookup_t nfs_lookup;
125 static vop_create_t nfs_create;
126 static vop_mknod_t nfs_mknod;
127 static vop_open_t nfs_open;
128 static vop_close_t nfs_close;
129 static vop_access_t nfs_access;
130 static vop_getattr_t nfs_getattr;
131 static vop_setattr_t nfs_setattr;
132 static vop_read_t nfs_read;
133 static vop_fsync_t nfs_fsync;
134 static vop_remove_t nfs_remove;
135 static vop_link_t nfs_link;
136 static vop_rename_t nfs_rename;
137 static vop_mkdir_t nfs_mkdir;
138 static vop_rmdir_t nfs_rmdir;
139 static vop_symlink_t nfs_symlink;
140 static vop_readdir_t nfs_readdir;
141 static vop_strategy_t nfs_strategy;
142 static int nfs_lookitup(struct vnode *, const char *, int,
143 struct ucred *, struct thread *, struct nfsnode **);
144 static int nfs_sillyrename(struct vnode *, struct vnode *,
145 struct componentname *);
146 static vop_access_t nfsspec_access;
147 static vop_readlink_t nfs_readlink;
148 static vop_print_t nfs_print;
149 static vop_advlock_t nfs_advlock;
150 static vop_advlockasync_t nfs_advlockasync;
153 * Global vfs data structures for nfs
155 struct vop_vector nfs_vnodeops = {
156 .vop_default = &default_vnodeops,
157 .vop_access = nfs_access,
158 .vop_advlock = nfs_advlock,
159 .vop_advlockasync = nfs_advlockasync,
160 .vop_close = nfs_close,
161 .vop_create = nfs_create,
162 .vop_fsync = nfs_fsync,
163 .vop_getattr = nfs_getattr,
164 .vop_getpages = nfs_getpages,
165 .vop_putpages = nfs_putpages,
166 .vop_inactive = nfs_inactive,
167 .vop_link = nfs_link,
168 .vop_lookup = nfs_lookup,
169 .vop_mkdir = nfs_mkdir,
170 .vop_mknod = nfs_mknod,
171 .vop_open = nfs_open,
172 .vop_print = nfs_print,
173 .vop_read = nfs_read,
174 .vop_readdir = nfs_readdir,
175 .vop_readlink = nfs_readlink,
176 .vop_reclaim = nfs_reclaim,
177 .vop_remove = nfs_remove,
178 .vop_rename = nfs_rename,
179 .vop_rmdir = nfs_rmdir,
180 .vop_setattr = nfs_setattr,
181 .vop_strategy = nfs_strategy,
182 .vop_symlink = nfs_symlink,
183 .vop_write = nfs_write,
186 struct vop_vector nfs_fifoops = {
187 .vop_default = &fifo_specops,
188 .vop_access = nfsspec_access,
189 .vop_close = nfsfifo_close,
190 .vop_fsync = nfs_fsync,
191 .vop_getattr = nfs_getattr,
192 .vop_inactive = nfs_inactive,
193 .vop_print = nfs_print,
194 .vop_read = nfsfifo_read,
195 .vop_reclaim = nfs_reclaim,
196 .vop_setattr = nfs_setattr,
197 .vop_write = nfsfifo_write,
200 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
201 struct componentname *cnp, struct vattr *vap);
202 static int nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
203 struct ucred *cred, struct thread *td);
204 static int nfs_renamerpc(struct vnode *fdvp, const char *fnameptr,
205 int fnamelen, struct vnode *tdvp,
206 const char *tnameptr, int tnamelen,
207 struct ucred *cred, struct thread *td);
208 static int nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
209 struct sillyrename *sp);
214 struct mtx nfs_iod_mtx;
215 enum nfsiod_state nfs_iodwant[NFS_MAXASYNCDAEMON];
216 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
217 int nfs_numasync = 0;
218 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
220 SYSCTL_DECL(_vfs_oldnfs);
222 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
223 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
224 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
226 static int nfs_prime_access_cache = 0;
227 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
228 &nfs_prime_access_cache, 0,
229 "Prime NFS ACCESS cache when fetching attributes");
231 static int nfsv3_commit_on_close = 0;
232 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
233 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
235 static int nfs_clean_pages_on_close = 1;
236 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
237 &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
239 int nfs_directio_enable = 0;
240 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
241 &nfs_directio_enable, 0, "Enable NFS directio");
244 * This sysctl allows other processes to mmap a file that has been opened
245 * O_DIRECT by a process. In general, having processes mmap the file while
246 * Direct IO is in progress can lead to Data Inconsistencies. But, we allow
247 * this by default to prevent DoS attacks - to prevent a malicious user from
248 * opening up files O_DIRECT preventing other users from mmap'ing these
249 * files. "Protected" environments where stricter consistency guarantees are
250 * required can disable this knob. The process that opened the file O_DIRECT
251 * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
254 int nfs_directio_allow_mmap = 1;
255 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
256 &nfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
259 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
260 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
262 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
263 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
266 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
267 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
268 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
272 * The list of locks after the description of the lock is the ordering
273 * of other locks acquired with the lock held.
274 * np->n_mtx : Protects the fields in the nfsnode.
276 VI_MTX (acquired indirectly)
277 * nmp->nm_mtx : Protects the fields in the nfsmount.
279 * nfs_iod_mtx : Global lock, protects shared nfsiod state.
280 * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
283 * rep->r_mtx : Protects the fields in an nfsreq.
287 nfs3_access_otw(struct vnode *vp, int wmode, struct thread *td,
288 struct ucred *cred, uint32_t *retmode)
292 int error = 0, attrflag, i, lrupos;
294 struct mbuf *mreq, *mrep, *md, *mb;
297 struct nfsnode *np = VTONFS(vp);
299 nfsstats.rpccnt[NFSPROC_ACCESS]++;
300 mreq = m_get2(NFSX_FH(v3) + NFSX_UNSIGNED, M_WAITOK, MT_DATA, 0);
302 bpos = mtod(mb, caddr_t);
304 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
305 *tl = txdr_unsigned(wmode);
306 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
307 nfsm_postop_attr(vp, attrflag);
310 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
311 rmode = fxdr_unsigned(u_int32_t, *tl);
312 mtx_lock(&np->n_mtx);
313 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
314 if (np->n_accesscache[i].uid == cred->cr_uid) {
315 np->n_accesscache[i].mode = rmode;
316 np->n_accesscache[i].stamp = time_second;
319 if (i > 0 && np->n_accesscache[i].stamp <
320 np->n_accesscache[lrupos].stamp)
323 if (i == NFS_ACCESSCACHESIZE) {
324 np->n_accesscache[lrupos].uid = cred->cr_uid;
325 np->n_accesscache[lrupos].mode = rmode;
326 np->n_accesscache[lrupos].stamp = time_second;
328 mtx_unlock(&np->n_mtx);
331 KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, rmode, 0);
337 KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, 0,
345 * nfs access vnode op.
346 * For nfs version 2, just return ok. File accesses may fail later.
347 * For nfs version 3, use the access rpc to check accessibility. If file modes
348 * are changed on the server, accesses might still fail later.
351 nfs_access(struct vop_access_args *ap)
353 struct vnode *vp = ap->a_vp;
354 int error = 0, i, gotahit;
355 u_int32_t mode, rmode, wmode;
356 int v3 = NFS_ISV3(vp);
357 struct nfsnode *np = VTONFS(vp);
360 * Disallow write attempts on filesystems mounted read-only;
361 * unless the file is a socket, fifo, or a block or character
362 * device resident on the filesystem.
364 if ((ap->a_accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
365 switch (vp->v_type) {
375 * For nfs v3, check to see if we have done this recently, and if
376 * so return our cached result instead of making an ACCESS call.
377 * If not, do an access rpc, otherwise you are stuck emulating
378 * ufs_access() locally using the vattr. This may not be correct,
379 * since the server may apply other access criteria such as
380 * client uid-->server uid mapping that we do not know about.
383 if (ap->a_accmode & VREAD)
384 mode = NFSV3ACCESS_READ;
387 if (vp->v_type != VDIR) {
388 if (ap->a_accmode & VWRITE)
389 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
390 if (ap->a_accmode & VEXEC)
391 mode |= NFSV3ACCESS_EXECUTE;
393 if (ap->a_accmode & VWRITE)
394 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
396 if (ap->a_accmode & VEXEC)
397 mode |= NFSV3ACCESS_LOOKUP;
399 /* XXX safety belt, only make blanket request if caching */
400 if (nfsaccess_cache_timeout > 0) {
401 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
402 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
403 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
409 * Does our cached result allow us to give a definite yes to
413 mtx_lock(&np->n_mtx);
414 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
415 if (ap->a_cred->cr_uid == np->n_accesscache[i].uid) {
416 if (time_second < (np->n_accesscache[i].stamp +
417 nfsaccess_cache_timeout) &&
418 (np->n_accesscache[i].mode & mode) == mode) {
419 nfsstats.accesscache_hits++;
425 mtx_unlock(&np->n_mtx);
428 KDTRACE_NFS_ACCESSCACHE_GET_HIT(vp,
429 ap->a_cred->cr_uid, mode);
431 KDTRACE_NFS_ACCESSCACHE_GET_MISS(vp,
432 ap->a_cred->cr_uid, mode);
436 * Either a no, or a don't know. Go to the wire.
438 nfsstats.accesscache_misses++;
439 error = nfs3_access_otw(vp, wmode, ap->a_td, ap->a_cred,
442 if ((rmode & mode) != mode)
448 if ((error = nfsspec_access(ap)) != 0) {
452 * Attempt to prevent a mapped root from accessing a file
453 * which it shouldn't. We try to read a byte from the file
454 * if the user is root and the file is not zero length.
455 * After calling nfsspec_access, we should have the correct
458 mtx_lock(&np->n_mtx);
459 if (ap->a_cred->cr_uid == 0 && (ap->a_accmode & VREAD)
460 && VTONFS(vp)->n_size > 0) {
465 mtx_unlock(&np->n_mtx);
468 auio.uio_iov = &aiov;
472 auio.uio_segflg = UIO_SYSSPACE;
473 auio.uio_rw = UIO_READ;
474 auio.uio_td = ap->a_td;
476 if (vp->v_type == VREG)
477 error = nfs_readrpc(vp, &auio, ap->a_cred);
478 else if (vp->v_type == VDIR) {
480 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
482 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
483 error = nfs_readdirrpc(vp, &auio, ap->a_cred);
485 } else if (vp->v_type == VLNK)
486 error = nfs_readlinkrpc(vp, &auio, ap->a_cred);
490 mtx_unlock(&np->n_mtx);
495 int nfs_otw_getattr_avoid = 0;
499 * Check to see if the type is ok
500 * and that deletion is not in progress.
501 * For paged in text files, you will need to flush the page cache
502 * if consistency is lost.
506 nfs_open(struct vop_open_args *ap)
508 struct vnode *vp = ap->a_vp;
509 struct nfsnode *np = VTONFS(vp);
512 int fmode = ap->a_mode;
515 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
519 * Get a valid lease. If cached data is stale, flush it.
521 mtx_lock(&np->n_mtx);
522 if (np->n_flag & NMODIFIED) {
523 mtx_unlock(&np->n_mtx);
524 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
525 if (error == EINTR || error == EIO)
527 mtx_lock(&np->n_mtx);
529 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
530 if (vp->v_type == VDIR)
531 np->n_direofoffset = 0;
532 mtx_unlock(&np->n_mtx);
533 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
536 mtx_lock(&np->n_mtx);
537 np->n_mtime = vattr.va_mtime;
539 mtx_unlock(&np->n_mtx);
540 error = VOP_GETATTR(vp, &vattr, ap->a_cred);
543 mtx_lock(&np->n_mtx);
544 if (NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
545 if (vp->v_type == VDIR)
546 np->n_direofoffset = 0;
547 mtx_unlock(&np->n_mtx);
548 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
549 if (error == EINTR || error == EIO) {
552 mtx_lock(&np->n_mtx);
553 np->n_mtime = vattr.va_mtime;
557 * If the object has >= 1 O_DIRECT active opens, we disable caching.
559 if (nfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
560 if (np->n_directio_opens == 0) {
561 mtx_unlock(&np->n_mtx);
562 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
565 mtx_lock(&np->n_mtx);
566 np->n_flag |= NNONCACHE;
568 np->n_directio_opens++;
572 * If this is an open for writing, capture a reference to the
573 * credentials, so they can be used by nfs_putpages(). Using
574 * these write credentials is preferable to the credentials of
575 * whatever thread happens to be doing the VOP_PUTPAGES() since
576 * the write RPCs are less likely to fail with EACCES.
578 if ((fmode & FWRITE) != 0) {
579 cred = np->n_writecred;
580 np->n_writecred = crhold(ap->a_cred);
583 mtx_unlock(&np->n_mtx);
586 vnode_create_vobject(vp, vattr.va_size, ap->a_td);
592 * What an NFS client should do upon close after writing is a debatable issue.
593 * Most NFS clients push delayed writes to the server upon close, basically for
595 * 1 - So that any write errors may be reported back to the client process
596 * doing the close system call. By far the two most likely errors are
597 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
598 * 2 - To put a worst case upper bound on cache inconsistency between
599 * multiple clients for the file.
600 * There is also a consistency problem for Version 2 of the protocol w.r.t.
601 * not being able to tell if other clients are writing a file concurrently,
602 * since there is no way of knowing if the changed modify time in the reply
603 * is only due to the write for this client.
604 * (NFS Version 3 provides weak cache consistency data in the reply that
605 * should be sufficient to detect and handle this case.)
607 * The current code does the following:
608 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
609 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
610 * or commit them (this satisfies 1 and 2 except for the
611 * case where the server crashes after this close but
612 * before the commit RPC, which is felt to be "good
613 * enough". Changing the last argument to nfs_flush() to
614 * a 1 would force a commit operation, if it is felt a
615 * commit is necessary now.
619 nfs_close(struct vop_close_args *ap)
621 struct vnode *vp = ap->a_vp;
622 struct nfsnode *np = VTONFS(vp);
624 int fmode = ap->a_fflag;
626 if (vp->v_type == VREG) {
628 * Examine and clean dirty pages, regardless of NMODIFIED.
629 * This closes a major hole in close-to-open consistency.
630 * We want to push out all dirty pages (and buffers) on
631 * close, regardless of whether they were dirtied by
632 * mmap'ed writes or via write().
634 if (nfs_clean_pages_on_close && vp->v_object) {
635 VM_OBJECT_WLOCK(vp->v_object);
636 vm_object_page_clean(vp->v_object, 0, 0, 0);
637 VM_OBJECT_WUNLOCK(vp->v_object);
639 mtx_lock(&np->n_mtx);
640 if (np->n_flag & NMODIFIED) {
641 mtx_unlock(&np->n_mtx);
644 * Under NFSv3 we have dirty buffers to dispose of. We
645 * must flush them to the NFS server. We have the option
646 * of waiting all the way through the commit rpc or just
647 * waiting for the initial write. The default is to only
648 * wait through the initial write so the data is in the
649 * server's cache, which is roughly similar to the state
650 * a standard disk subsystem leaves the file in on close().
652 * We cannot clear the NMODIFIED bit in np->n_flag due to
653 * potential races with other processes, and certainly
654 * cannot clear it if we don't commit.
656 int cm = nfsv3_commit_on_close ? 1 : 0;
657 error = nfs_flush(vp, MNT_WAIT, cm);
658 /* np->n_flag &= ~NMODIFIED; */
660 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
661 mtx_lock(&np->n_mtx);
663 if (np->n_flag & NWRITEERR) {
664 np->n_flag &= ~NWRITEERR;
667 mtx_unlock(&np->n_mtx);
669 if (nfs_directio_enable)
670 KASSERT((np->n_directio_asyncwr == 0),
671 ("nfs_close: dirty unflushed (%d) directio buffers\n",
672 np->n_directio_asyncwr));
673 if (nfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
674 mtx_lock(&np->n_mtx);
675 KASSERT((np->n_directio_opens > 0),
676 ("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
677 np->n_directio_opens--;
678 if (np->n_directio_opens == 0)
679 np->n_flag &= ~NNONCACHE;
680 mtx_unlock(&np->n_mtx);
686 * nfs getattr call from vfs.
689 nfs_getattr(struct vop_getattr_args *ap)
691 struct vnode *vp = ap->a_vp;
692 struct nfsnode *np = VTONFS(vp);
693 struct thread *td = curthread;
694 struct vattr *vap = ap->a_vap;
698 struct mbuf *mreq, *mrep, *md, *mb;
699 int v3 = NFS_ISV3(vp);
702 * Update local times for special files.
704 mtx_lock(&np->n_mtx);
705 if (np->n_flag & (NACC | NUPD))
707 mtx_unlock(&np->n_mtx);
709 * First look in the cache.
711 if (nfs_getattrcache(vp, &vattr) == 0)
713 if (v3 && nfs_prime_access_cache && nfsaccess_cache_timeout > 0) {
714 nfsstats.accesscache_misses++;
715 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, ap->a_cred, NULL);
716 if (nfs_getattrcache(vp, &vattr) == 0)
719 nfsstats.rpccnt[NFSPROC_GETATTR]++;
720 mreq = m_get2(NFSX_FH(v3), M_WAITOK, MT_DATA, 0);
722 bpos = mtod(mb, caddr_t);
724 nfsm_request(vp, NFSPROC_GETATTR, td, ap->a_cred);
726 nfsm_loadattr(vp, &vattr);
730 vap->va_type = vattr.va_type;
731 vap->va_mode = vattr.va_mode;
732 vap->va_nlink = vattr.va_nlink;
733 vap->va_uid = vattr.va_uid;
734 vap->va_gid = vattr.va_gid;
735 vap->va_fsid = vattr.va_fsid;
736 vap->va_fileid = vattr.va_fileid;
737 vap->va_size = vattr.va_size;
738 vap->va_blocksize = vattr.va_blocksize;
739 vap->va_atime = vattr.va_atime;
740 vap->va_mtime = vattr.va_mtime;
741 vap->va_ctime = vattr.va_ctime;
742 vap->va_gen = vattr.va_gen;
743 vap->va_flags = vattr.va_flags;
744 vap->va_rdev = vattr.va_rdev;
745 vap->va_bytes = vattr.va_bytes;
746 vap->va_filerev = vattr.va_filerev;
755 nfs_setattr(struct vop_setattr_args *ap)
757 struct vnode *vp = ap->a_vp;
758 struct nfsnode *np = VTONFS(vp);
759 struct vattr *vap = ap->a_vap;
760 struct thread *td = curthread;
769 * Setting of flags is not supported.
771 if (vap->va_flags != VNOVAL)
775 * Disallow write attempts if the filesystem is mounted read-only.
777 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
778 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
779 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
780 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
784 if (vap->va_size != VNOVAL) {
785 switch (vp->v_type) {
792 if (vap->va_mtime.tv_sec == VNOVAL &&
793 vap->va_atime.tv_sec == VNOVAL &&
794 vap->va_mode == (mode_t)VNOVAL &&
795 vap->va_uid == (uid_t)VNOVAL &&
796 vap->va_gid == (gid_t)VNOVAL)
798 vap->va_size = VNOVAL;
802 * Disallow write attempts if the filesystem is
805 if (vp->v_mount->mnt_flag & MNT_RDONLY)
808 * We run vnode_pager_setsize() early (why?),
809 * we must set np->n_size now to avoid vinvalbuf
810 * V_SAVE races that might setsize a lower
813 mtx_lock(&np->n_mtx);
815 mtx_unlock(&np->n_mtx);
816 error = nfs_meta_setsize(vp, ap->a_cred, td,
818 mtx_lock(&np->n_mtx);
819 if (np->n_flag & NMODIFIED) {
821 mtx_unlock(&np->n_mtx);
822 if (vap->va_size == 0)
823 error = nfs_vinvalbuf(vp, 0, td, 1);
825 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
827 vnode_pager_setsize(vp, tsize);
831 mtx_unlock(&np->n_mtx);
833 * np->n_size has already been set to vap->va_size
834 * in nfs_meta_setsize(). We must set it again since
835 * nfs_loadattrcache() could be called through
836 * nfs_meta_setsize() and could modify np->n_size.
838 mtx_lock(&np->n_mtx);
839 np->n_vattr.va_size = np->n_size = vap->va_size;
840 mtx_unlock(&np->n_mtx);
843 mtx_lock(&np->n_mtx);
844 if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
845 (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
846 mtx_unlock(&np->n_mtx);
847 if ((error = nfs_vinvalbuf(vp, V_SAVE, td, 1)) != 0 &&
848 (error == EINTR || error == EIO))
851 mtx_unlock(&np->n_mtx);
853 error = nfs_setattrrpc(vp, vap, ap->a_cred);
854 if (error && vap->va_size != VNOVAL) {
855 mtx_lock(&np->n_mtx);
856 np->n_size = np->n_vattr.va_size = tsize;
857 vnode_pager_setsize(vp, tsize);
858 mtx_unlock(&np->n_mtx);
865 * Do an nfs setattr rpc.
868 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred)
870 struct nfsv2_sattr *sp;
871 struct nfsnode *np = VTONFS(vp);
874 int error = 0, i, wccflag = NFSV3_WCCRATTR;
875 struct mbuf *mreq, *mrep, *md, *mb;
876 int v3 = NFS_ISV3(vp);
878 nfsstats.rpccnt[NFSPROC_SETATTR]++;
879 mreq = m_get2(NFSX_FH(v3) + NFSX_SATTR(v3), M_WAITOK, MT_DATA, 0);
881 bpos = mtod(mb, caddr_t);
884 nfsm_v3attrbuild(vap, TRUE);
885 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
888 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
889 if (vap->va_mode == (mode_t)VNOVAL)
890 sp->sa_mode = nfs_xdrneg1;
892 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
893 if (vap->va_uid == (uid_t)VNOVAL)
894 sp->sa_uid = nfs_xdrneg1;
896 sp->sa_uid = txdr_unsigned(vap->va_uid);
897 if (vap->va_gid == (gid_t)VNOVAL)
898 sp->sa_gid = nfs_xdrneg1;
900 sp->sa_gid = txdr_unsigned(vap->va_gid);
901 sp->sa_size = txdr_unsigned(vap->va_size);
902 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
903 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
905 nfsm_request(vp, NFSPROC_SETATTR, curthread, cred);
907 mtx_lock(&np->n_mtx);
908 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
909 np->n_accesscache[i].stamp = 0;
910 mtx_unlock(&np->n_mtx);
911 KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
912 nfsm_wcc_data(vp, wccflag);
914 nfsm_loadattr(vp, NULL);
921 * nfs lookup call, one step at a time...
922 * First look in cache
923 * If not found, unlock the directory nfsnode and do the rpc
926 nfs_lookup(struct vop_lookup_args *ap)
928 struct componentname *cnp = ap->a_cnp;
929 struct vnode *dvp = ap->a_dvp;
930 struct vnode **vpp = ap->a_vpp;
931 struct mount *mp = dvp->v_mount;
932 struct vattr dvattr, vattr;
933 struct timespec nctime;
934 int flags = cnp->cn_flags;
936 struct nfsmount *nmp;
938 struct mbuf *mreq, *mrep, *md, *mb;
941 struct nfsnode *np, *newnp;
942 int error = 0, attrflag, dattrflag, fhsize, ltype, ncticks;
943 int v3 = NFS_ISV3(dvp);
944 struct thread *td = cnp->cn_thread;
947 if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
948 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
950 if (dvp->v_type != VDIR)
954 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
958 error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
959 if (error > 0 && error != ENOENT)
963 * Lookups of "." are special and always return the
964 * current directory. cache_lookup() already handles
965 * associated locking bookkeeping, etc.
967 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
968 /* XXX: Is this really correct? */
969 if (cnp->cn_nameiop != LOOKUP &&
971 cnp->cn_flags |= SAVENAME;
976 * We only accept a positive hit in the cache if the
977 * change time of the file matches our cached copy.
978 * Otherwise, we discard the cache entry and fallback
979 * to doing a lookup RPC. We also only trust cache
980 * entries for less than nm_nametimeo seconds.
982 * To better handle stale file handles and attributes,
983 * clear the attribute cache of this node if it is a
984 * leaf component, part of an open() call, and not
985 * locally modified before fetching the attributes.
986 * This should allow stale file handles to be detected
987 * here where we can fall back to a LOOKUP RPC to
988 * recover rather than having nfs_open() detect the
989 * stale file handle and failing open(2) with ESTALE.
992 newnp = VTONFS(newvp);
993 if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
994 (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
995 !(newnp->n_flag & NMODIFIED)) {
996 mtx_lock(&newnp->n_mtx);
997 newnp->n_attrstamp = 0;
998 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
999 mtx_unlock(&newnp->n_mtx);
1001 if ((u_int)(ticks - ncticks) < (nmp->nm_nametimeo * hz) &&
1002 VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1003 timespeccmp(&vattr.va_ctime, &nctime, ==)) {
1004 nfsstats.lookupcache_hits++;
1005 if (cnp->cn_nameiop != LOOKUP &&
1007 cnp->cn_flags |= SAVENAME;
1016 } else if (error == ENOENT) {
1017 if (dvp->v_iflag & VI_DOOMED)
1020 * We only accept a negative hit in the cache if the
1021 * modification time of the parent directory matches
1022 * the cached copy in the name cache entry.
1023 * Otherwise, we discard all of the negative cache
1024 * entries for this directory. We also only trust
1025 * negative cache entries for up to nm_negnametimeo
1028 if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) &&
1029 VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1030 timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1031 nfsstats.lookupcache_hits++;
1034 cache_purge_negative(dvp);
1037 attrflag = dattrflag = 0;
1040 nfsstats.lookupcache_misses++;
1041 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1042 len = cnp->cn_namelen;
1043 mreq = m_get2(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len), M_WAITOK,
1046 bpos = mtod(mb, caddr_t);
1047 nfsm_fhtom(dvp, v3);
1048 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1049 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_thread, cnp->cn_cred);
1052 nfsm_postop_attr_va(dvp, dattrflag, &vattr);
1057 nfsm_getfh(fhp, fhsize, v3);
1060 * Handle RENAME case...
1062 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1063 if (NFS_CMPFH(np, fhp, fhsize)) {
1067 error = nfs_nget(mp, fhp, fhsize, &np, LK_EXCLUSIVE);
1074 nfsm_postop_attr(newvp, attrflag);
1075 nfsm_postop_attr(dvp, attrflag);
1077 nfsm_loadattr(newvp, NULL);
1080 cnp->cn_flags |= SAVENAME;
1084 if (flags & ISDOTDOT) {
1085 ltype = VOP_ISLOCKED(dvp);
1086 error = vfs_busy(mp, MBF_NOWAIT);
1090 error = vfs_busy(mp, 0);
1091 vn_lock(dvp, ltype | LK_RETRY);
1093 if (error == 0 && (dvp->v_iflag & VI_DOOMED)) {
1103 error = nfs_nget(mp, fhp, fhsize, &np, cnp->cn_lkflags);
1108 vn_lock(dvp, ltype | LK_RETRY);
1109 if (dvp->v_iflag & VI_DOOMED) {
1122 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1126 error = nfs_nget(mp, fhp, fhsize, &np, cnp->cn_lkflags);
1134 * Flush the attribute cache when opening a leaf node
1135 * to ensure that fresh attributes are fetched in
1136 * nfs_open() if we are unable to fetch attributes
1137 * from the LOOKUP reply.
1139 if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1140 !(np->n_flag & NMODIFIED)) {
1141 mtx_lock(&np->n_mtx);
1142 np->n_attrstamp = 0;
1143 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1144 mtx_unlock(&np->n_mtx);
1148 nfsm_postop_attr_va(newvp, attrflag, &vattr);
1149 nfsm_postop_attr_va(dvp, dattrflag, &dvattr);
1151 nfsm_loadattr(newvp, &vattr);
1154 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1155 cnp->cn_flags |= SAVENAME;
1156 if ((cnp->cn_flags & MAKEENTRY) &&
1157 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1158 attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1159 cache_enter_time(dvp, newvp, cnp, &vattr.va_ctime,
1160 newvp->v_type != VDIR ? NULL : &dvattr.va_ctime);
1165 if (newvp != NULLVP) {
1170 if (error != ENOENT)
1173 /* The requested file was not found. */
1174 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1175 (flags & ISLASTCN)) {
1177 * XXX: UFS does a full VOP_ACCESS(dvp,
1178 * VWRITE) here instead of just checking
1181 if (mp->mnt_flag & MNT_RDONLY)
1183 cnp->cn_flags |= SAVENAME;
1184 return (EJUSTRETURN);
1187 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE &&
1190 * Cache the modification time of the parent
1191 * directory from the post-op attributes in
1192 * the name cache entry. The negative cache
1193 * entry will be ignored once the directory
1194 * has changed. Don't bother adding the entry
1195 * if the directory has already changed.
1197 mtx_lock(&np->n_mtx);
1198 if (timespeccmp(&np->n_vattr.va_mtime,
1199 &vattr.va_mtime, ==)) {
1200 mtx_unlock(&np->n_mtx);
1201 cache_enter_time(dvp, NULL, cnp,
1202 &vattr.va_mtime, NULL);
1204 mtx_unlock(&np->n_mtx);
1214 * Just call nfs_bioread() to do the work.
1217 nfs_read(struct vop_read_args *ap)
1219 struct vnode *vp = ap->a_vp;
1221 switch (vp->v_type) {
1223 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1227 return (EOPNOTSUPP);
1235 nfs_readlink(struct vop_readlink_args *ap)
1237 struct vnode *vp = ap->a_vp;
1239 if (vp->v_type != VLNK)
1241 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred));
1245 * Do a readlink rpc.
1246 * Called by nfs_doio() from below the buffer cache.
1249 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1252 int error = 0, len, attrflag;
1253 struct mbuf *mreq, *mrep, *md, *mb;
1254 int v3 = NFS_ISV3(vp);
1256 nfsstats.rpccnt[NFSPROC_READLINK]++;
1257 mreq = m_get2(NFSX_FH(v3), M_WAITOK, MT_DATA, 0);
1259 bpos = mtod(mb, caddr_t);
1261 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, cred);
1263 nfsm_postop_attr(vp, attrflag);
1265 nfsm_strsiz(len, NFS_MAXPATHLEN);
1266 if (len == NFS_MAXPATHLEN) {
1267 struct nfsnode *np = VTONFS(vp);
1268 mtx_lock(&np->n_mtx);
1269 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1271 mtx_unlock(&np->n_mtx);
1273 nfsm_mtouio(uiop, len);
1285 nfs_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1289 struct mbuf *mreq, *mrep, *md, *mb;
1290 struct nfsmount *nmp;
1292 int error = 0, len, retlen, tsiz, eof, attrflag;
1293 int v3 = NFS_ISV3(vp);
1299 nmp = VFSTONFS(vp->v_mount);
1300 tsiz = uiop->uio_resid;
1301 mtx_lock(&nmp->nm_mtx);
1302 end = uiop->uio_offset + tsiz;
1303 if (end > nmp->nm_maxfilesize || end < uiop->uio_offset) {
1304 mtx_unlock(&nmp->nm_mtx);
1307 rsize = nmp->nm_rsize;
1308 mtx_unlock(&nmp->nm_mtx);
1310 nfsstats.rpccnt[NFSPROC_READ]++;
1311 len = (tsiz > rsize) ? rsize : tsiz;
1312 mreq = m_get2(NFSX_FH(v3) + NFSX_UNSIGNED * 3, M_WAITOK,
1315 bpos = mtod(mb, caddr_t);
1317 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED * 3);
1319 txdr_hyper(uiop->uio_offset, tl);
1320 *(tl + 2) = txdr_unsigned(len);
1322 *tl++ = txdr_unsigned(uiop->uio_offset);
1323 *tl++ = txdr_unsigned(len);
1326 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, cred);
1328 nfsm_postop_attr(vp, attrflag);
1333 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
1334 eof = fxdr_unsigned(int, *(tl + 1));
1336 nfsm_loadattr(vp, NULL);
1338 nfsm_strsiz(retlen, rsize);
1339 nfsm_mtouio(uiop, retlen);
1343 if (eof || retlen == 0) {
1346 } else if (retlen < len) {
1358 nfs_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1359 int *iomode, int *must_commit)
1364 struct mbuf *mreq, *mrep, *md, *mb;
1365 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1367 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1368 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1371 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1"));
1373 tsiz = uiop->uio_resid;
1374 mtx_lock(&nmp->nm_mtx);
1375 end = uiop->uio_offset + tsiz;
1376 if (end > nmp->nm_maxfilesize || end < uiop->uio_offset) {
1377 mtx_unlock(&nmp->nm_mtx);
1380 wsize = nmp->nm_wsize;
1381 mtx_unlock(&nmp->nm_mtx);
1383 nfsstats.rpccnt[NFSPROC_WRITE]++;
1384 len = (tsiz > wsize) ? wsize : tsiz;
1385 mreq = m_get2(NFSX_FH(v3) + 5 * NFSX_UNSIGNED, M_WAITOK,
1388 bpos = mtod(mb, caddr_t);
1391 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
1392 txdr_hyper(uiop->uio_offset, tl);
1394 *tl++ = txdr_unsigned(len);
1395 *tl++ = txdr_unsigned(*iomode);
1396 *tl = txdr_unsigned(len);
1400 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1401 /* Set both "begin" and "current" to non-garbage. */
1402 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1403 *tl++ = x; /* "begin offset" */
1404 *tl++ = x; /* "current offset" */
1405 x = txdr_unsigned(len);
1406 *tl++ = x; /* total to this offset */
1407 *tl = x; /* size of this write */
1409 nfsm_uiotom(uiop, len);
1410 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, cred);
1412 wccflag = NFSV3_WCCCHK;
1413 nfsm_wcc_data(vp, wccflag);
1415 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED
1416 + NFSX_V3WRITEVERF);
1417 rlen = fxdr_unsigned(int, *tl++);
1422 } else if (rlen < len) {
1423 backup = len - rlen;
1424 uiop->uio_iov->iov_base =
1425 (char *)uiop->uio_iov->iov_base -
1427 uiop->uio_iov->iov_len += backup;
1428 uiop->uio_offset -= backup;
1429 uiop->uio_resid += backup;
1432 commit = fxdr_unsigned(int, *tl++);
1435 * Return the lowest committment level
1436 * obtained by any of the RPCs.
1438 if (committed == NFSV3WRITE_FILESYNC)
1440 else if (committed == NFSV3WRITE_DATASYNC &&
1441 commit == NFSV3WRITE_UNSTABLE)
1443 mtx_lock(&nmp->nm_mtx);
1444 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1445 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1447 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1448 } else if (bcmp((caddr_t)tl,
1449 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1451 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1454 mtx_unlock(&nmp->nm_mtx);
1457 nfsm_loadattr(vp, NULL);
1460 mtx_lock(&(VTONFS(vp))->n_mtx);
1461 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime;
1462 mtx_unlock(&(VTONFS(vp))->n_mtx);
1471 committed = NFSV3WRITE_FILESYNC;
1472 *iomode = committed;
1474 uiop->uio_resid = tsiz;
1480 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1481 * mode set to specify the file type and the size field for rdev.
1484 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1487 struct nfsv2_sattr *sp;
1489 struct vnode *newvp = NULL;
1490 struct nfsnode *np = NULL;
1493 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1494 struct mbuf *mreq, *mrep, *md, *mb;
1496 int v3 = NFS_ISV3(dvp);
1498 if (vap->va_type == VCHR || vap->va_type == VBLK)
1499 rdev = txdr_unsigned(vap->va_rdev);
1500 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1503 return (EOPNOTSUPP);
1505 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
1507 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1508 mreq = m_get2(NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1509 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3), M_WAITOK, MT_DATA, 0);
1511 bpos = mtod(mb, caddr_t);
1512 nfsm_fhtom(dvp, v3);
1513 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1515 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1516 *tl++ = vtonfsv3_type(vap->va_type);
1517 nfsm_v3attrbuild(vap, FALSE);
1518 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1519 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
1520 *tl++ = txdr_unsigned(major(vap->va_rdev));
1521 *tl = txdr_unsigned(minor(vap->va_rdev));
1524 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1525 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1526 sp->sa_uid = nfs_xdrneg1;
1527 sp->sa_gid = nfs_xdrneg1;
1529 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1530 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1532 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_thread, cnp->cn_cred);
1534 nfsm_mtofh(dvp, newvp, v3, gotvp);
1540 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1541 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1547 nfsm_wcc_data(dvp, wccflag);
1556 mtx_lock(&(VTONFS(dvp))->n_mtx);
1557 VTONFS(dvp)->n_flag |= NMODIFIED;
1559 VTONFS(dvp)->n_attrstamp = 0;
1560 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1562 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1568 * just call nfs_mknodrpc() to do the work.
1572 nfs_mknod(struct vop_mknod_args *ap)
1574 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1577 static u_long create_verf;
1579 * nfs file create call
1582 nfs_create(struct vop_create_args *ap)
1584 struct vnode *dvp = ap->a_dvp;
1585 struct vattr *vap = ap->a_vap;
1586 struct componentname *cnp = ap->a_cnp;
1587 struct nfsv2_sattr *sp;
1589 struct nfsnode *np = NULL;
1590 struct vnode *newvp = NULL;
1592 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1593 struct mbuf *mreq, *mrep, *md, *mb;
1595 int v3 = NFS_ISV3(dvp);
1598 * Oops, not for me..
1600 if (vap->va_type == VSOCK) {
1601 error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap);
1605 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0) {
1608 if (vap->va_vaflags & VA_EXCLUSIVE)
1611 nfsstats.rpccnt[NFSPROC_CREATE]++;
1612 mreq = m_get2(NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1613 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3), M_WAITOK, MT_DATA, 0);
1615 bpos = mtod(mb, caddr_t);
1616 nfsm_fhtom(dvp, v3);
1617 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1619 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1620 if (fmode & O_EXCL) {
1621 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1622 tl = nfsm_build(u_int32_t *, NFSX_V3CREATEVERF);
1624 CURVNET_SET(CRED_TO_VNET(cnp->cn_cred));
1626 if (!TAILQ_EMPTY(&V_in_ifaddrhead))
1627 *tl++ = IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr.s_addr;
1630 *tl++ = create_verf;
1632 IN_IFADDR_RUNLOCK();
1635 *tl = ++create_verf;
1637 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1638 nfsm_v3attrbuild(vap, FALSE);
1641 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1642 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1643 sp->sa_uid = nfs_xdrneg1;
1644 sp->sa_gid = nfs_xdrneg1;
1646 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1647 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1649 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_thread, cnp->cn_cred);
1651 nfsm_mtofh(dvp, newvp, v3, gotvp);
1657 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1658 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1664 nfsm_wcc_data(dvp, wccflag);
1668 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1674 } else if (v3 && (fmode & O_EXCL)) {
1676 * We are normally called with only a partially initialized
1677 * VAP. Since the NFSv3 spec says that server may use the
1678 * file attributes to store the verifier, the spec requires
1679 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1680 * in atime, but we can't really assume that all servers will
1681 * so we ensure that our SETATTR sets both atime and mtime.
1683 if (vap->va_mtime.tv_sec == VNOVAL)
1684 vfs_timestamp(&vap->va_mtime);
1685 if (vap->va_atime.tv_sec == VNOVAL)
1686 vap->va_atime = vap->va_mtime;
1687 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred);
1694 mtx_lock(&(VTONFS(dvp))->n_mtx);
1695 VTONFS(dvp)->n_flag |= NMODIFIED;
1697 VTONFS(dvp)->n_attrstamp = 0;
1698 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1700 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1705 * nfs file remove call
1706 * To try and make nfs semantics closer to ufs semantics, a file that has
1707 * other processes using the vnode is renamed instead of removed and then
1708 * removed later on the last close.
1709 * - If v_usecount > 1
1710 * If a rename is not already in the works
1711 * call nfs_sillyrename() to set it up
1716 nfs_remove(struct vop_remove_args *ap)
1718 struct vnode *vp = ap->a_vp;
1719 struct vnode *dvp = ap->a_dvp;
1720 struct componentname *cnp = ap->a_cnp;
1721 struct nfsnode *np = VTONFS(vp);
1725 KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name"));
1726 KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
1727 if (vp->v_type == VDIR)
1729 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1730 !VOP_GETATTR(vp, &vattr, cnp->cn_cred) && vattr.va_nlink > 1)) {
1732 * Purge the name cache so that the chance of a lookup for
1733 * the name succeeding while the remove is in progress is
1734 * minimized. Without node locking it can still happen, such
1735 * that an I/O op returns ESTALE, but since you get this if
1736 * another host removes the file..
1740 * throw away biocache buffers, mainly to avoid
1741 * unnecessary delayed writes later.
1743 error = nfs_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1745 if (error != EINTR && error != EIO)
1746 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1747 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1749 * Kludge City: If the first reply to the remove rpc is lost..
1750 * the reply to the retransmitted request will be ENOENT
1751 * since the file was in fact removed
1752 * Therefore, we cheat and return success.
1754 if (error == ENOENT)
1756 } else if (!np->n_sillyrename)
1757 error = nfs_sillyrename(dvp, vp, cnp);
1758 mtx_lock(&np->n_mtx);
1759 np->n_attrstamp = 0;
1760 mtx_unlock(&np->n_mtx);
1761 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1766 * nfs file remove rpc called from nfs_inactive
1769 nfs_removeit(struct sillyrename *sp)
1772 * Make sure that the directory vnode is still valid.
1773 * XXX we should lock sp->s_dvp here.
1775 if (sp->s_dvp->v_type == VBAD)
1777 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1782 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1785 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1786 struct ucred *cred, struct thread *td)
1789 int error = 0, wccflag = NFSV3_WCCRATTR;
1790 struct mbuf *mreq, *mrep, *md, *mb;
1791 int v3 = NFS_ISV3(dvp);
1793 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1794 mreq = m_get2(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen),
1795 M_WAITOK, MT_DATA, 0);
1797 bpos = mtod(mb, caddr_t);
1798 nfsm_fhtom(dvp, v3);
1799 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1800 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1802 nfsm_wcc_data(dvp, wccflag);
1805 mtx_lock(&(VTONFS(dvp))->n_mtx);
1806 VTONFS(dvp)->n_flag |= NMODIFIED;
1808 VTONFS(dvp)->n_attrstamp = 0;
1809 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1811 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1816 * nfs file rename call
1819 nfs_rename(struct vop_rename_args *ap)
1821 struct vnode *fvp = ap->a_fvp;
1822 struct vnode *tvp = ap->a_tvp;
1823 struct vnode *fdvp = ap->a_fdvp;
1824 struct vnode *tdvp = ap->a_tdvp;
1825 struct componentname *tcnp = ap->a_tcnp;
1826 struct componentname *fcnp = ap->a_fcnp;
1829 KASSERT((tcnp->cn_flags & HASBUF) != 0 &&
1830 (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name"));
1831 /* Check for cross-device rename */
1832 if ((fvp->v_mount != tdvp->v_mount) ||
1833 (tvp && (fvp->v_mount != tvp->v_mount))) {
1839 nfs_printf("nfs_rename: fvp == tvp (can't happen)\n");
1843 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
1847 * We have to flush B_DELWRI data prior to renaming
1848 * the file. If we don't, the delayed-write buffers
1849 * can be flushed out later after the file has gone stale
1850 * under NFSV3. NFSV2 does not have this problem because
1851 * ( as far as I can tell ) it flushes dirty buffers more
1854 * Skip the rename operation if the fsync fails, this can happen
1855 * due to the server's volume being full, when we pushed out data
1856 * that was written back to our cache earlier. Not checking for
1857 * this condition can result in potential (silent) data loss.
1859 error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1862 error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1867 * If the tvp exists and is in use, sillyrename it before doing the
1868 * rename of the new file over it.
1869 * XXX Can't sillyrename a directory.
1871 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1872 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1877 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1878 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1881 if (fvp->v_type == VDIR) {
1882 if (tvp != NULL && tvp->v_type == VDIR)
1897 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1899 if (error == ENOENT)
1905 * nfs file rename rpc called from nfs_remove() above
1908 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1909 struct sillyrename *sp)
1912 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen, sdvp,
1913 sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_thread));
1917 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1920 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1921 struct vnode *tdvp, const char *tnameptr, int tnamelen, struct ucred *cred,
1925 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1926 struct mbuf *mreq, *mrep, *md, *mb;
1927 int v3 = NFS_ISV3(fdvp);
1929 nfsstats.rpccnt[NFSPROC_RENAME]++;
1930 mreq = m_get2((NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1931 nfsm_rndup(tnamelen), M_WAITOK, MT_DATA, 0);
1933 bpos = mtod(mb, caddr_t);
1934 nfsm_fhtom(fdvp, v3);
1935 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1936 nfsm_fhtom(tdvp, v3);
1937 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1938 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1940 nfsm_wcc_data(fdvp, fwccflag);
1941 nfsm_wcc_data(tdvp, twccflag);
1945 mtx_lock(&(VTONFS(fdvp))->n_mtx);
1946 VTONFS(fdvp)->n_flag |= NMODIFIED;
1947 mtx_unlock(&(VTONFS(fdvp))->n_mtx);
1948 mtx_lock(&(VTONFS(tdvp))->n_mtx);
1949 VTONFS(tdvp)->n_flag |= NMODIFIED;
1950 mtx_unlock(&(VTONFS(tdvp))->n_mtx);
1952 VTONFS(fdvp)->n_attrstamp = 0;
1953 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
1956 VTONFS(tdvp)->n_attrstamp = 0;
1957 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
1963 * nfs hard link create call
1966 nfs_link(struct vop_link_args *ap)
1968 struct vnode *vp = ap->a_vp;
1969 struct vnode *tdvp = ap->a_tdvp;
1970 struct componentname *cnp = ap->a_cnp;
1972 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1973 struct mbuf *mreq, *mrep, *md, *mb;
1976 if (vp->v_mount != tdvp->v_mount) {
1981 * Push all writes to the server, so that the attribute cache
1982 * doesn't get "out of sync" with the server.
1983 * XXX There should be a better way!
1985 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1988 nfsstats.rpccnt[NFSPROC_LINK]++;
1989 mreq = m_get2(NFSX_FH(v3)*2 + NFSX_UNSIGNED +
1990 nfsm_rndup(cnp->cn_namelen), M_WAITOK, MT_DATA, 0);
1992 bpos = mtod(mb, caddr_t);
1994 nfsm_fhtom(tdvp, v3);
1995 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1996 nfsm_request(vp, NFSPROC_LINK, cnp->cn_thread, cnp->cn_cred);
1998 nfsm_postop_attr(vp, attrflag);
1999 nfsm_wcc_data(tdvp, wccflag);
2003 mtx_lock(&(VTONFS(tdvp))->n_mtx);
2004 VTONFS(tdvp)->n_flag |= NMODIFIED;
2005 mtx_unlock(&(VTONFS(tdvp))->n_mtx);
2007 VTONFS(vp)->n_attrstamp = 0;
2008 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2011 VTONFS(tdvp)->n_attrstamp = 0;
2012 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2018 * nfs symbolic link create call
2021 nfs_symlink(struct vop_symlink_args *ap)
2023 struct vnode *dvp = ap->a_dvp;
2024 struct vattr *vap = ap->a_vap;
2025 struct componentname *cnp = ap->a_cnp;
2026 struct nfsv2_sattr *sp;
2028 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2029 struct mbuf *mreq, *mrep, *md, *mb;
2030 struct vnode *newvp = NULL;
2031 int v3 = NFS_ISV3(dvp);
2033 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2034 slen = strlen(ap->a_target);
2035 mreq = m_get2(NFSX_FH(v3) + 2*NFSX_UNSIGNED +
2036 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3),
2037 M_WAITOK, MT_DATA, 0);
2039 bpos = mtod(mb, caddr_t);
2040 nfsm_fhtom(dvp, v3);
2041 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2043 nfsm_v3attrbuild(vap, FALSE);
2045 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
2047 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
2048 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2049 sp->sa_uid = nfs_xdrneg1;
2050 sp->sa_gid = nfs_xdrneg1;
2051 sp->sa_size = nfs_xdrneg1;
2052 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2053 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2057 * Issue the NFS request and get the rpc response.
2059 * Only NFSv3 responses returning an error of 0 actually return
2060 * a file handle that can be converted into newvp without having
2061 * to do an extra lookup rpc.
2063 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_thread, cnp->cn_cred);
2066 nfsm_mtofh(dvp, newvp, v3, gotvp);
2067 nfsm_wcc_data(dvp, wccflag);
2071 * out code jumps -> here, mrep is also freed.
2078 * If we do not have an error and we could not extract the newvp from
2079 * the response due to the request being NFSv2, we have to do a
2080 * lookup in order to obtain a newvp to return.
2082 if (error == 0 && newvp == NULL) {
2083 struct nfsnode *np = NULL;
2085 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2086 cnp->cn_cred, cnp->cn_thread, &np);
2096 mtx_lock(&(VTONFS(dvp))->n_mtx);
2097 VTONFS(dvp)->n_flag |= NMODIFIED;
2098 mtx_unlock(&(VTONFS(dvp))->n_mtx);
2100 VTONFS(dvp)->n_attrstamp = 0;
2101 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2110 nfs_mkdir(struct vop_mkdir_args *ap)
2112 struct vnode *dvp = ap->a_dvp;
2113 struct vattr *vap = ap->a_vap;
2114 struct componentname *cnp = ap->a_cnp;
2115 struct nfsv2_sattr *sp;
2117 struct nfsnode *np = NULL;
2118 struct vnode *newvp = NULL;
2120 int error = 0, wccflag = NFSV3_WCCRATTR;
2122 struct mbuf *mreq, *mrep, *md, *mb;
2124 int v3 = NFS_ISV3(dvp);
2126 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2128 len = cnp->cn_namelen;
2129 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2130 mreq = m_get2(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) +
2131 NFSX_SATTR(v3), M_WAITOK, MT_DATA, 0);
2133 bpos = mtod(mb, caddr_t);
2134 nfsm_fhtom(dvp, v3);
2135 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
2137 nfsm_v3attrbuild(vap, FALSE);
2139 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
2140 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2141 sp->sa_uid = nfs_xdrneg1;
2142 sp->sa_gid = nfs_xdrneg1;
2143 sp->sa_size = nfs_xdrneg1;
2144 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2145 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2147 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_thread, cnp->cn_cred);
2149 nfsm_mtofh(dvp, newvp, v3, gotvp);
2151 nfsm_wcc_data(dvp, wccflag);
2154 mtx_lock(&(VTONFS(dvp))->n_mtx);
2155 VTONFS(dvp)->n_flag |= NMODIFIED;
2156 mtx_unlock(&(VTONFS(dvp))->n_mtx);
2158 VTONFS(dvp)->n_attrstamp = 0;
2159 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2161 if (error == 0 && newvp == NULL) {
2162 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2163 cnp->cn_thread, &np);
2166 if (newvp->v_type != VDIR)
2179 * nfs remove directory call
2182 nfs_rmdir(struct vop_rmdir_args *ap)
2184 struct vnode *vp = ap->a_vp;
2185 struct vnode *dvp = ap->a_dvp;
2186 struct componentname *cnp = ap->a_cnp;
2188 int error = 0, wccflag = NFSV3_WCCRATTR;
2189 struct mbuf *mreq, *mrep, *md, *mb;
2190 int v3 = NFS_ISV3(dvp);
2194 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2195 mreq = m_get2(NFSX_FH(v3) + NFSX_UNSIGNED +
2196 nfsm_rndup(cnp->cn_namelen), M_WAITOK, MT_DATA, 0);
2198 bpos = mtod(mb, caddr_t);
2199 nfsm_fhtom(dvp, v3);
2200 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2201 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_thread, cnp->cn_cred);
2203 nfsm_wcc_data(dvp, wccflag);
2206 mtx_lock(&(VTONFS(dvp))->n_mtx);
2207 VTONFS(dvp)->n_flag |= NMODIFIED;
2208 mtx_unlock(&(VTONFS(dvp))->n_mtx);
2210 VTONFS(dvp)->n_attrstamp = 0;
2211 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2216 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2218 if (error == ENOENT)
2227 nfs_readdir(struct vop_readdir_args *ap)
2229 struct vnode *vp = ap->a_vp;
2230 struct nfsnode *np = VTONFS(vp);
2231 struct uio *uio = ap->a_uio;
2232 int tresid, error = 0;
2235 if (vp->v_type != VDIR)
2239 * First, check for hit on the EOF offset cache
2241 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2242 (np->n_flag & NMODIFIED) == 0) {
2243 if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2244 mtx_lock(&np->n_mtx);
2245 if (!NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2246 mtx_unlock(&np->n_mtx);
2247 nfsstats.direofcache_hits++;
2250 mtx_unlock(&np->n_mtx);
2255 * Call nfs_bioread() to do the real work.
2257 tresid = uio->uio_resid;
2258 error = nfs_bioread(vp, uio, 0, ap->a_cred);
2260 if (!error && uio->uio_resid == tresid) {
2261 nfsstats.direofcache_misses++;
2269 * Called from below the buffer cache by nfs_doio().
2272 nfs_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
2275 struct dirent *dp = NULL;
2280 struct mbuf *mreq, *mrep, *md, *mb;
2282 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2283 struct nfsnode *dnp = VTONFS(vp);
2285 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2287 int v3 = NFS_ISV3(vp);
2289 KASSERT(uiop->uio_iovcnt == 1 &&
2290 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2291 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2292 ("nfs readdirrpc bad uio"));
2295 * If there is no cookie, assume directory was stale.
2297 nfs_dircookie_lock(dnp);
2298 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2301 nfs_dircookie_unlock(dnp);
2303 nfs_dircookie_unlock(dnp);
2304 return (NFSERR_BAD_COOKIE);
2308 * Loop around doing readdir rpc's of size nm_readdirsize
2309 * truncated to a multiple of DIRBLKSIZ.
2310 * The stopping criteria is EOF or buffer full.
2312 while (more_dirs && bigenough) {
2313 nfsstats.rpccnt[NFSPROC_READDIR]++;
2314 mreq = m_get2(NFSX_FH(v3) + NFSX_READDIR(v3), M_WAITOK,
2317 bpos = mtod(mb, caddr_t);
2320 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
2321 *tl++ = cookie.nfsuquad[0];
2322 *tl++ = cookie.nfsuquad[1];
2323 mtx_lock(&dnp->n_mtx);
2324 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2325 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2326 mtx_unlock(&dnp->n_mtx);
2328 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
2329 *tl++ = cookie.nfsuquad[0];
2331 *tl = txdr_unsigned(nmp->nm_readdirsize);
2332 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, cred);
2334 nfsm_postop_attr(vp, attrflag);
2336 tl = nfsm_dissect(u_int32_t *,
2338 mtx_lock(&dnp->n_mtx);
2339 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2340 dnp->n_cookieverf.nfsuquad[1] = *tl;
2341 mtx_unlock(&dnp->n_mtx);
2347 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2348 more_dirs = fxdr_unsigned(int, *tl);
2350 /* loop thru the dir entries, doctoring them to 4bsd form */
2351 while (more_dirs && bigenough) {
2353 tl = nfsm_dissect(u_int32_t *,
2355 fileno = fxdr_hyper(tl);
2356 len = fxdr_unsigned(int, *(tl + 2));
2358 tl = nfsm_dissect(u_int32_t *,
2360 fileno = fxdr_unsigned(u_quad_t, *tl++);
2361 len = fxdr_unsigned(int, *tl);
2363 if (len <= 0 || len > NFS_MAXNAMLEN) {
2368 tlen = nfsm_rndup(len);
2370 tlen += 4; /* To ensure null termination */
2371 left = DIRBLKSIZ - blksiz;
2372 if ((tlen + DIRHDSIZ) > left) {
2373 dp->d_reclen += left;
2374 uiop->uio_iov->iov_base =
2375 (char *)uiop->uio_iov->iov_base + left;
2376 uiop->uio_iov->iov_len -= left;
2377 uiop->uio_offset += left;
2378 uiop->uio_resid -= left;
2381 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2384 dp = (struct dirent *)uiop->uio_iov->iov_base;
2385 dp->d_fileno = (int)fileno;
2387 dp->d_reclen = tlen + DIRHDSIZ;
2388 dp->d_type = DT_UNKNOWN;
2389 blksiz += dp->d_reclen;
2390 if (blksiz == DIRBLKSIZ)
2392 uiop->uio_offset += DIRHDSIZ;
2393 uiop->uio_resid -= DIRHDSIZ;
2394 uiop->uio_iov->iov_base =
2395 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
2396 uiop->uio_iov->iov_len -= DIRHDSIZ;
2397 nfsm_mtouio(uiop, len);
2398 cp = uiop->uio_iov->iov_base;
2400 *cp = '\0'; /* null terminate */
2401 uiop->uio_iov->iov_base =
2402 (char *)uiop->uio_iov->iov_base + tlen;
2403 uiop->uio_iov->iov_len -= tlen;
2404 uiop->uio_offset += tlen;
2405 uiop->uio_resid -= tlen;
2407 nfsm_adv(nfsm_rndup(len));
2409 tl = nfsm_dissect(u_int32_t *,
2412 tl = nfsm_dissect(u_int32_t *,
2416 cookie.nfsuquad[0] = *tl++;
2418 cookie.nfsuquad[1] = *tl++;
2423 more_dirs = fxdr_unsigned(int, *tl);
2426 * If at end of rpc data, get the eof boolean
2429 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2430 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2435 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2436 * by increasing d_reclen for the last record.
2439 left = DIRBLKSIZ - blksiz;
2440 dp->d_reclen += left;
2441 uiop->uio_iov->iov_base =
2442 (char *)uiop->uio_iov->iov_base + left;
2443 uiop->uio_iov->iov_len -= left;
2444 uiop->uio_offset += left;
2445 uiop->uio_resid -= left;
2449 * We are now either at the end of the directory or have filled the
2453 dnp->n_direofoffset = uiop->uio_offset;
2455 if (uiop->uio_resid > 0)
2456 nfs_printf("EEK! readdirrpc resid > 0\n");
2457 nfs_dircookie_lock(dnp);
2458 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2460 nfs_dircookie_unlock(dnp);
2467 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2470 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
2476 struct vnode *newvp;
2478 caddr_t bpos, dpos, dpossav1, dpossav2;
2479 struct mbuf *mreq, *mrep, *md, *mb, *mdsav1, *mdsav2;
2480 struct nameidata nami, *ndp = &nami;
2481 struct componentname *cnp = &ndp->ni_cnd;
2483 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2484 struct nfsnode *dnp = VTONFS(vp), *np;
2485 struct vattr vattr, dvattr;
2488 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2489 int attrflag, dattrflag, fhsize;
2494 KASSERT(uiop->uio_iovcnt == 1 &&
2495 (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2496 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2497 ("nfs readdirplusrpc bad uio"));
2502 * If there is no cookie, assume directory was stale.
2504 nfs_dircookie_lock(dnp);
2505 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2508 nfs_dircookie_unlock(dnp);
2510 nfs_dircookie_unlock(dnp);
2511 return (NFSERR_BAD_COOKIE);
2514 * Loop around doing readdir rpc's of size nm_readdirsize
2515 * truncated to a multiple of DIRBLKSIZ.
2516 * The stopping criteria is EOF or buffer full.
2518 while (more_dirs && bigenough) {
2519 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2520 mreq = m_get2(NFSX_FH(1) + 6 * NFSX_UNSIGNED, M_WAITOK,
2523 bpos = mtod(mb, caddr_t);
2525 tl = nfsm_build(u_int32_t *, 6 * NFSX_UNSIGNED);
2526 *tl++ = cookie.nfsuquad[0];
2527 *tl++ = cookie.nfsuquad[1];
2528 mtx_lock(&dnp->n_mtx);
2529 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2530 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2531 mtx_unlock(&dnp->n_mtx);
2532 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2533 *tl = txdr_unsigned(nmp->nm_rsize);
2534 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, cred);
2535 nfsm_postop_attr_va(vp, dattrflag, &dvattr);
2540 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2541 mtx_lock(&dnp->n_mtx);
2542 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2543 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2544 mtx_unlock(&dnp->n_mtx);
2545 more_dirs = fxdr_unsigned(int, *tl);
2547 /* loop thru the dir entries, doctoring them to 4bsd form */
2548 while (more_dirs && bigenough) {
2549 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2550 fileno = fxdr_hyper(tl);
2551 len = fxdr_unsigned(int, *(tl + 2));
2552 if (len <= 0 || len > NFS_MAXNAMLEN) {
2557 tlen = nfsm_rndup(len);
2559 tlen += 4; /* To ensure null termination*/
2560 left = DIRBLKSIZ - blksiz;
2561 if ((tlen + DIRHDSIZ) > left) {
2562 dp->d_reclen += left;
2563 uiop->uio_iov->iov_base =
2564 (char *)uiop->uio_iov->iov_base + left;
2565 uiop->uio_iov->iov_len -= left;
2566 uiop->uio_offset += left;
2567 uiop->uio_resid -= left;
2570 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2573 dp = (struct dirent *)uiop->uio_iov->iov_base;
2574 dp->d_fileno = (int)fileno;
2576 dp->d_reclen = tlen + DIRHDSIZ;
2577 dp->d_type = DT_UNKNOWN;
2578 blksiz += dp->d_reclen;
2579 if (blksiz == DIRBLKSIZ)
2581 uiop->uio_offset += DIRHDSIZ;
2582 uiop->uio_resid -= DIRHDSIZ;
2583 uiop->uio_iov->iov_base =
2584 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
2585 uiop->uio_iov->iov_len -= DIRHDSIZ;
2586 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2587 cnp->cn_namelen = len;
2588 nfsm_mtouio(uiop, len);
2589 cp = uiop->uio_iov->iov_base;
2592 uiop->uio_iov->iov_base =
2593 (char *)uiop->uio_iov->iov_base + tlen;
2594 uiop->uio_iov->iov_len -= tlen;
2595 uiop->uio_offset += tlen;
2596 uiop->uio_resid -= tlen;
2598 nfsm_adv(nfsm_rndup(len));
2599 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2601 cookie.nfsuquad[0] = *tl++;
2602 cookie.nfsuquad[1] = *tl++;
2607 * Since the attributes are before the file handle
2608 * (sigh), we must skip over the attributes and then
2609 * come back and get them.
2611 attrflag = fxdr_unsigned(int, *tl);
2615 nfsm_adv(NFSX_V3FATTR);
2616 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2617 doit = fxdr_unsigned(int, *tl);
2619 * Skip loading the attrs for "..". There's a
2620 * race between loading the attrs here and
2621 * lookups that look for the directory currently
2622 * being read (in the parent). We try to acquire
2623 * the exclusive lock on ".." here, owning the
2624 * lock on the directory being read. Lookup will
2625 * hold the lock on ".." and try to acquire the
2626 * lock on the directory being read.
2628 * There are other ways of fixing this, one would
2629 * be to do a trylock on the ".." vnode and skip
2630 * loading the attrs on ".." if it happens to be
2631 * locked by another process. But skipping the
2632 * attrload on ".." seems the easiest option.
2634 if (strcmp(dp->d_name, "..") == 0) {
2637 * We've already skipped over the attrs,
2638 * skip over the filehandle. And store d_type
2641 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2642 i = fxdr_unsigned(int, *tl);
2643 nfsm_adv(nfsm_rndup(i));
2644 dp->d_type = IFTODT(VTTOIF(VDIR));
2647 nfsm_getfh(fhp, fhsize, 1);
2648 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2653 error = nfs_nget(vp->v_mount, fhp,
2654 fhsize, &np, LK_EXCLUSIVE);
2661 if (doit && bigenough) {
2666 nfsm_loadattr(newvp, &vattr);
2669 dp->d_type = IFTODT(VTTOIF(vattr.va_type));
2671 if (newvp->v_type != VDIR || dattrflag != 0)
2672 cache_enter_time(ndp->ni_dvp, ndp->ni_vp,
2673 cnp, &vattr.va_ctime,
2674 newvp->v_type != VDIR ? NULL :
2678 /* Just skip over the file handle */
2679 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2680 i = fxdr_unsigned(int, *tl);
2682 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2683 fhsize = fxdr_unsigned(int, *tl);
2684 nfsm_adv(nfsm_rndup(fhsize));
2687 if (newvp != NULLVP) {
2694 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2695 more_dirs = fxdr_unsigned(int, *tl);
2698 * If at end of rpc data, get the eof boolean
2701 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2702 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2707 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2708 * by increasing d_reclen for the last record.
2711 left = DIRBLKSIZ - blksiz;
2712 dp->d_reclen += left;
2713 uiop->uio_iov->iov_base =
2714 (char *)uiop->uio_iov->iov_base + left;
2715 uiop->uio_iov->iov_len -= left;
2716 uiop->uio_offset += left;
2717 uiop->uio_resid -= left;
2721 * We are now either at the end of the directory or have filled the
2725 dnp->n_direofoffset = uiop->uio_offset;
2727 if (uiop->uio_resid > 0)
2728 nfs_printf("EEK! readdirplusrpc resid > 0\n");
2729 nfs_dircookie_lock(dnp);
2730 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2732 nfs_dircookie_unlock(dnp);
2735 if (newvp != NULLVP) {
2746 * Silly rename. To make the NFS filesystem that is stateless look a little
2747 * more like the "ufs" a remove of an active vnode is translated to a rename
2748 * to a funny looking filename that is removed by nfs_inactive on the
2749 * nfsnode. There is the potential for another process on a different client
2750 * to create the same funny name between the nfs_lookitup() fails and the
2751 * nfs_rename() completes, but...
2754 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2756 struct sillyrename *sp;
2760 unsigned int lticks;
2764 KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2765 sp = malloc(sizeof (struct sillyrename),
2766 M_NFSREQ, M_WAITOK);
2767 sp->s_cred = crhold(cnp->cn_cred);
2769 sp->s_removeit = nfs_removeit;
2773 * Fudge together a funny name.
2774 * Changing the format of the funny name to accomodate more
2775 * sillynames per directory.
2776 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2777 * CPU ticks since boot.
2779 pid = cnp->cn_thread->td_proc->p_pid;
2780 lticks = (unsigned int)ticks;
2782 sp->s_namlen = sprintf(sp->s_name,
2783 ".nfs.%08x.%04x4.4", lticks,
2785 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2786 cnp->cn_thread, NULL))
2790 error = nfs_renameit(dvp, cnp, sp);
2793 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2794 cnp->cn_thread, &np);
2795 np->n_sillyrename = sp;
2800 free((caddr_t)sp, M_NFSREQ);
2805 * Look up a file name and optionally either update the file handle or
2806 * allocate an nfsnode, depending on the value of npp.
2807 * npp == NULL --> just do the lookup
2808 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2810 * *npp != NULL --> update the file handle in the vnode
2813 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2814 struct thread *td, struct nfsnode **npp)
2816 struct vnode *newvp = NULL;
2817 struct nfsnode *np, *dnp = VTONFS(dvp);
2819 int error = 0, fhlen, attrflag;
2820 struct mbuf *mreq, *mrep, *md, *mb;
2822 int v3 = NFS_ISV3(dvp);
2824 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2825 mreq = m_get2(NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len),
2826 M_WAITOK, MT_DATA, 0);
2828 bpos = mtod(mb, caddr_t);
2829 nfsm_fhtom(dvp, v3);
2830 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2831 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2832 if (npp && !error) {
2833 nfsm_getfh(nfhp, fhlen, v3);
2836 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2837 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2838 np->n_fhp = &np->n_fh;
2839 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2840 np->n_fhp =(nfsfh_t *)malloc(fhlen, M_NFSBIGFH, M_WAITOK);
2841 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2842 np->n_fhsize = fhlen;
2844 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2848 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, LK_EXCLUSIVE);
2856 nfsm_postop_attr(newvp, attrflag);
2857 if (!attrflag && *npp == NULL) {
2866 nfsm_loadattr(newvp, NULL);
2870 if (npp && *npp == NULL) {
2885 * Nfs Version 3 commit rpc
2888 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2892 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2894 int error = 0, wccflag = NFSV3_WCCRATTR;
2895 struct mbuf *mreq, *mrep, *md, *mb;
2897 mtx_lock(&nmp->nm_mtx);
2898 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2899 mtx_unlock(&nmp->nm_mtx);
2902 mtx_unlock(&nmp->nm_mtx);
2903 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2904 mreq = m_get2(NFSX_FH(1), M_WAITOK, MT_DATA, 0);
2906 bpos = mtod(mb, caddr_t);
2908 tl = nfsm_build(u_int32_t *, 3 * NFSX_UNSIGNED);
2909 txdr_hyper(offset, tl);
2911 *tl = txdr_unsigned(cnt);
2912 nfsm_request(vp, NFSPROC_COMMIT, td, cred);
2913 nfsm_wcc_data(vp, wccflag);
2915 tl = nfsm_dissect(u_int32_t *, NFSX_V3WRITEVERF);
2916 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2917 NFSX_V3WRITEVERF)) {
2918 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2920 error = NFSERR_STALEWRITEVERF;
2930 * For async requests when nfsiod(s) are running, queue the request by
2931 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2935 nfs_strategy(struct vop_strategy_args *ap)
2937 struct buf *bp = ap->a_bp;
2940 KASSERT(!(bp->b_flags & B_DONE),
2941 ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2942 BUF_ASSERT_HELD(bp);
2944 if (bp->b_iocmd == BIO_READ)
2950 * If the op is asynchronous and an i/o daemon is waiting
2951 * queue the request, wake it up and wait for completion
2952 * otherwise just do it ourselves.
2954 if ((bp->b_flags & B_ASYNC) == 0 ||
2955 nfs_asyncio(VFSTONFS(ap->a_vp->v_mount), bp, NOCRED, curthread))
2956 (void)nfs_doio(ap->a_vp, bp, cr, curthread);
2961 * fsync vnode op. Just call nfs_flush() with commit == 1.
2965 nfs_fsync(struct vop_fsync_args *ap)
2968 return (nfs_flush(ap->a_vp, ap->a_waitfor, 1));
2972 * Flush all the blocks associated with a vnode.
2973 * Walk through the buffer pool and push any dirty pages
2974 * associated with the vnode.
2977 nfs_flush(struct vnode *vp, int waitfor, int commit)
2979 struct nfsnode *np = VTONFS(vp);
2983 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2984 int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2986 u_quad_t off, endoff, toff;
2987 struct ucred* wcred = NULL;
2988 struct buf **bvec = NULL;
2990 struct thread *td = curthread;
2991 #ifndef NFS_COMMITBVECSIZ
2992 #define NFS_COMMITBVECSIZ 20
2994 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2995 int bvecsize = 0, bveccount;
2997 if (nmp->nm_flag & NFSMNT_INT)
3003 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
3004 * server, but has not been committed to stable storage on the server
3005 * yet. On the first pass, the byte range is worked out and the commit
3006 * rpc is done. On the second pass, nfs_writebp() is called to do the
3013 if (NFS_ISV3(vp) && commit) {
3014 if (bvec != NULL && bvec != bvec_on_stack)
3017 * Count up how many buffers waiting for a commit.
3021 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3022 if (!BUF_ISLOCKED(bp) &&
3023 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
3024 == (B_DELWRI | B_NEEDCOMMIT))
3028 * Allocate space to remember the list of bufs to commit. It is
3029 * important to use M_NOWAIT here to avoid a race with nfs_write.
3030 * If we can't get memory (for whatever reason), we will end up
3031 * committing the buffers one-by-one in the loop below.
3033 if (bveccount > NFS_COMMITBVECSIZ) {
3035 * Release the vnode interlock to avoid a lock
3039 bvec = (struct buf **)
3040 malloc(bveccount * sizeof(struct buf *),
3044 bvec = bvec_on_stack;
3045 bvecsize = NFS_COMMITBVECSIZ;
3047 bvecsize = bveccount;
3049 bvec = bvec_on_stack;
3050 bvecsize = NFS_COMMITBVECSIZ;
3052 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3053 if (bvecpos >= bvecsize)
3055 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3056 nbp = TAILQ_NEXT(bp, b_bobufs);
3059 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3060 (B_DELWRI | B_NEEDCOMMIT)) {
3062 nbp = TAILQ_NEXT(bp, b_bobufs);
3068 * Work out if all buffers are using the same cred
3069 * so we can deal with them all with one commit.
3071 * NOTE: we are not clearing B_DONE here, so we have
3072 * to do it later on in this routine if we intend to
3073 * initiate I/O on the bp.
3075 * Note: to avoid loopback deadlocks, we do not
3076 * assign b_runningbufspace.
3079 wcred = bp->b_wcred;
3080 else if (wcred != bp->b_wcred)
3082 vfs_busy_pages(bp, 1);
3086 * bp is protected by being locked, but nbp is not
3087 * and vfs_busy_pages() may sleep. We have to
3090 nbp = TAILQ_NEXT(bp, b_bobufs);
3093 * A list of these buffers is kept so that the
3094 * second loop knows which buffers have actually
3095 * been committed. This is necessary, since there
3096 * may be a race between the commit rpc and new
3097 * uncommitted writes on the file.
3099 bvec[bvecpos++] = bp;
3100 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3104 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
3112 * Commit data on the server, as required.
3113 * If all bufs are using the same wcred, then use that with
3114 * one call for all of them, otherwise commit each one
3117 if (wcred != NOCRED)
3118 retv = nfs_commit(vp, off, (int)(endoff - off),
3122 for (i = 0; i < bvecpos; i++) {
3125 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3127 size = (u_quad_t)(bp->b_dirtyend
3129 retv = nfs_commit(vp, off, (int)size,
3135 if (retv == NFSERR_STALEWRITEVERF)
3136 nfs_clearcommit(vp->v_mount);
3139 * Now, either mark the blocks I/O done or mark the
3140 * blocks dirty, depending on whether the commit
3143 for (i = 0; i < bvecpos; i++) {
3145 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3148 * Error, leave B_DELWRI intact
3150 vfs_unbusy_pages(bp);
3154 * Success, remove B_DELWRI ( bundirty() ).
3156 * b_dirtyoff/b_dirtyend seem to be NFS
3157 * specific. We should probably move that
3158 * into bundirty(). XXX
3161 bp->b_flags |= B_ASYNC;
3163 bp->b_flags &= ~B_DONE;
3164 bp->b_ioflags &= ~BIO_ERROR;
3165 bp->b_dirtyoff = bp->b_dirtyend = 0;
3172 * Start/do any write(s) that are required.
3176 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3177 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3178 if (waitfor != MNT_WAIT || passone)
3181 error = BUF_TIMELOCK(bp,
3182 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
3183 BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
3188 if (error == ENOLCK) {
3192 if (nfs_sigintr(nmp, td)) {
3196 if (slpflag == PCATCH) {
3202 if ((bp->b_flags & B_DELWRI) == 0)
3203 panic("nfs_fsync: not dirty");
3204 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3210 if (passone || !commit)
3211 bp->b_flags |= B_ASYNC;
3213 bp->b_flags |= B_ASYNC;
3215 if (nfs_sigintr(nmp, td)) {
3226 if (waitfor == MNT_WAIT) {
3227 while (bo->bo_numoutput) {
3228 error = bufobj_wwait(bo, slpflag, slptimeo);
3231 error = nfs_sigintr(nmp, td);
3234 if (slpflag == PCATCH) {
3241 if (bo->bo_dirty.bv_cnt != 0 && commit) {
3246 * Wait for all the async IO requests to drain
3249 mtx_lock(&np->n_mtx);
3250 while (np->n_directio_asyncwr > 0) {
3251 np->n_flag |= NFSYNCWAIT;
3252 error = nfs_msleep(td, (caddr_t)&np->n_directio_asyncwr,
3253 &np->n_mtx, slpflag | (PRIBIO + 1),
3256 if (nfs_sigintr(nmp, td)) {
3257 mtx_unlock(&np->n_mtx);
3263 mtx_unlock(&np->n_mtx);
3266 mtx_lock(&np->n_mtx);
3267 if (np->n_flag & NWRITEERR) {
3268 error = np->n_error;
3269 np->n_flag &= ~NWRITEERR;
3271 if (commit && bo->bo_dirty.bv_cnt == 0 &&
3272 bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
3273 np->n_flag &= ~NMODIFIED;
3274 mtx_unlock(&np->n_mtx);
3276 if (bvec != NULL && bvec != bvec_on_stack)
3282 * NFS advisory byte-level locks.
3285 nfs_advlock(struct vop_advlock_args *ap)
3287 struct vnode *vp = ap->a_vp;
3291 error = vn_lock(vp, LK_SHARED);
3294 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3295 size = VTONFS(vp)->n_size;
3297 error = lf_advlock(ap, &(vp->v_lockf), size);
3300 error = nfs_advlock_p(ap);
3309 * NFS advisory byte-level locks.
3312 nfs_advlockasync(struct vop_advlockasync_args *ap)
3314 struct vnode *vp = ap->a_vp;
3318 error = vn_lock(vp, LK_SHARED);
3321 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3322 size = VTONFS(vp)->n_size;
3324 error = lf_advlockasync(ap, &(vp->v_lockf), size);
3333 * Print out the contents of an nfsnode.
3336 nfs_print(struct vop_print_args *ap)
3338 struct vnode *vp = ap->a_vp;
3339 struct nfsnode *np = VTONFS(vp);
3341 nfs_printf("\tfileid %ld fsid 0x%x",
3342 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3343 if (vp->v_type == VFIFO)
3350 * This is the "real" nfs::bwrite(struct buf*).
3351 * We set B_CACHE if this is a VMIO buffer.
3354 nfs_writebp(struct buf *bp, int force __unused, struct thread *td)
3357 int oldflags = bp->b_flags;
3363 BUF_ASSERT_HELD(bp);
3365 if (bp->b_flags & B_INVAL) {
3370 bp->b_flags |= B_CACHE;
3373 * Undirty the bp. We will redirty it later if the I/O fails.
3378 bp->b_flags &= ~B_DONE;
3379 bp->b_ioflags &= ~BIO_ERROR;
3380 bp->b_iocmd = BIO_WRITE;
3382 bufobj_wref(bp->b_bufobj);
3383 curthread->td_ru.ru_oublock++;
3387 * Note: to avoid loopback deadlocks, we do not
3388 * assign b_runningbufspace.
3390 vfs_busy_pages(bp, 1);
3393 bp->b_iooffset = dbtob(bp->b_blkno);
3396 if( (oldflags & B_ASYNC) == 0) {
3397 int rtval = bufwait(bp);
3399 if (oldflags & B_DELWRI) {
3412 * nfs special file access vnode op.
3413 * Essentially just get vattr and then imitate iaccess() since the device is
3414 * local to the client.
3417 nfsspec_access(struct vop_access_args *ap)
3420 struct ucred *cred = ap->a_cred;
3421 struct vnode *vp = ap->a_vp;
3422 accmode_t accmode = ap->a_accmode;
3427 * Disallow write attempts on filesystems mounted read-only;
3428 * unless the file is a socket, fifo, or a block or character
3429 * device resident on the filesystem.
3431 if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3432 switch (vp->v_type) {
3442 error = VOP_GETATTR(vp, vap, cred);
3445 error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3446 accmode, cred, NULL);
3452 * Read wrapper for fifos.
3455 nfsfifo_read(struct vop_read_args *ap)
3457 struct nfsnode *np = VTONFS(ap->a_vp);
3463 mtx_lock(&np->n_mtx);
3465 vfs_timestamp(&np->n_atim);
3466 mtx_unlock(&np->n_mtx);
3467 error = fifo_specops.vop_read(ap);
3472 * Write wrapper for fifos.
3475 nfsfifo_write(struct vop_write_args *ap)
3477 struct nfsnode *np = VTONFS(ap->a_vp);
3482 mtx_lock(&np->n_mtx);
3484 vfs_timestamp(&np->n_mtim);
3485 mtx_unlock(&np->n_mtx);
3486 return(fifo_specops.vop_write(ap));
3490 * Close wrapper for fifos.
3492 * Update the times on the nfsnode then do fifo close.
3495 nfsfifo_close(struct vop_close_args *ap)
3497 struct vnode *vp = ap->a_vp;
3498 struct nfsnode *np = VTONFS(vp);
3502 mtx_lock(&np->n_mtx);
3503 if (np->n_flag & (NACC | NUPD)) {
3505 if (np->n_flag & NACC)
3507 if (np->n_flag & NUPD)
3510 if (vrefcnt(vp) == 1 &&
3511 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3513 if (np->n_flag & NACC)
3514 vattr.va_atime = np->n_atim;
3515 if (np->n_flag & NUPD)
3516 vattr.va_mtime = np->n_mtim;
3517 mtx_unlock(&np->n_mtx);
3518 (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3522 mtx_unlock(&np->n_mtx);
3524 return (fifo_specops.vop_close(ap));
3528 * Just call nfs_writebp() with the force argument set to 1.
3530 * NOTE: B_DONE may or may not be set in a_bp on call.
3533 nfs_bwrite(struct buf *bp)
3536 return (nfs_writebp(bp, 1, curthread));
3539 struct buf_ops buf_ops_nfs = {
3540 .bop_name = "buf_ops_nfs",
3541 .bop_write = nfs_bwrite,
3542 .bop_strategy = bufstrategy,
3543 .bop_sync = bufsync,
3544 .bop_bdflush = bufbdflush,