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.23 2004/05/08 04:11:48 dillon Exp $
43 * vnode op calls for Sun NFS version 2 and 3
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/resourcevar.h>
53 #include <sys/mount.h>
55 #include <sys/malloc.h>
57 #include <sys/namei.h>
58 #include <sys/socket.h>
59 #include <sys/vnode.h>
60 #include <sys/dirent.h>
61 #include <sys/fcntl.h>
62 #include <sys/lockf.h>
64 #include <sys/sysctl.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_zone.h>
73 #include <vfs/fifofs/fifo.h>
81 #include "nfsm_subs.h"
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
93 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
94 * calls are not in getblk() and brelse() so that they would not be necessary
98 #define vfs_busy_pages(bp, f)
101 static int nfsspec_read (struct vop_read_args *);
102 static int nfsspec_write (struct vop_write_args *);
103 static int nfsfifo_read (struct vop_read_args *);
104 static int nfsfifo_write (struct vop_write_args *);
105 static int nfsspec_close (struct vop_close_args *);
106 static int nfsfifo_close (struct vop_close_args *);
107 #define nfs_poll vop_nopoll
108 static int nfs_flush (struct vnode *,int,struct thread *,int);
109 static int nfs_setattrrpc (struct vnode *,struct vattr *,struct ucred *,struct thread *);
110 static int nfs_lookup (struct vop_lookup_args *);
111 static int nfs_create (struct vop_create_args *);
112 static int nfs_mknod (struct vop_mknod_args *);
113 static int nfs_open (struct vop_open_args *);
114 static int nfs_close (struct vop_close_args *);
115 static int nfs_access (struct vop_access_args *);
116 static int nfs_getattr (struct vop_getattr_args *);
117 static int nfs_setattr (struct vop_setattr_args *);
118 static int nfs_read (struct vop_read_args *);
119 static int nfs_mmap (struct vop_mmap_args *);
120 static int nfs_fsync (struct vop_fsync_args *);
121 static int nfs_remove (struct vop_remove_args *);
122 static int nfs_link (struct vop_link_args *);
123 static int nfs_rename (struct vop_rename_args *);
124 static int nfs_mkdir (struct vop_mkdir_args *);
125 static int nfs_rmdir (struct vop_rmdir_args *);
126 static int nfs_symlink (struct vop_symlink_args *);
127 static int nfs_readdir (struct vop_readdir_args *);
128 static int nfs_bmap (struct vop_bmap_args *);
129 static int nfs_strategy (struct vop_strategy_args *);
130 static int nfs_lookitup (struct vnode *, const char *, int,
131 struct ucred *, struct thread *, struct nfsnode **);
132 static int nfs_sillyrename (struct vnode *,struct vnode *,struct componentname *);
133 static int nfsspec_access (struct vop_access_args *);
134 static int nfs_readlink (struct vop_readlink_args *);
135 static int nfs_print (struct vop_print_args *);
136 static int nfs_advlock (struct vop_advlock_args *);
137 static int nfs_bwrite (struct vop_bwrite_args *);
139 * Global vfs data structures for nfs
141 vop_t **nfsv2_vnodeop_p;
142 static struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
143 { &vop_default_desc, (vop_t *) vop_defaultop },
144 { &vop_access_desc, (vop_t *) nfs_access },
145 { &vop_advlock_desc, (vop_t *) nfs_advlock },
146 { &vop_bmap_desc, (vop_t *) nfs_bmap },
147 { &vop_bwrite_desc, (vop_t *) nfs_bwrite },
148 { &vop_close_desc, (vop_t *) nfs_close },
149 { &vop_create_desc, (vop_t *) nfs_create },
150 { &vop_fsync_desc, (vop_t *) nfs_fsync },
151 { &vop_getattr_desc, (vop_t *) nfs_getattr },
152 { &vop_getpages_desc, (vop_t *) nfs_getpages },
153 { &vop_putpages_desc, (vop_t *) nfs_putpages },
154 { &vop_inactive_desc, (vop_t *) nfs_inactive },
155 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
156 { &vop_lease_desc, (vop_t *) vop_null },
157 { &vop_link_desc, (vop_t *) nfs_link },
158 { &vop_lock_desc, (vop_t *) vop_sharedlock },
159 { &vop_lookup_desc, (vop_t *) nfs_lookup },
160 { &vop_mkdir_desc, (vop_t *) nfs_mkdir },
161 { &vop_mknod_desc, (vop_t *) nfs_mknod },
162 { &vop_mmap_desc, (vop_t *) nfs_mmap },
163 { &vop_open_desc, (vop_t *) nfs_open },
164 { &vop_poll_desc, (vop_t *) nfs_poll },
165 { &vop_print_desc, (vop_t *) nfs_print },
166 { &vop_read_desc, (vop_t *) nfs_read },
167 { &vop_readdir_desc, (vop_t *) nfs_readdir },
168 { &vop_readlink_desc, (vop_t *) nfs_readlink },
169 { &vop_reclaim_desc, (vop_t *) nfs_reclaim },
170 { &vop_remove_desc, (vop_t *) nfs_remove },
171 { &vop_rename_desc, (vop_t *) nfs_rename },
172 { &vop_rmdir_desc, (vop_t *) nfs_rmdir },
173 { &vop_setattr_desc, (vop_t *) nfs_setattr },
174 { &vop_strategy_desc, (vop_t *) nfs_strategy },
175 { &vop_symlink_desc, (vop_t *) nfs_symlink },
176 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
177 { &vop_write_desc, (vop_t *) nfs_write },
180 static struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
181 { &nfsv2_vnodeop_p, nfsv2_vnodeop_entries };
182 VNODEOP_SET(nfsv2_vnodeop_opv_desc);
185 * Special device vnode ops
187 vop_t **spec_nfsv2nodeop_p;
188 static struct vnodeopv_entry_desc nfsv2_specop_entries[] = {
189 { &vop_default_desc, (vop_t *) spec_vnoperate },
190 { &vop_access_desc, (vop_t *) nfsspec_access },
191 { &vop_close_desc, (vop_t *) nfsspec_close },
192 { &vop_fsync_desc, (vop_t *) nfs_fsync },
193 { &vop_getattr_desc, (vop_t *) nfs_getattr },
194 { &vop_inactive_desc, (vop_t *) nfs_inactive },
195 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
196 { &vop_lock_desc, (vop_t *) vop_sharedlock },
197 { &vop_print_desc, (vop_t *) nfs_print },
198 { &vop_read_desc, (vop_t *) nfsspec_read },
199 { &vop_reclaim_desc, (vop_t *) nfs_reclaim },
200 { &vop_setattr_desc, (vop_t *) nfs_setattr },
201 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
202 { &vop_write_desc, (vop_t *) nfsspec_write },
205 static struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
206 { &spec_nfsv2nodeop_p, nfsv2_specop_entries };
207 VNODEOP_SET(spec_nfsv2nodeop_opv_desc);
209 vop_t **fifo_nfsv2nodeop_p;
210 static struct vnodeopv_entry_desc nfsv2_fifoop_entries[] = {
211 { &vop_default_desc, (vop_t *) fifo_vnoperate },
212 { &vop_access_desc, (vop_t *) nfsspec_access },
213 { &vop_close_desc, (vop_t *) nfsfifo_close },
214 { &vop_fsync_desc, (vop_t *) nfs_fsync },
215 { &vop_getattr_desc, (vop_t *) nfs_getattr },
216 { &vop_inactive_desc, (vop_t *) nfs_inactive },
217 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
218 { &vop_lock_desc, (vop_t *) vop_sharedlock },
219 { &vop_print_desc, (vop_t *) nfs_print },
220 { &vop_read_desc, (vop_t *) nfsfifo_read },
221 { &vop_reclaim_desc, (vop_t *) nfs_reclaim },
222 { &vop_setattr_desc, (vop_t *) nfs_setattr },
223 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
224 { &vop_write_desc, (vop_t *) nfsfifo_write },
227 static struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
228 { &fifo_nfsv2nodeop_p, nfsv2_fifoop_entries };
229 VNODEOP_SET(fifo_nfsv2nodeop_opv_desc);
231 static int nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
232 struct componentname *cnp,
234 static int nfs_removerpc (struct vnode *dvp, const char *name,
236 struct ucred *cred, struct thread *td);
237 static int nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
238 int fnamelen, struct vnode *tdvp,
239 const char *tnameptr, int tnamelen,
240 struct ucred *cred, struct thread *td);
241 static int nfs_renameit (struct vnode *sdvp,
242 struct componentname *scnp,
243 struct sillyrename *sp);
248 extern u_int32_t nfs_true, nfs_false;
249 extern u_int32_t nfs_xdrneg1;
250 extern struct nfsstats nfsstats;
251 extern nfstype nfsv3_type[9];
252 struct thread *nfs_iodwant[NFS_MAXASYNCDAEMON];
253 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
254 int nfs_numasync = 0;
255 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
257 SYSCTL_DECL(_vfs_nfs);
259 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
260 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
261 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
263 static int nfsneg_cache_timeout = NFS_MINATTRTIMO;
264 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
265 &nfsneg_cache_timeout, 0, "NFS NEGATIVE ACCESS cache timeout");
267 static int nfsv3_commit_on_close = 0;
268 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
269 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
271 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
272 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
274 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
275 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
278 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
279 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
280 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
282 nfs3_access_otw(struct vnode *vp, int wmode,
283 struct thread *td, struct ucred *cred)
287 int error = 0, attrflag;
289 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
290 caddr_t bpos, dpos, cp2;
294 struct nfsnode *np = VTONFS(vp);
296 nfsstats.rpccnt[NFSPROC_ACCESS]++;
297 nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
299 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
300 *tl = txdr_unsigned(wmode);
301 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
302 nfsm_postop_attr(vp, attrflag);
304 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
305 rmode = fxdr_unsigned(u_int32_t, *tl);
307 np->n_modeuid = cred->cr_uid;
308 np->n_modestamp = mycpu->gd_time_seconds;
316 * nfs access vnode op.
317 * For nfs version 2, just return ok. File accesses may fail later.
318 * For nfs version 3, use the access rpc to check accessibility. If file modes
319 * are changed on the server, accesses might still fail later.
321 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
322 * struct thread *a_td)
325 nfs_access(struct vop_access_args *ap)
327 struct vnode *vp = ap->a_vp;
329 u_int32_t mode, wmode;
330 int v3 = NFS_ISV3(vp);
331 struct nfsnode *np = VTONFS(vp);
334 * Disallow write attempts on filesystems mounted read-only;
335 * unless the file is a socket, fifo, or a block or character
336 * device resident on the filesystem.
338 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
339 switch (vp->v_type) {
349 * For nfs v3, check to see if we have done this recently, and if
350 * so return our cached result instead of making an ACCESS call.
351 * If not, do an access rpc, otherwise you are stuck emulating
352 * ufs_access() locally using the vattr. This may not be correct,
353 * since the server may apply other access criteria such as
354 * client uid-->server uid mapping that we do not know about.
357 if (ap->a_mode & VREAD)
358 mode = NFSV3ACCESS_READ;
361 if (vp->v_type != VDIR) {
362 if (ap->a_mode & VWRITE)
363 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
364 if (ap->a_mode & VEXEC)
365 mode |= NFSV3ACCESS_EXECUTE;
367 if (ap->a_mode & VWRITE)
368 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
370 if (ap->a_mode & VEXEC)
371 mode |= NFSV3ACCESS_LOOKUP;
373 /* XXX safety belt, only make blanket request if caching */
374 if (nfsaccess_cache_timeout > 0) {
375 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
376 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
377 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
383 * Does our cached result allow us to give a definite yes to
386 if ((mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
387 (ap->a_cred->cr_uid == np->n_modeuid) &&
388 ((np->n_mode & mode) == mode)) {
389 nfsstats.accesscache_hits++;
392 * Either a no, or a don't know. Go to the wire.
394 nfsstats.accesscache_misses++;
395 error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred);
397 if ((np->n_mode & mode) != mode) {
403 if ((error = nfsspec_access(ap)) != 0)
407 * Attempt to prevent a mapped root from accessing a file
408 * which it shouldn't. We try to read a byte from the file
409 * if the user is root and the file is not zero length.
410 * After calling nfsspec_access, we should have the correct
413 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
414 && VTONFS(vp)->n_size > 0) {
421 auio.uio_iov = &aiov;
425 auio.uio_segflg = UIO_SYSSPACE;
426 auio.uio_rw = UIO_READ;
427 auio.uio_td = ap->a_td;
429 if (vp->v_type == VREG) {
430 error = nfs_readrpc(vp, &auio);
431 } else if (vp->v_type == VDIR) {
433 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
435 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
436 error = nfs_readdirrpc(vp, &auio);
438 } else if (vp->v_type == VLNK) {
439 error = nfs_readlinkrpc(vp, &auio);
446 * [re]record creds for reading and/or writing if access
447 * was granted. Assume the NFS server will grant read access
448 * for execute requests.
451 if ((ap->a_mode & (VREAD|VEXEC)) && ap->a_cred != np->n_rucred) {
454 crfree(np->n_rucred);
455 np->n_rucred = ap->a_cred;
457 if ((ap->a_mode & VWRITE) && ap->a_cred != np->n_wucred) {
460 crfree(np->n_wucred);
461 np->n_wucred = ap->a_cred;
469 * Check to see if the type is ok
470 * and that deletion is not in progress.
471 * For paged in text files, you will need to flush the page cache
472 * if consistency is lost.
474 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
475 * struct thread *a_td)
479 nfs_open(struct vop_open_args *ap)
481 struct vnode *vp = ap->a_vp;
482 struct nfsnode *np = VTONFS(vp);
483 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
487 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
489 printf("open eacces vtyp=%d\n",vp->v_type);
494 * Get a valid lease. If cached data is stale, flush it.
496 if (nmp->nm_flag & NFSMNT_NQNFS) {
497 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
499 error = nqnfs_getlease(vp, ND_READ, ap->a_td);
500 } while (error == NQNFS_EXPIRED);
503 if (np->n_lrev != np->n_brev ||
504 (np->n_flag & NQNFSNONCACHE)) {
505 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1))
509 np->n_brev = np->n_lrev;
513 if (np->n_flag & NMODIFIED) {
514 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1))
519 if (vp->v_type == VDIR)
520 np->n_direofoffset = 0;
521 error = VOP_GETATTR(vp, &vattr, ap->a_td);
524 np->n_mtime = vattr.va_mtime.tv_sec;
526 error = VOP_GETATTR(vp, &vattr, ap->a_td);
529 if (np->n_mtime != vattr.va_mtime.tv_sec) {
530 if (vp->v_type == VDIR)
531 np->n_direofoffset = 0;
532 if ((error = nfs_vinvalbuf(vp, V_SAVE,
533 ap->a_td, 1)) == EINTR) {
536 np->n_mtime = vattr.va_mtime.tv_sec;
540 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0)
541 np->n_attrstamp = 0; /* For Open/Close consistency */
547 * What an NFS client should do upon close after writing is a debatable issue.
548 * Most NFS clients push delayed writes to the server upon close, basically for
550 * 1 - So that any write errors may be reported back to the client process
551 * doing the close system call. By far the two most likely errors are
552 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
553 * 2 - To put a worst case upper bound on cache inconsistency between
554 * multiple clients for the file.
555 * There is also a consistency problem for Version 2 of the protocol w.r.t.
556 * not being able to tell if other clients are writing a file concurrently,
557 * since there is no way of knowing if the changed modify time in the reply
558 * is only due to the write for this client.
559 * (NFS Version 3 provides weak cache consistency data in the reply that
560 * should be sufficient to detect and handle this case.)
562 * The current code does the following:
563 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
564 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
565 * or commit them (this satisfies 1 and 2 except for the
566 * case where the server crashes after this close but
567 * before the commit RPC, which is felt to be "good
568 * enough". Changing the last argument to nfs_flush() to
569 * a 1 would force a commit operation, if it is felt a
570 * commit is necessary now.
571 * for NQNFS - do nothing now, since 2 is dealt with via leases and
572 * 1 should be dealt with via an fsync() system call for
573 * cases where write errors are important.
575 * nfs_close(struct vnodeop_desc *a_desc, struct vnode *a_vp, int a_fflag,
576 * struct ucred *a_cred, struct thread *a_td)
580 nfs_close(struct vop_close_args *ap)
582 struct vnode *vp = ap->a_vp;
583 struct nfsnode *np = VTONFS(vp);
586 if (vp->v_type == VREG) {
587 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 &&
588 (np->n_flag & NMODIFIED)) {
591 * Under NFSv3 we have dirty buffers to dispose of. We
592 * must flush them to the NFS server. We have the option
593 * of waiting all the way through the commit rpc or just
594 * waiting for the initial write. The default is to only
595 * wait through the initial write so the data is in the
596 * server's cache, which is roughly similar to the state
597 * a standard disk subsystem leaves the file in on close().
599 * We cannot clear the NMODIFIED bit in np->n_flag due to
600 * potential races with other processes, and certainly
601 * cannot clear it if we don't commit.
603 int cm = nfsv3_commit_on_close ? 1 : 0;
604 error = nfs_flush(vp, MNT_WAIT, ap->a_td, cm);
605 /* np->n_flag &= ~NMODIFIED; */
607 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
611 if (np->n_flag & NWRITEERR) {
612 np->n_flag &= ~NWRITEERR;
620 * nfs getattr call from vfs.
622 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred,
623 * struct thread *a_td)
626 nfs_getattr(struct vop_getattr_args *ap)
628 struct vnode *vp = ap->a_vp;
629 struct nfsnode *np = VTONFS(vp);
635 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
636 int v3 = NFS_ISV3(vp);
639 * Update local times for special files.
641 if (np->n_flag & (NACC | NUPD))
644 * First look in the cache.
646 if (nfs_getattrcache(vp, ap->a_vap) == 0)
649 if (v3 && nfsaccess_cache_timeout > 0) {
650 nfsstats.accesscache_misses++;
651 nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_td, nfs_vpcred(vp, ND_CHECK));
652 if (nfs_getattrcache(vp, ap->a_vap) == 0)
656 nfsstats.rpccnt[NFSPROC_GETATTR]++;
657 nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
659 nfsm_request(vp, NFSPROC_GETATTR, ap->a_td, nfs_vpcred(vp, ND_CHECK));
661 nfsm_loadattr(vp, ap->a_vap);
671 * nfs_setattr(struct vnodeop_desc *a_desc, struct vnode *a_vp,
672 * struct vattr *a_vap, struct ucred *a_cred,
673 * struct thread *a_td)
676 nfs_setattr(struct vop_setattr_args *ap)
678 struct vnode *vp = ap->a_vp;
679 struct nfsnode *np = VTONFS(vp);
680 struct vattr *vap = ap->a_vap;
689 * Setting of flags is not supported.
691 if (vap->va_flags != VNOVAL)
695 * Disallow write attempts if the filesystem is mounted read-only.
697 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
698 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
699 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
700 (vp->v_mount->mnt_flag & MNT_RDONLY))
702 if (vap->va_size != VNOVAL) {
703 switch (vp->v_type) {
710 if (vap->va_mtime.tv_sec == VNOVAL &&
711 vap->va_atime.tv_sec == VNOVAL &&
712 vap->va_mode == (mode_t)VNOVAL &&
713 vap->va_uid == (uid_t)VNOVAL &&
714 vap->va_gid == (gid_t)VNOVAL)
716 vap->va_size = VNOVAL;
720 * Disallow write attempts if the filesystem is
723 if (vp->v_mount->mnt_flag & MNT_RDONLY)
727 * We run vnode_pager_setsize() early (why?),
728 * we must set np->n_size now to avoid vinvalbuf
729 * V_SAVE races that might setsize a lower
734 error = nfs_meta_setsize(vp, ap->a_td, vap->va_size);
736 if (np->n_flag & NMODIFIED) {
737 if (vap->va_size == 0)
738 error = nfs_vinvalbuf(vp, 0, ap->a_td, 1);
740 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
743 vnode_pager_setsize(vp, np->n_size);
748 * np->n_size has already been set to vap->va_size
749 * in nfs_meta_setsize(). We must set it again since
750 * nfs_loadattrcache() could be called through
751 * nfs_meta_setsize() and could modify np->n_size.
753 * (note that nfs_loadattrcache() will have called
754 * vnode_pager_setsize() for us in that case).
756 np->n_vattr.va_size = np->n_size = vap->va_size;
758 } else if ((vap->va_mtime.tv_sec != VNOVAL ||
759 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NMODIFIED) &&
760 vp->v_type == VREG &&
761 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) == EINTR)
763 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
764 if (error && vap->va_size != VNOVAL) {
765 np->n_size = np->n_vattr.va_size = tsize;
766 vnode_pager_setsize(vp, np->n_size);
772 * Do an nfs setattr rpc.
775 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
776 struct ucred *cred, struct thread *td)
778 struct nfsv2_sattr *sp;
781 caddr_t bpos, dpos, cp2;
783 int error = 0, wccflag = NFSV3_WCCRATTR;
784 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
785 int v3 = NFS_ISV3(vp);
787 nfsstats.rpccnt[NFSPROC_SETATTR]++;
788 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
791 nfsm_v3attrbuild(vap, TRUE);
792 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
795 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
796 if (vap->va_mode == (mode_t)VNOVAL)
797 sp->sa_mode = nfs_xdrneg1;
799 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
800 if (vap->va_uid == (uid_t)VNOVAL)
801 sp->sa_uid = nfs_xdrneg1;
803 sp->sa_uid = txdr_unsigned(vap->va_uid);
804 if (vap->va_gid == (gid_t)VNOVAL)
805 sp->sa_gid = nfs_xdrneg1;
807 sp->sa_gid = txdr_unsigned(vap->va_gid);
808 sp->sa_size = txdr_unsigned(vap->va_size);
809 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
810 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
812 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
814 nfsm_wcc_data(vp, wccflag);
816 nfsm_loadattr(vp, (struct vattr *)0);
823 * 'cached' nfs directory lookup
825 * nfs_lookup(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
826 * struct vnode **a_vpp, struct componentname *a_cnp)
829 nfs_lookup(struct vop_lookup_args *ap)
831 struct componentname *cnp = ap->a_cnp;
832 struct vnode *dvp = ap->a_dvp;
833 struct vnode **vpp = ap->a_vpp;
834 int flags = cnp->cn_flags;
839 struct nfsmount *nmp;
840 caddr_t bpos, dpos, cp2;
841 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
845 int lockparent, wantparent, error = 0, attrflag, fhsize;
846 int v3 = NFS_ISV3(dvp);
847 struct thread *td = cnp->cn_td;
850 * Read-only mount check and directory check.
853 if ((flags & CNP_ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
854 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
857 if (dvp->v_type != VDIR)
861 * Look it up in the cache. Note that ENOENT is only returned if we
862 * previously entered a negative hit (see later on). The additional
863 * nfsneg_cache_timeout check causes previously cached results to
864 * be instantly ignored if the negative caching is turned off.
866 lockparent = flags & CNP_LOCKPARENT;
867 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
868 nmp = VFSTONFS(dvp->v_mount);
870 error = cache_lookup(dvp, NCPNULL, vpp, NCPPNULL, cnp);
875 if (error == ENOENT && nfsneg_cache_timeout) {
879 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
887 * See the comment starting `Step through' in ufs/ufs_lookup.c
888 * for an explanation of the locking protocol
893 } else if (flags & CNP_ISDOTDOT) {
894 VOP_UNLOCK(dvp, NULL, 0, td);
895 error = vget(newvp, NULL, LK_EXCLUSIVE, td);
896 if (!error && lockparent && (flags & CNP_ISLASTCN))
897 error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td);
899 error = vget(newvp, NULL, LK_EXCLUSIVE, td);
900 if (!lockparent || error || !(flags & CNP_ISLASTCN))
901 VOP_UNLOCK(dvp, NULL, 0, td);
904 if (vpid == newvp->v_id) {
905 if (!VOP_GETATTR(newvp, &vattr, td)
906 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
907 nfsstats.lookupcache_hits++;
908 if (cnp->cn_nameiop != NAMEI_LOOKUP &&
909 (flags & CNP_ISLASTCN))
910 cnp->cn_flags |= CNP_SAVENAME;
916 if (lockparent && dvp != newvp && (flags & CNP_ISLASTCN))
917 VOP_UNLOCK(dvp, NULL, 0, td);
919 error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td);
926 * Cache miss, go the wire.
930 nfsstats.lookupcache_misses++;
931 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
932 len = cnp->cn_namelen;
933 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
934 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
936 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
937 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
940 * Cache negatve lookups to reduce NFS traffic, but use
943 if (error == ENOENT &&
944 (cnp->cn_flags & CNP_MAKEENTRY) &&
945 cnp->cn_nameiop == NAMEI_LOOKUP &&
946 nfsneg_cache_timeout) {
947 int toval = nfsneg_cache_timeout * hz;
948 if (cnp->cn_flags & CNP_CACHETIMEOUT) {
949 if (cnp->cn_timeout > toval)
950 cnp->cn_timeout = toval;
952 cnp->cn_flags |= CNP_CACHETIMEOUT;
953 cnp->cn_timeout = toval;
955 cache_enter(dvp, NCPNULL, NULL, cnp);
957 nfsm_postop_attr(dvp, attrflag);
961 nfsm_getfh(fhp, fhsize, v3);
964 * Handle RENAME case...
966 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent && (flags & CNP_ISLASTCN)) {
967 if (NFS_CMPFH(np, fhp, fhsize)) {
971 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
978 nfsm_postop_attr(newvp, attrflag);
979 nfsm_postop_attr(dvp, attrflag);
981 nfsm_loadattr(newvp, (struct vattr *)0);
984 cnp->cn_flags |= CNP_SAVENAME;
986 VOP_UNLOCK(dvp, NULL, 0, td);
990 if (flags & CNP_ISDOTDOT) {
991 VOP_UNLOCK(dvp, NULL, 0, td);
992 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
994 vn_lock(dvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
998 if (lockparent && (flags & CNP_ISLASTCN) &&
999 (error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td))) {
1003 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1007 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1012 if (!lockparent || !(flags & CNP_ISLASTCN))
1013 VOP_UNLOCK(dvp, NULL, 0, td);
1017 nfsm_postop_attr(newvp, attrflag);
1018 nfsm_postop_attr(dvp, attrflag);
1020 nfsm_loadattr(newvp, (struct vattr *)0);
1021 if (cnp->cn_nameiop != NAMEI_LOOKUP && (flags & CNP_ISLASTCN))
1022 cnp->cn_flags |= CNP_SAVENAME;
1023 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1024 (cnp->cn_nameiop != NAMEI_DELETE || !(flags & CNP_ISLASTCN))) {
1025 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
1026 cache_enter(dvp, NCPNULL, newvp, cnp);
1032 if (newvp != NULLVP) {
1036 if ((cnp->cn_nameiop == NAMEI_CREATE || cnp->cn_nameiop == NAMEI_RENAME) &&
1037 (flags & CNP_ISLASTCN) && error == ENOENT) {
1039 VOP_UNLOCK(dvp, NULL, 0, td);
1040 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1043 error = EJUSTRETURN;
1045 if (cnp->cn_nameiop != NAMEI_LOOKUP && (flags & CNP_ISLASTCN))
1046 cnp->cn_flags |= CNP_SAVENAME;
1053 * Just call nfs_bioread() to do the work.
1055 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1056 * struct ucred *a_cred)
1059 nfs_read(struct vop_read_args *ap)
1061 struct vnode *vp = ap->a_vp;
1063 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1064 switch (vp->v_type) {
1066 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1077 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1080 nfs_readlink(struct vop_readlink_args *ap)
1082 struct vnode *vp = ap->a_vp;
1084 if (vp->v_type != VLNK)
1086 return (nfs_bioread(vp, ap->a_uio, 0));
1090 * Do a readlink rpc.
1091 * Called by nfs_doio() from below the buffer cache.
1094 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1099 caddr_t bpos, dpos, cp2;
1100 int error = 0, len, attrflag;
1101 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1102 int v3 = NFS_ISV3(vp);
1104 nfsstats.rpccnt[NFSPROC_READLINK]++;
1105 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1107 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1109 nfsm_postop_attr(vp, attrflag);
1111 nfsm_strsiz(len, NFS_MAXPATHLEN);
1112 if (len == NFS_MAXPATHLEN) {
1113 struct nfsnode *np = VTONFS(vp);
1114 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1117 nfsm_mtouio(uiop, len);
1129 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1134 caddr_t bpos, dpos, cp2;
1135 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1136 struct nfsmount *nmp;
1137 int error = 0, len, retlen, tsiz, eof, attrflag;
1138 int v3 = NFS_ISV3(vp);
1143 nmp = VFSTONFS(vp->v_mount);
1144 tsiz = uiop->uio_resid;
1145 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1148 nfsstats.rpccnt[NFSPROC_READ]++;
1149 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1150 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1152 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1154 txdr_hyper(uiop->uio_offset, tl);
1155 *(tl + 2) = txdr_unsigned(len);
1157 *tl++ = txdr_unsigned(uiop->uio_offset);
1158 *tl++ = txdr_unsigned(len);
1161 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1163 nfsm_postop_attr(vp, attrflag);
1168 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1169 eof = fxdr_unsigned(int, *(tl + 1));
1171 nfsm_loadattr(vp, (struct vattr *)0);
1172 nfsm_strsiz(retlen, nmp->nm_rsize);
1173 nfsm_mtouio(uiop, retlen);
1177 if (eof || retlen == 0) {
1180 } else if (retlen < len) {
1192 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, int *must_commit)
1196 int32_t t1, t2, backup;
1197 caddr_t bpos, dpos, cp2;
1198 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1199 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1200 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1201 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1204 if (uiop->uio_iovcnt != 1)
1205 panic("nfs: writerpc iovcnt > 1");
1208 tsiz = uiop->uio_resid;
1209 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1212 nfsstats.rpccnt[NFSPROC_WRITE]++;
1213 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1214 nfsm_reqhead(vp, NFSPROC_WRITE,
1215 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1218 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1219 txdr_hyper(uiop->uio_offset, tl);
1221 *tl++ = txdr_unsigned(len);
1222 *tl++ = txdr_unsigned(*iomode);
1223 *tl = txdr_unsigned(len);
1227 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1228 /* Set both "begin" and "current" to non-garbage. */
1229 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1230 *tl++ = x; /* "begin offset" */
1231 *tl++ = x; /* "current offset" */
1232 x = txdr_unsigned(len);
1233 *tl++ = x; /* total to this offset */
1234 *tl = x; /* size of this write */
1236 nfsm_uiotom(uiop, len);
1237 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1239 wccflag = NFSV3_WCCCHK;
1240 nfsm_wcc_data(vp, wccflag);
1242 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1243 + NFSX_V3WRITEVERF);
1244 rlen = fxdr_unsigned(int, *tl++);
1249 } else if (rlen < len) {
1250 backup = len - rlen;
1251 uiop->uio_iov->iov_base -= backup;
1252 uiop->uio_iov->iov_len += backup;
1253 uiop->uio_offset -= backup;
1254 uiop->uio_resid += backup;
1257 commit = fxdr_unsigned(int, *tl++);
1260 * Return the lowest committment level
1261 * obtained by any of the RPCs.
1263 if (committed == NFSV3WRITE_FILESYNC)
1265 else if (committed == NFSV3WRITE_DATASYNC &&
1266 commit == NFSV3WRITE_UNSTABLE)
1268 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1269 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1271 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1272 } else if (bcmp((caddr_t)tl,
1273 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1275 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1280 nfsm_loadattr(vp, (struct vattr *)0);
1282 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime.tv_sec;
1289 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1290 committed = NFSV3WRITE_FILESYNC;
1291 *iomode = committed;
1293 uiop->uio_resid = tsiz;
1299 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1300 * mode set to specify the file type and the size field for rdev.
1303 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1306 struct nfsv2_sattr *sp;
1310 struct vnode *newvp = (struct vnode *)0;
1311 struct nfsnode *np = (struct nfsnode *)0;
1315 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1316 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1318 int v3 = NFS_ISV3(dvp);
1320 if (vap->va_type == VCHR || vap->va_type == VBLK)
1321 rdev = txdr_unsigned(vap->va_rdev);
1322 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1325 return (EOPNOTSUPP);
1327 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1330 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1331 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1332 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1333 nfsm_fhtom(dvp, v3);
1334 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1336 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1337 *tl++ = vtonfsv3_type(vap->va_type);
1338 nfsm_v3attrbuild(vap, FALSE);
1339 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1340 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1341 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1342 *tl = txdr_unsigned(uminor(vap->va_rdev));
1345 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1346 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1347 sp->sa_uid = nfs_xdrneg1;
1348 sp->sa_gid = nfs_xdrneg1;
1350 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1351 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1353 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1355 nfsm_mtofh(dvp, newvp, v3, gotvp);
1359 newvp = (struct vnode *)0;
1361 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1362 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1368 nfsm_wcc_data(dvp, wccflag);
1375 if (cnp->cn_flags & CNP_MAKEENTRY)
1376 cache_enter(dvp, NCPNULL, newvp, cnp);
1379 VTONFS(dvp)->n_flag |= NMODIFIED;
1381 VTONFS(dvp)->n_attrstamp = 0;
1387 * just call nfs_mknodrpc() to do the work.
1389 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1390 * struct componentname *a_cnp, struct vattr *a_vap)
1394 nfs_mknod(struct vop_mknod_args *ap)
1396 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1399 static u_long create_verf;
1401 * nfs file create call
1403 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1404 * struct componentname *a_cnp, struct vattr *a_vap)
1407 nfs_create(struct vop_create_args *ap)
1409 struct vnode *dvp = ap->a_dvp;
1410 struct vattr *vap = ap->a_vap;
1411 struct componentname *cnp = ap->a_cnp;
1412 struct nfsv2_sattr *sp;
1416 struct nfsnode *np = (struct nfsnode *)0;
1417 struct vnode *newvp = (struct vnode *)0;
1418 caddr_t bpos, dpos, cp2;
1419 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1420 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1422 int v3 = NFS_ISV3(dvp);
1425 * Oops, not for me..
1427 if (vap->va_type == VSOCK)
1428 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1430 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1433 if (vap->va_vaflags & VA_EXCLUSIVE)
1436 nfsstats.rpccnt[NFSPROC_CREATE]++;
1437 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1438 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1439 nfsm_fhtom(dvp, v3);
1440 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1442 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1443 if (fmode & O_EXCL) {
1444 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1445 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1447 if (!TAILQ_EMPTY(&in_ifaddrhead))
1448 *tl++ = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr.s_addr;
1451 *tl++ = create_verf;
1452 *tl = ++create_verf;
1454 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1455 nfsm_v3attrbuild(vap, FALSE);
1458 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1459 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1460 sp->sa_uid = nfs_xdrneg1;
1461 sp->sa_gid = nfs_xdrneg1;
1463 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1464 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1466 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1468 nfsm_mtofh(dvp, newvp, v3, gotvp);
1472 newvp = (struct vnode *)0;
1474 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1475 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1481 nfsm_wcc_data(dvp, wccflag);
1485 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1491 } else if (v3 && (fmode & O_EXCL)) {
1493 * We are normally called with only a partially initialized
1494 * VAP. Since the NFSv3 spec says that server may use the
1495 * file attributes to store the verifier, the spec requires
1496 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1497 * in atime, but we can't really assume that all servers will
1498 * so we ensure that our SETATTR sets both atime and mtime.
1500 if (vap->va_mtime.tv_sec == VNOVAL)
1501 vfs_timestamp(&vap->va_mtime);
1502 if (vap->va_atime.tv_sec == VNOVAL)
1503 vap->va_atime = vap->va_mtime;
1504 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1507 if (cnp->cn_flags & CNP_MAKEENTRY)
1508 cache_enter(dvp, NCPNULL, newvp, cnp);
1510 * The new np may have enough info for access
1511 * checks, make sure rucred and wucred are
1512 * initialized for read and write rpc's.
1515 if (np->n_rucred == NULL)
1516 np->n_rucred = crhold(cnp->cn_cred);
1517 if (np->n_wucred == NULL)
1518 np->n_wucred = crhold(cnp->cn_cred);
1521 VTONFS(dvp)->n_flag |= NMODIFIED;
1523 VTONFS(dvp)->n_attrstamp = 0;
1528 * nfs file remove call
1529 * To try and make nfs semantics closer to ufs semantics, a file that has
1530 * other processes using the vnode is renamed instead of removed and then
1531 * removed later on the last close.
1532 * - If v_usecount > 1
1533 * If a rename is not already in the works
1534 * call nfs_sillyrename() to set it up
1538 * nfs_remove(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
1539 * struct vnode *a_vp, struct componentname *a_cnp)
1542 nfs_remove(struct vop_remove_args *ap)
1544 struct vnode *vp = ap->a_vp;
1545 struct vnode *dvp = ap->a_dvp;
1546 struct componentname *cnp = ap->a_cnp;
1547 struct nfsnode *np = VTONFS(vp);
1552 if ((cnp->cn_flags & CNP_HASBUF) == 0)
1553 panic("nfs_remove: no name");
1554 if (vp->v_usecount < 1)
1555 panic("nfs_remove: bad v_usecount");
1557 if (vp->v_type == VDIR)
1559 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1560 VOP_GETATTR(vp, &vattr, cnp->cn_td) == 0 &&
1561 vattr.va_nlink > 1)) {
1563 * Purge the name cache so that the chance of a lookup for
1564 * the name succeeding while the remove is in progress is
1565 * minimized. Without node locking it can still happen, such
1566 * that an I/O op returns ESTALE, but since you get this if
1567 * another host removes the file..
1571 * throw away biocache buffers, mainly to avoid
1572 * unnecessary delayed writes later.
1574 error = nfs_vinvalbuf(vp, 0, cnp->cn_td, 1);
1577 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1578 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1580 * Kludge City: If the first reply to the remove rpc is lost..
1581 * the reply to the retransmitted request will be ENOENT
1582 * since the file was in fact removed
1583 * Therefore, we cheat and return success.
1585 if (error == ENOENT)
1587 } else if (!np->n_sillyrename)
1588 error = nfs_sillyrename(dvp, vp, cnp);
1589 np->n_attrstamp = 0;
1594 * nfs file remove rpc called from nfs_inactive
1597 nfs_removeit(struct sillyrename *sp)
1599 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1604 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1607 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1608 struct ucred *cred, struct thread *td)
1613 caddr_t bpos, dpos, cp2;
1614 int error = 0, wccflag = NFSV3_WCCRATTR;
1615 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1616 int v3 = NFS_ISV3(dvp);
1618 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1619 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1620 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1621 nfsm_fhtom(dvp, v3);
1622 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1623 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1625 nfsm_wcc_data(dvp, wccflag);
1628 VTONFS(dvp)->n_flag |= NMODIFIED;
1630 VTONFS(dvp)->n_attrstamp = 0;
1635 * nfs file rename call
1637 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1638 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1639 * struct vnode *a_tvp, struct componentname *a_tcnp)
1642 nfs_rename(struct vop_rename_args *ap)
1644 struct vnode *fvp = ap->a_fvp;
1645 struct vnode *tvp = ap->a_tvp;
1646 struct vnode *fdvp = ap->a_fdvp;
1647 struct vnode *tdvp = ap->a_tdvp;
1648 struct componentname *tcnp = ap->a_tcnp;
1649 struct componentname *fcnp = ap->a_fcnp;
1653 if ((tcnp->cn_flags & CNP_HASBUF) == 0 ||
1654 (fcnp->cn_flags & CNP_HASBUF) == 0)
1655 panic("nfs_rename: no name");
1657 /* Check for cross-device rename */
1658 if ((fvp->v_mount != tdvp->v_mount) ||
1659 (tvp && (fvp->v_mount != tvp->v_mount))) {
1665 * We have to flush B_DELWRI data prior to renaming
1666 * the file. If we don't, the delayed-write buffers
1667 * can be flushed out later after the file has gone stale
1668 * under NFSV3. NFSV2 does not have this problem because
1669 * ( as far as I can tell ) it flushes dirty buffers more
1673 VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_td);
1675 VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_td);
1678 * If the tvp exists and is in use, sillyrename it before doing the
1679 * rename of the new file over it.
1680 * XXX Can't sillyrename a directory.
1682 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1683 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1688 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1689 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1692 if (fvp->v_type == VDIR) {
1693 if (tvp != NULL && tvp->v_type == VDIR)
1708 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1710 if (error == ENOENT)
1716 * nfs file rename rpc called from nfs_remove() above
1719 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1720 struct sillyrename *sp)
1722 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1723 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1727 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1730 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1731 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1732 struct ucred *cred, struct thread *td)
1737 caddr_t bpos, dpos, cp2;
1738 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1739 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1740 int v3 = NFS_ISV3(fdvp);
1742 nfsstats.rpccnt[NFSPROC_RENAME]++;
1743 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1744 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1745 nfsm_rndup(tnamelen));
1746 nfsm_fhtom(fdvp, v3);
1747 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1748 nfsm_fhtom(tdvp, v3);
1749 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1750 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1752 nfsm_wcc_data(fdvp, fwccflag);
1753 nfsm_wcc_data(tdvp, twccflag);
1757 VTONFS(fdvp)->n_flag |= NMODIFIED;
1758 VTONFS(tdvp)->n_flag |= NMODIFIED;
1760 VTONFS(fdvp)->n_attrstamp = 0;
1762 VTONFS(tdvp)->n_attrstamp = 0;
1767 * nfs hard link create call
1769 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1770 * struct componentname *a_cnp)
1773 nfs_link(struct vop_link_args *ap)
1775 struct vnode *vp = ap->a_vp;
1776 struct vnode *tdvp = ap->a_tdvp;
1777 struct componentname *cnp = ap->a_cnp;
1781 caddr_t bpos, dpos, cp2;
1782 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1783 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1786 if (vp->v_mount != tdvp->v_mount) {
1791 * Push all writes to the server, so that the attribute cache
1792 * doesn't get "out of sync" with the server.
1793 * XXX There should be a better way!
1795 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_td);
1798 nfsstats.rpccnt[NFSPROC_LINK]++;
1799 nfsm_reqhead(vp, NFSPROC_LINK,
1800 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1802 nfsm_fhtom(tdvp, v3);
1803 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1804 nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1806 nfsm_postop_attr(vp, attrflag);
1807 nfsm_wcc_data(tdvp, wccflag);
1811 VTONFS(tdvp)->n_flag |= NMODIFIED;
1813 VTONFS(vp)->n_attrstamp = 0;
1815 VTONFS(tdvp)->n_attrstamp = 0;
1817 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1819 if (error == EEXIST)
1825 * nfs symbolic link create call
1827 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1828 * struct componentname *a_cnp, struct vattr *a_vap,
1832 nfs_symlink(struct vop_symlink_args *ap)
1834 struct vnode *dvp = ap->a_dvp;
1835 struct vattr *vap = ap->a_vap;
1836 struct componentname *cnp = ap->a_cnp;
1837 struct nfsv2_sattr *sp;
1841 caddr_t bpos, dpos, cp2;
1842 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1843 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1844 struct vnode *newvp = (struct vnode *)0;
1845 int v3 = NFS_ISV3(dvp);
1847 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1848 slen = strlen(ap->a_target);
1849 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1850 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1851 nfsm_fhtom(dvp, v3);
1852 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1854 nfsm_v3attrbuild(vap, FALSE);
1856 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1858 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1859 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1860 sp->sa_uid = nfs_xdrneg1;
1861 sp->sa_gid = nfs_xdrneg1;
1862 sp->sa_size = nfs_xdrneg1;
1863 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1864 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1868 * Issue the NFS request and get the rpc response.
1870 * Only NFSv3 responses returning an error of 0 actually return
1871 * a file handle that can be converted into newvp without having
1872 * to do an extra lookup rpc.
1874 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1877 nfsm_mtofh(dvp, newvp, v3, gotvp);
1878 nfsm_wcc_data(dvp, wccflag);
1882 * out code jumps -> here, mrep is also freed.
1889 * If we get an EEXIST error, silently convert it to no-error
1890 * in case of an NFS retry.
1892 if (error == EEXIST)
1896 * If we do not have (or no longer have) an error, and we could
1897 * not extract the newvp from the response due to the request being
1898 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1899 * to obtain a newvp to return.
1901 if (error == 0 && newvp == NULL) {
1902 struct nfsnode *np = NULL;
1904 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1905 cnp->cn_cred, cnp->cn_td, &np);
1915 VTONFS(dvp)->n_flag |= NMODIFIED;
1917 VTONFS(dvp)->n_attrstamp = 0;
1924 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1925 * struct componentname *a_cnp, struct vattr *a_vap)
1928 nfs_mkdir(struct vop_mkdir_args *ap)
1930 struct vnode *dvp = ap->a_dvp;
1931 struct vattr *vap = ap->a_vap;
1932 struct componentname *cnp = ap->a_cnp;
1933 struct nfsv2_sattr *sp;
1938 struct nfsnode *np = (struct nfsnode *)0;
1939 struct vnode *newvp = (struct vnode *)0;
1940 caddr_t bpos, dpos, cp2;
1941 int error = 0, wccflag = NFSV3_WCCRATTR;
1943 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1945 int v3 = NFS_ISV3(dvp);
1947 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1950 len = cnp->cn_namelen;
1951 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1952 nfsm_reqhead(dvp, NFSPROC_MKDIR,
1953 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1954 nfsm_fhtom(dvp, v3);
1955 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1957 nfsm_v3attrbuild(vap, FALSE);
1959 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1960 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1961 sp->sa_uid = nfs_xdrneg1;
1962 sp->sa_gid = nfs_xdrneg1;
1963 sp->sa_size = nfs_xdrneg1;
1964 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1965 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1967 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
1969 nfsm_mtofh(dvp, newvp, v3, gotvp);
1971 nfsm_wcc_data(dvp, wccflag);
1974 VTONFS(dvp)->n_flag |= NMODIFIED;
1976 VTONFS(dvp)->n_attrstamp = 0;
1978 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1979 * if we can succeed in looking up the directory.
1981 if (error == EEXIST || (!error && !gotvp)) {
1984 newvp = (struct vnode *)0;
1986 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
1990 if (newvp->v_type != VDIR)
2003 * nfs remove directory call
2005 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2006 * struct componentname *a_cnp)
2009 nfs_rmdir(struct vop_rmdir_args *ap)
2011 struct vnode *vp = ap->a_vp;
2012 struct vnode *dvp = ap->a_dvp;
2013 struct componentname *cnp = ap->a_cnp;
2017 caddr_t bpos, dpos, cp2;
2018 int error = 0, wccflag = NFSV3_WCCRATTR;
2019 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2020 int v3 = NFS_ISV3(dvp);
2024 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2025 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2026 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2027 nfsm_fhtom(dvp, v3);
2028 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2029 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2031 nfsm_wcc_data(dvp, wccflag);
2034 VTONFS(dvp)->n_flag |= NMODIFIED;
2036 VTONFS(dvp)->n_attrstamp = 0;
2040 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2042 if (error == ENOENT)
2050 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2053 nfs_readdir(struct vop_readdir_args *ap)
2055 struct vnode *vp = ap->a_vp;
2056 struct nfsnode *np = VTONFS(vp);
2057 struct uio *uio = ap->a_uio;
2061 if (vp->v_type != VDIR)
2064 * First, check for hit on the EOF offset cache
2066 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2067 (np->n_flag & NMODIFIED) == 0) {
2068 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
2069 if (NQNFS_CKCACHABLE(vp, ND_READ)) {
2070 nfsstats.direofcache_hits++;
2073 } else if (VOP_GETATTR(vp, &vattr, uio->uio_td) == 0 &&
2074 np->n_mtime == vattr.va_mtime.tv_sec) {
2075 nfsstats.direofcache_hits++;
2081 * Call nfs_bioread() to do the real work.
2083 tresid = uio->uio_resid;
2084 error = nfs_bioread(vp, uio, 0);
2086 if (!error && uio->uio_resid == tresid)
2087 nfsstats.direofcache_misses++;
2093 * Called from below the buffer cache by nfs_doio().
2096 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2099 struct dirent *dp = NULL;
2104 caddr_t bpos, dpos, cp2;
2105 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2107 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2108 struct nfsnode *dnp = VTONFS(vp);
2110 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2112 int v3 = NFS_ISV3(vp);
2115 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2116 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2117 panic("nfs readdirrpc bad uio");
2121 * If there is no cookie, assume directory was stale.
2123 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2127 return (NFSERR_BAD_COOKIE);
2129 * Loop around doing readdir rpc's of size nm_readdirsize
2130 * truncated to a multiple of DIRBLKSIZ.
2131 * The stopping criteria is EOF or buffer full.
2133 while (more_dirs && bigenough) {
2134 nfsstats.rpccnt[NFSPROC_READDIR]++;
2135 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2139 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2140 *tl++ = cookie.nfsuquad[0];
2141 *tl++ = cookie.nfsuquad[1];
2142 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2143 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2145 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2146 *tl++ = cookie.nfsuquad[0];
2148 *tl = txdr_unsigned(nmp->nm_readdirsize);
2149 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2151 nfsm_postop_attr(vp, attrflag);
2153 nfsm_dissect(tl, u_int32_t *,
2155 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2156 dnp->n_cookieverf.nfsuquad[1] = *tl;
2162 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2163 more_dirs = fxdr_unsigned(int, *tl);
2165 /* loop thru the dir entries, doctoring them to 4bsd form */
2166 while (more_dirs && bigenough) {
2168 nfsm_dissect(tl, u_int32_t *,
2170 fileno = fxdr_hyper(tl);
2171 len = fxdr_unsigned(int, *(tl + 2));
2173 nfsm_dissect(tl, u_int32_t *,
2175 fileno = fxdr_unsigned(u_quad_t, *tl++);
2176 len = fxdr_unsigned(int, *tl);
2178 if (len <= 0 || len > NFS_MAXNAMLEN) {
2183 tlen = nfsm_rndup(len);
2185 tlen += 4; /* To ensure null termination */
2186 left = DIRBLKSIZ - blksiz;
2187 if ((tlen + DIRHDSIZ) > left) {
2188 dp->d_reclen += left;
2189 uiop->uio_iov->iov_base += left;
2190 uiop->uio_iov->iov_len -= left;
2191 uiop->uio_offset += left;
2192 uiop->uio_resid -= left;
2195 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2198 dp = (struct dirent *)uiop->uio_iov->iov_base;
2199 dp->d_fileno = (int)fileno;
2201 dp->d_reclen = tlen + DIRHDSIZ;
2202 dp->d_type = DT_UNKNOWN;
2203 blksiz += dp->d_reclen;
2204 if (blksiz == DIRBLKSIZ)
2206 uiop->uio_offset += DIRHDSIZ;
2207 uiop->uio_resid -= DIRHDSIZ;
2208 uiop->uio_iov->iov_base += DIRHDSIZ;
2209 uiop->uio_iov->iov_len -= DIRHDSIZ;
2210 nfsm_mtouio(uiop, len);
2211 cp = uiop->uio_iov->iov_base;
2213 *cp = '\0'; /* null terminate */
2214 uiop->uio_iov->iov_base += tlen;
2215 uiop->uio_iov->iov_len -= tlen;
2216 uiop->uio_offset += tlen;
2217 uiop->uio_resid -= tlen;
2219 nfsm_adv(nfsm_rndup(len));
2221 nfsm_dissect(tl, u_int32_t *,
2224 nfsm_dissect(tl, u_int32_t *,
2228 cookie.nfsuquad[0] = *tl++;
2230 cookie.nfsuquad[1] = *tl++;
2235 more_dirs = fxdr_unsigned(int, *tl);
2238 * If at end of rpc data, get the eof boolean
2241 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2242 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2247 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2248 * by increasing d_reclen for the last record.
2251 left = DIRBLKSIZ - blksiz;
2252 dp->d_reclen += left;
2253 uiop->uio_iov->iov_base += left;
2254 uiop->uio_iov->iov_len -= left;
2255 uiop->uio_offset += left;
2256 uiop->uio_resid -= left;
2260 * We are now either at the end of the directory or have filled the
2264 dnp->n_direofoffset = uiop->uio_offset;
2266 if (uiop->uio_resid > 0)
2267 printf("EEK! readdirrpc resid > 0\n");
2268 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2276 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2279 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2286 struct vnode *newvp;
2288 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2289 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2290 struct nameidata nami, *ndp = &nami;
2291 struct componentname *cnp = &ndp->ni_cnd;
2293 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2294 struct nfsnode *dnp = VTONFS(vp), *np;
2297 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2298 int attrflag, fhsize;
2301 dp = (struct dirent *)0;
2304 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2305 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2306 panic("nfs readdirplusrpc bad uio");
2312 * If there is no cookie, assume directory was stale.
2314 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2318 return (NFSERR_BAD_COOKIE);
2320 * Loop around doing readdir rpc's of size nm_readdirsize
2321 * truncated to a multiple of DIRBLKSIZ.
2322 * The stopping criteria is EOF or buffer full.
2324 while (more_dirs && bigenough) {
2325 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2326 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2327 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2329 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2330 *tl++ = cookie.nfsuquad[0];
2331 *tl++ = cookie.nfsuquad[1];
2332 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2333 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2334 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2335 *tl = txdr_unsigned(nmp->nm_rsize);
2336 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2337 nfsm_postop_attr(vp, attrflag);
2342 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2343 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2344 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2345 more_dirs = fxdr_unsigned(int, *tl);
2347 /* loop thru the dir entries, doctoring them to 4bsd form */
2348 while (more_dirs && bigenough) {
2349 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2350 fileno = fxdr_hyper(tl);
2351 len = fxdr_unsigned(int, *(tl + 2));
2352 if (len <= 0 || len > NFS_MAXNAMLEN) {
2357 tlen = nfsm_rndup(len);
2359 tlen += 4; /* To ensure null termination*/
2360 left = DIRBLKSIZ - blksiz;
2361 if ((tlen + DIRHDSIZ) > left) {
2362 dp->d_reclen += left;
2363 uiop->uio_iov->iov_base += left;
2364 uiop->uio_iov->iov_len -= left;
2365 uiop->uio_offset += left;
2366 uiop->uio_resid -= left;
2369 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2372 dp = (struct dirent *)uiop->uio_iov->iov_base;
2373 dp->d_fileno = (int)fileno;
2375 dp->d_reclen = tlen + DIRHDSIZ;
2376 dp->d_type = DT_UNKNOWN;
2377 blksiz += dp->d_reclen;
2378 if (blksiz == DIRBLKSIZ)
2380 uiop->uio_offset += DIRHDSIZ;
2381 uiop->uio_resid -= DIRHDSIZ;
2382 uiop->uio_iov->iov_base += DIRHDSIZ;
2383 uiop->uio_iov->iov_len -= DIRHDSIZ;
2384 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2385 cnp->cn_namelen = len;
2386 nfsm_mtouio(uiop, len);
2387 cp = uiop->uio_iov->iov_base;
2390 uiop->uio_iov->iov_base += tlen;
2391 uiop->uio_iov->iov_len -= tlen;
2392 uiop->uio_offset += tlen;
2393 uiop->uio_resid -= tlen;
2395 nfsm_adv(nfsm_rndup(len));
2396 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2398 cookie.nfsuquad[0] = *tl++;
2399 cookie.nfsuquad[1] = *tl++;
2404 * Since the attributes are before the file handle
2405 * (sigh), we must skip over the attributes and then
2406 * come back and get them.
2408 attrflag = fxdr_unsigned(int, *tl);
2412 nfsm_adv(NFSX_V3FATTR);
2413 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2414 doit = fxdr_unsigned(int, *tl);
2416 nfsm_getfh(fhp, fhsize, 1);
2417 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2422 error = nfs_nget(vp->v_mount, fhp,
2430 if (doit && bigenough) {
2435 nfsm_loadattr(newvp, (struct vattr *)0);
2439 IFTODT(VTTOIF(np->n_vattr.va_type));
2441 cache_enter(ndp->ni_dvp, NCPNULL, ndp->ni_vp, cnp);
2444 /* Just skip over the file handle */
2445 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2446 i = fxdr_unsigned(int, *tl);
2447 nfsm_adv(nfsm_rndup(i));
2449 if (newvp != NULLVP) {
2456 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2457 more_dirs = fxdr_unsigned(int, *tl);
2460 * If at end of rpc data, get the eof boolean
2463 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2464 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2469 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2470 * by increasing d_reclen for the last record.
2473 left = DIRBLKSIZ - blksiz;
2474 dp->d_reclen += left;
2475 uiop->uio_iov->iov_base += left;
2476 uiop->uio_iov->iov_len -= left;
2477 uiop->uio_offset += left;
2478 uiop->uio_resid -= left;
2482 * We are now either at the end of the directory or have filled the
2486 dnp->n_direofoffset = uiop->uio_offset;
2488 if (uiop->uio_resid > 0)
2489 printf("EEK! readdirplusrpc resid > 0\n");
2490 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2494 if (newvp != NULLVP) {
2505 * Silly rename. To make the NFS filesystem that is stateless look a little
2506 * more like the "ufs" a remove of an active vnode is translated to a rename
2507 * to a funny looking filename that is removed by nfs_inactive on the
2508 * nfsnode. There is the potential for another process on a different client
2509 * to create the same funny name between the nfs_lookitup() fails and the
2510 * nfs_rename() completes, but...
2513 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2515 struct sillyrename *sp;
2522 if (vp->v_type == VDIR)
2523 panic("nfs: sillyrename dir");
2525 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2526 M_NFSREQ, M_WAITOK);
2527 sp->s_cred = crdup(cnp->cn_cred);
2531 /* Fudge together a funny name */
2532 sp->s_namlen = sprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
2534 /* Try lookitups until we get one that isn't there */
2535 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2536 cnp->cn_td, (struct nfsnode **)0) == 0) {
2538 if (sp->s_name[4] > 'z') {
2543 error = nfs_renameit(dvp, cnp, sp);
2546 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2548 np->n_sillyrename = sp;
2553 free((caddr_t)sp, M_NFSREQ);
2558 * Look up a file name and optionally either update the file handle or
2559 * allocate an nfsnode, depending on the value of npp.
2560 * npp == NULL --> just do the lookup
2561 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2563 * *npp != NULL --> update the file handle in the vnode
2566 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2567 struct thread *td, struct nfsnode **npp)
2572 struct vnode *newvp = (struct vnode *)0;
2573 struct nfsnode *np, *dnp = VTONFS(dvp);
2574 caddr_t bpos, dpos, cp2;
2575 int error = 0, fhlen, attrflag;
2576 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2578 int v3 = NFS_ISV3(dvp);
2580 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2581 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2582 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2583 nfsm_fhtom(dvp, v3);
2584 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2585 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2586 if (npp && !error) {
2587 nfsm_getfh(nfhp, fhlen, v3);
2590 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2591 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2592 np->n_fhp = &np->n_fh;
2593 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2594 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2595 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2596 np->n_fhsize = fhlen;
2598 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2602 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2610 nfsm_postop_attr(newvp, attrflag);
2611 if (!attrflag && *npp == NULL) {
2620 nfsm_loadattr(newvp, (struct vattr *)0);
2624 if (npp && *npp == NULL) {
2639 * Nfs Version 3 commit rpc
2642 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2647 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2648 caddr_t bpos, dpos, cp2;
2649 int error = 0, wccflag = NFSV3_WCCRATTR;
2650 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2652 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2654 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2655 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2657 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2658 txdr_hyper(offset, tl);
2660 *tl = txdr_unsigned(cnt);
2661 nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2662 nfsm_wcc_data(vp, wccflag);
2664 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2665 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2666 NFSX_V3WRITEVERF)) {
2667 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2669 error = NFSERR_STALEWRITEVERF;
2679 * - make nfs_bmap() essentially a no-op that does no translation
2680 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2681 * (Maybe I could use the process's page mapping, but I was concerned that
2682 * Kernel Write might not be enabled and also figured copyout() would do
2683 * a lot more work than bcopy() and also it currently happens in the
2684 * context of the swapper process (2).
2686 * nfs_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
2687 * daddr_t *a_bnp, int *a_runp, int *a_runb)
2690 nfs_bmap(struct vop_bmap_args *ap)
2692 struct vnode *vp = ap->a_vp;
2694 if (ap->a_vpp != NULL)
2696 if (ap->a_bnp != NULL)
2697 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2698 if (ap->a_runp != NULL)
2700 if (ap->a_runb != NULL)
2707 * For async requests when nfsiod(s) are running, queue the request by
2708 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2712 nfs_strategy(struct vop_strategy_args *ap)
2714 struct buf *bp = ap->a_bp;
2718 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2719 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2721 if (bp->b_flags & B_PHYS)
2722 panic("nfs physio");
2724 if (bp->b_flags & B_ASYNC)
2727 td = curthread; /* XXX */
2730 * If the op is asynchronous and an i/o daemon is waiting
2731 * queue the request, wake it up and wait for completion
2732 * otherwise just do it ourselves.
2734 if ((bp->b_flags & B_ASYNC) == 0 ||
2735 nfs_asyncio(bp, td))
2736 error = nfs_doio(bp, td);
2743 * NB Currently unsupported.
2745 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
2746 * struct thread *a_td)
2750 nfs_mmap(struct vop_mmap_args *ap)
2756 * fsync vnode op. Just call nfs_flush() with commit == 1.
2758 * nfs_fsync(struct vnodeop_desc *a_desc, struct vnode *a_vp,
2759 * struct ucred * a_cred, int a_waitfor, struct thread *a_td)
2763 nfs_fsync(struct vop_fsync_args *ap)
2765 return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2769 * Flush all the blocks associated with a vnode.
2770 * Walk through the buffer pool and push any dirty pages
2771 * associated with the vnode.
2774 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
2776 struct nfsnode *np = VTONFS(vp);
2780 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2781 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2783 u_quad_t off, endoff, toff;
2784 struct buf **bvec = NULL;
2785 #ifndef NFS_COMMITBVECSIZ
2786 #define NFS_COMMITBVECSIZ 20
2788 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2789 int bvecsize = 0, bveccount;
2791 if (nmp->nm_flag & NFSMNT_INT)
2796 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2797 * server, but nas not been committed to stable storage on the server
2798 * yet. On the first pass, the byte range is worked out and the commit
2799 * rpc is done. On the second pass, nfs_writebp() is called to do the
2806 if (NFS_ISV3(vp) && commit) {
2809 * Count up how many buffers waiting for a commit.
2812 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2813 nbp = TAILQ_NEXT(bp, b_vnbufs);
2814 if (BUF_REFCNT(bp) == 0 &&
2815 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2816 == (B_DELWRI | B_NEEDCOMMIT))
2820 * Allocate space to remember the list of bufs to commit. It is
2821 * important to use M_NOWAIT here to avoid a race with nfs_write.
2822 * If we can't get memory (for whatever reason), we will end up
2823 * committing the buffers one-by-one in the loop below.
2825 if (bvec != NULL && bvec != bvec_on_stack)
2827 if (bveccount > NFS_COMMITBVECSIZ) {
2828 bvec = (struct buf **)
2829 malloc(bveccount * sizeof(struct buf *),
2832 bvec = bvec_on_stack;
2833 bvecsize = NFS_COMMITBVECSIZ;
2835 bvecsize = bveccount;
2837 bvec = bvec_on_stack;
2838 bvecsize = NFS_COMMITBVECSIZ;
2840 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2841 nbp = TAILQ_NEXT(bp, b_vnbufs);
2842 if (bvecpos >= bvecsize)
2844 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2845 (B_DELWRI | B_NEEDCOMMIT) ||
2846 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
2850 * NOTE: we are not clearing B_DONE here, so we have
2851 * to do it later on in this routine if we intend to
2852 * initiate I/O on the bp.
2854 * Note: to avoid loopback deadlocks, we do not
2855 * assign b_runningbufspace.
2857 bp->b_flags |= B_WRITEINPROG;
2858 vfs_busy_pages(bp, 1);
2861 * bp is protected by being locked, but nbp is not
2862 * and vfs_busy_pages() may sleep. We have to
2865 nbp = TAILQ_NEXT(bp, b_vnbufs);
2868 * A list of these buffers is kept so that the
2869 * second loop knows which buffers have actually
2870 * been committed. This is necessary, since there
2871 * may be a race between the commit rpc and new
2872 * uncommitted writes on the file.
2874 bvec[bvecpos++] = bp;
2875 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2879 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2887 * Commit data on the server, as required. Note that
2888 * nfs_commit will use the vnode's cred for the commit.
2890 retv = nfs_commit(vp, off, (int)(endoff - off), td);
2892 if (retv == NFSERR_STALEWRITEVERF)
2893 nfs_clearcommit(vp->v_mount);
2896 * Now, either mark the blocks I/O done or mark the
2897 * blocks dirty, depending on whether the commit
2900 for (i = 0; i < bvecpos; i++) {
2902 bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG | B_CLUSTEROK);
2905 * Error, leave B_DELWRI intact
2907 vfs_unbusy_pages(bp);
2911 * Success, remove B_DELWRI ( bundirty() ).
2913 * b_dirtyoff/b_dirtyend seem to be NFS
2914 * specific. We should probably move that
2915 * into bundirty(). XXX
2919 bp->b_flags |= B_ASYNC;
2921 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
2922 bp->b_dirtyoff = bp->b_dirtyend = 0;
2930 * Start/do any write(s) that are required.
2934 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2935 nbp = TAILQ_NEXT(bp, b_vnbufs);
2936 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
2937 if (waitfor != MNT_WAIT || passone)
2939 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL,
2940 "nfsfsync", slpflag, slptimeo);
2943 panic("nfs_fsync: inconsistent lock");
2944 if (error == ENOLCK)
2946 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
2950 if (slpflag == PCATCH) {
2956 if ((bp->b_flags & B_DELWRI) == 0)
2957 panic("nfs_fsync: not dirty");
2958 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2963 if (passone || !commit)
2964 bp->b_flags |= B_ASYNC;
2966 bp->b_flags |= B_ASYNC | B_WRITEINPROG;
2968 VOP_BWRITE(bp->b_vp, bp);
2976 if (waitfor == MNT_WAIT) {
2977 while (vp->v_numoutput) {
2978 vp->v_flag |= VBWAIT;
2979 error = tsleep((caddr_t)&vp->v_numoutput,
2980 slpflag, "nfsfsync", slptimeo);
2982 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
2986 if (slpflag == PCATCH) {
2992 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) && commit) {
2996 if (np->n_flag & NWRITEERR) {
2997 error = np->n_error;
2998 np->n_flag &= ~NWRITEERR;
3001 if (bvec != NULL && bvec != bvec_on_stack)
3007 * NFS advisory byte-level locks.
3008 * Currently unsupported.
3010 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3014 nfs_advlock(struct vop_advlock_args *ap)
3016 struct nfsnode *np = VTONFS(ap->a_vp);
3019 * The following kludge is to allow diskless support to work
3020 * until a real NFS lockd is implemented. Basically, just pretend
3021 * that this is a local lock.
3023 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3027 * Print out the contents of an nfsnode.
3029 * nfs_print(struct vnode *a_vp)
3032 nfs_print(struct vop_print_args *ap)
3034 struct vnode *vp = ap->a_vp;
3035 struct nfsnode *np = VTONFS(vp);
3037 printf("tag VT_NFS, fileid %ld fsid 0x%x",
3038 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3039 if (vp->v_type == VFIFO)
3046 * Just call nfs_writebp() with the force argument set to 1.
3048 * NOTE: B_DONE may or may not be set in a_bp on call.
3050 * nfs_bwrite(struct vnode *a_bp)
3053 nfs_bwrite(struct vop_bwrite_args *ap)
3055 return (nfs_writebp(ap->a_bp, 1, curthread));
3059 * This is a clone of vn_bwrite(), except that B_WRITEINPROG isn't set unless
3060 * the force flag is one and it also handles the B_NEEDCOMMIT flag. We set
3061 * B_CACHE if this is a VMIO buffer.
3064 nfs_writebp(struct buf *bp, int force, struct thread *td)
3067 int oldflags = bp->b_flags;
3073 if (BUF_REFCNT(bp) == 0)
3074 panic("bwrite: buffer is not locked???");
3076 if (bp->b_flags & B_INVAL) {
3081 bp->b_flags |= B_CACHE;
3084 * Undirty the bp. We will redirty it later if the I/O fails.
3089 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3091 bp->b_vp->v_numoutput++;
3095 * Note: to avoid loopback deadlocks, we do not
3096 * assign b_runningbufspace.
3098 vfs_busy_pages(bp, 1);
3101 bp->b_flags |= B_WRITEINPROG;
3103 VOP_STRATEGY(bp->b_vp, bp);
3105 if( (oldflags & B_ASYNC) == 0) {
3106 int rtval = biowait(bp);
3108 if (oldflags & B_DELWRI) {
3110 reassignbuf(bp, bp->b_vp);
3122 * nfs special file access vnode op.
3123 * Essentially just get vattr and then imitate iaccess() since the device is
3124 * local to the client.
3126 * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
3127 * struct thread *a_td)
3130 nfsspec_access(struct vop_access_args *ap)
3134 struct ucred *cred = ap->a_cred;
3135 struct vnode *vp = ap->a_vp;
3136 mode_t mode = ap->a_mode;
3142 * Disallow write attempts on filesystems mounted read-only;
3143 * unless the file is a socket, fifo, or a block or character
3144 * device resident on the filesystem.
3146 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3147 switch (vp->v_type) {
3157 * If you're the super-user,
3158 * you always get access.
3160 if (cred->cr_uid == 0)
3163 error = VOP_GETATTR(vp, vap, ap->a_td);
3167 * Access check is based on only one of owner, group, public.
3168 * If not owner, then check group. If not a member of the
3169 * group, then check public access.
3171 if (cred->cr_uid != vap->va_uid) {
3173 gp = cred->cr_groups;
3174 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3175 if (vap->va_gid == *gp)
3181 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3186 * Read wrapper for special devices.
3188 * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3189 * struct ucred *a_cred)
3192 nfsspec_read(struct vop_read_args *ap)
3194 struct nfsnode *np = VTONFS(ap->a_vp);
3200 getnanotime(&np->n_atim);
3201 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3205 * Write wrapper for special devices.
3207 * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3208 * struct ucred *a_cred)
3211 nfsspec_write(struct vop_write_args *ap)
3213 struct nfsnode *np = VTONFS(ap->a_vp);
3219 getnanotime(&np->n_mtim);
3220 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3224 * Close wrapper for special devices.
3226 * Update the times on the nfsnode then do device close.
3228 * nfsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
3229 * struct thread *a_td)
3232 nfsspec_close(struct vop_close_args *ap)
3234 struct vnode *vp = ap->a_vp;
3235 struct nfsnode *np = VTONFS(vp);
3238 if (np->n_flag & (NACC | NUPD)) {
3240 if (vp->v_usecount == 1 &&
3241 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3243 if (np->n_flag & NACC)
3244 vattr.va_atime = np->n_atim;
3245 if (np->n_flag & NUPD)
3246 vattr.va_mtime = np->n_mtim;
3247 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3250 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3254 * Read wrapper for fifos.
3256 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3257 * struct ucred *a_cred)
3260 nfsfifo_read(struct vop_read_args *ap)
3262 struct nfsnode *np = VTONFS(ap->a_vp);
3268 getnanotime(&np->n_atim);
3269 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3273 * Write wrapper for fifos.
3275 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3276 * struct ucred *a_cred)
3279 nfsfifo_write(struct vop_write_args *ap)
3281 struct nfsnode *np = VTONFS(ap->a_vp);
3287 getnanotime(&np->n_mtim);
3288 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3292 * Close wrapper for fifos.
3294 * Update the times on the nfsnode then do fifo close.
3296 * nfsfifo_close(struct vnode *a_vp, int a_fflag, struct thread *a_td)
3299 nfsfifo_close(struct vop_close_args *ap)
3301 struct vnode *vp = ap->a_vp;
3302 struct nfsnode *np = VTONFS(vp);
3306 if (np->n_flag & (NACC | NUPD)) {
3308 if (np->n_flag & NACC)
3310 if (np->n_flag & NUPD)
3313 if (vp->v_usecount == 1 &&
3314 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3316 if (np->n_flag & NACC)
3317 vattr.va_atime = np->n_atim;
3318 if (np->n_flag & NUPD)
3319 vattr.va_mtime = np->n_mtim;
3320 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3323 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));