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