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