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;
132 static vop_advlockasync_t nfs_advlockasync;
135 * Global vfs data structures for nfs
137 struct vop_vector nfs_vnodeops = {
138 .vop_default = &default_vnodeops,
139 .vop_access = nfs_access,
140 .vop_advlock = nfs_advlock,
141 .vop_advlockasync = nfs_advlockasync,
142 .vop_close = nfs_close,
143 .vop_create = nfs_create,
144 .vop_fsync = nfs_fsync,
145 .vop_getattr = nfs_getattr,
146 .vop_getpages = nfs_getpages,
147 .vop_putpages = nfs_putpages,
148 .vop_inactive = nfs_inactive,
149 .vop_lease = VOP_NULL,
150 .vop_link = nfs_link,
151 .vop_lookup = nfs_lookup,
152 .vop_mkdir = nfs_mkdir,
153 .vop_mknod = nfs_mknod,
154 .vop_open = nfs_open,
155 .vop_print = nfs_print,
156 .vop_read = nfs_read,
157 .vop_readdir = nfs_readdir,
158 .vop_readlink = nfs_readlink,
159 .vop_reclaim = nfs_reclaim,
160 .vop_remove = nfs_remove,
161 .vop_rename = nfs_rename,
162 .vop_rmdir = nfs_rmdir,
163 .vop_setattr = nfs_setattr,
164 .vop_strategy = nfs_strategy,
165 .vop_symlink = nfs_symlink,
166 .vop_write = nfs_write,
169 struct vop_vector nfs_fifoops = {
170 .vop_default = &fifo_specops,
171 .vop_access = nfsspec_access,
172 .vop_close = nfsfifo_close,
173 .vop_fsync = nfs_fsync,
174 .vop_getattr = nfs_getattr,
175 .vop_inactive = nfs_inactive,
176 .vop_print = nfs_print,
177 .vop_read = nfsfifo_read,
178 .vop_reclaim = nfs_reclaim,
179 .vop_setattr = nfs_setattr,
180 .vop_write = nfsfifo_write,
183 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
184 struct componentname *cnp, struct vattr *vap);
185 static int nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
186 struct ucred *cred, struct thread *td);
187 static int nfs_renamerpc(struct vnode *fdvp, const char *fnameptr,
188 int fnamelen, struct vnode *tdvp,
189 const char *tnameptr, int tnamelen,
190 struct ucred *cred, struct thread *td);
191 static int nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
192 struct sillyrename *sp);
197 struct mtx nfs_iod_mtx;
198 struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
199 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
200 int nfs_numasync = 0;
201 vop_advlock_t *nfs_advlock_p = nfs_dolock;
202 vop_reclaim_t *nfs_reclaim_p = NULL;
203 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
205 SYSCTL_DECL(_vfs_nfs);
207 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
208 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
209 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
211 static int nfs_prime_access_cache = 1;
212 SYSCTL_INT(_vfs_nfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
213 &nfs_prime_access_cache, 0,
214 "Prime NFS ACCESS cache when fetching attributes");
216 static int nfsv3_commit_on_close = 0;
217 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
218 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
220 static int nfs_clean_pages_on_close = 1;
221 SYSCTL_INT(_vfs_nfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
222 &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
224 int nfs_directio_enable = 0;
225 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
226 &nfs_directio_enable, 0, "Enable NFS directio");
229 * This sysctl allows other processes to mmap a file that has been opened
230 * O_DIRECT by a process. In general, having processes mmap the file while
231 * Direct IO is in progress can lead to Data Inconsistencies. But, we allow
232 * this by default to prevent DoS attacks - to prevent a malicious user from
233 * opening up files O_DIRECT preventing other users from mmap'ing these
234 * files. "Protected" environments where stricter consistency guarantees are
235 * required can disable this knob. The process that opened the file O_DIRECT
236 * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
239 int nfs_directio_allow_mmap = 1;
240 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
241 &nfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
244 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
245 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
247 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
248 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
251 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
252 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
253 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
257 * The list of locks after the description of the lock is the ordering
258 * of other locks acquired with the lock held.
259 * np->n_mtx : Protects the fields in the nfsnode.
261 VI_MTX (acquired indirectly)
262 * nmp->nm_mtx : Protects the fields in the nfsmount.
264 * nfs_iod_mtx : Global lock, protects shared nfsiod state.
265 * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
268 * rep->r_mtx : Protects the fields in an nfsreq.
272 nfs3_access_otw(struct vnode *vp, int wmode, struct thread *td,
277 int error = 0, attrflag;
279 struct mbuf *mreq, *mrep, *md, *mb;
282 struct nfsnode *np = VTONFS(vp);
284 nfsstats.rpccnt[NFSPROC_ACCESS]++;
285 mreq = nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
287 bpos = mtod(mb, caddr_t);
289 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
290 *tl = txdr_unsigned(wmode);
291 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
292 nfsm_postop_attr(vp, attrflag);
294 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
295 rmode = fxdr_unsigned(u_int32_t, *tl);
296 mtx_lock(&np->n_mtx);
298 np->n_modeuid = cred->cr_uid;
299 np->n_modestamp = time_second;
300 mtx_unlock(&np->n_mtx);
308 * nfs access vnode op.
309 * For nfs version 2, just return ok. File accesses may fail later.
310 * For nfs version 3, use the access rpc to check accessibility. If file modes
311 * are changed on the server, accesses might still fail later.
314 nfs_access(struct vop_access_args *ap)
316 struct vnode *vp = ap->a_vp;
318 u_int32_t mode, wmode;
319 int v3 = NFS_ISV3(vp);
320 struct nfsnode *np = VTONFS(vp);
323 * Disallow write attempts on filesystems mounted read-only;
324 * unless the file is a socket, fifo, or a block or character
325 * device resident on the filesystem.
327 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
328 switch (vp->v_type) {
338 * For nfs v3, check to see if we have done this recently, and if
339 * so return our cached result instead of making an ACCESS call.
340 * If not, do an access rpc, otherwise you are stuck emulating
341 * ufs_access() locally using the vattr. This may not be correct,
342 * since the server may apply other access criteria such as
343 * client uid-->server uid mapping that we do not know about.
346 if (ap->a_mode & VREAD)
347 mode = NFSV3ACCESS_READ;
350 if (vp->v_type != VDIR) {
351 if (ap->a_mode & VWRITE)
352 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
353 if (ap->a_mode & VEXEC)
354 mode |= NFSV3ACCESS_EXECUTE;
356 if (ap->a_mode & VWRITE)
357 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
359 if (ap->a_mode & VEXEC)
360 mode |= NFSV3ACCESS_LOOKUP;
362 /* XXX safety belt, only make blanket request if caching */
363 if (nfsaccess_cache_timeout > 0) {
364 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
365 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
366 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
372 * Does our cached result allow us to give a definite yes to
375 mtx_lock(&np->n_mtx);
376 if ((time_second < (np->n_modestamp + nfsaccess_cache_timeout)) &&
377 (ap->a_cred->cr_uid == np->n_modeuid) &&
378 ((np->n_mode & mode) == mode)) {
379 nfsstats.accesscache_hits++;
382 * Either a no, or a don't know. Go to the wire.
384 nfsstats.accesscache_misses++;
385 mtx_unlock(&np->n_mtx);
386 error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred);
387 mtx_lock(&np->n_mtx);
389 if ((np->n_mode & mode) != mode) {
394 mtx_unlock(&np->n_mtx);
397 if ((error = nfsspec_access(ap)) != 0) {
401 * Attempt to prevent a mapped root from accessing a file
402 * which it shouldn't. We try to read a byte from the file
403 * if the user is root and the file is not zero length.
404 * After calling nfsspec_access, we should have the correct
407 mtx_lock(&np->n_mtx);
408 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
409 && VTONFS(vp)->n_size > 0) {
414 mtx_unlock(&np->n_mtx);
417 auio.uio_iov = &aiov;
421 auio.uio_segflg = UIO_SYSSPACE;
422 auio.uio_rw = UIO_READ;
423 auio.uio_td = ap->a_td;
425 if (vp->v_type == VREG)
426 error = nfs_readrpc(vp, &auio, ap->a_cred);
427 else if (vp->v_type == VDIR) {
429 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
431 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
432 error = nfs_readdirrpc(vp, &auio, ap->a_cred);
434 } else if (vp->v_type == VLNK)
435 error = nfs_readlinkrpc(vp, &auio, ap->a_cred);
439 mtx_unlock(&np->n_mtx);
444 int nfs_otw_getattr_avoid = 0;
448 * Check to see if the type is ok
449 * and that deletion is not in progress.
450 * For paged in text files, you will need to flush the page cache
451 * if consistency is lost.
455 nfs_open(struct vop_open_args *ap)
457 struct vnode *vp = ap->a_vp;
458 struct nfsnode *np = VTONFS(vp);
461 int fmode = ap->a_mode;
463 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
467 * Get a valid lease. If cached data is stale, flush it.
469 mtx_lock(&np->n_mtx);
470 if (np->n_flag & NMODIFIED) {
471 mtx_unlock(&np->n_mtx);
472 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
473 if (error == EINTR || error == EIO)
476 if (vp->v_type == VDIR)
477 np->n_direofoffset = 0;
478 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
481 mtx_lock(&np->n_mtx);
482 np->n_mtime = vattr.va_mtime;
483 mtx_unlock(&np->n_mtx);
485 struct thread *td = curthread;
487 if (np->n_ac_ts_syscalls != td->td_syscalls ||
488 np->n_ac_ts_tid != td->td_tid ||
489 td->td_proc == NULL ||
490 np->n_ac_ts_pid != td->td_proc->p_pid) {
493 mtx_unlock(&np->n_mtx);
494 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
497 mtx_lock(&np->n_mtx);
498 if (NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
499 if (vp->v_type == VDIR)
500 np->n_direofoffset = 0;
501 mtx_unlock(&np->n_mtx);
502 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
503 if (error == EINTR || error == EIO) {
506 mtx_lock(&np->n_mtx);
507 np->n_mtime = vattr.va_mtime;
509 mtx_unlock(&np->n_mtx);
512 * If the object has >= 1 O_DIRECT active opens, we disable caching.
514 if (nfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
515 if (np->n_directio_opens == 0) {
516 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
519 mtx_lock(&np->n_mtx);
520 np->n_flag |= NNONCACHE;
521 mtx_unlock(&np->n_mtx);
523 np->n_directio_opens++;
525 vnode_create_vobject(vp, vattr.va_size, ap->a_td);
531 * What an NFS client should do upon close after writing is a debatable issue.
532 * Most NFS clients push delayed writes to the server upon close, basically for
534 * 1 - So that any write errors may be reported back to the client process
535 * doing the close system call. By far the two most likely errors are
536 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
537 * 2 - To put a worst case upper bound on cache inconsistency between
538 * multiple clients for the file.
539 * There is also a consistency problem for Version 2 of the protocol w.r.t.
540 * not being able to tell if other clients are writing a file concurrently,
541 * since there is no way of knowing if the changed modify time in the reply
542 * is only due to the write for this client.
543 * (NFS Version 3 provides weak cache consistency data in the reply that
544 * should be sufficient to detect and handle this case.)
546 * The current code does the following:
547 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
548 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
549 * or commit them (this satisfies 1 and 2 except for the
550 * case where the server crashes after this close but
551 * before the commit RPC, which is felt to be "good
552 * enough". Changing the last argument to nfs_flush() to
553 * a 1 would force a commit operation, if it is felt a
554 * commit is necessary now.
558 nfs_close(struct vop_close_args *ap)
560 struct vnode *vp = ap->a_vp;
561 struct nfsnode *np = VTONFS(vp);
563 int fmode = ap->a_fflag;
565 if (vp->v_type == VREG) {
567 * Examine and clean dirty pages, regardless of NMODIFIED.
568 * This closes a major hole in close-to-open consistency.
569 * We want to push out all dirty pages (and buffers) on
570 * close, regardless of whether they were dirtied by
571 * mmap'ed writes or via write().
573 if (nfs_clean_pages_on_close && vp->v_object) {
574 VM_OBJECT_LOCK(vp->v_object);
575 vm_object_page_clean(vp->v_object, 0, 0, 0);
576 VM_OBJECT_UNLOCK(vp->v_object);
578 mtx_lock(&np->n_mtx);
579 if (np->n_flag & NMODIFIED) {
580 mtx_unlock(&np->n_mtx);
583 * Under NFSv3 we have dirty buffers to dispose of. We
584 * must flush them to the NFS server. We have the option
585 * of waiting all the way through the commit rpc or just
586 * waiting for the initial write. The default is to only
587 * wait through the initial write so the data is in the
588 * server's cache, which is roughly similar to the state
589 * a standard disk subsystem leaves the file in on close().
591 * We cannot clear the NMODIFIED bit in np->n_flag due to
592 * potential races with other processes, and certainly
593 * cannot clear it if we don't commit.
595 int cm = nfsv3_commit_on_close ? 1 : 0;
596 error = nfs_flush(vp, MNT_WAIT, ap->a_td, cm);
597 /* np->n_flag &= ~NMODIFIED; */
599 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
600 mtx_lock(&np->n_mtx);
602 if (np->n_flag & NWRITEERR) {
603 np->n_flag &= ~NWRITEERR;
606 mtx_unlock(&np->n_mtx);
608 if (nfs_directio_enable)
609 KASSERT((np->n_directio_asyncwr == 0),
610 ("nfs_close: dirty unflushed (%d) directio buffers\n",
611 np->n_directio_asyncwr));
612 if (nfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
613 mtx_lock(&np->n_mtx);
614 KASSERT((np->n_directio_opens > 0),
615 ("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
616 np->n_directio_opens--;
617 if (np->n_directio_opens == 0)
618 np->n_flag &= ~NNONCACHE;
619 mtx_unlock(&np->n_mtx);
625 * nfs getattr call from vfs.
628 nfs_getattr(struct vop_getattr_args *ap)
630 struct vnode *vp = ap->a_vp;
631 struct nfsnode *np = VTONFS(vp);
632 struct vattr *vap = ap->a_vap;
636 struct mbuf *mreq, *mrep, *md, *mb;
637 int v3 = NFS_ISV3(vp);
640 * Update local times for special files.
642 mtx_lock(&np->n_mtx);
643 if (np->n_flag & (NACC | NUPD))
645 mtx_unlock(&np->n_mtx);
647 * First look in the cache.
649 if (nfs_getattrcache(vp, &vattr) == 0)
651 if (v3 && nfs_prime_access_cache && nfsaccess_cache_timeout > 0) {
652 nfsstats.accesscache_misses++;
653 nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_td, ap->a_cred);
654 if (nfs_getattrcache(vp, &vattr) == 0)
657 nfsstats.rpccnt[NFSPROC_GETATTR]++;
658 mreq = nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
660 bpos = mtod(mb, caddr_t);
662 nfsm_request(vp, NFSPROC_GETATTR, ap->a_td, ap->a_cred);
664 nfsm_loadattr(vp, &vattr);
668 vap->va_type = vattr.va_type;
669 vap->va_mode = vattr.va_mode;
670 vap->va_nlink = vattr.va_nlink;
671 vap->va_uid = vattr.va_uid;
672 vap->va_gid = vattr.va_gid;
673 vap->va_fsid = vattr.va_fsid;
674 vap->va_fileid = vattr.va_fileid;
675 vap->va_size = vattr.va_size;
676 vap->va_blocksize = vattr.va_blocksize;
677 vap->va_atime = vattr.va_atime;
678 vap->va_mtime = vattr.va_mtime;
679 vap->va_ctime = vattr.va_ctime;
680 vap->va_gen = vattr.va_gen;
681 vap->va_flags = vattr.va_flags;
682 vap->va_rdev = vattr.va_rdev;
683 vap->va_bytes = vattr.va_bytes;
684 vap->va_filerev = vattr.va_filerev;
693 nfs_setattr(struct vop_setattr_args *ap)
695 struct vnode *vp = ap->a_vp;
696 struct nfsnode *np = VTONFS(vp);
697 struct vattr *vap = ap->a_vap;
706 * Setting of flags and marking of atimes are not supported.
708 if (vap->va_flags != VNOVAL || (vap->va_vaflags & VA_MARK_ATIME))
712 * Disallow write attempts if the filesystem is mounted read-only.
714 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
715 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
716 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
717 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
721 if (vap->va_size != VNOVAL) {
722 switch (vp->v_type) {
729 if (vap->va_mtime.tv_sec == VNOVAL &&
730 vap->va_atime.tv_sec == VNOVAL &&
731 vap->va_mode == (mode_t)VNOVAL &&
732 vap->va_uid == (uid_t)VNOVAL &&
733 vap->va_gid == (gid_t)VNOVAL)
735 vap->va_size = VNOVAL;
739 * Disallow write attempts if the filesystem is
742 if (vp->v_mount->mnt_flag & MNT_RDONLY)
745 * We run vnode_pager_setsize() early (why?),
746 * we must set np->n_size now to avoid vinvalbuf
747 * V_SAVE races that might setsize a lower
750 mtx_lock(&np->n_mtx);
752 mtx_unlock(&np->n_mtx);
753 error = nfs_meta_setsize(vp, ap->a_cred,
754 ap->a_td, vap->va_size);
755 mtx_lock(&np->n_mtx);
756 if (np->n_flag & NMODIFIED) {
758 mtx_unlock(&np->n_mtx);
759 if (vap->va_size == 0)
760 error = nfs_vinvalbuf(vp, 0, ap->a_td, 1);
762 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
764 vnode_pager_setsize(vp, tsize);
768 mtx_unlock(&np->n_mtx);
770 * np->n_size has already been set to vap->va_size
771 * in nfs_meta_setsize(). We must set it again since
772 * nfs_loadattrcache() could be called through
773 * nfs_meta_setsize() and could modify np->n_size.
775 mtx_lock(&np->n_mtx);
776 np->n_vattr.va_size = np->n_size = vap->va_size;
777 mtx_unlock(&np->n_mtx);
780 mtx_lock(&np->n_mtx);
781 if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
782 (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
783 mtx_unlock(&np->n_mtx);
784 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) != 0 &&
785 (error == EINTR || error == EIO))
788 mtx_unlock(&np->n_mtx);
790 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
791 if (error && vap->va_size != VNOVAL) {
792 mtx_lock(&np->n_mtx);
793 np->n_size = np->n_vattr.va_size = tsize;
794 vnode_pager_setsize(vp, tsize);
795 mtx_unlock(&np->n_mtx);
802 * Do an nfs setattr rpc.
805 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
808 struct nfsv2_sattr *sp;
809 struct nfsnode *np = VTONFS(vp);
812 int error = 0, wccflag = NFSV3_WCCRATTR;
813 struct mbuf *mreq, *mrep, *md, *mb;
814 int v3 = NFS_ISV3(vp);
816 nfsstats.rpccnt[NFSPROC_SETATTR]++;
817 mreq = nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
819 bpos = mtod(mb, caddr_t);
822 nfsm_v3attrbuild(vap, TRUE);
823 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
826 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
827 if (vap->va_mode == (mode_t)VNOVAL)
828 sp->sa_mode = nfs_xdrneg1;
830 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
831 if (vap->va_uid == (uid_t)VNOVAL)
832 sp->sa_uid = nfs_xdrneg1;
834 sp->sa_uid = txdr_unsigned(vap->va_uid);
835 if (vap->va_gid == (gid_t)VNOVAL)
836 sp->sa_gid = nfs_xdrneg1;
838 sp->sa_gid = txdr_unsigned(vap->va_gid);
839 sp->sa_size = txdr_unsigned(vap->va_size);
840 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
841 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
843 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
846 nfsm_wcc_data(vp, wccflag);
848 nfsm_loadattr(vp, NULL);
855 * nfs lookup call, one step at a time...
856 * First look in cache
857 * If not found, unlock the directory nfsnode and do the rpc
860 nfs_lookup(struct vop_lookup_args *ap)
862 struct componentname *cnp = ap->a_cnp;
863 struct vnode *dvp = ap->a_dvp;
864 struct vnode **vpp = ap->a_vpp;
865 int flags = cnp->cn_flags;
867 struct nfsmount *nmp;
869 struct mbuf *mreq, *mrep, *md, *mb;
873 int error = 0, attrflag, fhsize;
874 int v3 = NFS_ISV3(dvp);
875 struct thread *td = cnp->cn_thread;
878 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
879 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
881 if (dvp->v_type != VDIR)
883 nmp = VFSTONFS(dvp->v_mount);
885 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
889 error = cache_lookup(dvp, vpp, cnp);
890 if (error > 0 && error != ENOENT)
896 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, td)
897 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
898 nfsstats.lookupcache_hits++;
899 if (cnp->cn_nameiop != LOOKUP &&
901 cnp->cn_flags |= SAVENAME;
913 nfsstats.lookupcache_misses++;
914 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
915 len = cnp->cn_namelen;
916 mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP,
917 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
919 bpos = mtod(mb, caddr_t);
921 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
922 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_thread, cnp->cn_cred);
925 nfsm_postop_attr(dvp, attrflag);
930 nfsm_getfh(fhp, fhsize, v3);
933 * Handle RENAME case...
935 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
936 if (NFS_CMPFH(np, fhp, fhsize)) {
940 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, LK_EXCLUSIVE);
947 nfsm_postop_attr(newvp, attrflag);
948 nfsm_postop_attr(dvp, attrflag);
950 nfsm_loadattr(newvp, NULL);
953 cnp->cn_flags |= SAVENAME;
957 if (flags & ISDOTDOT) {
958 VOP_UNLOCK(dvp, 0, td);
959 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, cnp->cn_lkflags);
960 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
964 } else if (NFS_CMPFH(np, fhp, fhsize)) {
968 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, cnp->cn_lkflags);
976 nfsm_postop_attr(newvp, attrflag);
977 nfsm_postop_attr(dvp, attrflag);
979 nfsm_loadattr(newvp, NULL);
980 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
981 cnp->cn_flags |= SAVENAME;
982 if ((cnp->cn_flags & MAKEENTRY) &&
983 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
984 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
985 cache_enter(dvp, newvp, cnp);
991 if (newvp != NULLVP) {
995 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
996 (flags & ISLASTCN) && error == ENOENT) {
997 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1000 error = EJUSTRETURN;
1002 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1003 cnp->cn_flags |= SAVENAME;
1010 * Just call nfs_bioread() to do the work.
1013 nfs_read(struct vop_read_args *ap)
1015 struct vnode *vp = ap->a_vp;
1017 switch (vp->v_type) {
1019 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1023 return (EOPNOTSUPP);
1031 nfs_readlink(struct vop_readlink_args *ap)
1033 struct vnode *vp = ap->a_vp;
1035 if (vp->v_type != VLNK)
1037 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred));
1041 * Do a readlink rpc.
1042 * Called by nfs_doio() from below the buffer cache.
1045 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1048 int error = 0, len, attrflag;
1049 struct mbuf *mreq, *mrep, *md, *mb;
1050 int v3 = NFS_ISV3(vp);
1052 nfsstats.rpccnt[NFSPROC_READLINK]++;
1053 mreq = nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1055 bpos = mtod(mb, caddr_t);
1057 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, cred);
1059 nfsm_postop_attr(vp, attrflag);
1061 nfsm_strsiz(len, NFS_MAXPATHLEN);
1062 if (len == NFS_MAXPATHLEN) {
1063 struct nfsnode *np = VTONFS(vp);
1064 mtx_lock(&np->n_mtx);
1065 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1067 mtx_unlock(&np->n_mtx);
1069 nfsm_mtouio(uiop, len);
1081 nfs_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1085 struct mbuf *mreq, *mrep, *md, *mb;
1086 struct nfsmount *nmp;
1087 int error = 0, len, retlen, tsiz, eof, attrflag;
1088 int v3 = NFS_ISV3(vp);
1094 nmp = VFSTONFS(vp->v_mount);
1095 tsiz = uiop->uio_resid;
1096 mtx_lock(&nmp->nm_mtx);
1097 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize) {
1098 mtx_unlock(&nmp->nm_mtx);
1101 rsize = nmp->nm_rsize;
1102 mtx_unlock(&nmp->nm_mtx);
1104 nfsstats.rpccnt[NFSPROC_READ]++;
1105 len = (tsiz > rsize) ? rsize : tsiz;
1106 mreq = nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1108 bpos = mtod(mb, caddr_t);
1110 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED * 3);
1112 txdr_hyper(uiop->uio_offset, tl);
1113 *(tl + 2) = txdr_unsigned(len);
1115 *tl++ = txdr_unsigned(uiop->uio_offset);
1116 *tl++ = txdr_unsigned(len);
1119 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, cred);
1121 nfsm_postop_attr(vp, attrflag);
1126 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
1127 eof = fxdr_unsigned(int, *(tl + 1));
1129 nfsm_loadattr(vp, NULL);
1131 nfsm_strsiz(retlen, rsize);
1132 nfsm_mtouio(uiop, retlen);
1136 if (eof || retlen == 0) {
1139 } else if (retlen < len) {
1151 nfs_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1152 int *iomode, int *must_commit)
1157 struct mbuf *mreq, *mrep, *md, *mb;
1158 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1159 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1160 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1164 if (uiop->uio_iovcnt != 1)
1165 panic("nfs: writerpc iovcnt > 1");
1168 tsiz = uiop->uio_resid;
1169 mtx_lock(&nmp->nm_mtx);
1170 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize) {
1171 mtx_unlock(&nmp->nm_mtx);
1174 wsize = nmp->nm_wsize;
1175 mtx_unlock(&nmp->nm_mtx);
1177 nfsstats.rpccnt[NFSPROC_WRITE]++;
1178 len = (tsiz > wsize) ? wsize : tsiz;
1179 mreq = nfsm_reqhead(vp, NFSPROC_WRITE,
1180 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1182 bpos = mtod(mb, caddr_t);
1185 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
1186 txdr_hyper(uiop->uio_offset, tl);
1188 *tl++ = txdr_unsigned(len);
1189 *tl++ = txdr_unsigned(*iomode);
1190 *tl = txdr_unsigned(len);
1194 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1195 /* Set both "begin" and "current" to non-garbage. */
1196 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1197 *tl++ = x; /* "begin offset" */
1198 *tl++ = x; /* "current offset" */
1199 x = txdr_unsigned(len);
1200 *tl++ = x; /* total to this offset */
1201 *tl = x; /* size of this write */
1203 nfsm_uiotom(uiop, len);
1204 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, cred);
1206 wccflag = NFSV3_WCCCHK;
1207 nfsm_wcc_data(vp, wccflag);
1209 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED
1210 + NFSX_V3WRITEVERF);
1211 rlen = fxdr_unsigned(int, *tl++);
1216 } else if (rlen < len) {
1217 backup = len - rlen;
1218 uiop->uio_iov->iov_base =
1219 (char *)uiop->uio_iov->iov_base -
1221 uiop->uio_iov->iov_len += backup;
1222 uiop->uio_offset -= backup;
1223 uiop->uio_resid += backup;
1226 commit = fxdr_unsigned(int, *tl++);
1229 * Return the lowest committment level
1230 * obtained by any of the RPCs.
1232 if (committed == NFSV3WRITE_FILESYNC)
1234 else if (committed == NFSV3WRITE_DATASYNC &&
1235 commit == NFSV3WRITE_UNSTABLE)
1237 mtx_lock(&nmp->nm_mtx);
1238 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1239 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1241 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1242 } else if (bcmp((caddr_t)tl,
1243 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1245 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1248 mtx_unlock(&nmp->nm_mtx);
1251 nfsm_loadattr(vp, NULL);
1254 mtx_lock(&(VTONFS(vp))->n_mtx);
1255 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime;
1256 mtx_unlock(&(VTONFS(vp))->n_mtx);
1264 if (vp->v_mount->mnt_kern_flag & MNTK_ASYNC)
1265 committed = NFSV3WRITE_FILESYNC;
1266 *iomode = committed;
1268 uiop->uio_resid = tsiz;
1274 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1275 * mode set to specify the file type and the size field for rdev.
1278 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1281 struct nfsv2_sattr *sp;
1283 struct vnode *newvp = NULL;
1284 struct nfsnode *np = NULL;
1287 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1288 struct mbuf *mreq, *mrep, *md, *mb;
1290 int v3 = NFS_ISV3(dvp);
1292 if (vap->va_type == VCHR || vap->va_type == VBLK)
1293 rdev = txdr_unsigned(vap->va_rdev);
1294 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1297 return (EOPNOTSUPP);
1299 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1302 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1303 mreq = nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1304 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1306 bpos = mtod(mb, caddr_t);
1307 nfsm_fhtom(dvp, v3);
1308 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1310 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1311 *tl++ = vtonfsv3_type(vap->va_type);
1312 nfsm_v3attrbuild(vap, FALSE);
1313 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1314 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
1315 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1316 *tl = txdr_unsigned(uminor(vap->va_rdev));
1319 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1320 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1321 sp->sa_uid = nfs_xdrneg1;
1322 sp->sa_gid = nfs_xdrneg1;
1324 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1325 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1327 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_thread, cnp->cn_cred);
1329 nfsm_mtofh(dvp, newvp, v3, gotvp);
1335 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1336 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1342 nfsm_wcc_data(dvp, wccflag);
1349 if (cnp->cn_flags & MAKEENTRY)
1350 cache_enter(dvp, newvp, cnp);
1353 mtx_lock(&(VTONFS(dvp))->n_mtx);
1354 VTONFS(dvp)->n_flag |= NMODIFIED;
1356 VTONFS(dvp)->n_attrstamp = 0;
1357 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1363 * just call nfs_mknodrpc() to do the work.
1367 nfs_mknod(struct vop_mknod_args *ap)
1369 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1372 static u_long create_verf;
1374 * nfs file create call
1377 nfs_create(struct vop_create_args *ap)
1379 struct vnode *dvp = ap->a_dvp;
1380 struct vattr *vap = ap->a_vap;
1381 struct componentname *cnp = ap->a_cnp;
1382 struct nfsv2_sattr *sp;
1384 struct nfsnode *np = NULL;
1385 struct vnode *newvp = NULL;
1387 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1388 struct mbuf *mreq, *mrep, *md, *mb;
1390 int v3 = NFS_ISV3(dvp);
1393 * Oops, not for me..
1395 if (vap->va_type == VSOCK)
1396 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1398 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1401 if (vap->va_vaflags & VA_EXCLUSIVE)
1404 nfsstats.rpccnt[NFSPROC_CREATE]++;
1405 mreq = nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1406 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1408 bpos = mtod(mb, caddr_t);
1409 nfsm_fhtom(dvp, v3);
1410 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1412 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1413 if (fmode & O_EXCL) {
1414 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1415 tl = nfsm_build(u_int32_t *, NFSX_V3CREATEVERF);
1417 if (!TAILQ_EMPTY(&in_ifaddrhead))
1418 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr;
1421 *tl++ = create_verf;
1422 *tl = ++create_verf;
1424 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1425 nfsm_v3attrbuild(vap, FALSE);
1428 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1429 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1430 sp->sa_uid = nfs_xdrneg1;
1431 sp->sa_gid = nfs_xdrneg1;
1433 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1434 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1436 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_thread, cnp->cn_cred);
1438 nfsm_mtofh(dvp, newvp, v3, gotvp);
1444 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1445 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1451 nfsm_wcc_data(dvp, wccflag);
1455 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1461 } else if (v3 && (fmode & O_EXCL)) {
1463 * We are normally called with only a partially initialized
1464 * VAP. Since the NFSv3 spec says that server may use the
1465 * file attributes to store the verifier, the spec requires
1466 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1467 * in atime, but we can't really assume that all servers will
1468 * so we ensure that our SETATTR sets both atime and mtime.
1470 if (vap->va_mtime.tv_sec == VNOVAL)
1471 vfs_timestamp(&vap->va_mtime);
1472 if (vap->va_atime.tv_sec == VNOVAL)
1473 vap->va_atime = vap->va_mtime;
1474 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_thread);
1479 if (cnp->cn_flags & MAKEENTRY)
1480 cache_enter(dvp, newvp, cnp);
1483 mtx_lock(&(VTONFS(dvp))->n_mtx);
1484 VTONFS(dvp)->n_flag |= NMODIFIED;
1486 VTONFS(dvp)->n_attrstamp = 0;
1487 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1492 * nfs file remove call
1493 * To try and make nfs semantics closer to ufs semantics, a file that has
1494 * other processes using the vnode is renamed instead of removed and then
1495 * removed later on the last close.
1496 * - If v_usecount > 1
1497 * If a rename is not already in the works
1498 * call nfs_sillyrename() to set it up
1503 nfs_remove(struct vop_remove_args *ap)
1505 struct vnode *vp = ap->a_vp;
1506 struct vnode *dvp = ap->a_dvp;
1507 struct componentname *cnp = ap->a_cnp;
1508 struct nfsnode *np = VTONFS(vp);
1513 if ((cnp->cn_flags & HASBUF) == 0)
1514 panic("nfs_remove: no name");
1515 if (vrefcnt(vp) < 1)
1516 panic("nfs_remove: bad v_usecount");
1518 if (vp->v_type == VDIR)
1520 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1521 VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_thread) == 0 &&
1522 vattr.va_nlink > 1)) {
1524 * Purge the name cache so that the chance of a lookup for
1525 * the name succeeding while the remove is in progress is
1526 * minimized. Without node locking it can still happen, such
1527 * that an I/O op returns ESTALE, but since you get this if
1528 * another host removes the file..
1532 * throw away biocache buffers, mainly to avoid
1533 * unnecessary delayed writes later.
1535 error = nfs_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1537 if (error != EINTR && error != EIO)
1538 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1539 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1541 * Kludge City: If the first reply to the remove rpc is lost..
1542 * the reply to the retransmitted request will be ENOENT
1543 * since the file was in fact removed
1544 * Therefore, we cheat and return success.
1546 if (error == ENOENT)
1548 } else if (!np->n_sillyrename)
1549 error = nfs_sillyrename(dvp, vp, cnp);
1550 np->n_attrstamp = 0;
1555 * nfs file remove rpc called from nfs_inactive
1558 nfs_removeit(struct sillyrename *sp)
1561 * Make sure that the directory vnode is still valid.
1562 * XXX we should lock sp->s_dvp here.
1564 if (sp->s_dvp->v_type == VBAD)
1566 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1571 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1574 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1575 struct ucred *cred, struct thread *td)
1578 int error = 0, wccflag = NFSV3_WCCRATTR;
1579 struct mbuf *mreq, *mrep, *md, *mb;
1580 int v3 = NFS_ISV3(dvp);
1582 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1583 mreq = nfsm_reqhead(dvp, NFSPROC_REMOVE,
1584 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1586 bpos = mtod(mb, caddr_t);
1587 nfsm_fhtom(dvp, v3);
1588 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1589 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1591 nfsm_wcc_data(dvp, wccflag);
1594 mtx_lock(&(VTONFS(dvp))->n_mtx);
1595 VTONFS(dvp)->n_flag |= NMODIFIED;
1597 VTONFS(dvp)->n_attrstamp = 0;
1598 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1603 * nfs file rename call
1606 nfs_rename(struct vop_rename_args *ap)
1608 struct vnode *fvp = ap->a_fvp;
1609 struct vnode *tvp = ap->a_tvp;
1610 struct vnode *fdvp = ap->a_fdvp;
1611 struct vnode *tdvp = ap->a_tdvp;
1612 struct componentname *tcnp = ap->a_tcnp;
1613 struct componentname *fcnp = ap->a_fcnp;
1617 if ((tcnp->cn_flags & HASBUF) == 0 ||
1618 (fcnp->cn_flags & HASBUF) == 0)
1619 panic("nfs_rename: no name");
1621 /* Check for cross-device rename */
1622 if ((fvp->v_mount != tdvp->v_mount) ||
1623 (tvp && (fvp->v_mount != tvp->v_mount))) {
1629 nfs_printf("nfs_rename: fvp == tvp (can't happen)\n");
1633 if ((error = vn_lock(fvp, LK_EXCLUSIVE, fcnp->cn_thread)) != 0)
1637 * We have to flush B_DELWRI data prior to renaming
1638 * the file. If we don't, the delayed-write buffers
1639 * can be flushed out later after the file has gone stale
1640 * under NFSV3. NFSV2 does not have this problem because
1641 * ( as far as I can tell ) it flushes dirty buffers more
1644 * Skip the rename operation if the fsync fails, this can happen
1645 * due to the server's volume being full, when we pushed out data
1646 * that was written back to our cache earlier. Not checking for
1647 * this condition can result in potential (silent) data loss.
1649 error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1650 VOP_UNLOCK(fvp, 0, fcnp->cn_thread);
1652 error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1657 * If the tvp exists and is in use, sillyrename it before doing the
1658 * rename of the new file over it.
1659 * XXX Can't sillyrename a directory.
1661 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1662 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1667 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1668 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1671 if (fvp->v_type == VDIR) {
1672 if (tvp != NULL && tvp->v_type == VDIR)
1687 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1689 if (error == ENOENT)
1695 * nfs file rename rpc called from nfs_remove() above
1698 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1699 struct sillyrename *sp)
1702 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen, sdvp,
1703 sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_thread));
1707 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1710 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1711 struct vnode *tdvp, const char *tnameptr, int tnamelen, struct ucred *cred,
1715 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1716 struct mbuf *mreq, *mrep, *md, *mb;
1717 int v3 = NFS_ISV3(fdvp);
1719 nfsstats.rpccnt[NFSPROC_RENAME]++;
1720 mreq = nfsm_reqhead(fdvp, NFSPROC_RENAME,
1721 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1722 nfsm_rndup(tnamelen));
1724 bpos = mtod(mb, caddr_t);
1725 nfsm_fhtom(fdvp, v3);
1726 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1727 nfsm_fhtom(tdvp, v3);
1728 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1729 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1731 nfsm_wcc_data(fdvp, fwccflag);
1732 nfsm_wcc_data(tdvp, twccflag);
1736 mtx_lock(&(VTONFS(fdvp))->n_mtx);
1737 VTONFS(fdvp)->n_flag |= NMODIFIED;
1738 mtx_unlock(&(VTONFS(fdvp))->n_mtx);
1739 mtx_lock(&(VTONFS(tdvp))->n_mtx);
1740 VTONFS(tdvp)->n_flag |= NMODIFIED;
1741 mtx_unlock(&(VTONFS(tdvp))->n_mtx);
1743 VTONFS(fdvp)->n_attrstamp = 0;
1745 VTONFS(tdvp)->n_attrstamp = 0;
1750 * nfs hard link create call
1753 nfs_link(struct vop_link_args *ap)
1755 struct vnode *vp = ap->a_vp;
1756 struct vnode *tdvp = ap->a_tdvp;
1757 struct componentname *cnp = ap->a_cnp;
1759 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1760 struct mbuf *mreq, *mrep, *md, *mb;
1763 if (vp->v_mount != tdvp->v_mount) {
1768 * Push all writes to the server, so that the attribute cache
1769 * doesn't get "out of sync" with the server.
1770 * XXX There should be a better way!
1772 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1775 nfsstats.rpccnt[NFSPROC_LINK]++;
1776 mreq = nfsm_reqhead(vp, NFSPROC_LINK,
1777 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1779 bpos = mtod(mb, caddr_t);
1781 nfsm_fhtom(tdvp, v3);
1782 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1783 nfsm_request(vp, NFSPROC_LINK, cnp->cn_thread, cnp->cn_cred);
1785 nfsm_postop_attr(vp, attrflag);
1786 nfsm_wcc_data(tdvp, wccflag);
1790 mtx_lock(&(VTONFS(tdvp))->n_mtx);
1791 VTONFS(tdvp)->n_flag |= NMODIFIED;
1792 mtx_unlock(&(VTONFS(tdvp))->n_mtx);
1794 VTONFS(vp)->n_attrstamp = 0;
1796 VTONFS(tdvp)->n_attrstamp = 0;
1801 * nfs symbolic link create call
1804 nfs_symlink(struct vop_symlink_args *ap)
1806 struct vnode *dvp = ap->a_dvp;
1807 struct vattr *vap = ap->a_vap;
1808 struct componentname *cnp = ap->a_cnp;
1809 struct nfsv2_sattr *sp;
1811 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1812 struct mbuf *mreq, *mrep, *md, *mb;
1813 struct vnode *newvp = NULL;
1814 int v3 = NFS_ISV3(dvp);
1816 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1817 slen = strlen(ap->a_target);
1818 mreq = nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1819 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1821 bpos = mtod(mb, caddr_t);
1822 nfsm_fhtom(dvp, v3);
1823 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1825 nfsm_v3attrbuild(vap, FALSE);
1827 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1829 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1830 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1831 sp->sa_uid = nfs_xdrneg1;
1832 sp->sa_gid = nfs_xdrneg1;
1833 sp->sa_size = nfs_xdrneg1;
1834 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1835 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1839 * Issue the NFS request and get the rpc response.
1841 * Only NFSv3 responses returning an error of 0 actually return
1842 * a file handle that can be converted into newvp without having
1843 * to do an extra lookup rpc.
1845 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_thread, cnp->cn_cred);
1848 nfsm_mtofh(dvp, newvp, v3, gotvp);
1849 nfsm_wcc_data(dvp, wccflag);
1853 * out code jumps -> here, mrep is also freed.
1860 * If we do not have an error and we could not extract the newvp from
1861 * the response due to the request being NFSv2, we have to do a
1862 * lookup in order to obtain a newvp to return.
1864 if (error == 0 && newvp == NULL) {
1865 struct nfsnode *np = NULL;
1867 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1868 cnp->cn_cred, cnp->cn_thread, &np);
1878 mtx_lock(&(VTONFS(dvp))->n_mtx);
1879 VTONFS(dvp)->n_flag |= NMODIFIED;
1880 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1882 VTONFS(dvp)->n_attrstamp = 0;
1890 nfs_mkdir(struct vop_mkdir_args *ap)
1892 struct vnode *dvp = ap->a_dvp;
1893 struct vattr *vap = ap->a_vap;
1894 struct componentname *cnp = ap->a_cnp;
1895 struct nfsv2_sattr *sp;
1897 struct nfsnode *np = NULL;
1898 struct vnode *newvp = NULL;
1900 int error = 0, wccflag = NFSV3_WCCRATTR;
1902 struct mbuf *mreq, *mrep, *md, *mb;
1904 int v3 = NFS_ISV3(dvp);
1906 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1909 len = cnp->cn_namelen;
1910 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1911 mreq = nfsm_reqhead(dvp, NFSPROC_MKDIR,
1912 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1914 bpos = mtod(mb, caddr_t);
1915 nfsm_fhtom(dvp, v3);
1916 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1918 nfsm_v3attrbuild(vap, FALSE);
1920 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1921 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1922 sp->sa_uid = nfs_xdrneg1;
1923 sp->sa_gid = nfs_xdrneg1;
1924 sp->sa_size = nfs_xdrneg1;
1925 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1926 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1928 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_thread, cnp->cn_cred);
1930 nfsm_mtofh(dvp, newvp, v3, gotvp);
1932 nfsm_wcc_data(dvp, wccflag);
1935 mtx_lock(&(VTONFS(dvp))->n_mtx);
1936 VTONFS(dvp)->n_flag |= NMODIFIED;
1937 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1939 VTONFS(dvp)->n_attrstamp = 0;
1940 if (error == 0 && newvp == NULL) {
1941 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
1942 cnp->cn_thread, &np);
1945 if (newvp->v_type != VDIR)
1958 * nfs remove directory call
1961 nfs_rmdir(struct vop_rmdir_args *ap)
1963 struct vnode *vp = ap->a_vp;
1964 struct vnode *dvp = ap->a_dvp;
1965 struct componentname *cnp = ap->a_cnp;
1967 int error = 0, wccflag = NFSV3_WCCRATTR;
1968 struct mbuf *mreq, *mrep, *md, *mb;
1969 int v3 = NFS_ISV3(dvp);
1973 nfsstats.rpccnt[NFSPROC_RMDIR]++;
1974 mreq = nfsm_reqhead(dvp, NFSPROC_RMDIR,
1975 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1977 bpos = mtod(mb, caddr_t);
1978 nfsm_fhtom(dvp, v3);
1979 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1980 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_thread, cnp->cn_cred);
1982 nfsm_wcc_data(dvp, wccflag);
1985 mtx_lock(&(VTONFS(dvp))->n_mtx);
1986 VTONFS(dvp)->n_flag |= NMODIFIED;
1987 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1989 VTONFS(dvp)->n_attrstamp = 0;
1993 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
1995 if (error == ENOENT)
2004 nfs_readdir(struct vop_readdir_args *ap)
2006 struct vnode *vp = ap->a_vp;
2007 struct nfsnode *np = VTONFS(vp);
2008 struct uio *uio = ap->a_uio;
2009 int tresid, error = 0;
2012 if (vp->v_type != VDIR)
2016 * First, check for hit on the EOF offset cache
2018 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2019 (np->n_flag & NMODIFIED) == 0) {
2020 if (VOP_GETATTR(vp, &vattr, ap->a_cred, uio->uio_td) == 0) {
2021 mtx_lock(&np->n_mtx);
2022 if (!NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2023 mtx_unlock(&np->n_mtx);
2024 nfsstats.direofcache_hits++;
2027 mtx_unlock(&np->n_mtx);
2032 * Call nfs_bioread() to do the real work.
2034 tresid = uio->uio_resid;
2035 error = nfs_bioread(vp, uio, 0, ap->a_cred);
2037 if (!error && uio->uio_resid == tresid) {
2038 nfsstats.direofcache_misses++;
2046 * Called from below the buffer cache by nfs_doio().
2049 nfs_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
2052 struct dirent *dp = NULL;
2057 struct mbuf *mreq, *mrep, *md, *mb;
2059 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2060 struct nfsnode *dnp = VTONFS(vp);
2062 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2064 int v3 = NFS_ISV3(vp);
2067 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2068 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2069 panic("nfs readdirrpc bad uio");
2073 * If there is no cookie, assume directory was stale.
2075 nfs_dircookie_lock(dnp);
2076 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2079 nfs_dircookie_unlock(dnp);
2081 nfs_dircookie_unlock(dnp);
2082 return (NFSERR_BAD_COOKIE);
2086 * Loop around doing readdir rpc's of size nm_readdirsize
2087 * truncated to a multiple of DIRBLKSIZ.
2088 * The stopping criteria is EOF or buffer full.
2090 while (more_dirs && bigenough) {
2091 nfsstats.rpccnt[NFSPROC_READDIR]++;
2092 mreq = nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2095 bpos = mtod(mb, caddr_t);
2098 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
2099 *tl++ = cookie.nfsuquad[0];
2100 *tl++ = cookie.nfsuquad[1];
2101 mtx_lock(&dnp->n_mtx);
2102 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2103 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2104 mtx_unlock(&dnp->n_mtx);
2106 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
2107 *tl++ = cookie.nfsuquad[0];
2109 *tl = txdr_unsigned(nmp->nm_readdirsize);
2110 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, cred);
2112 nfsm_postop_attr(vp, attrflag);
2114 tl = nfsm_dissect(u_int32_t *,
2116 mtx_lock(&dnp->n_mtx);
2117 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2118 dnp->n_cookieverf.nfsuquad[1] = *tl;
2119 mtx_unlock(&dnp->n_mtx);
2125 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2126 more_dirs = fxdr_unsigned(int, *tl);
2128 /* loop thru the dir entries, doctoring them to 4bsd form */
2129 while (more_dirs && bigenough) {
2131 tl = nfsm_dissect(u_int32_t *,
2133 fileno = fxdr_hyper(tl);
2134 len = fxdr_unsigned(int, *(tl + 2));
2136 tl = nfsm_dissect(u_int32_t *,
2138 fileno = fxdr_unsigned(u_quad_t, *tl++);
2139 len = fxdr_unsigned(int, *tl);
2141 if (len <= 0 || len > NFS_MAXNAMLEN) {
2146 tlen = nfsm_rndup(len);
2148 tlen += 4; /* To ensure null termination */
2149 left = DIRBLKSIZ - blksiz;
2150 if ((tlen + DIRHDSIZ) > left) {
2151 dp->d_reclen += left;
2152 uiop->uio_iov->iov_base =
2153 (char *)uiop->uio_iov->iov_base + left;
2154 uiop->uio_iov->iov_len -= left;
2155 uiop->uio_offset += left;
2156 uiop->uio_resid -= left;
2159 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2162 dp = (struct dirent *)uiop->uio_iov->iov_base;
2163 dp->d_fileno = (int)fileno;
2165 dp->d_reclen = tlen + DIRHDSIZ;
2166 dp->d_type = DT_UNKNOWN;
2167 blksiz += dp->d_reclen;
2168 if (blksiz == DIRBLKSIZ)
2170 uiop->uio_offset += DIRHDSIZ;
2171 uiop->uio_resid -= DIRHDSIZ;
2172 uiop->uio_iov->iov_base =
2173 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
2174 uiop->uio_iov->iov_len -= DIRHDSIZ;
2175 nfsm_mtouio(uiop, len);
2176 cp = uiop->uio_iov->iov_base;
2178 *cp = '\0'; /* null terminate */
2179 uiop->uio_iov->iov_base =
2180 (char *)uiop->uio_iov->iov_base + tlen;
2181 uiop->uio_iov->iov_len -= tlen;
2182 uiop->uio_offset += tlen;
2183 uiop->uio_resid -= tlen;
2185 nfsm_adv(nfsm_rndup(len));
2187 tl = nfsm_dissect(u_int32_t *,
2190 tl = nfsm_dissect(u_int32_t *,
2194 cookie.nfsuquad[0] = *tl++;
2196 cookie.nfsuquad[1] = *tl++;
2201 more_dirs = fxdr_unsigned(int, *tl);
2204 * If at end of rpc data, get the eof boolean
2207 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2208 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2213 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2214 * by increasing d_reclen for the last record.
2217 left = DIRBLKSIZ - blksiz;
2218 dp->d_reclen += left;
2219 uiop->uio_iov->iov_base =
2220 (char *)uiop->uio_iov->iov_base + left;
2221 uiop->uio_iov->iov_len -= left;
2222 uiop->uio_offset += left;
2223 uiop->uio_resid -= left;
2227 * We are now either at the end of the directory or have filled the
2231 dnp->n_direofoffset = uiop->uio_offset;
2233 if (uiop->uio_resid > 0)
2234 nfs_printf("EEK! readdirrpc resid > 0\n");
2235 nfs_dircookie_lock(dnp);
2236 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2238 nfs_dircookie_unlock(dnp);
2245 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2248 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
2254 struct vnode *newvp;
2256 caddr_t bpos, dpos, dpossav1, dpossav2;
2257 struct mbuf *mreq, *mrep, *md, *mb, *mdsav1, *mdsav2;
2258 struct nameidata nami, *ndp = &nami;
2259 struct componentname *cnp = &ndp->ni_cnd;
2261 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2262 struct nfsnode *dnp = VTONFS(vp), *np;
2265 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2266 int attrflag, fhsize;
2272 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2273 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2274 panic("nfs readdirplusrpc bad uio");
2280 * If there is no cookie, assume directory was stale.
2282 nfs_dircookie_lock(dnp);
2283 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2286 nfs_dircookie_unlock(dnp);
2288 nfs_dircookie_unlock(dnp);
2289 return (NFSERR_BAD_COOKIE);
2292 * Loop around doing readdir rpc's of size nm_readdirsize
2293 * truncated to a multiple of DIRBLKSIZ.
2294 * The stopping criteria is EOF or buffer full.
2296 while (more_dirs && bigenough) {
2297 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2298 mreq = nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2299 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2301 bpos = mtod(mb, caddr_t);
2303 tl = nfsm_build(u_int32_t *, 6 * NFSX_UNSIGNED);
2304 *tl++ = cookie.nfsuquad[0];
2305 *tl++ = cookie.nfsuquad[1];
2306 mtx_lock(&dnp->n_mtx);
2307 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2308 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2309 mtx_unlock(&dnp->n_mtx);
2310 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2311 *tl = txdr_unsigned(nmp->nm_rsize);
2312 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, cred);
2313 nfsm_postop_attr(vp, attrflag);
2318 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2319 mtx_lock(&dnp->n_mtx);
2320 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2321 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2322 mtx_unlock(&dnp->n_mtx);
2323 more_dirs = fxdr_unsigned(int, *tl);
2325 /* loop thru the dir entries, doctoring them to 4bsd form */
2326 while (more_dirs && bigenough) {
2327 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2328 fileno = fxdr_hyper(tl);
2329 len = fxdr_unsigned(int, *(tl + 2));
2330 if (len <= 0 || len > NFS_MAXNAMLEN) {
2335 tlen = nfsm_rndup(len);
2337 tlen += 4; /* To ensure null termination*/
2338 left = DIRBLKSIZ - blksiz;
2339 if ((tlen + DIRHDSIZ) > left) {
2340 dp->d_reclen += left;
2341 uiop->uio_iov->iov_base =
2342 (char *)uiop->uio_iov->iov_base + left;
2343 uiop->uio_iov->iov_len -= left;
2344 uiop->uio_offset += left;
2345 uiop->uio_resid -= left;
2348 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2351 dp = (struct dirent *)uiop->uio_iov->iov_base;
2352 dp->d_fileno = (int)fileno;
2354 dp->d_reclen = tlen + DIRHDSIZ;
2355 dp->d_type = DT_UNKNOWN;
2356 blksiz += dp->d_reclen;
2357 if (blksiz == DIRBLKSIZ)
2359 uiop->uio_offset += DIRHDSIZ;
2360 uiop->uio_resid -= DIRHDSIZ;
2361 uiop->uio_iov->iov_base =
2362 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
2363 uiop->uio_iov->iov_len -= DIRHDSIZ;
2364 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2365 cnp->cn_namelen = len;
2366 nfsm_mtouio(uiop, len);
2367 cp = uiop->uio_iov->iov_base;
2370 uiop->uio_iov->iov_base =
2371 (char *)uiop->uio_iov->iov_base + tlen;
2372 uiop->uio_iov->iov_len -= tlen;
2373 uiop->uio_offset += tlen;
2374 uiop->uio_resid -= tlen;
2376 nfsm_adv(nfsm_rndup(len));
2377 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2379 cookie.nfsuquad[0] = *tl++;
2380 cookie.nfsuquad[1] = *tl++;
2385 * Since the attributes are before the file handle
2386 * (sigh), we must skip over the attributes and then
2387 * come back and get them.
2389 attrflag = fxdr_unsigned(int, *tl);
2393 nfsm_adv(NFSX_V3FATTR);
2394 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2395 doit = fxdr_unsigned(int, *tl);
2397 * Skip loading the attrs for "..". There's a
2398 * race between loading the attrs here and
2399 * lookups that look for the directory currently
2400 * being read (in the parent). We try to acquire
2401 * the exclusive lock on ".." here, owning the
2402 * lock on the directory being read. Lookup will
2403 * hold the lock on ".." and try to acquire the
2404 * lock on the directory being read.
2406 * There are other ways of fixing this, one would
2407 * be to do a trylock on the ".." vnode and skip
2408 * loading the attrs on ".." if it happens to be
2409 * locked by another process. But skipping the
2410 * attrload on ".." seems the easiest option.
2412 if (strcmp(dp->d_name, "..") == 0) {
2415 * We've already skipped over the attrs,
2416 * skip over the filehandle. And store d_type
2419 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2420 i = fxdr_unsigned(int, *tl);
2421 nfsm_adv(nfsm_rndup(i));
2422 dp->d_type = IFTODT(VTTOIF(VDIR));
2425 nfsm_getfh(fhp, fhsize, 1);
2426 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2431 error = nfs_nget(vp->v_mount, fhp,
2432 fhsize, &np, LK_EXCLUSIVE);
2439 if (doit && bigenough) {
2444 nfsm_loadattr(newvp, NULL);
2448 IFTODT(VTTOIF(np->n_vattr.va_type));
2450 /* Update n_ctime, so subsequent lookup doesn't purge entry */
2451 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
2452 cache_enter(ndp->ni_dvp, ndp->ni_vp, cnp);
2455 /* Just skip over the file handle */
2456 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2457 i = fxdr_unsigned(int, *tl);
2459 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2460 fhsize = fxdr_unsigned(int, *tl);
2461 nfsm_adv(nfsm_rndup(fhsize));
2464 if (newvp != NULLVP) {
2471 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2472 more_dirs = fxdr_unsigned(int, *tl);
2475 * If at end of rpc data, get the eof boolean
2478 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2479 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2484 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2485 * by increasing d_reclen for the last record.
2488 left = DIRBLKSIZ - blksiz;
2489 dp->d_reclen += left;
2490 uiop->uio_iov->iov_base =
2491 (char *)uiop->uio_iov->iov_base + left;
2492 uiop->uio_iov->iov_len -= left;
2493 uiop->uio_offset += left;
2494 uiop->uio_resid -= left;
2498 * We are now either at the end of the directory or have filled the
2502 dnp->n_direofoffset = uiop->uio_offset;
2504 if (uiop->uio_resid > 0)
2505 nfs_printf("EEK! readdirplusrpc resid > 0\n");
2506 nfs_dircookie_lock(dnp);
2507 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2509 nfs_dircookie_unlock(dnp);
2512 if (newvp != NULLVP) {
2523 * Silly rename. To make the NFS filesystem that is stateless look a little
2524 * more like the "ufs" a remove of an active vnode is translated to a rename
2525 * to a funny looking filename that is removed by nfs_inactive on the
2526 * nfsnode. There is the potential for another process on a different client
2527 * to create the same funny name between the nfs_lookitup() fails and the
2528 * nfs_rename() completes, but...
2531 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2533 struct sillyrename *sp;
2537 unsigned int lticks;
2542 if (vp->v_type == VDIR)
2543 panic("nfs: sillyrename dir");
2545 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2546 M_NFSREQ, M_WAITOK);
2547 sp->s_cred = crhold(cnp->cn_cred);
2549 sp->s_removeit = nfs_removeit;
2553 * Fudge together a funny name.
2554 * Changing the format of the funny name to accomodate more
2555 * sillynames per directory.
2556 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2557 * CPU ticks since boot.
2559 pid = cnp->cn_thread->td_proc->p_pid;
2560 lticks = (unsigned int)ticks;
2562 sp->s_namlen = sprintf(sp->s_name,
2563 ".nfs.%08x.%04x4.4", lticks,
2565 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2566 cnp->cn_thread, NULL))
2570 error = nfs_renameit(dvp, cnp, sp);
2573 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2574 cnp->cn_thread, &np);
2575 np->n_sillyrename = sp;
2580 free((caddr_t)sp, M_NFSREQ);
2585 * Look up a file name and optionally either update the file handle or
2586 * allocate an nfsnode, depending on the value of npp.
2587 * npp == NULL --> just do the lookup
2588 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2590 * *npp != NULL --> update the file handle in the vnode
2593 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2594 struct thread *td, struct nfsnode **npp)
2596 struct vnode *newvp = NULL;
2597 struct nfsnode *np, *dnp = VTONFS(dvp);
2599 int error = 0, fhlen, attrflag;
2600 struct mbuf *mreq, *mrep, *md, *mb;
2602 int v3 = NFS_ISV3(dvp);
2604 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2605 mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2606 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2608 bpos = mtod(mb, caddr_t);
2609 nfsm_fhtom(dvp, v3);
2610 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2611 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2612 if (npp && !error) {
2613 nfsm_getfh(nfhp, fhlen, v3);
2616 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2617 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2618 np->n_fhp = &np->n_fh;
2619 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2620 np->n_fhp =(nfsfh_t *)malloc(fhlen, M_NFSBIGFH, M_WAITOK);
2621 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2622 np->n_fhsize = fhlen;
2624 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2628 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, LK_EXCLUSIVE);
2636 nfsm_postop_attr(newvp, attrflag);
2637 if (!attrflag && *npp == NULL) {
2646 nfsm_loadattr(newvp, NULL);
2650 if (npp && *npp == NULL) {
2665 * Nfs Version 3 commit rpc
2668 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2672 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2674 int error = 0, wccflag = NFSV3_WCCRATTR;
2675 struct mbuf *mreq, *mrep, *md, *mb;
2677 mtx_lock(&nmp->nm_mtx);
2678 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2679 mtx_unlock(&nmp->nm_mtx);
2682 mtx_unlock(&nmp->nm_mtx);
2683 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2684 mreq = nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2686 bpos = mtod(mb, caddr_t);
2688 tl = nfsm_build(u_int32_t *, 3 * NFSX_UNSIGNED);
2689 txdr_hyper(offset, tl);
2691 *tl = txdr_unsigned(cnt);
2692 nfsm_request(vp, NFSPROC_COMMIT, td, cred);
2693 nfsm_wcc_data(vp, wccflag);
2695 tl = nfsm_dissect(u_int32_t *, NFSX_V3WRITEVERF);
2696 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2697 NFSX_V3WRITEVERF)) {
2698 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2700 error = NFSERR_STALEWRITEVERF;
2710 * For async requests when nfsiod(s) are running, queue the request by
2711 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2715 nfs_strategy(struct vop_strategy_args *ap)
2717 struct buf *bp = ap->a_bp;
2720 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2721 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2723 if (bp->b_iocmd == BIO_READ)
2729 * If the op is asynchronous and an i/o daemon is waiting
2730 * queue the request, wake it up and wait for completion
2731 * otherwise just do it ourselves.
2733 if ((bp->b_flags & B_ASYNC) == 0 ||
2734 nfs_asyncio(VFSTONFS(ap->a_vp->v_mount), bp, NOCRED, curthread))
2735 (void)nfs_doio(ap->a_vp, bp, cr, curthread);
2740 * fsync vnode op. Just call nfs_flush() with commit == 1.
2744 nfs_fsync(struct vop_fsync_args *ap)
2746 return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2750 * Flush all the blocks associated with a vnode.
2751 * Walk through the buffer pool and push any dirty pages
2752 * associated with the vnode.
2755 nfs_flush(struct vnode *vp, int waitfor, struct thread *td,
2758 struct nfsnode *np = VTONFS(vp);
2762 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2763 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2765 u_quad_t off, endoff, toff;
2766 struct ucred* wcred = NULL;
2767 struct buf **bvec = NULL;
2768 #ifndef NFS_COMMITBVECSIZ
2769 #define NFS_COMMITBVECSIZ 20
2771 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2772 int bvecsize = 0, bveccount;
2774 if (nmp->nm_flag & NFSMNT_INT)
2779 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2780 * server, but has not been committed to stable storage on the server
2781 * yet. On the first pass, the byte range is worked out and the commit
2782 * rpc is done. On the second pass, nfs_writebp() is called to do the
2789 if (NFS_ISV3(vp) && commit) {
2791 if (bvec != NULL && bvec != bvec_on_stack)
2794 * Count up how many buffers waiting for a commit.
2798 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
2799 if (BUF_REFCNT(bp) == 0 &&
2800 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2801 == (B_DELWRI | B_NEEDCOMMIT))
2805 * Allocate space to remember the list of bufs to commit. It is
2806 * important to use M_NOWAIT here to avoid a race with nfs_write.
2807 * If we can't get memory (for whatever reason), we will end up
2808 * committing the buffers one-by-one in the loop below.
2810 if (bveccount > NFS_COMMITBVECSIZ) {
2812 * Release the vnode interlock to avoid a lock
2816 bvec = (struct buf **)
2817 malloc(bveccount * sizeof(struct buf *),
2821 bvec = bvec_on_stack;
2822 bvecsize = NFS_COMMITBVECSIZ;
2824 bvecsize = bveccount;
2826 bvec = bvec_on_stack;
2827 bvecsize = NFS_COMMITBVECSIZ;
2829 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
2830 if (bvecpos >= bvecsize)
2832 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2833 nbp = TAILQ_NEXT(bp, b_bobufs);
2836 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2837 (B_DELWRI | B_NEEDCOMMIT)) {
2839 nbp = TAILQ_NEXT(bp, b_bobufs);
2845 * Work out if all buffers are using the same cred
2846 * so we can deal with them all with one commit.
2848 * NOTE: we are not clearing B_DONE here, so we have
2849 * to do it later on in this routine if we intend to
2850 * initiate I/O on the bp.
2852 * Note: to avoid loopback deadlocks, we do not
2853 * assign b_runningbufspace.
2856 wcred = bp->b_wcred;
2857 else if (wcred != bp->b_wcred)
2859 vfs_busy_pages(bp, 1);
2863 * bp is protected by being locked, but nbp is not
2864 * and vfs_busy_pages() may sleep. We have to
2867 nbp = TAILQ_NEXT(bp, b_bobufs);
2870 * A list of these buffers is kept so that the
2871 * second loop knows which buffers have actually
2872 * been committed. This is necessary, since there
2873 * may be a race between the commit rpc and new
2874 * uncommitted writes on the file.
2876 bvec[bvecpos++] = bp;
2877 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2881 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2890 * Commit data on the server, as required.
2891 * If all bufs are using the same wcred, then use that with
2892 * one call for all of them, otherwise commit each one
2895 if (wcred != NOCRED)
2896 retv = nfs_commit(vp, off, (int)(endoff - off),
2900 for (i = 0; i < bvecpos; i++) {
2903 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2905 size = (u_quad_t)(bp->b_dirtyend
2907 retv = nfs_commit(vp, off, (int)size,
2913 if (retv == NFSERR_STALEWRITEVERF)
2914 nfs_clearcommit(vp->v_mount);
2917 * Now, either mark the blocks I/O done or mark the
2918 * blocks dirty, depending on whether the commit
2921 for (i = 0; i < bvecpos; i++) {
2923 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2926 * Error, leave B_DELWRI intact
2928 vfs_unbusy_pages(bp);
2932 * Success, remove B_DELWRI ( bundirty() ).
2934 * b_dirtyoff/b_dirtyend seem to be NFS
2935 * specific. We should probably move that
2936 * into bundirty(). XXX
2939 bufobj_wref(&vp->v_bufobj);
2940 bp->b_flags |= B_ASYNC;
2942 bp->b_flags &= ~B_DONE;
2943 bp->b_ioflags &= ~BIO_ERROR;
2944 bp->b_dirtyoff = bp->b_dirtyend = 0;
2952 * Start/do any write(s) that are required.
2957 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
2958 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2959 if (waitfor != MNT_WAIT || passone)
2962 error = BUF_TIMELOCK(bp,
2963 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2964 VI_MTX(vp), "nfsfsync", slpflag, slptimeo);
2970 if (error == ENOLCK)
2972 if (nfs_sigintr(nmp, NULL, td)) {
2976 if (slpflag == PCATCH) {
2982 if ((bp->b_flags & B_DELWRI) == 0)
2983 panic("nfs_fsync: not dirty");
2984 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2990 if (passone || !commit)
2991 bp->b_flags |= B_ASYNC;
2993 bp->b_flags |= B_ASYNC;
2996 if (nfs_sigintr(nmp, NULL, td)) {
3008 if (waitfor == MNT_WAIT) {
3009 while (vp->v_bufobj.bo_numoutput) {
3010 error = bufobj_wwait(&vp->v_bufobj, slpflag, slptimeo);
3013 error = nfs_sigintr(nmp, NULL, td);
3016 if (slpflag == PCATCH) {
3023 if (vp->v_bufobj.bo_dirty.bv_cnt != 0 && commit) {
3028 * Wait for all the async IO requests to drain
3031 mtx_lock(&np->n_mtx);
3032 while (np->n_directio_asyncwr > 0) {
3033 np->n_flag |= NFSYNCWAIT;
3034 error = nfs_msleep(td, (caddr_t)&np->n_directio_asyncwr,
3035 &np->n_mtx, slpflag | (PRIBIO + 1),
3038 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
3039 mtx_unlock(&np->n_mtx);
3045 mtx_unlock(&np->n_mtx);
3048 mtx_lock(&np->n_mtx);
3049 if (np->n_flag & NWRITEERR) {
3050 error = np->n_error;
3051 np->n_flag &= ~NWRITEERR;
3053 if (commit && vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
3054 vp->v_bufobj.bo_numoutput == 0 && np->n_directio_asyncwr == 0)
3055 np->n_flag &= ~NMODIFIED;
3056 mtx_unlock(&np->n_mtx);
3058 if (bvec != NULL && bvec != bvec_on_stack)
3064 * NFS advisory byte-level locks.
3067 nfs_advlock(struct vop_advlock_args *ap)
3069 struct vnode *vp = ap->a_vp;
3073 error = vn_lock(vp, LK_SHARED, curthread);
3076 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3077 size = VTONFS(vp)->n_size;
3078 VOP_UNLOCK(vp, 0, curthread);
3079 error = lf_advlock(ap, &(vp->v_lockf), size);
3082 error = nfs_advlock_p(ap);
3091 * NFS advisory byte-level locks.
3094 nfs_advlockasync(struct vop_advlockasync_args *ap)
3096 struct vnode *vp = ap->a_vp;
3100 error = vn_lock(vp, LK_SHARED, curthread);
3103 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3104 size = VTONFS(vp)->n_size;
3105 VOP_UNLOCK(vp, 0, curthread);
3106 error = lf_advlockasync(ap, &(vp->v_lockf), size);
3108 VOP_UNLOCK(vp, 0, curthread);
3115 * Print out the contents of an nfsnode.
3118 nfs_print(struct vop_print_args *ap)
3120 struct vnode *vp = ap->a_vp;
3121 struct nfsnode *np = VTONFS(vp);
3123 nfs_printf("\tfileid %ld fsid 0x%x",
3124 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3125 if (vp->v_type == VFIFO)
3132 * This is the "real" nfs::bwrite(struct buf*).
3133 * We set B_CACHE if this is a VMIO buffer.
3136 nfs_writebp(struct buf *bp, int force __unused, struct thread *td)
3139 int oldflags = bp->b_flags;
3145 if (BUF_REFCNT(bp) == 0)
3146 panic("bwrite: buffer is not locked???");
3148 if (bp->b_flags & B_INVAL) {
3153 bp->b_flags |= B_CACHE;
3156 * Undirty the bp. We will redirty it later if the I/O fails.
3161 bp->b_flags &= ~B_DONE;
3162 bp->b_ioflags &= ~BIO_ERROR;
3163 bp->b_iocmd = BIO_WRITE;
3165 bufobj_wref(bp->b_bufobj);
3166 curthread->td_ru.ru_oublock++;
3170 * Note: to avoid loopback deadlocks, we do not
3171 * assign b_runningbufspace.
3173 vfs_busy_pages(bp, 1);
3176 bp->b_iooffset = dbtob(bp->b_blkno);
3179 if( (oldflags & B_ASYNC) == 0) {
3180 int rtval = bufwait(bp);
3182 if (oldflags & B_DELWRI) {
3195 * nfs special file access vnode op.
3196 * Essentially just get vattr and then imitate iaccess() since the device is
3197 * local to the client.
3200 nfsspec_access(struct vop_access_args *ap)
3203 struct ucred *cred = ap->a_cred;
3204 struct vnode *vp = ap->a_vp;
3205 mode_t mode = ap->a_mode;
3210 * Disallow write attempts on filesystems mounted read-only;
3211 * unless the file is a socket, fifo, or a block or character
3212 * device resident on the filesystem.
3214 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3215 switch (vp->v_type) {
3225 error = VOP_GETATTR(vp, vap, cred, ap->a_td);
3228 error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3235 * Read wrapper for fifos.
3238 nfsfifo_read(struct vop_read_args *ap)
3240 struct nfsnode *np = VTONFS(ap->a_vp);
3246 mtx_lock(&np->n_mtx);
3248 getnanotime(&np->n_atim);
3249 mtx_unlock(&np->n_mtx);
3250 error = fifo_specops.vop_read(ap);
3255 * Write wrapper for fifos.
3258 nfsfifo_write(struct vop_write_args *ap)
3260 struct nfsnode *np = VTONFS(ap->a_vp);
3265 mtx_lock(&np->n_mtx);
3267 getnanotime(&np->n_mtim);
3268 mtx_unlock(&np->n_mtx);
3269 return(fifo_specops.vop_write(ap));
3273 * Close wrapper for fifos.
3275 * Update the times on the nfsnode then do fifo close.
3278 nfsfifo_close(struct vop_close_args *ap)
3280 struct vnode *vp = ap->a_vp;
3281 struct nfsnode *np = VTONFS(vp);
3285 mtx_lock(&np->n_mtx);
3286 if (np->n_flag & (NACC | NUPD)) {
3288 if (np->n_flag & NACC)
3290 if (np->n_flag & NUPD)
3293 if (vrefcnt(vp) == 1 &&
3294 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3296 if (np->n_flag & NACC)
3297 vattr.va_atime = np->n_atim;
3298 if (np->n_flag & NUPD)
3299 vattr.va_mtime = np->n_mtim;
3300 mtx_unlock(&np->n_mtx);
3301 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_td);
3305 mtx_unlock(&np->n_mtx);
3307 return (fifo_specops.vop_close(ap));
3311 * Just call nfs_writebp() with the force argument set to 1.
3313 * NOTE: B_DONE may or may not be set in a_bp on call.
3316 nfs_bwrite(struct buf *bp)
3319 return (nfs_writebp(bp, 1, curthread));
3322 struct buf_ops buf_ops_nfs = {
3323 .bop_name = "buf_ops_nfs",
3324 .bop_write = nfs_bwrite,
3325 .bop_strategy = bufstrategy,
3326 .bop_sync = bufsync,
3327 .bop_bdflush = bufbdflush,