nfs_vnops.c revision 1.193 1 /* $NetBSD: nfs_vnops.c,v 1.193 2004/05/07 16:09:46 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.193 2004/05/07 16:09:46 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)
2217 error = EEXIST;
2218 }
2219 }
2220 if (error) {
2221 if (newvp)
2222 vput(newvp);
2223 } else {
2224 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
2225 if (cnp->cn_flags & MAKEENTRY)
2226 nfs_cache_enter(dvp, newvp, cnp);
2227 *ap->a_vpp = newvp;
2228 }
2229 PNBUF_PUT(cnp->cn_pnbuf);
2230 vput(dvp);
2231 return (error);
2232 }
2233
2234 /*
2235 * nfs remove directory call
2236 */
2237 int
2238 nfs_rmdir(v)
2239 void *v;
2240 {
2241 struct vop_rmdir_args /* {
2242 struct vnode *a_dvp;
2243 struct vnode *a_vp;
2244 struct componentname *a_cnp;
2245 } */ *ap = v;
2246 struct vnode *vp = ap->a_vp;
2247 struct vnode *dvp = ap->a_dvp;
2248 struct componentname *cnp = ap->a_cnp;
2249 u_int32_t *tl;
2250 caddr_t cp;
2251 int32_t t1, t2;
2252 caddr_t bpos, dpos, cp2;
2253 int error = 0, wccflag = NFSV3_WCCRATTR;
2254 struct mbuf *mreq, *mrep, *md, *mb;
2255 const int v3 = NFS_ISV3(dvp);
2256 struct nfsnode *dnp;
2257
2258 if (dvp == vp) {
2259 vrele(dvp);
2260 vput(dvp);
2261 PNBUF_PUT(cnp->cn_pnbuf);
2262 return (EINVAL);
2263 }
2264 nfsstats.rpccnt[NFSPROC_RMDIR]++;
2265 dnp = VTONFS(dvp);
2266 nfsm_reqhead(dnp, NFSPROC_RMDIR,
2267 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2268 nfsm_fhtom(dnp, v3);
2269 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2270 nfsm_request(dnp, NFSPROC_RMDIR, cnp->cn_proc, cnp->cn_cred);
2271 if (v3)
2272 nfsm_wcc_data(dvp, wccflag, 0);
2273 nfsm_reqdone;
2274 PNBUF_PUT(cnp->cn_pnbuf);
2275 VTONFS(dvp)->n_flag |= NMODIFIED;
2276 if (!wccflag)
2277 NFS_INVALIDATE_ATTRCACHE(VTONFS(dvp));
2278 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
2279 VN_KNOTE(vp, NOTE_DELETE);
2280 cache_purge(vp);
2281 vput(vp);
2282 vput(dvp);
2283 /*
2284 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2285 */
2286 if (error == ENOENT)
2287 error = 0;
2288 return (error);
2289 }
2290
2291 /*
2292 * nfs readdir call
2293 */
2294 int
2295 nfs_readdir(v)
2296 void *v;
2297 {
2298 struct vop_readdir_args /* {
2299 struct vnode *a_vp;
2300 struct uio *a_uio;
2301 struct ucred *a_cred;
2302 int *a_eofflag;
2303 off_t **a_cookies;
2304 int *a_ncookies;
2305 } */ *ap = v;
2306 struct vnode *vp = ap->a_vp;
2307 struct uio *uio = ap->a_uio;
2308 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2309 char *base = uio->uio_iov->iov_base;
2310 int tresid, error;
2311 size_t count, lost;
2312 struct dirent *dp;
2313 off_t *cookies = NULL;
2314 int ncookies = 0, nc;
2315
2316 if (vp->v_type != VDIR)
2317 return (EPERM);
2318
2319 lost = uio->uio_resid & (NFS_DIRFRAGSIZ - 1);
2320 count = uio->uio_resid - lost;
2321 if (count <= 0)
2322 return (EINVAL);
2323
2324 /*
2325 * Call nfs_bioread() to do the real work.
2326 */
2327 tresid = uio->uio_resid = count;
2328 error = nfs_bioread(vp, uio, 0, ap->a_cred,
2329 ap->a_cookies ? NFSBIO_CACHECOOKIES : 0);
2330
2331 if (!error && ap->a_cookies) {
2332 ncookies = count / 16;
2333 cookies = malloc(sizeof (off_t) * ncookies, M_TEMP, M_WAITOK);
2334 *ap->a_cookies = cookies;
2335 }
2336
2337 if (!error && uio->uio_resid == tresid) {
2338 uio->uio_resid += lost;
2339 nfsstats.direofcache_misses++;
2340 if (ap->a_cookies)
2341 *ap->a_ncookies = 0;
2342 *ap->a_eofflag = 1;
2343 return (0);
2344 }
2345
2346 if (!error && ap->a_cookies) {
2347 /*
2348 * Only the NFS server and emulations use cookies, and they
2349 * load the directory block into system space, so we can
2350 * just look at it directly.
2351 */
2352 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2353 panic("nfs_readdir: lost in space");
2354 for (nc = 0; ncookies-- &&
2355 base < (char *)uio->uio_iov->iov_base; nc++){
2356 dp = (struct dirent *) base;
2357 if (dp->d_reclen == 0)
2358 break;
2359 if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
2360 *(cookies++) = (off_t)NFS_GETCOOKIE32(dp);
2361 else
2362 *(cookies++) = NFS_GETCOOKIE(dp);
2363 base += dp->d_reclen;
2364 }
2365 uio->uio_resid +=
2366 ((caddr_t)uio->uio_iov->iov_base - base);
2367 uio->uio_iov->iov_len +=
2368 ((caddr_t)uio->uio_iov->iov_base - base);
2369 uio->uio_iov->iov_base = base;
2370 *ap->a_ncookies = nc;
2371 }
2372
2373 uio->uio_resid += lost;
2374 *ap->a_eofflag = 0;
2375 return (error);
2376 }
2377
2378 /*
2379 * Readdir rpc call.
2380 * Called from below the buffer cache by nfs_doio().
2381 */
2382 int
2383 nfs_readdirrpc(vp, uiop, cred)
2384 struct vnode *vp;
2385 struct uio *uiop;
2386 struct ucred *cred;
2387 {
2388 int len, left;
2389 struct dirent *dp = NULL;
2390 u_int32_t *tl;
2391 caddr_t cp;
2392 int32_t t1, t2;
2393 caddr_t bpos, dpos, cp2;
2394 struct mbuf *mreq, *mrep, *md, *mb;
2395 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2396 struct nfsnode *dnp = VTONFS(vp);
2397 u_quad_t fileno;
2398 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2399 int attrflag, nrpcs = 0, reclen;
2400 const int v3 = NFS_ISV3(vp);
2401
2402 #ifdef DIAGNOSTIC
2403 /*
2404 * Should be called from buffer cache, so only amount of
2405 * NFS_DIRBLKSIZ will be requested.
2406 */
2407 if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1)))
2408 panic("nfs readdirrpc bad uio");
2409 #endif
2410
2411 /*
2412 * Loop around doing readdir rpc's of size nm_readdirsize
2413 * truncated to a multiple of NFS_DIRFRAGSIZ.
2414 * The stopping criteria is EOF or buffer full.
2415 */
2416 while (more_dirs && bigenough) {
2417 /*
2418 * Heuristic: don't bother to do another RPC to further
2419 * fill up this block if there is not much room left. (< 50%
2420 * of the readdir RPC size). This wastes some buffer space
2421 * but can save up to 50% in RPC calls.
2422 */
2423 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
2424 bigenough = 0;
2425 break;
2426 }
2427 nfsstats.rpccnt[NFSPROC_READDIR]++;
2428 nfsm_reqhead(dnp, NFSPROC_READDIR, NFSX_FH(v3) +
2429 NFSX_READDIR(v3));
2430 nfsm_fhtom(dnp, v3);
2431 if (v3) {
2432 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2433 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2434 txdr_swapcookie3(uiop->uio_offset, tl);
2435 } else {
2436 txdr_cookie3(uiop->uio_offset, tl);
2437 }
2438 tl += 2;
2439 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2440 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2441 } else {
2442 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2443 *tl++ = txdr_unsigned(uiop->uio_offset);
2444 }
2445 *tl = txdr_unsigned(nmp->nm_readdirsize);
2446 nfsm_request(dnp, NFSPROC_READDIR, uiop->uio_procp, cred);
2447 nrpcs++;
2448 if (v3) {
2449 nfsm_postop_attr(vp, attrflag, 0);
2450 if (!error) {
2451 nfsm_dissect(tl, u_int32_t *,
2452 2 * NFSX_UNSIGNED);
2453 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2454 dnp->n_cookieverf.nfsuquad[1] = *tl;
2455 } else {
2456 m_freem(mrep);
2457 goto nfsmout;
2458 }
2459 }
2460 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2461 more_dirs = fxdr_unsigned(int, *tl);
2462
2463 /* loop thru the dir entries, doctoring them to 4bsd form */
2464 while (more_dirs && bigenough) {
2465 if (v3) {
2466 nfsm_dissect(tl, u_int32_t *,
2467 3 * NFSX_UNSIGNED);
2468 fileno = fxdr_hyper(tl);
2469 len = fxdr_unsigned(int, *(tl + 2));
2470 } else {
2471 nfsm_dissect(tl, u_int32_t *,
2472 2 * NFSX_UNSIGNED);
2473 fileno = fxdr_unsigned(u_quad_t, *tl++);
2474 len = fxdr_unsigned(int, *tl);
2475 }
2476 if (len <= 0 || len > NFS_MAXNAMLEN) {
2477 error = EBADRPC;
2478 m_freem(mrep);
2479 goto nfsmout;
2480 }
2481 tlen = nfsm_rndup(len);
2482 if (tlen == len)
2483 tlen += 4; /* To ensure null termination */
2484 tlen += sizeof (off_t) + sizeof (int);
2485 reclen = ALIGN(tlen + DIRHDSIZ);
2486 tlen = reclen - DIRHDSIZ;
2487 left = NFS_DIRFRAGSIZ - blksiz;
2488 if (reclen > left) {
2489 dp->d_reclen += left;
2490 uiop->uio_iov->iov_base =
2491 (caddr_t)uiop->uio_iov->iov_base + left;
2492 uiop->uio_iov->iov_len -= left;
2493 uiop->uio_resid -= left;
2494 blksiz = 0;
2495 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2496 }
2497 if (reclen > uiop->uio_resid)
2498 bigenough = 0;
2499 if (bigenough) {
2500 dp = (struct dirent *)uiop->uio_iov->iov_base;
2501 dp->d_fileno = (int)fileno;
2502 dp->d_namlen = len;
2503 dp->d_reclen = reclen;
2504 dp->d_type = DT_UNKNOWN;
2505 blksiz += dp->d_reclen;
2506 if (blksiz == NFS_DIRFRAGSIZ)
2507 blksiz = 0;
2508 uiop->uio_resid -= DIRHDSIZ;
2509 uiop->uio_iov->iov_base =
2510 (caddr_t)uiop->uio_iov->iov_base + DIRHDSIZ;
2511 uiop->uio_iov->iov_len -= DIRHDSIZ;
2512 nfsm_mtouio(uiop, len);
2513 cp = uiop->uio_iov->iov_base;
2514 tlen -= len;
2515 *cp = '\0'; /* null terminate */
2516 uiop->uio_iov->iov_base =
2517 (caddr_t)uiop->uio_iov->iov_base + tlen;
2518 uiop->uio_iov->iov_len -= tlen;
2519 uiop->uio_resid -= tlen;
2520 } else
2521 nfsm_adv(nfsm_rndup(len));
2522 if (v3) {
2523 nfsm_dissect(tl, u_int32_t *,
2524 3 * NFSX_UNSIGNED);
2525 } else {
2526 nfsm_dissect(tl, u_int32_t *,
2527 2 * NFSX_UNSIGNED);
2528 }
2529 if (bigenough) {
2530 if (v3) {
2531 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2532 uiop->uio_offset =
2533 fxdr_swapcookie3(tl);
2534 else
2535 uiop->uio_offset =
2536 fxdr_cookie3(tl);
2537 }
2538 else {
2539 uiop->uio_offset =
2540 fxdr_unsigned(off_t, *tl);
2541 }
2542 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2543 }
2544 if (v3)
2545 tl += 2;
2546 else
2547 tl++;
2548 more_dirs = fxdr_unsigned(int, *tl);
2549 }
2550 /*
2551 * If at end of rpc data, get the eof boolean
2552 */
2553 if (!more_dirs) {
2554 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2555 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2556 }
2557 m_freem(mrep);
2558 }
2559 /*
2560 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2561 * by increasing d_reclen for the last record.
2562 */
2563 if (blksiz > 0) {
2564 left = NFS_DIRFRAGSIZ - blksiz;
2565 dp->d_reclen += left;
2566 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2567 uiop->uio_iov->iov_base = (caddr_t)uiop->uio_iov->iov_base +
2568 left;
2569 uiop->uio_iov->iov_len -= left;
2570 uiop->uio_resid -= left;
2571 }
2572
2573 /*
2574 * We are now either at the end of the directory or have filled the
2575 * block.
2576 */
2577 if (bigenough)
2578 dnp->n_direofoffset = uiop->uio_offset;
2579 nfsmout:
2580 return (error);
2581 }
2582
2583 /*
2584 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2585 */
2586 int
2587 nfs_readdirplusrpc(vp, uiop, cred)
2588 struct vnode *vp;
2589 struct uio *uiop;
2590 struct ucred *cred;
2591 {
2592 int len, left;
2593 struct dirent *dp = NULL;
2594 u_int32_t *tl;
2595 caddr_t cp;
2596 int32_t t1, t2;
2597 struct vnode *newvp;
2598 caddr_t bpos, dpos, cp2;
2599 struct mbuf *mreq, *mrep, *md, *mb;
2600 struct nameidata nami, *ndp = &nami;
2601 struct componentname *cnp = &ndp->ni_cnd;
2602 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2603 struct nfsnode *dnp = VTONFS(vp), *np;
2604 nfsfh_t *fhp;
2605 u_quad_t fileno;
2606 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2607 int attrflag, fhsize, nrpcs = 0, reclen;
2608 struct nfs_fattr fattr, *fp;
2609
2610 #ifdef DIAGNOSTIC
2611 if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1)))
2612 panic("nfs readdirplusrpc bad uio");
2613 #endif
2614 ndp->ni_dvp = vp;
2615 newvp = NULLVP;
2616
2617 /*
2618 * Loop around doing readdir rpc's of size nm_readdirsize
2619 * truncated to a multiple of NFS_DIRFRAGSIZ.
2620 * The stopping criteria is EOF or buffer full.
2621 */
2622 while (more_dirs && bigenough) {
2623 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
2624 bigenough = 0;
2625 break;
2626 }
2627 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2628 nfsm_reqhead(dnp, NFSPROC_READDIRPLUS,
2629 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2630 nfsm_fhtom(dnp, 1);
2631 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2632 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2633 txdr_swapcookie3(uiop->uio_offset, tl);
2634 } else {
2635 txdr_cookie3(uiop->uio_offset, tl);
2636 }
2637 tl += 2;
2638 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2639 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2640 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2641 *tl = txdr_unsigned(nmp->nm_rsize);
2642 nfsm_request(dnp, NFSPROC_READDIRPLUS, uiop->uio_procp, cred);
2643 nfsm_postop_attr(vp, attrflag, 0);
2644 if (error) {
2645 m_freem(mrep);
2646 goto nfsmout;
2647 }
2648 nrpcs++;
2649 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2650 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2651 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2652 more_dirs = fxdr_unsigned(int, *tl);
2653
2654 /* loop thru the dir entries, doctoring them to 4bsd form */
2655 while (more_dirs && bigenough) {
2656 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2657 fileno = fxdr_hyper(tl);
2658 len = fxdr_unsigned(int, *(tl + 2));
2659 if (len <= 0 || len > NFS_MAXNAMLEN) {
2660 error = EBADRPC;
2661 m_freem(mrep);
2662 goto nfsmout;
2663 }
2664 tlen = nfsm_rndup(len);
2665 if (tlen == len)
2666 tlen += 4; /* To ensure null termination*/
2667 tlen += sizeof (off_t) + sizeof (int);
2668 reclen = ALIGN(tlen + DIRHDSIZ);
2669 tlen = reclen - DIRHDSIZ;
2670 left = NFS_DIRFRAGSIZ - blksiz;
2671 if (reclen > left) {
2672 /*
2673 * DIRFRAGSIZ is aligned, no need to align
2674 * again here.
2675 */
2676 dp->d_reclen += left;
2677 uiop->uio_iov->iov_base =
2678 (caddr_t)uiop->uio_iov->iov_base + left;
2679 uiop->uio_iov->iov_len -= left;
2680 uiop->uio_resid -= left;
2681 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2682 blksiz = 0;
2683 }
2684 if (reclen > uiop->uio_resid)
2685 bigenough = 0;
2686 if (bigenough) {
2687 dp = (struct dirent *)uiop->uio_iov->iov_base;
2688 dp->d_fileno = (int)fileno;
2689 dp->d_namlen = len;
2690 dp->d_reclen = reclen;
2691 dp->d_type = DT_UNKNOWN;
2692 blksiz += dp->d_reclen;
2693 if (blksiz == NFS_DIRFRAGSIZ)
2694 blksiz = 0;
2695 uiop->uio_resid -= DIRHDSIZ;
2696 uiop->uio_iov->iov_base =
2697 (caddr_t)uiop->uio_iov->iov_base +
2698 DIRHDSIZ;
2699 uiop->uio_iov->iov_len -= DIRHDSIZ;
2700 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2701 cnp->cn_namelen = len;
2702 nfsm_mtouio(uiop, len);
2703 cp = uiop->uio_iov->iov_base;
2704 tlen -= len;
2705 *cp = '\0';
2706 uiop->uio_iov->iov_base =
2707 (caddr_t)uiop->uio_iov->iov_base + tlen;
2708 uiop->uio_iov->iov_len -= tlen;
2709 uiop->uio_resid -= tlen;
2710 } else
2711 nfsm_adv(nfsm_rndup(len));
2712 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2713 if (bigenough) {
2714 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2715 uiop->uio_offset =
2716 fxdr_swapcookie3(tl);
2717 else
2718 uiop->uio_offset =
2719 fxdr_cookie3(tl);
2720 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2721 }
2722 tl += 2;
2723
2724 /*
2725 * Since the attributes are before the file handle
2726 * (sigh), we must skip over the attributes and then
2727 * come back and get them.
2728 */
2729 attrflag = fxdr_unsigned(int, *tl);
2730 if (attrflag) {
2731 nfsm_dissect(fp, struct nfs_fattr *, NFSX_V3FATTR);
2732 memcpy(&fattr, fp, NFSX_V3FATTR);
2733 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2734 doit = fxdr_unsigned(int, *tl);
2735 if (doit) {
2736 nfsm_getfh(fhp, fhsize, 1);
2737 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2738 VREF(vp);
2739 newvp = vp;
2740 np = dnp;
2741 } else {
2742 error = nfs_nget1(vp->v_mount, fhp,
2743 fhsize, &np, LK_NOWAIT);
2744 if (!error)
2745 newvp = NFSTOV(np);
2746 }
2747 if (!error) {
2748 const char *cp;
2749
2750 nfs_loadattrcache(&newvp, &fattr, 0, 0);
2751 dp->d_type =
2752 IFTODT(VTTOIF(np->n_vattr->va_type));
2753 ndp->ni_vp = newvp;
2754 cp = cnp->cn_nameptr + cnp->cn_namelen;
2755 cnp->cn_hash =
2756 namei_hash(cnp->cn_nameptr, &cp);
2757 if (cnp->cn_namelen <= NCHNAMLEN) {
2758 cache_purge1(ndp->ni_dvp, cnp, 0);
2759 nfs_cache_enter(ndp->ni_dvp, ndp->ni_vp,
2760 cnp);
2761 }
2762 }
2763 }
2764 } else {
2765 /* Just skip over the file handle */
2766 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2767 i = fxdr_unsigned(int, *tl);
2768 nfsm_adv(nfsm_rndup(i));
2769 }
2770 if (newvp != NULLVP) {
2771 if (newvp == vp)
2772 vrele(newvp);
2773 else
2774 vput(newvp);
2775 newvp = NULLVP;
2776 }
2777 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2778 more_dirs = fxdr_unsigned(int, *tl);
2779 }
2780 /*
2781 * If at end of rpc data, get the eof boolean
2782 */
2783 if (!more_dirs) {
2784 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2785 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2786 }
2787 m_freem(mrep);
2788 }
2789 /*
2790 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2791 * by increasing d_reclen for the last record.
2792 */
2793 if (blksiz > 0) {
2794 left = NFS_DIRFRAGSIZ - blksiz;
2795 dp->d_reclen += left;
2796 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2797 uiop->uio_iov->iov_base = (caddr_t)uiop->uio_iov->iov_base +
2798 left;
2799 uiop->uio_iov->iov_len -= left;
2800 uiop->uio_resid -= left;
2801 }
2802
2803 /*
2804 * We are now either at the end of the directory or have filled the
2805 * block.
2806 */
2807 if (bigenough)
2808 dnp->n_direofoffset = uiop->uio_offset;
2809 nfsmout:
2810 if (newvp != NULLVP) {
2811 if(newvp == vp)
2812 vrele(newvp);
2813 else
2814 vput(newvp);
2815 }
2816 return (error);
2817 }
2818 static char hextoasc[] = "0123456789abcdef";
2819
2820 /*
2821 * Silly rename. To make the NFS filesystem that is stateless look a little
2822 * more like the "ufs" a remove of an active vnode is translated to a rename
2823 * to a funny looking filename that is removed by nfs_inactive on the
2824 * nfsnode. There is the potential for another process on a different client
2825 * to create the same funny name between the nfs_lookitup() fails and the
2826 * nfs_rename() completes, but...
2827 */
2828 int
2829 nfs_sillyrename(dvp, vp, cnp)
2830 struct vnode *dvp, *vp;
2831 struct componentname *cnp;
2832 {
2833 struct sillyrename *sp;
2834 struct nfsnode *np;
2835 int error;
2836 short pid;
2837
2838 cache_purge(dvp);
2839 np = VTONFS(vp);
2840 #ifndef DIAGNOSTIC
2841 if (vp->v_type == VDIR)
2842 panic("nfs: sillyrename dir");
2843 #endif
2844 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2845 M_NFSREQ, M_WAITOK);
2846 sp->s_cred = crdup(cnp->cn_cred);
2847 sp->s_dvp = dvp;
2848 VREF(dvp);
2849
2850 /* Fudge together a funny name */
2851 pid = cnp->cn_proc->p_pid;
2852 memcpy(sp->s_name, ".nfsAxxxx4.4", 13);
2853 sp->s_namlen = 12;
2854 sp->s_name[8] = hextoasc[pid & 0xf];
2855 sp->s_name[7] = hextoasc[(pid >> 4) & 0xf];
2856 sp->s_name[6] = hextoasc[(pid >> 8) & 0xf];
2857 sp->s_name[5] = hextoasc[(pid >> 12) & 0xf];
2858
2859 /* Try lookitups until we get one that isn't there */
2860 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2861 cnp->cn_proc, (struct nfsnode **)0) == 0) {
2862 sp->s_name[4]++;
2863 if (sp->s_name[4] > 'z') {
2864 error = EINVAL;
2865 goto bad;
2866 }
2867 }
2868 error = nfs_renameit(dvp, cnp, sp);
2869 if (error)
2870 goto bad;
2871 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2872 cnp->cn_proc, &np);
2873 np->n_sillyrename = sp;
2874 return (0);
2875 bad:
2876 vrele(sp->s_dvp);
2877 crfree(sp->s_cred);
2878 free((caddr_t)sp, M_NFSREQ);
2879 return (error);
2880 }
2881
2882 /*
2883 * Look up a file name and optionally either update the file handle or
2884 * allocate an nfsnode, depending on the value of npp.
2885 * npp == NULL --> just do the lookup
2886 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2887 * handled too
2888 * *npp != NULL --> update the file handle in the vnode
2889 */
2890 int
2891 nfs_lookitup(dvp, name, len, cred, procp, npp)
2892 struct vnode *dvp;
2893 const char *name;
2894 int len;
2895 struct ucred *cred;
2896 struct proc *procp;
2897 struct nfsnode **npp;
2898 {
2899 u_int32_t *tl;
2900 caddr_t cp;
2901 int32_t t1, t2;
2902 struct vnode *newvp = (struct vnode *)0;
2903 struct nfsnode *np, *dnp = VTONFS(dvp);
2904 caddr_t bpos, dpos, cp2;
2905 int error = 0, fhlen, attrflag;
2906 struct mbuf *mreq, *mrep, *md, *mb;
2907 nfsfh_t *nfhp;
2908 const int v3 = NFS_ISV3(dvp);
2909
2910 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2911 nfsm_reqhead(dnp, NFSPROC_LOOKUP,
2912 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2913 nfsm_fhtom(dnp, v3);
2914 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2915 nfsm_request(dnp, NFSPROC_LOOKUP, procp, cred);
2916 if (npp && !error) {
2917 nfsm_getfh(nfhp, fhlen, v3);
2918 if (*npp) {
2919 np = *npp;
2920 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2921 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2922 np->n_fhp = &np->n_fh;
2923 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2924 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2925 memcpy((caddr_t)np->n_fhp, (caddr_t)nfhp, fhlen);
2926 np->n_fhsize = fhlen;
2927 newvp = NFSTOV(np);
2928 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2929 VREF(dvp);
2930 newvp = dvp;
2931 } else {
2932 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2933 if (error) {
2934 m_freem(mrep);
2935 return (error);
2936 }
2937 newvp = NFSTOV(np);
2938 }
2939 if (v3) {
2940 nfsm_postop_attr(newvp, attrflag, 0);
2941 if (!attrflag && *npp == NULL) {
2942 m_freem(mrep);
2943 vput(newvp);
2944 return (ENOENT);
2945 }
2946 } else
2947 nfsm_loadattr(newvp, (struct vattr *)0, 0);
2948 }
2949 nfsm_reqdone;
2950 if (npp && *npp == NULL) {
2951 if (error) {
2952 if (newvp)
2953 vput(newvp);
2954 } else
2955 *npp = np;
2956 }
2957 return (error);
2958 }
2959
2960 /*
2961 * Nfs Version 3 commit rpc
2962 */
2963 int
2964 nfs_commit(vp, offset, cnt, procp)
2965 struct vnode *vp;
2966 off_t offset;
2967 uint32_t cnt;
2968 struct proc *procp;
2969 {
2970 caddr_t cp;
2971 u_int32_t *tl;
2972 int32_t t1, t2;
2973 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2974 caddr_t bpos, dpos, cp2;
2975 int error = 0, wccflag = NFSV3_WCCRATTR;
2976 struct mbuf *mreq, *mrep, *md, *mb;
2977 struct nfsnode *np;
2978
2979 KASSERT(NFS_ISV3(vp));
2980
2981 #ifdef NFS_DEBUG_COMMIT
2982 printf("commit %lu - %lu\n", (unsigned long)offset,
2983 (unsigned long)(offset + cnt));
2984 #endif
2985
2986 simple_lock(&nmp->nm_slock);
2987 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0) {
2988 simple_unlock(&nmp->nm_slock);
2989 return (0);
2990 }
2991 simple_unlock(&nmp->nm_slock);
2992 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2993 np = VTONFS(vp);
2994 nfsm_reqhead(np, NFSPROC_COMMIT, NFSX_FH(1));
2995 nfsm_fhtom(np, 1);
2996 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2997 txdr_hyper(offset, tl);
2998 tl += 2;
2999 *tl = txdr_unsigned(cnt);
3000 nfsm_request(np, NFSPROC_COMMIT, procp, np->n_wcred);
3001 nfsm_wcc_data(vp, wccflag, 0);
3002 if (!error) {
3003 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
3004 simple_lock(&nmp->nm_slock);
3005 if ((nmp->nm_iflag & NFSMNT_STALEWRITEVERF) ||
3006 memcmp(nmp->nm_writeverf, tl, NFSX_V3WRITEVERF)) {
3007 memcpy(nmp->nm_writeverf, tl, NFSX_V3WRITEVERF);
3008 error = NFSERR_STALEWRITEVERF;
3009 nmp->nm_iflag |= NFSMNT_STALEWRITEVERF;
3010 }
3011 simple_unlock(&nmp->nm_slock);
3012 }
3013 nfsm_reqdone;
3014 return (error);
3015 }
3016
3017 /*
3018 * Kludge City..
3019 * - make nfs_bmap() essentially a no-op that does no translation
3020 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
3021 * (Maybe I could use the process's page mapping, but I was concerned that
3022 * Kernel Write might not be enabled and also figured copyout() would do
3023 * a lot more work than memcpy() and also it currently happens in the
3024 * context of the swapper process (2).
3025 */
3026 int
3027 nfs_bmap(v)
3028 void *v;
3029 {
3030 struct vop_bmap_args /* {
3031 struct vnode *a_vp;
3032 daddr_t a_bn;
3033 struct vnode **a_vpp;
3034 daddr_t *a_bnp;
3035 int *a_runp;
3036 } */ *ap = v;
3037 struct vnode *vp = ap->a_vp;
3038 int bshift = vp->v_mount->mnt_fs_bshift - vp->v_mount->mnt_dev_bshift;
3039
3040 if (ap->a_vpp != NULL)
3041 *ap->a_vpp = vp;
3042 if (ap->a_bnp != NULL)
3043 *ap->a_bnp = ap->a_bn << bshift;
3044 if (ap->a_runp != NULL)
3045 *ap->a_runp = 1024 * 1024; /* XXX */
3046 return (0);
3047 }
3048
3049 /*
3050 * Strategy routine.
3051 * For async requests when nfsiod(s) are running, queue the request by
3052 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
3053 * request.
3054 */
3055 int
3056 nfs_strategy(v)
3057 void *v;
3058 {
3059 struct vop_strategy_args *ap = v;
3060 struct buf *bp = ap->a_bp;
3061 struct proc *p;
3062 int error = 0;
3063
3064 if ((bp->b_flags & (B_PHYS|B_ASYNC)) == (B_PHYS|B_ASYNC))
3065 panic("nfs physio/async");
3066 if (bp->b_flags & B_ASYNC)
3067 p = NULL;
3068 else
3069 p = curproc; /* XXX */
3070
3071 /*
3072 * If the op is asynchronous and an i/o daemon is waiting
3073 * queue the request, wake it up and wait for completion
3074 * otherwise just do it ourselves.
3075 */
3076
3077 if ((bp->b_flags & B_ASYNC) == 0 ||
3078 nfs_asyncio(bp))
3079 error = nfs_doio(bp, p);
3080 return (error);
3081 }
3082
3083 /*
3084 * fsync vnode op. Just call nfs_flush() with commit == 1.
3085 */
3086 /* ARGSUSED */
3087 int
3088 nfs_fsync(v)
3089 void *v;
3090 {
3091 struct vop_fsync_args /* {
3092 struct vnodeop_desc *a_desc;
3093 struct vnode * a_vp;
3094 struct ucred * a_cred;
3095 int a_flags;
3096 off_t offlo;
3097 off_t offhi;
3098 struct proc * a_p;
3099 } */ *ap = v;
3100
3101 struct vnode *vp = ap->a_vp;
3102
3103 if (vp->v_type != VREG)
3104 return 0;
3105
3106 return (nfs_flush(vp, ap->a_cred,
3107 (ap->a_flags & FSYNC_WAIT) != 0 ? MNT_WAIT : 0, ap->a_p, 1));
3108 }
3109
3110 /*
3111 * Flush all the data associated with a vnode.
3112 */
3113 int
3114 nfs_flush(vp, cred, waitfor, p, commit)
3115 struct vnode *vp;
3116 struct ucred *cred;
3117 int waitfor;
3118 struct proc *p;
3119 int commit;
3120 {
3121 struct nfsnode *np = VTONFS(vp);
3122 int error;
3123 int flushflags = PGO_ALLPAGES|PGO_CLEANIT|PGO_SYNCIO;
3124 UVMHIST_FUNC("nfs_flush"); UVMHIST_CALLED(ubchist);
3125
3126 simple_lock(&vp->v_interlock);
3127 error = VOP_PUTPAGES(vp, 0, 0, flushflags);
3128 if (np->n_flag & NWRITEERR) {
3129 error = np->n_error;
3130 np->n_flag &= ~NWRITEERR;
3131 }
3132 UVMHIST_LOG(ubchist, "returning %d", error,0,0,0);
3133 return (error);
3134 }
3135
3136 /*
3137 * Return POSIX pathconf information applicable to nfs.
3138 *
3139 * N.B. The NFS V2 protocol doesn't support this RPC.
3140 */
3141 /* ARGSUSED */
3142 int
3143 nfs_pathconf(v)
3144 void *v;
3145 {
3146 struct vop_pathconf_args /* {
3147 struct vnode *a_vp;
3148 int a_name;
3149 register_t *a_retval;
3150 } */ *ap = v;
3151 struct nfsv3_pathconf *pcp;
3152 struct vnode *vp = ap->a_vp;
3153 struct mbuf *mreq, *mrep, *md, *mb;
3154 int32_t t1, t2;
3155 u_int32_t *tl;
3156 caddr_t bpos, dpos, cp, cp2;
3157 int error = 0, attrflag;
3158 #ifndef NFS_V2_ONLY
3159 struct nfsmount *nmp;
3160 unsigned int l;
3161 u_int64_t maxsize;
3162 #endif
3163 const int v3 = NFS_ISV3(vp);
3164 struct nfsnode *np = VTONFS(vp);
3165
3166 switch (ap->a_name) {
3167 /* Names that can be resolved locally. */
3168 case _PC_PIPE_BUF:
3169 *ap->a_retval = PIPE_BUF;
3170 break;
3171 case _PC_SYNC_IO:
3172 *ap->a_retval = 1;
3173 break;
3174 /* Names that cannot be resolved locally; do an RPC, if possible. */
3175 case _PC_LINK_MAX:
3176 case _PC_NAME_MAX:
3177 case _PC_CHOWN_RESTRICTED:
3178 case _PC_NO_TRUNC:
3179 if (!v3) {
3180 error = EINVAL;
3181 break;
3182 }
3183 nfsstats.rpccnt[NFSPROC_PATHCONF]++;
3184 nfsm_reqhead(np, NFSPROC_PATHCONF, NFSX_FH(1));
3185 nfsm_fhtom(np, 1);
3186 nfsm_request(np, NFSPROC_PATHCONF,
3187 curproc, curproc->p_ucred); /* XXX */
3188 nfsm_postop_attr(vp, attrflag, 0);
3189 if (!error) {
3190 nfsm_dissect(pcp, struct nfsv3_pathconf *,
3191 NFSX_V3PATHCONF);
3192 switch (ap->a_name) {
3193 case _PC_LINK_MAX:
3194 *ap->a_retval =
3195 fxdr_unsigned(register_t, pcp->pc_linkmax);
3196 break;
3197 case _PC_NAME_MAX:
3198 *ap->a_retval =
3199 fxdr_unsigned(register_t, pcp->pc_namemax);
3200 break;
3201 case _PC_CHOWN_RESTRICTED:
3202 *ap->a_retval =
3203 (pcp->pc_chownrestricted == nfs_true);
3204 break;
3205 case _PC_NO_TRUNC:
3206 *ap->a_retval =
3207 (pcp->pc_notrunc == nfs_true);
3208 break;
3209 }
3210 }
3211 nfsm_reqdone;
3212 break;
3213 case _PC_FILESIZEBITS:
3214 #ifndef NFS_V2_ONLY
3215 if (v3) {
3216 nmp = VFSTONFS(vp->v_mount);
3217 if ((nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
3218 if ((error = nfs_fsinfo(nmp, vp,
3219 curproc->p_ucred, curproc)) != 0) /* XXX */
3220 break;
3221 for (l = 0, maxsize = nmp->nm_maxfilesize;
3222 (maxsize >> l) > 0; l++)
3223 ;
3224 *ap->a_retval = l + 1;
3225 } else
3226 #endif
3227 {
3228 *ap->a_retval = 32; /* NFS V2 limitation */
3229 }
3230 break;
3231 default:
3232 error = EINVAL;
3233 break;
3234 }
3235
3236 return (error);
3237 }
3238
3239 /*
3240 * NFS advisory byte-level locks.
3241 */
3242 int
3243 nfs_advlock(v)
3244 void *v;
3245 {
3246 struct vop_advlock_args /* {
3247 struct vnode *a_vp;
3248 caddr_t a_id;
3249 int a_op;
3250 struct flock *a_fl;
3251 int a_flags;
3252 } */ *ap = v;
3253 struct nfsnode *np = VTONFS(ap->a_vp);
3254
3255 return lf_advlock(ap, &np->n_lockf, np->n_size);
3256 }
3257
3258 /*
3259 * Print out the contents of an nfsnode.
3260 */
3261 int
3262 nfs_print(v)
3263 void *v;
3264 {
3265 struct vop_print_args /* {
3266 struct vnode *a_vp;
3267 } */ *ap = v;
3268 struct vnode *vp = ap->a_vp;
3269 struct nfsnode *np = VTONFS(vp);
3270
3271 printf("tag VT_NFS, fileid %ld fsid 0x%lx",
3272 np->n_vattr->va_fileid, np->n_vattr->va_fsid);
3273 if (vp->v_type == VFIFO)
3274 fifo_printinfo(vp);
3275 printf("\n");
3276 return (0);
3277 }
3278
3279 /*
3280 * NFS file truncation.
3281 */
3282 int
3283 nfs_truncate(v)
3284 void *v;
3285 {
3286 #if 0
3287 struct vop_truncate_args /* {
3288 struct vnode *a_vp;
3289 off_t a_length;
3290 int a_flags;
3291 struct ucred *a_cred;
3292 struct proc *a_p;
3293 } */ *ap = v;
3294 #endif
3295
3296 /* Use nfs_setattr */
3297 return (EOPNOTSUPP);
3298 }
3299
3300 /*
3301 * NFS update.
3302 */
3303 int
3304 nfs_update(v)
3305 void *v;
3306 #if 0
3307 struct vop_update_args /* {
3308 struct vnode *a_vp;
3309 struct timespec *a_ta;
3310 struct timespec *a_tm;
3311 int a_waitfor;
3312 } */ *ap = v;
3313 #endif
3314 {
3315
3316 /* Use nfs_setattr */
3317 return (EOPNOTSUPP);
3318 }
3319
3320 /*
3321 * Just call bwrite().
3322 */
3323 int
3324 nfs_bwrite(v)
3325 void *v;
3326 {
3327 struct vop_bwrite_args /* {
3328 struct vnode *a_bp;
3329 } */ *ap = v;
3330
3331 return (bwrite(ap->a_bp));
3332 }
3333
3334 /*
3335 * nfs unlock wrapper.
3336 */
3337 int
3338 nfs_unlock(void *v)
3339 {
3340 struct vop_unlock_args /* {
3341 struct vnode *a_vp;
3342 int a_flags;
3343 } */ *ap = v;
3344 struct vnode *vp = ap->a_vp;
3345
3346 /*
3347 * VOP_UNLOCK can be called by nfs_loadattrcache
3348 * with v_data == 0.
3349 */
3350 if (VTONFS(vp)) {
3351 nfs_delayedtruncate(vp);
3352 }
3353
3354 return genfs_unlock(v);
3355 }
3356
3357 /*
3358 * nfs special file access vnode op.
3359 * Essentially just get vattr and then imitate iaccess() since the device is
3360 * local to the client.
3361 */
3362 int
3363 nfsspec_access(v)
3364 void *v;
3365 {
3366 struct vop_access_args /* {
3367 struct vnode *a_vp;
3368 int a_mode;
3369 struct ucred *a_cred;
3370 struct proc *a_p;
3371 } */ *ap = v;
3372 struct vattr va;
3373 struct vnode *vp = ap->a_vp;
3374 int error;
3375
3376 error = VOP_GETATTR(vp, &va, ap->a_cred, ap->a_p);
3377 if (error)
3378 return (error);
3379
3380 /*
3381 * Disallow write attempts on filesystems mounted read-only;
3382 * unless the file is a socket, fifo, or a block or character
3383 * device resident on the filesystem.
3384 */
3385 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3386 switch (vp->v_type) {
3387 case VREG:
3388 case VDIR:
3389 case VLNK:
3390 return (EROFS);
3391 default:
3392 break;
3393 }
3394 }
3395
3396 return (vaccess(va.va_type, va.va_mode,
3397 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
3398 }
3399
3400 /*
3401 * Read wrapper for special devices.
3402 */
3403 int
3404 nfsspec_read(v)
3405 void *v;
3406 {
3407 struct vop_read_args /* {
3408 struct vnode *a_vp;
3409 struct uio *a_uio;
3410 int a_ioflag;
3411 struct ucred *a_cred;
3412 } */ *ap = v;
3413 struct nfsnode *np = VTONFS(ap->a_vp);
3414
3415 /*
3416 * Set access flag.
3417 */
3418 np->n_flag |= NACC;
3419 np->n_atim.tv_sec = time.tv_sec;
3420 np->n_atim.tv_nsec = time.tv_usec * 1000;
3421 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3422 }
3423
3424 /*
3425 * Write wrapper for special devices.
3426 */
3427 int
3428 nfsspec_write(v)
3429 void *v;
3430 {
3431 struct vop_write_args /* {
3432 struct vnode *a_vp;
3433 struct uio *a_uio;
3434 int a_ioflag;
3435 struct ucred *a_cred;
3436 } */ *ap = v;
3437 struct nfsnode *np = VTONFS(ap->a_vp);
3438
3439 /*
3440 * Set update flag.
3441 */
3442 np->n_flag |= NUPD;
3443 np->n_mtim.tv_sec = time.tv_sec;
3444 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3445 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3446 }
3447
3448 /*
3449 * Close wrapper for special devices.
3450 *
3451 * Update the times on the nfsnode then do device close.
3452 */
3453 int
3454 nfsspec_close(v)
3455 void *v;
3456 {
3457 struct vop_close_args /* {
3458 struct vnode *a_vp;
3459 int a_fflag;
3460 struct ucred *a_cred;
3461 struct proc *a_p;
3462 } */ *ap = v;
3463 struct vnode *vp = ap->a_vp;
3464 struct nfsnode *np = VTONFS(vp);
3465 struct vattr vattr;
3466
3467 if (np->n_flag & (NACC | NUPD)) {
3468 np->n_flag |= NCHG;
3469 if (vp->v_usecount == 1 &&
3470 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3471 VATTR_NULL(&vattr);
3472 if (np->n_flag & NACC)
3473 vattr.va_atime = np->n_atim;
3474 if (np->n_flag & NUPD)
3475 vattr.va_mtime = np->n_mtim;
3476 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
3477 }
3478 }
3479 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3480 }
3481
3482 /*
3483 * Read wrapper for fifos.
3484 */
3485 int
3486 nfsfifo_read(v)
3487 void *v;
3488 {
3489 struct vop_read_args /* {
3490 struct vnode *a_vp;
3491 struct uio *a_uio;
3492 int a_ioflag;
3493 struct ucred *a_cred;
3494 } */ *ap = v;
3495 struct nfsnode *np = VTONFS(ap->a_vp);
3496
3497 /*
3498 * Set access flag.
3499 */
3500 np->n_flag |= NACC;
3501 np->n_atim.tv_sec = time.tv_sec;
3502 np->n_atim.tv_nsec = time.tv_usec * 1000;
3503 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3504 }
3505
3506 /*
3507 * Write wrapper for fifos.
3508 */
3509 int
3510 nfsfifo_write(v)
3511 void *v;
3512 {
3513 struct vop_write_args /* {
3514 struct vnode *a_vp;
3515 struct uio *a_uio;
3516 int a_ioflag;
3517 struct ucred *a_cred;
3518 } */ *ap = v;
3519 struct nfsnode *np = VTONFS(ap->a_vp);
3520
3521 /*
3522 * Set update flag.
3523 */
3524 np->n_flag |= NUPD;
3525 np->n_mtim.tv_sec = time.tv_sec;
3526 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3527 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3528 }
3529
3530 /*
3531 * Close wrapper for fifos.
3532 *
3533 * Update the times on the nfsnode then do fifo close.
3534 */
3535 int
3536 nfsfifo_close(v)
3537 void *v;
3538 {
3539 struct vop_close_args /* {
3540 struct vnode *a_vp;
3541 int a_fflag;
3542 struct ucred *a_cred;
3543 struct proc *a_p;
3544 } */ *ap = v;
3545 struct vnode *vp = ap->a_vp;
3546 struct nfsnode *np = VTONFS(vp);
3547 struct vattr vattr;
3548
3549 if (np->n_flag & (NACC | NUPD)) {
3550 if (np->n_flag & NACC) {
3551 np->n_atim.tv_sec = time.tv_sec;
3552 np->n_atim.tv_nsec = time.tv_usec * 1000;
3553 }
3554 if (np->n_flag & NUPD) {
3555 np->n_mtim.tv_sec = time.tv_sec;
3556 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3557 }
3558 np->n_flag |= NCHG;
3559 if (vp->v_usecount == 1 &&
3560 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3561 VATTR_NULL(&vattr);
3562 if (np->n_flag & NACC)
3563 vattr.va_atime = np->n_atim;
3564 if (np->n_flag & NUPD)
3565 vattr.va_mtime = np->n_mtim;
3566 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
3567 }
3568 }
3569 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
3570 }
3571