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