inode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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/xattr.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. int xattr_id = SQUASHFS_INVALID_XATTR;
  100. TRACE("Entered squashfs_read_inode\n");
  101. /*
  102. * Read inode base common to all inode types.
  103. */
  104. err = squashfs_read_metadata(sb, sqshb_ino, &block,
  105. &offset, sizeof(*sqshb_ino));
  106. if (err < 0)
  107. goto failed_read;
  108. err = squashfs_new_inode(sb, inode, sqshb_ino);
  109. if (err)
  110. goto failed_read;
  111. block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
  112. offset = SQUASHFS_INODE_OFFSET(ino);
  113. type = le16_to_cpu(sqshb_ino->inode_type);
  114. switch (type) {
  115. case SQUASHFS_REG_TYPE: {
  116. unsigned int frag_offset, frag;
  117. int frag_size;
  118. u64 frag_blk;
  119. struct squashfs_reg_inode *sqsh_ino = &squashfs_ino.reg;
  120. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  121. sizeof(*sqsh_ino));
  122. if (err < 0)
  123. goto failed_read;
  124. frag = le32_to_cpu(sqsh_ino->fragment);
  125. if (frag != SQUASHFS_INVALID_FRAG) {
  126. frag_offset = le32_to_cpu(sqsh_ino->offset);
  127. frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
  128. if (frag_size < 0) {
  129. err = frag_size;
  130. goto failed_read;
  131. }
  132. } else {
  133. frag_blk = SQUASHFS_INVALID_BLK;
  134. frag_size = 0;
  135. frag_offset = 0;
  136. }
  137. inode->i_nlink = 1;
  138. inode->i_size = le32_to_cpu(sqsh_ino->file_size);
  139. inode->i_fop = &generic_ro_fops;
  140. inode->i_mode |= S_IFREG;
  141. inode->i_blocks = ((inode->i_size - 1) >> 9) + 1;
  142. squashfs_i(inode)->fragment_block = frag_blk;
  143. squashfs_i(inode)->fragment_size = frag_size;
  144. squashfs_i(inode)->fragment_offset = frag_offset;
  145. squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  146. squashfs_i(inode)->block_list_start = block;
  147. squashfs_i(inode)->offset = offset;
  148. inode->i_data.a_ops = &squashfs_aops;
  149. TRACE("File inode %x:%x, start_block %llx, block_list_start "
  150. "%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
  151. offset, squashfs_i(inode)->start, block, offset);
  152. break;
  153. }
  154. case SQUASHFS_LREG_TYPE: {
  155. unsigned int frag_offset, frag;
  156. int frag_size;
  157. u64 frag_blk;
  158. struct squashfs_lreg_inode *sqsh_ino = &squashfs_ino.lreg;
  159. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  160. sizeof(*sqsh_ino));
  161. if (err < 0)
  162. goto failed_read;
  163. frag = le32_to_cpu(sqsh_ino->fragment);
  164. if (frag != SQUASHFS_INVALID_FRAG) {
  165. frag_offset = le32_to_cpu(sqsh_ino->offset);
  166. frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
  167. if (frag_size < 0) {
  168. err = frag_size;
  169. goto failed_read;
  170. }
  171. } else {
  172. frag_blk = SQUASHFS_INVALID_BLK;
  173. frag_size = 0;
  174. frag_offset = 0;
  175. }
  176. xattr_id = le32_to_cpu(sqsh_ino->xattr);
  177. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  178. inode->i_size = le64_to_cpu(sqsh_ino->file_size);
  179. inode->i_op = &squashfs_inode_ops;
  180. inode->i_fop = &generic_ro_fops;
  181. inode->i_mode |= S_IFREG;
  182. inode->i_blocks = ((inode->i_size -
  183. le64_to_cpu(sqsh_ino->sparse) - 1) >> 9) + 1;
  184. squashfs_i(inode)->fragment_block = frag_blk;
  185. squashfs_i(inode)->fragment_size = frag_size;
  186. squashfs_i(inode)->fragment_offset = frag_offset;
  187. squashfs_i(inode)->start = le64_to_cpu(sqsh_ino->start_block);
  188. squashfs_i(inode)->block_list_start = block;
  189. squashfs_i(inode)->offset = offset;
  190. inode->i_data.a_ops = &squashfs_aops;
  191. TRACE("File inode %x:%x, start_block %llx, block_list_start "
  192. "%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
  193. offset, squashfs_i(inode)->start, block, offset);
  194. break;
  195. }
  196. case SQUASHFS_DIR_TYPE: {
  197. struct squashfs_dir_inode *sqsh_ino = &squashfs_ino.dir;
  198. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  199. sizeof(*sqsh_ino));
  200. if (err < 0)
  201. goto failed_read;
  202. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  203. inode->i_size = le16_to_cpu(sqsh_ino->file_size);
  204. inode->i_op = &squashfs_dir_inode_ops;
  205. inode->i_fop = &squashfs_dir_ops;
  206. inode->i_mode |= S_IFDIR;
  207. squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  208. squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
  209. squashfs_i(inode)->dir_idx_cnt = 0;
  210. squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
  211. TRACE("Directory inode %x:%x, start_block %llx, offset %x\n",
  212. SQUASHFS_INODE_BLK(ino), offset,
  213. squashfs_i(inode)->start,
  214. le16_to_cpu(sqsh_ino->offset));
  215. break;
  216. }
  217. case SQUASHFS_LDIR_TYPE: {
  218. struct squashfs_ldir_inode *sqsh_ino = &squashfs_ino.ldir;
  219. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  220. sizeof(*sqsh_ino));
  221. if (err < 0)
  222. goto failed_read;
  223. xattr_id = le32_to_cpu(sqsh_ino->xattr);
  224. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  225. inode->i_size = le32_to_cpu(sqsh_ino->file_size);
  226. inode->i_op = &squashfs_dir_inode_ops;
  227. inode->i_fop = &squashfs_dir_ops;
  228. inode->i_mode |= S_IFDIR;
  229. squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  230. squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
  231. squashfs_i(inode)->dir_idx_start = block;
  232. squashfs_i(inode)->dir_idx_offset = offset;
  233. squashfs_i(inode)->dir_idx_cnt = le16_to_cpu(sqsh_ino->i_count);
  234. squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
  235. TRACE("Long directory inode %x:%x, start_block %llx, offset "
  236. "%x\n", SQUASHFS_INODE_BLK(ino), offset,
  237. squashfs_i(inode)->start,
  238. le16_to_cpu(sqsh_ino->offset));
  239. break;
  240. }
  241. case SQUASHFS_SYMLINK_TYPE:
  242. case SQUASHFS_LSYMLINK_TYPE: {
  243. struct squashfs_symlink_inode *sqsh_ino = &squashfs_ino.symlink;
  244. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  245. sizeof(*sqsh_ino));
  246. if (err < 0)
  247. goto failed_read;
  248. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  249. inode->i_size = le32_to_cpu(sqsh_ino->symlink_size);
  250. inode->i_op = &squashfs_symlink_inode_ops;
  251. inode->i_data.a_ops = &squashfs_symlink_aops;
  252. inode->i_mode |= S_IFLNK;
  253. squashfs_i(inode)->start = block;
  254. squashfs_i(inode)->offset = offset;
  255. if (type == SQUASHFS_LSYMLINK_TYPE) {
  256. __le32 xattr;
  257. err = squashfs_read_metadata(sb, NULL, &block,
  258. &offset, inode->i_size);
  259. if (err < 0)
  260. goto failed_read;
  261. err = squashfs_read_metadata(sb, &xattr, &block,
  262. &offset, sizeof(xattr));
  263. if (err < 0)
  264. goto failed_read;
  265. xattr_id = le32_to_cpu(xattr);
  266. }
  267. TRACE("Symbolic link inode %x:%x, start_block %llx, offset "
  268. "%x\n", SQUASHFS_INODE_BLK(ino), offset,
  269. block, offset);
  270. break;
  271. }
  272. case SQUASHFS_BLKDEV_TYPE:
  273. case SQUASHFS_CHRDEV_TYPE: {
  274. struct squashfs_dev_inode *sqsh_ino = &squashfs_ino.dev;
  275. unsigned int rdev;
  276. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  277. sizeof(*sqsh_ino));
  278. if (err < 0)
  279. goto failed_read;
  280. if (type == SQUASHFS_CHRDEV_TYPE)
  281. inode->i_mode |= S_IFCHR;
  282. else
  283. inode->i_mode |= S_IFBLK;
  284. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  285. rdev = le32_to_cpu(sqsh_ino->rdev);
  286. init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
  287. TRACE("Device inode %x:%x, rdev %x\n",
  288. SQUASHFS_INODE_BLK(ino), offset, rdev);
  289. break;
  290. }
  291. case SQUASHFS_LBLKDEV_TYPE:
  292. case SQUASHFS_LCHRDEV_TYPE: {
  293. struct squashfs_ldev_inode *sqsh_ino = &squashfs_ino.ldev;
  294. unsigned int rdev;
  295. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  296. sizeof(*sqsh_ino));
  297. if (err < 0)
  298. goto failed_read;
  299. if (type == SQUASHFS_LCHRDEV_TYPE)
  300. inode->i_mode |= S_IFCHR;
  301. else
  302. inode->i_mode |= S_IFBLK;
  303. xattr_id = le32_to_cpu(sqsh_ino->xattr);
  304. inode->i_op = &squashfs_inode_ops;
  305. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  306. rdev = le32_to_cpu(sqsh_ino->rdev);
  307. init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
  308. TRACE("Device inode %x:%x, rdev %x\n",
  309. SQUASHFS_INODE_BLK(ino), offset, rdev);
  310. break;
  311. }
  312. case SQUASHFS_FIFO_TYPE:
  313. case SQUASHFS_SOCKET_TYPE: {
  314. struct squashfs_ipc_inode *sqsh_ino = &squashfs_ino.ipc;
  315. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  316. sizeof(*sqsh_ino));
  317. if (err < 0)
  318. goto failed_read;
  319. if (type == SQUASHFS_FIFO_TYPE)
  320. inode->i_mode |= S_IFIFO;
  321. else
  322. inode->i_mode |= S_IFSOCK;
  323. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  324. init_special_inode(inode, inode->i_mode, 0);
  325. break;
  326. }
  327. case SQUASHFS_LFIFO_TYPE:
  328. case SQUASHFS_LSOCKET_TYPE: {
  329. struct squashfs_lipc_inode *sqsh_ino = &squashfs_ino.lipc;
  330. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  331. sizeof(*sqsh_ino));
  332. if (err < 0)
  333. goto failed_read;
  334. if (type == SQUASHFS_LFIFO_TYPE)
  335. inode->i_mode |= S_IFIFO;
  336. else
  337. inode->i_mode |= S_IFSOCK;
  338. xattr_id = le32_to_cpu(sqsh_ino->xattr);
  339. inode->i_op = &squashfs_inode_ops;
  340. inode->i_nlink = le32_to_cpu(sqsh_ino->nlink);
  341. init_special_inode(inode, inode->i_mode, 0);
  342. break;
  343. }
  344. default:
  345. ERROR("Unknown inode type %d in squashfs_iget!\n", type);
  346. return -EINVAL;
  347. }
  348. if (xattr_id != SQUASHFS_INVALID_XATTR && msblk->xattr_id_table) {
  349. err = squashfs_xattr_lookup(sb, xattr_id,
  350. &squashfs_i(inode)->xattr_count,
  351. &squashfs_i(inode)->xattr_size,
  352. &squashfs_i(inode)->xattr);
  353. if (err < 0)
  354. goto failed_read;
  355. inode->i_blocks += ((squashfs_i(inode)->xattr_size - 1) >> 9)
  356. + 1;
  357. } else
  358. squashfs_i(inode)->xattr_count = 0;
  359. return 0;
  360. failed_read:
  361. ERROR("Unable to read inode 0x%llx\n", ino);
  362. return err;
  363. }
  364. const struct inode_operations squashfs_inode_ops = {
  365. .getxattr = generic_getxattr,
  366. .listxattr = squashfs_listxattr
  367. };