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