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