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