node.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450
  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(nm_i) 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_folio(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] && nids[i] == dn->inode->i_ino) {
  694. err = -EFSCORRUPTED;
  695. f2fs_err_ratelimited(sbi,
  696. "inode mapping table is corrupted, run fsck to fix it, "
  697. "ino:%lu, nid:%u, level:%d, offset:%d",
  698. dn->inode->i_ino, nids[i], level, offset[level]);
  699. set_sbi_flag(sbi, SBI_NEED_FSCK);
  700. goto release_pages;
  701. }
  702. if (!nids[i] && mode == ALLOC_NODE) {
  703. /* alloc new node */
  704. if (!f2fs_alloc_nid(sbi, &(nids[i]))) {
  705. err = -ENOSPC;
  706. goto release_pages;
  707. }
  708. dn->nid = nids[i];
  709. npage[i] = f2fs_new_node_page(dn, noffset[i]);
  710. if (IS_ERR(npage[i])) {
  711. f2fs_alloc_nid_failed(sbi, nids[i]);
  712. err = PTR_ERR(npage[i]);
  713. goto release_pages;
  714. }
  715. set_nid(parent, offset[i - 1], nids[i], i == 1);
  716. f2fs_alloc_nid_done(sbi, nids[i]);
  717. done = true;
  718. } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
  719. npage[i] = f2fs_get_node_page_ra(parent, offset[i - 1]);
  720. if (IS_ERR(npage[i])) {
  721. err = PTR_ERR(npage[i]);
  722. goto release_pages;
  723. }
  724. done = true;
  725. }
  726. if (i == 1) {
  727. dn->inode_page_locked = false;
  728. unlock_page(parent);
  729. } else {
  730. f2fs_put_page(parent, 1);
  731. }
  732. if (!done) {
  733. npage[i] = f2fs_get_node_page(sbi, nids[i]);
  734. if (IS_ERR(npage[i])) {
  735. err = PTR_ERR(npage[i]);
  736. f2fs_put_page(npage[0], 0);
  737. goto release_out;
  738. }
  739. }
  740. if (i < level) {
  741. parent = npage[i];
  742. nids[i + 1] = get_nid(parent, offset[i], false);
  743. }
  744. }
  745. dn->nid = nids[level];
  746. dn->ofs_in_node = offset[level];
  747. dn->node_page = npage[level];
  748. dn->data_blkaddr = f2fs_data_blkaddr(dn);
  749. if (is_inode_flag_set(dn->inode, FI_COMPRESSED_FILE) &&
  750. f2fs_sb_has_readonly(sbi)) {
  751. unsigned int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
  752. unsigned int ofs_in_node = dn->ofs_in_node;
  753. pgoff_t fofs = index;
  754. unsigned int c_len;
  755. block_t blkaddr;
  756. /* should align fofs and ofs_in_node to cluster_size */
  757. if (fofs % cluster_size) {
  758. fofs = round_down(fofs, cluster_size);
  759. ofs_in_node = round_down(ofs_in_node, cluster_size);
  760. }
  761. c_len = f2fs_cluster_blocks_are_contiguous(dn, ofs_in_node);
  762. if (!c_len)
  763. goto out;
  764. blkaddr = data_blkaddr(dn->inode, dn->node_page, ofs_in_node);
  765. if (blkaddr == COMPRESS_ADDR)
  766. blkaddr = data_blkaddr(dn->inode, dn->node_page,
  767. ofs_in_node + 1);
  768. f2fs_update_read_extent_tree_range_compressed(dn->inode,
  769. fofs, blkaddr, cluster_size, c_len);
  770. }
  771. out:
  772. return 0;
  773. release_pages:
  774. f2fs_put_page(parent, 1);
  775. if (i > 1)
  776. f2fs_put_page(npage[0], 0);
  777. release_out:
  778. dn->inode_page = NULL;
  779. dn->node_page = NULL;
  780. if (err == -ENOENT) {
  781. dn->cur_level = i;
  782. dn->max_level = level;
  783. dn->ofs_in_node = offset[level];
  784. }
  785. return err;
  786. }
  787. static int truncate_node(struct dnode_of_data *dn)
  788. {
  789. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  790. struct node_info ni;
  791. int err;
  792. pgoff_t index;
  793. err = f2fs_get_node_info(sbi, dn->nid, &ni, false);
  794. if (err)
  795. return err;
  796. if (ni.blk_addr != NEW_ADDR &&
  797. !f2fs_is_valid_blkaddr(sbi, ni.blk_addr, DATA_GENERIC_ENHANCE)) {
  798. f2fs_err_ratelimited(sbi,
  799. "nat entry is corrupted, run fsck to fix it, ino:%u, "
  800. "nid:%u, blkaddr:%u", ni.ino, ni.nid, ni.blk_addr);
  801. set_sbi_flag(sbi, SBI_NEED_FSCK);
  802. f2fs_handle_error(sbi, ERROR_INCONSISTENT_NAT);
  803. return -EFSCORRUPTED;
  804. }
  805. /* Deallocate node address */
  806. f2fs_invalidate_blocks(sbi, ni.blk_addr);
  807. dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
  808. set_node_addr(sbi, &ni, NULL_ADDR, false);
  809. if (dn->nid == dn->inode->i_ino) {
  810. f2fs_remove_orphan_inode(sbi, dn->nid);
  811. dec_valid_inode_count(sbi);
  812. f2fs_inode_synced(dn->inode);
  813. }
  814. clear_node_page_dirty(dn->node_page);
  815. set_sbi_flag(sbi, SBI_IS_DIRTY);
  816. index = page_folio(dn->node_page)->index;
  817. f2fs_put_page(dn->node_page, 1);
  818. invalidate_mapping_pages(NODE_MAPPING(sbi),
  819. index, index);
  820. dn->node_page = NULL;
  821. trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
  822. return 0;
  823. }
  824. static int truncate_dnode(struct dnode_of_data *dn)
  825. {
  826. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  827. struct page *page;
  828. int err;
  829. if (dn->nid == 0)
  830. return 1;
  831. /* get direct node */
  832. page = f2fs_get_node_page(sbi, dn->nid);
  833. if (PTR_ERR(page) == -ENOENT)
  834. return 1;
  835. else if (IS_ERR(page))
  836. return PTR_ERR(page);
  837. if (IS_INODE(page) || ino_of_node(page) != dn->inode->i_ino) {
  838. f2fs_err(sbi, "incorrect node reference, ino: %lu, nid: %u, ino_of_node: %u",
  839. dn->inode->i_ino, dn->nid, ino_of_node(page));
  840. set_sbi_flag(sbi, SBI_NEED_FSCK);
  841. f2fs_handle_error(sbi, ERROR_INVALID_NODE_REFERENCE);
  842. f2fs_put_page(page, 1);
  843. return -EFSCORRUPTED;
  844. }
  845. /* Make dnode_of_data for parameter */
  846. dn->node_page = page;
  847. dn->ofs_in_node = 0;
  848. f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
  849. err = truncate_node(dn);
  850. if (err) {
  851. f2fs_put_page(page, 1);
  852. return err;
  853. }
  854. return 1;
  855. }
  856. static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
  857. int ofs, int depth)
  858. {
  859. struct dnode_of_data rdn = *dn;
  860. struct page *page;
  861. struct f2fs_node *rn;
  862. nid_t child_nid;
  863. unsigned int child_nofs;
  864. int freed = 0;
  865. int i, ret;
  866. if (dn->nid == 0)
  867. return NIDS_PER_BLOCK + 1;
  868. trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
  869. page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
  870. if (IS_ERR(page)) {
  871. trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
  872. return PTR_ERR(page);
  873. }
  874. f2fs_ra_node_pages(page, ofs, NIDS_PER_BLOCK);
  875. rn = F2FS_NODE(page);
  876. if (depth < 3) {
  877. for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
  878. child_nid = le32_to_cpu(rn->in.nid[i]);
  879. if (child_nid == 0)
  880. continue;
  881. rdn.nid = child_nid;
  882. ret = truncate_dnode(&rdn);
  883. if (ret < 0)
  884. goto out_err;
  885. if (set_nid(page, i, 0, false))
  886. dn->node_changed = true;
  887. }
  888. } else {
  889. child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
  890. for (i = ofs; i < NIDS_PER_BLOCK; i++) {
  891. child_nid = le32_to_cpu(rn->in.nid[i]);
  892. if (child_nid == 0) {
  893. child_nofs += NIDS_PER_BLOCK + 1;
  894. continue;
  895. }
  896. rdn.nid = child_nid;
  897. ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
  898. if (ret == (NIDS_PER_BLOCK + 1)) {
  899. if (set_nid(page, i, 0, false))
  900. dn->node_changed = true;
  901. child_nofs += ret;
  902. } else if (ret < 0 && ret != -ENOENT) {
  903. goto out_err;
  904. }
  905. }
  906. freed = child_nofs;
  907. }
  908. if (!ofs) {
  909. /* remove current indirect node */
  910. dn->node_page = page;
  911. ret = truncate_node(dn);
  912. if (ret)
  913. goto out_err;
  914. freed++;
  915. } else {
  916. f2fs_put_page(page, 1);
  917. }
  918. trace_f2fs_truncate_nodes_exit(dn->inode, freed);
  919. return freed;
  920. out_err:
  921. f2fs_put_page(page, 1);
  922. trace_f2fs_truncate_nodes_exit(dn->inode, ret);
  923. return ret;
  924. }
  925. static int truncate_partial_nodes(struct dnode_of_data *dn,
  926. struct f2fs_inode *ri, int *offset, int depth)
  927. {
  928. struct page *pages[2];
  929. nid_t nid[3];
  930. nid_t child_nid;
  931. int err = 0;
  932. int i;
  933. int idx = depth - 2;
  934. nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
  935. if (!nid[0])
  936. return 0;
  937. /* get indirect nodes in the path */
  938. for (i = 0; i < idx + 1; i++) {
  939. /* reference count'll be increased */
  940. pages[i] = f2fs_get_node_page(F2FS_I_SB(dn->inode), nid[i]);
  941. if (IS_ERR(pages[i])) {
  942. err = PTR_ERR(pages[i]);
  943. idx = i - 1;
  944. goto fail;
  945. }
  946. nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
  947. }
  948. f2fs_ra_node_pages(pages[idx], offset[idx + 1], NIDS_PER_BLOCK);
  949. /* free direct nodes linked to a partial indirect node */
  950. for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
  951. child_nid = get_nid(pages[idx], i, false);
  952. if (!child_nid)
  953. continue;
  954. dn->nid = child_nid;
  955. err = truncate_dnode(dn);
  956. if (err < 0)
  957. goto fail;
  958. if (set_nid(pages[idx], i, 0, false))
  959. dn->node_changed = true;
  960. }
  961. if (offset[idx + 1] == 0) {
  962. dn->node_page = pages[idx];
  963. dn->nid = nid[idx];
  964. err = truncate_node(dn);
  965. if (err)
  966. goto fail;
  967. } else {
  968. f2fs_put_page(pages[idx], 1);
  969. }
  970. offset[idx]++;
  971. offset[idx + 1] = 0;
  972. idx--;
  973. fail:
  974. for (i = idx; i >= 0; i--)
  975. f2fs_put_page(pages[i], 1);
  976. trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
  977. return err;
  978. }
  979. /*
  980. * All the block addresses of data and nodes should be nullified.
  981. */
  982. int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from)
  983. {
  984. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  985. int err = 0, cont = 1;
  986. int level, offset[4], noffset[4];
  987. unsigned int nofs = 0;
  988. struct f2fs_inode *ri;
  989. struct dnode_of_data dn;
  990. struct page *page;
  991. trace_f2fs_truncate_inode_blocks_enter(inode, from);
  992. level = get_node_path(inode, from, offset, noffset);
  993. if (level <= 0) {
  994. if (!level) {
  995. level = -EFSCORRUPTED;
  996. f2fs_err(sbi, "%s: inode ino=%lx has corrupted node block, from:%lu addrs:%u",
  997. __func__, inode->i_ino,
  998. from, ADDRS_PER_INODE(inode));
  999. set_sbi_flag(sbi, SBI_NEED_FSCK);
  1000. }
  1001. trace_f2fs_truncate_inode_blocks_exit(inode, level);
  1002. return level;
  1003. }
  1004. page = f2fs_get_node_page(sbi, inode->i_ino);
  1005. if (IS_ERR(page)) {
  1006. trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
  1007. return PTR_ERR(page);
  1008. }
  1009. set_new_dnode(&dn, inode, page, NULL, 0);
  1010. unlock_page(page);
  1011. ri = F2FS_INODE(page);
  1012. switch (level) {
  1013. case 0:
  1014. case 1:
  1015. nofs = noffset[1];
  1016. break;
  1017. case 2:
  1018. nofs = noffset[1];
  1019. if (!offset[level - 1])
  1020. goto skip_partial;
  1021. err = truncate_partial_nodes(&dn, ri, offset, level);
  1022. if (err < 0 && err != -ENOENT)
  1023. goto fail;
  1024. nofs += 1 + NIDS_PER_BLOCK;
  1025. break;
  1026. case 3:
  1027. nofs = 5 + 2 * NIDS_PER_BLOCK;
  1028. if (!offset[level - 1])
  1029. goto skip_partial;
  1030. err = truncate_partial_nodes(&dn, ri, offset, level);
  1031. if (err < 0 && err != -ENOENT)
  1032. goto fail;
  1033. break;
  1034. default:
  1035. BUG();
  1036. }
  1037. skip_partial:
  1038. while (cont) {
  1039. dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
  1040. switch (offset[0]) {
  1041. case NODE_DIR1_BLOCK:
  1042. case NODE_DIR2_BLOCK:
  1043. err = truncate_dnode(&dn);
  1044. break;
  1045. case NODE_IND1_BLOCK:
  1046. case NODE_IND2_BLOCK:
  1047. err = truncate_nodes(&dn, nofs, offset[1], 2);
  1048. break;
  1049. case NODE_DIND_BLOCK:
  1050. err = truncate_nodes(&dn, nofs, offset[1], 3);
  1051. cont = 0;
  1052. break;
  1053. default:
  1054. BUG();
  1055. }
  1056. if (err == -ENOENT) {
  1057. set_sbi_flag(F2FS_P_SB(page), SBI_NEED_FSCK);
  1058. f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
  1059. f2fs_err_ratelimited(sbi,
  1060. "truncate node fail, ino:%lu, nid:%u, "
  1061. "offset[0]:%d, offset[1]:%d, nofs:%d",
  1062. inode->i_ino, dn.nid, offset[0],
  1063. offset[1], nofs);
  1064. err = 0;
  1065. }
  1066. if (err < 0)
  1067. goto fail;
  1068. if (offset[1] == 0 &&
  1069. ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
  1070. lock_page(page);
  1071. BUG_ON(page->mapping != NODE_MAPPING(sbi));
  1072. f2fs_wait_on_page_writeback(page, NODE, true, true);
  1073. ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
  1074. set_page_dirty(page);
  1075. unlock_page(page);
  1076. }
  1077. offset[1] = 0;
  1078. offset[0]++;
  1079. nofs += err;
  1080. }
  1081. fail:
  1082. f2fs_put_page(page, 0);
  1083. trace_f2fs_truncate_inode_blocks_exit(inode, err);
  1084. return err > 0 ? 0 : err;
  1085. }
  1086. /* caller must lock inode page */
  1087. int f2fs_truncate_xattr_node(struct inode *inode)
  1088. {
  1089. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1090. nid_t nid = F2FS_I(inode)->i_xattr_nid;
  1091. struct dnode_of_data dn;
  1092. struct page *npage;
  1093. int err;
  1094. if (!nid)
  1095. return 0;
  1096. npage = f2fs_get_node_page(sbi, nid);
  1097. if (IS_ERR(npage))
  1098. return PTR_ERR(npage);
  1099. set_new_dnode(&dn, inode, NULL, npage, nid);
  1100. err = truncate_node(&dn);
  1101. if (err) {
  1102. f2fs_put_page(npage, 1);
  1103. return err;
  1104. }
  1105. f2fs_i_xnid_write(inode, 0);
  1106. return 0;
  1107. }
  1108. /*
  1109. * Caller should grab and release a rwsem by calling f2fs_lock_op() and
  1110. * f2fs_unlock_op().
  1111. */
  1112. int f2fs_remove_inode_page(struct inode *inode)
  1113. {
  1114. struct dnode_of_data dn;
  1115. int err;
  1116. set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
  1117. err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
  1118. if (err)
  1119. return err;
  1120. err = f2fs_truncate_xattr_node(inode);
  1121. if (err) {
  1122. f2fs_put_dnode(&dn);
  1123. return err;
  1124. }
  1125. /* remove potential inline_data blocks */
  1126. if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1127. S_ISLNK(inode->i_mode))
  1128. f2fs_truncate_data_blocks_range(&dn, 1);
  1129. /* 0 is possible, after f2fs_new_inode() has failed */
  1130. if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
  1131. f2fs_put_dnode(&dn);
  1132. return -EIO;
  1133. }
  1134. if (unlikely(inode->i_blocks != 0 && inode->i_blocks != 8)) {
  1135. f2fs_warn(F2FS_I_SB(inode),
  1136. "f2fs_remove_inode_page: inconsistent i_blocks, ino:%lu, iblocks:%llu",
  1137. inode->i_ino, (unsigned long long)inode->i_blocks);
  1138. set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
  1139. }
  1140. /* will put inode & node pages */
  1141. err = truncate_node(&dn);
  1142. if (err) {
  1143. f2fs_put_dnode(&dn);
  1144. return err;
  1145. }
  1146. return 0;
  1147. }
  1148. struct page *f2fs_new_inode_page(struct inode *inode)
  1149. {
  1150. struct dnode_of_data dn;
  1151. /* allocate inode page for new inode */
  1152. set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
  1153. /* caller should f2fs_put_page(page, 1); */
  1154. return f2fs_new_node_page(&dn, 0);
  1155. }
  1156. struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs)
  1157. {
  1158. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  1159. struct node_info new_ni;
  1160. struct page *page;
  1161. int err;
  1162. if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
  1163. return ERR_PTR(-EPERM);
  1164. page = f2fs_grab_cache_page(NODE_MAPPING(sbi), dn->nid, false);
  1165. if (!page)
  1166. return ERR_PTR(-ENOMEM);
  1167. if (unlikely((err = inc_valid_node_count(sbi, dn->inode, !ofs))))
  1168. goto fail;
  1169. #ifdef CONFIG_F2FS_CHECK_FS
  1170. err = f2fs_get_node_info(sbi, dn->nid, &new_ni, false);
  1171. if (err) {
  1172. dec_valid_node_count(sbi, dn->inode, !ofs);
  1173. goto fail;
  1174. }
  1175. if (unlikely(new_ni.blk_addr != NULL_ADDR)) {
  1176. err = -EFSCORRUPTED;
  1177. dec_valid_node_count(sbi, dn->inode, !ofs);
  1178. set_sbi_flag(sbi, SBI_NEED_FSCK);
  1179. f2fs_warn_ratelimited(sbi,
  1180. "f2fs_new_node_page: inconsistent nat entry, "
  1181. "ino:%u, nid:%u, blkaddr:%u, ver:%u, flag:%u",
  1182. new_ni.ino, new_ni.nid, new_ni.blk_addr,
  1183. new_ni.version, new_ni.flag);
  1184. f2fs_handle_error(sbi, ERROR_INCONSISTENT_NAT);
  1185. goto fail;
  1186. }
  1187. #endif
  1188. new_ni.nid = dn->nid;
  1189. new_ni.ino = dn->inode->i_ino;
  1190. new_ni.blk_addr = NULL_ADDR;
  1191. new_ni.flag = 0;
  1192. new_ni.version = 0;
  1193. set_node_addr(sbi, &new_ni, NEW_ADDR, false);
  1194. f2fs_wait_on_page_writeback(page, NODE, true, true);
  1195. fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
  1196. set_cold_node(page, S_ISDIR(dn->inode->i_mode));
  1197. if (!PageUptodate(page))
  1198. SetPageUptodate(page);
  1199. if (set_page_dirty(page))
  1200. dn->node_changed = true;
  1201. if (f2fs_has_xattr_block(ofs))
  1202. f2fs_i_xnid_write(dn->inode, dn->nid);
  1203. if (ofs == 0)
  1204. inc_valid_inode_count(sbi);
  1205. return page;
  1206. fail:
  1207. clear_node_page_dirty(page);
  1208. f2fs_put_page(page, 1);
  1209. return ERR_PTR(err);
  1210. }
  1211. /*
  1212. * Caller should do after getting the following values.
  1213. * 0: f2fs_put_page(page, 0)
  1214. * LOCKED_PAGE or error: f2fs_put_page(page, 1)
  1215. */
  1216. static int read_node_page(struct page *page, blk_opf_t op_flags)
  1217. {
  1218. struct folio *folio = page_folio(page);
  1219. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  1220. struct node_info ni;
  1221. struct f2fs_io_info fio = {
  1222. .sbi = sbi,
  1223. .type = NODE,
  1224. .op = REQ_OP_READ,
  1225. .op_flags = op_flags,
  1226. .page = page,
  1227. .encrypted_page = NULL,
  1228. };
  1229. int err;
  1230. if (folio_test_uptodate(folio)) {
  1231. if (!f2fs_inode_chksum_verify(sbi, page)) {
  1232. folio_clear_uptodate(folio);
  1233. return -EFSBADCRC;
  1234. }
  1235. return LOCKED_PAGE;
  1236. }
  1237. err = f2fs_get_node_info(sbi, folio->index, &ni, false);
  1238. if (err)
  1239. return err;
  1240. /* NEW_ADDR can be seen, after cp_error drops some dirty node pages */
  1241. if (unlikely(ni.blk_addr == NULL_ADDR || ni.blk_addr == NEW_ADDR)) {
  1242. folio_clear_uptodate(folio);
  1243. return -ENOENT;
  1244. }
  1245. fio.new_blkaddr = fio.old_blkaddr = ni.blk_addr;
  1246. err = f2fs_submit_page_bio(&fio);
  1247. if (!err)
  1248. f2fs_update_iostat(sbi, NULL, FS_NODE_READ_IO, F2FS_BLKSIZE);
  1249. return err;
  1250. }
  1251. /*
  1252. * Readahead a node page
  1253. */
  1254. void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
  1255. {
  1256. struct page *apage;
  1257. int err;
  1258. if (!nid)
  1259. return;
  1260. if (f2fs_check_nid_range(sbi, nid))
  1261. return;
  1262. apage = xa_load(&NODE_MAPPING(sbi)->i_pages, nid);
  1263. if (apage)
  1264. return;
  1265. apage = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
  1266. if (!apage)
  1267. return;
  1268. err = read_node_page(apage, REQ_RAHEAD);
  1269. f2fs_put_page(apage, err ? 1 : 0);
  1270. }
  1271. static struct page *__get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid,
  1272. struct page *parent, int start)
  1273. {
  1274. struct page *page;
  1275. int err;
  1276. if (!nid)
  1277. return ERR_PTR(-ENOENT);
  1278. if (f2fs_check_nid_range(sbi, nid))
  1279. return ERR_PTR(-EINVAL);
  1280. repeat:
  1281. page = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
  1282. if (!page)
  1283. return ERR_PTR(-ENOMEM);
  1284. err = read_node_page(page, 0);
  1285. if (err < 0) {
  1286. goto out_put_err;
  1287. } else if (err == LOCKED_PAGE) {
  1288. err = 0;
  1289. goto page_hit;
  1290. }
  1291. if (parent)
  1292. f2fs_ra_node_pages(parent, start + 1, MAX_RA_NODE);
  1293. lock_page(page);
  1294. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1295. f2fs_put_page(page, 1);
  1296. goto repeat;
  1297. }
  1298. if (unlikely(!PageUptodate(page))) {
  1299. err = -EIO;
  1300. goto out_err;
  1301. }
  1302. if (!f2fs_inode_chksum_verify(sbi, page)) {
  1303. err = -EFSBADCRC;
  1304. goto out_err;
  1305. }
  1306. page_hit:
  1307. if (likely(nid == nid_of_node(page)))
  1308. return page;
  1309. f2fs_warn(sbi, "inconsistent node block, nid:%lu, node_footer[nid:%u,ino:%u,ofs:%u,cpver:%llu,blkaddr:%u]",
  1310. nid, nid_of_node(page), ino_of_node(page),
  1311. ofs_of_node(page), cpver_of_node(page),
  1312. next_blkaddr_of_node(page));
  1313. set_sbi_flag(sbi, SBI_NEED_FSCK);
  1314. f2fs_handle_error(sbi, ERROR_INCONSISTENT_FOOTER);
  1315. err = -EFSCORRUPTED;
  1316. out_err:
  1317. ClearPageUptodate(page);
  1318. out_put_err:
  1319. /* ENOENT comes from read_node_page which is not an error. */
  1320. if (err != -ENOENT)
  1321. f2fs_handle_page_eio(sbi, page_folio(page), NODE);
  1322. f2fs_put_page(page, 1);
  1323. return ERR_PTR(err);
  1324. }
  1325. struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
  1326. {
  1327. return __get_node_page(sbi, nid, NULL, 0);
  1328. }
  1329. struct page *f2fs_get_node_page_ra(struct page *parent, int start)
  1330. {
  1331. struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
  1332. nid_t nid = get_nid(parent, start, false);
  1333. return __get_node_page(sbi, nid, parent, start);
  1334. }
  1335. static void flush_inline_data(struct f2fs_sb_info *sbi, nid_t ino)
  1336. {
  1337. struct inode *inode;
  1338. struct page *page;
  1339. int ret;
  1340. /* should flush inline_data before evict_inode */
  1341. inode = ilookup(sbi->sb, ino);
  1342. if (!inode)
  1343. return;
  1344. page = f2fs_pagecache_get_page(inode->i_mapping, 0,
  1345. FGP_LOCK|FGP_NOWAIT, 0);
  1346. if (!page)
  1347. goto iput_out;
  1348. if (!PageUptodate(page))
  1349. goto page_out;
  1350. if (!PageDirty(page))
  1351. goto page_out;
  1352. if (!clear_page_dirty_for_io(page))
  1353. goto page_out;
  1354. ret = f2fs_write_inline_data(inode, page_folio(page));
  1355. inode_dec_dirty_pages(inode);
  1356. f2fs_remove_dirty_inode(inode);
  1357. if (ret)
  1358. set_page_dirty(page);
  1359. page_out:
  1360. f2fs_put_page(page, 1);
  1361. iput_out:
  1362. iput(inode);
  1363. }
  1364. static struct page *last_fsync_dnode(struct f2fs_sb_info *sbi, nid_t ino)
  1365. {
  1366. pgoff_t index;
  1367. struct folio_batch fbatch;
  1368. struct page *last_page = NULL;
  1369. int nr_folios;
  1370. folio_batch_init(&fbatch);
  1371. index = 0;
  1372. while ((nr_folios = filemap_get_folios_tag(NODE_MAPPING(sbi), &index,
  1373. (pgoff_t)-1, PAGECACHE_TAG_DIRTY,
  1374. &fbatch))) {
  1375. int i;
  1376. for (i = 0; i < nr_folios; i++) {
  1377. struct page *page = &fbatch.folios[i]->page;
  1378. if (unlikely(f2fs_cp_error(sbi))) {
  1379. f2fs_put_page(last_page, 0);
  1380. folio_batch_release(&fbatch);
  1381. return ERR_PTR(-EIO);
  1382. }
  1383. if (!IS_DNODE(page) || !is_cold_node(page))
  1384. continue;
  1385. if (ino_of_node(page) != ino)
  1386. continue;
  1387. lock_page(page);
  1388. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1389. continue_unlock:
  1390. unlock_page(page);
  1391. continue;
  1392. }
  1393. if (ino_of_node(page) != ino)
  1394. goto continue_unlock;
  1395. if (!PageDirty(page)) {
  1396. /* someone wrote it for us */
  1397. goto continue_unlock;
  1398. }
  1399. if (last_page)
  1400. f2fs_put_page(last_page, 0);
  1401. get_page(page);
  1402. last_page = page;
  1403. unlock_page(page);
  1404. }
  1405. folio_batch_release(&fbatch);
  1406. cond_resched();
  1407. }
  1408. return last_page;
  1409. }
  1410. static int __write_node_page(struct page *page, bool atomic, bool *submitted,
  1411. struct writeback_control *wbc, bool do_balance,
  1412. enum iostat_type io_type, unsigned int *seq_id)
  1413. {
  1414. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  1415. struct folio *folio = page_folio(page);
  1416. nid_t nid;
  1417. struct node_info ni;
  1418. struct f2fs_io_info fio = {
  1419. .sbi = sbi,
  1420. .ino = ino_of_node(page),
  1421. .type = NODE,
  1422. .op = REQ_OP_WRITE,
  1423. .op_flags = wbc_to_write_flags(wbc),
  1424. .page = page,
  1425. .encrypted_page = NULL,
  1426. .submitted = 0,
  1427. .io_type = io_type,
  1428. .io_wbc = wbc,
  1429. };
  1430. unsigned int seq;
  1431. trace_f2fs_writepage(folio, NODE);
  1432. if (unlikely(f2fs_cp_error(sbi))) {
  1433. /* keep node pages in remount-ro mode */
  1434. if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY)
  1435. goto redirty_out;
  1436. folio_clear_uptodate(folio);
  1437. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1438. folio_unlock(folio);
  1439. return 0;
  1440. }
  1441. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  1442. goto redirty_out;
  1443. if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
  1444. wbc->sync_mode == WB_SYNC_NONE &&
  1445. IS_DNODE(page) && is_cold_node(page))
  1446. goto redirty_out;
  1447. /* get old block addr of this node page */
  1448. nid = nid_of_node(page);
  1449. f2fs_bug_on(sbi, folio->index != nid);
  1450. if (f2fs_get_node_info(sbi, nid, &ni, !do_balance))
  1451. goto redirty_out;
  1452. if (wbc->for_reclaim) {
  1453. if (!f2fs_down_read_trylock(&sbi->node_write))
  1454. goto redirty_out;
  1455. } else {
  1456. f2fs_down_read(&sbi->node_write);
  1457. }
  1458. /* This page is already truncated */
  1459. if (unlikely(ni.blk_addr == NULL_ADDR)) {
  1460. folio_clear_uptodate(folio);
  1461. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1462. f2fs_up_read(&sbi->node_write);
  1463. folio_unlock(folio);
  1464. return 0;
  1465. }
  1466. if (__is_valid_data_blkaddr(ni.blk_addr) &&
  1467. !f2fs_is_valid_blkaddr(sbi, ni.blk_addr,
  1468. DATA_GENERIC_ENHANCE)) {
  1469. f2fs_up_read(&sbi->node_write);
  1470. goto redirty_out;
  1471. }
  1472. if (atomic && !test_opt(sbi, NOBARRIER))
  1473. fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
  1474. /* should add to global list before clearing PAGECACHE status */
  1475. if (f2fs_in_warm_node_list(sbi, page)) {
  1476. seq = f2fs_add_fsync_node_entry(sbi, page);
  1477. if (seq_id)
  1478. *seq_id = seq;
  1479. }
  1480. folio_start_writeback(folio);
  1481. fio.old_blkaddr = ni.blk_addr;
  1482. f2fs_do_write_node_page(nid, &fio);
  1483. set_node_addr(sbi, &ni, fio.new_blkaddr, is_fsync_dnode(page));
  1484. dec_page_count(sbi, F2FS_DIRTY_NODES);
  1485. f2fs_up_read(&sbi->node_write);
  1486. if (wbc->for_reclaim) {
  1487. f2fs_submit_merged_write_cond(sbi, NULL, page, 0, NODE);
  1488. submitted = NULL;
  1489. }
  1490. folio_unlock(folio);
  1491. if (unlikely(f2fs_cp_error(sbi))) {
  1492. f2fs_submit_merged_write(sbi, NODE);
  1493. submitted = NULL;
  1494. }
  1495. if (submitted)
  1496. *submitted = fio.submitted;
  1497. if (do_balance)
  1498. f2fs_balance_fs(sbi, false);
  1499. return 0;
  1500. redirty_out:
  1501. folio_redirty_for_writepage(wbc, folio);
  1502. return AOP_WRITEPAGE_ACTIVATE;
  1503. }
  1504. int f2fs_move_node_page(struct page *node_page, int gc_type)
  1505. {
  1506. int err = 0;
  1507. if (gc_type == FG_GC) {
  1508. struct writeback_control wbc = {
  1509. .sync_mode = WB_SYNC_ALL,
  1510. .nr_to_write = 1,
  1511. .for_reclaim = 0,
  1512. };
  1513. f2fs_wait_on_page_writeback(node_page, NODE, true, true);
  1514. set_page_dirty(node_page);
  1515. if (!clear_page_dirty_for_io(node_page)) {
  1516. err = -EAGAIN;
  1517. goto out_page;
  1518. }
  1519. if (__write_node_page(node_page, false, NULL,
  1520. &wbc, false, FS_GC_NODE_IO, NULL)) {
  1521. err = -EAGAIN;
  1522. unlock_page(node_page);
  1523. }
  1524. goto release_page;
  1525. } else {
  1526. /* set page dirty and write it */
  1527. if (!folio_test_writeback(page_folio(node_page)))
  1528. set_page_dirty(node_page);
  1529. }
  1530. out_page:
  1531. unlock_page(node_page);
  1532. release_page:
  1533. f2fs_put_page(node_page, 0);
  1534. return err;
  1535. }
  1536. static int f2fs_write_node_page(struct page *page,
  1537. struct writeback_control *wbc)
  1538. {
  1539. return __write_node_page(page, false, NULL, wbc, false,
  1540. FS_NODE_IO, NULL);
  1541. }
  1542. int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
  1543. struct writeback_control *wbc, bool atomic,
  1544. unsigned int *seq_id)
  1545. {
  1546. pgoff_t index;
  1547. struct folio_batch fbatch;
  1548. int ret = 0;
  1549. struct page *last_page = NULL;
  1550. bool marked = false;
  1551. nid_t ino = inode->i_ino;
  1552. int nr_folios;
  1553. int nwritten = 0;
  1554. if (atomic) {
  1555. last_page = last_fsync_dnode(sbi, ino);
  1556. if (IS_ERR_OR_NULL(last_page))
  1557. return PTR_ERR_OR_ZERO(last_page);
  1558. }
  1559. retry:
  1560. folio_batch_init(&fbatch);
  1561. index = 0;
  1562. while ((nr_folios = filemap_get_folios_tag(NODE_MAPPING(sbi), &index,
  1563. (pgoff_t)-1, PAGECACHE_TAG_DIRTY,
  1564. &fbatch))) {
  1565. int i;
  1566. for (i = 0; i < nr_folios; i++) {
  1567. struct page *page = &fbatch.folios[i]->page;
  1568. bool submitted = false;
  1569. if (unlikely(f2fs_cp_error(sbi))) {
  1570. f2fs_put_page(last_page, 0);
  1571. folio_batch_release(&fbatch);
  1572. ret = -EIO;
  1573. goto out;
  1574. }
  1575. if (!IS_DNODE(page) || !is_cold_node(page))
  1576. continue;
  1577. if (ino_of_node(page) != ino)
  1578. continue;
  1579. lock_page(page);
  1580. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1581. continue_unlock:
  1582. unlock_page(page);
  1583. continue;
  1584. }
  1585. if (ino_of_node(page) != ino)
  1586. goto continue_unlock;
  1587. if (!PageDirty(page) && page != last_page) {
  1588. /* someone wrote it for us */
  1589. goto continue_unlock;
  1590. }
  1591. f2fs_wait_on_page_writeback(page, NODE, true, true);
  1592. set_fsync_mark(page, 0);
  1593. set_dentry_mark(page, 0);
  1594. if (!atomic || page == last_page) {
  1595. set_fsync_mark(page, 1);
  1596. percpu_counter_inc(&sbi->rf_node_block_count);
  1597. if (IS_INODE(page)) {
  1598. if (is_inode_flag_set(inode,
  1599. FI_DIRTY_INODE))
  1600. f2fs_update_inode(inode, page);
  1601. set_dentry_mark(page,
  1602. f2fs_need_dentry_mark(sbi, ino));
  1603. }
  1604. /* may be written by other thread */
  1605. if (!PageDirty(page))
  1606. set_page_dirty(page);
  1607. }
  1608. if (!clear_page_dirty_for_io(page))
  1609. goto continue_unlock;
  1610. ret = __write_node_page(page, atomic &&
  1611. page == last_page,
  1612. &submitted, wbc, true,
  1613. FS_NODE_IO, seq_id);
  1614. if (ret) {
  1615. unlock_page(page);
  1616. f2fs_put_page(last_page, 0);
  1617. break;
  1618. } else if (submitted) {
  1619. nwritten++;
  1620. }
  1621. if (page == last_page) {
  1622. f2fs_put_page(page, 0);
  1623. marked = true;
  1624. break;
  1625. }
  1626. }
  1627. folio_batch_release(&fbatch);
  1628. cond_resched();
  1629. if (ret || marked)
  1630. break;
  1631. }
  1632. if (!ret && atomic && !marked) {
  1633. f2fs_debug(sbi, "Retry to write fsync mark: ino=%u, idx=%lx",
  1634. ino, page_folio(last_page)->index);
  1635. lock_page(last_page);
  1636. f2fs_wait_on_page_writeback(last_page, NODE, true, true);
  1637. set_page_dirty(last_page);
  1638. unlock_page(last_page);
  1639. goto retry;
  1640. }
  1641. out:
  1642. if (nwritten)
  1643. f2fs_submit_merged_write_cond(sbi, NULL, NULL, ino, NODE);
  1644. return ret ? -EIO : 0;
  1645. }
  1646. static int f2fs_match_ino(struct inode *inode, unsigned long ino, void *data)
  1647. {
  1648. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1649. bool clean;
  1650. if (inode->i_ino != ino)
  1651. return 0;
  1652. if (!is_inode_flag_set(inode, FI_DIRTY_INODE))
  1653. return 0;
  1654. spin_lock(&sbi->inode_lock[DIRTY_META]);
  1655. clean = list_empty(&F2FS_I(inode)->gdirty_list);
  1656. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  1657. if (clean)
  1658. return 0;
  1659. inode = igrab(inode);
  1660. if (!inode)
  1661. return 0;
  1662. return 1;
  1663. }
  1664. static bool flush_dirty_inode(struct page *page)
  1665. {
  1666. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  1667. struct inode *inode;
  1668. nid_t ino = ino_of_node(page);
  1669. inode = find_inode_nowait(sbi->sb, ino, f2fs_match_ino, NULL);
  1670. if (!inode)
  1671. return false;
  1672. f2fs_update_inode(inode, page);
  1673. unlock_page(page);
  1674. iput(inode);
  1675. return true;
  1676. }
  1677. void f2fs_flush_inline_data(struct f2fs_sb_info *sbi)
  1678. {
  1679. pgoff_t index = 0;
  1680. struct folio_batch fbatch;
  1681. int nr_folios;
  1682. folio_batch_init(&fbatch);
  1683. while ((nr_folios = filemap_get_folios_tag(NODE_MAPPING(sbi), &index,
  1684. (pgoff_t)-1, PAGECACHE_TAG_DIRTY,
  1685. &fbatch))) {
  1686. int i;
  1687. for (i = 0; i < nr_folios; i++) {
  1688. struct page *page = &fbatch.folios[i]->page;
  1689. if (!IS_INODE(page))
  1690. continue;
  1691. lock_page(page);
  1692. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1693. continue_unlock:
  1694. unlock_page(page);
  1695. continue;
  1696. }
  1697. if (!PageDirty(page)) {
  1698. /* someone wrote it for us */
  1699. goto continue_unlock;
  1700. }
  1701. /* flush inline_data, if it's async context. */
  1702. if (page_private_inline(page)) {
  1703. clear_page_private_inline(page);
  1704. unlock_page(page);
  1705. flush_inline_data(sbi, ino_of_node(page));
  1706. continue;
  1707. }
  1708. unlock_page(page);
  1709. }
  1710. folio_batch_release(&fbatch);
  1711. cond_resched();
  1712. }
  1713. }
  1714. int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
  1715. struct writeback_control *wbc,
  1716. bool do_balance, enum iostat_type io_type)
  1717. {
  1718. pgoff_t index;
  1719. struct folio_batch fbatch;
  1720. int step = 0;
  1721. int nwritten = 0;
  1722. int ret = 0;
  1723. int nr_folios, done = 0;
  1724. folio_batch_init(&fbatch);
  1725. next_step:
  1726. index = 0;
  1727. while (!done && (nr_folios = filemap_get_folios_tag(NODE_MAPPING(sbi),
  1728. &index, (pgoff_t)-1, PAGECACHE_TAG_DIRTY,
  1729. &fbatch))) {
  1730. int i;
  1731. for (i = 0; i < nr_folios; i++) {
  1732. struct page *page = &fbatch.folios[i]->page;
  1733. bool submitted = false;
  1734. /* give a priority to WB_SYNC threads */
  1735. if (atomic_read(&sbi->wb_sync_req[NODE]) &&
  1736. wbc->sync_mode == WB_SYNC_NONE) {
  1737. done = 1;
  1738. break;
  1739. }
  1740. /*
  1741. * flushing sequence with step:
  1742. * 0. indirect nodes
  1743. * 1. dentry dnodes
  1744. * 2. file dnodes
  1745. */
  1746. if (step == 0 && IS_DNODE(page))
  1747. continue;
  1748. if (step == 1 && (!IS_DNODE(page) ||
  1749. is_cold_node(page)))
  1750. continue;
  1751. if (step == 2 && (!IS_DNODE(page) ||
  1752. !is_cold_node(page)))
  1753. continue;
  1754. lock_node:
  1755. if (wbc->sync_mode == WB_SYNC_ALL)
  1756. lock_page(page);
  1757. else if (!trylock_page(page))
  1758. continue;
  1759. if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
  1760. continue_unlock:
  1761. unlock_page(page);
  1762. continue;
  1763. }
  1764. if (!PageDirty(page)) {
  1765. /* someone wrote it for us */
  1766. goto continue_unlock;
  1767. }
  1768. /* flush inline_data/inode, if it's async context. */
  1769. if (!do_balance)
  1770. goto write_node;
  1771. /* flush inline_data */
  1772. if (page_private_inline(page)) {
  1773. clear_page_private_inline(page);
  1774. unlock_page(page);
  1775. flush_inline_data(sbi, ino_of_node(page));
  1776. goto lock_node;
  1777. }
  1778. /* flush dirty inode */
  1779. if (IS_INODE(page) && flush_dirty_inode(page))
  1780. goto lock_node;
  1781. write_node:
  1782. f2fs_wait_on_page_writeback(page, NODE, true, true);
  1783. if (!clear_page_dirty_for_io(page))
  1784. goto continue_unlock;
  1785. set_fsync_mark(page, 0);
  1786. set_dentry_mark(page, 0);
  1787. ret = __write_node_page(page, false, &submitted,
  1788. wbc, do_balance, io_type, NULL);
  1789. if (ret)
  1790. unlock_page(page);
  1791. else if (submitted)
  1792. nwritten++;
  1793. if (--wbc->nr_to_write == 0)
  1794. break;
  1795. }
  1796. folio_batch_release(&fbatch);
  1797. cond_resched();
  1798. if (wbc->nr_to_write == 0) {
  1799. step = 2;
  1800. break;
  1801. }
  1802. }
  1803. if (step < 2) {
  1804. if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
  1805. wbc->sync_mode == WB_SYNC_NONE && step == 1)
  1806. goto out;
  1807. step++;
  1808. goto next_step;
  1809. }
  1810. out:
  1811. if (nwritten)
  1812. f2fs_submit_merged_write(sbi, NODE);
  1813. if (unlikely(f2fs_cp_error(sbi)))
  1814. return -EIO;
  1815. return ret;
  1816. }
  1817. int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
  1818. unsigned int seq_id)
  1819. {
  1820. struct fsync_node_entry *fn;
  1821. struct page *page;
  1822. struct list_head *head = &sbi->fsync_node_list;
  1823. unsigned long flags;
  1824. unsigned int cur_seq_id = 0;
  1825. while (seq_id && cur_seq_id < seq_id) {
  1826. spin_lock_irqsave(&sbi->fsync_node_lock, flags);
  1827. if (list_empty(head)) {
  1828. spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
  1829. break;
  1830. }
  1831. fn = list_first_entry(head, struct fsync_node_entry, list);
  1832. if (fn->seq_id > seq_id) {
  1833. spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
  1834. break;
  1835. }
  1836. cur_seq_id = fn->seq_id;
  1837. page = fn->page;
  1838. get_page(page);
  1839. spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
  1840. f2fs_wait_on_page_writeback(page, NODE, true, false);
  1841. put_page(page);
  1842. }
  1843. return filemap_check_errors(NODE_MAPPING(sbi));
  1844. }
  1845. static int f2fs_write_node_pages(struct address_space *mapping,
  1846. struct writeback_control *wbc)
  1847. {
  1848. struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
  1849. struct blk_plug plug;
  1850. long diff;
  1851. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  1852. goto skip_write;
  1853. /* balancing f2fs's metadata in background */
  1854. f2fs_balance_fs_bg(sbi, true);
  1855. /* collect a number of dirty node pages and write together */
  1856. if (wbc->sync_mode != WB_SYNC_ALL &&
  1857. get_pages(sbi, F2FS_DIRTY_NODES) <
  1858. nr_pages_to_skip(sbi, NODE))
  1859. goto skip_write;
  1860. if (wbc->sync_mode == WB_SYNC_ALL)
  1861. atomic_inc(&sbi->wb_sync_req[NODE]);
  1862. else if (atomic_read(&sbi->wb_sync_req[NODE])) {
  1863. /* to avoid potential deadlock */
  1864. if (current->plug)
  1865. blk_finish_plug(current->plug);
  1866. goto skip_write;
  1867. }
  1868. trace_f2fs_writepages(mapping->host, wbc, NODE);
  1869. diff = nr_pages_to_write(sbi, NODE, wbc);
  1870. blk_start_plug(&plug);
  1871. f2fs_sync_node_pages(sbi, wbc, true, FS_NODE_IO);
  1872. blk_finish_plug(&plug);
  1873. wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
  1874. if (wbc->sync_mode == WB_SYNC_ALL)
  1875. atomic_dec(&sbi->wb_sync_req[NODE]);
  1876. return 0;
  1877. skip_write:
  1878. wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
  1879. trace_f2fs_writepages(mapping->host, wbc, NODE);
  1880. return 0;
  1881. }
  1882. static bool f2fs_dirty_node_folio(struct address_space *mapping,
  1883. struct folio *folio)
  1884. {
  1885. trace_f2fs_set_page_dirty(folio, NODE);
  1886. if (!folio_test_uptodate(folio))
  1887. folio_mark_uptodate(folio);
  1888. #ifdef CONFIG_F2FS_CHECK_FS
  1889. if (IS_INODE(&folio->page))
  1890. f2fs_inode_chksum_set(F2FS_M_SB(mapping), &folio->page);
  1891. #endif
  1892. if (filemap_dirty_folio(mapping, folio)) {
  1893. inc_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
  1894. set_page_private_reference(&folio->page);
  1895. return true;
  1896. }
  1897. return false;
  1898. }
  1899. /*
  1900. * Structure of the f2fs node operations
  1901. */
  1902. const struct address_space_operations f2fs_node_aops = {
  1903. .writepage = f2fs_write_node_page,
  1904. .writepages = f2fs_write_node_pages,
  1905. .dirty_folio = f2fs_dirty_node_folio,
  1906. .invalidate_folio = f2fs_invalidate_folio,
  1907. .release_folio = f2fs_release_folio,
  1908. .migrate_folio = filemap_migrate_folio,
  1909. };
  1910. static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
  1911. nid_t n)
  1912. {
  1913. return radix_tree_lookup(&nm_i->free_nid_root, n);
  1914. }
  1915. static int __insert_free_nid(struct f2fs_sb_info *sbi,
  1916. struct free_nid *i)
  1917. {
  1918. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1919. int err = radix_tree_insert(&nm_i->free_nid_root, i->nid, i);
  1920. if (err)
  1921. return err;
  1922. nm_i->nid_cnt[FREE_NID]++;
  1923. list_add_tail(&i->list, &nm_i->free_nid_list);
  1924. return 0;
  1925. }
  1926. static void __remove_free_nid(struct f2fs_sb_info *sbi,
  1927. struct free_nid *i, enum nid_state state)
  1928. {
  1929. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1930. f2fs_bug_on(sbi, state != i->state);
  1931. nm_i->nid_cnt[state]--;
  1932. if (state == FREE_NID)
  1933. list_del(&i->list);
  1934. radix_tree_delete(&nm_i->free_nid_root, i->nid);
  1935. }
  1936. static void __move_free_nid(struct f2fs_sb_info *sbi, struct free_nid *i,
  1937. enum nid_state org_state, enum nid_state dst_state)
  1938. {
  1939. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1940. f2fs_bug_on(sbi, org_state != i->state);
  1941. i->state = dst_state;
  1942. nm_i->nid_cnt[org_state]--;
  1943. nm_i->nid_cnt[dst_state]++;
  1944. switch (dst_state) {
  1945. case PREALLOC_NID:
  1946. list_del(&i->list);
  1947. break;
  1948. case FREE_NID:
  1949. list_add_tail(&i->list, &nm_i->free_nid_list);
  1950. break;
  1951. default:
  1952. BUG_ON(1);
  1953. }
  1954. }
  1955. static void update_free_nid_bitmap(struct f2fs_sb_info *sbi, nid_t nid,
  1956. bool set, bool build)
  1957. {
  1958. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1959. unsigned int nat_ofs = NAT_BLOCK_OFFSET(nid);
  1960. unsigned int nid_ofs = nid - START_NID(nid);
  1961. if (!test_bit_le(nat_ofs, nm_i->nat_block_bitmap))
  1962. return;
  1963. if (set) {
  1964. if (test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
  1965. return;
  1966. __set_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
  1967. nm_i->free_nid_count[nat_ofs]++;
  1968. } else {
  1969. if (!test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
  1970. return;
  1971. __clear_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
  1972. if (!build)
  1973. nm_i->free_nid_count[nat_ofs]--;
  1974. }
  1975. }
  1976. /* return if the nid is recognized as free */
  1977. static bool add_free_nid(struct f2fs_sb_info *sbi,
  1978. nid_t nid, bool build, bool update)
  1979. {
  1980. struct f2fs_nm_info *nm_i = NM_I(sbi);
  1981. struct free_nid *i, *e;
  1982. struct nat_entry *ne;
  1983. int err = -EINVAL;
  1984. bool ret = false;
  1985. /* 0 nid should not be used */
  1986. if (unlikely(nid == 0))
  1987. return false;
  1988. if (unlikely(f2fs_check_nid_range(sbi, nid)))
  1989. return false;
  1990. i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS, true, NULL);
  1991. i->nid = nid;
  1992. i->state = FREE_NID;
  1993. radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
  1994. spin_lock(&nm_i->nid_list_lock);
  1995. if (build) {
  1996. /*
  1997. * Thread A Thread B
  1998. * - f2fs_create
  1999. * - f2fs_new_inode
  2000. * - f2fs_alloc_nid
  2001. * - __insert_nid_to_list(PREALLOC_NID)
  2002. * - f2fs_balance_fs_bg
  2003. * - f2fs_build_free_nids
  2004. * - __f2fs_build_free_nids
  2005. * - scan_nat_page
  2006. * - add_free_nid
  2007. * - __lookup_nat_cache
  2008. * - f2fs_add_link
  2009. * - f2fs_init_inode_metadata
  2010. * - f2fs_new_inode_page
  2011. * - f2fs_new_node_page
  2012. * - set_node_addr
  2013. * - f2fs_alloc_nid_done
  2014. * - __remove_nid_from_list(PREALLOC_NID)
  2015. * - __insert_nid_to_list(FREE_NID)
  2016. */
  2017. ne = __lookup_nat_cache(nm_i, nid);
  2018. if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) ||
  2019. nat_get_blkaddr(ne) != NULL_ADDR))
  2020. goto err_out;
  2021. e = __lookup_free_nid_list(nm_i, nid);
  2022. if (e) {
  2023. if (e->state == FREE_NID)
  2024. ret = true;
  2025. goto err_out;
  2026. }
  2027. }
  2028. ret = true;
  2029. err = __insert_free_nid(sbi, i);
  2030. err_out:
  2031. if (update) {
  2032. update_free_nid_bitmap(sbi, nid, ret, build);
  2033. if (!build)
  2034. nm_i->available_nids++;
  2035. }
  2036. spin_unlock(&nm_i->nid_list_lock);
  2037. radix_tree_preload_end();
  2038. if (err)
  2039. kmem_cache_free(free_nid_slab, i);
  2040. return ret;
  2041. }
  2042. static void remove_free_nid(struct f2fs_sb_info *sbi, nid_t nid)
  2043. {
  2044. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2045. struct free_nid *i;
  2046. bool need_free = false;
  2047. spin_lock(&nm_i->nid_list_lock);
  2048. i = __lookup_free_nid_list(nm_i, nid);
  2049. if (i && i->state == FREE_NID) {
  2050. __remove_free_nid(sbi, i, FREE_NID);
  2051. need_free = true;
  2052. }
  2053. spin_unlock(&nm_i->nid_list_lock);
  2054. if (need_free)
  2055. kmem_cache_free(free_nid_slab, i);
  2056. }
  2057. static int scan_nat_page(struct f2fs_sb_info *sbi,
  2058. struct page *nat_page, nid_t start_nid)
  2059. {
  2060. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2061. struct f2fs_nat_block *nat_blk = page_address(nat_page);
  2062. block_t blk_addr;
  2063. unsigned int nat_ofs = NAT_BLOCK_OFFSET(start_nid);
  2064. int i;
  2065. __set_bit_le(nat_ofs, nm_i->nat_block_bitmap);
  2066. i = start_nid % NAT_ENTRY_PER_BLOCK;
  2067. for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
  2068. if (unlikely(start_nid >= nm_i->max_nid))
  2069. break;
  2070. blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
  2071. if (blk_addr == NEW_ADDR)
  2072. return -EFSCORRUPTED;
  2073. if (blk_addr == NULL_ADDR) {
  2074. add_free_nid(sbi, start_nid, true, true);
  2075. } else {
  2076. spin_lock(&NM_I(sbi)->nid_list_lock);
  2077. update_free_nid_bitmap(sbi, start_nid, false, true);
  2078. spin_unlock(&NM_I(sbi)->nid_list_lock);
  2079. }
  2080. }
  2081. return 0;
  2082. }
  2083. static void scan_curseg_cache(struct f2fs_sb_info *sbi)
  2084. {
  2085. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  2086. struct f2fs_journal *journal = curseg->journal;
  2087. int i;
  2088. down_read(&curseg->journal_rwsem);
  2089. for (i = 0; i < nats_in_cursum(journal); i++) {
  2090. block_t addr;
  2091. nid_t nid;
  2092. addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
  2093. nid = le32_to_cpu(nid_in_journal(journal, i));
  2094. if (addr == NULL_ADDR)
  2095. add_free_nid(sbi, nid, true, false);
  2096. else
  2097. remove_free_nid(sbi, nid);
  2098. }
  2099. up_read(&curseg->journal_rwsem);
  2100. }
  2101. static void scan_free_nid_bits(struct f2fs_sb_info *sbi)
  2102. {
  2103. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2104. unsigned int i, idx;
  2105. nid_t nid;
  2106. f2fs_down_read(&nm_i->nat_tree_lock);
  2107. for (i = 0; i < nm_i->nat_blocks; i++) {
  2108. if (!test_bit_le(i, nm_i->nat_block_bitmap))
  2109. continue;
  2110. if (!nm_i->free_nid_count[i])
  2111. continue;
  2112. for (idx = 0; idx < NAT_ENTRY_PER_BLOCK; idx++) {
  2113. idx = find_next_bit_le(nm_i->free_nid_bitmap[i],
  2114. NAT_ENTRY_PER_BLOCK, idx);
  2115. if (idx >= NAT_ENTRY_PER_BLOCK)
  2116. break;
  2117. nid = i * NAT_ENTRY_PER_BLOCK + idx;
  2118. add_free_nid(sbi, nid, true, false);
  2119. if (nm_i->nid_cnt[FREE_NID] >= MAX_FREE_NIDS)
  2120. goto out;
  2121. }
  2122. }
  2123. out:
  2124. scan_curseg_cache(sbi);
  2125. f2fs_up_read(&nm_i->nat_tree_lock);
  2126. }
  2127. static int __f2fs_build_free_nids(struct f2fs_sb_info *sbi,
  2128. bool sync, bool mount)
  2129. {
  2130. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2131. int i = 0, ret;
  2132. nid_t nid = nm_i->next_scan_nid;
  2133. if (unlikely(nid >= nm_i->max_nid))
  2134. nid = 0;
  2135. if (unlikely(nid % NAT_ENTRY_PER_BLOCK))
  2136. nid = NAT_BLOCK_OFFSET(nid) * NAT_ENTRY_PER_BLOCK;
  2137. /* Enough entries */
  2138. if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
  2139. return 0;
  2140. if (!sync && !f2fs_available_free_memory(sbi, FREE_NIDS))
  2141. return 0;
  2142. if (!mount) {
  2143. /* try to find free nids in free_nid_bitmap */
  2144. scan_free_nid_bits(sbi);
  2145. if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
  2146. return 0;
  2147. }
  2148. /* readahead nat pages to be scanned */
  2149. f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES,
  2150. META_NAT, true);
  2151. f2fs_down_read(&nm_i->nat_tree_lock);
  2152. while (1) {
  2153. if (!test_bit_le(NAT_BLOCK_OFFSET(nid),
  2154. nm_i->nat_block_bitmap)) {
  2155. struct page *page = get_current_nat_page(sbi, nid);
  2156. if (IS_ERR(page)) {
  2157. ret = PTR_ERR(page);
  2158. } else {
  2159. ret = scan_nat_page(sbi, page, nid);
  2160. f2fs_put_page(page, 1);
  2161. }
  2162. if (ret) {
  2163. f2fs_up_read(&nm_i->nat_tree_lock);
  2164. if (ret == -EFSCORRUPTED) {
  2165. f2fs_err(sbi, "NAT is corrupt, run fsck to fix it");
  2166. set_sbi_flag(sbi, SBI_NEED_FSCK);
  2167. f2fs_handle_error(sbi,
  2168. ERROR_INCONSISTENT_NAT);
  2169. }
  2170. return ret;
  2171. }
  2172. }
  2173. nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
  2174. if (unlikely(nid >= nm_i->max_nid))
  2175. nid = 0;
  2176. if (++i >= FREE_NID_PAGES)
  2177. break;
  2178. }
  2179. /* go to the next free nat pages to find free nids abundantly */
  2180. nm_i->next_scan_nid = nid;
  2181. /* find free nids from current sum_pages */
  2182. scan_curseg_cache(sbi);
  2183. f2fs_up_read(&nm_i->nat_tree_lock);
  2184. f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid),
  2185. nm_i->ra_nid_pages, META_NAT, false);
  2186. return 0;
  2187. }
  2188. int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount)
  2189. {
  2190. int ret;
  2191. mutex_lock(&NM_I(sbi)->build_lock);
  2192. ret = __f2fs_build_free_nids(sbi, sync, mount);
  2193. mutex_unlock(&NM_I(sbi)->build_lock);
  2194. return ret;
  2195. }
  2196. /*
  2197. * If this function returns success, caller can obtain a new nid
  2198. * from second parameter of this function.
  2199. * The returned nid could be used ino as well as nid when inode is created.
  2200. */
  2201. bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
  2202. {
  2203. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2204. struct free_nid *i = NULL;
  2205. retry:
  2206. if (time_to_inject(sbi, FAULT_ALLOC_NID))
  2207. return false;
  2208. spin_lock(&nm_i->nid_list_lock);
  2209. if (unlikely(nm_i->available_nids == 0)) {
  2210. spin_unlock(&nm_i->nid_list_lock);
  2211. return false;
  2212. }
  2213. /* We should not use stale free nids created by f2fs_build_free_nids */
  2214. if (nm_i->nid_cnt[FREE_NID] && !on_f2fs_build_free_nids(nm_i)) {
  2215. f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
  2216. i = list_first_entry(&nm_i->free_nid_list,
  2217. struct free_nid, list);
  2218. *nid = i->nid;
  2219. __move_free_nid(sbi, i, FREE_NID, PREALLOC_NID);
  2220. nm_i->available_nids--;
  2221. update_free_nid_bitmap(sbi, *nid, false, false);
  2222. spin_unlock(&nm_i->nid_list_lock);
  2223. return true;
  2224. }
  2225. spin_unlock(&nm_i->nid_list_lock);
  2226. /* Let's scan nat pages and its caches to get free nids */
  2227. if (!f2fs_build_free_nids(sbi, true, false))
  2228. goto retry;
  2229. return false;
  2230. }
  2231. /*
  2232. * f2fs_alloc_nid() should be called prior to this function.
  2233. */
  2234. void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
  2235. {
  2236. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2237. struct free_nid *i;
  2238. spin_lock(&nm_i->nid_list_lock);
  2239. i = __lookup_free_nid_list(nm_i, nid);
  2240. f2fs_bug_on(sbi, !i);
  2241. __remove_free_nid(sbi, i, PREALLOC_NID);
  2242. spin_unlock(&nm_i->nid_list_lock);
  2243. kmem_cache_free(free_nid_slab, i);
  2244. }
  2245. /*
  2246. * f2fs_alloc_nid() should be called prior to this function.
  2247. */
  2248. void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
  2249. {
  2250. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2251. struct free_nid *i;
  2252. bool need_free = false;
  2253. if (!nid)
  2254. return;
  2255. spin_lock(&nm_i->nid_list_lock);
  2256. i = __lookup_free_nid_list(nm_i, nid);
  2257. f2fs_bug_on(sbi, !i);
  2258. if (!f2fs_available_free_memory(sbi, FREE_NIDS)) {
  2259. __remove_free_nid(sbi, i, PREALLOC_NID);
  2260. need_free = true;
  2261. } else {
  2262. __move_free_nid(sbi, i, PREALLOC_NID, FREE_NID);
  2263. }
  2264. nm_i->available_nids++;
  2265. update_free_nid_bitmap(sbi, nid, true, false);
  2266. spin_unlock(&nm_i->nid_list_lock);
  2267. if (need_free)
  2268. kmem_cache_free(free_nid_slab, i);
  2269. }
  2270. int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
  2271. {
  2272. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2273. int nr = nr_shrink;
  2274. if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
  2275. return 0;
  2276. if (!mutex_trylock(&nm_i->build_lock))
  2277. return 0;
  2278. while (nr_shrink && nm_i->nid_cnt[FREE_NID] > MAX_FREE_NIDS) {
  2279. struct free_nid *i, *next;
  2280. unsigned int batch = SHRINK_NID_BATCH_SIZE;
  2281. spin_lock(&nm_i->nid_list_lock);
  2282. list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
  2283. if (!nr_shrink || !batch ||
  2284. nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
  2285. break;
  2286. __remove_free_nid(sbi, i, FREE_NID);
  2287. kmem_cache_free(free_nid_slab, i);
  2288. nr_shrink--;
  2289. batch--;
  2290. }
  2291. spin_unlock(&nm_i->nid_list_lock);
  2292. }
  2293. mutex_unlock(&nm_i->build_lock);
  2294. return nr - nr_shrink;
  2295. }
  2296. int f2fs_recover_inline_xattr(struct inode *inode, struct page *page)
  2297. {
  2298. void *src_addr, *dst_addr;
  2299. size_t inline_size;
  2300. struct page *ipage;
  2301. struct f2fs_inode *ri;
  2302. ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
  2303. if (IS_ERR(ipage))
  2304. return PTR_ERR(ipage);
  2305. ri = F2FS_INODE(page);
  2306. if (ri->i_inline & F2FS_INLINE_XATTR) {
  2307. if (!f2fs_has_inline_xattr(inode)) {
  2308. set_inode_flag(inode, FI_INLINE_XATTR);
  2309. stat_inc_inline_xattr(inode);
  2310. }
  2311. } else {
  2312. if (f2fs_has_inline_xattr(inode)) {
  2313. stat_dec_inline_xattr(inode);
  2314. clear_inode_flag(inode, FI_INLINE_XATTR);
  2315. }
  2316. goto update_inode;
  2317. }
  2318. dst_addr = inline_xattr_addr(inode, ipage);
  2319. src_addr = inline_xattr_addr(inode, page);
  2320. inline_size = inline_xattr_size(inode);
  2321. f2fs_wait_on_page_writeback(ipage, NODE, true, true);
  2322. memcpy(dst_addr, src_addr, inline_size);
  2323. update_inode:
  2324. f2fs_update_inode(inode, ipage);
  2325. f2fs_put_page(ipage, 1);
  2326. return 0;
  2327. }
  2328. int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
  2329. {
  2330. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  2331. nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
  2332. nid_t new_xnid;
  2333. struct dnode_of_data dn;
  2334. struct node_info ni;
  2335. struct page *xpage;
  2336. int err;
  2337. if (!prev_xnid)
  2338. goto recover_xnid;
  2339. /* 1: invalidate the previous xattr nid */
  2340. err = f2fs_get_node_info(sbi, prev_xnid, &ni, false);
  2341. if (err)
  2342. return err;
  2343. f2fs_invalidate_blocks(sbi, ni.blk_addr);
  2344. dec_valid_node_count(sbi, inode, false);
  2345. set_node_addr(sbi, &ni, NULL_ADDR, false);
  2346. recover_xnid:
  2347. /* 2: update xattr nid in inode */
  2348. if (!f2fs_alloc_nid(sbi, &new_xnid))
  2349. return -ENOSPC;
  2350. set_new_dnode(&dn, inode, NULL, NULL, new_xnid);
  2351. xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
  2352. if (IS_ERR(xpage)) {
  2353. f2fs_alloc_nid_failed(sbi, new_xnid);
  2354. return PTR_ERR(xpage);
  2355. }
  2356. f2fs_alloc_nid_done(sbi, new_xnid);
  2357. f2fs_update_inode_page(inode);
  2358. /* 3: update and set xattr node page dirty */
  2359. if (page) {
  2360. memcpy(F2FS_NODE(xpage), F2FS_NODE(page),
  2361. VALID_XATTR_BLOCK_SIZE);
  2362. set_page_dirty(xpage);
  2363. }
  2364. f2fs_put_page(xpage, 1);
  2365. return 0;
  2366. }
  2367. int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
  2368. {
  2369. struct f2fs_inode *src, *dst;
  2370. nid_t ino = ino_of_node(page);
  2371. struct node_info old_ni, new_ni;
  2372. struct page *ipage;
  2373. int err;
  2374. err = f2fs_get_node_info(sbi, ino, &old_ni, false);
  2375. if (err)
  2376. return err;
  2377. if (unlikely(old_ni.blk_addr != NULL_ADDR))
  2378. return -EINVAL;
  2379. retry:
  2380. ipage = f2fs_grab_cache_page(NODE_MAPPING(sbi), ino, false);
  2381. if (!ipage) {
  2382. memalloc_retry_wait(GFP_NOFS);
  2383. goto retry;
  2384. }
  2385. /* Should not use this inode from free nid list */
  2386. remove_free_nid(sbi, ino);
  2387. if (!PageUptodate(ipage))
  2388. SetPageUptodate(ipage);
  2389. fill_node_footer(ipage, ino, ino, 0, true);
  2390. set_cold_node(ipage, false);
  2391. src = F2FS_INODE(page);
  2392. dst = F2FS_INODE(ipage);
  2393. memcpy(dst, src, offsetof(struct f2fs_inode, i_ext));
  2394. dst->i_size = 0;
  2395. dst->i_blocks = cpu_to_le64(1);
  2396. dst->i_links = cpu_to_le32(1);
  2397. dst->i_xattr_nid = 0;
  2398. dst->i_inline = src->i_inline & (F2FS_INLINE_XATTR | F2FS_EXTRA_ATTR);
  2399. if (dst->i_inline & F2FS_EXTRA_ATTR) {
  2400. dst->i_extra_isize = src->i_extra_isize;
  2401. if (f2fs_sb_has_flexible_inline_xattr(sbi) &&
  2402. F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
  2403. i_inline_xattr_size))
  2404. dst->i_inline_xattr_size = src->i_inline_xattr_size;
  2405. if (f2fs_sb_has_project_quota(sbi) &&
  2406. F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
  2407. i_projid))
  2408. dst->i_projid = src->i_projid;
  2409. if (f2fs_sb_has_inode_crtime(sbi) &&
  2410. F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
  2411. i_crtime_nsec)) {
  2412. dst->i_crtime = src->i_crtime;
  2413. dst->i_crtime_nsec = src->i_crtime_nsec;
  2414. }
  2415. }
  2416. new_ni = old_ni;
  2417. new_ni.ino = ino;
  2418. if (unlikely(inc_valid_node_count(sbi, NULL, true)))
  2419. WARN_ON(1);
  2420. set_node_addr(sbi, &new_ni, NEW_ADDR, false);
  2421. inc_valid_inode_count(sbi);
  2422. set_page_dirty(ipage);
  2423. f2fs_put_page(ipage, 1);
  2424. return 0;
  2425. }
  2426. int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
  2427. unsigned int segno, struct f2fs_summary_block *sum)
  2428. {
  2429. struct f2fs_node *rn;
  2430. struct f2fs_summary *sum_entry;
  2431. block_t addr;
  2432. int i, idx, last_offset, nrpages;
  2433. /* scan the node segment */
  2434. last_offset = BLKS_PER_SEG(sbi);
  2435. addr = START_BLOCK(sbi, segno);
  2436. sum_entry = &sum->entries[0];
  2437. for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
  2438. nrpages = bio_max_segs(last_offset - i);
  2439. /* readahead node pages */
  2440. f2fs_ra_meta_pages(sbi, addr, nrpages, META_POR, true);
  2441. for (idx = addr; idx < addr + nrpages; idx++) {
  2442. struct page *page = f2fs_get_tmp_page(sbi, idx);
  2443. if (IS_ERR(page))
  2444. return PTR_ERR(page);
  2445. rn = F2FS_NODE(page);
  2446. sum_entry->nid = rn->footer.nid;
  2447. sum_entry->version = 0;
  2448. sum_entry->ofs_in_node = 0;
  2449. sum_entry++;
  2450. f2fs_put_page(page, 1);
  2451. }
  2452. invalidate_mapping_pages(META_MAPPING(sbi), addr,
  2453. addr + nrpages);
  2454. }
  2455. return 0;
  2456. }
  2457. static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
  2458. {
  2459. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2460. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  2461. struct f2fs_journal *journal = curseg->journal;
  2462. int i;
  2463. down_write(&curseg->journal_rwsem);
  2464. for (i = 0; i < nats_in_cursum(journal); i++) {
  2465. struct nat_entry *ne;
  2466. struct f2fs_nat_entry raw_ne;
  2467. nid_t nid = le32_to_cpu(nid_in_journal(journal, i));
  2468. if (f2fs_check_nid_range(sbi, nid))
  2469. continue;
  2470. raw_ne = nat_in_journal(journal, i);
  2471. ne = __lookup_nat_cache(nm_i, nid);
  2472. if (!ne) {
  2473. ne = __alloc_nat_entry(sbi, nid, true);
  2474. __init_nat_entry(nm_i, ne, &raw_ne, true);
  2475. }
  2476. /*
  2477. * if a free nat in journal has not been used after last
  2478. * checkpoint, we should remove it from available nids,
  2479. * since later we will add it again.
  2480. */
  2481. if (!get_nat_flag(ne, IS_DIRTY) &&
  2482. le32_to_cpu(raw_ne.block_addr) == NULL_ADDR) {
  2483. spin_lock(&nm_i->nid_list_lock);
  2484. nm_i->available_nids--;
  2485. spin_unlock(&nm_i->nid_list_lock);
  2486. }
  2487. __set_nat_cache_dirty(nm_i, ne);
  2488. }
  2489. update_nats_in_cursum(journal, -i);
  2490. up_write(&curseg->journal_rwsem);
  2491. }
  2492. static void __adjust_nat_entry_set(struct nat_entry_set *nes,
  2493. struct list_head *head, int max)
  2494. {
  2495. struct nat_entry_set *cur;
  2496. if (nes->entry_cnt >= max)
  2497. goto add_out;
  2498. list_for_each_entry(cur, head, set_list) {
  2499. if (cur->entry_cnt >= nes->entry_cnt) {
  2500. list_add(&nes->set_list, cur->set_list.prev);
  2501. return;
  2502. }
  2503. }
  2504. add_out:
  2505. list_add_tail(&nes->set_list, head);
  2506. }
  2507. static void __update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid,
  2508. struct page *page)
  2509. {
  2510. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2511. unsigned int nat_index = start_nid / NAT_ENTRY_PER_BLOCK;
  2512. struct f2fs_nat_block *nat_blk = page_address(page);
  2513. int valid = 0;
  2514. int i = 0;
  2515. if (!enabled_nat_bits(sbi, NULL))
  2516. return;
  2517. if (nat_index == 0) {
  2518. valid = 1;
  2519. i = 1;
  2520. }
  2521. for (; i < NAT_ENTRY_PER_BLOCK; i++) {
  2522. if (le32_to_cpu(nat_blk->entries[i].block_addr) != NULL_ADDR)
  2523. valid++;
  2524. }
  2525. if (valid == 0) {
  2526. __set_bit_le(nat_index, nm_i->empty_nat_bits);
  2527. __clear_bit_le(nat_index, nm_i->full_nat_bits);
  2528. return;
  2529. }
  2530. __clear_bit_le(nat_index, nm_i->empty_nat_bits);
  2531. if (valid == NAT_ENTRY_PER_BLOCK)
  2532. __set_bit_le(nat_index, nm_i->full_nat_bits);
  2533. else
  2534. __clear_bit_le(nat_index, nm_i->full_nat_bits);
  2535. }
  2536. static int __flush_nat_entry_set(struct f2fs_sb_info *sbi,
  2537. struct nat_entry_set *set, struct cp_control *cpc)
  2538. {
  2539. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  2540. struct f2fs_journal *journal = curseg->journal;
  2541. nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
  2542. bool to_journal = true;
  2543. struct f2fs_nat_block *nat_blk;
  2544. struct nat_entry *ne, *cur;
  2545. struct page *page = NULL;
  2546. /*
  2547. * there are two steps to flush nat entries:
  2548. * #1, flush nat entries to journal in current hot data summary block.
  2549. * #2, flush nat entries to nat page.
  2550. */
  2551. if (enabled_nat_bits(sbi, cpc) ||
  2552. !__has_cursum_space(journal, set->entry_cnt, NAT_JOURNAL))
  2553. to_journal = false;
  2554. if (to_journal) {
  2555. down_write(&curseg->journal_rwsem);
  2556. } else {
  2557. page = get_next_nat_page(sbi, start_nid);
  2558. if (IS_ERR(page))
  2559. return PTR_ERR(page);
  2560. nat_blk = page_address(page);
  2561. f2fs_bug_on(sbi, !nat_blk);
  2562. }
  2563. /* flush dirty nats in nat entry set */
  2564. list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
  2565. struct f2fs_nat_entry *raw_ne;
  2566. nid_t nid = nat_get_nid(ne);
  2567. int offset;
  2568. f2fs_bug_on(sbi, nat_get_blkaddr(ne) == NEW_ADDR);
  2569. if (to_journal) {
  2570. offset = f2fs_lookup_journal_in_cursum(journal,
  2571. NAT_JOURNAL, nid, 1);
  2572. f2fs_bug_on(sbi, offset < 0);
  2573. raw_ne = &nat_in_journal(journal, offset);
  2574. nid_in_journal(journal, offset) = cpu_to_le32(nid);
  2575. } else {
  2576. raw_ne = &nat_blk->entries[nid - start_nid];
  2577. }
  2578. raw_nat_from_node_info(raw_ne, &ne->ni);
  2579. nat_reset_flag(ne);
  2580. __clear_nat_cache_dirty(NM_I(sbi), set, ne);
  2581. if (nat_get_blkaddr(ne) == NULL_ADDR) {
  2582. add_free_nid(sbi, nid, false, true);
  2583. } else {
  2584. spin_lock(&NM_I(sbi)->nid_list_lock);
  2585. update_free_nid_bitmap(sbi, nid, false, false);
  2586. spin_unlock(&NM_I(sbi)->nid_list_lock);
  2587. }
  2588. }
  2589. if (to_journal) {
  2590. up_write(&curseg->journal_rwsem);
  2591. } else {
  2592. __update_nat_bits(sbi, start_nid, page);
  2593. f2fs_put_page(page, 1);
  2594. }
  2595. /* Allow dirty nats by node block allocation in write_begin */
  2596. if (!set->entry_cnt) {
  2597. radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
  2598. kmem_cache_free(nat_entry_set_slab, set);
  2599. }
  2600. return 0;
  2601. }
  2602. /*
  2603. * This function is called during the checkpointing process.
  2604. */
  2605. int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  2606. {
  2607. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2608. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
  2609. struct f2fs_journal *journal = curseg->journal;
  2610. struct nat_entry_set *setvec[NAT_VEC_SIZE];
  2611. struct nat_entry_set *set, *tmp;
  2612. unsigned int found;
  2613. nid_t set_idx = 0;
  2614. LIST_HEAD(sets);
  2615. int err = 0;
  2616. /*
  2617. * during unmount, let's flush nat_bits before checking
  2618. * nat_cnt[DIRTY_NAT].
  2619. */
  2620. if (enabled_nat_bits(sbi, cpc)) {
  2621. f2fs_down_write(&nm_i->nat_tree_lock);
  2622. remove_nats_in_journal(sbi);
  2623. f2fs_up_write(&nm_i->nat_tree_lock);
  2624. }
  2625. if (!nm_i->nat_cnt[DIRTY_NAT])
  2626. return 0;
  2627. f2fs_down_write(&nm_i->nat_tree_lock);
  2628. /*
  2629. * if there are no enough space in journal to store dirty nat
  2630. * entries, remove all entries from journal and merge them
  2631. * into nat entry set.
  2632. */
  2633. if (enabled_nat_bits(sbi, cpc) ||
  2634. !__has_cursum_space(journal,
  2635. nm_i->nat_cnt[DIRTY_NAT], NAT_JOURNAL))
  2636. remove_nats_in_journal(sbi);
  2637. while ((found = __gang_lookup_nat_set(nm_i,
  2638. set_idx, NAT_VEC_SIZE, setvec))) {
  2639. unsigned idx;
  2640. set_idx = setvec[found - 1]->set + 1;
  2641. for (idx = 0; idx < found; idx++)
  2642. __adjust_nat_entry_set(setvec[idx], &sets,
  2643. MAX_NAT_JENTRIES(journal));
  2644. }
  2645. /* flush dirty nats in nat entry set */
  2646. list_for_each_entry_safe(set, tmp, &sets, set_list) {
  2647. err = __flush_nat_entry_set(sbi, set, cpc);
  2648. if (err)
  2649. break;
  2650. }
  2651. f2fs_up_write(&nm_i->nat_tree_lock);
  2652. /* Allow dirty nats by node block allocation in write_begin */
  2653. return err;
  2654. }
  2655. static int __get_nat_bitmaps(struct f2fs_sb_info *sbi)
  2656. {
  2657. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  2658. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2659. unsigned int nat_bits_bytes = nm_i->nat_blocks / BITS_PER_BYTE;
  2660. unsigned int i;
  2661. __u64 cp_ver = cur_cp_version(ckpt);
  2662. block_t nat_bits_addr;
  2663. if (!enabled_nat_bits(sbi, NULL))
  2664. return 0;
  2665. nm_i->nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
  2666. nm_i->nat_bits = f2fs_kvzalloc(sbi,
  2667. F2FS_BLK_TO_BYTES(nm_i->nat_bits_blocks), GFP_KERNEL);
  2668. if (!nm_i->nat_bits)
  2669. return -ENOMEM;
  2670. nat_bits_addr = __start_cp_addr(sbi) + BLKS_PER_SEG(sbi) -
  2671. nm_i->nat_bits_blocks;
  2672. for (i = 0; i < nm_i->nat_bits_blocks; i++) {
  2673. struct page *page;
  2674. page = f2fs_get_meta_page(sbi, nat_bits_addr++);
  2675. if (IS_ERR(page))
  2676. return PTR_ERR(page);
  2677. memcpy(nm_i->nat_bits + F2FS_BLK_TO_BYTES(i),
  2678. page_address(page), F2FS_BLKSIZE);
  2679. f2fs_put_page(page, 1);
  2680. }
  2681. cp_ver |= (cur_cp_crc(ckpt) << 32);
  2682. if (cpu_to_le64(cp_ver) != *(__le64 *)nm_i->nat_bits) {
  2683. disable_nat_bits(sbi, true);
  2684. return 0;
  2685. }
  2686. nm_i->full_nat_bits = nm_i->nat_bits + 8;
  2687. nm_i->empty_nat_bits = nm_i->full_nat_bits + nat_bits_bytes;
  2688. f2fs_notice(sbi, "Found nat_bits in checkpoint");
  2689. return 0;
  2690. }
  2691. static inline void load_free_nid_bitmap(struct f2fs_sb_info *sbi)
  2692. {
  2693. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2694. unsigned int i = 0;
  2695. nid_t nid, last_nid;
  2696. if (!enabled_nat_bits(sbi, NULL))
  2697. return;
  2698. for (i = 0; i < nm_i->nat_blocks; i++) {
  2699. i = find_next_bit_le(nm_i->empty_nat_bits, nm_i->nat_blocks, i);
  2700. if (i >= nm_i->nat_blocks)
  2701. break;
  2702. __set_bit_le(i, nm_i->nat_block_bitmap);
  2703. nid = i * NAT_ENTRY_PER_BLOCK;
  2704. last_nid = nid + NAT_ENTRY_PER_BLOCK;
  2705. spin_lock(&NM_I(sbi)->nid_list_lock);
  2706. for (; nid < last_nid; nid++)
  2707. update_free_nid_bitmap(sbi, nid, true, true);
  2708. spin_unlock(&NM_I(sbi)->nid_list_lock);
  2709. }
  2710. for (i = 0; i < nm_i->nat_blocks; i++) {
  2711. i = find_next_bit_le(nm_i->full_nat_bits, nm_i->nat_blocks, i);
  2712. if (i >= nm_i->nat_blocks)
  2713. break;
  2714. __set_bit_le(i, nm_i->nat_block_bitmap);
  2715. }
  2716. }
  2717. static int init_node_manager(struct f2fs_sb_info *sbi)
  2718. {
  2719. struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
  2720. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2721. unsigned char *version_bitmap;
  2722. unsigned int nat_segs;
  2723. int err;
  2724. nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
  2725. /* segment_count_nat includes pair segment so divide to 2. */
  2726. nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
  2727. nm_i->nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
  2728. nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks;
  2729. /* not used nids: 0, node, meta, (and root counted as valid node) */
  2730. nm_i->available_nids = nm_i->max_nid - sbi->total_valid_node_count -
  2731. F2FS_RESERVED_NODE_NUM;
  2732. nm_i->nid_cnt[FREE_NID] = 0;
  2733. nm_i->nid_cnt[PREALLOC_NID] = 0;
  2734. nm_i->ram_thresh = DEF_RAM_THRESHOLD;
  2735. nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
  2736. nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD;
  2737. nm_i->max_rf_node_blocks = DEF_RF_NODE_BLOCKS;
  2738. INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
  2739. INIT_LIST_HEAD(&nm_i->free_nid_list);
  2740. INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
  2741. INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
  2742. INIT_LIST_HEAD(&nm_i->nat_entries);
  2743. spin_lock_init(&nm_i->nat_list_lock);
  2744. mutex_init(&nm_i->build_lock);
  2745. spin_lock_init(&nm_i->nid_list_lock);
  2746. init_f2fs_rwsem(&nm_i->nat_tree_lock);
  2747. nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
  2748. nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
  2749. version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
  2750. nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
  2751. GFP_KERNEL);
  2752. if (!nm_i->nat_bitmap)
  2753. return -ENOMEM;
  2754. err = __get_nat_bitmaps(sbi);
  2755. if (err)
  2756. return err;
  2757. #ifdef CONFIG_F2FS_CHECK_FS
  2758. nm_i->nat_bitmap_mir = kmemdup(version_bitmap, nm_i->bitmap_size,
  2759. GFP_KERNEL);
  2760. if (!nm_i->nat_bitmap_mir)
  2761. return -ENOMEM;
  2762. #endif
  2763. return 0;
  2764. }
  2765. static int init_free_nid_cache(struct f2fs_sb_info *sbi)
  2766. {
  2767. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2768. int i;
  2769. nm_i->free_nid_bitmap =
  2770. f2fs_kvzalloc(sbi, array_size(sizeof(unsigned char *),
  2771. nm_i->nat_blocks),
  2772. GFP_KERNEL);
  2773. if (!nm_i->free_nid_bitmap)
  2774. return -ENOMEM;
  2775. for (i = 0; i < nm_i->nat_blocks; i++) {
  2776. nm_i->free_nid_bitmap[i] = f2fs_kvzalloc(sbi,
  2777. f2fs_bitmap_size(NAT_ENTRY_PER_BLOCK), GFP_KERNEL);
  2778. if (!nm_i->free_nid_bitmap[i])
  2779. return -ENOMEM;
  2780. }
  2781. nm_i->nat_block_bitmap = f2fs_kvzalloc(sbi, nm_i->nat_blocks / 8,
  2782. GFP_KERNEL);
  2783. if (!nm_i->nat_block_bitmap)
  2784. return -ENOMEM;
  2785. nm_i->free_nid_count =
  2786. f2fs_kvzalloc(sbi, array_size(sizeof(unsigned short),
  2787. nm_i->nat_blocks),
  2788. GFP_KERNEL);
  2789. if (!nm_i->free_nid_count)
  2790. return -ENOMEM;
  2791. return 0;
  2792. }
  2793. int f2fs_build_node_manager(struct f2fs_sb_info *sbi)
  2794. {
  2795. int err;
  2796. sbi->nm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_nm_info),
  2797. GFP_KERNEL);
  2798. if (!sbi->nm_info)
  2799. return -ENOMEM;
  2800. err = init_node_manager(sbi);
  2801. if (err)
  2802. return err;
  2803. err = init_free_nid_cache(sbi);
  2804. if (err)
  2805. return err;
  2806. /* load free nid status from nat_bits table */
  2807. load_free_nid_bitmap(sbi);
  2808. return f2fs_build_free_nids(sbi, true, true);
  2809. }
  2810. void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi)
  2811. {
  2812. struct f2fs_nm_info *nm_i = NM_I(sbi);
  2813. struct free_nid *i, *next_i;
  2814. void *vec[NAT_VEC_SIZE];
  2815. struct nat_entry **natvec = (struct nat_entry **)vec;
  2816. struct nat_entry_set **setvec = (struct nat_entry_set **)vec;
  2817. nid_t nid = 0;
  2818. unsigned int found;
  2819. if (!nm_i)
  2820. return;
  2821. /* destroy free nid list */
  2822. spin_lock(&nm_i->nid_list_lock);
  2823. list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
  2824. __remove_free_nid(sbi, i, FREE_NID);
  2825. spin_unlock(&nm_i->nid_list_lock);
  2826. kmem_cache_free(free_nid_slab, i);
  2827. spin_lock(&nm_i->nid_list_lock);
  2828. }
  2829. f2fs_bug_on(sbi, nm_i->nid_cnt[FREE_NID]);
  2830. f2fs_bug_on(sbi, nm_i->nid_cnt[PREALLOC_NID]);
  2831. f2fs_bug_on(sbi, !list_empty(&nm_i->free_nid_list));
  2832. spin_unlock(&nm_i->nid_list_lock);
  2833. /* destroy nat cache */
  2834. f2fs_down_write(&nm_i->nat_tree_lock);
  2835. while ((found = __gang_lookup_nat_cache(nm_i,
  2836. nid, NAT_VEC_SIZE, natvec))) {
  2837. unsigned idx;
  2838. nid = nat_get_nid(natvec[found - 1]) + 1;
  2839. for (idx = 0; idx < found; idx++) {
  2840. spin_lock(&nm_i->nat_list_lock);
  2841. list_del(&natvec[idx]->list);
  2842. spin_unlock(&nm_i->nat_list_lock);
  2843. __del_from_nat_cache(nm_i, natvec[idx]);
  2844. }
  2845. }
  2846. f2fs_bug_on(sbi, nm_i->nat_cnt[TOTAL_NAT]);
  2847. /* destroy nat set cache */
  2848. nid = 0;
  2849. memset(vec, 0, sizeof(void *) * NAT_VEC_SIZE);
  2850. while ((found = __gang_lookup_nat_set(nm_i,
  2851. nid, NAT_VEC_SIZE, setvec))) {
  2852. unsigned idx;
  2853. nid = setvec[found - 1]->set + 1;
  2854. for (idx = 0; idx < found; idx++) {
  2855. /* entry_cnt is not zero, when cp_error was occurred */
  2856. f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
  2857. radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
  2858. kmem_cache_free(nat_entry_set_slab, setvec[idx]);
  2859. }
  2860. }
  2861. f2fs_up_write(&nm_i->nat_tree_lock);
  2862. kvfree(nm_i->nat_block_bitmap);
  2863. if (nm_i->free_nid_bitmap) {
  2864. int i;
  2865. for (i = 0; i < nm_i->nat_blocks; i++)
  2866. kvfree(nm_i->free_nid_bitmap[i]);
  2867. kvfree(nm_i->free_nid_bitmap);
  2868. }
  2869. kvfree(nm_i->free_nid_count);
  2870. kvfree(nm_i->nat_bitmap);
  2871. kvfree(nm_i->nat_bits);
  2872. #ifdef CONFIG_F2FS_CHECK_FS
  2873. kvfree(nm_i->nat_bitmap_mir);
  2874. #endif
  2875. sbi->nm_info = NULL;
  2876. kfree(nm_i);
  2877. }
  2878. int __init f2fs_create_node_manager_caches(void)
  2879. {
  2880. nat_entry_slab = f2fs_kmem_cache_create("f2fs_nat_entry",
  2881. sizeof(struct nat_entry));
  2882. if (!nat_entry_slab)
  2883. goto fail;
  2884. free_nid_slab = f2fs_kmem_cache_create("f2fs_free_nid",
  2885. sizeof(struct free_nid));
  2886. if (!free_nid_slab)
  2887. goto destroy_nat_entry;
  2888. nat_entry_set_slab = f2fs_kmem_cache_create("f2fs_nat_entry_set",
  2889. sizeof(struct nat_entry_set));
  2890. if (!nat_entry_set_slab)
  2891. goto destroy_free_nid;
  2892. fsync_node_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_node_entry",
  2893. sizeof(struct fsync_node_entry));
  2894. if (!fsync_node_entry_slab)
  2895. goto destroy_nat_entry_set;
  2896. return 0;
  2897. destroy_nat_entry_set:
  2898. kmem_cache_destroy(nat_entry_set_slab);
  2899. destroy_free_nid:
  2900. kmem_cache_destroy(free_nid_slab);
  2901. destroy_nat_entry:
  2902. kmem_cache_destroy(nat_entry_slab);
  2903. fail:
  2904. return -ENOMEM;
  2905. }
  2906. void f2fs_destroy_node_manager_caches(void)
  2907. {
  2908. kmem_cache_destroy(fsync_node_entry_slab);
  2909. kmem_cache_destroy(nat_entry_set_slab);
  2910. kmem_cache_destroy(free_nid_slab);
  2911. kmem_cache_destroy(nat_entry_slab);
  2912. }