feat: (iso9660) rock ridge extension
[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/valloc.h>
5 #include <lunaix/spike.h>
6
7 static struct v_inode_ops iso_inode_ops = {
8     .dir_lookup = iso9660_dir_lookup,
9     .open = iso9660_open,
10 };
11
12 static struct v_file_ops iso_file_ops = { .close = iso9660_close,
13                                           .read = iso9660_read,
14                                           .read_page = iso9660_read,
15                                           .write = iso9660_write,
16                                           .write_page = iso9660_write,
17                                           .seek = iso9660_seek,
18                                           .readdir = iso9660_readdir };
19
20 void
21 iso9660_init_inode(struct v_superblock* vsb, struct v_inode* inode)
22 {
23     inode->data = vzalloc(sizeof(struct iso_inode));
24 }
25
26 int
27 iso9660_fill_inode(struct v_inode* inode, struct iso_drecache* dir, int ino)
28 {
29     int errno = 0;
30     struct device* dev = inode->sb->dev;
31     struct iso_inode* isoino = (struct iso_inode*)inode->data;
32
33     // In the spec, there is a differentiation in how file section organized
34     // between interleaving and non-interleaving mode. To simplify this, we
35     // treat the non-interleaving as an interleaving with gap size = 0 and fu
36     // size = 1
37     isoino->fu_size = dir->fu_size;
38     isoino->gap_size = dir->gap_size;
39
40     u32_t fu_len = isoino->fu_size * ISO9660_BLKSZ;
41
42     inode->id = ino;
43     inode->lb_addr = dir->extent_addr;
44     inode->ops = &iso_inode_ops;
45     inode->default_fops = &iso_file_ops;
46
47     // xattr_len is in unit of FU. Each FU comprise <fu_sz> block(s).
48     inode->fsize = dir->data_size - dir->xattr_len * fu_len;
49
50     if ((dir->flags & ISO_FDIR)) {
51         inode->itype = VFS_IFDIR;
52     } else {
53         inode->itype = VFS_IFFILE;
54     }
55
56     if (dir->xattr_len) {
57         struct iso_xattr* xattr = (struct iso_xattr*)valloc(ISO9660_BLKSZ);
58         // Only bring in single FU, as we only care about the attributes.
59         errno =
60           dev->read(dev, xattr, ISO9660_BLKSZ * inode->lb_addr, ISO9660_BLKSZ);
61         if (errno < 0) {
62             return EIO;
63         }
64         isoino->record_fmt = xattr->record_fmt;
65
66         inode->ctime = iso9660_dt2unix(&xattr->ctime);
67         inode->mtime = iso9660_dt2unix(&xattr->mtime);
68
69         inode->lb_addr += dir->xattr_len * dir->fu_size;
70
71         vfree(xattr);
72     }
73
74     inode->ctime = dir->ctime ? dir->ctime : inode->ctime;
75     inode->mtime = dir->mtime ? dir->mtime : inode->mtime;
76     inode->atime = dir->atime ? dir->atime : inode->atime;
77
78     return 0;
79 }