2 * Compressed rom filesystem for Linux.
4 * Copyright (C) 1999 Linus Torvalds.
6 * This file is released under the GPL.
10 * These are the VFS interfaces to the compressed rom filesystem.
11 * The actual compression is based on zlib, see the other files.
14 #include <linux/module.h>
16 #include <linux/pagemap.h>
17 #include <linux/init.h>
18 #include <linux/string.h>
19 #include <linux/blkdev.h>
20 #include <linux/cramfs_fs.h>
21 #include <linux/slab.h>
22 #include <linux/cramfs_fs_sb.h>
23 #include <linux/buffer_head.h>
24 #include <linux/vfs.h>
25 #include <linux/mutex.h>
26 #include <asm/semaphore.h>
27 #include <asm/uaccess.h>
28 #include <asm/byteorder.h>
30 static struct super_operations cramfs_ops
;
31 static struct inode_operations cramfs_dir_inode_operations
;
32 static const struct file_operations cramfs_directory_operations
;
33 static const struct address_space_operations cramfs_aops
;
35 static DEFINE_MUTEX(read_mutex
);
38 #if defined(__LITTLE_ENDIAN)
39 #define le24_to_cpu(x) (x)
40 #define CRAMFS_GET_NAMELEN(x) ((x)->namelen)
41 #define CRAMFS_GET_OFFSET(x) ((x)->offset)
43 #if defined(__BIG_ENDIAN)
44 #define le24_to_cpu(x) ((le32_to_cpu(x)) >> 8)
45 #define CRAMFS_GET_NAMELEN(x) (((u8*)(x))[8] & 0x3f)
46 #define CRAMFS_GET_OFFSET(x) ((le24_to_cpu(((u32*)(x))[2] & 0xffffff) << 2) \
47 | ((((u32*)(x))[2] & 0xc0000000) >> 30))
49 #if !defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN)
50 #error "neither __LITTLE_ENDIAN or __BIG_ENDIAN defined?"
52 #if defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
53 #error "both __LITTLE_ENDIAN and __BIG_ENDIAN defined?"
56 /* These two macros may change in future, to provide better st_ino
58 #define CRAMINO(x) ((CRAMFS_GET_OFFSET(x) && le24_to_cpu((x)->size)) ? \
59 CRAMFS_GET_OFFSET(x)<<2 : 1)
60 #define OFFSET(x) ((x)->i_ino)
63 static int cramfs_iget5_test(struct inode
*inode
, void *opaque
)
65 struct cramfs_inode
*cramfs_inode
= opaque
;
67 if (inode
->i_ino
!= CRAMINO(cramfs_inode
))
68 return 0; /* does not match */
70 if (inode
->i_ino
!= 1)
73 /* all empty directories, char, block, pipe, and sock, share inode #1 */
75 if ((inode
->i_mode
!= cramfs_inode
->mode
) ||
76 (inode
->i_gid
!= cramfs_inode
->gid
) ||
77 (inode
->i_uid
!= cramfs_inode
->uid
))
78 return 0; /* does not match */
80 if ((S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
)) &&
81 (inode
->i_rdev
!= old_decode_dev(le24_to_cpu(cramfs_inode
->size
))))
82 return 0; /* does not match */
84 return 1; /* matches */
87 static int cramfs_iget5_set(struct inode
*inode
, void *opaque
)
89 static struct timespec zerotime
;
90 struct cramfs_inode
*cramfs_inode
= opaque
;
92 inode
->i_mode
= le16_to_cpu(cramfs_inode
->mode
);
93 inode
->i_uid
= le16_to_cpu(cramfs_inode
->uid
);
94 inode
->i_size
= le24_to_cpu(cramfs_inode
->size
);
95 inode
->i_blocks
= (inode
->i_size
- 1) / 512 + 1;
96 inode
->i_gid
= cramfs_inode
->gid
;
97 /* Struct copy intentional */
98 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= zerotime
;
99 inode
->i_ino
= CRAMINO(cramfs_inode
);
100 /* inode->i_nlink is left 1 - arguably wrong for directories,
101 but it's the best we can do without reading the directory
102 contents. 1 yields the right result in GNU find, even
103 without -noleaf option. */
104 if (S_ISREG(inode
->i_mode
)) {
105 inode
->i_fop
= &generic_ro_fops
;
106 inode
->i_data
.a_ops
= &cramfs_aops
;
107 } else if (S_ISDIR(inode
->i_mode
)) {
108 inode
->i_op
= &cramfs_dir_inode_operations
;
109 inode
->i_fop
= &cramfs_directory_operations
;
110 } else if (S_ISLNK(inode
->i_mode
)) {
111 inode
->i_op
= &page_symlink_inode_operations
;
112 inode
->i_data
.a_ops
= &cramfs_aops
;
116 init_special_inode(inode
, inode
->i_mode
,
117 old_decode_dev(le24_to_cpu(cramfs_inode
->size
)));
122 static struct inode
*get_cramfs_inode(struct super_block
*sb
,
123 struct cramfs_inode
* cramfs_inode
)
125 struct inode
*inode
= iget5_locked(sb
, CRAMINO(cramfs_inode
),
126 cramfs_iget5_test
, cramfs_iget5_set
,
129 if (inode
&& (inode
->i_state
& I_NEW
)) {
130 unlock_new_inode(inode
);
136 * We have our own block cache: don't fill up the buffer cache
137 * with the rom-image, because the way the filesystem is set
138 * up the accesses should be fairly regular and cached in the
139 * page cache and dentry tree anyway..
141 * This also acts as a way to guarantee contiguous areas of up to
142 * BLKS_PER_BUF*PAGE_CACHE_SIZE, so that the caller doesn't need to
143 * worry about end-of-buffer issues even when decompressing a full
146 #define READ_BUFFERS (2)
147 /* NEXT_BUFFER(): Loop over [0..(READ_BUFFERS-1)]. */
148 #define NEXT_BUFFER(_ix) ((_ix) ^ 1)
151 * BLKS_PER_BUF_SHIFT should be at least 2 to allow for "compressed"
152 * data that takes up more space than the original and with unlucky
155 #define BLKS_PER_BUF_SHIFT (2)
156 #define BLKS_PER_BUF (1 << BLKS_PER_BUF_SHIFT)
157 #define BUFFER_SIZE (BLKS_PER_BUF*PAGE_CACHE_SIZE)
159 static unsigned char read_buffers
[READ_BUFFERS
][BUFFER_SIZE
];
160 static unsigned buffer_blocknr
[READ_BUFFERS
];
161 static struct super_block
* buffer_dev
[READ_BUFFERS
];
162 static int next_buffer
;
165 * Returns a pointer to a buffer containing at least LEN bytes of
166 * filesystem starting at byte offset OFFSET into the filesystem.
168 static void *cramfs_read(struct super_block
*sb
, unsigned int offset
, unsigned int len
)
170 struct address_space
*mapping
= sb
->s_bdev
->bd_inode
->i_mapping
;
171 struct page
*pages
[BLKS_PER_BUF
];
172 unsigned i
, blocknr
, buffer
, unread
;
173 unsigned long devsize
;
178 blocknr
= offset
>> PAGE_CACHE_SHIFT
;
179 offset
&= PAGE_CACHE_SIZE
- 1;
181 /* Check if an existing buffer already has the data.. */
182 for (i
= 0; i
< READ_BUFFERS
; i
++) {
183 unsigned int blk_offset
;
185 if (buffer_dev
[i
] != sb
)
187 if (blocknr
< buffer_blocknr
[i
])
189 blk_offset
= (blocknr
- buffer_blocknr
[i
]) << PAGE_CACHE_SHIFT
;
190 blk_offset
+= offset
;
191 if (blk_offset
+ len
> BUFFER_SIZE
)
193 return read_buffers
[i
] + blk_offset
;
196 devsize
= mapping
->host
->i_size
>> PAGE_CACHE_SHIFT
;
198 /* Ok, read in BLKS_PER_BUF pages completely first. */
200 for (i
= 0; i
< BLKS_PER_BUF
; i
++) {
201 struct page
*page
= NULL
;
203 if (blocknr
+ i
< devsize
) {
204 page
= read_mapping_page(mapping
, blocknr
+ i
, NULL
);
205 /* synchronous error? */
212 for (i
= 0; i
< BLKS_PER_BUF
; i
++) {
213 struct page
*page
= pages
[i
];
215 wait_on_page_locked(page
);
216 if (!PageUptodate(page
)) {
217 /* asynchronous error */
218 page_cache_release(page
);
224 buffer
= next_buffer
;
225 next_buffer
= NEXT_BUFFER(buffer
);
226 buffer_blocknr
[buffer
] = blocknr
;
227 buffer_dev
[buffer
] = sb
;
229 data
= read_buffers
[buffer
];
230 for (i
= 0; i
< BLKS_PER_BUF
; i
++) {
231 struct page
*page
= pages
[i
];
233 memcpy(data
, kmap(page
), PAGE_CACHE_SIZE
);
235 page_cache_release(page
);
237 memset(data
, 0, PAGE_CACHE_SIZE
);
238 data
+= PAGE_CACHE_SIZE
;
240 return read_buffers
[buffer
] + offset
;
243 static void cramfs_put_super(struct super_block
*sb
)
245 kfree(sb
->s_fs_info
);
246 sb
->s_fs_info
= NULL
;
249 static int cramfs_remount(struct super_block
*sb
, int *flags
, char *data
)
255 static int cramfs_fill_super(struct super_block
*sb
, void *data
, int silent
)
258 struct cramfs_super super
;
259 unsigned long root_offset
;
260 struct cramfs_sb_info
*sbi
;
263 sb
->s_flags
|= MS_RDONLY
;
265 sbi
= kzalloc(sizeof(struct cramfs_sb_info
), GFP_KERNEL
);
270 /* Invalidate the read buffers on mount: think disk change.. */
271 mutex_lock(&read_mutex
);
272 for (i
= 0; i
< READ_BUFFERS
; i
++)
273 buffer_blocknr
[i
] = -1;
275 /* Read the first block and get the superblock from it */
276 memcpy(&super
, cramfs_read(sb
, 0, sizeof(super
)), sizeof(super
));
277 mutex_unlock(&read_mutex
);
279 /* Do sanity checks on the superblock */
280 if (le32_to_cpu(super
.magic
) != CRAMFS_MAGIC
) {
281 /* check at 512 byte offset */
282 mutex_lock(&read_mutex
);
283 memcpy(&super
, cramfs_read(sb
, 512, sizeof(super
)), sizeof(super
));
284 mutex_unlock(&read_mutex
);
285 if (le32_to_cpu(super
.magic
) != CRAMFS_MAGIC
) {
287 printk(KERN_ERR
"cramfs: wrong magic\n");
292 /* get feature flags first */
293 if (le32_to_cpu(super
.flags
) & ~CRAMFS_SUPPORTED_FLAGS
) {
294 printk(KERN_ERR
"cramfs: unsupported filesystem features\n");
298 /* Check that the root inode is in a sane state */
299 if (!S_ISDIR(le16_to_cpu(super
.root
.mode
))) {
300 printk(KERN_ERR
"cramfs: root is not a directory\n");
303 root_offset
= CRAMFS_GET_OFFSET(&super
.root
) << 2;
304 if (le32_to_cpu(super
.flags
) & CRAMFS_FLAG_FSID_VERSION_2
) {
305 sbi
->size
=le32_to_cpu(super
.size
);
306 sbi
->blocks
=le32_to_cpu(super
.fsid
.blocks
);
307 sbi
->files
=le32_to_cpu(super
.fsid
.files
);
313 sbi
->magic
=le32_to_cpu(super
.magic
);
314 sbi
->flags
=le32_to_cpu(super
.flags
);
315 if (root_offset
== 0)
316 printk(KERN_INFO
"cramfs: empty filesystem");
317 else if (!(le32_to_cpu(super
.flags
) & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET
) &&
318 ((root_offset
!= sizeof(struct cramfs_super
)) &&
319 (root_offset
!= 512 + sizeof(struct cramfs_super
))))
321 printk(KERN_ERR
"cramfs: bad root offset %lu\n", root_offset
);
325 /* Set it all up.. */
326 sb
->s_op
= &cramfs_ops
;
327 root
= get_cramfs_inode(sb
, &super
.root
);
330 sb
->s_root
= d_alloc_root(root
);
338 sb
->s_fs_info
= NULL
;
342 static int cramfs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
344 struct super_block
*sb
= dentry
->d_sb
;
346 buf
->f_type
= CRAMFS_MAGIC
;
347 buf
->f_bsize
= PAGE_CACHE_SIZE
;
348 buf
->f_blocks
= le32_to_cpu(CRAMFS_SB(sb
)->blocks
);
351 buf
->f_files
= le32_to_cpu(CRAMFS_SB(sb
)->files
);
353 buf
->f_namelen
= CRAMFS_MAXPATHLEN
;
358 * Read a cramfs directory entry.
360 static int cramfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
362 struct inode
*inode
= filp
->f_dentry
->d_inode
;
363 struct super_block
*sb
= inode
->i_sb
;
368 /* Offset within the thing. */
369 offset
= filp
->f_pos
;
370 if (offset
>= inode
->i_size
)
372 /* Directory entries are always 4-byte aligned */
376 buf
= kmalloc(256, GFP_KERNEL
);
381 while (offset
< inode
->i_size
) {
382 struct cramfs_inode
*de
;
383 unsigned long nextoffset
;
389 mutex_lock(&read_mutex
);
390 de
= cramfs_read(sb
, OFFSET(inode
) + offset
, sizeof(*de
)+256);
391 name
= (char *)(de
+1);
394 * Namelengths on disk are shifted by two
395 * and the name padded out to 4-byte boundaries
398 namelen
= CRAMFS_GET_NAMELEN(de
) << 2;
399 memcpy(buf
, name
, namelen
);
401 mode
= le16_to_cpu(de
->mode
);
402 mutex_unlock(&read_mutex
);
403 nextoffset
= offset
+ sizeof(*de
) + namelen
;
413 error
= filldir(dirent
, buf
, namelen
, offset
, ino
, mode
>> 12);
418 filp
->f_pos
= offset
;
426 * Lookup and fill in the inode data..
428 static struct dentry
* cramfs_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
430 unsigned int offset
= 0;
433 mutex_lock(&read_mutex
);
434 sorted
= le32_to_cpu(CRAMFS_SB(dir
->i_sb
)->flags
) & CRAMFS_FLAG_SORTED_DIRS
;
435 while (offset
< dir
->i_size
) {
436 struct cramfs_inode
*de
;
440 de
= cramfs_read(dir
->i_sb
, OFFSET(dir
) + offset
, sizeof(*de
)+256);
441 name
= (char *)(de
+1);
443 /* Try to take advantage of sorted directories */
444 if (sorted
&& (dentry
->d_name
.name
[0] < name
[0]))
447 namelen
= CRAMFS_GET_NAMELEN(de
) << 2;
448 offset
+= sizeof(*de
) + namelen
;
450 /* Quick check that the name is roughly the right length */
451 if (((dentry
->d_name
.len
+ 3) & ~3) != namelen
)
456 mutex_unlock(&read_mutex
);
457 return ERR_PTR(-EIO
);
463 if (namelen
!= dentry
->d_name
.len
)
465 retval
= memcmp(dentry
->d_name
.name
, name
, namelen
);
469 struct cramfs_inode entry
= *de
;
470 mutex_unlock(&read_mutex
);
471 d_add(dentry
, get_cramfs_inode(dir
->i_sb
, &entry
));
474 /* else (retval < 0) */
478 mutex_unlock(&read_mutex
);
483 static int cramfs_readpage(struct file
*file
, struct page
* page
)
485 struct inode
*inode
= page
->mapping
->host
;
486 u32 maxblock
, bytes_filled
;
489 maxblock
= (inode
->i_size
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
491 if (page
->index
< maxblock
) {
492 struct super_block
*sb
= inode
->i_sb
;
493 u32 blkptr_offset
= OFFSET(inode
) + page
->index
*4;
494 u32 start_offset
, compr_len
;
496 start_offset
= OFFSET(inode
) + maxblock
*4;
497 mutex_lock(&read_mutex
);
499 start_offset
= le32_to_cpu(*(u32
*) cramfs_read(sb
, blkptr_offset
-4, 4));
500 compr_len
= (le32_to_cpu(*(u32
*) cramfs_read(sb
, blkptr_offset
, 4)) - start_offset
);
501 mutex_unlock(&read_mutex
);
506 mutex_lock(&read_mutex
);
507 bytes_filled
= cramfs_uncompress_block(pgdata
,
509 cramfs_read(sb
, start_offset
, compr_len
),
511 mutex_unlock(&read_mutex
);
515 memset(pgdata
+ bytes_filled
, 0, PAGE_CACHE_SIZE
- bytes_filled
);
517 flush_dcache_page(page
);
518 SetPageUptodate(page
);
523 static const struct address_space_operations cramfs_aops
= {
524 .readpage
= cramfs_readpage
532 * A directory can only readdir
534 static const struct file_operations cramfs_directory_operations
= {
535 .llseek
= generic_file_llseek
,
536 .read
= generic_read_dir
,
537 .readdir
= cramfs_readdir
,
540 static struct inode_operations cramfs_dir_inode_operations
= {
541 .lookup
= cramfs_lookup
,
544 static struct super_operations cramfs_ops
= {
545 .put_super
= cramfs_put_super
,
546 .remount_fs
= cramfs_remount
,
547 .statfs
= cramfs_statfs
,
550 static int cramfs_get_sb(struct file_system_type
*fs_type
,
551 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
553 return get_sb_bdev(fs_type
, flags
, dev_name
, data
, cramfs_fill_super
,
557 static struct file_system_type cramfs_fs_type
= {
558 .owner
= THIS_MODULE
,
560 .get_sb
= cramfs_get_sb
,
561 .kill_sb
= kill_block_super
,
562 .fs_flags
= FS_REQUIRES_DEV
,
565 static int __init
init_cramfs_fs(void)
569 rv
= cramfs_uncompress_init();
572 rv
= register_filesystem(&cramfs_fs_type
);
574 cramfs_uncompress_exit();
578 static void __exit
exit_cramfs_fs(void)
580 cramfs_uncompress_exit();
581 unregister_filesystem(&cramfs_fs_type
);
584 module_init(init_cramfs_fs
)
585 module_exit(exit_cramfs_fs
)
586 MODULE_LICENSE("GPL");