inode.c 14 KB

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