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.42 2005/08/27 20:23:06 joerg 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>
75 #include <vfs/ufs/dir.h>
85 #include "nfsm_subs.h"
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
92 #include <sys/thread2.h>
99 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
100 * calls are not in getblk() and brelse() so that they would not be necessary
104 #define vfs_busy_pages(bp, f)
107 static int nfsspec_read (struct vop_read_args *);
108 static int nfsspec_write (struct vop_write_args *);
109 static int nfsfifo_read (struct vop_read_args *);
110 static int nfsfifo_write (struct vop_write_args *);
111 static int nfsspec_close (struct vop_close_args *);
112 static int nfsfifo_close (struct vop_close_args *);
113 #define nfs_poll vop_nopoll
114 static int nfs_setattrrpc (struct vnode *,struct vattr *,struct ucred *,struct thread *);
115 static int nfs_lookup (struct vop_lookup_args *);
116 static int nfs_create (struct vop_create_args *);
117 static int nfs_mknod (struct vop_mknod_args *);
118 static int nfs_open (struct vop_open_args *);
119 static int nfs_close (struct vop_close_args *);
120 static int nfs_access (struct vop_access_args *);
121 static int nfs_getattr (struct vop_getattr_args *);
122 static int nfs_setattr (struct vop_setattr_args *);
123 static int nfs_read (struct vop_read_args *);
124 static int nfs_mmap (struct vop_mmap_args *);
125 static int nfs_fsync (struct vop_fsync_args *);
126 static int nfs_remove (struct vop_remove_args *);
127 static int nfs_link (struct vop_link_args *);
128 static int nfs_rename (struct vop_rename_args *);
129 static int nfs_mkdir (struct vop_mkdir_args *);
130 static int nfs_rmdir (struct vop_rmdir_args *);
131 static int nfs_symlink (struct vop_symlink_args *);
132 static int nfs_readdir (struct vop_readdir_args *);
133 static int nfs_bmap (struct vop_bmap_args *);
134 static int nfs_strategy (struct vop_strategy_args *);
135 static int nfs_lookitup (struct vnode *, const char *, int,
136 struct ucred *, struct thread *, struct nfsnode **);
137 static int nfs_sillyrename (struct vnode *,struct vnode *,struct componentname *);
138 static int nfsspec_access (struct vop_access_args *);
139 static int nfs_readlink (struct vop_readlink_args *);
140 static int nfs_print (struct vop_print_args *);
141 static int nfs_advlock (struct vop_advlock_args *);
142 static int nfs_bwrite (struct vop_bwrite_args *);
144 static int nfs_nresolve (struct vop_nresolve_args *);
146 * Global vfs data structures for nfs
148 struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
149 { &vop_default_desc, vop_defaultop },
150 { &vop_access_desc, (vnodeopv_entry_t) nfs_access },
151 { &vop_advlock_desc, (vnodeopv_entry_t) nfs_advlock },
152 { &vop_bmap_desc, (vnodeopv_entry_t) nfs_bmap },
153 { &vop_bwrite_desc, (vnodeopv_entry_t) nfs_bwrite },
154 { &vop_close_desc, (vnodeopv_entry_t) nfs_close },
155 { &vop_create_desc, (vnodeopv_entry_t) nfs_create },
156 { &vop_fsync_desc, (vnodeopv_entry_t) nfs_fsync },
157 { &vop_getattr_desc, (vnodeopv_entry_t) nfs_getattr },
158 { &vop_getpages_desc, (vnodeopv_entry_t) nfs_getpages },
159 { &vop_putpages_desc, (vnodeopv_entry_t) nfs_putpages },
160 { &vop_inactive_desc, (vnodeopv_entry_t) nfs_inactive },
161 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
162 { &vop_lease_desc, vop_null },
163 { &vop_link_desc, (vnodeopv_entry_t) nfs_link },
164 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
165 { &vop_lookup_desc, (vnodeopv_entry_t) nfs_lookup },
166 { &vop_mkdir_desc, (vnodeopv_entry_t) nfs_mkdir },
167 { &vop_mknod_desc, (vnodeopv_entry_t) nfs_mknod },
168 { &vop_mmap_desc, (vnodeopv_entry_t) nfs_mmap },
169 { &vop_open_desc, (vnodeopv_entry_t) nfs_open },
170 { &vop_poll_desc, (vnodeopv_entry_t) nfs_poll },
171 { &vop_print_desc, (vnodeopv_entry_t) nfs_print },
172 { &vop_read_desc, (vnodeopv_entry_t) nfs_read },
173 { &vop_readdir_desc, (vnodeopv_entry_t) nfs_readdir },
174 { &vop_readlink_desc, (vnodeopv_entry_t) nfs_readlink },
175 { &vop_reclaim_desc, (vnodeopv_entry_t) nfs_reclaim },
176 { &vop_remove_desc, (vnodeopv_entry_t) nfs_remove },
177 { &vop_rename_desc, (vnodeopv_entry_t) nfs_rename },
178 { &vop_rmdir_desc, (vnodeopv_entry_t) nfs_rmdir },
179 { &vop_setattr_desc, (vnodeopv_entry_t) nfs_setattr },
180 { &vop_strategy_desc, (vnodeopv_entry_t) nfs_strategy },
181 { &vop_symlink_desc, (vnodeopv_entry_t) nfs_symlink },
182 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
183 { &vop_write_desc, (vnodeopv_entry_t) nfs_write },
185 { &vop_nresolve_desc, (vnodeopv_entry_t) nfs_nresolve },
190 * Special device vnode ops
192 struct vnodeopv_entry_desc nfsv2_specop_entries[] = {
193 { &vop_default_desc, (vnodeopv_entry_t) spec_vnoperate },
194 { &vop_access_desc, (vnodeopv_entry_t) nfsspec_access },
195 { &vop_close_desc, (vnodeopv_entry_t) nfsspec_close },
196 { &vop_fsync_desc, (vnodeopv_entry_t) nfs_fsync },
197 { &vop_getattr_desc, (vnodeopv_entry_t) nfs_getattr },
198 { &vop_inactive_desc, (vnodeopv_entry_t) nfs_inactive },
199 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
200 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
201 { &vop_print_desc, (vnodeopv_entry_t) nfs_print },
202 { &vop_read_desc, (vnodeopv_entry_t) nfsspec_read },
203 { &vop_reclaim_desc, (vnodeopv_entry_t) nfs_reclaim },
204 { &vop_setattr_desc, (vnodeopv_entry_t) nfs_setattr },
205 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
206 { &vop_write_desc, (vnodeopv_entry_t) nfsspec_write },
210 struct vnodeopv_entry_desc nfsv2_fifoop_entries[] = {
211 { &vop_default_desc, (vnodeopv_entry_t) fifo_vnoperate },
212 { &vop_access_desc, (vnodeopv_entry_t) nfsspec_access },
213 { &vop_close_desc, (vnodeopv_entry_t) nfsfifo_close },
214 { &vop_fsync_desc, (vnodeopv_entry_t) nfs_fsync },
215 { &vop_getattr_desc, (vnodeopv_entry_t) nfs_getattr },
216 { &vop_inactive_desc, (vnodeopv_entry_t) nfs_inactive },
217 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
218 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
219 { &vop_print_desc, (vnodeopv_entry_t) nfs_print },
220 { &vop_read_desc, (vnodeopv_entry_t) nfsfifo_read },
221 { &vop_reclaim_desc, (vnodeopv_entry_t) nfs_reclaim },
222 { &vop_setattr_desc, (vnodeopv_entry_t) nfs_setattr },
223 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
224 { &vop_write_desc, (vnodeopv_entry_t) nfsfifo_write },
228 static int nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
229 struct componentname *cnp,
231 static int nfs_removerpc (struct vnode *dvp, const char *name,
233 struct ucred *cred, struct thread *td);
234 static int nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
235 int fnamelen, struct vnode *tdvp,
236 const char *tnameptr, int tnamelen,
237 struct ucred *cred, struct thread *td);
238 static int nfs_renameit (struct vnode *sdvp,
239 struct componentname *scnp,
240 struct sillyrename *sp);
245 extern u_int32_t nfs_true, nfs_false;
246 extern u_int32_t nfs_xdrneg1;
247 extern struct nfsstats nfsstats;
248 extern nfstype nfsv3_type[9];
249 struct thread *nfs_iodwant[NFS_MAXASYNCDAEMON];
250 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
251 int nfs_numasync = 0;
253 SYSCTL_DECL(_vfs_nfs);
255 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
256 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
257 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
259 static int nfsneg_cache_timeout = NFS_MINATTRTIMO;
260 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
261 &nfsneg_cache_timeout, 0, "NFS NEGATIVE ACCESS cache timeout");
263 static int nfsv3_commit_on_close = 0;
264 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
265 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
267 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
268 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
270 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
271 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
274 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
275 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
276 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
278 nfs3_access_otw(struct vnode *vp, int wmode,
279 struct thread *td, struct ucred *cred)
283 int error = 0, attrflag;
285 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
286 caddr_t bpos, dpos, cp2;
290 struct nfsnode *np = VTONFS(vp);
292 nfsstats.rpccnt[NFSPROC_ACCESS]++;
293 nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
295 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
296 *tl = txdr_unsigned(wmode);
297 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
298 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
300 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
301 rmode = fxdr_unsigned(u_int32_t, *tl);
303 np->n_modeuid = cred->cr_uid;
304 np->n_modestamp = mycpu->gd_time_seconds;
312 * nfs access vnode op.
313 * For nfs version 2, just return ok. File accesses may fail later.
314 * For nfs version 3, use the access rpc to check accessibility. If file modes
315 * are changed on the server, accesses might still fail later.
317 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
318 * struct thread *a_td)
321 nfs_access(struct vop_access_args *ap)
323 struct vnode *vp = ap->a_vp;
325 u_int32_t mode, wmode;
326 int v3 = NFS_ISV3(vp);
327 struct nfsnode *np = VTONFS(vp);
330 * Disallow write attempts on filesystems mounted read-only;
331 * unless the file is a socket, fifo, or a block or character
332 * device resident on the filesystem.
334 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
335 switch (vp->v_type) {
345 * For nfs v3, check to see if we have done this recently, and if
346 * so return our cached result instead of making an ACCESS call.
347 * If not, do an access rpc, otherwise you are stuck emulating
348 * ufs_access() locally using the vattr. This may not be correct,
349 * since the server may apply other access criteria such as
350 * client uid-->server uid mapping that we do not know about.
353 if (ap->a_mode & VREAD)
354 mode = NFSV3ACCESS_READ;
357 if (vp->v_type != VDIR) {
358 if (ap->a_mode & VWRITE)
359 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
360 if (ap->a_mode & VEXEC)
361 mode |= NFSV3ACCESS_EXECUTE;
363 if (ap->a_mode & VWRITE)
364 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
366 if (ap->a_mode & VEXEC)
367 mode |= NFSV3ACCESS_LOOKUP;
369 /* XXX safety belt, only make blanket request if caching */
370 if (nfsaccess_cache_timeout > 0) {
371 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
372 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
373 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
379 * Does our cached result allow us to give a definite yes to
382 if (np->n_modestamp &&
383 (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
384 (ap->a_cred->cr_uid == np->n_modeuid) &&
385 ((np->n_mode & mode) == mode)) {
386 nfsstats.accesscache_hits++;
389 * Either a no, or a don't know. Go to the wire.
391 nfsstats.accesscache_misses++;
392 error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred);
394 if ((np->n_mode & mode) != mode) {
400 if ((error = nfsspec_access(ap)) != 0)
404 * Attempt to prevent a mapped root from accessing a file
405 * which it shouldn't. We try to read a byte from the file
406 * if the user is root and the file is not zero length.
407 * After calling nfsspec_access, we should have the correct
410 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
411 && VTONFS(vp)->n_size > 0) {
418 auio.uio_iov = &aiov;
422 auio.uio_segflg = UIO_SYSSPACE;
423 auio.uio_rw = UIO_READ;
424 auio.uio_td = ap->a_td;
426 if (vp->v_type == VREG) {
427 error = nfs_readrpc(vp, &auio);
428 } else if (vp->v_type == VDIR) {
430 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
432 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
433 error = nfs_readdirrpc(vp, &auio);
435 } else if (vp->v_type == VLNK) {
436 error = nfs_readlinkrpc(vp, &auio);
443 * [re]record creds for reading and/or writing if access
444 * was granted. Assume the NFS server will grant read access
445 * for execute requests.
448 if ((ap->a_mode & (VREAD|VEXEC)) && ap->a_cred != np->n_rucred) {
451 crfree(np->n_rucred);
452 np->n_rucred = ap->a_cred;
454 if ((ap->a_mode & VWRITE) && ap->a_cred != np->n_wucred) {
457 crfree(np->n_wucred);
458 np->n_wucred = ap->a_cred;
466 * Check to see if the type is ok
467 * and that deletion is not in progress.
468 * For paged in text files, you will need to flush the page cache
469 * if consistency is lost.
471 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
472 * struct thread *a_td)
476 nfs_open(struct vop_open_args *ap)
478 struct vnode *vp = ap->a_vp;
479 struct nfsnode *np = VTONFS(vp);
480 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
484 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
486 printf("open eacces vtyp=%d\n",vp->v_type);
492 * Clear the attribute cache only if opening with write access. It
493 * is unclear if we should do this at all here, but we certainly
494 * should not clear the cache unconditionally simply because a file
497 if (ap->a_mode & FWRITE)
500 if (nmp->nm_flag & NFSMNT_NQNFS) {
502 * If NQNFS is active, get a valid lease
504 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
506 error = nqnfs_getlease(vp, ND_READ, ap->a_td);
507 } while (error == NQNFS_EXPIRED);
510 if (np->n_lrev != np->n_brev ||
511 (np->n_flag & NQNFSNONCACHE)) {
512 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1))
516 np->n_brev = np->n_lrev;
521 * For normal NFS, reconcile changes made locally verses
522 * changes made remotely. Note that VOP_GETATTR only goes
523 * to the wire if the cached attribute has timed out or been
526 * If local modifications have been made clear the attribute
527 * cache to force an attribute and modified time check. If
528 * GETATTR detects that the file has been changed by someone
529 * other then us it will set NRMODIFIED.
531 * If we are opening a directory and local changes have been
532 * made we have to invalidate the cache in order to ensure
533 * that we get the most up-to-date information from the
536 if (np->n_flag & NLMODIFIED) {
538 if (vp->v_type == VDIR) {
539 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
545 error = VOP_GETATTR(vp, &vattr, ap->a_td);
548 if (np->n_flag & NRMODIFIED) {
549 if (vp->v_type == VDIR)
551 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
554 np->n_flag &= ~NRMODIFIED;
563 * What an NFS client should do upon close after writing is a debatable issue.
564 * Most NFS clients push delayed writes to the server upon close, basically for
566 * 1 - So that any write errors may be reported back to the client process
567 * doing the close system call. By far the two most likely errors are
568 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
569 * 2 - To put a worst case upper bound on cache inconsistency between
570 * multiple clients for the file.
571 * There is also a consistency problem for Version 2 of the protocol w.r.t.
572 * not being able to tell if other clients are writing a file concurrently,
573 * since there is no way of knowing if the changed modify time in the reply
574 * is only due to the write for this client.
575 * (NFS Version 3 provides weak cache consistency data in the reply that
576 * should be sufficient to detect and handle this case.)
578 * The current code does the following:
579 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
580 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
581 * or commit them (this satisfies 1 and 2 except for the
582 * case where the server crashes after this close but
583 * before the commit RPC, which is felt to be "good
584 * enough". Changing the last argument to nfs_flush() to
585 * a 1 would force a commit operation, if it is felt a
586 * commit is necessary now.
587 * for NQNFS - do nothing now, since 2 is dealt with via leases and
588 * 1 should be dealt with via an fsync() system call for
589 * cases where write errors are important.
591 * nfs_close(struct vnodeop_desc *a_desc, struct vnode *a_vp, int a_fflag,
592 * struct ucred *a_cred, struct thread *a_td)
596 nfs_close(struct vop_close_args *ap)
598 struct vnode *vp = ap->a_vp;
599 struct nfsnode *np = VTONFS(vp);
602 if (vp->v_type == VREG) {
603 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 &&
604 (np->n_flag & NLMODIFIED)) {
607 * Under NFSv3 we have dirty buffers to dispose of. We
608 * must flush them to the NFS server. We have the option
609 * of waiting all the way through the commit rpc or just
610 * waiting for the initial write. The default is to only
611 * wait through the initial write so the data is in the
612 * server's cache, which is roughly similar to the state
613 * a standard disk subsystem leaves the file in on close().
615 * We cannot clear the NLMODIFIED bit in np->n_flag due to
616 * potential races with other processes, and certainly
617 * cannot clear it if we don't commit.
619 int cm = nfsv3_commit_on_close ? 1 : 0;
620 error = nfs_flush(vp, MNT_WAIT, ap->a_td, cm);
621 /* np->n_flag &= ~NLMODIFIED; */
623 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
627 if (np->n_flag & NWRITEERR) {
628 np->n_flag &= ~NWRITEERR;
636 * nfs getattr call from vfs.
638 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred,
639 * struct thread *a_td)
642 nfs_getattr(struct vop_getattr_args *ap)
644 struct vnode *vp = ap->a_vp;
645 struct nfsnode *np = VTONFS(vp);
651 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
652 int v3 = NFS_ISV3(vp);
655 * Update local times for special files.
657 if (np->n_flag & (NACC | NUPD))
660 * First look in the cache.
662 if (nfs_getattrcache(vp, ap->a_vap) == 0)
665 if (v3 && nfsaccess_cache_timeout > 0) {
666 nfsstats.accesscache_misses++;
667 nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_td, nfs_vpcred(vp, ND_CHECK));
668 if (nfs_getattrcache(vp, ap->a_vap) == 0)
672 nfsstats.rpccnt[NFSPROC_GETATTR]++;
673 nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
675 nfsm_request(vp, NFSPROC_GETATTR, ap->a_td, nfs_vpcred(vp, ND_CHECK));
677 nfsm_loadattr(vp, ap->a_vap);
687 * nfs_setattr(struct vnodeop_desc *a_desc, struct vnode *a_vp,
688 * struct vattr *a_vap, struct ucred *a_cred,
689 * struct thread *a_td)
692 nfs_setattr(struct vop_setattr_args *ap)
694 struct vnode *vp = ap->a_vp;
695 struct nfsnode *np = VTONFS(vp);
696 struct vattr *vap = ap->a_vap;
705 * Setting of flags is not supported.
707 if (vap->va_flags != VNOVAL)
711 * Disallow write attempts if the filesystem is mounted read-only.
713 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
714 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
715 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
716 (vp->v_mount->mnt_flag & MNT_RDONLY))
718 if (vap->va_size != VNOVAL) {
719 switch (vp->v_type) {
726 if (vap->va_mtime.tv_sec == VNOVAL &&
727 vap->va_atime.tv_sec == VNOVAL &&
728 vap->va_mode == (mode_t)VNOVAL &&
729 vap->va_uid == (uid_t)VNOVAL &&
730 vap->va_gid == (gid_t)VNOVAL)
732 vap->va_size = VNOVAL;
736 * Disallow write attempts if the filesystem is
739 if (vp->v_mount->mnt_flag & MNT_RDONLY)
743 * This is nasty. The RPCs we send to flush pending
744 * data often return attribute information which is
745 * cached via a callback to nfs_loadattrcache(), which
746 * has the effect of changing our notion of the file
747 * size. Due to flushed appends and other operations
748 * the file size can be set to virtually anything,
749 * including values that do not match either the old
750 * or intended file size.
752 * When this condition is detected we must loop to
753 * try the operation again. Hopefully no more
754 * flushing is required on the loop so it works the
755 * second time around. THIS CASE ALMOST ALWAYS
760 error = nfs_meta_setsize(vp, ap->a_td, vap->va_size);
762 if (np->n_flag & NLMODIFIED) {
763 if (vap->va_size == 0)
764 error = nfs_vinvalbuf(vp, 0, ap->a_td, 1);
766 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
769 * note: this loop case almost always happens at
770 * least once per truncation.
772 if (error == 0 && np->n_size != vap->va_size)
774 np->n_vattr.va_size = vap->va_size;
777 } else if ((vap->va_mtime.tv_sec != VNOVAL ||
778 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NLMODIFIED) &&
779 vp->v_type == VREG &&
780 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) == EINTR
784 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
787 * Sanity check if a truncation was issued. This should only occur
788 * if multiple processes are racing on the same file.
790 if (error == 0 && vap->va_size != VNOVAL &&
791 np->n_size != vap->va_size) {
792 printf("NFS ftruncate: server disagrees on the file size: %lld/%lld/%lld\n", tsize, vap->va_size, np->n_size);
795 if (error && vap->va_size != VNOVAL) {
796 np->n_size = np->n_vattr.va_size = tsize;
797 vnode_pager_setsize(vp, np->n_size);
803 * Do an nfs setattr rpc.
806 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
807 struct ucred *cred, struct thread *td)
809 struct nfsv2_sattr *sp;
810 struct nfsnode *np = VTONFS(vp);
813 caddr_t bpos, dpos, cp2;
815 int error = 0, wccflag = NFSV3_WCCRATTR;
816 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
817 int v3 = NFS_ISV3(vp);
819 nfsstats.rpccnt[NFSPROC_SETATTR]++;
820 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
823 nfsm_v3attrbuild(vap, TRUE);
824 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
827 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
828 if (vap->va_mode == (mode_t)VNOVAL)
829 sp->sa_mode = nfs_xdrneg1;
831 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
832 if (vap->va_uid == (uid_t)VNOVAL)
833 sp->sa_uid = nfs_xdrneg1;
835 sp->sa_uid = txdr_unsigned(vap->va_uid);
836 if (vap->va_gid == (gid_t)VNOVAL)
837 sp->sa_gid = nfs_xdrneg1;
839 sp->sa_gid = txdr_unsigned(vap->va_gid);
840 sp->sa_size = txdr_unsigned(vap->va_size);
841 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
842 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
844 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
847 nfsm_wcc_data(vp, wccflag);
849 nfsm_loadattr(vp, (struct vattr *)0);
856 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
857 * nfs_lookup() until all remaining new api calls are implemented.
859 * Resolve a namecache entry. This function is passed a locked ncp and
860 * must call cache_setvp() on it as appropriate to resolve the entry.
863 nfs_nresolve(struct vop_nresolve_args *ap)
865 struct thread *td = curthread;
866 struct namecache *ncp;
877 /******NFSM MACROS********/
878 struct mbuf *mb, *mrep, *mreq, *mb2, *md;
879 caddr_t bpos, dpos, cp, cp2;
886 KKASSERT(ncp->nc_parent && ncp->nc_parent->nc_vp);
887 dvp = ncp->nc_parent->nc_vp;
888 if ((error = vget(dvp, LK_SHARED, td)) != 0)
893 nfsstats.lookupcache_misses++;
894 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
896 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
897 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
899 nfsm_strtom(ncp->nc_name, len, NFS_MAXNAMLEN);
900 nfsm_request(dvp, NFSPROC_LOOKUP, td, ap->a_cred);
903 * Cache negatve lookups to reduce NFS traffic, but use
904 * a fast timeout. Otherwise use a timeout of 1 tick.
905 * XXX we should add a namecache flag for no-caching
906 * to uncache the negative hit as soon as possible, but
907 * we cannot simply destroy the entry because it is used
908 * as a placeholder by the caller.
910 if (error == ENOENT) {
913 if (nfsneg_cache_timeout)
914 nticks = nfsneg_cache_timeout * hz;
917 cache_setvp(ncp, NULL);
918 cache_settimeout(ncp, nticks);
920 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
926 * Success, get the file handle, do various checks, and load
927 * post-operation data from the reply packet. Theoretically
928 * we should never be looking up "." so, theoretically, we
929 * should never get the same file handle as our directory. But
930 * we check anyway. XXX
932 * Note that no timeout is set for the positive cache hit. We
933 * assume, theoretically, that ESTALE returns will be dealt with
934 * properly to handle NFS races and in anycase we cannot depend
935 * on a timeout to deal with NFS open/create/excl issues so instead
936 * of a bad hack here the rest of the NFS client code needs to do
939 nfsm_getfh(fhp, fhsize, v3);
942 if (NFS_CMPFH(np, fhp, fhsize)) {
946 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
955 nfsm_postop_attr(nvp, attrflag, NFS_LATTR_NOSHRINK);
956 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
958 nfsm_loadattr(nvp, NULL);
960 cache_setvp(ncp, nvp);
974 * 'cached' nfs directory lookup
976 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
978 * nfs_lookup(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
979 * struct vnode **a_vpp, struct componentname *a_cnp)
982 nfs_lookup(struct vop_lookup_args *ap)
984 struct componentname *cnp = ap->a_cnp;
985 struct vnode *dvp = ap->a_dvp;
986 struct vnode **vpp = ap->a_vpp;
987 int flags = cnp->cn_flags;
992 struct nfsmount *nmp;
993 caddr_t bpos, dpos, cp2;
994 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
998 int lockparent, wantparent, error = 0, attrflag, fhsize;
999 int v3 = NFS_ISV3(dvp);
1000 struct thread *td = cnp->cn_td;
1003 * Read-only mount check and directory check.
1006 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1007 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
1010 if (dvp->v_type != VDIR)
1014 * Look it up in the cache. Note that ENOENT is only returned if we
1015 * previously entered a negative hit (see later on). The additional
1016 * nfsneg_cache_timeout check causes previously cached results to
1017 * be instantly ignored if the negative caching is turned off.
1019 lockparent = flags & CNP_LOCKPARENT;
1020 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1021 nmp = VFSTONFS(dvp->v_mount);
1029 nfsstats.lookupcache_misses++;
1030 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1031 len = cnp->cn_namelen;
1032 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
1033 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1034 nfsm_fhtom(dvp, v3);
1035 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1036 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
1038 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1042 nfsm_getfh(fhp, fhsize, v3);
1045 * Handle RENAME case...
1047 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1048 if (NFS_CMPFH(np, fhp, fhsize)) {
1052 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1059 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1060 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1062 nfsm_loadattr(newvp, (struct vattr *)0);
1066 VOP_UNLOCK(dvp, 0, td);
1067 cnp->cn_flags |= CNP_PDIRUNLOCK;
1072 if (flags & CNP_ISDOTDOT) {
1073 VOP_UNLOCK(dvp, 0, td);
1074 cnp->cn_flags |= CNP_PDIRUNLOCK;
1075 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1077 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
1078 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1079 return (error); /* NOTE: return error from nget */
1083 error = vn_lock(dvp, LK_EXCLUSIVE, td);
1088 cnp->cn_flags |= CNP_PDIRUNLOCK;
1090 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1094 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1100 VOP_UNLOCK(dvp, 0, td);
1101 cnp->cn_flags |= CNP_PDIRUNLOCK;
1106 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1107 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1109 nfsm_loadattr(newvp, (struct vattr *)0);
1111 /* XXX MOVE TO nfs_nremove() */
1112 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1113 cnp->cn_nameiop != NAMEI_DELETE) {
1114 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1121 if (newvp != NULLVP) {
1125 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1126 cnp->cn_nameiop == NAMEI_RENAME) &&
1129 VOP_UNLOCK(dvp, 0, td);
1130 cnp->cn_flags |= CNP_PDIRUNLOCK;
1132 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1135 error = EJUSTRETURN;
1143 * Just call nfs_bioread() to do the work.
1145 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1146 * struct ucred *a_cred)
1149 nfs_read(struct vop_read_args *ap)
1151 struct vnode *vp = ap->a_vp;
1153 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1154 switch (vp->v_type) {
1156 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1167 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1170 nfs_readlink(struct vop_readlink_args *ap)
1172 struct vnode *vp = ap->a_vp;
1174 if (vp->v_type != VLNK)
1176 return (nfs_bioread(vp, ap->a_uio, 0));
1180 * Do a readlink rpc.
1181 * Called by nfs_doio() from below the buffer cache.
1184 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1189 caddr_t bpos, dpos, cp2;
1190 int error = 0, len, attrflag;
1191 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1192 int v3 = NFS_ISV3(vp);
1194 nfsstats.rpccnt[NFSPROC_READLINK]++;
1195 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1197 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1199 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1201 nfsm_strsiz(len, NFS_MAXPATHLEN);
1202 if (len == NFS_MAXPATHLEN) {
1203 struct nfsnode *np = VTONFS(vp);
1204 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1207 nfsm_mtouio(uiop, len);
1219 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1224 caddr_t bpos, dpos, cp2;
1225 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1226 struct nfsmount *nmp;
1227 int error = 0, len, retlen, tsiz, eof, attrflag;
1228 int v3 = NFS_ISV3(vp);
1233 nmp = VFSTONFS(vp->v_mount);
1234 tsiz = uiop->uio_resid;
1235 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1238 nfsstats.rpccnt[NFSPROC_READ]++;
1239 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1240 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1242 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1244 txdr_hyper(uiop->uio_offset, tl);
1245 *(tl + 2) = txdr_unsigned(len);
1247 *tl++ = txdr_unsigned(uiop->uio_offset);
1248 *tl++ = txdr_unsigned(len);
1251 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1253 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1258 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1259 eof = fxdr_unsigned(int, *(tl + 1));
1261 nfsm_loadattr(vp, (struct vattr *)0);
1262 nfsm_strsiz(retlen, nmp->nm_rsize);
1263 nfsm_mtouio(uiop, retlen);
1267 if (eof || retlen == 0) {
1270 } else if (retlen < len) {
1282 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, int *must_commit)
1286 int32_t t1, t2, backup;
1287 caddr_t bpos, dpos, cp2;
1288 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1289 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1290 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1291 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1294 if (uiop->uio_iovcnt != 1)
1295 panic("nfs: writerpc iovcnt > 1");
1298 tsiz = uiop->uio_resid;
1299 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1302 nfsstats.rpccnt[NFSPROC_WRITE]++;
1303 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1304 nfsm_reqhead(vp, NFSPROC_WRITE,
1305 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1308 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1309 txdr_hyper(uiop->uio_offset, tl);
1311 *tl++ = txdr_unsigned(len);
1312 *tl++ = txdr_unsigned(*iomode);
1313 *tl = txdr_unsigned(len);
1317 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1318 /* Set both "begin" and "current" to non-garbage. */
1319 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1320 *tl++ = x; /* "begin offset" */
1321 *tl++ = x; /* "current offset" */
1322 x = txdr_unsigned(len);
1323 *tl++ = x; /* total to this offset */
1324 *tl = x; /* size of this write */
1326 nfsm_uiotom(uiop, len);
1327 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1330 * The write RPC returns a before and after mtime. The
1331 * nfsm_wcc_data() macro checks the before n_mtime
1332 * against the before time and stores the after time
1333 * in the nfsnode's cached vattr and n_mtime field.
1334 * The NRMODIFIED bit will be set if the before
1335 * time did not match the original mtime.
1337 wccflag = NFSV3_WCCCHK;
1338 nfsm_wcc_data(vp, wccflag);
1340 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1341 + NFSX_V3WRITEVERF);
1342 rlen = fxdr_unsigned(int, *tl++);
1347 } else if (rlen < len) {
1348 backup = len - rlen;
1349 uiop->uio_iov->iov_base -= backup;
1350 uiop->uio_iov->iov_len += backup;
1351 uiop->uio_offset -= backup;
1352 uiop->uio_resid += backup;
1355 commit = fxdr_unsigned(int, *tl++);
1358 * Return the lowest committment level
1359 * obtained by any of the RPCs.
1361 if (committed == NFSV3WRITE_FILESYNC)
1363 else if (committed == NFSV3WRITE_DATASYNC &&
1364 commit == NFSV3WRITE_UNSTABLE)
1366 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1367 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1369 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1370 } else if (bcmp((caddr_t)tl,
1371 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1373 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1378 nfsm_loadattr(vp, (struct vattr *)0);
1386 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1387 committed = NFSV3WRITE_FILESYNC;
1388 *iomode = committed;
1390 uiop->uio_resid = tsiz;
1396 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1397 * mode set to specify the file type and the size field for rdev.
1400 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1403 struct nfsv2_sattr *sp;
1407 struct vnode *newvp = (struct vnode *)0;
1408 struct nfsnode *np = (struct nfsnode *)0;
1412 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1413 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1415 int v3 = NFS_ISV3(dvp);
1417 if (vap->va_type == VCHR || vap->va_type == VBLK)
1418 rdev = txdr_unsigned(vap->va_rdev);
1419 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1422 return (EOPNOTSUPP);
1424 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1427 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1428 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1429 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1430 nfsm_fhtom(dvp, v3);
1431 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1433 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1434 *tl++ = vtonfsv3_type(vap->va_type);
1435 nfsm_v3attrbuild(vap, FALSE);
1436 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1437 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1438 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1439 *tl = txdr_unsigned(uminor(vap->va_rdev));
1442 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1443 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1444 sp->sa_uid = nfs_xdrneg1;
1445 sp->sa_gid = nfs_xdrneg1;
1447 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1448 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1450 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1452 nfsm_mtofh(dvp, newvp, v3, gotvp);
1456 newvp = (struct vnode *)0;
1458 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1459 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1465 nfsm_wcc_data(dvp, wccflag);
1474 VTONFS(dvp)->n_flag |= NLMODIFIED;
1476 VTONFS(dvp)->n_attrstamp = 0;
1482 * just call nfs_mknodrpc() to do the work.
1484 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1485 * struct componentname *a_cnp, struct vattr *a_vap)
1489 nfs_mknod(struct vop_mknod_args *ap)
1491 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1494 static u_long create_verf;
1496 * nfs file create call
1498 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1499 * struct componentname *a_cnp, struct vattr *a_vap)
1502 nfs_create(struct vop_create_args *ap)
1504 struct vnode *dvp = ap->a_dvp;
1505 struct vattr *vap = ap->a_vap;
1506 struct componentname *cnp = ap->a_cnp;
1507 struct nfsv2_sattr *sp;
1511 struct nfsnode *np = (struct nfsnode *)0;
1512 struct vnode *newvp = (struct vnode *)0;
1513 caddr_t bpos, dpos, cp2;
1514 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1515 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1517 int v3 = NFS_ISV3(dvp);
1520 * Oops, not for me..
1522 if (vap->va_type == VSOCK)
1523 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1525 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1528 if (vap->va_vaflags & VA_EXCLUSIVE)
1531 nfsstats.rpccnt[NFSPROC_CREATE]++;
1532 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1533 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1534 nfsm_fhtom(dvp, v3);
1535 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1537 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1538 if (fmode & O_EXCL) {
1539 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1540 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1542 if (!TAILQ_EMPTY(&in_ifaddrhead))
1543 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr;
1546 *tl++ = create_verf;
1547 *tl = ++create_verf;
1549 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1550 nfsm_v3attrbuild(vap, FALSE);
1553 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1554 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1555 sp->sa_uid = nfs_xdrneg1;
1556 sp->sa_gid = nfs_xdrneg1;
1558 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1559 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1561 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1563 nfsm_mtofh(dvp, newvp, v3, gotvp);
1567 newvp = (struct vnode *)0;
1569 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1570 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1576 nfsm_wcc_data(dvp, wccflag);
1580 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1586 } else if (v3 && (fmode & O_EXCL)) {
1588 * We are normally called with only a partially initialized
1589 * VAP. Since the NFSv3 spec says that server may use the
1590 * file attributes to store the verifier, the spec requires
1591 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1592 * in atime, but we can't really assume that all servers will
1593 * so we ensure that our SETATTR sets both atime and mtime.
1595 if (vap->va_mtime.tv_sec == VNOVAL)
1596 vfs_timestamp(&vap->va_mtime);
1597 if (vap->va_atime.tv_sec == VNOVAL)
1598 vap->va_atime = vap->va_mtime;
1599 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1603 * The new np may have enough info for access
1604 * checks, make sure rucred and wucred are
1605 * initialized for read and write rpc's.
1608 if (np->n_rucred == NULL)
1609 np->n_rucred = crhold(cnp->cn_cred);
1610 if (np->n_wucred == NULL)
1611 np->n_wucred = crhold(cnp->cn_cred);
1614 VTONFS(dvp)->n_flag |= NLMODIFIED;
1616 VTONFS(dvp)->n_attrstamp = 0;
1621 * nfs file remove call
1622 * To try and make nfs semantics closer to ufs semantics, a file that has
1623 * other processes using the vnode is renamed instead of removed and then
1624 * removed later on the last close.
1625 * - If v_usecount > 1
1626 * If a rename is not already in the works
1627 * call nfs_sillyrename() to set it up
1631 * nfs_remove(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
1632 * struct vnode *a_vp, struct componentname *a_cnp)
1635 nfs_remove(struct vop_remove_args *ap)
1637 struct vnode *vp = ap->a_vp;
1638 struct vnode *dvp = ap->a_dvp;
1639 struct componentname *cnp = ap->a_cnp;
1640 struct nfsnode *np = VTONFS(vp);
1645 if (vp->v_usecount < 1)
1646 panic("nfs_remove: bad v_usecount");
1648 if (vp->v_type == VDIR)
1650 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1651 VOP_GETATTR(vp, &vattr, cnp->cn_td) == 0 &&
1652 vattr.va_nlink > 1)) {
1654 * throw away biocache buffers, mainly to avoid
1655 * unnecessary delayed writes later.
1657 error = nfs_vinvalbuf(vp, 0, cnp->cn_td, 1);
1660 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1661 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1663 * Kludge City: If the first reply to the remove rpc is lost..
1664 * the reply to the retransmitted request will be ENOENT
1665 * since the file was in fact removed
1666 * Therefore, we cheat and return success.
1668 if (error == ENOENT)
1670 } else if (!np->n_sillyrename) {
1671 error = nfs_sillyrename(dvp, vp, cnp);
1673 np->n_attrstamp = 0;
1678 * nfs file remove rpc called from nfs_inactive
1681 nfs_removeit(struct sillyrename *sp)
1683 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1688 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1691 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1692 struct ucred *cred, struct thread *td)
1697 caddr_t bpos, dpos, cp2;
1698 int error = 0, wccflag = NFSV3_WCCRATTR;
1699 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1700 int v3 = NFS_ISV3(dvp);
1702 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1703 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1704 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1705 nfsm_fhtom(dvp, v3);
1706 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1707 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1709 nfsm_wcc_data(dvp, wccflag);
1712 VTONFS(dvp)->n_flag |= NLMODIFIED;
1714 VTONFS(dvp)->n_attrstamp = 0;
1719 * nfs file rename call
1721 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1722 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1723 * struct vnode *a_tvp, struct componentname *a_tcnp)
1726 nfs_rename(struct vop_rename_args *ap)
1728 struct vnode *fvp = ap->a_fvp;
1729 struct vnode *tvp = ap->a_tvp;
1730 struct vnode *fdvp = ap->a_fdvp;
1731 struct vnode *tdvp = ap->a_tdvp;
1732 struct componentname *tcnp = ap->a_tcnp;
1733 struct componentname *fcnp = ap->a_fcnp;
1736 /* Check for cross-device rename */
1737 if ((fvp->v_mount != tdvp->v_mount) ||
1738 (tvp && (fvp->v_mount != tvp->v_mount))) {
1744 * We have to flush B_DELWRI data prior to renaming
1745 * the file. If we don't, the delayed-write buffers
1746 * can be flushed out later after the file has gone stale
1747 * under NFSV3. NFSV2 does not have this problem because
1748 * ( as far as I can tell ) it flushes dirty buffers more
1752 VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_td);
1754 VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_td);
1757 * If the tvp exists and is in use, sillyrename it before doing the
1758 * rename of the new file over it.
1760 * XXX Can't sillyrename a directory.
1762 * We do not attempt to do any namecache purges in this old API
1763 * routine. The new API compat functions have access to the actual
1764 * namecache structures and will do it for us.
1766 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1767 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1774 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1775 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1788 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1790 if (error == ENOENT)
1796 * nfs file rename rpc called from nfs_remove() above
1799 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1800 struct sillyrename *sp)
1802 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1803 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1807 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1810 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1811 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1812 struct ucred *cred, struct thread *td)
1817 caddr_t bpos, dpos, cp2;
1818 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1819 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1820 int v3 = NFS_ISV3(fdvp);
1822 nfsstats.rpccnt[NFSPROC_RENAME]++;
1823 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1824 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1825 nfsm_rndup(tnamelen));
1826 nfsm_fhtom(fdvp, v3);
1827 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1828 nfsm_fhtom(tdvp, v3);
1829 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1830 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1832 nfsm_wcc_data(fdvp, fwccflag);
1833 nfsm_wcc_data(tdvp, twccflag);
1837 VTONFS(fdvp)->n_flag |= NLMODIFIED;
1838 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1840 VTONFS(fdvp)->n_attrstamp = 0;
1842 VTONFS(tdvp)->n_attrstamp = 0;
1847 * nfs hard link create call
1849 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1850 * struct componentname *a_cnp)
1853 nfs_link(struct vop_link_args *ap)
1855 struct vnode *vp = ap->a_vp;
1856 struct vnode *tdvp = ap->a_tdvp;
1857 struct componentname *cnp = ap->a_cnp;
1861 caddr_t bpos, dpos, cp2;
1862 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1863 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1866 if (vp->v_mount != tdvp->v_mount) {
1871 * Push all writes to the server, so that the attribute cache
1872 * doesn't get "out of sync" with the server.
1873 * XXX There should be a better way!
1875 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_td);
1878 nfsstats.rpccnt[NFSPROC_LINK]++;
1879 nfsm_reqhead(vp, NFSPROC_LINK,
1880 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1882 nfsm_fhtom(tdvp, v3);
1883 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1884 nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1886 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1887 nfsm_wcc_data(tdvp, wccflag);
1891 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1893 VTONFS(vp)->n_attrstamp = 0;
1895 VTONFS(tdvp)->n_attrstamp = 0;
1897 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1899 if (error == EEXIST)
1905 * nfs symbolic link create call
1907 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1908 * struct componentname *a_cnp, struct vattr *a_vap,
1912 nfs_symlink(struct vop_symlink_args *ap)
1914 struct vnode *dvp = ap->a_dvp;
1915 struct vattr *vap = ap->a_vap;
1916 struct componentname *cnp = ap->a_cnp;
1917 struct nfsv2_sattr *sp;
1921 caddr_t bpos, dpos, cp2;
1922 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1923 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1924 struct vnode *newvp = (struct vnode *)0;
1925 int v3 = NFS_ISV3(dvp);
1927 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1928 slen = strlen(ap->a_target);
1929 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1930 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1931 nfsm_fhtom(dvp, v3);
1932 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1934 nfsm_v3attrbuild(vap, FALSE);
1936 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1938 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1939 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1940 sp->sa_uid = nfs_xdrneg1;
1941 sp->sa_gid = nfs_xdrneg1;
1942 sp->sa_size = nfs_xdrneg1;
1943 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1944 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1948 * Issue the NFS request and get the rpc response.
1950 * Only NFSv3 responses returning an error of 0 actually return
1951 * a file handle that can be converted into newvp without having
1952 * to do an extra lookup rpc.
1954 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1957 nfsm_mtofh(dvp, newvp, v3, gotvp);
1958 nfsm_wcc_data(dvp, wccflag);
1962 * out code jumps -> here, mrep is also freed.
1969 * If we get an EEXIST error, silently convert it to no-error
1970 * in case of an NFS retry.
1972 if (error == EEXIST)
1976 * If we do not have (or no longer have) an error, and we could
1977 * not extract the newvp from the response due to the request being
1978 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1979 * to obtain a newvp to return.
1981 if (error == 0 && newvp == NULL) {
1982 struct nfsnode *np = NULL;
1984 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1985 cnp->cn_cred, cnp->cn_td, &np);
1995 VTONFS(dvp)->n_flag |= NLMODIFIED;
1997 VTONFS(dvp)->n_attrstamp = 0;
2004 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2005 * struct componentname *a_cnp, struct vattr *a_vap)
2008 nfs_mkdir(struct vop_mkdir_args *ap)
2010 struct vnode *dvp = ap->a_dvp;
2011 struct vattr *vap = ap->a_vap;
2012 struct componentname *cnp = ap->a_cnp;
2013 struct nfsv2_sattr *sp;
2018 struct nfsnode *np = (struct nfsnode *)0;
2019 struct vnode *newvp = (struct vnode *)0;
2020 caddr_t bpos, dpos, cp2;
2021 int error = 0, wccflag = NFSV3_WCCRATTR;
2023 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2025 int v3 = NFS_ISV3(dvp);
2027 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
2030 len = cnp->cn_namelen;
2031 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2032 nfsm_reqhead(dvp, NFSPROC_MKDIR,
2033 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2034 nfsm_fhtom(dvp, v3);
2035 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
2037 nfsm_v3attrbuild(vap, FALSE);
2039 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2040 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2041 sp->sa_uid = nfs_xdrneg1;
2042 sp->sa_gid = nfs_xdrneg1;
2043 sp->sa_size = nfs_xdrneg1;
2044 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2045 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2047 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
2049 nfsm_mtofh(dvp, newvp, v3, gotvp);
2051 nfsm_wcc_data(dvp, wccflag);
2054 VTONFS(dvp)->n_flag |= NLMODIFIED;
2056 VTONFS(dvp)->n_attrstamp = 0;
2058 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2059 * if we can succeed in looking up the directory.
2061 if (error == EEXIST || (!error && !gotvp)) {
2064 newvp = (struct vnode *)0;
2066 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2070 if (newvp->v_type != VDIR)
2083 * nfs remove directory call
2085 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2086 * struct componentname *a_cnp)
2089 nfs_rmdir(struct vop_rmdir_args *ap)
2091 struct vnode *vp = ap->a_vp;
2092 struct vnode *dvp = ap->a_dvp;
2093 struct componentname *cnp = ap->a_cnp;
2097 caddr_t bpos, dpos, cp2;
2098 int error = 0, wccflag = NFSV3_WCCRATTR;
2099 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2100 int v3 = NFS_ISV3(dvp);
2104 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2105 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2106 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2107 nfsm_fhtom(dvp, v3);
2108 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2109 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2111 nfsm_wcc_data(dvp, wccflag);
2114 VTONFS(dvp)->n_flag |= NLMODIFIED;
2116 VTONFS(dvp)->n_attrstamp = 0;
2118 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2120 if (error == ENOENT)
2128 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2131 nfs_readdir(struct vop_readdir_args *ap)
2133 struct vnode *vp = ap->a_vp;
2134 struct nfsnode *np = VTONFS(vp);
2135 struct uio *uio = ap->a_uio;
2139 if (vp->v_type != VDIR)
2143 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2144 * and then check that is still valid, or if this is an NQNFS mount
2145 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2146 * VOP_GETATTR() does not necessarily go to the wire.
2148 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2149 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2150 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
2151 if (NQNFS_CKCACHABLE(vp, ND_READ)) {
2152 nfsstats.direofcache_hits++;
2155 } else if (VOP_GETATTR(vp, &vattr, uio->uio_td) == 0 &&
2156 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2158 nfsstats.direofcache_hits++;
2164 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2165 * own cache coherency checks so we do not have to.
2167 tresid = uio->uio_resid;
2168 error = nfs_bioread(vp, uio, 0);
2170 if (!error && uio->uio_resid == tresid)
2171 nfsstats.direofcache_misses++;
2177 * Called from below the buffer cache by nfs_doio().
2180 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2183 struct nfs_dirent *dp = NULL;
2188 caddr_t bpos, dpos, cp2;
2189 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2191 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2192 struct nfsnode *dnp = VTONFS(vp);
2194 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2196 int v3 = NFS_ISV3(vp);
2199 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2200 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2201 panic("nfs readdirrpc bad uio");
2205 * If there is no cookie, assume directory was stale.
2207 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2211 return (NFSERR_BAD_COOKIE);
2213 * Loop around doing readdir rpc's of size nm_readdirsize
2214 * truncated to a multiple of DIRBLKSIZ.
2215 * The stopping criteria is EOF or buffer full.
2217 while (more_dirs && bigenough) {
2218 nfsstats.rpccnt[NFSPROC_READDIR]++;
2219 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2223 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2224 *tl++ = cookie.nfsuquad[0];
2225 *tl++ = cookie.nfsuquad[1];
2226 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2227 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2229 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2230 *tl++ = cookie.nfsuquad[0];
2232 *tl = txdr_unsigned(nmp->nm_readdirsize);
2233 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2235 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2237 nfsm_dissect(tl, u_int32_t *,
2239 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2240 dnp->n_cookieverf.nfsuquad[1] = *tl;
2246 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2247 more_dirs = fxdr_unsigned(int, *tl);
2249 /* loop thru the dir entries, doctoring them to 4bsd form */
2250 while (more_dirs && bigenough) {
2252 nfsm_dissect(tl, u_int32_t *,
2254 fileno = fxdr_hyper(tl);
2255 len = fxdr_unsigned(int, *(tl + 2));
2257 nfsm_dissect(tl, u_int32_t *,
2259 fileno = fxdr_unsigned(u_quad_t, *tl++);
2260 len = fxdr_unsigned(int, *tl);
2262 if (len <= 0 || len > NFS_MAXNAMLEN) {
2267 tlen = nfsm_rndup(len);
2269 tlen += 4; /* To ensure null termination */
2270 left = DIRBLKSIZ - blksiz;
2271 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2272 dp->nfs_reclen += left;
2273 uiop->uio_iov->iov_base += left;
2274 uiop->uio_iov->iov_len -= left;
2275 uiop->uio_offset += left;
2276 uiop->uio_resid -= left;
2279 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2282 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2283 dp->nfs_ino = fileno;
2284 dp->nfs_namlen = len;
2285 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2286 dp->nfs_type = DT_UNKNOWN;
2287 blksiz += dp->nfs_reclen;
2288 if (blksiz == DIRBLKSIZ)
2290 uiop->uio_offset += sizeof(struct nfs_dirent);
2291 uiop->uio_resid -= sizeof(struct nfs_dirent);
2292 uiop->uio_iov->iov_base += sizeof(struct nfs_dirent);
2293 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2294 nfsm_mtouio(uiop, len);
2295 cp = uiop->uio_iov->iov_base;
2297 *cp = '\0'; /* null terminate */
2298 uiop->uio_iov->iov_base += tlen;
2299 uiop->uio_iov->iov_len -= tlen;
2300 uiop->uio_offset += tlen;
2301 uiop->uio_resid -= tlen;
2303 nfsm_adv(nfsm_rndup(len));
2305 nfsm_dissect(tl, u_int32_t *,
2308 nfsm_dissect(tl, u_int32_t *,
2312 cookie.nfsuquad[0] = *tl++;
2314 cookie.nfsuquad[1] = *tl++;
2319 more_dirs = fxdr_unsigned(int, *tl);
2322 * If at end of rpc data, get the eof boolean
2325 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2326 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2331 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2332 * by increasing d_reclen for the last record.
2335 left = DIRBLKSIZ - blksiz;
2336 dp->nfs_reclen += left;
2337 uiop->uio_iov->iov_base += left;
2338 uiop->uio_iov->iov_len -= left;
2339 uiop->uio_offset += left;
2340 uiop->uio_resid -= left;
2344 * We are now either at the end of the directory or have filled the
2348 dnp->n_direofoffset = uiop->uio_offset;
2350 if (uiop->uio_resid > 0)
2351 printf("EEK! readdirrpc resid > 0\n");
2352 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2360 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2363 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2366 struct nfs_dirent *dp;
2370 struct vnode *newvp;
2372 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2373 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2375 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2376 struct nfsnode *dnp = VTONFS(vp), *np;
2379 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2380 int attrflag, fhsize;
2381 struct namecache *ncp;
2382 struct namecache *dncp;
2383 struct nlcomponent nlc;
2389 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2390 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2391 panic("nfs readdirplusrpc bad uio");
2394 * Obtain the namecache record for the directory so we have something
2395 * to use as a basis for creating the entries. This function will
2396 * return a held (but not locked) ncp. The ncp may be disconnected
2397 * from the tree and cannot be used for upward traversals, and the
2398 * ncp may be unnamed. Note that other unrelated operations may
2399 * cause the ncp to be named at any time.
2401 dncp = cache_fromdvp(vp, NULL, 0);
2402 bzero(&nlc, sizeof(nlc));
2406 * If there is no cookie, assume directory was stale.
2408 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2412 return (NFSERR_BAD_COOKIE);
2414 * Loop around doing readdir rpc's of size nm_readdirsize
2415 * truncated to a multiple of DIRBLKSIZ.
2416 * The stopping criteria is EOF or buffer full.
2418 while (more_dirs && bigenough) {
2419 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2420 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2421 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2423 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2424 *tl++ = cookie.nfsuquad[0];
2425 *tl++ = cookie.nfsuquad[1];
2426 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2427 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2428 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2429 *tl = txdr_unsigned(nmp->nm_rsize);
2430 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2431 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2436 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2437 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2438 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2439 more_dirs = fxdr_unsigned(int, *tl);
2441 /* loop thru the dir entries, doctoring them to 4bsd form */
2442 while (more_dirs && bigenough) {
2443 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2444 fileno = fxdr_hyper(tl);
2445 len = fxdr_unsigned(int, *(tl + 2));
2446 if (len <= 0 || len > NFS_MAXNAMLEN) {
2451 tlen = nfsm_rndup(len);
2453 tlen += 4; /* To ensure null termination*/
2454 left = DIRBLKSIZ - blksiz;
2455 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2456 dp->nfs_reclen += left;
2457 uiop->uio_iov->iov_base += left;
2458 uiop->uio_iov->iov_len -= left;
2459 uiop->uio_offset += left;
2460 uiop->uio_resid -= left;
2463 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2466 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2467 dp->nfs_ino = fileno;
2468 dp->nfs_namlen = len;
2469 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2470 dp->nfs_type = DT_UNKNOWN;
2471 blksiz += dp->nfs_reclen;
2472 if (blksiz == DIRBLKSIZ)
2474 uiop->uio_offset += sizeof(struct nfs_dirent);
2475 uiop->uio_resid -= sizeof(struct nfs_dirent);
2476 uiop->uio_iov->iov_base += sizeof(struct nfs_dirent);
2477 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2478 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2479 nlc.nlc_namelen = len;
2480 nfsm_mtouio(uiop, len);
2481 cp = uiop->uio_iov->iov_base;
2484 uiop->uio_iov->iov_base += tlen;
2485 uiop->uio_iov->iov_len -= tlen;
2486 uiop->uio_offset += tlen;
2487 uiop->uio_resid -= tlen;
2489 nfsm_adv(nfsm_rndup(len));
2490 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2492 cookie.nfsuquad[0] = *tl++;
2493 cookie.nfsuquad[1] = *tl++;
2498 * Since the attributes are before the file handle
2499 * (sigh), we must skip over the attributes and then
2500 * come back and get them.
2502 attrflag = fxdr_unsigned(int, *tl);
2506 nfsm_adv(NFSX_V3FATTR);
2507 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2508 doit = fxdr_unsigned(int, *tl);
2510 nfsm_getfh(fhp, fhsize, 1);
2511 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2516 error = nfs_nget(vp->v_mount, fhp,
2524 if (doit && bigenough) {
2529 nfsm_loadattr(newvp, (struct vattr *)0);
2533 IFTODT(VTTOIF(np->n_vattr.va_type));
2535 printf("NFS/READDIRPLUS, ENTER %*.*s\n",
2536 nlc.nlc_namelen, nlc.nlc_namelen,
2538 ncp = cache_nlookup(dncp, &nlc);
2539 cache_setunresolved(ncp);
2540 cache_setvp(ncp, newvp);
2543 printf("NFS/READDIRPLUS, UNABLE TO ENTER"
2545 nlc.nlc_namelen, nlc.nlc_namelen,
2550 /* Just skip over the file handle */
2551 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2552 i = fxdr_unsigned(int, *tl);
2553 nfsm_adv(nfsm_rndup(i));
2555 if (newvp != NULLVP) {
2562 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2563 more_dirs = fxdr_unsigned(int, *tl);
2566 * If at end of rpc data, get the eof boolean
2569 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2570 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2575 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2576 * by increasing d_reclen for the last record.
2579 left = DIRBLKSIZ - blksiz;
2580 dp->nfs_reclen += left;
2581 uiop->uio_iov->iov_base += left;
2582 uiop->uio_iov->iov_len -= left;
2583 uiop->uio_offset += left;
2584 uiop->uio_resid -= left;
2588 * We are now either at the end of the directory or have filled the
2592 dnp->n_direofoffset = uiop->uio_offset;
2594 if (uiop->uio_resid > 0)
2595 printf("EEK! readdirplusrpc resid > 0\n");
2596 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2600 if (newvp != NULLVP) {
2613 * Silly rename. To make the NFS filesystem that is stateless look a little
2614 * more like the "ufs" a remove of an active vnode is translated to a rename
2615 * to a funny looking filename that is removed by nfs_inactive on the
2616 * nfsnode. There is the potential for another process on a different client
2617 * to create the same funny name between the nfs_lookitup() fails and the
2618 * nfs_rename() completes, but...
2621 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2623 struct sillyrename *sp;
2628 * We previously purged dvp instead of vp. I don't know why, it
2629 * completely destroys performance. We can't do it anyway with the
2630 * new VFS API since we would be breaking the namecache topology.
2632 cache_purge(vp); /* XXX */
2635 if (vp->v_type == VDIR)
2636 panic("nfs: sillyrename dir");
2638 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2639 M_NFSREQ, M_WAITOK);
2640 sp->s_cred = crdup(cnp->cn_cred);
2644 /* Fudge together a funny name */
2645 sp->s_namlen = sprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
2647 /* Try lookitups until we get one that isn't there */
2648 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2649 cnp->cn_td, (struct nfsnode **)0) == 0) {
2651 if (sp->s_name[4] > 'z') {
2656 error = nfs_renameit(dvp, cnp, sp);
2659 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2661 np->n_sillyrename = sp;
2666 free((caddr_t)sp, M_NFSREQ);
2671 * Look up a file name and optionally either update the file handle or
2672 * allocate an nfsnode, depending on the value of npp.
2673 * npp == NULL --> just do the lookup
2674 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2676 * *npp != NULL --> update the file handle in the vnode
2679 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2680 struct thread *td, struct nfsnode **npp)
2685 struct vnode *newvp = (struct vnode *)0;
2686 struct nfsnode *np, *dnp = VTONFS(dvp);
2687 caddr_t bpos, dpos, cp2;
2688 int error = 0, fhlen, attrflag;
2689 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2691 int v3 = NFS_ISV3(dvp);
2693 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2694 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2695 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2696 nfsm_fhtom(dvp, v3);
2697 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2698 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2699 if (npp && !error) {
2700 nfsm_getfh(nfhp, fhlen, v3);
2703 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2704 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2705 np->n_fhp = &np->n_fh;
2706 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2707 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2708 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2709 np->n_fhsize = fhlen;
2711 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2715 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2723 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
2724 if (!attrflag && *npp == NULL) {
2733 nfsm_loadattr(newvp, (struct vattr *)0);
2737 if (npp && *npp == NULL) {
2752 * Nfs Version 3 commit rpc
2755 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2760 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2761 caddr_t bpos, dpos, cp2;
2762 int error = 0, wccflag = NFSV3_WCCRATTR;
2763 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2765 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2767 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2768 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2770 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2771 txdr_hyper(offset, tl);
2773 *tl = txdr_unsigned(cnt);
2774 nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2775 nfsm_wcc_data(vp, wccflag);
2777 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2778 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2779 NFSX_V3WRITEVERF)) {
2780 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2782 error = NFSERR_STALEWRITEVERF;
2792 * - make nfs_bmap() essentially a no-op that does no translation
2793 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2794 * (Maybe I could use the process's page mapping, but I was concerned that
2795 * Kernel Write might not be enabled and also figured copyout() would do
2796 * a lot more work than bcopy() and also it currently happens in the
2797 * context of the swapper process (2).
2799 * nfs_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
2800 * daddr_t *a_bnp, int *a_runp, int *a_runb)
2803 nfs_bmap(struct vop_bmap_args *ap)
2805 struct vnode *vp = ap->a_vp;
2807 if (ap->a_vpp != NULL)
2809 if (ap->a_bnp != NULL)
2810 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2811 if (ap->a_runp != NULL)
2813 if (ap->a_runb != NULL)
2820 * For async requests when nfsiod(s) are running, queue the request by
2821 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2825 nfs_strategy(struct vop_strategy_args *ap)
2827 struct buf *bp = ap->a_bp;
2831 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2832 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2834 if (bp->b_flags & B_PHYS)
2835 panic("nfs physio");
2837 if (bp->b_flags & B_ASYNC)
2840 td = curthread; /* XXX */
2843 * If the op is asynchronous and an i/o daemon is waiting
2844 * queue the request, wake it up and wait for completion
2845 * otherwise just do it ourselves.
2847 if ((bp->b_flags & B_ASYNC) == 0 ||
2848 nfs_asyncio(bp, td))
2849 error = nfs_doio(bp, td);
2856 * NB Currently unsupported.
2858 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
2859 * struct thread *a_td)
2863 nfs_mmap(struct vop_mmap_args *ap)
2869 * fsync vnode op. Just call nfs_flush() with commit == 1.
2871 * nfs_fsync(struct vnodeop_desc *a_desc, struct vnode *a_vp,
2872 * struct ucred * a_cred, int a_waitfor, struct thread *a_td)
2876 nfs_fsync(struct vop_fsync_args *ap)
2878 return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2882 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
2883 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
2884 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
2885 * set the buffer contains data that has already been written to the server
2886 * and which now needs a commit RPC.
2888 * If commit is 0 we only take one pass and only flush buffers containing new
2891 * If commit is 1 we take two passes, issuing a commit RPC in the second
2894 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
2895 * to completely flush all pending data.
2897 * Note that the RB_SCAN code properly handles the case where the
2898 * callback might block and directly or indirectly (another thread) cause
2899 * the RB tree to change.
2902 #ifndef NFS_COMMITBVECSIZ
2903 #define NFS_COMMITBVECSIZ 16
2906 struct nfs_flush_info {
2907 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
2914 struct buf *bvary[NFS_COMMITBVECSIZ];
2920 static int nfs_flush_bp(struct buf *bp, void *data);
2921 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
2924 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
2926 struct nfsnode *np = VTONFS(vp);
2927 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2928 struct nfs_flush_info info;
2931 bzero(&info, sizeof(info));
2934 info.waitfor = waitfor;
2935 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
2942 info.mode = NFI_FLUSHNEW;
2943 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2944 nfs_flush_bp, &info);
2947 * Take a second pass if committing and no error occured.
2948 * Clean up any left over collection (whether an error
2951 if (commit && error == 0) {
2952 info.mode = NFI_COMMIT;
2953 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2954 nfs_flush_bp, &info);
2956 error = nfs_flush_docommit(&info, error);
2960 * Wait for pending I/O to complete before checking whether
2961 * any further dirty buffers exist.
2963 while (waitfor == MNT_WAIT && vp->v_numoutput) {
2964 vp->v_flag |= VBWAIT;
2965 error = tsleep((caddr_t)&vp->v_numoutput,
2966 info.slpflag, "nfsfsync", info.slptimeo);
2969 * We have to be able to break out if this
2970 * is an 'intr' mount.
2972 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
2978 * Since we do not process pending signals,
2979 * once we get a PCATCH our tsleep() will no
2980 * longer sleep, switch to a fixed timeout
2983 if (info.slpflag == PCATCH) {
2985 info.slptimeo = 2 * hz;
2992 * Loop if we are flushing synchronous as well as committing,
2993 * and dirty buffers are still present. Otherwise we might livelock.
2995 } while (waitfor == MNT_WAIT && commit &&
2996 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
2999 * The callbacks have to return a negative error to terminate the
3006 * Deal with any error collection
3008 if (np->n_flag & NWRITEERR) {
3009 error = np->n_error;
3010 np->n_flag &= ~NWRITEERR;
3018 nfs_flush_bp(struct buf *bp, void *data)
3020 struct nfs_flush_info *info = data;
3025 switch(info->mode) {
3028 if (info->loops && info->waitfor == MNT_WAIT) {
3029 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3031 error = BUF_TIMELOCK(bp,
3032 LK_EXCLUSIVE | LK_SLEEPFAIL,
3034 info->slpflag, info->slptimeo);
3037 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3040 if ((bp->b_flags & B_DELWRI) == 0)
3041 panic("nfs_fsync: not dirty");
3042 if (bp->b_flags & B_NEEDCOMMIT) {
3049 bp->b_flags |= B_ASYNC;
3051 VOP_BWRITE(bp->b_vp, bp);
3059 * Only process buffers in need of a commit which we can
3060 * immediately lock. This may prevent a buffer from being
3061 * committed, but the normal flush loop will block on the
3062 * same buffer so we shouldn't get into an endless loop.
3065 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3066 (B_DELWRI | B_NEEDCOMMIT) ||
3067 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
3075 * NOTE: we are not clearing B_DONE here, so we have
3076 * to do it later on in this routine if we intend to
3077 * initiate I/O on the bp.
3079 * Note: to avoid loopback deadlocks, we do not
3080 * assign b_runningbufspace.
3082 vfs_busy_pages(bp, 1);
3084 info->bvary[info->bvsize] = bp;
3085 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3087 if (info->bvsize == 0 || toff < info->beg_off)
3088 info->beg_off = toff;
3089 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
3090 if (info->bvsize == 0 || toff > info->end_off)
3091 info->end_off = toff;
3093 if (info->bvsize == NFS_COMMITBVECSIZ) {
3094 error = nfs_flush_docommit(info, 0);
3095 KKASSERT(info->bvsize == 0);
3104 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3114 if (info->bvsize > 0) {
3116 * Commit data on the server, as required. Note that
3117 * nfs_commit will use the vnode's cred for the commit.
3118 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3120 bytes = info->end_off - info->beg_off;
3121 if (bytes > 0x40000000)
3126 retv = nfs_commit(vp, info->beg_off,
3127 (int)bytes, info->td);
3128 if (retv == NFSERR_STALEWRITEVERF)
3129 nfs_clearcommit(vp->v_mount);
3133 * Now, either mark the blocks I/O done or mark the
3134 * blocks dirty, depending on whether the commit
3137 for (i = 0; i < info->bvsize; ++i) {
3138 bp = info->bvary[i];
3139 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3142 * Error, leave B_DELWRI intact
3144 vfs_unbusy_pages(bp);
3148 * Success, remove B_DELWRI ( bundirty() ).
3150 * b_dirtyoff/b_dirtyend seem to be NFS
3151 * specific. We should probably move that
3152 * into bundirty(). XXX
3156 bp->b_flags |= B_ASYNC;
3158 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3159 bp->b_dirtyoff = bp->b_dirtyend = 0;
3170 * NFS advisory byte-level locks.
3171 * Currently unsupported.
3173 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3177 nfs_advlock(struct vop_advlock_args *ap)
3179 struct nfsnode *np = VTONFS(ap->a_vp);
3182 * The following kludge is to allow diskless support to work
3183 * until a real NFS lockd is implemented. Basically, just pretend
3184 * that this is a local lock.
3186 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3190 * Print out the contents of an nfsnode.
3192 * nfs_print(struct vnode *a_vp)
3195 nfs_print(struct vop_print_args *ap)
3197 struct vnode *vp = ap->a_vp;
3198 struct nfsnode *np = VTONFS(vp);
3200 printf("tag VT_NFS, fileid %ld fsid 0x%x",
3201 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3202 if (vp->v_type == VFIFO)
3209 * Just call nfs_writebp() with the force argument set to 1.
3211 * NOTE: B_DONE may or may not be set in a_bp on call.
3213 * nfs_bwrite(struct vnode *a_bp)
3216 nfs_bwrite(struct vop_bwrite_args *ap)
3218 return (nfs_writebp(ap->a_bp, 1, curthread));
3222 * This is a clone of vn_bwrite(), except that it also handles the
3223 * B_NEEDCOMMIT flag. We set B_CACHE if this is a VMIO buffer.
3226 nfs_writebp(struct buf *bp, int force, struct thread *td)
3228 int oldflags = bp->b_flags;
3234 if (BUF_REFCNT(bp) == 0)
3235 panic("bwrite: buffer is not locked???");
3237 if (bp->b_flags & B_INVAL) {
3242 bp->b_flags |= B_CACHE;
3245 * Undirty the bp. We will redirty it later if the I/O fails.
3250 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3252 bp->b_vp->v_numoutput++;
3256 * Note: to avoid loopback deadlocks, we do not
3257 * assign b_runningbufspace.
3259 vfs_busy_pages(bp, 1);
3262 VOP_STRATEGY(bp->b_vp, bp);
3264 if( (oldflags & B_ASYNC) == 0) {
3265 int rtval = biowait(bp);
3267 if (oldflags & B_DELWRI) {
3269 reassignbuf(bp, bp->b_vp);
3281 * nfs special file access vnode op.
3282 * Essentially just get vattr and then imitate iaccess() since the device is
3283 * local to the client.
3285 * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
3286 * struct thread *a_td)
3289 nfsspec_access(struct vop_access_args *ap)
3293 struct ucred *cred = ap->a_cred;
3294 struct vnode *vp = ap->a_vp;
3295 mode_t mode = ap->a_mode;
3301 * Disallow write attempts on filesystems mounted read-only;
3302 * unless the file is a socket, fifo, or a block or character
3303 * device resident on the filesystem.
3305 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3306 switch (vp->v_type) {
3316 * If you're the super-user,
3317 * you always get access.
3319 if (cred->cr_uid == 0)
3322 error = VOP_GETATTR(vp, vap, ap->a_td);
3326 * Access check is based on only one of owner, group, public.
3327 * If not owner, then check group. If not a member of the
3328 * group, then check public access.
3330 if (cred->cr_uid != vap->va_uid) {
3332 gp = cred->cr_groups;
3333 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3334 if (vap->va_gid == *gp)
3340 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3345 * Read wrapper for special devices.
3347 * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3348 * struct ucred *a_cred)
3351 nfsspec_read(struct vop_read_args *ap)
3353 struct nfsnode *np = VTONFS(ap->a_vp);
3359 getnanotime(&np->n_atim);
3360 return (VOCALL(spec_vnode_vops, &ap->a_head));
3364 * Write wrapper for special devices.
3366 * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3367 * struct ucred *a_cred)
3370 nfsspec_write(struct vop_write_args *ap)
3372 struct nfsnode *np = VTONFS(ap->a_vp);
3378 getnanotime(&np->n_mtim);
3379 return (VOCALL(spec_vnode_vops, &ap->a_head));
3383 * Close wrapper for special devices.
3385 * Update the times on the nfsnode then do device close.
3387 * nfsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
3388 * struct thread *a_td)
3391 nfsspec_close(struct vop_close_args *ap)
3393 struct vnode *vp = ap->a_vp;
3394 struct nfsnode *np = VTONFS(vp);
3397 if (np->n_flag & (NACC | NUPD)) {
3399 if (vp->v_usecount == 1 &&
3400 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3402 if (np->n_flag & NACC)
3403 vattr.va_atime = np->n_atim;
3404 if (np->n_flag & NUPD)
3405 vattr.va_mtime = np->n_mtim;
3406 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3409 return (VOCALL(spec_vnode_vops, &ap->a_head));
3413 * Read wrapper for fifos.
3415 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3416 * struct ucred *a_cred)
3419 nfsfifo_read(struct vop_read_args *ap)
3421 struct nfsnode *np = VTONFS(ap->a_vp);
3427 getnanotime(&np->n_atim);
3428 return (VOCALL(fifo_vnode_vops, &ap->a_head));
3432 * Write wrapper for fifos.
3434 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3435 * struct ucred *a_cred)
3438 nfsfifo_write(struct vop_write_args *ap)
3440 struct nfsnode *np = VTONFS(ap->a_vp);
3446 getnanotime(&np->n_mtim);
3447 return (VOCALL(fifo_vnode_vops, &ap->a_head));
3451 * Close wrapper for fifos.
3453 * Update the times on the nfsnode then do fifo close.
3455 * nfsfifo_close(struct vnode *a_vp, int a_fflag, struct thread *a_td)
3458 nfsfifo_close(struct vop_close_args *ap)
3460 struct vnode *vp = ap->a_vp;
3461 struct nfsnode *np = VTONFS(vp);
3465 if (np->n_flag & (NACC | NUPD)) {
3467 if (np->n_flag & NACC)
3469 if (np->n_flag & NUPD)
3472 if (vp->v_usecount == 1 &&
3473 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3475 if (np->n_flag & NACC)
3476 vattr.va_atime = np->n_atim;
3477 if (np->n_flag & NUPD)
3478 vattr.va_mtime = np->n_mtim;
3479 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3482 return (VOCALL(fifo_vnode_vops, &ap->a_head));