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