inode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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/backing-dev.h>
  15. #include <linux/writeback.h>
  16. #include "f2fs.h"
  17. #include "node.h"
  18. #include <trace/events/f2fs.h>
  19. void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync)
  20. {
  21. if (f2fs_inode_dirtied(inode, sync))
  22. return;
  23. mark_inode_dirty_sync(inode);
  24. }
  25. void f2fs_set_inode_flags(struct inode *inode)
  26. {
  27. unsigned int flags = F2FS_I(inode)->i_flags;
  28. inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE |
  29. S_NOATIME | S_DIRSYNC);
  30. if (flags & FS_SYNC_FL)
  31. inode->i_flags |= S_SYNC;
  32. if (flags & FS_APPEND_FL)
  33. inode->i_flags |= S_APPEND;
  34. if (flags & FS_IMMUTABLE_FL)
  35. inode->i_flags |= S_IMMUTABLE;
  36. if (flags & FS_NOATIME_FL)
  37. inode->i_flags |= S_NOATIME;
  38. if (flags & FS_DIRSYNC_FL)
  39. inode->i_flags |= S_DIRSYNC;
  40. f2fs_mark_inode_dirty_sync(inode, false);
  41. }
  42. static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
  43. {
  44. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  45. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  46. if (ri->i_addr[0])
  47. inode->i_rdev =
  48. old_decode_dev(le32_to_cpu(ri->i_addr[0]));
  49. else
  50. inode->i_rdev =
  51. new_decode_dev(le32_to_cpu(ri->i_addr[1]));
  52. }
  53. }
  54. static bool __written_first_block(struct f2fs_inode *ri)
  55. {
  56. block_t addr = le32_to_cpu(ri->i_addr[0]);
  57. if (addr != NEW_ADDR && addr != NULL_ADDR)
  58. return true;
  59. return false;
  60. }
  61. static void __set_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
  62. {
  63. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  64. if (old_valid_dev(inode->i_rdev)) {
  65. ri->i_addr[0] =
  66. cpu_to_le32(old_encode_dev(inode->i_rdev));
  67. ri->i_addr[1] = 0;
  68. } else {
  69. ri->i_addr[0] = 0;
  70. ri->i_addr[1] =
  71. cpu_to_le32(new_encode_dev(inode->i_rdev));
  72. ri->i_addr[2] = 0;
  73. }
  74. }
  75. }
  76. static void __recover_inline_status(struct inode *inode, struct page *ipage)
  77. {
  78. void *inline_data = inline_data_addr(ipage);
  79. __le32 *start = inline_data;
  80. __le32 *end = start + MAX_INLINE_DATA / sizeof(__le32);
  81. while (start < end) {
  82. if (*start++) {
  83. f2fs_wait_on_page_writeback(ipage, NODE, true);
  84. set_inode_flag(inode, FI_DATA_EXIST);
  85. set_raw_inline(inode, F2FS_INODE(ipage));
  86. set_page_dirty(ipage);
  87. return;
  88. }
  89. }
  90. return;
  91. }
  92. static int do_read_inode(struct inode *inode)
  93. {
  94. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  95. struct f2fs_inode_info *fi = F2FS_I(inode);
  96. struct page *node_page;
  97. struct f2fs_inode *ri;
  98. /* Check if ino is within scope */
  99. if (check_nid_range(sbi, inode->i_ino)) {
  100. f2fs_msg(inode->i_sb, KERN_ERR, "bad inode number: %lu",
  101. (unsigned long) inode->i_ino);
  102. WARN_ON(1);
  103. return -EINVAL;
  104. }
  105. node_page = get_node_page(sbi, inode->i_ino);
  106. if (IS_ERR(node_page))
  107. return PTR_ERR(node_page);
  108. ri = F2FS_INODE(node_page);
  109. inode->i_mode = le16_to_cpu(ri->i_mode);
  110. inode->i_uid = le32_to_cpu(ri->i_uid);
  111. inode->i_gid = le32_to_cpu(ri->i_gid);
  112. set_nlink(inode, le32_to_cpu(ri->i_links));
  113. inode->i_size = le64_to_cpu(ri->i_size);
  114. inode->i_blocks = le64_to_cpu(ri->i_blocks);
  115. inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime);
  116. inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime);
  117. inode->i_mtime.tv_sec = le64_to_cpu(ri->i_mtime);
  118. inode->i_atime.tv_nsec = le32_to_cpu(ri->i_atime_nsec);
  119. inode->i_ctime.tv_nsec = le32_to_cpu(ri->i_ctime_nsec);
  120. inode->i_mtime.tv_nsec = le32_to_cpu(ri->i_mtime_nsec);
  121. inode->i_generation = le32_to_cpu(ri->i_generation);
  122. fi->i_current_depth = le32_to_cpu(ri->i_current_depth);
  123. fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid);
  124. fi->i_flags = le32_to_cpu(ri->i_flags);
  125. fi->flags = 0;
  126. fi->i_advise = ri->i_advise;
  127. fi->i_pino = le32_to_cpu(ri->i_pino);
  128. fi->i_dir_level = ri->i_dir_level;
  129. if (f2fs_init_extent_tree(inode, &ri->i_ext))
  130. set_page_dirty(node_page);
  131. get_inline_info(inode, ri);
  132. /* check data exist */
  133. if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode))
  134. __recover_inline_status(inode, node_page);
  135. /* get rdev by using inline_info */
  136. __get_inode_rdev(inode, ri);
  137. if (__written_first_block(ri))
  138. set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
  139. if (!need_inode_block_update(sbi, inode->i_ino))
  140. fi->last_disk_size = inode->i_size;
  141. f2fs_put_page(node_page, 1);
  142. stat_inc_inline_xattr(inode);
  143. stat_inc_inline_inode(inode);
  144. stat_inc_inline_dir(inode);
  145. return 0;
  146. }
  147. struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
  148. {
  149. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  150. struct inode *inode;
  151. int ret = 0;
  152. inode = iget_locked(sb, ino);
  153. if (!inode)
  154. return ERR_PTR(-ENOMEM);
  155. if (!(inode->i_state & I_NEW)) {
  156. trace_f2fs_iget(inode);
  157. return inode;
  158. }
  159. if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
  160. goto make_now;
  161. ret = do_read_inode(inode);
  162. if (ret)
  163. goto bad_inode;
  164. make_now:
  165. if (ino == F2FS_NODE_INO(sbi)) {
  166. inode->i_mapping->a_ops = &f2fs_node_aops;
  167. mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
  168. } else if (ino == F2FS_META_INO(sbi)) {
  169. inode->i_mapping->a_ops = &f2fs_meta_aops;
  170. mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
  171. } else if (S_ISREG(inode->i_mode)) {
  172. inode->i_op = &f2fs_file_inode_operations;
  173. inode->i_fop = &f2fs_file_operations;
  174. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  175. } else if (S_ISDIR(inode->i_mode)) {
  176. inode->i_op = &f2fs_dir_inode_operations;
  177. inode->i_fop = &f2fs_dir_operations;
  178. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  179. mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
  180. } else if (S_ISLNK(inode->i_mode)) {
  181. if (f2fs_encrypted_inode(inode))
  182. inode->i_op = &f2fs_encrypted_symlink_inode_operations;
  183. else
  184. inode->i_op = &f2fs_symlink_inode_operations;
  185. inode_nohighmem(inode);
  186. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  187. } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  188. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  189. inode->i_op = &f2fs_special_inode_operations;
  190. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  191. } else {
  192. ret = -EIO;
  193. goto bad_inode;
  194. }
  195. unlock_new_inode(inode);
  196. trace_f2fs_iget(inode);
  197. return inode;
  198. bad_inode:
  199. iget_failed(inode);
  200. trace_f2fs_iget_exit(inode, ret);
  201. return ERR_PTR(ret);
  202. }
  203. struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino)
  204. {
  205. struct inode *inode;
  206. retry:
  207. inode = f2fs_iget(sb, ino);
  208. if (IS_ERR(inode)) {
  209. if (PTR_ERR(inode) == -ENOMEM) {
  210. congestion_wait(BLK_RW_ASYNC, HZ/50);
  211. goto retry;
  212. }
  213. }
  214. return inode;
  215. }
  216. int update_inode(struct inode *inode, struct page *node_page)
  217. {
  218. struct f2fs_inode *ri;
  219. struct extent_tree *et = F2FS_I(inode)->extent_tree;
  220. f2fs_inode_synced(inode);
  221. f2fs_wait_on_page_writeback(node_page, NODE, true);
  222. ri = F2FS_INODE(node_page);
  223. ri->i_mode = cpu_to_le16(inode->i_mode);
  224. ri->i_advise = F2FS_I(inode)->i_advise;
  225. ri->i_uid = cpu_to_le32(inode->i_uid);
  226. ri->i_gid = cpu_to_le32(inode->i_gid);
  227. ri->i_links = cpu_to_le32(inode->i_nlink);
  228. ri->i_size = cpu_to_le64(i_size_read(inode));
  229. ri->i_blocks = cpu_to_le64(inode->i_blocks);
  230. if (et) {
  231. read_lock(&et->lock);
  232. set_raw_extent(&et->largest, &ri->i_ext);
  233. read_unlock(&et->lock);
  234. } else {
  235. memset(&ri->i_ext, 0, sizeof(ri->i_ext));
  236. }
  237. set_raw_inline(inode, ri);
  238. ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  239. ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  240. ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
  241. ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  242. ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  243. ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  244. ri->i_current_depth = cpu_to_le32(F2FS_I(inode)->i_current_depth);
  245. ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid);
  246. ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags);
  247. ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
  248. ri->i_generation = cpu_to_le32(inode->i_generation);
  249. ri->i_dir_level = F2FS_I(inode)->i_dir_level;
  250. __set_inode_rdev(inode, ri);
  251. set_cold_node(inode, node_page);
  252. /* deleted inode */
  253. if (inode->i_nlink == 0)
  254. clear_inline_node(node_page);
  255. return set_page_dirty(node_page);
  256. }
  257. int update_inode_page(struct inode *inode)
  258. {
  259. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  260. struct page *node_page;
  261. int ret = 0;
  262. retry:
  263. node_page = get_node_page(sbi, inode->i_ino);
  264. if (IS_ERR(node_page)) {
  265. int err = PTR_ERR(node_page);
  266. if (err == -ENOMEM) {
  267. cond_resched();
  268. goto retry;
  269. } else if (err != -ENOENT) {
  270. f2fs_stop_checkpoint(sbi, false);
  271. }
  272. f2fs_inode_synced(inode);
  273. return 0;
  274. }
  275. ret = update_inode(inode, node_page);
  276. f2fs_put_page(node_page, 1);
  277. return ret;
  278. }
  279. int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
  280. {
  281. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  282. if (inode->i_ino == F2FS_NODE_INO(sbi) ||
  283. inode->i_ino == F2FS_META_INO(sbi))
  284. return 0;
  285. if (!is_inode_flag_set(inode, FI_DIRTY_INODE))
  286. return 0;
  287. /*
  288. * We need to balance fs here to prevent from producing dirty node pages
  289. * during the urgent cleaning time when runing out of free sections.
  290. */
  291. if (update_inode_page(inode) && wbc && wbc->nr_to_write)
  292. f2fs_balance_fs(sbi, true);
  293. return 0;
  294. }
  295. /*
  296. * Called at the last iput() if i_nlink is zero
  297. */
  298. void f2fs_evict_inode(struct inode *inode)
  299. {
  300. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  301. nid_t xnid = F2FS_I(inode)->i_xattr_nid;
  302. int err = 0;
  303. /* some remained atomic pages should discarded */
  304. if (f2fs_is_atomic_file(inode))
  305. drop_inmem_pages(inode);
  306. trace_f2fs_evict_inode(inode);
  307. truncate_inode_pages(&inode->i_data, 0);
  308. if (inode->i_ino == F2FS_NODE_INO(sbi) ||
  309. inode->i_ino == F2FS_META_INO(sbi))
  310. goto out_clear;
  311. f2fs_bug_on(sbi, get_dirty_pages(inode));
  312. remove_dirty_inode(inode);
  313. f2fs_destroy_extent_tree(inode);
  314. if (inode->i_nlink || is_bad_inode(inode))
  315. goto no_delete;
  316. #ifdef CONFIG_F2FS_FAULT_INJECTION
  317. if (time_to_inject(sbi, FAULT_EVICT_INODE))
  318. goto no_delete;
  319. #endif
  320. remove_ino_entry(sbi, inode->i_ino, APPEND_INO);
  321. remove_ino_entry(sbi, inode->i_ino, UPDATE_INO);
  322. set_inode_flag(inode, FI_NO_ALLOC);
  323. i_size_write(inode, 0);
  324. retry:
  325. if (F2FS_HAS_BLOCKS(inode))
  326. err = f2fs_truncate(inode);
  327. if (!err) {
  328. f2fs_lock_op(sbi);
  329. err = remove_inode_page(inode);
  330. f2fs_unlock_op(sbi);
  331. if (err == -ENOENT)
  332. err = 0;
  333. }
  334. /* give more chances, if ENOMEM case */
  335. if (err == -ENOMEM) {
  336. err = 0;
  337. goto retry;
  338. }
  339. if (err)
  340. update_inode_page(inode);
  341. no_delete:
  342. stat_dec_inline_xattr(inode);
  343. stat_dec_inline_dir(inode);
  344. stat_dec_inline_inode(inode);
  345. invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino, inode->i_ino);
  346. if (xnid)
  347. invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
  348. if (inode->i_nlink) {
  349. if (is_inode_flag_set(inode, FI_APPEND_WRITE))
  350. add_ino_entry(sbi, inode->i_ino, APPEND_INO);
  351. if (is_inode_flag_set(inode, FI_UPDATE_WRITE))
  352. add_ino_entry(sbi, inode->i_ino, UPDATE_INO);
  353. }
  354. if (is_inode_flag_set(inode, FI_FREE_NID)) {
  355. alloc_nid_failed(sbi, inode->i_ino);
  356. clear_inode_flag(inode, FI_FREE_NID);
  357. }
  358. f2fs_bug_on(sbi, err &&
  359. !exist_written_data(sbi, inode->i_ino, ORPHAN_INO));
  360. out_clear:
  361. fscrypt_put_encryption_info(inode, NULL);
  362. end_writeback(inode);
  363. }
  364. /* caller should call f2fs_lock_op() */
  365. void handle_failed_inode(struct inode *inode)
  366. {
  367. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  368. struct node_info ni;
  369. /*
  370. * clear nlink of inode in order to release resource of inode
  371. * immediately.
  372. */
  373. clear_nlink(inode);
  374. /*
  375. * we must call this to avoid inode being remained as dirty, resulting
  376. * in a panic when flushing dirty inodes in gdirty_list.
  377. */
  378. update_inode_page(inode);
  379. /* don't make bad inode, since it becomes a regular file. */
  380. unlock_new_inode(inode);
  381. /*
  382. * Note: we should add inode to orphan list before f2fs_unlock_op()
  383. * so we can prevent losing this orphan when encoutering checkpoint
  384. * and following suddenly power-off.
  385. */
  386. get_node_info(sbi, inode->i_ino, &ni);
  387. if (ni.blk_addr != NULL_ADDR) {
  388. int err = acquire_orphan_inode(sbi);
  389. if (err) {
  390. set_sbi_flag(sbi, SBI_NEED_FSCK);
  391. f2fs_msg(sbi->sb, KERN_WARNING,
  392. "Too many orphan inodes, run fsck to fix.");
  393. } else {
  394. add_orphan_inode(inode);
  395. }
  396. alloc_nid_done(sbi, inode->i_ino);
  397. } else {
  398. set_inode_flag(inode, FI_FREE_NID);
  399. }
  400. f2fs_unlock_op(sbi);
  401. /* iput will drop the inode object */
  402. iput(inode);
  403. }