inode.c 14 KB

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