Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/fs/cramfs/inode.c
26282 views
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
14
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16
#include <linux/module.h>
17
#include <linux/fs.h>
18
#include <linux/file.h>
19
#include <linux/pagemap.h>
20
#include <linux/ramfs.h>
21
#include <linux/init.h>
22
#include <linux/string.h>
23
#include <linux/blkdev.h>
24
#include <linux/mtd/mtd.h>
25
#include <linux/mtd/super.h>
26
#include <linux/fs_context.h>
27
#include <linux/slab.h>
28
#include <linux/vfs.h>
29
#include <linux/mutex.h>
30
#include <uapi/linux/cramfs_fs.h>
31
#include <linux/uaccess.h>
32
33
#include "internal.h"
34
35
/*
36
* cramfs super-block data in memory
37
*/
38
struct cramfs_sb_info {
39
unsigned long magic;
40
unsigned long size;
41
unsigned long blocks;
42
unsigned long files;
43
unsigned long flags;
44
void *linear_virt_addr;
45
resource_size_t linear_phys_addr;
46
size_t mtd_point_size;
47
};
48
49
static inline struct cramfs_sb_info *CRAMFS_SB(struct super_block *sb)
50
{
51
return sb->s_fs_info;
52
}
53
54
static const struct super_operations cramfs_ops;
55
static const struct inode_operations cramfs_dir_inode_operations;
56
static const struct file_operations cramfs_directory_operations;
57
static const struct file_operations cramfs_physmem_fops;
58
static const struct address_space_operations cramfs_aops;
59
60
static DEFINE_MUTEX(read_mutex);
61
62
63
/* These macros may change in future, to provide better st_ino semantics. */
64
#define OFFSET(x) ((x)->i_ino)
65
66
static unsigned long cramino(const struct cramfs_inode *cino, unsigned int offset)
67
{
68
if (!cino->offset)
69
return offset + 1;
70
if (!cino->size)
71
return offset + 1;
72
73
/*
74
* The file mode test fixes buggy mkcramfs implementations where
75
* cramfs_inode->offset is set to a non zero value for entries
76
* which did not contain data, like devices node and fifos.
77
*/
78
switch (cino->mode & S_IFMT) {
79
case S_IFREG:
80
case S_IFDIR:
81
case S_IFLNK:
82
return cino->offset << 2;
83
default:
84
break;
85
}
86
return offset + 1;
87
}
88
89
static struct inode *get_cramfs_inode(struct super_block *sb,
90
const struct cramfs_inode *cramfs_inode, unsigned int offset)
91
{
92
struct inode *inode;
93
static struct timespec64 zerotime;
94
95
inode = iget_locked(sb, cramino(cramfs_inode, offset));
96
if (!inode)
97
return ERR_PTR(-ENOMEM);
98
if (!(inode->i_state & I_NEW))
99
return inode;
100
101
switch (cramfs_inode->mode & S_IFMT) {
102
case S_IFREG:
103
inode->i_fop = &generic_ro_fops;
104
inode->i_data.a_ops = &cramfs_aops;
105
if (IS_ENABLED(CONFIG_CRAMFS_MTD) &&
106
CRAMFS_SB(sb)->flags & CRAMFS_FLAG_EXT_BLOCK_POINTERS &&
107
CRAMFS_SB(sb)->linear_phys_addr)
108
inode->i_fop = &cramfs_physmem_fops;
109
break;
110
case S_IFDIR:
111
inode->i_op = &cramfs_dir_inode_operations;
112
inode->i_fop = &cramfs_directory_operations;
113
break;
114
case S_IFLNK:
115
inode->i_op = &page_symlink_inode_operations;
116
inode_nohighmem(inode);
117
inode->i_data.a_ops = &cramfs_aops;
118
break;
119
default:
120
init_special_inode(inode, cramfs_inode->mode,
121
old_decode_dev(cramfs_inode->size));
122
}
123
124
inode->i_mode = cramfs_inode->mode;
125
i_uid_write(inode, cramfs_inode->uid);
126
i_gid_write(inode, cramfs_inode->gid);
127
128
/* if the lower 2 bits are zero, the inode contains data */
129
if (!(inode->i_ino & 3)) {
130
inode->i_size = cramfs_inode->size;
131
inode->i_blocks = (cramfs_inode->size - 1) / 512 + 1;
132
}
133
134
/* Struct copy intentional */
135
inode_set_mtime_to_ts(inode,
136
inode_set_atime_to_ts(inode, inode_set_ctime_to_ts(inode, 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. */
141
142
unlock_new_inode(inode);
143
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
154
* BLKS_PER_BUF*PAGE_SIZE, so that the caller doesn't need to
155
* worry about end-of-buffer issues even when decompressing a full
156
* page cache.
157
*
158
* Note: This is all optimized away at compile time when
159
* CONFIG_CRAMFS_BLOCKDEV=n.
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)
172
#define BUFFER_SIZE (BLKS_PER_BUF*PAGE_SIZE)
173
174
static unsigned char read_buffers[READ_BUFFERS][BUFFER_SIZE];
175
static unsigned buffer_blocknr[READ_BUFFERS];
176
static struct super_block *buffer_dev[READ_BUFFERS];
177
static int next_buffer;
178
179
/*
180
* Populate our block cache and return a pointer to it.
181
*/
182
static void *cramfs_blkdev_read(struct super_block *sb, unsigned int offset,
183
unsigned int len)
184
{
185
struct address_space *mapping = sb->s_bdev->bd_mapping;
186
struct file_ra_state ra = {};
187
struct page *pages[BLKS_PER_BUF];
188
unsigned i, blocknr, buffer;
189
unsigned long devsize;
190
char *data;
191
192
if (!len)
193
return NULL;
194
blocknr = offset >> PAGE_SHIFT;
195
offset &= PAGE_SIZE - 1;
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;
205
blk_offset = (blocknr - buffer_blocknr[i]) << PAGE_SHIFT;
206
blk_offset += offset;
207
if (blk_offset > BUFFER_SIZE ||
208
blk_offset + len > BUFFER_SIZE)
209
continue;
210
return read_buffers[i] + blk_offset;
211
}
212
213
devsize = bdev_nr_bytes(sb->s_bdev) >> PAGE_SHIFT;
214
215
/* Ok, read in BLKS_PER_BUF pages completely first. */
216
file_ra_state_init(&ra, mapping);
217
page_cache_sync_readahead(mapping, &ra, NULL, blocknr, BLKS_PER_BUF);
218
219
for (i = 0; i < BLKS_PER_BUF; i++) {
220
struct page *page = NULL;
221
222
if (blocknr + i < devsize) {
223
page = read_mapping_page(mapping, blocknr + i, NULL);
224
/* synchronous error? */
225
if (IS_ERR(page))
226
page = NULL;
227
}
228
pages[i] = page;
229
}
230
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];
239
240
if (page) {
241
memcpy_from_page(data, page, 0, PAGE_SIZE);
242
put_page(page);
243
} else
244
memset(data, 0, PAGE_SIZE);
245
data += PAGE_SIZE;
246
}
247
return read_buffers[buffer] + offset;
248
}
249
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
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
*/
287
static 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
*/
328
static 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
345
static 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)) {
385
pr_debug("mmap: %pD: last page is shared\n", file);
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
407
* unpopulated ptes via cramfs_read_folio().
408
*/
409
int i;
410
vm_flags_set(vma, VM_MIXEDMAP);
411
for (i = 0; i < pages && !ret; i++) {
412
vm_fault_t vmf;
413
unsigned long off = i * PAGE_SIZE;
414
vmf = vmf_insert_mixed(vma, vma->vm_start + off,
415
address + off);
416
if (vmf & VM_FAULT_ERROR)
417
ret = vm_fault_to_errno(vmf, 0);
418
}
419
}
420
421
if (!ret)
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,
425
(unsigned long long)pgprot_val(vma->vm_page_prot));
426
return ret;
427
428
bailout:
429
pr_debug("%pD[%lu]: direct mmap impossible: %s\n",
430
file, pgoff, bailout_reason);
431
/* Didn't manage any direct map, but normal paging is still possible */
432
return 0;
433
}
434
435
#else /* CONFIG_MMU */
436
437
static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma)
438
{
439
return is_nommu_shared_mapping(vma->vm_flags) ? 0 : -ENOSYS;
440
}
441
442
static 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;
460
pr_debug("get_unmapped for %pD ofs %#lx siz %lu at 0x%08lx\n",
461
file, pgoff*PAGE_SIZE, len, addr);
462
return addr;
463
}
464
465
static 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
473
static const struct file_operations cramfs_physmem_fops = {
474
.llseek = generic_file_llseek,
475
.read_iter = generic_file_read_iter,
476
.splice_read = filemap_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
484
static void cramfs_kill_sb(struct super_block *sb)
485
{
486
struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
487
488
generic_shutdown_super(sb);
489
490
if (IS_ENABLED(CONFIG_CRAMFS_MTD) && sb->s_mtd) {
491
if (sbi && sbi->mtd_point_size)
492
mtd_unpoint(sb->s_mtd, 0, sbi->mtd_point_size);
493
put_mtd_device(sb->s_mtd);
494
sb->s_mtd = NULL;
495
} else if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV) && sb->s_bdev) {
496
sync_blockdev(sb->s_bdev);
497
bdev_fput(sb->s_bdev_file);
498
}
499
kfree(sbi);
500
}
501
502
static int cramfs_reconfigure(struct fs_context *fc)
503
{
504
sync_filesystem(fc->root->d_sb);
505
fc->sb_flags |= SB_RDONLY;
506
return 0;
507
}
508
509
static int cramfs_read_super(struct super_block *sb, struct fs_context *fc,
510
struct cramfs_super *super)
511
{
512
struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
513
unsigned long root_offset;
514
bool silent = fc->sb_flags & SB_SILENT;
515
516
/* We don't know the real size yet */
517
sbi->size = PAGE_SIZE;
518
519
/* Read the first block and get the superblock from it */
520
mutex_lock(&read_mutex);
521
memcpy(super, cramfs_read(sb, 0, sizeof(*super)), sizeof(*super));
522
mutex_unlock(&read_mutex);
523
524
/* Do sanity checks on the superblock */
525
if (super->magic != CRAMFS_MAGIC) {
526
/* check for wrong endianness */
527
if (super->magic == CRAMFS_MAGIC_WEND) {
528
if (!silent)
529
errorfc(fc, "wrong endianness");
530
return -EINVAL;
531
}
532
533
/* check at 512 byte offset */
534
mutex_lock(&read_mutex);
535
memcpy(super,
536
cramfs_read(sb, 512, sizeof(*super)),
537
sizeof(*super));
538
mutex_unlock(&read_mutex);
539
if (super->magic != CRAMFS_MAGIC) {
540
if (super->magic == CRAMFS_MAGIC_WEND && !silent)
541
errorfc(fc, "wrong endianness");
542
else if (!silent)
543
errorfc(fc, "wrong magic");
544
return -EINVAL;
545
}
546
}
547
548
/* get feature flags first */
549
if (super->flags & ~CRAMFS_SUPPORTED_FLAGS) {
550
errorfc(fc, "unsupported filesystem features");
551
return -EINVAL;
552
}
553
554
/* Check that the root inode is in a sane state */
555
if (!S_ISDIR(super->root.mode)) {
556
errorfc(fc, "root is not a directory");
557
return -EINVAL;
558
}
559
/* correct strange, hard-coded permissions of mkcramfs */
560
super->root.mode |= 0555;
561
562
root_offset = super->root.offset << 2;
563
if (super->flags & CRAMFS_FLAG_FSID_VERSION_2) {
564
sbi->size = super->size;
565
sbi->blocks = super->fsid.blocks;
566
sbi->files = super->fsid.files;
567
} else {
568
sbi->size = 1<<28;
569
sbi->blocks = 0;
570
sbi->files = 0;
571
}
572
sbi->magic = super->magic;
573
sbi->flags = super->flags;
574
if (root_offset == 0)
575
infofc(fc, "empty filesystem");
576
else if (!(super->flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
577
((root_offset != sizeof(struct cramfs_super)) &&
578
(root_offset != 512 + sizeof(struct cramfs_super))))
579
{
580
errorfc(fc, "bad root offset %lu", root_offset);
581
return -EINVAL;
582
}
583
584
return 0;
585
}
586
587
static int cramfs_finalize_super(struct super_block *sb,
588
struct cramfs_inode *cramfs_root)
589
{
590
struct inode *root;
591
592
/* Set it all up.. */
593
sb->s_flags |= SB_RDONLY;
594
sb->s_time_min = 0;
595
sb->s_time_max = 0;
596
sb->s_op = &cramfs_ops;
597
root = get_cramfs_inode(sb, cramfs_root, 0);
598
if (IS_ERR(root))
599
return PTR_ERR(root);
600
sb->s_root = d_make_root(root);
601
if (!sb->s_root)
602
return -ENOMEM;
603
return 0;
604
}
605
606
static int cramfs_blkdev_fill_super(struct super_block *sb, struct fs_context *fc)
607
{
608
struct cramfs_sb_info *sbi;
609
struct cramfs_super super;
610
int i, err;
611
612
sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
613
if (!sbi)
614
return -ENOMEM;
615
sb->s_fs_info = sbi;
616
617
/* Invalidate the read buffers on mount: think disk change.. */
618
for (i = 0; i < READ_BUFFERS; i++)
619
buffer_blocknr[i] = -1;
620
621
err = cramfs_read_super(sb, fc, &super);
622
if (err)
623
return err;
624
return cramfs_finalize_super(sb, &super.root);
625
}
626
627
static int cramfs_mtd_fill_super(struct super_block *sb, struct fs_context *fc)
628
{
629
struct cramfs_sb_info *sbi;
630
struct cramfs_super super;
631
int err;
632
633
sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
634
if (!sbi)
635
return -ENOMEM;
636
sb->s_fs_info = sbi;
637
638
/* Map only one page for now. Will remap it when fs size is known. */
639
err = mtd_point(sb->s_mtd, 0, PAGE_SIZE, &sbi->mtd_point_size,
640
&sbi->linear_virt_addr, &sbi->linear_phys_addr);
641
if (err || sbi->mtd_point_size != PAGE_SIZE) {
642
pr_err("unable to get direct memory access to mtd:%s\n",
643
sb->s_mtd->name);
644
return err ? : -ENODATA;
645
}
646
647
pr_info("checking physical address %pap for linear cramfs image\n",
648
&sbi->linear_phys_addr);
649
err = cramfs_read_super(sb, fc, &super);
650
if (err)
651
return err;
652
653
/* Remap the whole filesystem now */
654
pr_info("linear cramfs image on mtd:%s appears to be %lu KB in size\n",
655
sb->s_mtd->name, sbi->size/1024);
656
mtd_unpoint(sb->s_mtd, 0, PAGE_SIZE);
657
err = mtd_point(sb->s_mtd, 0, sbi->size, &sbi->mtd_point_size,
658
&sbi->linear_virt_addr, &sbi->linear_phys_addr);
659
if (err || sbi->mtd_point_size != sbi->size) {
660
pr_err("unable to get direct memory access to mtd:%s\n",
661
sb->s_mtd->name);
662
return err ? : -ENODATA;
663
}
664
665
return cramfs_finalize_super(sb, &super.root);
666
}
667
668
static int cramfs_statfs(struct dentry *dentry, struct kstatfs *buf)
669
{
670
struct super_block *sb = dentry->d_sb;
671
u64 id = 0;
672
673
if (sb->s_bdev)
674
id = huge_encode_dev(sb->s_bdev->bd_dev);
675
else if (sb->s_dev)
676
id = huge_encode_dev(sb->s_dev);
677
678
buf->f_type = CRAMFS_MAGIC;
679
buf->f_bsize = PAGE_SIZE;
680
buf->f_blocks = CRAMFS_SB(sb)->blocks;
681
buf->f_bfree = 0;
682
buf->f_bavail = 0;
683
buf->f_files = CRAMFS_SB(sb)->files;
684
buf->f_ffree = 0;
685
buf->f_fsid = u64_to_fsid(id);
686
buf->f_namelen = CRAMFS_MAXPATHLEN;
687
return 0;
688
}
689
690
/*
691
* Read a cramfs directory entry.
692
*/
693
static int cramfs_readdir(struct file *file, struct dir_context *ctx)
694
{
695
struct inode *inode = file_inode(file);
696
struct super_block *sb = inode->i_sb;
697
char *buf;
698
unsigned int offset;
699
700
/* Offset within the thing. */
701
if (ctx->pos >= inode->i_size)
702
return 0;
703
offset = ctx->pos;
704
/* Directory entries are always 4-byte aligned */
705
if (offset & 3)
706
return -EINVAL;
707
708
buf = kmalloc(CRAMFS_MAXPATHLEN, GFP_KERNEL);
709
if (!buf)
710
return -ENOMEM;
711
712
while (offset < inode->i_size) {
713
struct cramfs_inode *de;
714
unsigned long nextoffset;
715
char *name;
716
ino_t ino;
717
umode_t mode;
718
int namelen;
719
720
mutex_lock(&read_mutex);
721
de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+CRAMFS_MAXPATHLEN);
722
name = (char *)(de+1);
723
724
/*
725
* Namelengths on disk are shifted by two
726
* and the name padded out to 4-byte boundaries
727
* with zeroes.
728
*/
729
namelen = de->namelen << 2;
730
memcpy(buf, name, namelen);
731
ino = cramino(de, OFFSET(inode) + offset);
732
mode = de->mode;
733
mutex_unlock(&read_mutex);
734
nextoffset = offset + sizeof(*de) + namelen;
735
for (;;) {
736
if (!namelen) {
737
kfree(buf);
738
return -EIO;
739
}
740
if (buf[namelen-1])
741
break;
742
namelen--;
743
}
744
if (!dir_emit(ctx, buf, namelen, ino, mode >> 12))
745
break;
746
747
ctx->pos = offset = nextoffset;
748
}
749
kfree(buf);
750
return 0;
751
}
752
753
/*
754
* Lookup and fill in the inode data..
755
*/
756
static struct dentry *cramfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
757
{
758
unsigned int offset = 0;
759
struct inode *inode = NULL;
760
int sorted;
761
762
mutex_lock(&read_mutex);
763
sorted = CRAMFS_SB(dir->i_sb)->flags & CRAMFS_FLAG_SORTED_DIRS;
764
while (offset < dir->i_size) {
765
struct cramfs_inode *de;
766
char *name;
767
int namelen, retval;
768
int dir_off = OFFSET(dir) + offset;
769
770
de = cramfs_read(dir->i_sb, dir_off, sizeof(*de)+CRAMFS_MAXPATHLEN);
771
name = (char *)(de+1);
772
773
/* Try to take advantage of sorted directories */
774
if (sorted && (dentry->d_name.name[0] < name[0]))
775
break;
776
777
namelen = de->namelen << 2;
778
offset += sizeof(*de) + namelen;
779
780
/* Quick check that the name is roughly the right length */
781
if (((dentry->d_name.len + 3) & ~3) != namelen)
782
continue;
783
784
for (;;) {
785
if (!namelen) {
786
inode = ERR_PTR(-EIO);
787
goto out;
788
}
789
if (name[namelen-1])
790
break;
791
namelen--;
792
}
793
if (namelen != dentry->d_name.len)
794
continue;
795
retval = memcmp(dentry->d_name.name, name, namelen);
796
if (retval > 0)
797
continue;
798
if (!retval) {
799
inode = get_cramfs_inode(dir->i_sb, de, dir_off);
800
break;
801
}
802
/* else (retval < 0) */
803
if (sorted)
804
break;
805
}
806
out:
807
mutex_unlock(&read_mutex);
808
return d_splice_alias(inode, dentry);
809
}
810
811
static int cramfs_read_folio(struct file *file, struct folio *folio)
812
{
813
struct inode *inode = folio->mapping->host;
814
u32 maxblock;
815
int bytes_filled;
816
void *pgdata;
817
bool success = false;
818
819
maxblock = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
820
bytes_filled = 0;
821
pgdata = kmap_local_folio(folio, 0);
822
823
if (folio->index < maxblock) {
824
struct super_block *sb = inode->i_sb;
825
u32 blkptr_offset = OFFSET(inode) + folio->index * 4;
826
u32 block_ptr, block_start, block_len;
827
bool uncompressed, direct;
828
829
mutex_lock(&read_mutex);
830
block_ptr = *(u32 *) cramfs_read(sb, blkptr_offset, 4);
831
uncompressed = (block_ptr & CRAMFS_BLK_FLAG_UNCOMPRESSED);
832
direct = (block_ptr & CRAMFS_BLK_FLAG_DIRECT_PTR);
833
block_ptr &= ~CRAMFS_BLK_FLAGS;
834
835
if (direct) {
836
/*
837
* The block pointer is an absolute start pointer,
838
* shifted by 2 bits. The size is included in the
839
* first 2 bytes of the data block when compressed,
840
* or PAGE_SIZE otherwise.
841
*/
842
block_start = block_ptr << CRAMFS_BLK_DIRECT_PTR_SHIFT;
843
if (uncompressed) {
844
block_len = PAGE_SIZE;
845
/* if last block: cap to file length */
846
if (folio->index == maxblock - 1)
847
block_len =
848
offset_in_page(inode->i_size);
849
} else {
850
block_len = *(u16 *)
851
cramfs_read(sb, block_start, 2);
852
block_start += 2;
853
}
854
} else {
855
/*
856
* The block pointer indicates one past the end of
857
* the current block (start of next block). If this
858
* is the first block then it starts where the block
859
* pointer table ends, otherwise its start comes
860
* from the previous block's pointer.
861
*/
862
block_start = OFFSET(inode) + maxblock * 4;
863
if (folio->index)
864
block_start = *(u32 *)
865
cramfs_read(sb, blkptr_offset - 4, 4);
866
/* Beware... previous ptr might be a direct ptr */
867
if (unlikely(block_start & CRAMFS_BLK_FLAG_DIRECT_PTR)) {
868
/* See comments on earlier code. */
869
u32 prev_start = block_start;
870
block_start = prev_start & ~CRAMFS_BLK_FLAGS;
871
block_start <<= CRAMFS_BLK_DIRECT_PTR_SHIFT;
872
if (prev_start & CRAMFS_BLK_FLAG_UNCOMPRESSED) {
873
block_start += PAGE_SIZE;
874
} else {
875
block_len = *(u16 *)
876
cramfs_read(sb, block_start, 2);
877
block_start += 2 + block_len;
878
}
879
}
880
block_start &= ~CRAMFS_BLK_FLAGS;
881
block_len = block_ptr - block_start;
882
}
883
884
if (block_len == 0)
885
; /* hole */
886
else if (unlikely(block_len > 2*PAGE_SIZE ||
887
(uncompressed && block_len > PAGE_SIZE))) {
888
mutex_unlock(&read_mutex);
889
pr_err("bad data blocksize %u\n", block_len);
890
goto err;
891
} else if (uncompressed) {
892
memcpy(pgdata,
893
cramfs_read(sb, block_start, block_len),
894
block_len);
895
bytes_filled = block_len;
896
} else {
897
bytes_filled = cramfs_uncompress_block(pgdata,
898
PAGE_SIZE,
899
cramfs_read(sb, block_start, block_len),
900
block_len);
901
}
902
mutex_unlock(&read_mutex);
903
if (unlikely(bytes_filled < 0))
904
goto err;
905
}
906
907
memset(pgdata + bytes_filled, 0, PAGE_SIZE - bytes_filled);
908
flush_dcache_folio(folio);
909
910
success = true;
911
err:
912
kunmap_local(pgdata);
913
folio_end_read(folio, success);
914
return 0;
915
}
916
917
static const struct address_space_operations cramfs_aops = {
918
.read_folio = cramfs_read_folio
919
};
920
921
/*
922
* Our operations:
923
*/
924
925
/*
926
* A directory can only readdir
927
*/
928
static const struct file_operations cramfs_directory_operations = {
929
.llseek = generic_file_llseek,
930
.read = generic_read_dir,
931
.iterate_shared = cramfs_readdir,
932
};
933
934
static const struct inode_operations cramfs_dir_inode_operations = {
935
.lookup = cramfs_lookup,
936
};
937
938
static const struct super_operations cramfs_ops = {
939
.statfs = cramfs_statfs,
940
};
941
942
static int cramfs_get_tree(struct fs_context *fc)
943
{
944
int ret = -ENOPROTOOPT;
945
946
if (IS_ENABLED(CONFIG_CRAMFS_MTD)) {
947
ret = get_tree_mtd(fc, cramfs_mtd_fill_super);
948
if (!ret)
949
return 0;
950
}
951
if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV))
952
ret = get_tree_bdev(fc, cramfs_blkdev_fill_super);
953
return ret;
954
}
955
956
static const struct fs_context_operations cramfs_context_ops = {
957
.get_tree = cramfs_get_tree,
958
.reconfigure = cramfs_reconfigure,
959
};
960
961
/*
962
* Set up the filesystem mount context.
963
*/
964
static int cramfs_init_fs_context(struct fs_context *fc)
965
{
966
fc->ops = &cramfs_context_ops;
967
return 0;
968
}
969
970
static struct file_system_type cramfs_fs_type = {
971
.owner = THIS_MODULE,
972
.name = "cramfs",
973
.init_fs_context = cramfs_init_fs_context,
974
.kill_sb = cramfs_kill_sb,
975
.fs_flags = FS_REQUIRES_DEV,
976
};
977
MODULE_ALIAS_FS("cramfs");
978
979
static int __init init_cramfs_fs(void)
980
{
981
int rv;
982
983
rv = cramfs_uncompress_init();
984
if (rv < 0)
985
return rv;
986
rv = register_filesystem(&cramfs_fs_type);
987
if (rv < 0)
988
cramfs_uncompress_exit();
989
return rv;
990
}
991
992
static void __exit exit_cramfs_fs(void)
993
{
994
cramfs_uncompress_exit();
995
unregister_filesystem(&cramfs_fs_type);
996
}
997
998
module_init(init_cramfs_fs)
999
module_exit(exit_cramfs_fs)
1000
MODULE_DESCRIPTION("Compressed ROM file system support");
1001
MODULE_LICENSE("GPL");
1002
1003