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.35 2004/10/12 19:21:01 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/socket.h>
59 #include <sys/vnode.h>
60 #include <sys/dirent.h>
61 #include <sys/fcntl.h>
62 #include <sys/lockf.h>
64 #include <sys/sysctl.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_zone.h>
73 #include <vfs/fifofs/fifo.h>
81 #include "nfsm_subs.h"
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
93 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
94 * calls are not in getblk() and brelse() so that they would not be necessary
98 #define vfs_busy_pages(bp, f)
101 static int nfsspec_read (struct vop_read_args *);
102 static int nfsspec_write (struct vop_write_args *);
103 static int nfsfifo_read (struct vop_read_args *);
104 static int nfsfifo_write (struct vop_write_args *);
105 static int nfsspec_close (struct vop_close_args *);
106 static int nfsfifo_close (struct vop_close_args *);
107 #define nfs_poll vop_nopoll
108 static int nfs_flush (struct vnode *,int,struct thread *,int);
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 * Global vfs data structures for nfs
141 struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
142 { &vop_default_desc, vop_defaultop },
143 { &vop_access_desc, (void *) nfs_access },
144 { &vop_advlock_desc, (void *) nfs_advlock },
145 { &vop_bmap_desc, (void *) nfs_bmap },
146 { &vop_bwrite_desc, (void *) nfs_bwrite },
147 { &vop_close_desc, (void *) nfs_close },
148 { &vop_create_desc, (void *) nfs_create },
149 { &vop_fsync_desc, (void *) nfs_fsync },
150 { &vop_getattr_desc, (void *) nfs_getattr },
151 { &vop_getpages_desc, (void *) nfs_getpages },
152 { &vop_putpages_desc, (void *) nfs_putpages },
153 { &vop_inactive_desc, (void *) nfs_inactive },
154 { &vop_islocked_desc, (void *) vop_stdislocked },
155 { &vop_lease_desc, vop_null },
156 { &vop_link_desc, (void *) nfs_link },
157 { &vop_lock_desc, (void *) vop_stdlock },
158 { &vop_lookup_desc, (void *) nfs_lookup },
159 { &vop_mkdir_desc, (void *) nfs_mkdir },
160 { &vop_mknod_desc, (void *) nfs_mknod },
161 { &vop_mmap_desc, (void *) nfs_mmap },
162 { &vop_open_desc, (void *) nfs_open },
163 { &vop_poll_desc, (void *) nfs_poll },
164 { &vop_print_desc, (void *) nfs_print },
165 { &vop_read_desc, (void *) nfs_read },
166 { &vop_readdir_desc, (void *) nfs_readdir },
167 { &vop_readlink_desc, (void *) nfs_readlink },
168 { &vop_reclaim_desc, (void *) nfs_reclaim },
169 { &vop_remove_desc, (void *) nfs_remove },
170 { &vop_rename_desc, (void *) nfs_rename },
171 { &vop_rmdir_desc, (void *) nfs_rmdir },
172 { &vop_setattr_desc, (void *) nfs_setattr },
173 { &vop_strategy_desc, (void *) nfs_strategy },
174 { &vop_symlink_desc, (void *) nfs_symlink },
175 { &vop_unlock_desc, (void *) vop_stdunlock },
176 { &vop_write_desc, (void *) nfs_write },
181 * Special device vnode ops
183 struct vnodeopv_entry_desc nfsv2_specop_entries[] = {
184 { &vop_default_desc, (void *) spec_vnoperate },
185 { &vop_access_desc, (void *) nfsspec_access },
186 { &vop_close_desc, (void *) nfsspec_close },
187 { &vop_fsync_desc, (void *) nfs_fsync },
188 { &vop_getattr_desc, (void *) nfs_getattr },
189 { &vop_inactive_desc, (void *) nfs_inactive },
190 { &vop_islocked_desc, (void *) vop_stdislocked },
191 { &vop_lock_desc, (void *) vop_stdlock },
192 { &vop_print_desc, (void *) nfs_print },
193 { &vop_read_desc, (void *) nfsspec_read },
194 { &vop_reclaim_desc, (void *) nfs_reclaim },
195 { &vop_setattr_desc, (void *) nfs_setattr },
196 { &vop_unlock_desc, (void *) vop_stdunlock },
197 { &vop_write_desc, (void *) nfsspec_write },
201 struct vnodeopv_entry_desc nfsv2_fifoop_entries[] = {
202 { &vop_default_desc, (void *) fifo_vnoperate },
203 { &vop_access_desc, (void *) nfsspec_access },
204 { &vop_close_desc, (void *) nfsfifo_close },
205 { &vop_fsync_desc, (void *) nfs_fsync },
206 { &vop_getattr_desc, (void *) nfs_getattr },
207 { &vop_inactive_desc, (void *) nfs_inactive },
208 { &vop_islocked_desc, (void *) vop_stdislocked },
209 { &vop_lock_desc, (void *) vop_stdlock },
210 { &vop_print_desc, (void *) nfs_print },
211 { &vop_read_desc, (void *) nfsfifo_read },
212 { &vop_reclaim_desc, (void *) nfs_reclaim },
213 { &vop_setattr_desc, (void *) nfs_setattr },
214 { &vop_unlock_desc, (void *) vop_stdunlock },
215 { &vop_write_desc, (void *) nfsfifo_write },
219 static int nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
220 struct componentname *cnp,
222 static int nfs_removerpc (struct vnode *dvp, const char *name,
224 struct ucred *cred, struct thread *td);
225 static int nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
226 int fnamelen, struct vnode *tdvp,
227 const char *tnameptr, int tnamelen,
228 struct ucred *cred, struct thread *td);
229 static int nfs_renameit (struct vnode *sdvp,
230 struct componentname *scnp,
231 struct sillyrename *sp);
236 extern u_int32_t nfs_true, nfs_false;
237 extern u_int32_t nfs_xdrneg1;
238 extern struct nfsstats nfsstats;
239 extern nfstype nfsv3_type[9];
240 struct thread *nfs_iodwant[NFS_MAXASYNCDAEMON];
241 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
242 int nfs_numasync = 0;
243 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
245 SYSCTL_DECL(_vfs_nfs);
247 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
248 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
249 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
251 static int nfsneg_cache_timeout = NFS_MINATTRTIMO;
252 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
253 &nfsneg_cache_timeout, 0, "NFS NEGATIVE ACCESS cache timeout");
255 static int nfsv3_commit_on_close = 0;
256 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
257 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
259 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
260 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
262 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
263 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
266 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
267 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
268 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
270 nfs3_access_otw(struct vnode *vp, int wmode,
271 struct thread *td, struct ucred *cred)
275 int error = 0, attrflag;
277 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
278 caddr_t bpos, dpos, cp2;
282 struct nfsnode *np = VTONFS(vp);
284 nfsstats.rpccnt[NFSPROC_ACCESS]++;
285 nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
287 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
288 *tl = txdr_unsigned(wmode);
289 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
290 nfsm_postop_attr(vp, attrflag);
292 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
293 rmode = fxdr_unsigned(u_int32_t, *tl);
295 np->n_modeuid = cred->cr_uid;
296 np->n_modestamp = mycpu->gd_time_seconds;
304 * nfs access vnode op.
305 * For nfs version 2, just return ok. File accesses may fail later.
306 * For nfs version 3, use the access rpc to check accessibility. If file modes
307 * are changed on the server, accesses might still fail later.
309 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
310 * struct thread *a_td)
313 nfs_access(struct vop_access_args *ap)
315 struct vnode *vp = ap->a_vp;
317 u_int32_t mode, wmode;
318 int v3 = NFS_ISV3(vp);
319 struct nfsnode *np = VTONFS(vp);
322 * Disallow write attempts on filesystems mounted read-only;
323 * unless the file is a socket, fifo, or a block or character
324 * device resident on the filesystem.
326 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
327 switch (vp->v_type) {
337 * For nfs v3, check to see if we have done this recently, and if
338 * so return our cached result instead of making an ACCESS call.
339 * If not, do an access rpc, otherwise you are stuck emulating
340 * ufs_access() locally using the vattr. This may not be correct,
341 * since the server may apply other access criteria such as
342 * client uid-->server uid mapping that we do not know about.
345 if (ap->a_mode & VREAD)
346 mode = NFSV3ACCESS_READ;
349 if (vp->v_type != VDIR) {
350 if (ap->a_mode & VWRITE)
351 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
352 if (ap->a_mode & VEXEC)
353 mode |= NFSV3ACCESS_EXECUTE;
355 if (ap->a_mode & VWRITE)
356 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
358 if (ap->a_mode & VEXEC)
359 mode |= NFSV3ACCESS_LOOKUP;
361 /* XXX safety belt, only make blanket request if caching */
362 if (nfsaccess_cache_timeout > 0) {
363 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
364 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
365 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
371 * Does our cached result allow us to give a definite yes to
374 if ((mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
375 (ap->a_cred->cr_uid == np->n_modeuid) &&
376 ((np->n_mode & mode) == mode)) {
377 nfsstats.accesscache_hits++;
380 * Either a no, or a don't know. Go to the wire.
382 nfsstats.accesscache_misses++;
383 error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred);
385 if ((np->n_mode & mode) != mode) {
391 if ((error = nfsspec_access(ap)) != 0)
395 * Attempt to prevent a mapped root from accessing a file
396 * which it shouldn't. We try to read a byte from the file
397 * if the user is root and the file is not zero length.
398 * After calling nfsspec_access, we should have the correct
401 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
402 && VTONFS(vp)->n_size > 0) {
409 auio.uio_iov = &aiov;
413 auio.uio_segflg = UIO_SYSSPACE;
414 auio.uio_rw = UIO_READ;
415 auio.uio_td = ap->a_td;
417 if (vp->v_type == VREG) {
418 error = nfs_readrpc(vp, &auio);
419 } else if (vp->v_type == VDIR) {
421 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
423 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
424 error = nfs_readdirrpc(vp, &auio);
426 } else if (vp->v_type == VLNK) {
427 error = nfs_readlinkrpc(vp, &auio);
434 * [re]record creds for reading and/or writing if access
435 * was granted. Assume the NFS server will grant read access
436 * for execute requests.
439 if ((ap->a_mode & (VREAD|VEXEC)) && ap->a_cred != np->n_rucred) {
442 crfree(np->n_rucred);
443 np->n_rucred = ap->a_cred;
445 if ((ap->a_mode & VWRITE) && ap->a_cred != np->n_wucred) {
448 crfree(np->n_wucred);
449 np->n_wucred = ap->a_cred;
457 * Check to see if the type is ok
458 * and that deletion is not in progress.
459 * For paged in text files, you will need to flush the page cache
460 * if consistency is lost.
462 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
463 * struct thread *a_td)
467 nfs_open(struct vop_open_args *ap)
469 struct vnode *vp = ap->a_vp;
470 struct nfsnode *np = VTONFS(vp);
471 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
475 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
477 printf("open eacces vtyp=%d\n",vp->v_type);
482 * Get a valid lease. If cached data is stale, flush it.
484 if (nmp->nm_flag & NFSMNT_NQNFS) {
485 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
487 error = nqnfs_getlease(vp, ND_READ, ap->a_td);
488 } while (error == NQNFS_EXPIRED);
491 if (np->n_lrev != np->n_brev ||
492 (np->n_flag & NQNFSNONCACHE)) {
493 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1))
497 np->n_brev = np->n_lrev;
501 if (np->n_flag & NMODIFIED) {
502 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1))
507 if (vp->v_type == VDIR)
508 np->n_direofoffset = 0;
509 error = VOP_GETATTR(vp, &vattr, ap->a_td);
512 np->n_mtime = vattr.va_mtime.tv_sec;
514 error = VOP_GETATTR(vp, &vattr, ap->a_td);
517 if (np->n_mtime != vattr.va_mtime.tv_sec) {
518 if (vp->v_type == VDIR)
519 np->n_direofoffset = 0;
520 if ((error = nfs_vinvalbuf(vp, V_SAVE,
521 ap->a_td, 1)) == EINTR) {
524 np->n_mtime = vattr.va_mtime.tv_sec;
528 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0)
529 np->n_attrstamp = 0; /* For Open/Close consistency */
535 * What an NFS client should do upon close after writing is a debatable issue.
536 * Most NFS clients push delayed writes to the server upon close, basically for
538 * 1 - So that any write errors may be reported back to the client process
539 * doing the close system call. By far the two most likely errors are
540 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
541 * 2 - To put a worst case upper bound on cache inconsistency between
542 * multiple clients for the file.
543 * There is also a consistency problem for Version 2 of the protocol w.r.t.
544 * not being able to tell if other clients are writing a file concurrently,
545 * since there is no way of knowing if the changed modify time in the reply
546 * is only due to the write for this client.
547 * (NFS Version 3 provides weak cache consistency data in the reply that
548 * should be sufficient to detect and handle this case.)
550 * The current code does the following:
551 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
552 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
553 * or commit them (this satisfies 1 and 2 except for the
554 * case where the server crashes after this close but
555 * before the commit RPC, which is felt to be "good
556 * enough". Changing the last argument to nfs_flush() to
557 * a 1 would force a commit operation, if it is felt a
558 * commit is necessary now.
559 * for NQNFS - do nothing now, since 2 is dealt with via leases and
560 * 1 should be dealt with via an fsync() system call for
561 * cases where write errors are important.
563 * nfs_close(struct vnodeop_desc *a_desc, struct vnode *a_vp, int a_fflag,
564 * struct ucred *a_cred, struct thread *a_td)
568 nfs_close(struct vop_close_args *ap)
570 struct vnode *vp = ap->a_vp;
571 struct nfsnode *np = VTONFS(vp);
574 if (vp->v_type == VREG) {
575 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 &&
576 (np->n_flag & NMODIFIED)) {
579 * Under NFSv3 we have dirty buffers to dispose of. We
580 * must flush them to the NFS server. We have the option
581 * of waiting all the way through the commit rpc or just
582 * waiting for the initial write. The default is to only
583 * wait through the initial write so the data is in the
584 * server's cache, which is roughly similar to the state
585 * a standard disk subsystem leaves the file in on close().
587 * We cannot clear the NMODIFIED bit in np->n_flag due to
588 * potential races with other processes, and certainly
589 * cannot clear it if we don't commit.
591 int cm = nfsv3_commit_on_close ? 1 : 0;
592 error = nfs_flush(vp, MNT_WAIT, ap->a_td, cm);
593 /* np->n_flag &= ~NMODIFIED; */
595 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
599 if (np->n_flag & NWRITEERR) {
600 np->n_flag &= ~NWRITEERR;
608 * nfs getattr call from vfs.
610 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred,
611 * struct thread *a_td)
614 nfs_getattr(struct vop_getattr_args *ap)
616 struct vnode *vp = ap->a_vp;
617 struct nfsnode *np = VTONFS(vp);
623 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
624 int v3 = NFS_ISV3(vp);
627 * Update local times for special files.
629 if (np->n_flag & (NACC | NUPD))
632 * First look in the cache.
634 if (nfs_getattrcache(vp, ap->a_vap) == 0)
637 if (v3 && nfsaccess_cache_timeout > 0) {
638 nfsstats.accesscache_misses++;
639 nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_td, nfs_vpcred(vp, ND_CHECK));
640 if (nfs_getattrcache(vp, ap->a_vap) == 0)
644 nfsstats.rpccnt[NFSPROC_GETATTR]++;
645 nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
647 nfsm_request(vp, NFSPROC_GETATTR, ap->a_td, nfs_vpcred(vp, ND_CHECK));
649 nfsm_loadattr(vp, ap->a_vap);
659 * nfs_setattr(struct vnodeop_desc *a_desc, struct vnode *a_vp,
660 * struct vattr *a_vap, struct ucred *a_cred,
661 * struct thread *a_td)
664 nfs_setattr(struct vop_setattr_args *ap)
666 struct vnode *vp = ap->a_vp;
667 struct nfsnode *np = VTONFS(vp);
668 struct vattr *vap = ap->a_vap;
677 * Setting of flags is not supported.
679 if (vap->va_flags != VNOVAL)
683 * Disallow write attempts if the filesystem is mounted read-only.
685 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
686 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
687 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
688 (vp->v_mount->mnt_flag & MNT_RDONLY))
690 if (vap->va_size != VNOVAL) {
691 switch (vp->v_type) {
698 if (vap->va_mtime.tv_sec == VNOVAL &&
699 vap->va_atime.tv_sec == VNOVAL &&
700 vap->va_mode == (mode_t)VNOVAL &&
701 vap->va_uid == (uid_t)VNOVAL &&
702 vap->va_gid == (gid_t)VNOVAL)
704 vap->va_size = VNOVAL;
708 * Disallow write attempts if the filesystem is
711 if (vp->v_mount->mnt_flag & MNT_RDONLY)
715 * We run vnode_pager_setsize() early (why?),
716 * we must set np->n_size now to avoid vinvalbuf
717 * V_SAVE races that might setsize a lower
722 error = nfs_meta_setsize(vp, ap->a_td, vap->va_size);
724 if (np->n_flag & NMODIFIED) {
725 if (vap->va_size == 0)
726 error = nfs_vinvalbuf(vp, 0, ap->a_td, 1);
728 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
731 vnode_pager_setsize(vp, np->n_size);
736 * np->n_size has already been set to vap->va_size
737 * in nfs_meta_setsize(). We must set it again since
738 * nfs_loadattrcache() could be called through
739 * nfs_meta_setsize() and could modify np->n_size.
741 * (note that nfs_loadattrcache() will have called
742 * vnode_pager_setsize() for us in that case).
744 np->n_vattr.va_size = np->n_size = vap->va_size;
746 } else if ((vap->va_mtime.tv_sec != VNOVAL ||
747 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NMODIFIED) &&
748 vp->v_type == VREG &&
749 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) == EINTR)
751 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
752 if (error && vap->va_size != VNOVAL) {
753 np->n_size = np->n_vattr.va_size = tsize;
754 vnode_pager_setsize(vp, np->n_size);
760 * Do an nfs setattr rpc.
763 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
764 struct ucred *cred, struct thread *td)
766 struct nfsv2_sattr *sp;
767 struct nfsnode *np = VTONFS(vp);
770 caddr_t bpos, dpos, cp2;
772 int error = 0, wccflag = NFSV3_WCCRATTR;
773 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
774 int v3 = NFS_ISV3(vp);
776 nfsstats.rpccnt[NFSPROC_SETATTR]++;
777 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
780 nfsm_v3attrbuild(vap, TRUE);
781 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
784 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
785 if (vap->va_mode == (mode_t)VNOVAL)
786 sp->sa_mode = nfs_xdrneg1;
788 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
789 if (vap->va_uid == (uid_t)VNOVAL)
790 sp->sa_uid = nfs_xdrneg1;
792 sp->sa_uid = txdr_unsigned(vap->va_uid);
793 if (vap->va_gid == (gid_t)VNOVAL)
794 sp->sa_gid = nfs_xdrneg1;
796 sp->sa_gid = txdr_unsigned(vap->va_gid);
797 sp->sa_size = txdr_unsigned(vap->va_size);
798 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
799 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
801 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
804 nfsm_wcc_data(vp, wccflag);
806 nfsm_loadattr(vp, (struct vattr *)0);
813 * 'cached' nfs directory lookup
815 * nfs_lookup(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
816 * struct vnode **a_vpp, struct componentname *a_cnp)
819 nfs_lookup(struct vop_lookup_args *ap)
821 struct componentname *cnp = ap->a_cnp;
822 struct vnode *dvp = ap->a_dvp;
823 struct vnode **vpp = ap->a_vpp;
824 int flags = cnp->cn_flags;
829 struct nfsmount *nmp;
830 caddr_t bpos, dpos, cp2;
831 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
835 int lockparent, wantparent, error = 0, attrflag, fhsize;
836 int v3 = NFS_ISV3(dvp);
837 struct thread *td = cnp->cn_td;
840 * Read-only mount check and directory check.
843 if ((flags & CNP_ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
844 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
847 if (dvp->v_type != VDIR)
851 * Look it up in the cache. Note that ENOENT is only returned if we
852 * previously entered a negative hit (see later on). The additional
853 * nfsneg_cache_timeout check causes previously cached results to
854 * be instantly ignored if the negative caching is turned off.
856 lockparent = flags & CNP_LOCKPARENT;
857 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
858 nmp = VFSTONFS(dvp->v_mount);
860 error = cache_lookup(dvp, vpp, cnp);
865 if (error == ENOENT) {
866 if (nfsneg_cache_timeout) {
873 printf("nfs_lookup: %*.*s weird error %d\n",
874 (int)cnp->cn_namelen, (int)cnp->cn_namelen,
875 cnp->cn_nameptr, error);
881 * At this point we have a cache hit (error should be -1).
882 * The vnode returned in *vpp will be referenced but not
885 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
894 * See the comment starting `Step through' in ufs/ufs_lookup.c
895 * for an explanation of the locking protocol
898 /* newvp already ref'd from lookup */
900 } else if (flags & CNP_ISDOTDOT) {
901 VOP_UNLOCK(dvp, 0, td);
902 cnp->cn_flags |= CNP_PDIRUNLOCK;
903 error = vget(newvp, LK_EXCLUSIVE, td);
904 vrele(newvp); /* get rid of ref from lookup */
905 if (!error && lockparent && (flags & CNP_ISLASTCN)) {
906 error = vn_lock(dvp, LK_EXCLUSIVE, td);
908 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
911 error = vget(newvp, LK_EXCLUSIVE, td);
912 vrele(newvp); /* get rid of ref from lookup */
913 if (!lockparent || error || !(flags & CNP_ISLASTCN)) {
914 VOP_UNLOCK(dvp, 0, td);
915 cnp->cn_flags |= CNP_PDIRUNLOCK;
920 * Attempt to do a better job synchronizing our cache
921 * to the NFS server by checking the vnode against
922 * the nfs-only cache via VOP_GETATTR().
924 * WARNING! An old ctime check has been removed. We
925 * can't just willy-nilly purge a directory vnode that
926 * might have children in the new VFS scheme. The
927 * ctime check was bogus anyway.
929 if (vpid == newvp->v_id) {
930 if (VOP_GETATTR(newvp, &vattr, td) == 0) {
931 nfsstats.lookupcache_hits++;
932 if (cnp->cn_nameiop != NAMEI_LOOKUP &&
933 (flags & CNP_ISLASTCN))
934 cnp->cn_flags |= CNP_SAVENAME;
940 if (lockparent && dvp != newvp && (flags & CNP_ISLASTCN)) {
941 VOP_UNLOCK(dvp, 0, td);
942 cnp->cn_flags |= CNP_PDIRUNLOCK;
945 error = vn_lock(dvp, LK_EXCLUSIVE, td);
947 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
955 * Cache miss, go the wire.
959 nfsstats.lookupcache_misses++;
960 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
961 len = cnp->cn_namelen;
962 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
963 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
965 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
966 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
969 * Cache negatve lookups to reduce NFS traffic, but use
972 if (error == ENOENT &&
973 (cnp->cn_flags & CNP_MAKEENTRY) &&
974 cnp->cn_nameiop == NAMEI_LOOKUP &&
975 nfsneg_cache_timeout) {
976 int toval = nfsneg_cache_timeout * hz;
977 if (cnp->cn_flags & CNP_CACHETIMEOUT) {
978 if (cnp->cn_timeout > toval)
979 cnp->cn_timeout = toval;
981 cnp->cn_flags |= CNP_CACHETIMEOUT;
982 cnp->cn_timeout = toval;
984 cache_enter(dvp, NULL, cnp);
986 nfsm_postop_attr(dvp, attrflag);
990 nfsm_getfh(fhp, fhsize, v3);
993 * Handle RENAME case...
995 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent && (flags & CNP_ISLASTCN)) {
996 if (NFS_CMPFH(np, fhp, fhsize)) {
1000 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1007 nfsm_postop_attr(newvp, attrflag);
1008 nfsm_postop_attr(dvp, attrflag);
1010 nfsm_loadattr(newvp, (struct vattr *)0);
1013 cnp->cn_flags |= CNP_SAVENAME;
1015 VOP_UNLOCK(dvp, 0, td);
1016 cnp->cn_flags |= CNP_PDIRUNLOCK;
1021 if (flags & CNP_ISDOTDOT) {
1022 VOP_UNLOCK(dvp, 0, td);
1023 cnp->cn_flags |= CNP_PDIRUNLOCK;
1024 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1026 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
1027 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1028 return (error); /* NOTE: return error from nget */
1031 if (lockparent && (flags & CNP_ISLASTCN)) {
1032 error = vn_lock(dvp, LK_EXCLUSIVE, td);
1037 cnp->cn_flags |= CNP_PDIRUNLOCK;
1039 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1043 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1048 if (!lockparent || !(flags & CNP_ISLASTCN)) {
1049 VOP_UNLOCK(dvp, 0, td);
1050 cnp->cn_flags |= CNP_PDIRUNLOCK;
1055 nfsm_postop_attr(newvp, attrflag);
1056 nfsm_postop_attr(dvp, attrflag);
1058 nfsm_loadattr(newvp, (struct vattr *)0);
1059 if (cnp->cn_nameiop != NAMEI_LOOKUP && (flags & CNP_ISLASTCN))
1060 cnp->cn_flags |= CNP_SAVENAME;
1061 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1062 (cnp->cn_nameiop != NAMEI_DELETE || !(flags & CNP_ISLASTCN))) {
1063 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
1064 cache_enter(dvp, newvp, cnp);
1070 if (newvp != NULLVP) {
1074 if ((cnp->cn_nameiop == NAMEI_CREATE || cnp->cn_nameiop == NAMEI_RENAME) &&
1075 (flags & CNP_ISLASTCN) && error == ENOENT) {
1077 VOP_UNLOCK(dvp, 0, td);
1078 cnp->cn_flags |= CNP_PDIRUNLOCK;
1080 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1083 error = EJUSTRETURN;
1085 if (cnp->cn_nameiop != NAMEI_LOOKUP && (flags & CNP_ISLASTCN))
1086 cnp->cn_flags |= CNP_SAVENAME;
1093 * Just call nfs_bioread() to do the work.
1095 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1096 * struct ucred *a_cred)
1099 nfs_read(struct vop_read_args *ap)
1101 struct vnode *vp = ap->a_vp;
1103 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1104 switch (vp->v_type) {
1106 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1117 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1120 nfs_readlink(struct vop_readlink_args *ap)
1122 struct vnode *vp = ap->a_vp;
1124 if (vp->v_type != VLNK)
1126 return (nfs_bioread(vp, ap->a_uio, 0));
1130 * Do a readlink rpc.
1131 * Called by nfs_doio() from below the buffer cache.
1134 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1139 caddr_t bpos, dpos, cp2;
1140 int error = 0, len, attrflag;
1141 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1142 int v3 = NFS_ISV3(vp);
1144 nfsstats.rpccnt[NFSPROC_READLINK]++;
1145 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1147 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1149 nfsm_postop_attr(vp, attrflag);
1151 nfsm_strsiz(len, NFS_MAXPATHLEN);
1152 if (len == NFS_MAXPATHLEN) {
1153 struct nfsnode *np = VTONFS(vp);
1154 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1157 nfsm_mtouio(uiop, len);
1169 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1174 caddr_t bpos, dpos, cp2;
1175 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1176 struct nfsmount *nmp;
1177 int error = 0, len, retlen, tsiz, eof, attrflag;
1178 int v3 = NFS_ISV3(vp);
1183 nmp = VFSTONFS(vp->v_mount);
1184 tsiz = uiop->uio_resid;
1185 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1188 nfsstats.rpccnt[NFSPROC_READ]++;
1189 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1190 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1192 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1194 txdr_hyper(uiop->uio_offset, tl);
1195 *(tl + 2) = txdr_unsigned(len);
1197 *tl++ = txdr_unsigned(uiop->uio_offset);
1198 *tl++ = txdr_unsigned(len);
1201 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1203 nfsm_postop_attr(vp, attrflag);
1208 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1209 eof = fxdr_unsigned(int, *(tl + 1));
1211 nfsm_loadattr(vp, (struct vattr *)0);
1212 nfsm_strsiz(retlen, nmp->nm_rsize);
1213 nfsm_mtouio(uiop, retlen);
1217 if (eof || retlen == 0) {
1220 } else if (retlen < len) {
1232 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, int *must_commit)
1236 int32_t t1, t2, backup;
1237 caddr_t bpos, dpos, cp2;
1238 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1239 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1240 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1241 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1244 if (uiop->uio_iovcnt != 1)
1245 panic("nfs: writerpc iovcnt > 1");
1248 tsiz = uiop->uio_resid;
1249 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1252 nfsstats.rpccnt[NFSPROC_WRITE]++;
1253 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1254 nfsm_reqhead(vp, NFSPROC_WRITE,
1255 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1258 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1259 txdr_hyper(uiop->uio_offset, tl);
1261 *tl++ = txdr_unsigned(len);
1262 *tl++ = txdr_unsigned(*iomode);
1263 *tl = txdr_unsigned(len);
1267 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1268 /* Set both "begin" and "current" to non-garbage. */
1269 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1270 *tl++ = x; /* "begin offset" */
1271 *tl++ = x; /* "current offset" */
1272 x = txdr_unsigned(len);
1273 *tl++ = x; /* total to this offset */
1274 *tl = x; /* size of this write */
1276 nfsm_uiotom(uiop, len);
1277 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1279 wccflag = NFSV3_WCCCHK;
1280 nfsm_wcc_data(vp, wccflag);
1282 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1283 + NFSX_V3WRITEVERF);
1284 rlen = fxdr_unsigned(int, *tl++);
1289 } else if (rlen < len) {
1290 backup = len - rlen;
1291 uiop->uio_iov->iov_base -= backup;
1292 uiop->uio_iov->iov_len += backup;
1293 uiop->uio_offset -= backup;
1294 uiop->uio_resid += backup;
1297 commit = fxdr_unsigned(int, *tl++);
1300 * Return the lowest committment level
1301 * obtained by any of the RPCs.
1303 if (committed == NFSV3WRITE_FILESYNC)
1305 else if (committed == NFSV3WRITE_DATASYNC &&
1306 commit == NFSV3WRITE_UNSTABLE)
1308 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1309 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1311 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1312 } else if (bcmp((caddr_t)tl,
1313 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1315 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1320 nfsm_loadattr(vp, (struct vattr *)0);
1322 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime.tv_sec;
1329 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1330 committed = NFSV3WRITE_FILESYNC;
1331 *iomode = committed;
1333 uiop->uio_resid = tsiz;
1339 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1340 * mode set to specify the file type and the size field for rdev.
1343 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1346 struct nfsv2_sattr *sp;
1350 struct vnode *newvp = (struct vnode *)0;
1351 struct nfsnode *np = (struct nfsnode *)0;
1355 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1356 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1358 int v3 = NFS_ISV3(dvp);
1360 if (vap->va_type == VCHR || vap->va_type == VBLK)
1361 rdev = txdr_unsigned(vap->va_rdev);
1362 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1365 return (EOPNOTSUPP);
1367 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1370 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1371 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1372 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1373 nfsm_fhtom(dvp, v3);
1374 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1376 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1377 *tl++ = vtonfsv3_type(vap->va_type);
1378 nfsm_v3attrbuild(vap, FALSE);
1379 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1380 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1381 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1382 *tl = txdr_unsigned(uminor(vap->va_rdev));
1385 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1386 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1387 sp->sa_uid = nfs_xdrneg1;
1388 sp->sa_gid = nfs_xdrneg1;
1390 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1391 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1393 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1395 nfsm_mtofh(dvp, newvp, v3, gotvp);
1399 newvp = (struct vnode *)0;
1401 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1402 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1408 nfsm_wcc_data(dvp, wccflag);
1415 if (cnp->cn_flags & CNP_MAKEENTRY)
1416 cache_enter(dvp, newvp, cnp);
1419 VTONFS(dvp)->n_flag |= NMODIFIED;
1421 VTONFS(dvp)->n_attrstamp = 0;
1427 * just call nfs_mknodrpc() to do the work.
1429 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1430 * struct componentname *a_cnp, struct vattr *a_vap)
1434 nfs_mknod(struct vop_mknod_args *ap)
1436 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1439 static u_long create_verf;
1441 * nfs file create call
1443 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1444 * struct componentname *a_cnp, struct vattr *a_vap)
1447 nfs_create(struct vop_create_args *ap)
1449 struct vnode *dvp = ap->a_dvp;
1450 struct vattr *vap = ap->a_vap;
1451 struct componentname *cnp = ap->a_cnp;
1452 struct nfsv2_sattr *sp;
1456 struct nfsnode *np = (struct nfsnode *)0;
1457 struct vnode *newvp = (struct vnode *)0;
1458 caddr_t bpos, dpos, cp2;
1459 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1460 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1462 int v3 = NFS_ISV3(dvp);
1465 * Oops, not for me..
1467 if (vap->va_type == VSOCK)
1468 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1470 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1473 if (vap->va_vaflags & VA_EXCLUSIVE)
1476 nfsstats.rpccnt[NFSPROC_CREATE]++;
1477 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1478 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1479 nfsm_fhtom(dvp, v3);
1480 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1482 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1483 if (fmode & O_EXCL) {
1484 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1485 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1487 if (!TAILQ_EMPTY(&in_ifaddrhead))
1488 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr;
1491 *tl++ = create_verf;
1492 *tl = ++create_verf;
1494 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1495 nfsm_v3attrbuild(vap, FALSE);
1498 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1499 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1500 sp->sa_uid = nfs_xdrneg1;
1501 sp->sa_gid = nfs_xdrneg1;
1503 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1504 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1506 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1508 nfsm_mtofh(dvp, newvp, v3, gotvp);
1512 newvp = (struct vnode *)0;
1514 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1515 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1521 nfsm_wcc_data(dvp, wccflag);
1525 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1531 } else if (v3 && (fmode & O_EXCL)) {
1533 * We are normally called with only a partially initialized
1534 * VAP. Since the NFSv3 spec says that server may use the
1535 * file attributes to store the verifier, the spec requires
1536 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1537 * in atime, but we can't really assume that all servers will
1538 * so we ensure that our SETATTR sets both atime and mtime.
1540 if (vap->va_mtime.tv_sec == VNOVAL)
1541 vfs_timestamp(&vap->va_mtime);
1542 if (vap->va_atime.tv_sec == VNOVAL)
1543 vap->va_atime = vap->va_mtime;
1544 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1547 if (cnp->cn_flags & CNP_MAKEENTRY)
1548 cache_enter(dvp, newvp, cnp);
1550 * The new np may have enough info for access
1551 * checks, make sure rucred and wucred are
1552 * initialized for read and write rpc's.
1555 if (np->n_rucred == NULL)
1556 np->n_rucred = crhold(cnp->cn_cred);
1557 if (np->n_wucred == NULL)
1558 np->n_wucred = crhold(cnp->cn_cred);
1561 VTONFS(dvp)->n_flag |= NMODIFIED;
1563 VTONFS(dvp)->n_attrstamp = 0;
1568 * nfs file remove call
1569 * To try and make nfs semantics closer to ufs semantics, a file that has
1570 * other processes using the vnode is renamed instead of removed and then
1571 * removed later on the last close.
1572 * - If v_usecount > 1
1573 * If a rename is not already in the works
1574 * call nfs_sillyrename() to set it up
1578 * nfs_remove(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
1579 * struct vnode *a_vp, struct componentname *a_cnp)
1582 nfs_remove(struct vop_remove_args *ap)
1584 struct vnode *vp = ap->a_vp;
1585 struct vnode *dvp = ap->a_dvp;
1586 struct componentname *cnp = ap->a_cnp;
1587 struct nfsnode *np = VTONFS(vp);
1592 if ((cnp->cn_flags & CNP_HASBUF) == 0)
1593 panic("nfs_remove: no name");
1594 if (vp->v_usecount < 1)
1595 panic("nfs_remove: bad v_usecount");
1597 if (vp->v_type == VDIR)
1599 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1600 VOP_GETATTR(vp, &vattr, cnp->cn_td) == 0 &&
1601 vattr.va_nlink > 1)) {
1603 * Purge the name cache so that the chance of a lookup for
1604 * the name succeeding while the remove is in progress is
1605 * minimized. Without node locking it can still happen, such
1606 * that an I/O op returns ESTALE, but since you get this if
1607 * another host removes the file..
1611 * throw away biocache buffers, mainly to avoid
1612 * unnecessary delayed writes later.
1614 error = nfs_vinvalbuf(vp, 0, cnp->cn_td, 1);
1617 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1618 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1620 * Kludge City: If the first reply to the remove rpc is lost..
1621 * the reply to the retransmitted request will be ENOENT
1622 * since the file was in fact removed
1623 * Therefore, we cheat and return success.
1625 if (error == ENOENT)
1627 } else if (!np->n_sillyrename)
1628 error = nfs_sillyrename(dvp, vp, cnp);
1629 np->n_attrstamp = 0;
1634 * nfs file remove rpc called from nfs_inactive
1637 nfs_removeit(struct sillyrename *sp)
1639 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1644 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1647 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1648 struct ucred *cred, struct thread *td)
1653 caddr_t bpos, dpos, cp2;
1654 int error = 0, wccflag = NFSV3_WCCRATTR;
1655 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1656 int v3 = NFS_ISV3(dvp);
1658 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1659 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1660 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1661 nfsm_fhtom(dvp, v3);
1662 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1663 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1665 nfsm_wcc_data(dvp, wccflag);
1668 VTONFS(dvp)->n_flag |= NMODIFIED;
1670 VTONFS(dvp)->n_attrstamp = 0;
1675 * nfs file rename call
1677 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1678 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1679 * struct vnode *a_tvp, struct componentname *a_tcnp)
1682 nfs_rename(struct vop_rename_args *ap)
1684 struct vnode *fvp = ap->a_fvp;
1685 struct vnode *tvp = ap->a_tvp;
1686 struct vnode *fdvp = ap->a_fdvp;
1687 struct vnode *tdvp = ap->a_tdvp;
1688 struct componentname *tcnp = ap->a_tcnp;
1689 struct componentname *fcnp = ap->a_fcnp;
1693 if ((tcnp->cn_flags & CNP_HASBUF) == 0 ||
1694 (fcnp->cn_flags & CNP_HASBUF) == 0)
1695 panic("nfs_rename: no name");
1697 /* Check for cross-device rename */
1698 if ((fvp->v_mount != tdvp->v_mount) ||
1699 (tvp && (fvp->v_mount != tvp->v_mount))) {
1705 * We have to flush B_DELWRI data prior to renaming
1706 * the file. If we don't, the delayed-write buffers
1707 * can be flushed out later after the file has gone stale
1708 * under NFSV3. NFSV2 does not have this problem because
1709 * ( as far as I can tell ) it flushes dirty buffers more
1713 VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_td);
1715 VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_td);
1718 * If the tvp exists and is in use, sillyrename it before doing the
1719 * rename of the new file over it.
1720 * XXX Can't sillyrename a directory.
1722 * We must purge tvp from the cache (old API) or further accesses
1723 * will see the old version of the file and return ESTALE.
1725 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1726 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1734 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1735 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1740 if (fvp->v_type == VDIR) {
1741 if (tvp != NULL && tvp->v_type == VDIR)
1757 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1759 if (error == ENOENT)
1765 * nfs file rename rpc called from nfs_remove() above
1768 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1769 struct sillyrename *sp)
1771 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1772 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1776 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1779 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1780 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1781 struct ucred *cred, struct thread *td)
1786 caddr_t bpos, dpos, cp2;
1787 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1788 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1789 int v3 = NFS_ISV3(fdvp);
1791 nfsstats.rpccnt[NFSPROC_RENAME]++;
1792 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1793 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1794 nfsm_rndup(tnamelen));
1795 nfsm_fhtom(fdvp, v3);
1796 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1797 nfsm_fhtom(tdvp, v3);
1798 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1799 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1801 nfsm_wcc_data(fdvp, fwccflag);
1802 nfsm_wcc_data(tdvp, twccflag);
1806 VTONFS(fdvp)->n_flag |= NMODIFIED;
1807 VTONFS(tdvp)->n_flag |= NMODIFIED;
1809 VTONFS(fdvp)->n_attrstamp = 0;
1811 VTONFS(tdvp)->n_attrstamp = 0;
1816 * nfs hard link create call
1818 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1819 * struct componentname *a_cnp)
1822 nfs_link(struct vop_link_args *ap)
1824 struct vnode *vp = ap->a_vp;
1825 struct vnode *tdvp = ap->a_tdvp;
1826 struct componentname *cnp = ap->a_cnp;
1830 caddr_t bpos, dpos, cp2;
1831 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1832 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1835 if (vp->v_mount != tdvp->v_mount) {
1840 * Push all writes to the server, so that the attribute cache
1841 * doesn't get "out of sync" with the server.
1842 * XXX There should be a better way!
1844 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_td);
1847 nfsstats.rpccnt[NFSPROC_LINK]++;
1848 nfsm_reqhead(vp, NFSPROC_LINK,
1849 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1851 nfsm_fhtom(tdvp, v3);
1852 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1853 nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1855 nfsm_postop_attr(vp, attrflag);
1856 nfsm_wcc_data(tdvp, wccflag);
1860 VTONFS(tdvp)->n_flag |= NMODIFIED;
1862 VTONFS(vp)->n_attrstamp = 0;
1864 VTONFS(tdvp)->n_attrstamp = 0;
1866 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1868 if (error == EEXIST)
1874 * nfs symbolic link create call
1876 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1877 * struct componentname *a_cnp, struct vattr *a_vap,
1881 nfs_symlink(struct vop_symlink_args *ap)
1883 struct vnode *dvp = ap->a_dvp;
1884 struct vattr *vap = ap->a_vap;
1885 struct componentname *cnp = ap->a_cnp;
1886 struct nfsv2_sattr *sp;
1890 caddr_t bpos, dpos, cp2;
1891 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1892 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1893 struct vnode *newvp = (struct vnode *)0;
1894 int v3 = NFS_ISV3(dvp);
1896 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1897 slen = strlen(ap->a_target);
1898 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1899 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1900 nfsm_fhtom(dvp, v3);
1901 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1903 nfsm_v3attrbuild(vap, FALSE);
1905 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1907 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1908 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1909 sp->sa_uid = nfs_xdrneg1;
1910 sp->sa_gid = nfs_xdrneg1;
1911 sp->sa_size = nfs_xdrneg1;
1912 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1913 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1917 * Issue the NFS request and get the rpc response.
1919 * Only NFSv3 responses returning an error of 0 actually return
1920 * a file handle that can be converted into newvp without having
1921 * to do an extra lookup rpc.
1923 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1926 nfsm_mtofh(dvp, newvp, v3, gotvp);
1927 nfsm_wcc_data(dvp, wccflag);
1931 * out code jumps -> here, mrep is also freed.
1938 * If we get an EEXIST error, silently convert it to no-error
1939 * in case of an NFS retry.
1941 if (error == EEXIST)
1945 * If we do not have (or no longer have) an error, and we could
1946 * not extract the newvp from the response due to the request being
1947 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1948 * to obtain a newvp to return.
1950 if (error == 0 && newvp == NULL) {
1951 struct nfsnode *np = NULL;
1953 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1954 cnp->cn_cred, cnp->cn_td, &np);
1964 VTONFS(dvp)->n_flag |= NMODIFIED;
1966 VTONFS(dvp)->n_attrstamp = 0;
1973 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1974 * struct componentname *a_cnp, struct vattr *a_vap)
1977 nfs_mkdir(struct vop_mkdir_args *ap)
1979 struct vnode *dvp = ap->a_dvp;
1980 struct vattr *vap = ap->a_vap;
1981 struct componentname *cnp = ap->a_cnp;
1982 struct nfsv2_sattr *sp;
1987 struct nfsnode *np = (struct nfsnode *)0;
1988 struct vnode *newvp = (struct vnode *)0;
1989 caddr_t bpos, dpos, cp2;
1990 int error = 0, wccflag = NFSV3_WCCRATTR;
1992 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1994 int v3 = NFS_ISV3(dvp);
1996 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1999 len = cnp->cn_namelen;
2000 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2001 nfsm_reqhead(dvp, NFSPROC_MKDIR,
2002 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2003 nfsm_fhtom(dvp, v3);
2004 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
2006 nfsm_v3attrbuild(vap, FALSE);
2008 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2009 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2010 sp->sa_uid = nfs_xdrneg1;
2011 sp->sa_gid = nfs_xdrneg1;
2012 sp->sa_size = nfs_xdrneg1;
2013 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2014 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2016 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
2018 nfsm_mtofh(dvp, newvp, v3, gotvp);
2020 nfsm_wcc_data(dvp, wccflag);
2023 VTONFS(dvp)->n_flag |= NMODIFIED;
2025 VTONFS(dvp)->n_attrstamp = 0;
2027 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2028 * if we can succeed in looking up the directory.
2030 if (error == EEXIST || (!error && !gotvp)) {
2033 newvp = (struct vnode *)0;
2035 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2039 if (newvp->v_type != VDIR)
2052 * nfs remove directory call
2054 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2055 * struct componentname *a_cnp)
2058 nfs_rmdir(struct vop_rmdir_args *ap)
2060 struct vnode *vp = ap->a_vp;
2061 struct vnode *dvp = ap->a_dvp;
2062 struct componentname *cnp = ap->a_cnp;
2066 caddr_t bpos, dpos, cp2;
2067 int error = 0, wccflag = NFSV3_WCCRATTR;
2068 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2069 int v3 = NFS_ISV3(dvp);
2073 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2074 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2075 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2076 nfsm_fhtom(dvp, v3);
2077 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2078 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2080 nfsm_wcc_data(dvp, wccflag);
2083 VTONFS(dvp)->n_flag |= NMODIFIED;
2085 VTONFS(dvp)->n_attrstamp = 0;
2091 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2093 if (error == ENOENT)
2101 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2104 nfs_readdir(struct vop_readdir_args *ap)
2106 struct vnode *vp = ap->a_vp;
2107 struct nfsnode *np = VTONFS(vp);
2108 struct uio *uio = ap->a_uio;
2112 if (vp->v_type != VDIR)
2115 * First, check for hit on the EOF offset cache
2117 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2118 (np->n_flag & NMODIFIED) == 0) {
2119 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
2120 if (NQNFS_CKCACHABLE(vp, ND_READ)) {
2121 nfsstats.direofcache_hits++;
2124 } else if (VOP_GETATTR(vp, &vattr, uio->uio_td) == 0 &&
2125 np->n_mtime == vattr.va_mtime.tv_sec) {
2126 nfsstats.direofcache_hits++;
2132 * Call nfs_bioread() to do the real work.
2134 tresid = uio->uio_resid;
2135 error = nfs_bioread(vp, uio, 0);
2137 if (!error && uio->uio_resid == tresid)
2138 nfsstats.direofcache_misses++;
2144 * Called from below the buffer cache by nfs_doio().
2147 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2150 struct dirent *dp = NULL;
2155 caddr_t bpos, dpos, cp2;
2156 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2158 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2159 struct nfsnode *dnp = VTONFS(vp);
2161 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2163 int v3 = NFS_ISV3(vp);
2166 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2167 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2168 panic("nfs readdirrpc bad uio");
2172 * If there is no cookie, assume directory was stale.
2174 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2178 return (NFSERR_BAD_COOKIE);
2180 * Loop around doing readdir rpc's of size nm_readdirsize
2181 * truncated to a multiple of DIRBLKSIZ.
2182 * The stopping criteria is EOF or buffer full.
2184 while (more_dirs && bigenough) {
2185 nfsstats.rpccnt[NFSPROC_READDIR]++;
2186 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2190 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2191 *tl++ = cookie.nfsuquad[0];
2192 *tl++ = cookie.nfsuquad[1];
2193 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2194 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2196 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2197 *tl++ = cookie.nfsuquad[0];
2199 *tl = txdr_unsigned(nmp->nm_readdirsize);
2200 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2202 nfsm_postop_attr(vp, attrflag);
2204 nfsm_dissect(tl, u_int32_t *,
2206 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2207 dnp->n_cookieverf.nfsuquad[1] = *tl;
2213 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2214 more_dirs = fxdr_unsigned(int, *tl);
2216 /* loop thru the dir entries, doctoring them to 4bsd form */
2217 while (more_dirs && bigenough) {
2219 nfsm_dissect(tl, u_int32_t *,
2221 fileno = fxdr_hyper(tl);
2222 len = fxdr_unsigned(int, *(tl + 2));
2224 nfsm_dissect(tl, u_int32_t *,
2226 fileno = fxdr_unsigned(u_quad_t, *tl++);
2227 len = fxdr_unsigned(int, *tl);
2229 if (len <= 0 || len > NFS_MAXNAMLEN) {
2234 tlen = nfsm_rndup(len);
2236 tlen += 4; /* To ensure null termination */
2237 left = DIRBLKSIZ - blksiz;
2238 if ((tlen + DIRHDSIZ) > left) {
2239 dp->d_reclen += left;
2240 uiop->uio_iov->iov_base += left;
2241 uiop->uio_iov->iov_len -= left;
2242 uiop->uio_offset += left;
2243 uiop->uio_resid -= left;
2246 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2249 dp = (struct dirent *)uiop->uio_iov->iov_base;
2250 dp->d_fileno = (int)fileno;
2252 dp->d_reclen = tlen + DIRHDSIZ;
2253 dp->d_type = DT_UNKNOWN;
2254 blksiz += dp->d_reclen;
2255 if (blksiz == DIRBLKSIZ)
2257 uiop->uio_offset += DIRHDSIZ;
2258 uiop->uio_resid -= DIRHDSIZ;
2259 uiop->uio_iov->iov_base += DIRHDSIZ;
2260 uiop->uio_iov->iov_len -= DIRHDSIZ;
2261 nfsm_mtouio(uiop, len);
2262 cp = uiop->uio_iov->iov_base;
2264 *cp = '\0'; /* null terminate */
2265 uiop->uio_iov->iov_base += tlen;
2266 uiop->uio_iov->iov_len -= tlen;
2267 uiop->uio_offset += tlen;
2268 uiop->uio_resid -= tlen;
2270 nfsm_adv(nfsm_rndup(len));
2272 nfsm_dissect(tl, u_int32_t *,
2275 nfsm_dissect(tl, u_int32_t *,
2279 cookie.nfsuquad[0] = *tl++;
2281 cookie.nfsuquad[1] = *tl++;
2286 more_dirs = fxdr_unsigned(int, *tl);
2289 * If at end of rpc data, get the eof boolean
2292 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2293 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2298 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2299 * by increasing d_reclen for the last record.
2302 left = DIRBLKSIZ - blksiz;
2303 dp->d_reclen += left;
2304 uiop->uio_iov->iov_base += left;
2305 uiop->uio_iov->iov_len -= left;
2306 uiop->uio_offset += left;
2307 uiop->uio_resid -= left;
2311 * We are now either at the end of the directory or have filled the
2315 dnp->n_direofoffset = uiop->uio_offset;
2317 if (uiop->uio_resid > 0)
2318 printf("EEK! readdirrpc resid > 0\n");
2319 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2327 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2330 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2337 struct vnode *newvp;
2339 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2340 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2341 struct nameidata nami, *ndp = &nami;
2342 struct componentname *cnp = &ndp->ni_cnd;
2344 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2345 struct nfsnode *dnp = VTONFS(vp), *np;
2348 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2349 int attrflag, fhsize;
2352 dp = (struct dirent *)0;
2355 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2356 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2357 panic("nfs readdirplusrpc bad uio");
2363 * If there is no cookie, assume directory was stale.
2365 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2369 return (NFSERR_BAD_COOKIE);
2371 * Loop around doing readdir rpc's of size nm_readdirsize
2372 * truncated to a multiple of DIRBLKSIZ.
2373 * The stopping criteria is EOF or buffer full.
2375 while (more_dirs && bigenough) {
2376 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2377 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2378 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2380 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2381 *tl++ = cookie.nfsuquad[0];
2382 *tl++ = cookie.nfsuquad[1];
2383 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2384 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2385 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2386 *tl = txdr_unsigned(nmp->nm_rsize);
2387 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2388 nfsm_postop_attr(vp, attrflag);
2393 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2394 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2395 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2396 more_dirs = fxdr_unsigned(int, *tl);
2398 /* loop thru the dir entries, doctoring them to 4bsd form */
2399 while (more_dirs && bigenough) {
2400 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2401 fileno = fxdr_hyper(tl);
2402 len = fxdr_unsigned(int, *(tl + 2));
2403 if (len <= 0 || len > NFS_MAXNAMLEN) {
2408 tlen = nfsm_rndup(len);
2410 tlen += 4; /* To ensure null termination*/
2411 left = DIRBLKSIZ - blksiz;
2412 if ((tlen + DIRHDSIZ) > left) {
2413 dp->d_reclen += left;
2414 uiop->uio_iov->iov_base += left;
2415 uiop->uio_iov->iov_len -= left;
2416 uiop->uio_offset += left;
2417 uiop->uio_resid -= left;
2420 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2423 dp = (struct dirent *)uiop->uio_iov->iov_base;
2424 dp->d_fileno = (int)fileno;
2426 dp->d_reclen = tlen + DIRHDSIZ;
2427 dp->d_type = DT_UNKNOWN;
2428 blksiz += dp->d_reclen;
2429 if (blksiz == DIRBLKSIZ)
2431 uiop->uio_offset += DIRHDSIZ;
2432 uiop->uio_resid -= DIRHDSIZ;
2433 uiop->uio_iov->iov_base += DIRHDSIZ;
2434 uiop->uio_iov->iov_len -= DIRHDSIZ;
2435 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2436 cnp->cn_namelen = len;
2437 nfsm_mtouio(uiop, len);
2438 cp = uiop->uio_iov->iov_base;
2441 uiop->uio_iov->iov_base += tlen;
2442 uiop->uio_iov->iov_len -= tlen;
2443 uiop->uio_offset += tlen;
2444 uiop->uio_resid -= tlen;
2446 nfsm_adv(nfsm_rndup(len));
2447 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2449 cookie.nfsuquad[0] = *tl++;
2450 cookie.nfsuquad[1] = *tl++;
2455 * Since the attributes are before the file handle
2456 * (sigh), we must skip over the attributes and then
2457 * come back and get them.
2459 attrflag = fxdr_unsigned(int, *tl);
2463 nfsm_adv(NFSX_V3FATTR);
2464 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2465 doit = fxdr_unsigned(int, *tl);
2467 nfsm_getfh(fhp, fhsize, 1);
2468 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2473 error = nfs_nget(vp->v_mount, fhp,
2481 if (doit && bigenough) {
2486 nfsm_loadattr(newvp, (struct vattr *)0);
2490 IFTODT(VTTOIF(np->n_vattr.va_type));
2492 cache_enter(ndp->ni_dvp, ndp->ni_vp, cnp);
2495 /* Just skip over the file handle */
2496 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2497 i = fxdr_unsigned(int, *tl);
2498 nfsm_adv(nfsm_rndup(i));
2500 if (newvp != NULLVP) {
2507 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2508 more_dirs = fxdr_unsigned(int, *tl);
2511 * If at end of rpc data, get the eof boolean
2514 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2515 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2520 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2521 * by increasing d_reclen for the last record.
2524 left = DIRBLKSIZ - blksiz;
2525 dp->d_reclen += left;
2526 uiop->uio_iov->iov_base += left;
2527 uiop->uio_iov->iov_len -= left;
2528 uiop->uio_offset += left;
2529 uiop->uio_resid -= left;
2533 * We are now either at the end of the directory or have filled the
2537 dnp->n_direofoffset = uiop->uio_offset;
2539 if (uiop->uio_resid > 0)
2540 printf("EEK! readdirplusrpc resid > 0\n");
2541 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2545 if (newvp != NULLVP) {
2556 * Silly rename. To make the NFS filesystem that is stateless look a little
2557 * more like the "ufs" a remove of an active vnode is translated to a rename
2558 * to a funny looking filename that is removed by nfs_inactive on the
2559 * nfsnode. There is the potential for another process on a different client
2560 * to create the same funny name between the nfs_lookitup() fails and the
2561 * nfs_rename() completes, but...
2564 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2566 struct sillyrename *sp;
2571 * We previously purged dvp instead of vp. I don't know why, it
2572 * completely destroys performance. We can't do it anyway with the
2573 * new VFS API since we would be breaking the namecache topology.
2578 if (vp->v_type == VDIR)
2579 panic("nfs: sillyrename dir");
2581 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2582 M_NFSREQ, M_WAITOK);
2583 sp->s_cred = crdup(cnp->cn_cred);
2587 /* Fudge together a funny name */
2588 sp->s_namlen = sprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
2590 /* Try lookitups until we get one that isn't there */
2591 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2592 cnp->cn_td, (struct nfsnode **)0) == 0) {
2594 if (sp->s_name[4] > 'z') {
2599 error = nfs_renameit(dvp, cnp, sp);
2602 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2604 np->n_sillyrename = sp;
2609 free((caddr_t)sp, M_NFSREQ);
2614 * Look up a file name and optionally either update the file handle or
2615 * allocate an nfsnode, depending on the value of npp.
2616 * npp == NULL --> just do the lookup
2617 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2619 * *npp != NULL --> update the file handle in the vnode
2622 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2623 struct thread *td, struct nfsnode **npp)
2628 struct vnode *newvp = (struct vnode *)0;
2629 struct nfsnode *np, *dnp = VTONFS(dvp);
2630 caddr_t bpos, dpos, cp2;
2631 int error = 0, fhlen, attrflag;
2632 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2634 int v3 = NFS_ISV3(dvp);
2636 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2637 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2638 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2639 nfsm_fhtom(dvp, v3);
2640 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2641 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2642 if (npp && !error) {
2643 nfsm_getfh(nfhp, fhlen, v3);
2646 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2647 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2648 np->n_fhp = &np->n_fh;
2649 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2650 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2651 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2652 np->n_fhsize = fhlen;
2654 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2658 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2666 nfsm_postop_attr(newvp, attrflag);
2667 if (!attrflag && *npp == NULL) {
2676 nfsm_loadattr(newvp, (struct vattr *)0);
2680 if (npp && *npp == NULL) {
2695 * Nfs Version 3 commit rpc
2698 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2703 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2704 caddr_t bpos, dpos, cp2;
2705 int error = 0, wccflag = NFSV3_WCCRATTR;
2706 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2708 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2710 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2711 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2713 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2714 txdr_hyper(offset, tl);
2716 *tl = txdr_unsigned(cnt);
2717 nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2718 nfsm_wcc_data(vp, wccflag);
2720 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2721 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2722 NFSX_V3WRITEVERF)) {
2723 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2725 error = NFSERR_STALEWRITEVERF;
2735 * - make nfs_bmap() essentially a no-op that does no translation
2736 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2737 * (Maybe I could use the process's page mapping, but I was concerned that
2738 * Kernel Write might not be enabled and also figured copyout() would do
2739 * a lot more work than bcopy() and also it currently happens in the
2740 * context of the swapper process (2).
2742 * nfs_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
2743 * daddr_t *a_bnp, int *a_runp, int *a_runb)
2746 nfs_bmap(struct vop_bmap_args *ap)
2748 struct vnode *vp = ap->a_vp;
2750 if (ap->a_vpp != NULL)
2752 if (ap->a_bnp != NULL)
2753 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2754 if (ap->a_runp != NULL)
2756 if (ap->a_runb != NULL)
2763 * For async requests when nfsiod(s) are running, queue the request by
2764 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2768 nfs_strategy(struct vop_strategy_args *ap)
2770 struct buf *bp = ap->a_bp;
2774 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2775 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2777 if (bp->b_flags & B_PHYS)
2778 panic("nfs physio");
2780 if (bp->b_flags & B_ASYNC)
2783 td = curthread; /* XXX */
2786 * If the op is asynchronous and an i/o daemon is waiting
2787 * queue the request, wake it up and wait for completion
2788 * otherwise just do it ourselves.
2790 if ((bp->b_flags & B_ASYNC) == 0 ||
2791 nfs_asyncio(bp, td))
2792 error = nfs_doio(bp, td);
2799 * NB Currently unsupported.
2801 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
2802 * struct thread *a_td)
2806 nfs_mmap(struct vop_mmap_args *ap)
2812 * fsync vnode op. Just call nfs_flush() with commit == 1.
2814 * nfs_fsync(struct vnodeop_desc *a_desc, struct vnode *a_vp,
2815 * struct ucred * a_cred, int a_waitfor, struct thread *a_td)
2819 nfs_fsync(struct vop_fsync_args *ap)
2821 return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2825 * Flush all the blocks associated with a vnode.
2826 * Walk through the buffer pool and push any dirty pages
2827 * associated with the vnode.
2830 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
2832 struct nfsnode *np = VTONFS(vp);
2836 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2837 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2839 u_quad_t off, endoff, toff;
2840 struct buf **bvec = NULL;
2841 #ifndef NFS_COMMITBVECSIZ
2842 #define NFS_COMMITBVECSIZ 20
2844 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2845 int bvecsize = 0, bveccount;
2847 if (nmp->nm_flag & NFSMNT_INT)
2852 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2853 * server, but nas not been committed to stable storage on the server
2854 * yet. On the first pass, the byte range is worked out and the commit
2855 * rpc is done. On the second pass, nfs_writebp() is called to do the
2862 if (NFS_ISV3(vp) && commit) {
2865 * Count up how many buffers waiting for a commit.
2868 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2869 nbp = TAILQ_NEXT(bp, b_vnbufs);
2870 if (BUF_REFCNT(bp) == 0 &&
2871 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2872 == (B_DELWRI | B_NEEDCOMMIT))
2876 * Allocate space to remember the list of bufs to commit. It is
2877 * important to use M_NOWAIT here to avoid a race with nfs_write.
2878 * If we can't get memory (for whatever reason), we will end up
2879 * committing the buffers one-by-one in the loop below.
2881 if (bvec != NULL && bvec != bvec_on_stack)
2883 if (bveccount > NFS_COMMITBVECSIZ) {
2884 bvec = (struct buf **)
2885 malloc(bveccount * sizeof(struct buf *),
2888 bvec = bvec_on_stack;
2889 bvecsize = NFS_COMMITBVECSIZ;
2891 bvecsize = bveccount;
2893 bvec = bvec_on_stack;
2894 bvecsize = NFS_COMMITBVECSIZ;
2896 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2897 nbp = TAILQ_NEXT(bp, b_vnbufs);
2898 if (bvecpos >= bvecsize)
2900 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2901 (B_DELWRI | B_NEEDCOMMIT) ||
2902 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
2906 * NOTE: we are not clearing B_DONE here, so we have
2907 * to do it later on in this routine if we intend to
2908 * initiate I/O on the bp.
2910 * Note: to avoid loopback deadlocks, we do not
2911 * assign b_runningbufspace.
2913 bp->b_flags |= B_WRITEINPROG;
2914 vfs_busy_pages(bp, 1);
2917 * bp is protected by being locked, but nbp is not
2918 * and vfs_busy_pages() may sleep. We have to
2921 nbp = TAILQ_NEXT(bp, b_vnbufs);
2924 * A list of these buffers is kept so that the
2925 * second loop knows which buffers have actually
2926 * been committed. This is necessary, since there
2927 * may be a race between the commit rpc and new
2928 * uncommitted writes on the file.
2930 bvec[bvecpos++] = bp;
2931 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2935 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2943 * Commit data on the server, as required. Note that
2944 * nfs_commit will use the vnode's cred for the commit.
2946 retv = nfs_commit(vp, off, (int)(endoff - off), td);
2948 if (retv == NFSERR_STALEWRITEVERF)
2949 nfs_clearcommit(vp->v_mount);
2952 * Now, either mark the blocks I/O done or mark the
2953 * blocks dirty, depending on whether the commit
2956 for (i = 0; i < bvecpos; i++) {
2958 bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG | B_CLUSTEROK);
2961 * Error, leave B_DELWRI intact
2963 vfs_unbusy_pages(bp);
2967 * Success, remove B_DELWRI ( bundirty() ).
2969 * b_dirtyoff/b_dirtyend seem to be NFS
2970 * specific. We should probably move that
2971 * into bundirty(). XXX
2975 bp->b_flags |= B_ASYNC;
2977 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
2978 bp->b_dirtyoff = bp->b_dirtyend = 0;
2986 * Start/do any write(s) that are required.
2990 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2991 nbp = TAILQ_NEXT(bp, b_vnbufs);
2992 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
2993 if (waitfor != MNT_WAIT || passone)
2995 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL,
2996 "nfsfsync", slpflag, slptimeo);
2999 panic("nfs_fsync: inconsistent lock");
3000 if (error == ENOLCK)
3002 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
3006 if (slpflag == PCATCH) {
3012 if ((bp->b_flags & B_DELWRI) == 0)
3013 panic("nfs_fsync: not dirty");
3014 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3019 if (passone || !commit)
3020 bp->b_flags |= B_ASYNC;
3022 bp->b_flags |= B_ASYNC | B_WRITEINPROG;
3024 VOP_BWRITE(bp->b_vp, bp);
3032 if (waitfor == MNT_WAIT) {
3033 while (vp->v_numoutput) {
3034 vp->v_flag |= VBWAIT;
3035 error = tsleep((caddr_t)&vp->v_numoutput,
3036 slpflag, "nfsfsync", slptimeo);
3038 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
3042 if (slpflag == PCATCH) {
3048 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) && commit) {
3052 if (np->n_flag & NWRITEERR) {
3053 error = np->n_error;
3054 np->n_flag &= ~NWRITEERR;
3057 if (bvec != NULL && bvec != bvec_on_stack)
3063 * NFS advisory byte-level locks.
3064 * Currently unsupported.
3066 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3070 nfs_advlock(struct vop_advlock_args *ap)
3072 struct nfsnode *np = VTONFS(ap->a_vp);
3075 * The following kludge is to allow diskless support to work
3076 * until a real NFS lockd is implemented. Basically, just pretend
3077 * that this is a local lock.
3079 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3083 * Print out the contents of an nfsnode.
3085 * nfs_print(struct vnode *a_vp)
3088 nfs_print(struct vop_print_args *ap)
3090 struct vnode *vp = ap->a_vp;
3091 struct nfsnode *np = VTONFS(vp);
3093 printf("tag VT_NFS, fileid %ld fsid 0x%x",
3094 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3095 if (vp->v_type == VFIFO)
3102 * Just call nfs_writebp() with the force argument set to 1.
3104 * NOTE: B_DONE may or may not be set in a_bp on call.
3106 * nfs_bwrite(struct vnode *a_bp)
3109 nfs_bwrite(struct vop_bwrite_args *ap)
3111 return (nfs_writebp(ap->a_bp, 1, curthread));
3115 * This is a clone of vn_bwrite(), except that B_WRITEINPROG isn't set unless
3116 * the force flag is one and it also handles the B_NEEDCOMMIT flag. We set
3117 * B_CACHE if this is a VMIO buffer.
3120 nfs_writebp(struct buf *bp, int force, struct thread *td)
3123 int oldflags = bp->b_flags;
3129 if (BUF_REFCNT(bp) == 0)
3130 panic("bwrite: buffer is not locked???");
3132 if (bp->b_flags & B_INVAL) {
3137 bp->b_flags |= B_CACHE;
3140 * Undirty the bp. We will redirty it later if the I/O fails.
3145 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3147 bp->b_vp->v_numoutput++;
3151 * Note: to avoid loopback deadlocks, we do not
3152 * assign b_runningbufspace.
3154 vfs_busy_pages(bp, 1);
3157 bp->b_flags |= B_WRITEINPROG;
3159 VOP_STRATEGY(bp->b_vp, bp);
3161 if( (oldflags & B_ASYNC) == 0) {
3162 int rtval = biowait(bp);
3164 if (oldflags & B_DELWRI) {
3166 reassignbuf(bp, bp->b_vp);
3178 * nfs special file access vnode op.
3179 * Essentially just get vattr and then imitate iaccess() since the device is
3180 * local to the client.
3182 * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
3183 * struct thread *a_td)
3186 nfsspec_access(struct vop_access_args *ap)
3190 struct ucred *cred = ap->a_cred;
3191 struct vnode *vp = ap->a_vp;
3192 mode_t mode = ap->a_mode;
3198 * Disallow write attempts on filesystems mounted read-only;
3199 * unless the file is a socket, fifo, or a block or character
3200 * device resident on the filesystem.
3202 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3203 switch (vp->v_type) {
3213 * If you're the super-user,
3214 * you always get access.
3216 if (cred->cr_uid == 0)
3219 error = VOP_GETATTR(vp, vap, ap->a_td);
3223 * Access check is based on only one of owner, group, public.
3224 * If not owner, then check group. If not a member of the
3225 * group, then check public access.
3227 if (cred->cr_uid != vap->va_uid) {
3229 gp = cred->cr_groups;
3230 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3231 if (vap->va_gid == *gp)
3237 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3242 * Read wrapper for special devices.
3244 * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3245 * struct ucred *a_cred)
3248 nfsspec_read(struct vop_read_args *ap)
3250 struct nfsnode *np = VTONFS(ap->a_vp);
3256 getnanotime(&np->n_atim);
3257 return (VOCALL(spec_vnode_vops, &ap->a_head));
3261 * Write wrapper for special devices.
3263 * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3264 * struct ucred *a_cred)
3267 nfsspec_write(struct vop_write_args *ap)
3269 struct nfsnode *np = VTONFS(ap->a_vp);
3275 getnanotime(&np->n_mtim);
3276 return (VOCALL(spec_vnode_vops, &ap->a_head));
3280 * Close wrapper for special devices.
3282 * Update the times on the nfsnode then do device close.
3284 * nfsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
3285 * struct thread *a_td)
3288 nfsspec_close(struct vop_close_args *ap)
3290 struct vnode *vp = ap->a_vp;
3291 struct nfsnode *np = VTONFS(vp);
3294 if (np->n_flag & (NACC | NUPD)) {
3296 if (vp->v_usecount == 1 &&
3297 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3299 if (np->n_flag & NACC)
3300 vattr.va_atime = np->n_atim;
3301 if (np->n_flag & NUPD)
3302 vattr.va_mtime = np->n_mtim;
3303 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3306 return (VOCALL(spec_vnode_vops, &ap->a_head));
3310 * Read wrapper for fifos.
3312 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3313 * struct ucred *a_cred)
3316 nfsfifo_read(struct vop_read_args *ap)
3318 struct nfsnode *np = VTONFS(ap->a_vp);
3324 getnanotime(&np->n_atim);
3325 return (VOCALL(fifo_vnode_vops, &ap->a_head));
3329 * Write wrapper for fifos.
3331 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3332 * struct ucred *a_cred)
3335 nfsfifo_write(struct vop_write_args *ap)
3337 struct nfsnode *np = VTONFS(ap->a_vp);
3343 getnanotime(&np->n_mtim);
3344 return (VOCALL(fifo_vnode_vops, &ap->a_head));
3348 * Close wrapper for fifos.
3350 * Update the times on the nfsnode then do fifo close.
3352 * nfsfifo_close(struct vnode *a_vp, int a_fflag, struct thread *a_td)
3355 nfsfifo_close(struct vop_close_args *ap)
3357 struct vnode *vp = ap->a_vp;
3358 struct nfsnode *np = VTONFS(vp);
3362 if (np->n_flag & (NACC | NUPD)) {
3364 if (np->n_flag & NACC)
3366 if (np->n_flag & NUPD)
3369 if (vp->v_usecount == 1 &&
3370 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3372 if (np->n_flag & NACC)
3373 vattr.va_atime = np->n_atim;
3374 if (np->n_flag & NUPD)
3375 vattr.va_mtime = np->n_mtim;
3376 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3379 return (VOCALL(fifo_vnode_vops, &ap->a_head));