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