1 /*-
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95
33 */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: releng/7.3/sys/nfsclient/nfs_vnops.c 198425 2009-10-23 19:53:04Z jhb $");
37
38 /*
39 * vnode op calls for Sun NFS version 2 and 3
40 */
41
42 #include "opt_inet.h"
43
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/systm.h>
47 #include <sys/resourcevar.h>
48 #include <sys/proc.h>
49 #include <sys/mount.h>
50 #include <sys/bio.h>
51 #include <sys/buf.h>
52 #include <sys/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/namei.h>
55 #include <sys/socket.h>
56 #include <sys/vnode.h>
57 #include <sys/dirent.h>
58 #include <sys/fcntl.h>
59 #include <sys/lockf.h>
60 #include <sys/stat.h>
61 #include <sys/sysctl.h>
62 #include <sys/signalvar.h>
63
64 #include <vm/vm.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_extern.h>
67 #include <vm/vm_object.h>
68
69 #include <fs/fifofs/fifo.h>
70
71 #include <rpc/rpcclnt.h>
72
73 #include <nfs/rpcv2.h>
74 #include <nfs/nfsproto.h>
75 #include <nfsclient/nfs.h>
76 #include <nfsclient/nfsnode.h>
77 #include <nfsclient/nfsmount.h>
78 #include <nfsclient/nfs_lock.h>
79 #include <nfs/xdr_subs.h>
80 #include <nfsclient/nfsm_subs.h>
81
82 #include <net/if.h>
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
85
86 /* Defs */
87 #define TRUE 1
88 #define FALSE 0
89
90 /*
91 * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
92 * calls are not in getblk() and brelse() so that they would not be necessary
93 * here.
94 */
95 #ifndef B_VMIO
96 #define vfs_busy_pages(bp, f)
97 #endif
98
99 static vop_read_t nfsfifo_read;
100 static vop_write_t nfsfifo_write;
101 static vop_close_t nfsfifo_close;
102 static int nfs_flush(struct vnode *, int, struct thread *,
103 int);
104 static int nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
105 struct thread *);
106 static vop_lookup_t nfs_lookup;
107 static vop_create_t nfs_create;
108 static vop_mknod_t nfs_mknod;
109 static vop_open_t nfs_open;
110 static vop_close_t nfs_close;
111 static vop_access_t nfs_access;
112 static vop_getattr_t nfs_getattr;
113 static vop_setattr_t nfs_setattr;
114 static vop_read_t nfs_read;
115 static vop_fsync_t nfs_fsync;
116 static vop_remove_t nfs_remove;
117 static vop_link_t nfs_link;
118 static vop_rename_t nfs_rename;
119 static vop_mkdir_t nfs_mkdir;
120 static vop_rmdir_t nfs_rmdir;
121 static vop_symlink_t nfs_symlink;
122 static vop_readdir_t nfs_readdir;
123 static vop_strategy_t nfs_strategy;
124 static int nfs_lookitup(struct vnode *, const char *, int,
125 struct ucred *, struct thread *, struct nfsnode **);
126 static int nfs_sillyrename(struct vnode *, struct vnode *,
127 struct componentname *);
128 static vop_access_t nfsspec_access;
129 static vop_readlink_t nfs_readlink;
130 static vop_print_t nfs_print;
131 static vop_advlock_t nfs_advlock;
132 static vop_advlockasync_t nfs_advlockasync;
133
134 /*
135 * Global vfs data structures for nfs
136 */
137 struct vop_vector nfs_vnodeops = {
138 .vop_default = &default_vnodeops,
139 .vop_access = nfs_access,
140 .vop_advlock = nfs_advlock,
141 .vop_advlockasync = nfs_advlockasync,
142 .vop_close = nfs_close,
143 .vop_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_lease = VOP_NULL,
150 .vop_link = nfs_link,
151 .vop_lookup = nfs_lookup,
152 .vop_mkdir = nfs_mkdir,
153 .vop_mknod = nfs_mknod,
154 .vop_open = nfs_open,
155 .vop_print = nfs_print,
156 .vop_read = nfs_read,
157 .vop_readdir = nfs_readdir,
158 .vop_readlink = nfs_readlink,
159 .vop_reclaim = nfs_reclaim,
160 .vop_remove = nfs_remove,
161 .vop_rename = nfs_rename,
162 .vop_rmdir = nfs_rmdir,
163 .vop_setattr = nfs_setattr,
164 .vop_strategy = nfs_strategy,
165 .vop_symlink = nfs_symlink,
166 .vop_write = nfs_write,
167 };
168
169 struct vop_vector nfs_fifoops = {
170 .vop_default = &fifo_specops,
171 .vop_access = nfsspec_access,
172 .vop_close = nfsfifo_close,
173 .vop_fsync = nfs_fsync,
174 .vop_getattr = nfs_getattr,
175 .vop_inactive = nfs_inactive,
176 .vop_print = nfs_print,
177 .vop_read = nfsfifo_read,
178 .vop_reclaim = nfs_reclaim,
179 .vop_setattr = nfs_setattr,
180 .vop_write = nfsfifo_write,
181 };
182
183 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
184 struct componentname *cnp, struct vattr *vap);
185 static int nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
186 struct ucred *cred, struct thread *td);
187 static int nfs_renamerpc(struct vnode *fdvp, const char *fnameptr,
188 int fnamelen, struct vnode *tdvp,
189 const char *tnameptr, int tnamelen,
190 struct ucred *cred, struct thread *td);
191 static int nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
192 struct sillyrename *sp);
193
194 /*
195 * Global variables
196 */
197 struct mtx nfs_iod_mtx;
198 struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
199 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
200 int nfs_numasync = 0;
201 vop_advlock_t *nfs_advlock_p = nfs_dolock;
202 vop_reclaim_t *nfs_reclaim_p = NULL;
203 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
204
205 SYSCTL_DECL(_vfs_nfs);
206
207 static int nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
208 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
209 &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
210
211 static int nfs_prime_access_cache = 1;
212 SYSCTL_INT(_vfs_nfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
213 &nfs_prime_access_cache, 0,
214 "Prime NFS ACCESS cache when fetching attributes");
215
216 static int nfsv3_commit_on_close = 0;
217 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
218 &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
219
220 static int nfs_clean_pages_on_close = 1;
221 SYSCTL_INT(_vfs_nfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
222 &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
223
224 int nfs_directio_enable = 0;
225 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
226 &nfs_directio_enable, 0, "Enable NFS directio");
227
228 /*
229 * This sysctl allows other processes to mmap a file that has been opened
230 * O_DIRECT by a process. In general, having processes mmap the file while
231 * Direct IO is in progress can lead to Data Inconsistencies. But, we allow
232 * this by default to prevent DoS attacks - to prevent a malicious user from
233 * opening up files O_DIRECT preventing other users from mmap'ing these
234 * files. "Protected" environments where stricter consistency guarantees are
235 * required can disable this knob. The process that opened the file O_DIRECT
236 * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
237 * meaningful.
238 */
239 int nfs_directio_allow_mmap = 1;
240 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
241 &nfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
242
243 #if 0
244 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
245 &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
246
247 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
248 &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
249 #endif
250
251 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY \
252 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE \
253 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
254
255 /*
256 * SMP Locking Note :
257 * The list of locks after the description of the lock is the ordering
258 * of other locks acquired with the lock held.
259 * np->n_mtx : Protects the fields in the nfsnode.
260 VM Object Lock
261 VI_MTX (acquired indirectly)
262 * nmp->nm_mtx : Protects the fields in the nfsmount.
263 rep->r_mtx
264 * nfs_iod_mtx : Global lock, protects shared nfsiod state.
265 * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
266 nmp->nm_mtx
267 rep->r_mtx
268 * rep->r_mtx : Protects the fields in an nfsreq.
269 */
270
271 static int
272 nfs3_access_otw(struct vnode *vp, int wmode, struct thread *td,
273 struct ucred *cred)
274 {
275 const int v3 = 1;
276 u_int32_t *tl;
277 int error = 0, attrflag;
278
279 struct mbuf *mreq, *mrep, *md, *mb;
280 caddr_t bpos, dpos;
281 u_int32_t rmode;
282 struct nfsnode *np = VTONFS(vp);
283
284 nfsstats.rpccnt[NFSPROC_ACCESS]++;
285 mreq = nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
286 mb = mreq;
287 bpos = mtod(mb, caddr_t);
288 nfsm_fhtom(vp, v3);
289 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
290 *tl = txdr_unsigned(wmode);
291 nfsm_request(vp, NFSPROC_ACCESS, td, cred);
292 nfsm_postop_attr(vp, attrflag);
293 if (!error) {
294 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
295 rmode = fxdr_unsigned(u_int32_t, *tl);
296 mtx_lock(&np->n_mtx);
297 np->n_mode = rmode;
298 np->n_modeuid = cred->cr_uid;
299 np->n_modestamp = time_second;
300 mtx_unlock(&np->n_mtx);
301 }
302 m_freem(mrep);
303 nfsmout:
304 return (error);
305 }
306
307 /*
308 * nfs access vnode op.
309 * For nfs version 2, just return ok. File accesses may fail later.
310 * For nfs version 3, use the access rpc to check accessibility. If file modes
311 * are changed on the server, accesses might still fail later.
312 */
313 static int
314 nfs_access(struct vop_access_args *ap)
315 {
316 struct vnode *vp = ap->a_vp;
317 int error = 0;
318 u_int32_t mode, wmode;
319 int v3 = NFS_ISV3(vp);
320 struct nfsnode *np = VTONFS(vp);
321
322 /*
323 * Disallow write attempts on filesystems mounted read-only;
324 * unless the file is a socket, fifo, or a block or character
325 * device resident on the filesystem.
326 */
327 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
328 switch (vp->v_type) {
329 case VREG:
330 case VDIR:
331 case VLNK:
332 return (EROFS);
333 default:
334 break;
335 }
336 }
337 /*
338 * For nfs v3, check to see if we have done this recently, and if
339 * so return our cached result instead of making an ACCESS call.
340 * If not, do an access rpc, otherwise you are stuck emulating
341 * ufs_access() locally using the vattr. This may not be correct,
342 * since the server may apply other access criteria such as
343 * client uid-->server uid mapping that we do not know about.
344 */
345 if (v3) {
346 if (ap->a_mode & VREAD)
347 mode = NFSV3ACCESS_READ;
348 else
349 mode = 0;
350 if (vp->v_type != VDIR) {
351 if (ap->a_mode & VWRITE)
352 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
353 if (ap->a_mode & VEXEC)
354 mode |= NFSV3ACCESS_EXECUTE;
355 } else {
356 if (ap->a_mode & VWRITE)
357 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
358 NFSV3ACCESS_DELETE);
359 if (ap->a_mode & VEXEC)
360 mode |= NFSV3ACCESS_LOOKUP;
361 }
362 /* XXX safety belt, only make blanket request if caching */
363 if (nfsaccess_cache_timeout > 0) {
364 wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
365 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
366 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
367 } else {
368 wmode = mode;
369 }
370
371 /*
372 * Does our cached result allow us to give a definite yes to
373 * this request?
374 */
375 mtx_lock(&np->n_mtx);
376 if ((time_second < (np->n_modestamp + nfsaccess_cache_timeout)) &&
377 (ap->a_cred->cr_uid == np->n_modeuid) &&
378 ((np->n_mode & mode) == mode)) {
379 nfsstats.accesscache_hits++;
380 } else {
381 /*
382 * Either a no, or a don't know. Go to the wire.
383 */
384 nfsstats.accesscache_misses++;
385 mtx_unlock(&np->n_mtx);
386 error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred);
387 mtx_lock(&np->n_mtx);
388 if (!error) {
389 if ((np->n_mode & mode) != mode) {
390 error = EACCES;
391 }
392 }
393 }
394 mtx_unlock(&np->n_mtx);
395 return (error);
396 } else {
397 if ((error = nfsspec_access(ap)) != 0) {
398 return (error);
399 }
400 /*
401 * Attempt to prevent a mapped root from accessing a file
402 * which it shouldn't. We try to read a byte from the file
403 * if the user is root and the file is not zero length.
404 * After calling nfsspec_access, we should have the correct
405 * file size cached.
406 */
407 mtx_lock(&np->n_mtx);
408 if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
409 && VTONFS(vp)->n_size > 0) {
410 struct iovec aiov;
411 struct uio auio;
412 char buf[1];
413
414 mtx_unlock(&np->n_mtx);
415 aiov.iov_base = buf;
416 aiov.iov_len = 1;
417 auio.uio_iov = &aiov;
418 auio.uio_iovcnt = 1;
419 auio.uio_offset = 0;
420 auio.uio_resid = 1;
421 auio.uio_segflg = UIO_SYSSPACE;
422 auio.uio_rw = UIO_READ;
423 auio.uio_td = ap->a_td;
424
425 if (vp->v_type == VREG)
426 error = nfs_readrpc(vp, &auio, ap->a_cred);
427 else if (vp->v_type == VDIR) {
428 char* bp;
429 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
430 aiov.iov_base = bp;
431 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
432 error = nfs_readdirrpc(vp, &auio, ap->a_cred);
433 free(bp, M_TEMP);
434 } else if (vp->v_type == VLNK)
435 error = nfs_readlinkrpc(vp, &auio, ap->a_cred);
436 else
437 error = EACCES;
438 } else
439 mtx_unlock(&np->n_mtx);
440 return (error);
441 }
442 }
443
444 int nfs_otw_getattr_avoid = 0;
445
446 /*
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.
452 */
453 /* ARGSUSED */
454 static int
455 nfs_open(struct vop_open_args *ap)
456 {
457 struct vnode *vp = ap->a_vp;
458 struct nfsnode *np = VTONFS(vp);
459 struct vattr vattr;
460 int error;
461 int fmode = ap->a_mode;
462
463 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
464 return (EOPNOTSUPP);
465
466 /*
467 * Get a valid lease. If cached data is stale, flush it.
468 */
469 mtx_lock(&np->n_mtx);
470 if (np->n_flag & NMODIFIED) {
471 mtx_unlock(&np->n_mtx);
472 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
473 if (error == EINTR || error == EIO)
474 return (error);
475 np->n_attrstamp = 0;
476 if (vp->v_type == VDIR)
477 np->n_direofoffset = 0;
478 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
479 if (error)
480 return (error);
481 mtx_lock(&np->n_mtx);
482 np->n_mtime = vattr.va_mtime;
483 mtx_unlock(&np->n_mtx);
484 } else {
485 struct thread *td = curthread;
486
487 if (np->n_ac_ts_syscalls != td->td_syscalls ||
488 np->n_ac_ts_tid != td->td_tid ||
489 td->td_proc == NULL ||
490 np->n_ac_ts_pid != td->td_proc->p_pid) {
491 np->n_attrstamp = 0;
492 }
493 mtx_unlock(&np->n_mtx);
494 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
495 if (error)
496 return (error);
497 mtx_lock(&np->n_mtx);
498 if (NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
499 if (vp->v_type == VDIR)
500 np->n_direofoffset = 0;
501 mtx_unlock(&np->n_mtx);
502 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
503 if (error == EINTR || error == EIO) {
504 return (error);
505 }
506 mtx_lock(&np->n_mtx);
507 np->n_mtime = vattr.va_mtime;
508 }
509 mtx_unlock(&np->n_mtx);
510 }
511 /*
512 * If the object has >= 1 O_DIRECT active opens, we disable caching.
513 */
514 if (nfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
515 if (np->n_directio_opens == 0) {
516 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
517 if (error)
518 return (error);
519 mtx_lock(&np->n_mtx);
520 np->n_flag |= NNONCACHE;
521 mtx_unlock(&np->n_mtx);
522 }
523 np->n_directio_opens++;
524 }
525 vnode_create_vobject(vp, vattr.va_size, ap->a_td);
526 return (0);
527 }
528
529 /*
530 * nfs close vnode op
531 * What an NFS client should do upon close after writing is a debatable issue.
532 * Most NFS clients push delayed writes to the server upon close, basically for
533 * two reasons:
534 * 1 - So that any write errors may be reported back to the client process
535 * doing the close system call. By far the two most likely errors are
536 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
537 * 2 - To put a worst case upper bound on cache inconsistency between
538 * multiple clients for the file.
539 * There is also a consistency problem for Version 2 of the protocol w.r.t.
540 * not being able to tell if other clients are writing a file concurrently,
541 * since there is no way of knowing if the changed modify time in the reply
542 * is only due to the write for this client.
543 * (NFS Version 3 provides weak cache consistency data in the reply that
544 * should be sufficient to detect and handle this case.)
545 *
546 * The current code does the following:
547 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
548 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
549 * or commit them (this satisfies 1 and 2 except for the
550 * case where the server crashes after this close but
551 * before the commit RPC, which is felt to be "good
552 * enough". Changing the last argument to nfs_flush() to
553 * a 1 would force a commit operation, if it is felt a
554 * commit is necessary now.
555 */
556 /* ARGSUSED */
557 static int
558 nfs_close(struct vop_close_args *ap)
559 {
560 struct vnode *vp = ap->a_vp;
561 struct nfsnode *np = VTONFS(vp);
562 int error = 0;
563 int fmode = ap->a_fflag;
564
565 if (vp->v_type == VREG) {
566 /*
567 * Examine and clean dirty pages, regardless of NMODIFIED.
568 * This closes a major hole in close-to-open consistency.
569 * We want to push out all dirty pages (and buffers) on
570 * close, regardless of whether they were dirtied by
571 * mmap'ed writes or via write().
572 */
573 if (nfs_clean_pages_on_close && vp->v_object) {
574 VM_OBJECT_LOCK(vp->v_object);
575 vm_object_page_clean(vp->v_object, 0, 0, 0);
576 VM_OBJECT_UNLOCK(vp->v_object);
577 }
578 mtx_lock(&np->n_mtx);
579 if (np->n_flag & NMODIFIED) {
580 mtx_unlock(&np->n_mtx);
581 if (NFS_ISV3(vp)) {
582 /*
583 * Under NFSv3 we have dirty buffers to dispose of. We
584 * must flush them to the NFS server. We have the option
585 * of waiting all the way through the commit rpc or just
586 * waiting for the initial write. The default is to only
587 * wait through the initial write so the data is in the
588 * server's cache, which is roughly similar to the state
589 * a standard disk subsystem leaves the file in on close().
590 *
591 * We cannot clear the NMODIFIED bit in np->n_flag due to
592 * potential races with other processes, and certainly
593 * cannot clear it if we don't commit.
594 */
595 int cm = nfsv3_commit_on_close ? 1 : 0;
596 error = nfs_flush(vp, MNT_WAIT, ap->a_td, cm);
597 /* np->n_flag &= ~NMODIFIED; */
598 } else
599 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
600 mtx_lock(&np->n_mtx);
601 }
602 if (np->n_flag & NWRITEERR) {
603 np->n_flag &= ~NWRITEERR;
604 error = np->n_error;
605 }
606 mtx_unlock(&np->n_mtx);
607 }
608 if (nfs_directio_enable)
609 KASSERT((np->n_directio_asyncwr == 0),
610 ("nfs_close: dirty unflushed (%d) directio buffers\n",
611 np->n_directio_asyncwr));
612 if (nfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
613 mtx_lock(&np->n_mtx);
614 KASSERT((np->n_directio_opens > 0),
615 ("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
616 np->n_directio_opens--;
617 if (np->n_directio_opens == 0)
618 np->n_flag &= ~NNONCACHE;
619 mtx_unlock(&np->n_mtx);
620 }
621 return (error);
622 }
623
624 /*
625 * nfs getattr call from vfs.
626 */
627 static int
628 nfs_getattr(struct vop_getattr_args *ap)
629 {
630 struct vnode *vp = ap->a_vp;
631 struct nfsnode *np = VTONFS(vp);
632 struct vattr *vap = ap->a_vap;
633 struct vattr vattr;
634 caddr_t bpos, dpos;
635 int error = 0;
636 struct mbuf *mreq, *mrep, *md, *mb;
637 int v3 = NFS_ISV3(vp);
638
639 /*
640 * Update local times for special files.
641 */
642 mtx_lock(&np->n_mtx);
643 if (np->n_flag & (NACC | NUPD))
644 np->n_flag |= NCHG;
645 mtx_unlock(&np->n_mtx);
646 /*
647 * First look in the cache.
648 */
649 if (nfs_getattrcache(vp, &vattr) == 0)
650 goto nfsmout;
651 if (v3 && nfs_prime_access_cache && nfsaccess_cache_timeout > 0) {
652 nfsstats.accesscache_misses++;
653 nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_td, ap->a_cred);
654 if (nfs_getattrcache(vp, &vattr) == 0)
655 goto nfsmout;
656 }
657 nfsstats.rpccnt[NFSPROC_GETATTR]++;
658 mreq = nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
659 mb = mreq;
660 bpos = mtod(mb, caddr_t);
661 nfsm_fhtom(vp, v3);
662 nfsm_request(vp, NFSPROC_GETATTR, ap->a_td, ap->a_cred);
663 if (!error) {
664 nfsm_loadattr(vp, &vattr);
665 }
666 m_freem(mrep);
667 nfsmout:
668 vap->va_type = vattr.va_type;
669 vap->va_mode = vattr.va_mode;
670 vap->va_nlink = vattr.va_nlink;
671 vap->va_uid = vattr.va_uid;
672 vap->va_gid = vattr.va_gid;
673 vap->va_fsid = vattr.va_fsid;
674 vap->va_fileid = vattr.va_fileid;
675 vap->va_size = vattr.va_size;
676 vap->va_blocksize = vattr.va_blocksize;
677 vap->va_atime = vattr.va_atime;
678 vap->va_mtime = vattr.va_mtime;
679 vap->va_ctime = vattr.va_ctime;
680 vap->va_gen = vattr.va_gen;
681 vap->va_flags = vattr.va_flags;
682 vap->va_rdev = vattr.va_rdev;
683 vap->va_bytes = vattr.va_bytes;
684 vap->va_filerev = vattr.va_filerev;
685
686 return (error);
687 }
688
689 /*
690 * nfs setattr call.
691 */
692 static int
693 nfs_setattr(struct vop_setattr_args *ap)
694 {
695 struct vnode *vp = ap->a_vp;
696 struct nfsnode *np = VTONFS(vp);
697 struct vattr *vap = ap->a_vap;
698 int error = 0;
699 u_quad_t tsize;
700
701 #ifndef nolint
702 tsize = (u_quad_t)0;
703 #endif
704
705 /*
706 * Setting of flags and marking of atimes are not supported.
707 */
708 if (vap->va_flags != VNOVAL || (vap->va_vaflags & VA_MARK_ATIME))
709 return (EOPNOTSUPP);
710
711 /*
712 * Disallow write attempts if the filesystem is mounted read-only.
713 */
714 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
715 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
716 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
717 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
718 error = EROFS;
719 goto out;
720 }
721 if (vap->va_size != VNOVAL) {
722 switch (vp->v_type) {
723 case VDIR:
724 return (EISDIR);
725 case VCHR:
726 case VBLK:
727 case VSOCK:
728 case VFIFO:
729 if (vap->va_mtime.tv_sec == VNOVAL &&
730 vap->va_atime.tv_sec == VNOVAL &&
731 vap->va_mode == (mode_t)VNOVAL &&
732 vap->va_uid == (uid_t)VNOVAL &&
733 vap->va_gid == (gid_t)VNOVAL)
734 return (0);
735 vap->va_size = VNOVAL;
736 break;
737 default:
738 /*
739 * Disallow write attempts if the filesystem is
740 * mounted read-only.
741 */
742 if (vp->v_mount->mnt_flag & MNT_RDONLY)
743 return (EROFS);
744 /*
745 * We run vnode_pager_setsize() early (why?),
746 * we must set np->n_size now to avoid vinvalbuf
747 * V_SAVE races that might setsize a lower
748 * value.
749 */
750 mtx_lock(&np->n_mtx);
751 tsize = np->n_size;
752 mtx_unlock(&np->n_mtx);
753 error = nfs_meta_setsize(vp, ap->a_cred,
754 ap->a_td, vap->va_size);
755 mtx_lock(&np->n_mtx);
756 if (np->n_flag & NMODIFIED) {
757 tsize = np->n_size;
758 mtx_unlock(&np->n_mtx);
759 if (vap->va_size == 0)
760 error = nfs_vinvalbuf(vp, 0, ap->a_td, 1);
761 else
762 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
763 if (error) {
764 vnode_pager_setsize(vp, tsize);
765 goto out;
766 }
767 } else
768 mtx_unlock(&np->n_mtx);
769 /*
770 * np->n_size has already been set to vap->va_size
771 * in nfs_meta_setsize(). We must set it again since
772 * nfs_loadattrcache() could be called through
773 * nfs_meta_setsize() and could modify np->n_size.
774 */
775 mtx_lock(&np->n_mtx);
776 np->n_vattr.va_size = np->n_size = vap->va_size;
777 mtx_unlock(&np->n_mtx);
778 };
779 } else {
780 mtx_lock(&np->n_mtx);
781 if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
782 (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
783 mtx_unlock(&np->n_mtx);
784 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) != 0 &&
785 (error == EINTR || error == EIO))
786 return error;
787 } else
788 mtx_unlock(&np->n_mtx);
789 }
790 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
791 if (error && vap->va_size != VNOVAL) {
792 mtx_lock(&np->n_mtx);
793 np->n_size = np->n_vattr.va_size = tsize;
794 vnode_pager_setsize(vp, tsize);
795 mtx_unlock(&np->n_mtx);
796 }
797 out:
798 return (error);
799 }
800
801 /*
802 * Do an nfs setattr rpc.
803 */
804 static int
805 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
806 struct thread *td)
807 {
808 struct nfsv2_sattr *sp;
809 struct nfsnode *np = VTONFS(vp);
810 caddr_t bpos, dpos;
811 u_int32_t *tl;
812 int error = 0, wccflag = NFSV3_WCCRATTR;
813 struct mbuf *mreq, *mrep, *md, *mb;
814 int v3 = NFS_ISV3(vp);
815
816 nfsstats.rpccnt[NFSPROC_SETATTR]++;
817 mreq = nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
818 mb = mreq;
819 bpos = mtod(mb, caddr_t);
820 nfsm_fhtom(vp, v3);
821 if (v3) {
822 nfsm_v3attrbuild(vap, TRUE);
823 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
824 *tl = nfs_false;
825 } else {
826 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
827 if (vap->va_mode == (mode_t)VNOVAL)
828 sp->sa_mode = nfs_xdrneg1;
829 else
830 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
831 if (vap->va_uid == (uid_t)VNOVAL)
832 sp->sa_uid = nfs_xdrneg1;
833 else
834 sp->sa_uid = txdr_unsigned(vap->va_uid);
835 if (vap->va_gid == (gid_t)VNOVAL)
836 sp->sa_gid = nfs_xdrneg1;
837 else
838 sp->sa_gid = txdr_unsigned(vap->va_gid);
839 sp->sa_size = txdr_unsigned(vap->va_size);
840 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
841 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
842 }
843 nfsm_request(vp, NFSPROC_SETATTR, td, cred);
844 if (v3) {
845 np->n_modestamp = 0;
846 nfsm_wcc_data(vp, wccflag);
847 } else
848 nfsm_loadattr(vp, NULL);
849 m_freem(mrep);
850 nfsmout:
851 return (error);
852 }
853
854 /*
855 * nfs lookup call, one step at a time...
856 * First look in cache
857 * If not found, unlock the directory nfsnode and do the rpc
858 */
859 static int
860 nfs_lookup(struct vop_lookup_args *ap)
861 {
862 struct componentname *cnp = ap->a_cnp;
863 struct vnode *dvp = ap->a_dvp;
864 struct vnode **vpp = ap->a_vpp;
865 struct vattr vattr;
866 time_t dmtime;
867 int flags = cnp->cn_flags;
868 struct vnode *newvp;
869 struct nfsmount *nmp;
870 caddr_t bpos, dpos;
871 struct mbuf *mreq, *mrep, *md, *mb;
872 long len;
873 nfsfh_t *fhp;
874 struct nfsnode *np;
875 int error = 0, attrflag, fhsize;
876 int v3 = NFS_ISV3(dvp);
877 struct thread *td = cnp->cn_thread;
878
879 *vpp = NULLVP;
880 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
881 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
882 return (EROFS);
883 if (dvp->v_type != VDIR)
884 return (ENOTDIR);
885 nmp = VFSTONFS(dvp->v_mount);
886 np = VTONFS(dvp);
887 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
888 *vpp = NULLVP;
889 return (error);
890 }
891 error = cache_lookup(dvp, vpp, cnp);
892 if (error > 0 && error != ENOENT)
893 return (error);
894 if (error == -1) {
895 /*
896 * We only accept a positive hit in the cache if the
897 * change time of the file matches our cached copy.
898 * Otherwise, we discard the cache entry and fallback
899 * to doing a lookup RPC.
900 */
901 newvp = *vpp;
902 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, td)
903 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
904 nfsstats.lookupcache_hits++;
905 if (cnp->cn_nameiop != LOOKUP &&
906 (flags & ISLASTCN))
907 cnp->cn_flags |= SAVENAME;
908 return (0);
909 }
910 cache_purge(newvp);
911 if (dvp != newvp)
912 vput(newvp);
913 else
914 vrele(newvp);
915 *vpp = NULLVP;
916 } else if (error == ENOENT) {
917 /*
918 * We only accept a negative hit in the cache if the
919 * modification time of the parent directory matches
920 * our cached copy. Otherwise, we discard all of the
921 * negative cache entries for this directory.
922 */
923 if (VOP_GETATTR(dvp, &vattr, cnp->cn_cred, td) == 0 &&
924 vattr.va_mtime.tv_sec == np->n_dmtime) {
925 nfsstats.lookupcache_hits++;
926 return (ENOENT);
927 }
928 cache_purge_negative(dvp);
929 mtx_lock(&np->n_mtx);
930 np->n_dmtime = 0;
931 mtx_unlock(&np->n_mtx);
932 }
933
934 /*
935 * Cache the modification time of the parent directory in case
936 * the lookup fails and results in adding the first negative
937 * name cache entry for the directory. Since this is reading
938 * a single time_t, don't bother with locking. The
939 * modification time may be a bit stale, but it must be read
940 * before performing the lookup RPC to prevent a race where
941 * another lookup updates the timestamp on the directory after
942 * the lookup RPC has been performed on the server but before
943 * n_dmtime is set at the end of this function.
944 */
945 dmtime = np->n_vattr.va_mtime.tv_sec;
946 error = 0;
947 newvp = NULLVP;
948 nfsstats.lookupcache_misses++;
949 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
950 len = cnp->cn_namelen;
951 mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP,
952 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
953 mb = mreq;
954 bpos = mtod(mb, caddr_t);
955 nfsm_fhtom(dvp, v3);
956 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
957 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_thread, cnp->cn_cred);
958 if (error) {
959 if (v3) {
960 nfsm_postop_attr(dvp, attrflag);
961 m_freem(mrep);
962 }
963 goto nfsmout;
964 }
965 nfsm_getfh(fhp, fhsize, v3);
966
967 /*
968 * Handle RENAME case...
969 */
970 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
971 if (NFS_CMPFH(np, fhp, fhsize)) {
972 m_freem(mrep);
973 return (EISDIR);
974 }
975 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, LK_EXCLUSIVE);
976 if (error) {
977 m_freem(mrep);
978 return (error);
979 }
980 newvp = NFSTOV(np);
981 if (v3) {
982 nfsm_postop_attr(newvp, attrflag);
983 nfsm_postop_attr(dvp, attrflag);
984 } else
985 nfsm_loadattr(newvp, NULL);
986 *vpp = newvp;
987 m_freem(mrep);
988 cnp->cn_flags |= SAVENAME;
989 return (0);
990 }
991
992 if (flags & ISDOTDOT) {
993 VOP_UNLOCK(dvp, 0, td);
994 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, cnp->cn_lkflags);
995 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
996 if (error)
997 return (error);
998 newvp = NFSTOV(np);
999 } else if (NFS_CMPFH(np, fhp, fhsize)) {
1000 VREF(dvp);
1001 newvp = dvp;
1002 } else {
1003 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, cnp->cn_lkflags);
1004 if (error) {
1005 m_freem(mrep);
1006 return (error);
1007 }
1008 newvp = NFSTOV(np);
1009 }
1010 if (v3) {
1011 nfsm_postop_attr(newvp, attrflag);
1012 nfsm_postop_attr(dvp, attrflag);
1013 } else
1014 nfsm_loadattr(newvp, NULL);
1015 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1016 cnp->cn_flags |= SAVENAME;
1017 if ((cnp->cn_flags & MAKEENTRY) &&
1018 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
1019 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
1020 cache_enter(dvp, newvp, cnp);
1021 }
1022 *vpp = newvp;
1023 m_freem(mrep);
1024 nfsmout:
1025 if (error) {
1026 if (newvp != NULLVP) {
1027 vput(newvp);
1028 *vpp = NULLVP;
1029 }
1030
1031 if (error != ENOENT)
1032 goto done;
1033
1034 /* The requested file was not found. */
1035 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1036 (flags & ISLASTCN)) {
1037 /*
1038 * XXX: UFS does a full VOP_ACCESS(dvp,
1039 * VWRITE) here instead of just checking
1040 * MNT_RDONLY.
1041 */
1042 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1043 return (EROFS);
1044 cnp->cn_flags |= SAVENAME;
1045 return (EJUSTRETURN);
1046 }
1047
1048 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE) {
1049 /*
1050 * Maintain n_dmtime as the modification time
1051 * of the parent directory when the oldest -ve
1052 * name cache entry for this directory was
1053 * added. If a -ve cache entry has already
1054 * been added with a newer modification time
1055 * by a concurrent lookup, then don't bother
1056 * adding a cache entry. The modification
1057 * time of the directory might have changed
1058 * due to the file this lookup failed to find
1059 * being created. In that case a subsequent
1060 * lookup would incorrectly use the entry
1061 * added here instead of doing an extra
1062 * lookup.
1063 */
1064 mtx_lock(&np->n_mtx);
1065 if (np->n_dmtime <= dmtime) {
1066 if (np->n_dmtime == 0)
1067 np->n_dmtime = dmtime;
1068 mtx_unlock(&np->n_mtx);
1069 cache_enter(dvp, NULL, cnp);
1070 } else
1071 mtx_unlock(&np->n_mtx);
1072 }
1073 return (ENOENT);
1074 }
1075 done:
1076 return (error);
1077 }
1078
1079 /*
1080 * nfs read call.
1081 * Just call nfs_bioread() to do the work.
1082 */
1083 static int
1084 nfs_read(struct vop_read_args *ap)
1085 {
1086 struct vnode *vp = ap->a_vp;
1087
1088 switch (vp->v_type) {
1089 case VREG:
1090 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1091 case VDIR:
1092 return (EISDIR);
1093 default:
1094 return (EOPNOTSUPP);
1095 }
1096 }
1097
1098 /*
1099 * nfs readlink call
1100 */
1101 static int
1102 nfs_readlink(struct vop_readlink_args *ap)
1103 {
1104 struct vnode *vp = ap->a_vp;
1105
1106 if (vp->v_type != VLNK)
1107 return (EINVAL);
1108 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred));
1109 }
1110
1111 /*
1112 * Do a readlink rpc.
1113 * Called by nfs_doio() from below the buffer cache.
1114 */
1115 int
1116 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1117 {
1118 caddr_t bpos, dpos;
1119 int error = 0, len, attrflag;
1120 struct mbuf *mreq, *mrep, *md, *mb;
1121 int v3 = NFS_ISV3(vp);
1122
1123 nfsstats.rpccnt[NFSPROC_READLINK]++;
1124 mreq = nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1125 mb = mreq;
1126 bpos = mtod(mb, caddr_t);
1127 nfsm_fhtom(vp, v3);
1128 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, cred);
1129 if (v3)
1130 nfsm_postop_attr(vp, attrflag);
1131 if (!error) {
1132 nfsm_strsiz(len, NFS_MAXPATHLEN);
1133 if (len == NFS_MAXPATHLEN) {
1134 struct nfsnode *np = VTONFS(vp);
1135 mtx_lock(&np->n_mtx);
1136 if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1137 len = np->n_size;
1138 mtx_unlock(&np->n_mtx);
1139 }
1140 nfsm_mtouio(uiop, len);
1141 }
1142 m_freem(mrep);
1143 nfsmout:
1144 return (error);
1145 }
1146
1147 /*
1148 * nfs read rpc call
1149 * Ditto above
1150 */
1151 int
1152 nfs_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1153 {
1154 u_int32_t *tl;
1155 caddr_t bpos, dpos;
1156 struct mbuf *mreq, *mrep, *md, *mb;
1157 struct nfsmount *nmp;
1158 int error = 0, len, retlen, tsiz, eof, attrflag;
1159 int v3 = NFS_ISV3(vp);
1160 int rsize;
1161
1162 #ifndef nolint
1163 eof = 0;
1164 #endif
1165 nmp = VFSTONFS(vp->v_mount);
1166 tsiz = uiop->uio_resid;
1167 mtx_lock(&nmp->nm_mtx);
1168 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize) {
1169 mtx_unlock(&nmp->nm_mtx);
1170 return (EFBIG);
1171 }
1172 rsize = nmp->nm_rsize;
1173 mtx_unlock(&nmp->nm_mtx);
1174 while (tsiz > 0) {
1175 nfsstats.rpccnt[NFSPROC_READ]++;
1176 len = (tsiz > rsize) ? rsize : tsiz;
1177 mreq = nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1178 mb = mreq;
1179 bpos = mtod(mb, caddr_t);
1180 nfsm_fhtom(vp, v3);
1181 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED * 3);
1182 if (v3) {
1183 txdr_hyper(uiop->uio_offset, tl);
1184 *(tl + 2) = txdr_unsigned(len);
1185 } else {
1186 *tl++ = txdr_unsigned(uiop->uio_offset);
1187 *tl++ = txdr_unsigned(len);
1188 *tl = 0;
1189 }
1190 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, cred);
1191 if (v3) {
1192 nfsm_postop_attr(vp, attrflag);
1193 if (error) {
1194 m_freem(mrep);
1195 goto nfsmout;
1196 }
1197 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
1198 eof = fxdr_unsigned(int, *(tl + 1));
1199 } else {
1200 nfsm_loadattr(vp, NULL);
1201 }
1202 nfsm_strsiz(retlen, rsize);
1203 nfsm_mtouio(uiop, retlen);
1204 m_freem(mrep);
1205 tsiz -= retlen;
1206 if (v3) {
1207 if (eof || retlen == 0) {
1208 tsiz = 0;
1209 }
1210 } else if (retlen < len) {
1211 tsiz = 0;
1212 }
1213 }
1214 nfsmout:
1215 return (error);
1216 }
1217
1218 /*
1219 * nfs write call
1220 */
1221 int
1222 nfs_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1223 int *iomode, int *must_commit)
1224 {
1225 u_int32_t *tl;
1226 int32_t backup;
1227 caddr_t bpos, dpos;
1228 struct mbuf *mreq, *mrep, *md, *mb;
1229 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1230 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1231 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1232 int wsize;
1233
1234 #ifndef DIAGNOSTIC
1235 if (uiop->uio_iovcnt != 1)
1236 panic("nfs: writerpc iovcnt > 1");
1237 #endif
1238 *must_commit = 0;
1239 tsiz = uiop->uio_resid;
1240 mtx_lock(&nmp->nm_mtx);
1241 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize) {
1242 mtx_unlock(&nmp->nm_mtx);
1243 return (EFBIG);
1244 }
1245 wsize = nmp->nm_wsize;
1246 mtx_unlock(&nmp->nm_mtx);
1247 while (tsiz > 0) {
1248 nfsstats.rpccnt[NFSPROC_WRITE]++;
1249 len = (tsiz > wsize) ? wsize : tsiz;
1250 mreq = nfsm_reqhead(vp, NFSPROC_WRITE,
1251 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1252 mb = mreq;
1253 bpos = mtod(mb, caddr_t);
1254 nfsm_fhtom(vp, v3);
1255 if (v3) {
1256 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
1257 txdr_hyper(uiop->uio_offset, tl);
1258 tl += 2;
1259 *tl++ = txdr_unsigned(len);
1260 *tl++ = txdr_unsigned(*iomode);
1261 *tl = txdr_unsigned(len);
1262 } else {
1263 u_int32_t x;
1264
1265 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1266 /* Set both "begin" and "current" to non-garbage. */
1267 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1268 *tl++ = x; /* "begin offset" */
1269 *tl++ = x; /* "current offset" */
1270 x = txdr_unsigned(len);
1271 *tl++ = x; /* total to this offset */
1272 *tl = x; /* size of this write */
1273 }
1274 nfsm_uiotom(uiop, len);
1275 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, cred);
1276 if (v3) {
1277 wccflag = NFSV3_WCCCHK;
1278 nfsm_wcc_data(vp, wccflag);
1279 if (!error) {
1280 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED
1281 + NFSX_V3WRITEVERF);
1282 rlen = fxdr_unsigned(int, *tl++);
1283 if (rlen == 0) {
1284 error = NFSERR_IO;
1285 m_freem(mrep);
1286 break;
1287 } else if (rlen < len) {
1288 backup = len - rlen;
1289 uiop->uio_iov->iov_base =
1290 (char *)uiop->uio_iov->iov_base -
1291 backup;
1292 uiop->uio_iov->iov_len += backup;
1293 uiop->uio_offset -= backup;
1294 uiop->uio_resid += backup;
1295 len = rlen;
1296 }
1297 commit = fxdr_unsigned(int, *tl++);
1298
1299 /*
1300 * Return the lowest committment level
1301 * obtained by any of the RPCs.
1302 */
1303 if (committed == NFSV3WRITE_FILESYNC)
1304 committed = commit;
1305 else if (committed == NFSV3WRITE_DATASYNC &&
1306 commit == NFSV3WRITE_UNSTABLE)
1307 committed = commit;
1308 mtx_lock(&nmp->nm_mtx);
1309 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1310 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1311 NFSX_V3WRITEVERF);
1312 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1313 } else if (bcmp((caddr_t)tl,
1314 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1315 *must_commit = 1;
1316 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1317 NFSX_V3WRITEVERF);
1318 }
1319 mtx_unlock(&nmp->nm_mtx);
1320 }
1321 } else {
1322 nfsm_loadattr(vp, NULL);
1323 }
1324 if (wccflag) {
1325 mtx_lock(&(VTONFS(vp))->n_mtx);
1326 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime;
1327 mtx_unlock(&(VTONFS(vp))->n_mtx);
1328 }
1329 m_freem(mrep);
1330 if (error)
1331 break;
1332 tsiz -= len;
1333 }
1334 nfsmout:
1335 if (vp->v_mount->mnt_kern_flag & MNTK_ASYNC)
1336 committed = NFSV3WRITE_FILESYNC;
1337 *iomode = committed;
1338 if (error)
1339 uiop->uio_resid = tsiz;
1340 return (error);
1341 }
1342
1343 /*
1344 * nfs mknod rpc
1345 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1346 * mode set to specify the file type and the size field for rdev.
1347 */
1348 static int
1349 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1350 struct vattr *vap)
1351 {
1352 struct nfsv2_sattr *sp;
1353 u_int32_t *tl;
1354 struct vnode *newvp = NULL;
1355 struct nfsnode *np = NULL;
1356 struct vattr vattr;
1357 caddr_t bpos, dpos;
1358 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1359 struct mbuf *mreq, *mrep, *md, *mb;
1360 u_int32_t rdev;
1361 int v3 = NFS_ISV3(dvp);
1362
1363 if (vap->va_type == VCHR || vap->va_type == VBLK)
1364 rdev = txdr_unsigned(vap->va_rdev);
1365 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1366 rdev = nfs_xdrneg1;
1367 else {
1368 return (EOPNOTSUPP);
1369 }
1370 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1371 return (error);
1372 }
1373 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1374 mreq = nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1375 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1376 mb = mreq;
1377 bpos = mtod(mb, caddr_t);
1378 nfsm_fhtom(dvp, v3);
1379 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1380 if (v3) {
1381 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1382 *tl++ = vtonfsv3_type(vap->va_type);
1383 nfsm_v3attrbuild(vap, FALSE);
1384 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1385 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
1386 *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1387 *tl = txdr_unsigned(uminor(vap->va_rdev));
1388 }
1389 } else {
1390 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1391 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1392 sp->sa_uid = nfs_xdrneg1;
1393 sp->sa_gid = nfs_xdrneg1;
1394 sp->sa_size = rdev;
1395 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1396 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1397 }
1398 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_thread, cnp->cn_cred);
1399 if (!error) {
1400 nfsm_mtofh(dvp, newvp, v3, gotvp);
1401 if (!gotvp) {
1402 if (newvp) {
1403 vput(newvp);
1404 newvp = NULL;
1405 }
1406 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1407 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1408 if (!error)
1409 newvp = NFSTOV(np);
1410 }
1411 }
1412 if (v3)
1413 nfsm_wcc_data(dvp, wccflag);
1414 m_freem(mrep);
1415 nfsmout:
1416 if (error) {
1417 if (newvp)
1418 vput(newvp);
1419 } else {
1420 if (cnp->cn_flags & MAKEENTRY)
1421 cache_enter(dvp, newvp, cnp);
1422 *vpp = newvp;
1423 }
1424 mtx_lock(&(VTONFS(dvp))->n_mtx);
1425 VTONFS(dvp)->n_flag |= NMODIFIED;
1426 if (!wccflag)
1427 VTONFS(dvp)->n_attrstamp = 0;
1428 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1429 return (error);
1430 }
1431
1432 /*
1433 * nfs mknod vop
1434 * just call nfs_mknodrpc() to do the work.
1435 */
1436 /* ARGSUSED */
1437 static int
1438 nfs_mknod(struct vop_mknod_args *ap)
1439 {
1440 return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1441 }
1442
1443 static u_long create_verf;
1444 /*
1445 * nfs file create call
1446 */
1447 static int
1448 nfs_create(struct vop_create_args *ap)
1449 {
1450 struct vnode *dvp = ap->a_dvp;
1451 struct vattr *vap = ap->a_vap;
1452 struct componentname *cnp = ap->a_cnp;
1453 struct nfsv2_sattr *sp;
1454 u_int32_t *tl;
1455 struct nfsnode *np = NULL;
1456 struct vnode *newvp = NULL;
1457 caddr_t bpos, dpos;
1458 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1459 struct mbuf *mreq, *mrep, *md, *mb;
1460 struct vattr vattr;
1461 int v3 = NFS_ISV3(dvp);
1462
1463 /*
1464 * Oops, not for me..
1465 */
1466 if (vap->va_type == VSOCK)
1467 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1468
1469 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1470 return (error);
1471 }
1472 if (vap->va_vaflags & VA_EXCLUSIVE)
1473 fmode |= O_EXCL;
1474 again:
1475 nfsstats.rpccnt[NFSPROC_CREATE]++;
1476 mreq = nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1477 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1478 mb = mreq;
1479 bpos = mtod(mb, caddr_t);
1480 nfsm_fhtom(dvp, v3);
1481 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1482 if (v3) {
1483 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1484 if (fmode & O_EXCL) {
1485 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1486 tl = nfsm_build(u_int32_t *, NFSX_V3CREATEVERF);
1487 #ifdef INET
1488 if (!TAILQ_EMPTY(&in_ifaddrhead))
1489 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr;
1490 else
1491 #endif
1492 *tl++ = create_verf;
1493 *tl = ++create_verf;
1494 } else {
1495 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1496 nfsm_v3attrbuild(vap, FALSE);
1497 }
1498 } else {
1499 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1500 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1501 sp->sa_uid = nfs_xdrneg1;
1502 sp->sa_gid = nfs_xdrneg1;
1503 sp->sa_size = 0;
1504 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1505 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1506 }
1507 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_thread, cnp->cn_cred);
1508 if (!error) {
1509 nfsm_mtofh(dvp, newvp, v3, gotvp);
1510 if (!gotvp) {
1511 if (newvp) {
1512 vput(newvp);
1513 newvp = NULL;
1514 }
1515 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1516 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1517 if (!error)
1518 newvp = NFSTOV(np);
1519 }
1520 }
1521 if (v3)
1522 nfsm_wcc_data(dvp, wccflag);
1523 m_freem(mrep);
1524 nfsmout:
1525 if (error) {
1526 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1527 fmode &= ~O_EXCL;
1528 goto again;
1529 }
1530 if (newvp)
1531 vput(newvp);
1532 } else if (v3 && (fmode & O_EXCL)) {
1533 /*
1534 * We are normally called with only a partially initialized
1535 * VAP. Since the NFSv3 spec says that server may use the
1536 * file attributes to store the verifier, the spec requires
1537 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1538 * in atime, but we can't really assume that all servers will
1539 * so we ensure that our SETATTR sets both atime and mtime.
1540 */
1541 if (vap->va_mtime.tv_sec == VNOVAL)
1542 vfs_timestamp(&vap->va_mtime);
1543 if (vap->va_atime.tv_sec == VNOVAL)
1544 vap->va_atime = vap->va_mtime;
1545 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_thread);
1546 if (error)
1547 vput(newvp);
1548 }
1549 if (!error) {
1550 if (cnp->cn_flags & MAKEENTRY)
1551 cache_enter(dvp, newvp, cnp);
1552 *ap->a_vpp = newvp;
1553 }
1554 mtx_lock(&(VTONFS(dvp))->n_mtx);
1555 VTONFS(dvp)->n_flag |= NMODIFIED;
1556 if (!wccflag)
1557 VTONFS(dvp)->n_attrstamp = 0;
1558 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1559 return (error);
1560 }
1561
1562 /*
1563 * nfs file remove call
1564 * To try and make nfs semantics closer to ufs semantics, a file that has
1565 * other processes using the vnode is renamed instead of removed and then
1566 * removed later on the last close.
1567 * - If v_usecount > 1
1568 * If a rename is not already in the works
1569 * call nfs_sillyrename() to set it up
1570 * else
1571 * do the remove rpc
1572 */
1573 static int
1574 nfs_remove(struct vop_remove_args *ap)
1575 {
1576 struct vnode *vp = ap->a_vp;
1577 struct vnode *dvp = ap->a_dvp;
1578 struct componentname *cnp = ap->a_cnp;
1579 struct nfsnode *np = VTONFS(vp);
1580 int error = 0;
1581 struct vattr vattr;
1582
1583 #ifndef DIAGNOSTIC
1584 if ((cnp->cn_flags & HASBUF) == 0)
1585 panic("nfs_remove: no name");
1586 if (vrefcnt(vp) < 1)
1587 panic("nfs_remove: bad v_usecount");
1588 #endif
1589 if (vp->v_type == VDIR)
1590 error = EPERM;
1591 else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1592 VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_thread) == 0 &&
1593 vattr.va_nlink > 1)) {
1594 /*
1595 * Purge the name cache so that the chance of a lookup for
1596 * the name succeeding while the remove is in progress is
1597 * minimized. Without node locking it can still happen, such
1598 * that an I/O op returns ESTALE, but since you get this if
1599 * another host removes the file..
1600 */
1601 cache_purge(vp);
1602 /*
1603 * throw away biocache buffers, mainly to avoid
1604 * unnecessary delayed writes later.
1605 */
1606 error = nfs_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1607 /* Do the rpc */
1608 if (error != EINTR && error != EIO)
1609 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1610 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1611 /*
1612 * Kludge City: If the first reply to the remove rpc is lost..
1613 * the reply to the retransmitted request will be ENOENT
1614 * since the file was in fact removed
1615 * Therefore, we cheat and return success.
1616 */
1617 if (error == ENOENT)
1618 error = 0;
1619 } else if (!np->n_sillyrename)
1620 error = nfs_sillyrename(dvp, vp, cnp);
1621 np->n_attrstamp = 0;
1622 return (error);
1623 }
1624
1625 /*
1626 * nfs file remove rpc called from nfs_inactive
1627 */
1628 int
1629 nfs_removeit(struct sillyrename *sp)
1630 {
1631 /*
1632 * Make sure that the directory vnode is still valid.
1633 * XXX we should lock sp->s_dvp here.
1634 */
1635 if (sp->s_dvp->v_type == VBAD)
1636 return (0);
1637 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1638 NULL));
1639 }
1640
1641 /*
1642 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1643 */
1644 static int
1645 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1646 struct ucred *cred, struct thread *td)
1647 {
1648 caddr_t bpos, dpos;
1649 int error = 0, wccflag = NFSV3_WCCRATTR;
1650 struct mbuf *mreq, *mrep, *md, *mb;
1651 int v3 = NFS_ISV3(dvp);
1652
1653 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1654 mreq = nfsm_reqhead(dvp, NFSPROC_REMOVE,
1655 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1656 mb = mreq;
1657 bpos = mtod(mb, caddr_t);
1658 nfsm_fhtom(dvp, v3);
1659 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1660 nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1661 if (v3)
1662 nfsm_wcc_data(dvp, wccflag);
1663 m_freem(mrep);
1664 nfsmout:
1665 mtx_lock(&(VTONFS(dvp))->n_mtx);
1666 VTONFS(dvp)->n_flag |= NMODIFIED;
1667 if (!wccflag)
1668 VTONFS(dvp)->n_attrstamp = 0;
1669 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1670 return (error);
1671 }
1672
1673 /*
1674 * nfs file rename call
1675 */
1676 static int
1677 nfs_rename(struct vop_rename_args *ap)
1678 {
1679 struct vnode *fvp = ap->a_fvp;
1680 struct vnode *tvp = ap->a_tvp;
1681 struct vnode *fdvp = ap->a_fdvp;
1682 struct vnode *tdvp = ap->a_tdvp;
1683 struct componentname *tcnp = ap->a_tcnp;
1684 struct componentname *fcnp = ap->a_fcnp;
1685 int error;
1686
1687 #ifndef DIAGNOSTIC
1688 if ((tcnp->cn_flags & HASBUF) == 0 ||
1689 (fcnp->cn_flags & HASBUF) == 0)
1690 panic("nfs_rename: no name");
1691 #endif
1692 /* Check for cross-device rename */
1693 if ((fvp->v_mount != tdvp->v_mount) ||
1694 (tvp && (fvp->v_mount != tvp->v_mount))) {
1695 error = EXDEV;
1696 goto out;
1697 }
1698
1699 if (fvp == tvp) {
1700 nfs_printf("nfs_rename: fvp == tvp (can't happen)\n");
1701 error = 0;
1702 goto out;
1703 }
1704 if ((error = vn_lock(fvp, LK_EXCLUSIVE, fcnp->cn_thread)) != 0)
1705 goto out;
1706
1707 /*
1708 * We have to flush B_DELWRI data prior to renaming
1709 * the file. If we don't, the delayed-write buffers
1710 * can be flushed out later after the file has gone stale
1711 * under NFSV3. NFSV2 does not have this problem because
1712 * ( as far as I can tell ) it flushes dirty buffers more
1713 * often.
1714 *
1715 * Skip the rename operation if the fsync fails, this can happen
1716 * due to the server's volume being full, when we pushed out data
1717 * that was written back to our cache earlier. Not checking for
1718 * this condition can result in potential (silent) data loss.
1719 */
1720 error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1721 VOP_UNLOCK(fvp, 0, fcnp->cn_thread);
1722 if (!error && tvp)
1723 error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1724 if (error)
1725 goto out;
1726
1727 /*
1728 * If the tvp exists and is in use, sillyrename it before doing the
1729 * rename of the new file over it.
1730 * XXX Can't sillyrename a directory.
1731 */
1732 if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1733 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1734 vput(tvp);
1735 tvp = NULL;
1736 }
1737
1738 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1739 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1740 tcnp->cn_thread);
1741
1742 if (fvp->v_type == VDIR) {
1743 if (tvp != NULL && tvp->v_type == VDIR)
1744 cache_purge(tdvp);
1745 cache_purge(fdvp);
1746 }
1747
1748 out:
1749 if (tdvp == tvp)
1750 vrele(tdvp);
1751 else
1752 vput(tdvp);
1753 if (tvp)
1754 vput(tvp);
1755 vrele(fdvp);
1756 vrele(fvp);
1757 /*
1758 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1759 */
1760 if (error == ENOENT)
1761 error = 0;
1762 return (error);
1763 }
1764
1765 /*
1766 * nfs file rename rpc called from nfs_remove() above
1767 */
1768 static int
1769 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1770 struct sillyrename *sp)
1771 {
1772
1773 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen, sdvp,
1774 sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_thread));
1775 }
1776
1777 /*
1778 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1779 */
1780 static int
1781 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1782 struct vnode *tdvp, const char *tnameptr, int tnamelen, struct ucred *cred,
1783 struct thread *td)
1784 {
1785 caddr_t bpos, dpos;
1786 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1787 struct mbuf *mreq, *mrep, *md, *mb;
1788 int v3 = NFS_ISV3(fdvp);
1789
1790 nfsstats.rpccnt[NFSPROC_RENAME]++;
1791 mreq = nfsm_reqhead(fdvp, NFSPROC_RENAME,
1792 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1793 nfsm_rndup(tnamelen));
1794 mb = mreq;
1795 bpos = mtod(mb, caddr_t);
1796 nfsm_fhtom(fdvp, v3);
1797 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1798 nfsm_fhtom(tdvp, v3);
1799 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1800 nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1801 if (v3) {
1802 nfsm_wcc_data(fdvp, fwccflag);
1803 nfsm_wcc_data(tdvp, twccflag);
1804 }
1805 m_freem(mrep);
1806 nfsmout:
1807 mtx_lock(&(VTONFS(fdvp))->n_mtx);
1808 VTONFS(fdvp)->n_flag |= NMODIFIED;
1809 mtx_unlock(&(VTONFS(fdvp))->n_mtx);
1810 mtx_lock(&(VTONFS(tdvp))->n_mtx);
1811 VTONFS(tdvp)->n_flag |= NMODIFIED;
1812 mtx_unlock(&(VTONFS(tdvp))->n_mtx);
1813 if (!fwccflag)
1814 VTONFS(fdvp)->n_attrstamp = 0;
1815 if (!twccflag)
1816 VTONFS(tdvp)->n_attrstamp = 0;
1817 return (error);
1818 }
1819
1820 /*
1821 * nfs hard link create call
1822 */
1823 static int
1824 nfs_link(struct vop_link_args *ap)
1825 {
1826 struct vnode *vp = ap->a_vp;
1827 struct vnode *tdvp = ap->a_tdvp;
1828 struct componentname *cnp = ap->a_cnp;
1829 caddr_t bpos, dpos;
1830 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1831 struct mbuf *mreq, *mrep, *md, *mb;
1832 int v3;
1833
1834 if (vp->v_mount != tdvp->v_mount) {
1835 return (EXDEV);
1836 }
1837
1838 /*
1839 * Push all writes to the server, so that the attribute cache
1840 * doesn't get "out of sync" with the server.
1841 * XXX There should be a better way!
1842 */
1843 VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1844
1845 v3 = NFS_ISV3(vp);
1846 nfsstats.rpccnt[NFSPROC_LINK]++;
1847 mreq = nfsm_reqhead(vp, NFSPROC_LINK,
1848 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1849 mb = mreq;
1850 bpos = mtod(mb, caddr_t);
1851 nfsm_fhtom(vp, v3);
1852 nfsm_fhtom(tdvp, v3);
1853 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1854 nfsm_request(vp, NFSPROC_LINK, cnp->cn_thread, cnp->cn_cred);
1855 if (v3) {
1856 nfsm_postop_attr(vp, attrflag);
1857 nfsm_wcc_data(tdvp, wccflag);
1858 }
1859 m_freem(mrep);
1860 nfsmout:
1861 mtx_lock(&(VTONFS(tdvp))->n_mtx);
1862 VTONFS(tdvp)->n_flag |= NMODIFIED;
1863 mtx_unlock(&(VTONFS(tdvp))->n_mtx);
1864 if (!attrflag)
1865 VTONFS(vp)->n_attrstamp = 0;
1866 if (!wccflag)
1867 VTONFS(tdvp)->n_attrstamp = 0;
1868 return (error);
1869 }
1870
1871 /*
1872 * nfs symbolic link create call
1873 */
1874 static int
1875 nfs_symlink(struct vop_symlink_args *ap)
1876 {
1877 struct vnode *dvp = ap->a_dvp;
1878 struct vattr *vap = ap->a_vap;
1879 struct componentname *cnp = ap->a_cnp;
1880 struct nfsv2_sattr *sp;
1881 caddr_t bpos, dpos;
1882 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1883 struct mbuf *mreq, *mrep, *md, *mb;
1884 struct vnode *newvp = NULL;
1885 int v3 = NFS_ISV3(dvp);
1886
1887 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1888 slen = strlen(ap->a_target);
1889 mreq = nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1890 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1891 mb = mreq;
1892 bpos = mtod(mb, caddr_t);
1893 nfsm_fhtom(dvp, v3);
1894 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1895 if (v3) {
1896 nfsm_v3attrbuild(vap, FALSE);
1897 }
1898 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1899 if (!v3) {
1900 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1901 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1902 sp->sa_uid = nfs_xdrneg1;
1903 sp->sa_gid = nfs_xdrneg1;
1904 sp->sa_size = nfs_xdrneg1;
1905 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1906 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1907 }
1908
1909 /*
1910 * Issue the NFS request and get the rpc response.
1911 *
1912 * Only NFSv3 responses returning an error of 0 actually return
1913 * a file handle that can be converted into newvp without having
1914 * to do an extra lookup rpc.
1915 */
1916 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_thread, cnp->cn_cred);
1917 if (v3) {
1918 if (error == 0)
1919 nfsm_mtofh(dvp, newvp, v3, gotvp);
1920 nfsm_wcc_data(dvp, wccflag);
1921 }
1922
1923 /*
1924 * out code jumps -> here, mrep is also freed.
1925 */
1926
1927 m_freem(mrep);
1928 nfsmout:
1929
1930 /*
1931 * If we do not have an error and we could not extract the newvp from
1932 * the response due to the request being NFSv2, we have to do a
1933 * lookup in order to obtain a newvp to return.
1934 */
1935 if (error == 0 && newvp == NULL) {
1936 struct nfsnode *np = NULL;
1937
1938 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1939 cnp->cn_cred, cnp->cn_thread, &np);
1940 if (!error)
1941 newvp = NFSTOV(np);
1942 }
1943 if (error) {
1944 if (newvp)
1945 vput(newvp);
1946 } else {
1947 *ap->a_vpp = newvp;
1948 }
1949 mtx_lock(&(VTONFS(dvp))->n_mtx);
1950 VTONFS(dvp)->n_flag |= NMODIFIED;
1951 mtx_unlock(&(VTONFS(dvp))->n_mtx);
1952 if (!wccflag)
1953 VTONFS(dvp)->n_attrstamp = 0;
1954 return (error);
1955 }
1956
1957 /*
1958 * nfs make dir call
1959 */
1960 static int
1961 nfs_mkdir(struct vop_mkdir_args *ap)
1962 {
1963 struct vnode *dvp = ap->a_dvp;
1964 struct vattr *vap = ap->a_vap;
1965 struct componentname *cnp = ap->a_cnp;
1966 struct nfsv2_sattr *sp;
1967 int len;
1968 struct nfsnode *np = NULL;
1969 struct vnode *newvp = NULL;
1970 caddr_t bpos, dpos;
1971 int error = 0, wccflag = NFSV3_WCCRATTR;
1972 int gotvp = 0;
1973 struct mbuf *mreq, *mrep, *md, *mb;
1974 struct vattr vattr;
1975 int v3 = NFS_ISV3(dvp);
1976
1977 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1978 return (error);
1979 }
1980 len = cnp->cn_namelen;
1981 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1982 mreq = nfsm_reqhead(dvp, NFSPROC_MKDIR,
1983 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1984 mb = mreq;
1985 bpos = mtod(mb, caddr_t);
1986 nfsm_fhtom(dvp, v3);
1987 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1988 if (v3) {
1989 nfsm_v3attrbuild(vap, FALSE);
1990 } else {
1991 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1992 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1993 sp->sa_uid = nfs_xdrneg1;
1994 sp->sa_gid = nfs_xdrneg1;
1995 sp->sa_size = nfs_xdrneg1;
1996 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1997 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1998 }
1999 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_thread, cnp->cn_cred);
2000 if (!error)
2001 nfsm_mtofh(dvp, newvp, v3, gotvp);
2002 if (v3)
2003 nfsm_wcc_data(dvp, wccflag);
2004 m_freem(mrep);
2005 nfsmout:
2006 mtx_lock(&(VTONFS(dvp))->n_mtx);
2007 VTONFS(dvp)->n_flag |= NMODIFIED;
2008 mtx_unlock(&(VTONFS(dvp))->n_mtx);
2009 if (!wccflag)
2010 VTONFS(dvp)->n_attrstamp = 0;
2011 if (error == 0 && newvp == NULL) {
2012 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2013 cnp->cn_thread, &np);
2014 if (!error) {
2015 newvp = NFSTOV(np);
2016 if (newvp->v_type != VDIR)
2017 error = EEXIST;
2018 }
2019 }
2020 if (error) {
2021 if (newvp)
2022 vput(newvp);
2023 } else
2024 *ap->a_vpp = newvp;
2025 return (error);
2026 }
2027
2028 /*
2029 * nfs remove directory call
2030 */
2031 static int
2032 nfs_rmdir(struct vop_rmdir_args *ap)
2033 {
2034 struct vnode *vp = ap->a_vp;
2035 struct vnode *dvp = ap->a_dvp;
2036 struct componentname *cnp = ap->a_cnp;
2037 caddr_t bpos, dpos;
2038 int error = 0, wccflag = NFSV3_WCCRATTR;
2039 struct mbuf *mreq, *mrep, *md, *mb;
2040 int v3 = NFS_ISV3(dvp);
2041
2042 if (dvp == vp)
2043 return (EINVAL);
2044 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2045 mreq = nfsm_reqhead(dvp, NFSPROC_RMDIR,
2046 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2047 mb = mreq;
2048 bpos = mtod(mb, caddr_t);
2049 nfsm_fhtom(dvp, v3);
2050 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2051 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_thread, cnp->cn_cred);
2052 if (v3)
2053 nfsm_wcc_data(dvp, wccflag);
2054 m_freem(mrep);
2055 nfsmout:
2056 mtx_lock(&(VTONFS(dvp))->n_mtx);
2057 VTONFS(dvp)->n_flag |= NMODIFIED;
2058 mtx_unlock(&(VTONFS(dvp))->n_mtx);
2059 if (!wccflag)
2060 VTONFS(dvp)->n_attrstamp = 0;
2061 cache_purge(dvp);
2062 cache_purge(vp);
2063 /*
2064 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2065 */
2066 if (error == ENOENT)
2067 error = 0;
2068 return (error);
2069 }
2070
2071 /*
2072 * nfs readdir call
2073 */
2074 static int
2075 nfs_readdir(struct vop_readdir_args *ap)
2076 {
2077 struct vnode *vp = ap->a_vp;
2078 struct nfsnode *np = VTONFS(vp);
2079 struct uio *uio = ap->a_uio;
2080 int tresid, error = 0;
2081 struct vattr vattr;
2082
2083 if (vp->v_type != VDIR)
2084 return(EPERM);
2085
2086 /*
2087 * First, check for hit on the EOF offset cache
2088 */
2089 if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2090 (np->n_flag & NMODIFIED) == 0) {
2091 if (VOP_GETATTR(vp, &vattr, ap->a_cred, uio->uio_td) == 0) {
2092 mtx_lock(&np->n_mtx);
2093 if (!NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2094 mtx_unlock(&np->n_mtx);
2095 nfsstats.direofcache_hits++;
2096 goto out;
2097 } else
2098 mtx_unlock(&np->n_mtx);
2099 }
2100 }
2101
2102 /*
2103 * Call nfs_bioread() to do the real work.
2104 */
2105 tresid = uio->uio_resid;
2106 error = nfs_bioread(vp, uio, 0, ap->a_cred);
2107
2108 if (!error && uio->uio_resid == tresid) {
2109 nfsstats.direofcache_misses++;
2110 }
2111 out:
2112 return (error);
2113 }
2114
2115 /*
2116 * Readdir rpc call.
2117 * Called from below the buffer cache by nfs_doio().
2118 */
2119 int
2120 nfs_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
2121 {
2122 int len, left;
2123 struct dirent *dp = NULL;
2124 u_int32_t *tl;
2125 caddr_t cp;
2126 nfsuint64 *cookiep;
2127 caddr_t bpos, dpos;
2128 struct mbuf *mreq, *mrep, *md, *mb;
2129 nfsuint64 cookie;
2130 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2131 struct nfsnode *dnp = VTONFS(vp);
2132 u_quad_t fileno;
2133 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2134 int attrflag;
2135 int v3 = NFS_ISV3(vp);
2136
2137 #ifndef DIAGNOSTIC
2138 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2139 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2140 panic("nfs readdirrpc bad uio");
2141 #endif
2142
2143 /*
2144 * If there is no cookie, assume directory was stale.
2145 */
2146 nfs_dircookie_lock(dnp);
2147 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2148 if (cookiep) {
2149 cookie = *cookiep;
2150 nfs_dircookie_unlock(dnp);
2151 } else {
2152 nfs_dircookie_unlock(dnp);
2153 return (NFSERR_BAD_COOKIE);
2154 }
2155
2156 /*
2157 * Loop around doing readdir rpc's of size nm_readdirsize
2158 * truncated to a multiple of DIRBLKSIZ.
2159 * The stopping criteria is EOF or buffer full.
2160 */
2161 while (more_dirs && bigenough) {
2162 nfsstats.rpccnt[NFSPROC_READDIR]++;
2163 mreq = nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2164 NFSX_READDIR(v3));
2165 mb = mreq;
2166 bpos = mtod(mb, caddr_t);
2167 nfsm_fhtom(vp, v3);
2168 if (v3) {
2169 tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
2170 *tl++ = cookie.nfsuquad[0];
2171 *tl++ = cookie.nfsuquad[1];
2172 mtx_lock(&dnp->n_mtx);
2173 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2174 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2175 mtx_unlock(&dnp->n_mtx);
2176 } else {
2177 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
2178 *tl++ = cookie.nfsuquad[0];
2179 }
2180 *tl = txdr_unsigned(nmp->nm_readdirsize);
2181 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, cred);
2182 if (v3) {
2183 nfsm_postop_attr(vp, attrflag);
2184 if (!error) {
2185 tl = nfsm_dissect(u_int32_t *,
2186 2 * NFSX_UNSIGNED);
2187 mtx_lock(&dnp->n_mtx);
2188 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2189 dnp->n_cookieverf.nfsuquad[1] = *tl;
2190 mtx_unlock(&dnp->n_mtx);
2191 } else {
2192 m_freem(mrep);
2193 goto nfsmout;
2194 }
2195 }
2196 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2197 more_dirs = fxdr_unsigned(int, *tl);
2198
2199 /* loop thru the dir entries, doctoring them to 4bsd form */
2200 while (more_dirs && bigenough) {
2201 if (v3) {
2202 tl = nfsm_dissect(u_int32_t *,
2203 3 * NFSX_UNSIGNED);
2204 fileno = fxdr_hyper(tl);
2205 len = fxdr_unsigned(int, *(tl + 2));
2206 } else {
2207 tl = nfsm_dissect(u_int32_t *,
2208 2 * NFSX_UNSIGNED);
2209 fileno = fxdr_unsigned(u_quad_t, *tl++);
2210 len = fxdr_unsigned(int, *tl);
2211 }
2212 if (len <= 0 || len > NFS_MAXNAMLEN) {
2213 error = EBADRPC;
2214 m_freem(mrep);
2215 goto nfsmout;
2216 }
2217 tlen = nfsm_rndup(len);
2218 if (tlen == len)
2219 tlen += 4; /* To ensure null termination */
2220 left = DIRBLKSIZ - blksiz;
2221 if ((tlen + DIRHDSIZ) > left) {
2222 dp->d_reclen += left;
2223 uiop->uio_iov->iov_base =
2224 (char *)uiop->uio_iov->iov_base + left;
2225 uiop->uio_iov->iov_len -= left;
2226 uiop->uio_offset += left;
2227 uiop->uio_resid -= left;
2228 blksiz = 0;
2229 }
2230 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2231 bigenough = 0;
2232 if (bigenough) {
2233 dp = (struct dirent *)uiop->uio_iov->iov_base;
2234 dp->d_fileno = (int)fileno;
2235 dp->d_namlen = len;
2236 dp->d_reclen = tlen + DIRHDSIZ;
2237 dp->d_type = DT_UNKNOWN;
2238 blksiz += dp->d_reclen;
2239 if (blksiz == DIRBLKSIZ)
2240 blksiz = 0;
2241 uiop->uio_offset += DIRHDSIZ;
2242 uiop->uio_resid -= DIRHDSIZ;
2243 uiop->uio_iov->iov_base =
2244 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
2245 uiop->uio_iov->iov_len -= DIRHDSIZ;
2246 nfsm_mtouio(uiop, len);
2247 cp = uiop->uio_iov->iov_base;
2248 tlen -= len;
2249 *cp = '\0'; /* null terminate */
2250 uiop->uio_iov->iov_base =
2251 (char *)uiop->uio_iov->iov_base + tlen;
2252 uiop->uio_iov->iov_len -= tlen;
2253 uiop->uio_offset += tlen;
2254 uiop->uio_resid -= tlen;
2255 } else
2256 nfsm_adv(nfsm_rndup(len));
2257 if (v3) {
2258 tl = nfsm_dissect(u_int32_t *,
2259 3 * NFSX_UNSIGNED);
2260 } else {
2261 tl = nfsm_dissect(u_int32_t *,
2262 2 * NFSX_UNSIGNED);
2263 }
2264 if (bigenough) {
2265 cookie.nfsuquad[0] = *tl++;
2266 if (v3)
2267 cookie.nfsuquad[1] = *tl++;
2268 } else if (v3)
2269 tl += 2;
2270 else
2271 tl++;
2272 more_dirs = fxdr_unsigned(int, *tl);
2273 }
2274 /*
2275 * If at end of rpc data, get the eof boolean
2276 */
2277 if (!more_dirs) {
2278 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2279 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2280 }
2281 m_freem(mrep);
2282 }
2283 /*
2284 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2285 * by increasing d_reclen for the last record.
2286 */
2287 if (blksiz > 0) {
2288 left = DIRBLKSIZ - blksiz;
2289 dp->d_reclen += left;
2290 uiop->uio_iov->iov_base =
2291 (char *)uiop->uio_iov->iov_base + left;
2292 uiop->uio_iov->iov_len -= left;
2293 uiop->uio_offset += left;
2294 uiop->uio_resid -= left;
2295 }
2296
2297 /*
2298 * We are now either at the end of the directory or have filled the
2299 * block.
2300 */
2301 if (bigenough)
2302 dnp->n_direofoffset = uiop->uio_offset;
2303 else {
2304 if (uiop->uio_resid > 0)
2305 nfs_printf("EEK! readdirrpc resid > 0\n");
2306 nfs_dircookie_lock(dnp);
2307 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2308 *cookiep = cookie;
2309 nfs_dircookie_unlock(dnp);
2310 }
2311 nfsmout:
2312 return (error);
2313 }
2314
2315 /*
2316 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2317 */
2318 int
2319 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
2320 {
2321 int len, left;
2322 struct dirent *dp;
2323 u_int32_t *tl;
2324 caddr_t cp;
2325 struct vnode *newvp;
2326 nfsuint64 *cookiep;
2327 caddr_t bpos, dpos, dpossav1, dpossav2;
2328 struct mbuf *mreq, *mrep, *md, *mb, *mdsav1, *mdsav2;
2329 struct nameidata nami, *ndp = &nami;
2330 struct componentname *cnp = &ndp->ni_cnd;
2331 nfsuint64 cookie;
2332 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2333 struct nfsnode *dnp = VTONFS(vp), *np;
2334 nfsfh_t *fhp;
2335 u_quad_t fileno;
2336 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2337 int attrflag, fhsize;
2338
2339 #ifndef nolint
2340 dp = NULL;
2341 #endif
2342 #ifndef DIAGNOSTIC
2343 if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2344 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2345 panic("nfs readdirplusrpc bad uio");
2346 #endif
2347 ndp->ni_dvp = vp;
2348 newvp = NULLVP;
2349
2350 /*
2351 * If there is no cookie, assume directory was stale.
2352 */
2353 nfs_dircookie_lock(dnp);
2354 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2355 if (cookiep) {
2356 cookie = *cookiep;
2357 nfs_dircookie_unlock(dnp);
2358 } else {
2359 nfs_dircookie_unlock(dnp);
2360 return (NFSERR_BAD_COOKIE);
2361 }
2362 /*
2363 * Loop around doing readdir rpc's of size nm_readdirsize
2364 * truncated to a multiple of DIRBLKSIZ.
2365 * The stopping criteria is EOF or buffer full.
2366 */
2367 while (more_dirs && bigenough) {
2368 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2369 mreq = nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2370 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2371 mb = mreq;
2372 bpos = mtod(mb, caddr_t);
2373 nfsm_fhtom(vp, 1);
2374 tl = nfsm_build(u_int32_t *, 6 * NFSX_UNSIGNED);
2375 *tl++ = cookie.nfsuquad[0];
2376 *tl++ = cookie.nfsuquad[1];
2377 mtx_lock(&dnp->n_mtx);
2378 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2379 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2380 mtx_unlock(&dnp->n_mtx);
2381 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2382 *tl = txdr_unsigned(nmp->nm_rsize);
2383 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, cred);
2384 nfsm_postop_attr(vp, attrflag);
2385 if (error) {
2386 m_freem(mrep);
2387 goto nfsmout;
2388 }
2389 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2390 mtx_lock(&dnp->n_mtx);
2391 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2392 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2393 mtx_unlock(&dnp->n_mtx);
2394 more_dirs = fxdr_unsigned(int, *tl);
2395
2396 /* loop thru the dir entries, doctoring them to 4bsd form */
2397 while (more_dirs && bigenough) {
2398 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2399 fileno = fxdr_hyper(tl);
2400 len = fxdr_unsigned(int, *(tl + 2));
2401 if (len <= 0 || len > NFS_MAXNAMLEN) {
2402 error = EBADRPC;
2403 m_freem(mrep);
2404 goto nfsmout;
2405 }
2406 tlen = nfsm_rndup(len);
2407 if (tlen == len)
2408 tlen += 4; /* To ensure null termination*/
2409 left = DIRBLKSIZ - blksiz;
2410 if ((tlen + DIRHDSIZ) > left) {
2411 dp->d_reclen += left;
2412 uiop->uio_iov->iov_base =
2413 (char *)uiop->uio_iov->iov_base + left;
2414 uiop->uio_iov->iov_len -= left;
2415 uiop->uio_offset += left;
2416 uiop->uio_resid -= left;
2417 blksiz = 0;
2418 }
2419 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2420 bigenough = 0;
2421 if (bigenough) {
2422 dp = (struct dirent *)uiop->uio_iov->iov_base;
2423 dp->d_fileno = (int)fileno;
2424 dp->d_namlen = len;
2425 dp->d_reclen = tlen + DIRHDSIZ;
2426 dp->d_type = DT_UNKNOWN;
2427 blksiz += dp->d_reclen;
2428 if (blksiz == DIRBLKSIZ)
2429 blksiz = 0;
2430 uiop->uio_offset += DIRHDSIZ;
2431 uiop->uio_resid -= DIRHDSIZ;
2432 uiop->uio_iov->iov_base =
2433 (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
2434 uiop->uio_iov->iov_len -= DIRHDSIZ;
2435 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2436 cnp->cn_namelen = len;
2437 nfsm_mtouio(uiop, len);
2438 cp = uiop->uio_iov->iov_base;
2439 tlen -= len;
2440 *cp = '\0';
2441 uiop->uio_iov->iov_base =
2442 (char *)uiop->uio_iov->iov_base + tlen;
2443 uiop->uio_iov->iov_len -= tlen;
2444 uiop->uio_offset += tlen;
2445 uiop->uio_resid -= tlen;
2446 } else
2447 nfsm_adv(nfsm_rndup(len));
2448 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2449 if (bigenough) {
2450 cookie.nfsuquad[0] = *tl++;
2451 cookie.nfsuquad[1] = *tl++;
2452 } else
2453 tl += 2;
2454
2455 /*
2456 * Since the attributes are before the file handle
2457 * (sigh), we must skip over the attributes and then
2458 * come back and get them.
2459 */
2460 attrflag = fxdr_unsigned(int, *tl);
2461 if (attrflag) {
2462 dpossav1 = dpos;
2463 mdsav1 = md;
2464 nfsm_adv(NFSX_V3FATTR);
2465 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2466 doit = fxdr_unsigned(int, *tl);
2467 /*
2468 * Skip loading the attrs for "..". There's a
2469 * race between loading the attrs here and
2470 * lookups that look for the directory currently
2471 * being read (in the parent). We try to acquire
2472 * the exclusive lock on ".." here, owning the
2473 * lock on the directory being read. Lookup will
2474 * hold the lock on ".." and try to acquire the
2475 * lock on the directory being read.
2476 *
2477 * There are other ways of fixing this, one would
2478 * be to do a trylock on the ".." vnode and skip
2479 * loading the attrs on ".." if it happens to be
2480 * locked by another process. But skipping the
2481 * attrload on ".." seems the easiest option.
2482 */
2483 if (strcmp(dp->d_name, "..") == 0) {
2484 doit = 0;
2485 /*
2486 * We've already skipped over the attrs,
2487 * skip over the filehandle. And store d_type
2488 * as VDIR.
2489 */
2490 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2491 i = fxdr_unsigned(int, *tl);
2492 nfsm_adv(nfsm_rndup(i));
2493 dp->d_type = IFTODT(VTTOIF(VDIR));
2494 }
2495 if (doit) {
2496 nfsm_getfh(fhp, fhsize, 1);
2497 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2498 VREF(vp);
2499 newvp = vp;
2500 np = dnp;
2501 } else {
2502 error = nfs_nget(vp->v_mount, fhp,
2503 fhsize, &np, LK_EXCLUSIVE);
2504 if (error)
2505 doit = 0;
2506 else
2507 newvp = NFSTOV(np);
2508 }
2509 }
2510 if (doit && bigenough) {
2511 dpossav2 = dpos;
2512 dpos = dpossav1;
2513 mdsav2 = md;
2514 md = mdsav1;
2515 nfsm_loadattr(newvp, NULL);
2516 dpos = dpossav2;
2517 md = mdsav2;
2518 dp->d_type =
2519 IFTODT(VTTOIF(np->n_vattr.va_type));
2520 ndp->ni_vp = newvp;
2521 /* Update n_ctime, so subsequent lookup doesn't purge entry */
2522 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
2523 cache_enter(ndp->ni_dvp, ndp->ni_vp, cnp);
2524 }
2525 } else {
2526 /* Just skip over the file handle */
2527 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2528 i = fxdr_unsigned(int, *tl);
2529 if (i) {
2530 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2531 fhsize = fxdr_unsigned(int, *tl);
2532 nfsm_adv(nfsm_rndup(fhsize));
2533 }
2534 }
2535 if (newvp != NULLVP) {
2536 if (newvp == vp)
2537 vrele(newvp);
2538 else
2539 vput(newvp);
2540 newvp = NULLVP;
2541 }
2542 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2543 more_dirs = fxdr_unsigned(int, *tl);
2544 }
2545 /*
2546 * If at end of rpc data, get the eof boolean
2547 */
2548 if (!more_dirs) {
2549 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2550 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2551 }
2552 m_freem(mrep);
2553 }
2554 /*
2555 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2556 * by increasing d_reclen for the last record.
2557 */
2558 if (blksiz > 0) {
2559 left = DIRBLKSIZ - blksiz;
2560 dp->d_reclen += left;
2561 uiop->uio_iov->iov_base =
2562 (char *)uiop->uio_iov->iov_base + left;
2563 uiop->uio_iov->iov_len -= left;
2564 uiop->uio_offset += left;
2565 uiop->uio_resid -= left;
2566 }
2567
2568 /*
2569 * We are now either at the end of the directory or have filled the
2570 * block.
2571 */
2572 if (bigenough)
2573 dnp->n_direofoffset = uiop->uio_offset;
2574 else {
2575 if (uiop->uio_resid > 0)
2576 nfs_printf("EEK! readdirplusrpc resid > 0\n");
2577 nfs_dircookie_lock(dnp);
2578 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2579 *cookiep = cookie;
2580 nfs_dircookie_unlock(dnp);
2581 }
2582 nfsmout:
2583 if (newvp != NULLVP) {
2584 if (newvp == vp)
2585 vrele(newvp);
2586 else
2587 vput(newvp);
2588 newvp = NULLVP;
2589 }
2590 return (error);
2591 }
2592
2593 /*
2594 * Silly rename. To make the NFS filesystem that is stateless look a little
2595 * more like the "ufs" a remove of an active vnode is translated to a rename
2596 * to a funny looking filename that is removed by nfs_inactive on the
2597 * nfsnode. There is the potential for another process on a different client
2598 * to create the same funny name between the nfs_lookitup() fails and the
2599 * nfs_rename() completes, but...
2600 */
2601 static int
2602 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2603 {
2604 struct sillyrename *sp;
2605 struct nfsnode *np;
2606 int error;
2607 short pid;
2608 unsigned int lticks;
2609
2610 cache_purge(dvp);
2611 np = VTONFS(vp);
2612 #ifndef DIAGNOSTIC
2613 if (vp->v_type == VDIR)
2614 panic("nfs: sillyrename dir");
2615 #endif
2616 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2617 M_NFSREQ, M_WAITOK);
2618 sp->s_cred = crhold(cnp->cn_cred);
2619 sp->s_dvp = dvp;
2620 sp->s_removeit = nfs_removeit;
2621 VREF(dvp);
2622
2623 /*
2624 * Fudge together a funny name.
2625 * Changing the format of the funny name to accomodate more
2626 * sillynames per directory.
2627 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2628 * CPU ticks since boot.
2629 */
2630 pid = cnp->cn_thread->td_proc->p_pid;
2631 lticks = (unsigned int)ticks;
2632 for ( ; ; ) {
2633 sp->s_namlen = sprintf(sp->s_name,
2634 ".nfs.%08x.%04x4.4", lticks,
2635 pid);
2636 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2637 cnp->cn_thread, NULL))
2638 break;
2639 lticks++;
2640 }
2641 error = nfs_renameit(dvp, cnp, sp);
2642 if (error)
2643 goto bad;
2644 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2645 cnp->cn_thread, &np);
2646 np->n_sillyrename = sp;
2647 return (0);
2648 bad:
2649 vrele(sp->s_dvp);
2650 crfree(sp->s_cred);
2651 free((caddr_t)sp, M_NFSREQ);
2652 return (error);
2653 }
2654
2655 /*
2656 * Look up a file name and optionally either update the file handle or
2657 * allocate an nfsnode, depending on the value of npp.
2658 * npp == NULL --> just do the lookup
2659 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2660 * handled too
2661 * *npp != NULL --> update the file handle in the vnode
2662 */
2663 static int
2664 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2665 struct thread *td, struct nfsnode **npp)
2666 {
2667 struct vnode *newvp = NULL;
2668 struct nfsnode *np, *dnp = VTONFS(dvp);
2669 caddr_t bpos, dpos;
2670 int error = 0, fhlen, attrflag;
2671 struct mbuf *mreq, *mrep, *md, *mb;
2672 nfsfh_t *nfhp;
2673 int v3 = NFS_ISV3(dvp);
2674
2675 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2676 mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2677 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2678 mb = mreq;
2679 bpos = mtod(mb, caddr_t);
2680 nfsm_fhtom(dvp, v3);
2681 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2682 nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2683 if (npp && !error) {
2684 nfsm_getfh(nfhp, fhlen, v3);
2685 if (*npp) {
2686 np = *npp;
2687 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2688 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2689 np->n_fhp = &np->n_fh;
2690 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2691 np->n_fhp =(nfsfh_t *)malloc(fhlen, M_NFSBIGFH, M_WAITOK);
2692 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2693 np->n_fhsize = fhlen;
2694 newvp = NFSTOV(np);
2695 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2696 VREF(dvp);
2697 newvp = dvp;
2698 } else {
2699 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, LK_EXCLUSIVE);
2700 if (error) {
2701 m_freem(mrep);
2702 return (error);
2703 }
2704 newvp = NFSTOV(np);
2705 }
2706 if (v3) {
2707 nfsm_postop_attr(newvp, attrflag);
2708 if (!attrflag && *npp == NULL) {
2709 m_freem(mrep);
2710 if (newvp == dvp)
2711 vrele(newvp);
2712 else
2713 vput(newvp);
2714 return (ENOENT);
2715 }
2716 } else
2717 nfsm_loadattr(newvp, NULL);
2718 }
2719 m_freem(mrep);
2720 nfsmout:
2721 if (npp && *npp == NULL) {
2722 if (error) {
2723 if (newvp) {
2724 if (newvp == dvp)
2725 vrele(newvp);
2726 else
2727 vput(newvp);
2728 }
2729 } else
2730 *npp = np;
2731 }
2732 return (error);
2733 }
2734
2735 /*
2736 * Nfs Version 3 commit rpc
2737 */
2738 int
2739 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2740 struct thread *td)
2741 {
2742 u_int32_t *tl;
2743 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2744 caddr_t bpos, dpos;
2745 int error = 0, wccflag = NFSV3_WCCRATTR;
2746 struct mbuf *mreq, *mrep, *md, *mb;
2747
2748 mtx_lock(&nmp->nm_mtx);
2749 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2750 mtx_unlock(&nmp->nm_mtx);
2751 return (0);
2752 }
2753 mtx_unlock(&nmp->nm_mtx);
2754 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2755 mreq = nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2756 mb = mreq;
2757 bpos = mtod(mb, caddr_t);
2758 nfsm_fhtom(vp, 1);
2759 tl = nfsm_build(u_int32_t *, 3 * NFSX_UNSIGNED);
2760 txdr_hyper(offset, tl);
2761 tl += 2;
2762 *tl = txdr_unsigned(cnt);
2763 nfsm_request(vp, NFSPROC_COMMIT, td, cred);
2764 nfsm_wcc_data(vp, wccflag);
2765 if (!error) {
2766 tl = nfsm_dissect(u_int32_t *, NFSX_V3WRITEVERF);
2767 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2768 NFSX_V3WRITEVERF)) {
2769 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2770 NFSX_V3WRITEVERF);
2771 error = NFSERR_STALEWRITEVERF;
2772 }
2773 }
2774 m_freem(mrep);
2775 nfsmout:
2776 return (error);
2777 }
2778
2779 /*
2780 * Strategy routine.
2781 * For async requests when nfsiod(s) are running, queue the request by
2782 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2783 * request.
2784 */
2785 static int
2786 nfs_strategy(struct vop_strategy_args *ap)
2787 {
2788 struct buf *bp = ap->a_bp;
2789 struct ucred *cr;
2790
2791 KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2792 KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2793
2794 if (bp->b_iocmd == BIO_READ)
2795 cr = bp->b_rcred;
2796 else
2797 cr = bp->b_wcred;
2798
2799 /*
2800 * If the op is asynchronous and an i/o daemon is waiting
2801 * queue the request, wake it up and wait for completion
2802 * otherwise just do it ourselves.
2803 */
2804 if ((bp->b_flags & B_ASYNC) == 0 ||
2805 nfs_asyncio(VFSTONFS(ap->a_vp->v_mount), bp, NOCRED, curthread))
2806 (void)nfs_doio(ap->a_vp, bp, cr, curthread);
2807 return (0);
2808 }
2809
2810 /*
2811 * fsync vnode op. Just call nfs_flush() with commit == 1.
2812 */
2813 /* ARGSUSED */
2814 static int
2815 nfs_fsync(struct vop_fsync_args *ap)
2816 {
2817 return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2818 }
2819
2820 /*
2821 * Flush all the blocks associated with a vnode.
2822 * Walk through the buffer pool and push any dirty pages
2823 * associated with the vnode.
2824 */
2825 static int
2826 nfs_flush(struct vnode *vp, int waitfor, struct thread *td,
2827 int commit)
2828 {
2829 struct nfsnode *np = VTONFS(vp);
2830 struct buf *bp;
2831 int i;
2832 struct buf *nbp;
2833 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2834 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2835 int passone = 1;
2836 u_quad_t off, endoff, toff;
2837 struct ucred* wcred = NULL;
2838 struct buf **bvec = NULL;
2839 #ifndef NFS_COMMITBVECSIZ
2840 #define NFS_COMMITBVECSIZ 20
2841 #endif
2842 struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2843 int bvecsize = 0, bveccount;
2844
2845 if (nmp->nm_flag & NFSMNT_INT)
2846 slpflag = PCATCH;
2847 if (!commit)
2848 passone = 0;
2849 /*
2850 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2851 * server, but has not been committed to stable storage on the server
2852 * yet. On the first pass, the byte range is worked out and the commit
2853 * rpc is done. On the second pass, nfs_writebp() is called to do the
2854 * job.
2855 */
2856 again:
2857 off = (u_quad_t)-1;
2858 endoff = 0;
2859 bvecpos = 0;
2860 if (NFS_ISV3(vp) && commit) {
2861 s = splbio();
2862 if (bvec != NULL && bvec != bvec_on_stack)
2863 free(bvec, M_TEMP);
2864 /*
2865 * Count up how many buffers waiting for a commit.
2866 */
2867 bveccount = 0;
2868 VI_LOCK(vp);
2869 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
2870 if (BUF_REFCNT(bp) == 0 &&
2871 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2872 == (B_DELWRI | B_NEEDCOMMIT))
2873 bveccount++;
2874 }
2875 /*
2876 * Allocate space to remember the list of bufs to commit. It is
2877 * important to use M_NOWAIT here to avoid a race with nfs_write.
2878 * If we can't get memory (for whatever reason), we will end up
2879 * committing the buffers one-by-one in the loop below.
2880 */
2881 if (bveccount > NFS_COMMITBVECSIZ) {
2882 /*
2883 * Release the vnode interlock to avoid a lock
2884 * order reversal.
2885 */
2886 VI_UNLOCK(vp);
2887 bvec = (struct buf **)
2888 malloc(bveccount * sizeof(struct buf *),
2889 M_TEMP, M_NOWAIT);
2890 VI_LOCK(vp);
2891 if (bvec == NULL) {
2892 bvec = bvec_on_stack;
2893 bvecsize = NFS_COMMITBVECSIZ;
2894 } else
2895 bvecsize = bveccount;
2896 } else {
2897 bvec = bvec_on_stack;
2898 bvecsize = NFS_COMMITBVECSIZ;
2899 }
2900 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
2901 if (bvecpos >= bvecsize)
2902 break;
2903 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2904 nbp = TAILQ_NEXT(bp, b_bobufs);
2905 continue;
2906 }
2907 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2908 (B_DELWRI | B_NEEDCOMMIT)) {
2909 BUF_UNLOCK(bp);
2910 nbp = TAILQ_NEXT(bp, b_bobufs);
2911 continue;
2912 }
2913 VI_UNLOCK(vp);
2914 bremfree(bp);
2915 /*
2916 * Work out if all buffers are using the same cred
2917 * so we can deal with them all with one commit.
2918 *
2919 * NOTE: we are not clearing B_DONE here, so we have
2920 * to do it later on in this routine if we intend to
2921 * initiate I/O on the bp.
2922 *
2923 * Note: to avoid loopback deadlocks, we do not
2924 * assign b_runningbufspace.
2925 */
2926 if (wcred == NULL)
2927 wcred = bp->b_wcred;
2928 else if (wcred != bp->b_wcred)
2929 wcred = NOCRED;
2930 vfs_busy_pages(bp, 1);
2931
2932 VI_LOCK(vp);
2933 /*
2934 * bp is protected by being locked, but nbp is not
2935 * and vfs_busy_pages() may sleep. We have to
2936 * recalculate nbp.
2937 */
2938 nbp = TAILQ_NEXT(bp, b_bobufs);
2939
2940 /*
2941 * A list of these buffers is kept so that the
2942 * second loop knows which buffers have actually
2943 * been committed. This is necessary, since there
2944 * may be a race between the commit rpc and new
2945 * uncommitted writes on the file.
2946 */
2947 bvec[bvecpos++] = bp;
2948 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2949 bp->b_dirtyoff;
2950 if (toff < off)
2951 off = toff;
2952 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2953 if (toff > endoff)
2954 endoff = toff;
2955 }
2956 splx(s);
2957 VI_UNLOCK(vp);
2958 }
2959 if (bvecpos > 0) {
2960 /*
2961 * Commit data on the server, as required.
2962 * If all bufs are using the same wcred, then use that with
2963 * one call for all of them, otherwise commit each one
2964 * separately.
2965 */
2966 if (wcred != NOCRED)
2967 retv = nfs_commit(vp, off, (int)(endoff - off),
2968 wcred, td);
2969 else {
2970 retv = 0;
2971 for (i = 0; i < bvecpos; i++) {
2972 off_t off, size;
2973 bp = bvec[i];
2974 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2975 bp->b_dirtyoff;
2976 size = (u_quad_t)(bp->b_dirtyend
2977 - bp->b_dirtyoff);
2978 retv = nfs_commit(vp, off, (int)size,
2979 bp->b_wcred, td);
2980 if (retv) break;
2981 }
2982 }
2983
2984 if (retv == NFSERR_STALEWRITEVERF)
2985 nfs_clearcommit(vp->v_mount);
2986
2987 /*
2988 * Now, either mark the blocks I/O done or mark the
2989 * blocks dirty, depending on whether the commit
2990 * succeeded.
2991 */
2992 for (i = 0; i < bvecpos; i++) {
2993 bp = bvec[i];
2994 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2995 if (retv) {
2996 /*
2997 * Error, leave B_DELWRI intact
2998 */
2999 vfs_unbusy_pages(bp);
3000 brelse(bp);
3001 } else {
3002 /*
3003 * Success, remove B_DELWRI ( bundirty() ).
3004 *
3005 * b_dirtyoff/b_dirtyend seem to be NFS
3006 * specific. We should probably move that
3007 * into bundirty(). XXX
3008 */
3009 s = splbio();
3010 bufobj_wref(&vp->v_bufobj);
3011 bp->b_flags |= B_ASYNC;
3012 bundirty(bp);
3013 bp->b_flags &= ~B_DONE;
3014 bp->b_ioflags &= ~BIO_ERROR;
3015 bp->b_dirtyoff = bp->b_dirtyend = 0;
3016 splx(s);
3017 bufdone(bp);
3018 }
3019 }
3020 }
3021
3022 /*
3023 * Start/do any write(s) that are required.
3024 */
3025 loop:
3026 s = splbio();
3027 VI_LOCK(vp);
3028 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
3029 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3030 if (waitfor != MNT_WAIT || passone)
3031 continue;
3032
3033 error = BUF_TIMELOCK(bp,
3034 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
3035 VI_MTX(vp), "nfsfsync", slpflag, slptimeo);
3036 splx(s);
3037 if (error == 0) {
3038 BUF_UNLOCK(bp);
3039 goto loop;
3040 }
3041 if (error == ENOLCK)
3042 goto loop;
3043 if (nfs_sigintr(nmp, NULL, td)) {
3044 error = EINTR;
3045 goto done;
3046 }
3047 if (slpflag == PCATCH) {
3048 slpflag = 0;
3049 slptimeo = 2 * hz;
3050 }
3051 goto loop;
3052 }
3053 if ((bp->b_flags & B_DELWRI) == 0)
3054 panic("nfs_fsync: not dirty");
3055 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3056 BUF_UNLOCK(bp);
3057 continue;
3058 }
3059 VI_UNLOCK(vp);
3060 bremfree(bp);
3061 if (passone || !commit)
3062 bp->b_flags |= B_ASYNC;
3063 else
3064 bp->b_flags |= B_ASYNC;
3065 splx(s);
3066 bwrite(bp);
3067 if (nfs_sigintr(nmp, NULL, td)) {
3068 error = EINTR;
3069 goto done;
3070 }
3071 goto loop;
3072 }
3073 splx(s);
3074 if (passone) {
3075 passone = 0;
3076 VI_UNLOCK(vp);
3077 goto again;
3078 }
3079 if (waitfor == MNT_WAIT) {
3080 while (vp->v_bufobj.bo_numoutput) {
3081 error = bufobj_wwait(&vp->v_bufobj, slpflag, slptimeo);
3082 if (error) {
3083 VI_UNLOCK(vp);
3084 error = nfs_sigintr(nmp, NULL, td);
3085 if (error)
3086 goto done;
3087 if (slpflag == PCATCH) {
3088 slpflag = 0;
3089 slptimeo = 2 * hz;
3090 }
3091 VI_LOCK(vp);
3092 }
3093 }
3094 if (vp->v_bufobj.bo_dirty.bv_cnt != 0 && commit) {
3095 VI_UNLOCK(vp);
3096 goto loop;
3097 }
3098 /*
3099 * Wait for all the async IO requests to drain
3100 */
3101 VI_UNLOCK(vp);
3102 mtx_lock(&np->n_mtx);
3103 while (np->n_directio_asyncwr > 0) {
3104 np->n_flag |= NFSYNCWAIT;
3105 error = nfs_msleep(td, (caddr_t)&np->n_directio_asyncwr,
3106 &np->n_mtx, slpflag | (PRIBIO + 1),
3107 "nfsfsync", 0);
3108 if (error) {
3109 if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
3110 mtx_unlock(&np->n_mtx);
3111 error = EINTR;
3112 goto done;
3113 }
3114 }
3115 }
3116 mtx_unlock(&np->n_mtx);
3117 } else
3118 VI_UNLOCK(vp);
3119 mtx_lock(&np->n_mtx);
3120 if (np->n_flag & NWRITEERR) {
3121 error = np->n_error;
3122 np->n_flag &= ~NWRITEERR;
3123 }
3124 if (commit && vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
3125 vp->v_bufobj.bo_numoutput == 0 && np->n_directio_asyncwr == 0)
3126 np->n_flag &= ~NMODIFIED;
3127 mtx_unlock(&np->n_mtx);
3128 done:
3129 if (bvec != NULL && bvec != bvec_on_stack)
3130 free(bvec, M_TEMP);
3131 return (error);
3132 }
3133
3134 /*
3135 * NFS advisory byte-level locks.
3136 */
3137 static int
3138 nfs_advlock(struct vop_advlock_args *ap)
3139 {
3140 struct vnode *vp = ap->a_vp;
3141 u_quad_t size;
3142 int error;
3143
3144 error = vn_lock(vp, LK_SHARED, curthread);
3145 if (error)
3146 return (error);
3147 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3148 size = VTONFS(vp)->n_size;
3149 VOP_UNLOCK(vp, 0, curthread);
3150 error = lf_advlock(ap, &(vp->v_lockf), size);
3151 } else {
3152 if (nfs_advlock_p)
3153 error = nfs_advlock_p(ap);
3154 else
3155 error = ENOLCK;
3156 }
3157
3158 return (error);
3159 }
3160
3161 /*
3162 * NFS advisory byte-level locks.
3163 */
3164 static int
3165 nfs_advlockasync(struct vop_advlockasync_args *ap)
3166 {
3167 struct vnode *vp = ap->a_vp;
3168 u_quad_t size;
3169 int error;
3170
3171 error = vn_lock(vp, LK_SHARED, curthread);
3172 if (error)
3173 return (error);
3174 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3175 size = VTONFS(vp)->n_size;
3176 VOP_UNLOCK(vp, 0, curthread);
3177 error = lf_advlockasync(ap, &(vp->v_lockf), size);
3178 } else {
3179 VOP_UNLOCK(vp, 0, curthread);
3180 error = EOPNOTSUPP;
3181 }
3182 return (error);
3183 }
3184
3185 /*
3186 * Print out the contents of an nfsnode.
3187 */
3188 static int
3189 nfs_print(struct vop_print_args *ap)
3190 {
3191 struct vnode *vp = ap->a_vp;
3192 struct nfsnode *np = VTONFS(vp);
3193
3194 nfs_printf("\tfileid %ld fsid 0x%x",
3195 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3196 if (vp->v_type == VFIFO)
3197 fifo_printinfo(vp);
3198 printf("\n");
3199 return (0);
3200 }
3201
3202 /*
3203 * This is the "real" nfs::bwrite(struct buf*).
3204 * We set B_CACHE if this is a VMIO buffer.
3205 */
3206 int
3207 nfs_writebp(struct buf *bp, int force __unused, struct thread *td)
3208 {
3209 int s;
3210 int oldflags = bp->b_flags;
3211 #if 0
3212 int retv = 1;
3213 off_t off;
3214 #endif
3215
3216 if (BUF_REFCNT(bp) == 0)
3217 panic("bwrite: buffer is not locked???");
3218
3219 if (bp->b_flags & B_INVAL) {
3220 brelse(bp);
3221 return(0);
3222 }
3223
3224 bp->b_flags |= B_CACHE;
3225
3226 /*
3227 * Undirty the bp. We will redirty it later if the I/O fails.
3228 */
3229
3230 s = splbio();
3231 bundirty(bp);
3232 bp->b_flags &= ~B_DONE;
3233 bp->b_ioflags &= ~BIO_ERROR;
3234 bp->b_iocmd = BIO_WRITE;
3235
3236 bufobj_wref(bp->b_bufobj);
3237 curthread->td_ru.ru_oublock++;
3238 splx(s);
3239
3240 /*
3241 * Note: to avoid loopback deadlocks, we do not
3242 * assign b_runningbufspace.
3243 */
3244 vfs_busy_pages(bp, 1);
3245
3246 BUF_KERNPROC(bp);
3247 bp->b_iooffset = dbtob(bp->b_blkno);
3248 bstrategy(bp);
3249
3250 if( (oldflags & B_ASYNC) == 0) {
3251 int rtval = bufwait(bp);
3252
3253 if (oldflags & B_DELWRI) {
3254 s = splbio();
3255 reassignbuf(bp);
3256 splx(s);
3257 }
3258 brelse(bp);
3259 return (rtval);
3260 }
3261
3262 return (0);
3263 }
3264
3265 /*
3266 * nfs special file access vnode op.
3267 * Essentially just get vattr and then imitate iaccess() since the device is
3268 * local to the client.
3269 */
3270 static int
3271 nfsspec_access(struct vop_access_args *ap)
3272 {
3273 struct vattr *vap;
3274 struct ucred *cred = ap->a_cred;
3275 struct vnode *vp = ap->a_vp;
3276 mode_t mode = ap->a_mode;
3277 struct vattr vattr;
3278 int error;
3279
3280 /*
3281 * Disallow write attempts on filesystems mounted read-only;
3282 * unless the file is a socket, fifo, or a block or character
3283 * device resident on the filesystem.
3284 */
3285 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3286 switch (vp->v_type) {
3287 case VREG:
3288 case VDIR:
3289 case VLNK:
3290 return (EROFS);
3291 default:
3292 break;
3293 }
3294 }
3295 vap = &vattr;
3296 error = VOP_GETATTR(vp, vap, cred, ap->a_td);
3297 if (error)
3298 goto out;
3299 error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3300 mode, cred, NULL);
3301 out:
3302 return error;
3303 }
3304
3305 /*
3306 * Read wrapper for fifos.
3307 */
3308 static int
3309 nfsfifo_read(struct vop_read_args *ap)
3310 {
3311 struct nfsnode *np = VTONFS(ap->a_vp);
3312 int error;
3313
3314 /*
3315 * Set access flag.
3316 */
3317 mtx_lock(&np->n_mtx);
3318 np->n_flag |= NACC;
3319 getnanotime(&np->n_atim);
3320 mtx_unlock(&np->n_mtx);
3321 error = fifo_specops.vop_read(ap);
3322 return error;
3323 }
3324
3325 /*
3326 * Write wrapper for fifos.
3327 */
3328 static int
3329 nfsfifo_write(struct vop_write_args *ap)
3330 {
3331 struct nfsnode *np = VTONFS(ap->a_vp);
3332
3333 /*
3334 * Set update flag.
3335 */
3336 mtx_lock(&np->n_mtx);
3337 np->n_flag |= NUPD;
3338 getnanotime(&np->n_mtim);
3339 mtx_unlock(&np->n_mtx);
3340 return(fifo_specops.vop_write(ap));
3341 }
3342
3343 /*
3344 * Close wrapper for fifos.
3345 *
3346 * Update the times on the nfsnode then do fifo close.
3347 */
3348 static int
3349 nfsfifo_close(struct vop_close_args *ap)
3350 {
3351 struct vnode *vp = ap->a_vp;
3352 struct nfsnode *np = VTONFS(vp);
3353 struct vattr vattr;
3354 struct timespec ts;
3355
3356 mtx_lock(&np->n_mtx);
3357 if (np->n_flag & (NACC | NUPD)) {
3358 getnanotime(&ts);
3359 if (np->n_flag & NACC)
3360 np->n_atim = ts;
3361 if (np->n_flag & NUPD)
3362 np->n_mtim = ts;
3363 np->n_flag |= NCHG;
3364 if (vrefcnt(vp) == 1 &&
3365 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3366 VATTR_NULL(&vattr);
3367 if (np->n_flag & NACC)
3368 vattr.va_atime = np->n_atim;
3369 if (np->n_flag & NUPD)
3370 vattr.va_mtime = np->n_mtim;
3371 mtx_unlock(&np->n_mtx);
3372 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_td);
3373 goto out;
3374 }
3375 }
3376 mtx_unlock(&np->n_mtx);
3377 out:
3378 return (fifo_specops.vop_close(ap));
3379 }
3380
3381 /*
3382 * Just call nfs_writebp() with the force argument set to 1.
3383 *
3384 * NOTE: B_DONE may or may not be set in a_bp on call.
3385 */
3386 static int
3387 nfs_bwrite(struct buf *bp)
3388 {
3389
3390 return (nfs_writebp(bp, 1, curthread));
3391 }
3392
3393 struct buf_ops buf_ops_nfs = {
3394 .bop_name = "buf_ops_nfs",
3395 .bop_write = nfs_bwrite,
3396 .bop_strategy = bufstrategy,
3397 .bop_sync = bufsync,
3398 .bop_bdflush = bufbdflush,
3399 };
Cache object: 0afcc9e1a3007436418ebf1ae85b6f6c
|