Merge tag 'for-6.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/pateldipen19...
[linux-block.git] / fs / cramfs / inode.c
CommitLineData
1da177e4
LT
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
4f21e1ea
FF
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
1da177e4
LT
16#include <linux/module.h>
17#include <linux/fs.h>
eddcd976 18#include <linux/file.h>
1da177e4 19#include <linux/pagemap.h>
eddcd976
NP
20#include <linux/pfn_t.h>
21#include <linux/ramfs.h>
1da177e4
LT
22#include <linux/init.h>
23#include <linux/string.h>
24#include <linux/blkdev.h>
99c18ce5
NP
25#include <linux/mtd/mtd.h>
26#include <linux/mtd/super.h>
74f78fc5 27#include <linux/fs_context.h>
1da177e4 28#include <linux/slab.h>
1da177e4 29#include <linux/vfs.h>
353ab6e9 30#include <linux/mutex.h>
f7f4f4dd 31#include <uapi/linux/cramfs_fs.h>
1508f3eb 32#include <linux/uaccess.h>
1da177e4 33
f7f4f4dd
AV
34#include "internal.h"
35
36/*
37 * cramfs super-block data in memory
38 */
39struct cramfs_sb_info {
40 unsigned long magic;
41 unsigned long size;
42 unsigned long blocks;
43 unsigned long files;
44 unsigned long flags;
99c18ce5
NP
45 void *linear_virt_addr;
46 resource_size_t linear_phys_addr;
47 size_t mtd_point_size;
f7f4f4dd
AV
48};
49
50static inline struct cramfs_sb_info *CRAMFS_SB(struct super_block *sb)
51{
52 return sb->s_fs_info;
53}
54
ee9b6d61 55static const struct super_operations cramfs_ops;
754661f1 56static const struct inode_operations cramfs_dir_inode_operations;
4b6f5d20 57static const struct file_operations cramfs_directory_operations;
eddcd976 58static const struct file_operations cramfs_physmem_fops;
f5e54d6e 59static const struct address_space_operations cramfs_aops;
1da177e4 60
353ab6e9 61static DEFINE_MUTEX(read_mutex);
1da177e4
LT
62
63
6f772fe6 64/* These macros may change in future, to provide better st_ino semantics. */
1da177e4
LT
65#define OFFSET(x) ((x)->i_ino)
66
0577d1ba 67static unsigned long cramino(const struct cramfs_inode *cino, unsigned int offset)
a97c9bf3 68{
6f772fe6
SS
69 if (!cino->offset)
70 return offset + 1;
71 if (!cino->size)
72 return offset + 1;
73
74 /*
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.
78 */
79 switch (cino->mode & S_IFMT) {
80 case S_IFREG:
81 case S_IFDIR:
82 case S_IFLNK:
83 return cino->offset << 2;
84 default:
85 break;
86 }
87 return offset + 1;
88}
89
90static struct inode *get_cramfs_inode(struct super_block *sb,
0577d1ba 91 const struct cramfs_inode *cramfs_inode, unsigned int offset)
6f772fe6
SS
92{
93 struct inode *inode;
95582b00 94 static struct timespec64 zerotime;
6f772fe6
SS
95
96 inode = iget_locked(sb, cramino(cramfs_inode, offset));
97 if (!inode)
98 return ERR_PTR(-ENOMEM);
99 if (!(inode->i_state & I_NEW))
100 return inode;
101
102 switch (cramfs_inode->mode & S_IFMT) {
103 case S_IFREG:
104 inode->i_fop = &generic_ro_fops;
105 inode->i_data.a_ops = &cramfs_aops;
eddcd976
NP
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;
6f772fe6
SS
110 break;
111 case S_IFDIR:
112 inode->i_op = &cramfs_dir_inode_operations;
113 inode->i_fop = &cramfs_directory_operations;
114 break;
115 case S_IFLNK:
116 inode->i_op = &page_symlink_inode_operations;
21fc61c7 117 inode_nohighmem(inode);
6f772fe6
SS
118 inode->i_data.a_ops = &cramfs_aops;
119 break;
120 default:
121 init_special_inode(inode, cramfs_inode->mode,
122 old_decode_dev(cramfs_inode->size));
123 }
124
77b8a75f 125 inode->i_mode = cramfs_inode->mode;
a7d9cfe9
EB
126 i_uid_write(inode, cramfs_inode->uid);
127 i_gid_write(inode, cramfs_inode->gid);
6f772fe6
SS
128
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;
133 }
134
77b8a75f
AV
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. */
a97c9bf3 141
6f772fe6
SS
142 unlock_new_inode(inode);
143
1da177e4
LT
144 return inode;
145}
146
147/*
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..
152 *
153 * This also acts as a way to guarantee contiguous areas of up to
ea1754a0 154 * BLKS_PER_BUF*PAGE_SIZE, so that the caller doesn't need to
1da177e4
LT
155 * worry about end-of-buffer issues even when decompressing a full
156 * page cache.
99c18ce5
NP
157 *
158 * Note: This is all optimized away at compile time when
159 * CONFIG_CRAMFS_BLOCKDEV=n.
1da177e4
LT
160 */
161#define READ_BUFFERS (2)
162/* NEXT_BUFFER(): Loop over [0..(READ_BUFFERS-1)]. */
163#define NEXT_BUFFER(_ix) ((_ix) ^ 1)
164
165/*
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
168 * alignment.
169 */
170#define BLKS_PER_BUF_SHIFT (2)
171#define BLKS_PER_BUF (1 << BLKS_PER_BUF_SHIFT)
09cbfeaf 172#define BUFFER_SIZE (BLKS_PER_BUF*PAGE_SIZE)
1da177e4
LT
173
174static unsigned char read_buffers[READ_BUFFERS][BUFFER_SIZE];
175static unsigned buffer_blocknr[READ_BUFFERS];
31d92e55 176static struct super_block *buffer_dev[READ_BUFFERS];
1da177e4
LT
177static int next_buffer;
178
179/*
99c18ce5 180 * Populate our block cache and return a pointer to it.
1da177e4 181 */
99c18ce5
NP
182static void *cramfs_blkdev_read(struct super_block *sb, unsigned int offset,
183 unsigned int len)
1da177e4
LT
184{
185 struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
3e351026 186 struct file_ra_state ra = {};
1da177e4 187 struct page *pages[BLKS_PER_BUF];
6bbfb077 188 unsigned i, blocknr, buffer;
1da177e4
LT
189 unsigned long devsize;
190 char *data;
191
192 if (!len)
193 return NULL;
09cbfeaf
KS
194 blocknr = offset >> PAGE_SHIFT;
195 offset &= PAGE_SIZE - 1;
1da177e4
LT
196
197 /* Check if an existing buffer already has the data.. */
198 for (i = 0; i < READ_BUFFERS; i++) {
199 unsigned int blk_offset;
200
201 if (buffer_dev[i] != sb)
202 continue;
203 if (blocknr < buffer_blocknr[i])
204 continue;
09cbfeaf 205 blk_offset = (blocknr - buffer_blocknr[i]) << PAGE_SHIFT;
1da177e4 206 blk_offset += offset;
672ca9dd
NP
207 if (blk_offset > BUFFER_SIZE ||
208 blk_offset + len > BUFFER_SIZE)
1da177e4
LT
209 continue;
210 return read_buffers[i] + blk_offset;
211 }
212
5816e91e 213 devsize = bdev_nr_bytes(sb->s_bdev) >> PAGE_SHIFT;
1da177e4
LT
214
215 /* Ok, read in BLKS_PER_BUF pages completely first. */
cf948cbc
MWO
216 file_ra_state_init(&ra, mapping);
217 page_cache_sync_readahead(mapping, &ra, NULL, blocknr, BLKS_PER_BUF);
218
1da177e4
LT
219 for (i = 0; i < BLKS_PER_BUF; i++) {
220 struct page *page = NULL;
221
222 if (blocknr + i < devsize) {
67f9fd91 223 page = read_mapping_page(mapping, blocknr + i, NULL);
1da177e4
LT
224 /* synchronous error? */
225 if (IS_ERR(page))
226 page = NULL;
227 }
228 pages[i] = page;
229 }
230
1da177e4
LT
231 buffer = next_buffer;
232 next_buffer = NEXT_BUFFER(buffer);
233 buffer_blocknr[buffer] = blocknr;
234 buffer_dev[buffer] = sb;
235
236 data = read_buffers[buffer];
237 for (i = 0; i < BLKS_PER_BUF; i++) {
238 struct page *page = pages[i];
31d92e55 239
1da177e4 240 if (page) {
f27c942e 241 memcpy_from_page(data, page, 0, PAGE_SIZE);
09cbfeaf 242 put_page(page);
1da177e4 243 } else
09cbfeaf
KS
244 memset(data, 0, PAGE_SIZE);
245 data += PAGE_SIZE;
1da177e4
LT
246 }
247 return read_buffers[buffer] + offset;
248}
249
99c18ce5
NP
250/*
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
eddcd976
NP
282/*
283 * For a mapping to be possible, we need a range of uncompressed and
284 * contiguous blocks. Return the offset for the first block and number of
285 * valid blocks for which that is true, or zero otherwise.
286 */
287static u32 cramfs_get_block_range(struct inode *inode, u32 pgoff, u32 *pages)
288{
289 struct cramfs_sb_info *sbi = CRAMFS_SB(inode->i_sb);
290 int i;
291 u32 *blockptrs, first_block_addr;
292
293 /*
294 * We can dereference memory directly here as this code may be
295 * reached only when there is a direct filesystem image mapping
296 * available in memory.
297 */
298 blockptrs = (u32 *)(sbi->linear_virt_addr + OFFSET(inode) + pgoff * 4);
299 first_block_addr = blockptrs[0] & ~CRAMFS_BLK_FLAGS;
300 i = 0;
301 do {
302 u32 block_off = i * (PAGE_SIZE >> CRAMFS_BLK_DIRECT_PTR_SHIFT);
303 u32 expect = (first_block_addr + block_off) |
304 CRAMFS_BLK_FLAG_DIRECT_PTR |
305 CRAMFS_BLK_FLAG_UNCOMPRESSED;
306 if (blockptrs[i] != expect) {
307 pr_debug("range: block %d/%d got %#x expects %#x\n",
308 pgoff+i, pgoff + *pages - 1,
309 blockptrs[i], expect);
310 if (i == 0)
311 return 0;
312 break;
313 }
314 } while (++i < *pages);
315
316 *pages = i;
317 return first_block_addr << CRAMFS_BLK_DIRECT_PTR_SHIFT;
318}
319
320#ifdef CONFIG_MMU
321
322/*
323 * Return true if the last page of a file in the filesystem image contains
324 * some other data that doesn't belong to that file. It is assumed that the
325 * last block is CRAMFS_BLK_FLAG_DIRECT_PTR | CRAMFS_BLK_FLAG_UNCOMPRESSED
326 * (verified by cramfs_get_block_range() and directly accessible in memory.
327 */
328static bool cramfs_last_page_is_shared(struct inode *inode)
329{
330 struct cramfs_sb_info *sbi = CRAMFS_SB(inode->i_sb);
331 u32 partial, last_page, blockaddr, *blockptrs;
332 char *tail_data;
333
334 partial = offset_in_page(inode->i_size);
335 if (!partial)
336 return false;
337 last_page = inode->i_size >> PAGE_SHIFT;
338 blockptrs = (u32 *)(sbi->linear_virt_addr + OFFSET(inode));
339 blockaddr = blockptrs[last_page] & ~CRAMFS_BLK_FLAGS;
340 blockaddr <<= CRAMFS_BLK_DIRECT_PTR_SHIFT;
341 tail_data = sbi->linear_virt_addr + blockaddr + partial;
342 return memchr_inv(tail_data, 0, PAGE_SIZE - partial) ? true : false;
343}
344
345static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma)
346{
347 struct inode *inode = file_inode(file);
348 struct cramfs_sb_info *sbi = CRAMFS_SB(inode->i_sb);
349 unsigned int pages, max_pages, offset;
350 unsigned long address, pgoff = vma->vm_pgoff;
351 char *bailout_reason;
352 int ret;
353
354 ret = generic_file_readonly_mmap(file, vma);
355 if (ret)
356 return ret;
357
358 /*
359 * Now try to pre-populate ptes for this vma with a direct
360 * mapping avoiding memory allocation when possible.
361 */
362
363 /* Could COW work here? */
364 bailout_reason = "vma is writable";
365 if (vma->vm_flags & VM_WRITE)
366 goto bailout;
367
368 max_pages = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
369 bailout_reason = "beyond file limit";
370 if (pgoff >= max_pages)
371 goto bailout;
372 pages = min(vma_pages(vma), max_pages - pgoff);
373
374 offset = cramfs_get_block_range(inode, pgoff, &pages);
375 bailout_reason = "unsuitable block layout";
376 if (!offset)
377 goto bailout;
378 address = sbi->linear_phys_addr + offset;
379 bailout_reason = "data is not page aligned";
380 if (!PAGE_ALIGNED(address))
381 goto bailout;
382
383 /* Don't map the last page if it contains some other data */
384 if (pgoff + pages == max_pages && cramfs_last_page_is_shared(inode)) {
aa7d5c7e 385 pr_debug("mmap: %pD: last page is shared\n", file);
eddcd976
NP
386 pages--;
387 }
388
389 if (!pages) {
390 bailout_reason = "no suitable block remaining";
391 goto bailout;
392 }
393
394 if (pages == vma_pages(vma)) {
395 /*
396 * The entire vma is mappable. remap_pfn_range() will
397 * make it distinguishable from a non-direct mapping
398 * in /proc/<pid>/maps by substituting the file offset
399 * with the actual physical address.
400 */
401 ret = remap_pfn_range(vma, vma->vm_start, address >> PAGE_SHIFT,
402 pages * PAGE_SIZE, vma->vm_page_prot);
403 } else {
404 /*
405 * Let's create a mixed map if we can't map it all.
406 * The normal paging machinery will take care of the
5aab331a 407 * unpopulated ptes via cramfs_read_folio().
eddcd976
NP
408 */
409 int i;
1c71222e 410 vm_flags_set(vma, VM_MIXEDMAP);
eddcd976 411 for (i = 0; i < pages && !ret; i++) {
7f2764cf 412 vm_fault_t vmf;
eddcd976
NP
413 unsigned long off = i * PAGE_SIZE;
414 pfn_t pfn = phys_to_pfn_t(address + off, PFN_DEV);
7f2764cf
NP
415 vmf = vmf_insert_mixed(vma, vma->vm_start + off, pfn);
416 if (vmf & VM_FAULT_ERROR)
417 ret = vm_fault_to_errno(vmf, 0);
eddcd976
NP
418 }
419 }
420
421 if (!ret)
aa7d5c7e
AV
422 pr_debug("mapped %pD[%lu] at 0x%08lx (%u/%lu pages) "
423 "to vma 0x%08lx, page_prot 0x%llx\n", file,
424 pgoff, address, pages, vma_pages(vma), vma->vm_start,
eddcd976
NP
425 (unsigned long long)pgprot_val(vma->vm_page_prot));
426 return ret;
427
428bailout:
aa7d5c7e
AV
429 pr_debug("%pD[%lu]: direct mmap impossible: %s\n",
430 file, pgoff, bailout_reason);
eddcd976
NP
431 /* Didn't manage any direct map, but normal paging is still possible */
432 return 0;
433}
434
435#else /* CONFIG_MMU */
436
437static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma)
438{
fc4f4be9 439 return is_nommu_shared_mapping(vma->vm_flags) ? 0 : -ENOSYS;
eddcd976
NP
440}
441
442static unsigned long cramfs_physmem_get_unmapped_area(struct file *file,
443 unsigned long addr, unsigned long len,
444 unsigned long pgoff, unsigned long flags)
445{
446 struct inode *inode = file_inode(file);
447 struct super_block *sb = inode->i_sb;
448 struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
449 unsigned int pages, block_pages, max_pages, offset;
450
451 pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
452 max_pages = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
453 if (pgoff >= max_pages || pages > max_pages - pgoff)
454 return -EINVAL;
455 block_pages = pages;
456 offset = cramfs_get_block_range(inode, pgoff, &block_pages);
457 if (!offset || block_pages != pages)
458 return -ENOSYS;
459 addr = sbi->linear_phys_addr + offset;
aa7d5c7e
AV
460 pr_debug("get_unmapped for %pD ofs %#lx siz %lu at 0x%08lx\n",
461 file, pgoff*PAGE_SIZE, len, addr);
eddcd976
NP
462 return addr;
463}
464
465static unsigned int cramfs_physmem_mmap_capabilities(struct file *file)
466{
467 return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT |
468 NOMMU_MAP_READ | NOMMU_MAP_EXEC;
469}
470
471#endif /* CONFIG_MMU */
472
473static const struct file_operations cramfs_physmem_fops = {
474 .llseek = generic_file_llseek,
475 .read_iter = generic_file_read_iter,
476 .splice_read = generic_file_splice_read,
477 .mmap = cramfs_physmem_mmap,
478#ifndef CONFIG_MMU
479 .get_unmapped_area = cramfs_physmem_get_unmapped_area,
480 .mmap_capabilities = cramfs_physmem_mmap_capabilities,
481#endif
482};
483
2309fb8e 484static void cramfs_kill_sb(struct super_block *sb)
1da177e4 485{
f7f4f4dd 486 struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
31d92e55 487
08a8f308 488 if (IS_ENABLED(CONFIG_CRAMFS_MTD) && sb->s_mtd) {
99c18ce5
NP
489 if (sbi && sbi->mtd_point_size)
490 mtd_unpoint(sb->s_mtd, 0, sbi->mtd_point_size);
491 kill_mtd_super(sb);
492 } else if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV) && sb->s_bdev) {
493 kill_block_super(sb);
494 }
2309fb8e 495 kfree(sbi);
1da177e4
LT
496}
497
74f78fc5 498static int cramfs_reconfigure(struct fs_context *fc)
1da177e4 499{
74f78fc5
DH
500 sync_filesystem(fc->root->d_sb);
501 fc->sb_flags |= SB_RDONLY;
1da177e4
LT
502 return 0;
503}
504
74f78fc5
DH
505static int cramfs_read_super(struct super_block *sb, struct fs_context *fc,
506 struct cramfs_super *super)
1da177e4 507{
99c18ce5 508 struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
1da177e4 509 unsigned long root_offset;
74f78fc5 510 bool silent = fc->sb_flags & SB_SILENT;
1da177e4 511
99c18ce5
NP
512 /* We don't know the real size yet */
513 sbi->size = PAGE_SIZE;
1da177e4
LT
514
515 /* Read the first block and get the superblock from it */
99c18ce5
NP
516 mutex_lock(&read_mutex);
517 memcpy(super, cramfs_read(sb, 0, sizeof(*super)), sizeof(*super));
353ab6e9 518 mutex_unlock(&read_mutex);
1da177e4
LT
519
520 /* Do sanity checks on the superblock */
99c18ce5 521 if (super->magic != CRAMFS_MAGIC) {
0cc785ec 522 /* check for wrong endianness */
99c18ce5 523 if (super->magic == CRAMFS_MAGIC_WEND) {
ac8d35c5 524 if (!silent)
e1ee7d85 525 errorfc(fc, "wrong endianness");
2309fb8e 526 return -EINVAL;
ac8d35c5
AD
527 }
528
1da177e4 529 /* check at 512 byte offset */
353ab6e9 530 mutex_lock(&read_mutex);
99c18ce5
NP
531 memcpy(super,
532 cramfs_read(sb, 512, sizeof(*super)),
533 sizeof(*super));
353ab6e9 534 mutex_unlock(&read_mutex);
99c18ce5
NP
535 if (super->magic != CRAMFS_MAGIC) {
536 if (super->magic == CRAMFS_MAGIC_WEND && !silent)
e1ee7d85 537 errorfc(fc, "wrong endianness");
ac8d35c5 538 else if (!silent)
e1ee7d85 539 errorfc(fc, "wrong magic");
2309fb8e 540 return -EINVAL;
1da177e4
LT
541 }
542 }
543
544 /* get feature flags first */
99c18ce5 545 if (super->flags & ~CRAMFS_SUPPORTED_FLAGS) {
e1ee7d85 546 errorfc(fc, "unsupported filesystem features");
2309fb8e 547 return -EINVAL;
1da177e4
LT
548 }
549
550 /* Check that the root inode is in a sane state */
99c18ce5 551 if (!S_ISDIR(super->root.mode)) {
e1ee7d85 552 errorfc(fc, "root is not a directory");
2309fb8e 553 return -EINVAL;
1da177e4 554 }
6f772fe6 555 /* correct strange, hard-coded permissions of mkcramfs */
99c18ce5 556 super->root.mode |= 0555;
6f772fe6 557
99c18ce5
NP
558 root_offset = super->root.offset << 2;
559 if (super->flags & CRAMFS_FLAG_FSID_VERSION_2) {
560 sbi->size = super->size;
561 sbi->blocks = super->fsid.blocks;
562 sbi->files = super->fsid.files;
1da177e4 563 } else {
31d92e55
FF
564 sbi->size = 1<<28;
565 sbi->blocks = 0;
566 sbi->files = 0;
1da177e4 567 }
99c18ce5
NP
568 sbi->magic = super->magic;
569 sbi->flags = super->flags;
1da177e4 570 if (root_offset == 0)
e1ee7d85 571 infofc(fc, "empty filesystem");
99c18ce5 572 else if (!(super->flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
1da177e4
LT
573 ((root_offset != sizeof(struct cramfs_super)) &&
574 (root_offset != 512 + sizeof(struct cramfs_super))))
575 {
e1ee7d85 576 errorfc(fc, "bad root offset %lu", root_offset);
2309fb8e 577 return -EINVAL;
1da177e4
LT
578 }
579
99c18ce5
NP
580 return 0;
581}
582
583static int cramfs_finalize_super(struct super_block *sb,
584 struct cramfs_inode *cramfs_root)
585{
586 struct inode *root;
587
1da177e4 588 /* Set it all up.. */
1751e8a6 589 sb->s_flags |= SB_RDONLY;
22b13969
DD
590 sb->s_time_min = 0;
591 sb->s_time_max = 0;
1da177e4 592 sb->s_op = &cramfs_ops;
99c18ce5 593 root = get_cramfs_inode(sb, cramfs_root, 0);
0577d1ba 594 if (IS_ERR(root))
2309fb8e 595 return PTR_ERR(root);
48fde701
AV
596 sb->s_root = d_make_root(root);
597 if (!sb->s_root)
2309fb8e 598 return -ENOMEM;
1da177e4 599 return 0;
1da177e4
LT
600}
601
74f78fc5 602static int cramfs_blkdev_fill_super(struct super_block *sb, struct fs_context *fc)
99c18ce5
NP
603{
604 struct cramfs_sb_info *sbi;
605 struct cramfs_super super;
606 int i, err;
607
608 sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
609 if (!sbi)
610 return -ENOMEM;
611 sb->s_fs_info = sbi;
612
613 /* Invalidate the read buffers on mount: think disk change.. */
614 for (i = 0; i < READ_BUFFERS; i++)
615 buffer_blocknr[i] = -1;
616
74f78fc5 617 err = cramfs_read_super(sb, fc, &super);
99c18ce5
NP
618 if (err)
619 return err;
620 return cramfs_finalize_super(sb, &super.root);
621}
622
74f78fc5 623static int cramfs_mtd_fill_super(struct super_block *sb, struct fs_context *fc)
99c18ce5
NP
624{
625 struct cramfs_sb_info *sbi;
626 struct cramfs_super super;
627 int err;
628
629 sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
630 if (!sbi)
631 return -ENOMEM;
632 sb->s_fs_info = sbi;
633
634 /* Map only one page for now. Will remap it when fs size is known. */
635 err = mtd_point(sb->s_mtd, 0, PAGE_SIZE, &sbi->mtd_point_size,
636 &sbi->linear_virt_addr, &sbi->linear_phys_addr);
637 if (err || sbi->mtd_point_size != PAGE_SIZE) {
638 pr_err("unable to get direct memory access to mtd:%s\n",
639 sb->s_mtd->name);
640 return err ? : -ENODATA;
641 }
642
643 pr_info("checking physical address %pap for linear cramfs image\n",
644 &sbi->linear_phys_addr);
74f78fc5 645 err = cramfs_read_super(sb, fc, &super);
99c18ce5
NP
646 if (err)
647 return err;
648
649 /* Remap the whole filesystem now */
650 pr_info("linear cramfs image on mtd:%s appears to be %lu KB in size\n",
651 sb->s_mtd->name, sbi->size/1024);
652 mtd_unpoint(sb->s_mtd, 0, PAGE_SIZE);
653 err = mtd_point(sb->s_mtd, 0, sbi->size, &sbi->mtd_point_size,
654 &sbi->linear_virt_addr, &sbi->linear_phys_addr);
655 if (err || sbi->mtd_point_size != sbi->size) {
656 pr_err("unable to get direct memory access to mtd:%s\n",
657 sb->s_mtd->name);
658 return err ? : -ENODATA;
659 }
660
661 return cramfs_finalize_super(sb, &super.root);
662}
663
726c3342 664static int cramfs_statfs(struct dentry *dentry, struct kstatfs *buf)
1da177e4 665{
726c3342 666 struct super_block *sb = dentry->d_sb;
99c18ce5
NP
667 u64 id = 0;
668
669 if (sb->s_bdev)
670 id = huge_encode_dev(sb->s_bdev->bd_dev);
671 else if (sb->s_dev)
672 id = huge_encode_dev(sb->s_dev);
726c3342 673
1da177e4 674 buf->f_type = CRAMFS_MAGIC;
09cbfeaf 675 buf->f_bsize = PAGE_SIZE;
1da177e4
LT
676 buf->f_blocks = CRAMFS_SB(sb)->blocks;
677 buf->f_bfree = 0;
678 buf->f_bavail = 0;
679 buf->f_files = CRAMFS_SB(sb)->files;
680 buf->f_ffree = 0;
6d1349c7 681 buf->f_fsid = u64_to_fsid(id);
1da177e4
LT
682 buf->f_namelen = CRAMFS_MAXPATHLEN;
683 return 0;
684}
685
686/*
687 * Read a cramfs directory entry.
688 */
6f7f231e 689static int cramfs_readdir(struct file *file, struct dir_context *ctx)
1da177e4 690{
6f7f231e 691 struct inode *inode = file_inode(file);
1da177e4
LT
692 struct super_block *sb = inode->i_sb;
693 char *buf;
694 unsigned int offset;
1da177e4
LT
695
696 /* Offset within the thing. */
6f7f231e 697 if (ctx->pos >= inode->i_size)
1da177e4 698 return 0;
6f7f231e 699 offset = ctx->pos;
1da177e4
LT
700 /* Directory entries are always 4-byte aligned */
701 if (offset & 3)
702 return -EINVAL;
703
4176ed59 704 buf = kmalloc(CRAMFS_MAXPATHLEN, GFP_KERNEL);
1da177e4
LT
705 if (!buf)
706 return -ENOMEM;
707
1da177e4
LT
708 while (offset < inode->i_size) {
709 struct cramfs_inode *de;
710 unsigned long nextoffset;
711 char *name;
712 ino_t ino;
175a4eb7 713 umode_t mode;
6f7f231e 714 int namelen;
1da177e4 715
353ab6e9 716 mutex_lock(&read_mutex);
4176ed59 717 de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+CRAMFS_MAXPATHLEN);
1da177e4
LT
718 name = (char *)(de+1);
719
720 /*
721 * Namelengths on disk are shifted by two
722 * and the name padded out to 4-byte boundaries
723 * with zeroes.
724 */
725 namelen = de->namelen << 2;
726 memcpy(buf, name, namelen);
6f772fe6 727 ino = cramino(de, OFFSET(inode) + offset);
1da177e4 728 mode = de->mode;
353ab6e9 729 mutex_unlock(&read_mutex);
1da177e4
LT
730 nextoffset = offset + sizeof(*de) + namelen;
731 for (;;) {
732 if (!namelen) {
733 kfree(buf);
734 return -EIO;
735 }
736 if (buf[namelen-1])
737 break;
738 namelen--;
739 }
6f7f231e 740 if (!dir_emit(ctx, buf, namelen, ino, mode >> 12))
1da177e4
LT
741 break;
742
6f7f231e 743 ctx->pos = offset = nextoffset;
1da177e4
LT
744 }
745 kfree(buf);
746 return 0;
747}
748
749/*
750 * Lookup and fill in the inode data..
751 */
31d92e55 752static struct dentry *cramfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1da177e4
LT
753{
754 unsigned int offset = 0;
0577d1ba 755 struct inode *inode = NULL;
1da177e4
LT
756 int sorted;
757
353ab6e9 758 mutex_lock(&read_mutex);
1da177e4
LT
759 sorted = CRAMFS_SB(dir->i_sb)->flags & CRAMFS_FLAG_SORTED_DIRS;
760 while (offset < dir->i_size) {
761 struct cramfs_inode *de;
762 char *name;
763 int namelen, retval;
6f772fe6 764 int dir_off = OFFSET(dir) + offset;
1da177e4 765
6f772fe6 766 de = cramfs_read(dir->i_sb, dir_off, sizeof(*de)+CRAMFS_MAXPATHLEN);
1da177e4
LT
767 name = (char *)(de+1);
768
769 /* Try to take advantage of sorted directories */
770 if (sorted && (dentry->d_name.name[0] < name[0]))
771 break;
772
773 namelen = de->namelen << 2;
774 offset += sizeof(*de) + namelen;
775
776 /* Quick check that the name is roughly the right length */
777 if (((dentry->d_name.len + 3) & ~3) != namelen)
778 continue;
779
780 for (;;) {
781 if (!namelen) {
0577d1ba
AV
782 inode = ERR_PTR(-EIO);
783 goto out;
1da177e4
LT
784 }
785 if (name[namelen-1])
786 break;
787 namelen--;
788 }
789 if (namelen != dentry->d_name.len)
790 continue;
791 retval = memcmp(dentry->d_name.name, name, namelen);
792 if (retval > 0)
793 continue;
794 if (!retval) {
0577d1ba
AV
795 inode = get_cramfs_inode(dir->i_sb, de, dir_off);
796 break;
1da177e4
LT
797 }
798 /* else (retval < 0) */
799 if (sorted)
800 break;
801 }
0577d1ba 802out:
353ab6e9 803 mutex_unlock(&read_mutex);
d023b3a1 804 return d_splice_alias(inode, dentry);
1da177e4
LT
805}
806
5aab331a 807static int cramfs_read_folio(struct file *file, struct folio *folio)
1da177e4 808{
5aab331a 809 struct page *page = &folio->page;
1da177e4 810 struct inode *inode = page->mapping->host;
98310e58
DV
811 u32 maxblock;
812 int bytes_filled;
1da177e4
LT
813 void *pgdata;
814
09cbfeaf 815 maxblock = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1da177e4 816 bytes_filled = 0;
f27c942e 817 pgdata = kmap_local_page(page);
98310e58 818
1da177e4
LT
819 if (page->index < maxblock) {
820 struct super_block *sb = inode->i_sb;
fd4f6f2a
NP
821 u32 blkptr_offset = OFFSET(inode) + page->index * 4;
822 u32 block_ptr, block_start, block_len;
823 bool uncompressed, direct;
1da177e4 824
353ab6e9 825 mutex_lock(&read_mutex);
fd4f6f2a
NP
826 block_ptr = *(u32 *) cramfs_read(sb, blkptr_offset, 4);
827 uncompressed = (block_ptr & CRAMFS_BLK_FLAG_UNCOMPRESSED);
828 direct = (block_ptr & CRAMFS_BLK_FLAG_DIRECT_PTR);
829 block_ptr &= ~CRAMFS_BLK_FLAGS;
830
831 if (direct) {
832 /*
833 * The block pointer is an absolute start pointer,
834 * shifted by 2 bits. The size is included in the
835 * first 2 bytes of the data block when compressed,
836 * or PAGE_SIZE otherwise.
837 */
838 block_start = block_ptr << CRAMFS_BLK_DIRECT_PTR_SHIFT;
839 if (uncompressed) {
840 block_len = PAGE_SIZE;
841 /* if last block: cap to file length */
842 if (page->index == maxblock - 1)
843 block_len =
844 offset_in_page(inode->i_size);
845 } else {
846 block_len = *(u16 *)
847 cramfs_read(sb, block_start, 2);
848 block_start += 2;
849 }
850 } else {
851 /*
852 * The block pointer indicates one past the end of
853 * the current block (start of next block). If this
854 * is the first block then it starts where the block
855 * pointer table ends, otherwise its start comes
856 * from the previous block's pointer.
857 */
858 block_start = OFFSET(inode) + maxblock * 4;
859 if (page->index)
860 block_start = *(u32 *)
861 cramfs_read(sb, blkptr_offset - 4, 4);
862 /* Beware... previous ptr might be a direct ptr */
863 if (unlikely(block_start & CRAMFS_BLK_FLAG_DIRECT_PTR)) {
864 /* See comments on earlier code. */
865 u32 prev_start = block_start;
56ce68bc
NP
866 block_start = prev_start & ~CRAMFS_BLK_FLAGS;
867 block_start <<= CRAMFS_BLK_DIRECT_PTR_SHIFT;
fd4f6f2a
NP
868 if (prev_start & CRAMFS_BLK_FLAG_UNCOMPRESSED) {
869 block_start += PAGE_SIZE;
870 } else {
871 block_len = *(u16 *)
872 cramfs_read(sb, block_start, 2);
873 block_start += 2 + block_len;
874 }
875 }
876 block_start &= ~CRAMFS_BLK_FLAGS;
877 block_len = block_ptr - block_start;
878 }
98310e58 879
fd4f6f2a 880 if (block_len == 0)
1da177e4 881 ; /* hole */
fd4f6f2a
NP
882 else if (unlikely(block_len > 2*PAGE_SIZE ||
883 (uncompressed && block_len > PAGE_SIZE))) {
884 mutex_unlock(&read_mutex);
885 pr_err("bad data blocksize %u\n", block_len);
98310e58 886 goto err;
fd4f6f2a
NP
887 } else if (uncompressed) {
888 memcpy(pgdata,
889 cramfs_read(sb, block_start, block_len),
890 block_len);
891 bytes_filled = block_len;
98310e58 892 } else {
1da177e4 893 bytes_filled = cramfs_uncompress_block(pgdata,
09cbfeaf 894 PAGE_SIZE,
fd4f6f2a
NP
895 cramfs_read(sb, block_start, block_len),
896 block_len);
1da177e4 897 }
fd4f6f2a
NP
898 mutex_unlock(&read_mutex);
899 if (unlikely(bytes_filled < 0))
900 goto err;
98310e58
DV
901 }
902
09cbfeaf 903 memset(pgdata + bytes_filled, 0, PAGE_SIZE - bytes_filled);
1da177e4 904 flush_dcache_page(page);
f27c942e 905 kunmap_local(pgdata);
1da177e4
LT
906 SetPageUptodate(page);
907 unlock_page(page);
908 return 0;
98310e58
DV
909
910err:
f27c942e 911 kunmap_local(pgdata);
98310e58
DV
912 ClearPageUptodate(page);
913 SetPageError(page);
914 unlock_page(page);
915 return 0;
1da177e4
LT
916}
917
f5e54d6e 918static const struct address_space_operations cramfs_aops = {
5aab331a 919 .read_folio = cramfs_read_folio
1da177e4
LT
920};
921
922/*
923 * Our operations:
924 */
925
926/*
927 * A directory can only readdir
928 */
4b6f5d20 929static const struct file_operations cramfs_directory_operations = {
1da177e4
LT
930 .llseek = generic_file_llseek,
931 .read = generic_read_dir,
c51da20c 932 .iterate_shared = cramfs_readdir,
1da177e4
LT
933};
934
754661f1 935static const struct inode_operations cramfs_dir_inode_operations = {
1da177e4
LT
936 .lookup = cramfs_lookup,
937};
938
ee9b6d61 939static const struct super_operations cramfs_ops = {
1da177e4
LT
940 .statfs = cramfs_statfs,
941};
942
74f78fc5 943static int cramfs_get_tree(struct fs_context *fc)
1da177e4 944{
74f78fc5 945 int ret = -ENOPROTOOPT;
99c18ce5
NP
946
947 if (IS_ENABLED(CONFIG_CRAMFS_MTD)) {
74f78fc5 948 ret = get_tree_mtd(fc, cramfs_mtd_fill_super);
3e5aeec0
MB
949 if (!ret)
950 return 0;
99c18ce5 951 }
74f78fc5
DH
952 if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV))
953 ret = get_tree_bdev(fc, cramfs_blkdev_fill_super);
99c18ce5 954 return ret;
1da177e4
LT
955}
956
74f78fc5
DH
957static const struct fs_context_operations cramfs_context_ops = {
958 .get_tree = cramfs_get_tree,
959 .reconfigure = cramfs_reconfigure,
960};
961
962/*
963 * Set up the filesystem mount context.
964 */
965static int cramfs_init_fs_context(struct fs_context *fc)
966{
967 fc->ops = &cramfs_context_ops;
968 return 0;
969}
970
1da177e4
LT
971static struct file_system_type cramfs_fs_type = {
972 .owner = THIS_MODULE,
973 .name = "cramfs",
74f78fc5 974 .init_fs_context = cramfs_init_fs_context,
2309fb8e 975 .kill_sb = cramfs_kill_sb,
1da177e4
LT
976 .fs_flags = FS_REQUIRES_DEV,
977};
7f78e035 978MODULE_ALIAS_FS("cramfs");
1da177e4
LT
979
980static int __init init_cramfs_fs(void)
981{
50d44ed0
AD
982 int rv;
983
984 rv = cramfs_uncompress_init();
985 if (rv < 0)
986 return rv;
987 rv = register_filesystem(&cramfs_fs_type);
988 if (rv < 0)
989 cramfs_uncompress_exit();
990 return rv;
1da177e4
LT
991}
992
993static void __exit exit_cramfs_fs(void)
994{
995 cramfs_uncompress_exit();
996 unregister_filesystem(&cramfs_fs_type);
997}
998
999module_init(init_cramfs_fs)
1000module_exit(exit_cramfs_fs)
1001MODULE_LICENSE("GPL");