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