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