Add vfs.nfs.flush_on_hlink and default to off.
[dragonfly.git] / sys / vfs / nfs / nfs_vnops.c
blob5434c7fb52cb9e00a86f7e5b4c086dc1b61f2ab6
1 /*
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
10 * are met:
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
34 * SUCH DAMAGE.
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.80 2008/10/18 01:13:54 dillon Exp $
43 * vnode op calls for Sun NFS version 2 and 3
46 #include "opt_inet.h"
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/resourcevar.h>
52 #include <sys/proc.h>
53 #include <sys/mount.h>
54 #include <sys/buf.h>
55 #include <sys/malloc.h>
56 #include <sys/mbuf.h>
57 #include <sys/namei.h>
58 #include <sys/nlookup.h>
59 #include <sys/socket.h>
60 #include <sys/vnode.h>
61 #include <sys/dirent.h>
62 #include <sys/fcntl.h>
63 #include <sys/lockf.h>
64 #include <sys/stat.h>
65 #include <sys/sysctl.h>
66 #include <sys/conf.h>
68 #include <vm/vm.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_zone.h>
72 #include <sys/buf2.h>
74 #include <vfs/fifofs/fifo.h>
75 #include <vfs/ufs/dir.h>
77 #undef DIRBLKSIZ
79 #include "rpcv2.h"
80 #include "nfsproto.h"
81 #include "nfs.h"
82 #include "nfsmount.h"
83 #include "nfsnode.h"
84 #include "xdr_subs.h"
85 #include "nfsm_subs.h"
87 #include <net/if.h>
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
91 #include <sys/thread2.h>
93 /* Defs */
94 #define TRUE 1
95 #define FALSE 0
97 static int nfsspec_read (struct vop_read_args *);
98 static int nfsspec_write (struct vop_write_args *);
99 static int nfsfifo_read (struct vop_read_args *);
100 static int nfsfifo_write (struct vop_write_args *);
101 static int nfsspec_close (struct vop_close_args *);
102 static int nfsfifo_close (struct vop_close_args *);
103 #define nfs_poll vop_nopoll
104 static int nfs_setattrrpc (struct vnode *,struct vattr *,struct ucred *,struct thread *);
105 static int nfs_lookup (struct vop_old_lookup_args *);
106 static int nfs_create (struct vop_old_create_args *);
107 static int nfs_mknod (struct vop_old_mknod_args *);
108 static int nfs_open (struct vop_open_args *);
109 static int nfs_close (struct vop_close_args *);
110 static int nfs_access (struct vop_access_args *);
111 static int nfs_getattr (struct vop_getattr_args *);
112 static int nfs_setattr (struct vop_setattr_args *);
113 static int nfs_read (struct vop_read_args *);
114 static int nfs_mmap (struct vop_mmap_args *);
115 static int nfs_fsync (struct vop_fsync_args *);
116 static int nfs_remove (struct vop_old_remove_args *);
117 static int nfs_link (struct vop_old_link_args *);
118 static int nfs_rename (struct vop_old_rename_args *);
119 static int nfs_mkdir (struct vop_old_mkdir_args *);
120 static int nfs_rmdir (struct vop_old_rmdir_args *);
121 static int nfs_symlink (struct vop_old_symlink_args *);
122 static int nfs_readdir (struct vop_readdir_args *);
123 static int nfs_bmap (struct vop_bmap_args *);
124 static int nfs_strategy (struct vop_strategy_args *);
125 static int nfs_lookitup (struct vnode *, const char *, int,
126 struct ucred *, struct thread *, struct nfsnode **);
127 static int nfs_sillyrename (struct vnode *,struct vnode *,struct componentname *);
128 static int nfsspec_access (struct vop_access_args *);
129 static int nfs_readlink (struct vop_readlink_args *);
130 static int nfs_print (struct vop_print_args *);
131 static int nfs_advlock (struct vop_advlock_args *);
133 static int nfs_nresolve (struct vop_nresolve_args *);
135 * Global vfs data structures for nfs
137 struct vop_ops nfsv2_vnode_vops = {
138 .vop_default = vop_defaultop,
139 .vop_access = nfs_access,
140 .vop_advlock = nfs_advlock,
141 .vop_bmap = nfs_bmap,
142 .vop_close = nfs_close,
143 .vop_old_create = nfs_create,
144 .vop_fsync = nfs_fsync,
145 .vop_getattr = nfs_getattr,
146 .vop_getpages = nfs_getpages,
147 .vop_putpages = nfs_putpages,
148 .vop_inactive = nfs_inactive,
149 .vop_old_link = nfs_link,
150 .vop_old_lookup = nfs_lookup,
151 .vop_old_mkdir = nfs_mkdir,
152 .vop_old_mknod = nfs_mknod,
153 .vop_mmap = nfs_mmap,
154 .vop_open = nfs_open,
155 .vop_poll = nfs_poll,
156 .vop_print = nfs_print,
157 .vop_read = nfs_read,
158 .vop_readdir = nfs_readdir,
159 .vop_readlink = nfs_readlink,
160 .vop_reclaim = nfs_reclaim,
161 .vop_old_remove = nfs_remove,
162 .vop_old_rename = nfs_rename,
163 .vop_old_rmdir = nfs_rmdir,
164 .vop_setattr = nfs_setattr,
165 .vop_strategy = nfs_strategy,
166 .vop_old_symlink = nfs_symlink,
167 .vop_write = nfs_write,
168 .vop_nresolve = nfs_nresolve
172 * Special device vnode ops
174 struct vop_ops nfsv2_spec_vops = {
175 .vop_default = spec_vnoperate,
176 .vop_access = nfsspec_access,
177 .vop_close = nfsspec_close,
178 .vop_fsync = nfs_fsync,
179 .vop_getattr = nfs_getattr,
180 .vop_inactive = nfs_inactive,
181 .vop_print = nfs_print,
182 .vop_read = nfsspec_read,
183 .vop_reclaim = nfs_reclaim,
184 .vop_setattr = nfs_setattr,
185 .vop_write = nfsspec_write
188 struct vop_ops nfsv2_fifo_vops = {
189 .vop_default = fifo_vnoperate,
190 .vop_access = nfsspec_access,
191 .vop_close = nfsfifo_close,
192 .vop_fsync = nfs_fsync,
193 .vop_getattr = nfs_getattr,
194 .vop_inactive = nfs_inactive,
195 .vop_print = nfs_print,
196 .vop_read = nfsfifo_read,
197 .vop_reclaim = nfs_reclaim,
198 .vop_setattr = nfs_setattr,
199 .vop_write = nfsfifo_write
202 static int nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
203 struct componentname *cnp,
204 struct vattr *vap);
205 static int nfs_removerpc (struct vnode *dvp, const char *name,
206 int namelen,
207 struct ucred *cred, struct thread *td);
208 static int nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
209 int fnamelen, struct vnode *tdvp,
210 const char *tnameptr, int tnamelen,
211 struct ucred *cred, struct thread *td);
212 static int nfs_renameit (struct vnode *sdvp,
213 struct componentname *scnp,
214 struct sillyrename *sp);
217 * Global variables
219 extern u_int32_t nfs_true, nfs_false;
220 extern u_int32_t nfs_xdrneg1;
221 extern struct nfsstats nfsstats;
222 extern nfstype nfsv3_type[9];
223 struct thread *nfs_iodwant[NFS_MAXASYNCDAEMON];
224 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
225 int nfs_numasync = 0;
227 SYSCTL_DECL(_vfs_nfs);
229 static int nfs_flush_on_rename = 1;
230 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_rename, CTLFLAG_RW,
231 &nfs_flush_on_rename, 0, "flush fvp prior to rename");
232 static int nfs_flush_on_hlink = 0;
233 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_hlink, CTLFLAG_RW,
234 &nfs_flush_on_hlink, 0, "flush fvp prior to hard link");
236 static int nfsaccess_cache_timeout = NFS_DEFATTRTIMO;
237 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
238 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
240 static int nfsneg_cache_timeout = NFS_MINATTRTIMO;
241 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
242 &nfsneg_cache_timeout, 0, "NFS NEGATIVE NAMECACHE timeout");
244 static int nfspos_cache_timeout = NFS_MINATTRTIMO;
245 SYSCTL_INT(_vfs_nfs, OID_AUTO, pos_cache_timeout, CTLFLAG_RW,
246 &nfspos_cache_timeout, 0, "NFS POSITIVE NAMECACHE timeout");
248 static int nfsv3_commit_on_close = 0;
249 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
250 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
251 #if 0
252 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
253 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
255 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
256 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
257 #endif
259 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
260 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
261 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
262 static int
263 nfs3_access_otw(struct vnode *vp, int wmode,
264 struct thread *td, struct ucred *cred)
266 const int v3 = 1;
267 u_int32_t *tl;
268 int error = 0, attrflag;
270 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
271 caddr_t bpos, dpos, cp2;
272 int32_t t1, t2;
273 caddr_t cp;
274 u_int32_t rmode;
275 struct nfsnode *np = VTONFS(vp);
277 nfsstats.rpccnt[NFSPROC_ACCESS]++;
278 nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
279 nfsm_fhtom(vp, v3);
280 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
281 *tl = txdr_unsigned(wmode);
282 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
283 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
284 if (!error) {
285 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
286 rmode = fxdr_unsigned(u_int32_t, *tl);
287 np->n_mode = rmode;
288 np->n_modeuid = cred->cr_uid;
289 np->n_modestamp = mycpu->gd_time_seconds;
291 m_freem(mrep);
292 nfsmout:
293 return error;
297 * nfs access vnode op.
298 * For nfs version 2, just return ok. File accesses may fail later.
299 * For nfs version 3, use the access rpc to check accessibility. If file modes
300 * are changed on the server, accesses might still fail later.
302 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
304 static int
305 nfs_access(struct vop_access_args *ap)
307 struct vnode *vp = ap->a_vp;
308 thread_t td = curthread;
309 int error = 0;
310 u_int32_t mode, wmode;
311 int v3 = NFS_ISV3(vp);
312 struct nfsnode *np = VTONFS(vp);
315 * Disallow write attempts on filesystems mounted read-only;
316 * unless the file is a socket, fifo, or a block or character
317 * device resident on the filesystem.
319 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
320 switch (vp->v_type) {
321 case VREG:
322 case VDIR:
323 case VLNK:
324 return (EROFS);
325 default:
326 break;
330 * For nfs v3, check to see if we have done this recently, and if
331 * so return our cached result instead of making an ACCESS call.
332 * If not, do an access rpc, otherwise you are stuck emulating
333 * ufs_access() locally using the vattr. This may not be correct,
334 * since the server may apply other access criteria such as
335 * client uid-->server uid mapping that we do not know about.
337 if (v3) {
338 if (ap->a_mode & VREAD)
339 mode = NFSV3ACCESS_READ;
340 else
341 mode = 0;
342 if (vp->v_type != VDIR) {
343 if (ap->a_mode & VWRITE)
344 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
345 if (ap->a_mode & VEXEC)
346 mode |= NFSV3ACCESS_EXECUTE;
347 } else {
348 if (ap->a_mode & VWRITE)
349 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
350 NFSV3ACCESS_DELETE);
351 if (ap->a_mode & VEXEC)
352 mode |= NFSV3ACCESS_LOOKUP;
354 /* XXX safety belt, only make blanket request if caching */
355 if (nfsaccess_cache_timeout > 0) {
356 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
357 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
358 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
359 } else {
360 wmode = mode;
364 * Does our cached result allow us to give a definite yes to
365 * this request?
367 if (np->n_modestamp &&
368 (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
369 (ap->a_cred->cr_uid == np->n_modeuid) &&
370 ((np->n_mode & mode) == mode)) {
371 nfsstats.accesscache_hits++;
372 } else {
374 * Either a no, or a don't know. Go to the wire.
376 nfsstats.accesscache_misses++;
377 error = nfs3_access_otw(vp, wmode, td, ap->a_cred);
378 if (!error) {
379 if ((np->n_mode & mode) != mode) {
380 error = EACCES;
384 } else {
385 if ((error = nfsspec_access(ap)) != 0)
386 return (error);
389 * Attempt to prevent a mapped root from accessing a file
390 * which it shouldn't. We try to read a byte from the file
391 * if the user is root and the file is not zero length.
392 * After calling nfsspec_access, we should have the correct
393 * file size cached.
395 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
396 && VTONFS(vp)->n_size > 0) {
397 struct iovec aiov;
398 struct uio auio;
399 char buf[1];
401 aiov.iov_base = buf;
402 aiov.iov_len = 1;
403 auio.uio_iov = &aiov;
404 auio.uio_iovcnt = 1;
405 auio.uio_offset = 0;
406 auio.uio_resid = 1;
407 auio.uio_segflg = UIO_SYSSPACE;
408 auio.uio_rw = UIO_READ;
409 auio.uio_td = td;
411 if (vp->v_type == VREG) {
412 error = nfs_readrpc(vp, &auio);
413 } else if (vp->v_type == VDIR) {
414 char* bp;
415 bp = kmalloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
416 aiov.iov_base = bp;
417 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
418 error = nfs_readdirrpc(vp, &auio);
419 kfree(bp, M_TEMP);
420 } else if (vp->v_type == VLNK) {
421 error = nfs_readlinkrpc(vp, &auio);
422 } else {
423 error = EACCES;
428 * [re]record creds for reading and/or writing if access
429 * was granted. Assume the NFS server will grant read access
430 * for execute requests.
432 if (error == 0) {
433 if ((ap->a_mode & (VREAD|VEXEC)) && ap->a_cred != np->n_rucred) {
434 crhold(ap->a_cred);
435 if (np->n_rucred)
436 crfree(np->n_rucred);
437 np->n_rucred = ap->a_cred;
439 if ((ap->a_mode & VWRITE) && ap->a_cred != np->n_wucred) {
440 crhold(ap->a_cred);
441 if (np->n_wucred)
442 crfree(np->n_wucred);
443 np->n_wucred = ap->a_cred;
446 return(error);
450 * nfs open vnode op
451 * Check to see if the type is ok
452 * and that deletion is not in progress.
453 * For paged in text files, you will need to flush the page cache
454 * if consistency is lost.
456 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
457 * struct file *a_fp)
459 /* ARGSUSED */
460 static int
461 nfs_open(struct vop_open_args *ap)
463 struct vnode *vp = ap->a_vp;
464 struct nfsnode *np = VTONFS(vp);
465 struct vattr vattr;
466 int error;
468 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
469 #ifdef DIAGNOSTIC
470 kprintf("open eacces vtyp=%d\n",vp->v_type);
471 #endif
472 return (EOPNOTSUPP);
476 * Clear the attribute cache only if opening with write access. It
477 * is unclear if we should do this at all here, but we certainly
478 * should not clear the cache unconditionally simply because a file
479 * is being opened.
481 if (ap->a_mode & FWRITE)
482 np->n_attrstamp = 0;
485 * For normal NFS, reconcile changes made locally verses
486 * changes made remotely. Note that VOP_GETATTR only goes
487 * to the wire if the cached attribute has timed out or been
488 * cleared.
490 * If local modifications have been made clear the attribute
491 * cache to force an attribute and modified time check. If
492 * GETATTR detects that the file has been changed by someone
493 * other then us it will set NRMODIFIED.
495 * If we are opening a directory and local changes have been
496 * made we have to invalidate the cache in order to ensure
497 * that we get the most up-to-date information from the
498 * server. XXX
500 if (np->n_flag & NLMODIFIED) {
501 np->n_attrstamp = 0;
502 if (vp->v_type == VDIR) {
503 error = nfs_vinvalbuf(vp, V_SAVE, 1);
504 if (error == EINTR)
505 return (error);
506 nfs_invaldir(vp);
509 error = VOP_GETATTR(vp, &vattr);
510 if (error)
511 return (error);
512 if (np->n_flag & NRMODIFIED) {
513 if (vp->v_type == VDIR)
514 nfs_invaldir(vp);
515 error = nfs_vinvalbuf(vp, V_SAVE, 1);
516 if (error == EINTR)
517 return (error);
518 np->n_flag &= ~NRMODIFIED;
521 return (vop_stdopen(ap));
525 * nfs close vnode op
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
528 * two reasons:
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.
554 * nfs_close(struct vnode *a_vp, int a_fflag)
556 /* ARGSUSED */
557 static int
558 nfs_close(struct vop_close_args *ap)
560 struct vnode *vp = ap->a_vp;
561 struct nfsnode *np = VTONFS(vp);
562 int error = 0;
563 thread_t td = curthread;
565 if (vp->v_type == VREG) {
566 if (np->n_flag & NLMODIFIED) {
567 if (NFS_ISV3(vp)) {
569 * Under NFSv3 we have dirty buffers to dispose of. We
570 * must flush them to the NFS server. We have the option
571 * of waiting all the way through the commit rpc or just
572 * waiting for the initial write. The default is to only
573 * wait through the initial write so the data is in the
574 * server's cache, which is roughly similar to the state
575 * a standard disk subsystem leaves the file in on close().
577 * We cannot clear the NLMODIFIED bit in np->n_flag due to
578 * potential races with other processes, and certainly
579 * cannot clear it if we don't commit.
581 int cm = nfsv3_commit_on_close ? 1 : 0;
582 error = nfs_flush(vp, MNT_WAIT, td, cm);
583 /* np->n_flag &= ~NLMODIFIED; */
584 } else {
585 error = nfs_vinvalbuf(vp, V_SAVE, 1);
587 np->n_attrstamp = 0;
589 if (np->n_flag & NWRITEERR) {
590 np->n_flag &= ~NWRITEERR;
591 error = np->n_error;
594 vop_stdclose(ap);
595 return (error);
599 * nfs getattr call from vfs.
601 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
603 static int
604 nfs_getattr(struct vop_getattr_args *ap)
606 struct vnode *vp = ap->a_vp;
607 struct nfsnode *np = VTONFS(vp);
608 caddr_t cp;
609 u_int32_t *tl;
610 int32_t t1, t2;
611 caddr_t bpos, dpos;
612 int error = 0;
613 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
614 int v3 = NFS_ISV3(vp);
615 thread_t td = curthread;
618 * Update local times for special files.
620 if (np->n_flag & (NACC | NUPD))
621 np->n_flag |= NCHG;
623 * First look in the cache.
625 if (nfs_getattrcache(vp, ap->a_vap) == 0)
626 return (0);
628 if (v3 && nfsaccess_cache_timeout > 0) {
629 nfsstats.accesscache_misses++;
630 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, nfs_vpcred(vp, ND_CHECK));
631 if (nfs_getattrcache(vp, ap->a_vap) == 0)
632 return (0);
635 nfsstats.rpccnt[NFSPROC_GETATTR]++;
636 nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
637 nfsm_fhtom(vp, v3);
638 nfsm_request(vp, NFSPROC_GETATTR, td, nfs_vpcred(vp, ND_CHECK));
639 if (!error) {
640 nfsm_loadattr(vp, ap->a_vap);
642 m_freem(mrep);
643 nfsmout:
644 return (error);
648 * nfs setattr call.
650 * nfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
652 static int
653 nfs_setattr(struct vop_setattr_args *ap)
655 struct vnode *vp = ap->a_vp;
656 struct nfsnode *np = VTONFS(vp);
657 struct vattr *vap = ap->a_vap;
658 int error = 0;
659 u_quad_t tsize;
660 thread_t td = curthread;
662 #ifndef nolint
663 tsize = (u_quad_t)0;
664 #endif
667 * Setting of flags is not supported.
669 if (vap->va_flags != VNOVAL)
670 return (EOPNOTSUPP);
673 * Disallow write attempts if the filesystem is mounted read-only.
675 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
676 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
677 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
678 (vp->v_mount->mnt_flag & MNT_RDONLY))
679 return (EROFS);
681 if (vap->va_size != VNOVAL) {
683 * truncation requested
685 switch (vp->v_type) {
686 case VDIR:
687 return (EISDIR);
688 case VCHR:
689 case VBLK:
690 case VSOCK:
691 case VFIFO:
692 if (vap->va_mtime.tv_sec == VNOVAL &&
693 vap->va_atime.tv_sec == VNOVAL &&
694 vap->va_mode == (mode_t)VNOVAL &&
695 vap->va_uid == (uid_t)VNOVAL &&
696 vap->va_gid == (gid_t)VNOVAL)
697 return (0);
698 vap->va_size = VNOVAL;
699 break;
700 default:
702 * Disallow write attempts if the filesystem is
703 * mounted read-only.
705 if (vp->v_mount->mnt_flag & MNT_RDONLY)
706 return (EROFS);
709 * This is nasty. The RPCs we send to flush pending
710 * data often return attribute information which is
711 * cached via a callback to nfs_loadattrcache(), which
712 * has the effect of changing our notion of the file
713 * size. Due to flushed appends and other operations
714 * the file size can be set to virtually anything,
715 * including values that do not match either the old
716 * or intended file size.
718 * When this condition is detected we must loop to
719 * try the operation again. Hopefully no more
720 * flushing is required on the loop so it works the
721 * second time around. THIS CASE ALMOST ALWAYS
722 * HAPPENS!
724 tsize = np->n_size;
725 again:
726 error = nfs_meta_setsize(vp, td, vap->va_size);
728 if (np->n_flag & NLMODIFIED) {
729 if (vap->va_size == 0)
730 error = nfs_vinvalbuf(vp, 0, 1);
731 else
732 error = nfs_vinvalbuf(vp, V_SAVE, 1);
735 * note: this loop case almost always happens at
736 * least once per truncation.
738 if (error == 0 && np->n_size != vap->va_size)
739 goto again;
740 np->n_vattr.va_size = vap->va_size;
741 break;
743 } else if ((np->n_flag & NLMODIFIED) && vp->v_type == VREG) {
745 * What to do. If we are modifying the mtime we lose
746 * mtime detection of changes made by the server or other
747 * clients. But programs like rsync/rdist/cpdup are going
748 * to call utimes a lot. We don't want to piecemeal sync.
750 * For now sync if any prior remote changes were detected,
751 * but allow us to lose track of remote changes made during
752 * the utimes operation.
754 if (np->n_flag & NRMODIFIED)
755 error = nfs_vinvalbuf(vp, V_SAVE, 1);
756 if (error == EINTR)
757 return (error);
758 if (error == 0) {
759 if (vap->va_mtime.tv_sec != VNOVAL) {
760 np->n_mtime = vap->va_mtime.tv_sec;
764 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
767 * Sanity check if a truncation was issued. This should only occur
768 * if multiple processes are racing on the same file.
770 if (error == 0 && vap->va_size != VNOVAL &&
771 np->n_size != vap->va_size) {
772 kprintf("NFS ftruncate: server disagrees on the file size: %lld/%lld/%lld\n", tsize, vap->va_size, np->n_size);
773 goto again;
775 if (error && vap->va_size != VNOVAL) {
776 np->n_size = np->n_vattr.va_size = tsize;
777 vnode_pager_setsize(vp, np->n_size);
779 return (error);
783 * Do an nfs setattr rpc.
785 static int
786 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
787 struct ucred *cred, struct thread *td)
789 struct nfsv2_sattr *sp;
790 struct nfsnode *np = VTONFS(vp);
791 caddr_t cp;
792 int32_t t1, t2;
793 caddr_t bpos, dpos, cp2;
794 u_int32_t *tl;
795 int error = 0, wccflag = NFSV3_WCCRATTR;
796 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
797 int v3 = NFS_ISV3(vp);
799 nfsstats.rpccnt[NFSPROC_SETATTR]++;
800 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
801 nfsm_fhtom(vp, v3);
802 if (v3) {
803 nfsm_v3attrbuild(vap, TRUE);
804 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
805 *tl = nfs_false;
806 } else {
807 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
808 if (vap->va_mode == (mode_t)VNOVAL)
809 sp->sa_mode = nfs_xdrneg1;
810 else
811 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
812 if (vap->va_uid == (uid_t)VNOVAL)
813 sp->sa_uid = nfs_xdrneg1;
814 else
815 sp->sa_uid = txdr_unsigned(vap->va_uid);
816 if (vap->va_gid == (gid_t)VNOVAL)
817 sp->sa_gid = nfs_xdrneg1;
818 else
819 sp->sa_gid = txdr_unsigned(vap->va_gid);
820 sp->sa_size = txdr_unsigned(vap->va_size);
821 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
822 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
824 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
825 if (v3) {
826 np->n_modestamp = 0;
827 nfsm_wcc_data(vp, wccflag);
828 } else
829 nfsm_loadattr(vp, (struct vattr *)0);
830 m_freem(mrep);
831 nfsmout:
832 return (error);
835 static
836 void
837 nfs_cache_setvp(struct nchandle *nch, struct vnode *vp, int nctimeout)
839 if (nctimeout == 0)
840 nctimeout = 1;
841 else
842 nctimeout *= hz;
843 cache_setvp(nch, vp);
844 cache_settimeout(nch, nctimeout);
848 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
849 * nfs_lookup() until all remaining new api calls are implemented.
851 * Resolve a namecache entry. This function is passed a locked ncp and
852 * must call nfs_cache_setvp() on it as appropriate to resolve the entry.
854 static int
855 nfs_nresolve(struct vop_nresolve_args *ap)
857 struct thread *td = curthread;
858 struct namecache *ncp;
859 struct ucred *cred;
860 struct nfsnode *np;
861 struct vnode *dvp;
862 struct vnode *nvp;
863 nfsfh_t *fhp;
864 int attrflag;
865 int fhsize;
866 int error;
867 int len;
868 int v3;
869 /******NFSM MACROS********/
870 struct mbuf *mb, *mrep, *mreq, *mb2, *md;
871 caddr_t bpos, dpos, cp, cp2;
872 u_int32_t *tl;
873 int32_t t1, t2;
875 cred = ap->a_cred;
876 dvp = ap->a_dvp;
878 if ((error = vget(dvp, LK_SHARED)) != 0)
879 return (error);
881 nvp = NULL;
882 v3 = NFS_ISV3(dvp);
883 nfsstats.lookupcache_misses++;
884 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
885 ncp = ap->a_nch->ncp;
886 len = ncp->nc_nlen;
887 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
888 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
889 nfsm_fhtom(dvp, v3);
890 nfsm_strtom(ncp->nc_name, len, NFS_MAXNAMLEN);
891 nfsm_request(dvp, NFSPROC_LOOKUP, td, ap->a_cred);
892 if (error) {
894 * Cache negatve lookups to reduce NFS traffic, but use
895 * a fast timeout. Otherwise use a timeout of 1 tick.
896 * XXX we should add a namecache flag for no-caching
897 * to uncache the negative hit as soon as possible, but
898 * we cannot simply destroy the entry because it is used
899 * as a placeholder by the caller.
901 if (error == ENOENT)
902 nfs_cache_setvp(ap->a_nch, NULL, nfsneg_cache_timeout);
903 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
904 m_freem(mrep);
905 goto nfsmout;
909 * Success, get the file handle, do various checks, and load
910 * post-operation data from the reply packet. Theoretically
911 * we should never be looking up "." so, theoretically, we
912 * should never get the same file handle as our directory. But
913 * we check anyway. XXX
915 * Note that no timeout is set for the positive cache hit. We
916 * assume, theoretically, that ESTALE returns will be dealt with
917 * properly to handle NFS races and in anycase we cannot depend
918 * on a timeout to deal with NFS open/create/excl issues so instead
919 * of a bad hack here the rest of the NFS client code needs to do
920 * the right thing.
922 nfsm_getfh(fhp, fhsize, v3);
924 np = VTONFS(dvp);
925 if (NFS_CMPFH(np, fhp, fhsize)) {
926 vref(dvp);
927 nvp = dvp;
928 } else {
929 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
930 if (error) {
931 m_freem(mrep);
932 vput(dvp);
933 return (error);
935 nvp = NFSTOV(np);
937 if (v3) {
938 nfsm_postop_attr(nvp, attrflag, NFS_LATTR_NOSHRINK);
939 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
940 } else {
941 nfsm_loadattr(nvp, NULL);
943 nfs_cache_setvp(ap->a_nch, nvp, nfspos_cache_timeout);
944 m_freem(mrep);
945 nfsmout:
946 vput(dvp);
947 if (nvp) {
948 if (nvp == dvp)
949 vrele(nvp);
950 else
951 vput(nvp);
953 return (error);
957 * 'cached' nfs directory lookup
959 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
961 * nfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
962 * struct componentname *a_cnp)
964 static int
965 nfs_lookup(struct vop_old_lookup_args *ap)
967 struct componentname *cnp = ap->a_cnp;
968 struct vnode *dvp = ap->a_dvp;
969 struct vnode **vpp = ap->a_vpp;
970 int flags = cnp->cn_flags;
971 struct vnode *newvp;
972 u_int32_t *tl;
973 caddr_t cp;
974 int32_t t1, t2;
975 struct nfsmount *nmp;
976 caddr_t bpos, dpos, cp2;
977 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
978 long len;
979 nfsfh_t *fhp;
980 struct nfsnode *np;
981 int lockparent, wantparent, error = 0, attrflag, fhsize;
982 int v3 = NFS_ISV3(dvp);
985 * Read-only mount check and directory check.
987 *vpp = NULLVP;
988 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
989 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
990 return (EROFS);
992 if (dvp->v_type != VDIR)
993 return (ENOTDIR);
996 * Look it up in the cache. Note that ENOENT is only returned if we
997 * previously entered a negative hit (see later on). The additional
998 * nfsneg_cache_timeout check causes previously cached results to
999 * be instantly ignored if the negative caching is turned off.
1001 lockparent = flags & CNP_LOCKPARENT;
1002 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1003 nmp = VFSTONFS(dvp->v_mount);
1004 np = VTONFS(dvp);
1007 * Go to the wire.
1009 error = 0;
1010 newvp = NULLVP;
1011 nfsstats.lookupcache_misses++;
1012 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1013 len = cnp->cn_namelen;
1014 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
1015 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1016 nfsm_fhtom(dvp, v3);
1017 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1018 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
1019 if (error) {
1020 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1021 m_freem(mrep);
1022 goto nfsmout;
1024 nfsm_getfh(fhp, fhsize, v3);
1027 * Handle RENAME case...
1029 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1030 if (NFS_CMPFH(np, fhp, fhsize)) {
1031 m_freem(mrep);
1032 return (EISDIR);
1034 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1035 if (error) {
1036 m_freem(mrep);
1037 return (error);
1039 newvp = NFSTOV(np);
1040 if (v3) {
1041 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1042 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1043 } else
1044 nfsm_loadattr(newvp, (struct vattr *)0);
1045 *vpp = newvp;
1046 m_freem(mrep);
1047 if (!lockparent) {
1048 vn_unlock(dvp);
1049 cnp->cn_flags |= CNP_PDIRUNLOCK;
1051 return (0);
1054 if (flags & CNP_ISDOTDOT) {
1055 vn_unlock(dvp);
1056 cnp->cn_flags |= CNP_PDIRUNLOCK;
1057 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1058 if (error) {
1059 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1060 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1061 return (error); /* NOTE: return error from nget */
1063 newvp = NFSTOV(np);
1064 if (lockparent) {
1065 error = vn_lock(dvp, LK_EXCLUSIVE);
1066 if (error) {
1067 vput(newvp);
1068 return (error);
1070 cnp->cn_flags |= CNP_PDIRUNLOCK;
1072 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1073 vref(dvp);
1074 newvp = dvp;
1075 } else {
1076 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1077 if (error) {
1078 m_freem(mrep);
1079 return (error);
1081 if (!lockparent) {
1082 vn_unlock(dvp);
1083 cnp->cn_flags |= CNP_PDIRUNLOCK;
1085 newvp = NFSTOV(np);
1087 if (v3) {
1088 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1089 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1090 } else
1091 nfsm_loadattr(newvp, (struct vattr *)0);
1092 #if 0
1093 /* XXX MOVE TO nfs_nremove() */
1094 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1095 cnp->cn_nameiop != NAMEI_DELETE) {
1096 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1098 #endif
1099 *vpp = newvp;
1100 m_freem(mrep);
1101 nfsmout:
1102 if (error) {
1103 if (newvp != NULLVP) {
1104 vrele(newvp);
1105 *vpp = NULLVP;
1107 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1108 cnp->cn_nameiop == NAMEI_RENAME) &&
1109 error == ENOENT) {
1110 if (!lockparent) {
1111 vn_unlock(dvp);
1112 cnp->cn_flags |= CNP_PDIRUNLOCK;
1114 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1115 error = EROFS;
1116 else
1117 error = EJUSTRETURN;
1120 return (error);
1124 * nfs read call.
1125 * Just call nfs_bioread() to do the work.
1127 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1128 * struct ucred *a_cred)
1130 static int
1131 nfs_read(struct vop_read_args *ap)
1133 struct vnode *vp = ap->a_vp;
1135 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1136 switch (vp->v_type) {
1137 case VREG:
1138 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1139 case VDIR:
1140 return (EISDIR);
1141 default:
1142 return EOPNOTSUPP;
1147 * nfs readlink call
1149 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1151 static int
1152 nfs_readlink(struct vop_readlink_args *ap)
1154 struct vnode *vp = ap->a_vp;
1156 if (vp->v_type != VLNK)
1157 return (EINVAL);
1158 return (nfs_bioread(vp, ap->a_uio, 0));
1162 * Do a readlink rpc.
1163 * Called by nfs_doio() from below the buffer cache.
1166 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1168 u_int32_t *tl;
1169 caddr_t cp;
1170 int32_t t1, t2;
1171 caddr_t bpos, dpos, cp2;
1172 int error = 0, len, attrflag;
1173 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1174 int v3 = NFS_ISV3(vp);
1176 nfsstats.rpccnt[NFSPROC_READLINK]++;
1177 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1178 nfsm_fhtom(vp, v3);
1179 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1180 if (v3)
1181 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1182 if (!error) {
1183 nfsm_strsiz(len, NFS_MAXPATHLEN);
1184 if (len == NFS_MAXPATHLEN) {
1185 struct nfsnode *np = VTONFS(vp);
1186 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1187 len = np->n_size;
1189 nfsm_mtouio(uiop, len);
1191 m_freem(mrep);
1192 nfsmout:
1193 return (error);
1197 * nfs read rpc call
1198 * Ditto above
1201 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1203 u_int32_t *tl;
1204 caddr_t cp;
1205 int32_t t1, t2;
1206 caddr_t bpos, dpos, cp2;
1207 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1208 struct nfsmount *nmp;
1209 int error = 0, len, retlen, tsiz, eof, attrflag;
1210 int v3 = NFS_ISV3(vp);
1212 #ifndef nolint
1213 eof = 0;
1214 #endif
1215 nmp = VFSTONFS(vp->v_mount);
1216 tsiz = uiop->uio_resid;
1217 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1218 return (EFBIG);
1219 while (tsiz > 0) {
1220 nfsstats.rpccnt[NFSPROC_READ]++;
1221 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1222 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1223 nfsm_fhtom(vp, v3);
1224 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1225 if (v3) {
1226 txdr_hyper(uiop->uio_offset, tl);
1227 *(tl + 2) = txdr_unsigned(len);
1228 } else {
1229 *tl++ = txdr_unsigned(uiop->uio_offset);
1230 *tl++ = txdr_unsigned(len);
1231 *tl = 0;
1233 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1234 if (v3) {
1235 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1236 if (error) {
1237 m_freem(mrep);
1238 goto nfsmout;
1240 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1241 eof = fxdr_unsigned(int, *(tl + 1));
1242 } else
1243 nfsm_loadattr(vp, (struct vattr *)0);
1244 nfsm_strsiz(retlen, nmp->nm_rsize);
1245 nfsm_mtouio(uiop, retlen);
1246 m_freem(mrep);
1247 tsiz -= retlen;
1248 if (v3) {
1249 if (eof || retlen == 0) {
1250 tsiz = 0;
1252 } else if (retlen < len) {
1253 tsiz = 0;
1256 nfsmout:
1257 return (error);
1261 * nfs write call
1264 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, int *must_commit)
1266 u_int32_t *tl;
1267 caddr_t cp;
1268 int32_t t1, t2, backup;
1269 caddr_t bpos, dpos, cp2;
1270 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1271 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1272 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1273 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1275 #ifndef DIAGNOSTIC
1276 if (uiop->uio_iovcnt != 1)
1277 panic("nfs: writerpc iovcnt > 1");
1278 #endif
1279 *must_commit = 0;
1280 tsiz = uiop->uio_resid;
1281 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1282 return (EFBIG);
1283 while (tsiz > 0) {
1284 nfsstats.rpccnt[NFSPROC_WRITE]++;
1285 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1286 nfsm_reqhead(vp, NFSPROC_WRITE,
1287 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1288 nfsm_fhtom(vp, v3);
1289 if (v3) {
1290 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1291 txdr_hyper(uiop->uio_offset, tl);
1292 tl += 2;
1293 *tl++ = txdr_unsigned(len);
1294 *tl++ = txdr_unsigned(*iomode);
1295 *tl = txdr_unsigned(len);
1296 } else {
1297 u_int32_t x;
1299 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1300 /* Set both "begin" and "current" to non-garbage. */
1301 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1302 *tl++ = x; /* "begin offset" */
1303 *tl++ = x; /* "current offset" */
1304 x = txdr_unsigned(len);
1305 *tl++ = x; /* total to this offset */
1306 *tl = x; /* size of this write */
1308 nfsm_uiotom(uiop, len);
1309 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1310 if (v3) {
1312 * The write RPC returns a before and after mtime. The
1313 * nfsm_wcc_data() macro checks the before n_mtime
1314 * against the before time and stores the after time
1315 * in the nfsnode's cached vattr and n_mtime field.
1316 * The NRMODIFIED bit will be set if the before
1317 * time did not match the original mtime.
1319 wccflag = NFSV3_WCCCHK;
1320 nfsm_wcc_data(vp, wccflag);
1321 if (!error) {
1322 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1323 + NFSX_V3WRITEVERF);
1324 rlen = fxdr_unsigned(int, *tl++);
1325 if (rlen == 0) {
1326 error = NFSERR_IO;
1327 m_freem(mrep);
1328 break;
1329 } else if (rlen < len) {
1330 backup = len - rlen;
1331 uiop->uio_iov->iov_base -= backup;
1332 uiop->uio_iov->iov_len += backup;
1333 uiop->uio_offset -= backup;
1334 uiop->uio_resid += backup;
1335 len = rlen;
1337 commit = fxdr_unsigned(int, *tl++);
1340 * Return the lowest committment level
1341 * obtained by any of the RPCs.
1343 if (committed == NFSV3WRITE_FILESYNC)
1344 committed = commit;
1345 else if (committed == NFSV3WRITE_DATASYNC &&
1346 commit == NFSV3WRITE_UNSTABLE)
1347 committed = commit;
1348 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1349 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1350 NFSX_V3WRITEVERF);
1351 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1352 } else if (bcmp((caddr_t)tl,
1353 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1354 *must_commit = 1;
1355 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1356 NFSX_V3WRITEVERF);
1359 } else {
1360 nfsm_loadattr(vp, (struct vattr *)0);
1362 m_freem(mrep);
1363 if (error)
1364 break;
1365 tsiz -= len;
1367 nfsmout:
1368 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1369 committed = NFSV3WRITE_FILESYNC;
1370 *iomode = committed;
1371 if (error)
1372 uiop->uio_resid = tsiz;
1373 return (error);
1377 * nfs mknod rpc
1378 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1379 * mode set to specify the file type and the size field for rdev.
1381 static int
1382 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1383 struct vattr *vap)
1385 struct nfsv2_sattr *sp;
1386 u_int32_t *tl;
1387 caddr_t cp;
1388 int32_t t1, t2;
1389 struct vnode *newvp = (struct vnode *)0;
1390 struct nfsnode *np = (struct nfsnode *)0;
1391 struct vattr vattr;
1392 char *cp2;
1393 caddr_t bpos, dpos;
1394 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1395 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1396 int rmajor, rminor;
1397 int v3 = NFS_ISV3(dvp);
1399 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1400 rmajor = txdr_unsigned(vap->va_rmajor);
1401 rminor = txdr_unsigned(vap->va_rminor);
1402 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) {
1403 rmajor = nfs_xdrneg1;
1404 rminor = nfs_xdrneg1;
1405 } else {
1406 return (EOPNOTSUPP);
1408 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1409 return (error);
1411 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1412 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1413 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1414 nfsm_fhtom(dvp, v3);
1415 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1416 if (v3) {
1417 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1418 *tl++ = vtonfsv3_type(vap->va_type);
1419 nfsm_v3attrbuild(vap, FALSE);
1420 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1421 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1422 *tl++ = txdr_unsigned(vap->va_rmajor);
1423 *tl = txdr_unsigned(vap->va_rminor);
1425 } else {
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;
1430 sp->sa_size = makeudev(rmajor, rminor);
1431 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1432 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1434 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1435 if (!error) {
1436 nfsm_mtofh(dvp, newvp, v3, gotvp);
1437 if (!gotvp) {
1438 if (newvp) {
1439 vput(newvp);
1440 newvp = (struct vnode *)0;
1442 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1443 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1444 if (!error)
1445 newvp = NFSTOV(np);
1448 if (v3)
1449 nfsm_wcc_data(dvp, wccflag);
1450 m_freem(mrep);
1451 nfsmout:
1452 if (error) {
1453 if (newvp)
1454 vput(newvp);
1455 } else {
1456 *vpp = newvp;
1458 VTONFS(dvp)->n_flag |= NLMODIFIED;
1459 if (!wccflag)
1460 VTONFS(dvp)->n_attrstamp = 0;
1461 return (error);
1465 * nfs mknod vop
1466 * just call nfs_mknodrpc() to do the work.
1468 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1469 * struct componentname *a_cnp, struct vattr *a_vap)
1471 /* ARGSUSED */
1472 static int
1473 nfs_mknod(struct vop_old_mknod_args *ap)
1475 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1478 static u_long create_verf;
1480 * nfs file create call
1482 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1483 * struct componentname *a_cnp, struct vattr *a_vap)
1485 static int
1486 nfs_create(struct vop_old_create_args *ap)
1488 struct vnode *dvp = ap->a_dvp;
1489 struct vattr *vap = ap->a_vap;
1490 struct componentname *cnp = ap->a_cnp;
1491 struct nfsv2_sattr *sp;
1492 u_int32_t *tl;
1493 caddr_t cp;
1494 int32_t t1, t2;
1495 struct nfsnode *np = (struct nfsnode *)0;
1496 struct vnode *newvp = (struct vnode *)0;
1497 caddr_t bpos, dpos, cp2;
1498 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1499 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1500 struct vattr vattr;
1501 int v3 = NFS_ISV3(dvp);
1504 * Oops, not for me..
1506 if (vap->va_type == VSOCK)
1507 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1509 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1510 return (error);
1512 if (vap->va_vaflags & VA_EXCLUSIVE)
1513 fmode |= O_EXCL;
1514 again:
1515 nfsstats.rpccnt[NFSPROC_CREATE]++;
1516 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1517 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1518 nfsm_fhtom(dvp, v3);
1519 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1520 if (v3) {
1521 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1522 if (fmode & O_EXCL) {
1523 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1524 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1525 #ifdef INET
1526 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
1527 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia)->sin_addr.s_addr;
1528 else
1529 #endif
1530 *tl++ = create_verf;
1531 *tl = ++create_verf;
1532 } else {
1533 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1534 nfsm_v3attrbuild(vap, FALSE);
1536 } else {
1537 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1538 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1539 sp->sa_uid = nfs_xdrneg1;
1540 sp->sa_gid = nfs_xdrneg1;
1541 sp->sa_size = 0;
1542 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1543 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1545 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1546 if (!error) {
1547 nfsm_mtofh(dvp, newvp, v3, gotvp);
1548 if (!gotvp) {
1549 if (newvp) {
1550 vput(newvp);
1551 newvp = (struct vnode *)0;
1553 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1554 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1555 if (!error)
1556 newvp = NFSTOV(np);
1559 if (v3)
1560 nfsm_wcc_data(dvp, wccflag);
1561 m_freem(mrep);
1562 nfsmout:
1563 if (error) {
1564 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1565 KKASSERT(newvp == NULL);
1566 fmode &= ~O_EXCL;
1567 goto again;
1569 } else if (v3 && (fmode & O_EXCL)) {
1571 * We are normally called with only a partially initialized
1572 * VAP. Since the NFSv3 spec says that server may use the
1573 * file attributes to store the verifier, the spec requires
1574 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1575 * in atime, but we can't really assume that all servers will
1576 * so we ensure that our SETATTR sets both atime and mtime.
1578 if (vap->va_mtime.tv_sec == VNOVAL)
1579 vfs_timestamp(&vap->va_mtime);
1580 if (vap->va_atime.tv_sec == VNOVAL)
1581 vap->va_atime = vap->va_mtime;
1582 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1584 if (error == 0) {
1586 * The new np may have enough info for access
1587 * checks, make sure rucred and wucred are
1588 * initialized for read and write rpc's.
1590 np = VTONFS(newvp);
1591 if (np->n_rucred == NULL)
1592 np->n_rucred = crhold(cnp->cn_cred);
1593 if (np->n_wucred == NULL)
1594 np->n_wucred = crhold(cnp->cn_cred);
1595 *ap->a_vpp = newvp;
1596 } else if (newvp) {
1597 vput(newvp);
1599 VTONFS(dvp)->n_flag |= NLMODIFIED;
1600 if (!wccflag)
1601 VTONFS(dvp)->n_attrstamp = 0;
1602 return (error);
1606 * nfs file remove call
1607 * To try and make nfs semantics closer to ufs semantics, a file that has
1608 * other processes using the vnode is renamed instead of removed and then
1609 * removed later on the last close.
1610 * - If v_sysref.refcnt > 1
1611 * If a rename is not already in the works
1612 * call nfs_sillyrename() to set it up
1613 * else
1614 * do the remove rpc
1616 * nfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
1617 * struct componentname *a_cnp)
1619 static int
1620 nfs_remove(struct vop_old_remove_args *ap)
1622 struct vnode *vp = ap->a_vp;
1623 struct vnode *dvp = ap->a_dvp;
1624 struct componentname *cnp = ap->a_cnp;
1625 struct nfsnode *np = VTONFS(vp);
1626 int error = 0;
1627 struct vattr vattr;
1629 #ifndef DIAGNOSTIC
1630 if (vp->v_sysref.refcnt < 1)
1631 panic("nfs_remove: bad v_sysref.refcnt");
1632 #endif
1633 if (vp->v_type == VDIR)
1634 error = EPERM;
1635 else if (vp->v_sysref.refcnt == 1 || (np->n_sillyrename &&
1636 VOP_GETATTR(vp, &vattr) == 0 &&
1637 vattr.va_nlink > 1)) {
1639 * throw away biocache buffers, mainly to avoid
1640 * unnecessary delayed writes later.
1642 error = nfs_vinvalbuf(vp, 0, 1);
1643 /* Do the rpc */
1644 if (error != EINTR)
1645 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1646 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1648 * Kludge City: If the first reply to the remove rpc is lost..
1649 * the reply to the retransmitted request will be ENOENT
1650 * since the file was in fact removed
1651 * Therefore, we cheat and return success.
1653 if (error == ENOENT)
1654 error = 0;
1655 } else if (!np->n_sillyrename) {
1656 error = nfs_sillyrename(dvp, vp, cnp);
1658 np->n_attrstamp = 0;
1659 return (error);
1663 * nfs file remove rpc called from nfs_inactive
1666 nfs_removeit(struct sillyrename *sp)
1668 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1669 sp->s_cred, NULL));
1673 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1675 static int
1676 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1677 struct ucred *cred, struct thread *td)
1679 u_int32_t *tl;
1680 caddr_t cp;
1681 int32_t t1, t2;
1682 caddr_t bpos, dpos, cp2;
1683 int error = 0, wccflag = NFSV3_WCCRATTR;
1684 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1685 int v3 = NFS_ISV3(dvp);
1687 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1688 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1689 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1690 nfsm_fhtom(dvp, v3);
1691 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1692 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1693 if (v3)
1694 nfsm_wcc_data(dvp, wccflag);
1695 m_freem(mrep);
1696 nfsmout:
1697 VTONFS(dvp)->n_flag |= NLMODIFIED;
1698 if (!wccflag)
1699 VTONFS(dvp)->n_attrstamp = 0;
1700 return (error);
1704 * nfs file rename call
1706 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1707 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1708 * struct vnode *a_tvp, struct componentname *a_tcnp)
1710 static int
1711 nfs_rename(struct vop_old_rename_args *ap)
1713 struct vnode *fvp = ap->a_fvp;
1714 struct vnode *tvp = ap->a_tvp;
1715 struct vnode *fdvp = ap->a_fdvp;
1716 struct vnode *tdvp = ap->a_tdvp;
1717 struct componentname *tcnp = ap->a_tcnp;
1718 struct componentname *fcnp = ap->a_fcnp;
1719 int error;
1721 /* Check for cross-device rename */
1722 if ((fvp->v_mount != tdvp->v_mount) ||
1723 (tvp && (fvp->v_mount != tvp->v_mount))) {
1724 error = EXDEV;
1725 goto out;
1729 * We shouldn't have to flush fvp on rename for most server-side
1730 * filesystems as the file handle should not change. Unfortunately
1731 * the inode for some filesystems (msdosfs) might be tied to the
1732 * file name or directory position so to be completely safe
1733 * vfs.nfs.flush_on_rename is set by default. Clear to improve
1734 * performance.
1736 * We must flush tvp on rename because it might become stale on the
1737 * server after the rename.
1739 if (nfs_flush_on_rename)
1740 VOP_FSYNC(fvp, MNT_WAIT);
1741 if (tvp)
1742 VOP_FSYNC(tvp, MNT_WAIT);
1745 * If the tvp exists and is in use, sillyrename it before doing the
1746 * rename of the new file over it.
1748 * XXX Can't sillyrename a directory.
1750 * We do not attempt to do any namecache purges in this old API
1751 * routine. The new API compat functions have access to the actual
1752 * namecache structures and will do it for us.
1754 if (tvp && tvp->v_sysref.refcnt > 1 && !VTONFS(tvp)->n_sillyrename &&
1755 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1756 vput(tvp);
1757 tvp = NULL;
1758 } else if (tvp) {
1762 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1763 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1764 tcnp->cn_td);
1766 out:
1767 if (tdvp == tvp)
1768 vrele(tdvp);
1769 else
1770 vput(tdvp);
1771 if (tvp)
1772 vput(tvp);
1773 vrele(fdvp);
1774 vrele(fvp);
1776 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1778 if (error == ENOENT)
1779 error = 0;
1780 return (error);
1784 * nfs file rename rpc called from nfs_remove() above
1786 static int
1787 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1788 struct sillyrename *sp)
1790 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1791 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1795 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1797 static int
1798 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1799 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1800 struct ucred *cred, struct thread *td)
1802 u_int32_t *tl;
1803 caddr_t cp;
1804 int32_t t1, t2;
1805 caddr_t bpos, dpos, cp2;
1806 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1807 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1808 int v3 = NFS_ISV3(fdvp);
1810 nfsstats.rpccnt[NFSPROC_RENAME]++;
1811 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1812 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1813 nfsm_rndup(tnamelen));
1814 nfsm_fhtom(fdvp, v3);
1815 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1816 nfsm_fhtom(tdvp, v3);
1817 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1818 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1819 if (v3) {
1820 nfsm_wcc_data(fdvp, fwccflag);
1821 nfsm_wcc_data(tdvp, twccflag);
1823 m_freem(mrep);
1824 nfsmout:
1825 VTONFS(fdvp)->n_flag |= NLMODIFIED;
1826 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1827 if (!fwccflag)
1828 VTONFS(fdvp)->n_attrstamp = 0;
1829 if (!twccflag)
1830 VTONFS(tdvp)->n_attrstamp = 0;
1831 return (error);
1835 * nfs hard link create call
1837 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1838 * struct componentname *a_cnp)
1840 static int
1841 nfs_link(struct vop_old_link_args *ap)
1843 struct vnode *vp = ap->a_vp;
1844 struct vnode *tdvp = ap->a_tdvp;
1845 struct componentname *cnp = ap->a_cnp;
1846 u_int32_t *tl;
1847 caddr_t cp;
1848 int32_t t1, t2;
1849 caddr_t bpos, dpos, cp2;
1850 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1851 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1852 int v3;
1854 if (vp->v_mount != tdvp->v_mount) {
1855 return (EXDEV);
1859 * The attribute cache may get out of sync with the server on link.
1860 * Pushing writes to the server before handle was inherited from
1861 * long long ago and it is unclear if we still need to do this.
1862 * Defaults to off.
1864 if (nfs_flush_on_hlink)
1865 VOP_FSYNC(vp, MNT_WAIT);
1867 v3 = NFS_ISV3(vp);
1868 nfsstats.rpccnt[NFSPROC_LINK]++;
1869 nfsm_reqhead(vp, NFSPROC_LINK,
1870 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1871 nfsm_fhtom(vp, v3);
1872 nfsm_fhtom(tdvp, v3);
1873 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1874 nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1875 if (v3) {
1876 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1877 nfsm_wcc_data(tdvp, wccflag);
1879 m_freem(mrep);
1880 nfsmout:
1881 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1882 if (!attrflag)
1883 VTONFS(vp)->n_attrstamp = 0;
1884 if (!wccflag)
1885 VTONFS(tdvp)->n_attrstamp = 0;
1887 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1889 if (error == EEXIST)
1890 error = 0;
1891 return (error);
1895 * nfs symbolic link create call
1897 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1898 * struct componentname *a_cnp, struct vattr *a_vap,
1899 * char *a_target)
1901 static int
1902 nfs_symlink(struct vop_old_symlink_args *ap)
1904 struct vnode *dvp = ap->a_dvp;
1905 struct vattr *vap = ap->a_vap;
1906 struct componentname *cnp = ap->a_cnp;
1907 struct nfsv2_sattr *sp;
1908 u_int32_t *tl;
1909 caddr_t cp;
1910 int32_t t1, t2;
1911 caddr_t bpos, dpos, cp2;
1912 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1913 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1914 struct vnode *newvp = (struct vnode *)0;
1915 int v3 = NFS_ISV3(dvp);
1917 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1918 slen = strlen(ap->a_target);
1919 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1920 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1921 nfsm_fhtom(dvp, v3);
1922 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1923 if (v3) {
1924 nfsm_v3attrbuild(vap, FALSE);
1926 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1927 if (!v3) {
1928 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1929 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1930 sp->sa_uid = nfs_xdrneg1;
1931 sp->sa_gid = nfs_xdrneg1;
1932 sp->sa_size = nfs_xdrneg1;
1933 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1934 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1938 * Issue the NFS request and get the rpc response.
1940 * Only NFSv3 responses returning an error of 0 actually return
1941 * a file handle that can be converted into newvp without having
1942 * to do an extra lookup rpc.
1944 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1945 if (v3) {
1946 if (error == 0)
1947 nfsm_mtofh(dvp, newvp, v3, gotvp);
1948 nfsm_wcc_data(dvp, wccflag);
1952 * out code jumps -> here, mrep is also freed.
1955 m_freem(mrep);
1956 nfsmout:
1959 * If we get an EEXIST error, silently convert it to no-error
1960 * in case of an NFS retry.
1962 if (error == EEXIST)
1963 error = 0;
1966 * If we do not have (or no longer have) an error, and we could
1967 * not extract the newvp from the response due to the request being
1968 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1969 * to obtain a newvp to return.
1971 if (error == 0 && newvp == NULL) {
1972 struct nfsnode *np = NULL;
1974 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1975 cnp->cn_cred, cnp->cn_td, &np);
1976 if (!error)
1977 newvp = NFSTOV(np);
1979 if (error) {
1980 if (newvp)
1981 vput(newvp);
1982 } else {
1983 *ap->a_vpp = newvp;
1985 VTONFS(dvp)->n_flag |= NLMODIFIED;
1986 if (!wccflag)
1987 VTONFS(dvp)->n_attrstamp = 0;
1988 return (error);
1992 * nfs make dir call
1994 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1995 * struct componentname *a_cnp, struct vattr *a_vap)
1997 static int
1998 nfs_mkdir(struct vop_old_mkdir_args *ap)
2000 struct vnode *dvp = ap->a_dvp;
2001 struct vattr *vap = ap->a_vap;
2002 struct componentname *cnp = ap->a_cnp;
2003 struct nfsv2_sattr *sp;
2004 u_int32_t *tl;
2005 caddr_t cp;
2006 int32_t t1, t2;
2007 int len;
2008 struct nfsnode *np = (struct nfsnode *)0;
2009 struct vnode *newvp = (struct vnode *)0;
2010 caddr_t bpos, dpos, cp2;
2011 int error = 0, wccflag = NFSV3_WCCRATTR;
2012 int gotvp = 0;
2013 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2014 struct vattr vattr;
2015 int v3 = NFS_ISV3(dvp);
2017 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
2018 return (error);
2020 len = cnp->cn_namelen;
2021 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2022 nfsm_reqhead(dvp, NFSPROC_MKDIR,
2023 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2024 nfsm_fhtom(dvp, v3);
2025 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
2026 if (v3) {
2027 nfsm_v3attrbuild(vap, FALSE);
2028 } else {
2029 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2030 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2031 sp->sa_uid = nfs_xdrneg1;
2032 sp->sa_gid = nfs_xdrneg1;
2033 sp->sa_size = nfs_xdrneg1;
2034 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2035 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2037 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
2038 if (!error)
2039 nfsm_mtofh(dvp, newvp, v3, gotvp);
2040 if (v3)
2041 nfsm_wcc_data(dvp, wccflag);
2042 m_freem(mrep);
2043 nfsmout:
2044 VTONFS(dvp)->n_flag |= NLMODIFIED;
2045 if (!wccflag)
2046 VTONFS(dvp)->n_attrstamp = 0;
2048 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2049 * if we can succeed in looking up the directory.
2051 if (error == EEXIST || (!error && !gotvp)) {
2052 if (newvp) {
2053 vrele(newvp);
2054 newvp = (struct vnode *)0;
2056 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2057 cnp->cn_td, &np);
2058 if (!error) {
2059 newvp = NFSTOV(np);
2060 if (newvp->v_type != VDIR)
2061 error = EEXIST;
2064 if (error) {
2065 if (newvp)
2066 vrele(newvp);
2067 } else
2068 *ap->a_vpp = newvp;
2069 return (error);
2073 * nfs remove directory call
2075 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2076 * struct componentname *a_cnp)
2078 static int
2079 nfs_rmdir(struct vop_old_rmdir_args *ap)
2081 struct vnode *vp = ap->a_vp;
2082 struct vnode *dvp = ap->a_dvp;
2083 struct componentname *cnp = ap->a_cnp;
2084 u_int32_t *tl;
2085 caddr_t cp;
2086 int32_t t1, t2;
2087 caddr_t bpos, dpos, cp2;
2088 int error = 0, wccflag = NFSV3_WCCRATTR;
2089 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2090 int v3 = NFS_ISV3(dvp);
2092 if (dvp == vp)
2093 return (EINVAL);
2094 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2095 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2096 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2097 nfsm_fhtom(dvp, v3);
2098 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2099 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2100 if (v3)
2101 nfsm_wcc_data(dvp, wccflag);
2102 m_freem(mrep);
2103 nfsmout:
2104 VTONFS(dvp)->n_flag |= NLMODIFIED;
2105 if (!wccflag)
2106 VTONFS(dvp)->n_attrstamp = 0;
2108 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2110 if (error == ENOENT)
2111 error = 0;
2112 return (error);
2116 * nfs readdir call
2118 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2120 static int
2121 nfs_readdir(struct vop_readdir_args *ap)
2123 struct vnode *vp = ap->a_vp;
2124 struct nfsnode *np = VTONFS(vp);
2125 struct uio *uio = ap->a_uio;
2126 int tresid, error;
2127 struct vattr vattr;
2129 if (vp->v_type != VDIR)
2130 return (EPERM);
2132 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
2133 return (error);
2136 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2137 * and then check that is still valid, or if this is an NQNFS mount
2138 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2139 * VOP_GETATTR() does not necessarily go to the wire.
2141 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2142 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2143 if (VOP_GETATTR(vp, &vattr) == 0 &&
2144 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2146 nfsstats.direofcache_hits++;
2147 goto done;
2152 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2153 * own cache coherency checks so we do not have to.
2155 tresid = uio->uio_resid;
2156 error = nfs_bioread(vp, uio, 0);
2158 if (!error && uio->uio_resid == tresid)
2159 nfsstats.direofcache_misses++;
2160 done:
2161 vn_unlock(vp);
2162 return (error);
2166 * Readdir rpc call. nfs_bioread->nfs_doio->nfs_readdirrpc.
2168 * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2169 * offset/block and converts the nfs formatted directory entries for userland
2170 * consumption as well as deals with offsets into the middle of blocks.
2171 * nfs_doio only deals with logical blocks. In particular, uio_offset will
2172 * be block-bounded. It must convert to cookies for the actual RPC.
2175 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2177 int len, left;
2178 struct nfs_dirent *dp = NULL;
2179 u_int32_t *tl;
2180 caddr_t cp;
2181 int32_t t1, t2;
2182 nfsuint64 *cookiep;
2183 caddr_t bpos, dpos, cp2;
2184 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2185 nfsuint64 cookie;
2186 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2187 struct nfsnode *dnp = VTONFS(vp);
2188 u_quad_t fileno;
2189 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2190 int attrflag;
2191 int v3 = NFS_ISV3(vp);
2193 #ifndef DIAGNOSTIC
2194 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2195 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2196 panic("nfs readdirrpc bad uio");
2197 #endif
2200 * If there is no cookie, assume directory was stale.
2202 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2203 if (cookiep)
2204 cookie = *cookiep;
2205 else
2206 return (NFSERR_BAD_COOKIE);
2208 * Loop around doing readdir rpc's of size nm_readdirsize
2209 * truncated to a multiple of DIRBLKSIZ.
2210 * The stopping criteria is EOF or buffer full.
2212 while (more_dirs && bigenough) {
2213 nfsstats.rpccnt[NFSPROC_READDIR]++;
2214 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2215 NFSX_READDIR(v3));
2216 nfsm_fhtom(vp, v3);
2217 if (v3) {
2218 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2219 *tl++ = cookie.nfsuquad[0];
2220 *tl++ = cookie.nfsuquad[1];
2221 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2222 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2223 } else {
2224 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2225 *tl++ = cookie.nfsuquad[0];
2227 *tl = txdr_unsigned(nmp->nm_readdirsize);
2228 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2229 if (v3) {
2230 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2231 if (!error) {
2232 nfsm_dissect(tl, u_int32_t *,
2233 2 * NFSX_UNSIGNED);
2234 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2235 dnp->n_cookieverf.nfsuquad[1] = *tl;
2236 } else {
2237 m_freem(mrep);
2238 goto nfsmout;
2241 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2242 more_dirs = fxdr_unsigned(int, *tl);
2244 /* loop thru the dir entries, converting them to std form */
2245 while (more_dirs && bigenough) {
2246 if (v3) {
2247 nfsm_dissect(tl, u_int32_t *,
2248 3 * NFSX_UNSIGNED);
2249 fileno = fxdr_hyper(tl);
2250 len = fxdr_unsigned(int, *(tl + 2));
2251 } else {
2252 nfsm_dissect(tl, u_int32_t *,
2253 2 * NFSX_UNSIGNED);
2254 fileno = fxdr_unsigned(u_quad_t, *tl++);
2255 len = fxdr_unsigned(int, *tl);
2257 if (len <= 0 || len > NFS_MAXNAMLEN) {
2258 error = EBADRPC;
2259 m_freem(mrep);
2260 goto nfsmout;
2264 * len is the number of bytes in the path element
2265 * name, not including the \0 termination.
2267 * tlen is the number of bytes w have to reserve for
2268 * the path element name.
2270 tlen = nfsm_rndup(len);
2271 if (tlen == len)
2272 tlen += 4; /* To ensure null termination */
2275 * If the entry would cross a DIRBLKSIZ boundary,
2276 * extend the previous nfs_dirent to cover the
2277 * remaining space.
2279 left = DIRBLKSIZ - blksiz;
2280 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2281 dp->nfs_reclen += left;
2282 uiop->uio_iov->iov_base += left;
2283 uiop->uio_iov->iov_len -= left;
2284 uiop->uio_offset += left;
2285 uiop->uio_resid -= left;
2286 blksiz = 0;
2288 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2289 bigenough = 0;
2290 if (bigenough) {
2291 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2292 dp->nfs_ino = fileno;
2293 dp->nfs_namlen = len;
2294 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2295 dp->nfs_type = DT_UNKNOWN;
2296 blksiz += dp->nfs_reclen;
2297 if (blksiz == DIRBLKSIZ)
2298 blksiz = 0;
2299 uiop->uio_offset += sizeof(struct nfs_dirent);
2300 uiop->uio_resid -= sizeof(struct nfs_dirent);
2301 uiop->uio_iov->iov_base += sizeof(struct nfs_dirent);
2302 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2303 nfsm_mtouio(uiop, len);
2306 * The uiop has advanced by nfs_dirent + len
2307 * but really needs to advance by
2308 * nfs_dirent + tlen
2310 cp = uiop->uio_iov->iov_base;
2311 tlen -= len;
2312 *cp = '\0'; /* null terminate */
2313 uiop->uio_iov->iov_base += tlen;
2314 uiop->uio_iov->iov_len -= tlen;
2315 uiop->uio_offset += tlen;
2316 uiop->uio_resid -= tlen;
2317 } else {
2319 * NFS strings must be rounded up (nfsm_myouio
2320 * handled that in the bigenough case).
2322 nfsm_adv(nfsm_rndup(len));
2324 if (v3) {
2325 nfsm_dissect(tl, u_int32_t *,
2326 3 * NFSX_UNSIGNED);
2327 } else {
2328 nfsm_dissect(tl, u_int32_t *,
2329 2 * NFSX_UNSIGNED);
2333 * If we were able to accomodate the last entry,
2334 * get the cookie for the next one. Otherwise
2335 * hold-over the cookie for the one we were not
2336 * able to accomodate.
2338 if (bigenough) {
2339 cookie.nfsuquad[0] = *tl++;
2340 if (v3)
2341 cookie.nfsuquad[1] = *tl++;
2342 } else if (v3) {
2343 tl += 2;
2344 } else {
2345 tl++;
2347 more_dirs = fxdr_unsigned(int, *tl);
2350 * If at end of rpc data, get the eof boolean
2352 if (!more_dirs) {
2353 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2354 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2356 m_freem(mrep);
2359 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2360 * by increasing d_reclen for the last record.
2362 if (blksiz > 0) {
2363 left = DIRBLKSIZ - blksiz;
2364 dp->nfs_reclen += left;
2365 uiop->uio_iov->iov_base += left;
2366 uiop->uio_iov->iov_len -= left;
2367 uiop->uio_offset += left;
2368 uiop->uio_resid -= left;
2371 if (bigenough) {
2373 * We hit the end of the directory, update direofoffset.
2375 dnp->n_direofoffset = uiop->uio_offset;
2376 } else {
2378 * There is more to go, insert the link cookie so the
2379 * next block can be read.
2381 if (uiop->uio_resid > 0)
2382 kprintf("EEK! readdirrpc resid > 0\n");
2383 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2384 *cookiep = cookie;
2386 nfsmout:
2387 return (error);
2391 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2394 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2396 int len, left;
2397 struct nfs_dirent *dp;
2398 u_int32_t *tl;
2399 caddr_t cp;
2400 int32_t t1, t2;
2401 struct vnode *newvp;
2402 nfsuint64 *cookiep;
2403 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2404 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2405 nfsuint64 cookie;
2406 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2407 struct nfsnode *dnp = VTONFS(vp), *np;
2408 nfsfh_t *fhp;
2409 u_quad_t fileno;
2410 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2411 int attrflag, fhsize;
2412 struct nchandle nch;
2413 struct nchandle dnch;
2414 struct nlcomponent nlc;
2416 #ifndef nolint
2417 dp = NULL;
2418 #endif
2419 #ifndef DIAGNOSTIC
2420 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2421 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2422 panic("nfs readdirplusrpc bad uio");
2423 #endif
2425 * Obtain the namecache record for the directory so we have something
2426 * to use as a basis for creating the entries. This function will
2427 * return a held (but not locked) ncp. The ncp may be disconnected
2428 * from the tree and cannot be used for upward traversals, and the
2429 * ncp may be unnamed. Note that other unrelated operations may
2430 * cause the ncp to be named at any time.
2432 cache_fromdvp(vp, NULL, 0, &dnch);
2433 bzero(&nlc, sizeof(nlc));
2434 newvp = NULLVP;
2437 * If there is no cookie, assume directory was stale.
2439 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2440 if (cookiep)
2441 cookie = *cookiep;
2442 else
2443 return (NFSERR_BAD_COOKIE);
2445 * Loop around doing readdir rpc's of size nm_readdirsize
2446 * truncated to a multiple of DIRBLKSIZ.
2447 * The stopping criteria is EOF or buffer full.
2449 while (more_dirs && bigenough) {
2450 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2451 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2452 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2453 nfsm_fhtom(vp, 1);
2454 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2455 *tl++ = cookie.nfsuquad[0];
2456 *tl++ = cookie.nfsuquad[1];
2457 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2458 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2459 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2460 *tl = txdr_unsigned(nmp->nm_rsize);
2461 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2462 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2463 if (error) {
2464 m_freem(mrep);
2465 goto nfsmout;
2467 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2468 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2469 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2470 more_dirs = fxdr_unsigned(int, *tl);
2472 /* loop thru the dir entries, doctoring them to 4bsd form */
2473 while (more_dirs && bigenough) {
2474 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2475 fileno = fxdr_hyper(tl);
2476 len = fxdr_unsigned(int, *(tl + 2));
2477 if (len <= 0 || len > NFS_MAXNAMLEN) {
2478 error = EBADRPC;
2479 m_freem(mrep);
2480 goto nfsmout;
2482 tlen = nfsm_rndup(len);
2483 if (tlen == len)
2484 tlen += 4; /* To ensure null termination*/
2485 left = DIRBLKSIZ - blksiz;
2486 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2487 dp->nfs_reclen += left;
2488 uiop->uio_iov->iov_base += left;
2489 uiop->uio_iov->iov_len -= left;
2490 uiop->uio_offset += left;
2491 uiop->uio_resid -= left;
2492 blksiz = 0;
2494 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2495 bigenough = 0;
2496 if (bigenough) {
2497 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2498 dp->nfs_ino = fileno;
2499 dp->nfs_namlen = len;
2500 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2501 dp->nfs_type = DT_UNKNOWN;
2502 blksiz += dp->nfs_reclen;
2503 if (blksiz == DIRBLKSIZ)
2504 blksiz = 0;
2505 uiop->uio_offset += sizeof(struct nfs_dirent);
2506 uiop->uio_resid -= sizeof(struct nfs_dirent);
2507 uiop->uio_iov->iov_base += sizeof(struct nfs_dirent);
2508 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2509 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2510 nlc.nlc_namelen = len;
2511 nfsm_mtouio(uiop, len);
2512 cp = uiop->uio_iov->iov_base;
2513 tlen -= len;
2514 *cp = '\0';
2515 uiop->uio_iov->iov_base += tlen;
2516 uiop->uio_iov->iov_len -= tlen;
2517 uiop->uio_offset += tlen;
2518 uiop->uio_resid -= tlen;
2519 } else
2520 nfsm_adv(nfsm_rndup(len));
2521 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2522 if (bigenough) {
2523 cookie.nfsuquad[0] = *tl++;
2524 cookie.nfsuquad[1] = *tl++;
2525 } else
2526 tl += 2;
2529 * Since the attributes are before the file handle
2530 * (sigh), we must skip over the attributes and then
2531 * come back and get them.
2533 attrflag = fxdr_unsigned(int, *tl);
2534 if (attrflag) {
2535 dpossav1 = dpos;
2536 mdsav1 = md;
2537 nfsm_adv(NFSX_V3FATTR);
2538 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2539 doit = fxdr_unsigned(int, *tl);
2540 if (doit) {
2541 nfsm_getfh(fhp, fhsize, 1);
2542 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2543 vref(vp);
2544 newvp = vp;
2545 np = dnp;
2546 } else {
2547 error = nfs_nget(vp->v_mount, fhp,
2548 fhsize, &np);
2549 if (error)
2550 doit = 0;
2551 else
2552 newvp = NFSTOV(np);
2555 if (doit && bigenough) {
2556 dpossav2 = dpos;
2557 dpos = dpossav1;
2558 mdsav2 = md;
2559 md = mdsav1;
2560 nfsm_loadattr(newvp, (struct vattr *)0);
2561 dpos = dpossav2;
2562 md = mdsav2;
2563 dp->nfs_type =
2564 IFTODT(VTTOIF(np->n_vattr.va_type));
2565 if (dnch.ncp) {
2566 kprintf("NFS/READDIRPLUS, ENTER %*.*s\n",
2567 nlc.nlc_namelen, nlc.nlc_namelen,
2568 nlc.nlc_nameptr);
2569 nch = cache_nlookup(&dnch, &nlc);
2570 cache_setunresolved(&nch);
2571 nfs_cache_setvp(&nch, newvp,
2572 nfspos_cache_timeout);
2573 cache_put(&nch);
2574 } else {
2575 kprintf("NFS/READDIRPLUS, UNABLE TO ENTER"
2576 " %*.*s\n",
2577 nlc.nlc_namelen, nlc.nlc_namelen,
2578 nlc.nlc_nameptr);
2581 } else {
2582 /* Just skip over the file handle */
2583 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2584 i = fxdr_unsigned(int, *tl);
2585 nfsm_adv(nfsm_rndup(i));
2587 if (newvp != NULLVP) {
2588 if (newvp == vp)
2589 vrele(newvp);
2590 else
2591 vput(newvp);
2592 newvp = NULLVP;
2594 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2595 more_dirs = fxdr_unsigned(int, *tl);
2598 * If at end of rpc data, get the eof boolean
2600 if (!more_dirs) {
2601 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2602 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2604 m_freem(mrep);
2607 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2608 * by increasing d_reclen for the last record.
2610 if (blksiz > 0) {
2611 left = DIRBLKSIZ - blksiz;
2612 dp->nfs_reclen += left;
2613 uiop->uio_iov->iov_base += left;
2614 uiop->uio_iov->iov_len -= left;
2615 uiop->uio_offset += left;
2616 uiop->uio_resid -= left;
2620 * We are now either at the end of the directory or have filled the
2621 * block.
2623 if (bigenough)
2624 dnp->n_direofoffset = uiop->uio_offset;
2625 else {
2626 if (uiop->uio_resid > 0)
2627 kprintf("EEK! readdirplusrpc resid > 0\n");
2628 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2629 *cookiep = cookie;
2631 nfsmout:
2632 if (newvp != NULLVP) {
2633 if (newvp == vp)
2634 vrele(newvp);
2635 else
2636 vput(newvp);
2637 newvp = NULLVP;
2639 if (dnch.ncp)
2640 cache_drop(&dnch);
2641 return (error);
2645 * Silly rename. To make the NFS filesystem that is stateless look a little
2646 * more like the "ufs" a remove of an active vnode is translated to a rename
2647 * to a funny looking filename that is removed by nfs_inactive on the
2648 * nfsnode. There is the potential for another process on a different client
2649 * to create the same funny name between the nfs_lookitup() fails and the
2650 * nfs_rename() completes, but...
2652 static int
2653 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2655 struct sillyrename *sp;
2656 struct nfsnode *np;
2657 int error;
2660 * We previously purged dvp instead of vp. I don't know why, it
2661 * completely destroys performance. We can't do it anyway with the
2662 * new VFS API since we would be breaking the namecache topology.
2664 cache_purge(vp); /* XXX */
2665 np = VTONFS(vp);
2666 #ifndef DIAGNOSTIC
2667 if (vp->v_type == VDIR)
2668 panic("nfs: sillyrename dir");
2669 #endif
2670 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2671 M_NFSREQ, M_WAITOK);
2672 sp->s_cred = crdup(cnp->cn_cred);
2673 sp->s_dvp = dvp;
2674 vref(dvp);
2676 /* Fudge together a funny name */
2677 sp->s_namlen = ksprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
2679 /* Try lookitups until we get one that isn't there */
2680 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2681 cnp->cn_td, (struct nfsnode **)0) == 0) {
2682 sp->s_name[4]++;
2683 if (sp->s_name[4] > 'z') {
2684 error = EINVAL;
2685 goto bad;
2688 error = nfs_renameit(dvp, cnp, sp);
2689 if (error)
2690 goto bad;
2691 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2692 cnp->cn_td, &np);
2693 np->n_sillyrename = sp;
2694 return (0);
2695 bad:
2696 vrele(sp->s_dvp);
2697 crfree(sp->s_cred);
2698 kfree((caddr_t)sp, M_NFSREQ);
2699 return (error);
2703 * Look up a file name and optionally either update the file handle or
2704 * allocate an nfsnode, depending on the value of npp.
2705 * npp == NULL --> just do the lookup
2706 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2707 * handled too
2708 * *npp != NULL --> update the file handle in the vnode
2710 static int
2711 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2712 struct thread *td, struct nfsnode **npp)
2714 u_int32_t *tl;
2715 caddr_t cp;
2716 int32_t t1, t2;
2717 struct vnode *newvp = (struct vnode *)0;
2718 struct nfsnode *np, *dnp = VTONFS(dvp);
2719 caddr_t bpos, dpos, cp2;
2720 int error = 0, fhlen, attrflag;
2721 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2722 nfsfh_t *nfhp;
2723 int v3 = NFS_ISV3(dvp);
2725 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2726 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2727 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2728 nfsm_fhtom(dvp, v3);
2729 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2730 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2731 if (npp && !error) {
2732 nfsm_getfh(nfhp, fhlen, v3);
2733 if (*npp) {
2734 np = *npp;
2735 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2736 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
2737 np->n_fhp = &np->n_fh;
2738 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2739 np->n_fhp =(nfsfh_t *)kmalloc(fhlen,M_NFSBIGFH,M_WAITOK);
2740 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2741 np->n_fhsize = fhlen;
2742 newvp = NFSTOV(np);
2743 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2744 vref(dvp);
2745 newvp = dvp;
2746 } else {
2747 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2748 if (error) {
2749 m_freem(mrep);
2750 return (error);
2752 newvp = NFSTOV(np);
2754 if (v3) {
2755 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
2756 if (!attrflag && *npp == NULL) {
2757 m_freem(mrep);
2758 if (newvp == dvp)
2759 vrele(newvp);
2760 else
2761 vput(newvp);
2762 return (ENOENT);
2764 } else
2765 nfsm_loadattr(newvp, (struct vattr *)0);
2767 m_freem(mrep);
2768 nfsmout:
2769 if (npp && *npp == NULL) {
2770 if (error) {
2771 if (newvp) {
2772 if (newvp == dvp)
2773 vrele(newvp);
2774 else
2775 vput(newvp);
2777 } else
2778 *npp = np;
2780 return (error);
2784 * Nfs Version 3 commit rpc
2787 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2789 caddr_t cp;
2790 u_int32_t *tl;
2791 int32_t t1, t2;
2792 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2793 caddr_t bpos, dpos, cp2;
2794 int error = 0, wccflag = NFSV3_WCCRATTR;
2795 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2797 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2798 return (0);
2799 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2800 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2801 nfsm_fhtom(vp, 1);
2802 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2803 txdr_hyper(offset, tl);
2804 tl += 2;
2805 *tl = txdr_unsigned(cnt);
2806 nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2807 nfsm_wcc_data(vp, wccflag);
2808 if (!error) {
2809 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2810 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2811 NFSX_V3WRITEVERF)) {
2812 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2813 NFSX_V3WRITEVERF);
2814 error = NFSERR_STALEWRITEVERF;
2817 m_freem(mrep);
2818 nfsmout:
2819 return (error);
2823 * Kludge City..
2824 * - make nfs_bmap() essentially a no-op that does no translation
2825 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2826 * (Maybe I could use the process's page mapping, but I was concerned that
2827 * Kernel Write might not be enabled and also figured copyout() would do
2828 * a lot more work than bcopy() and also it currently happens in the
2829 * context of the swapper process (2).
2831 * nfs_bmap(struct vnode *a_vp, off_t a_loffset,
2832 * off_t *a_doffsetp, int *a_runp, int *a_runb)
2834 static int
2835 nfs_bmap(struct vop_bmap_args *ap)
2837 if (ap->a_doffsetp != NULL)
2838 *ap->a_doffsetp = ap->a_loffset;
2839 if (ap->a_runp != NULL)
2840 *ap->a_runp = 0;
2841 if (ap->a_runb != NULL)
2842 *ap->a_runb = 0;
2843 return (0);
2847 * Strategy routine.
2849 * For async requests when nfsiod(s) are running, queue the request by
2850 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2851 * request.
2853 static int
2854 nfs_strategy(struct vop_strategy_args *ap)
2856 struct bio *bio = ap->a_bio;
2857 struct bio *nbio;
2858 struct buf *bp = bio->bio_buf;
2859 struct thread *td;
2860 int error = 0;
2862 KASSERT(bp->b_cmd != BUF_CMD_DONE,
2863 ("nfs_strategy: buffer %p unexpectedly marked done", bp));
2864 KASSERT(BUF_REFCNT(bp) > 0,
2865 ("nfs_strategy: buffer %p not locked", bp));
2867 if (bp->b_flags & B_ASYNC)
2868 td = NULL;
2869 else
2870 td = curthread; /* XXX */
2873 * We probably don't need to push an nbio any more since no
2874 * block conversion is required due to the use of 64 bit byte
2875 * offsets, but do it anyway.
2877 nbio = push_bio(bio);
2878 nbio->bio_offset = bio->bio_offset;
2881 * If the op is asynchronous and an i/o daemon is waiting
2882 * queue the request, wake it up and wait for completion
2883 * otherwise just do it ourselves.
2885 if ((bp->b_flags & B_ASYNC) == 0 || nfs_asyncio(ap->a_vp, nbio, td))
2886 error = nfs_doio(ap->a_vp, nbio, td);
2887 return (error);
2891 * Mmap a file
2893 * NB Currently unsupported.
2895 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
2897 /* ARGSUSED */
2898 static int
2899 nfs_mmap(struct vop_mmap_args *ap)
2901 return (EINVAL);
2905 * fsync vnode op. Just call nfs_flush() with commit == 1.
2907 * nfs_fsync(struct vnode *a_vp, int a_waitfor)
2909 /* ARGSUSED */
2910 static int
2911 nfs_fsync(struct vop_fsync_args *ap)
2913 return (nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1));
2917 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
2918 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
2919 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
2920 * set the buffer contains data that has already been written to the server
2921 * and which now needs a commit RPC.
2923 * If commit is 0 we only take one pass and only flush buffers containing new
2924 * dirty data.
2926 * If commit is 1 we take two passes, issuing a commit RPC in the second
2927 * pass.
2929 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
2930 * to completely flush all pending data.
2932 * Note that the RB_SCAN code properly handles the case where the
2933 * callback might block and directly or indirectly (another thread) cause
2934 * the RB tree to change.
2937 #ifndef NFS_COMMITBVECSIZ
2938 #define NFS_COMMITBVECSIZ 16
2939 #endif
2941 struct nfs_flush_info {
2942 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
2943 struct thread *td;
2944 struct vnode *vp;
2945 int waitfor;
2946 int slpflag;
2947 int slptimeo;
2948 int loops;
2949 struct buf *bvary[NFS_COMMITBVECSIZ];
2950 int bvsize;
2951 off_t beg_off;
2952 off_t end_off;
2955 static int nfs_flush_bp(struct buf *bp, void *data);
2956 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
2959 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
2961 struct nfsnode *np = VTONFS(vp);
2962 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2963 struct nfs_flush_info info;
2964 int error;
2966 bzero(&info, sizeof(info));
2967 info.td = td;
2968 info.vp = vp;
2969 info.waitfor = waitfor;
2970 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
2971 info.loops = 0;
2973 do {
2975 * Flush mode
2977 info.mode = NFI_FLUSHNEW;
2978 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2979 nfs_flush_bp, &info);
2982 * Take a second pass if committing and no error occured.
2983 * Clean up any left over collection (whether an error
2984 * occurs or not).
2986 if (commit && error == 0) {
2987 info.mode = NFI_COMMIT;
2988 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2989 nfs_flush_bp, &info);
2990 if (info.bvsize)
2991 error = nfs_flush_docommit(&info, error);
2995 * Wait for pending I/O to complete before checking whether
2996 * any further dirty buffers exist.
2998 while (waitfor == MNT_WAIT && vp->v_track_write.bk_active) {
2999 vp->v_track_write.bk_waitflag = 1;
3000 error = tsleep(&vp->v_track_write,
3001 info.slpflag, "nfsfsync", info.slptimeo);
3002 if (error) {
3004 * We have to be able to break out if this
3005 * is an 'intr' mount.
3007 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
3008 error = -EINTR;
3009 break;
3013 * Since we do not process pending signals,
3014 * once we get a PCATCH our tsleep() will no
3015 * longer sleep, switch to a fixed timeout
3016 * instead.
3018 if (info.slpflag == PCATCH) {
3019 info.slpflag = 0;
3020 info.slptimeo = 2 * hz;
3022 error = 0;
3025 ++info.loops;
3027 * Loop if we are flushing synchronous as well as committing,
3028 * and dirty buffers are still present. Otherwise we might livelock.
3030 } while (waitfor == MNT_WAIT && commit &&
3031 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3034 * The callbacks have to return a negative error to terminate the
3035 * RB scan.
3037 if (error < 0)
3038 error = -error;
3041 * Deal with any error collection
3043 if (np->n_flag & NWRITEERR) {
3044 error = np->n_error;
3045 np->n_flag &= ~NWRITEERR;
3047 return (error);
3051 static
3053 nfs_flush_bp(struct buf *bp, void *data)
3055 struct nfs_flush_info *info = data;
3056 off_t toff;
3057 int error;
3059 error = 0;
3060 switch(info->mode) {
3061 case NFI_FLUSHNEW:
3062 crit_enter();
3063 if (info->loops && info->waitfor == MNT_WAIT) {
3064 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3065 if (error) {
3066 int lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3067 if (info->slpflag & PCATCH)
3068 lkflags |= LK_PCATCH;
3069 error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3070 info->slptimeo);
3072 } else {
3073 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3075 if (error == 0) {
3076 KKASSERT(bp->b_vp == info->vp);
3078 if ((bp->b_flags & B_DELWRI) == 0)
3079 panic("nfs_fsync: not dirty");
3080 if (bp->b_flags & B_NEEDCOMMIT) {
3081 BUF_UNLOCK(bp);
3082 crit_exit();
3083 break;
3085 bremfree(bp);
3087 crit_exit();
3088 bawrite(bp);
3089 } else {
3090 crit_exit();
3091 error = 0;
3093 break;
3094 case NFI_COMMIT:
3096 * Only process buffers in need of a commit which we can
3097 * immediately lock. This may prevent a buffer from being
3098 * committed, but the normal flush loop will block on the
3099 * same buffer so we shouldn't get into an endless loop.
3101 crit_enter();
3102 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3103 (B_DELWRI | B_NEEDCOMMIT) ||
3104 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
3105 crit_exit();
3106 break;
3109 KKASSERT(bp->b_vp == info->vp);
3110 bremfree(bp);
3113 * NOTE: storing the bp in the bvary[] basically sets
3114 * it up for a commit operation.
3116 * We must call vfs_busy_pages() now so the commit operation
3117 * is interlocked with user modifications to memory mapped
3118 * pages.
3120 * Note: to avoid loopback deadlocks, we do not
3121 * assign b_runningbufspace.
3123 bp->b_cmd = BUF_CMD_WRITE;
3124 vfs_busy_pages(bp->b_vp, bp);
3125 info->bvary[info->bvsize] = bp;
3126 toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3127 if (info->bvsize == 0 || toff < info->beg_off)
3128 info->beg_off = toff;
3129 toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3130 if (info->bvsize == 0 || toff > info->end_off)
3131 info->end_off = toff;
3132 ++info->bvsize;
3133 if (info->bvsize == NFS_COMMITBVECSIZ) {
3134 error = nfs_flush_docommit(info, 0);
3135 KKASSERT(info->bvsize == 0);
3137 crit_exit();
3139 return (error);
3142 static
3144 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3146 struct vnode *vp;
3147 struct buf *bp;
3148 off_t bytes;
3149 int retv;
3150 int i;
3152 vp = info->vp;
3154 if (info->bvsize > 0) {
3156 * Commit data on the server, as required. Note that
3157 * nfs_commit will use the vnode's cred for the commit.
3158 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3160 bytes = info->end_off - info->beg_off;
3161 if (bytes > 0x40000000)
3162 bytes = 0x40000000;
3163 if (error) {
3164 retv = -error;
3165 } else {
3166 retv = nfs_commit(vp, info->beg_off,
3167 (int)bytes, info->td);
3168 if (retv == NFSERR_STALEWRITEVERF)
3169 nfs_clearcommit(vp->v_mount);
3173 * Now, either mark the blocks I/O done or mark the
3174 * blocks dirty, depending on whether the commit
3175 * succeeded.
3177 for (i = 0; i < info->bvsize; ++i) {
3178 bp = info->bvary[i];
3179 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3180 if (retv) {
3182 * Error, leave B_DELWRI intact
3184 vfs_unbusy_pages(bp);
3185 bp->b_cmd = BUF_CMD_DONE;
3186 brelse(bp);
3187 } else {
3189 * Success, remove B_DELWRI ( bundirty() ).
3191 * b_dirtyoff/b_dirtyend seem to be NFS
3192 * specific. We should probably move that
3193 * into bundirty(). XXX
3195 * We are faking an I/O write, we have to
3196 * start the transaction in order to
3197 * immediately biodone() it.
3199 crit_enter();
3200 bp->b_flags |= B_ASYNC;
3201 bundirty(bp);
3202 bp->b_flags &= ~B_ERROR;
3203 bp->b_dirtyoff = bp->b_dirtyend = 0;
3204 crit_exit();
3205 biodone(&bp->b_bio1);
3208 info->bvsize = 0;
3210 return (error);
3214 * NFS advisory byte-level locks.
3215 * Currently unsupported.
3217 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3218 * int a_flags)
3220 static int
3221 nfs_advlock(struct vop_advlock_args *ap)
3223 struct nfsnode *np = VTONFS(ap->a_vp);
3226 * The following kludge is to allow diskless support to work
3227 * until a real NFS lockd is implemented. Basically, just pretend
3228 * that this is a local lock.
3230 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3234 * Print out the contents of an nfsnode.
3236 * nfs_print(struct vnode *a_vp)
3238 static int
3239 nfs_print(struct vop_print_args *ap)
3241 struct vnode *vp = ap->a_vp;
3242 struct nfsnode *np = VTONFS(vp);
3244 kprintf("tag VT_NFS, fileid %lld fsid 0x%x",
3245 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3246 if (vp->v_type == VFIFO)
3247 fifo_printinfo(vp);
3248 kprintf("\n");
3249 return (0);
3253 * nfs special file access vnode op.
3254 * Essentially just get vattr and then imitate iaccess() since the device is
3255 * local to the client.
3257 * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
3259 static int
3260 nfsspec_access(struct vop_access_args *ap)
3262 struct vattr *vap;
3263 gid_t *gp;
3264 struct ucred *cred = ap->a_cred;
3265 struct vnode *vp = ap->a_vp;
3266 mode_t mode = ap->a_mode;
3267 struct vattr vattr;
3268 int i;
3269 int error;
3272 * Disallow write attempts on filesystems mounted read-only;
3273 * unless the file is a socket, fifo, or a block or character
3274 * device resident on the filesystem.
3276 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3277 switch (vp->v_type) {
3278 case VREG:
3279 case VDIR:
3280 case VLNK:
3281 return (EROFS);
3282 default:
3283 break;
3287 * If you're the super-user,
3288 * you always get access.
3290 if (cred->cr_uid == 0)
3291 return (0);
3292 vap = &vattr;
3293 error = VOP_GETATTR(vp, vap);
3294 if (error)
3295 return (error);
3297 * Access check is based on only one of owner, group, public.
3298 * If not owner, then check group. If not a member of the
3299 * group, then check public access.
3301 if (cred->cr_uid != vap->va_uid) {
3302 mode >>= 3;
3303 gp = cred->cr_groups;
3304 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3305 if (vap->va_gid == *gp)
3306 goto found;
3307 mode >>= 3;
3308 found:
3311 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3312 return (error);
3316 * Read wrapper for special devices.
3318 * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3319 * struct ucred *a_cred)
3321 static int
3322 nfsspec_read(struct vop_read_args *ap)
3324 struct nfsnode *np = VTONFS(ap->a_vp);
3327 * Set access flag.
3329 np->n_flag |= NACC;
3330 getnanotime(&np->n_atim);
3331 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3335 * Write wrapper for special devices.
3337 * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3338 * struct ucred *a_cred)
3340 static int
3341 nfsspec_write(struct vop_write_args *ap)
3343 struct nfsnode *np = VTONFS(ap->a_vp);
3346 * Set update flag.
3348 np->n_flag |= NUPD;
3349 getnanotime(&np->n_mtim);
3350 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3354 * Close wrapper for special devices.
3356 * Update the times on the nfsnode then do device close.
3358 * nfsspec_close(struct vnode *a_vp, int a_fflag)
3360 static int
3361 nfsspec_close(struct vop_close_args *ap)
3363 struct vnode *vp = ap->a_vp;
3364 struct nfsnode *np = VTONFS(vp);
3365 struct vattr vattr;
3367 if (np->n_flag & (NACC | NUPD)) {
3368 np->n_flag |= NCHG;
3369 if (vp->v_sysref.refcnt == 1 &&
3370 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3371 VATTR_NULL(&vattr);
3372 if (np->n_flag & NACC)
3373 vattr.va_atime = np->n_atim;
3374 if (np->n_flag & NUPD)
3375 vattr.va_mtime = np->n_mtim;
3376 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3379 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3383 * Read wrapper for fifos.
3385 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3386 * struct ucred *a_cred)
3388 static int
3389 nfsfifo_read(struct vop_read_args *ap)
3391 struct nfsnode *np = VTONFS(ap->a_vp);
3394 * Set access flag.
3396 np->n_flag |= NACC;
3397 getnanotime(&np->n_atim);
3398 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3402 * Write wrapper for fifos.
3404 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3405 * struct ucred *a_cred)
3407 static int
3408 nfsfifo_write(struct vop_write_args *ap)
3410 struct nfsnode *np = VTONFS(ap->a_vp);
3413 * Set update flag.
3415 np->n_flag |= NUPD;
3416 getnanotime(&np->n_mtim);
3417 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3421 * Close wrapper for fifos.
3423 * Update the times on the nfsnode then do fifo close.
3425 * nfsfifo_close(struct vnode *a_vp, int a_fflag)
3427 static int
3428 nfsfifo_close(struct vop_close_args *ap)
3430 struct vnode *vp = ap->a_vp;
3431 struct nfsnode *np = VTONFS(vp);
3432 struct vattr vattr;
3433 struct timespec ts;
3435 if (np->n_flag & (NACC | NUPD)) {
3436 getnanotime(&ts);
3437 if (np->n_flag & NACC)
3438 np->n_atim = ts;
3439 if (np->n_flag & NUPD)
3440 np->n_mtim = ts;
3441 np->n_flag |= NCHG;
3442 if (vp->v_sysref.refcnt == 1 &&
3443 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3444 VATTR_NULL(&vattr);
3445 if (np->n_flag & NACC)
3446 vattr.va_atime = np->n_atim;
3447 if (np->n_flag & NUPD)
3448 vattr.va_mtime = np->n_mtim;
3449 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3452 return (VOCALL(&fifo_vnode_vops, &ap->a_head));