inode.c (f26e8817b235d8764363bffcc9cbfc61867371f2) inode.c (99c18ce580c6cc6763e694b4ce320d7b226ab59b)
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

--- 5 unchanged lines hidden (view full) ---

14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/module.h>
17#include <linux/fs.h>
18#include <linux/pagemap.h>
19#include <linux/init.h>
20#include <linux/string.h>
21#include <linux/blkdev.h>
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

--- 5 unchanged lines hidden (view full) ---

14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/module.h>
17#include <linux/fs.h>
18#include <linux/pagemap.h>
19#include <linux/init.h>
20#include <linux/string.h>
21#include <linux/blkdev.h>
22#include <linux/mtd/mtd.h>
23#include <linux/mtd/super.h>
22#include <linux/slab.h>
23#include <linux/vfs.h>
24#include <linux/mutex.h>
25#include <uapi/linux/cramfs_fs.h>
26#include <linux/uaccess.h>
27
28#include "internal.h"
29
30/*
31 * cramfs super-block data in memory
32 */
33struct cramfs_sb_info {
34 unsigned long magic;
35 unsigned long size;
36 unsigned long blocks;
37 unsigned long files;
38 unsigned long flags;
24#include <linux/slab.h>
25#include <linux/vfs.h>
26#include <linux/mutex.h>
27#include <uapi/linux/cramfs_fs.h>
28#include <linux/uaccess.h>
29
30#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;
41 void *linear_virt_addr;
42 resource_size_t linear_phys_addr;
43 size_t mtd_point_size;
39};
40
41static inline struct cramfs_sb_info *CRAMFS_SB(struct super_block *sb)
42{
43 return sb->s_fs_info;
44}
45
46static const struct super_operations cramfs_ops;

--- 88 unchanged lines hidden (view full) ---

135 * with the rom-image, because the way the filesystem is set
136 * up the accesses should be fairly regular and cached in the
137 * page cache and dentry tree anyway..
138 *
139 * This also acts as a way to guarantee contiguous areas of up to
140 * BLKS_PER_BUF*PAGE_SIZE, so that the caller doesn't need to
141 * worry about end-of-buffer issues even when decompressing a full
142 * page cache.
44};
45
46static inline struct cramfs_sb_info *CRAMFS_SB(struct super_block *sb)
47{
48 return sb->s_fs_info;
49}
50
51static const struct super_operations cramfs_ops;

--- 88 unchanged lines hidden (view full) ---

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
145 * BLKS_PER_BUF*PAGE_SIZE, so that the caller doesn't need to
146 * worry about end-of-buffer issues even when decompressing a full
147 * page cache.
148 *
149 * Note: This is all optimized away at compile time when
150 * CONFIG_CRAMFS_BLOCKDEV=n.
143 */
144#define READ_BUFFERS (2)
145/* NEXT_BUFFER(): Loop over [0..(READ_BUFFERS-1)]. */
146#define NEXT_BUFFER(_ix) ((_ix) ^ 1)
147
148/*
149 * BLKS_PER_BUF_SHIFT should be at least 2 to allow for "compressed"
150 * data that takes up more space than the original and with unlucky

--- 4 unchanged lines hidden (view full) ---

155#define BUFFER_SIZE (BLKS_PER_BUF*PAGE_SIZE)
156
157static unsigned char read_buffers[READ_BUFFERS][BUFFER_SIZE];
158static unsigned buffer_blocknr[READ_BUFFERS];
159static struct super_block *buffer_dev[READ_BUFFERS];
160static int next_buffer;
161
162/*
151 */
152#define READ_BUFFERS (2)
153/* NEXT_BUFFER(): Loop over [0..(READ_BUFFERS-1)]. */
154#define NEXT_BUFFER(_ix) ((_ix) ^ 1)
155
156/*
157 * BLKS_PER_BUF_SHIFT should be at least 2 to allow for "compressed"
158 * data that takes up more space than the original and with unlucky

--- 4 unchanged lines hidden (view full) ---

163#define BUFFER_SIZE (BLKS_PER_BUF*PAGE_SIZE)
164
165static unsigned char read_buffers[READ_BUFFERS][BUFFER_SIZE];
166static unsigned buffer_blocknr[READ_BUFFERS];
167static struct super_block *buffer_dev[READ_BUFFERS];
168static int next_buffer;
169
170/*
163 * Returns a pointer to a buffer containing at least LEN bytes of
164 * filesystem starting at byte offset OFFSET into the filesystem.
171 * Populate our block cache and return a pointer to it.
165 */
172 */
166static void *cramfs_read(struct super_block *sb, unsigned int offset, unsigned int len)
173static void *cramfs_blkdev_read(struct super_block *sb, unsigned int offset,
174 unsigned int len)
167{
168 struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
169 struct page *pages[BLKS_PER_BUF];
170 unsigned i, blocknr, buffer;
171 unsigned long devsize;
172 char *data;
173
174 if (!len)

--- 59 unchanged lines hidden (view full) ---

234 put_page(page);
235 } else
236 memset(data, 0, PAGE_SIZE);
237 data += PAGE_SIZE;
238 }
239 return read_buffers[buffer] + offset;
240}
241
175{
176 struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
177 struct page *pages[BLKS_PER_BUF];
178 unsigned i, blocknr, buffer;
179 unsigned long devsize;
180 char *data;
181
182 if (!len)

--- 59 unchanged lines hidden (view full) ---

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 */
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
242static void cramfs_kill_sb(struct super_block *sb)
243{
244 struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
245
282static void cramfs_kill_sb(struct super_block *sb)
283{
284 struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
285
246 kill_block_super(sb);
286 if (IS_ENABLED(CCONFIG_CRAMFS_MTD) && sb->s_mtd) {
287 if (sbi && sbi->mtd_point_size)
288 mtd_unpoint(sb->s_mtd, 0, sbi->mtd_point_size);
289 kill_mtd_super(sb);
290 } else if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV) && sb->s_bdev) {
291 kill_block_super(sb);
292 }
247 kfree(sbi);
248}
249
250static int cramfs_remount(struct super_block *sb, int *flags, char *data)
251{
252 sync_filesystem(sb);
253 *flags |= MS_RDONLY;
254 return 0;
255}
256
293 kfree(sbi);
294}
295
296static int cramfs_remount(struct super_block *sb, int *flags, char *data)
297{
298 sync_filesystem(sb);
299 *flags |= MS_RDONLY;
300 return 0;
301}
302
257static int cramfs_fill_super(struct super_block *sb, void *data, int silent)
303static int cramfs_read_super(struct super_block *sb,
304 struct cramfs_super *super, int silent)
258{
305{
259 int i;
260 struct cramfs_super super;
306 struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
261 unsigned long root_offset;
307 unsigned long root_offset;
262 struct cramfs_sb_info *sbi;
263 struct inode *root;
264
308
265 sb->s_flags |= MS_RDONLY;
309 /* We don't know the real size yet */
310 sbi->size = PAGE_SIZE;
266
311
267 sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
268 if (!sbi)
269 return -ENOMEM;
270 sb->s_fs_info = sbi;
271
272 /* Invalidate the read buffers on mount: think disk change.. */
273 mutex_lock(&read_mutex);
274 for (i = 0; i < READ_BUFFERS; i++)
275 buffer_blocknr[i] = -1;
276
277 /* Read the first block and get the superblock from it */
312 /* Read the first block and get the superblock from it */
278 memcpy(&super, cramfs_read(sb, 0, sizeof(super)), sizeof(super));
313 mutex_lock(&read_mutex);
314 memcpy(super, cramfs_read(sb, 0, sizeof(*super)), sizeof(*super));
279 mutex_unlock(&read_mutex);
280
281 /* Do sanity checks on the superblock */
315 mutex_unlock(&read_mutex);
316
317 /* Do sanity checks on the superblock */
282 if (super.magic != CRAMFS_MAGIC) {
318 if (super->magic != CRAMFS_MAGIC) {
283 /* check for wrong endianness */
319 /* check for wrong endianness */
284 if (super.magic == CRAMFS_MAGIC_WEND) {
320 if (super->magic == CRAMFS_MAGIC_WEND) {
285 if (!silent)
286 pr_err("wrong endianness\n");
287 return -EINVAL;
288 }
289
290 /* check at 512 byte offset */
291 mutex_lock(&read_mutex);
321 if (!silent)
322 pr_err("wrong endianness\n");
323 return -EINVAL;
324 }
325
326 /* check at 512 byte offset */
327 mutex_lock(&read_mutex);
292 memcpy(&super, cramfs_read(sb, 512, sizeof(super)), sizeof(super));
328 memcpy(super,
329 cramfs_read(sb, 512, sizeof(*super)),
330 sizeof(*super));
293 mutex_unlock(&read_mutex);
331 mutex_unlock(&read_mutex);
294 if (super.magic != CRAMFS_MAGIC) {
295 if (super.magic == CRAMFS_MAGIC_WEND && !silent)
332 if (super->magic != CRAMFS_MAGIC) {
333 if (super->magic == CRAMFS_MAGIC_WEND && !silent)
296 pr_err("wrong endianness\n");
297 else if (!silent)
298 pr_err("wrong magic\n");
299 return -EINVAL;
300 }
301 }
302
303 /* get feature flags first */
334 pr_err("wrong endianness\n");
335 else if (!silent)
336 pr_err("wrong magic\n");
337 return -EINVAL;
338 }
339 }
340
341 /* get feature flags first */
304 if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
342 if (super->flags & ~CRAMFS_SUPPORTED_FLAGS) {
305 pr_err("unsupported filesystem features\n");
306 return -EINVAL;
307 }
308
309 /* Check that the root inode is in a sane state */
343 pr_err("unsupported filesystem features\n");
344 return -EINVAL;
345 }
346
347 /* Check that the root inode is in a sane state */
310 if (!S_ISDIR(super.root.mode)) {
348 if (!S_ISDIR(super->root.mode)) {
311 pr_err("root is not a directory\n");
312 return -EINVAL;
313 }
314 /* correct strange, hard-coded permissions of mkcramfs */
349 pr_err("root is not a directory\n");
350 return -EINVAL;
351 }
352 /* correct strange, hard-coded permissions of mkcramfs */
315 super.root.mode |= (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
353 super->root.mode |= 0555;
316
354
317 root_offset = super.root.offset << 2;
318 if (super.flags & CRAMFS_FLAG_FSID_VERSION_2) {
319 sbi->size = super.size;
320 sbi->blocks = super.fsid.blocks;
321 sbi->files = super.fsid.files;
355 root_offset = super->root.offset << 2;
356 if (super->flags & CRAMFS_FLAG_FSID_VERSION_2) {
357 sbi->size = super->size;
358 sbi->blocks = super->fsid.blocks;
359 sbi->files = super->fsid.files;
322 } else {
323 sbi->size = 1<<28;
324 sbi->blocks = 0;
325 sbi->files = 0;
326 }
360 } else {
361 sbi->size = 1<<28;
362 sbi->blocks = 0;
363 sbi->files = 0;
364 }
327 sbi->magic = super.magic;
328 sbi->flags = super.flags;
365 sbi->magic = super->magic;
366 sbi->flags = super->flags;
329 if (root_offset == 0)
330 pr_info("empty filesystem");
367 if (root_offset == 0)
368 pr_info("empty filesystem");
331 else if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
369 else if (!(super->flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
332 ((root_offset != sizeof(struct cramfs_super)) &&
333 (root_offset != 512 + sizeof(struct cramfs_super))))
334 {
335 pr_err("bad root offset %lu\n", root_offset);
336 return -EINVAL;
337 }
338
370 ((root_offset != sizeof(struct cramfs_super)) &&
371 (root_offset != 512 + sizeof(struct cramfs_super))))
372 {
373 pr_err("bad root offset %lu\n", root_offset);
374 return -EINVAL;
375 }
376
377 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
339 /* Set it all up.. */
385 /* Set it all up.. */
386 sb->s_flags |= MS_RDONLY;
340 sb->s_op = &cramfs_ops;
387 sb->s_op = &cramfs_ops;
341 root = get_cramfs_inode(sb, &super.root, 0);
388 root = get_cramfs_inode(sb, cramfs_root, 0);
342 if (IS_ERR(root))
343 return PTR_ERR(root);
344 sb->s_root = d_make_root(root);
345 if (!sb->s_root)
346 return -ENOMEM;
347 return 0;
348}
349
389 if (IS_ERR(root))
390 return PTR_ERR(root);
391 sb->s_root = d_make_root(root);
392 if (!sb->s_root)
393 return -ENOMEM;
394 return 0;
395}
396
397static 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
350static int cramfs_statfs(struct dentry *dentry, struct kstatfs *buf)
351{
352 struct super_block *sb = dentry->d_sb;
461static int cramfs_statfs(struct dentry *dentry, struct kstatfs *buf)
462{
463 struct super_block *sb = dentry->d_sb;
353 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
464 u64 id = 0;
354
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);
470
355 buf->f_type = CRAMFS_MAGIC;
356 buf->f_bsize = PAGE_SIZE;
357 buf->f_blocks = CRAMFS_SB(sb)->blocks;
358 buf->f_bfree = 0;
359 buf->f_bavail = 0;
360 buf->f_files = CRAMFS_SB(sb)->files;
361 buf->f_ffree = 0;
362 buf->f_fsid.val[0] = (u32)id;

--- 205 unchanged lines hidden (view full) ---

568 .lookup = cramfs_lookup,
569};
570
571static const struct super_operations cramfs_ops = {
572 .remount_fs = cramfs_remount,
573 .statfs = cramfs_statfs,
574};
575
471 buf->f_type = CRAMFS_MAGIC;
472 buf->f_bsize = PAGE_SIZE;
473 buf->f_blocks = CRAMFS_SB(sb)->blocks;
474 buf->f_bfree = 0;
475 buf->f_bavail = 0;
476 buf->f_files = CRAMFS_SB(sb)->files;
477 buf->f_ffree = 0;
478 buf->f_fsid.val[0] = (u32)id;

--- 205 unchanged lines hidden (view full) ---

684 .lookup = cramfs_lookup,
685};
686
687static const struct super_operations cramfs_ops = {
688 .remount_fs = cramfs_remount,
689 .statfs = cramfs_statfs,
690};
691
576static struct dentry *cramfs_mount(struct file_system_type *fs_type,
577 int flags, const char *dev_name, void *data)
692static struct dentry *cramfs_mount(struct file_system_type *fs_type, int flags,
693 const char *dev_name, void *data)
578{
694{
579 return mount_bdev(fs_type, flags, dev_name, data, cramfs_fill_super);
695 struct dentry *ret = ERR_PTR(-ENOPROTOOPT);
696
697 if (IS_ENABLED(CONFIG_CRAMFS_MTD)) {
698 ret = mount_mtd(fs_type, flags, dev_name, data,
699 cramfs_mtd_fill_super);
700 if (!IS_ERR(ret))
701 return ret;
702 }
703 if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV)) {
704 ret = mount_bdev(fs_type, flags, dev_name, data,
705 cramfs_blkdev_fill_super);
706 }
707 return ret;
580}
581
582static struct file_system_type cramfs_fs_type = {
583 .owner = THIS_MODULE,
584 .name = "cramfs",
585 .mount = cramfs_mount,
586 .kill_sb = cramfs_kill_sb,
587 .fs_flags = FS_REQUIRES_DEV,

--- 25 unchanged lines hidden ---
708}
709
710static struct file_system_type cramfs_fs_type = {
711 .owner = THIS_MODULE,
712 .name = "cramfs",
713 .mount = cramfs_mount,
714 .kill_sb = cramfs_kill_sb,
715 .fs_flags = FS_REQUIRES_DEV,

--- 25 unchanged lines hidden ---