inode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. /* File type must not be set at this moment, for it will later be set by the caller. */
  63. if (inode->i_mode & S_IFMT)
  64. err = -EIO;
  65. return err;
  66. }
  67. struct inode *squashfs_iget(struct super_block *sb, long long ino,
  68. unsigned int ino_number)
  69. {
  70. struct inode *inode = iget_locked(sb, ino_number);
  71. int err;
  72. TRACE("Entered squashfs_iget\n");
  73. if (!inode)
  74. return ERR_PTR(-ENOMEM);
  75. if (!(inode_state_read_once(inode) & I_NEW))
  76. return inode;
  77. err = squashfs_read_inode(inode, ino);
  78. if (err) {
  79. iget_failed(inode);
  80. return ERR_PTR(err);
  81. }
  82. unlock_new_inode(inode);
  83. return inode;
  84. }
  85. /*
  86. * Initialise VFS inode by reading inode from inode table (compressed
  87. * metadata). The format and amount of data read depends on type.
  88. */
  89. int squashfs_read_inode(struct inode *inode, long long ino)
  90. {
  91. struct super_block *sb = inode->i_sb;
  92. struct squashfs_sb_info *msblk = sb->s_fs_info;
  93. u64 block = SQUASHFS_INODE_BLK(ino) + msblk->inode_table;
  94. int err, type, offset = SQUASHFS_INODE_OFFSET(ino);
  95. union squashfs_inode squashfs_ino;
  96. struct squashfs_base_inode *sqshb_ino = &squashfs_ino.base;
  97. int xattr_id = SQUASHFS_INVALID_XATTR;
  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. inode->i_size = le32_to_cpu(sqsh_ino->file_size);
  123. frag = le32_to_cpu(sqsh_ino->fragment);
  124. if (frag != SQUASHFS_INVALID_FRAG) {
  125. /*
  126. * the file cannot have a fragment (tailend) and have a
  127. * file size a multiple of the block size
  128. */
  129. if ((inode->i_size & (msblk->block_size - 1)) == 0) {
  130. err = -EINVAL;
  131. goto failed_read;
  132. }
  133. frag_offset = le32_to_cpu(sqsh_ino->offset);
  134. frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
  135. if (frag_size < 0) {
  136. err = frag_size;
  137. goto failed_read;
  138. }
  139. } else {
  140. frag_blk = SQUASHFS_INVALID_BLK;
  141. frag_size = 0;
  142. frag_offset = 0;
  143. }
  144. set_nlink(inode, 1);
  145. inode->i_fop = &squashfs_file_operations;
  146. inode->i_mode |= S_IFREG;
  147. inode->i_blocks = ((inode->i_size - 1) >> 9) + 1;
  148. squashfs_i(inode)->fragment_block = frag_blk;
  149. squashfs_i(inode)->fragment_size = frag_size;
  150. squashfs_i(inode)->fragment_offset = frag_offset;
  151. squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  152. squashfs_i(inode)->block_list_start = block;
  153. squashfs_i(inode)->offset = offset;
  154. squashfs_i(inode)->parent = 0;
  155. inode->i_data.a_ops = &squashfs_aops;
  156. TRACE("File inode %x:%x, start_block %llx, block_list_start "
  157. "%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
  158. offset, squashfs_i(inode)->start, block, offset);
  159. break;
  160. }
  161. case SQUASHFS_LREG_TYPE: {
  162. unsigned int frag_offset, frag;
  163. int frag_size;
  164. u64 frag_blk;
  165. struct squashfs_lreg_inode *sqsh_ino = &squashfs_ino.lreg;
  166. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  167. sizeof(*sqsh_ino));
  168. if (err < 0)
  169. goto failed_read;
  170. inode->i_size = le64_to_cpu(sqsh_ino->file_size);
  171. if (inode->i_size < 0) {
  172. err = -EINVAL;
  173. goto failed_read;
  174. }
  175. frag = le32_to_cpu(sqsh_ino->fragment);
  176. if (frag != SQUASHFS_INVALID_FRAG) {
  177. /*
  178. * the file cannot have a fragment (tailend) and have a
  179. * file size a multiple of the block size
  180. */
  181. if ((inode->i_size & (msblk->block_size - 1)) == 0) {
  182. err = -EINVAL;
  183. goto failed_read;
  184. }
  185. frag_offset = le32_to_cpu(sqsh_ino->offset);
  186. frag_size = squashfs_frag_lookup(sb, frag, &frag_blk);
  187. if (frag_size < 0) {
  188. err = frag_size;
  189. goto failed_read;
  190. }
  191. } else {
  192. frag_blk = SQUASHFS_INVALID_BLK;
  193. frag_size = 0;
  194. frag_offset = 0;
  195. }
  196. xattr_id = le32_to_cpu(sqsh_ino->xattr);
  197. set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
  198. inode->i_op = &squashfs_inode_ops;
  199. inode->i_fop = &squashfs_file_operations;
  200. inode->i_mode |= S_IFREG;
  201. inode->i_blocks = (inode->i_size -
  202. le64_to_cpu(sqsh_ino->sparse) + 511) >> 9;
  203. squashfs_i(inode)->fragment_block = frag_blk;
  204. squashfs_i(inode)->fragment_size = frag_size;
  205. squashfs_i(inode)->fragment_offset = frag_offset;
  206. squashfs_i(inode)->start = le64_to_cpu(sqsh_ino->start_block);
  207. squashfs_i(inode)->block_list_start = block;
  208. squashfs_i(inode)->offset = offset;
  209. squashfs_i(inode)->parent = 0;
  210. inode->i_data.a_ops = &squashfs_aops;
  211. TRACE("File inode %x:%x, start_block %llx, block_list_start "
  212. "%llx, offset %x\n", SQUASHFS_INODE_BLK(ino),
  213. offset, squashfs_i(inode)->start, block, offset);
  214. break;
  215. }
  216. case SQUASHFS_DIR_TYPE: {
  217. struct squashfs_dir_inode *sqsh_ino = &squashfs_ino.dir;
  218. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  219. sizeof(*sqsh_ino));
  220. if (err < 0)
  221. goto failed_read;
  222. set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
  223. inode->i_size = le16_to_cpu(sqsh_ino->file_size);
  224. inode->i_op = &squashfs_dir_inode_ops;
  225. inode->i_fop = &squashfs_dir_ops;
  226. inode->i_mode |= S_IFDIR;
  227. squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  228. squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
  229. squashfs_i(inode)->dir_idx_cnt = 0;
  230. squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
  231. TRACE("Directory inode %x:%x, start_block %llx, offset %x\n",
  232. SQUASHFS_INODE_BLK(ino), offset,
  233. squashfs_i(inode)->start,
  234. le16_to_cpu(sqsh_ino->offset));
  235. break;
  236. }
  237. case SQUASHFS_LDIR_TYPE: {
  238. struct squashfs_ldir_inode *sqsh_ino = &squashfs_ino.ldir;
  239. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  240. sizeof(*sqsh_ino));
  241. if (err < 0)
  242. goto failed_read;
  243. xattr_id = le32_to_cpu(sqsh_ino->xattr);
  244. set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
  245. inode->i_size = le32_to_cpu(sqsh_ino->file_size);
  246. inode->i_op = &squashfs_dir_inode_ops;
  247. inode->i_fop = &squashfs_dir_ops;
  248. inode->i_mode |= S_IFDIR;
  249. squashfs_i(inode)->start = le32_to_cpu(sqsh_ino->start_block);
  250. squashfs_i(inode)->offset = le16_to_cpu(sqsh_ino->offset);
  251. squashfs_i(inode)->dir_idx_start = block;
  252. squashfs_i(inode)->dir_idx_offset = offset;
  253. squashfs_i(inode)->dir_idx_cnt = le16_to_cpu(sqsh_ino->i_count);
  254. squashfs_i(inode)->parent = le32_to_cpu(sqsh_ino->parent_inode);
  255. TRACE("Long directory inode %x:%x, start_block %llx, offset "
  256. "%x\n", SQUASHFS_INODE_BLK(ino), offset,
  257. squashfs_i(inode)->start,
  258. le16_to_cpu(sqsh_ino->offset));
  259. break;
  260. }
  261. case SQUASHFS_SYMLINK_TYPE:
  262. case SQUASHFS_LSYMLINK_TYPE: {
  263. struct squashfs_symlink_inode *sqsh_ino = &squashfs_ino.symlink;
  264. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  265. sizeof(*sqsh_ino));
  266. if (err < 0)
  267. goto failed_read;
  268. inode->i_size = le32_to_cpu(sqsh_ino->symlink_size);
  269. if (inode->i_size > PAGE_SIZE) {
  270. ERROR("Corrupted symlink\n");
  271. return -EINVAL;
  272. }
  273. set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
  274. inode->i_op = &squashfs_symlink_inode_ops;
  275. inode_nohighmem(inode);
  276. inode->i_data.a_ops = &squashfs_symlink_aops;
  277. inode->i_mode |= S_IFLNK;
  278. squashfs_i(inode)->start = block;
  279. squashfs_i(inode)->offset = offset;
  280. squashfs_i(inode)->parent = 0;
  281. if (type == SQUASHFS_LSYMLINK_TYPE) {
  282. __le32 xattr;
  283. err = squashfs_read_metadata(sb, NULL, &block,
  284. &offset, inode->i_size);
  285. if (err < 0)
  286. goto failed_read;
  287. err = squashfs_read_metadata(sb, &xattr, &block,
  288. &offset, sizeof(xattr));
  289. if (err < 0)
  290. goto failed_read;
  291. xattr_id = le32_to_cpu(xattr);
  292. }
  293. TRACE("Symbolic link inode %x:%x, start_block %llx, offset "
  294. "%x\n", SQUASHFS_INODE_BLK(ino), offset,
  295. block, offset);
  296. break;
  297. }
  298. case SQUASHFS_BLKDEV_TYPE:
  299. case SQUASHFS_CHRDEV_TYPE: {
  300. struct squashfs_dev_inode *sqsh_ino = &squashfs_ino.dev;
  301. unsigned int rdev;
  302. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  303. sizeof(*sqsh_ino));
  304. if (err < 0)
  305. goto failed_read;
  306. if (type == SQUASHFS_CHRDEV_TYPE)
  307. inode->i_mode |= S_IFCHR;
  308. else
  309. inode->i_mode |= S_IFBLK;
  310. set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
  311. rdev = le32_to_cpu(sqsh_ino->rdev);
  312. init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
  313. squashfs_i(inode)->parent = 0;
  314. TRACE("Device inode %x:%x, rdev %x\n",
  315. SQUASHFS_INODE_BLK(ino), offset, rdev);
  316. break;
  317. }
  318. case SQUASHFS_LBLKDEV_TYPE:
  319. case SQUASHFS_LCHRDEV_TYPE: {
  320. struct squashfs_ldev_inode *sqsh_ino = &squashfs_ino.ldev;
  321. unsigned int rdev;
  322. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  323. sizeof(*sqsh_ino));
  324. if (err < 0)
  325. goto failed_read;
  326. if (type == SQUASHFS_LCHRDEV_TYPE)
  327. inode->i_mode |= S_IFCHR;
  328. else
  329. inode->i_mode |= S_IFBLK;
  330. xattr_id = le32_to_cpu(sqsh_ino->xattr);
  331. inode->i_op = &squashfs_inode_ops;
  332. set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
  333. rdev = le32_to_cpu(sqsh_ino->rdev);
  334. init_special_inode(inode, inode->i_mode, new_decode_dev(rdev));
  335. squashfs_i(inode)->parent = 0;
  336. TRACE("Device inode %x:%x, rdev %x\n",
  337. SQUASHFS_INODE_BLK(ino), offset, rdev);
  338. break;
  339. }
  340. case SQUASHFS_FIFO_TYPE:
  341. case SQUASHFS_SOCKET_TYPE: {
  342. struct squashfs_ipc_inode *sqsh_ino = &squashfs_ino.ipc;
  343. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  344. sizeof(*sqsh_ino));
  345. if (err < 0)
  346. goto failed_read;
  347. if (type == SQUASHFS_FIFO_TYPE)
  348. inode->i_mode |= S_IFIFO;
  349. else
  350. inode->i_mode |= S_IFSOCK;
  351. set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
  352. init_special_inode(inode, inode->i_mode, 0);
  353. squashfs_i(inode)->parent = 0;
  354. break;
  355. }
  356. case SQUASHFS_LFIFO_TYPE:
  357. case SQUASHFS_LSOCKET_TYPE: {
  358. struct squashfs_lipc_inode *sqsh_ino = &squashfs_ino.lipc;
  359. err = squashfs_read_metadata(sb, sqsh_ino, &block, &offset,
  360. sizeof(*sqsh_ino));
  361. if (err < 0)
  362. goto failed_read;
  363. if (type == SQUASHFS_LFIFO_TYPE)
  364. inode->i_mode |= S_IFIFO;
  365. else
  366. inode->i_mode |= S_IFSOCK;
  367. xattr_id = le32_to_cpu(sqsh_ino->xattr);
  368. inode->i_op = &squashfs_inode_ops;
  369. set_nlink(inode, le32_to_cpu(sqsh_ino->nlink));
  370. init_special_inode(inode, inode->i_mode, 0);
  371. squashfs_i(inode)->parent = 0;
  372. break;
  373. }
  374. default:
  375. ERROR("Unknown inode type %d in squashfs_iget!\n", type);
  376. return -EINVAL;
  377. }
  378. if (xattr_id != SQUASHFS_INVALID_XATTR && msblk->xattr_id_table) {
  379. err = squashfs_xattr_lookup(sb, xattr_id,
  380. &squashfs_i(inode)->xattr_count,
  381. &squashfs_i(inode)->xattr_size,
  382. &squashfs_i(inode)->xattr);
  383. if (err < 0)
  384. goto failed_read;
  385. inode->i_blocks += ((squashfs_i(inode)->xattr_size - 1) >> 9)
  386. + 1;
  387. } else
  388. squashfs_i(inode)->xattr_count = 0;
  389. return 0;
  390. failed_read:
  391. ERROR("Unable to read inode 0x%llx\n", ino);
  392. return err;
  393. }
  394. const struct inode_operations squashfs_inode_ops = {
  395. .listxattr = squashfs_listxattr
  396. };