nfs_vnops.c revision 1.100.4.3 1 /* $NetBSD: nfs_vnops.c,v 1.100.4.3 1999/07/04 01:46:53 chs Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)nfs_vnops.c 8.19 (Berkeley) 7/31/95
39 */
40
41 /*
42 * vnode op calls for Sun NFS version 2 and 3
43 */
44
45 #include <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_balloc_desc, nfs_balloc }, /* balloc */
135 { &vop_reallocblks_desc, nfs_reallocblks }, /* reallocblks */
136 { &vop_vfree_desc, nfs_vfree }, /* vfree */
137 { &vop_truncate_desc, nfs_truncate }, /* truncate */
138 { &vop_update_desc, nfs_update }, /* update */
139 { &vop_bwrite_desc, nfs_bwrite }, /* bwrite */
140 { &vop_getpages_desc, nfs_getpages }, /* getpages */
141 { &vop_putpages_desc, nfs_putpages }, /* putpages */
142 { NULL, NULL }
143 };
144 struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
145 { &nfsv2_vnodeop_p, nfsv2_vnodeop_entries };
146
147 /*
148 * Special device vnode ops
149 */
150 int (**spec_nfsv2nodeop_p) __P((void *));
151 struct vnodeopv_entry_desc spec_nfsv2nodeop_entries[] = {
152 { &vop_default_desc, vn_default_error },
153 { &vop_lookup_desc, spec_lookup }, /* lookup */
154 { &vop_create_desc, spec_create }, /* create */
155 { &vop_mknod_desc, spec_mknod }, /* mknod */
156 { &vop_open_desc, spec_open }, /* open */
157 { &vop_close_desc, nfsspec_close }, /* close */
158 { &vop_access_desc, nfsspec_access }, /* access */
159 { &vop_getattr_desc, nfs_getattr }, /* getattr */
160 { &vop_setattr_desc, nfs_setattr }, /* setattr */
161 { &vop_read_desc, nfsspec_read }, /* read */
162 { &vop_write_desc, nfsspec_write }, /* write */
163 { &vop_lease_desc, spec_lease_check }, /* lease */
164 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
165 { &vop_poll_desc, spec_poll }, /* poll */
166 { &vop_revoke_desc, spec_revoke }, /* revoke */
167 { &vop_mmap_desc, spec_mmap }, /* mmap */
168 { &vop_fsync_desc, nfs_fsync }, /* fsync */
169 { &vop_seek_desc, spec_seek }, /* seek */
170 { &vop_remove_desc, spec_remove }, /* remove */
171 { &vop_link_desc, spec_link }, /* link */
172 { &vop_rename_desc, spec_rename }, /* rename */
173 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
174 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
175 { &vop_symlink_desc, spec_symlink }, /* symlink */
176 { &vop_readdir_desc, spec_readdir }, /* readdir */
177 { &vop_readlink_desc, spec_readlink }, /* readlink */
178 { &vop_abortop_desc, spec_abortop }, /* abortop */
179 { &vop_inactive_desc, nfs_inactive }, /* inactive */
180 { &vop_reclaim_desc, nfs_reclaim }, /* reclaim */
181 { &vop_lock_desc, nfs_lock }, /* lock */
182 { &vop_unlock_desc, nfs_unlock }, /* unlock */
183 { &vop_bmap_desc, spec_bmap }, /* bmap */
184 { &vop_strategy_desc, spec_strategy }, /* strategy */
185 { &vop_print_desc, nfs_print }, /* print */
186 { &vop_islocked_desc, nfs_islocked }, /* islocked */
187 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
188 { &vop_advlock_desc, spec_advlock }, /* advlock */
189 { &vop_blkatoff_desc, spec_blkatoff }, /* blkatoff */
190 { &vop_valloc_desc, spec_valloc }, /* valloc */
191 { &vop_reallocblks_desc, spec_reallocblks }, /* reallocblks */
192 { &vop_vfree_desc, spec_vfree }, /* vfree */
193 { &vop_truncate_desc, spec_truncate }, /* truncate */
194 { &vop_update_desc, nfs_update }, /* update */
195 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
196 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
197 };
198 struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
199 { &spec_nfsv2nodeop_p, spec_nfsv2nodeop_entries };
200
201 int (**fifo_nfsv2nodeop_p) __P((void *));
202 struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries[] = {
203 { &vop_default_desc, vn_default_error },
204 { &vop_lookup_desc, fifo_lookup }, /* lookup */
205 { &vop_create_desc, fifo_create }, /* create */
206 { &vop_mknod_desc, fifo_mknod }, /* mknod */
207 { &vop_open_desc, fifo_open }, /* open */
208 { &vop_close_desc, nfsfifo_close }, /* close */
209 { &vop_access_desc, nfsspec_access }, /* access */
210 { &vop_getattr_desc, nfs_getattr }, /* getattr */
211 { &vop_setattr_desc, nfs_setattr }, /* setattr */
212 { &vop_read_desc, nfsfifo_read }, /* read */
213 { &vop_write_desc, nfsfifo_write }, /* write */
214 { &vop_lease_desc, fifo_lease_check }, /* lease */
215 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
216 { &vop_poll_desc, fifo_poll }, /* poll */
217 { &vop_revoke_desc, fifo_revoke }, /* revoke */
218 { &vop_mmap_desc, fifo_mmap }, /* mmap */
219 { &vop_fsync_desc, nfs_fsync }, /* fsync */
220 { &vop_seek_desc, fifo_seek }, /* seek */
221 { &vop_remove_desc, fifo_remove }, /* remove */
222 { &vop_link_desc, fifo_link }, /* link */
223 { &vop_rename_desc, fifo_rename }, /* rename */
224 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
225 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
226 { &vop_symlink_desc, fifo_symlink }, /* symlink */
227 { &vop_readdir_desc, fifo_readdir }, /* readdir */
228 { &vop_readlink_desc, fifo_readlink }, /* readlink */
229 { &vop_abortop_desc, fifo_abortop }, /* abortop */
230 { &vop_inactive_desc, nfs_inactive }, /* inactive */
231 { &vop_reclaim_desc, nfs_reclaim }, /* reclaim */
232 { &vop_lock_desc, nfs_lock }, /* lock */
233 { &vop_unlock_desc, nfs_unlock }, /* unlock */
234 { &vop_bmap_desc, fifo_bmap }, /* bmap */
235 { &vop_strategy_desc, genfs_badop }, /* strategy */
236 { &vop_print_desc, nfs_print }, /* print */
237 { &vop_islocked_desc, nfs_islocked }, /* islocked */
238 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
239 { &vop_advlock_desc, fifo_advlock }, /* advlock */
240 { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */
241 { &vop_valloc_desc, fifo_valloc }, /* valloc */
242 { &vop_reallocblks_desc, fifo_reallocblks }, /* reallocblks */
243 { &vop_vfree_desc, fifo_vfree }, /* vfree */
244 { &vop_truncate_desc, fifo_truncate }, /* truncate */
245 { &vop_update_desc, nfs_update }, /* update */
246 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
247 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
248 };
249 struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
250 { &fifo_nfsv2nodeop_p, fifo_nfsv2nodeop_entries };
251
252 /*
253 * Global variables
254 */
255 extern u_int32_t nfs_true, nfs_false;
256 extern u_int32_t nfs_xdrneg1;
257 extern struct nfsstats nfsstats;
258 extern nfstype nfsv3_type[9];
259 struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
260 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
261 int nfs_numasync = 0;
262 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1))
263
264 /*
265 * nfs null call from vfs.
266 */
267 int
268 nfs_null(vp, cred, procp)
269 struct vnode *vp;
270 struct ucred *cred;
271 struct proc *procp;
272 {
273 caddr_t bpos, dpos;
274 int error = 0;
275 struct mbuf *mreq, *mrep, *md, *mb;
276
277 nfsm_reqhead(vp, NFSPROC_NULL, 0);
278 nfsm_request(vp, NFSPROC_NULL, procp, cred);
279 nfsm_reqdone;
280 return (error);
281 }
282
283 /*
284 * nfs access vnode op.
285 * For nfs version 2, just return ok. File accesses may fail later.
286 * For nfs version 3, use the access rpc to check accessibility. If file modes
287 * are changed on the server, accesses might still fail later.
288 */
289 int
290 nfs_access(v)
291 void *v;
292 {
293 struct vop_access_args /* {
294 struct vnode *a_vp;
295 int a_mode;
296 struct ucred *a_cred;
297 struct proc *a_p;
298 } */ *ap = v;
299 register struct vnode *vp = ap->a_vp;
300 register u_int32_t *tl;
301 register caddr_t cp;
302 register int32_t t1, t2;
303 caddr_t bpos, dpos, cp2;
304 int error = 0, attrflag;
305 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
306 u_int32_t mode, rmode;
307 int v3 = NFS_ISV3(vp);
308
309 /*
310 * For nfs v3, do an access rpc, otherwise you are stuck emulating
311 * ufs_access() locally using the vattr. This may not be correct,
312 * since the server may apply other access criteria such as
313 * client uid-->server uid mapping that we do not know about, but
314 * this is better than just returning anything that is lying about
315 * in the cache.
316 */
317 if (v3) {
318 nfsstats.rpccnt[NFSPROC_ACCESS]++;
319 nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
320 nfsm_fhtom(vp, v3);
321 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
322 if (ap->a_mode & VREAD)
323 mode = NFSV3ACCESS_READ;
324 else
325 mode = 0;
326 if (vp->v_type != VDIR) {
327 if (ap->a_mode & VWRITE)
328 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
329 if (ap->a_mode & VEXEC)
330 mode |= NFSV3ACCESS_EXECUTE;
331 } else {
332 if (ap->a_mode & VWRITE)
333 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
334 NFSV3ACCESS_DELETE);
335 if (ap->a_mode & VEXEC)
336 mode |= NFSV3ACCESS_LOOKUP;
337 }
338 *tl = txdr_unsigned(mode);
339 nfsm_request(vp, NFSPROC_ACCESS, ap->a_p, ap->a_cred);
340 nfsm_postop_attr(vp, attrflag);
341 if (!error) {
342 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
343 rmode = fxdr_unsigned(u_int32_t, *tl);
344 /*
345 * The NFS V3 spec does not clarify whether or not
346 * the returned access bits can be a superset of
347 * the ones requested, so...
348 */
349 if ((rmode & mode) != mode)
350 error = EACCES;
351 }
352 nfsm_reqdone;
353 if (error)
354 return (error);
355 } else
356 return (nfsspec_access(ap));
357 /*
358 * Disallow write attempts on filesystems mounted read-only;
359 * unless the file is a socket, fifo, or a block or character
360 * device resident on the filesystem.
361 */
362 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
363 switch (vp->v_type) {
364 case VREG:
365 case VDIR:
366 case VLNK:
367 return (EROFS);
368 default:
369 break;
370 }
371 }
372 return (0);
373 }
374
375 /*
376 * nfs open vnode op
377 * Check to see if the type is ok
378 * and that deletion is not in progress.
379 * For paged in text files, you will need to flush the page cache
380 * if consistency is lost.
381 */
382 /* ARGSUSED */
383 int
384 nfs_open(v)
385 void *v;
386 {
387 struct vop_open_args /* {
388 struct vnode *a_vp;
389 int a_mode;
390 struct ucred *a_cred;
391 struct proc *a_p;
392 } */ *ap = v;
393 register struct vnode *vp = ap->a_vp;
394 struct nfsnode *np = VTONFS(vp);
395 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
396 struct vattr vattr;
397 int error;
398
399 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
400 #ifdef DIAGNOSTIC
401 printf("open eacces vtyp=%d\n",vp->v_type);
402 #endif
403 return (EACCES);
404 }
405 /*
406 * Get a valid lease. If cached data is stale, flush it.
407 */
408 if (nmp->nm_flag & NFSMNT_NQNFS) {
409 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
410 do {
411 error = nqnfs_getlease(vp, ND_READ, ap->a_cred,
412 ap->a_p);
413 } while (error == NQNFS_EXPIRED);
414 if (error)
415 return (error);
416 if (np->n_lrev != np->n_brev ||
417 (np->n_flag & NQNFSNONCACHE)) {
418 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
419 ap->a_p, 1)) == EINTR)
420 return (error);
421 (void) uvm_vnp_uncache(vp);
422 np->n_brev = np->n_lrev;
423 }
424 }
425 } else {
426 if (np->n_flag & NMODIFIED) {
427 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
428 ap->a_p, 1)) == EINTR)
429 return (error);
430 (void) uvm_vnp_uncache(vp);
431 np->n_attrstamp = 0;
432 if (vp->v_type == VDIR) {
433 nfs_invaldircache(vp, 0);
434 np->n_direofoffset = 0;
435 }
436 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_p);
437 if (error)
438 return (error);
439 np->n_mtime = vattr.va_mtime.tv_sec;
440 } else {
441 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_p);
442 if (error)
443 return (error);
444 if (np->n_mtime != vattr.va_mtime.tv_sec) {
445 if (vp->v_type == VDIR) {
446 nfs_invaldircache(vp, 0);
447 np->n_direofoffset = 0;
448 }
449 if ((error = nfs_vinvalbuf(vp, V_SAVE,
450 ap->a_cred, ap->a_p, 1)) == EINTR)
451 return (error);
452 (void) uvm_vnp_uncache(vp);
453 np->n_mtime = vattr.va_mtime.tv_sec;
454 }
455 }
456 }
457 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0)
458 np->n_attrstamp = 0; /* For Open/Close consistency */
459 return (0);
460 }
461
462 /*
463 * nfs close vnode op
464 * What an NFS client should do upon close after writing is a debatable issue.
465 * Most NFS clients push delayed writes to the server upon close, basically for
466 * two reasons:
467 * 1 - So that any write errors may be reported back to the client process
468 * doing the close system call. By far the two most likely errors are
469 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
470 * 2 - To put a worst case upper bound on cache inconsistency between
471 * multiple clients for the file.
472 * There is also a consistency problem for Version 2 of the protocol w.r.t.
473 * not being able to tell if other clients are writing a file concurrently,
474 * since there is no way of knowing if the changed modify time in the reply
475 * is only due to the write for this client.
476 * (NFS Version 3 provides weak cache consistency data in the reply that
477 * should be sufficient to detect and handle this case.)
478 *
479 * The current code does the following:
480 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
481 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
482 * or commit them (this satisfies 1 and 2 except for the
483 * case where the server crashes after this close but
484 * before the commit RPC, which is felt to be "good
485 * enough". Changing the last argument to nfs_flush() to
486 * a 1 would force a commit operation, if it is felt a
487 * commit is necessary now.
488 * for NQNFS - do nothing now, since 2 is dealt with via leases and
489 * 1 should be dealt with via an fsync() system call for
490 * cases where write errors are important.
491 */
492 /* ARGSUSED */
493 int
494 nfs_close(v)
495 void *v;
496 {
497 struct vop_close_args /* {
498 struct vnodeop_desc *a_desc;
499 struct vnode *a_vp;
500 int a_fflag;
501 struct ucred *a_cred;
502 struct proc *a_p;
503 } */ *ap = v;
504 register struct vnode *vp = ap->a_vp;
505 register struct nfsnode *np = VTONFS(vp);
506 int error = 0;
507
508 if (vp->v_type == VREG) {
509 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 &&
510 (np->n_flag & NMODIFIED)) {
511 if (NFS_ISV3(vp)) {
512 error = nfs_flush(vp, ap->a_cred, MNT_WAIT, ap->a_p, 0);
513 np->n_flag &= ~NMODIFIED;
514 } else
515 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 1);
516 np->n_attrstamp = 0;
517 }
518 if (np->n_flag & NWRITEERR) {
519 np->n_flag &= ~NWRITEERR;
520 error = np->n_error;
521 }
522 }
523 return (error);
524 }
525
526 /*
527 * nfs getattr call from vfs.
528 */
529 int
530 nfs_getattr(v)
531 void *v;
532 {
533 struct vop_getattr_args /* {
534 struct vnode *a_vp;
535 struct vattr *a_vap;
536 struct ucred *a_cred;
537 struct proc *a_p;
538 } */ *ap = v;
539 register struct vnode *vp = ap->a_vp;
540 register struct nfsnode *np = VTONFS(vp);
541 register caddr_t cp;
542 register u_int32_t *tl;
543 register int32_t t1, t2;
544 caddr_t bpos, dpos;
545 int error = 0;
546 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
547 int v3 = NFS_ISV3(vp);
548
549 /*
550 * Update local times for special files.
551 */
552 if (np->n_flag & (NACC | NUPD))
553 np->n_flag |= NCHG;
554 /*
555 * First look in the cache.
556 */
557 if (nfs_getattrcache(vp, ap->a_vap) == 0)
558 return (0);
559 nfsstats.rpccnt[NFSPROC_GETATTR]++;
560 nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
561 nfsm_fhtom(vp, v3);
562 nfsm_request(vp, NFSPROC_GETATTR, ap->a_p, ap->a_cred);
563 if (!error) {
564 nfsm_loadattr(vp, ap->a_vap);
565 if (vp->v_type == VDIR &&
566 ap->a_vap->va_blocksize < NFS_DIRFRAGSIZ)
567 ap->a_vap->va_blocksize = NFS_DIRFRAGSIZ;
568 }
569 nfsm_reqdone;
570 return (error);
571 }
572
573 /*
574 * nfs setattr call.
575 */
576 int
577 nfs_setattr(v)
578 void *v;
579 {
580 struct vop_setattr_args /* {
581 struct vnodeop_desc *a_desc;
582 struct vnode *a_vp;
583 struct vattr *a_vap;
584 struct ucred *a_cred;
585 struct proc *a_p;
586 } */ *ap = v;
587 register struct vnode *vp = ap->a_vp;
588 register struct nfsnode *np = VTONFS(vp);
589 register struct vattr *vap = ap->a_vap;
590 int error = 0;
591 u_quad_t tsize = 0;
592
593 /*
594 * Setting of flags is not supported.
595 */
596 if (vap->va_flags != VNOVAL)
597 return (EOPNOTSUPP);
598
599 /*
600 * Disallow write attempts if the filesystem is mounted read-only.
601 */
602 if ((vap->va_uid != (uid_t)VNOVAL ||
603 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
604 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
605 (vp->v_mount->mnt_flag & MNT_RDONLY))
606 return (EROFS);
607 if (vap->va_size != VNOVAL) {
608 switch (vp->v_type) {
609 case VDIR:
610 return (EISDIR);
611 case VCHR:
612 case VBLK:
613 case VSOCK:
614 case VFIFO:
615 if (vap->va_mtime.tv_sec == VNOVAL &&
616 vap->va_atime.tv_sec == VNOVAL &&
617 vap->va_mode == (mode_t)VNOVAL &&
618 vap->va_uid == (uid_t)VNOVAL &&
619 vap->va_gid == (gid_t)VNOVAL)
620 return (0);
621 vap->va_size = VNOVAL;
622 break;
623 default:
624 /*
625 * Disallow write attempts if the filesystem is
626 * mounted read-only.
627 */
628 if (vp->v_mount->mnt_flag & MNT_RDONLY)
629 return (EROFS);
630 uvm_vnp_setsize(vp, vap->va_size);
631 if (vap->va_size == 0)
632 error = nfs_vinvalbuf(vp, 0,
633 ap->a_cred, ap->a_p, 1);
634 else
635 error = nfs_vinvalbuf(vp, V_SAVE,
636 ap->a_cred, ap->a_p, 1);
637 if (error) {
638 uvm_vnp_setsize(vp, np->n_size);
639 return (error);
640 }
641 tsize = np->n_size;
642 np->n_size = np->n_vattr->va_size = vap->va_size;
643 }
644 } else if ((vap->va_mtime.tv_sec != VNOVAL ||
645 vap->va_atime.tv_sec != VNOVAL) &&
646 vp->v_type == VREG &&
647 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred,
648 ap->a_p, 1)) == EINTR)
649 return (error);
650 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_p);
651 if (error && vap->va_size != VNOVAL) {
652 np->n_size = np->n_vattr->va_size = tsize;
653 uvm_vnp_setsize(vp, np->n_size);
654 }
655 return (error);
656 }
657
658 /*
659 * Do an nfs setattr rpc.
660 */
661 int
662 nfs_setattrrpc(vp, vap, cred, procp)
663 register struct vnode *vp;
664 register struct vattr *vap;
665 struct ucred *cred;
666 struct proc *procp;
667 {
668 register struct nfsv2_sattr *sp;
669 register caddr_t cp;
670 register int32_t t1, t2;
671 caddr_t bpos, dpos, cp2;
672 u_int32_t *tl;
673 int error = 0, wccflag = NFSV3_WCCRATTR;
674 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
675 int v3 = NFS_ISV3(vp);
676
677 nfsstats.rpccnt[NFSPROC_SETATTR]++;
678 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
679 nfsm_fhtom(vp, v3);
680 if (v3) {
681 nfsm_v3attrbuild(vap, TRUE);
682 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
683 *tl = nfs_false;
684 } else {
685 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
686 if (vap->va_mode == (mode_t)VNOVAL)
687 sp->sa_mode = nfs_xdrneg1;
688 else
689 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
690 if (vap->va_uid == (uid_t)VNOVAL)
691 sp->sa_uid = nfs_xdrneg1;
692 else
693 sp->sa_uid = txdr_unsigned(vap->va_uid);
694 if (vap->va_gid == (gid_t)VNOVAL)
695 sp->sa_gid = nfs_xdrneg1;
696 else
697 sp->sa_gid = txdr_unsigned(vap->va_gid);
698 sp->sa_size = txdr_unsigned(vap->va_size);
699 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
700 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
701 }
702 nfsm_request(vp, NFSPROC_SETATTR, procp, cred);
703 if (v3) {
704 nfsm_wcc_data(vp, wccflag);
705 } else
706 nfsm_loadattr(vp, (struct vattr *)0);
707 nfsm_reqdone;
708 return (error);
709 }
710
711 /*
712 * nfs lookup call, one step at a time...
713 * First look in cache
714 * If not found, unlock the directory nfsnode and do the rpc
715 */
716 int
717 nfs_lookup(v)
718 void *v;
719 {
720 struct vop_lookup_args /* {
721 struct vnodeop_desc *a_desc;
722 struct vnode *a_dvp;
723 struct vnode **a_vpp;
724 struct componentname *a_cnp;
725 } */ *ap = v;
726 struct componentname *cnp = ap->a_cnp;
727 struct vnode *dvp = ap->a_dvp;
728 struct vnode **vpp = ap->a_vpp;
729 int flags = cnp->cn_flags;
730 struct vnode *newvp;
731 u_int32_t *tl;
732 caddr_t cp;
733 int32_t t1, t2;
734 struct nfsmount *nmp;
735 caddr_t bpos, dpos, cp2;
736 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
737 long len;
738 nfsfh_t *fhp;
739 struct nfsnode *np;
740 int lockparent, wantparent, error = 0, attrflag, fhsize;
741 int v3 = NFS_ISV3(dvp);
742
743 *vpp = NULLVP;
744 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
745 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
746 return (EROFS);
747 if (dvp->v_type != VDIR)
748 return (ENOTDIR);
749 lockparent = flags & LOCKPARENT;
750 wantparent = flags & (LOCKPARENT|WANTPARENT);
751 nmp = VFSTONFS(dvp->v_mount);
752 np = VTONFS(dvp);
753 if ((error = cache_lookup(dvp, vpp, cnp)) != 0) {
754 struct vattr vattr;
755 int vpid;
756
757 if (error == ENOENT) {
758 if (!VOP_GETATTR(dvp, &vattr, cnp->cn_cred,
759 cnp->cn_proc) && vattr.va_mtime.tv_sec ==
760 VTONFS(dvp)->n_nctime)
761 return (ENOENT);
762 cache_purge(dvp);
763 np->n_nctime = 0;
764 goto dorpc;
765 }
766
767 newvp = *vpp;
768 vpid = newvp->v_id;
769 /*
770 * See the comment starting `Step through' in ufs/ufs_lookup.c
771 * for an explanation of the locking protocol
772 */
773 if (dvp == newvp) {
774 VREF(newvp);
775 error = 0;
776 } else
777 error = vget(newvp, LK_EXCLUSIVE);
778 if (!error) {
779 if (vpid == newvp->v_id) {
780 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, cnp->cn_proc)
781 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
782 nfsstats.lookupcache_hits++;
783 if (cnp->cn_nameiop != LOOKUP &&
784 (flags & ISLASTCN))
785 cnp->cn_flags |= SAVENAME;
786 return (0);
787 }
788 cache_purge(newvp);
789 }
790 vrele(newvp);
791 }
792 *vpp = NULLVP;
793 }
794 dorpc:
795 error = 0;
796 newvp = NULLVP;
797 nfsstats.lookupcache_misses++;
798 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
799 len = cnp->cn_namelen;
800 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
801 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
802 nfsm_fhtom(dvp, v3);
803 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
804 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_proc, cnp->cn_cred);
805 if (error) {
806 nfsm_postop_attr(dvp, attrflag);
807 m_freem(mrep);
808 goto nfsmout;
809 }
810 nfsm_getfh(fhp, fhsize, v3);
811
812 /*
813 * Handle RENAME case...
814 */
815 if (cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
816 if (NFS_CMPFH(np, fhp, fhsize)) {
817 m_freem(mrep);
818 return (EISDIR);
819 }
820 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
821 if (error) {
822 m_freem(mrep);
823 return (error);
824 }
825 newvp = NFSTOV(np);
826 if (v3) {
827 nfsm_postop_attr(newvp, attrflag);
828 nfsm_postop_attr(dvp, attrflag);
829 } else
830 nfsm_loadattr(newvp, (struct vattr *)0);
831 *vpp = newvp;
832 m_freem(mrep);
833 cnp->cn_flags |= SAVENAME;
834 return (0);
835 }
836
837 if (NFS_CMPFH(np, fhp, fhsize)) {
838 VREF(dvp);
839 newvp = dvp;
840 } else {
841 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
842 if (error) {
843 m_freem(mrep);
844 return (error);
845 }
846 newvp = NFSTOV(np);
847 }
848 if (v3) {
849 nfsm_postop_attr(newvp, attrflag);
850 nfsm_postop_attr(dvp, attrflag);
851 } else
852 nfsm_loadattr(newvp, (struct vattr *)0);
853 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
854 cnp->cn_flags |= SAVENAME;
855 if ((cnp->cn_flags & MAKEENTRY) &&
856 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
857 np->n_ctime = np->n_vattr->va_ctime.tv_sec;
858 cache_enter(dvp, newvp, cnp);
859 }
860 *vpp = newvp;
861 nfsm_reqdone;
862 if (error) {
863 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) &&
864 cnp->cn_nameiop != CREATE) {
865 if (VTONFS(dvp)->n_nctime == 0)
866 VTONFS(dvp)->n_nctime =
867 VTONFS(dvp)->n_vattr->va_mtime.tv_sec;
868 cache_enter(dvp, NULL, cnp);
869 }
870 if (newvp != NULLVP)
871 vrele(newvp);
872 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
873 (flags & ISLASTCN) && error == ENOENT) {
874 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
875 error = EROFS;
876 else
877 error = EJUSTRETURN;
878 }
879 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
880 cnp->cn_flags |= SAVENAME;
881 }
882 return (error);
883 }
884
885 /*
886 * nfs read call.
887 * Just call nfs_bioread() to do the work.
888 */
889 int
890 nfs_read(v)
891 void *v;
892 {
893 struct vop_read_args /* {
894 struct vnode *a_vp;
895 struct uio *a_uio;
896 int a_ioflag;
897 struct ucred *a_cred;
898 } */ *ap = v;
899 register struct vnode *vp = ap->a_vp;
900
901 if (vp->v_type != VREG)
902 return (EPERM);
903 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred, 0));
904 }
905
906 /*
907 * nfs readlink call
908 */
909 int
910 nfs_readlink(v)
911 void *v;
912 {
913 struct vop_readlink_args /* {
914 struct vnode *a_vp;
915 struct uio *a_uio;
916 struct ucred *a_cred;
917 } */ *ap = v;
918 register struct vnode *vp = ap->a_vp;
919
920 if (vp->v_type != VLNK)
921 return (EPERM);
922 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred, 0));
923 }
924
925 /*
926 * Do a readlink rpc.
927 * Called by nfs_doio() from below the buffer cache.
928 */
929 int
930 nfs_readlinkrpc(vp, uiop, cred)
931 register struct vnode *vp;
932 struct uio *uiop;
933 struct ucred *cred;
934 {
935 register u_int32_t *tl;
936 register caddr_t cp;
937 register int32_t t1, t2;
938 caddr_t bpos, dpos, cp2;
939 int error = 0, len, attrflag;
940 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
941 int v3 = NFS_ISV3(vp);
942
943 nfsstats.rpccnt[NFSPROC_READLINK]++;
944 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
945 nfsm_fhtom(vp, v3);
946 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_procp, cred);
947 if (v3)
948 nfsm_postop_attr(vp, attrflag);
949 if (!error) {
950 nfsm_strsiz(len, NFS_MAXPATHLEN);
951 nfsm_mtouio(uiop, len);
952 }
953 nfsm_reqdone;
954 return (error);
955 }
956
957 /*
958 * nfs read rpc call
959 * Ditto above
960 */
961 int
962 nfs_readrpc(vp, uiop, cred)
963 register struct vnode *vp;
964 struct uio *uiop;
965 struct ucred *cred;
966 {
967 register u_int32_t *tl;
968 register caddr_t cp;
969 register int32_t t1, t2;
970 caddr_t bpos, dpos, cp2;
971 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
972 struct nfsmount *nmp;
973 int error = 0, len, retlen, tsiz, eof, attrflag;
974 int v3 = NFS_ISV3(vp);
975
976 #ifndef nolint
977 eof = 0;
978 #endif
979 nmp = VFSTONFS(vp->v_mount);
980 tsiz = uiop->uio_resid;
981 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
982 return (EFBIG);
983 while (tsiz > 0) {
984 nfsstats.rpccnt[NFSPROC_READ]++;
985 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
986 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
987 nfsm_fhtom(vp, v3);
988 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
989 if (v3) {
990 txdr_hyper(uiop->uio_offset, tl);
991 *(tl + 2) = txdr_unsigned(len);
992 } else {
993 *tl++ = txdr_unsigned(uiop->uio_offset);
994 *tl++ = txdr_unsigned(len);
995 *tl = 0;
996 }
997 nfsm_request(vp, NFSPROC_READ, uiop->uio_procp, cred);
998 if (v3) {
999 nfsm_postop_attr(vp, attrflag);
1000 if (error) {
1001 m_freem(mrep);
1002 goto nfsmout;
1003 }
1004 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1005 eof = fxdr_unsigned(int, *(tl + 1));
1006 } else
1007 nfsm_loadattr(vp, (struct vattr *)0);
1008 nfsm_strsiz(retlen, nmp->nm_rsize);
1009 nfsm_mtouio(uiop, retlen);
1010 m_freem(mrep);
1011 tsiz -= retlen;
1012 if (v3) {
1013 if (eof || retlen == 0)
1014 tsiz = 0;
1015 } else if (retlen < len)
1016 tsiz = 0;
1017 }
1018 nfsmout:
1019 return (error);
1020 }
1021
1022 /*
1023 * nfs write call
1024 */
1025 int
1026 nfs_writerpc(vp, uiop, cred, iomode, must_commit)
1027 register struct vnode *vp;
1028 register struct uio *uiop;
1029 struct ucred *cred;
1030 int *iomode, *must_commit;
1031 {
1032 register u_int32_t *tl;
1033 register caddr_t cp;
1034 register int32_t t1, t2, backup;
1035 caddr_t bpos, dpos, cp2;
1036 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1037 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1038 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1039 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1040
1041 #ifndef DIAGNOSTIC
1042 if (uiop->uio_iovcnt != 1)
1043 panic("nfs: writerpc iovcnt > 1");
1044 #endif
1045 *must_commit = 0;
1046 tsiz = uiop->uio_resid;
1047 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1048 return (EFBIG);
1049 while (tsiz > 0) {
1050 nfsstats.rpccnt[NFSPROC_WRITE]++;
1051 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1052 nfsm_reqhead(vp, NFSPROC_WRITE,
1053 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1054 nfsm_fhtom(vp, v3);
1055 if (v3) {
1056 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1057 txdr_hyper(uiop->uio_offset, tl);
1058 tl += 2;
1059 *tl++ = txdr_unsigned(len);
1060 *tl++ = txdr_unsigned(*iomode);
1061 *tl = txdr_unsigned(len);
1062 } else {
1063 register u_int32_t x;
1064
1065 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1066 /* Set both "begin" and "current" to non-garbage. */
1067 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1068 *tl++ = x; /* "begin offset" */
1069 *tl++ = x; /* "current offset" */
1070 x = txdr_unsigned(len);
1071 *tl++ = x; /* total to this offset */
1072 *tl = x; /* size of this write */
1073
1074 }
1075 nfsm_uiotom(uiop, len);
1076 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_procp, cred);
1077 if (v3) {
1078 wccflag = NFSV3_WCCCHK;
1079 nfsm_wcc_data(vp, wccflag);
1080 if (!error) {
1081 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1082 + NFSX_V3WRITEVERF);
1083 rlen = fxdr_unsigned(int, *tl++);
1084 if (rlen == 0) {
1085 error = NFSERR_IO;
1086 m_freem(mrep);
1087 break;
1088 } else if (rlen < len) {
1089 backup = len - rlen;
1090 (caddr_t)uiop->uio_iov->iov_base -=
1091 backup;
1092 uiop->uio_iov->iov_len += backup;
1093 uiop->uio_offset -= backup;
1094 uiop->uio_resid += backup;
1095 len = rlen;
1096 }
1097 commit = fxdr_unsigned(int, *tl++);
1098
1099 /*
1100 * Return the lowest committment level
1101 * obtained by any of the RPCs.
1102 */
1103 if (committed == NFSV3WRITE_FILESYNC)
1104 committed = commit;
1105 else if (committed == NFSV3WRITE_DATASYNC &&
1106 commit == NFSV3WRITE_UNSTABLE)
1107 committed = commit;
1108 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0){
1109 memcpy((caddr_t)nmp->nm_verf, (caddr_t)tl,
1110 NFSX_V3WRITEVERF);
1111 nmp->nm_iflag |= NFSMNT_HASWRITEVERF;
1112 } else if (memcmp((caddr_t)tl,
1113 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1114 *must_commit = 1;
1115 memcpy((caddr_t)nmp->nm_verf, (caddr_t)tl,
1116 NFSX_V3WRITEVERF);
1117 }
1118 }
1119 } else
1120 nfsm_loadattr(vp, (struct vattr *)0);
1121 if (wccflag)
1122 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr->va_mtime.tv_sec;
1123 m_freem(mrep);
1124 if (error)
1125 break;
1126 tsiz -= len;
1127 }
1128 nfsmout:
1129 *iomode = committed;
1130 if (error)
1131 uiop->uio_resid = tsiz;
1132 return (error);
1133 }
1134
1135 /*
1136 * nfs mknod rpc
1137 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1138 * mode set to specify the file type and the size field for rdev.
1139 */
1140 int
1141 nfs_mknodrpc(dvp, vpp, cnp, vap)
1142 register struct vnode *dvp;
1143 register struct vnode **vpp;
1144 register struct componentname *cnp;
1145 register struct vattr *vap;
1146 {
1147 register struct nfsv2_sattr *sp;
1148 register u_int32_t *tl;
1149 register caddr_t cp;
1150 register int32_t t1, t2;
1151 struct vnode *newvp = (struct vnode *)0;
1152 struct nfsnode *np;
1153 char *cp2;
1154 caddr_t bpos, dpos;
1155 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1156 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1157 u_int32_t rdev;
1158 int v3 = NFS_ISV3(dvp);
1159
1160 if (vap->va_type == VCHR || vap->va_type == VBLK)
1161 rdev = txdr_unsigned(vap->va_rdev);
1162 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1163 rdev = nfs_xdrneg1;
1164 else {
1165 VOP_ABORTOP(dvp, cnp);
1166 vput(dvp);
1167 return (EOPNOTSUPP);
1168 }
1169 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1170 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1171 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1172 nfsm_fhtom(dvp, v3);
1173 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1174 if (v3) {
1175 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1176 *tl++ = vtonfsv3_type(vap->va_type);
1177 nfsm_v3attrbuild(vap, FALSE);
1178 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1179 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1180 *tl++ = txdr_unsigned(major(vap->va_rdev));
1181 *tl = txdr_unsigned(minor(vap->va_rdev));
1182 }
1183 } else {
1184 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1185 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1186 sp->sa_uid = nfs_xdrneg1;
1187 sp->sa_gid = nfs_xdrneg1;
1188 sp->sa_size = rdev;
1189 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1190 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1191 }
1192 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_proc, cnp->cn_cred);
1193 if (!error) {
1194 nfsm_mtofh(dvp, newvp, v3, gotvp);
1195 if (!gotvp) {
1196 if (newvp) {
1197 vrele(newvp);
1198 newvp = (struct vnode *)0;
1199 }
1200 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1201 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np);
1202 if (!error)
1203 newvp = NFSTOV(np);
1204 }
1205 }
1206 if (v3)
1207 nfsm_wcc_data(dvp, wccflag);
1208 nfsm_reqdone;
1209 if (error) {
1210 if (newvp)
1211 vrele(newvp);
1212 } else {
1213 if (cnp->cn_flags & MAKEENTRY)
1214 cache_enter(dvp, newvp, cnp);
1215 *vpp = newvp;
1216 }
1217 FREE(cnp->cn_pnbuf, M_NAMEI);
1218 VTONFS(dvp)->n_flag |= NMODIFIED;
1219 if (!wccflag)
1220 VTONFS(dvp)->n_attrstamp = 0;
1221 vrele(dvp);
1222 return (error);
1223 }
1224
1225 /*
1226 * nfs mknod vop
1227 * just call nfs_mknodrpc() to do the work.
1228 */
1229 /* ARGSUSED */
1230 int
1231 nfs_mknod(v)
1232 void *v;
1233 {
1234 struct vop_mknod_args /* {
1235 struct vnode *a_dvp;
1236 struct vnode **a_vpp;
1237 struct componentname *a_cnp;
1238 struct vattr *a_vap;
1239 } */ *ap = v;
1240 struct vnode *newvp;
1241 int error;
1242
1243 error = nfs_mknodrpc(ap->a_dvp, &newvp, ap->a_cnp, ap->a_vap);
1244 if (!error)
1245 vrele(newvp);
1246 return (error);
1247 }
1248
1249 static u_long create_verf;
1250 /*
1251 * nfs file create call
1252 */
1253 int
1254 nfs_create(v)
1255 void *v;
1256 {
1257 struct vop_create_args /* {
1258 struct vnode *a_dvp;
1259 struct vnode **a_vpp;
1260 struct componentname *a_cnp;
1261 struct vattr *a_vap;
1262 } */ *ap = v;
1263 register struct vnode *dvp = ap->a_dvp;
1264 register struct vattr *vap = ap->a_vap;
1265 register struct componentname *cnp = ap->a_cnp;
1266 register struct nfsv2_sattr *sp;
1267 register u_int32_t *tl;
1268 register caddr_t cp;
1269 register int32_t t1, t2;
1270 struct nfsnode *np = (struct nfsnode *)0;
1271 struct vnode *newvp = (struct vnode *)0;
1272 caddr_t bpos, dpos, cp2;
1273 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1274 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1275 int v3 = NFS_ISV3(dvp);
1276
1277 /*
1278 * Oops, not for me..
1279 */
1280 if (vap->va_type == VSOCK)
1281 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1282
1283 #ifdef VA_EXCLUSIVE
1284 if (vap->va_vaflags & VA_EXCLUSIVE)
1285 fmode |= O_EXCL;
1286 #endif
1287 again:
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