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