inode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * fs/f2fs/inode.c
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com/
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/f2fs_fs.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/writeback.h>
  15. #include "f2fs.h"
  16. #include "node.h"
  17. #include <trace/events/f2fs.h>
  18. void f2fs_set_inode_flags(struct inode *inode)
  19. {
  20. unsigned int flags = F2FS_I(inode)->i_flags;
  21. unsigned int new_fl = 0;
  22. if (flags & FS_SYNC_FL)
  23. new_fl |= S_SYNC;
  24. if (flags & FS_APPEND_FL)
  25. new_fl |= S_APPEND;
  26. if (flags & FS_IMMUTABLE_FL)
  27. new_fl |= S_IMMUTABLE;
  28. if (flags & FS_NOATIME_FL)
  29. new_fl |= S_NOATIME;
  30. if (flags & FS_DIRSYNC_FL)
  31. new_fl |= S_DIRSYNC;
  32. inode_set_flags(inode, new_fl,
  33. S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  34. }
  35. static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
  36. {
  37. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  38. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  39. if (ri->i_addr[0])
  40. inode->i_rdev =
  41. old_decode_dev(le32_to_cpu(ri->i_addr[0]));
  42. else
  43. inode->i_rdev =
  44. new_decode_dev(le32_to_cpu(ri->i_addr[1]));
  45. }
  46. }
  47. static int __written_first_block(struct f2fs_sb_info *sbi,
  48. struct f2fs_inode *ri)
  49. {
  50. block_t addr = le32_to_cpu(ri->i_addr[0]);
  51. if (!__is_valid_data_blkaddr(addr))
  52. return 1;
  53. if (!f2fs_is_valid_blkaddr(sbi, addr, DATA_GENERIC))
  54. return -EFAULT;
  55. return 0;
  56. }
  57. static void __set_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
  58. {
  59. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  60. if (old_valid_dev(inode->i_rdev)) {
  61. ri->i_addr[0] =
  62. cpu_to_le32(old_encode_dev(inode->i_rdev));
  63. ri->i_addr[1] = 0;
  64. } else {
  65. ri->i_addr[0] = 0;
  66. ri->i_addr[1] =
  67. cpu_to_le32(new_encode_dev(inode->i_rdev));
  68. ri->i_addr[2] = 0;
  69. }
  70. }
  71. }
  72. static void __recover_inline_status(struct inode *inode, struct page *ipage)
  73. {
  74. void *inline_data = inline_data_addr(ipage);
  75. __le32 *start = inline_data;
  76. __le32 *end = start + MAX_INLINE_DATA / sizeof(__le32);
  77. while (start < end) {
  78. if (*start++) {
  79. f2fs_wait_on_page_writeback(ipage, NODE);
  80. set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
  81. set_raw_inline(F2FS_I(inode), F2FS_INODE(ipage));
  82. set_page_dirty(ipage);
  83. return;
  84. }
  85. }
  86. return;
  87. }
  88. static bool sanity_check_inode(struct inode *inode, struct page *node_page)
  89. {
  90. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  91. unsigned long long iblocks;
  92. iblocks = le64_to_cpu(F2FS_INODE(node_page)->i_blocks);
  93. if (!iblocks) {
  94. set_sbi_flag(sbi, SBI_NEED_FSCK);
  95. f2fs_msg(sbi->sb, KERN_WARNING,
  96. "%s: corrupted inode i_blocks i_ino=%lx iblocks=%llu, "
  97. "run fsck to fix.",
  98. __func__, inode->i_ino, iblocks);
  99. return false;
  100. }
  101. if (ino_of_node(node_page) != nid_of_node(node_page)) {
  102. set_sbi_flag(sbi, SBI_NEED_FSCK);
  103. f2fs_msg(sbi->sb, KERN_WARNING,
  104. "%s: corrupted inode footer i_ino=%lx, ino,nid: "
  105. "[%u, %u] run fsck to fix.",
  106. __func__, inode->i_ino,
  107. ino_of_node(node_page), nid_of_node(node_page));
  108. return false;
  109. }
  110. if (F2FS_I(inode)->extent_tree) {
  111. struct extent_info *ei = &F2FS_I(inode)->extent_tree->largest;
  112. if (ei->len &&
  113. (!f2fs_is_valid_blkaddr(sbi, ei->blk, DATA_GENERIC) ||
  114. !f2fs_is_valid_blkaddr(sbi, ei->blk + ei->len - 1,
  115. DATA_GENERIC))) {
  116. set_sbi_flag(sbi, SBI_NEED_FSCK);
  117. f2fs_msg(sbi->sb, KERN_WARNING,
  118. "%s: inode (ino=%lx) extent info [%u, %u, %u] "
  119. "is incorrect, run fsck to fix",
  120. __func__, inode->i_ino,
  121. ei->blk, ei->fofs, ei->len);
  122. return false;
  123. }
  124. }
  125. return true;
  126. }
  127. static int do_read_inode(struct inode *inode)
  128. {
  129. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  130. struct f2fs_inode_info *fi = F2FS_I(inode);
  131. struct page *node_page;
  132. struct f2fs_inode *ri;
  133. int err;
  134. /* Check if ino is within scope */
  135. if (check_nid_range(sbi, inode->i_ino)) {
  136. f2fs_msg(inode->i_sb, KERN_ERR, "bad inode number: %lu",
  137. (unsigned long) inode->i_ino);
  138. WARN_ON(1);
  139. return -EINVAL;
  140. }
  141. node_page = get_node_page(sbi, inode->i_ino);
  142. if (IS_ERR(node_page))
  143. return PTR_ERR(node_page);
  144. ri = F2FS_INODE(node_page);
  145. inode->i_mode = le16_to_cpu(ri->i_mode);
  146. i_uid_write(inode, le32_to_cpu(ri->i_uid));
  147. i_gid_write(inode, le32_to_cpu(ri->i_gid));
  148. set_nlink(inode, le32_to_cpu(ri->i_links));
  149. inode->i_size = le64_to_cpu(ri->i_size);
  150. inode->i_blocks = le64_to_cpu(ri->i_blocks);
  151. inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime);
  152. inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime);
  153. inode->i_mtime.tv_sec = le64_to_cpu(ri->i_mtime);
  154. inode->i_atime.tv_nsec = le32_to_cpu(ri->i_atime_nsec);
  155. inode->i_ctime.tv_nsec = le32_to_cpu(ri->i_ctime_nsec);
  156. inode->i_mtime.tv_nsec = le32_to_cpu(ri->i_mtime_nsec);
  157. inode->i_generation = le32_to_cpu(ri->i_generation);
  158. fi->i_current_depth = le32_to_cpu(ri->i_current_depth);
  159. fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid);
  160. fi->i_flags = le32_to_cpu(ri->i_flags);
  161. fi->flags = 0;
  162. fi->i_advise = ri->i_advise;
  163. fi->i_pino = le32_to_cpu(ri->i_pino);
  164. fi->i_dir_level = ri->i_dir_level;
  165. f2fs_init_extent_tree(inode, &ri->i_ext);
  166. get_inline_info(fi, ri);
  167. if (!sanity_check_inode(inode, node_page)) {
  168. f2fs_put_page(node_page, 1);
  169. return -EINVAL;
  170. }
  171. /* check data exist */
  172. if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode))
  173. __recover_inline_status(inode, node_page);
  174. /* get rdev by using inline_info */
  175. __get_inode_rdev(inode, ri);
  176. err = __written_first_block(sbi, ri);
  177. if (err < 0) {
  178. f2fs_put_page(node_page, 1);
  179. return err;
  180. }
  181. if (!err)
  182. set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
  183. f2fs_put_page(node_page, 1);
  184. stat_inc_inline_xattr(inode);
  185. stat_inc_inline_inode(inode);
  186. stat_inc_inline_dir(inode);
  187. return 0;
  188. }
  189. struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
  190. {
  191. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  192. struct inode *inode;
  193. int ret = 0;
  194. inode = iget_locked(sb, ino);
  195. if (!inode)
  196. return ERR_PTR(-ENOMEM);
  197. if (!(inode->i_state & I_NEW)) {
  198. trace_f2fs_iget(inode);
  199. return inode;
  200. }
  201. if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
  202. goto make_now;
  203. ret = do_read_inode(inode);
  204. if (ret)
  205. goto bad_inode;
  206. make_now:
  207. if (ino == F2FS_NODE_INO(sbi)) {
  208. inode->i_mapping->a_ops = &f2fs_node_aops;
  209. mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
  210. } else if (ino == F2FS_META_INO(sbi)) {
  211. inode->i_mapping->a_ops = &f2fs_meta_aops;
  212. mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
  213. } else if (S_ISREG(inode->i_mode)) {
  214. inode->i_op = &f2fs_file_inode_operations;
  215. inode->i_fop = &f2fs_file_operations;
  216. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  217. } else if (S_ISDIR(inode->i_mode)) {
  218. inode->i_op = &f2fs_dir_inode_operations;
  219. inode->i_fop = &f2fs_dir_operations;
  220. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  221. mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
  222. } else if (S_ISLNK(inode->i_mode)) {
  223. if (f2fs_encrypted_inode(inode))
  224. inode->i_op = &f2fs_encrypted_symlink_inode_operations;
  225. else
  226. inode->i_op = &f2fs_symlink_inode_operations;
  227. inode_nohighmem(inode);
  228. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  229. } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  230. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  231. inode->i_op = &f2fs_special_inode_operations;
  232. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  233. } else {
  234. ret = -EIO;
  235. goto bad_inode;
  236. }
  237. unlock_new_inode(inode);
  238. trace_f2fs_iget(inode);
  239. return inode;
  240. bad_inode:
  241. iget_failed(inode);
  242. trace_f2fs_iget_exit(inode, ret);
  243. return ERR_PTR(ret);
  244. }
  245. void update_inode(struct inode *inode, struct page *node_page)
  246. {
  247. struct f2fs_inode *ri;
  248. f2fs_wait_on_page_writeback(node_page, NODE);
  249. ri = F2FS_INODE(node_page);
  250. ri->i_mode = cpu_to_le16(inode->i_mode);
  251. ri->i_advise = F2FS_I(inode)->i_advise;
  252. ri->i_uid = cpu_to_le32(i_uid_read(inode));
  253. ri->i_gid = cpu_to_le32(i_gid_read(inode));
  254. ri->i_links = cpu_to_le32(inode->i_nlink);
  255. ri->i_size = cpu_to_le64(i_size_read(inode));
  256. ri->i_blocks = cpu_to_le64(inode->i_blocks);
  257. if (F2FS_I(inode)->extent_tree)
  258. set_raw_extent(&F2FS_I(inode)->extent_tree->largest,
  259. &ri->i_ext);
  260. else
  261. memset(&ri->i_ext, 0, sizeof(ri->i_ext));
  262. set_raw_inline(F2FS_I(inode), ri);
  263. ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  264. ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  265. ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
  266. ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  267. ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  268. ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  269. ri->i_current_depth = cpu_to_le32(F2FS_I(inode)->i_current_depth);
  270. ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid);
  271. ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags);
  272. ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
  273. ri->i_generation = cpu_to_le32(inode->i_generation);
  274. ri->i_dir_level = F2FS_I(inode)->i_dir_level;
  275. __set_inode_rdev(inode, ri);
  276. set_cold_node(inode, node_page);
  277. set_page_dirty(node_page);
  278. clear_inode_flag(F2FS_I(inode), FI_DIRTY_INODE);
  279. }
  280. void update_inode_page(struct inode *inode)
  281. {
  282. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  283. struct page *node_page;
  284. retry:
  285. node_page = get_node_page(sbi, inode->i_ino);
  286. if (IS_ERR(node_page)) {
  287. int err = PTR_ERR(node_page);
  288. if (err == -ENOMEM) {
  289. cond_resched();
  290. goto retry;
  291. } else if (err != -ENOENT) {
  292. f2fs_stop_checkpoint(sbi);
  293. }
  294. return;
  295. }
  296. update_inode(inode, node_page);
  297. f2fs_put_page(node_page, 1);
  298. }
  299. int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
  300. {
  301. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  302. if (inode->i_ino == F2FS_NODE_INO(sbi) ||
  303. inode->i_ino == F2FS_META_INO(sbi))
  304. return 0;
  305. if (!is_inode_flag_set(F2FS_I(inode), FI_DIRTY_INODE))
  306. return 0;
  307. /*
  308. * We need to balance fs here to prevent from producing dirty node pages
  309. * during the urgent cleaning time when runing out of free sections.
  310. */
  311. update_inode_page(inode);
  312. f2fs_balance_fs(sbi);
  313. return 0;
  314. }
  315. /*
  316. * Called at the last iput() if i_nlink is zero
  317. */
  318. void f2fs_evict_inode(struct inode *inode)
  319. {
  320. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  321. struct f2fs_inode_info *fi = F2FS_I(inode);
  322. nid_t xnid = fi->i_xattr_nid;
  323. int err = 0;
  324. /* some remained atomic pages should discarded */
  325. if (f2fs_is_atomic_file(inode))
  326. commit_inmem_pages(inode, true);
  327. trace_f2fs_evict_inode(inode);
  328. truncate_inode_pages_final(&inode->i_data);
  329. if (inode->i_ino == F2FS_NODE_INO(sbi) ||
  330. inode->i_ino == F2FS_META_INO(sbi))
  331. goto out_clear;
  332. f2fs_bug_on(sbi, get_dirty_pages(inode));
  333. remove_dirty_dir_inode(inode);
  334. f2fs_destroy_extent_tree(inode);
  335. if (inode->i_nlink || is_bad_inode(inode))
  336. goto no_delete;
  337. sb_start_intwrite(inode->i_sb);
  338. set_inode_flag(fi, FI_NO_ALLOC);
  339. i_size_write(inode, 0);
  340. if (F2FS_HAS_BLOCKS(inode))
  341. err = f2fs_truncate(inode, true);
  342. if (!err) {
  343. f2fs_lock_op(sbi);
  344. err = remove_inode_page(inode);
  345. f2fs_unlock_op(sbi);
  346. }
  347. sb_end_intwrite(inode->i_sb);
  348. no_delete:
  349. stat_dec_inline_xattr(inode);
  350. stat_dec_inline_dir(inode);
  351. stat_dec_inline_inode(inode);
  352. invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino, inode->i_ino);
  353. if (xnid)
  354. invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
  355. if (is_inode_flag_set(fi, FI_APPEND_WRITE))
  356. add_dirty_inode(sbi, inode->i_ino, APPEND_INO);
  357. if (is_inode_flag_set(fi, FI_UPDATE_WRITE))
  358. add_dirty_inode(sbi, inode->i_ino, UPDATE_INO);
  359. if (is_inode_flag_set(fi, FI_FREE_NID)) {
  360. if (err && err != -ENOENT)
  361. alloc_nid_done(sbi, inode->i_ino);
  362. else
  363. alloc_nid_failed(sbi, inode->i_ino);
  364. clear_inode_flag(fi, FI_FREE_NID);
  365. }
  366. if (err && err != -ENOENT) {
  367. if (!exist_written_data(sbi, inode->i_ino, ORPHAN_INO)) {
  368. /*
  369. * get here because we failed to release resource
  370. * of inode previously, reminder our user to run fsck
  371. * for fixing.
  372. */
  373. set_sbi_flag(sbi, SBI_NEED_FSCK);
  374. f2fs_msg(sbi->sb, KERN_WARNING,
  375. "inode (ino:%lu) resource leak, run fsck "
  376. "to fix this issue!", inode->i_ino);
  377. }
  378. }
  379. out_clear:
  380. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  381. if (fi->i_crypt_info)
  382. f2fs_free_encryption_info(inode, fi->i_crypt_info);
  383. #endif
  384. clear_inode(inode);
  385. }
  386. /* caller should call f2fs_lock_op() */
  387. void handle_failed_inode(struct inode *inode)
  388. {
  389. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  390. int err = 0;
  391. clear_nlink(inode);
  392. make_bad_inode(inode);
  393. unlock_new_inode(inode);
  394. i_size_write(inode, 0);
  395. if (F2FS_HAS_BLOCKS(inode))
  396. err = f2fs_truncate(inode, false);
  397. if (!err)
  398. err = remove_inode_page(inode);
  399. /*
  400. * if we skip truncate_node in remove_inode_page bacause we failed
  401. * before, it's better to find another way to release resource of
  402. * this inode (e.g. valid block count, node block or nid). Here we
  403. * choose to add this inode to orphan list, so that we can call iput
  404. * for releasing in orphan recovery flow.
  405. *
  406. * Note: we should add inode to orphan list before f2fs_unlock_op()
  407. * so we can prevent losing this orphan when encoutering checkpoint
  408. * and following suddenly power-off.
  409. */
  410. if (err && err != -ENOENT) {
  411. err = acquire_orphan_inode(sbi);
  412. if (!err)
  413. add_orphan_inode(sbi, inode->i_ino);
  414. }
  415. set_inode_flag(F2FS_I(inode), FI_FREE_NID);
  416. f2fs_unlock_op(sbi);
  417. /* iput will drop the inode object */
  418. iput(inode);
  419. }