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