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