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