inode.c 12 KB

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