MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / fs / cramfs / inode.c
blobeecc2ec4952295e7eab214119743e7c0981629d5
1 /*
2 * Compressed rom filesystem for Linux.
4 * Copyright (C) 1999 Linus Torvalds.
6 * This file is released under the GPL.
7 */
9 /*
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>
15 #include <linux/fs.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)
42 #endif
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))
48 #endif
49 #if !defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN)
50 #error "neither __LITTLE_ENDIAN or __BIG_ENDIAN defined?"
51 #endif
52 #if defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
53 #error "both __LITTLE_ENDIAN and __BIG_ENDIAN defined?"
54 #endif
56 /* These two macros may change in future, to provide better st_ino
57 semantics. */
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)
71 return 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;
113 } else {
114 inode->i_size = 0;
115 inode->i_blocks = 0;
116 init_special_inode(inode, inode->i_mode,
117 old_decode_dev(le24_to_cpu(cramfs_inode->size)));
119 return 0;
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,
127 cramfs_inode);
129 if (inode && (inode->i_state & I_NEW)) {
130 unlock_new_inode(inode);
132 return 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
144 * page cache.
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
153 * alignment.
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;
174 char *data;
176 if (!len)
177 return NULL;
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)
186 continue;
187 if (blocknr < buffer_blocknr[i])
188 continue;
189 blk_offset = (blocknr - buffer_blocknr[i]) << PAGE_CACHE_SHIFT;
190 blk_offset += offset;
191 if (blk_offset + len > BUFFER_SIZE)
192 continue;
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. */
199 unread = 0;
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? */
206 if (IS_ERR(page))
207 page = NULL;
209 pages[i] = page;
212 for (i = 0; i < BLKS_PER_BUF; i++) {
213 struct page *page = pages[i];
214 if (page) {
215 wait_on_page_locked(page);
216 if (!PageUptodate(page)) {
217 /* asynchronous error */
218 page_cache_release(page);
219 pages[i] = NULL;
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];
232 if (page) {
233 memcpy(data, kmap(page), PAGE_CACHE_SIZE);
234 kunmap(page);
235 page_cache_release(page);
236 } else
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)
251 *flags |= MS_RDONLY;
252 return 0;
255 static int cramfs_fill_super(struct super_block *sb, void *data, int silent)
257 int i;
258 struct cramfs_super super;
259 unsigned long root_offset;
260 struct cramfs_sb_info *sbi;
261 struct inode *root;
263 sb->s_flags |= MS_RDONLY;
265 sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
266 if (!sbi)
267 return -ENOMEM;
268 sb->s_fs_info = sbi;
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) {
286 if (!silent)
287 printk(KERN_ERR "cramfs: wrong magic\n");
288 goto out;
292 /* get feature flags first */
293 if (le32_to_cpu(super.flags) & ~CRAMFS_SUPPORTED_FLAGS) {
294 printk(KERN_ERR "cramfs: unsupported filesystem features\n");
295 goto out;
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");
301 goto out;
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);
308 } else {
309 sbi->size=1<<28;
310 sbi->blocks=0;
311 sbi->files=0;
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);
322 goto out;
325 /* Set it all up.. */
326 sb->s_op = &cramfs_ops;
327 root = get_cramfs_inode(sb, &super.root);
328 if (!root)
329 goto out;
330 sb->s_root = d_alloc_root(root);
331 if (!sb->s_root) {
332 iput(root);
333 goto out;
335 return 0;
336 out:
337 kfree(sbi);
338 sb->s_fs_info = NULL;
339 return -EINVAL;
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);
349 buf->f_bfree = 0;
350 buf->f_bavail = 0;
351 buf->f_files = le32_to_cpu(CRAMFS_SB(sb)->files);
352 buf->f_ffree = 0;
353 buf->f_namelen = CRAMFS_MAXPATHLEN;
354 return 0;
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;
364 char *buf;
365 unsigned int offset;
366 int copied;
368 /* Offset within the thing. */
369 offset = filp->f_pos;
370 if (offset >= inode->i_size)
371 return 0;
372 /* Directory entries are always 4-byte aligned */
373 if (offset & 3)
374 return -EINVAL;
376 buf = kmalloc(256, GFP_KERNEL);
377 if (!buf)
378 return -ENOMEM;
380 copied = 0;
381 while (offset < inode->i_size) {
382 struct cramfs_inode *de;
383 unsigned long nextoffset;
384 char *name;
385 ino_t ino;
386 mode_t mode;
387 int namelen, error;
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
396 * with zeroes.
398 namelen = CRAMFS_GET_NAMELEN(de) << 2;
399 memcpy(buf, name, namelen);
400 ino = CRAMINO(de);
401 mode = le16_to_cpu(de->mode);
402 mutex_unlock(&read_mutex);
403 nextoffset = offset + sizeof(*de) + namelen;
404 for (;;) {
405 if (!namelen) {
406 kfree(buf);
407 return -EIO;
409 if (buf[namelen-1])
410 break;
411 namelen--;
413 error = filldir(dirent, buf, namelen, offset, ino, mode >> 12);
414 if (error)
415 break;
417 offset = nextoffset;
418 filp->f_pos = offset;
419 copied++;
421 kfree(buf);
422 return 0;
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;
431 int sorted;
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;
437 char *name;
438 int namelen, retval;
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]))
445 break;
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)
452 continue;
454 for (;;) {
455 if (!namelen) {
456 mutex_unlock(&read_mutex);
457 return ERR_PTR(-EIO);
459 if (name[namelen-1])
460 break;
461 namelen--;
463 if (namelen != dentry->d_name.len)
464 continue;
465 retval = memcmp(dentry->d_name.name, name, namelen);
466 if (retval > 0)
467 continue;
468 if (!retval) {
469 struct cramfs_inode entry = *de;
470 mutex_unlock(&read_mutex);
471 d_add(dentry, get_cramfs_inode(dir->i_sb, &entry));
472 return NULL;
474 /* else (retval < 0) */
475 if (sorted)
476 break;
478 mutex_unlock(&read_mutex);
479 d_add(dentry, NULL);
480 return NULL;
483 static int cramfs_readpage(struct file *file, struct page * page)
485 struct inode *inode = page->mapping->host;
486 u32 maxblock, bytes_filled;
487 void *pgdata;
489 maxblock = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
490 bytes_filled = 0;
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);
498 if (page->index)
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);
502 pgdata = kmap(page);
503 if (compr_len == 0)
504 ; /* hole */
505 else {
506 mutex_lock(&read_mutex);
507 bytes_filled = cramfs_uncompress_block(pgdata,
508 PAGE_CACHE_SIZE,
509 cramfs_read(sb, start_offset, compr_len),
510 compr_len);
511 mutex_unlock(&read_mutex);
513 } else
514 pgdata = kmap(page);
515 memset(pgdata + bytes_filled, 0, PAGE_CACHE_SIZE - bytes_filled);
516 kunmap(page);
517 flush_dcache_page(page);
518 SetPageUptodate(page);
519 unlock_page(page);
520 return 0;
523 static const struct address_space_operations cramfs_aops = {
524 .readpage = cramfs_readpage
528 * Our operations:
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,
554 mnt);
557 static struct file_system_type cramfs_fs_type = {
558 .owner = THIS_MODULE,
559 .name = "cramfs",
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)
567 int rv;
569 rv = cramfs_uncompress_init();
570 if (rv < 0)
571 return rv;
572 rv = register_filesystem(&cramfs_fs_type);
573 if (rv < 0)
574 cramfs_uncompress_exit();
575 return rv;
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");