inode.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 "squashfs_fs.h"
  42. #include "squashfs_fs_sb.h"
  43. #include "squashfs_fs_i.h"
  44. #include "squashfs.h"
  45. /*
  46. * Initialise VFS inode with the base inode information common to all
  47. * Squashfs inode types. Sqsh_ino contains the unswapped base inode
  48. * off disk.
  49. */
  50. static int squashfs_new_inode(struct super_block *sb, struct inode *inode,
  51. struct squashfs_base_inode *sqsh_ino)
  52. {
  53. int err;
  54. err = squashfs_get_id(sb, le16_to_cpu(sqsh_ino->uid), &inode->i_uid);
  55. if (err)
  56. return err;
  57. err = squashfs_get_id(sb, le16_to_cpu(sqsh_ino->guid), &inode->i_gid);
  58. if (err)
  59. return err;
  60. inode->i_ino = le32_to_cpu(sqsh_ino->inode_number);
  61. inode->i_mtime.tv_sec = le32_to_cpu(sqsh_ino->mtime);
  62. inode->i_atime.tv_sec = inode->i_mtime.tv_sec;
  63. inode->i_ctime.tv_sec = inode->i_mtime.tv_sec;
  64. inode->i_mode = le16_to_cpu(sqsh_ino->mode);
  65. inode->i_size = 0;
  66. return err;
  67. }
  68. struct inode *squashfs_iget(struct super_block *sb, long long ino,
  69. unsigned int ino_number)
  70. {
  71. struct inode *inode = iget_locked(sb, ino_number);
  72. int err;
  73. TRACE("Entered squashfs_iget\n");
  74. if (!inode)
  75. return ERR_PTR(-ENOMEM);
  76. if (!(inode->i_state & I_NEW))
  77. return inode;
  78. err = squashfs_read_inode(inode, ino);
  79. if (err) {
  80. iget_failed(inode);
  81. return ERR_PTR(err);
  82. }
  83. unlock_new_inode(inode);
  84. return inode;
  85. }
  86. /*
  87. * Initialise VFS inode by reading inode from inode table (compressed
  88. * metadata). The format and amount of data read depends on type.
  89. */
  90. int squashfs_read_inode(struct inode *inode, long long ino)
  91. {
  92. struct super_block *sb = inode->i_sb;
  93. struct squashfs_sb_info *msblk = sb->s_fs_info;
  94. u64 block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
  95. int err, type, offset = SQUASHFS_INODE_OFFSET(ino);
  96. union squashfs_inode squashfs_ino;
  97. struct squashfs_base_inode *sqshb_ino = &squashfs_ino.base;
  98. TRACE("Entered squashfs_read_inode\n");
  99. /*
  100. * Read inode base common to all inode types.
  101. */
  102. err = squashfs_read_metadata(sb, sqshb_ino, &block,
  103. &offset, sizeof(*sqshb_ino));
  104. if (err < 0)
  105. goto failed_read;
  106. err = squashfs_new_inode(sb, inode, sqshb_ino);
  107. if (err)
  108. goto failed_read;
  109. block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
  110. offset = SQUASHFS_INODE_OFFSET(ino);
  111. type = le16_to_cpu(sqshb_ino->inode_type);
  112. switch (type) {
  113. case SQUASHFS_REG_TYPE: {
  114. unsigned int frag_offset, frag;
  115. int frag_size;
  116. u64 frag_blk;
  117. struct squashfs_reg_inode *sqsh_ino = &squashfs_ino.reg;
  118. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  119. sizeof(*sqsh_ino));
  120. if (err < 0)
  121. goto failed_read;
  122. frag = le32_to_cpu(sqsh_ino->fragment);
  123. if (frag != SQUASHFS_INVALID_FRAG) {
  124. frag_offset = le32_to_cpu(sqsh_ino->offset);
  125. frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
  126. if (frag_size < 0) {
  127. err = frag_size;
  128. goto failed_read;
  129. }
  130. } else {
  131. frag_blk = SQUASHFS_INVALID_BLK;
  132. frag_size = 0;
  133. frag_offset = 0;
  134. }
  135. inode->i_nlink = 1;
  136. inode->i_size = le32_to_cpu(sqsh_ino->file_size);
  137. inode->i_fop = &generic_ro_fops;
  138. inode->i_mode |= S_IFREG;
  139. inode->i_blocks = ((inode->i_size - 1) >> 9) + 1;
  140. squashfs_i(inode)->fragment_block = frag_blk;
  141. squashfs_i(inode)->fragment_size = frag_size;
  142. squashfs_i(inode)->fragment_offset = frag_offset;
  143. squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  144. squashfs_i(inode)->block_list_start = block;
  145. squashfs_i(inode)->offset = offset;
  146. inode->i_data.a_ops = &squashfs_aops;
  147. TRACE("File inode %x:%x, start_block %llx, block_list_start "
  148. "%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
  149. offset, squashfs_i(inode)->start, block, offset);
  150. break;
  151. }
  152. case SQUASHFS_LREG_TYPE: {
  153. unsigned int frag_offset, frag;
  154. int frag_size;
  155. u64 frag_blk;
  156. struct squashfs_lreg_inode *sqsh_ino = &squashfs_ino.lreg;
  157. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  158. sizeof(*sqsh_ino));
  159. if (err < 0)
  160. goto failed_read;
  161. frag = le32_to_cpu(sqsh_ino->fragment);
  162. if (frag != SQUASHFS_INVALID_FRAG) {
  163. frag_offset = le32_to_cpu(sqsh_ino->offset);
  164. frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
  165. if (frag_size < 0) {
  166. err = frag_size;
  167. goto failed_read;
  168. }
  169. } else {
  170. frag_blk = SQUASHFS_INVALID_BLK;
  171. frag_size = 0;
  172. frag_offset = 0;
  173. }
  174. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  175. inode->i_size = le64_to_cpu(sqsh_ino->file_size);
  176. inode->i_fop = &generic_ro_fops;
  177. inode->i_mode |= S_IFREG;
  178. inode->i_blocks = ((inode->i_size -
  179. le64_to_cpu(sqsh_ino->sparse) - 1) >> 9) + 1;
  180. squashfs_i(inode)->fragment_block = frag_blk;
  181. squashfs_i(inode)->fragment_size = frag_size;
  182. squashfs_i(inode)->fragment_offset = frag_offset;
  183. squashfs_i(inode)->start = le64_to_cpu(sqsh_ino->start_block);
  184. squashfs_i(inode)->block_list_start = block;
  185. squashfs_i(inode)->offset = offset;
  186. inode->i_data.a_ops = &squashfs_aops;
  187. TRACE("File inode %x:%x, start_block %llx, block_list_start "
  188. "%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
  189. offset, squashfs_i(inode)->start, block, offset);
  190. break;
  191. }
  192. case SQUASHFS_DIR_TYPE: {
  193. struct squashfs_dir_inode *sqsh_ino = &squashfs_ino.dir;
  194. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  195. sizeof(*sqsh_ino));
  196. if (err < 0)
  197. goto failed_read;
  198. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  199. inode->i_size = le16_to_cpu(sqsh_ino->file_size);
  200. inode->i_op = &squashfs_dir_inode_ops;
  201. inode->i_fop = &squashfs_dir_ops;
  202. inode->i_mode |= S_IFDIR;
  203. squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  204. squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
  205. squashfs_i(inode)->dir_idx_cnt = 0;
  206. squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
  207. TRACE("Directory inode %x:%x, start_block %llx, offset %x\n",
  208. SQUASHFS_INODE_BLK(ino), offset,
  209. squashfs_i(inode)->start,
  210. le16_to_cpu(sqsh_ino->offset));
  211. break;
  212. }
  213. case SQUASHFS_LDIR_TYPE: {
  214. struct squashfs_ldir_inode *sqsh_ino = &squashfs_ino.ldir;
  215. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  216. sizeof(*sqsh_ino));
  217. if (err < 0)
  218. goto failed_read;
  219. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  220. inode->i_size = le32_to_cpu(sqsh_ino->file_size);
  221. inode->i_op = &squashfs_dir_inode_ops;
  222. inode->i_fop = &squashfs_dir_ops;
  223. inode->i_mode |= S_IFDIR;
  224. squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  225. squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
  226. squashfs_i(inode)->dir_idx_start = block;
  227. squashfs_i(inode)->dir_idx_offset = offset;
  228. squashfs_i(inode)->dir_idx_cnt = le16_to_cpu(sqsh_ino->i_count);
  229. squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
  230. TRACE("Long directory inode %x:%x, start_block %llx, offset "
  231. "%x\n", SQUASHFS_INODE_BLK(ino), offset,
  232. squashfs_i(inode)->start,
  233. le16_to_cpu(sqsh_ino->offset));
  234. break;
  235. }
  236. case SQUASHFS_SYMLINK_TYPE:
  237. case SQUASHFS_LSYMLINK_TYPE: {
  238. struct squashfs_symlink_inode *sqsh_ino = &squashfs_ino.symlink;
  239. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  240. sizeof(*sqsh_ino));
  241. if (err < 0)
  242. goto failed_read;
  243. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  244. inode->i_size = le32_to_cpu(sqsh_ino->symlink_size);
  245. inode->i_op = &page_symlink_inode_operations;
  246. inode->i_data.a_ops = &squashfs_symlink_aops;
  247. inode->i_mode |= S_IFLNK;
  248. squashfs_i(inode)->start = block;
  249. squashfs_i(inode)->offset = offset;
  250. TRACE("Symbolic link inode %x:%x, start_block %llx, offset "
  251. "%x\n", SQUASHFS_INODE_BLK(ino), offset,
  252. block, offset);
  253. break;
  254. }
  255. case SQUASHFS_BLKDEV_TYPE:
  256. case SQUASHFS_CHRDEV_TYPE:
  257. case SQUASHFS_LBLKDEV_TYPE:
  258. case SQUASHFS_LCHRDEV_TYPE: {
  259. struct squashfs_dev_inode *sqsh_ino = &squashfs_ino.dev;
  260. unsigned int rdev;
  261. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  262. sizeof(*sqsh_ino));
  263. if (err < 0)
  264. goto failed_read;
  265. if (type == SQUASHFS_CHRDEV_TYPE)
  266. inode->i_mode |= S_IFCHR;
  267. else
  268. inode->i_mode |= S_IFBLK;
  269. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  270. rdev = le32_to_cpu(sqsh_ino->rdev);
  271. init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
  272. TRACE("Device inode %x:%x, rdev %x\n",
  273. SQUASHFS_INODE_BLK(ino), offset, rdev);
  274. break;
  275. }
  276. case SQUASHFS_FIFO_TYPE:
  277. case SQUASHFS_SOCKET_TYPE:
  278. case SQUASHFS_LFIFO_TYPE:
  279. case SQUASHFS_LSOCKET_TYPE: {
  280. struct squashfs_ipc_inode *sqsh_ino = &squashfs_ino.ipc;
  281. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  282. sizeof(*sqsh_ino));
  283. if (err < 0)
  284. goto failed_read;
  285. if (type == SQUASHFS_FIFO_TYPE)
  286. inode->i_mode |= S_IFIFO;
  287. else
  288. inode->i_mode |= S_IFSOCK;
  289. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  290. init_special_inode(inode, inode->i_mode, 0);
  291. break;
  292. }
  293. default:
  294. ERROR("Unknown inode type %d in squashfs_iget!\n", type);
  295. return -EINVAL;
  296. }
  297. return 0;
  298. failed_read:
  299. ERROR("Unable to read inode 0x%llx\n", ino);
  300. return err;
  301. }