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