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