nfs_vnops.c revision 1.83 1 /* $NetBSD: nfs_vnops.c,v 1.83 1997/10/13 08:35:53 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 tlen = (int)ALIGN(tlen);
2140 left = NFS_DIRFRAGSIZ - blksiz;
2141 if ((tlen + DIRHDSIZ) > left) {
2142 dp->d_reclen += left;
2143 uiop->uio_iov->iov_base += left;
2144 uiop->uio_iov->iov_len -= left;
2145 uiop->uio_resid -= left;
2146 blksiz = 0;
2147 NFS_STASHCOOKIE(dp, uiop->uio_offset);
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 }
2194 if (v3)
2195 tl += 2;
2196 else
2197 tl++;
2198 more_dirs = fxdr_unsigned(int, *tl);
2199 }
2200 /*
2201 * If at end of rpc data, get the eof boolean
2202 */
2203 if (!more_dirs) {
2204 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2205 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2206 }
2207 m_freem(mrep);
2208 }
2209 /*
2210 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2211 * by increasing d_reclen for the last record.
2212 */
2213 if (blksiz > 0) {
2214 left = NFS_DIRFRAGSIZ - blksiz;
2215 dp->d_reclen += left;
2216 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2217 uiop->uio_iov->iov_base += left;
2218 uiop->uio_iov->iov_len -= left;
2219 uiop->uio_resid -= left;
2220 }
2221
2222 /*
2223 * We are now either at the end of the directory or have filled the
2224 * block.
2225 */
2226 if (bigenough)
2227 dnp->n_direofoffset = uiop->uio_offset;
2228 nfsmout:
2229 return (error);
2230 }
2231
2232 /*
2233 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2234 */
2235 int
2236 nfs_readdirplusrpc(vp, uiop, cred)
2237 struct vnode *vp;
2238 register struct uio *uiop;
2239 struct ucred *cred;
2240 {
2241 register int len, left;
2242 register struct dirent *dp = NULL;
2243 register u_int32_t *tl;
2244 register caddr_t cp;
2245 register int32_t t1, t2;
2246 struct vnode *newvp;
2247 caddr_t bpos, dpos, cp2;
2248 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2249 struct nameidata nami, *ndp = &nami;
2250 struct componentname *cnp = &ndp->ni_cnd;
2251 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2252 struct nfsnode *dnp = VTONFS(vp), *np;
2253 const unsigned char *hcp;
2254 nfsfh_t *fhp;
2255 u_quad_t fileno;
2256 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2257 int attrflag, fhsize;
2258 struct nfs_fattr fattr, *fp;
2259
2260 #ifdef DIAGNOSTIC
2261 if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1)))
2262 panic("nfs readdirplusrpc bad uio");
2263 #endif
2264 ndp->ni_dvp = vp;
2265 newvp = NULLVP;
2266
2267 /*
2268 * Loop around doing readdir rpc's of size nm_readdirsize
2269 * truncated to a multiple of NFS_DIRFRAGSIZ.
2270 * The stopping criteria is EOF or buffer full.
2271 */
2272 while (more_dirs && bigenough) {
2273 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2274 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2275 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2276 nfsm_fhtom(vp, 1);
2277 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2278 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2279 txdr_swapcookie3(uiop->uio_offset, tl);
2280 } else {
2281 txdr_cookie3(uiop->uio_offset, tl);
2282 }
2283 tl += 2;
2284 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2285 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2286 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2287 *tl = txdr_unsigned(nmp->nm_rsize);
2288 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_procp, cred);
2289 nfsm_postop_attr(vp, attrflag);
2290 if (error) {
2291 m_freem(mrep);
2292 goto nfsmout;
2293 }
2294 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2295 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2296 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2297 more_dirs = fxdr_unsigned(int, *tl);
2298
2299 /* loop thru the dir entries, doctoring them to 4bsd form */
2300 while (more_dirs && bigenough) {
2301 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2302 fxdr_hyper(tl, &fileno);
2303 len = fxdr_unsigned(int, *(tl + 2));
2304 if (len <= 0 || len > NFS_MAXNAMLEN) {
2305 error = EBADRPC;
2306 m_freem(mrep);
2307 goto nfsmout;
2308 }
2309 tlen = nfsm_rndup(len);
2310 if (tlen == len)
2311 tlen += 4; /* To ensure null termination*/
2312 tlen += sizeof (off_t) + sizeof (int);
2313 tlen = (int)ALIGN(tlen);
2314 left = NFS_DIRFRAGSIZ - blksiz;
2315 if ((tlen + DIRHDSIZ) > left) {
2316 dp->d_reclen += left;
2317 uiop->uio_iov->iov_base += left;
2318 uiop->uio_iov->iov_len -= left;
2319 uiop->uio_resid -= left;
2320 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2321 blksiz = 0;
2322 }
2323 if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2324 bigenough = 0;
2325 if (bigenough) {
2326 dp = (struct dirent *)uiop->uio_iov->iov_base;
2327 dp->d_fileno = (int)fileno;
2328 dp->d_namlen = len;
2329 dp->d_reclen = tlen + DIRHDSIZ;
2330 dp->d_type = DT_UNKNOWN;
2331 blksiz += dp->d_reclen;
2332 if (blksiz == NFS_DIRFRAGSIZ)
2333 blksiz = 0;
2334 uiop->uio_resid -= DIRHDSIZ;
2335 uiop->uio_iov->iov_base += DIRHDSIZ;
2336 uiop->uio_iov->iov_len -= DIRHDSIZ;
2337 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2338 cnp->cn_namelen = len;
2339 nfsm_mtouio(uiop, len);
2340 cp = uiop->uio_iov->iov_base;
2341 tlen -= len;
2342 *cp = '\0';
2343 uiop->uio_iov->iov_base += tlen;
2344 uiop->uio_iov->iov_len -= tlen;
2345 uiop->uio_resid -= tlen;
2346 } else
2347 nfsm_adv(nfsm_rndup(len));
2348 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2349 if (bigenough) {
2350 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2351 uiop->uio_offset =
2352 fxdr_swapcookie3(tl);
2353 else
2354 uiop->uio_offset =
2355 fxdr_cookie3(tl);
2356 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2357 }
2358 tl += 2;
2359
2360 /*
2361 * Since the attributes are before the file handle
2362 * (sigh), we must skip over the attributes and then
2363 * come back and get them.
2364 */
2365 attrflag = fxdr_unsigned(int, *tl);
2366 if (attrflag) {
2367 nfsm_dissect(fp, struct nfs_fattr *, NFSX_V3FATTR);
2368 bcopy(fp, &fattr, NFSX_V3FATTR);
2369 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2370 doit = fxdr_unsigned(int, *tl);
2371 if (doit) {
2372 nfsm_getfh(fhp, fhsize, 1);
2373 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2374 VREF(vp);
2375 newvp = vp;
2376 np = dnp;
2377 } else {
2378 error = nfs_nget(vp->v_mount, fhp,
2379 fhsize, &np);
2380 if (!error)
2381 newvp = NFSTOV(np);
2382 }
2383 if (!error) {
2384 nfs_loadattrcache(&newvp, &fattr, 0);
2385 dp->d_type =
2386 IFTODT(VTTOIF(np->n_vattr.va_type));
2387 ndp->ni_vp = newvp;
2388 cnp->cn_hash = 0;
2389 for (hcp = cnp->cn_nameptr, i = 1; i <= len;
2390 i++, hcp++)
2391 cnp->cn_hash += *hcp * i;
2392 if (cnp->cn_namelen <= NCHNAMLEN)
2393 cache_enter(ndp->ni_dvp, ndp->ni_vp,
2394 cnp);
2395 }
2396 }
2397 } else {
2398 /* Just skip over the file handle */
2399 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2400 i = fxdr_unsigned(int, *tl);
2401 nfsm_adv(nfsm_rndup(i));
2402 }
2403 if (newvp != NULLVP) {
2404 vrele(newvp);
2405 newvp = NULLVP;
2406 }
2407 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2408 more_dirs = fxdr_unsigned(int, *tl);
2409 }
2410 /*
2411 * If at end of rpc data, get the eof boolean
2412 */
2413 if (!more_dirs) {
2414 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2415 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2416 }
2417 m_freem(mrep);
2418 }
2419 /*
2420 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2421 * by increasing d_reclen for the last record.
2422 */
2423 if (blksiz > 0) {
2424 left = NFS_DIRFRAGSIZ - blksiz;
2425 dp->d_reclen += left;
2426 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2427 uiop->uio_iov->iov_base += left;
2428 uiop->uio_iov->iov_len -= left;
2429 uiop->uio_resid -= left;
2430 }
2431
2432 /*
2433 * We are now either at the end of the directory or have filled the
2434 * block.
2435 */
2436 if (bigenough)
2437 dnp->n_direofoffset = uiop->uio_offset;
2438 nfsmout:
2439 if (newvp != NULLVP)
2440 vrele(newvp);
2441 return (error);
2442 }
2443 static char hextoasc[] = "0123456789abcdef";
2444
2445 /*
2446 * Silly rename. To make the NFS filesystem that is stateless look a little
2447 * more like the "ufs" a remove of an active vnode is translated to a rename
2448 * to a funny looking filename that is removed by nfs_inactive on the
2449 * nfsnode. There is the potential for another process on a different client
2450 * to create the same funny name between the nfs_lookitup() fails and the
2451 * nfs_rename() completes, but...
2452 */
2453 int
2454 nfs_sillyrename(dvp, vp, cnp)
2455 struct vnode *dvp, *vp;
2456 struct componentname *cnp;
2457 {
2458 register struct sillyrename *sp;
2459 struct nfsnode *np;
2460 int error;
2461 short pid;
2462
2463 cache_purge(dvp);
2464 np = VTONFS(vp);
2465 #ifndef DIAGNOSTIC
2466 if (vp->v_type == VDIR)
2467 panic("nfs: sillyrename dir");
2468 #endif
2469 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2470 M_NFSREQ, M_WAITOK);
2471 sp->s_cred = crdup(cnp->cn_cred);
2472 sp->s_dvp = dvp;
2473 VREF(dvp);
2474
2475 /* Fudge together a funny name */
2476 pid = cnp->cn_proc->p_pid;
2477 bcopy(".nfsAxxxx4.4", sp->s_name, 13);
2478 sp->s_namlen = 12;
2479 sp->s_name[8] = hextoasc[pid & 0xf];
2480 sp->s_name[7] = hextoasc[(pid >> 4) & 0xf];
2481 sp->s_name[6] = hextoasc[(pid >> 8) & 0xf];
2482 sp->s_name[5] = hextoasc[(pid >> 12) & 0xf];
2483
2484 /* Try lookitups until we get one that isn't there */
2485 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2486 cnp->cn_proc, (struct nfsnode **)0) == 0) {
2487 sp->s_name[4]++;
2488 if (sp->s_name[4] > 'z') {
2489 error = EINVAL;
2490 goto bad;
2491 }
2492 }
2493 error = nfs_renameit(dvp, cnp, sp);
2494 if (error)
2495 goto bad;
2496 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2497 cnp->cn_proc, &np);
2498 np->n_sillyrename = sp;
2499 return (0);
2500 bad:
2501 vrele(sp->s_dvp);
2502 crfree(sp->s_cred);
2503 free((caddr_t)sp, M_NFSREQ);
2504 return (error);
2505 }
2506
2507 /*
2508 * Look up a file name and optionally either update the file handle or
2509 * allocate an nfsnode, depending on the value of npp.
2510 * npp == NULL --> just do the lookup
2511 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2512 * handled too
2513 * *npp != NULL --> update the file handle in the vnode
2514 */
2515 int
2516 nfs_lookitup(dvp, name, len, cred, procp, npp)
2517 register struct vnode *dvp;
2518 const char *name;
2519 int len;
2520 struct ucred *cred;
2521 struct proc *procp;
2522 struct nfsnode **npp;
2523 {
2524 register u_int32_t *tl;
2525 register caddr_t cp;
2526 register int32_t t1, t2;
2527 struct vnode *newvp = (struct vnode *)0;
2528 struct nfsnode *np, *dnp = VTONFS(dvp);
2529 caddr_t bpos, dpos, cp2;
2530 int error = 0, fhlen, attrflag;
2531 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2532 nfsfh_t *nfhp;
2533 int v3 = NFS_ISV3(dvp);
2534
2535 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2536 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2537 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2538 nfsm_fhtom(dvp, v3);
2539 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2540 nfsm_request(dvp, NFSPROC_LOOKUP, procp, cred);
2541 if (npp && !error) {
2542 nfsm_getfh(nfhp, fhlen, v3);
2543 if (*npp) {
2544 np = *npp;
2545 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2546 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2547 np->n_fhp = &np->n_fh;
2548 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2549 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2550 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2551 np->n_fhsize = fhlen;
2552 newvp = NFSTOV(np);
2553 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2554 VREF(dvp);
2555 newvp = dvp;
2556 } else {
2557 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2558 if (error) {
2559 m_freem(mrep);
2560 return (error);
2561 }
2562 newvp = NFSTOV(np);
2563 }
2564 if (v3) {
2565 nfsm_postop_attr(newvp, attrflag);
2566 if (!attrflag && *npp == NULL) {
2567 m_freem(mrep);
2568 vrele(newvp);
2569 return (ENOENT);
2570 }
2571 } else
2572 nfsm_loadattr(newvp, (struct vattr *)0);
2573 }
2574 nfsm_reqdone;
2575 if (npp && *npp == NULL) {
2576 if (error) {
2577 if (newvp)
2578 vrele(newvp);
2579 } else
2580 *npp = np;
2581 }
2582 return (error);
2583 }
2584
2585 /*
2586 * Nfs Version 3 commit rpc
2587 */
2588 int
2589 nfs_commit(vp, offset, cnt, cred, procp)
2590 register struct vnode *vp;
2591 u_quad_t offset;
2592 int cnt;
2593 struct ucred *cred;
2594 struct proc *procp;
2595 {
2596 register caddr_t cp;
2597 register u_int32_t *tl;
2598 register int32_t t1, t2;
2599 register struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2600 caddr_t bpos, dpos, cp2;
2601 int error = 0, wccflag = NFSV3_WCCRATTR;
2602 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2603
2604 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0)
2605 return (0);
2606 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2607 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2608 nfsm_fhtom(vp, 1);
2609 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2610 txdr_hyper(&offset, tl);
2611 tl += 2;
2612 *tl = txdr_unsigned(cnt);
2613 nfsm_request(vp, NFSPROC_COMMIT, procp, cred);
2614 nfsm_wcc_data(vp, wccflag);
2615 if (!error) {
2616 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2617 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2618 NFSX_V3WRITEVERF)) {
2619 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2620 NFSX_V3WRITEVERF);
2621 error = NFSERR_STALEWRITEVERF;
2622 }
2623 }
2624 nfsm_reqdone;
2625 return (error);
2626 }
2627
2628 /*
2629 * Kludge City..
2630 * - make nfs_bmap() essentially a no-op that does no translation
2631 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2632 * (Maybe I could use the process's page mapping, but I was concerned that
2633 * Kernel Write might not be enabled and also figured copyout() would do
2634 * a lot more work than bcopy() and also it currently happens in the
2635 * context of the swapper process (2).
2636 */
2637 int
2638 nfs_bmap(v)
2639 void *v;
2640 {
2641 struct vop_bmap_args /* {
2642 struct vnode *a_vp;
2643 daddr_t a_bn;
2644 struct vnode **a_vpp;
2645 daddr_t *a_bnp;
2646 int *a_runp;
2647 } */ *ap = v;
2648 register struct vnode *vp = ap->a_vp;
2649
2650 if (ap->a_vpp != NULL)
2651 *ap->a_vpp = vp;
2652 if (ap->a_bnp != NULL)
2653 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2654 return (0);
2655 }
2656
2657 /*
2658 * Strategy routine.
2659 * For async requests when nfsiod(s) are running, queue the request by
2660 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2661 * request.
2662 */
2663 int
2664 nfs_strategy(v)
2665 void *v;
2666 {
2667 struct vop_strategy_args *ap = v;
2668 register struct buf *bp = ap->a_bp;
2669 struct ucred *cr;
2670 struct proc *p;
2671 int error = 0;
2672
2673 if ((bp->b_flags & (B_PHYS|B_ASYNC)) == (B_PHYS|B_ASYNC))
2674 panic("nfs physio/async");
2675 if (bp->b_flags & B_ASYNC)
2676 p = (struct proc *)0;
2677 else
2678 p = curproc; /* XXX */
2679 if (bp->b_flags & B_READ)
2680 cr = bp->b_rcred;
2681 else
2682 cr = bp->b_wcred;
2683 /*
2684 * If the op is asynchronous and an i/o daemon is waiting
2685 * queue the request, wake it up and wait for completion
2686 * otherwise just do it ourselves.
2687 */
2688 if ((bp->b_flags & B_ASYNC) == 0 ||
2689 nfs_asyncio(bp, NOCRED))
2690 error = nfs_doio(bp, cr, p);
2691 return (error);
2692 }
2693
2694 /*
2695 * Mmap a file
2696 *
2697 * NB Currently unsupported.
2698 */
2699 /* ARGSUSED */
2700 int
2701 nfs_mmap(v)
2702 void *v;
2703 {
2704 #if 0
2705 struct vop_mmap_args /* {
2706 struct vnode *a_vp;
2707 int a_fflags;
2708 struct ucred *a_cred;
2709 struct proc *a_p;
2710 } */ *ap = v;
2711 #endif
2712
2713 return (EINVAL);
2714 }
2715
2716 /*
2717 * fsync vnode op. Just call nfs_flush() with commit == 1.
2718 */
2719 /* ARGSUSED */
2720 int
2721 nfs_fsync(v)
2722 void *v;
2723 {
2724 struct vop_fsync_args /* {
2725 struct vnodeop_desc *a_desc;
2726 struct vnode * a_vp;
2727 struct ucred * a_cred;
2728 int a_waitfor;
2729 struct proc * a_p;
2730 } */ *ap = v;
2731
2732 return (nfs_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_p, 1));
2733 }
2734
2735 /*
2736 * Flush all the blocks associated with a vnode.
2737 * Walk through the buffer pool and push any dirty pages
2738 * associated with the vnode.
2739 */
2740 int
2741 nfs_flush(vp, cred, waitfor, p, commit)
2742 register struct vnode *vp;
2743 struct ucred *cred;
2744 int waitfor;
2745 struct proc *p;
2746 int commit;
2747 {
2748 register struct nfsnode *np = VTONFS(vp);
2749 register struct buf *bp;
2750 register int i;
2751 struct buf *nbp;
2752 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2753 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2754 int passone = 1;
2755 u_quad_t off = (u_quad_t)-1, endoff = 0, toff;
2756 struct ucred* wcred = NULL;
2757 #ifndef NFS_COMMITBVECSIZ
2758 #define NFS_COMMITBVECSIZ 20
2759 #endif
2760 struct buf *bvec[NFS_COMMITBVECSIZ];
2761
2762 if (nmp->nm_flag & NFSMNT_INT)
2763 slpflag = PCATCH;
2764 if (!commit)
2765 passone = 0;
2766 /*
2767 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2768 * server, but nas not been committed to stable storage on the server
2769 * yet. On the first pass, the byte range is worked out and the commit
2770 * rpc is done. On the second pass, nfs_writebp() is called to do the
2771 * job.
2772 */
2773 again:
2774 bvecpos = 0;
2775 if (NFS_ISV3(vp) && commit) {
2776 s = splbio();
2777 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
2778 nbp = bp->b_vnbufs.le_next;
2779 if (bvecpos >= NFS_COMMITBVECSIZ)
2780 break;
2781 if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
2782 != (B_DELWRI | B_NEEDCOMMIT))
2783 continue;
2784 bremfree(bp);
2785 /*
2786 * Work out if all buffers are using the same cred
2787 * so we can deal with them all with one commit.
2788 */
2789 if (wcred == NULL)
2790 wcred = bp->b_wcred;
2791 else if (wcred != bp->b_wcred)
2792 wcred = NOCRED;
2793 bp->b_flags |= (B_BUSY | B_WRITEINPROG);
2794 /*
2795 * A list of these buffers is kept so that the
2796 * second loop knows which buffers have actually
2797 * been committed. This is necessary, since there
2798 * may be a race between the commit rpc and new
2799 * uncommitted writes on the file.
2800 */
2801 bvec[bvecpos++] = bp;
2802 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2803 bp->b_dirtyoff;
2804 if (toff < off)
2805 off = toff;
2806 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2807 if (toff > endoff)
2808 endoff = toff;
2809 }
2810 splx(s);
2811 }
2812 if (bvecpos > 0) {
2813 /*
2814 * Commit data on the server, as required.
2815 * If all bufs are using the same wcred, then use that with
2816 * one call for all of them, otherwise commit each one
2817 * separately.
2818 */
2819 if (wcred != NOCRED)
2820 retv = nfs_commit(vp, off, (int)(endoff - off),
2821 wcred, p);
2822 else {
2823 retv = 0;
2824 for (i = 0; i < bvecpos; i++) {
2825 off_t off, size;
2826 bp = bvec[i];
2827 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2828 bp->b_dirtyoff;
2829 size = (u_quad_t)(bp->b_dirtyend
2830 - bp->b_dirtyoff);
2831 retv = nfs_commit(vp, off, (int)size,
2832 bp->b_wcred, p);
2833 if (retv) break;
2834 }
2835 }
2836
2837 if (retv == NFSERR_STALEWRITEVERF)
2838 nfs_clearcommit(vp->v_mount);
2839 /*
2840 * Now, either mark the blocks I/O done or mark the
2841 * blocks dirty, depending on whether the commit
2842 * succeeded.
2843 */
2844 for (i = 0; i < bvecpos; i++) {
2845 bp = bvec[i];
2846 bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG);
2847 if (retv)
2848 brelse(bp);
2849 else {
2850 vp->v_numoutput++;
2851 bp->b_flags |= B_ASYNC;
2852 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR|B_DELWRI);
2853 bp->b_dirtyoff = bp->b_dirtyend = 0;
2854 reassignbuf(bp, vp);
2855 biodone(bp);
2856 }
2857 }
2858 }
2859
2860 /*
2861 * Start/do any write(s) that are required.
2862 */
2863 loop:
2864 s = splbio();
2865 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
2866 nbp = bp->b_vnbufs.le_next;
2867 if (bp->b_flags & B_BUSY) {
2868 if (waitfor != MNT_WAIT || passone)
2869 continue;
2870 bp->b_flags |= B_WANTED;
2871 error = tsleep((caddr_t)bp, slpflag | (PRIBIO + 1),
2872 "nfsfsync", slptimeo);
2873 splx(s);
2874 if (error) {
2875 if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
2876 return (EINTR);
2877 if (slpflag == PCATCH) {
2878 slpflag = 0;
2879 slptimeo = 2 * hz;
2880 }
2881 }
2882 goto loop;
2883 }
2884 if ((bp->b_flags & B_DELWRI) == 0)
2885 panic("nfs_fsync: not dirty");
2886 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT))
2887 continue;
2888 bremfree(bp);
2889 if (passone || !commit)
2890 bp->b_flags |= (B_BUSY|B_ASYNC);
2891 else
2892 bp->b_flags |= (B_BUSY|B_ASYNC|B_WRITEINPROG|B_NEEDCOMMIT);
2893 splx(s);
2894 VOP_BWRITE(bp);
2895 goto loop;
2896 }
2897 splx(s);
2898 if (passone) {
2899 passone = 0;
2900 goto again;
2901 }
2902 if (waitfor == MNT_WAIT) {
2903 while (vp->v_numoutput) {
2904 vp->v_flag |= VBWAIT;
2905 error = tsleep((caddr_t)&vp->v_numoutput,
2906 slpflag | (PRIBIO + 1), "nfsfsync", slptimeo);
2907 if (error) {
2908 if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
2909 return (EINTR);
2910 if (slpflag == PCATCH) {
2911 slpflag = 0;
2912 slptimeo = 2 * hz;
2913 }
2914 }
2915 }
2916 if (vp->v_dirtyblkhd.lh_first && commit) {
2917 #if 0
2918 vprint("nfs_fsync: dirty", vp);
2919 #endif
2920 goto loop;
2921 }
2922 }
2923 if (np->n_flag & NWRITEERR) {
2924 error = np->n_error;
2925 np->n_flag &= ~NWRITEERR;
2926 }
2927 return (error);
2928 }
2929
2930 /*
2931 * Return POSIX pathconf information applicable to nfs.
2932 *
2933 * The NFS V2 protocol doesn't support this, so just return EINVAL
2934 * for V2.
2935 */
2936 /* ARGSUSED */
2937 int
2938 nfs_pathconf(v)
2939 void *v;
2940 {
2941 #if 0
2942 struct vop_pathconf_args /* {
2943 struct vnode *a_vp;
2944 int a_name;
2945 register_t *a_retval;
2946 } */ *ap = v;
2947 #endif
2948
2949 return (EINVAL);
2950 }
2951
2952 /*
2953 * NFS advisory byte-level locks.
2954 */
2955 int
2956 nfs_advlock(v)
2957 void *v;
2958 {
2959 struct vop_advlock_args /* {
2960 struct vnode *a_vp;
2961 caddr_t a_id;
2962 int a_op;
2963 struct flock *a_fl;
2964 int a_flags;
2965 } */ *ap = v;
2966 register struct nfsnode *np = VTONFS(ap->a_vp);
2967
2968 return (lf_advlock(&np->n_lockf, np->n_size, ap->a_id, ap->a_op,
2969 ap->a_fl, ap->a_flags));
2970 }
2971
2972 /*
2973 * Print out the contents of an nfsnode.
2974 */
2975 int
2976 nfs_print(v)
2977 void *v;
2978 {
2979 struct vop_print_args /* {
2980 struct vnode *a_vp;
2981 } */ *ap = v;
2982 register struct vnode *vp = ap->a_vp;
2983 register struct nfsnode *np = VTONFS(vp);
2984
2985 printf("tag VT_NFS, fileid %ld fsid 0x%lx",
2986 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
2987 #ifdef FIFO
2988 if (vp->v_type == VFIFO)
2989 fifo_printinfo(vp);
2990 #endif
2991 printf("\n");
2992 return (0);
2993 }
2994
2995 /*
2996 * NFS file truncation.
2997 */
2998 int
2999 nfs_truncate(v)
3000 void *v;
3001 {
3002 #if 0
3003 struct vop_truncate_args /* {
3004 struct vnode *a_vp;
3005 off_t a_length;
3006 int a_flags;
3007 struct ucred *a_cred;
3008 struct proc *a_p;
3009 } */ *ap = v;
3010 #endif
3011
3012 /* Use nfs_setattr */
3013 return (EOPNOTSUPP);
3014 }
3015
3016 /*
3017 * NFS update.
3018 */
3019 int
3020 nfs_update(v)
3021 void *v;
3022 #if 0
3023 struct vop_update_args /* {
3024 struct vnode *a_vp;
3025 struct timespec *a_ta;
3026 struct timespec *a_tm;
3027 int a_waitfor;
3028 } */ *ap = v;
3029 #endif
3030 {
3031
3032 /* Use nfs_setattr */
3033 return (EOPNOTSUPP);
3034 }
3035
3036 /*
3037 * Just call nfs_writebp() with the force argument set to 1.
3038 */
3039 int
3040 nfs_bwrite(v)
3041 void *v;
3042 {
3043 struct vop_bwrite_args /* {
3044 struct vnode *a_bp;
3045 } */ *ap = v;
3046
3047 return (nfs_writebp(ap->a_bp, 1));
3048 }
3049
3050 /*
3051 * This is a clone of vn_bwrite(), except that B_WRITEINPROG isn't set unless
3052 * the force flag is one and it also handles the B_NEEDCOMMIT flag.
3053 */
3054 int
3055 nfs_writebp(bp, force)
3056 register struct buf *bp;
3057 int force;
3058 {
3059 register int oldflags = bp->b_flags, retv = 1;
3060 register struct proc *p = curproc; /* XXX */
3061 off_t off;
3062
3063 if(!(bp->b_flags & B_BUSY))
3064 panic("bwrite: buffer is not busy???");
3065
3066 #ifdef fvdl_debug
3067 printf("nfs_writebp(%x): vp %x voff %d vend %d doff %d dend %d\n",
3068 bp, bp->b_vp, bp->b_validoff, bp->b_validend, bp->b_dirtyoff,
3069 bp->b_dirtyend);
3070 #endif
3071 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR|B_DELWRI|B_AGE);
3072
3073 if (oldflags & B_ASYNC) {
3074 if (oldflags & B_DELWRI) {
3075 reassignbuf(bp, bp->b_vp);
3076 } else if (p) {
3077 ++p->p_stats->p_ru.ru_oublock;
3078 }
3079 }
3080 bp->b_vp->v_numoutput++;
3081
3082 /*
3083 * If B_NEEDCOMMIT is set, a commit rpc may do the trick. If not
3084 * an actual write will have to be scheduled via. VOP_STRATEGY().
3085 * If B_WRITEINPROG is already set, then push it with a write anyhow.
3086 */
3087 if ((oldflags & (B_NEEDCOMMIT | B_WRITEINPROG)) == B_NEEDCOMMIT) {
3088 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
3089 bp->b_flags |= B_WRITEINPROG;
3090 retv = nfs_commit(bp->b_vp, off, bp->b_dirtyend-bp->b_dirtyoff,
3091 bp->b_wcred, bp->b_proc);
3092 bp->b_flags &= ~B_WRITEINPROG;
3093 if (!retv) {
3094 bp->b_dirtyoff = bp->b_dirtyend = 0;
3095 bp->b_flags &= ~B_NEEDCOMMIT;
3096 biodone(bp);
3097 } else if (retv == NFSERR_STALEWRITEVERF)
3098 nfs_clearcommit(bp->b_vp->v_mount);
3099 }
3100 if (retv) {
3101 if (force)
3102 bp->b_flags |= B_WRITEINPROG;
3103 VOP_STRATEGY(bp);
3104 }
3105
3106 if( (oldflags & B_ASYNC) == 0) {
3107 int rtval = biowait(bp);
3108 if (oldflags & B_DELWRI) {
3109 reassignbuf(bp, bp->b_vp);
3110 } else if (p) {
3111 ++p->p_stats->p_ru.ru_oublock;
3112 }
3113 brelse(bp);
3114 return (rtval);
3115 }
3116
3117 return (0);
3118 }
3119
3120 /*
3121 * nfs special file access vnode op.
3122 * Essentially just get vattr and then imitate iaccess() since the device is
3123 * local to the client.
3124 */
3125 int
3126 nfsspec_access(v)
3127 void *v;
3128 {
3129 struct vop_access_args /* {
3130 struct vnode *a_vp;
3131 int a_mode;
3132 struct ucred *a_cred;
3133 struct proc *a_p;
3134 } */ *ap = v;
3135 struct vattr va;
3136 struct vnode *vp = ap->a_vp;
3137 int error;
3138
3139 error = VOP_GETATTR(vp, &va, ap->a_cred, ap->a_p);
3140 if (error)
3141 return (error);
3142
3143 /*
3144 * Disallow write attempts on filesystems mounted read-only;
3145 * unless the file is a socket, fifo, or a block or character
3146 * device resident on the filesystem.
3147 */
3148 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3149 switch (vp->v_type) {
3150 case VREG:
3151 case VDIR:
3152 case VLNK:
3153 return (EROFS);
3154 default:
3155 break;
3156 }
3157 }
3158
3159 return (vaccess(va.va_type, va.va_mode,
3160 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
3161 }
3162
3163 /*
3164 * Read wrapper for special devices.
3165 */
3166 int
3167 nfsspec_read(v)
3168 void *v;
3169 {
3170 struct vop_read_args /* {
3171 struct vnode *a_vp;
3172 struct uio *a_uio;
3173 int a_ioflag;
3174 struct ucred *a_cred;
3175 } */ *ap = v;
3176 register struct nfsnode *np = VTONFS(ap->a_vp);
3177
3178 /*
3179 * Set access flag.
3180 */
3181 np->n_flag |= NACC;
3182 np->n_atim.tv_sec = time.tv_sec;
3183 np->n_atim.tv_nsec = time.tv_usec * 1000;
3184 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3185 }
3186
3187 /*
3188 * Write wrapper for special devices.
3189 */
3190 int
3191 nfsspec_write(v)
3192 void *v;
3193 {
3194 struct vop_write_args /* {
3195 struct vnode *a_vp;
3196 struct uio *a_uio;
3197 int a_ioflag;
3198 struct ucred *a_cred;
3199 } */ *ap = v;
3200 register struct nfsnode *np = VTONFS(ap->a_vp);
3201
3202 /*
3203 * Set update flag.
3204 */
3205 np->n_flag |= NUPD;
3206 np->n_mtim.tv_sec = time.tv_sec;
3207 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3208 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3209 }
3210
3211 /*
3212 * Close wrapper for special devices.
3213 *
3214 * Update the times on the nfsnode then do device close.
3215 */
3216 int
3217 nfsspec_close(v)
3218 void *v;
3219 {
3220 struct vop_close_args /* {
3221 struct vnode *a_vp;
3222 int a_fflag;
3223 struct ucred *a_cred;
3224 struct proc *a_p;
3225 } */ *ap = v;
3226 register struct vnode *vp = ap->a_vp;
3227 register struct nfsnode *np = VTONFS(vp);
3228 struct vattr vattr;
3229
3230 if (np->n_flag & (NACC | NUPD)) {
3231 np->n_flag |= NCHG;
3232 if (vp->v_usecount == 1 &&
3233 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3234 VATTR_NULL(&vattr);
3235 if (np->n_flag & NACC)
3236 vattr.va_atime = np->n_atim;
3237 if (np->n_flag & NUPD)
3238 vattr.va_mtime = np->n_mtim;
3239 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
3240 }
3241 }
3242 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3243 }
3244
3245 #ifdef FIFO
3246 /*
3247 * Read wrapper for fifos.
3248 */
3249 int
3250 nfsfifo_read(v)
3251 void *v;
3252 {
3253 struct vop_read_args /* {
3254 struct vnode *a_vp;
3255 struct uio *a_uio;
3256 int a_ioflag;
3257 struct ucred *a_cred;
3258 } */ *ap = v;
3259 extern int (**fifo_vnodeop_p) __P((void *));
3260 register struct nfsnode *np = VTONFS(ap->a_vp);
3261
3262 /*
3263 * Set access flag.
3264 */
3265 np->n_flag |= NACC;
3266 np->n_atim.tv_sec = time.tv_sec;
3267 np->n_atim.tv_nsec = time.tv_usec * 1000;
3268 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3269 }
3270
3271 /*
3272 * Write wrapper for fifos.
3273 */
3274 int
3275 nfsfifo_write(v)
3276 void *v;
3277 {
3278 struct vop_write_args /* {
3279 struct vnode *a_vp;
3280 struct uio *a_uio;
3281 int a_ioflag;
3282 struct ucred *a_cred;
3283 } */ *ap = v;
3284 extern int (**fifo_vnodeop_p) __P((void *));
3285 register struct nfsnode *np = VTONFS(ap->a_vp);
3286
3287 /*
3288 * Set update flag.
3289 */
3290 np->n_flag |= NUPD;
3291 np->n_mtim.tv_sec = time.tv_sec;
3292 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3293 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3294 }
3295
3296 /*
3297 * Close wrapper for fifos.
3298 *
3299 * Update the times on the nfsnode then do fifo close.
3300 */
3301 int
3302 nfsfifo_close(v)
3303 void *v;
3304 {
3305 struct vop_close_args /* {
3306 struct vnode *a_vp;
3307 int a_fflag;
3308 struct ucred *a_cred;
3309 struct proc *a_p;
3310 } */ *ap = v;
3311 register struct vnode *vp = ap->a_vp;
3312 register struct nfsnode *np = VTONFS(vp);
3313 struct vattr vattr;
3314 extern int (**fifo_vnodeop_p) __P((void *));
3315
3316 if (np->n_flag & (NACC | NUPD)) {
3317 if (np->n_flag & NACC) {
3318 np->n_atim.tv_sec = time.tv_sec;
3319 np->n_atim.tv_nsec = time.tv_usec * 1000;
3320 }
3321 if (np->n_flag & NUPD) {
3322 np->n_mtim.tv_sec = time.tv_sec;
3323 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3324 }
3325 np->n_flag |= NCHG;
3326 if (vp->v_usecount == 1 &&
3327 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3328 VATTR_NULL(&vattr);
3329 if (np->n_flag & NACC)
3330 vattr.va_atime = np->n_atim;
3331 if (np->n_flag & NUPD)
3332 vattr.va_mtime = np->n_mtim;
3333 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
3334 }
3335 }
3336 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
3337 }
3338 #endif /* ! FIFO */
3339