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.15 2003/11/15 21:05:44 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 nfsv3_commit_on_close = 0;
264 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
265 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
267 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
268 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
270 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
271 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
274 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
275 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
276 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
278 nfs3_access_otw(struct vnode *vp, int wmode,
279 struct thread *td, struct ucred *cred)
283 int error = 0, attrflag;
285 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
286 caddr_t bpos, dpos, cp2;
290 struct nfsnode *np = VTONFS(vp);
292 nfsstats.rpccnt[NFSPROC_ACCESS]++;
293 nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
295 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
296 *tl = txdr_unsigned(wmode);
297 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
298 nfsm_postop_attr(vp, attrflag);
300 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
301 rmode = fxdr_unsigned(u_int32_t, *tl);
303 np->n_modeuid = cred->cr_uid;
304 np->n_modestamp = time_second;
312 * nfs access vnode op.
313 * For nfs version 2, just return ok. File accesses may fail later.
314 * For nfs version 3, use the access rpc to check accessibility. If file modes
315 * are changed on the server, accesses might still fail later.
319 struct vop_access_args /* {
322 struct ucred *a_cred;
326 struct vnode *vp = ap->a_vp;
328 u_int32_t mode, wmode;
329 int v3 = NFS_ISV3(vp);
330 struct nfsnode *np = VTONFS(vp);
333 * Disallow write attempts on filesystems mounted read-only;
334 * unless the file is a socket, fifo, or a block or character
335 * device resident on the filesystem.
337 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
338 switch (vp->v_type) {
348 * For nfs v3, check to see if we have done this recently, and if
349 * so return our cached result instead of making an ACCESS call.
350 * If not, do an access rpc, otherwise you are stuck emulating
351 * ufs_access() locally using the vattr. This may not be correct,
352 * since the server may apply other access criteria such as
353 * client uid-->server uid mapping that we do not know about.
356 if (ap->a_mode & VREAD)
357 mode = NFSV3ACCESS_READ;
360 if (vp->v_type != VDIR) {
361 if (ap->a_mode & VWRITE)
362 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
363 if (ap->a_mode & VEXEC)
364 mode |= NFSV3ACCESS_EXECUTE;
366 if (ap->a_mode & VWRITE)
367 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
369 if (ap->a_mode & VEXEC)
370 mode |= NFSV3ACCESS_LOOKUP;
372 /* XXX safety belt, only make blanket request if caching */
373 if (nfsaccess_cache_timeout > 0) {
374 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
375 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
376 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
382 * Does our cached result allow us to give a definite yes to
385 if ((time_second < (np->n_modestamp + nfsaccess_cache_timeout)) &&
386 (ap->a_cred->cr_uid == np->n_modeuid) &&
387 ((np->n_mode & mode) == mode)) {
388 nfsstats.accesscache_hits++;
391 * Either a no, or a don't know. Go to the wire.
393 nfsstats.accesscache_misses++;
394 error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred);
396 if ((np->n_mode & mode) != mode) {
402 if ((error = nfsspec_access(ap)) != 0)
406 * Attempt to prevent a mapped root from accessing a file
407 * which it shouldn't. We try to read a byte from the file
408 * if the user is root and the file is not zero length.
409 * After calling nfsspec_access, we should have the correct
412 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
413 && VTONFS(vp)->n_size > 0) {
420 auio.uio_iov = &aiov;
424 auio.uio_segflg = UIO_SYSSPACE;
425 auio.uio_rw = UIO_READ;
426 auio.uio_td = ap->a_td;
428 if (vp->v_type == VREG) {
429 error = nfs_readrpc(vp, &auio);
430 } else if (vp->v_type == VDIR) {
432 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
434 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
435 error = nfs_readdirrpc(vp, &auio);
437 } else if (vp->v_type == VLNK) {
438 error = nfs_readlinkrpc(vp, &auio);
445 * [re]record creds for reading and/or writing if access
449 if ((ap->a_mode & VREAD) && ap->a_cred != np->n_rucred) {
452 crfree(np->n_rucred);
453 np->n_rucred = ap->a_cred;
455 if ((ap->a_mode & VWRITE) && ap->a_cred != np->n_wucred) {
458 crfree(np->n_wucred);
459 np->n_wucred = ap->a_cred;
467 * Check to see if the type is ok
468 * and that deletion is not in progress.
469 * For paged in text files, you will need to flush the page cache
470 * if consistency is lost.
475 struct vop_open_args /* {
478 struct ucred *a_cred;
482 struct vnode *vp = ap->a_vp;
483 struct nfsnode *np = VTONFS(vp);
484 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
488 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
490 printf("open eacces vtyp=%d\n",vp->v_type);
495 * Get a valid lease. If cached data is stale, flush it.
497 if (nmp->nm_flag & NFSMNT_NQNFS) {
498 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
500 error = nqnfs_getlease(vp, ND_READ, ap->a_td);
501 } while (error == NQNFS_EXPIRED);
504 if (np->n_lrev != np->n_brev ||
505 (np->n_flag & NQNFSNONCACHE)) {
506 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1))
510 np->n_brev = np->n_lrev;
514 if (np->n_flag & NMODIFIED) {
515 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1))
520 if (vp->v_type == VDIR)
521 np->n_direofoffset = 0;
522 error = VOP_GETATTR(vp, &vattr, ap->a_td);
525 np->n_mtime = vattr.va_mtime.tv_sec;
527 error = VOP_GETATTR(vp, &vattr, ap->a_td);
530 if (np->n_mtime != vattr.va_mtime.tv_sec) {
531 if (vp->v_type == VDIR)
532 np->n_direofoffset = 0;
533 if ((error = nfs_vinvalbuf(vp, V_SAVE,
534 ap->a_td, 1)) == EINTR) {
537 np->n_mtime = vattr.va_mtime.tv_sec;
541 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0)
542 np->n_attrstamp = 0; /* For Open/Close consistency */
548 * What an NFS client should do upon close after writing is a debatable issue.
549 * Most NFS clients push delayed writes to the server upon close, basically for
551 * 1 - So that any write errors may be reported back to the client process
552 * doing the close system call. By far the two most likely errors are
553 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
554 * 2 - To put a worst case upper bound on cache inconsistency between
555 * multiple clients for the file.
556 * There is also a consistency problem for Version 2 of the protocol w.r.t.
557 * not being able to tell if other clients are writing a file concurrently,
558 * since there is no way of knowing if the changed modify time in the reply
559 * is only due to the write for this client.
560 * (NFS Version 3 provides weak cache consistency data in the reply that
561 * should be sufficient to detect and handle this case.)
563 * The current code does the following:
564 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
565 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
566 * or commit them (this satisfies 1 and 2 except for the
567 * case where the server crashes after this close but
568 * before the commit RPC, which is felt to be "good
569 * enough". Changing the last argument to nfs_flush() to
570 * a 1 would force a commit operation, if it is felt a
571 * commit is necessary now.
572 * for NQNFS - do nothing now, since 2 is dealt with via leases and
573 * 1 should be dealt with via an fsync() system call for
574 * cases where write errors are important.
579 struct vop_close_args /* {
580 struct vnodeop_desc *a_desc;
583 struct ucred *a_cred;
587 struct vnode *vp = ap->a_vp;
588 struct nfsnode *np = VTONFS(vp);
591 if (vp->v_type == VREG) {
592 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 &&
593 (np->n_flag & NMODIFIED)) {
596 * Under NFSv3 we have dirty buffers to dispose of. We
597 * must flush them to the NFS server. We have the option
598 * of waiting all the way through the commit rpc or just
599 * waiting for the initial write. The default is to only
600 * wait through the initial write so the data is in the
601 * server's cache, which is roughly similar to the state
602 * a standard disk subsystem leaves the file in on close().
604 * We cannot clear the NMODIFIED bit in np->n_flag due to
605 * potential races with other processes, and certainly
606 * cannot clear it if we don't commit.
608 int cm = nfsv3_commit_on_close ? 1 : 0;
609 error = nfs_flush(vp, MNT_WAIT, ap->a_td, cm);
610 /* np->n_flag &= ~NMODIFIED; */
612 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
616 if (np->n_flag & NWRITEERR) {
617 np->n_flag &= ~NWRITEERR;
625 * nfs getattr call from vfs.
629 struct vop_getattr_args /* {
632 struct ucred *a_cred;
636 struct vnode *vp = ap->a_vp;
637 struct nfsnode *np = VTONFS(vp);
643 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
644 int v3 = NFS_ISV3(vp);
647 * Update local times for special files.
649 if (np->n_flag & (NACC | NUPD))
652 * First look in the cache.
654 if (nfs_getattrcache(vp, ap->a_vap) == 0)
657 if (v3 && nfsaccess_cache_timeout > 0) {
658 nfsstats.accesscache_misses++;
659 nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_td, nfs_vpcred(vp, ND_CHECK));
660 if (nfs_getattrcache(vp, ap->a_vap) == 0)
664 nfsstats.rpccnt[NFSPROC_GETATTR]++;
665 nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
667 nfsm_request(vp, NFSPROC_GETATTR, ap->a_td, nfs_vpcred(vp, ND_CHECK));
669 nfsm_loadattr(vp, ap->a_vap);
681 struct vop_setattr_args /* {
682 struct vnodeop_desc *a_desc;
685 struct ucred *a_cred;
689 struct vnode *vp = ap->a_vp;
690 struct nfsnode *np = VTONFS(vp);
691 struct vattr *vap = ap->a_vap;
700 * Setting of flags is not supported.
702 if (vap->va_flags != VNOVAL)
706 * Disallow write attempts if the filesystem is mounted read-only.
708 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
709 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
710 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
711 (vp->v_mount->mnt_flag & MNT_RDONLY))
713 if (vap->va_size != VNOVAL) {
714 switch (vp->v_type) {
721 if (vap->va_mtime.tv_sec == VNOVAL &&
722 vap->va_atime.tv_sec == VNOVAL &&
723 vap->va_mode == (mode_t)VNOVAL &&
724 vap->va_uid == (uid_t)VNOVAL &&
725 vap->va_gid == (gid_t)VNOVAL)
727 vap->va_size = VNOVAL;
731 * Disallow write attempts if the filesystem is
734 if (vp->v_mount->mnt_flag & MNT_RDONLY)
738 * We run vnode_pager_setsize() early (why?),
739 * we must set np->n_size now to avoid vinvalbuf
740 * V_SAVE races that might setsize a lower
745 error = nfs_meta_setsize(vp, ap->a_td, vap->va_size);
747 if (np->n_flag & NMODIFIED) {
748 if (vap->va_size == 0)
749 error = nfs_vinvalbuf(vp, 0, ap->a_td, 1);
751 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
754 vnode_pager_setsize(vp, np->n_size);
758 /* np->n_size has already been set to vap->va_size
759 * in nfs_meta_setsize(). We must set it again since
760 * nfs_loadattrcache() could be called through
761 * nfs_meta_setsize() and could modify np->n_size.
763 np->n_vattr.va_size = np->n_size = vap->va_size;
765 } else if ((vap->va_mtime.tv_sec != VNOVAL ||
766 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NMODIFIED) &&
767 vp->v_type == VREG &&
768 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) == EINTR)
770 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
771 if (error && vap->va_size != VNOVAL) {
772 np->n_size = np->n_vattr.va_size = tsize;
773 vnode_pager_setsize(vp, np->n_size);
779 * Do an nfs setattr rpc.
782 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
783 struct ucred *cred, struct thread *td)
785 struct nfsv2_sattr *sp;
788 caddr_t bpos, dpos, cp2;
790 int error = 0, wccflag = NFSV3_WCCRATTR;
791 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
792 int v3 = NFS_ISV3(vp);
794 nfsstats.rpccnt[NFSPROC_SETATTR]++;
795 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
798 nfsm_v3attrbuild(vap, TRUE);
799 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
802 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
803 if (vap->va_mode == (mode_t)VNOVAL)
804 sp->sa_mode = nfs_xdrneg1;
806 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
807 if (vap->va_uid == (uid_t)VNOVAL)
808 sp->sa_uid = nfs_xdrneg1;
810 sp->sa_uid = txdr_unsigned(vap->va_uid);
811 if (vap->va_gid == (gid_t)VNOVAL)
812 sp->sa_gid = nfs_xdrneg1;
814 sp->sa_gid = txdr_unsigned(vap->va_gid);
815 sp->sa_size = txdr_unsigned(vap->va_size);
816 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
817 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
819 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
821 nfsm_wcc_data(vp, wccflag);
823 nfsm_loadattr(vp, (struct vattr *)0);
830 * nfs lookup call, one step at a time...
831 * First look in cache
832 * If not found, unlock the directory nfsnode and do the rpc
836 struct vop_lookup_args /* {
837 struct vnodeop_desc *a_desc;
839 struct vnode **a_vpp;
840 struct componentname *a_cnp;
843 struct componentname *cnp = ap->a_cnp;
844 struct vnode *dvp = ap->a_dvp;
845 struct vnode **vpp = ap->a_vpp;
846 int flags = cnp->cn_flags;
851 struct nfsmount *nmp;
852 caddr_t bpos, dpos, cp2;
853 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
857 int lockparent, wantparent, error = 0, attrflag, fhsize;
858 int v3 = NFS_ISV3(dvp);
859 struct thread *td = cnp->cn_td;
862 if ((flags & CNP_ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
863 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
865 if (dvp->v_type != VDIR)
867 lockparent = flags & CNP_LOCKPARENT;
868 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
869 nmp = VFSTONFS(dvp->v_mount);
871 if ((error = cache_lookup(dvp, NCPNULL, vpp, NCPPNULL, cnp)) && error != ENOENT) {
875 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
883 * See the comment starting `Step through' in ufs/ufs_lookup.c
884 * for an explanation of the locking protocol
889 } else if (flags & CNP_ISDOTDOT) {
890 VOP_UNLOCK(dvp, 0, td);
891 error = vget(newvp, LK_EXCLUSIVE, td);
892 if (!error && lockparent && (flags & CNP_ISLASTCN))
893 error = vn_lock(dvp, LK_EXCLUSIVE, td);
895 error = vget(newvp, LK_EXCLUSIVE, td);
896 if (!lockparent || error || !(flags & CNP_ISLASTCN))
897 VOP_UNLOCK(dvp, 0, td);
900 if (vpid == newvp->v_id) {
901 if (!VOP_GETATTR(newvp, &vattr, td)
902 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
903 nfsstats.lookupcache_hits++;
904 if (cnp->cn_nameiop != NAMEI_LOOKUP &&
905 (flags & CNP_ISLASTCN))
906 cnp->cn_flags |= CNP_SAVENAME;
912 if (lockparent && dvp != newvp && (flags & CNP_ISLASTCN))
913 VOP_UNLOCK(dvp, 0, td);
915 error = vn_lock(dvp, LK_EXCLUSIVE, td);
922 nfsstats.lookupcache_misses++;
923 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
924 len = cnp->cn_namelen;
925 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
926 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
928 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
929 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
931 nfsm_postop_attr(dvp, attrflag);
935 nfsm_getfh(fhp, fhsize, v3);
938 * Handle RENAME case...
940 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent && (flags & CNP_ISLASTCN)) {
941 if (NFS_CMPFH(np, fhp, fhsize)) {
945 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
952 nfsm_postop_attr(newvp, attrflag);
953 nfsm_postop_attr(dvp, attrflag);
955 nfsm_loadattr(newvp, (struct vattr *)0);
958 cnp->cn_flags |= CNP_SAVENAME;
960 VOP_UNLOCK(dvp, 0, td);
964 if (flags & CNP_ISDOTDOT) {
965 VOP_UNLOCK(dvp, 0, td);
966 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
968 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
972 if (lockparent && (flags & CNP_ISLASTCN) &&
973 (error = vn_lock(dvp, LK_EXCLUSIVE, td))) {
977 } else if (NFS_CMPFH(np, fhp, fhsize)) {
981 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
986 if (!lockparent || !(flags & CNP_ISLASTCN))
987 VOP_UNLOCK(dvp, 0, td);
991 nfsm_postop_attr(newvp, attrflag);
992 nfsm_postop_attr(dvp, attrflag);
994 nfsm_loadattr(newvp, (struct vattr *)0);
995 if (cnp->cn_nameiop != NAMEI_LOOKUP && (flags & CNP_ISLASTCN))
996 cnp->cn_flags |= CNP_SAVENAME;
997 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
998 (cnp->cn_nameiop != NAMEI_DELETE || !(flags & CNP_ISLASTCN))) {
999 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
1000 cache_enter(dvp, NCPNULL, newvp, cnp);
1006 if (newvp != NULLVP) {
1010 if ((cnp->cn_nameiop == NAMEI_CREATE || cnp->cn_nameiop == NAMEI_RENAME) &&
1011 (flags & CNP_ISLASTCN) && error == ENOENT) {
1013 VOP_UNLOCK(dvp, 0, td);
1014 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1017 error = EJUSTRETURN;
1019 if (cnp->cn_nameiop != NAMEI_LOOKUP && (flags & CNP_ISLASTCN))
1020 cnp->cn_flags |= CNP_SAVENAME;
1027 * Just call nfs_bioread() to do the work.
1031 struct vop_read_args /* {
1035 struct ucred *a_cred;
1038 struct vnode *vp = ap->a_vp;
1040 if (vp->v_type != VREG)
1042 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1050 struct vop_readlink_args /* {
1053 struct ucred *a_cred;
1056 struct vnode *vp = ap->a_vp;
1058 if (vp->v_type != VLNK)
1060 return (nfs_bioread(vp, ap->a_uio, 0));
1064 * Do a readlink rpc.
1065 * Called by nfs_doio() from below the buffer cache.
1068 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1073 caddr_t bpos, dpos, cp2;
1074 int error = 0, len, attrflag;
1075 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1076 int v3 = NFS_ISV3(vp);
1078 nfsstats.rpccnt[NFSPROC_READLINK]++;
1079 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1081 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1083 nfsm_postop_attr(vp, attrflag);
1085 nfsm_strsiz(len, NFS_MAXPATHLEN);
1086 if (len == NFS_MAXPATHLEN) {
1087 struct nfsnode *np = VTONFS(vp);
1088 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1091 nfsm_mtouio(uiop, len);
1103 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1108 caddr_t bpos, dpos, cp2;
1109 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1110 struct nfsmount *nmp;
1111 int error = 0, len, retlen, tsiz, eof, attrflag;
1112 int v3 = NFS_ISV3(vp);
1117 nmp = VFSTONFS(vp->v_mount);
1118 tsiz = uiop->uio_resid;
1119 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1122 nfsstats.rpccnt[NFSPROC_READ]++;
1123 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1124 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1126 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1128 txdr_hyper(uiop->uio_offset, tl);
1129 *(tl + 2) = txdr_unsigned(len);
1131 *tl++ = txdr_unsigned(uiop->uio_offset);
1132 *tl++ = txdr_unsigned(len);
1135 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1137 nfsm_postop_attr(vp, attrflag);
1142 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1143 eof = fxdr_unsigned(int, *(tl + 1));
1145 nfsm_loadattr(vp, (struct vattr *)0);
1146 nfsm_strsiz(retlen, nmp->nm_rsize);
1147 nfsm_mtouio(uiop, retlen);
1151 if (eof || retlen == 0) {
1154 } else if (retlen < len) {
1166 nfs_writerpc(vp, uiop, iomode, must_commit)
1169 int *iomode, *must_commit;
1173 int32_t t1, t2, backup;
1174 caddr_t bpos, dpos, cp2;
1175 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1176 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1177 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1178 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1181 if (uiop->uio_iovcnt != 1)
1182 panic("nfs: writerpc iovcnt > 1");
1185 tsiz = uiop->uio_resid;
1186 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1189 nfsstats.rpccnt[NFSPROC_WRITE]++;
1190 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1191 nfsm_reqhead(vp, NFSPROC_WRITE,
1192 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1195 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1196 txdr_hyper(uiop->uio_offset, tl);
1198 *tl++ = txdr_unsigned(len);
1199 *tl++ = txdr_unsigned(*iomode);
1200 *tl = txdr_unsigned(len);
1204 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1205 /* Set both "begin" and "current" to non-garbage. */
1206 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1207 *tl++ = x; /* "begin offset" */
1208 *tl++ = x; /* "current offset" */
1209 x = txdr_unsigned(len);
1210 *tl++ = x; /* total to this offset */
1211 *tl = x; /* size of this write */
1213 nfsm_uiotom(uiop, len);
1214 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1216 wccflag = NFSV3_WCCCHK;
1217 nfsm_wcc_data(vp, wccflag);
1219 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1220 + NFSX_V3WRITEVERF);
1221 rlen = fxdr_unsigned(int, *tl++);
1226 } else if (rlen < len) {
1227 backup = len - rlen;
1228 uiop->uio_iov->iov_base -= backup;
1229 uiop->uio_iov->iov_len += backup;
1230 uiop->uio_offset -= backup;
1231 uiop->uio_resid += backup;
1234 commit = fxdr_unsigned(int, *tl++);
1237 * Return the lowest committment level
1238 * obtained by any of the RPCs.
1240 if (committed == NFSV3WRITE_FILESYNC)
1242 else if (committed == NFSV3WRITE_DATASYNC &&
1243 commit == NFSV3WRITE_UNSTABLE)
1245 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1246 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1248 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1249 } else if (bcmp((caddr_t)tl,
1250 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1252 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1257 nfsm_loadattr(vp, (struct vattr *)0);
1259 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime.tv_sec;
1266 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1267 committed = NFSV3WRITE_FILESYNC;
1268 *iomode = committed;
1270 uiop->uio_resid = tsiz;
1276 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1277 * mode set to specify the file type and the size field for rdev.
1280 nfs_mknodrpc(dvp, vpp, cnp, vap)
1283 struct componentname *cnp;
1286 struct nfsv2_sattr *sp;
1290 struct vnode *newvp = (struct vnode *)0;
1291 struct nfsnode *np = (struct nfsnode *)0;
1295 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1296 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1298 int v3 = NFS_ISV3(dvp);
1300 if (vap->va_type == VCHR || vap->va_type == VBLK)
1301 rdev = txdr_unsigned(vap->va_rdev);
1302 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1305 return (EOPNOTSUPP);
1307 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1310 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1311 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1312 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1313 nfsm_fhtom(dvp, v3);
1314 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1316 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1317 *tl++ = vtonfsv3_type(vap->va_type);
1318 nfsm_v3attrbuild(vap, FALSE);
1319 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1320 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1321 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1322 *tl = txdr_unsigned(uminor(vap->va_rdev));
1325 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1326 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1327 sp->sa_uid = nfs_xdrneg1;
1328 sp->sa_gid = nfs_xdrneg1;
1330 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1331 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1333 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1335 nfsm_mtofh(dvp, newvp, v3, gotvp);
1339 newvp = (struct vnode *)0;
1341 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1342 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1348 nfsm_wcc_data(dvp, wccflag);
1355 if (cnp->cn_flags & CNP_MAKEENTRY)
1356 cache_enter(dvp, NCPNULL, newvp, cnp);
1359 VTONFS(dvp)->n_flag |= NMODIFIED;
1361 VTONFS(dvp)->n_attrstamp = 0;
1367 * just call nfs_mknodrpc() to do the work.
1372 struct vop_mknod_args /* {
1373 struct vnode *a_dvp;
1374 struct vnode **a_vpp;
1375 struct componentname *a_cnp;
1376 struct vattr *a_vap;
1379 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1382 static u_long create_verf;
1384 * nfs file create call
1388 struct vop_create_args /* {
1389 struct vnode *a_dvp;
1390 struct vnode **a_vpp;
1391 struct componentname *a_cnp;
1392 struct vattr *a_vap;
1395 struct vnode *dvp = ap->a_dvp;
1396 struct vattr *vap = ap->a_vap;
1397 struct componentname *cnp = ap->a_cnp;
1398 struct nfsv2_sattr *sp;
1402 struct nfsnode *np = (struct nfsnode *)0;
1403 struct vnode *newvp = (struct vnode *)0;
1404 caddr_t bpos, dpos, cp2;
1405 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1406 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1408 int v3 = NFS_ISV3(dvp);
1411 * Oops, not for me..
1413 if (vap->va_type == VSOCK)
1414 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1416 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1419 if (vap->va_vaflags & VA_EXCLUSIVE)
1422 nfsstats.rpccnt[NFSPROC_CREATE]++;
1423 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1424 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1425 nfsm_fhtom(dvp, v3);
1426 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1428 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1429 if (fmode & O_EXCL) {
1430 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1431 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1433 if (!TAILQ_EMPTY(&in_ifaddrhead))
1434 *tl++ = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr.s_addr;
1437 *tl++ = create_verf;
1438 *tl = ++create_verf;
1440 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1441 nfsm_v3attrbuild(vap, FALSE);
1444 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1445 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1446 sp->sa_uid = nfs_xdrneg1;
1447 sp->sa_gid = nfs_xdrneg1;
1449 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1450 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1452 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1454 nfsm_mtofh(dvp, newvp, v3, gotvp);
1458 newvp = (struct vnode *)0;
1460 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1461 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1467 nfsm_wcc_data(dvp, wccflag);
1471 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1477 } else if (v3 && (fmode & O_EXCL)) {
1479 * We are normally called with only a partially initialized
1480 * VAP. Since the NFSv3 spec says that server may use the
1481 * file attributes to store the verifier, the spec requires
1482 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1483 * in atime, but we can't really assume that all servers will
1484 * so we ensure that our SETATTR sets both atime and mtime.
1486 if (vap->va_mtime.tv_sec == VNOVAL)
1487 vfs_timestamp(&vap->va_mtime);
1488 if (vap->va_atime.tv_sec == VNOVAL)
1489 vap->va_atime = vap->va_mtime;
1490 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1493 if (cnp->cn_flags & CNP_MAKEENTRY)
1494 cache_enter(dvp, NCPNULL, newvp, cnp);
1496 * The new np may have enough info for access
1497 * checks, make sure rucred and wucred are
1498 * initialized for read and write rpc's.
1501 if (np->n_rucred == NULL)
1502 np->n_rucred = crhold(cnp->cn_cred);
1503 if (np->n_wucred == NULL)
1504 np->n_wucred = crhold(cnp->cn_cred);
1507 VTONFS(dvp)->n_flag |= NMODIFIED;
1509 VTONFS(dvp)->n_attrstamp = 0;
1514 * nfs file remove call
1515 * To try and make nfs semantics closer to ufs semantics, a file that has
1516 * other processes using the vnode is renamed instead of removed and then
1517 * removed later on the last close.
1518 * - If v_usecount > 1
1519 * If a rename is not already in the works
1520 * call nfs_sillyrename() to set it up
1526 struct vop_remove_args /* {
1527 struct vnodeop_desc *a_desc;
1528 struct vnode * a_dvp;
1529 struct vnode * a_vp;
1530 struct componentname * a_cnp;
1533 struct vnode *vp = ap->a_vp;
1534 struct vnode *dvp = ap->a_dvp;
1535 struct componentname *cnp = ap->a_cnp;
1536 struct nfsnode *np = VTONFS(vp);
1541 if ((cnp->cn_flags & CNP_HASBUF) == 0)
1542 panic("nfs_remove: no name");
1543 if (vp->v_usecount < 1)
1544 panic("nfs_remove: bad v_usecount");
1546 if (vp->v_type == VDIR)
1548 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1549 VOP_GETATTR(vp, &vattr, cnp->cn_td) == 0 &&
1550 vattr.va_nlink > 1)) {
1552 * Purge the name cache so that the chance of a lookup for
1553 * the name succeeding while the remove is in progress is
1554 * minimized. Without node locking it can still happen, such
1555 * that an I/O op returns ESTALE, but since you get this if
1556 * another host removes the file..
1560 * throw away biocache buffers, mainly to avoid
1561 * unnecessary delayed writes later.
1563 error = nfs_vinvalbuf(vp, 0, cnp->cn_td, 1);
1566 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1567 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1569 * Kludge City: If the first reply to the remove rpc is lost..
1570 * the reply to the retransmitted request will be ENOENT
1571 * since the file was in fact removed
1572 * Therefore, we cheat and return success.
1574 if (error == ENOENT)
1576 } else if (!np->n_sillyrename)
1577 error = nfs_sillyrename(dvp, vp, cnp);
1578 np->n_attrstamp = 0;
1583 * nfs file remove rpc called from nfs_inactive
1586 nfs_removeit(struct sillyrename *sp)
1589 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1594 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1597 nfs_removerpc(dvp, name, namelen, cred, td)
1607 caddr_t bpos, dpos, cp2;
1608 int error = 0, wccflag = NFSV3_WCCRATTR;
1609 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1610 int v3 = NFS_ISV3(dvp);
1612 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1613 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1614 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1615 nfsm_fhtom(dvp, v3);
1616 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1617 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1619 nfsm_wcc_data(dvp, wccflag);
1622 VTONFS(dvp)->n_flag |= NMODIFIED;
1624 VTONFS(dvp)->n_attrstamp = 0;
1629 * nfs file rename call
1633 struct vop_rename_args /* {
1634 struct vnode *a_fdvp;
1635 struct vnode *a_fvp;
1636 struct componentname *a_fcnp;
1637 struct vnode *a_tdvp;
1638 struct vnode *a_tvp;
1639 struct componentname *a_tcnp;
1642 struct vnode *fvp = ap->a_fvp;
1643 struct vnode *tvp = ap->a_tvp;
1644 struct vnode *fdvp = ap->a_fdvp;
1645 struct vnode *tdvp = ap->a_tdvp;
1646 struct componentname *tcnp = ap->a_tcnp;
1647 struct componentname *fcnp = ap->a_fcnp;
1651 if ((tcnp->cn_flags & CNP_HASBUF) == 0 ||
1652 (fcnp->cn_flags & CNP_HASBUF) == 0)
1653 panic("nfs_rename: no name");
1655 /* Check for cross-device rename */
1656 if ((fvp->v_mount != tdvp->v_mount) ||
1657 (tvp && (fvp->v_mount != tvp->v_mount))) {
1663 * We have to flush B_DELWRI data prior to renaming
1664 * the file. If we don't, the delayed-write buffers
1665 * can be flushed out later after the file has gone stale
1666 * under NFSV3. NFSV2 does not have this problem because
1667 * ( as far as I can tell ) it flushes dirty buffers more
1671 VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_td);
1673 VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_td);
1676 * If the tvp exists and is in use, sillyrename it before doing the
1677 * rename of the new file over it.
1678 * XXX Can't sillyrename a directory.
1680 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1681 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1686 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1687 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1690 if (fvp->v_type == VDIR) {
1691 if (tvp != NULL && tvp->v_type == VDIR)
1706 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1708 if (error == ENOENT)
1714 * nfs file rename rpc called from nfs_remove() above
1717 nfs_renameit(sdvp, scnp, sp)
1719 struct componentname *scnp;
1720 struct sillyrename *sp;
1722 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1723 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1727 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1730 nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, td)
1732 const char *fnameptr;
1735 const char *tnameptr;
1743 caddr_t bpos, dpos, cp2;
1744 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1745 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1746 int v3 = NFS_ISV3(fdvp);
1748 nfsstats.rpccnt[NFSPROC_RENAME]++;
1749 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1750 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1751 nfsm_rndup(tnamelen));
1752 nfsm_fhtom(fdvp, v3);
1753 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1754 nfsm_fhtom(tdvp, v3);
1755 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1756 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1758 nfsm_wcc_data(fdvp, fwccflag);
1759 nfsm_wcc_data(tdvp, twccflag);
1763 VTONFS(fdvp)->n_flag |= NMODIFIED;
1764 VTONFS(tdvp)->n_flag |= NMODIFIED;
1766 VTONFS(fdvp)->n_attrstamp = 0;
1768 VTONFS(tdvp)->n_attrstamp = 0;
1773 * nfs hard link create call
1777 struct vop_link_args /* {
1778 struct vnode *a_tdvp;
1780 struct componentname *a_cnp;
1783 struct vnode *vp = ap->a_vp;
1784 struct vnode *tdvp = ap->a_tdvp;
1785 struct componentname *cnp = ap->a_cnp;
1789 caddr_t bpos, dpos, cp2;
1790 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1791 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1794 if (vp->v_mount != tdvp->v_mount) {
1799 * Push all writes to the server, so that the attribute cache
1800 * doesn't get "out of sync" with the server.
1801 * XXX There should be a better way!
1803 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_td);
1806 nfsstats.rpccnt[NFSPROC_LINK]++;
1807 nfsm_reqhead(vp, NFSPROC_LINK,
1808 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1810 nfsm_fhtom(tdvp, v3);
1811 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1812 nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1814 nfsm_postop_attr(vp, attrflag);
1815 nfsm_wcc_data(tdvp, wccflag);
1819 VTONFS(tdvp)->n_flag |= NMODIFIED;
1821 VTONFS(vp)->n_attrstamp = 0;
1823 VTONFS(tdvp)->n_attrstamp = 0;
1825 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1827 if (error == EEXIST)
1833 * nfs symbolic link create call
1837 struct vop_symlink_args /* {
1838 struct vnode *a_dvp;
1839 struct vnode **a_vpp;
1840 struct componentname *a_cnp;
1841 struct vattr *a_vap;
1845 struct vnode *dvp = ap->a_dvp;
1846 struct vattr *vap = ap->a_vap;
1847 struct componentname *cnp = ap->a_cnp;
1848 struct nfsv2_sattr *sp;
1852 caddr_t bpos, dpos, cp2;
1853 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1854 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1855 struct vnode *newvp = (struct vnode *)0;
1856 int v3 = NFS_ISV3(dvp);
1858 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1859 slen = strlen(ap->a_target);
1860 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1861 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1862 nfsm_fhtom(dvp, v3);
1863 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1865 nfsm_v3attrbuild(vap, FALSE);
1867 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1869 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1870 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1871 sp->sa_uid = nfs_xdrneg1;
1872 sp->sa_gid = nfs_xdrneg1;
1873 sp->sa_size = nfs_xdrneg1;
1874 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1875 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1879 * Issue the NFS request and get the rpc response.
1881 * Only NFSv3 responses returning an error of 0 actually return
1882 * a file handle that can be converted into newvp without having
1883 * to do an extra lookup rpc.
1885 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1888 nfsm_mtofh(dvp, newvp, v3, gotvp);
1889 nfsm_wcc_data(dvp, wccflag);
1893 * out code jumps -> here, mrep is also freed.
1900 * If we get an EEXIST error, silently convert it to no-error
1901 * in case of an NFS retry.
1903 if (error == EEXIST)
1907 * If we do not have (or no longer have) an error, and we could
1908 * not extract the newvp from the response due to the request being
1909 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1910 * to obtain a newvp to return.
1912 if (error == 0 && newvp == NULL) {
1913 struct nfsnode *np = NULL;
1915 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1916 cnp->cn_cred, cnp->cn_td, &np);
1926 VTONFS(dvp)->n_flag |= NMODIFIED;
1928 VTONFS(dvp)->n_attrstamp = 0;
1937 struct vop_mkdir_args /* {
1938 struct vnode *a_dvp;
1939 struct vnode **a_vpp;
1940 struct componentname *a_cnp;
1941 struct vattr *a_vap;
1944 struct vnode *dvp = ap->a_dvp;
1945 struct vattr *vap = ap->a_vap;
1946 struct componentname *cnp = ap->a_cnp;
1947 struct nfsv2_sattr *sp;
1952 struct nfsnode *np = (struct nfsnode *)0;
1953 struct vnode *newvp = (struct vnode *)0;
1954 caddr_t bpos, dpos, cp2;
1955 int error = 0, wccflag = NFSV3_WCCRATTR;
1957 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1959 int v3 = NFS_ISV3(dvp);
1961 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1964 len = cnp->cn_namelen;
1965 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1966 nfsm_reqhead(dvp, NFSPROC_MKDIR,
1967 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1968 nfsm_fhtom(dvp, v3);
1969 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1971 nfsm_v3attrbuild(vap, FALSE);
1973 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1974 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1975 sp->sa_uid = nfs_xdrneg1;
1976 sp->sa_gid = nfs_xdrneg1;
1977 sp->sa_size = nfs_xdrneg1;
1978 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1979 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1981 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
1983 nfsm_mtofh(dvp, newvp, v3, gotvp);
1985 nfsm_wcc_data(dvp, wccflag);
1988 VTONFS(dvp)->n_flag |= NMODIFIED;
1990 VTONFS(dvp)->n_attrstamp = 0;
1992 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1993 * if we can succeed in looking up the directory.
1995 if (error == EEXIST || (!error && !gotvp)) {
1998 newvp = (struct vnode *)0;
2000 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2004 if (newvp->v_type != VDIR)
2017 * nfs remove directory call
2021 struct vop_rmdir_args /* {
2022 struct vnode *a_dvp;
2024 struct componentname *a_cnp;
2027 struct vnode *vp = ap->a_vp;
2028 struct vnode *dvp = ap->a_dvp;
2029 struct componentname *cnp = ap->a_cnp;
2033 caddr_t bpos, dpos, cp2;
2034 int error = 0, wccflag = NFSV3_WCCRATTR;
2035 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2036 int v3 = NFS_ISV3(dvp);
2040 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2041 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2042 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2043 nfsm_fhtom(dvp, v3);
2044 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2045 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2047 nfsm_wcc_data(dvp, wccflag);
2050 VTONFS(dvp)->n_flag |= NMODIFIED;
2052 VTONFS(dvp)->n_attrstamp = 0;
2056 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2058 if (error == ENOENT)
2068 struct vop_readdir_args /* {
2071 struct ucred *a_cred;
2074 struct vnode *vp = ap->a_vp;
2075 struct nfsnode *np = VTONFS(vp);
2076 struct uio *uio = ap->a_uio;
2080 if (vp->v_type != VDIR)
2083 * First, check for hit on the EOF offset cache
2085 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2086 (np->n_flag & NMODIFIED) == 0) {
2087 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
2088 if (NQNFS_CKCACHABLE(vp, ND_READ)) {
2089 nfsstats.direofcache_hits++;
2092 } else if (VOP_GETATTR(vp, &vattr, uio->uio_td) == 0 &&
2093 np->n_mtime == vattr.va_mtime.tv_sec) {
2094 nfsstats.direofcache_hits++;
2100 * Call nfs_bioread() to do the real work.
2102 tresid = uio->uio_resid;
2103 error = nfs_bioread(vp, uio, 0);
2105 if (!error && uio->uio_resid == tresid)
2106 nfsstats.direofcache_misses++;
2112 * Called from below the buffer cache by nfs_doio().
2115 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2118 struct dirent *dp = NULL;
2123 caddr_t bpos, dpos, cp2;
2124 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2126 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2127 struct nfsnode *dnp = VTONFS(vp);
2129 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2131 int v3 = NFS_ISV3(vp);
2134 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2135 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2136 panic("nfs readdirrpc bad uio");
2140 * If there is no cookie, assume directory was stale.
2142 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2146 return (NFSERR_BAD_COOKIE);
2148 * Loop around doing readdir rpc's of size nm_readdirsize
2149 * truncated to a multiple of DIRBLKSIZ.
2150 * The stopping criteria is EOF or buffer full.
2152 while (more_dirs && bigenough) {
2153 nfsstats.rpccnt[NFSPROC_READDIR]++;
2154 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2158 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2159 *tl++ = cookie.nfsuquad[0];
2160 *tl++ = cookie.nfsuquad[1];
2161 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2162 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2164 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2165 *tl++ = cookie.nfsuquad[0];
2167 *tl = txdr_unsigned(nmp->nm_readdirsize);
2168 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2170 nfsm_postop_attr(vp, attrflag);
2172 nfsm_dissect(tl, u_int32_t *,
2174 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2175 dnp->n_cookieverf.nfsuquad[1] = *tl;
2181 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2182 more_dirs = fxdr_unsigned(int, *tl);
2184 /* loop thru the dir entries, doctoring them to 4bsd form */
2185 while (more_dirs && bigenough) {
2187 nfsm_dissect(tl, u_int32_t *,
2189 fileno = fxdr_hyper(tl);
2190 len = fxdr_unsigned(int, *(tl + 2));
2192 nfsm_dissect(tl, u_int32_t *,
2194 fileno = fxdr_unsigned(u_quad_t, *tl++);
2195 len = fxdr_unsigned(int, *tl);
2197 if (len <= 0 || len > NFS_MAXNAMLEN) {
2202 tlen = nfsm_rndup(len);
2204 tlen += 4; /* To ensure null termination */
2205 left = DIRBLKSIZ - blksiz;
2206 if ((tlen + DIRHDSIZ) > left) {
2207 dp->d_reclen += left;
2208 uiop->uio_iov->iov_base += left;
2209 uiop->uio_iov->iov_len -= left;
2210 uiop->uio_offset += left;
2211 uiop->uio_resid -= left;
2214 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2217 dp = (struct dirent *)uiop->uio_iov->iov_base;
2218 dp->d_fileno = (int)fileno;
2220 dp->d_reclen = tlen + DIRHDSIZ;
2221 dp->d_type = DT_UNKNOWN;
2222 blksiz += dp->d_reclen;
2223 if (blksiz == DIRBLKSIZ)
2225 uiop->uio_offset += DIRHDSIZ;
2226 uiop->uio_resid -= DIRHDSIZ;
2227 uiop->uio_iov->iov_base += DIRHDSIZ;
2228 uiop->uio_iov->iov_len -= DIRHDSIZ;
2229 nfsm_mtouio(uiop, len);
2230 cp = uiop->uio_iov->iov_base;
2232 *cp = '\0'; /* null terminate */
2233 uiop->uio_iov->iov_base += tlen;
2234 uiop->uio_iov->iov_len -= tlen;
2235 uiop->uio_offset += tlen;
2236 uiop->uio_resid -= tlen;
2238 nfsm_adv(nfsm_rndup(len));
2240 nfsm_dissect(tl, u_int32_t *,
2243 nfsm_dissect(tl, u_int32_t *,
2247 cookie.nfsuquad[0] = *tl++;
2249 cookie.nfsuquad[1] = *tl++;
2254 more_dirs = fxdr_unsigned(int, *tl);
2257 * If at end of rpc data, get the eof boolean
2260 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2261 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2266 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2267 * by increasing d_reclen for the last record.
2270 left = DIRBLKSIZ - blksiz;
2271 dp->d_reclen += left;
2272 uiop->uio_iov->iov_base += left;
2273 uiop->uio_iov->iov_len -= left;
2274 uiop->uio_offset += left;
2275 uiop->uio_resid -= left;
2279 * We are now either at the end of the directory or have filled the
2283 dnp->n_direofoffset = uiop->uio_offset;
2285 if (uiop->uio_resid > 0)
2286 printf("EEK! readdirrpc resid > 0\n");
2287 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2295 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2298 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2305 struct vnode *newvp;
2307 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2308 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2309 struct nameidata nami, *ndp = &nami;
2310 struct componentname *cnp = &ndp->ni_cnd;
2312 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2313 struct nfsnode *dnp = VTONFS(vp), *np;
2316 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2317 int attrflag, fhsize;
2320 dp = (struct dirent *)0;
2323 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2324 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2325 panic("nfs readdirplusrpc bad uio");
2331 * If there is no cookie, assume directory was stale.
2333 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2337 return (NFSERR_BAD_COOKIE);
2339 * Loop around doing readdir rpc's of size nm_readdirsize
2340 * truncated to a multiple of DIRBLKSIZ.
2341 * The stopping criteria is EOF or buffer full.
2343 while (more_dirs && bigenough) {
2344 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2345 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2346 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2348 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2349 *tl++ = cookie.nfsuquad[0];
2350 *tl++ = cookie.nfsuquad[1];
2351 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2352 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2353 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2354 *tl = txdr_unsigned(nmp->nm_rsize);
2355 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2356 nfsm_postop_attr(vp, attrflag);
2361 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2362 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2363 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2364 more_dirs = fxdr_unsigned(int, *tl);
2366 /* loop thru the dir entries, doctoring them to 4bsd form */
2367 while (more_dirs && bigenough) {
2368 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2369 fileno = fxdr_hyper(tl);
2370 len = fxdr_unsigned(int, *(tl + 2));
2371 if (len <= 0 || len > NFS_MAXNAMLEN) {
2376 tlen = nfsm_rndup(len);
2378 tlen += 4; /* To ensure null termination*/
2379 left = DIRBLKSIZ - blksiz;
2380 if ((tlen + DIRHDSIZ) > left) {
2381 dp->d_reclen += left;
2382 uiop->uio_iov->iov_base += left;
2383 uiop->uio_iov->iov_len -= left;
2384 uiop->uio_offset += left;
2385 uiop->uio_resid -= left;
2388 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2391 dp = (struct dirent *)uiop->uio_iov->iov_base;
2392 dp->d_fileno = (int)fileno;
2394 dp->d_reclen = tlen + DIRHDSIZ;
2395 dp->d_type = DT_UNKNOWN;
2396 blksiz += dp->d_reclen;
2397 if (blksiz == DIRBLKSIZ)
2399 uiop->uio_offset += DIRHDSIZ;
2400 uiop->uio_resid -= DIRHDSIZ;
2401 uiop->uio_iov->iov_base += DIRHDSIZ;
2402 uiop->uio_iov->iov_len -= DIRHDSIZ;
2403 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2404 cnp->cn_namelen = len;
2405 nfsm_mtouio(uiop, len);
2406 cp = uiop->uio_iov->iov_base;
2409 uiop->uio_iov->iov_base += tlen;
2410 uiop->uio_iov->iov_len -= tlen;
2411 uiop->uio_offset += tlen;
2412 uiop->uio_resid -= tlen;
2414 nfsm_adv(nfsm_rndup(len));
2415 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2417 cookie.nfsuquad[0] = *tl++;
2418 cookie.nfsuquad[1] = *tl++;
2423 * Since the attributes are before the file handle
2424 * (sigh), we must skip over the attributes and then
2425 * come back and get them.
2427 attrflag = fxdr_unsigned(int, *tl);
2431 nfsm_adv(NFSX_V3FATTR);
2432 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2433 doit = fxdr_unsigned(int, *tl);
2435 nfsm_getfh(fhp, fhsize, 1);
2436 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2441 error = nfs_nget(vp->v_mount, fhp,
2449 if (doit && bigenough) {
2454 nfsm_loadattr(newvp, (struct vattr *)0);
2458 IFTODT(VTTOIF(np->n_vattr.va_type));
2460 cache_enter(ndp->ni_dvp, NCPNULL, ndp->ni_vp, cnp);
2463 /* Just skip over the file handle */
2464 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2465 i = fxdr_unsigned(int, *tl);
2466 nfsm_adv(nfsm_rndup(i));
2468 if (newvp != NULLVP) {
2475 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2476 more_dirs = fxdr_unsigned(int, *tl);
2479 * If at end of rpc data, get the eof boolean
2482 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2483 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2488 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2489 * by increasing d_reclen for the last record.
2492 left = DIRBLKSIZ - blksiz;
2493 dp->d_reclen += left;
2494 uiop->uio_iov->iov_base += left;
2495 uiop->uio_iov->iov_len -= left;
2496 uiop->uio_offset += left;
2497 uiop->uio_resid -= left;
2501 * We are now either at the end of the directory or have filled the
2505 dnp->n_direofoffset = uiop->uio_offset;
2507 if (uiop->uio_resid > 0)
2508 printf("EEK! readdirplusrpc resid > 0\n");
2509 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2513 if (newvp != NULLVP) {
2524 * Silly rename. To make the NFS filesystem that is stateless look a little
2525 * more like the "ufs" a remove of an active vnode is translated to a rename
2526 * to a funny looking filename that is removed by nfs_inactive on the
2527 * nfsnode. There is the potential for another process on a different client
2528 * to create the same funny name between the nfs_lookitup() fails and the
2529 * nfs_rename() completes, but...
2532 nfs_sillyrename(dvp, vp, cnp)
2533 struct vnode *dvp, *vp;
2534 struct componentname *cnp;
2536 struct sillyrename *sp;
2543 if (vp->v_type == VDIR)
2544 panic("nfs: sillyrename dir");
2546 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2547 M_NFSREQ, M_WAITOK);
2548 sp->s_cred = crdup(cnp->cn_cred);
2552 /* Fudge together a funny name */
2553 sp->s_namlen = sprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
2555 /* Try lookitups until we get one that isn't there */
2556 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2557 cnp->cn_td, (struct nfsnode **)0) == 0) {
2559 if (sp->s_name[4] > 'z') {
2564 error = nfs_renameit(dvp, cnp, sp);
2567 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2569 np->n_sillyrename = sp;
2574 free((caddr_t)sp, M_NFSREQ);
2579 * Look up a file name and optionally either update the file handle or
2580 * allocate an nfsnode, depending on the value of npp.
2581 * npp == NULL --> just do the lookup
2582 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2584 * *npp != NULL --> update the file handle in the vnode
2587 nfs_lookitup(dvp, name, len, cred, td, npp)
2593 struct nfsnode **npp;
2598 struct vnode *newvp = (struct vnode *)0;
2599 struct nfsnode *np, *dnp = VTONFS(dvp);
2600 caddr_t bpos, dpos, cp2;
2601 int error = 0, fhlen, attrflag;
2602 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2604 int v3 = NFS_ISV3(dvp);
2606 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2607 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2608 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2609 nfsm_fhtom(dvp, v3);
2610 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2611 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2612 if (npp && !error) {
2613 nfsm_getfh(nfhp, fhlen, v3);
2616 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2617 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2618 np->n_fhp = &np->n_fh;
2619 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2620 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2621 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2622 np->n_fhsize = fhlen;
2624 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2628 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2636 nfsm_postop_attr(newvp, attrflag);
2637 if (!attrflag && *npp == NULL) {
2646 nfsm_loadattr(newvp, (struct vattr *)0);
2650 if (npp && *npp == NULL) {
2665 * Nfs Version 3 commit rpc
2668 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2673 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2674 caddr_t bpos, dpos, cp2;
2675 int error = 0, wccflag = NFSV3_WCCRATTR;
2676 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2678 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2680 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2681 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2683 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2684 txdr_hyper(offset, tl);
2686 *tl = txdr_unsigned(cnt);
2687 nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2688 nfsm_wcc_data(vp, wccflag);
2690 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2691 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2692 NFSX_V3WRITEVERF)) {
2693 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2695 error = NFSERR_STALEWRITEVERF;
2705 * - make nfs_bmap() essentially a no-op that does no translation
2706 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2707 * (Maybe I could use the process's page mapping, but I was concerned that
2708 * Kernel Write might not be enabled and also figured copyout() would do
2709 * a lot more work than bcopy() and also it currently happens in the
2710 * context of the swapper process (2).
2714 struct vop_bmap_args /* {
2717 struct vnode **a_vpp;
2723 struct vnode *vp = ap->a_vp;
2725 if (ap->a_vpp != NULL)
2727 if (ap->a_bnp != NULL)
2728 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2729 if (ap->a_runp != NULL)
2731 if (ap->a_runb != NULL)
2738 * For async requests when nfsiod(s) are running, queue the request by
2739 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2744 struct vop_strategy_args *ap;
2746 struct buf *bp = ap->a_bp;
2750 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2751 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2753 if (bp->b_flags & B_PHYS)
2754 panic("nfs physio");
2756 if (bp->b_flags & B_ASYNC)
2759 td = curthread; /* XXX */
2762 * If the op is asynchronous and an i/o daemon is waiting
2763 * queue the request, wake it up and wait for completion
2764 * otherwise just do it ourselves.
2766 if ((bp->b_flags & B_ASYNC) == 0 ||
2767 nfs_asyncio(bp, td))
2768 error = nfs_doio(bp, td);
2775 * NB Currently unsupported.
2780 struct vop_mmap_args /* {
2783 struct ucred *a_cred;
2784 struct thread *a_td;
2792 * fsync vnode op. Just call nfs_flush() with commit == 1.
2797 struct vop_fsync_args /* {
2798 struct vnodeop_desc *a_desc;
2799 struct vnode * a_vp;
2800 struct ucred * a_cred;
2802 struct thread * a_td;
2806 return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2810 * Flush all the blocks associated with a vnode.
2811 * Walk through the buffer pool and push any dirty pages
2812 * associated with the vnode.
2815 nfs_flush(vp, waitfor, td, commit)
2821 struct nfsnode *np = VTONFS(vp);
2825 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2826 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2828 u_quad_t off, endoff, toff;
2829 struct buf **bvec = NULL;
2830 #ifndef NFS_COMMITBVECSIZ
2831 #define NFS_COMMITBVECSIZ 20
2833 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2834 int bvecsize = 0, bveccount;
2836 if (nmp->nm_flag & NFSMNT_INT)
2841 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2842 * server, but nas not been committed to stable storage on the server
2843 * yet. On the first pass, the byte range is worked out and the commit
2844 * rpc is done. On the second pass, nfs_writebp() is called to do the
2851 if (NFS_ISV3(vp) && commit) {
2854 * Count up how many buffers waiting for a commit.
2857 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2858 nbp = TAILQ_NEXT(bp, b_vnbufs);
2859 if (BUF_REFCNT(bp) == 0 &&
2860 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2861 == (B_DELWRI | B_NEEDCOMMIT))
2865 * Allocate space to remember the list of bufs to commit. It is
2866 * important to use M_NOWAIT here to avoid a race with nfs_write.
2867 * If we can't get memory (for whatever reason), we will end up
2868 * committing the buffers one-by-one in the loop below.
2870 if (bvec != NULL && bvec != bvec_on_stack)
2872 if (bveccount > NFS_COMMITBVECSIZ) {
2873 bvec = (struct buf **)
2874 malloc(bveccount * sizeof(struct buf *),
2877 bvec = bvec_on_stack;
2878 bvecsize = NFS_COMMITBVECSIZ;
2880 bvecsize = bveccount;
2882 bvec = bvec_on_stack;
2883 bvecsize = NFS_COMMITBVECSIZ;
2885 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2886 nbp = TAILQ_NEXT(bp, b_vnbufs);
2887 if (bvecpos >= bvecsize)
2889 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2890 (B_DELWRI | B_NEEDCOMMIT) ||
2891 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
2895 * NOTE: we are not clearing B_DONE here, so we have
2896 * to do it later on in this routine if we intend to
2897 * initiate I/O on the bp.
2899 * Note: to avoid loopback deadlocks, we do not
2900 * assign b_runningbufspace.
2902 bp->b_flags |= B_WRITEINPROG;
2903 vfs_busy_pages(bp, 1);
2906 * bp is protected by being locked, but nbp is not
2907 * and vfs_busy_pages() may sleep. We have to
2910 nbp = TAILQ_NEXT(bp, b_vnbufs);
2913 * A list of these buffers is kept so that the
2914 * second loop knows which buffers have actually
2915 * been committed. This is necessary, since there
2916 * may be a race between the commit rpc and new
2917 * uncommitted writes on the file.
2919 bvec[bvecpos++] = bp;
2920 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2924 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2932 * Commit data on the server, as required. Note that
2933 * nfs_commit will use the vnode's cred for the commit.
2935 retv = nfs_commit(vp, off, (int)(endoff - off), td);
2937 if (retv == NFSERR_STALEWRITEVERF)
2938 nfs_clearcommit(vp->v_mount);
2941 * Now, either mark the blocks I/O done or mark the
2942 * blocks dirty, depending on whether the commit
2945 for (i = 0; i < bvecpos; i++) {
2947 bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG | B_CLUSTEROK);
2950 * Error, leave B_DELWRI intact
2952 vfs_unbusy_pages(bp);
2956 * Success, remove B_DELWRI ( bundirty() ).
2958 * b_dirtyoff/b_dirtyend seem to be NFS
2959 * specific. We should probably move that
2960 * into bundirty(). XXX
2964 bp->b_flags |= B_ASYNC;
2966 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
2967 bp->b_dirtyoff = bp->b_dirtyend = 0;
2975 * Start/do any write(s) that are required.
2979 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2980 nbp = TAILQ_NEXT(bp, b_vnbufs);
2981 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
2982 if (waitfor != MNT_WAIT || passone)
2984 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL,
2985 "nfsfsync", slpflag, slptimeo);
2988 panic("nfs_fsync: inconsistent lock");
2989 if (error == ENOLCK)
2991 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
2995 if (slpflag == PCATCH) {
3001 if ((bp->b_flags & B_DELWRI) == 0)
3002 panic("nfs_fsync: not dirty");
3003 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3008 if (passone || !commit)
3009 bp->b_flags |= B_ASYNC;
3011 bp->b_flags |= B_ASYNC | B_WRITEINPROG;
3013 VOP_BWRITE(bp->b_vp, bp);
3021 if (waitfor == MNT_WAIT) {
3022 while (vp->v_numoutput) {
3023 vp->v_flag |= VBWAIT;
3024 error = tsleep((caddr_t)&vp->v_numoutput,
3025 slpflag, "nfsfsync", slptimeo);
3027 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
3031 if (slpflag == PCATCH) {
3037 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) && commit) {
3041 if (np->n_flag & NWRITEERR) {
3042 error = np->n_error;
3043 np->n_flag &= ~NWRITEERR;
3046 if (bvec != NULL && bvec != bvec_on_stack)
3052 * NFS advisory byte-level locks.
3053 * Currently unsupported.
3057 struct vop_advlock_args /* {
3065 struct nfsnode *np = VTONFS(ap->a_vp);
3068 * The following kludge is to allow diskless support to work
3069 * until a real NFS lockd is implemented. Basically, just pretend
3070 * that this is a local lock.
3072 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3076 * Print out the contents of an nfsnode.
3080 struct vop_print_args /* {
3084 struct vnode *vp = ap->a_vp;
3085 struct nfsnode *np = VTONFS(vp);
3087 printf("tag VT_NFS, fileid %ld fsid 0x%x",
3088 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3089 if (vp->v_type == VFIFO)
3096 * Just call nfs_writebp() with the force argument set to 1.
3098 * NOTE: B_DONE may or may not be set in a_bp on call.
3102 struct vop_bwrite_args /* {
3106 return (nfs_writebp(ap->a_bp, 1, curthread));
3110 * This is a clone of vn_bwrite(), except that B_WRITEINPROG isn't set unless
3111 * the force flag is one and it also handles the B_NEEDCOMMIT flag. We set
3112 * B_CACHE if this is a VMIO buffer.
3115 nfs_writebp(bp, force, td)
3121 int oldflags = bp->b_flags;
3127 if (BUF_REFCNT(bp) == 0)
3128 panic("bwrite: buffer is not locked???");
3130 if (bp->b_flags & B_INVAL) {
3135 bp->b_flags |= B_CACHE;
3138 * Undirty the bp. We will redirty it later if the I/O fails.
3143 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3145 bp->b_vp->v_numoutput++;
3149 * Note: to avoid loopback deadlocks, we do not
3150 * assign b_runningbufspace.
3152 vfs_busy_pages(bp, 1);
3155 bp->b_flags |= B_WRITEINPROG;
3157 VOP_STRATEGY(bp->b_vp, bp);
3159 if( (oldflags & B_ASYNC) == 0) {
3160 int rtval = biowait(bp);
3162 if (oldflags & B_DELWRI) {
3164 reassignbuf(bp, bp->b_vp);
3176 * nfs special file access vnode op.
3177 * Essentially just get vattr and then imitate iaccess() since the device is
3178 * local to the client.
3182 struct vop_access_args /* {
3185 struct ucred *a_cred;
3186 struct thread *a_td;
3191 struct ucred *cred = ap->a_cred;
3192 struct vnode *vp = ap->a_vp;
3193 mode_t mode = ap->a_mode;
3199 * Disallow write attempts on filesystems mounted read-only;
3200 * unless the file is a socket, fifo, or a block or character
3201 * device resident on the filesystem.
3203 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3204 switch (vp->v_type) {
3214 * If you're the super-user,
3215 * you always get access.
3217 if (cred->cr_uid == 0)
3220 error = VOP_GETATTR(vp, vap, ap->a_td);
3224 * Access check is based on only one of owner, group, public.
3225 * If not owner, then check group. If not a member of the
3226 * group, then check public access.
3228 if (cred->cr_uid != vap->va_uid) {
3230 gp = cred->cr_groups;
3231 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3232 if (vap->va_gid == *gp)
3238 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3243 * Read wrapper for special devices.
3247 struct vop_read_args /* {
3251 struct ucred *a_cred;
3254 struct nfsnode *np = VTONFS(ap->a_vp);
3260 getnanotime(&np->n_atim);
3261 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3265 * Write wrapper for special devices.
3269 struct vop_write_args /* {
3273 struct ucred *a_cred;
3276 struct nfsnode *np = VTONFS(ap->a_vp);
3282 getnanotime(&np->n_mtim);
3283 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3287 * Close wrapper for special devices.
3289 * Update the times on the nfsnode then do device close.
3293 struct vop_close_args /* {
3296 struct ucred *a_cred;
3297 struct thread *a_td;
3300 struct vnode *vp = ap->a_vp;
3301 struct nfsnode *np = VTONFS(vp);
3304 if (np->n_flag & (NACC | NUPD)) {
3306 if (vp->v_usecount == 1 &&
3307 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3309 if (np->n_flag & NACC)
3310 vattr.va_atime = np->n_atim;
3311 if (np->n_flag & NUPD)
3312 vattr.va_mtime = np->n_mtim;
3313 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3316 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3320 * Read wrapper for fifos.
3324 struct vop_read_args /* {
3328 struct ucred *a_cred;
3331 struct nfsnode *np = VTONFS(ap->a_vp);
3337 getnanotime(&np->n_atim);
3338 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3342 * Write wrapper for fifos.
3346 struct vop_write_args /* {
3350 struct ucred *a_cred;
3353 struct nfsnode *np = VTONFS(ap->a_vp);
3359 getnanotime(&np->n_mtim);
3360 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3364 * Close wrapper for fifos.
3366 * Update the times on the nfsnode then do fifo close.
3370 struct vop_close_args /* {
3373 struct thread *a_td;
3376 struct vnode *vp = ap->a_vp;
3377 struct nfsnode *np = VTONFS(vp);
3381 if (np->n_flag & (NACC | NUPD)) {
3383 if (np->n_flag & NACC)
3385 if (np->n_flag & NUPD)
3388 if (vp->v_usecount == 1 &&
3389 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3391 if (np->n_flag & NACC)
3392 vattr.va_atime = np->n_atim;
3393 if (np->n_flag & NUPD)
3394 vattr.va_mtime = np->n_mtim;
3395 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3398 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));