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