node.c 59 KB

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