nfs_vnops.c revision 1.199 1 /* $NetBSD: nfs_vnops.c,v 1.199 2004/05/17 12:17:18 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.199 2004/05/17 12:17:18 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 /*
847 * RFC1813(nfsv3) 3.2 says clients should handle "." by themselves.
848 */
849 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
850 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_proc);
851 if (error)
852 return error;
853 if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN))
854 return EISDIR;
855 VREF(dvp);
856 *vpp = dvp;
857 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
858 cnp->cn_flags |= SAVENAME;
859 return 0;
860 }
861
862 lockparent = flags & LOCKPARENT;
863 wantparent = flags & (LOCKPARENT|WANTPARENT);
864 np = VTONFS(dvp);
865
866 /*
867 * Before tediously performing a linear scan of the directory,
868 * check the name cache to see if the directory/name pair
869 * we are looking for is known already.
870 * If the directory/name pair is found in the name cache,
871 * we have to ensure the directory has not changed from
872 * the time the cache entry has been created. If it has,
873 * the cache entry has to be ignored.
874 */
875 if ((error = cache_lookup(dvp, vpp, cnp)) >= 0) {
876 struct vattr vattr;
877 int err2;
878
879 if (error && error != ENOENT) {
880 *vpp = NULLVP;
881 return error;
882 }
883
884 if (cnp->cn_flags & PDIRUNLOCK) {
885 err2 = vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
886 if (err2 != 0) {
887 *vpp = NULLVP;
888 return err2;
889 }
890 cnp->cn_flags &= ~PDIRUNLOCK;
891 }
892
893 err2 = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_proc);
894 if (err2 != 0) {
895 if (error == 0) {
896 if (*vpp != dvp)
897 vput(*vpp);
898 else
899 vrele(*vpp);
900 }
901 *vpp = NULLVP;
902 return err2;
903 }
904
905 if (error == ENOENT) {
906 if (!VOP_GETATTR(dvp, &vattr, cnp->cn_cred,
907 cnp->cn_proc) && timespeccmp(&vattr.va_mtime,
908 &VTONFS(dvp)->n_nctime, ==))
909 return ENOENT;
910 cache_purge1(dvp, NULL, PURGE_CHILDREN);
911 timespecclear(&np->n_nctime);
912 goto dorpc;
913 }
914
915 newvp = *vpp;
916 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, cnp->cn_proc)
917 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime)
918 {
919 nfsstats.lookupcache_hits++;
920 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
921 cnp->cn_flags |= SAVENAME;
922 if ((!lockparent || !(flags & ISLASTCN)) &&
923 newvp != dvp)
924 VOP_UNLOCK(dvp, 0);
925 KASSERT(newvp->v_type != VNON);
926 return (0);
927 }
928 cache_purge1(newvp, NULL, PURGE_PARENTS);
929 if (newvp != dvp)
930 vput(newvp);
931 else
932 vrele(newvp);
933 *vpp = NULLVP;
934 }
935 dorpc:
936 #if 0
937 /*
938 * because nfsv3 has the same CREATE semantics as ours,
939 * we don't have to perform LOOKUPs beforehand.
940 *
941 * XXX ideally we can do the same for nfsv2 in the case of !O_EXCL.
942 * XXX although we have no way to know if O_EXCL is requested or not.
943 */
944
945 if (v3 && cnp->cn_nameiop == CREATE &&
946 (flags & (ISLASTCN|ISDOTDOT)) == ISLASTCN &&
947 (dvp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
948 KASSERT(lockparent);
949 cnp->cn_flags |= SAVENAME;
950 return (EJUSTRETURN);
951 }
952 #endif /* 0 */
953
954 error = 0;
955 newvp = NULLVP;
956 nfsstats.lookupcache_misses++;
957 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
958 len = cnp->cn_namelen;
959 nfsm_reqhead(np, NFSPROC_LOOKUP,
960 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
961 nfsm_fhtom(np, v3);
962 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
963 nfsm_request(np, NFSPROC_LOOKUP, cnp->cn_proc, cnp->cn_cred);
964 if (error) {
965 nfsm_postop_attr(dvp, attrflag, 0);
966 m_freem(mrep);
967 goto nfsmout;
968 }
969 nfsm_getfh(fhp, fhsize, v3);
970
971 /*
972 * Handle RENAME case...
973 */
974 if (cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
975 if (NFS_CMPFH(np, fhp, fhsize)) {
976 m_freem(mrep);
977 return (EISDIR);
978 }
979 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
980 if (error) {
981 m_freem(mrep);
982 return error;
983 }
984 newvp = NFSTOV(np);
985 if (v3) {
986 nfsm_postop_attr(newvp, attrflag, 0);
987 nfsm_postop_attr(dvp, attrflag, 0);
988 } else
989 nfsm_loadattr(newvp, (struct vattr *)0, 0);
990 *vpp = newvp;
991 m_freem(mrep);
992 cnp->cn_flags |= SAVENAME;
993 if (!lockparent) {
994 VOP_UNLOCK(dvp, 0);
995 cnp->cn_flags |= PDIRUNLOCK;
996 }
997 goto validate;
998 }
999
1000 /*
1001 * The postop attr handling is duplicated for each if case,
1002 * because it should be done while dvp is locked (unlocking
1003 * dvp is different for each case).
1004 */
1005
1006 if (NFS_CMPFH(np, fhp, fhsize)) {
1007 /*
1008 * "." lookup
1009 */
1010 VREF(dvp);
1011 newvp = dvp;
1012 if (v3) {
1013 nfsm_postop_attr(newvp, attrflag, 0);
1014 nfsm_postop_attr(dvp, attrflag, 0);
1015 } else
1016 nfsm_loadattr(newvp, (struct vattr *)0, 0);
1017 } else if (flags & ISDOTDOT) {
1018 /*
1019 * ".." lookup
1020 */
1021 VOP_UNLOCK(dvp, 0);
1022 cnp->cn_flags |= PDIRUNLOCK;
1023
1024 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1025 if (error) {
1026 if (vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY) == 0)
1027 cnp->cn_flags &= ~PDIRUNLOCK;
1028 m_freem(mrep);
1029 return error;
1030 }
1031 newvp = NFSTOV(np);
1032
1033 if (v3) {
1034 nfsm_postop_attr(newvp, attrflag, 0);
1035 nfsm_postop_attr(dvp, attrflag, 0);
1036 } else
1037 nfsm_loadattr(newvp, (struct vattr *)0, 0);
1038
1039 if (lockparent && (flags & ISLASTCN)) {
1040 if ((error = vn_lock(dvp, LK_EXCLUSIVE))) {
1041 m_freem(mrep);
1042 vput(newvp);
1043 return error;
1044 }
1045 cnp->cn_flags &= ~PDIRUNLOCK;
1046 }
1047 } else {
1048 /*
1049 * Other lookups.
1050 */
1051 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1052 if (error) {
1053 m_freem(mrep);
1054 return error;
1055 }
1056 newvp = NFSTOV(np);
1057 if (v3) {
1058 nfsm_postop_attr(newvp, attrflag, 0);
1059 nfsm_postop_attr(dvp, attrflag, 0);
1060 } else
1061 nfsm_loadattr(newvp, (struct vattr *)0, 0);
1062 if (!lockparent || !(flags & ISLASTCN)) {
1063 VOP_UNLOCK(dvp, 0);
1064 cnp->cn_flags |= PDIRUNLOCK;
1065 }
1066 }
1067 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1068 cnp->cn_flags |= SAVENAME;
1069 if ((cnp->cn_flags & MAKEENTRY) &&
1070 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
1071 nfs_cache_enter(dvp, newvp, cnp);
1072 }
1073 *vpp = newvp;
1074 nfsm_reqdone;
1075 if (error) {
1076 /*
1077 * We get here only because of errors returned by
1078 * the RPC. Otherwise we'll have returned above
1079 * (the nfsm_* macros will jump to nfsm_reqdone
1080 * on error).
1081 */
1082 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) &&
1083 cnp->cn_nameiop != CREATE) {
1084 nfs_cache_enter(dvp, NULL, cnp);
1085 }
1086 if (newvp != NULLVP) {
1087 vrele(newvp);
1088 if (newvp != dvp)
1089 VOP_UNLOCK(newvp, 0);
1090 }
1091 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1092 (flags & ISLASTCN) && error == ENOENT) {
1093 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1094 error = EROFS;
1095 else
1096 error = EJUSTRETURN;
1097 }
1098 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1099 cnp->cn_flags |= SAVENAME;
1100 *vpp = NULL;
1101 return error;
1102 }
1103
1104 validate:
1105 /*
1106 * make sure we have valid type and size.
1107 */
1108
1109 newvp = *vpp;
1110 if (newvp->v_type == VNON) {
1111 struct vattr vattr; /* dummy */
1112
1113 KASSERT(VTONFS(newvp)->n_attrstamp == 0);
1114 error = VOP_GETATTR(newvp, &vattr, cnp->cn_cred, cnp->cn_proc);
1115 if (error) {
1116 vput(newvp);
1117 *vpp = NULL;
1118 }
1119 }
1120
1121 return error;
1122 }
1123
1124 /*
1125 * nfs read call.
1126 * Just call nfs_bioread() to do the work.
1127 */
1128 int
1129 nfs_read(v)
1130 void *v;
1131 {
1132 struct vop_read_args /* {
1133 struct vnode *a_vp;
1134 struct uio *a_uio;
1135 int a_ioflag;
1136 struct ucred *a_cred;
1137 } */ *ap = v;
1138 struct vnode *vp = ap->a_vp;
1139
1140 if (vp->v_type != VREG)
1141 return (EPERM);
1142 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred, 0));
1143 }
1144
1145 /*
1146 * nfs readlink call
1147 */
1148 int
1149 nfs_readlink(v)
1150 void *v;
1151 {
1152 struct vop_readlink_args /* {
1153 struct vnode *a_vp;
1154 struct uio *a_uio;
1155 struct ucred *a_cred;
1156 } */ *ap = v;
1157 struct vnode *vp = ap->a_vp;
1158
1159 if (vp->v_type != VLNK)
1160 return (EPERM);
1161 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred, 0));
1162 }
1163
1164 /*
1165 * Do a readlink rpc.
1166 * Called by nfs_doio() from below the buffer cache.
1167 */
1168 int
1169 nfs_readlinkrpc(vp, uiop, cred)
1170 struct vnode *vp;
1171 struct uio *uiop;
1172 struct ucred *cred;
1173 {
1174 u_int32_t *tl;
1175 caddr_t cp;
1176 int32_t t1, t2;
1177 caddr_t bpos, dpos, cp2;
1178 int error = 0, attrflag;
1179 uint32_t len;
1180 struct mbuf *mreq, *mrep, *md, *mb;
1181 const int v3 = NFS_ISV3(vp);
1182 struct nfsnode *np = VTONFS(vp);
1183
1184 nfsstats.rpccnt[NFSPROC_READLINK]++;
1185 nfsm_reqhead(np, NFSPROC_READLINK, NFSX_FH(v3));
1186 nfsm_fhtom(np, v3);
1187 nfsm_request(np, NFSPROC_READLINK, uiop->uio_procp, cred);
1188 if (v3)
1189 nfsm_postop_attr(vp, attrflag, 0);
1190 if (!error) {
1191 if (v3) {
1192 nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
1193 len = fxdr_unsigned(uint32_t, *tl);
1194 if (len > MAXPATHLEN) {
1195 /*
1196 * this pathname is too long for us.
1197 */
1198 m_freem(mrep);
1199 /* Solaris returns EINVAL. should we follow? */
1200 error = ENAMETOOLONG;
1201 goto nfsmout;
1202 }
1203 } else {
1204 nfsm_strsiz(len, NFS_MAXPATHLEN);
1205 }
1206 nfsm_mtouio(uiop, len);
1207 }
1208 nfsm_reqdone;
1209 return (error);
1210 }
1211
1212 /*
1213 * nfs read rpc call
1214 * Ditto above
1215 */
1216 int
1217 nfs_readrpc(vp, uiop)
1218 struct vnode *vp;
1219 struct uio *uiop;
1220 {
1221 u_int32_t *tl;
1222 caddr_t cp;
1223 int32_t t1, t2;
1224 caddr_t bpos, dpos, cp2;
1225 struct mbuf *mreq, *mrep, *md, *mb;
1226 struct nfsmount *nmp;
1227 int error = 0, len, retlen, tsiz, eof, attrflag;
1228 const int v3 = NFS_ISV3(vp);
1229 struct nfsnode *np = VTONFS(vp);
1230
1231 #ifndef nolint
1232 eof = 0;
1233 #endif
1234 nmp = VFSTONFS(vp->v_mount);
1235 tsiz = uiop->uio_resid;
1236 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1237 return (EFBIG);
1238 while (tsiz > 0) {
1239 nfsstats.rpccnt[NFSPROC_READ]++;
1240 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1241 nfsm_reqhead(np, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1242 nfsm_fhtom(np, v3);
1243 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1244 if (v3) {
1245 txdr_hyper(uiop->uio_offset, tl);
1246 *(tl + 2) = txdr_unsigned(len);
1247 } else {
1248 *tl++ = txdr_unsigned(uiop->uio_offset);
1249 *tl++ = txdr_unsigned(len);
1250 *tl = 0;
1251 }
1252 nfsm_request(np, NFSPROC_READ, uiop->uio_procp, np->n_rcred);
1253 if (v3) {
1254 nfsm_postop_attr(vp, attrflag, NAC_NOTRUNC);
1255 if (error) {
1256 m_freem(mrep);
1257 goto nfsmout;
1258 }
1259 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1260 eof = fxdr_unsigned(int, *(tl + 1));
1261 } else
1262 nfsm_loadattr(vp, (struct vattr *)0, NAC_NOTRUNC);
1263 nfsm_strsiz(retlen, nmp->nm_rsize);
1264 nfsm_mtouio(uiop, retlen);
1265 m_freem(mrep);
1266 tsiz -= retlen;
1267 if (v3) {
1268 if (eof || retlen == 0)
1269 tsiz = 0;
1270 } else if (retlen < len)
1271 tsiz = 0;
1272 }
1273 nfsmout:
1274 return (error);
1275 }
1276
1277 struct nfs_writerpc_context {
1278 struct simplelock nwc_slock;
1279 volatile int nwc_mbufcount;
1280 };
1281
1282 /*
1283 * free mbuf used to refer protected pages while write rpc call.
1284 * called at splvm.
1285 */
1286 static void
1287 nfs_writerpc_extfree(struct mbuf *m, caddr_t buf, size_t size, void *arg)
1288 {
1289 struct nfs_writerpc_context *ctx = arg;
1290
1291 KASSERT(m != NULL);
1292 KASSERT(ctx != NULL);
1293 pool_cache_put(&mbpool_cache, m);
1294 simple_lock(&ctx->nwc_slock);
1295 if (--ctx->nwc_mbufcount == 0) {
1296 wakeup(ctx);
1297 }
1298 simple_unlock(&ctx->nwc_slock);
1299 }
1300
1301 /*
1302 * nfs write call
1303 */
1304 int
1305 nfs_writerpc(vp, uiop, iomode, pageprotected, stalewriteverfp)
1306 struct vnode *vp;
1307 struct uio *uiop;
1308 int *iomode;
1309 boolean_t pageprotected;
1310 boolean_t *stalewriteverfp;
1311 {
1312 u_int32_t *tl;
1313 caddr_t cp;
1314 int32_t t1, t2;
1315 caddr_t bpos, dpos, cp2;
1316 struct mbuf *mreq, *mrep, *md, *mb;
1317 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1318 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1319 const int v3 = NFS_ISV3(vp);
1320 int committed = NFSV3WRITE_FILESYNC;
1321 struct nfsnode *np = VTONFS(vp);
1322 struct nfs_writerpc_context ctx;
1323 int s;
1324 struct lwp *l = NULL;
1325 size_t origresid;
1326
1327 simple_lock_init(&ctx.nwc_slock);
1328 ctx.nwc_mbufcount = 1;
1329
1330 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
1331 panic("writerpc readonly vp %p", vp);
1332 }
1333
1334 #ifdef DIAGNOSTIC
1335 if (uiop->uio_iovcnt != 1)
1336 panic("nfs: writerpc iovcnt > 1");
1337 #endif
1338 tsiz = uiop->uio_resid;
1339 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1340 return (EFBIG);
1341 if (pageprotected) {
1342 l = curlwp;
1343 PHOLD(l);
1344 }
1345 retry:
1346 origresid = uiop->uio_resid;
1347 KASSERT(origresid == uiop->uio_iov->iov_len);
1348 while (tsiz > 0) {
1349 uint32_t datalen; /* data bytes need to be allocated in mbuf */
1350 uint32_t backup;
1351 boolean_t stalewriteverf = FALSE;
1352
1353 nfsstats.rpccnt[NFSPROC_WRITE]++;
1354 len = min(tsiz, nmp->nm_wsize);
1355 datalen = pageprotected ? 0 : nfsm_rndup(len);
1356 nfsm_reqhead(np, NFSPROC_WRITE,
1357 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + datalen);
1358 nfsm_fhtom(np, v3);
1359 if (v3) {
1360 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1361 txdr_hyper(uiop->uio_offset, tl);
1362 tl += 2;
1363 *tl++ = txdr_unsigned(len);
1364 *tl++ = txdr_unsigned(*iomode);
1365 *tl = txdr_unsigned(len);
1366 } else {
1367 u_int32_t x;
1368
1369 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1370 /* Set both "begin" and "current" to non-garbage. */
1371 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1372 *tl++ = x; /* "begin offset" */
1373 *tl++ = x; /* "current offset" */
1374 x = txdr_unsigned(len);
1375 *tl++ = x; /* total to this offset */
1376 *tl = x; /* size of this write */
1377
1378 }
1379 if (pageprotected) {
1380 /*
1381 * since we know pages can't be modified during i/o,
1382 * no need to copy them for us.
1383 */
1384 struct mbuf *m;
1385 struct iovec *iovp = uiop->uio_iov;
1386
1387 m = m_get(M_WAIT, MT_DATA);
1388 MCLAIM(m, &nfs_mowner);
1389 MEXTADD(m, iovp->iov_base, len, M_MBUF,
1390 nfs_writerpc_extfree, &ctx);
1391 m->m_flags |= M_EXT_ROMAP;
1392 m->m_len = len;
1393 mb->m_next = m;
1394 /*
1395 * no need to maintain mb and bpos here
1396 * because no one care them later.
1397 */
1398 #if 0
1399 mb = m;
1400 bpos = mtod(caddr_t, mb) + mb->m_len;
1401 #endif
1402 iovp->iov_base = (char *)iovp->iov_base + len;
1403 iovp->iov_len -= len;
1404 uiop->uio_offset += len;
1405 uiop->uio_resid -= len;
1406 s = splvm();
1407 simple_lock(&ctx.nwc_slock);
1408 ctx.nwc_mbufcount++;
1409 simple_unlock(&ctx.nwc_slock);
1410 splx(s);
1411 nfs_zeropad(mb, 0, nfsm_padlen(len));
1412 } else {
1413 nfsm_uiotom(uiop, len);
1414 }
1415 nfsm_request(np, NFSPROC_WRITE, uiop->uio_procp, np->n_wcred);
1416 if (v3) {
1417 wccflag = NFSV3_WCCCHK;
1418 nfsm_wcc_data(vp, wccflag, NAC_NOTRUNC);
1419 if (!error) {
1420 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1421 + NFSX_V3WRITEVERF);
1422 rlen = fxdr_unsigned(int, *tl++);
1423 if (rlen == 0) {
1424 error = NFSERR_IO;
1425 m_freem(mrep);
1426 break;
1427 } else if (rlen < len) {
1428 backup = len - rlen;
1429 uiop->uio_iov->iov_base =
1430 (caddr_t)uiop->uio_iov->iov_base -
1431 backup;
1432 uiop->uio_iov->iov_len += backup;
1433 uiop->uio_offset -= backup;
1434 uiop->uio_resid += backup;
1435 len = rlen;
1436 }
1437 commit = fxdr_unsigned(int, *tl++);
1438
1439 /*
1440 * Return the lowest committment level
1441 * obtained by any of the RPCs.
1442 */
1443 if (committed == NFSV3WRITE_FILESYNC)
1444 committed = commit;
1445 else if (committed == NFSV3WRITE_DATASYNC &&
1446 commit == NFSV3WRITE_UNSTABLE)
1447 committed = commit;
1448 simple_lock(&nmp->nm_slock);
1449 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0){
1450 memcpy(nmp->nm_writeverf, tl,
1451 NFSX_V3WRITEVERF);
1452 nmp->nm_iflag |= NFSMNT_HASWRITEVERF;
1453 } else if ((nmp->nm_iflag &
1454 NFSMNT_STALEWRITEVERF) ||
1455 memcmp(tl, nmp->nm_writeverf,
1456 NFSX_V3WRITEVERF)) {
1457 memcpy(nmp->nm_writeverf, tl,
1458 NFSX_V3WRITEVERF);
1459 /*
1460 * note NFSMNT_STALEWRITEVERF
1461 * if we're the first thread to
1462 * notice it.
1463 */
1464 if ((nmp->nm_iflag &
1465 NFSMNT_STALEWRITEVERF) == 0) {
1466 stalewriteverf = TRUE;
1467 nmp->nm_iflag |=
1468 NFSMNT_STALEWRITEVERF;
1469 }
1470 }
1471 simple_unlock(&nmp->nm_slock);
1472 }
1473 } else
1474 nfsm_loadattr(vp, (struct vattr *)0, NAC_NOTRUNC);
1475 if (wccflag)
1476 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr->va_mtime;
1477 m_freem(mrep);
1478 if (error)
1479 break;
1480 tsiz -= len;
1481 if (stalewriteverf) {
1482 *stalewriteverfp = TRUE;
1483 stalewriteverf = FALSE;
1484 if (committed == NFSV3WRITE_UNSTABLE &&
1485 len != origresid) {
1486 /*
1487 * if our write requests weren't atomic but
1488 * unstable, datas in previous iterations
1489 * might have already been lost now.
1490 * then, we should resend them to nfsd.
1491 */
1492 backup = origresid - tsiz;
1493 uiop->uio_iov->iov_base =
1494 (caddr_t)uiop->uio_iov->iov_base - backup;
1495 uiop->uio_iov->iov_len += backup;
1496 uiop->uio_offset -= backup;
1497 uiop->uio_resid += backup;
1498 tsiz = origresid;
1499 goto retry;
1500 }
1501 }
1502 }
1503 if (pageprotected) {
1504 /*
1505 * wait until mbufs go away.
1506 * retransmitted mbufs can survive longer than rpc requests
1507 * themselves.
1508 */
1509 s = splvm();
1510 simple_lock(&ctx.nwc_slock);
1511 ctx.nwc_mbufcount--;
1512 while (ctx.nwc_mbufcount > 0) {
1513 ltsleep(&ctx, PRIBIO, "nfsmblk", 0, &ctx.nwc_slock);
1514 }
1515 simple_unlock(&ctx.nwc_slock);
1516 splx(s);
1517 PRELE(l);
1518 }
1519 nfsmout:
1520 *iomode = committed;
1521 if (error)
1522 uiop->uio_resid = tsiz;
1523 return (error);
1524 }
1525
1526 /*
1527 * nfs mknod rpc
1528 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1529 * mode set to specify the file type and the size field for rdev.
1530 */
1531 int
1532 nfs_mknodrpc(dvp, vpp, cnp, vap)
1533 struct vnode *dvp;
1534 struct vnode **vpp;
1535 struct componentname *cnp;
1536 struct vattr *vap;
1537 {
1538 struct nfsv2_sattr *sp;
1539 u_int32_t *tl;
1540 caddr_t cp;
1541 int32_t t1, t2;
1542 struct vnode *newvp = (struct vnode *)0;
1543 struct nfsnode *dnp, *np;
1544 char *cp2;
1545 caddr_t bpos, dpos;
1546 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1547 struct mbuf *mreq, *mrep, *md, *mb;
1548 u_int32_t rdev;
1549 const int v3 = NFS_ISV3(dvp);
1550
1551 if (vap->va_type == VCHR || vap->va_type == VBLK)
1552 rdev = txdr_unsigned(vap->va_rdev);
1553 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1554 rdev = nfs_xdrneg1;
1555 else {
1556 VOP_ABORTOP(dvp, cnp);
1557 vput(dvp);
1558 return (EOPNOTSUPP);
1559 }
1560 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1561 dnp = VTONFS(dvp);
1562 nfsm_reqhead(dnp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1563 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1564 nfsm_fhtom(dnp, v3);
1565 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1566 if (v3) {
1567 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1568 *tl++ = vtonfsv3_type(vap->va_type);
1569 nfsm_v3attrbuild(vap, FALSE);
1570 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1571 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1572 *tl++ = txdr_unsigned(major(vap->va_rdev));
1573 *tl = txdr_unsigned(minor(vap->va_rdev));
1574 }
1575 } else {
1576 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1577 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1578 sp->sa_uid = nfs_xdrneg1;
1579 sp->sa_gid = nfs_xdrneg1;
1580 sp->sa_size = rdev;
1581 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1582 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1583 }
1584 nfsm_request(dnp, NFSPROC_MKNOD, cnp->cn_proc, cnp->cn_cred);
1585 if (!error) {
1586 nfsm_mtofh(dvp, newvp, v3, gotvp);
1587 if (!gotvp) {
1588 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1589 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np);
1590 if (!error)
1591 newvp = NFSTOV(np);
1592 }
1593 }
1594 if (v3)
1595 nfsm_wcc_data(dvp, wccflag, 0);
1596 nfsm_reqdone;
1597 if (error) {
1598 if (newvp)
1599 vput(newvp);
1600 } else {
1601 if (cnp->cn_flags & MAKEENTRY)
1602 nfs_cache_enter(dvp, newvp, cnp);
1603 *vpp = newvp;
1604 }
1605 PNBUF_PUT(cnp->cn_pnbuf);
1606 VTONFS(dvp)->n_flag |= NMODIFIED;
1607 if (!wccflag)
1608 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
1609 vput(dvp);
1610 return (error);
1611 }
1612
1613 /*
1614 * nfs mknod vop
1615 * just call nfs_mknodrpc() to do the work.
1616 */
1617 /* ARGSUSED */
1618 int
1619 nfs_mknod(v)
1620 void *v;
1621 {
1622 struct vop_mknod_args /* {
1623 struct vnode *a_dvp;
1624 struct vnode **a_vpp;
1625 struct componentname *a_cnp;
1626 struct vattr *a_vap;
1627 } */ *ap = v;
1628 int error;
1629
1630 error = nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1631 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1632 return (error);
1633 }
1634
1635 static u_long create_verf;
1636 /*
1637 * nfs file create call
1638 */
1639 int
1640 nfs_create(v)
1641 void *v;
1642 {
1643 struct vop_create_args /* {
1644 struct vnode *a_dvp;
1645 struct vnode **a_vpp;
1646 struct componentname *a_cnp;
1647 struct vattr *a_vap;
1648 } */ *ap = v;
1649 struct vnode *dvp = ap->a_dvp;
1650 struct vattr *vap = ap->a_vap;
1651 struct componentname *cnp = ap->a_cnp;
1652 struct nfsv2_sattr *sp;
1653 u_int32_t *tl;
1654 caddr_t cp;
1655 int32_t t1, t2;
1656 struct nfsnode *dnp, *np = (struct nfsnode *)0;
1657 struct vnode *newvp = (struct vnode *)0;
1658 caddr_t bpos, dpos, cp2;
1659 int error, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1660 struct mbuf *mreq, *mrep, *md, *mb;
1661 const int v3 = NFS_ISV3(dvp);
1662
1663 /*
1664 * Oops, not for me..
1665 */
1666 if (vap->va_type == VSOCK)
1667 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1668
1669 #ifdef VA_EXCLUSIVE
1670 if (vap->va_vaflags & VA_EXCLUSIVE)
1671 fmode |= O_EXCL;
1672 #endif
1673 again:
1674 error = 0;
1675 nfsstats.rpccnt[NFSPROC_CREATE]++;
1676 dnp = VTONFS(dvp);
1677 nfsm_reqhead(dnp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1678 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1679 nfsm_fhtom(dnp, v3);
1680 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1681 if (v3) {
1682 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1683 if (fmode & O_EXCL) {
1684 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1685 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1686 #ifdef INET
1687 if (TAILQ_FIRST(&in_ifaddr))
1688 *tl++ = TAILQ_FIRST(&in_ifaddr)->ia_addr.sin_addr.s_addr;
1689 else
1690 *tl++ = create_verf;
1691 #else
1692 *tl++ = create_verf;
1693 #endif
1694 *tl = ++create_verf;
1695 } else {
1696 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1697 nfsm_v3attrbuild(vap, FALSE);
1698 }
1699 } else {
1700 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1701 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1702 sp->sa_uid = nfs_xdrneg1;
1703 sp->sa_gid = nfs_xdrneg1;
1704 sp->sa_size = 0;
1705 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1706 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1707 }
1708 nfsm_request(dnp, NFSPROC_CREATE, cnp->cn_proc, cnp->cn_cred);
1709 if (!error) {
1710 nfsm_mtofh(dvp, newvp, v3, gotvp);
1711 if (!gotvp) {
1712 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1713 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np);
1714 if (!error)
1715 newvp = NFSTOV(np);
1716 }
1717 }
1718 if (v3)
1719 nfsm_wcc_data(dvp, wccflag, 0);
1720 nfsm_reqdone;
1721 if (error) {
1722 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1723 fmode &= ~O_EXCL;
1724 goto again;
1725 }
1726 if (newvp)
1727 vput(newvp);
1728 } else if (v3 && (fmode & O_EXCL))
1729 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_proc);
1730 if (!error) {
1731 if (cnp->cn_flags & MAKEENTRY)
1732 nfs_cache_enter(dvp, newvp, cnp);
1733 *ap->a_vpp = newvp;
1734 }
1735 PNBUF_PUT(cnp->cn_pnbuf);
1736 VTONFS(dvp)->n_flag |= NMODIFIED;
1737 if (!wccflag)
1738 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
1739 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1740 vput(dvp);
1741 return (error);
1742 }
1743
1744 /*
1745 * nfs file remove call
1746 * To try and make nfs semantics closer to ufs semantics, a file that has
1747 * other processes using the vnode is renamed instead of removed and then
1748 * removed later on the last close.
1749 * - If v_usecount > 1
1750 * If a rename is not already in the works
1751 * call nfs_sillyrename() to set it up
1752 * else
1753 * do the remove rpc
1754 */
1755 int
1756 nfs_remove(v)
1757 void *v;
1758 {
1759 struct vop_remove_args /* {
1760 struct vnodeop_desc *a_desc;
1761 struct vnode * a_dvp;
1762 struct vnode * a_vp;
1763 struct componentname * a_cnp;
1764 } */ *ap = v;
1765 struct vnode *vp = ap->a_vp;
1766 struct vnode *dvp = ap->a_dvp;
1767 struct componentname *cnp = ap->a_cnp;
1768 struct nfsnode *np = VTONFS(vp);
1769 int error = 0;
1770 struct vattr vattr;
1771
1772 #ifndef DIAGNOSTIC
1773 if ((cnp->cn_flags & HASBUF) == 0)
1774 panic("nfs_remove: no name");
1775 if (vp->v_usecount < 1)
1776 panic("nfs_remove: bad v_usecount");
1777 #endif
1778 if (vp->v_type == VDIR)
1779 error = EPERM;
1780 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1781 VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_proc) == 0 &&
1782 vattr.va_nlink > 1)) {
1783 /*
1784 * Purge the name cache so that the chance of a lookup for
1785 * the name succeeding while the remove is in progress is
1786 * minimized. Without node locking it can still happen, such
1787 * that an I/O op returns ESTALE, but since you get this if
1788 * another host removes the file..
1789 */
1790 cache_purge(vp);
1791 /*
1792 * throw away biocache buffers, mainly to avoid
1793 * unnecessary delayed writes later.
1794 */
1795 error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, cnp->cn_proc, 1);
1796 /* Do the rpc */
1797 if (error != EINTR)
1798 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1799 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc);
1800 } else if (!np->n_sillyrename)
1801 error = nfs_sillyrename(dvp, vp, cnp);
1802 PNBUF_PUT(cnp->cn_pnbuf);
1803 if (!error && nfs_getattrcache(vp, &vattr) == 0 &&
1804 vattr.va_nlink == 1) {
1805 np->n_flag |= NREMOVED;
1806 }
1807 NFS_INVALIDATE_ATTRCACHE(np);
1808 VN_KNOTE(vp, NOTE_DELETE);
1809 VN_KNOTE(dvp, NOTE_WRITE);
1810 if (dvp == vp)
1811 vrele(vp);
1812 else
1813 vput(vp);
1814 vput(dvp);
1815 return (error);
1816 }
1817
1818 /*
1819 * nfs file remove rpc called from nfs_inactive
1820 */
1821 int
1822 nfs_removeit(sp)
1823 struct sillyrename *sp;
1824 {
1825
1826 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1827 (struct proc *)0));
1828 }
1829
1830 /*
1831 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1832 */
1833 int
1834 nfs_removerpc(dvp, name, namelen, cred, proc)
1835 struct vnode *dvp;
1836 const char *name;
1837 int namelen;
1838 struct ucred *cred;
1839 struct proc *proc;
1840 {
1841 u_int32_t *tl;
1842 caddr_t cp;
1843 int32_t t1, t2;
1844 caddr_t bpos, dpos, cp2;
1845 int error = 0, wccflag = NFSV3_WCCRATTR;
1846 struct mbuf *mreq, *mrep, *md, *mb;
1847 const int v3 = NFS_ISV3(dvp);
1848 int rexmit;
1849 struct nfsnode *dnp = VTONFS(dvp);
1850
1851 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1852 nfsm_reqhead(dnp, NFSPROC_REMOVE,
1853 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1854 nfsm_fhtom(dnp, v3);
1855 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1856 nfsm_request1(dnp, NFSPROC_REMOVE, proc, cred, &rexmit);
1857 if (v3)
1858 nfsm_wcc_data(dvp, wccflag, 0);
1859 nfsm_reqdone;
1860 VTONFS(dvp)->n_flag |= NMODIFIED;
1861 if (!wccflag)
1862 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
1863 /*
1864 * Kludge City: If the first reply to the remove rpc is lost..
1865 * the reply to the retransmitted request will be ENOENT
1866 * since the file was in fact removed
1867 * Therefore, we cheat and return success.
1868 */
1869 if (rexmit && error == ENOENT)
1870 error = 0;
1871 return (error);
1872 }
1873
1874 /*
1875 * nfs file rename call
1876 */
1877 int
1878 nfs_rename(v)
1879 void *v;
1880 {
1881 struct vop_rename_args /* {
1882 struct vnode *a_fdvp;
1883 struct vnode *a_fvp;
1884 struct componentname *a_fcnp;
1885 struct vnode *a_tdvp;
1886 struct vnode *a_tvp;
1887 struct componentname *a_tcnp;
1888 } */ *ap = v;
1889 struct vnode *fvp = ap->a_fvp;
1890 struct vnode *tvp = ap->a_tvp;
1891 struct vnode *fdvp = ap->a_fdvp;
1892 struct vnode *tdvp = ap->a_tdvp;
1893 struct componentname *tcnp = ap->a_tcnp;
1894 struct componentname *fcnp = ap->a_fcnp;
1895 int error;
1896
1897 #ifndef DIAGNOSTIC
1898 if ((tcnp->cn_flags & HASBUF) == 0 ||
1899 (fcnp->cn_flags & HASBUF) == 0)
1900 panic("nfs_rename: no name");
1901 #endif
1902 /* Check for cross-device rename */
1903 if ((fvp->v_mount != tdvp->v_mount) ||
1904 (tvp && (fvp->v_mount != tvp->v_mount))) {
1905 error = EXDEV;
1906 goto out;
1907 }
1908
1909 /*
1910 * If the tvp exists and is in use, sillyrename it before doing the
1911 * rename of the new file over it.
1912 */
1913 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1914 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1915 VN_KNOTE(tvp, NOTE_DELETE);
1916 vput(tvp);
1917 tvp = NULL;
1918 }
1919
1920 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1921 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1922 tcnp->cn_proc);
1923
1924 VN_KNOTE(fdvp, NOTE_WRITE);
1925 VN_KNOTE(tdvp, NOTE_WRITE);
1926 if (fvp->v_type == VDIR) {
1927 if (tvp != NULL && tvp->v_type == VDIR)
1928 cache_purge(tdvp);
1929 cache_purge(fdvp);
1930 }
1931 out:
1932 if (tdvp == tvp)
1933 vrele(tdvp);
1934 else
1935 vput(tdvp);
1936 if (tvp)
1937 vput(tvp);
1938 vrele(fdvp);
1939 vrele(fvp);
1940 return (error);
1941 }
1942
1943 /*
1944 * nfs file rename rpc called from nfs_remove() above
1945 */
1946 int
1947 nfs_renameit(sdvp, scnp, sp)
1948 struct vnode *sdvp;
1949 struct componentname *scnp;
1950 struct sillyrename *sp;
1951 {
1952 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1953 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_proc));
1954 }
1955
1956 /*
1957 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1958 */
1959 int
1960 nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, proc)
1961 struct vnode *fdvp;
1962 const char *fnameptr;
1963 int fnamelen;
1964 struct vnode *tdvp;
1965 const char *tnameptr;
1966 int tnamelen;
1967 struct ucred *cred;
1968 struct proc *proc;
1969 {
1970 u_int32_t *tl;
1971 caddr_t cp;
1972 int32_t t1, t2;
1973 caddr_t bpos, dpos, cp2;
1974 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1975 struct mbuf *mreq, *mrep, *md, *mb;
1976 const int v3 = NFS_ISV3(fdvp);
1977 int rexmit;
1978 struct nfsnode *fdnp = VTONFS(fdvp);
1979
1980 nfsstats.rpccnt[NFSPROC_RENAME]++;
1981 nfsm_reqhead(fdnp, NFSPROC_RENAME,
1982 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1983 nfsm_rndup(tnamelen));
1984 nfsm_fhtom(fdnp, v3);
1985 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1986 nfsm_fhtom(VTONFS(tdvp), v3);
1987 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1988 nfsm_request1(fdnp, NFSPROC_RENAME, proc, cred, &rexmit);
1989 if (v3) {
1990 nfsm_wcc_data(fdvp, fwccflag, 0);
1991 nfsm_wcc_data(tdvp, twccflag, 0);
1992 }
1993 nfsm_reqdone;
1994 VTONFS(fdvp)->n_flag |= NMODIFIED;
1995 VTONFS(tdvp)->n_flag |= NMODIFIED;
1996 if (!fwccflag)
1997 NFS_INVALIDATE_ATTRCACHE(VTONFS(fdvp));
1998 if (!twccflag)
1999 NFS_INVALIDATE_ATTRCACHE(VTONFS(tdvp));
2000 /*
2001 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2002 */
2003 if (rexmit && error == ENOENT)
2004 error = 0;
2005 return (error);
2006 }
2007
2008 /*
2009 * nfs hard link create call
2010 */
2011 int
2012 nfs_link(v)
2013 void *v;
2014 {
2015 struct vop_link_args /* {
2016 struct vnode *a_dvp;
2017 struct vnode *a_vp;
2018 struct componentname *a_cnp;
2019 } */ *ap = v;
2020 struct vnode *vp = ap->a_vp;
2021 struct vnode *dvp = ap->a_dvp;
2022 struct componentname *cnp = ap->a_cnp;
2023 u_int32_t *tl;
2024 caddr_t cp;
2025 int32_t t1, t2;
2026 caddr_t bpos, dpos, cp2;
2027 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
2028 struct mbuf *mreq, *mrep, *md, *mb;
2029 /* XXX Should be const and initialised? */
2030 int v3;
2031 int rexmit;
2032 struct nfsnode *np;
2033
2034 if (dvp->v_mount != vp->v_mount) {
2035 VOP_ABORTOP(dvp, cnp);
2036 vput(dvp);
2037 return (EXDEV);
2038 }
2039 if (dvp != vp) {
2040 error = vn_lock(vp, LK_EXCLUSIVE);
2041 if (error != 0) {
2042 VOP_ABORTOP(dvp, cnp);
2043 vput(dvp);
2044 return error;
2045 }
2046 }
2047
2048 /*
2049 * Push all writes to the server, so that the attribute cache
2050 * doesn't get "out of sync" with the server.
2051 * XXX There should be a better way!
2052 */
2053 VOP_FSYNC(vp, cnp->cn_cred, FSYNC_WAIT, 0, 0, cnp->cn_proc);
2054
2055 v3 = NFS_ISV3(vp);
2056 nfsstats.rpccnt[NFSPROC_LINK]++;
2057 np = VTONFS(vp);
2058 nfsm_reqhead(np, NFSPROC_LINK,
2059 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2060 nfsm_fhtom(np, v3);
2061 nfsm_fhtom(VTONFS(dvp), v3);
2062 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2063 nfsm_request1(np, NFSPROC_LINK, cnp->cn_proc, cnp->cn_cred, &rexmit);
2064 if (v3) {
2065 nfsm_postop_attr(vp, attrflag, 0);
2066 nfsm_wcc_data(dvp, wccflag, 0);
2067 }
2068 nfsm_reqdone;
2069 PNBUF_PUT(cnp->cn_pnbuf);
2070 VTONFS(dvp)->n_flag |= NMODIFIED;
2071 if (!attrflag)
2072 NFS_INVALIDATE_ATTRCACHE(VTONFS(vp));
2073 if (!wccflag)
2074 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2075 if (dvp != vp)
2076 VOP_UNLOCK(vp, 0);
2077 VN_KNOTE(vp, NOTE_LINK);
2078 VN_KNOTE(dvp, NOTE_WRITE);
2079 vput(dvp);
2080 /*
2081 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2082 */
2083 if (rexmit && error == EEXIST)
2084 error = 0;
2085 return (error);
2086 }
2087
2088 /*
2089 * nfs symbolic link create call
2090 */
2091 int
2092 nfs_symlink(v)
2093 void *v;
2094 {
2095 struct vop_symlink_args /* {
2096 struct vnode *a_dvp;
2097 struct vnode **a_vpp;
2098 struct componentname *a_cnp;
2099 struct vattr *a_vap;
2100 char *a_target;
2101 } */ *ap = v;
2102 struct vnode *dvp = ap->a_dvp;
2103 struct vattr *vap = ap->a_vap;
2104 struct componentname *cnp = ap->a_cnp;
2105 struct nfsv2_sattr *sp;
2106 u_int32_t *tl;
2107 caddr_t cp;
2108 int32_t t1, t2;
2109 caddr_t bpos, dpos, cp2;
2110 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2111 struct mbuf *mreq, *mrep, *md, *mb;
2112 struct vnode *newvp = (struct vnode *)0;
2113 const int v3 = NFS_ISV3(dvp);
2114 int rexmit;
2115 struct nfsnode *dnp = VTONFS(dvp);
2116
2117 *ap->a_vpp = NULL;
2118 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2119 slen = strlen(ap->a_target);
2120 nfsm_reqhead(dnp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
2121 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
2122 nfsm_fhtom(dnp, v3);
2123 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2124 if (v3)
2125 nfsm_v3attrbuild(vap, FALSE);
2126 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
2127 if (!v3) {
2128 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2129 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2130 sp->sa_uid = nfs_xdrneg1;
2131 sp->sa_gid = nfs_xdrneg1;
2132 sp->sa_size = nfs_xdrneg1;
2133 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2134 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2135 }
2136 nfsm_request1(dnp, NFSPROC_SYMLINK, cnp->cn_proc, cnp->cn_cred,
2137 &rexmit);
2138 if (v3) {
2139 if (!error)
2140 nfsm_mtofh(dvp, newvp, v3, gotvp);
2141 nfsm_wcc_data(dvp, wccflag, 0);
2142 }
2143 nfsm_reqdone;
2144 /*
2145 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2146 */
2147 if (rexmit && error == EEXIST)
2148 error = 0;
2149 if (error == 0 && newvp == NULL) {
2150 struct nfsnode *np = NULL;
2151
2152 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2153 cnp->cn_cred, cnp->cn_proc, &np);
2154 if (error == 0)
2155 newvp = NFSTOV(np);
2156 }
2157 if (error) {
2158 if (newvp != NULL)
2159 vput(newvp);
2160 } else {
2161 *ap->a_vpp = newvp;
2162 }
2163 PNBUF_PUT(cnp->cn_pnbuf);
2164 VTONFS(dvp)->n_flag |= NMODIFIED;
2165 if (!wccflag)
2166 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2167 VN_KNOTE(dvp, NOTE_WRITE);
2168 vput(dvp);
2169 return (error);
2170 }
2171
2172 /*
2173 * nfs make dir call
2174 */
2175 int
2176 nfs_mkdir(v)
2177 void *v;
2178 {
2179 struct vop_mkdir_args /* {
2180 struct vnode *a_dvp;
2181 struct vnode **a_vpp;
2182 struct componentname *a_cnp;
2183 struct vattr *a_vap;
2184 } */ *ap = v;
2185 struct vnode *dvp = ap->a_dvp;
2186 struct vattr *vap = ap->a_vap;
2187 struct componentname *cnp = ap->a_cnp;
2188 struct nfsv2_sattr *sp;
2189 u_int32_t *tl;
2190 caddr_t cp;
2191 int32_t t1, t2;
2192 int len;
2193 struct nfsnode *dnp = VTONFS(dvp), *np = (struct nfsnode *)0;
2194 struct vnode *newvp = (struct vnode *)0;
2195 caddr_t bpos, dpos, cp2;
2196 int error = 0, wccflag = NFSV3_WCCRATTR;
2197 int gotvp = 0;
2198 int rexmit;
2199 struct mbuf *mreq, *mrep, *md, *mb;
2200 const int v3 = NFS_ISV3(dvp);
2201
2202 len = cnp->cn_namelen;
2203 nfsstats.rpccnt[NFSPROC_MKDIR]++;
2204 nfsm_reqhead(dnp, NFSPROC_MKDIR,
2205 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2206 nfsm_fhtom(dnp, v3);
2207 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
2208 if (v3) {
2209 nfsm_v3attrbuild(vap, FALSE);
2210 } else {
2211 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2212 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2213 sp->sa_uid = nfs_xdrneg1;
2214 sp->sa_gid = nfs_xdrneg1;
2215 sp->sa_size = nfs_xdrneg1;
2216 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2217 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2218 }
2219 nfsm_request1(dnp, NFSPROC_MKDIR, cnp->cn_proc, cnp->cn_cred, &rexmit);
2220 if (!error)
2221 nfsm_mtofh(dvp, newvp, v3, gotvp);
2222 if (v3)
2223 nfsm_wcc_data(dvp, wccflag, 0);
2224 nfsm_reqdone;
2225 VTONFS(dvp)->n_flag |= NMODIFIED;
2226 if (!wccflag)
2227 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2228 /*
2229 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2230 * if we can succeed in looking up the directory.
2231 */
2232 if ((rexmit && error == EEXIST) || (!error && !gotvp)) {
2233 if (newvp) {
2234 vput(newvp);
2235 newvp = (struct vnode *)0;
2236 }
2237 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2238 cnp->cn_proc, &np);
2239 if (!error) {
2240 newvp = NFSTOV(np);
2241 if (newvp->v_type != VDIR || newvp == dvp)
2242 error = EEXIST;
2243 }
2244 }
2245 if (error) {
2246 if (newvp) {
2247 if (dvp != newvp)
2248 vput(newvp);
2249 else
2250 vrele(newvp);
2251 }
2252 } else {
2253 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
2254 if (cnp->cn_flags & MAKEENTRY)
2255 nfs_cache_enter(dvp, newvp, cnp);
2256 *ap->a_vpp = newvp;
2257 }
2258 PNBUF_PUT(cnp->cn_pnbuf);
2259 vput(dvp);
2260 return (error);
2261 }
2262
2263 /*
2264 * nfs remove directory call
2265 */
2266 int
2267 nfs_rmdir(v)
2268 void *v;
2269 {
2270 struct vop_rmdir_args /* {
2271 struct vnode *a_dvp;
2272 struct vnode *a_vp;
2273 struct componentname *a_cnp;
2274 } */ *ap = v;
2275 struct vnode *vp = ap->a_vp;
2276 struct vnode *dvp = ap->a_dvp;
2277 struct componentname *cnp = ap->a_cnp;
2278 u_int32_t *tl;
2279 caddr_t cp;
2280 int32_t t1, t2;
2281 caddr_t bpos, dpos, cp2;
2282 int error = 0, wccflag = NFSV3_WCCRATTR;
2283 int rexmit;
2284 struct mbuf *mreq, *mrep, *md, *mb;
2285 const int v3 = NFS_ISV3(dvp);
2286 struct nfsnode *dnp;
2287
2288 if (dvp == vp) {
2289 vrele(dvp);
2290 vput(dvp);
2291 PNBUF_PUT(cnp->cn_pnbuf);
2292 return (EINVAL);
2293 }
2294 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2295 dnp = VTONFS(dvp);
2296 nfsm_reqhead(dnp, NFSPROC_RMDIR,
2297 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2298 nfsm_fhtom(dnp, v3);
2299 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2300 nfsm_request1(dnp, NFSPROC_RMDIR, cnp->cn_proc, cnp->cn_cred, &rexmit);
2301 if (v3)
2302 nfsm_wcc_data(dvp, wccflag, 0);
2303 nfsm_reqdone;
2304 PNBUF_PUT(cnp->cn_pnbuf);
2305 VTONFS(dvp)->n_flag |= NMODIFIED;
2306 if (!wccflag)
2307 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2308 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
2309 VN_KNOTE(vp, NOTE_DELETE);
2310 cache_purge(vp);
2311 vput(vp);
2312 vput(dvp);
2313 /*
2314 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2315 */
2316 if (rexmit && error == ENOENT)
2317 error = 0;
2318 return (error);
2319 }
2320
2321 /*
2322 * nfs readdir call
2323 */
2324 int
2325 nfs_readdir(v)
2326 void *v;
2327 {
2328 struct vop_readdir_args /* {
2329 struct vnode *a_vp;
2330 struct uio *a_uio;
2331 struct ucred *a_cred;
2332 int *a_eofflag;
2333 off_t **a_cookies;
2334 int *a_ncookies;
2335 } */ *ap = v;
2336 struct vnode *vp = ap->a_vp;
2337 struct uio *uio = ap->a_uio;
2338 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2339 char *base = uio->uio_iov->iov_base;
2340 int tresid, error;
2341 size_t count, lost;
2342 struct dirent *dp;
2343 off_t *cookies = NULL;
2344 int ncookies = 0, nc;
2345
2346 if (vp->v_type != VDIR)
2347 return (EPERM);
2348
2349 lost = uio->uio_resid & (NFS_DIRFRAGSIZ - 1);
2350 count = uio->uio_resid - lost;
2351 if (count <= 0)
2352 return (EINVAL);
2353
2354 /*
2355 * Call nfs_bioread() to do the real work.
2356 */
2357 tresid = uio->uio_resid = count;
2358 error = nfs_bioread(vp, uio, 0, ap->a_cred,
2359 ap->a_cookies ? NFSBIO_CACHECOOKIES : 0);
2360
2361 if (!error && ap->a_cookies) {
2362 ncookies = count / 16;
2363 cookies = malloc(sizeof (off_t) * ncookies, M_TEMP, M_WAITOK);
2364 *ap->a_cookies = cookies;
2365 }
2366
2367 if (!error && uio->uio_resid == tresid) {
2368 uio->uio_resid += lost;
2369 nfsstats.direofcache_misses++;
2370 if (ap->a_cookies)
2371 *ap->a_ncookies = 0;
2372 *ap->a_eofflag = 1;
2373 return (0);
2374 }
2375
2376 if (!error && ap->a_cookies) {
2377 /*
2378 * Only the NFS server and emulations use cookies, and they
2379 * load the directory block into system space, so we can
2380 * just look at it directly.
2381 */
2382 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2383 panic("nfs_readdir: lost in space");
2384 for (nc = 0; ncookies-- &&
2385 base < (char *)uio->uio_iov->iov_base; nc++){
2386 dp = (struct dirent *) base;
2387 if (dp->d_reclen == 0)
2388 break;
2389 if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
2390 *(cookies++) = (off_t)NFS_GETCOOKIE32(dp);
2391 else
2392 *(cookies++) = NFS_GETCOOKIE(dp);
2393 base += dp->d_reclen;
2394 }
2395 uio->uio_resid +=
2396 ((caddr_t)uio->uio_iov->iov_base - base);
2397 uio->uio_iov->iov_len +=
2398 ((caddr_t)uio->uio_iov->iov_base - base);
2399 uio->uio_iov->iov_base = base;
2400 *ap->a_ncookies = nc;
2401 }
2402
2403 uio->uio_resid += lost;
2404 *ap->a_eofflag = 0;
2405 return (error);
2406 }
2407
2408 /*
2409 * Readdir rpc call.
2410 * Called from below the buffer cache by nfs_doio().
2411 */
2412 int
2413 nfs_readdirrpc(vp, uiop, cred)
2414 struct vnode *vp;
2415 struct uio *uiop;
2416 struct ucred *cred;
2417 {
2418 int len, left;
2419 struct dirent *dp = NULL;
2420 u_int32_t *tl;
2421 caddr_t cp;
2422 int32_t t1, t2;
2423 caddr_t bpos, dpos, cp2;
2424 struct mbuf *mreq, *mrep, *md, *mb;
2425 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2426 struct nfsnode *dnp = VTONFS(vp);
2427 u_quad_t fileno;
2428 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2429 int attrflag, nrpcs = 0, reclen;
2430 const int v3 = NFS_ISV3(vp);
2431
2432 #ifdef DIAGNOSTIC
2433 /*
2434 * Should be called from buffer cache, so only amount of
2435 * NFS_DIRBLKSIZ will be requested.
2436 */
2437 if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1)))
2438 panic("nfs readdirrpc bad uio");
2439 #endif
2440
2441 /*
2442 * Loop around doing readdir rpc's of size nm_readdirsize
2443 * truncated to a multiple of NFS_DIRFRAGSIZ.
2444 * The stopping criteria is EOF or buffer full.
2445 */
2446 while (more_dirs && bigenough) {
2447 /*
2448 * Heuristic: don't bother to do another RPC to further
2449 * fill up this block if there is not much room left. (< 50%
2450 * of the readdir RPC size). This wastes some buffer space
2451 * but can save up to 50% in RPC calls.
2452 */
2453 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
2454 bigenough = 0;
2455 break;
2456 }
2457 nfsstats.rpccnt[NFSPROC_READDIR]++;
2458 nfsm_reqhead(dnp, NFSPROC_READDIR, NFSX_FH(v3) +
2459 NFSX_READDIR(v3));
2460 nfsm_fhtom(dnp, v3);
2461 if (v3) {
2462 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2463 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2464 txdr_swapcookie3(uiop->uio_offset, tl);
2465 } else {
2466 txdr_cookie3(uiop->uio_offset, tl);
2467 }
2468 tl += 2;
2469 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2470 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2471 } else {
2472 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2473 *tl++ = txdr_unsigned(uiop->uio_offset);
2474 }
2475 *tl = txdr_unsigned(nmp->nm_readdirsize);
2476 nfsm_request(dnp, NFSPROC_READDIR, uiop->uio_procp, cred);
2477 nrpcs++;
2478 if (v3) {
2479 nfsm_postop_attr(vp, attrflag, 0);
2480 if (!error) {
2481 nfsm_dissect(tl, u_int32_t *,
2482 2 * NFSX_UNSIGNED);
2483 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2484 dnp->n_cookieverf.nfsuquad[1] = *tl;
2485 } else {
2486 m_freem(mrep);
2487 goto nfsmout;
2488 }
2489 }
2490 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2491 more_dirs = fxdr_unsigned(int, *tl);
2492
2493 /* loop thru the dir entries, doctoring them to 4bsd form */
2494 while (more_dirs && bigenough) {
2495 if (v3) {
2496 nfsm_dissect(tl, u_int32_t *,
2497 3 * NFSX_UNSIGNED);
2498 fileno = fxdr_hyper(tl);
2499 len = fxdr_unsigned(int, *(tl + 2));
2500 } else {
2501 nfsm_dissect(tl, u_int32_t *,
2502 2 * NFSX_UNSIGNED);
2503 fileno = fxdr_unsigned(u_quad_t, *tl++);
2504 len = fxdr_unsigned(int, *tl);
2505 }
2506 if (len <= 0 || len > NFS_MAXNAMLEN) {
2507 error = EBADRPC;
2508 m_freem(mrep);
2509 goto nfsmout;
2510 }
2511 tlen = nfsm_rndup(len);
2512 if (tlen == len)
2513 tlen += 4; /* To ensure null termination */
2514 tlen += sizeof (off_t) + sizeof (int);
2515 reclen = ALIGN(tlen + DIRHDSIZ);
2516 tlen = reclen - DIRHDSIZ;
2517 left = NFS_DIRFRAGSIZ - blksiz;
2518 if (reclen > left) {
2519 dp->d_reclen += left;
2520 uiop->uio_iov->iov_base =
2521 (caddr_t)uiop->uio_iov->iov_base + left;
2522 uiop->uio_iov->iov_len -= left;
2523 uiop->uio_resid -= left;
2524 blksiz = 0;
2525 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2526 }
2527 if (reclen > uiop->uio_resid)
2528 bigenough = 0;
2529 if (bigenough) {
2530 dp = (struct dirent *)uiop->uio_iov->iov_base;
2531 dp->d_fileno = (int)fileno;
2532 dp->d_namlen = len;
2533 dp->d_reclen = reclen;
2534 dp->d_type = DT_UNKNOWN;
2535 blksiz += dp->d_reclen;
2536 if (blksiz == NFS_DIRFRAGSIZ)
2537 blksiz = 0;
2538 uiop->uio_resid -= DIRHDSIZ;
2539 uiop->uio_iov->iov_base =
2540 (caddr_t)uiop->uio_iov->iov_base + DIRHDSIZ;
2541 uiop->uio_iov->iov_len -= DIRHDSIZ;
2542 nfsm_mtouio(uiop, len);
2543 cp = uiop->uio_iov->iov_base;
2544 tlen -= len;
2545 *cp = '\0'; /* null terminate */
2546 uiop->uio_iov->iov_base =
2547 (caddr_t)uiop->uio_iov->iov_base + tlen;
2548 uiop->uio_iov->iov_len -= tlen;
2549 uiop->uio_resid -= tlen;
2550 } else
2551 nfsm_adv(nfsm_rndup(len));
2552 if (v3) {
2553 nfsm_dissect(tl, u_int32_t *,
2554 3 * NFSX_UNSIGNED);
2555 } else {
2556 nfsm_dissect(tl, u_int32_t *,
2557 2 * NFSX_UNSIGNED);
2558 }
2559 if (bigenough) {
2560 if (v3) {
2561 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2562 uiop->uio_offset =
2563 fxdr_swapcookie3(tl);
2564 else
2565 uiop->uio_offset =
2566 fxdr_cookie3(tl);
2567 }
2568 else {
2569 uiop->uio_offset =
2570 fxdr_unsigned(off_t, *tl);
2571 }
2572 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2573 }
2574 if (v3)
2575 tl += 2;
2576 else
2577 tl++;
2578 more_dirs = fxdr_unsigned(int, *tl);
2579 }
2580 /*
2581 * If at end of rpc data, get the eof boolean
2582 */
2583 if (!more_dirs) {
2584 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2585 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2586 }
2587 m_freem(mrep);
2588 }
2589 /*
2590 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2591 * by increasing d_reclen for the last record.
2592 */
2593 if (blksiz > 0) {
2594 left = NFS_DIRFRAGSIZ - blksiz;
2595 dp->d_reclen += left;
2596 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2597 uiop->uio_iov->iov_base = (caddr_t)uiop->uio_iov->iov_base +
2598 left;
2599 uiop->uio_iov->iov_len -= left;
2600 uiop->uio_resid -= left;
2601 }
2602
2603 /*
2604 * We are now either at the end of the directory or have filled the
2605 * block.
2606 */
2607 if (bigenough)
2608 dnp->n_direofoffset = uiop->uio_offset;
2609 nfsmout:
2610 return (error);
2611 }
2612
2613 /*
2614 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2615 */
2616 int
2617 nfs_readdirplusrpc(vp, uiop, cred)
2618 struct vnode *vp;
2619 struct uio *uiop;
2620 struct ucred *cred;
2621 {
2622 int len, left;
2623 struct dirent *dp = NULL;
2624 u_int32_t *tl;
2625 caddr_t cp;
2626 int32_t t1, t2;
2627 struct vnode *newvp;
2628 caddr_t bpos, dpos, cp2;
2629 struct mbuf *mreq, *mrep, *md, *mb;
2630 struct nameidata nami, *ndp = &nami;
2631 struct componentname *cnp = &ndp->ni_cnd;
2632 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2633 struct nfsnode *dnp = VTONFS(vp), *np;
2634 nfsfh_t *fhp;
2635 u_quad_t fileno;
2636 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2637 int attrflag, fhsize, nrpcs = 0, reclen;
2638 struct nfs_fattr fattr, *fp;
2639
2640 #ifdef DIAGNOSTIC
2641 if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1)))
2642 panic("nfs readdirplusrpc bad uio");
2643 #endif
2644 ndp->ni_dvp = vp;
2645 newvp = NULLVP;
2646
2647 /*
2648 * Loop around doing readdir rpc's of size nm_readdirsize
2649 * truncated to a multiple of NFS_DIRFRAGSIZ.
2650 * The stopping criteria is EOF or buffer full.
2651 */
2652 while (more_dirs && bigenough) {
2653 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
2654 bigenough = 0;
2655 break;
2656 }
2657 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2658 nfsm_reqhead(dnp, NFSPROC_READDIRPLUS,
2659 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2660 nfsm_fhtom(dnp, 1);
2661 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2662 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2663 txdr_swapcookie3(uiop->uio_offset, tl);
2664 } else {
2665 txdr_cookie3(uiop->uio_offset, tl);
2666 }
2667 tl += 2;
2668 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2669 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2670 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2671 *tl = txdr_unsigned(nmp->nm_rsize);
2672 nfsm_request(dnp, NFSPROC_READDIRPLUS, uiop->uio_procp, cred);
2673 nfsm_postop_attr(vp, attrflag, 0);
2674 if (error) {
2675 m_freem(mrep);
2676 goto nfsmout;
2677 }
2678 nrpcs++;
2679 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2680 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2681 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2682 more_dirs = fxdr_unsigned(int, *tl);
2683
2684 /* loop thru the dir entries, doctoring them to 4bsd form */
2685 while (more_dirs && bigenough) {
2686 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2687 fileno = fxdr_hyper(tl);
2688 len = fxdr_unsigned(int, *(tl + 2));
2689 if (len <= 0 || len > NFS_MAXNAMLEN) {
2690 error = EBADRPC;
2691 m_freem(mrep);
2692 goto nfsmout;
2693 }
2694 tlen = nfsm_rndup(len);
2695 if (tlen == len)
2696 tlen += 4; /* To ensure null termination*/
2697 tlen += sizeof (off_t) + sizeof (int);
2698 reclen = ALIGN(tlen + DIRHDSIZ);
2699 tlen = reclen - DIRHDSIZ;
2700 left = NFS_DIRFRAGSIZ - blksiz;
2701 if (reclen > left) {
2702 /*
2703 * DIRFRAGSIZ is aligned, no need to align
2704 * again here.
2705 */
2706 dp->d_reclen += left;
2707 uiop->uio_iov->iov_base =
2708 (caddr_t)uiop->uio_iov->iov_base + left;
2709 uiop->uio_iov->iov_len -= left;
2710 uiop->uio_resid -= left;
2711 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2712 blksiz = 0;
2713 }
2714 if (reclen > uiop->uio_resid)
2715 bigenough = 0;
2716 if (bigenough) {
2717 dp = (struct dirent *)uiop->uio_iov->iov_base;
2718 dp->d_fileno = (int)fileno;
2719 dp->d_namlen = len;
2720 dp->d_reclen = reclen;
2721 dp->d_type = DT_UNKNOWN;
2722 blksiz += dp->d_reclen;
2723 if (blksiz == NFS_DIRFRAGSIZ)
2724 blksiz = 0;
2725 uiop->uio_resid -= DIRHDSIZ;
2726 uiop->uio_iov->iov_base =
2727 (caddr_t)uiop->uio_iov->iov_base +
2728 DIRHDSIZ;
2729 uiop->uio_iov->iov_len -= DIRHDSIZ;
2730 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2731 cnp->cn_namelen = len;
2732 nfsm_mtouio(uiop, len);
2733 cp = uiop->uio_iov->iov_base;
2734 tlen -= len;
2735 *cp = '\0';
2736 uiop->uio_iov->iov_base =
2737 (caddr_t)uiop->uio_iov->iov_base + tlen;
2738 uiop->uio_iov->iov_len -= tlen;
2739 uiop->uio_resid -= tlen;
2740 } else
2741 nfsm_adv(nfsm_rndup(len));
2742 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2743 if (bigenough) {
2744 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2745 uiop->uio_offset =
2746 fxdr_swapcookie3(tl);
2747 else
2748 uiop->uio_offset =
2749 fxdr_cookie3(tl);
2750 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2751 }
2752 tl += 2;
2753
2754 /*
2755 * Since the attributes are before the file handle
2756 * (sigh), we must skip over the attributes and then
2757 * come back and get them.
2758 */
2759 attrflag = fxdr_unsigned(int, *tl);
2760 if (attrflag) {
2761 nfsm_dissect(fp, struct nfs_fattr *, NFSX_V3FATTR);
2762 memcpy(&fattr, fp, NFSX_V3FATTR);
2763 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2764 doit = fxdr_unsigned(int, *tl);
2765 if (doit) {
2766 nfsm_getfh(fhp, fhsize, 1);
2767 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2768 VREF(vp);
2769 newvp = vp;
2770 np = dnp;
2771 } else {
2772 error = nfs_nget1(vp->v_mount, fhp,
2773 fhsize, &np, LK_NOWAIT);
2774 if (!error)
2775 newvp = NFSTOV(np);
2776 }
2777 if (!error) {
2778 const char *cp;
2779
2780 nfs_loadattrcache(&newvp, &fattr, 0, 0);
2781 dp->d_type =
2782 IFTODT(VTTOIF(np->n_vattr->va_type));
2783 ndp->ni_vp = newvp;
2784 cp = cnp->cn_nameptr + cnp->cn_namelen;
2785 cnp->cn_hash =
2786 namei_hash(cnp->cn_nameptr, &cp);
2787 if (cnp->cn_namelen <= NCHNAMLEN) {
2788 cache_purge1(ndp->ni_dvp, cnp, 0);
2789 nfs_cache_enter(ndp->ni_dvp, ndp->ni_vp,
2790 cnp);
2791 }
2792 }
2793 }
2794 } else {
2795 /* Just skip over the file handle */
2796 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2797 i = fxdr_unsigned(int, *tl);
2798 nfsm_adv(nfsm_rndup(i));
2799 }
2800 if (newvp != NULLVP) {
2801 if (newvp == vp)
2802 vrele(newvp);
2803 else
2804 vput(newvp);
2805 newvp = NULLVP;
2806 }
2807 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2808 more_dirs = fxdr_unsigned(int, *tl);
2809 }
2810 /*
2811 * If at end of rpc data, get the eof boolean
2812 */
2813 if (!more_dirs) {
2814 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2815 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2816 }
2817 m_freem(mrep);
2818 }
2819 /*
2820 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2821 * by increasing d_reclen for the last record.
2822 */
2823 if (blksiz > 0) {
2824 left = NFS_DIRFRAGSIZ - blksiz;
2825 dp->d_reclen += left;
2826 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2827 uiop->uio_iov->iov_base = (caddr_t)uiop->uio_iov->iov_base +
2828 left;
2829 uiop->uio_iov->iov_len -= left;
2830 uiop->uio_resid -= left;
2831 }
2832
2833 /*
2834 * We are now either at the end of the directory or have filled the
2835 * block.
2836 */
2837 if (bigenough)
2838 dnp->n_direofoffset = uiop->uio_offset;
2839 nfsmout:
2840 if (newvp != NULLVP) {
2841 if(newvp == vp)
2842 vrele(newvp);
2843 else
2844 vput(newvp);
2845 }
2846 return (error);
2847 }
2848 static char hextoasc[] = "0123456789abcdef";
2849
2850 /*
2851 * Silly rename. To make the NFS filesystem that is stateless look a little
2852 * more like the "ufs" a remove of an active vnode is translated to a rename
2853 * to a funny looking filename that is removed by nfs_inactive on the
2854 * nfsnode. There is the potential for another process on a different client
2855 * to create the same funny name between the nfs_lookitup() fails and the
2856 * nfs_rename() completes, but...
2857 */
2858 int
2859 nfs_sillyrename(dvp, vp, cnp)
2860 struct vnode *dvp, *vp;
2861 struct componentname *cnp;
2862 {
2863 struct sillyrename *sp;
2864 struct nfsnode *np;
2865 int error;
2866 short pid;
2867
2868 cache_purge(dvp);
2869 np = VTONFS(vp);
2870 #ifndef DIAGNOSTIC
2871 if (vp->v_type == VDIR)
2872 panic("nfs: sillyrename dir");
2873 #endif
2874 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2875 M_NFSREQ, M_WAITOK);
2876 sp->s_cred = crdup(cnp->cn_cred);
2877 sp->s_dvp = dvp;
2878 VREF(dvp);
2879
2880 /* Fudge together a funny name */
2881 pid = cnp->cn_proc->p_pid;
2882 memcpy(sp->s_name, ".nfsAxxxx4.4", 13);
2883 sp->s_namlen = 12;
2884 sp->s_name[8] = hextoasc[pid & 0xf];
2885 sp->s_name[7] = hextoasc[(pid >> 4) & 0xf];
2886 sp->s_name[6] = hextoasc[(pid >> 8) & 0xf];
2887 sp->s_name[5] = hextoasc[(pid >> 12) & 0xf];
2888
2889 /* Try lookitups until we get one that isn't there */
2890 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2891 cnp->cn_proc, (struct nfsnode **)0) == 0) {
2892 sp->s_name[4]++;
2893 if (sp->s_name[4] > 'z') {
2894 error = EINVAL;
2895 goto bad;
2896 }
2897 }
2898 error = nfs_renameit(dvp, cnp, sp);
2899 if (error)
2900 goto bad;
2901 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2902 cnp->cn_proc, &np);
2903 np->n_sillyrename = sp;
2904 return (0);
2905 bad:
2906 vrele(sp->s_dvp);
2907 crfree(sp->s_cred);
2908 free((caddr_t)sp, M_NFSREQ);
2909 return (error);
2910 }
2911
2912 /*
2913 * Look up a file name and optionally either update the file handle or
2914 * allocate an nfsnode, depending on the value of npp.
2915 * npp == NULL --> just do the lookup
2916 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2917 * handled too
2918 * *npp != NULL --> update the file handle in the vnode
2919 */
2920 int
2921 nfs_lookitup(dvp, name, len, cred, procp, npp)
2922 struct vnode *dvp;
2923 const char *name;
2924 int len;
2925 struct ucred *cred;
2926 struct proc *procp;
2927 struct nfsnode **npp;
2928 {
2929 u_int32_t *tl;
2930 caddr_t cp;
2931 int32_t t1, t2;
2932 struct vnode *newvp = (struct vnode *)0;
2933 struct nfsnode *np, *dnp = VTONFS(dvp);
2934 caddr_t bpos, dpos, cp2;
2935 int error = 0, fhlen, attrflag;
2936 struct mbuf *mreq, *mrep, *md, *mb;
2937 nfsfh_t *nfhp;
2938 const int v3 = NFS_ISV3(dvp);
2939
2940 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2941 nfsm_reqhead(dnp, NFSPROC_LOOKUP,
2942 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2943 nfsm_fhtom(dnp, v3);
2944 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2945 nfsm_request(dnp, NFSPROC_LOOKUP, procp, cred);
2946 if (npp && !error) {
2947 nfsm_getfh(nfhp, fhlen, v3);
2948 if (*npp) {
2949 np = *npp;
2950 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2951 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2952 np->n_fhp = &np->n_fh;
2953 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2954 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2955 memcpy((caddr_t)np->n_fhp, (caddr_t)nfhp, fhlen);
2956 np->n_fhsize = fhlen;
2957 newvp = NFSTOV(np);
2958 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2959 VREF(dvp);
2960 newvp = dvp;
2961 np = dnp;
2962 } else {
2963 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2964 if (error) {
2965 m_freem(mrep);
2966 return (error);
2967 }
2968 newvp = NFSTOV(np);
2969 }
2970 if (v3) {
2971 nfsm_postop_attr(newvp, attrflag, 0);
2972 if (!attrflag && *npp == NULL) {
2973 m_freem(mrep);
2974 vput(newvp);
2975 return (ENOENT);
2976 }
2977 } else
2978 nfsm_loadattr(newvp, (struct vattr *)0, 0);
2979 }
2980 nfsm_reqdone;
2981 if (npp && *npp == NULL) {
2982 if (error) {
2983 if (newvp)
2984 vput(newvp);
2985 } else
2986 *npp = np;
2987 }
2988 return (error);
2989 }
2990
2991 /*
2992 * Nfs Version 3 commit rpc
2993 */
2994 int
2995 nfs_commit(vp, offset, cnt, procp)
2996 struct vnode *vp;
2997 off_t offset;
2998 uint32_t cnt;
2999 struct proc *procp;
3000 {
3001 caddr_t cp;
3002 u_int32_t *tl;
3003 int32_t t1, t2;
3004 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3005 caddr_t bpos, dpos, cp2;
3006 int error = 0, wccflag = NFSV3_WCCRATTR;
3007 struct mbuf *mreq, *mrep, *md, *mb;
3008 struct nfsnode *np;
3009
3010 KASSERT(NFS_ISV3(vp));
3011
3012 #ifdef NFS_DEBUG_COMMIT
3013 printf("commit %lu - %lu\n", (unsigned long)offset,
3014 (unsigned long)(offset + cnt));
3015 #endif
3016
3017 simple_lock(&nmp->nm_slock);
3018 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0) {
3019 simple_unlock(&nmp->nm_slock);
3020 return (0);
3021 }
3022 simple_unlock(&nmp->nm_slock);
3023 nfsstats.rpccnt[NFSPROC_COMMIT]++;
3024 np = VTONFS(vp);
3025 nfsm_reqhead(np, NFSPROC_COMMIT, NFSX_FH(1));
3026 nfsm_fhtom(np, 1);
3027 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3028 txdr_hyper(offset, tl);
3029 tl += 2;
3030 *tl = txdr_unsigned(cnt);
3031 nfsm_request(np, NFSPROC_COMMIT, procp, np->n_wcred);
3032 nfsm_wcc_data(vp, wccflag, 0);
3033 if (!error) {
3034 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
3035 simple_lock(&nmp->nm_slock);
3036 if ((nmp->nm_iflag & NFSMNT_STALEWRITEVERF) ||
3037 memcmp(nmp->nm_writeverf, tl, NFSX_V3WRITEVERF)) {
3038 memcpy(nmp->nm_writeverf, tl, NFSX_V3WRITEVERF);
3039 error = NFSERR_STALEWRITEVERF;
3040 nmp->nm_iflag |= NFSMNT_STALEWRITEVERF;
3041 }
3042 simple_unlock(&nmp->nm_slock);
3043 }
3044 nfsm_reqdone;
3045 return (error);
3046 }
3047
3048 /*
3049 * Kludge City..
3050 * - make nfs_bmap() essentially a no-op that does no translation
3051 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
3052 * (Maybe I could use the process's page mapping, but I was concerned that
3053 * Kernel Write might not be enabled and also figured copyout() would do
3054 * a lot more work than memcpy() and also it currently happens in the
3055 * context of the swapper process (2).
3056 */
3057 int
3058 nfs_bmap(v)
3059 void *v;
3060 {
3061 struct vop_bmap_args /* {
3062 struct vnode *a_vp;
3063 daddr_t a_bn;
3064 struct vnode **a_vpp;
3065 daddr_t *a_bnp;
3066 int *a_runp;
3067 } */ *ap = v;
3068 struct vnode *vp = ap->a_vp;
3069 int bshift = vp->v_mount->mnt_fs_bshift - vp->v_mount->mnt_dev_bshift;
3070
3071 if (ap->a_vpp != NULL)
3072 *ap->a_vpp = vp;
3073 if (ap->a_bnp != NULL)
3074 *ap->a_bnp = ap->a_bn << bshift;
3075 if (ap->a_runp != NULL)
3076 *ap->a_runp = 1024 * 1024; /* XXX */
3077 return (0);
3078 }
3079
3080 /*
3081 * Strategy routine.
3082 * For async requests when nfsiod(s) are running, queue the request by
3083 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
3084 * request.
3085 */
3086 int
3087 nfs_strategy(v)
3088 void *v;
3089 {
3090 struct vop_strategy_args *ap = v;
3091 struct buf *bp = ap->a_bp;
3092 struct proc *p;
3093 int error = 0;
3094
3095 if ((bp->b_flags & (B_PHYS|B_ASYNC)) == (B_PHYS|B_ASYNC))
3096 panic("nfs physio/async");
3097 if (bp->b_flags & B_ASYNC)
3098 p = NULL;
3099 else
3100 p = curproc; /* XXX */
3101
3102 /*
3103 * If the op is asynchronous and an i/o daemon is waiting
3104 * queue the request, wake it up and wait for completion
3105 * otherwise just do it ourselves.
3106 */
3107
3108 if ((bp->b_flags & B_ASYNC) == 0 ||
3109 nfs_asyncio(bp))
3110 error = nfs_doio(bp, p);
3111 return (error);
3112 }
3113
3114 /*
3115 * fsync vnode op. Just call nfs_flush() with commit == 1.
3116 */
3117 /* ARGSUSED */
3118 int
3119 nfs_fsync(v)
3120 void *v;
3121 {
3122 struct vop_fsync_args /* {
3123 struct vnodeop_desc *a_desc;
3124 struct vnode * a_vp;
3125 struct ucred * a_cred;
3126 int a_flags;
3127 off_t offlo;
3128 off_t offhi;
3129 struct proc * a_p;
3130 } */ *ap = v;
3131
3132 struct vnode *vp = ap->a_vp;
3133
3134 if (vp->v_type != VREG)
3135 return 0;
3136
3137 return (nfs_flush(vp, ap->a_cred,
3138 (ap->a_flags & FSYNC_WAIT) != 0 ? MNT_WAIT : 0, ap->a_p, 1));
3139 }
3140
3141 /*
3142 * Flush all the data associated with a vnode.
3143 */
3144 int
3145 nfs_flush(vp, cred, waitfor, p, commit)
3146 struct vnode *vp;
3147 struct ucred *cred;
3148 int waitfor;
3149 struct proc *p;
3150 int commit;
3151 {
3152 struct nfsnode *np = VTONFS(vp);
3153 int error;
3154 int flushflags = PGO_ALLPAGES|PGO_CLEANIT|PGO_SYNCIO;
3155 UVMHIST_FUNC("nfs_flush"); UVMHIST_CALLED(ubchist);
3156
3157 simple_lock(&vp->v_interlock);
3158 error = VOP_PUTPAGES(vp, 0, 0, flushflags);
3159 if (np->n_flag & NWRITEERR) {
3160 error = np->n_error;
3161 np->n_flag &= ~NWRITEERR;
3162 }
3163 UVMHIST_LOG(ubchist, "returning %d", error,0,0,0);
3164 return (error);
3165 }
3166
3167 /*
3168 * Return POSIX pathconf information applicable to nfs.
3169 *
3170 * N.B. The NFS V2 protocol doesn't support this RPC.
3171 */
3172 /* ARGSUSED */
3173 int
3174 nfs_pathconf(v)
3175 void *v;
3176 {
3177 struct vop_pathconf_args /* {
3178 struct vnode *a_vp;
3179 int a_name;
3180 register_t *a_retval;
3181 } */ *ap = v;
3182 struct nfsv3_pathconf *pcp;
3183 struct vnode *vp = ap->a_vp;
3184 struct mbuf *mreq, *mrep, *md, *mb;
3185 int32_t t1, t2;
3186 u_int32_t *tl;
3187 caddr_t bpos, dpos, cp, cp2;
3188 int error = 0, attrflag;
3189 #ifndef NFS_V2_ONLY
3190 struct nfsmount *nmp;
3191 unsigned int l;
3192 u_int64_t maxsize;
3193 #endif
3194 const int v3 = NFS_ISV3(vp);
3195 struct nfsnode *np = VTONFS(vp);
3196
3197 switch (ap->a_name) {
3198 /* Names that can be resolved locally. */
3199 case _PC_PIPE_BUF:
3200 *ap->a_retval = PIPE_BUF;
3201 break;
3202 case _PC_SYNC_IO:
3203 *ap->a_retval = 1;
3204 break;
3205 /* Names that cannot be resolved locally; do an RPC, if possible. */
3206 case _PC_LINK_MAX:
3207 case _PC_NAME_MAX:
3208 case _PC_CHOWN_RESTRICTED:
3209 case _PC_NO_TRUNC:
3210 if (!v3) {
3211 error = EINVAL;
3212 break;
3213 }
3214 nfsstats.rpccnt[NFSPROC_PATHCONF]++;
3215 nfsm_reqhead(np, NFSPROC_PATHCONF, NFSX_FH(1));
3216 nfsm_fhtom(np, 1);
3217 nfsm_request(np, NFSPROC_PATHCONF,
3218 curproc, curproc->p_ucred); /* XXX */
3219 nfsm_postop_attr(vp, attrflag, 0);
3220 if (!error) {
3221 nfsm_dissect(pcp, struct nfsv3_pathconf *,
3222 NFSX_V3PATHCONF);
3223 switch (ap->a_name) {
3224 case _PC_LINK_MAX:
3225 *ap->a_retval =
3226 fxdr_unsigned(register_t, pcp->pc_linkmax);
3227 break;
3228 case _PC_NAME_MAX:
3229 *ap->a_retval =
3230 fxdr_unsigned(register_t, pcp->pc_namemax);
3231 break;
3232 case _PC_CHOWN_RESTRICTED:
3233 *ap->a_retval =
3234 (pcp->pc_chownrestricted == nfs_true);
3235 break;
3236 case _PC_NO_TRUNC:
3237 *ap->a_retval =
3238 (pcp->pc_notrunc == nfs_true);
3239 break;
3240 }
3241 }
3242 nfsm_reqdone;
3243 break;
3244 case _PC_FILESIZEBITS:
3245 #ifndef NFS_V2_ONLY
3246 if (v3) {
3247 nmp = VFSTONFS(vp->v_mount);
3248 if ((nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
3249 if ((error = nfs_fsinfo(nmp, vp,
3250 curproc->p_ucred, curproc)) != 0) /* XXX */
3251 break;
3252 for (l = 0, maxsize = nmp->nm_maxfilesize;
3253 (maxsize >> l) > 0; l++)
3254 ;
3255 *ap->a_retval = l + 1;
3256 } else
3257 #endif
3258 {
3259 *ap->a_retval = 32; /* NFS V2 limitation */
3260 }
3261 break;
3262 default:
3263 error = EINVAL;
3264 break;
3265 }
3266
3267 return (error);
3268 }
3269
3270 /*
3271 * NFS advisory byte-level locks.
3272 */
3273 int
3274 nfs_advlock(v)
3275 void *v;
3276 {
3277 struct vop_advlock_args /* {
3278 struct vnode *a_vp;
3279 caddr_t a_id;
3280 int a_op;
3281 struct flock *a_fl;
3282 int a_flags;
3283 } */ *ap = v;
3284 struct nfsnode *np = VTONFS(ap->a_vp);
3285
3286 return lf_advlock(ap, &np->n_lockf, np->n_size);
3287 }
3288
3289 /*
3290 * Print out the contents of an nfsnode.
3291 */
3292 int
3293 nfs_print(v)
3294 void *v;
3295 {
3296 struct vop_print_args /* {
3297 struct vnode *a_vp;
3298 } */ *ap = v;
3299 struct vnode *vp = ap->a_vp;
3300 struct nfsnode *np = VTONFS(vp);
3301
3302 printf("tag VT_NFS, fileid %ld fsid 0x%lx",
3303 np->n_vattr->va_fileid, np->n_vattr->va_fsid);
3304 if (vp->v_type == VFIFO)
3305 fifo_printinfo(vp);
3306 printf("\n");
3307 return (0);
3308 }
3309
3310 /*
3311 * NFS file truncation.
3312 */
3313 int
3314 nfs_truncate(v)
3315 void *v;
3316 {
3317 #if 0
3318 struct vop_truncate_args /* {
3319 struct vnode *a_vp;
3320 off_t a_length;
3321 int a_flags;
3322 struct ucred *a_cred;
3323 struct proc *a_p;
3324 } */ *ap = v;
3325 #endif
3326
3327 /* Use nfs_setattr */
3328 return (EOPNOTSUPP);
3329 }
3330
3331 /*
3332 * NFS update.
3333 */
3334 int
3335 nfs_update(v)
3336 void *v;
3337 #if 0
3338 struct vop_update_args /* {
3339 struct vnode *a_vp;
3340 struct timespec *a_ta;
3341 struct timespec *a_tm;
3342 int a_waitfor;
3343 } */ *ap = v;
3344 #endif
3345 {
3346
3347 /* Use nfs_setattr */
3348 return (EOPNOTSUPP);
3349 }
3350
3351 /*
3352 * Just call bwrite().
3353 */
3354 int
3355 nfs_bwrite(v)
3356 void *v;
3357 {
3358 struct vop_bwrite_args /* {
3359 struct vnode *a_bp;
3360 } */ *ap = v;
3361
3362 return (bwrite(ap->a_bp));
3363 }
3364
3365 /*
3366 * nfs unlock wrapper.
3367 */
3368 int
3369 nfs_unlock(void *v)
3370 {
3371 struct vop_unlock_args /* {
3372 struct vnode *a_vp;
3373 int a_flags;
3374 } */ *ap = v;
3375 struct vnode *vp = ap->a_vp;
3376
3377 /*
3378 * VOP_UNLOCK can be called by nfs_loadattrcache
3379 * with v_data == 0.
3380 */
3381 if (VTONFS(vp)) {
3382 nfs_delayedtruncate(vp);
3383 }
3384
3385 return genfs_unlock(v);
3386 }
3387
3388 /*
3389 * nfs special file access vnode op.
3390 * Essentially just get vattr and then imitate iaccess() since the device is
3391 * local to the client.
3392 */
3393 int
3394 nfsspec_access(v)
3395 void *v;
3396 {
3397 struct vop_access_args /* {
3398 struct vnode *a_vp;
3399 int a_mode;
3400 struct ucred *a_cred;
3401 struct proc *a_p;
3402 } */ *ap = v;
3403 struct vattr va;
3404 struct vnode *vp = ap->a_vp;
3405 int error;
3406
3407 error = VOP_GETATTR(vp, &va, ap->a_cred, ap->a_p);
3408 if (error)
3409 return (error);
3410
3411 /*
3412 * Disallow write attempts on filesystems mounted read-only;
3413 * unless the file is a socket, fifo, or a block or character
3414 * device resident on the filesystem.
3415 */
3416 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3417 switch (vp->v_type) {
3418 case VREG:
3419 case VDIR:
3420 case VLNK:
3421 return (EROFS);
3422 default:
3423 break;
3424 }
3425 }
3426
3427 return (vaccess(va.va_type, va.va_mode,
3428 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
3429 }
3430
3431 /*
3432 * Read wrapper for special devices.
3433 */
3434 int
3435 nfsspec_read(v)
3436 void *v;
3437 {
3438 struct vop_read_args /* {
3439 struct vnode *a_vp;
3440 struct uio *a_uio;
3441 int a_ioflag;
3442 struct ucred *a_cred;
3443 } */ *ap = v;
3444 struct nfsnode *np = VTONFS(ap->a_vp);
3445
3446 /*
3447 * Set access flag.
3448 */
3449 np->n_flag |= NACC;
3450 np->n_atim.tv_sec = time.tv_sec;
3451 np->n_atim.tv_nsec = time.tv_usec * 1000;
3452 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3453 }
3454
3455 /*
3456 * Write wrapper for special devices.
3457 */
3458 int
3459 nfsspec_write(v)
3460 void *v;
3461 {
3462 struct vop_write_args /* {
3463 struct vnode *a_vp;
3464 struct uio *a_uio;
3465 int a_ioflag;
3466 struct ucred *a_cred;
3467 } */ *ap = v;
3468 struct nfsnode *np = VTONFS(ap->a_vp);
3469
3470 /*
3471 * Set update flag.
3472 */
3473 np->n_flag |= NUPD;
3474 np->n_mtim.tv_sec = time.tv_sec;
3475 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3476 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3477 }
3478
3479 /*
3480 * Close wrapper for special devices.
3481 *
3482 * Update the times on the nfsnode then do device close.
3483 */
3484 int
3485 nfsspec_close(v)
3486 void *v;
3487 {
3488 struct vop_close_args /* {
3489 struct vnode *a_vp;
3490 int a_fflag;
3491 struct ucred *a_cred;
3492 struct proc *a_p;
3493 } */ *ap = v;
3494 struct vnode *vp = ap->a_vp;
3495 struct nfsnode *np = VTONFS(vp);
3496 struct vattr vattr;
3497
3498 if (np->n_flag & (NACC | NUPD)) {
3499 np->n_flag |= NCHG;
3500 if (vp->v_usecount == 1 &&
3501 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3502 VATTR_NULL(&vattr);
3503 if (np->n_flag & NACC)
3504 vattr.va_atime = np->n_atim;
3505 if (np->n_flag & NUPD)
3506 vattr.va_mtime = np->n_mtim;
3507 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
3508 }
3509 }
3510 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3511 }
3512
3513 /*
3514 * Read wrapper for fifos.
3515 */
3516 int
3517 nfsfifo_read(v)
3518 void *v;
3519 {
3520 struct vop_read_args /* {
3521 struct vnode *a_vp;
3522 struct uio *a_uio;
3523 int a_ioflag;
3524 struct ucred *a_cred;
3525 } */ *ap = v;
3526 struct nfsnode *np = VTONFS(ap->a_vp);
3527
3528 /*
3529 * Set access flag.
3530 */
3531 np->n_flag |= NACC;
3532 np->n_atim.tv_sec = time.tv_sec;
3533 np->n_atim.tv_nsec = time.tv_usec * 1000;
3534 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3535 }
3536
3537 /*
3538 * Write wrapper for fifos.
3539 */
3540 int
3541 nfsfifo_write(v)
3542 void *v;
3543 {
3544 struct vop_write_args /* {
3545 struct vnode *a_vp;
3546 struct uio *a_uio;
3547 int a_ioflag;
3548 struct ucred *a_cred;
3549 } */ *ap = v;
3550 struct nfsnode *np = VTONFS(ap->a_vp);
3551
3552 /*
3553 * Set update flag.
3554 */
3555 np->n_flag |= NUPD;
3556 np->n_mtim.tv_sec = time.tv_sec;
3557 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3558 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3559 }
3560
3561 /*
3562 * Close wrapper for fifos.
3563 *
3564 * Update the times on the nfsnode then do fifo close.
3565 */
3566 int
3567 nfsfifo_close(v)
3568 void *v;
3569 {
3570 struct vop_close_args /* {
3571 struct vnode *a_vp;
3572 int a_fflag;
3573 struct ucred *a_cred;
3574 struct proc *a_p;
3575 } */ *ap = v;
3576 struct vnode *vp = ap->a_vp;
3577 struct nfsnode *np = VTONFS(vp);
3578 struct vattr vattr;
3579
3580 if (np->n_flag & (NACC | NUPD)) {
3581 if (np->n_flag & NACC) {
3582 np->n_atim.tv_sec = time.tv_sec;
3583 np->n_atim.tv_nsec = time.tv_usec * 1000;
3584 }
3585 if (np->n_flag & NUPD) {
3586 np->n_mtim.tv_sec = time.tv_sec;
3587 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3588 }
3589 np->n_flag |= NCHG;
3590 if (vp->v_usecount == 1 &&
3591 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3592 VATTR_NULL(&vattr);
3593 if (np->n_flag & NACC)
3594 vattr.va_atime = np->n_atim;
3595 if (np->n_flag & NUPD)
3596 vattr.va_mtime = np->n_mtim;
3597 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
3598 }
3599 }
3600 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
3601 }
3602