inode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Squashfs - a compressed read only filesystem for Linux
  4. *
  5. * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
  6. * Phillip Lougher <phillip@squashfs.org.uk>
  7. *
  8. * inode.c
  9. */
  10. /*
  11. * This file implements code to create and read inodes from disk.
  12. *
  13. * Inodes in Squashfs are identified by a 48-bit inode which encodes the
  14. * location of the compressed metadata block containing the inode, and the byte
  15. * offset into that block where the inode is placed (<block, offset>).
  16. *
  17. * To maximise compression there are different inodes for each file type
  18. * (regular file, directory, device, etc.), the inode contents and length
  19. * varying with the type.
  20. *
  21. * To further maximise compression, two types of regular file inode and
  22. * directory inode are defined: inodes optimised for frequently occurring
  23. * regular files and directories, and extended types where extra
  24. * information has to be stored.
  25. */
  26. #include <linux/fs.h>
  27. #include <linux/vfs.h>
  28. #include <linux/xattr.h>
  29. #include <linux/pagemap.h>
  30. #include "squashfs_fs.h"
  31. #include "squashfs_fs_sb.h"
  32. #include "squashfs_fs_i.h"
  33. #include "squashfs.h"
  34. #include "xattr.h"
  35. /*
  36. * Initialise VFS inode with the base inode information common to all
  37. * Squashfs inode types. Sqsh_ino contains the unswapped base inode
  38. * off disk.
  39. */
  40. static int squashfs_new_inode(struct super_block *sb, struct inode *inode,
  41. struct squashfs_base_inode *sqsh_ino)
  42. {
  43. uid_t i_uid;
  44. gid_t i_gid;
  45. int err;
  46. inode->i_ino = le32_to_cpu(sqsh_ino->inode_number);
  47. if (inode->i_ino == 0)
  48. return -EINVAL;
  49. err = squashfs_get_id(sb, le16_to_cpu(sqsh_ino->uid), &i_uid);
  50. if (err)
  51. return err;
  52. err = squashfs_get_id(sb, le16_to_cpu(sqsh_ino->guid), &i_gid);
  53. if (err)
  54. return err;
  55. i_uid_write(inode, i_uid);
  56. i_gid_write(inode, i_gid);
  57. inode_set_mtime(inode, le32_to_cpu(sqsh_ino->mtime), 0);
  58. inode_set_atime(inode, inode_get_mtime_sec(inode), 0);
  59. inode_set_ctime(inode, inode_get_mtime_sec(inode), 0);
  60. inode->i_mode = le16_to_cpu(sqsh_ino->mode);
  61. inode->i_size = 0;
  62. return err;
  63. }
  64. struct inode *squashfs_iget(struct super_block *sb, long long ino,
  65. unsigned int ino_number)
  66. {
  67. struct inode *inode = iget_locked(sb, ino_number);
  68. int err;
  69. TRACE("Entered squashfs_iget\n");
  70. if (!inode)
  71. return ERR_PTR(-ENOMEM);
  72. if (!(inode->i_state & I_NEW))
  73. return inode;
  74. err = squashfs_read_inode(inode, ino);
  75. if (err) {
  76. iget_failed(inode);
  77. return ERR_PTR(err);
  78. }
  79. unlock_new_inode(inode);
  80. return inode;
  81. }
  82. /*
  83. * Initialise VFS inode by reading inode from inode table (compressed
  84. * metadata). The format and amount of data read depends on type.
  85. */
  86. int squashfs_read_inode(struct inode *inode, long long ino)
  87. {
  88. struct super_block *sb = inode->i_sb;
  89. struct squashfs_sb_info *msblk = sb->s_fs_info;
  90. u64 block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
  91. int err, type, offset = SQUASHFS_INODE_OFFSET(ino);
  92. union squashfs_inode squashfs_ino;
  93. struct squashfs_base_inode *sqshb_ino = &squashfs_ino.base;
  94. int xattr_id = SQUASHFS_INVALID_XATTR;
  95. TRACE("Entered squashfs_read_inode\n");
  96. /*
  97. * Read inode base common to all inode types.
  98. */
  99. err = squashfs_read_metadata(sb, sqshb_ino, &block,
  100. &offset, sizeof(*sqshb_ino));
  101. if (err < 0)
  102. goto failed_read;
  103. err = squashfs_new_inode(sb, inode, sqshb_ino);
  104. if (err)
  105. goto failed_read;
  106. block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
  107. offset = SQUASHFS_INODE_OFFSET(ino);
  108. type = le16_to_cpu(sqshb_ino->inode_type);
  109. switch (type) {
  110. case SQUASHFS_REG_TYPE: {
  111. unsigned int frag_offset, frag;
  112. int frag_size;
  113. u64 frag_blk;
  114. struct squashfs_reg_inode *sqsh_ino = &squashfs_ino.reg;
  115. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  116. sizeof(*sqsh_ino));
  117. if (err < 0)
  118. goto failed_read;
  119. inode->i_size = le32_to_cpu(sqsh_ino->file_size);
  120. frag = le32_to_cpu(sqsh_ino->fragment);
  121. if (frag != SQUASHFS_INVALID_FRAG) {
  122. /*
  123. * the file cannot have a fragment (tailend) and have a
  124. * file size a multiple of the block size
  125. */
  126. if ((inode->i_size & (msblk->block_size - 1)) == 0) {
  127. err = -EINVAL;
  128. goto failed_read;
  129. }
  130. frag_offset = le32_to_cpu(sqsh_ino->offset);
  131. frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
  132. if (frag_size < 0) {
  133. err = frag_size;
  134. goto failed_read;
  135. }
  136. } else {
  137. frag_blk = SQUASHFS_INVALID_BLK;
  138. frag_size = 0;
  139. frag_offset = 0;
  140. }
  141. set_nlink(inode, 1);
  142. inode->i_fop = &generic_ro_fops;
  143. inode->i_mode |= S_IFREG;
  144. inode->i_blocks = ((inode->i_size - 1) >> 9) + 1;
  145. squashfs_i(inode)->fragment_block = frag_blk;
  146. squashfs_i(inode)->fragment_size = frag_size;
  147. squashfs_i(inode)->fragment_offset = frag_offset;
  148. squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  149. squashfs_i(inode)->block_list_start = block;
  150. squashfs_i(inode)->offset = offset;
  151. squashfs_i(inode)->parent = 0;
  152. inode->i_data.a_ops = &squashfs_aops;
  153. TRACE("File inode %x:%x, start_block %llx, block_list_start "
  154. "%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
  155. offset, squashfs_i(inode)->start, block, offset);
  156. break;
  157. }
  158. case SQUASHFS_LREG_TYPE: {
  159. unsigned int frag_offset, frag;
  160. int frag_size;
  161. u64 frag_blk;
  162. struct squashfs_lreg_inode *sqsh_ino = &squashfs_ino.lreg;
  163. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  164. sizeof(*sqsh_ino));
  165. if (err < 0)
  166. goto failed_read;
  167. inode->i_size = le64_to_cpu(sqsh_ino->file_size);
  168. if (inode->i_size < 0) {
  169. err = -EINVAL;
  170. goto failed_read;
  171. }
  172. frag = le32_to_cpu(sqsh_ino->fragment);
  173. if (frag != SQUASHFS_INVALID_FRAG) {
  174. /*
  175. * the file cannot have a fragment (tailend) and have a
  176. * file size a multiple of the block size
  177. */
  178. if ((inode->i_size & (msblk->block_size - 1)) == 0) {
  179. err = -EINVAL;
  180. goto failed_read;
  181. }
  182. frag_offset = le32_to_cpu(sqsh_ino->offset);
  183. frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
  184. if (frag_size < 0) {
  185. err = frag_size;
  186. goto failed_read;
  187. }
  188. } else {
  189. frag_blk = SQUASHFS_INVALID_BLK;
  190. frag_size = 0;
  191. frag_offset = 0;
  192. }
  193. xattr_id = le32_to_cpu(sqsh_ino->xattr);
  194. set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
  195. inode->i_op = &squashfs_inode_ops;
  196. inode->i_fop = &generic_ro_fops;
  197. inode->i_mode |= S_IFREG;
  198. inode->i_blocks = (inode->i_size -
  199. le64_to_cpu(sqsh_ino->sparse) + 511) >> 9;
  200. squashfs_i(inode)->fragment_block = frag_blk;
  201. squashfs_i(inode)->fragment_size = frag_size;
  202. squashfs_i(inode)->fragment_offset = frag_offset;
  203. squashfs_i(inode)->start = le64_to_cpu(sqsh_ino->start_block);
  204. squashfs_i(inode)->block_list_start = block;
  205. squashfs_i(inode)->offset = offset;
  206. squashfs_i(inode)->parent = 0;
  207. inode->i_data.a_ops = &squashfs_aops;
  208. TRACE("File inode %x:%x, start_block %llx, block_list_start "
  209. "%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
  210. offset, squashfs_i(inode)->start, block, offset);
  211. break;
  212. }
  213. case SQUASHFS_DIR_TYPE: {
  214. struct squashfs_dir_inode *sqsh_ino = &squashfs_ino.dir;
  215. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  216. sizeof(*sqsh_ino));
  217. if (err < 0)
  218. goto failed_read;
  219. set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
  220. inode->i_size = le16_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_cnt = 0;
  227. squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
  228. TRACE("Directory inode %x:%x, start_block %llx, offset %x\n",
  229. SQUASHFS_INODE_BLK(ino), offset,
  230. squashfs_i(inode)->start,
  231. le16_to_cpu(sqsh_ino->offset));
  232. break;
  233. }
  234. case SQUASHFS_LDIR_TYPE: {
  235. struct squashfs_ldir_inode *sqsh_ino = &squashfs_ino.ldir;
  236. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  237. sizeof(*sqsh_ino));
  238. if (err < 0)
  239. goto failed_read;
  240. xattr_id = le32_to_cpu(sqsh_ino->xattr);
  241. set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
  242. inode->i_size = le32_to_cpu(sqsh_ino->file_size);
  243. inode->i_op = &squashfs_dir_inode_ops;
  244. inode->i_fop = &squashfs_dir_ops;
  245. inode->i_mode |= S_IFDIR;
  246. squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  247. squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
  248. squashfs_i(inode)->dir_idx_start = block;
  249. squashfs_i(inode)->dir_idx_offset = offset;
  250. squashfs_i(inode)->dir_idx_cnt = le16_to_cpu(sqsh_ino->i_count);
  251. squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
  252. TRACE("Long directory inode %x:%x, start_block %llx, offset "
  253. "%x\n", SQUASHFS_INODE_BLK(ino), offset,
  254. squashfs_i(inode)->start,
  255. le16_to_cpu(sqsh_ino->offset));
  256. break;
  257. }
  258. case SQUASHFS_SYMLINK_TYPE:
  259. case SQUASHFS_LSYMLINK_TYPE: {
  260. struct squashfs_symlink_inode *sqsh_ino = &squashfs_ino.symlink;
  261. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  262. sizeof(*sqsh_ino));
  263. if (err < 0)
  264. goto failed_read;
  265. inode->i_size = le32_to_cpu(sqsh_ino->symlink_size);
  266. if (inode->i_size > PAGE_SIZE) {
  267. ERROR("Corrupted symlink\n");
  268. return -EINVAL;
  269. }
  270. set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
  271. inode->i_op = &squashfs_symlink_inode_ops;
  272. inode_nohighmem(inode);
  273. inode->i_data.a_ops = &squashfs_symlink_aops;
  274. inode->i_mode |= S_IFLNK;
  275. squashfs_i(inode)->start = block;
  276. squashfs_i(inode)->offset = offset;
  277. squashfs_i(inode)->parent = 0;
  278. if (type == SQUASHFS_LSYMLINK_TYPE) {
  279. __le32 xattr;
  280. err = squashfs_read_metadata(sb, NULL, &block,
  281. &offset, inode->i_size);
  282. if (err < 0)
  283. goto failed_read;
  284. err = squashfs_read_metadata(sb, &xattr, &block,
  285. &offset, sizeof(xattr));
  286. if (err < 0)
  287. goto failed_read;
  288. xattr_id = le32_to_cpu(xattr);
  289. }
  290. TRACE("Symbolic link inode %x:%x, start_block %llx, offset "
  291. "%x\n", SQUASHFS_INODE_BLK(ino), offset,
  292. block, offset);
  293. break;
  294. }
  295. case SQUASHFS_BLKDEV_TYPE:
  296. case SQUASHFS_CHRDEV_TYPE: {
  297. struct squashfs_dev_inode *sqsh_ino = &squashfs_ino.dev;
  298. unsigned int rdev;
  299. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  300. sizeof(*sqsh_ino));
  301. if (err < 0)
  302. goto failed_read;
  303. if (type == SQUASHFS_CHRDEV_TYPE)
  304. inode->i_mode |= S_IFCHR;
  305. else
  306. inode->i_mode |= S_IFBLK;
  307. set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
  308. rdev = le32_to_cpu(sqsh_ino->rdev);
  309. init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
  310. squashfs_i(inode)->parent = 0;
  311. TRACE("Device inode %x:%x, rdev %x\n",
  312. SQUASHFS_INODE_BLK(ino), offset, rdev);
  313. break;
  314. }
  315. case SQUASHFS_LBLKDEV_TYPE:
  316. case SQUASHFS_LCHRDEV_TYPE: {
  317. struct squashfs_ldev_inode *sqsh_ino = &squashfs_ino.ldev;
  318. unsigned int rdev;
  319. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  320. sizeof(*sqsh_ino));
  321. if (err < 0)
  322. goto failed_read;
  323. if (type == SQUASHFS_LCHRDEV_TYPE)
  324. inode->i_mode |= S_IFCHR;
  325. else
  326. inode->i_mode |= S_IFBLK;
  327. xattr_id = le32_to_cpu(sqsh_ino->xattr);
  328. inode->i_op = &squashfs_inode_ops;
  329. set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
  330. rdev = le32_to_cpu(sqsh_ino->rdev);
  331. init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
  332. squashfs_i(inode)->parent = 0;
  333. TRACE("Device inode %x:%x, rdev %x\n",
  334. SQUASHFS_INODE_BLK(ino), offset, rdev);
  335. break;
  336. }
  337. case SQUASHFS_FIFO_TYPE:
  338. case SQUASHFS_SOCKET_TYPE: {
  339. struct squashfs_ipc_inode *sqsh_ino = &squashfs_ino.ipc;
  340. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  341. sizeof(*sqsh_ino));
  342. if (err < 0)
  343. goto failed_read;
  344. if (type == SQUASHFS_FIFO_TYPE)
  345. inode->i_mode |= S_IFIFO;
  346. else
  347. inode->i_mode |= S_IFSOCK;
  348. set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
  349. init_special_inode(inode, inode->i_mode, 0);
  350. squashfs_i(inode)->parent = 0;
  351. break;
  352. }
  353. case SQUASHFS_LFIFO_TYPE:
  354. case SQUASHFS_LSOCKET_TYPE: {
  355. struct squashfs_lipc_inode *sqsh_ino = &squashfs_ino.lipc;
  356. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  357. sizeof(*sqsh_ino));
  358. if (err < 0)
  359. goto failed_read;
  360. if (type == SQUASHFS_LFIFO_TYPE)
  361. inode->i_mode |= S_IFIFO;
  362. else
  363. inode->i_mode |= S_IFSOCK;
  364. xattr_id = le32_to_cpu(sqsh_ino->xattr);
  365. inode->i_op = &squashfs_inode_ops;
  366. set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
  367. init_special_inode(inode, inode->i_mode, 0);
  368. squashfs_i(inode)->parent = 0;
  369. break;
  370. }
  371. default:
  372. ERROR("Unknown inode type %d in squashfs_iget!\n", type);
  373. return -EINVAL;
  374. }
  375. if (xattr_id != SQUASHFS_INVALID_XATTR && msblk->xattr_id_table) {
  376. err = squashfs_xattr_lookup(sb, xattr_id,
  377. &squashfs_i(inode)->xattr_count,
  378. &squashfs_i(inode)->xattr_size,
  379. &squashfs_i(inode)->xattr);
  380. if (err < 0)
  381. goto failed_read;
  382. inode->i_blocks += ((squashfs_i(inode)->xattr_size - 1) >> 9)
  383. + 1;
  384. } else
  385. squashfs_i(inode)->xattr_count = 0;
  386. return 0;
  387. failed_read:
  388. ERROR("Unable to read inode 0x%llx\n", ino);
  389. return err;
  390. }
  391. const struct inode_operations squashfs_inode_ops = {
  392. .listxattr = squashfs_listxattr
  393. };