inode.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 <linux/bitops.h>
  16. #include "f2fs.h"
  17. #include "node.h"
  18. #include <trace/events/f2fs.h>
  19. void f2fs_set_inode_flags(struct inode *inode)
  20. {
  21. unsigned int flags = F2FS_I(inode)->i_flags;
  22. unsigned int new_fl = 0;
  23. if (flags & FS_SYNC_FL)
  24. new_fl |= S_SYNC;
  25. if (flags & FS_APPEND_FL)
  26. new_fl |= S_APPEND;
  27. if (flags & FS_IMMUTABLE_FL)
  28. new_fl |= S_IMMUTABLE;
  29. if (flags & FS_NOATIME_FL)
  30. new_fl |= S_NOATIME;
  31. if (flags & FS_DIRSYNC_FL)
  32. new_fl |= S_DIRSYNC;
  33. set_mask_bits(&inode->i_flags,
  34. S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC, new_fl);
  35. }
  36. static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
  37. {
  38. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  39. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  40. if (ri->i_addr[0])
  41. inode->i_rdev =
  42. old_decode_dev(le32_to_cpu(ri->i_addr[0]));
  43. else
  44. inode->i_rdev =
  45. new_decode_dev(le32_to_cpu(ri->i_addr[1]));
  46. }
  47. }
  48. static void __set_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
  49. {
  50. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  51. if (old_valid_dev(inode->i_rdev)) {
  52. ri->i_addr[0] =
  53. cpu_to_le32(old_encode_dev(inode->i_rdev));
  54. ri->i_addr[1] = 0;
  55. } else {
  56. ri->i_addr[0] = 0;
  57. ri->i_addr[1] =
  58. cpu_to_le32(new_encode_dev(inode->i_rdev));
  59. ri->i_addr[2] = 0;
  60. }
  61. }
  62. }
  63. static void __recover_inline_status(struct inode *inode, struct page *ipage)
  64. {
  65. void *inline_data = inline_data_addr(ipage);
  66. __le32 *start = inline_data;
  67. __le32 *end = start + MAX_INLINE_DATA / sizeof(__le32);
  68. while (start < end) {
  69. if (*start++) {
  70. f2fs_wait_on_page_writeback(ipage, NODE);
  71. set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
  72. set_raw_inline(F2FS_I(inode), F2FS_INODE(ipage));
  73. set_page_dirty(ipage);
  74. return;
  75. }
  76. }
  77. return;
  78. }
  79. static int do_read_inode(struct inode *inode)
  80. {
  81. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  82. struct f2fs_inode_info *fi = F2FS_I(inode);
  83. struct page *node_page;
  84. struct f2fs_inode *ri;
  85. /* Check if ino is within scope */
  86. if (check_nid_range(sbi, inode->i_ino)) {
  87. f2fs_msg(inode->i_sb, KERN_ERR, "bad inode number: %lu",
  88. (unsigned long) inode->i_ino);
  89. WARN_ON(1);
  90. return -EINVAL;
  91. }
  92. node_page = get_node_page(sbi, inode->i_ino);
  93. if (IS_ERR(node_page))
  94. return PTR_ERR(node_page);
  95. ri = F2FS_INODE(node_page);
  96. inode->i_mode = le16_to_cpu(ri->i_mode);
  97. i_uid_write(inode, le32_to_cpu(ri->i_uid));
  98. i_gid_write(inode, le32_to_cpu(ri->i_gid));
  99. set_nlink(inode, le32_to_cpu(ri->i_links));
  100. inode->i_size = le64_to_cpu(ri->i_size);
  101. inode->i_blocks = le64_to_cpu(ri->i_blocks);
  102. inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime);
  103. inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime);
  104. inode->i_mtime.tv_sec = le64_to_cpu(ri->i_mtime);
  105. inode->i_atime.tv_nsec = le32_to_cpu(ri->i_atime_nsec);
  106. inode->i_ctime.tv_nsec = le32_to_cpu(ri->i_ctime_nsec);
  107. inode->i_mtime.tv_nsec = le32_to_cpu(ri->i_mtime_nsec);
  108. inode->i_generation = le32_to_cpu(ri->i_generation);
  109. fi->i_current_depth = le32_to_cpu(ri->i_current_depth);
  110. fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid);
  111. fi->i_flags = le32_to_cpu(ri->i_flags);
  112. fi->flags = 0;
  113. fi->i_advise = ri->i_advise;
  114. fi->i_pino = le32_to_cpu(ri->i_pino);
  115. fi->i_dir_level = ri->i_dir_level;
  116. get_extent_info(&fi->ext, ri->i_ext);
  117. get_inline_info(fi, ri);
  118. /* check data exist */
  119. if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode))
  120. __recover_inline_status(inode, node_page);
  121. /* get rdev by using inline_info */
  122. __get_inode_rdev(inode, ri);
  123. f2fs_put_page(node_page, 1);
  124. stat_inc_inline_inode(inode);
  125. stat_inc_inline_dir(inode);
  126. return 0;
  127. }
  128. struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
  129. {
  130. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  131. struct inode *inode;
  132. int ret = 0;
  133. inode = iget_locked(sb, ino);
  134. if (!inode)
  135. return ERR_PTR(-ENOMEM);
  136. if (!(inode->i_state & I_NEW)) {
  137. trace_f2fs_iget(inode);
  138. return inode;
  139. }
  140. if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
  141. goto make_now;
  142. ret = do_read_inode(inode);
  143. if (ret)
  144. goto bad_inode;
  145. make_now:
  146. if (ino == F2FS_NODE_INO(sbi)) {
  147. inode->i_mapping->a_ops = &f2fs_node_aops;
  148. mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
  149. } else if (ino == F2FS_META_INO(sbi)) {
  150. inode->i_mapping->a_ops = &f2fs_meta_aops;
  151. mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_ZERO);
  152. } else if (S_ISREG(inode->i_mode)) {
  153. inode->i_op = &f2fs_file_inode_operations;
  154. inode->i_fop = &f2fs_file_operations;
  155. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  156. } else if (S_ISDIR(inode->i_mode)) {
  157. inode->i_op = &f2fs_dir_inode_operations;
  158. inode->i_fop = &f2fs_dir_operations;
  159. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  160. mapping_set_gfp_mask(inode->i_mapping, GFP_F2FS_HIGH_ZERO);
  161. } else if (S_ISLNK(inode->i_mode)) {
  162. inode->i_op = &f2fs_symlink_inode_operations;
  163. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  164. } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  165. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  166. inode->i_op = &f2fs_special_inode_operations;
  167. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  168. } else {
  169. ret = -EIO;
  170. goto bad_inode;
  171. }
  172. unlock_new_inode(inode);
  173. trace_f2fs_iget(inode);
  174. return inode;
  175. bad_inode:
  176. iget_failed(inode);
  177. trace_f2fs_iget_exit(inode, ret);
  178. return ERR_PTR(ret);
  179. }
  180. void update_inode(struct inode *inode, struct page *node_page)
  181. {
  182. struct f2fs_inode *ri;
  183. f2fs_wait_on_page_writeback(node_page, NODE);
  184. ri = F2FS_INODE(node_page);
  185. ri->i_mode = cpu_to_le16(inode->i_mode);
  186. ri->i_advise = F2FS_I(inode)->i_advise;
  187. ri->i_uid = cpu_to_le32(i_uid_read(inode));
  188. ri->i_gid = cpu_to_le32(i_gid_read(inode));
  189. ri->i_links = cpu_to_le32(inode->i_nlink);
  190. ri->i_size = cpu_to_le64(i_size_read(inode));
  191. ri->i_blocks = cpu_to_le64(inode->i_blocks);
  192. set_raw_extent(&F2FS_I(inode)->ext, &ri->i_ext);
  193. set_raw_inline(F2FS_I(inode), ri);
  194. ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  195. ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  196. ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
  197. ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  198. ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  199. ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  200. ri->i_current_depth = cpu_to_le32(F2FS_I(inode)->i_current_depth);
  201. ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid);
  202. ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags);
  203. ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
  204. ri->i_generation = cpu_to_le32(inode->i_generation);
  205. ri->i_dir_level = F2FS_I(inode)->i_dir_level;
  206. __set_inode_rdev(inode, ri);
  207. set_cold_node(inode, node_page);
  208. set_page_dirty(node_page);
  209. clear_inode_flag(F2FS_I(inode), FI_DIRTY_INODE);
  210. }
  211. void update_inode_page(struct inode *inode)
  212. {
  213. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  214. struct page *node_page;
  215. retry:
  216. node_page = get_node_page(sbi, inode->i_ino);
  217. if (IS_ERR(node_page)) {
  218. int err = PTR_ERR(node_page);
  219. if (err == -ENOMEM) {
  220. cond_resched();
  221. goto retry;
  222. } else if (err != -ENOENT) {
  223. f2fs_stop_checkpoint(sbi);
  224. }
  225. return;
  226. }
  227. update_inode(inode, node_page);
  228. f2fs_put_page(node_page, 1);
  229. }
  230. int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
  231. {
  232. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  233. if (inode->i_ino == F2FS_NODE_INO(sbi) ||
  234. inode->i_ino == F2FS_META_INO(sbi))
  235. return 0;
  236. if (!is_inode_flag_set(F2FS_I(inode), FI_DIRTY_INODE))
  237. return 0;
  238. /*
  239. * We need to lock here to prevent from producing dirty node pages
  240. * during the urgent cleaning time when runing out of free sections.
  241. */
  242. f2fs_lock_op(sbi);
  243. update_inode_page(inode);
  244. f2fs_unlock_op(sbi);
  245. if (wbc)
  246. f2fs_balance_fs(sbi);
  247. return 0;
  248. }
  249. /*
  250. * Called at the last iput() if i_nlink is zero
  251. */
  252. void f2fs_evict_inode(struct inode *inode)
  253. {
  254. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  255. nid_t xnid = F2FS_I(inode)->i_xattr_nid;
  256. /* some remained atomic pages should discarded */
  257. if (f2fs_is_atomic_file(inode))
  258. commit_inmem_pages(inode, true);
  259. trace_f2fs_evict_inode(inode);
  260. truncate_inode_pages_final(&inode->i_data);
  261. if (inode->i_ino == F2FS_NODE_INO(sbi) ||
  262. inode->i_ino == F2FS_META_INO(sbi))
  263. goto out_clear;
  264. f2fs_bug_on(sbi, get_dirty_pages(inode));
  265. remove_dirty_dir_inode(inode);
  266. if (inode->i_nlink || is_bad_inode(inode))
  267. goto no_delete;
  268. sb_start_intwrite(inode->i_sb);
  269. set_inode_flag(F2FS_I(inode), FI_NO_ALLOC);
  270. i_size_write(inode, 0);
  271. if (F2FS_HAS_BLOCKS(inode))
  272. f2fs_truncate(inode);
  273. f2fs_lock_op(sbi);
  274. remove_inode_page(inode);
  275. f2fs_unlock_op(sbi);
  276. sb_end_intwrite(inode->i_sb);
  277. no_delete:
  278. stat_dec_inline_dir(inode);
  279. stat_dec_inline_inode(inode);
  280. invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino, inode->i_ino);
  281. if (xnid)
  282. invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
  283. if (is_inode_flag_set(F2FS_I(inode), FI_APPEND_WRITE))
  284. add_dirty_inode(sbi, inode->i_ino, APPEND_INO);
  285. if (is_inode_flag_set(F2FS_I(inode), FI_UPDATE_WRITE))
  286. add_dirty_inode(sbi, inode->i_ino, UPDATE_INO);
  287. out_clear:
  288. clear_inode(inode);
  289. }
  290. /* caller should call f2fs_lock_op() */
  291. void handle_failed_inode(struct inode *inode)
  292. {
  293. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  294. clear_nlink(inode);
  295. make_bad_inode(inode);
  296. unlock_new_inode(inode);
  297. i_size_write(inode, 0);
  298. if (F2FS_HAS_BLOCKS(inode))
  299. f2fs_truncate(inode);
  300. remove_inode_page(inode);
  301. clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
  302. clear_inode_flag(F2FS_I(inode), FI_INLINE_DENTRY);
  303. alloc_nid_failed(sbi, inode->i_ino);
  304. f2fs_unlock_op(sbi);
  305. /* iput will drop the inode object */
  306. iput(inode);
  307. }