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.41 2005/06/06 15:09:38 drhodus 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>
89 #include <sys/thread2.h>
96 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
97 * calls are not in getblk() and brelse() so that they would not be necessary
101 #define vfs_busy_pages(bp, f)
104 static int nfsspec_read (struct vop_read_args *);
105 static int nfsspec_write (struct vop_write_args *);
106 static int nfsfifo_read (struct vop_read_args *);
107 static int nfsfifo_write (struct vop_write_args *);
108 static int nfsspec_close (struct vop_close_args *);
109 static int nfsfifo_close (struct vop_close_args *);
110 #define nfs_poll vop_nopoll
111 static int nfs_setattrrpc (struct vnode *,struct vattr *,struct ucred *,struct thread *);
112 static int nfs_lookup (struct vop_lookup_args *);
113 static int nfs_create (struct vop_create_args *);
114 static int nfs_mknod (struct vop_mknod_args *);
115 static int nfs_open (struct vop_open_args *);
116 static int nfs_close (struct vop_close_args *);
117 static int nfs_access (struct vop_access_args *);
118 static int nfs_getattr (struct vop_getattr_args *);
119 static int nfs_setattr (struct vop_setattr_args *);
120 static int nfs_read (struct vop_read_args *);
121 static int nfs_mmap (struct vop_mmap_args *);
122 static int nfs_fsync (struct vop_fsync_args *);
123 static int nfs_remove (struct vop_remove_args *);
124 static int nfs_link (struct vop_link_args *);
125 static int nfs_rename (struct vop_rename_args *);
126 static int nfs_mkdir (struct vop_mkdir_args *);
127 static int nfs_rmdir (struct vop_rmdir_args *);
128 static int nfs_symlink (struct vop_symlink_args *);
129 static int nfs_readdir (struct vop_readdir_args *);
130 static int nfs_bmap (struct vop_bmap_args *);
131 static int nfs_strategy (struct vop_strategy_args *);
132 static int nfs_lookitup (struct vnode *, const char *, int,
133 struct ucred *, struct thread *, struct nfsnode **);
134 static int nfs_sillyrename (struct vnode *,struct vnode *,struct componentname *);
135 static int nfsspec_access (struct vop_access_args *);
136 static int nfs_readlink (struct vop_readlink_args *);
137 static int nfs_print (struct vop_print_args *);
138 static int nfs_advlock (struct vop_advlock_args *);
139 static int nfs_bwrite (struct vop_bwrite_args *);
141 static int nfs_nresolve (struct vop_nresolve_args *);
143 * Global vfs data structures for nfs
145 struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
146 { &vop_default_desc, vop_defaultop },
147 { &vop_access_desc, (vnodeopv_entry_t) nfs_access },
148 { &vop_advlock_desc, (vnodeopv_entry_t) nfs_advlock },
149 { &vop_bmap_desc, (vnodeopv_entry_t) nfs_bmap },
150 { &vop_bwrite_desc, (vnodeopv_entry_t) nfs_bwrite },
151 { &vop_close_desc, (vnodeopv_entry_t) nfs_close },
152 { &vop_create_desc, (vnodeopv_entry_t) nfs_create },
153 { &vop_fsync_desc, (vnodeopv_entry_t) nfs_fsync },
154 { &vop_getattr_desc, (vnodeopv_entry_t) nfs_getattr },
155 { &vop_getpages_desc, (vnodeopv_entry_t) nfs_getpages },
156 { &vop_putpages_desc, (vnodeopv_entry_t) nfs_putpages },
157 { &vop_inactive_desc, (vnodeopv_entry_t) nfs_inactive },
158 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
159 { &vop_lease_desc, vop_null },
160 { &vop_link_desc, (vnodeopv_entry_t) nfs_link },
161 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
162 { &vop_lookup_desc, (vnodeopv_entry_t) nfs_lookup },
163 { &vop_mkdir_desc, (vnodeopv_entry_t) nfs_mkdir },
164 { &vop_mknod_desc, (vnodeopv_entry_t) nfs_mknod },
165 { &vop_mmap_desc, (vnodeopv_entry_t) nfs_mmap },
166 { &vop_open_desc, (vnodeopv_entry_t) nfs_open },
167 { &vop_poll_desc, (vnodeopv_entry_t) nfs_poll },
168 { &vop_print_desc, (vnodeopv_entry_t) nfs_print },
169 { &vop_read_desc, (vnodeopv_entry_t) nfs_read },
170 { &vop_readdir_desc, (vnodeopv_entry_t) nfs_readdir },
171 { &vop_readlink_desc, (vnodeopv_entry_t) nfs_readlink },
172 { &vop_reclaim_desc, (vnodeopv_entry_t) nfs_reclaim },
173 { &vop_remove_desc, (vnodeopv_entry_t) nfs_remove },
174 { &vop_rename_desc, (vnodeopv_entry_t) nfs_rename },
175 { &vop_rmdir_desc, (vnodeopv_entry_t) nfs_rmdir },
176 { &vop_setattr_desc, (vnodeopv_entry_t) nfs_setattr },
177 { &vop_strategy_desc, (vnodeopv_entry_t) nfs_strategy },
178 { &vop_symlink_desc, (vnodeopv_entry_t) nfs_symlink },
179 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
180 { &vop_write_desc, (vnodeopv_entry_t) nfs_write },
182 { &vop_nresolve_desc, (vnodeopv_entry_t) nfs_nresolve },
187 * Special device vnode ops
189 struct vnodeopv_entry_desc nfsv2_specop_entries[] = {
190 { &vop_default_desc, (vnodeopv_entry_t) spec_vnoperate },
191 { &vop_access_desc, (vnodeopv_entry_t) nfsspec_access },
192 { &vop_close_desc, (vnodeopv_entry_t) nfsspec_close },
193 { &vop_fsync_desc, (vnodeopv_entry_t) nfs_fsync },
194 { &vop_getattr_desc, (vnodeopv_entry_t) nfs_getattr },
195 { &vop_inactive_desc, (vnodeopv_entry_t) nfs_inactive },
196 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
197 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
198 { &vop_print_desc, (vnodeopv_entry_t) nfs_print },
199 { &vop_read_desc, (vnodeopv_entry_t) nfsspec_read },
200 { &vop_reclaim_desc, (vnodeopv_entry_t) nfs_reclaim },
201 { &vop_setattr_desc, (vnodeopv_entry_t) nfs_setattr },
202 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
203 { &vop_write_desc, (vnodeopv_entry_t) nfsspec_write },
207 struct vnodeopv_entry_desc nfsv2_fifoop_entries[] = {
208 { &vop_default_desc, (vnodeopv_entry_t) fifo_vnoperate },
209 { &vop_access_desc, (vnodeopv_entry_t) nfsspec_access },
210 { &vop_close_desc, (vnodeopv_entry_t) nfsfifo_close },
211 { &vop_fsync_desc, (vnodeopv_entry_t) nfs_fsync },
212 { &vop_getattr_desc, (vnodeopv_entry_t) nfs_getattr },
213 { &vop_inactive_desc, (vnodeopv_entry_t) nfs_inactive },
214 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
215 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
216 { &vop_print_desc, (vnodeopv_entry_t) nfs_print },
217 { &vop_read_desc, (vnodeopv_entry_t) nfsfifo_read },
218 { &vop_reclaim_desc, (vnodeopv_entry_t) nfs_reclaim },
219 { &vop_setattr_desc, (vnodeopv_entry_t) nfs_setattr },
220 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
221 { &vop_write_desc, (vnodeopv_entry_t) nfsfifo_write },
225 static int nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
226 struct componentname *cnp,
228 static int nfs_removerpc (struct vnode *dvp, const char *name,
230 struct ucred *cred, struct thread *td);
231 static int nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
232 int fnamelen, struct vnode *tdvp,
233 const char *tnameptr, int tnamelen,
234 struct ucred *cred, struct thread *td);
235 static int nfs_renameit (struct vnode *sdvp,
236 struct componentname *scnp,
237 struct sillyrename *sp);
242 extern u_int32_t nfs_true, nfs_false;
243 extern u_int32_t nfs_xdrneg1;
244 extern struct nfsstats nfsstats;
245 extern nfstype nfsv3_type[9];
246 struct thread *nfs_iodwant[NFS_MAXASYNCDAEMON];
247 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
248 int nfs_numasync = 0;
249 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
251 SYSCTL_DECL(_vfs_nfs);
253 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
254 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
255 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
257 static int nfsneg_cache_timeout = NFS_MINATTRTIMO;
258 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
259 &nfsneg_cache_timeout, 0, "NFS NEGATIVE ACCESS cache timeout");
261 static int nfsv3_commit_on_close = 0;
262 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
263 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
265 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
266 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
268 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
269 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
272 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
273 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
274 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
276 nfs3_access_otw(struct vnode *vp, int wmode,
277 struct thread *td, struct ucred *cred)
281 int error = 0, attrflag;
283 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
284 caddr_t bpos, dpos, cp2;
288 struct nfsnode *np = VTONFS(vp);
290 nfsstats.rpccnt[NFSPROC_ACCESS]++;
291 nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
293 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
294 *tl = txdr_unsigned(wmode);
295 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
296 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
298 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
299 rmode = fxdr_unsigned(u_int32_t, *tl);
301 np->n_modeuid = cred->cr_uid;
302 np->n_modestamp = mycpu->gd_time_seconds;
310 * nfs access vnode op.
311 * For nfs version 2, just return ok. File accesses may fail later.
312 * For nfs version 3, use the access rpc to check accessibility. If file modes
313 * are changed on the server, accesses might still fail later.
315 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
316 * struct thread *a_td)
319 nfs_access(struct vop_access_args *ap)
321 struct vnode *vp = ap->a_vp;
323 u_int32_t mode, wmode;
324 int v3 = NFS_ISV3(vp);
325 struct nfsnode *np = VTONFS(vp);
328 * Disallow write attempts on filesystems mounted read-only;
329 * unless the file is a socket, fifo, or a block or character
330 * device resident on the filesystem.
332 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
333 switch (vp->v_type) {
343 * For nfs v3, check to see if we have done this recently, and if
344 * so return our cached result instead of making an ACCESS call.
345 * If not, do an access rpc, otherwise you are stuck emulating
346 * ufs_access() locally using the vattr. This may not be correct,
347 * since the server may apply other access criteria such as
348 * client uid-->server uid mapping that we do not know about.
351 if (ap->a_mode & VREAD)
352 mode = NFSV3ACCESS_READ;
355 if (vp->v_type != VDIR) {
356 if (ap->a_mode & VWRITE)
357 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
358 if (ap->a_mode & VEXEC)
359 mode |= NFSV3ACCESS_EXECUTE;
361 if (ap->a_mode & VWRITE)
362 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
364 if (ap->a_mode & VEXEC)
365 mode |= NFSV3ACCESS_LOOKUP;
367 /* XXX safety belt, only make blanket request if caching */
368 if (nfsaccess_cache_timeout > 0) {
369 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
370 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
371 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
377 * Does our cached result allow us to give a definite yes to
380 if (np->n_modestamp &&
381 (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
382 (ap->a_cred->cr_uid == np->n_modeuid) &&
383 ((np->n_mode & mode) == mode)) {
384 nfsstats.accesscache_hits++;
387 * Either a no, or a don't know. Go to the wire.
389 nfsstats.accesscache_misses++;
390 error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred);
392 if ((np->n_mode & mode) != mode) {
398 if ((error = nfsspec_access(ap)) != 0)
402 * Attempt to prevent a mapped root from accessing a file
403 * which it shouldn't. We try to read a byte from the file
404 * if the user is root and the file is not zero length.
405 * After calling nfsspec_access, we should have the correct
408 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
409 && VTONFS(vp)->n_size > 0) {
416 auio.uio_iov = &aiov;
420 auio.uio_segflg = UIO_SYSSPACE;
421 auio.uio_rw = UIO_READ;
422 auio.uio_td = ap->a_td;
424 if (vp->v_type == VREG) {
425 error = nfs_readrpc(vp, &auio);
426 } else if (vp->v_type == VDIR) {
428 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
430 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
431 error = nfs_readdirrpc(vp, &auio);
433 } else if (vp->v_type == VLNK) {
434 error = nfs_readlinkrpc(vp, &auio);
441 * [re]record creds for reading and/or writing if access
442 * was granted. Assume the NFS server will grant read access
443 * for execute requests.
446 if ((ap->a_mode & (VREAD|VEXEC)) && ap->a_cred != np->n_rucred) {
449 crfree(np->n_rucred);
450 np->n_rucred = ap->a_cred;
452 if ((ap->a_mode & VWRITE) && ap->a_cred != np->n_wucred) {
455 crfree(np->n_wucred);
456 np->n_wucred = ap->a_cred;
464 * Check to see if the type is ok
465 * and that deletion is not in progress.
466 * For paged in text files, you will need to flush the page cache
467 * if consistency is lost.
469 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
470 * struct thread *a_td)
474 nfs_open(struct vop_open_args *ap)
476 struct vnode *vp = ap->a_vp;
477 struct nfsnode *np = VTONFS(vp);
478 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
482 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
484 printf("open eacces vtyp=%d\n",vp->v_type);
490 * Clear the attribute cache only if opening with write access. It
491 * is unclear if we should do this at all here, but we certainly
492 * should not clear the cache unconditionally simply because a file
495 if (ap->a_mode & FWRITE)
498 if (nmp->nm_flag & NFSMNT_NQNFS) {
500 * If NQNFS is active, get a valid lease
502 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
504 error = nqnfs_getlease(vp, ND_READ, ap->a_td);
505 } while (error == NQNFS_EXPIRED);
508 if (np->n_lrev != np->n_brev ||
509 (np->n_flag & NQNFSNONCACHE)) {
510 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1))
514 np->n_brev = np->n_lrev;
519 * For normal NFS, reconcile changes made locally verses
520 * changes made remotely. Note that VOP_GETATTR only goes
521 * to the wire if the cached attribute has timed out or been
524 * If local modifications have been made clear the attribute
525 * cache to force an attribute and modified time check. If
526 * GETATTR detects that the file has been changed by someone
527 * other then us it will set NRMODIFIED.
529 * If we are opening a directory and local changes have been
530 * made we have to invalidate the cache in order to ensure
531 * that we get the most up-to-date information from the
534 if (np->n_flag & NLMODIFIED) {
536 if (vp->v_type == VDIR) {
537 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
543 error = VOP_GETATTR(vp, &vattr, ap->a_td);
546 if (np->n_flag & NRMODIFIED) {
547 if (vp->v_type == VDIR)
549 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
552 np->n_flag &= ~NRMODIFIED;
561 * What an NFS client should do upon close after writing is a debatable issue.
562 * Most NFS clients push delayed writes to the server upon close, basically for
564 * 1 - So that any write errors may be reported back to the client process
565 * doing the close system call. By far the two most likely errors are
566 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
567 * 2 - To put a worst case upper bound on cache inconsistency between
568 * multiple clients for the file.
569 * There is also a consistency problem for Version 2 of the protocol w.r.t.
570 * not being able to tell if other clients are writing a file concurrently,
571 * since there is no way of knowing if the changed modify time in the reply
572 * is only due to the write for this client.
573 * (NFS Version 3 provides weak cache consistency data in the reply that
574 * should be sufficient to detect and handle this case.)
576 * The current code does the following:
577 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
578 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
579 * or commit them (this satisfies 1 and 2 except for the
580 * case where the server crashes after this close but
581 * before the commit RPC, which is felt to be "good
582 * enough". Changing the last argument to nfs_flush() to
583 * a 1 would force a commit operation, if it is felt a
584 * commit is necessary now.
585 * for NQNFS - do nothing now, since 2 is dealt with via leases and
586 * 1 should be dealt with via an fsync() system call for
587 * cases where write errors are important.
589 * nfs_close(struct vnodeop_desc *a_desc, struct vnode *a_vp, int a_fflag,
590 * struct ucred *a_cred, struct thread *a_td)
594 nfs_close(struct vop_close_args *ap)
596 struct vnode *vp = ap->a_vp;
597 struct nfsnode *np = VTONFS(vp);
600 if (vp->v_type == VREG) {
601 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 &&
602 (np->n_flag & NLMODIFIED)) {
605 * Under NFSv3 we have dirty buffers to dispose of. We
606 * must flush them to the NFS server. We have the option
607 * of waiting all the way through the commit rpc or just
608 * waiting for the initial write. The default is to only
609 * wait through the initial write so the data is in the
610 * server's cache, which is roughly similar to the state
611 * a standard disk subsystem leaves the file in on close().
613 * We cannot clear the NLMODIFIED bit in np->n_flag due to
614 * potential races with other processes, and certainly
615 * cannot clear it if we don't commit.
617 int cm = nfsv3_commit_on_close ? 1 : 0;
618 error = nfs_flush(vp, MNT_WAIT, ap->a_td, cm);
619 /* np->n_flag &= ~NLMODIFIED; */
621 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
625 if (np->n_flag & NWRITEERR) {
626 np->n_flag &= ~NWRITEERR;
634 * nfs getattr call from vfs.
636 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred,
637 * struct thread *a_td)
640 nfs_getattr(struct vop_getattr_args *ap)
642 struct vnode *vp = ap->a_vp;
643 struct nfsnode *np = VTONFS(vp);
649 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
650 int v3 = NFS_ISV3(vp);
653 * Update local times for special files.
655 if (np->n_flag & (NACC | NUPD))
658 * First look in the cache.
660 if (nfs_getattrcache(vp, ap->a_vap) == 0)
663 if (v3 && nfsaccess_cache_timeout > 0) {
664 nfsstats.accesscache_misses++;
665 nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_td, nfs_vpcred(vp, ND_CHECK));
666 if (nfs_getattrcache(vp, ap->a_vap) == 0)
670 nfsstats.rpccnt[NFSPROC_GETATTR]++;
671 nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
673 nfsm_request(vp, NFSPROC_GETATTR, ap->a_td, nfs_vpcred(vp, ND_CHECK));
675 nfsm_loadattr(vp, ap->a_vap);
685 * nfs_setattr(struct vnodeop_desc *a_desc, struct vnode *a_vp,
686 * struct vattr *a_vap, struct ucred *a_cred,
687 * struct thread *a_td)
690 nfs_setattr(struct vop_setattr_args *ap)
692 struct vnode *vp = ap->a_vp;
693 struct nfsnode *np = VTONFS(vp);
694 struct vattr *vap = ap->a_vap;
703 * Setting of flags is not supported.
705 if (vap->va_flags != VNOVAL)
709 * Disallow write attempts if the filesystem is mounted read-only.
711 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
712 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
713 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
714 (vp->v_mount->mnt_flag & MNT_RDONLY))
716 if (vap->va_size != VNOVAL) {
717 switch (vp->v_type) {
724 if (vap->va_mtime.tv_sec == VNOVAL &&
725 vap->va_atime.tv_sec == VNOVAL &&
726 vap->va_mode == (mode_t)VNOVAL &&
727 vap->va_uid == (uid_t)VNOVAL &&
728 vap->va_gid == (gid_t)VNOVAL)
730 vap->va_size = VNOVAL;
734 * Disallow write attempts if the filesystem is
737 if (vp->v_mount->mnt_flag & MNT_RDONLY)
741 * This is nasty. The RPCs we send to flush pending
742 * data often return attribute information which is
743 * cached via a callback to nfs_loadattrcache(), which
744 * has the effect of changing our notion of the file
745 * size. Due to flushed appends and other operations
746 * the file size can be set to virtually anything,
747 * including values that do not match either the old
748 * or intended file size.
750 * When this condition is detected we must loop to
751 * try the operation again. Hopefully no more
752 * flushing is required on the loop so it works the
753 * second time around. THIS CASE ALMOST ALWAYS
758 error = nfs_meta_setsize(vp, ap->a_td, vap->va_size);
760 if (np->n_flag & NLMODIFIED) {
761 if (vap->va_size == 0)
762 error = nfs_vinvalbuf(vp, 0, ap->a_td, 1);
764 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
767 * note: this loop case almost always happens at
768 * least once per truncation.
770 if (error == 0 && np->n_size != vap->va_size)
772 np->n_vattr.va_size = vap->va_size;
775 } else if ((vap->va_mtime.tv_sec != VNOVAL ||
776 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NLMODIFIED) &&
777 vp->v_type == VREG &&
778 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) == EINTR
782 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
785 * Sanity check if a truncation was issued. This should only occur
786 * if multiple processes are racing on the same file.
788 if (error == 0 && vap->va_size != VNOVAL &&
789 np->n_size != vap->va_size) {
790 printf("NFS ftruncate: server disagrees on the file size: %lld/%lld/%lld\n", tsize, vap->va_size, np->n_size);
793 if (error && vap->va_size != VNOVAL) {
794 np->n_size = np->n_vattr.va_size = tsize;
795 vnode_pager_setsize(vp, np->n_size);
801 * Do an nfs setattr rpc.
804 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
805 struct ucred *cred, struct thread *td)
807 struct nfsv2_sattr *sp;
808 struct nfsnode *np = VTONFS(vp);
811 caddr_t bpos, dpos, cp2;
813 int error = 0, wccflag = NFSV3_WCCRATTR;
814 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
815 int v3 = NFS_ISV3(vp);
817 nfsstats.rpccnt[NFSPROC_SETATTR]++;
818 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
821 nfsm_v3attrbuild(vap, TRUE);
822 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
825 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
826 if (vap->va_mode == (mode_t)VNOVAL)
827 sp->sa_mode = nfs_xdrneg1;
829 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
830 if (vap->va_uid == (uid_t)VNOVAL)
831 sp->sa_uid = nfs_xdrneg1;
833 sp->sa_uid = txdr_unsigned(vap->va_uid);
834 if (vap->va_gid == (gid_t)VNOVAL)
835 sp->sa_gid = nfs_xdrneg1;
837 sp->sa_gid = txdr_unsigned(vap->va_gid);
838 sp->sa_size = txdr_unsigned(vap->va_size);
839 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
840 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
842 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
845 nfsm_wcc_data(vp, wccflag);
847 nfsm_loadattr(vp, (struct vattr *)0);
854 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
855 * nfs_lookup() until all remaining new api calls are implemented.
857 * Resolve a namecache entry. This function is passed a locked ncp and
858 * must call cache_setvp() on it as appropriate to resolve the entry.
861 nfs_nresolve(struct vop_nresolve_args *ap)
863 struct thread *td = curthread;
864 struct namecache *ncp;
875 /******NFSM MACROS********/
876 struct mbuf *mb, *mrep, *mreq, *mb2, *md;
877 caddr_t bpos, dpos, cp, cp2;
884 KKASSERT(ncp->nc_parent && ncp->nc_parent->nc_vp);
885 dvp = ncp->nc_parent->nc_vp;
886 if ((error = vget(dvp, LK_SHARED, td)) != 0)
891 nfsstats.lookupcache_misses++;
892 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
894 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
895 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
897 nfsm_strtom(ncp->nc_name, len, NFS_MAXNAMLEN);
898 nfsm_request(dvp, NFSPROC_LOOKUP, td, ap->a_cred);
901 * Cache negatve lookups to reduce NFS traffic, but use
902 * a fast timeout. Otherwise use a timeout of 1 tick.
903 * XXX we should add a namecache flag for no-caching
904 * to uncache the negative hit as soon as possible, but
905 * we cannot simply destroy the entry because it is used
906 * as a placeholder by the caller.
908 if (error == ENOENT) {
911 if (nfsneg_cache_timeout)
912 nticks = nfsneg_cache_timeout * hz;
915 cache_setvp(ncp, NULL);
916 cache_settimeout(ncp, nticks);
918 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
924 * Success, get the file handle, do various checks, and load
925 * post-operation data from the reply packet. Theoretically
926 * we should never be looking up "." so, theoretically, we
927 * should never get the same file handle as our directory. But
928 * we check anyway. XXX
930 * Note that no timeout is set for the positive cache hit. We
931 * assume, theoretically, that ESTALE returns will be dealt with
932 * properly to handle NFS races and in anycase we cannot depend
933 * on a timeout to deal with NFS open/create/excl issues so instead
934 * of a bad hack here the rest of the NFS client code needs to do
937 nfsm_getfh(fhp, fhsize, v3);
940 if (NFS_CMPFH(np, fhp, fhsize)) {
944 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
953 nfsm_postop_attr(nvp, attrflag, NFS_LATTR_NOSHRINK);
954 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
956 nfsm_loadattr(nvp, NULL);
958 cache_setvp(ncp, nvp);
972 * 'cached' nfs directory lookup
974 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
976 * nfs_lookup(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
977 * struct vnode **a_vpp, struct componentname *a_cnp)
980 nfs_lookup(struct vop_lookup_args *ap)
982 struct componentname *cnp = ap->a_cnp;
983 struct vnode *dvp = ap->a_dvp;
984 struct vnode **vpp = ap->a_vpp;
985 int flags = cnp->cn_flags;
990 struct nfsmount *nmp;
991 caddr_t bpos, dpos, cp2;
992 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
996 int lockparent, wantparent, error = 0, attrflag, fhsize;
997 int v3 = NFS_ISV3(dvp);
998 struct thread *td = cnp->cn_td;
1001 * Read-only mount check and directory check.
1004 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1005 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
1008 if (dvp->v_type != VDIR)
1012 * Look it up in the cache. Note that ENOENT is only returned if we
1013 * previously entered a negative hit (see later on). The additional
1014 * nfsneg_cache_timeout check causes previously cached results to
1015 * be instantly ignored if the negative caching is turned off.
1017 lockparent = flags & CNP_LOCKPARENT;
1018 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1019 nmp = VFSTONFS(dvp->v_mount);
1027 nfsstats.lookupcache_misses++;
1028 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1029 len = cnp->cn_namelen;
1030 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
1031 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1032 nfsm_fhtom(dvp, v3);
1033 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1034 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
1036 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1040 nfsm_getfh(fhp, fhsize, v3);
1043 * Handle RENAME case...
1045 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1046 if (NFS_CMPFH(np, fhp, fhsize)) {
1050 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1057 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1058 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1060 nfsm_loadattr(newvp, (struct vattr *)0);
1064 VOP_UNLOCK(dvp, 0, td);
1065 cnp->cn_flags |= CNP_PDIRUNLOCK;
1070 if (flags & CNP_ISDOTDOT) {
1071 VOP_UNLOCK(dvp, 0, td);
1072 cnp->cn_flags |= CNP_PDIRUNLOCK;
1073 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1075 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
1076 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1077 return (error); /* NOTE: return error from nget */
1081 error = vn_lock(dvp, LK_EXCLUSIVE, td);
1086 cnp->cn_flags |= CNP_PDIRUNLOCK;
1088 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1092 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1098 VOP_UNLOCK(dvp, 0, td);
1099 cnp->cn_flags |= CNP_PDIRUNLOCK;
1104 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1105 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1107 nfsm_loadattr(newvp, (struct vattr *)0);
1109 /* XXX MOVE TO nfs_nremove() */
1110 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1111 cnp->cn_nameiop != NAMEI_DELETE) {
1112 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1119 if (newvp != NULLVP) {
1123 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1124 cnp->cn_nameiop == NAMEI_RENAME) &&
1127 VOP_UNLOCK(dvp, 0, td);
1128 cnp->cn_flags |= CNP_PDIRUNLOCK;
1130 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1133 error = EJUSTRETURN;
1141 * Just call nfs_bioread() to do the work.
1143 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1144 * struct ucred *a_cred)
1147 nfs_read(struct vop_read_args *ap)
1149 struct vnode *vp = ap->a_vp;
1151 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1152 switch (vp->v_type) {
1154 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1165 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1168 nfs_readlink(struct vop_readlink_args *ap)
1170 struct vnode *vp = ap->a_vp;
1172 if (vp->v_type != VLNK)
1174 return (nfs_bioread(vp, ap->a_uio, 0));
1178 * Do a readlink rpc.
1179 * Called by nfs_doio() from below the buffer cache.
1182 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1187 caddr_t bpos, dpos, cp2;
1188 int error = 0, len, attrflag;
1189 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1190 int v3 = NFS_ISV3(vp);
1192 nfsstats.rpccnt[NFSPROC_READLINK]++;
1193 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1195 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1197 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1199 nfsm_strsiz(len, NFS_MAXPATHLEN);
1200 if (len == NFS_MAXPATHLEN) {
1201 struct nfsnode *np = VTONFS(vp);
1202 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1205 nfsm_mtouio(uiop, len);
1217 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1222 caddr_t bpos, dpos, cp2;
1223 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1224 struct nfsmount *nmp;
1225 int error = 0, len, retlen, tsiz, eof, attrflag;
1226 int v3 = NFS_ISV3(vp);
1231 nmp = VFSTONFS(vp->v_mount);
1232 tsiz = uiop->uio_resid;
1233 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1236 nfsstats.rpccnt[NFSPROC_READ]++;
1237 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1238 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1240 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1242 txdr_hyper(uiop->uio_offset, tl);
1243 *(tl + 2) = txdr_unsigned(len);
1245 *tl++ = txdr_unsigned(uiop->uio_offset);
1246 *tl++ = txdr_unsigned(len);
1249 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1251 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1256 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1257 eof = fxdr_unsigned(int, *(tl + 1));
1259 nfsm_loadattr(vp, (struct vattr *)0);
1260 nfsm_strsiz(retlen, nmp->nm_rsize);
1261 nfsm_mtouio(uiop, retlen);
1265 if (eof || retlen == 0) {
1268 } else if (retlen < len) {
1280 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, int *must_commit)
1284 int32_t t1, t2, backup;
1285 caddr_t bpos, dpos, cp2;
1286 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1287 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1288 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1289 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1292 if (uiop->uio_iovcnt != 1)
1293 panic("nfs: writerpc iovcnt > 1");
1296 tsiz = uiop->uio_resid;
1297 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1300 nfsstats.rpccnt[NFSPROC_WRITE]++;
1301 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1302 nfsm_reqhead(vp, NFSPROC_WRITE,
1303 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1306 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1307 txdr_hyper(uiop->uio_offset, tl);
1309 *tl++ = txdr_unsigned(len);
1310 *tl++ = txdr_unsigned(*iomode);
1311 *tl = txdr_unsigned(len);
1315 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1316 /* Set both "begin" and "current" to non-garbage. */
1317 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1318 *tl++ = x; /* "begin offset" */
1319 *tl++ = x; /* "current offset" */
1320 x = txdr_unsigned(len);
1321 *tl++ = x; /* total to this offset */
1322 *tl = x; /* size of this write */
1324 nfsm_uiotom(uiop, len);
1325 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1328 * The write RPC returns a before and after mtime. The
1329 * nfsm_wcc_data() macro checks the before n_mtime
1330 * against the before time and stores the after time
1331 * in the nfsnode's cached vattr and n_mtime field.
1332 * The NRMODIFIED bit will be set if the before
1333 * time did not match the original mtime.
1335 wccflag = NFSV3_WCCCHK;
1336 nfsm_wcc_data(vp, wccflag);
1338 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1339 + NFSX_V3WRITEVERF);
1340 rlen = fxdr_unsigned(int, *tl++);
1345 } else if (rlen < len) {
1346 backup = len - rlen;
1347 uiop->uio_iov->iov_base -= backup;
1348 uiop->uio_iov->iov_len += backup;
1349 uiop->uio_offset -= backup;
1350 uiop->uio_resid += backup;
1353 commit = fxdr_unsigned(int, *tl++);
1356 * Return the lowest committment level
1357 * obtained by any of the RPCs.
1359 if (committed == NFSV3WRITE_FILESYNC)
1361 else if (committed == NFSV3WRITE_DATASYNC &&
1362 commit == NFSV3WRITE_UNSTABLE)
1364 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1365 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1367 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1368 } else if (bcmp((caddr_t)tl,
1369 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1371 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1376 nfsm_loadattr(vp, (struct vattr *)0);
1384 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1385 committed = NFSV3WRITE_FILESYNC;
1386 *iomode = committed;
1388 uiop->uio_resid = tsiz;
1394 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1395 * mode set to specify the file type and the size field for rdev.
1398 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1401 struct nfsv2_sattr *sp;
1405 struct vnode *newvp = (struct vnode *)0;
1406 struct nfsnode *np = (struct nfsnode *)0;
1410 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1411 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1413 int v3 = NFS_ISV3(dvp);
1415 if (vap->va_type == VCHR || vap->va_type == VBLK)
1416 rdev = txdr_unsigned(vap->va_rdev);
1417 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1420 return (EOPNOTSUPP);
1422 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1425 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1426 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1427 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1428 nfsm_fhtom(dvp, v3);
1429 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1431 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1432 *tl++ = vtonfsv3_type(vap->va_type);
1433 nfsm_v3attrbuild(vap, FALSE);
1434 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1435 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1436 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1437 *tl = txdr_unsigned(uminor(vap->va_rdev));
1440 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1441 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1442 sp->sa_uid = nfs_xdrneg1;
1443 sp->sa_gid = nfs_xdrneg1;
1445 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1446 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1448 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1450 nfsm_mtofh(dvp, newvp, v3, gotvp);
1454 newvp = (struct vnode *)0;
1456 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1457 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1463 nfsm_wcc_data(dvp, wccflag);
1472 VTONFS(dvp)->n_flag |= NLMODIFIED;
1474 VTONFS(dvp)->n_attrstamp = 0;
1480 * just call nfs_mknodrpc() to do the work.
1482 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1483 * struct componentname *a_cnp, struct vattr *a_vap)
1487 nfs_mknod(struct vop_mknod_args *ap)
1489 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1492 static u_long create_verf;
1494 * nfs file create call
1496 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1497 * struct componentname *a_cnp, struct vattr *a_vap)
1500 nfs_create(struct vop_create_args *ap)
1502 struct vnode *dvp = ap->a_dvp;
1503 struct vattr *vap = ap->a_vap;
1504 struct componentname *cnp = ap->a_cnp;
1505 struct nfsv2_sattr *sp;
1509 struct nfsnode *np = (struct nfsnode *)0;
1510 struct vnode *newvp = (struct vnode *)0;
1511 caddr_t bpos, dpos, cp2;
1512 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1513 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1515 int v3 = NFS_ISV3(dvp);
1518 * Oops, not for me..
1520 if (vap->va_type == VSOCK)
1521 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1523 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1526 if (vap->va_vaflags & VA_EXCLUSIVE)
1529 nfsstats.rpccnt[NFSPROC_CREATE]++;
1530 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1531 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1532 nfsm_fhtom(dvp, v3);
1533 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1535 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1536 if (fmode & O_EXCL) {
1537 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1538 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1540 if (!TAILQ_EMPTY(&in_ifaddrhead))
1541 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr;
1544 *tl++ = create_verf;
1545 *tl = ++create_verf;
1547 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1548 nfsm_v3attrbuild(vap, FALSE);
1551 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1552 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1553 sp->sa_uid = nfs_xdrneg1;
1554 sp->sa_gid = nfs_xdrneg1;
1556 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1557 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1559 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1561 nfsm_mtofh(dvp, newvp, v3, gotvp);
1565 newvp = (struct vnode *)0;
1567 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1568 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1574 nfsm_wcc_data(dvp, wccflag);
1578 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1584 } else if (v3 && (fmode & O_EXCL)) {
1586 * We are normally called with only a partially initialized
1587 * VAP. Since the NFSv3 spec says that server may use the
1588 * file attributes to store the verifier, the spec requires
1589 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1590 * in atime, but we can't really assume that all servers will
1591 * so we ensure that our SETATTR sets both atime and mtime.
1593 if (vap->va_mtime.tv_sec == VNOVAL)
1594 vfs_timestamp(&vap->va_mtime);
1595 if (vap->va_atime.tv_sec == VNOVAL)
1596 vap->va_atime = vap->va_mtime;
1597 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1601 * The new np may have enough info for access
1602 * checks, make sure rucred and wucred are
1603 * initialized for read and write rpc's.
1606 if (np->n_rucred == NULL)
1607 np->n_rucred = crhold(cnp->cn_cred);
1608 if (np->n_wucred == NULL)
1609 np->n_wucred = crhold(cnp->cn_cred);
1612 VTONFS(dvp)->n_flag |= NLMODIFIED;
1614 VTONFS(dvp)->n_attrstamp = 0;
1619 * nfs file remove call
1620 * To try and make nfs semantics closer to ufs semantics, a file that has
1621 * other processes using the vnode is renamed instead of removed and then
1622 * removed later on the last close.
1623 * - If v_usecount > 1
1624 * If a rename is not already in the works
1625 * call nfs_sillyrename() to set it up
1629 * nfs_remove(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
1630 * struct vnode *a_vp, struct componentname *a_cnp)
1633 nfs_remove(struct vop_remove_args *ap)
1635 struct vnode *vp = ap->a_vp;
1636 struct vnode *dvp = ap->a_dvp;
1637 struct componentname *cnp = ap->a_cnp;
1638 struct nfsnode *np = VTONFS(vp);
1643 if (vp->v_usecount < 1)
1644 panic("nfs_remove: bad v_usecount");
1646 if (vp->v_type == VDIR)
1648 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1649 VOP_GETATTR(vp, &vattr, cnp->cn_td) == 0 &&
1650 vattr.va_nlink > 1)) {
1652 * throw away biocache buffers, mainly to avoid
1653 * unnecessary delayed writes later.
1655 error = nfs_vinvalbuf(vp, 0, cnp->cn_td, 1);
1658 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1659 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1661 * Kludge City: If the first reply to the remove rpc is lost..
1662 * the reply to the retransmitted request will be ENOENT
1663 * since the file was in fact removed
1664 * Therefore, we cheat and return success.
1666 if (error == ENOENT)
1668 } else if (!np->n_sillyrename) {
1669 error = nfs_sillyrename(dvp, vp, cnp);
1671 np->n_attrstamp = 0;
1676 * nfs file remove rpc called from nfs_inactive
1679 nfs_removeit(struct sillyrename *sp)
1681 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1686 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1689 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1690 struct ucred *cred, struct thread *td)
1695 caddr_t bpos, dpos, cp2;
1696 int error = 0, wccflag = NFSV3_WCCRATTR;
1697 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1698 int v3 = NFS_ISV3(dvp);
1700 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1701 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1702 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1703 nfsm_fhtom(dvp, v3);
1704 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1705 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1707 nfsm_wcc_data(dvp, wccflag);
1710 VTONFS(dvp)->n_flag |= NLMODIFIED;
1712 VTONFS(dvp)->n_attrstamp = 0;
1717 * nfs file rename call
1719 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1720 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1721 * struct vnode *a_tvp, struct componentname *a_tcnp)
1724 nfs_rename(struct vop_rename_args *ap)
1726 struct vnode *fvp = ap->a_fvp;
1727 struct vnode *tvp = ap->a_tvp;
1728 struct vnode *fdvp = ap->a_fdvp;
1729 struct vnode *tdvp = ap->a_tdvp;
1730 struct componentname *tcnp = ap->a_tcnp;
1731 struct componentname *fcnp = ap->a_fcnp;
1734 /* Check for cross-device rename */
1735 if ((fvp->v_mount != tdvp->v_mount) ||
1736 (tvp && (fvp->v_mount != tvp->v_mount))) {
1742 * We have to flush B_DELWRI data prior to renaming
1743 * the file. If we don't, the delayed-write buffers
1744 * can be flushed out later after the file has gone stale
1745 * under NFSV3. NFSV2 does not have this problem because
1746 * ( as far as I can tell ) it flushes dirty buffers more
1750 VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_td);
1752 VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_td);
1755 * If the tvp exists and is in use, sillyrename it before doing the
1756 * rename of the new file over it.
1758 * XXX Can't sillyrename a directory.
1760 * We do not attempt to do any namecache purges in this old API
1761 * routine. The new API compat functions have access to the actual
1762 * namecache structures and will do it for us.
1764 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1765 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1772 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1773 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1786 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1788 if (error == ENOENT)
1794 * nfs file rename rpc called from nfs_remove() above
1797 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1798 struct sillyrename *sp)
1800 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1801 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1805 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1808 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1809 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1810 struct ucred *cred, struct thread *td)
1815 caddr_t bpos, dpos, cp2;
1816 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1817 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1818 int v3 = NFS_ISV3(fdvp);
1820 nfsstats.rpccnt[NFSPROC_RENAME]++;
1821 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1822 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1823 nfsm_rndup(tnamelen));
1824 nfsm_fhtom(fdvp, v3);
1825 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1826 nfsm_fhtom(tdvp, v3);
1827 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1828 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1830 nfsm_wcc_data(fdvp, fwccflag);
1831 nfsm_wcc_data(tdvp, twccflag);
1835 VTONFS(fdvp)->n_flag |= NLMODIFIED;
1836 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1838 VTONFS(fdvp)->n_attrstamp = 0;
1840 VTONFS(tdvp)->n_attrstamp = 0;
1845 * nfs hard link create call
1847 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1848 * struct componentname *a_cnp)
1851 nfs_link(struct vop_link_args *ap)
1853 struct vnode *vp = ap->a_vp;
1854 struct vnode *tdvp = ap->a_tdvp;
1855 struct componentname *cnp = ap->a_cnp;
1859 caddr_t bpos, dpos, cp2;
1860 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1861 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1864 if (vp->v_mount != tdvp->v_mount) {
1869 * Push all writes to the server, so that the attribute cache
1870 * doesn't get "out of sync" with the server.
1871 * XXX There should be a better way!
1873 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_td);
1876 nfsstats.rpccnt[NFSPROC_LINK]++;
1877 nfsm_reqhead(vp, NFSPROC_LINK,
1878 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1880 nfsm_fhtom(tdvp, v3);
1881 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1882 nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1884 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1885 nfsm_wcc_data(tdvp, wccflag);
1889 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1891 VTONFS(vp)->n_attrstamp = 0;
1893 VTONFS(tdvp)->n_attrstamp = 0;
1895 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1897 if (error == EEXIST)
1903 * nfs symbolic link create call
1905 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1906 * struct componentname *a_cnp, struct vattr *a_vap,
1910 nfs_symlink(struct vop_symlink_args *ap)
1912 struct vnode *dvp = ap->a_dvp;
1913 struct vattr *vap = ap->a_vap;
1914 struct componentname *cnp = ap->a_cnp;
1915 struct nfsv2_sattr *sp;
1919 caddr_t bpos, dpos, cp2;
1920 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1921 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1922 struct vnode *newvp = (struct vnode *)0;
1923 int v3 = NFS_ISV3(dvp);
1925 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1926 slen = strlen(ap->a_target);
1927 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1928 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1929 nfsm_fhtom(dvp, v3);
1930 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1932 nfsm_v3attrbuild(vap, FALSE);
1934 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1936 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1937 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1938 sp->sa_uid = nfs_xdrneg1;
1939 sp->sa_gid = nfs_xdrneg1;
1940 sp->sa_size = nfs_xdrneg1;
1941 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1942 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1946 * Issue the NFS request and get the rpc response.
1948 * Only NFSv3 responses returning an error of 0 actually return
1949 * a file handle that can be converted into newvp without having
1950 * to do an extra lookup rpc.
1952 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1955 nfsm_mtofh(dvp, newvp, v3, gotvp);
1956 nfsm_wcc_data(dvp, wccflag);
1960 * out code jumps -> here, mrep is also freed.
1967 * If we get an EEXIST error, silently convert it to no-error
1968 * in case of an NFS retry.
1970 if (error == EEXIST)
1974 * If we do not have (or no longer have) an error, and we could
1975 * not extract the newvp from the response due to the request being
1976 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1977 * to obtain a newvp to return.
1979 if (error == 0 && newvp == NULL) {
1980 struct nfsnode *np = NULL;
1982 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1983 cnp->cn_cred, cnp->cn_td, &np);
1993 VTONFS(dvp)->n_flag |= NLMODIFIED;
1995 VTONFS(dvp)->n_attrstamp = 0;
2002 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2003 * struct componentname *a_cnp, struct vattr *a_vap)
2006 nfs_mkdir(struct vop_mkdir_args *ap)
2008 struct vnode *dvp = ap->a_dvp;
2009 struct vattr *vap = ap->a_vap;
2010 struct componentname *cnp = ap->a_cnp;
2011 struct nfsv2_sattr *sp;
2016 struct nfsnode *np = (struct nfsnode *)0;
2017 struct vnode *newvp = (struct vnode *)0;
2018 caddr_t bpos, dpos, cp2;
2019 int error = 0, wccflag = NFSV3_WCCRATTR;
2021 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2023 int v3 = NFS_ISV3(dvp);
2025 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
2028 len = cnp->cn_namelen;
2029 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2030 nfsm_reqhead(dvp, NFSPROC_MKDIR,
2031 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2032 nfsm_fhtom(dvp, v3);
2033 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
2035 nfsm_v3attrbuild(vap, FALSE);
2037 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2038 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2039 sp->sa_uid = nfs_xdrneg1;
2040 sp->sa_gid = nfs_xdrneg1;
2041 sp->sa_size = nfs_xdrneg1;
2042 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2043 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2045 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
2047 nfsm_mtofh(dvp, newvp, v3, gotvp);
2049 nfsm_wcc_data(dvp, wccflag);
2052 VTONFS(dvp)->n_flag |= NLMODIFIED;
2054 VTONFS(dvp)->n_attrstamp = 0;
2056 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2057 * if we can succeed in looking up the directory.
2059 if (error == EEXIST || (!error && !gotvp)) {
2062 newvp = (struct vnode *)0;
2064 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2068 if (newvp->v_type != VDIR)
2081 * nfs remove directory call
2083 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2084 * struct componentname *a_cnp)
2087 nfs_rmdir(struct vop_rmdir_args *ap)
2089 struct vnode *vp = ap->a_vp;
2090 struct vnode *dvp = ap->a_dvp;
2091 struct componentname *cnp = ap->a_cnp;
2095 caddr_t bpos, dpos, cp2;
2096 int error = 0, wccflag = NFSV3_WCCRATTR;
2097 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2098 int v3 = NFS_ISV3(dvp);
2102 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2103 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2104 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2105 nfsm_fhtom(dvp, v3);
2106 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2107 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2109 nfsm_wcc_data(dvp, wccflag);
2112 VTONFS(dvp)->n_flag |= NLMODIFIED;
2114 VTONFS(dvp)->n_attrstamp = 0;
2116 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2118 if (error == ENOENT)
2126 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2129 nfs_readdir(struct vop_readdir_args *ap)
2131 struct vnode *vp = ap->a_vp;
2132 struct nfsnode *np = VTONFS(vp);
2133 struct uio *uio = ap->a_uio;
2137 if (vp->v_type != VDIR)
2141 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2142 * and then check that is still valid, or if this is an NQNFS mount
2143 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2144 * VOP_GETATTR() does not necessarily go to the wire.
2146 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2147 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2148 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
2149 if (NQNFS_CKCACHABLE(vp, ND_READ)) {
2150 nfsstats.direofcache_hits++;
2153 } else if (VOP_GETATTR(vp, &vattr, uio->uio_td) == 0 &&
2154 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2156 nfsstats.direofcache_hits++;
2162 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2163 * own cache coherency checks so we do not have to.
2165 tresid = uio->uio_resid;
2166 error = nfs_bioread(vp, uio, 0);
2168 if (!error && uio->uio_resid == tresid)
2169 nfsstats.direofcache_misses++;
2175 * Called from below the buffer cache by nfs_doio().
2178 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2181 struct dirent *dp = NULL;
2186 caddr_t bpos, dpos, cp2;
2187 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2189 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2190 struct nfsnode *dnp = VTONFS(vp);
2192 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2194 int v3 = NFS_ISV3(vp);
2197 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2198 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2199 panic("nfs readdirrpc bad uio");
2203 * If there is no cookie, assume directory was stale.
2205 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2209 return (NFSERR_BAD_COOKIE);
2211 * Loop around doing readdir rpc's of size nm_readdirsize
2212 * truncated to a multiple of DIRBLKSIZ.
2213 * The stopping criteria is EOF or buffer full.
2215 while (more_dirs && bigenough) {
2216 nfsstats.rpccnt[NFSPROC_READDIR]++;
2217 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2221 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2222 *tl++ = cookie.nfsuquad[0];
2223 *tl++ = cookie.nfsuquad[1];
2224 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2225 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2227 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2228 *tl++ = cookie.nfsuquad[0];
2230 *tl = txdr_unsigned(nmp->nm_readdirsize);
2231 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2233 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2235 nfsm_dissect(tl, u_int32_t *,
2237 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2238 dnp->n_cookieverf.nfsuquad[1] = *tl;
2244 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2245 more_dirs = fxdr_unsigned(int, *tl);
2247 /* loop thru the dir entries, doctoring them to 4bsd form */
2248 while (more_dirs && bigenough) {
2250 nfsm_dissect(tl, u_int32_t *,
2252 fileno = fxdr_hyper(tl);
2253 len = fxdr_unsigned(int, *(tl + 2));
2255 nfsm_dissect(tl, u_int32_t *,
2257 fileno = fxdr_unsigned(u_quad_t, *tl++);
2258 len = fxdr_unsigned(int, *tl);
2260 if (len <= 0 || len > NFS_MAXNAMLEN) {
2265 tlen = nfsm_rndup(len);
2267 tlen += 4; /* To ensure null termination */
2268 left = DIRBLKSIZ - blksiz;
2269 if ((tlen + DIRHDSIZ) > left) {
2270 dp->d_reclen += left;
2271 uiop->uio_iov->iov_base += left;
2272 uiop->uio_iov->iov_len -= left;
2273 uiop->uio_offset += left;
2274 uiop->uio_resid -= left;
2277 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2280 dp = (struct dirent *)uiop->uio_iov->iov_base;
2281 dp->d_fileno = (int)fileno;
2283 dp->d_reclen = tlen + DIRHDSIZ;
2284 dp->d_type = DT_UNKNOWN;
2285 blksiz += dp->d_reclen;
2286 if (blksiz == DIRBLKSIZ)
2288 uiop->uio_offset += DIRHDSIZ;
2289 uiop->uio_resid -= DIRHDSIZ;
2290 uiop->uio_iov->iov_base += DIRHDSIZ;
2291 uiop->uio_iov->iov_len -= DIRHDSIZ;
2292 nfsm_mtouio(uiop, len);
2293 cp = uiop->uio_iov->iov_base;
2295 *cp = '\0'; /* null terminate */
2296 uiop->uio_iov->iov_base += tlen;
2297 uiop->uio_iov->iov_len -= tlen;
2298 uiop->uio_offset += tlen;
2299 uiop->uio_resid -= tlen;
2301 nfsm_adv(nfsm_rndup(len));
2303 nfsm_dissect(tl, u_int32_t *,
2306 nfsm_dissect(tl, u_int32_t *,
2310 cookie.nfsuquad[0] = *tl++;
2312 cookie.nfsuquad[1] = *tl++;
2317 more_dirs = fxdr_unsigned(int, *tl);
2320 * If at end of rpc data, get the eof boolean
2323 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2324 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2329 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2330 * by increasing d_reclen for the last record.
2333 left = DIRBLKSIZ - blksiz;
2334 dp->d_reclen += left;
2335 uiop->uio_iov->iov_base += left;
2336 uiop->uio_iov->iov_len -= left;
2337 uiop->uio_offset += left;
2338 uiop->uio_resid -= left;
2342 * We are now either at the end of the directory or have filled the
2346 dnp->n_direofoffset = uiop->uio_offset;
2348 if (uiop->uio_resid > 0)
2349 printf("EEK! readdirrpc resid > 0\n");
2350 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2358 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2361 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2368 struct vnode *newvp;
2370 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2371 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2373 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2374 struct nfsnode *dnp = VTONFS(vp), *np;
2377 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2378 int attrflag, fhsize;
2379 struct namecache *ncp;
2380 struct namecache *dncp;
2381 struct nlcomponent nlc;
2384 dp = (struct dirent *)0;
2387 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2388 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2389 panic("nfs readdirplusrpc bad uio");
2392 * Obtain the namecache record for the directory so we have something
2393 * to use as a basis for creating the entries. This function will
2394 * return a held (but not locked) ncp. The ncp may be disconnected
2395 * from the tree and cannot be used for upward traversals, and the
2396 * ncp may be unnamed. Note that other unrelated operations may
2397 * cause the ncp to be named at any time.
2399 dncp = cache_fromdvp(vp, NULL, 0);
2400 bzero(&nlc, sizeof(nlc));
2404 * If there is no cookie, assume directory was stale.
2406 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2410 return (NFSERR_BAD_COOKIE);
2412 * Loop around doing readdir rpc's of size nm_readdirsize
2413 * truncated to a multiple of DIRBLKSIZ.
2414 * The stopping criteria is EOF or buffer full.
2416 while (more_dirs && bigenough) {
2417 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2418 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2419 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2421 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2422 *tl++ = cookie.nfsuquad[0];
2423 *tl++ = cookie.nfsuquad[1];
2424 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2425 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2426 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2427 *tl = txdr_unsigned(nmp->nm_rsize);
2428 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2429 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2434 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2435 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2436 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2437 more_dirs = fxdr_unsigned(int, *tl);
2439 /* loop thru the dir entries, doctoring them to 4bsd form */
2440 while (more_dirs && bigenough) {
2441 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2442 fileno = fxdr_hyper(tl);
2443 len = fxdr_unsigned(int, *(tl + 2));
2444 if (len <= 0 || len > NFS_MAXNAMLEN) {
2449 tlen = nfsm_rndup(len);
2451 tlen += 4; /* To ensure null termination*/
2452 left = DIRBLKSIZ - blksiz;
2453 if ((tlen + DIRHDSIZ) > left) {
2454 dp->d_reclen += left;
2455 uiop->uio_iov->iov_base += left;
2456 uiop->uio_iov->iov_len -= left;
2457 uiop->uio_offset += left;
2458 uiop->uio_resid -= left;
2461 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2464 dp = (struct dirent *)uiop->uio_iov->iov_base;
2465 dp->d_fileno = (int)fileno;
2467 dp->d_reclen = tlen + DIRHDSIZ;
2468 dp->d_type = DT_UNKNOWN;
2469 blksiz += dp->d_reclen;
2470 if (blksiz == DIRBLKSIZ)
2472 uiop->uio_offset += DIRHDSIZ;
2473 uiop->uio_resid -= DIRHDSIZ;
2474 uiop->uio_iov->iov_base += DIRHDSIZ;
2475 uiop->uio_iov->iov_len -= DIRHDSIZ;
2476 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2477 nlc.nlc_namelen = len;
2478 nfsm_mtouio(uiop, len);
2479 cp = uiop->uio_iov->iov_base;
2482 uiop->uio_iov->iov_base += tlen;
2483 uiop->uio_iov->iov_len -= tlen;
2484 uiop->uio_offset += tlen;
2485 uiop->uio_resid -= tlen;
2487 nfsm_adv(nfsm_rndup(len));
2488 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2490 cookie.nfsuquad[0] = *tl++;
2491 cookie.nfsuquad[1] = *tl++;
2496 * Since the attributes are before the file handle
2497 * (sigh), we must skip over the attributes and then
2498 * come back and get them.
2500 attrflag = fxdr_unsigned(int, *tl);
2504 nfsm_adv(NFSX_V3FATTR);
2505 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2506 doit = fxdr_unsigned(int, *tl);
2508 nfsm_getfh(fhp, fhsize, 1);
2509 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2514 error = nfs_nget(vp->v_mount, fhp,
2522 if (doit && bigenough) {
2527 nfsm_loadattr(newvp, (struct vattr *)0);
2531 IFTODT(VTTOIF(np->n_vattr.va_type));
2533 printf("NFS/READDIRPLUS, ENTER %*.*s\n",
2534 nlc.nlc_namelen, nlc.nlc_namelen,
2536 ncp = cache_nlookup(dncp, &nlc);
2537 cache_setunresolved(ncp);
2538 cache_setvp(ncp, newvp);
2541 printf("NFS/READDIRPLUS, UNABLE TO ENTER"
2543 nlc.nlc_namelen, nlc.nlc_namelen,
2548 /* Just skip over the file handle */
2549 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2550 i = fxdr_unsigned(int, *tl);
2551 nfsm_adv(nfsm_rndup(i));
2553 if (newvp != NULLVP) {
2560 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2561 more_dirs = fxdr_unsigned(int, *tl);
2564 * If at end of rpc data, get the eof boolean
2567 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2568 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2573 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2574 * by increasing d_reclen for the last record.
2577 left = DIRBLKSIZ - blksiz;
2578 dp->d_reclen += left;
2579 uiop->uio_iov->iov_base += left;
2580 uiop->uio_iov->iov_len -= left;
2581 uiop->uio_offset += left;
2582 uiop->uio_resid -= left;
2586 * We are now either at the end of the directory or have filled the
2590 dnp->n_direofoffset = uiop->uio_offset;
2592 if (uiop->uio_resid > 0)
2593 printf("EEK! readdirplusrpc resid > 0\n");
2594 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2598 if (newvp != NULLVP) {
2611 * Silly rename. To make the NFS filesystem that is stateless look a little
2612 * more like the "ufs" a remove of an active vnode is translated to a rename
2613 * to a funny looking filename that is removed by nfs_inactive on the
2614 * nfsnode. There is the potential for another process on a different client
2615 * to create the same funny name between the nfs_lookitup() fails and the
2616 * nfs_rename() completes, but...
2619 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2621 struct sillyrename *sp;
2626 * We previously purged dvp instead of vp. I don't know why, it
2627 * completely destroys performance. We can't do it anyway with the
2628 * new VFS API since we would be breaking the namecache topology.
2630 cache_purge(vp); /* XXX */
2633 if (vp->v_type == VDIR)
2634 panic("nfs: sillyrename dir");
2636 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2637 M_NFSREQ, M_WAITOK);
2638 sp->s_cred = crdup(cnp->cn_cred);
2642 /* Fudge together a funny name */
2643 sp->s_namlen = sprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
2645 /* Try lookitups until we get one that isn't there */
2646 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2647 cnp->cn_td, (struct nfsnode **)0) == 0) {
2649 if (sp->s_name[4] > 'z') {
2654 error = nfs_renameit(dvp, cnp, sp);
2657 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2659 np->n_sillyrename = sp;
2664 free((caddr_t)sp, M_NFSREQ);
2669 * Look up a file name and optionally either update the file handle or
2670 * allocate an nfsnode, depending on the value of npp.
2671 * npp == NULL --> just do the lookup
2672 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2674 * *npp != NULL --> update the file handle in the vnode
2677 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2678 struct thread *td, struct nfsnode **npp)
2683 struct vnode *newvp = (struct vnode *)0;
2684 struct nfsnode *np, *dnp = VTONFS(dvp);
2685 caddr_t bpos, dpos, cp2;
2686 int error = 0, fhlen, attrflag;
2687 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2689 int v3 = NFS_ISV3(dvp);
2691 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2692 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2693 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2694 nfsm_fhtom(dvp, v3);
2695 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2696 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2697 if (npp && !error) {
2698 nfsm_getfh(nfhp, fhlen, v3);
2701 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2702 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2703 np->n_fhp = &np->n_fh;
2704 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2705 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2706 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2707 np->n_fhsize = fhlen;
2709 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2713 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2721 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
2722 if (!attrflag && *npp == NULL) {
2731 nfsm_loadattr(newvp, (struct vattr *)0);
2735 if (npp && *npp == NULL) {
2750 * Nfs Version 3 commit rpc
2753 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2758 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2759 caddr_t bpos, dpos, cp2;
2760 int error = 0, wccflag = NFSV3_WCCRATTR;
2761 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2763 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2765 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2766 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2768 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2769 txdr_hyper(offset, tl);
2771 *tl = txdr_unsigned(cnt);
2772 nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2773 nfsm_wcc_data(vp, wccflag);
2775 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2776 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2777 NFSX_V3WRITEVERF)) {
2778 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2780 error = NFSERR_STALEWRITEVERF;
2790 * - make nfs_bmap() essentially a no-op that does no translation
2791 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2792 * (Maybe I could use the process's page mapping, but I was concerned that
2793 * Kernel Write might not be enabled and also figured copyout() would do
2794 * a lot more work than bcopy() and also it currently happens in the
2795 * context of the swapper process (2).
2797 * nfs_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
2798 * daddr_t *a_bnp, int *a_runp, int *a_runb)
2801 nfs_bmap(struct vop_bmap_args *ap)
2803 struct vnode *vp = ap->a_vp;
2805 if (ap->a_vpp != NULL)
2807 if (ap->a_bnp != NULL)
2808 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2809 if (ap->a_runp != NULL)
2811 if (ap->a_runb != NULL)
2818 * For async requests when nfsiod(s) are running, queue the request by
2819 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2823 nfs_strategy(struct vop_strategy_args *ap)
2825 struct buf *bp = ap->a_bp;
2829 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2830 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2832 if (bp->b_flags & B_PHYS)
2833 panic("nfs physio");
2835 if (bp->b_flags & B_ASYNC)
2838 td = curthread; /* XXX */
2841 * If the op is asynchronous and an i/o daemon is waiting
2842 * queue the request, wake it up and wait for completion
2843 * otherwise just do it ourselves.
2845 if ((bp->b_flags & B_ASYNC) == 0 ||
2846 nfs_asyncio(bp, td))
2847 error = nfs_doio(bp, td);
2854 * NB Currently unsupported.
2856 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
2857 * struct thread *a_td)
2861 nfs_mmap(struct vop_mmap_args *ap)
2867 * fsync vnode op. Just call nfs_flush() with commit == 1.
2869 * nfs_fsync(struct vnodeop_desc *a_desc, struct vnode *a_vp,
2870 * struct ucred * a_cred, int a_waitfor, struct thread *a_td)
2874 nfs_fsync(struct vop_fsync_args *ap)
2876 return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2880 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
2881 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
2882 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
2883 * set the buffer contains data that has already been written to the server
2884 * and which now needs a commit RPC.
2886 * If commit is 0 we only take one pass and only flush buffers containing new
2889 * If commit is 1 we take two passes, issuing a commit RPC in the second
2892 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
2893 * to completely flush all pending data.
2895 * Note that the RB_SCAN code properly handles the case where the
2896 * callback might block and directly or indirectly (another thread) cause
2897 * the RB tree to change.
2900 #ifndef NFS_COMMITBVECSIZ
2901 #define NFS_COMMITBVECSIZ 16
2904 struct nfs_flush_info {
2905 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
2912 struct buf *bvary[NFS_COMMITBVECSIZ];
2918 static int nfs_flush_bp(struct buf *bp, void *data);
2919 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
2922 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
2924 struct nfsnode *np = VTONFS(vp);
2925 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2926 struct nfs_flush_info info;
2929 bzero(&info, sizeof(info));
2932 info.waitfor = waitfor;
2933 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
2940 info.mode = NFI_FLUSHNEW;
2941 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2942 nfs_flush_bp, &info);
2945 * Take a second pass if committing and no error occured.
2946 * Clean up any left over collection (whether an error
2949 if (commit && error == 0) {
2950 info.mode = NFI_COMMIT;
2951 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2952 nfs_flush_bp, &info);
2954 error = nfs_flush_docommit(&info, error);
2958 * Wait for pending I/O to complete before checking whether
2959 * any further dirty buffers exist.
2961 while (waitfor == MNT_WAIT && vp->v_numoutput) {
2962 vp->v_flag |= VBWAIT;
2963 error = tsleep((caddr_t)&vp->v_numoutput,
2964 info.slpflag, "nfsfsync", info.slptimeo);
2967 * We have to be able to break out if this
2968 * is an 'intr' mount.
2970 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
2976 * Since we do not process pending signals,
2977 * once we get a PCATCH our tsleep() will no
2978 * longer sleep, switch to a fixed timeout
2981 if (info.slpflag == PCATCH) {
2983 info.slptimeo = 2 * hz;
2990 * Loop if we are flushing synchronous as well as committing,
2991 * and dirty buffers are still present. Otherwise we might livelock.
2993 } while (waitfor == MNT_WAIT && commit &&
2994 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
2997 * The callbacks have to return a negative error to terminate the
3004 * Deal with any error collection
3006 if (np->n_flag & NWRITEERR) {
3007 error = np->n_error;
3008 np->n_flag &= ~NWRITEERR;
3016 nfs_flush_bp(struct buf *bp, void *data)
3018 struct nfs_flush_info *info = data;
3023 switch(info->mode) {
3026 if (info->loops && info->waitfor == MNT_WAIT) {
3027 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3029 error = BUF_TIMELOCK(bp,
3030 LK_EXCLUSIVE | LK_SLEEPFAIL,
3032 info->slpflag, info->slptimeo);
3035 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3038 if ((bp->b_flags & B_DELWRI) == 0)
3039 panic("nfs_fsync: not dirty");
3040 if (bp->b_flags & B_NEEDCOMMIT) {
3047 bp->b_flags |= B_ASYNC;
3049 VOP_BWRITE(bp->b_vp, bp);
3057 * Only process buffers in need of a commit which we can
3058 * immediately lock. This may prevent a buffer from being
3059 * committed, but the normal flush loop will block on the
3060 * same buffer so we shouldn't get into an endless loop.
3063 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3064 (B_DELWRI | B_NEEDCOMMIT) ||
3065 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
3073 * NOTE: we are not clearing B_DONE here, so we have
3074 * to do it later on in this routine if we intend to
3075 * initiate I/O on the bp.
3077 * Note: to avoid loopback deadlocks, we do not
3078 * assign b_runningbufspace.
3080 vfs_busy_pages(bp, 1);
3082 info->bvary[info->bvsize] = bp;
3083 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3085 if (info->bvsize == 0 || toff < info->beg_off)
3086 info->beg_off = toff;
3087 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
3088 if (info->bvsize == 0 || toff > info->end_off)
3089 info->end_off = toff;
3091 if (info->bvsize == NFS_COMMITBVECSIZ) {
3092 error = nfs_flush_docommit(info, 0);
3093 KKASSERT(info->bvsize == 0);
3102 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)
3226 int oldflags = bp->b_flags;
3232 if (BUF_REFCNT(bp) == 0)
3233 panic("bwrite: buffer is not locked???");
3235 if (bp->b_flags & B_INVAL) {
3240 bp->b_flags |= B_CACHE;
3243 * Undirty the bp. We will redirty it later if the I/O fails.
3248 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3250 bp->b_vp->v_numoutput++;
3254 * Note: to avoid loopback deadlocks, we do not
3255 * assign b_runningbufspace.
3257 vfs_busy_pages(bp, 1);
3260 VOP_STRATEGY(bp->b_vp, bp);
3262 if( (oldflags & B_ASYNC) == 0) {
3263 int rtval = biowait(bp);
3265 if (oldflags & B_DELWRI) {
3267 reassignbuf(bp, bp->b_vp);
3279 * nfs special file access vnode op.
3280 * Essentially just get vattr and then imitate iaccess() since the device is
3281 * local to the client.
3283 * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
3284 * struct thread *a_td)
3287 nfsspec_access(struct vop_access_args *ap)
3291 struct ucred *cred = ap->a_cred;
3292 struct vnode *vp = ap->a_vp;
3293 mode_t mode = ap->a_mode;
3299 * Disallow write attempts on filesystems mounted read-only;
3300 * unless the file is a socket, fifo, or a block or character
3301 * device resident on the filesystem.
3303 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3304 switch (vp->v_type) {
3314 * If you're the super-user,
3315 * you always get access.
3317 if (cred->cr_uid == 0)
3320 error = VOP_GETATTR(vp, vap, ap->a_td);
3324 * Access check is based on only one of owner, group, public.
3325 * If not owner, then check group. If not a member of the
3326 * group, then check public access.
3328 if (cred->cr_uid != vap->va_uid) {
3330 gp = cred->cr_groups;
3331 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3332 if (vap->va_gid == *gp)
3338 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3343 * Read wrapper for special devices.
3345 * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3346 * struct ucred *a_cred)
3349 nfsspec_read(struct vop_read_args *ap)
3351 struct nfsnode *np = VTONFS(ap->a_vp);
3357 getnanotime(&np->n_atim);
3358 return (VOCALL(spec_vnode_vops, &ap->a_head));
3362 * Write wrapper for special devices.
3364 * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3365 * struct ucred *a_cred)
3368 nfsspec_write(struct vop_write_args *ap)
3370 struct nfsnode *np = VTONFS(ap->a_vp);
3376 getnanotime(&np->n_mtim);
3377 return (VOCALL(spec_vnode_vops, &ap->a_head));
3381 * Close wrapper for special devices.
3383 * Update the times on the nfsnode then do device close.
3385 * nfsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
3386 * struct thread *a_td)
3389 nfsspec_close(struct vop_close_args *ap)
3391 struct vnode *vp = ap->a_vp;
3392 struct nfsnode *np = VTONFS(vp);
3395 if (np->n_flag & (NACC | NUPD)) {
3397 if (vp->v_usecount == 1 &&
3398 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3400 if (np->n_flag & NACC)
3401 vattr.va_atime = np->n_atim;
3402 if (np->n_flag & NUPD)
3403 vattr.va_mtime = np->n_mtim;
3404 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3407 return (VOCALL(spec_vnode_vops, &ap->a_head));
3411 * Read wrapper for fifos.
3413 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3414 * struct ucred *a_cred)
3417 nfsfifo_read(struct vop_read_args *ap)
3419 struct nfsnode *np = VTONFS(ap->a_vp);
3425 getnanotime(&np->n_atim);
3426 return (VOCALL(fifo_vnode_vops, &ap->a_head));
3430 * Write wrapper for fifos.
3432 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3433 * struct ucred *a_cred)
3436 nfsfifo_write(struct vop_write_args *ap)
3438 struct nfsnode *np = VTONFS(ap->a_vp);
3444 getnanotime(&np->n_mtim);
3445 return (VOCALL(fifo_vnode_vops, &ap->a_head));
3449 * Close wrapper for fifos.
3451 * Update the times on the nfsnode then do fifo close.
3453 * nfsfifo_close(struct vnode *a_vp, int a_fflag, struct thread *a_td)
3456 nfsfifo_close(struct vop_close_args *ap)
3458 struct vnode *vp = ap->a_vp;
3459 struct nfsnode *np = VTONFS(vp);
3463 if (np->n_flag & (NACC | NUPD)) {
3465 if (np->n_flag & NACC)
3467 if (np->n_flag & NUPD)
3470 if (vp->v_usecount == 1 &&
3471 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3473 if (np->n_flag & NACC)
3474 vattr.va_atime = np->n_atim;
3475 if (np->n_flag & NUPD)
3476 vattr.va_mtime = np->n_mtim;
3477 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3480 return (VOCALL(fifo_vnode_vops, &ap->a_head));