nfs_vnops.c revision 1.89 1 /* $NetBSD: nfs_vnops.c,v 1.89 1998/03/01 02:24:29 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)nfs_vnops.c 8.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 uiop->uio_iov->iov_base -= backup;
1174 uiop->uio_iov->iov_len += backup;
1175 uiop->uio_offset -= backup;
1176 uiop->uio_resid += backup;
1177 len = rlen;
1178 }
1179 commit = fxdr_unsigned(int, *tl++);
1180
1181 /*
1182 * Return the lowest committment level
1183 * obtained by any of the RPCs.
1184 */
1185 if (committed == NFSV3WRITE_FILESYNC)
1186 committed = commit;
1187 else if (committed == NFSV3WRITE_DATASYNC &&
1188 commit == NFSV3WRITE_UNSTABLE)
1189 committed = commit;
1190 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0){
1191 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1192 NFSX_V3WRITEVERF);
1193 nmp->nm_iflag |= NFSMNT_HASWRITEVERF;
1194 } else if (bcmp((caddr_t)tl,
1195 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1196 *must_commit = 1;
1197 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1198 NFSX_V3WRITEVERF);
1199 }
1200 }
1201 } else
1202 nfsm_loadattr(vp, (struct vattr *)0);
1203 if (wccflag)
1204 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr->va_mtime.tv_sec;
1205 m_freem(mrep);
1206 if (error)
1207 break;
1208 tsiz -= len;
1209 }
1210 nfsmout:
1211 *iomode = committed;
1212 if (error)
1213 uiop->uio_resid = tsiz;
1214 return (error);
1215 }
1216
1217 /*
1218 * nfs mknod rpc
1219 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1220 * mode set to specify the file type and the size field for rdev.
1221 */
1222 int
1223 nfs_mknodrpc(dvp, vpp, cnp, vap)
1224 register struct vnode *dvp;
1225 register struct vnode **vpp;
1226 register struct componentname *cnp;
1227 register struct vattr *vap;
1228 {
1229 register struct nfsv2_sattr *sp;
1230 register struct nfsv3_sattr *sp3;
1231 register u_int32_t *tl;
1232 register caddr_t cp;
1233 register int32_t t1, t2;
1234 struct vnode *newvp = (struct vnode *)0;
1235 struct nfsnode *np;
1236 char *cp2;
1237 caddr_t bpos, dpos;
1238 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1239 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1240 u_int32_t rdev;
1241 int v3 = NFS_ISV3(dvp);
1242
1243 if (vap->va_type == VCHR || vap->va_type == VBLK)
1244 rdev = txdr_unsigned(vap->va_rdev);
1245 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1246 rdev = nfs_xdrneg1;
1247 else {
1248 VOP_ABORTOP(dvp, cnp);
1249 vput(dvp);
1250 return (EOPNOTSUPP);
1251 }
1252 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1253 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1254 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1255 nfsm_fhtom(dvp, v3);
1256 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1257 if (v3) {
1258 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3SRVSATTR);
1259 *tl++ = vtonfsv3_type(vap->va_type);
1260 sp3 = (struct nfsv3_sattr *)tl;
1261 nfsm_v3sattr(sp3, vap);
1262 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1263 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1264 *tl++ = txdr_unsigned(major(vap->va_rdev));
1265 *tl = txdr_unsigned(minor(vap->va_rdev));
1266 }
1267 } else {
1268 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1269 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1270 sp->sa_uid = nfs_xdrneg1;
1271 sp->sa_gid = nfs_xdrneg1;
1272 sp->sa_size = rdev;
1273 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1274 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1275 }
1276 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_proc, cnp->cn_cred);
1277 if (!error) {
1278 nfsm_mtofh(dvp, newvp, v3, gotvp);
1279 if (!gotvp) {
1280 if (newvp) {
1281 vrele(newvp);
1282 newvp = (struct vnode *)0;
1283 }
1284 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1285 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np);
1286 if (!error)
1287 newvp = NFSTOV(np);
1288 }
1289 }
1290 if (v3)
1291 nfsm_wcc_data(dvp, wccflag);
1292 nfsm_reqdone;
1293 if (error) {
1294 if (newvp)
1295 vrele(newvp);
1296 } else {
1297 if (cnp->cn_flags & MAKEENTRY)
1298 cache_enter(dvp, newvp, cnp);
1299 *vpp = newvp;
1300 }
1301 FREE(cnp->cn_pnbuf, M_NAMEI);
1302 VTONFS(dvp)->n_flag |= NMODIFIED;
1303 if (!wccflag)
1304 VTONFS(dvp)->n_attrstamp = 0;
1305 vrele(dvp);
1306 return (error);
1307 }
1308
1309 /*
1310 * nfs mknod vop
1311 * just call nfs_mknodrpc() to do the work.
1312 */
1313 /* ARGSUSED */
1314 int
1315 nfs_mknod(v)
1316 void *v;
1317 {
1318 struct vop_mknod_args /* {
1319 struct vnode *a_dvp;
1320 struct vnode **a_vpp;
1321 struct componentname *a_cnp;
1322 struct vattr *a_vap;
1323 } */ *ap = v;
1324 struct vnode *newvp;
1325 int error;
1326
1327 error = nfs_mknodrpc(ap->a_dvp, &newvp, ap->a_cnp, ap->a_vap);
1328 if (!error)
1329 vrele(newvp);
1330 return (error);
1331 }
1332
1333 static u_long create_verf;
1334 /*
1335 * nfs file create call
1336 */
1337 int
1338 nfs_create(v)
1339 void *v;
1340 {
1341 struct vop_create_args /* {
1342 struct vnode *a_dvp;
1343 struct vnode **a_vpp;
1344 struct componentname *a_cnp;
1345 struct vattr *a_vap;
1346 } */ *ap = v;
1347 register struct vnode *dvp = ap->a_dvp;
1348 register struct vattr *vap = ap->a_vap;
1349 register struct componentname *cnp = ap->a_cnp;
1350 register struct nfsv2_sattr *sp;
1351 register struct nfsv3_sattr *sp3;
1352 register u_int32_t *tl;
1353 register caddr_t cp;
1354 register int32_t t1, t2;
1355 struct nfsnode *np = (struct nfsnode *)0;
1356 struct vnode *newvp = (struct vnode *)0;
1357 caddr_t bpos, dpos, cp2;
1358 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1359 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1360 int v3 = NFS_ISV3(dvp);
1361
1362 /*
1363 * Oops, not for me..
1364 */
1365 if (vap->va_type == VSOCK)
1366 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1367
1368 #ifdef VA_EXCLUSIVE
1369 if (vap->va_vaflags & VA_EXCLUSIVE)
1370 fmode |= O_EXCL;
1371 #endif
1372 again:
1373 nfsstats.rpccnt[NFSPROC_CREATE]++;
1374 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1375 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1376 nfsm_fhtom(dvp, v3);
1377 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1378 if (v3) {
1379 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1380 if (fmode & O_EXCL) {
1381 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1382 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1383 if (in_ifaddr.tqh_first)
1384 *tl++ = in_ifaddr.tqh_first->ia_addr.sin_addr.s_addr;
1385 else
1386 *tl++ = create_verf;
1387 *tl = ++create_verf;
1388 } else {
1389 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1390 nfsm_build(tl, u_int32_t *, NFSX_V3SRVSATTR);
1391 sp3 = (struct nfsv3_sattr *)tl;
1392 nfsm_v3sattr(sp3, vap);
1393 }
1394 } else {
1395 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1396 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1397 sp->sa_uid = nfs_xdrneg1;
1398 sp->sa_gid = nfs_xdrneg1;
1399 sp->sa_size = 0;
1400 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1401 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1402 }
1403 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_proc, cnp->cn_cred);
1404 if (!error) {
1405 nfsm_mtofh(dvp, newvp, v3, gotvp);
1406 if (!gotvp) {
1407 if (newvp) {
1408 vrele(newvp);
1409 newvp = (struct vnode *)0;
1410 }
1411 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1412 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np);
1413 if (!error)
1414 newvp = NFSTOV(np);
1415 }
1416 }
1417 if (v3)
1418 nfsm_wcc_data(dvp, wccflag);
1419 nfsm_reqdone;
1420 if (error) {
1421 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1422 fmode &= ~O_EXCL;
1423 goto again;
1424 }
1425 if (newvp)
1426 vrele(newvp);
1427 } else if (v3 && (fmode & O_EXCL))
1428 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_proc);
1429 if (!error) {
1430 if (cnp->cn_flags & MAKEENTRY)
1431 cache_enter(dvp, newvp, cnp);
1432 *ap->a_vpp = newvp;
1433 }
1434 FREE(cnp->cn_pnbuf, M_NAMEI);
1435 VTONFS(dvp)->n_flag |= NMODIFIED;
1436 if (!wccflag)
1437 VTONFS(dvp)->n_attrstamp = 0;
1438 vrele(dvp);
1439 return (error);
1440 }
1441
1442 /*
1443 * nfs file remove call
1444 * To try and make nfs semantics closer to ufs semantics, a file that has
1445 * other processes using the vnode is renamed instead of removed and then
1446 * removed later on the last close.
1447 * - If v_usecount > 1
1448 * If a rename is not already in the works
1449 * call nfs_sillyrename() to set it up
1450 * else
1451 * do the remove rpc
1452 */
1453 int
1454 nfs_remove(v)
1455 void *v;
1456 {
1457 struct vop_remove_args /* {
1458 struct vnodeop_desc *a_desc;
1459 struct vnode * a_dvp;
1460 struct vnode * a_vp;
1461 struct componentname * a_cnp;
1462 } */ *ap = v;
1463 register struct vnode *vp = ap->a_vp;
1464 register struct vnode *dvp = ap->a_dvp;
1465 register struct componentname *cnp = ap->a_cnp;
1466 register struct nfsnode *np = VTONFS(vp);
1467 int error = 0;
1468 struct vattr vattr;
1469
1470 #ifndef DIAGNOSTIC
1471 if ((cnp->cn_flags & HASBUF) == 0)
1472 panic("nfs_remove: no name");
1473 if (vp->v_usecount < 1)
1474 panic("nfs_remove: bad v_usecount");
1475 #endif
1476 if (vp->v_type == VDIR)
1477 error = EPERM;
1478 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1479 VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_proc) == 0 &&
1480 vattr.va_nlink > 1)) {
1481 /*
1482 * Purge the name cache so that the chance of a lookup for
1483 * the name succeeding while the remove is in progress is
1484 * minimized. Without node locking it can still happen, such
1485 * that an I/O op returns ESTALE, but since you get this if
1486 * another host removes the file..
1487 */
1488 cache_purge(vp);
1489 /*
1490 * throw away biocache buffers, mainly to avoid
1491 * unnecessary delayed writes later.
1492 */
1493 error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, cnp->cn_proc, 1);
1494 /* Do the rpc */
1495 if (error != EINTR)
1496 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1497 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc);
1498 /*
1499 * Kludge City: If the first reply to the remove rpc is lost..
1500 * the reply to the retransmitted request will be ENOENT
1501 * since the file was in fact removed
1502 * Therefore, we cheat and return success.
1503 */
1504 if (error == ENOENT)
1505 error = 0;
1506 } else if (!np->n_sillyrename)
1507 error = nfs_sillyrename(dvp, vp, cnp);
1508 FREE(cnp->cn_pnbuf, M_NAMEI);
1509 np->n_attrstamp = 0;
1510 vrele(dvp);
1511 vrele(vp);
1512 return (error);
1513 }
1514
1515 /*
1516 * nfs file remove rpc called from nfs_inactive
1517 */
1518 int
1519 nfs_removeit(sp)
1520 register struct sillyrename *sp;
1521 {
1522
1523 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1524 (struct proc *)0));
1525 }
1526
1527 /*
1528 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1529 */
1530 int
1531 nfs_removerpc(dvp, name, namelen, cred, proc)
1532 register struct vnode *dvp;
1533 const char *name;
1534 int namelen;
1535 struct ucred *cred;
1536 struct proc *proc;
1537 {
1538 register u_int32_t *tl;
1539 register caddr_t cp;
1540 register int32_t t1, t2;
1541 caddr_t bpos, dpos, cp2;
1542 int error = 0, wccflag = NFSV3_WCCRATTR;
1543 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1544 int v3 = NFS_ISV3(dvp);
1545
1546 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1547 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1548 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1549 nfsm_fhtom(dvp, v3);
1550 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1551 nfsm_request(dvp, NFSPROC_REMOVE, proc, cred);
1552 if (v3)
1553 nfsm_wcc_data(dvp, wccflag);
1554 nfsm_reqdone;
1555 VTONFS(dvp)->n_flag |= NMODIFIED;
1556 if (!wccflag)
1557 VTONFS(dvp)->n_attrstamp = 0;
1558 return (error);
1559 }
1560
1561 /*
1562 * nfs file rename call
1563 */
1564 int
1565 nfs_rename(v)
1566 void *v;
1567 {
1568 struct vop_rename_args /* {
1569 struct vnode *a_fdvp;
1570 struct vnode *a_fvp;
1571 struct componentname *a_fcnp;
1572 struct vnode *a_tdvp;
1573 struct vnode *a_tvp;
1574 struct componentname *a_tcnp;
1575 } */ *ap = v;
1576 register struct vnode *fvp = ap->a_fvp;
1577 register struct vnode *tvp = ap->a_tvp;
1578 register struct vnode *fdvp = ap->a_fdvp;
1579 register struct vnode *tdvp = ap->a_tdvp;
1580 register struct componentname *tcnp = ap->a_tcnp;
1581 register struct componentname *fcnp = ap->a_fcnp;
1582 int error;
1583
1584 #ifndef DIAGNOSTIC
1585 if ((tcnp->cn_flags & HASBUF) == 0 ||
1586 (fcnp->cn_flags & HASBUF) == 0)
1587 panic("nfs_rename: no name");
1588 #endif
1589 /* Check for cross-device rename */
1590 if ((fvp->v_mount != tdvp->v_mount) ||
1591 (tvp && (fvp->v_mount != tvp->v_mount))) {
1592 error = EXDEV;
1593 goto out;
1594 }
1595
1596 /*
1597 * If the tvp exists and is in use, sillyrename it before doing the
1598 * rename of the new file over it.
1599 */
1600 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1601 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1602 vrele(tvp);
1603 tvp = NULL;
1604 }
1605
1606 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1607 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1608 tcnp->cn_proc);
1609
1610 if (fvp->v_type == VDIR) {
1611 if (tvp != NULL && tvp->v_type == VDIR)
1612 cache_purge(tdvp);
1613 cache_purge(fdvp);
1614 }
1615 out:
1616 if (tdvp == tvp)
1617 vrele(tdvp);
1618 else
1619 vput(tdvp);
1620 if (tvp)
1621 vput(tvp);
1622 vrele(fdvp);
1623 vrele(fvp);
1624 /*
1625 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1626 */
1627 if (error == ENOENT)
1628 error = 0;
1629 return (error);
1630 }
1631
1632 /*
1633 * nfs file rename rpc called from nfs_remove() above
1634 */
1635 int
1636 nfs_renameit(sdvp, scnp, sp)
1637 struct vnode *sdvp;
1638 struct componentname *scnp;
1639 register struct sillyrename *sp;
1640 {
1641 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1642 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_proc));
1643 }
1644
1645 /*
1646 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1647 */
1648 int
1649 nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, proc)
1650 register struct vnode *fdvp;
1651 const char *fnameptr;
1652 int fnamelen;
1653 register struct vnode *tdvp;
1654 const char *tnameptr;
1655 int tnamelen;
1656 struct ucred *cred;
1657 struct proc *proc;
1658 {
1659 register u_int32_t *tl;
1660 register caddr_t cp;
1661 register int32_t t1, t2;
1662 caddr_t bpos, dpos, cp2;
1663 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1664 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1665 int v3 = NFS_ISV3(fdvp);
1666
1667 nfsstats.rpccnt[NFSPROC_RENAME]++;
1668 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1669 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1670 nfsm_rndup(tnamelen));
1671 nfsm_fhtom(fdvp, v3);
1672 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1673 nfsm_fhtom(tdvp, v3);
1674 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1675 nfsm_request(fdvp, NFSPROC_RENAME, proc, cred);
1676 if (v3) {
1677 nfsm_wcc_data(fdvp, fwccflag);
1678 nfsm_wcc_data(tdvp, twccflag);
1679 }
1680 nfsm_reqdone;
1681 VTONFS(fdvp)->n_flag |= NMODIFIED;
1682 VTONFS(tdvp)->n_flag |= NMODIFIED;
1683 if (!fwccflag)
1684 VTONFS(fdvp)->n_attrstamp = 0;
1685 if (!twccflag)
1686 VTONFS(tdvp)->n_attrstamp = 0;
1687 return (error);
1688 }
1689
1690 /*
1691 * nfs hard link create call
1692 */
1693 int
1694 nfs_link(v)
1695 void *v;
1696 {
1697 struct vop_link_args /* {
1698 struct vnode *a_dvp;
1699 struct vnode *a_vp;
1700 struct componentname *a_cnp;
1701 } */ *ap = v;
1702 register struct vnode *vp = ap->a_vp;
1703 register struct vnode *dvp = ap->a_dvp;
1704 register struct componentname *cnp = ap->a_cnp;
1705 register u_int32_t *tl;
1706 register caddr_t cp;
1707 register int32_t t1, t2;
1708 caddr_t bpos, dpos, cp2;
1709 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1710 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1711 int v3;
1712
1713 if (dvp->v_mount != vp->v_mount) {
1714 VOP_ABORTOP(vp, cnp);
1715 vput(dvp);
1716 return (EXDEV);
1717 }
1718
1719 /*
1720 * Push all writes to the server, so that the attribute cache
1721 * doesn't get "out of sync" with the server.
1722 * XXX There should be a better way!
1723 */
1724 VOP_FSYNC(vp, cnp->cn_cred, MNT_WAIT, cnp->cn_proc);
1725
1726 v3 = NFS_ISV3(vp);
1727 nfsstats.rpccnt[NFSPROC_LINK]++;
1728 nfsm_reqhead(vp, NFSPROC_LINK,
1729 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1730 nfsm_fhtom(vp, v3);
1731 nfsm_fhtom(dvp, v3);
1732 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1733 nfsm_request(vp, NFSPROC_LINK, cnp->cn_proc, cnp->cn_cred);
1734 if (v3) {
1735 nfsm_postop_attr(vp, attrflag);
1736 nfsm_wcc_data(dvp, wccflag);
1737 }
1738 nfsm_reqdone;
1739 FREE(cnp->cn_pnbuf, M_NAMEI);
1740 VTONFS(dvp)->n_flag |= NMODIFIED;
1741 if (!attrflag)
1742 VTONFS(vp)->n_attrstamp = 0;
1743 if (!wccflag)
1744 VTONFS(dvp)->n_attrstamp = 0;
1745 vput(dvp);
1746 /*
1747 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1748 */
1749 if (error == EEXIST)
1750 error = 0;
1751 return (error);
1752 }
1753
1754 /*
1755 * nfs symbolic link create call
1756 */
1757 int
1758 nfs_symlink(v)
1759 void *v;
1760 {
1761 struct vop_symlink_args /* {
1762 struct vnode *a_dvp;
1763 struct vnode **a_vpp;
1764 struct componentname *a_cnp;
1765 struct vattr *a_vap;
1766 char *a_target;
1767 } */ *ap = v;
1768 register struct vnode *dvp = ap->a_dvp;
1769 register struct vattr *vap = ap->a_vap;
1770 register struct componentname *cnp = ap->a_cnp;
1771 register struct nfsv2_sattr *sp;
1772 register struct nfsv3_sattr *sp3;
1773 register u_int32_t *tl;
1774 register caddr_t cp;
1775 register int32_t t1, t2;
1776 caddr_t bpos, dpos, cp2;
1777 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1778 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1779 struct vnode *newvp = (struct vnode *)0;
1780 int v3 = NFS_ISV3(dvp);
1781
1782 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1783 slen = strlen(ap->a_target);
1784 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1785 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1786 nfsm_fhtom(dvp, v3);
1787 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1788 if (v3) {
1789 nfsm_build(sp3, struct nfsv3_sattr *, NFSX_V3SRVSATTR);
1790 nfsm_v3sattr(sp3, vap);
1791 }
1792 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1793 if (!v3) {
1794 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1795 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1796 sp->sa_uid = nfs_xdrneg1;
1797 sp->sa_gid = nfs_xdrneg1;
1798 sp->sa_size = nfs_xdrneg1;
1799 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1800 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1801 }
1802 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_proc, cnp->cn_cred);
1803 if (v3) {
1804 if (!error)
1805 nfsm_mtofh(dvp, newvp, v3, gotvp);
1806 nfsm_wcc_data(dvp, wccflag);
1807 }
1808 nfsm_reqdone;
1809 if (newvp)
1810 vrele(newvp);
1811 FREE(cnp->cn_pnbuf, M_NAMEI);
1812 VTONFS(dvp)->n_flag |= NMODIFIED;
1813 if (!wccflag)
1814 VTONFS(dvp)->n_attrstamp = 0;
1815 vrele(dvp);
1816 /*
1817 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1818 */
1819 if (error == EEXIST)
1820 error = 0;
1821 return (error);
1822 }
1823
1824 /*
1825 * nfs make dir call
1826 */
1827 int
1828 nfs_mkdir(v)
1829 void *v;
1830 {
1831 struct vop_mkdir_args /* {
1832 struct vnode *a_dvp;
1833 struct vnode **a_vpp;
1834 struct componentname *a_cnp;
1835 struct vattr *a_vap;
1836 } */ *ap = v;
1837 register struct vnode *dvp = ap->a_dvp;
1838 register struct vattr *vap = ap->a_vap;
1839 register struct componentname *cnp = ap->a_cnp;
1840 register struct nfsv2_sattr *sp;
1841 register struct nfsv3_sattr *sp3;
1842 register u_int32_t *tl;
1843 register caddr_t cp;
1844 register int32_t t1, t2;
1845 register int len;
1846 struct nfsnode *np = (struct nfsnode *)0;
1847 struct vnode *newvp = (struct vnode *)0;
1848 caddr_t bpos, dpos, cp2;
1849 int error = 0, wccflag = NFSV3_WCCRATTR;
1850 int gotvp = 0;
1851 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1852 int v3 = NFS_ISV3(dvp);
1853
1854 len = cnp->cn_namelen;
1855 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1856 nfsm_reqhead(dvp, NFSPROC_MKDIR,
1857 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1858 nfsm_fhtom(dvp, v3);
1859 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1860 if (v3) {
1861 nfsm_build(sp3, struct nfsv3_sattr *, NFSX_V3SRVSATTR);
1862 nfsm_v3sattr(sp3, vap);
1863 } else {
1864 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1865 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1866 sp->sa_uid = nfs_xdrneg1;
1867 sp->sa_gid = nfs_xdrneg1;
1868 sp->sa_size = nfs_xdrneg1;
1869 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1870 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1871 }
1872 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_proc, cnp->cn_cred);
1873 if (!error)
1874 nfsm_mtofh(dvp, newvp, v3, gotvp);
1875 if (v3)
1876 nfsm_wcc_data(dvp, wccflag);
1877 nfsm_reqdone;
1878 VTONFS(dvp)->n_flag |= NMODIFIED;
1879 if (!wccflag)
1880 VTONFS(dvp)->n_attrstamp = 0;
1881 /*
1882 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1883 * if we can succeed in looking up the directory.
1884 */
1885 if (error == EEXIST || (!error && !gotvp)) {
1886 if (newvp) {
1887 vrele(newvp);
1888 newvp = (struct vnode *)0;
1889 }
1890 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
1891 cnp->cn_proc, &np);
1892 if (!error) {
1893 newvp = NFSTOV(np);
1894 if (newvp->v_type != VDIR)
1895 error = EEXIST;
1896 }
1897 }
1898 if (error) {
1899 if (newvp)
1900 vrele(newvp);
1901 } else {
1902 if (cnp->cn_flags & MAKEENTRY)
1903 cache_enter(dvp, newvp, cnp);
1904 *ap->a_vpp = newvp;
1905 }
1906 FREE(cnp->cn_pnbuf, M_NAMEI);
1907 vrele(dvp);
1908 return (error);
1909 }
1910
1911 /*
1912 * nfs remove directory call
1913 */
1914 int
1915 nfs_rmdir(v)
1916 void *v;
1917 {
1918 struct vop_rmdir_args /* {
1919 struct vnode *a_dvp;
1920 struct vnode *a_vp;
1921 struct componentname *a_cnp;
1922 } */ *ap = v;
1923 register struct vnode *vp = ap->a_vp;
1924 register struct vnode *dvp = ap->a_dvp;
1925 register struct componentname *cnp = ap->a_cnp;
1926 register u_int32_t *tl;
1927 register caddr_t cp;
1928 register int32_t t1, t2;
1929 caddr_t bpos, dpos, cp2;
1930 int error = 0, wccflag = NFSV3_WCCRATTR;
1931 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1932 int v3 = NFS_ISV3(dvp);
1933
1934 if (dvp == vp) {
1935 vrele(dvp);
1936 vrele(dvp);
1937 FREE(cnp->cn_pnbuf, M_NAMEI);
1938 return (EINVAL);
1939 }
1940 nfsstats.rpccnt[NFSPROC_RMDIR]++;
1941 nfsm_reqhead(dvp, NFSPROC_RMDIR,
1942 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1943 nfsm_fhtom(dvp, v3);
1944 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1945 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_proc, cnp->cn_cred);
1946 if (v3)
1947 nfsm_wcc_data(dvp, wccflag);
1948 nfsm_reqdone;
1949 FREE(cnp->cn_pnbuf, M_NAMEI);
1950 VTONFS(dvp)->n_flag |= NMODIFIED;
1951 if (!wccflag)
1952 VTONFS(dvp)->n_attrstamp = 0;
1953 cache_purge(dvp);
1954 cache_purge(vp);
1955 vrele(vp);
1956 vrele(dvp);
1957 /*
1958 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
1959 */
1960 if (error == ENOENT)
1961 error = 0;
1962 return (error);
1963 }
1964
1965 /*
1966 * nfs readdir call
1967 */
1968 int
1969 nfs_readdir(v)
1970 void *v;
1971 {
1972 struct vop_readdir_args /* {
1973 struct vnode *a_vp;
1974 struct uio *a_uio;
1975 struct ucred *a_cred;
1976 int *a_eofflag;
1977 off_t **a_cookies;
1978 int *a_ncookies;
1979 } */ *ap = v;
1980 register struct vnode *vp = ap->a_vp;
1981 register struct uio *uio = ap->a_uio;
1982 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1983 char *base = uio->uio_iov->iov_base;
1984 int tresid, error;
1985 size_t count, lost;
1986
1987 if (vp->v_type != VDIR)
1988 return (EPERM);
1989
1990 lost = uio->uio_resid & (NFS_DIRFRAGSIZ - 1);
1991 count = uio->uio_resid - lost;
1992 if (count <= 0)
1993 return (EINVAL);
1994
1995 /*
1996 * Call nfs_bioread() to do the real work.
1997 */
1998 tresid = uio->uio_resid = count;
1999 error = nfs_bioread(vp, uio, 0, ap->a_cred,
2000 ap->a_cookies ? NFSBIO_CACHECOOKIES : 0);
2001
2002 if (!error && uio->uio_resid == tresid) {
2003 uio->uio_resid += lost;
2004 nfsstats.direofcache_misses++;
2005 *ap->a_eofflag = 1;
2006 return (0);
2007 }
2008
2009 if (!error && ap->a_cookies) {
2010 struct dirent *dp;
2011 off_t *cookies;
2012 int ncookies;
2013
2014 ncookies = uio->uio_resid / 16;
2015 MALLOC(cookies, off_t *, sizeof (off_t) * ncookies, M_TEMP,
2016 M_WAITOK);
2017 /*
2018 * Only the NFS server and emulations use cookies, and they
2019 * load the directory block into system space, so we can
2020 * just look at it directly.
2021 */
2022 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2023 panic("nfs_readdir: lost in space");
2024 while (ncookies-- && base < uio->uio_iov->iov_base) {
2025 dp = (struct dirent *) base;
2026 if (dp->d_reclen == 0)
2027 break;
2028 if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
2029 *(cookies++) = (off_t)NFS_GETCOOKIE32(dp);
2030 else
2031 *(cookies++) = NFS_GETCOOKIE(dp);
2032 base += dp->d_reclen;
2033 }
2034 uio->uio_resid += (uio->uio_iov->iov_base - base);
2035 uio->uio_iov->iov_len += (uio->uio_iov->iov_base - base);
2036 uio->uio_iov->iov_base = base;
2037 *ap->a_cookies = cookies;
2038 *ap->a_ncookies = ncookies;
2039 }
2040
2041 uio->uio_resid += lost;
2042 *ap->a_eofflag = 0;
2043 return (error);
2044 }
2045
2046 /*
2047 * Readdir rpc call.
2048 * Called from below the buffer cache by nfs_doio().
2049 */
2050 int
2051 nfs_readdirrpc(vp, uiop, cred)
2052 struct vnode *vp;
2053 register struct uio *uiop;
2054 struct ucred *cred;
2055 {
2056 register int len, left;
2057 register struct dirent *dp = NULL;
2058 register u_int32_t *tl;
2059 register caddr_t cp;
2060 register int32_t t1, t2;
2061 caddr_t bpos, dpos, cp2;
2062 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2063 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2064 struct nfsnode *dnp = VTONFS(vp);
2065 u_quad_t fileno;
2066 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2067 int attrflag, nrpcs = 0, reclen;
2068 int v3 = NFS_ISV3(vp);
2069 nfsquad_t cookie;
2070
2071 #ifdef DIAGNOSTIC
2072 /*
2073 * Should be called from buffer cache, so only amount of
2074 * NFS_DIRBLKSIZ will be requested.
2075 */
2076 if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1)))
2077 panic("nfs readdirrpc bad uio");
2078 #endif
2079
2080 /*
2081 * Loop around doing readdir rpc's of size nm_readdirsize
2082 * truncated to a multiple of NFS_DIRFRAGSIZ.
2083 * The stopping criteria is EOF or buffer full.
2084 */
2085 while (more_dirs && bigenough) {
2086 /*
2087 * Heuristic: don't bother to do another RPC to further
2088 * fill up this block if there is not much room left. (< 50%
2089 * of the readdir RPC size). This wastes some buffer space
2090 * but can save up to 50% in RPC calls.
2091 */
2092 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
2093 bigenough = 0;
2094 break;
2095 }
2096 nfsstats.rpccnt[NFSPROC_READDIR]++;
2097 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2098 NFSX_READDIR(v3));
2099 nfsm_fhtom(vp, v3);
2100 if (v3) {
2101 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2102 cookie.qval = uiop->uio_offset;
2103 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2104 txdr_swapcookie3(uiop->uio_offset, tl);
2105 } else {
2106 txdr_cookie3(uiop->uio_offset, tl);
2107 }
2108 tl += 2;
2109 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2110 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2111 } else {
2112 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2113 *tl++ = txdr_unsigned(uiop->uio_offset);
2114 }
2115 *tl = txdr_unsigned(nmp->nm_readdirsize);
2116 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_procp, cred);
2117 nrpcs++;
2118 if (v3) {
2119 nfsm_postop_attr(vp, attrflag);
2120 if (!error) {
2121 nfsm_dissect(tl, u_int32_t *,
2122 2 * NFSX_UNSIGNED);
2123 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2124 dnp->n_cookieverf.nfsuquad[1] = *tl;
2125 } else {
2126 m_freem(mrep);
2127 goto nfsmout;
2128 }
2129 }
2130 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2131 more_dirs = fxdr_unsigned(int, *tl);
2132
2133 /* loop thru the dir entries, doctoring them to 4bsd form */
2134 while (more_dirs && bigenough) {
2135 if (v3) {
2136 nfsm_dissect(tl, u_int32_t *,
2137 3 * NFSX_UNSIGNED);
2138 fxdr_hyper(tl, &fileno);
2139 len = fxdr_unsigned(int, *(tl + 2));
2140 } else {
2141 nfsm_dissect(tl, u_int32_t *,
2142 2 * NFSX_UNSIGNED);
2143 fileno = fxdr_unsigned(u_quad_t, *tl++);
2144 len = fxdr_unsigned(int, *tl);
2145 }
2146 if (len <= 0 || len > NFS_MAXNAMLEN) {
2147 error = EBADRPC;
2148 m_freem(mrep);
2149 goto nfsmout;
2150 }
2151 tlen = nfsm_rndup(len);
2152 if (tlen == len)
2153 tlen += 4; /* To ensure null termination */
2154 tlen += sizeof (off_t) + sizeof (int);
2155 reclen = ALIGN(tlen + DIRHDSIZ);
2156 tlen = reclen - DIRHDSIZ;
2157 left = NFS_DIRFRAGSIZ - blksiz;
2158 if (reclen > left) {
2159 dp->d_reclen += left;
2160 uiop->uio_iov->iov_base += left;
2161 uiop->uio_iov->iov_len -= left;
2162 uiop->uio_resid -= left;
2163 blksiz = 0;
2164 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2165 }
2166 if (reclen > uiop->uio_resid)
2167 bigenough = 0;
2168 if (bigenough) {
2169 dp = (struct dirent *)uiop->uio_iov->iov_base;
2170 dp->d_fileno = (int)fileno;
2171 dp->d_namlen = len;
2172 dp->d_reclen = reclen;
2173 dp->d_type = DT_UNKNOWN;
2174 blksiz += dp->d_reclen;
2175 if (blksiz == NFS_DIRFRAGSIZ)
2176 blksiz = 0;
2177 uiop->uio_resid -= DIRHDSIZ;
2178 uiop->uio_iov->iov_base += DIRHDSIZ;
2179 uiop->uio_iov->iov_len -= DIRHDSIZ;
2180 nfsm_mtouio(uiop, len);
2181 cp = uiop->uio_iov->iov_base;
2182 tlen -= len;
2183 *cp = '\0'; /* null terminate */
2184 uiop->uio_iov->iov_base += tlen;
2185 uiop->uio_iov->iov_len -= tlen;
2186 uiop->uio_resid -= tlen;
2187 } else
2188 nfsm_adv(nfsm_rndup(len));
2189 if (v3) {
2190 nfsm_dissect(tl, u_int32_t *,
2191 3 * NFSX_UNSIGNED);
2192 } else {
2193 nfsm_dissect(tl, u_int32_t *,
2194 2 * NFSX_UNSIGNED);
2195 }
2196 if (bigenough) {
2197 if (v3) {
2198 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2199 uiop->uio_offset =
2200 fxdr_swapcookie3(tl);
2201 else
2202 uiop->uio_offset =
2203 fxdr_cookie3(tl);
2204 }
2205 else {
2206 uiop->uio_offset =
2207 fxdr_unsigned(off_t, *tl);
2208 }
2209 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2210 }
2211 if (v3)
2212 tl += 2;
2213 else
2214 tl++;
2215 more_dirs = fxdr_unsigned(int, *tl);
2216 }
2217 /*
2218 * If at end of rpc data, get the eof boolean
2219 */
2220 if (!more_dirs) {
2221 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2222 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2223 }
2224 m_freem(mrep);
2225 }
2226 /*
2227 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2228 * by increasing d_reclen for the last record.
2229 */
2230 if (blksiz > 0) {
2231 left = NFS_DIRFRAGSIZ - blksiz;
2232 dp->d_reclen += left;
2233 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2234 uiop->uio_iov->iov_base += left;
2235 uiop->uio_iov->iov_len -= left;
2236 uiop->uio_resid -= left;
2237 }
2238
2239 /*
2240 * We are now either at the end of the directory or have filled the
2241 * block.
2242 */
2243 if (bigenough)
2244 dnp->n_direofoffset = uiop->uio_offset;
2245 nfsmout:
2246 return (error);
2247 }
2248
2249 /*
2250 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2251 */
2252 int
2253 nfs_readdirplusrpc(vp, uiop, cred)
2254 struct vnode *vp;
2255 register struct uio *uiop;
2256 struct ucred *cred;
2257 {
2258 register int len, left;
2259 register struct dirent *dp = NULL;
2260 register u_int32_t *tl;
2261 register caddr_t cp;
2262 register int32_t t1, t2;
2263 struct vnode *newvp;
2264 caddr_t bpos, dpos, cp2;
2265 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2266 struct nameidata nami, *ndp = &nami;
2267 struct componentname *cnp = &ndp->ni_cnd;
2268 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2269 struct nfsnode *dnp = VTONFS(vp), *np;
2270 const unsigned char *hcp;
2271 nfsfh_t *fhp;
2272 u_quad_t fileno;
2273 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2274 int attrflag, fhsize, nrpcs = 0, reclen;
2275 struct nfs_fattr fattr, *fp;
2276
2277 #ifdef DIAGNOSTIC
2278 if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1)))
2279 panic("nfs readdirplusrpc bad uio");
2280 #endif
2281 ndp->ni_dvp = vp;
2282 newvp = NULLVP;
2283
2284 /*
2285 * Loop around doing readdir rpc's of size nm_readdirsize
2286 * truncated to a multiple of NFS_DIRFRAGSIZ.
2287 * The stopping criteria is EOF or buffer full.
2288 */
2289 while (more_dirs && bigenough) {
2290 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
2291 bigenough = 0;
2292 break;
2293 }
2294 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2295 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2296 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2297 nfsm_fhtom(vp, 1);
2298 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2299 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2300 txdr_swapcookie3(uiop->uio_offset, tl);
2301 } else {
2302 txdr_cookie3(uiop->uio_offset, tl);
2303 }
2304 tl += 2;
2305 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2306 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2307 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2308 *tl = txdr_unsigned(nmp->nm_rsize);
2309 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_procp, cred);
2310 nfsm_postop_attr(vp, attrflag);
2311 if (error) {
2312 m_freem(mrep);
2313 goto nfsmout;
2314 }
2315 nrpcs++;
2316 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2317 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2318 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2319 more_dirs = fxdr_unsigned(int, *tl);
2320
2321 /* loop thru the dir entries, doctoring them to 4bsd form */
2322 while (more_dirs && bigenough) {
2323 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2324 fxdr_hyper(tl, &fileno);
2325 len = fxdr_unsigned(int, *(tl + 2));
2326 if (len <= 0 || len > NFS_MAXNAMLEN) {
2327 error = EBADRPC;
2328 m_freem(mrep);
2329 goto nfsmout;
2330 }
2331 tlen = nfsm_rndup(len);
2332 if (tlen == len)
2333 tlen += 4; /* To ensure null termination*/
2334 tlen += sizeof (off_t) + sizeof (int);
2335 reclen = ALIGN(tlen + DIRHDSIZ);
2336 tlen = reclen - DIRHDSIZ;
2337 left = NFS_DIRFRAGSIZ - blksiz;
2338 if (reclen > left) {
2339 /*
2340 * DIRFRAGSIZ is aligned, no need to align
2341 * again here.
2342 */
2343 dp->d_reclen += left;
2344 uiop->uio_iov->iov_base += left;
2345 uiop->uio_iov->iov_len -= left;
2346 uiop->uio_resid -= left;
2347 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2348 blksiz = 0;
2349 }
2350 if (reclen > uiop->uio_resid)
2351 bigenough = 0;
2352 if (bigenough) {
2353 dp = (struct dirent *)uiop->uio_iov->iov_base;
2354 dp->d_fileno = (int)fileno;
2355 dp->d_namlen = len;
2356 dp->d_reclen = reclen;
2357 dp->d_type = DT_UNKNOWN;
2358 blksiz += dp->d_reclen;
2359 if (blksiz == NFS_DIRFRAGSIZ)
2360 blksiz = 0;
2361 uiop->uio_resid -= DIRHDSIZ;
2362 uiop->uio_iov->iov_base += DIRHDSIZ;
2363 uiop->uio_iov->iov_len -= DIRHDSIZ;
2364 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2365 cnp->cn_namelen = len;
2366 nfsm_mtouio(uiop, len);
2367 cp = uiop->uio_iov->iov_base;
2368 tlen -= len;
2369 *cp = '\0';
2370 uiop->uio_iov->iov_base += tlen;
2371 uiop->uio_iov->iov_len -= tlen;
2372 uiop->uio_resid -= tlen;
2373 } else
2374 nfsm_adv(nfsm_rndup(len));
2375 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2376 if (bigenough) {
2377 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2378 uiop->uio_offset =
2379 fxdr_swapcookie3(tl);
2380 else
2381 uiop->uio_offset =
2382 fxdr_cookie3(tl);
2383 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2384 }
2385 tl += 2;
2386
2387 /*
2388 * Since the attributes are before the file handle
2389 * (sigh), we must skip over the attributes and then
2390 * come back and get them.
2391 */
2392 attrflag = fxdr_unsigned(int, *tl);
2393 if (attrflag) {
2394 nfsm_dissect(fp, struct nfs_fattr *, NFSX_V3FATTR);
2395 bcopy(fp, &fattr, NFSX_V3FATTR);
2396 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2397 doit = fxdr_unsigned(int, *tl);
2398 if (doit) {
2399 nfsm_getfh(fhp, fhsize, 1);
2400 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2401 VREF(vp);
2402 newvp = vp;
2403 np = dnp;
2404 } else {
2405 error = nfs_nget(vp->v_mount, fhp,
2406 fhsize, &np);
2407 if (!error)
2408 newvp = NFSTOV(np);
2409 }
2410 if (!error) {
2411 nfs_loadattrcache(&newvp, &fattr, 0);
2412 dp->d_type =
2413 IFTODT(VTTOIF(np->n_vattr->va_type));
2414 ndp->ni_vp = newvp;
2415 cnp->cn_hash = 0;
2416 for (hcp = cnp->cn_nameptr, i = 1; i <= len;
2417 i++, hcp++)
2418 cnp->cn_hash += *hcp * i;
2419 if (cnp->cn_namelen <= NCHNAMLEN)
2420 cache_enter(ndp->ni_dvp, ndp->ni_vp,
2421 cnp);
2422 }
2423 }
2424 } else {
2425 /* Just skip over the file handle */
2426 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2427 i = fxdr_unsigned(int, *tl);
2428 nfsm_adv(nfsm_rndup(i));
2429 }
2430 if (newvp != NULLVP) {
2431 vrele(newvp);
2432 newvp = NULLVP;
2433 }
2434 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2435 more_dirs = fxdr_unsigned(int, *tl);
2436 }
2437 /*
2438 * If at end of rpc data, get the eof boolean
2439 */
2440 if (!more_dirs) {
2441 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2442 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2443 }
2444 m_freem(mrep);
2445 }
2446 /*
2447 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2448 * by increasing d_reclen for the last record.
2449 */
2450 if (blksiz > 0) {
2451 left = NFS_DIRFRAGSIZ - blksiz;
2452 dp->d_reclen += left;
2453 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2454 uiop->uio_iov->iov_base += left;
2455 uiop->uio_iov->iov_len -= left;
2456 uiop->uio_resid -= left;
2457 }
2458
2459 /*
2460 * We are now either at the end of the directory or have filled the
2461 * block.
2462 */
2463 if (bigenough)
2464 dnp->n_direofoffset = uiop->uio_offset;
2465 nfsmout:
2466 if (newvp != NULLVP)
2467 vrele(newvp);
2468 return (error);
2469 }
2470 static char hextoasc[] = "0123456789abcdef";
2471
2472 /*
2473 * Silly rename. To make the NFS filesystem that is stateless look a little
2474 * more like the "ufs" a remove of an active vnode is translated to a rename
2475 * to a funny looking filename that is removed by nfs_inactive on the
2476 * nfsnode. There is the potential for another process on a different client
2477 * to create the same funny name between the nfs_lookitup() fails and the
2478 * nfs_rename() completes, but...
2479 */
2480 int
2481 nfs_sillyrename(dvp, vp, cnp)
2482 struct vnode *dvp, *vp;
2483 struct componentname *cnp;
2484 {
2485 register struct sillyrename *sp;
2486 struct nfsnode *np;
2487 int error;
2488 short pid;
2489
2490 cache_purge(dvp);
2491 np = VTONFS(vp);
2492 #ifndef DIAGNOSTIC
2493 if (vp->v_type == VDIR)
2494 panic("nfs: sillyrename dir");
2495 #endif
2496 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2497 M_NFSREQ, M_WAITOK);
2498 sp->s_cred = crdup(cnp->cn_cred);
2499 sp->s_dvp = dvp;
2500 VREF(dvp);
2501
2502 /* Fudge together a funny name */
2503 pid = cnp->cn_proc->p_pid;
2504 bcopy(".nfsAxxxx4.4", sp->s_name, 13);
2505 sp->s_namlen = 12;
2506 sp->s_name[8] = hextoasc[pid & 0xf];
2507 sp->s_name[7] = hextoasc[(pid >> 4) & 0xf];
2508 sp->s_name[6] = hextoasc[(pid >> 8) & 0xf];
2509 sp->s_name[5] = hextoasc[(pid >> 12) & 0xf];
2510
2511 /* Try lookitups until we get one that isn't there */
2512 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2513 cnp->cn_proc, (struct nfsnode **)0) == 0) {
2514 sp->s_name[4]++;
2515 if (sp->s_name[4] > 'z') {
2516 error = EINVAL;
2517 goto bad;
2518 }
2519 }
2520 error = nfs_renameit(dvp, cnp, sp);
2521 if (error)
2522 goto bad;
2523 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2524 cnp->cn_proc, &np);
2525 np->n_sillyrename = sp;
2526 return (0);
2527 bad:
2528 vrele(sp->s_dvp);
2529 crfree(sp->s_cred);
2530 free((caddr_t)sp, M_NFSREQ);
2531 return (error);
2532 }
2533
2534 /*
2535 * Look up a file name and optionally either update the file handle or
2536 * allocate an nfsnode, depending on the value of npp.
2537 * npp == NULL --> just do the lookup
2538 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2539 * handled too
2540 * *npp != NULL --> update the file handle in the vnode
2541 */
2542 int
2543 nfs_lookitup(dvp, name, len, cred, procp, npp)
2544 register struct vnode *dvp;
2545 const char *name;
2546 int len;
2547 struct ucred *cred;
2548 struct proc *procp;
2549 struct nfsnode **npp;
2550 {
2551 register u_int32_t *tl;
2552 register caddr_t cp;
2553 register int32_t t1, t2;
2554 struct vnode *newvp = (struct vnode *)0;
2555 struct nfsnode *np, *dnp = VTONFS(dvp);
2556 caddr_t bpos, dpos, cp2;
2557 int error = 0, fhlen, attrflag;
2558 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2559 nfsfh_t *nfhp;
2560 int v3 = NFS_ISV3(dvp);
2561
2562 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2563 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2564 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2565 nfsm_fhtom(dvp, v3);
2566 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2567 nfsm_request(dvp, NFSPROC_LOOKUP, procp, cred);
2568 if (npp && !error) {
2569 nfsm_getfh(nfhp, fhlen, v3);
2570 if (*npp) {
2571 np = *npp;
2572 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2573 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2574 np->n_fhp = &np->n_fh;
2575 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2576 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2577 bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2578 np->n_fhsize = fhlen;
2579 newvp = NFSTOV(np);
2580 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2581 VREF(dvp);
2582 newvp = dvp;
2583 } else {
2584 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2585 if (error) {
2586 m_freem(mrep);
2587 return (error);
2588 }
2589 newvp = NFSTOV(np);
2590 }
2591 if (v3) {
2592 nfsm_postop_attr(newvp, attrflag);
2593 if (!attrflag && *npp == NULL) {
2594 m_freem(mrep);
2595 vrele(newvp);
2596 return (ENOENT);
2597 }
2598 } else
2599 nfsm_loadattr(newvp, (struct vattr *)0);
2600 }
2601 nfsm_reqdone;
2602 if (npp && *npp == NULL) {
2603 if (error) {
2604 if (newvp)
2605 vrele(newvp);
2606 } else
2607 *npp = np;
2608 }
2609 return (error);
2610 }
2611
2612 /*
2613 * Nfs Version 3 commit rpc
2614 */
2615 int
2616 nfs_commit(vp, offset, cnt, cred, procp)
2617 register struct vnode *vp;
2618 u_quad_t offset;
2619 int cnt;
2620 struct ucred *cred;
2621 struct proc *procp;
2622 {
2623 register caddr_t cp;
2624 register u_int32_t *tl;
2625 register int32_t t1, t2;
2626 register struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2627 caddr_t bpos, dpos, cp2;
2628 int error = 0, wccflag = NFSV3_WCCRATTR;
2629 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2630
2631 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0)
2632 return (0);
2633 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2634 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2635 nfsm_fhtom(vp, 1);
2636 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2637 txdr_hyper(&offset, tl);
2638 tl += 2;
2639 *tl = txdr_unsigned(cnt);
2640 nfsm_request(vp, NFSPROC_COMMIT, procp, cred);
2641 nfsm_wcc_data(vp, wccflag);
2642 if (!error) {
2643 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2644 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2645 NFSX_V3WRITEVERF)) {
2646 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2647 NFSX_V3WRITEVERF);
2648 error = NFSERR_STALEWRITEVERF;
2649 }
2650 }
2651 nfsm_reqdone;
2652 return (error);
2653 }
2654
2655 /*
2656 * Kludge City..
2657 * - make nfs_bmap() essentially a no-op that does no translation
2658 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2659 * (Maybe I could use the process's page mapping, but I was concerned that
2660 * Kernel Write might not be enabled and also figured copyout() would do
2661 * a lot more work than bcopy() and also it currently happens in the
2662 * context of the swapper process (2).
2663 */
2664 int
2665 nfs_bmap(v)
2666 void *v;
2667 {
2668 struct vop_bmap_args /* {
2669 struct vnode *a_vp;
2670 daddr_t a_bn;
2671 struct vnode **a_vpp;
2672 daddr_t *a_bnp;
2673 int *a_runp;
2674 } */ *ap = v;
2675 register struct vnode *vp = ap->a_vp;
2676
2677 if (ap->a_vpp != NULL)
2678 *ap->a_vpp = vp;
2679 if (ap->a_bnp != NULL)
2680 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2681 return (0);
2682 }
2683
2684 /*
2685 * Strategy routine.
2686 * For async requests when nfsiod(s) are running, queue the request by
2687 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2688 * request.
2689 */
2690 int
2691 nfs_strategy(v)
2692 void *v;
2693 {
2694 struct vop_strategy_args *ap = v;
2695 register struct buf *bp = ap->a_bp;
2696 struct ucred *cr;
2697 struct proc *p;
2698 int error = 0;
2699
2700 if ((bp->b_flags & (B_PHYS|B_ASYNC)) == (B_PHYS|B_ASYNC))
2701 panic("nfs physio/async");
2702 if (bp->b_flags & B_ASYNC)
2703 p = (struct proc *)0;
2704 else
2705 p = curproc; /* XXX */
2706 if (bp->b_flags & B_READ)
2707 cr = bp->b_rcred;
2708 else
2709 cr = bp->b_wcred;
2710 /*
2711 * If the op is asynchronous and an i/o daemon is waiting
2712 * queue the request, wake it up and wait for completion
2713 * otherwise just do it ourselves.
2714 */
2715 if ((bp->b_flags & B_ASYNC) == 0 ||
2716 nfs_asyncio(bp, NOCRED))
2717 error = nfs_doio(bp, cr, p);
2718 return (error);
2719 }
2720
2721 /*
2722 * Mmap a file
2723 *
2724 * NB Currently unsupported.
2725 */
2726 /* ARGSUSED */
2727 int
2728 nfs_mmap(v)
2729 void *v;
2730 {
2731 #if 0
2732 struct vop_mmap_args /* {
2733 struct vnode *a_vp;
2734 int a_fflags;
2735 struct ucred *a_cred;
2736 struct proc *a_p;
2737 } */ *ap = v;
2738 #endif
2739
2740 return (EINVAL);
2741 }
2742
2743 /*
2744 * fsync vnode op. Just call nfs_flush() with commit == 1.
2745 */
2746 /* ARGSUSED */
2747 int
2748 nfs_fsync(v)
2749 void *v;
2750 {
2751 struct vop_fsync_args /* {
2752 struct vnodeop_desc *a_desc;
2753 struct vnode * a_vp;
2754 struct ucred * a_cred;
2755 int a_waitfor;
2756 struct proc * a_p;
2757 } */ *ap = v;
2758
2759 return (nfs_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_p, 1));
2760 }
2761
2762 /*
2763 * Flush all the blocks associated with a vnode.
2764 * Walk through the buffer pool and push any dirty pages
2765 * associated with the vnode.
2766 */
2767 int
2768 nfs_flush(vp, cred, waitfor, p, commit)
2769 register struct vnode *vp;
2770 struct ucred *cred;
2771 int waitfor;
2772 struct proc *p;
2773 int commit;
2774 {
2775 register struct nfsnode *np = VTONFS(vp);
2776 register struct buf *bp;
2777 register int i;
2778 struct buf *nbp;
2779 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2780 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2781 int passone = 1;
2782 u_quad_t off = (u_quad_t)-1, endoff = 0, toff;
2783 struct ucred* wcred = NULL;
2784 #ifndef NFS_COMMITBVECSIZ
2785 #define NFS_COMMITBVECSIZ 20
2786 #endif
2787 struct buf *bvec[NFS_COMMITBVECSIZ];
2788
2789 if (nmp->nm_flag & NFSMNT_INT)
2790 slpflag = PCATCH;
2791 if (!commit)
2792 passone = 0;
2793 /*
2794 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2795 * server, but nas not been committed to stable storage on the server
2796 * yet. On the first pass, the byte range is worked out and the commit
2797 * rpc is done. On the second pass, nfs_writebp() is called to do the
2798 * job.
2799 */
2800 again:
2801 bvecpos = 0;
2802 if (NFS_ISV3(vp) && commit) {
2803 s = splbio();
2804 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
2805 nbp = bp->b_vnbufs.le_next;
2806 if (bvecpos >= NFS_COMMITBVECSIZ)
2807 break;
2808 if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
2809 != (B_DELWRI | B_NEEDCOMMIT))
2810 continue;
2811 bremfree(bp);
2812 /*
2813 * Work out if all buffers are using the same cred
2814 * so we can deal with them all with one commit.
2815 */
2816 if (wcred == NULL)
2817 wcred = bp->b_wcred;
2818 else if (wcred != bp->b_wcred)
2819 wcred = NOCRED;
2820 bp->b_flags |= (B_BUSY | B_WRITEINPROG);
2821 /*
2822 * A list of these buffers is kept so that the
2823 * second loop knows which buffers have actually
2824 * been committed. This is necessary, since there
2825 * may be a race between the commit rpc and new
2826 * uncommitted writes on the file.
2827 */
2828 bvec[bvecpos++] = bp;
2829 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2830 bp->b_dirtyoff;
2831 if (toff < off)
2832 off = toff;
2833 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2834 if (toff > endoff)
2835 endoff = toff;
2836 }
2837 splx(s);
2838 }
2839 if (bvecpos > 0) {
2840 /*
2841 * Commit data on the server, as required.
2842 * If all bufs are using the same wcred, then use that with
2843 * one call for all of them, otherwise commit each one
2844 * separately.
2845 */
2846 if (wcred != NOCRED)
2847 retv = nfs_commit(vp, off, (int)(endoff - off),
2848 wcred, p);
2849 else {
2850 retv = 0;
2851 for (i = 0; i < bvecpos; i++) {
2852 off_t off, size;
2853 bp = bvec[i];
2854 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2855 bp->b_dirtyoff;
2856 size = (u_quad_t)(bp->b_dirtyend
2857 - bp->b_dirtyoff);
2858 retv = nfs_commit(vp, off, (int)size,
2859 bp->b_wcred, p);
2860 if (retv) break;
2861 }
2862 }
2863
2864 if (retv == NFSERR_STALEWRITEVERF)
2865 nfs_clearcommit(vp->v_mount);
2866 /*
2867 * Now, either mark the blocks I/O done or mark the
2868 * blocks dirty, depending on whether the commit
2869 * succeeded.
2870 */
2871 for (i = 0; i < bvecpos; i++) {
2872 bp = bvec[i];
2873 bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG);
2874 if (retv)
2875 brelse(bp);
2876 else {
2877 vp->v_numoutput++;
2878 bp->b_flags |= B_ASYNC;
2879 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR|B_DELWRI);
2880 bp->b_dirtyoff = bp->b_dirtyend = 0;
2881 reassignbuf(bp, vp);
2882 biodone(bp);
2883 }
2884 }
2885 }
2886
2887 /*
2888 * Start/do any write(s) that are required.
2889 */
2890 loop:
2891 s = splbio();
2892 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
2893 nbp = bp->b_vnbufs.le_next;
2894 if (bp->b_flags & B_BUSY) {
2895 if (waitfor != MNT_WAIT || passone)
2896 continue;
2897 bp->b_flags |= B_WANTED;
2898 error = tsleep((caddr_t)bp, slpflag | (PRIBIO + 1),
2899 "nfsfsync", slptimeo);
2900 splx(s);
2901 if (error) {
2902 if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
2903 return (EINTR);
2904 if (slpflag == PCATCH) {
2905 slpflag = 0;
2906 slptimeo = 2 * hz;
2907 }
2908 }
2909 goto loop;
2910 }
2911 if ((bp->b_flags & B_DELWRI) == 0)
2912 panic("nfs_fsync: not dirty");
2913 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT))
2914 continue;
2915 bremfree(bp);
2916 if (passone || !commit)
2917 bp->b_flags |= (B_BUSY|B_ASYNC);
2918 else
2919 bp->b_flags |= (B_BUSY|B_ASYNC|B_WRITEINPROG|B_NEEDCOMMIT);
2920 splx(s);
2921 VOP_BWRITE(bp);
2922 goto loop;
2923 }
2924 splx(s);
2925 if (passone) {
2926 passone = 0;
2927 goto again;
2928 }
2929 if (waitfor == MNT_WAIT) {
2930 while (vp->v_numoutput) {
2931 vp->v_flag |= VBWAIT;
2932 error = tsleep((caddr_t)&vp->v_numoutput,
2933 slpflag | (PRIBIO + 1), "nfsfsync", slptimeo);
2934 if (error) {
2935 if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
2936 return (EINTR);
2937 if (slpflag == PCATCH) {
2938 slpflag = 0;
2939 slptimeo = 2 * hz;
2940 }
2941 }
2942 }
2943 if (vp->v_dirtyblkhd.lh_first && commit) {
2944 #if 0
2945 vprint("nfs_fsync: dirty", vp);
2946 #endif
2947 goto loop;
2948 }
2949 }
2950 if (np->n_flag & NWRITEERR) {
2951 error = np->n_error;
2952 np->n_flag &= ~NWRITEERR;
2953 }
2954 return (error);
2955 }
2956
2957 /*
2958 * Return POSIX pathconf information applicable to nfs.
2959 *
2960 * The NFS V2 protocol doesn't support this, so just return EINVAL
2961 * for V2.
2962 */
2963 /* ARGSUSED */
2964 int
2965 nfs_pathconf(v)
2966 void *v;
2967 {
2968 #if 0
2969 struct vop_pathconf_args /* {
2970 struct vnode *a_vp;
2971 int a_name;
2972 register_t *a_retval;
2973 } */ *ap = v;
2974 #endif
2975
2976 return (EINVAL);
2977 }
2978
2979 /*
2980 * NFS advisory byte-level locks.
2981 */
2982 int
2983 nfs_advlock(v)
2984 void *v;
2985 {
2986 struct vop_advlock_args /* {
2987 struct vnode *a_vp;
2988 caddr_t a_id;
2989 int a_op;
2990 struct flock *a_fl;
2991 int a_flags;
2992 } */ *ap = v;
2993 register struct nfsnode *np = VTONFS(ap->a_vp);
2994
2995 return (lf_advlock(&np->n_lockf, np->n_size, ap->a_id, ap->a_op,
2996 ap->a_fl, ap->a_flags));
2997 }
2998
2999 /*
3000 * Print out the contents of an nfsnode.
3001 */
3002 int
3003 nfs_print(v)
3004 void *v;
3005 {
3006 struct vop_print_args /* {
3007 struct vnode *a_vp;
3008 } */ *ap = v;
3009 register struct vnode *vp = ap->a_vp;
3010 register struct nfsnode *np = VTONFS(vp);
3011
3012 printf("tag VT_NFS, fileid %ld fsid 0x%lx",
3013 np->n_vattr->va_fileid, np->n_vattr->va_fsid);
3014 #ifdef FIFO
3015 if (vp->v_type == VFIFO)
3016 fifo_printinfo(vp);
3017 #endif
3018 printf("\n");
3019 return (0);
3020 }
3021
3022 /*
3023 * NFS file truncation.
3024 */
3025 int
3026 nfs_truncate(v)
3027 void *v;
3028 {
3029 #if 0
3030 struct vop_truncate_args /* {
3031 struct vnode *a_vp;
3032 off_t a_length;
3033 int a_flags;
3034 struct ucred *a_cred;
3035 struct proc *a_p;
3036 } */ *ap = v;
3037 #endif
3038
3039 /* Use nfs_setattr */
3040 return (EOPNOTSUPP);
3041 }
3042
3043 /*
3044 * NFS update.
3045 */
3046 int
3047 nfs_update(v)
3048 void *v;
3049 #if 0
3050 struct vop_update_args /* {
3051 struct vnode *a_vp;
3052 struct timespec *a_ta;
3053 struct timespec *a_tm;
3054 int a_waitfor;
3055 } */ *ap = v;
3056 #endif
3057 {
3058
3059 /* Use nfs_setattr */
3060 return (EOPNOTSUPP);
3061 }
3062
3063 /*
3064 * Just call nfs_writebp() with the force argument set to 1.
3065 */
3066 int
3067 nfs_bwrite(v)
3068 void *v;
3069 {
3070 struct vop_bwrite_args /* {
3071 struct vnode *a_bp;
3072 } */ *ap = v;
3073
3074 return (nfs_writebp(ap->a_bp, 1));
3075 }
3076
3077 /*
3078 * This is a clone of vn_bwrite(), except that B_WRITEINPROG isn't set unless
3079 * the force flag is one and it also handles the B_NEEDCOMMIT flag.
3080 */
3081 int
3082 nfs_writebp(bp, force)
3083 register struct buf *bp;
3084 int force;
3085 {
3086 register int oldflags = bp->b_flags, retv = 1;
3087 register struct proc *p = curproc; /* XXX */
3088 off_t off;
3089
3090 if(!(bp->b_flags & B_BUSY))
3091 panic("bwrite: buffer is not busy???");
3092
3093 #ifdef fvdl_debug
3094 printf("nfs_writebp(%x): vp %x voff %d vend %d doff %d dend %d\n",
3095 bp, bp->b_vp, bp->b_validoff, bp->b_validend, bp->b_dirtyoff,
3096 bp->b_dirtyend);
3097 #endif
3098 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR|B_DELWRI|B_AGE);
3099
3100 if (oldflags & B_ASYNC) {
3101 if (oldflags & B_DELWRI) {
3102 reassignbuf(bp, bp->b_vp);
3103 } else if (p) {
3104 ++p->p_stats->p_ru.ru_oublock;
3105 }
3106 }
3107 bp->b_vp->v_numoutput++;
3108
3109 /*
3110 * If B_NEEDCOMMIT is set, a commit rpc may do the trick. If not
3111 * an actual write will have to be scheduled via. VOP_STRATEGY().
3112 * If B_WRITEINPROG is already set, then push it with a write anyhow.
3113 */
3114 if ((oldflags & (B_NEEDCOMMIT | B_WRITEINPROG)) == B_NEEDCOMMIT) {
3115 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
3116 bp->b_flags |= B_WRITEINPROG;
3117 retv = nfs_commit(bp->b_vp, off, bp->b_dirtyend-bp->b_dirtyoff,
3118 bp->b_wcred, bp->b_proc);
3119 bp->b_flags &= ~B_WRITEINPROG;
3120 if (!retv) {
3121 bp->b_dirtyoff = bp->b_dirtyend = 0;
3122 bp->b_flags &= ~B_NEEDCOMMIT;
3123 biodone(bp);
3124 } else if (retv == NFSERR_STALEWRITEVERF)
3125 nfs_clearcommit(bp->b_vp->v_mount);
3126 }
3127 if (retv) {
3128 if (force)
3129 bp->b_flags |= B_WRITEINPROG;
3130 VOP_STRATEGY(bp);
3131 }
3132
3133 if( (oldflags & B_ASYNC) == 0) {
3134 int rtval = biowait(bp);
3135 if (oldflags & B_DELWRI) {
3136 reassignbuf(bp, bp->b_vp);
3137 } else if (p) {
3138 ++p->p_stats->p_ru.ru_oublock;
3139 }
3140 brelse(bp);
3141 return (rtval);
3142 }
3143
3144 return (0);
3145 }
3146
3147 /*
3148 * nfs special file access vnode op.
3149 * Essentially just get vattr and then imitate iaccess() since the device is
3150 * local to the client.
3151 */
3152 int
3153 nfsspec_access(v)
3154 void *v;
3155 {
3156 struct vop_access_args /* {
3157 struct vnode *a_vp;
3158 int a_mode;
3159 struct ucred *a_cred;
3160 struct proc *a_p;
3161 } */ *ap = v;
3162 struct vattr va;
3163 struct vnode *vp = ap->a_vp;
3164 int error;
3165
3166 error = VOP_GETATTR(vp, &va, ap->a_cred, ap->a_p);
3167 if (error)
3168 return (error);
3169
3170 /*
3171 * Disallow write attempts on filesystems mounted read-only;
3172 * unless the file is a socket, fifo, or a block or character
3173 * device resident on the filesystem.
3174 */
3175 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3176 switch (vp->v_type) {
3177 case VREG:
3178 case VDIR:
3179 case VLNK:
3180 return (EROFS);
3181 default:
3182 break;
3183 }
3184 }
3185
3186 return (vaccess(va.va_type, va.va_mode,
3187 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
3188 }
3189
3190 /*
3191 * Read wrapper for special devices.
3192 */
3193 int
3194 nfsspec_read(v)
3195 void *v;
3196 {
3197 struct vop_read_args /* {
3198 struct vnode *a_vp;
3199 struct uio *a_uio;
3200 int a_ioflag;
3201 struct ucred *a_cred;
3202 } */ *ap = v;
3203 register struct nfsnode *np = VTONFS(ap->a_vp);
3204
3205 /*
3206 * Set access flag.
3207 */
3208 np->n_flag |= NACC;
3209 np->n_atim.tv_sec = time.tv_sec;
3210 np->n_atim.tv_nsec = time.tv_usec * 1000;
3211 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3212 }
3213
3214 /*
3215 * Write wrapper for special devices.
3216 */
3217 int
3218 nfsspec_write(v)
3219 void *v;
3220 {
3221 struct vop_write_args /* {
3222 struct vnode *a_vp;
3223 struct uio *a_uio;
3224 int a_ioflag;
3225 struct ucred *a_cred;
3226 } */ *ap = v;
3227 register struct nfsnode *np = VTONFS(ap->a_vp);
3228
3229 /*
3230 * Set update flag.
3231 */
3232 np->n_flag |= NUPD;
3233 np->n_mtim.tv_sec = time.tv_sec;
3234 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3235 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3236 }
3237
3238 /*
3239 * Close wrapper for special devices.
3240 *
3241 * Update the times on the nfsnode then do device close.
3242 */
3243 int
3244 nfsspec_close(v)
3245 void *v;
3246 {
3247 struct vop_close_args /* {
3248 struct vnode *a_vp;
3249 int a_fflag;
3250 struct ucred *a_cred;
3251 struct proc *a_p;
3252 } */ *ap = v;
3253 register struct vnode *vp = ap->a_vp;
3254 register struct nfsnode *np = VTONFS(vp);
3255 struct vattr vattr;
3256
3257 if (np->n_flag & (NACC | NUPD)) {
3258 np->n_flag |= NCHG;
3259 if (vp->v_usecount == 1 &&
3260 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3261 VATTR_NULL(&vattr);
3262 if (np->n_flag & NACC)
3263 vattr.va_atime = np->n_atim;
3264 if (np->n_flag & NUPD)
3265 vattr.va_mtime = np->n_mtim;
3266 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
3267 }
3268 }
3269 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3270 }
3271
3272 #ifdef FIFO
3273 /*
3274 * Read wrapper for fifos.
3275 */
3276 int
3277 nfsfifo_read(v)
3278 void *v;
3279 {
3280 struct vop_read_args /* {
3281 struct vnode *a_vp;
3282 struct uio *a_uio;
3283 int a_ioflag;
3284 struct ucred *a_cred;
3285 } */ *ap = v;
3286 extern int (**fifo_vnodeop_p) __P((void *));
3287 register struct nfsnode *np = VTONFS(ap->a_vp);
3288
3289 /*
3290 * Set access flag.
3291 */
3292 np->n_flag |= NACC;
3293 np->n_atim.tv_sec = time.tv_sec;
3294 np->n_atim.tv_nsec = time.tv_usec * 1000;
3295 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3296 }
3297
3298 /*
3299 * Write wrapper for fifos.
3300 */
3301 int
3302 nfsfifo_write(v)
3303 void *v;
3304 {
3305 struct vop_write_args /* {
3306 struct vnode *a_vp;
3307 struct uio *a_uio;
3308 int a_ioflag;
3309 struct ucred *a_cred;
3310 } */ *ap = v;
3311 extern int (**fifo_vnodeop_p) __P((void *));
3312 register struct nfsnode *np = VTONFS(ap->a_vp);
3313
3314 /*
3315 * Set update flag.
3316 */
3317 np->n_flag |= NUPD;
3318 np->n_mtim.tv_sec = time.tv_sec;
3319 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3320 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3321 }
3322
3323 /*
3324 * Close wrapper for fifos.
3325 *
3326 * Update the times on the nfsnode then do fifo close.
3327 */
3328 int
3329 nfsfifo_close(v)
3330 void *v;
3331 {
3332 struct vop_close_args /* {
3333 struct vnode *a_vp;
3334 int a_fflag;
3335 struct ucred *a_cred;
3336 struct proc *a_p;
3337 } */ *ap = v;
3338 register struct vnode *vp = ap->a_vp;
3339 register struct nfsnode *np = VTONFS(vp);
3340 struct vattr vattr;
3341 extern int (**fifo_vnodeop_p) __P((void *));
3342
3343 if (np->n_flag & (NACC | NUPD)) {
3344 if (np->n_flag & NACC) {
3345 np->n_atim.tv_sec = time.tv_sec;
3346 np->n_atim.tv_nsec = time.tv_usec * 1000;
3347 }
3348 if (np->n_flag & NUPD) {
3349 np->n_mtim.tv_sec = time.tv_sec;
3350 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3351 }
3352 np->n_flag |= NCHG;
3353 if (vp->v_usecount == 1 &&
3354 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3355 VATTR_NULL(&vattr);
3356 if (np->n_flag & NACC)
3357 vattr.va_atime = np->n_atim;
3358 if (np->n_flag & NUPD)
3359 vattr.va_mtime = np->n_mtim;
3360 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
3361 }
3362 }
3363 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
3364 }
3365 #endif /* ! FIFO */
3366