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