node.c 58 KB

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