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