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