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