inode.c 15 KB

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