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