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