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