inode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. * These are the VFS interfaces to the compressed rom filesystem.
  10. * The actual compression is based on zlib, see the other files.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/fs.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/init.h>
  16. #include <linux/string.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/slab.h>
  19. #include <linux/vfs.h>
  20. #include <linux/mutex.h>
  21. #include <uapi/linux/cramfs_fs.h>
  22. #include <asm/uaccess.h>
  23. #include "internal.h"
  24. /*
  25. * cramfs super-block data in memory
  26. */
  27. struct cramfs_sb_info {
  28. unsigned long magic;
  29. unsigned long size;
  30. unsigned long blocks;
  31. unsigned long files;
  32. unsigned long flags;
  33. };
  34. static inline struct cramfs_sb_info *CRAMFS_SB(struct super_block *sb)
  35. {
  36. return sb->s_fs_info;
  37. }
  38. static const struct super_operations cramfs_ops;
  39. static const struct inode_operations cramfs_dir_inode_operations;
  40. static const struct file_operations cramfs_directory_operations;
  41. static const struct address_space_operations cramfs_aops;
  42. static DEFINE_MUTEX(read_mutex);
  43. /* These macros may change in future, to provide better st_ino semantics. */
  44. #define OFFSET(x) ((x)->i_ino)
  45. static unsigned long cramino(const struct cramfs_inode *cino, unsigned int offset)
  46. {
  47. if (!cino->offset)
  48. return offset + 1;
  49. if (!cino->size)
  50. return offset + 1;
  51. /*
  52. * The file mode test fixes buggy mkcramfs implementations where
  53. * cramfs_inode->offset is set to a non zero value for entries
  54. * which did not contain data, like devices node and fifos.
  55. */
  56. switch (cino->mode & S_IFMT) {
  57. case S_IFREG:
  58. case S_IFDIR:
  59. case S_IFLNK:
  60. return cino->offset << 2;
  61. default:
  62. break;
  63. }
  64. return offset + 1;
  65. }
  66. static struct inode *get_cramfs_inode(struct super_block *sb,
  67. const struct cramfs_inode *cramfs_inode, unsigned int offset)
  68. {
  69. struct inode *inode;
  70. static struct timespec zerotime;
  71. inode = iget_locked(sb, cramino(cramfs_inode, offset));
  72. if (!inode)
  73. return ERR_PTR(-ENOMEM);
  74. if (!(inode->i_state & I_NEW))
  75. return inode;
  76. switch (cramfs_inode->mode & S_IFMT) {
  77. case S_IFREG:
  78. inode->i_fop = &generic_ro_fops;
  79. inode->i_data.a_ops = &cramfs_aops;
  80. break;
  81. case S_IFDIR:
  82. inode->i_op = &cramfs_dir_inode_operations;
  83. inode->i_fop = &cramfs_directory_operations;
  84. break;
  85. case S_IFLNK:
  86. inode->i_op = &page_symlink_inode_operations;
  87. inode->i_data.a_ops = &cramfs_aops;
  88. break;
  89. default:
  90. init_special_inode(inode, cramfs_inode->mode,
  91. old_decode_dev(cramfs_inode->size));
  92. }
  93. inode->i_mode = cramfs_inode->mode;
  94. i_uid_write(inode, cramfs_inode->uid);
  95. i_gid_write(inode, cramfs_inode->gid);
  96. /* if the lower 2 bits are zero, the inode contains data */
  97. if (!(inode->i_ino & 3)) {
  98. inode->i_size = cramfs_inode->size;
  99. inode->i_blocks = (cramfs_inode->size - 1) / 512 + 1;
  100. }
  101. /* Struct copy intentional */
  102. inode->i_mtime = inode->i_atime = inode->i_ctime = zerotime;
  103. /* inode->i_nlink is left 1 - arguably wrong for directories,
  104. but it's the best we can do without reading the directory
  105. contents. 1 yields the right result in GNU find, even
  106. without -noleaf option. */
  107. unlock_new_inode(inode);
  108. return inode;
  109. }
  110. /*
  111. * We have our own block cache: don't fill up the buffer cache
  112. * with the rom-image, because the way the filesystem is set
  113. * up the accesses should be fairly regular and cached in the
  114. * page cache and dentry tree anyway..
  115. *
  116. * This also acts as a way to guarantee contiguous areas of up to
  117. * BLKS_PER_BUF*PAGE_CACHE_SIZE, so that the caller doesn't need to
  118. * worry about end-of-buffer issues even when decompressing a full
  119. * page cache.
  120. */
  121. #define READ_BUFFERS (2)
  122. /* NEXT_BUFFER(): Loop over [0..(READ_BUFFERS-1)]. */
  123. #define NEXT_BUFFER(_ix) ((_ix) ^ 1)
  124. /*
  125. * BLKS_PER_BUF_SHIFT should be at least 2 to allow for "compressed"
  126. * data that takes up more space than the original and with unlucky
  127. * alignment.
  128. */
  129. #define BLKS_PER_BUF_SHIFT (2)
  130. #define BLKS_PER_BUF (1 << BLKS_PER_BUF_SHIFT)
  131. #define BUFFER_SIZE (BLKS_PER_BUF*PAGE_CACHE_SIZE)
  132. static unsigned char read_buffers[READ_BUFFERS][BUFFER_SIZE];
  133. static unsigned buffer_blocknr[READ_BUFFERS];
  134. static struct super_block * buffer_dev[READ_BUFFERS];
  135. static int next_buffer;
  136. /*
  137. * Returns a pointer to a buffer containing at least LEN bytes of
  138. * filesystem starting at byte offset OFFSET into the filesystem.
  139. */
  140. static void *cramfs_read(struct super_block *sb, unsigned int offset, unsigned int len)
  141. {
  142. struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
  143. struct page *pages[BLKS_PER_BUF];
  144. unsigned i, blocknr, buffer;
  145. unsigned long devsize;
  146. char *data;
  147. if (!len)
  148. return NULL;
  149. blocknr = offset >> PAGE_CACHE_SHIFT;
  150. offset &= PAGE_CACHE_SIZE - 1;
  151. /* Check if an existing buffer already has the data.. */
  152. for (i = 0; i < READ_BUFFERS; i++) {
  153. unsigned int blk_offset;
  154. if (buffer_dev[i] != sb)
  155. continue;
  156. if (blocknr < buffer_blocknr[i])
  157. continue;
  158. blk_offset = (blocknr - buffer_blocknr[i]) << PAGE_CACHE_SHIFT;
  159. blk_offset += offset;
  160. if (blk_offset + len > BUFFER_SIZE)
  161. continue;
  162. return read_buffers[i] + blk_offset;
  163. }
  164. devsize = mapping->host->i_size >> PAGE_CACHE_SHIFT;
  165. /* Ok, read in BLKS_PER_BUF pages completely first. */
  166. for (i = 0; i < BLKS_PER_BUF; i++) {
  167. struct page *page = NULL;
  168. if (blocknr + i < devsize) {
  169. page = read_mapping_page(mapping, blocknr + i, NULL);
  170. /* synchronous error? */
  171. if (IS_ERR(page))
  172. page = NULL;
  173. }
  174. pages[i] = page;
  175. }
  176. for (i = 0; i < BLKS_PER_BUF; i++) {
  177. struct page *page = pages[i];
  178. if (page) {
  179. wait_on_page_locked(page);
  180. if (!PageUptodate(page)) {
  181. /* asynchronous error */
  182. page_cache_release(page);
  183. pages[i] = NULL;
  184. }
  185. }
  186. }
  187. buffer = next_buffer;
  188. next_buffer = NEXT_BUFFER(buffer);
  189. buffer_blocknr[buffer] = blocknr;
  190. buffer_dev[buffer] = sb;
  191. data = read_buffers[buffer];
  192. for (i = 0; i < BLKS_PER_BUF; i++) {
  193. struct page *page = pages[i];
  194. if (page) {
  195. memcpy(data, kmap(page), PAGE_CACHE_SIZE);
  196. kunmap(page);
  197. page_cache_release(page);
  198. } else
  199. memset(data, 0, PAGE_CACHE_SIZE);
  200. data += PAGE_CACHE_SIZE;
  201. }
  202. return read_buffers[buffer] + offset;
  203. }
  204. static void cramfs_kill_sb(struct super_block *sb)
  205. {
  206. struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
  207. kill_block_super(sb);
  208. kfree(sbi);
  209. }
  210. static int cramfs_remount(struct super_block *sb, int *flags, char *data)
  211. {
  212. sync_filesystem(sb);
  213. *flags |= MS_RDONLY;
  214. return 0;
  215. }
  216. static int cramfs_fill_super(struct super_block *sb, void *data, int silent)
  217. {
  218. int i;
  219. struct cramfs_super super;
  220. unsigned long root_offset;
  221. struct cramfs_sb_info *sbi;
  222. struct inode *root;
  223. sb->s_flags |= MS_RDONLY;
  224. sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
  225. if (!sbi)
  226. return -ENOMEM;
  227. sb->s_fs_info = sbi;
  228. /* Invalidate the read buffers on mount: think disk change.. */
  229. mutex_lock(&read_mutex);
  230. for (i = 0; i < READ_BUFFERS; i++)
  231. buffer_blocknr[i] = -1;
  232. /* Read the first block and get the superblock from it */
  233. memcpy(&super, cramfs_read(sb, 0, sizeof(super)), sizeof(super));
  234. mutex_unlock(&read_mutex);
  235. /* Do sanity checks on the superblock */
  236. if (super.magic != CRAMFS_MAGIC) {
  237. /* check for wrong endianness */
  238. if (super.magic == CRAMFS_MAGIC_WEND) {
  239. if (!silent)
  240. printk(KERN_ERR "cramfs: wrong endianness\n");
  241. return -EINVAL;
  242. }
  243. /* check at 512 byte offset */
  244. mutex_lock(&read_mutex);
  245. memcpy(&super, cramfs_read(sb, 512, sizeof(super)), sizeof(super));
  246. mutex_unlock(&read_mutex);
  247. if (super.magic != CRAMFS_MAGIC) {
  248. if (super.magic == CRAMFS_MAGIC_WEND && !silent)
  249. printk(KERN_ERR "cramfs: wrong endianness\n");
  250. else if (!silent)
  251. printk(KERN_ERR "cramfs: wrong magic\n");
  252. return -EINVAL;
  253. }
  254. }
  255. /* get feature flags first */
  256. if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
  257. printk(KERN_ERR "cramfs: unsupported filesystem features\n");
  258. return -EINVAL;
  259. }
  260. /* Check that the root inode is in a sane state */
  261. if (!S_ISDIR(super.root.mode)) {
  262. printk(KERN_ERR "cramfs: root is not a directory\n");
  263. return -EINVAL;
  264. }
  265. /* correct strange, hard-coded permissions of mkcramfs */
  266. super.root.mode |= (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  267. root_offset = super.root.offset << 2;
  268. if (super.flags & CRAMFS_FLAG_FSID_VERSION_2) {
  269. sbi->size=super.size;
  270. sbi->blocks=super.fsid.blocks;
  271. sbi->files=super.fsid.files;
  272. } else {
  273. sbi->size=1<<28;
  274. sbi->blocks=0;
  275. sbi->files=0;
  276. }
  277. sbi->magic=super.magic;
  278. sbi->flags=super.flags;
  279. if (root_offset == 0)
  280. printk(KERN_INFO "cramfs: empty filesystem");
  281. else if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
  282. ((root_offset != sizeof(struct cramfs_super)) &&
  283. (root_offset != 512 + sizeof(struct cramfs_super))))
  284. {
  285. printk(KERN_ERR "cramfs: bad root offset %lu\n", root_offset);
  286. return -EINVAL;
  287. }
  288. /* Set it all up.. */
  289. sb->s_op = &cramfs_ops;
  290. root = get_cramfs_inode(sb, &super.root, 0);
  291. if (IS_ERR(root))
  292. return PTR_ERR(root);
  293. sb->s_root = d_make_root(root);
  294. if (!sb->s_root)
  295. return -ENOMEM;
  296. return 0;
  297. }
  298. static int cramfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  299. {
  300. struct super_block *sb = dentry->d_sb;
  301. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  302. buf->f_type = CRAMFS_MAGIC;
  303. buf->f_bsize = PAGE_CACHE_SIZE;
  304. buf->f_blocks = CRAMFS_SB(sb)->blocks;
  305. buf->f_bfree = 0;
  306. buf->f_bavail = 0;
  307. buf->f_files = CRAMFS_SB(sb)->files;
  308. buf->f_ffree = 0;
  309. buf->f_fsid.val[0] = (u32)id;
  310. buf->f_fsid.val[1] = (u32)(id >> 32);
  311. buf->f_namelen = CRAMFS_MAXPATHLEN;
  312. return 0;
  313. }
  314. /*
  315. * Read a cramfs directory entry.
  316. */
  317. static int cramfs_readdir(struct file *file, struct dir_context *ctx)
  318. {
  319. struct inode *inode = file_inode(file);
  320. struct super_block *sb = inode->i_sb;
  321. char *buf;
  322. unsigned int offset;
  323. /* Offset within the thing. */
  324. if (ctx->pos >= inode->i_size)
  325. return 0;
  326. offset = ctx->pos;
  327. /* Directory entries are always 4-byte aligned */
  328. if (offset & 3)
  329. return -EINVAL;
  330. buf = kmalloc(CRAMFS_MAXPATHLEN, GFP_KERNEL);
  331. if (!buf)
  332. return -ENOMEM;
  333. while (offset < inode->i_size) {
  334. struct cramfs_inode *de;
  335. unsigned long nextoffset;
  336. char *name;
  337. ino_t ino;
  338. umode_t mode;
  339. int namelen;
  340. mutex_lock(&read_mutex);
  341. de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+CRAMFS_MAXPATHLEN);
  342. name = (char *)(de+1);
  343. /*
  344. * Namelengths on disk are shifted by two
  345. * and the name padded out to 4-byte boundaries
  346. * with zeroes.
  347. */
  348. namelen = de->namelen << 2;
  349. memcpy(buf, name, namelen);
  350. ino = cramino(de, OFFSET(inode) + offset);
  351. mode = de->mode;
  352. mutex_unlock(&read_mutex);
  353. nextoffset = offset + sizeof(*de) + namelen;
  354. for (;;) {
  355. if (!namelen) {
  356. kfree(buf);
  357. return -EIO;
  358. }
  359. if (buf[namelen-1])
  360. break;
  361. namelen--;
  362. }
  363. if (!dir_emit(ctx, buf, namelen, ino, mode >> 12))
  364. break;
  365. ctx->pos = offset = nextoffset;
  366. }
  367. kfree(buf);
  368. return 0;
  369. }
  370. /*
  371. * Lookup and fill in the inode data..
  372. */
  373. static struct dentry * cramfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  374. {
  375. unsigned int offset = 0;
  376. struct inode *inode = NULL;
  377. int sorted;
  378. mutex_lock(&read_mutex);
  379. sorted = CRAMFS_SB(dir->i_sb)->flags & CRAMFS_FLAG_SORTED_DIRS;
  380. while (offset < dir->i_size) {
  381. struct cramfs_inode *de;
  382. char *name;
  383. int namelen, retval;
  384. int dir_off = OFFSET(dir) + offset;
  385. de = cramfs_read(dir->i_sb, dir_off, sizeof(*de)+CRAMFS_MAXPATHLEN);
  386. name = (char *)(de+1);
  387. /* Try to take advantage of sorted directories */
  388. if (sorted && (dentry->d_name.name[0] < name[0]))
  389. break;
  390. namelen = de->namelen << 2;
  391. offset += sizeof(*de) + namelen;
  392. /* Quick check that the name is roughly the right length */
  393. if (((dentry->d_name.len + 3) & ~3) != namelen)
  394. continue;
  395. for (;;) {
  396. if (!namelen) {
  397. inode = ERR_PTR(-EIO);
  398. goto out;
  399. }
  400. if (name[namelen-1])
  401. break;
  402. namelen--;
  403. }
  404. if (namelen != dentry->d_name.len)
  405. continue;
  406. retval = memcmp(dentry->d_name.name, name, namelen);
  407. if (retval > 0)
  408. continue;
  409. if (!retval) {
  410. inode = get_cramfs_inode(dir->i_sb, de, dir_off);
  411. break;
  412. }
  413. /* else (retval < 0) */
  414. if (sorted)
  415. break;
  416. }
  417. out:
  418. mutex_unlock(&read_mutex);
  419. if (IS_ERR(inode))
  420. return ERR_CAST(inode);
  421. d_add(dentry, inode);
  422. return NULL;
  423. }
  424. static int cramfs_readpage(struct file *file, struct page * page)
  425. {
  426. struct inode *inode = page->mapping->host;
  427. u32 maxblock;
  428. int bytes_filled;
  429. void *pgdata;
  430. maxblock = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  431. bytes_filled = 0;
  432. pgdata = kmap(page);
  433. if (page->index < maxblock) {
  434. struct super_block *sb = inode->i_sb;
  435. u32 blkptr_offset = OFFSET(inode) + page->index*4;
  436. u32 start_offset, compr_len;
  437. start_offset = OFFSET(inode) + maxblock*4;
  438. mutex_lock(&read_mutex);
  439. if (page->index)
  440. start_offset = *(u32 *) cramfs_read(sb, blkptr_offset-4,
  441. 4);
  442. compr_len = (*(u32 *) cramfs_read(sb, blkptr_offset, 4) -
  443. start_offset);
  444. mutex_unlock(&read_mutex);
  445. if (compr_len == 0)
  446. ; /* hole */
  447. else if (unlikely(compr_len > (PAGE_CACHE_SIZE << 1))) {
  448. pr_err("cramfs: bad compressed blocksize %u\n",
  449. compr_len);
  450. goto err;
  451. } else {
  452. mutex_lock(&read_mutex);
  453. bytes_filled = cramfs_uncompress_block(pgdata,
  454. PAGE_CACHE_SIZE,
  455. cramfs_read(sb, start_offset, compr_len),
  456. compr_len);
  457. mutex_unlock(&read_mutex);
  458. if (unlikely(bytes_filled < 0))
  459. goto err;
  460. }
  461. }
  462. memset(pgdata + bytes_filled, 0, PAGE_CACHE_SIZE - bytes_filled);
  463. flush_dcache_page(page);
  464. kunmap(page);
  465. SetPageUptodate(page);
  466. unlock_page(page);
  467. return 0;
  468. err:
  469. kunmap(page);
  470. ClearPageUptodate(page);
  471. SetPageError(page);
  472. unlock_page(page);
  473. return 0;
  474. }
  475. static const struct address_space_operations cramfs_aops = {
  476. .readpage = cramfs_readpage
  477. };
  478. /*
  479. * Our operations:
  480. */
  481. /*
  482. * A directory can only readdir
  483. */
  484. static const struct file_operations cramfs_directory_operations = {
  485. .llseek = generic_file_llseek,
  486. .read = generic_read_dir,
  487. .iterate = cramfs_readdir,
  488. };
  489. static const struct inode_operations cramfs_dir_inode_operations = {
  490. .lookup = cramfs_lookup,
  491. };
  492. static const struct super_operations cramfs_ops = {
  493. .remount_fs = cramfs_remount,
  494. .statfs = cramfs_statfs,
  495. };
  496. static struct dentry *cramfs_mount(struct file_system_type *fs_type,
  497. int flags, const char *dev_name, void *data)
  498. {
  499. return mount_bdev(fs_type, flags, dev_name, data, cramfs_fill_super);
  500. }
  501. static struct file_system_type cramfs_fs_type = {
  502. .owner = THIS_MODULE,
  503. .name = "cramfs",
  504. .mount = cramfs_mount,
  505. .kill_sb = cramfs_kill_sb,
  506. .fs_flags = FS_REQUIRES_DEV,
  507. };
  508. MODULE_ALIAS_FS("cramfs");
  509. static int __init init_cramfs_fs(void)
  510. {
  511. int rv;
  512. rv = cramfs_uncompress_init();
  513. if (rv < 0)
  514. return rv;
  515. rv = register_filesystem(&cramfs_fs_type);
  516. if (rv < 0)
  517. cramfs_uncompress_exit();
  518. return rv;
  519. }
  520. static void __exit exit_cramfs_fs(void)
  521. {
  522. cramfs_uncompress_exit();
  523. unregister_filesystem(&cramfs_fs_type);
  524. }
  525. module_init(init_cramfs_fs)
  526. module_exit(exit_cramfs_fs)
  527. MODULE_LICENSE("GPL");