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