inode.c 14 KB

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