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