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