inode.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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_size, frag;
  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_size, frag;
  154. u64 frag_blk;
  155. struct squashfs_lreg_inode *sqsh_ino = &squashfs_ino.lreg;
  156. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  157. sizeof(*sqsh_ino));
  158. if (err < 0)
  159. goto failed_read;
  160. frag = le32_to_cpu(sqsh_ino->fragment);
  161. if (frag != SQUASHFS_INVALID_FRAG) {
  162. frag_offset = le32_to_cpu(sqsh_ino->offset);
  163. frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
  164. if (frag_size < 0) {
  165. err = frag_size;
  166. goto failed_read;
  167. }
  168. } else {
  169. frag_blk = SQUASHFS_INVALID_BLK;
  170. frag_size = 0;
  171. frag_offset = 0;
  172. }
  173. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  174. inode->i_size = le64_to_cpu(sqsh_ino->file_size);
  175. inode->i_fop = &generic_ro_fops;
  176. inode->i_mode |= S_IFREG;
  177. inode->i_blocks = ((inode->i_size -
  178. le64_to_cpu(sqsh_ino->sparse) - 1) >> 9) + 1;
  179. squashfs_i(inode)->fragment_block = frag_blk;
  180. squashfs_i(inode)->fragment_size = frag_size;
  181. squashfs_i(inode)->fragment_offset = frag_offset;
  182. squashfs_i(inode)->start = le64_to_cpu(sqsh_ino->start_block);
  183. squashfs_i(inode)->block_list_start = block;
  184. squashfs_i(inode)->offset = offset;
  185. inode->i_data.a_ops = &squashfs_aops;
  186. TRACE("File inode %x:%x, start_block %llx, block_list_start "
  187. "%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
  188. offset, squashfs_i(inode)->start, block, offset);
  189. break;
  190. }
  191. case SQUASHFS_DIR_TYPE: {
  192. struct squashfs_dir_inode *sqsh_ino = &squashfs_ino.dir;
  193. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  194. sizeof(*sqsh_ino));
  195. if (err < 0)
  196. goto failed_read;
  197. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  198. inode->i_size = le16_to_cpu(sqsh_ino->file_size);
  199. inode->i_op = &squashfs_dir_inode_ops;
  200. inode->i_fop = &squashfs_dir_ops;
  201. inode->i_mode |= S_IFDIR;
  202. squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  203. squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
  204. squashfs_i(inode)->dir_idx_cnt = 0;
  205. squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
  206. TRACE("Directory inode %x:%x, start_block %llx, offset %x\n",
  207. SQUASHFS_INODE_BLK(ino), offset,
  208. squashfs_i(inode)->start,
  209. le16_to_cpu(sqsh_ino->offset));
  210. break;
  211. }
  212. case SQUASHFS_LDIR_TYPE: {
  213. struct squashfs_ldir_inode *sqsh_ino = &squashfs_ino.ldir;
  214. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  215. sizeof(*sqsh_ino));
  216. if (err < 0)
  217. goto failed_read;
  218. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  219. inode->i_size = le32_to_cpu(sqsh_ino->file_size);
  220. inode->i_op = &squashfs_dir_inode_ops;
  221. inode->i_fop = &squashfs_dir_ops;
  222. inode->i_mode |= S_IFDIR;
  223. squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  224. squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
  225. squashfs_i(inode)->dir_idx_start = block;
  226. squashfs_i(inode)->dir_idx_offset = offset;
  227. squashfs_i(inode)->dir_idx_cnt = le16_to_cpu(sqsh_ino->i_count);
  228. squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
  229. TRACE("Long directory inode %x:%x, start_block %llx, offset "
  230. "%x\n", SQUASHFS_INODE_BLK(ino), offset,
  231. squashfs_i(inode)->start,
  232. le16_to_cpu(sqsh_ino->offset));
  233. break;
  234. }
  235. case SQUASHFS_SYMLINK_TYPE:
  236. case SQUASHFS_LSYMLINK_TYPE: {
  237. struct squashfs_symlink_inode *sqsh_ino = &squashfs_ino.symlink;
  238. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  239. sizeof(*sqsh_ino));
  240. if (err < 0)
  241. goto failed_read;
  242. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  243. inode->i_size = le32_to_cpu(sqsh_ino->symlink_size);
  244. inode->i_op = &page_symlink_inode_operations;
  245. inode->i_data.a_ops = &squashfs_symlink_aops;
  246. inode->i_mode |= S_IFLNK;
  247. squashfs_i(inode)->start = block;
  248. squashfs_i(inode)->offset = offset;
  249. TRACE("Symbolic link inode %x:%x, start_block %llx, offset "
  250. "%x\n", SQUASHFS_INODE_BLK(ino), offset,
  251. block, offset);
  252. break;
  253. }
  254. case SQUASHFS_BLKDEV_TYPE:
  255. case SQUASHFS_CHRDEV_TYPE:
  256. case SQUASHFS_LBLKDEV_TYPE:
  257. case SQUASHFS_LCHRDEV_TYPE: {
  258. struct squashfs_dev_inode *sqsh_ino = &squashfs_ino.dev;
  259. unsigned int rdev;
  260. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  261. sizeof(*sqsh_ino));
  262. if (err < 0)
  263. goto failed_read;
  264. if (type == SQUASHFS_CHRDEV_TYPE)
  265. inode->i_mode |= S_IFCHR;
  266. else
  267. inode->i_mode |= S_IFBLK;
  268. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  269. rdev = le32_to_cpu(sqsh_ino->rdev);
  270. init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
  271. TRACE("Device inode %x:%x, rdev %x\n",
  272. SQUASHFS_INODE_BLK(ino), offset, rdev);
  273. break;
  274. }
  275. case SQUASHFS_FIFO_TYPE:
  276. case SQUASHFS_SOCKET_TYPE:
  277. case SQUASHFS_LFIFO_TYPE:
  278. case SQUASHFS_LSOCKET_TYPE: {
  279. struct squashfs_ipc_inode *sqsh_ino = &squashfs_ino.ipc;
  280. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  281. sizeof(*sqsh_ino));
  282. if (err < 0)
  283. goto failed_read;
  284. if (type == SQUASHFS_FIFO_TYPE)
  285. inode->i_mode |= S_IFIFO;
  286. else
  287. inode->i_mode |= S_IFSOCK;
  288. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  289. init_special_inode(inode, inode->i_mode, 0);
  290. break;
  291. }
  292. default:
  293. ERROR("Unknown inode type %d in squashfs_iget!\n", type);
  294. return -EINVAL;
  295. }
  296. return 0;
  297. failed_read:
  298. ERROR("Unable to read inode 0x%llx\n", ino);
  299. return err;
  300. }