node.c 67 KB

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