inode.c 13 KB

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