inode.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 <asm/semaphore.h>
  24. #include <asm/uaccess.h>
  25. static struct super_operations cramfs_ops;
  26. static struct inode_operations cramfs_dir_inode_operations;
  27. static struct file_operations cramfs_directory_operations;
  28. static struct address_space_operations cramfs_aops;
  29. static DECLARE_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)->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. if (inode->i_ino != CRAMINO(cramfs_inode))
  38. return 0; /* does not match */
  39. if (inode->i_ino != 1)
  40. return 1;
  41. /* all empty directories, char, block, pipe, and sock, share inode #1 */
  42. if ((inode->i_mode != cramfs_inode->mode) ||
  43. (inode->i_gid != cramfs_inode->gid) ||
  44. (inode->i_uid != cramfs_inode->uid))
  45. return 0; /* does not match */
  46. if ((S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) &&
  47. (inode->i_rdev != old_decode_dev(cramfs_inode->size)))
  48. return 0; /* does not match */
  49. return 1; /* matches */
  50. }
  51. static int cramfs_iget5_set(struct inode *inode, void *opaque)
  52. {
  53. struct cramfs_inode *cramfs_inode = opaque;
  54. inode->i_ino = CRAMINO(cramfs_inode);
  55. return 0;
  56. }
  57. static struct inode *get_cramfs_inode(struct super_block *sb,
  58. struct cramfs_inode * cramfs_inode)
  59. {
  60. struct inode *inode = iget5_locked(sb, CRAMINO(cramfs_inode),
  61. cramfs_iget5_test, cramfs_iget5_set,
  62. cramfs_inode);
  63. static struct timespec zerotime;
  64. if (inode && (inode->i_state & I_NEW)) {
  65. inode->i_mode = cramfs_inode->mode;
  66. inode->i_uid = cramfs_inode->uid;
  67. inode->i_size = cramfs_inode->size;
  68. inode->i_blocks = (cramfs_inode->size - 1) / 512 + 1;
  69. inode->i_blksize = PAGE_CACHE_SIZE;
  70. inode->i_gid = cramfs_inode->gid;
  71. /* Struct copy intentional */
  72. inode->i_mtime = inode->i_atime = inode->i_ctime = zerotime;
  73. inode->i_ino = CRAMINO(cramfs_inode);
  74. /* inode->i_nlink is left 1 - arguably wrong for directories,
  75. but it's the best we can do without reading the directory
  76. contents. 1 yields the right result in GNU find, even
  77. without -noleaf option. */
  78. if (S_ISREG(inode->i_mode)) {
  79. inode->i_fop = &generic_ro_fops;
  80. inode->i_data.a_ops = &cramfs_aops;
  81. } else if (S_ISDIR(inode->i_mode)) {
  82. inode->i_op = &cramfs_dir_inode_operations;
  83. inode->i_fop = &cramfs_directory_operations;
  84. } else if (S_ISLNK(inode->i_mode)) {
  85. inode->i_op = &page_symlink_inode_operations;
  86. inode->i_data.a_ops = &cramfs_aops;
  87. } else {
  88. inode->i_size = 0;
  89. inode->i_blocks = 0;
  90. init_special_inode(inode, inode->i_mode,
  91. old_decode_dev(cramfs_inode->size));
  92. }
  93. unlock_new_inode(inode);
  94. }
  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, unread;
  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. unread = 0;
  154. for (i = 0; i < BLKS_PER_BUF; i++) {
  155. struct page *page = NULL;
  156. if (blocknr + i < devsize) {
  157. page = read_cache_page(mapping, blocknr + i,
  158. (filler_t *)mapping->a_ops->readpage,
  159. NULL);
  160. /* synchronous error? */
  161. if (IS_ERR(page))
  162. page = NULL;
  163. }
  164. pages[i] = page;
  165. }
  166. for (i = 0; i < BLKS_PER_BUF; i++) {
  167. struct page *page = pages[i];
  168. if (page) {
  169. wait_on_page_locked(page);
  170. if (!PageUptodate(page)) {
  171. /* asynchronous error */
  172. page_cache_release(page);
  173. pages[i] = NULL;
  174. }
  175. }
  176. }
  177. buffer = next_buffer;
  178. next_buffer = NEXT_BUFFER(buffer);
  179. buffer_blocknr[buffer] = blocknr;
  180. buffer_dev[buffer] = sb;
  181. data = read_buffers[buffer];
  182. for (i = 0; i < BLKS_PER_BUF; i++) {
  183. struct page *page = pages[i];
  184. if (page) {
  185. memcpy(data, kmap(page), PAGE_CACHE_SIZE);
  186. kunmap(page);
  187. page_cache_release(page);
  188. } else
  189. memset(data, 0, PAGE_CACHE_SIZE);
  190. data += PAGE_CACHE_SIZE;
  191. }
  192. return read_buffers[buffer] + offset;
  193. }
  194. static void cramfs_put_super(struct super_block *sb)
  195. {
  196. kfree(sb->s_fs_info);
  197. sb->s_fs_info = NULL;
  198. }
  199. static int cramfs_remount(struct super_block *sb, int *flags, char *data)
  200. {
  201. *flags |= MS_RDONLY;
  202. return 0;
  203. }
  204. static int cramfs_fill_super(struct super_block *sb, void *data, int silent)
  205. {
  206. int i;
  207. struct cramfs_super super;
  208. unsigned long root_offset;
  209. struct cramfs_sb_info *sbi;
  210. struct inode *root;
  211. sb->s_flags |= MS_RDONLY;
  212. sbi = kmalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
  213. if (!sbi)
  214. return -ENOMEM;
  215. sb->s_fs_info = sbi;
  216. memset(sbi, 0, sizeof(struct cramfs_sb_info));
  217. /* Invalidate the read buffers on mount: think disk change.. */
  218. down(&read_mutex);
  219. for (i = 0; i < READ_BUFFERS; i++)
  220. buffer_blocknr[i] = -1;
  221. /* Read the first block and get the superblock from it */
  222. memcpy(&super, cramfs_read(sb, 0, sizeof(super)), sizeof(super));
  223. up(&read_mutex);
  224. /* Do sanity checks on the superblock */
  225. if (super.magic != CRAMFS_MAGIC) {
  226. /* check at 512 byte offset */
  227. down(&read_mutex);
  228. memcpy(&super, cramfs_read(sb, 512, sizeof(super)), sizeof(super));
  229. up(&read_mutex);
  230. if (super.magic != CRAMFS_MAGIC) {
  231. if (!silent)
  232. printk(KERN_ERR "cramfs: wrong magic\n");
  233. goto out;
  234. }
  235. }
  236. /* get feature flags first */
  237. if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
  238. printk(KERN_ERR "cramfs: unsupported filesystem features\n");
  239. goto out;
  240. }
  241. /* Check that the root inode is in a sane state */
  242. if (!S_ISDIR(super.root.mode)) {
  243. printk(KERN_ERR "cramfs: root is not a directory\n");
  244. goto out;
  245. }
  246. root_offset = super.root.offset << 2;
  247. if (super.flags & CRAMFS_FLAG_FSID_VERSION_2) {
  248. sbi->size=super.size;
  249. sbi->blocks=super.fsid.blocks;
  250. sbi->files=super.fsid.files;
  251. } else {
  252. sbi->size=1<<28;
  253. sbi->blocks=0;
  254. sbi->files=0;
  255. }
  256. sbi->magic=super.magic;
  257. sbi->flags=super.flags;
  258. if (root_offset == 0)
  259. printk(KERN_INFO "cramfs: empty filesystem");
  260. else if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
  261. ((root_offset != sizeof(struct cramfs_super)) &&
  262. (root_offset != 512 + sizeof(struct cramfs_super))))
  263. {
  264. printk(KERN_ERR "cramfs: bad root offset %lu\n", root_offset);
  265. goto out;
  266. }
  267. /* Set it all up.. */
  268. sb->s_op = &cramfs_ops;
  269. root = get_cramfs_inode(sb, &super.root);
  270. if (!root)
  271. goto out;
  272. sb->s_root = d_alloc_root(root);
  273. if (!sb->s_root) {
  274. iput(root);
  275. goto out;
  276. }
  277. return 0;
  278. out:
  279. kfree(sbi);
  280. sb->s_fs_info = NULL;
  281. return -EINVAL;
  282. }
  283. static int cramfs_statfs(struct super_block *sb, struct kstatfs *buf)
  284. {
  285. buf->f_type = CRAMFS_MAGIC;
  286. buf->f_bsize = PAGE_CACHE_SIZE;
  287. buf->f_blocks = CRAMFS_SB(sb)->blocks;
  288. buf->f_bfree = 0;
  289. buf->f_bavail = 0;
  290. buf->f_files = CRAMFS_SB(sb)->files;
  291. buf->f_ffree = 0;
  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_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(256, 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. down(&read_mutex);
  324. de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+256);
  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. up(&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. down(&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)+256);
  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. up(&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. up(&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. up(&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, bytes_filled;
  411. void *pgdata;
  412. maxblock = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  413. bytes_filled = 0;
  414. if (page->index < maxblock) {
  415. struct super_block *sb = inode->i_sb;
  416. u32 blkptr_offset = OFFSET(inode) + page->index*4;
  417. u32 start_offset, compr_len;
  418. start_offset = OFFSET(inode) + maxblock*4;
  419. down(&read_mutex);
  420. if (page->index)
  421. start_offset = *(u32 *) cramfs_read(sb, blkptr_offset-4, 4);
  422. compr_len = (*(u32 *) cramfs_read(sb, blkptr_offset, 4) - start_offset);
  423. up(&read_mutex);
  424. pgdata = kmap(page);
  425. if (compr_len == 0)
  426. ; /* hole */
  427. else {
  428. down(&read_mutex);
  429. bytes_filled = cramfs_uncompress_block(pgdata,
  430. PAGE_CACHE_SIZE,
  431. cramfs_read(sb, start_offset, compr_len),
  432. compr_len);
  433. up(&read_mutex);
  434. }
  435. } else
  436. pgdata = kmap(page);
  437. memset(pgdata + bytes_filled, 0, PAGE_CACHE_SIZE - bytes_filled);
  438. kunmap(page);
  439. flush_dcache_page(page);
  440. SetPageUptodate(page);
  441. unlock_page(page);
  442. return 0;
  443. }
  444. static struct address_space_operations cramfs_aops = {
  445. .readpage = cramfs_readpage
  446. };
  447. /*
  448. * Our operations:
  449. */
  450. /*
  451. * A directory can only readdir
  452. */
  453. static struct file_operations cramfs_directory_operations = {
  454. .llseek = generic_file_llseek,
  455. .read = generic_read_dir,
  456. .readdir = cramfs_readdir,
  457. };
  458. static struct inode_operations cramfs_dir_inode_operations = {
  459. .lookup = cramfs_lookup,
  460. };
  461. static struct super_operations cramfs_ops = {
  462. .put_super = cramfs_put_super,
  463. .remount_fs = cramfs_remount,
  464. .statfs = cramfs_statfs,
  465. };
  466. static struct super_block *cramfs_get_sb(struct file_system_type *fs_type,
  467. int flags, const char *dev_name, void *data)
  468. {
  469. return get_sb_bdev(fs_type, flags, dev_name, data, cramfs_fill_super);
  470. }
  471. static struct file_system_type cramfs_fs_type = {
  472. .owner = THIS_MODULE,
  473. .name = "cramfs",
  474. .get_sb = cramfs_get_sb,
  475. .kill_sb = kill_block_super,
  476. .fs_flags = FS_REQUIRES_DEV,
  477. };
  478. static int __init init_cramfs_fs(void)
  479. {
  480. cramfs_uncompress_init();
  481. return register_filesystem(&cramfs_fs_type);
  482. }
  483. static void __exit exit_cramfs_fs(void)
  484. {
  485. cramfs_uncompress_exit();
  486. unregister_filesystem(&cramfs_fs_type);
  487. }
  488. module_init(init_cramfs_fs)
  489. module_exit(exit_cramfs_fs)
  490. MODULE_LICENSE("GPL");