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