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