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.38 2005/03/17 17:28:46 dillon Exp $
43 * vnode op calls for Sun NFS version 2 and 3
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/resourcevar.h>
53 #include <sys/mount.h>
55 #include <sys/malloc.h>
57 #include <sys/namei.h>
58 #include <sys/nlookup.h>
59 #include <sys/socket.h>
60 #include <sys/vnode.h>
61 #include <sys/dirent.h>
62 #include <sys/fcntl.h>
63 #include <sys/lockf.h>
65 #include <sys/sysctl.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_zone.h>
74 #include <vfs/fifofs/fifo.h>
82 #include "nfsm_subs.h"
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
94 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
95 * calls are not in getblk() and brelse() so that they would not be necessary
99 #define vfs_busy_pages(bp, f)
102 static int nfsspec_read (struct vop_read_args *);
103 static int nfsspec_write (struct vop_write_args *);
104 static int nfsfifo_read (struct vop_read_args *);
105 static int nfsfifo_write (struct vop_write_args *);
106 static int nfsspec_close (struct vop_close_args *);
107 static int nfsfifo_close (struct vop_close_args *);
108 #define nfs_poll vop_nopoll
109 static int nfs_setattrrpc (struct vnode *,struct vattr *,struct ucred *,struct thread *);
110 static int nfs_lookup (struct vop_lookup_args *);
111 static int nfs_create (struct vop_create_args *);
112 static int nfs_mknod (struct vop_mknod_args *);
113 static int nfs_open (struct vop_open_args *);
114 static int nfs_close (struct vop_close_args *);
115 static int nfs_access (struct vop_access_args *);
116 static int nfs_getattr (struct vop_getattr_args *);
117 static int nfs_setattr (struct vop_setattr_args *);
118 static int nfs_read (struct vop_read_args *);
119 static int nfs_mmap (struct vop_mmap_args *);
120 static int nfs_fsync (struct vop_fsync_args *);
121 static int nfs_remove (struct vop_remove_args *);
122 static int nfs_link (struct vop_link_args *);
123 static int nfs_rename (struct vop_rename_args *);
124 static int nfs_mkdir (struct vop_mkdir_args *);
125 static int nfs_rmdir (struct vop_rmdir_args *);
126 static int nfs_symlink (struct vop_symlink_args *);
127 static int nfs_readdir (struct vop_readdir_args *);
128 static int nfs_bmap (struct vop_bmap_args *);
129 static int nfs_strategy (struct vop_strategy_args *);
130 static int nfs_lookitup (struct vnode *, const char *, int,
131 struct ucred *, struct thread *, struct nfsnode **);
132 static int nfs_sillyrename (struct vnode *,struct vnode *,struct componentname *);
133 static int nfsspec_access (struct vop_access_args *);
134 static int nfs_readlink (struct vop_readlink_args *);
135 static int nfs_print (struct vop_print_args *);
136 static int nfs_advlock (struct vop_advlock_args *);
137 static int nfs_bwrite (struct vop_bwrite_args *);
139 static int nfs_nresolve (struct vop_nresolve_args *);
141 * Global vfs data structures for nfs
143 struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
144 { &vop_default_desc, vop_defaultop },
145 { &vop_access_desc, (vnodeopv_entry_t) nfs_access },
146 { &vop_advlock_desc, (vnodeopv_entry_t) nfs_advlock },
147 { &vop_bmap_desc, (vnodeopv_entry_t) nfs_bmap },
148 { &vop_bwrite_desc, (vnodeopv_entry_t) nfs_bwrite },
149 { &vop_close_desc, (vnodeopv_entry_t) nfs_close },
150 { &vop_create_desc, (vnodeopv_entry_t) nfs_create },
151 { &vop_fsync_desc, (vnodeopv_entry_t) nfs_fsync },
152 { &vop_getattr_desc, (vnodeopv_entry_t) nfs_getattr },
153 { &vop_getpages_desc, (vnodeopv_entry_t) nfs_getpages },
154 { &vop_putpages_desc, (vnodeopv_entry_t) nfs_putpages },
155 { &vop_inactive_desc, (vnodeopv_entry_t) nfs_inactive },
156 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
157 { &vop_lease_desc, vop_null },
158 { &vop_link_desc, (vnodeopv_entry_t) nfs_link },
159 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
160 { &vop_lookup_desc, (vnodeopv_entry_t) nfs_lookup },
161 { &vop_mkdir_desc, (vnodeopv_entry_t) nfs_mkdir },
162 { &vop_mknod_desc, (vnodeopv_entry_t) nfs_mknod },
163 { &vop_mmap_desc, (vnodeopv_entry_t) nfs_mmap },
164 { &vop_open_desc, (vnodeopv_entry_t) nfs_open },
165 { &vop_poll_desc, (vnodeopv_entry_t) nfs_poll },
166 { &vop_print_desc, (vnodeopv_entry_t) nfs_print },
167 { &vop_read_desc, (vnodeopv_entry_t) nfs_read },
168 { &vop_readdir_desc, (vnodeopv_entry_t) nfs_readdir },
169 { &vop_readlink_desc, (vnodeopv_entry_t) nfs_readlink },
170 { &vop_reclaim_desc, (vnodeopv_entry_t) nfs_reclaim },
171 { &vop_remove_desc, (vnodeopv_entry_t) nfs_remove },
172 { &vop_rename_desc, (vnodeopv_entry_t) nfs_rename },
173 { &vop_rmdir_desc, (vnodeopv_entry_t) nfs_rmdir },
174 { &vop_setattr_desc, (vnodeopv_entry_t) nfs_setattr },
175 { &vop_strategy_desc, (vnodeopv_entry_t) nfs_strategy },
176 { &vop_symlink_desc, (vnodeopv_entry_t) nfs_symlink },
177 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
178 { &vop_write_desc, (vnodeopv_entry_t) nfs_write },
180 { &vop_nresolve_desc, (vnodeopv_entry_t) nfs_nresolve },
185 * Special device vnode ops
187 struct vnodeopv_entry_desc nfsv2_specop_entries[] = {
188 { &vop_default_desc, (vnodeopv_entry_t) spec_vnoperate },
189 { &vop_access_desc, (vnodeopv_entry_t) nfsspec_access },
190 { &vop_close_desc, (vnodeopv_entry_t) nfsspec_close },
191 { &vop_fsync_desc, (vnodeopv_entry_t) nfs_fsync },
192 { &vop_getattr_desc, (vnodeopv_entry_t) nfs_getattr },
193 { &vop_inactive_desc, (vnodeopv_entry_t) nfs_inactive },
194 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
195 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
196 { &vop_print_desc, (vnodeopv_entry_t) nfs_print },
197 { &vop_read_desc, (vnodeopv_entry_t) nfsspec_read },
198 { &vop_reclaim_desc, (vnodeopv_entry_t) nfs_reclaim },
199 { &vop_setattr_desc, (vnodeopv_entry_t) nfs_setattr },
200 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
201 { &vop_write_desc, (vnodeopv_entry_t) nfsspec_write },
205 struct vnodeopv_entry_desc nfsv2_fifoop_entries[] = {
206 { &vop_default_desc, (vnodeopv_entry_t) fifo_vnoperate },
207 { &vop_access_desc, (vnodeopv_entry_t) nfsspec_access },
208 { &vop_close_desc, (vnodeopv_entry_t) nfsfifo_close },
209 { &vop_fsync_desc, (vnodeopv_entry_t) nfs_fsync },
210 { &vop_getattr_desc, (vnodeopv_entry_t) nfs_getattr },
211 { &vop_inactive_desc, (vnodeopv_entry_t) nfs_inactive },
212 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
213 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
214 { &vop_print_desc, (vnodeopv_entry_t) nfs_print },
215 { &vop_read_desc, (vnodeopv_entry_t) nfsfifo_read },
216 { &vop_reclaim_desc, (vnodeopv_entry_t) nfs_reclaim },
217 { &vop_setattr_desc, (vnodeopv_entry_t) nfs_setattr },
218 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
219 { &vop_write_desc, (vnodeopv_entry_t) nfsfifo_write },
223 static int nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
224 struct componentname *cnp,
226 static int nfs_removerpc (struct vnode *dvp, const char *name,
228 struct ucred *cred, struct thread *td);
229 static int nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
230 int fnamelen, struct vnode *tdvp,
231 const char *tnameptr, int tnamelen,
232 struct ucred *cred, struct thread *td);
233 static int nfs_renameit (struct vnode *sdvp,
234 struct componentname *scnp,
235 struct sillyrename *sp);
240 extern u_int32_t nfs_true, nfs_false;
241 extern u_int32_t nfs_xdrneg1;
242 extern struct nfsstats nfsstats;
243 extern nfstype nfsv3_type[9];
244 struct thread *nfs_iodwant[NFS_MAXASYNCDAEMON];
245 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
246 int nfs_numasync = 0;
247 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
249 SYSCTL_DECL(_vfs_nfs);
251 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
252 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
253 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
255 static int nfsneg_cache_timeout = NFS_MINATTRTIMO;
256 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
257 &nfsneg_cache_timeout, 0, "NFS NEGATIVE ACCESS cache timeout");
259 static int nfsv3_commit_on_close = 0;
260 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
261 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
263 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
264 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
266 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
267 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
270 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
271 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
272 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
274 nfs3_access_otw(struct vnode *vp, int wmode,
275 struct thread *td, struct ucred *cred)
279 int error = 0, attrflag;
281 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
282 caddr_t bpos, dpos, cp2;
286 struct nfsnode *np = VTONFS(vp);
288 nfsstats.rpccnt[NFSPROC_ACCESS]++;
289 nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
291 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
292 *tl = txdr_unsigned(wmode);
293 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
294 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
296 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
297 rmode = fxdr_unsigned(u_int32_t, *tl);
299 np->n_modeuid = cred->cr_uid;
300 np->n_modestamp = mycpu->gd_time_seconds;
308 * nfs access vnode op.
309 * For nfs version 2, just return ok. File accesses may fail later.
310 * For nfs version 3, use the access rpc to check accessibility. If file modes
311 * are changed on the server, accesses might still fail later.
313 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
314 * struct thread *a_td)
317 nfs_access(struct vop_access_args *ap)
319 struct vnode *vp = ap->a_vp;
321 u_int32_t mode, wmode;
322 int v3 = NFS_ISV3(vp);
323 struct nfsnode *np = VTONFS(vp);
326 * Disallow write attempts on filesystems mounted read-only;
327 * unless the file is a socket, fifo, or a block or character
328 * device resident on the filesystem.
330 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
331 switch (vp->v_type) {
341 * For nfs v3, check to see if we have done this recently, and if
342 * so return our cached result instead of making an ACCESS call.
343 * If not, do an access rpc, otherwise you are stuck emulating
344 * ufs_access() locally using the vattr. This may not be correct,
345 * since the server may apply other access criteria such as
346 * client uid-->server uid mapping that we do not know about.
349 if (ap->a_mode & VREAD)
350 mode = NFSV3ACCESS_READ;
353 if (vp->v_type != VDIR) {
354 if (ap->a_mode & VWRITE)
355 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
356 if (ap->a_mode & VEXEC)
357 mode |= NFSV3ACCESS_EXECUTE;
359 if (ap->a_mode & VWRITE)
360 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
362 if (ap->a_mode & VEXEC)
363 mode |= NFSV3ACCESS_LOOKUP;
365 /* XXX safety belt, only make blanket request if caching */
366 if (nfsaccess_cache_timeout > 0) {
367 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
368 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
369 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
375 * Does our cached result allow us to give a definite yes to
378 if (np->n_modestamp &&
379 (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
380 (ap->a_cred->cr_uid == np->n_modeuid) &&
381 ((np->n_mode & mode) == mode)) {
382 nfsstats.accesscache_hits++;
385 * Either a no, or a don't know. Go to the wire.
387 nfsstats.accesscache_misses++;
388 error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred);
390 if ((np->n_mode & mode) != mode) {
396 if ((error = nfsspec_access(ap)) != 0)
400 * Attempt to prevent a mapped root from accessing a file
401 * which it shouldn't. We try to read a byte from the file
402 * if the user is root and the file is not zero length.
403 * After calling nfsspec_access, we should have the correct
406 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
407 && VTONFS(vp)->n_size > 0) {
414 auio.uio_iov = &aiov;
418 auio.uio_segflg = UIO_SYSSPACE;
419 auio.uio_rw = UIO_READ;
420 auio.uio_td = ap->a_td;
422 if (vp->v_type == VREG) {
423 error = nfs_readrpc(vp, &auio);
424 } else if (vp->v_type == VDIR) {
426 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
428 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
429 error = nfs_readdirrpc(vp, &auio);
431 } else if (vp->v_type == VLNK) {
432 error = nfs_readlinkrpc(vp, &auio);
439 * [re]record creds for reading and/or writing if access
440 * was granted. Assume the NFS server will grant read access
441 * for execute requests.
444 if ((ap->a_mode & (VREAD|VEXEC)) && ap->a_cred != np->n_rucred) {
447 crfree(np->n_rucred);
448 np->n_rucred = ap->a_cred;
450 if ((ap->a_mode & VWRITE) && ap->a_cred != np->n_wucred) {
453 crfree(np->n_wucred);
454 np->n_wucred = ap->a_cred;
462 * Check to see if the type is ok
463 * and that deletion is not in progress.
464 * For paged in text files, you will need to flush the page cache
465 * if consistency is lost.
467 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
468 * struct thread *a_td)
472 nfs_open(struct vop_open_args *ap)
474 struct vnode *vp = ap->a_vp;
475 struct nfsnode *np = VTONFS(vp);
476 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
480 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
482 printf("open eacces vtyp=%d\n",vp->v_type);
488 * Clear the attribute cache only if opening with write access. It
489 * is unclear if we should do this at all here, but we certainly
490 * should not clear the cache unconditionally simply because a file
493 if (ap->a_mode & FWRITE)
496 if (nmp->nm_flag & NFSMNT_NQNFS) {
498 * If NQNFS is active, get a valid lease
500 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
502 error = nqnfs_getlease(vp, ND_READ, ap->a_td);
503 } while (error == NQNFS_EXPIRED);
506 if (np->n_lrev != np->n_brev ||
507 (np->n_flag & NQNFSNONCACHE)) {
508 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1))
512 np->n_brev = np->n_lrev;
517 * For normal NFS, reconcile changes made locally verses
518 * changes made remotely. Note that VOP_GETATTR only goes
519 * to the wire if the cached attribute has timed out or been
522 * If local modifications have been made clear the attribute
523 * cache to force an attribute and modified time check. If
524 * GETATTR detects that the file has been changed by someone
525 * other then us it will set NRMODIFIED.
527 * If we are opening a directory and local changes have been
528 * made we have to invalidate the cache in order to ensure
529 * that we get the most up-to-date information from the
532 if (np->n_flag & NLMODIFIED) {
534 if (vp->v_type == VDIR) {
535 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
541 error = VOP_GETATTR(vp, &vattr, ap->a_td);
544 if (np->n_flag & NRMODIFIED) {
545 if (vp->v_type == VDIR)
547 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
550 np->n_flag &= ~NRMODIFIED;
559 * What an NFS client should do upon close after writing is a debatable issue.
560 * Most NFS clients push delayed writes to the server upon close, basically for
562 * 1 - So that any write errors may be reported back to the client process
563 * doing the close system call. By far the two most likely errors are
564 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
565 * 2 - To put a worst case upper bound on cache inconsistency between
566 * multiple clients for the file.
567 * There is also a consistency problem for Version 2 of the protocol w.r.t.
568 * not being able to tell if other clients are writing a file concurrently,
569 * since there is no way of knowing if the changed modify time in the reply
570 * is only due to the write for this client.
571 * (NFS Version 3 provides weak cache consistency data in the reply that
572 * should be sufficient to detect and handle this case.)
574 * The current code does the following:
575 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
576 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
577 * or commit them (this satisfies 1 and 2 except for the
578 * case where the server crashes after this close but
579 * before the commit RPC, which is felt to be "good
580 * enough". Changing the last argument to nfs_flush() to
581 * a 1 would force a commit operation, if it is felt a
582 * commit is necessary now.
583 * for NQNFS - do nothing now, since 2 is dealt with via leases and
584 * 1 should be dealt with via an fsync() system call for
585 * cases where write errors are important.
587 * nfs_close(struct vnodeop_desc *a_desc, struct vnode *a_vp, int a_fflag,
588 * struct ucred *a_cred, struct thread *a_td)
592 nfs_close(struct vop_close_args *ap)
594 struct vnode *vp = ap->a_vp;
595 struct nfsnode *np = VTONFS(vp);
598 if (vp->v_type == VREG) {
599 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 &&
600 (np->n_flag & NLMODIFIED)) {
603 * Under NFSv3 we have dirty buffers to dispose of. We
604 * must flush them to the NFS server. We have the option
605 * of waiting all the way through the commit rpc or just
606 * waiting for the initial write. The default is to only
607 * wait through the initial write so the data is in the
608 * server's cache, which is roughly similar to the state
609 * a standard disk subsystem leaves the file in on close().
611 * We cannot clear the NLMODIFIED bit in np->n_flag due to
612 * potential races with other processes, and certainly
613 * cannot clear it if we don't commit.
615 int cm = nfsv3_commit_on_close ? 1 : 0;
616 error = nfs_flush(vp, MNT_WAIT, ap->a_td, cm);
617 /* np->n_flag &= ~NLMODIFIED; */
619 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
623 if (np->n_flag & NWRITEERR) {
624 np->n_flag &= ~NWRITEERR;
632 * nfs getattr call from vfs.
634 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred,
635 * struct thread *a_td)
638 nfs_getattr(struct vop_getattr_args *ap)
640 struct vnode *vp = ap->a_vp;
641 struct nfsnode *np = VTONFS(vp);
647 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
648 int v3 = NFS_ISV3(vp);
651 * Update local times for special files.
653 if (np->n_flag & (NACC | NUPD))
656 * First look in the cache.
658 if (nfs_getattrcache(vp, ap->a_vap) == 0)
661 if (v3 && nfsaccess_cache_timeout > 0) {
662 nfsstats.accesscache_misses++;
663 nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_td, nfs_vpcred(vp, ND_CHECK));
664 if (nfs_getattrcache(vp, ap->a_vap) == 0)
668 nfsstats.rpccnt[NFSPROC_GETATTR]++;
669 nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
671 nfsm_request(vp, NFSPROC_GETATTR, ap->a_td, nfs_vpcred(vp, ND_CHECK));
673 nfsm_loadattr(vp, ap->a_vap);
683 * nfs_setattr(struct vnodeop_desc *a_desc, struct vnode *a_vp,
684 * struct vattr *a_vap, struct ucred *a_cred,
685 * struct thread *a_td)
688 nfs_setattr(struct vop_setattr_args *ap)
690 struct vnode *vp = ap->a_vp;
691 struct nfsnode *np = VTONFS(vp);
692 struct vattr *vap = ap->a_vap;
701 * Setting of flags is not supported.
703 if (vap->va_flags != VNOVAL)
707 * Disallow write attempts if the filesystem is mounted read-only.
709 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
710 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
711 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
712 (vp->v_mount->mnt_flag & MNT_RDONLY))
714 if (vap->va_size != VNOVAL) {
715 switch (vp->v_type) {
722 if (vap->va_mtime.tv_sec == VNOVAL &&
723 vap->va_atime.tv_sec == VNOVAL &&
724 vap->va_mode == (mode_t)VNOVAL &&
725 vap->va_uid == (uid_t)VNOVAL &&
726 vap->va_gid == (gid_t)VNOVAL)
728 vap->va_size = VNOVAL;
732 * Disallow write attempts if the filesystem is
735 if (vp->v_mount->mnt_flag & MNT_RDONLY)
739 * We run vnode_pager_setsize() early (why?),
740 * we must set np->n_size now to avoid vinvalbuf
741 * V_SAVE races that might setsize a lower
746 error = nfs_meta_setsize(vp, ap->a_td, vap->va_size);
748 if (np->n_flag & NLMODIFIED) {
749 if (vap->va_size == 0)
750 error = nfs_vinvalbuf(vp, 0, ap->a_td, 1);
752 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
755 vnode_pager_setsize(vp, np->n_size);
760 * np->n_size has already been set to vap->va_size
761 * in nfs_meta_setsize(). We must set it again since
762 * nfs_loadattrcache() could be called through
763 * nfs_meta_setsize() and could modify np->n_size.
765 * (note that nfs_loadattrcache() will have called
766 * vnode_pager_setsize() for us in that case).
768 np->n_vattr.va_size = np->n_size = vap->va_size;
771 } else if ((vap->va_mtime.tv_sec != VNOVAL ||
772 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NLMODIFIED) &&
773 vp->v_type == VREG &&
774 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) == EINTR)
776 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
777 if (error && vap->va_size != VNOVAL) {
778 np->n_size = np->n_vattr.va_size = tsize;
779 vnode_pager_setsize(vp, np->n_size);
785 * Do an nfs setattr rpc.
788 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
789 struct ucred *cred, struct thread *td)
791 struct nfsv2_sattr *sp;
792 struct nfsnode *np = VTONFS(vp);
795 caddr_t bpos, dpos, cp2;
797 int error = 0, wccflag = NFSV3_WCCRATTR;
798 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
799 int v3 = NFS_ISV3(vp);
801 nfsstats.rpccnt[NFSPROC_SETATTR]++;
802 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
805 nfsm_v3attrbuild(vap, TRUE);
806 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
809 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
810 if (vap->va_mode == (mode_t)VNOVAL)
811 sp->sa_mode = nfs_xdrneg1;
813 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
814 if (vap->va_uid == (uid_t)VNOVAL)
815 sp->sa_uid = nfs_xdrneg1;
817 sp->sa_uid = txdr_unsigned(vap->va_uid);
818 if (vap->va_gid == (gid_t)VNOVAL)
819 sp->sa_gid = nfs_xdrneg1;
821 sp->sa_gid = txdr_unsigned(vap->va_gid);
822 sp->sa_size = txdr_unsigned(vap->va_size);
823 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
824 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
826 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
829 nfsm_wcc_data(vp, wccflag);
831 nfsm_loadattr(vp, (struct vattr *)0);
838 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
839 * nfs_lookup() until all remaining new api calls are implemented.
841 * Resolve a namecache entry. This function is passed a locked ncp and
842 * must call cache_setvp() on it as appropriate to resolve the entry.
845 nfs_nresolve(struct vop_nresolve_args *ap)
847 struct thread *td = curthread;
848 struct namecache *ncp;
859 /******NFSM MACROS********/
860 struct mbuf *mb, *mrep, *mreq, *mb2, *md;
861 caddr_t bpos, dpos, cp, cp2;
868 KKASSERT(ncp->nc_parent && ncp->nc_parent->nc_vp);
869 dvp = ncp->nc_parent->nc_vp;
870 if ((error = vget(dvp, LK_SHARED, td)) != 0)
875 nfsstats.lookupcache_misses++;
876 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
878 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
879 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
881 nfsm_strtom(ncp->nc_name, len, NFS_MAXNAMLEN);
882 nfsm_request(dvp, NFSPROC_LOOKUP, td, ap->a_cred);
885 * Cache negatve lookups to reduce NFS traffic, but use
886 * a fast timeout. Otherwise use a timeout of 1 tick.
887 * XXX we should add a namecache flag for no-caching
888 * to uncache the negative hit as soon as possible, but
889 * we cannot simply destroy the entry because it is used
890 * as a placeholder by the caller.
892 if (error == ENOENT) {
895 if (nfsneg_cache_timeout)
896 nticks = nfsneg_cache_timeout * hz;
899 cache_setvp(ncp, NULL);
900 cache_settimeout(ncp, nticks);
902 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
908 * Success, get the file handle, do various checks, and load
909 * post-operation data from the reply packet. Theoretically
910 * we should never be looking up "." so, theoretically, we
911 * should never get the same file handle as our directory. But
912 * we check anyway. XXX
914 * Note that no timeout is set for the positive cache hit. We
915 * assume, theoretically, that ESTALE returns will be dealt with
916 * properly to handle NFS races and in anycase we cannot depend
917 * on a timeout to deal with NFS open/create/excl issues so instead
918 * of a bad hack here the rest of the NFS client code needs to do
921 nfsm_getfh(fhp, fhsize, v3);
924 if (NFS_CMPFH(np, fhp, fhsize)) {
928 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
937 nfsm_postop_attr(nvp, attrflag, NFS_LATTR_NOSHRINK);
938 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
940 nfsm_loadattr(nvp, NULL);
942 cache_setvp(ncp, nvp);
956 * 'cached' nfs directory lookup
958 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
960 * nfs_lookup(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
961 * struct vnode **a_vpp, struct componentname *a_cnp)
964 nfs_lookup(struct vop_lookup_args *ap)
966 struct componentname *cnp = ap->a_cnp;
967 struct vnode *dvp = ap->a_dvp;
968 struct vnode **vpp = ap->a_vpp;
969 int flags = cnp->cn_flags;
974 struct nfsmount *nmp;
975 caddr_t bpos, dpos, cp2;
976 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
980 int lockparent, wantparent, error = 0, attrflag, fhsize;
981 int v3 = NFS_ISV3(dvp);
982 struct thread *td = cnp->cn_td;
985 * Read-only mount check and directory check.
988 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
989 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
992 if (dvp->v_type != VDIR)
996 * Look it up in the cache. Note that ENOENT is only returned if we
997 * previously entered a negative hit (see later on). The additional
998 * nfsneg_cache_timeout check causes previously cached results to
999 * be instantly ignored if the negative caching is turned off.
1001 lockparent = flags & CNP_LOCKPARENT;
1002 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1003 nmp = VFSTONFS(dvp->v_mount);
1011 nfsstats.lookupcache_misses++;
1012 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1013 len = cnp->cn_namelen;
1014 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
1015 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1016 nfsm_fhtom(dvp, v3);
1017 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1018 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
1020 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1024 nfsm_getfh(fhp, fhsize, v3);
1027 * Handle RENAME case...
1029 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1030 if (NFS_CMPFH(np, fhp, fhsize)) {
1034 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1041 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1042 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1044 nfsm_loadattr(newvp, (struct vattr *)0);
1048 VOP_UNLOCK(dvp, 0, td);
1049 cnp->cn_flags |= CNP_PDIRUNLOCK;
1054 if (flags & CNP_ISDOTDOT) {
1055 VOP_UNLOCK(dvp, 0, td);
1056 cnp->cn_flags |= CNP_PDIRUNLOCK;
1057 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1059 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
1060 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1061 return (error); /* NOTE: return error from nget */
1065 error = vn_lock(dvp, LK_EXCLUSIVE, td);
1070 cnp->cn_flags |= CNP_PDIRUNLOCK;
1072 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1076 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1082 VOP_UNLOCK(dvp, 0, td);
1083 cnp->cn_flags |= CNP_PDIRUNLOCK;
1088 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1089 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1091 nfsm_loadattr(newvp, (struct vattr *)0);
1093 /* XXX MOVE TO nfs_nremove() */
1094 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1095 cnp->cn_nameiop != NAMEI_DELETE) {
1096 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1103 if (newvp != NULLVP) {
1107 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1108 cnp->cn_nameiop == NAMEI_RENAME) &&
1111 VOP_UNLOCK(dvp, 0, td);
1112 cnp->cn_flags |= CNP_PDIRUNLOCK;
1114 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1117 error = EJUSTRETURN;
1125 * Just call nfs_bioread() to do the work.
1127 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1128 * struct ucred *a_cred)
1131 nfs_read(struct vop_read_args *ap)
1133 struct vnode *vp = ap->a_vp;
1135 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1136 switch (vp->v_type) {
1138 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1149 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1152 nfs_readlink(struct vop_readlink_args *ap)
1154 struct vnode *vp = ap->a_vp;
1156 if (vp->v_type != VLNK)
1158 return (nfs_bioread(vp, ap->a_uio, 0));
1162 * Do a readlink rpc.
1163 * Called by nfs_doio() from below the buffer cache.
1166 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1171 caddr_t bpos, dpos, cp2;
1172 int error = 0, len, attrflag;
1173 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1174 int v3 = NFS_ISV3(vp);
1176 nfsstats.rpccnt[NFSPROC_READLINK]++;
1177 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1179 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1181 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1183 nfsm_strsiz(len, NFS_MAXPATHLEN);
1184 if (len == NFS_MAXPATHLEN) {
1185 struct nfsnode *np = VTONFS(vp);
1186 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1189 nfsm_mtouio(uiop, len);
1201 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1206 caddr_t bpos, dpos, cp2;
1207 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1208 struct nfsmount *nmp;
1209 int error = 0, len, retlen, tsiz, eof, attrflag;
1210 int v3 = NFS_ISV3(vp);
1215 nmp = VFSTONFS(vp->v_mount);
1216 tsiz = uiop->uio_resid;
1217 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1220 nfsstats.rpccnt[NFSPROC_READ]++;
1221 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1222 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1224 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1226 txdr_hyper(uiop->uio_offset, tl);
1227 *(tl + 2) = txdr_unsigned(len);
1229 *tl++ = txdr_unsigned(uiop->uio_offset);
1230 *tl++ = txdr_unsigned(len);
1233 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1235 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1240 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1241 eof = fxdr_unsigned(int, *(tl + 1));
1243 nfsm_loadattr(vp, (struct vattr *)0);
1244 nfsm_strsiz(retlen, nmp->nm_rsize);
1245 nfsm_mtouio(uiop, retlen);
1249 if (eof || retlen == 0) {
1252 } else if (retlen < len) {
1264 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, int *must_commit)
1268 int32_t t1, t2, backup;
1269 caddr_t bpos, dpos, cp2;
1270 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1271 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1272 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1273 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1276 if (uiop->uio_iovcnt != 1)
1277 panic("nfs: writerpc iovcnt > 1");
1280 tsiz = uiop->uio_resid;
1281 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1284 nfsstats.rpccnt[NFSPROC_WRITE]++;
1285 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1286 nfsm_reqhead(vp, NFSPROC_WRITE,
1287 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1290 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1291 txdr_hyper(uiop->uio_offset, tl);
1293 *tl++ = txdr_unsigned(len);
1294 *tl++ = txdr_unsigned(*iomode);
1295 *tl = txdr_unsigned(len);
1299 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1300 /* Set both "begin" and "current" to non-garbage. */
1301 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1302 *tl++ = x; /* "begin offset" */
1303 *tl++ = x; /* "current offset" */
1304 x = txdr_unsigned(len);
1305 *tl++ = x; /* total to this offset */
1306 *tl = x; /* size of this write */
1308 nfsm_uiotom(uiop, len);
1309 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1312 * The write RPC returns a before and after mtime. The
1313 * nfsm_wcc_data() macro checks the before n_mtime
1314 * against the before time and stores the after time
1315 * in the nfsnode's cached vattr and n_mtime field.
1316 * The NRMODIFIED bit will be set if the before
1317 * time did not match the original mtime.
1319 wccflag = NFSV3_WCCCHK;
1320 nfsm_wcc_data(vp, wccflag);
1322 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1323 + NFSX_V3WRITEVERF);
1324 rlen = fxdr_unsigned(int, *tl++);
1329 } else if (rlen < len) {
1330 backup = len - rlen;
1331 uiop->uio_iov->iov_base -= backup;
1332 uiop->uio_iov->iov_len += backup;
1333 uiop->uio_offset -= backup;
1334 uiop->uio_resid += backup;
1337 commit = fxdr_unsigned(int, *tl++);
1340 * Return the lowest committment level
1341 * obtained by any of the RPCs.
1343 if (committed == NFSV3WRITE_FILESYNC)
1345 else if (committed == NFSV3WRITE_DATASYNC &&
1346 commit == NFSV3WRITE_UNSTABLE)
1348 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1349 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1351 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1352 } else if (bcmp((caddr_t)tl,
1353 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1355 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1360 nfsm_loadattr(vp, (struct vattr *)0);
1368 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1369 committed = NFSV3WRITE_FILESYNC;
1370 *iomode = committed;
1372 uiop->uio_resid = tsiz;
1378 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1379 * mode set to specify the file type and the size field for rdev.
1382 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1385 struct nfsv2_sattr *sp;
1389 struct vnode *newvp = (struct vnode *)0;
1390 struct nfsnode *np = (struct nfsnode *)0;
1394 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1395 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1397 int v3 = NFS_ISV3(dvp);
1399 if (vap->va_type == VCHR || vap->va_type == VBLK)
1400 rdev = txdr_unsigned(vap->va_rdev);
1401 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1404 return (EOPNOTSUPP);
1406 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1409 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1410 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1411 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1412 nfsm_fhtom(dvp, v3);
1413 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1415 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1416 *tl++ = vtonfsv3_type(vap->va_type);
1417 nfsm_v3attrbuild(vap, FALSE);
1418 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1419 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1420 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1421 *tl = txdr_unsigned(uminor(vap->va_rdev));
1424 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1425 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1426 sp->sa_uid = nfs_xdrneg1;
1427 sp->sa_gid = nfs_xdrneg1;
1429 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1430 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1432 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1434 nfsm_mtofh(dvp, newvp, v3, gotvp);
1438 newvp = (struct vnode *)0;
1440 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1441 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1447 nfsm_wcc_data(dvp, wccflag);
1456 VTONFS(dvp)->n_flag |= NLMODIFIED;
1458 VTONFS(dvp)->n_attrstamp = 0;
1464 * just call nfs_mknodrpc() to do the work.
1466 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1467 * struct componentname *a_cnp, struct vattr *a_vap)
1471 nfs_mknod(struct vop_mknod_args *ap)
1473 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1476 static u_long create_verf;
1478 * nfs file create call
1480 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1481 * struct componentname *a_cnp, struct vattr *a_vap)
1484 nfs_create(struct vop_create_args *ap)
1486 struct vnode *dvp = ap->a_dvp;
1487 struct vattr *vap = ap->a_vap;
1488 struct componentname *cnp = ap->a_cnp;
1489 struct nfsv2_sattr *sp;
1493 struct nfsnode *np = (struct nfsnode *)0;
1494 struct vnode *newvp = (struct vnode *)0;
1495 caddr_t bpos, dpos, cp2;
1496 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1497 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1499 int v3 = NFS_ISV3(dvp);
1502 * Oops, not for me..
1504 if (vap->va_type == VSOCK)
1505 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1507 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1510 if (vap->va_vaflags & VA_EXCLUSIVE)
1513 nfsstats.rpccnt[NFSPROC_CREATE]++;
1514 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1515 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1516 nfsm_fhtom(dvp, v3);
1517 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1519 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1520 if (fmode & O_EXCL) {
1521 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1522 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1524 if (!TAILQ_EMPTY(&in_ifaddrhead))
1525 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr;
1528 *tl++ = create_verf;
1529 *tl = ++create_verf;
1531 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1532 nfsm_v3attrbuild(vap, FALSE);
1535 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1536 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1537 sp->sa_uid = nfs_xdrneg1;
1538 sp->sa_gid = nfs_xdrneg1;
1540 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1541 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1543 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1545 nfsm_mtofh(dvp, newvp, v3, gotvp);
1549 newvp = (struct vnode *)0;
1551 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1552 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1558 nfsm_wcc_data(dvp, wccflag);
1562 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1568 } else if (v3 && (fmode & O_EXCL)) {
1570 * We are normally called with only a partially initialized
1571 * VAP. Since the NFSv3 spec says that server may use the
1572 * file attributes to store the verifier, the spec requires
1573 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1574 * in atime, but we can't really assume that all servers will
1575 * so we ensure that our SETATTR sets both atime and mtime.
1577 if (vap->va_mtime.tv_sec == VNOVAL)
1578 vfs_timestamp(&vap->va_mtime);
1579 if (vap->va_atime.tv_sec == VNOVAL)
1580 vap->va_atime = vap->va_mtime;
1581 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1585 * The new np may have enough info for access
1586 * checks, make sure rucred and wucred are
1587 * initialized for read and write rpc's.
1590 if (np->n_rucred == NULL)
1591 np->n_rucred = crhold(cnp->cn_cred);
1592 if (np->n_wucred == NULL)
1593 np->n_wucred = crhold(cnp->cn_cred);
1596 VTONFS(dvp)->n_flag |= NLMODIFIED;
1598 VTONFS(dvp)->n_attrstamp = 0;
1603 * nfs file remove call
1604 * To try and make nfs semantics closer to ufs semantics, a file that has
1605 * other processes using the vnode is renamed instead of removed and then
1606 * removed later on the last close.
1607 * - If v_usecount > 1
1608 * If a rename is not already in the works
1609 * call nfs_sillyrename() to set it up
1613 * nfs_remove(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
1614 * struct vnode *a_vp, struct componentname *a_cnp)
1617 nfs_remove(struct vop_remove_args *ap)
1619 struct vnode *vp = ap->a_vp;
1620 struct vnode *dvp = ap->a_dvp;
1621 struct componentname *cnp = ap->a_cnp;
1622 struct nfsnode *np = VTONFS(vp);
1627 if (vp->v_usecount < 1)
1628 panic("nfs_remove: bad v_usecount");
1630 if (vp->v_type == VDIR)
1632 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1633 VOP_GETATTR(vp, &vattr, cnp->cn_td) == 0 &&
1634 vattr.va_nlink > 1)) {
1636 * throw away biocache buffers, mainly to avoid
1637 * unnecessary delayed writes later.
1639 error = nfs_vinvalbuf(vp, 0, cnp->cn_td, 1);
1642 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1643 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1645 * Kludge City: If the first reply to the remove rpc is lost..
1646 * the reply to the retransmitted request will be ENOENT
1647 * since the file was in fact removed
1648 * Therefore, we cheat and return success.
1650 if (error == ENOENT)
1652 } else if (!np->n_sillyrename) {
1653 error = nfs_sillyrename(dvp, vp, cnp);
1655 np->n_attrstamp = 0;
1660 * nfs file remove rpc called from nfs_inactive
1663 nfs_removeit(struct sillyrename *sp)
1665 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1670 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1673 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1674 struct ucred *cred, struct thread *td)
1679 caddr_t bpos, dpos, cp2;
1680 int error = 0, wccflag = NFSV3_WCCRATTR;
1681 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1682 int v3 = NFS_ISV3(dvp);
1684 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1685 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1686 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1687 nfsm_fhtom(dvp, v3);
1688 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1689 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1691 nfsm_wcc_data(dvp, wccflag);
1694 VTONFS(dvp)->n_flag |= NLMODIFIED;
1696 VTONFS(dvp)->n_attrstamp = 0;
1701 * nfs file rename call
1703 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1704 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1705 * struct vnode *a_tvp, struct componentname *a_tcnp)
1708 nfs_rename(struct vop_rename_args *ap)
1710 struct vnode *fvp = ap->a_fvp;
1711 struct vnode *tvp = ap->a_tvp;
1712 struct vnode *fdvp = ap->a_fdvp;
1713 struct vnode *tdvp = ap->a_tdvp;
1714 struct componentname *tcnp = ap->a_tcnp;
1715 struct componentname *fcnp = ap->a_fcnp;
1718 /* Check for cross-device rename */
1719 if ((fvp->v_mount != tdvp->v_mount) ||
1720 (tvp && (fvp->v_mount != tvp->v_mount))) {
1726 * We have to flush B_DELWRI data prior to renaming
1727 * the file. If we don't, the delayed-write buffers
1728 * can be flushed out later after the file has gone stale
1729 * under NFSV3. NFSV2 does not have this problem because
1730 * ( as far as I can tell ) it flushes dirty buffers more
1734 VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_td);
1736 VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_td);
1739 * If the tvp exists and is in use, sillyrename it before doing the
1740 * rename of the new file over it.
1742 * XXX Can't sillyrename a directory.
1744 * We do not attempt to do any namecache purges in this old API
1745 * routine. The new API compat functions have access to the actual
1746 * namecache structures and will do it for us.
1748 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1749 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1756 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1757 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1770 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1772 if (error == ENOENT)
1778 * nfs file rename rpc called from nfs_remove() above
1781 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1782 struct sillyrename *sp)
1784 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1785 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1789 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1792 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1793 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1794 struct ucred *cred, struct thread *td)
1799 caddr_t bpos, dpos, cp2;
1800 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1801 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1802 int v3 = NFS_ISV3(fdvp);
1804 nfsstats.rpccnt[NFSPROC_RENAME]++;
1805 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1806 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1807 nfsm_rndup(tnamelen));
1808 nfsm_fhtom(fdvp, v3);
1809 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1810 nfsm_fhtom(tdvp, v3);
1811 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1812 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1814 nfsm_wcc_data(fdvp, fwccflag);
1815 nfsm_wcc_data(tdvp, twccflag);
1819 VTONFS(fdvp)->n_flag |= NLMODIFIED;
1820 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1822 VTONFS(fdvp)->n_attrstamp = 0;
1824 VTONFS(tdvp)->n_attrstamp = 0;
1829 * nfs hard link create call
1831 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1832 * struct componentname *a_cnp)
1835 nfs_link(struct vop_link_args *ap)
1837 struct vnode *vp = ap->a_vp;
1838 struct vnode *tdvp = ap->a_tdvp;
1839 struct componentname *cnp = ap->a_cnp;
1843 caddr_t bpos, dpos, cp2;
1844 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1845 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1848 if (vp->v_mount != tdvp->v_mount) {
1853 * Push all writes to the server, so that the attribute cache
1854 * doesn't get "out of sync" with the server.
1855 * XXX There should be a better way!
1857 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_td);
1860 nfsstats.rpccnt[NFSPROC_LINK]++;
1861 nfsm_reqhead(vp, NFSPROC_LINK,
1862 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1864 nfsm_fhtom(tdvp, v3);
1865 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1866 nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1868 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1869 nfsm_wcc_data(tdvp, wccflag);
1873 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1875 VTONFS(vp)->n_attrstamp = 0;
1877 VTONFS(tdvp)->n_attrstamp = 0;
1879 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1881 if (error == EEXIST)
1887 * nfs symbolic link create call
1889 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1890 * struct componentname *a_cnp, struct vattr *a_vap,
1894 nfs_symlink(struct vop_symlink_args *ap)
1896 struct vnode *dvp = ap->a_dvp;
1897 struct vattr *vap = ap->a_vap;
1898 struct componentname *cnp = ap->a_cnp;
1899 struct nfsv2_sattr *sp;
1903 caddr_t bpos, dpos, cp2;
1904 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1905 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1906 struct vnode *newvp = (struct vnode *)0;
1907 int v3 = NFS_ISV3(dvp);
1909 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1910 slen = strlen(ap->a_target);
1911 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1912 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1913 nfsm_fhtom(dvp, v3);
1914 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1916 nfsm_v3attrbuild(vap, FALSE);
1918 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1920 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1921 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1922 sp->sa_uid = nfs_xdrneg1;
1923 sp->sa_gid = nfs_xdrneg1;
1924 sp->sa_size = nfs_xdrneg1;
1925 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1926 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1930 * Issue the NFS request and get the rpc response.
1932 * Only NFSv3 responses returning an error of 0 actually return
1933 * a file handle that can be converted into newvp without having
1934 * to do an extra lookup rpc.
1936 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1939 nfsm_mtofh(dvp, newvp, v3, gotvp);
1940 nfsm_wcc_data(dvp, wccflag);
1944 * out code jumps -> here, mrep is also freed.
1951 * If we get an EEXIST error, silently convert it to no-error
1952 * in case of an NFS retry.
1954 if (error == EEXIST)
1958 * If we do not have (or no longer have) an error, and we could
1959 * not extract the newvp from the response due to the request being
1960 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1961 * to obtain a newvp to return.
1963 if (error == 0 && newvp == NULL) {
1964 struct nfsnode *np = NULL;
1966 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1967 cnp->cn_cred, cnp->cn_td, &np);
1977 VTONFS(dvp)->n_flag |= NLMODIFIED;
1979 VTONFS(dvp)->n_attrstamp = 0;
1986 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1987 * struct componentname *a_cnp, struct vattr *a_vap)
1990 nfs_mkdir(struct vop_mkdir_args *ap)
1992 struct vnode *dvp = ap->a_dvp;
1993 struct vattr *vap = ap->a_vap;
1994 struct componentname *cnp = ap->a_cnp;
1995 struct nfsv2_sattr *sp;
2000 struct nfsnode *np = (struct nfsnode *)0;
2001 struct vnode *newvp = (struct vnode *)0;
2002 caddr_t bpos, dpos, cp2;
2003 int error = 0, wccflag = NFSV3_WCCRATTR;
2005 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2007 int v3 = NFS_ISV3(dvp);
2009 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
2012 len = cnp->cn_namelen;
2013 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2014 nfsm_reqhead(dvp, NFSPROC_MKDIR,
2015 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2016 nfsm_fhtom(dvp, v3);
2017 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
2019 nfsm_v3attrbuild(vap, FALSE);
2021 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2022 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2023 sp->sa_uid = nfs_xdrneg1;
2024 sp->sa_gid = nfs_xdrneg1;
2025 sp->sa_size = nfs_xdrneg1;
2026 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2027 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2029 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
2031 nfsm_mtofh(dvp, newvp, v3, gotvp);
2033 nfsm_wcc_data(dvp, wccflag);
2036 VTONFS(dvp)->n_flag |= NLMODIFIED;
2038 VTONFS(dvp)->n_attrstamp = 0;
2040 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2041 * if we can succeed in looking up the directory.
2043 if (error == EEXIST || (!error && !gotvp)) {
2046 newvp = (struct vnode *)0;
2048 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2052 if (newvp->v_type != VDIR)
2065 * nfs remove directory call
2067 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2068 * struct componentname *a_cnp)
2071 nfs_rmdir(struct vop_rmdir_args *ap)
2073 struct vnode *vp = ap->a_vp;
2074 struct vnode *dvp = ap->a_dvp;
2075 struct componentname *cnp = ap->a_cnp;
2079 caddr_t bpos, dpos, cp2;
2080 int error = 0, wccflag = NFSV3_WCCRATTR;
2081 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2082 int v3 = NFS_ISV3(dvp);
2086 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2087 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2088 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2089 nfsm_fhtom(dvp, v3);
2090 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2091 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2093 nfsm_wcc_data(dvp, wccflag);
2096 VTONFS(dvp)->n_flag |= NLMODIFIED;
2098 VTONFS(dvp)->n_attrstamp = 0;
2100 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2102 if (error == ENOENT)
2110 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2113 nfs_readdir(struct vop_readdir_args *ap)
2115 struct vnode *vp = ap->a_vp;
2116 struct nfsnode *np = VTONFS(vp);
2117 struct uio *uio = ap->a_uio;
2121 if (vp->v_type != VDIR)
2125 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2126 * and then check that is still valid, or if this is an NQNFS mount
2127 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2128 * VOP_GETATTR() does not necessarily go to the wire.
2130 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2131 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2132 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
2133 if (NQNFS_CKCACHABLE(vp, ND_READ)) {
2134 nfsstats.direofcache_hits++;
2137 } else if (VOP_GETATTR(vp, &vattr, uio->uio_td) == 0 &&
2138 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2140 nfsstats.direofcache_hits++;
2146 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2147 * own cache coherency checks so we do not have to.
2149 tresid = uio->uio_resid;
2150 error = nfs_bioread(vp, uio, 0);
2152 if (!error && uio->uio_resid == tresid)
2153 nfsstats.direofcache_misses++;
2159 * Called from below the buffer cache by nfs_doio().
2162 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2165 struct dirent *dp = NULL;
2170 caddr_t bpos, dpos, cp2;
2171 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2173 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2174 struct nfsnode *dnp = VTONFS(vp);
2176 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2178 int v3 = NFS_ISV3(vp);
2181 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2182 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2183 panic("nfs readdirrpc bad uio");
2187 * If there is no cookie, assume directory was stale.
2189 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2193 return (NFSERR_BAD_COOKIE);
2195 * Loop around doing readdir rpc's of size nm_readdirsize
2196 * truncated to a multiple of DIRBLKSIZ.
2197 * The stopping criteria is EOF or buffer full.
2199 while (more_dirs && bigenough) {
2200 nfsstats.rpccnt[NFSPROC_READDIR]++;
2201 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2205 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2206 *tl++ = cookie.nfsuquad[0];
2207 *tl++ = cookie.nfsuquad[1];
2208 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2209 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2211 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2212 *tl++ = cookie.nfsuquad[0];
2214 *tl = txdr_unsigned(nmp->nm_readdirsize);
2215 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2217 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2219 nfsm_dissect(tl, u_int32_t *,
2221 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2222 dnp->n_cookieverf.nfsuquad[1] = *tl;
2228 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2229 more_dirs = fxdr_unsigned(int, *tl);
2231 /* loop thru the dir entries, doctoring them to 4bsd form */
2232 while (more_dirs && bigenough) {
2234 nfsm_dissect(tl, u_int32_t *,
2236 fileno = fxdr_hyper(tl);
2237 len = fxdr_unsigned(int, *(tl + 2));
2239 nfsm_dissect(tl, u_int32_t *,
2241 fileno = fxdr_unsigned(u_quad_t, *tl++);
2242 len = fxdr_unsigned(int, *tl);
2244 if (len <= 0 || len > NFS_MAXNAMLEN) {
2249 tlen = nfsm_rndup(len);
2251 tlen += 4; /* To ensure null termination */
2252 left = DIRBLKSIZ - blksiz;
2253 if ((tlen + DIRHDSIZ) > left) {
2254 dp->d_reclen += left;
2255 uiop->uio_iov->iov_base += left;
2256 uiop->uio_iov->iov_len -= left;
2257 uiop->uio_offset += left;
2258 uiop->uio_resid -= left;
2261 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2264 dp = (struct dirent *)uiop->uio_iov->iov_base;
2265 dp->d_fileno = (int)fileno;
2267 dp->d_reclen = tlen + DIRHDSIZ;
2268 dp->d_type = DT_UNKNOWN;
2269 blksiz += dp->d_reclen;
2270 if (blksiz == DIRBLKSIZ)
2272 uiop->uio_offset += DIRHDSIZ;
2273 uiop->uio_resid -= DIRHDSIZ;
2274 uiop->uio_iov->iov_base += DIRHDSIZ;
2275 uiop->uio_iov->iov_len -= DIRHDSIZ;
2276 nfsm_mtouio(uiop, len);
2277 cp = uiop->uio_iov->iov_base;
2279 *cp = '\0'; /* null terminate */
2280 uiop->uio_iov->iov_base += tlen;
2281 uiop->uio_iov->iov_len -= tlen;
2282 uiop->uio_offset += tlen;
2283 uiop->uio_resid -= tlen;
2285 nfsm_adv(nfsm_rndup(len));
2287 nfsm_dissect(tl, u_int32_t *,
2290 nfsm_dissect(tl, u_int32_t *,
2294 cookie.nfsuquad[0] = *tl++;
2296 cookie.nfsuquad[1] = *tl++;
2301 more_dirs = fxdr_unsigned(int, *tl);
2304 * If at end of rpc data, get the eof boolean
2307 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2308 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2313 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2314 * by increasing d_reclen for the last record.
2317 left = DIRBLKSIZ - blksiz;
2318 dp->d_reclen += left;
2319 uiop->uio_iov->iov_base += left;
2320 uiop->uio_iov->iov_len -= left;
2321 uiop->uio_offset += left;
2322 uiop->uio_resid -= left;
2326 * We are now either at the end of the directory or have filled the
2330 dnp->n_direofoffset = uiop->uio_offset;
2332 if (uiop->uio_resid > 0)
2333 printf("EEK! readdirrpc resid > 0\n");
2334 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2342 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2345 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2352 struct vnode *newvp;
2354 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2355 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2357 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2358 struct nfsnode *dnp = VTONFS(vp), *np;
2361 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2362 int attrflag, fhsize;
2363 struct namecache *ncp;
2364 struct namecache *dncp;
2365 struct nlcomponent nlc;
2368 dp = (struct dirent *)0;
2371 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2372 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2373 panic("nfs readdirplusrpc bad uio");
2376 * Obtain the namecache record for the directory so we have something
2377 * to use as a basis for creating the entries. This function will
2378 * return a held (but not locked) ncp. The ncp may be disconnected
2379 * from the tree and cannot be used for upward traversals, and the
2380 * ncp may be unnamed. Note that other unrelated operations may
2381 * cause the ncp to be named at any time.
2383 dncp = cache_fromdvp(vp, NULL, 0);
2384 bzero(&nlc, sizeof(nlc));
2388 * If there is no cookie, assume directory was stale.
2390 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2394 return (NFSERR_BAD_COOKIE);
2396 * Loop around doing readdir rpc's of size nm_readdirsize
2397 * truncated to a multiple of DIRBLKSIZ.
2398 * The stopping criteria is EOF or buffer full.
2400 while (more_dirs && bigenough) {
2401 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2402 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2403 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2405 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2406 *tl++ = cookie.nfsuquad[0];
2407 *tl++ = cookie.nfsuquad[1];
2408 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2409 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2410 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2411 *tl = txdr_unsigned(nmp->nm_rsize);
2412 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2413 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2418 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2419 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2420 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2421 more_dirs = fxdr_unsigned(int, *tl);
2423 /* loop thru the dir entries, doctoring them to 4bsd form */
2424 while (more_dirs && bigenough) {
2425 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2426 fileno = fxdr_hyper(tl);
2427 len = fxdr_unsigned(int, *(tl + 2));
2428 if (len <= 0 || len > NFS_MAXNAMLEN) {
2433 tlen = nfsm_rndup(len);
2435 tlen += 4; /* To ensure null termination*/
2436 left = DIRBLKSIZ - blksiz;
2437 if ((tlen + DIRHDSIZ) > left) {
2438 dp->d_reclen += left;
2439 uiop->uio_iov->iov_base += left;
2440 uiop->uio_iov->iov_len -= left;
2441 uiop->uio_offset += left;
2442 uiop->uio_resid -= left;
2445 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2448 dp = (struct dirent *)uiop->uio_iov->iov_base;
2449 dp->d_fileno = (int)fileno;
2451 dp->d_reclen = tlen + DIRHDSIZ;
2452 dp->d_type = DT_UNKNOWN;
2453 blksiz += dp->d_reclen;
2454 if (blksiz == DIRBLKSIZ)
2456 uiop->uio_offset += DIRHDSIZ;
2457 uiop->uio_resid -= DIRHDSIZ;
2458 uiop->uio_iov->iov_base += DIRHDSIZ;
2459 uiop->uio_iov->iov_len -= DIRHDSIZ;
2460 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2461 nlc.nlc_namelen = len;
2462 nfsm_mtouio(uiop, len);
2463 cp = uiop->uio_iov->iov_base;
2466 uiop->uio_iov->iov_base += tlen;
2467 uiop->uio_iov->iov_len -= tlen;
2468 uiop->uio_offset += tlen;
2469 uiop->uio_resid -= tlen;
2471 nfsm_adv(nfsm_rndup(len));
2472 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2474 cookie.nfsuquad[0] = *tl++;
2475 cookie.nfsuquad[1] = *tl++;
2480 * Since the attributes are before the file handle
2481 * (sigh), we must skip over the attributes and then
2482 * come back and get them.
2484 attrflag = fxdr_unsigned(int, *tl);
2488 nfsm_adv(NFSX_V3FATTR);
2489 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2490 doit = fxdr_unsigned(int, *tl);
2492 nfsm_getfh(fhp, fhsize, 1);
2493 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2498 error = nfs_nget(vp->v_mount, fhp,
2506 if (doit && bigenough) {
2511 nfsm_loadattr(newvp, (struct vattr *)0);
2515 IFTODT(VTTOIF(np->n_vattr.va_type));
2517 printf("NFS/READDIRPLUS, ENTER %*.*s\n",
2518 nlc.nlc_namelen, nlc.nlc_namelen,
2520 ncp = cache_nlookup(dncp, &nlc);
2521 cache_setunresolved(ncp);
2522 cache_setvp(ncp, newvp);
2525 printf("NFS/READDIRPLUS, UNABLE TO ENTER"
2527 nlc.nlc_namelen, nlc.nlc_namelen,
2532 /* Just skip over the file handle */
2533 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2534 i = fxdr_unsigned(int, *tl);
2535 nfsm_adv(nfsm_rndup(i));
2537 if (newvp != NULLVP) {
2544 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2545 more_dirs = fxdr_unsigned(int, *tl);
2548 * If at end of rpc data, get the eof boolean
2551 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2552 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2557 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2558 * by increasing d_reclen for the last record.
2561 left = DIRBLKSIZ - blksiz;
2562 dp->d_reclen += left;
2563 uiop->uio_iov->iov_base += left;
2564 uiop->uio_iov->iov_len -= left;
2565 uiop->uio_offset += left;
2566 uiop->uio_resid -= left;
2570 * We are now either at the end of the directory or have filled the
2574 dnp->n_direofoffset = uiop->uio_offset;
2576 if (uiop->uio_resid > 0)
2577 printf("EEK! readdirplusrpc resid > 0\n");
2578 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2582 if (newvp != NULLVP) {
2595 * Silly rename. To make the NFS filesystem that is stateless look a little
2596 * more like the "ufs" a remove of an active vnode is translated to a rename
2597 * to a funny looking filename that is removed by nfs_inactive on the
2598 * nfsnode. There is the potential for another process on a different client
2599 * to create the same funny name between the nfs_lookitup() fails and the
2600 * nfs_rename() completes, but...
2603 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2605 struct sillyrename *sp;
2610 * We previously purged dvp instead of vp. I don't know why, it
2611 * completely destroys performance. We can't do it anyway with the
2612 * new VFS API since we would be breaking the namecache topology.
2614 cache_purge(vp); /* XXX */
2617 if (vp->v_type == VDIR)
2618 panic("nfs: sillyrename dir");
2620 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2621 M_NFSREQ, M_WAITOK);
2622 sp->s_cred = crdup(cnp->cn_cred);
2626 /* Fudge together a funny name */
2627 sp->s_namlen = sprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
2629 /* Try lookitups until we get one that isn't there */
2630 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2631 cnp->cn_td, (struct nfsnode **)0) == 0) {
2633 if (sp->s_name[4] > 'z') {
2638 error = nfs_renameit(dvp, cnp, sp);
2641 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2643 np->n_sillyrename = sp;
2648 free((caddr_t)sp, M_NFSREQ);
2653 * Look up a file name and optionally either update the file handle or
2654 * allocate an nfsnode, depending on the value of npp.
2655 * npp == NULL --> just do the lookup
2656 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2658 * *npp != NULL --> update the file handle in the vnode
2661 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2662 struct thread *td, struct nfsnode **npp)
2667 struct vnode *newvp = (struct vnode *)0;
2668 struct nfsnode *np, *dnp = VTONFS(dvp);
2669 caddr_t bpos, dpos, cp2;
2670 int error = 0, fhlen, attrflag;
2671 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2673 int v3 = NFS_ISV3(dvp);
2675 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2676 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2677 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2678 nfsm_fhtom(dvp, v3);
2679 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2680 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2681 if (npp && !error) {
2682 nfsm_getfh(nfhp, fhlen, v3);
2685 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2686 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2687 np->n_fhp = &np->n_fh;
2688 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2689 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2690 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2691 np->n_fhsize = fhlen;
2693 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2697 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2705 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
2706 if (!attrflag && *npp == NULL) {
2715 nfsm_loadattr(newvp, (struct vattr *)0);
2719 if (npp && *npp == NULL) {
2734 * Nfs Version 3 commit rpc
2737 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2742 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2743 caddr_t bpos, dpos, cp2;
2744 int error = 0, wccflag = NFSV3_WCCRATTR;
2745 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2747 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2749 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2750 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2752 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2753 txdr_hyper(offset, tl);
2755 *tl = txdr_unsigned(cnt);
2756 nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2757 nfsm_wcc_data(vp, wccflag);
2759 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2760 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2761 NFSX_V3WRITEVERF)) {
2762 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2764 error = NFSERR_STALEWRITEVERF;
2774 * - make nfs_bmap() essentially a no-op that does no translation
2775 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2776 * (Maybe I could use the process's page mapping, but I was concerned that
2777 * Kernel Write might not be enabled and also figured copyout() would do
2778 * a lot more work than bcopy() and also it currently happens in the
2779 * context of the swapper process (2).
2781 * nfs_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
2782 * daddr_t *a_bnp, int *a_runp, int *a_runb)
2785 nfs_bmap(struct vop_bmap_args *ap)
2787 struct vnode *vp = ap->a_vp;
2789 if (ap->a_vpp != NULL)
2791 if (ap->a_bnp != NULL)
2792 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2793 if (ap->a_runp != NULL)
2795 if (ap->a_runb != NULL)
2802 * For async requests when nfsiod(s) are running, queue the request by
2803 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2807 nfs_strategy(struct vop_strategy_args *ap)
2809 struct buf *bp = ap->a_bp;
2813 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2814 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2816 if (bp->b_flags & B_PHYS)
2817 panic("nfs physio");
2819 if (bp->b_flags & B_ASYNC)
2822 td = curthread; /* XXX */
2825 * If the op is asynchronous and an i/o daemon is waiting
2826 * queue the request, wake it up and wait for completion
2827 * otherwise just do it ourselves.
2829 if ((bp->b_flags & B_ASYNC) == 0 ||
2830 nfs_asyncio(bp, td))
2831 error = nfs_doio(bp, td);
2838 * NB Currently unsupported.
2840 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
2841 * struct thread *a_td)
2845 nfs_mmap(struct vop_mmap_args *ap)
2851 * fsync vnode op. Just call nfs_flush() with commit == 1.
2853 * nfs_fsync(struct vnodeop_desc *a_desc, struct vnode *a_vp,
2854 * struct ucred * a_cred, int a_waitfor, struct thread *a_td)
2858 nfs_fsync(struct vop_fsync_args *ap)
2860 return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2864 * Flush all the blocks associated with a vnode.
2865 * Walk through the buffer pool and push any dirty pages
2866 * associated with the vnode.
2869 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
2871 struct nfsnode *np = VTONFS(vp);
2875 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2876 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2878 u_quad_t off, endoff, toff;
2879 struct buf **bvec = NULL;
2880 #ifndef NFS_COMMITBVECSIZ
2881 #define NFS_COMMITBVECSIZ 20
2883 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2884 int bvecsize = 0, bveccount;
2886 if (nmp->nm_flag & NFSMNT_INT)
2891 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2892 * server, but nas not been committed to stable storage on the server
2893 * yet. On the first pass, the byte range is worked out and the commit
2894 * rpc is done. On the second pass, nfs_writebp() is called to do the
2901 if (NFS_ISV3(vp) && commit) {
2904 * Count up how many buffers waiting for a commit.
2907 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2908 nbp = TAILQ_NEXT(bp, b_vnbufs);
2909 if (BUF_REFCNT(bp) == 0 &&
2910 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2911 == (B_DELWRI | B_NEEDCOMMIT))
2915 * Allocate space to remember the list of bufs to commit. It is
2916 * important to use M_NOWAIT here to avoid a race with nfs_write.
2917 * If we can't get memory (for whatever reason), we will end up
2918 * committing the buffers one-by-one in the loop below.
2920 if (bvec != NULL && bvec != bvec_on_stack)
2922 if (bveccount > NFS_COMMITBVECSIZ) {
2923 bvec = (struct buf **)
2924 malloc(bveccount * sizeof(struct buf *),
2927 bvec = bvec_on_stack;
2928 bvecsize = NFS_COMMITBVECSIZ;
2930 bvecsize = bveccount;
2932 bvec = bvec_on_stack;
2933 bvecsize = NFS_COMMITBVECSIZ;
2935 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2936 nbp = TAILQ_NEXT(bp, b_vnbufs);
2937 if (bvecpos >= bvecsize)
2939 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2940 (B_DELWRI | B_NEEDCOMMIT) ||
2941 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
2945 * NOTE: we are not clearing B_DONE here, so we have
2946 * to do it later on in this routine if we intend to
2947 * initiate I/O on the bp.
2949 * Note: to avoid loopback deadlocks, we do not
2950 * assign b_runningbufspace.
2952 bp->b_flags |= B_WRITEINPROG;
2953 vfs_busy_pages(bp, 1);
2956 * bp is protected by being locked, but nbp is not
2957 * and vfs_busy_pages() may sleep. We have to
2960 nbp = TAILQ_NEXT(bp, b_vnbufs);
2963 * A list of these buffers is kept so that the
2964 * second loop knows which buffers have actually
2965 * been committed. This is necessary, since there
2966 * may be a race between the commit rpc and new
2967 * uncommitted writes on the file.
2969 bvec[bvecpos++] = bp;
2970 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2974 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2982 * Commit data on the server, as required. Note that
2983 * nfs_commit will use the vnode's cred for the commit.
2985 retv = nfs_commit(vp, off, (int)(endoff - off), td);
2987 if (retv == NFSERR_STALEWRITEVERF)
2988 nfs_clearcommit(vp->v_mount);
2991 * Now, either mark the blocks I/O done or mark the
2992 * blocks dirty, depending on whether the commit
2995 for (i = 0; i < bvecpos; i++) {
2997 bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG | B_CLUSTEROK);
3000 * Error, leave B_DELWRI intact
3002 vfs_unbusy_pages(bp);
3006 * Success, remove B_DELWRI ( bundirty() ).
3008 * b_dirtyoff/b_dirtyend seem to be NFS
3009 * specific. We should probably move that
3010 * into bundirty(). XXX
3014 bp->b_flags |= B_ASYNC;
3016 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3017 bp->b_dirtyoff = bp->b_dirtyend = 0;
3025 * Start/do any write(s) that are required.
3029 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
3030 nbp = TAILQ_NEXT(bp, b_vnbufs);
3031 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
3032 if (waitfor != MNT_WAIT || passone)
3034 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL,
3035 "nfsfsync", slpflag, slptimeo);
3038 panic("nfs_fsync: inconsistent lock");
3039 if (error == ENOLCK)
3041 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
3045 if (slpflag == PCATCH) {
3051 if ((bp->b_flags & B_DELWRI) == 0)
3052 panic("nfs_fsync: not dirty");
3053 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3058 if (passone || !commit)
3059 bp->b_flags |= B_ASYNC;
3061 bp->b_flags |= B_ASYNC | B_WRITEINPROG;
3063 VOP_BWRITE(bp->b_vp, bp);
3071 if (waitfor == MNT_WAIT) {
3072 while (vp->v_numoutput) {
3073 vp->v_flag |= VBWAIT;
3074 error = tsleep((caddr_t)&vp->v_numoutput,
3075 slpflag, "nfsfsync", slptimeo);
3077 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
3081 if (slpflag == PCATCH) {
3087 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) && commit) {
3091 if (np->n_flag & NWRITEERR) {
3092 error = np->n_error;
3093 np->n_flag &= ~NWRITEERR;
3096 if (bvec != NULL && bvec != bvec_on_stack)
3102 * NFS advisory byte-level locks.
3103 * Currently unsupported.
3105 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3109 nfs_advlock(struct vop_advlock_args *ap)
3111 struct nfsnode *np = VTONFS(ap->a_vp);
3114 * The following kludge is to allow diskless support to work
3115 * until a real NFS lockd is implemented. Basically, just pretend
3116 * that this is a local lock.
3118 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3122 * Print out the contents of an nfsnode.
3124 * nfs_print(struct vnode *a_vp)
3127 nfs_print(struct vop_print_args *ap)
3129 struct vnode *vp = ap->a_vp;
3130 struct nfsnode *np = VTONFS(vp);
3132 printf("tag VT_NFS, fileid %ld fsid 0x%x",
3133 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3134 if (vp->v_type == VFIFO)
3141 * Just call nfs_writebp() with the force argument set to 1.
3143 * NOTE: B_DONE may or may not be set in a_bp on call.
3145 * nfs_bwrite(struct vnode *a_bp)
3148 nfs_bwrite(struct vop_bwrite_args *ap)
3150 return (nfs_writebp(ap->a_bp, 1, curthread));
3154 * This is a clone of vn_bwrite(), except that B_WRITEINPROG isn't set unless
3155 * the force flag is one and it also handles the B_NEEDCOMMIT flag. We set
3156 * B_CACHE if this is a VMIO buffer.
3159 nfs_writebp(struct buf *bp, int force, struct thread *td)
3162 int oldflags = bp->b_flags;
3168 if (BUF_REFCNT(bp) == 0)
3169 panic("bwrite: buffer is not locked???");
3171 if (bp->b_flags & B_INVAL) {
3176 bp->b_flags |= B_CACHE;
3179 * Undirty the bp. We will redirty it later if the I/O fails.
3184 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3186 bp->b_vp->v_numoutput++;
3190 * Note: to avoid loopback deadlocks, we do not
3191 * assign b_runningbufspace.
3193 vfs_busy_pages(bp, 1);
3196 bp->b_flags |= B_WRITEINPROG;
3198 VOP_STRATEGY(bp->b_vp, bp);
3200 if( (oldflags & B_ASYNC) == 0) {
3201 int rtval = biowait(bp);
3203 if (oldflags & B_DELWRI) {
3205 reassignbuf(bp, bp->b_vp);
3217 * nfs special file access vnode op.
3218 * Essentially just get vattr and then imitate iaccess() since the device is
3219 * local to the client.
3221 * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
3222 * struct thread *a_td)
3225 nfsspec_access(struct vop_access_args *ap)
3229 struct ucred *cred = ap->a_cred;
3230 struct vnode *vp = ap->a_vp;
3231 mode_t mode = ap->a_mode;
3237 * Disallow write attempts on filesystems mounted read-only;
3238 * unless the file is a socket, fifo, or a block or character
3239 * device resident on the filesystem.
3241 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3242 switch (vp->v_type) {
3252 * If you're the super-user,
3253 * you always get access.
3255 if (cred->cr_uid == 0)
3258 error = VOP_GETATTR(vp, vap, ap->a_td);
3262 * Access check is based on only one of owner, group, public.
3263 * If not owner, then check group. If not a member of the
3264 * group, then check public access.
3266 if (cred->cr_uid != vap->va_uid) {
3268 gp = cred->cr_groups;
3269 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3270 if (vap->va_gid == *gp)
3276 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3281 * Read wrapper for special devices.
3283 * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3284 * struct ucred *a_cred)
3287 nfsspec_read(struct vop_read_args *ap)
3289 struct nfsnode *np = VTONFS(ap->a_vp);
3295 getnanotime(&np->n_atim);
3296 return (VOCALL(spec_vnode_vops, &ap->a_head));
3300 * Write wrapper for special devices.
3302 * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3303 * struct ucred *a_cred)
3306 nfsspec_write(struct vop_write_args *ap)
3308 struct nfsnode *np = VTONFS(ap->a_vp);
3314 getnanotime(&np->n_mtim);
3315 return (VOCALL(spec_vnode_vops, &ap->a_head));
3319 * Close wrapper for special devices.
3321 * Update the times on the nfsnode then do device close.
3323 * nfsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
3324 * struct thread *a_td)
3327 nfsspec_close(struct vop_close_args *ap)
3329 struct vnode *vp = ap->a_vp;
3330 struct nfsnode *np = VTONFS(vp);
3333 if (np->n_flag & (NACC | NUPD)) {
3335 if (vp->v_usecount == 1 &&
3336 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3338 if (np->n_flag & NACC)
3339 vattr.va_atime = np->n_atim;
3340 if (np->n_flag & NUPD)
3341 vattr.va_mtime = np->n_mtim;
3342 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3345 return (VOCALL(spec_vnode_vops, &ap->a_head));
3349 * Read wrapper for fifos.
3351 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3352 * struct ucred *a_cred)
3355 nfsfifo_read(struct vop_read_args *ap)
3357 struct nfsnode *np = VTONFS(ap->a_vp);
3363 getnanotime(&np->n_atim);
3364 return (VOCALL(fifo_vnode_vops, &ap->a_head));
3368 * Write wrapper for fifos.
3370 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3371 * struct ucred *a_cred)
3374 nfsfifo_write(struct vop_write_args *ap)
3376 struct nfsnode *np = VTONFS(ap->a_vp);
3382 getnanotime(&np->n_mtim);
3383 return (VOCALL(fifo_vnode_vops, &ap->a_head));
3387 * Close wrapper for fifos.
3389 * Update the times on the nfsnode then do fifo close.
3391 * nfsfifo_close(struct vnode *a_vp, int a_fflag, struct thread *a_td)
3394 nfsfifo_close(struct vop_close_args *ap)
3396 struct vnode *vp = ap->a_vp;
3397 struct nfsnode *np = VTONFS(vp);
3401 if (np->n_flag & (NACC | NUPD)) {
3403 if (np->n_flag & NACC)
3405 if (np->n_flag & NUPD)
3408 if (vp->v_usecount == 1 &&
3409 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3411 if (np->n_flag & NACC)
3412 vattr.va_atime = np->n_atim;
3413 if (np->n_flag & NUPD)
3414 vattr.va_mtime = np->n_mtim;
3415 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3418 return (VOCALL(fifo_vnode_vops, &ap->a_head));