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