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 + len > BUFFER_SIZE)
  162. continue;
  163. return read_buffers[i] + blk_offset;
  164. }
  165. devsize = mapping->host->i_size >> PAGE_CACHE_SHIFT;
  166. /* Ok, read in BLKS_PER_BUF pages completely first. */
  167. for (i = 0; i < BLKS_PER_BUF; i++) {
  168. struct page *page = NULL;
  169. if (blocknr + i < devsize) {
  170. page = read_mapping_page(mapping, blocknr + i, NULL);
  171. /* synchronous error? */
  172. if (IS_ERR(page))
  173. page = NULL;
  174. }
  175. pages[i] = page;
  176. }
  177. for (i = 0; i < BLKS_PER_BUF; i++) {
  178. struct page *page = pages[i];
  179. if (page) {
  180. wait_on_page_locked(page);
  181. if (!PageUptodate(page)) {
  182. /* asynchronous error */
  183. page_cache_release(page);
  184. pages[i] = NULL;
  185. }
  186. }
  187. }
  188. buffer = next_buffer;
  189. next_buffer = NEXT_BUFFER(buffer);
  190. buffer_blocknr[buffer] = blocknr;
  191. buffer_dev[buffer] = sb;
  192. data = read_buffers[buffer];
  193. for (i = 0; i < BLKS_PER_BUF; i++) {
  194. struct page *page = pages[i];
  195. if (page) {
  196. memcpy(data, kmap(page), PAGE_CACHE_SIZE);
  197. kunmap(page);
  198. page_cache_release(page);
  199. } else
  200. memset(data, 0, PAGE_CACHE_SIZE);
  201. data += PAGE_CACHE_SIZE;
  202. }
  203. return read_buffers[buffer] + offset;
  204. }
  205. static void cramfs_kill_sb(struct super_block *sb)
  206. {
  207. struct cramfs_sb_info *sbi = CRAMFS_SB(sb);
  208. kill_block_super(sb);
  209. kfree(sbi);
  210. }
  211. static int cramfs_remount(struct super_block *sb, int *flags, char *data)
  212. {
  213. sync_filesystem(sb);
  214. *flags |= MS_RDONLY;
  215. return 0;
  216. }
  217. static int cramfs_fill_super(struct super_block *sb, void *data, int silent)
  218. {
  219. int i;
  220. struct cramfs_super super;
  221. unsigned long root_offset;
  222. struct cramfs_sb_info *sbi;
  223. struct inode *root;
  224. sb->s_flags |= MS_RDONLY;
  225. sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
  226. if (!sbi)
  227. return -ENOMEM;
  228. sb->s_fs_info = sbi;
  229. /* Invalidate the read buffers on mount: think disk change.. */
  230. mutex_lock(&read_mutex);
  231. for (i = 0; i < READ_BUFFERS; i++)
  232. buffer_blocknr[i] = -1;
  233. /* Read the first block and get the superblock from it */
  234. memcpy(&super, cramfs_read(sb, 0, sizeof(super)), sizeof(super));
  235. mutex_unlock(&read_mutex);
  236. /* Do sanity checks on the superblock */
  237. if (super.magic != CRAMFS_MAGIC) {
  238. /* check for wrong endianness */
  239. if (super.magic == CRAMFS_MAGIC_WEND) {
  240. if (!silent)
  241. pr_err("wrong endianness\n");
  242. return -EINVAL;
  243. }
  244. /* check at 512 byte offset */
  245. mutex_lock(&read_mutex);
  246. memcpy(&super, cramfs_read(sb, 512, sizeof(super)), sizeof(super));
  247. mutex_unlock(&read_mutex);
  248. if (super.magic != CRAMFS_MAGIC) {
  249. if (super.magic == CRAMFS_MAGIC_WEND && !silent)
  250. pr_err("wrong endianness\n");
  251. else if (!silent)
  252. pr_err("wrong magic\n");
  253. return -EINVAL;
  254. }
  255. }
  256. /* get feature flags first */
  257. if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
  258. pr_err("unsupported filesystem features\n");
  259. return -EINVAL;
  260. }
  261. /* Check that the root inode is in a sane state */
  262. if (!S_ISDIR(super.root.mode)) {
  263. pr_err("root is not a directory\n");
  264. return -EINVAL;
  265. }
  266. /* correct strange, hard-coded permissions of mkcramfs */
  267. super.root.mode |= (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  268. root_offset = super.root.offset << 2;
  269. if (super.flags & CRAMFS_FLAG_FSID_VERSION_2) {
  270. sbi->size = super.size;
  271. sbi->blocks = super.fsid.blocks;
  272. sbi->files = super.fsid.files;
  273. } else {
  274. sbi->size = 1<<28;
  275. sbi->blocks = 0;
  276. sbi->files = 0;
  277. }
  278. sbi->magic = super.magic;
  279. sbi->flags = super.flags;
  280. if (root_offset == 0)
  281. pr_info("empty filesystem");
  282. else if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
  283. ((root_offset != sizeof(struct cramfs_super)) &&
  284. (root_offset != 512 + sizeof(struct cramfs_super))))
  285. {
  286. pr_err("bad root offset %lu\n", root_offset);
  287. return -EINVAL;
  288. }
  289. /* Set it all up.. */
  290. sb->s_op = &cramfs_ops;
  291. root = get_cramfs_inode(sb, &super.root, 0);
  292. if (IS_ERR(root))
  293. return PTR_ERR(root);
  294. sb->s_root = d_make_root(root);
  295. if (!sb->s_root)
  296. return -ENOMEM;
  297. return 0;
  298. }
  299. static int cramfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  300. {
  301. struct super_block *sb = dentry->d_sb;
  302. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  303. buf->f_type = CRAMFS_MAGIC;
  304. buf->f_bsize = PAGE_CACHE_SIZE;
  305. buf->f_blocks = CRAMFS_SB(sb)->blocks;
  306. buf->f_bfree = 0;
  307. buf->f_bavail = 0;
  308. buf->f_files = CRAMFS_SB(sb)->files;
  309. buf->f_ffree = 0;
  310. buf->f_fsid.val[0] = (u32)id;
  311. buf->f_fsid.val[1] = (u32)(id >> 32);
  312. buf->f_namelen = CRAMFS_MAXPATHLEN;
  313. return 0;
  314. }
  315. /*
  316. * Read a cramfs directory entry.
  317. */
  318. static int cramfs_readdir(struct file *file, struct dir_context *ctx)
  319. {
  320. struct inode *inode = file_inode(file);
  321. struct super_block *sb = inode->i_sb;
  322. char *buf;
  323. unsigned int offset;
  324. /* Offset within the thing. */
  325. if (ctx->pos >= inode->i_size)
  326. return 0;
  327. offset = ctx->pos;
  328. /* Directory entries are always 4-byte aligned */
  329. if (offset & 3)
  330. return -EINVAL;
  331. buf = kmalloc(CRAMFS_MAXPATHLEN, GFP_KERNEL);
  332. if (!buf)
  333. return -ENOMEM;
  334. while (offset < inode->i_size) {
  335. struct cramfs_inode *de;
  336. unsigned long nextoffset;
  337. char *name;
  338. ino_t ino;
  339. umode_t mode;
  340. int namelen;
  341. mutex_lock(&read_mutex);
  342. de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+CRAMFS_MAXPATHLEN);
  343. name = (char *)(de+1);
  344. /*
  345. * Namelengths on disk are shifted by two
  346. * and the name padded out to 4-byte boundaries
  347. * with zeroes.
  348. */
  349. namelen = de->namelen << 2;
  350. memcpy(buf, name, namelen);
  351. ino = cramino(de, OFFSET(inode) + offset);
  352. mode = de->mode;
  353. mutex_unlock(&read_mutex);
  354. nextoffset = offset + sizeof(*de) + namelen;
  355. for (;;) {
  356. if (!namelen) {
  357. kfree(buf);
  358. return -EIO;
  359. }
  360. if (buf[namelen-1])
  361. break;
  362. namelen--;
  363. }
  364. if (!dir_emit(ctx, buf, namelen, ino, mode >> 12))
  365. break;
  366. ctx->pos = offset = nextoffset;
  367. }
  368. kfree(buf);
  369. return 0;
  370. }
  371. /*
  372. * Lookup and fill in the inode data..
  373. */
  374. static struct dentry *cramfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  375. {
  376. unsigned int offset = 0;
  377. struct inode *inode = NULL;
  378. int sorted;
  379. mutex_lock(&read_mutex);
  380. sorted = CRAMFS_SB(dir->i_sb)->flags & CRAMFS_FLAG_SORTED_DIRS;
  381. while (offset < dir->i_size) {
  382. struct cramfs_inode *de;
  383. char *name;
  384. int namelen, retval;
  385. int dir_off = OFFSET(dir) + offset;
  386. de = cramfs_read(dir->i_sb, dir_off, sizeof(*de)+CRAMFS_MAXPATHLEN);
  387. name = (char *)(de+1);
  388. /* Try to take advantage of sorted directories */
  389. if (sorted && (dentry->d_name.name[0] < name[0]))
  390. break;
  391. namelen = de->namelen << 2;
  392. offset += sizeof(*de) + namelen;
  393. /* Quick check that the name is roughly the right length */
  394. if (((dentry->d_name.len + 3) & ~3) != namelen)
  395. continue;
  396. for (;;) {
  397. if (!namelen) {
  398. inode = ERR_PTR(-EIO);
  399. goto out;
  400. }
  401. if (name[namelen-1])
  402. break;
  403. namelen--;
  404. }
  405. if (namelen != dentry->d_name.len)
  406. continue;
  407. retval = memcmp(dentry->d_name.name, name, namelen);
  408. if (retval > 0)
  409. continue;
  410. if (!retval) {
  411. inode = get_cramfs_inode(dir->i_sb, de, dir_off);
  412. break;
  413. }
  414. /* else (retval < 0) */
  415. if (sorted)
  416. break;
  417. }
  418. out:
  419. mutex_unlock(&read_mutex);
  420. if (IS_ERR(inode))
  421. return ERR_CAST(inode);
  422. d_add(dentry, inode);
  423. return NULL;
  424. }
  425. static int cramfs_readpage(struct file *file, struct page *page)
  426. {
  427. struct inode *inode = page->mapping->host;
  428. u32 maxblock;
  429. int bytes_filled;
  430. void *pgdata;
  431. maxblock = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  432. bytes_filled = 0;
  433. pgdata = kmap(page);
  434. if (page->index < maxblock) {
  435. struct super_block *sb = inode->i_sb;
  436. u32 blkptr_offset = OFFSET(inode) + page->index*4;
  437. u32 start_offset, compr_len;
  438. start_offset = OFFSET(inode) + maxblock*4;
  439. mutex_lock(&read_mutex);
  440. if (page->index)
  441. start_offset = *(u32 *) cramfs_read(sb, blkptr_offset-4,
  442. 4);
  443. compr_len = (*(u32 *) cramfs_read(sb, blkptr_offset, 4) -
  444. start_offset);
  445. mutex_unlock(&read_mutex);
  446. if (compr_len == 0)
  447. ; /* hole */
  448. else if (unlikely(compr_len > (PAGE_CACHE_SIZE << 1))) {
  449. pr_err("bad compressed blocksize %u\n",
  450. compr_len);
  451. goto err;
  452. } else {
  453. mutex_lock(&read_mutex);
  454. bytes_filled = cramfs_uncompress_block(pgdata,
  455. PAGE_CACHE_SIZE,
  456. cramfs_read(sb, start_offset, compr_len),
  457. compr_len);
  458. mutex_unlock(&read_mutex);
  459. if (unlikely(bytes_filled < 0))
  460. goto err;
  461. }
  462. }
  463. memset(pgdata + bytes_filled, 0, PAGE_CACHE_SIZE - bytes_filled);
  464. flush_dcache_page(page);
  465. kunmap(page);
  466. SetPageUptodate(page);
  467. unlock_page(page);
  468. return 0;
  469. err:
  470. kunmap(page);
  471. ClearPageUptodate(page);
  472. SetPageError(page);
  473. unlock_page(page);
  474. return 0;
  475. }
  476. static const struct address_space_operations cramfs_aops = {
  477. .readpage = cramfs_readpage
  478. };
  479. /*
  480. * Our operations:
  481. */
  482. /*
  483. * A directory can only readdir
  484. */
  485. static const struct file_operations cramfs_directory_operations = {
  486. .llseek = generic_file_llseek,
  487. .read = generic_read_dir,
  488. .iterate = cramfs_readdir,
  489. };
  490. static const struct inode_operations cramfs_dir_inode_operations = {
  491. .lookup = cramfs_lookup,
  492. };
  493. static const struct super_operations cramfs_ops = {
  494. .remount_fs = cramfs_remount,
  495. .statfs = cramfs_statfs,
  496. };
  497. static struct dentry *cramfs_mount(struct file_system_type *fs_type,
  498. int flags, const char *dev_name, void *data)
  499. {
  500. return mount_bdev(fs_type, flags, dev_name, data, cramfs_fill_super);
  501. }
  502. static struct file_system_type cramfs_fs_type = {
  503. .owner = THIS_MODULE,
  504. .name = "cramfs",
  505. .mount = cramfs_mount,
  506. .kill_sb = cramfs_kill_sb,
  507. .fs_flags = FS_REQUIRES_DEV,
  508. };
  509. MODULE_ALIAS_FS("cramfs");
  510. static int __init init_cramfs_fs(void)
  511. {
  512. int rv;
  513. rv = cramfs_uncompress_init();
  514. if (rv < 0)
  515. return rv;
  516. rv = register_filesystem(&cramfs_fs_type);
  517. if (rv < 0)
  518. cramfs_uncompress_exit();
  519. return rv;
  520. }
  521. static void __exit exit_cramfs_fs(void)
  522. {
  523. cramfs_uncompress_exit();
  524. unregister_filesystem(&cramfs_fs_type);
  525. }
  526. module_init(init_cramfs_fs)
  527. module_exit(exit_cramfs_fs)
  528. MODULE_LICENSE("GPL");