nfs_vnops.c revision 1.296 1 /* $NetBSD: nfs_vnops.c,v 1.296 2012/11/05 17:24:11 dholland 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.296 2012/11/05 17:24:11 dholland 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);
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;
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, NULL, vpp);
822 KASSERT(dvp != *vpp);
823 KASSERT((cnp->cn_flags & ISWHITEOUT) == 0);
824 if (cachefound) {
825 struct vattr vattr;
826
827 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
828 if (error != 0) {
829 if (*vpp != NULLVP)
830 vrele(*vpp);
831 *vpp = NULLVP;
832 return error;
833 }
834
835 if (VOP_GETATTR(dvp, &vattr, cnp->cn_cred)
836 || timespeccmp(&vattr.va_mtime,
837 &VTONFS(dvp)->n_nctime, !=)) {
838 if (*vpp != NULLVP) {
839 vrele(*vpp);
840 *vpp = NULLVP;
841 }
842 cache_purge1(dvp, NULL, PURGE_CHILDREN);
843 timespecclear(&np->n_nctime);
844 goto dorpc;
845 }
846
847 if (*vpp == NULLVP) {
848 /* namecache gave us a negative result */
849 goto noentry;
850 }
851
852 /*
853 * investigate the vnode returned by cache_lookup_raw.
854 * if it isn't appropriate, do an rpc.
855 */
856 newvp = *vpp;
857 if ((flags & ISDOTDOT) != 0) {
858 VOP_UNLOCK(dvp);
859 }
860 error = vn_lock(newvp, LK_EXCLUSIVE);
861 if ((flags & ISDOTDOT) != 0) {
862 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
863 }
864 if (error != 0) {
865 /* newvp has been reclaimed. */
866 vrele(newvp);
867 *vpp = NULLVP;
868 goto dorpc;
869 }
870 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred)
871 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
872 nfsstats.lookupcache_hits++;
873 KASSERT(newvp->v_type != VNON);
874 return (0);
875 }
876 cache_purge1(newvp, NULL, PURGE_PARENTS);
877 vput(newvp);
878 *vpp = NULLVP;
879 }
880 dorpc:
881 #if 0
882 /*
883 * because nfsv3 has the same CREATE semantics as ours,
884 * we don't have to perform LOOKUPs beforehand.
885 *
886 * XXX ideally we can do the same for nfsv2 in the case of !O_EXCL.
887 * XXX although we have no way to know if O_EXCL is requested or not.
888 */
889
890 if (v3 && cnp->cn_nameiop == CREATE &&
891 (flags & (ISLASTCN|ISDOTDOT)) == ISLASTCN &&
892 (dvp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
893 return (EJUSTRETURN);
894 }
895 #endif /* 0 */
896
897 error = 0;
898 newvp = NULLVP;
899 nfsstats.lookupcache_misses++;
900 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
901 len = cnp->cn_namelen;
902 nfsm_reqhead(np, NFSPROC_LOOKUP,
903 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
904 nfsm_fhtom(np, v3);
905 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
906 nfsm_request(np, NFSPROC_LOOKUP, curlwp, cnp->cn_cred);
907 if (error) {
908 nfsm_postop_attr(dvp, attrflag, 0);
909 m_freem(mrep);
910 goto nfsmout;
911 }
912 nfsm_getfh(fhp, fhsize, v3);
913
914 /*
915 * Handle RENAME case...
916 */
917 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
918 if (NFS_CMPFH(np, fhp, fhsize)) {
919 m_freem(mrep);
920 return (EISDIR);
921 }
922 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
923 if (error) {
924 m_freem(mrep);
925 return error;
926 }
927 newvp = NFSTOV(np);
928 #ifndef NFS_V2_ONLY
929 if (v3) {
930 nfsm_postop_attr(newvp, attrflag, 0);
931 nfsm_postop_attr(dvp, attrflag, 0);
932 } else
933 #endif
934 nfsm_loadattr(newvp, (struct vattr *)0, 0);
935 *vpp = newvp;
936 m_freem(mrep);
937 goto validate;
938 }
939
940 /*
941 * The postop attr handling is duplicated for each if case,
942 * because it should be done while dvp is locked (unlocking
943 * dvp is different for each case).
944 */
945
946 if (NFS_CMPFH(np, fhp, fhsize)) {
947 /*
948 * as we handle "." lookup locally, this should be
949 * a broken server.
950 */
951 vref(dvp);
952 newvp = dvp;
953 #ifndef NFS_V2_ONLY
954 if (v3) {
955 nfsm_postop_attr(newvp, attrflag, 0);
956 nfsm_postop_attr(dvp, attrflag, 0);
957 } else
958 #endif
959 nfsm_loadattr(newvp, (struct vattr *)0, 0);
960 } else if (flags & ISDOTDOT) {
961 /*
962 * ".." lookup
963 */
964 VOP_UNLOCK(dvp);
965 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
966 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
967 if (error) {
968 m_freem(mrep);
969 return error;
970 }
971 newvp = NFSTOV(np);
972
973 #ifndef NFS_V2_ONLY
974 if (v3) {
975 nfsm_postop_attr(newvp, attrflag, 0);
976 nfsm_postop_attr(dvp, attrflag, 0);
977 } else
978 #endif
979 nfsm_loadattr(newvp, (struct vattr *)0, 0);
980 } else {
981 /*
982 * Other lookups.
983 */
984 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
985 if (error) {
986 m_freem(mrep);
987 return error;
988 }
989 newvp = NFSTOV(np);
990 #ifndef NFS_V2_ONLY
991 if (v3) {
992 nfsm_postop_attr(newvp, attrflag, 0);
993 nfsm_postop_attr(dvp, attrflag, 0);
994 } else
995 #endif
996 nfsm_loadattr(newvp, (struct vattr *)0, 0);
997 }
998 if (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) {
999 nfs_cache_enter(dvp, newvp, cnp);
1000 }
1001 *vpp = newvp;
1002 nfsm_reqdone;
1003 if (error) {
1004 /*
1005 * We get here only because of errors returned by
1006 * the RPC. Otherwise we'll have returned above
1007 * (the nfsm_* macros will jump to nfsm_reqdone
1008 * on error).
1009 */
1010 if (error == ENOENT && cnp->cn_nameiop != CREATE) {
1011 nfs_cache_enter(dvp, NULL, cnp);
1012 }
1013 if (newvp != NULLVP) {
1014 if (newvp == dvp) {
1015 vrele(newvp);
1016 } else {
1017 vput(newvp);
1018 }
1019 }
1020 noentry:
1021 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1022 (flags & ISLASTCN) && error == ENOENT) {
1023 if (dvp->v_mount->mnt_flag & MNT_RDONLY) {
1024 error = EROFS;
1025 } else {
1026 error = EJUSTRETURN;
1027 }
1028 }
1029 *vpp = NULL;
1030 return error;
1031 }
1032
1033 validate:
1034 /*
1035 * make sure we have valid type and size.
1036 */
1037
1038 newvp = *vpp;
1039 if (newvp->v_type == VNON) {
1040 struct vattr vattr; /* dummy */
1041
1042 KASSERT(VTONFS(newvp)->n_attrstamp == 0);
1043 error = VOP_GETATTR(newvp, &vattr, cnp->cn_cred);
1044 if (error) {
1045 vput(newvp);
1046 *vpp = NULL;
1047 }
1048 }
1049
1050 return error;
1051 }
1052
1053 /*
1054 * nfs read call.
1055 * Just call nfs_bioread() to do the work.
1056 */
1057 int
1058 nfs_read(void *v)
1059 {
1060 struct vop_read_args /* {
1061 struct vnode *a_vp;
1062 struct uio *a_uio;
1063 int a_ioflag;
1064 kauth_cred_t a_cred;
1065 } */ *ap = v;
1066 struct vnode *vp = ap->a_vp;
1067
1068 if (vp->v_type != VREG)
1069 return EISDIR;
1070 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred, 0));
1071 }
1072
1073 /*
1074 * nfs readlink call
1075 */
1076 int
1077 nfs_readlink(void *v)
1078 {
1079 struct vop_readlink_args /* {
1080 struct vnode *a_vp;
1081 struct uio *a_uio;
1082 kauth_cred_t a_cred;
1083 } */ *ap = v;
1084 struct vnode *vp = ap->a_vp;
1085 struct nfsnode *np = VTONFS(vp);
1086
1087 if (vp->v_type != VLNK)
1088 return (EPERM);
1089
1090 if (np->n_rcred != NULL) {
1091 kauth_cred_free(np->n_rcred);
1092 }
1093 np->n_rcred = ap->a_cred;
1094 kauth_cred_hold(np->n_rcred);
1095
1096 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred, 0));
1097 }
1098
1099 /*
1100 * Do a readlink rpc.
1101 * Called by nfs_doio() from below the buffer cache.
1102 */
1103 int
1104 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop, kauth_cred_t cred)
1105 {
1106 u_int32_t *tl;
1107 char *cp;
1108 int32_t t1, t2;
1109 char *bpos, *dpos, *cp2;
1110 int error = 0;
1111 uint32_t len;
1112 struct mbuf *mreq, *mrep, *md, *mb;
1113 const int v3 = NFS_ISV3(vp);
1114 struct nfsnode *np = VTONFS(vp);
1115 #ifndef NFS_V2_ONLY
1116 int attrflag;
1117 #endif
1118
1119 nfsstats.rpccnt[NFSPROC_READLINK]++;
1120 nfsm_reqhead(np, NFSPROC_READLINK, NFSX_FH(v3));
1121 nfsm_fhtom(np, v3);
1122 nfsm_request(np, NFSPROC_READLINK, curlwp, cred);
1123 #ifndef NFS_V2_ONLY
1124 if (v3)
1125 nfsm_postop_attr(vp, attrflag, 0);
1126 #endif
1127 if (!error) {
1128 #ifndef NFS_V2_ONLY
1129 if (v3) {
1130 nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
1131 len = fxdr_unsigned(uint32_t, *tl);
1132 if (len > NFS_MAXPATHLEN) {
1133 /*
1134 * this pathname is too long for us.
1135 */
1136 m_freem(mrep);
1137 /* Solaris returns EINVAL. should we follow? */
1138 error = ENAMETOOLONG;
1139 goto nfsmout;
1140 }
1141 } else
1142 #endif
1143 {
1144 nfsm_strsiz(len, NFS_MAXPATHLEN);
1145 }
1146 nfsm_mtouio(uiop, len);
1147 }
1148 nfsm_reqdone;
1149 return (error);
1150 }
1151
1152 /*
1153 * nfs read rpc call
1154 * Ditto above
1155 */
1156 int
1157 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1158 {
1159 u_int32_t *tl;
1160 char *cp;
1161 int32_t t1, t2;
1162 char *bpos, *dpos, *cp2;
1163 struct mbuf *mreq, *mrep, *md, *mb;
1164 struct nfsmount *nmp;
1165 int error = 0, len, retlen, tsiz, eof, byte_count;
1166 const int v3 = NFS_ISV3(vp);
1167 struct nfsnode *np = VTONFS(vp);
1168 #ifndef NFS_V2_ONLY
1169 int attrflag;
1170 #endif
1171
1172 #ifndef nolint
1173 eof = 0;
1174 #endif
1175 nmp = VFSTONFS(vp->v_mount);
1176 tsiz = uiop->uio_resid;
1177 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1178 return (EFBIG);
1179 iostat_busy(nmp->nm_stats);
1180 byte_count = 0; /* count bytes actually transferred */
1181 while (tsiz > 0) {
1182 nfsstats.rpccnt[NFSPROC_READ]++;
1183 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1184 nfsm_reqhead(np, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1185 nfsm_fhtom(np, v3);
1186 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1187 #ifndef NFS_V2_ONLY
1188 if (v3) {
1189 txdr_hyper(uiop->uio_offset, tl);
1190 *(tl + 2) = txdr_unsigned(len);
1191 } else
1192 #endif
1193 {
1194 *tl++ = txdr_unsigned(uiop->uio_offset);
1195 *tl++ = txdr_unsigned(len);
1196 *tl = 0;
1197 }
1198 nfsm_request(np, NFSPROC_READ, curlwp, np->n_rcred);
1199 #ifndef NFS_V2_ONLY
1200 if (v3) {
1201 nfsm_postop_attr(vp, attrflag, NAC_NOTRUNC);
1202 if (error) {
1203 m_freem(mrep);
1204 goto nfsmout;
1205 }
1206 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1207 eof = fxdr_unsigned(int, *(tl + 1));
1208 } else
1209 #endif
1210 nfsm_loadattr(vp, (struct vattr *)0, NAC_NOTRUNC);
1211 nfsm_strsiz(retlen, nmp->nm_rsize);
1212 nfsm_mtouio(uiop, retlen);
1213 m_freem(mrep);
1214 tsiz -= retlen;
1215 byte_count += retlen;
1216 #ifndef NFS_V2_ONLY
1217 if (v3) {
1218 if (eof || retlen == 0)
1219 tsiz = 0;
1220 } else
1221 #endif
1222 if (retlen < len)
1223 tsiz = 0;
1224 }
1225 nfsmout:
1226 iostat_unbusy(nmp->nm_stats, byte_count, 1);
1227 return (error);
1228 }
1229
1230 struct nfs_writerpc_context {
1231 kmutex_t nwc_lock;
1232 kcondvar_t nwc_cv;
1233 int nwc_mbufcount;
1234 };
1235
1236 /*
1237 * free mbuf used to refer protected pages while write rpc call.
1238 * called at splvm.
1239 */
1240 static void
1241 nfs_writerpc_extfree(struct mbuf *m, void *tbuf, size_t size, void *arg)
1242 {
1243 struct nfs_writerpc_context *ctx = arg;
1244
1245 KASSERT(m != NULL);
1246 KASSERT(ctx != NULL);
1247 pool_cache_put(mb_cache, m);
1248 mutex_enter(&ctx->nwc_lock);
1249 if (--ctx->nwc_mbufcount == 0) {
1250 cv_signal(&ctx->nwc_cv);
1251 }
1252 mutex_exit(&ctx->nwc_lock);
1253 }
1254
1255 /*
1256 * nfs write call
1257 */
1258 int
1259 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, bool pageprotected, bool *stalewriteverfp)
1260 {
1261 u_int32_t *tl;
1262 char *cp;
1263 int32_t t1, t2;
1264 char *bpos, *dpos;
1265 struct mbuf *mreq, *mrep, *md, *mb;
1266 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1267 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR;
1268 const int v3 = NFS_ISV3(vp);
1269 int committed = NFSV3WRITE_FILESYNC;
1270 struct nfsnode *np = VTONFS(vp);
1271 struct nfs_writerpc_context ctx;
1272 int byte_count;
1273 size_t origresid;
1274 #ifndef NFS_V2_ONLY
1275 char *cp2;
1276 int rlen, commit;
1277 #endif
1278
1279 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
1280 panic("writerpc readonly vp %p", vp);
1281 }
1282
1283 #ifdef DIAGNOSTIC
1284 if (uiop->uio_iovcnt != 1)
1285 panic("nfs: writerpc iovcnt > 1");
1286 #endif
1287 tsiz = uiop->uio_resid;
1288 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1289 return EFBIG;
1290
1291 mutex_init(&ctx.nwc_lock, MUTEX_DRIVER, IPL_VM);
1292 cv_init(&ctx.nwc_cv, "nfsmblk");
1293 ctx.nwc_mbufcount = 1;
1294
1295 retry:
1296 origresid = uiop->uio_resid;
1297 KASSERT(origresid == uiop->uio_iov->iov_len);
1298 iostat_busy(nmp->nm_stats);
1299 byte_count = 0; /* count of bytes actually written */
1300 while (tsiz > 0) {
1301 uint32_t datalen; /* data bytes need to be allocated in mbuf */
1302 uint32_t backup;
1303 bool stalewriteverf = false;
1304
1305 nfsstats.rpccnt[NFSPROC_WRITE]++;
1306 len = min(tsiz, nmp->nm_wsize);
1307 datalen = pageprotected ? 0 : nfsm_rndup(len);
1308 nfsm_reqhead(np, NFSPROC_WRITE,
1309 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + datalen);
1310 nfsm_fhtom(np, v3);
1311 #ifndef NFS_V2_ONLY
1312 if (v3) {
1313 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1314 txdr_hyper(uiop->uio_offset, tl);
1315 tl += 2;
1316 *tl++ = txdr_unsigned(len);
1317 *tl++ = txdr_unsigned(*iomode);
1318 *tl = txdr_unsigned(len);
1319 } else
1320 #endif
1321 {
1322 u_int32_t x;
1323
1324 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1325 /* Set both "begin" and "current" to non-garbage. */
1326 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1327 *tl++ = x; /* "begin offset" */
1328 *tl++ = x; /* "current offset" */
1329 x = txdr_unsigned(len);
1330 *tl++ = x; /* total to this offset */
1331 *tl = x; /* size of this write */
1332
1333 }
1334 if (pageprotected) {
1335 /*
1336 * since we know pages can't be modified during i/o,
1337 * no need to copy them for us.
1338 */
1339 struct mbuf *m;
1340 struct iovec *iovp = uiop->uio_iov;
1341
1342 m = m_get(M_WAIT, MT_DATA);
1343 MCLAIM(m, &nfs_mowner);
1344 MEXTADD(m, iovp->iov_base, len, M_MBUF,
1345 nfs_writerpc_extfree, &ctx);
1346 m->m_flags |= M_EXT_ROMAP;
1347 m->m_len = len;
1348 mb->m_next = m;
1349 /*
1350 * no need to maintain mb and bpos here
1351 * because no one care them later.
1352 */
1353 #if 0
1354 mb = m;
1355 bpos = mtod(void *, mb) + mb->m_len;
1356 #endif
1357 UIO_ADVANCE(uiop, len);
1358 uiop->uio_offset += len;
1359 mutex_enter(&ctx.nwc_lock);
1360 ctx.nwc_mbufcount++;
1361 mutex_exit(&ctx.nwc_lock);
1362 nfs_zeropad(mb, 0, nfsm_padlen(len));
1363 } else {
1364 nfsm_uiotom(uiop, len);
1365 }
1366 nfsm_request(np, NFSPROC_WRITE, curlwp, np->n_wcred);
1367 #ifndef NFS_V2_ONLY
1368 if (v3) {
1369 wccflag = NFSV3_WCCCHK;
1370 nfsm_wcc_data(vp, wccflag, NAC_NOTRUNC, !error);
1371 if (!error) {
1372 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1373 + NFSX_V3WRITEVERF);
1374 rlen = fxdr_unsigned(int, *tl++);
1375 if (rlen == 0) {
1376 error = NFSERR_IO;
1377 m_freem(mrep);
1378 break;
1379 } else if (rlen < len) {
1380 backup = len - rlen;
1381 UIO_ADVANCE(uiop, -backup);
1382 uiop->uio_offset -= backup;
1383 len = rlen;
1384 }
1385 commit = fxdr_unsigned(int, *tl++);
1386
1387 /*
1388 * Return the lowest committment level
1389 * obtained by any of the RPCs.
1390 */
1391 if (committed == NFSV3WRITE_FILESYNC)
1392 committed = commit;
1393 else if (committed == NFSV3WRITE_DATASYNC &&
1394 commit == NFSV3WRITE_UNSTABLE)
1395 committed = commit;
1396 mutex_enter(&nmp->nm_lock);
1397 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0){
1398 memcpy(nmp->nm_writeverf, tl,
1399 NFSX_V3WRITEVERF);
1400 nmp->nm_iflag |= NFSMNT_HASWRITEVERF;
1401 } else if ((nmp->nm_iflag &
1402 NFSMNT_STALEWRITEVERF) ||
1403 memcmp(tl, nmp->nm_writeverf,
1404 NFSX_V3WRITEVERF)) {
1405 memcpy(nmp->nm_writeverf, tl,
1406 NFSX_V3WRITEVERF);
1407 /*
1408 * note NFSMNT_STALEWRITEVERF
1409 * if we're the first thread to
1410 * notice it.
1411 */
1412 if ((nmp->nm_iflag &
1413 NFSMNT_STALEWRITEVERF) == 0) {
1414 stalewriteverf = true;
1415 nmp->nm_iflag |=
1416 NFSMNT_STALEWRITEVERF;
1417 }
1418 }
1419 mutex_exit(&nmp->nm_lock);
1420 }
1421 } else
1422 #endif
1423 nfsm_loadattr(vp, (struct vattr *)0, NAC_NOTRUNC);
1424 if (wccflag)
1425 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr->va_mtime;
1426 m_freem(mrep);
1427 if (error)
1428 break;
1429 tsiz -= len;
1430 byte_count += len;
1431 if (stalewriteverf) {
1432 *stalewriteverfp = true;
1433 stalewriteverf = false;
1434 if (committed == NFSV3WRITE_UNSTABLE &&
1435 len != origresid) {
1436 /*
1437 * if our write requests weren't atomic but
1438 * unstable, datas in previous iterations
1439 * might have already been lost now.
1440 * then, we should resend them to nfsd.
1441 */
1442 backup = origresid - tsiz;
1443 UIO_ADVANCE(uiop, -backup);
1444 uiop->uio_offset -= backup;
1445 tsiz = origresid;
1446 goto retry;
1447 }
1448 }
1449 }
1450 nfsmout:
1451 iostat_unbusy(nmp->nm_stats, byte_count, 0);
1452 if (pageprotected) {
1453 /*
1454 * wait until mbufs go away.
1455 * retransmitted mbufs can survive longer than rpc requests
1456 * themselves.
1457 */
1458 mutex_enter(&ctx.nwc_lock);
1459 ctx.nwc_mbufcount--;
1460 while (ctx.nwc_mbufcount > 0) {
1461 cv_wait(&ctx.nwc_cv, &ctx.nwc_lock);
1462 }
1463 mutex_exit(&ctx.nwc_lock);
1464 }
1465 mutex_destroy(&ctx.nwc_lock);
1466 cv_destroy(&ctx.nwc_cv);
1467 *iomode = committed;
1468 if (error)
1469 uiop->uio_resid = tsiz;
1470 return (error);
1471 }
1472
1473 /*
1474 * nfs mknod rpc
1475 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1476 * mode set to specify the file type and the size field for rdev.
1477 */
1478 int
1479 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, struct vattr *vap)
1480 {
1481 struct nfsv2_sattr *sp;
1482 u_int32_t *tl;
1483 char *cp;
1484 int32_t t1, t2;
1485 struct vnode *newvp = (struct vnode *)0;
1486 struct nfsnode *dnp, *np;
1487 char *cp2;
1488 char *bpos, *dpos;
1489 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1490 struct mbuf *mreq, *mrep, *md, *mb;
1491 u_int32_t rdev;
1492 const int v3 = NFS_ISV3(dvp);
1493
1494 if (vap->va_type == VCHR || vap->va_type == VBLK)
1495 rdev = txdr_unsigned(vap->va_rdev);
1496 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1497 rdev = nfs_xdrneg1;
1498 else {
1499 VOP_ABORTOP(dvp, cnp);
1500 vput(dvp);
1501 return (EOPNOTSUPP);
1502 }
1503 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1504 dnp = VTONFS(dvp);
1505 nfsm_reqhead(dnp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1506 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1507 nfsm_fhtom(dnp, v3);
1508 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1509 #ifndef NFS_V2_ONLY
1510 if (v3) {
1511 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1512 *tl++ = vtonfsv3_type(vap->va_type);
1513 nfsm_v3attrbuild(vap, false);
1514 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1515 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1516 *tl++ = txdr_unsigned(major(vap->va_rdev));
1517 *tl = txdr_unsigned(minor(vap->va_rdev));
1518 }
1519 } else
1520 #endif
1521 {
1522 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1523 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1524 sp->sa_uid = nfs_xdrneg1;
1525 sp->sa_gid = nfs_xdrneg1;
1526 sp->sa_size = rdev;
1527 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1528 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1529 }
1530 nfsm_request(dnp, NFSPROC_MKNOD, curlwp, cnp->cn_cred);
1531 if (!error) {
1532 nfsm_mtofh(dvp, newvp, v3, gotvp);
1533 if (!gotvp) {
1534 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1535 cnp->cn_namelen, cnp->cn_cred, curlwp, &np);
1536 if (!error)
1537 newvp = NFSTOV(np);
1538 }
1539 }
1540 #ifndef NFS_V2_ONLY
1541 if (v3)
1542 nfsm_wcc_data(dvp, wccflag, 0, !error);
1543 #endif
1544 nfsm_reqdone;
1545 if (error) {
1546 if (newvp)
1547 vput(newvp);
1548 } else {
1549 nfs_cache_enter(dvp, newvp, cnp);
1550 *vpp = newvp;
1551 }
1552 VTONFS(dvp)->n_flag |= NMODIFIED;
1553 if (!wccflag)
1554 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
1555 vput(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_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, 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_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, 0);
1708 *ap->a_vpp = newvp;
1709 } else {
1710 if (newvp)
1711 vput(newvp);
1712 if (error == EEXIST)
1713 cache_purge1(dvp, cnp, 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 vput(dvp);
1720 return (error);
1721 }
1722
1723 /*
1724 * nfs file remove call
1725 * To try and make nfs semantics closer to ufs semantics, a file that has
1726 * other processes using the vnode is renamed instead of removed and then
1727 * removed later on the last close.
1728 * - If v_usecount > 1
1729 * If a rename is not already in the works
1730 * call nfs_sillyrename() to set it up
1731 * else
1732 * do the remove rpc
1733 */
1734 int
1735 nfs_remove(void *v)
1736 {
1737 struct vop_remove_args /* {
1738 struct vnodeop_desc *a_desc;
1739 struct vnode * a_dvp;
1740 struct vnode * a_vp;
1741 struct componentname * a_cnp;
1742 } */ *ap = v;
1743 struct vnode *vp = ap->a_vp;
1744 struct vnode *dvp = ap->a_dvp;
1745 struct componentname *cnp = ap->a_cnp;
1746 struct nfsnode *np = VTONFS(vp);
1747 int error = 0;
1748 struct vattr vattr;
1749
1750 #ifndef DIAGNOSTIC
1751 if (vp->v_usecount < 1)
1752 panic("nfs_remove: bad v_usecount");
1753 #endif
1754 if (vp->v_type == VDIR)
1755 error = EPERM;
1756 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1757 VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1758 vattr.va_nlink > 1)) {
1759 /*
1760 * Purge the name cache so that the chance of a lookup for
1761 * the name succeeding while the remove is in progress is
1762 * minimized. Without node locking it can still happen, such
1763 * that an I/O op returns ESTALE, but since you get this if
1764 * another host removes the file..
1765 */
1766 cache_purge(vp);
1767 /*
1768 * throw away biocache buffers, mainly to avoid
1769 * unnecessary delayed writes later.
1770 */
1771 error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, curlwp, 1);
1772 /* Do the rpc */
1773 if (error != EINTR)
1774 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1775 cnp->cn_namelen, cnp->cn_cred, curlwp);
1776 } else if (!np->n_sillyrename)
1777 error = nfs_sillyrename(dvp, vp, cnp, false);
1778 if (!error && nfs_getattrcache(vp, &vattr) == 0 &&
1779 vattr.va_nlink == 1) {
1780 np->n_flag |= NREMOVED;
1781 }
1782 NFS_INVALIDATE_ATTRCACHE(np);
1783 VN_KNOTE(vp, NOTE_DELETE);
1784 VN_KNOTE(dvp, NOTE_WRITE);
1785 if (dvp == vp)
1786 vrele(vp);
1787 else
1788 vput(vp);
1789 vput(dvp);
1790 return (error);
1791 }
1792
1793 /*
1794 * nfs file remove rpc called from nfs_inactive
1795 */
1796 int
1797 nfs_removeit(struct sillyrename *sp)
1798 {
1799
1800 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1801 (struct lwp *)0));
1802 }
1803
1804 /*
1805 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1806 */
1807 int
1808 nfs_removerpc(struct vnode *dvp, const char *name, int namelen, kauth_cred_t cred, struct lwp *l)
1809 {
1810 u_int32_t *tl;
1811 char *cp;
1812 #ifndef NFS_V2_ONLY
1813 int32_t t1;
1814 char *cp2;
1815 #endif
1816 int32_t t2;
1817 char *bpos, *dpos;
1818 int error = 0, wccflag = NFSV3_WCCRATTR;
1819 struct mbuf *mreq, *mrep, *md, *mb;
1820 const int v3 = NFS_ISV3(dvp);
1821 int rexmit = 0;
1822 struct nfsnode *dnp = VTONFS(dvp);
1823
1824 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1825 nfsm_reqhead(dnp, NFSPROC_REMOVE,
1826 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1827 nfsm_fhtom(dnp, v3);
1828 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1829 nfsm_request1(dnp, NFSPROC_REMOVE, l, cred, &rexmit);
1830 #ifndef NFS_V2_ONLY
1831 if (v3)
1832 nfsm_wcc_data(dvp, wccflag, 0, !error);
1833 #endif
1834 nfsm_reqdone;
1835 VTONFS(dvp)->n_flag |= NMODIFIED;
1836 if (!wccflag)
1837 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
1838 /*
1839 * Kludge City: If the first reply to the remove rpc is lost..
1840 * the reply to the retransmitted request will be ENOENT
1841 * since the file was in fact removed
1842 * Therefore, we cheat and return success.
1843 */
1844 if (rexmit && error == ENOENT)
1845 error = 0;
1846 return (error);
1847 }
1848
1849 /*
1850 * nfs file rename call
1851 */
1852 int
1853 nfs_rename(void *v)
1854 {
1855 struct vop_rename_args /* {
1856 struct vnode *a_fdvp;
1857 struct vnode *a_fvp;
1858 struct componentname *a_fcnp;
1859 struct vnode *a_tdvp;
1860 struct vnode *a_tvp;
1861 struct componentname *a_tcnp;
1862 } */ *ap = v;
1863 struct vnode *fvp = ap->a_fvp;
1864 struct vnode *tvp = ap->a_tvp;
1865 struct vnode *fdvp = ap->a_fdvp;
1866 struct vnode *tdvp = ap->a_tdvp;
1867 struct componentname *tcnp = ap->a_tcnp;
1868 struct componentname *fcnp = ap->a_fcnp;
1869 int error;
1870
1871 /* Check for cross-device rename */
1872 if ((fvp->v_mount != tdvp->v_mount) ||
1873 (tvp && (fvp->v_mount != tvp->v_mount))) {
1874 error = EXDEV;
1875 goto out;
1876 }
1877
1878 /*
1879 * If the tvp exists and is in use, sillyrename it before doing the
1880 * rename of the new file over it.
1881 *
1882 * Have sillyrename use link instead of rename if possible,
1883 * so that we don't lose the file if the rename fails, and so
1884 * that there's no window when the "to" file doesn't exist.
1885 */
1886 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1887 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp, true)) {
1888 VN_KNOTE(tvp, NOTE_DELETE);
1889 vput(tvp);
1890 tvp = NULL;
1891 }
1892
1893 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1894 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1895 curlwp);
1896
1897 VN_KNOTE(fdvp, NOTE_WRITE);
1898 VN_KNOTE(tdvp, NOTE_WRITE);
1899 if (error == 0 || error == EEXIST) {
1900 if (fvp->v_type == VDIR)
1901 cache_purge(fvp);
1902 else
1903 cache_purge1(fdvp, fcnp, 0);
1904 if (tvp != NULL && tvp->v_type == VDIR)
1905 cache_purge(tvp);
1906 else
1907 cache_purge1(tdvp, tcnp, 0);
1908 }
1909 out:
1910 if (tdvp == tvp)
1911 vrele(tdvp);
1912 else
1913 vput(tdvp);
1914 if (tvp)
1915 vput(tvp);
1916 vrele(fdvp);
1917 vrele(fvp);
1918 return (error);
1919 }
1920
1921 /*
1922 * nfs file rename rpc called from nfs_remove() above
1923 */
1924 int
1925 nfs_renameit(struct vnode *sdvp, struct componentname *scnp, struct sillyrename *sp)
1926 {
1927 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1928 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, curlwp));
1929 }
1930
1931 /*
1932 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1933 */
1934 int
1935 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)
1936 {
1937 u_int32_t *tl;
1938 char *cp;
1939 #ifndef NFS_V2_ONLY
1940 int32_t t1;
1941 char *cp2;
1942 #endif
1943 int32_t t2;
1944 char *bpos, *dpos;
1945 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1946 struct mbuf *mreq, *mrep, *md, *mb;
1947 const int v3 = NFS_ISV3(fdvp);
1948 int rexmit = 0;
1949 struct nfsnode *fdnp = VTONFS(fdvp);
1950
1951 nfsstats.rpccnt[NFSPROC_RENAME]++;
1952 nfsm_reqhead(fdnp, NFSPROC_RENAME,
1953 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1954 nfsm_rndup(tnamelen));
1955 nfsm_fhtom(fdnp, v3);
1956 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1957 nfsm_fhtom(VTONFS(tdvp), v3);
1958 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1959 nfsm_request1(fdnp, NFSPROC_RENAME, l, cred, &rexmit);
1960 #ifndef NFS_V2_ONLY
1961 if (v3) {
1962 nfsm_wcc_data(fdvp, fwccflag, 0, !error);
1963 nfsm_wcc_data(tdvp, twccflag, 0, !error);
1964 }
1965 #endif
1966 nfsm_reqdone;
1967 VTONFS(fdvp)->n_flag |= NMODIFIED;
1968 VTONFS(tdvp)->n_flag |= NMODIFIED;
1969 if (!fwccflag)
1970 NFS_INVALIDATE_ATTRCACHE(VTONFS(fdvp));
1971 if (!twccflag)
1972 NFS_INVALIDATE_ATTRCACHE(VTONFS(tdvp));
1973 /*
1974 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1975 */
1976 if (rexmit && error == ENOENT)
1977 error = 0;
1978 return (error);
1979 }
1980
1981 /*
1982 * NFS link RPC, called from nfs_link.
1983 * Assumes dvp and vp locked, and leaves them that way.
1984 */
1985
1986 static int
1987 nfs_linkrpc(struct vnode *dvp, struct vnode *vp, const char *name,
1988 size_t namelen, kauth_cred_t cred, struct lwp *l)
1989 {
1990 u_int32_t *tl;
1991 char *cp;
1992 #ifndef NFS_V2_ONLY
1993 int32_t t1;
1994 char *cp2;
1995 #endif
1996 int32_t t2;
1997 char *bpos, *dpos;
1998 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1999 struct mbuf *mreq, *mrep, *md, *mb;
2000 const int v3 = NFS_ISV3(dvp);
2001 int rexmit = 0;
2002 struct nfsnode *np = VTONFS(vp);
2003
2004 nfsstats.rpccnt[NFSPROC_LINK]++;
2005 nfsm_reqhead(np, NFSPROC_LINK,
2006 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(namelen));
2007 nfsm_fhtom(np, v3);
2008 nfsm_fhtom(VTONFS(dvp), v3);
2009 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
2010 nfsm_request1(np, NFSPROC_LINK, l, cred, &rexmit);
2011 #ifndef NFS_V2_ONLY
2012 if (v3) {
2013 nfsm_postop_attr(vp, attrflag, 0);
2014 nfsm_wcc_data(dvp, wccflag, 0, !error);
2015 }
2016 #endif
2017 nfsm_reqdone;
2018
2019 VTONFS(dvp)->n_flag |= NMODIFIED;
2020 if (!attrflag)
2021 NFS_INVALIDATE_ATTRCACHE(VTONFS(vp));
2022 if (!wccflag)
2023 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2024
2025 /*
2026 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2027 */
2028 if (rexmit && error == EEXIST)
2029 error = 0;
2030
2031 return error;
2032 }
2033
2034 /*
2035 * nfs hard link create call
2036 */
2037 int
2038 nfs_link(void *v)
2039 {
2040 struct vop_link_args /* {
2041 struct vnode *a_dvp;
2042 struct vnode *a_vp;
2043 struct componentname *a_cnp;
2044 } */ *ap = v;
2045 struct vnode *vp = ap->a_vp;
2046 struct vnode *dvp = ap->a_dvp;
2047 struct componentname *cnp = ap->a_cnp;
2048 int error = 0;
2049
2050 error = vn_lock(vp, LK_EXCLUSIVE);
2051 if (error != 0) {
2052 VOP_ABORTOP(dvp, cnp);
2053 vput(dvp);
2054 return error;
2055 }
2056
2057 /*
2058 * Push all writes to the server, so that the attribute cache
2059 * doesn't get "out of sync" with the server.
2060 * XXX There should be a better way!
2061 */
2062 VOP_FSYNC(vp, cnp->cn_cred, FSYNC_WAIT, 0, 0);
2063
2064 error = nfs_linkrpc(dvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
2065 cnp->cn_cred, curlwp);
2066
2067 if (error == 0) {
2068 cache_purge1(dvp, cnp, 0);
2069 }
2070 VOP_UNLOCK(vp);
2071 VN_KNOTE(vp, NOTE_LINK);
2072 VN_KNOTE(dvp, NOTE_WRITE);
2073 vput(dvp);
2074 return (error);
2075 }
2076
2077 /*
2078 * nfs symbolic link create call
2079 */
2080 int
2081 nfs_symlink(void *v)
2082 {
2083 struct vop_symlink_args /* {
2084 struct vnode *a_dvp;
2085 struct vnode **a_vpp;
2086 struct componentname *a_cnp;
2087 struct vattr *a_vap;
2088 char *a_target;
2089 } */ *ap = v;
2090 struct vnode *dvp = ap->a_dvp;
2091 struct vattr *vap = ap->a_vap;
2092 struct componentname *cnp = ap->a_cnp;
2093 struct nfsv2_sattr *sp;
2094 u_int32_t *tl;
2095 char *cp;
2096 int32_t t1, t2;
2097 char *bpos, *dpos, *cp2;
2098 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2099 struct mbuf *mreq, *mrep, *md, *mb;
2100 struct vnode *newvp = (struct vnode *)0;
2101 const int v3 = NFS_ISV3(dvp);
2102 int rexmit = 0;
2103 struct nfsnode *dnp = VTONFS(dvp);
2104
2105 *ap->a_vpp = NULL;
2106 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2107 slen = strlen(ap->a_target);
2108 nfsm_reqhead(dnp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
2109 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
2110 nfsm_fhtom(dnp, v3);
2111 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2112 #ifndef NFS_V2_ONlY
2113 if (v3)
2114 nfsm_v3attrbuild(vap, false);
2115 #endif
2116 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
2117 #ifndef NFS_V2_ONlY
2118 if (!v3) {
2119 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2120 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2121 sp->sa_uid = nfs_xdrneg1;
2122 sp->sa_gid = nfs_xdrneg1;
2123 sp->sa_size = nfs_xdrneg1;
2124 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2125 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2126 }
2127 #endif
2128 nfsm_request1(dnp, NFSPROC_SYMLINK, curlwp, cnp->cn_cred,
2129 &rexmit);
2130 #ifndef NFS_V2_ONlY
2131 if (v3) {
2132 if (!error)
2133 nfsm_mtofh(dvp, newvp, v3, gotvp);
2134 nfsm_wcc_data(dvp, wccflag, 0, !error);
2135 }
2136 #endif
2137 nfsm_reqdone;
2138 /*
2139 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2140 */
2141 if (rexmit && error == EEXIST)
2142 error = 0;
2143 if (error == 0 || error == EEXIST)
2144 cache_purge1(dvp, cnp, 0);
2145 if (error == 0 && newvp == NULL) {
2146 struct nfsnode *np = NULL;
2147
2148 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2149 cnp->cn_cred, curlwp, &np);
2150 if (error == 0)
2151 newvp = NFSTOV(np);
2152 }
2153 if (error) {
2154 if (newvp != NULL)
2155 vput(newvp);
2156 } else {
2157 *ap->a_vpp = newvp;
2158 }
2159 VTONFS(dvp)->n_flag |= NMODIFIED;
2160 if (!wccflag)
2161 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2162 VN_KNOTE(dvp, NOTE_WRITE);
2163 vput(dvp);
2164 return (error);
2165 }
2166
2167 /*
2168 * nfs make dir call
2169 */
2170 int
2171 nfs_mkdir(void *v)
2172 {
2173 struct vop_mkdir_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 vput(dvp);
2255 return (error);
2256 }
2257
2258 /*
2259 * nfs remove directory call
2260 */
2261 int
2262 nfs_rmdir(void *v)
2263 {
2264 struct vop_rmdir_args /* {
2265 struct vnode *a_dvp;
2266 struct vnode *a_vp;
2267 struct componentname *a_cnp;
2268 } */ *ap = v;
2269 struct vnode *vp = ap->a_vp;
2270 struct vnode *dvp = ap->a_dvp;
2271 struct componentname *cnp = ap->a_cnp;
2272 u_int32_t *tl;
2273 char *cp;
2274 #ifndef NFS_V2_ONLY
2275 int32_t t1;
2276 char *cp2;
2277 #endif
2278 int32_t t2;
2279 char *bpos, *dpos;
2280 int error = 0, wccflag = NFSV3_WCCRATTR;
2281 int rexmit = 0;
2282 struct mbuf *mreq, *mrep, *md, *mb;
2283 const int v3 = NFS_ISV3(dvp);
2284 struct nfsnode *dnp;
2285
2286 if (dvp == vp) {
2287 vrele(dvp);
2288 vput(dvp);
2289 return (EINVAL);
2290 }
2291 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2292 dnp = VTONFS(dvp);
2293 nfsm_reqhead(dnp, NFSPROC_RMDIR,
2294 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2295 nfsm_fhtom(dnp, v3);
2296 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2297 nfsm_request1(dnp, NFSPROC_RMDIR, curlwp, cnp->cn_cred, &rexmit);
2298 #ifndef NFS_V2_ONLY
2299 if (v3)
2300 nfsm_wcc_data(dvp, wccflag, 0, !error);
2301 #endif
2302 nfsm_reqdone;
2303 VTONFS(dvp)->n_flag |= NMODIFIED;
2304 if (!wccflag)
2305 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2306 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
2307 VN_KNOTE(vp, NOTE_DELETE);
2308 cache_purge(vp);
2309 vput(vp);
2310 vput(dvp);
2311 /*
2312 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2313 */
2314 if (rexmit && error == ENOENT)
2315 error = 0;
2316 return (error);
2317 }
2318
2319 /*
2320 * nfs readdir call
2321 */
2322 int
2323 nfs_readdir(void *v)
2324 {
2325 struct vop_readdir_args /* {
2326 struct vnode *a_vp;
2327 struct uio *a_uio;
2328 kauth_cred_t a_cred;
2329 int *a_eofflag;
2330 off_t **a_cookies;
2331 int *a_ncookies;
2332 } */ *ap = v;
2333 struct vnode *vp = ap->a_vp;
2334 struct uio *uio = ap->a_uio;
2335 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2336 char *base = uio->uio_iov->iov_base;
2337 int tresid, error;
2338 size_t count, lost;
2339 struct dirent *dp;
2340 off_t *cookies = NULL;
2341 int ncookies = 0, nc;
2342
2343 if (vp->v_type != VDIR)
2344 return (EPERM);
2345
2346 lost = uio->uio_resid & (NFS_DIRFRAGSIZ - 1);
2347 count = uio->uio_resid - lost;
2348 if (count <= 0)
2349 return (EINVAL);
2350
2351 /*
2352 * Call nfs_bioread() to do the real work.
2353 */
2354 tresid = uio->uio_resid = count;
2355 error = nfs_bioread(vp, uio, 0, ap->a_cred,
2356 ap->a_cookies ? NFSBIO_CACHECOOKIES : 0);
2357
2358 if (!error && ap->a_cookies) {
2359 ncookies = count / 16;
2360 cookies = malloc(sizeof (off_t) * ncookies, M_TEMP, M_WAITOK);
2361 *ap->a_cookies = cookies;
2362 }
2363
2364 if (!error && uio->uio_resid == tresid) {
2365 uio->uio_resid += lost;
2366 nfsstats.direofcache_misses++;
2367 if (ap->a_cookies)
2368 *ap->a_ncookies = 0;
2369 *ap->a_eofflag = 1;
2370 return (0);
2371 }
2372
2373 if (!error && ap->a_cookies) {
2374 /*
2375 * Only the NFS server and emulations use cookies, and they
2376 * load the directory block into system space, so we can
2377 * just look at it directly.
2378 */
2379 if (!VMSPACE_IS_KERNEL_P(uio->uio_vmspace) ||
2380 uio->uio_iovcnt != 1)
2381 panic("nfs_readdir: lost in space");
2382 for (nc = 0; ncookies-- &&
2383 base < (char *)uio->uio_iov->iov_base; nc++){
2384 dp = (struct dirent *) base;
2385 if (dp->d_reclen == 0)
2386 break;
2387 if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
2388 *(cookies++) = (off_t)NFS_GETCOOKIE32(dp);
2389 else
2390 *(cookies++) = NFS_GETCOOKIE(dp);
2391 base += dp->d_reclen;
2392 }
2393 uio->uio_resid +=
2394 ((char *)uio->uio_iov->iov_base - base);
2395 uio->uio_iov->iov_len +=
2396 ((char *)uio->uio_iov->iov_base - base);
2397 uio->uio_iov->iov_base = base;
2398 *ap->a_ncookies = nc;
2399 }
2400
2401 uio->uio_resid += lost;
2402 *ap->a_eofflag = 0;
2403 return (error);
2404 }
2405
2406 /*
2407 * Readdir rpc call.
2408 * Called from below the buffer cache by nfs_doio().
2409 */
2410 int
2411 nfs_readdirrpc(struct vnode *vp, struct uio *uiop, kauth_cred_t cred)
2412 {
2413 int len, left;
2414 struct dirent *dp = NULL;
2415 u_int32_t *tl;
2416 char *cp;
2417 int32_t t1, t2;
2418 char *bpos, *dpos, *cp2;
2419 struct mbuf *mreq, *mrep, *md, *mb;
2420 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2421 struct nfsnode *dnp = VTONFS(vp);
2422 u_quad_t fileno;
2423 int error = 0, more_dirs = 1, blksiz = 0, bigenough = 1;
2424 #ifndef NFS_V2_ONLY
2425 int attrflag;
2426 #endif
2427 int nrpcs = 0, reclen;
2428 const int v3 = NFS_ISV3(vp);
2429
2430 #ifdef DIAGNOSTIC
2431 /*
2432 * Should be called from buffer cache, so only amount of
2433 * NFS_DIRBLKSIZ will be requested.
2434 */
2435 if (uiop->uio_iovcnt != 1 || uiop->uio_resid != NFS_DIRBLKSIZ)
2436 panic("nfs readdirrpc bad uio");
2437 #endif
2438
2439 /*
2440 * Loop around doing readdir rpc's of size nm_readdirsize
2441 * truncated to a multiple of NFS_DIRFRAGSIZ.
2442 * The stopping criteria is EOF or buffer full.
2443 */
2444 while (more_dirs && bigenough) {
2445 /*
2446 * Heuristic: don't bother to do another RPC to further
2447 * fill up this block if there is not much room left. (< 50%
2448 * of the readdir RPC size). This wastes some buffer space
2449 * but can save up to 50% in RPC calls.
2450 */
2451 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
2452 bigenough = 0;
2453 break;
2454 }
2455 nfsstats.rpccnt[NFSPROC_READDIR]++;
2456 nfsm_reqhead(dnp, NFSPROC_READDIR, NFSX_FH(v3) +
2457 NFSX_READDIR(v3));
2458 nfsm_fhtom(dnp, v3);
2459 #ifndef NFS_V2_ONLY
2460 if (v3) {
2461 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2462 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2463 txdr_swapcookie3(uiop->uio_offset, tl);
2464 } else {
2465 txdr_cookie3(uiop->uio_offset, tl);
2466 }
2467 tl += 2;
2468 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2469 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2470 } else
2471 #endif
2472 {
2473 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2474 *tl++ = txdr_unsigned(uiop->uio_offset);
2475 }
2476 *tl = txdr_unsigned(nmp->nm_readdirsize);
2477 nfsm_request(dnp, NFSPROC_READDIR, curlwp, cred);
2478 nrpcs++;
2479 #ifndef NFS_V2_ONLY
2480 if (v3) {
2481 nfsm_postop_attr(vp, attrflag, 0);
2482 if (!error) {
2483 nfsm_dissect(tl, u_int32_t *,
2484 2 * NFSX_UNSIGNED);
2485 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2486 dnp->n_cookieverf.nfsuquad[1] = *tl;
2487 } else {
2488 m_freem(mrep);
2489 goto nfsmout;
2490 }
2491 }
2492 #endif
2493 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2494 more_dirs = fxdr_unsigned(int, *tl);
2495
2496 /* loop thru the dir entries, doctoring them to 4bsd form */
2497 while (more_dirs && bigenough) {
2498 #ifndef NFS_V2_ONLY
2499 if (v3) {
2500 nfsm_dissect(tl, u_int32_t *,
2501 3 * NFSX_UNSIGNED);
2502 fileno = fxdr_hyper(tl);
2503 len = fxdr_unsigned(int, *(tl + 2));
2504 } else
2505 #endif
2506 {
2507 nfsm_dissect(tl, u_int32_t *,
2508 2 * NFSX_UNSIGNED);
2509 fileno = fxdr_unsigned(u_quad_t, *tl++);
2510 len = fxdr_unsigned(int, *tl);
2511 }
2512 if (len <= 0 || len > NFS_MAXNAMLEN) {
2513 error = EBADRPC;
2514 m_freem(mrep);
2515 goto nfsmout;
2516 }
2517 /* for cookie stashing */
2518 reclen = _DIRENT_RECLEN(dp, len) + 2 * sizeof(off_t);
2519 left = NFS_DIRFRAGSIZ - blksiz;
2520 if (reclen > left) {
2521 memset(uiop->uio_iov->iov_base, 0, left);
2522 dp->d_reclen += left;
2523 UIO_ADVANCE(uiop, left);
2524 blksiz = 0;
2525 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2526 }
2527 if (reclen > uiop->uio_resid)
2528 bigenough = 0;
2529 if (bigenough) {
2530 int tlen;
2531
2532 dp = (struct dirent *)uiop->uio_iov->iov_base;
2533 dp->d_fileno = fileno;
2534 dp->d_namlen = len;
2535 dp->d_reclen = reclen;
2536 dp->d_type = DT_UNKNOWN;
2537 blksiz += reclen;
2538 if (blksiz == NFS_DIRFRAGSIZ)
2539 blksiz = 0;
2540 UIO_ADVANCE(uiop, DIRHDSIZ);
2541 nfsm_mtouio(uiop, len);
2542 tlen = reclen - (DIRHDSIZ + len);
2543 (void)memset(uiop->uio_iov->iov_base, 0, tlen);
2544 UIO_ADVANCE(uiop, tlen);
2545 } else
2546 nfsm_adv(nfsm_rndup(len));
2547 #ifndef NFS_V2_ONLY
2548 if (v3) {
2549 nfsm_dissect(tl, u_int32_t *,
2550 3 * NFSX_UNSIGNED);
2551 } else
2552 #endif
2553 {
2554 nfsm_dissect(tl, u_int32_t *,
2555 2 * NFSX_UNSIGNED);
2556 }
2557 if (bigenough) {
2558 #ifndef NFS_V2_ONLY
2559 if (v3) {
2560 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2561 uiop->uio_offset =
2562 fxdr_swapcookie3(tl);
2563 else
2564 uiop->uio_offset =
2565 fxdr_cookie3(tl);
2566 }
2567 else
2568 #endif
2569 {
2570 uiop->uio_offset =
2571 fxdr_unsigned(off_t, *tl);
2572 }
2573 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2574 }
2575 if (v3)
2576 tl += 2;
2577 else
2578 tl++;
2579 more_dirs = fxdr_unsigned(int, *tl);
2580 }
2581 /*
2582 * If at end of rpc data, get the eof boolean
2583 */
2584 if (!more_dirs) {
2585 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2586 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2587
2588 /*
2589 * kludge: if we got no entries, treat it as EOF.
2590 * some server sometimes send a reply without any
2591 * entries or EOF.
2592 * although it might mean the server has very long name,
2593 * we can't handle such entries anyway.
2594 */
2595
2596 if (uiop->uio_resid >= NFS_DIRBLKSIZ)
2597 more_dirs = 0;
2598 }
2599 m_freem(mrep);
2600 }
2601 /*
2602 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2603 * by increasing d_reclen for the last record.
2604 */
2605 if (blksiz > 0) {
2606 left = NFS_DIRFRAGSIZ - blksiz;
2607 memset(uiop->uio_iov->iov_base, 0, left);
2608 dp->d_reclen += left;
2609 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2610 UIO_ADVANCE(uiop, left);
2611 }
2612
2613 /*
2614 * We are now either at the end of the directory or have filled the
2615 * block.
2616 */
2617 if (bigenough) {
2618 dnp->n_direofoffset = uiop->uio_offset;
2619 dnp->n_flag |= NEOFVALID;
2620 }
2621 nfsmout:
2622 return (error);
2623 }
2624
2625 #ifndef NFS_V2_ONLY
2626 /*
2627 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2628 */
2629 int
2630 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop, kauth_cred_t cred)
2631 {
2632 int len, left;
2633 struct dirent *dp = NULL;
2634 u_int32_t *tl;
2635 char *cp;
2636 int32_t t1, t2;
2637 struct vnode *newvp;
2638 char *bpos, *dpos, *cp2;
2639 struct mbuf *mreq, *mrep, *md, *mb;
2640 struct nameidata nami, *ndp = &nami;
2641 struct componentname *cnp = &ndp->ni_cnd;
2642 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2643 struct nfsnode *dnp = VTONFS(vp), *np;
2644 nfsfh_t *fhp;
2645 u_quad_t fileno;
2646 int error = 0, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2647 int attrflag, fhsize, nrpcs = 0, reclen;
2648 struct nfs_fattr fattr, *fp;
2649
2650 #ifdef DIAGNOSTIC
2651 if (uiop->uio_iovcnt != 1 || uiop->uio_resid != NFS_DIRBLKSIZ)
2652 panic("nfs readdirplusrpc bad uio");
2653 #endif
2654 ndp->ni_dvp = vp;
2655 newvp = NULLVP;
2656
2657 /*
2658 * Loop around doing readdir rpc's of size nm_readdirsize
2659 * truncated to a multiple of NFS_DIRFRAGSIZ.
2660 * The stopping criteria is EOF or buffer full.
2661 */
2662 while (more_dirs && bigenough) {
2663 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
2664 bigenough = 0;
2665 break;
2666 }
2667 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2668 nfsm_reqhead(dnp, NFSPROC_READDIRPLUS,
2669 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2670 nfsm_fhtom(dnp, 1);
2671 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2672 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2673 txdr_swapcookie3(uiop->uio_offset, tl);
2674 } else {
2675 txdr_cookie3(uiop->uio_offset, tl);
2676 }
2677 tl += 2;
2678 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2679 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2680 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2681 *tl = txdr_unsigned(nmp->nm_rsize);
2682 nfsm_request(dnp, NFSPROC_READDIRPLUS, curlwp, cred);
2683 nfsm_postop_attr(vp, attrflag, 0);
2684 if (error) {
2685 m_freem(mrep);
2686 goto nfsmout;
2687 }
2688 nrpcs++;
2689 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2690 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2691 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2692 more_dirs = fxdr_unsigned(int, *tl);
2693
2694 /* loop thru the dir entries, doctoring them to 4bsd form */
2695 while (more_dirs && bigenough) {
2696 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2697 fileno = fxdr_hyper(tl);
2698 len = fxdr_unsigned(int, *(tl + 2));
2699 if (len <= 0 || len > NFS_MAXNAMLEN) {
2700 error = EBADRPC;
2701 m_freem(mrep);
2702 goto nfsmout;
2703 }
2704 /* for cookie stashing */
2705 reclen = _DIRENT_RECLEN(dp, len) + 2 * sizeof(off_t);
2706 left = NFS_DIRFRAGSIZ - blksiz;
2707 if (reclen > left) {
2708 /*
2709 * DIRFRAGSIZ is aligned, no need to align
2710 * again here.
2711 */
2712 memset(uiop->uio_iov->iov_base, 0, left);
2713 dp->d_reclen += left;
2714 UIO_ADVANCE(uiop, left);
2715 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2716 blksiz = 0;
2717 }
2718 if (reclen > uiop->uio_resid)
2719 bigenough = 0;
2720 if (bigenough) {
2721 int tlen;
2722
2723 dp = (struct dirent *)uiop->uio_iov->iov_base;
2724 dp->d_fileno = fileno;
2725 dp->d_namlen = len;
2726 dp->d_reclen = reclen;
2727 dp->d_type = DT_UNKNOWN;
2728 blksiz += reclen;
2729 if (blksiz == NFS_DIRFRAGSIZ)
2730 blksiz = 0;
2731 UIO_ADVANCE(uiop, DIRHDSIZ);
2732 nfsm_mtouio(uiop, len);
2733 tlen = reclen - (DIRHDSIZ + len);
2734 (void)memset(uiop->uio_iov->iov_base, 0, tlen);
2735 UIO_ADVANCE(uiop, tlen);
2736 cnp->cn_nameptr = dp->d_name;
2737 cnp->cn_namelen = dp->d_namlen;
2738 } else
2739 nfsm_adv(nfsm_rndup(len));
2740 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2741 if (bigenough) {
2742 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2743 uiop->uio_offset =
2744 fxdr_swapcookie3(tl);
2745 else
2746 uiop->uio_offset =
2747 fxdr_cookie3(tl);
2748 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2749 }
2750 tl += 2;
2751
2752 /*
2753 * Since the attributes are before the file handle
2754 * (sigh), we must skip over the attributes and then
2755 * come back and get them.
2756 */
2757 attrflag = fxdr_unsigned(int, *tl);
2758 if (attrflag) {
2759 nfsm_dissect(fp, struct nfs_fattr *, NFSX_V3FATTR);
2760 memcpy(&fattr, fp, NFSX_V3FATTR);
2761 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2762 doit = fxdr_unsigned(int, *tl);
2763 if (doit) {
2764 nfsm_getfh(fhp, fhsize, 1);
2765 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2766 vref(vp);
2767 newvp = vp;
2768 np = dnp;
2769 } else {
2770 error = nfs_nget1(vp->v_mount, fhp,
2771 fhsize, &np, LK_NOWAIT);
2772 if (!error)
2773 newvp = NFSTOV(np);
2774 }
2775 if (!error) {
2776 nfs_loadattrcache(&newvp, &fattr, 0, 0);
2777 if (bigenough) {
2778 dp->d_type =
2779 IFTODT(VTTOIF(np->n_vattr->va_type));
2780 if (cnp->cn_namelen <= NCHNAMLEN) {
2781 ndp->ni_vp = newvp;
2782 nfs_cache_enter(ndp->ni_dvp,
2783 ndp->ni_vp, cnp);
2784 }
2785 }
2786 }
2787 error = 0;
2788 }
2789 } else {
2790 /* Just skip over the file handle */
2791 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2792 i = fxdr_unsigned(int, *tl);
2793 nfsm_adv(nfsm_rndup(i));
2794 }
2795 if (newvp != NULLVP) {
2796 if (newvp == vp)
2797 vrele(newvp);
2798 else
2799 vput(newvp);
2800 newvp = NULLVP;
2801 }
2802 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2803 more_dirs = fxdr_unsigned(int, *tl);
2804 }
2805 /*
2806 * If at end of rpc data, get the eof boolean
2807 */
2808 if (!more_dirs) {
2809 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2810 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2811
2812 /*
2813 * kludge: see a comment in nfs_readdirrpc.
2814 */
2815
2816 if (uiop->uio_resid >= NFS_DIRBLKSIZ)
2817 more_dirs = 0;
2818 }
2819 m_freem(mrep);
2820 }
2821 /*
2822 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2823 * by increasing d_reclen for the last record.
2824 */
2825 if (blksiz > 0) {
2826 left = NFS_DIRFRAGSIZ - blksiz;
2827 memset(uiop->uio_iov->iov_base, 0, left);
2828 dp->d_reclen += left;
2829 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2830 UIO_ADVANCE(uiop, left);
2831 }
2832
2833 /*
2834 * We are now either at the end of the directory or have filled the
2835 * block.
2836 */
2837 if (bigenough) {
2838 dnp->n_direofoffset = uiop->uio_offset;
2839 dnp->n_flag |= NEOFVALID;
2840 }
2841 nfsmout:
2842 if (newvp != NULLVP) {
2843 if(newvp == vp)
2844 vrele(newvp);
2845 else
2846 vput(newvp);
2847 }
2848 return (error);
2849 }
2850 #endif
2851
2852 /*
2853 * Silly rename. To make the NFS filesystem that is stateless look a little
2854 * more like the "ufs" a remove of an active vnode is translated to a rename
2855 * to a funny looking filename that is removed by nfs_inactive on the
2856 * nfsnode. There is the potential for another process on a different client
2857 * to create the same funny name between the nfs_lookitup() fails and the
2858 * nfs_rename() completes, but...
2859 */
2860 int
2861 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp, bool dolink)
2862 {
2863 struct sillyrename *sp;
2864 struct nfsnode *np;
2865 int error;
2866 pid_t pid;
2867
2868 cache_purge(dvp);
2869 np = VTONFS(vp);
2870 #ifndef DIAGNOSTIC
2871 if (vp->v_type == VDIR)
2872 panic("nfs: sillyrename dir");
2873 #endif
2874 sp = kmem_alloc(sizeof(*sp), KM_SLEEP);
2875 sp->s_cred = kauth_cred_dup(cnp->cn_cred);
2876 sp->s_dvp = dvp;
2877 vref(dvp);
2878
2879 /* Fudge together a funny name */
2880 pid = curlwp->l_proc->p_pid;
2881 memcpy(sp->s_name, ".nfsAxxxx4.4", 13);
2882 sp->s_namlen = 12;
2883 sp->s_name[8] = hexdigits[pid & 0xf];
2884 sp->s_name[7] = hexdigits[(pid >> 4) & 0xf];
2885 sp->s_name[6] = hexdigits[(pid >> 8) & 0xf];
2886 sp->s_name[5] = hexdigits[(pid >> 12) & 0xf];
2887
2888 /* Try lookitups until we get one that isn't there */
2889 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2890 curlwp, (struct nfsnode **)0) == 0) {
2891 sp->s_name[4]++;
2892 if (sp->s_name[4] > 'z') {
2893 error = EINVAL;
2894 goto bad;
2895 }
2896 }
2897 if (dolink) {
2898 error = nfs_linkrpc(dvp, vp, sp->s_name, sp->s_namlen,
2899 sp->s_cred, curlwp);
2900 /*
2901 * nfs_request maps NFSERR_NOTSUPP to ENOTSUP.
2902 */
2903 if (error == ENOTSUP) {
2904 error = nfs_renameit(dvp, cnp, sp);
2905 }
2906 } else {
2907 error = nfs_renameit(dvp, cnp, sp);
2908 }
2909 if (error)
2910 goto bad;
2911 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2912 curlwp, &np);
2913 np->n_sillyrename = sp;
2914 return (0);
2915 bad:
2916 vrele(sp->s_dvp);
2917 kauth_cred_free(sp->s_cred);
2918 kmem_free(sp, sizeof(*sp));
2919 return (error);
2920 }
2921
2922 /*
2923 * Look up a file name and optionally either update the file handle or
2924 * allocate an nfsnode, depending on the value of npp.
2925 * npp == NULL --> just do the lookup
2926 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2927 * handled too
2928 * *npp != NULL --> update the file handle in the vnode
2929 */
2930 int
2931 nfs_lookitup(struct vnode *dvp, const char *name, int len, kauth_cred_t cred, struct lwp *l, struct nfsnode **npp)
2932 {
2933 u_int32_t *tl;
2934 char *cp;
2935 int32_t t1, t2;
2936 struct vnode *newvp = (struct vnode *)0;
2937 struct nfsnode *np, *dnp = VTONFS(dvp);
2938 char *bpos, *dpos, *cp2;
2939 int error = 0, fhlen;
2940 #ifndef NFS_V2_ONLY
2941 int attrflag;
2942 #endif
2943 struct mbuf *mreq, *mrep, *md, *mb;
2944 nfsfh_t *nfhp;
2945 const int v3 = NFS_ISV3(dvp);
2946
2947 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2948 nfsm_reqhead(dnp, NFSPROC_LOOKUP,
2949 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2950 nfsm_fhtom(dnp, v3);
2951 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2952 nfsm_request(dnp, NFSPROC_LOOKUP, l, cred);
2953 if (npp && !error) {
2954 nfsm_getfh(nfhp, fhlen, v3);
2955 if (*npp) {
2956 np = *npp;
2957 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2958 kmem_free(np->n_fhp, np->n_fhsize);
2959 np->n_fhp = &np->n_fh;
2960 }
2961 #if NFS_SMALLFH < NFSX_V3FHMAX
2962 else if (np->n_fhsize <= NFS_SMALLFH && fhlen > NFS_SMALLFH)
2963 np->n_fhp = kmem_alloc(fhlen, KM_SLEEP);
2964 #endif
2965 memcpy(np->n_fhp, nfhp, fhlen);
2966 np->n_fhsize = fhlen;
2967 newvp = NFSTOV(np);
2968 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2969 vref(dvp);
2970 newvp = dvp;
2971 np = dnp;
2972 } else {
2973 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2974 if (error) {
2975 m_freem(mrep);
2976 return (error);
2977 }
2978 newvp = NFSTOV(np);
2979 }
2980 #ifndef NFS_V2_ONLY
2981 if (v3) {
2982 nfsm_postop_attr(newvp, attrflag, 0);
2983 if (!attrflag && *npp == NULL) {
2984 m_freem(mrep);
2985 vput(newvp);
2986 return (ENOENT);
2987 }
2988 } else
2989 #endif
2990 nfsm_loadattr(newvp, (struct vattr *)0, 0);
2991 }
2992 nfsm_reqdone;
2993 if (npp && *npp == NULL) {
2994 if (error) {
2995 if (newvp)
2996 vput(newvp);
2997 } else
2998 *npp = np;
2999 }
3000 return (error);
3001 }
3002
3003 #ifndef NFS_V2_ONLY
3004 /*
3005 * Nfs Version 3 commit rpc
3006 */
3007 int
3008 nfs_commit(struct vnode *vp, off_t offset, uint32_t cnt, struct lwp *l)
3009 {
3010 char *cp;
3011 u_int32_t *tl;
3012 int32_t t1, t2;
3013 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3014 char *bpos, *dpos, *cp2;
3015 int error = 0, wccflag = NFSV3_WCCRATTR;
3016 struct mbuf *mreq, *mrep, *md, *mb;
3017 struct nfsnode *np;
3018
3019 KASSERT(NFS_ISV3(vp));
3020
3021 #ifdef NFS_DEBUG_COMMIT
3022 printf("commit %lu - %lu\n", (unsigned long)offset,
3023 (unsigned long)(offset + cnt));
3024 #endif
3025
3026 mutex_enter(&nmp->nm_lock);
3027 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0) {
3028 mutex_exit(&nmp->nm_lock);
3029 return (0);
3030 }
3031 mutex_exit(&nmp->nm_lock);
3032 nfsstats.rpccnt[NFSPROC_COMMIT]++;
3033 np = VTONFS(vp);
3034 nfsm_reqhead(np, NFSPROC_COMMIT, NFSX_FH(1));
3035 nfsm_fhtom(np, 1);
3036 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3037 txdr_hyper(offset, tl);
3038 tl += 2;
3039 *tl = txdr_unsigned(cnt);
3040 nfsm_request(np, NFSPROC_COMMIT, l, np->n_wcred);
3041 nfsm_wcc_data(vp, wccflag, NAC_NOTRUNC, false);
3042 if (!error) {
3043 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
3044 mutex_enter(&nmp->nm_lock);
3045 if ((nmp->nm_iflag & NFSMNT_STALEWRITEVERF) ||
3046 memcmp(nmp->nm_writeverf, tl, NFSX_V3WRITEVERF)) {
3047 memcpy(nmp->nm_writeverf, tl, NFSX_V3WRITEVERF);
3048 error = NFSERR_STALEWRITEVERF;
3049 nmp->nm_iflag |= NFSMNT_STALEWRITEVERF;
3050 }
3051 mutex_exit(&nmp->nm_lock);
3052 }
3053 nfsm_reqdone;
3054 return (error);
3055 }
3056 #endif
3057
3058 /*
3059 * Kludge City..
3060 * - make nfs_bmap() essentially a no-op that does no translation
3061 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
3062 * (Maybe I could use the process's page mapping, but I was concerned that
3063 * Kernel Write might not be enabled and also figured copyout() would do
3064 * a lot more work than memcpy() and also it currently happens in the
3065 * context of the swapper process (2).
3066 */
3067 int
3068 nfs_bmap(void *v)
3069 {
3070 struct vop_bmap_args /* {
3071 struct vnode *a_vp;
3072 daddr_t a_bn;
3073 struct vnode **a_vpp;
3074 daddr_t *a_bnp;
3075 int *a_runp;
3076 } */ *ap = v;
3077 struct vnode *vp = ap->a_vp;
3078 int bshift = vp->v_mount->mnt_fs_bshift - vp->v_mount->mnt_dev_bshift;
3079
3080 if (ap->a_vpp != NULL)
3081 *ap->a_vpp = vp;
3082 if (ap->a_bnp != NULL)
3083 *ap->a_bnp = ap->a_bn << bshift;
3084 if (ap->a_runp != NULL)
3085 *ap->a_runp = 1024 * 1024; /* XXX */
3086 return (0);
3087 }
3088
3089 /*
3090 * Strategy routine.
3091 * For async requests when nfsiod(s) are running, queue the request by
3092 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
3093 * request.
3094 */
3095 int
3096 nfs_strategy(void *v)
3097 {
3098 struct vop_strategy_args *ap = v;
3099 struct buf *bp = ap->a_bp;
3100 int error = 0;
3101
3102 if ((bp->b_flags & (B_PHYS|B_ASYNC)) == (B_PHYS|B_ASYNC))
3103 panic("nfs physio/async");
3104
3105 /*
3106 * If the op is asynchronous and an i/o daemon is waiting
3107 * queue the request, wake it up and wait for completion
3108 * otherwise just do it ourselves.
3109 */
3110 if ((bp->b_flags & B_ASYNC) == 0 || nfs_asyncio(bp))
3111 error = nfs_doio(bp);
3112 return (error);
3113 }
3114
3115 /*
3116 * fsync vnode op. Just call nfs_flush() with commit == 1.
3117 */
3118 /* ARGSUSED */
3119 int
3120 nfs_fsync(void *v)
3121 {
3122 struct vop_fsync_args /* {
3123 struct vnodeop_desc *a_desc;
3124 struct vnode * a_vp;
3125 kauth_cred_t a_cred;
3126 int a_flags;
3127 off_t offlo;
3128 off_t offhi;
3129 struct lwp * a_l;
3130 } */ *ap = v;
3131
3132 struct vnode *vp = ap->a_vp;
3133
3134 if (vp->v_type != VREG)
3135 return 0;
3136
3137 return (nfs_flush(vp, ap->a_cred,
3138 (ap->a_flags & FSYNC_WAIT) != 0 ? MNT_WAIT : 0, curlwp, 1));
3139 }
3140
3141 /*
3142 * Flush all the data associated with a vnode.
3143 */
3144 int
3145 nfs_flush(struct vnode *vp, kauth_cred_t cred, int waitfor, struct lwp *l,
3146 int commit)
3147 {
3148 struct nfsnode *np = VTONFS(vp);
3149 int error;
3150 int flushflags = PGO_ALLPAGES|PGO_CLEANIT|PGO_SYNCIO;
3151 UVMHIST_FUNC("nfs_flush"); UVMHIST_CALLED(ubchist);
3152
3153 mutex_enter(vp->v_interlock);
3154 error = VOP_PUTPAGES(vp, 0, 0, flushflags);
3155 if (np->n_flag & NWRITEERR) {
3156 error = np->n_error;
3157 np->n_flag &= ~NWRITEERR;
3158 }
3159 UVMHIST_LOG(ubchist, "returning %d", error,0,0,0);
3160 return (error);
3161 }
3162
3163 /*
3164 * Return POSIX pathconf information applicable to nfs.
3165 *
3166 * N.B. The NFS V2 protocol doesn't support this RPC.
3167 */
3168 /* ARGSUSED */
3169 int
3170 nfs_pathconf(void *v)
3171 {
3172 struct vop_pathconf_args /* {
3173 struct vnode *a_vp;
3174 int a_name;
3175 register_t *a_retval;
3176 } */ *ap = v;
3177 struct nfsv3_pathconf *pcp;
3178 struct vnode *vp = ap->a_vp;
3179 struct mbuf *mreq, *mrep, *md, *mb;
3180 int32_t t1, t2;
3181 u_int32_t *tl;
3182 char *bpos, *dpos, *cp, *cp2;
3183 int error = 0, attrflag;
3184 #ifndef NFS_V2_ONLY
3185 struct nfsmount *nmp;
3186 unsigned int l;
3187 u_int64_t maxsize;
3188 #endif
3189 const int v3 = NFS_ISV3(vp);
3190 struct nfsnode *np = VTONFS(vp);
3191
3192 switch (ap->a_name) {
3193 /* Names that can be resolved locally. */
3194 case _PC_PIPE_BUF:
3195 *ap->a_retval = PIPE_BUF;
3196 break;
3197 case _PC_SYNC_IO:
3198 *ap->a_retval = 1;
3199 break;
3200 /* Names that cannot be resolved locally; do an RPC, if possible. */
3201 case _PC_LINK_MAX:
3202 case _PC_NAME_MAX:
3203 case _PC_CHOWN_RESTRICTED:
3204 case _PC_NO_TRUNC:
3205 if (!v3) {
3206 error = EINVAL;
3207 break;
3208 }
3209 nfsstats.rpccnt[NFSPROC_PATHCONF]++;
3210 nfsm_reqhead(np, NFSPROC_PATHCONF, NFSX_FH(1));
3211 nfsm_fhtom(np, 1);
3212 nfsm_request(np, NFSPROC_PATHCONF,
3213 curlwp, curlwp->l_cred); /* XXX */
3214 nfsm_postop_attr(vp, attrflag, 0);
3215 if (!error) {
3216 nfsm_dissect(pcp, struct nfsv3_pathconf *,
3217 NFSX_V3PATHCONF);
3218 switch (ap->a_name) {
3219 case _PC_LINK_MAX:
3220 *ap->a_retval =
3221 fxdr_unsigned(register_t, pcp->pc_linkmax);
3222 break;
3223 case _PC_NAME_MAX:
3224 *ap->a_retval =
3225 fxdr_unsigned(register_t, pcp->pc_namemax);
3226 break;
3227 case _PC_CHOWN_RESTRICTED:
3228 *ap->a_retval =
3229 (pcp->pc_chownrestricted == nfs_true);
3230 break;
3231 case _PC_NO_TRUNC:
3232 *ap->a_retval =
3233 (pcp->pc_notrunc == nfs_true);
3234 break;
3235 }
3236 }
3237 nfsm_reqdone;
3238 break;
3239 case _PC_FILESIZEBITS:
3240 #ifndef NFS_V2_ONLY
3241 if (v3) {
3242 nmp = VFSTONFS(vp->v_mount);
3243 if ((nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
3244 if ((error = nfs_fsinfo(nmp, vp,
3245 curlwp->l_cred, curlwp)) != 0) /* XXX */
3246 break;
3247 for (l = 0, maxsize = nmp->nm_maxfilesize;
3248 (maxsize >> l) > 0; l++)
3249 ;
3250 *ap->a_retval = l + 1;
3251 } else
3252 #endif
3253 {
3254 *ap->a_retval = 32; /* NFS V2 limitation */
3255 }
3256 break;
3257 default:
3258 error = EINVAL;
3259 break;
3260 }
3261
3262 return (error);
3263 }
3264
3265 /*
3266 * NFS advisory byte-level locks.
3267 */
3268 int
3269 nfs_advlock(void *v)
3270 {
3271 struct vop_advlock_args /* {
3272 struct vnode *a_vp;
3273 void *a_id;
3274 int a_op;
3275 struct flock *a_fl;
3276 int a_flags;
3277 } */ *ap = v;
3278 struct nfsnode *np = VTONFS(ap->a_vp);
3279
3280 return lf_advlock(ap, &np->n_lockf, np->n_size);
3281 }
3282
3283 /*
3284 * Print out the contents of an nfsnode.
3285 */
3286 int
3287 nfs_print(void *v)
3288 {
3289 struct vop_print_args /* {
3290 struct vnode *a_vp;
3291 } */ *ap = v;
3292 struct vnode *vp = ap->a_vp;
3293 struct nfsnode *np = VTONFS(vp);
3294
3295 printf("tag VT_NFS, fileid %lld fsid 0x%llx",
3296 (unsigned long long)np->n_vattr->va_fileid,
3297 (unsigned long long)np->n_vattr->va_fsid);
3298 if (vp->v_type == VFIFO)
3299 VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
3300 printf("\n");
3301 return (0);
3302 }
3303
3304 /*
3305 * nfs unlock wrapper.
3306 */
3307 int
3308 nfs_unlock(void *v)
3309 {
3310 struct vop_unlock_args /* {
3311 struct vnode *a_vp;
3312 int a_flags;
3313 } */ *ap = v;
3314 struct vnode *vp = ap->a_vp;
3315
3316 /*
3317 * VOP_UNLOCK can be called by nfs_loadattrcache
3318 * with v_data == 0.
3319 */
3320 if (VTONFS(vp)) {
3321 nfs_delayedtruncate(vp);
3322 }
3323
3324 return genfs_unlock(v);
3325 }
3326
3327 /*
3328 * nfs special file access vnode op.
3329 * Essentially just get vattr and then imitate iaccess() since the device is
3330 * local to the client.
3331 */
3332 int
3333 nfsspec_access(void *v)
3334 {
3335 struct vop_access_args /* {
3336 struct vnode *a_vp;
3337 int a_mode;
3338 kauth_cred_t a_cred;
3339 struct lwp *a_l;
3340 } */ *ap = v;
3341 struct vattr va;
3342 struct vnode *vp = ap->a_vp;
3343 int error;
3344
3345 error = VOP_GETATTR(vp, &va, ap->a_cred);
3346 if (error)
3347 return (error);
3348
3349 /*
3350 * Disallow write attempts on filesystems mounted read-only;
3351 * unless the file is a socket, fifo, or a block or character
3352 * device resident on the filesystem.
3353 */
3354 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3355 switch (vp->v_type) {
3356 case VREG:
3357 case VDIR:
3358 case VLNK:
3359 return (EROFS);
3360 default:
3361 break;
3362 }
3363 }
3364
3365 return kauth_authorize_vnode(ap->a_cred, kauth_access_action(ap->a_mode,
3366 va.va_type, va.va_mode), vp, NULL, genfs_can_access(va.va_type,
3367 va.va_mode, va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
3368 }
3369
3370 /*
3371 * Read wrapper for special devices.
3372 */
3373 int
3374 nfsspec_read(void *v)
3375 {
3376 struct vop_read_args /* {
3377 struct vnode *a_vp;
3378 struct uio *a_uio;
3379 int a_ioflag;
3380 kauth_cred_t a_cred;
3381 } */ *ap = v;
3382 struct nfsnode *np = VTONFS(ap->a_vp);
3383
3384 /*
3385 * Set access flag.
3386 */
3387 np->n_flag |= NACC;
3388 getnanotime(&np->n_atim);
3389 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3390 }
3391
3392 /*
3393 * Write wrapper for special devices.
3394 */
3395 int
3396 nfsspec_write(void *v)
3397 {
3398 struct vop_write_args /* {
3399 struct vnode *a_vp;
3400 struct uio *a_uio;
3401 int a_ioflag;
3402 kauth_cred_t a_cred;
3403 } */ *ap = v;
3404 struct nfsnode *np = VTONFS(ap->a_vp);
3405
3406 /*
3407 * Set update flag.
3408 */
3409 np->n_flag |= NUPD;
3410 getnanotime(&np->n_mtim);
3411 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3412 }
3413
3414 /*
3415 * Close wrapper for special devices.
3416 *
3417 * Update the times on the nfsnode then do device close.
3418 */
3419 int
3420 nfsspec_close(void *v)
3421 {
3422 struct vop_close_args /* {
3423 struct vnode *a_vp;
3424 int a_fflag;
3425 kauth_cred_t a_cred;
3426 struct lwp *a_l;
3427 } */ *ap = v;
3428 struct vnode *vp = ap->a_vp;
3429 struct nfsnode *np = VTONFS(vp);
3430 struct vattr vattr;
3431
3432 if (np->n_flag & (NACC | NUPD)) {
3433 np->n_flag |= NCHG;
3434 if (vp->v_usecount == 1 &&
3435 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3436 vattr_null(&vattr);
3437 if (np->n_flag & NACC)
3438 vattr.va_atime = np->n_atim;
3439 if (np->n_flag & NUPD)
3440 vattr.va_mtime = np->n_mtim;
3441 (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3442 }
3443 }
3444 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3445 }
3446
3447 /*
3448 * Read wrapper for fifos.
3449 */
3450 int
3451 nfsfifo_read(void *v)
3452 {
3453 struct vop_read_args /* {
3454 struct vnode *a_vp;
3455 struct uio *a_uio;
3456 int a_ioflag;
3457 kauth_cred_t a_cred;
3458 } */ *ap = v;
3459 struct nfsnode *np = VTONFS(ap->a_vp);
3460
3461 /*
3462 * Set access flag.
3463 */
3464 np->n_flag |= NACC;
3465 getnanotime(&np->n_atim);
3466 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3467 }
3468
3469 /*
3470 * Write wrapper for fifos.
3471 */
3472 int
3473 nfsfifo_write(void *v)
3474 {
3475 struct vop_write_args /* {
3476 struct vnode *a_vp;
3477 struct uio *a_uio;
3478 int a_ioflag;
3479 kauth_cred_t a_cred;
3480 } */ *ap = v;
3481 struct nfsnode *np = VTONFS(ap->a_vp);
3482
3483 /*
3484 * Set update flag.
3485 */
3486 np->n_flag |= NUPD;
3487 getnanotime(&np->n_mtim);
3488 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3489 }
3490
3491 /*
3492 * Close wrapper for fifos.
3493 *
3494 * Update the times on the nfsnode then do fifo close.
3495 */
3496 int
3497 nfsfifo_close(void *v)
3498 {
3499 struct vop_close_args /* {
3500 struct vnode *a_vp;
3501 int a_fflag;
3502 kauth_cred_t a_cred;
3503 struct lwp *a_l;
3504 } */ *ap = v;
3505 struct vnode *vp = ap->a_vp;
3506 struct nfsnode *np = VTONFS(vp);
3507 struct vattr vattr;
3508
3509 if (np->n_flag & (NACC | NUPD)) {
3510 struct timespec ts;
3511
3512 getnanotime(&ts);
3513 if (np->n_flag & NACC)
3514 np->n_atim = ts;
3515 if (np->n_flag & NUPD)
3516 np->n_mtim = ts;
3517 np->n_flag |= NCHG;
3518 if (vp->v_usecount == 1 &&
3519 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3520 vattr_null(&vattr);
3521 if (np->n_flag & NACC)
3522 vattr.va_atime = np->n_atim;
3523 if (np->n_flag & NUPD)
3524 vattr.va_mtime = np->n_mtim;
3525 (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3526 }
3527 }
3528 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
3529 }
3530