Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Compressed rom filesystem for Linux. |
| 3 | * |
| 4 | * Copyright (C) 1999 Linus Torvalds. |
| 5 | * |
| 6 | * This file is released under the GPL. |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * These are the VFS interfaces to the compressed rom filesystem. |
| 11 | * The actual compression is based on zlib, see the other files. |
| 12 | */ |
| 13 | |
Fabian Frederick | 4f21e1e | 2014-08-08 14:22:50 -0700 | [diff] [blame] | 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/fs.h> |
| 18 | #include <linux/pagemap.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/blkdev.h> |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 22 | #include <linux/mtd/mtd.h> |
| 23 | #include <linux/mtd/super.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/vfs.h> |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 26 | #include <linux/mutex.h> |
Al Viro | f7f4f4d | 2013-12-10 16:54:28 -0500 | [diff] [blame] | 27 | #include <uapi/linux/cramfs_fs.h> |
Fabian Frederick | 1508f3eb | 2014-08-08 14:22:55 -0700 | [diff] [blame] | 28 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Al Viro | f7f4f4d | 2013-12-10 16:54:28 -0500 | [diff] [blame] | 30 | #include "internal.h" |
| 31 | |
| 32 | /* |
| 33 | * cramfs super-block data in memory |
| 34 | */ |
| 35 | struct cramfs_sb_info { |
| 36 | unsigned long magic; |
| 37 | unsigned long size; |
| 38 | unsigned long blocks; |
| 39 | unsigned long files; |
| 40 | unsigned long flags; |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 41 | void *linear_virt_addr; |
| 42 | resource_size_t linear_phys_addr; |
| 43 | size_t mtd_point_size; |
Al Viro | f7f4f4d | 2013-12-10 16:54:28 -0500 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static inline struct cramfs_sb_info *CRAMFS_SB(struct super_block *sb) |
| 47 | { |
| 48 | return sb->s_fs_info; |
| 49 | } |
| 50 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 51 | static const struct super_operations cramfs_ops; |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 52 | static const struct inode_operations cramfs_dir_inode_operations; |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 53 | static const struct file_operations cramfs_directory_operations; |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 54 | static const struct address_space_operations cramfs_aops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 56 | static DEFINE_MUTEX(read_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | |
Stefani Seibold | 6f772fe | 2011-01-12 17:01:10 -0800 | [diff] [blame] | 59 | /* These macros may change in future, to provide better st_ino semantics. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #define OFFSET(x) ((x)->i_ino) |
| 61 | |
Al Viro | 0577d1b | 2011-07-17 19:04:14 -0400 | [diff] [blame] | 62 | static unsigned long cramino(const struct cramfs_inode *cino, unsigned int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
Stefani Seibold | 6f772fe | 2011-01-12 17:01:10 -0800 | [diff] [blame] | 64 | if (!cino->offset) |
| 65 | return offset + 1; |
| 66 | if (!cino->size) |
| 67 | return offset + 1; |
| 68 | |
| 69 | /* |
| 70 | * The file mode test fixes buggy mkcramfs implementations where |
| 71 | * cramfs_inode->offset is set to a non zero value for entries |
| 72 | * which did not contain data, like devices node and fifos. |
| 73 | */ |
| 74 | switch (cino->mode & S_IFMT) { |
| 75 | case S_IFREG: |
| 76 | case S_IFDIR: |
| 77 | case S_IFLNK: |
| 78 | return cino->offset << 2; |
| 79 | default: |
| 80 | break; |
| 81 | } |
| 82 | return offset + 1; |
| 83 | } |
| 84 | |
| 85 | static struct inode *get_cramfs_inode(struct super_block *sb, |
Al Viro | 0577d1b | 2011-07-17 19:04:14 -0400 | [diff] [blame] | 86 | const struct cramfs_inode *cramfs_inode, unsigned int offset) |
Stefani Seibold | 6f772fe | 2011-01-12 17:01:10 -0800 | [diff] [blame] | 87 | { |
| 88 | struct inode *inode; |
Al Viro | 77b8a75 | 2010-06-04 21:19:01 -0400 | [diff] [blame] | 89 | static struct timespec zerotime; |
Stefani Seibold | 6f772fe | 2011-01-12 17:01:10 -0800 | [diff] [blame] | 90 | |
| 91 | inode = iget_locked(sb, cramino(cramfs_inode, offset)); |
| 92 | if (!inode) |
| 93 | return ERR_PTR(-ENOMEM); |
| 94 | if (!(inode->i_state & I_NEW)) |
| 95 | return inode; |
| 96 | |
| 97 | switch (cramfs_inode->mode & S_IFMT) { |
| 98 | case S_IFREG: |
| 99 | inode->i_fop = &generic_ro_fops; |
| 100 | inode->i_data.a_ops = &cramfs_aops; |
| 101 | break; |
| 102 | case S_IFDIR: |
| 103 | inode->i_op = &cramfs_dir_inode_operations; |
| 104 | inode->i_fop = &cramfs_directory_operations; |
| 105 | break; |
| 106 | case S_IFLNK: |
| 107 | inode->i_op = &page_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 108 | inode_nohighmem(inode); |
Stefani Seibold | 6f772fe | 2011-01-12 17:01:10 -0800 | [diff] [blame] | 109 | inode->i_data.a_ops = &cramfs_aops; |
| 110 | break; |
| 111 | default: |
| 112 | init_special_inode(inode, cramfs_inode->mode, |
| 113 | old_decode_dev(cramfs_inode->size)); |
| 114 | } |
| 115 | |
Al Viro | 77b8a75 | 2010-06-04 21:19:01 -0400 | [diff] [blame] | 116 | inode->i_mode = cramfs_inode->mode; |
Eric W. Biederman | a7d9cfe | 2012-02-10 11:06:08 -0800 | [diff] [blame] | 117 | i_uid_write(inode, cramfs_inode->uid); |
| 118 | i_gid_write(inode, cramfs_inode->gid); |
Stefani Seibold | 6f772fe | 2011-01-12 17:01:10 -0800 | [diff] [blame] | 119 | |
| 120 | /* if the lower 2 bits are zero, the inode contains data */ |
| 121 | if (!(inode->i_ino & 3)) { |
| 122 | inode->i_size = cramfs_inode->size; |
| 123 | inode->i_blocks = (cramfs_inode->size - 1) / 512 + 1; |
| 124 | } |
| 125 | |
Al Viro | 77b8a75 | 2010-06-04 21:19:01 -0400 | [diff] [blame] | 126 | /* Struct copy intentional */ |
| 127 | inode->i_mtime = inode->i_atime = inode->i_ctime = zerotime; |
| 128 | /* inode->i_nlink is left 1 - arguably wrong for directories, |
| 129 | but it's the best we can do without reading the directory |
| 130 | contents. 1 yields the right result in GNU find, even |
| 131 | without -noleaf option. */ |
Dave Johnson | a97c9bf | 2005-09-06 15:17:40 -0700 | [diff] [blame] | 132 | |
Stefani Seibold | 6f772fe | 2011-01-12 17:01:10 -0800 | [diff] [blame] | 133 | unlock_new_inode(inode); |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | return inode; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * We have our own block cache: don't fill up the buffer cache |
| 140 | * with the rom-image, because the way the filesystem is set |
| 141 | * up the accesses should be fairly regular and cached in the |
| 142 | * page cache and dentry tree anyway.. |
| 143 | * |
| 144 | * This also acts as a way to guarantee contiguous areas of up to |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 145 | * BLKS_PER_BUF*PAGE_SIZE, so that the caller doesn't need to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | * worry about end-of-buffer issues even when decompressing a full |
| 147 | * page cache. |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 148 | * |
| 149 | * Note: This is all optimized away at compile time when |
| 150 | * CONFIG_CRAMFS_BLOCKDEV=n. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | */ |
| 152 | #define READ_BUFFERS (2) |
| 153 | /* NEXT_BUFFER(): Loop over [0..(READ_BUFFERS-1)]. */ |
| 154 | #define NEXT_BUFFER(_ix) ((_ix) ^ 1) |
| 155 | |
| 156 | /* |
| 157 | * BLKS_PER_BUF_SHIFT should be at least 2 to allow for "compressed" |
| 158 | * data that takes up more space than the original and with unlucky |
| 159 | * alignment. |
| 160 | */ |
| 161 | #define BLKS_PER_BUF_SHIFT (2) |
| 162 | #define BLKS_PER_BUF (1 << BLKS_PER_BUF_SHIFT) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 163 | #define BUFFER_SIZE (BLKS_PER_BUF*PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | static unsigned char read_buffers[READ_BUFFERS][BUFFER_SIZE]; |
| 166 | static unsigned buffer_blocknr[READ_BUFFERS]; |
Fabian Frederick | 31d92e5 | 2014-08-08 14:22:52 -0700 | [diff] [blame] | 167 | static struct super_block *buffer_dev[READ_BUFFERS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | static int next_buffer; |
| 169 | |
| 170 | /* |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 171 | * Populate our block cache and return a pointer to it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | */ |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 173 | static void *cramfs_blkdev_read(struct super_block *sb, unsigned int offset, |
| 174 | unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
| 176 | struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping; |
| 177 | struct page *pages[BLKS_PER_BUF]; |
Andi Drebes | 6bbfb07 | 2007-10-18 03:06:54 -0700 | [diff] [blame] | 178 | unsigned i, blocknr, buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | unsigned long devsize; |
| 180 | char *data; |
| 181 | |
| 182 | if (!len) |
| 183 | return NULL; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 184 | blocknr = offset >> PAGE_SHIFT; |
| 185 | offset &= PAGE_SIZE - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | /* Check if an existing buffer already has the data.. */ |
| 188 | for (i = 0; i < READ_BUFFERS; i++) { |
| 189 | unsigned int blk_offset; |
| 190 | |
| 191 | if (buffer_dev[i] != sb) |
| 192 | continue; |
| 193 | if (blocknr < buffer_blocknr[i]) |
| 194 | continue; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 195 | blk_offset = (blocknr - buffer_blocknr[i]) << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | blk_offset += offset; |
| 197 | if (blk_offset + len > BUFFER_SIZE) |
| 198 | continue; |
| 199 | return read_buffers[i] + blk_offset; |
| 200 | } |
| 201 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 202 | devsize = mapping->host->i_size >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
| 204 | /* Ok, read in BLKS_PER_BUF pages completely first. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | for (i = 0; i < BLKS_PER_BUF; i++) { |
| 206 | struct page *page = NULL; |
| 207 | |
| 208 | if (blocknr + i < devsize) { |
Sasha Levin | 67f9fd9 | 2014-04-03 14:48:18 -0700 | [diff] [blame] | 209 | page = read_mapping_page(mapping, blocknr + i, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | /* synchronous error? */ |
| 211 | if (IS_ERR(page)) |
| 212 | page = NULL; |
| 213 | } |
| 214 | pages[i] = page; |
| 215 | } |
| 216 | |
| 217 | for (i = 0; i < BLKS_PER_BUF; i++) { |
| 218 | struct page *page = pages[i]; |
Fabian Frederick | 31d92e5 | 2014-08-08 14:22:52 -0700 | [diff] [blame] | 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | if (page) { |
| 221 | wait_on_page_locked(page); |
| 222 | if (!PageUptodate(page)) { |
| 223 | /* asynchronous error */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 224 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | pages[i] = NULL; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | buffer = next_buffer; |
| 231 | next_buffer = NEXT_BUFFER(buffer); |
| 232 | buffer_blocknr[buffer] = blocknr; |
| 233 | buffer_dev[buffer] = sb; |
| 234 | |
| 235 | data = read_buffers[buffer]; |
| 236 | for (i = 0; i < BLKS_PER_BUF; i++) { |
| 237 | struct page *page = pages[i]; |
Fabian Frederick | 31d92e5 | 2014-08-08 14:22:52 -0700 | [diff] [blame] | 238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | if (page) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 240 | memcpy(data, kmap(page), PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | kunmap(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 242 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 244 | memset(data, 0, PAGE_SIZE); |
| 245 | data += PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
| 247 | return read_buffers[buffer] + offset; |
| 248 | } |
| 249 | |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 250 | /* |
| 251 | * Return a pointer to the linearly addressed cramfs image in memory. |
| 252 | */ |
| 253 | static void *cramfs_direct_read(struct super_block *sb, unsigned int offset, |
| 254 | unsigned int len) |
| 255 | { |
| 256 | struct cramfs_sb_info *sbi = CRAMFS_SB(sb); |
| 257 | |
| 258 | if (!len) |
| 259 | return NULL; |
| 260 | if (len > sbi->size || offset > sbi->size - len) |
| 261 | return page_address(ZERO_PAGE(0)); |
| 262 | return sbi->linear_virt_addr + offset; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Returns a pointer to a buffer containing at least LEN bytes of |
| 267 | * filesystem starting at byte offset OFFSET into the filesystem. |
| 268 | */ |
| 269 | static void *cramfs_read(struct super_block *sb, unsigned int offset, |
| 270 | unsigned int len) |
| 271 | { |
| 272 | struct cramfs_sb_info *sbi = CRAMFS_SB(sb); |
| 273 | |
| 274 | if (IS_ENABLED(CONFIG_CRAMFS_MTD) && sbi->linear_virt_addr) |
| 275 | return cramfs_direct_read(sb, offset, len); |
| 276 | else if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV)) |
| 277 | return cramfs_blkdev_read(sb, offset, len); |
| 278 | else |
| 279 | return NULL; |
| 280 | } |
| 281 | |
Al Viro | 2309fb8 | 2013-12-10 16:35:14 -0500 | [diff] [blame] | 282 | static void cramfs_kill_sb(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { |
Al Viro | f7f4f4d | 2013-12-10 16:54:28 -0500 | [diff] [blame] | 284 | struct cramfs_sb_info *sbi = CRAMFS_SB(sb); |
Fabian Frederick | 31d92e5 | 2014-08-08 14:22:52 -0700 | [diff] [blame] | 285 | |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 286 | if (IS_ENABLED(CCONFIG_CRAMFS_MTD) && sb->s_mtd) { |
| 287 | if (sbi && sbi->mtd_point_size) |
| 288 | mtd_unpoint(sb->s_mtd, 0, sbi->mtd_point_size); |
| 289 | kill_mtd_super(sb); |
| 290 | } else if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV) && sb->s_bdev) { |
| 291 | kill_block_super(sb); |
| 292 | } |
Al Viro | 2309fb8 | 2013-12-10 16:35:14 -0500 | [diff] [blame] | 293 | kfree(sbi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static int cramfs_remount(struct super_block *sb, int *flags, char *data) |
| 297 | { |
Theodore Ts'o | 02b9984 | 2014-03-13 10:14:33 -0400 | [diff] [blame] | 298 | sync_filesystem(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | *flags |= MS_RDONLY; |
| 300 | return 0; |
| 301 | } |
| 302 | |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 303 | static int cramfs_read_super(struct super_block *sb, |
| 304 | struct cramfs_super *super, int silent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | { |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 306 | struct cramfs_sb_info *sbi = CRAMFS_SB(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | unsigned long root_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 309 | /* We don't know the real size yet */ |
| 310 | sbi->size = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | /* Read the first block and get the superblock from it */ |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 313 | mutex_lock(&read_mutex); |
| 314 | memcpy(super, cramfs_read(sb, 0, sizeof(*super)), sizeof(*super)); |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 315 | mutex_unlock(&read_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
| 317 | /* Do sanity checks on the superblock */ |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 318 | if (super->magic != CRAMFS_MAGIC) { |
Masanari Iida | 0cc785e | 2012-02-11 21:35:12 +0900 | [diff] [blame] | 319 | /* check for wrong endianness */ |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 320 | if (super->magic == CRAMFS_MAGIC_WEND) { |
Andi Drebes | ac8d35c | 2007-10-16 23:27:12 -0700 | [diff] [blame] | 321 | if (!silent) |
Fabian Frederick | 4f21e1e | 2014-08-08 14:22:50 -0700 | [diff] [blame] | 322 | pr_err("wrong endianness\n"); |
Al Viro | 2309fb8 | 2013-12-10 16:35:14 -0500 | [diff] [blame] | 323 | return -EINVAL; |
Andi Drebes | ac8d35c | 2007-10-16 23:27:12 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | /* check at 512 byte offset */ |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 327 | mutex_lock(&read_mutex); |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 328 | memcpy(super, |
| 329 | cramfs_read(sb, 512, sizeof(*super)), |
| 330 | sizeof(*super)); |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 331 | mutex_unlock(&read_mutex); |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 332 | if (super->magic != CRAMFS_MAGIC) { |
| 333 | if (super->magic == CRAMFS_MAGIC_WEND && !silent) |
Fabian Frederick | 4f21e1e | 2014-08-08 14:22:50 -0700 | [diff] [blame] | 334 | pr_err("wrong endianness\n"); |
Andi Drebes | ac8d35c | 2007-10-16 23:27:12 -0700 | [diff] [blame] | 335 | else if (!silent) |
Fabian Frederick | 4f21e1e | 2014-08-08 14:22:50 -0700 | [diff] [blame] | 336 | pr_err("wrong magic\n"); |
Al Viro | 2309fb8 | 2013-12-10 16:35:14 -0500 | [diff] [blame] | 337 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | /* get feature flags first */ |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 342 | if (super->flags & ~CRAMFS_SUPPORTED_FLAGS) { |
Fabian Frederick | 4f21e1e | 2014-08-08 14:22:50 -0700 | [diff] [blame] | 343 | pr_err("unsupported filesystem features\n"); |
Al Viro | 2309fb8 | 2013-12-10 16:35:14 -0500 | [diff] [blame] | 344 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /* Check that the root inode is in a sane state */ |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 348 | if (!S_ISDIR(super->root.mode)) { |
Fabian Frederick | 4f21e1e | 2014-08-08 14:22:50 -0700 | [diff] [blame] | 349 | pr_err("root is not a directory\n"); |
Al Viro | 2309fb8 | 2013-12-10 16:35:14 -0500 | [diff] [blame] | 350 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } |
Stefani Seibold | 6f772fe | 2011-01-12 17:01:10 -0800 | [diff] [blame] | 352 | /* correct strange, hard-coded permissions of mkcramfs */ |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 353 | super->root.mode |= 0555; |
Stefani Seibold | 6f772fe | 2011-01-12 17:01:10 -0800 | [diff] [blame] | 354 | |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 355 | root_offset = super->root.offset << 2; |
| 356 | if (super->flags & CRAMFS_FLAG_FSID_VERSION_2) { |
| 357 | sbi->size = super->size; |
| 358 | sbi->blocks = super->fsid.blocks; |
| 359 | sbi->files = super->fsid.files; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } else { |
Fabian Frederick | 31d92e5 | 2014-08-08 14:22:52 -0700 | [diff] [blame] | 361 | sbi->size = 1<<28; |
| 362 | sbi->blocks = 0; |
| 363 | sbi->files = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 365 | sbi->magic = super->magic; |
| 366 | sbi->flags = super->flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | if (root_offset == 0) |
Fabian Frederick | 4f21e1e | 2014-08-08 14:22:50 -0700 | [diff] [blame] | 368 | pr_info("empty filesystem"); |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 369 | else if (!(super->flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | ((root_offset != sizeof(struct cramfs_super)) && |
| 371 | (root_offset != 512 + sizeof(struct cramfs_super)))) |
| 372 | { |
Fabian Frederick | 4f21e1e | 2014-08-08 14:22:50 -0700 | [diff] [blame] | 373 | pr_err("bad root offset %lu\n", root_offset); |
Al Viro | 2309fb8 | 2013-12-10 16:35:14 -0500 | [diff] [blame] | 374 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | static int cramfs_finalize_super(struct super_block *sb, |
| 381 | struct cramfs_inode *cramfs_root) |
| 382 | { |
| 383 | struct inode *root; |
| 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | /* Set it all up.. */ |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 386 | sb->s_flags |= MS_RDONLY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | sb->s_op = &cramfs_ops; |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 388 | root = get_cramfs_inode(sb, cramfs_root, 0); |
Al Viro | 0577d1b | 2011-07-17 19:04:14 -0400 | [diff] [blame] | 389 | if (IS_ERR(root)) |
Al Viro | 2309fb8 | 2013-12-10 16:35:14 -0500 | [diff] [blame] | 390 | return PTR_ERR(root); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 391 | sb->s_root = d_make_root(root); |
| 392 | if (!sb->s_root) |
Al Viro | 2309fb8 | 2013-12-10 16:35:14 -0500 | [diff] [blame] | 393 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 397 | static int cramfs_blkdev_fill_super(struct super_block *sb, void *data, |
| 398 | int silent) |
| 399 | { |
| 400 | struct cramfs_sb_info *sbi; |
| 401 | struct cramfs_super super; |
| 402 | int i, err; |
| 403 | |
| 404 | sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL); |
| 405 | if (!sbi) |
| 406 | return -ENOMEM; |
| 407 | sb->s_fs_info = sbi; |
| 408 | |
| 409 | /* Invalidate the read buffers on mount: think disk change.. */ |
| 410 | for (i = 0; i < READ_BUFFERS; i++) |
| 411 | buffer_blocknr[i] = -1; |
| 412 | |
| 413 | err = cramfs_read_super(sb, &super, silent); |
| 414 | if (err) |
| 415 | return err; |
| 416 | return cramfs_finalize_super(sb, &super.root); |
| 417 | } |
| 418 | |
| 419 | static int cramfs_mtd_fill_super(struct super_block *sb, void *data, |
| 420 | int silent) |
| 421 | { |
| 422 | struct cramfs_sb_info *sbi; |
| 423 | struct cramfs_super super; |
| 424 | int err; |
| 425 | |
| 426 | sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL); |
| 427 | if (!sbi) |
| 428 | return -ENOMEM; |
| 429 | sb->s_fs_info = sbi; |
| 430 | |
| 431 | /* Map only one page for now. Will remap it when fs size is known. */ |
| 432 | err = mtd_point(sb->s_mtd, 0, PAGE_SIZE, &sbi->mtd_point_size, |
| 433 | &sbi->linear_virt_addr, &sbi->linear_phys_addr); |
| 434 | if (err || sbi->mtd_point_size != PAGE_SIZE) { |
| 435 | pr_err("unable to get direct memory access to mtd:%s\n", |
| 436 | sb->s_mtd->name); |
| 437 | return err ? : -ENODATA; |
| 438 | } |
| 439 | |
| 440 | pr_info("checking physical address %pap for linear cramfs image\n", |
| 441 | &sbi->linear_phys_addr); |
| 442 | err = cramfs_read_super(sb, &super, silent); |
| 443 | if (err) |
| 444 | return err; |
| 445 | |
| 446 | /* Remap the whole filesystem now */ |
| 447 | pr_info("linear cramfs image on mtd:%s appears to be %lu KB in size\n", |
| 448 | sb->s_mtd->name, sbi->size/1024); |
| 449 | mtd_unpoint(sb->s_mtd, 0, PAGE_SIZE); |
| 450 | err = mtd_point(sb->s_mtd, 0, sbi->size, &sbi->mtd_point_size, |
| 451 | &sbi->linear_virt_addr, &sbi->linear_phys_addr); |
| 452 | if (err || sbi->mtd_point_size != sbi->size) { |
| 453 | pr_err("unable to get direct memory access to mtd:%s\n", |
| 454 | sb->s_mtd->name); |
| 455 | return err ? : -ENODATA; |
| 456 | } |
| 457 | |
| 458 | return cramfs_finalize_super(sb, &super.root); |
| 459 | } |
| 460 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 461 | static int cramfs_statfs(struct dentry *dentry, struct kstatfs *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | { |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 463 | struct super_block *sb = dentry->d_sb; |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 464 | u64 id = 0; |
| 465 | |
| 466 | if (sb->s_bdev) |
| 467 | id = huge_encode_dev(sb->s_bdev->bd_dev); |
| 468 | else if (sb->s_dev) |
| 469 | id = huge_encode_dev(sb->s_dev); |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | buf->f_type = CRAMFS_MAGIC; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 472 | buf->f_bsize = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | buf->f_blocks = CRAMFS_SB(sb)->blocks; |
| 474 | buf->f_bfree = 0; |
| 475 | buf->f_bavail = 0; |
| 476 | buf->f_files = CRAMFS_SB(sb)->files; |
| 477 | buf->f_ffree = 0; |
Coly Li | 94ea77a | 2009-04-02 16:59:33 -0700 | [diff] [blame] | 478 | buf->f_fsid.val[0] = (u32)id; |
| 479 | buf->f_fsid.val[1] = (u32)(id >> 32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | buf->f_namelen = CRAMFS_MAXPATHLEN; |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | /* |
| 485 | * Read a cramfs directory entry. |
| 486 | */ |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 487 | static int cramfs_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 489 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | struct super_block *sb = inode->i_sb; |
| 491 | char *buf; |
| 492 | unsigned int offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | /* Offset within the thing. */ |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 495 | if (ctx->pos >= inode->i_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | return 0; |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 497 | offset = ctx->pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | /* Directory entries are always 4-byte aligned */ |
| 499 | if (offset & 3) |
| 500 | return -EINVAL; |
| 501 | |
Andi Drebes | 4176ed5 | 2007-10-18 03:06:55 -0700 | [diff] [blame] | 502 | buf = kmalloc(CRAMFS_MAXPATHLEN, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | if (!buf) |
| 504 | return -ENOMEM; |
| 505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | while (offset < inode->i_size) { |
| 507 | struct cramfs_inode *de; |
| 508 | unsigned long nextoffset; |
| 509 | char *name; |
| 510 | ino_t ino; |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 511 | umode_t mode; |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 512 | int namelen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 514 | mutex_lock(&read_mutex); |
Andi Drebes | 4176ed5 | 2007-10-18 03:06:55 -0700 | [diff] [blame] | 515 | de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+CRAMFS_MAXPATHLEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | name = (char *)(de+1); |
| 517 | |
| 518 | /* |
| 519 | * Namelengths on disk are shifted by two |
| 520 | * and the name padded out to 4-byte boundaries |
| 521 | * with zeroes. |
| 522 | */ |
| 523 | namelen = de->namelen << 2; |
| 524 | memcpy(buf, name, namelen); |
Stefani Seibold | 6f772fe | 2011-01-12 17:01:10 -0800 | [diff] [blame] | 525 | ino = cramino(de, OFFSET(inode) + offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | mode = de->mode; |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 527 | mutex_unlock(&read_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | nextoffset = offset + sizeof(*de) + namelen; |
| 529 | for (;;) { |
| 530 | if (!namelen) { |
| 531 | kfree(buf); |
| 532 | return -EIO; |
| 533 | } |
| 534 | if (buf[namelen-1]) |
| 535 | break; |
| 536 | namelen--; |
| 537 | } |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 538 | if (!dir_emit(ctx, buf, namelen, ino, mode >> 12)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | break; |
| 540 | |
Al Viro | 6f7f231 | 2013-05-17 18:02:17 -0400 | [diff] [blame] | 541 | ctx->pos = offset = nextoffset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | } |
| 543 | kfree(buf); |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | * Lookup and fill in the inode data.. |
| 549 | */ |
Fabian Frederick | 31d92e5 | 2014-08-08 14:22:52 -0700 | [diff] [blame] | 550 | static struct dentry *cramfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | { |
| 552 | unsigned int offset = 0; |
Al Viro | 0577d1b | 2011-07-17 19:04:14 -0400 | [diff] [blame] | 553 | struct inode *inode = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | int sorted; |
| 555 | |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 556 | mutex_lock(&read_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | sorted = CRAMFS_SB(dir->i_sb)->flags & CRAMFS_FLAG_SORTED_DIRS; |
| 558 | while (offset < dir->i_size) { |
| 559 | struct cramfs_inode *de; |
| 560 | char *name; |
| 561 | int namelen, retval; |
Stefani Seibold | 6f772fe | 2011-01-12 17:01:10 -0800 | [diff] [blame] | 562 | int dir_off = OFFSET(dir) + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
Stefani Seibold | 6f772fe | 2011-01-12 17:01:10 -0800 | [diff] [blame] | 564 | de = cramfs_read(dir->i_sb, dir_off, sizeof(*de)+CRAMFS_MAXPATHLEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | name = (char *)(de+1); |
| 566 | |
| 567 | /* Try to take advantage of sorted directories */ |
| 568 | if (sorted && (dentry->d_name.name[0] < name[0])) |
| 569 | break; |
| 570 | |
| 571 | namelen = de->namelen << 2; |
| 572 | offset += sizeof(*de) + namelen; |
| 573 | |
| 574 | /* Quick check that the name is roughly the right length */ |
| 575 | if (((dentry->d_name.len + 3) & ~3) != namelen) |
| 576 | continue; |
| 577 | |
| 578 | for (;;) { |
| 579 | if (!namelen) { |
Al Viro | 0577d1b | 2011-07-17 19:04:14 -0400 | [diff] [blame] | 580 | inode = ERR_PTR(-EIO); |
| 581 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } |
| 583 | if (name[namelen-1]) |
| 584 | break; |
| 585 | namelen--; |
| 586 | } |
| 587 | if (namelen != dentry->d_name.len) |
| 588 | continue; |
| 589 | retval = memcmp(dentry->d_name.name, name, namelen); |
| 590 | if (retval > 0) |
| 591 | continue; |
| 592 | if (!retval) { |
Al Viro | 0577d1b | 2011-07-17 19:04:14 -0400 | [diff] [blame] | 593 | inode = get_cramfs_inode(dir->i_sb, de, dir_off); |
| 594 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | /* else (retval < 0) */ |
| 597 | if (sorted) |
| 598 | break; |
| 599 | } |
Al Viro | 0577d1b | 2011-07-17 19:04:14 -0400 | [diff] [blame] | 600 | out: |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 601 | mutex_unlock(&read_mutex); |
Al Viro | 0577d1b | 2011-07-17 19:04:14 -0400 | [diff] [blame] | 602 | if (IS_ERR(inode)) |
| 603 | return ERR_CAST(inode); |
| 604 | d_add(dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | return NULL; |
| 606 | } |
| 607 | |
Fabian Frederick | 31d92e5 | 2014-08-08 14:22:52 -0700 | [diff] [blame] | 608 | static int cramfs_readpage(struct file *file, struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | { |
| 610 | struct inode *inode = page->mapping->host; |
David VomLehn | 98310e5 | 2009-04-02 16:59:15 -0700 | [diff] [blame] | 611 | u32 maxblock; |
| 612 | int bytes_filled; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | void *pgdata; |
| 614 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 615 | maxblock = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | bytes_filled = 0; |
David VomLehn | 98310e5 | 2009-04-02 16:59:15 -0700 | [diff] [blame] | 617 | pgdata = kmap(page); |
| 618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | if (page->index < maxblock) { |
| 620 | struct super_block *sb = inode->i_sb; |
| 621 | u32 blkptr_offset = OFFSET(inode) + page->index*4; |
| 622 | u32 start_offset, compr_len; |
| 623 | |
| 624 | start_offset = OFFSET(inode) + maxblock*4; |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 625 | mutex_lock(&read_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | if (page->index) |
David VomLehn | 98310e5 | 2009-04-02 16:59:15 -0700 | [diff] [blame] | 627 | start_offset = *(u32 *) cramfs_read(sb, blkptr_offset-4, |
| 628 | 4); |
| 629 | compr_len = (*(u32 *) cramfs_read(sb, blkptr_offset, 4) - |
| 630 | start_offset); |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 631 | mutex_unlock(&read_mutex); |
David VomLehn | 98310e5 | 2009-04-02 16:59:15 -0700 | [diff] [blame] | 632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | if (compr_len == 0) |
| 634 | ; /* hole */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 635 | else if (unlikely(compr_len > (PAGE_SIZE << 1))) { |
Fabian Frederick | 4f21e1e | 2014-08-08 14:22:50 -0700 | [diff] [blame] | 636 | pr_err("bad compressed blocksize %u\n", |
David VomLehn | 98310e5 | 2009-04-02 16:59:15 -0700 | [diff] [blame] | 637 | compr_len); |
| 638 | goto err; |
| 639 | } else { |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 640 | mutex_lock(&read_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | bytes_filled = cramfs_uncompress_block(pgdata, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 642 | PAGE_SIZE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | cramfs_read(sb, start_offset, compr_len), |
| 644 | compr_len); |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 645 | mutex_unlock(&read_mutex); |
David VomLehn | 98310e5 | 2009-04-02 16:59:15 -0700 | [diff] [blame] | 646 | if (unlikely(bytes_filled < 0)) |
| 647 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } |
David VomLehn | 98310e5 | 2009-04-02 16:59:15 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 651 | memset(pgdata + bytes_filled, 0, PAGE_SIZE - bytes_filled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | flush_dcache_page(page); |
David VomLehn | 98310e5 | 2009-04-02 16:59:15 -0700 | [diff] [blame] | 653 | kunmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | SetPageUptodate(page); |
| 655 | unlock_page(page); |
| 656 | return 0; |
David VomLehn | 98310e5 | 2009-04-02 16:59:15 -0700 | [diff] [blame] | 657 | |
| 658 | err: |
| 659 | kunmap(page); |
| 660 | ClearPageUptodate(page); |
| 661 | SetPageError(page); |
| 662 | unlock_page(page); |
| 663 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 666 | static const struct address_space_operations cramfs_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | .readpage = cramfs_readpage |
| 668 | }; |
| 669 | |
| 670 | /* |
| 671 | * Our operations: |
| 672 | */ |
| 673 | |
| 674 | /* |
| 675 | * A directory can only readdir |
| 676 | */ |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 677 | static const struct file_operations cramfs_directory_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | .llseek = generic_file_llseek, |
| 679 | .read = generic_read_dir, |
Al Viro | c51da20 | 2016-04-30 22:37:34 -0400 | [diff] [blame] | 680 | .iterate_shared = cramfs_readdir, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | }; |
| 682 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 683 | static const struct inode_operations cramfs_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | .lookup = cramfs_lookup, |
| 685 | }; |
| 686 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 687 | static const struct super_operations cramfs_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | .remount_fs = cramfs_remount, |
| 689 | .statfs = cramfs_statfs, |
| 690 | }; |
| 691 | |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 692 | static struct dentry *cramfs_mount(struct file_system_type *fs_type, int flags, |
| 693 | const char *dev_name, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | { |
Nicolas Pitre | 99c18ce | 2017-10-13 16:09:23 -0400 | [diff] [blame^] | 695 | struct dentry *ret = ERR_PTR(-ENOPROTOOPT); |
| 696 | |
| 697 | if (IS_ENABLED(CONFIG_CRAMFS_MTD)) { |
| 698 | ret = mount_mtd(fs_type, flags, dev_name, data, |
| 699 | cramfs_mtd_fill_super); |
| 700 | if (!IS_ERR(ret)) |
| 701 | return ret; |
| 702 | } |
| 703 | if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV)) { |
| 704 | ret = mount_bdev(fs_type, flags, dev_name, data, |
| 705 | cramfs_blkdev_fill_super); |
| 706 | } |
| 707 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | static struct file_system_type cramfs_fs_type = { |
| 711 | .owner = THIS_MODULE, |
| 712 | .name = "cramfs", |
Al Viro | 152a083 | 2010-07-25 00:46:55 +0400 | [diff] [blame] | 713 | .mount = cramfs_mount, |
Al Viro | 2309fb8 | 2013-12-10 16:35:14 -0500 | [diff] [blame] | 714 | .kill_sb = cramfs_kill_sb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | .fs_flags = FS_REQUIRES_DEV, |
| 716 | }; |
Eric W. Biederman | 7f78e03 | 2013-03-02 19:39:14 -0800 | [diff] [blame] | 717 | MODULE_ALIAS_FS("cramfs"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | |
| 719 | static int __init init_cramfs_fs(void) |
| 720 | { |
Alexey Dobriyan | 50d44ed | 2006-09-29 02:01:04 -0700 | [diff] [blame] | 721 | int rv; |
| 722 | |
| 723 | rv = cramfs_uncompress_init(); |
| 724 | if (rv < 0) |
| 725 | return rv; |
| 726 | rv = register_filesystem(&cramfs_fs_type); |
| 727 | if (rv < 0) |
| 728 | cramfs_uncompress_exit(); |
| 729 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | static void __exit exit_cramfs_fs(void) |
| 733 | { |
| 734 | cramfs_uncompress_exit(); |
| 735 | unregister_filesystem(&cramfs_fs_type); |
| 736 | } |
| 737 | |
| 738 | module_init(init_cramfs_fs) |
| 739 | module_exit(exit_cramfs_fs) |
| 740 | MODULE_LICENSE("GPL"); |