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