data.c 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/f2fs/data.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/sched/mm.h>
  11. #include <linux/mpage.h>
  12. #include <linux/writeback.h>
  13. #include <linux/pagevec.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/bio.h>
  16. #include <linux/blk-crypto.h>
  17. #include <linux/swap.h>
  18. #include <linux/prefetch.h>
  19. #include <linux/uio.h>
  20. #include <linux/sched/signal.h>
  21. #include <linux/fiemap.h>
  22. #include <linux/iomap.h>
  23. #include "f2fs.h"
  24. #include "node.h"
  25. #include "segment.h"
  26. #include "iostat.h"
  27. #include <trace/events/f2fs.h>
  28. #define NUM_PREALLOC_POST_READ_CTXS 128
  29. static struct kmem_cache *bio_post_read_ctx_cache;
  30. static struct kmem_cache *bio_entry_slab;
  31. static mempool_t *bio_post_read_ctx_pool;
  32. static struct bio_set f2fs_bioset;
  33. #define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE
  34. int __init f2fs_init_bioset(void)
  35. {
  36. return bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE,
  37. 0, BIOSET_NEED_BVECS);
  38. }
  39. void f2fs_destroy_bioset(void)
  40. {
  41. bioset_exit(&f2fs_bioset);
  42. }
  43. bool f2fs_is_cp_guaranteed(struct page *page)
  44. {
  45. struct address_space *mapping = page->mapping;
  46. struct inode *inode;
  47. struct f2fs_sb_info *sbi;
  48. if (!mapping)
  49. return false;
  50. inode = mapping->host;
  51. sbi = F2FS_I_SB(inode);
  52. if (inode->i_ino == F2FS_META_INO(sbi) ||
  53. inode->i_ino == F2FS_NODE_INO(sbi) ||
  54. S_ISDIR(inode->i_mode))
  55. return true;
  56. if ((S_ISREG(inode->i_mode) && IS_NOQUOTA(inode)) ||
  57. page_private_gcing(page))
  58. return true;
  59. return false;
  60. }
  61. static enum count_type __read_io_type(struct page *page)
  62. {
  63. struct address_space *mapping = page_file_mapping(page);
  64. if (mapping) {
  65. struct inode *inode = mapping->host;
  66. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  67. if (inode->i_ino == F2FS_META_INO(sbi))
  68. return F2FS_RD_META;
  69. if (inode->i_ino == F2FS_NODE_INO(sbi))
  70. return F2FS_RD_NODE;
  71. }
  72. return F2FS_RD_DATA;
  73. }
  74. /* postprocessing steps for read bios */
  75. enum bio_post_read_step {
  76. #ifdef CONFIG_FS_ENCRYPTION
  77. STEP_DECRYPT = BIT(0),
  78. #else
  79. STEP_DECRYPT = 0, /* compile out the decryption-related code */
  80. #endif
  81. #ifdef CONFIG_F2FS_FS_COMPRESSION
  82. STEP_DECOMPRESS = BIT(1),
  83. #else
  84. STEP_DECOMPRESS = 0, /* compile out the decompression-related code */
  85. #endif
  86. #ifdef CONFIG_FS_VERITY
  87. STEP_VERITY = BIT(2),
  88. #else
  89. STEP_VERITY = 0, /* compile out the verity-related code */
  90. #endif
  91. };
  92. struct bio_post_read_ctx {
  93. struct bio *bio;
  94. struct f2fs_sb_info *sbi;
  95. struct work_struct work;
  96. unsigned int enabled_steps;
  97. /*
  98. * decompression_attempted keeps track of whether
  99. * f2fs_end_read_compressed_page() has been called on the pages in the
  100. * bio that belong to a compressed cluster yet.
  101. */
  102. bool decompression_attempted;
  103. block_t fs_blkaddr;
  104. };
  105. /*
  106. * Update and unlock a bio's pages, and free the bio.
  107. *
  108. * This marks pages up-to-date only if there was no error in the bio (I/O error,
  109. * decryption error, or verity error), as indicated by bio->bi_status.
  110. *
  111. * "Compressed pages" (pagecache pages backed by a compressed cluster on-disk)
  112. * aren't marked up-to-date here, as decompression is done on a per-compression-
  113. * cluster basis rather than a per-bio basis. Instead, we only must do two
  114. * things for each compressed page here: call f2fs_end_read_compressed_page()
  115. * with failed=true if an error occurred before it would have normally gotten
  116. * called (i.e., I/O error or decryption error, but *not* verity error), and
  117. * release the bio's reference to the decompress_io_ctx of the page's cluster.
  118. */
  119. static void f2fs_finish_read_bio(struct bio *bio, bool in_task)
  120. {
  121. struct bio_vec *bv;
  122. struct bvec_iter_all iter_all;
  123. struct bio_post_read_ctx *ctx = bio->bi_private;
  124. bio_for_each_segment_all(bv, bio, iter_all) {
  125. struct page *page = bv->bv_page;
  126. if (f2fs_is_compressed_page(page)) {
  127. if (ctx && !ctx->decompression_attempted)
  128. f2fs_end_read_compressed_page(page, true, 0,
  129. in_task);
  130. f2fs_put_page_dic(page, in_task);
  131. continue;
  132. }
  133. if (bio->bi_status)
  134. ClearPageUptodate(page);
  135. else
  136. SetPageUptodate(page);
  137. dec_page_count(F2FS_P_SB(page), __read_io_type(page));
  138. unlock_page(page);
  139. }
  140. if (ctx)
  141. mempool_free(ctx, bio_post_read_ctx_pool);
  142. bio_put(bio);
  143. }
  144. static void f2fs_verify_bio(struct work_struct *work)
  145. {
  146. struct bio_post_read_ctx *ctx =
  147. container_of(work, struct bio_post_read_ctx, work);
  148. struct bio *bio = ctx->bio;
  149. bool may_have_compressed_pages = (ctx->enabled_steps & STEP_DECOMPRESS);
  150. /*
  151. * fsverity_verify_bio() may call readahead() again, and while verity
  152. * will be disabled for this, decryption and/or decompression may still
  153. * be needed, resulting in another bio_post_read_ctx being allocated.
  154. * So to prevent deadlocks we need to release the current ctx to the
  155. * mempool first. This assumes that verity is the last post-read step.
  156. */
  157. mempool_free(ctx, bio_post_read_ctx_pool);
  158. bio->bi_private = NULL;
  159. /*
  160. * Verify the bio's pages with fs-verity. Exclude compressed pages,
  161. * as those were handled separately by f2fs_end_read_compressed_page().
  162. */
  163. if (may_have_compressed_pages) {
  164. struct bio_vec *bv;
  165. struct bvec_iter_all iter_all;
  166. bio_for_each_segment_all(bv, bio, iter_all) {
  167. struct page *page = bv->bv_page;
  168. if (!f2fs_is_compressed_page(page) &&
  169. !fsverity_verify_page(page)) {
  170. bio->bi_status = BLK_STS_IOERR;
  171. break;
  172. }
  173. }
  174. } else {
  175. fsverity_verify_bio(bio);
  176. }
  177. f2fs_finish_read_bio(bio, true);
  178. }
  179. /*
  180. * If the bio's data needs to be verified with fs-verity, then enqueue the
  181. * verity work for the bio. Otherwise finish the bio now.
  182. *
  183. * Note that to avoid deadlocks, the verity work can't be done on the
  184. * decryption/decompression workqueue. This is because verifying the data pages
  185. * can involve reading verity metadata pages from the file, and these verity
  186. * metadata pages may be encrypted and/or compressed.
  187. */
  188. static void f2fs_verify_and_finish_bio(struct bio *bio, bool in_task)
  189. {
  190. struct bio_post_read_ctx *ctx = bio->bi_private;
  191. if (ctx && (ctx->enabled_steps & STEP_VERITY)) {
  192. INIT_WORK(&ctx->work, f2fs_verify_bio);
  193. fsverity_enqueue_verify_work(&ctx->work);
  194. } else {
  195. f2fs_finish_read_bio(bio, in_task);
  196. }
  197. }
  198. /*
  199. * Handle STEP_DECOMPRESS by decompressing any compressed clusters whose last
  200. * remaining page was read by @ctx->bio.
  201. *
  202. * Note that a bio may span clusters (even a mix of compressed and uncompressed
  203. * clusters) or be for just part of a cluster. STEP_DECOMPRESS just indicates
  204. * that the bio includes at least one compressed page. The actual decompression
  205. * is done on a per-cluster basis, not a per-bio basis.
  206. */
  207. static void f2fs_handle_step_decompress(struct bio_post_read_ctx *ctx,
  208. bool in_task)
  209. {
  210. struct bio_vec *bv;
  211. struct bvec_iter_all iter_all;
  212. bool all_compressed = true;
  213. block_t blkaddr = ctx->fs_blkaddr;
  214. bio_for_each_segment_all(bv, ctx->bio, iter_all) {
  215. struct page *page = bv->bv_page;
  216. if (f2fs_is_compressed_page(page))
  217. f2fs_end_read_compressed_page(page, false, blkaddr,
  218. in_task);
  219. else
  220. all_compressed = false;
  221. blkaddr++;
  222. }
  223. ctx->decompression_attempted = true;
  224. /*
  225. * Optimization: if all the bio's pages are compressed, then scheduling
  226. * the per-bio verity work is unnecessary, as verity will be fully
  227. * handled at the compression cluster level.
  228. */
  229. if (all_compressed)
  230. ctx->enabled_steps &= ~STEP_VERITY;
  231. }
  232. static void f2fs_post_read_work(struct work_struct *work)
  233. {
  234. struct bio_post_read_ctx *ctx =
  235. container_of(work, struct bio_post_read_ctx, work);
  236. struct bio *bio = ctx->bio;
  237. if ((ctx->enabled_steps & STEP_DECRYPT) && !fscrypt_decrypt_bio(bio)) {
  238. f2fs_finish_read_bio(bio, true);
  239. return;
  240. }
  241. if (ctx->enabled_steps & STEP_DECOMPRESS)
  242. f2fs_handle_step_decompress(ctx, true);
  243. f2fs_verify_and_finish_bio(bio, true);
  244. }
  245. static void f2fs_read_end_io(struct bio *bio)
  246. {
  247. struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
  248. struct bio_post_read_ctx *ctx;
  249. bool intask = in_task();
  250. iostat_update_and_unbind_ctx(bio);
  251. ctx = bio->bi_private;
  252. if (time_to_inject(sbi, FAULT_READ_IO))
  253. bio->bi_status = BLK_STS_IOERR;
  254. if (bio->bi_status) {
  255. f2fs_finish_read_bio(bio, intask);
  256. return;
  257. }
  258. if (ctx) {
  259. unsigned int enabled_steps = ctx->enabled_steps &
  260. (STEP_DECRYPT | STEP_DECOMPRESS);
  261. /*
  262. * If we have only decompression step between decompression and
  263. * decrypt, we don't need post processing for this.
  264. */
  265. if (enabled_steps == STEP_DECOMPRESS &&
  266. !f2fs_low_mem_mode(sbi)) {
  267. f2fs_handle_step_decompress(ctx, intask);
  268. } else if (enabled_steps) {
  269. INIT_WORK(&ctx->work, f2fs_post_read_work);
  270. queue_work(ctx->sbi->post_read_wq, &ctx->work);
  271. return;
  272. }
  273. }
  274. f2fs_verify_and_finish_bio(bio, intask);
  275. }
  276. static void f2fs_write_end_io(struct bio *bio)
  277. {
  278. struct f2fs_sb_info *sbi;
  279. struct bio_vec *bvec;
  280. struct bvec_iter_all iter_all;
  281. iostat_update_and_unbind_ctx(bio);
  282. sbi = bio->bi_private;
  283. if (time_to_inject(sbi, FAULT_WRITE_IO))
  284. bio->bi_status = BLK_STS_IOERR;
  285. bio_for_each_segment_all(bvec, bio, iter_all) {
  286. struct page *page = bvec->bv_page;
  287. enum count_type type = WB_DATA_TYPE(page, false);
  288. fscrypt_finalize_bounce_page(&page);
  289. #ifdef CONFIG_F2FS_FS_COMPRESSION
  290. if (f2fs_is_compressed_page(page)) {
  291. f2fs_compress_write_end_io(bio, page);
  292. continue;
  293. }
  294. #endif
  295. if (unlikely(bio->bi_status)) {
  296. mapping_set_error(page->mapping, -EIO);
  297. if (type == F2FS_WB_CP_DATA)
  298. f2fs_stop_checkpoint(sbi, true,
  299. STOP_CP_REASON_WRITE_FAIL);
  300. }
  301. f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
  302. page_folio(page)->index != nid_of_node(page));
  303. dec_page_count(sbi, type);
  304. if (f2fs_in_warm_node_list(sbi, page))
  305. f2fs_del_fsync_node_entry(sbi, page);
  306. clear_page_private_gcing(page);
  307. end_page_writeback(page);
  308. }
  309. if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
  310. wq_has_sleeper(&sbi->cp_wait))
  311. wake_up(&sbi->cp_wait);
  312. bio_put(bio);
  313. }
  314. #ifdef CONFIG_BLK_DEV_ZONED
  315. static void f2fs_zone_write_end_io(struct bio *bio)
  316. {
  317. struct f2fs_bio_info *io = (struct f2fs_bio_info *)bio->bi_private;
  318. bio->bi_private = io->bi_private;
  319. complete(&io->zone_wait);
  320. f2fs_write_end_io(bio);
  321. }
  322. #endif
  323. struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
  324. block_t blk_addr, sector_t *sector)
  325. {
  326. struct block_device *bdev = sbi->sb->s_bdev;
  327. int i;
  328. if (f2fs_is_multi_device(sbi)) {
  329. for (i = 0; i < sbi->s_ndevs; i++) {
  330. if (FDEV(i).start_blk <= blk_addr &&
  331. FDEV(i).end_blk >= blk_addr) {
  332. blk_addr -= FDEV(i).start_blk;
  333. bdev = FDEV(i).bdev;
  334. break;
  335. }
  336. }
  337. }
  338. if (sector)
  339. *sector = SECTOR_FROM_BLOCK(blk_addr);
  340. return bdev;
  341. }
  342. int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
  343. {
  344. int i;
  345. if (!f2fs_is_multi_device(sbi))
  346. return 0;
  347. for (i = 0; i < sbi->s_ndevs; i++)
  348. if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
  349. return i;
  350. return 0;
  351. }
  352. static blk_opf_t f2fs_io_flags(struct f2fs_io_info *fio)
  353. {
  354. unsigned int temp_mask = GENMASK(NR_TEMP_TYPE - 1, 0);
  355. unsigned int fua_flag, meta_flag, io_flag;
  356. blk_opf_t op_flags = 0;
  357. if (fio->op != REQ_OP_WRITE)
  358. return 0;
  359. if (fio->type == DATA)
  360. io_flag = fio->sbi->data_io_flag;
  361. else if (fio->type == NODE)
  362. io_flag = fio->sbi->node_io_flag;
  363. else
  364. return 0;
  365. fua_flag = io_flag & temp_mask;
  366. meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask;
  367. /*
  368. * data/node io flag bits per temp:
  369. * REQ_META | REQ_FUA |
  370. * 5 | 4 | 3 | 2 | 1 | 0 |
  371. * Cold | Warm | Hot | Cold | Warm | Hot |
  372. */
  373. if (BIT(fio->temp) & meta_flag)
  374. op_flags |= REQ_META;
  375. if (BIT(fio->temp) & fua_flag)
  376. op_flags |= REQ_FUA;
  377. return op_flags;
  378. }
  379. static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
  380. {
  381. struct f2fs_sb_info *sbi = fio->sbi;
  382. struct block_device *bdev;
  383. sector_t sector;
  384. struct bio *bio;
  385. bdev = f2fs_target_device(sbi, fio->new_blkaddr, &sector);
  386. bio = bio_alloc_bioset(bdev, npages,
  387. fio->op | fio->op_flags | f2fs_io_flags(fio),
  388. GFP_NOIO, &f2fs_bioset);
  389. bio->bi_iter.bi_sector = sector;
  390. if (is_read_io(fio->op)) {
  391. bio->bi_end_io = f2fs_read_end_io;
  392. bio->bi_private = NULL;
  393. } else {
  394. bio->bi_end_io = f2fs_write_end_io;
  395. bio->bi_private = sbi;
  396. bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
  397. fio->type, fio->temp);
  398. }
  399. iostat_alloc_and_bind_ctx(sbi, bio, NULL);
  400. if (fio->io_wbc)
  401. wbc_init_bio(fio->io_wbc, bio);
  402. return bio;
  403. }
  404. static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
  405. pgoff_t first_idx,
  406. const struct f2fs_io_info *fio,
  407. gfp_t gfp_mask)
  408. {
  409. /*
  410. * The f2fs garbage collector sets ->encrypted_page when it wants to
  411. * read/write raw data without encryption.
  412. */
  413. if (!fio || !fio->encrypted_page)
  414. fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask);
  415. }
  416. static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode,
  417. pgoff_t next_idx,
  418. const struct f2fs_io_info *fio)
  419. {
  420. /*
  421. * The f2fs garbage collector sets ->encrypted_page when it wants to
  422. * read/write raw data without encryption.
  423. */
  424. if (fio && fio->encrypted_page)
  425. return !bio_has_crypt_ctx(bio);
  426. return fscrypt_mergeable_bio(bio, inode, next_idx);
  427. }
  428. void f2fs_submit_read_bio(struct f2fs_sb_info *sbi, struct bio *bio,
  429. enum page_type type)
  430. {
  431. WARN_ON_ONCE(!is_read_io(bio_op(bio)));
  432. trace_f2fs_submit_read_bio(sbi->sb, type, bio);
  433. iostat_update_submit_ctx(bio, type);
  434. submit_bio(bio);
  435. }
  436. static void f2fs_submit_write_bio(struct f2fs_sb_info *sbi, struct bio *bio,
  437. enum page_type type)
  438. {
  439. WARN_ON_ONCE(is_read_io(bio_op(bio)));
  440. if (f2fs_lfs_mode(sbi) && current->plug && PAGE_TYPE_ON_MAIN(type))
  441. blk_finish_plug(current->plug);
  442. trace_f2fs_submit_write_bio(sbi->sb, type, bio);
  443. iostat_update_submit_ctx(bio, type);
  444. submit_bio(bio);
  445. }
  446. static void __submit_merged_bio(struct f2fs_bio_info *io)
  447. {
  448. struct f2fs_io_info *fio = &io->fio;
  449. if (!io->bio)
  450. return;
  451. if (is_read_io(fio->op)) {
  452. trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
  453. f2fs_submit_read_bio(io->sbi, io->bio, fio->type);
  454. } else {
  455. trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
  456. f2fs_submit_write_bio(io->sbi, io->bio, fio->type);
  457. }
  458. io->bio = NULL;
  459. }
  460. static bool __has_merged_page(struct bio *bio, struct inode *inode,
  461. struct page *page, nid_t ino)
  462. {
  463. struct bio_vec *bvec;
  464. struct bvec_iter_all iter_all;
  465. if (!bio)
  466. return false;
  467. if (!inode && !page && !ino)
  468. return true;
  469. bio_for_each_segment_all(bvec, bio, iter_all) {
  470. struct page *target = bvec->bv_page;
  471. if (fscrypt_is_bounce_page(target)) {
  472. target = fscrypt_pagecache_page(target);
  473. if (IS_ERR(target))
  474. continue;
  475. }
  476. if (f2fs_is_compressed_page(target)) {
  477. target = f2fs_compress_control_page(target);
  478. if (IS_ERR(target))
  479. continue;
  480. }
  481. if (inode && inode == target->mapping->host)
  482. return true;
  483. if (page && page == target)
  484. return true;
  485. if (ino && ino == ino_of_node(target))
  486. return true;
  487. }
  488. return false;
  489. }
  490. int f2fs_init_write_merge_io(struct f2fs_sb_info *sbi)
  491. {
  492. int i;
  493. for (i = 0; i < NR_PAGE_TYPE; i++) {
  494. int n = (i == META) ? 1 : NR_TEMP_TYPE;
  495. int j;
  496. sbi->write_io[i] = f2fs_kmalloc(sbi,
  497. array_size(n, sizeof(struct f2fs_bio_info)),
  498. GFP_KERNEL);
  499. if (!sbi->write_io[i])
  500. return -ENOMEM;
  501. for (j = HOT; j < n; j++) {
  502. struct f2fs_bio_info *io = &sbi->write_io[i][j];
  503. init_f2fs_rwsem(&io->io_rwsem);
  504. io->sbi = sbi;
  505. io->bio = NULL;
  506. io->last_block_in_bio = 0;
  507. spin_lock_init(&io->io_lock);
  508. INIT_LIST_HEAD(&io->io_list);
  509. INIT_LIST_HEAD(&io->bio_list);
  510. init_f2fs_rwsem(&io->bio_list_lock);
  511. #ifdef CONFIG_BLK_DEV_ZONED
  512. init_completion(&io->zone_wait);
  513. io->zone_pending_bio = NULL;
  514. io->bi_private = NULL;
  515. #endif
  516. }
  517. }
  518. return 0;
  519. }
  520. static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
  521. enum page_type type, enum temp_type temp)
  522. {
  523. enum page_type btype = PAGE_TYPE_OF_BIO(type);
  524. struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
  525. f2fs_down_write(&io->io_rwsem);
  526. if (!io->bio)
  527. goto unlock_out;
  528. /* change META to META_FLUSH in the checkpoint procedure */
  529. if (type >= META_FLUSH) {
  530. io->fio.type = META_FLUSH;
  531. io->bio->bi_opf |= REQ_META | REQ_PRIO | REQ_SYNC;
  532. if (!test_opt(sbi, NOBARRIER))
  533. io->bio->bi_opf |= REQ_PREFLUSH | REQ_FUA;
  534. }
  535. __submit_merged_bio(io);
  536. unlock_out:
  537. f2fs_up_write(&io->io_rwsem);
  538. }
  539. static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
  540. struct inode *inode, struct page *page,
  541. nid_t ino, enum page_type type, bool force)
  542. {
  543. enum temp_type temp;
  544. bool ret = true;
  545. for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
  546. if (!force) {
  547. enum page_type btype = PAGE_TYPE_OF_BIO(type);
  548. struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
  549. f2fs_down_read(&io->io_rwsem);
  550. ret = __has_merged_page(io->bio, inode, page, ino);
  551. f2fs_up_read(&io->io_rwsem);
  552. }
  553. if (ret)
  554. __f2fs_submit_merged_write(sbi, type, temp);
  555. /* TODO: use HOT temp only for meta pages now. */
  556. if (type >= META)
  557. break;
  558. }
  559. }
  560. void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
  561. {
  562. __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
  563. }
  564. void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
  565. struct inode *inode, struct page *page,
  566. nid_t ino, enum page_type type)
  567. {
  568. __submit_merged_write_cond(sbi, inode, page, ino, type, false);
  569. }
  570. void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
  571. {
  572. f2fs_submit_merged_write(sbi, DATA);
  573. f2fs_submit_merged_write(sbi, NODE);
  574. f2fs_submit_merged_write(sbi, META);
  575. }
  576. /*
  577. * Fill the locked page with data located in the block address.
  578. * A caller needs to unlock the page on failure.
  579. */
  580. int f2fs_submit_page_bio(struct f2fs_io_info *fio)
  581. {
  582. struct bio *bio;
  583. struct page *page = fio->encrypted_page ?
  584. fio->encrypted_page : fio->page;
  585. if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
  586. fio->is_por ? META_POR : (__is_meta_io(fio) ?
  587. META_GENERIC : DATA_GENERIC_ENHANCE)))
  588. return -EFSCORRUPTED;
  589. trace_f2fs_submit_page_bio(page, fio);
  590. /* Allocate a new bio */
  591. bio = __bio_alloc(fio, 1);
  592. f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
  593. page_folio(fio->page)->index, fio, GFP_NOIO);
  594. if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
  595. bio_put(bio);
  596. return -EFAULT;
  597. }
  598. if (fio->io_wbc && !is_read_io(fio->op))
  599. wbc_account_cgroup_owner(fio->io_wbc, page_folio(fio->page),
  600. PAGE_SIZE);
  601. inc_page_count(fio->sbi, is_read_io(fio->op) ?
  602. __read_io_type(page) : WB_DATA_TYPE(fio->page, false));
  603. if (is_read_io(bio_op(bio)))
  604. f2fs_submit_read_bio(fio->sbi, bio, fio->type);
  605. else
  606. f2fs_submit_write_bio(fio->sbi, bio, fio->type);
  607. return 0;
  608. }
  609. static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
  610. block_t last_blkaddr, block_t cur_blkaddr)
  611. {
  612. if (unlikely(sbi->max_io_bytes &&
  613. bio->bi_iter.bi_size >= sbi->max_io_bytes))
  614. return false;
  615. if (last_blkaddr + 1 != cur_blkaddr)
  616. return false;
  617. return bio->bi_bdev == f2fs_target_device(sbi, cur_blkaddr, NULL);
  618. }
  619. static bool io_type_is_mergeable(struct f2fs_bio_info *io,
  620. struct f2fs_io_info *fio)
  621. {
  622. if (io->fio.op != fio->op)
  623. return false;
  624. return io->fio.op_flags == fio->op_flags;
  625. }
  626. static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
  627. struct f2fs_bio_info *io,
  628. struct f2fs_io_info *fio,
  629. block_t last_blkaddr,
  630. block_t cur_blkaddr)
  631. {
  632. if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
  633. return false;
  634. return io_type_is_mergeable(io, fio);
  635. }
  636. static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
  637. struct page *page, enum temp_type temp)
  638. {
  639. struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
  640. struct bio_entry *be;
  641. be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS, true, NULL);
  642. be->bio = bio;
  643. bio_get(bio);
  644. if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
  645. f2fs_bug_on(sbi, 1);
  646. f2fs_down_write(&io->bio_list_lock);
  647. list_add_tail(&be->list, &io->bio_list);
  648. f2fs_up_write(&io->bio_list_lock);
  649. }
  650. static void del_bio_entry(struct bio_entry *be)
  651. {
  652. list_del(&be->list);
  653. kmem_cache_free(bio_entry_slab, be);
  654. }
  655. static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
  656. struct page *page)
  657. {
  658. struct f2fs_sb_info *sbi = fio->sbi;
  659. enum temp_type temp;
  660. bool found = false;
  661. int ret = -EAGAIN;
  662. for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
  663. struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
  664. struct list_head *head = &io->bio_list;
  665. struct bio_entry *be;
  666. f2fs_down_write(&io->bio_list_lock);
  667. list_for_each_entry(be, head, list) {
  668. if (be->bio != *bio)
  669. continue;
  670. found = true;
  671. f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio,
  672. *fio->last_block,
  673. fio->new_blkaddr));
  674. if (f2fs_crypt_mergeable_bio(*bio,
  675. fio->page->mapping->host,
  676. page_folio(fio->page)->index, fio) &&
  677. bio_add_page(*bio, page, PAGE_SIZE, 0) ==
  678. PAGE_SIZE) {
  679. ret = 0;
  680. break;
  681. }
  682. /* page can't be merged into bio; submit the bio */
  683. del_bio_entry(be);
  684. f2fs_submit_write_bio(sbi, *bio, DATA);
  685. break;
  686. }
  687. f2fs_up_write(&io->bio_list_lock);
  688. }
  689. if (ret) {
  690. bio_put(*bio);
  691. *bio = NULL;
  692. }
  693. return ret;
  694. }
  695. void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
  696. struct bio **bio, struct page *page)
  697. {
  698. enum temp_type temp;
  699. bool found = false;
  700. struct bio *target = bio ? *bio : NULL;
  701. f2fs_bug_on(sbi, !target && !page);
  702. for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
  703. struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
  704. struct list_head *head = &io->bio_list;
  705. struct bio_entry *be;
  706. if (list_empty(head))
  707. continue;
  708. f2fs_down_read(&io->bio_list_lock);
  709. list_for_each_entry(be, head, list) {
  710. if (target)
  711. found = (target == be->bio);
  712. else
  713. found = __has_merged_page(be->bio, NULL,
  714. page, 0);
  715. if (found)
  716. break;
  717. }
  718. f2fs_up_read(&io->bio_list_lock);
  719. if (!found)
  720. continue;
  721. found = false;
  722. f2fs_down_write(&io->bio_list_lock);
  723. list_for_each_entry(be, head, list) {
  724. if (target)
  725. found = (target == be->bio);
  726. else
  727. found = __has_merged_page(be->bio, NULL,
  728. page, 0);
  729. if (found) {
  730. target = be->bio;
  731. del_bio_entry(be);
  732. break;
  733. }
  734. }
  735. f2fs_up_write(&io->bio_list_lock);
  736. }
  737. if (found)
  738. f2fs_submit_write_bio(sbi, target, DATA);
  739. if (bio && *bio) {
  740. bio_put(*bio);
  741. *bio = NULL;
  742. }
  743. }
  744. int f2fs_merge_page_bio(struct f2fs_io_info *fio)
  745. {
  746. struct bio *bio = *fio->bio;
  747. struct page *page = fio->encrypted_page ?
  748. fio->encrypted_page : fio->page;
  749. if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
  750. __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
  751. return -EFSCORRUPTED;
  752. trace_f2fs_submit_page_bio(page, fio);
  753. if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
  754. fio->new_blkaddr))
  755. f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
  756. alloc_new:
  757. if (!bio) {
  758. bio = __bio_alloc(fio, BIO_MAX_VECS);
  759. f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
  760. page_folio(fio->page)->index, fio, GFP_NOIO);
  761. add_bio_entry(fio->sbi, bio, page, fio->temp);
  762. } else {
  763. if (add_ipu_page(fio, &bio, page))
  764. goto alloc_new;
  765. }
  766. if (fio->io_wbc)
  767. wbc_account_cgroup_owner(fio->io_wbc, page_folio(fio->page),
  768. PAGE_SIZE);
  769. inc_page_count(fio->sbi, WB_DATA_TYPE(page, false));
  770. *fio->last_block = fio->new_blkaddr;
  771. *fio->bio = bio;
  772. return 0;
  773. }
  774. #ifdef CONFIG_BLK_DEV_ZONED
  775. static bool is_end_zone_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr)
  776. {
  777. struct block_device *bdev = sbi->sb->s_bdev;
  778. int devi = 0;
  779. if (f2fs_is_multi_device(sbi)) {
  780. devi = f2fs_target_device_index(sbi, blkaddr);
  781. if (blkaddr < FDEV(devi).start_blk ||
  782. blkaddr > FDEV(devi).end_blk) {
  783. f2fs_err(sbi, "Invalid block %x", blkaddr);
  784. return false;
  785. }
  786. blkaddr -= FDEV(devi).start_blk;
  787. bdev = FDEV(devi).bdev;
  788. }
  789. return bdev_is_zoned(bdev) &&
  790. f2fs_blkz_is_seq(sbi, devi, blkaddr) &&
  791. (blkaddr % sbi->blocks_per_blkz == sbi->blocks_per_blkz - 1);
  792. }
  793. #endif
  794. void f2fs_submit_page_write(struct f2fs_io_info *fio)
  795. {
  796. struct f2fs_sb_info *sbi = fio->sbi;
  797. enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
  798. struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
  799. struct page *bio_page;
  800. enum count_type type;
  801. f2fs_bug_on(sbi, is_read_io(fio->op));
  802. f2fs_down_write(&io->io_rwsem);
  803. next:
  804. #ifdef CONFIG_BLK_DEV_ZONED
  805. if (f2fs_sb_has_blkzoned(sbi) && btype < META && io->zone_pending_bio) {
  806. wait_for_completion_io(&io->zone_wait);
  807. bio_put(io->zone_pending_bio);
  808. io->zone_pending_bio = NULL;
  809. io->bi_private = NULL;
  810. }
  811. #endif
  812. if (fio->in_list) {
  813. spin_lock(&io->io_lock);
  814. if (list_empty(&io->io_list)) {
  815. spin_unlock(&io->io_lock);
  816. goto out;
  817. }
  818. fio = list_first_entry(&io->io_list,
  819. struct f2fs_io_info, list);
  820. list_del(&fio->list);
  821. spin_unlock(&io->io_lock);
  822. }
  823. verify_fio_blkaddr(fio);
  824. if (fio->encrypted_page)
  825. bio_page = fio->encrypted_page;
  826. else if (fio->compressed_page)
  827. bio_page = fio->compressed_page;
  828. else
  829. bio_page = fio->page;
  830. /* set submitted = true as a return value */
  831. fio->submitted = 1;
  832. type = WB_DATA_TYPE(bio_page, fio->compressed_page);
  833. inc_page_count(sbi, type);
  834. if (io->bio &&
  835. (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
  836. fio->new_blkaddr) ||
  837. !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host,
  838. page_folio(bio_page)->index, fio)))
  839. __submit_merged_bio(io);
  840. alloc_new:
  841. if (io->bio == NULL) {
  842. io->bio = __bio_alloc(fio, BIO_MAX_VECS);
  843. f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host,
  844. page_folio(bio_page)->index, fio, GFP_NOIO);
  845. io->fio = *fio;
  846. }
  847. if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
  848. __submit_merged_bio(io);
  849. goto alloc_new;
  850. }
  851. if (fio->io_wbc)
  852. wbc_account_cgroup_owner(fio->io_wbc, page_folio(fio->page),
  853. PAGE_SIZE);
  854. io->last_block_in_bio = fio->new_blkaddr;
  855. trace_f2fs_submit_page_write(fio->page, fio);
  856. #ifdef CONFIG_BLK_DEV_ZONED
  857. if (f2fs_sb_has_blkzoned(sbi) && btype < META &&
  858. is_end_zone_blkaddr(sbi, fio->new_blkaddr)) {
  859. bio_get(io->bio);
  860. reinit_completion(&io->zone_wait);
  861. io->bi_private = io->bio->bi_private;
  862. io->bio->bi_private = io;
  863. io->bio->bi_end_io = f2fs_zone_write_end_io;
  864. io->zone_pending_bio = io->bio;
  865. __submit_merged_bio(io);
  866. }
  867. #endif
  868. if (fio->in_list)
  869. goto next;
  870. out:
  871. if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
  872. !f2fs_is_checkpoint_ready(sbi))
  873. __submit_merged_bio(io);
  874. f2fs_up_write(&io->io_rwsem);
  875. }
  876. static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
  877. unsigned nr_pages, blk_opf_t op_flag,
  878. pgoff_t first_idx, bool for_write)
  879. {
  880. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  881. struct bio *bio;
  882. struct bio_post_read_ctx *ctx = NULL;
  883. unsigned int post_read_steps = 0;
  884. sector_t sector;
  885. struct block_device *bdev = f2fs_target_device(sbi, blkaddr, &sector);
  886. bio = bio_alloc_bioset(bdev, bio_max_segs(nr_pages),
  887. REQ_OP_READ | op_flag,
  888. for_write ? GFP_NOIO : GFP_KERNEL, &f2fs_bioset);
  889. if (!bio)
  890. return ERR_PTR(-ENOMEM);
  891. bio->bi_iter.bi_sector = sector;
  892. f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS);
  893. bio->bi_end_io = f2fs_read_end_io;
  894. if (fscrypt_inode_uses_fs_layer_crypto(inode))
  895. post_read_steps |= STEP_DECRYPT;
  896. if (f2fs_need_verity(inode, first_idx))
  897. post_read_steps |= STEP_VERITY;
  898. /*
  899. * STEP_DECOMPRESS is handled specially, since a compressed file might
  900. * contain both compressed and uncompressed clusters. We'll allocate a
  901. * bio_post_read_ctx if the file is compressed, but the caller is
  902. * responsible for enabling STEP_DECOMPRESS if it's actually needed.
  903. */
  904. if (post_read_steps || f2fs_compressed_file(inode)) {
  905. /* Due to the mempool, this never fails. */
  906. ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
  907. ctx->bio = bio;
  908. ctx->sbi = sbi;
  909. ctx->enabled_steps = post_read_steps;
  910. ctx->fs_blkaddr = blkaddr;
  911. ctx->decompression_attempted = false;
  912. bio->bi_private = ctx;
  913. }
  914. iostat_alloc_and_bind_ctx(sbi, bio, ctx);
  915. return bio;
  916. }
  917. /* This can handle encryption stuffs */
  918. static int f2fs_submit_page_read(struct inode *inode, struct folio *folio,
  919. block_t blkaddr, blk_opf_t op_flags,
  920. bool for_write)
  921. {
  922. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  923. struct bio *bio;
  924. bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags,
  925. folio->index, for_write);
  926. if (IS_ERR(bio))
  927. return PTR_ERR(bio);
  928. /* wait for GCed page writeback via META_MAPPING */
  929. f2fs_wait_on_block_writeback(inode, blkaddr);
  930. if (!bio_add_folio(bio, folio, PAGE_SIZE, 0)) {
  931. iostat_update_and_unbind_ctx(bio);
  932. if (bio->bi_private)
  933. mempool_free(bio->bi_private, bio_post_read_ctx_pool);
  934. bio_put(bio);
  935. return -EFAULT;
  936. }
  937. inc_page_count(sbi, F2FS_RD_DATA);
  938. f2fs_update_iostat(sbi, NULL, FS_DATA_READ_IO, F2FS_BLKSIZE);
  939. f2fs_submit_read_bio(sbi, bio, DATA);
  940. return 0;
  941. }
  942. static void __set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
  943. {
  944. __le32 *addr = get_dnode_addr(dn->inode, dn->node_page);
  945. dn->data_blkaddr = blkaddr;
  946. addr[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
  947. }
  948. /*
  949. * Lock ordering for the change of data block address:
  950. * ->data_page
  951. * ->node_page
  952. * update block addresses in the node page
  953. */
  954. void f2fs_set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
  955. {
  956. f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
  957. __set_data_blkaddr(dn, blkaddr);
  958. if (set_page_dirty(dn->node_page))
  959. dn->node_changed = true;
  960. }
  961. void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
  962. {
  963. f2fs_set_data_blkaddr(dn, blkaddr);
  964. f2fs_update_read_extent_cache(dn);
  965. }
  966. /* dn->ofs_in_node will be returned with up-to-date last block pointer */
  967. int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
  968. {
  969. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  970. int err;
  971. if (!count)
  972. return 0;
  973. if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
  974. return -EPERM;
  975. err = inc_valid_block_count(sbi, dn->inode, &count, true);
  976. if (unlikely(err))
  977. return err;
  978. trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
  979. dn->ofs_in_node, count);
  980. f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
  981. for (; count > 0; dn->ofs_in_node++) {
  982. block_t blkaddr = f2fs_data_blkaddr(dn);
  983. if (blkaddr == NULL_ADDR) {
  984. __set_data_blkaddr(dn, NEW_ADDR);
  985. count--;
  986. }
  987. }
  988. if (set_page_dirty(dn->node_page))
  989. dn->node_changed = true;
  990. return 0;
  991. }
  992. /* Should keep dn->ofs_in_node unchanged */
  993. int f2fs_reserve_new_block(struct dnode_of_data *dn)
  994. {
  995. unsigned int ofs_in_node = dn->ofs_in_node;
  996. int ret;
  997. ret = f2fs_reserve_new_blocks(dn, 1);
  998. dn->ofs_in_node = ofs_in_node;
  999. return ret;
  1000. }
  1001. int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
  1002. {
  1003. bool need_put = dn->inode_page ? false : true;
  1004. int err;
  1005. err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
  1006. if (err)
  1007. return err;
  1008. if (dn->data_blkaddr == NULL_ADDR)
  1009. err = f2fs_reserve_new_block(dn);
  1010. if (err || need_put)
  1011. f2fs_put_dnode(dn);
  1012. return err;
  1013. }
  1014. struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
  1015. blk_opf_t op_flags, bool for_write,
  1016. pgoff_t *next_pgofs)
  1017. {
  1018. struct address_space *mapping = inode->i_mapping;
  1019. struct dnode_of_data dn;
  1020. struct page *page;
  1021. int err;
  1022. page = f2fs_grab_cache_page(mapping, index, for_write);
  1023. if (!page)
  1024. return ERR_PTR(-ENOMEM);
  1025. if (f2fs_lookup_read_extent_cache_block(inode, index,
  1026. &dn.data_blkaddr)) {
  1027. if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
  1028. DATA_GENERIC_ENHANCE_READ)) {
  1029. err = -EFSCORRUPTED;
  1030. goto put_err;
  1031. }
  1032. goto got_it;
  1033. }
  1034. set_new_dnode(&dn, inode, NULL, NULL, 0);
  1035. err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
  1036. if (err) {
  1037. if (err == -ENOENT && next_pgofs)
  1038. *next_pgofs = f2fs_get_next_page_offset(&dn, index);
  1039. goto put_err;
  1040. }
  1041. f2fs_put_dnode(&dn);
  1042. if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
  1043. err = -ENOENT;
  1044. if (next_pgofs)
  1045. *next_pgofs = index + 1;
  1046. goto put_err;
  1047. }
  1048. if (dn.data_blkaddr != NEW_ADDR &&
  1049. !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
  1050. dn.data_blkaddr,
  1051. DATA_GENERIC_ENHANCE)) {
  1052. err = -EFSCORRUPTED;
  1053. goto put_err;
  1054. }
  1055. got_it:
  1056. if (PageUptodate(page)) {
  1057. unlock_page(page);
  1058. return page;
  1059. }
  1060. /*
  1061. * A new dentry page is allocated but not able to be written, since its
  1062. * new inode page couldn't be allocated due to -ENOSPC.
  1063. * In such the case, its blkaddr can be remained as NEW_ADDR.
  1064. * see, f2fs_add_link -> f2fs_get_new_data_page ->
  1065. * f2fs_init_inode_metadata.
  1066. */
  1067. if (dn.data_blkaddr == NEW_ADDR) {
  1068. zero_user_segment(page, 0, PAGE_SIZE);
  1069. if (!PageUptodate(page))
  1070. SetPageUptodate(page);
  1071. unlock_page(page);
  1072. return page;
  1073. }
  1074. err = f2fs_submit_page_read(inode, page_folio(page), dn.data_blkaddr,
  1075. op_flags, for_write);
  1076. if (err)
  1077. goto put_err;
  1078. return page;
  1079. put_err:
  1080. f2fs_put_page(page, 1);
  1081. return ERR_PTR(err);
  1082. }
  1083. struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index,
  1084. pgoff_t *next_pgofs)
  1085. {
  1086. struct address_space *mapping = inode->i_mapping;
  1087. struct page *page;
  1088. page = find_get_page(mapping, index);
  1089. if (page && PageUptodate(page))
  1090. return page;
  1091. f2fs_put_page(page, 0);
  1092. page = f2fs_get_read_data_page(inode, index, 0, false, next_pgofs);
  1093. if (IS_ERR(page))
  1094. return page;
  1095. if (PageUptodate(page))
  1096. return page;
  1097. wait_on_page_locked(page);
  1098. if (unlikely(!PageUptodate(page))) {
  1099. f2fs_put_page(page, 0);
  1100. return ERR_PTR(-EIO);
  1101. }
  1102. return page;
  1103. }
  1104. /*
  1105. * If it tries to access a hole, return an error.
  1106. * Because, the callers, functions in dir.c and GC, should be able to know
  1107. * whether this page exists or not.
  1108. */
  1109. struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
  1110. bool for_write)
  1111. {
  1112. struct address_space *mapping = inode->i_mapping;
  1113. struct page *page;
  1114. page = f2fs_get_read_data_page(inode, index, 0, for_write, NULL);
  1115. if (IS_ERR(page))
  1116. return page;
  1117. /* wait for read completion */
  1118. lock_page(page);
  1119. if (unlikely(page->mapping != mapping || !PageUptodate(page))) {
  1120. f2fs_put_page(page, 1);
  1121. return ERR_PTR(-EIO);
  1122. }
  1123. return page;
  1124. }
  1125. /*
  1126. * Caller ensures that this data page is never allocated.
  1127. * A new zero-filled data page is allocated in the page cache.
  1128. *
  1129. * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
  1130. * f2fs_unlock_op().
  1131. * Note that, ipage is set only by make_empty_dir, and if any error occur,
  1132. * ipage should be released by this function.
  1133. */
  1134. struct page *f2fs_get_new_data_page(struct inode *inode,
  1135. struct page *ipage, pgoff_t index, bool new_i_size)
  1136. {
  1137. struct address_space *mapping = inode->i_mapping;
  1138. struct page *page;
  1139. struct dnode_of_data dn;
  1140. int err;
  1141. page = f2fs_grab_cache_page(mapping, index, true);
  1142. if (!page) {
  1143. /*
  1144. * before exiting, we should make sure ipage will be released
  1145. * if any error occur.
  1146. */
  1147. f2fs_put_page(ipage, 1);
  1148. return ERR_PTR(-ENOMEM);
  1149. }
  1150. set_new_dnode(&dn, inode, ipage, NULL, 0);
  1151. err = f2fs_reserve_block(&dn, index);
  1152. if (err) {
  1153. f2fs_put_page(page, 1);
  1154. return ERR_PTR(err);
  1155. }
  1156. if (!ipage)
  1157. f2fs_put_dnode(&dn);
  1158. if (PageUptodate(page))
  1159. goto got_it;
  1160. if (dn.data_blkaddr == NEW_ADDR) {
  1161. zero_user_segment(page, 0, PAGE_SIZE);
  1162. if (!PageUptodate(page))
  1163. SetPageUptodate(page);
  1164. } else {
  1165. f2fs_put_page(page, 1);
  1166. /* if ipage exists, blkaddr should be NEW_ADDR */
  1167. f2fs_bug_on(F2FS_I_SB(inode), ipage);
  1168. page = f2fs_get_lock_data_page(inode, index, true);
  1169. if (IS_ERR(page))
  1170. return page;
  1171. }
  1172. got_it:
  1173. if (new_i_size && i_size_read(inode) <
  1174. ((loff_t)(index + 1) << PAGE_SHIFT))
  1175. f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
  1176. return page;
  1177. }
  1178. static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
  1179. {
  1180. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  1181. struct f2fs_summary sum;
  1182. struct node_info ni;
  1183. block_t old_blkaddr;
  1184. blkcnt_t count = 1;
  1185. int err;
  1186. if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
  1187. return -EPERM;
  1188. err = f2fs_get_node_info(sbi, dn->nid, &ni, false);
  1189. if (err)
  1190. return err;
  1191. dn->data_blkaddr = f2fs_data_blkaddr(dn);
  1192. if (dn->data_blkaddr == NULL_ADDR) {
  1193. err = inc_valid_block_count(sbi, dn->inode, &count, true);
  1194. if (unlikely(err))
  1195. return err;
  1196. }
  1197. set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
  1198. old_blkaddr = dn->data_blkaddr;
  1199. err = f2fs_allocate_data_block(sbi, NULL, old_blkaddr,
  1200. &dn->data_blkaddr, &sum, seg_type, NULL);
  1201. if (err)
  1202. return err;
  1203. if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
  1204. f2fs_invalidate_internal_cache(sbi, old_blkaddr);
  1205. f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
  1206. return 0;
  1207. }
  1208. static void f2fs_map_lock(struct f2fs_sb_info *sbi, int flag)
  1209. {
  1210. if (flag == F2FS_GET_BLOCK_PRE_AIO)
  1211. f2fs_down_read(&sbi->node_change);
  1212. else
  1213. f2fs_lock_op(sbi);
  1214. }
  1215. static void f2fs_map_unlock(struct f2fs_sb_info *sbi, int flag)
  1216. {
  1217. if (flag == F2FS_GET_BLOCK_PRE_AIO)
  1218. f2fs_up_read(&sbi->node_change);
  1219. else
  1220. f2fs_unlock_op(sbi);
  1221. }
  1222. int f2fs_get_block_locked(struct dnode_of_data *dn, pgoff_t index)
  1223. {
  1224. struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
  1225. int err = 0;
  1226. f2fs_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO);
  1227. if (!f2fs_lookup_read_extent_cache_block(dn->inode, index,
  1228. &dn->data_blkaddr))
  1229. err = f2fs_reserve_block(dn, index);
  1230. f2fs_map_unlock(sbi, F2FS_GET_BLOCK_PRE_AIO);
  1231. return err;
  1232. }
  1233. static int f2fs_map_no_dnode(struct inode *inode,
  1234. struct f2fs_map_blocks *map, struct dnode_of_data *dn,
  1235. pgoff_t pgoff)
  1236. {
  1237. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1238. /*
  1239. * There is one exceptional case that read_node_page() may return
  1240. * -ENOENT due to filesystem has been shutdown or cp_error, return
  1241. * -EIO in that case.
  1242. */
  1243. if (map->m_may_create &&
  1244. (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) || f2fs_cp_error(sbi)))
  1245. return -EIO;
  1246. if (map->m_next_pgofs)
  1247. *map->m_next_pgofs = f2fs_get_next_page_offset(dn, pgoff);
  1248. if (map->m_next_extent)
  1249. *map->m_next_extent = f2fs_get_next_page_offset(dn, pgoff);
  1250. return 0;
  1251. }
  1252. static bool f2fs_map_blocks_cached(struct inode *inode,
  1253. struct f2fs_map_blocks *map, int flag)
  1254. {
  1255. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1256. unsigned int maxblocks = map->m_len;
  1257. pgoff_t pgoff = (pgoff_t)map->m_lblk;
  1258. struct extent_info ei = {};
  1259. if (!f2fs_lookup_read_extent_cache(inode, pgoff, &ei))
  1260. return false;
  1261. map->m_pblk = ei.blk + pgoff - ei.fofs;
  1262. map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgoff);
  1263. map->m_flags = F2FS_MAP_MAPPED;
  1264. if (map->m_next_extent)
  1265. *map->m_next_extent = pgoff + map->m_len;
  1266. /* for hardware encryption, but to avoid potential issue in future */
  1267. if (flag == F2FS_GET_BLOCK_DIO)
  1268. f2fs_wait_on_block_writeback_range(inode,
  1269. map->m_pblk, map->m_len);
  1270. if (f2fs_allow_multi_device_dio(sbi, flag)) {
  1271. int bidx = f2fs_target_device_index(sbi, map->m_pblk);
  1272. struct f2fs_dev_info *dev = &sbi->devs[bidx];
  1273. map->m_bdev = dev->bdev;
  1274. map->m_pblk -= dev->start_blk;
  1275. map->m_len = min(map->m_len, dev->end_blk + 1 - map->m_pblk);
  1276. } else {
  1277. map->m_bdev = inode->i_sb->s_bdev;
  1278. }
  1279. return true;
  1280. }
  1281. static bool map_is_mergeable(struct f2fs_sb_info *sbi,
  1282. struct f2fs_map_blocks *map,
  1283. block_t blkaddr, int flag, int bidx,
  1284. int ofs)
  1285. {
  1286. if (map->m_multidev_dio && map->m_bdev != FDEV(bidx).bdev)
  1287. return false;
  1288. if (map->m_pblk != NEW_ADDR && blkaddr == (map->m_pblk + ofs))
  1289. return true;
  1290. if (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR)
  1291. return true;
  1292. if (flag == F2FS_GET_BLOCK_PRE_DIO)
  1293. return true;
  1294. if (flag == F2FS_GET_BLOCK_DIO &&
  1295. map->m_pblk == NULL_ADDR && blkaddr == NULL_ADDR)
  1296. return true;
  1297. return false;
  1298. }
  1299. /*
  1300. * f2fs_map_blocks() tries to find or build mapping relationship which
  1301. * maps continuous logical blocks to physical blocks, and return such
  1302. * info via f2fs_map_blocks structure.
  1303. */
  1304. int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag)
  1305. {
  1306. unsigned int maxblocks = map->m_len;
  1307. struct dnode_of_data dn;
  1308. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1309. int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
  1310. pgoff_t pgofs, end_offset, end;
  1311. int err = 0, ofs = 1;
  1312. unsigned int ofs_in_node, last_ofs_in_node;
  1313. blkcnt_t prealloc;
  1314. block_t blkaddr;
  1315. unsigned int start_pgofs;
  1316. int bidx = 0;
  1317. bool is_hole;
  1318. if (!maxblocks)
  1319. return 0;
  1320. if (!map->m_may_create && f2fs_map_blocks_cached(inode, map, flag))
  1321. goto out;
  1322. map->m_bdev = inode->i_sb->s_bdev;
  1323. map->m_multidev_dio =
  1324. f2fs_allow_multi_device_dio(F2FS_I_SB(inode), flag);
  1325. map->m_len = 0;
  1326. map->m_flags = 0;
  1327. /* it only supports block size == page size */
  1328. pgofs = (pgoff_t)map->m_lblk;
  1329. end = pgofs + maxblocks;
  1330. next_dnode:
  1331. if (map->m_may_create)
  1332. f2fs_map_lock(sbi, flag);
  1333. /* When reading holes, we need its node page */
  1334. set_new_dnode(&dn, inode, NULL, NULL, 0);
  1335. err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
  1336. if (err) {
  1337. if (flag == F2FS_GET_BLOCK_BMAP)
  1338. map->m_pblk = 0;
  1339. if (err == -ENOENT)
  1340. err = f2fs_map_no_dnode(inode, map, &dn, pgofs);
  1341. goto unlock_out;
  1342. }
  1343. start_pgofs = pgofs;
  1344. prealloc = 0;
  1345. last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
  1346. end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
  1347. next_block:
  1348. blkaddr = f2fs_data_blkaddr(&dn);
  1349. is_hole = !__is_valid_data_blkaddr(blkaddr);
  1350. if (!is_hole &&
  1351. !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
  1352. err = -EFSCORRUPTED;
  1353. goto sync_out;
  1354. }
  1355. /* use out-place-update for direct IO under LFS mode */
  1356. if (map->m_may_create && (is_hole ||
  1357. (flag == F2FS_GET_BLOCK_DIO && f2fs_lfs_mode(sbi) &&
  1358. !f2fs_is_pinned_file(inode)))) {
  1359. if (unlikely(f2fs_cp_error(sbi))) {
  1360. err = -EIO;
  1361. goto sync_out;
  1362. }
  1363. switch (flag) {
  1364. case F2FS_GET_BLOCK_PRE_AIO:
  1365. if (blkaddr == NULL_ADDR) {
  1366. prealloc++;
  1367. last_ofs_in_node = dn.ofs_in_node;
  1368. }
  1369. break;
  1370. case F2FS_GET_BLOCK_PRE_DIO:
  1371. case F2FS_GET_BLOCK_DIO:
  1372. err = __allocate_data_block(&dn, map->m_seg_type);
  1373. if (err)
  1374. goto sync_out;
  1375. if (flag == F2FS_GET_BLOCK_PRE_DIO)
  1376. file_need_truncate(inode);
  1377. set_inode_flag(inode, FI_APPEND_WRITE);
  1378. break;
  1379. default:
  1380. WARN_ON_ONCE(1);
  1381. err = -EIO;
  1382. goto sync_out;
  1383. }
  1384. blkaddr = dn.data_blkaddr;
  1385. if (is_hole)
  1386. map->m_flags |= F2FS_MAP_NEW;
  1387. } else if (is_hole) {
  1388. if (f2fs_compressed_file(inode) &&
  1389. f2fs_sanity_check_cluster(&dn)) {
  1390. err = -EFSCORRUPTED;
  1391. f2fs_handle_error(sbi,
  1392. ERROR_CORRUPTED_CLUSTER);
  1393. goto sync_out;
  1394. }
  1395. switch (flag) {
  1396. case F2FS_GET_BLOCK_PRECACHE:
  1397. goto sync_out;
  1398. case F2FS_GET_BLOCK_BMAP:
  1399. map->m_pblk = 0;
  1400. goto sync_out;
  1401. case F2FS_GET_BLOCK_FIEMAP:
  1402. if (blkaddr == NULL_ADDR) {
  1403. if (map->m_next_pgofs)
  1404. *map->m_next_pgofs = pgofs + 1;
  1405. goto sync_out;
  1406. }
  1407. break;
  1408. case F2FS_GET_BLOCK_DIO:
  1409. if (map->m_next_pgofs)
  1410. *map->m_next_pgofs = pgofs + 1;
  1411. break;
  1412. default:
  1413. /* for defragment case */
  1414. if (map->m_next_pgofs)
  1415. *map->m_next_pgofs = pgofs + 1;
  1416. goto sync_out;
  1417. }
  1418. }
  1419. if (flag == F2FS_GET_BLOCK_PRE_AIO)
  1420. goto skip;
  1421. if (map->m_multidev_dio)
  1422. bidx = f2fs_target_device_index(sbi, blkaddr);
  1423. if (map->m_len == 0) {
  1424. /* reserved delalloc block should be mapped for fiemap. */
  1425. if (blkaddr == NEW_ADDR)
  1426. map->m_flags |= F2FS_MAP_DELALLOC;
  1427. /* DIO READ and hole case, should not map the blocks. */
  1428. if (!(flag == F2FS_GET_BLOCK_DIO && is_hole && !map->m_may_create))
  1429. map->m_flags |= F2FS_MAP_MAPPED;
  1430. map->m_pblk = blkaddr;
  1431. map->m_len = 1;
  1432. if (map->m_multidev_dio)
  1433. map->m_bdev = FDEV(bidx).bdev;
  1434. } else if (map_is_mergeable(sbi, map, blkaddr, flag, bidx, ofs)) {
  1435. ofs++;
  1436. map->m_len++;
  1437. } else {
  1438. goto sync_out;
  1439. }
  1440. skip:
  1441. dn.ofs_in_node++;
  1442. pgofs++;
  1443. /* preallocate blocks in batch for one dnode page */
  1444. if (flag == F2FS_GET_BLOCK_PRE_AIO &&
  1445. (pgofs == end || dn.ofs_in_node == end_offset)) {
  1446. dn.ofs_in_node = ofs_in_node;
  1447. err = f2fs_reserve_new_blocks(&dn, prealloc);
  1448. if (err)
  1449. goto sync_out;
  1450. map->m_len += dn.ofs_in_node - ofs_in_node;
  1451. if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
  1452. err = -ENOSPC;
  1453. goto sync_out;
  1454. }
  1455. dn.ofs_in_node = end_offset;
  1456. }
  1457. if (flag == F2FS_GET_BLOCK_DIO && f2fs_lfs_mode(sbi) &&
  1458. map->m_may_create) {
  1459. /* the next block to be allocated may not be contiguous. */
  1460. if (GET_SEGOFF_FROM_SEG0(sbi, blkaddr) % BLKS_PER_SEC(sbi) ==
  1461. CAP_BLKS_PER_SEC(sbi) - 1)
  1462. goto sync_out;
  1463. }
  1464. if (pgofs >= end)
  1465. goto sync_out;
  1466. else if (dn.ofs_in_node < end_offset)
  1467. goto next_block;
  1468. if (flag == F2FS_GET_BLOCK_PRECACHE) {
  1469. if (map->m_flags & F2FS_MAP_MAPPED) {
  1470. unsigned int ofs = start_pgofs - map->m_lblk;
  1471. f2fs_update_read_extent_cache_range(&dn,
  1472. start_pgofs, map->m_pblk + ofs,
  1473. map->m_len - ofs);
  1474. }
  1475. }
  1476. f2fs_put_dnode(&dn);
  1477. if (map->m_may_create) {
  1478. f2fs_map_unlock(sbi, flag);
  1479. f2fs_balance_fs(sbi, dn.node_changed);
  1480. }
  1481. goto next_dnode;
  1482. sync_out:
  1483. if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED) {
  1484. /*
  1485. * for hardware encryption, but to avoid potential issue
  1486. * in future
  1487. */
  1488. f2fs_wait_on_block_writeback_range(inode,
  1489. map->m_pblk, map->m_len);
  1490. if (map->m_multidev_dio) {
  1491. block_t blk_addr = map->m_pblk;
  1492. bidx = f2fs_target_device_index(sbi, map->m_pblk);
  1493. map->m_bdev = FDEV(bidx).bdev;
  1494. map->m_pblk -= FDEV(bidx).start_blk;
  1495. if (map->m_may_create)
  1496. f2fs_update_device_state(sbi, inode->i_ino,
  1497. blk_addr, map->m_len);
  1498. f2fs_bug_on(sbi, blk_addr + map->m_len >
  1499. FDEV(bidx).end_blk + 1);
  1500. }
  1501. }
  1502. if (flag == F2FS_GET_BLOCK_PRECACHE) {
  1503. if (map->m_flags & F2FS_MAP_MAPPED) {
  1504. unsigned int ofs = start_pgofs - map->m_lblk;
  1505. f2fs_update_read_extent_cache_range(&dn,
  1506. start_pgofs, map->m_pblk + ofs,
  1507. map->m_len - ofs);
  1508. }
  1509. if (map->m_next_extent)
  1510. *map->m_next_extent = pgofs + 1;
  1511. }
  1512. f2fs_put_dnode(&dn);
  1513. unlock_out:
  1514. if (map->m_may_create) {
  1515. f2fs_map_unlock(sbi, flag);
  1516. f2fs_balance_fs(sbi, dn.node_changed);
  1517. }
  1518. out:
  1519. trace_f2fs_map_blocks(inode, map, flag, err);
  1520. return err;
  1521. }
  1522. bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
  1523. {
  1524. struct f2fs_map_blocks map;
  1525. block_t last_lblk;
  1526. int err;
  1527. if (pos + len > i_size_read(inode))
  1528. return false;
  1529. map.m_lblk = F2FS_BYTES_TO_BLK(pos);
  1530. map.m_next_pgofs = NULL;
  1531. map.m_next_extent = NULL;
  1532. map.m_seg_type = NO_CHECK_TYPE;
  1533. map.m_may_create = false;
  1534. last_lblk = F2FS_BLK_ALIGN(pos + len);
  1535. while (map.m_lblk < last_lblk) {
  1536. map.m_len = last_lblk - map.m_lblk;
  1537. err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
  1538. if (err || map.m_len == 0)
  1539. return false;
  1540. map.m_lblk += map.m_len;
  1541. }
  1542. return true;
  1543. }
  1544. static int f2fs_xattr_fiemap(struct inode *inode,
  1545. struct fiemap_extent_info *fieinfo)
  1546. {
  1547. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1548. struct page *page;
  1549. struct node_info ni;
  1550. __u64 phys = 0, len;
  1551. __u32 flags;
  1552. nid_t xnid = F2FS_I(inode)->i_xattr_nid;
  1553. int err = 0;
  1554. if (f2fs_has_inline_xattr(inode)) {
  1555. int offset;
  1556. page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
  1557. inode->i_ino, false);
  1558. if (!page)
  1559. return -ENOMEM;
  1560. err = f2fs_get_node_info(sbi, inode->i_ino, &ni, false);
  1561. if (err) {
  1562. f2fs_put_page(page, 1);
  1563. return err;
  1564. }
  1565. phys = F2FS_BLK_TO_BYTES(ni.blk_addr);
  1566. offset = offsetof(struct f2fs_inode, i_addr) +
  1567. sizeof(__le32) * (DEF_ADDRS_PER_INODE -
  1568. get_inline_xattr_addrs(inode));
  1569. phys += offset;
  1570. len = inline_xattr_size(inode);
  1571. f2fs_put_page(page, 1);
  1572. flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
  1573. if (!xnid)
  1574. flags |= FIEMAP_EXTENT_LAST;
  1575. err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
  1576. trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
  1577. if (err)
  1578. return err;
  1579. }
  1580. if (xnid) {
  1581. page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
  1582. if (!page)
  1583. return -ENOMEM;
  1584. err = f2fs_get_node_info(sbi, xnid, &ni, false);
  1585. if (err) {
  1586. f2fs_put_page(page, 1);
  1587. return err;
  1588. }
  1589. phys = F2FS_BLK_TO_BYTES(ni.blk_addr);
  1590. len = inode->i_sb->s_blocksize;
  1591. f2fs_put_page(page, 1);
  1592. flags = FIEMAP_EXTENT_LAST;
  1593. }
  1594. if (phys) {
  1595. err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
  1596. trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
  1597. }
  1598. return (err < 0 ? err : 0);
  1599. }
  1600. int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1601. u64 start, u64 len)
  1602. {
  1603. struct f2fs_map_blocks map;
  1604. sector_t start_blk, last_blk, blk_len, max_len;
  1605. pgoff_t next_pgofs;
  1606. u64 logical = 0, phys = 0, size = 0;
  1607. u32 flags = 0;
  1608. int ret = 0;
  1609. bool compr_cluster = false, compr_appended;
  1610. unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
  1611. unsigned int count_in_cluster = 0;
  1612. loff_t maxbytes;
  1613. if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
  1614. ret = f2fs_precache_extents(inode);
  1615. if (ret)
  1616. return ret;
  1617. }
  1618. ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
  1619. if (ret)
  1620. return ret;
  1621. inode_lock_shared(inode);
  1622. maxbytes = F2FS_BLK_TO_BYTES(max_file_blocks(inode));
  1623. if (start > maxbytes) {
  1624. ret = -EFBIG;
  1625. goto out;
  1626. }
  1627. if (len > maxbytes || (maxbytes - len) < start)
  1628. len = maxbytes - start;
  1629. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
  1630. ret = f2fs_xattr_fiemap(inode, fieinfo);
  1631. goto out;
  1632. }
  1633. if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
  1634. ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
  1635. if (ret != -EAGAIN)
  1636. goto out;
  1637. }
  1638. start_blk = F2FS_BYTES_TO_BLK(start);
  1639. last_blk = F2FS_BYTES_TO_BLK(start + len - 1);
  1640. blk_len = last_blk - start_blk + 1;
  1641. max_len = F2FS_BYTES_TO_BLK(maxbytes) - start_blk;
  1642. next:
  1643. memset(&map, 0, sizeof(map));
  1644. map.m_lblk = start_blk;
  1645. map.m_len = blk_len;
  1646. map.m_next_pgofs = &next_pgofs;
  1647. map.m_seg_type = NO_CHECK_TYPE;
  1648. if (compr_cluster) {
  1649. map.m_lblk += 1;
  1650. map.m_len = cluster_size - count_in_cluster;
  1651. }
  1652. ret = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_FIEMAP);
  1653. if (ret)
  1654. goto out;
  1655. /* HOLE */
  1656. if (!compr_cluster && !(map.m_flags & F2FS_MAP_FLAGS)) {
  1657. start_blk = next_pgofs;
  1658. if (F2FS_BLK_TO_BYTES(start_blk) < maxbytes)
  1659. goto prep_next;
  1660. flags |= FIEMAP_EXTENT_LAST;
  1661. }
  1662. /*
  1663. * current extent may cross boundary of inquiry, increase len to
  1664. * requery.
  1665. */
  1666. if (!compr_cluster && (map.m_flags & F2FS_MAP_MAPPED) &&
  1667. map.m_lblk + map.m_len - 1 == last_blk &&
  1668. blk_len != max_len) {
  1669. blk_len = max_len;
  1670. goto next;
  1671. }
  1672. compr_appended = false;
  1673. /* In a case of compressed cluster, append this to the last extent */
  1674. if (compr_cluster && ((map.m_flags & F2FS_MAP_DELALLOC) ||
  1675. !(map.m_flags & F2FS_MAP_FLAGS))) {
  1676. compr_appended = true;
  1677. goto skip_fill;
  1678. }
  1679. if (size) {
  1680. flags |= FIEMAP_EXTENT_MERGED;
  1681. if (IS_ENCRYPTED(inode))
  1682. flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
  1683. ret = fiemap_fill_next_extent(fieinfo, logical,
  1684. phys, size, flags);
  1685. trace_f2fs_fiemap(inode, logical, phys, size, flags, ret);
  1686. if (ret)
  1687. goto out;
  1688. size = 0;
  1689. }
  1690. if (start_blk > last_blk)
  1691. goto out;
  1692. skip_fill:
  1693. if (map.m_pblk == COMPRESS_ADDR) {
  1694. compr_cluster = true;
  1695. count_in_cluster = 1;
  1696. } else if (compr_appended) {
  1697. unsigned int appended_blks = cluster_size -
  1698. count_in_cluster + 1;
  1699. size += F2FS_BLK_TO_BYTES(appended_blks);
  1700. start_blk += appended_blks;
  1701. compr_cluster = false;
  1702. } else {
  1703. logical = F2FS_BLK_TO_BYTES(start_blk);
  1704. phys = __is_valid_data_blkaddr(map.m_pblk) ?
  1705. F2FS_BLK_TO_BYTES(map.m_pblk) : 0;
  1706. size = F2FS_BLK_TO_BYTES(map.m_len);
  1707. flags = 0;
  1708. if (compr_cluster) {
  1709. flags = FIEMAP_EXTENT_ENCODED;
  1710. count_in_cluster += map.m_len;
  1711. if (count_in_cluster == cluster_size) {
  1712. compr_cluster = false;
  1713. size += F2FS_BLKSIZE;
  1714. }
  1715. } else if (map.m_flags & F2FS_MAP_DELALLOC) {
  1716. flags = FIEMAP_EXTENT_UNWRITTEN;
  1717. }
  1718. start_blk += F2FS_BYTES_TO_BLK(size);
  1719. }
  1720. prep_next:
  1721. cond_resched();
  1722. if (fatal_signal_pending(current))
  1723. ret = -EINTR;
  1724. else
  1725. goto next;
  1726. out:
  1727. if (ret == 1)
  1728. ret = 0;
  1729. inode_unlock_shared(inode);
  1730. return ret;
  1731. }
  1732. static inline loff_t f2fs_readpage_limit(struct inode *inode)
  1733. {
  1734. if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode))
  1735. return F2FS_BLK_TO_BYTES(max_file_blocks(inode));
  1736. return i_size_read(inode);
  1737. }
  1738. static inline blk_opf_t f2fs_ra_op_flags(struct readahead_control *rac)
  1739. {
  1740. return rac ? REQ_RAHEAD : 0;
  1741. }
  1742. static int f2fs_read_single_page(struct inode *inode, struct folio *folio,
  1743. unsigned nr_pages,
  1744. struct f2fs_map_blocks *map,
  1745. struct bio **bio_ret,
  1746. sector_t *last_block_in_bio,
  1747. struct readahead_control *rac)
  1748. {
  1749. struct bio *bio = *bio_ret;
  1750. const unsigned int blocksize = F2FS_BLKSIZE;
  1751. sector_t block_in_file;
  1752. sector_t last_block;
  1753. sector_t last_block_in_file;
  1754. sector_t block_nr;
  1755. pgoff_t index = folio_index(folio);
  1756. int ret = 0;
  1757. block_in_file = (sector_t)index;
  1758. last_block = block_in_file + nr_pages;
  1759. last_block_in_file = F2FS_BYTES_TO_BLK(f2fs_readpage_limit(inode) +
  1760. blocksize - 1);
  1761. if (last_block > last_block_in_file)
  1762. last_block = last_block_in_file;
  1763. /* just zeroing out page which is beyond EOF */
  1764. if (block_in_file >= last_block)
  1765. goto zero_out;
  1766. /*
  1767. * Map blocks using the previous result first.
  1768. */
  1769. if ((map->m_flags & F2FS_MAP_MAPPED) &&
  1770. block_in_file > map->m_lblk &&
  1771. block_in_file < (map->m_lblk + map->m_len))
  1772. goto got_it;
  1773. /*
  1774. * Then do more f2fs_map_blocks() calls until we are
  1775. * done with this page.
  1776. */
  1777. map->m_lblk = block_in_file;
  1778. map->m_len = last_block - block_in_file;
  1779. ret = f2fs_map_blocks(inode, map, F2FS_GET_BLOCK_DEFAULT);
  1780. if (ret)
  1781. goto out;
  1782. got_it:
  1783. if ((map->m_flags & F2FS_MAP_MAPPED)) {
  1784. block_nr = map->m_pblk + block_in_file - map->m_lblk;
  1785. folio_set_mappedtodisk(folio);
  1786. if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
  1787. DATA_GENERIC_ENHANCE_READ)) {
  1788. ret = -EFSCORRUPTED;
  1789. goto out;
  1790. }
  1791. } else {
  1792. zero_out:
  1793. folio_zero_segment(folio, 0, folio_size(folio));
  1794. if (f2fs_need_verity(inode, index) &&
  1795. !fsverity_verify_folio(folio)) {
  1796. ret = -EIO;
  1797. goto out;
  1798. }
  1799. if (!folio_test_uptodate(folio))
  1800. folio_mark_uptodate(folio);
  1801. folio_unlock(folio);
  1802. goto out;
  1803. }
  1804. /*
  1805. * This page will go to BIO. Do we need to send this
  1806. * BIO off first?
  1807. */
  1808. if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio,
  1809. *last_block_in_bio, block_nr) ||
  1810. !f2fs_crypt_mergeable_bio(bio, inode, index, NULL))) {
  1811. submit_and_realloc:
  1812. f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA);
  1813. bio = NULL;
  1814. }
  1815. if (bio == NULL) {
  1816. bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
  1817. f2fs_ra_op_flags(rac), index,
  1818. false);
  1819. if (IS_ERR(bio)) {
  1820. ret = PTR_ERR(bio);
  1821. bio = NULL;
  1822. goto out;
  1823. }
  1824. }
  1825. /*
  1826. * If the page is under writeback, we need to wait for
  1827. * its completion to see the correct decrypted data.
  1828. */
  1829. f2fs_wait_on_block_writeback(inode, block_nr);
  1830. if (!bio_add_folio(bio, folio, blocksize, 0))
  1831. goto submit_and_realloc;
  1832. inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
  1833. f2fs_update_iostat(F2FS_I_SB(inode), NULL, FS_DATA_READ_IO,
  1834. F2FS_BLKSIZE);
  1835. *last_block_in_bio = block_nr;
  1836. out:
  1837. *bio_ret = bio;
  1838. return ret;
  1839. }
  1840. #ifdef CONFIG_F2FS_FS_COMPRESSION
  1841. int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
  1842. unsigned nr_pages, sector_t *last_block_in_bio,
  1843. struct readahead_control *rac, bool for_write)
  1844. {
  1845. struct dnode_of_data dn;
  1846. struct inode *inode = cc->inode;
  1847. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1848. struct bio *bio = *bio_ret;
  1849. unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
  1850. sector_t last_block_in_file;
  1851. const unsigned int blocksize = F2FS_BLKSIZE;
  1852. struct decompress_io_ctx *dic = NULL;
  1853. struct extent_info ei = {};
  1854. bool from_dnode = true;
  1855. int i;
  1856. int ret = 0;
  1857. f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
  1858. last_block_in_file = F2FS_BYTES_TO_BLK(f2fs_readpage_limit(inode) +
  1859. blocksize - 1);
  1860. /* get rid of pages beyond EOF */
  1861. for (i = 0; i < cc->cluster_size; i++) {
  1862. struct page *page = cc->rpages[i];
  1863. struct folio *folio;
  1864. if (!page)
  1865. continue;
  1866. folio = page_folio(page);
  1867. if ((sector_t)folio->index >= last_block_in_file) {
  1868. folio_zero_segment(folio, 0, folio_size(folio));
  1869. if (!folio_test_uptodate(folio))
  1870. folio_mark_uptodate(folio);
  1871. } else if (!folio_test_uptodate(folio)) {
  1872. continue;
  1873. }
  1874. folio_unlock(folio);
  1875. if (for_write)
  1876. folio_put(folio);
  1877. cc->rpages[i] = NULL;
  1878. cc->nr_rpages--;
  1879. }
  1880. /* we are done since all pages are beyond EOF */
  1881. if (f2fs_cluster_is_empty(cc))
  1882. goto out;
  1883. if (f2fs_lookup_read_extent_cache(inode, start_idx, &ei))
  1884. from_dnode = false;
  1885. if (!from_dnode)
  1886. goto skip_reading_dnode;
  1887. set_new_dnode(&dn, inode, NULL, NULL, 0);
  1888. ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
  1889. if (ret)
  1890. goto out;
  1891. if (unlikely(f2fs_cp_error(sbi))) {
  1892. ret = -EIO;
  1893. goto out_put_dnode;
  1894. }
  1895. f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR);
  1896. skip_reading_dnode:
  1897. for (i = 1; i < cc->cluster_size; i++) {
  1898. block_t blkaddr;
  1899. blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
  1900. dn.ofs_in_node + i) :
  1901. ei.blk + i - 1;
  1902. if (!__is_valid_data_blkaddr(blkaddr))
  1903. break;
  1904. if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
  1905. ret = -EFAULT;
  1906. goto out_put_dnode;
  1907. }
  1908. cc->nr_cpages++;
  1909. if (!from_dnode && i >= ei.c_len)
  1910. break;
  1911. }
  1912. /* nothing to decompress */
  1913. if (cc->nr_cpages == 0) {
  1914. ret = 0;
  1915. goto out_put_dnode;
  1916. }
  1917. dic = f2fs_alloc_dic(cc);
  1918. if (IS_ERR(dic)) {
  1919. ret = PTR_ERR(dic);
  1920. goto out_put_dnode;
  1921. }
  1922. for (i = 0; i < cc->nr_cpages; i++) {
  1923. struct folio *folio = page_folio(dic->cpages[i]);
  1924. block_t blkaddr;
  1925. struct bio_post_read_ctx *ctx;
  1926. blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
  1927. dn.ofs_in_node + i + 1) :
  1928. ei.blk + i;
  1929. f2fs_wait_on_block_writeback(inode, blkaddr);
  1930. if (f2fs_load_compressed_page(sbi, folio_page(folio, 0),
  1931. blkaddr)) {
  1932. if (atomic_dec_and_test(&dic->remaining_pages)) {
  1933. f2fs_decompress_cluster(dic, true);
  1934. break;
  1935. }
  1936. continue;
  1937. }
  1938. if (bio && (!page_is_mergeable(sbi, bio,
  1939. *last_block_in_bio, blkaddr) ||
  1940. !f2fs_crypt_mergeable_bio(bio, inode, folio->index, NULL))) {
  1941. submit_and_realloc:
  1942. f2fs_submit_read_bio(sbi, bio, DATA);
  1943. bio = NULL;
  1944. }
  1945. if (!bio) {
  1946. bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
  1947. f2fs_ra_op_flags(rac),
  1948. folio->index, for_write);
  1949. if (IS_ERR(bio)) {
  1950. ret = PTR_ERR(bio);
  1951. f2fs_decompress_end_io(dic, ret, true);
  1952. f2fs_put_dnode(&dn);
  1953. *bio_ret = NULL;
  1954. return ret;
  1955. }
  1956. }
  1957. if (!bio_add_folio(bio, folio, blocksize, 0))
  1958. goto submit_and_realloc;
  1959. ctx = get_post_read_ctx(bio);
  1960. ctx->enabled_steps |= STEP_DECOMPRESS;
  1961. refcount_inc(&dic->refcnt);
  1962. inc_page_count(sbi, F2FS_RD_DATA);
  1963. f2fs_update_iostat(sbi, inode, FS_DATA_READ_IO, F2FS_BLKSIZE);
  1964. *last_block_in_bio = blkaddr;
  1965. }
  1966. if (from_dnode)
  1967. f2fs_put_dnode(&dn);
  1968. *bio_ret = bio;
  1969. return 0;
  1970. out_put_dnode:
  1971. if (from_dnode)
  1972. f2fs_put_dnode(&dn);
  1973. out:
  1974. for (i = 0; i < cc->cluster_size; i++) {
  1975. if (cc->rpages[i]) {
  1976. ClearPageUptodate(cc->rpages[i]);
  1977. unlock_page(cc->rpages[i]);
  1978. }
  1979. }
  1980. *bio_ret = bio;
  1981. return ret;
  1982. }
  1983. #endif
  1984. /*
  1985. * This function was originally taken from fs/mpage.c, and customized for f2fs.
  1986. * Major change was from block_size == page_size in f2fs by default.
  1987. */
  1988. static int f2fs_mpage_readpages(struct inode *inode,
  1989. struct readahead_control *rac, struct folio *folio)
  1990. {
  1991. struct bio *bio = NULL;
  1992. sector_t last_block_in_bio = 0;
  1993. struct f2fs_map_blocks map;
  1994. #ifdef CONFIG_F2FS_FS_COMPRESSION
  1995. struct compress_ctx cc = {
  1996. .inode = inode,
  1997. .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
  1998. .cluster_size = F2FS_I(inode)->i_cluster_size,
  1999. .cluster_idx = NULL_CLUSTER,
  2000. .rpages = NULL,
  2001. .cpages = NULL,
  2002. .nr_rpages = 0,
  2003. .nr_cpages = 0,
  2004. };
  2005. pgoff_t nc_cluster_idx = NULL_CLUSTER;
  2006. pgoff_t index;
  2007. #endif
  2008. unsigned nr_pages = rac ? readahead_count(rac) : 1;
  2009. unsigned max_nr_pages = nr_pages;
  2010. int ret = 0;
  2011. map.m_pblk = 0;
  2012. map.m_lblk = 0;
  2013. map.m_len = 0;
  2014. map.m_flags = 0;
  2015. map.m_next_pgofs = NULL;
  2016. map.m_next_extent = NULL;
  2017. map.m_seg_type = NO_CHECK_TYPE;
  2018. map.m_may_create = false;
  2019. for (; nr_pages; nr_pages--) {
  2020. if (rac) {
  2021. folio = readahead_folio(rac);
  2022. prefetchw(&folio->flags);
  2023. }
  2024. #ifdef CONFIG_F2FS_FS_COMPRESSION
  2025. index = folio_index(folio);
  2026. if (!f2fs_compressed_file(inode))
  2027. goto read_single_page;
  2028. /* there are remained compressed pages, submit them */
  2029. if (!f2fs_cluster_can_merge_page(&cc, index)) {
  2030. ret = f2fs_read_multi_pages(&cc, &bio,
  2031. max_nr_pages,
  2032. &last_block_in_bio,
  2033. rac, false);
  2034. f2fs_destroy_compress_ctx(&cc, false);
  2035. if (ret)
  2036. goto set_error_page;
  2037. }
  2038. if (cc.cluster_idx == NULL_CLUSTER) {
  2039. if (nc_cluster_idx == index >> cc.log_cluster_size)
  2040. goto read_single_page;
  2041. ret = f2fs_is_compressed_cluster(inode, index);
  2042. if (ret < 0)
  2043. goto set_error_page;
  2044. else if (!ret) {
  2045. nc_cluster_idx =
  2046. index >> cc.log_cluster_size;
  2047. goto read_single_page;
  2048. }
  2049. nc_cluster_idx = NULL_CLUSTER;
  2050. }
  2051. ret = f2fs_init_compress_ctx(&cc);
  2052. if (ret)
  2053. goto set_error_page;
  2054. f2fs_compress_ctx_add_page(&cc, folio);
  2055. goto next_page;
  2056. read_single_page:
  2057. #endif
  2058. ret = f2fs_read_single_page(inode, folio, max_nr_pages, &map,
  2059. &bio, &last_block_in_bio, rac);
  2060. if (ret) {
  2061. #ifdef CONFIG_F2FS_FS_COMPRESSION
  2062. set_error_page:
  2063. #endif
  2064. folio_zero_segment(folio, 0, folio_size(folio));
  2065. folio_unlock(folio);
  2066. }
  2067. #ifdef CONFIG_F2FS_FS_COMPRESSION
  2068. next_page:
  2069. #endif
  2070. #ifdef CONFIG_F2FS_FS_COMPRESSION
  2071. if (f2fs_compressed_file(inode)) {
  2072. /* last page */
  2073. if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
  2074. ret = f2fs_read_multi_pages(&cc, &bio,
  2075. max_nr_pages,
  2076. &last_block_in_bio,
  2077. rac, false);
  2078. f2fs_destroy_compress_ctx(&cc, false);
  2079. }
  2080. }
  2081. #endif
  2082. }
  2083. if (bio)
  2084. f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA);
  2085. return ret;
  2086. }
  2087. static int f2fs_read_data_folio(struct file *file, struct folio *folio)
  2088. {
  2089. struct inode *inode = folio_file_mapping(folio)->host;
  2090. int ret = -EAGAIN;
  2091. trace_f2fs_readpage(folio, DATA);
  2092. if (!f2fs_is_compress_backend_ready(inode)) {
  2093. folio_unlock(folio);
  2094. return -EOPNOTSUPP;
  2095. }
  2096. /* If the file has inline data, try to read it directly */
  2097. if (f2fs_has_inline_data(inode))
  2098. ret = f2fs_read_inline_data(inode, folio);
  2099. if (ret == -EAGAIN)
  2100. ret = f2fs_mpage_readpages(inode, NULL, folio);
  2101. return ret;
  2102. }
  2103. static void f2fs_readahead(struct readahead_control *rac)
  2104. {
  2105. struct inode *inode = rac->mapping->host;
  2106. trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
  2107. if (!f2fs_is_compress_backend_ready(inode))
  2108. return;
  2109. /* If the file has inline data, skip readahead */
  2110. if (f2fs_has_inline_data(inode))
  2111. return;
  2112. f2fs_mpage_readpages(inode, rac, NULL);
  2113. }
  2114. int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
  2115. {
  2116. struct inode *inode = fio->page->mapping->host;
  2117. struct page *mpage, *page;
  2118. gfp_t gfp_flags = GFP_NOFS;
  2119. if (!f2fs_encrypted_file(inode))
  2120. return 0;
  2121. page = fio->compressed_page ? fio->compressed_page : fio->page;
  2122. if (fscrypt_inode_uses_inline_crypto(inode))
  2123. return 0;
  2124. retry_encrypt:
  2125. fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
  2126. PAGE_SIZE, 0, gfp_flags);
  2127. if (IS_ERR(fio->encrypted_page)) {
  2128. /* flush pending IOs and wait for a while in the ENOMEM case */
  2129. if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
  2130. f2fs_flush_merged_writes(fio->sbi);
  2131. memalloc_retry_wait(GFP_NOFS);
  2132. gfp_flags |= __GFP_NOFAIL;
  2133. goto retry_encrypt;
  2134. }
  2135. return PTR_ERR(fio->encrypted_page);
  2136. }
  2137. mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
  2138. if (mpage) {
  2139. if (PageUptodate(mpage))
  2140. memcpy(page_address(mpage),
  2141. page_address(fio->encrypted_page), PAGE_SIZE);
  2142. f2fs_put_page(mpage, 1);
  2143. }
  2144. return 0;
  2145. }
  2146. static inline bool check_inplace_update_policy(struct inode *inode,
  2147. struct f2fs_io_info *fio)
  2148. {
  2149. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  2150. if (IS_F2FS_IPU_HONOR_OPU_WRITE(sbi) &&
  2151. is_inode_flag_set(inode, FI_OPU_WRITE))
  2152. return false;
  2153. if (IS_F2FS_IPU_FORCE(sbi))
  2154. return true;
  2155. if (IS_F2FS_IPU_SSR(sbi) && f2fs_need_SSR(sbi))
  2156. return true;
  2157. if (IS_F2FS_IPU_UTIL(sbi) && utilization(sbi) > SM_I(sbi)->min_ipu_util)
  2158. return true;
  2159. if (IS_F2FS_IPU_SSR_UTIL(sbi) && f2fs_need_SSR(sbi) &&
  2160. utilization(sbi) > SM_I(sbi)->min_ipu_util)
  2161. return true;
  2162. /*
  2163. * IPU for rewrite async pages
  2164. */
  2165. if (IS_F2FS_IPU_ASYNC(sbi) && fio && fio->op == REQ_OP_WRITE &&
  2166. !(fio->op_flags & REQ_SYNC) && !IS_ENCRYPTED(inode))
  2167. return true;
  2168. /* this is only set during fdatasync */
  2169. if (IS_F2FS_IPU_FSYNC(sbi) && is_inode_flag_set(inode, FI_NEED_IPU))
  2170. return true;
  2171. if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
  2172. !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
  2173. return true;
  2174. return false;
  2175. }
  2176. bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
  2177. {
  2178. /* swap file is migrating in aligned write mode */
  2179. if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
  2180. return false;
  2181. if (f2fs_is_pinned_file(inode))
  2182. return true;
  2183. /* if this is cold file, we should overwrite to avoid fragmentation */
  2184. if (file_is_cold(inode) && !is_inode_flag_set(inode, FI_OPU_WRITE))
  2185. return true;
  2186. return check_inplace_update_policy(inode, fio);
  2187. }
  2188. bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
  2189. {
  2190. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  2191. /* The below cases were checked when setting it. */
  2192. if (f2fs_is_pinned_file(inode))
  2193. return false;
  2194. if (fio && is_sbi_flag_set(sbi, SBI_NEED_FSCK))
  2195. return true;
  2196. if (f2fs_lfs_mode(sbi))
  2197. return true;
  2198. if (S_ISDIR(inode->i_mode))
  2199. return true;
  2200. if (IS_NOQUOTA(inode))
  2201. return true;
  2202. if (f2fs_used_in_atomic_write(inode))
  2203. return true;
  2204. /* rewrite low ratio compress data w/ OPU mode to avoid fragmentation */
  2205. if (f2fs_compressed_file(inode) &&
  2206. F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER &&
  2207. is_inode_flag_set(inode, FI_ENABLE_COMPRESS))
  2208. return true;
  2209. /* swap file is migrating in aligned write mode */
  2210. if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
  2211. return true;
  2212. if (is_inode_flag_set(inode, FI_OPU_WRITE))
  2213. return true;
  2214. if (fio) {
  2215. if (page_private_gcing(fio->page))
  2216. return true;
  2217. if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
  2218. f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
  2219. return true;
  2220. }
  2221. return false;
  2222. }
  2223. static inline bool need_inplace_update(struct f2fs_io_info *fio)
  2224. {
  2225. struct inode *inode = fio->page->mapping->host;
  2226. if (f2fs_should_update_outplace(inode, fio))
  2227. return false;
  2228. return f2fs_should_update_inplace(inode, fio);
  2229. }
  2230. int f2fs_do_write_data_page(struct f2fs_io_info *fio)
  2231. {
  2232. struct folio *folio = page_folio(fio->page);
  2233. struct inode *inode = folio->mapping->host;
  2234. struct dnode_of_data dn;
  2235. struct node_info ni;
  2236. bool ipu_force = false;
  2237. bool atomic_commit;
  2238. int err = 0;
  2239. /* Use COW inode to make dnode_of_data for atomic write */
  2240. atomic_commit = f2fs_is_atomic_file(inode) &&
  2241. page_private_atomic(folio_page(folio, 0));
  2242. if (atomic_commit)
  2243. set_new_dnode(&dn, F2FS_I(inode)->cow_inode, NULL, NULL, 0);
  2244. else
  2245. set_new_dnode(&dn, inode, NULL, NULL, 0);
  2246. if (need_inplace_update(fio) &&
  2247. f2fs_lookup_read_extent_cache_block(inode, folio->index,
  2248. &fio->old_blkaddr)) {
  2249. if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
  2250. DATA_GENERIC_ENHANCE))
  2251. return -EFSCORRUPTED;
  2252. ipu_force = true;
  2253. fio->need_lock = LOCK_DONE;
  2254. goto got_it;
  2255. }
  2256. /* Deadlock due to between page->lock and f2fs_lock_op */
  2257. if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
  2258. return -EAGAIN;
  2259. err = f2fs_get_dnode_of_data(&dn, folio->index, LOOKUP_NODE);
  2260. if (err)
  2261. goto out;
  2262. fio->old_blkaddr = dn.data_blkaddr;
  2263. /* This page is already truncated */
  2264. if (fio->old_blkaddr == NULL_ADDR) {
  2265. folio_clear_uptodate(folio);
  2266. clear_page_private_gcing(folio_page(folio, 0));
  2267. goto out_writepage;
  2268. }
  2269. got_it:
  2270. if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
  2271. !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
  2272. DATA_GENERIC_ENHANCE)) {
  2273. err = -EFSCORRUPTED;
  2274. goto out_writepage;
  2275. }
  2276. /* wait for GCed page writeback via META_MAPPING */
  2277. if (fio->meta_gc)
  2278. f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
  2279. /*
  2280. * If current allocation needs SSR,
  2281. * it had better in-place writes for updated data.
  2282. */
  2283. if (ipu_force ||
  2284. (__is_valid_data_blkaddr(fio->old_blkaddr) &&
  2285. need_inplace_update(fio))) {
  2286. err = f2fs_encrypt_one_page(fio);
  2287. if (err)
  2288. goto out_writepage;
  2289. folio_start_writeback(folio);
  2290. f2fs_put_dnode(&dn);
  2291. if (fio->need_lock == LOCK_REQ)
  2292. f2fs_unlock_op(fio->sbi);
  2293. err = f2fs_inplace_write_data(fio);
  2294. if (err) {
  2295. if (fscrypt_inode_uses_fs_layer_crypto(inode))
  2296. fscrypt_finalize_bounce_page(&fio->encrypted_page);
  2297. folio_end_writeback(folio);
  2298. } else {
  2299. set_inode_flag(inode, FI_UPDATE_WRITE);
  2300. }
  2301. trace_f2fs_do_write_data_page(folio, IPU);
  2302. return err;
  2303. }
  2304. if (fio->need_lock == LOCK_RETRY) {
  2305. if (!f2fs_trylock_op(fio->sbi)) {
  2306. err = -EAGAIN;
  2307. goto out_writepage;
  2308. }
  2309. fio->need_lock = LOCK_REQ;
  2310. }
  2311. err = f2fs_get_node_info(fio->sbi, dn.nid, &ni, false);
  2312. if (err)
  2313. goto out_writepage;
  2314. fio->version = ni.version;
  2315. err = f2fs_encrypt_one_page(fio);
  2316. if (err)
  2317. goto out_writepage;
  2318. folio_start_writeback(folio);
  2319. if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
  2320. f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
  2321. /* LFS mode write path */
  2322. f2fs_outplace_write_data(&dn, fio);
  2323. trace_f2fs_do_write_data_page(folio, OPU);
  2324. set_inode_flag(inode, FI_APPEND_WRITE);
  2325. if (atomic_commit)
  2326. clear_page_private_atomic(folio_page(folio, 0));
  2327. out_writepage:
  2328. f2fs_put_dnode(&dn);
  2329. out:
  2330. if (fio->need_lock == LOCK_REQ)
  2331. f2fs_unlock_op(fio->sbi);
  2332. return err;
  2333. }
  2334. int f2fs_write_single_data_page(struct folio *folio, int *submitted,
  2335. struct bio **bio,
  2336. sector_t *last_block,
  2337. struct writeback_control *wbc,
  2338. enum iostat_type io_type,
  2339. int compr_blocks,
  2340. bool allow_balance)
  2341. {
  2342. struct inode *inode = folio->mapping->host;
  2343. struct page *page = folio_page(folio, 0);
  2344. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  2345. loff_t i_size = i_size_read(inode);
  2346. const pgoff_t end_index = ((unsigned long long)i_size)
  2347. >> PAGE_SHIFT;
  2348. loff_t psize = (loff_t)(folio->index + 1) << PAGE_SHIFT;
  2349. unsigned offset = 0;
  2350. bool need_balance_fs = false;
  2351. bool quota_inode = IS_NOQUOTA(inode);
  2352. int err = 0;
  2353. struct f2fs_io_info fio = {
  2354. .sbi = sbi,
  2355. .ino = inode->i_ino,
  2356. .type = DATA,
  2357. .op = REQ_OP_WRITE,
  2358. .op_flags = wbc_to_write_flags(wbc),
  2359. .old_blkaddr = NULL_ADDR,
  2360. .page = page,
  2361. .encrypted_page = NULL,
  2362. .submitted = 0,
  2363. .compr_blocks = compr_blocks,
  2364. .need_lock = compr_blocks ? LOCK_DONE : LOCK_RETRY,
  2365. .meta_gc = f2fs_meta_inode_gc_required(inode) ? 1 : 0,
  2366. .io_type = io_type,
  2367. .io_wbc = wbc,
  2368. .bio = bio,
  2369. .last_block = last_block,
  2370. };
  2371. trace_f2fs_writepage(folio, DATA);
  2372. /* we should bypass data pages to proceed the kworker jobs */
  2373. if (unlikely(f2fs_cp_error(sbi))) {
  2374. mapping_set_error(folio->mapping, -EIO);
  2375. /*
  2376. * don't drop any dirty dentry pages for keeping lastest
  2377. * directory structure.
  2378. */
  2379. if (S_ISDIR(inode->i_mode) &&
  2380. !is_sbi_flag_set(sbi, SBI_IS_CLOSE))
  2381. goto redirty_out;
  2382. /* keep data pages in remount-ro mode */
  2383. if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY)
  2384. goto redirty_out;
  2385. goto out;
  2386. }
  2387. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  2388. goto redirty_out;
  2389. if (folio->index < end_index ||
  2390. f2fs_verity_in_progress(inode) ||
  2391. compr_blocks)
  2392. goto write;
  2393. /*
  2394. * If the offset is out-of-range of file size,
  2395. * this page does not have to be written to disk.
  2396. */
  2397. offset = i_size & (PAGE_SIZE - 1);
  2398. if ((folio->index >= end_index + 1) || !offset)
  2399. goto out;
  2400. folio_zero_segment(folio, offset, folio_size(folio));
  2401. write:
  2402. /* Dentry/quota blocks are controlled by checkpoint */
  2403. if (S_ISDIR(inode->i_mode) || quota_inode) {
  2404. /*
  2405. * We need to wait for node_write to avoid block allocation during
  2406. * checkpoint. This can only happen to quota writes which can cause
  2407. * the below discard race condition.
  2408. */
  2409. if (quota_inode)
  2410. f2fs_down_read(&sbi->node_write);
  2411. fio.need_lock = LOCK_DONE;
  2412. err = f2fs_do_write_data_page(&fio);
  2413. if (quota_inode)
  2414. f2fs_up_read(&sbi->node_write);
  2415. goto done;
  2416. }
  2417. if (!wbc->for_reclaim)
  2418. need_balance_fs = true;
  2419. else if (has_not_enough_free_secs(sbi, 0, 0))
  2420. goto redirty_out;
  2421. else
  2422. set_inode_flag(inode, FI_HOT_DATA);
  2423. err = -EAGAIN;
  2424. if (f2fs_has_inline_data(inode)) {
  2425. err = f2fs_write_inline_data(inode, folio);
  2426. if (!err)
  2427. goto out;
  2428. }
  2429. if (err == -EAGAIN) {
  2430. err = f2fs_do_write_data_page(&fio);
  2431. if (err == -EAGAIN) {
  2432. f2fs_bug_on(sbi, compr_blocks);
  2433. fio.need_lock = LOCK_REQ;
  2434. err = f2fs_do_write_data_page(&fio);
  2435. }
  2436. }
  2437. if (err) {
  2438. file_set_keep_isize(inode);
  2439. } else {
  2440. spin_lock(&F2FS_I(inode)->i_size_lock);
  2441. if (F2FS_I(inode)->last_disk_size < psize)
  2442. F2FS_I(inode)->last_disk_size = psize;
  2443. spin_unlock(&F2FS_I(inode)->i_size_lock);
  2444. }
  2445. done:
  2446. if (err && err != -ENOENT)
  2447. goto redirty_out;
  2448. out:
  2449. inode_dec_dirty_pages(inode);
  2450. if (err) {
  2451. folio_clear_uptodate(folio);
  2452. clear_page_private_gcing(page);
  2453. }
  2454. if (wbc->for_reclaim) {
  2455. f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
  2456. clear_inode_flag(inode, FI_HOT_DATA);
  2457. f2fs_remove_dirty_inode(inode);
  2458. submitted = NULL;
  2459. }
  2460. folio_unlock(folio);
  2461. if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
  2462. !F2FS_I(inode)->wb_task && allow_balance)
  2463. f2fs_balance_fs(sbi, need_balance_fs);
  2464. if (unlikely(f2fs_cp_error(sbi))) {
  2465. f2fs_submit_merged_write(sbi, DATA);
  2466. if (bio && *bio)
  2467. f2fs_submit_merged_ipu_write(sbi, bio, NULL);
  2468. submitted = NULL;
  2469. }
  2470. if (submitted)
  2471. *submitted = fio.submitted;
  2472. return 0;
  2473. redirty_out:
  2474. folio_redirty_for_writepage(wbc, folio);
  2475. /*
  2476. * pageout() in MM translates EAGAIN, so calls handle_write_error()
  2477. * -> mapping_set_error() -> set_bit(AS_EIO, ...).
  2478. * file_write_and_wait_range() will see EIO error, which is critical
  2479. * to return value of fsync() followed by atomic_write failure to user.
  2480. */
  2481. if (!err || wbc->for_reclaim)
  2482. return AOP_WRITEPAGE_ACTIVATE;
  2483. folio_unlock(folio);
  2484. return err;
  2485. }
  2486. static int f2fs_write_data_page(struct page *page,
  2487. struct writeback_control *wbc)
  2488. {
  2489. struct folio *folio = page_folio(page);
  2490. #ifdef CONFIG_F2FS_FS_COMPRESSION
  2491. struct inode *inode = folio->mapping->host;
  2492. if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
  2493. goto out;
  2494. if (f2fs_compressed_file(inode)) {
  2495. if (f2fs_is_compressed_cluster(inode, folio->index)) {
  2496. folio_redirty_for_writepage(wbc, folio);
  2497. return AOP_WRITEPAGE_ACTIVATE;
  2498. }
  2499. }
  2500. out:
  2501. #endif
  2502. return f2fs_write_single_data_page(folio, NULL, NULL, NULL,
  2503. wbc, FS_DATA_IO, 0, true);
  2504. }
  2505. /*
  2506. * This function was copied from write_cache_pages from mm/page-writeback.c.
  2507. * The major change is making write step of cold data page separately from
  2508. * warm/hot data page.
  2509. */
  2510. static int f2fs_write_cache_pages(struct address_space *mapping,
  2511. struct writeback_control *wbc,
  2512. enum iostat_type io_type)
  2513. {
  2514. int ret = 0;
  2515. int done = 0, retry = 0;
  2516. struct page *pages_local[F2FS_ONSTACK_PAGES];
  2517. struct page **pages = pages_local;
  2518. struct folio_batch fbatch;
  2519. struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
  2520. struct bio *bio = NULL;
  2521. sector_t last_block;
  2522. #ifdef CONFIG_F2FS_FS_COMPRESSION
  2523. struct inode *inode = mapping->host;
  2524. struct compress_ctx cc = {
  2525. .inode = inode,
  2526. .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
  2527. .cluster_size = F2FS_I(inode)->i_cluster_size,
  2528. .cluster_idx = NULL_CLUSTER,
  2529. .rpages = NULL,
  2530. .nr_rpages = 0,
  2531. .cpages = NULL,
  2532. .valid_nr_cpages = 0,
  2533. .rbuf = NULL,
  2534. .cbuf = NULL,
  2535. .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
  2536. .private = NULL,
  2537. };
  2538. #endif
  2539. int nr_folios, p, idx;
  2540. int nr_pages;
  2541. unsigned int max_pages = F2FS_ONSTACK_PAGES;
  2542. pgoff_t index;
  2543. pgoff_t end; /* Inclusive */
  2544. pgoff_t done_index;
  2545. int range_whole = 0;
  2546. xa_mark_t tag;
  2547. int nwritten = 0;
  2548. int submitted = 0;
  2549. int i;
  2550. #ifdef CONFIG_F2FS_FS_COMPRESSION
  2551. if (f2fs_compressed_file(inode) &&
  2552. 1 << cc.log_cluster_size > F2FS_ONSTACK_PAGES) {
  2553. pages = f2fs_kzalloc(sbi, sizeof(struct page *) <<
  2554. cc.log_cluster_size, GFP_NOFS | __GFP_NOFAIL);
  2555. max_pages = 1 << cc.log_cluster_size;
  2556. }
  2557. #endif
  2558. folio_batch_init(&fbatch);
  2559. if (get_dirty_pages(mapping->host) <=
  2560. SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
  2561. set_inode_flag(mapping->host, FI_HOT_DATA);
  2562. else
  2563. clear_inode_flag(mapping->host, FI_HOT_DATA);
  2564. if (wbc->range_cyclic) {
  2565. index = mapping->writeback_index; /* prev offset */
  2566. end = -1;
  2567. } else {
  2568. index = wbc->range_start >> PAGE_SHIFT;
  2569. end = wbc->range_end >> PAGE_SHIFT;
  2570. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  2571. range_whole = 1;
  2572. }
  2573. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  2574. tag = PAGECACHE_TAG_TOWRITE;
  2575. else
  2576. tag = PAGECACHE_TAG_DIRTY;
  2577. retry:
  2578. retry = 0;
  2579. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  2580. tag_pages_for_writeback(mapping, index, end);
  2581. done_index = index;
  2582. while (!done && !retry && (index <= end)) {
  2583. nr_pages = 0;
  2584. again:
  2585. nr_folios = filemap_get_folios_tag(mapping, &index, end,
  2586. tag, &fbatch);
  2587. if (nr_folios == 0) {
  2588. if (nr_pages)
  2589. goto write;
  2590. break;
  2591. }
  2592. for (i = 0; i < nr_folios; i++) {
  2593. struct folio *folio = fbatch.folios[i];
  2594. idx = 0;
  2595. p = folio_nr_pages(folio);
  2596. add_more:
  2597. pages[nr_pages] = folio_page(folio, idx);
  2598. folio_get(folio);
  2599. if (++nr_pages == max_pages) {
  2600. index = folio->index + idx + 1;
  2601. folio_batch_release(&fbatch);
  2602. goto write;
  2603. }
  2604. if (++idx < p)
  2605. goto add_more;
  2606. }
  2607. folio_batch_release(&fbatch);
  2608. goto again;
  2609. write:
  2610. for (i = 0; i < nr_pages; i++) {
  2611. struct page *page = pages[i];
  2612. struct folio *folio = page_folio(page);
  2613. bool need_readd;
  2614. readd:
  2615. need_readd = false;
  2616. #ifdef CONFIG_F2FS_FS_COMPRESSION
  2617. if (f2fs_compressed_file(inode)) {
  2618. void *fsdata = NULL;
  2619. struct page *pagep;
  2620. int ret2;
  2621. ret = f2fs_init_compress_ctx(&cc);
  2622. if (ret) {
  2623. done = 1;
  2624. break;
  2625. }
  2626. if (!f2fs_cluster_can_merge_page(&cc,
  2627. folio->index)) {
  2628. ret = f2fs_write_multi_pages(&cc,
  2629. &submitted, wbc, io_type);
  2630. if (!ret)
  2631. need_readd = true;
  2632. goto result;
  2633. }
  2634. if (unlikely(f2fs_cp_error(sbi)))
  2635. goto lock_folio;
  2636. if (!f2fs_cluster_is_empty(&cc))
  2637. goto lock_folio;
  2638. if (f2fs_all_cluster_page_ready(&cc,
  2639. pages, i, nr_pages, true))
  2640. goto lock_folio;
  2641. ret2 = f2fs_prepare_compress_overwrite(
  2642. inode, &pagep,
  2643. folio->index, &fsdata);
  2644. if (ret2 < 0) {
  2645. ret = ret2;
  2646. done = 1;
  2647. break;
  2648. } else if (ret2 &&
  2649. (!f2fs_compress_write_end(inode,
  2650. fsdata, folio->index, 1) ||
  2651. !f2fs_all_cluster_page_ready(&cc,
  2652. pages, i, nr_pages,
  2653. false))) {
  2654. retry = 1;
  2655. break;
  2656. }
  2657. }
  2658. #endif
  2659. /* give a priority to WB_SYNC threads */
  2660. if (atomic_read(&sbi->wb_sync_req[DATA]) &&
  2661. wbc->sync_mode == WB_SYNC_NONE) {
  2662. done = 1;
  2663. break;
  2664. }
  2665. #ifdef CONFIG_F2FS_FS_COMPRESSION
  2666. lock_folio:
  2667. #endif
  2668. done_index = folio->index;
  2669. retry_write:
  2670. folio_lock(folio);
  2671. if (unlikely(folio->mapping != mapping)) {
  2672. continue_unlock:
  2673. folio_unlock(folio);
  2674. continue;
  2675. }
  2676. if (!folio_test_dirty(folio)) {
  2677. /* someone wrote it for us */
  2678. goto continue_unlock;
  2679. }
  2680. if (folio_test_writeback(folio)) {
  2681. if (wbc->sync_mode == WB_SYNC_NONE)
  2682. goto continue_unlock;
  2683. f2fs_wait_on_page_writeback(&folio->page, DATA, true, true);
  2684. }
  2685. if (!folio_clear_dirty_for_io(folio))
  2686. goto continue_unlock;
  2687. #ifdef CONFIG_F2FS_FS_COMPRESSION
  2688. if (f2fs_compressed_file(inode)) {
  2689. folio_get(folio);
  2690. f2fs_compress_ctx_add_page(&cc, folio);
  2691. continue;
  2692. }
  2693. #endif
  2694. ret = f2fs_write_single_data_page(folio,
  2695. &submitted, &bio, &last_block,
  2696. wbc, io_type, 0, true);
  2697. if (ret == AOP_WRITEPAGE_ACTIVATE)
  2698. folio_unlock(folio);
  2699. #ifdef CONFIG_F2FS_FS_COMPRESSION
  2700. result:
  2701. #endif
  2702. nwritten += submitted;
  2703. wbc->nr_to_write -= submitted;
  2704. if (unlikely(ret)) {
  2705. /*
  2706. * keep nr_to_write, since vfs uses this to
  2707. * get # of written pages.
  2708. */
  2709. if (ret == AOP_WRITEPAGE_ACTIVATE) {
  2710. ret = 0;
  2711. goto next;
  2712. } else if (ret == -EAGAIN) {
  2713. ret = 0;
  2714. if (wbc->sync_mode == WB_SYNC_ALL) {
  2715. f2fs_io_schedule_timeout(
  2716. DEFAULT_IO_TIMEOUT);
  2717. goto retry_write;
  2718. }
  2719. goto next;
  2720. }
  2721. done_index = folio_next_index(folio);
  2722. done = 1;
  2723. break;
  2724. }
  2725. if (wbc->nr_to_write <= 0 &&
  2726. wbc->sync_mode == WB_SYNC_NONE) {
  2727. done = 1;
  2728. break;
  2729. }
  2730. next:
  2731. if (need_readd)
  2732. goto readd;
  2733. }
  2734. release_pages(pages, nr_pages);
  2735. cond_resched();
  2736. }
  2737. #ifdef CONFIG_F2FS_FS_COMPRESSION
  2738. /* flush remained pages in compress cluster */
  2739. if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
  2740. ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
  2741. nwritten += submitted;
  2742. wbc->nr_to_write -= submitted;
  2743. if (ret) {
  2744. done = 1;
  2745. retry = 0;
  2746. }
  2747. }
  2748. if (f2fs_compressed_file(inode))
  2749. f2fs_destroy_compress_ctx(&cc, false);
  2750. #endif
  2751. if (retry) {
  2752. index = 0;
  2753. end = -1;
  2754. goto retry;
  2755. }
  2756. if (wbc->range_cyclic && !done)
  2757. done_index = 0;
  2758. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  2759. mapping->writeback_index = done_index;
  2760. if (nwritten)
  2761. f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
  2762. NULL, 0, DATA);
  2763. /* submit cached bio of IPU write */
  2764. if (bio)
  2765. f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
  2766. #ifdef CONFIG_F2FS_FS_COMPRESSION
  2767. if (pages != pages_local)
  2768. kfree(pages);
  2769. #endif
  2770. return ret;
  2771. }
  2772. static inline bool __should_serialize_io(struct inode *inode,
  2773. struct writeback_control *wbc)
  2774. {
  2775. /* to avoid deadlock in path of data flush */
  2776. if (F2FS_I(inode)->wb_task)
  2777. return false;
  2778. if (!S_ISREG(inode->i_mode))
  2779. return false;
  2780. if (IS_NOQUOTA(inode))
  2781. return false;
  2782. if (f2fs_need_compress_data(inode))
  2783. return true;
  2784. if (wbc->sync_mode != WB_SYNC_ALL)
  2785. return true;
  2786. if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
  2787. return true;
  2788. return false;
  2789. }
  2790. static int __f2fs_write_data_pages(struct address_space *mapping,
  2791. struct writeback_control *wbc,
  2792. enum iostat_type io_type)
  2793. {
  2794. struct inode *inode = mapping->host;
  2795. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  2796. struct blk_plug plug;
  2797. int ret;
  2798. bool locked = false;
  2799. /* deal with chardevs and other special file */
  2800. if (!mapping->a_ops->writepage)
  2801. return 0;
  2802. /* skip writing if there is no dirty page in this inode */
  2803. if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
  2804. return 0;
  2805. /* during POR, we don't need to trigger writepage at all. */
  2806. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  2807. goto skip_write;
  2808. if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
  2809. wbc->sync_mode == WB_SYNC_NONE &&
  2810. get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
  2811. f2fs_available_free_memory(sbi, DIRTY_DENTS))
  2812. goto skip_write;
  2813. /* skip writing in file defragment preparing stage */
  2814. if (is_inode_flag_set(inode, FI_SKIP_WRITES))
  2815. goto skip_write;
  2816. trace_f2fs_writepages(mapping->host, wbc, DATA);
  2817. /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
  2818. if (wbc->sync_mode == WB_SYNC_ALL)
  2819. atomic_inc(&sbi->wb_sync_req[DATA]);
  2820. else if (atomic_read(&sbi->wb_sync_req[DATA])) {
  2821. /* to avoid potential deadlock */
  2822. if (current->plug)
  2823. blk_finish_plug(current->plug);
  2824. goto skip_write;
  2825. }
  2826. if (__should_serialize_io(inode, wbc)) {
  2827. mutex_lock(&sbi->writepages);
  2828. locked = true;
  2829. }
  2830. blk_start_plug(&plug);
  2831. ret = f2fs_write_cache_pages(mapping, wbc, io_type);
  2832. blk_finish_plug(&plug);
  2833. if (locked)
  2834. mutex_unlock(&sbi->writepages);
  2835. if (wbc->sync_mode == WB_SYNC_ALL)
  2836. atomic_dec(&sbi->wb_sync_req[DATA]);
  2837. /*
  2838. * if some pages were truncated, we cannot guarantee its mapping->host
  2839. * to detect pending bios.
  2840. */
  2841. f2fs_remove_dirty_inode(inode);
  2842. return ret;
  2843. skip_write:
  2844. wbc->pages_skipped += get_dirty_pages(inode);
  2845. trace_f2fs_writepages(mapping->host, wbc, DATA);
  2846. return 0;
  2847. }
  2848. static int f2fs_write_data_pages(struct address_space *mapping,
  2849. struct writeback_control *wbc)
  2850. {
  2851. struct inode *inode = mapping->host;
  2852. return __f2fs_write_data_pages(mapping, wbc,
  2853. F2FS_I(inode)->cp_task == current ?
  2854. FS_CP_DATA_IO : FS_DATA_IO);
  2855. }
  2856. void f2fs_write_failed(struct inode *inode, loff_t to)
  2857. {
  2858. loff_t i_size = i_size_read(inode);
  2859. if (IS_NOQUOTA(inode))
  2860. return;
  2861. /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
  2862. if (to > i_size && !f2fs_verity_in_progress(inode)) {
  2863. f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  2864. filemap_invalidate_lock(inode->i_mapping);
  2865. truncate_pagecache(inode, i_size);
  2866. f2fs_truncate_blocks(inode, i_size, true);
  2867. filemap_invalidate_unlock(inode->i_mapping);
  2868. f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  2869. }
  2870. }
  2871. static int prepare_write_begin(struct f2fs_sb_info *sbi,
  2872. struct folio *folio, loff_t pos, unsigned int len,
  2873. block_t *blk_addr, bool *node_changed)
  2874. {
  2875. struct inode *inode = folio->mapping->host;
  2876. pgoff_t index = folio->index;
  2877. struct dnode_of_data dn;
  2878. struct page *ipage;
  2879. bool locked = false;
  2880. int flag = F2FS_GET_BLOCK_PRE_AIO;
  2881. int err = 0;
  2882. /*
  2883. * If a whole page is being written and we already preallocated all the
  2884. * blocks, then there is no need to get a block address now.
  2885. */
  2886. if (len == PAGE_SIZE && is_inode_flag_set(inode, FI_PREALLOCATED_ALL))
  2887. return 0;
  2888. /* f2fs_lock_op avoids race between write CP and convert_inline_page */
  2889. if (f2fs_has_inline_data(inode)) {
  2890. if (pos + len > MAX_INLINE_DATA(inode))
  2891. flag = F2FS_GET_BLOCK_DEFAULT;
  2892. f2fs_map_lock(sbi, flag);
  2893. locked = true;
  2894. } else if ((pos & PAGE_MASK) >= i_size_read(inode)) {
  2895. f2fs_map_lock(sbi, flag);
  2896. locked = true;
  2897. }
  2898. restart:
  2899. /* check inline_data */
  2900. ipage = f2fs_get_node_page(sbi, inode->i_ino);
  2901. if (IS_ERR(ipage)) {
  2902. err = PTR_ERR(ipage);
  2903. goto unlock_out;
  2904. }
  2905. set_new_dnode(&dn, inode, ipage, ipage, 0);
  2906. if (f2fs_has_inline_data(inode)) {
  2907. if (pos + len <= MAX_INLINE_DATA(inode)) {
  2908. f2fs_do_read_inline_data(folio, ipage);
  2909. set_inode_flag(inode, FI_DATA_EXIST);
  2910. if (inode->i_nlink)
  2911. set_page_private_inline(ipage);
  2912. goto out;
  2913. }
  2914. err = f2fs_convert_inline_page(&dn, folio_page(folio, 0));
  2915. if (err || dn.data_blkaddr != NULL_ADDR)
  2916. goto out;
  2917. }
  2918. if (!f2fs_lookup_read_extent_cache_block(inode, index,
  2919. &dn.data_blkaddr)) {
  2920. if (locked) {
  2921. err = f2fs_reserve_block(&dn, index);
  2922. goto out;
  2923. }
  2924. /* hole case */
  2925. err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
  2926. if (!err && dn.data_blkaddr != NULL_ADDR)
  2927. goto out;
  2928. f2fs_put_dnode(&dn);
  2929. f2fs_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO);
  2930. WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
  2931. locked = true;
  2932. goto restart;
  2933. }
  2934. out:
  2935. if (!err) {
  2936. /* convert_inline_page can make node_changed */
  2937. *blk_addr = dn.data_blkaddr;
  2938. *node_changed = dn.node_changed;
  2939. }
  2940. f2fs_put_dnode(&dn);
  2941. unlock_out:
  2942. if (locked)
  2943. f2fs_map_unlock(sbi, flag);
  2944. return err;
  2945. }
  2946. static int __find_data_block(struct inode *inode, pgoff_t index,
  2947. block_t *blk_addr)
  2948. {
  2949. struct dnode_of_data dn;
  2950. struct page *ipage;
  2951. int err = 0;
  2952. ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
  2953. if (IS_ERR(ipage))
  2954. return PTR_ERR(ipage);
  2955. set_new_dnode(&dn, inode, ipage, ipage, 0);
  2956. if (!f2fs_lookup_read_extent_cache_block(inode, index,
  2957. &dn.data_blkaddr)) {
  2958. /* hole case */
  2959. err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
  2960. if (err) {
  2961. dn.data_blkaddr = NULL_ADDR;
  2962. err = 0;
  2963. }
  2964. }
  2965. *blk_addr = dn.data_blkaddr;
  2966. f2fs_put_dnode(&dn);
  2967. return err;
  2968. }
  2969. static int __reserve_data_block(struct inode *inode, pgoff_t index,
  2970. block_t *blk_addr, bool *node_changed)
  2971. {
  2972. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  2973. struct dnode_of_data dn;
  2974. struct page *ipage;
  2975. int err = 0;
  2976. f2fs_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO);
  2977. ipage = f2fs_get_node_page(sbi, inode->i_ino);
  2978. if (IS_ERR(ipage)) {
  2979. err = PTR_ERR(ipage);
  2980. goto unlock_out;
  2981. }
  2982. set_new_dnode(&dn, inode, ipage, ipage, 0);
  2983. if (!f2fs_lookup_read_extent_cache_block(dn.inode, index,
  2984. &dn.data_blkaddr))
  2985. err = f2fs_reserve_block(&dn, index);
  2986. *blk_addr = dn.data_blkaddr;
  2987. *node_changed = dn.node_changed;
  2988. f2fs_put_dnode(&dn);
  2989. unlock_out:
  2990. f2fs_map_unlock(sbi, F2FS_GET_BLOCK_PRE_AIO);
  2991. return err;
  2992. }
  2993. static int prepare_atomic_write_begin(struct f2fs_sb_info *sbi,
  2994. struct folio *folio, loff_t pos, unsigned int len,
  2995. block_t *blk_addr, bool *node_changed, bool *use_cow)
  2996. {
  2997. struct inode *inode = folio->mapping->host;
  2998. struct inode *cow_inode = F2FS_I(inode)->cow_inode;
  2999. pgoff_t index = folio->index;
  3000. int err = 0;
  3001. block_t ori_blk_addr = NULL_ADDR;
  3002. /* If pos is beyond the end of file, reserve a new block in COW inode */
  3003. if ((pos & PAGE_MASK) >= i_size_read(inode))
  3004. goto reserve_block;
  3005. /* Look for the block in COW inode first */
  3006. err = __find_data_block(cow_inode, index, blk_addr);
  3007. if (err) {
  3008. return err;
  3009. } else if (*blk_addr != NULL_ADDR) {
  3010. *use_cow = true;
  3011. return 0;
  3012. }
  3013. if (is_inode_flag_set(inode, FI_ATOMIC_REPLACE))
  3014. goto reserve_block;
  3015. /* Look for the block in the original inode */
  3016. err = __find_data_block(inode, index, &ori_blk_addr);
  3017. if (err)
  3018. return err;
  3019. reserve_block:
  3020. /* Finally, we should reserve a new block in COW inode for the update */
  3021. err = __reserve_data_block(cow_inode, index, blk_addr, node_changed);
  3022. if (err)
  3023. return err;
  3024. inc_atomic_write_cnt(inode);
  3025. if (ori_blk_addr != NULL_ADDR)
  3026. *blk_addr = ori_blk_addr;
  3027. return 0;
  3028. }
  3029. static int f2fs_write_begin(struct file *file, struct address_space *mapping,
  3030. loff_t pos, unsigned len, struct folio **foliop, void **fsdata)
  3031. {
  3032. struct inode *inode = mapping->host;
  3033. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  3034. struct folio *folio;
  3035. pgoff_t index = pos >> PAGE_SHIFT;
  3036. bool need_balance = false;
  3037. bool use_cow = false;
  3038. block_t blkaddr = NULL_ADDR;
  3039. int err = 0;
  3040. trace_f2fs_write_begin(inode, pos, len);
  3041. if (!f2fs_is_checkpoint_ready(sbi)) {
  3042. err = -ENOSPC;
  3043. goto fail;
  3044. }
  3045. /*
  3046. * We should check this at this moment to avoid deadlock on inode page
  3047. * and #0 page. The locking rule for inline_data conversion should be:
  3048. * folio_lock(folio #0) -> folio_lock(inode_page)
  3049. */
  3050. if (index != 0) {
  3051. err = f2fs_convert_inline_inode(inode);
  3052. if (err)
  3053. goto fail;
  3054. }
  3055. #ifdef CONFIG_F2FS_FS_COMPRESSION
  3056. if (f2fs_compressed_file(inode)) {
  3057. int ret;
  3058. struct page *page;
  3059. *fsdata = NULL;
  3060. if (len == PAGE_SIZE && !(f2fs_is_atomic_file(inode)))
  3061. goto repeat;
  3062. ret = f2fs_prepare_compress_overwrite(inode, &page,
  3063. index, fsdata);
  3064. if (ret < 0) {
  3065. err = ret;
  3066. goto fail;
  3067. } else if (ret) {
  3068. *foliop = page_folio(page);
  3069. return 0;
  3070. }
  3071. }
  3072. #endif
  3073. repeat:
  3074. /*
  3075. * Do not use FGP_STABLE to avoid deadlock.
  3076. * Will wait that below with our IO control.
  3077. */
  3078. folio = __filemap_get_folio(mapping, index,
  3079. FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
  3080. if (IS_ERR(folio)) {
  3081. err = PTR_ERR(folio);
  3082. goto fail;
  3083. }
  3084. /* TODO: cluster can be compressed due to race with .writepage */
  3085. *foliop = folio;
  3086. if (f2fs_is_atomic_file(inode))
  3087. err = prepare_atomic_write_begin(sbi, folio, pos, len,
  3088. &blkaddr, &need_balance, &use_cow);
  3089. else
  3090. err = prepare_write_begin(sbi, folio, pos, len,
  3091. &blkaddr, &need_balance);
  3092. if (err)
  3093. goto put_folio;
  3094. if (need_balance && !IS_NOQUOTA(inode) &&
  3095. has_not_enough_free_secs(sbi, 0, 0)) {
  3096. folio_unlock(folio);
  3097. f2fs_balance_fs(sbi, true);
  3098. folio_lock(folio);
  3099. if (folio->mapping != mapping) {
  3100. /* The folio got truncated from under us */
  3101. folio_unlock(folio);
  3102. folio_put(folio);
  3103. goto repeat;
  3104. }
  3105. }
  3106. f2fs_wait_on_page_writeback(&folio->page, DATA, false, true);
  3107. if (len == folio_size(folio) || folio_test_uptodate(folio))
  3108. return 0;
  3109. if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
  3110. !f2fs_verity_in_progress(inode)) {
  3111. folio_zero_segment(folio, len, folio_size(folio));
  3112. return 0;
  3113. }
  3114. if (blkaddr == NEW_ADDR) {
  3115. folio_zero_segment(folio, 0, folio_size(folio));
  3116. folio_mark_uptodate(folio);
  3117. } else {
  3118. if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
  3119. DATA_GENERIC_ENHANCE_READ)) {
  3120. err = -EFSCORRUPTED;
  3121. goto put_folio;
  3122. }
  3123. err = f2fs_submit_page_read(use_cow ?
  3124. F2FS_I(inode)->cow_inode : inode,
  3125. folio, blkaddr, 0, true);
  3126. if (err)
  3127. goto put_folio;
  3128. folio_lock(folio);
  3129. if (unlikely(folio->mapping != mapping)) {
  3130. folio_unlock(folio);
  3131. folio_put(folio);
  3132. goto repeat;
  3133. }
  3134. if (unlikely(!folio_test_uptodate(folio))) {
  3135. err = -EIO;
  3136. goto put_folio;
  3137. }
  3138. }
  3139. return 0;
  3140. put_folio:
  3141. folio_unlock(folio);
  3142. folio_put(folio);
  3143. fail:
  3144. f2fs_write_failed(inode, pos + len);
  3145. return err;
  3146. }
  3147. static int f2fs_write_end(struct file *file,
  3148. struct address_space *mapping,
  3149. loff_t pos, unsigned len, unsigned copied,
  3150. struct folio *folio, void *fsdata)
  3151. {
  3152. struct inode *inode = folio->mapping->host;
  3153. trace_f2fs_write_end(inode, pos, len, copied);
  3154. /*
  3155. * This should be come from len == PAGE_SIZE, and we expect copied
  3156. * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
  3157. * let generic_perform_write() try to copy data again through copied=0.
  3158. */
  3159. if (!folio_test_uptodate(folio)) {
  3160. if (unlikely(copied != len))
  3161. copied = 0;
  3162. else
  3163. folio_mark_uptodate(folio);
  3164. }
  3165. #ifdef CONFIG_F2FS_FS_COMPRESSION
  3166. /* overwrite compressed file */
  3167. if (f2fs_compressed_file(inode) && fsdata) {
  3168. f2fs_compress_write_end(inode, fsdata, folio->index, copied);
  3169. f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
  3170. if (pos + copied > i_size_read(inode) &&
  3171. !f2fs_verity_in_progress(inode))
  3172. f2fs_i_size_write(inode, pos + copied);
  3173. return copied;
  3174. }
  3175. #endif
  3176. if (!copied)
  3177. goto unlock_out;
  3178. folio_mark_dirty(folio);
  3179. if (f2fs_is_atomic_file(inode))
  3180. set_page_private_atomic(folio_page(folio, 0));
  3181. if (pos + copied > i_size_read(inode) &&
  3182. !f2fs_verity_in_progress(inode)) {
  3183. f2fs_i_size_write(inode, pos + copied);
  3184. if (f2fs_is_atomic_file(inode))
  3185. f2fs_i_size_write(F2FS_I(inode)->cow_inode,
  3186. pos + copied);
  3187. }
  3188. unlock_out:
  3189. folio_unlock(folio);
  3190. folio_put(folio);
  3191. f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
  3192. return copied;
  3193. }
  3194. void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length)
  3195. {
  3196. struct inode *inode = folio->mapping->host;
  3197. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  3198. if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
  3199. (offset || length != folio_size(folio)))
  3200. return;
  3201. if (folio_test_dirty(folio)) {
  3202. if (inode->i_ino == F2FS_META_INO(sbi)) {
  3203. dec_page_count(sbi, F2FS_DIRTY_META);
  3204. } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
  3205. dec_page_count(sbi, F2FS_DIRTY_NODES);
  3206. } else {
  3207. inode_dec_dirty_pages(inode);
  3208. f2fs_remove_dirty_inode(inode);
  3209. }
  3210. }
  3211. clear_page_private_all(&folio->page);
  3212. }
  3213. bool f2fs_release_folio(struct folio *folio, gfp_t wait)
  3214. {
  3215. /* If this is dirty folio, keep private data */
  3216. if (folio_test_dirty(folio))
  3217. return false;
  3218. clear_page_private_all(&folio->page);
  3219. return true;
  3220. }
  3221. static bool f2fs_dirty_data_folio(struct address_space *mapping,
  3222. struct folio *folio)
  3223. {
  3224. struct inode *inode = mapping->host;
  3225. trace_f2fs_set_page_dirty(folio, DATA);
  3226. if (!folio_test_uptodate(folio))
  3227. folio_mark_uptodate(folio);
  3228. BUG_ON(folio_test_swapcache(folio));
  3229. if (filemap_dirty_folio(mapping, folio)) {
  3230. f2fs_update_dirty_folio(inode, folio);
  3231. return true;
  3232. }
  3233. return false;
  3234. }
  3235. static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
  3236. {
  3237. #ifdef CONFIG_F2FS_FS_COMPRESSION
  3238. struct dnode_of_data dn;
  3239. sector_t start_idx, blknr = 0;
  3240. int ret;
  3241. start_idx = round_down(block, F2FS_I(inode)->i_cluster_size);
  3242. set_new_dnode(&dn, inode, NULL, NULL, 0);
  3243. ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
  3244. if (ret)
  3245. return 0;
  3246. if (dn.data_blkaddr != COMPRESS_ADDR) {
  3247. dn.ofs_in_node += block - start_idx;
  3248. blknr = f2fs_data_blkaddr(&dn);
  3249. if (!__is_valid_data_blkaddr(blknr))
  3250. blknr = 0;
  3251. }
  3252. f2fs_put_dnode(&dn);
  3253. return blknr;
  3254. #else
  3255. return 0;
  3256. #endif
  3257. }
  3258. static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
  3259. {
  3260. struct inode *inode = mapping->host;
  3261. sector_t blknr = 0;
  3262. if (f2fs_has_inline_data(inode))
  3263. goto out;
  3264. /* make sure allocating whole blocks */
  3265. if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
  3266. filemap_write_and_wait(mapping);
  3267. /* Block number less than F2FS MAX BLOCKS */
  3268. if (unlikely(block >= max_file_blocks(inode)))
  3269. goto out;
  3270. if (f2fs_compressed_file(inode)) {
  3271. blknr = f2fs_bmap_compress(inode, block);
  3272. } else {
  3273. struct f2fs_map_blocks map;
  3274. memset(&map, 0, sizeof(map));
  3275. map.m_lblk = block;
  3276. map.m_len = 1;
  3277. map.m_next_pgofs = NULL;
  3278. map.m_seg_type = NO_CHECK_TYPE;
  3279. if (!f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_BMAP))
  3280. blknr = map.m_pblk;
  3281. }
  3282. out:
  3283. trace_f2fs_bmap(inode, block, blknr);
  3284. return blknr;
  3285. }
  3286. #ifdef CONFIG_SWAP
  3287. static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk,
  3288. unsigned int blkcnt)
  3289. {
  3290. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  3291. unsigned int blkofs;
  3292. unsigned int blk_per_sec = BLKS_PER_SEC(sbi);
  3293. unsigned int end_blk = start_blk + blkcnt - 1;
  3294. unsigned int secidx = start_blk / blk_per_sec;
  3295. unsigned int end_sec;
  3296. int ret = 0;
  3297. if (!blkcnt)
  3298. return 0;
  3299. end_sec = end_blk / blk_per_sec;
  3300. f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  3301. filemap_invalidate_lock(inode->i_mapping);
  3302. set_inode_flag(inode, FI_ALIGNED_WRITE);
  3303. set_inode_flag(inode, FI_OPU_WRITE);
  3304. for (; secidx <= end_sec; secidx++) {
  3305. unsigned int blkofs_end = secidx == end_sec ?
  3306. end_blk % blk_per_sec : blk_per_sec - 1;
  3307. f2fs_down_write(&sbi->pin_sem);
  3308. ret = f2fs_allocate_pinning_section(sbi);
  3309. if (ret) {
  3310. f2fs_up_write(&sbi->pin_sem);
  3311. break;
  3312. }
  3313. set_inode_flag(inode, FI_SKIP_WRITES);
  3314. for (blkofs = 0; blkofs <= blkofs_end; blkofs++) {
  3315. struct page *page;
  3316. unsigned int blkidx = secidx * blk_per_sec + blkofs;
  3317. page = f2fs_get_lock_data_page(inode, blkidx, true);
  3318. if (IS_ERR(page)) {
  3319. f2fs_up_write(&sbi->pin_sem);
  3320. ret = PTR_ERR(page);
  3321. goto done;
  3322. }
  3323. set_page_dirty(page);
  3324. f2fs_put_page(page, 1);
  3325. }
  3326. clear_inode_flag(inode, FI_SKIP_WRITES);
  3327. ret = filemap_fdatawrite(inode->i_mapping);
  3328. f2fs_up_write(&sbi->pin_sem);
  3329. if (ret)
  3330. break;
  3331. }
  3332. done:
  3333. clear_inode_flag(inode, FI_SKIP_WRITES);
  3334. clear_inode_flag(inode, FI_OPU_WRITE);
  3335. clear_inode_flag(inode, FI_ALIGNED_WRITE);
  3336. filemap_invalidate_unlock(inode->i_mapping);
  3337. f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
  3338. return ret;
  3339. }
  3340. static int check_swap_activate(struct swap_info_struct *sis,
  3341. struct file *swap_file, sector_t *span)
  3342. {
  3343. struct address_space *mapping = swap_file->f_mapping;
  3344. struct inode *inode = mapping->host;
  3345. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  3346. block_t cur_lblock;
  3347. block_t last_lblock;
  3348. block_t pblock;
  3349. block_t lowest_pblock = -1;
  3350. block_t highest_pblock = 0;
  3351. int nr_extents = 0;
  3352. unsigned int nr_pblocks;
  3353. unsigned int blks_per_sec = BLKS_PER_SEC(sbi);
  3354. unsigned int not_aligned = 0;
  3355. int ret = 0;
  3356. /*
  3357. * Map all the blocks into the extent list. This code doesn't try
  3358. * to be very smart.
  3359. */
  3360. cur_lblock = 0;
  3361. last_lblock = F2FS_BYTES_TO_BLK(i_size_read(inode));
  3362. while (cur_lblock < last_lblock && cur_lblock < sis->max) {
  3363. struct f2fs_map_blocks map;
  3364. retry:
  3365. cond_resched();
  3366. memset(&map, 0, sizeof(map));
  3367. map.m_lblk = cur_lblock;
  3368. map.m_len = last_lblock - cur_lblock;
  3369. map.m_next_pgofs = NULL;
  3370. map.m_next_extent = NULL;
  3371. map.m_seg_type = NO_CHECK_TYPE;
  3372. map.m_may_create = false;
  3373. ret = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_FIEMAP);
  3374. if (ret)
  3375. goto out;
  3376. /* hole */
  3377. if (!(map.m_flags & F2FS_MAP_FLAGS)) {
  3378. f2fs_err(sbi, "Swapfile has holes");
  3379. ret = -EINVAL;
  3380. goto out;
  3381. }
  3382. pblock = map.m_pblk;
  3383. nr_pblocks = map.m_len;
  3384. if ((pblock - SM_I(sbi)->main_blkaddr) % blks_per_sec ||
  3385. nr_pblocks % blks_per_sec ||
  3386. !f2fs_valid_pinned_area(sbi, pblock)) {
  3387. bool last_extent = false;
  3388. not_aligned++;
  3389. nr_pblocks = roundup(nr_pblocks, blks_per_sec);
  3390. if (cur_lblock + nr_pblocks > sis->max)
  3391. nr_pblocks -= blks_per_sec;
  3392. /* this extent is last one */
  3393. if (!nr_pblocks) {
  3394. nr_pblocks = last_lblock - cur_lblock;
  3395. last_extent = true;
  3396. }
  3397. ret = f2fs_migrate_blocks(inode, cur_lblock,
  3398. nr_pblocks);
  3399. if (ret) {
  3400. if (ret == -ENOENT)
  3401. ret = -EINVAL;
  3402. goto out;
  3403. }
  3404. if (!last_extent)
  3405. goto retry;
  3406. }
  3407. if (cur_lblock + nr_pblocks >= sis->max)
  3408. nr_pblocks = sis->max - cur_lblock;
  3409. if (cur_lblock) { /* exclude the header page */
  3410. if (pblock < lowest_pblock)
  3411. lowest_pblock = pblock;
  3412. if (pblock + nr_pblocks - 1 > highest_pblock)
  3413. highest_pblock = pblock + nr_pblocks - 1;
  3414. }
  3415. /*
  3416. * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
  3417. */
  3418. ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock);
  3419. if (ret < 0)
  3420. goto out;
  3421. nr_extents += ret;
  3422. cur_lblock += nr_pblocks;
  3423. }
  3424. ret = nr_extents;
  3425. *span = 1 + highest_pblock - lowest_pblock;
  3426. if (cur_lblock == 0)
  3427. cur_lblock = 1; /* force Empty message */
  3428. sis->max = cur_lblock;
  3429. sis->pages = cur_lblock - 1;
  3430. sis->highest_bit = cur_lblock - 1;
  3431. out:
  3432. if (not_aligned)
  3433. f2fs_warn(sbi, "Swapfile (%u) is not align to section: 1) creat(), 2) ioctl(F2FS_IOC_SET_PIN_FILE), 3) fallocate(%lu * N)",
  3434. not_aligned, blks_per_sec * F2FS_BLKSIZE);
  3435. return ret;
  3436. }
  3437. static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
  3438. sector_t *span)
  3439. {
  3440. struct inode *inode = file_inode(file);
  3441. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  3442. int ret;
  3443. if (!S_ISREG(inode->i_mode))
  3444. return -EINVAL;
  3445. if (f2fs_readonly(sbi->sb))
  3446. return -EROFS;
  3447. if (f2fs_lfs_mode(sbi) && !f2fs_sb_has_blkzoned(sbi)) {
  3448. f2fs_err(sbi, "Swapfile not supported in LFS mode");
  3449. return -EINVAL;
  3450. }
  3451. ret = f2fs_convert_inline_inode(inode);
  3452. if (ret)
  3453. return ret;
  3454. if (!f2fs_disable_compressed_file(inode))
  3455. return -EINVAL;
  3456. ret = filemap_fdatawrite(inode->i_mapping);
  3457. if (ret < 0)
  3458. return ret;
  3459. f2fs_precache_extents(inode);
  3460. ret = check_swap_activate(sis, file, span);
  3461. if (ret < 0)
  3462. return ret;
  3463. stat_inc_swapfile_inode(inode);
  3464. set_inode_flag(inode, FI_PIN_FILE);
  3465. f2fs_update_time(sbi, REQ_TIME);
  3466. return ret;
  3467. }
  3468. static void f2fs_swap_deactivate(struct file *file)
  3469. {
  3470. struct inode *inode = file_inode(file);
  3471. stat_dec_swapfile_inode(inode);
  3472. clear_inode_flag(inode, FI_PIN_FILE);
  3473. }
  3474. #else
  3475. static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
  3476. sector_t *span)
  3477. {
  3478. return -EOPNOTSUPP;
  3479. }
  3480. static void f2fs_swap_deactivate(struct file *file)
  3481. {
  3482. }
  3483. #endif
  3484. const struct address_space_operations f2fs_dblock_aops = {
  3485. .read_folio = f2fs_read_data_folio,
  3486. .readahead = f2fs_readahead,
  3487. .writepage = f2fs_write_data_page,
  3488. .writepages = f2fs_write_data_pages,
  3489. .write_begin = f2fs_write_begin,
  3490. .write_end = f2fs_write_end,
  3491. .dirty_folio = f2fs_dirty_data_folio,
  3492. .migrate_folio = filemap_migrate_folio,
  3493. .invalidate_folio = f2fs_invalidate_folio,
  3494. .release_folio = f2fs_release_folio,
  3495. .bmap = f2fs_bmap,
  3496. .swap_activate = f2fs_swap_activate,
  3497. .swap_deactivate = f2fs_swap_deactivate,
  3498. };
  3499. void f2fs_clear_page_cache_dirty_tag(struct folio *folio)
  3500. {
  3501. struct address_space *mapping = folio->mapping;
  3502. unsigned long flags;
  3503. xa_lock_irqsave(&mapping->i_pages, flags);
  3504. __xa_clear_mark(&mapping->i_pages, folio->index,
  3505. PAGECACHE_TAG_DIRTY);
  3506. xa_unlock_irqrestore(&mapping->i_pages, flags);
  3507. }
  3508. int __init f2fs_init_post_read_processing(void)
  3509. {
  3510. bio_post_read_ctx_cache =
  3511. kmem_cache_create("f2fs_bio_post_read_ctx",
  3512. sizeof(struct bio_post_read_ctx), 0, 0, NULL);
  3513. if (!bio_post_read_ctx_cache)
  3514. goto fail;
  3515. bio_post_read_ctx_pool =
  3516. mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
  3517. bio_post_read_ctx_cache);
  3518. if (!bio_post_read_ctx_pool)
  3519. goto fail_free_cache;
  3520. return 0;
  3521. fail_free_cache:
  3522. kmem_cache_destroy(bio_post_read_ctx_cache);
  3523. fail:
  3524. return -ENOMEM;
  3525. }
  3526. void f2fs_destroy_post_read_processing(void)
  3527. {
  3528. mempool_destroy(bio_post_read_ctx_pool);
  3529. kmem_cache_destroy(bio_post_read_ctx_cache);
  3530. }
  3531. int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
  3532. {
  3533. if (!f2fs_sb_has_encrypt(sbi) &&
  3534. !f2fs_sb_has_verity(sbi) &&
  3535. !f2fs_sb_has_compression(sbi))
  3536. return 0;
  3537. sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
  3538. WQ_UNBOUND | WQ_HIGHPRI,
  3539. num_online_cpus());
  3540. return sbi->post_read_wq ? 0 : -ENOMEM;
  3541. }
  3542. void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
  3543. {
  3544. if (sbi->post_read_wq)
  3545. destroy_workqueue(sbi->post_read_wq);
  3546. }
  3547. int __init f2fs_init_bio_entry_cache(void)
  3548. {
  3549. bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab",
  3550. sizeof(struct bio_entry));
  3551. return bio_entry_slab ? 0 : -ENOMEM;
  3552. }
  3553. void f2fs_destroy_bio_entry_cache(void)
  3554. {
  3555. kmem_cache_destroy(bio_entry_slab);
  3556. }
  3557. static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
  3558. unsigned int flags, struct iomap *iomap,
  3559. struct iomap *srcmap)
  3560. {
  3561. struct f2fs_map_blocks map = {};
  3562. pgoff_t next_pgofs = 0;
  3563. int err;
  3564. map.m_lblk = F2FS_BYTES_TO_BLK(offset);
  3565. map.m_len = F2FS_BYTES_TO_BLK(offset + length - 1) - map.m_lblk + 1;
  3566. map.m_next_pgofs = &next_pgofs;
  3567. map.m_seg_type = f2fs_rw_hint_to_seg_type(F2FS_I_SB(inode),
  3568. inode->i_write_hint);
  3569. if (flags & IOMAP_WRITE)
  3570. map.m_may_create = true;
  3571. err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DIO);
  3572. if (err)
  3573. return err;
  3574. iomap->offset = F2FS_BLK_TO_BYTES(map.m_lblk);
  3575. /*
  3576. * When inline encryption is enabled, sometimes I/O to an encrypted file
  3577. * has to be broken up to guarantee DUN contiguity. Handle this by
  3578. * limiting the length of the mapping returned.
  3579. */
  3580. map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len);
  3581. /*
  3582. * We should never see delalloc or compressed extents here based on
  3583. * prior flushing and checks.
  3584. */
  3585. if (WARN_ON_ONCE(map.m_pblk == COMPRESS_ADDR))
  3586. return -EINVAL;
  3587. if (map.m_flags & F2FS_MAP_MAPPED) {
  3588. if (WARN_ON_ONCE(map.m_pblk == NEW_ADDR))
  3589. return -EINVAL;
  3590. iomap->length = F2FS_BLK_TO_BYTES(map.m_len);
  3591. iomap->type = IOMAP_MAPPED;
  3592. iomap->flags |= IOMAP_F_MERGED;
  3593. iomap->bdev = map.m_bdev;
  3594. iomap->addr = F2FS_BLK_TO_BYTES(map.m_pblk);
  3595. } else {
  3596. if (flags & IOMAP_WRITE)
  3597. return -ENOTBLK;
  3598. if (map.m_pblk == NULL_ADDR) {
  3599. iomap->length = F2FS_BLK_TO_BYTES(next_pgofs) -
  3600. iomap->offset;
  3601. iomap->type = IOMAP_HOLE;
  3602. } else if (map.m_pblk == NEW_ADDR) {
  3603. iomap->length = F2FS_BLK_TO_BYTES(map.m_len);
  3604. iomap->type = IOMAP_UNWRITTEN;
  3605. } else {
  3606. f2fs_bug_on(F2FS_I_SB(inode), 1);
  3607. }
  3608. iomap->addr = IOMAP_NULL_ADDR;
  3609. }
  3610. if (map.m_flags & F2FS_MAP_NEW)
  3611. iomap->flags |= IOMAP_F_NEW;
  3612. if ((inode->i_state & I_DIRTY_DATASYNC) ||
  3613. offset + length > i_size_read(inode))
  3614. iomap->flags |= IOMAP_F_DIRTY;
  3615. return 0;
  3616. }
  3617. const struct iomap_ops f2fs_iomap_ops = {
  3618. .iomap_begin = f2fs_iomap_begin,
  3619. };