inode.c 14 KB

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