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