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