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