node.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161
  1. /*
  2. * fs/f2fs/node.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/mpage.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/pagevec.h>
  17. #include <linux/swap.h>
  18. #include "f2fs.h"
  19. #include "node.h"
  20. #include "segment.h"
  21. #include "trace.h"
  22. #include <trace/events/f2fs.h>
  23. #define on_build_free_nids(nmi) mutex_is_locked(&nm_i->build_lock)
  24. static struct kmem_cache *nat_entry_slab;
  25. static struct kmem_cache *free_nid_slab;
  26. static struct kmem_cache *nat_entry_set_slab;
  27. bool available_free_memory(struct f2fs_sb_info *sbi, int type)
  28. {
  29. struct f2fs_nm_info *nm_i = NM_I(sbi);
  30. struct sysinfo val;
  31. unsigned long avail_ram;
  32. unsigned long mem_size = 0;
  33. bool res = false;
  34. si_meminfo(&val);
  35. /* only uses low memory */
  36. avail_ram = val.totalram - val.totalhigh;
  37. /*
  38. * give 25%, 25%, 50%, 50%, 50% memory for each components respectively
  39. */
  40. if (type == FREE_NIDS) {
  41. mem_size = (nm_i->fcnt * sizeof(struct free_nid)) >>
  42. PAGE_CACHE_SHIFT;
  43. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
  44. } else if (type == NAT_ENTRIES) {
  45. mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >>
  46. PAGE_CACHE_SHIFT;
  47. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
  48. } else if (type == DIRTY_DENTS) {
  49. if (sbi->sb->s_bdi->wb.dirty_exceeded)
  50. return false;
  51. mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
  52. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
  53. } else if (type == INO_ENTRIES) {
  54. int i;
  55. for (i = 0; i <= UPDATE_INO; i++)
  56. mem_size += (sbi->im[i].ino_num *
  57. sizeof(struct ino_entry)) >> PAGE_CACHE_SHIFT;
  58. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
  59. } else if (type == EXTENT_CACHE) {
  60. mem_size = (sbi->total_ext_tree * sizeof(struct extent_tree) +
  61. atomic_read(&sbi->total_ext_node) *
  62. sizeof(struct extent_node)) >> PAGE_CACHE_SHIFT;
  63. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
  64. } else {
  65. if (sbi->sb->s_bdi->wb.dirty_exceeded)
  66. return false;
  67. }
  68. return res;
  69. }
  70. static void clear_node_page_dirty(struct page *page)
  71. {
  72. struct address_space *mapping = page->mapping;
  73. unsigned int long flags;
  74. if (PageDirty(page)) {
  75. spin_lock_irqsave(&mapping->tree_lock, flags);
  76. radix_tree_tag_clear(&mapping->page_tree,
  77. page_index(page),
  78. PAGECACHE_TAG_DIRTY);
  79. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  80. clear_page_dirty_for_io(page);
  81. dec_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
  82. }
  83. ClearPageUptodate(page);
  84. }
  85. static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
  86. {
  87. pgoff_t index = current_nat_addr(sbi, nid);
  88. return get_meta_page(sbi, index);
  89. }
  90. static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
  91. {
  92. struct page *src_page;
  93. struct page *dst_page;
  94. pgoff_t src_off;
  95. pgoff_t dst_off;
  96. void *src_addr;
  97. void *dst_addr;
  98. struct f2fs_nm_info *nm_i = NM_I(sbi);
  99. src_off = current_nat_addr(sbi, nid);
  100. dst_off = next_nat_addr(sbi, src_off);
  101. /* get current nat block page with lock */
  102. src_page = get_meta_page(sbi, src_off);
  103. dst_page = grab_meta_page(sbi, dst_off);
  104. f2fs_bug_on(sbi, PageDirty(src_page));
  105. src_addr = page_address(src_page);
  106. dst_addr = page_address(dst_page);
  107. memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
  108. set_page_dirty(dst_page);
  109. f2fs_put_page(src_page, 1);
  110. set_to_next_nat(nm_i, nid);
  111. return dst_page;
  112. }
  113. static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
  114. {
  115. return radix_tree_lookup(&nm_i->nat_root, n);
  116. }
  117. static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
  118. nid_t start, unsigned int nr, struct nat_entry **ep)
  119. {
  120. return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
  121. }
  122. static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
  123. {
  124. list_del(&e->list);
  125. radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
  126. nm_i->nat_cnt--;
  127. kmem_cache_free(nat_entry_slab, e);
  128. }
  129. static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
  130. struct nat_entry *ne)
  131. {
  132. nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
  133. struct nat_entry_set *head;
  134. if (get_nat_flag(ne, IS_DIRTY))
  135. return;
  136. head = radix_tree_lookup(&nm_i->nat_set_root, set);
  137. if (!head) {
  138. head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_NOFS);
  139. INIT_LIST_HEAD(&head->entry_list);
  140. INIT_LIST_HEAD(&head->set_list);
  141. head->set = set;
  142. head->entry_cnt = 0;
  143. f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
  144. }
  145. list_move_tail(&ne->list, &head->entry_list);
  146. nm_i->dirty_nat_cnt++;
  147. head->entry_cnt++;
  148. set_nat_flag(ne, IS_DIRTY, true);
  149. }
  150. static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
  151. struct nat_entry *ne)
  152. {
  153. nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
  154. struct nat_entry_set *head;
  155. head = radix_tree_lookup(&nm_i->nat_set_root, set);
  156. if (head) {
  157. list_move_tail(&ne->list, &nm_i->nat_entries);
  158. set_nat_flag(ne, IS_DIRTY, false);
  159. head->entry_cnt--;
  160. nm_i->dirty_nat_cnt--;
  161. }
  162. }
  163. static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
  164. nid_t start, unsigned int nr, struct nat_entry_set **ep)
  165. {
  166. return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
  167. start, nr);
  168. }
  169. int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
  170. {
  171. struct f2fs_nm_info *nm_i = NM_I(sbi);
  172. struct nat_entry *e;
  173. bool need = false;
  174. down_read(&nm_i->nat_tree_lock);
  175. e = __lookup_nat_cache(nm_i, nid);
  176. if (e) {
  177. if (!get_nat_flag(e, IS_CHECKPOINTED) &&
  178. !get_nat_flag(e, HAS_FSYNCED_INODE))
  179. need = true;
  180. }
  181. up_read(&nm_i->nat_tree_lock);
  182. return need;
  183. }
  184. bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
  185. {
  186. struct f2fs_nm_info *nm_i = NM_I(sbi);
  187. struct nat_entry *e;
  188. bool is_cp = true;
  189. down_read(&nm_i->nat_tree_lock);
  190. e = __lookup_nat_cache(nm_i, nid);
  191. if (e && !get_nat_flag(e, IS_CHECKPOINTED))
  192. is_cp = false;
  193. up_read(&nm_i->nat_tree_lock);
  194. return is_cp;
  195. }
  196. bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
  197. {
  198. struct f2fs_nm_info *nm_i = NM_I(sbi);
  199. struct nat_entry *e;
  200. bool need_update = true;
  201. down_read(&nm_i->nat_tree_lock);
  202. e = __lookup_nat_cache(nm_i, ino);
  203. if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
  204. (get_nat_flag(e, IS_CHECKPOINTED) ||
  205. get_nat_flag(e, HAS_FSYNCED_INODE)))
  206. need_update = false;
  207. up_read(&nm_i->nat_tree_lock);
  208. return need_update;
  209. }
  210. static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
  211. {
  212. struct nat_entry *new;
  213. new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_NOFS);
  214. f2fs_radix_tree_insert(&nm_i->nat_root, nid, new);
  215. memset(new, 0, sizeof(struct nat_entry));
  216. nat_set_nid(new, nid);
  217. nat_reset_flag(new);
  218. list_add_tail(&new->list, &nm_i->nat_entries);
  219. nm_i->nat_cnt++;
  220. return new;
  221. }
  222. static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
  223. struct f2fs_nat_entry *ne)
  224. {
  225. struct nat_entry *e;
  226. e = __lookup_nat_cache(nm_i, nid);
  227. if (!e) {
  228. e = grab_nat_entry(nm_i, nid);
  229. node_info_from_raw_nat(&e->ni, ne);
  230. }
  231. }
  232. static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
  233. block_t new_blkaddr, bool fsync_done)
  234. {
  235. struct f2fs_nm_info *nm_i = NM_I(sbi);
  236. struct nat_entry *e;
  237. down_write(&nm_i->nat_tree_lock);
  238. e = __lookup_nat_cache(nm_i, ni->nid);
  239. if (!e) {
  240. e = grab_nat_entry(nm_i, ni->nid);
  241. copy_node_info(&e->ni, ni);
  242. f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
  243. } else if (new_blkaddr == NEW_ADDR) {
  244. /*
  245. * when nid is reallocated,
  246. * previous nat entry can be remained in nat cache.
  247. * So, reinitialize it with new information.
  248. */
  249. copy_node_info(&e->ni, ni);
  250. f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
  251. }
  252. /* sanity check */
  253. f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
  254. f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
  255. new_blkaddr == NULL_ADDR);
  256. f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
  257. new_blkaddr == NEW_ADDR);
  258. f2fs_bug_on(sbi, is_valid_data_blkaddr(sbi, nat_get_blkaddr(e)) &&
  259. new_blkaddr == NEW_ADDR);
  260. /* increment version no as node is removed */
  261. if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
  262. unsigned char version = nat_get_version(e);
  263. nat_set_version(e, inc_node_version(version));
  264. /* in order to reuse the nid */
  265. if (nm_i->next_scan_nid > ni->nid)
  266. nm_i->next_scan_nid = ni->nid;
  267. }
  268. /* change address */
  269. nat_set_blkaddr(e, new_blkaddr);
  270. if (!is_valid_data_blkaddr(sbi, new_blkaddr))
  271. set_nat_flag(e, IS_CHECKPOINTED, false);
  272. __set_nat_cache_dirty(nm_i, e);
  273. /* update fsync_mark if its inode nat entry is still alive */
  274. if (ni->nid != ni->ino)
  275. e = __lookup_nat_cache(nm_i, ni->ino);
  276. if (e) {
  277. if (fsync_done && ni->nid == ni->ino)
  278. set_nat_flag(e, HAS_FSYNCED_INODE, true);
  279. set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
  280. }
  281. up_write(&nm_i->nat_tree_lock);
  282. }
  283. int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
  284. {
  285. struct f2fs_nm_info *nm_i = NM_I(sbi);
  286. int nr = nr_shrink;
  287. if (!down_write_trylock(&nm_i->nat_tree_lock))
  288. return 0;
  289. while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
  290. struct nat_entry *ne;
  291. ne = list_first_entry(&nm_i->nat_entries,
  292. struct nat_entry, list);
  293. __del_from_nat_cache(nm_i, ne);
  294. nr_shrink--;
  295. }
  296. up_write(&nm_i->nat_tree_lock);
  297. return nr - nr_shrink;
  298. }
  299. /*
  300. * This function always returns success
  301. */
  302. void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
  303. {
  304. struct f2fs_nm_info *nm_i = NM_I(sbi);
  305. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  306. struct f2fs_summary_block *sum = curseg->sum_blk;
  307. nid_t start_nid = START_NID(nid);
  308. struct f2fs_nat_block *nat_blk;
  309. struct page *page = NULL;
  310. struct f2fs_nat_entry ne;
  311. struct nat_entry *e;
  312. int i;
  313. ni->nid = nid;
  314. /* Check nat cache */
  315. down_read(&nm_i->nat_tree_lock);
  316. e = __lookup_nat_cache(nm_i, nid);
  317. if (e) {
  318. ni->ino = nat_get_ino(e);
  319. ni->blk_addr = nat_get_blkaddr(e);
  320. ni->version = nat_get_version(e);
  321. }
  322. up_read(&nm_i->nat_tree_lock);
  323. if (e)
  324. return;
  325. memset(&ne, 0, sizeof(struct f2fs_nat_entry));
  326. down_write(&nm_i->nat_tree_lock);
  327. /* Check current segment summary */
  328. mutex_lock(&curseg->curseg_mutex);
  329. i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
  330. if (i >= 0) {
  331. ne = nat_in_journal(sum, i);
  332. node_info_from_raw_nat(ni, &ne);
  333. }
  334. mutex_unlock(&curseg->curseg_mutex);
  335. if (i >= 0)
  336. goto cache;
  337. /* Fill node_info from nat page */
  338. page = get_current_nat_page(sbi, start_nid);
  339. nat_blk = (struct f2fs_nat_block *)page_address(page);
  340. ne = nat_blk->entries[nid - start_nid];
  341. node_info_from_raw_nat(ni, &ne);
  342. f2fs_put_page(page, 1);
  343. cache:
  344. /* cache nat entry */
  345. cache_nat_entry(NM_I(sbi), nid, &ne);
  346. up_write(&nm_i->nat_tree_lock);
  347. }
  348. /*
  349. * The maximum depth is four.
  350. * Offset[0] will have raw inode offset.
  351. */
  352. static int get_node_path(struct f2fs_inode_info *fi, long block,
  353. int offset[4], unsigned int noffset[4])
  354. {
  355. const long direct_index = ADDRS_PER_INODE(fi);
  356. const long direct_blks = ADDRS_PER_BLOCK;
  357. const long dptrs_per_blk = NIDS_PER_BLOCK;
  358. const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
  359. const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
  360. int n = 0;
  361. int level = 0;
  362. noffset[0] = 0;
  363. if (block < direct_index) {
  364. offset[n] = block;
  365. goto got;
  366. }
  367. block -= direct_index;
  368. if (block < direct_blks) {
  369. offset[n++] = NODE_DIR1_BLOCK;
  370. noffset[n] = 1;
  371. offset[n] = block;
  372. level = 1;
  373. goto got;
  374. }
  375. block -= direct_blks;
  376. if (block < direct_blks) {
  377. offset[n++] = NODE_DIR2_BLOCK;
  378. noffset[n] = 2;
  379. offset[n] = block;
  380. level = 1;
  381. goto got;
  382. }
  383. block -= direct_blks;
  384. if (block < indirect_blks) {
  385. offset[n++] = NODE_IND1_BLOCK;
  386. noffset[n] = 3;
  387. offset[n++] = block / direct_blks;
  388. noffset[n] = 4 + offset[n - 1];
  389. offset[n] = block % direct_blks;
  390. level = 2;
  391. goto got;
  392. }
  393. block -= indirect_blks;
  394. if (block < indirect_blks) {
  395. offset[n++] = NODE_IND2_BLOCK;
  396. noffset[n] = 4 + dptrs_per_blk;
  397. offset[n++] = block / direct_blks;
  398. noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
  399. offset[n] = block % direct_blks;
  400. level = 2;
  401. goto got;
  402. }
  403. block -= indirect_blks;
  404. if (block < dindirect_blks) {
  405. offset[n++] = NODE_DIND_BLOCK;
  406. noffset[n] = 5 + (dptrs_per_blk * 2);
  407. offset[n++] = block / indirect_blks;
  408. noffset[n] = 6 + (dptrs_per_blk * 2) +
  409. offset[n - 1] * (dptrs_per_blk + 1);
  410. offset[n++] = (block / direct_blks) % dptrs_per_blk;
  411. noffset[n] = 7 + (dptrs_per_blk * 2) +
  412. offset[n - 2] * (dptrs_per_blk + 1) +
  413. offset[n - 1];
  414. offset[n] = block % direct_blks;
  415. level = 3;
  416. goto got;
  417. } else {
  418. BUG();
  419. }
  420. got:
  421. return level;
  422. }
  423. /*
  424. * Caller should call f2fs_put_dnode(dn).
  425. * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
  426. * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
  427. * In the case of RDONLY_NODE, we don't need to care about mutex.
  428. */
  429. int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
  430. {
  431. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  432. struct page *npage[4];
  433. struct page *parent = NULL;
  434. int offset[4];
  435. unsigned int noffset[4];
  436. nid_t nids[4];
  437. int level, i;
  438. int err = 0;
  439. level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
  440. nids[0] = dn->inode->i_ino;
  441. npage[0] = dn->inode_page;
  442. if (!npage[0]) {
  443. npage[0] = get_node_page(sbi, nids[0]);
  444. if (IS_ERR(npage[0]))
  445. return PTR_ERR(npage[0]);
  446. }
  447. /* if inline_data is set, should not report any block indices */
  448. if (f2fs_has_inline_data(dn->inode) && index) {
  449. err = -ENOENT;
  450. f2fs_put_page(npage[0], 1);
  451. goto release_out;
  452. }
  453. parent = npage[0];
  454. if (level != 0)
  455. nids[1] = get_nid(parent, offset[0], true);
  456. dn->inode_page = npage[0];
  457. dn->inode_page_locked = true;
  458. /* get indirect or direct nodes */
  459. for (i = 1; i <= level; i++) {
  460. bool done = false;
  461. if (!nids[i] && mode == ALLOC_NODE) {
  462. /* alloc new node */
  463. if (!alloc_nid(sbi, &(nids[i]))) {
  464. err = -ENOSPC;
  465. goto release_pages;
  466. }
  467. dn->nid = nids[i];
  468. npage[i] = new_node_page(dn, noffset[i], NULL);
  469. if (IS_ERR(npage[i])) {
  470. alloc_nid_failed(sbi, nids[i]);
  471. err = PTR_ERR(npage[i]);
  472. goto release_pages;
  473. }
  474. set_nid(parent, offset[i - 1], nids[i], i == 1);
  475. alloc_nid_done(sbi, nids[i]);
  476. done = true;
  477. } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
  478. npage[i] = get_node_page_ra(parent, offset[i - 1]);
  479. if (IS_ERR(npage[i])) {
  480. err = PTR_ERR(npage[i]);
  481. goto release_pages;
  482. }
  483. done = true;
  484. }
  485. if (i == 1) {
  486. dn->inode_page_locked = false;
  487. unlock_page(parent);
  488. } else {
  489. f2fs_put_page(parent, 1);
  490. }
  491. if (!done) {
  492. npage[i] = get_node_page(sbi, nids[i]);
  493. if (IS_ERR(npage[i])) {
  494. err = PTR_ERR(npage[i]);
  495. f2fs_put_page(npage[0], 0);
  496. goto release_out;
  497. }
  498. }
  499. if (i < level) {
  500. parent = npage[i];
  501. nids[i + 1] = get_nid(parent, offset[i], false);
  502. }
  503. }
  504. dn->nid = nids[level];
  505. dn->ofs_in_node = offset[level];
  506. dn->node_page = npage[level];
  507. dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
  508. return 0;
  509. release_pages:
  510. f2fs_put_page(parent, 1);
  511. if (i > 1)
  512. f2fs_put_page(npage[0], 0);
  513. release_out:
  514. dn->inode_page = NULL;
  515. dn->node_page = NULL;
  516. return err;
  517. }
  518. static void truncate_node(struct dnode_of_data *dn)
  519. {
  520. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  521. struct node_info ni;
  522. pgoff_t index;
  523. get_node_info(sbi, dn->nid, &ni);
  524. if (dn->inode->i_blocks == 0) {
  525. f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR);
  526. goto invalidate;
  527. }
  528. f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
  529. /* Deallocate node address */
  530. invalidate_blocks(sbi, ni.blk_addr);
  531. dec_valid_node_count(sbi, dn->inode);
  532. set_node_addr(sbi, &ni, NULL_ADDR, false);
  533. if (dn->nid == dn->inode->i_ino) {
  534. remove_orphan_inode(sbi, dn->nid);
  535. dec_valid_inode_count(sbi);
  536. } else {
  537. sync_inode_page(dn);
  538. }
  539. invalidate:
  540. clear_node_page_dirty(dn->node_page);
  541. set_sbi_flag(sbi, SBI_IS_DIRTY);
  542. index = dn->node_page->index;
  543. f2fs_put_page(dn->node_page, 1);
  544. invalidate_mapping_pages(NODE_MAPPING(sbi),
  545. index, index);
  546. dn->node_page = NULL;
  547. trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
  548. }
  549. static int truncate_dnode(struct dnode_of_data *dn)
  550. {
  551. struct page *page;
  552. if (dn->nid == 0)
  553. return 1;
  554. /* get direct node */
  555. page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
  556. if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
  557. return 1;
  558. else if (IS_ERR(page))
  559. return PTR_ERR(page);
  560. /* Make dnode_of_data for parameter */
  561. dn->node_page = page;
  562. dn->ofs_in_node = 0;
  563. truncate_data_blocks(dn);
  564. truncate_node(dn);
  565. return 1;
  566. }
  567. static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
  568. int ofs, int depth)
  569. {
  570. struct dnode_of_data rdn = *dn;
  571. struct page *page;
  572. struct f2fs_node *rn;
  573. nid_t child_nid;
  574. unsigned int child_nofs;
  575. int freed = 0;
  576. int i, ret;
  577. if (dn->nid == 0)
  578. return NIDS_PER_BLOCK + 1;
  579. trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
  580. page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
  581. if (IS_ERR(page)) {
  582. trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
  583. return PTR_ERR(page);
  584. }
  585. rn = F2FS_NODE(page);
  586. if (depth < 3) {
  587. for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
  588. child_nid = le32_to_cpu(rn->in.nid[i]);
  589. if (child_nid == 0)
  590. continue;
  591. rdn.nid = child_nid;
  592. ret = truncate_dnode(&rdn);
  593. if (ret < 0)
  594. goto out_err;
  595. set_nid(page, i, 0, false);
  596. }
  597. } else {
  598. child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
  599. for (i = ofs; i < NIDS_PER_BLOCK; i++) {
  600. child_nid = le32_to_cpu(rn->in.nid[i]);
  601. if (child_nid == 0) {
  602. child_nofs += NIDS_PER_BLOCK + 1;
  603. continue;
  604. }
  605. rdn.nid = child_nid;
  606. ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
  607. if (ret == (NIDS_PER_BLOCK + 1)) {
  608. set_nid(page, i, 0, false);
  609. child_nofs += ret;
  610. } else if (ret < 0 && ret != -ENOENT) {
  611. goto out_err;
  612. }
  613. }
  614. freed = child_nofs;
  615. }
  616. if (!ofs) {
  617. /* remove current indirect node */
  618. dn->node_page = page;
  619. truncate_node(dn);
  620. freed++;
  621. } else {
  622. f2fs_put_page(page, 1);
  623. }
  624. trace_f2fs_truncate_nodes_exit(dn->inode, freed);
  625. return freed;
  626. out_err:
  627. f2fs_put_page(page, 1);
  628. trace_f2fs_truncate_nodes_exit(dn->inode, ret);
  629. return ret;
  630. }
  631. static int truncate_partial_nodes(struct dnode_of_data *dn,
  632. struct f2fs_inode *ri, int *offset, int depth)
  633. {
  634. struct page *pages[2];
  635. nid_t nid[3];
  636. nid_t child_nid;
  637. int err = 0;
  638. int i;
  639. int idx = depth - 2;
  640. nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
  641. if (!nid[0])
  642. return 0;
  643. /* get indirect nodes in the path */
  644. for (i = 0; i < idx + 1; i++) {
  645. /* reference count'll be increased */
  646. pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]);
  647. if (IS_ERR(pages[i])) {
  648. err = PTR_ERR(pages[i]);
  649. idx = i - 1;
  650. goto fail;
  651. }
  652. nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
  653. }
  654. /* free direct nodes linked to a partial indirect node */
  655. for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
  656. child_nid = get_nid(pages[idx], i, false);
  657. if (!child_nid)
  658. continue;
  659. dn->nid = child_nid;
  660. err = truncate_dnode(dn);
  661. if (err < 0)
  662. goto fail;
  663. set_nid(pages[idx], i, 0, false);
  664. }
  665. if (offset[idx + 1] == 0) {
  666. dn->node_page = pages[idx];
  667. dn->nid = nid[idx];
  668. truncate_node(dn);
  669. } else {
  670. f2fs_put_page(pages[idx], 1);
  671. }
  672. offset[idx]++;
  673. offset[idx + 1] = 0;
  674. idx--;
  675. fail:
  676. for (i = idx; i >= 0; i--)
  677. f2fs_put_page(pages[i], 1);
  678. trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
  679. return err;
  680. }
  681. /*
  682. * All the block addresses of data and nodes should be nullified.
  683. */
  684. int truncate_inode_blocks(struct inode *inode, pgoff_t from)
  685. {
  686. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  687. int err = 0, cont = 1;
  688. int level, offset[4], noffset[4];
  689. unsigned int nofs = 0;
  690. struct f2fs_inode *ri;
  691. struct dnode_of_data dn;
  692. struct page *page;
  693. trace_f2fs_truncate_inode_blocks_enter(inode, from);
  694. level = get_node_path(F2FS_I(inode), from, offset, noffset);
  695. restart:
  696. page = get_node_page(sbi, inode->i_ino);
  697. if (IS_ERR(page)) {
  698. trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
  699. return PTR_ERR(page);
  700. }
  701. set_new_dnode(&dn, inode, page, NULL, 0);
  702. unlock_page(page);
  703. ri = F2FS_INODE(page);
  704. switch (level) {
  705. case 0:
  706. case 1:
  707. nofs = noffset[1];
  708. break;
  709. case 2:
  710. nofs = noffset[1];
  711. if (!offset[level - 1])
  712. goto skip_partial;
  713. err = truncate_partial_nodes(&dn, ri, offset, level);
  714. if (err < 0 && err != -ENOENT)
  715. goto fail;
  716. nofs += 1 + NIDS_PER_BLOCK;
  717. break;
  718. case 3:
  719. nofs = 5 + 2 * NIDS_PER_BLOCK;
  720. if (!offset[level - 1])
  721. goto skip_partial;
  722. err = truncate_partial_nodes(&dn, ri, offset, level);
  723. if (err < 0 && err != -ENOENT)
  724. goto fail;
  725. break;
  726. default:
  727. BUG();
  728. }
  729. skip_partial:
  730. while (cont) {
  731. dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
  732. switch (offset[0]) {
  733. case NODE_DIR1_BLOCK:
  734. case NODE_DIR2_BLOCK:
  735. err = truncate_dnode(&dn);
  736. break;
  737. case NODE_IND1_BLOCK:
  738. case NODE_IND2_BLOCK:
  739. err = truncate_nodes(&dn, nofs, offset[1], 2);
  740. break;
  741. case NODE_DIND_BLOCK:
  742. err = truncate_nodes(&dn, nofs, offset[1], 3);
  743. cont = 0;
  744. break;
  745. default:
  746. BUG();
  747. }
  748. if (err < 0 && err != -ENOENT)
  749. goto fail;
  750. if (offset[1] == 0 &&
  751. ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
  752. lock_page(page);
  753. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  754. f2fs_put_page(page, 1);
  755. goto restart;
  756. }
  757. f2fs_wait_on_page_writeback(page, NODE);
  758. ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
  759. set_page_dirty(page);
  760. unlock_page(page);
  761. }
  762. offset[1] = 0;
  763. offset[0]++;
  764. nofs += err;
  765. }
  766. fail:
  767. f2fs_put_page(page, 0);
  768. trace_f2fs_truncate_inode_blocks_exit(inode, err);
  769. return err > 0 ? 0 : err;
  770. }
  771. int truncate_xattr_node(struct inode *inode, struct page *page)
  772. {
  773. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  774. nid_t nid = F2FS_I(inode)->i_xattr_nid;
  775. struct dnode_of_data dn;
  776. struct page *npage;
  777. if (!nid)
  778. return 0;
  779. npage = get_node_page(sbi, nid);
  780. if (IS_ERR(npage))
  781. return PTR_ERR(npage);
  782. F2FS_I(inode)->i_xattr_nid = 0;
  783. /* need to do checkpoint during fsync */
  784. F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
  785. set_new_dnode(&dn, inode, page, npage, nid);
  786. if (page)
  787. dn.inode_page_locked = true;
  788. truncate_node(&dn);
  789. return 0;
  790. }
  791. /*
  792. * Caller should grab and release a rwsem by calling f2fs_lock_op() and
  793. * f2fs_unlock_op().
  794. */
  795. int remove_inode_page(struct inode *inode)
  796. {
  797. struct dnode_of_data dn;
  798. int err;
  799. set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
  800. err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
  801. if (err)
  802. return err;
  803. err = truncate_xattr_node(inode, dn.inode_page);
  804. if (err) {
  805. f2fs_put_dnode(&dn);
  806. return err;
  807. }
  808. /* remove potential inline_data blocks */
  809. if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  810. S_ISLNK(inode->i_mode))
  811. truncate_data_blocks_range(&dn, 1);
  812. /* 0 is possible, after f2fs_new_inode() has failed */
  813. f2fs_bug_on(F2FS_I_SB(inode),
  814. inode->i_blocks != 0 && inode->i_blocks != 1);
  815. /* will put inode & node pages */
  816. truncate_node(&dn);
  817. return 0;
  818. }
  819. struct page *new_inode_page(struct inode *inode)
  820. {
  821. struct dnode_of_data dn;
  822. /* allocate inode page for new inode */
  823. set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
  824. /* caller should f2fs_put_page(page, 1); */
  825. return new_node_page(&dn, 0, NULL);
  826. }
  827. struct page *new_node_page(struct dnode_of_data *dn,
  828. unsigned int ofs, struct page *ipage)
  829. {
  830. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  831. struct node_info old_ni, new_ni;
  832. struct page *page;
  833. int err;
  834. if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
  835. return ERR_PTR(-EPERM);
  836. page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
  837. if (!page)
  838. return ERR_PTR(-ENOMEM);
  839. if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
  840. err = -ENOSPC;
  841. goto fail;
  842. }
  843. get_node_info(sbi, dn->nid, &old_ni);
  844. /* Reinitialize old_ni with new node page */
  845. f2fs_bug_on(sbi, old_ni.blk_addr != NULL_ADDR);
  846. new_ni = old_ni;
  847. new_ni.ino = dn->inode->i_ino;
  848. set_node_addr(sbi, &new_ni, NEW_ADDR, false);
  849. f2fs_wait_on_page_writeback(page, NODE);
  850. fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
  851. set_cold_node(dn->inode, page);
  852. SetPageUptodate(page);
  853. set_page_dirty(page);
  854. if (f2fs_has_xattr_block(ofs))
  855. F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
  856. dn->node_page = page;
  857. if (ipage)
  858. update_inode(dn->inode, ipage);
  859. else
  860. sync_inode_page(dn);
  861. if (ofs == 0)
  862. inc_valid_inode_count(sbi);
  863. return page;
  864. fail:
  865. clear_node_page_dirty(page);
  866. f2fs_put_page(page, 1);
  867. return ERR_PTR(err);
  868. }
  869. /*
  870. * Caller should do after getting the following values.
  871. * 0: f2fs_put_page(page, 0)
  872. * LOCKED_PAGE or error: f2fs_put_page(page, 1)
  873. */
  874. static int read_node_page(struct page *page, int rw)
  875. {
  876. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  877. struct node_info ni;
  878. struct f2fs_io_info fio = {
  879. .sbi = sbi,
  880. .type = NODE,
  881. .rw = rw,
  882. .page = page,
  883. .encrypted_page = NULL,
  884. };
  885. get_node_info(sbi, page->index, &ni);
  886. if (unlikely(ni.blk_addr == NULL_ADDR)) {
  887. ClearPageUptodate(page);
  888. return -ENOENT;
  889. }
  890. if (PageUptodate(page))
  891. return LOCKED_PAGE;
  892. fio.blk_addr = ni.blk_addr;
  893. return f2fs_submit_page_bio(&fio);
  894. }
  895. /*
  896. * Readahead a node page
  897. */
  898. void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
  899. {
  900. struct page *apage;
  901. int err;
  902. apage = find_get_page(NODE_MAPPING(sbi), nid);
  903. if (apage && PageUptodate(apage)) {
  904. f2fs_put_page(apage, 0);
  905. return;
  906. }
  907. f2fs_put_page(apage, 0);
  908. apage = grab_cache_page(NODE_MAPPING(sbi), nid);
  909. if (!apage)
  910. return;
  911. err = read_node_page(apage, READA);
  912. f2fs_put_page(apage, err ? 1 : 0);
  913. }
  914. struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
  915. {
  916. struct page *page;
  917. int err;
  918. repeat:
  919. page = grab_cache_page(NODE_MAPPING(sbi), nid);
  920. if (!page)
  921. return ERR_PTR(-ENOMEM);
  922. err = read_node_page(page, READ_SYNC);
  923. if (err < 0) {
  924. f2fs_put_page(page, 1);
  925. return ERR_PTR(err);
  926. } else if (err != LOCKED_PAGE) {
  927. lock_page(page);
  928. }
  929. if (unlikely(!PageUptodate(page) || nid != nid_of_node(page))) {
  930. ClearPageUptodate(page);
  931. f2fs_put_page(page, 1);
  932. return ERR_PTR(-EIO);
  933. }
  934. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  935. f2fs_put_page(page, 1);
  936. goto repeat;
  937. }
  938. return page;
  939. }
  940. /*
  941. * Return a locked page for the desired node page.
  942. * And, readahead MAX_RA_NODE number of node pages.
  943. */
  944. struct page *get_node_page_ra(struct page *parent, int start)
  945. {
  946. struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
  947. struct blk_plug plug;
  948. struct page *page;
  949. int err, i, end;
  950. nid_t nid;
  951. /* First, try getting the desired direct node. */
  952. nid = get_nid(parent, start, false);
  953. if (!nid)
  954. return ERR_PTR(-ENOENT);
  955. repeat:
  956. page = grab_cache_page(NODE_MAPPING(sbi), nid);
  957. if (!page)
  958. return ERR_PTR(-ENOMEM);
  959. err = read_node_page(page, READ_SYNC);
  960. if (err < 0) {
  961. f2fs_put_page(page, 1);
  962. return ERR_PTR(err);
  963. } else if (err == LOCKED_PAGE) {
  964. goto page_hit;
  965. }
  966. blk_start_plug(&plug);
  967. /* Then, try readahead for siblings of the desired node */
  968. end = start + MAX_RA_NODE;
  969. end = min(end, NIDS_PER_BLOCK);
  970. for (i = start + 1; i < end; i++) {
  971. nid = get_nid(parent, i, false);
  972. if (!nid)
  973. continue;
  974. ra_node_page(sbi, nid);
  975. }
  976. blk_finish_plug(&plug);
  977. lock_page(page);
  978. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  979. f2fs_put_page(page, 1);
  980. goto repeat;
  981. }
  982. page_hit:
  983. if (unlikely(!PageUptodate(page))) {
  984. f2fs_put_page(page, 1);
  985. return ERR_PTR(-EIO);
  986. }
  987. return page;
  988. }
  989. void sync_inode_page(struct dnode_of_data *dn)
  990. {
  991. if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
  992. update_inode(dn->inode, dn->node_page);
  993. } else if (dn->inode_page) {
  994. if (!dn->inode_page_locked)
  995. lock_page(dn->inode_page);
  996. update_inode(dn->inode, dn->inode_page);
  997. if (!dn->inode_page_locked)
  998. unlock_page(dn->inode_page);
  999. } else {
  1000. update_inode_page(dn->inode);
  1001. }
  1002. }
  1003. int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
  1004. struct writeback_control *wbc)
  1005. {
  1006. pgoff_t index, end;
  1007. struct pagevec pvec;
  1008. int step = ino ? 2 : 0;
  1009. int nwritten = 0, wrote = 0;
  1010. pagevec_init(&pvec, 0);
  1011. next_step:
  1012. index = 0;
  1013. end = LONG_MAX;
  1014. while (index <= end) {
  1015. int i, nr_pages;
  1016. nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  1017. PAGECACHE_TAG_DIRTY,
  1018. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  1019. if (nr_pages == 0)
  1020. break;
  1021. for (i = 0; i < nr_pages; i++) {
  1022. struct page *page = pvec.pages[i];
  1023. /*
  1024. * flushing sequence with step:
  1025. * 0. indirect nodes
  1026. * 1. dentry dnodes
  1027. * 2. file dnodes
  1028. */
  1029. if (step == 0 && IS_DNODE(page))
  1030. continue;
  1031. if (step == 1 && (!IS_DNODE(page) ||
  1032. is_cold_node(page)))
  1033. continue;
  1034. if (step == 2 && (!IS_DNODE(page) ||
  1035. !is_cold_node(page)))
  1036. continue;
  1037. /*
  1038. * If an fsync mode,
  1039. * we should not skip writing node pages.
  1040. */
  1041. if (ino && ino_of_node(page) == ino)
  1042. lock_page(page);
  1043. else if (!trylock_page(page))
  1044. continue;
  1045. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1046. continue_unlock:
  1047. unlock_page(page);
  1048. continue;
  1049. }
  1050. if (ino && ino_of_node(page) != ino)
  1051. goto continue_unlock;
  1052. if (!PageDirty(page)) {
  1053. /* someone wrote it for us */
  1054. goto continue_unlock;
  1055. }
  1056. if (!clear_page_dirty_for_io(page))
  1057. goto continue_unlock;
  1058. /* called by fsync() */
  1059. if (ino && IS_DNODE(page)) {
  1060. set_fsync_mark(page, 1);
  1061. if (IS_INODE(page))
  1062. set_dentry_mark(page,
  1063. need_dentry_mark(sbi, ino));
  1064. nwritten++;
  1065. } else {
  1066. set_fsync_mark(page, 0);
  1067. set_dentry_mark(page, 0);
  1068. }
  1069. if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
  1070. unlock_page(page);
  1071. else
  1072. wrote++;
  1073. if (--wbc->nr_to_write == 0)
  1074. break;
  1075. }
  1076. pagevec_release(&pvec);
  1077. cond_resched();
  1078. if (wbc->nr_to_write == 0) {
  1079. step = 2;
  1080. break;
  1081. }
  1082. }
  1083. if (step < 2) {
  1084. step++;
  1085. goto next_step;
  1086. }
  1087. if (wrote)
  1088. f2fs_submit_merged_bio(sbi, NODE, WRITE);
  1089. return nwritten;
  1090. }
  1091. int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
  1092. {
  1093. pgoff_t index = 0, end = LONG_MAX;
  1094. struct pagevec pvec;
  1095. int ret2 = 0, ret = 0;
  1096. pagevec_init(&pvec, 0);
  1097. while (index <= end) {
  1098. int i, nr_pages;
  1099. nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  1100. PAGECACHE_TAG_WRITEBACK,
  1101. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  1102. if (nr_pages == 0)
  1103. break;
  1104. for (i = 0; i < nr_pages; i++) {
  1105. struct page *page = pvec.pages[i];
  1106. /* until radix tree lookup accepts end_index */
  1107. if (unlikely(page->index > end))
  1108. continue;
  1109. if (ino && ino_of_node(page) == ino) {
  1110. f2fs_wait_on_page_writeback(page, NODE);
  1111. if (TestClearPageError(page))
  1112. ret = -EIO;
  1113. }
  1114. }
  1115. pagevec_release(&pvec);
  1116. cond_resched();
  1117. }
  1118. if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
  1119. ret2 = -ENOSPC;
  1120. if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
  1121. ret2 = -EIO;
  1122. if (!ret)
  1123. ret = ret2;
  1124. return ret;
  1125. }
  1126. static int f2fs_write_node_page(struct page *page,
  1127. struct writeback_control *wbc)
  1128. {
  1129. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  1130. nid_t nid;
  1131. struct node_info ni;
  1132. struct f2fs_io_info fio = {
  1133. .sbi = sbi,
  1134. .type = NODE,
  1135. .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
  1136. .page = page,
  1137. .encrypted_page = NULL,
  1138. };
  1139. trace_f2fs_writepage(page, NODE);
  1140. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  1141. goto redirty_out;
  1142. if (unlikely(f2fs_cp_error(sbi)))
  1143. goto redirty_out;
  1144. f2fs_wait_on_page_writeback(page, NODE);
  1145. /* get old block addr of this node page */
  1146. nid = nid_of_node(page);
  1147. f2fs_bug_on(sbi, page->index != nid);
  1148. if (wbc->for_reclaim) {
  1149. if (!down_read_trylock(&sbi->node_write))
  1150. goto redirty_out;
  1151. } else {
  1152. down_read(&sbi->node_write);
  1153. }
  1154. get_node_info(sbi, nid, &ni);
  1155. /* This page is already truncated */
  1156. if (unlikely(ni.blk_addr == NULL_ADDR)) {
  1157. ClearPageUptodate(page);
  1158. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1159. up_read(&sbi->node_write);
  1160. unlock_page(page);
  1161. return 0;
  1162. }
  1163. if (__is_valid_data_blkaddr(ni.blk_addr) &&
  1164. !f2fs_is_valid_blkaddr(sbi, ni.blk_addr, DATA_GENERIC)) {
  1165. up_read(&sbi->node_write);
  1166. goto redirty_out;
  1167. }
  1168. set_page_writeback(page);
  1169. fio.blk_addr = ni.blk_addr;
  1170. write_node_page(nid, &fio);
  1171. set_node_addr(sbi, &ni, fio.blk_addr, is_fsync_dnode(page));
  1172. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1173. up_read(&sbi->node_write);
  1174. unlock_page(page);
  1175. if (wbc->for_reclaim)
  1176. f2fs_submit_merged_bio(sbi, NODE, WRITE);
  1177. return 0;
  1178. redirty_out:
  1179. redirty_page_for_writepage(wbc, page);
  1180. return AOP_WRITEPAGE_ACTIVATE;
  1181. }
  1182. static int f2fs_write_node_pages(struct address_space *mapping,
  1183. struct writeback_control *wbc)
  1184. {
  1185. struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
  1186. long diff;
  1187. trace_f2fs_writepages(mapping->host, wbc, NODE);
  1188. /* balancing f2fs's metadata in background */
  1189. f2fs_balance_fs_bg(sbi);
  1190. /* collect a number of dirty node pages and write together */
  1191. if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
  1192. goto skip_write;
  1193. diff = nr_pages_to_write(sbi, NODE, wbc);
  1194. wbc->sync_mode = WB_SYNC_NONE;
  1195. sync_node_pages(sbi, 0, wbc);
  1196. wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
  1197. return 0;
  1198. skip_write:
  1199. wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
  1200. return 0;
  1201. }
  1202. static int f2fs_set_node_page_dirty(struct page *page)
  1203. {
  1204. trace_f2fs_set_page_dirty(page, NODE);
  1205. SetPageUptodate(page);
  1206. if (!PageDirty(page)) {
  1207. __set_page_dirty_nobuffers(page);
  1208. inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
  1209. SetPagePrivate(page);
  1210. f2fs_trace_pid(page);
  1211. return 1;
  1212. }
  1213. return 0;
  1214. }
  1215. /*
  1216. * Structure of the f2fs node operations
  1217. */
  1218. const struct address_space_operations f2fs_node_aops = {
  1219. .writepage = f2fs_write_node_page,
  1220. .writepages = f2fs_write_node_pages,
  1221. .set_page_dirty = f2fs_set_node_page_dirty,
  1222. .invalidatepage = f2fs_invalidate_page,
  1223. .releasepage = f2fs_release_page,
  1224. };
  1225. static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
  1226. nid_t n)
  1227. {
  1228. return radix_tree_lookup(&nm_i->free_nid_root, n);
  1229. }
  1230. static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
  1231. struct free_nid *i)
  1232. {
  1233. list_del(&i->list);
  1234. radix_tree_delete(&nm_i->free_nid_root, i->nid);
  1235. }
  1236. static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
  1237. {
  1238. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1239. struct free_nid *i, *e;
  1240. struct nat_entry *ne;
  1241. int err = -EINVAL;
  1242. if (!available_free_memory(sbi, FREE_NIDS))
  1243. return -1;
  1244. /* 0 nid should not be used */
  1245. if (unlikely(nid == 0))
  1246. return 0;
  1247. i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
  1248. i->nid = nid;
  1249. i->state = NID_NEW;
  1250. if (radix_tree_preload(GFP_NOFS))
  1251. goto err;
  1252. spin_lock(&nm_i->free_nid_list_lock);
  1253. if (build) {
  1254. /*
  1255. * Thread A Thread B
  1256. * - f2fs_create
  1257. * - f2fs_new_inode
  1258. * - alloc_nid
  1259. * - __insert_nid_to_list(ALLOC_NID_LIST)
  1260. * - f2fs_balance_fs_bg
  1261. * - build_free_nids
  1262. * - __build_free_nids
  1263. * - scan_nat_page
  1264. * - add_free_nid
  1265. * - __lookup_nat_cache
  1266. * - f2fs_add_link
  1267. * - init_inode_metadata
  1268. * - new_inode_page
  1269. * - new_node_page
  1270. * - set_node_addr
  1271. * - alloc_nid_done
  1272. * - __remove_nid_from_list(ALLOC_NID_LIST)
  1273. * - __insert_nid_to_list(FREE_NID_LIST)
  1274. */
  1275. ne = __lookup_nat_cache(nm_i, nid);
  1276. if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) ||
  1277. nat_get_blkaddr(ne) != NULL_ADDR))
  1278. goto err_out;
  1279. e = __lookup_free_nid_list(nm_i, nid);
  1280. if (e)
  1281. goto err_out;
  1282. }
  1283. if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i))
  1284. goto err_out;
  1285. err = 0;
  1286. list_add_tail(&i->list, &nm_i->free_nid_list);
  1287. nm_i->fcnt++;
  1288. err_out:
  1289. spin_unlock(&nm_i->free_nid_list_lock);
  1290. radix_tree_preload_end();
  1291. err:
  1292. if (err)
  1293. kmem_cache_free(free_nid_slab, i);
  1294. return !err;
  1295. }
  1296. static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
  1297. {
  1298. struct free_nid *i;
  1299. bool need_free = false;
  1300. spin_lock(&nm_i->free_nid_list_lock);
  1301. i = __lookup_free_nid_list(nm_i, nid);
  1302. if (i && i->state == NID_NEW) {
  1303. __del_from_free_nid_list(nm_i, i);
  1304. nm_i->fcnt--;
  1305. need_free = true;
  1306. }
  1307. spin_unlock(&nm_i->free_nid_list_lock);
  1308. if (need_free)
  1309. kmem_cache_free(free_nid_slab, i);
  1310. }
  1311. static void scan_nat_page(struct f2fs_sb_info *sbi,
  1312. struct page *nat_page, nid_t start_nid)
  1313. {
  1314. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1315. struct f2fs_nat_block *nat_blk = page_address(nat_page);
  1316. block_t blk_addr;
  1317. int i;
  1318. i = start_nid % NAT_ENTRY_PER_BLOCK;
  1319. for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
  1320. if (unlikely(start_nid >= nm_i->max_nid))
  1321. break;
  1322. blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
  1323. f2fs_bug_on(sbi, blk_addr == NEW_ADDR);
  1324. if (blk_addr == NULL_ADDR) {
  1325. if (add_free_nid(sbi, start_nid, true) < 0)
  1326. break;
  1327. }
  1328. }
  1329. }
  1330. static void build_free_nids(struct f2fs_sb_info *sbi)
  1331. {
  1332. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1333. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1334. struct f2fs_summary_block *sum = curseg->sum_blk;
  1335. int i = 0;
  1336. nid_t nid = nm_i->next_scan_nid;
  1337. /* Enough entries */
  1338. if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
  1339. return;
  1340. /* readahead nat pages to be scanned */
  1341. ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES,
  1342. META_NAT, true);
  1343. down_read(&nm_i->nat_tree_lock);
  1344. while (1) {
  1345. struct page *page = get_current_nat_page(sbi, nid);
  1346. scan_nat_page(sbi, page, nid);
  1347. f2fs_put_page(page, 1);
  1348. nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
  1349. if (unlikely(nid >= nm_i->max_nid))
  1350. nid = 0;
  1351. if (++i >= FREE_NID_PAGES)
  1352. break;
  1353. }
  1354. /* go to the next free nat pages to find free nids abundantly */
  1355. nm_i->next_scan_nid = nid;
  1356. /* find free nids from current sum_pages */
  1357. mutex_lock(&curseg->curseg_mutex);
  1358. for (i = 0; i < nats_in_cursum(sum); i++) {
  1359. block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
  1360. nid = le32_to_cpu(nid_in_journal(sum, i));
  1361. if (addr == NULL_ADDR)
  1362. add_free_nid(sbi, nid, true);
  1363. else
  1364. remove_free_nid(nm_i, nid);
  1365. }
  1366. mutex_unlock(&curseg->curseg_mutex);
  1367. up_read(&nm_i->nat_tree_lock);
  1368. ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid),
  1369. nm_i->ra_nid_pages, META_NAT, false);
  1370. }
  1371. /*
  1372. * If this function returns success, caller can obtain a new nid
  1373. * from second parameter of this function.
  1374. * The returned nid could be used ino as well as nid when inode is created.
  1375. */
  1376. bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
  1377. {
  1378. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1379. struct free_nid *i = NULL;
  1380. retry:
  1381. if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
  1382. return false;
  1383. spin_lock(&nm_i->free_nid_list_lock);
  1384. /* We should not use stale free nids created by build_free_nids */
  1385. if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
  1386. struct node_info ni;
  1387. f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
  1388. list_for_each_entry(i, &nm_i->free_nid_list, list)
  1389. if (i->state == NID_NEW)
  1390. break;
  1391. f2fs_bug_on(sbi, i->state != NID_NEW);
  1392. *nid = i->nid;
  1393. i->state = NID_ALLOC;
  1394. nm_i->fcnt--;
  1395. spin_unlock(&nm_i->free_nid_list_lock);
  1396. /* check nid is allocated already */
  1397. get_node_info(sbi, *nid, &ni);
  1398. if (ni.blk_addr != NULL_ADDR) {
  1399. alloc_nid_done(sbi, *nid);
  1400. goto retry;
  1401. }
  1402. return true;
  1403. }
  1404. spin_unlock(&nm_i->free_nid_list_lock);
  1405. /* Let's scan nat pages and its caches to get free nids */
  1406. mutex_lock(&nm_i->build_lock);
  1407. build_free_nids(sbi);
  1408. mutex_unlock(&nm_i->build_lock);
  1409. goto retry;
  1410. }
  1411. /*
  1412. * alloc_nid() should be called prior to this function.
  1413. */
  1414. void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
  1415. {
  1416. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1417. struct free_nid *i;
  1418. spin_lock(&nm_i->free_nid_list_lock);
  1419. i = __lookup_free_nid_list(nm_i, nid);
  1420. f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
  1421. __del_from_free_nid_list(nm_i, i);
  1422. spin_unlock(&nm_i->free_nid_list_lock);
  1423. kmem_cache_free(free_nid_slab, i);
  1424. }
  1425. /*
  1426. * alloc_nid() should be called prior to this function.
  1427. */
  1428. void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
  1429. {
  1430. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1431. struct free_nid *i;
  1432. bool need_free = false;
  1433. if (!nid)
  1434. return;
  1435. spin_lock(&nm_i->free_nid_list_lock);
  1436. i = __lookup_free_nid_list(nm_i, nid);
  1437. f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
  1438. if (!available_free_memory(sbi, FREE_NIDS)) {
  1439. __del_from_free_nid_list(nm_i, i);
  1440. need_free = true;
  1441. } else {
  1442. i->state = NID_NEW;
  1443. nm_i->fcnt++;
  1444. }
  1445. spin_unlock(&nm_i->free_nid_list_lock);
  1446. if (need_free)
  1447. kmem_cache_free(free_nid_slab, i);
  1448. }
  1449. int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
  1450. {
  1451. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1452. struct free_nid *i, *next;
  1453. int nr = nr_shrink;
  1454. if (!mutex_trylock(&nm_i->build_lock))
  1455. return 0;
  1456. spin_lock(&nm_i->free_nid_list_lock);
  1457. list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
  1458. if (nr_shrink <= 0 || nm_i->fcnt <= NAT_ENTRY_PER_BLOCK)
  1459. break;
  1460. if (i->state == NID_ALLOC)
  1461. continue;
  1462. __del_from_free_nid_list(nm_i, i);
  1463. kmem_cache_free(free_nid_slab, i);
  1464. nm_i->fcnt--;
  1465. nr_shrink--;
  1466. }
  1467. spin_unlock(&nm_i->free_nid_list_lock);
  1468. mutex_unlock(&nm_i->build_lock);
  1469. return nr - nr_shrink;
  1470. }
  1471. void recover_inline_xattr(struct inode *inode, struct page *page)
  1472. {
  1473. void *src_addr, *dst_addr;
  1474. size_t inline_size;
  1475. struct page *ipage;
  1476. struct f2fs_inode *ri;
  1477. ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
  1478. f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
  1479. ri = F2FS_INODE(page);
  1480. if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
  1481. clear_inode_flag(F2FS_I(inode), FI_INLINE_XATTR);
  1482. goto update_inode;
  1483. }
  1484. dst_addr = inline_xattr_addr(ipage);
  1485. src_addr = inline_xattr_addr(page);
  1486. inline_size = inline_xattr_size(inode);
  1487. f2fs_wait_on_page_writeback(ipage, NODE);
  1488. memcpy(dst_addr, src_addr, inline_size);
  1489. update_inode:
  1490. update_inode(inode, ipage);
  1491. f2fs_put_page(ipage, 1);
  1492. }
  1493. void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
  1494. {
  1495. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1496. nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
  1497. nid_t new_xnid = nid_of_node(page);
  1498. struct node_info ni;
  1499. /* 1: invalidate the previous xattr nid */
  1500. if (!prev_xnid)
  1501. goto recover_xnid;
  1502. /* Deallocate node address */
  1503. get_node_info(sbi, prev_xnid, &ni);
  1504. f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
  1505. invalidate_blocks(sbi, ni.blk_addr);
  1506. dec_valid_node_count(sbi, inode);
  1507. set_node_addr(sbi, &ni, NULL_ADDR, false);
  1508. recover_xnid:
  1509. /* 2: allocate new xattr nid */
  1510. if (unlikely(!inc_valid_node_count(sbi, inode)))
  1511. f2fs_bug_on(sbi, 1);
  1512. remove_free_nid(NM_I(sbi), new_xnid);
  1513. get_node_info(sbi, new_xnid, &ni);
  1514. ni.ino = inode->i_ino;
  1515. set_node_addr(sbi, &ni, NEW_ADDR, false);
  1516. F2FS_I(inode)->i_xattr_nid = new_xnid;
  1517. /* 3: update xattr blkaddr */
  1518. refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
  1519. set_node_addr(sbi, &ni, blkaddr, false);
  1520. update_inode_page(inode);
  1521. }
  1522. int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
  1523. {
  1524. struct f2fs_inode *src, *dst;
  1525. nid_t ino = ino_of_node(page);
  1526. struct node_info old_ni, new_ni;
  1527. struct page *ipage;
  1528. get_node_info(sbi, ino, &old_ni);
  1529. if (unlikely(old_ni.blk_addr != NULL_ADDR))
  1530. return -EINVAL;
  1531. ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
  1532. if (!ipage)
  1533. return -ENOMEM;
  1534. /* Should not use this inode from free nid list */
  1535. remove_free_nid(NM_I(sbi), ino);
  1536. SetPageUptodate(ipage);
  1537. fill_node_footer(ipage, ino, ino, 0, true);
  1538. src = F2FS_INODE(page);
  1539. dst = F2FS_INODE(ipage);
  1540. memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
  1541. dst->i_size = 0;
  1542. dst->i_blocks = cpu_to_le64(1);
  1543. dst->i_links = cpu_to_le32(1);
  1544. dst->i_xattr_nid = 0;
  1545. dst->i_inline = src->i_inline & F2FS_INLINE_XATTR;
  1546. new_ni = old_ni;
  1547. new_ni.ino = ino;
  1548. if (unlikely(!inc_valid_node_count(sbi, NULL)))
  1549. WARN_ON(1);
  1550. set_node_addr(sbi, &new_ni, NEW_ADDR, false);
  1551. inc_valid_inode_count(sbi);
  1552. set_page_dirty(ipage);
  1553. f2fs_put_page(ipage, 1);
  1554. return 0;
  1555. }
  1556. int restore_node_summary(struct f2fs_sb_info *sbi,
  1557. unsigned int segno, struct f2fs_summary_block *sum)
  1558. {
  1559. struct f2fs_node *rn;
  1560. struct f2fs_summary *sum_entry;
  1561. block_t addr;
  1562. int bio_blocks = MAX_BIO_BLOCKS(sbi);
  1563. int i, idx, last_offset, nrpages;
  1564. /* scan the node segment */
  1565. last_offset = sbi->blocks_per_seg;
  1566. addr = START_BLOCK(sbi, segno);
  1567. sum_entry = &sum->entries[0];
  1568. for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
  1569. nrpages = min(last_offset - i, bio_blocks);
  1570. /* readahead node pages */
  1571. ra_meta_pages(sbi, addr, nrpages, META_POR, true);
  1572. for (idx = addr; idx < addr + nrpages; idx++) {
  1573. struct page *page = get_tmp_page(sbi, idx);
  1574. rn = F2FS_NODE(page);
  1575. sum_entry->nid = rn->footer.nid;
  1576. sum_entry->version = 0;
  1577. sum_entry->ofs_in_node = 0;
  1578. sum_entry++;
  1579. f2fs_put_page(page, 1);
  1580. }
  1581. invalidate_mapping_pages(META_MAPPING(sbi), addr,
  1582. addr + nrpages);
  1583. }
  1584. return 0;
  1585. }
  1586. static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
  1587. {
  1588. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1589. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1590. struct f2fs_summary_block *sum = curseg->sum_blk;
  1591. int i;
  1592. mutex_lock(&curseg->curseg_mutex);
  1593. for (i = 0; i < nats_in_cursum(sum); i++) {
  1594. struct nat_entry *ne;
  1595. struct f2fs_nat_entry raw_ne;
  1596. nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
  1597. raw_ne = nat_in_journal(sum, i);
  1598. ne = __lookup_nat_cache(nm_i, nid);
  1599. if (!ne) {
  1600. ne = grab_nat_entry(nm_i, nid);
  1601. node_info_from_raw_nat(&ne->ni, &raw_ne);
  1602. }
  1603. __set_nat_cache_dirty(nm_i, ne);
  1604. }
  1605. update_nats_in_cursum(sum, -i);
  1606. mutex_unlock(&curseg->curseg_mutex);
  1607. }
  1608. static void __adjust_nat_entry_set(struct nat_entry_set *nes,
  1609. struct list_head *head, int max)
  1610. {
  1611. struct nat_entry_set *cur;
  1612. if (nes->entry_cnt >= max)
  1613. goto add_out;
  1614. list_for_each_entry(cur, head, set_list) {
  1615. if (cur->entry_cnt >= nes->entry_cnt) {
  1616. list_add(&nes->set_list, cur->set_list.prev);
  1617. return;
  1618. }
  1619. }
  1620. add_out:
  1621. list_add_tail(&nes->set_list, head);
  1622. }
  1623. static void __flush_nat_entry_set(struct f2fs_sb_info *sbi,
  1624. struct nat_entry_set *set)
  1625. {
  1626. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1627. struct f2fs_summary_block *sum = curseg->sum_blk;
  1628. nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
  1629. bool to_journal = true;
  1630. struct f2fs_nat_block *nat_blk;
  1631. struct nat_entry *ne, *cur;
  1632. struct page *page = NULL;
  1633. /*
  1634. * there are two steps to flush nat entries:
  1635. * #1, flush nat entries to journal in current hot data summary block.
  1636. * #2, flush nat entries to nat page.
  1637. */
  1638. if (!__has_cursum_space(sum, set->entry_cnt, NAT_JOURNAL))
  1639. to_journal = false;
  1640. if (to_journal) {
  1641. mutex_lock(&curseg->curseg_mutex);
  1642. } else {
  1643. page = get_next_nat_page(sbi, start_nid);
  1644. nat_blk = page_address(page);
  1645. f2fs_bug_on(sbi, !nat_blk);
  1646. }
  1647. /* flush dirty nats in nat entry set */
  1648. list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
  1649. struct f2fs_nat_entry *raw_ne;
  1650. nid_t nid = nat_get_nid(ne);
  1651. int offset;
  1652. if (nat_get_blkaddr(ne) == NEW_ADDR)
  1653. continue;
  1654. if (to_journal) {
  1655. offset = lookup_journal_in_cursum(sum,
  1656. NAT_JOURNAL, nid, 1);
  1657. f2fs_bug_on(sbi, offset < 0);
  1658. raw_ne = &nat_in_journal(sum, offset);
  1659. nid_in_journal(sum, offset) = cpu_to_le32(nid);
  1660. } else {
  1661. raw_ne = &nat_blk->entries[nid - start_nid];
  1662. }
  1663. raw_nat_from_node_info(raw_ne, &ne->ni);
  1664. nat_reset_flag(ne);
  1665. __clear_nat_cache_dirty(NM_I(sbi), ne);
  1666. if (nat_get_blkaddr(ne) == NULL_ADDR)
  1667. add_free_nid(sbi, nid, false);
  1668. }
  1669. if (to_journal)
  1670. mutex_unlock(&curseg->curseg_mutex);
  1671. else
  1672. f2fs_put_page(page, 1);
  1673. f2fs_bug_on(sbi, set->entry_cnt);
  1674. radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
  1675. kmem_cache_free(nat_entry_set_slab, set);
  1676. }
  1677. /*
  1678. * This function is called during the checkpointing process.
  1679. */
  1680. void flush_nat_entries(struct f2fs_sb_info *sbi)
  1681. {
  1682. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1683. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1684. struct f2fs_summary_block *sum = curseg->sum_blk;
  1685. struct nat_entry_set *setvec[SETVEC_SIZE];
  1686. struct nat_entry_set *set, *tmp;
  1687. unsigned int found;
  1688. nid_t set_idx = 0;
  1689. LIST_HEAD(sets);
  1690. if (!nm_i->dirty_nat_cnt)
  1691. return;
  1692. down_write(&nm_i->nat_tree_lock);
  1693. /*
  1694. * if there are no enough space in journal to store dirty nat
  1695. * entries, remove all entries from journal and merge them
  1696. * into nat entry set.
  1697. */
  1698. if (!__has_cursum_space(sum, nm_i->dirty_nat_cnt, NAT_JOURNAL))
  1699. remove_nats_in_journal(sbi);
  1700. while ((found = __gang_lookup_nat_set(nm_i,
  1701. set_idx, SETVEC_SIZE, setvec))) {
  1702. unsigned idx;
  1703. set_idx = setvec[found - 1]->set + 1;
  1704. for (idx = 0; idx < found; idx++)
  1705. __adjust_nat_entry_set(setvec[idx], &sets,
  1706. MAX_NAT_JENTRIES(sum));
  1707. }
  1708. /* flush dirty nats in nat entry set */
  1709. list_for_each_entry_safe(set, tmp, &sets, set_list)
  1710. __flush_nat_entry_set(sbi, set);
  1711. up_write(&nm_i->nat_tree_lock);
  1712. f2fs_bug_on(sbi, nm_i->dirty_nat_cnt);
  1713. }
  1714. static int init_node_manager(struct f2fs_sb_info *sbi)
  1715. {
  1716. struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
  1717. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1718. unsigned char *version_bitmap;
  1719. unsigned int nat_segs, nat_blocks;
  1720. nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
  1721. /* segment_count_nat includes pair segment so divide to 2. */
  1722. nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
  1723. nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
  1724. nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
  1725. /* not used nids: 0, node, meta, (and root counted as valid node) */
  1726. nm_i->available_nids = nm_i->max_nid - F2FS_RESERVED_NODE_NUM;
  1727. nm_i->fcnt = 0;
  1728. nm_i->nat_cnt = 0;
  1729. nm_i->ram_thresh = DEF_RAM_THRESHOLD;
  1730. nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
  1731. INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
  1732. INIT_LIST_HEAD(&nm_i->free_nid_list);
  1733. INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
  1734. INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
  1735. INIT_LIST_HEAD(&nm_i->nat_entries);
  1736. mutex_init(&nm_i->build_lock);
  1737. spin_lock_init(&nm_i->free_nid_list_lock);
  1738. init_rwsem(&nm_i->nat_tree_lock);
  1739. nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
  1740. nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
  1741. version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
  1742. if (!version_bitmap)
  1743. return -EFAULT;
  1744. nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
  1745. GFP_KERNEL);
  1746. if (!nm_i->nat_bitmap)
  1747. return -ENOMEM;
  1748. return 0;
  1749. }
  1750. int build_node_manager(struct f2fs_sb_info *sbi)
  1751. {
  1752. int err;
  1753. sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
  1754. if (!sbi->nm_info)
  1755. return -ENOMEM;
  1756. err = init_node_manager(sbi);
  1757. if (err)
  1758. return err;
  1759. build_free_nids(sbi);
  1760. return 0;
  1761. }
  1762. void destroy_node_manager(struct f2fs_sb_info *sbi)
  1763. {
  1764. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1765. struct free_nid *i, *next_i;
  1766. struct nat_entry *natvec[NATVEC_SIZE];
  1767. struct nat_entry_set *setvec[SETVEC_SIZE];
  1768. nid_t nid = 0;
  1769. unsigned int found;
  1770. if (!nm_i)
  1771. return;
  1772. /* destroy free nid list */
  1773. spin_lock(&nm_i->free_nid_list_lock);
  1774. list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
  1775. f2fs_bug_on(sbi, i->state == NID_ALLOC);
  1776. __del_from_free_nid_list(nm_i, i);
  1777. nm_i->fcnt--;
  1778. spin_unlock(&nm_i->free_nid_list_lock);
  1779. kmem_cache_free(free_nid_slab, i);
  1780. spin_lock(&nm_i->free_nid_list_lock);
  1781. }
  1782. f2fs_bug_on(sbi, nm_i->fcnt);
  1783. spin_unlock(&nm_i->free_nid_list_lock);
  1784. /* destroy nat cache */
  1785. down_write(&nm_i->nat_tree_lock);
  1786. while ((found = __gang_lookup_nat_cache(nm_i,
  1787. nid, NATVEC_SIZE, natvec))) {
  1788. unsigned idx;
  1789. nid = nat_get_nid(natvec[found - 1]) + 1;
  1790. for (idx = 0; idx < found; idx++)
  1791. __del_from_nat_cache(nm_i, natvec[idx]);
  1792. }
  1793. f2fs_bug_on(sbi, nm_i->nat_cnt);
  1794. /* destroy nat set cache */
  1795. nid = 0;
  1796. while ((found = __gang_lookup_nat_set(nm_i,
  1797. nid, SETVEC_SIZE, setvec))) {
  1798. unsigned idx;
  1799. nid = setvec[found - 1]->set + 1;
  1800. for (idx = 0; idx < found; idx++) {
  1801. /* entry_cnt is not zero, when cp_error was occurred */
  1802. f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
  1803. radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
  1804. kmem_cache_free(nat_entry_set_slab, setvec[idx]);
  1805. }
  1806. }
  1807. up_write(&nm_i->nat_tree_lock);
  1808. kfree(nm_i->nat_bitmap);
  1809. sbi->nm_info = NULL;
  1810. kfree(nm_i);
  1811. }
  1812. int __init create_node_manager_caches(void)
  1813. {
  1814. nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
  1815. sizeof(struct nat_entry));
  1816. if (!nat_entry_slab)
  1817. goto fail;
  1818. free_nid_slab = f2fs_kmem_cache_create("free_nid",
  1819. sizeof(struct free_nid));
  1820. if (!free_nid_slab)
  1821. goto destroy_nat_entry;
  1822. nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
  1823. sizeof(struct nat_entry_set));
  1824. if (!nat_entry_set_slab)
  1825. goto destroy_free_nid;
  1826. return 0;
  1827. destroy_free_nid:
  1828. kmem_cache_destroy(free_nid_slab);
  1829. destroy_nat_entry:
  1830. kmem_cache_destroy(nat_entry_slab);
  1831. fail:
  1832. return -ENOMEM;
  1833. }
  1834. void destroy_node_manager_caches(void)
  1835. {
  1836. kmem_cache_destroy(nat_entry_set_slab);
  1837. kmem_cache_destroy(free_nid_slab);
  1838. kmem_cache_destroy(nat_entry_slab);
  1839. }