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