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