nfs_vnops.c revision 1.271 1 /* $NetBSD: nfs_vnops.c,v 1.271 2009/03/14 14:46:11 dsl Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)nfs_vnops.c 8.19 (Berkeley) 7/31/95
35 */
36
37 /*
38 * vnode op calls for Sun NFS version 2 and 3
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: nfs_vnops.c,v 1.271 2009/03/14 14:46:11 dsl Exp $");
43
44 #ifdef _KERNEL_OPT
45 #include "opt_nfs.h"
46 #include "opt_uvmhist.h"
47 #endif
48
49 #include <sys/param.h>
50 #include <sys/proc.h>
51 #include <sys/kernel.h>
52 #include <sys/systm.h>
53 #include <sys/resourcevar.h>
54 #include <sys/mount.h>
55 #include <sys/buf.h>
56 #include <sys/condvar.h>
57 #include <sys/disk.h>
58 #include <sys/malloc.h>
59 #include <sys/kmem.h>
60 #include <sys/mbuf.h>
61 #include <sys/mutex.h>
62 #include <sys/namei.h>
63 #include <sys/vnode.h>
64 #include <sys/dirent.h>
65 #include <sys/fcntl.h>
66 #include <sys/hash.h>
67 #include <sys/lockf.h>
68 #include <sys/stat.h>
69 #include <sys/unistd.h>
70 #include <sys/kauth.h>
71
72 #include <uvm/uvm_extern.h>
73 #include <uvm/uvm.h>
74
75 #include <miscfs/fifofs/fifo.h>
76 #include <miscfs/genfs/genfs.h>
77 #include <miscfs/genfs/genfs_node.h>
78 #include <miscfs/specfs/specdev.h>
79
80 #include <nfs/rpcv2.h>
81 #include <nfs/nfsproto.h>
82 #include <nfs/nfs.h>
83 #include <nfs/nfsnode.h>
84 #include <nfs/nfsmount.h>
85 #include <nfs/xdr_subs.h>
86 #include <nfs/nfsm_subs.h>
87 #include <nfs/nfs_var.h>
88
89 #include <net/if.h>
90 #include <netinet/in.h>
91 #include <netinet/in_var.h>
92
93 /*
94 * Global vfs data structures for nfs
95 */
96 int (**nfsv2_vnodeop_p)(void *);
97 const struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
98 { &vop_default_desc, vn_default_error },
99 { &vop_lookup_desc, nfs_lookup }, /* lookup */
100 { &vop_create_desc, nfs_create }, /* create */
101 { &vop_mknod_desc, nfs_mknod }, /* mknod */
102 { &vop_open_desc, nfs_open }, /* open */
103 { &vop_close_desc, nfs_close }, /* close */
104 { &vop_access_desc, nfs_access }, /* access */
105 { &vop_getattr_desc, nfs_getattr }, /* getattr */
106 { &vop_setattr_desc, nfs_setattr }, /* setattr */
107 { &vop_read_desc, nfs_read }, /* read */
108 { &vop_write_desc, nfs_write }, /* write */
109 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
110 { &vop_ioctl_desc, nfs_ioctl }, /* ioctl */
111 { &vop_poll_desc, nfs_poll }, /* poll */
112 { &vop_kqfilter_desc, nfs_kqfilter }, /* kqfilter */
113 { &vop_revoke_desc, nfs_revoke }, /* revoke */
114 { &vop_mmap_desc, nfs_mmap }, /* mmap */
115 { &vop_fsync_desc, nfs_fsync }, /* fsync */
116 { &vop_seek_desc, nfs_seek }, /* seek */
117 { &vop_remove_desc, nfs_remove }, /* remove */
118 { &vop_link_desc, nfs_link }, /* link */
119 { &vop_rename_desc, nfs_rename }, /* rename */
120 { &vop_mkdir_desc, nfs_mkdir }, /* mkdir */
121 { &vop_rmdir_desc, nfs_rmdir }, /* rmdir */
122 { &vop_symlink_desc, nfs_symlink }, /* symlink */
123 { &vop_readdir_desc, nfs_readdir }, /* readdir */
124 { &vop_readlink_desc, nfs_readlink }, /* readlink */
125 { &vop_abortop_desc, nfs_abortop }, /* abortop */
126 { &vop_inactive_desc, nfs_inactive }, /* inactive */
127 { &vop_reclaim_desc, nfs_reclaim }, /* reclaim */
128 { &vop_lock_desc, nfs_lock }, /* lock */
129 { &vop_unlock_desc, nfs_unlock }, /* unlock */
130 { &vop_bmap_desc, nfs_bmap }, /* bmap */
131 { &vop_strategy_desc, nfs_strategy }, /* strategy */
132 { &vop_print_desc, nfs_print }, /* print */
133 { &vop_islocked_desc, nfs_islocked }, /* islocked */
134 { &vop_pathconf_desc, nfs_pathconf }, /* pathconf */
135 { &vop_advlock_desc, nfs_advlock }, /* advlock */
136 { &vop_bwrite_desc, genfs_badop }, /* bwrite */
137 { &vop_getpages_desc, nfs_getpages }, /* getpages */
138 { &vop_putpages_desc, genfs_putpages }, /* putpages */
139 { NULL, NULL }
140 };
141 const struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
142 { &nfsv2_vnodeop_p, nfsv2_vnodeop_entries };
143
144 /*
145 * Special device vnode ops
146 */
147 int (**spec_nfsv2nodeop_p)(void *);
148 const struct vnodeopv_entry_desc spec_nfsv2nodeop_entries[] = {
149 { &vop_default_desc, vn_default_error },
150 { &vop_lookup_desc, spec_lookup }, /* lookup */
151 { &vop_create_desc, spec_create }, /* create */
152 { &vop_mknod_desc, spec_mknod }, /* mknod */
153 { &vop_open_desc, spec_open }, /* open */
154 { &vop_close_desc, nfsspec_close }, /* close */
155 { &vop_access_desc, nfsspec_access }, /* access */
156 { &vop_getattr_desc, nfs_getattr }, /* getattr */
157 { &vop_setattr_desc, nfs_setattr }, /* setattr */
158 { &vop_read_desc, nfsspec_read }, /* read */
159 { &vop_write_desc, nfsspec_write }, /* write */
160 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
161 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
162 { &vop_poll_desc, spec_poll }, /* poll */
163 { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
164 { &vop_revoke_desc, spec_revoke }, /* revoke */
165 { &vop_mmap_desc, spec_mmap }, /* mmap */
166 { &vop_fsync_desc, spec_fsync }, /* fsync */
167 { &vop_seek_desc, spec_seek }, /* seek */
168 { &vop_remove_desc, spec_remove }, /* remove */
169 { &vop_link_desc, spec_link }, /* link */
170 { &vop_rename_desc, spec_rename }, /* rename */
171 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
172 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
173 { &vop_symlink_desc, spec_symlink }, /* symlink */
174 { &vop_readdir_desc, spec_readdir }, /* readdir */
175 { &vop_readlink_desc, spec_readlink }, /* readlink */
176 { &vop_abortop_desc, spec_abortop }, /* abortop */
177 { &vop_inactive_desc, nfs_inactive }, /* inactive */
178 { &vop_reclaim_desc, nfs_reclaim }, /* reclaim */
179 { &vop_lock_desc, nfs_lock }, /* lock */
180 { &vop_unlock_desc, nfs_unlock }, /* unlock */
181 { &vop_bmap_desc, spec_bmap }, /* bmap */
182 { &vop_strategy_desc, spec_strategy }, /* strategy */
183 { &vop_print_desc, nfs_print }, /* print */
184 { &vop_islocked_desc, nfs_islocked }, /* islocked */
185 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
186 { &vop_advlock_desc, spec_advlock }, /* advlock */
187 { &vop_bwrite_desc, spec_bwrite }, /* bwrite */
188 { &vop_getpages_desc, spec_getpages }, /* getpages */
189 { &vop_putpages_desc, spec_putpages }, /* putpages */
190 { NULL, NULL }
191 };
192 const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
193 { &spec_nfsv2nodeop_p, spec_nfsv2nodeop_entries };
194
195 int (**fifo_nfsv2nodeop_p)(void *);
196 const struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries[] = {
197 { &vop_default_desc, vn_default_error },
198 { &vop_lookup_desc, fifo_lookup }, /* lookup */
199 { &vop_create_desc, fifo_create }, /* create */
200 { &vop_mknod_desc, fifo_mknod }, /* mknod */
201 { &vop_open_desc, fifo_open }, /* open */
202 { &vop_close_desc, nfsfifo_close }, /* close */
203 { &vop_access_desc, nfsspec_access }, /* access */
204 { &vop_getattr_desc, nfs_getattr }, /* getattr */
205 { &vop_setattr_desc, nfs_setattr }, /* setattr */
206 { &vop_read_desc, nfsfifo_read }, /* read */
207 { &vop_write_desc, nfsfifo_write }, /* write */
208 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
209 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
210 { &vop_poll_desc, fifo_poll }, /* poll */
211 { &vop_kqfilter_desc, fifo_kqfilter }, /* kqfilter */
212 { &vop_revoke_desc, fifo_revoke }, /* revoke */
213 { &vop_mmap_desc, fifo_mmap }, /* mmap */
214 { &vop_fsync_desc, nfs_fsync }, /* fsync */
215 { &vop_seek_desc, fifo_seek }, /* seek */
216 { &vop_remove_desc, fifo_remove }, /* remove */
217 { &vop_link_desc, fifo_link }, /* link */
218 { &vop_rename_desc, fifo_rename }, /* rename */
219 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
220 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
221 { &vop_symlink_desc, fifo_symlink }, /* symlink */
222 { &vop_readdir_desc, fifo_readdir }, /* readdir */
223 { &vop_readlink_desc, fifo_readlink }, /* readlink */
224 { &vop_abortop_desc, fifo_abortop }, /* abortop */
225 { &vop_inactive_desc, nfs_inactive }, /* inactive */
226 { &vop_reclaim_desc, nfs_reclaim }, /* reclaim */
227 { &vop_lock_desc, nfs_lock }, /* lock */
228 { &vop_unlock_desc, nfs_unlock }, /* unlock */
229 { &vop_bmap_desc, fifo_bmap }, /* bmap */
230 { &vop_strategy_desc, genfs_badop }, /* strategy */
231 { &vop_print_desc, nfs_print }, /* print */
232 { &vop_islocked_desc, nfs_islocked }, /* islocked */
233 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
234 { &vop_advlock_desc, fifo_advlock }, /* advlock */
235 { &vop_bwrite_desc, genfs_badop }, /* bwrite */
236 { &vop_putpages_desc, fifo_putpages }, /* putpages */
237 { NULL, NULL }
238 };
239 const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
240 { &fifo_nfsv2nodeop_p, fifo_nfsv2nodeop_entries };
241
242 static int nfs_linkrpc(struct vnode *, struct vnode *, const char *,
243 size_t, kauth_cred_t, struct lwp *);
244 static void nfs_writerpc_extfree(struct mbuf *, void *, size_t, void *);
245
246 /*
247 * Global variables
248 */
249 extern u_int32_t nfs_true, nfs_false;
250 extern u_int32_t nfs_xdrneg1;
251 extern const nfstype nfsv3_type[9];
252
253 int nfs_numasync = 0;
254 #define DIRHDSIZ _DIRENT_NAMEOFF(dp)
255 #define UIO_ADVANCE(uio, siz) \
256 (void)((uio)->uio_resid -= (siz), \
257 (uio)->uio_iov->iov_base = (char *)(uio)->uio_iov->iov_base + (siz), \
258 (uio)->uio_iov->iov_len -= (siz))
259
260 static void nfs_cache_enter(struct vnode *, struct vnode *,
261 struct componentname *);
262
263 static void
264 nfs_cache_enter(struct vnode *dvp, struct vnode *vp,
265 struct componentname *cnp)
266 {
267 struct nfsnode *dnp = VTONFS(dvp);
268
269 if (vp != NULL) {
270 struct nfsnode *np = VTONFS(vp);
271
272 np->n_ctime = np->n_vattr->va_ctime.tv_sec;
273 }
274
275 if (!timespecisset(&dnp->n_nctime))
276 dnp->n_nctime = dnp->n_vattr->va_mtime;
277
278 cache_enter(dvp, vp, cnp);
279 }
280
281 /*
282 * nfs null call from vfs.
283 */
284 int
285 nfs_null(vp, cred, l)
286 struct vnode *vp;
287 kauth_cred_t cred;
288 struct lwp *l;
289 {
290 char *bpos, *dpos;
291 int error = 0;
292 struct mbuf *mreq, *mrep, *md, *mb;
293 struct nfsnode *np = VTONFS(vp);
294
295 nfsm_reqhead(np, NFSPROC_NULL, 0);
296 nfsm_request(np, NFSPROC_NULL, l, cred);
297 nfsm_reqdone;
298 return (error);
299 }
300
301 /*
302 * nfs access vnode op.
303 * For nfs version 2, just return ok. File accesses may fail later.
304 * For nfs version 3, use the access rpc to check accessibility. If file modes
305 * are changed on the server, accesses might still fail later.
306 */
307 int
308 nfs_access(v)
309 void *v;
310 {
311 struct vop_access_args /* {
312 struct vnode *a_vp;
313 int a_mode;
314 kauth_cred_t a_cred;
315 } */ *ap = v;
316 struct vnode *vp = ap->a_vp;
317 #ifndef NFS_V2_ONLY
318 u_int32_t *tl;
319 char *cp;
320 int32_t t1, t2;
321 char *bpos, *dpos, *cp2;
322 int error = 0, attrflag;
323 struct mbuf *mreq, *mrep, *md, *mb;
324 u_int32_t mode, rmode;
325 const int v3 = NFS_ISV3(vp);
326 #endif
327 int cachevalid;
328 struct nfsnode *np = VTONFS(vp);
329 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
330
331 cachevalid = (np->n_accstamp != -1 &&
332 (time_uptime - np->n_accstamp) < nfs_attrtimeo(nmp, np) &&
333 np->n_accuid == kauth_cred_geteuid(ap->a_cred));
334
335 /*
336 * Check access cache first. If this request has been made for this
337 * uid shortly before, use the cached result.
338 */
339 if (cachevalid) {
340 if (!np->n_accerror) {
341 if ((np->n_accmode & ap->a_mode) == ap->a_mode)
342 return np->n_accerror;
343 } else if ((np->n_accmode & ap->a_mode) == np->n_accmode)
344 return np->n_accerror;
345 }
346
347 #ifndef NFS_V2_ONLY
348 /*
349 * For nfs v3, do an access rpc, otherwise you are stuck emulating
350 * ufs_access() locally using the vattr. This may not be correct,
351 * since the server may apply other access criteria such as
352 * client uid-->server uid mapping that we do not know about, but
353 * this is better than just returning anything that is lying about
354 * in the cache.
355 */
356 if (v3) {
357 nfsstats.rpccnt[NFSPROC_ACCESS]++;
358 nfsm_reqhead(np, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
359 nfsm_fhtom(np, v3);
360 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
361 if (ap->a_mode & VREAD)
362 mode = NFSV3ACCESS_READ;
363 else
364 mode = 0;
365 if (vp->v_type != VDIR) {
366 if (ap->a_mode & VWRITE)
367 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
368 if (ap->a_mode & VEXEC)
369 mode |= NFSV3ACCESS_EXECUTE;
370 } else {
371 if (ap->a_mode & VWRITE)
372 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
373 NFSV3ACCESS_DELETE);
374 if (ap->a_mode & VEXEC)
375 mode |= NFSV3ACCESS_LOOKUP;
376 }
377 *tl = txdr_unsigned(mode);
378 nfsm_request(np, NFSPROC_ACCESS, curlwp, ap->a_cred);
379 nfsm_postop_attr(vp, attrflag, 0);
380 if (!error) {
381 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
382 rmode = fxdr_unsigned(u_int32_t, *tl);
383 /*
384 * The NFS V3 spec does not clarify whether or not
385 * the returned access bits can be a superset of
386 * the ones requested, so...
387 */
388 if ((rmode & mode) != mode)
389 error = EACCES;
390 }
391 nfsm_reqdone;
392 } else
393 #endif
394 return (nfsspec_access(ap));
395 #ifndef NFS_V2_ONLY
396 /*
397 * Disallow write attempts on filesystems mounted read-only;
398 * unless the file is a socket, fifo, or a block or character
399 * device resident on the filesystem.
400 */
401 if (!error && (ap->a_mode & VWRITE) &&
402 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
403 switch (vp->v_type) {
404 case VREG:
405 case VDIR:
406 case VLNK:
407 error = EROFS;
408 default:
409 break;
410 }
411 }
412
413 if (!error || error == EACCES) {
414 /*
415 * If we got the same result as for a previous,
416 * different request, OR it in. Don't update
417 * the timestamp in that case.
418 */
419 if (cachevalid && np->n_accstamp != -1 &&
420 error == np->n_accerror) {
421 if (!error)
422 np->n_accmode |= ap->a_mode;
423 else if ((np->n_accmode & ap->a_mode) == ap->a_mode)
424 np->n_accmode = ap->a_mode;
425 } else {
426 np->n_accstamp = time_uptime;
427 np->n_accuid = kauth_cred_geteuid(ap->a_cred);
428 np->n_accmode = ap->a_mode;
429 np->n_accerror = error;
430 }
431 }
432
433 return (error);
434 #endif
435 }
436
437 /*
438 * nfs open vnode op
439 * Check to see if the type is ok
440 * and that deletion is not in progress.
441 * For paged in text files, you will need to flush the page cache
442 * if consistency is lost.
443 */
444 /* ARGSUSED */
445 int
446 nfs_open(v)
447 void *v;
448 {
449 struct vop_open_args /* {
450 struct vnode *a_vp;
451 int a_mode;
452 kauth_cred_t a_cred;
453 } */ *ap = v;
454 struct vnode *vp = ap->a_vp;
455 struct nfsnode *np = VTONFS(vp);
456 int error;
457
458 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
459 return (EACCES);
460 }
461
462 if (ap->a_mode & FREAD) {
463 if (np->n_rcred != NULL)
464 kauth_cred_free(np->n_rcred);
465 np->n_rcred = ap->a_cred;
466 kauth_cred_hold(np->n_rcred);
467 }
468 if (ap->a_mode & FWRITE) {
469 if (np->n_wcred != NULL)
470 kauth_cred_free(np->n_wcred);
471 np->n_wcred = ap->a_cred;
472 kauth_cred_hold(np->n_wcred);
473 }
474
475 error = nfs_flushstalebuf(vp, ap->a_cred, curlwp, 0);
476 if (error)
477 return error;
478
479 NFS_INVALIDATE_ATTRCACHE(np); /* For Open/Close consistency */
480
481 return (0);
482 }
483
484 /*
485 * nfs close vnode op
486 * What an NFS client should do upon close after writing is a debatable issue.
487 * Most NFS clients push delayed writes to the server upon close, basically for
488 * two reasons:
489 * 1 - So that any write errors may be reported back to the client process
490 * doing the close system call. By far the two most likely errors are
491 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
492 * 2 - To put a worst case upper bound on cache inconsistency between
493 * multiple clients for the file.
494 * There is also a consistency problem for Version 2 of the protocol w.r.t.
495 * not being able to tell if other clients are writing a file concurrently,
496 * since there is no way of knowing if the changed modify time in the reply
497 * is only due to the write for this client.
498 * (NFS Version 3 provides weak cache consistency data in the reply that
499 * should be sufficient to detect and handle this case.)
500 *
501 * The current code does the following:
502 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
503 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
504 * or commit them (this satisfies 1 and 2 except for the
505 * case where the server crashes after this close but
506 * before the commit RPC, which is felt to be "good
507 * enough". Changing the last argument to nfs_flush() to
508 * a 1 would force a commit operation, if it is felt a
509 * commit is necessary now.
510 */
511 /* ARGSUSED */
512 int
513 nfs_close(v)
514 void *v;
515 {
516 struct vop_close_args /* {
517 struct vnodeop_desc *a_desc;
518 struct vnode *a_vp;
519 int a_fflag;
520 kauth_cred_t a_cred;
521 } */ *ap = v;
522 struct vnode *vp = ap->a_vp;
523 struct nfsnode *np = VTONFS(vp);
524 int error = 0;
525 UVMHIST_FUNC("nfs_close"); UVMHIST_CALLED(ubchist);
526
527 if (vp->v_type == VREG) {
528 if (np->n_flag & NMODIFIED) {
529 #ifndef NFS_V2_ONLY
530 if (NFS_ISV3(vp)) {
531 error = nfs_flush(vp, ap->a_cred, MNT_WAIT, curlwp, 0);
532 np->n_flag &= ~NMODIFIED;
533 } else
534 #endif
535 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, curlwp, 1);
536 NFS_INVALIDATE_ATTRCACHE(np);
537 }
538 if (np->n_flag & NWRITEERR) {
539 np->n_flag &= ~NWRITEERR;
540 error = np->n_error;
541 }
542 }
543 UVMHIST_LOG(ubchist, "returning %d", error,0,0,0);
544 return (error);
545 }
546
547 /*
548 * nfs getattr call from vfs.
549 */
550 int
551 nfs_getattr(v)
552 void *v;
553 {
554 struct vop_getattr_args /* {
555 struct vnode *a_vp;
556 struct vattr *a_vap;
557 kauth_cred_t a_cred;
558 } */ *ap = v;
559 struct vnode *vp = ap->a_vp;
560 struct nfsnode *np = VTONFS(vp);
561 char *cp;
562 u_int32_t *tl;
563 int32_t t1, t2;
564 char *bpos, *dpos;
565 int error = 0;
566 struct mbuf *mreq, *mrep, *md, *mb;
567 const int v3 = NFS_ISV3(vp);
568
569 /*
570 * Update local times for special files.
571 */
572 if (np->n_flag & (NACC | NUPD))
573 np->n_flag |= NCHG;
574
575 /*
576 * if we have delayed truncation, do it now.
577 */
578 nfs_delayedtruncate(vp);
579
580 /*
581 * First look in the cache.
582 */
583 if (nfs_getattrcache(vp, ap->a_vap) == 0)
584 return (0);
585 nfsstats.rpccnt[NFSPROC_GETATTR]++;
586 nfsm_reqhead(np, NFSPROC_GETATTR, NFSX_FH(v3));
587 nfsm_fhtom(np, v3);
588 nfsm_request(np, NFSPROC_GETATTR, curlwp, ap->a_cred);
589 if (!error) {
590 nfsm_loadattr(vp, ap->a_vap, 0);
591 if (vp->v_type == VDIR &&
592 ap->a_vap->va_blocksize < NFS_DIRFRAGSIZ)
593 ap->a_vap->va_blocksize = NFS_DIRFRAGSIZ;
594 }
595 nfsm_reqdone;
596 return (error);
597 }
598
599 /*
600 * nfs setattr call.
601 */
602 int
603 nfs_setattr(v)
604 void *v;
605 {
606 struct vop_setattr_args /* {
607 struct vnodeop_desc *a_desc;
608 struct vnode *a_vp;
609 struct vattr *a_vap;
610 kauth_cred_t a_cred;
611 } */ *ap = v;
612 struct vnode *vp = ap->a_vp;
613 struct nfsnode *np = VTONFS(vp);
614 struct vattr *vap = ap->a_vap;
615 int error = 0;
616 u_quad_t tsize = 0;
617
618 /*
619 * Setting of flags is not supported.
620 */
621 if (vap->va_flags != VNOVAL)
622 return (EOPNOTSUPP);
623
624 /*
625 * Disallow write attempts if the filesystem is mounted read-only.
626 */
627 if ((vap->va_uid != (uid_t)VNOVAL ||
628 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
629 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
630 (vp->v_mount->mnt_flag & MNT_RDONLY))
631 return (EROFS);
632 if (vap->va_size != VNOVAL) {
633 if (vap->va_size > VFSTONFS(vp->v_mount)->nm_maxfilesize) {
634 return EFBIG;
635 }
636 switch (vp->v_type) {
637 case VDIR:
638 return (EISDIR);
639 case VCHR:
640 case VBLK:
641 case VSOCK:
642 case VFIFO:
643 if (vap->va_mtime.tv_sec == VNOVAL &&
644 vap->va_atime.tv_sec == VNOVAL &&
645 vap->va_mode == (mode_t)VNOVAL &&
646 vap->va_uid == (uid_t)VNOVAL &&
647 vap->va_gid == (gid_t)VNOVAL)
648 return (0);
649 vap->va_size = VNOVAL;
650 break;
651 default:
652 /*
653 * Disallow write attempts if the filesystem is
654 * mounted read-only.
655 */
656 if (vp->v_mount->mnt_flag & MNT_RDONLY)
657 return (EROFS);
658 genfs_node_wrlock(vp);
659 uvm_vnp_setsize(vp, vap->va_size);
660 tsize = np->n_size;
661 np->n_size = vap->va_size;
662 if (vap->va_size == 0)
663 error = nfs_vinvalbuf(vp, 0,
664 ap->a_cred, curlwp, 1);
665 else
666 error = nfs_vinvalbuf(vp, V_SAVE,
667 ap->a_cred, curlwp, 1);
668 if (error) {
669 uvm_vnp_setsize(vp, tsize);
670 genfs_node_unlock(vp);
671 return (error);
672 }
673 np->n_vattr->va_size = vap->va_size;
674 }
675 } else {
676 /*
677 * flush files before setattr because a later write of
678 * cached data might change timestamps or reset sugid bits
679 */
680 if ((vap->va_mtime.tv_sec != VNOVAL ||
681 vap->va_atime.tv_sec != VNOVAL ||
682 vap->va_mode != VNOVAL) &&
683 vp->v_type == VREG &&
684 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
685 curlwp, 1)) == EINTR)
686 return (error);
687 }
688 error = nfs_setattrrpc(vp, vap, ap->a_cred, curlwp);
689 if (vap->va_size != VNOVAL) {
690 if (error) {
691 np->n_size = np->n_vattr->va_size = tsize;
692 uvm_vnp_setsize(vp, np->n_size);
693 }
694 genfs_node_unlock(vp);
695 }
696 VN_KNOTE(vp, NOTE_ATTRIB);
697 return (error);
698 }
699
700 /*
701 * Do an nfs setattr rpc.
702 */
703 int
704 nfs_setattrrpc(vp, vap, cred, l)
705 struct vnode *vp;
706 struct vattr *vap;
707 kauth_cred_t cred;
708 struct lwp *l;
709 {
710 struct nfsv2_sattr *sp;
711 char *cp;
712 int32_t t1, t2;
713 char *bpos, *dpos;
714 u_int32_t *tl;
715 int error = 0;
716 struct mbuf *mreq, *mrep, *md, *mb;
717 const int v3 = NFS_ISV3(vp);
718 struct nfsnode *np = VTONFS(vp);
719 #ifndef NFS_V2_ONLY
720 int wccflag = NFSV3_WCCRATTR;
721 char *cp2;
722 #endif
723
724 nfsstats.rpccnt[NFSPROC_SETATTR]++;
725 nfsm_reqhead(np, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
726 nfsm_fhtom(np, v3);
727 #ifndef NFS_V2_ONLY
728 if (v3) {
729 nfsm_v3attrbuild(vap, true);
730 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
731 *tl = nfs_false;
732 } else {
733 #endif
734 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
735 if (vap->va_mode == (mode_t)VNOVAL)
736 sp->sa_mode = nfs_xdrneg1;
737 else
738 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
739 if (vap->va_uid == (uid_t)VNOVAL)
740 sp->sa_uid = nfs_xdrneg1;
741 else
742 sp->sa_uid = txdr_unsigned(vap->va_uid);
743 if (vap->va_gid == (gid_t)VNOVAL)
744 sp->sa_gid = nfs_xdrneg1;
745 else
746 sp->sa_gid = txdr_unsigned(vap->va_gid);
747 sp->sa_size = txdr_unsigned(vap->va_size);
748 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
749 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
750 #ifndef NFS_V2_ONLY
751 }
752 #endif
753 nfsm_request(np, NFSPROC_SETATTR, l, cred);
754 #ifndef NFS_V2_ONLY
755 if (v3) {
756 nfsm_wcc_data(vp, wccflag, NAC_NOTRUNC, false);
757 } else
758 #endif
759 nfsm_loadattr(vp, (struct vattr *)0, NAC_NOTRUNC);
760 nfsm_reqdone;
761 return (error);
762 }
763
764 /*
765 * nfs lookup call, one step at a time...
766 * First look in cache
767 * If not found, unlock the directory nfsnode and do the rpc
768 *
769 * This code is full of lock/unlock statements and checks, because
770 * we continue after cache_lookup has finished (we need to check
771 * with the attr cache and do an rpc if it has timed out). This means
772 * that the locking effects of cache_lookup have to be taken into
773 * account.
774 */
775 int
776 nfs_lookup(v)
777 void *v;
778 {
779 struct vop_lookup_args /* {
780 struct vnodeop_desc *a_desc;
781 struct vnode *a_dvp;
782 struct vnode **a_vpp;
783 struct componentname *a_cnp;
784 } */ *ap = v;
785 struct componentname *cnp = ap->a_cnp;
786 struct vnode *dvp = ap->a_dvp;
787 struct vnode **vpp = ap->a_vpp;
788 int flags;
789 struct vnode *newvp;
790 u_int32_t *tl;
791 char *cp;
792 int32_t t1, t2;
793 char *bpos, *dpos, *cp2;
794 struct mbuf *mreq, *mrep, *md, *mb;
795 long len;
796 nfsfh_t *fhp;
797 struct nfsnode *np;
798 int error = 0, attrflag, fhsize;
799 const int v3 = NFS_ISV3(dvp);
800
801 flags = cnp->cn_flags;
802
803 *vpp = NULLVP;
804 newvp = NULLVP;
805 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
806 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
807 return (EROFS);
808 if (dvp->v_type != VDIR)
809 return (ENOTDIR);
810
811 /*
812 * RFC1813(nfsv3) 3.2 says clients should handle "." by themselves.
813 */
814 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
815 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
816 if (error)
817 return error;
818 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN))
819 return EISDIR;
820 VREF(dvp);
821 *vpp = dvp;
822 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
823 cnp->cn_flags |= SAVENAME;
824 return 0;
825 }
826
827 np = VTONFS(dvp);
828
829 /*
830 * Before performing an RPC, check the name cache to see if
831 * the directory/name pair we are looking for is known already.
832 * If the directory/name pair is found in the name cache,
833 * we have to ensure the directory has not changed from
834 * the time the cache entry has been created. If it has,
835 * the cache entry has to be ignored.
836 */
837 error = cache_lookup_raw(dvp, vpp, cnp);
838 KASSERT(dvp != *vpp);
839 if (error >= 0) {
840 struct vattr vattr;
841 int err2;
842
843 if (error && error != ENOENT) {
844 *vpp = NULLVP;
845 return error;
846 }
847
848 err2 = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
849 if (err2 != 0) {
850 if (error == 0)
851 vrele(*vpp);
852 *vpp = NULLVP;
853 return err2;
854 }
855
856 if (VOP_GETATTR(dvp, &vattr, cnp->cn_cred)
857 || timespeccmp(&vattr.va_mtime,
858 &VTONFS(dvp)->n_nctime, !=)) {
859 if (error == 0) {
860 vrele(*vpp);
861 *vpp = NULLVP;
862 }
863 cache_purge1(dvp, NULL, PURGE_CHILDREN);
864 timespecclear(&np->n_nctime);
865 goto dorpc;
866 }
867
868 if (error == ENOENT) {
869 goto noentry;
870 }
871
872 newvp = *vpp;
873 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred)
874 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
875 nfsstats.lookupcache_hits++;
876 if ((flags & ISDOTDOT) != 0) {
877 VOP_UNLOCK(dvp, 0);
878 }
879 error = vn_lock(newvp, LK_EXCLUSIVE);
880 if ((flags & ISDOTDOT) != 0) {
881 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
882 }
883 if (error) {
884 /* newvp has been revoked. */
885 vrele(newvp);
886 *vpp = NULL;
887 return error;
888 }
889 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
890 cnp->cn_flags |= SAVENAME;
891 KASSERT(newvp->v_type != VNON);
892 return (0);
893 }
894 cache_purge1(newvp, NULL, PURGE_PARENTS);
895 vrele(newvp);
896 *vpp = NULLVP;
897 }
898 dorpc:
899 #if 0
900 /*
901 * because nfsv3 has the same CREATE semantics as ours,
902 * we don't have to perform LOOKUPs beforehand.
903 *
904 * XXX ideally we can do the same for nfsv2 in the case of !O_EXCL.
905 * XXX although we have no way to know if O_EXCL is requested or not.
906 */
907
908 if (v3 && cnp->cn_nameiop == CREATE &&
909 (flags & (ISLASTCN|ISDOTDOT)) == ISLASTCN &&
910 (dvp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
911 cnp->cn_flags |= SAVENAME;
912 return (EJUSTRETURN);
913 }
914 #endif /* 0 */
915
916 error = 0;
917 newvp = NULLVP;
918 nfsstats.lookupcache_misses++;
919 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
920 len = cnp->cn_namelen;
921 nfsm_reqhead(np, NFSPROC_LOOKUP,
922 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
923 nfsm_fhtom(np, v3);
924 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
925 nfsm_request(np, NFSPROC_LOOKUP, curlwp, cnp->cn_cred);
926 if (error) {
927 nfsm_postop_attr(dvp, attrflag, 0);
928 m_freem(mrep);
929 goto nfsmout;
930 }
931 nfsm_getfh(fhp, fhsize, v3);
932
933 /*
934 * Handle RENAME case...
935 */
936 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
937 if (NFS_CMPFH(np, fhp, fhsize)) {
938 m_freem(mrep);
939 return (EISDIR);
940 }
941 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
942 if (error) {
943 m_freem(mrep);
944 return error;
945 }
946 newvp = NFSTOV(np);
947 #ifndef NFS_V2_ONLY
948 if (v3) {
949 nfsm_postop_attr(newvp, attrflag, 0);
950 nfsm_postop_attr(dvp, attrflag, 0);
951 } else
952 #endif
953 nfsm_loadattr(newvp, (struct vattr *)0, 0);
954 *vpp = newvp;
955 m_freem(mrep);
956 cnp->cn_flags |= SAVENAME;
957 goto validate;
958 }
959
960 /*
961 * The postop attr handling is duplicated for each if case,
962 * because it should be done while dvp is locked (unlocking
963 * dvp is different for each case).
964 */
965
966 if (NFS_CMPFH(np, fhp, fhsize)) {
967 /*
968 * "." lookup
969 */
970 VREF(dvp);
971 newvp = dvp;
972 #ifndef NFS_V2_ONLY
973 if (v3) {
974 nfsm_postop_attr(newvp, attrflag, 0);
975 nfsm_postop_attr(dvp, attrflag, 0);
976 } else
977 #endif
978 nfsm_loadattr(newvp, (struct vattr *)0, 0);
979 } else if (flags & ISDOTDOT) {
980 /*
981 * ".." lookup
982 */
983 VOP_UNLOCK(dvp, 0);
984 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
985 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
986 if (error) {
987 m_freem(mrep);
988 return error;
989 }
990 newvp = NFSTOV(np);
991
992 #ifndef NFS_V2_ONLY
993 if (v3) {
994 nfsm_postop_attr(newvp, attrflag, 0);
995 nfsm_postop_attr(dvp, attrflag, 0);
996 } else
997 #endif
998 nfsm_loadattr(newvp, (struct vattr *)0, 0);
999 } else {
1000 /*
1001 * Other lookups.
1002 */
1003 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1004 if (error) {
1005 m_freem(mrep);
1006 return error;
1007 }
1008 newvp = NFSTOV(np);
1009 #ifndef NFS_V2_ONLY
1010 if (v3) {
1011 nfsm_postop_attr(newvp, attrflag, 0);
1012 nfsm_postop_attr(dvp, attrflag, 0);
1013 } else
1014 #endif
1015 nfsm_loadattr(newvp, (struct vattr *)0, 0);
1016 }
1017 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1018 cnp->cn_flags |= SAVENAME;
1019 if ((cnp->cn_flags & MAKEENTRY) &&
1020 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
1021 nfs_cache_enter(dvp, newvp, cnp);
1022 }
1023 *vpp = newvp;
1024 nfsm_reqdone;
1025 if (error) {
1026 /*
1027 * We get here only because of errors returned by
1028 * the RPC. Otherwise we'll have returned above
1029 * (the nfsm_* macros will jump to nfsm_reqdone
1030 * on error).
1031 */
1032 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) &&
1033 cnp->cn_nameiop != CREATE) {
1034 nfs_cache_enter(dvp, NULL, cnp);
1035 }
1036 if (newvp != NULLVP) {
1037 if (newvp == dvp) {
1038 vrele(newvp);
1039 } else {
1040 vput(newvp);
1041 }
1042 }
1043 noentry:
1044 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1045 (flags & ISLASTCN) && error == ENOENT) {
1046 if (dvp->v_mount->mnt_flag & MNT_RDONLY) {
1047 error = EROFS;
1048 } else {
1049 error = EJUSTRETURN;
1050 cnp->cn_flags |= SAVENAME;
1051 }
1052 }
1053 *vpp = NULL;
1054 return error;
1055 }
1056
1057 validate:
1058 /*
1059 * make sure we have valid type and size.
1060 */
1061
1062 newvp = *vpp;
1063 if (newvp->v_type == VNON) {
1064 struct vattr vattr; /* dummy */
1065
1066 KASSERT(VTONFS(newvp)->n_attrstamp == 0);
1067 error = VOP_GETATTR(newvp, &vattr, cnp->cn_cred);
1068 if (error) {
1069 vput(newvp);
1070 *vpp = NULL;
1071 }
1072 }
1073
1074 return error;
1075 }
1076
1077 /*
1078 * nfs read call.
1079 * Just call nfs_bioread() to do the work.
1080 */
1081 int
1082 nfs_read(v)
1083 void *v;
1084 {
1085 struct vop_read_args /* {
1086 struct vnode *a_vp;
1087 struct uio *a_uio;
1088 int a_ioflag;
1089 kauth_cred_t a_cred;
1090 } */ *ap = v;
1091 struct vnode *vp = ap->a_vp;
1092
1093 if (vp->v_type != VREG)
1094 return EISDIR;
1095 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred, 0));
1096 }
1097
1098 /*
1099 * nfs readlink call
1100 */
1101 int
1102 nfs_readlink(v)
1103 void *v;
1104 {
1105 struct vop_readlink_args /* {
1106 struct vnode *a_vp;
1107 struct uio *a_uio;
1108 kauth_cred_t a_cred;
1109 } */ *ap = v;
1110 struct vnode *vp = ap->a_vp;
1111 struct nfsnode *np = VTONFS(vp);
1112
1113 if (vp->v_type != VLNK)
1114 return (EPERM);
1115
1116 if (np->n_rcred != NULL) {
1117 kauth_cred_free(np->n_rcred);
1118 }
1119 np->n_rcred = ap->a_cred;
1120 kauth_cred_hold(np->n_rcred);
1121
1122 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred, 0));
1123 }
1124
1125 /*
1126 * Do a readlink rpc.
1127 * Called by nfs_doio() from below the buffer cache.
1128 */
1129 int
1130 nfs_readlinkrpc(vp, uiop, cred)
1131 struct vnode *vp;
1132 struct uio *uiop;
1133 kauth_cred_t cred;
1134 {
1135 u_int32_t *tl;
1136 char *cp;
1137 int32_t t1, t2;
1138 char *bpos, *dpos, *cp2;
1139 int error = 0;
1140 uint32_t len;
1141 struct mbuf *mreq, *mrep, *md, *mb;
1142 const int v3 = NFS_ISV3(vp);
1143 struct nfsnode *np = VTONFS(vp);
1144 #ifndef NFS_V2_ONLY
1145 int attrflag;
1146 #endif
1147
1148 nfsstats.rpccnt[NFSPROC_READLINK]++;
1149 nfsm_reqhead(np, NFSPROC_READLINK, NFSX_FH(v3));
1150 nfsm_fhtom(np, v3);
1151 nfsm_request(np, NFSPROC_READLINK, curlwp, cred);
1152 #ifndef NFS_V2_ONLY
1153 if (v3)
1154 nfsm_postop_attr(vp, attrflag, 0);
1155 #endif
1156 if (!error) {
1157 #ifndef NFS_V2_ONLY
1158 if (v3) {
1159 nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
1160 len = fxdr_unsigned(uint32_t, *tl);
1161 if (len > MAXPATHLEN) {
1162 /*
1163 * this pathname is too long for us.
1164 */
1165 m_freem(mrep);
1166 /* Solaris returns EINVAL. should we follow? */
1167 error = ENAMETOOLONG;
1168 goto nfsmout;
1169 }
1170 } else
1171 #endif
1172 {
1173 nfsm_strsiz(len, NFS_MAXPATHLEN);
1174 }
1175 nfsm_mtouio(uiop, len);
1176 }
1177 nfsm_reqdone;
1178 return (error);
1179 }
1180
1181 /*
1182 * nfs read rpc call
1183 * Ditto above
1184 */
1185 int
1186 nfs_readrpc(vp, uiop)
1187 struct vnode *vp;
1188 struct uio *uiop;
1189 {
1190 u_int32_t *tl;
1191 char *cp;
1192 int32_t t1, t2;
1193 char *bpos, *dpos, *cp2;
1194 struct mbuf *mreq, *mrep, *md, *mb;
1195 struct nfsmount *nmp;
1196 int error = 0, len, retlen, tsiz, eof, byte_count;
1197 const int v3 = NFS_ISV3(vp);
1198 struct nfsnode *np = VTONFS(vp);
1199 #ifndef NFS_V2_ONLY
1200 int attrflag;
1201 #endif
1202
1203 #ifndef nolint
1204 eof = 0;
1205 #endif
1206 nmp = VFSTONFS(vp->v_mount);
1207 tsiz = uiop->uio_resid;
1208 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1209 return (EFBIG);
1210 iostat_busy(nmp->nm_stats);
1211 byte_count = 0; /* count bytes actually transferred */
1212 while (tsiz > 0) {
1213 nfsstats.rpccnt[NFSPROC_READ]++;
1214 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1215 nfsm_reqhead(np, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1216 nfsm_fhtom(np, v3);
1217 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1218 #ifndef NFS_V2_ONLY
1219 if (v3) {
1220 txdr_hyper(uiop->uio_offset, tl);
1221 *(tl + 2) = txdr_unsigned(len);
1222 } else
1223 #endif
1224 {
1225 *tl++ = txdr_unsigned(uiop->uio_offset);
1226 *tl++ = txdr_unsigned(len);
1227 *tl = 0;
1228 }
1229 nfsm_request(np, NFSPROC_READ, curlwp, np->n_rcred);
1230 #ifndef NFS_V2_ONLY
1231 if (v3) {
1232 nfsm_postop_attr(vp, attrflag, NAC_NOTRUNC);
1233 if (error) {
1234 m_freem(mrep);
1235 goto nfsmout;
1236 }
1237 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1238 eof = fxdr_unsigned(int, *(tl + 1));
1239 } else
1240 #endif
1241 nfsm_loadattr(vp, (struct vattr *)0, NAC_NOTRUNC);
1242 nfsm_strsiz(retlen, nmp->nm_rsize);
1243 nfsm_mtouio(uiop, retlen);
1244 m_freem(mrep);
1245 tsiz -= retlen;
1246 byte_count += retlen;
1247 #ifndef NFS_V2_ONLY
1248 if (v3) {
1249 if (eof || retlen == 0)
1250 tsiz = 0;
1251 } else
1252 #endif
1253 if (retlen < len)
1254 tsiz = 0;
1255 }
1256 nfsmout:
1257 iostat_unbusy(nmp->nm_stats, byte_count, 1);
1258 return (error);
1259 }
1260
1261 struct nfs_writerpc_context {
1262 kmutex_t nwc_lock;
1263 kcondvar_t nwc_cv;
1264 int nwc_mbufcount;
1265 };
1266
1267 /*
1268 * free mbuf used to refer protected pages while write rpc call.
1269 * called at splvm.
1270 */
1271 static void
1272 nfs_writerpc_extfree(struct mbuf *m, void *tbuf, size_t size, void *arg)
1273 {
1274 struct nfs_writerpc_context *ctx = arg;
1275
1276 KASSERT(m != NULL);
1277 KASSERT(ctx != NULL);
1278 pool_cache_put(mb_cache, m);
1279 mutex_enter(&ctx->nwc_lock);
1280 if (--ctx->nwc_mbufcount == 0) {
1281 cv_signal(&ctx->nwc_cv);
1282 }
1283 mutex_exit(&ctx->nwc_lock);
1284 }
1285
1286 /*
1287 * nfs write call
1288 */
1289 int
1290 nfs_writerpc(vp, uiop, iomode, pageprotected, stalewriteverfp)
1291 struct vnode *vp;
1292 struct uio *uiop;
1293 int *iomode;
1294 bool pageprotected;
1295 bool *stalewriteverfp;
1296 {
1297 u_int32_t *tl;
1298 char *cp;
1299 int32_t t1, t2;
1300 char *bpos, *dpos;
1301 struct mbuf *mreq, *mrep, *md, *mb;
1302 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1303 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR;
1304 const int v3 = NFS_ISV3(vp);
1305 int committed = NFSV3WRITE_FILESYNC;
1306 struct nfsnode *np = VTONFS(vp);
1307 struct nfs_writerpc_context ctx;
1308 int byte_count;
1309 struct lwp *l = NULL;
1310 size_t origresid;
1311 #ifndef NFS_V2_ONLY
1312 char *cp2;
1313 int rlen, commit;
1314 #endif
1315
1316 mutex_init(&ctx.nwc_lock, MUTEX_DRIVER, IPL_VM);
1317 cv_init(&ctx.nwc_cv, "nfsmblk");
1318 ctx.nwc_mbufcount = 1;
1319
1320 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
1321 panic("writerpc readonly vp %p", vp);
1322 }
1323
1324 #ifdef DIAGNOSTIC
1325 if (uiop->uio_iovcnt != 1)
1326 panic("nfs: writerpc iovcnt > 1");
1327 #endif
1328 tsiz = uiop->uio_resid;
1329 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1330 return (EFBIG);
1331 if (pageprotected) {
1332 l = curlwp;
1333 uvm_lwp_hold(l);
1334 }
1335 retry:
1336 origresid = uiop->uio_resid;
1337 KASSERT(origresid == uiop->uio_iov->iov_len);
1338 iostat_busy(nmp->nm_stats);
1339 byte_count = 0; /* count of bytes actually written */
1340 while (tsiz > 0) {
1341 uint32_t datalen; /* data bytes need to be allocated in mbuf */
1342 uint32_t backup;
1343 bool stalewriteverf = false;
1344
1345 nfsstats.rpccnt[NFSPROC_WRITE]++;
1346 len = min(tsiz, nmp->nm_wsize);
1347 datalen = pageprotected ? 0 : nfsm_rndup(len);
1348 nfsm_reqhead(np, NFSPROC_WRITE,
1349 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + datalen);
1350 nfsm_fhtom(np, v3);
1351 #ifndef NFS_V2_ONLY
1352 if (v3) {
1353 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1354 txdr_hyper(uiop->uio_offset, tl);
1355 tl += 2;
1356 *tl++ = txdr_unsigned(len);
1357 *tl++ = txdr_unsigned(*iomode);
1358 *tl = txdr_unsigned(len);
1359 } else
1360 #endif
1361 {
1362 u_int32_t x;
1363
1364 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1365 /* Set both "begin" and "current" to non-garbage. */
1366 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1367 *tl++ = x; /* "begin offset" */
1368 *tl++ = x; /* "current offset" */
1369 x = txdr_unsigned(len);
1370 *tl++ = x; /* total to this offset */
1371 *tl = x; /* size of this write */
1372
1373 }
1374 if (pageprotected) {
1375 /*
1376 * since we know pages can't be modified during i/o,
1377 * no need to copy them for us.
1378 */
1379 struct mbuf *m;
1380 struct iovec *iovp = uiop->uio_iov;
1381
1382 m = m_get(M_WAIT, MT_DATA);
1383 MCLAIM(m, &nfs_mowner);
1384 MEXTADD(m, iovp->iov_base, len, M_MBUF,
1385 nfs_writerpc_extfree, &ctx);
1386 m->m_flags |= M_EXT_ROMAP;
1387 m->m_len = len;
1388 mb->m_next = m;
1389 /*
1390 * no need to maintain mb and bpos here
1391 * because no one care them later.
1392 */
1393 #if 0
1394 mb = m;
1395 bpos = mtod(void *, mb) + mb->m_len;
1396 #endif
1397 UIO_ADVANCE(uiop, len);
1398 uiop->uio_offset += len;
1399 mutex_enter(&ctx.nwc_lock);
1400 ctx.nwc_mbufcount++;
1401 mutex_exit(&ctx.nwc_lock);
1402 nfs_zeropad(mb, 0, nfsm_padlen(len));
1403 } else {
1404 nfsm_uiotom(uiop, len);
1405 }
1406 nfsm_request(np, NFSPROC_WRITE, curlwp, np->n_wcred);
1407 #ifndef NFS_V2_ONLY
1408 if (v3) {
1409 wccflag = NFSV3_WCCCHK;
1410 nfsm_wcc_data(vp, wccflag, NAC_NOTRUNC, !error);
1411 if (!error) {
1412 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1413 + NFSX_V3WRITEVERF);
1414 rlen = fxdr_unsigned(int, *tl++);
1415 if (rlen == 0) {
1416 error = NFSERR_IO;
1417 m_freem(mrep);
1418 break;
1419 } else if (rlen < len) {
1420 backup = len - rlen;
1421 UIO_ADVANCE(uiop, -backup);
1422 uiop->uio_offset -= backup;
1423 len = rlen;
1424 }
1425 commit = fxdr_unsigned(int, *tl++);
1426
1427 /*
1428 * Return the lowest committment level
1429 * obtained by any of the RPCs.
1430 */
1431 if (committed == NFSV3WRITE_FILESYNC)
1432 committed = commit;
1433 else if (committed == NFSV3WRITE_DATASYNC &&
1434 commit == NFSV3WRITE_UNSTABLE)
1435 committed = commit;
1436 mutex_enter(&nmp->nm_lock);
1437 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0){
1438 memcpy(nmp->nm_writeverf, tl,
1439 NFSX_V3WRITEVERF);
1440 nmp->nm_iflag |= NFSMNT_HASWRITEVERF;
1441 } else if ((nmp->nm_iflag &
1442 NFSMNT_STALEWRITEVERF) ||
1443 memcmp(tl, nmp->nm_writeverf,
1444 NFSX_V3WRITEVERF)) {
1445 memcpy(nmp->nm_writeverf, tl,
1446 NFSX_V3WRITEVERF);
1447 /*
1448 * note NFSMNT_STALEWRITEVERF
1449 * if we're the first thread to
1450 * notice it.
1451 */
1452 if ((nmp->nm_iflag &
1453 NFSMNT_STALEWRITEVERF) == 0) {
1454 stalewriteverf = true;
1455 nmp->nm_iflag |=
1456 NFSMNT_STALEWRITEVERF;
1457 }
1458 }
1459 mutex_exit(&nmp->nm_lock);
1460 }
1461 } else
1462 #endif
1463 nfsm_loadattr(vp, (struct vattr *)0, NAC_NOTRUNC);
1464 if (wccflag)
1465 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr->va_mtime;
1466 m_freem(mrep);
1467 if (error)
1468 break;
1469 tsiz -= len;
1470 byte_count += len;
1471 if (stalewriteverf) {
1472 *stalewriteverfp = true;
1473 stalewriteverf = false;
1474 if (committed == NFSV3WRITE_UNSTABLE &&
1475 len != origresid) {
1476 /*
1477 * if our write requests weren't atomic but
1478 * unstable, datas in previous iterations
1479 * might have already been lost now.
1480 * then, we should resend them to nfsd.
1481 */
1482 backup = origresid - tsiz;
1483 UIO_ADVANCE(uiop, -backup);
1484 uiop->uio_offset -= backup;
1485 tsiz = origresid;
1486 goto retry;
1487 }
1488 }
1489 }
1490 nfsmout:
1491 iostat_unbusy(nmp->nm_stats, byte_count, 0);
1492 if (pageprotected) {
1493 /*
1494 * wait until mbufs go away.
1495 * retransmitted mbufs can survive longer than rpc requests
1496 * themselves.
1497 */
1498 mutex_enter(&ctx.nwc_lock);
1499 ctx.nwc_mbufcount--;
1500 while (ctx.nwc_mbufcount > 0) {
1501 cv_wait(&ctx.nwc_cv, &ctx.nwc_lock);
1502 }
1503 mutex_exit(&ctx.nwc_lock);
1504 uvm_lwp_rele(l);
1505 }
1506 mutex_destroy(&ctx.nwc_lock);
1507 cv_destroy(&ctx.nwc_cv);
1508 *iomode = committed;
1509 if (error)
1510 uiop->uio_resid = tsiz;
1511 return (error);
1512 }
1513
1514 /*
1515 * nfs mknod rpc
1516 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1517 * mode set to specify the file type and the size field for rdev.
1518 */
1519 int
1520 nfs_mknodrpc(dvp, vpp, cnp, vap)
1521 struct vnode *dvp;
1522 struct vnode **vpp;
1523 struct componentname *cnp;
1524 struct vattr *vap;
1525 {
1526 struct nfsv2_sattr *sp;
1527 u_int32_t *tl;
1528 char *cp;
1529 int32_t t1, t2;
1530 struct vnode *newvp = (struct vnode *)0;
1531 struct nfsnode *dnp, *np;
1532 char *cp2;
1533 char *bpos, *dpos;
1534 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1535 struct mbuf *mreq, *mrep, *md, *mb;
1536 u_int32_t rdev;
1537 const int v3 = NFS_ISV3(dvp);
1538
1539 if (vap->va_type == VCHR || vap->va_type == VBLK)
1540 rdev = txdr_unsigned(vap->va_rdev);
1541 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1542 rdev = nfs_xdrneg1;
1543 else {
1544 VOP_ABORTOP(dvp, cnp);
1545 vput(dvp);
1546 return (EOPNOTSUPP);
1547 }
1548 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1549 dnp = VTONFS(dvp);
1550 nfsm_reqhead(dnp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1551 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1552 nfsm_fhtom(dnp, v3);
1553 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1554 #ifndef NFS_V2_ONLY
1555 if (v3) {
1556 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1557 *tl++ = vtonfsv3_type(vap->va_type);
1558 nfsm_v3attrbuild(vap, false);
1559 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1560 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1561 *tl++ = txdr_unsigned(major(vap->va_rdev));
1562 *tl = txdr_unsigned(minor(vap->va_rdev));
1563 }
1564 } else
1565 #endif
1566 {
1567 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1568 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1569 sp->sa_uid = nfs_xdrneg1;
1570 sp->sa_gid = nfs_xdrneg1;
1571 sp->sa_size = rdev;
1572 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1573 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1574 }
1575 nfsm_request(dnp, NFSPROC_MKNOD, curlwp, cnp->cn_cred);
1576 if (!error) {
1577 nfsm_mtofh(dvp, newvp, v3, gotvp);
1578 if (!gotvp) {
1579 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1580 cnp->cn_namelen, cnp->cn_cred, curlwp, &np);
1581 if (!error)
1582 newvp = NFSTOV(np);
1583 }
1584 }
1585 #ifndef NFS_V2_ONLY
1586 if (v3)
1587 nfsm_wcc_data(dvp, wccflag, 0, !error);
1588 #endif
1589 nfsm_reqdone;
1590 if (error) {
1591 if (newvp)
1592 vput(newvp);
1593 } else {
1594 if (cnp->cn_flags & MAKEENTRY)
1595 nfs_cache_enter(dvp, newvp, cnp);
1596 *vpp = newvp;
1597 }
1598 PNBUF_PUT(cnp->cn_pnbuf);
1599 VTONFS(dvp)->n_flag |= NMODIFIED;
1600 if (!wccflag)
1601 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
1602 vput(dvp);
1603 return (error);
1604 }
1605
1606 /*
1607 * nfs mknod vop
1608 * just call nfs_mknodrpc() to do the work.
1609 */
1610 /* ARGSUSED */
1611 int
1612 nfs_mknod(v)
1613 void *v;
1614 {
1615 struct vop_mknod_args /* {
1616 struct vnode *a_dvp;
1617 struct vnode **a_vpp;
1618 struct componentname *a_cnp;
1619 struct vattr *a_vap;
1620 } */ *ap = v;
1621 struct vnode *dvp = ap->a_dvp;
1622 struct componentname *cnp = ap->a_cnp;
1623 int error;
1624
1625 error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, ap->a_vap);
1626 VN_KNOTE(dvp, NOTE_WRITE);
1627 if (error == 0 || error == EEXIST)
1628 cache_purge1(dvp, cnp, 0);
1629 return (error);
1630 }
1631
1632 /*
1633 * nfs file create call
1634 */
1635 int
1636 nfs_create(v)
1637 void *v;
1638 {
1639 struct vop_create_args /* {
1640 struct vnode *a_dvp;
1641 struct vnode **a_vpp;
1642 struct componentname *a_cnp;
1643 struct vattr *a_vap;
1644 } */ *ap = v;
1645 struct vnode *dvp = ap->a_dvp;
1646 struct vattr *vap = ap->a_vap;
1647 struct componentname *cnp = ap->a_cnp;
1648 struct nfsv2_sattr *sp;
1649 u_int32_t *tl;
1650 char *cp;
1651 int32_t t1, t2;
1652 struct nfsnode *dnp, *np = (struct nfsnode *)0;
1653 struct vnode *newvp = (struct vnode *)0;
1654 char *bpos, *dpos, *cp2;
1655 int error, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1656 struct mbuf *mreq, *mrep, *md, *mb;
1657 const int v3 = NFS_ISV3(dvp);
1658 u_int32_t excl_mode = NFSV3CREATE_UNCHECKED;
1659
1660 /*
1661 * Oops, not for me..
1662 */
1663 if (vap->va_type == VSOCK)
1664 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1665
1666 KASSERT(vap->va_type == VREG);
1667
1668 #ifdef VA_EXCLUSIVE
1669 if (vap->va_vaflags & VA_EXCLUSIVE) {
1670 excl_mode = NFSV3CREATE_EXCLUSIVE;
1671 }
1672 #endif
1673 again:
1674 error = 0;
1675 nfsstats.rpccnt[NFSPROC_CREATE]++;
1676 dnp = VTONFS(dvp);
1677 nfsm_reqhead(dnp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1678 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1679 nfsm_fhtom(dnp, v3);
1680 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1681 #ifndef NFS_V2_ONLY
1682 if (v3) {
1683 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1684 if (excl_mode == NFSV3CREATE_EXCLUSIVE) {
1685 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1686 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1687 *tl++ = arc4random();
1688 *tl = arc4random();
1689 } else {
1690 *tl = txdr_unsigned(excl_mode);
1691 nfsm_v3attrbuild(vap, false);
1692 }
1693 } else
1694 #endif
1695 {
1696 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1697 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1698 sp->sa_uid = nfs_xdrneg1;
1699 sp->sa_gid = nfs_xdrneg1;
1700 sp->sa_size = 0;
1701 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1702 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1703 }
1704 nfsm_request(dnp, NFSPROC_CREATE, curlwp, cnp->cn_cred);
1705 if (!error) {
1706 nfsm_mtofh(dvp, newvp, v3, gotvp);
1707 if (!gotvp) {
1708 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1709 cnp->cn_namelen, cnp->cn_cred, curlwp, &np);
1710 if (!error)
1711 newvp = NFSTOV(np);
1712 }
1713 }
1714 #ifndef NFS_V2_ONLY
1715 if (v3)
1716 nfsm_wcc_data(dvp, wccflag, 0, !error);
1717 #endif
1718 nfsm_reqdone;
1719 if (error) {
1720 /*
1721 * nfs_request maps NFSERR_NOTSUPP to ENOTSUP.
1722 */
1723 if (v3 && error == ENOTSUP) {
1724 if (excl_mode == NFSV3CREATE_EXCLUSIVE) {
1725 excl_mode = NFSV3CREATE_GUARDED;
1726 goto again;
1727 } else if (excl_mode == NFSV3CREATE_GUARDED) {
1728 excl_mode = NFSV3CREATE_UNCHECKED;
1729 goto again;
1730 }
1731 }
1732 } else if (v3 && (excl_mode == NFSV3CREATE_EXCLUSIVE)) {
1733 struct timespec ts;
1734
1735 getnanotime(&ts);
1736
1737 /*
1738 * make sure that we'll update timestamps as
1739 * most server implementations use them to store
1740 * the create verifier.
1741 *
1742 * XXX it's better to use TOSERVER always.
1743 */
1744
1745 if (vap->va_atime.tv_sec == VNOVAL)
1746 vap->va_atime = ts;
1747 if (vap->va_mtime.tv_sec == VNOVAL)
1748 vap->va_mtime = ts;
1749
1750 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, curlwp);
1751 }
1752 if (error == 0) {
1753 if (cnp->cn_flags & MAKEENTRY)
1754 nfs_cache_enter(dvp, newvp, cnp);
1755 else
1756 cache_purge1(dvp, cnp, 0);
1757 *ap->a_vpp = newvp;
1758 } else {
1759 if (newvp)
1760 vput(newvp);
1761 if (error == EEXIST)
1762 cache_purge1(dvp, cnp, 0);
1763 }
1764 PNBUF_PUT(cnp->cn_pnbuf);
1765 VTONFS(dvp)->n_flag |= NMODIFIED;
1766 if (!wccflag)
1767 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
1768 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1769 vput(dvp);
1770 return (error);
1771 }
1772
1773 /*
1774 * nfs file remove call
1775 * To try and make nfs semantics closer to ufs semantics, a file that has
1776 * other processes using the vnode is renamed instead of removed and then
1777 * removed later on the last close.
1778 * - If v_usecount > 1
1779 * If a rename is not already in the works
1780 * call nfs_sillyrename() to set it up
1781 * else
1782 * do the remove rpc
1783 */
1784 int
1785 nfs_remove(v)
1786 void *v;
1787 {
1788 struct vop_remove_args /* {
1789 struct vnodeop_desc *a_desc;
1790 struct vnode * a_dvp;
1791 struct vnode * a_vp;
1792 struct componentname * a_cnp;
1793 } */ *ap = v;
1794 struct vnode *vp = ap->a_vp;
1795 struct vnode *dvp = ap->a_dvp;
1796 struct componentname *cnp = ap->a_cnp;
1797 struct nfsnode *np = VTONFS(vp);
1798 int error = 0;
1799 struct vattr vattr;
1800
1801 #ifndef DIAGNOSTIC
1802 if ((cnp->cn_flags & HASBUF) == 0)
1803 panic("nfs_remove: no name");
1804 if (vp->v_usecount < 1)
1805 panic("nfs_remove: bad v_usecount");
1806 #endif
1807 if (vp->v_type == VDIR)
1808 error = EPERM;
1809 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1810 VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1811 vattr.va_nlink > 1)) {
1812 /*
1813 * Purge the name cache so that the chance of a lookup for
1814 * the name succeeding while the remove is in progress is
1815 * minimized. Without node locking it can still happen, such
1816 * that an I/O op returns ESTALE, but since you get this if
1817 * another host removes the file..
1818 */
1819 cache_purge(vp);
1820 /*
1821 * throw away biocache buffers, mainly to avoid
1822 * unnecessary delayed writes later.
1823 */
1824 error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, curlwp, 1);
1825 /* Do the rpc */
1826 if (error != EINTR)
1827 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1828 cnp->cn_namelen, cnp->cn_cred, curlwp);
1829 } else if (!np->n_sillyrename)
1830 error = nfs_sillyrename(dvp, vp, cnp, false);
1831 PNBUF_PUT(cnp->cn_pnbuf);
1832 if (!error && nfs_getattrcache(vp, &vattr) == 0 &&
1833 vattr.va_nlink == 1) {
1834 np->n_flag |= NREMOVED;
1835 }
1836 NFS_INVALIDATE_ATTRCACHE(np);
1837 VN_KNOTE(vp, NOTE_DELETE);
1838 VN_KNOTE(dvp, NOTE_WRITE);
1839 if (dvp == vp)
1840 vrele(vp);
1841 else
1842 vput(vp);
1843 vput(dvp);
1844 return (error);
1845 }
1846
1847 /*
1848 * nfs file remove rpc called from nfs_inactive
1849 */
1850 int
1851 nfs_removeit(sp)
1852 struct sillyrename *sp;
1853 {
1854
1855 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1856 (struct lwp *)0));
1857 }
1858
1859 /*
1860 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1861 */
1862 int
1863 nfs_removerpc(dvp, name, namelen, cred, l)
1864 struct vnode *dvp;
1865 const char *name;
1866 int namelen;
1867 kauth_cred_t cred;
1868 struct lwp *l;
1869 {
1870 u_int32_t *tl;
1871 char *cp;
1872 #ifndef NFS_V2_ONLY
1873 int32_t t1;
1874 char *cp2;
1875 #endif
1876 int32_t t2;
1877 char *bpos, *dpos;
1878 int error = 0, wccflag = NFSV3_WCCRATTR;
1879 struct mbuf *mreq, *mrep, *md, *mb;
1880 const int v3 = NFS_ISV3(dvp);
1881 int rexmit = 0;
1882 struct nfsnode *dnp = VTONFS(dvp);
1883
1884 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1885 nfsm_reqhead(dnp, NFSPROC_REMOVE,
1886 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1887 nfsm_fhtom(dnp, v3);
1888 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1889 nfsm_request1(dnp, NFSPROC_REMOVE, l, cred, &rexmit);
1890 #ifndef NFS_V2_ONLY
1891 if (v3)
1892 nfsm_wcc_data(dvp, wccflag, 0, !error);
1893 #endif
1894 nfsm_reqdone;
1895 VTONFS(dvp)->n_flag |= NMODIFIED;
1896 if (!wccflag)
1897 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
1898 /*
1899 * Kludge City: If the first reply to the remove rpc is lost..
1900 * the reply to the retransmitted request will be ENOENT
1901 * since the file was in fact removed
1902 * Therefore, we cheat and return success.
1903 */
1904 if (rexmit && error == ENOENT)
1905 error = 0;
1906 return (error);
1907 }
1908
1909 /*
1910 * nfs file rename call
1911 */
1912 int
1913 nfs_rename(v)
1914 void *v;
1915 {
1916 struct vop_rename_args /* {
1917 struct vnode *a_fdvp;
1918 struct vnode *a_fvp;
1919 struct componentname *a_fcnp;
1920 struct vnode *a_tdvp;
1921 struct vnode *a_tvp;
1922 struct componentname *a_tcnp;
1923 } */ *ap = v;
1924 struct vnode *fvp = ap->a_fvp;
1925 struct vnode *tvp = ap->a_tvp;
1926 struct vnode *fdvp = ap->a_fdvp;
1927 struct vnode *tdvp = ap->a_tdvp;
1928 struct componentname *tcnp = ap->a_tcnp;
1929 struct componentname *fcnp = ap->a_fcnp;
1930 int error;
1931
1932 #ifndef DIAGNOSTIC
1933 if ((tcnp->cn_flags & HASBUF) == 0 ||
1934 (fcnp->cn_flags & HASBUF) == 0)
1935 panic("nfs_rename: no name");
1936 #endif
1937 /* Check for cross-device rename */
1938 if ((fvp->v_mount != tdvp->v_mount) ||
1939 (tvp && (fvp->v_mount != tvp->v_mount))) {
1940 error = EXDEV;
1941 goto out;
1942 }
1943
1944 /*
1945 * If the tvp exists and is in use, sillyrename it before doing the
1946 * rename of the new file over it.
1947 *
1948 * Have sillyrename use link instead of rename if possible,
1949 * so that we don't lose the file if the rename fails, and so
1950 * that there's no window when the "to" file doesn't exist.
1951 */
1952 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1953 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp, true)) {
1954 VN_KNOTE(tvp, NOTE_DELETE);
1955 vput(tvp);
1956 tvp = NULL;
1957 }
1958
1959 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1960 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1961 curlwp);
1962
1963 VN_KNOTE(fdvp, NOTE_WRITE);
1964 VN_KNOTE(tdvp, NOTE_WRITE);
1965 if (error == 0 || error == EEXIST) {
1966 if (fvp->v_type == VDIR)
1967 cache_purge(fvp);
1968 else
1969 cache_purge1(fdvp, fcnp, 0);
1970 if (tvp != NULL && tvp->v_type == VDIR)
1971 cache_purge(tvp);
1972 else
1973 cache_purge1(tdvp, tcnp, 0);
1974 }
1975 out:
1976 if (tdvp == tvp)
1977 vrele(tdvp);
1978 else
1979 vput(tdvp);
1980 if (tvp)
1981 vput(tvp);
1982 vrele(fdvp);
1983 vrele(fvp);
1984 return (error);
1985 }
1986
1987 /*
1988 * nfs file rename rpc called from nfs_remove() above
1989 */
1990 int
1991 nfs_renameit(sdvp, scnp, sp)
1992 struct vnode *sdvp;
1993 struct componentname *scnp;
1994 struct sillyrename *sp;
1995 {
1996 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1997 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, curlwp));
1998 }
1999
2000 /*
2001 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
2002 */
2003 int
2004 nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, l)
2005 struct vnode *fdvp;
2006 const char *fnameptr;
2007 int fnamelen;
2008 struct vnode *tdvp;
2009 const char *tnameptr;
2010 int tnamelen;
2011 kauth_cred_t cred;
2012 struct lwp *l;
2013 {
2014 u_int32_t *tl;
2015 char *cp;
2016 #ifndef NFS_V2_ONLY
2017 int32_t t1;
2018 char *cp2;
2019 #endif
2020 int32_t t2;
2021 char *bpos, *dpos;
2022 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
2023 struct mbuf *mreq, *mrep, *md, *mb;
2024 const int v3 = NFS_ISV3(fdvp);
2025 int rexmit = 0;
2026 struct nfsnode *fdnp = VTONFS(fdvp);
2027
2028 nfsstats.rpccnt[NFSPROC_RENAME]++;
2029 nfsm_reqhead(fdnp, NFSPROC_RENAME,
2030 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
2031 nfsm_rndup(tnamelen));
2032 nfsm_fhtom(fdnp, v3);
2033 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
2034 nfsm_fhtom(VTONFS(tdvp), v3);
2035 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
2036 nfsm_request1(fdnp, NFSPROC_RENAME, l, cred, &rexmit);
2037 #ifndef NFS_V2_ONLY
2038 if (v3) {
2039 nfsm_wcc_data(fdvp, fwccflag, 0, !error);
2040 nfsm_wcc_data(tdvp, twccflag, 0, !error);
2041 }
2042 #endif
2043 nfsm_reqdone;
2044 VTONFS(fdvp)->n_flag |= NMODIFIED;
2045 VTONFS(tdvp)->n_flag |= NMODIFIED;
2046 if (!fwccflag)
2047 NFS_INVALIDATE_ATTRCACHE(VTONFS(fdvp));
2048 if (!twccflag)
2049 NFS_INVALIDATE_ATTRCACHE(VTONFS(tdvp));
2050 /*
2051 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2052 */
2053 if (rexmit && error == ENOENT)
2054 error = 0;
2055 return (error);
2056 }
2057
2058 /*
2059 * NFS link RPC, called from nfs_link.
2060 * Assumes dvp and vp locked, and leaves them that way.
2061 */
2062
2063 static int
2064 nfs_linkrpc(struct vnode *dvp, struct vnode *vp, const char *name,
2065 size_t namelen, kauth_cred_t cred, struct lwp *l)
2066 {
2067 u_int32_t *tl;
2068 char *cp;
2069 #ifndef NFS_V2_ONLY
2070 int32_t t1;
2071 char *cp2;
2072 #endif
2073 int32_t t2;
2074 char *bpos, *dpos;
2075 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
2076 struct mbuf *mreq, *mrep, *md, *mb;
2077 const int v3 = NFS_ISV3(dvp);
2078 int rexmit = 0;
2079 struct nfsnode *np = VTONFS(vp);
2080
2081 nfsstats.rpccnt[NFSPROC_LINK]++;
2082 nfsm_reqhead(np, NFSPROC_LINK,
2083 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(namelen));
2084 nfsm_fhtom(np, v3);
2085 nfsm_fhtom(VTONFS(dvp), v3);
2086 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
2087 nfsm_request1(np, NFSPROC_LINK, l, cred, &rexmit);
2088 #ifndef NFS_V2_ONLY
2089 if (v3) {
2090 nfsm_postop_attr(vp, attrflag, 0);
2091 nfsm_wcc_data(dvp, wccflag, 0, !error);
2092 }
2093 #endif
2094 nfsm_reqdone;
2095
2096 VTONFS(dvp)->n_flag |= NMODIFIED;
2097 if (!attrflag)
2098 NFS_INVALIDATE_ATTRCACHE(VTONFS(vp));
2099 if (!wccflag)
2100 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2101
2102 /*
2103 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2104 */
2105 if (rexmit && error == EEXIST)
2106 error = 0;
2107
2108 return error;
2109 }
2110
2111 /*
2112 * nfs hard link create call
2113 */
2114 int
2115 nfs_link(v)
2116 void *v;
2117 {
2118 struct vop_link_args /* {
2119 struct vnode *a_dvp;
2120 struct vnode *a_vp;
2121 struct componentname *a_cnp;
2122 } */ *ap = v;
2123 struct vnode *vp = ap->a_vp;
2124 struct vnode *dvp = ap->a_dvp;
2125 struct componentname *cnp = ap->a_cnp;
2126 int error = 0;
2127
2128 if (dvp->v_mount != vp->v_mount) {
2129 VOP_ABORTOP(dvp, cnp);
2130 vput(dvp);
2131 return (EXDEV);
2132 }
2133 if (dvp != vp) {
2134 error = vn_lock(vp, LK_EXCLUSIVE);
2135 if (error != 0) {
2136 VOP_ABORTOP(dvp, cnp);
2137 vput(dvp);
2138 return error;
2139 }
2140 }
2141
2142 /*
2143 * Push all writes to the server, so that the attribute cache
2144 * doesn't get "out of sync" with the server.
2145 * XXX There should be a better way!
2146 */
2147 VOP_FSYNC(vp, cnp->cn_cred, FSYNC_WAIT, 0, 0);
2148
2149 error = nfs_linkrpc(dvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
2150 cnp->cn_cred, curlwp);
2151
2152 if (error == 0)
2153 cache_purge1(dvp, cnp, 0);
2154 PNBUF_PUT(cnp->cn_pnbuf);
2155 if (dvp != vp)
2156 VOP_UNLOCK(vp, 0);
2157 VN_KNOTE(vp, NOTE_LINK);
2158 VN_KNOTE(dvp, NOTE_WRITE);
2159 vput(dvp);
2160 return (error);
2161 }
2162
2163 /*
2164 * nfs symbolic link create call
2165 */
2166 int
2167 nfs_symlink(v)
2168 void *v;
2169 {
2170 struct vop_symlink_args /* {
2171 struct vnode *a_dvp;
2172 struct vnode **a_vpp;
2173 struct componentname *a_cnp;
2174 struct vattr *a_vap;
2175 char *a_target;
2176 } */ *ap = v;
2177 struct vnode *dvp = ap->a_dvp;
2178 struct vattr *vap = ap->a_vap;
2179 struct componentname *cnp = ap->a_cnp;
2180 struct nfsv2_sattr *sp;
2181 u_int32_t *tl;
2182 char *cp;
2183 int32_t t1, t2;
2184 char *bpos, *dpos, *cp2;
2185 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2186 struct mbuf *mreq, *mrep, *md, *mb;
2187 struct vnode *newvp = (struct vnode *)0;
2188 const int v3 = NFS_ISV3(dvp);
2189 int rexmit = 0;
2190 struct nfsnode *dnp = VTONFS(dvp);
2191
2192 *ap->a_vpp = NULL;
2193 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2194 slen = strlen(ap->a_target);
2195 nfsm_reqhead(dnp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
2196 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
2197 nfsm_fhtom(dnp, v3);
2198 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2199 #ifndef NFS_V2_ONlY
2200 if (v3)
2201 nfsm_v3attrbuild(vap, false);
2202 #endif
2203 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
2204 #ifndef NFS_V2_ONlY
2205 if (!v3) {
2206 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2207 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2208 sp->sa_uid = nfs_xdrneg1;
2209 sp->sa_gid = nfs_xdrneg1;
2210 sp->sa_size = nfs_xdrneg1;
2211 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2212 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2213 }
2214 #endif
2215 nfsm_request1(dnp, NFSPROC_SYMLINK, curlwp, cnp->cn_cred,
2216 &rexmit);
2217 #ifndef NFS_V2_ONlY
2218 if (v3) {
2219 if (!error)
2220 nfsm_mtofh(dvp, newvp, v3, gotvp);
2221 nfsm_wcc_data(dvp, wccflag, 0, !error);
2222 }
2223 #endif
2224 nfsm_reqdone;
2225 /*
2226 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2227 */
2228 if (rexmit && error == EEXIST)
2229 error = 0;
2230 if (error == 0 || error == EEXIST)
2231 cache_purge1(dvp, cnp, 0);
2232 if (error == 0 && newvp == NULL) {
2233 struct nfsnode *np = NULL;
2234
2235 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2236 cnp->cn_cred, curlwp, &np);
2237 if (error == 0)
2238 newvp = NFSTOV(np);
2239 }
2240 if (error) {
2241 if (newvp != NULL)
2242 vput(newvp);
2243 } else {
2244 *ap->a_vpp = newvp;
2245 }
2246 PNBUF_PUT(cnp->cn_pnbuf);
2247 VTONFS(dvp)->n_flag |= NMODIFIED;
2248 if (!wccflag)
2249 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2250 VN_KNOTE(dvp, NOTE_WRITE);
2251 vput(dvp);
2252 return (error);
2253 }
2254
2255 /*
2256 * nfs make dir call
2257 */
2258 int
2259 nfs_mkdir(v)
2260 void *v;
2261 {
2262 struct vop_mkdir_args /* {
2263 struct vnode *a_dvp;
2264 struct vnode **a_vpp;
2265 struct componentname *a_cnp;
2266 struct vattr *a_vap;
2267 } */ *ap = v;
2268 struct vnode *dvp = ap->a_dvp;
2269 struct vattr *vap = ap->a_vap;
2270 struct componentname *cnp = ap->a_cnp;
2271 struct nfsv2_sattr *sp;
2272 u_int32_t *tl;
2273 char *cp;
2274 int32_t t1, t2;
2275 int len;
2276 struct nfsnode *dnp = VTONFS(dvp), *np = (struct nfsnode *)0;
2277 struct vnode *newvp = (struct vnode *)0;
2278 char *bpos, *dpos, *cp2;
2279 int error = 0, wccflag = NFSV3_WCCRATTR;
2280 int gotvp = 0;
2281 int rexmit = 0;
2282 struct mbuf *mreq, *mrep, *md, *mb;
2283 const int v3 = NFS_ISV3(dvp);
2284
2285 len = cnp->cn_namelen;
2286 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2287 nfsm_reqhead(dnp, NFSPROC_MKDIR,
2288 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2289 nfsm_fhtom(dnp, v3);
2290 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
2291 #ifndef NFS_V2_ONLY
2292 if (v3) {
2293 nfsm_v3attrbuild(vap, false);
2294 } else
2295 #endif
2296 {
2297 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2298 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2299 sp->sa_uid = nfs_xdrneg1;
2300 sp->sa_gid = nfs_xdrneg1;
2301 sp->sa_size = nfs_xdrneg1;
2302 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2303 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2304 }
2305 nfsm_request1(dnp, NFSPROC_MKDIR, curlwp, cnp->cn_cred, &rexmit);
2306 if (!error)
2307 nfsm_mtofh(dvp, newvp, v3, gotvp);
2308 if (v3)
2309 nfsm_wcc_data(dvp, wccflag, 0, !error);
2310 nfsm_reqdone;
2311 VTONFS(dvp)->n_flag |= NMODIFIED;
2312 if (!wccflag)
2313 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2314 /*
2315 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2316 * if we can succeed in looking up the directory.
2317 */
2318 if ((rexmit && error == EEXIST) || (!error && !gotvp)) {
2319 if (newvp) {
2320 vput(newvp);
2321 newvp = (struct vnode *)0;
2322 }
2323 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2324 curlwp, &np);
2325 if (!error) {
2326 newvp = NFSTOV(np);
2327 if (newvp->v_type != VDIR || newvp == dvp)
2328 error = EEXIST;
2329 }
2330 }
2331 if (error) {
2332 if (newvp) {
2333 if (dvp != newvp)
2334 vput(newvp);
2335 else
2336 vrele(newvp);
2337 }
2338 } else {
2339 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
2340 if (cnp->cn_flags & MAKEENTRY)
2341 nfs_cache_enter(dvp, newvp, cnp);
2342 *ap->a_vpp = newvp;
2343 }
2344 PNBUF_PUT(cnp->cn_pnbuf);
2345 vput(dvp);
2346 return (error);
2347 }
2348
2349 /*
2350 * nfs remove directory call
2351 */
2352 int
2353 nfs_rmdir(v)
2354 void *v;
2355 {
2356 struct vop_rmdir_args /* {
2357 struct vnode *a_dvp;
2358 struct vnode *a_vp;
2359 struct componentname *a_cnp;
2360 } */ *ap = v;
2361 struct vnode *vp = ap->a_vp;
2362 struct vnode *dvp = ap->a_dvp;
2363 struct componentname *cnp = ap->a_cnp;
2364 u_int32_t *tl;
2365 char *cp;
2366 #ifndef NFS_V2_ONLY
2367 int32_t t1;
2368 char *cp2;
2369 #endif
2370 int32_t t2;
2371 char *bpos, *dpos;
2372 int error = 0, wccflag = NFSV3_WCCRATTR;
2373 int rexmit = 0;
2374 struct mbuf *mreq, *mrep, *md, *mb;
2375 const int v3 = NFS_ISV3(dvp);
2376 struct nfsnode *dnp;
2377
2378 if (dvp == vp) {
2379 vrele(dvp);
2380 vput(dvp);
2381 PNBUF_PUT(cnp->cn_pnbuf);
2382 return (EINVAL);
2383 }
2384 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2385 dnp = VTONFS(dvp);
2386 nfsm_reqhead(dnp, NFSPROC_RMDIR,
2387 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2388 nfsm_fhtom(dnp, v3);
2389 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2390 nfsm_request1(dnp, NFSPROC_RMDIR, curlwp, cnp->cn_cred, &rexmit);
2391 #ifndef NFS_V2_ONLY
2392 if (v3)
2393 nfsm_wcc_data(dvp, wccflag, 0, !error);
2394 #endif
2395 nfsm_reqdone;
2396 PNBUF_PUT(cnp->cn_pnbuf);
2397 VTONFS(dvp)->n_flag |= NMODIFIED;
2398 if (!wccflag)
2399 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2400 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
2401 VN_KNOTE(vp, NOTE_DELETE);
2402 cache_purge(vp);
2403 vput(vp);
2404 vput(dvp);
2405 /*
2406 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2407 */
2408 if (rexmit && error == ENOENT)
2409 error = 0;
2410 return (error);
2411 }
2412
2413 /*
2414 * nfs readdir call
2415 */
2416 int
2417 nfs_readdir(v)
2418 void *v;
2419 {
2420 struct vop_readdir_args /* {
2421 struct vnode *a_vp;
2422 struct uio *a_uio;
2423 kauth_cred_t a_cred;
2424 int *a_eofflag;
2425 off_t **a_cookies;
2426 int *a_ncookies;
2427 } */ *ap = v;
2428 struct vnode *vp = ap->a_vp;
2429 struct uio *uio = ap->a_uio;
2430 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2431 char *base = uio->uio_iov->iov_base;
2432 int tresid, error;
2433 size_t count, lost;
2434 struct dirent *dp;
2435 off_t *cookies = NULL;
2436 int ncookies = 0, nc;
2437
2438 if (vp->v_type != VDIR)
2439 return (EPERM);
2440
2441 lost = uio->uio_resid & (NFS_DIRFRAGSIZ - 1);
2442 count = uio->uio_resid - lost;
2443 if (count <= 0)
2444 return (EINVAL);
2445
2446 /*
2447 * Call nfs_bioread() to do the real work.
2448 */
2449 tresid = uio->uio_resid = count;
2450 error = nfs_bioread(vp, uio, 0, ap->a_cred,
2451 ap->a_cookies ? NFSBIO_CACHECOOKIES : 0);
2452
2453 if (!error && ap->a_cookies) {
2454 ncookies = count / 16;
2455 cookies = malloc(sizeof (off_t) * ncookies, M_TEMP, M_WAITOK);
2456 *ap->a_cookies = cookies;
2457 }
2458
2459 if (!error && uio->uio_resid == tresid) {
2460 uio->uio_resid += lost;
2461 nfsstats.direofcache_misses++;
2462 if (ap->a_cookies)
2463 *ap->a_ncookies = 0;
2464 *ap->a_eofflag = 1;
2465 return (0);
2466 }
2467
2468 if (!error && ap->a_cookies) {
2469 /*
2470 * Only the NFS server and emulations use cookies, and they
2471 * load the directory block into system space, so we can
2472 * just look at it directly.
2473 */
2474 if (!VMSPACE_IS_KERNEL_P(uio->uio_vmspace) ||
2475 uio->uio_iovcnt != 1)
2476 panic("nfs_readdir: lost in space");
2477 for (nc = 0; ncookies-- &&
2478 base < (char *)uio->uio_iov->iov_base; nc++){
2479 dp = (struct dirent *) base;
2480 if (dp->d_reclen == 0)
2481 break;
2482 if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
2483 *(cookies++) = (off_t)NFS_GETCOOKIE32(dp);
2484 else
2485 *(cookies++) = NFS_GETCOOKIE(dp);
2486 base += dp->d_reclen;
2487 }
2488 uio->uio_resid +=
2489 ((char *)uio->uio_iov->iov_base - base);
2490 uio->uio_iov->iov_len +=
2491 ((char *)uio->uio_iov->iov_base - base);
2492 uio->uio_iov->iov_base = base;
2493 *ap->a_ncookies = nc;
2494 }
2495
2496 uio->uio_resid += lost;
2497 *ap->a_eofflag = 0;
2498 return (error);
2499 }
2500
2501 /*
2502 * Readdir rpc call.
2503 * Called from below the buffer cache by nfs_doio().
2504 */
2505 int
2506 nfs_readdirrpc(vp, uiop, cred)
2507 struct vnode *vp;
2508 struct uio *uiop;
2509 kauth_cred_t cred;
2510 {
2511 int len, left;
2512 struct dirent *dp = NULL;
2513 u_int32_t *tl;
2514 char *cp;
2515 int32_t t1, t2;
2516 char *bpos, *dpos, *cp2;
2517 struct mbuf *mreq, *mrep, *md, *mb;
2518 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2519 struct nfsnode *dnp = VTONFS(vp);
2520 u_quad_t fileno;
2521 int error = 0, more_dirs = 1, blksiz = 0, bigenough = 1;
2522 #ifndef NFS_V2_ONLY
2523 int attrflag;
2524 #endif
2525 int nrpcs = 0, reclen;
2526 const int v3 = NFS_ISV3(vp);
2527
2528 #ifdef DIAGNOSTIC
2529 /*
2530 * Should be called from buffer cache, so only amount of
2531 * NFS_DIRBLKSIZ will be requested.
2532 */
2533 if (uiop->uio_iovcnt != 1 || uiop->uio_resid != NFS_DIRBLKSIZ)
2534 panic("nfs readdirrpc bad uio");
2535 #endif
2536
2537 /*
2538 * Loop around doing readdir rpc's of size nm_readdirsize
2539 * truncated to a multiple of NFS_DIRFRAGSIZ.
2540 * The stopping criteria is EOF or buffer full.
2541 */
2542 while (more_dirs && bigenough) {
2543 /*
2544 * Heuristic: don't bother to do another RPC to further
2545 * fill up this block if there is not much room left. (< 50%
2546 * of the readdir RPC size). This wastes some buffer space
2547 * but can save up to 50% in RPC calls.
2548 */
2549 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
2550 bigenough = 0;
2551 break;
2552 }
2553 nfsstats.rpccnt[NFSPROC_READDIR]++;
2554 nfsm_reqhead(dnp, NFSPROC_READDIR, NFSX_FH(v3) +
2555 NFSX_READDIR(v3));
2556 nfsm_fhtom(dnp, v3);
2557 #ifndef NFS_V2_ONLY
2558 if (v3) {
2559 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2560 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2561 txdr_swapcookie3(uiop->uio_offset, tl);
2562 } else {
2563 txdr_cookie3(uiop->uio_offset, tl);
2564 }
2565 tl += 2;
2566 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2567 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2568 } else
2569 #endif
2570 {
2571 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2572 *tl++ = txdr_unsigned(uiop->uio_offset);
2573 }
2574 *tl = txdr_unsigned(nmp->nm_readdirsize);
2575 nfsm_request(dnp, NFSPROC_READDIR, curlwp, cred);
2576 nrpcs++;
2577 #ifndef NFS_V2_ONLY
2578 if (v3) {
2579 nfsm_postop_attr(vp, attrflag, 0);
2580 if (!error) {
2581 nfsm_dissect(tl, u_int32_t *,
2582 2 * NFSX_UNSIGNED);
2583 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2584 dnp->n_cookieverf.nfsuquad[1] = *tl;
2585 } else {
2586 m_freem(mrep);
2587 goto nfsmout;
2588 }
2589 }
2590 #endif
2591 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2592 more_dirs = fxdr_unsigned(int, *tl);
2593
2594 /* loop thru the dir entries, doctoring them to 4bsd form */
2595 while (more_dirs && bigenough) {
2596 #ifndef NFS_V2_ONLY
2597 if (v3) {
2598 nfsm_dissect(tl, u_int32_t *,
2599 3 * NFSX_UNSIGNED);
2600 fileno = fxdr_hyper(tl);
2601 len = fxdr_unsigned(int, *(tl + 2));
2602 } else
2603 #endif
2604 {
2605 nfsm_dissect(tl, u_int32_t *,
2606 2 * NFSX_UNSIGNED);
2607 fileno = fxdr_unsigned(u_quad_t, *tl++);
2608 len = fxdr_unsigned(int, *tl);
2609 }
2610 if (len <= 0 || len > NFS_MAXNAMLEN) {
2611 error = EBADRPC;
2612 m_freem(mrep);
2613 goto nfsmout;
2614 }
2615 /* for cookie stashing */
2616 reclen = _DIRENT_RECLEN(dp, len) + 2 * sizeof(off_t);
2617 left = NFS_DIRFRAGSIZ - blksiz;
2618 if (reclen > left) {
2619 memset(uiop->uio_iov->iov_base, 0, left);
2620 dp->d_reclen += left;
2621 UIO_ADVANCE(uiop, left);
2622 blksiz = 0;
2623 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2624 }
2625 if (reclen > uiop->uio_resid)
2626 bigenough = 0;
2627 if (bigenough) {
2628 int tlen;
2629
2630 dp = (struct dirent *)uiop->uio_iov->iov_base;
2631 dp->d_fileno = fileno;
2632 dp->d_namlen = len;
2633 dp->d_reclen = reclen;
2634 dp->d_type = DT_UNKNOWN;
2635 blksiz += reclen;
2636 if (blksiz == NFS_DIRFRAGSIZ)
2637 blksiz = 0;
2638 UIO_ADVANCE(uiop, DIRHDSIZ);
2639 nfsm_mtouio(uiop, len);
2640 tlen = reclen - (DIRHDSIZ + len);
2641 (void)memset(uiop->uio_iov->iov_base, 0, tlen);
2642 UIO_ADVANCE(uiop, tlen);
2643 } else
2644 nfsm_adv(nfsm_rndup(len));
2645 #ifndef NFS_V2_ONLY
2646 if (v3) {
2647 nfsm_dissect(tl, u_int32_t *,
2648 3 * NFSX_UNSIGNED);
2649 } else
2650 #endif
2651 {
2652 nfsm_dissect(tl, u_int32_t *,
2653 2 * NFSX_UNSIGNED);
2654 }
2655 if (bigenough) {
2656 #ifndef NFS_V2_ONLY
2657 if (v3) {
2658 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2659 uiop->uio_offset =
2660 fxdr_swapcookie3(tl);
2661 else
2662 uiop->uio_offset =
2663 fxdr_cookie3(tl);
2664 }
2665 else
2666 #endif
2667 {
2668 uiop->uio_offset =
2669 fxdr_unsigned(off_t, *tl);
2670 }
2671 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2672 }
2673 if (v3)
2674 tl += 2;
2675 else
2676 tl++;
2677 more_dirs = fxdr_unsigned(int, *tl);
2678 }
2679 /*
2680 * If at end of rpc data, get the eof boolean
2681 */
2682 if (!more_dirs) {
2683 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2684 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2685
2686 /*
2687 * kludge: if we got no entries, treat it as EOF.
2688 * some server sometimes send a reply without any
2689 * entries or EOF.
2690 * although it might mean the server has very long name,
2691 * we can't handle such entries anyway.
2692 */
2693
2694 if (uiop->uio_resid >= NFS_DIRBLKSIZ)
2695 more_dirs = 0;
2696 }
2697 m_freem(mrep);
2698 }
2699 /*
2700 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2701 * by increasing d_reclen for the last record.
2702 */
2703 if (blksiz > 0) {
2704 left = NFS_DIRFRAGSIZ - blksiz;
2705 memset(uiop->uio_iov->iov_base, 0, left);
2706 dp->d_reclen += left;
2707 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2708 UIO_ADVANCE(uiop, left);
2709 }
2710
2711 /*
2712 * We are now either at the end of the directory or have filled the
2713 * block.
2714 */
2715 if (bigenough) {
2716 dnp->n_direofoffset = uiop->uio_offset;
2717 dnp->n_flag |= NEOFVALID;
2718 }
2719 nfsmout:
2720 return (error);
2721 }
2722
2723 #ifndef NFS_V2_ONLY
2724 /*
2725 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2726 */
2727 int
2728 nfs_readdirplusrpc(vp, uiop, cred)
2729 struct vnode *vp;
2730 struct uio *uiop;
2731 kauth_cred_t cred;
2732 {
2733 int len, left;
2734 struct dirent *dp = NULL;
2735 u_int32_t *tl;
2736 char *cp;
2737 int32_t t1, t2;
2738 struct vnode *newvp;
2739 char *bpos, *dpos, *cp2;
2740 struct mbuf *mreq, *mrep, *md, *mb;
2741 struct nameidata nami, *ndp = &nami;
2742 struct componentname *cnp = &ndp->ni_cnd;
2743 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2744 struct nfsnode *dnp = VTONFS(vp), *np;
2745 nfsfh_t *fhp;
2746 u_quad_t fileno;
2747 int error = 0, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2748 int attrflag, fhsize, nrpcs = 0, reclen;
2749 struct nfs_fattr fattr, *fp;
2750
2751 #ifdef DIAGNOSTIC
2752 if (uiop->uio_iovcnt != 1 || uiop->uio_resid != NFS_DIRBLKSIZ)
2753 panic("nfs readdirplusrpc bad uio");
2754 #endif
2755 ndp->ni_dvp = vp;
2756 newvp = NULLVP;
2757
2758 /*
2759 * Loop around doing readdir rpc's of size nm_readdirsize
2760 * truncated to a multiple of NFS_DIRFRAGSIZ.
2761 * The stopping criteria is EOF or buffer full.
2762 */
2763 while (more_dirs && bigenough) {
2764 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
2765 bigenough = 0;
2766 break;
2767 }
2768 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2769 nfsm_reqhead(dnp, NFSPROC_READDIRPLUS,
2770 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2771 nfsm_fhtom(dnp, 1);
2772 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2773 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2774 txdr_swapcookie3(uiop->uio_offset, tl);
2775 } else {
2776 txdr_cookie3(uiop->uio_offset, tl);
2777 }
2778 tl += 2;
2779 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2780 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2781 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2782 *tl = txdr_unsigned(nmp->nm_rsize);
2783 nfsm_request(dnp, NFSPROC_READDIRPLUS, curlwp, cred);
2784 nfsm_postop_attr(vp, attrflag, 0);
2785 if (error) {
2786 m_freem(mrep);
2787 goto nfsmout;
2788 }
2789 nrpcs++;
2790 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2791 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2792 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2793 more_dirs = fxdr_unsigned(int, *tl);
2794
2795 /* loop thru the dir entries, doctoring them to 4bsd form */
2796 while (more_dirs && bigenough) {
2797 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2798 fileno = fxdr_hyper(tl);
2799 len = fxdr_unsigned(int, *(tl + 2));
2800 if (len <= 0 || len > NFS_MAXNAMLEN) {
2801 error = EBADRPC;
2802 m_freem(mrep);
2803 goto nfsmout;
2804 }
2805 /* for cookie stashing */
2806 reclen = _DIRENT_RECLEN(dp, len) + 2 * sizeof(off_t);
2807 left = NFS_DIRFRAGSIZ - blksiz;
2808 if (reclen > left) {
2809 /*
2810 * DIRFRAGSIZ is aligned, no need to align
2811 * again here.
2812 */
2813 memset(uiop->uio_iov->iov_base, 0, left);
2814 dp->d_reclen += left;
2815 UIO_ADVANCE(uiop, left);
2816 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2817 blksiz = 0;
2818 }
2819 if (reclen > uiop->uio_resid)
2820 bigenough = 0;
2821 if (bigenough) {
2822 int tlen;
2823
2824 dp = (struct dirent *)uiop->uio_iov->iov_base;
2825 dp->d_fileno = fileno;
2826 dp->d_namlen = len;
2827 dp->d_reclen = reclen;
2828 dp->d_type = DT_UNKNOWN;
2829 blksiz += reclen;
2830 if (blksiz == NFS_DIRFRAGSIZ)
2831 blksiz = 0;
2832 UIO_ADVANCE(uiop, DIRHDSIZ);
2833 nfsm_mtouio(uiop, len);
2834 tlen = reclen - (DIRHDSIZ + len);
2835 (void)memset(uiop->uio_iov->iov_base, 0, tlen);
2836 UIO_ADVANCE(uiop, tlen);
2837 cnp->cn_nameptr = dp->d_name;
2838 cnp->cn_namelen = dp->d_namlen;
2839 } else
2840 nfsm_adv(nfsm_rndup(len));
2841 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2842 if (bigenough) {
2843 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2844 uiop->uio_offset =
2845 fxdr_swapcookie3(tl);
2846 else
2847 uiop->uio_offset =
2848 fxdr_cookie3(tl);
2849 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2850 }
2851 tl += 2;
2852
2853 /*
2854 * Since the attributes are before the file handle
2855 * (sigh), we must skip over the attributes and then
2856 * come back and get them.
2857 */
2858 attrflag = fxdr_unsigned(int, *tl);
2859 if (attrflag) {
2860 nfsm_dissect(fp, struct nfs_fattr *, NFSX_V3FATTR);
2861 memcpy(&fattr, fp, NFSX_V3FATTR);
2862 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2863 doit = fxdr_unsigned(int, *tl);
2864 if (doit) {
2865 nfsm_getfh(fhp, fhsize, 1);
2866 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2867 VREF(vp);
2868 newvp = vp;
2869 np = dnp;
2870 } else {
2871 error = nfs_nget1(vp->v_mount, fhp,
2872 fhsize, &np, LK_NOWAIT);
2873 if (!error)
2874 newvp = NFSTOV(np);
2875 }
2876 if (!error) {
2877 const char *xcp;
2878
2879 nfs_loadattrcache(&newvp, &fattr, 0, 0);
2880 if (bigenough) {
2881 dp->d_type =
2882 IFTODT(VTTOIF(np->n_vattr->va_type));
2883 if (cnp->cn_namelen <= NCHNAMLEN) {
2884 ndp->ni_vp = newvp;
2885 xcp = cnp->cn_nameptr +
2886 cnp->cn_namelen;
2887 cnp->cn_hash =
2888 namei_hash(cnp->cn_nameptr, &xcp);
2889 nfs_cache_enter(ndp->ni_dvp,
2890 ndp->ni_vp, cnp);
2891 }
2892 }
2893 }
2894 error = 0;
2895 }
2896 } else {
2897 /* Just skip over the file handle */
2898 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2899 i = fxdr_unsigned(int, *tl);
2900 nfsm_adv(nfsm_rndup(i));
2901 }
2902 if (newvp != NULLVP) {
2903 if (newvp == vp)
2904 vrele(newvp);
2905 else
2906 vput(newvp);
2907 newvp = NULLVP;
2908 }
2909 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2910 more_dirs = fxdr_unsigned(int, *tl);
2911 }
2912 /*
2913 * If at end of rpc data, get the eof boolean
2914 */
2915 if (!more_dirs) {
2916 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2917 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2918
2919 /*
2920 * kludge: see a comment in nfs_readdirrpc.
2921 */
2922
2923 if (uiop->uio_resid >= NFS_DIRBLKSIZ)
2924 more_dirs = 0;
2925 }
2926 m_freem(mrep);
2927 }
2928 /*
2929 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2930 * by increasing d_reclen for the last record.
2931 */
2932 if (blksiz > 0) {
2933 left = NFS_DIRFRAGSIZ - blksiz;
2934 memset(uiop->uio_iov->iov_base, 0, left);
2935 dp->d_reclen += left;
2936 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2937 UIO_ADVANCE(uiop, left);
2938 }
2939
2940 /*
2941 * We are now either at the end of the directory or have filled the
2942 * block.
2943 */
2944 if (bigenough) {
2945 dnp->n_direofoffset = uiop->uio_offset;
2946 dnp->n_flag |= NEOFVALID;
2947 }
2948 nfsmout:
2949 if (newvp != NULLVP) {
2950 if(newvp == vp)
2951 vrele(newvp);
2952 else
2953 vput(newvp);
2954 }
2955 return (error);
2956 }
2957 #endif
2958
2959 /*
2960 * Silly rename. To make the NFS filesystem that is stateless look a little
2961 * more like the "ufs" a remove of an active vnode is translated to a rename
2962 * to a funny looking filename that is removed by nfs_inactive on the
2963 * nfsnode. There is the potential for another process on a different client
2964 * to create the same funny name between the nfs_lookitup() fails and the
2965 * nfs_rename() completes, but...
2966 */
2967 int
2968 nfs_sillyrename(dvp, vp, cnp, dolink)
2969 struct vnode *dvp, *vp;
2970 struct componentname *cnp;
2971 bool dolink;
2972 {
2973 struct sillyrename *sp;
2974 struct nfsnode *np;
2975 int error;
2976 short pid;
2977
2978 cache_purge(dvp);
2979 np = VTONFS(vp);
2980 #ifndef DIAGNOSTIC
2981 if (vp->v_type == VDIR)
2982 panic("nfs: sillyrename dir");
2983 #endif
2984 sp = kmem_alloc(sizeof(*sp), KM_SLEEP);
2985 sp->s_cred = kauth_cred_dup(cnp->cn_cred);
2986 sp->s_dvp = dvp;
2987 VREF(dvp);
2988
2989 /* Fudge together a funny name */
2990 pid = curlwp->l_proc->p_pid;
2991 memcpy(sp->s_name, ".nfsAxxxx4.4", 13);
2992 sp->s_namlen = 12;
2993 sp->s_name[8] = hexdigits[pid & 0xf];
2994 sp->s_name[7] = hexdigits[(pid >> 4) & 0xf];
2995 sp->s_name[6] = hexdigits[(pid >> 8) & 0xf];
2996 sp->s_name[5] = hexdigits[(pid >> 12) & 0xf];
2997
2998 /* Try lookitups until we get one that isn't there */
2999 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
3000 curlwp, (struct nfsnode **)0) == 0) {
3001 sp->s_name[4]++;
3002 if (sp->s_name[4] > 'z') {
3003 error = EINVAL;
3004 goto bad;
3005 }
3006 }
3007 if (dolink) {
3008 error = nfs_linkrpc(dvp, vp, sp->s_name, sp->s_namlen,
3009 sp->s_cred, curlwp);
3010 /*
3011 * nfs_request maps NFSERR_NOTSUPP to ENOTSUP.
3012 */
3013 if (error == ENOTSUP) {
3014 error = nfs_renameit(dvp, cnp, sp);
3015 }
3016 } else {
3017 error = nfs_renameit(dvp, cnp, sp);
3018 }
3019 if (error)
3020 goto bad;
3021 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
3022 curlwp, &np);
3023 np->n_sillyrename = sp;
3024 return (0);
3025 bad:
3026 vrele(sp->s_dvp);
3027 kauth_cred_free(sp->s_cred);
3028 kmem_free(sp, sizeof(*sp));
3029 return (error);
3030 }
3031
3032 /*
3033 * Look up a file name and optionally either update the file handle or
3034 * allocate an nfsnode, depending on the value of npp.
3035 * npp == NULL --> just do the lookup
3036 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
3037 * handled too
3038 * *npp != NULL --> update the file handle in the vnode
3039 */
3040 int
3041 nfs_lookitup(dvp, name, len, cred, l, npp)
3042 struct vnode *dvp;
3043 const char *name;
3044 int len;
3045 kauth_cred_t cred;
3046 struct lwp *l;
3047 struct nfsnode **npp;
3048 {
3049 u_int32_t *tl;
3050 char *cp;
3051 int32_t t1, t2;
3052 struct vnode *newvp = (struct vnode *)0;
3053 struct nfsnode *np, *dnp = VTONFS(dvp);
3054 char *bpos, *dpos, *cp2;
3055 int error = 0, fhlen;
3056 #ifndef NFS_V2_ONLY
3057 int attrflag;
3058 #endif
3059 struct mbuf *mreq, *mrep, *md, *mb;
3060 nfsfh_t *nfhp;
3061 const int v3 = NFS_ISV3(dvp);
3062
3063 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
3064 nfsm_reqhead(dnp, NFSPROC_LOOKUP,
3065 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
3066 nfsm_fhtom(dnp, v3);
3067 nfsm_strtom(name, len, NFS_MAXNAMLEN);
3068 nfsm_request(dnp, NFSPROC_LOOKUP, l, cred);
3069 if (npp && !error) {
3070 nfsm_getfh(nfhp, fhlen, v3);
3071 if (*npp) {
3072 np = *npp;
3073 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
3074 kmem_free(np->n_fhp, np->n_fhsize);
3075 np->n_fhp = &np->n_fh;
3076 }
3077 #if NFS_SMALLFH < NFSX_V3FHMAX
3078 else if (np->n_fhsize <= NFS_SMALLFH && fhlen > NFS_SMALLFH)
3079 np->n_fhp = kmem_alloc(fhlen, KM_SLEEP);
3080 #endif
3081 memcpy(np->n_fhp, nfhp, fhlen);
3082 np->n_fhsize = fhlen;
3083 newvp = NFSTOV(np);
3084 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
3085 VREF(dvp);
3086 newvp = dvp;
3087 np = dnp;
3088 } else {
3089 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
3090 if (error) {
3091 m_freem(mrep);
3092 return (error);
3093 }
3094 newvp = NFSTOV(np);
3095 }
3096 #ifndef NFS_V2_ONLY
3097 if (v3) {
3098 nfsm_postop_attr(newvp, attrflag, 0);
3099 if (!attrflag && *npp == NULL) {
3100 m_freem(mrep);
3101 vput(newvp);
3102 return (ENOENT);
3103 }
3104 } else
3105 #endif
3106 nfsm_loadattr(newvp, (struct vattr *)0, 0);
3107 }
3108 nfsm_reqdone;
3109 if (npp && *npp == NULL) {
3110 if (error) {
3111 if (newvp)
3112 vput(newvp);
3113 } else
3114 *npp = np;
3115 }
3116 return (error);
3117 }
3118
3119 #ifndef NFS_V2_ONLY
3120 /*
3121 * Nfs Version 3 commit rpc
3122 */
3123 int
3124 nfs_commit(vp, offset, cnt, l)
3125 struct vnode *vp;
3126 off_t offset;
3127 uint32_t cnt;
3128 struct lwp *l;
3129 {
3130 char *cp;
3131 u_int32_t *tl;
3132 int32_t t1, t2;
3133 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3134 char *bpos, *dpos, *cp2;
3135 int error = 0, wccflag = NFSV3_WCCRATTR;
3136 struct mbuf *mreq, *mrep, *md, *mb;
3137 struct nfsnode *np;
3138
3139 KASSERT(NFS_ISV3(vp));
3140
3141 #ifdef NFS_DEBUG_COMMIT
3142 printf("commit %lu - %lu\n", (unsigned long)offset,
3143 (unsigned long)(offset + cnt));
3144 #endif
3145
3146 mutex_enter(&nmp->nm_lock);
3147 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0) {
3148 mutex_exit(&nmp->nm_lock);
3149 return (0);
3150 }
3151 mutex_exit(&nmp->nm_lock);
3152 nfsstats.rpccnt[NFSPROC_COMMIT]++;
3153 np = VTONFS(vp);
3154 nfsm_reqhead(np, NFSPROC_COMMIT, NFSX_FH(1));
3155 nfsm_fhtom(np, 1);
3156 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3157 txdr_hyper(offset, tl);
3158 tl += 2;
3159 *tl = txdr_unsigned(cnt);
3160 nfsm_request(np, NFSPROC_COMMIT, l, np->n_wcred);
3161 nfsm_wcc_data(vp, wccflag, NAC_NOTRUNC, false);
3162 if (!error) {
3163 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
3164 mutex_enter(&nmp->nm_lock);
3165 if ((nmp->nm_iflag & NFSMNT_STALEWRITEVERF) ||
3166 memcmp(nmp->nm_writeverf, tl, NFSX_V3WRITEVERF)) {
3167 memcpy(nmp->nm_writeverf, tl, NFSX_V3WRITEVERF);
3168 error = NFSERR_STALEWRITEVERF;
3169 nmp->nm_iflag |= NFSMNT_STALEWRITEVERF;
3170 }
3171 mutex_exit(&nmp->nm_lock);
3172 }
3173 nfsm_reqdone;
3174 return (error);
3175 }
3176 #endif
3177
3178 /*
3179 * Kludge City..
3180 * - make nfs_bmap() essentially a no-op that does no translation
3181 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
3182 * (Maybe I could use the process's page mapping, but I was concerned that
3183 * Kernel Write might not be enabled and also figured copyout() would do
3184 * a lot more work than memcpy() and also it currently happens in the
3185 * context of the swapper process (2).
3186 */
3187 int
3188 nfs_bmap(v)
3189 void *v;
3190 {
3191 struct vop_bmap_args /* {
3192 struct vnode *a_vp;
3193 daddr_t a_bn;
3194 struct vnode **a_vpp;
3195 daddr_t *a_bnp;
3196 int *a_runp;
3197 } */ *ap = v;
3198 struct vnode *vp = ap->a_vp;
3199 int bshift = vp->v_mount->mnt_fs_bshift - vp->v_mount->mnt_dev_bshift;
3200
3201 if (ap->a_vpp != NULL)
3202 *ap->a_vpp = vp;
3203 if (ap->a_bnp != NULL)
3204 *ap->a_bnp = ap->a_bn << bshift;
3205 if (ap->a_runp != NULL)
3206 *ap->a_runp = 1024 * 1024; /* XXX */
3207 return (0);
3208 }
3209
3210 /*
3211 * Strategy routine.
3212 * For async requests when nfsiod(s) are running, queue the request by
3213 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
3214 * request.
3215 */
3216 int
3217 nfs_strategy(v)
3218 void *v;
3219 {
3220 struct vop_strategy_args *ap = v;
3221 struct buf *bp = ap->a_bp;
3222 int error = 0;
3223
3224 if ((bp->b_flags & (B_PHYS|B_ASYNC)) == (B_PHYS|B_ASYNC))
3225 panic("nfs physio/async");
3226
3227 /*
3228 * If the op is asynchronous and an i/o daemon is waiting
3229 * queue the request, wake it up and wait for completion
3230 * otherwise just do it ourselves.
3231 */
3232 if ((bp->b_flags & B_ASYNC) == 0 || nfs_asyncio(bp))
3233 error = nfs_doio(bp);
3234 return (error);
3235 }
3236
3237 /*
3238 * fsync vnode op. Just call nfs_flush() with commit == 1.
3239 */
3240 /* ARGSUSED */
3241 int
3242 nfs_fsync(v)
3243 void *v;
3244 {
3245 struct vop_fsync_args /* {
3246 struct vnodeop_desc *a_desc;
3247 struct vnode * a_vp;
3248 kauth_cred_t a_cred;
3249 int a_flags;
3250 off_t offlo;
3251 off_t offhi;
3252 struct lwp * a_l;
3253 } */ *ap = v;
3254
3255 struct vnode *vp = ap->a_vp;
3256
3257 if (vp->v_type != VREG)
3258 return 0;
3259
3260 return (nfs_flush(vp, ap->a_cred,
3261 (ap->a_flags & FSYNC_WAIT) != 0 ? MNT_WAIT : 0, curlwp, 1));
3262 }
3263
3264 /*
3265 * Flush all the data associated with a vnode.
3266 */
3267 int
3268 nfs_flush(struct vnode *vp, kauth_cred_t cred, int waitfor, struct lwp *l,
3269 int commit)
3270 {
3271 struct nfsnode *np = VTONFS(vp);
3272 int error;
3273 int flushflags = PGO_ALLPAGES|PGO_CLEANIT|PGO_SYNCIO;
3274 UVMHIST_FUNC("nfs_flush"); UVMHIST_CALLED(ubchist);
3275
3276 mutex_enter(&vp->v_interlock);
3277 error = VOP_PUTPAGES(vp, 0, 0, flushflags);
3278 if (np->n_flag & NWRITEERR) {
3279 error = np->n_error;
3280 np->n_flag &= ~NWRITEERR;
3281 }
3282 UVMHIST_LOG(ubchist, "returning %d", error,0,0,0);
3283 return (error);
3284 }
3285
3286 /*
3287 * Return POSIX pathconf information applicable to nfs.
3288 *
3289 * N.B. The NFS V2 protocol doesn't support this RPC.
3290 */
3291 /* ARGSUSED */
3292 int
3293 nfs_pathconf(v)
3294 void *v;
3295 {
3296 struct vop_pathconf_args /* {
3297 struct vnode *a_vp;
3298 int a_name;
3299 register_t *a_retval;
3300 } */ *ap = v;
3301 struct nfsv3_pathconf *pcp;
3302 struct vnode *vp = ap->a_vp;
3303 struct mbuf *mreq, *mrep, *md, *mb;
3304 int32_t t1, t2;
3305 u_int32_t *tl;
3306 char *bpos, *dpos, *cp, *cp2;
3307 int error = 0, attrflag;
3308 #ifndef NFS_V2_ONLY
3309 struct nfsmount *nmp;
3310 unsigned int l;
3311 u_int64_t maxsize;
3312 #endif
3313 const int v3 = NFS_ISV3(vp);
3314 struct nfsnode *np = VTONFS(vp);
3315
3316 switch (ap->a_name) {
3317 /* Names that can be resolved locally. */
3318 case _PC_PIPE_BUF:
3319 *ap->a_retval = PIPE_BUF;
3320 break;
3321 case _PC_SYNC_IO:
3322 *ap->a_retval = 1;
3323 break;
3324 /* Names that cannot be resolved locally; do an RPC, if possible. */
3325 case _PC_LINK_MAX:
3326 case _PC_NAME_MAX:
3327 case _PC_CHOWN_RESTRICTED:
3328 case _PC_NO_TRUNC:
3329 if (!v3) {
3330 error = EINVAL;
3331 break;
3332 }
3333 nfsstats.rpccnt[NFSPROC_PATHCONF]++;
3334 nfsm_reqhead(np, NFSPROC_PATHCONF, NFSX_FH(1));
3335 nfsm_fhtom(np, 1);
3336 nfsm_request(np, NFSPROC_PATHCONF,
3337 curlwp, curlwp->l_cred); /* XXX */
3338 nfsm_postop_attr(vp, attrflag, 0);
3339 if (!error) {
3340 nfsm_dissect(pcp, struct nfsv3_pathconf *,
3341 NFSX_V3PATHCONF);
3342 switch (ap->a_name) {
3343 case _PC_LINK_MAX:
3344 *ap->a_retval =
3345 fxdr_unsigned(register_t, pcp->pc_linkmax);
3346 break;
3347 case _PC_NAME_MAX:
3348 *ap->a_retval =
3349 fxdr_unsigned(register_t, pcp->pc_namemax);
3350 break;
3351 case _PC_CHOWN_RESTRICTED:
3352 *ap->a_retval =
3353 (pcp->pc_chownrestricted == nfs_true);
3354 break;
3355 case _PC_NO_TRUNC:
3356 *ap->a_retval =
3357 (pcp->pc_notrunc == nfs_true);
3358 break;
3359 }
3360 }
3361 nfsm_reqdone;
3362 break;
3363 case _PC_FILESIZEBITS:
3364 #ifndef NFS_V2_ONLY
3365 if (v3) {
3366 nmp = VFSTONFS(vp->v_mount);
3367 if ((nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
3368 if ((error = nfs_fsinfo(nmp, vp,
3369 curlwp->l_cred, curlwp)) != 0) /* XXX */
3370 break;
3371 for (l = 0, maxsize = nmp->nm_maxfilesize;
3372 (maxsize >> l) > 0; l++)
3373 ;
3374 *ap->a_retval = l + 1;
3375 } else
3376 #endif
3377 {
3378 *ap->a_retval = 32; /* NFS V2 limitation */
3379 }
3380 break;
3381 default:
3382 error = EINVAL;
3383 break;
3384 }
3385
3386 return (error);
3387 }
3388
3389 /*
3390 * NFS advisory byte-level locks.
3391 */
3392 int
3393 nfs_advlock(v)
3394 void *v;
3395 {
3396 struct vop_advlock_args /* {
3397 struct vnode *a_vp;
3398 void *a_id;
3399 int a_op;
3400 struct flock *a_fl;
3401 int a_flags;
3402 } */ *ap = v;
3403 struct nfsnode *np = VTONFS(ap->a_vp);
3404
3405 return lf_advlock(ap, &np->n_lockf, np->n_size);
3406 }
3407
3408 /*
3409 * Print out the contents of an nfsnode.
3410 */
3411 int
3412 nfs_print(v)
3413 void *v;
3414 {
3415 struct vop_print_args /* {
3416 struct vnode *a_vp;
3417 } */ *ap = v;
3418 struct vnode *vp = ap->a_vp;
3419 struct nfsnode *np = VTONFS(vp);
3420
3421 printf("tag VT_NFS, fileid %lld fsid 0x%llx",
3422 (unsigned long long)np->n_vattr->va_fileid,
3423 (unsigned long long)np->n_vattr->va_fsid);
3424 if (vp->v_type == VFIFO)
3425 fifo_printinfo(vp);
3426 printf("\n");
3427 return (0);
3428 }
3429
3430 /*
3431 * nfs unlock wrapper.
3432 */
3433 int
3434 nfs_unlock(void *v)
3435 {
3436 struct vop_unlock_args /* {
3437 struct vnode *a_vp;
3438 int a_flags;
3439 } */ *ap = v;
3440 struct vnode *vp = ap->a_vp;
3441
3442 /*
3443 * VOP_UNLOCK can be called by nfs_loadattrcache
3444 * with v_data == 0.
3445 */
3446 if (VTONFS(vp)) {
3447 nfs_delayedtruncate(vp);
3448 }
3449
3450 return genfs_unlock(v);
3451 }
3452
3453 /*
3454 * nfs special file access vnode op.
3455 * Essentially just get vattr and then imitate iaccess() since the device is
3456 * local to the client.
3457 */
3458 int
3459 nfsspec_access(v)
3460 void *v;
3461 {
3462 struct vop_access_args /* {
3463 struct vnode *a_vp;
3464 int a_mode;
3465 kauth_cred_t a_cred;
3466 struct lwp *a_l;
3467 } */ *ap = v;
3468 struct vattr va;
3469 struct vnode *vp = ap->a_vp;
3470 int error;
3471
3472 error = VOP_GETATTR(vp, &va, ap->a_cred);
3473 if (error)
3474 return (error);
3475
3476 /*
3477 * Disallow write attempts on filesystems mounted read-only;
3478 * unless the file is a socket, fifo, or a block or character
3479 * device resident on the filesystem.
3480 */
3481 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3482 switch (vp->v_type) {
3483 case VREG:
3484 case VDIR:
3485 case VLNK:
3486 return (EROFS);
3487 default:
3488 break;
3489 }
3490 }
3491
3492 return (vaccess(va.va_type, va.va_mode,
3493 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
3494 }
3495
3496 /*
3497 * Read wrapper for special devices.
3498 */
3499 int
3500 nfsspec_read(v)
3501 void *v;
3502 {
3503 struct vop_read_args /* {
3504 struct vnode *a_vp;
3505 struct uio *a_uio;
3506 int a_ioflag;
3507 kauth_cred_t a_cred;
3508 } */ *ap = v;
3509 struct nfsnode *np = VTONFS(ap->a_vp);
3510
3511 /*
3512 * Set access flag.
3513 */
3514 np->n_flag |= NACC;
3515 getnanotime(&np->n_atim);
3516 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3517 }
3518
3519 /*
3520 * Write wrapper for special devices.
3521 */
3522 int
3523 nfsspec_write(v)
3524 void *v;
3525 {
3526 struct vop_write_args /* {
3527 struct vnode *a_vp;
3528 struct uio *a_uio;
3529 int a_ioflag;
3530 kauth_cred_t a_cred;
3531 } */ *ap = v;
3532 struct nfsnode *np = VTONFS(ap->a_vp);
3533
3534 /*
3535 * Set update flag.
3536 */
3537 np->n_flag |= NUPD;
3538 getnanotime(&np->n_mtim);
3539 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3540 }
3541
3542 /*
3543 * Close wrapper for special devices.
3544 *
3545 * Update the times on the nfsnode then do device close.
3546 */
3547 int
3548 nfsspec_close(v)
3549 void *v;
3550 {
3551 struct vop_close_args /* {
3552 struct vnode *a_vp;
3553 int a_fflag;
3554 kauth_cred_t a_cred;
3555 struct lwp *a_l;
3556 } */ *ap = v;
3557 struct vnode *vp = ap->a_vp;
3558 struct nfsnode *np = VTONFS(vp);
3559 struct vattr vattr;
3560
3561 if (np->n_flag & (NACC | NUPD)) {
3562 np->n_flag |= NCHG;
3563 if (vp->v_usecount == 1 &&
3564 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3565 VATTR_NULL(&vattr);
3566 if (np->n_flag & NACC)
3567 vattr.va_atime = np->n_atim;
3568 if (np->n_flag & NUPD)
3569 vattr.va_mtime = np->n_mtim;
3570 (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3571 }
3572 }
3573 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3574 }
3575
3576 /*
3577 * Read wrapper for fifos.
3578 */
3579 int
3580 nfsfifo_read(v)
3581 void *v;
3582 {
3583 struct vop_read_args /* {
3584 struct vnode *a_vp;
3585 struct uio *a_uio;
3586 int a_ioflag;
3587 kauth_cred_t a_cred;
3588 } */ *ap = v;
3589 struct nfsnode *np = VTONFS(ap->a_vp);
3590
3591 /*
3592 * Set access flag.
3593 */
3594 np->n_flag |= NACC;
3595 getnanotime(&np->n_atim);
3596 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3597 }
3598
3599 /*
3600 * Write wrapper for fifos.
3601 */
3602 int
3603 nfsfifo_write(v)
3604 void *v;
3605 {
3606 struct vop_write_args /* {
3607 struct vnode *a_vp;
3608 struct uio *a_uio;
3609 int a_ioflag;
3610 kauth_cred_t a_cred;
3611 } */ *ap = v;
3612 struct nfsnode *np = VTONFS(ap->a_vp);
3613
3614 /*
3615 * Set update flag.
3616 */
3617 np->n_flag |= NUPD;
3618 getnanotime(&np->n_mtim);
3619 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3620 }
3621
3622 /*
3623 * Close wrapper for fifos.
3624 *
3625 * Update the times on the nfsnode then do fifo close.
3626 */
3627 int
3628 nfsfifo_close(v)
3629 void *v;
3630 {
3631 struct vop_close_args /* {
3632 struct vnode *a_vp;
3633 int a_fflag;
3634 kauth_cred_t a_cred;
3635 struct lwp *a_l;
3636 } */ *ap = v;
3637 struct vnode *vp = ap->a_vp;
3638 struct nfsnode *np = VTONFS(vp);
3639 struct vattr vattr;
3640
3641 if (np->n_flag & (NACC | NUPD)) {
3642 struct timespec ts;
3643
3644 getnanotime(&ts);
3645 if (np->n_flag & NACC)
3646 np->n_atim = ts;
3647 if (np->n_flag & NUPD)
3648 np->n_mtim = ts;
3649 np->n_flag |= NCHG;
3650 if (vp->v_usecount == 1 &&
3651 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3652 VATTR_NULL(&vattr);
3653 if (np->n_flag & NACC)
3654 vattr.va_atime = np->n_atim;
3655 if (np->n_flag & NUPD)
3656 vattr.va_mtime = np->n_mtim;
3657 (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3658 }
3659 }
3660 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
3661 }
3662