Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Compressed rom filesystem for Linux.
0003  *
0004  * Copyright (C) 1999 Linus Torvalds.
0005  *
0006  * This file is released under the GPL.
0007  */
0008 
0009 /*
0010  * These are the VFS interfaces to the compressed rom filesystem.
0011  * The actual compression is based on zlib, see the other files.
0012  */
0013 
0014 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0015 
0016 #include <linux/module.h>
0017 #include <linux/fs.h>
0018 #include <linux/file.h>
0019 #include <linux/pagemap.h>
0020 #include <linux/pfn_t.h>
0021 #include <linux/ramfs.h>
0022 #include <linux/init.h>
0023 #include <linux/string.h>
0024 #include <linux/blkdev.h>
0025 #include <linux/mtd/mtd.h>
0026 #include <linux/mtd/super.h>
0027 #include <linux/fs_context.h>
0028 #include <linux/slab.h>
0029 #include <linux/vfs.h>
0030 #include <linux/mutex.h>
0031 #include <uapi/linux/cramfs_fs.h>
0032 #include <linux/uaccess.h>
0033 
0034 #include "internal.h"
0035 
0036 /*
0037  * cramfs super-block data in memory
0038  */
0039 struct cramfs_sb_info {
0040     unsigned long magic;
0041     unsigned long size;
0042     unsigned long blocks;
0043     unsigned long files;
0044     unsigned long flags;
0045     void *linear_virt_addr;
0046     resource_size_t linear_phys_addr;
0047     size_t mtd_point_size;
0048 };
0049 
0050 static inline struct cramfs_sb_info *CRAMFS_SB(struct super_block *sb)
0051 {
0052     return sb->s_fs_info;
0053 }
0054 
0055 static const struct super_operations cramfs_ops;
0056 static const struct inode_operations cramfs_dir_inode_operations;
0057 static const struct file_operations cramfs_directory_operations;
0058 static const struct file_operations cramfs_physmem_fops;
0059 static const struct address_space_operations cramfs_aops;
0060 
0061 static DEFINE_MUTEX(read_mutex);
0062 
0063 
0064 /* These macros may change in future, to provide better st_ino semantics. */
0065 #define OFFSET(x)   ((x)->i_ino)
0066 
0067 static unsigned long cramino(const struct cramfs_inode *cino, unsigned int offset)
0068 {
0069     if (!cino->offset)
0070         return offset + 1;
0071     if (!cino->size)
0072         return offset + 1;
0073 
0074     /*
0075      * The file mode test fixes buggy mkcramfs implementations where
0076      * cramfs_inode->offset is set to a non zero value for entries
0077      * which did not contain data, like devices node and fifos.
0078      */
0079     switch (cino->mode & S_IFMT) {
0080     case S_IFREG:
0081     case S_IFDIR:
0082     case S_IFLNK:
0083         return cino->offset << 2;
0084     default:
0085         break;
0086     }
0087     return offset + 1;
0088 }
0089 
0090 static struct inode *get_cramfs_inode(struct super_block *sb,
0091     const struct cramfs_inode *cramfs_inode, unsigned int offset)
0092 {
0093     struct inode *inode;
0094     static struct timespec64 zerotime;
0095 
0096     inode = iget_locked(sb, cramino(cramfs_inode, offset));
0097     if (!inode)
0098         return ERR_PTR(-ENOMEM);
0099     if (!(inode->i_state & I_NEW))
0100         return inode;
0101 
0102     switch (cramfs_inode->mode & S_IFMT) {
0103     case S_IFREG:
0104         inode->i_fop = &generic_ro_fops;
0105         inode->i_data.a_ops = &cramfs_aops;
0106         if (IS_ENABLED(CONFIG_CRAMFS_MTD) &&
0107             CRAMFS_SB(sb)->flags & CRAMFS_FLAG_EXT_BLOCK_POINTERS &&
0108             CRAMFS_SB(sb)->linear_phys_addr)
0109             inode->i_fop = &cramfs_physmem_fops;
0110         break;
0111     case S_IFDIR:
0112         inode->i_op = &cramfs_dir_inode_operations;
0113         inode->i_fop = &cramfs_directory_operations;
0114         break;
0115     case S_IFLNK:
0116         inode->i_op = &page_symlink_inode_operations;
0117         inode_nohighmem(inode);
0118         inode->i_data.a_ops = &cramfs_aops;
0119         break;
0120     default:
0121         init_special_inode(inode, cramfs_inode->mode,
0122                 old_decode_dev(cramfs_inode->size));
0123     }
0124 
0125     inode->i_mode = cramfs_inode->mode;
0126     i_uid_write(inode, cramfs_inode->uid);
0127     i_gid_write(inode, cramfs_inode->gid);
0128 
0129     /* if the lower 2 bits are zero, the inode contains data */
0130     if (!(inode->i_ino & 3)) {
0131         inode->i_size = cramfs_inode->size;
0132         inode->i_blocks = (cramfs_inode->size - 1) / 512 + 1;
0133     }
0134 
0135     /* Struct copy intentional */
0136     inode->i_mtime = inode->i_atime = inode->i_ctime = zerotime;
0137     /* inode->i_nlink is left 1 - arguably wrong for directories,
0138        but it's the best we can do without reading the directory
0139        contents.  1 yields the right result in GNU find, even
0140        without -noleaf option. */
0141 
0142     unlock_new_inode(inode);
0143 
0144     return inode;
0145 }
0146 
0147 /*
0148  * We have our own block cache: don't fill up the buffer cache
0149  * with the rom-image, because the way the filesystem is set
0150  * up the accesses should be fairly regular and cached in the
0151  * page cache and dentry tree anyway..
0152  *
0153  * This also acts as a way to guarantee contiguous areas of up to
0154  * BLKS_PER_BUF*PAGE_SIZE, so that the caller doesn't need to
0155  * worry about end-of-buffer issues even when decompressing a full
0156  * page cache.
0157  *
0158  * Note: This is all optimized away at compile time when
0159  *       CONFIG_CRAMFS_BLOCKDEV=n.
0160  */
0161 #define READ_BUFFERS (2)
0162 /* NEXT_BUFFER(): Loop over [0..(READ_BUFFERS-1)]. */
0163 #define NEXT_BUFFER(_ix) ((_ix) ^ 1)
0164 
0165 /*
0166  * BLKS_PER_BUF_SHIFT should be at least 2 to allow for "compressed"
0167  * data that takes up more space than the original and with unlucky
0168  * alignment.
0169  */
0170 #define BLKS_PER_BUF_SHIFT  (2)
0171 #define BLKS_PER_BUF        (1 << BLKS_PER_BUF_SHIFT)
0172 #define BUFFER_SIZE     (BLKS_PER_BUF*PAGE_SIZE)
0173 
0174 static unsigned char read_buffers[READ_BUFFERS][BUFFER_SIZE];
0175 static unsigned buffer_blocknr[READ_BUFFERS];
0176 static struct super_block *buffer_dev[READ_BUFFERS];
0177 static int next_buffer;
0178 
0179 /*
0180  * Populate our block cache and return a pointer to it.
0181  */
0182 static void *cramfs_blkdev_read(struct super_block *sb, unsigned int offset,
0183                 unsigned int len)
0184 {
0185     struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
0186     struct file_ra_state ra;
0187     struct page *pages[BLKS_PER_BUF];
0188     unsigned i, blocknr, buffer;
0189     unsigned long devsize;
0190     char *data;
0191 
0192     if (!len)
0193         return NULL;
0194     blocknr = offset >> PAGE_SHIFT;
0195     offset &= PAGE_SIZE - 1;
0196 
0197     /* Check if an existing buffer already has the data.. */
0198     for (i = 0; i < READ_BUFFERS; i++) {
0199         unsigned int blk_offset;
0200 
0201         if (buffer_dev[i] != sb)
0202             continue;
0203         if (blocknr < buffer_blocknr[i])
0204             continue;
0205         blk_offset = (blocknr - buffer_blocknr[i]) << PAGE_SHIFT;
0206         blk_offset += offset;
0207         if (blk_offset > BUFFER_SIZE ||
0208             blk_offset + len > BUFFER_SIZE)
0209             continue;
0210         return read_buffers[i] + blk_offset;
0211     }
0212 
0213     devsize = bdev_nr_bytes(sb->s_bdev) >> PAGE_SHIFT;
0214 
0215     /* Ok, read in BLKS_PER_BUF pages completely first. */
0216     file_ra_state_init(&ra, mapping);
0217     page_cache_sync_readahead(mapping, &ra, NULL, blocknr, BLKS_PER_BUF);
0218 
0219     for (i = 0; i < BLKS_PER_BUF; i++) {
0220         struct page *page = NULL;
0221 
0222         if (blocknr + i < devsize) {
0223             page = read_mapping_page(mapping, blocknr + i, NULL);
0224             /* synchronous error? */
0225             if (IS_ERR(page))
0226                 page = NULL;
0227         }
0228         pages[i] = page;
0229     }
0230 
0231     buffer = next_buffer;
0232     next_buffer = NEXT_BUFFER(buffer);
0233     buffer_blocknr[buffer] = blocknr;
0234     buffer_dev[buffer] = sb;
0235 
0236     data = read_buffers[buffer];
0237     for (i = 0; i < BLKS_PER_BUF; i++) {
0238         struct page *page = pages[i];
0239 
0240         if (page) {
0241             memcpy(data, kmap(page), PAGE_SIZE);
0242             kunmap(page);
0243             put_page(page);
0244         } else
0245             memset(data, 0, PAGE_SIZE);
0246         data += PAGE_SIZE;
0247     }
0248     return read_buffers[buffer] + offset;
0249 }
0250 
0251 /*
0252  * Return a pointer to the linearly addressed cramfs image in memory.
0253  */
0254 static void *cramfs_direct_read(struct super_block *sb, unsigned int offset,
0255                 unsigned int len)
0256 {
0257     struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
0258 
0259     if (!len)
0260         return NULL;
0261     if (len > sbi->size || offset > sbi->size - len)
0262         return page_address(ZERO_PAGE(0));
0263     return sbi->linear_virt_addr + offset;
0264 }
0265 
0266 /*
0267  * Returns a pointer to a buffer containing at least LEN bytes of
0268  * filesystem starting at byte offset OFFSET into the filesystem.
0269  */
0270 static void *cramfs_read(struct super_block *sb, unsigned int offset,
0271              unsigned int len)
0272 {
0273     struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
0274 
0275     if (IS_ENABLED(CONFIG_CRAMFS_MTD) && sbi->linear_virt_addr)
0276         return cramfs_direct_read(sb, offset, len);
0277     else if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV))
0278         return cramfs_blkdev_read(sb, offset, len);
0279     else
0280         return NULL;
0281 }
0282 
0283 /*
0284  * For a mapping to be possible, we need a range of uncompressed and
0285  * contiguous blocks. Return the offset for the first block and number of
0286  * valid blocks for which that is true, or zero otherwise.
0287  */
0288 static u32 cramfs_get_block_range(struct inode *inode, u32 pgoff, u32 *pages)
0289 {
0290     struct cramfs_sb_info *sbi = CRAMFS_SB(inode->i_sb);
0291     int i;
0292     u32 *blockptrs, first_block_addr;
0293 
0294     /*
0295      * We can dereference memory directly here as this code may be
0296      * reached only when there is a direct filesystem image mapping
0297      * available in memory.
0298      */
0299     blockptrs = (u32 *)(sbi->linear_virt_addr + OFFSET(inode) + pgoff * 4);
0300     first_block_addr = blockptrs[0] & ~CRAMFS_BLK_FLAGS;
0301     i = 0;
0302     do {
0303         u32 block_off = i * (PAGE_SIZE >> CRAMFS_BLK_DIRECT_PTR_SHIFT);
0304         u32 expect = (first_block_addr + block_off) |
0305                  CRAMFS_BLK_FLAG_DIRECT_PTR |
0306                  CRAMFS_BLK_FLAG_UNCOMPRESSED;
0307         if (blockptrs[i] != expect) {
0308             pr_debug("range: block %d/%d got %#x expects %#x\n",
0309                  pgoff+i, pgoff + *pages - 1,
0310                  blockptrs[i], expect);
0311             if (i == 0)
0312                 return 0;
0313             break;
0314         }
0315     } while (++i < *pages);
0316 
0317     *pages = i;
0318     return first_block_addr << CRAMFS_BLK_DIRECT_PTR_SHIFT;
0319 }
0320 
0321 #ifdef CONFIG_MMU
0322 
0323 /*
0324  * Return true if the last page of a file in the filesystem image contains
0325  * some other data that doesn't belong to that file. It is assumed that the
0326  * last block is CRAMFS_BLK_FLAG_DIRECT_PTR | CRAMFS_BLK_FLAG_UNCOMPRESSED
0327  * (verified by cramfs_get_block_range() and directly accessible in memory.
0328  */
0329 static bool cramfs_last_page_is_shared(struct inode *inode)
0330 {
0331     struct cramfs_sb_info *sbi = CRAMFS_SB(inode->i_sb);
0332     u32 partial, last_page, blockaddr, *blockptrs;
0333     char *tail_data;
0334 
0335     partial = offset_in_page(inode->i_size);
0336     if (!partial)
0337         return false;
0338     last_page = inode->i_size >> PAGE_SHIFT;
0339     blockptrs = (u32 *)(sbi->linear_virt_addr + OFFSET(inode));
0340     blockaddr = blockptrs[last_page] & ~CRAMFS_BLK_FLAGS;
0341     blockaddr <<= CRAMFS_BLK_DIRECT_PTR_SHIFT;
0342     tail_data = sbi->linear_virt_addr + blockaddr + partial;
0343     return memchr_inv(tail_data, 0, PAGE_SIZE - partial) ? true : false;
0344 }
0345 
0346 static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma)
0347 {
0348     struct inode *inode = file_inode(file);
0349     struct cramfs_sb_info *sbi = CRAMFS_SB(inode->i_sb);
0350     unsigned int pages, max_pages, offset;
0351     unsigned long address, pgoff = vma->vm_pgoff;
0352     char *bailout_reason;
0353     int ret;
0354 
0355     ret = generic_file_readonly_mmap(file, vma);
0356     if (ret)
0357         return ret;
0358 
0359     /*
0360      * Now try to pre-populate ptes for this vma with a direct
0361      * mapping avoiding memory allocation when possible.
0362      */
0363 
0364     /* Could COW work here? */
0365     bailout_reason = "vma is writable";
0366     if (vma->vm_flags & VM_WRITE)
0367         goto bailout;
0368 
0369     max_pages = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
0370     bailout_reason = "beyond file limit";
0371     if (pgoff >= max_pages)
0372         goto bailout;
0373     pages = min(vma_pages(vma), max_pages - pgoff);
0374 
0375     offset = cramfs_get_block_range(inode, pgoff, &pages);
0376     bailout_reason = "unsuitable block layout";
0377     if (!offset)
0378         goto bailout;
0379     address = sbi->linear_phys_addr + offset;
0380     bailout_reason = "data is not page aligned";
0381     if (!PAGE_ALIGNED(address))
0382         goto bailout;
0383 
0384     /* Don't map the last page if it contains some other data */
0385     if (pgoff + pages == max_pages && cramfs_last_page_is_shared(inode)) {
0386         pr_debug("mmap: %pD: last page is shared\n", file);
0387         pages--;
0388     }
0389 
0390     if (!pages) {
0391         bailout_reason = "no suitable block remaining";
0392         goto bailout;
0393     }
0394 
0395     if (pages == vma_pages(vma)) {
0396         /*
0397          * The entire vma is mappable. remap_pfn_range() will
0398          * make it distinguishable from a non-direct mapping
0399          * in /proc/<pid>/maps by substituting the file offset
0400          * with the actual physical address.
0401          */
0402         ret = remap_pfn_range(vma, vma->vm_start, address >> PAGE_SHIFT,
0403                       pages * PAGE_SIZE, vma->vm_page_prot);
0404     } else {
0405         /*
0406          * Let's create a mixed map if we can't map it all.
0407          * The normal paging machinery will take care of the
0408          * unpopulated ptes via cramfs_read_folio().
0409          */
0410         int i;
0411         vma->vm_flags |= VM_MIXEDMAP;
0412         for (i = 0; i < pages && !ret; i++) {
0413             vm_fault_t vmf;
0414             unsigned long off = i * PAGE_SIZE;
0415             pfn_t pfn = phys_to_pfn_t(address + off, PFN_DEV);
0416             vmf = vmf_insert_mixed(vma, vma->vm_start + off, pfn);
0417             if (vmf & VM_FAULT_ERROR)
0418                 ret = vm_fault_to_errno(vmf, 0);
0419         }
0420     }
0421 
0422     if (!ret)
0423         pr_debug("mapped %pD[%lu] at 0x%08lx (%u/%lu pages) "
0424              "to vma 0x%08lx, page_prot 0x%llx\n", file,
0425              pgoff, address, pages, vma_pages(vma), vma->vm_start,
0426              (unsigned long long)pgprot_val(vma->vm_page_prot));
0427     return ret;
0428 
0429 bailout:
0430     pr_debug("%pD[%lu]: direct mmap impossible: %s\n",
0431          file, pgoff, bailout_reason);
0432     /* Didn't manage any direct map, but normal paging is still possible */
0433     return 0;
0434 }
0435 
0436 #else /* CONFIG_MMU */
0437 
0438 static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma)
0439 {
0440     return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -ENOSYS;
0441 }
0442 
0443 static unsigned long cramfs_physmem_get_unmapped_area(struct file *file,
0444             unsigned long addr, unsigned long len,
0445             unsigned long pgoff, unsigned long flags)
0446 {
0447     struct inode *inode = file_inode(file);
0448     struct super_block *sb = inode->i_sb;
0449     struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
0450     unsigned int pages, block_pages, max_pages, offset;
0451 
0452     pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
0453     max_pages = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
0454     if (pgoff >= max_pages || pages > max_pages - pgoff)
0455         return -EINVAL;
0456     block_pages = pages;
0457     offset = cramfs_get_block_range(inode, pgoff, &block_pages);
0458     if (!offset || block_pages != pages)
0459         return -ENOSYS;
0460     addr = sbi->linear_phys_addr + offset;
0461     pr_debug("get_unmapped for %pD ofs %#lx siz %lu at 0x%08lx\n",
0462          file, pgoff*PAGE_SIZE, len, addr);
0463     return addr;
0464 }
0465 
0466 static unsigned int cramfs_physmem_mmap_capabilities(struct file *file)
0467 {
0468     return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT |
0469            NOMMU_MAP_READ | NOMMU_MAP_EXEC;
0470 }
0471 
0472 #endif /* CONFIG_MMU */
0473 
0474 static const struct file_operations cramfs_physmem_fops = {
0475     .llseek         = generic_file_llseek,
0476     .read_iter      = generic_file_read_iter,
0477     .splice_read        = generic_file_splice_read,
0478     .mmap           = cramfs_physmem_mmap,
0479 #ifndef CONFIG_MMU
0480     .get_unmapped_area  = cramfs_physmem_get_unmapped_area,
0481     .mmap_capabilities  = cramfs_physmem_mmap_capabilities,
0482 #endif
0483 };
0484 
0485 static void cramfs_kill_sb(struct super_block *sb)
0486 {
0487     struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
0488 
0489     if (IS_ENABLED(CONFIG_CRAMFS_MTD) && sb->s_mtd) {
0490         if (sbi && sbi->mtd_point_size)
0491             mtd_unpoint(sb->s_mtd, 0, sbi->mtd_point_size);
0492         kill_mtd_super(sb);
0493     } else if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV) && sb->s_bdev) {
0494         kill_block_super(sb);
0495     }
0496     kfree(sbi);
0497 }
0498 
0499 static int cramfs_reconfigure(struct fs_context *fc)
0500 {
0501     sync_filesystem(fc->root->d_sb);
0502     fc->sb_flags |= SB_RDONLY;
0503     return 0;
0504 }
0505 
0506 static int cramfs_read_super(struct super_block *sb, struct fs_context *fc,
0507                  struct cramfs_super *super)
0508 {
0509     struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
0510     unsigned long root_offset;
0511     bool silent = fc->sb_flags & SB_SILENT;
0512 
0513     /* We don't know the real size yet */
0514     sbi->size = PAGE_SIZE;
0515 
0516     /* Read the first block and get the superblock from it */
0517     mutex_lock(&read_mutex);
0518     memcpy(super, cramfs_read(sb, 0, sizeof(*super)), sizeof(*super));
0519     mutex_unlock(&read_mutex);
0520 
0521     /* Do sanity checks on the superblock */
0522     if (super->magic != CRAMFS_MAGIC) {
0523         /* check for wrong endianness */
0524         if (super->magic == CRAMFS_MAGIC_WEND) {
0525             if (!silent)
0526                 errorfc(fc, "wrong endianness");
0527             return -EINVAL;
0528         }
0529 
0530         /* check at 512 byte offset */
0531         mutex_lock(&read_mutex);
0532         memcpy(super,
0533                cramfs_read(sb, 512, sizeof(*super)),
0534                sizeof(*super));
0535         mutex_unlock(&read_mutex);
0536         if (super->magic != CRAMFS_MAGIC) {
0537             if (super->magic == CRAMFS_MAGIC_WEND && !silent)
0538                 errorfc(fc, "wrong endianness");
0539             else if (!silent)
0540                 errorfc(fc, "wrong magic");
0541             return -EINVAL;
0542         }
0543     }
0544 
0545     /* get feature flags first */
0546     if (super->flags & ~CRAMFS_SUPPORTED_FLAGS) {
0547         errorfc(fc, "unsupported filesystem features");
0548         return -EINVAL;
0549     }
0550 
0551     /* Check that the root inode is in a sane state */
0552     if (!S_ISDIR(super->root.mode)) {
0553         errorfc(fc, "root is not a directory");
0554         return -EINVAL;
0555     }
0556     /* correct strange, hard-coded permissions of mkcramfs */
0557     super->root.mode |= 0555;
0558 
0559     root_offset = super->root.offset << 2;
0560     if (super->flags & CRAMFS_FLAG_FSID_VERSION_2) {
0561         sbi->size = super->size;
0562         sbi->blocks = super->fsid.blocks;
0563         sbi->files = super->fsid.files;
0564     } else {
0565         sbi->size = 1<<28;
0566         sbi->blocks = 0;
0567         sbi->files = 0;
0568     }
0569     sbi->magic = super->magic;
0570     sbi->flags = super->flags;
0571     if (root_offset == 0)
0572         infofc(fc, "empty filesystem");
0573     else if (!(super->flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
0574          ((root_offset != sizeof(struct cramfs_super)) &&
0575           (root_offset != 512 + sizeof(struct cramfs_super))))
0576     {
0577         errorfc(fc, "bad root offset %lu", root_offset);
0578         return -EINVAL;
0579     }
0580 
0581     return 0;
0582 }
0583 
0584 static int cramfs_finalize_super(struct super_block *sb,
0585                  struct cramfs_inode *cramfs_root)
0586 {
0587     struct inode *root;
0588 
0589     /* Set it all up.. */
0590     sb->s_flags |= SB_RDONLY;
0591     sb->s_time_min = 0;
0592     sb->s_time_max = 0;
0593     sb->s_op = &cramfs_ops;
0594     root = get_cramfs_inode(sb, cramfs_root, 0);
0595     if (IS_ERR(root))
0596         return PTR_ERR(root);
0597     sb->s_root = d_make_root(root);
0598     if (!sb->s_root)
0599         return -ENOMEM;
0600     return 0;
0601 }
0602 
0603 static int cramfs_blkdev_fill_super(struct super_block *sb, struct fs_context *fc)
0604 {
0605     struct cramfs_sb_info *sbi;
0606     struct cramfs_super super;
0607     int i, err;
0608 
0609     sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
0610     if (!sbi)
0611         return -ENOMEM;
0612     sb->s_fs_info = sbi;
0613 
0614     /* Invalidate the read buffers on mount: think disk change.. */
0615     for (i = 0; i < READ_BUFFERS; i++)
0616         buffer_blocknr[i] = -1;
0617 
0618     err = cramfs_read_super(sb, fc, &super);
0619     if (err)
0620         return err;
0621     return cramfs_finalize_super(sb, &super.root);
0622 }
0623 
0624 static int cramfs_mtd_fill_super(struct super_block *sb, struct fs_context *fc)
0625 {
0626     struct cramfs_sb_info *sbi;
0627     struct cramfs_super super;
0628     int err;
0629 
0630     sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
0631     if (!sbi)
0632         return -ENOMEM;
0633     sb->s_fs_info = sbi;
0634 
0635     /* Map only one page for now.  Will remap it when fs size is known. */
0636     err = mtd_point(sb->s_mtd, 0, PAGE_SIZE, &sbi->mtd_point_size,
0637             &sbi->linear_virt_addr, &sbi->linear_phys_addr);
0638     if (err || sbi->mtd_point_size != PAGE_SIZE) {
0639         pr_err("unable to get direct memory access to mtd:%s\n",
0640                sb->s_mtd->name);
0641         return err ? : -ENODATA;
0642     }
0643 
0644     pr_info("checking physical address %pap for linear cramfs image\n",
0645         &sbi->linear_phys_addr);
0646     err = cramfs_read_super(sb, fc, &super);
0647     if (err)
0648         return err;
0649 
0650     /* Remap the whole filesystem now */
0651     pr_info("linear cramfs image on mtd:%s appears to be %lu KB in size\n",
0652         sb->s_mtd->name, sbi->size/1024);
0653     mtd_unpoint(sb->s_mtd, 0, PAGE_SIZE);
0654     err = mtd_point(sb->s_mtd, 0, sbi->size, &sbi->mtd_point_size,
0655             &sbi->linear_virt_addr, &sbi->linear_phys_addr);
0656     if (err || sbi->mtd_point_size != sbi->size) {
0657         pr_err("unable to get direct memory access to mtd:%s\n",
0658                sb->s_mtd->name);
0659         return err ? : -ENODATA;
0660     }
0661 
0662     return cramfs_finalize_super(sb, &super.root);
0663 }
0664 
0665 static int cramfs_statfs(struct dentry *dentry, struct kstatfs *buf)
0666 {
0667     struct super_block *sb = dentry->d_sb;
0668     u64 id = 0;
0669 
0670     if (sb->s_bdev)
0671         id = huge_encode_dev(sb->s_bdev->bd_dev);
0672     else if (sb->s_dev)
0673         id = huge_encode_dev(sb->s_dev);
0674 
0675     buf->f_type = CRAMFS_MAGIC;
0676     buf->f_bsize = PAGE_SIZE;
0677     buf->f_blocks = CRAMFS_SB(sb)->blocks;
0678     buf->f_bfree = 0;
0679     buf->f_bavail = 0;
0680     buf->f_files = CRAMFS_SB(sb)->files;
0681     buf->f_ffree = 0;
0682     buf->f_fsid = u64_to_fsid(id);
0683     buf->f_namelen = CRAMFS_MAXPATHLEN;
0684     return 0;
0685 }
0686 
0687 /*
0688  * Read a cramfs directory entry.
0689  */
0690 static int cramfs_readdir(struct file *file, struct dir_context *ctx)
0691 {
0692     struct inode *inode = file_inode(file);
0693     struct super_block *sb = inode->i_sb;
0694     char *buf;
0695     unsigned int offset;
0696 
0697     /* Offset within the thing. */
0698     if (ctx->pos >= inode->i_size)
0699         return 0;
0700     offset = ctx->pos;
0701     /* Directory entries are always 4-byte aligned */
0702     if (offset & 3)
0703         return -EINVAL;
0704 
0705     buf = kmalloc(CRAMFS_MAXPATHLEN, GFP_KERNEL);
0706     if (!buf)
0707         return -ENOMEM;
0708 
0709     while (offset < inode->i_size) {
0710         struct cramfs_inode *de;
0711         unsigned long nextoffset;
0712         char *name;
0713         ino_t ino;
0714         umode_t mode;
0715         int namelen;
0716 
0717         mutex_lock(&read_mutex);
0718         de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+CRAMFS_MAXPATHLEN);
0719         name = (char *)(de+1);
0720 
0721         /*
0722          * Namelengths on disk are shifted by two
0723          * and the name padded out to 4-byte boundaries
0724          * with zeroes.
0725          */
0726         namelen = de->namelen << 2;
0727         memcpy(buf, name, namelen);
0728         ino = cramino(de, OFFSET(inode) + offset);
0729         mode = de->mode;
0730         mutex_unlock(&read_mutex);
0731         nextoffset = offset + sizeof(*de) + namelen;
0732         for (;;) {
0733             if (!namelen) {
0734                 kfree(buf);
0735                 return -EIO;
0736             }
0737             if (buf[namelen-1])
0738                 break;
0739             namelen--;
0740         }
0741         if (!dir_emit(ctx, buf, namelen, ino, mode >> 12))
0742             break;
0743 
0744         ctx->pos = offset = nextoffset;
0745     }
0746     kfree(buf);
0747     return 0;
0748 }
0749 
0750 /*
0751  * Lookup and fill in the inode data..
0752  */
0753 static struct dentry *cramfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
0754 {
0755     unsigned int offset = 0;
0756     struct inode *inode = NULL;
0757     int sorted;
0758 
0759     mutex_lock(&read_mutex);
0760     sorted = CRAMFS_SB(dir->i_sb)->flags & CRAMFS_FLAG_SORTED_DIRS;
0761     while (offset < dir->i_size) {
0762         struct cramfs_inode *de;
0763         char *name;
0764         int namelen, retval;
0765         int dir_off = OFFSET(dir) + offset;
0766 
0767         de = cramfs_read(dir->i_sb, dir_off, sizeof(*de)+CRAMFS_MAXPATHLEN);
0768         name = (char *)(de+1);
0769 
0770         /* Try to take advantage of sorted directories */
0771         if (sorted && (dentry->d_name.name[0] < name[0]))
0772             break;
0773 
0774         namelen = de->namelen << 2;
0775         offset += sizeof(*de) + namelen;
0776 
0777         /* Quick check that the name is roughly the right length */
0778         if (((dentry->d_name.len + 3) & ~3) != namelen)
0779             continue;
0780 
0781         for (;;) {
0782             if (!namelen) {
0783                 inode = ERR_PTR(-EIO);
0784                 goto out;
0785             }
0786             if (name[namelen-1])
0787                 break;
0788             namelen--;
0789         }
0790         if (namelen != dentry->d_name.len)
0791             continue;
0792         retval = memcmp(dentry->d_name.name, name, namelen);
0793         if (retval > 0)
0794             continue;
0795         if (!retval) {
0796             inode = get_cramfs_inode(dir->i_sb, de, dir_off);
0797             break;
0798         }
0799         /* else (retval < 0) */
0800         if (sorted)
0801             break;
0802     }
0803 out:
0804     mutex_unlock(&read_mutex);
0805     return d_splice_alias(inode, dentry);
0806 }
0807 
0808 static int cramfs_read_folio(struct file *file, struct folio *folio)
0809 {
0810     struct page *page = &folio->page;
0811     struct inode *inode = page->mapping->host;
0812     u32 maxblock;
0813     int bytes_filled;
0814     void *pgdata;
0815 
0816     maxblock = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
0817     bytes_filled = 0;
0818     pgdata = kmap(page);
0819 
0820     if (page->index < maxblock) {
0821         struct super_block *sb = inode->i_sb;
0822         u32 blkptr_offset = OFFSET(inode) + page->index * 4;
0823         u32 block_ptr, block_start, block_len;
0824         bool uncompressed, direct;
0825 
0826         mutex_lock(&read_mutex);
0827         block_ptr = *(u32 *) cramfs_read(sb, blkptr_offset, 4);
0828         uncompressed = (block_ptr & CRAMFS_BLK_FLAG_UNCOMPRESSED);
0829         direct = (block_ptr & CRAMFS_BLK_FLAG_DIRECT_PTR);
0830         block_ptr &= ~CRAMFS_BLK_FLAGS;
0831 
0832         if (direct) {
0833             /*
0834              * The block pointer is an absolute start pointer,
0835              * shifted by 2 bits. The size is included in the
0836              * first 2 bytes of the data block when compressed,
0837              * or PAGE_SIZE otherwise.
0838              */
0839             block_start = block_ptr << CRAMFS_BLK_DIRECT_PTR_SHIFT;
0840             if (uncompressed) {
0841                 block_len = PAGE_SIZE;
0842                 /* if last block: cap to file length */
0843                 if (page->index == maxblock - 1)
0844                     block_len =
0845                         offset_in_page(inode->i_size);
0846             } else {
0847                 block_len = *(u16 *)
0848                     cramfs_read(sb, block_start, 2);
0849                 block_start += 2;
0850             }
0851         } else {
0852             /*
0853              * The block pointer indicates one past the end of
0854              * the current block (start of next block). If this
0855              * is the first block then it starts where the block
0856              * pointer table ends, otherwise its start comes
0857              * from the previous block's pointer.
0858              */
0859             block_start = OFFSET(inode) + maxblock * 4;
0860             if (page->index)
0861                 block_start = *(u32 *)
0862                     cramfs_read(sb, blkptr_offset - 4, 4);
0863             /* Beware... previous ptr might be a direct ptr */
0864             if (unlikely(block_start & CRAMFS_BLK_FLAG_DIRECT_PTR)) {
0865                 /* See comments on earlier code. */
0866                 u32 prev_start = block_start;
0867                 block_start = prev_start & ~CRAMFS_BLK_FLAGS;
0868                 block_start <<= CRAMFS_BLK_DIRECT_PTR_SHIFT;
0869                 if (prev_start & CRAMFS_BLK_FLAG_UNCOMPRESSED) {
0870                     block_start += PAGE_SIZE;
0871                 } else {
0872                     block_len = *(u16 *)
0873                         cramfs_read(sb, block_start, 2);
0874                     block_start += 2 + block_len;
0875                 }
0876             }
0877             block_start &= ~CRAMFS_BLK_FLAGS;
0878             block_len = block_ptr - block_start;
0879         }
0880 
0881         if (block_len == 0)
0882             ; /* hole */
0883         else if (unlikely(block_len > 2*PAGE_SIZE ||
0884                   (uncompressed && block_len > PAGE_SIZE))) {
0885             mutex_unlock(&read_mutex);
0886             pr_err("bad data blocksize %u\n", block_len);
0887             goto err;
0888         } else if (uncompressed) {
0889             memcpy(pgdata,
0890                    cramfs_read(sb, block_start, block_len),
0891                    block_len);
0892             bytes_filled = block_len;
0893         } else {
0894             bytes_filled = cramfs_uncompress_block(pgdata,
0895                  PAGE_SIZE,
0896                  cramfs_read(sb, block_start, block_len),
0897                  block_len);
0898         }
0899         mutex_unlock(&read_mutex);
0900         if (unlikely(bytes_filled < 0))
0901             goto err;
0902     }
0903 
0904     memset(pgdata + bytes_filled, 0, PAGE_SIZE - bytes_filled);
0905     flush_dcache_page(page);
0906     kunmap(page);
0907     SetPageUptodate(page);
0908     unlock_page(page);
0909     return 0;
0910 
0911 err:
0912     kunmap(page);
0913     ClearPageUptodate(page);
0914     SetPageError(page);
0915     unlock_page(page);
0916     return 0;
0917 }
0918 
0919 static const struct address_space_operations cramfs_aops = {
0920     .read_folio = cramfs_read_folio
0921 };
0922 
0923 /*
0924  * Our operations:
0925  */
0926 
0927 /*
0928  * A directory can only readdir
0929  */
0930 static const struct file_operations cramfs_directory_operations = {
0931     .llseek     = generic_file_llseek,
0932     .read       = generic_read_dir,
0933     .iterate_shared = cramfs_readdir,
0934 };
0935 
0936 static const struct inode_operations cramfs_dir_inode_operations = {
0937     .lookup     = cramfs_lookup,
0938 };
0939 
0940 static const struct super_operations cramfs_ops = {
0941     .statfs     = cramfs_statfs,
0942 };
0943 
0944 static int cramfs_get_tree(struct fs_context *fc)
0945 {
0946     int ret = -ENOPROTOOPT;
0947 
0948     if (IS_ENABLED(CONFIG_CRAMFS_MTD)) {
0949         ret = get_tree_mtd(fc, cramfs_mtd_fill_super);
0950         if (!ret)
0951             return 0;
0952     }
0953     if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV))
0954         ret = get_tree_bdev(fc, cramfs_blkdev_fill_super);
0955     return ret;
0956 }
0957 
0958 static const struct fs_context_operations cramfs_context_ops = {
0959     .get_tree   = cramfs_get_tree,
0960     .reconfigure    = cramfs_reconfigure,
0961 };
0962 
0963 /*
0964  * Set up the filesystem mount context.
0965  */
0966 static int cramfs_init_fs_context(struct fs_context *fc)
0967 {
0968     fc->ops = &cramfs_context_ops;
0969     return 0;
0970 }
0971 
0972 static struct file_system_type cramfs_fs_type = {
0973     .owner      = THIS_MODULE,
0974     .name       = "cramfs",
0975     .init_fs_context = cramfs_init_fs_context,
0976     .kill_sb    = cramfs_kill_sb,
0977     .fs_flags   = FS_REQUIRES_DEV,
0978 };
0979 MODULE_ALIAS_FS("cramfs");
0980 
0981 static int __init init_cramfs_fs(void)
0982 {
0983     int rv;
0984 
0985     rv = cramfs_uncompress_init();
0986     if (rv < 0)
0987         return rv;
0988     rv = register_filesystem(&cramfs_fs_type);
0989     if (rv < 0)
0990         cramfs_uncompress_exit();
0991     return rv;
0992 }
0993 
0994 static void __exit exit_cramfs_fs(void)
0995 {
0996     cramfs_uncompress_exit();
0997     unregister_filesystem(&cramfs_fs_type);
0998 }
0999 
1000 module_init(init_cramfs_fs)
1001 module_exit(exit_cramfs_fs)
1002 MODULE_LICENSE("GPL");