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