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