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.21 2004/04/19 16:33:49 cpressey 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);
747 /* np->n_size has already been set to vap->va_size
748 * in nfs_meta_setsize(). We must set it again since
749 * nfs_loadattrcache() could be called through
750 * nfs_meta_setsize() and could modify np->n_size.
752 np->n_vattr.va_size = np->n_size = vap->va_size;
754 } else if ((vap->va_mtime.tv_sec != VNOVAL ||
755 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NMODIFIED) &&
756 vp->v_type == VREG &&
757 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) == EINTR)
759 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
760 if (error && vap->va_size != VNOVAL) {
761 np->n_size = np->n_vattr.va_size = tsize;
762 vnode_pager_setsize(vp, np->n_size);
768 * Do an nfs setattr rpc.
771 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
772 struct ucred *cred, struct thread *td)
774 struct nfsv2_sattr *sp;
777 caddr_t bpos, dpos, cp2;
779 int error = 0, wccflag = NFSV3_WCCRATTR;
780 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
781 int v3 = NFS_ISV3(vp);
783 nfsstats.rpccnt[NFSPROC_SETATTR]++;
784 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
787 nfsm_v3attrbuild(vap, TRUE);
788 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
791 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
792 if (vap->va_mode == (mode_t)VNOVAL)
793 sp->sa_mode = nfs_xdrneg1;
795 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
796 if (vap->va_uid == (uid_t)VNOVAL)
797 sp->sa_uid = nfs_xdrneg1;
799 sp->sa_uid = txdr_unsigned(vap->va_uid);
800 if (vap->va_gid == (gid_t)VNOVAL)
801 sp->sa_gid = nfs_xdrneg1;
803 sp->sa_gid = txdr_unsigned(vap->va_gid);
804 sp->sa_size = txdr_unsigned(vap->va_size);
805 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
806 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
808 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
810 nfsm_wcc_data(vp, wccflag);
812 nfsm_loadattr(vp, (struct vattr *)0);
819 * 'cached' nfs directory lookup
821 * nfs_lookup(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
822 * struct vnode **a_vpp, struct componentname *a_cnp)
825 nfs_lookup(struct vop_lookup_args *ap)
827 struct componentname *cnp = ap->a_cnp;
828 struct vnode *dvp = ap->a_dvp;
829 struct vnode **vpp = ap->a_vpp;
830 int flags = cnp->cn_flags;
835 struct nfsmount *nmp;
836 caddr_t bpos, dpos, cp2;
837 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
841 int lockparent, wantparent, error = 0, attrflag, fhsize;
842 int v3 = NFS_ISV3(dvp);
843 struct thread *td = cnp->cn_td;
846 * Read-only mount check and directory check.
849 if ((flags & CNP_ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
850 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
853 if (dvp->v_type != VDIR)
857 * Look it up in the cache. Note that ENOENT is only returned if we
858 * previously entered a negative hit (see later on). The additional
859 * nfsneg_cache_timeout check causes previously cached results to
860 * be instantly ignored if the negative caching is turned off.
862 lockparent = flags & CNP_LOCKPARENT;
863 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
864 nmp = VFSTONFS(dvp->v_mount);
866 error = cache_lookup(dvp, NCPNULL, vpp, NCPPNULL, cnp);
871 if (error == ENOENT && nfsneg_cache_timeout) {
875 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
883 * See the comment starting `Step through' in ufs/ufs_lookup.c
884 * for an explanation of the locking protocol
889 } else if (flags & CNP_ISDOTDOT) {
890 VOP_UNLOCK(dvp, NULL, 0, td);
891 error = vget(newvp, NULL, LK_EXCLUSIVE, td);
892 if (!error && lockparent && (flags & CNP_ISLASTCN))
893 error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td);
895 error = vget(newvp, NULL, LK_EXCLUSIVE, td);
896 if (!lockparent || error || !(flags & CNP_ISLASTCN))
897 VOP_UNLOCK(dvp, NULL, 0, td);
900 if (vpid == newvp->v_id) {
901 if (!VOP_GETATTR(newvp, &vattr, td)
902 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
903 nfsstats.lookupcache_hits++;
904 if (cnp->cn_nameiop != NAMEI_LOOKUP &&
905 (flags & CNP_ISLASTCN))
906 cnp->cn_flags |= CNP_SAVENAME;
912 if (lockparent && dvp != newvp && (flags & CNP_ISLASTCN))
913 VOP_UNLOCK(dvp, NULL, 0, td);
915 error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td);
922 * Cache miss, go the wire.
926 nfsstats.lookupcache_misses++;
927 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
928 len = cnp->cn_namelen;
929 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
930 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
932 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
933 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
936 * Cache negatve lookups to reduce NFS traffic, but use
939 if (error == ENOENT &&
940 (cnp->cn_flags & CNP_MAKEENTRY) &&
941 cnp->cn_nameiop == NAMEI_LOOKUP &&
942 nfsneg_cache_timeout) {
943 int toval = nfsneg_cache_timeout * hz;
944 if (cnp->cn_flags & CNP_CACHETIMEOUT) {
945 if (cnp->cn_timeout > toval)
946 cnp->cn_timeout = toval;
948 cnp->cn_flags |= CNP_CACHETIMEOUT;
949 cnp->cn_timeout = toval;
951 cache_enter(dvp, NCPNULL, NULL, cnp);
953 nfsm_postop_attr(dvp, attrflag);
957 nfsm_getfh(fhp, fhsize, v3);
960 * Handle RENAME case...
962 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent && (flags & CNP_ISLASTCN)) {
963 if (NFS_CMPFH(np, fhp, fhsize)) {
967 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
974 nfsm_postop_attr(newvp, attrflag);
975 nfsm_postop_attr(dvp, attrflag);
977 nfsm_loadattr(newvp, (struct vattr *)0);
980 cnp->cn_flags |= CNP_SAVENAME;
982 VOP_UNLOCK(dvp, NULL, 0, td);
986 if (flags & CNP_ISDOTDOT) {
987 VOP_UNLOCK(dvp, NULL, 0, td);
988 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
990 vn_lock(dvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
994 if (lockparent && (flags & CNP_ISLASTCN) &&
995 (error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td))) {
999 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1003 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1008 if (!lockparent || !(flags & CNP_ISLASTCN))
1009 VOP_UNLOCK(dvp, NULL, 0, td);
1013 nfsm_postop_attr(newvp, attrflag);
1014 nfsm_postop_attr(dvp, attrflag);
1016 nfsm_loadattr(newvp, (struct vattr *)0);
1017 if (cnp->cn_nameiop != NAMEI_LOOKUP && (flags & CNP_ISLASTCN))
1018 cnp->cn_flags |= CNP_SAVENAME;
1019 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1020 (cnp->cn_nameiop != NAMEI_DELETE || !(flags & CNP_ISLASTCN))) {
1021 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
1022 cache_enter(dvp, NCPNULL, newvp, cnp);
1028 if (newvp != NULLVP) {
1032 if ((cnp->cn_nameiop == NAMEI_CREATE || cnp->cn_nameiop == NAMEI_RENAME) &&
1033 (flags & CNP_ISLASTCN) && error == ENOENT) {
1035 VOP_UNLOCK(dvp, NULL, 0, td);
1036 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1039 error = EJUSTRETURN;
1041 if (cnp->cn_nameiop != NAMEI_LOOKUP && (flags & CNP_ISLASTCN))
1042 cnp->cn_flags |= CNP_SAVENAME;
1049 * Just call nfs_bioread() to do the work.
1051 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1052 * struct ucred *a_cred)
1055 nfs_read(struct vop_read_args *ap)
1057 struct vnode *vp = ap->a_vp;
1059 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1060 switch (vp->v_type) {
1062 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1073 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1076 nfs_readlink(struct vop_readlink_args *ap)
1078 struct vnode *vp = ap->a_vp;
1080 if (vp->v_type != VLNK)
1082 return (nfs_bioread(vp, ap->a_uio, 0));
1086 * Do a readlink rpc.
1087 * Called by nfs_doio() from below the buffer cache.
1090 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1095 caddr_t bpos, dpos, cp2;
1096 int error = 0, len, attrflag;
1097 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1098 int v3 = NFS_ISV3(vp);
1100 nfsstats.rpccnt[NFSPROC_READLINK]++;
1101 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1103 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1105 nfsm_postop_attr(vp, attrflag);
1107 nfsm_strsiz(len, NFS_MAXPATHLEN);
1108 if (len == NFS_MAXPATHLEN) {
1109 struct nfsnode *np = VTONFS(vp);
1110 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1113 nfsm_mtouio(uiop, len);
1125 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1130 caddr_t bpos, dpos, cp2;
1131 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1132 struct nfsmount *nmp;
1133 int error = 0, len, retlen, tsiz, eof, attrflag;
1134 int v3 = NFS_ISV3(vp);
1139 nmp = VFSTONFS(vp->v_mount);
1140 tsiz = uiop->uio_resid;
1141 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1144 nfsstats.rpccnt[NFSPROC_READ]++;
1145 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1146 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1148 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1150 txdr_hyper(uiop->uio_offset, tl);
1151 *(tl + 2) = txdr_unsigned(len);
1153 *tl++ = txdr_unsigned(uiop->uio_offset);
1154 *tl++ = txdr_unsigned(len);
1157 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1159 nfsm_postop_attr(vp, attrflag);
1164 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1165 eof = fxdr_unsigned(int, *(tl + 1));
1167 nfsm_loadattr(vp, (struct vattr *)0);
1168 nfsm_strsiz(retlen, nmp->nm_rsize);
1169 nfsm_mtouio(uiop, retlen);
1173 if (eof || retlen == 0) {
1176 } else if (retlen < len) {
1188 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, int *must_commit)
1192 int32_t t1, t2, backup;
1193 caddr_t bpos, dpos, cp2;
1194 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1195 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1196 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1197 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1200 if (uiop->uio_iovcnt != 1)
1201 panic("nfs: writerpc iovcnt > 1");
1204 tsiz = uiop->uio_resid;
1205 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1208 nfsstats.rpccnt[NFSPROC_WRITE]++;
1209 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1210 nfsm_reqhead(vp, NFSPROC_WRITE,
1211 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1214 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1215 txdr_hyper(uiop->uio_offset, tl);
1217 *tl++ = txdr_unsigned(len);
1218 *tl++ = txdr_unsigned(*iomode);
1219 *tl = txdr_unsigned(len);
1223 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1224 /* Set both "begin" and "current" to non-garbage. */
1225 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1226 *tl++ = x; /* "begin offset" */
1227 *tl++ = x; /* "current offset" */
1228 x = txdr_unsigned(len);
1229 *tl++ = x; /* total to this offset */
1230 *tl = x; /* size of this write */
1232 nfsm_uiotom(uiop, len);
1233 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1235 wccflag = NFSV3_WCCCHK;
1236 nfsm_wcc_data(vp, wccflag);
1238 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1239 + NFSX_V3WRITEVERF);
1240 rlen = fxdr_unsigned(int, *tl++);
1245 } else if (rlen < len) {
1246 backup = len - rlen;
1247 uiop->uio_iov->iov_base -= backup;
1248 uiop->uio_iov->iov_len += backup;
1249 uiop->uio_offset -= backup;
1250 uiop->uio_resid += backup;
1253 commit = fxdr_unsigned(int, *tl++);
1256 * Return the lowest committment level
1257 * obtained by any of the RPCs.
1259 if (committed == NFSV3WRITE_FILESYNC)
1261 else if (committed == NFSV3WRITE_DATASYNC &&
1262 commit == NFSV3WRITE_UNSTABLE)
1264 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1265 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1267 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1268 } else if (bcmp((caddr_t)tl,
1269 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1271 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1276 nfsm_loadattr(vp, (struct vattr *)0);
1278 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime.tv_sec;
1285 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1286 committed = NFSV3WRITE_FILESYNC;
1287 *iomode = committed;
1289 uiop->uio_resid = tsiz;
1295 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1296 * mode set to specify the file type and the size field for rdev.
1299 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1302 struct nfsv2_sattr *sp;
1306 struct vnode *newvp = (struct vnode *)0;
1307 struct nfsnode *np = (struct nfsnode *)0;
1311 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1312 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1314 int v3 = NFS_ISV3(dvp);
1316 if (vap->va_type == VCHR || vap->va_type == VBLK)
1317 rdev = txdr_unsigned(vap->va_rdev);
1318 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1321 return (EOPNOTSUPP);
1323 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1326 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1327 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1328 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1329 nfsm_fhtom(dvp, v3);
1330 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1332 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1333 *tl++ = vtonfsv3_type(vap->va_type);
1334 nfsm_v3attrbuild(vap, FALSE);
1335 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1336 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1337 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1338 *tl = txdr_unsigned(uminor(vap->va_rdev));
1341 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1342 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1343 sp->sa_uid = nfs_xdrneg1;
1344 sp->sa_gid = nfs_xdrneg1;
1346 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1347 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1349 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1351 nfsm_mtofh(dvp, newvp, v3, gotvp);
1355 newvp = (struct vnode *)0;
1357 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1358 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1364 nfsm_wcc_data(dvp, wccflag);
1371 if (cnp->cn_flags & CNP_MAKEENTRY)
1372 cache_enter(dvp, NCPNULL, newvp, cnp);
1375 VTONFS(dvp)->n_flag |= NMODIFIED;
1377 VTONFS(dvp)->n_attrstamp = 0;
1383 * just call nfs_mknodrpc() to do the work.
1385 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1386 * struct componentname *a_cnp, struct vattr *a_vap)
1390 nfs_mknod(struct vop_mknod_args *ap)
1392 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1395 static u_long create_verf;
1397 * nfs file create call
1399 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1400 * struct componentname *a_cnp, struct vattr *a_vap)
1403 nfs_create(struct vop_create_args *ap)
1405 struct vnode *dvp = ap->a_dvp;
1406 struct vattr *vap = ap->a_vap;
1407 struct componentname *cnp = ap->a_cnp;
1408 struct nfsv2_sattr *sp;
1412 struct nfsnode *np = (struct nfsnode *)0;
1413 struct vnode *newvp = (struct vnode *)0;
1414 caddr_t bpos, dpos, cp2;
1415 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1416 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1418 int v3 = NFS_ISV3(dvp);
1421 * Oops, not for me..
1423 if (vap->va_type == VSOCK)
1424 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1426 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1429 if (vap->va_vaflags & VA_EXCLUSIVE)
1432 nfsstats.rpccnt[NFSPROC_CREATE]++;
1433 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1434 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1435 nfsm_fhtom(dvp, v3);
1436 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1438 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1439 if (fmode & O_EXCL) {
1440 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1441 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1443 if (!TAILQ_EMPTY(&in_ifaddrhead))
1444 *tl++ = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr.s_addr;
1447 *tl++ = create_verf;
1448 *tl = ++create_verf;
1450 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1451 nfsm_v3attrbuild(vap, FALSE);
1454 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1455 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1456 sp->sa_uid = nfs_xdrneg1;
1457 sp->sa_gid = nfs_xdrneg1;
1459 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1460 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1462 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1464 nfsm_mtofh(dvp, newvp, v3, gotvp);
1468 newvp = (struct vnode *)0;
1470 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1471 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1477 nfsm_wcc_data(dvp, wccflag);
1481 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1487 } else if (v3 && (fmode & O_EXCL)) {
1489 * We are normally called with only a partially initialized
1490 * VAP. Since the NFSv3 spec says that server may use the
1491 * file attributes to store the verifier, the spec requires
1492 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1493 * in atime, but we can't really assume that all servers will
1494 * so we ensure that our SETATTR sets both atime and mtime.
1496 if (vap->va_mtime.tv_sec == VNOVAL)
1497 vfs_timestamp(&vap->va_mtime);
1498 if (vap->va_atime.tv_sec == VNOVAL)
1499 vap->va_atime = vap->va_mtime;
1500 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1503 if (cnp->cn_flags & CNP_MAKEENTRY)
1504 cache_enter(dvp, NCPNULL, newvp, cnp);
1506 * The new np may have enough info for access
1507 * checks, make sure rucred and wucred are
1508 * initialized for read and write rpc's.
1511 if (np->n_rucred == NULL)
1512 np->n_rucred = crhold(cnp->cn_cred);
1513 if (np->n_wucred == NULL)
1514 np->n_wucred = crhold(cnp->cn_cred);
1517 VTONFS(dvp)->n_flag |= NMODIFIED;
1519 VTONFS(dvp)->n_attrstamp = 0;
1524 * nfs file remove call
1525 * To try and make nfs semantics closer to ufs semantics, a file that has
1526 * other processes using the vnode is renamed instead of removed and then
1527 * removed later on the last close.
1528 * - If v_usecount > 1
1529 * If a rename is not already in the works
1530 * call nfs_sillyrename() to set it up
1534 * nfs_remove(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
1535 * struct vnode *a_vp, struct componentname *a_cnp)
1538 nfs_remove(struct vop_remove_args *ap)
1540 struct vnode *vp = ap->a_vp;
1541 struct vnode *dvp = ap->a_dvp;
1542 struct componentname *cnp = ap->a_cnp;
1543 struct nfsnode *np = VTONFS(vp);
1548 if ((cnp->cn_flags & CNP_HASBUF) == 0)
1549 panic("nfs_remove: no name");
1550 if (vp->v_usecount < 1)
1551 panic("nfs_remove: bad v_usecount");
1553 if (vp->v_type == VDIR)
1555 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1556 VOP_GETATTR(vp, &vattr, cnp->cn_td) == 0 &&
1557 vattr.va_nlink > 1)) {
1559 * Purge the name cache so that the chance of a lookup for
1560 * the name succeeding while the remove is in progress is
1561 * minimized. Without node locking it can still happen, such
1562 * that an I/O op returns ESTALE, but since you get this if
1563 * another host removes the file..
1567 * throw away biocache buffers, mainly to avoid
1568 * unnecessary delayed writes later.
1570 error = nfs_vinvalbuf(vp, 0, cnp->cn_td, 1);
1573 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1574 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1576 * Kludge City: If the first reply to the remove rpc is lost..
1577 * the reply to the retransmitted request will be ENOENT
1578 * since the file was in fact removed
1579 * Therefore, we cheat and return success.
1581 if (error == ENOENT)
1583 } else if (!np->n_sillyrename)
1584 error = nfs_sillyrename(dvp, vp, cnp);
1585 np->n_attrstamp = 0;
1590 * nfs file remove rpc called from nfs_inactive
1593 nfs_removeit(struct sillyrename *sp)
1595 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1600 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1603 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1604 struct ucred *cred, struct thread *td)
1609 caddr_t bpos, dpos, cp2;
1610 int error = 0, wccflag = NFSV3_WCCRATTR;
1611 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1612 int v3 = NFS_ISV3(dvp);
1614 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1615 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1616 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1617 nfsm_fhtom(dvp, v3);
1618 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1619 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1621 nfsm_wcc_data(dvp, wccflag);
1624 VTONFS(dvp)->n_flag |= NMODIFIED;
1626 VTONFS(dvp)->n_attrstamp = 0;
1631 * nfs file rename call
1633 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1634 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1635 * struct vnode *a_tvp, struct componentname *a_tcnp)
1638 nfs_rename(struct vop_rename_args *ap)
1640 struct vnode *fvp = ap->a_fvp;
1641 struct vnode *tvp = ap->a_tvp;
1642 struct vnode *fdvp = ap->a_fdvp;
1643 struct vnode *tdvp = ap->a_tdvp;
1644 struct componentname *tcnp = ap->a_tcnp;
1645 struct componentname *fcnp = ap->a_fcnp;
1649 if ((tcnp->cn_flags & CNP_HASBUF) == 0 ||
1650 (fcnp->cn_flags & CNP_HASBUF) == 0)
1651 panic("nfs_rename: no name");
1653 /* Check for cross-device rename */
1654 if ((fvp->v_mount != tdvp->v_mount) ||
1655 (tvp && (fvp->v_mount != tvp->v_mount))) {
1661 * We have to flush B_DELWRI data prior to renaming
1662 * the file. If we don't, the delayed-write buffers
1663 * can be flushed out later after the file has gone stale
1664 * under NFSV3. NFSV2 does not have this problem because
1665 * ( as far as I can tell ) it flushes dirty buffers more
1669 VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_td);
1671 VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_td);
1674 * If the tvp exists and is in use, sillyrename it before doing the
1675 * rename of the new file over it.
1676 * XXX Can't sillyrename a directory.
1678 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1679 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1684 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1685 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1688 if (fvp->v_type == VDIR) {
1689 if (tvp != NULL && tvp->v_type == VDIR)
1704 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1706 if (error == ENOENT)
1712 * nfs file rename rpc called from nfs_remove() above
1715 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1716 struct sillyrename *sp)
1718 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1719 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1723 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1726 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1727 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1728 struct ucred *cred, struct thread *td)
1733 caddr_t bpos, dpos, cp2;
1734 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1735 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1736 int v3 = NFS_ISV3(fdvp);
1738 nfsstats.rpccnt[NFSPROC_RENAME]++;
1739 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1740 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1741 nfsm_rndup(tnamelen));
1742 nfsm_fhtom(fdvp, v3);
1743 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1744 nfsm_fhtom(tdvp, v3);
1745 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1746 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1748 nfsm_wcc_data(fdvp, fwccflag);
1749 nfsm_wcc_data(tdvp, twccflag);
1753 VTONFS(fdvp)->n_flag |= NMODIFIED;
1754 VTONFS(tdvp)->n_flag |= NMODIFIED;
1756 VTONFS(fdvp)->n_attrstamp = 0;
1758 VTONFS(tdvp)->n_attrstamp = 0;
1763 * nfs hard link create call
1765 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1766 * struct componentname *a_cnp)
1769 nfs_link(struct vop_link_args *ap)
1771 struct vnode *vp = ap->a_vp;
1772 struct vnode *tdvp = ap->a_tdvp;
1773 struct componentname *cnp = ap->a_cnp;
1777 caddr_t bpos, dpos, cp2;
1778 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1779 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1782 if (vp->v_mount != tdvp->v_mount) {
1787 * Push all writes to the server, so that the attribute cache
1788 * doesn't get "out of sync" with the server.
1789 * XXX There should be a better way!
1791 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_td);
1794 nfsstats.rpccnt[NFSPROC_LINK]++;
1795 nfsm_reqhead(vp, NFSPROC_LINK,
1796 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1798 nfsm_fhtom(tdvp, v3);
1799 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1800 nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1802 nfsm_postop_attr(vp, attrflag);
1803 nfsm_wcc_data(tdvp, wccflag);
1807 VTONFS(tdvp)->n_flag |= NMODIFIED;
1809 VTONFS(vp)->n_attrstamp = 0;
1811 VTONFS(tdvp)->n_attrstamp = 0;
1813 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1815 if (error == EEXIST)
1821 * nfs symbolic link create call
1823 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1824 * struct componentname *a_cnp, struct vattr *a_vap,
1828 nfs_symlink(struct vop_symlink_args *ap)
1830 struct vnode *dvp = ap->a_dvp;
1831 struct vattr *vap = ap->a_vap;
1832 struct componentname *cnp = ap->a_cnp;
1833 struct nfsv2_sattr *sp;
1837 caddr_t bpos, dpos, cp2;
1838 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1839 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1840 struct vnode *newvp = (struct vnode *)0;
1841 int v3 = NFS_ISV3(dvp);
1843 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1844 slen = strlen(ap->a_target);
1845 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1846 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1847 nfsm_fhtom(dvp, v3);
1848 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1850 nfsm_v3attrbuild(vap, FALSE);
1852 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1854 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1855 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1856 sp->sa_uid = nfs_xdrneg1;
1857 sp->sa_gid = nfs_xdrneg1;
1858 sp->sa_size = nfs_xdrneg1;
1859 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1860 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1864 * Issue the NFS request and get the rpc response.
1866 * Only NFSv3 responses returning an error of 0 actually return
1867 * a file handle that can be converted into newvp without having
1868 * to do an extra lookup rpc.
1870 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1873 nfsm_mtofh(dvp, newvp, v3, gotvp);
1874 nfsm_wcc_data(dvp, wccflag);
1878 * out code jumps -> here, mrep is also freed.
1885 * If we get an EEXIST error, silently convert it to no-error
1886 * in case of an NFS retry.
1888 if (error == EEXIST)
1892 * If we do not have (or no longer have) an error, and we could
1893 * not extract the newvp from the response due to the request being
1894 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1895 * to obtain a newvp to return.
1897 if (error == 0 && newvp == NULL) {
1898 struct nfsnode *np = NULL;
1900 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1901 cnp->cn_cred, cnp->cn_td, &np);
1911 VTONFS(dvp)->n_flag |= NMODIFIED;
1913 VTONFS(dvp)->n_attrstamp = 0;
1920 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1921 * struct componentname *a_cnp, struct vattr *a_vap)
1924 nfs_mkdir(struct vop_mkdir_args *ap)
1926 struct vnode *dvp = ap->a_dvp;
1927 struct vattr *vap = ap->a_vap;
1928 struct componentname *cnp = ap->a_cnp;
1929 struct nfsv2_sattr *sp;
1934 struct nfsnode *np = (struct nfsnode *)0;
1935 struct vnode *newvp = (struct vnode *)0;
1936 caddr_t bpos, dpos, cp2;
1937 int error = 0, wccflag = NFSV3_WCCRATTR;
1939 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1941 int v3 = NFS_ISV3(dvp);
1943 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1946 len = cnp->cn_namelen;
1947 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1948 nfsm_reqhead(dvp, NFSPROC_MKDIR,
1949 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1950 nfsm_fhtom(dvp, v3);
1951 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1953 nfsm_v3attrbuild(vap, FALSE);
1955 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1956 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1957 sp->sa_uid = nfs_xdrneg1;
1958 sp->sa_gid = nfs_xdrneg1;
1959 sp->sa_size = nfs_xdrneg1;
1960 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1961 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1963 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
1965 nfsm_mtofh(dvp, newvp, v3, gotvp);
1967 nfsm_wcc_data(dvp, wccflag);
1970 VTONFS(dvp)->n_flag |= NMODIFIED;
1972 VTONFS(dvp)->n_attrstamp = 0;
1974 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1975 * if we can succeed in looking up the directory.
1977 if (error == EEXIST || (!error && !gotvp)) {
1980 newvp = (struct vnode *)0;
1982 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
1986 if (newvp->v_type != VDIR)
1999 * nfs remove directory call
2001 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2002 * struct componentname *a_cnp)
2005 nfs_rmdir(struct vop_rmdir_args *ap)
2007 struct vnode *vp = ap->a_vp;
2008 struct vnode *dvp = ap->a_dvp;
2009 struct componentname *cnp = ap->a_cnp;
2013 caddr_t bpos, dpos, cp2;
2014 int error = 0, wccflag = NFSV3_WCCRATTR;
2015 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2016 int v3 = NFS_ISV3(dvp);
2020 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2021 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2022 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2023 nfsm_fhtom(dvp, v3);
2024 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2025 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2027 nfsm_wcc_data(dvp, wccflag);
2030 VTONFS(dvp)->n_flag |= NMODIFIED;
2032 VTONFS(dvp)->n_attrstamp = 0;
2036 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2038 if (error == ENOENT)
2046 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2049 nfs_readdir(struct vop_readdir_args *ap)
2051 struct vnode *vp = ap->a_vp;
2052 struct nfsnode *np = VTONFS(vp);
2053 struct uio *uio = ap->a_uio;
2057 if (vp->v_type != VDIR)
2060 * First, check for hit on the EOF offset cache
2062 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2063 (np->n_flag & NMODIFIED) == 0) {
2064 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
2065 if (NQNFS_CKCACHABLE(vp, ND_READ)) {
2066 nfsstats.direofcache_hits++;
2069 } else if (VOP_GETATTR(vp, &vattr, uio->uio_td) == 0 &&
2070 np->n_mtime == vattr.va_mtime.tv_sec) {
2071 nfsstats.direofcache_hits++;
2077 * Call nfs_bioread() to do the real work.
2079 tresid = uio->uio_resid;
2080 error = nfs_bioread(vp, uio, 0);
2082 if (!error && uio->uio_resid == tresid)
2083 nfsstats.direofcache_misses++;
2089 * Called from below the buffer cache by nfs_doio().
2092 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2095 struct dirent *dp = NULL;
2100 caddr_t bpos, dpos, cp2;
2101 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2103 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2104 struct nfsnode *dnp = VTONFS(vp);
2106 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2108 int v3 = NFS_ISV3(vp);
2111 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2112 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2113 panic("nfs readdirrpc bad uio");
2117 * If there is no cookie, assume directory was stale.
2119 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2123 return (NFSERR_BAD_COOKIE);
2125 * Loop around doing readdir rpc's of size nm_readdirsize
2126 * truncated to a multiple of DIRBLKSIZ.
2127 * The stopping criteria is EOF or buffer full.
2129 while (more_dirs && bigenough) {
2130 nfsstats.rpccnt[NFSPROC_READDIR]++;
2131 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2135 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2136 *tl++ = cookie.nfsuquad[0];
2137 *tl++ = cookie.nfsuquad[1];
2138 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2139 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2141 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2142 *tl++ = cookie.nfsuquad[0];
2144 *tl = txdr_unsigned(nmp->nm_readdirsize);
2145 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2147 nfsm_postop_attr(vp, attrflag);
2149 nfsm_dissect(tl, u_int32_t *,
2151 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2152 dnp->n_cookieverf.nfsuquad[1] = *tl;
2158 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2159 more_dirs = fxdr_unsigned(int, *tl);
2161 /* loop thru the dir entries, doctoring them to 4bsd form */
2162 while (more_dirs && bigenough) {
2164 nfsm_dissect(tl, u_int32_t *,
2166 fileno = fxdr_hyper(tl);
2167 len = fxdr_unsigned(int, *(tl + 2));
2169 nfsm_dissect(tl, u_int32_t *,
2171 fileno = fxdr_unsigned(u_quad_t, *tl++);
2172 len = fxdr_unsigned(int, *tl);
2174 if (len <= 0 || len > NFS_MAXNAMLEN) {
2179 tlen = nfsm_rndup(len);
2181 tlen += 4; /* To ensure null termination */
2182 left = DIRBLKSIZ - blksiz;
2183 if ((tlen + DIRHDSIZ) > left) {
2184 dp->d_reclen += left;
2185 uiop->uio_iov->iov_base += left;
2186 uiop->uio_iov->iov_len -= left;
2187 uiop->uio_offset += left;
2188 uiop->uio_resid -= left;
2191 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2194 dp = (struct dirent *)uiop->uio_iov->iov_base;
2195 dp->d_fileno = (int)fileno;
2197 dp->d_reclen = tlen + DIRHDSIZ;
2198 dp->d_type = DT_UNKNOWN;
2199 blksiz += dp->d_reclen;
2200 if (blksiz == DIRBLKSIZ)
2202 uiop->uio_offset += DIRHDSIZ;
2203 uiop->uio_resid -= DIRHDSIZ;
2204 uiop->uio_iov->iov_base += DIRHDSIZ;
2205 uiop->uio_iov->iov_len -= DIRHDSIZ;
2206 nfsm_mtouio(uiop, len);
2207 cp = uiop->uio_iov->iov_base;
2209 *cp = '\0'; /* null terminate */
2210 uiop->uio_iov->iov_base += tlen;
2211 uiop->uio_iov->iov_len -= tlen;
2212 uiop->uio_offset += tlen;
2213 uiop->uio_resid -= tlen;
2215 nfsm_adv(nfsm_rndup(len));
2217 nfsm_dissect(tl, u_int32_t *,
2220 nfsm_dissect(tl, u_int32_t *,
2224 cookie.nfsuquad[0] = *tl++;
2226 cookie.nfsuquad[1] = *tl++;
2231 more_dirs = fxdr_unsigned(int, *tl);
2234 * If at end of rpc data, get the eof boolean
2237 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2238 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2243 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2244 * by increasing d_reclen for the last record.
2247 left = DIRBLKSIZ - blksiz;
2248 dp->d_reclen += left;
2249 uiop->uio_iov->iov_base += left;
2250 uiop->uio_iov->iov_len -= left;
2251 uiop->uio_offset += left;
2252 uiop->uio_resid -= left;
2256 * We are now either at the end of the directory or have filled the
2260 dnp->n_direofoffset = uiop->uio_offset;
2262 if (uiop->uio_resid > 0)
2263 printf("EEK! readdirrpc resid > 0\n");
2264 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2272 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2275 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2282 struct vnode *newvp;
2284 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2285 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2286 struct nameidata nami, *ndp = &nami;
2287 struct componentname *cnp = &ndp->ni_cnd;
2289 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2290 struct nfsnode *dnp = VTONFS(vp), *np;
2293 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2294 int attrflag, fhsize;
2297 dp = (struct dirent *)0;
2300 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2301 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2302 panic("nfs readdirplusrpc bad uio");
2308 * If there is no cookie, assume directory was stale.
2310 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2314 return (NFSERR_BAD_COOKIE);
2316 * Loop around doing readdir rpc's of size nm_readdirsize
2317 * truncated to a multiple of DIRBLKSIZ.
2318 * The stopping criteria is EOF or buffer full.
2320 while (more_dirs && bigenough) {
2321 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2322 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2323 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2325 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2326 *tl++ = cookie.nfsuquad[0];
2327 *tl++ = cookie.nfsuquad[1];
2328 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2329 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2330 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2331 *tl = txdr_unsigned(nmp->nm_rsize);
2332 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2333 nfsm_postop_attr(vp, attrflag);
2338 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2339 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2340 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2341 more_dirs = fxdr_unsigned(int, *tl);
2343 /* loop thru the dir entries, doctoring them to 4bsd form */
2344 while (more_dirs && bigenough) {
2345 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2346 fileno = fxdr_hyper(tl);
2347 len = fxdr_unsigned(int, *(tl + 2));
2348 if (len <= 0 || len > NFS_MAXNAMLEN) {
2353 tlen = nfsm_rndup(len);
2355 tlen += 4; /* To ensure null termination*/
2356 left = DIRBLKSIZ - blksiz;
2357 if ((tlen + DIRHDSIZ) > left) {
2358 dp->d_reclen += left;
2359 uiop->uio_iov->iov_base += left;
2360 uiop->uio_iov->iov_len -= left;
2361 uiop->uio_offset += left;
2362 uiop->uio_resid -= left;
2365 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2368 dp = (struct dirent *)uiop->uio_iov->iov_base;
2369 dp->d_fileno = (int)fileno;
2371 dp->d_reclen = tlen + DIRHDSIZ;
2372 dp->d_type = DT_UNKNOWN;
2373 blksiz += dp->d_reclen;
2374 if (blksiz == DIRBLKSIZ)
2376 uiop->uio_offset += DIRHDSIZ;
2377 uiop->uio_resid -= DIRHDSIZ;
2378 uiop->uio_iov->iov_base += DIRHDSIZ;
2379 uiop->uio_iov->iov_len -= DIRHDSIZ;
2380 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2381 cnp->cn_namelen = len;
2382 nfsm_mtouio(uiop, len);
2383 cp = uiop->uio_iov->iov_base;
2386 uiop->uio_iov->iov_base += tlen;
2387 uiop->uio_iov->iov_len -= tlen;
2388 uiop->uio_offset += tlen;
2389 uiop->uio_resid -= tlen;
2391 nfsm_adv(nfsm_rndup(len));
2392 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2394 cookie.nfsuquad[0] = *tl++;
2395 cookie.nfsuquad[1] = *tl++;
2400 * Since the attributes are before the file handle
2401 * (sigh), we must skip over the attributes and then
2402 * come back and get them.
2404 attrflag = fxdr_unsigned(int, *tl);
2408 nfsm_adv(NFSX_V3FATTR);
2409 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2410 doit = fxdr_unsigned(int, *tl);
2412 nfsm_getfh(fhp, fhsize, 1);
2413 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2418 error = nfs_nget(vp->v_mount, fhp,
2426 if (doit && bigenough) {
2431 nfsm_loadattr(newvp, (struct vattr *)0);
2435 IFTODT(VTTOIF(np->n_vattr.va_type));
2437 cache_enter(ndp->ni_dvp, NCPNULL, ndp->ni_vp, cnp);
2440 /* Just skip over the file handle */
2441 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2442 i = fxdr_unsigned(int, *tl);
2443 nfsm_adv(nfsm_rndup(i));
2445 if (newvp != NULLVP) {
2452 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2453 more_dirs = fxdr_unsigned(int, *tl);
2456 * If at end of rpc data, get the eof boolean
2459 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2460 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2465 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2466 * by increasing d_reclen for the last record.
2469 left = DIRBLKSIZ - blksiz;
2470 dp->d_reclen += left;
2471 uiop->uio_iov->iov_base += left;
2472 uiop->uio_iov->iov_len -= left;
2473 uiop->uio_offset += left;
2474 uiop->uio_resid -= left;
2478 * We are now either at the end of the directory or have filled the
2482 dnp->n_direofoffset = uiop->uio_offset;
2484 if (uiop->uio_resid > 0)
2485 printf("EEK! readdirplusrpc resid > 0\n");
2486 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2490 if (newvp != NULLVP) {
2501 * Silly rename. To make the NFS filesystem that is stateless look a little
2502 * more like the "ufs" a remove of an active vnode is translated to a rename
2503 * to a funny looking filename that is removed by nfs_inactive on the
2504 * nfsnode. There is the potential for another process on a different client
2505 * to create the same funny name between the nfs_lookitup() fails and the
2506 * nfs_rename() completes, but...
2509 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2511 struct sillyrename *sp;
2518 if (vp->v_type == VDIR)
2519 panic("nfs: sillyrename dir");
2521 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2522 M_NFSREQ, M_WAITOK);
2523 sp->s_cred = crdup(cnp->cn_cred);
2527 /* Fudge together a funny name */
2528 sp->s_namlen = sprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
2530 /* Try lookitups until we get one that isn't there */
2531 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2532 cnp->cn_td, (struct nfsnode **)0) == 0) {
2534 if (sp->s_name[4] > 'z') {
2539 error = nfs_renameit(dvp, cnp, sp);
2542 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2544 np->n_sillyrename = sp;
2549 free((caddr_t)sp, M_NFSREQ);
2554 * Look up a file name and optionally either update the file handle or
2555 * allocate an nfsnode, depending on the value of npp.
2556 * npp == NULL --> just do the lookup
2557 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2559 * *npp != NULL --> update the file handle in the vnode
2562 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2563 struct thread *td, struct nfsnode **npp)
2568 struct vnode *newvp = (struct vnode *)0;
2569 struct nfsnode *np, *dnp = VTONFS(dvp);
2570 caddr_t bpos, dpos, cp2;
2571 int error = 0, fhlen, attrflag;
2572 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2574 int v3 = NFS_ISV3(dvp);
2576 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2577 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2578 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2579 nfsm_fhtom(dvp, v3);
2580 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2581 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2582 if (npp && !error) {
2583 nfsm_getfh(nfhp, fhlen, v3);
2586 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2587 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2588 np->n_fhp = &np->n_fh;
2589 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2590 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2591 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2592 np->n_fhsize = fhlen;
2594 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2598 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2606 nfsm_postop_attr(newvp, attrflag);
2607 if (!attrflag && *npp == NULL) {
2616 nfsm_loadattr(newvp, (struct vattr *)0);
2620 if (npp && *npp == NULL) {
2635 * Nfs Version 3 commit rpc
2638 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2643 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2644 caddr_t bpos, dpos, cp2;
2645 int error = 0, wccflag = NFSV3_WCCRATTR;
2646 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2648 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2650 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2651 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2653 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2654 txdr_hyper(offset, tl);
2656 *tl = txdr_unsigned(cnt);
2657 nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2658 nfsm_wcc_data(vp, wccflag);
2660 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2661 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2662 NFSX_V3WRITEVERF)) {
2663 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2665 error = NFSERR_STALEWRITEVERF;
2675 * - make nfs_bmap() essentially a no-op that does no translation
2676 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2677 * (Maybe I could use the process's page mapping, but I was concerned that
2678 * Kernel Write might not be enabled and also figured copyout() would do
2679 * a lot more work than bcopy() and also it currently happens in the
2680 * context of the swapper process (2).
2682 * nfs_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
2683 * daddr_t *a_bnp, int *a_runp, int *a_runb)
2686 nfs_bmap(struct vop_bmap_args *ap)
2688 struct vnode *vp = ap->a_vp;
2690 if (ap->a_vpp != NULL)
2692 if (ap->a_bnp != NULL)
2693 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2694 if (ap->a_runp != NULL)
2696 if (ap->a_runb != NULL)
2703 * For async requests when nfsiod(s) are running, queue the request by
2704 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2708 nfs_strategy(struct vop_strategy_args *ap)
2710 struct buf *bp = ap->a_bp;
2714 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2715 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2717 if (bp->b_flags & B_PHYS)
2718 panic("nfs physio");
2720 if (bp->b_flags & B_ASYNC)
2723 td = curthread; /* XXX */
2726 * If the op is asynchronous and an i/o daemon is waiting
2727 * queue the request, wake it up and wait for completion
2728 * otherwise just do it ourselves.
2730 if ((bp->b_flags & B_ASYNC) == 0 ||
2731 nfs_asyncio(bp, td))
2732 error = nfs_doio(bp, td);
2739 * NB Currently unsupported.
2741 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
2742 * struct thread *a_td)
2746 nfs_mmap(struct vop_mmap_args *ap)
2752 * fsync vnode op. Just call nfs_flush() with commit == 1.
2754 * nfs_fsync(struct vnodeop_desc *a_desc, struct vnode *a_vp,
2755 * struct ucred * a_cred, int a_waitfor, struct thread *a_td)
2759 nfs_fsync(struct vop_fsync_args *ap)
2761 return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2765 * Flush all the blocks associated with a vnode.
2766 * Walk through the buffer pool and push any dirty pages
2767 * associated with the vnode.
2770 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
2772 struct nfsnode *np = VTONFS(vp);
2776 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2777 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2779 u_quad_t off, endoff, toff;
2780 struct buf **bvec = NULL;
2781 #ifndef NFS_COMMITBVECSIZ
2782 #define NFS_COMMITBVECSIZ 20
2784 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2785 int bvecsize = 0, bveccount;
2787 if (nmp->nm_flag & NFSMNT_INT)
2792 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2793 * server, but nas not been committed to stable storage on the server
2794 * yet. On the first pass, the byte range is worked out and the commit
2795 * rpc is done. On the second pass, nfs_writebp() is called to do the
2802 if (NFS_ISV3(vp) && commit) {
2805 * Count up how many buffers waiting for a commit.
2808 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2809 nbp = TAILQ_NEXT(bp, b_vnbufs);
2810 if (BUF_REFCNT(bp) == 0 &&
2811 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2812 == (B_DELWRI | B_NEEDCOMMIT))
2816 * Allocate space to remember the list of bufs to commit. It is
2817 * important to use M_NOWAIT here to avoid a race with nfs_write.
2818 * If we can't get memory (for whatever reason), we will end up
2819 * committing the buffers one-by-one in the loop below.
2821 if (bvec != NULL && bvec != bvec_on_stack)
2823 if (bveccount > NFS_COMMITBVECSIZ) {
2824 bvec = (struct buf **)
2825 malloc(bveccount * sizeof(struct buf *),
2828 bvec = bvec_on_stack;
2829 bvecsize = NFS_COMMITBVECSIZ;
2831 bvecsize = bveccount;
2833 bvec = bvec_on_stack;
2834 bvecsize = NFS_COMMITBVECSIZ;
2836 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2837 nbp = TAILQ_NEXT(bp, b_vnbufs);
2838 if (bvecpos >= bvecsize)
2840 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2841 (B_DELWRI | B_NEEDCOMMIT) ||
2842 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
2846 * NOTE: we are not clearing B_DONE here, so we have
2847 * to do it later on in this routine if we intend to
2848 * initiate I/O on the bp.
2850 * Note: to avoid loopback deadlocks, we do not
2851 * assign b_runningbufspace.
2853 bp->b_flags |= B_WRITEINPROG;
2854 vfs_busy_pages(bp, 1);
2857 * bp is protected by being locked, but nbp is not
2858 * and vfs_busy_pages() may sleep. We have to
2861 nbp = TAILQ_NEXT(bp, b_vnbufs);
2864 * A list of these buffers is kept so that the
2865 * second loop knows which buffers have actually
2866 * been committed. This is necessary, since there
2867 * may be a race between the commit rpc and new
2868 * uncommitted writes on the file.
2870 bvec[bvecpos++] = bp;
2871 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2875 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2883 * Commit data on the server, as required. Note that
2884 * nfs_commit will use the vnode's cred for the commit.
2886 retv = nfs_commit(vp, off, (int)(endoff - off), td);
2888 if (retv == NFSERR_STALEWRITEVERF)
2889 nfs_clearcommit(vp->v_mount);
2892 * Now, either mark the blocks I/O done or mark the
2893 * blocks dirty, depending on whether the commit
2896 for (i = 0; i < bvecpos; i++) {
2898 bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG | B_CLUSTEROK);
2901 * Error, leave B_DELWRI intact
2903 vfs_unbusy_pages(bp);
2907 * Success, remove B_DELWRI ( bundirty() ).
2909 * b_dirtyoff/b_dirtyend seem to be NFS
2910 * specific. We should probably move that
2911 * into bundirty(). XXX
2915 bp->b_flags |= B_ASYNC;
2917 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
2918 bp->b_dirtyoff = bp->b_dirtyend = 0;
2926 * Start/do any write(s) that are required.
2930 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2931 nbp = TAILQ_NEXT(bp, b_vnbufs);
2932 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
2933 if (waitfor != MNT_WAIT || passone)
2935 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL,
2936 "nfsfsync", slpflag, slptimeo);
2939 panic("nfs_fsync: inconsistent lock");
2940 if (error == ENOLCK)
2942 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
2946 if (slpflag == PCATCH) {
2952 if ((bp->b_flags & B_DELWRI) == 0)
2953 panic("nfs_fsync: not dirty");
2954 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2959 if (passone || !commit)
2960 bp->b_flags |= B_ASYNC;
2962 bp->b_flags |= B_ASYNC | B_WRITEINPROG;
2964 VOP_BWRITE(bp->b_vp, bp);
2972 if (waitfor == MNT_WAIT) {
2973 while (vp->v_numoutput) {
2974 vp->v_flag |= VBWAIT;
2975 error = tsleep((caddr_t)&vp->v_numoutput,
2976 slpflag, "nfsfsync", slptimeo);
2978 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
2982 if (slpflag == PCATCH) {
2988 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) && commit) {
2992 if (np->n_flag & NWRITEERR) {
2993 error = np->n_error;
2994 np->n_flag &= ~NWRITEERR;
2997 if (bvec != NULL && bvec != bvec_on_stack)
3003 * NFS advisory byte-level locks.
3004 * Currently unsupported.
3006 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3010 nfs_advlock(struct vop_advlock_args *ap)
3012 struct nfsnode *np = VTONFS(ap->a_vp);
3015 * The following kludge is to allow diskless support to work
3016 * until a real NFS lockd is implemented. Basically, just pretend
3017 * that this is a local lock.
3019 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3023 * Print out the contents of an nfsnode.
3025 * nfs_print(struct vnode *a_vp)
3028 nfs_print(struct vop_print_args *ap)
3030 struct vnode *vp = ap->a_vp;
3031 struct nfsnode *np = VTONFS(vp);
3033 printf("tag VT_NFS, fileid %ld fsid 0x%x",
3034 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3035 if (vp->v_type == VFIFO)
3042 * Just call nfs_writebp() with the force argument set to 1.
3044 * NOTE: B_DONE may or may not be set in a_bp on call.
3046 * nfs_bwrite(struct vnode *a_bp)
3049 nfs_bwrite(struct vop_bwrite_args *ap)
3051 return (nfs_writebp(ap->a_bp, 1, curthread));
3055 * This is a clone of vn_bwrite(), except that B_WRITEINPROG isn't set unless
3056 * the force flag is one and it also handles the B_NEEDCOMMIT flag. We set
3057 * B_CACHE if this is a VMIO buffer.
3060 nfs_writebp(struct buf *bp, int force, struct thread *td)
3063 int oldflags = bp->b_flags;
3069 if (BUF_REFCNT(bp) == 0)
3070 panic("bwrite: buffer is not locked???");
3072 if (bp->b_flags & B_INVAL) {
3077 bp->b_flags |= B_CACHE;
3080 * Undirty the bp. We will redirty it later if the I/O fails.
3085 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3087 bp->b_vp->v_numoutput++;
3091 * Note: to avoid loopback deadlocks, we do not
3092 * assign b_runningbufspace.
3094 vfs_busy_pages(bp, 1);
3097 bp->b_flags |= B_WRITEINPROG;
3099 VOP_STRATEGY(bp->b_vp, bp);
3101 if( (oldflags & B_ASYNC) == 0) {
3102 int rtval = biowait(bp);
3104 if (oldflags & B_DELWRI) {
3106 reassignbuf(bp, bp->b_vp);
3118 * nfs special file access vnode op.
3119 * Essentially just get vattr and then imitate iaccess() since the device is
3120 * local to the client.
3122 * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
3123 * struct thread *a_td)
3126 nfsspec_access(struct vop_access_args *ap)
3130 struct ucred *cred = ap->a_cred;
3131 struct vnode *vp = ap->a_vp;
3132 mode_t mode = ap->a_mode;
3138 * Disallow write attempts on filesystems mounted read-only;
3139 * unless the file is a socket, fifo, or a block or character
3140 * device resident on the filesystem.
3142 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3143 switch (vp->v_type) {
3153 * If you're the super-user,
3154 * you always get access.
3156 if (cred->cr_uid == 0)
3159 error = VOP_GETATTR(vp, vap, ap->a_td);
3163 * Access check is based on only one of owner, group, public.
3164 * If not owner, then check group. If not a member of the
3165 * group, then check public access.
3167 if (cred->cr_uid != vap->va_uid) {
3169 gp = cred->cr_groups;
3170 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3171 if (vap->va_gid == *gp)
3177 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3182 * Read wrapper for special devices.
3184 * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3185 * struct ucred *a_cred)
3188 nfsspec_read(struct vop_read_args *ap)
3190 struct nfsnode *np = VTONFS(ap->a_vp);
3196 getnanotime(&np->n_atim);
3197 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3201 * Write wrapper for special devices.
3203 * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3204 * struct ucred *a_cred)
3207 nfsspec_write(struct vop_write_args *ap)
3209 struct nfsnode *np = VTONFS(ap->a_vp);
3215 getnanotime(&np->n_mtim);
3216 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3220 * Close wrapper for special devices.
3222 * Update the times on the nfsnode then do device close.
3224 * nfsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
3225 * struct thread *a_td)
3228 nfsspec_close(struct vop_close_args *ap)
3230 struct vnode *vp = ap->a_vp;
3231 struct nfsnode *np = VTONFS(vp);
3234 if (np->n_flag & (NACC | NUPD)) {
3236 if (vp->v_usecount == 1 &&
3237 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3239 if (np->n_flag & NACC)
3240 vattr.va_atime = np->n_atim;
3241 if (np->n_flag & NUPD)
3242 vattr.va_mtime = np->n_mtim;
3243 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3246 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3250 * Read wrapper for fifos.
3252 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3253 * struct ucred *a_cred)
3256 nfsfifo_read(struct vop_read_args *ap)
3258 struct nfsnode *np = VTONFS(ap->a_vp);
3264 getnanotime(&np->n_atim);
3265 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3269 * Write wrapper for fifos.
3271 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3272 * struct ucred *a_cred)
3275 nfsfifo_write(struct vop_write_args *ap)
3277 struct nfsnode *np = VTONFS(ap->a_vp);
3283 getnanotime(&np->n_mtim);
3284 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3288 * Close wrapper for fifos.
3290 * Update the times on the nfsnode then do fifo close.
3292 * nfsfifo_close(struct vnode *a_vp, int a_fflag, struct thread *a_td)
3295 nfsfifo_close(struct vop_close_args *ap)
3297 struct vnode *vp = ap->a_vp;
3298 struct nfsnode *np = VTONFS(vp);
3302 if (np->n_flag & (NACC | NUPD)) {
3304 if (np->n_flag & NACC)
3306 if (np->n_flag & NUPD)
3309 if (vp->v_usecount == 1 &&
3310 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3312 if (np->n_flag & NACC)
3313 vattr.va_atime = np->n_atim;
3314 if (np->n_flag & NUPD)
3315 vattr.va_mtime = np->n_mtim;
3316 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3319 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));