├── Kbuild ├── tests ├── image.iso.xz.bak ├── image_truncated.iso.xz.bak └── tests.sh ├── .gitignore ├── Makefile ├── xattr.c ├── file.c ├── inode.c ├── emu3_fs.h ├── README.md ├── super.c ├── dir.c └── COPYING /Kbuild: -------------------------------------------------------------------------------- 1 | obj-m += emu3_fs.o 2 | emu3_fs-y := super.o inode.o file.o dir.o xattr.o 3 | -------------------------------------------------------------------------------- /tests/image.iso.xz.bak: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dagargo/emu3fs/HEAD/tests/image.iso.xz.bak -------------------------------------------------------------------------------- /tests/image_truncated.iso.xz.bak: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dagargo/emu3fs/HEAD/tests/image_truncated.iso.xz.bak -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.tmp_versions/ 2 | .*.cmd 3 | *.o 4 | *.o.d 5 | *.ko 6 | modules.order 7 | Module.symvers 8 | *.mod.c 9 | .cache.mk 10 | emu3_fs.mod 11 | *~ 12 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ifneq ($(KERNELRELEASE),) 2 | include Kbuild 3 | 4 | else 5 | KDIR ?= /lib/modules/`uname -r`/build 6 | 7 | all: 8 | $(MAKE) -C $(KDIR) M=$$PWD 9 | 10 | clean: 11 | $(MAKE) -C $(KDIR) M=$$PWD clean 12 | rm -rf *~ 13 | 14 | format: 15 | indent -linux *.[ch] 16 | 17 | install: 18 | $(MAKE) -C $(KDIR) M=$$PWD modules_install 19 | endif 20 | -------------------------------------------------------------------------------- /xattr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * xattr.c 3 | * Copyright (C) 2021 David García Goñi 4 | * 5 | * This file is part of emu3fs. 6 | * 7 | * emu3fs is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * emu3fs is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with emu3fs. If not, see . 19 | */ 20 | 21 | #include 22 | #include "emu3_fs.h" 23 | 24 | #define EMU3_XATTR_BNUM "bank.number" 25 | #define EMU3_XATTR_BNUM_LEN_MAX 8 26 | 27 | ssize_t emu3_listxattr(struct dentry *dentry, char *buffer, size_t size) 28 | { 29 | return snprintf(buffer, size, "%s%s", XATTR_USER_PREFIX, 30 | EMU3_XATTR_BNUM) + 1; 31 | } 32 | 33 | static int emu3_xattr_get(const struct xattr_handler *handler, 34 | struct dentry *dentry, struct inode *inode, 35 | const char *name, void *buffer, size_t size) 36 | { 37 | int ret; 38 | struct emu3_inode *e3i; 39 | struct emu3_sb_info *info = EMU3_SB(inode->i_sb); 40 | 41 | if (strcmp(name, EMU3_XATTR_BNUM)) 42 | return -ENODATA; 43 | 44 | mutex_lock(&info->lock); 45 | e3i = EMU3_I(inode); 46 | ret = snprintf(buffer, size, "%d", e3i->data.id); 47 | mutex_unlock(&info->lock); 48 | 49 | return ret; 50 | } 51 | 52 | static int emu3_xattr_set(const struct xattr_handler *handler, 53 | struct mnt_idmap *idmap, struct dentry *dentry, 54 | struct inode *inode, const char *name, 55 | const void *buffer, size_t size, int flags) 56 | { 57 | long bn; 58 | int ret; 59 | struct buffer_head *b; 60 | struct emu3_dentry *e3d; 61 | struct emu3_inode *e3i; 62 | struct emu3_sb_info *info = EMU3_SB(inode->i_sb); 63 | char value[EMU3_XATTR_BNUM_LEN_MAX]; 64 | 65 | if (strcmp(name, EMU3_XATTR_BNUM)) 66 | return -ENODATA; 67 | 68 | if (size >= EMU3_XATTR_BNUM_LEN_MAX) { 69 | return -ERANGE; 70 | } 71 | 72 | strncpy(value, buffer, size); 73 | value[size] = '\0'; 74 | ret = kstrtoul(value, 0, &bn); 75 | if (ret) { 76 | return ret; 77 | } 78 | 79 | if (bn >= EMU3_MAX_FILES_PER_DIR) { 80 | return -ERANGE; 81 | } 82 | 83 | mutex_lock(&info->lock); 84 | e3i = EMU3_I(inode); 85 | e3i->data.id = bn; 86 | mark_inode_dirty(inode); 87 | e3d = emu3_find_dentry_by_inode(inode, &b); 88 | e3d->data.id = bn; 89 | mark_buffer_dirty_inode(b, inode); 90 | brelse(b); 91 | mutex_unlock(&info->lock); 92 | 93 | return ret; 94 | } 95 | 96 | static const struct xattr_handler emu3_xattr_handler = { 97 | .prefix = XATTR_USER_PREFIX, 98 | .get = emu3_xattr_get, 99 | .set = emu3_xattr_set 100 | }; 101 | 102 | const struct xattr_handler *emu3_xattr_handlers[] = { 103 | &emu3_xattr_handler, 104 | NULL 105 | }; 106 | -------------------------------------------------------------------------------- /file.c: -------------------------------------------------------------------------------- 1 | /* 2 | * file.c 3 | * Copyright (C) 2018 David García Goñi 4 | * 5 | * This file is part of emu3fs. 6 | * 7 | * emu3fs is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * emu3fs is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with emu3fs. If not, see . 19 | */ 20 | 21 | #include 22 | #include 23 | #include "emu3_fs.h" 24 | 25 | //Base 0 search 26 | static int emu3_expand_cluster_list(struct inode *inode, sector_t block) 27 | { 28 | struct emu3_sb_info *info = EMU3_SB(inode->i_sb); 29 | int cluster = ((int)block) / info->blocks_per_cluster; 30 | short next = EMU3_I_START_CLUSTER(inode); 31 | int new, i = 0; 32 | 33 | while (le16_to_cpu(info->cluster_list[next]) != EMU_LAST_FILE_CLUSTER) { 34 | next = le16_to_cpu(info->cluster_list[next]); 35 | i++; 36 | } 37 | while (i < cluster) { 38 | new = emu3_next_free_cluster(info); 39 | if (new < 0) 40 | return -ENOSPC; 41 | info->cluster_list[next] = cpu_to_le16(new); 42 | next = new; 43 | i++; 44 | } 45 | info->cluster_list[next] = cpu_to_le16(EMU_LAST_FILE_CLUSTER); 46 | return 0; 47 | } 48 | 49 | static int 50 | emu3_get_block(struct inode *inode, sector_t block, 51 | struct buffer_head *bh_result, int create) 52 | { 53 | sector_t phys; 54 | struct super_block *sb = inode->i_sb; 55 | struct emu3_inode *e3i = EMU3_I(inode); 56 | struct emu3_sb_info *info = EMU3_SB(sb); 57 | int err; 58 | 59 | phys = emu3_get_phys_block(inode, block); 60 | if (phys != -1) { 61 | map_bh(bh_result, sb, phys); 62 | return 0; 63 | } 64 | 65 | if (!create) 66 | return 0; 67 | 68 | mutex_lock(&info->lock); 69 | err = emu3_expand_cluster_list(inode, block); 70 | mutex_unlock(&info->lock); 71 | 72 | if (err) 73 | return err; 74 | else { 75 | phys = emu3_get_phys_block(inode, block); 76 | map_bh(bh_result, sb, phys); 77 | inode->i_blocks += info->blocks_per_cluster; 78 | e3i->data.fattrs.clusters++; 79 | } 80 | 81 | return 0; 82 | } 83 | 84 | static int emu3_read_folio(struct file *file, struct folio *folio) 85 | { 86 | return block_read_full_folio(folio, emu3_get_block); 87 | } 88 | 89 | static int emu3_writepages(struct address_space *mapping, 90 | struct writeback_control *wbc) 91 | { 92 | return mpage_writepages(mapping, wbc, emu3_get_block); 93 | } 94 | 95 | static int 96 | emu3_write_begin(struct file *file, struct address_space *mapping, 97 | loff_t pos, unsigned len, struct folio **foliop, void **fsdata) 98 | { 99 | return block_write_begin(mapping, pos, len, foliop, emu3_get_block); 100 | } 101 | 102 | static sector_t emu3_bmap(struct address_space *mapping, sector_t block) 103 | { 104 | return generic_block_bmap(mapping, block, emu3_get_block); 105 | } 106 | 107 | static int emu3_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 108 | struct iattr *attr) 109 | { 110 | struct inode *inode = d_inode(dentry); 111 | struct emu3_sb_info *info = EMU3_SB(inode->i_sb); 112 | struct emu3_inode *e3i = EMU3_I(inode); 113 | blkcnt_t blocks; 114 | int err; 115 | 116 | err = setattr_prepare(&nop_mnt_idmap, dentry, attr); 117 | if (err) 118 | return err; 119 | 120 | if ((attr->ia_valid & ATTR_SIZE) && attr->ia_size != i_size_read(inode)) { 121 | err = inode_newsize_ok(inode, attr->ia_size); 122 | if (err) 123 | return err; 124 | 125 | truncate_setsize(inode, attr->ia_size); 126 | mutex_lock(&info->lock); 127 | emu3_set_fattrs(info, &e3i->data.fattrs, attr->ia_size); 128 | emu3_prune_cluster_list(inode); 129 | blocks = e3i->data.fattrs.clusters * info->blocks_per_cluster; 130 | mutex_unlock(&info->lock); 131 | 132 | inode->i_blocks = blocks; 133 | } 134 | setattr_copy(&nop_mnt_idmap, inode, attr); 135 | mark_inode_dirty(inode); 136 | 137 | return 0; 138 | } 139 | 140 | const struct address_space_operations emu3_aops = { 141 | .read_folio = emu3_read_folio, 142 | .writepages = emu3_writepages, 143 | .write_begin = emu3_write_begin, 144 | .write_end = generic_write_end, 145 | .bmap = emu3_bmap, 146 | }; 147 | 148 | const struct file_operations emu3_file_operations_file = { 149 | .llseek = generic_file_llseek, 150 | .read_iter = generic_file_read_iter, 151 | .write_iter = generic_file_write_iter, 152 | .mmap = generic_file_mmap, 153 | .splice_read = filemap_splice_read, 154 | .fsync = generic_file_fsync 155 | }; 156 | 157 | const struct inode_operations emu3_inode_operations_file = { 158 | .listxattr = emu3_listxattr, 159 | .setattr = emu3_setattr, 160 | }; 161 | -------------------------------------------------------------------------------- /inode.c: -------------------------------------------------------------------------------- 1 | /* 2 | * inode.c 3 | * Copyright (C) 2018 David García Goñi 4 | * 5 | * This file is part of emu3fs. 6 | * 7 | * emu3fs is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * emu3fs is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with emu3fs. If not, see . 19 | */ 20 | 21 | #include 22 | #include 23 | 24 | #include "emu3_fs.h" 25 | 26 | inline void emu3_set_emu3_inode_data(struct inode *inode, 27 | struct emu3_dentry *e3d) 28 | { 29 | struct emu3_inode *e3i = EMU3_I(inode); 30 | memcpy(&e3i->data, &e3d->data, sizeof(struct emu3_dentry_data)); 31 | } 32 | 33 | inline void emu3_set_i_map(struct emu3_sb_info *info, 34 | struct inode *inode, unsigned int dnum) 35 | { 36 | info->i_maps[inode->i_ino - EMU3_I_ID_MAP_OFFSET] = dnum; 37 | } 38 | 39 | inline unsigned int emu3_get_i_map(struct emu3_sb_info *info, 40 | struct inode *inode) 41 | { 42 | return info->i_maps[inode->i_ino - EMU3_I_ID_MAP_OFFSET]; 43 | } 44 | 45 | inline void emu3_clear_i_map(struct emu3_sb_info *info, struct inode *inode) 46 | { 47 | info->i_maps[inode->i_ino - EMU3_I_ID_MAP_OFFSET] = 0; 48 | } 49 | 50 | unsigned long emu3_get_or_add_i_map(struct emu3_sb_info *info, 51 | unsigned int dnum) 52 | { 53 | int i, pos; 54 | bool found; 55 | unsigned int *empty; 56 | unsigned int *v; 57 | 58 | empty = NULL; 59 | found = 0; 60 | v = info->i_maps; 61 | for (i = 0; i < EMU3_TOTAL_ENTRIES(info); i++, v++) { 62 | if ((*v) == dnum) { 63 | found = 1; 64 | break; 65 | } 66 | 67 | if (!(*v) && !empty) { 68 | empty = v; 69 | pos = i; 70 | } 71 | } 72 | 73 | if (!found) 74 | *empty = dnum; 75 | 76 | return (found ? i : pos) + EMU3_I_ID_MAP_OFFSET; 77 | } 78 | 79 | struct emu3_dentry *emu3_find_dentry_by_inode(struct inode *inode, 80 | struct buffer_head **b) 81 | { 82 | struct emu3_dentry *e3d; 83 | struct emu3_sb_info *info = EMU3_SB(inode->i_sb); 84 | unsigned int dnum = emu3_get_i_map(info, inode); 85 | unsigned int blknum = EMU3_DNUM_BLKNUM(dnum); 86 | unsigned int offset = EMU3_DNUM_OFFSET(dnum); 87 | 88 | *b = sb_bread(inode->i_sb, blknum); 89 | 90 | e3d = (struct emu3_dentry *)(*b)->b_data; 91 | e3d += offset; 92 | 93 | return e3d; 94 | } 95 | 96 | static void emu3_set_inode_size_dir(struct inode *inode) 97 | { 98 | struct emu3_inode *e3i = EMU3_I(inode); 99 | unsigned int i; 100 | short blknum, *block = e3i->data.dattrs.block_list; 101 | 102 | for (i = 0; i < EMU3_BLOCKS_PER_DIR; i++, block++) { 103 | blknum = le16_to_cpu(*block); 104 | if (EMU3_IS_DIR_BLOCK_FREE(blknum)) 105 | break; 106 | } 107 | inode->i_blocks = i; 108 | inode->i_size = inode->i_blocks * EMU3_BSIZE; 109 | } 110 | 111 | static void emu3_set_inode_size_file(struct inode *inode) 112 | { 113 | struct emu3_sb_info *info = EMU3_SB(inode->i_sb); 114 | struct emu3_inode *e3i = EMU3_I(inode); 115 | short clusters = cpu_to_le16(e3i->data.fattrs.clusters); 116 | short blocks = cpu_to_le16(e3i->data.fattrs.blocks); 117 | short bytes = cpu_to_le16(e3i->data.fattrs.bytes); 118 | 119 | if (blocks > info->blocks_per_cluster) { 120 | printk(KERN_CRIT "%s: Bad data in inode %ld\n", 121 | EMU3_MODULE_NAME, inode->i_ino); 122 | } 123 | inode->i_blocks = clusters * info->blocks_per_cluster; 124 | 125 | if (clusters == 1 && blocks == 1 && bytes == 0) 126 | inode->i_size = 0; 127 | else { 128 | if (blocks > 1) 129 | clusters--; 130 | if (bytes) 131 | blocks--; 132 | inode->i_size = 133 | (clusters * info->blocks_per_cluster + 134 | blocks) * EMU3_BSIZE + bytes; 135 | } 136 | } 137 | 138 | struct inode *emu3_get_inode(struct super_block *sb, unsigned long ino) 139 | { 140 | umode_t mode; 141 | unsigned int links; 142 | struct inode *inode; 143 | struct timespec64 tv; 144 | struct emu3_dentry *e3d; 145 | struct buffer_head *b; 146 | const struct inode_operations *iops; 147 | const struct file_operations *fops; 148 | struct emu3_sb_info *info = EMU3_SB(sb); 149 | 150 | inode = iget_locked(sb, ino); 151 | 152 | if (IS_ERR(inode)) 153 | return ERR_PTR(-ENOMEM); 154 | 155 | if (!(inode->i_state & I_NEW)) 156 | return inode; 157 | 158 | if (EMU3_IS_I_ROOT_DIR(inode)) { 159 | inode->i_blocks = info->root_blocks; 160 | inode->i_size = info->root_blocks * EMU3_BSIZE; 161 | iops = &emu3_inode_operations_dir; 162 | fops = &emu3_file_operations_dir; 163 | links = 2; 164 | mode = EMU3_ROOT_DIR_MODE; 165 | } else { 166 | e3d = emu3_find_dentry_by_inode(inode, &b); 167 | 168 | if (!e3d) 169 | return ERR_PTR(-EIO); 170 | 171 | emu3_set_emu3_inode_data(inode, e3d); 172 | brelse(b); 173 | 174 | if (EMU3_DENTRY_IS_FILE(e3d)) { 175 | emu3_set_inode_size_file(inode); 176 | iops = &emu3_inode_operations_file; 177 | fops = &emu3_file_operations_file; 178 | links = 1; 179 | mode = EMU3_FILE_MODE; 180 | inode->i_mapping->a_ops = &emu3_aops; 181 | } else if (EMU3_DENTRY_IS_DIR(e3d)) { 182 | emu3_set_inode_size_dir(inode); 183 | iops = &emu3_inode_operations_dir; 184 | fops = &emu3_file_operations_dir; 185 | links = 2; 186 | mode = EMU3_DIR_MODE; 187 | } else { 188 | printk(KERN_ERR 189 | "%s: entry is neither a file nor a directory\n", 190 | EMU3_MODULE_NAME); 191 | brelse(b); 192 | return ERR_PTR(-EIO); 193 | } 194 | } 195 | 196 | if (mode & S_IFDIR) 197 | inode->i_opflags &= ~IOP_XATTR; 198 | if (mode & S_IFREG) 199 | inode->i_opflags |= IOP_XATTR; 200 | 201 | inode->i_mode = mode; 202 | inode->i_uid = current_fsuid(); 203 | inode->i_gid = current_fsgid(); 204 | set_nlink(inode, links); 205 | inode->i_op = iops; 206 | inode->i_fop = fops; 207 | tv = inode_set_ctime_current(inode); 208 | inode_set_mtime_to_ts(inode, tv); 209 | inode_set_ctime_to_ts(inode, tv); 210 | 211 | unlock_new_inode(inode); 212 | 213 | return inode; 214 | } 215 | -------------------------------------------------------------------------------- /emu3_fs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * emu3_fs.h 3 | * Copyright (C) 2018 David García Goñi 4 | * 5 | * This file is part of emu3fs. 6 | * 7 | * emu3fs is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * emu3fs is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with emu3fs. If not, see . 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #define EMU3_MODULE_NAME "emu3fs" 30 | 31 | #define EMU3_FS_SIGNATURE "EMU3" 32 | #define EMU3_FS_TYPE 0x454d5533 33 | 34 | #define EMU3_BSIZE_BITS 9 35 | #define EMU3_BSIZE (1 << EMU3_BSIZE_BITS) 36 | #define EMU3_CLUSTER_ENTRIES_PER_BLOCK (EMU3_BSIZE >> 1) 37 | 38 | #define EMU3_I_ID_ROOT_DIR 1 //Any value is valid as long as is lower than the first inode ID. 39 | #define EMU3_I_ID_MAP_OFFSET (EMU3_I_ID_ROOT_DIR + 1) //As inodes are mapped to emu3 dentries in an array, we need to add an offset greater than EMU3_ROOT_DIR_I_ID. 40 | 41 | #define EMU3_SB(sb) ((struct emu3_sb_info *)(sb)->s_fs_info) 42 | 43 | #define EMU3_I(inode) ((struct emu3_inode *)container_of((inode), struct emu3_inode, vfs_inode)) 44 | #define EMU3_I_START_CLUSTER(inode) (le16_to_cpu(EMU3_I(inode)->data.fattrs.start_cluster)) 45 | 46 | #define EMU3_DNUM_OFFSET_SIZE 4 47 | #define EMU3_DNUM_OFFSET_MASK ((1 << EMU3_DNUM_OFFSET_SIZE) - 1) 48 | #define EMU3_DNUM(blknum, offset) ((unsigned int)((blknum) << EMU3_DNUM_OFFSET_SIZE) | ((offset) & EMU3_DNUM_OFFSET_MASK)) 49 | #define EMU3_DNUM_BLKNUM(dnum) ((dnum) >> EMU3_DNUM_OFFSET_SIZE) 50 | #define EMU3_DNUM_OFFSET(dnum) ((dnum) & EMU3_DNUM_OFFSET_MASK) 51 | 52 | #define EMU_LAST_FILE_CLUSTER ((short)0x7fff) 53 | 54 | #define EMU3_BLOCKS_PER_DIR 7 55 | 56 | #define EMU3_LENGTH_FILENAME 16 57 | 58 | #define EMU3_ENTRIES_PER_BLOCK (EMU3_BSIZE / (sizeof(struct emu3_dentry))) 59 | 60 | #define EMU3_TOTAL_ENTRIES(info) (((info)->root_blocks + (info)->dir_content_blocks) * EMU3_ENTRIES_PER_BLOCK) 61 | 62 | //For devices, this should be 102, 100 regular banks + 2 special rom files with fixed ids at 0x6b and 0x6d. 63 | //We use the maximum physically allowed. 64 | #define EMU3_MAX_FILES_PER_DIR (EMU3_ENTRIES_PER_BLOCK * EMU3_BLOCKS_PER_DIR) 65 | #define EMU3_MAX_REGULAR_FILE 100 //Not used 66 | 67 | #define EMU3_FTYPE_DEL 0x00 //Deleted file 68 | #define EMU3_FTYPE_STD 0x81 69 | #define EMU3_FTYPE_UPD 0x83 //Used by the first file after a deleted file 70 | #define EMU3_FTYPE_SYS 0x80 71 | 72 | #define EMU3_DTYPE_1 0x40 73 | #define EMU3_DTYPE_2 0x80 74 | 75 | #define EMU3_IS_I_ROOT_DIR(inode) ((inode)->i_ino == EMU3_I_ID_ROOT_DIR) 76 | 77 | #define EMU3_IS_I_REG_DIR(dir, info) (((emu3_get_i_map(info, dir)) >= EMU3_DNUM((info)->start_root_block, 0)) && \ 78 | ((emu3_get_i_map(info, dir)) < EMU3_DNUM((info)->start_dir_content_block, 0))) 79 | 80 | #define EMU3_DENTRY_IS_FILE(e3d) (((e3d)->data.id >= 0) && \ 81 | ((e3d)->data.id < EMU3_MAX_FILES_PER_DIR) && \ 82 | ((e3d)->data.fattrs.clusters > 0) && \ 83 | ( \ 84 | (e3d)->data.fattrs.type == EMU3_FTYPE_STD || \ 85 | (e3d)->data.fattrs.type == EMU3_FTYPE_UPD || \ 86 | (e3d)->data.fattrs.type == EMU3_FTYPE_SYS \ 87 | ) \ 88 | ) 89 | 90 | #define EMU3_DENTRY_IS_DIR(e3d) (((e3d)->data.id == EMU3_DTYPE_1 || (e3d)->data.id == EMU3_DTYPE_2) && \ 91 | (le16_to_cpu((e3d)->data.dattrs.block_list[0]) > 0)) 92 | 93 | #define EMU3_DIR_BLOCK_OK(block, info) ((block) >= info->start_dir_content_block && (block) < info->start_data_block) 94 | 95 | #define EMU3_COMMON_MODE (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR) 96 | #define EMU3_DIR_MODE_ (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH) 97 | #define EMU3_FILE_MODE_ (S_IFREG) 98 | #define EMU3_ROOT_DIR_MODE (EMU3_COMMON_MODE | EMU3_DIR_MODE_| S_IWGRP | S_IWOTH) 99 | #define EMU3_DIR_MODE (EMU3_COMMON_MODE | EMU3_DIR_MODE_) 100 | #define EMU3_FILE_MODE (EMU3_COMMON_MODE | EMU3_FILE_MODE_) 101 | 102 | #define EMU3_FREE_DIR_BLOCK (-1) 103 | #define EMU3_IS_DIR_BLOCK_FREE(block) ((block) == EMU3_FREE_DIR_BLOCK) 104 | 105 | #define EMU3_FILE_PROPS_LEN 5 106 | 107 | #define EMU3_ERR_NOT_BLK "%s: block %d not available\n" 108 | 109 | struct emu3_sb_info { 110 | unsigned int blocks; 111 | unsigned int start_root_block; 112 | unsigned int root_blocks; 113 | unsigned int start_dir_content_block; 114 | unsigned int dir_content_blocks; 115 | unsigned int start_cluster_list_block; 116 | unsigned int cluster_list_blocks; 117 | unsigned int start_data_block; 118 | unsigned int blocks_per_cluster; 119 | unsigned int clusters; 120 | unsigned char cluster_size_shift; //Cluster size always a power of 2 121 | short *cluster_list; 122 | bool *dir_content_block_list; 123 | unsigned int *i_maps; 124 | struct mutex lock; 125 | bool emu4; 126 | }; 127 | 128 | struct emu3_file_attrs { 129 | unsigned short start_cluster; 130 | unsigned short clusters; 131 | unsigned short blocks; 132 | unsigned short bytes; 133 | unsigned char type; 134 | unsigned char props[EMU3_FILE_PROPS_LEN]; 135 | }; 136 | 137 | struct emu3_dir_attrs { 138 | short block_list[EMU3_BLOCKS_PER_DIR]; 139 | }; 140 | 141 | struct emu3_dentry_data { 142 | unsigned char unknown; 143 | unsigned char id; //This can be 0. No inode id in linux can be 0. 144 | union { 145 | struct emu3_file_attrs fattrs; 146 | struct emu3_dir_attrs dattrs; 147 | }; 148 | }; 149 | 150 | struct emu3_dentry { 151 | char name[EMU3_LENGTH_FILENAME]; 152 | struct emu3_dentry_data data; 153 | }; 154 | 155 | struct emu3_inode { 156 | struct inode vfs_inode; 157 | struct emu3_dentry_data data; 158 | }; 159 | 160 | extern const struct file_operations emu3_file_operations_dir; 161 | 162 | extern const struct inode_operations emu3_inode_operations_dir; 163 | 164 | extern const struct file_operations emu3_file_operations_file; 165 | 166 | extern const struct inode_operations emu3_inode_operations_file; 167 | 168 | extern const struct address_space_operations emu3_aops; 169 | 170 | extern const struct xattr_handler *emu3_xattr_handlers[]; 171 | 172 | struct inode *emu3_get_inode(struct super_block *, unsigned long); 173 | 174 | int emu3_next_free_cluster(struct emu3_sb_info *); 175 | 176 | void emu3_init_cluster_list(struct inode *); 177 | 178 | int emu3_get_cluster(struct inode *, int); 179 | 180 | sector_t emu3_get_phys_block(struct inode *, sector_t); 181 | 182 | struct emu3_dentry *emu3_find_dentry_by_inode(struct inode *, 183 | struct buffer_head **); 184 | 185 | unsigned long emu3_get_or_add_i_map(struct emu3_sb_info *, unsigned int); 186 | 187 | unsigned int emu3_get_i_map(struct emu3_sb_info *, struct inode *); 188 | 189 | void emu3_clear_i_map(struct emu3_sb_info *, struct inode *); 190 | 191 | void emu3_set_i_map(struct emu3_sb_info *, struct inode *, unsigned int); 192 | 193 | void emu3_set_emu3_inode_data(struct inode *, struct emu3_dentry *); 194 | 195 | ssize_t emu3_listxattr(struct dentry *, char *, size_t); 196 | 197 | void emu3_free_dir_content_block(struct emu3_sb_info *, short); 198 | 199 | void emu3_use_dir_content_block(struct emu3_sb_info *, short); 200 | 201 | short emu3_get_free_dir_content_blknum(struct emu3_sb_info *); 202 | 203 | void emu3_set_fattrs(struct emu3_sb_info *, struct emu3_file_attrs *, loff_t); 204 | 205 | void emu3_init_fattrs(struct emu3_sb_info *, struct emu3_file_attrs *, short); 206 | 207 | void emu3_set_inode_blocks(struct inode *, struct emu3_file_attrs *); 208 | 209 | void emu3_prune_cluster_list(struct inode *); 210 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # emu3fs 2 | 3 | emu3fs is a Linux kernel module that allows you to read from and write to block devices formatted as E-Mu EIII filesystem. The Emulator samplers using this filesystem are the EIII series, the ESI series (which belongs to the EIII series) and the EIV series. 4 | 5 | Currently, it has been verified to work with CDs, Zip drives and SD cards (used with [SCSI2SD](http://www.codesrc.com/mediawiki/index.php/SCSI2SD) and limited to 14 GB because that is the biggest size supported by the ESI 3.02 OS). 6 | 7 | ## Installation 8 | 9 | Simply run `make && sudo make install && sudo depmod`. Note that you will need the Linux kernel headers to compile the module. 10 | 11 | As emu3fs is outside the kernel tree, the `master` branch should not be used when compiling. Instead, the tag matching the kernel version the module is going be compiled against to should be checked out. If there is not a tag for such version, the closest ones might work as the API could be the same. Fixes and improvements implemented later would not be available for prior tags. 12 | 13 | ## Usage 14 | 15 | Once the module is inserted into the kernel (you might use `sudo modprobe emu3_fs` for this), you can mount the devices. You have 2 options here. 16 | 17 | * Mount an EIII block device with `sudo mount -t emu3 device mountpoint`. 18 | 19 | * Mount an EIV block device with `sudo mount -t emu4 device mountpoint`. 20 | 21 | There are no differences between these two filesystems at the structure level. The only difference is that the root node is either the first directory on disk or the root directory respectively. The reason behind this is that EIV series allow to have directories, or folders as they call it, at the root level while older devices only give the user access to the first directory. Hence, it is possible but no recommended to mount an EIII disk as an `emu4` or vice versa. 22 | 23 | If the filesystem type is not provided, `emu3` is used as default. 24 | 25 | If you get the error below, use the `-t` option. 26 | 27 | ``` 28 | NTFS signature is missing. 29 | Failed to mount '/dev/loop0': Invalid argument 30 | The device '/dev/loop0' doesn't seem to have a valid NTFS. 31 | Maybe the wrong device is used? Or the whole disk instead of a 32 | partition (e.g. /dev/sda, not /dev/sda1)? Or the other way around? 33 | ``` 34 | 35 | ### Mounting ISO images 36 | 37 | ISO images can be accessed through loop devices. In this example, we are using the `loop0` device. 38 | 39 | ``` 40 | $ sudo losetup /dev/loop0 image 41 | ``` 42 | 43 | ### Mounting CDs and other drives 44 | 45 | CDs need to be mounted through a loop device when the CD reader is not SCSI. This is due to the fact that the EIII filesystem uses a 512 B block size, which is allowed in SCSI drives, and non SCSI drives have usually a 2 KiB block size. Notice that this applies as well to other drives that are not capable of providing 512 B block size. 46 | If you are using a 512 B block size capable drive, like the `/dev/cdrom`, just do the following and forget about the loop devices. 47 | 48 | ``` 49 | $ sudo mount -t emu3 /dev/cdrom mountpoint 50 | ``` 51 | 52 | ### Mounting SCSI2SD partitions 53 | 54 | SCSI2SD does not store partition information on the card. Therefore, mounting the card only allows to see the first partition. Nevertheless, `losetup` allows to mount arbitraty portions of the card. 55 | 56 | First, we need to know the starting points of each partition, either from a SCSI2SD configuration file, or from SCI2SD itself through `sci2sd-util`. 57 | 58 | For example, a 16GB card could be splitted in 4 portions, which would give 4 offsets: 0, 7733504, 15467008 and 23200512 sectors. In order to calculate the offset for the `losetup` command, offsets in bytes have either to be multiplied the sector number by 512 or divide it by 2 in order to have the offset in KiB. 59 | 60 | In this example, that will be 0, 3866752K, 7733504K and 11600256K. 61 | 62 | Next, we need to know the first available /dev/loop. 63 | 64 | ``` 65 | $ losetup -f 66 | /dev/loop20 67 | ``` 68 | 69 | Then, we can create the loop devices for our partitions. 70 | 71 | ``` 72 | $ sudo losetup /dev/loop20 /dev/sdb 73 | $ sudo losetup -o 3866752K /dev/loop21 /dev/sdb 74 | $ sudo losetup -o 7733504K /dev/loop22 /dev/sdb 75 | $ sudo losetup -o 11600256K /dev/loop23 /dev/sdb 76 | ``` 77 | 78 | And, finally, can be mount the loop devices as usual. 79 | 80 | ## Bank numbers 81 | 82 | The bank number is part of the structure stored on the device but it is **not** a part of the name. When a file is created, the lowest bank number available is used; when a file is deleted, the bank number it was using becomes available. 83 | 84 | While any command line tool will work for listing the content, there is no way to show the bank number with any of them as it is not an stardard file attribute. However, it is an extended file attribute and, therefore, it is possible to read and write it with `getfattr` and `setfattr`. 85 | 86 | This is how it works. 87 | 88 | ``` 89 | $ getfattr -d -m ".*" * 90 | # file: 12 String Guitar 91 | user.bank.number="31" 92 | 93 | # file: 4 Piece Horns 4M 94 | user.bank.number="8" 95 | 96 | # file: E3 Main Code 97 | user.bank.number="109" 98 | [...] 99 | 100 | $ getfattr -n "user.bank.number" Textural\ Strings 101 | # file: Textural Strings 102 | user.bank.number="6" 103 | 104 | $ setfattr -n "user.bank.number" -v "99" Textural\ Strings 105 | 106 | $ getfattr -n "user.bank.number" Textural\ Strings 107 | # file: Textural Strings 108 | user.bank.number="99" 109 | ``` 110 | 111 | Keep in mind that setting a bank number does **not** alter the remaining ones so attention must be paid for repeated numbers as devices will show **only** the first one they find for a given bank number. 112 | 113 | Alternatively, an `lsemu3` command could be defined as follows. 114 | 115 | ``` 116 | #!/bin/bash 117 | 118 | if [ $# -eq 0 ]; then 119 | lsemu3 * .* 120 | exit $? 121 | fi 122 | 123 | if [ $# -eq 1 ] && [ "$1" == "-s" ]; then 124 | lsemu3 * | sort 125 | exit ${PIPESTATUS[0]} 126 | fi 127 | 128 | if [ $# -gt 1 ] && [ "$1" == "-s" ]; then 129 | shift 130 | lsemu3 "$@" | sort 131 | exit ${PIPESTATUS[0]} 132 | fi 133 | 134 | e=0 135 | t=0 136 | for f in "$@"; do 137 | t=$((t + 1)) 138 | [ ! -e "$f" ] && echo "'$f' does not exist" >&2 && e=$((e + 1)) && continue 139 | if [ -f "$f" ]; then 140 | bn=$(getfattr -d -m user.bank.number "$f" 2> /dev/null | grep -v "^#" | awk -F\" '{print $2}'); 141 | if [ -n "$bn" ]; then 142 | if [ $bn -lt 100 ]; then 143 | bn=$(printf " B%02d " $bn) 144 | else 145 | bn=$(printf ".%3d " $bn) 146 | fi 147 | else 148 | bn="F" 149 | fi 150 | elif [ -d "$f" ]; then 151 | bn="D" 152 | else 153 | bn="?" 154 | fi 155 | 156 | i=$(stat --printf="%i" "$f") 157 | s=$(stat --printf="%s" "$f") 158 | if [ $s -gt 1024 ]; then 159 | s=$((s / 1024)) 160 | if [ $s -gt 1024 ]; then 161 | s=$((s / 1024))M 162 | else 163 | s=${s}K 164 | fi 165 | else 166 | s=${s}B 167 | fi 168 | printf "%4s %9s %6s '$f'\n" $bn $i "$s" 169 | done 170 | 171 | [ $e -gt 0 ] && [ $e -eq $t ] && exit 1 172 | exit 0 173 | ``` 174 | 175 | This is how it works. The `-s` option sorts them by bank number and the second and third columns are the inode and the size respectively. 176 | 177 | ``` 178 | $ lsemu3 -s 179 | B00 5 1K 'E-mu Banks 1-44' 180 | B01 4 3M 'Full Arco String' 181 | B02 6 4M 'SecViolinTrils4M' 182 | B03 7 8M 'SecViolinTrils8M' 183 | B04 8 2M 'Solo Violin' 184 | [...] 185 | B42 46 3M 'StereoGrandPiano' 186 | B43 47 3M 'Flautas Bonita' 187 | B44 48 3M 'Tenor Sax' 188 | B99 10 3M 'Textural Strings' 189 | .109 3 64K 'E3 Main Code' 190 | ``` 191 | 192 | This helps to detect banks with the same number and it is useful when reordering banks. Files with bank number greater or equal than 100 are not considered banks but they are still there. 193 | 194 | ## About repeated filenames 195 | 196 | Remember that although Unix does **not allow** files with the same name in the same directory, the samplers **do allow** this and thus some commands might seem to behave strangely so try to avoid this scenario. In Unix, paths are unique and point to a single inode. 197 | 198 | ``` 199 | Default Folder$ $ ls -li 200 | total 32 201 | 4 -rw-r--r-- 1 user user 10738 ago 30 19:54 'Untitled Bank' 202 | 4 -rw-r--r-- 1 user user 10738 ago 30 19:54 'Untitled Bank' 203 | 4 -rw-r--r-- 1 user user 10738 ago 30 19:54 'Untitled Bank' 204 | ``` 205 | 206 | Listing the bank number does not work either. 207 | 208 | ``` 209 | Default Folder$ lsemu3 210 | B00 4 10738 'Untitled Bank' 211 | B00 4 10738 'Untitled Bank' 212 | B00 4 10738 'Untitled Bank' 213 | ``` 214 | 215 | However, it can be addressed easily although you still can not tell them apart. 216 | 217 | ``` 218 | Default Folder$ mv Untitled\ Bank Untitled\ Bank\ 2 219 | 220 | Default Folder$ ls -li 221 | total 32 222 | 5 -rw-r--r-- 1 user user 10738 ago 30 19:55 'Untitled Bank' 223 | 5 -rw-r--r-- 1 user user 10738 ago 30 19:55 'Untitled Bank' 224 | 4 -rw-r--r-- 1 user user 10738 ago 30 19:54 'Untitled Bank 2' 225 | 226 | Default Folder$ mv Untitled\ Bank Untitled\ Bank\ 3 227 | 228 | Default Folder$ ls -li 229 | total 32 230 | 6 -rw-r--r-- 1 user user 10738 ago 30 20:56 'Untitled Bank' 231 | 5 -rw-r--r-- 1 user user 10738 ago 30 19:55 'Untitled Bank 2' 232 | 4 -rw-r--r-- 1 user user 10738 ago 30 19:54 'Untitled Bank 3' 233 | 234 | Default Folder$ lsemu3 -s 235 | B00 4 10738 'Untitled Bank 2' 236 | B01 5 10738 'Untitled Bank 3' 237 | B02 6 10738 'Untitled Bank' 238 | ``` 239 | 240 | ## Testing 241 | 242 | You can run some simple tests from the `tests` directory. The script mounts a clean image and run some commands on it. **Be aware that you will be asked for the root password** because some commands like `mount` requiere this. 243 | 244 | 245 | ``` 246 | $ ./tests.sh 247 | ``` 248 | 249 | ## Related project 250 | 251 | [emu3bm](https://github.com/dagargo/emu3bm) is a EIII and EIV bank manager that allows a basic edition of presets and sample export and import. 252 | -------------------------------------------------------------------------------- /tests/tests.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | [ -z "$EMU3_TEST_DEBUG" ] && EMU3_TEST_DEBUG=0 4 | 5 | EMU3_MOUNTPOINT=mountpoint 6 | 7 | LANG=C 8 | 9 | function cleanUp() { 10 | echo "Cleaning up..." 11 | sudo umount -f $EMU3_MOUNTPOINT 12 | rmdir $EMU3_MOUNTPOINT 13 | sudo losetup -d /dev/loop0 14 | rm -f image.iso image_truncated.iso 15 | } 16 | 17 | function logAndRun() { 18 | echo "Running '$*'..." 19 | out=$(eval $*) 20 | err=$? 21 | [ -n "$out" ] && echo $out 22 | return $err 23 | } 24 | 25 | function printTest() { 26 | printf "\033[1;34m*** Test: $* ***\033[0m\n" 27 | echo "emu3fs: *** Test: $* ***" | sudo tee /dev/kmsg 28 | echo 29 | } 30 | 31 | function testCommon() { 32 | [ $1 -eq 1 ] && ok=$((ok+1)) 33 | total=$((total+1)) 34 | [ $EMU3_TEST_DEBUG -eq 1 ] && [ -n "$2" ] && echo "Listing '$EMU3_MOUNTPOINT/$2'..." && ls -lai $EMU3_MOUNTPOINT/$2 35 | if [ $1 -eq 1 ]; then 36 | printf "\033[0;32m" 37 | else 38 | printf "\033[0;31m" 39 | fi 40 | printf "Results: $ok/$total\033[0m\n\n" 41 | [ $1 -ne 1 ] && echo "emu3fs: Test error: $(date)" | sudo tee /dev/kmsg && sudo dmesg | tail -n 20 && cleanUp && exit 1 42 | } 43 | 44 | function test() { 45 | [ $? -eq 0 ] && this=1 || this=0 46 | testCommon $this $1 47 | } 48 | 49 | function testError() { 50 | [ $? -ne 0 ] && this=1 || this=0 51 | testCommon $this $1 52 | } 53 | 54 | total=0 55 | ok=0 56 | mkdir $EMU3_MOUNTPOINT 57 | 58 | echo "Inserting module..." 59 | logAndRun sudo modprobe -r emu3_fs 60 | logAndRun sudo modprobe emu3_fs 61 | echo 62 | 63 | echo "Uncompressing image..." 64 | logAndRun cp image.iso.xz.bak image.iso.xz 65 | logAndRun sudo rm -f image.iso 66 | logAndRun unxz image.iso.xz 67 | logAndRun sudo losetup /dev/loop0 image.iso 68 | echo 69 | 70 | printTest "Very basic emu3 testing" 71 | 72 | echo "Mounting image as emu3..." 73 | logAndRun sudo mount -t emu3 /dev/loop0 $EMU3_MOUNTPOINT 74 | test . 75 | logAndRun mkdir $EMU3_MOUNTPOINT/foo 76 | testError . 77 | logAndRun 'echo "123" > $EMU3_MOUNTPOINT/t1' 78 | logAndRun cat $EMU3_MOUNTPOINT/t1 79 | test . 80 | logAndRun '[ "123" == "$out" ]' 81 | test 82 | logAndRun 'rm $EMU3_MOUNTPOINT/t1' 83 | test 84 | logAndRun 'ls $EMU3_MOUNTPOINT/t1' 85 | testError 86 | logAndRun sudo umount $EMU3_MOUNTPOINT 87 | test 88 | 89 | printTest "mkdir and rmdir" 90 | 91 | echo "Mounting image as emu4..." 92 | sudo mount -t emu4 /dev/loop0 $EMU3_MOUNTPOINT 93 | test . 94 | logAndRun mkdir $EMU3_MOUNTPOINT/foo 95 | test . 96 | 97 | logAndRun mkdir $EMU3_MOUNTPOINT/foo 98 | testError . 99 | 100 | logAndRun rmdir $EMU3_MOUNTPOINT/foo 101 | test . 102 | 103 | logAndRun mkdir $EMU3_MOUNTPOINT/foo 104 | test . 105 | 106 | printTest "File creation and basic edition" 107 | 108 | logAndRun touch $EMU3_MOUNTPOINT/foo/t0 109 | logAndRun '[ 1024 -eq $(stat --print "%b" $EMU3_MOUNTPOINT/foo/t0) ]' 110 | test 111 | 112 | logAndRun 'echo "123" > $EMU3_MOUNTPOINT/foo/t1' 113 | test 114 | logAndRun cat $EMU3_MOUNTPOINT/foo/t1 115 | logAndRun '[ "123" == "$out" ]' 116 | test 117 | logAndRun 'echo "4567" >> $EMU3_MOUNTPOINT/foo/t1' 118 | test foo/t1 119 | logAndRun cat $EMU3_MOUNTPOINT/foo/t1 120 | test 121 | logAndRun '[ 123$'\''\n'\''4567 == "$out" ]' 122 | test 123 | 124 | printTest "cp" 125 | 126 | logAndRun cp $EMU3_MOUNTPOINT/foo/t1 $EMU3_MOUNTPOINT/foo/t3 127 | test 128 | logAndRun ls -l $EMU3_MOUNTPOINT/foo/t1 129 | test 130 | logAndRun ls -l $EMU3_MOUNTPOINT/foo/t3 131 | test 132 | 133 | printTest "cp -r" 134 | 135 | logAndRun cp -r $EMU3_MOUNTPOINT/foo $EMU3_MOUNTPOINT/bar 136 | test 137 | logAndRun ls -l $EMU3_MOUNTPOINT/bar/t1 138 | test 139 | logAndRun cat $EMU3_MOUNTPOINT/bar/t1 140 | test 141 | logAndRun '[ 123$'\''\n'\''4567 == "$out" ]' 142 | test 143 | 144 | printTest "mv" 145 | 146 | logAndRun mv $EMU3_MOUNTPOINT/foo/t3 $EMU3_MOUNTPOINT/foo/t2 147 | test foo 148 | logAndRun ls -l $EMU3_MOUNTPOINT/foo/t3 149 | testError 150 | logAndRun ls -l $EMU3_MOUNTPOINT/foo/t2 151 | test 152 | 153 | logAndRun '[ "$(< $EMU3_MOUNTPOINT/foo/t1)" == "$(< $EMU3_MOUNTPOINT/foo/t2)" ]' 154 | test 155 | 156 | printTest "cp with big files" 157 | 158 | logAndRun 'head -c 32M t3' 159 | logAndRun cp t3 $EMU3_MOUNTPOINT/foo 160 | test 161 | logAndRun ls -l $EMU3_MOUNTPOINT/foo/t3 162 | test 163 | logAndRun '[ $(stat --print "%s" t3) -eq $(stat --print "%s" $EMU3_MOUNTPOINT/foo/t3) ]' 164 | test 165 | logAndRun '[ 65536 -eq $(stat --print "%b" $EMU3_MOUNTPOINT/foo/t3) ]' 166 | test 167 | 168 | logAndRun cp t3 $EMU3_MOUNTPOINT/foo/t4 169 | test 170 | logAndRun ls -l $EMU3_MOUNTPOINT/foo/t4 171 | test 172 | logAndRun '[ $(stat --print "%s" $EMU3_MOUNTPOINT/foo/t3) -eq $(stat --print "%s" $EMU3_MOUNTPOINT/foo/t4) ]' 173 | test 174 | logAndRun '[ 65536 -eq $(stat --print "%b" $EMU3_MOUNTPOINT/foo/t3) ]' 175 | test 176 | 177 | echo "Remounting..." 178 | logAndRun sudo umount $EMU3_MOUNTPOINT 179 | test 180 | logAndRun sudo mount -t emu4 /dev/loop0 $EMU3_MOUNTPOINT 181 | test 182 | 183 | logAndRun '[ $(stat --print "%s" $EMU3_MOUNTPOINT/foo/t3) -eq $(stat --print "%s" $EMU3_MOUNTPOINT/foo/t4) ]' 184 | test 185 | logAndRun '[ $(stat --print "%s" t3) -eq $(stat --print "%s" $EMU3_MOUNTPOINT/foo/t3) ]' 186 | test 187 | logAndRun '[ 65536 -eq $(stat --print "%b" $EMU3_MOUNTPOINT/foo/t3) ]' 188 | test 189 | 190 | date | tee 191 | 192 | logAndRun diff $EMU3_MOUNTPOINT/foo/t3 $EMU3_MOUNTPOINT/foo/t4 193 | test 194 | 195 | logAndRun cp $EMU3_MOUNTPOINT/foo/t3 t3.bak 196 | test 197 | 198 | logAndRun diff t3 t3.bak 199 | test 200 | rm -f t3 t3.bak 201 | 202 | printTest "Truncate" 203 | 204 | logAndRun '> $EMU3_MOUNTPOINT/foo/t1' 205 | test foo/t1 206 | 207 | logAndRun '[ 0 -eq $(wc -c $EMU3_MOUNTPOINT/foo/t1 | awk '\''{print $1}'\'') ]' 208 | test 209 | 210 | logAndRun '> $EMU3_MOUNTPOINT/foo/t3' 211 | logAndRun '[ $(stat --print "%s" $EMU3_MOUNTPOINT/foo/t3) -eq 0 ]' 212 | test 213 | logAndRun '[ 1024 -eq $(stat --print "%b" $EMU3_MOUNTPOINT/foo/t3) ]' 214 | test 215 | 216 | for i in $(seq 0 4); do 217 | f=t${i} 218 | logAndRun rm $EMU3_MOUNTPOINT/foo/$f 219 | test foo 220 | logAndRun rm $EMU3_MOUNTPOINT/foo/$f 221 | testError 222 | done 223 | 224 | logAndRun ls -l $EMU3_MOUNTPOINT/foo 225 | test foo 226 | 227 | logAndRun rmdir $EMU3_MOUNTPOINT/foo 228 | test . 229 | 230 | printTest "cp with other size files (0 and non multiple of 512)..." 231 | 232 | logAndRun mkdir $EMU3_MOUNTPOINT/foo 233 | test . 234 | 235 | logAndRun touch t5 236 | logAndRun cp t5 $EMU3_MOUNTPOINT/foo 237 | test foo/t5 238 | 239 | logAndRun 'head -c 1234567 t6' 240 | logAndRun cp t6 $EMU3_MOUNTPOINT/foo 241 | test foo/t6 242 | 243 | logAndRun '[ $(stat --print "%s" t5) -eq $(stat --print "%s" $EMU3_MOUNTPOINT/foo/t5) ]' 244 | test 245 | logAndRun '[ $(stat --print "%s" t6) -eq $(stat --print "%s" $EMU3_MOUNTPOINT/foo/t6) ]' 246 | test 247 | 248 | logAndRun diff t5 $EMU3_MOUNTPOINT/foo/t5 249 | test 250 | logAndRun diff t6 $EMU3_MOUNTPOINT/foo/t6 251 | test 252 | 253 | logAndRun '[ 1024 -eq $(stat --print "%b" $EMU3_MOUNTPOINT/foo/t5) ]' 254 | test 255 | logAndRun '[ 3072 -eq $(stat --print "%b" $EMU3_MOUNTPOINT/foo/t6) ]' 256 | test 257 | 258 | logAndRun diff t5 $EMU3_MOUNTPOINT/foo/t5 259 | test 260 | logAndRun diff t6 $EMU3_MOUNTPOINT/foo/t6 261 | test 262 | 263 | echo "Remounting..." 264 | logAndRun sudo umount $EMU3_MOUNTPOINT 265 | test 266 | logAndRun sudo mount -t emu4 /dev/loop0 $EMU3_MOUNTPOINT 267 | test 268 | 269 | logAndRun '[ $(stat --print "%s" t5) -eq $(stat --print "%s" $EMU3_MOUNTPOINT/foo/t5) ]' 270 | test 271 | logAndRun '[ $(stat --print "%s" t6) -eq $(stat --print "%s" $EMU3_MOUNTPOINT/foo/t6) ]' 272 | test 273 | logAndRun '[ 1024 -eq $(stat --print "%b" $EMU3_MOUNTPOINT/foo/t5) ]' 274 | test 275 | logAndRun '[ 3072 -eq $(stat --print "%b" $EMU3_MOUNTPOINT/foo/t6) ]' 276 | test 277 | 278 | logAndRun diff t5 $EMU3_MOUNTPOINT/foo/t5 279 | test 280 | logAndRun diff t6 $EMU3_MOUNTPOINT/foo/t6 281 | test 282 | 283 | logAndRun rm t5 t6 284 | 285 | printTest "Directory expansion" 286 | 287 | logAndRun mkdir $EMU3_MOUNTPOINT/expansion 288 | test expansion 289 | logAndRun '[ 1 -eq $(stat --print "%b" $EMU3_MOUNTPOINT/expansion) ]' 290 | test 291 | 292 | for i in $(seq 1 16); do 293 | name=f-${i} 294 | logAndRun touch $EMU3_MOUNTPOINT/expansion/$name 295 | test expansion/$name 296 | done 297 | 298 | logAndRun '[ 512 -eq $(stat --print "%s" $EMU3_MOUNTPOINT/expansion) ]' 299 | test 300 | logAndRun '[ 1 -eq $(stat --print "%b" $EMU3_MOUNTPOINT/expansion) ]' 301 | test 302 | 303 | logAndRun touch $EMU3_MOUNTPOINT/expansion/f-17 304 | test expansion/f-17 305 | 306 | logAndRun '[ $((2*512)) -eq $(stat --print "%s" $EMU3_MOUNTPOINT/expansion) ]' 307 | test 308 | logAndRun '[ 2 -eq $(stat --print "%b" $EMU3_MOUNTPOINT/expansion) ]' 309 | test 310 | 311 | logAndRun rm -rf $EMU3_MOUNTPOINT/expansion 312 | test . 313 | 314 | logAndRun mkdir $EMU3_MOUNTPOINT/src 315 | test . 316 | logAndRun mv $EMU3_MOUNTPOINT/src $EMU3_MOUNTPOINT/dst 317 | logAndRun ls -l $EMU3_MOUNTPOINT/dst 318 | test . 319 | logAndRun ls -l $EMU3_MOUNTPOINT/src 320 | testError 321 | 322 | logAndRun mkdir $EMU3_MOUNTPOINT/d1 323 | test 324 | logAndRun mkdir $EMU3_MOUNTPOINT/d2 325 | test 326 | logAndRun 'echo "12345" > $EMU3_MOUNTPOINT/d1/t1' 327 | logAndRun cat $EMU3_MOUNTPOINT/d1/t1 328 | test d1 329 | 330 | printTest "Directory full" 331 | 332 | logAndRun mkdir $EMU3_MOUNTPOINT/full 333 | test full 334 | for i in $(seq 1 112); do 335 | name=f-${i} 336 | logAndRun touch $EMU3_MOUNTPOINT/full/$name 337 | test full/$name 338 | done 339 | logAndRun touch $EMU3_MOUNTPOINT/full/error 340 | testError full 341 | 342 | logAndRun '[ $((7*512)) -eq $(stat --print "%s" $EMU3_MOUNTPOINT/full) ]' 343 | test 344 | logAndRun '[ 7 -eq $(stat --print "%b" $EMU3_MOUNTPOINT/full) ]' 345 | test 346 | 347 | printTest "mv (rename)" 348 | 349 | logAndRun mv $EMU3_MOUNTPOINT/d1/t1 $EMU3_MOUNTPOINT/d1/t2 350 | test d1 351 | logAndRun cat $EMU3_MOUNTPOINT/d1/t2 352 | logAndRun ls -li $EMU3_MOUNTPOINT/d1/t2 353 | test d1 354 | logAndRun ls -li $EMU3_MOUNTPOINT/d1/t1 355 | testError 356 | 357 | logAndRun touch $EMU3_MOUNTPOINT/d2/t2 358 | logAndRun ls -li $EMU3_MOUNTPOINT/d2 359 | logAndRun mv $EMU3_MOUNTPOINT/d1/t2 $EMU3_MOUNTPOINT/d2 360 | test d2 361 | logAndRun cat $EMU3_MOUNTPOINT/d2/t2 362 | logAndRun ls -li $EMU3_MOUNTPOINT/d2/t2 363 | test d2 364 | logAndRun ls -li $EMU3_MOUNTPOINT/d1/t1 365 | testError d1 366 | 367 | logAndRun rm $EMU3_MOUNTPOINT/d2/t2 368 | logAndRun 'echo "1234567" > $EMU3_MOUNTPOINT/d1/t2' 369 | logAndRun mv $EMU3_MOUNTPOINT/d1/t2 $EMU3_MOUNTPOINT/d2 370 | test d2 371 | logAndRun cat $EMU3_MOUNTPOINT/d2/t2 372 | logAndRun ls -li $EMU3_MOUNTPOINT/d2/t2 373 | test d2 374 | logAndRun ls -li $EMU3_MOUNTPOINT/d1/t1 375 | testError d1 376 | 377 | logAndRun mv $EMU3_MOUNTPOINT/d1 $EMU3_MOUNTPOINT/d2 378 | testError 379 | 380 | printTest "Extended attributes (bank number)" 381 | 382 | logAndRun 'getfattr -n "user.bank.number" $EMU3_MOUNTPOINT/d2/t2 2> /dev/null | awk -F\" '\''{print $2}'\''' 383 | test 384 | logAndRun '[ $out -eq 0 ]' 385 | test 386 | logAndRun setfattr -n "user.bank.number" -v 111 $EMU3_MOUNTPOINT/d2/t2 387 | test 388 | logAndRun 'getfattr -n "user.bank.number" $EMU3_MOUNTPOINT/d2/t2 2> /dev/null | awk -F\" '\''{print $2}'\''' 389 | test 390 | logAndRun '[ $out -eq 111 ]' 391 | test 392 | logAndRun setfattr -n "user.bank.number" -v 112 $EMU3_MOUNTPOINT/d2/t2 393 | testError 394 | logAndRun setfattr -n "user.bank.number" --value=-1 $EMU3_MOUNTPOINT/d2/t2 395 | testError 396 | 397 | logAndRun getfattr -d -m "user.bank.number" $EMU3_MOUNTPOINT/d2 398 | test 399 | logAndRun '[ -z "$out" ]' 400 | test 401 | logAndRun getfattr -n "user.bank.number" $EMU3_MOUNTPOINT/d2 402 | testError 403 | logAndRun setfattr -n "user.bank.number" -v 0 $EMU3_MOUNTPOINT/d2 404 | testError 405 | 406 | logAndRun getfattr -n "user.foo" $EMU3_MOUNTPOINT/d2/t2 407 | testError 408 | logAndRun setfattr -n "user.foo" -v 0 $EMU3_MOUNTPOINT/d2/t2 409 | testError 410 | 411 | logAndRun getfattr -n "user.foo" $EMU3_MOUNTPOINT/d2 412 | testError 413 | logAndRun setfattr -n "user.foo" -v 0 $EMU3_MOUNTPOINT/d2 414 | testError 415 | 416 | logAndRun setfattr -n "user.bank.number" -v foo $EMU3_MOUNTPOINT/d2/t2 417 | testError 418 | 419 | logAndRun sudo umount $EMU3_MOUNTPOINT 420 | logAndRun sudo losetup -d /dev/loop0 421 | echo 422 | 423 | echo "Uncompressing truncated image..." 424 | logAndRun cp image_truncated.iso.xz.bak image_truncated.iso.xz 425 | logAndRun sudo rm -f image_truncated.iso 426 | logAndRun unxz image_truncated.iso.xz 427 | logAndRun sudo losetup /dev/loop0 image_truncated.iso 428 | echo 429 | 430 | printTest "Mounting truncated image" 431 | 432 | logAndRun sudo mount -t emu3 /dev/loop0 $EMU3_MOUNTPOINT 433 | testError 434 | 435 | cleanUp 436 | 437 | v=0 438 | [ $ok -ne $total ] && v=1 439 | 440 | exit $v 441 | -------------------------------------------------------------------------------- /super.c: -------------------------------------------------------------------------------- 1 | /* 2 | * super.c 3 | * Copyright (C) 2018 David García Goñi 4 | * 5 | * This file is part of emu3fs. 6 | * 7 | * emu3fs is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * emu3fs is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with emu3fs. If not, see . 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | #include "emu3_fs.h" 25 | 26 | static struct kmem_cache *emu3_inode_cachep; 27 | 28 | inline void emu3_free_dir_content_block(struct emu3_sb_info *info, short blknum) 29 | { 30 | info->dir_content_block_list[blknum - info->start_dir_content_block] = 31 | 0; 32 | } 33 | 34 | inline void emu3_use_dir_content_block(struct emu3_sb_info *info, short blknum) 35 | { 36 | info->dir_content_block_list[blknum - info->start_dir_content_block] = 37 | 1; 38 | } 39 | 40 | short emu3_get_free_dir_content_blknum(struct emu3_sb_info *info) 41 | { 42 | int i; 43 | for (i = 0; i < info->dir_content_blocks; i++) 44 | if (!info->dir_content_block_list[i]) { 45 | return info->start_dir_content_block + i; 46 | } 47 | return -1; 48 | } 49 | 50 | static struct inode *emu3_alloc_inode(struct super_block *sb) 51 | { 52 | struct emu3_inode *e3i; 53 | 54 | e3i = kmem_cache_alloc(emu3_inode_cachep, GFP_KERNEL); 55 | if (!e3i) 56 | return NULL; 57 | return &e3i->vfs_inode; 58 | } 59 | 60 | static void emu3_i_callback(struct rcu_head *head) 61 | { 62 | struct inode *inode = container_of(head, struct inode, i_rcu); 63 | 64 | kmem_cache_free(emu3_inode_cachep, EMU3_I(inode)); 65 | } 66 | 67 | static void emu3_destroy_inode(struct inode *inode) 68 | { 69 | call_rcu(&inode->i_rcu, emu3_i_callback); 70 | } 71 | 72 | void emu3_set_fattrs(struct emu3_sb_info *info, 73 | struct emu3_file_attrs *fattrs, loff_t size) 74 | { 75 | unsigned int rem; 76 | if (size == 0) { 77 | fattrs->clusters = cpu_to_le16(1); 78 | fattrs->blocks = cpu_to_le16(1); 79 | fattrs->bytes = cpu_to_le16(0); 80 | } else { 81 | fattrs->clusters = size >> info->cluster_size_shift; 82 | rem = size - (fattrs->clusters << info->cluster_size_shift); 83 | if (rem) 84 | fattrs->clusters++; 85 | fattrs->blocks = rem >> EMU3_BSIZE_BITS; 86 | rem = rem % EMU3_BSIZE; 87 | if (rem) 88 | fattrs->blocks++; 89 | fattrs->bytes = rem; 90 | fattrs->clusters = cpu_to_le16(fattrs->clusters); 91 | fattrs->blocks = cpu_to_le16(fattrs->blocks); 92 | fattrs->bytes = cpu_to_le16(fattrs->bytes); 93 | } 94 | } 95 | 96 | void emu3_init_fattrs(struct emu3_sb_info *info, 97 | struct emu3_file_attrs *fattrs, short start_cluster) 98 | { 99 | fattrs->start_cluster = cpu_to_le16(start_cluster); 100 | emu3_set_fattrs(info, fattrs, 0); 101 | fattrs->type = EMU3_FTYPE_STD; 102 | if (info->emu4) { 103 | memcpy(fattrs->props, "\0E4B0", EMU3_FILE_PROPS_LEN); 104 | } else { 105 | memset(fattrs->props, 0, EMU3_FILE_PROPS_LEN); 106 | } 107 | } 108 | 109 | //Prunes the cluster list to the real inode size 110 | void emu3_prune_cluster_list(struct inode *inode) 111 | { 112 | struct emu3_sb_info *info = EMU3_SB(inode->i_sb); 113 | struct emu3_inode *e3i = EMU3_I(inode); 114 | short clusters, last_cluster, next_cluster; 115 | int pruning; 116 | 117 | clusters = le16_to_cpu(e3i->data.fattrs.clusters); 118 | last_cluster = emu3_get_cluster(inode, clusters - 1); 119 | pruning = 0; 120 | 121 | next_cluster = le16_to_cpu(info->cluster_list[last_cluster]); 122 | while (next_cluster != EMU_LAST_FILE_CLUSTER) { 123 | info->cluster_list[last_cluster] = 124 | pruning ? 0 : cpu_to_le16(EMU_LAST_FILE_CLUSTER); 125 | last_cluster = next_cluster; 126 | next_cluster = le16_to_cpu(info->cluster_list[last_cluster]); 127 | pruning = 1; 128 | } 129 | if (pruning) 130 | info->cluster_list[last_cluster] = 0; 131 | } 132 | 133 | void emu3_set_inode_blocks(struct inode *inode, struct emu3_file_attrs *fattrs) 134 | { 135 | struct emu3_sb_info *info = EMU3_SB(inode->i_sb); 136 | inode->i_blocks = 137 | le16_to_cpu(fattrs->clusters) * info->blocks_per_cluster; 138 | } 139 | 140 | static int emu3_write_inode(struct inode *inode, struct writeback_control *wbc) 141 | { 142 | struct emu3_sb_info *info = EMU3_SB(inode->i_sb); 143 | struct emu3_dentry *e3d; 144 | struct buffer_head *bh; 145 | int err = 0; 146 | 147 | if (EMU3_IS_I_ROOT_DIR(inode) || EMU3_IS_I_REG_DIR(inode, info)) 148 | return 0; 149 | 150 | mutex_lock(&info->lock); 151 | 152 | e3d = emu3_find_dentry_by_inode(inode, &bh); 153 | if (!e3d) { 154 | mutex_unlock(&info->lock); 155 | return -ENOENT; 156 | } 157 | 158 | emu3_set_fattrs(info, &e3d->data.fattrs, inode->i_size); 159 | emu3_set_inode_blocks(inode, &e3d->data.fattrs); 160 | emu3_set_emu3_inode_data(inode, e3d); 161 | emu3_prune_cluster_list(inode); 162 | 163 | mark_buffer_dirty(bh); 164 | if (wbc->sync_mode == WB_SYNC_ALL) { 165 | sync_dirty_buffer(bh); 166 | if (buffer_req(bh) && !buffer_uptodate(bh)) 167 | err = -EIO; 168 | } 169 | 170 | brelse(bh); 171 | mutex_unlock(&info->lock); 172 | return err; 173 | } 174 | 175 | //This happens occasionally, luckily only on single dir images, so we try to fix it. 176 | //In some cases, all the used blocks are bad. See E-mu Classic Series V5. 177 | static bool emu3_fix_first_dir_blocks(struct emu3_dentry *e3d, 178 | struct emu3_sb_info *info) 179 | { 180 | int i; 181 | short new, old, *block = e3d->data.dattrs.block_list; 182 | 183 | for (i = 0; i < EMU3_BLOCKS_PER_DIR; i++, block++) { 184 | old = le16_to_cpu(*block); 185 | if (EMU3_IS_DIR_BLOCK_FREE(old)) 186 | break; 187 | 188 | new = info->start_dir_content_block + i; 189 | if (new != old) { 190 | printk(KERN_WARNING 191 | "%s: Directory block changed from 0x%04x to 0x%04x", 192 | EMU3_MODULE_NAME, old, new); 193 | *block = cpu_to_le16(new); 194 | } 195 | } 196 | 197 | return 1; 198 | } 199 | 200 | static void emu3_init_once(void *foo) 201 | { 202 | struct emu3_inode *e3i = foo; 203 | inode_init_once(&e3i->vfs_inode); 204 | } 205 | 206 | static int init_inodecache(void) 207 | { 208 | emu3_inode_cachep = kmem_cache_create("emu3_inode_cache", 209 | sizeof(struct emu3_inode), 210 | 0, (SLAB_RECLAIM_ACCOUNT), 211 | emu3_init_once); 212 | if (emu3_inode_cachep == NULL) 213 | return -ENOMEM; 214 | return 0; 215 | } 216 | 217 | static void destroy_inodecache(void) 218 | { 219 | kmem_cache_destroy(emu3_inode_cachep); 220 | } 221 | 222 | static int emu3_get_free_clusters(struct emu3_sb_info *info) 223 | { 224 | int free_clusters = 0; 225 | int i; 226 | 227 | for (i = 1; i <= info->clusters; i++) 228 | if (!info->cluster_list[i]) 229 | free_clusters++; 230 | return free_clusters; 231 | } 232 | 233 | static int emu3_get_free_inodes(struct super_block *sb) 234 | { 235 | int i, j, blknum; 236 | int free_inos = 0; 237 | struct emu3_dentry *e3d; 238 | struct buffer_head *b; 239 | struct emu3_sb_info *info = EMU3_SB(sb); 240 | 241 | for (i = 0; i < info->root_blocks + info->dir_content_blocks; i++) { 242 | blknum = info->start_root_block + i; 243 | b = sb_bread(sb, blknum); 244 | if (!b) { 245 | printk(KERN_CRIT EMU3_ERR_NOT_BLK, EMU3_MODULE_NAME, 246 | blknum); 247 | break; 248 | } 249 | 250 | e3d = (struct emu3_dentry *)b->b_data; 251 | for (j = 0; j < EMU3_ENTRIES_PER_BLOCK; j++, e3d++) 252 | if (i < info->root_blocks) { 253 | if (!EMU3_DENTRY_IS_DIR(e3d)) 254 | free_inos++; 255 | } else { 256 | if (!EMU3_DENTRY_IS_FILE(e3d)) 257 | free_inos++; 258 | } 259 | 260 | brelse(b); 261 | } 262 | 263 | return free_inos; 264 | } 265 | 266 | static int emu3_get_free_dir_blocks(struct emu3_sb_info *info) 267 | { 268 | bool *b; 269 | int i, free_blks = 0; 270 | 271 | b = info->dir_content_block_list; 272 | for (i = 0; i < info->dir_content_blocks; i++, b++) 273 | if (!*b) 274 | free_blks++; 275 | 276 | return free_blks; 277 | } 278 | 279 | static inline int emu3_get_addressable_blocks(struct emu3_sb_info *info) 280 | { 281 | return info->root_blocks + info->dir_content_blocks + 282 | info->clusters * info->blocks_per_cluster; 283 | } 284 | 285 | static int emu3_statfs(struct dentry *dentry, struct kstatfs *buf) 286 | { 287 | struct super_block *sb = dentry->d_sb; 288 | struct emu3_sb_info *info = EMU3_SB(sb); 289 | u64 id = huge_encode_dev(sb->s_bdev->bd_dev); 290 | 291 | //For the free space and free inodes we do not consider files. 292 | buf->f_type = EMU3_FS_TYPE; 293 | buf->f_bsize = EMU3_BSIZE; 294 | //Total addressable blocks. 295 | buf->f_blocks = emu3_get_addressable_blocks(info); 296 | buf->f_bfree = 297 | emu3_get_free_clusters(info) * info->blocks_per_cluster + 298 | emu3_get_free_dir_blocks(info); 299 | buf->f_bavail = buf->f_bfree; 300 | buf->f_files = EMU3_ENTRIES_PER_BLOCK * (info->root_blocks + 301 | info->dir_content_blocks); 302 | buf->f_ffree = emu3_get_free_inodes(sb); 303 | buf->f_fsid.val[0] = (u32) id; 304 | buf->f_fsid.val[1] = (u32) (id >> 32); 305 | buf->f_namelen = EMU3_LENGTH_FILENAME; 306 | return 0; 307 | } 308 | 309 | //Base 0 search 310 | int emu3_get_cluster(struct inode *inode, int n) 311 | { 312 | struct emu3_sb_info *info = EMU3_SB(inode->i_sb); 313 | short next = EMU3_I_START_CLUSTER(inode); 314 | int i = 0; 315 | 316 | while (i < n) { 317 | if (le16_to_cpu(info->cluster_list[next]) == 318 | EMU_LAST_FILE_CLUSTER) 319 | return -1; 320 | next = le16_to_cpu(info->cluster_list[next]); 321 | i++; 322 | } 323 | return next; 324 | } 325 | 326 | void emu3_init_cluster_list(struct inode *inode) 327 | { 328 | struct emu3_sb_info *info = EMU3_SB(inode->i_sb); 329 | 330 | info->cluster_list[EMU3_I_START_CLUSTER(inode)] = 331 | cpu_to_le16(EMU_LAST_FILE_CLUSTER); 332 | } 333 | 334 | static void emu3_clear_cluster_list(struct inode *inode) 335 | { 336 | int i = 1; 337 | struct emu3_sb_info *info = EMU3_SB(inode->i_sb); 338 | short prev, next = EMU3_I_START_CLUSTER(inode); 339 | 340 | while (le16_to_cpu(info->cluster_list[next]) != EMU_LAST_FILE_CLUSTER) { 341 | prev = next; 342 | next = le16_to_cpu(info->cluster_list[next]); 343 | info->cluster_list[prev] = 0; 344 | i++; 345 | if (i > info->clusters) { 346 | printk(KERN_CRIT "%s: Loop detected in cluster list\n", 347 | EMU3_MODULE_NAME); 348 | break; 349 | } 350 | } 351 | info->cluster_list[next] = 0; 352 | } 353 | 354 | int emu3_next_free_cluster(struct emu3_sb_info *info) 355 | { 356 | int i; 357 | 358 | for (i = 1; i < info->clusters; i++) 359 | if (info->cluster_list[i] == 0) 360 | return i; 361 | return -ENOSPC; 362 | } 363 | 364 | sector_t emu3_get_phys_block(struct inode *inode, sector_t block) 365 | { 366 | struct emu3_sb_info *info = EMU3_SB(inode->i_sb); 367 | int cluster = ((int)block) / info->blocks_per_cluster; 368 | int offset = ((int)block) % info->blocks_per_cluster; 369 | 370 | cluster = emu3_get_cluster(inode, cluster); 371 | if (cluster == -1) 372 | return -1; 373 | return info->start_data_block + 374 | ((cluster - 1) * info->blocks_per_cluster) + offset; 375 | } 376 | 377 | static void emu3_evict_inode(struct inode *inode) 378 | { 379 | struct emu3_sb_info *info = EMU3_SB(inode->i_sb); 380 | truncate_inode_pages(&inode->i_data, 0); 381 | if (!inode->i_nlink && inode->i_mode & S_IFREG) { 382 | mutex_lock(&info->lock); 383 | emu3_clear_i_map(info, inode); 384 | emu3_clear_cluster_list(inode); 385 | mutex_unlock(&info->lock); 386 | inode->i_size = 0; 387 | } 388 | invalidate_inode_buffers(inode); 389 | clear_inode(inode); 390 | } 391 | 392 | static int emu3_write_cluster_list(struct super_block *sb) 393 | { 394 | struct emu3_sb_info *info = EMU3_SB(sb); 395 | struct buffer_head *b; 396 | int i, blknum; 397 | 398 | for (i = 0; i < info->cluster_list_blocks; i++) { 399 | blknum = info->start_cluster_list_block + i; 400 | b = sb_bread(sb, blknum); 401 | if (!b) { 402 | printk(KERN_CRIT EMU3_ERR_NOT_BLK, EMU3_MODULE_NAME, 403 | blknum); 404 | return -EIO; 405 | } 406 | 407 | memcpy(b->b_data, 408 | &info->cluster_list[EMU3_CLUSTER_ENTRIES_PER_BLOCK * i], 409 | EMU3_BSIZE); 410 | mark_buffer_dirty(b); 411 | brelse(b); 412 | } 413 | 414 | return 0; 415 | } 416 | 417 | static int emu3_read_cluster_list(struct super_block *sb) 418 | { 419 | struct emu3_sb_info *info = EMU3_SB(sb); 420 | struct buffer_head *b; 421 | int i, blknum; 422 | 423 | for (i = 0; i < info->cluster_list_blocks; i++) { 424 | blknum = info->start_cluster_list_block + i; 425 | b = sb_bread(sb, blknum); 426 | if (!b) { 427 | printk(KERN_CRIT EMU3_ERR_NOT_BLK, EMU3_MODULE_NAME, 428 | blknum); 429 | return -EIO; 430 | } 431 | 432 | memcpy(&info->cluster_list[EMU3_CLUSTER_ENTRIES_PER_BLOCK * i], 433 | b->b_data, EMU3_BSIZE); 434 | brelse(b); 435 | } 436 | 437 | return 0; 438 | } 439 | 440 | static void emu3_put_super(struct super_block *sb) 441 | { 442 | struct emu3_sb_info *info = EMU3_SB(sb); 443 | 444 | if (info) { 445 | mutex_lock(&info->lock); 446 | emu3_write_cluster_list(sb); 447 | mutex_unlock(&info->lock); 448 | 449 | mutex_destroy(&info->lock); 450 | 451 | kfree(info->cluster_list); 452 | kfree(info->dir_content_block_list); 453 | kfree(info->i_maps); 454 | kfree(info); 455 | sb->s_fs_info = NULL; 456 | } 457 | } 458 | 459 | static const struct super_operations emu3_super_operations = { 460 | .alloc_inode = emu3_alloc_inode, 461 | .destroy_inode = emu3_destroy_inode, 462 | .write_inode = emu3_write_inode, 463 | .evict_inode = emu3_evict_inode, 464 | .put_super = emu3_put_super, 465 | .statfs = emu3_statfs 466 | }; 467 | 468 | static int emu3_fill_super(struct super_block *sb, void *data, 469 | int silent, bool emu4) 470 | { 471 | struct emu3_sb_info *info; 472 | struct buffer_head *sbh; 473 | struct buffer_head *b; 474 | unsigned char *e3sb; 475 | struct inode *inode; 476 | int i, j, k, blknum, size, err = 0; 477 | short *block, index; 478 | struct emu3_dentry *e3d; 479 | unsigned int *parameters; 480 | unsigned int root_ino; 481 | 482 | if (sb_set_blocksize(sb, EMU3_BSIZE) != EMU3_BSIZE) { 483 | printk(KERN_ERR 484 | "%s: 512B block size not allowed on this device\n", 485 | EMU3_MODULE_NAME); 486 | return -EINVAL; 487 | } 488 | 489 | info = kzalloc(sizeof(struct emu3_sb_info), GFP_KERNEL); 490 | if (!info) 491 | return -ENOMEM; 492 | 493 | sb->s_fs_info = info; 494 | 495 | sbh = sb_bread(sb, 0); 496 | if (!sbh) { 497 | printk(KERN_CRIT EMU3_ERR_NOT_BLK, EMU3_MODULE_NAME, 0); 498 | err = -EIO; 499 | goto out1; 500 | } 501 | 502 | e3sb = (unsigned char *)sbh->b_data; 503 | 504 | //Check EMU3 string 505 | if (strncmp(EMU3_FS_SIGNATURE, e3sb, 4) != 0) { 506 | printk(KERN_ERR "%s: volume is not an EMU3 disk\n", 507 | EMU3_MODULE_NAME); 508 | err = -EINVAL; 509 | goto out2; 510 | } 511 | 512 | parameters = (unsigned int *)e3sb; 513 | 514 | info->blocks = le32_to_cpu(parameters[1]) + 1; //Total blocks in the physical device. 515 | info->start_root_block = le32_to_cpu(parameters[2]); 516 | info->root_blocks = le32_to_cpu(parameters[3]); 517 | info->start_dir_content_block = le32_to_cpu(parameters[4]); 518 | info->dir_content_blocks = le32_to_cpu(parameters[5]); 519 | info->start_cluster_list_block = le32_to_cpu(parameters[6]); 520 | info->cluster_list_blocks = le32_to_cpu(parameters[7]); 521 | info->start_data_block = le32_to_cpu(parameters[8]); 522 | info->cluster_size_shift = 15 + e3sb[0x28]; //32kB minimum 523 | info->blocks_per_cluster = 524 | 1 << (info->cluster_size_shift - EMU3_BSIZE_BITS); 525 | //In Formula 4000 only, the total amount of blocks and clusters would allow to have a disk bigger than the ISO image itself. 526 | //Thus, the reported amount of blocks, size and free space is not right. 527 | //However, if the iso image is resized to accommodate all the blocks, the format is valid and stat and df output the right values. 528 | //This is not a problem on RO disks. 529 | info->clusters = le32_to_cpu(parameters[9]); 530 | 531 | //Now it's time to read the cluster list... 532 | size = EMU3_BSIZE * info->cluster_list_blocks; 533 | info->cluster_list = kzalloc(size, GFP_KERNEL); 534 | if (!info->cluster_list) { 535 | err = -ENOMEM; 536 | goto out2; 537 | } 538 | err = emu3_read_cluster_list(sb); 539 | if (err) 540 | goto out3; 541 | 542 | printk(KERN_INFO 543 | "%s: %d physical blocks, %d addressable blocks, %d clusters, %d blocks/cluster\n", 544 | EMU3_MODULE_NAME, info->blocks, 545 | emu3_get_addressable_blocks(info), info->clusters, 546 | info->blocks_per_cluster); 547 | printk(KERN_INFO "%s: cluster list start block @ %d + %d blocks\n", 548 | EMU3_MODULE_NAME, info->start_cluster_list_block, 549 | info->cluster_list_blocks); 550 | printk(KERN_INFO "%s: root start block @ %d + %d blocks\n", 551 | EMU3_MODULE_NAME, info->start_root_block, info->root_blocks); 552 | printk(KERN_INFO "%s: dir content start block @ %d + %d blocks\n", 553 | EMU3_MODULE_NAME, info->start_dir_content_block, 554 | info->dir_content_blocks); 555 | printk(KERN_INFO "%s: data start block @ %d + %d clusters\n", 556 | EMU3_MODULE_NAME, info->start_data_block, info->clusters); 557 | 558 | size = sizeof(bool) * info->dir_content_blocks; 559 | info->dir_content_block_list = kzalloc(size, GFP_KERNEL); 560 | if (!info->dir_content_block_list) { 561 | err = -ENOMEM; 562 | goto out3; 563 | } 564 | memset(info->dir_content_block_list, 0, size); 565 | 566 | size = sizeof(unsigned int) * EMU3_TOTAL_ENTRIES(info); 567 | info->i_maps = kzalloc(size, GFP_KERNEL); 568 | if (!info->i_maps) { 569 | err = -ENOMEM; 570 | goto out4; 571 | } 572 | memset(info->i_maps, 0, size); 573 | 574 | sb->s_op = &emu3_super_operations; 575 | sb->s_xattr = emu3_xattr_handlers; 576 | 577 | info->emu4 = emu4; 578 | 579 | if (emu4) 580 | root_ino = 1; 581 | else 582 | root_ino = 583 | emu3_get_or_add_i_map(info, EMU3_DNUM 584 | (info->start_root_block, 0)); 585 | inode = emu3_get_inode(sb, root_ino); 586 | if (IS_ERR(inode)) { 587 | err = -EIO; 588 | goto out5; 589 | } 590 | if (!emu4) 591 | inode->i_mode = EMU3_ROOT_DIR_MODE; 592 | 593 | sb->s_root = d_make_root(inode); 594 | if (!sb->s_root) { 595 | iput(inode); 596 | err = -ENOMEM; 597 | goto out5; 598 | } 599 | 600 | for (i = 0; i < info->root_blocks; i++) { 601 | blknum = info->start_root_block + i; 602 | b = sb_bread(sb, blknum); 603 | if (!b) { 604 | printk(KERN_CRIT EMU3_ERR_NOT_BLK, EMU3_MODULE_NAME, 605 | blknum); 606 | err = -EIO; 607 | goto out5; 608 | } 609 | 610 | e3d = (struct emu3_dentry *)b->b_data; 611 | 612 | if (i == 0 && emu3_fix_first_dir_blocks(e3d, info)) 613 | mark_buffer_dirty_inode(b, inode); 614 | 615 | for (j = 0; j < EMU3_ENTRIES_PER_BLOCK; j++, e3d++) { 616 | if (!EMU3_DENTRY_IS_DIR(e3d)) 617 | continue; 618 | 619 | block = e3d->data.dattrs.block_list; 620 | for (k = 0; k < EMU3_BLOCKS_PER_DIR; k++, block++) { 621 | index = le16_to_cpu(*block); 622 | if (EMU3_IS_DIR_BLOCK_FREE(index)) 623 | continue; 624 | 625 | index = index - info->start_dir_content_block; 626 | 627 | if (index < 0 628 | || index >= info->dir_content_blocks) { 629 | printk(KERN_CRIT 630 | "%s: block %d marked as used by dir %.16s\n", 631 | EMU3_MODULE_NAME, *block, 632 | e3d->name); 633 | err = -EIO; 634 | goto out5; 635 | } 636 | 637 | info->dir_content_block_list[index] = 1; 638 | } 639 | } 640 | 641 | brelse(b); 642 | } 643 | 644 | if (!err) { 645 | mutex_init(&info->lock); 646 | brelse(sbh); 647 | return 0; 648 | } 649 | 650 | out5: 651 | kfree(info->dir_content_block_list); 652 | out4: 653 | kfree(info->i_maps); 654 | out3: 655 | kfree(info->cluster_list); 656 | out2: 657 | brelse(sbh); 658 | out1: 659 | kfree(info); 660 | sb->s_fs_info = NULL; 661 | return err; 662 | } 663 | 664 | static int emu3_fill_super_v3(struct super_block *sb, void *data, int silent) 665 | { 666 | return emu3_fill_super(sb, data, silent, 0); 667 | } 668 | 669 | static int emu3_fill_super_v4(struct super_block *sb, void *data, int silent) 670 | { 671 | return emu3_fill_super(sb, data, silent, 1); 672 | } 673 | 674 | static struct dentry *emu3_mount_v3(struct file_system_type *fs_type, 675 | int flags, const char *dev_name, void *data) 676 | { 677 | return mount_bdev(fs_type, flags, dev_name, data, emu3_fill_super_v3); 678 | } 679 | 680 | static struct dentry *emu3_mount_v4(struct file_system_type *fs_type, 681 | int flags, const char *dev_name, void *data) 682 | { 683 | return mount_bdev(fs_type, flags, dev_name, data, emu3_fill_super_v4); 684 | } 685 | 686 | static struct file_system_type emu3_fs_type_v3 = { 687 | .owner = THIS_MODULE, 688 | .name = "emu3", 689 | .mount = emu3_mount_v3, 690 | .kill_sb = kill_block_super, 691 | .fs_flags = FS_REQUIRES_DEV, 692 | }; 693 | 694 | static struct file_system_type emu3_fs_type_v4 = { 695 | .owner = THIS_MODULE, 696 | .name = "emu4", 697 | .mount = emu3_mount_v4, 698 | .kill_sb = kill_block_super, 699 | .fs_flags = FS_REQUIRES_DEV, 700 | }; 701 | 702 | static int __init emu3_init(void) 703 | { 704 | int err; 705 | 706 | printk(KERN_INFO "%s: init\n", EMU3_MODULE_NAME); 707 | err = init_inodecache(); 708 | if (err) 709 | return err; 710 | err = register_filesystem(&emu3_fs_type_v3) 711 | || register_filesystem(&emu3_fs_type_v4); 712 | if (err) 713 | destroy_inodecache(); 714 | return err; 715 | } 716 | 717 | static void __exit emu3_exit(void) 718 | { 719 | unregister_filesystem(&emu3_fs_type_v3); 720 | unregister_filesystem(&emu3_fs_type_v4); 721 | destroy_inodecache(); 722 | printk(KERN_INFO "%s: exit\n", EMU3_MODULE_NAME); 723 | } 724 | 725 | module_init(emu3_init); 726 | module_exit(emu3_exit); 727 | 728 | MODULE_LICENSE("GPL"); 729 | 730 | MODULE_AUTHOR("David García Goñi "); 731 | MODULE_DESCRIPTION("E-Mu EIII filesystem for Linux"); 732 | -------------------------------------------------------------------------------- /dir.c: -------------------------------------------------------------------------------- 1 | /* 2 | * dir.c 3 | * Copyright (C) 2018 David García Goñi 4 | * 5 | * This file is part of emu3fs. 6 | * 7 | * emu3fs is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * emu3fs is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with emu3fs. If not, see . 19 | */ 20 | 21 | #include "emu3_fs.h" 22 | 23 | static void emu3_set_dentry_name(struct emu3_dentry *e3d, struct qstr *q) 24 | { 25 | memcpy(e3d->name, q->name, q->len); 26 | memset(&e3d->name[q->len], ' ', EMU3_LENGTH_FILENAME - q->len); 27 | } 28 | 29 | static void emu3_filename_fix(char *in, char *out) 30 | { 31 | int i; 32 | char c; 33 | 34 | for (i = 0; i < EMU3_LENGTH_FILENAME; i++) { 35 | c = in[i]; 36 | // 32 <= c <= 126 37 | if (c == '/') 38 | c = '?'; //Whatever will be nicer 39 | out[i] = c; 40 | } 41 | } 42 | 43 | static int emu3_filename_length(const char *filename) 44 | { 45 | const char *last = &filename[EMU3_LENGTH_FILENAME - 1]; 46 | int len; 47 | 48 | for (len = EMU3_LENGTH_FILENAME; len > 0; len--) { 49 | if (*last != ' ' && *last != '\0') 50 | return len; 51 | last--; 52 | } 53 | 54 | return -1; //A dentry with an empty name? 55 | } 56 | 57 | static int emu3_strncmp(struct dentry *dentry, struct emu3_dentry *e3d) 58 | { 59 | int len; 60 | char fixed[EMU3_LENGTH_FILENAME]; 61 | 62 | emu3_filename_fix(e3d->name, fixed); 63 | len = emu3_filename_length(e3d->name); 64 | len = len > dentry->d_name.len ? len : dentry->d_name.len; 65 | return strncmp(fixed, dentry->d_name.name, len); 66 | } 67 | 68 | static struct emu3_dentry *emu3_find_dentry_by_name_in_blk(struct inode *dir, struct dentry 69 | *dentry, struct buffer_head 70 | **b, 71 | unsigned int blknum, 72 | unsigned int *dnum) 73 | { 74 | unsigned int i; 75 | struct emu3_dentry *e3d; 76 | 77 | *b = sb_bread(dir->i_sb, blknum); 78 | if (!*b) { 79 | printk(KERN_CRIT EMU3_ERR_NOT_BLK, EMU3_MODULE_NAME, blknum); 80 | return NULL; 81 | } 82 | 83 | e3d = (struct emu3_dentry *)(*b)->b_data; 84 | for (i = 0; i < EMU3_ENTRIES_PER_BLOCK; i++, e3d++) { 85 | if (!EMU3_DENTRY_IS_DIR(e3d) && !EMU3_DENTRY_IS_FILE(e3d)) 86 | continue; 87 | 88 | if (!emu3_strncmp(dentry, e3d)) { 89 | if (dnum) 90 | *dnum = EMU3_DNUM(blknum, i); 91 | return e3d; 92 | } 93 | } 94 | 95 | brelse(*b); 96 | return NULL; 97 | } 98 | 99 | static struct emu3_dentry *emu3_find_dentry_by_name(struct inode *dir, 100 | struct dentry *dentry, 101 | struct buffer_head **b, 102 | unsigned int *dnum) 103 | { 104 | int i; 105 | short blknum; 106 | struct buffer_head *db; 107 | struct emu3_dentry *e3d, *res = NULL; 108 | struct emu3_sb_info *info = EMU3_SB(dir->i_sb); 109 | 110 | if (EMU3_IS_I_ROOT_DIR(dir)) { 111 | for (i = 0; i < info->root_blocks; i++) { 112 | blknum = info->start_root_block + i; 113 | res = 114 | emu3_find_dentry_by_name_in_blk(dir, dentry, b, 115 | blknum, dnum); 116 | if (res) 117 | return res; 118 | } 119 | return NULL; 120 | } 121 | 122 | e3d = emu3_find_dentry_by_inode(dir, &db); 123 | 124 | if (!e3d) 125 | return NULL; 126 | 127 | if (!EMU3_DENTRY_IS_DIR(e3d)) 128 | goto cleanup; 129 | 130 | for (i = 0; i < EMU3_BLOCKS_PER_DIR; i++) { 131 | blknum = le16_to_cpu(e3d->data.dattrs.block_list[i]); 132 | if (EMU3_IS_DIR_BLOCK_FREE(blknum)) 133 | break; 134 | 135 | res = 136 | emu3_find_dentry_by_name_in_blk(dir, dentry, b, 137 | blknum, dnum); 138 | if (res) 139 | break; 140 | } 141 | 142 | cleanup: 143 | brelse(db); 144 | return res; 145 | } 146 | 147 | static int emu3_emit(struct dir_context *ctx, 148 | struct emu3_dentry *e3d, unsigned int blknum, 149 | unsigned int offset, unsigned type, 150 | struct emu3_sb_info *info) 151 | { 152 | int len; 153 | unsigned long ino; 154 | char fixed[EMU3_LENGTH_FILENAME]; 155 | 156 | emu3_filename_fix(e3d->name, fixed); 157 | len = emu3_filename_length(fixed); 158 | ino = emu3_get_or_add_i_map(info, EMU3_DNUM(blknum, offset)); 159 | ctx->pos++; 160 | return dir_emit(ctx, fixed, len, ino, type); 161 | } 162 | 163 | static int emu3_iterate_dir(struct file *f, struct dir_context *ctx, 164 | struct inode *dir, struct emu3_sb_info *info) 165 | { 166 | loff_t k; 167 | unsigned int i, j; 168 | short blknum; 169 | struct buffer_head *b; 170 | struct buffer_head *db; 171 | struct emu3_dentry *e3d; 172 | struct emu3_dentry *e3d_dir; 173 | 174 | k = 2; 175 | e3d_dir = emu3_find_dentry_by_inode(dir, &db); 176 | 177 | if (!EMU3_DENTRY_IS_DIR(e3d_dir)) 178 | goto cleanup; 179 | 180 | for (i = 0; i < EMU3_BLOCKS_PER_DIR; i++) { 181 | blknum = le16_to_cpu(e3d_dir->data.dattrs.block_list[i]); 182 | if (EMU3_IS_DIR_BLOCK_FREE(blknum)) 183 | break; 184 | 185 | b = sb_bread(dir->i_sb, blknum); 186 | if (!b) { 187 | printk(KERN_CRIT EMU3_ERR_NOT_BLK, EMU3_MODULE_NAME, 188 | blknum); 189 | goto cleanup; 190 | } 191 | 192 | e3d = (struct emu3_dentry *)b->b_data; 193 | for (j = 0; j < EMU3_ENTRIES_PER_BLOCK; j++, e3d++) { 194 | if (!EMU3_DENTRY_IS_FILE(e3d)) 195 | continue; 196 | 197 | if (ctx->pos == k) { 198 | if (!emu3_emit 199 | (ctx, e3d, blknum, j, DT_REG, info)) { 200 | brelse(b); 201 | goto cleanup; 202 | } 203 | } 204 | k++; 205 | } 206 | brelse(b); 207 | } 208 | 209 | cleanup: 210 | brelse(db); 211 | return k; 212 | } 213 | 214 | static int emu3_iterate_root(struct file *f, struct dir_context *ctx, 215 | struct inode *dir, struct emu3_sb_info *info) 216 | { 217 | loff_t k; 218 | unsigned int i, j, blknum; 219 | struct emu3_dentry *e3d; 220 | struct buffer_head *b; 221 | 222 | k = 2; 223 | for (i = 0; i < info->root_blocks; i++) { 224 | blknum = info->start_root_block + i; 225 | b = sb_bread(dir->i_sb, blknum); 226 | if (!b) { 227 | printk(KERN_CRIT EMU3_ERR_NOT_BLK, EMU3_MODULE_NAME, 228 | blknum); 229 | break; 230 | } 231 | 232 | e3d = (struct emu3_dentry *)b->b_data; 233 | 234 | for (j = 0; j < EMU3_ENTRIES_PER_BLOCK; j++, e3d++) { 235 | if (!EMU3_DENTRY_IS_DIR(e3d)) 236 | continue; 237 | 238 | if (ctx->pos == k) { 239 | if (!emu3_emit 240 | (ctx, e3d, blknum, j, DT_DIR, info)) { 241 | brelse(b); 242 | return k; 243 | } 244 | } 245 | k++; 246 | } 247 | brelse(b); 248 | } 249 | 250 | return k; 251 | } 252 | 253 | static int emu3_iterate(struct file *f, struct dir_context *ctx) 254 | { 255 | struct inode *dir = file_inode(f); 256 | struct emu3_sb_info *info = EMU3_SB(dir->i_sb); 257 | 258 | if (!EMU3_IS_I_ROOT_DIR(dir) && !EMU3_IS_I_REG_DIR(dir, info)) 259 | return -ENOTDIR; 260 | 261 | if (ctx->pos == 0) { 262 | if (!dir_emit_dot(f, ctx)) 263 | return 0; 264 | ctx->pos++; 265 | } 266 | 267 | if (ctx->pos == 1) { 268 | if (!dir_emit_dotdot(f, ctx)) 269 | return 0; 270 | ctx->pos++; 271 | } 272 | 273 | if (EMU3_IS_I_ROOT_DIR(dir)) 274 | return emu3_iterate_root(f, ctx, dir, info); 275 | else 276 | return emu3_iterate_dir(f, ctx, dir, info); 277 | } 278 | 279 | static struct dentry *emu3_lookup(struct inode *dir, 280 | struct dentry *dentry, unsigned int flags) 281 | { 282 | unsigned long i_ino; 283 | unsigned int dnum; 284 | struct buffer_head *b; 285 | struct emu3_dentry *e3d; 286 | struct dentry *newent; 287 | struct inode *inode = NULL; 288 | struct emu3_sb_info *info = EMU3_SB(dir->i_sb); 289 | 290 | if (dentry->d_name.len > EMU3_LENGTH_FILENAME) 291 | return ERR_PTR(-ENAMETOOLONG); 292 | 293 | mutex_lock(&info->lock); 294 | 295 | e3d = emu3_find_dentry_by_name(dir, dentry, &b, &dnum); 296 | if (e3d) { 297 | brelse(b); 298 | i_ino = emu3_get_or_add_i_map(info, dnum); 299 | inode = emu3_get_inode(dir->i_sb, i_ino); 300 | if (IS_ERR(inode)) { 301 | mutex_unlock(&info->lock); 302 | return ERR_CAST(inode); 303 | } 304 | } 305 | newent = d_splice_alias(inode, dentry); 306 | 307 | mutex_unlock(&info->lock); 308 | 309 | return newent; 310 | } 311 | 312 | static int emu3_get_free_file_id(struct inode *dir) 313 | { 314 | int i, j, id = -1; 315 | short *block; 316 | short blknum; 317 | bool ids[EMU3_MAX_FILES_PER_DIR]; 318 | struct buffer_head *b, *db; 319 | struct emu3_dentry *e3d, *e3d_dir; 320 | struct emu3_sb_info *info = EMU3_SB(dir->i_sb); 321 | 322 | for (i = 0; i < EMU3_MAX_FILES_PER_DIR; i++) 323 | ids[i] = 0; 324 | 325 | e3d_dir = emu3_find_dentry_by_inode(dir, &db); 326 | 327 | if (!e3d_dir) 328 | return -1; 329 | 330 | if (!EMU3_DENTRY_IS_DIR(e3d_dir)) 331 | goto cleanup; 332 | 333 | block = e3d_dir->data.dattrs.block_list; 334 | 335 | for (i = 0; i < EMU3_BLOCKS_PER_DIR; i++, block++) { 336 | blknum = le16_to_cpu(*block); 337 | if (!EMU3_DIR_BLOCK_OK(blknum, info)) 338 | break; 339 | 340 | b = sb_bread(dir->i_sb, blknum); 341 | if (!b) { 342 | printk(KERN_CRIT EMU3_ERR_NOT_BLK, EMU3_MODULE_NAME, 343 | blknum); 344 | goto cleanup; 345 | } 346 | 347 | e3d = (struct emu3_dentry *)b->b_data; 348 | 349 | for (j = 0; j < EMU3_ENTRIES_PER_BLOCK; j++, e3d++) { 350 | if (EMU3_DENTRY_IS_FILE(e3d)) 351 | ids[e3d->data.id] = 1; 352 | } 353 | 354 | brelse(b); 355 | } 356 | 357 | for (i = 0; i < EMU3_MAX_FILES_PER_DIR; i++) 358 | if (!ids[i]) { 359 | id = i; 360 | break; 361 | } 362 | 363 | cleanup: 364 | brelse(db); 365 | return id; 366 | } 367 | 368 | static int emu3_find_empty_file_dentry(struct inode *dir, 369 | struct emu3_dentry **e3d, 370 | struct buffer_head **b, 371 | unsigned int *dnum) 372 | { 373 | int i, j, id, err = 0; 374 | short *block, blknum; 375 | struct buffer_head *db; 376 | struct emu3_dentry *e3d_dir; 377 | struct emu3_sb_info *info = EMU3_SB(dir->i_sb); 378 | 379 | e3d_dir = emu3_find_dentry_by_inode(dir, &db); 380 | 381 | if (!e3d_dir) { 382 | return -ENOENT; 383 | } 384 | 385 | if (!EMU3_DENTRY_IS_DIR(e3d_dir)) { 386 | err = -ENOTDIR; 387 | goto cleanup; 388 | } 389 | 390 | block = e3d_dir->data.dattrs.block_list; 391 | for (i = 0; i < EMU3_BLOCKS_PER_DIR; i++, block++) { 392 | blknum = le16_to_cpu(*block); 393 | if (!EMU3_DIR_BLOCK_OK(blknum, info)) 394 | break; 395 | 396 | *b = sb_bread(dir->i_sb, blknum); 397 | if (!*b) { 398 | printk(KERN_CRIT EMU3_ERR_NOT_BLK, EMU3_MODULE_NAME, 399 | blknum); 400 | err = -EIO; 401 | goto cleanup; 402 | } 403 | 404 | *e3d = (struct emu3_dentry *)(*b)->b_data; 405 | for (j = 0; j < EMU3_ENTRIES_PER_BLOCK; j++, (*e3d)++) { 406 | if (!EMU3_DENTRY_IS_FILE(*e3d)) { 407 | *dnum = EMU3_DNUM(blknum, j); 408 | goto add_id; 409 | } 410 | } 411 | 412 | brelse(*b); 413 | } 414 | 415 | if (i == EMU3_BLOCKS_PER_DIR) { 416 | err = -EFBIG; 417 | goto cleanup; 418 | } 419 | 420 | blknum = emu3_get_free_dir_content_blknum(info); 421 | if (blknum < 0) { 422 | err = -ENOSPC; 423 | goto cleanup; 424 | } 425 | 426 | e3d_dir->data.dattrs.block_list[i] = cpu_to_le16(blknum); 427 | emu3_set_emu3_inode_data(dir, e3d_dir); 428 | mark_buffer_dirty_inode(db, dir); 429 | 430 | *b = sb_bread(dir->i_sb, blknum); 431 | if (!*b) { 432 | printk(KERN_CRIT EMU3_ERR_NOT_BLK, EMU3_MODULE_NAME, blknum); 433 | err = -EIO; 434 | goto cleanup; 435 | } 436 | 437 | emu3_use_dir_content_block(info, blknum); 438 | 439 | *dnum = EMU3_DNUM(blknum, 0); 440 | 441 | *e3d = (struct emu3_dentry *)(*b)->b_data; 442 | 443 | dir->i_blocks++; 444 | dir->i_size = dir->i_blocks * EMU3_BSIZE; 445 | inode_set_mtime_to_ts(dir, current_time(dir)); 446 | mark_inode_dirty(dir); 447 | 448 | add_id: 449 | (*e3d)->data.unknown = 0; 450 | 451 | id = emu3_get_free_file_id(dir); 452 | if (id < 0) { 453 | printk(KERN_CRIT 454 | "%s: No ID available for a newly created dentry\n", 455 | EMU3_MODULE_NAME); 456 | err = -EIO; 457 | } else 458 | (*e3d)->data.id = id; 459 | 460 | cleanup: 461 | brelse(db); 462 | return err; 463 | } 464 | 465 | static int emu3_add_file_dentry(struct inode *dir, struct dentry *dentry, 466 | unsigned int *dnum, struct emu3_dentry **e3d, 467 | struct buffer_head **b) 468 | { 469 | int err = 0; 470 | short start_cluster; 471 | struct super_block *sb = dir->i_sb; 472 | struct emu3_sb_info *info = EMU3_SB(sb); 473 | 474 | if (!dentry->d_name.len) 475 | return -ENOENT; 476 | 477 | if (dentry->d_name.len > EMU3_LENGTH_FILENAME) 478 | return -ENAMETOOLONG; 479 | 480 | start_cluster = emu3_next_free_cluster(info); 481 | if (start_cluster < 0) 482 | return -ENOSPC; 483 | 484 | err = emu3_find_empty_file_dentry(dir, e3d, b, dnum); 485 | if (err) 486 | return err; 487 | 488 | emu3_set_dentry_name(*e3d, &dentry->d_name); 489 | //The id is set in emu3_find_empty_file_dentry 490 | emu3_init_fattrs(info, &(*e3d)->data.fattrs, start_cluster); 491 | mark_buffer_dirty_inode(*b, dir); 492 | 493 | return err; 494 | } 495 | 496 | static int emu3_create(struct mnt_idmap *idmap, struct inode *dir, 497 | struct dentry *dentry, umode_t mode, bool excl) 498 | { 499 | int err; 500 | unsigned int dnum; 501 | struct inode *inode; 502 | struct timespec64 tv; 503 | struct buffer_head *b; 504 | struct emu3_dentry *e3d; 505 | struct super_block *sb = dir->i_sb; 506 | struct emu3_sb_info *info = EMU3_SB(sb); 507 | 508 | mutex_lock(&info->lock); 509 | 510 | //Files are not allowed at root 511 | if (EMU3_IS_I_ROOT_DIR(dir)) { 512 | err = -EPERM; 513 | goto end; 514 | } 515 | 516 | inode = new_inode(sb); 517 | if (!inode) { 518 | err = -ENOSPC; 519 | goto end; 520 | } 521 | 522 | err = emu3_add_file_dentry(dir, dentry, &dnum, &e3d, &b); 523 | if (err) { 524 | iput(inode); 525 | goto end; 526 | } 527 | inode_init_owner(&nop_mnt_idmap, inode, dir, mode); 528 | tv = inode_set_ctime_current(inode); 529 | inode_set_mtime_to_ts(inode, tv); 530 | inode_set_ctime_to_ts(inode, tv); 531 | inode->i_blocks = info->blocks_per_cluster; 532 | inode->i_op = &emu3_inode_operations_file; 533 | inode->i_fop = &emu3_file_operations_file; 534 | inode->i_opflags |= IOP_XATTR; 535 | inode->i_mapping->a_ops = &emu3_aops; 536 | inode->i_ino = emu3_get_or_add_i_map(info, dnum); 537 | inode->i_size = 0; 538 | 539 | emu3_set_emu3_inode_data(inode, e3d); 540 | brelse(b); 541 | 542 | emu3_init_cluster_list(inode); 543 | 544 | insert_inode_hash(inode); 545 | mark_inode_dirty(inode); 546 | 547 | d_instantiate(dentry, inode); 548 | 549 | end: 550 | mutex_unlock(&info->lock); 551 | return err; 552 | } 553 | 554 | static bool emu3_is_dir_blk_used(struct emu3_dentry *e3d) 555 | { 556 | int i; 557 | 558 | for (i = 0; i < EMU3_ENTRIES_PER_BLOCK; i++, e3d++) { 559 | if (EMU3_DENTRY_IS_FILE(e3d)) 560 | return 1; 561 | } 562 | 563 | return 0; 564 | } 565 | 566 | static bool emu3_is_dir_empty(struct emu3_dentry *e3d_dir, 567 | struct super_block *sb) 568 | { 569 | int i; 570 | struct buffer_head *b; 571 | struct emu3_dentry *e3d; 572 | short blknum, *block = e3d_dir->data.dattrs.block_list; 573 | 574 | for (i = 0; i < EMU3_BLOCKS_PER_DIR; i++, block++) { 575 | blknum = le16_to_cpu(*block); 576 | if (EMU3_IS_DIR_BLOCK_FREE(blknum)) 577 | break; 578 | 579 | b = sb_bread(sb, blknum); 580 | if (!b) { 581 | printk(KERN_CRIT EMU3_ERR_NOT_BLK, EMU3_MODULE_NAME, 582 | blknum); 583 | return 0; 584 | } 585 | 586 | e3d = (struct emu3_dentry *)b->b_data; 587 | 588 | if (emu3_is_dir_blk_used(e3d)) { 589 | brelse(b); 590 | return 0; 591 | } 592 | 593 | brelse(b); 594 | } 595 | 596 | return 1; 597 | } 598 | 599 | static struct emu3_dentry *emu3_find_empty_dir_dentry(struct super_block *sb, 600 | struct buffer_head **b, 601 | unsigned int *dnum) 602 | { 603 | int i, j; 604 | unsigned int blknum; 605 | struct emu3_dentry *e3d; 606 | struct emu3_sb_info *info = EMU3_SB(sb); 607 | 608 | for (i = 0; i < info->root_blocks; i++) { 609 | blknum = info->start_root_block + i; 610 | 611 | *b = sb_bread(sb, blknum); 612 | if (!*b) { 613 | printk(KERN_CRIT EMU3_ERR_NOT_BLK, EMU3_MODULE_NAME, 614 | blknum); 615 | return NULL; 616 | } 617 | 618 | e3d = (struct emu3_dentry *)(*b)->b_data; 619 | 620 | for (j = 0; j < EMU3_ENTRIES_PER_BLOCK; j++, e3d++) { 621 | if (!EMU3_DENTRY_IS_DIR(e3d)) { 622 | *dnum = EMU3_DNUM(blknum, j); 623 | return e3d; 624 | } 625 | } 626 | 627 | brelse(*b); 628 | } 629 | 630 | return NULL; 631 | } 632 | 633 | static int emu3_add_dir_dentry(struct inode *dir, struct qstr *q, 634 | unsigned int *dnum, struct emu3_dentry **e3d, 635 | struct buffer_head **b) 636 | { 637 | int i; 638 | short blknum; 639 | struct emu3_sb_info *info = EMU3_SB(dir->i_sb); 640 | struct super_block *sb = dir->i_sb; 641 | 642 | if (!q->len) 643 | return -ENOENT; 644 | 645 | if (q->len > EMU3_LENGTH_FILENAME) 646 | return -ENAMETOOLONG; 647 | 648 | *e3d = emu3_find_empty_dir_dentry(sb, b, dnum); 649 | 650 | if (!*e3d) 651 | return -ENOSPC; 652 | 653 | blknum = emu3_get_free_dir_content_blknum(info); 654 | if (blknum < 0) { 655 | brelse(*b); 656 | return -ENOSPC; 657 | } 658 | 659 | emu3_use_dir_content_block(info, blknum); 660 | 661 | emu3_set_dentry_name(*e3d, q); 662 | (*e3d)->data.unknown = 0; 663 | (*e3d)->data.id = EMU3_DTYPE_1; 664 | (*e3d)->data.dattrs.block_list[0] = cpu_to_le16(blknum); 665 | for (i = 1; i < EMU3_BLOCKS_PER_DIR; i++) { 666 | (*e3d)->data.dattrs.block_list[i] = 667 | cpu_to_le16(EMU3_FREE_DIR_BLOCK); 668 | } 669 | inode_set_mtime_to_ts(dir, current_time(dir)); 670 | mark_buffer_dirty_inode(*b, dir); 671 | 672 | return 0; 673 | } 674 | 675 | static int emu3_unlink(struct inode *dir, struct dentry *dentry) 676 | { 677 | struct timespec64 tv; 678 | struct buffer_head *b; 679 | struct emu3_dentry *e3d; 680 | struct inode *inode = dentry->d_inode; 681 | struct emu3_sb_info *info = EMU3_SB(inode->i_sb); 682 | 683 | e3d = emu3_find_dentry_by_inode(inode, &b); 684 | 685 | if (e3d == NULL) 686 | return -ENOENT; 687 | 688 | mutex_lock(&info->lock); 689 | 690 | e3d->data.fattrs.type = EMU3_FTYPE_DEL; 691 | mark_buffer_dirty_inode(b, dir); 692 | tv = inode_set_ctime_current(dir); 693 | mark_inode_dirty(dir); 694 | inode_set_ctime_to_ts(inode, tv); 695 | inode_dec_link_count(inode); 696 | brelse(b); 697 | 698 | mutex_unlock(&info->lock); 699 | 700 | return 0; 701 | } 702 | 703 | static int emu3_rename(struct mnt_idmap *idmap, struct inode *old_dir, 704 | struct dentry *old_dentry, struct inode *new_dir, 705 | struct dentry *new_dentry, unsigned int flags) 706 | { 707 | int err = 0; 708 | unsigned char id; 709 | unsigned int dnum = 0; 710 | struct super_block *sb = old_dentry->d_inode->i_sb; 711 | struct emu3_sb_info *info = EMU3_SB(sb); 712 | struct buffer_head *old_b, *new_b; 713 | struct emu3_dentry *old_e3d, *new_e3d; 714 | 715 | if (flags & ~RENAME_NOREPLACE) 716 | return -EINVAL; 717 | 718 | mutex_lock(&info->lock); 719 | 720 | if (EMU3_IS_I_ROOT_DIR(old_dir) && !EMU3_IS_I_ROOT_DIR(new_dir)) { 721 | //The emu3 filesystem does not allow directories in directories. 722 | err = -EPERM; 723 | goto end; 724 | } 725 | 726 | if (new_dentry->d_inode) { 727 | if (flags & RENAME_NOREPLACE) { 728 | err = -EEXIST; 729 | goto cleanup; 730 | } 731 | 732 | new_e3d = 733 | emu3_find_dentry_by_inode(new_dentry->d_inode, &new_b); 734 | if (new_e3d) { 735 | if (old_dir == new_dir) { 736 | new_e3d->data.fattrs.type = EMU3_FTYPE_DEL; 737 | mark_buffer_dirty_inode(new_b, new_dir); 738 | inode_set_mtime_to_ts(new_dir, 739 | current_time(new_dir)); 740 | mark_inode_dirty(new_dir); 741 | } 742 | } else 743 | printk(KERN_WARNING 744 | "%s: No entry found. As it was meant to be deleted, we can continue safely.\n", 745 | EMU3_MODULE_NAME); 746 | 747 | brelse(new_b); 748 | 749 | dnum = emu3_get_i_map(info, new_dentry->d_inode); 750 | inode_dec_link_count(new_dentry->d_inode); 751 | d_delete(new_dentry); 752 | } 753 | 754 | old_e3d = emu3_find_dentry_by_inode(old_dentry->d_inode, &old_b); 755 | if (!old_e3d) { 756 | err = -ENOENT; 757 | goto end; 758 | } 759 | 760 | if (old_dir == new_dir) { 761 | emu3_set_dentry_name(old_e3d, &new_dentry->d_name); 762 | mark_buffer_dirty_inode(old_b, old_dir); 763 | inode_set_mtime_to_ts(old_dir, current_time(old_dir)); 764 | mark_inode_dirty(old_dir); 765 | } else { 766 | if (dnum) 767 | emu3_set_i_map(info, old_dentry->d_inode, dnum); 768 | else { 769 | err = emu3_find_empty_file_dentry(new_dir, &new_e3d, 770 | &new_b, &dnum); 771 | if (err) 772 | goto cleanup; 773 | 774 | id = new_e3d->data.id; 775 | memcpy(new_e3d, old_e3d, sizeof(struct emu3_dentry)); 776 | new_e3d->data.id = id; 777 | 778 | emu3_set_emu3_inode_data(old_dentry->d_inode, new_e3d); 779 | 780 | inode_set_mtime_to_ts(new_dir, current_time(old_dir)); 781 | mark_buffer_dirty_inode(new_b, new_dir); 782 | 783 | emu3_set_i_map(info, old_dentry->d_inode, dnum); 784 | } 785 | 786 | old_e3d->data.fattrs.type = EMU3_FTYPE_DEL; 787 | mark_buffer_dirty_inode(old_b, old_dir); 788 | inode_set_mtime_to_ts(old_dir, current_time(old_dir)); 789 | mark_inode_dirty(old_dir); 790 | } 791 | 792 | cleanup: 793 | brelse(old_b); 794 | end: 795 | mutex_unlock(&info->lock); 796 | return err; 797 | } 798 | 799 | static int emu3_mkdir(struct mnt_idmap *idmap, struct inode *dir, 800 | struct dentry *dentry, umode_t mode) 801 | { 802 | int err; 803 | unsigned int dnum; 804 | struct inode *inode; 805 | struct timespec64 tv; 806 | struct buffer_head *b; 807 | struct emu3_dentry *e3d; 808 | struct super_block *sb = dir->i_sb; 809 | struct emu3_sb_info *info = EMU3_SB(sb); 810 | 811 | if (!EMU3_IS_I_ROOT_DIR(dir)) 812 | return -EPERM; 813 | 814 | inode = new_inode(sb); 815 | if (!inode) 816 | return -ENOSPC; 817 | 818 | mutex_lock(&info->lock); 819 | 820 | err = emu3_add_dir_dentry(dir, &dentry->d_name, &dnum, &e3d, &b); 821 | if (err) { 822 | mutex_unlock(&info->lock); 823 | iput(inode); 824 | return err; 825 | } 826 | 827 | inode_init_owner(&nop_mnt_idmap, inode, dir, EMU3_DIR_MODE); 828 | inode->i_blocks = 1; 829 | inode->i_op = &emu3_inode_operations_dir; 830 | inode->i_fop = &emu3_file_operations_dir; 831 | inode->i_opflags &= ~IOP_XATTR; 832 | inode->i_ino = emu3_get_or_add_i_map(info, dnum); 833 | inode->i_size = EMU3_BSIZE; 834 | tv = inode_set_ctime_current(inode); 835 | inode_set_mtime_to_ts(inode, tv); 836 | inode_set_ctime_to_ts(inode, tv); 837 | 838 | emu3_set_emu3_inode_data(inode, e3d); 839 | brelse(b); 840 | 841 | set_nlink(inode, 2); 842 | inode_inc_link_count(dir); 843 | 844 | insert_inode_hash(inode); 845 | mark_inode_dirty(inode); 846 | mutex_unlock(&info->lock); 847 | 848 | d_instantiate(dentry, inode); 849 | 850 | return 0; 851 | } 852 | 853 | static int emu3_rmdir(struct inode *dir, struct dentry *dentry) 854 | { 855 | int i, ret = 0; 856 | short *block, blknum; 857 | struct buffer_head *b; 858 | struct emu3_dentry *e3d; 859 | struct inode *inode = d_inode(dentry); 860 | struct emu3_sb_info *info = EMU3_SB(inode->i_sb); 861 | 862 | mutex_lock(&info->lock); 863 | 864 | e3d = emu3_find_dentry_by_inode(inode, &b); 865 | if (!e3d) { 866 | ret = -ENOENT; 867 | goto end; 868 | } 869 | 870 | if (!EMU3_DENTRY_IS_DIR(e3d)) { 871 | ret = -ENOTDIR; 872 | goto cleanup; 873 | } 874 | 875 | if (!emu3_is_dir_empty(e3d, inode->i_sb)) { 876 | ret = -ENOTEMPTY; 877 | goto cleanup; 878 | } 879 | 880 | block = e3d->data.dattrs.block_list; 881 | for (i = 0; i < EMU3_BLOCKS_PER_DIR; i++, block++) { 882 | blknum = le16_to_cpu(*block); 883 | if (EMU3_IS_DIR_BLOCK_FREE(blknum)) 884 | break; 885 | 886 | emu3_free_dir_content_block(info, blknum); 887 | } 888 | 889 | memset(e3d, 0, sizeof(struct emu3_dentry)); 890 | mark_buffer_dirty_inode(b, dir); 891 | emu3_clear_i_map(info, inode); 892 | inode_dec_link_count(inode); 893 | inode_dec_link_count(inode); 894 | inode_dec_link_count(dir); 895 | cleanup: 896 | brelse(b); 897 | end: 898 | mutex_unlock(&info->lock); 899 | return ret; 900 | } 901 | 902 | WRAP_DIR_ITER(emu3_iterate) // FIXME! 903 | const struct file_operations emu3_file_operations_dir = { 904 | .read = generic_read_dir, 905 | .iterate_shared = shared_emu3_iterate, 906 | .fsync = generic_file_fsync, 907 | .llseek = generic_file_llseek, 908 | }; 909 | 910 | const struct inode_operations emu3_inode_operations_dir = { 911 | .create = emu3_create, 912 | .lookup = emu3_lookup, 913 | .unlink = emu3_unlink, 914 | .rename = emu3_rename, 915 | .mkdir = emu3_mkdir, 916 | .rmdir = emu3_rmdir 917 | }; 918 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------