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