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.20 2004/04/08 22:32:14 dillon Exp $
43 * vnode op calls for Sun NFS version 2 and 3
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/resourcevar.h>
53 #include <sys/mount.h>
55 #include <sys/malloc.h>
57 #include <sys/namei.h>
58 #include <sys/socket.h>
59 #include <sys/vnode.h>
60 #include <sys/dirent.h>
61 #include <sys/fcntl.h>
62 #include <sys/lockf.h>
64 #include <sys/sysctl.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_zone.h>
73 #include <vfs/fifofs/fifo.h>
81 #include "nfsm_subs.h"
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
93 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
94 * calls are not in getblk() and brelse() so that they would not be necessary
98 #define vfs_busy_pages(bp, f)
101 static int nfsspec_read (struct vop_read_args *);
102 static int nfsspec_write (struct vop_write_args *);
103 static int nfsfifo_read (struct vop_read_args *);
104 static int nfsfifo_write (struct vop_write_args *);
105 static int nfsspec_close (struct vop_close_args *);
106 static int nfsfifo_close (struct vop_close_args *);
107 #define nfs_poll vop_nopoll
108 static int nfs_flush (struct vnode *,int,struct thread *,int);
109 static int nfs_setattrrpc (struct vnode *,struct vattr *,struct ucred *,struct thread *);
110 static int nfs_lookup (struct vop_lookup_args *);
111 static int nfs_create (struct vop_create_args *);
112 static int nfs_mknod (struct vop_mknod_args *);
113 static int nfs_open (struct vop_open_args *);
114 static int nfs_close (struct vop_close_args *);
115 static int nfs_access (struct vop_access_args *);
116 static int nfs_getattr (struct vop_getattr_args *);
117 static int nfs_setattr (struct vop_setattr_args *);
118 static int nfs_read (struct vop_read_args *);
119 static int nfs_mmap (struct vop_mmap_args *);
120 static int nfs_fsync (struct vop_fsync_args *);
121 static int nfs_remove (struct vop_remove_args *);
122 static int nfs_link (struct vop_link_args *);
123 static int nfs_rename (struct vop_rename_args *);
124 static int nfs_mkdir (struct vop_mkdir_args *);
125 static int nfs_rmdir (struct vop_rmdir_args *);
126 static int nfs_symlink (struct vop_symlink_args *);
127 static int nfs_readdir (struct vop_readdir_args *);
128 static int nfs_bmap (struct vop_bmap_args *);
129 static int nfs_strategy (struct vop_strategy_args *);
130 static int nfs_lookitup (struct vnode *, const char *, int,
131 struct ucred *, struct thread *, struct nfsnode **);
132 static int nfs_sillyrename (struct vnode *,struct vnode *,struct componentname *);
133 static int nfsspec_access (struct vop_access_args *);
134 static int nfs_readlink (struct vop_readlink_args *);
135 static int nfs_print (struct vop_print_args *);
136 static int nfs_advlock (struct vop_advlock_args *);
137 static int nfs_bwrite (struct vop_bwrite_args *);
139 * Global vfs data structures for nfs
141 vop_t **nfsv2_vnodeop_p;
142 static struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
143 { &vop_default_desc, (vop_t *) vop_defaultop },
144 { &vop_access_desc, (vop_t *) nfs_access },
145 { &vop_advlock_desc, (vop_t *) nfs_advlock },
146 { &vop_bmap_desc, (vop_t *) nfs_bmap },
147 { &vop_bwrite_desc, (vop_t *) nfs_bwrite },
148 { &vop_close_desc, (vop_t *) nfs_close },
149 { &vop_create_desc, (vop_t *) nfs_create },
150 { &vop_fsync_desc, (vop_t *) nfs_fsync },
151 { &vop_getattr_desc, (vop_t *) nfs_getattr },
152 { &vop_getpages_desc, (vop_t *) nfs_getpages },
153 { &vop_putpages_desc, (vop_t *) nfs_putpages },
154 { &vop_inactive_desc, (vop_t *) nfs_inactive },
155 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
156 { &vop_lease_desc, (vop_t *) vop_null },
157 { &vop_link_desc, (vop_t *) nfs_link },
158 { &vop_lock_desc, (vop_t *) vop_sharedlock },
159 { &vop_lookup_desc, (vop_t *) nfs_lookup },
160 { &vop_mkdir_desc, (vop_t *) nfs_mkdir },
161 { &vop_mknod_desc, (vop_t *) nfs_mknod },
162 { &vop_mmap_desc, (vop_t *) nfs_mmap },
163 { &vop_open_desc, (vop_t *) nfs_open },
164 { &vop_poll_desc, (vop_t *) nfs_poll },
165 { &vop_print_desc, (vop_t *) nfs_print },
166 { &vop_read_desc, (vop_t *) nfs_read },
167 { &vop_readdir_desc, (vop_t *) nfs_readdir },
168 { &vop_readlink_desc, (vop_t *) nfs_readlink },
169 { &vop_reclaim_desc, (vop_t *) nfs_reclaim },
170 { &vop_remove_desc, (vop_t *) nfs_remove },
171 { &vop_rename_desc, (vop_t *) nfs_rename },
172 { &vop_rmdir_desc, (vop_t *) nfs_rmdir },
173 { &vop_setattr_desc, (vop_t *) nfs_setattr },
174 { &vop_strategy_desc, (vop_t *) nfs_strategy },
175 { &vop_symlink_desc, (vop_t *) nfs_symlink },
176 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
177 { &vop_write_desc, (vop_t *) nfs_write },
180 static struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
181 { &nfsv2_vnodeop_p, nfsv2_vnodeop_entries };
182 VNODEOP_SET(nfsv2_vnodeop_opv_desc);
185 * Special device vnode ops
187 vop_t **spec_nfsv2nodeop_p;
188 static struct vnodeopv_entry_desc nfsv2_specop_entries[] = {
189 { &vop_default_desc, (vop_t *) spec_vnoperate },
190 { &vop_access_desc, (vop_t *) nfsspec_access },
191 { &vop_close_desc, (vop_t *) nfsspec_close },
192 { &vop_fsync_desc, (vop_t *) nfs_fsync },
193 { &vop_getattr_desc, (vop_t *) nfs_getattr },
194 { &vop_inactive_desc, (vop_t *) nfs_inactive },
195 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
196 { &vop_lock_desc, (vop_t *) vop_sharedlock },
197 { &vop_print_desc, (vop_t *) nfs_print },
198 { &vop_read_desc, (vop_t *) nfsspec_read },
199 { &vop_reclaim_desc, (vop_t *) nfs_reclaim },
200 { &vop_setattr_desc, (vop_t *) nfs_setattr },
201 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
202 { &vop_write_desc, (vop_t *) nfsspec_write },
205 static struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
206 { &spec_nfsv2nodeop_p, nfsv2_specop_entries };
207 VNODEOP_SET(spec_nfsv2nodeop_opv_desc);
209 vop_t **fifo_nfsv2nodeop_p;
210 static struct vnodeopv_entry_desc nfsv2_fifoop_entries[] = {
211 { &vop_default_desc, (vop_t *) fifo_vnoperate },
212 { &vop_access_desc, (vop_t *) nfsspec_access },
213 { &vop_close_desc, (vop_t *) nfsfifo_close },
214 { &vop_fsync_desc, (vop_t *) nfs_fsync },
215 { &vop_getattr_desc, (vop_t *) nfs_getattr },
216 { &vop_inactive_desc, (vop_t *) nfs_inactive },
217 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
218 { &vop_lock_desc, (vop_t *) vop_sharedlock },
219 { &vop_print_desc, (vop_t *) nfs_print },
220 { &vop_read_desc, (vop_t *) nfsfifo_read },
221 { &vop_reclaim_desc, (vop_t *) nfs_reclaim },
222 { &vop_setattr_desc, (vop_t *) nfs_setattr },
223 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
224 { &vop_write_desc, (vop_t *) nfsfifo_write },
227 static struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
228 { &fifo_nfsv2nodeop_p, nfsv2_fifoop_entries };
229 VNODEOP_SET(fifo_nfsv2nodeop_opv_desc);
231 static int nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
232 struct componentname *cnp,
234 static int nfs_removerpc (struct vnode *dvp, const char *name,
236 struct ucred *cred, struct thread *td);
237 static int nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
238 int fnamelen, struct vnode *tdvp,
239 const char *tnameptr, int tnamelen,
240 struct ucred *cred, struct thread *td);
241 static int nfs_renameit (struct vnode *sdvp,
242 struct componentname *scnp,
243 struct sillyrename *sp);
248 extern u_int32_t nfs_true, nfs_false;
249 extern u_int32_t nfs_xdrneg1;
250 extern struct nfsstats nfsstats;
251 extern nfstype nfsv3_type[9];
252 struct thread *nfs_iodwant[NFS_MAXASYNCDAEMON];
253 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
254 int nfs_numasync = 0;
255 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
257 SYSCTL_DECL(_vfs_nfs);
259 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
260 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
261 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
263 static int nfsneg_cache_timeout = NFS_MINATTRTIMO;
264 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
265 &nfsneg_cache_timeout, 0, "NFS NEGATIVE ACCESS cache timeout");
267 static int nfsv3_commit_on_close = 0;
268 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
269 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
271 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
272 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
274 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
275 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
278 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
279 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
280 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
282 nfs3_access_otw(struct vnode *vp, int wmode,
283 struct thread *td, struct ucred *cred)
287 int error = 0, attrflag;
289 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
290 caddr_t bpos, dpos, cp2;
294 struct nfsnode *np = VTONFS(vp);
296 nfsstats.rpccnt[NFSPROC_ACCESS]++;
297 nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
299 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
300 *tl = txdr_unsigned(wmode);
301 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
302 nfsm_postop_attr(vp, attrflag);
304 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
305 rmode = fxdr_unsigned(u_int32_t, *tl);
307 np->n_modeuid = cred->cr_uid;
308 np->n_modestamp = mycpu->gd_time_seconds;
316 * nfs access vnode op.
317 * For nfs version 2, just return ok. File accesses may fail later.
318 * For nfs version 3, use the access rpc to check accessibility. If file modes
319 * are changed on the server, accesses might still fail later.
323 struct vop_access_args /* {
326 struct ucred *a_cred;
330 struct vnode *vp = ap->a_vp;
332 u_int32_t mode, wmode;
333 int v3 = NFS_ISV3(vp);
334 struct nfsnode *np = VTONFS(vp);
337 * Disallow write attempts on filesystems mounted read-only;
338 * unless the file is a socket, fifo, or a block or character
339 * device resident on the filesystem.
341 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
342 switch (vp->v_type) {
352 * For nfs v3, check to see if we have done this recently, and if
353 * so return our cached result instead of making an ACCESS call.
354 * If not, do an access rpc, otherwise you are stuck emulating
355 * ufs_access() locally using the vattr. This may not be correct,
356 * since the server may apply other access criteria such as
357 * client uid-->server uid mapping that we do not know about.
360 if (ap->a_mode & VREAD)
361 mode = NFSV3ACCESS_READ;
364 if (vp->v_type != VDIR) {
365 if (ap->a_mode & VWRITE)
366 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
367 if (ap->a_mode & VEXEC)
368 mode |= NFSV3ACCESS_EXECUTE;
370 if (ap->a_mode & VWRITE)
371 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
373 if (ap->a_mode & VEXEC)
374 mode |= NFSV3ACCESS_LOOKUP;
376 /* XXX safety belt, only make blanket request if caching */
377 if (nfsaccess_cache_timeout > 0) {
378 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
379 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
380 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
386 * Does our cached result allow us to give a definite yes to
389 if ((mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
390 (ap->a_cred->cr_uid == np->n_modeuid) &&
391 ((np->n_mode & mode) == mode)) {
392 nfsstats.accesscache_hits++;
395 * Either a no, or a don't know. Go to the wire.
397 nfsstats.accesscache_misses++;
398 error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred);
400 if ((np->n_mode & mode) != mode) {
406 if ((error = nfsspec_access(ap)) != 0)
410 * Attempt to prevent a mapped root from accessing a file
411 * which it shouldn't. We try to read a byte from the file
412 * if the user is root and the file is not zero length.
413 * After calling nfsspec_access, we should have the correct
416 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
417 && VTONFS(vp)->n_size > 0) {
424 auio.uio_iov = &aiov;
428 auio.uio_segflg = UIO_SYSSPACE;
429 auio.uio_rw = UIO_READ;
430 auio.uio_td = ap->a_td;
432 if (vp->v_type == VREG) {
433 error = nfs_readrpc(vp, &auio);
434 } else if (vp->v_type == VDIR) {
436 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
438 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
439 error = nfs_readdirrpc(vp, &auio);
441 } else if (vp->v_type == VLNK) {
442 error = nfs_readlinkrpc(vp, &auio);
449 * [re]record creds for reading and/or writing if access
450 * was granted. Assume the NFS server will grant read access
451 * for execute requests.
454 if ((ap->a_mode & (VREAD|VEXEC)) && ap->a_cred != np->n_rucred) {
457 crfree(np->n_rucred);
458 np->n_rucred = ap->a_cred;
460 if ((ap->a_mode & VWRITE) && ap->a_cred != np->n_wucred) {
463 crfree(np->n_wucred);
464 np->n_wucred = ap->a_cred;
472 * Check to see if the type is ok
473 * and that deletion is not in progress.
474 * For paged in text files, you will need to flush the page cache
475 * if consistency is lost.
480 struct vop_open_args /* {
483 struct ucred *a_cred;
487 struct vnode *vp = ap->a_vp;
488 struct nfsnode *np = VTONFS(vp);
489 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
493 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
495 printf("open eacces vtyp=%d\n",vp->v_type);
500 * Get a valid lease. If cached data is stale, flush it.
502 if (nmp->nm_flag & NFSMNT_NQNFS) {
503 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
505 error = nqnfs_getlease(vp, ND_READ, ap->a_td);
506 } while (error == NQNFS_EXPIRED);
509 if (np->n_lrev != np->n_brev ||
510 (np->n_flag & NQNFSNONCACHE)) {
511 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1))
515 np->n_brev = np->n_lrev;
519 if (np->n_flag & NMODIFIED) {
520 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1))
525 if (vp->v_type == VDIR)
526 np->n_direofoffset = 0;
527 error = VOP_GETATTR(vp, &vattr, ap->a_td);
530 np->n_mtime = vattr.va_mtime.tv_sec;
532 error = VOP_GETATTR(vp, &vattr, ap->a_td);
535 if (np->n_mtime != vattr.va_mtime.tv_sec) {
536 if (vp->v_type == VDIR)
537 np->n_direofoffset = 0;
538 if ((error = nfs_vinvalbuf(vp, V_SAVE,
539 ap->a_td, 1)) == EINTR) {
542 np->n_mtime = vattr.va_mtime.tv_sec;
546 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0)
547 np->n_attrstamp = 0; /* For Open/Close consistency */
553 * What an NFS client should do upon close after writing is a debatable issue.
554 * Most NFS clients push delayed writes to the server upon close, basically for
556 * 1 - So that any write errors may be reported back to the client process
557 * doing the close system call. By far the two most likely errors are
558 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
559 * 2 - To put a worst case upper bound on cache inconsistency between
560 * multiple clients for the file.
561 * There is also a consistency problem for Version 2 of the protocol w.r.t.
562 * not being able to tell if other clients are writing a file concurrently,
563 * since there is no way of knowing if the changed modify time in the reply
564 * is only due to the write for this client.
565 * (NFS Version 3 provides weak cache consistency data in the reply that
566 * should be sufficient to detect and handle this case.)
568 * The current code does the following:
569 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
570 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
571 * or commit them (this satisfies 1 and 2 except for the
572 * case where the server crashes after this close but
573 * before the commit RPC, which is felt to be "good
574 * enough". Changing the last argument to nfs_flush() to
575 * a 1 would force a commit operation, if it is felt a
576 * commit is necessary now.
577 * for NQNFS - do nothing now, since 2 is dealt with via leases and
578 * 1 should be dealt with via an fsync() system call for
579 * cases where write errors are important.
584 struct vop_close_args /* {
585 struct vnodeop_desc *a_desc;
588 struct ucred *a_cred;
592 struct vnode *vp = ap->a_vp;
593 struct nfsnode *np = VTONFS(vp);
596 if (vp->v_type == VREG) {
597 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 &&
598 (np->n_flag & NMODIFIED)) {
601 * Under NFSv3 we have dirty buffers to dispose of. We
602 * must flush them to the NFS server. We have the option
603 * of waiting all the way through the commit rpc or just
604 * waiting for the initial write. The default is to only
605 * wait through the initial write so the data is in the
606 * server's cache, which is roughly similar to the state
607 * a standard disk subsystem leaves the file in on close().
609 * We cannot clear the NMODIFIED bit in np->n_flag due to
610 * potential races with other processes, and certainly
611 * cannot clear it if we don't commit.
613 int cm = nfsv3_commit_on_close ? 1 : 0;
614 error = nfs_flush(vp, MNT_WAIT, ap->a_td, cm);
615 /* np->n_flag &= ~NMODIFIED; */
617 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
621 if (np->n_flag & NWRITEERR) {
622 np->n_flag &= ~NWRITEERR;
630 * nfs getattr call from vfs.
634 struct vop_getattr_args /* {
637 struct ucred *a_cred;
641 struct vnode *vp = ap->a_vp;
642 struct nfsnode *np = VTONFS(vp);
648 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
649 int v3 = NFS_ISV3(vp);
652 * Update local times for special files.
654 if (np->n_flag & (NACC | NUPD))
657 * First look in the cache.
659 if (nfs_getattrcache(vp, ap->a_vap) == 0)
662 if (v3 && nfsaccess_cache_timeout > 0) {
663 nfsstats.accesscache_misses++;
664 nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_td, nfs_vpcred(vp, ND_CHECK));
665 if (nfs_getattrcache(vp, ap->a_vap) == 0)
669 nfsstats.rpccnt[NFSPROC_GETATTR]++;
670 nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
672 nfsm_request(vp, NFSPROC_GETATTR, ap->a_td, nfs_vpcred(vp, ND_CHECK));
674 nfsm_loadattr(vp, ap->a_vap);
686 struct vop_setattr_args /* {
687 struct vnodeop_desc *a_desc;
690 struct ucred *a_cred;
694 struct vnode *vp = ap->a_vp;
695 struct nfsnode *np = VTONFS(vp);
696 struct vattr *vap = ap->a_vap;
705 * Setting of flags is not supported.
707 if (vap->va_flags != VNOVAL)
711 * Disallow write attempts if the filesystem is mounted read-only.
713 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
714 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
715 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
716 (vp->v_mount->mnt_flag & MNT_RDONLY))
718 if (vap->va_size != VNOVAL) {
719 switch (vp->v_type) {
726 if (vap->va_mtime.tv_sec == VNOVAL &&
727 vap->va_atime.tv_sec == VNOVAL &&
728 vap->va_mode == (mode_t)VNOVAL &&
729 vap->va_uid == (uid_t)VNOVAL &&
730 vap->va_gid == (gid_t)VNOVAL)
732 vap->va_size = VNOVAL;
736 * Disallow write attempts if the filesystem is
739 if (vp->v_mount->mnt_flag & MNT_RDONLY)
743 * We run vnode_pager_setsize() early (why?),
744 * we must set np->n_size now to avoid vinvalbuf
745 * V_SAVE races that might setsize a lower
750 error = nfs_meta_setsize(vp, ap->a_td, vap->va_size);
752 if (np->n_flag & NMODIFIED) {
753 if (vap->va_size == 0)
754 error = nfs_vinvalbuf(vp, 0, ap->a_td, 1);
756 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
759 vnode_pager_setsize(vp, np->n_size);
763 /* np->n_size has already been set to vap->va_size
764 * in nfs_meta_setsize(). We must set it again since
765 * nfs_loadattrcache() could be called through
766 * nfs_meta_setsize() and could modify np->n_size.
768 np->n_vattr.va_size = np->n_size = vap->va_size;
770 } else if ((vap->va_mtime.tv_sec != VNOVAL ||
771 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NMODIFIED) &&
772 vp->v_type == VREG &&
773 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) == EINTR)
775 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
776 if (error && vap->va_size != VNOVAL) {
777 np->n_size = np->n_vattr.va_size = tsize;
778 vnode_pager_setsize(vp, np->n_size);
784 * Do an nfs setattr rpc.
787 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
788 struct ucred *cred, struct thread *td)
790 struct nfsv2_sattr *sp;
793 caddr_t bpos, dpos, cp2;
795 int error = 0, wccflag = NFSV3_WCCRATTR;
796 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
797 int v3 = NFS_ISV3(vp);
799 nfsstats.rpccnt[NFSPROC_SETATTR]++;
800 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
803 nfsm_v3attrbuild(vap, TRUE);
804 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
807 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
808 if (vap->va_mode == (mode_t)VNOVAL)
809 sp->sa_mode = nfs_xdrneg1;
811 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
812 if (vap->va_uid == (uid_t)VNOVAL)
813 sp->sa_uid = nfs_xdrneg1;
815 sp->sa_uid = txdr_unsigned(vap->va_uid);
816 if (vap->va_gid == (gid_t)VNOVAL)
817 sp->sa_gid = nfs_xdrneg1;
819 sp->sa_gid = txdr_unsigned(vap->va_gid);
820 sp->sa_size = txdr_unsigned(vap->va_size);
821 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
822 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
824 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
826 nfsm_wcc_data(vp, wccflag);
828 nfsm_loadattr(vp, (struct vattr *)0);
835 * 'cached' nfs directory lookup
839 struct vop_lookup_args /* {
840 struct vnodeop_desc *a_desc;
842 struct vnode **a_vpp;
843 struct componentname *a_cnp;
846 struct componentname *cnp = ap->a_cnp;
847 struct vnode *dvp = ap->a_dvp;
848 struct vnode **vpp = ap->a_vpp;
849 int flags = cnp->cn_flags;
854 struct nfsmount *nmp;
855 caddr_t bpos, dpos, cp2;
856 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
860 int lockparent, wantparent, error = 0, attrflag, fhsize;
861 int v3 = NFS_ISV3(dvp);
862 struct thread *td = cnp->cn_td;
865 * Read-only mount check and directory check.
868 if ((flags & CNP_ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
869 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
872 if (dvp->v_type != VDIR)
876 * Look it up in the cache. Note that ENOENT is only returned if we
877 * previously entered a negative hit (see later on). The additional
878 * nfsneg_cache_timeout check causes previously cached results to
879 * be instantly ignored if the negative caching is turned off.
881 lockparent = flags & CNP_LOCKPARENT;
882 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
883 nmp = VFSTONFS(dvp->v_mount);
885 error = cache_lookup(dvp, NCPNULL, vpp, NCPPNULL, cnp);
890 if (error == ENOENT && nfsneg_cache_timeout) {
894 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
902 * See the comment starting `Step through' in ufs/ufs_lookup.c
903 * for an explanation of the locking protocol
908 } else if (flags & CNP_ISDOTDOT) {
909 VOP_UNLOCK(dvp, NULL, 0, td);
910 error = vget(newvp, NULL, LK_EXCLUSIVE, td);
911 if (!error && lockparent && (flags & CNP_ISLASTCN))
912 error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td);
914 error = vget(newvp, NULL, LK_EXCLUSIVE, td);
915 if (!lockparent || error || !(flags & CNP_ISLASTCN))
916 VOP_UNLOCK(dvp, NULL, 0, td);
919 if (vpid == newvp->v_id) {
920 if (!VOP_GETATTR(newvp, &vattr, td)
921 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
922 nfsstats.lookupcache_hits++;
923 if (cnp->cn_nameiop != NAMEI_LOOKUP &&
924 (flags & CNP_ISLASTCN))
925 cnp->cn_flags |= CNP_SAVENAME;
931 if (lockparent && dvp != newvp && (flags & CNP_ISLASTCN))
932 VOP_UNLOCK(dvp, NULL, 0, td);
934 error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td);
941 * Cache miss, go the wire.
945 nfsstats.lookupcache_misses++;
946 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
947 len = cnp->cn_namelen;
948 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
949 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
951 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
952 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
955 * Cache negatve lookups to reduce NFS traffic, but use
958 if (error == ENOENT &&
959 (cnp->cn_flags & CNP_MAKEENTRY) &&
960 cnp->cn_nameiop == NAMEI_LOOKUP &&
961 nfsneg_cache_timeout) {
962 int toval = nfsneg_cache_timeout * hz;
963 if (cnp->cn_flags & CNP_CACHETIMEOUT) {
964 if (cnp->cn_timeout > toval)
965 cnp->cn_timeout = toval;
967 cnp->cn_flags |= CNP_CACHETIMEOUT;
968 cnp->cn_timeout = toval;
970 cache_enter(dvp, NCPNULL, NULL, cnp);
972 nfsm_postop_attr(dvp, attrflag);
976 nfsm_getfh(fhp, fhsize, v3);
979 * Handle RENAME case...
981 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent && (flags & CNP_ISLASTCN)) {
982 if (NFS_CMPFH(np, fhp, fhsize)) {
986 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
993 nfsm_postop_attr(newvp, attrflag);
994 nfsm_postop_attr(dvp, attrflag);
996 nfsm_loadattr(newvp, (struct vattr *)0);
999 cnp->cn_flags |= CNP_SAVENAME;
1001 VOP_UNLOCK(dvp, NULL, 0, td);
1005 if (flags & CNP_ISDOTDOT) {
1006 VOP_UNLOCK(dvp, NULL, 0, td);
1007 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1009 vn_lock(dvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1013 if (lockparent && (flags & CNP_ISLASTCN) &&
1014 (error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td))) {
1018 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1022 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1027 if (!lockparent || !(flags & CNP_ISLASTCN))
1028 VOP_UNLOCK(dvp, NULL, 0, td);
1032 nfsm_postop_attr(newvp, attrflag);
1033 nfsm_postop_attr(dvp, attrflag);
1035 nfsm_loadattr(newvp, (struct vattr *)0);
1036 if (cnp->cn_nameiop != NAMEI_LOOKUP && (flags & CNP_ISLASTCN))
1037 cnp->cn_flags |= CNP_SAVENAME;
1038 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1039 (cnp->cn_nameiop != NAMEI_DELETE || !(flags & CNP_ISLASTCN))) {
1040 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
1041 cache_enter(dvp, NCPNULL, newvp, cnp);
1047 if (newvp != NULLVP) {
1051 if ((cnp->cn_nameiop == NAMEI_CREATE || cnp->cn_nameiop == NAMEI_RENAME) &&
1052 (flags & CNP_ISLASTCN) && error == ENOENT) {
1054 VOP_UNLOCK(dvp, NULL, 0, td);
1055 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1058 error = EJUSTRETURN;
1060 if (cnp->cn_nameiop != NAMEI_LOOKUP && (flags & CNP_ISLASTCN))
1061 cnp->cn_flags |= CNP_SAVENAME;
1068 * Just call nfs_bioread() to do the work.
1072 struct vop_read_args /* {
1076 struct ucred *a_cred;
1079 struct vnode *vp = ap->a_vp;
1081 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1082 switch (vp->v_type) {
1084 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1097 struct vop_readlink_args /* {
1100 struct ucred *a_cred;
1103 struct vnode *vp = ap->a_vp;
1105 if (vp->v_type != VLNK)
1107 return (nfs_bioread(vp, ap->a_uio, 0));
1111 * Do a readlink rpc.
1112 * Called by nfs_doio() from below the buffer cache.
1115 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1120 caddr_t bpos, dpos, cp2;
1121 int error = 0, len, attrflag;
1122 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1123 int v3 = NFS_ISV3(vp);
1125 nfsstats.rpccnt[NFSPROC_READLINK]++;
1126 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1128 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1130 nfsm_postop_attr(vp, attrflag);
1132 nfsm_strsiz(len, NFS_MAXPATHLEN);
1133 if (len == NFS_MAXPATHLEN) {
1134 struct nfsnode *np = VTONFS(vp);
1135 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1138 nfsm_mtouio(uiop, len);
1150 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1155 caddr_t bpos, dpos, cp2;
1156 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1157 struct nfsmount *nmp;
1158 int error = 0, len, retlen, tsiz, eof, attrflag;
1159 int v3 = NFS_ISV3(vp);
1164 nmp = VFSTONFS(vp->v_mount);
1165 tsiz = uiop->uio_resid;
1166 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1169 nfsstats.rpccnt[NFSPROC_READ]++;
1170 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1171 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1173 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1175 txdr_hyper(uiop->uio_offset, tl);
1176 *(tl + 2) = txdr_unsigned(len);
1178 *tl++ = txdr_unsigned(uiop->uio_offset);
1179 *tl++ = txdr_unsigned(len);
1182 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1184 nfsm_postop_attr(vp, attrflag);
1189 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1190 eof = fxdr_unsigned(int, *(tl + 1));
1192 nfsm_loadattr(vp, (struct vattr *)0);
1193 nfsm_strsiz(retlen, nmp->nm_rsize);
1194 nfsm_mtouio(uiop, retlen);
1198 if (eof || retlen == 0) {
1201 } else if (retlen < len) {
1213 nfs_writerpc(vp, uiop, iomode, must_commit)
1216 int *iomode, *must_commit;
1220 int32_t t1, t2, backup;
1221 caddr_t bpos, dpos, cp2;
1222 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1223 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1224 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1225 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1228 if (uiop->uio_iovcnt != 1)
1229 panic("nfs: writerpc iovcnt > 1");
1232 tsiz = uiop->uio_resid;
1233 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1236 nfsstats.rpccnt[NFSPROC_WRITE]++;
1237 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1238 nfsm_reqhead(vp, NFSPROC_WRITE,
1239 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1242 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1243 txdr_hyper(uiop->uio_offset, tl);
1245 *tl++ = txdr_unsigned(len);
1246 *tl++ = txdr_unsigned(*iomode);
1247 *tl = txdr_unsigned(len);
1251 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1252 /* Set both "begin" and "current" to non-garbage. */
1253 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1254 *tl++ = x; /* "begin offset" */
1255 *tl++ = x; /* "current offset" */
1256 x = txdr_unsigned(len);
1257 *tl++ = x; /* total to this offset */
1258 *tl = x; /* size of this write */
1260 nfsm_uiotom(uiop, len);
1261 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1263 wccflag = NFSV3_WCCCHK;
1264 nfsm_wcc_data(vp, wccflag);
1266 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1267 + NFSX_V3WRITEVERF);
1268 rlen = fxdr_unsigned(int, *tl++);
1273 } else if (rlen < len) {
1274 backup = len - rlen;
1275 uiop->uio_iov->iov_base -= backup;
1276 uiop->uio_iov->iov_len += backup;
1277 uiop->uio_offset -= backup;
1278 uiop->uio_resid += backup;
1281 commit = fxdr_unsigned(int, *tl++);
1284 * Return the lowest committment level
1285 * obtained by any of the RPCs.
1287 if (committed == NFSV3WRITE_FILESYNC)
1289 else if (committed == NFSV3WRITE_DATASYNC &&
1290 commit == NFSV3WRITE_UNSTABLE)
1292 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1293 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1295 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1296 } else if (bcmp((caddr_t)tl,
1297 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1299 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1304 nfsm_loadattr(vp, (struct vattr *)0);
1306 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime.tv_sec;
1313 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1314 committed = NFSV3WRITE_FILESYNC;
1315 *iomode = committed;
1317 uiop->uio_resid = tsiz;
1323 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1324 * mode set to specify the file type and the size field for rdev.
1327 nfs_mknodrpc(dvp, vpp, cnp, vap)
1330 struct componentname *cnp;
1333 struct nfsv2_sattr *sp;
1337 struct vnode *newvp = (struct vnode *)0;
1338 struct nfsnode *np = (struct nfsnode *)0;
1342 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1343 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1345 int v3 = NFS_ISV3(dvp);
1347 if (vap->va_type == VCHR || vap->va_type == VBLK)
1348 rdev = txdr_unsigned(vap->va_rdev);
1349 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1352 return (EOPNOTSUPP);
1354 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1357 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1358 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1359 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1360 nfsm_fhtom(dvp, v3);
1361 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1363 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1364 *tl++ = vtonfsv3_type(vap->va_type);
1365 nfsm_v3attrbuild(vap, FALSE);
1366 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1367 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1368 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1369 *tl = txdr_unsigned(uminor(vap->va_rdev));
1372 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1373 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1374 sp->sa_uid = nfs_xdrneg1;
1375 sp->sa_gid = nfs_xdrneg1;
1377 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1378 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1380 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1382 nfsm_mtofh(dvp, newvp, v3, gotvp);
1386 newvp = (struct vnode *)0;
1388 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1389 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1395 nfsm_wcc_data(dvp, wccflag);
1402 if (cnp->cn_flags & CNP_MAKEENTRY)
1403 cache_enter(dvp, NCPNULL, newvp, cnp);
1406 VTONFS(dvp)->n_flag |= NMODIFIED;
1408 VTONFS(dvp)->n_attrstamp = 0;
1414 * just call nfs_mknodrpc() to do the work.
1419 struct vop_mknod_args /* {
1420 struct vnode *a_dvp;
1421 struct vnode **a_vpp;
1422 struct componentname *a_cnp;
1423 struct vattr *a_vap;
1426 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1429 static u_long create_verf;
1431 * nfs file create call
1435 struct vop_create_args /* {
1436 struct vnode *a_dvp;
1437 struct vnode **a_vpp;
1438 struct componentname *a_cnp;
1439 struct vattr *a_vap;
1442 struct vnode *dvp = ap->a_dvp;
1443 struct vattr *vap = ap->a_vap;
1444 struct componentname *cnp = ap->a_cnp;
1445 struct nfsv2_sattr *sp;
1449 struct nfsnode *np = (struct nfsnode *)0;
1450 struct vnode *newvp = (struct vnode *)0;
1451 caddr_t bpos, dpos, cp2;
1452 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1453 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1455 int v3 = NFS_ISV3(dvp);
1458 * Oops, not for me..
1460 if (vap->va_type == VSOCK)
1461 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1463 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1466 if (vap->va_vaflags & VA_EXCLUSIVE)
1469 nfsstats.rpccnt[NFSPROC_CREATE]++;
1470 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1471 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1472 nfsm_fhtom(dvp, v3);
1473 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1475 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1476 if (fmode & O_EXCL) {
1477 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1478 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1480 if (!TAILQ_EMPTY(&in_ifaddrhead))
1481 *tl++ = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr.s_addr;
1484 *tl++ = create_verf;
1485 *tl = ++create_verf;
1487 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1488 nfsm_v3attrbuild(vap, FALSE);
1491 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1492 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1493 sp->sa_uid = nfs_xdrneg1;
1494 sp->sa_gid = nfs_xdrneg1;
1496 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1497 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1499 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1501 nfsm_mtofh(dvp, newvp, v3, gotvp);
1505 newvp = (struct vnode *)0;
1507 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1508 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1514 nfsm_wcc_data(dvp, wccflag);
1518 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1524 } else if (v3 && (fmode & O_EXCL)) {
1526 * We are normally called with only a partially initialized
1527 * VAP. Since the NFSv3 spec says that server may use the
1528 * file attributes to store the verifier, the spec requires
1529 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1530 * in atime, but we can't really assume that all servers will
1531 * so we ensure that our SETATTR sets both atime and mtime.
1533 if (vap->va_mtime.tv_sec == VNOVAL)
1534 vfs_timestamp(&vap->va_mtime);
1535 if (vap->va_atime.tv_sec == VNOVAL)
1536 vap->va_atime = vap->va_mtime;
1537 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1540 if (cnp->cn_flags & CNP_MAKEENTRY)
1541 cache_enter(dvp, NCPNULL, newvp, cnp);
1543 * The new np may have enough info for access
1544 * checks, make sure rucred and wucred are
1545 * initialized for read and write rpc's.
1548 if (np->n_rucred == NULL)
1549 np->n_rucred = crhold(cnp->cn_cred);
1550 if (np->n_wucred == NULL)
1551 np->n_wucred = crhold(cnp->cn_cred);
1554 VTONFS(dvp)->n_flag |= NMODIFIED;
1556 VTONFS(dvp)->n_attrstamp = 0;
1561 * nfs file remove call
1562 * To try and make nfs semantics closer to ufs semantics, a file that has
1563 * other processes using the vnode is renamed instead of removed and then
1564 * removed later on the last close.
1565 * - If v_usecount > 1
1566 * If a rename is not already in the works
1567 * call nfs_sillyrename() to set it up
1573 struct vop_remove_args /* {
1574 struct vnodeop_desc *a_desc;
1575 struct vnode * a_dvp;
1576 struct vnode * a_vp;
1577 struct componentname * a_cnp;
1580 struct vnode *vp = ap->a_vp;
1581 struct vnode *dvp = ap->a_dvp;
1582 struct componentname *cnp = ap->a_cnp;
1583 struct nfsnode *np = VTONFS(vp);
1588 if ((cnp->cn_flags & CNP_HASBUF) == 0)
1589 panic("nfs_remove: no name");
1590 if (vp->v_usecount < 1)
1591 panic("nfs_remove: bad v_usecount");
1593 if (vp->v_type == VDIR)
1595 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1596 VOP_GETATTR(vp, &vattr, cnp->cn_td) == 0 &&
1597 vattr.va_nlink > 1)) {
1599 * Purge the name cache so that the chance of a lookup for
1600 * the name succeeding while the remove is in progress is
1601 * minimized. Without node locking it can still happen, such
1602 * that an I/O op returns ESTALE, but since you get this if
1603 * another host removes the file..
1607 * throw away biocache buffers, mainly to avoid
1608 * unnecessary delayed writes later.
1610 error = nfs_vinvalbuf(vp, 0, cnp->cn_td, 1);
1613 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1614 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1616 * Kludge City: If the first reply to the remove rpc is lost..
1617 * the reply to the retransmitted request will be ENOENT
1618 * since the file was in fact removed
1619 * Therefore, we cheat and return success.
1621 if (error == ENOENT)
1623 } else if (!np->n_sillyrename)
1624 error = nfs_sillyrename(dvp, vp, cnp);
1625 np->n_attrstamp = 0;
1630 * nfs file remove rpc called from nfs_inactive
1633 nfs_removeit(struct sillyrename *sp)
1636 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1641 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1644 nfs_removerpc(dvp, name, namelen, cred, td)
1654 caddr_t bpos, dpos, cp2;
1655 int error = 0, wccflag = NFSV3_WCCRATTR;
1656 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1657 int v3 = NFS_ISV3(dvp);
1659 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1660 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1661 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1662 nfsm_fhtom(dvp, v3);
1663 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1664 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1666 nfsm_wcc_data(dvp, wccflag);
1669 VTONFS(dvp)->n_flag |= NMODIFIED;
1671 VTONFS(dvp)->n_attrstamp = 0;
1676 * nfs file rename call
1680 struct vop_rename_args /* {
1681 struct vnode *a_fdvp;
1682 struct vnode *a_fvp;
1683 struct componentname *a_fcnp;
1684 struct vnode *a_tdvp;
1685 struct vnode *a_tvp;
1686 struct componentname *a_tcnp;
1689 struct vnode *fvp = ap->a_fvp;
1690 struct vnode *tvp = ap->a_tvp;
1691 struct vnode *fdvp = ap->a_fdvp;
1692 struct vnode *tdvp = ap->a_tdvp;
1693 struct componentname *tcnp = ap->a_tcnp;
1694 struct componentname *fcnp = ap->a_fcnp;
1698 if ((tcnp->cn_flags & CNP_HASBUF) == 0 ||
1699 (fcnp->cn_flags & CNP_HASBUF) == 0)
1700 panic("nfs_rename: no name");
1702 /* Check for cross-device rename */
1703 if ((fvp->v_mount != tdvp->v_mount) ||
1704 (tvp && (fvp->v_mount != tvp->v_mount))) {
1710 * We have to flush B_DELWRI data prior to renaming
1711 * the file. If we don't, the delayed-write buffers
1712 * can be flushed out later after the file has gone stale
1713 * under NFSV3. NFSV2 does not have this problem because
1714 * ( as far as I can tell ) it flushes dirty buffers more
1718 VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_td);
1720 VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_td);
1723 * If the tvp exists and is in use, sillyrename it before doing the
1724 * rename of the new file over it.
1725 * XXX Can't sillyrename a directory.
1727 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1728 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1733 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1734 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1737 if (fvp->v_type == VDIR) {
1738 if (tvp != NULL && tvp->v_type == VDIR)
1753 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1755 if (error == ENOENT)
1761 * nfs file rename rpc called from nfs_remove() above
1764 nfs_renameit(sdvp, scnp, sp)
1766 struct componentname *scnp;
1767 struct sillyrename *sp;
1769 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1770 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1774 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1777 nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, td)
1779 const char *fnameptr;
1782 const char *tnameptr;
1790 caddr_t bpos, dpos, cp2;
1791 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1792 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1793 int v3 = NFS_ISV3(fdvp);
1795 nfsstats.rpccnt[NFSPROC_RENAME]++;
1796 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1797 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1798 nfsm_rndup(tnamelen));
1799 nfsm_fhtom(fdvp, v3);
1800 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1801 nfsm_fhtom(tdvp, v3);
1802 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1803 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1805 nfsm_wcc_data(fdvp, fwccflag);
1806 nfsm_wcc_data(tdvp, twccflag);
1810 VTONFS(fdvp)->n_flag |= NMODIFIED;
1811 VTONFS(tdvp)->n_flag |= NMODIFIED;
1813 VTONFS(fdvp)->n_attrstamp = 0;
1815 VTONFS(tdvp)->n_attrstamp = 0;
1820 * nfs hard link create call
1824 struct vop_link_args /* {
1825 struct vnode *a_tdvp;
1827 struct componentname *a_cnp;
1830 struct vnode *vp = ap->a_vp;
1831 struct vnode *tdvp = ap->a_tdvp;
1832 struct componentname *cnp = ap->a_cnp;
1836 caddr_t bpos, dpos, cp2;
1837 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1838 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1841 if (vp->v_mount != tdvp->v_mount) {
1846 * Push all writes to the server, so that the attribute cache
1847 * doesn't get "out of sync" with the server.
1848 * XXX There should be a better way!
1850 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_td);
1853 nfsstats.rpccnt[NFSPROC_LINK]++;
1854 nfsm_reqhead(vp, NFSPROC_LINK,
1855 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1857 nfsm_fhtom(tdvp, v3);
1858 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1859 nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1861 nfsm_postop_attr(vp, attrflag);
1862 nfsm_wcc_data(tdvp, wccflag);
1866 VTONFS(tdvp)->n_flag |= NMODIFIED;
1868 VTONFS(vp)->n_attrstamp = 0;
1870 VTONFS(tdvp)->n_attrstamp = 0;
1872 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1874 if (error == EEXIST)
1880 * nfs symbolic link create call
1884 struct vop_symlink_args /* {
1885 struct vnode *a_dvp;
1886 struct vnode **a_vpp;
1887 struct componentname *a_cnp;
1888 struct vattr *a_vap;
1892 struct vnode *dvp = ap->a_dvp;
1893 struct vattr *vap = ap->a_vap;
1894 struct componentname *cnp = ap->a_cnp;
1895 struct nfsv2_sattr *sp;
1899 caddr_t bpos, dpos, cp2;
1900 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1901 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1902 struct vnode *newvp = (struct vnode *)0;
1903 int v3 = NFS_ISV3(dvp);
1905 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1906 slen = strlen(ap->a_target);
1907 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1908 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1909 nfsm_fhtom(dvp, v3);
1910 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1912 nfsm_v3attrbuild(vap, FALSE);
1914 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1916 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1917 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1918 sp->sa_uid = nfs_xdrneg1;
1919 sp->sa_gid = nfs_xdrneg1;
1920 sp->sa_size = nfs_xdrneg1;
1921 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1922 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1926 * Issue the NFS request and get the rpc response.
1928 * Only NFSv3 responses returning an error of 0 actually return
1929 * a file handle that can be converted into newvp without having
1930 * to do an extra lookup rpc.
1932 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1935 nfsm_mtofh(dvp, newvp, v3, gotvp);
1936 nfsm_wcc_data(dvp, wccflag);
1940 * out code jumps -> here, mrep is also freed.
1947 * If we get an EEXIST error, silently convert it to no-error
1948 * in case of an NFS retry.
1950 if (error == EEXIST)
1954 * If we do not have (or no longer have) an error, and we could
1955 * not extract the newvp from the response due to the request being
1956 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1957 * to obtain a newvp to return.
1959 if (error == 0 && newvp == NULL) {
1960 struct nfsnode *np = NULL;
1962 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1963 cnp->cn_cred, cnp->cn_td, &np);
1973 VTONFS(dvp)->n_flag |= NMODIFIED;
1975 VTONFS(dvp)->n_attrstamp = 0;
1984 struct vop_mkdir_args /* {
1985 struct vnode *a_dvp;
1986 struct vnode **a_vpp;
1987 struct componentname *a_cnp;
1988 struct vattr *a_vap;
1991 struct vnode *dvp = ap->a_dvp;
1992 struct vattr *vap = ap->a_vap;
1993 struct componentname *cnp = ap->a_cnp;
1994 struct nfsv2_sattr *sp;
1999 struct nfsnode *np = (struct nfsnode *)0;
2000 struct vnode *newvp = (struct vnode *)0;
2001 caddr_t bpos, dpos, cp2;
2002 int error = 0, wccflag = NFSV3_WCCRATTR;
2004 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2006 int v3 = NFS_ISV3(dvp);
2008 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
2011 len = cnp->cn_namelen;
2012 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2013 nfsm_reqhead(dvp, NFSPROC_MKDIR,
2014 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2015 nfsm_fhtom(dvp, v3);
2016 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
2018 nfsm_v3attrbuild(vap, FALSE);
2020 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2021 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2022 sp->sa_uid = nfs_xdrneg1;
2023 sp->sa_gid = nfs_xdrneg1;
2024 sp->sa_size = nfs_xdrneg1;
2025 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2026 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2028 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
2030 nfsm_mtofh(dvp, newvp, v3, gotvp);
2032 nfsm_wcc_data(dvp, wccflag);
2035 VTONFS(dvp)->n_flag |= NMODIFIED;
2037 VTONFS(dvp)->n_attrstamp = 0;
2039 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2040 * if we can succeed in looking up the directory.
2042 if (error == EEXIST || (!error && !gotvp)) {
2045 newvp = (struct vnode *)0;
2047 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2051 if (newvp->v_type != VDIR)
2064 * nfs remove directory call
2068 struct vop_rmdir_args /* {
2069 struct vnode *a_dvp;
2071 struct componentname *a_cnp;
2074 struct vnode *vp = ap->a_vp;
2075 struct vnode *dvp = ap->a_dvp;
2076 struct componentname *cnp = ap->a_cnp;
2080 caddr_t bpos, dpos, cp2;
2081 int error = 0, wccflag = NFSV3_WCCRATTR;
2082 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2083 int v3 = NFS_ISV3(dvp);
2087 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2088 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2089 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2090 nfsm_fhtom(dvp, v3);
2091 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2092 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2094 nfsm_wcc_data(dvp, wccflag);
2097 VTONFS(dvp)->n_flag |= NMODIFIED;
2099 VTONFS(dvp)->n_attrstamp = 0;
2103 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2105 if (error == ENOENT)
2115 struct vop_readdir_args /* {
2118 struct ucred *a_cred;
2121 struct vnode *vp = ap->a_vp;
2122 struct nfsnode *np = VTONFS(vp);
2123 struct uio *uio = ap->a_uio;
2127 if (vp->v_type != VDIR)
2130 * First, check for hit on the EOF offset cache
2132 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2133 (np->n_flag & NMODIFIED) == 0) {
2134 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
2135 if (NQNFS_CKCACHABLE(vp, ND_READ)) {
2136 nfsstats.direofcache_hits++;
2139 } else if (VOP_GETATTR(vp, &vattr, uio->uio_td) == 0 &&
2140 np->n_mtime == vattr.va_mtime.tv_sec) {
2141 nfsstats.direofcache_hits++;
2147 * Call nfs_bioread() to do the real work.
2149 tresid = uio->uio_resid;
2150 error = nfs_bioread(vp, uio, 0);
2152 if (!error && uio->uio_resid == tresid)
2153 nfsstats.direofcache_misses++;
2159 * Called from below the buffer cache by nfs_doio().
2162 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2165 struct dirent *dp = NULL;
2170 caddr_t bpos, dpos, cp2;
2171 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2173 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2174 struct nfsnode *dnp = VTONFS(vp);
2176 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2178 int v3 = NFS_ISV3(vp);
2181 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2182 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2183 panic("nfs readdirrpc bad uio");
2187 * If there is no cookie, assume directory was stale.
2189 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2193 return (NFSERR_BAD_COOKIE);
2195 * Loop around doing readdir rpc's of size nm_readdirsize
2196 * truncated to a multiple of DIRBLKSIZ.
2197 * The stopping criteria is EOF or buffer full.
2199 while (more_dirs && bigenough) {
2200 nfsstats.rpccnt[NFSPROC_READDIR]++;
2201 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2205 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2206 *tl++ = cookie.nfsuquad[0];
2207 *tl++ = cookie.nfsuquad[1];
2208 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2209 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2211 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2212 *tl++ = cookie.nfsuquad[0];
2214 *tl = txdr_unsigned(nmp->nm_readdirsize);
2215 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2217 nfsm_postop_attr(vp, attrflag);
2219 nfsm_dissect(tl, u_int32_t *,
2221 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2222 dnp->n_cookieverf.nfsuquad[1] = *tl;
2228 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2229 more_dirs = fxdr_unsigned(int, *tl);
2231 /* loop thru the dir entries, doctoring them to 4bsd form */
2232 while (more_dirs && bigenough) {
2234 nfsm_dissect(tl, u_int32_t *,
2236 fileno = fxdr_hyper(tl);
2237 len = fxdr_unsigned(int, *(tl + 2));
2239 nfsm_dissect(tl, u_int32_t *,
2241 fileno = fxdr_unsigned(u_quad_t, *tl++);
2242 len = fxdr_unsigned(int, *tl);
2244 if (len <= 0 || len > NFS_MAXNAMLEN) {
2249 tlen = nfsm_rndup(len);
2251 tlen += 4; /* To ensure null termination */
2252 left = DIRBLKSIZ - blksiz;
2253 if ((tlen + DIRHDSIZ) > left) {
2254 dp->d_reclen += left;
2255 uiop->uio_iov->iov_base += left;
2256 uiop->uio_iov->iov_len -= left;
2257 uiop->uio_offset += left;
2258 uiop->uio_resid -= left;
2261 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2264 dp = (struct dirent *)uiop->uio_iov->iov_base;
2265 dp->d_fileno = (int)fileno;
2267 dp->d_reclen = tlen + DIRHDSIZ;
2268 dp->d_type = DT_UNKNOWN;
2269 blksiz += dp->d_reclen;
2270 if (blksiz == DIRBLKSIZ)
2272 uiop->uio_offset += DIRHDSIZ;
2273 uiop->uio_resid -= DIRHDSIZ;
2274 uiop->uio_iov->iov_base += DIRHDSIZ;
2275 uiop->uio_iov->iov_len -= DIRHDSIZ;
2276 nfsm_mtouio(uiop, len);
2277 cp = uiop->uio_iov->iov_base;
2279 *cp = '\0'; /* null terminate */
2280 uiop->uio_iov->iov_base += tlen;
2281 uiop->uio_iov->iov_len -= tlen;
2282 uiop->uio_offset += tlen;
2283 uiop->uio_resid -= tlen;
2285 nfsm_adv(nfsm_rndup(len));
2287 nfsm_dissect(tl, u_int32_t *,
2290 nfsm_dissect(tl, u_int32_t *,
2294 cookie.nfsuquad[0] = *tl++;
2296 cookie.nfsuquad[1] = *tl++;
2301 more_dirs = fxdr_unsigned(int, *tl);
2304 * If at end of rpc data, get the eof boolean
2307 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2308 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2313 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2314 * by increasing d_reclen for the last record.
2317 left = DIRBLKSIZ - blksiz;
2318 dp->d_reclen += left;
2319 uiop->uio_iov->iov_base += left;
2320 uiop->uio_iov->iov_len -= left;
2321 uiop->uio_offset += left;
2322 uiop->uio_resid -= left;
2326 * We are now either at the end of the directory or have filled the
2330 dnp->n_direofoffset = uiop->uio_offset;
2332 if (uiop->uio_resid > 0)
2333 printf("EEK! readdirrpc resid > 0\n");
2334 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2342 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2345 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2352 struct vnode *newvp;
2354 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2355 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2356 struct nameidata nami, *ndp = &nami;
2357 struct componentname *cnp = &ndp->ni_cnd;
2359 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2360 struct nfsnode *dnp = VTONFS(vp), *np;
2363 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2364 int attrflag, fhsize;
2367 dp = (struct dirent *)0;
2370 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2371 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2372 panic("nfs readdirplusrpc bad uio");
2378 * If there is no cookie, assume directory was stale.
2380 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2384 return (NFSERR_BAD_COOKIE);
2386 * Loop around doing readdir rpc's of size nm_readdirsize
2387 * truncated to a multiple of DIRBLKSIZ.
2388 * The stopping criteria is EOF or buffer full.
2390 while (more_dirs && bigenough) {
2391 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2392 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2393 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2395 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2396 *tl++ = cookie.nfsuquad[0];
2397 *tl++ = cookie.nfsuquad[1];
2398 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2399 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2400 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2401 *tl = txdr_unsigned(nmp->nm_rsize);
2402 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2403 nfsm_postop_attr(vp, attrflag);
2408 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2409 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2410 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2411 more_dirs = fxdr_unsigned(int, *tl);
2413 /* loop thru the dir entries, doctoring them to 4bsd form */
2414 while (more_dirs && bigenough) {
2415 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2416 fileno = fxdr_hyper(tl);
2417 len = fxdr_unsigned(int, *(tl + 2));
2418 if (len <= 0 || len > NFS_MAXNAMLEN) {
2423 tlen = nfsm_rndup(len);
2425 tlen += 4; /* To ensure null termination*/
2426 left = DIRBLKSIZ - blksiz;
2427 if ((tlen + DIRHDSIZ) > left) {
2428 dp->d_reclen += left;
2429 uiop->uio_iov->iov_base += left;
2430 uiop->uio_iov->iov_len -= left;
2431 uiop->uio_offset += left;
2432 uiop->uio_resid -= left;
2435 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2438 dp = (struct dirent *)uiop->uio_iov->iov_base;
2439 dp->d_fileno = (int)fileno;
2441 dp->d_reclen = tlen + DIRHDSIZ;
2442 dp->d_type = DT_UNKNOWN;
2443 blksiz += dp->d_reclen;
2444 if (blksiz == DIRBLKSIZ)
2446 uiop->uio_offset += DIRHDSIZ;
2447 uiop->uio_resid -= DIRHDSIZ;
2448 uiop->uio_iov->iov_base += DIRHDSIZ;
2449 uiop->uio_iov->iov_len -= DIRHDSIZ;
2450 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2451 cnp->cn_namelen = len;
2452 nfsm_mtouio(uiop, len);
2453 cp = uiop->uio_iov->iov_base;
2456 uiop->uio_iov->iov_base += tlen;
2457 uiop->uio_iov->iov_len -= tlen;
2458 uiop->uio_offset += tlen;
2459 uiop->uio_resid -= tlen;
2461 nfsm_adv(nfsm_rndup(len));
2462 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2464 cookie.nfsuquad[0] = *tl++;
2465 cookie.nfsuquad[1] = *tl++;
2470 * Since the attributes are before the file handle
2471 * (sigh), we must skip over the attributes and then
2472 * come back and get them.
2474 attrflag = fxdr_unsigned(int, *tl);
2478 nfsm_adv(NFSX_V3FATTR);
2479 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2480 doit = fxdr_unsigned(int, *tl);
2482 nfsm_getfh(fhp, fhsize, 1);
2483 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2488 error = nfs_nget(vp->v_mount, fhp,
2496 if (doit && bigenough) {
2501 nfsm_loadattr(newvp, (struct vattr *)0);
2505 IFTODT(VTTOIF(np->n_vattr.va_type));
2507 cache_enter(ndp->ni_dvp, NCPNULL, ndp->ni_vp, cnp);
2510 /* Just skip over the file handle */
2511 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2512 i = fxdr_unsigned(int, *tl);
2513 nfsm_adv(nfsm_rndup(i));
2515 if (newvp != NULLVP) {
2522 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2523 more_dirs = fxdr_unsigned(int, *tl);
2526 * If at end of rpc data, get the eof boolean
2529 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2530 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2535 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2536 * by increasing d_reclen for the last record.
2539 left = DIRBLKSIZ - blksiz;
2540 dp->d_reclen += left;
2541 uiop->uio_iov->iov_base += left;
2542 uiop->uio_iov->iov_len -= left;
2543 uiop->uio_offset += left;
2544 uiop->uio_resid -= left;
2548 * We are now either at the end of the directory or have filled the
2552 dnp->n_direofoffset = uiop->uio_offset;
2554 if (uiop->uio_resid > 0)
2555 printf("EEK! readdirplusrpc resid > 0\n");
2556 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2560 if (newvp != NULLVP) {
2571 * Silly rename. To make the NFS filesystem that is stateless look a little
2572 * more like the "ufs" a remove of an active vnode is translated to a rename
2573 * to a funny looking filename that is removed by nfs_inactive on the
2574 * nfsnode. There is the potential for another process on a different client
2575 * to create the same funny name between the nfs_lookitup() fails and the
2576 * nfs_rename() completes, but...
2579 nfs_sillyrename(dvp, vp, cnp)
2580 struct vnode *dvp, *vp;
2581 struct componentname *cnp;
2583 struct sillyrename *sp;
2590 if (vp->v_type == VDIR)
2591 panic("nfs: sillyrename dir");
2593 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2594 M_NFSREQ, M_WAITOK);
2595 sp->s_cred = crdup(cnp->cn_cred);
2599 /* Fudge together a funny name */
2600 sp->s_namlen = sprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
2602 /* Try lookitups until we get one that isn't there */
2603 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2604 cnp->cn_td, (struct nfsnode **)0) == 0) {
2606 if (sp->s_name[4] > 'z') {
2611 error = nfs_renameit(dvp, cnp, sp);
2614 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2616 np->n_sillyrename = sp;
2621 free((caddr_t)sp, M_NFSREQ);
2626 * Look up a file name and optionally either update the file handle or
2627 * allocate an nfsnode, depending on the value of npp.
2628 * npp == NULL --> just do the lookup
2629 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2631 * *npp != NULL --> update the file handle in the vnode
2634 nfs_lookitup(dvp, name, len, cred, td, npp)
2640 struct nfsnode **npp;
2645 struct vnode *newvp = (struct vnode *)0;
2646 struct nfsnode *np, *dnp = VTONFS(dvp);
2647 caddr_t bpos, dpos, cp2;
2648 int error = 0, fhlen, attrflag;
2649 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2651 int v3 = NFS_ISV3(dvp);
2653 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2654 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2655 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2656 nfsm_fhtom(dvp, v3);
2657 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2658 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2659 if (npp && !error) {
2660 nfsm_getfh(nfhp, fhlen, v3);
2663 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2664 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2665 np->n_fhp = &np->n_fh;
2666 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2667 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2668 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2669 np->n_fhsize = fhlen;
2671 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2675 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2683 nfsm_postop_attr(newvp, attrflag);
2684 if (!attrflag && *npp == NULL) {
2693 nfsm_loadattr(newvp, (struct vattr *)0);
2697 if (npp && *npp == NULL) {
2712 * Nfs Version 3 commit rpc
2715 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2720 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2721 caddr_t bpos, dpos, cp2;
2722 int error = 0, wccflag = NFSV3_WCCRATTR;
2723 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2725 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2727 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2728 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2730 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2731 txdr_hyper(offset, tl);
2733 *tl = txdr_unsigned(cnt);
2734 nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2735 nfsm_wcc_data(vp, wccflag);
2737 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2738 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2739 NFSX_V3WRITEVERF)) {
2740 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2742 error = NFSERR_STALEWRITEVERF;
2752 * - make nfs_bmap() essentially a no-op that does no translation
2753 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2754 * (Maybe I could use the process's page mapping, but I was concerned that
2755 * Kernel Write might not be enabled and also figured copyout() would do
2756 * a lot more work than bcopy() and also it currently happens in the
2757 * context of the swapper process (2).
2761 struct vop_bmap_args /* {
2764 struct vnode **a_vpp;
2770 struct vnode *vp = ap->a_vp;
2772 if (ap->a_vpp != NULL)
2774 if (ap->a_bnp != NULL)
2775 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2776 if (ap->a_runp != NULL)
2778 if (ap->a_runb != NULL)
2785 * For async requests when nfsiod(s) are running, queue the request by
2786 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2791 struct vop_strategy_args *ap;
2793 struct buf *bp = ap->a_bp;
2797 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2798 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2800 if (bp->b_flags & B_PHYS)
2801 panic("nfs physio");
2803 if (bp->b_flags & B_ASYNC)
2806 td = curthread; /* XXX */
2809 * If the op is asynchronous and an i/o daemon is waiting
2810 * queue the request, wake it up and wait for completion
2811 * otherwise just do it ourselves.
2813 if ((bp->b_flags & B_ASYNC) == 0 ||
2814 nfs_asyncio(bp, td))
2815 error = nfs_doio(bp, td);
2822 * NB Currently unsupported.
2827 struct vop_mmap_args /* {
2830 struct ucred *a_cred;
2831 struct thread *a_td;
2839 * fsync vnode op. Just call nfs_flush() with commit == 1.
2844 struct vop_fsync_args /* {
2845 struct vnodeop_desc *a_desc;
2846 struct vnode * a_vp;
2847 struct ucred * a_cred;
2849 struct thread * a_td;
2853 return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2857 * Flush all the blocks associated with a vnode.
2858 * Walk through the buffer pool and push any dirty pages
2859 * associated with the vnode.
2862 nfs_flush(vp, waitfor, td, commit)
2868 struct nfsnode *np = VTONFS(vp);
2872 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2873 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2875 u_quad_t off, endoff, toff;
2876 struct buf **bvec = NULL;
2877 #ifndef NFS_COMMITBVECSIZ
2878 #define NFS_COMMITBVECSIZ 20
2880 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2881 int bvecsize = 0, bveccount;
2883 if (nmp->nm_flag & NFSMNT_INT)
2888 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2889 * server, but nas not been committed to stable storage on the server
2890 * yet. On the first pass, the byte range is worked out and the commit
2891 * rpc is done. On the second pass, nfs_writebp() is called to do the
2898 if (NFS_ISV3(vp) && commit) {
2901 * Count up how many buffers waiting for a commit.
2904 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2905 nbp = TAILQ_NEXT(bp, b_vnbufs);
2906 if (BUF_REFCNT(bp) == 0 &&
2907 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2908 == (B_DELWRI | B_NEEDCOMMIT))
2912 * Allocate space to remember the list of bufs to commit. It is
2913 * important to use M_NOWAIT here to avoid a race with nfs_write.
2914 * If we can't get memory (for whatever reason), we will end up
2915 * committing the buffers one-by-one in the loop below.
2917 if (bvec != NULL && bvec != bvec_on_stack)
2919 if (bveccount > NFS_COMMITBVECSIZ) {
2920 bvec = (struct buf **)
2921 malloc(bveccount * sizeof(struct buf *),
2924 bvec = bvec_on_stack;
2925 bvecsize = NFS_COMMITBVECSIZ;
2927 bvecsize = bveccount;
2929 bvec = bvec_on_stack;
2930 bvecsize = NFS_COMMITBVECSIZ;
2932 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2933 nbp = TAILQ_NEXT(bp, b_vnbufs);
2934 if (bvecpos >= bvecsize)
2936 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2937 (B_DELWRI | B_NEEDCOMMIT) ||
2938 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
2942 * NOTE: we are not clearing B_DONE here, so we have
2943 * to do it later on in this routine if we intend to
2944 * initiate I/O on the bp.
2946 * Note: to avoid loopback deadlocks, we do not
2947 * assign b_runningbufspace.
2949 bp->b_flags |= B_WRITEINPROG;
2950 vfs_busy_pages(bp, 1);
2953 * bp is protected by being locked, but nbp is not
2954 * and vfs_busy_pages() may sleep. We have to
2957 nbp = TAILQ_NEXT(bp, b_vnbufs);
2960 * A list of these buffers is kept so that the
2961 * second loop knows which buffers have actually
2962 * been committed. This is necessary, since there
2963 * may be a race between the commit rpc and new
2964 * uncommitted writes on the file.
2966 bvec[bvecpos++] = bp;
2967 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2971 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2979 * Commit data on the server, as required. Note that
2980 * nfs_commit will use the vnode's cred for the commit.
2982 retv = nfs_commit(vp, off, (int)(endoff - off), td);
2984 if (retv == NFSERR_STALEWRITEVERF)
2985 nfs_clearcommit(vp->v_mount);
2988 * Now, either mark the blocks I/O done or mark the
2989 * blocks dirty, depending on whether the commit
2992 for (i = 0; i < bvecpos; i++) {
2994 bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG | B_CLUSTEROK);
2997 * Error, leave B_DELWRI intact
2999 vfs_unbusy_pages(bp);
3003 * Success, remove B_DELWRI ( bundirty() ).
3005 * b_dirtyoff/b_dirtyend seem to be NFS
3006 * specific. We should probably move that
3007 * into bundirty(). XXX
3011 bp->b_flags |= B_ASYNC;
3013 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3014 bp->b_dirtyoff = bp->b_dirtyend = 0;
3022 * Start/do any write(s) that are required.
3026 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
3027 nbp = TAILQ_NEXT(bp, b_vnbufs);
3028 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
3029 if (waitfor != MNT_WAIT || passone)
3031 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL,
3032 "nfsfsync", slpflag, slptimeo);
3035 panic("nfs_fsync: inconsistent lock");
3036 if (error == ENOLCK)
3038 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
3042 if (slpflag == PCATCH) {
3048 if ((bp->b_flags & B_DELWRI) == 0)
3049 panic("nfs_fsync: not dirty");
3050 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3055 if (passone || !commit)
3056 bp->b_flags |= B_ASYNC;
3058 bp->b_flags |= B_ASYNC | B_WRITEINPROG;
3060 VOP_BWRITE(bp->b_vp, bp);
3068 if (waitfor == MNT_WAIT) {
3069 while (vp->v_numoutput) {
3070 vp->v_flag |= VBWAIT;
3071 error = tsleep((caddr_t)&vp->v_numoutput,
3072 slpflag, "nfsfsync", slptimeo);
3074 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
3078 if (slpflag == PCATCH) {
3084 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) && commit) {
3088 if (np->n_flag & NWRITEERR) {
3089 error = np->n_error;
3090 np->n_flag &= ~NWRITEERR;
3093 if (bvec != NULL && bvec != bvec_on_stack)
3099 * NFS advisory byte-level locks.
3100 * Currently unsupported.
3104 struct vop_advlock_args /* {
3112 struct nfsnode *np = VTONFS(ap->a_vp);
3115 * The following kludge is to allow diskless support to work
3116 * until a real NFS lockd is implemented. Basically, just pretend
3117 * that this is a local lock.
3119 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3123 * Print out the contents of an nfsnode.
3127 struct vop_print_args /* {
3131 struct vnode *vp = ap->a_vp;
3132 struct nfsnode *np = VTONFS(vp);
3134 printf("tag VT_NFS, fileid %ld fsid 0x%x",
3135 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3136 if (vp->v_type == VFIFO)
3143 * Just call nfs_writebp() with the force argument set to 1.
3145 * NOTE: B_DONE may or may not be set in a_bp on call.
3149 struct vop_bwrite_args /* {
3153 return (nfs_writebp(ap->a_bp, 1, curthread));
3157 * This is a clone of vn_bwrite(), except that B_WRITEINPROG isn't set unless
3158 * the force flag is one and it also handles the B_NEEDCOMMIT flag. We set
3159 * B_CACHE if this is a VMIO buffer.
3162 nfs_writebp(bp, force, td)
3168 int oldflags = bp->b_flags;
3174 if (BUF_REFCNT(bp) == 0)
3175 panic("bwrite: buffer is not locked???");
3177 if (bp->b_flags & B_INVAL) {
3182 bp->b_flags |= B_CACHE;
3185 * Undirty the bp. We will redirty it later if the I/O fails.
3190 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3192 bp->b_vp->v_numoutput++;
3196 * Note: to avoid loopback deadlocks, we do not
3197 * assign b_runningbufspace.
3199 vfs_busy_pages(bp, 1);
3202 bp->b_flags |= B_WRITEINPROG;
3204 VOP_STRATEGY(bp->b_vp, bp);
3206 if( (oldflags & B_ASYNC) == 0) {
3207 int rtval = biowait(bp);
3209 if (oldflags & B_DELWRI) {
3211 reassignbuf(bp, bp->b_vp);
3223 * nfs special file access vnode op.
3224 * Essentially just get vattr and then imitate iaccess() since the device is
3225 * local to the client.
3229 struct vop_access_args /* {
3232 struct ucred *a_cred;
3233 struct thread *a_td;
3238 struct ucred *cred = ap->a_cred;
3239 struct vnode *vp = ap->a_vp;
3240 mode_t mode = ap->a_mode;
3246 * Disallow write attempts on filesystems mounted read-only;
3247 * unless the file is a socket, fifo, or a block or character
3248 * device resident on the filesystem.
3250 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3251 switch (vp->v_type) {
3261 * If you're the super-user,
3262 * you always get access.
3264 if (cred->cr_uid == 0)
3267 error = VOP_GETATTR(vp, vap, ap->a_td);
3271 * Access check is based on only one of owner, group, public.
3272 * If not owner, then check group. If not a member of the
3273 * group, then check public access.
3275 if (cred->cr_uid != vap->va_uid) {
3277 gp = cred->cr_groups;
3278 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3279 if (vap->va_gid == *gp)
3285 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3290 * Read wrapper for special devices.
3294 struct vop_read_args /* {
3298 struct ucred *a_cred;
3301 struct nfsnode *np = VTONFS(ap->a_vp);
3307 getnanotime(&np->n_atim);
3308 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3312 * Write wrapper for special devices.
3316 struct vop_write_args /* {
3320 struct ucred *a_cred;
3323 struct nfsnode *np = VTONFS(ap->a_vp);
3329 getnanotime(&np->n_mtim);
3330 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3334 * Close wrapper for special devices.
3336 * Update the times on the nfsnode then do device close.
3340 struct vop_close_args /* {
3343 struct ucred *a_cred;
3344 struct thread *a_td;
3347 struct vnode *vp = ap->a_vp;
3348 struct nfsnode *np = VTONFS(vp);
3351 if (np->n_flag & (NACC | NUPD)) {
3353 if (vp->v_usecount == 1 &&
3354 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3356 if (np->n_flag & NACC)
3357 vattr.va_atime = np->n_atim;
3358 if (np->n_flag & NUPD)
3359 vattr.va_mtime = np->n_mtim;
3360 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3363 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3367 * Read wrapper for fifos.
3371 struct vop_read_args /* {
3375 struct ucred *a_cred;
3378 struct nfsnode *np = VTONFS(ap->a_vp);
3384 getnanotime(&np->n_atim);
3385 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3389 * Write wrapper for fifos.
3393 struct vop_write_args /* {
3397 struct ucred *a_cred;
3400 struct nfsnode *np = VTONFS(ap->a_vp);
3406 getnanotime(&np->n_mtim);
3407 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3411 * Close wrapper for fifos.
3413 * Update the times on the nfsnode then do fifo close.
3417 struct vop_close_args /* {
3420 struct thread *a_td;
3423 struct vnode *vp = ap->a_vp;
3424 struct nfsnode *np = VTONFS(vp);
3428 if (np->n_flag & (NACC | NUPD)) {
3430 if (np->n_flag & NACC)
3432 if (np->n_flag & NUPD)
3435 if (vp->v_usecount == 1 &&
3436 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3438 if (np->n_flag & NACC)
3439 vattr.va_atime = np->n_atim;
3440 if (np->n_flag & NUPD)
3441 vattr.va_mtime = np->n_mtim;
3442 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3445 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));