blob: bcdccb7a820b8661e2a3221706ac9e5e60729380 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Frederick4f21e1e2014-08-08 14:22:50 -070014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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 Pitre99c18ce2017-10-13 16:09:23 -040022#include <linux/mtd/mtd.h>
23#include <linux/mtd/super.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/vfs.h>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080026#include <linux/mutex.h>
Al Virof7f4f4d2013-12-10 16:54:28 -050027#include <uapi/linux/cramfs_fs.h>
Fabian Frederick1508f3eb2014-08-08 14:22:55 -070028#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Al Virof7f4f4d2013-12-10 16:54:28 -050030#include "internal.h"
31
32/*
33 * cramfs super-block data in memory
34 */
35struct cramfs_sb_info {
36 unsigned long magic;
37 unsigned long size;
38 unsigned long blocks;
39 unsigned long files;
40 unsigned long flags;
Nicolas Pitre99c18ce2017-10-13 16:09:23 -040041 void *linear_virt_addr;
42 resource_size_t linear_phys_addr;
43 size_t mtd_point_size;
Al Virof7f4f4d2013-12-10 16:54:28 -050044};
45
46static inline struct cramfs_sb_info *CRAMFS_SB(struct super_block *sb)
47{
48 return sb->s_fs_info;
49}
50
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080051static const struct super_operations cramfs_ops;
Arjan van de Ven754661f2007-02-12 00:55:38 -080052static const struct inode_operations cramfs_dir_inode_operations;
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080053static const struct file_operations cramfs_directory_operations;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070054static const struct address_space_operations cramfs_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Ingo Molnar353ab6e2006-03-26 01:37:12 -080056static DEFINE_MUTEX(read_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58
Stefani Seibold6f772fe2011-01-12 17:01:10 -080059/* These macros may change in future, to provide better st_ino semantics. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define OFFSET(x) ((x)->i_ino)
61
Al Viro0577d1b2011-07-17 19:04:14 -040062static unsigned long cramino(const struct cramfs_inode *cino, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Stefani Seibold6f772fe2011-01-12 17:01:10 -080064 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
85static struct inode *get_cramfs_inode(struct super_block *sb,
Al Viro0577d1b2011-07-17 19:04:14 -040086 const struct cramfs_inode *cramfs_inode, unsigned int offset)
Stefani Seibold6f772fe2011-01-12 17:01:10 -080087{
88 struct inode *inode;
Al Viro77b8a752010-06-04 21:19:01 -040089 static struct timespec zerotime;
Stefani Seibold6f772fe2011-01-12 17:01:10 -080090
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 Viro21fc61c2015-11-17 01:07:57 -0500108 inode_nohighmem(inode);
Stefani Seibold6f772fe2011-01-12 17:01:10 -0800109 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 Viro77b8a752010-06-04 21:19:01 -0400116 inode->i_mode = cramfs_inode->mode;
Eric W. Biedermana7d9cfe2012-02-10 11:06:08 -0800117 i_uid_write(inode, cramfs_inode->uid);
118 i_gid_write(inode, cramfs_inode->gid);
Stefani Seibold6f772fe2011-01-12 17:01:10 -0800119
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 Viro77b8a752010-06-04 21:19:01 -0400126 /* 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 Johnsona97c9bf2005-09-06 15:17:40 -0700132
Stefani Seibold6f772fe2011-01-12 17:01:10 -0800133 unlock_new_inode(inode);
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 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. Shutemovea1754a2016-04-01 15:29:48 +0300145 * BLKS_PER_BUF*PAGE_SIZE, so that the caller doesn't need to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * worry about end-of-buffer issues even when decompressing a full
147 * page cache.
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400148 *
149 * Note: This is all optimized away at compile time when
150 * CONFIG_CRAMFS_BLOCKDEV=n.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 */
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. Shutemov09cbfea2016-04-01 15:29:47 +0300163#define BUFFER_SIZE (BLKS_PER_BUF*PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165static unsigned char read_buffers[READ_BUFFERS][BUFFER_SIZE];
166static unsigned buffer_blocknr[READ_BUFFERS];
Fabian Frederick31d92e52014-08-08 14:22:52 -0700167static struct super_block *buffer_dev[READ_BUFFERS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static int next_buffer;
169
170/*
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400171 * Populate our block cache and return a pointer to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400173static void *cramfs_blkdev_read(struct super_block *sb, unsigned int offset,
174 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
177 struct page *pages[BLKS_PER_BUF];
Andi Drebes6bbfb072007-10-18 03:06:54 -0700178 unsigned i, blocknr, buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 unsigned long devsize;
180 char *data;
181
182 if (!len)
183 return NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300184 blocknr = offset >> PAGE_SHIFT;
185 offset &= PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
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. Shutemov09cbfea2016-04-01 15:29:47 +0300195 blk_offset = (blocknr - buffer_blocknr[i]) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 blk_offset += offset;
197 if (blk_offset + len > BUFFER_SIZE)
198 continue;
199 return read_buffers[i] + blk_offset;
200 }
201
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300202 devsize = mapping->host->i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 /* Ok, read in BLKS_PER_BUF pages completely first. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 for (i = 0; i < BLKS_PER_BUF; i++) {
206 struct page *page = NULL;
207
208 if (blocknr + i < devsize) {
Sasha Levin67f9fd92014-04-03 14:48:18 -0700209 page = read_mapping_page(mapping, blocknr + i, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /* 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 Frederick31d92e52014-08-08 14:22:52 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (page) {
221 wait_on_page_locked(page);
222 if (!PageUptodate(page)) {
223 /* asynchronous error */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300224 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 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 Frederick31d92e52014-08-08 14:22:52 -0700238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300240 memcpy(data, kmap(page), PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 kunmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300242 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 } else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300244 memset(data, 0, PAGE_SIZE);
245 data += PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247 return read_buffers[buffer] + offset;
248}
249
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400250/*
251 * Return a pointer to the linearly addressed cramfs image in memory.
252 */
253static 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 */
269static 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 Viro2309fb82013-12-10 16:35:14 -0500282static void cramfs_kill_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Al Virof7f4f4d2013-12-10 16:54:28 -0500284 struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
Fabian Frederick31d92e52014-08-08 14:22:52 -0700285
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400286 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 Viro2309fb82013-12-10 16:35:14 -0500293 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296static int cramfs_remount(struct super_block *sb, int *flags, char *data)
297{
Theodore Ts'o02b99842014-03-13 10:14:33 -0400298 sync_filesystem(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 *flags |= MS_RDONLY;
300 return 0;
301}
302
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400303static int cramfs_read_super(struct super_block *sb,
304 struct cramfs_super *super, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400306 struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 unsigned long root_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400309 /* We don't know the real size yet */
310 sbi->size = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 /* Read the first block and get the superblock from it */
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400313 mutex_lock(&read_mutex);
314 memcpy(super, cramfs_read(sb, 0, sizeof(*super)), sizeof(*super));
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800315 mutex_unlock(&read_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /* Do sanity checks on the superblock */
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400318 if (super->magic != CRAMFS_MAGIC) {
Masanari Iida0cc785e2012-02-11 21:35:12 +0900319 /* check for wrong endianness */
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400320 if (super->magic == CRAMFS_MAGIC_WEND) {
Andi Drebesac8d35c2007-10-16 23:27:12 -0700321 if (!silent)
Fabian Frederick4f21e1e2014-08-08 14:22:50 -0700322 pr_err("wrong endianness\n");
Al Viro2309fb82013-12-10 16:35:14 -0500323 return -EINVAL;
Andi Drebesac8d35c2007-10-16 23:27:12 -0700324 }
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /* check at 512 byte offset */
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800327 mutex_lock(&read_mutex);
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400328 memcpy(super,
329 cramfs_read(sb, 512, sizeof(*super)),
330 sizeof(*super));
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800331 mutex_unlock(&read_mutex);
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400332 if (super->magic != CRAMFS_MAGIC) {
333 if (super->magic == CRAMFS_MAGIC_WEND && !silent)
Fabian Frederick4f21e1e2014-08-08 14:22:50 -0700334 pr_err("wrong endianness\n");
Andi Drebesac8d35c2007-10-16 23:27:12 -0700335 else if (!silent)
Fabian Frederick4f21e1e2014-08-08 14:22:50 -0700336 pr_err("wrong magic\n");
Al Viro2309fb82013-12-10 16:35:14 -0500337 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 }
340
341 /* get feature flags first */
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400342 if (super->flags & ~CRAMFS_SUPPORTED_FLAGS) {
Fabian Frederick4f21e1e2014-08-08 14:22:50 -0700343 pr_err("unsupported filesystem features\n");
Al Viro2309fb82013-12-10 16:35:14 -0500344 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
347 /* Check that the root inode is in a sane state */
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400348 if (!S_ISDIR(super->root.mode)) {
Fabian Frederick4f21e1e2014-08-08 14:22:50 -0700349 pr_err("root is not a directory\n");
Al Viro2309fb82013-12-10 16:35:14 -0500350 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
Stefani Seibold6f772fe2011-01-12 17:01:10 -0800352 /* correct strange, hard-coded permissions of mkcramfs */
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400353 super->root.mode |= 0555;
Stefani Seibold6f772fe2011-01-12 17:01:10 -0800354
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400355 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 Torvalds1da177e2005-04-16 15:20:36 -0700360 } else {
Fabian Frederick31d92e52014-08-08 14:22:52 -0700361 sbi->size = 1<<28;
362 sbi->blocks = 0;
363 sbi->files = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400365 sbi->magic = super->magic;
366 sbi->flags = super->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (root_offset == 0)
Fabian Frederick4f21e1e2014-08-08 14:22:50 -0700368 pr_info("empty filesystem");
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400369 else if (!(super->flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 ((root_offset != sizeof(struct cramfs_super)) &&
371 (root_offset != 512 + sizeof(struct cramfs_super))))
372 {
Fabian Frederick4f21e1e2014-08-08 14:22:50 -0700373 pr_err("bad root offset %lu\n", root_offset);
Al Viro2309fb82013-12-10 16:35:14 -0500374 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400377 return 0;
378}
379
380static int cramfs_finalize_super(struct super_block *sb,
381 struct cramfs_inode *cramfs_root)
382{
383 struct inode *root;
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 /* Set it all up.. */
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400386 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 sb->s_op = &cramfs_ops;
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400388 root = get_cramfs_inode(sb, cramfs_root, 0);
Al Viro0577d1b2011-07-17 19:04:14 -0400389 if (IS_ERR(root))
Al Viro2309fb82013-12-10 16:35:14 -0500390 return PTR_ERR(root);
Al Viro48fde702012-01-08 22:15:13 -0500391 sb->s_root = d_make_root(root);
392 if (!sb->s_root)
Al Viro2309fb82013-12-10 16:35:14 -0500393 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400397static 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
419static 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 Howells726c3342006-06-23 02:02:58 -0700461static int cramfs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
David Howells726c3342006-06-23 02:02:58 -0700463 struct super_block *sb = dentry->d_sb;
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400464 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 Howells726c3342006-06-23 02:02:58 -0700470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 buf->f_type = CRAMFS_MAGIC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300472 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 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 Li94ea77a2009-04-02 16:59:33 -0700478 buf->f_fsid.val[0] = (u32)id;
479 buf->f_fsid.val[1] = (u32)(id >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 buf->f_namelen = CRAMFS_MAXPATHLEN;
481 return 0;
482}
483
484/*
485 * Read a cramfs directory entry.
486 */
Al Viro6f7f2312013-05-17 18:02:17 -0400487static int cramfs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Al Viro6f7f2312013-05-17 18:02:17 -0400489 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct super_block *sb = inode->i_sb;
491 char *buf;
492 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* Offset within the thing. */
Al Viro6f7f2312013-05-17 18:02:17 -0400495 if (ctx->pos >= inode->i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return 0;
Al Viro6f7f2312013-05-17 18:02:17 -0400497 offset = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 /* Directory entries are always 4-byte aligned */
499 if (offset & 3)
500 return -EINVAL;
501
Andi Drebes4176ed52007-10-18 03:06:55 -0700502 buf = kmalloc(CRAMFS_MAXPATHLEN, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (!buf)
504 return -ENOMEM;
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 while (offset < inode->i_size) {
507 struct cramfs_inode *de;
508 unsigned long nextoffset;
509 char *name;
510 ino_t ino;
Al Viro175a4eb2011-07-26 03:30:54 -0400511 umode_t mode;
Al Viro6f7f2312013-05-17 18:02:17 -0400512 int namelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800514 mutex_lock(&read_mutex);
Andi Drebes4176ed52007-10-18 03:06:55 -0700515 de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+CRAMFS_MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 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 Seibold6f772fe2011-01-12 17:01:10 -0800525 ino = cramino(de, OFFSET(inode) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 mode = de->mode;
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800527 mutex_unlock(&read_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 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 Viro6f7f2312013-05-17 18:02:17 -0400538 if (!dir_emit(ctx, buf, namelen, ino, mode >> 12))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 break;
540
Al Viro6f7f2312013-05-17 18:02:17 -0400541 ctx->pos = offset = nextoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543 kfree(buf);
544 return 0;
545}
546
547/*
548 * Lookup and fill in the inode data..
549 */
Fabian Frederick31d92e52014-08-08 14:22:52 -0700550static struct dentry *cramfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 unsigned int offset = 0;
Al Viro0577d1b2011-07-17 19:04:14 -0400553 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 int sorted;
555
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800556 mutex_lock(&read_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 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 Seibold6f772fe2011-01-12 17:01:10 -0800562 int dir_off = OFFSET(dir) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Stefani Seibold6f772fe2011-01-12 17:01:10 -0800564 de = cramfs_read(dir->i_sb, dir_off, sizeof(*de)+CRAMFS_MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 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 Viro0577d1b2011-07-17 19:04:14 -0400580 inode = ERR_PTR(-EIO);
581 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
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 Viro0577d1b2011-07-17 19:04:14 -0400593 inode = get_cramfs_inode(dir->i_sb, de, dir_off);
594 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596 /* else (retval < 0) */
597 if (sorted)
598 break;
599 }
Al Viro0577d1b2011-07-17 19:04:14 -0400600out:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800601 mutex_unlock(&read_mutex);
Al Viro0577d1b2011-07-17 19:04:14 -0400602 if (IS_ERR(inode))
603 return ERR_CAST(inode);
604 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return NULL;
606}
607
Fabian Frederick31d92e52014-08-08 14:22:52 -0700608static int cramfs_readpage(struct file *file, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 struct inode *inode = page->mapping->host;
David VomLehn98310e52009-04-02 16:59:15 -0700611 u32 maxblock;
612 int bytes_filled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 void *pgdata;
614
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300615 maxblock = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 bytes_filled = 0;
David VomLehn98310e52009-04-02 16:59:15 -0700617 pgdata = kmap(page);
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 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 Molnar353ab6e2006-03-26 01:37:12 -0800625 mutex_lock(&read_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (page->index)
David VomLehn98310e52009-04-02 16:59:15 -0700627 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 Molnar353ab6e2006-03-26 01:37:12 -0800631 mutex_unlock(&read_mutex);
David VomLehn98310e52009-04-02 16:59:15 -0700632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (compr_len == 0)
634 ; /* hole */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300635 else if (unlikely(compr_len > (PAGE_SIZE << 1))) {
Fabian Frederick4f21e1e2014-08-08 14:22:50 -0700636 pr_err("bad compressed blocksize %u\n",
David VomLehn98310e52009-04-02 16:59:15 -0700637 compr_len);
638 goto err;
639 } else {
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800640 mutex_lock(&read_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 bytes_filled = cramfs_uncompress_block(pgdata,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300642 PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 cramfs_read(sb, start_offset, compr_len),
644 compr_len);
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800645 mutex_unlock(&read_mutex);
David VomLehn98310e52009-04-02 16:59:15 -0700646 if (unlikely(bytes_filled < 0))
647 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
David VomLehn98310e52009-04-02 16:59:15 -0700649 }
650
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300651 memset(pgdata + bytes_filled, 0, PAGE_SIZE - bytes_filled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 flush_dcache_page(page);
David VomLehn98310e52009-04-02 16:59:15 -0700653 kunmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 SetPageUptodate(page);
655 unlock_page(page);
656 return 0;
David VomLehn98310e52009-04-02 16:59:15 -0700657
658err:
659 kunmap(page);
660 ClearPageUptodate(page);
661 SetPageError(page);
662 unlock_page(page);
663 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700666static const struct address_space_operations cramfs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 .readpage = cramfs_readpage
668};
669
670/*
671 * Our operations:
672 */
673
674/*
675 * A directory can only readdir
676 */
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800677static const struct file_operations cramfs_directory_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 .llseek = generic_file_llseek,
679 .read = generic_read_dir,
Al Viroc51da202016-04-30 22:37:34 -0400680 .iterate_shared = cramfs_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681};
682
Arjan van de Ven754661f2007-02-12 00:55:38 -0800683static const struct inode_operations cramfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 .lookup = cramfs_lookup,
685};
686
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800687static const struct super_operations cramfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 .remount_fs = cramfs_remount,
689 .statfs = cramfs_statfs,
690};
691
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400692static struct dentry *cramfs_mount(struct file_system_type *fs_type, int flags,
693 const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Nicolas Pitre99c18ce2017-10-13 16:09:23 -0400695 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 Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710static struct file_system_type cramfs_fs_type = {
711 .owner = THIS_MODULE,
712 .name = "cramfs",
Al Viro152a0832010-07-25 00:46:55 +0400713 .mount = cramfs_mount,
Al Viro2309fb82013-12-10 16:35:14 -0500714 .kill_sb = cramfs_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 .fs_flags = FS_REQUIRES_DEV,
716};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800717MODULE_ALIAS_FS("cramfs");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719static int __init init_cramfs_fs(void)
720{
Alexey Dobriyan50d44ed2006-09-29 02:01:04 -0700721 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 Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
732static void __exit exit_cramfs_fs(void)
733{
734 cramfs_uncompress_exit();
735 unregister_filesystem(&cramfs_fs_type);
736}
737
738module_init(init_cramfs_fs)
739module_exit(exit_cramfs_fs)
740MODULE_LICENSE("GPL");