inode.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Squashfs - a compressed read only filesystem for Linux
  3. *
  4. * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
  5. * Phillip Lougher <phillip@lougher.demon.co.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. *
  21. * inode.c
  22. */
  23. /*
  24. * This file implements code to create and read inodes from disk.
  25. *
  26. * Inodes in Squashfs are identified by a 48-bit inode which encodes the
  27. * location of the compressed metadata block containing the inode, and the byte
  28. * offset into that block where the inode is placed (<block, offset>).
  29. *
  30. * To maximise compression there are different inodes for each file type
  31. * (regular file, directory, device, etc.), the inode contents and length
  32. * varying with the type.
  33. *
  34. * To further maximise compression, two types of regular file inode and
  35. * directory inode are defined: inodes optimised for frequently occurring
  36. * regular files and directories, and extended types where extra
  37. * information has to be stored.
  38. */
  39. #include <linux/fs.h>
  40. #include <linux/vfs.h>
  41. #include <linux/zlib.h>
  42. #include "squashfs_fs.h"
  43. #include "squashfs_fs_sb.h"
  44. #include "squashfs_fs_i.h"
  45. #include "squashfs.h"
  46. /*
  47. * Initialise VFS inode with the base inode information common to all
  48. * Squashfs inode types. Sqsh_ino contains the unswapped base inode
  49. * off disk.
  50. */
  51. static int squashfs_new_inode(struct super_block *sb, struct inode *inode,
  52. struct squashfs_base_inode *sqsh_ino)
  53. {
  54. int err;
  55. err = squashfs_get_id(sb, le16_to_cpu(sqsh_ino->uid), &inode->i_uid);
  56. if (err)
  57. return err;
  58. err = squashfs_get_id(sb, le16_to_cpu(sqsh_ino->guid), &inode->i_gid);
  59. if (err)
  60. return err;
  61. inode->i_ino = le32_to_cpu(sqsh_ino->inode_number);
  62. inode->i_mtime.tv_sec = le32_to_cpu(sqsh_ino->mtime);
  63. inode->i_atime.tv_sec = inode->i_mtime.tv_sec;
  64. inode->i_ctime.tv_sec = inode->i_mtime.tv_sec;
  65. inode->i_mode = le16_to_cpu(sqsh_ino->mode);
  66. inode->i_size = 0;
  67. return err;
  68. }
  69. struct inode *squashfs_iget(struct super_block *sb, long long ino,
  70. unsigned int ino_number)
  71. {
  72. struct inode *inode = iget_locked(sb, ino_number);
  73. int err;
  74. TRACE("Entered squashfs_iget\n");
  75. if (!inode)
  76. return ERR_PTR(-ENOMEM);
  77. if (!(inode->i_state & I_NEW))
  78. return inode;
  79. err = squashfs_read_inode(inode, ino);
  80. if (err) {
  81. iget_failed(inode);
  82. return ERR_PTR(err);
  83. }
  84. unlock_new_inode(inode);
  85. return inode;
  86. }
  87. /*
  88. * Initialise VFS inode by reading inode from inode table (compressed
  89. * metadata). The format and amount of data read depends on type.
  90. */
  91. int squashfs_read_inode(struct inode *inode, long long ino)
  92. {
  93. struct super_block *sb = inode->i_sb;
  94. struct squashfs_sb_info *msblk = sb->s_fs_info;
  95. u64 block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
  96. int err, type, offset = SQUASHFS_INODE_OFFSET(ino);
  97. union squashfs_inode squashfs_ino;
  98. struct squashfs_base_inode *sqshb_ino = &squashfs_ino.base;
  99. TRACE("Entered squashfs_read_inode\n");
  100. /*
  101. * Read inode base common to all inode types.
  102. */
  103. err = squashfs_read_metadata(sb, sqshb_ino, &block,
  104. &offset, sizeof(*sqshb_ino));
  105. if (err < 0)
  106. goto failed_read;
  107. err = squashfs_new_inode(sb, inode, sqshb_ino);
  108. if (err)
  109. goto failed_read;
  110. block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
  111. offset = SQUASHFS_INODE_OFFSET(ino);
  112. type = le16_to_cpu(sqshb_ino->inode_type);
  113. switch (type) {
  114. case SQUASHFS_REG_TYPE: {
  115. unsigned int frag_offset, frag;
  116. int frag_size;
  117. u64 frag_blk;
  118. struct squashfs_reg_inode *sqsh_ino = &squashfs_ino.reg;
  119. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  120. sizeof(*sqsh_ino));
  121. if (err < 0)
  122. goto failed_read;
  123. frag = le32_to_cpu(sqsh_ino->fragment);
  124. if (frag != SQUASHFS_INVALID_FRAG) {
  125. frag_offset = le32_to_cpu(sqsh_ino->offset);
  126. frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
  127. if (frag_size < 0) {
  128. err = frag_size;
  129. goto failed_read;
  130. }
  131. } else {
  132. frag_blk = SQUASHFS_INVALID_BLK;
  133. frag_size = 0;
  134. frag_offset = 0;
  135. }
  136. inode->i_nlink = 1;
  137. inode->i_size = le32_to_cpu(sqsh_ino->file_size);
  138. inode->i_fop = &generic_ro_fops;
  139. inode->i_mode |= S_IFREG;
  140. inode->i_blocks = ((inode->i_size - 1) >> 9) + 1;
  141. squashfs_i(inode)->fragment_block = frag_blk;
  142. squashfs_i(inode)->fragment_size = frag_size;
  143. squashfs_i(inode)->fragment_offset = frag_offset;
  144. squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  145. squashfs_i(inode)->block_list_start = block;
  146. squashfs_i(inode)->offset = offset;
  147. inode->i_data.a_ops = &squashfs_aops;
  148. TRACE("File inode %x:%x, start_block %llx, block_list_start "
  149. "%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
  150. offset, squashfs_i(inode)->start, block, offset);
  151. break;
  152. }
  153. case SQUASHFS_LREG_TYPE: {
  154. unsigned int frag_offset, frag;
  155. int frag_size;
  156. u64 frag_blk;
  157. struct squashfs_lreg_inode *sqsh_ino = &squashfs_ino.lreg;
  158. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  159. sizeof(*sqsh_ino));
  160. if (err < 0)
  161. goto failed_read;
  162. frag = le32_to_cpu(sqsh_ino->fragment);
  163. if (frag != SQUASHFS_INVALID_FRAG) {
  164. frag_offset = le32_to_cpu(sqsh_ino->offset);
  165. frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
  166. if (frag_size < 0) {
  167. err = frag_size;
  168. goto failed_read;
  169. }
  170. } else {
  171. frag_blk = SQUASHFS_INVALID_BLK;
  172. frag_size = 0;
  173. frag_offset = 0;
  174. }
  175. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  176. inode->i_size = le64_to_cpu(sqsh_ino->file_size);
  177. inode->i_fop = &generic_ro_fops;
  178. inode->i_mode |= S_IFREG;
  179. inode->i_blocks = ((inode->i_size -
  180. le64_to_cpu(sqsh_ino->sparse) - 1) >> 9) + 1;
  181. squashfs_i(inode)->fragment_block = frag_blk;
  182. squashfs_i(inode)->fragment_size = frag_size;
  183. squashfs_i(inode)->fragment_offset = frag_offset;
  184. squashfs_i(inode)->start = le64_to_cpu(sqsh_ino->start_block);
  185. squashfs_i(inode)->block_list_start = block;
  186. squashfs_i(inode)->offset = offset;
  187. inode->i_data.a_ops = &squashfs_aops;
  188. TRACE("File inode %x:%x, start_block %llx, block_list_start "
  189. "%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
  190. offset, squashfs_i(inode)->start, block, offset);
  191. break;
  192. }
  193. case SQUASHFS_DIR_TYPE: {
  194. struct squashfs_dir_inode *sqsh_ino = &squashfs_ino.dir;
  195. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  196. sizeof(*sqsh_ino));
  197. if (err < 0)
  198. goto failed_read;
  199. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  200. inode->i_size = le16_to_cpu(sqsh_ino->file_size);
  201. inode->i_op = &squashfs_dir_inode_ops;
  202. inode->i_fop = &squashfs_dir_ops;
  203. inode->i_mode |= S_IFDIR;
  204. squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  205. squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
  206. squashfs_i(inode)->dir_idx_cnt = 0;
  207. squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
  208. TRACE("Directory inode %x:%x, start_block %llx, offset %x\n",
  209. SQUASHFS_INODE_BLK(ino), offset,
  210. squashfs_i(inode)->start,
  211. le16_to_cpu(sqsh_ino->offset));
  212. break;
  213. }
  214. case SQUASHFS_LDIR_TYPE: {
  215. struct squashfs_ldir_inode *sqsh_ino = &squashfs_ino.ldir;
  216. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  217. sizeof(*sqsh_ino));
  218. if (err < 0)
  219. goto failed_read;
  220. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  221. inode->i_size = le32_to_cpu(sqsh_ino->file_size);
  222. inode->i_op = &squashfs_dir_inode_ops;
  223. inode->i_fop = &squashfs_dir_ops;
  224. inode->i_mode |= S_IFDIR;
  225. squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  226. squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
  227. squashfs_i(inode)->dir_idx_start = block;
  228. squashfs_i(inode)->dir_idx_offset = offset;
  229. squashfs_i(inode)->dir_idx_cnt = le16_to_cpu(sqsh_ino->i_count);
  230. squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
  231. TRACE("Long directory inode %x:%x, start_block %llx, offset "
  232. "%x\n", SQUASHFS_INODE_BLK(ino), offset,
  233. squashfs_i(inode)->start,
  234. le16_to_cpu(sqsh_ino->offset));
  235. break;
  236. }
  237. case SQUASHFS_SYMLINK_TYPE:
  238. case SQUASHFS_LSYMLINK_TYPE: {
  239. struct squashfs_symlink_inode *sqsh_ino = &squashfs_ino.symlink;
  240. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  241. sizeof(*sqsh_ino));
  242. if (err < 0)
  243. goto failed_read;
  244. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  245. inode->i_size = le32_to_cpu(sqsh_ino->symlink_size);
  246. inode->i_op = &page_symlink_inode_operations;
  247. inode->i_data.a_ops = &squashfs_symlink_aops;
  248. inode->i_mode |= S_IFLNK;
  249. squashfs_i(inode)->start = block;
  250. squashfs_i(inode)->offset = offset;
  251. TRACE("Symbolic link inode %x:%x, start_block %llx, offset "
  252. "%x\n", SQUASHFS_INODE_BLK(ino), offset,
  253. block, offset);
  254. break;
  255. }
  256. case SQUASHFS_BLKDEV_TYPE:
  257. case SQUASHFS_CHRDEV_TYPE:
  258. case SQUASHFS_LBLKDEV_TYPE:
  259. case SQUASHFS_LCHRDEV_TYPE: {
  260. struct squashfs_dev_inode *sqsh_ino = &squashfs_ino.dev;
  261. unsigned int rdev;
  262. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  263. sizeof(*sqsh_ino));
  264. if (err < 0)
  265. goto failed_read;
  266. if (type == SQUASHFS_CHRDEV_TYPE)
  267. inode->i_mode |= S_IFCHR;
  268. else
  269. inode->i_mode |= S_IFBLK;
  270. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  271. rdev = le32_to_cpu(sqsh_ino->rdev);
  272. init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
  273. TRACE("Device inode %x:%x, rdev %x\n",
  274. SQUASHFS_INODE_BLK(ino), offset, rdev);
  275. break;
  276. }
  277. case SQUASHFS_FIFO_TYPE:
  278. case SQUASHFS_SOCKET_TYPE:
  279. case SQUASHFS_LFIFO_TYPE:
  280. case SQUASHFS_LSOCKET_TYPE: {
  281. struct squashfs_ipc_inode *sqsh_ino = &squashfs_ino.ipc;
  282. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  283. sizeof(*sqsh_ino));
  284. if (err < 0)
  285. goto failed_read;
  286. if (type == SQUASHFS_FIFO_TYPE)
  287. inode->i_mode |= S_IFIFO;
  288. else
  289. inode->i_mode |= S_IFSOCK;
  290. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  291. init_special_inode(inode, inode->i_mode, 0);
  292. break;
  293. }
  294. default:
  295. ERROR("Unknown inode type %d in squashfs_iget!\n", type);
  296. return -EINVAL;
  297. }
  298. return 0;
  299. failed_read:
  300. ERROR("Unable to read inode 0x%llx\n", ino);
  301. return err;
  302. }