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.24 2004/06/04 05:06:40 hmp 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;
779 struct nfsnode *np = VTONFS(vp);
782 caddr_t bpos, dpos, cp2;
784 int error = 0, wccflag = NFSV3_WCCRATTR;
785 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
786 int v3 = NFS_ISV3(vp);
788 nfsstats.rpccnt[NFSPROC_SETATTR]++;
789 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
792 nfsm_v3attrbuild(vap, TRUE);
793 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
796 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
797 if (vap->va_mode == (mode_t)VNOVAL)
798 sp->sa_mode = nfs_xdrneg1;
800 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
801 if (vap->va_uid == (uid_t)VNOVAL)
802 sp->sa_uid = nfs_xdrneg1;
804 sp->sa_uid = txdr_unsigned(vap->va_uid);
805 if (vap->va_gid == (gid_t)VNOVAL)
806 sp->sa_gid = nfs_xdrneg1;
808 sp->sa_gid = txdr_unsigned(vap->va_gid);
809 sp->sa_size = txdr_unsigned(vap->va_size);
810 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
811 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
813 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
816 nfsm_wcc_data(vp, wccflag);
818 nfsm_loadattr(vp, (struct vattr *)0);
825 * 'cached' nfs directory lookup
827 * nfs_lookup(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
828 * struct vnode **a_vpp, struct componentname *a_cnp)
831 nfs_lookup(struct vop_lookup_args *ap)
833 struct componentname *cnp = ap->a_cnp;
834 struct vnode *dvp = ap->a_dvp;
835 struct vnode **vpp = ap->a_vpp;
836 int flags = cnp->cn_flags;
841 struct nfsmount *nmp;
842 caddr_t bpos, dpos, cp2;
843 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
847 int lockparent, wantparent, error = 0, attrflag, fhsize;
848 int v3 = NFS_ISV3(dvp);
849 struct thread *td = cnp->cn_td;
852 * Read-only mount check and directory check.
855 if ((flags & CNP_ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
856 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
859 if (dvp->v_type != VDIR)
863 * Look it up in the cache. Note that ENOENT is only returned if we
864 * previously entered a negative hit (see later on). The additional
865 * nfsneg_cache_timeout check causes previously cached results to
866 * be instantly ignored if the negative caching is turned off.
868 lockparent = flags & CNP_LOCKPARENT;
869 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
870 nmp = VFSTONFS(dvp->v_mount);
872 error = cache_lookup(dvp, NCPNULL, vpp, NCPPNULL, cnp);
877 if (error == ENOENT && nfsneg_cache_timeout) {
881 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
889 * See the comment starting `Step through' in ufs/ufs_lookup.c
890 * for an explanation of the locking protocol
895 } else if (flags & CNP_ISDOTDOT) {
896 VOP_UNLOCK(dvp, NULL, 0, td);
897 error = vget(newvp, NULL, LK_EXCLUSIVE, td);
898 if (!error && lockparent && (flags & CNP_ISLASTCN))
899 error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td);
901 error = vget(newvp, NULL, LK_EXCLUSIVE, td);
902 if (!lockparent || error || !(flags & CNP_ISLASTCN))
903 VOP_UNLOCK(dvp, NULL, 0, td);
906 if (vpid == newvp->v_id) {
907 if (!VOP_GETATTR(newvp, &vattr, td)
908 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
909 nfsstats.lookupcache_hits++;
910 if (cnp->cn_nameiop != NAMEI_LOOKUP &&
911 (flags & CNP_ISLASTCN))
912 cnp->cn_flags |= CNP_SAVENAME;
918 if (lockparent && dvp != newvp && (flags & CNP_ISLASTCN))
919 VOP_UNLOCK(dvp, NULL, 0, td);
921 error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td);
928 * Cache miss, go the wire.
932 nfsstats.lookupcache_misses++;
933 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
934 len = cnp->cn_namelen;
935 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
936 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
938 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
939 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
942 * Cache negatve lookups to reduce NFS traffic, but use
945 if (error == ENOENT &&
946 (cnp->cn_flags & CNP_MAKEENTRY) &&
947 cnp->cn_nameiop == NAMEI_LOOKUP &&
948 nfsneg_cache_timeout) {
949 int toval = nfsneg_cache_timeout * hz;
950 if (cnp->cn_flags & CNP_CACHETIMEOUT) {
951 if (cnp->cn_timeout > toval)
952 cnp->cn_timeout = toval;
954 cnp->cn_flags |= CNP_CACHETIMEOUT;
955 cnp->cn_timeout = toval;
957 cache_enter(dvp, NCPNULL, NULL, cnp);
959 nfsm_postop_attr(dvp, attrflag);
963 nfsm_getfh(fhp, fhsize, v3);
966 * Handle RENAME case...
968 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent && (flags & CNP_ISLASTCN)) {
969 if (NFS_CMPFH(np, fhp, fhsize)) {
973 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
980 nfsm_postop_attr(newvp, attrflag);
981 nfsm_postop_attr(dvp, attrflag);
983 nfsm_loadattr(newvp, (struct vattr *)0);
986 cnp->cn_flags |= CNP_SAVENAME;
988 VOP_UNLOCK(dvp, NULL, 0, td);
992 if (flags & CNP_ISDOTDOT) {
993 VOP_UNLOCK(dvp, NULL, 0, td);
994 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
996 vn_lock(dvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1000 if (lockparent && (flags & CNP_ISLASTCN) &&
1001 (error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td))) {
1005 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1009 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1014 if (!lockparent || !(flags & CNP_ISLASTCN))
1015 VOP_UNLOCK(dvp, NULL, 0, td);
1019 nfsm_postop_attr(newvp, attrflag);
1020 nfsm_postop_attr(dvp, attrflag);
1022 nfsm_loadattr(newvp, (struct vattr *)0);
1023 if (cnp->cn_nameiop != NAMEI_LOOKUP && (flags & CNP_ISLASTCN))
1024 cnp->cn_flags |= CNP_SAVENAME;
1025 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1026 (cnp->cn_nameiop != NAMEI_DELETE || !(flags & CNP_ISLASTCN))) {
1027 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
1028 cache_enter(dvp, NCPNULL, newvp, cnp);
1034 if (newvp != NULLVP) {
1038 if ((cnp->cn_nameiop == NAMEI_CREATE || cnp->cn_nameiop == NAMEI_RENAME) &&
1039 (flags & CNP_ISLASTCN) && error == ENOENT) {
1041 VOP_UNLOCK(dvp, NULL, 0, td);
1042 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1045 error = EJUSTRETURN;
1047 if (cnp->cn_nameiop != NAMEI_LOOKUP && (flags & CNP_ISLASTCN))
1048 cnp->cn_flags |= CNP_SAVENAME;
1055 * Just call nfs_bioread() to do the work.
1057 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1058 * struct ucred *a_cred)
1061 nfs_read(struct vop_read_args *ap)
1063 struct vnode *vp = ap->a_vp;
1065 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1066 switch (vp->v_type) {
1068 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1079 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1082 nfs_readlink(struct vop_readlink_args *ap)
1084 struct vnode *vp = ap->a_vp;
1086 if (vp->v_type != VLNK)
1088 return (nfs_bioread(vp, ap->a_uio, 0));
1092 * Do a readlink rpc.
1093 * Called by nfs_doio() from below the buffer cache.
1096 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1101 caddr_t bpos, dpos, cp2;
1102 int error = 0, len, attrflag;
1103 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1104 int v3 = NFS_ISV3(vp);
1106 nfsstats.rpccnt[NFSPROC_READLINK]++;
1107 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1109 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1111 nfsm_postop_attr(vp, attrflag);
1113 nfsm_strsiz(len, NFS_MAXPATHLEN);
1114 if (len == NFS_MAXPATHLEN) {
1115 struct nfsnode *np = VTONFS(vp);
1116 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1119 nfsm_mtouio(uiop, len);
1131 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1136 caddr_t bpos, dpos, cp2;
1137 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1138 struct nfsmount *nmp;
1139 int error = 0, len, retlen, tsiz, eof, attrflag;
1140 int v3 = NFS_ISV3(vp);
1145 nmp = VFSTONFS(vp->v_mount);
1146 tsiz = uiop->uio_resid;
1147 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1150 nfsstats.rpccnt[NFSPROC_READ]++;
1151 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1152 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1154 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1156 txdr_hyper(uiop->uio_offset, tl);
1157 *(tl + 2) = txdr_unsigned(len);
1159 *tl++ = txdr_unsigned(uiop->uio_offset);
1160 *tl++ = txdr_unsigned(len);
1163 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1165 nfsm_postop_attr(vp, attrflag);
1170 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1171 eof = fxdr_unsigned(int, *(tl + 1));
1173 nfsm_loadattr(vp, (struct vattr *)0);
1174 nfsm_strsiz(retlen, nmp->nm_rsize);
1175 nfsm_mtouio(uiop, retlen);
1179 if (eof || retlen == 0) {
1182 } else if (retlen < len) {
1194 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, int *must_commit)
1198 int32_t t1, t2, backup;
1199 caddr_t bpos, dpos, cp2;
1200 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1201 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1202 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1203 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1206 if (uiop->uio_iovcnt != 1)
1207 panic("nfs: writerpc iovcnt > 1");
1210 tsiz = uiop->uio_resid;
1211 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1214 nfsstats.rpccnt[NFSPROC_WRITE]++;
1215 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1216 nfsm_reqhead(vp, NFSPROC_WRITE,
1217 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1220 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1221 txdr_hyper(uiop->uio_offset, tl);
1223 *tl++ = txdr_unsigned(len);
1224 *tl++ = txdr_unsigned(*iomode);
1225 *tl = txdr_unsigned(len);
1229 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1230 /* Set both "begin" and "current" to non-garbage. */
1231 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1232 *tl++ = x; /* "begin offset" */
1233 *tl++ = x; /* "current offset" */
1234 x = txdr_unsigned(len);
1235 *tl++ = x; /* total to this offset */
1236 *tl = x; /* size of this write */
1238 nfsm_uiotom(uiop, len);
1239 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1241 wccflag = NFSV3_WCCCHK;
1242 nfsm_wcc_data(vp, wccflag);
1244 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1245 + NFSX_V3WRITEVERF);
1246 rlen = fxdr_unsigned(int, *tl++);
1251 } else if (rlen < len) {
1252 backup = len - rlen;
1253 uiop->uio_iov->iov_base -= backup;
1254 uiop->uio_iov->iov_len += backup;
1255 uiop->uio_offset -= backup;
1256 uiop->uio_resid += backup;
1259 commit = fxdr_unsigned(int, *tl++);
1262 * Return the lowest committment level
1263 * obtained by any of the RPCs.
1265 if (committed == NFSV3WRITE_FILESYNC)
1267 else if (committed == NFSV3WRITE_DATASYNC &&
1268 commit == NFSV3WRITE_UNSTABLE)
1270 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1271 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1273 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1274 } else if (bcmp((caddr_t)tl,
1275 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1277 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1282 nfsm_loadattr(vp, (struct vattr *)0);
1284 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime.tv_sec;
1291 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1292 committed = NFSV3WRITE_FILESYNC;
1293 *iomode = committed;
1295 uiop->uio_resid = tsiz;
1301 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1302 * mode set to specify the file type and the size field for rdev.
1305 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1308 struct nfsv2_sattr *sp;
1312 struct vnode *newvp = (struct vnode *)0;
1313 struct nfsnode *np = (struct nfsnode *)0;
1317 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1318 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1320 int v3 = NFS_ISV3(dvp);
1322 if (vap->va_type == VCHR || vap->va_type == VBLK)
1323 rdev = txdr_unsigned(vap->va_rdev);
1324 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1327 return (EOPNOTSUPP);
1329 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1332 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1333 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1334 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1335 nfsm_fhtom(dvp, v3);
1336 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1338 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1339 *tl++ = vtonfsv3_type(vap->va_type);
1340 nfsm_v3attrbuild(vap, FALSE);
1341 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1342 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1343 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1344 *tl = txdr_unsigned(uminor(vap->va_rdev));
1347 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1348 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1349 sp->sa_uid = nfs_xdrneg1;
1350 sp->sa_gid = nfs_xdrneg1;
1352 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1353 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1355 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1357 nfsm_mtofh(dvp, newvp, v3, gotvp);
1361 newvp = (struct vnode *)0;
1363 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1364 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1370 nfsm_wcc_data(dvp, wccflag);
1377 if (cnp->cn_flags & CNP_MAKEENTRY)
1378 cache_enter(dvp, NCPNULL, newvp, cnp);
1381 VTONFS(dvp)->n_flag |= NMODIFIED;
1383 VTONFS(dvp)->n_attrstamp = 0;
1389 * just call nfs_mknodrpc() to do the work.
1391 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1392 * struct componentname *a_cnp, struct vattr *a_vap)
1396 nfs_mknod(struct vop_mknod_args *ap)
1398 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1401 static u_long create_verf;
1403 * nfs file create call
1405 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1406 * struct componentname *a_cnp, struct vattr *a_vap)
1409 nfs_create(struct vop_create_args *ap)
1411 struct vnode *dvp = ap->a_dvp;
1412 struct vattr *vap = ap->a_vap;
1413 struct componentname *cnp = ap->a_cnp;
1414 struct nfsv2_sattr *sp;
1418 struct nfsnode *np = (struct nfsnode *)0;
1419 struct vnode *newvp = (struct vnode *)0;
1420 caddr_t bpos, dpos, cp2;
1421 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1422 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1424 int v3 = NFS_ISV3(dvp);
1427 * Oops, not for me..
1429 if (vap->va_type == VSOCK)
1430 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1432 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1435 if (vap->va_vaflags & VA_EXCLUSIVE)
1438 nfsstats.rpccnt[NFSPROC_CREATE]++;
1439 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1440 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1441 nfsm_fhtom(dvp, v3);
1442 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1444 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1445 if (fmode & O_EXCL) {
1446 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1447 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1449 if (!TAILQ_EMPTY(&in_ifaddrhead))
1450 *tl++ = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr.s_addr;
1453 *tl++ = create_verf;
1454 *tl = ++create_verf;
1456 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1457 nfsm_v3attrbuild(vap, FALSE);
1460 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1461 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1462 sp->sa_uid = nfs_xdrneg1;
1463 sp->sa_gid = nfs_xdrneg1;
1465 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1466 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1468 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1470 nfsm_mtofh(dvp, newvp, v3, gotvp);
1474 newvp = (struct vnode *)0;
1476 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1477 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1483 nfsm_wcc_data(dvp, wccflag);
1487 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1493 } else if (v3 && (fmode & O_EXCL)) {
1495 * We are normally called with only a partially initialized
1496 * VAP. Since the NFSv3 spec says that server may use the
1497 * file attributes to store the verifier, the spec requires
1498 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1499 * in atime, but we can't really assume that all servers will
1500 * so we ensure that our SETATTR sets both atime and mtime.
1502 if (vap->va_mtime.tv_sec == VNOVAL)
1503 vfs_timestamp(&vap->va_mtime);
1504 if (vap->va_atime.tv_sec == VNOVAL)
1505 vap->va_atime = vap->va_mtime;
1506 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1509 if (cnp->cn_flags & CNP_MAKEENTRY)
1510 cache_enter(dvp, NCPNULL, newvp, cnp);
1512 * The new np may have enough info for access
1513 * checks, make sure rucred and wucred are
1514 * initialized for read and write rpc's.
1517 if (np->n_rucred == NULL)
1518 np->n_rucred = crhold(cnp->cn_cred);
1519 if (np->n_wucred == NULL)
1520 np->n_wucred = crhold(cnp->cn_cred);
1523 VTONFS(dvp)->n_flag |= NMODIFIED;
1525 VTONFS(dvp)->n_attrstamp = 0;
1530 * nfs file remove call
1531 * To try and make nfs semantics closer to ufs semantics, a file that has
1532 * other processes using the vnode is renamed instead of removed and then
1533 * removed later on the last close.
1534 * - If v_usecount > 1
1535 * If a rename is not already in the works
1536 * call nfs_sillyrename() to set it up
1540 * nfs_remove(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
1541 * struct vnode *a_vp, struct componentname *a_cnp)
1544 nfs_remove(struct vop_remove_args *ap)
1546 struct vnode *vp = ap->a_vp;
1547 struct vnode *dvp = ap->a_dvp;
1548 struct componentname *cnp = ap->a_cnp;
1549 struct nfsnode *np = VTONFS(vp);
1554 if ((cnp->cn_flags & CNP_HASBUF) == 0)
1555 panic("nfs_remove: no name");
1556 if (vp->v_usecount < 1)
1557 panic("nfs_remove: bad v_usecount");
1559 if (vp->v_type == VDIR)
1561 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1562 VOP_GETATTR(vp, &vattr, cnp->cn_td) == 0 &&
1563 vattr.va_nlink > 1)) {
1565 * Purge the name cache so that the chance of a lookup for
1566 * the name succeeding while the remove is in progress is
1567 * minimized. Without node locking it can still happen, such
1568 * that an I/O op returns ESTALE, but since you get this if
1569 * another host removes the file..
1573 * throw away biocache buffers, mainly to avoid
1574 * unnecessary delayed writes later.
1576 error = nfs_vinvalbuf(vp, 0, cnp->cn_td, 1);
1579 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1580 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1582 * Kludge City: If the first reply to the remove rpc is lost..
1583 * the reply to the retransmitted request will be ENOENT
1584 * since the file was in fact removed
1585 * Therefore, we cheat and return success.
1587 if (error == ENOENT)
1589 } else if (!np->n_sillyrename)
1590 error = nfs_sillyrename(dvp, vp, cnp);
1591 np->n_attrstamp = 0;
1596 * nfs file remove rpc called from nfs_inactive
1599 nfs_removeit(struct sillyrename *sp)
1601 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1606 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1609 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1610 struct ucred *cred, struct thread *td)
1615 caddr_t bpos, dpos, cp2;
1616 int error = 0, wccflag = NFSV3_WCCRATTR;
1617 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1618 int v3 = NFS_ISV3(dvp);
1620 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1621 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1622 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1623 nfsm_fhtom(dvp, v3);
1624 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1625 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1627 nfsm_wcc_data(dvp, wccflag);
1630 VTONFS(dvp)->n_flag |= NMODIFIED;
1632 VTONFS(dvp)->n_attrstamp = 0;
1637 * nfs file rename call
1639 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1640 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1641 * struct vnode *a_tvp, struct componentname *a_tcnp)
1644 nfs_rename(struct vop_rename_args *ap)
1646 struct vnode *fvp = ap->a_fvp;
1647 struct vnode *tvp = ap->a_tvp;
1648 struct vnode *fdvp = ap->a_fdvp;
1649 struct vnode *tdvp = ap->a_tdvp;
1650 struct componentname *tcnp = ap->a_tcnp;
1651 struct componentname *fcnp = ap->a_fcnp;
1655 if ((tcnp->cn_flags & CNP_HASBUF) == 0 ||
1656 (fcnp->cn_flags & CNP_HASBUF) == 0)
1657 panic("nfs_rename: no name");
1659 /* Check for cross-device rename */
1660 if ((fvp->v_mount != tdvp->v_mount) ||
1661 (tvp && (fvp->v_mount != tvp->v_mount))) {
1667 * We have to flush B_DELWRI data prior to renaming
1668 * the file. If we don't, the delayed-write buffers
1669 * can be flushed out later after the file has gone stale
1670 * under NFSV3. NFSV2 does not have this problem because
1671 * ( as far as I can tell ) it flushes dirty buffers more
1675 VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_td);
1677 VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_td);
1680 * If the tvp exists and is in use, sillyrename it before doing the
1681 * rename of the new file over it.
1682 * XXX Can't sillyrename a directory.
1684 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1685 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1690 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1691 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1694 if (fvp->v_type == VDIR) {
1695 if (tvp != NULL && tvp->v_type == VDIR)
1710 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1712 if (error == ENOENT)
1718 * nfs file rename rpc called from nfs_remove() above
1721 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1722 struct sillyrename *sp)
1724 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1725 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1729 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1732 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1733 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1734 struct ucred *cred, struct thread *td)
1739 caddr_t bpos, dpos, cp2;
1740 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1741 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1742 int v3 = NFS_ISV3(fdvp);
1744 nfsstats.rpccnt[NFSPROC_RENAME]++;
1745 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1746 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1747 nfsm_rndup(tnamelen));
1748 nfsm_fhtom(fdvp, v3);
1749 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1750 nfsm_fhtom(tdvp, v3);
1751 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1752 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1754 nfsm_wcc_data(fdvp, fwccflag);
1755 nfsm_wcc_data(tdvp, twccflag);
1759 VTONFS(fdvp)->n_flag |= NMODIFIED;
1760 VTONFS(tdvp)->n_flag |= NMODIFIED;
1762 VTONFS(fdvp)->n_attrstamp = 0;
1764 VTONFS(tdvp)->n_attrstamp = 0;
1769 * nfs hard link create call
1771 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1772 * struct componentname *a_cnp)
1775 nfs_link(struct vop_link_args *ap)
1777 struct vnode *vp = ap->a_vp;
1778 struct vnode *tdvp = ap->a_tdvp;
1779 struct componentname *cnp = ap->a_cnp;
1783 caddr_t bpos, dpos, cp2;
1784 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1785 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1788 if (vp->v_mount != tdvp->v_mount) {
1793 * Push all writes to the server, so that the attribute cache
1794 * doesn't get "out of sync" with the server.
1795 * XXX There should be a better way!
1797 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_td);
1800 nfsstats.rpccnt[NFSPROC_LINK]++;
1801 nfsm_reqhead(vp, NFSPROC_LINK,
1802 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1804 nfsm_fhtom(tdvp, v3);
1805 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1806 nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1808 nfsm_postop_attr(vp, attrflag);
1809 nfsm_wcc_data(tdvp, wccflag);
1813 VTONFS(tdvp)->n_flag |= NMODIFIED;
1815 VTONFS(vp)->n_attrstamp = 0;
1817 VTONFS(tdvp)->n_attrstamp = 0;
1819 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1821 if (error == EEXIST)
1827 * nfs symbolic link create call
1829 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1830 * struct componentname *a_cnp, struct vattr *a_vap,
1834 nfs_symlink(struct vop_symlink_args *ap)
1836 struct vnode *dvp = ap->a_dvp;
1837 struct vattr *vap = ap->a_vap;
1838 struct componentname *cnp = ap->a_cnp;
1839 struct nfsv2_sattr *sp;
1843 caddr_t bpos, dpos, cp2;
1844 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1845 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1846 struct vnode *newvp = (struct vnode *)0;
1847 int v3 = NFS_ISV3(dvp);
1849 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1850 slen = strlen(ap->a_target);
1851 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1852 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1853 nfsm_fhtom(dvp, v3);
1854 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1856 nfsm_v3attrbuild(vap, FALSE);
1858 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1860 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1861 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1862 sp->sa_uid = nfs_xdrneg1;
1863 sp->sa_gid = nfs_xdrneg1;
1864 sp->sa_size = nfs_xdrneg1;
1865 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1866 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1870 * Issue the NFS request and get the rpc response.
1872 * Only NFSv3 responses returning an error of 0 actually return
1873 * a file handle that can be converted into newvp without having
1874 * to do an extra lookup rpc.
1876 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1879 nfsm_mtofh(dvp, newvp, v3, gotvp);
1880 nfsm_wcc_data(dvp, wccflag);
1884 * out code jumps -> here, mrep is also freed.
1891 * If we get an EEXIST error, silently convert it to no-error
1892 * in case of an NFS retry.
1894 if (error == EEXIST)
1898 * If we do not have (or no longer have) an error, and we could
1899 * not extract the newvp from the response due to the request being
1900 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1901 * to obtain a newvp to return.
1903 if (error == 0 && newvp == NULL) {
1904 struct nfsnode *np = NULL;
1906 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1907 cnp->cn_cred, cnp->cn_td, &np);
1917 VTONFS(dvp)->n_flag |= NMODIFIED;
1919 VTONFS(dvp)->n_attrstamp = 0;
1926 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1927 * struct componentname *a_cnp, struct vattr *a_vap)
1930 nfs_mkdir(struct vop_mkdir_args *ap)
1932 struct vnode *dvp = ap->a_dvp;
1933 struct vattr *vap = ap->a_vap;
1934 struct componentname *cnp = ap->a_cnp;
1935 struct nfsv2_sattr *sp;
1940 struct nfsnode *np = (struct nfsnode *)0;
1941 struct vnode *newvp = (struct vnode *)0;
1942 caddr_t bpos, dpos, cp2;
1943 int error = 0, wccflag = NFSV3_WCCRATTR;
1945 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1947 int v3 = NFS_ISV3(dvp);
1949 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1952 len = cnp->cn_namelen;
1953 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1954 nfsm_reqhead(dvp, NFSPROC_MKDIR,
1955 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1956 nfsm_fhtom(dvp, v3);
1957 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1959 nfsm_v3attrbuild(vap, FALSE);
1961 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1962 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1963 sp->sa_uid = nfs_xdrneg1;
1964 sp->sa_gid = nfs_xdrneg1;
1965 sp->sa_size = nfs_xdrneg1;
1966 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1967 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1969 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
1971 nfsm_mtofh(dvp, newvp, v3, gotvp);
1973 nfsm_wcc_data(dvp, wccflag);
1976 VTONFS(dvp)->n_flag |= NMODIFIED;
1978 VTONFS(dvp)->n_attrstamp = 0;
1980 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1981 * if we can succeed in looking up the directory.
1983 if (error == EEXIST || (!error && !gotvp)) {
1986 newvp = (struct vnode *)0;
1988 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
1992 if (newvp->v_type != VDIR)
2005 * nfs remove directory call
2007 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2008 * struct componentname *a_cnp)
2011 nfs_rmdir(struct vop_rmdir_args *ap)
2013 struct vnode *vp = ap->a_vp;
2014 struct vnode *dvp = ap->a_dvp;
2015 struct componentname *cnp = ap->a_cnp;
2019 caddr_t bpos, dpos, cp2;
2020 int error = 0, wccflag = NFSV3_WCCRATTR;
2021 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2022 int v3 = NFS_ISV3(dvp);
2026 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2027 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2028 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2029 nfsm_fhtom(dvp, v3);
2030 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2031 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2033 nfsm_wcc_data(dvp, wccflag);
2036 VTONFS(dvp)->n_flag |= NMODIFIED;
2038 VTONFS(dvp)->n_attrstamp = 0;
2042 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2044 if (error == ENOENT)
2052 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2055 nfs_readdir(struct vop_readdir_args *ap)
2057 struct vnode *vp = ap->a_vp;
2058 struct nfsnode *np = VTONFS(vp);
2059 struct uio *uio = ap->a_uio;
2063 if (vp->v_type != VDIR)
2066 * First, check for hit on the EOF offset cache
2068 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2069 (np->n_flag & NMODIFIED) == 0) {
2070 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
2071 if (NQNFS_CKCACHABLE(vp, ND_READ)) {
2072 nfsstats.direofcache_hits++;
2075 } else if (VOP_GETATTR(vp, &vattr, uio->uio_td) == 0 &&
2076 np->n_mtime == vattr.va_mtime.tv_sec) {
2077 nfsstats.direofcache_hits++;
2083 * Call nfs_bioread() to do the real work.
2085 tresid = uio->uio_resid;
2086 error = nfs_bioread(vp, uio, 0);
2088 if (!error && uio->uio_resid == tresid)
2089 nfsstats.direofcache_misses++;
2095 * Called from below the buffer cache by nfs_doio().
2098 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2101 struct dirent *dp = NULL;
2106 caddr_t bpos, dpos, cp2;
2107 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2109 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2110 struct nfsnode *dnp = VTONFS(vp);
2112 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2114 int v3 = NFS_ISV3(vp);
2117 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2118 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2119 panic("nfs readdirrpc bad uio");
2123 * If there is no cookie, assume directory was stale.
2125 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2129 return (NFSERR_BAD_COOKIE);
2131 * Loop around doing readdir rpc's of size nm_readdirsize
2132 * truncated to a multiple of DIRBLKSIZ.
2133 * The stopping criteria is EOF or buffer full.
2135 while (more_dirs && bigenough) {
2136 nfsstats.rpccnt[NFSPROC_READDIR]++;
2137 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2141 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2142 *tl++ = cookie.nfsuquad[0];
2143 *tl++ = cookie.nfsuquad[1];
2144 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2145 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2147 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2148 *tl++ = cookie.nfsuquad[0];
2150 *tl = txdr_unsigned(nmp->nm_readdirsize);
2151 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2153 nfsm_postop_attr(vp, attrflag);
2155 nfsm_dissect(tl, u_int32_t *,
2157 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2158 dnp->n_cookieverf.nfsuquad[1] = *tl;
2164 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2165 more_dirs = fxdr_unsigned(int, *tl);
2167 /* loop thru the dir entries, doctoring them to 4bsd form */
2168 while (more_dirs && bigenough) {
2170 nfsm_dissect(tl, u_int32_t *,
2172 fileno = fxdr_hyper(tl);
2173 len = fxdr_unsigned(int, *(tl + 2));
2175 nfsm_dissect(tl, u_int32_t *,
2177 fileno = fxdr_unsigned(u_quad_t, *tl++);
2178 len = fxdr_unsigned(int, *tl);
2180 if (len <= 0 || len > NFS_MAXNAMLEN) {
2185 tlen = nfsm_rndup(len);
2187 tlen += 4; /* To ensure null termination */
2188 left = DIRBLKSIZ - blksiz;
2189 if ((tlen + DIRHDSIZ) > left) {
2190 dp->d_reclen += left;
2191 uiop->uio_iov->iov_base += left;
2192 uiop->uio_iov->iov_len -= left;
2193 uiop->uio_offset += left;
2194 uiop->uio_resid -= left;
2197 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2200 dp = (struct dirent *)uiop->uio_iov->iov_base;
2201 dp->d_fileno = (int)fileno;
2203 dp->d_reclen = tlen + DIRHDSIZ;
2204 dp->d_type = DT_UNKNOWN;
2205 blksiz += dp->d_reclen;
2206 if (blksiz == DIRBLKSIZ)
2208 uiop->uio_offset += DIRHDSIZ;
2209 uiop->uio_resid -= DIRHDSIZ;
2210 uiop->uio_iov->iov_base += DIRHDSIZ;
2211 uiop->uio_iov->iov_len -= DIRHDSIZ;
2212 nfsm_mtouio(uiop, len);
2213 cp = uiop->uio_iov->iov_base;
2215 *cp = '\0'; /* null terminate */
2216 uiop->uio_iov->iov_base += tlen;
2217 uiop->uio_iov->iov_len -= tlen;
2218 uiop->uio_offset += tlen;
2219 uiop->uio_resid -= tlen;
2221 nfsm_adv(nfsm_rndup(len));
2223 nfsm_dissect(tl, u_int32_t *,
2226 nfsm_dissect(tl, u_int32_t *,
2230 cookie.nfsuquad[0] = *tl++;
2232 cookie.nfsuquad[1] = *tl++;
2237 more_dirs = fxdr_unsigned(int, *tl);
2240 * If at end of rpc data, get the eof boolean
2243 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2244 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2249 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2250 * by increasing d_reclen for the last record.
2253 left = DIRBLKSIZ - blksiz;
2254 dp->d_reclen += left;
2255 uiop->uio_iov->iov_base += left;
2256 uiop->uio_iov->iov_len -= left;
2257 uiop->uio_offset += left;
2258 uiop->uio_resid -= left;
2262 * We are now either at the end of the directory or have filled the
2266 dnp->n_direofoffset = uiop->uio_offset;
2268 if (uiop->uio_resid > 0)
2269 printf("EEK! readdirrpc resid > 0\n");
2270 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2278 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2281 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2288 struct vnode *newvp;
2290 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2291 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2292 struct nameidata nami, *ndp = &nami;
2293 struct componentname *cnp = &ndp->ni_cnd;
2295 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2296 struct nfsnode *dnp = VTONFS(vp), *np;
2299 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2300 int attrflag, fhsize;
2303 dp = (struct dirent *)0;
2306 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2307 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2308 panic("nfs readdirplusrpc bad uio");
2314 * If there is no cookie, assume directory was stale.
2316 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2320 return (NFSERR_BAD_COOKIE);
2322 * Loop around doing readdir rpc's of size nm_readdirsize
2323 * truncated to a multiple of DIRBLKSIZ.
2324 * The stopping criteria is EOF or buffer full.
2326 while (more_dirs && bigenough) {
2327 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2328 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2329 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2331 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2332 *tl++ = cookie.nfsuquad[0];
2333 *tl++ = cookie.nfsuquad[1];
2334 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2335 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2336 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2337 *tl = txdr_unsigned(nmp->nm_rsize);
2338 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2339 nfsm_postop_attr(vp, attrflag);
2344 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2345 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2346 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2347 more_dirs = fxdr_unsigned(int, *tl);
2349 /* loop thru the dir entries, doctoring them to 4bsd form */
2350 while (more_dirs && bigenough) {
2351 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2352 fileno = fxdr_hyper(tl);
2353 len = fxdr_unsigned(int, *(tl + 2));
2354 if (len <= 0 || len > NFS_MAXNAMLEN) {
2359 tlen = nfsm_rndup(len);
2361 tlen += 4; /* To ensure null termination*/
2362 left = DIRBLKSIZ - blksiz;
2363 if ((tlen + DIRHDSIZ) > left) {
2364 dp->d_reclen += left;
2365 uiop->uio_iov->iov_base += left;
2366 uiop->uio_iov->iov_len -= left;
2367 uiop->uio_offset += left;
2368 uiop->uio_resid -= left;
2371 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2374 dp = (struct dirent *)uiop->uio_iov->iov_base;
2375 dp->d_fileno = (int)fileno;
2377 dp->d_reclen = tlen + DIRHDSIZ;
2378 dp->d_type = DT_UNKNOWN;
2379 blksiz += dp->d_reclen;
2380 if (blksiz == DIRBLKSIZ)
2382 uiop->uio_offset += DIRHDSIZ;
2383 uiop->uio_resid -= DIRHDSIZ;
2384 uiop->uio_iov->iov_base += DIRHDSIZ;
2385 uiop->uio_iov->iov_len -= DIRHDSIZ;
2386 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2387 cnp->cn_namelen = len;
2388 nfsm_mtouio(uiop, len);
2389 cp = uiop->uio_iov->iov_base;
2392 uiop->uio_iov->iov_base += tlen;
2393 uiop->uio_iov->iov_len -= tlen;
2394 uiop->uio_offset += tlen;
2395 uiop->uio_resid -= tlen;
2397 nfsm_adv(nfsm_rndup(len));
2398 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2400 cookie.nfsuquad[0] = *tl++;
2401 cookie.nfsuquad[1] = *tl++;
2406 * Since the attributes are before the file handle
2407 * (sigh), we must skip over the attributes and then
2408 * come back and get them.
2410 attrflag = fxdr_unsigned(int, *tl);
2414 nfsm_adv(NFSX_V3FATTR);
2415 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2416 doit = fxdr_unsigned(int, *tl);
2418 nfsm_getfh(fhp, fhsize, 1);
2419 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2424 error = nfs_nget(vp->v_mount, fhp,
2432 if (doit && bigenough) {
2437 nfsm_loadattr(newvp, (struct vattr *)0);
2441 IFTODT(VTTOIF(np->n_vattr.va_type));
2443 cache_enter(ndp->ni_dvp, NCPNULL, ndp->ni_vp, cnp);
2446 /* Just skip over the file handle */
2447 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2448 i = fxdr_unsigned(int, *tl);
2449 nfsm_adv(nfsm_rndup(i));
2451 if (newvp != NULLVP) {
2458 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2459 more_dirs = fxdr_unsigned(int, *tl);
2462 * If at end of rpc data, get the eof boolean
2465 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2466 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2471 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2472 * by increasing d_reclen for the last record.
2475 left = DIRBLKSIZ - blksiz;
2476 dp->d_reclen += left;
2477 uiop->uio_iov->iov_base += left;
2478 uiop->uio_iov->iov_len -= left;
2479 uiop->uio_offset += left;
2480 uiop->uio_resid -= left;
2484 * We are now either at the end of the directory or have filled the
2488 dnp->n_direofoffset = uiop->uio_offset;
2490 if (uiop->uio_resid > 0)
2491 printf("EEK! readdirplusrpc resid > 0\n");
2492 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2496 if (newvp != NULLVP) {
2507 * Silly rename. To make the NFS filesystem that is stateless look a little
2508 * more like the "ufs" a remove of an active vnode is translated to a rename
2509 * to a funny looking filename that is removed by nfs_inactive on the
2510 * nfsnode. There is the potential for another process on a different client
2511 * to create the same funny name between the nfs_lookitup() fails and the
2512 * nfs_rename() completes, but...
2515 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2517 struct sillyrename *sp;
2524 if (vp->v_type == VDIR)
2525 panic("nfs: sillyrename dir");
2527 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2528 M_NFSREQ, M_WAITOK);
2529 sp->s_cred = crdup(cnp->cn_cred);
2533 /* Fudge together a funny name */
2534 sp->s_namlen = sprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
2536 /* Try lookitups until we get one that isn't there */
2537 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2538 cnp->cn_td, (struct nfsnode **)0) == 0) {
2540 if (sp->s_name[4] > 'z') {
2545 error = nfs_renameit(dvp, cnp, sp);
2548 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2550 np->n_sillyrename = sp;
2555 free((caddr_t)sp, M_NFSREQ);
2560 * Look up a file name and optionally either update the file handle or
2561 * allocate an nfsnode, depending on the value of npp.
2562 * npp == NULL --> just do the lookup
2563 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2565 * *npp != NULL --> update the file handle in the vnode
2568 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2569 struct thread *td, struct nfsnode **npp)
2574 struct vnode *newvp = (struct vnode *)0;
2575 struct nfsnode *np, *dnp = VTONFS(dvp);
2576 caddr_t bpos, dpos, cp2;
2577 int error = 0, fhlen, attrflag;
2578 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2580 int v3 = NFS_ISV3(dvp);
2582 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2583 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2584 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2585 nfsm_fhtom(dvp, v3);
2586 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2587 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2588 if (npp && !error) {
2589 nfsm_getfh(nfhp, fhlen, v3);
2592 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2593 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2594 np->n_fhp = &np->n_fh;
2595 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2596 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2597 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2598 np->n_fhsize = fhlen;
2600 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2604 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2612 nfsm_postop_attr(newvp, attrflag);
2613 if (!attrflag && *npp == NULL) {
2622 nfsm_loadattr(newvp, (struct vattr *)0);
2626 if (npp && *npp == NULL) {
2641 * Nfs Version 3 commit rpc
2644 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2649 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2650 caddr_t bpos, dpos, cp2;
2651 int error = 0, wccflag = NFSV3_WCCRATTR;
2652 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2654 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2656 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2657 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2659 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2660 txdr_hyper(offset, tl);
2662 *tl = txdr_unsigned(cnt);
2663 nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2664 nfsm_wcc_data(vp, wccflag);
2666 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2667 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2668 NFSX_V3WRITEVERF)) {
2669 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2671 error = NFSERR_STALEWRITEVERF;
2681 * - make nfs_bmap() essentially a no-op that does no translation
2682 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2683 * (Maybe I could use the process's page mapping, but I was concerned that
2684 * Kernel Write might not be enabled and also figured copyout() would do
2685 * a lot more work than bcopy() and also it currently happens in the
2686 * context of the swapper process (2).
2688 * nfs_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
2689 * daddr_t *a_bnp, int *a_runp, int *a_runb)
2692 nfs_bmap(struct vop_bmap_args *ap)
2694 struct vnode *vp = ap->a_vp;
2696 if (ap->a_vpp != NULL)
2698 if (ap->a_bnp != NULL)
2699 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2700 if (ap->a_runp != NULL)
2702 if (ap->a_runb != NULL)
2709 * For async requests when nfsiod(s) are running, queue the request by
2710 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2714 nfs_strategy(struct vop_strategy_args *ap)
2716 struct buf *bp = ap->a_bp;
2720 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2721 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2723 if (bp->b_flags & B_PHYS)
2724 panic("nfs physio");
2726 if (bp->b_flags & B_ASYNC)
2729 td = curthread; /* XXX */
2732 * If the op is asynchronous and an i/o daemon is waiting
2733 * queue the request, wake it up and wait for completion
2734 * otherwise just do it ourselves.
2736 if ((bp->b_flags & B_ASYNC) == 0 ||
2737 nfs_asyncio(bp, td))
2738 error = nfs_doio(bp, td);
2745 * NB Currently unsupported.
2747 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
2748 * struct thread *a_td)
2752 nfs_mmap(struct vop_mmap_args *ap)
2758 * fsync vnode op. Just call nfs_flush() with commit == 1.
2760 * nfs_fsync(struct vnodeop_desc *a_desc, struct vnode *a_vp,
2761 * struct ucred * a_cred, int a_waitfor, struct thread *a_td)
2765 nfs_fsync(struct vop_fsync_args *ap)
2767 return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2771 * Flush all the blocks associated with a vnode.
2772 * Walk through the buffer pool and push any dirty pages
2773 * associated with the vnode.
2776 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
2778 struct nfsnode *np = VTONFS(vp);
2782 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2783 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2785 u_quad_t off, endoff, toff;
2786 struct buf **bvec = NULL;
2787 #ifndef NFS_COMMITBVECSIZ
2788 #define NFS_COMMITBVECSIZ 20
2790 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2791 int bvecsize = 0, bveccount;
2793 if (nmp->nm_flag & NFSMNT_INT)
2798 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2799 * server, but nas not been committed to stable storage on the server
2800 * yet. On the first pass, the byte range is worked out and the commit
2801 * rpc is done. On the second pass, nfs_writebp() is called to do the
2808 if (NFS_ISV3(vp) && commit) {
2811 * Count up how many buffers waiting for a commit.
2814 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2815 nbp = TAILQ_NEXT(bp, b_vnbufs);
2816 if (BUF_REFCNT(bp) == 0 &&
2817 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2818 == (B_DELWRI | B_NEEDCOMMIT))
2822 * Allocate space to remember the list of bufs to commit. It is
2823 * important to use M_NOWAIT here to avoid a race with nfs_write.
2824 * If we can't get memory (for whatever reason), we will end up
2825 * committing the buffers one-by-one in the loop below.
2827 if (bvec != NULL && bvec != bvec_on_stack)
2829 if (bveccount > NFS_COMMITBVECSIZ) {
2830 bvec = (struct buf **)
2831 malloc(bveccount * sizeof(struct buf *),
2834 bvec = bvec_on_stack;
2835 bvecsize = NFS_COMMITBVECSIZ;
2837 bvecsize = bveccount;
2839 bvec = bvec_on_stack;
2840 bvecsize = NFS_COMMITBVECSIZ;
2842 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2843 nbp = TAILQ_NEXT(bp, b_vnbufs);
2844 if (bvecpos >= bvecsize)
2846 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2847 (B_DELWRI | B_NEEDCOMMIT) ||
2848 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
2852 * NOTE: we are not clearing B_DONE here, so we have
2853 * to do it later on in this routine if we intend to
2854 * initiate I/O on the bp.
2856 * Note: to avoid loopback deadlocks, we do not
2857 * assign b_runningbufspace.
2859 bp->b_flags |= B_WRITEINPROG;
2860 vfs_busy_pages(bp, 1);
2863 * bp is protected by being locked, but nbp is not
2864 * and vfs_busy_pages() may sleep. We have to
2867 nbp = TAILQ_NEXT(bp, b_vnbufs);
2870 * A list of these buffers is kept so that the
2871 * second loop knows which buffers have actually
2872 * been committed. This is necessary, since there
2873 * may be a race between the commit rpc and new
2874 * uncommitted writes on the file.
2876 bvec[bvecpos++] = bp;
2877 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2881 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2889 * Commit data on the server, as required. Note that
2890 * nfs_commit will use the vnode's cred for the commit.
2892 retv = nfs_commit(vp, off, (int)(endoff - off), td);
2894 if (retv == NFSERR_STALEWRITEVERF)
2895 nfs_clearcommit(vp->v_mount);
2898 * Now, either mark the blocks I/O done or mark the
2899 * blocks dirty, depending on whether the commit
2902 for (i = 0; i < bvecpos; i++) {
2904 bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG | B_CLUSTEROK);
2907 * Error, leave B_DELWRI intact
2909 vfs_unbusy_pages(bp);
2913 * Success, remove B_DELWRI ( bundirty() ).
2915 * b_dirtyoff/b_dirtyend seem to be NFS
2916 * specific. We should probably move that
2917 * into bundirty(). XXX
2921 bp->b_flags |= B_ASYNC;
2923 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
2924 bp->b_dirtyoff = bp->b_dirtyend = 0;
2932 * Start/do any write(s) that are required.
2936 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2937 nbp = TAILQ_NEXT(bp, b_vnbufs);
2938 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
2939 if (waitfor != MNT_WAIT || passone)
2941 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL,
2942 "nfsfsync", slpflag, slptimeo);
2945 panic("nfs_fsync: inconsistent lock");
2946 if (error == ENOLCK)
2948 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
2952 if (slpflag == PCATCH) {
2958 if ((bp->b_flags & B_DELWRI) == 0)
2959 panic("nfs_fsync: not dirty");
2960 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2965 if (passone || !commit)
2966 bp->b_flags |= B_ASYNC;
2968 bp->b_flags |= B_ASYNC | B_WRITEINPROG;
2970 VOP_BWRITE(bp->b_vp, bp);
2978 if (waitfor == MNT_WAIT) {
2979 while (vp->v_numoutput) {
2980 vp->v_flag |= VBWAIT;
2981 error = tsleep((caddr_t)&vp->v_numoutput,
2982 slpflag, "nfsfsync", slptimeo);
2984 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
2988 if (slpflag == PCATCH) {
2994 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) && commit) {
2998 if (np->n_flag & NWRITEERR) {
2999 error = np->n_error;
3000 np->n_flag &= ~NWRITEERR;
3003 if (bvec != NULL && bvec != bvec_on_stack)
3009 * NFS advisory byte-level locks.
3010 * Currently unsupported.
3012 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3016 nfs_advlock(struct vop_advlock_args *ap)
3018 struct nfsnode *np = VTONFS(ap->a_vp);
3021 * The following kludge is to allow diskless support to work
3022 * until a real NFS lockd is implemented. Basically, just pretend
3023 * that this is a local lock.
3025 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3029 * Print out the contents of an nfsnode.
3031 * nfs_print(struct vnode *a_vp)
3034 nfs_print(struct vop_print_args *ap)
3036 struct vnode *vp = ap->a_vp;
3037 struct nfsnode *np = VTONFS(vp);
3039 printf("tag VT_NFS, fileid %ld fsid 0x%x",
3040 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3041 if (vp->v_type == VFIFO)
3048 * Just call nfs_writebp() with the force argument set to 1.
3050 * NOTE: B_DONE may or may not be set in a_bp on call.
3052 * nfs_bwrite(struct vnode *a_bp)
3055 nfs_bwrite(struct vop_bwrite_args *ap)
3057 return (nfs_writebp(ap->a_bp, 1, curthread));
3061 * This is a clone of vn_bwrite(), except that B_WRITEINPROG isn't set unless
3062 * the force flag is one and it also handles the B_NEEDCOMMIT flag. We set
3063 * B_CACHE if this is a VMIO buffer.
3066 nfs_writebp(struct buf *bp, int force, struct thread *td)
3069 int oldflags = bp->b_flags;
3075 if (BUF_REFCNT(bp) == 0)
3076 panic("bwrite: buffer is not locked???");
3078 if (bp->b_flags & B_INVAL) {
3083 bp->b_flags |= B_CACHE;
3086 * Undirty the bp. We will redirty it later if the I/O fails.
3091 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3093 bp->b_vp->v_numoutput++;
3097 * Note: to avoid loopback deadlocks, we do not
3098 * assign b_runningbufspace.
3100 vfs_busy_pages(bp, 1);
3103 bp->b_flags |= B_WRITEINPROG;
3105 VOP_STRATEGY(bp->b_vp, bp);
3107 if( (oldflags & B_ASYNC) == 0) {
3108 int rtval = biowait(bp);
3110 if (oldflags & B_DELWRI) {
3112 reassignbuf(bp, bp->b_vp);
3124 * nfs special file access vnode op.
3125 * Essentially just get vattr and then imitate iaccess() since the device is
3126 * local to the client.
3128 * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
3129 * struct thread *a_td)
3132 nfsspec_access(struct vop_access_args *ap)
3136 struct ucred *cred = ap->a_cred;
3137 struct vnode *vp = ap->a_vp;
3138 mode_t mode = ap->a_mode;
3144 * Disallow write attempts on filesystems mounted read-only;
3145 * unless the file is a socket, fifo, or a block or character
3146 * device resident on the filesystem.
3148 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3149 switch (vp->v_type) {
3159 * If you're the super-user,
3160 * you always get access.
3162 if (cred->cr_uid == 0)
3165 error = VOP_GETATTR(vp, vap, ap->a_td);
3169 * Access check is based on only one of owner, group, public.
3170 * If not owner, then check group. If not a member of the
3171 * group, then check public access.
3173 if (cred->cr_uid != vap->va_uid) {
3175 gp = cred->cr_groups;
3176 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3177 if (vap->va_gid == *gp)
3183 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3188 * Read wrapper for special devices.
3190 * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3191 * struct ucred *a_cred)
3194 nfsspec_read(struct vop_read_args *ap)
3196 struct nfsnode *np = VTONFS(ap->a_vp);
3202 getnanotime(&np->n_atim);
3203 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3207 * Write wrapper for special devices.
3209 * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3210 * struct ucred *a_cred)
3213 nfsspec_write(struct vop_write_args *ap)
3215 struct nfsnode *np = VTONFS(ap->a_vp);
3221 getnanotime(&np->n_mtim);
3222 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3226 * Close wrapper for special devices.
3228 * Update the times on the nfsnode then do device close.
3230 * nfsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
3231 * struct thread *a_td)
3234 nfsspec_close(struct vop_close_args *ap)
3236 struct vnode *vp = ap->a_vp;
3237 struct nfsnode *np = VTONFS(vp);
3240 if (np->n_flag & (NACC | NUPD)) {
3242 if (vp->v_usecount == 1 &&
3243 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3245 if (np->n_flag & NACC)
3246 vattr.va_atime = np->n_atim;
3247 if (np->n_flag & NUPD)
3248 vattr.va_mtime = np->n_mtim;
3249 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3252 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3256 * Read wrapper for fifos.
3258 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3259 * struct ucred *a_cred)
3262 nfsfifo_read(struct vop_read_args *ap)
3264 struct nfsnode *np = VTONFS(ap->a_vp);
3270 getnanotime(&np->n_atim);
3271 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3275 * Write wrapper for fifos.
3277 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3278 * struct ucred *a_cred)
3281 nfsfifo_write(struct vop_write_args *ap)
3283 struct nfsnode *np = VTONFS(ap->a_vp);
3289 getnanotime(&np->n_mtim);
3290 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3294 * Close wrapper for fifos.
3296 * Update the times on the nfsnode then do fifo close.
3298 * nfsfifo_close(struct vnode *a_vp, int a_fflag, struct thread *a_td)
3301 nfsfifo_close(struct vop_close_args *ap)
3303 struct vnode *vp = ap->a_vp;
3304 struct nfsnode *np = VTONFS(vp);
3308 if (np->n_flag & (NACC | NUPD)) {
3310 if (np->n_flag & NACC)
3312 if (np->n_flag & NUPD)
3315 if (vp->v_usecount == 1 &&
3316 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3318 if (np->n_flag & NACC)
3319 vattr.va_atime = np->n_atim;
3320 if (np->n_flag & NUPD)
3321 vattr.va_mtime = np->n_mtim;
3322 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3325 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));