node.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478
  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. #ifndef PTR_ERR_OR_ZERO
  25. static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
  26. {
  27. if (IS_ERR(ptr))
  28. return PTR_ERR(ptr);
  29. else
  30. return 0;
  31. }
  32. #endif
  33. static struct kmem_cache *nat_entry_slab;
  34. static struct kmem_cache *free_nid_slab;
  35. static struct kmem_cache *nat_entry_set_slab;
  36. bool available_free_memory(struct f2fs_sb_info *sbi, int type)
  37. {
  38. struct f2fs_nm_info *nm_i = NM_I(sbi);
  39. struct sysinfo val;
  40. unsigned long avail_ram;
  41. unsigned long mem_size = 0;
  42. bool res = false;
  43. si_meminfo(&val);
  44. /* only uses low memory */
  45. avail_ram = val.totalram - val.totalhigh;
  46. /*
  47. * give 25%, 25%, 50%, 50%, 50% memory for each components respectively
  48. */
  49. if (type == FREE_NIDS) {
  50. mem_size = (nm_i->nid_cnt[FREE_NID_LIST] *
  51. sizeof(struct free_nid)) >> PAGE_SHIFT;
  52. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
  53. } else if (type == NAT_ENTRIES) {
  54. mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >>
  55. PAGE_SHIFT;
  56. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
  57. if (excess_cached_nats(sbi))
  58. res = false;
  59. } else if (type == DIRTY_DENTS) {
  60. if (sbi->sb->s_bdi->dirty_exceeded)
  61. return false;
  62. mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
  63. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
  64. } else if (type == INO_ENTRIES) {
  65. int i;
  66. for (i = 0; i <= UPDATE_INO; i++)
  67. mem_size += (sbi->im[i].ino_num *
  68. sizeof(struct ino_entry)) >> PAGE_SHIFT;
  69. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
  70. } else if (type == EXTENT_CACHE) {
  71. mem_size = (atomic_read(&sbi->total_ext_tree) *
  72. sizeof(struct extent_tree) +
  73. atomic_read(&sbi->total_ext_node) *
  74. sizeof(struct extent_node)) >> PAGE_SHIFT;
  75. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
  76. } else {
  77. if (!sbi->sb->s_bdi->dirty_exceeded)
  78. return true;
  79. }
  80. return res;
  81. }
  82. static void clear_node_page_dirty(struct page *page)
  83. {
  84. struct address_space *mapping = page->mapping;
  85. unsigned int long flags;
  86. if (PageDirty(page)) {
  87. spin_lock_irqsave(&mapping->tree_lock, flags);
  88. radix_tree_tag_clear(&mapping->page_tree,
  89. page_index(page),
  90. PAGECACHE_TAG_DIRTY);
  91. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  92. clear_page_dirty_for_io(page);
  93. dec_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
  94. }
  95. ClearPageUptodate(page);
  96. }
  97. static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
  98. {
  99. pgoff_t index = current_nat_addr(sbi, nid);
  100. return get_meta_page(sbi, index);
  101. }
  102. static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
  103. {
  104. struct page *src_page;
  105. struct page *dst_page;
  106. pgoff_t src_off;
  107. pgoff_t dst_off;
  108. void *src_addr;
  109. void *dst_addr;
  110. struct f2fs_nm_info *nm_i = NM_I(sbi);
  111. src_off = current_nat_addr(sbi, nid);
  112. dst_off = next_nat_addr(sbi, src_off);
  113. /* get current nat block page with lock */
  114. src_page = get_meta_page(sbi, src_off);
  115. dst_page = grab_meta_page(sbi, dst_off);
  116. f2fs_bug_on(sbi, PageDirty(src_page));
  117. src_addr = page_address(src_page);
  118. dst_addr = page_address(dst_page);
  119. memcpy(dst_addr, src_addr, PAGE_SIZE);
  120. set_page_dirty(dst_page);
  121. f2fs_put_page(src_page, 1);
  122. set_to_next_nat(nm_i, nid);
  123. return dst_page;
  124. }
  125. static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
  126. {
  127. return radix_tree_lookup(&nm_i->nat_root, n);
  128. }
  129. static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
  130. nid_t start, unsigned int nr, struct nat_entry **ep)
  131. {
  132. return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
  133. }
  134. static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
  135. {
  136. list_del(&e->list);
  137. radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
  138. nm_i->nat_cnt--;
  139. kmem_cache_free(nat_entry_slab, e);
  140. }
  141. static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
  142. struct nat_entry *ne)
  143. {
  144. nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
  145. struct nat_entry_set *head;
  146. if (get_nat_flag(ne, IS_DIRTY))
  147. return;
  148. head = radix_tree_lookup(&nm_i->nat_set_root, set);
  149. if (!head) {
  150. head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_NOFS);
  151. INIT_LIST_HEAD(&head->entry_list);
  152. INIT_LIST_HEAD(&head->set_list);
  153. head->set = set;
  154. head->entry_cnt = 0;
  155. f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
  156. }
  157. list_move_tail(&ne->list, &head->entry_list);
  158. nm_i->dirty_nat_cnt++;
  159. head->entry_cnt++;
  160. set_nat_flag(ne, IS_DIRTY, true);
  161. }
  162. static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
  163. struct nat_entry *ne)
  164. {
  165. nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
  166. struct nat_entry_set *head;
  167. head = radix_tree_lookup(&nm_i->nat_set_root, set);
  168. if (head) {
  169. list_move_tail(&ne->list, &nm_i->nat_entries);
  170. set_nat_flag(ne, IS_DIRTY, false);
  171. head->entry_cnt--;
  172. nm_i->dirty_nat_cnt--;
  173. }
  174. }
  175. static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
  176. nid_t start, unsigned int nr, struct nat_entry_set **ep)
  177. {
  178. return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
  179. start, nr);
  180. }
  181. int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
  182. {
  183. struct f2fs_nm_info *nm_i = NM_I(sbi);
  184. struct nat_entry *e;
  185. bool need = false;
  186. down_read(&nm_i->nat_tree_lock);
  187. e = __lookup_nat_cache(nm_i, nid);
  188. if (e) {
  189. if (!get_nat_flag(e, IS_CHECKPOINTED) &&
  190. !get_nat_flag(e, HAS_FSYNCED_INODE))
  191. need = true;
  192. }
  193. up_read(&nm_i->nat_tree_lock);
  194. return need;
  195. }
  196. bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
  197. {
  198. struct f2fs_nm_info *nm_i = NM_I(sbi);
  199. struct nat_entry *e;
  200. bool is_cp = true;
  201. down_read(&nm_i->nat_tree_lock);
  202. e = __lookup_nat_cache(nm_i, nid);
  203. if (e && !get_nat_flag(e, IS_CHECKPOINTED))
  204. is_cp = false;
  205. up_read(&nm_i->nat_tree_lock);
  206. return is_cp;
  207. }
  208. bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
  209. {
  210. struct f2fs_nm_info *nm_i = NM_I(sbi);
  211. struct nat_entry *e;
  212. bool need_update = true;
  213. down_read(&nm_i->nat_tree_lock);
  214. e = __lookup_nat_cache(nm_i, ino);
  215. if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
  216. (get_nat_flag(e, IS_CHECKPOINTED) ||
  217. get_nat_flag(e, HAS_FSYNCED_INODE)))
  218. need_update = false;
  219. up_read(&nm_i->nat_tree_lock);
  220. return need_update;
  221. }
  222. static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
  223. {
  224. struct nat_entry *new;
  225. new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_NOFS);
  226. f2fs_radix_tree_insert(&nm_i->nat_root, nid, new);
  227. memset(new, 0, sizeof(struct nat_entry));
  228. nat_set_nid(new, nid);
  229. nat_reset_flag(new);
  230. list_add_tail(&new->list, &nm_i->nat_entries);
  231. nm_i->nat_cnt++;
  232. return new;
  233. }
  234. static void cache_nat_entry(struct f2fs_sb_info *sbi, nid_t nid,
  235. struct f2fs_nat_entry *ne)
  236. {
  237. struct f2fs_nm_info *nm_i = NM_I(sbi);
  238. struct nat_entry *e;
  239. e = __lookup_nat_cache(nm_i, nid);
  240. if (!e) {
  241. e = grab_nat_entry(nm_i, nid);
  242. node_info_from_raw_nat(&e->ni, ne);
  243. } else {
  244. f2fs_bug_on(sbi, nat_get_ino(e) != le32_to_cpu(ne->ino) ||
  245. nat_get_blkaddr(e) !=
  246. le32_to_cpu(ne->block_addr) ||
  247. nat_get_version(e) != ne->version);
  248. }
  249. }
  250. static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
  251. block_t new_blkaddr, bool fsync_done)
  252. {
  253. struct f2fs_nm_info *nm_i = NM_I(sbi);
  254. struct nat_entry *e;
  255. down_write(&nm_i->nat_tree_lock);
  256. e = __lookup_nat_cache(nm_i, ni->nid);
  257. if (!e) {
  258. e = grab_nat_entry(nm_i, ni->nid);
  259. copy_node_info(&e->ni, ni);
  260. f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
  261. } else if (new_blkaddr == NEW_ADDR) {
  262. /*
  263. * when nid is reallocated,
  264. * previous nat entry can be remained in nat cache.
  265. * So, reinitialize it with new information.
  266. */
  267. copy_node_info(&e->ni, ni);
  268. f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
  269. }
  270. /* sanity check */
  271. f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
  272. f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
  273. new_blkaddr == NULL_ADDR);
  274. f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
  275. new_blkaddr == NEW_ADDR);
  276. f2fs_bug_on(sbi, nat_get_blkaddr(e) != NEW_ADDR &&
  277. nat_get_blkaddr(e) != NULL_ADDR &&
  278. new_blkaddr == NEW_ADDR);
  279. /* increment version no as node is removed */
  280. if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
  281. unsigned char version = nat_get_version(e);
  282. nat_set_version(e, inc_node_version(version));
  283. /* in order to reuse the nid */
  284. if (nm_i->next_scan_nid > ni->nid)
  285. nm_i->next_scan_nid = ni->nid;
  286. }
  287. /* change address */
  288. nat_set_blkaddr(e, new_blkaddr);
  289. if (new_blkaddr == NEW_ADDR || new_blkaddr == NULL_ADDR)
  290. set_nat_flag(e, IS_CHECKPOINTED, false);
  291. __set_nat_cache_dirty(nm_i, e);
  292. /* update fsync_mark if its inode nat entry is still alive */
  293. if (ni->nid != ni->ino)
  294. e = __lookup_nat_cache(nm_i, ni->ino);
  295. if (e) {
  296. if (fsync_done && ni->nid == ni->ino)
  297. set_nat_flag(e, HAS_FSYNCED_INODE, true);
  298. set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
  299. }
  300. up_write(&nm_i->nat_tree_lock);
  301. }
  302. int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
  303. {
  304. struct f2fs_nm_info *nm_i = NM_I(sbi);
  305. int nr = nr_shrink;
  306. if (!down_write_trylock(&nm_i->nat_tree_lock))
  307. return 0;
  308. while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
  309. struct nat_entry *ne;
  310. ne = list_first_entry(&nm_i->nat_entries,
  311. struct nat_entry, list);
  312. __del_from_nat_cache(nm_i, ne);
  313. nr_shrink--;
  314. }
  315. up_write(&nm_i->nat_tree_lock);
  316. return nr - nr_shrink;
  317. }
  318. /*
  319. * This function always returns success
  320. */
  321. void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
  322. {
  323. struct f2fs_nm_info *nm_i = NM_I(sbi);
  324. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  325. struct f2fs_journal *journal = curseg->journal;
  326. nid_t start_nid = START_NID(nid);
  327. struct f2fs_nat_block *nat_blk;
  328. struct page *page = NULL;
  329. struct f2fs_nat_entry ne;
  330. struct nat_entry *e;
  331. int i;
  332. ni->nid = nid;
  333. /* Check nat cache */
  334. down_read(&nm_i->nat_tree_lock);
  335. e = __lookup_nat_cache(nm_i, nid);
  336. if (e) {
  337. ni->ino = nat_get_ino(e);
  338. ni->blk_addr = nat_get_blkaddr(e);
  339. ni->version = nat_get_version(e);
  340. up_read(&nm_i->nat_tree_lock);
  341. return;
  342. }
  343. memset(&ne, 0, sizeof(struct f2fs_nat_entry));
  344. /* Check current segment summary */
  345. down_read(&curseg->journal_rwsem);
  346. i = lookup_journal_in_cursum(journal, NAT_JOURNAL, nid, 0);
  347. if (i >= 0) {
  348. ne = nat_in_journal(journal, i);
  349. node_info_from_raw_nat(ni, &ne);
  350. }
  351. up_read(&curseg->journal_rwsem);
  352. if (i >= 0)
  353. goto cache;
  354. /* Fill node_info from nat page */
  355. page = get_current_nat_page(sbi, start_nid);
  356. nat_blk = (struct f2fs_nat_block *)page_address(page);
  357. ne = nat_blk->entries[nid - start_nid];
  358. node_info_from_raw_nat(ni, &ne);
  359. f2fs_put_page(page, 1);
  360. cache:
  361. up_read(&nm_i->nat_tree_lock);
  362. /* cache nat entry */
  363. down_write(&nm_i->nat_tree_lock);
  364. cache_nat_entry(sbi, nid, &ne);
  365. up_write(&nm_i->nat_tree_lock);
  366. }
  367. /*
  368. * readahead MAX_RA_NODE number of node pages.
  369. */
  370. static void ra_node_pages(struct page *parent, int start, int n)
  371. {
  372. struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
  373. struct blk_plug plug;
  374. int i, end;
  375. nid_t nid;
  376. blk_start_plug(&plug);
  377. /* Then, try readahead for siblings of the desired node */
  378. end = start + n;
  379. end = min(end, NIDS_PER_BLOCK);
  380. for (i = start; i < end; i++) {
  381. nid = get_nid(parent, i, false);
  382. ra_node_page(sbi, nid);
  383. }
  384. blk_finish_plug(&plug);
  385. }
  386. pgoff_t get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs)
  387. {
  388. const long direct_index = ADDRS_PER_INODE(dn->inode);
  389. const long direct_blks = ADDRS_PER_BLOCK;
  390. const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
  391. unsigned int skipped_unit = ADDRS_PER_BLOCK;
  392. int cur_level = dn->cur_level;
  393. int max_level = dn->max_level;
  394. pgoff_t base = 0;
  395. if (!dn->max_level)
  396. return pgofs + 1;
  397. while (max_level-- > cur_level)
  398. skipped_unit *= NIDS_PER_BLOCK;
  399. switch (dn->max_level) {
  400. case 3:
  401. base += 2 * indirect_blks;
  402. case 2:
  403. base += 2 * direct_blks;
  404. case 1:
  405. base += direct_index;
  406. break;
  407. default:
  408. f2fs_bug_on(F2FS_I_SB(dn->inode), 1);
  409. }
  410. return ((pgofs - base) / skipped_unit + 1) * skipped_unit + base;
  411. }
  412. /*
  413. * The maximum depth is four.
  414. * Offset[0] will have raw inode offset.
  415. */
  416. static int get_node_path(struct inode *inode, long block,
  417. int offset[4], unsigned int noffset[4])
  418. {
  419. const long direct_index = ADDRS_PER_INODE(inode);
  420. const long direct_blks = ADDRS_PER_BLOCK;
  421. const long dptrs_per_blk = NIDS_PER_BLOCK;
  422. const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
  423. const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
  424. int n = 0;
  425. int level = 0;
  426. noffset[0] = 0;
  427. if (block < direct_index) {
  428. offset[n] = block;
  429. goto got;
  430. }
  431. block -= direct_index;
  432. if (block < direct_blks) {
  433. offset[n++] = NODE_DIR1_BLOCK;
  434. noffset[n] = 1;
  435. offset[n] = block;
  436. level = 1;
  437. goto got;
  438. }
  439. block -= direct_blks;
  440. if (block < direct_blks) {
  441. offset[n++] = NODE_DIR2_BLOCK;
  442. noffset[n] = 2;
  443. offset[n] = block;
  444. level = 1;
  445. goto got;
  446. }
  447. block -= direct_blks;
  448. if (block < indirect_blks) {
  449. offset[n++] = NODE_IND1_BLOCK;
  450. noffset[n] = 3;
  451. offset[n++] = block / direct_blks;
  452. noffset[n] = 4 + offset[n - 1];
  453. offset[n] = block % direct_blks;
  454. level = 2;
  455. goto got;
  456. }
  457. block -= indirect_blks;
  458. if (block < indirect_blks) {
  459. offset[n++] = NODE_IND2_BLOCK;
  460. noffset[n] = 4 + dptrs_per_blk;
  461. offset[n++] = block / direct_blks;
  462. noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
  463. offset[n] = block % direct_blks;
  464. level = 2;
  465. goto got;
  466. }
  467. block -= indirect_blks;
  468. if (block < dindirect_blks) {
  469. offset[n++] = NODE_DIND_BLOCK;
  470. noffset[n] = 5 + (dptrs_per_blk * 2);
  471. offset[n++] = block / indirect_blks;
  472. noffset[n] = 6 + (dptrs_per_blk * 2) +
  473. offset[n - 1] * (dptrs_per_blk + 1);
  474. offset[n++] = (block / direct_blks) % dptrs_per_blk;
  475. noffset[n] = 7 + (dptrs_per_blk * 2) +
  476. offset[n - 2] * (dptrs_per_blk + 1) +
  477. offset[n - 1];
  478. offset[n] = block % direct_blks;
  479. level = 3;
  480. goto got;
  481. } else {
  482. BUG();
  483. }
  484. got:
  485. return level;
  486. }
  487. /*
  488. * Caller should call f2fs_put_dnode(dn).
  489. * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
  490. * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
  491. * In the case of RDONLY_NODE, we don't need to care about mutex.
  492. */
  493. int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
  494. {
  495. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  496. struct page *npage[4];
  497. struct page *parent = NULL;
  498. int offset[4];
  499. unsigned int noffset[4];
  500. nid_t nids[4];
  501. int level, i = 0;
  502. int err = 0;
  503. level = get_node_path(dn->inode, index, offset, noffset);
  504. nids[0] = dn->inode->i_ino;
  505. npage[0] = dn->inode_page;
  506. if (!npage[0]) {
  507. npage[0] = get_node_page(sbi, nids[0]);
  508. if (IS_ERR(npage[0]))
  509. return PTR_ERR(npage[0]);
  510. }
  511. /* if inline_data is set, should not report any block indices */
  512. if (f2fs_has_inline_data(dn->inode) && index) {
  513. err = -ENOENT;
  514. f2fs_put_page(npage[0], 1);
  515. goto release_out;
  516. }
  517. parent = npage[0];
  518. if (level != 0)
  519. nids[1] = get_nid(parent, offset[0], true);
  520. dn->inode_page = npage[0];
  521. dn->inode_page_locked = true;
  522. /* get indirect or direct nodes */
  523. for (i = 1; i <= level; i++) {
  524. bool done = false;
  525. if (!nids[i] && mode == ALLOC_NODE) {
  526. /* alloc new node */
  527. if (!alloc_nid(sbi, &(nids[i]))) {
  528. err = -ENOSPC;
  529. goto release_pages;
  530. }
  531. dn->nid = nids[i];
  532. npage[i] = new_node_page(dn, noffset[i], NULL);
  533. if (IS_ERR(npage[i])) {
  534. alloc_nid_failed(sbi, nids[i]);
  535. err = PTR_ERR(npage[i]);
  536. goto release_pages;
  537. }
  538. set_nid(parent, offset[i - 1], nids[i], i == 1);
  539. alloc_nid_done(sbi, nids[i]);
  540. done = true;
  541. } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
  542. npage[i] = get_node_page_ra(parent, offset[i - 1]);
  543. if (IS_ERR(npage[i])) {
  544. err = PTR_ERR(npage[i]);
  545. goto release_pages;
  546. }
  547. done = true;
  548. }
  549. if (i == 1) {
  550. dn->inode_page_locked = false;
  551. unlock_page(parent);
  552. } else {
  553. f2fs_put_page(parent, 1);
  554. }
  555. if (!done) {
  556. npage[i] = get_node_page(sbi, nids[i]);
  557. if (IS_ERR(npage[i])) {
  558. err = PTR_ERR(npage[i]);
  559. f2fs_put_page(npage[0], 0);
  560. goto release_out;
  561. }
  562. }
  563. if (i < level) {
  564. parent = npage[i];
  565. nids[i + 1] = get_nid(parent, offset[i], false);
  566. }
  567. }
  568. dn->nid = nids[level];
  569. dn->ofs_in_node = offset[level];
  570. dn->node_page = npage[level];
  571. dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
  572. return 0;
  573. release_pages:
  574. f2fs_put_page(parent, 1);
  575. if (i > 1)
  576. f2fs_put_page(npage[0], 0);
  577. release_out:
  578. dn->inode_page = NULL;
  579. dn->node_page = NULL;
  580. if (err == -ENOENT) {
  581. dn->cur_level = i;
  582. dn->max_level = level;
  583. dn->ofs_in_node = offset[level];
  584. }
  585. return err;
  586. }
  587. static void truncate_node(struct dnode_of_data *dn)
  588. {
  589. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  590. struct node_info ni;
  591. get_node_info(sbi, dn->nid, &ni);
  592. if (dn->inode->i_blocks == 0) {
  593. f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR);
  594. goto invalidate;
  595. }
  596. f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
  597. /* Deallocate node address */
  598. invalidate_blocks(sbi, ni.blk_addr);
  599. dec_valid_node_count(sbi, dn->inode);
  600. set_node_addr(sbi, &ni, NULL_ADDR, false);
  601. if (dn->nid == dn->inode->i_ino) {
  602. remove_orphan_inode(sbi, dn->nid);
  603. dec_valid_inode_count(sbi);
  604. f2fs_inode_synced(dn->inode);
  605. }
  606. invalidate:
  607. clear_node_page_dirty(dn->node_page);
  608. set_sbi_flag(sbi, SBI_IS_DIRTY);
  609. f2fs_put_page(dn->node_page, 1);
  610. invalidate_mapping_pages(NODE_MAPPING(sbi),
  611. dn->node_page->index, dn->node_page->index);
  612. dn->node_page = NULL;
  613. trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
  614. }
  615. static int truncate_dnode(struct dnode_of_data *dn)
  616. {
  617. struct page *page;
  618. if (dn->nid == 0)
  619. return 1;
  620. /* get direct node */
  621. page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
  622. if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
  623. return 1;
  624. else if (IS_ERR(page))
  625. return PTR_ERR(page);
  626. /* Make dnode_of_data for parameter */
  627. dn->node_page = page;
  628. dn->ofs_in_node = 0;
  629. truncate_data_blocks(dn);
  630. truncate_node(dn);
  631. return 1;
  632. }
  633. static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
  634. int ofs, int depth)
  635. {
  636. struct dnode_of_data rdn = *dn;
  637. struct page *page;
  638. struct f2fs_node *rn;
  639. nid_t child_nid;
  640. unsigned int child_nofs;
  641. int freed = 0;
  642. int i, ret;
  643. if (dn->nid == 0)
  644. return NIDS_PER_BLOCK + 1;
  645. trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
  646. page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
  647. if (IS_ERR(page)) {
  648. trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
  649. return PTR_ERR(page);
  650. }
  651. ra_node_pages(page, ofs, NIDS_PER_BLOCK);
  652. rn = F2FS_NODE(page);
  653. if (depth < 3) {
  654. for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
  655. child_nid = le32_to_cpu(rn->in.nid[i]);
  656. if (child_nid == 0)
  657. continue;
  658. rdn.nid = child_nid;
  659. ret = truncate_dnode(&rdn);
  660. if (ret < 0)
  661. goto out_err;
  662. if (set_nid(page, i, 0, false))
  663. dn->node_changed = true;
  664. }
  665. } else {
  666. child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
  667. for (i = ofs; i < NIDS_PER_BLOCK; i++) {
  668. child_nid = le32_to_cpu(rn->in.nid[i]);
  669. if (child_nid == 0) {
  670. child_nofs += NIDS_PER_BLOCK + 1;
  671. continue;
  672. }
  673. rdn.nid = child_nid;
  674. ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
  675. if (ret == (NIDS_PER_BLOCK + 1)) {
  676. if (set_nid(page, i, 0, false))
  677. dn->node_changed = true;
  678. child_nofs += ret;
  679. } else if (ret < 0 && ret != -ENOENT) {
  680. goto out_err;
  681. }
  682. }
  683. freed = child_nofs;
  684. }
  685. if (!ofs) {
  686. /* remove current indirect node */
  687. dn->node_page = page;
  688. truncate_node(dn);
  689. freed++;
  690. } else {
  691. f2fs_put_page(page, 1);
  692. }
  693. trace_f2fs_truncate_nodes_exit(dn->inode, freed);
  694. return freed;
  695. out_err:
  696. f2fs_put_page(page, 1);
  697. trace_f2fs_truncate_nodes_exit(dn->inode, ret);
  698. return ret;
  699. }
  700. static int truncate_partial_nodes(struct dnode_of_data *dn,
  701. struct f2fs_inode *ri, int *offset, int depth)
  702. {
  703. struct page *pages[2];
  704. nid_t nid[3];
  705. nid_t child_nid;
  706. int err = 0;
  707. int i;
  708. int idx = depth - 2;
  709. nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
  710. if (!nid[0])
  711. return 0;
  712. /* get indirect nodes in the path */
  713. for (i = 0; i < idx + 1; i++) {
  714. /* reference count'll be increased */
  715. pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]);
  716. if (IS_ERR(pages[i])) {
  717. err = PTR_ERR(pages[i]);
  718. idx = i - 1;
  719. goto fail;
  720. }
  721. nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
  722. }
  723. ra_node_pages(pages[idx], offset[idx + 1], NIDS_PER_BLOCK);
  724. /* free direct nodes linked to a partial indirect node */
  725. for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
  726. child_nid = get_nid(pages[idx], i, false);
  727. if (!child_nid)
  728. continue;
  729. dn->nid = child_nid;
  730. err = truncate_dnode(dn);
  731. if (err < 0)
  732. goto fail;
  733. if (set_nid(pages[idx], i, 0, false))
  734. dn->node_changed = true;
  735. }
  736. if (offset[idx + 1] == 0) {
  737. dn->node_page = pages[idx];
  738. dn->nid = nid[idx];
  739. truncate_node(dn);
  740. } else {
  741. f2fs_put_page(pages[idx], 1);
  742. }
  743. offset[idx]++;
  744. offset[idx + 1] = 0;
  745. idx--;
  746. fail:
  747. for (i = idx; i >= 0; i--)
  748. f2fs_put_page(pages[i], 1);
  749. trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
  750. return err;
  751. }
  752. /*
  753. * All the block addresses of data and nodes should be nullified.
  754. */
  755. int truncate_inode_blocks(struct inode *inode, pgoff_t from)
  756. {
  757. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  758. int err = 0, cont = 1;
  759. int level, offset[4], noffset[4];
  760. unsigned int nofs = 0;
  761. struct f2fs_inode *ri;
  762. struct dnode_of_data dn;
  763. struct page *page;
  764. trace_f2fs_truncate_inode_blocks_enter(inode, from);
  765. level = get_node_path(inode, from, offset, noffset);
  766. page = get_node_page(sbi, inode->i_ino);
  767. if (IS_ERR(page)) {
  768. trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
  769. return PTR_ERR(page);
  770. }
  771. set_new_dnode(&dn, inode, page, NULL, 0);
  772. unlock_page(page);
  773. ri = F2FS_INODE(page);
  774. switch (level) {
  775. case 0:
  776. case 1:
  777. nofs = noffset[1];
  778. break;
  779. case 2:
  780. nofs = noffset[1];
  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. nofs += 1 + NIDS_PER_BLOCK;
  787. break;
  788. case 3:
  789. nofs = 5 + 2 * NIDS_PER_BLOCK;
  790. if (!offset[level - 1])
  791. goto skip_partial;
  792. err = truncate_partial_nodes(&dn, ri, offset, level);
  793. if (err < 0 && err != -ENOENT)
  794. goto fail;
  795. break;
  796. default:
  797. BUG();
  798. }
  799. skip_partial:
  800. while (cont) {
  801. dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
  802. switch (offset[0]) {
  803. case NODE_DIR1_BLOCK:
  804. case NODE_DIR2_BLOCK:
  805. err = truncate_dnode(&dn);
  806. break;
  807. case NODE_IND1_BLOCK:
  808. case NODE_IND2_BLOCK:
  809. err = truncate_nodes(&dn, nofs, offset[1], 2);
  810. break;
  811. case NODE_DIND_BLOCK:
  812. err = truncate_nodes(&dn, nofs, offset[1], 3);
  813. cont = 0;
  814. break;
  815. default:
  816. BUG();
  817. }
  818. if (err < 0 && err != -ENOENT)
  819. goto fail;
  820. if (offset[1] == 0 &&
  821. ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
  822. lock_page(page);
  823. BUG_ON(page->mapping != NODE_MAPPING(sbi));
  824. f2fs_wait_on_page_writeback(page, NODE, true);
  825. ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
  826. set_page_dirty(page);
  827. unlock_page(page);
  828. }
  829. offset[1] = 0;
  830. offset[0]++;
  831. nofs += err;
  832. }
  833. fail:
  834. f2fs_put_page(page, 0);
  835. trace_f2fs_truncate_inode_blocks_exit(inode, err);
  836. return err > 0 ? 0 : err;
  837. }
  838. int truncate_xattr_node(struct inode *inode, struct page *page)
  839. {
  840. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  841. nid_t nid = F2FS_I(inode)->i_xattr_nid;
  842. struct dnode_of_data dn;
  843. struct page *npage;
  844. if (!nid)
  845. return 0;
  846. npage = get_node_page(sbi, nid);
  847. if (IS_ERR(npage))
  848. return PTR_ERR(npage);
  849. f2fs_i_xnid_write(inode, 0);
  850. /* need to do checkpoint during fsync */
  851. F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
  852. set_new_dnode(&dn, inode, page, npage, nid);
  853. if (page)
  854. dn.inode_page_locked = true;
  855. truncate_node(&dn);
  856. return 0;
  857. }
  858. /*
  859. * Caller should grab and release a rwsem by calling f2fs_lock_op() and
  860. * f2fs_unlock_op().
  861. */
  862. int remove_inode_page(struct inode *inode)
  863. {
  864. struct dnode_of_data dn;
  865. int err;
  866. set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
  867. err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
  868. if (err)
  869. return err;
  870. err = truncate_xattr_node(inode, dn.inode_page);
  871. if (err) {
  872. f2fs_put_dnode(&dn);
  873. return err;
  874. }
  875. /* remove potential inline_data blocks */
  876. if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  877. S_ISLNK(inode->i_mode))
  878. truncate_data_blocks_range(&dn, 1);
  879. /* 0 is possible, after f2fs_new_inode() has failed */
  880. f2fs_bug_on(F2FS_I_SB(inode),
  881. inode->i_blocks != 0 && inode->i_blocks != 1);
  882. /* will put inode & node pages */
  883. truncate_node(&dn);
  884. return 0;
  885. }
  886. struct page *new_inode_page(struct inode *inode)
  887. {
  888. struct dnode_of_data dn;
  889. /* allocate inode page for new inode */
  890. set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
  891. /* caller should f2fs_put_page(page, 1); */
  892. return new_node_page(&dn, 0, NULL);
  893. }
  894. struct page *new_node_page(struct dnode_of_data *dn,
  895. unsigned int ofs, struct page *ipage)
  896. {
  897. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  898. struct node_info old_ni, new_ni;
  899. struct page *page;
  900. int err;
  901. if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
  902. return ERR_PTR(-EPERM);
  903. page = f2fs_grab_cache_page(NODE_MAPPING(sbi), dn->nid, false);
  904. if (!page)
  905. return ERR_PTR(-ENOMEM);
  906. if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
  907. err = -ENOSPC;
  908. goto fail;
  909. }
  910. get_node_info(sbi, dn->nid, &old_ni);
  911. /* Reinitialize old_ni with new node page */
  912. f2fs_bug_on(sbi, old_ni.blk_addr != NULL_ADDR);
  913. new_ni = old_ni;
  914. new_ni.ino = dn->inode->i_ino;
  915. set_node_addr(sbi, &new_ni, NEW_ADDR, false);
  916. f2fs_wait_on_page_writeback(page, NODE, true);
  917. fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
  918. set_cold_node(dn->inode, page);
  919. if (!PageUptodate(page))
  920. SetPageUptodate(page);
  921. if (set_page_dirty(page))
  922. dn->node_changed = true;
  923. if (f2fs_has_xattr_block(ofs))
  924. f2fs_i_xnid_write(dn->inode, dn->nid);
  925. if (ofs == 0)
  926. inc_valid_inode_count(sbi);
  927. return page;
  928. fail:
  929. clear_node_page_dirty(page);
  930. f2fs_put_page(page, 1);
  931. return ERR_PTR(err);
  932. }
  933. /*
  934. * Caller should do after getting the following values.
  935. * 0: f2fs_put_page(page, 0)
  936. * LOCKED_PAGE or error: f2fs_put_page(page, 1)
  937. */
  938. static int read_node_page(struct page *page, int rw)
  939. {
  940. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  941. struct node_info ni;
  942. struct f2fs_io_info fio = {
  943. .sbi = sbi,
  944. .type = NODE,
  945. .rw = rw,
  946. .page = page,
  947. .encrypted_page = NULL,
  948. };
  949. if (PageUptodate(page))
  950. return LOCKED_PAGE;
  951. get_node_info(sbi, page->index, &ni);
  952. if (unlikely(ni.blk_addr == NULL_ADDR)) {
  953. ClearPageUptodate(page);
  954. return -ENOENT;
  955. }
  956. fio.new_blkaddr = fio.old_blkaddr = ni.blk_addr;
  957. return f2fs_submit_page_bio(&fio);
  958. }
  959. /*
  960. * Readahead a node page
  961. */
  962. void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
  963. {
  964. struct page *apage;
  965. int err;
  966. if (!nid)
  967. return;
  968. f2fs_bug_on(sbi, check_nid_range(sbi, nid));
  969. rcu_read_lock();
  970. apage = radix_tree_lookup(&NODE_MAPPING(sbi)->page_tree, nid);
  971. rcu_read_unlock();
  972. if (apage)
  973. return;
  974. apage = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
  975. if (!apage)
  976. return;
  977. err = read_node_page(apage, READA);
  978. f2fs_put_page(apage, err ? 1 : 0);
  979. }
  980. static struct page *__get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid,
  981. struct page *parent, int start)
  982. {
  983. struct page *page;
  984. int err;
  985. if (!nid)
  986. return ERR_PTR(-ENOENT);
  987. f2fs_bug_on(sbi, check_nid_range(sbi, nid));
  988. repeat:
  989. page = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
  990. if (!page)
  991. return ERR_PTR(-ENOMEM);
  992. err = read_node_page(page, READ_SYNC);
  993. if (err < 0) {
  994. f2fs_put_page(page, 1);
  995. return ERR_PTR(err);
  996. } else if (err == LOCKED_PAGE) {
  997. goto page_hit;
  998. }
  999. if (parent)
  1000. ra_node_pages(parent, start + 1, MAX_RA_NODE);
  1001. lock_page(page);
  1002. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1003. f2fs_put_page(page, 1);
  1004. goto repeat;
  1005. }
  1006. if (unlikely(!PageUptodate(page)))
  1007. goto out_err;
  1008. page_hit:
  1009. mark_page_accessed(page);
  1010. if(unlikely(nid != nid_of_node(page))) {
  1011. f2fs_bug_on(sbi, 1);
  1012. ClearPageUptodate(page);
  1013. out_err:
  1014. f2fs_put_page(page, 1);
  1015. return ERR_PTR(-EIO);
  1016. }
  1017. return page;
  1018. }
  1019. struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
  1020. {
  1021. return __get_node_page(sbi, nid, NULL, 0);
  1022. }
  1023. struct page *get_node_page_ra(struct page *parent, int start)
  1024. {
  1025. struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
  1026. nid_t nid = get_nid(parent, start, false);
  1027. return __get_node_page(sbi, nid, parent, start);
  1028. }
  1029. static void flush_inline_data(struct f2fs_sb_info *sbi, nid_t ino)
  1030. {
  1031. struct inode *inode;
  1032. struct page *page;
  1033. int ret;
  1034. /* should flush inline_data before evict_inode */
  1035. inode = ilookup(sbi->sb, ino);
  1036. if (!inode)
  1037. return;
  1038. page = find_get_page(inode->i_mapping, 0);
  1039. if (!page)
  1040. goto iput_out;
  1041. if (!trylock_page(page))
  1042. goto release_out;
  1043. if (!PageUptodate(page))
  1044. goto page_out;
  1045. if (!PageDirty(page))
  1046. goto page_out;
  1047. if (!clear_page_dirty_for_io(page))
  1048. goto page_out;
  1049. ret = f2fs_write_inline_data(inode, page);
  1050. inode_dec_dirty_pages(inode);
  1051. remove_dirty_inode(inode);
  1052. if (ret)
  1053. set_page_dirty(page);
  1054. page_out:
  1055. unlock_page(page);
  1056. release_out:
  1057. f2fs_put_page(page, 0);
  1058. iput_out:
  1059. iput(inode);
  1060. }
  1061. void move_node_page(struct page *node_page, int gc_type)
  1062. {
  1063. if (gc_type == FG_GC) {
  1064. struct f2fs_sb_info *sbi = F2FS_P_SB(node_page);
  1065. struct writeback_control wbc = {
  1066. .sync_mode = WB_SYNC_ALL,
  1067. .nr_to_write = 1,
  1068. .for_reclaim = 0,
  1069. };
  1070. set_page_dirty(node_page);
  1071. f2fs_wait_on_page_writeback(node_page, NODE, true);
  1072. f2fs_bug_on(sbi, PageWriteback(node_page));
  1073. if (!clear_page_dirty_for_io(node_page))
  1074. goto out_page;
  1075. if (NODE_MAPPING(sbi)->a_ops->writepage(node_page, &wbc))
  1076. unlock_page(node_page);
  1077. goto release_page;
  1078. } else {
  1079. /* set page dirty and write it */
  1080. if (!PageWriteback(node_page))
  1081. set_page_dirty(node_page);
  1082. }
  1083. out_page:
  1084. unlock_page(node_page);
  1085. release_page:
  1086. f2fs_put_page(node_page, 0);
  1087. }
  1088. static struct page *last_fsync_dnode(struct f2fs_sb_info *sbi, nid_t ino)
  1089. {
  1090. pgoff_t index, end;
  1091. struct pagevec pvec;
  1092. struct page *last_page = NULL;
  1093. pagevec_init(&pvec, 0);
  1094. index = 0;
  1095. end = ULONG_MAX;
  1096. while (index <= end) {
  1097. int i, nr_pages;
  1098. nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  1099. PAGECACHE_TAG_DIRTY,
  1100. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  1101. if (nr_pages == 0)
  1102. break;
  1103. for (i = 0; i < nr_pages; i++) {
  1104. struct page *page = pvec.pages[i];
  1105. if (unlikely(f2fs_cp_error(sbi))) {
  1106. f2fs_put_page(last_page, 0);
  1107. pagevec_release(&pvec);
  1108. return ERR_PTR(-EIO);
  1109. }
  1110. if (!IS_DNODE(page) || !is_cold_node(page))
  1111. continue;
  1112. if (ino_of_node(page) != ino)
  1113. continue;
  1114. lock_page(page);
  1115. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1116. continue_unlock:
  1117. unlock_page(page);
  1118. continue;
  1119. }
  1120. if (ino_of_node(page) != ino)
  1121. goto continue_unlock;
  1122. if (!PageDirty(page)) {
  1123. /* someone wrote it for us */
  1124. goto continue_unlock;
  1125. }
  1126. if (last_page)
  1127. f2fs_put_page(last_page, 0);
  1128. get_page(page);
  1129. last_page = page;
  1130. unlock_page(page);
  1131. }
  1132. pagevec_release(&pvec);
  1133. cond_resched();
  1134. }
  1135. return last_page;
  1136. }
  1137. int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
  1138. struct writeback_control *wbc, bool atomic)
  1139. {
  1140. pgoff_t index, end;
  1141. struct pagevec pvec;
  1142. int ret = 0;
  1143. struct page *last_page = NULL;
  1144. bool marked = false;
  1145. nid_t ino = inode->i_ino;
  1146. int nwritten = 0;
  1147. if (atomic) {
  1148. last_page = last_fsync_dnode(sbi, ino);
  1149. if (IS_ERR_OR_NULL(last_page))
  1150. return PTR_ERR_OR_ZERO(last_page);
  1151. }
  1152. retry:
  1153. pagevec_init(&pvec, 0);
  1154. index = 0;
  1155. end = ULONG_MAX;
  1156. while (index <= end) {
  1157. int i, nr_pages;
  1158. nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  1159. PAGECACHE_TAG_DIRTY,
  1160. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  1161. if (nr_pages == 0)
  1162. break;
  1163. for (i = 0; i < nr_pages; i++) {
  1164. struct page *page = pvec.pages[i];
  1165. if (unlikely(f2fs_cp_error(sbi))) {
  1166. f2fs_put_page(last_page, 0);
  1167. pagevec_release(&pvec);
  1168. ret = -EIO;
  1169. goto out;
  1170. }
  1171. if (!IS_DNODE(page) || !is_cold_node(page))
  1172. continue;
  1173. if (ino_of_node(page) != ino)
  1174. continue;
  1175. lock_page(page);
  1176. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1177. continue_unlock:
  1178. unlock_page(page);
  1179. continue;
  1180. }
  1181. if (ino_of_node(page) != ino)
  1182. goto continue_unlock;
  1183. if (!PageDirty(page) && page != last_page) {
  1184. /* someone wrote it for us */
  1185. goto continue_unlock;
  1186. }
  1187. f2fs_wait_on_page_writeback(page, NODE, true);
  1188. BUG_ON(PageWriteback(page));
  1189. if (!atomic || page == last_page) {
  1190. set_fsync_mark(page, 1);
  1191. if (IS_INODE(page)) {
  1192. if (is_inode_flag_set(inode,
  1193. FI_DIRTY_INODE))
  1194. update_inode(inode, page);
  1195. set_dentry_mark(page,
  1196. need_dentry_mark(sbi, ino));
  1197. }
  1198. /* may be written by other thread */
  1199. if (!PageDirty(page))
  1200. set_page_dirty(page);
  1201. }
  1202. if (!clear_page_dirty_for_io(page))
  1203. goto continue_unlock;
  1204. ret = NODE_MAPPING(sbi)->a_ops->writepage(page, wbc);
  1205. if (ret) {
  1206. unlock_page(page);
  1207. f2fs_put_page(last_page, 0);
  1208. break;
  1209. } else {
  1210. nwritten++;
  1211. }
  1212. if (page == last_page) {
  1213. f2fs_put_page(page, 0);
  1214. marked = true;
  1215. break;
  1216. }
  1217. }
  1218. pagevec_release(&pvec);
  1219. cond_resched();
  1220. if (ret || marked)
  1221. break;
  1222. }
  1223. if (!ret && atomic && !marked) {
  1224. f2fs_msg(sbi->sb, KERN_DEBUG,
  1225. "Retry to write fsync mark: ino=%u, idx=%lx",
  1226. ino, last_page->index);
  1227. lock_page(last_page);
  1228. f2fs_wait_on_page_writeback(last_page, NODE, true);
  1229. set_page_dirty(last_page);
  1230. unlock_page(last_page);
  1231. goto retry;
  1232. }
  1233. out:
  1234. if (nwritten)
  1235. f2fs_submit_merged_bio_cond(sbi, NULL, NULL, ino, NODE, WRITE);
  1236. return ret ? -EIO: 0;
  1237. }
  1238. int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc)
  1239. {
  1240. pgoff_t index, end;
  1241. struct pagevec pvec;
  1242. int step = 0;
  1243. int nwritten = 0;
  1244. int ret = 0;
  1245. pagevec_init(&pvec, 0);
  1246. next_step:
  1247. index = 0;
  1248. end = ULONG_MAX;
  1249. while (index <= end) {
  1250. int i, nr_pages;
  1251. nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  1252. PAGECACHE_TAG_DIRTY,
  1253. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  1254. if (nr_pages == 0)
  1255. break;
  1256. for (i = 0; i < nr_pages; i++) {
  1257. struct page *page = pvec.pages[i];
  1258. if (unlikely(f2fs_cp_error(sbi))) {
  1259. pagevec_release(&pvec);
  1260. ret = -EIO;
  1261. goto out;
  1262. }
  1263. /*
  1264. * flushing sequence with step:
  1265. * 0. indirect nodes
  1266. * 1. dentry dnodes
  1267. * 2. file dnodes
  1268. */
  1269. if (step == 0 && IS_DNODE(page))
  1270. continue;
  1271. if (step == 1 && (!IS_DNODE(page) ||
  1272. is_cold_node(page)))
  1273. continue;
  1274. if (step == 2 && (!IS_DNODE(page) ||
  1275. !is_cold_node(page)))
  1276. continue;
  1277. lock_node:
  1278. if (!trylock_page(page))
  1279. continue;
  1280. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1281. continue_unlock:
  1282. unlock_page(page);
  1283. continue;
  1284. }
  1285. if (!PageDirty(page)) {
  1286. /* someone wrote it for us */
  1287. goto continue_unlock;
  1288. }
  1289. /* flush inline_data */
  1290. if (is_inline_node(page)) {
  1291. clear_inline_node(page);
  1292. unlock_page(page);
  1293. flush_inline_data(sbi, ino_of_node(page));
  1294. goto lock_node;
  1295. }
  1296. f2fs_wait_on_page_writeback(page, NODE, true);
  1297. BUG_ON(PageWriteback(page));
  1298. if (!clear_page_dirty_for_io(page))
  1299. goto continue_unlock;
  1300. set_fsync_mark(page, 0);
  1301. set_dentry_mark(page, 0);
  1302. if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
  1303. unlock_page(page);
  1304. else
  1305. nwritten++;
  1306. if (--wbc->nr_to_write == 0)
  1307. break;
  1308. }
  1309. pagevec_release(&pvec);
  1310. cond_resched();
  1311. if (wbc->nr_to_write == 0) {
  1312. step = 2;
  1313. break;
  1314. }
  1315. }
  1316. if (step < 2) {
  1317. step++;
  1318. goto next_step;
  1319. }
  1320. out:
  1321. if (nwritten)
  1322. f2fs_submit_merged_bio(sbi, NODE, WRITE);
  1323. return ret;
  1324. }
  1325. int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
  1326. {
  1327. pgoff_t index = 0, end = ULONG_MAX;
  1328. struct pagevec pvec;
  1329. int ret2 = 0, ret = 0;
  1330. pagevec_init(&pvec, 0);
  1331. while (index <= end) {
  1332. int i, nr_pages;
  1333. nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  1334. PAGECACHE_TAG_WRITEBACK,
  1335. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  1336. if (nr_pages == 0)
  1337. break;
  1338. for (i = 0; i < nr_pages; i++) {
  1339. struct page *page = pvec.pages[i];
  1340. /* until radix tree lookup accepts end_index */
  1341. if (unlikely(page->index > end))
  1342. continue;
  1343. if (ino && ino_of_node(page) == ino) {
  1344. f2fs_wait_on_page_writeback(page, NODE, true);
  1345. if (TestClearPageError(page))
  1346. ret = -EIO;
  1347. }
  1348. }
  1349. pagevec_release(&pvec);
  1350. cond_resched();
  1351. }
  1352. if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
  1353. ret2 = -ENOSPC;
  1354. if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
  1355. ret2 = -EIO;
  1356. if (!ret)
  1357. ret = ret2;
  1358. return ret;
  1359. }
  1360. static int f2fs_write_node_page(struct page *page,
  1361. struct writeback_control *wbc)
  1362. {
  1363. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  1364. nid_t nid;
  1365. struct node_info ni;
  1366. struct f2fs_io_info fio = {
  1367. .sbi = sbi,
  1368. .type = NODE,
  1369. .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
  1370. .page = page,
  1371. .encrypted_page = NULL,
  1372. };
  1373. trace_f2fs_writepage(page, NODE);
  1374. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  1375. goto redirty_out;
  1376. if (unlikely(f2fs_cp_error(sbi)))
  1377. goto redirty_out;
  1378. /* get old block addr of this node page */
  1379. nid = nid_of_node(page);
  1380. f2fs_bug_on(sbi, page->index != nid);
  1381. if (wbc->for_reclaim) {
  1382. if (!down_read_trylock(&sbi->node_write))
  1383. goto redirty_out;
  1384. } else {
  1385. down_read(&sbi->node_write);
  1386. }
  1387. get_node_info(sbi, nid, &ni);
  1388. /* This page is already truncated */
  1389. if (unlikely(ni.blk_addr == NULL_ADDR)) {
  1390. ClearPageUptodate(page);
  1391. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1392. up_read(&sbi->node_write);
  1393. unlock_page(page);
  1394. return 0;
  1395. }
  1396. set_page_writeback(page);
  1397. fio.old_blkaddr = ni.blk_addr;
  1398. write_node_page(nid, &fio);
  1399. set_node_addr(sbi, &ni, fio.new_blkaddr, is_fsync_dnode(page));
  1400. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1401. up_read(&sbi->node_write);
  1402. if (wbc->for_reclaim)
  1403. f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, NODE, WRITE);
  1404. unlock_page(page);
  1405. if (unlikely(f2fs_cp_error(sbi)))
  1406. f2fs_submit_merged_bio(sbi, NODE, WRITE);
  1407. return 0;
  1408. redirty_out:
  1409. redirty_page_for_writepage(wbc, page);
  1410. return AOP_WRITEPAGE_ACTIVATE;
  1411. }
  1412. static int f2fs_write_node_pages(struct address_space *mapping,
  1413. struct writeback_control *wbc)
  1414. {
  1415. struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
  1416. struct blk_plug plug;
  1417. long diff;
  1418. /* balancing f2fs's metadata in background */
  1419. f2fs_balance_fs_bg(sbi);
  1420. /* collect a number of dirty node pages and write together */
  1421. if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
  1422. goto skip_write;
  1423. trace_f2fs_writepages(mapping->host, wbc, NODE);
  1424. diff = nr_pages_to_write(sbi, NODE, wbc);
  1425. wbc->sync_mode = WB_SYNC_NONE;
  1426. blk_start_plug(&plug);
  1427. sync_node_pages(sbi, wbc);
  1428. blk_finish_plug(&plug);
  1429. wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
  1430. return 0;
  1431. skip_write:
  1432. wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
  1433. trace_f2fs_writepages(mapping->host, wbc, NODE);
  1434. return 0;
  1435. }
  1436. static int f2fs_set_node_page_dirty(struct page *page)
  1437. {
  1438. trace_f2fs_set_page_dirty(page, NODE);
  1439. if (!PageUptodate(page))
  1440. SetPageUptodate(page);
  1441. if (!PageDirty(page)) {
  1442. f2fs_set_page_dirty_nobuffers(page);
  1443. inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
  1444. SetPagePrivate(page);
  1445. f2fs_trace_pid(page);
  1446. return 1;
  1447. }
  1448. return 0;
  1449. }
  1450. /*
  1451. * Structure of the f2fs node operations
  1452. */
  1453. const struct address_space_operations f2fs_node_aops = {
  1454. .writepage = f2fs_write_node_page,
  1455. .writepages = f2fs_write_node_pages,
  1456. .set_page_dirty = f2fs_set_node_page_dirty,
  1457. .invalidatepage = f2fs_invalidate_page,
  1458. .releasepage = f2fs_release_page,
  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);
  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. }