inode.c 14 KB

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