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