inode.c 12 KB

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