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