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