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