fix: corner cases when printing large content through term interface
[lunaix-os.git] / lunaix-os / kernel / fs / iso9660 / inode.c
1 #include <klibc/string.h>
2 #include <lunaix/fs.h>
3 #include <lunaix/fs/iso9660.h>
4 #include <lunaix/mm/cake.h>
5 #include <lunaix/mm/valloc.h>
6 #include <lunaix/spike.h>
7
8 extern struct cake_pile* drec_cache_pile;
9
10 static struct v_inode_ops iso_inode_ops = {
11     .dir_lookup = iso9660_dir_lookup,
12     .open = iso9660_open,
13 };
14
15 static struct v_file_ops iso_file_ops = { .close = iso9660_close,
16                                           .read = iso9660_read,
17                                           .read_page = iso9660_read_page,
18                                           .write = iso9660_write,
19                                           .write_page = iso9660_write_page,
20                                           .seek = iso9660_seek,
21                                           .readdir = iso9660_readdir };
22
23 void
24 iso9660_inode_destruct(struct v_inode* inode)
25 {
26     struct iso_inode* isoino = inode->data;
27
28     struct iso_drecache *pos, *n;
29     llist_for_each(pos, n, &isoino->drecaches, caches)
30     {
31         cake_release(drec_cache_pile, pos);
32     }
33
34     vfree(isoino);
35 }
36
37 void
38 iso9660_init_inode(struct v_superblock* vsb, struct v_inode* inode)
39 {
40     struct iso_inode* isoino = vzalloc(sizeof(struct iso_inode));
41     llist_init_head(&isoino->drecaches);
42     inode->data = isoino;
43     inode->destruct = iso9660_inode_destruct;
44 }
45
46 int
47 iso9660_fill_inode(struct v_inode* inode, struct iso_drecache* dir, int ino)
48 {
49     int errno = 0;
50     struct device* dev = inode->sb->dev;
51     struct iso_inode* isoino = (struct iso_inode*)inode->data;
52
53     // In the spec, there is a differentiation in how file section organized
54     // between interleaving and non-interleaving mode. To simplify this, we
55     // treat the non-interleaving as an interleaving with gap size = 0 and fu
56     // size = 1
57     isoino->fu_size = dir->fu_size;
58     isoino->gap_size = dir->gap_size;
59
60     u32_t fu_len = isoino->fu_size * ISO9660_BLKSZ;
61
62     inode->id = ino;
63     inode->lb_addr = dir->extent_addr;
64     inode->lb_usage = ICEIL(dir->data_size, fu_len);
65     inode->ops = &iso_inode_ops;
66     inode->default_fops = &iso_file_ops;
67
68     // xattr_len is in unit of FU. Each FU comprise <fu_sz> block(s).
69     inode->fsize = dir->data_size - dir->xattr_len * fu_len;
70
71     if ((dir->flags & ISO_FDIR)) {
72         inode->itype = VFS_IFDIR;
73     } else {
74         inode->itype = VFS_IFFILE;
75     }
76
77     if (dir->xattr_len) {
78         struct iso_xattr* xattr = (struct iso_xattr*)valloc(ISO9660_BLKSZ);
79         // Only bring in single FU, as we only care about the attributes.
80         errno = dev->ops.read(
81           dev, xattr, ISO9660_BLKSZ * inode->lb_addr, ISO9660_BLKSZ);
82         if (errno < 0) {
83             return EIO;
84         }
85         isoino->record_fmt = xattr->record_fmt;
86
87         inode->ctime = iso9660_dt2unix(&xattr->ctime);
88         inode->mtime = iso9660_dt2unix(&xattr->mtime);
89
90         inode->lb_addr += dir->xattr_len * dir->fu_size;
91
92         vfree(xattr);
93     }
94
95     inode->ctime = dir->ctime ? dir->ctime : inode->ctime;
96     inode->mtime = dir->mtime ? dir->mtime : inode->mtime;
97     inode->atime = dir->atime ? dir->atime : inode->atime;
98
99     return 0;
100 }