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