chfs_vnops.c revision 1.1 1 /* $NetBSD: chfs_vnops.c,v 1.1 2011/11/24 15:51:32 ahoka Exp $ */
2
3 /*-
4 * Copyright (c) 2010 Department of Software Engineering,
5 * University of Szeged, Hungary
6 * Copyright (C) 2010 Tamas Toth <ttoth (at) inf.u-szeged.hu>
7 * Copyright (C) 2010 Adam Hoka <ahoka (at) NetBSD.org>
8 * All rights reserved.
9 *
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by the Department of Software Engineering, University of Szeged, Hungary
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/param.h>
36 #include <miscfs/specfs/specdev.h>
37 #include <miscfs/fifofs/fifo.h>
38 #include <miscfs/genfs/genfs.h>
39 #include <ufs/ufs/dir.h>
40 #include <ufs/ufs/ufs_extern.h>
41 #include <uvm/uvm.h>
42 #include <sys/namei.h>
43 #include <sys/stat.h>
44 #include <sys/fcntl.h>
45 #include <sys/buf.h>
46 #include <sys/fstrans.h>
47 #include <sys/vnode.h>
48
49 #include "chfs.h"
50
51 #define READ_S "chfs_read"
52
53 int
54 chfs_lookup(void *v)
55 {
56 struct vnode *dvp = ((struct vop_lookup_args *) v)->a_dvp;
57 struct vnode **vpp = ((struct vop_lookup_args *) v)->a_vpp;
58 struct componentname *cnp = ((struct vop_lookup_args *) v)->a_cnp;
59
60 int error;
61 struct chfs_inode* ip;
62 struct ufsmount* ump;
63 struct chfs_mount* chmp;
64 struct chfs_vnode_cache* chvc;
65 struct chfs_dirent* fd;
66
67 dbg("lookup(): %s\n", cnp->cn_nameptr);
68
69 KASSERT(VOP_ISLOCKED(dvp));
70
71 *vpp = NULL;
72
73 // Check accessibility of requested node as a first step.
74 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
75 if (error != 0) {
76 goto out;
77 }
78
79 // If requesting the last path component on a read-only file system
80 // with a write operation, deny it.
81 if ((cnp->cn_flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY)
82 && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
83 error = EROFS;
84 goto out;
85 }
86
87 // Avoid doing a linear scan of the directory if the requested
88 // directory/name couple is already in the cache.
89 error = cache_lookup(dvp, vpp, cnp);
90 if (error >= 0) {
91 goto out;
92 }
93
94 ip = VTOI(dvp);
95 ump = VFSTOUFS(dvp->v_mount);
96 chmp = ump->um_chfs;
97 if (ip->ino == 0) {
98 ip->ino = ++chmp->chm_max_vno;
99 }
100 mutex_enter(&chmp->chm_lock_vnocache);
101 chvc = chfs_vnode_cache_get(chmp, ip->ino);
102 mutex_exit(&chmp->chm_lock_vnocache);
103
104 // We cannot be requesting the parent directory of the root node.
105 KASSERT(IMPLIES(dvp->v_type == VDIR && chvc->pvno == chvc->vno,
106 !(cnp->cn_flags & ISDOTDOT)));
107
108 if (cnp->cn_flags & ISDOTDOT) {
109 VOP_UNLOCK(dvp);
110 error = VFS_VGET(dvp->v_mount, ip->chvc->pvno, vpp);
111 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
112 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
113 vref(dvp);
114 *vpp = dvp;
115 error = 0;
116 } else {
117 fd = chfs_dir_lookup(ip, cnp);
118
119 if (fd == NULL) {
120 dbg("fd null\n");
121 // The entry was not found in the directory.
122 // This is OK if we are creating or renaming an
123 // entry and are working on the last component of
124 // the path name.
125 if ((cnp->cn_flags & ISLASTCN) && (cnp->cn_nameiop == CREATE
126 || cnp->cn_nameiop == RENAME)) {
127 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
128 if (error) {
129 dbg("after the entry was not found in dir\n");
130 goto out;
131 }
132
133 dbg("return EJUSTRETURN\n");
134 error = EJUSTRETURN;
135 } else {
136 error = ENOENT;
137 }
138 } else {
139 // If we are not at the last path component and
140 // found a non-directory or non-link entry (which
141 // may itself be pointing to a directory), raise
142 // an error.
143 if ((fd->type != VDIR && fd->type != VLNK) && !(cnp->cn_flags
144 & ISLASTCN)) {
145 error = ENOTDIR;
146 goto out;
147 }
148
149 dbg("vno@allocating new vnode: %llu\n", fd->vno);
150 error = VFS_VGET(dvp->v_mount, fd->vno, vpp);
151 }
152 }
153 // Store the result of this lookup in the cache. Avoid this if the
154 // request was for creation, as it does not improve timings on
155 // emprical tests.
156 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE
157 && (cnp->cn_flags & ISDOTDOT) == 0)
158 cache_enter(dvp, *vpp, cnp);
159
160 out:
161 // If there were no errors, *vpp cannot be null and it must be
162 // locked.
163 KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp)));
164
165 // dvp must always be locked.
166 KASSERT(VOP_ISLOCKED(dvp));
167
168 return error;
169 }
170
171 /* --------------------------------------------------------------------- */
172
173 int
174 chfs_create(void *v)
175 {
176 struct vop_create_args /* {
177 struct vnode *a_dvp;
178 struct vnode **a_vpp;
179 struct componentname *a_cnp;
180 struct vattr *a_vap;
181 } */*ap = v;
182 int error, mode;
183 dbg("create()\n");
184
185 mode = MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode);
186
187 if ((mode & IFMT) == 0) {
188 if (ap->a_vap->va_type == VREG)
189 mode |= IFREG;
190 if (ap->a_vap->va_type == VSOCK)
191 mode |= IFSOCK;
192 }
193
194 error = chfs_makeinode(mode, ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap->va_type);
195
196 if (error) {
197 dbg("error: %d\n", error);
198 return error;
199 }
200
201 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
202 return 0;
203 }
204 /* --------------------------------------------------------------------- */
205
206 int
207 chfs_mknod(void *v)
208 {
209 struct vnode *dvp = ((struct vop_mknod_args *) v)->a_dvp;
210 struct vnode **vpp = ((struct vop_mknod_args *) v)->a_vpp;
211 struct componentname *cnp = ((struct vop_mknod_args *) v)->a_cnp;
212 struct vattr *vap = ((struct vop_mknod_args *) v)->a_vap;
213 int mode, err = 0;
214 struct chfs_inode *ip;
215 struct vnode *vp;
216
217 struct ufsmount *ump;
218 struct chfs_mount *chmp;
219 ino_t ino;
220
221 struct chfs_full_dnode *fd;
222 struct buf *bp;
223 int len;
224 dbg("mknod()\n");
225
226 ump = VFSTOUFS(dvp->v_mount);
227 chmp = ump->um_chfs;
228
229 if (vap->va_type != VBLK && vap->va_type != VCHR && vap->va_type != VFIFO)
230 return EINVAL;
231
232 vp = *vpp;
233
234 mode = MAKEIMODE(vap->va_type, vap->va_mode);
235
236 if ((mode & IFMT) == 0) {
237 switch (vap->va_type) {
238 case VBLK:
239 mode |= IFBLK;
240 break;
241 case VCHR:
242 mode |= IFCHR;
243 break;
244 case VFIFO:
245 mode |= IFIFO;
246 break;
247 default:
248 break;
249 }
250 }
251
252 err = chfs_makeinode(mode, dvp, &vp, cnp, vap->va_type);
253
254 ip = VTOI(vp);
255 ino = ip->ino;
256 if (vap->va_rdev != VNOVAL)
257 ip->rdev = vap->va_rdev;
258
259 if (vap->va_type == VFIFO)
260 vp->v_op = chfs_fifoop_p;
261 else {
262 vp->v_op = chfs_specop_p;
263 spec_node_init(vp, ip->rdev);
264 }
265
266 if (err)
267 return err;
268
269 len = sizeof(dev_t);
270 chfs_set_vnode_size(vp, len);
271 bp = getiobuf(vp, true);
272 bp->b_bufsize = bp->b_resid = len;
273 bp->b_data = kmem_alloc(len, KM_SLEEP);
274 memcpy(bp->b_data, &ip->rdev, len);
275 bp->b_blkno = 0;
276
277 fd = chfs_alloc_full_dnode();
278
279 mutex_enter(&chmp->chm_lock_mountfields);
280
281 err = chfs_write_flash_dnode(chmp, vp, bp, fd);
282 if (err) {
283 mutex_exit(&chmp->chm_lock_mountfields);
284 kmem_free(bp->b_data, len);
285 return err;
286 }
287
288 err = chfs_add_full_dnode_to_inode(chmp, ip, fd);
289 if (err) {
290 mutex_exit(&chmp->chm_lock_mountfields);
291 kmem_free(bp->b_data, len);
292 return err;
293 }
294
295 mutex_exit(&chmp->chm_lock_mountfields);
296
297 *vpp = vp;
298 kmem_free(bp->b_data, len);
299 putiobuf(bp);
300
301 return 0;
302 }
303
304 /* --------------------------------------------------------------------- */
305
306 int
307 chfs_open(void *v)
308 {
309 struct vnode *vp = ((struct vop_open_args *) v)->a_vp;
310 int mode = ((struct vop_open_args *) v)->a_mode;
311 dbg("open()\n");
312
313 int error;
314 struct chfs_inode *ip;
315
316 KASSERT(VOP_ISLOCKED(vp));
317
318 ip = VTOI(vp);
319
320 KASSERT(vp->v_size == ip->size);
321 if (ip->chvc->nlink < 1) {
322 error = ENOENT;
323 goto out;
324 }
325
326 // If the file is marked append-only, deny write requests.
327 if (ip->flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
328 error = EPERM;
329 else
330 error = 0;
331
332 out:
333 KASSERT(VOP_ISLOCKED(vp));
334 return error;
335 }
336
337 /* --------------------------------------------------------------------- */
338
339 int
340 chfs_close(void *v)
341 {
342 struct vnode *vp = ((struct vop_close_args *) v)->a_vp;
343 dbg("close()\n");
344
345 struct chfs_inode *ip;
346
347 KASSERT(VOP_ISLOCKED(vp));
348
349 ip = VTOI(vp);
350
351 if (ip->chvc->nlink > 0) {
352 //ip->chvc->nlink = 0;
353 chfs_update(vp, NULL, NULL, UPDATE_CLOSE);
354 }
355
356 return 0;
357 }
358
359 /* --------------------------------------------------------------------- */
360
361 int
362 chfs_access(void *v)
363 {
364 struct vnode *vp = ((struct vop_access_args *) v)->a_vp;
365 int mode = ((struct vop_access_args *) v)->a_mode;
366 kauth_cred_t cred = ((struct vop_access_args *) v)->a_cred;
367
368 dbg("access()\n");
369 struct chfs_inode *ip = VTOI(vp);
370
371 if (mode & VWRITE) {
372 switch (vp->v_type) {
373 case VLNK:
374 case VDIR:
375 case VREG:
376 if (vp->v_mount->mnt_flag & MNT_RDONLY)
377 return (EROFS);
378 break;
379 case VBLK:
380 case VCHR:
381 case VSOCK:
382 case VFIFO:
383 break;
384 default:
385 break;
386 }
387 }
388
389 if (mode & VWRITE && ip->flags & IMMUTABLE)
390 return (EPERM);
391
392 return genfs_can_access(vp->v_type, ip->mode & ALLPERMS,
393 ip->uid, ip->gid, mode, cred);
394 }
395
396 /* --------------------------------------------------------------------- */
397
398 int
399 chfs_getattr(void *v)
400 {
401 struct vnode *vp = ((struct vop_getattr_args *) v)->a_vp;
402 struct vattr *vap = ((struct vop_getattr_args *) v)->a_vap;
403
404 struct chfs_inode *ip = VTOI(vp);
405 dbg("getattr()\n");
406
407 KASSERT(vp->v_size == ip->size);
408
409 vattr_null(vap);
410 CHFS_ITIMES(ip, NULL, NULL, NULL);
411
412 vap->va_type = vp->v_type;
413 vap->va_mode = ip->mode & ALLPERMS;
414 vap->va_nlink = ip->chvc->nlink;
415 vap->va_uid = ip->uid;
416 vap->va_gid = ip->gid;
417 vap->va_fsid = ip->dev;
418 vap->va_fileid = ip->ino;
419 vap->va_size = ip->size;
420 vap->va_blocksize = PAGE_SIZE;
421 vap->va_atime.tv_sec = ip->atime;
422 vap->va_atime.tv_nsec = 0;
423 vap->va_mtime.tv_sec = ip->mtime;
424 vap->va_mtime.tv_nsec = 0;
425 vap->va_ctime.tv_sec = ip->ctime;
426 vap->va_ctime.tv_nsec = 0;
427 vap->va_gen = ip->version;
428 vap->va_flags = ip->flags;
429 vap->va_rdev = ip->rdev;
430 vap->va_bytes = round_page(ip->size);
431 vap->va_filerev = VNOVAL;
432 vap->va_vaflags = 0;
433 vap->va_spare = VNOVAL;
434
435 return 0;
436 }
437
438 /* --------------------------------------------------------------------- */
439
440 /* Note: modelled after tmpfs's same function */
441
442 int
443 chfs_setattr(void *v)
444 {
445 struct vnode *vp = ((struct vop_setattr_args *) v)->a_vp;
446 struct vattr *vap = ((struct vop_setattr_args *) v)->a_vap;
447 kauth_cred_t cred = ((struct vop_setattr_args *) v)->a_cred;
448
449 struct chfs_inode *ip;
450 struct ufsmount *ump = VFSTOUFS(vp->v_mount);
451 struct chfs_mount *chmp = ump->um_chfs;
452 int error = 0;
453
454 dbg("setattr()\n");
455
456 KASSERT(VOP_ISLOCKED(vp));
457 ip = VTOI(vp);
458
459 /* Abort if any unsettable attribute is given. */
460 if (vap->va_type != VNON || vap->va_nlink != VNOVAL ||
461 vap->va_fsid != VNOVAL || vap->va_fileid != VNOVAL ||
462 vap->va_blocksize != VNOVAL /*|| GOODTIME(&vap->va_ctime)*/ ||
463 vap->va_gen != VNOVAL || vap->va_rdev != VNOVAL ||
464 vap->va_bytes != VNOVAL) {
465 return EINVAL;
466 }
467
468 if (error == 0 && (vap->va_flags != VNOVAL))
469 error = chfs_chflags(vp, vap->va_flags, cred);
470
471 if (error == 0 && (vap->va_size != VNOVAL))
472 error = chfs_chsize(vp, vap->va_size, cred);
473
474 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
475 error = chfs_chown(vp, vap->va_uid, vap->va_gid, cred);
476
477 if (error == 0 && (vap->va_mode != VNOVAL))
478 error = chfs_chmod(vp, vap->va_mode, cred);
479
480 #if 0
481 /* why do we need that? */
482 if (ip->flags & (IMMUTABLE | APPEND))
483 return EPERM;
484 #endif
485
486 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
487 error = genfs_can_chtimes(vp, vap->va_vaflags, ip->uid, cred);
488 if (error)
489 return error;
490 if (vap->va_atime.tv_sec != VNOVAL)
491 ip->iflag |= IN_ACCESS;
492 if (vap->va_mtime.tv_sec != VNOVAL)
493 ip->iflag |= IN_CHANGE | IN_UPDATE;
494 error = chfs_update(vp,
495 &vap->va_atime, &vap->va_mtime, UPDATE_WAIT);
496 if (error)
497 return error;
498 }
499
500 mutex_enter(&chmp->chm_lock_mountfields);
501 error = chfs_write_flash_vnode(chmp, ip, ALLOC_NORMAL);
502 mutex_exit(&chmp->chm_lock_mountfields);
503
504 return error;
505 }
506
507 int
508 chfs_chmod(struct vnode *vp, int mode, kauth_cred_t cred)
509 {
510 struct chfs_inode *ip = VTOI(vp);
511 int error;
512 dbg("chmod\n");
513
514 error = genfs_can_chmod(vp, cred, ip->uid, ip->gid, mode);
515 if (error)
516 return error;
517 ip->mode &= ~ALLPERMS;
518 ip->mode |= (mode & ALLPERMS);
519 ip->iflag |= IN_CHANGE;
520
521 error = chfs_update(vp, NULL, NULL, UPDATE_WAIT);
522 if (error)
523 return error;
524
525 return 0;
526 }
527
528 int
529 chfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred)
530 {
531 struct chfs_inode *ip = VTOI(vp);
532 int error;
533 dbg("chown\n");
534
535 if (uid == (uid_t)VNOVAL)
536 uid = ip->uid;
537 if (gid == (gid_t)VNOVAL)
538 gid = ip->gid;
539
540 error = genfs_can_chown(vp, cred, ip->uid, ip->gid, uid, gid);
541 if (error)
542 return error;
543
544 ip->gid = gid;
545 ip->uid = uid;
546 ip->iflag |= IN_CHANGE;
547
548 error = chfs_update(vp, NULL, NULL, UPDATE_WAIT);
549 if (error)
550 return error;
551
552 return 0;
553 }
554
555
556 /* --------------------------------------------------------------------- */
557 /* calculates ((off_t)blk * chmp->chm_chm_fs_bsize) */
558 #define lblktosize(chmp, blk) \
559 (((off_t)(blk)) << (chmp)->chm_fs_bshift)
560
561 /* calculates (loc % chmp->chm_chm_fs_bsize) */
562 #define blkoff(chmp, loc) \
563 ((loc) & (chmp)->chm_fs_qbmask)
564
565 /* calculates (loc / chmp->chm_chm_fs_bsize) */
566 #define lblkno(chmp, loc) \
567 ((loc) >> (chmp)->chm_fs_bshift)
568
569 /* calculates roundup(size, chmp->chm_chm_fs_fsize) */
570 #define fragroundup(chmp, size) \
571 (((size) + (chmp)->chm_fs_qfmask) & (chmp)->chm_fs_fmask)
572
573 #define blksize(chmp, ip, lbn) \
574 (((lbn) >= NDADDR || (ip)->size >= lblktosize(chmp, (lbn) + 1)) \
575 ? (chmp)->chm_fs_bsize \
576 : (fragroundup(chmp, blkoff(chmp, (ip)->size))))
577
578 /* calculates roundup(size, chmp->chm_chm_fs_bsize) */
579 #define blkroundup(chmp, size) \
580 (((size) + (chmp)->chm_fs_qbmask) & (chmp)->chm_fs_bmask)
581
582 int
583 chfs_read(void *v)
584 {
585 struct vop_read_args /* {
586 struct vnode *a_vp;
587 struct uio *a_uio;
588 int a_ioflag;
589 kauth_cred_t a_cred;
590 } */ *ap = v;
591 struct vnode *vp;
592 struct chfs_inode *ip;
593 struct uio *uio;
594 struct ufsmount *ump;
595 struct buf *bp;
596 struct chfs_mount *chmp;
597 daddr_t lbn, nextlbn;
598 off_t bytesinfile;
599 long size, xfersize, blkoffset;
600 int error, ioflag;
601 vsize_t bytelen;
602 bool usepc = false;
603
604 dbg("chfs_read\n");
605
606 vp = ap->a_vp;
607 ip = VTOI(vp);
608 ump = ip->ump;
609 uio = ap->a_uio;
610 ioflag = ap->a_ioflag;
611 error = 0;
612
613 dbg("ip->size:%llu\n", ip->size);
614
615 #ifdef DIAGNOSTIC
616 if (uio->uio_rw != UIO_READ)
617 panic("%s: mode", READ_S);
618
619 if (vp->v_type == VLNK) {
620 if (ip->size < ump->um_maxsymlinklen)
621 panic("%s: short symlink", READ_S);
622 } else if (vp->v_type != VREG && vp->v_type != VDIR)
623 panic("%s: type %d", READ_S, vp->v_type);
624 #endif
625 chmp = ip->chmp;
626 if ((u_int64_t)uio->uio_offset > ump->um_maxfilesize)
627 return (EFBIG);
628 if (uio->uio_resid == 0)
629 return (0);
630
631 fstrans_start(vp->v_mount, FSTRANS_SHARED);
632
633 if (uio->uio_offset >= ip->size)
634 goto out;
635
636 usepc = vp->v_type == VREG;
637 bytelen = 0;
638 if (usepc) {
639 const int advice = IO_ADV_DECODE(ap->a_ioflag);
640
641 while (uio->uio_resid > 0) {
642 if (ioflag & IO_DIRECT) {
643 genfs_directio(vp, uio, ioflag);
644 }
645 bytelen = MIN(ip->size - uio->uio_offset,
646 uio->uio_resid);
647 if (bytelen == 0)
648 break;
649 error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
650 UBC_READ | UBC_PARTIALOK |
651 (UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0));
652 if (error)
653 break;
654
655 }
656 goto out;
657 }
658
659
660 dbg("start reading\n");
661 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
662 bytesinfile = ip->size - uio->uio_offset;
663 if (bytesinfile <= 0)
664 break;
665 lbn = lblkno(chmp, uio->uio_offset);
666 nextlbn = lbn + 1;
667 size = blksize(chmp, ip, lbn);
668 blkoffset = blkoff(chmp, uio->uio_offset);
669 xfersize = MIN(MIN(chmp->chm_fs_bsize - blkoffset, uio->uio_resid),
670 bytesinfile);
671
672 if (lblktosize(chmp, nextlbn) >= ip->size) {
673 error = bread(vp, lbn, size, NOCRED, 0, &bp);
674 dbg("after bread\n");
675 } else {
676 int nextsize = blksize(chmp, ip, nextlbn);
677 dbg("size: %ld\n", size);
678 error = breadn(vp, lbn,
679 size, &nextlbn, &nextsize, 1, NOCRED, 0, &bp);
680 dbg("after breadN\n");
681 }
682 if (error)
683 break;
684
685 /*
686 * We should only get non-zero b_resid when an I/O error
687 * has occurred, which should cause us to break above.
688 * However, if the short read did not cause an error,
689 * then we want to ensure that we do not uiomove bad
690 * or uninitialized data.
691 */
692 size -= bp->b_resid;
693 if (size < xfersize) {
694 if (size == 0)
695 break;
696 xfersize = size;
697 }
698 dbg("uiomove\n");
699 error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
700 if (error)
701 break;
702 brelse(bp, 0);
703 }
704 if (bp != NULL)
705 brelse(bp, 0);
706
707 out:
708 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
709 ip->iflag |= IN_ACCESS;
710 if ((ap->a_ioflag & IO_SYNC) == IO_SYNC) {
711 //error = UFS_WAPBL_BEGIN(vp->v_mount);
712 if (error) {
713 fstrans_done(vp->v_mount);
714 return error;
715 }
716 error = chfs_update(vp, NULL, NULL, UPDATE_WAIT);
717 //UFS_WAPBL_END(vp->v_mount);
718 }
719 }
720
721 dbg("[END]\n");
722 fstrans_done(vp->v_mount);
723 return (error);
724 }
725
726
727 /* --------------------------------------------------------------------- */
728
729 /*from ffs write*/
730 int
731 chfs_write(void *v)
732 {
733 struct vop_write_args /* {
734 struct vnode *a_vp;
735 struct uio *a_uio;
736 int a_ioflag;
737 kauth_cred_t a_cred;
738 } */ *ap = v;
739 struct vnode *vp ;
740 struct uio *uio;
741 struct chfs_inode *ip;
742 struct chfs_mount *chmp;
743 struct lwp *l;
744 kauth_cred_t cred;
745 off_t osize, origoff, oldoff, preallocoff, endallocoff, nsize;
746 int blkoffset, error, flags, ioflag, resid;
747 int aflag;
748 int extended=0;
749 vsize_t bytelen;
750 bool async;
751 struct ufsmount *ump;
752
753
754 cred = ap->a_cred;
755 ioflag = ap->a_ioflag;
756 uio = ap->a_uio;
757 vp = ap->a_vp;
758 ip = VTOI(vp);
759 //dbg("file size (vp): %llu\n", vp->v_size);
760 //dbg("file size (ip): %llu\n", ip->i_size);
761 ump = ip->ump;
762
763 //dbg("uio->resid: %d\n", uio->uio_resid);
764 dbg("write\n");
765
766 KASSERT(vp->v_size == ip->size);
767
768 switch (vp->v_type) {
769 case VREG:
770 if (ioflag & IO_APPEND)
771 uio->uio_offset = ip->size;
772 if ((ip->flags & APPEND) && uio->uio_offset != ip->size)
773 return (EPERM);
774 /* FALLTHROUGH */
775 case VLNK:
776 break;
777 case VDIR:
778 if ((ioflag & IO_SYNC) == 0)
779 panic("chfs_write: nonsync dir write");
780 break;
781 default:
782 panic("chfs_write: type");
783 }
784
785 chmp = ip->chmp;
786 if (uio->uio_offset < 0 ||
787 (u_int64_t)uio->uio_offset +
788 uio->uio_resid > ump->um_maxfilesize) {
789 dbg("uio->uio_offset = %lld | uio->uio_offset + "
790 "uio->uio_resid (%llu) > ump->um_maxfilesize (%lld)\n",
791 uio->uio_offset,
792 (u_int64_t)uio->uio_offset + uio->uio_resid,
793 ump->um_maxfilesize);
794 return (EFBIG);
795 }
796 /*
797 * Maybe this should be above the vnode op call, but so long as
798 * file servers have no limits, I don't think it matters.
799 */
800 l = curlwp;
801 if (vp->v_type == VREG && l &&
802 uio->uio_offset + uio->uio_resid >
803 l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
804 mutex_enter(proc_lock);
805 psignal(l->l_proc, SIGXFSZ);
806 mutex_exit(proc_lock);
807 return (EFBIG);
808 }
809 if (uio->uio_resid == 0)
810 return (0);
811
812 //mutex_enter(&ip->inode_lock);
813 fstrans_start(vp->v_mount, FSTRANS_SHARED);
814
815 flags = ioflag & IO_SYNC ? B_SYNC : 0;
816 async = vp->v_mount->mnt_flag & MNT_ASYNC;
817 origoff = uio->uio_offset;
818 resid = uio->uio_resid;
819 osize = ip->size;
820 error = 0;
821
822
823 /*if ((ioflag & IO_JOURNALLOCKED) == 0) {
824 error = UFS_WAPBL_BEGIN(vp->v_mount);
825 if (error) {
826 fstrans_done(vp->v_mount);
827 return error;
828 }
829 }*/
830
831 preallocoff = round_page(blkroundup(chmp,
832 MAX(osize, uio->uio_offset)));
833 aflag = ioflag & IO_SYNC ? B_SYNC : 0;
834 nsize = MAX(osize, uio->uio_offset + uio->uio_resid);
835 endallocoff = nsize - blkoff(chmp, nsize);
836
837 /*
838 * if we're increasing the file size, deal with expanding
839 * the fragment if there is one.
840 */
841
842 if (nsize > osize && lblkno(chmp, osize) < NDADDR &&
843 lblkno(chmp, osize) != lblkno(chmp, nsize) &&
844 blkroundup(chmp, osize) != osize) {
845 off_t eob;
846
847 eob = blkroundup(chmp, osize);
848 uvm_vnp_setwritesize(vp, eob);
849 error = ufs_balloc_range(vp, osize, eob - osize, cred, aflag);
850 if (error)
851 goto out;
852 if (flags & B_SYNC) {
853 mutex_enter(vp->v_interlock);
854 VOP_PUTPAGES(vp,
855 trunc_page(osize & chmp->chm_fs_bmask),
856 round_page(eob),
857 PGO_CLEANIT | PGO_SYNCIO | PGO_JOURNALLOCKED);
858 }
859 }
860
861 while (uio->uio_resid > 0) {
862 int ubc_flags = UBC_WRITE;
863 bool overwrite; /* if we're overwrite a whole block */
864 off_t newoff;
865
866 if (ioflag & IO_DIRECT) {
867 genfs_directio(vp, uio, ioflag | IO_JOURNALLOCKED);
868 }
869
870 oldoff = uio->uio_offset;
871 blkoffset = blkoff(chmp, uio->uio_offset);
872 bytelen = MIN(chmp->chm_fs_bsize - blkoffset, uio->uio_resid);
873 if (bytelen == 0) {
874 break;
875 }
876
877 /*
878 * if we're filling in a hole, allocate the blocks now and
879 * initialize the pages first. if we're extending the file,
880 * we can safely allocate blocks without initializing pages
881 * since the new blocks will be inaccessible until the write
882 * is complete.
883 */
884 overwrite = uio->uio_offset >= preallocoff &&
885 uio->uio_offset < endallocoff;
886 if (!overwrite && (vp->v_vflag & VV_MAPPED) == 0 &&
887 blkoff(chmp, uio->uio_offset) == 0 &&
888 (uio->uio_offset & PAGE_MASK) == 0) {
889 vsize_t len;
890
891 len = trunc_page(bytelen);
892 len -= blkoff(chmp, len);
893 if (len > 0) {
894 overwrite = true;
895 bytelen = len;
896 }
897 }
898
899 newoff = oldoff + bytelen;
900 if (vp->v_size < newoff) {
901 uvm_vnp_setwritesize(vp, newoff);
902 }
903
904 if (!overwrite) {
905 error = ufs_balloc_range(vp, uio->uio_offset, bytelen,
906 cred, aflag);
907 if (error)
908 break;
909 } else {
910 genfs_node_wrlock(vp);
911 error = GOP_ALLOC(vp, uio->uio_offset, bytelen,
912 aflag, cred);
913 genfs_node_unlock(vp);
914 if (error)
915 break;
916 ubc_flags |= UBC_FAULTBUSY;
917 }
918
919 /*
920 * copy the data.
921 */
922
923 ubc_flags |= UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
924 error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
925 IO_ADV_DECODE(ioflag), ubc_flags);
926
927 /*
928 * update UVM's notion of the size now that we've
929 * copied the data into the vnode's pages.
930 *
931 * we should update the size even when uiomove failed.
932 */
933
934 if (vp->v_size < newoff) {
935 uvm_vnp_setsize(vp, newoff);
936 extended = 1;
937 }
938
939 if (error)
940 break;
941
942 /*
943 * flush what we just wrote if necessary.
944 * XXXUBC simplistic async flushing.
945 */
946
947 if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
948 mutex_enter(vp->v_interlock);
949 error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
950 (uio->uio_offset >> 16) << 16,
951 PGO_CLEANIT | PGO_JOURNALLOCKED);
952 if (error)
953 break;
954 }
955 }
956 out:
957 if (error == 0 && ioflag & IO_SYNC) {
958 mutex_enter(vp->v_interlock);
959 error = VOP_PUTPAGES(vp,
960 trunc_page(origoff & chmp->chm_fs_bmask),
961 round_page(blkroundup(chmp, uio->uio_offset)),
962 PGO_CLEANIT | PGO_SYNCIO | PGO_JOURNALLOCKED);
963 }
964 ip->iflag |= IN_CHANGE | IN_UPDATE;
965 if (resid > uio->uio_resid && ap->a_cred &&
966 kauth_authorize_generic(ap->a_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
967 ip->mode &= ~(ISUID | ISGID);
968 }
969 if (resid > uio->uio_resid)
970 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
971 if (error) {
972 (void) UFS_TRUNCATE(vp, osize, ioflag & IO_SYNC, ap->a_cred);
973 uio->uio_offset -= resid - uio->uio_resid;
974 uio->uio_resid = resid;
975 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
976 error = UFS_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
977
978 //XXX hack, i write the next line after i know ip->i_size and vp->v_size don't equal
979 chfs_set_vnode_size(vp, vp->v_size);
980
981
982 //dbg("end file size (vp): %llu\n", vp->v_size);
983 //dbg("end file size (ip): %llu\n", ip->i_size);
984 KASSERT(vp->v_size == ip->size);
985 fstrans_done(vp->v_mount);
986
987 mutex_enter(&chmp->chm_lock_mountfields);
988 error = chfs_write_flash_vnode(chmp, ip, ALLOC_NORMAL);
989 mutex_exit(&chmp->chm_lock_mountfields);
990
991 //mutex_exit(&ip->inode_lock);
992 //dbg("end\n");
993 return (error);
994 }
995
996
997 /* --------------------------------------------------------------------- */
998
999 int
1000 chfs_fsync(void *v)
1001 {
1002 //dbg("fsync\n");
1003 struct vop_fsync_args /* {
1004 struct vnode *a_vp;
1005 kauth_cred_t a_cred;
1006 int a_flags;
1007 off_t offlo;
1008 off_t offhi;
1009 } */ *ap = v;
1010 struct vnode *vp = ap->a_vp;
1011 int wait;
1012
1013 if (ap->a_flags & FSYNC_CACHE) {
1014 return ENODEV;
1015 }
1016 wait = (ap->a_flags & FSYNC_WAIT) != 0;
1017 vflushbuf(vp, wait);
1018 //struct chfs_inode *ip = VTOI(vp);
1019 //chfs_set_vnode_size(vp, ip->write_size);
1020
1021 return 0;
1022 }
1023
1024 /* --------------------------------------------------------------------- */
1025
1026 int
1027 chfs_remove(void *v)
1028 {
1029 struct vnode *dvp = ((struct vop_remove_args *) v)->a_dvp;
1030 struct vnode *vp = ((struct vop_remove_args *) v)->a_vp;
1031 struct componentname *cnp = (((struct vop_remove_args *) v)->a_cnp);
1032 dbg("remove\n");
1033
1034 KASSERT(VOP_ISLOCKED(dvp));
1035 KASSERT(VOP_ISLOCKED(vp));
1036
1037 struct chfs_inode *ip = VTOI(vp);
1038 struct chfs_inode *parent = VTOI(dvp);
1039 int error = 0;
1040
1041 KASSERT(ip->chvc->vno != ip->chvc->pvno);
1042
1043 error = chfs_do_unlink(ip,
1044 parent, cnp->cn_nameptr, cnp->cn_namelen);
1045
1046 vput(dvp);
1047 vput(vp);
1048
1049 return error;
1050 }
1051
1052 /* --------------------------------------------------------------------- */
1053
1054 int
1055 chfs_link(void *v)
1056 {
1057 struct vnode *dvp = ((struct vop_link_args *) v)->a_dvp;
1058 struct vnode *vp = ((struct vop_link_args *) v)->a_vp;
1059 struct componentname *cnp = ((struct vop_link_args *) v)->a_cnp;
1060
1061 struct chfs_inode *ip, *parent;
1062 int error = 0;
1063
1064 if (vp->v_type == VDIR) {
1065 VOP_ABORTOP(dvp, cnp);
1066 error = EISDIR;
1067 goto out;
1068 }
1069 if (dvp->v_mount != vp->v_mount) {
1070 VOP_ABORTOP(dvp, cnp);
1071 error = EXDEV;
1072 goto out;
1073 }
1074 if (dvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE))) {
1075 VOP_ABORTOP(dvp, cnp);
1076 goto out;
1077 }
1078
1079 parent = VTOI(dvp);
1080 ip = VTOI(vp);
1081
1082 error = chfs_do_link(ip,
1083 parent, cnp->cn_nameptr, cnp->cn_namelen, vp->v_type);
1084
1085 if (dvp != vp)
1086 VOP_UNLOCK(vp);
1087 out:
1088 vput(dvp);
1089 return error;
1090 }
1091
1092 /* --------------------------------------------------------------------- */
1093
1094 int
1095 chfs_rename(void *v)
1096 {
1097 struct vnode *fdvp = ((struct vop_rename_args *) v)->a_fdvp;
1098 struct vnode *fvp = ((struct vop_rename_args *) v)->a_fvp;
1099 struct componentname *fcnp = ((struct vop_rename_args *) v)->a_fcnp;
1100 struct vnode *tdvp = ((struct vop_rename_args *) v)->a_tdvp;
1101 struct vnode *tvp = ((struct vop_rename_args *) v)->a_tvp;
1102 struct componentname *tcnp = ((struct vop_rename_args *) v)->a_tcnp;
1103
1104 struct chfs_inode *oldparent, *old;
1105 struct chfs_inode *newparent;
1106 struct chfs_dirent *fd;//, *oldfd;
1107 struct chfs_inode *ip;
1108 int error = 0;
1109 dbg("rename\n");
1110
1111 KASSERT(VOP_ISLOCKED(tdvp));
1112 KASSERT(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
1113
1114 oldparent = VTOI(fdvp);
1115 old = VTOI(fvp);
1116 newparent = VTOI(tdvp);
1117 if (tvp) {
1118 dbg("tvp not null\n");
1119 ip = VTOI(tvp);
1120 if (tvp->v_type == VDIR) {
1121 //TODO: lock
1122 // fd = ip->dents;
1123 // while (fd) {
1124 TAILQ_FOREACH(fd, &ip->dents, fds) {
1125 if (fd->vno) {
1126 //TODO: unlock
1127 error = ENOTEMPTY;
1128 goto out_unlocked;
1129 }
1130 // fd = fd->next;
1131 }
1132 //TODO: unlock
1133 }
1134 error = chfs_do_unlink(ip,
1135 newparent, tcnp->cn_nameptr, tcnp->cn_namelen);
1136 vput(tvp);
1137 }
1138 VFS_VGET(tdvp->v_mount, old->ino, &tvp);
1139 ip = VTOI(tvp);
1140
1141 // for (oldfd = oldparent->dents;
1142 // oldfd->vno != old->ino;
1143 // oldfd = oldfd->next);
1144
1145 error = chfs_do_link(ip,
1146 newparent, tcnp->cn_nameptr, tcnp->cn_namelen, tvp->v_type);
1147 error = chfs_do_unlink(old,
1148 oldparent, fcnp->cn_nameptr, fcnp->cn_namelen);
1149
1150 //out:
1151 // if (fchnode != tchnode)
1152 // VOP_UNLOCK(fdvp, 0);
1153
1154 out_unlocked:
1155 // Release target nodes.
1156 if (tdvp == tvp)
1157 vrele(tdvp);
1158 else
1159 vput(tdvp);
1160 if (tvp != NULL)
1161 vput(tvp);
1162
1163 // Release source nodes.
1164 vrele(fdvp);
1165 vrele(fvp);
1166
1167 return error;
1168 }
1169
1170 /* --------------------------------------------------------------------- */
1171
1172 int
1173 chfs_mkdir(void *v)
1174 {
1175 struct vnode *dvp = ((struct vop_mkdir_args *) v)->a_dvp;
1176 struct vnode **vpp = ((struct vop_mkdir_args *)v)->a_vpp;
1177 struct componentname *cnp = ((struct vop_mkdir_args *) v)->a_cnp;
1178 struct vattr *vap = ((struct vop_mkdir_args *) v)->a_vap;
1179 dbg("mkdir()\n");
1180
1181 int mode;
1182
1183 mode = vap->va_mode & ACCESSPERMS;
1184 if ((mode & IFMT) == 0) {
1185 mode |= IFDIR;
1186 }
1187
1188 KASSERT(vap->va_type == VDIR);
1189
1190 return chfs_makeinode(mode, dvp, vpp, cnp, VDIR);
1191 }
1192
1193 /* --------------------------------------------------------------------- */
1194
1195 int
1196 chfs_rmdir(void *v)
1197 {
1198 struct vnode *dvp = ((struct vop_rmdir_args *) v)->a_dvp;
1199 struct vnode *vp = ((struct vop_rmdir_args *) v)->a_vp;
1200 struct componentname *cnp = ((struct vop_rmdir_args *) v)->a_cnp;
1201 dbg("rmdir()\n");
1202
1203 KASSERT(VOP_ISLOCKED(dvp));
1204 KASSERT(VOP_ISLOCKED(vp));
1205
1206 struct chfs_inode *ip = VTOI(vp);
1207 struct chfs_inode *parent = VTOI(dvp);
1208 struct chfs_dirent *fd;
1209 int error = 0;
1210
1211 if (vp->v_type != VDIR) {
1212 error = ENOTDIR;
1213 goto out;
1214 }
1215
1216 KASSERT(ip->chvc->vno != ip->chvc->pvno);
1217
1218 // for (fd = ip->dents; fd; fd = fd->next) {
1219 TAILQ_FOREACH(fd, &ip->dents, fds) {
1220 if (fd->vno) {
1221 error = ENOTEMPTY;
1222 goto out;
1223 }
1224 }
1225
1226 error = chfs_do_unlink(ip,
1227 parent, cnp->cn_nameptr, cnp->cn_namelen);
1228
1229 out:
1230 vput(dvp);
1231 vput(vp);
1232
1233 return error;
1234 }
1235
1236 /* --------------------------------------------------------------------- */
1237
1238 int
1239 chfs_symlink(void *v)
1240 {
1241 struct vnode *dvp = ((struct vop_symlink_args *) v)->a_dvp;
1242 struct vnode **vpp = ((struct vop_symlink_args *) v)->a_vpp;
1243 struct componentname *cnp = ((struct vop_symlink_args *) v)->a_cnp;
1244 struct vattr *vap = ((struct vop_symlink_args *) v)->a_vap;
1245 char *target = ((struct vop_symlink_args *) v)->a_target;
1246
1247 struct ufsmount *ump;
1248 struct chfs_mount *chmp;
1249 struct vnode *vp;
1250 struct chfs_inode *ip;
1251 int len, err;
1252 struct chfs_full_dnode *fd;
1253 struct buf *bp;
1254 dbg("symlink()\n");
1255
1256 ump = VFSTOUFS(dvp->v_mount);
1257 chmp = ump->um_chfs;
1258
1259 err = chfs_makeinode(IFLNK | vap->va_mode, dvp, vpp, cnp, VLNK);
1260 if (err)
1261 return (err);
1262 VN_KNOTE(dvp, NOTE_WRITE);
1263 vp = *vpp;
1264 len = strlen(target);
1265 ip = VTOI(vp);
1266 /* TODO max symlink len instead of "100" */
1267 if (len < 100) {
1268 ip->target = kmem_alloc(len, KM_SLEEP);
1269 memcpy(ip->target, target, len);
1270 chfs_set_vnode_size(vp, len);
1271 ip->iflag |= IN_CHANGE | IN_UPDATE;
1272
1273 bp = getiobuf(vp, true);
1274 bp->b_bufsize = bp->b_resid = len;
1275 bp->b_data = kmem_alloc(len, KM_SLEEP);
1276 memcpy(bp->b_data, target, len);
1277 bp->b_blkno = 0;
1278
1279 fd = chfs_alloc_full_dnode();
1280
1281 mutex_enter(&chmp->chm_lock_mountfields);
1282
1283 err = chfs_write_flash_dnode(chmp, vp, bp, fd);
1284 if (err) {
1285 mutex_exit(&chmp->chm_lock_mountfields);
1286 goto out;
1287 }
1288
1289 err = chfs_add_full_dnode_to_inode(chmp, ip, fd);
1290 if (err) {
1291 mutex_exit(&chmp->chm_lock_mountfields);
1292 goto out;
1293 }
1294
1295 mutex_exit(&chmp->chm_lock_mountfields);
1296
1297 kmem_free(bp->b_data, len);
1298 putiobuf(bp);
1299
1300 uvm_vnp_setsize(vp, len);
1301 } else {
1302 err = vn_rdwr(UIO_WRITE, vp, target, len, (off_t)0,
1303 UIO_SYSSPACE, IO_NODELOCKED, cnp->cn_cred,
1304 (size_t *)0, NULL);
1305 }
1306
1307 out:
1308 if (err)
1309 vput(vp);
1310
1311 return (err);
1312 }
1313
1314 /* --------------------------------------------------------------------- */
1315
1316 int
1317 chfs_readdir(void *v)
1318 {
1319 struct vnode *vp = ((struct vop_readdir_args *) v)->a_vp;
1320 struct uio *uio = ((struct vop_readdir_args *) v)->a_uio;
1321 int *eofflag = ((struct vop_readdir_args *) v)->a_eofflag;
1322
1323 int error = 0;
1324 off_t skip, offset;
1325 struct chfs_inode *ip;
1326 struct chfs_dirent *fd;
1327
1328 struct ufsmount *ump;
1329 struct chfs_mount *chmp;
1330 struct chfs_vnode_cache *chvc;
1331
1332 KASSERT(VOP_ISLOCKED(vp));
1333
1334 /* This operation only makes sense on directory nodes. */
1335 if (vp->v_type != VDIR) {
1336 error = ENOTDIR;
1337 goto out;
1338 }
1339
1340 ip = VTOI(vp);
1341
1342 /* uiomove in chfs_filldir automatically increments the
1343 * uio_offset by an arbitrary size, so we discard any change
1344 * to uio_offset and set it to our own value on return
1345 */
1346 offset = uio->uio_offset;
1347
1348 if (offset == CHFS_OFFSET_DOT) {
1349 error = chfs_filldir(uio, ip->ino, ".", 1, VDIR);
1350 if (error == -1) {
1351 error = 0;
1352 goto outok;
1353 } else if (error != 0)
1354 goto outok;
1355
1356 offset = CHFS_OFFSET_DOTDOT;
1357 }
1358
1359 if (offset == CHFS_OFFSET_DOTDOT) {
1360 ump = VFSTOUFS(vp->v_mount);
1361 chmp = ump->um_chfs;
1362 mutex_enter(&chmp->chm_lock_vnocache);
1363 chvc = chfs_vnode_cache_get(chmp, ip->ino);
1364 mutex_exit(&chmp->chm_lock_vnocache);
1365
1366 error = chfs_filldir(uio, chvc->pvno, "..", 2, VDIR);
1367 if (error == -1) {
1368 error = 0;
1369 goto outok;
1370 } else if (error != 0) {
1371 goto outok;
1372 }
1373
1374 if (TAILQ_EMPTY(&ip->dents)) {
1375 offset = CHFS_OFFSET_EOF;
1376 } else {
1377 offset = CHFS_OFFSET_FIRST;
1378 }
1379 }
1380
1381 if (offset != CHFS_OFFSET_EOF) {
1382 skip = offset - CHFS_OFFSET_FIRST;
1383
1384 TAILQ_FOREACH(fd, &ip->dents, fds) {
1385 /* seek to offset by skipping items */
1386 /* XXX race conditions by changed dirent? */
1387 if (skip > 0) {
1388 skip--;
1389 continue;
1390 }
1391
1392 if (fd->vno != 0) {
1393 error = chfs_filldir(uio, fd->vno,
1394 fd->name, fd->nsize, fd->type);
1395 if (error == -1) {
1396 error = 0;
1397 goto outok;
1398 } else if (error != 0) {
1399 dbg("err %d\n", error);
1400 goto outok;
1401 }
1402 }
1403 offset++;
1404 }
1405 }
1406 offset = CHFS_OFFSET_EOF;
1407
1408 outok:
1409 uio->uio_offset = offset;
1410
1411 if (eofflag != NULL) {
1412 *eofflag = (error == 0 &&
1413 uio->uio_offset == CHFS_OFFSET_EOF);
1414 }
1415
1416 out:
1417 KASSERT(VOP_ISLOCKED(vp));
1418
1419 return error;
1420 }
1421
1422 /* --------------------------------------------------------------------- */
1423
1424 int
1425 chfs_readlink(void *v)
1426 {
1427
1428 struct vnode *vp = ((struct vop_readlink_args *) v)->a_vp;
1429 struct uio *uio = ((struct vop_readlink_args *) v)->a_uio;
1430 kauth_cred_t cred = ((struct vop_readlink_args *) v)->a_cred;
1431
1432 struct chfs_inode *ip = VTOI(vp);
1433
1434 dbg("readlink()\n");
1435
1436 /* TODO max symlink len instead of "100" */
1437 if (ip->size < 100) {
1438 uiomove(ip->target, ip->size, uio);
1439 return (0);
1440 }
1441
1442 return (VOP_READ(vp, uio, 0, cred));
1443 }
1444
1445 /* --------------------------------------------------------------------- */
1446
1447 int
1448 chfs_inactive(void *v)
1449 {
1450 struct vnode *vp = ((struct vop_inactive_args *) v)->a_vp;
1451 struct chfs_inode *ip = VTOI(vp);
1452 struct chfs_vnode_cache *chvc;
1453 dbg("inactive | vno: %llu\n", ip->ino);
1454
1455 KASSERT(VOP_ISLOCKED(vp));
1456
1457 if (ip->ino) {
1458 chvc = ip->chvc;
1459 if (chvc->nlink)
1460 *((struct vop_inactive_args *) v)->a_recycle = 0;
1461 } else {
1462 *((struct vop_inactive_args *) v)->a_recycle = 1;
1463 }
1464
1465 VOP_UNLOCK(vp);
1466
1467 return 0;
1468 }
1469
1470 /* --------------------------------------------------------------------- */
1471
1472 int
1473 chfs_reclaim(void *v)
1474 {
1475 struct vop_reclaim_args *ap = v;
1476 struct vnode *vp = ap->a_vp;
1477 struct chfs_inode *ip = VTOI(vp);
1478 struct chfs_mount *chmp = ip->chmp;
1479 struct chfs_dirent *fd;
1480
1481 //dbg("reclaim() | ino: %llu\n", ip->ino);
1482 //mutex_enter(&ip->inode_lock);
1483
1484 mutex_enter(&chmp->chm_lock_vnocache);
1485 chfs_vnode_cache_set_state(chmp,
1486 ip->chvc, VNO_STATE_CHECKEDABSENT);
1487 mutex_exit(&chmp->chm_lock_vnocache);
1488
1489 chfs_update(vp, NULL, NULL, UPDATE_CLOSE);
1490
1491 if (vp->v_type == VREG || vp->v_type == VLNK || vp->v_type == VCHR ||
1492 vp->v_type == VBLK || vp->v_type == VFIFO || vp->v_type == VSOCK)
1493 chfs_kill_fragtree(&ip->fragtree);
1494
1495 fd = TAILQ_FIRST(&ip->dents);
1496 while(fd) {
1497 TAILQ_REMOVE(&ip->dents, fd, fds);
1498 chfs_free_dirent(fd);
1499 fd = TAILQ_FIRST(&ip->dents);
1500 }
1501 //mutex_exit(&ip->inode_lock);
1502 //mutex_destroy(&ip->inode_lock);
1503
1504 cache_purge(vp);
1505 if (ip->devvp) {
1506 vrele(ip->devvp);
1507 ip->devvp = 0;
1508 }
1509 chfs_ihashrem(ip);
1510
1511 genfs_node_destroy(vp);
1512 pool_put(&chfs_inode_pool, vp->v_data);
1513 vp->v_data = NULL;
1514 return (0);
1515 }
1516
1517 /* --------------------------------------------------------------------- */
1518
1519 int
1520 chfs_advlock(void *v)
1521 {
1522 //struct vnode *vp = ((struct vop_advlock_args *) v)->a_vp;
1523 dbg("advlock()\n");
1524 /*
1525 struct chfs_node *node;
1526
1527 node = VP_TO_CHFS_NODE(vp);
1528
1529 return lf_advlock(v, &node->chn_lockf, node->chn_size);
1530 */
1531 return 0;
1532 }
1533
1534 /* --------------------------------------------------------------------- */
1535 int
1536 chfs_strategy(void *v)
1537 {
1538 struct vop_strategy_args /* {
1539 const struct vnodeop_desc *a_desc;
1540 struct vnode *a_vp;
1541 struct buf *a_bp;
1542 } */ *ap = v;
1543 struct chfs_full_dnode *fd;
1544 struct buf *bp = ap->a_bp;
1545 struct vnode *vp = ap->a_vp;
1546 struct chfs_inode *ip = VTOI(vp);
1547 struct chfs_mount *chmp = ip->chmp;
1548 int read = (bp->b_flags & B_READ) ? 1 : 0;
1549 int err = 0;
1550
1551 /* dbg("bp dump:\n");
1552 dbg(" ->b_bcount: %d\n", bp->b_bcount);
1553 dbg(" ->b_resid: %d\n", bp->b_resid);
1554 dbg(" ->b_blkno: %llu\n", bp->b_blkno);
1555 dbg(" ->b_error: %d\n", bp->b_error);*/
1556 if (read) {
1557 err = chfs_read_data(chmp, vp, bp);
1558 } else {
1559 fd = chfs_alloc_full_dnode();
1560
1561 mutex_enter(&chmp->chm_lock_mountfields);
1562
1563 err = chfs_write_flash_dnode(chmp, vp, bp, fd);
1564 if (err) {
1565 mutex_exit(&chmp->chm_lock_mountfields);
1566 goto out;
1567 }
1568
1569 err = chfs_add_full_dnode_to_inode(chmp, ip, fd);
1570 /*if (err) {
1571 mutex_exit(&chmp->chm_lock_mountfields);
1572 goto out;
1573 }*/
1574
1575 mutex_exit(&chmp->chm_lock_mountfields);
1576 }
1577 out:
1578 biodone(bp);
1579 //dbg("end\n");
1580 return err;
1581 }
1582
1583 int
1584 chfs_bmap(void *v)
1585 {
1586 struct vop_bmap_args /* {
1587 struct vnode *a_vp;
1588 daddr_t a_bn;
1589 struct vnode **a_vpp;
1590 daddr_t *a_bnp;
1591 int *a_runp;
1592 int *a_runb;
1593 } */ *ap = v;
1594 if (ap->a_vpp != NULL)
1595 *ap->a_vpp = ap->a_vp;
1596 if (ap->a_bnp != NULL)
1597 *ap->a_bnp = ap->a_bn;
1598 if (ap->a_runp != NULL)
1599 *ap->a_runp = 0;
1600 return (0);
1601 }
1602
1603 /*
1604 * vnode operations vector used for files stored in a chfs file system.
1605 */
1606 int
1607 (**chfs_vnodeop_p)(void *);
1608 const struct vnodeopv_entry_desc chfs_vnodeop_entries[] =
1609 {
1610 { &vop_default_desc, vn_default_error },
1611 { &vop_lookup_desc, chfs_lookup },
1612 { &vop_create_desc, chfs_create },
1613 { &vop_mknod_desc, chfs_mknod },
1614 { &vop_open_desc, chfs_open },
1615 { &vop_close_desc, chfs_close },
1616 { &vop_access_desc, chfs_access },
1617 { &vop_getattr_desc, chfs_getattr },
1618 { &vop_setattr_desc, chfs_setattr },
1619 { &vop_read_desc, chfs_read },
1620 { &vop_write_desc, chfs_write },
1621 { &vop_ioctl_desc, genfs_enoioctl },
1622 { &vop_fcntl_desc, genfs_fcntl },
1623 { &vop_poll_desc, genfs_poll },
1624 { &vop_kqfilter_desc, genfs_kqfilter },
1625 { &vop_revoke_desc, genfs_revoke },
1626 { &vop_mmap_desc, genfs_mmap },
1627 { &vop_fsync_desc, chfs_fsync },
1628 { &vop_seek_desc, genfs_seek },
1629 { &vop_remove_desc, chfs_remove },
1630 { &vop_link_desc, chfs_link },
1631 { &vop_rename_desc, chfs_rename },
1632 { &vop_mkdir_desc, chfs_mkdir },
1633 { &vop_rmdir_desc, chfs_rmdir },
1634 { &vop_symlink_desc, chfs_symlink },
1635 { &vop_readdir_desc, chfs_readdir },
1636 { &vop_readlink_desc, chfs_readlink },
1637 { &vop_abortop_desc, genfs_abortop },
1638 { &vop_inactive_desc, chfs_inactive },
1639 { &vop_reclaim_desc, chfs_reclaim },
1640 { &vop_lock_desc, genfs_lock },
1641 { &vop_unlock_desc, genfs_unlock },
1642 { &vop_bmap_desc, chfs_bmap },
1643 { &vop_strategy_desc, chfs_strategy },
1644 { &vop_print_desc, ufs_print },
1645 { &vop_pathconf_desc, ufs_pathconf },
1646 { &vop_islocked_desc, genfs_islocked },
1647 { &vop_advlock_desc, chfs_advlock },
1648 { &vop_bwrite_desc, vn_bwrite },
1649 { &vop_getpages_desc, genfs_getpages },
1650 { &vop_putpages_desc, genfs_putpages },
1651 { NULL, NULL } };
1652
1653 const struct vnodeopv_desc chfs_vnodeop_opv_desc =
1654 { &chfs_vnodeop_p, chfs_vnodeop_entries };
1655
1656 /* --------------------------------------------------------------------- */
1657
1658 /*
1659 * vnode operations vector used for special devices stored in a chfs
1660 * file system.
1661 */
1662 int
1663 (**chfs_specop_p)(void *);
1664 const struct vnodeopv_entry_desc chfs_specop_entries[] =
1665 {
1666 { &vop_default_desc, vn_default_error },
1667 { &vop_lookup_desc, spec_lookup },
1668 { &vop_create_desc, spec_create },
1669 { &vop_mknod_desc, spec_mknod },
1670 { &vop_open_desc, spec_open },
1671 { &vop_close_desc, ufsspec_close },
1672 { &vop_access_desc, chfs_access },
1673 { &vop_getattr_desc, chfs_getattr },
1674 { &vop_setattr_desc, chfs_setattr },
1675 { &vop_read_desc, chfs_read },
1676 { &vop_write_desc, chfs_write },
1677 { &vop_ioctl_desc, spec_ioctl },
1678 { &vop_fcntl_desc, genfs_fcntl },
1679 { &vop_poll_desc, spec_poll },
1680 { &vop_kqfilter_desc, spec_kqfilter },
1681 { &vop_revoke_desc, spec_revoke },
1682 { &vop_mmap_desc, spec_mmap },
1683 { &vop_fsync_desc, spec_fsync },
1684 { &vop_seek_desc, spec_seek },
1685 { &vop_remove_desc, spec_remove },
1686 { &vop_link_desc, spec_link },
1687 { &vop_rename_desc, spec_rename },
1688 { &vop_mkdir_desc, spec_mkdir },
1689 { &vop_rmdir_desc, spec_rmdir },
1690 { &vop_symlink_desc, spec_symlink },
1691 { &vop_readdir_desc, spec_readdir },
1692 { &vop_readlink_desc, spec_readlink },
1693 { &vop_abortop_desc, spec_abortop },
1694 { &vop_inactive_desc, chfs_inactive },
1695 { &vop_reclaim_desc, chfs_reclaim },
1696 { &vop_lock_desc, genfs_lock },
1697 { &vop_unlock_desc, genfs_unlock },
1698 { &vop_bmap_desc, spec_bmap },
1699 { &vop_strategy_desc, spec_strategy },
1700 { &vop_print_desc, ufs_print },
1701 { &vop_pathconf_desc, spec_pathconf },
1702 { &vop_islocked_desc, genfs_islocked },
1703 { &vop_advlock_desc, spec_advlock },
1704 { &vop_bwrite_desc, vn_bwrite },
1705 { &vop_getpages_desc, spec_getpages },
1706 { &vop_putpages_desc, spec_putpages },
1707 { NULL, NULL } };
1708
1709 const struct vnodeopv_desc chfs_specop_opv_desc =
1710 { &chfs_specop_p, chfs_specop_entries };
1711
1712 /* --------------------------------------------------------------------- */
1713 /*
1714 * vnode operations vector used for fifos stored in a chfs file system.
1715 */
1716 int
1717 (**chfs_fifoop_p)(void *);
1718 const struct vnodeopv_entry_desc chfs_fifoop_entries[] =
1719 {
1720 { &vop_default_desc, vn_default_error },
1721 { &vop_lookup_desc, vn_fifo_bypass },
1722 { &vop_create_desc, vn_fifo_bypass },
1723 { &vop_mknod_desc, vn_fifo_bypass },
1724 { &vop_open_desc, vn_fifo_bypass },
1725 { &vop_close_desc, ufsfifo_close },
1726 { &vop_access_desc, chfs_access },
1727 { &vop_getattr_desc, chfs_getattr },
1728 { &vop_setattr_desc, chfs_setattr },
1729 { &vop_read_desc, ufsfifo_read },
1730 { &vop_write_desc, ufsfifo_write },
1731 { &vop_ioctl_desc, vn_fifo_bypass },
1732 { &vop_fcntl_desc, genfs_fcntl },
1733 { &vop_poll_desc, vn_fifo_bypass },
1734 { &vop_kqfilter_desc, vn_fifo_bypass },
1735 { &vop_revoke_desc, vn_fifo_bypass },
1736 { &vop_mmap_desc, vn_fifo_bypass },
1737 { &vop_fsync_desc, vn_fifo_bypass },
1738 { &vop_seek_desc, vn_fifo_bypass },
1739 { &vop_remove_desc, vn_fifo_bypass },
1740 { &vop_link_desc, vn_fifo_bypass },
1741 { &vop_rename_desc, vn_fifo_bypass },
1742 { &vop_mkdir_desc, vn_fifo_bypass },
1743 { &vop_rmdir_desc, vn_fifo_bypass },
1744 { &vop_symlink_desc, vn_fifo_bypass },
1745 { &vop_readdir_desc, vn_fifo_bypass },
1746 { &vop_readlink_desc, vn_fifo_bypass },
1747 { &vop_abortop_desc, vn_fifo_bypass },
1748 { &vop_inactive_desc, chfs_inactive },
1749 { &vop_reclaim_desc, chfs_reclaim },
1750 { &vop_lock_desc, genfs_lock },
1751 { &vop_unlock_desc, genfs_unlock },
1752 { &vop_bmap_desc, vn_fifo_bypass },
1753 { &vop_strategy_desc, vn_fifo_bypass },
1754 { &vop_print_desc, ufs_print },
1755 { &vop_pathconf_desc, vn_fifo_bypass },
1756 { &vop_islocked_desc, genfs_islocked },
1757 { &vop_advlock_desc, vn_fifo_bypass },
1758 { &vop_bwrite_desc, genfs_nullop },
1759 { &vop_getpages_desc, genfs_badop },
1760 { &vop_putpages_desc, vn_fifo_bypass },
1761 { NULL, NULL } };
1762
1763 const struct vnodeopv_desc chfs_fifoop_opv_desc =
1764 { &chfs_fifoop_p, chfs_fifoop_entries };
1765