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