2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 * vnode op calls for Sun NFS version 2 and 3
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/resourcevar.h>
53 #include <sys/mount.h>
56 #include <sys/malloc.h>
58 #include <sys/namei.h>
59 #include <sys/socket.h>
60 #include <sys/vnode.h>
61 #include <sys/dirent.h>
62 #include <sys/fcntl.h>
63 #include <sys/lockf.h>
65 #include <sys/sysctl.h>
68 #include <vm/vm_extern.h>
70 #include <fs/fifofs/fifo.h>
72 #include <nfs/rpcv2.h>
73 #include <nfs/nfsproto.h>
74 #include <nfsclient/nfs.h>
75 #include <nfsclient/nfsnode.h>
76 #include <nfsclient/nfsmount.h>
77 #include <nfsclient/nfs_lock.h>
78 #include <nfs/xdr_subs.h>
79 #include <nfsclient/nfsm_subs.h>
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
90 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
91 * calls are not in getblk() and brelse() so that they would not be necessary
95 #define vfs_busy_pages(bp, f)
98 static int nfsspec_read(struct vop_read_args *);
99 static int nfsspec_write(struct vop_write_args *);
100 static int nfsfifo_read(struct vop_read_args *);
101 static int nfsfifo_write(struct vop_write_args *);
102 static int nfsspec_close(struct vop_close_args *);
103 static int nfsfifo_close(struct vop_close_args *);
104 static int nfs_flush(struct vnode *, struct ucred *, int, struct thread *,
106 static int nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
108 static int nfs_lookup(struct vop_lookup_args *);
109 static int nfs_create(struct vop_create_args *);
110 static int nfs_mknod(struct vop_mknod_args *);
111 static int nfs_open(struct vop_open_args *);
112 static int nfs_close(struct vop_close_args *);
113 static int nfs_access(struct vop_access_args *);
114 static int nfs_getattr(struct vop_getattr_args *);
115 static int nfs_setattr(struct vop_setattr_args *);
116 static int nfs_read(struct vop_read_args *);
117 static int nfs_fsync(struct vop_fsync_args *);
118 static int nfs_remove(struct vop_remove_args *);
119 static int nfs_link(struct vop_link_args *);
120 static int nfs_rename(struct vop_rename_args *);
121 static int nfs_mkdir(struct vop_mkdir_args *);
122 static int nfs_rmdir(struct vop_rmdir_args *);
123 static int nfs_symlink(struct vop_symlink_args *);
124 static int nfs_readdir(struct vop_readdir_args *);
125 static int nfs_strategy(struct vop_strategy_args *);
126 static int nfs_lookitup(struct vnode *, const char *, int,
127 struct ucred *, struct thread *, struct nfsnode **);
128 static int nfs_sillyrename(struct vnode *, struct vnode *,
129 struct componentname *);
130 static int nfsspec_access(struct vop_access_args *);
131 static int nfs_readlink(struct vop_readlink_args *);
132 static int nfs_print(struct vop_print_args *);
133 static int nfs_advlock(struct vop_advlock_args *);
136 * Global vfs data structures for nfs
138 vop_t **nfsv2_vnodeop_p;
139 static struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
140 { &vop_default_desc, (vop_t *) vop_defaultop },
141 { &vop_access_desc, (vop_t *) nfs_access },
142 { &vop_advlock_desc, (vop_t *) nfs_advlock },
143 { &vop_close_desc, (vop_t *) nfs_close },
144 { &vop_create_desc, (vop_t *) nfs_create },
145 { &vop_fsync_desc, (vop_t *) nfs_fsync },
146 { &vop_getattr_desc, (vop_t *) nfs_getattr },
147 { &vop_getpages_desc, (vop_t *) nfs_getpages },
148 { &vop_putpages_desc, (vop_t *) nfs_putpages },
149 { &vop_inactive_desc, (vop_t *) nfs_inactive },
150 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
151 { &vop_lease_desc, (vop_t *) vop_null },
152 { &vop_link_desc, (vop_t *) nfs_link },
153 { &vop_lock_desc, (vop_t *) vop_sharedlock },
154 { &vop_lookup_desc, (vop_t *) nfs_lookup },
155 { &vop_mkdir_desc, (vop_t *) nfs_mkdir },
156 { &vop_mknod_desc, (vop_t *) nfs_mknod },
157 { &vop_open_desc, (vop_t *) nfs_open },
158 { &vop_print_desc, (vop_t *) nfs_print },
159 { &vop_read_desc, (vop_t *) nfs_read },
160 { &vop_readdir_desc, (vop_t *) nfs_readdir },
161 { &vop_readlink_desc, (vop_t *) nfs_readlink },
162 { &vop_reclaim_desc, (vop_t *) nfs_reclaim },
163 { &vop_remove_desc, (vop_t *) nfs_remove },
164 { &vop_rename_desc, (vop_t *) nfs_rename },
165 { &vop_rmdir_desc, (vop_t *) nfs_rmdir },
166 { &vop_setattr_desc, (vop_t *) nfs_setattr },
167 { &vop_strategy_desc, (vop_t *) nfs_strategy },
168 { &vop_symlink_desc, (vop_t *) nfs_symlink },
169 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
170 { &vop_write_desc, (vop_t *) nfs_write },
173 static struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
174 { &nfsv2_vnodeop_p, nfsv2_vnodeop_entries };
175 VNODEOP_SET(nfsv2_vnodeop_opv_desc);
178 * Special device vnode ops
180 vop_t **spec_nfsv2nodeop_p;
181 static struct vnodeopv_entry_desc nfsv2_specop_entries[] = {
182 { &vop_default_desc, (vop_t *) spec_vnoperate },
183 { &vop_access_desc, (vop_t *) nfsspec_access },
184 { &vop_close_desc, (vop_t *) nfsspec_close },
185 { &vop_fsync_desc, (vop_t *) nfs_fsync },
186 { &vop_getattr_desc, (vop_t *) nfs_getattr },
187 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
188 { &vop_inactive_desc, (vop_t *) nfs_inactive },
189 { &vop_lock_desc, (vop_t *) vop_sharedlock },
190 { &vop_print_desc, (vop_t *) nfs_print },
191 { &vop_read_desc, (vop_t *) nfsspec_read },
192 { &vop_reclaim_desc, (vop_t *) nfs_reclaim },
193 { &vop_setattr_desc, (vop_t *) nfs_setattr },
194 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
195 { &vop_write_desc, (vop_t *) nfsspec_write },
198 static struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
199 { &spec_nfsv2nodeop_p, nfsv2_specop_entries };
200 VNODEOP_SET(spec_nfsv2nodeop_opv_desc);
202 vop_t **fifo_nfsv2nodeop_p;
203 static struct vnodeopv_entry_desc nfsv2_fifoop_entries[] = {
204 { &vop_default_desc, (vop_t *) fifo_vnoperate },
205 { &vop_access_desc, (vop_t *) nfsspec_access },
206 { &vop_close_desc, (vop_t *) nfsfifo_close },
207 { &vop_fsync_desc, (vop_t *) nfs_fsync },
208 { &vop_getattr_desc, (vop_t *) nfs_getattr },
209 { &vop_inactive_desc, (vop_t *) nfs_inactive },
210 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
211 { &vop_lock_desc, (vop_t *) vop_sharedlock },
212 { &vop_print_desc, (vop_t *) nfs_print },
213 { &vop_read_desc, (vop_t *) nfsfifo_read },
214 { &vop_reclaim_desc, (vop_t *) nfs_reclaim },
215 { &vop_setattr_desc, (vop_t *) nfs_setattr },
216 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
217 { &vop_write_desc, (vop_t *) nfsfifo_write },
220 static struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
221 { &fifo_nfsv2nodeop_p, nfsv2_fifoop_entries };
222 VNODEOP_SET(fifo_nfsv2nodeop_opv_desc);
224 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
225 struct componentname *cnp, struct vattr *vap);
226 static int nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
227 struct ucred *cred, struct thread *td);
228 static int nfs_renamerpc(struct vnode *fdvp, const char *fnameptr,
229 int fnamelen, struct vnode *tdvp,
230 const char *tnameptr, int tnamelen,
231 struct ucred *cred, struct thread *td);
232 static int nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
233 struct sillyrename *sp);
238 struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
239 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
240 int nfs_numasync = 0;
241 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
243 SYSCTL_DECL(_vfs_nfs);
245 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
246 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
247 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
249 static int nfsv3_commit_on_close = 0;
250 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
251 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
253 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
254 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
256 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
257 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
260 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
261 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
262 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
264 nfs3_access_otw(struct vnode *vp, int wmode, struct thread *td,
269 int error = 0, attrflag;
271 struct mbuf *mreq, *mrep, *md, *mb;
274 struct nfsnode *np = VTONFS(vp);
276 nfsstats.rpccnt[NFSPROC_ACCESS]++;
277 mreq = nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
279 bpos = mtod(mb, caddr_t);
281 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
282 *tl = txdr_unsigned(wmode);
283 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
284 nfsm_postop_attr(vp, attrflag);
286 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
287 rmode = fxdr_unsigned(u_int32_t, *tl);
289 np->n_modeuid = cred->cr_uid;
290 np->n_modestamp = time_second;
298 * nfs access vnode op.
299 * For nfs version 2, just return ok. File accesses may fail later.
300 * For nfs version 3, use the access rpc to check accessibility. If file modes
301 * are changed on the server, accesses might still fail later.
304 nfs_access(struct vop_access_args *ap)
306 struct vnode *vp = ap->a_vp;
308 u_int32_t mode, wmode;
309 int v3 = NFS_ISV3(vp);
310 struct nfsnode *np = VTONFS(vp);
313 * Disallow write attempts on filesystems mounted read-only;
314 * unless the file is a socket, fifo, or a block or character
315 * device resident on the filesystem.
317 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
318 switch (vp->v_type) {
328 * For nfs v3, check to see if we have done this recently, and if
329 * so return our cached result instead of making an ACCESS call.
330 * If not, do an access rpc, otherwise you are stuck emulating
331 * ufs_access() locally using the vattr. This may not be correct,
332 * since the server may apply other access criteria such as
333 * client uid-->server uid mapping that we do not know about.
336 if (ap->a_mode & VREAD)
337 mode = NFSV3ACCESS_READ;
340 if (vp->v_type != VDIR) {
341 if (ap->a_mode & VWRITE)
342 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
343 if (ap->a_mode & VEXEC)
344 mode |= NFSV3ACCESS_EXECUTE;
346 if (ap->a_mode & VWRITE)
347 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
349 if (ap->a_mode & VEXEC)
350 mode |= NFSV3ACCESS_LOOKUP;
352 /* XXX safety belt, only make blanket request if caching */
353 if (nfsaccess_cache_timeout > 0) {
354 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
355 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
356 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
362 * Does our cached result allow us to give a definite yes to
365 if ((time_second < (np->n_modestamp + nfsaccess_cache_timeout)) &&
366 (ap->a_cred->cr_uid == np->n_modeuid) &&
367 ((np->n_mode & mode) == mode)) {
368 nfsstats.accesscache_hits++;
371 * Either a no, or a don't know. Go to the wire.
373 nfsstats.accesscache_misses++;
374 error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred);
376 if ((np->n_mode & mode) != mode) {
383 if ((error = nfsspec_access(ap)) != 0)
387 * Attempt to prevent a mapped root from accessing a file
388 * which it shouldn't. We try to read a byte from the file
389 * if the user is root and the file is not zero length.
390 * After calling nfsspec_access, we should have the correct
393 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
394 && VTONFS(vp)->n_size > 0) {
401 auio.uio_iov = &aiov;
405 auio.uio_segflg = UIO_SYSSPACE;
406 auio.uio_rw = UIO_READ;
407 auio.uio_td = ap->a_td;
409 if (vp->v_type == VREG)
410 error = nfs_readrpc(vp, &auio, ap->a_cred);
411 else if (vp->v_type == VDIR) {
413 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
415 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
416 error = nfs_readdirrpc(vp, &auio, ap->a_cred);
418 } else if (vp->v_type == VLNK)
419 error = nfs_readlinkrpc(vp, &auio, ap->a_cred);
429 * Check to see if the type is ok
430 * and that deletion is not in progress.
431 * For paged in text files, you will need to flush the page cache
432 * if consistency is lost.
436 nfs_open(struct vop_open_args *ap)
438 struct vnode *vp = ap->a_vp;
439 struct nfsnode *np = VTONFS(vp);
443 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
445 printf("open eacces vtyp=%d\n", vp->v_type);
450 * Get a valid lease. If cached data is stale, flush it.
452 if (np->n_flag & NMODIFIED) {
453 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
454 ap->a_td, 1)) == EINTR)
457 if (vp->v_type == VDIR)
458 np->n_direofoffset = 0;
459 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
462 np->n_mtime = vattr.va_mtime.tv_sec;
464 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
467 if (np->n_mtime != vattr.va_mtime.tv_sec) {
468 if (vp->v_type == VDIR)
469 np->n_direofoffset = 0;
470 if ((error = nfs_vinvalbuf(vp, V_SAVE,
471 ap->a_cred, ap->a_td, 1)) == EINTR)
473 np->n_mtime = vattr.va_mtime.tv_sec;
476 np->n_attrstamp = 0; /* For Open/Close consistency */
482 * What an NFS client should do upon close after writing is a debatable issue.
483 * Most NFS clients push delayed writes to the server upon close, basically for
485 * 1 - So that any write errors may be reported back to the client process
486 * doing the close system call. By far the two most likely errors are
487 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
488 * 2 - To put a worst case upper bound on cache inconsistency between
489 * multiple clients for the file.
490 * There is also a consistency problem for Version 2 of the protocol w.r.t.
491 * not being able to tell if other clients are writing a file concurrently,
492 * since there is no way of knowing if the changed modify time in the reply
493 * is only due to the write for this client.
494 * (NFS Version 3 provides weak cache consistency data in the reply that
495 * should be sufficient to detect and handle this case.)
497 * The current code does the following:
498 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
499 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
500 * or commit them (this satisfies 1 and 2 except for the
501 * case where the server crashes after this close but
502 * before the commit RPC, which is felt to be "good
503 * enough". Changing the last argument to nfs_flush() to
504 * a 1 would force a commit operation, if it is felt a
505 * commit is necessary now.
509 nfs_close(struct vop_close_args *ap)
511 struct vnode *vp = ap->a_vp;
512 struct nfsnode *np = VTONFS(vp);
515 if (vp->v_type == VREG) {
516 if (np->n_flag & NMODIFIED) {
519 * Under NFSv3 we have dirty buffers to dispose of. We
520 * must flush them to the NFS server. We have the option
521 * of waiting all the way through the commit rpc or just
522 * waiting for the initial write. The default is to only
523 * wait through the initial write so the data is in the
524 * server's cache, which is roughly similar to the state
525 * a standard disk subsystem leaves the file in on close().
527 * We cannot clear the NMODIFIED bit in np->n_flag due to
528 * potential races with other processes, and certainly
529 * cannot clear it if we don't commit.
531 int cm = nfsv3_commit_on_close ? 1 : 0;
532 error = nfs_flush(vp, ap->a_cred, MNT_WAIT, ap->a_td, cm);
533 /* np->n_flag &= ~NMODIFIED; */
535 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_td, 1);
539 if (np->n_flag & NWRITEERR) {
540 np->n_flag &= ~NWRITEERR;
548 * nfs getattr call from vfs.
551 nfs_getattr(struct vop_getattr_args *ap)
553 struct vnode *vp = ap->a_vp;
554 struct nfsnode *np = VTONFS(vp);
558 struct mbuf *mreq, *mrep, *md, *mb;
559 int v3 = NFS_ISV3(vp);
562 * Update local times for special files.
564 if (np->n_flag & (NACC | NUPD))
567 * First look in the cache.
569 if (nfs_getattrcache(vp, ap->a_vap) == 0)
572 if (v3 && nfsaccess_cache_timeout > 0) {
573 nfsstats.accesscache_misses++;
574 nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_td, ap->a_cred);
575 if (nfs_getattrcache(vp, ap->a_vap) == 0)
579 nfsstats.rpccnt[NFSPROC_GETATTR]++;
580 mreq = nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
582 bpos = mtod(mb, caddr_t);
584 nfsm_request(vp, NFSPROC_GETATTR, ap->a_td, ap->a_cred);
586 nfsm_loadattr(vp, ap->a_vap);
597 nfs_setattr(struct vop_setattr_args *ap)
599 struct vnode *vp = ap->a_vp;
600 struct nfsnode *np = VTONFS(vp);
601 struct vattr *vap = ap->a_vap;
610 * Setting of flags is not supported.
612 if (vap->va_flags != VNOVAL)
616 * Disallow write attempts if the filesystem is mounted read-only.
618 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
619 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
620 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
621 (vp->v_mount->mnt_flag & MNT_RDONLY))
623 if (vap->va_size != VNOVAL) {
624 switch (vp->v_type) {
631 if (vap->va_mtime.tv_sec == VNOVAL &&
632 vap->va_atime.tv_sec == VNOVAL &&
633 vap->va_mode == (mode_t)VNOVAL &&
634 vap->va_uid == (uid_t)VNOVAL &&
635 vap->va_gid == (gid_t)VNOVAL)
637 vap->va_size = VNOVAL;
641 * Disallow write attempts if the filesystem is
644 if (vp->v_mount->mnt_flag & MNT_RDONLY)
646 vnode_pager_setsize(vp, vap->va_size);
647 if (np->n_flag & NMODIFIED) {
648 if (vap->va_size == 0)
649 error = nfs_vinvalbuf(vp, 0,
650 ap->a_cred, ap->a_td, 1);
652 error = nfs_vinvalbuf(vp, V_SAVE,
653 ap->a_cred, ap->a_td, 1);
655 vnode_pager_setsize(vp, np->n_size);
660 np->n_size = np->n_vattr.va_size = vap->va_size;
662 } else if ((vap->va_mtime.tv_sec != VNOVAL ||
663 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NMODIFIED) &&
664 vp->v_type == VREG &&
665 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
666 ap->a_td, 1)) == EINTR)
668 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
669 if (error && vap->va_size != VNOVAL) {
670 np->n_size = np->n_vattr.va_size = tsize;
671 vnode_pager_setsize(vp, np->n_size);
677 * Do an nfs setattr rpc.
680 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
683 struct nfsv2_sattr *sp;
686 int error = 0, wccflag = NFSV3_WCCRATTR;
687 struct mbuf *mreq, *mrep, *md, *mb;
688 int v3 = NFS_ISV3(vp);
690 nfsstats.rpccnt[NFSPROC_SETATTR]++;
691 mreq = nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
693 bpos = mtod(mb, caddr_t);
696 nfsm_v3attrbuild(vap, TRUE);
697 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
700 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
701 if (vap->va_mode == (mode_t)VNOVAL)
702 sp->sa_mode = nfs_xdrneg1;
704 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
705 if (vap->va_uid == (uid_t)VNOVAL)
706 sp->sa_uid = nfs_xdrneg1;
708 sp->sa_uid = txdr_unsigned(vap->va_uid);
709 if (vap->va_gid == (gid_t)VNOVAL)
710 sp->sa_gid = nfs_xdrneg1;
712 sp->sa_gid = txdr_unsigned(vap->va_gid);
713 sp->sa_size = txdr_unsigned(vap->va_size);
714 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
715 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
717 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
719 nfsm_wcc_data(vp, wccflag);
721 nfsm_loadattr(vp, (struct vattr *)0);
728 * nfs lookup call, one step at a time...
729 * First look in cache
730 * If not found, unlock the directory nfsnode and do the rpc
733 nfs_lookup(struct vop_lookup_args *ap)
735 struct componentname *cnp = ap->a_cnp;
736 struct vnode *dvp = ap->a_dvp;
737 struct vnode **vpp = ap->a_vpp;
738 int flags = cnp->cn_flags;
741 struct nfsmount *nmp;
743 struct mbuf *mreq, *mrep, *md, *mb;
747 int lockparent, wantparent, error = 0, attrflag, fhsize;
748 int v3 = NFS_ISV3(dvp);
749 struct thread *td = cnp->cn_thread;
752 cnp->cn_flags &= ~PDIRUNLOCK;
753 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
754 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
756 if (dvp->v_type != VDIR)
758 lockparent = flags & LOCKPARENT;
759 wantparent = flags & (LOCKPARENT|WANTPARENT);
760 nmp = VFSTONFS(dvp->v_mount);
762 if ((error = cache_lookup(dvp, vpp, cnp)) && error != ENOENT) {
766 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
774 * See the comment starting `Step through' in ufs/ufs_lookup.c
775 * for an explanation of the locking protocol
780 } else if (flags & ISDOTDOT) {
781 VOP_UNLOCK(dvp, 0, td);
782 cnp->cn_flags |= PDIRUNLOCK;
783 error = vget(newvp, LK_EXCLUSIVE, td);
784 if (!error && lockparent && (flags & ISLASTCN)) {
785 error = vn_lock(dvp, LK_EXCLUSIVE, td);
787 cnp->cn_flags &= ~PDIRUNLOCK;
790 error = vget(newvp, LK_EXCLUSIVE, td);
791 if (!lockparent || error || !(flags & ISLASTCN)) {
792 VOP_UNLOCK(dvp, 0, td);
793 cnp->cn_flags |= PDIRUNLOCK;
797 if (vpid == newvp->v_id) {
798 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, td)
799 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
800 nfsstats.lookupcache_hits++;
801 if (cnp->cn_nameiop != LOOKUP &&
803 cnp->cn_flags |= SAVENAME;
809 if (lockparent && dvp != newvp && (flags & ISLASTCN))
810 VOP_UNLOCK(dvp, 0, td);
812 error = vn_lock(dvp, LK_EXCLUSIVE, td);
815 cnp->cn_flags |= PDIRUNLOCK;
818 cnp->cn_flags &= ~PDIRUNLOCK;
822 nfsstats.lookupcache_misses++;
823 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
824 len = cnp->cn_namelen;
825 mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP,
826 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
828 bpos = mtod(mb, caddr_t);
830 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
831 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_thread, cnp->cn_cred);
833 nfsm_postop_attr(dvp, attrflag);
837 nfsm_getfh(fhp, fhsize, v3);
840 * Handle RENAME case...
842 if (cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
843 if (NFS_CMPFH(np, fhp, fhsize)) {
847 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
854 nfsm_postop_attr(newvp, attrflag);
855 nfsm_postop_attr(dvp, attrflag);
857 nfsm_loadattr(newvp, (struct vattr *)0);
860 cnp->cn_flags |= SAVENAME;
862 VOP_UNLOCK(dvp, 0, td);
863 cnp->cn_flags |= PDIRUNLOCK;
868 if (flags & ISDOTDOT) {
869 VOP_UNLOCK(dvp, 0, td);
870 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
872 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
876 if (lockparent && (flags & ISLASTCN)) {
877 error = vn_lock(dvp, LK_EXCLUSIVE, td);
879 cnp->cn_flags |= PDIRUNLOCK;
884 cnp->cn_flags |= PDIRUNLOCK;
885 } else if (NFS_CMPFH(np, fhp, fhsize)) {
889 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
894 if (!lockparent || !(flags & ISLASTCN)) {
895 cnp->cn_flags |= PDIRUNLOCK;
896 VOP_UNLOCK(dvp, 0, td);
901 nfsm_postop_attr(newvp, attrflag);
902 nfsm_postop_attr(dvp, attrflag);
904 nfsm_loadattr(newvp, (struct vattr *)0);
905 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
906 cnp->cn_flags |= SAVENAME;
907 if ((cnp->cn_flags & MAKEENTRY) &&
908 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
909 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
910 cache_enter(dvp, newvp, cnp);
916 if (newvp != NULLVP) {
920 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
921 (flags & ISLASTCN) && error == ENOENT) {
923 VOP_UNLOCK(dvp, 0, td);
924 cnp->cn_flags |= PDIRUNLOCK;
926 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
931 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
932 cnp->cn_flags |= SAVENAME;
939 * Just call nfs_bioread() to do the work.
942 nfs_read(struct vop_read_args *ap)
944 struct vnode *vp = ap->a_vp;
946 if (vp->v_type != VREG)
948 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
955 nfs_readlink(struct vop_readlink_args *ap)
957 struct vnode *vp = ap->a_vp;
959 if (vp->v_type != VLNK)
961 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred));
966 * Called by nfs_doio() from below the buffer cache.
969 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
973 int error = 0, len, attrflag;
974 struct mbuf *mreq, *mrep, *md, *mb;
975 int v3 = NFS_ISV3(vp);
977 nfsstats.rpccnt[NFSPROC_READLINK]++;
978 mreq = nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
980 bpos = mtod(mb, caddr_t);
982 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, cred);
984 nfsm_postop_attr(vp, attrflag);
986 nfsm_strsiz(len, NFS_MAXPATHLEN);
987 if (len == NFS_MAXPATHLEN) {
988 struct nfsnode *np = VTONFS(vp);
989 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
992 nfsm_mtouio(uiop, len);
1004 nfs_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1008 struct mbuf *mreq, *mrep, *md, *mb;
1009 struct nfsmount *nmp;
1010 int error = 0, len, retlen, tsiz, eof, attrflag;
1011 int v3 = NFS_ISV3(vp);
1016 nmp = VFSTONFS(vp->v_mount);
1017 tsiz = uiop->uio_resid;
1018 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1021 nfsstats.rpccnt[NFSPROC_READ]++;
1022 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1023 mreq = nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1025 bpos = mtod(mb, caddr_t);
1027 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED * 3);
1029 txdr_hyper(uiop->uio_offset, tl);
1030 *(tl + 2) = txdr_unsigned(len);
1032 *tl++ = txdr_unsigned(uiop->uio_offset);
1033 *tl++ = txdr_unsigned(len);
1036 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, cred);
1038 nfsm_postop_attr(vp, attrflag);
1043 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
1044 eof = fxdr_unsigned(int, *(tl + 1));
1046 nfsm_loadattr(vp, (struct vattr *)0);
1047 nfsm_strsiz(retlen, nmp->nm_rsize);
1048 nfsm_mtouio(uiop, retlen);
1052 if (eof || retlen == 0)
1054 } else if (retlen < len)
1065 nfs_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1066 int *iomode, int *must_commit)
1071 struct mbuf *mreq, *mrep, *md, *mb;
1072 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1073 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1074 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1077 if (uiop->uio_iovcnt != 1)
1078 panic("nfs: writerpc iovcnt > 1");
1081 tsiz = uiop->uio_resid;
1082 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1085 nfsstats.rpccnt[NFSPROC_WRITE]++;
1086 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1087 mreq = nfsm_reqhead(vp, NFSPROC_WRITE,
1088 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1090 bpos = mtod(mb, caddr_t);
1093 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
1094 txdr_hyper(uiop->uio_offset, tl);
1096 *tl++ = txdr_unsigned(len);
1097 *tl++ = txdr_unsigned(*iomode);
1098 *tl = txdr_unsigned(len);
1102 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1103 /* Set both "begin" and "current" to non-garbage. */
1104 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1105 *tl++ = x; /* "begin offset" */
1106 *tl++ = x; /* "current offset" */
1107 x = txdr_unsigned(len);
1108 *tl++ = x; /* total to this offset */
1109 *tl = x; /* size of this write */
1111 nfsm_uiotom(uiop, len);
1112 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, cred);
1114 wccflag = NFSV3_WCCCHK;
1115 nfsm_wcc_data(vp, wccflag);
1117 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED
1118 + NFSX_V3WRITEVERF);
1119 rlen = fxdr_unsigned(int, *tl++);
1124 } else if (rlen < len) {
1125 backup = len - rlen;
1126 uiop->uio_iov->iov_base -= backup;
1127 uiop->uio_iov->iov_len += backup;
1128 uiop->uio_offset -= backup;
1129 uiop->uio_resid += backup;
1132 commit = fxdr_unsigned(int, *tl++);
1135 * Return the lowest committment level
1136 * obtained by any of the RPCs.
1138 if (committed == NFSV3WRITE_FILESYNC)
1140 else if (committed == NFSV3WRITE_DATASYNC &&
1141 commit == NFSV3WRITE_UNSTABLE)
1143 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1144 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1146 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1147 } else if (bcmp((caddr_t)tl,
1148 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1150 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1155 nfsm_loadattr(vp, (struct vattr *)0);
1157 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime.tv_sec;
1164 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1165 committed = NFSV3WRITE_FILESYNC;
1166 *iomode = committed;
1168 uiop->uio_resid = tsiz;
1174 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1175 * mode set to specify the file type and the size field for rdev.
1178 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1181 struct nfsv2_sattr *sp;
1183 struct vnode *newvp = (struct vnode *)0;
1184 struct nfsnode *np = (struct nfsnode *)0;
1187 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1188 struct mbuf *mreq, *mrep, *md, *mb;
1190 int v3 = NFS_ISV3(dvp);
1192 if (vap->va_type == VCHR || vap->va_type == VBLK)
1193 rdev = txdr_unsigned(vap->va_rdev);
1194 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1197 return (EOPNOTSUPP);
1199 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1202 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1203 mreq = nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1204 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1206 bpos = mtod(mb, caddr_t);
1207 nfsm_fhtom(dvp, v3);
1208 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1210 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1211 *tl++ = vtonfsv3_type(vap->va_type);
1212 nfsm_v3attrbuild(vap, FALSE);
1213 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1214 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
1215 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1216 *tl = txdr_unsigned(uminor(vap->va_rdev));
1219 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1220 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1221 sp->sa_uid = nfs_xdrneg1;
1222 sp->sa_gid = nfs_xdrneg1;
1224 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1225 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1227 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_thread, cnp->cn_cred);
1229 nfsm_mtofh(dvp, newvp, v3, gotvp);
1233 newvp = (struct vnode *)0;
1235 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1236 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1242 nfsm_wcc_data(dvp, wccflag);
1249 if (cnp->cn_flags & MAKEENTRY)
1250 cache_enter(dvp, newvp, cnp);
1253 VTONFS(dvp)->n_flag |= NMODIFIED;
1255 VTONFS(dvp)->n_attrstamp = 0;
1261 * just call nfs_mknodrpc() to do the work.
1265 nfs_mknod(struct vop_mknod_args *ap)
1268 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1271 static u_long create_verf;
1273 * nfs file create call
1276 nfs_create(struct vop_create_args *ap)
1278 struct vnode *dvp = ap->a_dvp;
1279 struct vattr *vap = ap->a_vap;
1280 struct componentname *cnp = ap->a_cnp;
1281 struct nfsv2_sattr *sp;
1283 struct nfsnode *np = (struct nfsnode *)0;
1284 struct vnode *newvp = (struct vnode *)0;
1286 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1287 struct mbuf *mreq, *mrep, *md, *mb;
1289 int v3 = NFS_ISV3(dvp);
1292 * Oops, not for me..
1294 if (vap->va_type == VSOCK)
1295 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1297 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1300 if (vap->va_vaflags & VA_EXCLUSIVE)
1303 nfsstats.rpccnt[NFSPROC_CREATE]++;
1304 mreq = nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1305 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1307 bpos = mtod(mb, caddr_t);
1308 nfsm_fhtom(dvp, v3);
1309 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1311 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1312 if (fmode & O_EXCL) {
1313 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1314 tl = nfsm_build(u_int32_t *, NFSX_V3CREATEVERF);
1316 if (!TAILQ_EMPTY(&in_ifaddrhead))
1317 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr;
1320 *tl++ = create_verf;
1321 *tl = ++create_verf;
1323 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1324 nfsm_v3attrbuild(vap, FALSE);
1327 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1328 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1329 sp->sa_uid = nfs_xdrneg1;
1330 sp->sa_gid = nfs_xdrneg1;
1332 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1333 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1335 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_thread, cnp->cn_cred);
1337 nfsm_mtofh(dvp, newvp, v3, gotvp);
1341 newvp = (struct vnode *)0;
1343 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1344 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1350 nfsm_wcc_data(dvp, wccflag);
1354 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1360 } else if (v3 && (fmode & O_EXCL)) {
1362 * We are normally called with only a partially initialized
1363 * VAP. Since the NFSv3 spec says that server may use the
1364 * file attributes to store the verifier, the spec requires
1365 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1366 * in atime, but we can't really assume that all servers will
1367 * so we ensure that our SETATTR sets both atime and mtime.
1369 if (vap->va_mtime.tv_sec == VNOVAL)
1370 vfs_timestamp(&vap->va_mtime);
1371 if (vap->va_atime.tv_sec == VNOVAL)
1372 vap->va_atime = vap->va_mtime;
1373 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_thread);
1376 if (cnp->cn_flags & MAKEENTRY)
1377 cache_enter(dvp, newvp, cnp);
1380 VTONFS(dvp)->n_flag |= NMODIFIED;
1382 VTONFS(dvp)->n_attrstamp = 0;
1387 * nfs file remove call
1388 * To try and make nfs semantics closer to ufs semantics, a file that has
1389 * other processes using the vnode is renamed instead of removed and then
1390 * removed later on the last close.
1391 * - If v_usecount > 1
1392 * If a rename is not already in the works
1393 * call nfs_sillyrename() to set it up
1398 nfs_remove(struct vop_remove_args *ap)
1400 struct vnode *vp = ap->a_vp;
1401 struct vnode *dvp = ap->a_dvp;
1402 struct componentname *cnp = ap->a_cnp;
1403 struct nfsnode *np = VTONFS(vp);
1408 if ((cnp->cn_flags & HASBUF) == 0)
1409 panic("nfs_remove: no name");
1410 if (vp->v_usecount < 1)
1411 panic("nfs_remove: bad v_usecount");
1413 if (vp->v_type == VDIR)
1415 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1416 VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_thread) == 0 &&
1417 vattr.va_nlink > 1)) {
1419 * Purge the name cache so that the chance of a lookup for
1420 * the name succeeding while the remove is in progress is
1421 * minimized. Without node locking it can still happen, such
1422 * that an I/O op returns ESTALE, but since you get this if
1423 * another host removes the file..
1427 * throw away biocache buffers, mainly to avoid
1428 * unnecessary delayed writes later.
1430 error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, cnp->cn_thread, 1);
1433 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1434 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1436 * Kludge City: If the first reply to the remove rpc is lost..
1437 * the reply to the retransmitted request will be ENOENT
1438 * since the file was in fact removed
1439 * Therefore, we cheat and return success.
1441 if (error == ENOENT)
1443 } else if (!np->n_sillyrename)
1444 error = nfs_sillyrename(dvp, vp, cnp);
1445 np->n_attrstamp = 0;
1450 * nfs file remove rpc called from nfs_inactive
1453 nfs_removeit(struct sillyrename *sp)
1456 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1457 (struct thread *)0));
1461 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1464 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1465 struct ucred *cred, struct thread *td)
1469 int error = 0, wccflag = NFSV3_WCCRATTR;
1470 struct mbuf *mreq, *mrep, *md, *mb;
1471 int v3 = NFS_ISV3(dvp);
1473 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1474 mreq = nfsm_reqhead(dvp, NFSPROC_REMOVE,
1475 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1477 bpos = mtod(mb, caddr_t);
1478 nfsm_fhtom(dvp, v3);
1479 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1480 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1482 nfsm_wcc_data(dvp, wccflag);
1485 VTONFS(dvp)->n_flag |= NMODIFIED;
1487 VTONFS(dvp)->n_attrstamp = 0;
1492 * nfs file rename call
1495 nfs_rename(struct vop_rename_args *ap)
1497 struct vnode *fvp = ap->a_fvp;
1498 struct vnode *tvp = ap->a_tvp;
1499 struct vnode *fdvp = ap->a_fdvp;
1500 struct vnode *tdvp = ap->a_tdvp;
1501 struct componentname *tcnp = ap->a_tcnp;
1502 struct componentname *fcnp = ap->a_fcnp;
1506 if ((tcnp->cn_flags & HASBUF) == 0 ||
1507 (fcnp->cn_flags & HASBUF) == 0)
1508 panic("nfs_rename: no name");
1510 /* Check for cross-device rename */
1511 if ((fvp->v_mount != tdvp->v_mount) ||
1512 (tvp && (fvp->v_mount != tvp->v_mount))) {
1518 * We have to flush B_DELWRI data prior to renaming
1519 * the file. If we don't, the delayed-write buffers
1520 * can be flushed out later after the file has gone stale
1521 * under NFSV3. NFSV2 does not have this problem because
1522 * ( as far as I can tell ) it flushes dirty buffers more
1526 VOP_FSYNC(fvp, fcnp->cn_cred, MNT_WAIT, fcnp->cn_thread);
1528 VOP_FSYNC(tvp, tcnp->cn_cred, MNT_WAIT, tcnp->cn_thread);
1531 * If the tvp exists and is in use, sillyrename it before doing the
1532 * rename of the new file over it.
1533 * XXX Can't sillyrename a directory.
1535 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1536 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1541 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1542 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1545 if (fvp->v_type == VDIR) {
1546 if (tvp != NULL && tvp->v_type == VDIR)
1561 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1563 if (error == ENOENT)
1569 * nfs file rename rpc called from nfs_remove() above
1572 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1573 struct sillyrename *sp)
1576 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen, sdvp,
1577 sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_thread));
1581 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1584 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1585 struct vnode *tdvp, const char *tnameptr, int tnamelen, struct ucred *cred,
1590 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1591 struct mbuf *mreq, *mrep, *md, *mb;
1592 int v3 = NFS_ISV3(fdvp);
1594 nfsstats.rpccnt[NFSPROC_RENAME]++;
1595 mreq = nfsm_reqhead(fdvp, NFSPROC_RENAME,
1596 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1597 nfsm_rndup(tnamelen));
1599 bpos = mtod(mb, caddr_t);
1600 nfsm_fhtom(fdvp, v3);
1601 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1602 nfsm_fhtom(tdvp, v3);
1603 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1604 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1606 nfsm_wcc_data(fdvp, fwccflag);
1607 nfsm_wcc_data(tdvp, twccflag);
1611 VTONFS(fdvp)->n_flag |= NMODIFIED;
1612 VTONFS(tdvp)->n_flag |= NMODIFIED;
1614 VTONFS(fdvp)->n_attrstamp = 0;
1616 VTONFS(tdvp)->n_attrstamp = 0;
1621 * nfs hard link create call
1624 nfs_link(struct vop_link_args *ap)
1626 struct vnode *vp = ap->a_vp;
1627 struct vnode *tdvp = ap->a_tdvp;
1628 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, cnp->cn_cred, 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;
1687 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1688 struct mbuf *mreq, *mrep, *md, *mb;
1689 struct vnode *newvp = (struct vnode *)0;
1690 int v3 = NFS_ISV3(dvp);
1692 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1693 slen = strlen(ap->a_target);
1694 mreq = nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1695 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1697 bpos = mtod(mb, caddr_t);
1698 nfsm_fhtom(dvp, v3);
1699 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1701 nfsm_v3attrbuild(vap, FALSE);
1703 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1705 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1706 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1707 sp->sa_uid = nfs_xdrneg1;
1708 sp->sa_gid = nfs_xdrneg1;
1709 sp->sa_size = nfs_xdrneg1;
1710 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1711 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1715 * Issue the NFS request and get the rpc response.
1717 * Only NFSv3 responses returning an error of 0 actually return
1718 * a file handle that can be converted into newvp without having
1719 * to do an extra lookup rpc.
1721 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_thread, cnp->cn_cred);
1724 nfsm_mtofh(dvp, newvp, v3, gotvp);
1725 nfsm_wcc_data(dvp, wccflag);
1729 * out code jumps -> here, mrep is also freed.
1736 * If we get an EEXIST error, silently convert it to no-error
1737 * in case of an NFS retry.
1739 if (error == EEXIST)
1743 * If we do not have (or no longer have) an error, and we could
1744 * not extract the newvp from the response due to the request being
1745 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1746 * to obtain a newvp to return.
1748 if (error == 0 && newvp == NULL) {
1749 struct nfsnode *np = NULL;
1751 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1752 cnp->cn_cred, cnp->cn_thread, &np);
1762 VTONFS(dvp)->n_flag |= NMODIFIED;
1764 VTONFS(dvp)->n_attrstamp = 0;
1772 nfs_mkdir(struct vop_mkdir_args *ap)
1774 struct vnode *dvp = ap->a_dvp;
1775 struct vattr *vap = ap->a_vap;
1776 struct componentname *cnp = ap->a_cnp;
1777 struct nfsv2_sattr *sp;
1780 struct nfsnode *np = (struct nfsnode *)0;
1781 struct vnode *newvp = (struct vnode *)0;
1783 int error = 0, wccflag = NFSV3_WCCRATTR;
1785 struct mbuf *mreq, *mrep, *md, *mb;
1787 int v3 = NFS_ISV3(dvp);
1789 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1792 len = cnp->cn_namelen;
1793 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1794 mreq = nfsm_reqhead(dvp, NFSPROC_MKDIR,
1795 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1797 bpos = mtod(mb, caddr_t);
1798 nfsm_fhtom(dvp, v3);
1799 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1801 nfsm_v3attrbuild(vap, FALSE);
1803 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1804 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1805 sp->sa_uid = nfs_xdrneg1;
1806 sp->sa_gid = nfs_xdrneg1;
1807 sp->sa_size = nfs_xdrneg1;
1808 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1809 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1811 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_thread, cnp->cn_cred);
1813 nfsm_mtofh(dvp, newvp, v3, gotvp);
1815 nfsm_wcc_data(dvp, wccflag);
1818 VTONFS(dvp)->n_flag |= NMODIFIED;
1820 VTONFS(dvp)->n_attrstamp = 0;
1822 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1823 * if we can succeed in looking up the directory.
1825 if (error == EEXIST || (!error && !gotvp)) {
1828 newvp = (struct vnode *)0;
1830 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
1831 cnp->cn_thread, &np);
1834 if (newvp->v_type != VDIR)
1847 * nfs remove directory call
1850 nfs_rmdir(struct vop_rmdir_args *ap)
1852 struct vnode *vp = ap->a_vp;
1853 struct vnode *dvp = ap->a_dvp;
1854 struct componentname *cnp = ap->a_cnp;
1857 int error = 0, wccflag = NFSV3_WCCRATTR;
1858 struct mbuf *mreq, *mrep, *md, *mb;
1859 int v3 = NFS_ISV3(dvp);
1863 nfsstats.rpccnt[NFSPROC_RMDIR]++;
1864 mreq = nfsm_reqhead(dvp, NFSPROC_RMDIR,
1865 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1867 bpos = mtod(mb, caddr_t);
1868 nfsm_fhtom(dvp, v3);
1869 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1870 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_thread, cnp->cn_cred);
1872 nfsm_wcc_data(dvp, wccflag);
1875 VTONFS(dvp)->n_flag |= NMODIFIED;
1877 VTONFS(dvp)->n_attrstamp = 0;
1881 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
1883 if (error == ENOENT)
1892 nfs_readdir(struct vop_readdir_args *ap)
1894 struct vnode *vp = ap->a_vp;
1895 struct nfsnode *np = VTONFS(vp);
1896 struct uio *uio = ap->a_uio;
1900 if (vp->v_type != VDIR)
1903 * First, check for hit on the EOF offset cache
1905 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
1906 (np->n_flag & NMODIFIED) == 0) {
1907 if (VOP_GETATTR(vp, &vattr, ap->a_cred, uio->uio_td) == 0 &&
1908 np->n_mtime == vattr.va_mtime.tv_sec) {
1909 nfsstats.direofcache_hits++;
1915 * Call nfs_bioread() to do the real work.
1917 tresid = uio->uio_resid;
1918 error = nfs_bioread(vp, uio, 0, ap->a_cred);
1920 if (!error && uio->uio_resid == tresid)
1921 nfsstats.direofcache_misses++;
1927 * Called from below the buffer cache by nfs_doio().
1930 nfs_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1933 struct dirent *dp = NULL;
1938 struct mbuf *mreq, *mrep, *md, *mb;
1940 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1941 struct nfsnode *dnp = VTONFS(vp);
1943 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
1945 int v3 = NFS_ISV3(vp);
1948 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
1949 (uiop->uio_resid & (DIRBLKSIZ - 1)))
1950 panic("nfs readdirrpc bad uio");
1954 * If there is no cookie, assume directory was stale.
1956 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
1960 return (NFSERR_BAD_COOKIE);
1962 * Loop around doing readdir rpc's of size nm_readdirsize
1963 * truncated to a multiple of DIRBLKSIZ.
1964 * The stopping criteria is EOF or buffer full.
1966 while (more_dirs && bigenough) {
1967 nfsstats.rpccnt[NFSPROC_READDIR]++;
1968 mreq = nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
1971 bpos = mtod(mb, caddr_t);
1974 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
1975 *tl++ = cookie.nfsuquad[0];
1976 *tl++ = cookie.nfsuquad[1];
1977 *tl++ = dnp->n_cookieverf.nfsuquad[0];
1978 *tl++ = dnp->n_cookieverf.nfsuquad[1];
1980 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
1981 *tl++ = cookie.nfsuquad[0];
1983 *tl = txdr_unsigned(nmp->nm_readdirsize);
1984 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, cred);
1986 nfsm_postop_attr(vp, attrflag);
1988 tl = nfsm_dissect(u_int32_t *,
1990 dnp->n_cookieverf.nfsuquad[0] = *tl++;
1991 dnp->n_cookieverf.nfsuquad[1] = *tl;
1997 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1998 more_dirs = fxdr_unsigned(int, *tl);
2000 /* loop thru the dir entries, doctoring them to 4bsd form */
2001 while (more_dirs && bigenough) {
2003 tl = nfsm_dissect(u_int32_t *,
2005 fileno = fxdr_hyper(tl);
2006 len = fxdr_unsigned(int, *(tl + 2));
2008 tl = nfsm_dissect(u_int32_t *,
2010 fileno = fxdr_unsigned(u_quad_t, *tl++);
2011 len = fxdr_unsigned(int, *tl);
2013 if (len <= 0 || len > NFS_MAXNAMLEN) {
2018 tlen = nfsm_rndup(len);
2020 tlen += 4; /* To ensure null termination */
2021 left = DIRBLKSIZ - blksiz;
2022 if ((tlen + DIRHDSIZ) > left) {
2023 dp->d_reclen += left;
2024 uiop->uio_iov->iov_base += left;
2025 uiop->uio_iov->iov_len -= left;
2026 uiop->uio_offset += left;
2027 uiop->uio_resid -= left;
2030 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2033 dp = (struct dirent *)uiop->uio_iov->iov_base;
2034 dp->d_fileno = (int)fileno;
2036 dp->d_reclen = tlen + DIRHDSIZ;
2037 dp->d_type = DT_UNKNOWN;
2038 blksiz += dp->d_reclen;
2039 if (blksiz == DIRBLKSIZ)
2041 uiop->uio_offset += DIRHDSIZ;
2042 uiop->uio_resid -= DIRHDSIZ;
2043 uiop->uio_iov->iov_base += DIRHDSIZ;
2044 uiop->uio_iov->iov_len -= DIRHDSIZ;
2045 nfsm_mtouio(uiop, len);
2046 cp = uiop->uio_iov->iov_base;
2048 *cp = '\0'; /* null terminate */
2049 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 += left;
2089 uiop->uio_iov->iov_len -= left;
2090 uiop->uio_offset += left;
2091 uiop->uio_resid -= left;
2095 * We are now either at the end of the directory or have filled the
2099 dnp->n_direofoffset = uiop->uio_offset;
2101 if (uiop->uio_resid > 0)
2102 printf("EEK! readdirrpc resid > 0\n");
2103 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2111 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2114 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
2120 struct vnode *newvp;
2122 caddr_t bpos, dpos, dpossav1, dpossav2;
2123 struct mbuf *mreq, *mrep, *md, *mb, *mdsav1, *mdsav2;
2124 struct nameidata nami, *ndp = &nami;
2125 struct componentname *cnp = &ndp->ni_cnd;
2127 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2128 struct nfsnode *dnp = VTONFS(vp), *np;
2131 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2132 int attrflag, fhsize;
2135 dp = (struct dirent *)0;
2138 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2139 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2140 panic("nfs readdirplusrpc bad uio");
2146 * If there is no cookie, assume directory was stale.
2148 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2152 return (NFSERR_BAD_COOKIE);
2154 * Loop around doing readdir rpc's of size nm_readdirsize
2155 * truncated to a multiple of DIRBLKSIZ.
2156 * The stopping criteria is EOF or buffer full.
2158 while (more_dirs && bigenough) {
2159 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2160 mreq = nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2161 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2163 bpos = mtod(mb, caddr_t);
2165 tl = nfsm_build(u_int32_t *, 6 * NFSX_UNSIGNED);
2166 *tl++ = cookie.nfsuquad[0];
2167 *tl++ = cookie.nfsuquad[1];
2168 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2169 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2170 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2171 *tl = txdr_unsigned(nmp->nm_rsize);
2172 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, cred);
2173 nfsm_postop_attr(vp, attrflag);
2178 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2179 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2180 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2181 more_dirs = fxdr_unsigned(int, *tl);
2183 /* loop thru the dir entries, doctoring them to 4bsd form */
2184 while (more_dirs && bigenough) {
2185 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2186 fileno = fxdr_hyper(tl);
2187 len = fxdr_unsigned(int, *(tl + 2));
2188 if (len <= 0 || len > NFS_MAXNAMLEN) {
2193 tlen = nfsm_rndup(len);
2195 tlen += 4; /* To ensure null termination*/
2196 left = DIRBLKSIZ - blksiz;
2197 if ((tlen + DIRHDSIZ) > left) {
2198 dp->d_reclen += left;
2199 uiop->uio_iov->iov_base += left;
2200 uiop->uio_iov->iov_len -= left;
2201 uiop->uio_offset += left;
2202 uiop->uio_resid -= left;
2205 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2208 dp = (struct dirent *)uiop->uio_iov->iov_base;
2209 dp->d_fileno = (int)fileno;
2211 dp->d_reclen = tlen + DIRHDSIZ;
2212 dp->d_type = DT_UNKNOWN;
2213 blksiz += dp->d_reclen;
2214 if (blksiz == DIRBLKSIZ)
2216 uiop->uio_offset += DIRHDSIZ;
2217 uiop->uio_resid -= DIRHDSIZ;
2218 uiop->uio_iov->iov_base += DIRHDSIZ;
2219 uiop->uio_iov->iov_len -= DIRHDSIZ;
2220 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2221 cnp->cn_namelen = len;
2222 nfsm_mtouio(uiop, len);
2223 cp = uiop->uio_iov->iov_base;
2226 uiop->uio_iov->iov_base += tlen;
2227 uiop->uio_iov->iov_len -= tlen;
2228 uiop->uio_offset += tlen;
2229 uiop->uio_resid -= tlen;
2231 nfsm_adv(nfsm_rndup(len));
2232 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2234 cookie.nfsuquad[0] = *tl++;
2235 cookie.nfsuquad[1] = *tl++;
2240 * Since the attributes are before the file handle
2241 * (sigh), we must skip over the attributes and then
2242 * come back and get them.
2244 attrflag = fxdr_unsigned(int, *tl);
2248 nfsm_adv(NFSX_V3FATTR);
2249 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2250 doit = fxdr_unsigned(int, *tl);
2252 nfsm_getfh(fhp, fhsize, 1);
2253 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2258 error = nfs_nget(vp->v_mount, fhp,
2266 if (doit && bigenough) {
2271 nfsm_loadattr(newvp, (struct vattr *)0);
2275 IFTODT(VTTOIF(np->n_vattr.va_type));
2277 cache_enter(ndp->ni_dvp, ndp->ni_vp, cnp);
2280 /* Just skip over the file handle */
2281 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2282 i = fxdr_unsigned(int, *tl);
2283 nfsm_adv(nfsm_rndup(i));
2285 if (newvp != NULLVP) {
2292 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2293 more_dirs = fxdr_unsigned(int, *tl);
2296 * If at end of rpc data, get the eof boolean
2299 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2300 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2305 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2306 * by increasing d_reclen for the last record.
2309 left = DIRBLKSIZ - blksiz;
2310 dp->d_reclen += left;
2311 uiop->uio_iov->iov_base += left;
2312 uiop->uio_iov->iov_len -= left;
2313 uiop->uio_offset += left;
2314 uiop->uio_resid -= left;
2318 * We are now either at the end of the directory or have filled the
2322 dnp->n_direofoffset = uiop->uio_offset;
2324 if (uiop->uio_resid > 0)
2325 printf("EEK! readdirplusrpc resid > 0\n");
2326 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2330 if (newvp != NULLVP) {
2341 * Silly rename. To make the NFS filesystem that is stateless look a little
2342 * more like the "ufs" a remove of an active vnode is translated to a rename
2343 * to a funny looking filename that is removed by nfs_inactive on the
2344 * nfsnode. There is the potential for another process on a different client
2345 * to create the same funny name between the nfs_lookitup() fails and the
2346 * nfs_rename() completes, but...
2349 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2351 struct sillyrename *sp;
2359 if (vp->v_type == VDIR)
2360 panic("nfs: sillyrename dir");
2362 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2363 M_NFSREQ, M_WAITOK);
2364 crhold(cnp->cn_cred);
2365 sp->s_cred = cnp->cn_cred;
2369 /* Fudge together a funny name */
2370 pid = cnp->cn_thread->td_proc->p_pid;
2371 sp->s_namlen = sprintf(sp->s_name, ".nfsA%04x4.4", pid);
2373 /* Try lookitups until we get one that isn't there */
2374 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2375 cnp->cn_thread, (struct nfsnode **)0) == 0) {
2377 if (sp->s_name[4] > 'z') {
2382 error = nfs_renameit(dvp, cnp, sp);
2385 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2386 cnp->cn_thread, &np);
2387 np->n_sillyrename = sp;
2392 free((caddr_t)sp, M_NFSREQ);
2397 * Look up a file name and optionally either update the file handle or
2398 * allocate an nfsnode, depending on the value of npp.
2399 * npp == NULL --> just do the lookup
2400 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2402 * *npp != NULL --> update the file handle in the vnode
2405 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2406 struct thread *td, struct nfsnode **npp)
2409 struct vnode *newvp = (struct vnode *)0;
2410 struct nfsnode *np, *dnp = VTONFS(dvp);
2412 int error = 0, fhlen, attrflag;
2413 struct mbuf *mreq, *mrep, *md, *mb;
2415 int v3 = NFS_ISV3(dvp);
2417 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2418 mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2419 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2421 bpos = mtod(mb, caddr_t);
2422 nfsm_fhtom(dvp, v3);
2423 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2424 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2425 if (npp && !error) {
2426 nfsm_getfh(nfhp, fhlen, v3);
2429 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2430 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2431 np->n_fhp = &np->n_fh;
2432 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2433 np->n_fhp =(nfsfh_t *)malloc(fhlen, M_NFSBIGFH, M_WAITOK);
2434 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2435 np->n_fhsize = fhlen;
2437 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2441 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2449 nfsm_postop_attr(newvp, attrflag);
2450 if (!attrflag && *npp == NULL) {
2459 nfsm_loadattr(newvp, (struct vattr *)0);
2463 if (npp && *npp == NULL) {
2478 * Nfs Version 3 commit rpc
2481 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2485 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2487 int error = 0, wccflag = NFSV3_WCCRATTR;
2488 struct mbuf *mreq, *mrep, *md, *mb;
2490 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2492 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2493 mreq = nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2495 bpos = mtod(mb, caddr_t);
2497 tl = nfsm_build(u_int32_t *, 3 * NFSX_UNSIGNED);
2498 txdr_hyper(offset, tl);
2500 *tl = txdr_unsigned(cnt);
2501 nfsm_request(vp, NFSPROC_COMMIT, td, cred);
2502 nfsm_wcc_data(vp, wccflag);
2504 tl = nfsm_dissect(u_int32_t *, NFSX_V3WRITEVERF);
2505 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2506 NFSX_V3WRITEVERF)) {
2507 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2509 error = NFSERR_STALEWRITEVERF;
2519 * For async requests when nfsiod(s) are running, queue the request by
2520 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2524 nfs_strategy(struct vop_strategy_args *ap)
2526 struct buf *bp = ap->a_bp;
2531 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2532 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2534 if (bp->b_flags & B_PHYS)
2535 panic("nfs physio");
2537 if (bp->b_flags & B_ASYNC)
2538 td = (struct thread *)0;
2540 td = curthread; /* XXX */
2542 if (bp->b_iocmd == BIO_READ)
2548 * If the op is asynchronous and an i/o daemon is waiting
2549 * queue the request, wake it up and wait for completion
2550 * otherwise just do it ourselves.
2552 if ((bp->b_flags & B_ASYNC) == 0 ||
2553 nfs_asyncio(bp, NOCRED, td))
2554 error = nfs_doio(bp, cr, td);
2559 * fsync vnode op. Just call nfs_flush() with commit == 1.
2563 nfs_fsync(struct vop_fsync_args *ap)
2566 return (nfs_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_td, 1));
2570 * Flush all the blocks associated with a vnode.
2571 * Walk through the buffer pool and push any dirty pages
2572 * associated with the vnode.
2575 nfs_flush(struct vnode *vp, struct ucred *cred, int waitfor, struct thread *td,
2578 struct nfsnode *np = VTONFS(vp);
2582 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2583 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2585 u_quad_t off, endoff, toff;
2586 struct ucred* wcred = NULL;
2587 struct buf **bvec = NULL;
2588 #ifndef NFS_COMMITBVECSIZ
2589 #define NFS_COMMITBVECSIZ 20
2591 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2592 int bvecsize = 0, bveccount;
2594 if (nmp->nm_flag & NFSMNT_INT)
2599 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2600 * server, but nas not been committed to stable storage on the server
2601 * yet. On the first pass, the byte range is worked out and the commit
2602 * rpc is done. On the second pass, nfs_writebp() is called to do the
2609 if (NFS_ISV3(vp) && commit) {
2612 * Count up how many buffers waiting for a commit.
2615 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2616 nbp = TAILQ_NEXT(bp, b_vnbufs);
2617 if (BUF_REFCNT(bp) == 0 &&
2618 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2619 == (B_DELWRI | B_NEEDCOMMIT))
2623 * Allocate space to remember the list of bufs to commit. It is
2624 * important to use M_NOWAIT here to avoid a race with nfs_write.
2625 * If we can't get memory (for whatever reason), we will end up
2626 * committing the buffers one-by-one in the loop below.
2628 if (bvec != NULL && bvec != bvec_on_stack)
2630 if (bveccount > NFS_COMMITBVECSIZ) {
2631 bvec = (struct buf **)
2632 malloc(bveccount * sizeof(struct buf *),
2635 bvec = bvec_on_stack;
2636 bvecsize = NFS_COMMITBVECSIZ;
2638 bvecsize = bveccount;
2640 bvec = bvec_on_stack;
2641 bvecsize = NFS_COMMITBVECSIZ;
2643 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2644 nbp = TAILQ_NEXT(bp, b_vnbufs);
2645 if (bvecpos >= bvecsize)
2647 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2648 (B_DELWRI | B_NEEDCOMMIT) ||
2649 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
2653 * Work out if all buffers are using the same cred
2654 * so we can deal with them all with one commit.
2656 * NOTE: we are not clearing B_DONE here, so we have
2657 * to do it later on in this routine if we intend to
2658 * initiate I/O on the bp.
2660 * Note: to avoid loopback deadlocks, we do not
2661 * assign b_runningbufspace.
2664 wcred = bp->b_wcred;
2665 else if (wcred != bp->b_wcred)
2667 bp->b_flags |= B_WRITEINPROG;
2668 vfs_busy_pages(bp, 1);
2671 * bp is protected by being locked, but nbp is not
2672 * and vfs_busy_pages() may sleep. We have to
2675 nbp = TAILQ_NEXT(bp, b_vnbufs);
2678 * A list of these buffers is kept so that the
2679 * second loop knows which buffers have actually
2680 * been committed. This is necessary, since there
2681 * may be a race between the commit rpc and new
2682 * uncommitted writes on the file.
2684 bvec[bvecpos++] = bp;
2685 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2689 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2697 * Commit data on the server, as required.
2698 * If all bufs are using the same wcred, then use that with
2699 * one call for all of them, otherwise commit each one
2702 if (wcred != NOCRED)
2703 retv = nfs_commit(vp, off, (int)(endoff - off),
2707 for (i = 0; i < bvecpos; i++) {
2710 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2712 size = (u_quad_t)(bp->b_dirtyend
2714 retv = nfs_commit(vp, off, (int)size,
2720 if (retv == NFSERR_STALEWRITEVERF)
2721 nfs_clearcommit(vp->v_mount);
2724 * Now, either mark the blocks I/O done or mark the
2725 * blocks dirty, depending on whether the commit
2728 for (i = 0; i < bvecpos; i++) {
2730 bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG | B_CLUSTEROK);
2733 * Error, leave B_DELWRI intact
2735 vfs_unbusy_pages(bp);
2739 * Success, remove B_DELWRI ( bundirty() ).
2741 * b_dirtyoff/b_dirtyend seem to be NFS
2742 * specific. We should probably move that
2743 * into bundirty(). XXX
2747 bp->b_flags |= B_ASYNC;
2749 bp->b_flags &= ~B_DONE;
2750 bp->b_ioflags &= ~BIO_ERROR;
2751 bp->b_dirtyoff = bp->b_dirtyend = 0;
2759 * Start/do any write(s) that are required.
2763 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2764 nbp = TAILQ_NEXT(bp, b_vnbufs);
2765 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
2766 if (waitfor != MNT_WAIT || passone)
2768 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL,
2769 "nfsfsync", slpflag, slptimeo);
2772 panic("nfs_fsync: inconsistent lock");
2773 if (error == ENOLCK)
2775 if (nfs_sigintr(nmp, (struct nfsreq *)0, td->td_proc)) {
2779 if (slpflag == PCATCH) {
2785 if ((bp->b_flags & B_DELWRI) == 0)
2786 panic("nfs_fsync: not dirty");
2787 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2792 if (passone || !commit)
2793 bp->b_flags |= B_ASYNC;
2795 bp->b_flags |= B_ASYNC | B_WRITEINPROG;
2805 if (waitfor == MNT_WAIT) {
2806 while (vp->v_numoutput) {
2807 vp->v_flag |= VBWAIT;
2808 error = tsleep((caddr_t)&vp->v_numoutput,
2809 slpflag | (PRIBIO + 1), "nfsfsync", slptimeo);
2811 if (nfs_sigintr(nmp, (struct nfsreq *)0, td->td_proc)) {
2815 if (slpflag == PCATCH) {
2821 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) && commit) {
2825 if (np->n_flag & NWRITEERR) {
2826 error = np->n_error;
2827 np->n_flag &= ~NWRITEERR;
2830 if (bvec != NULL && bvec != bvec_on_stack)
2836 * NFS advisory byte-level locks.
2839 nfs_advlock(struct vop_advlock_args *ap)
2842 return (nfs_dolock(ap));
2846 * Print out the contents of an nfsnode.
2849 nfs_print(struct vop_print_args *ap)
2851 struct vnode *vp = ap->a_vp;
2852 struct nfsnode *np = VTONFS(vp);
2854 printf("tag VT_NFS, fileid %ld fsid 0x%x",
2855 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
2856 if (vp->v_type == VFIFO)
2863 * This is the "real" nfs::bwrite(struct buf*).
2864 * B_WRITEINPROG isn't set unless the force flag is one and it
2865 * handles the B_NEEDCOMMIT flag.
2866 * We set B_CACHE if this is a VMIO buffer.
2869 nfs_writebp(struct buf *bp, int force, struct thread *td)
2872 int oldflags = bp->b_flags;
2878 if (BUF_REFCNT(bp) == 0)
2879 panic("bwrite: buffer is not locked???");
2881 if (bp->b_flags & B_INVAL) {
2886 bp->b_flags |= B_CACHE;
2889 * Undirty the bp. We will redirty it later if the I/O fails.
2894 bp->b_flags &= ~B_DONE;
2895 bp->b_ioflags &= ~BIO_ERROR;
2896 bp->b_iocmd = BIO_WRITE;
2898 bp->b_vp->v_numoutput++;
2899 curthread->td_proc->p_stats->p_ru.ru_oublock++;
2903 * Note: to avoid loopback deadlocks, we do not
2904 * assign b_runningbufspace.
2906 vfs_busy_pages(bp, 1);
2909 bp->b_flags |= B_WRITEINPROG;
2913 if( (oldflags & B_ASYNC) == 0) {
2914 int rtval = bufwait(bp);
2916 if (oldflags & B_DELWRI) {
2918 reassignbuf(bp, bp->b_vp);
2930 * nfs special file access vnode op.
2931 * Essentially just get vattr and then imitate iaccess() since the device is
2932 * local to the client.
2935 nfsspec_access(struct vop_access_args *ap)
2939 struct ucred *cred = ap->a_cred;
2940 struct vnode *vp = ap->a_vp;
2941 mode_t mode = ap->a_mode;
2947 * Disallow write attempts on filesystems mounted read-only;
2948 * unless the file is a socket, fifo, or a block or character
2949 * device resident on the filesystem.
2951 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
2952 switch (vp->v_type) {
2962 * If you're the super-user,
2963 * you always get access.
2965 if (cred->cr_uid == 0)
2968 error = VOP_GETATTR(vp, vap, cred, ap->a_td);
2972 * Access check is based on only one of owner, group, public.
2973 * If not owner, then check group. If not a member of the
2974 * group, then check public access.
2976 if (cred->cr_uid != vap->va_uid) {
2978 gp = cred->cr_groups;
2979 for (i = 0; i < cred->cr_ngroups; i++, gp++)
2980 if (vap->va_gid == *gp)
2986 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
2991 * Read wrapper for special devices.
2994 nfsspec_read(struct vop_read_args *ap)
2996 struct nfsnode *np = VTONFS(ap->a_vp);
3002 getnanotime(&np->n_atim);
3003 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3007 * Write wrapper for special devices.
3010 nfsspec_write(struct vop_write_args *ap)
3012 struct nfsnode *np = VTONFS(ap->a_vp);
3018 getnanotime(&np->n_mtim);
3019 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3023 * Close wrapper for special devices.
3025 * Update the times on the nfsnode then do device close.
3028 nfsspec_close(struct vop_close_args *ap)
3030 struct vnode *vp = ap->a_vp;
3031 struct nfsnode *np = VTONFS(vp);
3034 if (np->n_flag & (NACC | NUPD)) {
3036 if (vp->v_usecount == 1 &&
3037 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3039 if (np->n_flag & NACC)
3040 vattr.va_atime = np->n_atim;
3041 if (np->n_flag & NUPD)
3042 vattr.va_mtime = np->n_mtim;
3043 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_td);
3046 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3050 * Read wrapper for fifos.
3053 nfsfifo_read(struct vop_read_args *ap)
3055 struct nfsnode *np = VTONFS(ap->a_vp);
3061 getnanotime(&np->n_atim);
3062 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3066 * Write wrapper for fifos.
3069 nfsfifo_write(struct vop_write_args *ap)
3071 struct nfsnode *np = VTONFS(ap->a_vp);
3077 getnanotime(&np->n_mtim);
3078 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3082 * Close wrapper for fifos.
3084 * Update the times on the nfsnode then do fifo close.
3087 nfsfifo_close(struct vop_close_args *ap)
3089 struct vnode *vp = ap->a_vp;
3090 struct nfsnode *np = VTONFS(vp);
3094 if (np->n_flag & (NACC | NUPD)) {
3096 if (np->n_flag & NACC)
3098 if (np->n_flag & NUPD)
3101 if (vp->v_usecount == 1 &&
3102 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3104 if (np->n_flag & NACC)
3105 vattr.va_atime = np->n_atim;
3106 if (np->n_flag & NUPD)
3107 vattr.va_mtime = np->n_mtim;
3108 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_td);
3111 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));