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