inode.c 12 KB

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