node.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/f2fs/node.c
  4. *
  5. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  6. * http://www.samsung.com/
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/f2fs_fs.h>
  10. #include <linux/mpage.h>
  11. #include <linux/sched/mm.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/pagevec.h>
  14. #include <linux/swap.h>
  15. #include "f2fs.h"
  16. #include "node.h"
  17. #include "segment.h"
  18. #include "xattr.h"
  19. #include "iostat.h"
  20. #include <trace/events/f2fs.h>
  21. #define on_f2fs_build_free_nids(nmi) mutex_is_locked(&(nm_i)->build_lock)
  22. static struct kmem_cache *nat_entry_slab;
  23. static struct kmem_cache *free_nid_slab;
  24. static struct kmem_cache *nat_entry_set_slab;
  25. static struct kmem_cache *fsync_node_entry_slab;
  26. /*
  27. * Check whether the given nid is within node id range.
  28. */
  29. int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
  30. {
  31. if (unlikely(nid < F2FS_ROOT_INO(sbi) || nid >= NM_I(sbi)->max_nid)) {
  32. set_sbi_flag(sbi, SBI_NEED_FSCK);
  33. f2fs_warn(sbi, "%s: out-of-range nid=%x, run fsck to fix.",
  34. __func__, nid);
  35. f2fs_handle_error(sbi, ERROR_CORRUPTED_INODE);
  36. return -EFSCORRUPTED;
  37. }
  38. return 0;
  39. }
  40. bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)
  41. {
  42. struct f2fs_nm_info *nm_i = NM_I(sbi);
  43. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  44. struct sysinfo val;
  45. unsigned long avail_ram;
  46. unsigned long mem_size = 0;
  47. bool res = false;
  48. if (!nm_i)
  49. return true;
  50. si_meminfo(&val);
  51. /* only uses low memory */
  52. avail_ram = val.totalram - val.totalhigh;
  53. /*
  54. * give 25%, 25%, 50%, 50%, 25%, 25% memory for each components respectively
  55. */
  56. if (type == FREE_NIDS) {
  57. mem_size = (nm_i->nid_cnt[FREE_NID] *
  58. sizeof(struct free_nid)) >> PAGE_SHIFT;
  59. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
  60. } else if (type == NAT_ENTRIES) {
  61. mem_size = (nm_i->nat_cnt[TOTAL_NAT] *
  62. sizeof(struct nat_entry)) >> PAGE_SHIFT;
  63. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
  64. if (excess_cached_nats(sbi))
  65. res = false;
  66. } else if (type == DIRTY_DENTS) {
  67. if (sbi->sb->s_bdi->wb.dirty_exceeded)
  68. return false;
  69. mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
  70. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
  71. } else if (type == INO_ENTRIES) {
  72. int i;
  73. for (i = 0; i < MAX_INO_ENTRY; i++)
  74. mem_size += sbi->im[i].ino_num *
  75. sizeof(struct ino_entry);
  76. mem_size >>= PAGE_SHIFT;
  77. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
  78. } else if (type == READ_EXTENT_CACHE || type == AGE_EXTENT_CACHE) {
  79. enum extent_type etype = type == READ_EXTENT_CACHE ?
  80. EX_READ : EX_BLOCK_AGE;
  81. struct extent_tree_info *eti = &sbi->extent_tree[etype];
  82. mem_size = (atomic_read(&eti->total_ext_tree) *
  83. sizeof(struct extent_tree) +
  84. atomic_read(&eti->total_ext_node) *
  85. sizeof(struct extent_node)) >> PAGE_SHIFT;
  86. res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
  87. } else if (type == DISCARD_CACHE) {
  88. mem_size = (atomic_read(&dcc->discard_cmd_cnt) *
  89. sizeof(struct discard_cmd)) >> PAGE_SHIFT;
  90. res = mem_size < (avail_ram * nm_i->ram_thresh / 100);
  91. } else if (type == COMPRESS_PAGE) {
  92. #ifdef CONFIG_F2FS_FS_COMPRESSION
  93. unsigned long free_ram = val.freeram;
  94. /*
  95. * free memory is lower than watermark or cached page count
  96. * exceed threshold, deny caching compress page.
  97. */
  98. res = (free_ram > avail_ram * sbi->compress_watermark / 100) &&
  99. (COMPRESS_MAPPING(sbi)->nrpages <
  100. free_ram * sbi->compress_percent / 100);
  101. #else
  102. res = false;
  103. #endif
  104. } else {
  105. if (!sbi->sb->s_bdi->wb.dirty_exceeded)
  106. return true;
  107. }
  108. return res;
  109. }
  110. static void clear_node_page_dirty(struct page *page)
  111. {
  112. if (PageDirty(page)) {
  113. f2fs_clear_page_cache_dirty_tag(page);
  114. clear_page_dirty_for_io(page);
  115. dec_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
  116. }
  117. ClearPageUptodate(page);
  118. }
  119. static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
  120. {
  121. return f2fs_get_meta_page_retry(sbi, current_nat_addr(sbi, nid));
  122. }
  123. static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
  124. {
  125. struct page *src_page;
  126. struct page *dst_page;
  127. pgoff_t dst_off;
  128. void *src_addr;
  129. void *dst_addr;
  130. struct f2fs_nm_info *nm_i = NM_I(sbi);
  131. dst_off = next_nat_addr(sbi, current_nat_addr(sbi, nid));
  132. /* get current nat block page with lock */
  133. src_page = get_current_nat_page(sbi, nid);
  134. if (IS_ERR(src_page))
  135. return src_page;
  136. dst_page = f2fs_grab_meta_page(sbi, dst_off);
  137. f2fs_bug_on(sbi, PageDirty(src_page));
  138. src_addr = page_address(src_page);
  139. dst_addr = page_address(dst_page);
  140. memcpy(dst_addr, src_addr, PAGE_SIZE);
  141. set_page_dirty(dst_page);
  142. f2fs_put_page(src_page, 1);
  143. set_to_next_nat(nm_i, nid);
  144. return dst_page;
  145. }
  146. static struct nat_entry *__alloc_nat_entry(struct f2fs_sb_info *sbi,
  147. nid_t nid, bool no_fail)
  148. {
  149. struct nat_entry *new;
  150. new = f2fs_kmem_cache_alloc(nat_entry_slab,
  151. GFP_F2FS_ZERO, no_fail, sbi);
  152. if (new) {
  153. nat_set_nid(new, nid);
  154. nat_reset_flag(new);
  155. }
  156. return new;
  157. }
  158. static void __free_nat_entry(struct nat_entry *e)
  159. {
  160. kmem_cache_free(nat_entry_slab, e);
  161. }
  162. /* must be locked by nat_tree_lock */
  163. static struct nat_entry *__init_nat_entry(struct f2fs_nm_info *nm_i,
  164. struct nat_entry *ne, struct f2fs_nat_entry *raw_ne, bool no_fail)
  165. {
  166. if (no_fail)
  167. f2fs_radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne);
  168. else if (radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne))
  169. return NULL;
  170. if (raw_ne)
  171. node_info_from_raw_nat(&ne->ni, raw_ne);
  172. spin_lock(&nm_i->nat_list_lock);
  173. list_add_tail(&ne->list, &nm_i->nat_entries);
  174. spin_unlock(&nm_i->nat_list_lock);
  175. nm_i->nat_cnt[TOTAL_NAT]++;
  176. nm_i->nat_cnt[RECLAIMABLE_NAT]++;
  177. return ne;
  178. }
  179. static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
  180. {
  181. struct nat_entry *ne;
  182. ne = radix_tree_lookup(&nm_i->nat_root, n);
  183. /* for recent accessed nat entry, move it to tail of lru list */
  184. if (ne && !get_nat_flag(ne, IS_DIRTY)) {
  185. spin_lock(&nm_i->nat_list_lock);
  186. if (!list_empty(&ne->list))
  187. list_move_tail(&ne->list, &nm_i->nat_entries);
  188. spin_unlock(&nm_i->nat_list_lock);
  189. }
  190. return ne;
  191. }
  192. static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
  193. nid_t start, unsigned int nr, struct nat_entry **ep)
  194. {
  195. return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
  196. }
  197. static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
  198. {
  199. radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
  200. nm_i->nat_cnt[TOTAL_NAT]--;
  201. nm_i->nat_cnt[RECLAIMABLE_NAT]--;
  202. __free_nat_entry(e);
  203. }
  204. static struct nat_entry_set *__grab_nat_entry_set(struct f2fs_nm_info *nm_i,
  205. struct nat_entry *ne)
  206. {
  207. nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
  208. struct nat_entry_set *head;
  209. head = radix_tree_lookup(&nm_i->nat_set_root, set);
  210. if (!head) {
  211. head = f2fs_kmem_cache_alloc(nat_entry_set_slab,
  212. GFP_NOFS, true, NULL);
  213. INIT_LIST_HEAD(&head->entry_list);
  214. INIT_LIST_HEAD(&head->set_list);
  215. head->set = set;
  216. head->entry_cnt = 0;
  217. f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
  218. }
  219. return head;
  220. }
  221. static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
  222. struct nat_entry *ne)
  223. {
  224. struct nat_entry_set *head;
  225. bool new_ne = nat_get_blkaddr(ne) == NEW_ADDR;
  226. if (!new_ne)
  227. head = __grab_nat_entry_set(nm_i, ne);
  228. /*
  229. * update entry_cnt in below condition:
  230. * 1. update NEW_ADDR to valid block address;
  231. * 2. update old block address to new one;
  232. */
  233. if (!new_ne && (get_nat_flag(ne, IS_PREALLOC) ||
  234. !get_nat_flag(ne, IS_DIRTY)))
  235. head->entry_cnt++;
  236. set_nat_flag(ne, IS_PREALLOC, new_ne);
  237. if (get_nat_flag(ne, IS_DIRTY))
  238. goto refresh_list;
  239. nm_i->nat_cnt[DIRTY_NAT]++;
  240. nm_i->nat_cnt[RECLAIMABLE_NAT]--;
  241. set_nat_flag(ne, IS_DIRTY, true);
  242. refresh_list:
  243. spin_lock(&nm_i->nat_list_lock);
  244. if (new_ne)
  245. list_del_init(&ne->list);
  246. else
  247. list_move_tail(&ne->list, &head->entry_list);
  248. spin_unlock(&nm_i->nat_list_lock);
  249. }
  250. static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
  251. struct nat_entry_set *set, struct nat_entry *ne)
  252. {
  253. spin_lock(&nm_i->nat_list_lock);
  254. list_move_tail(&ne->list, &nm_i->nat_entries);
  255. spin_unlock(&nm_i->nat_list_lock);
  256. set_nat_flag(ne, IS_DIRTY, false);
  257. set->entry_cnt--;
  258. nm_i->nat_cnt[DIRTY_NAT]--;
  259. nm_i->nat_cnt[RECLAIMABLE_NAT]++;
  260. }
  261. static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
  262. nid_t start, unsigned int nr, struct nat_entry_set **ep)
  263. {
  264. return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
  265. start, nr);
  266. }
  267. bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page)
  268. {
  269. return NODE_MAPPING(sbi) == page->mapping &&
  270. IS_DNODE(page) && is_cold_node(page);
  271. }
  272. void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi)
  273. {
  274. spin_lock_init(&sbi->fsync_node_lock);
  275. INIT_LIST_HEAD(&sbi->fsync_node_list);
  276. sbi->fsync_seg_id = 0;
  277. sbi->fsync_node_num = 0;
  278. }
  279. static unsigned int f2fs_add_fsync_node_entry(struct f2fs_sb_info *sbi,
  280. struct page *page)
  281. {
  282. struct fsync_node_entry *fn;
  283. unsigned long flags;
  284. unsigned int seq_id;
  285. fn = f2fs_kmem_cache_alloc(fsync_node_entry_slab,
  286. GFP_NOFS, true, NULL);
  287. get_page(page);
  288. fn->page = page;
  289. INIT_LIST_HEAD(&fn->list);
  290. spin_lock_irqsave(&sbi->fsync_node_lock, flags);
  291. list_add_tail(&fn->list, &sbi->fsync_node_list);
  292. fn->seq_id = sbi->fsync_seg_id++;
  293. seq_id = fn->seq_id;
  294. sbi->fsync_node_num++;
  295. spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
  296. return seq_id;
  297. }
  298. void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page)
  299. {
  300. struct fsync_node_entry *fn;
  301. unsigned long flags;
  302. spin_lock_irqsave(&sbi->fsync_node_lock, flags);
  303. list_for_each_entry(fn, &sbi->fsync_node_list, list) {
  304. if (fn->page == page) {
  305. list_del(&fn->list);
  306. sbi->fsync_node_num--;
  307. spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
  308. kmem_cache_free(fsync_node_entry_slab, fn);
  309. put_page(page);
  310. return;
  311. }
  312. }
  313. spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
  314. f2fs_bug_on(sbi, 1);
  315. }
  316. void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi)
  317. {
  318. unsigned long flags;
  319. spin_lock_irqsave(&sbi->fsync_node_lock, flags);
  320. sbi->fsync_seg_id = 0;
  321. spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
  322. }
  323. int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
  324. {
  325. struct f2fs_nm_info *nm_i = NM_I(sbi);
  326. struct nat_entry *e;
  327. bool need = false;
  328. f2fs_down_read(&nm_i->nat_tree_lock);
  329. e = __lookup_nat_cache(nm_i, nid);
  330. if (e) {
  331. if (!get_nat_flag(e, IS_CHECKPOINTED) &&
  332. !get_nat_flag(e, HAS_FSYNCED_INODE))
  333. need = true;
  334. }
  335. f2fs_up_read(&nm_i->nat_tree_lock);
  336. return need;
  337. }
  338. bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
  339. {
  340. struct f2fs_nm_info *nm_i = NM_I(sbi);
  341. struct nat_entry *e;
  342. bool is_cp = true;
  343. f2fs_down_read(&nm_i->nat_tree_lock);
  344. e = __lookup_nat_cache(nm_i, nid);
  345. if (e && !get_nat_flag(e, IS_CHECKPOINTED))
  346. is_cp = false;
  347. f2fs_up_read(&nm_i->nat_tree_lock);
  348. return is_cp;
  349. }
  350. bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
  351. {
  352. struct f2fs_nm_info *nm_i = NM_I(sbi);
  353. struct nat_entry *e;
  354. bool need_update = true;
  355. f2fs_down_read(&nm_i->nat_tree_lock);
  356. e = __lookup_nat_cache(nm_i, ino);
  357. if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
  358. (get_nat_flag(e, IS_CHECKPOINTED) ||
  359. get_nat_flag(e, HAS_FSYNCED_INODE)))
  360. need_update = false;
  361. f2fs_up_read(&nm_i->nat_tree_lock);
  362. return need_update;
  363. }
  364. /* must be locked by nat_tree_lock */
  365. static void cache_nat_entry(struct f2fs_sb_info *sbi, nid_t nid,
  366. struct f2fs_nat_entry *ne)
  367. {
  368. struct f2fs_nm_info *nm_i = NM_I(sbi);
  369. struct nat_entry *new, *e;
  370. /* Let's mitigate lock contention of nat_tree_lock during checkpoint */
  371. if (f2fs_rwsem_is_locked(&sbi->cp_global_sem))
  372. return;
  373. new = __alloc_nat_entry(sbi, nid, false);
  374. if (!new)
  375. return;
  376. f2fs_down_write(&nm_i->nat_tree_lock);
  377. e = __lookup_nat_cache(nm_i, nid);
  378. if (!e)
  379. e = __init_nat_entry(nm_i, new, ne, false);
  380. else
  381. f2fs_bug_on(sbi, nat_get_ino(e) != le32_to_cpu(ne->ino) ||
  382. nat_get_blkaddr(e) !=
  383. le32_to_cpu(ne->block_addr) ||
  384. nat_get_version(e) != ne->version);
  385. f2fs_up_write(&nm_i->nat_tree_lock);
  386. if (e != new)
  387. __free_nat_entry(new);
  388. }
  389. static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
  390. block_t new_blkaddr, bool fsync_done)
  391. {
  392. struct f2fs_nm_info *nm_i = NM_I(sbi);
  393. struct nat_entry *e;
  394. struct nat_entry *new = __alloc_nat_entry(sbi, ni->nid, true);
  395. f2fs_down_write(&nm_i->nat_tree_lock);
  396. e = __lookup_nat_cache(nm_i, ni->nid);
  397. if (!e) {
  398. e = __init_nat_entry(nm_i, new, NULL, true);
  399. copy_node_info(&e->ni, ni);
  400. f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
  401. } else if (new_blkaddr == NEW_ADDR) {
  402. /*
  403. * when nid is reallocated,
  404. * previous nat entry can be remained in nat cache.
  405. * So, reinitialize it with new information.
  406. */
  407. copy_node_info(&e->ni, ni);
  408. f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
  409. }
  410. /* let's free early to reduce memory consumption */
  411. if (e != new)
  412. __free_nat_entry(new);
  413. /* sanity check */
  414. f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
  415. f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
  416. new_blkaddr == NULL_ADDR);
  417. f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
  418. new_blkaddr == NEW_ADDR);
  419. f2fs_bug_on(sbi, __is_valid_data_blkaddr(nat_get_blkaddr(e)) &&
  420. new_blkaddr == NEW_ADDR);
  421. /* increment version no as node is removed */
  422. if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
  423. unsigned char version = nat_get_version(e);
  424. nat_set_version(e, inc_node_version(version));
  425. }
  426. /* change address */
  427. nat_set_blkaddr(e, new_blkaddr);
  428. if (!__is_valid_data_blkaddr(new_blkaddr))
  429. set_nat_flag(e, IS_CHECKPOINTED, false);
  430. __set_nat_cache_dirty(nm_i, e);
  431. /* update fsync_mark if its inode nat entry is still alive */
  432. if (ni->nid != ni->ino)
  433. e = __lookup_nat_cache(nm_i, ni->ino);
  434. if (e) {
  435. if (fsync_done && ni->nid == ni->ino)
  436. set_nat_flag(e, HAS_FSYNCED_INODE, true);
  437. set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
  438. }
  439. f2fs_up_write(&nm_i->nat_tree_lock);
  440. }
  441. int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
  442. {
  443. struct f2fs_nm_info *nm_i = NM_I(sbi);
  444. int nr = nr_shrink;
  445. if (!f2fs_down_write_trylock(&nm_i->nat_tree_lock))
  446. return 0;
  447. spin_lock(&nm_i->nat_list_lock);
  448. while (nr_shrink) {
  449. struct nat_entry *ne;
  450. if (list_empty(&nm_i->nat_entries))
  451. break;
  452. ne = list_first_entry(&nm_i->nat_entries,
  453. struct nat_entry, list);
  454. list_del(&ne->list);
  455. spin_unlock(&nm_i->nat_list_lock);
  456. __del_from_nat_cache(nm_i, ne);
  457. nr_shrink--;
  458. spin_lock(&nm_i->nat_list_lock);
  459. }
  460. spin_unlock(&nm_i->nat_list_lock);
  461. f2fs_up_write(&nm_i->nat_tree_lock);
  462. return nr - nr_shrink;
  463. }
  464. int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
  465. struct node_info *ni, bool checkpoint_context)
  466. {
  467. struct f2fs_nm_info *nm_i = NM_I(sbi);
  468. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  469. struct f2fs_journal *journal = curseg->journal;
  470. nid_t start_nid = START_NID(nid);
  471. struct f2fs_nat_block *nat_blk;
  472. struct page *page = NULL;
  473. struct f2fs_nat_entry ne;
  474. struct nat_entry *e;
  475. pgoff_t index;
  476. block_t blkaddr;
  477. int i;
  478. ni->nid = nid;
  479. retry:
  480. /* Check nat cache */
  481. f2fs_down_read(&nm_i->nat_tree_lock);
  482. e = __lookup_nat_cache(nm_i, nid);
  483. if (e) {
  484. ni->ino = nat_get_ino(e);
  485. ni->blk_addr = nat_get_blkaddr(e);
  486. ni->version = nat_get_version(e);
  487. f2fs_up_read(&nm_i->nat_tree_lock);
  488. return 0;
  489. }
  490. /*
  491. * Check current segment summary by trying to grab journal_rwsem first.
  492. * This sem is on the critical path on the checkpoint requiring the above
  493. * nat_tree_lock. Therefore, we should retry, if we failed to grab here
  494. * while not bothering checkpoint.
  495. */
  496. if (!f2fs_rwsem_is_locked(&sbi->cp_global_sem) || checkpoint_context) {
  497. down_read(&curseg->journal_rwsem);
  498. } else if (f2fs_rwsem_is_contended(&nm_i->nat_tree_lock) ||
  499. !down_read_trylock(&curseg->journal_rwsem)) {
  500. f2fs_up_read(&nm_i->nat_tree_lock);
  501. goto retry;
  502. }
  503. i = f2fs_lookup_journal_in_cursum(journal, NAT_JOURNAL, nid, 0);
  504. if (i >= 0) {
  505. ne = nat_in_journal(journal, i);
  506. node_info_from_raw_nat(ni, &ne);
  507. }
  508. up_read(&curseg->journal_rwsem);
  509. if (i >= 0) {
  510. f2fs_up_read(&nm_i->nat_tree_lock);
  511. goto cache;
  512. }
  513. /* Fill node_info from nat page */
  514. index = current_nat_addr(sbi, nid);
  515. f2fs_up_read(&nm_i->nat_tree_lock);
  516. page = f2fs_get_meta_page(sbi, index);
  517. if (IS_ERR(page))
  518. return PTR_ERR(page);
  519. nat_blk = (struct f2fs_nat_block *)page_address(page);
  520. ne = nat_blk->entries[nid - start_nid];
  521. node_info_from_raw_nat(ni, &ne);
  522. f2fs_put_page(page, 1);
  523. cache:
  524. blkaddr = le32_to_cpu(ne.block_addr);
  525. if (__is_valid_data_blkaddr(blkaddr) &&
  526. !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE))
  527. return -EFAULT;
  528. /* cache nat entry */
  529. cache_nat_entry(sbi, nid, &ne);
  530. return 0;
  531. }
  532. /*
  533. * readahead MAX_RA_NODE number of node pages.
  534. */
  535. static void f2fs_ra_node_pages(struct page *parent, int start, int n)
  536. {
  537. struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
  538. struct blk_plug plug;
  539. int i, end;
  540. nid_t nid;
  541. blk_start_plug(&plug);
  542. /* Then, try readahead for siblings of the desired node */
  543. end = start + n;
  544. end = min(end, (int)NIDS_PER_BLOCK);
  545. for (i = start; i < end; i++) {
  546. nid = get_nid(parent, i, false);
  547. f2fs_ra_node_page(sbi, nid);
  548. }
  549. blk_finish_plug(&plug);
  550. }
  551. pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs)
  552. {
  553. const long direct_index = ADDRS_PER_INODE(dn->inode);
  554. const long direct_blks = ADDRS_PER_BLOCK(dn->inode);
  555. const long indirect_blks = ADDRS_PER_BLOCK(dn->inode) * NIDS_PER_BLOCK;
  556. unsigned int skipped_unit = ADDRS_PER_BLOCK(dn->inode);
  557. int cur_level = dn->cur_level;
  558. int max_level = dn->max_level;
  559. pgoff_t base = 0;
  560. if (!dn->max_level)
  561. return pgofs + 1;
  562. while (max_level-- > cur_level)
  563. skipped_unit *= NIDS_PER_BLOCK;
  564. switch (dn->max_level) {
  565. case 3:
  566. base += 2 * indirect_blks;
  567. fallthrough;
  568. case 2:
  569. base += 2 * direct_blks;
  570. fallthrough;
  571. case 1:
  572. base += direct_index;
  573. break;
  574. default:
  575. f2fs_bug_on(F2FS_I_SB(dn->inode), 1);
  576. }
  577. return ((pgofs - base) / skipped_unit + 1) * skipped_unit + base;
  578. }
  579. /*
  580. * The maximum depth is four.
  581. * Offset[0] will have raw inode offset.
  582. */
  583. static int get_node_path(struct inode *inode, long block,
  584. int offset[4], unsigned int noffset[4])
  585. {
  586. const long direct_index = ADDRS_PER_INODE(inode);
  587. const long direct_blks = ADDRS_PER_BLOCK(inode);
  588. const long dptrs_per_blk = NIDS_PER_BLOCK;
  589. const long indirect_blks = ADDRS_PER_BLOCK(inode) * NIDS_PER_BLOCK;
  590. const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
  591. int n = 0;
  592. int level = 0;
  593. noffset[0] = 0;
  594. if (block < direct_index) {
  595. offset[n] = block;
  596. goto got;
  597. }
  598. block -= direct_index;
  599. if (block < direct_blks) {
  600. offset[n++] = NODE_DIR1_BLOCK;
  601. noffset[n] = 1;
  602. offset[n] = block;
  603. level = 1;
  604. goto got;
  605. }
  606. block -= direct_blks;
  607. if (block < direct_blks) {
  608. offset[n++] = NODE_DIR2_BLOCK;
  609. noffset[n] = 2;
  610. offset[n] = block;
  611. level = 1;
  612. goto got;
  613. }
  614. block -= direct_blks;
  615. if (block < indirect_blks) {
  616. offset[n++] = NODE_IND1_BLOCK;
  617. noffset[n] = 3;
  618. offset[n++] = block / direct_blks;
  619. noffset[n] = 4 + offset[n - 1];
  620. offset[n] = block % direct_blks;
  621. level = 2;
  622. goto got;
  623. }
  624. block -= indirect_blks;
  625. if (block < indirect_blks) {
  626. offset[n++] = NODE_IND2_BLOCK;
  627. noffset[n] = 4 + dptrs_per_blk;
  628. offset[n++] = block / direct_blks;
  629. noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
  630. offset[n] = block % direct_blks;
  631. level = 2;
  632. goto got;
  633. }
  634. block -= indirect_blks;
  635. if (block < dindirect_blks) {
  636. offset[n++] = NODE_DIND_BLOCK;
  637. noffset[n] = 5 + (dptrs_per_blk * 2);
  638. offset[n++] = block / indirect_blks;
  639. noffset[n] = 6 + (dptrs_per_blk * 2) +
  640. offset[n - 1] * (dptrs_per_blk + 1);
  641. offset[n++] = (block / direct_blks) % dptrs_per_blk;
  642. noffset[n] = 7 + (dptrs_per_blk * 2) +
  643. offset[n - 2] * (dptrs_per_blk + 1) +
  644. offset[n - 1];
  645. offset[n] = block % direct_blks;
  646. level = 3;
  647. goto got;
  648. } else {
  649. return -E2BIG;
  650. }
  651. got:
  652. return level;
  653. }
  654. /*
  655. * Caller should call f2fs_put_dnode(dn).
  656. * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
  657. * f2fs_unlock_op() only if mode is set with ALLOC_NODE.
  658. */
  659. int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
  660. {
  661. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  662. struct page *npage[4];
  663. struct page *parent = NULL;
  664. int offset[4];
  665. unsigned int noffset[4];
  666. nid_t nids[4];
  667. int level, i = 0;
  668. int err = 0;
  669. level = get_node_path(dn->inode, index, offset, noffset);
  670. if (level < 0)
  671. return level;
  672. nids[0] = dn->inode->i_ino;
  673. npage[0] = dn->inode_page;
  674. if (!npage[0]) {
  675. npage[0] = f2fs_get_node_page(sbi, nids[0]);
  676. if (IS_ERR(npage[0]))
  677. return PTR_ERR(npage[0]);
  678. }
  679. /* if inline_data is set, should not report any block indices */
  680. if (f2fs_has_inline_data(dn->inode) && index) {
  681. err = -ENOENT;
  682. f2fs_put_page(npage[0], 1);
  683. goto release_out;
  684. }
  685. parent = npage[0];
  686. if (level != 0)
  687. nids[1] = get_nid(parent, offset[0], true);
  688. dn->inode_page = npage[0];
  689. dn->inode_page_locked = true;
  690. /* get indirect or direct nodes */
  691. for (i = 1; i <= level; i++) {
  692. bool done = false;
  693. if (!nids[i] && mode == ALLOC_NODE) {
  694. /* alloc new node */
  695. if (!f2fs_alloc_nid(sbi, &(nids[i]))) {
  696. err = -ENOSPC;
  697. goto release_pages;
  698. }
  699. dn->nid = nids[i];
  700. npage[i] = f2fs_new_node_page(dn, noffset[i]);
  701. if (IS_ERR(npage[i])) {
  702. f2fs_alloc_nid_failed(sbi, nids[i]);
  703. err = PTR_ERR(npage[i]);
  704. goto release_pages;
  705. }
  706. set_nid(parent, offset[i - 1], nids[i], i == 1);
  707. f2fs_alloc_nid_done(sbi, nids[i]);
  708. done = true;
  709. } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
  710. npage[i] = f2fs_get_node_page_ra(parent, offset[i - 1]);
  711. if (IS_ERR(npage[i])) {
  712. err = PTR_ERR(npage[i]);
  713. goto release_pages;
  714. }
  715. done = true;
  716. }
  717. if (i == 1) {
  718. dn->inode_page_locked = false;
  719. unlock_page(parent);
  720. } else {
  721. f2fs_put_page(parent, 1);
  722. }
  723. if (!done) {
  724. npage[i] = f2fs_get_node_page(sbi, nids[i]);
  725. if (IS_ERR(npage[i])) {
  726. err = PTR_ERR(npage[i]);
  727. f2fs_put_page(npage[0], 0);
  728. goto release_out;
  729. }
  730. }
  731. if (i < level) {
  732. parent = npage[i];
  733. nids[i + 1] = get_nid(parent, offset[i], false);
  734. }
  735. }
  736. dn->nid = nids[level];
  737. dn->ofs_in_node = offset[level];
  738. dn->node_page = npage[level];
  739. dn->data_blkaddr = f2fs_data_blkaddr(dn);
  740. if (is_inode_flag_set(dn->inode, FI_COMPRESSED_FILE) &&
  741. f2fs_sb_has_readonly(sbi)) {
  742. unsigned int c_len = f2fs_cluster_blocks_are_contiguous(dn);
  743. block_t blkaddr;
  744. if (!c_len)
  745. goto out;
  746. blkaddr = f2fs_data_blkaddr(dn);
  747. if (blkaddr == COMPRESS_ADDR)
  748. blkaddr = data_blkaddr(dn->inode, dn->node_page,
  749. dn->ofs_in_node + 1);
  750. f2fs_update_read_extent_tree_range_compressed(dn->inode,
  751. index, blkaddr,
  752. F2FS_I(dn->inode)->i_cluster_size,
  753. c_len);
  754. }
  755. out:
  756. return 0;
  757. release_pages:
  758. f2fs_put_page(parent, 1);
  759. if (i > 1)
  760. f2fs_put_page(npage[0], 0);
  761. release_out:
  762. dn->inode_page = NULL;
  763. dn->node_page = NULL;
  764. if (err == -ENOENT) {
  765. dn->cur_level = i;
  766. dn->max_level = level;
  767. dn->ofs_in_node = offset[level];
  768. }
  769. return err;
  770. }
  771. static int truncate_node(struct dnode_of_data *dn)
  772. {
  773. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  774. struct node_info ni;
  775. int err;
  776. pgoff_t index;
  777. err = f2fs_get_node_info(sbi, dn->nid, &ni, false);
  778. if (err)
  779. return err;
  780. /* Deallocate node address */
  781. f2fs_invalidate_blocks(sbi, ni.blk_addr);
  782. dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
  783. set_node_addr(sbi, &ni, NULL_ADDR, false);
  784. if (dn->nid == dn->inode->i_ino) {
  785. f2fs_remove_orphan_inode(sbi, dn->nid);
  786. dec_valid_inode_count(sbi);
  787. f2fs_inode_synced(dn->inode);
  788. }
  789. clear_node_page_dirty(dn->node_page);
  790. set_sbi_flag(sbi, SBI_IS_DIRTY);
  791. index = dn->node_page->index;
  792. f2fs_put_page(dn->node_page, 1);
  793. invalidate_mapping_pages(NODE_MAPPING(sbi),
  794. index, index);
  795. dn->node_page = NULL;
  796. trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
  797. return 0;
  798. }
  799. static int truncate_dnode(struct dnode_of_data *dn)
  800. {
  801. struct page *page;
  802. int err;
  803. if (dn->nid == 0)
  804. return 1;
  805. /* get direct node */
  806. page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
  807. if (PTR_ERR(page) == -ENOENT)
  808. return 1;
  809. else if (IS_ERR(page))
  810. return PTR_ERR(page);
  811. /* Make dnode_of_data for parameter */
  812. dn->node_page = page;
  813. dn->ofs_in_node = 0;
  814. f2fs_truncate_data_blocks(dn);
  815. err = truncate_node(dn);
  816. if (err) {
  817. f2fs_put_page(page, 1);
  818. return err;
  819. }
  820. return 1;
  821. }
  822. static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
  823. int ofs, int depth)
  824. {
  825. struct dnode_of_data rdn = *dn;
  826. struct page *page;
  827. struct f2fs_node *rn;
  828. nid_t child_nid;
  829. unsigned int child_nofs;
  830. int freed = 0;
  831. int i, ret;
  832. if (dn->nid == 0)
  833. return NIDS_PER_BLOCK + 1;
  834. trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
  835. page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
  836. if (IS_ERR(page)) {
  837. trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
  838. return PTR_ERR(page);
  839. }
  840. f2fs_ra_node_pages(page, ofs, NIDS_PER_BLOCK);
  841. rn = F2FS_NODE(page);
  842. if (depth < 3) {
  843. for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
  844. child_nid = le32_to_cpu(rn->in.nid[i]);
  845. if (child_nid == 0)
  846. continue;
  847. rdn.nid = child_nid;
  848. ret = truncate_dnode(&rdn);
  849. if (ret < 0)
  850. goto out_err;
  851. if (set_nid(page, i, 0, false))
  852. dn->node_changed = true;
  853. }
  854. } else {
  855. child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
  856. for (i = ofs; i < NIDS_PER_BLOCK; i++) {
  857. child_nid = le32_to_cpu(rn->in.nid[i]);
  858. if (child_nid == 0) {
  859. child_nofs += NIDS_PER_BLOCK + 1;
  860. continue;
  861. }
  862. rdn.nid = child_nid;
  863. ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
  864. if (ret == (NIDS_PER_BLOCK + 1)) {
  865. if (set_nid(page, i, 0, false))
  866. dn->node_changed = true;
  867. child_nofs += ret;
  868. } else if (ret < 0 && ret != -ENOENT) {
  869. goto out_err;
  870. }
  871. }
  872. freed = child_nofs;
  873. }
  874. if (!ofs) {
  875. /* remove current indirect node */
  876. dn->node_page = page;
  877. ret = truncate_node(dn);
  878. if (ret)
  879. goto out_err;
  880. freed++;
  881. } else {
  882. f2fs_put_page(page, 1);
  883. }
  884. trace_f2fs_truncate_nodes_exit(dn->inode, freed);
  885. return freed;
  886. out_err:
  887. f2fs_put_page(page, 1);
  888. trace_f2fs_truncate_nodes_exit(dn->inode, ret);
  889. return ret;
  890. }
  891. static int truncate_partial_nodes(struct dnode_of_data *dn,
  892. struct f2fs_inode *ri, int *offset, int depth)
  893. {
  894. struct page *pages[2];
  895. nid_t nid[3];
  896. nid_t child_nid;
  897. int err = 0;
  898. int i;
  899. int idx = depth - 2;
  900. nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
  901. if (!nid[0])
  902. return 0;
  903. /* get indirect nodes in the path */
  904. for (i = 0; i < idx + 1; i++) {
  905. /* reference count'll be increased */
  906. pages[i] = f2fs_get_node_page(F2FS_I_SB(dn->inode), nid[i]);
  907. if (IS_ERR(pages[i])) {
  908. err = PTR_ERR(pages[i]);
  909. idx = i - 1;
  910. goto fail;
  911. }
  912. nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
  913. }
  914. f2fs_ra_node_pages(pages[idx], offset[idx + 1], NIDS_PER_BLOCK);
  915. /* free direct nodes linked to a partial indirect node */
  916. for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
  917. child_nid = get_nid(pages[idx], i, false);
  918. if (!child_nid)
  919. continue;
  920. dn->nid = child_nid;
  921. err = truncate_dnode(dn);
  922. if (err < 0)
  923. goto fail;
  924. if (set_nid(pages[idx], i, 0, false))
  925. dn->node_changed = true;
  926. }
  927. if (offset[idx + 1] == 0) {
  928. dn->node_page = pages[idx];
  929. dn->nid = nid[idx];
  930. err = truncate_node(dn);
  931. if (err)
  932. goto fail;
  933. } else {
  934. f2fs_put_page(pages[idx], 1);
  935. }
  936. offset[idx]++;
  937. offset[idx + 1] = 0;
  938. idx--;
  939. fail:
  940. for (i = idx; i >= 0; i--)
  941. f2fs_put_page(pages[i], 1);
  942. trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
  943. return err;
  944. }
  945. /*
  946. * All the block addresses of data and nodes should be nullified.
  947. */
  948. int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from)
  949. {
  950. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  951. int err = 0, cont = 1;
  952. int level, offset[4], noffset[4];
  953. unsigned int nofs = 0;
  954. struct f2fs_inode *ri;
  955. struct dnode_of_data dn;
  956. struct page *page;
  957. trace_f2fs_truncate_inode_blocks_enter(inode, from);
  958. level = get_node_path(inode, from, offset, noffset);
  959. if (level < 0) {
  960. trace_f2fs_truncate_inode_blocks_exit(inode, level);
  961. return level;
  962. }
  963. page = f2fs_get_node_page(sbi, inode->i_ino);
  964. if (IS_ERR(page)) {
  965. trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
  966. return PTR_ERR(page);
  967. }
  968. set_new_dnode(&dn, inode, page, NULL, 0);
  969. unlock_page(page);
  970. ri = F2FS_INODE(page);
  971. switch (level) {
  972. case 0:
  973. case 1:
  974. nofs = noffset[1];
  975. break;
  976. case 2:
  977. nofs = noffset[1];
  978. if (!offset[level - 1])
  979. goto skip_partial;
  980. err = truncate_partial_nodes(&dn, ri, offset, level);
  981. if (err < 0 && err != -ENOENT)
  982. goto fail;
  983. nofs += 1 + NIDS_PER_BLOCK;
  984. break;
  985. case 3:
  986. nofs = 5 + 2 * NIDS_PER_BLOCK;
  987. if (!offset[level - 1])
  988. goto skip_partial;
  989. err = truncate_partial_nodes(&dn, ri, offset, level);
  990. if (err < 0 && err != -ENOENT)
  991. goto fail;
  992. break;
  993. default:
  994. BUG();
  995. }
  996. skip_partial:
  997. while (cont) {
  998. dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
  999. switch (offset[0]) {
  1000. case NODE_DIR1_BLOCK:
  1001. case NODE_DIR2_BLOCK:
  1002. err = truncate_dnode(&dn);
  1003. break;
  1004. case NODE_IND1_BLOCK:
  1005. case NODE_IND2_BLOCK:
  1006. err = truncate_nodes(&dn, nofs, offset[1], 2);
  1007. break;
  1008. case NODE_DIND_BLOCK:
  1009. err = truncate_nodes(&dn, nofs, offset[1], 3);
  1010. cont = 0;
  1011. break;
  1012. default:
  1013. BUG();
  1014. }
  1015. if (err < 0 && err != -ENOENT)
  1016. goto fail;
  1017. if (offset[1] == 0 &&
  1018. ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
  1019. lock_page(page);
  1020. BUG_ON(page->mapping != NODE_MAPPING(sbi));
  1021. f2fs_wait_on_page_writeback(page, NODE, true, true);
  1022. ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
  1023. set_page_dirty(page);
  1024. unlock_page(page);
  1025. }
  1026. offset[1] = 0;
  1027. offset[0]++;
  1028. nofs += err;
  1029. }
  1030. fail:
  1031. f2fs_put_page(page, 0);
  1032. trace_f2fs_truncate_inode_blocks_exit(inode, err);
  1033. return err > 0 ? 0 : err;
  1034. }
  1035. /* caller must lock inode page */
  1036. int f2fs_truncate_xattr_node(struct inode *inode)
  1037. {
  1038. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1039. nid_t nid = F2FS_I(inode)->i_xattr_nid;
  1040. struct dnode_of_data dn;
  1041. struct page *npage;
  1042. int err;
  1043. if (!nid)
  1044. return 0;
  1045. npage = f2fs_get_node_page(sbi, nid);
  1046. if (IS_ERR(npage))
  1047. return PTR_ERR(npage);
  1048. set_new_dnode(&dn, inode, NULL, npage, nid);
  1049. err = truncate_node(&dn);
  1050. if (err) {
  1051. f2fs_put_page(npage, 1);
  1052. return err;
  1053. }
  1054. f2fs_i_xnid_write(inode, 0);
  1055. return 0;
  1056. }
  1057. /*
  1058. * Caller should grab and release a rwsem by calling f2fs_lock_op() and
  1059. * f2fs_unlock_op().
  1060. */
  1061. int f2fs_remove_inode_page(struct inode *inode)
  1062. {
  1063. struct dnode_of_data dn;
  1064. int err;
  1065. set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
  1066. err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
  1067. if (err)
  1068. return err;
  1069. err = f2fs_truncate_xattr_node(inode);
  1070. if (err) {
  1071. f2fs_put_dnode(&dn);
  1072. return err;
  1073. }
  1074. /* remove potential inline_data blocks */
  1075. if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1076. S_ISLNK(inode->i_mode))
  1077. f2fs_truncate_data_blocks_range(&dn, 1);
  1078. /* 0 is possible, after f2fs_new_inode() has failed */
  1079. if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
  1080. f2fs_put_dnode(&dn);
  1081. return -EIO;
  1082. }
  1083. if (unlikely(inode->i_blocks != 0 && inode->i_blocks != 8)) {
  1084. f2fs_warn(F2FS_I_SB(inode),
  1085. "f2fs_remove_inode_page: inconsistent i_blocks, ino:%lu, iblocks:%llu",
  1086. inode->i_ino, (unsigned long long)inode->i_blocks);
  1087. set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
  1088. }
  1089. /* will put inode & node pages */
  1090. err = truncate_node(&dn);
  1091. if (err) {
  1092. f2fs_put_dnode(&dn);
  1093. return err;
  1094. }
  1095. return 0;
  1096. }
  1097. struct page *f2fs_new_inode_page(struct inode *inode)
  1098. {
  1099. struct dnode_of_data dn;
  1100. /* allocate inode page for new inode */
  1101. set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
  1102. /* caller should f2fs_put_page(page, 1); */
  1103. return f2fs_new_node_page(&dn, 0);
  1104. }
  1105. struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs)
  1106. {
  1107. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  1108. struct node_info new_ni;
  1109. struct page *page;
  1110. int err;
  1111. if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
  1112. return ERR_PTR(-EPERM);
  1113. page = f2fs_grab_cache_page(NODE_MAPPING(sbi), dn->nid, false);
  1114. if (!page)
  1115. return ERR_PTR(-ENOMEM);
  1116. if (unlikely((err = inc_valid_node_count(sbi, dn->inode, !ofs))))
  1117. goto fail;
  1118. #ifdef CONFIG_F2FS_CHECK_FS
  1119. err = f2fs_get_node_info(sbi, dn->nid, &new_ni, false);
  1120. if (err) {
  1121. dec_valid_node_count(sbi, dn->inode, !ofs);
  1122. goto fail;
  1123. }
  1124. if (unlikely(new_ni.blk_addr != NULL_ADDR)) {
  1125. err = -EFSCORRUPTED;
  1126. set_sbi_flag(sbi, SBI_NEED_FSCK);
  1127. f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
  1128. goto fail;
  1129. }
  1130. #endif
  1131. new_ni.nid = dn->nid;
  1132. new_ni.ino = dn->inode->i_ino;
  1133. new_ni.blk_addr = NULL_ADDR;
  1134. new_ni.flag = 0;
  1135. new_ni.version = 0;
  1136. set_node_addr(sbi, &new_ni, NEW_ADDR, false);
  1137. f2fs_wait_on_page_writeback(page, NODE, true, true);
  1138. fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
  1139. set_cold_node(page, S_ISDIR(dn->inode->i_mode));
  1140. if (!PageUptodate(page))
  1141. SetPageUptodate(page);
  1142. if (set_page_dirty(page))
  1143. dn->node_changed = true;
  1144. if (f2fs_has_xattr_block(ofs))
  1145. f2fs_i_xnid_write(dn->inode, dn->nid);
  1146. if (ofs == 0)
  1147. inc_valid_inode_count(sbi);
  1148. return page;
  1149. fail:
  1150. clear_node_page_dirty(page);
  1151. f2fs_put_page(page, 1);
  1152. return ERR_PTR(err);
  1153. }
  1154. /*
  1155. * Caller should do after getting the following values.
  1156. * 0: f2fs_put_page(page, 0)
  1157. * LOCKED_PAGE or error: f2fs_put_page(page, 1)
  1158. */
  1159. static int read_node_page(struct page *page, blk_opf_t op_flags)
  1160. {
  1161. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  1162. struct node_info ni;
  1163. struct f2fs_io_info fio = {
  1164. .sbi = sbi,
  1165. .type = NODE,
  1166. .op = REQ_OP_READ,
  1167. .op_flags = op_flags,
  1168. .page = page,
  1169. .encrypted_page = NULL,
  1170. };
  1171. int err;
  1172. if (PageUptodate(page)) {
  1173. if (!f2fs_inode_chksum_verify(sbi, page)) {
  1174. ClearPageUptodate(page);
  1175. return -EFSBADCRC;
  1176. }
  1177. return LOCKED_PAGE;
  1178. }
  1179. err = f2fs_get_node_info(sbi, page->index, &ni, false);
  1180. if (err)
  1181. return err;
  1182. /* NEW_ADDR can be seen, after cp_error drops some dirty node pages */
  1183. if (unlikely(ni.blk_addr == NULL_ADDR || ni.blk_addr == NEW_ADDR)) {
  1184. ClearPageUptodate(page);
  1185. return -ENOENT;
  1186. }
  1187. fio.new_blkaddr = fio.old_blkaddr = ni.blk_addr;
  1188. err = f2fs_submit_page_bio(&fio);
  1189. if (!err)
  1190. f2fs_update_iostat(sbi, NULL, FS_NODE_READ_IO, F2FS_BLKSIZE);
  1191. return err;
  1192. }
  1193. /*
  1194. * Readahead a node page
  1195. */
  1196. void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
  1197. {
  1198. struct page *apage;
  1199. int err;
  1200. if (!nid)
  1201. return;
  1202. if (f2fs_check_nid_range(sbi, nid))
  1203. return;
  1204. apage = xa_load(&NODE_MAPPING(sbi)->i_pages, nid);
  1205. if (apage)
  1206. return;
  1207. apage = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
  1208. if (!apage)
  1209. return;
  1210. err = read_node_page(apage, REQ_RAHEAD);
  1211. f2fs_put_page(apage, err ? 1 : 0);
  1212. }
  1213. static struct page *__get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid,
  1214. struct page *parent, int start)
  1215. {
  1216. struct page *page;
  1217. int err;
  1218. if (!nid)
  1219. return ERR_PTR(-ENOENT);
  1220. if (f2fs_check_nid_range(sbi, nid))
  1221. return ERR_PTR(-EINVAL);
  1222. repeat:
  1223. page = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
  1224. if (!page)
  1225. return ERR_PTR(-ENOMEM);
  1226. err = read_node_page(page, 0);
  1227. if (err < 0) {
  1228. goto out_put_err;
  1229. } else if (err == LOCKED_PAGE) {
  1230. err = 0;
  1231. goto page_hit;
  1232. }
  1233. if (parent)
  1234. f2fs_ra_node_pages(parent, start + 1, MAX_RA_NODE);
  1235. lock_page(page);
  1236. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1237. f2fs_put_page(page, 1);
  1238. goto repeat;
  1239. }
  1240. if (unlikely(!PageUptodate(page))) {
  1241. err = -EIO;
  1242. goto out_err;
  1243. }
  1244. if (!f2fs_inode_chksum_verify(sbi, page)) {
  1245. err = -EFSBADCRC;
  1246. goto out_err;
  1247. }
  1248. page_hit:
  1249. if (likely(nid == nid_of_node(page)))
  1250. return page;
  1251. f2fs_warn(sbi, "inconsistent node block, nid:%lu, node_footer[nid:%u,ino:%u,ofs:%u,cpver:%llu,blkaddr:%u]",
  1252. nid, nid_of_node(page), ino_of_node(page),
  1253. ofs_of_node(page), cpver_of_node(page),
  1254. next_blkaddr_of_node(page));
  1255. set_sbi_flag(sbi, SBI_NEED_FSCK);
  1256. f2fs_handle_error(sbi, ERROR_INCONSISTENT_FOOTER);
  1257. err = -EFSCORRUPTED;
  1258. out_err:
  1259. ClearPageUptodate(page);
  1260. out_put_err:
  1261. /* ENOENT comes from read_node_page which is not an error. */
  1262. if (err != -ENOENT)
  1263. f2fs_handle_page_eio(sbi, page->index, NODE);
  1264. f2fs_put_page(page, 1);
  1265. return ERR_PTR(err);
  1266. }
  1267. struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
  1268. {
  1269. return __get_node_page(sbi, nid, NULL, 0);
  1270. }
  1271. struct page *f2fs_get_node_page_ra(struct page *parent, int start)
  1272. {
  1273. struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
  1274. nid_t nid = get_nid(parent, start, false);
  1275. return __get_node_page(sbi, nid, parent, start);
  1276. }
  1277. static void flush_inline_data(struct f2fs_sb_info *sbi, nid_t ino)
  1278. {
  1279. struct inode *inode;
  1280. struct page *page;
  1281. int ret;
  1282. /* should flush inline_data before evict_inode */
  1283. inode = ilookup(sbi->sb, ino);
  1284. if (!inode)
  1285. return;
  1286. page = f2fs_pagecache_get_page(inode->i_mapping, 0,
  1287. FGP_LOCK|FGP_NOWAIT, 0);
  1288. if (!page)
  1289. goto iput_out;
  1290. if (!PageUptodate(page))
  1291. goto page_out;
  1292. if (!PageDirty(page))
  1293. goto page_out;
  1294. if (!clear_page_dirty_for_io(page))
  1295. goto page_out;
  1296. ret = f2fs_write_inline_data(inode, page);
  1297. inode_dec_dirty_pages(inode);
  1298. f2fs_remove_dirty_inode(inode);
  1299. if (ret)
  1300. set_page_dirty(page);
  1301. page_out:
  1302. f2fs_put_page(page, 1);
  1303. iput_out:
  1304. iput(inode);
  1305. }
  1306. static struct page *last_fsync_dnode(struct f2fs_sb_info *sbi, nid_t ino)
  1307. {
  1308. pgoff_t index;
  1309. struct pagevec pvec;
  1310. struct page *last_page = NULL;
  1311. int nr_pages;
  1312. pagevec_init(&pvec);
  1313. index = 0;
  1314. while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  1315. PAGECACHE_TAG_DIRTY))) {
  1316. int i;
  1317. for (i = 0; i < nr_pages; i++) {
  1318. struct page *page = pvec.pages[i];
  1319. if (unlikely(f2fs_cp_error(sbi))) {
  1320. f2fs_put_page(last_page, 0);
  1321. pagevec_release(&pvec);
  1322. return ERR_PTR(-EIO);
  1323. }
  1324. if (!IS_DNODE(page) || !is_cold_node(page))
  1325. continue;
  1326. if (ino_of_node(page) != ino)
  1327. continue;
  1328. lock_page(page);
  1329. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1330. continue_unlock:
  1331. unlock_page(page);
  1332. continue;
  1333. }
  1334. if (ino_of_node(page) != ino)
  1335. goto continue_unlock;
  1336. if (!PageDirty(page)) {
  1337. /* someone wrote it for us */
  1338. goto continue_unlock;
  1339. }
  1340. if (last_page)
  1341. f2fs_put_page(last_page, 0);
  1342. get_page(page);
  1343. last_page = page;
  1344. unlock_page(page);
  1345. }
  1346. pagevec_release(&pvec);
  1347. cond_resched();
  1348. }
  1349. return last_page;
  1350. }
  1351. static int __write_node_page(struct page *page, bool atomic, bool *submitted,
  1352. struct writeback_control *wbc, bool do_balance,
  1353. enum iostat_type io_type, unsigned int *seq_id)
  1354. {
  1355. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  1356. nid_t nid;
  1357. struct node_info ni;
  1358. struct f2fs_io_info fio = {
  1359. .sbi = sbi,
  1360. .ino = ino_of_node(page),
  1361. .type = NODE,
  1362. .op = REQ_OP_WRITE,
  1363. .op_flags = wbc_to_write_flags(wbc),
  1364. .page = page,
  1365. .encrypted_page = NULL,
  1366. .submitted = 0,
  1367. .io_type = io_type,
  1368. .io_wbc = wbc,
  1369. };
  1370. unsigned int seq;
  1371. trace_f2fs_writepage(page, NODE);
  1372. if (unlikely(f2fs_cp_error(sbi))) {
  1373. ClearPageUptodate(page);
  1374. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1375. unlock_page(page);
  1376. return 0;
  1377. }
  1378. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  1379. goto redirty_out;
  1380. if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
  1381. wbc->sync_mode == WB_SYNC_NONE &&
  1382. IS_DNODE(page) && is_cold_node(page))
  1383. goto redirty_out;
  1384. /* get old block addr of this node page */
  1385. nid = nid_of_node(page);
  1386. f2fs_bug_on(sbi, page->index != nid);
  1387. if (f2fs_get_node_info(sbi, nid, &ni, !do_balance))
  1388. goto redirty_out;
  1389. if (wbc->for_reclaim) {
  1390. if (!f2fs_down_read_trylock(&sbi->node_write))
  1391. goto redirty_out;
  1392. } else {
  1393. f2fs_down_read(&sbi->node_write);
  1394. }
  1395. /* This page is already truncated */
  1396. if (unlikely(ni.blk_addr == NULL_ADDR)) {
  1397. ClearPageUptodate(page);
  1398. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1399. f2fs_up_read(&sbi->node_write);
  1400. unlock_page(page);
  1401. return 0;
  1402. }
  1403. if (__is_valid_data_blkaddr(ni.blk_addr) &&
  1404. !f2fs_is_valid_blkaddr(sbi, ni.blk_addr,
  1405. DATA_GENERIC_ENHANCE)) {
  1406. f2fs_up_read(&sbi->node_write);
  1407. goto redirty_out;
  1408. }
  1409. if (atomic && !test_opt(sbi, NOBARRIER) && !f2fs_sb_has_blkzoned(sbi))
  1410. fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
  1411. /* should add to global list before clearing PAGECACHE status */
  1412. if (f2fs_in_warm_node_list(sbi, page)) {
  1413. seq = f2fs_add_fsync_node_entry(sbi, page);
  1414. if (seq_id)
  1415. *seq_id = seq;
  1416. }
  1417. set_page_writeback(page);
  1418. fio.old_blkaddr = ni.blk_addr;
  1419. f2fs_do_write_node_page(nid, &fio);
  1420. set_node_addr(sbi, &ni, fio.new_blkaddr, is_fsync_dnode(page));
  1421. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1422. f2fs_up_read(&sbi->node_write);
  1423. if (wbc->for_reclaim) {
  1424. f2fs_submit_merged_write_cond(sbi, NULL, page, 0, NODE);
  1425. submitted = NULL;
  1426. }
  1427. unlock_page(page);
  1428. if (unlikely(f2fs_cp_error(sbi))) {
  1429. f2fs_submit_merged_write(sbi, NODE);
  1430. submitted = NULL;
  1431. }
  1432. if (submitted)
  1433. *submitted = fio.submitted;
  1434. if (do_balance)
  1435. f2fs_balance_fs(sbi, false);
  1436. return 0;
  1437. redirty_out:
  1438. redirty_page_for_writepage(wbc, page);
  1439. return AOP_WRITEPAGE_ACTIVATE;
  1440. }
  1441. int f2fs_move_node_page(struct page *node_page, int gc_type)
  1442. {
  1443. int err = 0;
  1444. if (gc_type == FG_GC) {
  1445. struct writeback_control wbc = {
  1446. .sync_mode = WB_SYNC_ALL,
  1447. .nr_to_write = 1,
  1448. .for_reclaim = 0,
  1449. };
  1450. f2fs_wait_on_page_writeback(node_page, NODE, true, true);
  1451. set_page_dirty(node_page);
  1452. if (!clear_page_dirty_for_io(node_page)) {
  1453. err = -EAGAIN;
  1454. goto out_page;
  1455. }
  1456. if (__write_node_page(node_page, false, NULL,
  1457. &wbc, false, FS_GC_NODE_IO, NULL)) {
  1458. err = -EAGAIN;
  1459. unlock_page(node_page);
  1460. }
  1461. goto release_page;
  1462. } else {
  1463. /* set page dirty and write it */
  1464. if (!PageWriteback(node_page))
  1465. set_page_dirty(node_page);
  1466. }
  1467. out_page:
  1468. unlock_page(node_page);
  1469. release_page:
  1470. f2fs_put_page(node_page, 0);
  1471. return err;
  1472. }
  1473. static int f2fs_write_node_page(struct page *page,
  1474. struct writeback_control *wbc)
  1475. {
  1476. return __write_node_page(page, false, NULL, wbc, false,
  1477. FS_NODE_IO, NULL);
  1478. }
  1479. int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
  1480. struct writeback_control *wbc, bool atomic,
  1481. unsigned int *seq_id)
  1482. {
  1483. pgoff_t index;
  1484. struct pagevec pvec;
  1485. int ret = 0;
  1486. struct page *last_page = NULL;
  1487. bool marked = false;
  1488. nid_t ino = inode->i_ino;
  1489. int nr_pages;
  1490. int nwritten = 0;
  1491. if (atomic) {
  1492. last_page = last_fsync_dnode(sbi, ino);
  1493. if (IS_ERR_OR_NULL(last_page))
  1494. return PTR_ERR_OR_ZERO(last_page);
  1495. }
  1496. retry:
  1497. pagevec_init(&pvec);
  1498. index = 0;
  1499. while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
  1500. PAGECACHE_TAG_DIRTY))) {
  1501. int i;
  1502. for (i = 0; i < nr_pages; i++) {
  1503. struct page *page = pvec.pages[i];
  1504. bool submitted = false;
  1505. if (unlikely(f2fs_cp_error(sbi))) {
  1506. f2fs_put_page(last_page, 0);
  1507. pagevec_release(&pvec);
  1508. ret = -EIO;
  1509. goto out;
  1510. }
  1511. if (!IS_DNODE(page) || !is_cold_node(page))
  1512. continue;
  1513. if (ino_of_node(page) != ino)
  1514. continue;
  1515. lock_page(page);
  1516. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1517. continue_unlock:
  1518. unlock_page(page);
  1519. continue;
  1520. }
  1521. if (ino_of_node(page) != ino)
  1522. goto continue_unlock;
  1523. if (!PageDirty(page) && page != last_page) {
  1524. /* someone wrote it for us */
  1525. goto continue_unlock;
  1526. }
  1527. f2fs_wait_on_page_writeback(page, NODE, true, true);
  1528. set_fsync_mark(page, 0);
  1529. set_dentry_mark(page, 0);
  1530. if (!atomic || page == last_page) {
  1531. set_fsync_mark(page, 1);
  1532. percpu_counter_inc(&sbi->rf_node_block_count);
  1533. if (IS_INODE(page)) {
  1534. if (is_inode_flag_set(inode,
  1535. FI_DIRTY_INODE))
  1536. f2fs_update_inode(inode, page);
  1537. set_dentry_mark(page,
  1538. f2fs_need_dentry_mark(sbi, ino));
  1539. }
  1540. /* may be written by other thread */
  1541. if (!PageDirty(page))
  1542. set_page_dirty(page);
  1543. }
  1544. if (!clear_page_dirty_for_io(page))
  1545. goto continue_unlock;
  1546. ret = __write_node_page(page, atomic &&
  1547. page == last_page,
  1548. &submitted, wbc, true,
  1549. FS_NODE_IO, seq_id);
  1550. if (ret) {
  1551. unlock_page(page);
  1552. f2fs_put_page(last_page, 0);
  1553. break;
  1554. } else if (submitted) {
  1555. nwritten++;
  1556. }
  1557. if (page == last_page) {
  1558. f2fs_put_page(page, 0);
  1559. marked = true;
  1560. break;
  1561. }
  1562. }
  1563. pagevec_release(&pvec);
  1564. cond_resched();
  1565. if (ret || marked)
  1566. break;
  1567. }
  1568. if (!ret && atomic && !marked) {
  1569. f2fs_debug(sbi, "Retry to write fsync mark: ino=%u, idx=%lx",
  1570. ino, last_page->index);
  1571. lock_page(last_page);
  1572. f2fs_wait_on_page_writeback(last_page, NODE, true, true);
  1573. set_page_dirty(last_page);
  1574. unlock_page(last_page);
  1575. goto retry;
  1576. }
  1577. out:
  1578. if (nwritten)
  1579. f2fs_submit_merged_write_cond(sbi, NULL, NULL, ino, NODE);
  1580. return ret ? -EIO : 0;
  1581. }
  1582. static int f2fs_match_ino(struct inode *inode, unsigned long ino, void *data)
  1583. {
  1584. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1585. bool clean;
  1586. if (inode->i_ino != ino)
  1587. return 0;
  1588. if (!is_inode_flag_set(inode, FI_DIRTY_INODE))
  1589. return 0;
  1590. spin_lock(&sbi->inode_lock[DIRTY_META]);
  1591. clean = list_empty(&F2FS_I(inode)->gdirty_list);
  1592. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  1593. if (clean)
  1594. return 0;
  1595. inode = igrab(inode);
  1596. if (!inode)
  1597. return 0;
  1598. return 1;
  1599. }
  1600. static bool flush_dirty_inode(struct page *page)
  1601. {
  1602. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  1603. struct inode *inode;
  1604. nid_t ino = ino_of_node(page);
  1605. inode = find_inode_nowait(sbi->sb, ino, f2fs_match_ino, NULL);
  1606. if (!inode)
  1607. return false;
  1608. f2fs_update_inode(inode, page);
  1609. unlock_page(page);
  1610. iput(inode);
  1611. return true;
  1612. }
  1613. void f2fs_flush_inline_data(struct f2fs_sb_info *sbi)
  1614. {
  1615. pgoff_t index = 0;
  1616. struct pagevec pvec;
  1617. int nr_pages;
  1618. pagevec_init(&pvec);
  1619. while ((nr_pages = pagevec_lookup_tag(&pvec,
  1620. NODE_MAPPING(sbi), &index, PAGECACHE_TAG_DIRTY))) {
  1621. int i;
  1622. for (i = 0; i < nr_pages; i++) {
  1623. struct page *page = pvec.pages[i];
  1624. if (!IS_DNODE(page))
  1625. continue;
  1626. lock_page(page);
  1627. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1628. continue_unlock:
  1629. unlock_page(page);
  1630. continue;
  1631. }
  1632. if (!PageDirty(page)) {
  1633. /* someone wrote it for us */
  1634. goto continue_unlock;
  1635. }
  1636. /* flush inline_data, if it's async context. */
  1637. if (page_private_inline(page)) {
  1638. clear_page_private_inline(page);
  1639. unlock_page(page);
  1640. flush_inline_data(sbi, ino_of_node(page));
  1641. continue;
  1642. }
  1643. unlock_page(page);
  1644. }
  1645. pagevec_release(&pvec);
  1646. cond_resched();
  1647. }
  1648. }
  1649. int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
  1650. struct writeback_control *wbc,
  1651. bool do_balance, enum iostat_type io_type)
  1652. {
  1653. pgoff_t index;
  1654. struct pagevec pvec;
  1655. int step = 0;
  1656. int nwritten = 0;
  1657. int ret = 0;
  1658. int nr_pages, done = 0;
  1659. pagevec_init(&pvec);
  1660. next_step:
  1661. index = 0;
  1662. while (!done && (nr_pages = pagevec_lookup_tag(&pvec,
  1663. NODE_MAPPING(sbi), &index, PAGECACHE_TAG_DIRTY))) {
  1664. int i;
  1665. for (i = 0; i < nr_pages; i++) {
  1666. struct page *page = pvec.pages[i];
  1667. bool submitted = false;
  1668. /* give a priority to WB_SYNC threads */
  1669. if (atomic_read(&sbi->wb_sync_req[NODE]) &&
  1670. wbc->sync_mode == WB_SYNC_NONE) {
  1671. done = 1;
  1672. break;
  1673. }
  1674. /*
  1675. * flushing sequence with step:
  1676. * 0. indirect nodes
  1677. * 1. dentry dnodes
  1678. * 2. file dnodes
  1679. */
  1680. if (step == 0 && IS_DNODE(page))
  1681. continue;
  1682. if (step == 1 && (!IS_DNODE(page) ||
  1683. is_cold_node(page)))
  1684. continue;
  1685. if (step == 2 && (!IS_DNODE(page) ||
  1686. !is_cold_node(page)))
  1687. continue;
  1688. lock_node:
  1689. if (wbc->sync_mode == WB_SYNC_ALL)
  1690. lock_page(page);
  1691. else if (!trylock_page(page))
  1692. continue;
  1693. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1694. continue_unlock:
  1695. unlock_page(page);
  1696. continue;
  1697. }
  1698. if (!PageDirty(page)) {
  1699. /* someone wrote it for us */
  1700. goto continue_unlock;
  1701. }
  1702. /* flush inline_data/inode, if it's async context. */
  1703. if (!do_balance)
  1704. goto write_node;
  1705. /* flush inline_data */
  1706. if (page_private_inline(page)) {
  1707. clear_page_private_inline(page);
  1708. unlock_page(page);
  1709. flush_inline_data(sbi, ino_of_node(page));
  1710. goto lock_node;
  1711. }
  1712. /* flush dirty inode */
  1713. if (IS_INODE(page) && flush_dirty_inode(page))
  1714. goto lock_node;
  1715. write_node:
  1716. f2fs_wait_on_page_writeback(page, NODE, true, true);
  1717. if (!clear_page_dirty_for_io(page))
  1718. goto continue_unlock;
  1719. set_fsync_mark(page, 0);
  1720. set_dentry_mark(page, 0);
  1721. ret = __write_node_page(page, false, &submitted,
  1722. wbc, do_balance, io_type, NULL);
  1723. if (ret)
  1724. unlock_page(page);
  1725. else if (submitted)
  1726. nwritten++;
  1727. if (--wbc->nr_to_write == 0)
  1728. break;
  1729. }
  1730. pagevec_release(&pvec);
  1731. cond_resched();
  1732. if (wbc->nr_to_write == 0) {
  1733. step = 2;
  1734. break;
  1735. }
  1736. }
  1737. if (step < 2) {
  1738. if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
  1739. wbc->sync_mode == WB_SYNC_NONE && step == 1)
  1740. goto out;
  1741. step++;
  1742. goto next_step;
  1743. }
  1744. out:
  1745. if (nwritten)
  1746. f2fs_submit_merged_write(sbi, NODE);
  1747. if (unlikely(f2fs_cp_error(sbi)))
  1748. return -EIO;
  1749. return ret;
  1750. }
  1751. int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
  1752. unsigned int seq_id)
  1753. {
  1754. struct fsync_node_entry *fn;
  1755. struct page *page;
  1756. struct list_head *head = &sbi->fsync_node_list;
  1757. unsigned long flags;
  1758. unsigned int cur_seq_id = 0;
  1759. int ret2, ret = 0;
  1760. while (seq_id && cur_seq_id < seq_id) {
  1761. spin_lock_irqsave(&sbi->fsync_node_lock, flags);
  1762. if (list_empty(head)) {
  1763. spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
  1764. break;
  1765. }
  1766. fn = list_first_entry(head, struct fsync_node_entry, list);
  1767. if (fn->seq_id > seq_id) {
  1768. spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
  1769. break;
  1770. }
  1771. cur_seq_id = fn->seq_id;
  1772. page = fn->page;
  1773. get_page(page);
  1774. spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
  1775. f2fs_wait_on_page_writeback(page, NODE, true, false);
  1776. put_page(page);
  1777. if (ret)
  1778. break;
  1779. }
  1780. ret2 = filemap_check_errors(NODE_MAPPING(sbi));
  1781. if (!ret)
  1782. ret = ret2;
  1783. return ret;
  1784. }
  1785. static int f2fs_write_node_pages(struct address_space *mapping,
  1786. struct writeback_control *wbc)
  1787. {
  1788. struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
  1789. struct blk_plug plug;
  1790. long diff;
  1791. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  1792. goto skip_write;
  1793. /* balancing f2fs's metadata in background */
  1794. f2fs_balance_fs_bg(sbi, true);
  1795. /* collect a number of dirty node pages and write together */
  1796. if (wbc->sync_mode != WB_SYNC_ALL &&
  1797. get_pages(sbi, F2FS_DIRTY_NODES) <
  1798. nr_pages_to_skip(sbi, NODE))
  1799. goto skip_write;
  1800. if (wbc->sync_mode == WB_SYNC_ALL)
  1801. atomic_inc(&sbi->wb_sync_req[NODE]);
  1802. else if (atomic_read(&sbi->wb_sync_req[NODE])) {
  1803. /* to avoid potential deadlock */
  1804. if (current->plug)
  1805. blk_finish_plug(current->plug);
  1806. goto skip_write;
  1807. }
  1808. trace_f2fs_writepages(mapping->host, wbc, NODE);
  1809. diff = nr_pages_to_write(sbi, NODE, wbc);
  1810. blk_start_plug(&plug);
  1811. f2fs_sync_node_pages(sbi, wbc, true, FS_NODE_IO);
  1812. blk_finish_plug(&plug);
  1813. wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
  1814. if (wbc->sync_mode == WB_SYNC_ALL)
  1815. atomic_dec(&sbi->wb_sync_req[NODE]);
  1816. return 0;
  1817. skip_write:
  1818. wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
  1819. trace_f2fs_writepages(mapping->host, wbc, NODE);
  1820. return 0;
  1821. }
  1822. static bool f2fs_dirty_node_folio(struct address_space *mapping,
  1823. struct folio *folio)
  1824. {
  1825. trace_f2fs_set_page_dirty(&folio->page, NODE);
  1826. if (!folio_test_uptodate(folio))
  1827. folio_mark_uptodate(folio);
  1828. #ifdef CONFIG_F2FS_CHECK_FS
  1829. if (IS_INODE(&folio->page))
  1830. f2fs_inode_chksum_set(F2FS_M_SB(mapping), &folio->page);
  1831. #endif
  1832. if (filemap_dirty_folio(mapping, folio)) {
  1833. inc_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
  1834. set_page_private_reference(&folio->page);
  1835. return true;
  1836. }
  1837. return false;
  1838. }
  1839. /*
  1840. * Structure of the f2fs node operations
  1841. */
  1842. const struct address_space_operations f2fs_node_aops = {
  1843. .writepage = f2fs_write_node_page,
  1844. .writepages = f2fs_write_node_pages,
  1845. .dirty_folio = f2fs_dirty_node_folio,
  1846. .invalidate_folio = f2fs_invalidate_folio,
  1847. .release_folio = f2fs_release_folio,
  1848. .migrate_folio = filemap_migrate_folio,
  1849. };
  1850. static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
  1851. nid_t n)
  1852. {
  1853. return radix_tree_lookup(&nm_i->free_nid_root, n);
  1854. }
  1855. static int __insert_free_nid(struct f2fs_sb_info *sbi,
  1856. struct free_nid *i)
  1857. {
  1858. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1859. int err = radix_tree_insert(&nm_i->free_nid_root, i->nid, i);
  1860. if (err)
  1861. return err;
  1862. nm_i->nid_cnt[FREE_NID]++;
  1863. list_add_tail(&i->list, &nm_i->free_nid_list);
  1864. return 0;
  1865. }
  1866. static void __remove_free_nid(struct f2fs_sb_info *sbi,
  1867. struct free_nid *i, enum nid_state state)
  1868. {
  1869. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1870. f2fs_bug_on(sbi, state != i->state);
  1871. nm_i->nid_cnt[state]--;
  1872. if (state == FREE_NID)
  1873. list_del(&i->list);
  1874. radix_tree_delete(&nm_i->free_nid_root, i->nid);
  1875. }
  1876. static void __move_free_nid(struct f2fs_sb_info *sbi, struct free_nid *i,
  1877. enum nid_state org_state, enum nid_state dst_state)
  1878. {
  1879. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1880. f2fs_bug_on(sbi, org_state != i->state);
  1881. i->state = dst_state;
  1882. nm_i->nid_cnt[org_state]--;
  1883. nm_i->nid_cnt[dst_state]++;
  1884. switch (dst_state) {
  1885. case PREALLOC_NID:
  1886. list_del(&i->list);
  1887. break;
  1888. case FREE_NID:
  1889. list_add_tail(&i->list, &nm_i->free_nid_list);
  1890. break;
  1891. default:
  1892. BUG_ON(1);
  1893. }
  1894. }
  1895. bool f2fs_nat_bitmap_enabled(struct f2fs_sb_info *sbi)
  1896. {
  1897. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1898. unsigned int i;
  1899. bool ret = true;
  1900. f2fs_down_read(&nm_i->nat_tree_lock);
  1901. for (i = 0; i < nm_i->nat_blocks; i++) {
  1902. if (!test_bit_le(i, nm_i->nat_block_bitmap)) {
  1903. ret = false;
  1904. break;
  1905. }
  1906. }
  1907. f2fs_up_read(&nm_i->nat_tree_lock);
  1908. return ret;
  1909. }
  1910. static void update_free_nid_bitmap(struct f2fs_sb_info *sbi, nid_t nid,
  1911. bool set, bool build)
  1912. {
  1913. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1914. unsigned int nat_ofs = NAT_BLOCK_OFFSET(nid);
  1915. unsigned int nid_ofs = nid - START_NID(nid);
  1916. if (!test_bit_le(nat_ofs, nm_i->nat_block_bitmap))
  1917. return;
  1918. if (set) {
  1919. if (test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
  1920. return;
  1921. __set_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
  1922. nm_i->free_nid_count[nat_ofs]++;
  1923. } else {
  1924. if (!test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
  1925. return;
  1926. __clear_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
  1927. if (!build)
  1928. nm_i->free_nid_count[nat_ofs]--;
  1929. }
  1930. }
  1931. /* return if the nid is recognized as free */
  1932. static bool add_free_nid(struct f2fs_sb_info *sbi,
  1933. nid_t nid, bool build, bool update)
  1934. {
  1935. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1936. struct free_nid *i, *e;
  1937. struct nat_entry *ne;
  1938. int err = -EINVAL;
  1939. bool ret = false;
  1940. /* 0 nid should not be used */
  1941. if (unlikely(nid == 0))
  1942. return false;
  1943. if (unlikely(f2fs_check_nid_range(sbi, nid)))
  1944. return false;
  1945. i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS, true, NULL);
  1946. i->nid = nid;
  1947. i->state = FREE_NID;
  1948. radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
  1949. spin_lock(&nm_i->nid_list_lock);
  1950. if (build) {
  1951. /*
  1952. * Thread A Thread B
  1953. * - f2fs_create
  1954. * - f2fs_new_inode
  1955. * - f2fs_alloc_nid
  1956. * - __insert_nid_to_list(PREALLOC_NID)
  1957. * - f2fs_balance_fs_bg
  1958. * - f2fs_build_free_nids
  1959. * - __f2fs_build_free_nids
  1960. * - scan_nat_page
  1961. * - add_free_nid
  1962. * - __lookup_nat_cache
  1963. * - f2fs_add_link
  1964. * - f2fs_init_inode_metadata
  1965. * - f2fs_new_inode_page
  1966. * - f2fs_new_node_page
  1967. * - set_node_addr
  1968. * - f2fs_alloc_nid_done
  1969. * - __remove_nid_from_list(PREALLOC_NID)
  1970. * - __insert_nid_to_list(FREE_NID)
  1971. */
  1972. ne = __lookup_nat_cache(nm_i, nid);
  1973. if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) ||
  1974. nat_get_blkaddr(ne) != NULL_ADDR))
  1975. goto err_out;
  1976. e = __lookup_free_nid_list(nm_i, nid);
  1977. if (e) {
  1978. if (e->state == FREE_NID)
  1979. ret = true;
  1980. goto err_out;
  1981. }
  1982. }
  1983. ret = true;
  1984. err = __insert_free_nid(sbi, i);
  1985. err_out:
  1986. if (update) {
  1987. update_free_nid_bitmap(sbi, nid, ret, build);
  1988. if (!build)
  1989. nm_i->available_nids++;
  1990. }
  1991. spin_unlock(&nm_i->nid_list_lock);
  1992. radix_tree_preload_end();
  1993. if (err)
  1994. kmem_cache_free(free_nid_slab, i);
  1995. return ret;
  1996. }
  1997. static void remove_free_nid(struct f2fs_sb_info *sbi, nid_t nid)
  1998. {
  1999. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2000. struct free_nid *i;
  2001. bool need_free = false;
  2002. spin_lock(&nm_i->nid_list_lock);
  2003. i = __lookup_free_nid_list(nm_i, nid);
  2004. if (i && i->state == FREE_NID) {
  2005. __remove_free_nid(sbi, i, FREE_NID);
  2006. need_free = true;
  2007. }
  2008. spin_unlock(&nm_i->nid_list_lock);
  2009. if (need_free)
  2010. kmem_cache_free(free_nid_slab, i);
  2011. }
  2012. static int scan_nat_page(struct f2fs_sb_info *sbi,
  2013. struct page *nat_page, nid_t start_nid)
  2014. {
  2015. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2016. struct f2fs_nat_block *nat_blk = page_address(nat_page);
  2017. block_t blk_addr;
  2018. unsigned int nat_ofs = NAT_BLOCK_OFFSET(start_nid);
  2019. int i;
  2020. __set_bit_le(nat_ofs, nm_i->nat_block_bitmap);
  2021. i = start_nid % NAT_ENTRY_PER_BLOCK;
  2022. for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
  2023. if (unlikely(start_nid >= nm_i->max_nid))
  2024. break;
  2025. blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
  2026. if (blk_addr == NEW_ADDR)
  2027. return -EINVAL;
  2028. if (blk_addr == NULL_ADDR) {
  2029. add_free_nid(sbi, start_nid, true, true);
  2030. } else {
  2031. spin_lock(&NM_I(sbi)->nid_list_lock);
  2032. update_free_nid_bitmap(sbi, start_nid, false, true);
  2033. spin_unlock(&NM_I(sbi)->nid_list_lock);
  2034. }
  2035. }
  2036. return 0;
  2037. }
  2038. static void scan_curseg_cache(struct f2fs_sb_info *sbi)
  2039. {
  2040. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  2041. struct f2fs_journal *journal = curseg->journal;
  2042. int i;
  2043. down_read(&curseg->journal_rwsem);
  2044. for (i = 0; i < nats_in_cursum(journal); i++) {
  2045. block_t addr;
  2046. nid_t nid;
  2047. addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
  2048. nid = le32_to_cpu(nid_in_journal(journal, i));
  2049. if (addr == NULL_ADDR)
  2050. add_free_nid(sbi, nid, true, false);
  2051. else
  2052. remove_free_nid(sbi, nid);
  2053. }
  2054. up_read(&curseg->journal_rwsem);
  2055. }
  2056. static void scan_free_nid_bits(struct f2fs_sb_info *sbi)
  2057. {
  2058. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2059. unsigned int i, idx;
  2060. nid_t nid;
  2061. f2fs_down_read(&nm_i->nat_tree_lock);
  2062. for (i = 0; i < nm_i->nat_blocks; i++) {
  2063. if (!test_bit_le(i, nm_i->nat_block_bitmap))
  2064. continue;
  2065. if (!nm_i->free_nid_count[i])
  2066. continue;
  2067. for (idx = 0; idx < NAT_ENTRY_PER_BLOCK; idx++) {
  2068. idx = find_next_bit_le(nm_i->free_nid_bitmap[i],
  2069. NAT_ENTRY_PER_BLOCK, idx);
  2070. if (idx >= NAT_ENTRY_PER_BLOCK)
  2071. break;
  2072. nid = i * NAT_ENTRY_PER_BLOCK + idx;
  2073. add_free_nid(sbi, nid, true, false);
  2074. if (nm_i->nid_cnt[FREE_NID] >= MAX_FREE_NIDS)
  2075. goto out;
  2076. }
  2077. }
  2078. out:
  2079. scan_curseg_cache(sbi);
  2080. f2fs_up_read(&nm_i->nat_tree_lock);
  2081. }
  2082. static int __f2fs_build_free_nids(struct f2fs_sb_info *sbi,
  2083. bool sync, bool mount)
  2084. {
  2085. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2086. int i = 0, ret;
  2087. nid_t nid = nm_i->next_scan_nid;
  2088. if (unlikely(nid >= nm_i->max_nid))
  2089. nid = 0;
  2090. if (unlikely(nid % NAT_ENTRY_PER_BLOCK))
  2091. nid = NAT_BLOCK_OFFSET(nid) * NAT_ENTRY_PER_BLOCK;
  2092. /* Enough entries */
  2093. if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
  2094. return 0;
  2095. if (!sync && !f2fs_available_free_memory(sbi, FREE_NIDS))
  2096. return 0;
  2097. if (!mount) {
  2098. /* try to find free nids in free_nid_bitmap */
  2099. scan_free_nid_bits(sbi);
  2100. if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
  2101. return 0;
  2102. }
  2103. /* readahead nat pages to be scanned */
  2104. f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES,
  2105. META_NAT, true);
  2106. f2fs_down_read(&nm_i->nat_tree_lock);
  2107. while (1) {
  2108. if (!test_bit_le(NAT_BLOCK_OFFSET(nid),
  2109. nm_i->nat_block_bitmap)) {
  2110. struct page *page = get_current_nat_page(sbi, nid);
  2111. if (IS_ERR(page)) {
  2112. ret = PTR_ERR(page);
  2113. } else {
  2114. ret = scan_nat_page(sbi, page, nid);
  2115. f2fs_put_page(page, 1);
  2116. }
  2117. if (ret) {
  2118. f2fs_up_read(&nm_i->nat_tree_lock);
  2119. f2fs_err(sbi, "NAT is corrupt, run fsck to fix it");
  2120. return ret;
  2121. }
  2122. }
  2123. nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
  2124. if (unlikely(nid >= nm_i->max_nid))
  2125. nid = 0;
  2126. if (++i >= FREE_NID_PAGES)
  2127. break;
  2128. }
  2129. /* go to the next free nat pages to find free nids abundantly */
  2130. nm_i->next_scan_nid = nid;
  2131. /* find free nids from current sum_pages */
  2132. scan_curseg_cache(sbi);
  2133. f2fs_up_read(&nm_i->nat_tree_lock);
  2134. f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid),
  2135. nm_i->ra_nid_pages, META_NAT, false);
  2136. return 0;
  2137. }
  2138. int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount)
  2139. {
  2140. int ret;
  2141. mutex_lock(&NM_I(sbi)->build_lock);
  2142. ret = __f2fs_build_free_nids(sbi, sync, mount);
  2143. mutex_unlock(&NM_I(sbi)->build_lock);
  2144. return ret;
  2145. }
  2146. /*
  2147. * If this function returns success, caller can obtain a new nid
  2148. * from second parameter of this function.
  2149. * The returned nid could be used ino as well as nid when inode is created.
  2150. */
  2151. bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
  2152. {
  2153. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2154. struct free_nid *i = NULL;
  2155. retry:
  2156. if (time_to_inject(sbi, FAULT_ALLOC_NID))
  2157. return false;
  2158. spin_lock(&nm_i->nid_list_lock);
  2159. if (unlikely(nm_i->available_nids == 0)) {
  2160. spin_unlock(&nm_i->nid_list_lock);
  2161. return false;
  2162. }
  2163. /* We should not use stale free nids created by f2fs_build_free_nids */
  2164. if (nm_i->nid_cnt[FREE_NID] && !on_f2fs_build_free_nids(nm_i)) {
  2165. f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
  2166. i = list_first_entry(&nm_i->free_nid_list,
  2167. struct free_nid, list);
  2168. *nid = i->nid;
  2169. __move_free_nid(sbi, i, FREE_NID, PREALLOC_NID);
  2170. nm_i->available_nids--;
  2171. update_free_nid_bitmap(sbi, *nid, false, false);
  2172. spin_unlock(&nm_i->nid_list_lock);
  2173. return true;
  2174. }
  2175. spin_unlock(&nm_i->nid_list_lock);
  2176. /* Let's scan nat pages and its caches to get free nids */
  2177. if (!f2fs_build_free_nids(sbi, true, false))
  2178. goto retry;
  2179. return false;
  2180. }
  2181. /*
  2182. * f2fs_alloc_nid() should be called prior to this function.
  2183. */
  2184. void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
  2185. {
  2186. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2187. struct free_nid *i;
  2188. spin_lock(&nm_i->nid_list_lock);
  2189. i = __lookup_free_nid_list(nm_i, nid);
  2190. f2fs_bug_on(sbi, !i);
  2191. __remove_free_nid(sbi, i, PREALLOC_NID);
  2192. spin_unlock(&nm_i->nid_list_lock);
  2193. kmem_cache_free(free_nid_slab, i);
  2194. }
  2195. /*
  2196. * f2fs_alloc_nid() should be called prior to this function.
  2197. */
  2198. void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
  2199. {
  2200. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2201. struct free_nid *i;
  2202. bool need_free = false;
  2203. if (!nid)
  2204. return;
  2205. spin_lock(&nm_i->nid_list_lock);
  2206. i = __lookup_free_nid_list(nm_i, nid);
  2207. f2fs_bug_on(sbi, !i);
  2208. if (!f2fs_available_free_memory(sbi, FREE_NIDS)) {
  2209. __remove_free_nid(sbi, i, PREALLOC_NID);
  2210. need_free = true;
  2211. } else {
  2212. __move_free_nid(sbi, i, PREALLOC_NID, FREE_NID);
  2213. }
  2214. nm_i->available_nids++;
  2215. update_free_nid_bitmap(sbi, nid, true, false);
  2216. spin_unlock(&nm_i->nid_list_lock);
  2217. if (need_free)
  2218. kmem_cache_free(free_nid_slab, i);
  2219. }
  2220. int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
  2221. {
  2222. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2223. int nr = nr_shrink;
  2224. if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
  2225. return 0;
  2226. if (!mutex_trylock(&nm_i->build_lock))
  2227. return 0;
  2228. while (nr_shrink && nm_i->nid_cnt[FREE_NID] > MAX_FREE_NIDS) {
  2229. struct free_nid *i, *next;
  2230. unsigned int batch = SHRINK_NID_BATCH_SIZE;
  2231. spin_lock(&nm_i->nid_list_lock);
  2232. list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
  2233. if (!nr_shrink || !batch ||
  2234. nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
  2235. break;
  2236. __remove_free_nid(sbi, i, FREE_NID);
  2237. kmem_cache_free(free_nid_slab, i);
  2238. nr_shrink--;
  2239. batch--;
  2240. }
  2241. spin_unlock(&nm_i->nid_list_lock);
  2242. }
  2243. mutex_unlock(&nm_i->build_lock);
  2244. return nr - nr_shrink;
  2245. }
  2246. int f2fs_recover_inline_xattr(struct inode *inode, struct page *page)
  2247. {
  2248. void *src_addr, *dst_addr;
  2249. size_t inline_size;
  2250. struct page *ipage;
  2251. struct f2fs_inode *ri;
  2252. ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
  2253. if (IS_ERR(ipage))
  2254. return PTR_ERR(ipage);
  2255. ri = F2FS_INODE(page);
  2256. if (ri->i_inline & F2FS_INLINE_XATTR) {
  2257. if (!f2fs_has_inline_xattr(inode)) {
  2258. set_inode_flag(inode, FI_INLINE_XATTR);
  2259. stat_inc_inline_xattr(inode);
  2260. }
  2261. } else {
  2262. if (f2fs_has_inline_xattr(inode)) {
  2263. stat_dec_inline_xattr(inode);
  2264. clear_inode_flag(inode, FI_INLINE_XATTR);
  2265. }
  2266. goto update_inode;
  2267. }
  2268. dst_addr = inline_xattr_addr(inode, ipage);
  2269. src_addr = inline_xattr_addr(inode, page);
  2270. inline_size = inline_xattr_size(inode);
  2271. f2fs_wait_on_page_writeback(ipage, NODE, true, true);
  2272. memcpy(dst_addr, src_addr, inline_size);
  2273. update_inode:
  2274. f2fs_update_inode(inode, ipage);
  2275. f2fs_put_page(ipage, 1);
  2276. return 0;
  2277. }
  2278. int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
  2279. {
  2280. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  2281. nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
  2282. nid_t new_xnid;
  2283. struct dnode_of_data dn;
  2284. struct node_info ni;
  2285. struct page *xpage;
  2286. int err;
  2287. if (!prev_xnid)
  2288. goto recover_xnid;
  2289. /* 1: invalidate the previous xattr nid */
  2290. err = f2fs_get_node_info(sbi, prev_xnid, &ni, false);
  2291. if (err)
  2292. return err;
  2293. f2fs_invalidate_blocks(sbi, ni.blk_addr);
  2294. dec_valid_node_count(sbi, inode, false);
  2295. set_node_addr(sbi, &ni, NULL_ADDR, false);
  2296. recover_xnid:
  2297. /* 2: update xattr nid in inode */
  2298. if (!f2fs_alloc_nid(sbi, &new_xnid))
  2299. return -ENOSPC;
  2300. set_new_dnode(&dn, inode, NULL, NULL, new_xnid);
  2301. xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
  2302. if (IS_ERR(xpage)) {
  2303. f2fs_alloc_nid_failed(sbi, new_xnid);
  2304. return PTR_ERR(xpage);
  2305. }
  2306. f2fs_alloc_nid_done(sbi, new_xnid);
  2307. f2fs_update_inode_page(inode);
  2308. /* 3: update and set xattr node page dirty */
  2309. if (page)
  2310. memcpy(F2FS_NODE(xpage), F2FS_NODE(page),
  2311. VALID_XATTR_BLOCK_SIZE);
  2312. set_page_dirty(xpage);
  2313. f2fs_put_page(xpage, 1);
  2314. return 0;
  2315. }
  2316. int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
  2317. {
  2318. struct f2fs_inode *src, *dst;
  2319. nid_t ino = ino_of_node(page);
  2320. struct node_info old_ni, new_ni;
  2321. struct page *ipage;
  2322. int err;
  2323. err = f2fs_get_node_info(sbi, ino, &old_ni, false);
  2324. if (err)
  2325. return err;
  2326. if (unlikely(old_ni.blk_addr != NULL_ADDR))
  2327. return -EINVAL;
  2328. retry:
  2329. ipage = f2fs_grab_cache_page(NODE_MAPPING(sbi), ino, false);
  2330. if (!ipage) {
  2331. memalloc_retry_wait(GFP_NOFS);
  2332. goto retry;
  2333. }
  2334. /* Should not use this inode from free nid list */
  2335. remove_free_nid(sbi, ino);
  2336. if (!PageUptodate(ipage))
  2337. SetPageUptodate(ipage);
  2338. fill_node_footer(ipage, ino, ino, 0, true);
  2339. set_cold_node(ipage, false);
  2340. src = F2FS_INODE(page);
  2341. dst = F2FS_INODE(ipage);
  2342. memcpy(dst, src, offsetof(struct f2fs_inode, i_ext));
  2343. dst->i_size = 0;
  2344. dst->i_blocks = cpu_to_le64(1);
  2345. dst->i_links = cpu_to_le32(1);
  2346. dst->i_xattr_nid = 0;
  2347. dst->i_inline = src->i_inline & (F2FS_INLINE_XATTR | F2FS_EXTRA_ATTR);
  2348. if (dst->i_inline & F2FS_EXTRA_ATTR) {
  2349. dst->i_extra_isize = src->i_extra_isize;
  2350. if (f2fs_sb_has_flexible_inline_xattr(sbi) &&
  2351. F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
  2352. i_inline_xattr_size))
  2353. dst->i_inline_xattr_size = src->i_inline_xattr_size;
  2354. if (f2fs_sb_has_project_quota(sbi) &&
  2355. F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
  2356. i_projid))
  2357. dst->i_projid = src->i_projid;
  2358. if (f2fs_sb_has_inode_crtime(sbi) &&
  2359. F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
  2360. i_crtime_nsec)) {
  2361. dst->i_crtime = src->i_crtime;
  2362. dst->i_crtime_nsec = src->i_crtime_nsec;
  2363. }
  2364. }
  2365. new_ni = old_ni;
  2366. new_ni.ino = ino;
  2367. if (unlikely(inc_valid_node_count(sbi, NULL, true)))
  2368. WARN_ON(1);
  2369. set_node_addr(sbi, &new_ni, NEW_ADDR, false);
  2370. inc_valid_inode_count(sbi);
  2371. set_page_dirty(ipage);
  2372. f2fs_put_page(ipage, 1);
  2373. return 0;
  2374. }
  2375. int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
  2376. unsigned int segno, struct f2fs_summary_block *sum)
  2377. {
  2378. struct f2fs_node *rn;
  2379. struct f2fs_summary *sum_entry;
  2380. block_t addr;
  2381. int i, idx, last_offset, nrpages;
  2382. /* scan the node segment */
  2383. last_offset = sbi->blocks_per_seg;
  2384. addr = START_BLOCK(sbi, segno);
  2385. sum_entry = &sum->entries[0];
  2386. for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
  2387. nrpages = bio_max_segs(last_offset - i);
  2388. /* readahead node pages */
  2389. f2fs_ra_meta_pages(sbi, addr, nrpages, META_POR, true);
  2390. for (idx = addr; idx < addr + nrpages; idx++) {
  2391. struct page *page = f2fs_get_tmp_page(sbi, idx);
  2392. if (IS_ERR(page))
  2393. return PTR_ERR(page);
  2394. rn = F2FS_NODE(page);
  2395. sum_entry->nid = rn->footer.nid;
  2396. sum_entry->version = 0;
  2397. sum_entry->ofs_in_node = 0;
  2398. sum_entry++;
  2399. f2fs_put_page(page, 1);
  2400. }
  2401. invalidate_mapping_pages(META_MAPPING(sbi), addr,
  2402. addr + nrpages);
  2403. }
  2404. return 0;
  2405. }
  2406. static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
  2407. {
  2408. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2409. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  2410. struct f2fs_journal *journal = curseg->journal;
  2411. int i;
  2412. down_write(&curseg->journal_rwsem);
  2413. for (i = 0; i < nats_in_cursum(journal); i++) {
  2414. struct nat_entry *ne;
  2415. struct f2fs_nat_entry raw_ne;
  2416. nid_t nid = le32_to_cpu(nid_in_journal(journal, i));
  2417. if (f2fs_check_nid_range(sbi, nid))
  2418. continue;
  2419. raw_ne = nat_in_journal(journal, i);
  2420. ne = __lookup_nat_cache(nm_i, nid);
  2421. if (!ne) {
  2422. ne = __alloc_nat_entry(sbi, nid, true);
  2423. __init_nat_entry(nm_i, ne, &raw_ne, true);
  2424. }
  2425. /*
  2426. * if a free nat in journal has not been used after last
  2427. * checkpoint, we should remove it from available nids,
  2428. * since later we will add it again.
  2429. */
  2430. if (!get_nat_flag(ne, IS_DIRTY) &&
  2431. le32_to_cpu(raw_ne.block_addr) == NULL_ADDR) {
  2432. spin_lock(&nm_i->nid_list_lock);
  2433. nm_i->available_nids--;
  2434. spin_unlock(&nm_i->nid_list_lock);
  2435. }
  2436. __set_nat_cache_dirty(nm_i, ne);
  2437. }
  2438. update_nats_in_cursum(journal, -i);
  2439. up_write(&curseg->journal_rwsem);
  2440. }
  2441. static void __adjust_nat_entry_set(struct nat_entry_set *nes,
  2442. struct list_head *head, int max)
  2443. {
  2444. struct nat_entry_set *cur;
  2445. if (nes->entry_cnt >= max)
  2446. goto add_out;
  2447. list_for_each_entry(cur, head, set_list) {
  2448. if (cur->entry_cnt >= nes->entry_cnt) {
  2449. list_add(&nes->set_list, cur->set_list.prev);
  2450. return;
  2451. }
  2452. }
  2453. add_out:
  2454. list_add_tail(&nes->set_list, head);
  2455. }
  2456. static void __update_nat_bits(struct f2fs_nm_info *nm_i, unsigned int nat_ofs,
  2457. unsigned int valid)
  2458. {
  2459. if (valid == 0) {
  2460. __set_bit_le(nat_ofs, nm_i->empty_nat_bits);
  2461. __clear_bit_le(nat_ofs, nm_i->full_nat_bits);
  2462. return;
  2463. }
  2464. __clear_bit_le(nat_ofs, nm_i->empty_nat_bits);
  2465. if (valid == NAT_ENTRY_PER_BLOCK)
  2466. __set_bit_le(nat_ofs, nm_i->full_nat_bits);
  2467. else
  2468. __clear_bit_le(nat_ofs, nm_i->full_nat_bits);
  2469. }
  2470. static void update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid,
  2471. struct page *page)
  2472. {
  2473. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2474. unsigned int nat_index = start_nid / NAT_ENTRY_PER_BLOCK;
  2475. struct f2fs_nat_block *nat_blk = page_address(page);
  2476. int valid = 0;
  2477. int i = 0;
  2478. if (!is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG))
  2479. return;
  2480. if (nat_index == 0) {
  2481. valid = 1;
  2482. i = 1;
  2483. }
  2484. for (; i < NAT_ENTRY_PER_BLOCK; i++) {
  2485. if (le32_to_cpu(nat_blk->entries[i].block_addr) != NULL_ADDR)
  2486. valid++;
  2487. }
  2488. __update_nat_bits(nm_i, nat_index, valid);
  2489. }
  2490. void f2fs_enable_nat_bits(struct f2fs_sb_info *sbi)
  2491. {
  2492. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2493. unsigned int nat_ofs;
  2494. f2fs_down_read(&nm_i->nat_tree_lock);
  2495. for (nat_ofs = 0; nat_ofs < nm_i->nat_blocks; nat_ofs++) {
  2496. unsigned int valid = 0, nid_ofs = 0;
  2497. /* handle nid zero due to it should never be used */
  2498. if (unlikely(nat_ofs == 0)) {
  2499. valid = 1;
  2500. nid_ofs = 1;
  2501. }
  2502. for (; nid_ofs < NAT_ENTRY_PER_BLOCK; nid_ofs++) {
  2503. if (!test_bit_le(nid_ofs,
  2504. nm_i->free_nid_bitmap[nat_ofs]))
  2505. valid++;
  2506. }
  2507. __update_nat_bits(nm_i, nat_ofs, valid);
  2508. }
  2509. f2fs_up_read(&nm_i->nat_tree_lock);
  2510. }
  2511. static int __flush_nat_entry_set(struct f2fs_sb_info *sbi,
  2512. struct nat_entry_set *set, struct cp_control *cpc)
  2513. {
  2514. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  2515. struct f2fs_journal *journal = curseg->journal;
  2516. nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
  2517. bool to_journal = true;
  2518. struct f2fs_nat_block *nat_blk;
  2519. struct nat_entry *ne, *cur;
  2520. struct page *page = NULL;
  2521. /*
  2522. * there are two steps to flush nat entries:
  2523. * #1, flush nat entries to journal in current hot data summary block.
  2524. * #2, flush nat entries to nat page.
  2525. */
  2526. if ((cpc->reason & CP_UMOUNT) ||
  2527. !__has_cursum_space(journal, set->entry_cnt, NAT_JOURNAL))
  2528. to_journal = false;
  2529. if (to_journal) {
  2530. down_write(&curseg->journal_rwsem);
  2531. } else {
  2532. page = get_next_nat_page(sbi, start_nid);
  2533. if (IS_ERR(page))
  2534. return PTR_ERR(page);
  2535. nat_blk = page_address(page);
  2536. f2fs_bug_on(sbi, !nat_blk);
  2537. }
  2538. /* flush dirty nats in nat entry set */
  2539. list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
  2540. struct f2fs_nat_entry *raw_ne;
  2541. nid_t nid = nat_get_nid(ne);
  2542. int offset;
  2543. f2fs_bug_on(sbi, nat_get_blkaddr(ne) == NEW_ADDR);
  2544. if (to_journal) {
  2545. offset = f2fs_lookup_journal_in_cursum(journal,
  2546. NAT_JOURNAL, nid, 1);
  2547. f2fs_bug_on(sbi, offset < 0);
  2548. raw_ne = &nat_in_journal(journal, offset);
  2549. nid_in_journal(journal, offset) = cpu_to_le32(nid);
  2550. } else {
  2551. raw_ne = &nat_blk->entries[nid - start_nid];
  2552. }
  2553. raw_nat_from_node_info(raw_ne, &ne->ni);
  2554. nat_reset_flag(ne);
  2555. __clear_nat_cache_dirty(NM_I(sbi), set, ne);
  2556. if (nat_get_blkaddr(ne) == NULL_ADDR) {
  2557. add_free_nid(sbi, nid, false, true);
  2558. } else {
  2559. spin_lock(&NM_I(sbi)->nid_list_lock);
  2560. update_free_nid_bitmap(sbi, nid, false, false);
  2561. spin_unlock(&NM_I(sbi)->nid_list_lock);
  2562. }
  2563. }
  2564. if (to_journal) {
  2565. up_write(&curseg->journal_rwsem);
  2566. } else {
  2567. update_nat_bits(sbi, start_nid, page);
  2568. f2fs_put_page(page, 1);
  2569. }
  2570. /* Allow dirty nats by node block allocation in write_begin */
  2571. if (!set->entry_cnt) {
  2572. radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
  2573. kmem_cache_free(nat_entry_set_slab, set);
  2574. }
  2575. return 0;
  2576. }
  2577. /*
  2578. * This function is called during the checkpointing process.
  2579. */
  2580. int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  2581. {
  2582. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2583. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  2584. struct f2fs_journal *journal = curseg->journal;
  2585. struct nat_entry_set *setvec[SETVEC_SIZE];
  2586. struct nat_entry_set *set, *tmp;
  2587. unsigned int found;
  2588. nid_t set_idx = 0;
  2589. LIST_HEAD(sets);
  2590. int err = 0;
  2591. /*
  2592. * during unmount, let's flush nat_bits before checking
  2593. * nat_cnt[DIRTY_NAT].
  2594. */
  2595. if (cpc->reason & CP_UMOUNT) {
  2596. f2fs_down_write(&nm_i->nat_tree_lock);
  2597. remove_nats_in_journal(sbi);
  2598. f2fs_up_write(&nm_i->nat_tree_lock);
  2599. }
  2600. if (!nm_i->nat_cnt[DIRTY_NAT])
  2601. return 0;
  2602. f2fs_down_write(&nm_i->nat_tree_lock);
  2603. /*
  2604. * if there are no enough space in journal to store dirty nat
  2605. * entries, remove all entries from journal and merge them
  2606. * into nat entry set.
  2607. */
  2608. if (cpc->reason & CP_UMOUNT ||
  2609. !__has_cursum_space(journal,
  2610. nm_i->nat_cnt[DIRTY_NAT], NAT_JOURNAL))
  2611. remove_nats_in_journal(sbi);
  2612. while ((found = __gang_lookup_nat_set(nm_i,
  2613. set_idx, SETVEC_SIZE, setvec))) {
  2614. unsigned idx;
  2615. set_idx = setvec[found - 1]->set + 1;
  2616. for (idx = 0; idx < found; idx++)
  2617. __adjust_nat_entry_set(setvec[idx], &sets,
  2618. MAX_NAT_JENTRIES(journal));
  2619. }
  2620. /* flush dirty nats in nat entry set */
  2621. list_for_each_entry_safe(set, tmp, &sets, set_list) {
  2622. err = __flush_nat_entry_set(sbi, set, cpc);
  2623. if (err)
  2624. break;
  2625. }
  2626. f2fs_up_write(&nm_i->nat_tree_lock);
  2627. /* Allow dirty nats by node block allocation in write_begin */
  2628. return err;
  2629. }
  2630. static int __get_nat_bitmaps(struct f2fs_sb_info *sbi)
  2631. {
  2632. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  2633. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2634. unsigned int nat_bits_bytes = nm_i->nat_blocks / BITS_PER_BYTE;
  2635. unsigned int i;
  2636. __u64 cp_ver = cur_cp_version(ckpt);
  2637. block_t nat_bits_addr;
  2638. nm_i->nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
  2639. nm_i->nat_bits = f2fs_kvzalloc(sbi,
  2640. nm_i->nat_bits_blocks << F2FS_BLKSIZE_BITS, GFP_KERNEL);
  2641. if (!nm_i->nat_bits)
  2642. return -ENOMEM;
  2643. nm_i->full_nat_bits = nm_i->nat_bits + 8;
  2644. nm_i->empty_nat_bits = nm_i->full_nat_bits + nat_bits_bytes;
  2645. if (!is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG))
  2646. return 0;
  2647. nat_bits_addr = __start_cp_addr(sbi) + sbi->blocks_per_seg -
  2648. nm_i->nat_bits_blocks;
  2649. for (i = 0; i < nm_i->nat_bits_blocks; i++) {
  2650. struct page *page;
  2651. page = f2fs_get_meta_page(sbi, nat_bits_addr++);
  2652. if (IS_ERR(page))
  2653. return PTR_ERR(page);
  2654. memcpy(nm_i->nat_bits + (i << F2FS_BLKSIZE_BITS),
  2655. page_address(page), F2FS_BLKSIZE);
  2656. f2fs_put_page(page, 1);
  2657. }
  2658. cp_ver |= (cur_cp_crc(ckpt) << 32);
  2659. if (cpu_to_le64(cp_ver) != *(__le64 *)nm_i->nat_bits) {
  2660. clear_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
  2661. f2fs_notice(sbi, "Disable nat_bits due to incorrect cp_ver (%llu, %llu)",
  2662. cp_ver, le64_to_cpu(*(__le64 *)nm_i->nat_bits));
  2663. return 0;
  2664. }
  2665. f2fs_notice(sbi, "Found nat_bits in checkpoint");
  2666. return 0;
  2667. }
  2668. static inline void load_free_nid_bitmap(struct f2fs_sb_info *sbi)
  2669. {
  2670. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2671. unsigned int i = 0;
  2672. nid_t nid, last_nid;
  2673. if (!is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG))
  2674. return;
  2675. for (i = 0; i < nm_i->nat_blocks; i++) {
  2676. i = find_next_bit_le(nm_i->empty_nat_bits, nm_i->nat_blocks, i);
  2677. if (i >= nm_i->nat_blocks)
  2678. break;
  2679. __set_bit_le(i, nm_i->nat_block_bitmap);
  2680. nid = i * NAT_ENTRY_PER_BLOCK;
  2681. last_nid = nid + NAT_ENTRY_PER_BLOCK;
  2682. spin_lock(&NM_I(sbi)->nid_list_lock);
  2683. for (; nid < last_nid; nid++)
  2684. update_free_nid_bitmap(sbi, nid, true, true);
  2685. spin_unlock(&NM_I(sbi)->nid_list_lock);
  2686. }
  2687. for (i = 0; i < nm_i->nat_blocks; i++) {
  2688. i = find_next_bit_le(nm_i->full_nat_bits, nm_i->nat_blocks, i);
  2689. if (i >= nm_i->nat_blocks)
  2690. break;
  2691. __set_bit_le(i, nm_i->nat_block_bitmap);
  2692. }
  2693. }
  2694. static int init_node_manager(struct f2fs_sb_info *sbi)
  2695. {
  2696. struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
  2697. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2698. unsigned char *version_bitmap;
  2699. unsigned int nat_segs;
  2700. int err;
  2701. nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
  2702. /* segment_count_nat includes pair segment so divide to 2. */
  2703. nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
  2704. nm_i->nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
  2705. nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks;
  2706. /* not used nids: 0, node, meta, (and root counted as valid node) */
  2707. nm_i->available_nids = nm_i->max_nid - sbi->total_valid_node_count -
  2708. F2FS_RESERVED_NODE_NUM;
  2709. nm_i->nid_cnt[FREE_NID] = 0;
  2710. nm_i->nid_cnt[PREALLOC_NID] = 0;
  2711. nm_i->ram_thresh = DEF_RAM_THRESHOLD;
  2712. nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
  2713. nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD;
  2714. nm_i->max_rf_node_blocks = DEF_RF_NODE_BLOCKS;
  2715. INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
  2716. INIT_LIST_HEAD(&nm_i->free_nid_list);
  2717. INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
  2718. INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
  2719. INIT_LIST_HEAD(&nm_i->nat_entries);
  2720. spin_lock_init(&nm_i->nat_list_lock);
  2721. mutex_init(&nm_i->build_lock);
  2722. spin_lock_init(&nm_i->nid_list_lock);
  2723. init_f2fs_rwsem(&nm_i->nat_tree_lock);
  2724. nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
  2725. nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
  2726. version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
  2727. nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
  2728. GFP_KERNEL);
  2729. if (!nm_i->nat_bitmap)
  2730. return -ENOMEM;
  2731. err = __get_nat_bitmaps(sbi);
  2732. if (err)
  2733. return err;
  2734. #ifdef CONFIG_F2FS_CHECK_FS
  2735. nm_i->nat_bitmap_mir = kmemdup(version_bitmap, nm_i->bitmap_size,
  2736. GFP_KERNEL);
  2737. if (!nm_i->nat_bitmap_mir)
  2738. return -ENOMEM;
  2739. #endif
  2740. return 0;
  2741. }
  2742. static int init_free_nid_cache(struct f2fs_sb_info *sbi)
  2743. {
  2744. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2745. int i;
  2746. nm_i->free_nid_bitmap =
  2747. f2fs_kvzalloc(sbi, array_size(sizeof(unsigned char *),
  2748. nm_i->nat_blocks),
  2749. GFP_KERNEL);
  2750. if (!nm_i->free_nid_bitmap)
  2751. return -ENOMEM;
  2752. for (i = 0; i < nm_i->nat_blocks; i++) {
  2753. nm_i->free_nid_bitmap[i] = f2fs_kvzalloc(sbi,
  2754. f2fs_bitmap_size(NAT_ENTRY_PER_BLOCK), GFP_KERNEL);
  2755. if (!nm_i->free_nid_bitmap[i])
  2756. return -ENOMEM;
  2757. }
  2758. nm_i->nat_block_bitmap = f2fs_kvzalloc(sbi, nm_i->nat_blocks / 8,
  2759. GFP_KERNEL);
  2760. if (!nm_i->nat_block_bitmap)
  2761. return -ENOMEM;
  2762. nm_i->free_nid_count =
  2763. f2fs_kvzalloc(sbi, array_size(sizeof(unsigned short),
  2764. nm_i->nat_blocks),
  2765. GFP_KERNEL);
  2766. if (!nm_i->free_nid_count)
  2767. return -ENOMEM;
  2768. return 0;
  2769. }
  2770. int f2fs_build_node_manager(struct f2fs_sb_info *sbi)
  2771. {
  2772. int err;
  2773. sbi->nm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_nm_info),
  2774. GFP_KERNEL);
  2775. if (!sbi->nm_info)
  2776. return -ENOMEM;
  2777. err = init_node_manager(sbi);
  2778. if (err)
  2779. return err;
  2780. err = init_free_nid_cache(sbi);
  2781. if (err)
  2782. return err;
  2783. /* load free nid status from nat_bits table */
  2784. load_free_nid_bitmap(sbi);
  2785. return f2fs_build_free_nids(sbi, true, true);
  2786. }
  2787. void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi)
  2788. {
  2789. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2790. struct free_nid *i, *next_i;
  2791. struct nat_entry *natvec[NATVEC_SIZE];
  2792. struct nat_entry_set *setvec[SETVEC_SIZE];
  2793. nid_t nid = 0;
  2794. unsigned int found;
  2795. if (!nm_i)
  2796. return;
  2797. /* destroy free nid list */
  2798. spin_lock(&nm_i->nid_list_lock);
  2799. list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
  2800. __remove_free_nid(sbi, i, FREE_NID);
  2801. spin_unlock(&nm_i->nid_list_lock);
  2802. kmem_cache_free(free_nid_slab, i);
  2803. spin_lock(&nm_i->nid_list_lock);
  2804. }
  2805. f2fs_bug_on(sbi, nm_i->nid_cnt[FREE_NID]);
  2806. f2fs_bug_on(sbi, nm_i->nid_cnt[PREALLOC_NID]);
  2807. f2fs_bug_on(sbi, !list_empty(&nm_i->free_nid_list));
  2808. spin_unlock(&nm_i->nid_list_lock);
  2809. /* destroy nat cache */
  2810. f2fs_down_write(&nm_i->nat_tree_lock);
  2811. while ((found = __gang_lookup_nat_cache(nm_i,
  2812. nid, NATVEC_SIZE, natvec))) {
  2813. unsigned idx;
  2814. nid = nat_get_nid(natvec[found - 1]) + 1;
  2815. for (idx = 0; idx < found; idx++) {
  2816. spin_lock(&nm_i->nat_list_lock);
  2817. list_del(&natvec[idx]->list);
  2818. spin_unlock(&nm_i->nat_list_lock);
  2819. __del_from_nat_cache(nm_i, natvec[idx]);
  2820. }
  2821. }
  2822. f2fs_bug_on(sbi, nm_i->nat_cnt[TOTAL_NAT]);
  2823. /* destroy nat set cache */
  2824. nid = 0;
  2825. while ((found = __gang_lookup_nat_set(nm_i,
  2826. nid, SETVEC_SIZE, setvec))) {
  2827. unsigned idx;
  2828. nid = setvec[found - 1]->set + 1;
  2829. for (idx = 0; idx < found; idx++) {
  2830. /* entry_cnt is not zero, when cp_error was occurred */
  2831. f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
  2832. radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
  2833. kmem_cache_free(nat_entry_set_slab, setvec[idx]);
  2834. }
  2835. }
  2836. f2fs_up_write(&nm_i->nat_tree_lock);
  2837. kvfree(nm_i->nat_block_bitmap);
  2838. if (nm_i->free_nid_bitmap) {
  2839. int i;
  2840. for (i = 0; i < nm_i->nat_blocks; i++)
  2841. kvfree(nm_i->free_nid_bitmap[i]);
  2842. kvfree(nm_i->free_nid_bitmap);
  2843. }
  2844. kvfree(nm_i->free_nid_count);
  2845. kvfree(nm_i->nat_bitmap);
  2846. kvfree(nm_i->nat_bits);
  2847. #ifdef CONFIG_F2FS_CHECK_FS
  2848. kvfree(nm_i->nat_bitmap_mir);
  2849. #endif
  2850. sbi->nm_info = NULL;
  2851. kfree(nm_i);
  2852. }
  2853. int __init f2fs_create_node_manager_caches(void)
  2854. {
  2855. nat_entry_slab = f2fs_kmem_cache_create("f2fs_nat_entry",
  2856. sizeof(struct nat_entry));
  2857. if (!nat_entry_slab)
  2858. goto fail;
  2859. free_nid_slab = f2fs_kmem_cache_create("f2fs_free_nid",
  2860. sizeof(struct free_nid));
  2861. if (!free_nid_slab)
  2862. goto destroy_nat_entry;
  2863. nat_entry_set_slab = f2fs_kmem_cache_create("f2fs_nat_entry_set",
  2864. sizeof(struct nat_entry_set));
  2865. if (!nat_entry_set_slab)
  2866. goto destroy_free_nid;
  2867. fsync_node_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_node_entry",
  2868. sizeof(struct fsync_node_entry));
  2869. if (!fsync_node_entry_slab)
  2870. goto destroy_nat_entry_set;
  2871. return 0;
  2872. destroy_nat_entry_set:
  2873. kmem_cache_destroy(nat_entry_set_slab);
  2874. destroy_free_nid:
  2875. kmem_cache_destroy(free_nid_slab);
  2876. destroy_nat_entry:
  2877. kmem_cache_destroy(nat_entry_slab);
  2878. fail:
  2879. return -ENOMEM;
  2880. }
  2881. void f2fs_destroy_node_manager_caches(void)
  2882. {
  2883. kmem_cache_destroy(fsync_node_entry_slab);
  2884. kmem_cache_destroy(nat_entry_set_slab);
  2885. kmem_cache_destroy(free_nid_slab);
  2886. kmem_cache_destroy(nat_entry_slab);
  2887. }