inode.c 14 KB

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