2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * vnode op calls for Sun NFS version 2 and 3
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/systm.h>
47 #include <sys/resourcevar.h>
49 #include <sys/mount.h>
52 #include <sys/malloc.h>
54 #include <sys/namei.h>
55 #include <sys/socket.h>
56 #include <sys/vnode.h>
57 #include <sys/dirent.h>
58 #include <sys/fcntl.h>
59 #include <sys/lockf.h>
61 #include <sys/sysctl.h>
62 #include <sys/signalvar.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_extern.h>
67 #include <vm/vm_object.h>
69 #include <fs/fifofs/fifo.h>
71 #include <rpc/rpcclnt.h>
73 #include <nfs/rpcv2.h>
74 #include <nfs/nfsproto.h>
75 #include <nfsclient/nfs.h>
76 #include <nfsclient/nfsnode.h>
77 #include <nfsclient/nfsmount.h>
78 #include <nfsclient/nfs_lock.h>
79 #include <nfs/xdr_subs.h>
80 #include <nfsclient/nfsm_subs.h>
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
91 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
92 * calls are not in getblk() and brelse() so that they would not be necessary
96 #define vfs_busy_pages(bp, f)
99 static vop_read_t nfsfifo_read;
100 static vop_write_t nfsfifo_write;
101 static vop_close_t nfsfifo_close;
102 static int nfs_flush(struct vnode *, int, struct thread *,
104 static int nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
106 static vop_lookup_t nfs_lookup;
107 static vop_create_t nfs_create;
108 static vop_mknod_t nfs_mknod;
109 static vop_open_t nfs_open;
110 static vop_close_t nfs_close;
111 static vop_access_t nfs_access;
112 static vop_getattr_t nfs_getattr;
113 static vop_setattr_t nfs_setattr;
114 static vop_read_t nfs_read;
115 static vop_fsync_t nfs_fsync;
116 static vop_remove_t nfs_remove;
117 static vop_link_t nfs_link;
118 static vop_rename_t nfs_rename;
119 static vop_mkdir_t nfs_mkdir;
120 static vop_rmdir_t nfs_rmdir;
121 static vop_symlink_t nfs_symlink;
122 static vop_readdir_t nfs_readdir;
123 static vop_strategy_t nfs_strategy;
124 static int nfs_lookitup(struct vnode *, const char *, int,
125 struct ucred *, struct thread *, struct nfsnode **);
126 static int nfs_sillyrename(struct vnode *, struct vnode *,
127 struct componentname *);
128 static vop_access_t nfsspec_access;
129 static vop_readlink_t nfs_readlink;
130 static vop_print_t nfs_print;
131 static vop_advlock_t nfs_advlock;
134 * Global vfs data structures for nfs
136 struct vop_vector nfs_vnodeops = {
137 .vop_default = &default_vnodeops,
138 .vop_access = nfs_access,
139 .vop_advlock = nfs_advlock,
140 .vop_close = nfs_close,
141 .vop_create = nfs_create,
142 .vop_fsync = nfs_fsync,
143 .vop_getattr = nfs_getattr,
144 .vop_getpages = nfs_getpages,
145 .vop_putpages = nfs_putpages,
146 .vop_inactive = nfs_inactive,
147 .vop_lease = VOP_NULL,
148 .vop_link = nfs_link,
149 .vop_lookup = nfs_lookup,
150 .vop_mkdir = nfs_mkdir,
151 .vop_mknod = nfs_mknod,
152 .vop_open = nfs_open,
153 .vop_print = nfs_print,
154 .vop_read = nfs_read,
155 .vop_readdir = nfs_readdir,
156 .vop_readlink = nfs_readlink,
157 .vop_reclaim = nfs_reclaim,
158 .vop_remove = nfs_remove,
159 .vop_rename = nfs_rename,
160 .vop_rmdir = nfs_rmdir,
161 .vop_setattr = nfs_setattr,
162 .vop_strategy = nfs_strategy,
163 .vop_symlink = nfs_symlink,
164 .vop_write = nfs_write,
167 struct vop_vector nfs_fifoops = {
168 .vop_default = &fifo_specops,
169 .vop_access = nfsspec_access,
170 .vop_close = nfsfifo_close,
171 .vop_fsync = nfs_fsync,
172 .vop_getattr = nfs_getattr,
173 .vop_inactive = nfs_inactive,
174 .vop_print = nfs_print,
175 .vop_read = nfsfifo_read,
176 .vop_reclaim = nfs_reclaim,
177 .vop_setattr = nfs_setattr,
178 .vop_write = nfsfifo_write,
181 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
182 struct componentname *cnp, struct vattr *vap);
183 static int nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
184 struct ucred *cred, struct thread *td);
185 static int nfs_renamerpc(struct vnode *fdvp, const char *fnameptr,
186 int fnamelen, struct vnode *tdvp,
187 const char *tnameptr, int tnamelen,
188 struct ucred *cred, struct thread *td);
189 static int nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
190 struct sillyrename *sp);
195 struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
196 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
197 int nfs_numasync = 0;
198 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
200 SYSCTL_DECL(_vfs_nfs);
202 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
203 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
204 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
206 static int nfsv3_commit_on_close = 0;
207 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
208 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
210 static int nfs_clean_pages_on_close = 1;
211 SYSCTL_INT(_vfs_nfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
212 &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
214 int nfs_directio_enable = 0;
215 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
216 &nfs_directio_enable, 0, "Enable NFS directio");
219 * This sysctl allows other processes to mmap a file that has been opened
220 * O_DIRECT by a process. In general, having processes mmap the file while
221 * Direct IO is in progress can lead to Data Inconsistencies. But, we allow
222 * this by default to prevent DoS attacks - to prevent a malicious user from
223 * opening up files O_DIRECT preventing other users from mmap'ing these
224 * files. "Protected" environments where stricter consistency guarantees are
225 * required can disable this knob. The process that opened the file O_DIRECT
226 * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
229 int nfs_directio_allow_mmap = 1;
230 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
231 &nfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
234 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
235 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
237 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
238 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
241 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
242 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
243 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
245 nfs3_access_otw(struct vnode *vp, int wmode, struct thread *td,
250 int error = 0, attrflag;
252 struct mbuf *mreq, *mrep, *md, *mb;
255 struct nfsnode *np = VTONFS(vp);
257 nfsstats.rpccnt[NFSPROC_ACCESS]++;
258 mreq = nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
260 bpos = mtod(mb, caddr_t);
262 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
263 *tl = txdr_unsigned(wmode);
264 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
265 nfsm_postop_attr(vp, attrflag);
267 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
268 rmode = fxdr_unsigned(u_int32_t, *tl);
270 np->n_modeuid = cred->cr_uid;
271 np->n_modestamp = time_second;
279 * nfs access vnode op.
280 * For nfs version 2, just return ok. File accesses may fail later.
281 * For nfs version 3, use the access rpc to check accessibility. If file modes
282 * are changed on the server, accesses might still fail later.
285 nfs_access(struct vop_access_args *ap)
287 struct vnode *vp = ap->a_vp;
289 u_int32_t mode, wmode;
290 int v3 = NFS_ISV3(vp);
291 struct nfsnode *np = VTONFS(vp);
294 * Disallow write attempts on filesystems mounted read-only;
295 * unless the file is a socket, fifo, or a block or character
296 * device resident on the filesystem.
298 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
299 switch (vp->v_type) {
309 * For nfs v3, check to see if we have done this recently, and if
310 * so return our cached result instead of making an ACCESS call.
311 * If not, do an access rpc, otherwise you are stuck emulating
312 * ufs_access() locally using the vattr. This may not be correct,
313 * since the server may apply other access criteria such as
314 * client uid-->server uid mapping that we do not know about.
317 if (ap->a_mode & VREAD)
318 mode = NFSV3ACCESS_READ;
321 if (vp->v_type != VDIR) {
322 if (ap->a_mode & VWRITE)
323 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
324 if (ap->a_mode & VEXEC)
325 mode |= NFSV3ACCESS_EXECUTE;
327 if (ap->a_mode & VWRITE)
328 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
330 if (ap->a_mode & VEXEC)
331 mode |= NFSV3ACCESS_LOOKUP;
333 /* XXX safety belt, only make blanket request if caching */
334 if (nfsaccess_cache_timeout > 0) {
335 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
336 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
337 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
343 * Does our cached result allow us to give a definite yes to
346 if ((time_second < (np->n_modestamp + nfsaccess_cache_timeout)) &&
347 (ap->a_cred->cr_uid == np->n_modeuid) &&
348 ((np->n_mode & mode) == mode)) {
349 nfsstats.accesscache_hits++;
352 * Either a no, or a don't know. Go to the wire.
354 nfsstats.accesscache_misses++;
355 error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred);
357 if ((np->n_mode & mode) != mode) {
364 if ((error = nfsspec_access(ap)) != 0)
368 * Attempt to prevent a mapped root from accessing a file
369 * which it shouldn't. We try to read a byte from the file
370 * if the user is root and the file is not zero length.
371 * After calling nfsspec_access, we should have the correct
374 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
375 && VTONFS(vp)->n_size > 0) {
382 auio.uio_iov = &aiov;
386 auio.uio_segflg = UIO_SYSSPACE;
387 auio.uio_rw = UIO_READ;
388 auio.uio_td = ap->a_td;
390 if (vp->v_type == VREG)
391 error = nfs_readrpc(vp, &auio, ap->a_cred);
392 else if (vp->v_type == VDIR) {
394 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
396 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
397 error = nfs_readdirrpc(vp, &auio, ap->a_cred);
399 } else if (vp->v_type == VLNK)
400 error = nfs_readlinkrpc(vp, &auio, ap->a_cred);
410 * Check to see if the type is ok
411 * and that deletion is not in progress.
412 * For paged in text files, you will need to flush the page cache
413 * if consistency is lost.
417 nfs_open(struct vop_open_args *ap)
419 struct vnode *vp = ap->a_vp;
420 struct nfsnode *np = VTONFS(vp);
423 int fmode = ap->a_mode;
425 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
429 * Get a valid lease. If cached data is stale, flush it.
431 if (np->n_flag & NMODIFIED) {
432 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
433 if (error == EINTR || error == EIO)
436 if (vp->v_type == VDIR)
437 np->n_direofoffset = 0;
438 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
441 np->n_mtime = vattr.va_mtime;
444 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
447 if (NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
448 if (vp->v_type == VDIR)
449 np->n_direofoffset = 0;
450 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
451 if (error == EINTR || error == EIO)
453 np->n_mtime = vattr.va_mtime;
457 * If the object has >= 1 O_DIRECT active opens, we disable caching.
459 if (nfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
460 if (np->n_directio_opens == 0) {
461 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
464 np->n_flag |= NNONCACHE;
466 np->n_directio_opens++;
468 np->ra_expect_lbn = 0;
469 vnode_create_vobject_off(vp, vattr.va_size, ap->a_td);
475 * What an NFS client should do upon close after writing is a debatable issue.
476 * Most NFS clients push delayed writes to the server upon close, basically for
478 * 1 - So that any write errors may be reported back to the client process
479 * doing the close system call. By far the two most likely errors are
480 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
481 * 2 - To put a worst case upper bound on cache inconsistency between
482 * multiple clients for the file.
483 * There is also a consistency problem for Version 2 of the protocol w.r.t.
484 * not being able to tell if other clients are writing a file concurrently,
485 * since there is no way of knowing if the changed modify time in the reply
486 * is only due to the write for this client.
487 * (NFS Version 3 provides weak cache consistency data in the reply that
488 * should be sufficient to detect and handle this case.)
490 * The current code does the following:
491 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
492 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
493 * or commit them (this satisfies 1 and 2 except for the
494 * case where the server crashes after this close but
495 * before the commit RPC, which is felt to be "good
496 * enough". Changing the last argument to nfs_flush() to
497 * a 1 would force a commit operation, if it is felt a
498 * commit is necessary now.
502 nfs_close(struct vop_close_args *ap)
504 struct vnode *vp = ap->a_vp;
505 struct nfsnode *np = VTONFS(vp);
507 int fmode = ap->a_fflag;
509 if (vp->v_type == VREG) {
511 * Examine and clean dirty pages, regardless of NMODIFIED.
512 * This closes a major hole in close-to-open consistency.
513 * We want to push out all dirty pages (and buffers) on
514 * close, regardless of whether they were dirtied by
515 * mmap'ed writes or via write().
517 if (nfs_clean_pages_on_close && vp->v_object) {
518 VM_OBJECT_LOCK(vp->v_object);
519 vm_object_page_clean(vp->v_object, 0, 0, 0);
520 VM_OBJECT_UNLOCK(vp->v_object);
522 if (np->n_flag & NMODIFIED) {
525 * Under NFSv3 we have dirty buffers to dispose of. We
526 * must flush them to the NFS server. We have the option
527 * of waiting all the way through the commit rpc or just
528 * waiting for the initial write. The default is to only
529 * wait through the initial write so the data is in the
530 * server's cache, which is roughly similar to the state
531 * a standard disk subsystem leaves the file in on close().
533 * We cannot clear the NMODIFIED bit in np->n_flag due to
534 * potential races with other processes, and certainly
535 * cannot clear it if we don't commit.
537 int cm = nfsv3_commit_on_close ? 1 : 0;
538 error = nfs_flush(vp, MNT_WAIT, ap->a_td, cm);
539 /* np->n_flag &= ~NMODIFIED; */
541 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
544 * Invalidate the attribute cache in all cases.
545 * An open is going to fetch fresh attrs any way, other procs
546 * on this node that have file open will be forced to do an
547 * otw attr fetch, but this is safe.
550 if (np->n_flag & NWRITEERR) {
551 np->n_flag &= ~NWRITEERR;
555 if (nfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
556 KASSERT((np->n_directio_opens > 0),
557 ("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
558 np->n_directio_opens--;
559 if (np->n_directio_opens == 0)
560 np->n_flag &= ~NNONCACHE;
566 * nfs getattr call from vfs.
569 nfs_getattr(struct vop_getattr_args *ap)
571 struct vnode *vp = ap->a_vp;
572 struct nfsnode *np = VTONFS(vp);
575 struct mbuf *mreq, *mrep, *md, *mb;
576 int v3 = NFS_ISV3(vp);
579 * Update local times for special files.
581 if (np->n_flag & (NACC | NUPD))
584 * First look in the cache.
586 if (nfs_getattrcache(vp, ap->a_vap) == 0)
589 if (v3 && nfsaccess_cache_timeout > 0) {
590 nfsstats.accesscache_misses++;
591 nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_td, ap->a_cred);
592 if (nfs_getattrcache(vp, ap->a_vap) == 0)
596 nfsstats.rpccnt[NFSPROC_GETATTR]++;
597 mreq = nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
599 bpos = mtod(mb, caddr_t);
601 nfsm_request(vp, NFSPROC_GETATTR, ap->a_td, ap->a_cred);
603 nfsm_loadattr(vp, ap->a_vap);
614 nfs_setattr(struct vop_setattr_args *ap)
616 struct vnode *vp = ap->a_vp;
617 struct nfsnode *np = VTONFS(vp);
618 struct vattr *vap = ap->a_vap;
627 * Setting of flags is not supported.
629 if (vap->va_flags != VNOVAL)
633 * Disallow write attempts if the filesystem is mounted read-only.
635 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
636 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
637 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
638 (vp->v_mount->mnt_flag & MNT_RDONLY))
640 if (vap->va_size != VNOVAL) {
641 switch (vp->v_type) {
648 if (vap->va_mtime.tv_sec == VNOVAL &&
649 vap->va_atime.tv_sec == VNOVAL &&
650 vap->va_mode == (mode_t)VNOVAL &&
651 vap->va_uid == (uid_t)VNOVAL &&
652 vap->va_gid == (gid_t)VNOVAL)
654 vap->va_size = VNOVAL;
658 * Disallow write attempts if the filesystem is
661 if (vp->v_mount->mnt_flag & MNT_RDONLY)
665 * We run vnode_pager_setsize() early (why?),
666 * we must set np->n_size now to avoid vinvalbuf
667 * V_SAVE races that might setsize a lower
672 error = nfs_meta_setsize(vp, ap->a_cred,
673 ap->a_td, vap->va_size);
675 if (np->n_flag & NMODIFIED) {
676 if (vap->va_size == 0)
677 error = nfs_vinvalbuf(vp, 0, ap->a_td, 1);
679 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
681 vnode_pager_setsize(vp, np->n_size);
686 * np->n_size has already been set to vap->va_size
687 * in nfs_meta_setsize(). We must set it again since
688 * nfs_loadattrcache() could be called through
689 * nfs_meta_setsize() and could modify np->n_size.
691 np->n_vattr.va_size = np->n_size = vap->va_size;
693 } else if ((vap->va_mtime.tv_sec != VNOVAL ||
694 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NMODIFIED) &&
695 vp->v_type == VREG &&
696 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) != 0 &&
697 (error == EINTR || error == EIO))
699 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
700 if (error && vap->va_size != VNOVAL) {
701 np->n_size = np->n_vattr.va_size = tsize;
702 vnode_pager_setsize(vp, np->n_size);
708 * Do an nfs setattr rpc.
711 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
714 struct nfsv2_sattr *sp;
715 struct nfsnode *np = VTONFS(vp);
718 int error = 0, wccflag = NFSV3_WCCRATTR;
719 struct mbuf *mreq, *mrep, *md, *mb;
720 int v3 = NFS_ISV3(vp);
722 nfsstats.rpccnt[NFSPROC_SETATTR]++;
723 mreq = nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
725 bpos = mtod(mb, caddr_t);
728 nfsm_v3attrbuild(vap, TRUE);
729 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
732 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
733 if (vap->va_mode == (mode_t)VNOVAL)
734 sp->sa_mode = nfs_xdrneg1;
736 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
737 if (vap->va_uid == (uid_t)VNOVAL)
738 sp->sa_uid = nfs_xdrneg1;
740 sp->sa_uid = txdr_unsigned(vap->va_uid);
741 if (vap->va_gid == (gid_t)VNOVAL)
742 sp->sa_gid = nfs_xdrneg1;
744 sp->sa_gid = txdr_unsigned(vap->va_gid);
745 sp->sa_size = txdr_unsigned(vap->va_size);
746 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
747 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
749 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
752 nfsm_wcc_data(vp, wccflag);
754 nfsm_loadattr(vp, NULL);
761 * nfs lookup call, one step at a time...
762 * First look in cache
763 * If not found, unlock the directory nfsnode and do the rpc
766 nfs_lookup(struct vop_lookup_args *ap)
768 struct componentname *cnp = ap->a_cnp;
769 struct vnode *dvp = ap->a_dvp;
770 struct vnode **vpp = ap->a_vpp;
771 int flags = cnp->cn_flags;
773 struct nfsmount *nmp;
775 struct mbuf *mreq, *mrep, *md, *mb;
779 int error = 0, attrflag, fhsize;
780 int v3 = NFS_ISV3(dvp);
781 struct thread *td = cnp->cn_thread;
784 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
785 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
787 if (dvp->v_type != VDIR)
789 nmp = VFSTONFS(dvp->v_mount);
791 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
795 if ((error = cache_lookup(dvp, vpp, cnp)) && error != ENOENT) {
799 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, td)
800 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
801 nfsstats.lookupcache_hits++;
802 if (cnp->cn_nameiop != LOOKUP &&
804 cnp->cn_flags |= SAVENAME;
816 nfsstats.lookupcache_misses++;
817 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
818 len = cnp->cn_namelen;
819 mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP,
820 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
822 bpos = mtod(mb, caddr_t);
824 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
825 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_thread, cnp->cn_cred);
828 nfsm_postop_attr(dvp, attrflag);
833 nfsm_getfh(fhp, fhsize, v3);
836 * Handle RENAME case...
838 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
839 if (NFS_CMPFH(np, fhp, fhsize)) {
843 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
850 nfsm_postop_attr(newvp, attrflag);
851 nfsm_postop_attr(dvp, attrflag);
853 nfsm_loadattr(newvp, NULL);
856 cnp->cn_flags |= SAVENAME;
860 if (flags & ISDOTDOT) {
861 VOP_UNLOCK(dvp, 0, td);
862 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
863 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
867 } else if (NFS_CMPFH(np, fhp, fhsize)) {
871 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
879 nfsm_postop_attr(newvp, attrflag);
880 nfsm_postop_attr(dvp, attrflag);
882 nfsm_loadattr(newvp, NULL);
883 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
884 cnp->cn_flags |= SAVENAME;
885 if ((cnp->cn_flags & MAKEENTRY) &&
886 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
887 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
888 cache_enter(dvp, newvp, cnp);
894 if (newvp != NULLVP) {
898 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
899 (flags & ISLASTCN) && error == ENOENT) {
900 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
905 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
906 cnp->cn_flags |= SAVENAME;
913 * Just call nfs_bioread() to do the work.
916 nfs_read(struct vop_read_args *ap)
918 struct vnode *vp = ap->a_vp;
920 switch (vp->v_type) {
922 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
934 nfs_readlink(struct vop_readlink_args *ap)
936 struct vnode *vp = ap->a_vp;
938 if (vp->v_type != VLNK)
940 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred));
945 * Called by nfs_doio() from below the buffer cache.
948 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
951 int error = 0, len, attrflag;
952 struct mbuf *mreq, *mrep, *md, *mb;
953 int v3 = NFS_ISV3(vp);
955 nfsstats.rpccnt[NFSPROC_READLINK]++;
956 mreq = nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
958 bpos = mtod(mb, caddr_t);
960 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, cred);
962 nfsm_postop_attr(vp, attrflag);
964 nfsm_strsiz(len, NFS_MAXPATHLEN);
965 if (len == NFS_MAXPATHLEN) {
966 struct nfsnode *np = VTONFS(vp);
967 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
970 nfsm_mtouio(uiop, len);
982 nfs_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
986 struct mbuf *mreq, *mrep, *md, *mb;
987 struct nfsmount *nmp;
988 int error = 0, len, retlen, tsiz, eof, attrflag;
989 int v3 = NFS_ISV3(vp);
994 nmp = VFSTONFS(vp->v_mount);
995 tsiz = uiop->uio_resid;
996 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
999 nfsstats.rpccnt[NFSPROC_READ]++;
1000 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1001 mreq = nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1003 bpos = mtod(mb, caddr_t);
1005 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED * 3);
1007 txdr_hyper(uiop->uio_offset, tl);
1008 *(tl + 2) = txdr_unsigned(len);
1010 *tl++ = txdr_unsigned(uiop->uio_offset);
1011 *tl++ = txdr_unsigned(len);
1014 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, cred);
1016 nfsm_postop_attr(vp, attrflag);
1021 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
1022 eof = fxdr_unsigned(int, *(tl + 1));
1024 nfsm_loadattr(vp, NULL);
1025 nfsm_strsiz(retlen, nmp->nm_rsize);
1026 nfsm_mtouio(uiop, retlen);
1030 if (eof || retlen == 0) {
1033 } else if (retlen < len) {
1045 nfs_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1046 int *iomode, int *must_commit)
1051 struct mbuf *mreq, *mrep, *md, *mb;
1052 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1053 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1054 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1057 if (uiop->uio_iovcnt != 1)
1058 panic("nfs: writerpc iovcnt > 1");
1061 tsiz = uiop->uio_resid;
1062 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1065 nfsstats.rpccnt[NFSPROC_WRITE]++;
1066 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1067 mreq = nfsm_reqhead(vp, NFSPROC_WRITE,
1068 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1070 bpos = mtod(mb, caddr_t);
1073 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
1074 txdr_hyper(uiop->uio_offset, tl);
1076 *tl++ = txdr_unsigned(len);
1077 *tl++ = txdr_unsigned(*iomode);
1078 *tl = txdr_unsigned(len);
1082 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1083 /* Set both "begin" and "current" to non-garbage. */
1084 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1085 *tl++ = x; /* "begin offset" */
1086 *tl++ = x; /* "current offset" */
1087 x = txdr_unsigned(len);
1088 *tl++ = x; /* total to this offset */
1089 *tl = x; /* size of this write */
1091 nfsm_uiotom(uiop, len);
1092 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, cred);
1094 wccflag = NFSV3_WCCCHK;
1095 nfsm_wcc_data(vp, wccflag);
1097 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED
1098 + NFSX_V3WRITEVERF);
1099 rlen = fxdr_unsigned(int, *tl++);
1104 } else if (rlen < len) {
1105 backup = len - rlen;
1106 uiop->uio_iov->iov_base =
1107 (char *)uiop->uio_iov->iov_base -
1109 uiop->uio_iov->iov_len += backup;
1110 uiop->uio_offset -= backup;
1111 uiop->uio_resid += backup;
1114 commit = fxdr_unsigned(int, *tl++);
1117 * Return the lowest committment level
1118 * obtained by any of the RPCs.
1120 if (committed == NFSV3WRITE_FILESYNC)
1122 else if (committed == NFSV3WRITE_DATASYNC &&
1123 commit == NFSV3WRITE_UNSTABLE)
1125 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1126 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1128 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1129 } else if (bcmp((caddr_t)tl,
1130 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1132 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1137 nfsm_loadattr(vp, NULL);
1139 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime;
1146 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1147 committed = NFSV3WRITE_FILESYNC;
1148 *iomode = committed;
1150 uiop->uio_resid = tsiz;
1156 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1157 * mode set to specify the file type and the size field for rdev.
1160 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1163 struct nfsv2_sattr *sp;
1165 struct vnode *newvp = NULL;
1166 struct nfsnode *np = NULL;
1169 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1170 struct mbuf *mreq, *mrep, *md, *mb;
1172 int v3 = NFS_ISV3(dvp);
1174 if (vap->va_type == VCHR || vap->va_type == VBLK)
1175 rdev = txdr_unsigned(vap->va_rdev);
1176 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1179 return (EOPNOTSUPP);
1181 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1184 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1185 mreq = nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1186 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1188 bpos = mtod(mb, caddr_t);
1189 nfsm_fhtom(dvp, v3);
1190 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1192 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1193 *tl++ = vtonfsv3_type(vap->va_type);
1194 nfsm_v3attrbuild(vap, FALSE);
1195 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1196 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
1197 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1198 *tl = txdr_unsigned(uminor(vap->va_rdev));
1201 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1202 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1203 sp->sa_uid = nfs_xdrneg1;
1204 sp->sa_gid = nfs_xdrneg1;
1206 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1207 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1209 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_thread, cnp->cn_cred);
1211 nfsm_mtofh(dvp, newvp, v3, gotvp);
1217 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1218 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1224 nfsm_wcc_data(dvp, wccflag);
1231 if (cnp->cn_flags & MAKEENTRY)
1232 cache_enter(dvp, newvp, cnp);
1235 VTONFS(dvp)->n_flag |= NMODIFIED;
1237 VTONFS(dvp)->n_attrstamp = 0;
1243 * just call nfs_mknodrpc() to do the work.
1247 nfs_mknod(struct vop_mknod_args *ap)
1250 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1253 static u_long create_verf;
1255 * nfs file create call
1258 nfs_create(struct vop_create_args *ap)
1260 struct vnode *dvp = ap->a_dvp;
1261 struct vattr *vap = ap->a_vap;
1262 struct componentname *cnp = ap->a_cnp;
1263 struct nfsv2_sattr *sp;
1265 struct nfsnode *np = NULL;
1266 struct vnode *newvp = NULL;
1268 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1269 struct mbuf *mreq, *mrep, *md, *mb;
1271 int v3 = NFS_ISV3(dvp);
1274 * Oops, not for me..
1276 if (vap->va_type == VSOCK)
1277 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1279 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1282 if (vap->va_vaflags & VA_EXCLUSIVE)
1285 nfsstats.rpccnt[NFSPROC_CREATE]++;
1286 mreq = nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1287 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1289 bpos = mtod(mb, caddr_t);
1290 nfsm_fhtom(dvp, v3);
1291 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1293 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1294 if (fmode & O_EXCL) {
1295 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1296 tl = nfsm_build(u_int32_t *, NFSX_V3CREATEVERF);
1298 if (!TAILQ_EMPTY(&in_ifaddrhead))
1299 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr;
1302 *tl++ = create_verf;
1303 *tl = ++create_verf;
1305 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1306 nfsm_v3attrbuild(vap, FALSE);
1309 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1310 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1311 sp->sa_uid = nfs_xdrneg1;
1312 sp->sa_gid = nfs_xdrneg1;
1314 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1315 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1317 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_thread, cnp->cn_cred);
1319 nfsm_mtofh(dvp, newvp, v3, gotvp);
1325 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1326 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1332 nfsm_wcc_data(dvp, wccflag);
1336 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1342 } else if (v3 && (fmode & O_EXCL)) {
1344 * We are normally called with only a partially initialized
1345 * VAP. Since the NFSv3 spec says that server may use the
1346 * file attributes to store the verifier, the spec requires
1347 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1348 * in atime, but we can't really assume that all servers will
1349 * so we ensure that our SETATTR sets both atime and mtime.
1351 if (vap->va_mtime.tv_sec == VNOVAL)
1352 vfs_timestamp(&vap->va_mtime);
1353 if (vap->va_atime.tv_sec == VNOVAL)
1354 vap->va_atime = vap->va_mtime;
1355 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_thread);
1358 if (cnp->cn_flags & MAKEENTRY)
1359 cache_enter(dvp, newvp, cnp);
1362 VTONFS(dvp)->n_flag |= NMODIFIED;
1364 VTONFS(dvp)->n_attrstamp = 0;
1369 * nfs file remove call
1370 * To try and make nfs semantics closer to ufs semantics, a file that has
1371 * other processes using the vnode is renamed instead of removed and then
1372 * removed later on the last close.
1373 * - If v_usecount > 1
1374 * If a rename is not already in the works
1375 * call nfs_sillyrename() to set it up
1380 nfs_remove(struct vop_remove_args *ap)
1382 struct vnode *vp = ap->a_vp;
1383 struct vnode *dvp = ap->a_dvp;
1384 struct componentname *cnp = ap->a_cnp;
1385 struct nfsnode *np = VTONFS(vp);
1390 if ((cnp->cn_flags & HASBUF) == 0)
1391 panic("nfs_remove: no name");
1392 if (vrefcnt(vp) < 1)
1393 panic("nfs_remove: bad v_usecount");
1395 if (vp->v_type == VDIR)
1397 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1398 VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_thread) == 0 &&
1399 vattr.va_nlink > 1)) {
1401 * Purge the name cache so that the chance of a lookup for
1402 * the name succeeding while the remove is in progress is
1403 * minimized. Without node locking it can still happen, such
1404 * that an I/O op returns ESTALE, but since you get this if
1405 * another host removes the file..
1409 * throw away biocache buffers, mainly to avoid
1410 * unnecessary delayed writes later.
1412 error = nfs_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1414 if (error != EINTR && error != EIO)
1415 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1416 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1418 * Kludge City: If the first reply to the remove rpc is lost..
1419 * the reply to the retransmitted request will be ENOENT
1420 * since the file was in fact removed
1421 * Therefore, we cheat and return success.
1423 if (error == ENOENT)
1425 } else if (!np->n_sillyrename)
1426 error = nfs_sillyrename(dvp, vp, cnp);
1427 np->n_attrstamp = 0;
1432 * nfs file remove rpc called from nfs_inactive
1435 nfs_removeit(struct sillyrename *sp)
1439 * Make sure that the directory vnode is still valid.
1440 * XXX we should lock sp->s_dvp here.
1442 if (sp->s_dvp->v_type == VBAD)
1444 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1449 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1452 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1453 struct ucred *cred, struct thread *td)
1456 int error = 0, wccflag = NFSV3_WCCRATTR;
1457 struct mbuf *mreq, *mrep, *md, *mb;
1458 int v3 = NFS_ISV3(dvp);
1460 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1461 mreq = nfsm_reqhead(dvp, NFSPROC_REMOVE,
1462 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1464 bpos = mtod(mb, caddr_t);
1465 nfsm_fhtom(dvp, v3);
1466 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1467 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1469 nfsm_wcc_data(dvp, wccflag);
1472 VTONFS(dvp)->n_flag |= NMODIFIED;
1474 VTONFS(dvp)->n_attrstamp = 0;
1479 * nfs file rename call
1482 nfs_rename(struct vop_rename_args *ap)
1484 struct vnode *fvp = ap->a_fvp;
1485 struct vnode *tvp = ap->a_tvp;
1486 struct vnode *fdvp = ap->a_fdvp;
1487 struct vnode *tdvp = ap->a_tdvp;
1488 struct componentname *tcnp = ap->a_tcnp;
1489 struct componentname *fcnp = ap->a_fcnp;
1493 if ((tcnp->cn_flags & HASBUF) == 0 ||
1494 (fcnp->cn_flags & HASBUF) == 0)
1495 panic("nfs_rename: no name");
1497 /* Check for cross-device rename */
1498 if ((fvp->v_mount != tdvp->v_mount) ||
1499 (tvp && (fvp->v_mount != tvp->v_mount))) {
1505 printf("nfs_rename: fvp == tvp (can't happen)\n");
1509 if ((error = vn_lock(fvp, LK_EXCLUSIVE, fcnp->cn_thread)) != 0)
1513 * We have to flush B_DELWRI data prior to renaming
1514 * the file. If we don't, the delayed-write buffers
1515 * can be flushed out later after the file has gone stale
1516 * under NFSV3. NFSV2 does not have this problem because
1517 * ( as far as I can tell ) it flushes dirty buffers more
1520 * Skip the rename operation if the fsync fails, this can happen
1521 * due to the server's volume being full, when we pushed out data
1522 * that was written back to our cache earlier. Not checking for
1523 * this condition can result in potential (silent) data loss.
1525 error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1526 VOP_UNLOCK(fvp, 0, fcnp->cn_thread);
1528 error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1533 * If the tvp exists and is in use, sillyrename it before doing the
1534 * rename of the new file over it.
1535 * XXX Can't sillyrename a directory.
1537 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1538 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1543 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1544 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1547 if (fvp->v_type == VDIR) {
1548 if (tvp != NULL && tvp->v_type == VDIR)
1563 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1565 if (error == ENOENT)
1571 * nfs file rename rpc called from nfs_remove() above
1574 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1575 struct sillyrename *sp)
1578 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen, sdvp,
1579 sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_thread));
1583 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1586 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1587 struct vnode *tdvp, const char *tnameptr, int tnamelen, struct ucred *cred,
1591 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1592 struct mbuf *mreq, *mrep, *md, *mb;
1593 int v3 = NFS_ISV3(fdvp);
1595 nfsstats.rpccnt[NFSPROC_RENAME]++;
1596 mreq = nfsm_reqhead(fdvp, NFSPROC_RENAME,
1597 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1598 nfsm_rndup(tnamelen));
1600 bpos = mtod(mb, caddr_t);
1601 nfsm_fhtom(fdvp, v3);
1602 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1603 nfsm_fhtom(tdvp, v3);
1604 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1605 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1607 nfsm_wcc_data(fdvp, fwccflag);
1608 nfsm_wcc_data(tdvp, twccflag);
1612 VTONFS(fdvp)->n_flag |= NMODIFIED;
1613 VTONFS(tdvp)->n_flag |= NMODIFIED;
1615 VTONFS(fdvp)->n_attrstamp = 0;
1617 VTONFS(tdvp)->n_attrstamp = 0;
1622 * nfs hard link create call
1625 nfs_link(struct vop_link_args *ap)
1627 struct vnode *vp = ap->a_vp;
1628 struct vnode *tdvp = ap->a_tdvp;
1629 struct componentname *cnp = ap->a_cnp;
1631 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1632 struct mbuf *mreq, *mrep, *md, *mb;
1635 if (vp->v_mount != tdvp->v_mount) {
1640 * Push all writes to the server, so that the attribute cache
1641 * doesn't get "out of sync" with the server.
1642 * XXX There should be a better way!
1644 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1647 nfsstats.rpccnt[NFSPROC_LINK]++;
1648 mreq = nfsm_reqhead(vp, NFSPROC_LINK,
1649 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1651 bpos = mtod(mb, caddr_t);
1653 nfsm_fhtom(tdvp, v3);
1654 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1655 nfsm_request(vp, NFSPROC_LINK, cnp->cn_thread, cnp->cn_cred);
1657 nfsm_postop_attr(vp, attrflag);
1658 nfsm_wcc_data(tdvp, wccflag);
1662 VTONFS(tdvp)->n_flag |= NMODIFIED;
1664 VTONFS(vp)->n_attrstamp = 0;
1666 VTONFS(tdvp)->n_attrstamp = 0;
1668 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1670 if (error == EEXIST)
1676 * nfs symbolic link create call
1679 nfs_symlink(struct vop_symlink_args *ap)
1681 struct vnode *dvp = ap->a_dvp;
1682 struct vattr *vap = ap->a_vap;
1683 struct componentname *cnp = ap->a_cnp;
1684 struct nfsv2_sattr *sp;
1686 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1687 struct mbuf *mreq, *mrep, *md, *mb;
1688 struct vnode *newvp = NULL;
1689 int v3 = NFS_ISV3(dvp);
1691 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1692 slen = strlen(ap->a_target);
1693 mreq = nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1694 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1696 bpos = mtod(mb, caddr_t);
1697 nfsm_fhtom(dvp, v3);
1698 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1700 nfsm_v3attrbuild(vap, FALSE);
1702 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1704 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1705 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1706 sp->sa_uid = nfs_xdrneg1;
1707 sp->sa_gid = nfs_xdrneg1;
1708 sp->sa_size = nfs_xdrneg1;
1709 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1710 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1714 * Issue the NFS request and get the rpc response.
1716 * Only NFSv3 responses returning an error of 0 actually return
1717 * a file handle that can be converted into newvp without having
1718 * to do an extra lookup rpc.
1720 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_thread, cnp->cn_cred);
1723 nfsm_mtofh(dvp, newvp, v3, gotvp);
1724 nfsm_wcc_data(dvp, wccflag);
1728 * out code jumps -> here, mrep is also freed.
1735 * If we get an EEXIST error, silently convert it to no-error
1736 * in case of an NFS retry.
1738 if (error == EEXIST)
1742 * If we do not have (or no longer have) an error, and we could
1743 * not extract the newvp from the response due to the request being
1744 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1745 * to obtain a newvp to return.
1747 if (error == 0 && newvp == NULL) {
1748 struct nfsnode *np = NULL;
1750 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1751 cnp->cn_cred, cnp->cn_thread, &np);
1761 VTONFS(dvp)->n_flag |= NMODIFIED;
1763 VTONFS(dvp)->n_attrstamp = 0;
1771 nfs_mkdir(struct vop_mkdir_args *ap)
1773 struct vnode *dvp = ap->a_dvp;
1774 struct vattr *vap = ap->a_vap;
1775 struct componentname *cnp = ap->a_cnp;
1776 struct nfsv2_sattr *sp;
1778 struct nfsnode *np = NULL;
1779 struct vnode *newvp = NULL;
1781 int error = 0, wccflag = NFSV3_WCCRATTR;
1783 struct mbuf *mreq, *mrep, *md, *mb;
1785 int v3 = NFS_ISV3(dvp);
1787 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1790 len = cnp->cn_namelen;
1791 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1792 mreq = nfsm_reqhead(dvp, NFSPROC_MKDIR,
1793 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1795 bpos = mtod(mb, caddr_t);
1796 nfsm_fhtom(dvp, v3);
1797 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1799 nfsm_v3attrbuild(vap, FALSE);
1801 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1802 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1803 sp->sa_uid = nfs_xdrneg1;
1804 sp->sa_gid = nfs_xdrneg1;
1805 sp->sa_size = nfs_xdrneg1;
1806 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1807 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1809 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_thread, cnp->cn_cred);
1811 nfsm_mtofh(dvp, newvp, v3, gotvp);
1813 nfsm_wcc_data(dvp, wccflag);
1816 VTONFS(dvp)->n_flag |= NMODIFIED;
1818 VTONFS(dvp)->n_attrstamp = 0;
1820 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1821 * if we can succeed in looking up the directory.
1823 if (error == EEXIST || (!error && !gotvp)) {
1828 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
1829 cnp->cn_thread, &np);
1832 if (newvp->v_type != VDIR)
1845 * nfs remove directory call
1848 nfs_rmdir(struct vop_rmdir_args *ap)
1850 struct vnode *vp = ap->a_vp;
1851 struct vnode *dvp = ap->a_dvp;
1852 struct componentname *cnp = ap->a_cnp;
1854 int error = 0, wccflag = NFSV3_WCCRATTR;
1855 struct mbuf *mreq, *mrep, *md, *mb;
1856 int v3 = NFS_ISV3(dvp);
1860 nfsstats.rpccnt[NFSPROC_RMDIR]++;
1861 mreq = nfsm_reqhead(dvp, NFSPROC_RMDIR,
1862 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1864 bpos = mtod(mb, caddr_t);
1865 nfsm_fhtom(dvp, v3);
1866 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1867 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_thread, cnp->cn_cred);
1869 nfsm_wcc_data(dvp, wccflag);
1872 VTONFS(dvp)->n_flag |= NMODIFIED;
1874 VTONFS(dvp)->n_attrstamp = 0;
1878 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
1880 if (error == ENOENT)
1889 nfs_readdir(struct vop_readdir_args *ap)
1891 struct vnode *vp = ap->a_vp;
1892 struct nfsnode *np = VTONFS(vp);
1893 struct uio *uio = ap->a_uio;
1897 if (vp->v_type != VDIR)
1900 * First, check for hit on the EOF offset cache
1902 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
1903 (np->n_flag & NMODIFIED) == 0) {
1904 if (VOP_GETATTR(vp, &vattr, ap->a_cred, uio->uio_td) == 0 &&
1905 !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
1906 nfsstats.direofcache_hits++;
1912 * Call nfs_bioread() to do the real work.
1914 tresid = uio->uio_resid;
1915 error = nfs_bioread(vp, uio, 0, ap->a_cred);
1917 if (!error && uio->uio_resid == tresid)
1918 nfsstats.direofcache_misses++;
1924 * Called from below the buffer cache by nfs_doio().
1927 nfs_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1930 struct dirent *dp = NULL;
1935 struct mbuf *mreq, *mrep, *md, *mb;
1937 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1938 struct nfsnode *dnp = VTONFS(vp);
1940 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
1942 int v3 = NFS_ISV3(vp);
1945 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
1946 (uiop->uio_resid & (DIRBLKSIZ - 1)))
1947 panic("nfs readdirrpc bad uio");
1951 * If there is no cookie, assume directory was stale.
1953 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
1957 return (NFSERR_BAD_COOKIE);
1959 * Loop around doing readdir rpc's of size nm_readdirsize
1960 * truncated to a multiple of DIRBLKSIZ.
1961 * The stopping criteria is EOF or buffer full.
1963 while (more_dirs && bigenough) {
1964 nfsstats.rpccnt[NFSPROC_READDIR]++;
1965 mreq = nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
1968 bpos = mtod(mb, caddr_t);
1971 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
1972 *tl++ = cookie.nfsuquad[0];
1973 *tl++ = cookie.nfsuquad[1];
1974 *tl++ = dnp->n_cookieverf.nfsuquad[0];
1975 *tl++ = dnp->n_cookieverf.nfsuquad[1];
1977 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
1978 *tl++ = cookie.nfsuquad[0];
1980 *tl = txdr_unsigned(nmp->nm_readdirsize);
1981 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, cred);
1983 nfsm_postop_attr(vp, attrflag);
1985 tl = nfsm_dissect(u_int32_t *,
1987 dnp->n_cookieverf.nfsuquad[0] = *tl++;
1988 dnp->n_cookieverf.nfsuquad[1] = *tl;
1994 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1995 more_dirs = fxdr_unsigned(int, *tl);
1997 /* loop thru the dir entries, doctoring them to 4bsd form */
1998 while (more_dirs && bigenough) {
2000 tl = nfsm_dissect(u_int32_t *,
2002 fileno = fxdr_hyper(tl);
2003 len = fxdr_unsigned(int, *(tl + 2));
2005 tl = nfsm_dissect(u_int32_t *,
2007 fileno = fxdr_unsigned(u_quad_t, *tl++);
2008 len = fxdr_unsigned(int, *tl);
2010 if (len <= 0 || len > NFS_MAXNAMLEN) {
2015 tlen = nfsm_rndup(len);
2017 tlen += 4; /* To ensure null termination */
2018 left = DIRBLKSIZ - blksiz;
2019 if ((tlen + DIRHDSIZ) > left) {
2020 dp->d_reclen += left;
2021 uiop->uio_iov->iov_base =
2022 (char *)uiop->uio_iov->iov_base + left;
2023 uiop->uio_iov->iov_len -= left;
2024 uiop->uio_offset += left;
2025 uiop->uio_resid -= left;
2028 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2031 dp = (struct dirent *)uiop->uio_iov->iov_base;
2032 dp->d_fileno = (int)fileno;
2034 dp->d_reclen = tlen + DIRHDSIZ;
2035 dp->d_type = DT_UNKNOWN;
2036 blksiz += dp->d_reclen;
2037 if (blksiz == DIRBLKSIZ)
2039 uiop->uio_offset += DIRHDSIZ;
2040 uiop->uio_resid -= DIRHDSIZ;
2041 uiop->uio_iov->iov_base =
2042 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
2043 uiop->uio_iov->iov_len -= DIRHDSIZ;
2044 nfsm_mtouio(uiop, len);
2045 cp = uiop->uio_iov->iov_base;
2047 *cp = '\0'; /* null terminate */
2048 uiop->uio_iov->iov_base =
2049 (char *)uiop->uio_iov->iov_base + tlen;
2050 uiop->uio_iov->iov_len -= tlen;
2051 uiop->uio_offset += tlen;
2052 uiop->uio_resid -= tlen;
2054 nfsm_adv(nfsm_rndup(len));
2056 tl = nfsm_dissect(u_int32_t *,
2059 tl = nfsm_dissect(u_int32_t *,
2063 cookie.nfsuquad[0] = *tl++;
2065 cookie.nfsuquad[1] = *tl++;
2070 more_dirs = fxdr_unsigned(int, *tl);
2073 * If at end of rpc data, get the eof boolean
2076 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2077 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2082 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2083 * by increasing d_reclen for the last record.
2086 left = DIRBLKSIZ - blksiz;
2087 dp->d_reclen += left;
2088 uiop->uio_iov->iov_base =
2089 (char *)uiop->uio_iov->iov_base + left;
2090 uiop->uio_iov->iov_len -= left;
2091 uiop->uio_offset += left;
2092 uiop->uio_resid -= left;
2096 * We are now either at the end of the directory or have filled the
2100 dnp->n_direofoffset = uiop->uio_offset;
2102 if (uiop->uio_resid > 0)
2103 printf("EEK! readdirrpc resid > 0\n");
2104 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2112 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2115 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
2121 struct vnode *newvp;
2123 caddr_t bpos, dpos, dpossav1, dpossav2;
2124 struct mbuf *mreq, *mrep, *md, *mb, *mdsav1, *mdsav2;
2125 struct nameidata nami, *ndp = &nami;
2126 struct componentname *cnp = &ndp->ni_cnd;
2128 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2129 struct nfsnode *dnp = VTONFS(vp), *np;
2132 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2133 int attrflag, fhsize;
2139 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2140 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2141 panic("nfs readdirplusrpc bad uio");
2147 * If there is no cookie, assume directory was stale.
2149 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2153 return (NFSERR_BAD_COOKIE);
2155 * Loop around doing readdir rpc's of size nm_readdirsize
2156 * truncated to a multiple of DIRBLKSIZ.
2157 * The stopping criteria is EOF or buffer full.
2159 while (more_dirs && bigenough) {
2160 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2161 mreq = nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2162 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2164 bpos = mtod(mb, caddr_t);
2166 tl = nfsm_build(u_int32_t *, 6 * NFSX_UNSIGNED);
2167 *tl++ = cookie.nfsuquad[0];
2168 *tl++ = cookie.nfsuquad[1];
2169 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2170 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2171 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2172 *tl = txdr_unsigned(nmp->nm_rsize);
2173 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, cred);
2174 nfsm_postop_attr(vp, attrflag);
2179 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2180 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2181 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2182 more_dirs = fxdr_unsigned(int, *tl);
2184 /* loop thru the dir entries, doctoring them to 4bsd form */
2185 while (more_dirs && bigenough) {
2186 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2187 fileno = fxdr_hyper(tl);
2188 len = fxdr_unsigned(int, *(tl + 2));
2189 if (len <= 0 || len > NFS_MAXNAMLEN) {
2194 tlen = nfsm_rndup(len);
2196 tlen += 4; /* To ensure null termination*/
2197 left = DIRBLKSIZ - blksiz;
2198 if ((tlen + DIRHDSIZ) > left) {
2199 dp->d_reclen += left;
2200 uiop->uio_iov->iov_base =
2201 (char *)uiop->uio_iov->iov_base + left;
2202 uiop->uio_iov->iov_len -= left;
2203 uiop->uio_offset += left;
2204 uiop->uio_resid -= left;
2207 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2210 dp = (struct dirent *)uiop->uio_iov->iov_base;
2211 dp->d_fileno = (int)fileno;
2213 dp->d_reclen = tlen + DIRHDSIZ;
2214 dp->d_type = DT_UNKNOWN;
2215 blksiz += dp->d_reclen;
2216 if (blksiz == DIRBLKSIZ)
2218 uiop->uio_offset += DIRHDSIZ;
2219 uiop->uio_resid -= DIRHDSIZ;
2220 uiop->uio_iov->iov_base =
2221 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
2222 uiop->uio_iov->iov_len -= DIRHDSIZ;
2223 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2224 cnp->cn_namelen = len;
2225 nfsm_mtouio(uiop, len);
2226 cp = uiop->uio_iov->iov_base;
2229 uiop->uio_iov->iov_base =
2230 (char *)uiop->uio_iov->iov_base + tlen;
2231 uiop->uio_iov->iov_len -= tlen;
2232 uiop->uio_offset += tlen;
2233 uiop->uio_resid -= tlen;
2235 nfsm_adv(nfsm_rndup(len));
2236 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2238 cookie.nfsuquad[0] = *tl++;
2239 cookie.nfsuquad[1] = *tl++;
2244 * Since the attributes are before the file handle
2245 * (sigh), we must skip over the attributes and then
2246 * come back and get them.
2248 attrflag = fxdr_unsigned(int, *tl);
2252 nfsm_adv(NFSX_V3FATTR);
2253 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2254 doit = fxdr_unsigned(int, *tl);
2256 * Skip loading the attrs for "..". There's a
2257 * race between loading the attrs here and
2258 * lookups that look for the directory currently
2259 * being read (in the parent). We try to acquire
2260 * the exclusive lock on ".." here, owning the
2261 * lock on the directory being read. Lookup will
2262 * hold the lock on ".." and try to acquire the
2263 * lock on the directory being read.
2265 * There are other ways of fixing this, one would
2266 * be to do a trylock on the ".." vnode and skip
2267 * loading the attrs on ".." if it happens to be
2268 * locked by another process. But skipping the
2269 * attrload on ".." seems the easiest option.
2271 if (strcmp(dp->d_name, "..") == 0) {
2274 * We've already skipped over the attrs,
2275 * skip over the filehandle. And store d_type
2278 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2279 i = fxdr_unsigned(int, *tl);
2280 nfsm_adv(nfsm_rndup(i));
2281 dp->d_type = IFTODT(VTTOIF(VDIR));
2284 nfsm_getfh(fhp, fhsize, 1);
2285 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2290 error = nfs_nget(vp->v_mount, fhp,
2298 if (doit && bigenough) {
2303 nfsm_loadattr(newvp, NULL);
2307 IFTODT(VTTOIF(np->n_vattr.va_type));
2309 cache_enter(ndp->ni_dvp, ndp->ni_vp, cnp);
2312 /* Just skip over the file handle */
2313 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2314 i = fxdr_unsigned(int, *tl);
2316 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2317 fhsize = fxdr_unsigned(int, *tl);
2318 nfsm_adv(nfsm_rndup(fhsize));
2321 if (newvp != NULLVP) {
2328 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2329 more_dirs = fxdr_unsigned(int, *tl);
2332 * If at end of rpc data, get the eof boolean
2335 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2336 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2341 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2342 * by increasing d_reclen for the last record.
2345 left = DIRBLKSIZ - blksiz;
2346 dp->d_reclen += left;
2347 uiop->uio_iov->iov_base =
2348 (char *)uiop->uio_iov->iov_base + left;
2349 uiop->uio_iov->iov_len -= left;
2350 uiop->uio_offset += left;
2351 uiop->uio_resid -= left;
2355 * We are now either at the end of the directory or have filled the
2359 dnp->n_direofoffset = uiop->uio_offset;
2361 if (uiop->uio_resid > 0)
2362 printf("EEK! readdirplusrpc resid > 0\n");
2363 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2367 if (newvp != NULLVP) {
2378 * Silly rename. To make the NFS filesystem that is stateless look a little
2379 * more like the "ufs" a remove of an active vnode is translated to a rename
2380 * to a funny looking filename that is removed by nfs_inactive on the
2381 * nfsnode. There is the potential for another process on a different client
2382 * to create the same funny name between the nfs_lookitup() fails and the
2383 * nfs_rename() completes, but...
2386 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2388 struct sillyrename *sp;
2392 unsigned int lticks;
2397 if (vp->v_type == VDIR)
2398 panic("nfs: sillyrename dir");
2400 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2401 M_NFSREQ, M_WAITOK);
2402 sp->s_cred = crhold(cnp->cn_cred);
2404 sp->s_removeit = nfs_removeit;
2408 * Fudge together a funny name.
2409 * Changing the format of the funny name to accomodate more
2410 * sillynames per directory.
2411 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2412 * CPU ticks since boot.
2414 pid = cnp->cn_thread->td_proc->p_pid;
2415 lticks = (unsigned int)ticks;
2417 sp->s_namlen = sprintf(sp->s_name,
2418 ".nfs.%08x.%04x4.4", lticks,
2420 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2421 cnp->cn_thread, NULL))
2425 error = nfs_renameit(dvp, cnp, sp);
2428 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2429 cnp->cn_thread, &np);
2430 np->n_sillyrename = sp;
2435 free((caddr_t)sp, M_NFSREQ);
2440 * Look up a file name and optionally either update the file handle or
2441 * allocate an nfsnode, depending on the value of npp.
2442 * npp == NULL --> just do the lookup
2443 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2445 * *npp != NULL --> update the file handle in the vnode
2448 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2449 struct thread *td, struct nfsnode **npp)
2451 struct vnode *newvp = NULL;
2452 struct nfsnode *np, *dnp = VTONFS(dvp);
2454 int error = 0, fhlen, attrflag;
2455 struct mbuf *mreq, *mrep, *md, *mb;
2457 int v3 = NFS_ISV3(dvp);
2459 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2460 mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2461 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2463 bpos = mtod(mb, caddr_t);
2464 nfsm_fhtom(dvp, v3);
2465 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2466 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2467 if (npp && !error) {
2468 nfsm_getfh(nfhp, fhlen, v3);
2471 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2472 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2473 np->n_fhp = &np->n_fh;
2474 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2475 np->n_fhp =(nfsfh_t *)malloc(fhlen, M_NFSBIGFH, M_WAITOK);
2476 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2477 np->n_fhsize = fhlen;
2479 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2483 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2491 nfsm_postop_attr(newvp, attrflag);
2492 if (!attrflag && *npp == NULL) {
2501 nfsm_loadattr(newvp, NULL);
2505 if (npp && *npp == NULL) {
2520 * Nfs Version 3 commit rpc
2523 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2527 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2529 int error = 0, wccflag = NFSV3_WCCRATTR;
2530 struct mbuf *mreq, *mrep, *md, *mb;
2532 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2534 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2535 mreq = nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2537 bpos = mtod(mb, caddr_t);
2539 tl = nfsm_build(u_int32_t *, 3 * NFSX_UNSIGNED);
2540 txdr_hyper(offset, tl);
2542 *tl = txdr_unsigned(cnt);
2543 nfsm_request(vp, NFSPROC_COMMIT, td, cred);
2544 nfsm_wcc_data(vp, wccflag);
2546 tl = nfsm_dissect(u_int32_t *, NFSX_V3WRITEVERF);
2547 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2548 NFSX_V3WRITEVERF)) {
2549 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2551 error = NFSERR_STALEWRITEVERF;
2561 * For async requests when nfsiod(s) are running, queue the request by
2562 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2566 nfs_strategy(struct vop_strategy_args *ap)
2568 struct buf *bp = ap->a_bp;
2571 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2572 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2574 if (bp->b_iocmd == BIO_READ)
2580 * If the op is asynchronous and an i/o daemon is waiting
2581 * queue the request, wake it up and wait for completion
2582 * otherwise just do it ourselves.
2584 if ((bp->b_flags & B_ASYNC) == 0 ||
2585 nfs_asyncio(VFSTONFS(ap->a_vp->v_mount), bp, NOCRED, curthread))
2586 (void)nfs_doio(ap->a_vp, bp, cr, curthread);
2591 * fsync vnode op. Just call nfs_flush() with commit == 1.
2595 nfs_fsync(struct vop_fsync_args *ap)
2598 return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2602 * Flush all the blocks associated with a vnode.
2603 * Walk through the buffer pool and push any dirty pages
2604 * associated with the vnode.
2607 nfs_flush(struct vnode *vp, int waitfor, struct thread *td,
2610 struct nfsnode *np = VTONFS(vp);
2614 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2615 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2617 u_quad_t off, endoff, toff;
2618 struct ucred* wcred = NULL;
2619 struct buf **bvec = NULL;
2620 #ifndef NFS_COMMITBVECSIZ
2621 #define NFS_COMMITBVECSIZ 20
2623 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2624 int bvecsize = 0, bveccount;
2626 if (nmp->nm_flag & NFSMNT_INT)
2631 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2632 * server, but has not been committed to stable storage on the server
2633 * yet. On the first pass, the byte range is worked out and the commit
2634 * rpc is done. On the second pass, nfs_writebp() is called to do the
2641 if (NFS_ISV3(vp) && commit) {
2643 if (bvec != NULL && bvec != bvec_on_stack)
2646 * Count up how many buffers waiting for a commit.
2650 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
2651 if (BUF_REFCNT(bp) == 0 &&
2652 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2653 == (B_DELWRI | B_NEEDCOMMIT))
2657 * Allocate space to remember the list of bufs to commit. It is
2658 * important to use M_NOWAIT here to avoid a race with nfs_write.
2659 * If we can't get memory (for whatever reason), we will end up
2660 * committing the buffers one-by-one in the loop below.
2662 if (bveccount > NFS_COMMITBVECSIZ) {
2664 * Release the vnode interlock to avoid a lock
2668 bvec = (struct buf **)
2669 malloc(bveccount * sizeof(struct buf *),
2673 bvec = bvec_on_stack;
2674 bvecsize = NFS_COMMITBVECSIZ;
2676 bvecsize = bveccount;
2678 bvec = bvec_on_stack;
2679 bvecsize = NFS_COMMITBVECSIZ;
2681 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
2682 if (bvecpos >= bvecsize)
2684 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2685 nbp = TAILQ_NEXT(bp, b_bobufs);
2688 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2689 (B_DELWRI | B_NEEDCOMMIT)) {
2691 nbp = TAILQ_NEXT(bp, b_bobufs);
2697 * Work out if all buffers are using the same cred
2698 * so we can deal with them all with one commit.
2700 * NOTE: we are not clearing B_DONE here, so we have
2701 * to do it later on in this routine if we intend to
2702 * initiate I/O on the bp.
2704 * Note: to avoid loopback deadlocks, we do not
2705 * assign b_runningbufspace.
2708 wcred = bp->b_wcred;
2709 else if (wcred != bp->b_wcred)
2711 vfs_busy_pages(bp, 1);
2715 * bp is protected by being locked, but nbp is not
2716 * and vfs_busy_pages() may sleep. We have to
2719 nbp = TAILQ_NEXT(bp, b_bobufs);
2722 * A list of these buffers is kept so that the
2723 * second loop knows which buffers have actually
2724 * been committed. This is necessary, since there
2725 * may be a race between the commit rpc and new
2726 * uncommitted writes on the file.
2728 bvec[bvecpos++] = bp;
2729 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2733 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2742 * Commit data on the server, as required.
2743 * If all bufs are using the same wcred, then use that with
2744 * one call for all of them, otherwise commit each one
2747 if (wcred != NOCRED)
2748 retv = nfs_commit(vp, off, (int)(endoff - off),
2752 for (i = 0; i < bvecpos; i++) {
2755 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2757 size = (u_quad_t)(bp->b_dirtyend
2759 retv = nfs_commit(vp, off, (int)size,
2765 if (retv == NFSERR_STALEWRITEVERF)
2766 nfs_clearcommit(vp->v_mount);
2769 * Now, either mark the blocks I/O done or mark the
2770 * blocks dirty, depending on whether the commit
2773 for (i = 0; i < bvecpos; i++) {
2775 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2778 * Error, leave B_DELWRI intact
2780 vfs_unbusy_pages(bp);
2784 * Success, remove B_DELWRI ( bundirty() ).
2786 * b_dirtyoff/b_dirtyend seem to be NFS
2787 * specific. We should probably move that
2788 * into bundirty(). XXX
2791 bufobj_wref(&vp->v_bufobj);
2792 bp->b_flags |= B_ASYNC;
2794 bp->b_flags &= ~B_DONE;
2795 bp->b_ioflags &= ~BIO_ERROR;
2796 bp->b_dirtyoff = bp->b_dirtyend = 0;
2804 * Start/do any write(s) that are required.
2809 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
2810 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2811 if (waitfor != MNT_WAIT || passone)
2814 error = BUF_TIMELOCK(bp,
2815 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2816 VI_MTX(vp), "nfsfsync", slpflag, slptimeo);
2819 panic("nfs_fsync: inconsistent lock");
2820 if (error == ENOLCK)
2822 if (nfs_sigintr(nmp, NULL, td)) {
2826 if (slpflag == PCATCH) {
2832 if ((bp->b_flags & B_DELWRI) == 0)
2833 panic("nfs_fsync: not dirty");
2834 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2840 if (passone || !commit)
2841 bp->b_flags |= B_ASYNC;
2843 bp->b_flags |= B_ASYNC;
2846 if (nfs_sigintr(nmp, NULL, td)) {
2858 if (waitfor == MNT_WAIT) {
2859 while (vp->v_bufobj.bo_numoutput) {
2860 error = bufobj_wwait(&vp->v_bufobj, slpflag, slptimeo);
2863 error = nfs_sigintr(nmp, NULL, td);
2866 if (slpflag == PCATCH) {
2873 if (vp->v_bufobj.bo_dirty.bv_cnt != 0 && commit) {
2879 if (np->n_flag & NWRITEERR) {
2880 error = np->n_error;
2881 np->n_flag &= ~NWRITEERR;
2883 if (commit && vp->v_bufobj.bo_dirty.bv_cnt == 0)
2884 np->n_flag &= ~NMODIFIED;
2886 if (bvec != NULL && bvec != bvec_on_stack)
2892 * NFS advisory byte-level locks.
2895 nfs_advlock(struct vop_advlock_args *ap)
2898 if ((VFSTONFS(ap->a_vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
2899 struct nfsnode *np = VTONFS(ap->a_vp);
2901 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
2903 return (nfs_dolock(ap));
2907 * Print out the contents of an nfsnode.
2910 nfs_print(struct vop_print_args *ap)
2912 struct vnode *vp = ap->a_vp;
2913 struct nfsnode *np = VTONFS(vp);
2915 printf("\tfileid %ld fsid 0x%x",
2916 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
2917 if (vp->v_type == VFIFO)
2924 * This is the "real" nfs::bwrite(struct buf*).
2925 * We set B_CACHE if this is a VMIO buffer.
2928 nfs_writebp(struct buf *bp, int force __unused, struct thread *td)
2931 int oldflags = bp->b_flags;
2937 if (BUF_REFCNT(bp) == 0)
2938 panic("bwrite: buffer is not locked???");
2940 if (bp->b_flags & B_INVAL) {
2945 bp->b_flags |= B_CACHE;
2948 * Undirty the bp. We will redirty it later if the I/O fails.
2953 bp->b_flags &= ~B_DONE;
2954 bp->b_ioflags &= ~BIO_ERROR;
2955 bp->b_iocmd = BIO_WRITE;
2957 bufobj_wref(bp->b_bufobj);
2958 curthread->td_proc->p_stats->p_ru.ru_oublock++;
2962 * Note: to avoid loopback deadlocks, we do not
2963 * assign b_runningbufspace.
2965 vfs_busy_pages(bp, 1);
2968 bp->b_iooffset = dbtob(bp->b_blkno);
2971 if( (oldflags & B_ASYNC) == 0) {
2972 int rtval = bufwait(bp);
2974 if (oldflags & B_DELWRI) {
2988 * nfs special file access vnode op.
2989 * Essentially just get vattr and then imitate iaccess() since the device is
2990 * local to the client.
2993 nfsspec_access(struct vop_access_args *ap)
2996 struct ucred *cred = ap->a_cred;
2997 struct vnode *vp = ap->a_vp;
2998 mode_t mode = ap->a_mode;
3003 * Disallow write attempts on filesystems mounted read-only;
3004 * unless the file is a socket, fifo, or a block or character
3005 * device resident on the filesystem.
3007 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3008 switch (vp->v_type) {
3018 error = VOP_GETATTR(vp, vap, cred, ap->a_td);
3021 return (vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3026 * Read wrapper for fifos.
3029 nfsfifo_read(struct vop_read_args *ap)
3031 struct nfsnode *np = VTONFS(ap->a_vp);
3037 getnanotime(&np->n_atim);
3038 return (fifo_specops.vop_read(ap));
3042 * Write wrapper for fifos.
3045 nfsfifo_write(struct vop_write_args *ap)
3047 struct nfsnode *np = VTONFS(ap->a_vp);
3053 getnanotime(&np->n_mtim);
3054 return (fifo_specops.vop_write(ap));
3058 * Close wrapper for fifos.
3060 * Update the times on the nfsnode then do fifo close.
3063 nfsfifo_close(struct vop_close_args *ap)
3065 struct vnode *vp = ap->a_vp;
3066 struct nfsnode *np = VTONFS(vp);
3070 if (np->n_flag & (NACC | NUPD)) {
3072 if (np->n_flag & NACC)
3074 if (np->n_flag & NUPD)
3077 if (vrefcnt(vp) == 1 &&
3078 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3080 if (np->n_flag & NACC)
3081 vattr.va_atime = np->n_atim;
3082 if (np->n_flag & NUPD)
3083 vattr.va_mtime = np->n_mtim;
3084 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_td);
3087 return (fifo_specops.vop_close(ap));
3091 * Just call nfs_writebp() with the force argument set to 1.
3093 * NOTE: B_DONE may or may not be set in a_bp on call.
3096 nfs_bwrite(struct buf *bp)
3099 return (nfs_writebp(bp, 1, curthread));
3102 struct buf_ops buf_ops_nfs = {
3103 .bop_name = "buf_ops_nfs",
3104 .bop_write = nfs_bwrite,
3105 .bop_strategy = bufstrategy,
3106 .bop_sync = bufsync,