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
37 * $FreeBSD: src/sys/nfs/nfs_vnops.c,v 1.150.2.5 2001/12/20 19:56:28 dillon Exp $
38 * $DragonFly: src/sys/vfs/nfs/nfs_vnops.c,v 1.40 2005/04/15 19:08:21 dillon Exp $
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>
55 #include <sys/malloc.h>
57 #include <sys/namei.h>
58 #include <sys/nlookup.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>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_zone.h>
74 #include <vfs/fifofs/fifo.h>
82 #include "nfsm_subs.h"
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
94 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
95 * calls are not in getblk() and brelse() so that they would not be necessary
99 #define vfs_busy_pages(bp, f)
102 static int nfsspec_read (struct vop_read_args *);
103 static int nfsspec_write (struct vop_write_args *);
104 static int nfsfifo_read (struct vop_read_args *);
105 static int nfsfifo_write (struct vop_write_args *);
106 static int nfsspec_close (struct vop_close_args *);
107 static int nfsfifo_close (struct vop_close_args *);
108 #define nfs_poll vop_nopoll
109 static int nfs_setattrrpc (struct vnode *,struct vattr *,struct ucred *,struct thread *);
110 static int nfs_lookup (struct vop_lookup_args *);
111 static int nfs_create (struct vop_create_args *);
112 static int nfs_mknod (struct vop_mknod_args *);
113 static int nfs_open (struct vop_open_args *);
114 static int nfs_close (struct vop_close_args *);
115 static int nfs_access (struct vop_access_args *);
116 static int nfs_getattr (struct vop_getattr_args *);
117 static int nfs_setattr (struct vop_setattr_args *);
118 static int nfs_read (struct vop_read_args *);
119 static int nfs_mmap (struct vop_mmap_args *);
120 static int nfs_fsync (struct vop_fsync_args *);
121 static int nfs_remove (struct vop_remove_args *);
122 static int nfs_link (struct vop_link_args *);
123 static int nfs_rename (struct vop_rename_args *);
124 static int nfs_mkdir (struct vop_mkdir_args *);
125 static int nfs_rmdir (struct vop_rmdir_args *);
126 static int nfs_symlink (struct vop_symlink_args *);
127 static int nfs_readdir (struct vop_readdir_args *);
128 static int nfs_bmap (struct vop_bmap_args *);
129 static int nfs_strategy (struct vop_strategy_args *);
130 static int nfs_lookitup (struct vnode *, const char *, int,
131 struct ucred *, struct thread *, struct nfsnode **);
132 static int nfs_sillyrename (struct vnode *,struct vnode *,struct componentname *);
133 static int nfsspec_access (struct vop_access_args *);
134 static int nfs_readlink (struct vop_readlink_args *);
135 static int nfs_print (struct vop_print_args *);
136 static int nfs_advlock (struct vop_advlock_args *);
137 static int nfs_bwrite (struct vop_bwrite_args *);
139 static int nfs_nresolve (struct vop_nresolve_args *);
141 * Global vfs data structures for nfs
143 struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
144 { &vop_default_desc, vop_defaultop },
145 { &vop_access_desc, (vnodeopv_entry_t) nfs_access },
146 { &vop_advlock_desc, (vnodeopv_entry_t) nfs_advlock },
147 { &vop_bmap_desc, (vnodeopv_entry_t) nfs_bmap },
148 { &vop_bwrite_desc, (vnodeopv_entry_t) nfs_bwrite },
149 { &vop_close_desc, (vnodeopv_entry_t) nfs_close },
150 { &vop_create_desc, (vnodeopv_entry_t) nfs_create },
151 { &vop_fsync_desc, (vnodeopv_entry_t) nfs_fsync },
152 { &vop_getattr_desc, (vnodeopv_entry_t) nfs_getattr },
153 { &vop_getpages_desc, (vnodeopv_entry_t) nfs_getpages },
154 { &vop_putpages_desc, (vnodeopv_entry_t) nfs_putpages },
155 { &vop_inactive_desc, (vnodeopv_entry_t) nfs_inactive },
156 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
157 { &vop_lease_desc, vop_null },
158 { &vop_link_desc, (vnodeopv_entry_t) nfs_link },
159 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
160 { &vop_lookup_desc, (vnodeopv_entry_t) nfs_lookup },
161 { &vop_mkdir_desc, (vnodeopv_entry_t) nfs_mkdir },
162 { &vop_mknod_desc, (vnodeopv_entry_t) nfs_mknod },
163 { &vop_mmap_desc, (vnodeopv_entry_t) nfs_mmap },
164 { &vop_open_desc, (vnodeopv_entry_t) nfs_open },
165 { &vop_poll_desc, (vnodeopv_entry_t) nfs_poll },
166 { &vop_print_desc, (vnodeopv_entry_t) nfs_print },
167 { &vop_read_desc, (vnodeopv_entry_t) nfs_read },
168 { &vop_readdir_desc, (vnodeopv_entry_t) nfs_readdir },
169 { &vop_readlink_desc, (vnodeopv_entry_t) nfs_readlink },
170 { &vop_reclaim_desc, (vnodeopv_entry_t) nfs_reclaim },
171 { &vop_remove_desc, (vnodeopv_entry_t) nfs_remove },
172 { &vop_rename_desc, (vnodeopv_entry_t) nfs_rename },
173 { &vop_rmdir_desc, (vnodeopv_entry_t) nfs_rmdir },
174 { &vop_setattr_desc, (vnodeopv_entry_t) nfs_setattr },
175 { &vop_strategy_desc, (vnodeopv_entry_t) nfs_strategy },
176 { &vop_symlink_desc, (vnodeopv_entry_t) nfs_symlink },
177 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
178 { &vop_write_desc, (vnodeopv_entry_t) nfs_write },
180 { &vop_nresolve_desc, (vnodeopv_entry_t) nfs_nresolve },
185 * Special device vnode ops
187 struct vnodeopv_entry_desc nfsv2_specop_entries[] = {
188 { &vop_default_desc, (vnodeopv_entry_t) spec_vnoperate },
189 { &vop_access_desc, (vnodeopv_entry_t) nfsspec_access },
190 { &vop_close_desc, (vnodeopv_entry_t) nfsspec_close },
191 { &vop_fsync_desc, (vnodeopv_entry_t) nfs_fsync },
192 { &vop_getattr_desc, (vnodeopv_entry_t) nfs_getattr },
193 { &vop_inactive_desc, (vnodeopv_entry_t) nfs_inactive },
194 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
195 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
196 { &vop_print_desc, (vnodeopv_entry_t) nfs_print },
197 { &vop_read_desc, (vnodeopv_entry_t) nfsspec_read },
198 { &vop_reclaim_desc, (vnodeopv_entry_t) nfs_reclaim },
199 { &vop_setattr_desc, (vnodeopv_entry_t) nfs_setattr },
200 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
201 { &vop_write_desc, (vnodeopv_entry_t) nfsspec_write },
205 struct vnodeopv_entry_desc nfsv2_fifoop_entries[] = {
206 { &vop_default_desc, (vnodeopv_entry_t) fifo_vnoperate },
207 { &vop_access_desc, (vnodeopv_entry_t) nfsspec_access },
208 { &vop_close_desc, (vnodeopv_entry_t) nfsfifo_close },
209 { &vop_fsync_desc, (vnodeopv_entry_t) nfs_fsync },
210 { &vop_getattr_desc, (vnodeopv_entry_t) nfs_getattr },
211 { &vop_inactive_desc, (vnodeopv_entry_t) nfs_inactive },
212 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
213 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
214 { &vop_print_desc, (vnodeopv_entry_t) nfs_print },
215 { &vop_read_desc, (vnodeopv_entry_t) nfsfifo_read },
216 { &vop_reclaim_desc, (vnodeopv_entry_t) nfs_reclaim },
217 { &vop_setattr_desc, (vnodeopv_entry_t) nfs_setattr },
218 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
219 { &vop_write_desc, (vnodeopv_entry_t) nfsfifo_write },
223 static int nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
224 struct componentname *cnp,
226 static int nfs_removerpc (struct vnode *dvp, const char *name,
228 struct ucred *cred, struct thread *td);
229 static int nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
230 int fnamelen, struct vnode *tdvp,
231 const char *tnameptr, int tnamelen,
232 struct ucred *cred, struct thread *td);
233 static int nfs_renameit (struct vnode *sdvp,
234 struct componentname *scnp,
235 struct sillyrename *sp);
240 extern u_int32_t nfs_true, nfs_false;
241 extern u_int32_t nfs_xdrneg1;
242 extern struct nfsstats nfsstats;
243 extern nfstype nfsv3_type[9];
244 struct thread *nfs_iodwant[NFS_MAXASYNCDAEMON];
245 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
246 int nfs_numasync = 0;
247 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
249 SYSCTL_DECL(_vfs_nfs);
251 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
252 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
253 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
255 static int nfsneg_cache_timeout = NFS_MINATTRTIMO;
256 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
257 &nfsneg_cache_timeout, 0, "NFS NEGATIVE ACCESS cache timeout");
259 static int nfsv3_commit_on_close = 0;
260 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
261 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
263 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
264 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
266 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
267 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
270 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
271 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
272 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
274 nfs3_access_otw(struct vnode *vp, int wmode,
275 struct thread *td, struct ucred *cred)
279 int error = 0, attrflag;
281 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
282 caddr_t bpos, dpos, cp2;
286 struct nfsnode *np = VTONFS(vp);
288 nfsstats.rpccnt[NFSPROC_ACCESS]++;
289 nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
291 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
292 *tl = txdr_unsigned(wmode);
293 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
294 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
296 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
297 rmode = fxdr_unsigned(u_int32_t, *tl);
299 np->n_modeuid = cred->cr_uid;
300 np->n_modestamp = mycpu->gd_time_seconds;
308 * nfs access vnode op.
309 * For nfs version 2, just return ok. File accesses may fail later.
310 * For nfs version 3, use the access rpc to check accessibility. If file modes
311 * are changed on the server, accesses might still fail later.
313 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
314 * struct thread *a_td)
317 nfs_access(struct vop_access_args *ap)
319 struct vnode *vp = ap->a_vp;
321 u_int32_t mode, wmode;
322 int v3 = NFS_ISV3(vp);
323 struct nfsnode *np = VTONFS(vp);
326 * Disallow write attempts on filesystems mounted read-only;
327 * unless the file is a socket, fifo, or a block or character
328 * device resident on the filesystem.
330 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
331 switch (vp->v_type) {
341 * For nfs v3, check to see if we have done this recently, and if
342 * so return our cached result instead of making an ACCESS call.
343 * If not, do an access rpc, otherwise you are stuck emulating
344 * ufs_access() locally using the vattr. This may not be correct,
345 * since the server may apply other access criteria such as
346 * client uid-->server uid mapping that we do not know about.
349 if (ap->a_mode & VREAD)
350 mode = NFSV3ACCESS_READ;
353 if (vp->v_type != VDIR) {
354 if (ap->a_mode & VWRITE)
355 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
356 if (ap->a_mode & VEXEC)
357 mode |= NFSV3ACCESS_EXECUTE;
359 if (ap->a_mode & VWRITE)
360 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
362 if (ap->a_mode & VEXEC)
363 mode |= NFSV3ACCESS_LOOKUP;
365 /* XXX safety belt, only make blanket request if caching */
366 if (nfsaccess_cache_timeout > 0) {
367 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
368 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
369 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
375 * Does our cached result allow us to give a definite yes to
378 if (np->n_modestamp &&
379 (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
380 (ap->a_cred->cr_uid == np->n_modeuid) &&
381 ((np->n_mode & mode) == mode)) {
382 nfsstats.accesscache_hits++;
385 * Either a no, or a don't know. Go to the wire.
387 nfsstats.accesscache_misses++;
388 error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred);
390 if ((np->n_mode & mode) != mode) {
396 if ((error = nfsspec_access(ap)) != 0)
400 * Attempt to prevent a mapped root from accessing a file
401 * which it shouldn't. We try to read a byte from the file
402 * if the user is root and the file is not zero length.
403 * After calling nfsspec_access, we should have the correct
406 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
407 && VTONFS(vp)->n_size > 0) {
414 auio.uio_iov = &aiov;
418 auio.uio_segflg = UIO_SYSSPACE;
419 auio.uio_rw = UIO_READ;
420 auio.uio_td = ap->a_td;
422 if (vp->v_type == VREG) {
423 error = nfs_readrpc(vp, &auio);
424 } else if (vp->v_type == VDIR) {
426 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
428 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
429 error = nfs_readdirrpc(vp, &auio);
431 } else if (vp->v_type == VLNK) {
432 error = nfs_readlinkrpc(vp, &auio);
439 * [re]record creds for reading and/or writing if access
440 * was granted. Assume the NFS server will grant read access
441 * for execute requests.
444 if ((ap->a_mode & (VREAD|VEXEC)) && ap->a_cred != np->n_rucred) {
447 crfree(np->n_rucred);
448 np->n_rucred = ap->a_cred;
450 if ((ap->a_mode & VWRITE) && ap->a_cred != np->n_wucred) {
453 crfree(np->n_wucred);
454 np->n_wucred = ap->a_cred;
462 * Check to see if the type is ok
463 * and that deletion is not in progress.
464 * For paged in text files, you will need to flush the page cache
465 * if consistency is lost.
467 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
468 * struct thread *a_td)
472 nfs_open(struct vop_open_args *ap)
474 struct vnode *vp = ap->a_vp;
475 struct nfsnode *np = VTONFS(vp);
476 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
480 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
482 printf("open eacces vtyp=%d\n",vp->v_type);
488 * Clear the attribute cache only if opening with write access. It
489 * is unclear if we should do this at all here, but we certainly
490 * should not clear the cache unconditionally simply because a file
493 if (ap->a_mode & FWRITE)
496 if (nmp->nm_flag & NFSMNT_NQNFS) {
498 * If NQNFS is active, get a valid lease
500 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
502 error = nqnfs_getlease(vp, ND_READ, ap->a_td);
503 } while (error == NQNFS_EXPIRED);
506 if (np->n_lrev != np->n_brev ||
507 (np->n_flag & NQNFSNONCACHE)) {
508 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1))
512 np->n_brev = np->n_lrev;
517 * For normal NFS, reconcile changes made locally verses
518 * changes made remotely. Note that VOP_GETATTR only goes
519 * to the wire if the cached attribute has timed out or been
522 * If local modifications have been made clear the attribute
523 * cache to force an attribute and modified time check. If
524 * GETATTR detects that the file has been changed by someone
525 * other then us it will set NRMODIFIED.
527 * If we are opening a directory and local changes have been
528 * made we have to invalidate the cache in order to ensure
529 * that we get the most up-to-date information from the
532 if (np->n_flag & NLMODIFIED) {
534 if (vp->v_type == VDIR) {
535 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
541 error = VOP_GETATTR(vp, &vattr, ap->a_td);
544 if (np->n_flag & NRMODIFIED) {
545 if (vp->v_type == VDIR)
547 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
550 np->n_flag &= ~NRMODIFIED;
559 * What an NFS client should do upon close after writing is a debatable issue.
560 * Most NFS clients push delayed writes to the server upon close, basically for
562 * 1 - So that any write errors may be reported back to the client process
563 * doing the close system call. By far the two most likely errors are
564 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
565 * 2 - To put a worst case upper bound on cache inconsistency between
566 * multiple clients for the file.
567 * There is also a consistency problem for Version 2 of the protocol w.r.t.
568 * not being able to tell if other clients are writing a file concurrently,
569 * since there is no way of knowing if the changed modify time in the reply
570 * is only due to the write for this client.
571 * (NFS Version 3 provides weak cache consistency data in the reply that
572 * should be sufficient to detect and handle this case.)
574 * The current code does the following:
575 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
576 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
577 * or commit them (this satisfies 1 and 2 except for the
578 * case where the server crashes after this close but
579 * before the commit RPC, which is felt to be "good
580 * enough". Changing the last argument to nfs_flush() to
581 * a 1 would force a commit operation, if it is felt a
582 * commit is necessary now.
583 * for NQNFS - do nothing now, since 2 is dealt with via leases and
584 * 1 should be dealt with via an fsync() system call for
585 * cases where write errors are important.
587 * nfs_close(struct vnodeop_desc *a_desc, struct vnode *a_vp, int a_fflag,
588 * struct ucred *a_cred, struct thread *a_td)
592 nfs_close(struct vop_close_args *ap)
594 struct vnode *vp = ap->a_vp;
595 struct nfsnode *np = VTONFS(vp);
598 if (vp->v_type == VREG) {
599 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 &&
600 (np->n_flag & NLMODIFIED)) {
603 * Under NFSv3 we have dirty buffers to dispose of. We
604 * must flush them to the NFS server. We have the option
605 * of waiting all the way through the commit rpc or just
606 * waiting for the initial write. The default is to only
607 * wait through the initial write so the data is in the
608 * server's cache, which is roughly similar to the state
609 * a standard disk subsystem leaves the file in on close().
611 * We cannot clear the NLMODIFIED bit in np->n_flag due to
612 * potential races with other processes, and certainly
613 * cannot clear it if we don't commit.
615 int cm = nfsv3_commit_on_close ? 1 : 0;
616 error = nfs_flush(vp, MNT_WAIT, ap->a_td, cm);
617 /* np->n_flag &= ~NLMODIFIED; */
619 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
623 if (np->n_flag & NWRITEERR) {
624 np->n_flag &= ~NWRITEERR;
632 * nfs getattr call from vfs.
634 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred,
635 * struct thread *a_td)
638 nfs_getattr(struct vop_getattr_args *ap)
640 struct vnode *vp = ap->a_vp;
641 struct nfsnode *np = VTONFS(vp);
647 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
648 int v3 = NFS_ISV3(vp);
651 * Update local times for special files.
653 if (np->n_flag & (NACC | NUPD))
656 * First look in the cache.
658 if (nfs_getattrcache(vp, ap->a_vap) == 0)
661 if (v3 && nfsaccess_cache_timeout > 0) {
662 nfsstats.accesscache_misses++;
663 nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_td, nfs_vpcred(vp, ND_CHECK));
664 if (nfs_getattrcache(vp, ap->a_vap) == 0)
668 nfsstats.rpccnt[NFSPROC_GETATTR]++;
669 nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
671 nfsm_request(vp, NFSPROC_GETATTR, ap->a_td, nfs_vpcred(vp, ND_CHECK));
673 nfsm_loadattr(vp, ap->a_vap);
683 * nfs_setattr(struct vnodeop_desc *a_desc, struct vnode *a_vp,
684 * struct vattr *a_vap, struct ucred *a_cred,
685 * struct thread *a_td)
688 nfs_setattr(struct vop_setattr_args *ap)
690 struct vnode *vp = ap->a_vp;
691 struct nfsnode *np = VTONFS(vp);
692 struct vattr *vap = ap->a_vap;
701 * Setting of flags is not supported.
703 if (vap->va_flags != VNOVAL)
707 * Disallow write attempts if the filesystem is mounted read-only.
709 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
710 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
711 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
712 (vp->v_mount->mnt_flag & MNT_RDONLY))
714 if (vap->va_size != VNOVAL) {
715 switch (vp->v_type) {
722 if (vap->va_mtime.tv_sec == VNOVAL &&
723 vap->va_atime.tv_sec == VNOVAL &&
724 vap->va_mode == (mode_t)VNOVAL &&
725 vap->va_uid == (uid_t)VNOVAL &&
726 vap->va_gid == (gid_t)VNOVAL)
728 vap->va_size = VNOVAL;
732 * Disallow write attempts if the filesystem is
735 if (vp->v_mount->mnt_flag & MNT_RDONLY)
739 * This is nasty. The RPCs we send to flush pending
740 * data often return attribute information which is
741 * cached via a callback to nfs_loadattrcache(), which
742 * has the effect of changing our notion of the file
743 * size. Due to flushed appends and other operations
744 * the file size can be set to virtually anything,
745 * including values that do not match either the old
746 * or intended file size.
748 * When this condition is detected we must loop to
749 * try the operation again. Hopefully no more
750 * flushing is required on the loop so it works the
751 * second time around. THIS CASE ALMOST ALWAYS
756 error = nfs_meta_setsize(vp, ap->a_td, vap->va_size);
758 if (np->n_flag & NLMODIFIED) {
759 if (vap->va_size == 0)
760 error = nfs_vinvalbuf(vp, 0, ap->a_td, 1);
762 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
765 * note: this loop case almost always happens at
766 * least once per truncation.
768 if (error == 0 && np->n_size != vap->va_size)
770 np->n_vattr.va_size = vap->va_size;
773 } else if ((vap->va_mtime.tv_sec != VNOVAL ||
774 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NLMODIFIED) &&
775 vp->v_type == VREG &&
776 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) == EINTR
780 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
783 * Sanity check if a truncation was issued. This should only occur
784 * if multiple processes are racing on the same file.
786 if (error == 0 && vap->va_size != VNOVAL &&
787 np->n_size != vap->va_size) {
788 printf("NFS ftruncate: server disagrees on the file size: %lld/%lld/%lld\n", tsize, vap->va_size, np->n_size);
791 if (error && vap->va_size != VNOVAL) {
792 np->n_size = np->n_vattr.va_size = tsize;
793 vnode_pager_setsize(vp, np->n_size);
799 * Do an nfs setattr rpc.
802 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
803 struct ucred *cred, struct thread *td)
805 struct nfsv2_sattr *sp;
806 struct nfsnode *np = VTONFS(vp);
809 caddr_t bpos, dpos, cp2;
811 int error = 0, wccflag = NFSV3_WCCRATTR;
812 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
813 int v3 = NFS_ISV3(vp);
815 nfsstats.rpccnt[NFSPROC_SETATTR]++;
816 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
819 nfsm_v3attrbuild(vap, TRUE);
820 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
823 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
824 if (vap->va_mode == (mode_t)VNOVAL)
825 sp->sa_mode = nfs_xdrneg1;
827 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
828 if (vap->va_uid == (uid_t)VNOVAL)
829 sp->sa_uid = nfs_xdrneg1;
831 sp->sa_uid = txdr_unsigned(vap->va_uid);
832 if (vap->va_gid == (gid_t)VNOVAL)
833 sp->sa_gid = nfs_xdrneg1;
835 sp->sa_gid = txdr_unsigned(vap->va_gid);
836 sp->sa_size = txdr_unsigned(vap->va_size);
837 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
838 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
840 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
843 nfsm_wcc_data(vp, wccflag);
845 nfsm_loadattr(vp, (struct vattr *)0);
852 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
853 * nfs_lookup() until all remaining new api calls are implemented.
855 * Resolve a namecache entry. This function is passed a locked ncp and
856 * must call cache_setvp() on it as appropriate to resolve the entry.
859 nfs_nresolve(struct vop_nresolve_args *ap)
861 struct thread *td = curthread;
862 struct namecache *ncp;
873 /******NFSM MACROS********/
874 struct mbuf *mb, *mrep, *mreq, *mb2, *md;
875 caddr_t bpos, dpos, cp, cp2;
882 KKASSERT(ncp->nc_parent && ncp->nc_parent->nc_vp);
883 dvp = ncp->nc_parent->nc_vp;
884 if ((error = vget(dvp, LK_SHARED, td)) != 0)
889 nfsstats.lookupcache_misses++;
890 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
892 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
893 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
895 nfsm_strtom(ncp->nc_name, len, NFS_MAXNAMLEN);
896 nfsm_request(dvp, NFSPROC_LOOKUP, td, ap->a_cred);
899 * Cache negatve lookups to reduce NFS traffic, but use
900 * a fast timeout. Otherwise use a timeout of 1 tick.
901 * XXX we should add a namecache flag for no-caching
902 * to uncache the negative hit as soon as possible, but
903 * we cannot simply destroy the entry because it is used
904 * as a placeholder by the caller.
906 if (error == ENOENT) {
909 if (nfsneg_cache_timeout)
910 nticks = nfsneg_cache_timeout * hz;
913 cache_setvp(ncp, NULL);
914 cache_settimeout(ncp, nticks);
916 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
922 * Success, get the file handle, do various checks, and load
923 * post-operation data from the reply packet. Theoretically
924 * we should never be looking up "." so, theoretically, we
925 * should never get the same file handle as our directory. But
926 * we check anyway. XXX
928 * Note that no timeout is set for the positive cache hit. We
929 * assume, theoretically, that ESTALE returns will be dealt with
930 * properly to handle NFS races and in anycase we cannot depend
931 * on a timeout to deal with NFS open/create/excl issues so instead
932 * of a bad hack here the rest of the NFS client code needs to do
935 nfsm_getfh(fhp, fhsize, v3);
938 if (NFS_CMPFH(np, fhp, fhsize)) {
942 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
951 nfsm_postop_attr(nvp, attrflag, NFS_LATTR_NOSHRINK);
952 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
954 nfsm_loadattr(nvp, NULL);
956 cache_setvp(ncp, nvp);
970 * 'cached' nfs directory lookup
972 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
974 * nfs_lookup(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
975 * struct vnode **a_vpp, struct componentname *a_cnp)
978 nfs_lookup(struct vop_lookup_args *ap)
980 struct componentname *cnp = ap->a_cnp;
981 struct vnode *dvp = ap->a_dvp;
982 struct vnode **vpp = ap->a_vpp;
983 int flags = cnp->cn_flags;
988 struct nfsmount *nmp;
989 caddr_t bpos, dpos, cp2;
990 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
994 int lockparent, wantparent, error = 0, attrflag, fhsize;
995 int v3 = NFS_ISV3(dvp);
996 struct thread *td = cnp->cn_td;
999 * Read-only mount check and directory check.
1002 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1003 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
1006 if (dvp->v_type != VDIR)
1010 * Look it up in the cache. Note that ENOENT is only returned if we
1011 * previously entered a negative hit (see later on). The additional
1012 * nfsneg_cache_timeout check causes previously cached results to
1013 * be instantly ignored if the negative caching is turned off.
1015 lockparent = flags & CNP_LOCKPARENT;
1016 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1017 nmp = VFSTONFS(dvp->v_mount);
1025 nfsstats.lookupcache_misses++;
1026 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1027 len = cnp->cn_namelen;
1028 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
1029 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1030 nfsm_fhtom(dvp, v3);
1031 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1032 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
1034 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1038 nfsm_getfh(fhp, fhsize, v3);
1041 * Handle RENAME case...
1043 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1044 if (NFS_CMPFH(np, fhp, fhsize)) {
1048 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1055 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1056 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1058 nfsm_loadattr(newvp, (struct vattr *)0);
1062 VOP_UNLOCK(dvp, 0, td);
1063 cnp->cn_flags |= CNP_PDIRUNLOCK;
1068 if (flags & CNP_ISDOTDOT) {
1069 VOP_UNLOCK(dvp, 0, td);
1070 cnp->cn_flags |= CNP_PDIRUNLOCK;
1071 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1073 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
1074 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1075 return (error); /* NOTE: return error from nget */
1079 error = vn_lock(dvp, LK_EXCLUSIVE, td);
1084 cnp->cn_flags |= CNP_PDIRUNLOCK;
1086 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1090 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1096 VOP_UNLOCK(dvp, 0, td);
1097 cnp->cn_flags |= CNP_PDIRUNLOCK;
1102 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1103 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1105 nfsm_loadattr(newvp, (struct vattr *)0);
1107 /* XXX MOVE TO nfs_nremove() */
1108 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1109 cnp->cn_nameiop != NAMEI_DELETE) {
1110 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1117 if (newvp != NULLVP) {
1121 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1122 cnp->cn_nameiop == NAMEI_RENAME) &&
1125 VOP_UNLOCK(dvp, 0, td);
1126 cnp->cn_flags |= CNP_PDIRUNLOCK;
1128 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1131 error = EJUSTRETURN;
1139 * Just call nfs_bioread() to do the work.
1141 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1142 * struct ucred *a_cred)
1145 nfs_read(struct vop_read_args *ap)
1147 struct vnode *vp = ap->a_vp;
1149 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1150 switch (vp->v_type) {
1152 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1163 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1166 nfs_readlink(struct vop_readlink_args *ap)
1168 struct vnode *vp = ap->a_vp;
1170 if (vp->v_type != VLNK)
1172 return (nfs_bioread(vp, ap->a_uio, 0));
1176 * Do a readlink rpc.
1177 * Called by nfs_doio() from below the buffer cache.
1180 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1185 caddr_t bpos, dpos, cp2;
1186 int error = 0, len, attrflag;
1187 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1188 int v3 = NFS_ISV3(vp);
1190 nfsstats.rpccnt[NFSPROC_READLINK]++;
1191 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1193 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1195 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1197 nfsm_strsiz(len, NFS_MAXPATHLEN);
1198 if (len == NFS_MAXPATHLEN) {
1199 struct nfsnode *np = VTONFS(vp);
1200 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1203 nfsm_mtouio(uiop, len);
1215 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1220 caddr_t bpos, dpos, cp2;
1221 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1222 struct nfsmount *nmp;
1223 int error = 0, len, retlen, tsiz, eof, attrflag;
1224 int v3 = NFS_ISV3(vp);
1229 nmp = VFSTONFS(vp->v_mount);
1230 tsiz = uiop->uio_resid;
1231 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1234 nfsstats.rpccnt[NFSPROC_READ]++;
1235 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1236 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1238 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1240 txdr_hyper(uiop->uio_offset, tl);
1241 *(tl + 2) = txdr_unsigned(len);
1243 *tl++ = txdr_unsigned(uiop->uio_offset);
1244 *tl++ = txdr_unsigned(len);
1247 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1249 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1254 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1255 eof = fxdr_unsigned(int, *(tl + 1));
1257 nfsm_loadattr(vp, (struct vattr *)0);
1258 nfsm_strsiz(retlen, nmp->nm_rsize);
1259 nfsm_mtouio(uiop, retlen);
1263 if (eof || retlen == 0) {
1266 } else if (retlen < len) {
1278 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, int *must_commit)
1282 int32_t t1, t2, backup;
1283 caddr_t bpos, dpos, cp2;
1284 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1285 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1286 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1287 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1290 if (uiop->uio_iovcnt != 1)
1291 panic("nfs: writerpc iovcnt > 1");
1294 tsiz = uiop->uio_resid;
1295 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1298 nfsstats.rpccnt[NFSPROC_WRITE]++;
1299 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1300 nfsm_reqhead(vp, NFSPROC_WRITE,
1301 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1304 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1305 txdr_hyper(uiop->uio_offset, tl);
1307 *tl++ = txdr_unsigned(len);
1308 *tl++ = txdr_unsigned(*iomode);
1309 *tl = txdr_unsigned(len);
1313 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1314 /* Set both "begin" and "current" to non-garbage. */
1315 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1316 *tl++ = x; /* "begin offset" */
1317 *tl++ = x; /* "current offset" */
1318 x = txdr_unsigned(len);
1319 *tl++ = x; /* total to this offset */
1320 *tl = x; /* size of this write */
1322 nfsm_uiotom(uiop, len);
1323 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1326 * The write RPC returns a before and after mtime. The
1327 * nfsm_wcc_data() macro checks the before n_mtime
1328 * against the before time and stores the after time
1329 * in the nfsnode's cached vattr and n_mtime field.
1330 * The NRMODIFIED bit will be set if the before
1331 * time did not match the original mtime.
1333 wccflag = NFSV3_WCCCHK;
1334 nfsm_wcc_data(vp, wccflag);
1336 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1337 + NFSX_V3WRITEVERF);
1338 rlen = fxdr_unsigned(int, *tl++);
1343 } else if (rlen < len) {
1344 backup = len - rlen;
1345 uiop->uio_iov->iov_base -= backup;
1346 uiop->uio_iov->iov_len += backup;
1347 uiop->uio_offset -= backup;
1348 uiop->uio_resid += backup;
1351 commit = fxdr_unsigned(int, *tl++);
1354 * Return the lowest committment level
1355 * obtained by any of the RPCs.
1357 if (committed == NFSV3WRITE_FILESYNC)
1359 else if (committed == NFSV3WRITE_DATASYNC &&
1360 commit == NFSV3WRITE_UNSTABLE)
1362 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1363 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1365 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1366 } else if (bcmp((caddr_t)tl,
1367 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1369 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1374 nfsm_loadattr(vp, (struct vattr *)0);
1382 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1383 committed = NFSV3WRITE_FILESYNC;
1384 *iomode = committed;
1386 uiop->uio_resid = tsiz;
1392 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1393 * mode set to specify the file type and the size field for rdev.
1396 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1399 struct nfsv2_sattr *sp;
1403 struct vnode *newvp = (struct vnode *)0;
1404 struct nfsnode *np = (struct nfsnode *)0;
1408 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1409 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1411 int v3 = NFS_ISV3(dvp);
1413 if (vap->va_type == VCHR || vap->va_type == VBLK)
1414 rdev = txdr_unsigned(vap->va_rdev);
1415 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1418 return (EOPNOTSUPP);
1420 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1423 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1424 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1425 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1426 nfsm_fhtom(dvp, v3);
1427 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1429 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1430 *tl++ = vtonfsv3_type(vap->va_type);
1431 nfsm_v3attrbuild(vap, FALSE);
1432 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1433 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1434 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1435 *tl = txdr_unsigned(uminor(vap->va_rdev));
1438 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1439 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1440 sp->sa_uid = nfs_xdrneg1;
1441 sp->sa_gid = nfs_xdrneg1;
1443 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1444 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1446 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1448 nfsm_mtofh(dvp, newvp, v3, gotvp);
1452 newvp = (struct vnode *)0;
1454 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1455 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1461 nfsm_wcc_data(dvp, wccflag);
1470 VTONFS(dvp)->n_flag |= NLMODIFIED;
1472 VTONFS(dvp)->n_attrstamp = 0;
1478 * just call nfs_mknodrpc() to do the work.
1480 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1481 * struct componentname *a_cnp, struct vattr *a_vap)
1485 nfs_mknod(struct vop_mknod_args *ap)
1487 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1490 static u_long create_verf;
1492 * nfs file create call
1494 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1495 * struct componentname *a_cnp, struct vattr *a_vap)
1498 nfs_create(struct vop_create_args *ap)
1500 struct vnode *dvp = ap->a_dvp;
1501 struct vattr *vap = ap->a_vap;
1502 struct componentname *cnp = ap->a_cnp;
1503 struct nfsv2_sattr *sp;
1507 struct nfsnode *np = (struct nfsnode *)0;
1508 struct vnode *newvp = (struct vnode *)0;
1509 caddr_t bpos, dpos, cp2;
1510 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1511 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1513 int v3 = NFS_ISV3(dvp);
1516 * Oops, not for me..
1518 if (vap->va_type == VSOCK)
1519 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1521 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1524 if (vap->va_vaflags & VA_EXCLUSIVE)
1527 nfsstats.rpccnt[NFSPROC_CREATE]++;
1528 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1529 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1530 nfsm_fhtom(dvp, v3);
1531 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1533 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1534 if (fmode & O_EXCL) {
1535 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1536 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1538 if (!TAILQ_EMPTY(&in_ifaddrhead))
1539 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr;
1542 *tl++ = create_verf;
1543 *tl = ++create_verf;
1545 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1546 nfsm_v3attrbuild(vap, FALSE);
1549 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1550 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1551 sp->sa_uid = nfs_xdrneg1;
1552 sp->sa_gid = nfs_xdrneg1;
1554 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1555 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1557 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1559 nfsm_mtofh(dvp, newvp, v3, gotvp);
1563 newvp = (struct vnode *)0;
1565 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1566 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1572 nfsm_wcc_data(dvp, wccflag);
1576 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1582 } else if (v3 && (fmode & O_EXCL)) {
1584 * We are normally called with only a partially initialized
1585 * VAP. Since the NFSv3 spec says that server may use the
1586 * file attributes to store the verifier, the spec requires
1587 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1588 * in atime, but we can't really assume that all servers will
1589 * so we ensure that our SETATTR sets both atime and mtime.
1591 if (vap->va_mtime.tv_sec == VNOVAL)
1592 vfs_timestamp(&vap->va_mtime);
1593 if (vap->va_atime.tv_sec == VNOVAL)
1594 vap->va_atime = vap->va_mtime;
1595 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1599 * The new np may have enough info for access
1600 * checks, make sure rucred and wucred are
1601 * initialized for read and write rpc's.
1604 if (np->n_rucred == NULL)
1605 np->n_rucred = crhold(cnp->cn_cred);
1606 if (np->n_wucred == NULL)
1607 np->n_wucred = crhold(cnp->cn_cred);
1610 VTONFS(dvp)->n_flag |= NLMODIFIED;
1612 VTONFS(dvp)->n_attrstamp = 0;
1617 * nfs file remove call
1618 * To try and make nfs semantics closer to ufs semantics, a file that has
1619 * other processes using the vnode is renamed instead of removed and then
1620 * removed later on the last close.
1621 * - If v_usecount > 1
1622 * If a rename is not already in the works
1623 * call nfs_sillyrename() to set it up
1627 * nfs_remove(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
1628 * struct vnode *a_vp, struct componentname *a_cnp)
1631 nfs_remove(struct vop_remove_args *ap)
1633 struct vnode *vp = ap->a_vp;
1634 struct vnode *dvp = ap->a_dvp;
1635 struct componentname *cnp = ap->a_cnp;
1636 struct nfsnode *np = VTONFS(vp);
1641 if (vp->v_usecount < 1)
1642 panic("nfs_remove: bad v_usecount");
1644 if (vp->v_type == VDIR)
1646 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1647 VOP_GETATTR(vp, &vattr, cnp->cn_td) == 0 &&
1648 vattr.va_nlink > 1)) {
1650 * throw away biocache buffers, mainly to avoid
1651 * unnecessary delayed writes later.
1653 error = nfs_vinvalbuf(vp, 0, cnp->cn_td, 1);
1656 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1657 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1659 * Kludge City: If the first reply to the remove rpc is lost..
1660 * the reply to the retransmitted request will be ENOENT
1661 * since the file was in fact removed
1662 * Therefore, we cheat and return success.
1664 if (error == ENOENT)
1666 } else if (!np->n_sillyrename) {
1667 error = nfs_sillyrename(dvp, vp, cnp);
1669 np->n_attrstamp = 0;
1674 * nfs file remove rpc called from nfs_inactive
1677 nfs_removeit(struct sillyrename *sp)
1679 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1684 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1687 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1688 struct ucred *cred, struct thread *td)
1693 caddr_t bpos, dpos, cp2;
1694 int error = 0, wccflag = NFSV3_WCCRATTR;
1695 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1696 int v3 = NFS_ISV3(dvp);
1698 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1699 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1700 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1701 nfsm_fhtom(dvp, v3);
1702 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1703 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1705 nfsm_wcc_data(dvp, wccflag);
1708 VTONFS(dvp)->n_flag |= NLMODIFIED;
1710 VTONFS(dvp)->n_attrstamp = 0;
1715 * nfs file rename call
1717 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1718 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1719 * struct vnode *a_tvp, struct componentname *a_tcnp)
1722 nfs_rename(struct vop_rename_args *ap)
1724 struct vnode *fvp = ap->a_fvp;
1725 struct vnode *tvp = ap->a_tvp;
1726 struct vnode *fdvp = ap->a_fdvp;
1727 struct vnode *tdvp = ap->a_tdvp;
1728 struct componentname *tcnp = ap->a_tcnp;
1729 struct componentname *fcnp = ap->a_fcnp;
1732 /* Check for cross-device rename */
1733 if ((fvp->v_mount != tdvp->v_mount) ||
1734 (tvp && (fvp->v_mount != tvp->v_mount))) {
1740 * We have to flush B_DELWRI data prior to renaming
1741 * the file. If we don't, the delayed-write buffers
1742 * can be flushed out later after the file has gone stale
1743 * under NFSV3. NFSV2 does not have this problem because
1744 * ( as far as I can tell ) it flushes dirty buffers more
1748 VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_td);
1750 VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_td);
1753 * If the tvp exists and is in use, sillyrename it before doing the
1754 * rename of the new file over it.
1756 * XXX Can't sillyrename a directory.
1758 * We do not attempt to do any namecache purges in this old API
1759 * routine. The new API compat functions have access to the actual
1760 * namecache structures and will do it for us.
1762 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1763 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1770 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1771 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1784 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1786 if (error == ENOENT)
1792 * nfs file rename rpc called from nfs_remove() above
1795 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1796 struct sillyrename *sp)
1798 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1799 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1803 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1806 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1807 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1808 struct ucred *cred, struct thread *td)
1813 caddr_t bpos, dpos, cp2;
1814 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1815 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1816 int v3 = NFS_ISV3(fdvp);
1818 nfsstats.rpccnt[NFSPROC_RENAME]++;
1819 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1820 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1821 nfsm_rndup(tnamelen));
1822 nfsm_fhtom(fdvp, v3);
1823 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1824 nfsm_fhtom(tdvp, v3);
1825 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1826 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1828 nfsm_wcc_data(fdvp, fwccflag);
1829 nfsm_wcc_data(tdvp, twccflag);
1833 VTONFS(fdvp)->n_flag |= NLMODIFIED;
1834 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1836 VTONFS(fdvp)->n_attrstamp = 0;
1838 VTONFS(tdvp)->n_attrstamp = 0;
1843 * nfs hard link create call
1845 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1846 * struct componentname *a_cnp)
1849 nfs_link(struct vop_link_args *ap)
1851 struct vnode *vp = ap->a_vp;
1852 struct vnode *tdvp = ap->a_tdvp;
1853 struct componentname *cnp = ap->a_cnp;
1857 caddr_t bpos, dpos, cp2;
1858 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1859 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1862 if (vp->v_mount != tdvp->v_mount) {
1867 * Push all writes to the server, so that the attribute cache
1868 * doesn't get "out of sync" with the server.
1869 * XXX There should be a better way!
1871 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_td);
1874 nfsstats.rpccnt[NFSPROC_LINK]++;
1875 nfsm_reqhead(vp, NFSPROC_LINK,
1876 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1878 nfsm_fhtom(tdvp, v3);
1879 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1880 nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1882 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1883 nfsm_wcc_data(tdvp, wccflag);
1887 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1889 VTONFS(vp)->n_attrstamp = 0;
1891 VTONFS(tdvp)->n_attrstamp = 0;
1893 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1895 if (error == EEXIST)
1901 * nfs symbolic link create call
1903 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1904 * struct componentname *a_cnp, struct vattr *a_vap,
1908 nfs_symlink(struct vop_symlink_args *ap)
1910 struct vnode *dvp = ap->a_dvp;
1911 struct vattr *vap = ap->a_vap;
1912 struct componentname *cnp = ap->a_cnp;
1913 struct nfsv2_sattr *sp;
1917 caddr_t bpos, dpos, cp2;
1918 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1919 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1920 struct vnode *newvp = (struct vnode *)0;
1921 int v3 = NFS_ISV3(dvp);
1923 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1924 slen = strlen(ap->a_target);
1925 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1926 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1927 nfsm_fhtom(dvp, v3);
1928 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1930 nfsm_v3attrbuild(vap, FALSE);
1932 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1934 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1935 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1936 sp->sa_uid = nfs_xdrneg1;
1937 sp->sa_gid = nfs_xdrneg1;
1938 sp->sa_size = nfs_xdrneg1;
1939 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1940 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1944 * Issue the NFS request and get the rpc response.
1946 * Only NFSv3 responses returning an error of 0 actually return
1947 * a file handle that can be converted into newvp without having
1948 * to do an extra lookup rpc.
1950 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1953 nfsm_mtofh(dvp, newvp, v3, gotvp);
1954 nfsm_wcc_data(dvp, wccflag);
1958 * out code jumps -> here, mrep is also freed.
1965 * If we get an EEXIST error, silently convert it to no-error
1966 * in case of an NFS retry.
1968 if (error == EEXIST)
1972 * If we do not have (or no longer have) an error, and we could
1973 * not extract the newvp from the response due to the request being
1974 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1975 * to obtain a newvp to return.
1977 if (error == 0 && newvp == NULL) {
1978 struct nfsnode *np = NULL;
1980 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1981 cnp->cn_cred, cnp->cn_td, &np);
1991 VTONFS(dvp)->n_flag |= NLMODIFIED;
1993 VTONFS(dvp)->n_attrstamp = 0;
2000 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2001 * struct componentname *a_cnp, struct vattr *a_vap)
2004 nfs_mkdir(struct vop_mkdir_args *ap)
2006 struct vnode *dvp = ap->a_dvp;
2007 struct vattr *vap = ap->a_vap;
2008 struct componentname *cnp = ap->a_cnp;
2009 struct nfsv2_sattr *sp;
2014 struct nfsnode *np = (struct nfsnode *)0;
2015 struct vnode *newvp = (struct vnode *)0;
2016 caddr_t bpos, dpos, cp2;
2017 int error = 0, wccflag = NFSV3_WCCRATTR;
2019 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2021 int v3 = NFS_ISV3(dvp);
2023 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
2026 len = cnp->cn_namelen;
2027 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2028 nfsm_reqhead(dvp, NFSPROC_MKDIR,
2029 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2030 nfsm_fhtom(dvp, v3);
2031 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
2033 nfsm_v3attrbuild(vap, FALSE);
2035 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2036 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2037 sp->sa_uid = nfs_xdrneg1;
2038 sp->sa_gid = nfs_xdrneg1;
2039 sp->sa_size = nfs_xdrneg1;
2040 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2041 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2043 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
2045 nfsm_mtofh(dvp, newvp, v3, gotvp);
2047 nfsm_wcc_data(dvp, wccflag);
2050 VTONFS(dvp)->n_flag |= NLMODIFIED;
2052 VTONFS(dvp)->n_attrstamp = 0;
2054 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2055 * if we can succeed in looking up the directory.
2057 if (error == EEXIST || (!error && !gotvp)) {
2060 newvp = (struct vnode *)0;
2062 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2066 if (newvp->v_type != VDIR)
2079 * nfs remove directory call
2081 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2082 * struct componentname *a_cnp)
2085 nfs_rmdir(struct vop_rmdir_args *ap)
2087 struct vnode *vp = ap->a_vp;
2088 struct vnode *dvp = ap->a_dvp;
2089 struct componentname *cnp = ap->a_cnp;
2093 caddr_t bpos, dpos, cp2;
2094 int error = 0, wccflag = NFSV3_WCCRATTR;
2095 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2096 int v3 = NFS_ISV3(dvp);
2100 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2101 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2102 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2103 nfsm_fhtom(dvp, v3);
2104 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2105 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2107 nfsm_wcc_data(dvp, wccflag);
2110 VTONFS(dvp)->n_flag |= NLMODIFIED;
2112 VTONFS(dvp)->n_attrstamp = 0;
2114 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2116 if (error == ENOENT)
2124 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2127 nfs_readdir(struct vop_readdir_args *ap)
2129 struct vnode *vp = ap->a_vp;
2130 struct nfsnode *np = VTONFS(vp);
2131 struct uio *uio = ap->a_uio;
2135 if (vp->v_type != VDIR)
2139 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2140 * and then check that is still valid, or if this is an NQNFS mount
2141 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2142 * VOP_GETATTR() does not necessarily go to the wire.
2144 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2145 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2146 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
2147 if (NQNFS_CKCACHABLE(vp, ND_READ)) {
2148 nfsstats.direofcache_hits++;
2151 } else if (VOP_GETATTR(vp, &vattr, uio->uio_td) == 0 &&
2152 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2154 nfsstats.direofcache_hits++;
2160 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2161 * own cache coherency checks so we do not have to.
2163 tresid = uio->uio_resid;
2164 error = nfs_bioread(vp, uio, 0);
2166 if (!error && uio->uio_resid == tresid)
2167 nfsstats.direofcache_misses++;
2173 * Called from below the buffer cache by nfs_doio().
2176 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2179 struct dirent *dp = NULL;
2184 caddr_t bpos, dpos, cp2;
2185 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2187 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2188 struct nfsnode *dnp = VTONFS(vp);
2190 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2192 int v3 = NFS_ISV3(vp);
2195 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2196 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2197 panic("nfs readdirrpc bad uio");
2201 * If there is no cookie, assume directory was stale.
2203 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2207 return (NFSERR_BAD_COOKIE);
2209 * Loop around doing readdir rpc's of size nm_readdirsize
2210 * truncated to a multiple of DIRBLKSIZ.
2211 * The stopping criteria is EOF or buffer full.
2213 while (more_dirs && bigenough) {
2214 nfsstats.rpccnt[NFSPROC_READDIR]++;
2215 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2219 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2220 *tl++ = cookie.nfsuquad[0];
2221 *tl++ = cookie.nfsuquad[1];
2222 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2223 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2225 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2226 *tl++ = cookie.nfsuquad[0];
2228 *tl = txdr_unsigned(nmp->nm_readdirsize);
2229 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2231 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2233 nfsm_dissect(tl, u_int32_t *,
2235 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2236 dnp->n_cookieverf.nfsuquad[1] = *tl;
2242 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2243 more_dirs = fxdr_unsigned(int, *tl);
2245 /* loop thru the dir entries, doctoring them to 4bsd form */
2246 while (more_dirs && bigenough) {
2248 nfsm_dissect(tl, u_int32_t *,
2250 fileno = fxdr_hyper(tl);
2251 len = fxdr_unsigned(int, *(tl + 2));
2253 nfsm_dissect(tl, u_int32_t *,
2255 fileno = fxdr_unsigned(u_quad_t, *tl++);
2256 len = fxdr_unsigned(int, *tl);
2258 if (len <= 0 || len > NFS_MAXNAMLEN) {
2263 tlen = nfsm_rndup(len);
2265 tlen += 4; /* To ensure null termination */
2266 left = DIRBLKSIZ - blksiz;
2267 if ((tlen + DIRHDSIZ) > left) {
2268 dp->d_reclen += left;
2269 uiop->uio_iov->iov_base += left;
2270 uiop->uio_iov->iov_len -= left;
2271 uiop->uio_offset += left;
2272 uiop->uio_resid -= left;
2275 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2278 dp = (struct dirent *)uiop->uio_iov->iov_base;
2279 dp->d_fileno = (int)fileno;
2281 dp->d_reclen = tlen + DIRHDSIZ;
2282 dp->d_type = DT_UNKNOWN;
2283 blksiz += dp->d_reclen;
2284 if (blksiz == DIRBLKSIZ)
2286 uiop->uio_offset += DIRHDSIZ;
2287 uiop->uio_resid -= DIRHDSIZ;
2288 uiop->uio_iov->iov_base += DIRHDSIZ;
2289 uiop->uio_iov->iov_len -= DIRHDSIZ;
2290 nfsm_mtouio(uiop, len);
2291 cp = uiop->uio_iov->iov_base;
2293 *cp = '\0'; /* null terminate */
2294 uiop->uio_iov->iov_base += tlen;
2295 uiop->uio_iov->iov_len -= tlen;
2296 uiop->uio_offset += tlen;
2297 uiop->uio_resid -= tlen;
2299 nfsm_adv(nfsm_rndup(len));
2301 nfsm_dissect(tl, u_int32_t *,
2304 nfsm_dissect(tl, u_int32_t *,
2308 cookie.nfsuquad[0] = *tl++;
2310 cookie.nfsuquad[1] = *tl++;
2315 more_dirs = fxdr_unsigned(int, *tl);
2318 * If at end of rpc data, get the eof boolean
2321 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2322 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2327 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2328 * by increasing d_reclen for the last record.
2331 left = DIRBLKSIZ - blksiz;
2332 dp->d_reclen += left;
2333 uiop->uio_iov->iov_base += left;
2334 uiop->uio_iov->iov_len -= left;
2335 uiop->uio_offset += left;
2336 uiop->uio_resid -= left;
2340 * We are now either at the end of the directory or have filled the
2344 dnp->n_direofoffset = uiop->uio_offset;
2346 if (uiop->uio_resid > 0)
2347 printf("EEK! readdirrpc resid > 0\n");
2348 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2356 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2359 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2366 struct vnode *newvp;
2368 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2369 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2371 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2372 struct nfsnode *dnp = VTONFS(vp), *np;
2375 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2376 int attrflag, fhsize;
2377 struct namecache *ncp;
2378 struct namecache *dncp;
2379 struct nlcomponent nlc;
2382 dp = (struct dirent *)0;
2385 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2386 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2387 panic("nfs readdirplusrpc bad uio");
2390 * Obtain the namecache record for the directory so we have something
2391 * to use as a basis for creating the entries. This function will
2392 * return a held (but not locked) ncp. The ncp may be disconnected
2393 * from the tree and cannot be used for upward traversals, and the
2394 * ncp may be unnamed. Note that other unrelated operations may
2395 * cause the ncp to be named at any time.
2397 dncp = cache_fromdvp(vp, NULL, 0);
2398 bzero(&nlc, sizeof(nlc));
2402 * If there is no cookie, assume directory was stale.
2404 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2408 return (NFSERR_BAD_COOKIE);
2410 * Loop around doing readdir rpc's of size nm_readdirsize
2411 * truncated to a multiple of DIRBLKSIZ.
2412 * The stopping criteria is EOF or buffer full.
2414 while (more_dirs && bigenough) {
2415 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2416 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2417 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2419 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2420 *tl++ = cookie.nfsuquad[0];
2421 *tl++ = cookie.nfsuquad[1];
2422 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2423 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2424 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2425 *tl = txdr_unsigned(nmp->nm_rsize);
2426 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2427 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2432 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2433 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2434 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2435 more_dirs = fxdr_unsigned(int, *tl);
2437 /* loop thru the dir entries, doctoring them to 4bsd form */
2438 while (more_dirs && bigenough) {
2439 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2440 fileno = fxdr_hyper(tl);
2441 len = fxdr_unsigned(int, *(tl + 2));
2442 if (len <= 0 || len > NFS_MAXNAMLEN) {
2447 tlen = nfsm_rndup(len);
2449 tlen += 4; /* To ensure null termination*/
2450 left = DIRBLKSIZ - blksiz;
2451 if ((tlen + DIRHDSIZ) > left) {
2452 dp->d_reclen += left;
2453 uiop->uio_iov->iov_base += left;
2454 uiop->uio_iov->iov_len -= left;
2455 uiop->uio_offset += left;
2456 uiop->uio_resid -= left;
2459 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2462 dp = (struct dirent *)uiop->uio_iov->iov_base;
2463 dp->d_fileno = (int)fileno;
2465 dp->d_reclen = tlen + DIRHDSIZ;
2466 dp->d_type = DT_UNKNOWN;
2467 blksiz += dp->d_reclen;
2468 if (blksiz == DIRBLKSIZ)
2470 uiop->uio_offset += DIRHDSIZ;
2471 uiop->uio_resid -= DIRHDSIZ;
2472 uiop->uio_iov->iov_base += DIRHDSIZ;
2473 uiop->uio_iov->iov_len -= DIRHDSIZ;
2474 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2475 nlc.nlc_namelen = len;
2476 nfsm_mtouio(uiop, len);
2477 cp = uiop->uio_iov->iov_base;
2480 uiop->uio_iov->iov_base += tlen;
2481 uiop->uio_iov->iov_len -= tlen;
2482 uiop->uio_offset += tlen;
2483 uiop->uio_resid -= tlen;
2485 nfsm_adv(nfsm_rndup(len));
2486 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2488 cookie.nfsuquad[0] = *tl++;
2489 cookie.nfsuquad[1] = *tl++;
2494 * Since the attributes are before the file handle
2495 * (sigh), we must skip over the attributes and then
2496 * come back and get them.
2498 attrflag = fxdr_unsigned(int, *tl);
2502 nfsm_adv(NFSX_V3FATTR);
2503 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2504 doit = fxdr_unsigned(int, *tl);
2506 nfsm_getfh(fhp, fhsize, 1);
2507 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2512 error = nfs_nget(vp->v_mount, fhp,
2520 if (doit && bigenough) {
2525 nfsm_loadattr(newvp, (struct vattr *)0);
2529 IFTODT(VTTOIF(np->n_vattr.va_type));
2531 printf("NFS/READDIRPLUS, ENTER %*.*s\n",
2532 nlc.nlc_namelen, nlc.nlc_namelen,
2534 ncp = cache_nlookup(dncp, &nlc);
2535 cache_setunresolved(ncp);
2536 cache_setvp(ncp, newvp);
2539 printf("NFS/READDIRPLUS, UNABLE TO ENTER"
2541 nlc.nlc_namelen, nlc.nlc_namelen,
2546 /* Just skip over the file handle */
2547 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2548 i = fxdr_unsigned(int, *tl);
2549 nfsm_adv(nfsm_rndup(i));
2551 if (newvp != NULLVP) {
2558 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2559 more_dirs = fxdr_unsigned(int, *tl);
2562 * If at end of rpc data, get the eof boolean
2565 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2566 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2571 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2572 * by increasing d_reclen for the last record.
2575 left = DIRBLKSIZ - blksiz;
2576 dp->d_reclen += left;
2577 uiop->uio_iov->iov_base += left;
2578 uiop->uio_iov->iov_len -= left;
2579 uiop->uio_offset += left;
2580 uiop->uio_resid -= left;
2584 * We are now either at the end of the directory or have filled the
2588 dnp->n_direofoffset = uiop->uio_offset;
2590 if (uiop->uio_resid > 0)
2591 printf("EEK! readdirplusrpc resid > 0\n");
2592 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2596 if (newvp != NULLVP) {
2609 * Silly rename. To make the NFS filesystem that is stateless look a little
2610 * more like the "ufs" a remove of an active vnode is translated to a rename
2611 * to a funny looking filename that is removed by nfs_inactive on the
2612 * nfsnode. There is the potential for another process on a different client
2613 * to create the same funny name between the nfs_lookitup() fails and the
2614 * nfs_rename() completes, but...
2617 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2619 struct sillyrename *sp;
2624 * We previously purged dvp instead of vp. I don't know why, it
2625 * completely destroys performance. We can't do it anyway with the
2626 * new VFS API since we would be breaking the namecache topology.
2628 cache_purge(vp); /* XXX */
2631 if (vp->v_type == VDIR)
2632 panic("nfs: sillyrename dir");
2634 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2635 M_NFSREQ, M_WAITOK);
2636 sp->s_cred = crdup(cnp->cn_cred);
2640 /* Fudge together a funny name */
2641 sp->s_namlen = sprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
2643 /* Try lookitups until we get one that isn't there */
2644 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2645 cnp->cn_td, (struct nfsnode **)0) == 0) {
2647 if (sp->s_name[4] > 'z') {
2652 error = nfs_renameit(dvp, cnp, sp);
2655 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2657 np->n_sillyrename = sp;
2662 free((caddr_t)sp, M_NFSREQ);
2667 * Look up a file name and optionally either update the file handle or
2668 * allocate an nfsnode, depending on the value of npp.
2669 * npp == NULL --> just do the lookup
2670 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2672 * *npp != NULL --> update the file handle in the vnode
2675 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2676 struct thread *td, struct nfsnode **npp)
2681 struct vnode *newvp = (struct vnode *)0;
2682 struct nfsnode *np, *dnp = VTONFS(dvp);
2683 caddr_t bpos, dpos, cp2;
2684 int error = 0, fhlen, attrflag;
2685 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2687 int v3 = NFS_ISV3(dvp);
2689 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2690 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2691 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2692 nfsm_fhtom(dvp, v3);
2693 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2694 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2695 if (npp && !error) {
2696 nfsm_getfh(nfhp, fhlen, v3);
2699 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2700 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2701 np->n_fhp = &np->n_fh;
2702 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2703 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2704 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2705 np->n_fhsize = fhlen;
2707 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2711 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2719 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
2720 if (!attrflag && *npp == NULL) {
2729 nfsm_loadattr(newvp, (struct vattr *)0);
2733 if (npp && *npp == NULL) {
2748 * Nfs Version 3 commit rpc
2751 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2756 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2757 caddr_t bpos, dpos, cp2;
2758 int error = 0, wccflag = NFSV3_WCCRATTR;
2759 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2761 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2763 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2764 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2766 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2767 txdr_hyper(offset, tl);
2769 *tl = txdr_unsigned(cnt);
2770 nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2771 nfsm_wcc_data(vp, wccflag);
2773 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2774 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2775 NFSX_V3WRITEVERF)) {
2776 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2778 error = NFSERR_STALEWRITEVERF;
2788 * - make nfs_bmap() essentially a no-op that does no translation
2789 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2790 * (Maybe I could use the process's page mapping, but I was concerned that
2791 * Kernel Write might not be enabled and also figured copyout() would do
2792 * a lot more work than bcopy() and also it currently happens in the
2793 * context of the swapper process (2).
2795 * nfs_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
2796 * daddr_t *a_bnp, int *a_runp, int *a_runb)
2799 nfs_bmap(struct vop_bmap_args *ap)
2801 struct vnode *vp = ap->a_vp;
2803 if (ap->a_vpp != NULL)
2805 if (ap->a_bnp != NULL)
2806 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2807 if (ap->a_runp != NULL)
2809 if (ap->a_runb != NULL)
2816 * For async requests when nfsiod(s) are running, queue the request by
2817 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2821 nfs_strategy(struct vop_strategy_args *ap)
2823 struct buf *bp = ap->a_bp;
2827 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2828 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2830 if (bp->b_flags & B_PHYS)
2831 panic("nfs physio");
2833 if (bp->b_flags & B_ASYNC)
2836 td = curthread; /* XXX */
2839 * If the op is asynchronous and an i/o daemon is waiting
2840 * queue the request, wake it up and wait for completion
2841 * otherwise just do it ourselves.
2843 if ((bp->b_flags & B_ASYNC) == 0 ||
2844 nfs_asyncio(bp, td))
2845 error = nfs_doio(bp, td);
2852 * NB Currently unsupported.
2854 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
2855 * struct thread *a_td)
2859 nfs_mmap(struct vop_mmap_args *ap)
2865 * fsync vnode op. Just call nfs_flush() with commit == 1.
2867 * nfs_fsync(struct vnodeop_desc *a_desc, struct vnode *a_vp,
2868 * struct ucred * a_cred, int a_waitfor, struct thread *a_td)
2872 nfs_fsync(struct vop_fsync_args *ap)
2874 return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2878 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
2879 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
2880 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
2881 * set the buffer contains data that has already been written to the server
2882 * and which now needs a commit RPC.
2884 * If commit is 0 we only take one pass and only flush buffers containing new
2887 * If commit is 1 we take two passes, issuing a commit RPC in the second
2890 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
2891 * to completely flush all pending data.
2893 * Note that the RB_SCAN code properly handles the case where the
2894 * callback might block and directly or indirectly (another thread) cause
2895 * the RB tree to change.
2898 #ifndef NFS_COMMITBVECSIZ
2899 #define NFS_COMMITBVECSIZ 16
2902 struct nfs_flush_info {
2903 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
2910 struct buf *bvary[NFS_COMMITBVECSIZ];
2916 static int nfs_flush_bp(struct buf *bp, void *data);
2917 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
2920 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
2922 struct nfsnode *np = VTONFS(vp);
2923 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2924 struct nfs_flush_info info;
2927 bzero(&info, sizeof(info));
2930 info.waitfor = waitfor;
2931 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
2938 info.mode = NFI_FLUSHNEW;
2939 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2940 nfs_flush_bp, &info);
2943 * Take a second pass if committing and no error occured.
2944 * Clean up any left over collection (whether an error
2947 if (commit && error == 0) {
2948 info.mode = NFI_COMMIT;
2949 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2950 nfs_flush_bp, &info);
2952 error = nfs_flush_docommit(&info, error);
2956 * Wait for pending I/O to complete before checking whether
2957 * any further dirty buffers exist.
2959 while (waitfor == MNT_WAIT && vp->v_numoutput) {
2960 vp->v_flag |= VBWAIT;
2961 error = tsleep((caddr_t)&vp->v_numoutput,
2962 info.slpflag, "nfsfsync", info.slptimeo);
2965 * We have to be able to break out if this
2966 * is an 'intr' mount.
2968 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
2974 * Since we do not process pending signals,
2975 * once we get a PCATCH our tsleep() will no
2976 * longer sleep, switch to a fixed timeout
2979 if (info.slpflag == PCATCH) {
2981 info.slptimeo = 2 * hz;
2988 * Loop if we are flushing synchronous as well as committing,
2989 * and dirty buffers are still present. Otherwise we might livelock.
2991 } while (waitfor == MNT_WAIT && commit &&
2992 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
2995 * The callbacks have to return a negative error to terminate the
3002 * Deal with any error collection
3004 if (np->n_flag & NWRITEERR) {
3005 error = np->n_error;
3006 np->n_flag &= ~NWRITEERR;
3014 nfs_flush_bp(struct buf *bp, void *data)
3016 struct nfs_flush_info *info = data;
3022 switch(info->mode) {
3025 if (info->loops && info->waitfor == MNT_WAIT) {
3026 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3028 error = BUF_TIMELOCK(bp,
3029 LK_EXCLUSIVE | LK_SLEEPFAIL,
3031 info->slpflag, info->slptimeo);
3034 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3037 if ((bp->b_flags & B_DELWRI) == 0)
3038 panic("nfs_fsync: not dirty");
3039 if (bp->b_flags & B_NEEDCOMMIT) {
3046 bp->b_flags |= B_ASYNC;
3048 VOP_BWRITE(bp->b_vp, bp);
3056 * Only process buffers in need of a commit which we can
3057 * immediately lock. This may prevent a buffer from being
3058 * committed, but the normal flush loop will block on the
3059 * same buffer so we shouldn't get into an endless loop.
3062 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3063 (B_DELWRI | B_NEEDCOMMIT) ||
3064 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
3072 * NOTE: we are not clearing B_DONE here, so we have
3073 * to do it later on in this routine if we intend to
3074 * initiate I/O on the bp.
3076 * Note: to avoid loopback deadlocks, we do not
3077 * assign b_runningbufspace.
3079 vfs_busy_pages(bp, 1);
3081 info->bvary[info->bvsize] = bp;
3082 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3084 if (info->bvsize == 0 || toff < info->beg_off)
3085 info->beg_off = toff;
3086 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
3087 if (info->bvsize == 0 || toff > info->end_off)
3088 info->end_off = toff;
3090 if (info->bvsize == NFS_COMMITBVECSIZ) {
3091 error = nfs_flush_docommit(info, 0);
3092 KKASSERT(info->bvsize == 0);
3101 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3112 if (info->bvsize > 0) {
3114 * Commit data on the server, as required. Note that
3115 * nfs_commit will use the vnode's cred for the commit.
3116 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3118 bytes = info->end_off - info->beg_off;
3119 if (bytes > 0x40000000)
3124 retv = nfs_commit(vp, info->beg_off,
3125 (int)bytes, info->td);
3126 if (retv == NFSERR_STALEWRITEVERF)
3127 nfs_clearcommit(vp->v_mount);
3131 * Now, either mark the blocks I/O done or mark the
3132 * blocks dirty, depending on whether the commit
3135 for (i = 0; i < info->bvsize; ++i) {
3136 bp = info->bvary[i];
3137 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3140 * Error, leave B_DELWRI intact
3142 vfs_unbusy_pages(bp);
3146 * Success, remove B_DELWRI ( bundirty() ).
3148 * b_dirtyoff/b_dirtyend seem to be NFS
3149 * specific. We should probably move that
3150 * into bundirty(). XXX
3154 bp->b_flags |= B_ASYNC;
3156 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3157 bp->b_dirtyoff = bp->b_dirtyend = 0;
3168 * NFS advisory byte-level locks.
3169 * Currently unsupported.
3171 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3175 nfs_advlock(struct vop_advlock_args *ap)
3177 struct nfsnode *np = VTONFS(ap->a_vp);
3180 * The following kludge is to allow diskless support to work
3181 * until a real NFS lockd is implemented. Basically, just pretend
3182 * that this is a local lock.
3184 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3188 * Print out the contents of an nfsnode.
3190 * nfs_print(struct vnode *a_vp)
3193 nfs_print(struct vop_print_args *ap)
3195 struct vnode *vp = ap->a_vp;
3196 struct nfsnode *np = VTONFS(vp);
3198 printf("tag VT_NFS, fileid %ld fsid 0x%x",
3199 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3200 if (vp->v_type == VFIFO)
3207 * Just call nfs_writebp() with the force argument set to 1.
3209 * NOTE: B_DONE may or may not be set in a_bp on call.
3211 * nfs_bwrite(struct vnode *a_bp)
3214 nfs_bwrite(struct vop_bwrite_args *ap)
3216 return (nfs_writebp(ap->a_bp, 1, curthread));
3220 * This is a clone of vn_bwrite(), except that it also handles the
3221 * B_NEEDCOMMIT flag. We set B_CACHE if this is a VMIO buffer.
3224 nfs_writebp(struct buf *bp, int force, struct thread *td)
3227 int oldflags = bp->b_flags;
3233 if (BUF_REFCNT(bp) == 0)
3234 panic("bwrite: buffer is not locked???");
3236 if (bp->b_flags & B_INVAL) {
3241 bp->b_flags |= B_CACHE;
3244 * Undirty the bp. We will redirty it later if the I/O fails.
3249 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3251 bp->b_vp->v_numoutput++;
3255 * Note: to avoid loopback deadlocks, we do not
3256 * assign b_runningbufspace.
3258 vfs_busy_pages(bp, 1);
3261 VOP_STRATEGY(bp->b_vp, bp);
3263 if( (oldflags & B_ASYNC) == 0) {
3264 int rtval = biowait(bp);
3266 if (oldflags & B_DELWRI) {
3268 reassignbuf(bp, bp->b_vp);
3280 * nfs special file access vnode op.
3281 * Essentially just get vattr and then imitate iaccess() since the device is
3282 * local to the client.
3284 * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
3285 * struct thread *a_td)
3288 nfsspec_access(struct vop_access_args *ap)
3292 struct ucred *cred = ap->a_cred;
3293 struct vnode *vp = ap->a_vp;
3294 mode_t mode = ap->a_mode;
3300 * Disallow write attempts on filesystems mounted read-only;
3301 * unless the file is a socket, fifo, or a block or character
3302 * device resident on the filesystem.
3304 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3305 switch (vp->v_type) {
3315 * If you're the super-user,
3316 * you always get access.
3318 if (cred->cr_uid == 0)
3321 error = VOP_GETATTR(vp, vap, ap->a_td);
3325 * Access check is based on only one of owner, group, public.
3326 * If not owner, then check group. If not a member of the
3327 * group, then check public access.
3329 if (cred->cr_uid != vap->va_uid) {
3331 gp = cred->cr_groups;
3332 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3333 if (vap->va_gid == *gp)
3339 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3344 * Read wrapper for special devices.
3346 * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3347 * struct ucred *a_cred)
3350 nfsspec_read(struct vop_read_args *ap)
3352 struct nfsnode *np = VTONFS(ap->a_vp);
3358 getnanotime(&np->n_atim);
3359 return (VOCALL(spec_vnode_vops, &ap->a_head));
3363 * Write wrapper for special devices.
3365 * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3366 * struct ucred *a_cred)
3369 nfsspec_write(struct vop_write_args *ap)
3371 struct nfsnode *np = VTONFS(ap->a_vp);
3377 getnanotime(&np->n_mtim);
3378 return (VOCALL(spec_vnode_vops, &ap->a_head));
3382 * Close wrapper for special devices.
3384 * Update the times on the nfsnode then do device close.
3386 * nfsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
3387 * struct thread *a_td)
3390 nfsspec_close(struct vop_close_args *ap)
3392 struct vnode *vp = ap->a_vp;
3393 struct nfsnode *np = VTONFS(vp);
3396 if (np->n_flag & (NACC | NUPD)) {
3398 if (vp->v_usecount == 1 &&
3399 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3401 if (np->n_flag & NACC)
3402 vattr.va_atime = np->n_atim;
3403 if (np->n_flag & NUPD)
3404 vattr.va_mtime = np->n_mtim;
3405 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3408 return (VOCALL(spec_vnode_vops, &ap->a_head));
3412 * Read wrapper for fifos.
3414 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3415 * struct ucred *a_cred)
3418 nfsfifo_read(struct vop_read_args *ap)
3420 struct nfsnode *np = VTONFS(ap->a_vp);
3426 getnanotime(&np->n_atim);
3427 return (VOCALL(fifo_vnode_vops, &ap->a_head));
3431 * Write wrapper for fifos.
3433 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3434 * struct ucred *a_cred)
3437 nfsfifo_write(struct vop_write_args *ap)
3439 struct nfsnode *np = VTONFS(ap->a_vp);
3445 getnanotime(&np->n_mtim);
3446 return (VOCALL(fifo_vnode_vops, &ap->a_head));
3450 * Close wrapper for fifos.
3452 * Update the times on the nfsnode then do fifo close.
3454 * nfsfifo_close(struct vnode *a_vp, int a_fflag, struct thread *a_td)
3457 nfsfifo_close(struct vop_close_args *ap)
3459 struct vnode *vp = ap->a_vp;
3460 struct nfsnode *np = VTONFS(vp);
3464 if (np->n_flag & (NACC | NUPD)) {
3466 if (np->n_flag & NACC)
3468 if (np->n_flag & NUPD)
3471 if (vp->v_usecount == 1 &&
3472 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3474 if (np->n_flag & NACC)
3475 vattr.va_atime = np->n_atim;
3476 if (np->n_flag & NUPD)
3477 vattr.va_mtime = np->n_mtim;
3478 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3481 return (VOCALL(fifo_vnode_vops, &ap->a_head));