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