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