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