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