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