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