nfs_vnops.c revision 1.104 1 /* $NetBSD: nfs_vnops.c,v 1.104 1999/08/02 18:58:23 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 if (!lockparent || !(flags & ISLASTCN))
786 cnp->cn_flags |= PDIRUNLOCK;
787 return (0);
788 }
789 cache_purge(newvp);
790 }
791 vrele(newvp);
792 }
793 *vpp = NULLVP;
794 }
795 dorpc:
796 error = 0;
797 newvp = NULLVP;
798 nfsstats.lookupcache_misses++;
799 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
800 len = cnp->cn_namelen;
801 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
802 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
803 nfsm_fhtom(dvp, v3);
804 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
805 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_proc, cnp->cn_cred);
806 if (error) {
807 nfsm_postop_attr(dvp, attrflag);
808 m_freem(mrep);
809 goto nfsmout;
810 }
811 nfsm_getfh(fhp, fhsize, v3);
812
813 /*
814 * Handle RENAME case...
815 */
816 if (cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
817 if (NFS_CMPFH(np, fhp, fhsize)) {
818 m_freem(mrep);
819 return (EISDIR);
820 }
821 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
822 if (error) {
823 m_freem(mrep);
824 return (error);
825 }
826 newvp = NFSTOV(np);
827 if (v3) {
828 nfsm_postop_attr(newvp, attrflag);
829 nfsm_postop_attr(dvp, attrflag);
830 } else
831 nfsm_loadattr(newvp, (struct vattr *)0);
832 *vpp = newvp;
833 m_freem(mrep);
834 cnp->cn_flags |= SAVENAME;
835 if (!lockparent || !(flags & ISLASTCN))
836 cnp->cn_flags |= PDIRUNLOCK;
837 return (0);
838 }
839
840 if (NFS_CMPFH(np, fhp, fhsize)) {
841 VREF(dvp);
842 newvp = dvp;
843 } else {
844 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
845 if (error) {
846 m_freem(mrep);
847 return (error);
848 }
849 newvp = NFSTOV(np);
850 }
851 if (v3) {
852 nfsm_postop_attr(newvp, attrflag);
853 nfsm_postop_attr(dvp, attrflag);
854 } else
855 nfsm_loadattr(newvp, (struct vattr *)0);
856 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
857 cnp->cn_flags |= SAVENAME;
858 if ((cnp->cn_flags & MAKEENTRY) &&
859 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
860 np->n_ctime = np->n_vattr->va_ctime.tv_sec;
861 cache_enter(dvp, newvp, cnp);
862 }
863 *vpp = newvp;
864 nfsm_reqdone;
865 if (error) {
866 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) &&
867 cnp->cn_nameiop != CREATE) {
868 if (VTONFS(dvp)->n_nctime == 0)
869 VTONFS(dvp)->n_nctime =
870 VTONFS(dvp)->n_vattr->va_mtime.tv_sec;
871 cache_enter(dvp, NULL, cnp);
872 }
873 if (newvp != NULLVP)
874 vrele(newvp);
875 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
876 (flags & ISLASTCN) && error == ENOENT) {
877 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
878 error = EROFS;
879 else
880 error = EJUSTRETURN;
881 }
882 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
883 cnp->cn_flags |= SAVENAME;
884 } else {
885 if (!lockparent || !(flags & ISLASTCN))
886 cnp->cn_flags |= PDIRUNLOCK;
887 }
888 return (error);
889 }
890
891 /*
892 * nfs read call.
893 * Just call nfs_bioread() to do the work.
894 */
895 int
896 nfs_read(v)
897 void *v;
898 {
899 struct vop_read_args /* {
900 struct vnode *a_vp;
901 struct uio *a_uio;
902 int a_ioflag;
903 struct ucred *a_cred;
904 } */ *ap = v;
905 register struct vnode *vp = ap->a_vp;
906
907 if (vp->v_type != VREG)
908 return (EPERM);
909 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred, 0));
910 }
911
912 /*
913 * nfs readlink call
914 */
915 int
916 nfs_readlink(v)
917 void *v;
918 {
919 struct vop_readlink_args /* {
920 struct vnode *a_vp;
921 struct uio *a_uio;
922 struct ucred *a_cred;
923 } */ *ap = v;
924 register struct vnode *vp = ap->a_vp;
925
926 if (vp->v_type != VLNK)
927 return (EPERM);
928 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred, 0));
929 }
930
931 /*
932 * Do a readlink rpc.
933 * Called by nfs_doio() from below the buffer cache.
934 */
935 int
936 nfs_readlinkrpc(vp, uiop, cred)
937 register struct vnode *vp;
938 struct uio *uiop;
939 struct ucred *cred;
940 {
941 register u_int32_t *tl;
942 register caddr_t cp;
943 register int32_t t1, t2;
944 caddr_t bpos, dpos, cp2;
945 int error = 0, len, attrflag;
946 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
947 int v3 = NFS_ISV3(vp);
948
949 nfsstats.rpccnt[NFSPROC_READLINK]++;
950 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
951 nfsm_fhtom(vp, v3);
952 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_procp, cred);
953 if (v3)
954 nfsm_postop_attr(vp, attrflag);
955 if (!error) {
956 nfsm_strsiz(len, NFS_MAXPATHLEN);
957 nfsm_mtouio(uiop, len);
958 }
959 nfsm_reqdone;
960 return (error);
961 }
962
963 /*
964 * nfs read rpc call
965 * Ditto above
966 */
967 int
968 nfs_readrpc(vp, uiop, cred)
969 register struct vnode *vp;
970 struct uio *uiop;
971 struct ucred *cred;
972 {
973 register u_int32_t *tl;
974 register caddr_t cp;
975 register int32_t t1, t2;
976 caddr_t bpos, dpos, cp2;
977 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
978 struct nfsmount *nmp;
979 int error = 0, len, retlen, tsiz, eof, attrflag;
980 int v3 = NFS_ISV3(vp);
981
982 #ifndef nolint
983 eof = 0;
984 #endif
985 nmp = VFSTONFS(vp->v_mount);
986 tsiz = uiop->uio_resid;
987 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
988 return (EFBIG);
989 while (tsiz > 0) {
990 nfsstats.rpccnt[NFSPROC_READ]++;
991 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
992 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
993 nfsm_fhtom(vp, v3);
994 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
995 if (v3) {
996 txdr_hyper(uiop->uio_offset, tl);
997 *(tl + 2) = txdr_unsigned(len);
998 } else {
999 *tl++ = txdr_unsigned(uiop->uio_offset);
1000 *tl++ = txdr_unsigned(len);
1001 *tl = 0;
1002 }
1003 nfsm_request(vp, NFSPROC_READ, uiop->uio_procp, cred);
1004 if (v3) {
1005 nfsm_postop_attr(vp, attrflag);
1006 if (error) {
1007 m_freem(mrep);
1008 goto nfsmout;
1009 }
1010 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1011 eof = fxdr_unsigned(int, *(tl + 1));
1012 } else
1013 nfsm_loadattr(vp, (struct vattr *)0);
1014 nfsm_strsiz(retlen, nmp->nm_rsize);
1015 nfsm_mtouio(uiop, retlen);
1016 m_freem(mrep);
1017 tsiz -= retlen;
1018 if (v3) {
1019 if (eof || retlen == 0)
1020 tsiz = 0;
1021 } else if (retlen < len)
1022 tsiz = 0;
1023 }
1024 nfsmout:
1025 return (error);
1026 }
1027
1028 /*
1029 * nfs write call
1030 */
1031 int
1032 nfs_writerpc(vp, uiop, cred, iomode, must_commit)
1033 register struct vnode *vp;
1034 register struct uio *uiop;
1035 struct ucred *cred;
1036 int *iomode, *must_commit;
1037 {
1038 register u_int32_t *tl;
1039 register caddr_t cp;
1040 register int32_t t1, t2, backup;
1041 caddr_t bpos, dpos, cp2;
1042 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1043 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1044 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1045 int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1046
1047 #ifndef DIAGNOSTIC
1048 if (uiop->uio_iovcnt != 1)
1049 panic("nfs: writerpc iovcnt > 1");
1050 #endif
1051 *must_commit = 0;
1052 tsiz = uiop->uio_resid;
1053 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1054 return (EFBIG);
1055 while (tsiz > 0) {
1056 nfsstats.rpccnt[NFSPROC_WRITE]++;
1057 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1058 nfsm_reqhead(vp, NFSPROC_WRITE,
1059 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1060 nfsm_fhtom(vp, v3);
1061 if (v3) {
1062 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1063 txdr_hyper(uiop->uio_offset, tl);
1064 tl += 2;
1065 *tl++ = txdr_unsigned(len);
1066 *tl++ = txdr_unsigned(*iomode);
1067 *tl = txdr_unsigned(len);
1068 } else {
1069 register u_int32_t x;
1070
1071 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1072 /* Set both "begin" and "current" to non-garbage. */
1073 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1074 *tl++ = x; /* "begin offset" */
1075 *tl++ = x; /* "current offset" */
1076 x = txdr_unsigned(len);
1077 *tl++ = x; /* total to this offset */
1078 *tl = x; /* size of this write */
1079
1080 }
1081 nfsm_uiotom(uiop, len);
1082 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_procp, cred);
1083 if (v3) {
1084 wccflag = NFSV3_WCCCHK;
1085 nfsm_wcc_data(vp, wccflag);
1086 if (!error) {
1087 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1088 + NFSX_V3WRITEVERF);
1089 rlen = fxdr_unsigned(int, *tl++);
1090 if (rlen == 0) {
1091 error = NFSERR_IO;
1092 m_freem(mrep);
1093 break;
1094 } else if (rlen < len) {
1095 backup = len - rlen;
1096 (caddr_t)uiop->uio_iov->iov_base -=
1097 backup;
1098 uiop->uio_iov->iov_len += backup;
1099 uiop->uio_offset -= backup;
1100 uiop->uio_resid += backup;
1101 len = rlen;
1102 }
1103 commit = fxdr_unsigned(int, *tl++);
1104
1105 /*
1106 * Return the lowest committment level
1107 * obtained by any of the RPCs.
1108 */
1109 if (committed == NFSV3WRITE_FILESYNC)
1110 committed = commit;
1111 else if (committed == NFSV3WRITE_DATASYNC &&
1112 commit == NFSV3WRITE_UNSTABLE)
1113 committed = commit;
1114 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0){
1115 memcpy((caddr_t)nmp->nm_verf, (caddr_t)tl,
1116 NFSX_V3WRITEVERF);
1117 nmp->nm_iflag |= NFSMNT_HASWRITEVERF;
1118 } else if (memcmp((caddr_t)tl,
1119 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1120 *must_commit = 1;
1121 memcpy((caddr_t)nmp->nm_verf, (caddr_t)tl,
1122 NFSX_V3WRITEVERF);
1123 }
1124 }
1125 } else
1126 nfsm_loadattr(vp, (struct vattr *)0);
1127 if (wccflag)
1128 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr->va_mtime.tv_sec;
1129 m_freem(mrep);
1130 if (error)
1131 break;
1132 tsiz -= len;
1133 }
1134 nfsmout:
1135 *iomode = committed;
1136 if (error)
1137 uiop->uio_resid = tsiz;
1138 return (error);
1139 }
1140
1141 /*
1142 * nfs mknod rpc
1143 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1144 * mode set to specify the file type and the size field for rdev.
1145 */
1146 int
1147 nfs_mknodrpc(dvp, vpp, cnp, vap)
1148 register struct vnode *dvp;
1149 register struct vnode **vpp;
1150 register struct componentname *cnp;
1151 register struct vattr *vap;
1152 {
1153 register struct nfsv2_sattr *sp;
1154 register u_int32_t *tl;
1155 register caddr_t cp;
1156 register int32_t t1, t2;
1157 struct vnode *newvp = (struct vnode *)0;
1158 struct nfsnode *np;
1159 char *cp2;
1160 caddr_t bpos, dpos;
1161 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1162 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1163 u_int32_t rdev;
1164 int v3 = NFS_ISV3(dvp);
1165
1166 if (vap->va_type == VCHR || vap->va_type == VBLK)
1167 rdev = txdr_unsigned(vap->va_rdev);
1168 else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1169 rdev = nfs_xdrneg1;
1170 else {
1171 VOP_ABORTOP(dvp, cnp);
1172 vput(dvp);
1173 return (EOPNOTSUPP);
1174 }
1175 nfsstats.rpccnt[NFSPROC_MKNOD]++;
1176 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1177 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1178 nfsm_fhtom(dvp, v3);
1179 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1180 if (v3) {
1181 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1182 *tl++ = vtonfsv3_type(vap->va_type);
1183 nfsm_v3attrbuild(vap, FALSE);
1184 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1185 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1186 *tl++ = txdr_unsigned(major(vap->va_rdev));
1187 *tl = txdr_unsigned(minor(vap->va_rdev));
1188 }
1189 } else {
1190 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1191 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1192 sp->sa_uid = nfs_xdrneg1;
1193 sp->sa_gid = nfs_xdrneg1;
1194 sp->sa_size = rdev;
1195 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1196 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1197 }
1198 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_proc, cnp->cn_cred);
1199 if (!error) {
1200 nfsm_mtofh(dvp, newvp, v3, gotvp);
1201 if (!gotvp) {
1202 if (newvp) {
1203 vrele(newvp);
1204 newvp = (struct vnode *)0;
1205 }
1206 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1207 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np);
1208 if (!error)
1209 newvp = NFSTOV(np);
1210 }
1211 }
1212 if (v3)
1213 nfsm_wcc_data(dvp, wccflag);
1214 nfsm_reqdone;
1215 if (error) {
1216 if (newvp)
1217 vrele(newvp);
1218 } else {
1219 if (cnp->cn_flags & MAKEENTRY)
1220 cache_enter(dvp, newvp, cnp);
1221 *vpp = newvp;
1222 }
1223 FREE(cnp->cn_pnbuf, M_NAMEI);
1224 VTONFS(dvp)->n_flag |= NMODIFIED;
1225 if (!wccflag)
1226 VTONFS(dvp)->n_attrstamp = 0;
1227 vrele(dvp);
1228 return (error);
1229 }
1230
1231 /*
1232 * nfs mknod vop
1233 * just call nfs_mknodrpc() to do the work.
1234 */
1235 /* ARGSUSED */
1236 int
1237 nfs_mknod(v)
1238 void *v;
1239 {
1240 struct vop_mknod_args /* {
1241 struct vnode *a_dvp;
1242 struct vnode **a_vpp;
1243 struct componentname *a_cnp;
1244 struct vattr *a_vap;
1245 } */ *ap = v;
1246 struct vnode *newvp;
1247 int error;
1248
1249 error = nfs_mknodrpc(ap->a_dvp, &newvp, ap->a_cnp, ap->a_vap);
1250 if (!error)
1251 vrele(newvp);
1252 return (error);
1253 }
1254
1255 static u_long create_verf;
1256 /*
1257 * nfs file create call
1258 */
1259 int
1260 nfs_create(v)
1261 void *v;
1262 {
1263 struct vop_create_args /* {
1264 struct vnode *a_dvp;
1265 struct vnode **a_vpp;
1266 struct componentname *a_cnp;
1267 struct vattr *a_vap;
1268 } */ *ap = v;
1269 register struct vnode *dvp = ap->a_dvp;
1270 register struct vattr *vap = ap->a_vap;
1271 register struct componentname *cnp = ap->a_cnp;
1272 register struct nfsv2_sattr *sp;
1273 register u_int32_t *tl;
1274 register caddr_t cp;
1275 register int32_t t1, t2;
1276 struct nfsnode *np = (struct nfsnode *)0;
1277 struct vnode *newvp = (struct vnode *)0;
1278 caddr_t bpos, dpos, cp2;
1279 int error, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1280 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1281 int v3 = NFS_ISV3(dvp);
1282
1283 /*
1284 * Oops, not for me..
1285 */
1286 if (vap->va_type == VSOCK)
1287 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1288
1289 #ifdef VA_EXCLUSIVE
1290 if (vap->va_vaflags & VA_EXCLUSIVE)
1291 fmode |= O_EXCL;
1292 #endif
1293 again:
1294 error = 0;
1295 nfsstats.rpccnt[NFSPROC_CREATE]++;
1296 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1297 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1298 nfsm_fhtom(dvp, v3);
1299 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1300 if (v3) {
1301 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1302 if (fmode & O_EXCL) {
1303 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1304 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1305 if (in_ifaddr.tqh_first)
1306 *tl++ = in_ifaddr.tqh_first->ia_addr.sin_addr.s_addr;
1307 else
1308 *tl++ = create_verf;
1309 *tl = ++create_verf;
1310 } else {
1311 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1312 nfsm_v3attrbuild(vap, FALSE);
1313 }
1314 } else {
1315 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1316 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1317 sp->sa_uid = nfs_xdrneg1;
1318 sp->sa_gid = nfs_xdrneg1;
1319 sp->sa_size = 0;
1320 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1321 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1322 }
1323 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_proc, cnp->cn_cred);
1324 if (!error) {
1325 nfsm_mtofh(dvp, newvp, v3, gotvp);
1326 if (!gotvp) {
1327 if (newvp) {
1328 vrele(newvp);
1329 newvp = (struct vnode *)0;
1330 }
1331 error = nfs_lookitup(dvp, cnp->cn_nameptr,
1332 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np);
1333 if (!error)
1334 newvp = NFSTOV(np);
1335 }
1336 }
1337 if (v3)
1338 nfsm_wcc_data(dvp, wccflag);
1339 nfsm_reqdone;
1340 if (error) {
1341 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1342 fmode &= ~O_EXCL;
1343 goto again;
1344 }
1345 if (newvp)
1346 vrele(newvp);
1347 } else if (v3 && (fmode & O_EXCL))
1348 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_proc);
1349 if (!error) {
1350 if (cnp->cn_flags & MAKEENTRY)
1351 cache_enter(dvp, newvp, cnp);
1352 *ap->a_vpp = newvp;
1353 }
1354 FREE(cnp->cn_pnbuf, M_NAMEI);
1355 VTONFS(dvp)->n_flag |= NMODIFIED;
1356 if (!wccflag)
1357 VTONFS(dvp)->n_attrstamp = 0;
1358 vrele(dvp);
1359 return (error);
1360 }
1361
1362 /*
1363 * nfs file remove call
1364 * To try and make nfs semantics closer to ufs semantics, a file that has
1365 * other processes using the vnode is renamed instead of removed and then
1366 * removed later on the last close.
1367 * - If v_usecount > 1
1368 * If a rename is not already in the works
1369 * call nfs_sillyrename() to set it up
1370 * else
1371 * do the remove rpc
1372 */
1373 int
1374 nfs_remove(v)
1375 void *v;
1376 {
1377 struct vop_remove_args /* {
1378 struct vnodeop_desc *a_desc;
1379 struct vnode * a_dvp;
1380 struct vnode * a_vp;
1381 struct componentname * a_cnp;
1382 } */ *ap = v;
1383 register struct vnode *vp = ap->a_vp;
1384 register struct vnode *dvp = ap->a_dvp;
1385 register struct componentname *cnp = ap->a_cnp;
1386 register struct nfsnode *np = VTONFS(vp);
1387 int error = 0;
1388 struct vattr vattr;
1389
1390 #ifndef DIAGNOSTIC
1391 if ((cnp->cn_flags & HASBUF) == 0)
1392 panic("nfs_remove: no name");
1393 if (vp->v_usecount < 1)
1394 panic("nfs_remove: bad v_usecount");
1395 #endif
1396 if (vp->v_type == VDIR)
1397 error = EPERM;
1398 else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1399 VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_proc) == 0 &&
1400 vattr.va_nlink > 1)) {
1401 /*
1402 * Purge the name cache so that the chance of a lookup for
1403 * the name succeeding while the remove is in progress is
1404 * minimized. Without node locking it can still happen, such
1405 * that an I/O op returns ESTALE, but since you get this if
1406 * another host removes the file..
1407 */
1408 cache_purge(vp);
1409 /*
1410 * throw away biocache buffers, mainly to avoid
1411 * unnecessary delayed writes later.
1412 */
1413 error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, cnp->cn_proc, 1);
1414 /* Do the rpc */
1415 if (error != EINTR)
1416 error = nfs_removerpc(dvp, cnp->cn_nameptr,
1417 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc);
1418 /*
1419 * Kludge City: If the first reply to the remove rpc is lost..
1420 * the reply to the retransmitted request will be ENOENT
1421 * since the file was in fact removed
1422 * Therefore, we cheat and return success.
1423 */
1424 if (error == ENOENT)
1425 error = 0;
1426 } else if (!np->n_sillyrename)
1427 error = nfs_sillyrename(dvp, vp, cnp);
1428 FREE(cnp->cn_pnbuf, M_NAMEI);
1429 np->n_attrstamp = 0;
1430 vrele(dvp);
1431 vrele(vp);
1432 return (error);
1433 }
1434
1435 /*
1436 * nfs file remove rpc called from nfs_inactive
1437 */
1438 int
1439 nfs_removeit(sp)
1440 register struct sillyrename *sp;
1441 {
1442
1443 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1444 (struct proc *)0));
1445 }
1446
1447 /*
1448 * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1449 */
1450 int
1451 nfs_removerpc(dvp, name, namelen, cred, proc)
1452 register struct vnode *dvp;
1453 const char *name;
1454 int namelen;
1455 struct ucred *cred;
1456 struct proc *proc;
1457 {
1458 register u_int32_t *tl;
1459 register caddr_t cp;
1460 register int32_t t1, t2;
1461 caddr_t bpos, dpos, cp2;
1462 int error = 0, wccflag = NFSV3_WCCRATTR;
1463 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1464 int v3 = NFS_ISV3(dvp);
1465
1466 nfsstats.rpccnt[NFSPROC_REMOVE]++;
1467 nfsm_reqhead(dvp, NFSPROC_REMOVE,
1468 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1469 nfsm_fhtom(dvp, v3);
1470 nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1471 nfsm_request(dvp, NFSPROC_REMOVE, proc, cred);
1472 if (v3)
1473 nfsm_wcc_data(dvp, wccflag);
1474 nfsm_reqdone;
1475 VTONFS(dvp)->n_flag |= NMODIFIED;
1476 if (!wccflag)
1477 VTONFS(dvp)->n_attrstamp = 0;
1478 return (error);
1479 }
1480
1481 /*
1482 * nfs file rename call
1483 */
1484 int
1485 nfs_rename(v)
1486 void *v;
1487 {
1488 struct vop_rename_args /* {
1489 struct vnode *a_fdvp;
1490 struct vnode *a_fvp;
1491 struct componentname *a_fcnp;
1492 struct vnode *a_tdvp;
1493 struct vnode *a_tvp;
1494 struct componentname *a_tcnp;
1495 } */ *ap = v;
1496 register struct vnode *fvp = ap->a_fvp;
1497 register struct vnode *tvp = ap->a_tvp;
1498 register struct vnode *fdvp = ap->a_fdvp;
1499 register struct vnode *tdvp = ap->a_tdvp;
1500 register struct componentname *tcnp = ap->a_tcnp;
1501 register struct componentname *fcnp = ap->a_fcnp;
1502 int error;
1503
1504 #ifndef DIAGNOSTIC
1505 if ((tcnp->cn_flags & HASBUF) == 0 ||
1506 (fcnp->cn_flags & HASBUF) == 0)
1507 panic("nfs_rename: no name");
1508 #endif
1509 /* Check for cross-device rename */
1510 if ((fvp->v_mount != tdvp->v_mount) ||
1511 (tvp && (fvp->v_mount != tvp->v_mount))) {
1512 error = EXDEV;
1513 goto out;
1514 }
1515
1516 /*
1517 * If the tvp exists and is in use, sillyrename it before doing the
1518 * rename of the new file over it.
1519 */
1520 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1521 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1522 vrele(tvp);
1523 tvp = NULL;
1524 }
1525
1526 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1527 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1528 tcnp->cn_proc);
1529
1530 if (fvp->v_type == VDIR) {
1531 if (tvp != NULL && tvp->v_type == VDIR)
1532 cache_purge(tdvp);
1533 cache_purge(fdvp);
1534 }
1535 out:
1536 if (tdvp == tvp)
1537 vrele(tdvp);
1538 else
1539 vput(tdvp);
1540 if (tvp)
1541 vput(tvp);
1542 vrele(fdvp);
1543 vrele(fvp);
1544 /*
1545 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1546 */
1547 if (error == ENOENT)
1548 error = 0;
1549 return (error);
1550 }
1551
1552 /*
1553 * nfs file rename rpc called from nfs_remove() above
1554 */
1555 int
1556 nfs_renameit(sdvp, scnp, sp)
1557 struct vnode *sdvp;
1558 struct componentname *scnp;
1559 register struct sillyrename *sp;
1560 {
1561 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1562 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_proc));
1563 }
1564
1565 /*
1566 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1567 */
1568 int
1569 nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, proc)
1570 register struct vnode *fdvp;
1571 const char *fnameptr;
1572 int fnamelen;
1573 register struct vnode *tdvp;
1574 const char *tnameptr;
1575 int tnamelen;
1576 struct ucred *cred;
1577 struct proc *proc;
1578 {
1579 register u_int32_t *tl;
1580 register caddr_t cp;
1581 register int32_t t1, t2;
1582 caddr_t bpos, dpos, cp2;
1583 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1584 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1585 int v3 = NFS_ISV3(fdvp);
1586
1587 nfsstats.rpccnt[NFSPROC_RENAME]++;
1588 nfsm_reqhead(fdvp, NFSPROC_RENAME,
1589 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1590 nfsm_rndup(tnamelen));
1591 nfsm_fhtom(fdvp, v3);
1592 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1593 nfsm_fhtom(tdvp, v3);
1594 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1595 nfsm_request(fdvp, NFSPROC_RENAME, proc, cred);
1596 if (v3) {
1597 nfsm_wcc_data(fdvp, fwccflag);
1598 nfsm_wcc_data(tdvp, twccflag);
1599 }
1600 nfsm_reqdone;
1601 VTONFS(fdvp)->n_flag |= NMODIFIED;
1602 VTONFS(tdvp)->n_flag |= NMODIFIED;
1603 if (!fwccflag)
1604 VTONFS(fdvp)->n_attrstamp = 0;
1605 if (!twccflag)
1606 VTONFS(tdvp)->n_attrstamp = 0;
1607 return (error);
1608 }
1609
1610 /*
1611 * nfs hard link create call
1612 */
1613 int
1614 nfs_link(v)
1615 void *v;
1616 {
1617 struct vop_link_args /* {
1618 struct vnode *a_dvp;
1619 struct vnode *a_vp;
1620 struct componentname *a_cnp;
1621 } */ *ap = v;
1622 register struct vnode *vp = ap->a_vp;
1623 register struct vnode *dvp = ap->a_dvp;
1624 register struct componentname *cnp = ap->a_cnp;
1625 register u_int32_t *tl;
1626 register caddr_t cp;
1627 register int32_t t1, t2;
1628 caddr_t bpos, dpos, cp2;
1629 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1630 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1631 int v3;
1632
1633 if (dvp->v_mount != vp->v_mount) {
1634 VOP_ABORTOP(vp, cnp);
1635 vput(dvp);
1636 return (EXDEV);
1637 }
1638
1639 /*
1640 * Push all writes to the server, so that the attribute cache
1641 * doesn't get "out of sync" with the server.
1642 * XXX There should be a better way!
1643 */
1644 VOP_FSYNC(vp, cnp->cn_cred, FSYNC_WAIT, cnp->cn_proc);
1645
1646 v3 = NFS_ISV3(vp);
1647 nfsstats.rpccnt[NFSPROC_LINK]++;
1648 nfsm_reqhead(vp, NFSPROC_LINK,
1649 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1650 nfsm_fhtom(vp, v3);
1651 nfsm_fhtom(dvp, v3);
1652 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1653 nfsm_request(vp, NFSPROC_LINK, cnp->cn_proc, cnp->cn_cred);
1654 if (v3) {
1655 nfsm_postop_attr(vp, attrflag);
1656 nfsm_wcc_data(dvp, wccflag);
1657 }
1658 nfsm_reqdone;
1659 FREE(cnp->cn_pnbuf, M_NAMEI);
1660 VTONFS(dvp)->n_flag |= NMODIFIED;
1661 if (!attrflag)
1662 VTONFS(vp)->n_attrstamp = 0;
1663 if (!wccflag)
1664 VTONFS(dvp)->n_attrstamp = 0;
1665 vput(dvp);
1666 /*
1667 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1668 */
1669 if (error == EEXIST)
1670 error = 0;
1671 return (error);
1672 }
1673
1674 /*
1675 * nfs symbolic link create call
1676 */
1677 int
1678 nfs_symlink(v)
1679 void *v;
1680 {
1681 struct vop_symlink_args /* {
1682 struct vnode *a_dvp;
1683 struct vnode **a_vpp;
1684 struct componentname *a_cnp;
1685 struct vattr *a_vap;
1686 char *a_target;
1687 } */ *ap = v;
1688 register struct vnode *dvp = ap->a_dvp;
1689 register struct vattr *vap = ap->a_vap;
1690 register struct componentname *cnp = ap->a_cnp;
1691 register struct nfsv2_sattr *sp;
1692 register u_int32_t *tl;
1693 register caddr_t cp;
1694 register int32_t t1, t2;
1695 caddr_t bpos, dpos, cp2;
1696 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1697 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1698 struct vnode *newvp = (struct vnode *)0;
1699 int v3 = NFS_ISV3(dvp);
1700
1701 nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1702 slen = strlen(ap->a_target);
1703 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1704 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1705 nfsm_fhtom(dvp, v3);
1706 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1707 if (v3)
1708 nfsm_v3attrbuild(vap, FALSE);
1709 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1710 if (!v3) {
1711 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1712 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1713 sp->sa_uid = nfs_xdrneg1;
1714 sp->sa_gid = nfs_xdrneg1;
1715 sp->sa_size = nfs_xdrneg1;
1716 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1717 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1718 }
1719 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_proc, cnp->cn_cred);
1720 if (v3) {
1721 if (!error)
1722 nfsm_mtofh(dvp, newvp, v3, gotvp);
1723 nfsm_wcc_data(dvp, wccflag);
1724 }
1725 nfsm_reqdone;
1726 if (newvp)
1727 vrele(newvp);
1728 FREE(cnp->cn_pnbuf, M_NAMEI);
1729 VTONFS(dvp)->n_flag |= NMODIFIED;
1730 if (!wccflag)
1731 VTONFS(dvp)->n_attrstamp = 0;
1732 vrele(dvp);
1733 /*
1734 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1735 */
1736 if (error == EEXIST)
1737 error = 0;
1738 return (error);
1739 }
1740
1741 /*
1742 * nfs make dir call
1743 */
1744 int
1745 nfs_mkdir(v)
1746 void *v;
1747 {
1748 struct vop_mkdir_args /* {
1749 struct vnode *a_dvp;
1750 struct vnode **a_vpp;
1751 struct componentname *a_cnp;
1752 struct vattr *a_vap;
1753 } */ *ap = v;
1754 register struct vnode *dvp = ap->a_dvp;
1755 register struct vattr *vap = ap->a_vap;
1756 register struct componentname *cnp = ap->a_cnp;
1757 register struct nfsv2_sattr *sp;
1758 register u_int32_t *tl;
1759 register caddr_t cp;
1760 register int32_t t1, t2;
1761 register int len;
1762 struct nfsnode *np = (struct nfsnode *)0;
1763 struct vnode *newvp = (struct vnode *)0;
1764 caddr_t bpos, dpos, cp2;
1765 int error = 0, wccflag = NFSV3_WCCRATTR;
1766 int gotvp = 0;
1767 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1768 int v3 = NFS_ISV3(dvp);
1769
1770 len = cnp->cn_namelen;
1771 nfsstats.rpccnt[NFSPROC_MKDIR]++;
1772 nfsm_reqhead(dvp, NFSPROC_MKDIR,
1773 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1774 nfsm_fhtom(dvp, v3);
1775 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1776 if (v3) {
1777 nfsm_v3attrbuild(vap, FALSE);
1778 } else {
1779 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1780 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1781 sp->sa_uid = nfs_xdrneg1;
1782 sp->sa_gid = nfs_xdrneg1;
1783 sp->sa_size = nfs_xdrneg1;
1784 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1785 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1786 }
1787 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_proc, cnp->cn_cred);
1788 if (!error)
1789 nfsm_mtofh(dvp, newvp, v3, gotvp);
1790 if (v3)
1791 nfsm_wcc_data(dvp, wccflag);
1792 nfsm_reqdone;
1793 VTONFS(dvp)->n_flag |= NMODIFIED;
1794 if (!wccflag)
1795 VTONFS(dvp)->n_attrstamp = 0;
1796 /*
1797 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1798 * if we can succeed in looking up the directory.
1799 */
1800 if (error == EEXIST || (!error && !gotvp)) {
1801 if (newvp) {
1802 vrele(newvp);
1803 newvp = (struct vnode *)0;
1804 }
1805 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
1806 cnp->cn_proc, &np);
1807 if (!error) {
1808 newvp = NFSTOV(np);
1809 if (newvp->v_type != VDIR)
1810 error = EEXIST;
1811 }
1812 }
1813 if (error) {
1814 if (newvp)
1815 vrele(newvp);
1816 } else {
1817 if (cnp->cn_flags & MAKEENTRY)
1818 cache_enter(dvp, newvp, cnp);
1819 *ap->a_vpp = newvp;
1820 }
1821 FREE(cnp->cn_pnbuf, M_NAMEI);
1822 vrele(dvp);
1823 return (error);
1824 }
1825
1826 /*
1827 * nfs remove directory call
1828 */
1829 int
1830 nfs_rmdir(v)
1831 void *v;
1832 {
1833 struct vop_rmdir_args /* {
1834 struct vnode *a_dvp;
1835 struct vnode *a_vp;
1836 struct componentname *a_cnp;
1837 } */ *ap = v;
1838 register struct vnode *vp = ap->a_vp;
1839 register struct vnode *dvp = ap->a_dvp;
1840 register struct componentname *cnp = ap->a_cnp;
1841 register u_int32_t *tl;
1842 register caddr_t cp;
1843 register int32_t t1, t2;
1844 caddr_t bpos, dpos, cp2;
1845 int error = 0, wccflag = NFSV3_WCCRATTR;
1846 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1847 int v3 = NFS_ISV3(dvp);
1848
1849 if (dvp == vp) {
1850 vrele(dvp);
1851 vrele(dvp);
1852 FREE(cnp->cn_pnbuf, M_NAMEI);
1853 return (EINVAL);
1854 }
1855 nfsstats.rpccnt[NFSPROC_RMDIR]++;
1856 nfsm_reqhead(dvp, NFSPROC_RMDIR,
1857 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1858 nfsm_fhtom(dvp, v3);
1859 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1860 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_proc, cnp->cn_cred);
1861 if (v3)
1862 nfsm_wcc_data(dvp, wccflag);
1863 nfsm_reqdone;
1864 FREE(cnp->cn_pnbuf, M_NAMEI);
1865 VTONFS(dvp)->n_flag |= NMODIFIED;
1866 if (!wccflag)
1867 VTONFS(dvp)->n_attrstamp = 0;
1868 cache_purge(dvp);
1869 cache_purge(vp);
1870 vrele(vp);
1871 vrele(dvp);
1872 /*
1873 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
1874 */
1875 if (error == ENOENT)
1876 error = 0;
1877 return (error);
1878 }
1879
1880 /*
1881 * nfs readdir call
1882 */
1883 int
1884 nfs_readdir(v)
1885 void *v;
1886 {
1887 struct vop_readdir_args /* {
1888 struct vnode *a_vp;
1889 struct uio *a_uio;
1890 struct ucred *a_cred;
1891 int *a_eofflag;
1892 off_t **a_cookies;
1893 int *a_ncookies;
1894 } */ *ap = v;
1895 register struct vnode *vp = ap->a_vp;
1896 register struct uio *uio = ap->a_uio;
1897 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1898 char *base = uio->uio_iov->iov_base;
1899 int tresid, error;
1900 size_t count, lost;
1901 struct dirent *dp;
1902 off_t *cookies = NULL;
1903 int ncookies = 0, nc;
1904
1905 if (vp->v_type != VDIR)
1906 return (EPERM);
1907
1908 lost = uio->uio_resid & (NFS_DIRFRAGSIZ - 1);
1909 count = uio->uio_resid - lost;
1910 if (count <= 0)
1911 return (EINVAL);
1912
1913 /*
1914 * Call nfs_bioread() to do the real work.
1915 */
1916 tresid = uio->uio_resid = count;
1917 error = nfs_bioread(vp, uio, 0, ap->a_cred,
1918 ap->a_cookies ? NFSBIO_CACHECOOKIES : 0);
1919
1920 if (!error && ap->a_cookies) {
1921 ncookies = count / 16;
1922 MALLOC(cookies, off_t *, sizeof (off_t) * ncookies, M_TEMP,
1923 M_WAITOK);
1924 *ap->a_cookies = cookies;
1925 }
1926
1927 if (!error && uio->uio_resid == tresid) {
1928 uio->uio_resid += lost;
1929 nfsstats.direofcache_misses++;
1930 if (ap->a_cookies)
1931 *ap->a_ncookies = 0;
1932 *ap->a_eofflag = 1;
1933 return (0);
1934 }
1935
1936 if (!error && ap->a_cookies) {
1937 /*
1938 * Only the NFS server and emulations use cookies, and they
1939 * load the directory block into system space, so we can
1940 * just look at it directly.
1941 */
1942 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1943 panic("nfs_readdir: lost in space");
1944 for (nc = 0; ncookies-- &&
1945 base < (char *)uio->uio_iov->iov_base; nc++){
1946 dp = (struct dirent *) base;
1947 if (dp->d_reclen == 0)
1948 break;
1949 if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
1950 *(cookies++) = (off_t)NFS_GETCOOKIE32(dp);
1951 else
1952 *(cookies++) = NFS_GETCOOKIE(dp);
1953 base += dp->d_reclen;
1954 }
1955 uio->uio_resid +=
1956 ((caddr_t)uio->uio_iov->iov_base - base);
1957 uio->uio_iov->iov_len +=
1958 ((caddr_t)uio->uio_iov->iov_base - base);
1959 uio->uio_iov->iov_base = base;
1960 *ap->a_ncookies = nc;
1961 }
1962
1963 uio->uio_resid += lost;
1964 *ap->a_eofflag = 0;
1965 return (error);
1966 }
1967
1968 /*
1969 * Readdir rpc call.
1970 * Called from below the buffer cache by nfs_doio().
1971 */
1972 int
1973 nfs_readdirrpc(vp, uiop, cred)
1974 struct vnode *vp;
1975 register struct uio *uiop;
1976 struct ucred *cred;
1977 {
1978 register int len, left;
1979 register struct dirent *dp = NULL;
1980 register u_int32_t *tl;
1981 register caddr_t cp;
1982 register int32_t t1, t2;
1983 caddr_t bpos, dpos, cp2;
1984 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1985 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1986 struct nfsnode *dnp = VTONFS(vp);
1987 u_quad_t fileno;
1988 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
1989 int attrflag, nrpcs = 0, reclen;
1990 int v3 = NFS_ISV3(vp);
1991 nfsquad_t cookie;
1992
1993 #ifdef DIAGNOSTIC
1994 /*
1995 * Should be called from buffer cache, so only amount of
1996 * NFS_DIRBLKSIZ will be requested.
1997 */
1998 if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1)))
1999 panic("nfs readdirrpc bad uio");
2000 #endif
2001
2002 /*
2003 * Loop around doing readdir rpc's of size nm_readdirsize
2004 * truncated to a multiple of NFS_DIRFRAGSIZ.
2005 * The stopping criteria is EOF or buffer full.
2006 */
2007 while (more_dirs && bigenough) {
2008 /*
2009 * Heuristic: don't bother to do another RPC to further
2010 * fill up this block if there is not much room left. (< 50%
2011 * of the readdir RPC size). This wastes some buffer space
2012 * but can save up to 50% in RPC calls.
2013 */
2014 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
2015 bigenough = 0;
2016 break;
2017 }
2018 nfsstats.rpccnt[NFSPROC_READDIR]++;
2019 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2020 NFSX_READDIR(v3));
2021 nfsm_fhtom(vp, v3);
2022 if (v3) {
2023 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2024 cookie.qval = uiop->uio_offset;
2025 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2026 txdr_swapcookie3(uiop->uio_offset, tl);
2027 } else {
2028 txdr_cookie3(uiop->uio_offset, tl);
2029 }
2030 tl += 2;
2031 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2032 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2033 } else {
2034 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2035 *tl++ = txdr_unsigned(uiop->uio_offset);
2036 }
2037 *tl = txdr_unsigned(nmp->nm_readdirsize);
2038 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_procp, cred);
2039 nrpcs++;
2040 if (v3) {
2041 nfsm_postop_attr(vp, attrflag);
2042 if (!error) {
2043 nfsm_dissect(tl, u_int32_t *,
2044 2 * NFSX_UNSIGNED);
2045 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2046 dnp->n_cookieverf.nfsuquad[1] = *tl;
2047 } else {
2048 m_freem(mrep);
2049 goto nfsmout;
2050 }
2051 }
2052 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2053 more_dirs = fxdr_unsigned(int, *tl);
2054
2055 /* loop thru the dir entries, doctoring them to 4bsd form */
2056 while (more_dirs && bigenough) {
2057 if (v3) {
2058 nfsm_dissect(tl, u_int32_t *,
2059 3 * NFSX_UNSIGNED);
2060 fileno = fxdr_hyper(tl);
2061 len = fxdr_unsigned(int, *(tl + 2));
2062 } else {
2063 nfsm_dissect(tl, u_int32_t *,
2064 2 * NFSX_UNSIGNED);
2065 fileno = fxdr_unsigned(u_quad_t, *tl++);
2066 len = fxdr_unsigned(int, *tl);
2067 }
2068 if (len <= 0 || len > NFS_MAXNAMLEN) {
2069 error = EBADRPC;
2070 m_freem(mrep);
2071 goto nfsmout;
2072 }
2073 tlen = nfsm_rndup(len);
2074 if (tlen == len)
2075 tlen += 4; /* To ensure null termination */
2076 tlen += sizeof (off_t) + sizeof (int);
2077 reclen = ALIGN(tlen + DIRHDSIZ);
2078 tlen = reclen - DIRHDSIZ;
2079 left = NFS_DIRFRAGSIZ - blksiz;
2080 if (reclen > left) {
2081 dp->d_reclen += left;
2082 (caddr_t)uiop->uio_iov->iov_base += left;
2083 uiop->uio_iov->iov_len -= left;
2084 uiop->uio_resid -= left;
2085 blksiz = 0;
2086 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2087 }
2088 if (reclen > uiop->uio_resid)
2089 bigenough = 0;
2090 if (bigenough) {
2091 dp = (struct dirent *)uiop->uio_iov->iov_base;
2092 dp->d_fileno = (int)fileno;
2093 dp->d_namlen = len;
2094 dp->d_reclen = reclen;
2095 dp->d_type = DT_UNKNOWN;
2096 blksiz += dp->d_reclen;
2097 if (blksiz == NFS_DIRFRAGSIZ)
2098 blksiz = 0;
2099 uiop->uio_resid -= DIRHDSIZ;
2100 (caddr_t)uiop->uio_iov->iov_base += DIRHDSIZ;
2101 uiop->uio_iov->iov_len -= DIRHDSIZ;
2102 nfsm_mtouio(uiop, len);
2103 cp = uiop->uio_iov->iov_base;
2104 tlen -= len;
2105 *cp = '\0'; /* null terminate */
2106 (caddr_t)uiop->uio_iov->iov_base += tlen;
2107 uiop->uio_iov->iov_len -= tlen;
2108 uiop->uio_resid -= tlen;
2109 } else
2110 nfsm_adv(nfsm_rndup(len));
2111 if (v3) {
2112 nfsm_dissect(tl, u_int32_t *,
2113 3 * NFSX_UNSIGNED);
2114 } else {
2115 nfsm_dissect(tl, u_int32_t *,
2116 2 * NFSX_UNSIGNED);
2117 }
2118 if (bigenough) {
2119 if (v3) {
2120 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2121 uiop->uio_offset =
2122 fxdr_swapcookie3(tl);
2123 else
2124 uiop->uio_offset =
2125 fxdr_cookie3(tl);
2126 }
2127 else {
2128 uiop->uio_offset =
2129 fxdr_unsigned(off_t, *tl);
2130 }
2131 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2132 }
2133 if (v3)
2134 tl += 2;
2135 else
2136 tl++;
2137 more_dirs = fxdr_unsigned(int, *tl);
2138 }
2139 /*
2140 * If at end of rpc data, get the eof boolean
2141 */
2142 if (!more_dirs) {
2143 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2144 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2145 }
2146 m_freem(mrep);
2147 }
2148 /*
2149 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2150 * by increasing d_reclen for the last record.
2151 */
2152 if (blksiz > 0) {
2153 left = NFS_DIRFRAGSIZ - blksiz;
2154 dp->d_reclen += left;
2155 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2156 (caddr_t)uiop->uio_iov->iov_base += left;
2157 uiop->uio_iov->iov_len -= left;
2158 uiop->uio_resid -= left;
2159 }
2160
2161 /*
2162 * We are now either at the end of the directory or have filled the
2163 * block.
2164 */
2165 if (bigenough)
2166 dnp->n_direofoffset = uiop->uio_offset;
2167 nfsmout:
2168 return (error);
2169 }
2170
2171 /*
2172 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2173 */
2174 int
2175 nfs_readdirplusrpc(vp, uiop, cred)
2176 struct vnode *vp;
2177 register struct uio *uiop;
2178 struct ucred *cred;
2179 {
2180 register int len, left;
2181 register struct dirent *dp = NULL;
2182 register u_int32_t *tl;
2183 register caddr_t cp;
2184 register int32_t t1, t2;
2185 struct vnode *newvp;
2186 caddr_t bpos, dpos, cp2;
2187 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2188 struct nameidata nami, *ndp = &nami;
2189 struct componentname *cnp = &ndp->ni_cnd;
2190 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2191 struct nfsnode *dnp = VTONFS(vp), *np;
2192 const unsigned char *hcp;
2193 nfsfh_t *fhp;
2194 u_quad_t fileno;
2195 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2196 int attrflag, fhsize, nrpcs = 0, reclen;
2197 struct nfs_fattr fattr, *fp;
2198
2199 #ifdef DIAGNOSTIC
2200 if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1)))
2201 panic("nfs readdirplusrpc bad uio");
2202 #endif
2203 ndp->ni_dvp = vp;
2204 newvp = NULLVP;
2205
2206 /*
2207 * Loop around doing readdir rpc's of size nm_readdirsize
2208 * truncated to a multiple of NFS_DIRFRAGSIZ.
2209 * The stopping criteria is EOF or buffer full.
2210 */
2211 while (more_dirs && bigenough) {
2212 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) {
2213 bigenough = 0;
2214 break;
2215 }
2216 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2217 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2218 NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2219 nfsm_fhtom(vp, 1);
2220 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2221 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) {
2222 txdr_swapcookie3(uiop->uio_offset, tl);
2223 } else {
2224 txdr_cookie3(uiop->uio_offset, tl);
2225 }
2226 tl += 2;
2227 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2228 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2229 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2230 *tl = txdr_unsigned(nmp->nm_rsize);
2231 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_procp, cred);
2232 nfsm_postop_attr(vp, attrflag);
2233 if (error) {
2234 m_freem(mrep);
2235 goto nfsmout;
2236 }
2237 nrpcs++;
2238 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2239 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2240 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2241 more_dirs = fxdr_unsigned(int, *tl);
2242
2243 /* loop thru the dir entries, doctoring them to 4bsd form */
2244 while (more_dirs && bigenough) {
2245 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2246 fileno = fxdr_hyper(tl);
2247 len = fxdr_unsigned(int, *(tl + 2));
2248 if (len <= 0 || len > NFS_MAXNAMLEN) {
2249 error = EBADRPC;
2250 m_freem(mrep);
2251 goto nfsmout;
2252 }
2253 tlen = nfsm_rndup(len);
2254 if (tlen == len)
2255 tlen += 4; /* To ensure null termination*/
2256 tlen += sizeof (off_t) + sizeof (int);
2257 reclen = ALIGN(tlen + DIRHDSIZ);
2258 tlen = reclen - DIRHDSIZ;
2259 left = NFS_DIRFRAGSIZ - blksiz;
2260 if (reclen > left) {
2261 /*
2262 * DIRFRAGSIZ is aligned, no need to align
2263 * again here.
2264 */
2265 dp->d_reclen += left;
2266 (caddr_t)uiop->uio_iov->iov_base += left;
2267 uiop->uio_iov->iov_len -= left;
2268 uiop->uio_resid -= left;
2269 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2270 blksiz = 0;
2271 }
2272 if (reclen > uiop->uio_resid)
2273 bigenough = 0;
2274 if (bigenough) {
2275 dp = (struct dirent *)uiop->uio_iov->iov_base;
2276 dp->d_fileno = (int)fileno;
2277 dp->d_namlen = len;
2278 dp->d_reclen = reclen;
2279 dp->d_type = DT_UNKNOWN;
2280 blksiz += dp->d_reclen;
2281 if (blksiz == NFS_DIRFRAGSIZ)
2282 blksiz = 0;
2283 uiop->uio_resid -= DIRHDSIZ;
2284 (caddr_t)uiop->uio_iov->iov_base += DIRHDSIZ;
2285 uiop->uio_iov->iov_len -= DIRHDSIZ;
2286 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2287 cnp->cn_namelen = len;
2288 nfsm_mtouio(uiop, len);
2289 cp = uiop->uio_iov->iov_base;
2290 tlen -= len;
2291 *cp = '\0';
2292 (caddr_t)uiop->uio_iov->iov_base += tlen;
2293 uiop->uio_iov->iov_len -= tlen;
2294 uiop->uio_resid -= tlen;
2295 } else
2296 nfsm_adv(nfsm_rndup(len));
2297 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2298 if (bigenough) {
2299 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE)
2300 uiop->uio_offset =
2301 fxdr_swapcookie3(tl);
2302 else
2303 uiop->uio_offset =
2304 fxdr_cookie3(tl);
2305 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2306 }
2307 tl += 2;
2308
2309 /*
2310 * Since the attributes are before the file handle
2311 * (sigh), we must skip over the attributes and then
2312 * come back and get them.
2313 */
2314 attrflag = fxdr_unsigned(int, *tl);
2315 if (attrflag) {
2316 nfsm_dissect(fp, struct nfs_fattr *, NFSX_V3FATTR);
2317 memcpy(&fattr, fp, NFSX_V3FATTR);
2318 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2319 doit = fxdr_unsigned(int, *tl);
2320 if (doit) {
2321 nfsm_getfh(fhp, fhsize, 1);
2322 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2323 VREF(vp);
2324 newvp = vp;
2325 np = dnp;
2326 } else {
2327 error = nfs_nget(vp->v_mount, fhp,
2328 fhsize, &np);
2329 if (!error)
2330 newvp = NFSTOV(np);
2331 }
2332 if (!error) {
2333 nfs_loadattrcache(&newvp, &fattr, 0);
2334 dp->d_type =
2335 IFTODT(VTTOIF(np->n_vattr->va_type));
2336 ndp->ni_vp = newvp;
2337 cnp->cn_hash = 0;
2338 for (hcp = cnp->cn_nameptr, i = 1; i <= len;
2339 i++, hcp++)
2340 cnp->cn_hash += *hcp * i;
2341 if (cnp->cn_namelen <= NCHNAMLEN)
2342 cache_enter(ndp->ni_dvp, ndp->ni_vp,
2343 cnp);
2344 }
2345 }
2346 } else {
2347 /* Just skip over the file handle */
2348 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2349 i = fxdr_unsigned(int, *tl);
2350 nfsm_adv(nfsm_rndup(i));
2351 }
2352 if (newvp != NULLVP) {
2353 vrele(newvp);
2354 newvp = NULLVP;
2355 }
2356 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2357 more_dirs = fxdr_unsigned(int, *tl);
2358 }
2359 /*
2360 * If at end of rpc data, get the eof boolean
2361 */
2362 if (!more_dirs) {
2363 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2364 more_dirs = (fxdr_unsigned(int, *tl) == 0);
2365 }
2366 m_freem(mrep);
2367 }
2368 /*
2369 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ
2370 * by increasing d_reclen for the last record.
2371 */
2372 if (blksiz > 0) {
2373 left = NFS_DIRFRAGSIZ - blksiz;
2374 dp->d_reclen += left;
2375 NFS_STASHCOOKIE(dp, uiop->uio_offset);
2376 (caddr_t)uiop->uio_iov->iov_base += left;
2377 uiop->uio_iov->iov_len -= left;
2378 uiop->uio_resid -= left;
2379 }
2380
2381 /*
2382 * We are now either at the end of the directory or have filled the
2383 * block.
2384 */
2385 if (bigenough)
2386 dnp->n_direofoffset = uiop->uio_offset;
2387 nfsmout:
2388 if (newvp != NULLVP)
2389 vrele(newvp);
2390 return (error);
2391 }
2392 static char hextoasc[] = "0123456789abcdef";
2393
2394 /*
2395 * Silly rename. To make the NFS filesystem that is stateless look a little
2396 * more like the "ufs" a remove of an active vnode is translated to a rename
2397 * to a funny looking filename that is removed by nfs_inactive on the
2398 * nfsnode. There is the potential for another process on a different client
2399 * to create the same funny name between the nfs_lookitup() fails and the
2400 * nfs_rename() completes, but...
2401 */
2402 int
2403 nfs_sillyrename(dvp, vp, cnp)
2404 struct vnode *dvp, *vp;
2405 struct componentname *cnp;
2406 {
2407 register struct sillyrename *sp;
2408 struct nfsnode *np;
2409 int error;
2410 short pid;
2411
2412 cache_purge(dvp);
2413 np = VTONFS(vp);
2414 #ifndef DIAGNOSTIC
2415 if (vp->v_type == VDIR)
2416 panic("nfs: sillyrename dir");
2417 #endif
2418 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2419 M_NFSREQ, M_WAITOK);
2420 sp->s_cred = crdup(cnp->cn_cred);
2421 sp->s_dvp = dvp;
2422 VREF(dvp);
2423
2424 /* Fudge together a funny name */
2425 pid = cnp->cn_proc->p_pid;
2426 memcpy(sp->s_name, ".nfsAxxxx4.4", 13);
2427 sp->s_namlen = 12;
2428 sp->s_name[8] = hextoasc[pid & 0xf];
2429 sp->s_name[7] = hextoasc[(pid >> 4) & 0xf];
2430 sp->s_name[6] = hextoasc[(pid >> 8) & 0xf];
2431 sp->s_name[5] = hextoasc[(pid >> 12) & 0xf];
2432
2433 /* Try lookitups until we get one that isn't there */
2434 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2435 cnp->cn_proc, (struct nfsnode **)0) == 0) {
2436 sp->s_name[4]++;
2437 if (sp->s_name[4] > 'z') {
2438 error = EINVAL;
2439 goto bad;
2440 }
2441 }
2442 error = nfs_renameit(dvp, cnp, sp);
2443 if (error)
2444 goto bad;
2445 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2446 cnp->cn_proc, &np);
2447 np->n_sillyrename = sp;
2448 return (0);
2449 bad:
2450 vrele(sp->s_dvp);
2451 crfree(sp->s_cred);
2452 free((caddr_t)sp, M_NFSREQ);
2453 return (error);
2454 }
2455
2456 /*
2457 * Look up a file name and optionally either update the file handle or
2458 * allocate an nfsnode, depending on the value of npp.
2459 * npp == NULL --> just do the lookup
2460 * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2461 * handled too
2462 * *npp != NULL --> update the file handle in the vnode
2463 */
2464 int
2465 nfs_lookitup(dvp, name, len, cred, procp, npp)
2466 register struct vnode *dvp;
2467 const char *name;
2468 int len;
2469 struct ucred *cred;
2470 struct proc *procp;
2471 struct nfsnode **npp;
2472 {
2473 register u_int32_t *tl;
2474 register caddr_t cp;
2475 register int32_t t1, t2;
2476 struct vnode *newvp = (struct vnode *)0;
2477 struct nfsnode *np, *dnp = VTONFS(dvp);
2478 caddr_t bpos, dpos, cp2;
2479 int error = 0, fhlen, attrflag;
2480 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2481 nfsfh_t *nfhp;
2482 int v3 = NFS_ISV3(dvp);
2483
2484 nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2485 nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2486 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2487 nfsm_fhtom(dvp, v3);
2488 nfsm_strtom(name, len, NFS_MAXNAMLEN);
2489 nfsm_request(dvp, NFSPROC_LOOKUP, procp, cred);
2490 if (npp && !error) {
2491 nfsm_getfh(nfhp, fhlen, v3);
2492 if (*npp) {
2493 np = *npp;
2494 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2495 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2496 np->n_fhp = &np->n_fh;
2497 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2498 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2499 memcpy((caddr_t)np->n_fhp, (caddr_t)nfhp, fhlen);
2500 np->n_fhsize = fhlen;
2501 newvp = NFSTOV(np);
2502 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2503 VREF(dvp);
2504 newvp = dvp;
2505 } else {
2506 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2507 if (error) {
2508 m_freem(mrep);
2509 return (error);
2510 }
2511 newvp = NFSTOV(np);
2512 }
2513 if (v3) {
2514 nfsm_postop_attr(newvp, attrflag);
2515 if (!attrflag && *npp == NULL) {
2516 m_freem(mrep);
2517 vrele(newvp);
2518 return (ENOENT);
2519 }
2520 } else
2521 nfsm_loadattr(newvp, (struct vattr *)0);
2522 }
2523 nfsm_reqdone;
2524 if (npp && *npp == NULL) {
2525 if (error) {
2526 if (newvp)
2527 vrele(newvp);
2528 } else
2529 *npp = np;
2530 }
2531 return (error);
2532 }
2533
2534 /*
2535 * Nfs Version 3 commit rpc
2536 */
2537 int
2538 nfs_commit(vp, offset, cnt, cred, procp)
2539 register struct vnode *vp;
2540 u_quad_t offset;
2541 int cnt;
2542 struct ucred *cred;
2543 struct proc *procp;
2544 {
2545 register caddr_t cp;
2546 register u_int32_t *tl;
2547 register int32_t t1, t2;
2548 register struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2549 caddr_t bpos, dpos, cp2;
2550 int error = 0, wccflag = NFSV3_WCCRATTR;
2551 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2552
2553 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0)
2554 return (0);
2555 nfsstats.rpccnt[NFSPROC_COMMIT]++;
2556 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2557 nfsm_fhtom(vp, 1);
2558 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2559 txdr_hyper(offset, tl);
2560 tl += 2;
2561 *tl = txdr_unsigned(cnt);
2562 nfsm_request(vp, NFSPROC_COMMIT, procp, cred);
2563 nfsm_wcc_data(vp, wccflag);
2564 if (!error) {
2565 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2566 if (memcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2567 NFSX_V3WRITEVERF)) {
2568 memcpy((caddr_t)nmp->nm_verf, (caddr_t)tl,
2569 NFSX_V3WRITEVERF);
2570 error = NFSERR_STALEWRITEVERF;
2571 }
2572 }
2573 nfsm_reqdone;
2574 return (error);
2575 }
2576
2577 /*
2578 * Kludge City..
2579 * - make nfs_bmap() essentially a no-op that does no translation
2580 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2581 * (Maybe I could use the process's page mapping, but I was concerned that
2582 * Kernel Write might not be enabled and also figured copyout() would do
2583 * a lot more work than memcpy() and also it currently happens in the
2584 * context of the swapper process (2).
2585 */
2586 int
2587 nfs_bmap(v)
2588 void *v;
2589 {
2590 struct vop_bmap_args /* {
2591 struct vnode *a_vp;
2592 daddr_t a_bn;
2593 struct vnode **a_vpp;
2594 daddr_t *a_bnp;
2595 int *a_runp;
2596 } */ *ap = v;
2597 register struct vnode *vp = ap->a_vp;
2598
2599 if (ap->a_vpp != NULL)
2600 *ap->a_vpp = vp;
2601 if (ap->a_bnp != NULL)
2602 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2603 return (0);
2604 }
2605
2606 /*
2607 * Strategy routine.
2608 * For async requests when nfsiod(s) are running, queue the request by
2609 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2610 * request.
2611 */
2612 int
2613 nfs_strategy(v)
2614 void *v;
2615 {
2616 struct vop_strategy_args *ap = v;
2617 register struct buf *bp = ap->a_bp;
2618 struct ucred *cr;
2619 struct proc *p;
2620 int error = 0;
2621
2622 if ((bp->b_flags & (B_PHYS|B_ASYNC)) == (B_PHYS|B_ASYNC))
2623 panic("nfs physio/async");
2624 if (bp->b_flags & B_ASYNC)
2625 p = (struct proc *)0;
2626 else
2627 p = curproc; /* XXX */
2628 if (bp->b_flags & B_READ)
2629 cr = bp->b_rcred;
2630 else
2631 cr = bp->b_wcred;
2632 /*
2633 * If the op is asynchronous and an i/o daemon is waiting
2634 * queue the request, wake it up and wait for completion
2635 * otherwise just do it ourselves.
2636 */
2637 if ((bp->b_flags & B_ASYNC) == 0 ||
2638 nfs_asyncio(bp, NOCRED))
2639 error = nfs_doio(bp, cr, p);
2640 return (error);
2641 }
2642
2643 /*
2644 * Mmap a file
2645 *
2646 * NB Currently unsupported.
2647 */
2648 /* ARGSUSED */
2649 int
2650 nfs_mmap(v)
2651 void *v;
2652 {
2653 #if 0
2654 struct vop_mmap_args /* {
2655 struct vnode *a_vp;
2656 int a_fflags;
2657 struct ucred *a_cred;
2658 struct proc *a_p;
2659 } */ *ap = v;
2660 #endif
2661
2662 return (EINVAL);
2663 }
2664
2665 /*
2666 * fsync vnode op. Just call nfs_flush() with commit == 1.
2667 */
2668 /* ARGSUSED */
2669 int
2670 nfs_fsync(v)
2671 void *v;
2672 {
2673 struct vop_fsync_args /* {
2674 struct vnodeop_desc *a_desc;
2675 struct vnode * a_vp;
2676 struct ucred * a_cred;
2677 int a_flags;
2678 struct proc * a_p;
2679 } */ *ap = v;
2680
2681 return (nfs_flush(ap->a_vp, ap->a_cred,
2682 (ap->a_flags & FSYNC_WAIT) != 0 ? MNT_WAIT : 0, ap->a_p, 1));
2683 }
2684
2685 /*
2686 * Flush all the blocks associated with a vnode.
2687 * Walk through the buffer pool and push any dirty pages
2688 * associated with the vnode.
2689 */
2690 int
2691 nfs_flush(vp, cred, waitfor, p, commit)
2692 register struct vnode *vp;
2693 struct ucred *cred;
2694 int waitfor;
2695 struct proc *p;
2696 int commit;
2697 {
2698 register struct nfsnode *np = VTONFS(vp);
2699 register struct buf *bp;
2700 register int i;
2701 struct buf *nbp;
2702 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2703 int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2704 int passone = 1;
2705 u_quad_t off = (u_quad_t)-1, endoff = 0, toff;
2706 struct ucred* wcred = NULL;
2707 #ifndef NFS_COMMITBVECSIZ
2708 #define NFS_COMMITBVECSIZ 20
2709 #endif
2710 struct buf *bvec[NFS_COMMITBVECSIZ];
2711
2712 if (nmp->nm_flag & NFSMNT_INT)
2713 slpflag = PCATCH;
2714 if (!commit)
2715 passone = 0;
2716 /*
2717 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2718 * server, but nas not been committed to stable storage on the server
2719 * yet. On the first pass, the byte range is worked out and the commit
2720 * rpc is done. On the second pass, nfs_writebp() is called to do the
2721 * job.
2722 */
2723 again:
2724 bvecpos = 0;
2725 if (NFS_ISV3(vp) && commit) {
2726 s = splbio();
2727 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
2728 nbp = bp->b_vnbufs.le_next;
2729 if (bvecpos >= NFS_COMMITBVECSIZ)
2730 break;
2731 if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
2732 != (B_DELWRI | B_NEEDCOMMIT))
2733 continue;
2734 bremfree(bp);
2735 /*
2736 * Work out if all buffers are using the same cred
2737 * so we can deal with them all with one commit.
2738 */
2739 if (wcred == NULL)
2740 wcred = bp->b_wcred;
2741 else if (wcred != bp->b_wcred)
2742 wcred = NOCRED;
2743 bp->b_flags |= (B_BUSY | B_WRITEINPROG);
2744 /*
2745 * A list of these buffers is kept so that the
2746 * second loop knows which buffers have actually
2747 * been committed. This is necessary, since there
2748 * may be a race between the commit rpc and new
2749 * uncommitted writes on the file.
2750 */
2751 bvec[bvecpos++] = bp;
2752 toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2753 bp->b_dirtyoff;
2754 if (toff < off)
2755 off = toff;
2756 toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2757 if (toff > endoff)
2758 endoff = toff;
2759 }
2760 splx(s);
2761 }
2762 if (bvecpos > 0) {
2763 /*
2764 * Commit data on the server, as required.
2765 * If all bufs are using the same wcred, then use that with
2766 * one call for all of them, otherwise commit each one
2767 * separately.
2768 */
2769 if (wcred != NOCRED)
2770 retv = nfs_commit(vp, off, (int)(endoff - off),
2771 wcred, p);
2772 else {
2773 retv = 0;
2774 for (i = 0; i < bvecpos; i++) {
2775 off_t off, size;
2776 bp = bvec[i];
2777 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2778 bp->b_dirtyoff;
2779 size = (u_quad_t)(bp->b_dirtyend
2780 - bp->b_dirtyoff);
2781 retv = nfs_commit(vp, off, (int)size,
2782 bp->b_wcred, p);
2783 if (retv) break;
2784 }
2785 }
2786
2787 if (retv == NFSERR_STALEWRITEVERF)
2788 nfs_clearcommit(vp->v_mount);
2789 /*
2790 * Now, either mark the blocks I/O done or mark the
2791 * blocks dirty, depending on whether the commit
2792 * succeeded.
2793 */
2794 for (i = 0; i < bvecpos; i++) {
2795 bp = bvec[i];
2796 bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG);
2797 if (retv)
2798 brelse(bp);
2799 else {
2800 vp->v_numoutput++;
2801 bp->b_flags |= B_ASYNC;
2802 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR|B_DELWRI);
2803 bp->b_dirtyoff = bp->b_dirtyend = 0;
2804 reassignbuf(bp, vp);
2805 biodone(bp);
2806 }
2807 }
2808 }
2809
2810 /*
2811 * Start/do any write(s) that are required.
2812 */
2813 loop:
2814 s = splbio();
2815 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
2816 nbp = bp->b_vnbufs.le_next;
2817 if (bp->b_flags & B_BUSY) {
2818 if (waitfor != MNT_WAIT || passone)
2819 continue;
2820 bp->b_flags |= B_WANTED;
2821 error = tsleep((caddr_t)bp, slpflag | (PRIBIO + 1),
2822 "nfsfsync", slptimeo);
2823 splx(s);
2824 if (error) {
2825 if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
2826 return (EINTR);
2827 if (slpflag == PCATCH) {
2828 slpflag = 0;
2829 slptimeo = 2 * hz;
2830 }
2831 }
2832 goto loop;
2833 }
2834 if ((bp->b_flags & B_DELWRI) == 0)
2835 panic("nfs_fsync: not dirty");
2836 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT))
2837 continue;
2838 bremfree(bp);
2839 if (passone || !commit)
2840 bp->b_flags |= (B_BUSY|B_ASYNC);
2841 else
2842 bp->b_flags |= (B_BUSY|B_ASYNC|B_WRITEINPROG|B_NEEDCOMMIT);
2843 splx(s);
2844 VOP_BWRITE(bp);
2845 goto loop;
2846 }
2847 splx(s);
2848 if (passone) {
2849 passone = 0;
2850 goto again;
2851 }
2852 if (waitfor == MNT_WAIT) {
2853 while (vp->v_numoutput) {
2854 vp->v_flag |= VBWAIT;
2855 error = tsleep((caddr_t)&vp->v_numoutput,
2856 slpflag | (PRIBIO + 1), "nfsfsync", slptimeo);
2857 if (error) {
2858 if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
2859 return (EINTR);
2860 if (slpflag == PCATCH) {
2861 slpflag = 0;
2862 slptimeo = 2 * hz;
2863 }
2864 }
2865 }
2866 if (vp->v_dirtyblkhd.lh_first && commit) {
2867 #if 0
2868 vprint("nfs_fsync: dirty", vp);
2869 #endif
2870 goto loop;
2871 }
2872 }
2873 if (np->n_flag & NWRITEERR) {
2874 error = np->n_error;
2875 np->n_flag &= ~NWRITEERR;
2876 }
2877 return (error);
2878 }
2879
2880 /*
2881 * Return POSIX pathconf information applicable to nfs.
2882 *
2883 * N.B. The NFS V2 protocol doesn't support this RPC.
2884 */
2885 /* ARGSUSED */
2886 int
2887 nfs_pathconf(v)
2888 void *v;
2889 {
2890 struct vop_pathconf_args /* {
2891 struct vnode *a_vp;
2892 int a_name;
2893 register_t *a_retval;
2894 } */ *ap = v;
2895 struct nfsv3_pathconf *pcp;
2896 struct vnode *vp = ap->a_vp;
2897 struct nfsmount *nmp;
2898 struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2899 int32_t t1, t2;
2900 u_int32_t *tl;
2901 caddr_t bpos, dpos, cp, cp2;
2902 int error = 0, attrflag;
2903 unsigned int l;
2904 u_int64_t maxsize;
2905 int v3 = NFS_ISV3(vp);
2906
2907 switch (ap->a_name) {
2908 /* Names that can be resolved locally. */
2909 case _PC_PIPE_BUF:
2910 *ap->a_retval = PIPE_BUF;
2911 break;
2912 case _PC_SYNC_IO:
2913 *ap->a_retval = 1;
2914 break;
2915 /* Names that cannot be resolved locally; do an RPC, if possible. */
2916 case _PC_LINK_MAX:
2917 case _PC_NAME_MAX:
2918 case _PC_CHOWN_RESTRICTED:
2919 case _PC_NO_TRUNC:
2920 if (!v3) {
2921 error = EINVAL;
2922 break;
2923 }
2924 nfsstats.rpccnt[NFSPROC_PATHCONF]++;
2925 nfsm_reqhead(vp, NFSPROC_PATHCONF, NFSX_FH(1));
2926 nfsm_fhtom(vp, 1);
2927 nfsm_request(vp, NFSPROC_PATHCONF,
2928 curproc, curproc->p_ucred); /* XXX */
2929 nfsm_postop_attr(vp, attrflag);
2930 if (!error) {
2931 nfsm_dissect(pcp, struct nfsv3_pathconf *,
2932 NFSX_V3PATHCONF);
2933 switch (ap->a_name) {
2934 case _PC_LINK_MAX:
2935 *ap->a_retval =
2936 fxdr_unsigned(register_t, pcp->pc_linkmax);
2937 break;
2938 case _PC_NAME_MAX:
2939 *ap->a_retval =
2940 fxdr_unsigned(register_t, pcp->pc_namemax);
2941 break;
2942 case _PC_CHOWN_RESTRICTED:
2943 *ap->a_retval =
2944 (pcp->pc_chownrestricted == nfs_true);
2945 break;
2946 case _PC_NO_TRUNC:
2947 *ap->a_retval =
2948 (pcp->pc_notrunc == nfs_true);
2949 break;
2950 }
2951 }
2952 nfsm_reqdone;
2953 break;
2954 case _PC_FILESIZEBITS:
2955 if (v3) {
2956 nmp = VFSTONFS(vp->v_mount);
2957 if ((nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0)
2958 if ((error = nfs_fsinfo(nmp, vp,
2959 curproc->p_ucred, curproc)) != 0) /* XXX */
2960 break;
2961 for (l = 0, maxsize = nmp->nm_maxfilesize;
2962 (maxsize >> l) > 0; l++)
2963 ;
2964 *ap->a_retval = l + 1;
2965 } else {
2966 *ap->a_retval = 32; /* NFS V2 limitation */
2967 }
2968 break;
2969 default:
2970 error = EINVAL;
2971 break;
2972 }
2973
2974 return (error);
2975 }
2976
2977 /*
2978 * NFS advisory byte-level locks.
2979 */
2980 int
2981 nfs_advlock(v)
2982 void *v;
2983 {
2984 struct vop_advlock_args /* {
2985 struct vnode *a_vp;
2986 caddr_t a_id;
2987 int a_op;
2988 struct flock *a_fl;
2989 int a_flags;
2990 } */ *ap = v;
2991 register struct nfsnode *np = VTONFS(ap->a_vp);
2992
2993 return (lf_advlock(&np->n_lockf, np->n_size, ap->a_id, ap->a_op,
2994 ap->a_fl, ap->a_flags));
2995 }
2996
2997 /*
2998 * Print out the contents of an nfsnode.
2999 */
3000 int
3001 nfs_print(v)
3002 void *v;
3003 {
3004 struct vop_print_args /* {
3005 struct vnode *a_vp;
3006 } */ *ap = v;
3007 register struct vnode *vp = ap->a_vp;
3008 register struct nfsnode *np = VTONFS(vp);
3009
3010 printf("tag VT_NFS, fileid %ld fsid 0x%lx",
3011 np->n_vattr->va_fileid, np->n_vattr->va_fsid);
3012 if (vp->v_type == VFIFO)
3013 fifo_printinfo(vp);
3014 printf("\n");
3015 return (0);
3016 }
3017
3018 /*
3019 * NFS file truncation.
3020 */
3021 int
3022 nfs_truncate(v)
3023 void *v;
3024 {
3025 #if 0
3026 struct vop_truncate_args /* {
3027 struct vnode *a_vp;
3028 off_t a_length;
3029 int a_flags;
3030 struct ucred *a_cred;
3031 struct proc *a_p;
3032 } */ *ap = v;
3033 #endif
3034
3035 /* Use nfs_setattr */
3036 return (EOPNOTSUPP);
3037 }
3038
3039 /*
3040 * NFS update.
3041 */
3042 int
3043 nfs_update(v)
3044 void *v;
3045 #if 0
3046 struct vop_update_args /* {
3047 struct vnode *a_vp;
3048 struct timespec *a_ta;
3049 struct timespec *a_tm;
3050 int a_waitfor;
3051 } */ *ap = v;
3052 #endif
3053 {
3054
3055 /* Use nfs_setattr */
3056 return (EOPNOTSUPP);
3057 }
3058
3059 /*
3060 * Just call nfs_writebp() with the force argument set to 1.
3061 */
3062 int
3063 nfs_bwrite(v)
3064 void *v;
3065 {
3066 struct vop_bwrite_args /* {
3067 struct vnode *a_bp;
3068 } */ *ap = v;
3069
3070 return (nfs_writebp(ap->a_bp, 1));
3071 }
3072
3073 /*
3074 * This is a clone of vn_bwrite(), except that B_WRITEINPROG isn't set unless
3075 * the force flag is one and it also handles the B_NEEDCOMMIT flag.
3076 */
3077 int
3078 nfs_writebp(bp, force)
3079 register struct buf *bp;
3080 int force;
3081 {
3082 register int oldflags = bp->b_flags, retv = 1;
3083 register struct proc *p = curproc; /* XXX */
3084 off_t off;
3085
3086 if(!(bp->b_flags & B_BUSY))
3087 panic("bwrite: buffer is not busy???");
3088
3089 #ifdef fvdl_debug
3090 printf("nfs_writebp(%x): vp %x voff %d vend %d doff %d dend %d\n",
3091 bp, bp->b_vp, bp->b_validoff, bp->b_validend, bp->b_dirtyoff,
3092 bp->b_dirtyend);
3093 #endif
3094 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR|B_DELWRI|B_AGE);
3095
3096 if (oldflags & B_ASYNC) {
3097 if (oldflags & B_DELWRI) {
3098 reassignbuf(bp, bp->b_vp);
3099 } else if (p) {
3100 ++p->p_stats->p_ru.ru_oublock;
3101 }
3102 }
3103 bp->b_vp->v_numoutput++;
3104
3105 /*
3106 * If B_NEEDCOMMIT is set, a commit rpc may do the trick. If not
3107 * an actual write will have to be scheduled via. VOP_STRATEGY().
3108 * If B_WRITEINPROG is already set, then push it with a write anyhow.
3109 */
3110 if ((oldflags & (B_NEEDCOMMIT | B_WRITEINPROG)) == B_NEEDCOMMIT) {
3111 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
3112 bp->b_flags |= B_WRITEINPROG;
3113 retv = nfs_commit(bp->b_vp, off, bp->b_dirtyend-bp->b_dirtyoff,
3114 bp->b_wcred, bp->b_proc);
3115 bp->b_flags &= ~B_WRITEINPROG;
3116 if (!retv) {
3117 bp->b_dirtyoff = bp->b_dirtyend = 0;
3118 bp->b_flags &= ~B_NEEDCOMMIT;
3119 biodone(bp);
3120 } else if (retv == NFSERR_STALEWRITEVERF)
3121 nfs_clearcommit(bp->b_vp->v_mount);
3122 }
3123 if (retv) {
3124 if (force)
3125 bp->b_flags |= B_WRITEINPROG;
3126 VOP_STRATEGY(bp);
3127 }
3128
3129 if( (oldflags & B_ASYNC) == 0) {
3130 int rtval = biowait(bp);
3131 if (oldflags & B_DELWRI) {
3132 reassignbuf(bp, bp->b_vp);
3133 } else if (p) {
3134 ++p->p_stats->p_ru.ru_oublock;
3135 }
3136 brelse(bp);
3137 return (rtval);
3138 }
3139
3140 return (0);
3141 }
3142
3143 /*
3144 * nfs special file access vnode op.
3145 * Essentially just get vattr and then imitate iaccess() since the device is
3146 * local to the client.
3147 */
3148 int
3149 nfsspec_access(v)
3150 void *v;
3151 {
3152 struct vop_access_args /* {
3153 struct vnode *a_vp;
3154 int a_mode;
3155 struct ucred *a_cred;
3156 struct proc *a_p;
3157 } */ *ap = v;
3158 struct vattr va;
3159 struct vnode *vp = ap->a_vp;
3160 int error;
3161
3162 error = VOP_GETATTR(vp, &va, ap->a_cred, ap->a_p);
3163 if (error)
3164 return (error);
3165
3166 /*
3167 * Disallow write attempts on filesystems mounted read-only;
3168 * unless the file is a socket, fifo, or a block or character
3169 * device resident on the filesystem.
3170 */
3171 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3172 switch (vp->v_type) {
3173 case VREG:
3174 case VDIR:
3175 case VLNK:
3176 return (EROFS);
3177 default:
3178 break;
3179 }
3180 }
3181
3182 return (vaccess(va.va_type, va.va_mode,
3183 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
3184 }
3185
3186 /*
3187 * Read wrapper for special devices.
3188 */
3189 int
3190 nfsspec_read(v)
3191 void *v;
3192 {
3193 struct vop_read_args /* {
3194 struct vnode *a_vp;
3195 struct uio *a_uio;
3196 int a_ioflag;
3197 struct ucred *a_cred;
3198 } */ *ap = v;
3199 register struct nfsnode *np = VTONFS(ap->a_vp);
3200
3201 /*
3202 * Set access flag.
3203 */
3204 np->n_flag |= NACC;
3205 np->n_atim.tv_sec = time.tv_sec;
3206 np->n_atim.tv_nsec = time.tv_usec * 1000;
3207 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3208 }
3209
3210 /*
3211 * Write wrapper for special devices.
3212 */
3213 int
3214 nfsspec_write(v)
3215 void *v;
3216 {
3217 struct vop_write_args /* {
3218 struct vnode *a_vp;
3219 struct uio *a_uio;
3220 int a_ioflag;
3221 struct ucred *a_cred;
3222 } */ *ap = v;
3223 register struct nfsnode *np = VTONFS(ap->a_vp);
3224
3225 /*
3226 * Set update flag.
3227 */
3228 np->n_flag |= NUPD;
3229 np->n_mtim.tv_sec = time.tv_sec;
3230 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3231 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3232 }
3233
3234 /*
3235 * Close wrapper for special devices.
3236 *
3237 * Update the times on the nfsnode then do device close.
3238 */
3239 int
3240 nfsspec_close(v)
3241 void *v;
3242 {
3243 struct vop_close_args /* {
3244 struct vnode *a_vp;
3245 int a_fflag;
3246 struct ucred *a_cred;
3247 struct proc *a_p;
3248 } */ *ap = v;
3249 register struct vnode *vp = ap->a_vp;
3250 register struct nfsnode *np = VTONFS(vp);
3251 struct vattr vattr;
3252
3253 if (np->n_flag & (NACC | NUPD)) {
3254 np->n_flag |= NCHG;
3255 if (vp->v_usecount == 1 &&
3256 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3257 VATTR_NULL(&vattr);
3258 if (np->n_flag & NACC)
3259 vattr.va_atime = np->n_atim;
3260 if (np->n_flag & NUPD)
3261 vattr.va_mtime = np->n_mtim;
3262 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
3263 }
3264 }
3265 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3266 }
3267
3268 /*
3269 * Read wrapper for fifos.
3270 */
3271 int
3272 nfsfifo_read(v)
3273 void *v;
3274 {
3275 struct vop_read_args /* {
3276 struct vnode *a_vp;
3277 struct uio *a_uio;
3278 int a_ioflag;
3279 struct ucred *a_cred;
3280 } */ *ap = v;
3281 extern int (**fifo_vnodeop_p) __P((void *));
3282 register struct nfsnode *np = VTONFS(ap->a_vp);
3283
3284 /*
3285 * Set access flag.
3286 */
3287 np->n_flag |= NACC;
3288 np->n_atim.tv_sec = time.tv_sec;
3289 np->n_atim.tv_nsec = time.tv_usec * 1000;
3290 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3291 }
3292
3293 /*
3294 * Write wrapper for fifos.
3295 */
3296 int
3297 nfsfifo_write(v)
3298 void *v;
3299 {
3300 struct vop_write_args /* {
3301 struct vnode *a_vp;
3302 struct uio *a_uio;
3303 int a_ioflag;
3304 struct ucred *a_cred;
3305 } */ *ap = v;
3306 extern int (**fifo_vnodeop_p) __P((void *));
3307 register struct nfsnode *np = VTONFS(ap->a_vp);
3308
3309 /*
3310 * Set update flag.
3311 */
3312 np->n_flag |= NUPD;
3313 np->n_mtim.tv_sec = time.tv_sec;
3314 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3315 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3316 }
3317
3318 /*
3319 * Close wrapper for fifos.
3320 *
3321 * Update the times on the nfsnode then do fifo close.
3322 */
3323 int
3324 nfsfifo_close(v)
3325 void *v;
3326 {
3327 struct vop_close_args /* {
3328 struct vnode *a_vp;
3329 int a_fflag;
3330 struct ucred *a_cred;
3331 struct proc *a_p;
3332 } */ *ap = v;
3333 register struct vnode *vp = ap->a_vp;
3334 register struct nfsnode *np = VTONFS(vp);
3335 struct vattr vattr;
3336 extern int (**fifo_vnodeop_p) __P((void *));
3337
3338 if (np->n_flag & (NACC | NUPD)) {
3339 if (np->n_flag & NACC) {
3340 np->n_atim.tv_sec = time.tv_sec;
3341 np->n_atim.tv_nsec = time.tv_usec * 1000;
3342 }
3343 if (np->n_flag & NUPD) {
3344 np->n_mtim.tv_sec = time.tv_sec;
3345 np->n_mtim.tv_nsec = time.tv_usec * 1000;
3346 }
3347 np->n_flag |= NCHG;
3348 if (vp->v_usecount == 1 &&
3349 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3350 VATTR_NULL(&vattr);
3351 if (np->n_flag & NACC)
3352 vattr.va_atime = np->n_atim;
3353 if (np->n_flag & NUPD)
3354 vattr.va_mtime = np->n_mtim;
3355 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p);
3356 }
3357 }
3358 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
3359 }
3360