Fix a lock leak in nfs_create(), tracked down from a crash dump and
[dragonfly.git] / sys / vfs / nfs / nfs_vnops.c
blob14d9bb9bd3e7567f8c5bba5b3a7395d8698b4ca6
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.79 2008/07/16 18:20:40 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");
233 static int nfsaccess_cache_timeout = NFS_DEFATTRTIMO;
234 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
235 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
237 static int nfsneg_cache_timeout = NFS_MINATTRTIMO;
238 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
239 &nfsneg_cache_timeout, 0, "NFS NEGATIVE NAMECACHE timeout");
241 static int nfspos_cache_timeout = NFS_MINATTRTIMO;
242 SYSCTL_INT(_vfs_nfs, OID_AUTO, pos_cache_timeout, CTLFLAG_RW,
243 &nfspos_cache_timeout, 0, "NFS POSITIVE NAMECACHE timeout");
245 static int nfsv3_commit_on_close = 0;
246 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
247 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
248 #if 0
249 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
250 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
252 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
253 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
254 #endif
256 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
257 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
258 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
259 static int
260 nfs3_access_otw(struct vnode *vp, int wmode,
261 struct thread *td, struct ucred *cred)
263 const int v3 = 1;
264 u_int32_t *tl;
265 int error = 0, attrflag;
267 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
268 caddr_t bpos, dpos, cp2;
269 int32_t t1, t2;
270 caddr_t cp;
271 u_int32_t rmode;
272 struct nfsnode *np = VTONFS(vp);
274 nfsstats.rpccnt[NFSPROC_ACCESS]++;
275 nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
276 nfsm_fhtom(vp, v3);
277 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
278 *tl = txdr_unsigned(wmode);
279 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
280 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
281 if (!error) {
282 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
283 rmode = fxdr_unsigned(u_int32_t, *tl);
284 np->n_mode = rmode;
285 np->n_modeuid = cred->cr_uid;
286 np->n_modestamp = mycpu->gd_time_seconds;
288 m_freem(mrep);
289 nfsmout:
290 return error;
294 * nfs access vnode op.
295 * For nfs version 2, just return ok. File accesses may fail later.
296 * For nfs version 3, use the access rpc to check accessibility. If file modes
297 * are changed on the server, accesses might still fail later.
299 * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
301 static int
302 nfs_access(struct vop_access_args *ap)
304 struct vnode *vp = ap->a_vp;
305 thread_t td = curthread;
306 int error = 0;
307 u_int32_t mode, wmode;
308 int v3 = NFS_ISV3(vp);
309 struct nfsnode *np = VTONFS(vp);
312 * Disallow write attempts on filesystems mounted read-only;
313 * unless the file is a socket, fifo, or a block or character
314 * device resident on the filesystem.
316 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
317 switch (vp->v_type) {
318 case VREG:
319 case VDIR:
320 case VLNK:
321 return (EROFS);
322 default:
323 break;
327 * For nfs v3, check to see if we have done this recently, and if
328 * so return our cached result instead of making an ACCESS call.
329 * If not, do an access rpc, otherwise you are stuck emulating
330 * ufs_access() locally using the vattr. This may not be correct,
331 * since the server may apply other access criteria such as
332 * client uid-->server uid mapping that we do not know about.
334 if (v3) {
335 if (ap->a_mode & VREAD)
336 mode = NFSV3ACCESS_READ;
337 else
338 mode = 0;
339 if (vp->v_type != VDIR) {
340 if (ap->a_mode & VWRITE)
341 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
342 if (ap->a_mode & VEXEC)
343 mode |= NFSV3ACCESS_EXECUTE;
344 } else {
345 if (ap->a_mode & VWRITE)
346 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
347 NFSV3ACCESS_DELETE);
348 if (ap->a_mode & VEXEC)
349 mode |= NFSV3ACCESS_LOOKUP;
351 /* XXX safety belt, only make blanket request if caching */
352 if (nfsaccess_cache_timeout > 0) {
353 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
354 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
355 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
356 } else {
357 wmode = mode;
361 * Does our cached result allow us to give a definite yes to
362 * this request?
364 if (np->n_modestamp &&
365 (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
366 (ap->a_cred->cr_uid == np->n_modeuid) &&
367 ((np->n_mode & mode) == mode)) {
368 nfsstats.accesscache_hits++;
369 } else {
371 * Either a no, or a don't know. Go to the wire.
373 nfsstats.accesscache_misses++;
374 error = nfs3_access_otw(vp, wmode, td, ap->a_cred);
375 if (!error) {
376 if ((np->n_mode & mode) != mode) {
377 error = EACCES;
381 } else {
382 if ((error = nfsspec_access(ap)) != 0)
383 return (error);
386 * Attempt to prevent a mapped root from accessing a file
387 * which it shouldn't. We try to read a byte from the file
388 * if the user is root and the file is not zero length.
389 * After calling nfsspec_access, we should have the correct
390 * file size cached.
392 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
393 && VTONFS(vp)->n_size > 0) {
394 struct iovec aiov;
395 struct uio auio;
396 char buf[1];
398 aiov.iov_base = buf;
399 aiov.iov_len = 1;
400 auio.uio_iov = &aiov;
401 auio.uio_iovcnt = 1;
402 auio.uio_offset = 0;
403 auio.uio_resid = 1;
404 auio.uio_segflg = UIO_SYSSPACE;
405 auio.uio_rw = UIO_READ;
406 auio.uio_td = td;
408 if (vp->v_type == VREG) {
409 error = nfs_readrpc(vp, &auio);
410 } else if (vp->v_type == VDIR) {
411 char* bp;
412 bp = kmalloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
413 aiov.iov_base = bp;
414 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
415 error = nfs_readdirrpc(vp, &auio);
416 kfree(bp, M_TEMP);
417 } else if (vp->v_type == VLNK) {
418 error = nfs_readlinkrpc(vp, &auio);
419 } else {
420 error = EACCES;
425 * [re]record creds for reading and/or writing if access
426 * was granted. Assume the NFS server will grant read access
427 * for execute requests.
429 if (error == 0) {
430 if ((ap->a_mode & (VREAD|VEXEC)) && ap->a_cred != np->n_rucred) {
431 crhold(ap->a_cred);
432 if (np->n_rucred)
433 crfree(np->n_rucred);
434 np->n_rucred = ap->a_cred;
436 if ((ap->a_mode & VWRITE) && ap->a_cred != np->n_wucred) {
437 crhold(ap->a_cred);
438 if (np->n_wucred)
439 crfree(np->n_wucred);
440 np->n_wucred = ap->a_cred;
443 return(error);
447 * nfs open vnode op
448 * Check to see if the type is ok
449 * and that deletion is not in progress.
450 * For paged in text files, you will need to flush the page cache
451 * if consistency is lost.
453 * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
454 * struct file *a_fp)
456 /* ARGSUSED */
457 static int
458 nfs_open(struct vop_open_args *ap)
460 struct vnode *vp = ap->a_vp;
461 struct nfsnode *np = VTONFS(vp);
462 struct vattr vattr;
463 int error;
465 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
466 #ifdef DIAGNOSTIC
467 kprintf("open eacces vtyp=%d\n",vp->v_type);
468 #endif
469 return (EOPNOTSUPP);
473 * Clear the attribute cache only if opening with write access. It
474 * is unclear if we should do this at all here, but we certainly
475 * should not clear the cache unconditionally simply because a file
476 * is being opened.
478 if (ap->a_mode & FWRITE)
479 np->n_attrstamp = 0;
482 * For normal NFS, reconcile changes made locally verses
483 * changes made remotely. Note that VOP_GETATTR only goes
484 * to the wire if the cached attribute has timed out or been
485 * cleared.
487 * If local modifications have been made clear the attribute
488 * cache to force an attribute and modified time check. If
489 * GETATTR detects that the file has been changed by someone
490 * other then us it will set NRMODIFIED.
492 * If we are opening a directory and local changes have been
493 * made we have to invalidate the cache in order to ensure
494 * that we get the most up-to-date information from the
495 * server. XXX
497 if (np->n_flag & NLMODIFIED) {
498 np->n_attrstamp = 0;
499 if (vp->v_type == VDIR) {
500 error = nfs_vinvalbuf(vp, V_SAVE, 1);
501 if (error == EINTR)
502 return (error);
503 nfs_invaldir(vp);
506 error = VOP_GETATTR(vp, &vattr);
507 if (error)
508 return (error);
509 if (np->n_flag & NRMODIFIED) {
510 if (vp->v_type == VDIR)
511 nfs_invaldir(vp);
512 error = nfs_vinvalbuf(vp, V_SAVE, 1);
513 if (error == EINTR)
514 return (error);
515 np->n_flag &= ~NRMODIFIED;
518 return (vop_stdopen(ap));
522 * nfs close vnode op
523 * What an NFS client should do upon close after writing is a debatable issue.
524 * Most NFS clients push delayed writes to the server upon close, basically for
525 * two reasons:
526 * 1 - So that any write errors may be reported back to the client process
527 * doing the close system call. By far the two most likely errors are
528 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
529 * 2 - To put a worst case upper bound on cache inconsistency between
530 * multiple clients for the file.
531 * There is also a consistency problem for Version 2 of the protocol w.r.t.
532 * not being able to tell if other clients are writing a file concurrently,
533 * since there is no way of knowing if the changed modify time in the reply
534 * is only due to the write for this client.
535 * (NFS Version 3 provides weak cache consistency data in the reply that
536 * should be sufficient to detect and handle this case.)
538 * The current code does the following:
539 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
540 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
541 * or commit them (this satisfies 1 and 2 except for the
542 * case where the server crashes after this close but
543 * before the commit RPC, which is felt to be "good
544 * enough". Changing the last argument to nfs_flush() to
545 * a 1 would force a commit operation, if it is felt a
546 * commit is necessary now.
547 * for NQNFS - do nothing now, since 2 is dealt with via leases and
548 * 1 should be dealt with via an fsync() system call for
549 * cases where write errors are important.
551 * nfs_close(struct vnode *a_vp, int a_fflag)
553 /* ARGSUSED */
554 static int
555 nfs_close(struct vop_close_args *ap)
557 struct vnode *vp = ap->a_vp;
558 struct nfsnode *np = VTONFS(vp);
559 int error = 0;
560 thread_t td = curthread;
562 if (vp->v_type == VREG) {
563 if (np->n_flag & NLMODIFIED) {
564 if (NFS_ISV3(vp)) {
566 * Under NFSv3 we have dirty buffers to dispose of. We
567 * must flush them to the NFS server. We have the option
568 * of waiting all the way through the commit rpc or just
569 * waiting for the initial write. The default is to only
570 * wait through the initial write so the data is in the
571 * server's cache, which is roughly similar to the state
572 * a standard disk subsystem leaves the file in on close().
574 * We cannot clear the NLMODIFIED bit in np->n_flag due to
575 * potential races with other processes, and certainly
576 * cannot clear it if we don't commit.
578 int cm = nfsv3_commit_on_close ? 1 : 0;
579 error = nfs_flush(vp, MNT_WAIT, td, cm);
580 /* np->n_flag &= ~NLMODIFIED; */
581 } else {
582 error = nfs_vinvalbuf(vp, V_SAVE, 1);
584 np->n_attrstamp = 0;
586 if (np->n_flag & NWRITEERR) {
587 np->n_flag &= ~NWRITEERR;
588 error = np->n_error;
591 vop_stdclose(ap);
592 return (error);
596 * nfs getattr call from vfs.
598 * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
600 static int
601 nfs_getattr(struct vop_getattr_args *ap)
603 struct vnode *vp = ap->a_vp;
604 struct nfsnode *np = VTONFS(vp);
605 caddr_t cp;
606 u_int32_t *tl;
607 int32_t t1, t2;
608 caddr_t bpos, dpos;
609 int error = 0;
610 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
611 int v3 = NFS_ISV3(vp);
612 thread_t td = curthread;
615 * Update local times for special files.
617 if (np->n_flag & (NACC | NUPD))
618 np->n_flag |= NCHG;
620 * First look in the cache.
622 if (nfs_getattrcache(vp, ap->a_vap) == 0)
623 return (0);
625 if (v3 && nfsaccess_cache_timeout > 0) {
626 nfsstats.accesscache_misses++;
627 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, nfs_vpcred(vp, ND_CHECK));
628 if (nfs_getattrcache(vp, ap->a_vap) == 0)
629 return (0);
632 nfsstats.rpccnt[NFSPROC_GETATTR]++;
633 nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
634 nfsm_fhtom(vp, v3);
635 nfsm_request(vp, NFSPROC_GETATTR, td, nfs_vpcred(vp, ND_CHECK));
636 if (!error) {
637 nfsm_loadattr(vp, ap->a_vap);
639 m_freem(mrep);
640 nfsmout:
641 return (error);
645 * nfs setattr call.
647 * nfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
649 static int
650 nfs_setattr(struct vop_setattr_args *ap)
652 struct vnode *vp = ap->a_vp;
653 struct nfsnode *np = VTONFS(vp);
654 struct vattr *vap = ap->a_vap;
655 int error = 0;
656 u_quad_t tsize;
657 thread_t td = curthread;
659 #ifndef nolint
660 tsize = (u_quad_t)0;
661 #endif
664 * Setting of flags is not supported.
666 if (vap->va_flags != VNOVAL)
667 return (EOPNOTSUPP);
670 * Disallow write attempts if the filesystem is mounted read-only.
672 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
673 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
674 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
675 (vp->v_mount->mnt_flag & MNT_RDONLY))
676 return (EROFS);
678 if (vap->va_size != VNOVAL) {
680 * truncation requested
682 switch (vp->v_type) {
683 case VDIR:
684 return (EISDIR);
685 case VCHR:
686 case VBLK:
687 case VSOCK:
688 case VFIFO:
689 if (vap->va_mtime.tv_sec == VNOVAL &&
690 vap->va_atime.tv_sec == VNOVAL &&
691 vap->va_mode == (mode_t)VNOVAL &&
692 vap->va_uid == (uid_t)VNOVAL &&
693 vap->va_gid == (gid_t)VNOVAL)
694 return (0);
695 vap->va_size = VNOVAL;
696 break;
697 default:
699 * Disallow write attempts if the filesystem is
700 * mounted read-only.
702 if (vp->v_mount->mnt_flag & MNT_RDONLY)
703 return (EROFS);
706 * This is nasty. The RPCs we send to flush pending
707 * data often return attribute information which is
708 * cached via a callback to nfs_loadattrcache(), which
709 * has the effect of changing our notion of the file
710 * size. Due to flushed appends and other operations
711 * the file size can be set to virtually anything,
712 * including values that do not match either the old
713 * or intended file size.
715 * When this condition is detected we must loop to
716 * try the operation again. Hopefully no more
717 * flushing is required on the loop so it works the
718 * second time around. THIS CASE ALMOST ALWAYS
719 * HAPPENS!
721 tsize = np->n_size;
722 again:
723 error = nfs_meta_setsize(vp, td, vap->va_size);
725 if (np->n_flag & NLMODIFIED) {
726 if (vap->va_size == 0)
727 error = nfs_vinvalbuf(vp, 0, 1);
728 else
729 error = nfs_vinvalbuf(vp, V_SAVE, 1);
732 * note: this loop case almost always happens at
733 * least once per truncation.
735 if (error == 0 && np->n_size != vap->va_size)
736 goto again;
737 np->n_vattr.va_size = vap->va_size;
738 break;
740 } else if ((np->n_flag & NLMODIFIED) && vp->v_type == VREG) {
742 * What to do. If we are modifying the mtime we lose
743 * mtime detection of changes made by the server or other
744 * clients. But programs like rsync/rdist/cpdup are going
745 * to call utimes a lot. We don't want to piecemeal sync.
747 * For now sync if any prior remote changes were detected,
748 * but allow us to lose track of remote changes made during
749 * the utimes operation.
751 if (np->n_flag & NRMODIFIED)
752 error = nfs_vinvalbuf(vp, V_SAVE, 1);
753 if (error == EINTR)
754 return (error);
755 if (error == 0) {
756 if (vap->va_mtime.tv_sec != VNOVAL) {
757 np->n_mtime = vap->va_mtime.tv_sec;
761 error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
764 * Sanity check if a truncation was issued. This should only occur
765 * if multiple processes are racing on the same file.
767 if (error == 0 && vap->va_size != VNOVAL &&
768 np->n_size != vap->va_size) {
769 kprintf("NFS ftruncate: server disagrees on the file size: %lld/%lld/%lld\n", tsize, vap->va_size, np->n_size);
770 goto again;
772 if (error && vap->va_size != VNOVAL) {
773 np->n_size = np->n_vattr.va_size = tsize;
774 vnode_pager_setsize(vp, np->n_size);
776 return (error);
780 * Do an nfs setattr rpc.
782 static int
783 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
784 struct ucred *cred, struct thread *td)
786 struct nfsv2_sattr *sp;
787 struct nfsnode *np = VTONFS(vp);
788 caddr_t cp;
789 int32_t t1, t2;
790 caddr_t bpos, dpos, cp2;
791 u_int32_t *tl;
792 int error = 0, wccflag = NFSV3_WCCRATTR;
793 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
794 int v3 = NFS_ISV3(vp);
796 nfsstats.rpccnt[NFSPROC_SETATTR]++;
797 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
798 nfsm_fhtom(vp, v3);
799 if (v3) {
800 nfsm_v3attrbuild(vap, TRUE);
801 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
802 *tl = nfs_false;
803 } else {
804 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
805 if (vap->va_mode == (mode_t)VNOVAL)
806 sp->sa_mode = nfs_xdrneg1;
807 else
808 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
809 if (vap->va_uid == (uid_t)VNOVAL)
810 sp->sa_uid = nfs_xdrneg1;
811 else
812 sp->sa_uid = txdr_unsigned(vap->va_uid);
813 if (vap->va_gid == (gid_t)VNOVAL)
814 sp->sa_gid = nfs_xdrneg1;
815 else
816 sp->sa_gid = txdr_unsigned(vap->va_gid);
817 sp->sa_size = txdr_unsigned(vap->va_size);
818 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
819 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
821 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
822 if (v3) {
823 np->n_modestamp = 0;
824 nfsm_wcc_data(vp, wccflag);
825 } else
826 nfsm_loadattr(vp, (struct vattr *)0);
827 m_freem(mrep);
828 nfsmout:
829 return (error);
832 static
833 void
834 nfs_cache_setvp(struct nchandle *nch, struct vnode *vp, int nctimeout)
836 if (nctimeout == 0)
837 nctimeout = 1;
838 else
839 nctimeout *= hz;
840 cache_setvp(nch, vp);
841 cache_settimeout(nch, nctimeout);
845 * NEW API CALL - replaces nfs_lookup(). However, we cannot remove
846 * nfs_lookup() until all remaining new api calls are implemented.
848 * Resolve a namecache entry. This function is passed a locked ncp and
849 * must call nfs_cache_setvp() on it as appropriate to resolve the entry.
851 static int
852 nfs_nresolve(struct vop_nresolve_args *ap)
854 struct thread *td = curthread;
855 struct namecache *ncp;
856 struct ucred *cred;
857 struct nfsnode *np;
858 struct vnode *dvp;
859 struct vnode *nvp;
860 nfsfh_t *fhp;
861 int attrflag;
862 int fhsize;
863 int error;
864 int len;
865 int v3;
866 /******NFSM MACROS********/
867 struct mbuf *mb, *mrep, *mreq, *mb2, *md;
868 caddr_t bpos, dpos, cp, cp2;
869 u_int32_t *tl;
870 int32_t t1, t2;
872 cred = ap->a_cred;
873 dvp = ap->a_dvp;
875 if ((error = vget(dvp, LK_SHARED)) != 0)
876 return (error);
878 nvp = NULL;
879 v3 = NFS_ISV3(dvp);
880 nfsstats.lookupcache_misses++;
881 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
882 ncp = ap->a_nch->ncp;
883 len = ncp->nc_nlen;
884 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
885 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
886 nfsm_fhtom(dvp, v3);
887 nfsm_strtom(ncp->nc_name, len, NFS_MAXNAMLEN);
888 nfsm_request(dvp, NFSPROC_LOOKUP, td, ap->a_cred);
889 if (error) {
891 * Cache negatve lookups to reduce NFS traffic, but use
892 * a fast timeout. Otherwise use a timeout of 1 tick.
893 * XXX we should add a namecache flag for no-caching
894 * to uncache the negative hit as soon as possible, but
895 * we cannot simply destroy the entry because it is used
896 * as a placeholder by the caller.
898 if (error == ENOENT)
899 nfs_cache_setvp(ap->a_nch, NULL, nfsneg_cache_timeout);
900 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
901 m_freem(mrep);
902 goto nfsmout;
906 * Success, get the file handle, do various checks, and load
907 * post-operation data from the reply packet. Theoretically
908 * we should never be looking up "." so, theoretically, we
909 * should never get the same file handle as our directory. But
910 * we check anyway. XXX
912 * Note that no timeout is set for the positive cache hit. We
913 * assume, theoretically, that ESTALE returns will be dealt with
914 * properly to handle NFS races and in anycase we cannot depend
915 * on a timeout to deal with NFS open/create/excl issues so instead
916 * of a bad hack here the rest of the NFS client code needs to do
917 * the right thing.
919 nfsm_getfh(fhp, fhsize, v3);
921 np = VTONFS(dvp);
922 if (NFS_CMPFH(np, fhp, fhsize)) {
923 vref(dvp);
924 nvp = dvp;
925 } else {
926 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
927 if (error) {
928 m_freem(mrep);
929 vput(dvp);
930 return (error);
932 nvp = NFSTOV(np);
934 if (v3) {
935 nfsm_postop_attr(nvp, attrflag, NFS_LATTR_NOSHRINK);
936 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
937 } else {
938 nfsm_loadattr(nvp, NULL);
940 nfs_cache_setvp(ap->a_nch, nvp, nfspos_cache_timeout);
941 m_freem(mrep);
942 nfsmout:
943 vput(dvp);
944 if (nvp) {
945 if (nvp == dvp)
946 vrele(nvp);
947 else
948 vput(nvp);
950 return (error);
954 * 'cached' nfs directory lookup
956 * NOTE: cannot be removed until NFS implements all the new n*() API calls.
958 * nfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
959 * struct componentname *a_cnp)
961 static int
962 nfs_lookup(struct vop_old_lookup_args *ap)
964 struct componentname *cnp = ap->a_cnp;
965 struct vnode *dvp = ap->a_dvp;
966 struct vnode **vpp = ap->a_vpp;
967 int flags = cnp->cn_flags;
968 struct vnode *newvp;
969 u_int32_t *tl;
970 caddr_t cp;
971 int32_t t1, t2;
972 struct nfsmount *nmp;
973 caddr_t bpos, dpos, cp2;
974 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
975 long len;
976 nfsfh_t *fhp;
977 struct nfsnode *np;
978 int lockparent, wantparent, error = 0, attrflag, fhsize;
979 int v3 = NFS_ISV3(dvp);
982 * Read-only mount check and directory check.
984 *vpp = NULLVP;
985 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
986 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
987 return (EROFS);
989 if (dvp->v_type != VDIR)
990 return (ENOTDIR);
993 * Look it up in the cache. Note that ENOENT is only returned if we
994 * previously entered a negative hit (see later on). The additional
995 * nfsneg_cache_timeout check causes previously cached results to
996 * be instantly ignored if the negative caching is turned off.
998 lockparent = flags & CNP_LOCKPARENT;
999 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1000 nmp = VFSTONFS(dvp->v_mount);
1001 np = VTONFS(dvp);
1004 * Go to the wire.
1006 error = 0;
1007 newvp = NULLVP;
1008 nfsstats.lookupcache_misses++;
1009 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1010 len = cnp->cn_namelen;
1011 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
1012 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1013 nfsm_fhtom(dvp, v3);
1014 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1015 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
1016 if (error) {
1017 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1018 m_freem(mrep);
1019 goto nfsmout;
1021 nfsm_getfh(fhp, fhsize, v3);
1024 * Handle RENAME case...
1026 if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1027 if (NFS_CMPFH(np, fhp, fhsize)) {
1028 m_freem(mrep);
1029 return (EISDIR);
1031 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1032 if (error) {
1033 m_freem(mrep);
1034 return (error);
1036 newvp = NFSTOV(np);
1037 if (v3) {
1038 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1039 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1040 } else
1041 nfsm_loadattr(newvp, (struct vattr *)0);
1042 *vpp = newvp;
1043 m_freem(mrep);
1044 if (!lockparent) {
1045 vn_unlock(dvp);
1046 cnp->cn_flags |= CNP_PDIRUNLOCK;
1048 return (0);
1051 if (flags & CNP_ISDOTDOT) {
1052 vn_unlock(dvp);
1053 cnp->cn_flags |= CNP_PDIRUNLOCK;
1054 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1055 if (error) {
1056 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1057 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1058 return (error); /* NOTE: return error from nget */
1060 newvp = NFSTOV(np);
1061 if (lockparent) {
1062 error = vn_lock(dvp, LK_EXCLUSIVE);
1063 if (error) {
1064 vput(newvp);
1065 return (error);
1067 cnp->cn_flags |= CNP_PDIRUNLOCK;
1069 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1070 vref(dvp);
1071 newvp = dvp;
1072 } else {
1073 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1074 if (error) {
1075 m_freem(mrep);
1076 return (error);
1078 if (!lockparent) {
1079 vn_unlock(dvp);
1080 cnp->cn_flags |= CNP_PDIRUNLOCK;
1082 newvp = NFSTOV(np);
1084 if (v3) {
1085 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1086 nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1087 } else
1088 nfsm_loadattr(newvp, (struct vattr *)0);
1089 #if 0
1090 /* XXX MOVE TO nfs_nremove() */
1091 if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1092 cnp->cn_nameiop != NAMEI_DELETE) {
1093 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1095 #endif
1096 *vpp = newvp;
1097 m_freem(mrep);
1098 nfsmout:
1099 if (error) {
1100 if (newvp != NULLVP) {
1101 vrele(newvp);
1102 *vpp = NULLVP;
1104 if ((cnp->cn_nameiop == NAMEI_CREATE ||
1105 cnp->cn_nameiop == NAMEI_RENAME) &&
1106 error == ENOENT) {
1107 if (!lockparent) {
1108 vn_unlock(dvp);
1109 cnp->cn_flags |= CNP_PDIRUNLOCK;
1111 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1112 error = EROFS;
1113 else
1114 error = EJUSTRETURN;
1117 return (error);
1121 * nfs read call.
1122 * Just call nfs_bioread() to do the work.
1124 * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1125 * struct ucred *a_cred)
1127 static int
1128 nfs_read(struct vop_read_args *ap)
1130 struct vnode *vp = ap->a_vp;
1132 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1133 switch (vp->v_type) {
1134 case VREG:
1135 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1136 case VDIR:
1137 return (EISDIR);
1138 default:
1139 return EOPNOTSUPP;
1144 * nfs readlink call
1146 * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1148 static int
1149 nfs_readlink(struct vop_readlink_args *ap)
1151 struct vnode *vp = ap->a_vp;
1153 if (vp->v_type != VLNK)
1154 return (EINVAL);
1155 return (nfs_bioread(vp, ap->a_uio, 0));
1159 * Do a readlink rpc.
1160 * Called by nfs_doio() from below the buffer cache.
1163 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1165 u_int32_t *tl;
1166 caddr_t cp;
1167 int32_t t1, t2;
1168 caddr_t bpos, dpos, cp2;
1169 int error = 0, len, attrflag;
1170 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1171 int v3 = NFS_ISV3(vp);
1173 nfsstats.rpccnt[NFSPROC_READLINK]++;
1174 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1175 nfsm_fhtom(vp, v3);
1176 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1177 if (v3)
1178 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1179 if (!error) {
1180 nfsm_strsiz(len, NFS_MAXPATHLEN);
1181 if (len == NFS_MAXPATHLEN) {
1182 struct nfsnode *np = VTONFS(vp);
1183 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1184 len = np->n_size;
1186 nfsm_mtouio(uiop, len);
1188 m_freem(mrep);
1189 nfsmout:
1190 return (error);
1194 * nfs read rpc call
1195 * Ditto above
1198 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1200 u_int32_t *tl;
1201 caddr_t cp;
1202 int32_t t1, t2;
1203 caddr_t bpos, dpos, cp2;
1204 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1205 struct nfsmount *nmp;
1206 int error = 0, len, retlen, tsiz, eof, attrflag;
1207 int v3 = NFS_ISV3(vp);
1209 #ifndef nolint
1210 eof = 0;
1211 #endif
1212 nmp = VFSTONFS(vp->v_mount);
1213 tsiz = uiop->uio_resid;
1214 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1215 return (EFBIG);
1216 while (tsiz > 0) {
1217 nfsstats.rpccnt[NFSPROC_READ]++;
1218 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1219 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1220 nfsm_fhtom(vp, v3);
1221 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1222 if (v3) {
1223 txdr_hyper(uiop->uio_offset, tl);
1224 *(tl + 2) = txdr_unsigned(len);
1225 } else {
1226 *tl++ = txdr_unsigned(uiop->uio_offset);
1227 *tl++ = txdr_unsigned(len);
1228 *tl = 0;
1230 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1231 if (v3) {
1232 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1233 if (error) {
1234 m_freem(mrep);
1235 goto nfsmout;
1237 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1238 eof = fxdr_unsigned(int, *(tl + 1));
1239 } else
1240 nfsm_loadattr(vp, (struct vattr *)0);
1241 nfsm_strsiz(retlen, nmp->nm_rsize);
1242 nfsm_mtouio(uiop, retlen);
1243 m_freem(mrep);
1244 tsiz -= retlen;
1245 if (v3) {
1246 if (eof || retlen == 0) {
1247 tsiz = 0;
1249 } else if (retlen < len) {
1250 tsiz = 0;
1253 nfsmout:
1254 return (error);
1258 * nfs write call
1261 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, int *must_commit)
1263 u_int32_t *tl;
1264 caddr_t cp;
1265 int32_t t1, t2, backup;
1266 caddr_t bpos, dpos, cp2;
1267 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1268 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1269 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1270 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1272 #ifndef DIAGNOSTIC
1273 if (uiop->uio_iovcnt != 1)
1274 panic("nfs: writerpc iovcnt > 1");
1275 #endif
1276 *must_commit = 0;
1277 tsiz = uiop->uio_resid;
1278 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1279 return (EFBIG);
1280 while (tsiz > 0) {
1281 nfsstats.rpccnt[NFSPROC_WRITE]++;
1282 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1283 nfsm_reqhead(vp, NFSPROC_WRITE,
1284 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1285 nfsm_fhtom(vp, v3);
1286 if (v3) {
1287 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1288 txdr_hyper(uiop->uio_offset, tl);
1289 tl += 2;
1290 *tl++ = txdr_unsigned(len);
1291 *tl++ = txdr_unsigned(*iomode);
1292 *tl = txdr_unsigned(len);
1293 } else {
1294 u_int32_t x;
1296 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1297 /* Set both "begin" and "current" to non-garbage. */
1298 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1299 *tl++ = x; /* "begin offset" */
1300 *tl++ = x; /* "current offset" */
1301 x = txdr_unsigned(len);
1302 *tl++ = x; /* total to this offset */
1303 *tl = x; /* size of this write */
1305 nfsm_uiotom(uiop, len);
1306 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1307 if (v3) {
1309 * The write RPC returns a before and after mtime. The
1310 * nfsm_wcc_data() macro checks the before n_mtime
1311 * against the before time and stores the after time
1312 * in the nfsnode's cached vattr and n_mtime field.
1313 * The NRMODIFIED bit will be set if the before
1314 * time did not match the original mtime.
1316 wccflag = NFSV3_WCCCHK;
1317 nfsm_wcc_data(vp, wccflag);
1318 if (!error) {
1319 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1320 + NFSX_V3WRITEVERF);
1321 rlen = fxdr_unsigned(int, *tl++);
1322 if (rlen == 0) {
1323 error = NFSERR_IO;
1324 m_freem(mrep);
1325 break;
1326 } else if (rlen < len) {
1327 backup = len - rlen;
1328 uiop->uio_iov->iov_base -= backup;
1329 uiop->uio_iov->iov_len += backup;
1330 uiop->uio_offset -= backup;
1331 uiop->uio_resid += backup;
1332 len = rlen;
1334 commit = fxdr_unsigned(int, *tl++);
1337 * Return the lowest committment level
1338 * obtained by any of the RPCs.
1340 if (committed == NFSV3WRITE_FILESYNC)
1341 committed = commit;
1342 else if (committed == NFSV3WRITE_DATASYNC &&
1343 commit == NFSV3WRITE_UNSTABLE)
1344 committed = commit;
1345 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1346 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1347 NFSX_V3WRITEVERF);
1348 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1349 } else if (bcmp((caddr_t)tl,
1350 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1351 *must_commit = 1;
1352 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1353 NFSX_V3WRITEVERF);
1356 } else {
1357 nfsm_loadattr(vp, (struct vattr *)0);
1359 m_freem(mrep);
1360 if (error)
1361 break;
1362 tsiz -= len;
1364 nfsmout:
1365 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1366 committed = NFSV3WRITE_FILESYNC;
1367 *iomode = committed;
1368 if (error)
1369 uiop->uio_resid = tsiz;
1370 return (error);
1374 * nfs mknod rpc
1375 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1376 * mode set to specify the file type and the size field for rdev.
1378 static int
1379 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1380 struct vattr *vap)
1382 struct nfsv2_sattr *sp;
1383 u_int32_t *tl;
1384 caddr_t cp;
1385 int32_t t1, t2;
1386 struct vnode *newvp = (struct vnode *)0;
1387 struct nfsnode *np = (struct nfsnode *)0;
1388 struct vattr vattr;
1389 char *cp2;
1390 caddr_t bpos, dpos;
1391 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1392 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1393 int rmajor, rminor;
1394 int v3 = NFS_ISV3(dvp);
1396 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1397 rmajor = txdr_unsigned(vap->va_rmajor);
1398 rminor = txdr_unsigned(vap->va_rminor);
1399 } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) {
1400 rmajor = nfs_xdrneg1;
1401 rminor = nfs_xdrneg1;
1402 } else {
1403 return (EOPNOTSUPP);
1405 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1406 return (error);
1408 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1409 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1410 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1411 nfsm_fhtom(dvp, v3);
1412 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1413 if (v3) {
1414 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1415 *tl++ = vtonfsv3_type(vap->va_type);
1416 nfsm_v3attrbuild(vap, FALSE);
1417 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1418 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1419 *tl++ = txdr_unsigned(vap->va_rmajor);
1420 *tl = txdr_unsigned(vap->va_rminor);
1422 } else {
1423 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1424 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1425 sp->sa_uid = nfs_xdrneg1;
1426 sp->sa_gid = nfs_xdrneg1;
1427 sp->sa_size = makeudev(rmajor, rminor);
1428 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1429 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1431 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1432 if (!error) {
1433 nfsm_mtofh(dvp, newvp, v3, gotvp);
1434 if (!gotvp) {
1435 if (newvp) {
1436 vput(newvp);
1437 newvp = (struct vnode *)0;
1439 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1440 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1441 if (!error)
1442 newvp = NFSTOV(np);
1445 if (v3)
1446 nfsm_wcc_data(dvp, wccflag);
1447 m_freem(mrep);
1448 nfsmout:
1449 if (error) {
1450 if (newvp)
1451 vput(newvp);
1452 } else {
1453 *vpp = newvp;
1455 VTONFS(dvp)->n_flag |= NLMODIFIED;
1456 if (!wccflag)
1457 VTONFS(dvp)->n_attrstamp = 0;
1458 return (error);
1462 * nfs mknod vop
1463 * just call nfs_mknodrpc() to do the work.
1465 * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1466 * struct componentname *a_cnp, struct vattr *a_vap)
1468 /* ARGSUSED */
1469 static int
1470 nfs_mknod(struct vop_old_mknod_args *ap)
1472 return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1475 static u_long create_verf;
1477 * nfs file create call
1479 * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1480 * struct componentname *a_cnp, struct vattr *a_vap)
1482 static int
1483 nfs_create(struct vop_old_create_args *ap)
1485 struct vnode *dvp = ap->a_dvp;
1486 struct vattr *vap = ap->a_vap;
1487 struct componentname *cnp = ap->a_cnp;
1488 struct nfsv2_sattr *sp;
1489 u_int32_t *tl;
1490 caddr_t cp;
1491 int32_t t1, t2;
1492 struct nfsnode *np = (struct nfsnode *)0;
1493 struct vnode *newvp = (struct vnode *)0;
1494 caddr_t bpos, dpos, cp2;
1495 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1496 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1497 struct vattr vattr;
1498 int v3 = NFS_ISV3(dvp);
1501 * Oops, not for me..
1503 if (vap->va_type == VSOCK)
1504 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1506 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1507 return (error);
1509 if (vap->va_vaflags & VA_EXCLUSIVE)
1510 fmode |= O_EXCL;
1511 again:
1512 nfsstats.rpccnt[NFSPROC_CREATE]++;
1513 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1514 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1515 nfsm_fhtom(dvp, v3);
1516 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1517 if (v3) {
1518 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1519 if (fmode & O_EXCL) {
1520 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1521 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1522 #ifdef INET
1523 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
1524 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia)->sin_addr.s_addr;
1525 else
1526 #endif
1527 *tl++ = create_verf;
1528 *tl = ++create_verf;
1529 } else {
1530 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1531 nfsm_v3attrbuild(vap, FALSE);
1533 } else {
1534 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1535 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1536 sp->sa_uid = nfs_xdrneg1;
1537 sp->sa_gid = nfs_xdrneg1;
1538 sp->sa_size = 0;
1539 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1540 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1542 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1543 if (!error) {
1544 nfsm_mtofh(dvp, newvp, v3, gotvp);
1545 if (!gotvp) {
1546 if (newvp) {
1547 vput(newvp);
1548 newvp = (struct vnode *)0;
1550 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1551 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1552 if (!error)
1553 newvp = NFSTOV(np);
1556 if (v3)
1557 nfsm_wcc_data(dvp, wccflag);
1558 m_freem(mrep);
1559 nfsmout:
1560 if (error) {
1561 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1562 KKASSERT(newvp == NULL);
1563 fmode &= ~O_EXCL;
1564 goto again;
1566 } else if (v3 && (fmode & O_EXCL)) {
1568 * We are normally called with only a partially initialized
1569 * VAP. Since the NFSv3 spec says that server may use the
1570 * file attributes to store the verifier, the spec requires
1571 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1572 * in atime, but we can't really assume that all servers will
1573 * so we ensure that our SETATTR sets both atime and mtime.
1575 if (vap->va_mtime.tv_sec == VNOVAL)
1576 vfs_timestamp(&vap->va_mtime);
1577 if (vap->va_atime.tv_sec == VNOVAL)
1578 vap->va_atime = vap->va_mtime;
1579 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1581 if (error == 0) {
1583 * The new np may have enough info for access
1584 * checks, make sure rucred and wucred are
1585 * initialized for read and write rpc's.
1587 np = VTONFS(newvp);
1588 if (np->n_rucred == NULL)
1589 np->n_rucred = crhold(cnp->cn_cred);
1590 if (np->n_wucred == NULL)
1591 np->n_wucred = crhold(cnp->cn_cred);
1592 *ap->a_vpp = newvp;
1593 } else if (newvp) {
1594 vput(newvp);
1596 VTONFS(dvp)->n_flag |= NLMODIFIED;
1597 if (!wccflag)
1598 VTONFS(dvp)->n_attrstamp = 0;
1599 return (error);
1603 * nfs file remove call
1604 * To try and make nfs semantics closer to ufs semantics, a file that has
1605 * other processes using the vnode is renamed instead of removed and then
1606 * removed later on the last close.
1607 * - If v_sysref.refcnt > 1
1608 * If a rename is not already in the works
1609 * call nfs_sillyrename() to set it up
1610 * else
1611 * do the remove rpc
1613 * nfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
1614 * struct componentname *a_cnp)
1616 static int
1617 nfs_remove(struct vop_old_remove_args *ap)
1619 struct vnode *vp = ap->a_vp;
1620 struct vnode *dvp = ap->a_dvp;
1621 struct componentname *cnp = ap->a_cnp;
1622 struct nfsnode *np = VTONFS(vp);
1623 int error = 0;
1624 struct vattr vattr;
1626 #ifndef DIAGNOSTIC
1627 if (vp->v_sysref.refcnt < 1)
1628 panic("nfs_remove: bad v_sysref.refcnt");
1629 #endif
1630 if (vp->v_type == VDIR)
1631 error = EPERM;
1632 else if (vp->v_sysref.refcnt == 1 || (np->n_sillyrename &&
1633 VOP_GETATTR(vp, &vattr) == 0 &&
1634 vattr.va_nlink > 1)) {
1636 * throw away biocache buffers, mainly to avoid
1637 * unnecessary delayed writes later.
1639 error = nfs_vinvalbuf(vp, 0, 1);
1640 /* Do the rpc */
1641 if (error != EINTR)
1642 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1643 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1645 * Kludge City: If the first reply to the remove rpc is lost..
1646 * the reply to the retransmitted request will be ENOENT
1647 * since the file was in fact removed
1648 * Therefore, we cheat and return success.
1650 if (error == ENOENT)
1651 error = 0;
1652 } else if (!np->n_sillyrename) {
1653 error = nfs_sillyrename(dvp, vp, cnp);
1655 np->n_attrstamp = 0;
1656 return (error);
1660 * nfs file remove rpc called from nfs_inactive
1663 nfs_removeit(struct sillyrename *sp)
1665 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1666 sp->s_cred, NULL));
1670 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1672 static int
1673 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1674 struct ucred *cred, struct thread *td)
1676 u_int32_t *tl;
1677 caddr_t cp;
1678 int32_t t1, t2;
1679 caddr_t bpos, dpos, cp2;
1680 int error = 0, wccflag = NFSV3_WCCRATTR;
1681 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1682 int v3 = NFS_ISV3(dvp);
1684 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1685 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1686 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1687 nfsm_fhtom(dvp, v3);
1688 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1689 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1690 if (v3)
1691 nfsm_wcc_data(dvp, wccflag);
1692 m_freem(mrep);
1693 nfsmout:
1694 VTONFS(dvp)->n_flag |= NLMODIFIED;
1695 if (!wccflag)
1696 VTONFS(dvp)->n_attrstamp = 0;
1697 return (error);
1701 * nfs file rename call
1703 * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1704 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1705 * struct vnode *a_tvp, struct componentname *a_tcnp)
1707 static int
1708 nfs_rename(struct vop_old_rename_args *ap)
1710 struct vnode *fvp = ap->a_fvp;
1711 struct vnode *tvp = ap->a_tvp;
1712 struct vnode *fdvp = ap->a_fdvp;
1713 struct vnode *tdvp = ap->a_tdvp;
1714 struct componentname *tcnp = ap->a_tcnp;
1715 struct componentname *fcnp = ap->a_fcnp;
1716 int error;
1718 /* Check for cross-device rename */
1719 if ((fvp->v_mount != tdvp->v_mount) ||
1720 (tvp && (fvp->v_mount != tvp->v_mount))) {
1721 error = EXDEV;
1722 goto out;
1726 * We shouldn't have to flush fvp on rename as the file handle should
1727 * not change, but the default is to do so.
1729 * We must flush tvp on rename because it might become stale on the
1730 * server after the rename.
1732 if (nfs_flush_on_rename)
1733 VOP_FSYNC(fvp, MNT_WAIT);
1734 if (tvp)
1735 VOP_FSYNC(tvp, MNT_WAIT);
1738 * If the tvp exists and is in use, sillyrename it before doing the
1739 * rename of the new file over it.
1741 * XXX Can't sillyrename a directory.
1743 * We do not attempt to do any namecache purges in this old API
1744 * routine. The new API compat functions have access to the actual
1745 * namecache structures and will do it for us.
1747 if (tvp && tvp->v_sysref.refcnt > 1 && !VTONFS(tvp)->n_sillyrename &&
1748 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1749 vput(tvp);
1750 tvp = NULL;
1751 } else if (tvp) {
1755 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1756 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1757 tcnp->cn_td);
1759 out:
1760 if (tdvp == tvp)
1761 vrele(tdvp);
1762 else
1763 vput(tdvp);
1764 if (tvp)
1765 vput(tvp);
1766 vrele(fdvp);
1767 vrele(fvp);
1769 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1771 if (error == ENOENT)
1772 error = 0;
1773 return (error);
1777 * nfs file rename rpc called from nfs_remove() above
1779 static int
1780 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1781 struct sillyrename *sp)
1783 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1784 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1788 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1790 static int
1791 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1792 struct vnode *tdvp, const char *tnameptr, int tnamelen,
1793 struct ucred *cred, struct thread *td)
1795 u_int32_t *tl;
1796 caddr_t cp;
1797 int32_t t1, t2;
1798 caddr_t bpos, dpos, cp2;
1799 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1800 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1801 int v3 = NFS_ISV3(fdvp);
1803 nfsstats.rpccnt[NFSPROC_RENAME]++;
1804 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1805 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1806 nfsm_rndup(tnamelen));
1807 nfsm_fhtom(fdvp, v3);
1808 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1809 nfsm_fhtom(tdvp, v3);
1810 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1811 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1812 if (v3) {
1813 nfsm_wcc_data(fdvp, fwccflag);
1814 nfsm_wcc_data(tdvp, twccflag);
1816 m_freem(mrep);
1817 nfsmout:
1818 VTONFS(fdvp)->n_flag |= NLMODIFIED;
1819 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1820 if (!fwccflag)
1821 VTONFS(fdvp)->n_attrstamp = 0;
1822 if (!twccflag)
1823 VTONFS(tdvp)->n_attrstamp = 0;
1824 return (error);
1828 * nfs hard link create call
1830 * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1831 * struct componentname *a_cnp)
1833 static int
1834 nfs_link(struct vop_old_link_args *ap)
1836 struct vnode *vp = ap->a_vp;
1837 struct vnode *tdvp = ap->a_tdvp;
1838 struct componentname *cnp = ap->a_cnp;
1839 u_int32_t *tl;
1840 caddr_t cp;
1841 int32_t t1, t2;
1842 caddr_t bpos, dpos, cp2;
1843 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1844 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1845 int v3;
1847 if (vp->v_mount != tdvp->v_mount) {
1848 return (EXDEV);
1852 * Push all writes to the server, so that the attribute cache
1853 * doesn't get "out of sync" with the server.
1854 * XXX There should be a better way!
1856 VOP_FSYNC(vp, MNT_WAIT);
1858 v3 = NFS_ISV3(vp);
1859 nfsstats.rpccnt[NFSPROC_LINK]++;
1860 nfsm_reqhead(vp, NFSPROC_LINK,
1861 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1862 nfsm_fhtom(vp, v3);
1863 nfsm_fhtom(tdvp, v3);
1864 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1865 nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1866 if (v3) {
1867 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1868 nfsm_wcc_data(tdvp, wccflag);
1870 m_freem(mrep);
1871 nfsmout:
1872 VTONFS(tdvp)->n_flag |= NLMODIFIED;
1873 if (!attrflag)
1874 VTONFS(vp)->n_attrstamp = 0;
1875 if (!wccflag)
1876 VTONFS(tdvp)->n_attrstamp = 0;
1878 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1880 if (error == EEXIST)
1881 error = 0;
1882 return (error);
1886 * nfs symbolic link create call
1888 * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1889 * struct componentname *a_cnp, struct vattr *a_vap,
1890 * char *a_target)
1892 static int
1893 nfs_symlink(struct vop_old_symlink_args *ap)
1895 struct vnode *dvp = ap->a_dvp;
1896 struct vattr *vap = ap->a_vap;
1897 struct componentname *cnp = ap->a_cnp;
1898 struct nfsv2_sattr *sp;
1899 u_int32_t *tl;
1900 caddr_t cp;
1901 int32_t t1, t2;
1902 caddr_t bpos, dpos, cp2;
1903 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1904 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1905 struct vnode *newvp = (struct vnode *)0;
1906 int v3 = NFS_ISV3(dvp);
1908 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1909 slen = strlen(ap->a_target);
1910 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1911 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1912 nfsm_fhtom(dvp, v3);
1913 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1914 if (v3) {
1915 nfsm_v3attrbuild(vap, FALSE);
1917 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1918 if (!v3) {
1919 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1920 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1921 sp->sa_uid = nfs_xdrneg1;
1922 sp->sa_gid = nfs_xdrneg1;
1923 sp->sa_size = nfs_xdrneg1;
1924 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1925 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1929 * Issue the NFS request and get the rpc response.
1931 * Only NFSv3 responses returning an error of 0 actually return
1932 * a file handle that can be converted into newvp without having
1933 * to do an extra lookup rpc.
1935 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1936 if (v3) {
1937 if (error == 0)
1938 nfsm_mtofh(dvp, newvp, v3, gotvp);
1939 nfsm_wcc_data(dvp, wccflag);
1943 * out code jumps -> here, mrep is also freed.
1946 m_freem(mrep);
1947 nfsmout:
1950 * If we get an EEXIST error, silently convert it to no-error
1951 * in case of an NFS retry.
1953 if (error == EEXIST)
1954 error = 0;
1957 * If we do not have (or no longer have) an error, and we could
1958 * not extract the newvp from the response due to the request being
1959 * NFSv2 or the error being EEXIST. We have to do a lookup in order
1960 * to obtain a newvp to return.
1962 if (error == 0 && newvp == NULL) {
1963 struct nfsnode *np = NULL;
1965 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1966 cnp->cn_cred, cnp->cn_td, &np);
1967 if (!error)
1968 newvp = NFSTOV(np);
1970 if (error) {
1971 if (newvp)
1972 vput(newvp);
1973 } else {
1974 *ap->a_vpp = newvp;
1976 VTONFS(dvp)->n_flag |= NLMODIFIED;
1977 if (!wccflag)
1978 VTONFS(dvp)->n_attrstamp = 0;
1979 return (error);
1983 * nfs make dir call
1985 * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1986 * struct componentname *a_cnp, struct vattr *a_vap)
1988 static int
1989 nfs_mkdir(struct vop_old_mkdir_args *ap)
1991 struct vnode *dvp = ap->a_dvp;
1992 struct vattr *vap = ap->a_vap;
1993 struct componentname *cnp = ap->a_cnp;
1994 struct nfsv2_sattr *sp;
1995 u_int32_t *tl;
1996 caddr_t cp;
1997 int32_t t1, t2;
1998 int len;
1999 struct nfsnode *np = (struct nfsnode *)0;
2000 struct vnode *newvp = (struct vnode *)0;
2001 caddr_t bpos, dpos, cp2;
2002 int error = 0, wccflag = NFSV3_WCCRATTR;
2003 int gotvp = 0;
2004 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2005 struct vattr vattr;
2006 int v3 = NFS_ISV3(dvp);
2008 if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
2009 return (error);
2011 len = cnp->cn_namelen;
2012 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2013 nfsm_reqhead(dvp, NFSPROC_MKDIR,
2014 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2015 nfsm_fhtom(dvp, v3);
2016 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
2017 if (v3) {
2018 nfsm_v3attrbuild(vap, FALSE);
2019 } else {
2020 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2021 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2022 sp->sa_uid = nfs_xdrneg1;
2023 sp->sa_gid = nfs_xdrneg1;
2024 sp->sa_size = nfs_xdrneg1;
2025 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2026 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2028 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
2029 if (!error)
2030 nfsm_mtofh(dvp, newvp, v3, gotvp);
2031 if (v3)
2032 nfsm_wcc_data(dvp, wccflag);
2033 m_freem(mrep);
2034 nfsmout:
2035 VTONFS(dvp)->n_flag |= NLMODIFIED;
2036 if (!wccflag)
2037 VTONFS(dvp)->n_attrstamp = 0;
2039 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2040 * if we can succeed in looking up the directory.
2042 if (error == EEXIST || (!error && !gotvp)) {
2043 if (newvp) {
2044 vrele(newvp);
2045 newvp = (struct vnode *)0;
2047 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2048 cnp->cn_td, &np);
2049 if (!error) {
2050 newvp = NFSTOV(np);
2051 if (newvp->v_type != VDIR)
2052 error = EEXIST;
2055 if (error) {
2056 if (newvp)
2057 vrele(newvp);
2058 } else
2059 *ap->a_vpp = newvp;
2060 return (error);
2064 * nfs remove directory call
2066 * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2067 * struct componentname *a_cnp)
2069 static int
2070 nfs_rmdir(struct vop_old_rmdir_args *ap)
2072 struct vnode *vp = ap->a_vp;
2073 struct vnode *dvp = ap->a_dvp;
2074 struct componentname *cnp = ap->a_cnp;
2075 u_int32_t *tl;
2076 caddr_t cp;
2077 int32_t t1, t2;
2078 caddr_t bpos, dpos, cp2;
2079 int error = 0, wccflag = NFSV3_WCCRATTR;
2080 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2081 int v3 = NFS_ISV3(dvp);
2083 if (dvp == vp)
2084 return (EINVAL);
2085 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2086 nfsm_reqhead(dvp, NFSPROC_RMDIR,
2087 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2088 nfsm_fhtom(dvp, v3);
2089 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2090 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2091 if (v3)
2092 nfsm_wcc_data(dvp, wccflag);
2093 m_freem(mrep);
2094 nfsmout:
2095 VTONFS(dvp)->n_flag |= NLMODIFIED;
2096 if (!wccflag)
2097 VTONFS(dvp)->n_attrstamp = 0;
2099 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2101 if (error == ENOENT)
2102 error = 0;
2103 return (error);
2107 * nfs readdir call
2109 * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2111 static int
2112 nfs_readdir(struct vop_readdir_args *ap)
2114 struct vnode *vp = ap->a_vp;
2115 struct nfsnode *np = VTONFS(vp);
2116 struct uio *uio = ap->a_uio;
2117 int tresid, error;
2118 struct vattr vattr;
2120 if (vp->v_type != VDIR)
2121 return (EPERM);
2123 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
2124 return (error);
2127 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2128 * and then check that is still valid, or if this is an NQNFS mount
2129 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR(). Note that
2130 * VOP_GETATTR() does not necessarily go to the wire.
2132 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2133 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2134 if (VOP_GETATTR(vp, &vattr) == 0 &&
2135 (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2137 nfsstats.direofcache_hits++;
2138 goto done;
2143 * Call nfs_bioread() to do the real work. nfs_bioread() does its
2144 * own cache coherency checks so we do not have to.
2146 tresid = uio->uio_resid;
2147 error = nfs_bioread(vp, uio, 0);
2149 if (!error && uio->uio_resid == tresid)
2150 nfsstats.direofcache_misses++;
2151 done:
2152 vn_unlock(vp);
2153 return (error);
2157 * Readdir rpc call. nfs_bioread->nfs_doio->nfs_readdirrpc.
2159 * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2160 * offset/block and converts the nfs formatted directory entries for userland
2161 * consumption as well as deals with offsets into the middle of blocks.
2162 * nfs_doio only deals with logical blocks. In particular, uio_offset will
2163 * be block-bounded. It must convert to cookies for the actual RPC.
2166 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2168 int len, left;
2169 struct nfs_dirent *dp = NULL;
2170 u_int32_t *tl;
2171 caddr_t cp;
2172 int32_t t1, t2;
2173 nfsuint64 *cookiep;
2174 caddr_t bpos, dpos, cp2;
2175 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2176 nfsuint64 cookie;
2177 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2178 struct nfsnode *dnp = VTONFS(vp);
2179 u_quad_t fileno;
2180 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2181 int attrflag;
2182 int v3 = NFS_ISV3(vp);
2184 #ifndef DIAGNOSTIC
2185 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2186 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2187 panic("nfs readdirrpc bad uio");
2188 #endif
2191 * If there is no cookie, assume directory was stale.
2193 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2194 if (cookiep)
2195 cookie = *cookiep;
2196 else
2197 return (NFSERR_BAD_COOKIE);
2199 * Loop around doing readdir rpc's of size nm_readdirsize
2200 * truncated to a multiple of DIRBLKSIZ.
2201 * The stopping criteria is EOF or buffer full.
2203 while (more_dirs && bigenough) {
2204 nfsstats.rpccnt[NFSPROC_READDIR]++;
2205 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2206 NFSX_READDIR(v3));
2207 nfsm_fhtom(vp, v3);
2208 if (v3) {
2209 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2210 *tl++ = cookie.nfsuquad[0];
2211 *tl++ = cookie.nfsuquad[1];
2212 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2213 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2214 } else {
2215 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2216 *tl++ = cookie.nfsuquad[0];
2218 *tl = txdr_unsigned(nmp->nm_readdirsize);
2219 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2220 if (v3) {
2221 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2222 if (!error) {
2223 nfsm_dissect(tl, u_int32_t *,
2224 2 * NFSX_UNSIGNED);
2225 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2226 dnp->n_cookieverf.nfsuquad[1] = *tl;
2227 } else {
2228 m_freem(mrep);
2229 goto nfsmout;
2232 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2233 more_dirs = fxdr_unsigned(int, *tl);
2235 /* loop thru the dir entries, converting them to std form */
2236 while (more_dirs && bigenough) {
2237 if (v3) {
2238 nfsm_dissect(tl, u_int32_t *,
2239 3 * NFSX_UNSIGNED);
2240 fileno = fxdr_hyper(tl);
2241 len = fxdr_unsigned(int, *(tl + 2));
2242 } else {
2243 nfsm_dissect(tl, u_int32_t *,
2244 2 * NFSX_UNSIGNED);
2245 fileno = fxdr_unsigned(u_quad_t, *tl++);
2246 len = fxdr_unsigned(int, *tl);
2248 if (len <= 0 || len > NFS_MAXNAMLEN) {
2249 error = EBADRPC;
2250 m_freem(mrep);
2251 goto nfsmout;
2255 * len is the number of bytes in the path element
2256 * name, not including the \0 termination.
2258 * tlen is the number of bytes w have to reserve for
2259 * the path element name.
2261 tlen = nfsm_rndup(len);
2262 if (tlen == len)
2263 tlen += 4; /* To ensure null termination */
2266 * If the entry would cross a DIRBLKSIZ boundary,
2267 * extend the previous nfs_dirent to cover the
2268 * remaining space.
2270 left = DIRBLKSIZ - blksiz;
2271 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2272 dp->nfs_reclen += left;
2273 uiop->uio_iov->iov_base += left;
2274 uiop->uio_iov->iov_len -= left;
2275 uiop->uio_offset += left;
2276 uiop->uio_resid -= left;
2277 blksiz = 0;
2279 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2280 bigenough = 0;
2281 if (bigenough) {
2282 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2283 dp->nfs_ino = fileno;
2284 dp->nfs_namlen = len;
2285 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2286 dp->nfs_type = DT_UNKNOWN;
2287 blksiz += dp->nfs_reclen;
2288 if (blksiz == DIRBLKSIZ)
2289 blksiz = 0;
2290 uiop->uio_offset += sizeof(struct nfs_dirent);
2291 uiop->uio_resid -= sizeof(struct nfs_dirent);
2292 uiop->uio_iov->iov_base += sizeof(struct nfs_dirent);
2293 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2294 nfsm_mtouio(uiop, len);
2297 * The uiop has advanced by nfs_dirent + len
2298 * but really needs to advance by
2299 * nfs_dirent + tlen
2301 cp = uiop->uio_iov->iov_base;
2302 tlen -= len;
2303 *cp = '\0'; /* null terminate */
2304 uiop->uio_iov->iov_base += tlen;
2305 uiop->uio_iov->iov_len -= tlen;
2306 uiop->uio_offset += tlen;
2307 uiop->uio_resid -= tlen;
2308 } else {
2310 * NFS strings must be rounded up (nfsm_myouio
2311 * handled that in the bigenough case).
2313 nfsm_adv(nfsm_rndup(len));
2315 if (v3) {
2316 nfsm_dissect(tl, u_int32_t *,
2317 3 * NFSX_UNSIGNED);
2318 } else {
2319 nfsm_dissect(tl, u_int32_t *,
2320 2 * NFSX_UNSIGNED);
2324 * If we were able to accomodate the last entry,
2325 * get the cookie for the next one. Otherwise
2326 * hold-over the cookie for the one we were not
2327 * able to accomodate.
2329 if (bigenough) {
2330 cookie.nfsuquad[0] = *tl++;
2331 if (v3)
2332 cookie.nfsuquad[1] = *tl++;
2333 } else if (v3) {
2334 tl += 2;
2335 } else {
2336 tl++;
2338 more_dirs = fxdr_unsigned(int, *tl);
2341 * If at end of rpc data, get the eof boolean
2343 if (!more_dirs) {
2344 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2345 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2347 m_freem(mrep);
2350 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2351 * by increasing d_reclen for the last record.
2353 if (blksiz > 0) {
2354 left = DIRBLKSIZ - blksiz;
2355 dp->nfs_reclen += left;
2356 uiop->uio_iov->iov_base += left;
2357 uiop->uio_iov->iov_len -= left;
2358 uiop->uio_offset += left;
2359 uiop->uio_resid -= left;
2362 if (bigenough) {
2364 * We hit the end of the directory, update direofoffset.
2366 dnp->n_direofoffset = uiop->uio_offset;
2367 } else {
2369 * There is more to go, insert the link cookie so the
2370 * next block can be read.
2372 if (uiop->uio_resid > 0)
2373 kprintf("EEK! readdirrpc resid > 0\n");
2374 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2375 *cookiep = cookie;
2377 nfsmout:
2378 return (error);
2382 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2385 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2387 int len, left;
2388 struct nfs_dirent *dp;
2389 u_int32_t *tl;
2390 caddr_t cp;
2391 int32_t t1, t2;
2392 struct vnode *newvp;
2393 nfsuint64 *cookiep;
2394 caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2395 struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2396 nfsuint64 cookie;
2397 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2398 struct nfsnode *dnp = VTONFS(vp), *np;
2399 nfsfh_t *fhp;
2400 u_quad_t fileno;
2401 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2402 int attrflag, fhsize;
2403 struct nchandle nch;
2404 struct nchandle dnch;
2405 struct nlcomponent nlc;
2407 #ifndef nolint
2408 dp = NULL;
2409 #endif
2410 #ifndef DIAGNOSTIC
2411 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2412 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2413 panic("nfs readdirplusrpc bad uio");
2414 #endif
2416 * Obtain the namecache record for the directory so we have something
2417 * to use as a basis for creating the entries. This function will
2418 * return a held (but not locked) ncp. The ncp may be disconnected
2419 * from the tree and cannot be used for upward traversals, and the
2420 * ncp may be unnamed. Note that other unrelated operations may
2421 * cause the ncp to be named at any time.
2423 cache_fromdvp(vp, NULL, 0, &dnch);
2424 bzero(&nlc, sizeof(nlc));
2425 newvp = NULLVP;
2428 * If there is no cookie, assume directory was stale.
2430 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2431 if (cookiep)
2432 cookie = *cookiep;
2433 else
2434 return (NFSERR_BAD_COOKIE);
2436 * Loop around doing readdir rpc's of size nm_readdirsize
2437 * truncated to a multiple of DIRBLKSIZ.
2438 * The stopping criteria is EOF or buffer full.
2440 while (more_dirs && bigenough) {
2441 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2442 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2443 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2444 nfsm_fhtom(vp, 1);
2445 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2446 *tl++ = cookie.nfsuquad[0];
2447 *tl++ = cookie.nfsuquad[1];
2448 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2449 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2450 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2451 *tl = txdr_unsigned(nmp->nm_rsize);
2452 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2453 nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2454 if (error) {
2455 m_freem(mrep);
2456 goto nfsmout;
2458 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2459 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2460 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2461 more_dirs = fxdr_unsigned(int, *tl);
2463 /* loop thru the dir entries, doctoring them to 4bsd form */
2464 while (more_dirs && bigenough) {
2465 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2466 fileno = fxdr_hyper(tl);
2467 len = fxdr_unsigned(int, *(tl + 2));
2468 if (len <= 0 || len > NFS_MAXNAMLEN) {
2469 error = EBADRPC;
2470 m_freem(mrep);
2471 goto nfsmout;
2473 tlen = nfsm_rndup(len);
2474 if (tlen == len)
2475 tlen += 4; /* To ensure null termination*/
2476 left = DIRBLKSIZ - blksiz;
2477 if ((tlen + sizeof(struct nfs_dirent)) > left) {
2478 dp->nfs_reclen += left;
2479 uiop->uio_iov->iov_base += left;
2480 uiop->uio_iov->iov_len -= left;
2481 uiop->uio_offset += left;
2482 uiop->uio_resid -= left;
2483 blksiz = 0;
2485 if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2486 bigenough = 0;
2487 if (bigenough) {
2488 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2489 dp->nfs_ino = fileno;
2490 dp->nfs_namlen = len;
2491 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2492 dp->nfs_type = DT_UNKNOWN;
2493 blksiz += dp->nfs_reclen;
2494 if (blksiz == DIRBLKSIZ)
2495 blksiz = 0;
2496 uiop->uio_offset += sizeof(struct nfs_dirent);
2497 uiop->uio_resid -= sizeof(struct nfs_dirent);
2498 uiop->uio_iov->iov_base += sizeof(struct nfs_dirent);
2499 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2500 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2501 nlc.nlc_namelen = len;
2502 nfsm_mtouio(uiop, len);
2503 cp = uiop->uio_iov->iov_base;
2504 tlen -= len;
2505 *cp = '\0';
2506 uiop->uio_iov->iov_base += tlen;
2507 uiop->uio_iov->iov_len -= tlen;
2508 uiop->uio_offset += tlen;
2509 uiop->uio_resid -= tlen;
2510 } else
2511 nfsm_adv(nfsm_rndup(len));
2512 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2513 if (bigenough) {
2514 cookie.nfsuquad[0] = *tl++;
2515 cookie.nfsuquad[1] = *tl++;
2516 } else
2517 tl += 2;
2520 * Since the attributes are before the file handle
2521 * (sigh), we must skip over the attributes and then
2522 * come back and get them.
2524 attrflag = fxdr_unsigned(int, *tl);
2525 if (attrflag) {
2526 dpossav1 = dpos;
2527 mdsav1 = md;
2528 nfsm_adv(NFSX_V3FATTR);
2529 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2530 doit = fxdr_unsigned(int, *tl);
2531 if (doit) {
2532 nfsm_getfh(fhp, fhsize, 1);
2533 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2534 vref(vp);
2535 newvp = vp;
2536 np = dnp;
2537 } else {
2538 error = nfs_nget(vp->v_mount, fhp,
2539 fhsize, &np);
2540 if (error)
2541 doit = 0;
2542 else
2543 newvp = NFSTOV(np);
2546 if (doit && bigenough) {
2547 dpossav2 = dpos;
2548 dpos = dpossav1;
2549 mdsav2 = md;
2550 md = mdsav1;
2551 nfsm_loadattr(newvp, (struct vattr *)0);
2552 dpos = dpossav2;
2553 md = mdsav2;
2554 dp->nfs_type =
2555 IFTODT(VTTOIF(np->n_vattr.va_type));
2556 if (dnch.ncp) {
2557 kprintf("NFS/READDIRPLUS, ENTER %*.*s\n",
2558 nlc.nlc_namelen, nlc.nlc_namelen,
2559 nlc.nlc_nameptr);
2560 nch = cache_nlookup(&dnch, &nlc);
2561 cache_setunresolved(&nch);
2562 nfs_cache_setvp(&nch, newvp,
2563 nfspos_cache_timeout);
2564 cache_put(&nch);
2565 } else {
2566 kprintf("NFS/READDIRPLUS, UNABLE TO ENTER"
2567 " %*.*s\n",
2568 nlc.nlc_namelen, nlc.nlc_namelen,
2569 nlc.nlc_nameptr);
2572 } else {
2573 /* Just skip over the file handle */
2574 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2575 i = fxdr_unsigned(int, *tl);
2576 nfsm_adv(nfsm_rndup(i));
2578 if (newvp != NULLVP) {
2579 if (newvp == vp)
2580 vrele(newvp);
2581 else
2582 vput(newvp);
2583 newvp = NULLVP;
2585 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2586 more_dirs = fxdr_unsigned(int, *tl);
2589 * If at end of rpc data, get the eof boolean
2591 if (!more_dirs) {
2592 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2593 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2595 m_freem(mrep);
2598 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2599 * by increasing d_reclen for the last record.
2601 if (blksiz > 0) {
2602 left = DIRBLKSIZ - blksiz;
2603 dp->nfs_reclen += left;
2604 uiop->uio_iov->iov_base += left;
2605 uiop->uio_iov->iov_len -= left;
2606 uiop->uio_offset += left;
2607 uiop->uio_resid -= left;
2611 * We are now either at the end of the directory or have filled the
2612 * block.
2614 if (bigenough)
2615 dnp->n_direofoffset = uiop->uio_offset;
2616 else {
2617 if (uiop->uio_resid > 0)
2618 kprintf("EEK! readdirplusrpc resid > 0\n");
2619 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2620 *cookiep = cookie;
2622 nfsmout:
2623 if (newvp != NULLVP) {
2624 if (newvp == vp)
2625 vrele(newvp);
2626 else
2627 vput(newvp);
2628 newvp = NULLVP;
2630 if (dnch.ncp)
2631 cache_drop(&dnch);
2632 return (error);
2636 * Silly rename. To make the NFS filesystem that is stateless look a little
2637 * more like the "ufs" a remove of an active vnode is translated to a rename
2638 * to a funny looking filename that is removed by nfs_inactive on the
2639 * nfsnode. There is the potential for another process on a different client
2640 * to create the same funny name between the nfs_lookitup() fails and the
2641 * nfs_rename() completes, but...
2643 static int
2644 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2646 struct sillyrename *sp;
2647 struct nfsnode *np;
2648 int error;
2651 * We previously purged dvp instead of vp. I don't know why, it
2652 * completely destroys performance. We can't do it anyway with the
2653 * new VFS API since we would be breaking the namecache topology.
2655 cache_purge(vp); /* XXX */
2656 np = VTONFS(vp);
2657 #ifndef DIAGNOSTIC
2658 if (vp->v_type == VDIR)
2659 panic("nfs: sillyrename dir");
2660 #endif
2661 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2662 M_NFSREQ, M_WAITOK);
2663 sp->s_cred = crdup(cnp->cn_cred);
2664 sp->s_dvp = dvp;
2665 vref(dvp);
2667 /* Fudge together a funny name */
2668 sp->s_namlen = ksprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
2670 /* Try lookitups until we get one that isn't there */
2671 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2672 cnp->cn_td, (struct nfsnode **)0) == 0) {
2673 sp->s_name[4]++;
2674 if (sp->s_name[4] > 'z') {
2675 error = EINVAL;
2676 goto bad;
2679 error = nfs_renameit(dvp, cnp, sp);
2680 if (error)
2681 goto bad;
2682 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2683 cnp->cn_td, &np);
2684 np->n_sillyrename = sp;
2685 return (0);
2686 bad:
2687 vrele(sp->s_dvp);
2688 crfree(sp->s_cred);
2689 kfree((caddr_t)sp, M_NFSREQ);
2690 return (error);
2694 * Look up a file name and optionally either update the file handle or
2695 * allocate an nfsnode, depending on the value of npp.
2696 * npp == NULL --> just do the lookup
2697 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2698 * handled too
2699 * *npp != NULL --> update the file handle in the vnode
2701 static int
2702 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2703 struct thread *td, struct nfsnode **npp)
2705 u_int32_t *tl;
2706 caddr_t cp;
2707 int32_t t1, t2;
2708 struct vnode *newvp = (struct vnode *)0;
2709 struct nfsnode *np, *dnp = VTONFS(dvp);
2710 caddr_t bpos, dpos, cp2;
2711 int error = 0, fhlen, attrflag;
2712 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2713 nfsfh_t *nfhp;
2714 int v3 = NFS_ISV3(dvp);
2716 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2717 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2718 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2719 nfsm_fhtom(dvp, v3);
2720 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2721 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2722 if (npp && !error) {
2723 nfsm_getfh(nfhp, fhlen, v3);
2724 if (*npp) {
2725 np = *npp;
2726 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2727 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
2728 np->n_fhp = &np->n_fh;
2729 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2730 np->n_fhp =(nfsfh_t *)kmalloc(fhlen,M_NFSBIGFH,M_WAITOK);
2731 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2732 np->n_fhsize = fhlen;
2733 newvp = NFSTOV(np);
2734 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2735 vref(dvp);
2736 newvp = dvp;
2737 } else {
2738 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2739 if (error) {
2740 m_freem(mrep);
2741 return (error);
2743 newvp = NFSTOV(np);
2745 if (v3) {
2746 nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
2747 if (!attrflag && *npp == NULL) {
2748 m_freem(mrep);
2749 if (newvp == dvp)
2750 vrele(newvp);
2751 else
2752 vput(newvp);
2753 return (ENOENT);
2755 } else
2756 nfsm_loadattr(newvp, (struct vattr *)0);
2758 m_freem(mrep);
2759 nfsmout:
2760 if (npp && *npp == NULL) {
2761 if (error) {
2762 if (newvp) {
2763 if (newvp == dvp)
2764 vrele(newvp);
2765 else
2766 vput(newvp);
2768 } else
2769 *npp = np;
2771 return (error);
2775 * Nfs Version 3 commit rpc
2778 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2780 caddr_t cp;
2781 u_int32_t *tl;
2782 int32_t t1, t2;
2783 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2784 caddr_t bpos, dpos, cp2;
2785 int error = 0, wccflag = NFSV3_WCCRATTR;
2786 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2788 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2789 return (0);
2790 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2791 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2792 nfsm_fhtom(vp, 1);
2793 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2794 txdr_hyper(offset, tl);
2795 tl += 2;
2796 *tl = txdr_unsigned(cnt);
2797 nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2798 nfsm_wcc_data(vp, wccflag);
2799 if (!error) {
2800 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2801 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2802 NFSX_V3WRITEVERF)) {
2803 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2804 NFSX_V3WRITEVERF);
2805 error = NFSERR_STALEWRITEVERF;
2808 m_freem(mrep);
2809 nfsmout:
2810 return (error);
2814 * Kludge City..
2815 * - make nfs_bmap() essentially a no-op that does no translation
2816 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2817 * (Maybe I could use the process's page mapping, but I was concerned that
2818 * Kernel Write might not be enabled and also figured copyout() would do
2819 * a lot more work than bcopy() and also it currently happens in the
2820 * context of the swapper process (2).
2822 * nfs_bmap(struct vnode *a_vp, off_t a_loffset,
2823 * off_t *a_doffsetp, int *a_runp, int *a_runb)
2825 static int
2826 nfs_bmap(struct vop_bmap_args *ap)
2828 if (ap->a_doffsetp != NULL)
2829 *ap->a_doffsetp = ap->a_loffset;
2830 if (ap->a_runp != NULL)
2831 *ap->a_runp = 0;
2832 if (ap->a_runb != NULL)
2833 *ap->a_runb = 0;
2834 return (0);
2838 * Strategy routine.
2840 * For async requests when nfsiod(s) are running, queue the request by
2841 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2842 * request.
2844 static int
2845 nfs_strategy(struct vop_strategy_args *ap)
2847 struct bio *bio = ap->a_bio;
2848 struct bio *nbio;
2849 struct buf *bp = bio->bio_buf;
2850 struct thread *td;
2851 int error = 0;
2853 KASSERT(bp->b_cmd != BUF_CMD_DONE,
2854 ("nfs_strategy: buffer %p unexpectedly marked done", bp));
2855 KASSERT(BUF_REFCNT(bp) > 0,
2856 ("nfs_strategy: buffer %p not locked", bp));
2858 if (bp->b_flags & B_ASYNC)
2859 td = NULL;
2860 else
2861 td = curthread; /* XXX */
2864 * We probably don't need to push an nbio any more since no
2865 * block conversion is required due to the use of 64 bit byte
2866 * offsets, but do it anyway.
2868 nbio = push_bio(bio);
2869 nbio->bio_offset = bio->bio_offset;
2872 * If the op is asynchronous and an i/o daemon is waiting
2873 * queue the request, wake it up and wait for completion
2874 * otherwise just do it ourselves.
2876 if ((bp->b_flags & B_ASYNC) == 0 || nfs_asyncio(ap->a_vp, nbio, td))
2877 error = nfs_doio(ap->a_vp, nbio, td);
2878 return (error);
2882 * Mmap a file
2884 * NB Currently unsupported.
2886 * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
2888 /* ARGSUSED */
2889 static int
2890 nfs_mmap(struct vop_mmap_args *ap)
2892 return (EINVAL);
2896 * fsync vnode op. Just call nfs_flush() with commit == 1.
2898 * nfs_fsync(struct vnode *a_vp, int a_waitfor)
2900 /* ARGSUSED */
2901 static int
2902 nfs_fsync(struct vop_fsync_args *ap)
2904 return (nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1));
2908 * Flush all the blocks associated with a vnode. Dirty NFS buffers may be
2909 * in one of two states: If B_NEEDCOMMIT is clear then the buffer contains
2910 * new NFS data which needs to be written to the server. If B_NEEDCOMMIT is
2911 * set the buffer contains data that has already been written to the server
2912 * and which now needs a commit RPC.
2914 * If commit is 0 we only take one pass and only flush buffers containing new
2915 * dirty data.
2917 * If commit is 1 we take two passes, issuing a commit RPC in the second
2918 * pass.
2920 * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
2921 * to completely flush all pending data.
2923 * Note that the RB_SCAN code properly handles the case where the
2924 * callback might block and directly or indirectly (another thread) cause
2925 * the RB tree to change.
2928 #ifndef NFS_COMMITBVECSIZ
2929 #define NFS_COMMITBVECSIZ 16
2930 #endif
2932 struct nfs_flush_info {
2933 enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
2934 struct thread *td;
2935 struct vnode *vp;
2936 int waitfor;
2937 int slpflag;
2938 int slptimeo;
2939 int loops;
2940 struct buf *bvary[NFS_COMMITBVECSIZ];
2941 int bvsize;
2942 off_t beg_off;
2943 off_t end_off;
2946 static int nfs_flush_bp(struct buf *bp, void *data);
2947 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
2950 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
2952 struct nfsnode *np = VTONFS(vp);
2953 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2954 struct nfs_flush_info info;
2955 int error;
2957 bzero(&info, sizeof(info));
2958 info.td = td;
2959 info.vp = vp;
2960 info.waitfor = waitfor;
2961 info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
2962 info.loops = 0;
2964 do {
2966 * Flush mode
2968 info.mode = NFI_FLUSHNEW;
2969 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2970 nfs_flush_bp, &info);
2973 * Take a second pass if committing and no error occured.
2974 * Clean up any left over collection (whether an error
2975 * occurs or not).
2977 if (commit && error == 0) {
2978 info.mode = NFI_COMMIT;
2979 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2980 nfs_flush_bp, &info);
2981 if (info.bvsize)
2982 error = nfs_flush_docommit(&info, error);
2986 * Wait for pending I/O to complete before checking whether
2987 * any further dirty buffers exist.
2989 while (waitfor == MNT_WAIT && vp->v_track_write.bk_active) {
2990 vp->v_track_write.bk_waitflag = 1;
2991 error = tsleep(&vp->v_track_write,
2992 info.slpflag, "nfsfsync", info.slptimeo);
2993 if (error) {
2995 * We have to be able to break out if this
2996 * is an 'intr' mount.
2998 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
2999 error = -EINTR;
3000 break;
3004 * Since we do not process pending signals,
3005 * once we get a PCATCH our tsleep() will no
3006 * longer sleep, switch to a fixed timeout
3007 * instead.
3009 if (info.slpflag == PCATCH) {
3010 info.slpflag = 0;
3011 info.slptimeo = 2 * hz;
3013 error = 0;
3016 ++info.loops;
3018 * Loop if we are flushing synchronous as well as committing,
3019 * and dirty buffers are still present. Otherwise we might livelock.
3021 } while (waitfor == MNT_WAIT && commit &&
3022 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3025 * The callbacks have to return a negative error to terminate the
3026 * RB scan.
3028 if (error < 0)
3029 error = -error;
3032 * Deal with any error collection
3034 if (np->n_flag & NWRITEERR) {
3035 error = np->n_error;
3036 np->n_flag &= ~NWRITEERR;
3038 return (error);
3042 static
3044 nfs_flush_bp(struct buf *bp, void *data)
3046 struct nfs_flush_info *info = data;
3047 off_t toff;
3048 int error;
3050 error = 0;
3051 switch(info->mode) {
3052 case NFI_FLUSHNEW:
3053 crit_enter();
3054 if (info->loops && info->waitfor == MNT_WAIT) {
3055 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3056 if (error) {
3057 int lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3058 if (info->slpflag & PCATCH)
3059 lkflags |= LK_PCATCH;
3060 error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3061 info->slptimeo);
3063 } else {
3064 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3066 if (error == 0) {
3067 KKASSERT(bp->b_vp == info->vp);
3069 if ((bp->b_flags & B_DELWRI) == 0)
3070 panic("nfs_fsync: not dirty");
3071 if (bp->b_flags & B_NEEDCOMMIT) {
3072 BUF_UNLOCK(bp);
3073 crit_exit();
3074 break;
3076 bremfree(bp);
3078 crit_exit();
3079 bawrite(bp);
3080 } else {
3081 crit_exit();
3082 error = 0;
3084 break;
3085 case NFI_COMMIT:
3087 * Only process buffers in need of a commit which we can
3088 * immediately lock. This may prevent a buffer from being
3089 * committed, but the normal flush loop will block on the
3090 * same buffer so we shouldn't get into an endless loop.
3092 crit_enter();
3093 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3094 (B_DELWRI | B_NEEDCOMMIT) ||
3095 BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
3096 crit_exit();
3097 break;
3100 KKASSERT(bp->b_vp == info->vp);
3101 bremfree(bp);
3104 * NOTE: storing the bp in the bvary[] basically sets
3105 * it up for a commit operation.
3107 * We must call vfs_busy_pages() now so the commit operation
3108 * is interlocked with user modifications to memory mapped
3109 * pages.
3111 * Note: to avoid loopback deadlocks, we do not
3112 * assign b_runningbufspace.
3114 bp->b_cmd = BUF_CMD_WRITE;
3115 vfs_busy_pages(bp->b_vp, bp);
3116 info->bvary[info->bvsize] = bp;
3117 toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3118 if (info->bvsize == 0 || toff < info->beg_off)
3119 info->beg_off = toff;
3120 toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3121 if (info->bvsize == 0 || toff > info->end_off)
3122 info->end_off = toff;
3123 ++info->bvsize;
3124 if (info->bvsize == NFS_COMMITBVECSIZ) {
3125 error = nfs_flush_docommit(info, 0);
3126 KKASSERT(info->bvsize == 0);
3128 crit_exit();
3130 return (error);
3133 static
3135 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3137 struct vnode *vp;
3138 struct buf *bp;
3139 off_t bytes;
3140 int retv;
3141 int i;
3143 vp = info->vp;
3145 if (info->bvsize > 0) {
3147 * Commit data on the server, as required. Note that
3148 * nfs_commit will use the vnode's cred for the commit.
3149 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3151 bytes = info->end_off - info->beg_off;
3152 if (bytes > 0x40000000)
3153 bytes = 0x40000000;
3154 if (error) {
3155 retv = -error;
3156 } else {
3157 retv = nfs_commit(vp, info->beg_off,
3158 (int)bytes, info->td);
3159 if (retv == NFSERR_STALEWRITEVERF)
3160 nfs_clearcommit(vp->v_mount);
3164 * Now, either mark the blocks I/O done or mark the
3165 * blocks dirty, depending on whether the commit
3166 * succeeded.
3168 for (i = 0; i < info->bvsize; ++i) {
3169 bp = info->bvary[i];
3170 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3171 if (retv) {
3173 * Error, leave B_DELWRI intact
3175 vfs_unbusy_pages(bp);
3176 bp->b_cmd = BUF_CMD_DONE;
3177 brelse(bp);
3178 } else {
3180 * Success, remove B_DELWRI ( bundirty() ).
3182 * b_dirtyoff/b_dirtyend seem to be NFS
3183 * specific. We should probably move that
3184 * into bundirty(). XXX
3186 * We are faking an I/O write, we have to
3187 * start the transaction in order to
3188 * immediately biodone() it.
3190 crit_enter();
3191 bp->b_flags |= B_ASYNC;
3192 bundirty(bp);
3193 bp->b_flags &= ~B_ERROR;
3194 bp->b_dirtyoff = bp->b_dirtyend = 0;
3195 crit_exit();
3196 biodone(&bp->b_bio1);
3199 info->bvsize = 0;
3201 return (error);
3205 * NFS advisory byte-level locks.
3206 * Currently unsupported.
3208 * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3209 * int a_flags)
3211 static int
3212 nfs_advlock(struct vop_advlock_args *ap)
3214 struct nfsnode *np = VTONFS(ap->a_vp);
3217 * The following kludge is to allow diskless support to work
3218 * until a real NFS lockd is implemented. Basically, just pretend
3219 * that this is a local lock.
3221 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3225 * Print out the contents of an nfsnode.
3227 * nfs_print(struct vnode *a_vp)
3229 static int
3230 nfs_print(struct vop_print_args *ap)
3232 struct vnode *vp = ap->a_vp;
3233 struct nfsnode *np = VTONFS(vp);
3235 kprintf("tag VT_NFS, fileid %lld fsid 0x%x",
3236 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3237 if (vp->v_type == VFIFO)
3238 fifo_printinfo(vp);
3239 kprintf("\n");
3240 return (0);
3244 * nfs special file access vnode op.
3245 * Essentially just get vattr and then imitate iaccess() since the device is
3246 * local to the client.
3248 * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
3250 static int
3251 nfsspec_access(struct vop_access_args *ap)
3253 struct vattr *vap;
3254 gid_t *gp;
3255 struct ucred *cred = ap->a_cred;
3256 struct vnode *vp = ap->a_vp;
3257 mode_t mode = ap->a_mode;
3258 struct vattr vattr;
3259 int i;
3260 int error;
3263 * Disallow write attempts on filesystems mounted read-only;
3264 * unless the file is a socket, fifo, or a block or character
3265 * device resident on the filesystem.
3267 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3268 switch (vp->v_type) {
3269 case VREG:
3270 case VDIR:
3271 case VLNK:
3272 return (EROFS);
3273 default:
3274 break;
3278 * If you're the super-user,
3279 * you always get access.
3281 if (cred->cr_uid == 0)
3282 return (0);
3283 vap = &vattr;
3284 error = VOP_GETATTR(vp, vap);
3285 if (error)
3286 return (error);
3288 * Access check is based on only one of owner, group, public.
3289 * If not owner, then check group. If not a member of the
3290 * group, then check public access.
3292 if (cred->cr_uid != vap->va_uid) {
3293 mode >>= 3;
3294 gp = cred->cr_groups;
3295 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3296 if (vap->va_gid == *gp)
3297 goto found;
3298 mode >>= 3;
3299 found:
3302 error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3303 return (error);
3307 * Read wrapper for special devices.
3309 * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3310 * struct ucred *a_cred)
3312 static int
3313 nfsspec_read(struct vop_read_args *ap)
3315 struct nfsnode *np = VTONFS(ap->a_vp);
3318 * Set access flag.
3320 np->n_flag |= NACC;
3321 getnanotime(&np->n_atim);
3322 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3326 * Write wrapper for special devices.
3328 * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3329 * struct ucred *a_cred)
3331 static int
3332 nfsspec_write(struct vop_write_args *ap)
3334 struct nfsnode *np = VTONFS(ap->a_vp);
3337 * Set update flag.
3339 np->n_flag |= NUPD;
3340 getnanotime(&np->n_mtim);
3341 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3345 * Close wrapper for special devices.
3347 * Update the times on the nfsnode then do device close.
3349 * nfsspec_close(struct vnode *a_vp, int a_fflag)
3351 static int
3352 nfsspec_close(struct vop_close_args *ap)
3354 struct vnode *vp = ap->a_vp;
3355 struct nfsnode *np = VTONFS(vp);
3356 struct vattr vattr;
3358 if (np->n_flag & (NACC | NUPD)) {
3359 np->n_flag |= NCHG;
3360 if (vp->v_sysref.refcnt == 1 &&
3361 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3362 VATTR_NULL(&vattr);
3363 if (np->n_flag & NACC)
3364 vattr.va_atime = np->n_atim;
3365 if (np->n_flag & NUPD)
3366 vattr.va_mtime = np->n_mtim;
3367 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3370 return (VOCALL(&spec_vnode_vops, &ap->a_head));
3374 * Read wrapper for fifos.
3376 * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3377 * struct ucred *a_cred)
3379 static int
3380 nfsfifo_read(struct vop_read_args *ap)
3382 struct nfsnode *np = VTONFS(ap->a_vp);
3385 * Set access flag.
3387 np->n_flag |= NACC;
3388 getnanotime(&np->n_atim);
3389 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3393 * Write wrapper for fifos.
3395 * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3396 * struct ucred *a_cred)
3398 static int
3399 nfsfifo_write(struct vop_write_args *ap)
3401 struct nfsnode *np = VTONFS(ap->a_vp);
3404 * Set update flag.
3406 np->n_flag |= NUPD;
3407 getnanotime(&np->n_mtim);
3408 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3412 * Close wrapper for fifos.
3414 * Update the times on the nfsnode then do fifo close.
3416 * nfsfifo_close(struct vnode *a_vp, int a_fflag)
3418 static int
3419 nfsfifo_close(struct vop_close_args *ap)
3421 struct vnode *vp = ap->a_vp;
3422 struct nfsnode *np = VTONFS(vp);
3423 struct vattr vattr;
3424 struct timespec ts;
3426 if (np->n_flag & (NACC | NUPD)) {
3427 getnanotime(&ts);
3428 if (np->n_flag & NACC)
3429 np->n_atim = ts;
3430 if (np->n_flag & NUPD)
3431 np->n_mtim = ts;
3432 np->n_flag |= NCHG;
3433 if (vp->v_sysref.refcnt == 1 &&
3434 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3435 VATTR_NULL(&vattr);
3436 if (np->n_flag & NACC)
3437 vattr.va_atime = np->n_atim;
3438 if (np->n_flag & NUPD)
3439 vattr.va_mtime = np->n_mtim;
3440 (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3443 return (VOCALL(&fifo_vnode_vops, &ap->a_head));