2 * Compressed rom filesystem for Linux.
4 * Copyright (C) 1999 Linus Torvalds.
6 * This file is released under the GPL.
10 * These are the VFS interfaces to the compressed rom filesystem.
11 * The actual compression is based on zlib, see the other files.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/module.h>
18 #include <linux/file.h>
19 #include <linux/pagemap.h>
20 #include <linux/pfn_t.h>
21 #include <linux/ramfs.h>
22 #include <linux/init.h>
23 #include <linux/string.h>
24 #include <linux/blkdev.h>
25 #include <linux/mtd/mtd.h>
26 #include <linux/mtd/super.h>
27 #include <linux/fs_context.h>
28 #include <linux/slab.h>
29 #include <linux/vfs.h>
30 #include <linux/mutex.h>
31 #include <uapi/linux/cramfs_fs.h>
32 #include <linux/uaccess.h>
37 * cramfs super-block data in memory
39 struct cramfs_sb_info {
45 void *linear_virt_addr;
46 resource_size_t linear_phys_addr;
47 size_t mtd_point_size;
50 static inline struct cramfs_sb_info *CRAMFS_SB(struct super_block *sb)
55 static const struct super_operations cramfs_ops;
56 static const struct inode_operations cramfs_dir_inode_operations;
57 static const struct file_operations cramfs_directory_operations;
58 static const struct file_operations cramfs_physmem_fops;
59 static const struct address_space_operations cramfs_aops;
61 static DEFINE_MUTEX(read_mutex);
64 /* These macros may change in future, to provide better st_ino semantics. */
65 #define OFFSET(x) ((x)->i_ino)
67 static unsigned long cramino(const struct cramfs_inode *cino, unsigned int offset)
75 * The file mode test fixes buggy mkcramfs implementations where
76 * cramfs_inode->offset is set to a non zero value for entries
77 * which did not contain data, like devices node and fifos.
79 switch (cino->mode & S_IFMT) {
83 return cino->offset << 2;
90 static struct inode *get_cramfs_inode(struct super_block *sb,
91 const struct cramfs_inode *cramfs_inode, unsigned int offset)
94 static struct timespec64 zerotime;
96 inode = iget_locked(sb, cramino(cramfs_inode, offset));
98 return ERR_PTR(-ENOMEM);
99 if (!(inode->i_state & I_NEW))
102 switch (cramfs_inode->mode & S_IFMT) {
104 inode->i_fop = &generic_ro_fops;
105 inode->i_data.a_ops = &cramfs_aops;
106 if (IS_ENABLED(CONFIG_CRAMFS_MTD) &&
107 CRAMFS_SB(sb)->flags & CRAMFS_FLAG_EXT_BLOCK_POINTERS &&
108 CRAMFS_SB(sb)->linear_phys_addr)
109 inode->i_fop = &cramfs_physmem_fops;
112 inode->i_op = &cramfs_dir_inode_operations;
113 inode->i_fop = &cramfs_directory_operations;
116 inode->i_op = &page_symlink_inode_operations;
117 inode_nohighmem(inode);
118 inode->i_data.a_ops = &cramfs_aops;
121 init_special_inode(inode, cramfs_inode->mode,
122 old_decode_dev(cramfs_inode->size));
125 inode->i_mode = cramfs_inode->mode;
126 i_uid_write(inode, cramfs_inode->uid);
127 i_gid_write(inode, cramfs_inode->gid);
129 /* if the lower 2 bits are zero, the inode contains data */
130 if (!(inode->i_ino & 3)) {
131 inode->i_size = cramfs_inode->size;
132 inode->i_blocks = (cramfs_inode->size - 1) / 512 + 1;
135 /* Struct copy intentional */
136 inode->i_mtime = inode->i_atime = inode->i_ctime = zerotime;
137 /* inode->i_nlink is left 1 - arguably wrong for directories,
138 but it's the best we can do without reading the directory
139 contents. 1 yields the right result in GNU find, even
140 without -noleaf option. */
142 unlock_new_inode(inode);
148 * We have our own block cache: don't fill up the buffer cache
149 * with the rom-image, because the way the filesystem is set
150 * up the accesses should be fairly regular and cached in the
151 * page cache and dentry tree anyway..
153 * This also acts as a way to guarantee contiguous areas of up to
154 * BLKS_PER_BUF*PAGE_SIZE, so that the caller doesn't need to
155 * worry about end-of-buffer issues even when decompressing a full
158 * Note: This is all optimized away at compile time when
159 * CONFIG_CRAMFS_BLOCKDEV=n.
161 #define READ_BUFFERS (2)
162 /* NEXT_BUFFER(): Loop over [0..(READ_BUFFERS-1)]. */
163 #define NEXT_BUFFER(_ix) ((_ix) ^ 1)
166 * BLKS_PER_BUF_SHIFT should be at least 2 to allow for "compressed"
167 * data that takes up more space than the original and with unlucky
170 #define BLKS_PER_BUF_SHIFT (2)
171 #define BLKS_PER_BUF (1 << BLKS_PER_BUF_SHIFT)
172 #define BUFFER_SIZE (BLKS_PER_BUF*PAGE_SIZE)
174 static unsigned char read_buffers[READ_BUFFERS][BUFFER_SIZE];
175 static unsigned buffer_blocknr[READ_BUFFERS];
176 static struct super_block *buffer_dev[READ_BUFFERS];
177 static int next_buffer;
180 * Populate our block cache and return a pointer to it.
182 static void *cramfs_blkdev_read(struct super_block *sb, unsigned int offset,
185 struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
186 struct file_ra_state ra;
187 struct page *pages[BLKS_PER_BUF];
188 unsigned i, blocknr, buffer;
189 unsigned long devsize;
194 blocknr = offset >> PAGE_SHIFT;
195 offset &= PAGE_SIZE - 1;
197 /* Check if an existing buffer already has the data.. */
198 for (i = 0; i < READ_BUFFERS; i++) {
199 unsigned int blk_offset;
201 if (buffer_dev[i] != sb)
203 if (blocknr < buffer_blocknr[i])
205 blk_offset = (blocknr - buffer_blocknr[i]) << PAGE_SHIFT;
206 blk_offset += offset;
207 if (blk_offset > BUFFER_SIZE ||
208 blk_offset + len > BUFFER_SIZE)
210 return read_buffers[i] + blk_offset;
213 devsize = bdev_nr_bytes(sb->s_bdev) >> PAGE_SHIFT;
215 /* Ok, read in BLKS_PER_BUF pages completely first. */
216 file_ra_state_init(&ra, mapping);
217 page_cache_sync_readahead(mapping, &ra, NULL, blocknr, BLKS_PER_BUF);
219 for (i = 0; i < BLKS_PER_BUF; i++) {
220 struct page *page = NULL;
222 if (blocknr + i < devsize) {
223 page = read_mapping_page(mapping, blocknr + i, NULL);
224 /* synchronous error? */
231 buffer = next_buffer;
232 next_buffer = NEXT_BUFFER(buffer);
233 buffer_blocknr[buffer] = blocknr;
234 buffer_dev[buffer] = sb;
236 data = read_buffers[buffer];
237 for (i = 0; i < BLKS_PER_BUF; i++) {
238 struct page *page = pages[i];
241 memcpy(data, kmap(page), PAGE_SIZE);
245 memset(data, 0, PAGE_SIZE);
248 return read_buffers[buffer] + offset;
252 * Return a pointer to the linearly addressed cramfs image in memory.
254 static void *cramfs_direct_read(struct super_block *sb, unsigned int offset,
257 struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
261 if (len > sbi->size || offset > sbi->size - len)
262 return page_address(ZERO_PAGE(0));
263 return sbi->linear_virt_addr + offset;
267 * Returns a pointer to a buffer containing at least LEN bytes of
268 * filesystem starting at byte offset OFFSET into the filesystem.
270 static void *cramfs_read(struct super_block *sb, unsigned int offset,
273 struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
275 if (IS_ENABLED(CONFIG_CRAMFS_MTD) && sbi->linear_virt_addr)
276 return cramfs_direct_read(sb, offset, len);
277 else if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV))
278 return cramfs_blkdev_read(sb, offset, len);
284 * For a mapping to be possible, we need a range of uncompressed and
285 * contiguous blocks. Return the offset for the first block and number of
286 * valid blocks for which that is true, or zero otherwise.
288 static u32 cramfs_get_block_range(struct inode *inode, u32 pgoff, u32 *pages)
290 struct cramfs_sb_info *sbi = CRAMFS_SB(inode->i_sb);
292 u32 *blockptrs, first_block_addr;
295 * We can dereference memory directly here as this code may be
296 * reached only when there is a direct filesystem image mapping
297 * available in memory.
299 blockptrs = (u32 *)(sbi->linear_virt_addr + OFFSET(inode) + pgoff * 4);
300 first_block_addr = blockptrs[0] & ~CRAMFS_BLK_FLAGS;
303 u32 block_off = i * (PAGE_SIZE >> CRAMFS_BLK_DIRECT_PTR_SHIFT);
304 u32 expect = (first_block_addr + block_off) |
305 CRAMFS_BLK_FLAG_DIRECT_PTR |
306 CRAMFS_BLK_FLAG_UNCOMPRESSED;
307 if (blockptrs[i] != expect) {
308 pr_debug("range: block %d/%d got %#x expects %#x\n",
309 pgoff+i, pgoff + *pages - 1,
310 blockptrs[i], expect);
315 } while (++i < *pages);
318 return first_block_addr << CRAMFS_BLK_DIRECT_PTR_SHIFT;
324 * Return true if the last page of a file in the filesystem image contains
325 * some other data that doesn't belong to that file. It is assumed that the
326 * last block is CRAMFS_BLK_FLAG_DIRECT_PTR | CRAMFS_BLK_FLAG_UNCOMPRESSED
327 * (verified by cramfs_get_block_range() and directly accessible in memory.
329 static bool cramfs_last_page_is_shared(struct inode *inode)
331 struct cramfs_sb_info *sbi = CRAMFS_SB(inode->i_sb);
332 u32 partial, last_page, blockaddr, *blockptrs;
335 partial = offset_in_page(inode->i_size);
338 last_page = inode->i_size >> PAGE_SHIFT;
339 blockptrs = (u32 *)(sbi->linear_virt_addr + OFFSET(inode));
340 blockaddr = blockptrs[last_page] & ~CRAMFS_BLK_FLAGS;
341 blockaddr <<= CRAMFS_BLK_DIRECT_PTR_SHIFT;
342 tail_data = sbi->linear_virt_addr + blockaddr + partial;
343 return memchr_inv(tail_data, 0, PAGE_SIZE - partial) ? true : false;
346 static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma)
348 struct inode *inode = file_inode(file);
349 struct cramfs_sb_info *sbi = CRAMFS_SB(inode->i_sb);
350 unsigned int pages, max_pages, offset;
351 unsigned long address, pgoff = vma->vm_pgoff;
352 char *bailout_reason;
355 ret = generic_file_readonly_mmap(file, vma);
360 * Now try to pre-populate ptes for this vma with a direct
361 * mapping avoiding memory allocation when possible.
364 /* Could COW work here? */
365 bailout_reason = "vma is writable";
366 if (vma->vm_flags & VM_WRITE)
369 max_pages = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
370 bailout_reason = "beyond file limit";
371 if (pgoff >= max_pages)
373 pages = min(vma_pages(vma), max_pages - pgoff);
375 offset = cramfs_get_block_range(inode, pgoff, &pages);
376 bailout_reason = "unsuitable block layout";
379 address = sbi->linear_phys_addr + offset;
380 bailout_reason = "data is not page aligned";
381 if (!PAGE_ALIGNED(address))
384 /* Don't map the last page if it contains some other data */
385 if (pgoff + pages == max_pages && cramfs_last_page_is_shared(inode)) {
386 pr_debug("mmap: %pD: last page is shared\n", file);
391 bailout_reason = "no suitable block remaining";
395 if (pages == vma_pages(vma)) {
397 * The entire vma is mappable. remap_pfn_range() will
398 * make it distinguishable from a non-direct mapping
399 * in /proc/<pid>/maps by substituting the file offset
400 * with the actual physical address.
402 ret = remap_pfn_range(vma, vma->vm_start, address >> PAGE_SHIFT,
403 pages * PAGE_SIZE, vma->vm_page_prot);
406 * Let's create a mixed map if we can't map it all.
407 * The normal paging machinery will take care of the
408 * unpopulated ptes via cramfs_read_folio().
411 vma->vm_flags |= VM_MIXEDMAP;
412 for (i = 0; i < pages && !ret; i++) {
414 unsigned long off = i * PAGE_SIZE;
415 pfn_t pfn = phys_to_pfn_t(address + off, PFN_DEV);
416 vmf = vmf_insert_mixed(vma, vma->vm_start + off, pfn);
417 if (vmf & VM_FAULT_ERROR)
418 ret = vm_fault_to_errno(vmf, 0);
423 pr_debug("mapped %pD[%lu] at 0x%08lx (%u/%lu pages) "
424 "to vma 0x%08lx, page_prot 0x%llx\n", file,
425 pgoff, address, pages, vma_pages(vma), vma->vm_start,
426 (unsigned long long)pgprot_val(vma->vm_page_prot));
430 pr_debug("%pD[%lu]: direct mmap impossible: %s\n",
431 file, pgoff, bailout_reason);
432 /* Didn't manage any direct map, but normal paging is still possible */
436 #else /* CONFIG_MMU */
438 static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma)
440 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -ENOSYS;
443 static unsigned long cramfs_physmem_get_unmapped_area(struct file *file,
444 unsigned long addr, unsigned long len,
445 unsigned long pgoff, unsigned long flags)
447 struct inode *inode = file_inode(file);
448 struct super_block *sb = inode->i_sb;
449 struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
450 unsigned int pages, block_pages, max_pages, offset;
452 pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
453 max_pages = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
454 if (pgoff >= max_pages || pages > max_pages - pgoff)
457 offset = cramfs_get_block_range(inode, pgoff, &block_pages);
458 if (!offset || block_pages != pages)
460 addr = sbi->linear_phys_addr + offset;
461 pr_debug("get_unmapped for %pD ofs %#lx siz %lu at 0x%08lx\n",
462 file, pgoff*PAGE_SIZE, len, addr);
466 static unsigned int cramfs_physmem_mmap_capabilities(struct file *file)
468 return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT |
469 NOMMU_MAP_READ | NOMMU_MAP_EXEC;
472 #endif /* CONFIG_MMU */
474 static const struct file_operations cramfs_physmem_fops = {
475 .llseek = generic_file_llseek,
476 .read_iter = generic_file_read_iter,
477 .splice_read = generic_file_splice_read,
478 .mmap = cramfs_physmem_mmap,
480 .get_unmapped_area = cramfs_physmem_get_unmapped_area,
481 .mmap_capabilities = cramfs_physmem_mmap_capabilities,
485 static void cramfs_kill_sb(struct super_block *sb)
487 struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
489 if (IS_ENABLED(CONFIG_CRAMFS_MTD) && sb->s_mtd) {
490 if (sbi && sbi->mtd_point_size)
491 mtd_unpoint(sb->s_mtd, 0, sbi->mtd_point_size);
493 } else if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV) && sb->s_bdev) {
494 kill_block_super(sb);
499 static int cramfs_reconfigure(struct fs_context *fc)
501 sync_filesystem(fc->root->d_sb);
502 fc->sb_flags |= SB_RDONLY;
506 static int cramfs_read_super(struct super_block *sb, struct fs_context *fc,
507 struct cramfs_super *super)
509 struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
510 unsigned long root_offset;
511 bool silent = fc->sb_flags & SB_SILENT;
513 /* We don't know the real size yet */
514 sbi->size = PAGE_SIZE;
516 /* Read the first block and get the superblock from it */
517 mutex_lock(&read_mutex);
518 memcpy(super, cramfs_read(sb, 0, sizeof(*super)), sizeof(*super));
519 mutex_unlock(&read_mutex);
521 /* Do sanity checks on the superblock */
522 if (super->magic != CRAMFS_MAGIC) {
523 /* check for wrong endianness */
524 if (super->magic == CRAMFS_MAGIC_WEND) {
526 errorfc(fc, "wrong endianness");
530 /* check at 512 byte offset */
531 mutex_lock(&read_mutex);
533 cramfs_read(sb, 512, sizeof(*super)),
535 mutex_unlock(&read_mutex);
536 if (super->magic != CRAMFS_MAGIC) {
537 if (super->magic == CRAMFS_MAGIC_WEND && !silent)
538 errorfc(fc, "wrong endianness");
540 errorfc(fc, "wrong magic");
545 /* get feature flags first */
546 if (super->flags & ~CRAMFS_SUPPORTED_FLAGS) {
547 errorfc(fc, "unsupported filesystem features");
551 /* Check that the root inode is in a sane state */
552 if (!S_ISDIR(super->root.mode)) {
553 errorfc(fc, "root is not a directory");
556 /* correct strange, hard-coded permissions of mkcramfs */
557 super->root.mode |= 0555;
559 root_offset = super->root.offset << 2;
560 if (super->flags & CRAMFS_FLAG_FSID_VERSION_2) {
561 sbi->size = super->size;
562 sbi->blocks = super->fsid.blocks;
563 sbi->files = super->fsid.files;
569 sbi->magic = super->magic;
570 sbi->flags = super->flags;
571 if (root_offset == 0)
572 infofc(fc, "empty filesystem");
573 else if (!(super->flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
574 ((root_offset != sizeof(struct cramfs_super)) &&
575 (root_offset != 512 + sizeof(struct cramfs_super))))
577 errorfc(fc, "bad root offset %lu", root_offset);
584 static int cramfs_finalize_super(struct super_block *sb,
585 struct cramfs_inode *cramfs_root)
589 /* Set it all up.. */
590 sb->s_flags |= SB_RDONLY;
593 sb->s_op = &cramfs_ops;
594 root = get_cramfs_inode(sb, cramfs_root, 0);
596 return PTR_ERR(root);
597 sb->s_root = d_make_root(root);
603 static int cramfs_blkdev_fill_super(struct super_block *sb, struct fs_context *fc)
605 struct cramfs_sb_info *sbi;
606 struct cramfs_super super;
609 sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
614 /* Invalidate the read buffers on mount: think disk change.. */
615 for (i = 0; i < READ_BUFFERS; i++)
616 buffer_blocknr[i] = -1;
618 err = cramfs_read_super(sb, fc, &super);
621 return cramfs_finalize_super(sb, &super.root);
624 static int cramfs_mtd_fill_super(struct super_block *sb, struct fs_context *fc)
626 struct cramfs_sb_info *sbi;
627 struct cramfs_super super;
630 sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
635 /* Map only one page for now. Will remap it when fs size is known. */
636 err = mtd_point(sb->s_mtd, 0, PAGE_SIZE, &sbi->mtd_point_size,
637 &sbi->linear_virt_addr, &sbi->linear_phys_addr);
638 if (err || sbi->mtd_point_size != PAGE_SIZE) {
639 pr_err("unable to get direct memory access to mtd:%s\n",
641 return err ? : -ENODATA;
644 pr_info("checking physical address %pap for linear cramfs image\n",
645 &sbi->linear_phys_addr);
646 err = cramfs_read_super(sb, fc, &super);
650 /* Remap the whole filesystem now */
651 pr_info("linear cramfs image on mtd:%s appears to be %lu KB in size\n",
652 sb->s_mtd->name, sbi->size/1024);
653 mtd_unpoint(sb->s_mtd, 0, PAGE_SIZE);
654 err = mtd_point(sb->s_mtd, 0, sbi->size, &sbi->mtd_point_size,
655 &sbi->linear_virt_addr, &sbi->linear_phys_addr);
656 if (err || sbi->mtd_point_size != sbi->size) {
657 pr_err("unable to get direct memory access to mtd:%s\n",
659 return err ? : -ENODATA;
662 return cramfs_finalize_super(sb, &super.root);
665 static int cramfs_statfs(struct dentry *dentry, struct kstatfs *buf)
667 struct super_block *sb = dentry->d_sb;
671 id = huge_encode_dev(sb->s_bdev->bd_dev);
673 id = huge_encode_dev(sb->s_dev);
675 buf->f_type = CRAMFS_MAGIC;
676 buf->f_bsize = PAGE_SIZE;
677 buf->f_blocks = CRAMFS_SB(sb)->blocks;
680 buf->f_files = CRAMFS_SB(sb)->files;
682 buf->f_fsid = u64_to_fsid(id);
683 buf->f_namelen = CRAMFS_MAXPATHLEN;
688 * Read a cramfs directory entry.
690 static int cramfs_readdir(struct file *file, struct dir_context *ctx)
692 struct inode *inode = file_inode(file);
693 struct super_block *sb = inode->i_sb;
697 /* Offset within the thing. */
698 if (ctx->pos >= inode->i_size)
701 /* Directory entries are always 4-byte aligned */
705 buf = kmalloc(CRAMFS_MAXPATHLEN, GFP_KERNEL);
709 while (offset < inode->i_size) {
710 struct cramfs_inode *de;
711 unsigned long nextoffset;
717 mutex_lock(&read_mutex);
718 de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+CRAMFS_MAXPATHLEN);
719 name = (char *)(de+1);
722 * Namelengths on disk are shifted by two
723 * and the name padded out to 4-byte boundaries
726 namelen = de->namelen << 2;
727 memcpy(buf, name, namelen);
728 ino = cramino(de, OFFSET(inode) + offset);
730 mutex_unlock(&read_mutex);
731 nextoffset = offset + sizeof(*de) + namelen;
741 if (!dir_emit(ctx, buf, namelen, ino, mode >> 12))
744 ctx->pos = offset = nextoffset;
751 * Lookup and fill in the inode data..
753 static struct dentry *cramfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
755 unsigned int offset = 0;
756 struct inode *inode = NULL;
759 mutex_lock(&read_mutex);
760 sorted = CRAMFS_SB(dir->i_sb)->flags & CRAMFS_FLAG_SORTED_DIRS;
761 while (offset < dir->i_size) {
762 struct cramfs_inode *de;
765 int dir_off = OFFSET(dir) + offset;
767 de = cramfs_read(dir->i_sb, dir_off, sizeof(*de)+CRAMFS_MAXPATHLEN);
768 name = (char *)(de+1);
770 /* Try to take advantage of sorted directories */
771 if (sorted && (dentry->d_name.name[0] < name[0]))
774 namelen = de->namelen << 2;
775 offset += sizeof(*de) + namelen;
777 /* Quick check that the name is roughly the right length */
778 if (((dentry->d_name.len + 3) & ~3) != namelen)
783 inode = ERR_PTR(-EIO);
790 if (namelen != dentry->d_name.len)
792 retval = memcmp(dentry->d_name.name, name, namelen);
796 inode = get_cramfs_inode(dir->i_sb, de, dir_off);
799 /* else (retval < 0) */
804 mutex_unlock(&read_mutex);
805 return d_splice_alias(inode, dentry);
808 static int cramfs_read_folio(struct file *file, struct folio *folio)
810 struct page *page = &folio->page;
811 struct inode *inode = page->mapping->host;
816 maxblock = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
820 if (page->index < maxblock) {
821 struct super_block *sb = inode->i_sb;
822 u32 blkptr_offset = OFFSET(inode) + page->index * 4;
823 u32 block_ptr, block_start, block_len;
824 bool uncompressed, direct;
826 mutex_lock(&read_mutex);
827 block_ptr = *(u32 *) cramfs_read(sb, blkptr_offset, 4);
828 uncompressed = (block_ptr & CRAMFS_BLK_FLAG_UNCOMPRESSED);
829 direct = (block_ptr & CRAMFS_BLK_FLAG_DIRECT_PTR);
830 block_ptr &= ~CRAMFS_BLK_FLAGS;
834 * The block pointer is an absolute start pointer,
835 * shifted by 2 bits. The size is included in the
836 * first 2 bytes of the data block when compressed,
837 * or PAGE_SIZE otherwise.
839 block_start = block_ptr << CRAMFS_BLK_DIRECT_PTR_SHIFT;
841 block_len = PAGE_SIZE;
842 /* if last block: cap to file length */
843 if (page->index == maxblock - 1)
845 offset_in_page(inode->i_size);
848 cramfs_read(sb, block_start, 2);
853 * The block pointer indicates one past the end of
854 * the current block (start of next block). If this
855 * is the first block then it starts where the block
856 * pointer table ends, otherwise its start comes
857 * from the previous block's pointer.
859 block_start = OFFSET(inode) + maxblock * 4;
861 block_start = *(u32 *)
862 cramfs_read(sb, blkptr_offset - 4, 4);
863 /* Beware... previous ptr might be a direct ptr */
864 if (unlikely(block_start & CRAMFS_BLK_FLAG_DIRECT_PTR)) {
865 /* See comments on earlier code. */
866 u32 prev_start = block_start;
867 block_start = prev_start & ~CRAMFS_BLK_FLAGS;
868 block_start <<= CRAMFS_BLK_DIRECT_PTR_SHIFT;
869 if (prev_start & CRAMFS_BLK_FLAG_UNCOMPRESSED) {
870 block_start += PAGE_SIZE;
873 cramfs_read(sb, block_start, 2);
874 block_start += 2 + block_len;
877 block_start &= ~CRAMFS_BLK_FLAGS;
878 block_len = block_ptr - block_start;
883 else if (unlikely(block_len > 2*PAGE_SIZE ||
884 (uncompressed && block_len > PAGE_SIZE))) {
885 mutex_unlock(&read_mutex);
886 pr_err("bad data blocksize %u\n", block_len);
888 } else if (uncompressed) {
890 cramfs_read(sb, block_start, block_len),
892 bytes_filled = block_len;
894 bytes_filled = cramfs_uncompress_block(pgdata,
896 cramfs_read(sb, block_start, block_len),
899 mutex_unlock(&read_mutex);
900 if (unlikely(bytes_filled < 0))
904 memset(pgdata + bytes_filled, 0, PAGE_SIZE - bytes_filled);
905 flush_dcache_page(page);
907 SetPageUptodate(page);
913 ClearPageUptodate(page);
919 static const struct address_space_operations cramfs_aops = {
920 .read_folio = cramfs_read_folio
928 * A directory can only readdir
930 static const struct file_operations cramfs_directory_operations = {
931 .llseek = generic_file_llseek,
932 .read = generic_read_dir,
933 .iterate_shared = cramfs_readdir,
936 static const struct inode_operations cramfs_dir_inode_operations = {
937 .lookup = cramfs_lookup,
940 static const struct super_operations cramfs_ops = {
941 .statfs = cramfs_statfs,
944 static int cramfs_get_tree(struct fs_context *fc)
946 int ret = -ENOPROTOOPT;
948 if (IS_ENABLED(CONFIG_CRAMFS_MTD)) {
949 ret = get_tree_mtd(fc, cramfs_mtd_fill_super);
953 if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV))
954 ret = get_tree_bdev(fc, cramfs_blkdev_fill_super);
958 static const struct fs_context_operations cramfs_context_ops = {
959 .get_tree = cramfs_get_tree,
960 .reconfigure = cramfs_reconfigure,
964 * Set up the filesystem mount context.
966 static int cramfs_init_fs_context(struct fs_context *fc)
968 fc->ops = &cramfs_context_ops;
972 static struct file_system_type cramfs_fs_type = {
973 .owner = THIS_MODULE,
975 .init_fs_context = cramfs_init_fs_context,
976 .kill_sb = cramfs_kill_sb,
977 .fs_flags = FS_REQUIRES_DEV,
979 MODULE_ALIAS_FS("cramfs");
981 static int __init init_cramfs_fs(void)
985 rv = cramfs_uncompress_init();
988 rv = register_filesystem(&cramfs_fs_type);
990 cramfs_uncompress_exit();
994 static void __exit exit_cramfs_fs(void)
996 cramfs_uncompress_exit();
997 unregister_filesystem(&cramfs_fs_type);
1000 module_init(init_cramfs_fs)
1001 module_exit(exit_cramfs_fs)
1002 MODULE_LICENSE("GPL");