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 $
42 * vnode op calls for Sun NFS version 2 and 3
47 #include <sys/param.h>
48 #include <sys/kernel.h>
49 #include <sys/systm.h>
50 #include <sys/resourcevar.h>
52 #include <sys/mount.h>
54 #include <sys/malloc.h>
56 #include <sys/namei.h>
57 #include <sys/socket.h>
58 #include <sys/vnode.h>
59 #include <sys/dirent.h>
60 #include <sys/fcntl.h>
61 #include <sys/lockf.h>
63 #include <sys/sysctl.h>
67 #include <vm/vm_extern.h>
68 #include <vm/vm_zone.h>
70 #include <miscfs/fifofs/fifo.h>
72 #include <nfs/rpcv2.h>
73 #include <nfs/nfsproto.h>
75 #include <nfs/nfsnode.h>
76 #include <nfs/nfsmount.h>
77 #include <nfs/xdr_subs.h>
78 #include <nfs/nfsm_subs.h>
79 #include <nfs/nqnfs.h>
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
90 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
91 * calls are not in getblk() and brelse() so that they would not be necessary
95 #define vfs_busy_pages(bp, f)
98 static int nfsspec_read __P((struct vop_read_args *));
99 static int nfsspec_write __P((struct vop_write_args *));
100 static int nfsfifo_read __P((struct vop_read_args *));
101 static int nfsfifo_write __P((struct vop_write_args *));
102 static int nfsspec_close __P((struct vop_close_args *));
103 static int nfsfifo_close __P((struct vop_close_args *));
104 #define nfs_poll vop_nopoll
105 static int nfs_flush __P((struct vnode *,struct ucred *,int,struct proc *,int));
106 static int nfs_setattrrpc __P((struct vnode *,struct vattr *,struct ucred *,struct proc *));
107 static int nfs_lookup __P((struct vop_lookup_args *));
108 static int nfs_create __P((struct vop_create_args *));
109 static int nfs_mknod __P((struct vop_mknod_args *));
110 static int nfs_open __P((struct vop_open_args *));
111 static int nfs_close __P((struct vop_close_args *));
112 static int nfs_access __P((struct vop_access_args *));
113 static int nfs_getattr __P((struct vop_getattr_args *));
114 static int nfs_setattr __P((struct vop_setattr_args *));
115 static int nfs_read __P((struct vop_read_args *));
116 static int nfs_mmap __P((struct vop_mmap_args *));
117 static int nfs_fsync __P((struct vop_fsync_args *));
118 static int nfs_remove __P((struct vop_remove_args *));
119 static int nfs_link __P((struct vop_link_args *));
120 static int nfs_rename __P((struct vop_rename_args *));
121 static int nfs_mkdir __P((struct vop_mkdir_args *));
122 static int nfs_rmdir __P((struct vop_rmdir_args *));
123 static int nfs_symlink __P((struct vop_symlink_args *));
124 static int nfs_readdir __P((struct vop_readdir_args *));
125 static int nfs_bmap __P((struct vop_bmap_args *));
126 static int nfs_strategy __P((struct vop_strategy_args *));
127 static int nfs_lookitup __P((struct vnode *, const char *, int,
128 struct ucred *, struct proc *, struct nfsnode **));
129 static int nfs_sillyrename __P((struct vnode *,struct vnode *,struct componentname *));
130 static int nfsspec_access __P((struct vop_access_args *));
131 static int nfs_readlink __P((struct vop_readlink_args *));
132 static int nfs_print __P((struct vop_print_args *));
133 static int nfs_advlock __P((struct vop_advlock_args *));
134 static int nfs_bwrite __P((struct vop_bwrite_args *));
136 * Global vfs data structures for nfs
138 vop_t **nfsv2_vnodeop_p;
139 static struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
140 { &vop_default_desc, (vop_t *) vop_defaultop },
141 { &vop_access_desc, (vop_t *) nfs_access },
142 { &vop_advlock_desc, (vop_t *) nfs_advlock },
143 { &vop_bmap_desc, (vop_t *) nfs_bmap },
144 { &vop_bwrite_desc, (vop_t *) nfs_bwrite },
145 { &vop_close_desc, (vop_t *) nfs_close },
146 { &vop_create_desc, (vop_t *) nfs_create },
147 { &vop_fsync_desc, (vop_t *) nfs_fsync },
148 { &vop_getattr_desc, (vop_t *) nfs_getattr },
149 { &vop_getpages_desc, (vop_t *) nfs_getpages },
150 { &vop_putpages_desc, (vop_t *) nfs_putpages },
151 { &vop_inactive_desc, (vop_t *) nfs_inactive },
152 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
153 { &vop_lease_desc, (vop_t *) vop_null },
154 { &vop_link_desc, (vop_t *) nfs_link },
155 { &vop_lock_desc, (vop_t *) vop_sharedlock },
156 { &vop_lookup_desc, (vop_t *) nfs_lookup },
157 { &vop_mkdir_desc, (vop_t *) nfs_mkdir },
158 { &vop_mknod_desc, (vop_t *) nfs_mknod },
159 { &vop_mmap_desc, (vop_t *) nfs_mmap },
160 { &vop_open_desc, (vop_t *) nfs_open },
161 { &vop_poll_desc, (vop_t *) nfs_poll },
162 { &vop_print_desc, (vop_t *) nfs_print },
163 { &vop_read_desc, (vop_t *) nfs_read },
164 { &vop_readdir_desc, (vop_t *) nfs_readdir },
165 { &vop_readlink_desc, (vop_t *) nfs_readlink },
166 { &vop_reclaim_desc, (vop_t *) nfs_reclaim },
167 { &vop_remove_desc, (vop_t *) nfs_remove },
168 { &vop_rename_desc, (vop_t *) nfs_rename },
169 { &vop_rmdir_desc, (vop_t *) nfs_rmdir },
170 { &vop_setattr_desc, (vop_t *) nfs_setattr },
171 { &vop_strategy_desc, (vop_t *) nfs_strategy },
172 { &vop_symlink_desc, (vop_t *) nfs_symlink },
173 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
174 { &vop_write_desc, (vop_t *) nfs_write },
177 static struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
178 { &nfsv2_vnodeop_p, nfsv2_vnodeop_entries };
179 VNODEOP_SET(nfsv2_vnodeop_opv_desc);
182 * Special device vnode ops
184 vop_t **spec_nfsv2nodeop_p;
185 static struct vnodeopv_entry_desc nfsv2_specop_entries[] = {
186 { &vop_default_desc, (vop_t *) spec_vnoperate },
187 { &vop_access_desc, (vop_t *) nfsspec_access },
188 { &vop_close_desc, (vop_t *) nfsspec_close },
189 { &vop_fsync_desc, (vop_t *) nfs_fsync },
190 { &vop_getattr_desc, (vop_t *) nfs_getattr },
191 { &vop_inactive_desc, (vop_t *) nfs_inactive },
192 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
193 { &vop_lock_desc, (vop_t *) vop_sharedlock },
194 { &vop_print_desc, (vop_t *) nfs_print },
195 { &vop_read_desc, (vop_t *) nfsspec_read },
196 { &vop_reclaim_desc, (vop_t *) nfs_reclaim },
197 { &vop_setattr_desc, (vop_t *) nfs_setattr },
198 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
199 { &vop_write_desc, (vop_t *) nfsspec_write },
202 static struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
203 { &spec_nfsv2nodeop_p, nfsv2_specop_entries };
204 VNODEOP_SET(spec_nfsv2nodeop_opv_desc);
206 vop_t **fifo_nfsv2nodeop_p;
207 static struct vnodeopv_entry_desc nfsv2_fifoop_entries[] = {
208 { &vop_default_desc, (vop_t *) fifo_vnoperate },
209 { &vop_access_desc, (vop_t *) nfsspec_access },
210 { &vop_close_desc, (vop_t *) nfsfifo_close },
211 { &vop_fsync_desc, (vop_t *) nfs_fsync },
212 { &vop_getattr_desc, (vop_t *) nfs_getattr },
213 { &vop_inactive_desc, (vop_t *) nfs_inactive },
214 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
215 { &vop_lock_desc, (vop_t *) vop_sharedlock },
216 { &vop_print_desc, (vop_t *) nfs_print },
217 { &vop_read_desc, (vop_t *) nfsfifo_read },
218 { &vop_reclaim_desc, (vop_t *) nfs_reclaim },
219 { &vop_setattr_desc, (vop_t *) nfs_setattr },
220 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
221 { &vop_write_desc, (vop_t *) nfsfifo_write },
224 static struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
225 { &fifo_nfsv2nodeop_p, nfsv2_fifoop_entries };
226 VNODEOP_SET(fifo_nfsv2nodeop_opv_desc);
228 static int nfs_mknodrpc __P((struct vnode *dvp, struct vnode **vpp,
229 struct componentname *cnp,
231 static int nfs_removerpc __P((struct vnode *dvp, const char *name,
233 struct ucred *cred, struct proc *proc));
234 static int nfs_renamerpc __P((struct vnode *fdvp, const char *fnameptr,
235 int fnamelen, struct vnode *tdvp,
236 const char *tnameptr, int tnamelen,
237 struct ucred *cred, struct proc *proc));
238 static int nfs_renameit __P((struct vnode *sdvp,
239 struct componentname *scnp,
240 struct sillyrename *sp));
245 extern u_int32_t nfs_true, nfs_false;
246 extern u_int32_t nfs_xdrneg1;
247 extern struct nfsstats nfsstats;
248 extern nfstype nfsv3_type[9];
249 struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
250 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
251 int nfs_numasync = 0;
252 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
254 SYSCTL_DECL(_vfs_nfs);
256 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
257 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
258 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
260 static int nfsv3_commit_on_close = 0;
261 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
262 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
264 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
265 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
267 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
268 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
271 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
272 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
273 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
275 nfs3_access_otw(struct vnode *vp,
282 int error = 0, attrflag;
284 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
285 caddr_t bpos, dpos, cp2;
286 register int32_t t1, t2;
289 struct nfsnode *np = VTONFS(vp);
291 nfsstats.rpccnt[NFSPROC_ACCESS]++;
292 nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
294 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
295 *tl = txdr_unsigned(wmode);
296 nfsm_request(vp, NFSPROC_ACCESS, p, cred);
297 nfsm_postop_attr(vp, attrflag);
299 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
300 rmode = fxdr_unsigned(u_int32_t, *tl);
302 np->n_modeuid = cred->cr_uid;
303 np->n_modestamp = time_second;
310 * nfs access vnode op.
311 * For nfs version 2, just return ok. File accesses may fail later.
312 * For nfs version 3, use the access rpc to check accessibility. If file modes
313 * are changed on the server, accesses might still fail later.
317 struct vop_access_args /* {
320 struct ucred *a_cred;
324 register struct vnode *vp = ap->a_vp;
326 u_int32_t mode, wmode;
327 int v3 = NFS_ISV3(vp);
328 struct nfsnode *np = VTONFS(vp);
331 * Disallow write attempts on filesystems mounted read-only;
332 * unless the file is a socket, fifo, or a block or character
333 * device resident on the filesystem.
335 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
336 switch (vp->v_type) {
346 * For nfs v3, check to see if we have done this recently, and if
347 * so return our cached result instead of making an ACCESS call.
348 * If not, do an access rpc, otherwise you are stuck emulating
349 * ufs_access() locally using the vattr. This may not be correct,
350 * since the server may apply other access criteria such as
351 * client uid-->server uid mapping that we do not know about.
354 if (ap->a_mode & VREAD)
355 mode = NFSV3ACCESS_READ;
358 if (vp->v_type != VDIR) {
359 if (ap->a_mode & VWRITE)
360 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
361 if (ap->a_mode & VEXEC)
362 mode |= NFSV3ACCESS_EXECUTE;
364 if (ap->a_mode & VWRITE)
365 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
367 if (ap->a_mode & VEXEC)
368 mode |= NFSV3ACCESS_LOOKUP;
370 /* XXX safety belt, only make blanket request if caching */
371 if (nfsaccess_cache_timeout > 0) {
372 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
373 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
374 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
380 * Does our cached result allow us to give a definite yes to
383 if ((time_second < (np->n_modestamp + nfsaccess_cache_timeout)) &&
384 (ap->a_cred->cr_uid == np->n_modeuid) &&
385 ((np->n_mode & mode) == mode)) {
386 nfsstats.accesscache_hits++;
389 * Either a no, or a don't know. Go to the wire.
391 nfsstats.accesscache_misses++;
392 error = nfs3_access_otw(vp, wmode, ap->a_p,ap->a_cred);
394 if ((np->n_mode & mode) != mode) {
401 if ((error = nfsspec_access(ap)) != 0)
405 * Attempt to prevent a mapped root from accessing a file
406 * which it shouldn't. We try to read a byte from the file
407 * if the user is root and the file is not zero length.
408 * After calling nfsspec_access, we should have the correct
411 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
412 && VTONFS(vp)->n_size > 0) {
419 auio.uio_iov = &aiov;
423 auio.uio_segflg = UIO_SYSSPACE;
424 auio.uio_rw = UIO_READ;
425 auio.uio_procp = ap->a_p;
427 if (vp->v_type == VREG)
428 error = nfs_readrpc(vp, &auio, ap->a_cred);
429 else if (vp->v_type == VDIR) {
431 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
433 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
434 error = nfs_readdirrpc(vp, &auio, ap->a_cred);
436 } else if (vp->v_type == VLNK)
437 error = nfs_readlinkrpc(vp, &auio, ap->a_cred);
447 * Check to see if the type is ok
448 * and that deletion is not in progress.
449 * For paged in text files, you will need to flush the page cache
450 * if consistency is lost.
455 struct vop_open_args /* {
458 struct ucred *a_cred;
462 register struct vnode *vp = ap->a_vp;
463 struct nfsnode *np = VTONFS(vp);
464 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
468 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
470 printf("open eacces vtyp=%d\n",vp->v_type);
475 * Get a valid lease. If cached data is stale, flush it.
477 if (nmp->nm_flag & NFSMNT_NQNFS) {
478 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
480 error = nqnfs_getlease(vp, ND_READ, ap->a_cred,
482 } while (error == NQNFS_EXPIRED);
485 if (np->n_lrev != np->n_brev ||
486 (np->n_flag & NQNFSNONCACHE)) {
487 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
488 ap->a_p, 1)) == EINTR)
490 np->n_brev = np->n_lrev;
494 if (np->n_flag & NMODIFIED) {
495 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
496 ap->a_p, 1)) == EINTR)
499 if (vp->v_type == VDIR)
500 np->n_direofoffset = 0;
501 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_p);
504 np->n_mtime = vattr.va_mtime.tv_sec;
506 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_p);
509 if (np->n_mtime != vattr.va_mtime.tv_sec) {
510 if (vp->v_type == VDIR)
511 np->n_direofoffset = 0;
512 if ((error = nfs_vinvalbuf(vp, V_SAVE,
513 ap->a_cred, ap->a_p, 1)) == EINTR)
515 np->n_mtime = vattr.va_mtime.tv_sec;
519 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0)
520 np->n_attrstamp = 0; /* For Open/Close consistency */
526 * What an NFS client should do upon close after writing is a debatable issue.
527 * Most NFS clients push delayed writes to the server upon close, basically for
529 * 1 - So that any write errors may be reported back to the client process
530 * doing the close system call. By far the two most likely errors are
531 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
532 * 2 - To put a worst case upper bound on cache inconsistency between
533 * multiple clients for the file.
534 * There is also a consistency problem for Version 2 of the protocol w.r.t.
535 * not being able to tell if other clients are writing a file concurrently,
536 * since there is no way of knowing if the changed modify time in the reply
537 * is only due to the write for this client.
538 * (NFS Version 3 provides weak cache consistency data in the reply that
539 * should be sufficient to detect and handle this case.)
541 * The current code does the following:
542 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
543 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
544 * or commit them (this satisfies 1 and 2 except for the
545 * case where the server crashes after this close but
546 * before the commit RPC, which is felt to be "good
547 * enough". Changing the last argument to nfs_flush() to
548 * a 1 would force a commit operation, if it is felt a
549 * commit is necessary now.
550 * for NQNFS - do nothing now, since 2 is dealt with via leases and
551 * 1 should be dealt with via an fsync() system call for
552 * cases where write errors are important.
557 struct vop_close_args /* {
558 struct vnodeop_desc *a_desc;
561 struct ucred *a_cred;
565 register struct vnode *vp = ap->a_vp;
566 register struct nfsnode *np = VTONFS(vp);
569 if (vp->v_type == VREG) {
570 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 &&
571 (np->n_flag & NMODIFIED)) {
574 * Under NFSv3 we have dirty buffers to dispose of. We
575 * must flush them to the NFS server. We have the option
576 * of waiting all the way through the commit rpc or just
577 * waiting for the initial write. The default is to only
578 * wait through the initial write so the data is in the
579 * server's cache, which is roughly similar to the state
580 * a standard disk subsystem leaves the file in on close().
582 * We cannot clear the NMODIFIED bit in np->n_flag due to
583 * potential races with other processes, and certainly
584 * cannot clear it if we don't commit.
586 int cm = nfsv3_commit_on_close ? 1 : 0;
587 error = nfs_flush(vp, ap->a_cred, MNT_WAIT, ap->a_p, cm);
588 /* np->n_flag &= ~NMODIFIED; */
590 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 1);
594 if (np->n_flag & NWRITEERR) {
595 np->n_flag &= ~NWRITEERR;
603 * nfs getattr call from vfs.
607 struct vop_getattr_args /* {
610 struct ucred *a_cred;
614 register struct vnode *vp = ap->a_vp;
615 register struct nfsnode *np = VTONFS(vp);
617 register u_int32_t *tl;
618 register int32_t t1, t2;
621 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
622 int v3 = NFS_ISV3(vp);
625 * Update local times for special files.
627 if (np->n_flag & (NACC | NUPD))
630 * First look in the cache.
632 if (nfs_getattrcache(vp, ap->a_vap) == 0)
635 if (v3 && nfsaccess_cache_timeout > 0) {
636 nfsstats.accesscache_misses++;
637 nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_p, ap->a_cred);
638 if (nfs_getattrcache(vp, ap->a_vap) == 0)
642 nfsstats.rpccnt[NFSPROC_GETATTR]++;
643 nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
645 nfsm_request(vp, NFSPROC_GETATTR, ap->a_p, ap->a_cred);
647 nfsm_loadattr(vp, ap->a_vap);
658 struct vop_setattr_args /* {
659 struct vnodeop_desc *a_desc;
662 struct ucred *a_cred;
666 register struct vnode *vp = ap->a_vp;
667 register struct nfsnode *np = VTONFS(vp);
668 register struct vattr *vap = ap->a_vap;
677 * Setting of flags is not supported.
679 if (vap->va_flags != VNOVAL)
683 * Disallow write attempts if the filesystem is mounted read-only.
685 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
686 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
687 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
688 (vp->v_mount->mnt_flag & MNT_RDONLY))
690 if (vap->va_size != VNOVAL) {
691 switch (vp->v_type) {
698 if (vap->va_mtime.tv_sec == VNOVAL &&
699 vap->va_atime.tv_sec == VNOVAL &&
700 vap->va_mode == (mode_t)VNOVAL &&
701 vap->va_uid == (uid_t)VNOVAL &&
702 vap->va_gid == (gid_t)VNOVAL)
704 vap->va_size = VNOVAL;
708 * Disallow write attempts if the filesystem is
711 if (vp->v_mount->mnt_flag & MNT_RDONLY)
715 * We run vnode_pager_setsize() early (why?),
716 * we must set np->n_size now to avoid vinvalbuf
717 * V_SAVE races that might setsize a lower
722 error = nfs_meta_setsize(vp, ap->a_cred,
723 ap->a_p, vap->va_size);
725 if (np->n_flag & NMODIFIED) {
726 if (vap->va_size == 0)
727 error = nfs_vinvalbuf(vp, 0,
728 ap->a_cred, ap->a_p, 1);
730 error = nfs_vinvalbuf(vp, V_SAVE,
731 ap->a_cred, ap->a_p, 1);
734 vnode_pager_setsize(vp, np->n_size);
738 np->n_vattr.va_size = vap->va_size;
740 } else if ((vap->va_mtime.tv_sec != VNOVAL ||
741 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NMODIFIED) &&
742 vp->v_type == VREG &&
743 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
744 ap->a_p, 1)) == EINTR)
746 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_p);
747 if (error && vap->va_size != VNOVAL) {
748 np->n_size = np->n_vattr.va_size = tsize;
749 vnode_pager_setsize(vp, np->n_size);
755 * Do an nfs setattr rpc.
758 nfs_setattrrpc(vp, vap, cred, procp)
759 register struct vnode *vp;
760 register struct vattr *vap;
764 register struct nfsv2_sattr *sp;
766 register int32_t t1, t2;
767 caddr_t bpos, dpos, cp2;
769 int error = 0, wccflag = NFSV3_WCCRATTR;
770 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
771 int v3 = NFS_ISV3(vp);
773 nfsstats.rpccnt[NFSPROC_SETATTR]++;
774 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
777 nfsm_v3attrbuild(vap, TRUE);
778 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
781 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
782 if (vap->va_mode == (mode_t)VNOVAL)
783 sp->sa_mode = nfs_xdrneg1;
785 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
786 if (vap->va_uid == (uid_t)VNOVAL)
787 sp->sa_uid = nfs_xdrneg1;
789 sp->sa_uid = txdr_unsigned(vap->va_uid);
790 if (vap->va_gid == (gid_t)VNOVAL)
791 sp->sa_gid = nfs_xdrneg1;
793 sp->sa_gid = txdr_unsigned(vap->va_gid);
794 sp->sa_size = txdr_unsigned(vap->va_size);
795 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
796 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
798 nfsm_request(vp, NFSPROC_SETATTR, procp, cred);
800 nfsm_wcc_data(vp, wccflag);
802 nfsm_loadattr(vp, (struct vattr *)0);
808 * nfs lookup call, one step at a time...
809 * First look in cache
810 * If not found, unlock the directory nfsnode and do the rpc
814 struct vop_lookup_args /* {
815 struct vnodeop_desc *a_desc;
817 struct vnode **a_vpp;
818 struct componentname *a_cnp;
821 struct componentname *cnp = ap->a_cnp;
822 struct vnode *dvp = ap->a_dvp;
823 struct vnode **vpp = ap->a_vpp;
824 int flags = cnp->cn_flags;
829 struct nfsmount *nmp;
830 caddr_t bpos, dpos, cp2;
831 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
835 int lockparent, wantparent, error = 0, attrflag, fhsize;
836 int v3 = NFS_ISV3(dvp);
837 struct proc *p = cnp->cn_proc;
840 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
841 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
843 if (dvp->v_type != VDIR)
845 lockparent = flags & LOCKPARENT;
846 wantparent = flags & (LOCKPARENT|WANTPARENT);
847 nmp = VFSTONFS(dvp->v_mount);
849 if ((error = cache_lookup(dvp, vpp, cnp)) && error != ENOENT) {
853 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, p)) != 0) {
861 * See the comment starting `Step through' in ufs/ufs_lookup.c
862 * for an explanation of the locking protocol
867 } else if (flags & ISDOTDOT) {
868 VOP_UNLOCK(dvp, 0, p);
869 error = vget(newvp, LK_EXCLUSIVE, p);
870 if (!error && lockparent && (flags & ISLASTCN))
871 error = vn_lock(dvp, LK_EXCLUSIVE, p);
873 error = vget(newvp, LK_EXCLUSIVE, p);
874 if (!lockparent || error || !(flags & ISLASTCN))
875 VOP_UNLOCK(dvp, 0, p);
878 if (vpid == newvp->v_id) {
879 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, p)
880 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
881 nfsstats.lookupcache_hits++;
882 if (cnp->cn_nameiop != LOOKUP &&
884 cnp->cn_flags |= SAVENAME;
890 if (lockparent && dvp != newvp && (flags & ISLASTCN))
891 VOP_UNLOCK(dvp, 0, p);
893 error = vn_lock(dvp, LK_EXCLUSIVE, p);
900 nfsstats.lookupcache_misses++;
901 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
902 len = cnp->cn_namelen;
903 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
904 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
906 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
907 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_proc, cnp->cn_cred);
909 nfsm_postop_attr(dvp, attrflag);
913 nfsm_getfh(fhp, fhsize, v3);
916 * Handle RENAME case...
918 if (cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
919 if (NFS_CMPFH(np, fhp, fhsize)) {
923 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
930 nfsm_postop_attr(newvp, attrflag);
931 nfsm_postop_attr(dvp, attrflag);
933 nfsm_loadattr(newvp, (struct vattr *)0);
936 cnp->cn_flags |= SAVENAME;
938 VOP_UNLOCK(dvp, 0, p);
942 if (flags & ISDOTDOT) {
943 VOP_UNLOCK(dvp, 0, p);
944 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
946 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
950 if (lockparent && (flags & ISLASTCN) &&
951 (error = vn_lock(dvp, LK_EXCLUSIVE, p))) {
955 } else if (NFS_CMPFH(np, fhp, fhsize)) {
959 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
964 if (!lockparent || !(flags & ISLASTCN))
965 VOP_UNLOCK(dvp, 0, p);
969 nfsm_postop_attr(newvp, attrflag);
970 nfsm_postop_attr(dvp, attrflag);
972 nfsm_loadattr(newvp, (struct vattr *)0);
973 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
974 cnp->cn_flags |= SAVENAME;
975 if ((cnp->cn_flags & MAKEENTRY) &&
976 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
977 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
978 cache_enter(dvp, newvp, cnp);
983 if (newvp != NULLVP) {
987 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
988 (flags & ISLASTCN) && error == ENOENT) {
990 VOP_UNLOCK(dvp, 0, p);
991 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
996 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
997 cnp->cn_flags |= SAVENAME;
1004 * Just call nfs_bioread() to do the work.
1008 struct vop_read_args /* {
1012 struct ucred *a_cred;
1015 register struct vnode *vp = ap->a_vp;
1017 if (vp->v_type != VREG)
1019 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1027 struct vop_readlink_args /* {
1030 struct ucred *a_cred;
1033 register struct vnode *vp = ap->a_vp;
1035 if (vp->v_type != VLNK)
1037 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred));
1041 * Do a readlink rpc.
1042 * Called by nfs_doio() from below the buffer cache.
1045 nfs_readlinkrpc(vp, uiop, cred)
1046 register struct vnode *vp;
1050 register u_int32_t *tl;
1051 register caddr_t cp;
1052 register int32_t t1, t2;
1053 caddr_t bpos, dpos, cp2;
1054 int error = 0, len, attrflag;
1055 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1056 int v3 = NFS_ISV3(vp);
1058 nfsstats.rpccnt[NFSPROC_READLINK]++;
1059 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1061 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_procp, cred);
1063 nfsm_postop_attr(vp, attrflag);
1065 nfsm_strsiz(len, NFS_MAXPATHLEN);
1066 if (len == NFS_MAXPATHLEN) {
1067 struct nfsnode *np = VTONFS(vp);
1068 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1071 nfsm_mtouio(uiop, len);
1082 nfs_readrpc(vp, uiop, cred)
1083 register struct vnode *vp;
1087 register u_int32_t *tl;
1088 register caddr_t cp;
1089 register int32_t t1, t2;
1090 caddr_t bpos, dpos, cp2;
1091 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1092 struct nfsmount *nmp;
1093 int error = 0, len, retlen, tsiz, eof, attrflag;
1094 int v3 = NFS_ISV3(vp);
1099 nmp = VFSTONFS(vp->v_mount);
1100 tsiz = uiop->uio_resid;
1101 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1104 nfsstats.rpccnt[NFSPROC_READ]++;
1105 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1106 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1108 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1110 txdr_hyper(uiop->uio_offset, tl);
1111 *(tl + 2) = txdr_unsigned(len);
1113 *tl++ = txdr_unsigned(uiop->uio_offset);
1114 *tl++ = txdr_unsigned(len);
1117 nfsm_request(vp, NFSPROC_READ, uiop->uio_procp, cred);
1119 nfsm_postop_attr(vp, attrflag);
1124 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1125 eof = fxdr_unsigned(int, *(tl + 1));
1127 nfsm_loadattr(vp, (struct vattr *)0);
1128 nfsm_strsiz(retlen, nmp->nm_rsize);
1129 nfsm_mtouio(uiop, retlen);
1133 if (eof || retlen == 0) {
1136 } else if (retlen < len) {
1148 nfs_writerpc(vp, uiop, cred, iomode, must_commit)
1149 register struct vnode *vp;
1150 register struct uio *uiop;
1152 int *iomode, *must_commit;
1154 register u_int32_t *tl;
1155 register caddr_t cp;
1156 register int32_t t1, t2, backup;
1157 caddr_t bpos, dpos, cp2;
1158 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1159 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1160 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1161 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1164 if (uiop->uio_iovcnt != 1)
1165 panic("nfs: writerpc iovcnt > 1");
1168 tsiz = uiop->uio_resid;
1169 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1172 nfsstats.rpccnt[NFSPROC_WRITE]++;
1173 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1174 nfsm_reqhead(vp, NFSPROC_WRITE,
1175 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1178 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1179 txdr_hyper(uiop->uio_offset, tl);
1181 *tl++ = txdr_unsigned(len);
1182 *tl++ = txdr_unsigned(*iomode);
1183 *tl = txdr_unsigned(len);
1185 register u_int32_t x;
1187 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1188 /* Set both "begin" and "current" to non-garbage. */
1189 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1190 *tl++ = x; /* "begin offset" */
1191 *tl++ = x; /* "current offset" */
1192 x = txdr_unsigned(len);
1193 *tl++ = x; /* total to this offset */
1194 *tl = x; /* size of this write */
1196 nfsm_uiotom(uiop, len);
1197 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_procp, cred);
1199 wccflag = NFSV3_WCCCHK;
1200 nfsm_wcc_data(vp, wccflag);
1202 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1203 + NFSX_V3WRITEVERF);
1204 rlen = fxdr_unsigned(int, *tl++);
1209 } else if (rlen < len) {
1210 backup = len - rlen;
1211 uiop->uio_iov->iov_base -= backup;
1212 uiop->uio_iov->iov_len += backup;
1213 uiop->uio_offset -= backup;
1214 uiop->uio_resid += backup;
1217 commit = fxdr_unsigned(int, *tl++);
1220 * Return the lowest committment level
1221 * obtained by any of the RPCs.
1223 if (committed == NFSV3WRITE_FILESYNC)
1225 else if (committed == NFSV3WRITE_DATASYNC &&
1226 commit == NFSV3WRITE_UNSTABLE)
1228 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1229 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1231 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1232 } else if (bcmp((caddr_t)tl,
1233 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1235 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1240 nfsm_loadattr(vp, (struct vattr *)0);
1242 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime.tv_sec;
1249 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1250 committed = NFSV3WRITE_FILESYNC;
1251 *iomode = committed;
1253 uiop->uio_resid = tsiz;
1259 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1260 * mode set to specify the file type and the size field for rdev.
1263 nfs_mknodrpc(dvp, vpp, cnp, vap)
1264 register struct vnode *dvp;
1265 register struct vnode **vpp;
1266 register struct componentname *cnp;
1267 register struct vattr *vap;
1269 register struct nfsv2_sattr *sp;
1270 register u_int32_t *tl;
1271 register caddr_t cp;
1272 register int32_t t1, t2;
1273 struct vnode *newvp = (struct vnode *)0;
1274 struct nfsnode *np = (struct nfsnode *)0;
1278 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1279 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1281 int v3 = NFS_ISV3(dvp);
1283 if (vap->va_type == VCHR || vap->va_type == VBLK)
1284 rdev = txdr_unsigned(vap->va_rdev);
1285 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1288 return (EOPNOTSUPP);
1290 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_proc)) != 0) {
1293 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1294 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1295 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1296 nfsm_fhtom(dvp, v3);
1297 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1299 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1300 *tl++ = vtonfsv3_type(vap->va_type);
1301 nfsm_v3attrbuild(vap, FALSE);
1302 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1303 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1304 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1305 *tl = txdr_unsigned(uminor(vap->va_rdev));
1308 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1309 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1310 sp->sa_uid = nfs_xdrneg1;
1311 sp->sa_gid = nfs_xdrneg1;
1313 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1314 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1316 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_proc, cnp->cn_cred);
1318 nfsm_mtofh(dvp, newvp, v3, gotvp);
1322 newvp = (struct vnode *)0;
1324 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1325 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np);
1331 nfsm_wcc_data(dvp, wccflag);
1337 if (cnp->cn_flags & MAKEENTRY)
1338 cache_enter(dvp, newvp, cnp);
1341 VTONFS(dvp)->n_flag |= NMODIFIED;
1343 VTONFS(dvp)->n_attrstamp = 0;
1349 * just call nfs_mknodrpc() to do the work.
1354 struct vop_mknod_args /* {
1355 struct vnode *a_dvp;
1356 struct vnode **a_vpp;
1357 struct componentname *a_cnp;
1358 struct vattr *a_vap;
1361 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1364 static u_long create_verf;
1366 * nfs file create call
1370 struct vop_create_args /* {
1371 struct vnode *a_dvp;
1372 struct vnode **a_vpp;
1373 struct componentname *a_cnp;
1374 struct vattr *a_vap;
1377 register struct vnode *dvp = ap->a_dvp;
1378 register struct vattr *vap = ap->a_vap;
1379 register struct componentname *cnp = ap->a_cnp;
1380 register struct nfsv2_sattr *sp;
1381 register u_int32_t *tl;
1382 register caddr_t cp;
1383 register int32_t t1, t2;
1384 struct nfsnode *np = (struct nfsnode *)0;
1385 struct vnode *newvp = (struct vnode *)0;
1386 caddr_t bpos, dpos, cp2;
1387 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1388 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1390 int v3 = NFS_ISV3(dvp);
1393 * Oops, not for me..
1395 if (vap->va_type == VSOCK)
1396 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1398 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_proc)) != 0) {
1401 if (vap->va_vaflags & VA_EXCLUSIVE)
1404 nfsstats.rpccnt[NFSPROC_CREATE]++;
1405 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1406 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1407 nfsm_fhtom(dvp, v3);
1408 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1410 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1411 if (fmode & O_EXCL) {
1412 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1413 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1415 if (!TAILQ_EMPTY(&in_ifaddrhead))
1416 *tl++ = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr.s_addr;
1419 *tl++ = create_verf;
1420 *tl = ++create_verf;
1422 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1423 nfsm_v3attrbuild(vap, FALSE);
1426 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1427 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1428 sp->sa_uid = nfs_xdrneg1;
1429 sp->sa_gid = nfs_xdrneg1;
1431 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1432 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1434 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_proc, cnp->cn_cred);
1436 nfsm_mtofh(dvp, newvp, v3, gotvp);
1440 newvp = (struct vnode *)0;
1442 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1443 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np);
1449 nfsm_wcc_data(dvp, wccflag);
1452 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1458 } else if (v3 && (fmode & O_EXCL)) {
1460 * We are normally called with only a partially initialized
1461 * VAP. Since the NFSv3 spec says that server may use the
1462 * file attributes to store the verifier, the spec requires
1463 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1464 * in atime, but we can't really assume that all servers will
1465 * so we ensure that our SETATTR sets both atime and mtime.
1467 if (vap->va_mtime.tv_sec == VNOVAL)
1468 vfs_timestamp(&vap->va_mtime);
1469 if (vap->va_atime.tv_sec == VNOVAL)
1470 vap->va_atime = vap->va_mtime;
1471 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_proc);
1474 if (cnp->cn_flags & MAKEENTRY)
1475 cache_enter(dvp, newvp, cnp);
1478 VTONFS(dvp)->n_flag |= NMODIFIED;
1480 VTONFS(dvp)->n_attrstamp = 0;
1485 * nfs file remove call
1486 * To try and make nfs semantics closer to ufs semantics, a file that has
1487 * other processes using the vnode is renamed instead of removed and then
1488 * removed later on the last close.
1489 * - If v_usecount > 1
1490 * If a rename is not already in the works
1491 * call nfs_sillyrename() to set it up
1497 struct vop_remove_args /* {
1498 struct vnodeop_desc *a_desc;
1499 struct vnode * a_dvp;
1500 struct vnode * a_vp;
1501 struct componentname * a_cnp;
1504 register struct vnode *vp = ap->a_vp;
1505 register struct vnode *dvp = ap->a_dvp;
1506 register struct componentname *cnp = ap->a_cnp;
1507 register struct nfsnode *np = VTONFS(vp);
1512 if ((cnp->cn_flags & HASBUF) == 0)
1513 panic("nfs_remove: no name");
1514 if (vp->v_usecount < 1)
1515 panic("nfs_remove: bad v_usecount");
1517 if (vp->v_type == VDIR)
1519 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1520 VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_proc) == 0 &&
1521 vattr.va_nlink > 1)) {
1523 * Purge the name cache so that the chance of a lookup for
1524 * the name succeeding while the remove is in progress is
1525 * minimized. Without node locking it can still happen, such
1526 * that an I/O op returns ESTALE, but since you get this if
1527 * another host removes the file..
1531 * throw away biocache buffers, mainly to avoid
1532 * unnecessary delayed writes later.
1534 error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, cnp->cn_proc, 1);
1537 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1538 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc);
1540 * Kludge City: If the first reply to the remove rpc is lost..
1541 * the reply to the retransmitted request will be ENOENT
1542 * since the file was in fact removed
1543 * Therefore, we cheat and return success.
1545 if (error == ENOENT)
1547 } else if (!np->n_sillyrename)
1548 error = nfs_sillyrename(dvp, vp, cnp);
1549 np->n_attrstamp = 0;
1554 * nfs file remove rpc called from nfs_inactive
1558 register struct sillyrename *sp;
1561 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1566 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1569 nfs_removerpc(dvp, name, namelen, cred, proc)
1570 register struct vnode *dvp;
1576 register u_int32_t *tl;
1577 register caddr_t cp;
1578 register int32_t t1, t2;
1579 caddr_t bpos, dpos, cp2;
1580 int error = 0, wccflag = NFSV3_WCCRATTR;
1581 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1582 int v3 = NFS_ISV3(dvp);
1584 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1585 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1586 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1587 nfsm_fhtom(dvp, v3);
1588 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1589 nfsm_request(dvp, NFSPROC_REMOVE, proc, cred);
1591 nfsm_wcc_data(dvp, wccflag);
1593 VTONFS(dvp)->n_flag |= NMODIFIED;
1595 VTONFS(dvp)->n_attrstamp = 0;
1600 * nfs file rename call
1604 struct vop_rename_args /* {
1605 struct vnode *a_fdvp;
1606 struct vnode *a_fvp;
1607 struct componentname *a_fcnp;
1608 struct vnode *a_tdvp;
1609 struct vnode *a_tvp;
1610 struct componentname *a_tcnp;
1613 register struct vnode *fvp = ap->a_fvp;
1614 register struct vnode *tvp = ap->a_tvp;
1615 register struct vnode *fdvp = ap->a_fdvp;
1616 register struct vnode *tdvp = ap->a_tdvp;
1617 register struct componentname *tcnp = ap->a_tcnp;
1618 register struct componentname *fcnp = ap->a_fcnp;
1622 if ((tcnp->cn_flags & HASBUF) == 0 ||
1623 (fcnp->cn_flags & HASBUF) == 0)
1624 panic("nfs_rename: no name");
1626 /* Check for cross-device rename */
1627 if ((fvp->v_mount != tdvp->v_mount) ||
1628 (tvp && (fvp->v_mount != tvp->v_mount))) {
1634 * We have to flush B_DELWRI data prior to renaming
1635 * the file. If we don't, the delayed-write buffers
1636 * can be flushed out later after the file has gone stale
1637 * under NFSV3. NFSV2 does not have this problem because
1638 * ( as far as I can tell ) it flushes dirty buffers more
1642 VOP_FSYNC(fvp, fcnp->cn_cred, MNT_WAIT, fcnp->cn_proc);
1644 VOP_FSYNC(tvp, tcnp->cn_cred, MNT_WAIT, tcnp->cn_proc);
1647 * If the tvp exists and is in use, sillyrename it before doing the
1648 * rename of the new file over it.
1649 * XXX Can't sillyrename a directory.
1651 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1652 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1657 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1658 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1661 if (fvp->v_type == VDIR) {
1662 if (tvp != NULL && tvp->v_type == VDIR)
1677 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1679 if (error == ENOENT)
1685 * nfs file rename rpc called from nfs_remove() above
1688 nfs_renameit(sdvp, scnp, sp)
1690 struct componentname *scnp;
1691 register struct sillyrename *sp;
1693 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1694 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_proc));
1698 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1701 nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, proc)
1702 register struct vnode *fdvp;
1703 const char *fnameptr;
1705 register struct vnode *tdvp;
1706 const char *tnameptr;
1711 register u_int32_t *tl;
1712 register caddr_t cp;
1713 register int32_t t1, t2;
1714 caddr_t bpos, dpos, cp2;
1715 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1716 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1717 int v3 = NFS_ISV3(fdvp);
1719 nfsstats.rpccnt[NFSPROC_RENAME]++;
1720 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1721 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1722 nfsm_rndup(tnamelen));
1723 nfsm_fhtom(fdvp, v3);
1724 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1725 nfsm_fhtom(tdvp, v3);
1726 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1727 nfsm_request(fdvp, NFSPROC_RENAME, proc, cred);
1729 nfsm_wcc_data(fdvp, fwccflag);
1730 nfsm_wcc_data(tdvp, twccflag);
1733 VTONFS(fdvp)->n_flag |= NMODIFIED;
1734 VTONFS(tdvp)->n_flag |= NMODIFIED;
1736 VTONFS(fdvp)->n_attrstamp = 0;
1738 VTONFS(tdvp)->n_attrstamp = 0;
1743 * nfs hard link create call
1747 struct vop_link_args /* {
1748 struct vnode *a_tdvp;
1750 struct componentname *a_cnp;
1753 register struct vnode *vp = ap->a_vp;
1754 register struct vnode *tdvp = ap->a_tdvp;
1755 register struct componentname *cnp = ap->a_cnp;
1756 register u_int32_t *tl;
1757 register caddr_t cp;
1758 register int32_t t1, t2;
1759 caddr_t bpos, dpos, cp2;
1760 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1761 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1764 if (vp->v_mount != tdvp->v_mount) {
1769 * Push all writes to the server, so that the attribute cache
1770 * doesn't get "out of sync" with the server.
1771 * XXX There should be a better way!
1773 VOP_FSYNC(vp, cnp->cn_cred, MNT_WAIT, cnp->cn_proc);
1776 nfsstats.rpccnt[NFSPROC_LINK]++;
1777 nfsm_reqhead(vp, NFSPROC_LINK,
1778 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1780 nfsm_fhtom(tdvp, v3);
1781 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1782 nfsm_request(vp, NFSPROC_LINK, cnp->cn_proc, cnp->cn_cred);
1784 nfsm_postop_attr(vp, attrflag);
1785 nfsm_wcc_data(tdvp, wccflag);
1788 VTONFS(tdvp)->n_flag |= NMODIFIED;
1790 VTONFS(vp)->n_attrstamp = 0;
1792 VTONFS(tdvp)->n_attrstamp = 0;
1794 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1796 if (error == EEXIST)
1802 * nfs symbolic link create call
1806 struct vop_symlink_args /* {
1807 struct vnode *a_dvp;
1808 struct vnode **a_vpp;
1809 struct componentname *a_cnp;
1810 struct vattr *a_vap;
1814 register struct vnode *dvp = ap->a_dvp;
1815 register struct vattr *vap = ap->a_vap;
1816 register struct componentname *cnp = ap->a_cnp;
1817 register struct nfsv2_sattr *sp;
1818 register u_int32_t *tl;
1819 register caddr_t cp;
1820 register int32_t t1, t2;
1821 caddr_t bpos, dpos, cp2;
1822 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1823 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1824 struct vnode *newvp = (struct vnode *)0;
1825 int v3 = NFS_ISV3(dvp);
1827 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1828 slen = strlen(ap->a_target);
1829 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1830 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1831 nfsm_fhtom(dvp, v3);
1832 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1834 nfsm_v3attrbuild(vap, FALSE);
1836 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1838 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1839 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1840 sp->sa_uid = nfs_xdrneg1;
1841 sp->sa_gid = nfs_xdrneg1;
1842 sp->sa_size = nfs_xdrneg1;
1843 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1844 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1848 * Issue the NFS request and get the rpc response.
1850 * Only NFSv3 responses returning an error of 0 actually return
1851 * a file handle that can be converted into newvp without having
1852 * to do an extra lookup rpc.
1854 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_proc, cnp->cn_cred);
1857 nfsm_mtofh(dvp, newvp, v3, gotvp);
1858 nfsm_wcc_data(dvp, wccflag);
1862 * out code jumps -> here, mrep is also freed.
1868 * If we get an EEXIST error, silently convert it to no-error
1869 * in case of an NFS retry.
1871 if (error == EEXIST)
1875 * If we do not have (or no longer have) an error, and we could
1876 * not extract the newvp from the response due to the request being
1877 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1878 * to obtain a newvp to return.
1880 if (error == 0 && newvp == NULL) {
1881 struct nfsnode *np = NULL;
1883 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1884 cnp->cn_cred, cnp->cn_proc, &np);
1894 VTONFS(dvp)->n_flag |= NMODIFIED;
1896 VTONFS(dvp)->n_attrstamp = 0;
1905 struct vop_mkdir_args /* {
1906 struct vnode *a_dvp;
1907 struct vnode **a_vpp;
1908 struct componentname *a_cnp;
1909 struct vattr *a_vap;
1912 register struct vnode *dvp = ap->a_dvp;
1913 register struct vattr *vap = ap->a_vap;
1914 register struct componentname *cnp = ap->a_cnp;
1915 register struct nfsv2_sattr *sp;
1916 register u_int32_t *tl;
1917 register caddr_t cp;
1918 register int32_t t1, t2;
1920 struct nfsnode *np = (struct nfsnode *)0;
1921 struct vnode *newvp = (struct vnode *)0;
1922 caddr_t bpos, dpos, cp2;
1923 int error = 0, wccflag = NFSV3_WCCRATTR;
1925 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1927 int v3 = NFS_ISV3(dvp);
1929 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_proc)) != 0) {
1932 len = cnp->cn_namelen;
1933 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1934 nfsm_reqhead(dvp, NFSPROC_MKDIR,
1935 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1936 nfsm_fhtom(dvp, v3);
1937 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1939 nfsm_v3attrbuild(vap, FALSE);
1941 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1942 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1943 sp->sa_uid = nfs_xdrneg1;
1944 sp->sa_gid = nfs_xdrneg1;
1945 sp->sa_size = nfs_xdrneg1;
1946 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1947 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1949 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_proc, cnp->cn_cred);
1951 nfsm_mtofh(dvp, newvp, v3, gotvp);
1953 nfsm_wcc_data(dvp, wccflag);
1955 VTONFS(dvp)->n_flag |= NMODIFIED;
1957 VTONFS(dvp)->n_attrstamp = 0;
1959 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1960 * if we can succeed in looking up the directory.
1962 if (error == EEXIST || (!error && !gotvp)) {
1965 newvp = (struct vnode *)0;
1967 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
1971 if (newvp->v_type != VDIR)
1984 * nfs remove directory call
1988 struct vop_rmdir_args /* {
1989 struct vnode *a_dvp;
1991 struct componentname *a_cnp;
1994 register struct vnode *vp = ap->a_vp;
1995 register struct vnode *dvp = ap->a_dvp;
1996 register struct componentname *cnp = ap->a_cnp;
1997 register u_int32_t *tl;
1998 register caddr_t cp;
1999 register int32_t t1, t2;
2000 caddr_t bpos, dpos, cp2;
2001 int error = 0, wccflag = NFSV3_WCCRATTR;
2002 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2003 int v3 = NFS_ISV3(dvp);
2007 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2008 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2009 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2010 nfsm_fhtom(dvp, v3);
2011 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2012 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_proc, cnp->cn_cred);
2014 nfsm_wcc_data(dvp, wccflag);
2016 VTONFS(dvp)->n_flag |= NMODIFIED;
2018 VTONFS(dvp)->n_attrstamp = 0;
2022 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2024 if (error == ENOENT)
2034 struct vop_readdir_args /* {
2037 struct ucred *a_cred;
2040 register struct vnode *vp = ap->a_vp;
2041 register struct nfsnode *np = VTONFS(vp);
2042 register struct uio *uio = ap->a_uio;
2046 if (vp->v_type != VDIR)
2049 * First, check for hit on the EOF offset cache
2051 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2052 (np->n_flag & NMODIFIED) == 0) {
2053 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
2054 if (NQNFS_CKCACHABLE(vp, ND_READ)) {
2055 nfsstats.direofcache_hits++;
2058 } else if (VOP_GETATTR(vp, &vattr, ap->a_cred, uio->uio_procp) == 0 &&
2059 np->n_mtime == vattr.va_mtime.tv_sec) {
2060 nfsstats.direofcache_hits++;
2066 * Call nfs_bioread() to do the real work.
2068 tresid = uio->uio_resid;
2069 error = nfs_bioread(vp, uio, 0, ap->a_cred);
2071 if (!error && uio->uio_resid == tresid)
2072 nfsstats.direofcache_misses++;
2078 * Called from below the buffer cache by nfs_doio().
2081 nfs_readdirrpc(vp, uiop, cred)
2083 register struct uio *uiop;
2087 register int len, left;
2088 register struct dirent *dp = NULL;
2089 register u_int32_t *tl;
2090 register caddr_t cp;
2091 register int32_t t1, t2;
2092 register nfsuint64 *cookiep;
2093 caddr_t bpos, dpos, cp2;
2094 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2096 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2097 struct nfsnode *dnp = VTONFS(vp);
2099 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2101 int v3 = NFS_ISV3(vp);
2104 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2105 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2106 panic("nfs readdirrpc bad uio");
2110 * If there is no cookie, assume directory was stale.
2112 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2116 return (NFSERR_BAD_COOKIE);
2118 * Loop around doing readdir rpc's of size nm_readdirsize
2119 * truncated to a multiple of DIRBLKSIZ.
2120 * The stopping criteria is EOF or buffer full.
2122 while (more_dirs && bigenough) {
2123 nfsstats.rpccnt[NFSPROC_READDIR]++;
2124 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2128 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2129 *tl++ = cookie.nfsuquad[0];
2130 *tl++ = cookie.nfsuquad[1];
2131 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2132 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2134 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2135 *tl++ = cookie.nfsuquad[0];
2137 *tl = txdr_unsigned(nmp->nm_readdirsize);
2138 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_procp, cred);
2140 nfsm_postop_attr(vp, attrflag);
2142 nfsm_dissect(tl, u_int32_t *,
2144 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2145 dnp->n_cookieverf.nfsuquad[1] = *tl;
2151 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2152 more_dirs = fxdr_unsigned(int, *tl);
2154 /* loop thru the dir entries, doctoring them to 4bsd form */
2155 while (more_dirs && bigenough) {
2157 nfsm_dissect(tl, u_int32_t *,
2159 fileno = fxdr_hyper(tl);
2160 len = fxdr_unsigned(int, *(tl + 2));
2162 nfsm_dissect(tl, u_int32_t *,
2164 fileno = fxdr_unsigned(u_quad_t, *tl++);
2165 len = fxdr_unsigned(int, *tl);
2167 if (len <= 0 || len > NFS_MAXNAMLEN) {
2172 tlen = nfsm_rndup(len);
2174 tlen += 4; /* To ensure null termination */
2175 left = DIRBLKSIZ - blksiz;
2176 if ((tlen + DIRHDSIZ) > left) {
2177 dp->d_reclen += left;
2178 uiop->uio_iov->iov_base += left;
2179 uiop->uio_iov->iov_len -= left;
2180 uiop->uio_offset += left;
2181 uiop->uio_resid -= left;
2184 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2187 dp = (struct dirent *)uiop->uio_iov->iov_base;
2188 dp->d_fileno = (int)fileno;
2190 dp->d_reclen = tlen + DIRHDSIZ;
2191 dp->d_type = DT_UNKNOWN;
2192 blksiz += dp->d_reclen;
2193 if (blksiz == DIRBLKSIZ)
2195 uiop->uio_offset += DIRHDSIZ;
2196 uiop->uio_resid -= DIRHDSIZ;
2197 uiop->uio_iov->iov_base += DIRHDSIZ;
2198 uiop->uio_iov->iov_len -= DIRHDSIZ;
2199 nfsm_mtouio(uiop, len);
2200 cp = uiop->uio_iov->iov_base;
2202 *cp = '\0'; /* null terminate */
2203 uiop->uio_iov->iov_base += tlen;
2204 uiop->uio_iov->iov_len -= tlen;
2205 uiop->uio_offset += tlen;
2206 uiop->uio_resid -= tlen;
2208 nfsm_adv(nfsm_rndup(len));
2210 nfsm_dissect(tl, u_int32_t *,
2213 nfsm_dissect(tl, u_int32_t *,
2217 cookie.nfsuquad[0] = *tl++;
2219 cookie.nfsuquad[1] = *tl++;
2224 more_dirs = fxdr_unsigned(int, *tl);
2227 * If at end of rpc data, get the eof boolean
2230 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2231 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2236 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2237 * by increasing d_reclen for the last record.
2240 left = DIRBLKSIZ - blksiz;
2241 dp->d_reclen += left;
2242 uiop->uio_iov->iov_base += left;
2243 uiop->uio_iov->iov_len -= left;
2244 uiop->uio_offset += left;
2245 uiop->uio_resid -= left;
2249 * We are now either at the end of the directory or have filled the
2253 dnp->n_direofoffset = uiop->uio_offset;
2255 if (uiop->uio_resid > 0)
2256 printf("EEK! readdirrpc resid > 0\n");
2257 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2265 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2268 nfs_readdirplusrpc(vp, uiop, cred)
2270 register struct uio *uiop;
2273 register int len, left;
2274 register struct dirent *dp;
2275 register u_int32_t *tl;
2276 register caddr_t cp;
2277 register int32_t t1, t2;
2278 register struct vnode *newvp;
2279 register nfsuint64 *cookiep;
2280 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2281 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2282 struct nameidata nami, *ndp = &nami;
2283 struct componentname *cnp = &ndp->ni_cnd;
2285 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2286 struct nfsnode *dnp = VTONFS(vp), *np;
2289 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2290 int attrflag, fhsize;
2293 dp = (struct dirent *)0;
2296 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2297 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2298 panic("nfs readdirplusrpc bad uio");
2304 * If there is no cookie, assume directory was stale.
2306 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2310 return (NFSERR_BAD_COOKIE);
2312 * Loop around doing readdir rpc's of size nm_readdirsize
2313 * truncated to a multiple of DIRBLKSIZ.
2314 * The stopping criteria is EOF or buffer full.
2316 while (more_dirs && bigenough) {
2317 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2318 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2319 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2321 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2322 *tl++ = cookie.nfsuquad[0];
2323 *tl++ = cookie.nfsuquad[1];
2324 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2325 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2326 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2327 *tl = txdr_unsigned(nmp->nm_rsize);
2328 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_procp, cred);
2329 nfsm_postop_attr(vp, attrflag);
2334 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2335 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2336 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2337 more_dirs = fxdr_unsigned(int, *tl);
2339 /* loop thru the dir entries, doctoring them to 4bsd form */
2340 while (more_dirs && bigenough) {
2341 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2342 fileno = fxdr_hyper(tl);
2343 len = fxdr_unsigned(int, *(tl + 2));
2344 if (len <= 0 || len > NFS_MAXNAMLEN) {
2349 tlen = nfsm_rndup(len);
2351 tlen += 4; /* To ensure null termination*/
2352 left = DIRBLKSIZ - blksiz;
2353 if ((tlen + DIRHDSIZ) > left) {
2354 dp->d_reclen += left;
2355 uiop->uio_iov->iov_base += left;
2356 uiop->uio_iov->iov_len -= left;
2357 uiop->uio_offset += left;
2358 uiop->uio_resid -= left;
2361 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2364 dp = (struct dirent *)uiop->uio_iov->iov_base;
2365 dp->d_fileno = (int)fileno;
2367 dp->d_reclen = tlen + DIRHDSIZ;
2368 dp->d_type = DT_UNKNOWN;
2369 blksiz += dp->d_reclen;
2370 if (blksiz == DIRBLKSIZ)
2372 uiop->uio_offset += DIRHDSIZ;
2373 uiop->uio_resid -= DIRHDSIZ;
2374 uiop->uio_iov->iov_base += DIRHDSIZ;
2375 uiop->uio_iov->iov_len -= DIRHDSIZ;
2376 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2377 cnp->cn_namelen = len;
2378 nfsm_mtouio(uiop, len);
2379 cp = uiop->uio_iov->iov_base;
2382 uiop->uio_iov->iov_base += tlen;
2383 uiop->uio_iov->iov_len -= tlen;
2384 uiop->uio_offset += tlen;
2385 uiop->uio_resid -= tlen;
2387 nfsm_adv(nfsm_rndup(len));
2388 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2390 cookie.nfsuquad[0] = *tl++;
2391 cookie.nfsuquad[1] = *tl++;
2396 * Since the attributes are before the file handle
2397 * (sigh), we must skip over the attributes and then
2398 * come back and get them.
2400 attrflag = fxdr_unsigned(int, *tl);
2404 nfsm_adv(NFSX_V3FATTR);
2405 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2406 doit = fxdr_unsigned(int, *tl);
2408 nfsm_getfh(fhp, fhsize, 1);
2409 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2414 error = nfs_nget(vp->v_mount, fhp,
2422 if (doit && bigenough) {
2427 nfsm_loadattr(newvp, (struct vattr *)0);
2431 IFTODT(VTTOIF(np->n_vattr.va_type));
2433 cache_enter(ndp->ni_dvp, ndp->ni_vp, cnp);
2436 /* Just skip over the file handle */
2437 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2438 i = fxdr_unsigned(int, *tl);
2439 nfsm_adv(nfsm_rndup(i));
2441 if (newvp != NULLVP) {
2448 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2449 more_dirs = fxdr_unsigned(int, *tl);
2452 * If at end of rpc data, get the eof boolean
2455 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2456 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2461 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2462 * by increasing d_reclen for the last record.
2465 left = DIRBLKSIZ - blksiz;
2466 dp->d_reclen += left;
2467 uiop->uio_iov->iov_base += left;
2468 uiop->uio_iov->iov_len -= left;
2469 uiop->uio_offset += left;
2470 uiop->uio_resid -= left;
2474 * We are now either at the end of the directory or have filled the
2478 dnp->n_direofoffset = uiop->uio_offset;
2480 if (uiop->uio_resid > 0)
2481 printf("EEK! readdirplusrpc resid > 0\n");
2482 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2486 if (newvp != NULLVP) {
2497 * Silly rename. To make the NFS filesystem that is stateless look a little
2498 * more like the "ufs" a remove of an active vnode is translated to a rename
2499 * to a funny looking filename that is removed by nfs_inactive on the
2500 * nfsnode. There is the potential for another process on a different client
2501 * to create the same funny name between the nfs_lookitup() fails and the
2502 * nfs_rename() completes, but...
2505 nfs_sillyrename(dvp, vp, cnp)
2506 struct vnode *dvp, *vp;
2507 struct componentname *cnp;
2509 register struct sillyrename *sp;
2517 if (vp->v_type == VDIR)
2518 panic("nfs: sillyrename dir");
2520 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2521 M_NFSREQ, M_WAITOK);
2522 sp->s_cred = crdup(cnp->cn_cred);
2526 /* Fudge together a funny name */
2527 pid = cnp->cn_proc->p_pid;
2528 sp->s_namlen = sprintf(sp->s_name, ".nfsA%04x4.4", pid);
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_proc, (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(dvp, name, len, cred, procp, npp)
2563 register struct vnode *dvp;
2568 struct nfsnode **npp;
2570 register u_int32_t *tl;
2571 register caddr_t cp;
2572 register int32_t t1, t2;
2573 struct vnode *newvp = (struct vnode *)0;
2574 struct nfsnode *np, *dnp = VTONFS(dvp);
2575 caddr_t bpos, dpos, cp2;
2576 int error = 0, fhlen, attrflag;
2577 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2579 int v3 = NFS_ISV3(dvp);
2581 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2582 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2583 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2584 nfsm_fhtom(dvp, v3);
2585 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2586 nfsm_request(dvp, NFSPROC_LOOKUP, procp, cred);
2587 if (npp && !error) {
2588 nfsm_getfh(nfhp, fhlen, v3);
2591 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2592 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2593 np->n_fhp = &np->n_fh;
2594 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2595 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2596 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2597 np->n_fhsize = fhlen;
2599 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2603 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2611 nfsm_postop_attr(newvp, attrflag);
2612 if (!attrflag && *npp == NULL) {
2621 nfsm_loadattr(newvp, (struct vattr *)0);
2624 if (npp && *npp == NULL) {
2639 * Nfs Version 3 commit rpc
2642 nfs_commit(vp, offset, cnt, cred, procp)
2649 register caddr_t cp;
2650 register u_int32_t *tl;
2651 register int32_t t1, t2;
2652 register struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2653 caddr_t bpos, dpos, cp2;
2654 int error = 0, wccflag = NFSV3_WCCRATTR;
2655 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2657 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2659 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2660 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2662 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2663 txdr_hyper(offset, tl);
2665 *tl = txdr_unsigned(cnt);
2666 nfsm_request(vp, NFSPROC_COMMIT, procp, cred);
2667 nfsm_wcc_data(vp, wccflag);
2669 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2670 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2671 NFSX_V3WRITEVERF)) {
2672 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2674 error = NFSERR_STALEWRITEVERF;
2683 * - make nfs_bmap() essentially a no-op that does no translation
2684 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2685 * (Maybe I could use the process's page mapping, but I was concerned that
2686 * Kernel Write might not be enabled and also figured copyout() would do
2687 * a lot more work than bcopy() and also it currently happens in the
2688 * context of the swapper process (2).
2692 struct vop_bmap_args /* {
2695 struct vnode **a_vpp;
2701 register struct vnode *vp = ap->a_vp;
2703 if (ap->a_vpp != NULL)
2705 if (ap->a_bnp != NULL)
2706 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2707 if (ap->a_runp != NULL)
2709 if (ap->a_runb != NULL)
2716 * For async requests when nfsiod(s) are running, queue the request by
2717 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2722 struct vop_strategy_args *ap;
2724 register struct buf *bp = ap->a_bp;
2729 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2730 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2732 if (bp->b_flags & B_PHYS)
2733 panic("nfs physio");
2735 if (bp->b_flags & B_ASYNC)
2736 p = (struct proc *)0;
2738 p = curproc; /* XXX */
2740 if (bp->b_flags & B_READ)
2746 * If the op is asynchronous and an i/o daemon is waiting
2747 * queue the request, wake it up and wait for completion
2748 * otherwise just do it ourselves.
2750 if ((bp->b_flags & B_ASYNC) == 0 ||
2751 nfs_asyncio(bp, NOCRED, p))
2752 error = nfs_doio(bp, cr, p);
2759 * NB Currently unsupported.
2764 struct vop_mmap_args /* {
2767 struct ucred *a_cred;
2776 * fsync vnode op. Just call nfs_flush() with commit == 1.
2781 struct vop_fsync_args /* {
2782 struct vnodeop_desc *a_desc;
2783 struct vnode * a_vp;
2784 struct ucred * a_cred;
2790 return (nfs_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_p, 1));
2794 * Flush all the blocks associated with a vnode.
2795 * Walk through the buffer pool and push any dirty pages
2796 * associated with the vnode.
2799 nfs_flush(vp, cred, waitfor, p, commit)
2800 register struct vnode *vp;
2806 register struct nfsnode *np = VTONFS(vp);
2807 register struct buf *bp;
2810 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2811 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2813 u_quad_t off, endoff, toff;
2814 struct ucred* wcred = NULL;
2815 struct buf **bvec = NULL;
2816 #ifndef NFS_COMMITBVECSIZ
2817 #define NFS_COMMITBVECSIZ 20
2819 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2820 int bvecsize = 0, bveccount;
2822 if (nmp->nm_flag & NFSMNT_INT)
2827 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2828 * server, but nas not been committed to stable storage on the server
2829 * yet. On the first pass, the byte range is worked out and the commit
2830 * rpc is done. On the second pass, nfs_writebp() is called to do the
2837 if (NFS_ISV3(vp) && commit) {
2840 * Count up how many buffers waiting for a commit.
2843 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2844 nbp = TAILQ_NEXT(bp, b_vnbufs);
2845 if (BUF_REFCNT(bp) == 0 &&
2846 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2847 == (B_DELWRI | B_NEEDCOMMIT))
2851 * Allocate space to remember the list of bufs to commit. It is
2852 * important to use M_NOWAIT here to avoid a race with nfs_write.
2853 * If we can't get memory (for whatever reason), we will end up
2854 * committing the buffers one-by-one in the loop below.
2856 if (bvec != NULL && bvec != bvec_on_stack)
2858 if (bveccount > NFS_COMMITBVECSIZ) {
2859 bvec = (struct buf **)
2860 malloc(bveccount * sizeof(struct buf *),
2863 bvec = bvec_on_stack;
2864 bvecsize = NFS_COMMITBVECSIZ;
2866 bvecsize = bveccount;
2868 bvec = bvec_on_stack;
2869 bvecsize = NFS_COMMITBVECSIZ;
2871 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2872 nbp = TAILQ_NEXT(bp, b_vnbufs);
2873 if (bvecpos >= bvecsize)
2875 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2876 (B_DELWRI | B_NEEDCOMMIT) ||
2877 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
2881 * Work out if all buffers are using the same cred
2882 * so we can deal with them all with one commit.
2884 * NOTE: we are not clearing B_DONE here, so we have
2885 * to do it later on in this routine if we intend to
2886 * initiate I/O on the bp.
2888 * Note: to avoid loopback deadlocks, we do not
2889 * assign b_runningbufspace.
2892 wcred = bp->b_wcred;
2893 else if (wcred != bp->b_wcred)
2895 bp->b_flags |= B_WRITEINPROG;
2896 vfs_busy_pages(bp, 1);
2899 * bp is protected by being locked, but nbp is not
2900 * and vfs_busy_pages() may sleep. We have to
2903 nbp = TAILQ_NEXT(bp, b_vnbufs);
2906 * A list of these buffers is kept so that the
2907 * second loop knows which buffers have actually
2908 * been committed. This is necessary, since there
2909 * may be a race between the commit rpc and new
2910 * uncommitted writes on the file.
2912 bvec[bvecpos++] = bp;
2913 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2917 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2925 * Commit data on the server, as required.
2926 * If all bufs are using the same wcred, then use that with
2927 * one call for all of them, otherwise commit each one
2930 if (wcred != NOCRED)
2931 retv = nfs_commit(vp, off, (int)(endoff - off),
2935 for (i = 0; i < bvecpos; i++) {
2938 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2940 size = (u_quad_t)(bp->b_dirtyend
2942 retv = nfs_commit(vp, off, (int)size,
2948 if (retv == NFSERR_STALEWRITEVERF)
2949 nfs_clearcommit(vp->v_mount);
2952 * Now, either mark the blocks I/O done or mark the
2953 * blocks dirty, depending on whether the commit
2956 for (i = 0; i < bvecpos; i++) {
2958 bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG | B_CLUSTEROK);
2961 * Error, leave B_DELWRI intact
2963 vfs_unbusy_pages(bp);
2967 * Success, remove B_DELWRI ( bundirty() ).
2969 * b_dirtyoff/b_dirtyend seem to be NFS
2970 * specific. We should probably move that
2971 * into bundirty(). XXX
2975 bp->b_flags |= B_ASYNC;
2977 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
2978 bp->b_dirtyoff = bp->b_dirtyend = 0;
2986 * Start/do any write(s) that are required.
2990 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2991 nbp = TAILQ_NEXT(bp, b_vnbufs);
2992 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
2993 if (waitfor != MNT_WAIT || passone)
2995 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL,
2996 "nfsfsync", slpflag, slptimeo);
2999 panic("nfs_fsync: inconsistent lock");
3000 if (error == ENOLCK)
3002 if (nfs_sigintr(nmp, (struct nfsreq *)0, p)) {
3006 if (slpflag == PCATCH) {
3012 if ((bp->b_flags & B_DELWRI) == 0)
3013 panic("nfs_fsync: not dirty");
3014 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3019 if (passone || !commit)
3020 bp->b_flags |= B_ASYNC;
3022 bp->b_flags |= B_ASYNC | B_WRITEINPROG;
3024 VOP_BWRITE(bp->b_vp, bp);
3032 if (waitfor == MNT_WAIT) {
3033 while (vp->v_numoutput) {
3034 vp->v_flag |= VBWAIT;
3035 error = tsleep((caddr_t)&vp->v_numoutput,
3036 slpflag | (PRIBIO + 1), "nfsfsync", slptimeo);
3038 if (nfs_sigintr(nmp, (struct nfsreq *)0, p)) {
3042 if (slpflag == PCATCH) {
3048 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) && commit) {
3052 if (np->n_flag & NWRITEERR) {
3053 error = np->n_error;
3054 np->n_flag &= ~NWRITEERR;
3057 if (bvec != NULL && bvec != bvec_on_stack)
3063 * NFS advisory byte-level locks.
3064 * Currently unsupported.
3068 struct vop_advlock_args /* {
3076 register struct nfsnode *np = VTONFS(ap->a_vp);
3079 * The following kludge is to allow diskless support to work
3080 * until a real NFS lockd is implemented. Basically, just pretend
3081 * that this is a local lock.
3083 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3087 * Print out the contents of an nfsnode.
3091 struct vop_print_args /* {
3095 register struct vnode *vp = ap->a_vp;
3096 register struct nfsnode *np = VTONFS(vp);
3098 printf("tag VT_NFS, fileid %ld fsid 0x%x",
3099 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3100 if (vp->v_type == VFIFO)
3107 * Just call nfs_writebp() with the force argument set to 1.
3109 * NOTE: B_DONE may or may not be set in a_bp on call.
3113 struct vop_bwrite_args /* {
3117 return (nfs_writebp(ap->a_bp, 1, curproc));
3121 * This is a clone of vn_bwrite(), except that B_WRITEINPROG isn't set unless
3122 * the force flag is one and it also handles the B_NEEDCOMMIT flag. We set
3123 * B_CACHE if this is a VMIO buffer.
3126 nfs_writebp(bp, force, procp)
3127 register struct buf *bp;
3132 int oldflags = bp->b_flags;
3138 if (BUF_REFCNT(bp) == 0)
3139 panic("bwrite: buffer is not locked???");
3141 if (bp->b_flags & B_INVAL) {
3146 bp->b_flags |= B_CACHE;
3149 * Undirty the bp. We will redirty it later if the I/O fails.
3154 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3156 bp->b_vp->v_numoutput++;
3157 curproc->p_stats->p_ru.ru_oublock++;
3161 * Note: to avoid loopback deadlocks, we do not
3162 * assign b_runningbufspace.
3164 vfs_busy_pages(bp, 1);
3167 bp->b_flags |= B_WRITEINPROG;
3169 VOP_STRATEGY(bp->b_vp, bp);
3171 if( (oldflags & B_ASYNC) == 0) {
3172 int rtval = biowait(bp);
3174 if (oldflags & B_DELWRI) {
3176 reassignbuf(bp, bp->b_vp);
3188 * nfs special file access vnode op.
3189 * Essentially just get vattr and then imitate iaccess() since the device is
3190 * local to the client.
3194 struct vop_access_args /* {
3197 struct ucred *a_cred;
3201 register struct vattr *vap;
3203 register struct ucred *cred = ap->a_cred;
3204 struct vnode *vp = ap->a_vp;
3205 mode_t mode = ap->a_mode;
3211 * Disallow write attempts on filesystems mounted read-only;
3212 * unless the file is a socket, fifo, or a block or character
3213 * device resident on the filesystem.
3215 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3216 switch (vp->v_type) {
3226 * If you're the super-user,
3227 * you always get access.
3229 if (cred->cr_uid == 0)
3232 error = VOP_GETATTR(vp, vap, cred, ap->a_p);
3236 * Access check is based on only one of owner, group, public.
3237 * If not owner, then check group. If not a member of the
3238 * group, then check public access.
3240 if (cred->cr_uid != vap->va_uid) {
3242 gp = cred->cr_groups;
3243 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3244 if (vap->va_gid == *gp)
3250 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3255 * Read wrapper for special devices.
3259 struct vop_read_args /* {
3263 struct ucred *a_cred;
3266 register struct nfsnode *np = VTONFS(ap->a_vp);
3272 getnanotime(&np->n_atim);
3273 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3277 * Write wrapper for special devices.
3281 struct vop_write_args /* {
3285 struct ucred *a_cred;
3288 register struct nfsnode *np = VTONFS(ap->a_vp);
3294 getnanotime(&np->n_mtim);
3295 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3299 * Close wrapper for special devices.
3301 * Update the times on the nfsnode then do device close.
3305 struct vop_close_args /* {
3308 struct ucred *a_cred;
3312 register struct vnode *vp = ap->a_vp;
3313 register struct nfsnode *np = VTONFS(vp);
3316 if (np->n_flag & (NACC | NUPD)) {
3318 if (vp->v_usecount == 1 &&
3319 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3321 if (np->n_flag & NACC)
3322 vattr.va_atime = np->n_atim;
3323 if (np->n_flag & NUPD)
3324 vattr.va_mtime = np->n_mtim;
3325 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
3328 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3332 * Read wrapper for fifos.
3336 struct vop_read_args /* {
3340 struct ucred *a_cred;
3343 register struct nfsnode *np = VTONFS(ap->a_vp);
3349 getnanotime(&np->n_atim);
3350 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3354 * Write wrapper for fifos.
3358 struct vop_write_args /* {
3362 struct ucred *a_cred;
3365 register struct nfsnode *np = VTONFS(ap->a_vp);
3371 getnanotime(&np->n_mtim);
3372 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3376 * Close wrapper for fifos.
3378 * Update the times on the nfsnode then do fifo close.
3382 struct vop_close_args /* {
3385 struct ucred *a_cred;
3389 register struct vnode *vp = ap->a_vp;
3390 register struct nfsnode *np = VTONFS(vp);
3394 if (np->n_flag & (NACC | NUPD)) {
3396 if (np->n_flag & NACC)
3398 if (np->n_flag & NUPD)
3401 if (vp->v_usecount == 1 &&
3402 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3404 if (np->n_flag & NACC)
3405 vattr.va_atime = np->n_atim;
3406 if (np->n_flag & NUPD)
3407 vattr.va_mtime = np->n_mtim;
3408 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
3411 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));