18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * inode.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 1999 Al Smith 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Portions derived from work (c) 1995,1996 Christian Vogelgsang, 88c2ecf20Sopenharmony_ci * and from work (c) 1998 Mike Shaver. 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/buffer_head.h> 128c2ecf20Sopenharmony_ci#include <linux/module.h> 138c2ecf20Sopenharmony_ci#include <linux/fs.h> 148c2ecf20Sopenharmony_ci#include "efs.h" 158c2ecf20Sopenharmony_ci#include <linux/efs_fs_sb.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_cistatic int efs_readpage(struct file *file, struct page *page) 188c2ecf20Sopenharmony_ci{ 198c2ecf20Sopenharmony_ci return block_read_full_page(page,efs_get_block); 208c2ecf20Sopenharmony_ci} 218c2ecf20Sopenharmony_cistatic sector_t _efs_bmap(struct address_space *mapping, sector_t block) 228c2ecf20Sopenharmony_ci{ 238c2ecf20Sopenharmony_ci return generic_block_bmap(mapping,block,efs_get_block); 248c2ecf20Sopenharmony_ci} 258c2ecf20Sopenharmony_cistatic const struct address_space_operations efs_aops = { 268c2ecf20Sopenharmony_ci .readpage = efs_readpage, 278c2ecf20Sopenharmony_ci .bmap = _efs_bmap 288c2ecf20Sopenharmony_ci}; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistatic inline void extent_copy(efs_extent *src, efs_extent *dst) { 318c2ecf20Sopenharmony_ci /* 328c2ecf20Sopenharmony_ci * this is slightly evil. it doesn't just copy 338c2ecf20Sopenharmony_ci * efs_extent from src to dst, it also mangles 348c2ecf20Sopenharmony_ci * the bits so that dst ends up in cpu byte-order. 358c2ecf20Sopenharmony_ci */ 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci dst->cooked.ex_magic = (unsigned int) src->raw[0]; 388c2ecf20Sopenharmony_ci dst->cooked.ex_bn = ((unsigned int) src->raw[1] << 16) | 398c2ecf20Sopenharmony_ci ((unsigned int) src->raw[2] << 8) | 408c2ecf20Sopenharmony_ci ((unsigned int) src->raw[3] << 0); 418c2ecf20Sopenharmony_ci dst->cooked.ex_length = (unsigned int) src->raw[4]; 428c2ecf20Sopenharmony_ci dst->cooked.ex_offset = ((unsigned int) src->raw[5] << 16) | 438c2ecf20Sopenharmony_ci ((unsigned int) src->raw[6] << 8) | 448c2ecf20Sopenharmony_ci ((unsigned int) src->raw[7] << 0); 458c2ecf20Sopenharmony_ci return; 468c2ecf20Sopenharmony_ci} 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cistruct inode *efs_iget(struct super_block *super, unsigned long ino) 498c2ecf20Sopenharmony_ci{ 508c2ecf20Sopenharmony_ci int i, inode_index; 518c2ecf20Sopenharmony_ci dev_t device; 528c2ecf20Sopenharmony_ci u32 rdev; 538c2ecf20Sopenharmony_ci struct buffer_head *bh; 548c2ecf20Sopenharmony_ci struct efs_sb_info *sb = SUPER_INFO(super); 558c2ecf20Sopenharmony_ci struct efs_inode_info *in; 568c2ecf20Sopenharmony_ci efs_block_t block, offset; 578c2ecf20Sopenharmony_ci struct efs_dinode *efs_inode; 588c2ecf20Sopenharmony_ci struct inode *inode; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci inode = iget_locked(super, ino); 618c2ecf20Sopenharmony_ci if (!inode) 628c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 638c2ecf20Sopenharmony_ci if (!(inode->i_state & I_NEW)) 648c2ecf20Sopenharmony_ci return inode; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci in = INODE_INFO(inode); 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci /* 698c2ecf20Sopenharmony_ci ** EFS layout: 708c2ecf20Sopenharmony_ci ** 718c2ecf20Sopenharmony_ci ** | cylinder group | cylinder group | cylinder group ..etc 728c2ecf20Sopenharmony_ci ** |inodes|data |inodes|data |inodes|data ..etc 738c2ecf20Sopenharmony_ci ** 748c2ecf20Sopenharmony_ci ** work out the inode block index, (considering initially that the 758c2ecf20Sopenharmony_ci ** inodes are stored as consecutive blocks). then work out the block 768c2ecf20Sopenharmony_ci ** number of that inode given the above layout, and finally the 778c2ecf20Sopenharmony_ci ** offset of the inode within that block. 788c2ecf20Sopenharmony_ci */ 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci inode_index = inode->i_ino / 818c2ecf20Sopenharmony_ci (EFS_BLOCKSIZE / sizeof(struct efs_dinode)); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci block = sb->fs_start + sb->first_block + 848c2ecf20Sopenharmony_ci (sb->group_size * (inode_index / sb->inode_blocks)) + 858c2ecf20Sopenharmony_ci (inode_index % sb->inode_blocks); 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci offset = (inode->i_ino % 888c2ecf20Sopenharmony_ci (EFS_BLOCKSIZE / sizeof(struct efs_dinode))) * 898c2ecf20Sopenharmony_ci sizeof(struct efs_dinode); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci bh = sb_bread(inode->i_sb, block); 928c2ecf20Sopenharmony_ci if (!bh) { 938c2ecf20Sopenharmony_ci pr_warn("%s() failed at block %d\n", __func__, block); 948c2ecf20Sopenharmony_ci goto read_inode_error; 958c2ecf20Sopenharmony_ci } 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci efs_inode = (struct efs_dinode *) (bh->b_data + offset); 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci inode->i_mode = be16_to_cpu(efs_inode->di_mode); 1008c2ecf20Sopenharmony_ci set_nlink(inode, be16_to_cpu(efs_inode->di_nlink)); 1018c2ecf20Sopenharmony_ci i_uid_write(inode, (uid_t)be16_to_cpu(efs_inode->di_uid)); 1028c2ecf20Sopenharmony_ci i_gid_write(inode, (gid_t)be16_to_cpu(efs_inode->di_gid)); 1038c2ecf20Sopenharmony_ci inode->i_size = be32_to_cpu(efs_inode->di_size); 1048c2ecf20Sopenharmony_ci inode->i_atime.tv_sec = be32_to_cpu(efs_inode->di_atime); 1058c2ecf20Sopenharmony_ci inode->i_mtime.tv_sec = be32_to_cpu(efs_inode->di_mtime); 1068c2ecf20Sopenharmony_ci inode->i_ctime.tv_sec = be32_to_cpu(efs_inode->di_ctime); 1078c2ecf20Sopenharmony_ci inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci /* this is the number of blocks in the file */ 1108c2ecf20Sopenharmony_ci if (inode->i_size == 0) { 1118c2ecf20Sopenharmony_ci inode->i_blocks = 0; 1128c2ecf20Sopenharmony_ci } else { 1138c2ecf20Sopenharmony_ci inode->i_blocks = ((inode->i_size - 1) >> EFS_BLOCKSIZE_BITS) + 1; 1148c2ecf20Sopenharmony_ci } 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci rdev = be16_to_cpu(efs_inode->di_u.di_dev.odev); 1178c2ecf20Sopenharmony_ci if (rdev == 0xffff) { 1188c2ecf20Sopenharmony_ci rdev = be32_to_cpu(efs_inode->di_u.di_dev.ndev); 1198c2ecf20Sopenharmony_ci if (sysv_major(rdev) > 0xfff) 1208c2ecf20Sopenharmony_ci device = 0; 1218c2ecf20Sopenharmony_ci else 1228c2ecf20Sopenharmony_ci device = MKDEV(sysv_major(rdev), sysv_minor(rdev)); 1238c2ecf20Sopenharmony_ci } else 1248c2ecf20Sopenharmony_ci device = old_decode_dev(rdev); 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci /* get the number of extents for this object */ 1278c2ecf20Sopenharmony_ci in->numextents = be16_to_cpu(efs_inode->di_numextents); 1288c2ecf20Sopenharmony_ci in->lastextent = 0; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci /* copy the extents contained within the inode to memory */ 1318c2ecf20Sopenharmony_ci for(i = 0; i < EFS_DIRECTEXTENTS; i++) { 1328c2ecf20Sopenharmony_ci extent_copy(&(efs_inode->di_u.di_extents[i]), &(in->extents[i])); 1338c2ecf20Sopenharmony_ci if (i < in->numextents && in->extents[i].cooked.ex_magic != 0) { 1348c2ecf20Sopenharmony_ci pr_warn("extent %d has bad magic number in inode %lu\n", 1358c2ecf20Sopenharmony_ci i, inode->i_ino); 1368c2ecf20Sopenharmony_ci brelse(bh); 1378c2ecf20Sopenharmony_ci goto read_inode_error; 1388c2ecf20Sopenharmony_ci } 1398c2ecf20Sopenharmony_ci } 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci brelse(bh); 1428c2ecf20Sopenharmony_ci pr_debug("efs_iget(): inode %lu, extents %d, mode %o\n", 1438c2ecf20Sopenharmony_ci inode->i_ino, in->numextents, inode->i_mode); 1448c2ecf20Sopenharmony_ci switch (inode->i_mode & S_IFMT) { 1458c2ecf20Sopenharmony_ci case S_IFDIR: 1468c2ecf20Sopenharmony_ci inode->i_op = &efs_dir_inode_operations; 1478c2ecf20Sopenharmony_ci inode->i_fop = &efs_dir_operations; 1488c2ecf20Sopenharmony_ci break; 1498c2ecf20Sopenharmony_ci case S_IFREG: 1508c2ecf20Sopenharmony_ci inode->i_fop = &generic_ro_fops; 1518c2ecf20Sopenharmony_ci inode->i_data.a_ops = &efs_aops; 1528c2ecf20Sopenharmony_ci break; 1538c2ecf20Sopenharmony_ci case S_IFLNK: 1548c2ecf20Sopenharmony_ci inode->i_op = &page_symlink_inode_operations; 1558c2ecf20Sopenharmony_ci inode_nohighmem(inode); 1568c2ecf20Sopenharmony_ci inode->i_data.a_ops = &efs_symlink_aops; 1578c2ecf20Sopenharmony_ci break; 1588c2ecf20Sopenharmony_ci case S_IFCHR: 1598c2ecf20Sopenharmony_ci case S_IFBLK: 1608c2ecf20Sopenharmony_ci case S_IFIFO: 1618c2ecf20Sopenharmony_ci init_special_inode(inode, inode->i_mode, device); 1628c2ecf20Sopenharmony_ci break; 1638c2ecf20Sopenharmony_ci default: 1648c2ecf20Sopenharmony_ci pr_warn("unsupported inode mode %o\n", inode->i_mode); 1658c2ecf20Sopenharmony_ci goto read_inode_error; 1668c2ecf20Sopenharmony_ci break; 1678c2ecf20Sopenharmony_ci } 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci unlock_new_inode(inode); 1708c2ecf20Sopenharmony_ci return inode; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ciread_inode_error: 1738c2ecf20Sopenharmony_ci pr_warn("failed to read inode %lu\n", inode->i_ino); 1748c2ecf20Sopenharmony_ci iget_failed(inode); 1758c2ecf20Sopenharmony_ci return ERR_PTR(-EIO); 1768c2ecf20Sopenharmony_ci} 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_cistatic inline efs_block_t 1798c2ecf20Sopenharmony_ciefs_extent_check(efs_extent *ptr, efs_block_t block, struct efs_sb_info *sb) { 1808c2ecf20Sopenharmony_ci efs_block_t start; 1818c2ecf20Sopenharmony_ci efs_block_t length; 1828c2ecf20Sopenharmony_ci efs_block_t offset; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci /* 1858c2ecf20Sopenharmony_ci * given an extent and a logical block within a file, 1868c2ecf20Sopenharmony_ci * can this block be found within this extent ? 1878c2ecf20Sopenharmony_ci */ 1888c2ecf20Sopenharmony_ci start = ptr->cooked.ex_bn; 1898c2ecf20Sopenharmony_ci length = ptr->cooked.ex_length; 1908c2ecf20Sopenharmony_ci offset = ptr->cooked.ex_offset; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci if ((block >= offset) && (block < offset+length)) { 1938c2ecf20Sopenharmony_ci return(sb->fs_start + start + block - offset); 1948c2ecf20Sopenharmony_ci } else { 1958c2ecf20Sopenharmony_ci return 0; 1968c2ecf20Sopenharmony_ci } 1978c2ecf20Sopenharmony_ci} 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ciefs_block_t efs_map_block(struct inode *inode, efs_block_t block) { 2008c2ecf20Sopenharmony_ci struct efs_sb_info *sb = SUPER_INFO(inode->i_sb); 2018c2ecf20Sopenharmony_ci struct efs_inode_info *in = INODE_INFO(inode); 2028c2ecf20Sopenharmony_ci struct buffer_head *bh = NULL; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci int cur, last, first = 1; 2058c2ecf20Sopenharmony_ci int ibase, ioffset, dirext, direxts, indext, indexts; 2068c2ecf20Sopenharmony_ci efs_block_t iblock, result = 0, lastblock = 0; 2078c2ecf20Sopenharmony_ci efs_extent ext, *exts; 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci last = in->lastextent; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci if (in->numextents <= EFS_DIRECTEXTENTS) { 2128c2ecf20Sopenharmony_ci /* first check the last extent we returned */ 2138c2ecf20Sopenharmony_ci if ((result = efs_extent_check(&in->extents[last], block, sb))) 2148c2ecf20Sopenharmony_ci return result; 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci /* if we only have one extent then nothing can be found */ 2178c2ecf20Sopenharmony_ci if (in->numextents == 1) { 2188c2ecf20Sopenharmony_ci pr_err("%s() failed to map (1 extent)\n", __func__); 2198c2ecf20Sopenharmony_ci return 0; 2208c2ecf20Sopenharmony_ci } 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci direxts = in->numextents; 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci /* 2258c2ecf20Sopenharmony_ci * check the stored extents in the inode 2268c2ecf20Sopenharmony_ci * start with next extent and check forwards 2278c2ecf20Sopenharmony_ci */ 2288c2ecf20Sopenharmony_ci for(dirext = 1; dirext < direxts; dirext++) { 2298c2ecf20Sopenharmony_ci cur = (last + dirext) % in->numextents; 2308c2ecf20Sopenharmony_ci if ((result = efs_extent_check(&in->extents[cur], block, sb))) { 2318c2ecf20Sopenharmony_ci in->lastextent = cur; 2328c2ecf20Sopenharmony_ci return result; 2338c2ecf20Sopenharmony_ci } 2348c2ecf20Sopenharmony_ci } 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci pr_err("%s() failed to map block %u (dir)\n", __func__, block); 2378c2ecf20Sopenharmony_ci return 0; 2388c2ecf20Sopenharmony_ci } 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci pr_debug("%s(): indirect search for logical block %u\n", 2418c2ecf20Sopenharmony_ci __func__, block); 2428c2ecf20Sopenharmony_ci direxts = in->extents[0].cooked.ex_offset; 2438c2ecf20Sopenharmony_ci indexts = in->numextents; 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci for(indext = 0; indext < indexts; indext++) { 2468c2ecf20Sopenharmony_ci cur = (last + indext) % indexts; 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci /* 2498c2ecf20Sopenharmony_ci * work out which direct extent contains `cur'. 2508c2ecf20Sopenharmony_ci * 2518c2ecf20Sopenharmony_ci * also compute ibase: i.e. the number of the first 2528c2ecf20Sopenharmony_ci * indirect extent contained within direct extent `cur'. 2538c2ecf20Sopenharmony_ci * 2548c2ecf20Sopenharmony_ci */ 2558c2ecf20Sopenharmony_ci ibase = 0; 2568c2ecf20Sopenharmony_ci for(dirext = 0; cur < ibase && dirext < direxts; dirext++) { 2578c2ecf20Sopenharmony_ci ibase += in->extents[dirext].cooked.ex_length * 2588c2ecf20Sopenharmony_ci (EFS_BLOCKSIZE / sizeof(efs_extent)); 2598c2ecf20Sopenharmony_ci } 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci if (dirext == direxts) { 2628c2ecf20Sopenharmony_ci /* should never happen */ 2638c2ecf20Sopenharmony_ci pr_err("couldn't find direct extent for indirect extent %d (block %u)\n", 2648c2ecf20Sopenharmony_ci cur, block); 2658c2ecf20Sopenharmony_ci if (bh) brelse(bh); 2668c2ecf20Sopenharmony_ci return 0; 2678c2ecf20Sopenharmony_ci } 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci /* work out block number and offset of this indirect extent */ 2708c2ecf20Sopenharmony_ci iblock = sb->fs_start + in->extents[dirext].cooked.ex_bn + 2718c2ecf20Sopenharmony_ci (cur - ibase) / 2728c2ecf20Sopenharmony_ci (EFS_BLOCKSIZE / sizeof(efs_extent)); 2738c2ecf20Sopenharmony_ci ioffset = (cur - ibase) % 2748c2ecf20Sopenharmony_ci (EFS_BLOCKSIZE / sizeof(efs_extent)); 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci if (first || lastblock != iblock) { 2778c2ecf20Sopenharmony_ci if (bh) brelse(bh); 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci bh = sb_bread(inode->i_sb, iblock); 2808c2ecf20Sopenharmony_ci if (!bh) { 2818c2ecf20Sopenharmony_ci pr_err("%s() failed at block %d\n", 2828c2ecf20Sopenharmony_ci __func__, iblock); 2838c2ecf20Sopenharmony_ci return 0; 2848c2ecf20Sopenharmony_ci } 2858c2ecf20Sopenharmony_ci pr_debug("%s(): read indirect extent block %d\n", 2868c2ecf20Sopenharmony_ci __func__, iblock); 2878c2ecf20Sopenharmony_ci first = 0; 2888c2ecf20Sopenharmony_ci lastblock = iblock; 2898c2ecf20Sopenharmony_ci } 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci exts = (efs_extent *) bh->b_data; 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci extent_copy(&(exts[ioffset]), &ext); 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci if (ext.cooked.ex_magic != 0) { 2968c2ecf20Sopenharmony_ci pr_err("extent %d has bad magic number in block %d\n", 2978c2ecf20Sopenharmony_ci cur, iblock); 2988c2ecf20Sopenharmony_ci if (bh) brelse(bh); 2998c2ecf20Sopenharmony_ci return 0; 3008c2ecf20Sopenharmony_ci } 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci if ((result = efs_extent_check(&ext, block, sb))) { 3038c2ecf20Sopenharmony_ci if (bh) brelse(bh); 3048c2ecf20Sopenharmony_ci in->lastextent = cur; 3058c2ecf20Sopenharmony_ci return result; 3068c2ecf20Sopenharmony_ci } 3078c2ecf20Sopenharmony_ci } 3088c2ecf20Sopenharmony_ci if (bh) brelse(bh); 3098c2ecf20Sopenharmony_ci pr_err("%s() failed to map block %u (indir)\n", __func__, block); 3108c2ecf20Sopenharmony_ci return 0; 3118c2ecf20Sopenharmony_ci} 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 314