inode.c 12 KB

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