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