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