extent_io.c 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/bitops.h>
  3. #include <linux/slab.h>
  4. #include <linux/bio.h>
  5. #include <linux/mm.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/page-flags.h>
  8. #include <linux/sched/mm.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/swap.h>
  12. #include <linux/writeback.h>
  13. #include <linux/pagevec.h>
  14. #include <linux/prefetch.h>
  15. #include <linux/fsverity.h>
  16. #include "extent_io.h"
  17. #include "extent-io-tree.h"
  18. #include "extent_map.h"
  19. #include "ctree.h"
  20. #include "btrfs_inode.h"
  21. #include "bio.h"
  22. #include "locking.h"
  23. #include "backref.h"
  24. #include "disk-io.h"
  25. #include "subpage.h"
  26. #include "zoned.h"
  27. #include "block-group.h"
  28. #include "compression.h"
  29. #include "fs.h"
  30. #include "accessors.h"
  31. #include "file-item.h"
  32. #include "file.h"
  33. #include "dev-replace.h"
  34. #include "super.h"
  35. #include "transaction.h"
  36. static struct kmem_cache *extent_buffer_cache;
  37. #ifdef CONFIG_BTRFS_DEBUG
  38. static inline void btrfs_leak_debug_add_eb(struct extent_buffer *eb)
  39. {
  40. struct btrfs_fs_info *fs_info = eb->fs_info;
  41. unsigned long flags;
  42. spin_lock_irqsave(&fs_info->eb_leak_lock, flags);
  43. list_add(&eb->leak_list, &fs_info->allocated_ebs);
  44. spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags);
  45. }
  46. static inline void btrfs_leak_debug_del_eb(struct extent_buffer *eb)
  47. {
  48. struct btrfs_fs_info *fs_info = eb->fs_info;
  49. unsigned long flags;
  50. spin_lock_irqsave(&fs_info->eb_leak_lock, flags);
  51. list_del(&eb->leak_list);
  52. spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags);
  53. }
  54. void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info)
  55. {
  56. struct extent_buffer *eb;
  57. unsigned long flags;
  58. /*
  59. * If we didn't get into open_ctree our allocated_ebs will not be
  60. * initialized, so just skip this.
  61. */
  62. if (!fs_info->allocated_ebs.next)
  63. return;
  64. WARN_ON(!list_empty(&fs_info->allocated_ebs));
  65. spin_lock_irqsave(&fs_info->eb_leak_lock, flags);
  66. while (!list_empty(&fs_info->allocated_ebs)) {
  67. eb = list_first_entry(&fs_info->allocated_ebs,
  68. struct extent_buffer, leak_list);
  69. btrfs_err(fs_info,
  70. "buffer leak start %llu len %u refs %d bflags %lu owner %llu",
  71. eb->start, eb->len, refcount_read(&eb->refs), eb->bflags,
  72. btrfs_header_owner(eb));
  73. list_del(&eb->leak_list);
  74. WARN_ON_ONCE(1);
  75. kmem_cache_free(extent_buffer_cache, eb);
  76. }
  77. spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags);
  78. }
  79. #else
  80. #define btrfs_leak_debug_add_eb(eb) do {} while (0)
  81. #define btrfs_leak_debug_del_eb(eb) do {} while (0)
  82. #endif
  83. /*
  84. * Structure to record info about the bio being assembled, and other info like
  85. * how many bytes are there before stripe/ordered extent boundary.
  86. */
  87. struct btrfs_bio_ctrl {
  88. struct btrfs_bio *bbio;
  89. /* Last byte contained in bbio + 1 . */
  90. loff_t next_file_offset;
  91. enum btrfs_compression_type compress_type;
  92. u32 len_to_oe_boundary;
  93. blk_opf_t opf;
  94. /*
  95. * For data read bios, we attempt to optimize csum lookups if the extent
  96. * generation is older than the current one. To make this possible, we
  97. * need to track the maximum generation of an extent in a bio_ctrl to
  98. * make the decision when submitting the bio.
  99. *
  100. * The pattern between do_readpage(), submit_one_bio() and
  101. * submit_extent_folio() is quite subtle, so tracking this is tricky.
  102. *
  103. * As we process extent E, we might submit a bio with existing built up
  104. * extents before adding E to a new bio, or we might just add E to the
  105. * bio. As a result, E's generation could apply to the current bio or
  106. * to the next one, so we need to be careful to update the bio_ctrl's
  107. * generation with E's only when we are sure E is added to bio_ctrl->bbio
  108. * in submit_extent_folio().
  109. *
  110. * See the comment in btrfs_lookup_bio_sums() for more detail on the
  111. * need for this optimization.
  112. */
  113. u64 generation;
  114. btrfs_bio_end_io_t end_io_func;
  115. struct writeback_control *wbc;
  116. /*
  117. * The sectors of the page which are going to be submitted by
  118. * extent_writepage_io().
  119. * This is to avoid touching ranges covered by compression/inline.
  120. */
  121. unsigned long submit_bitmap;
  122. struct readahead_control *ractl;
  123. /*
  124. * The start offset of the last used extent map by a read operation.
  125. *
  126. * This is for proper compressed read merge.
  127. * U64_MAX means we are starting the read and have made no progress yet.
  128. *
  129. * The current btrfs_bio_is_contig() only uses disk_bytenr as
  130. * the condition to check if the read can be merged with previous
  131. * bio, which is not correct. E.g. two file extents pointing to the
  132. * same extent but with different offset.
  133. *
  134. * So here we need to do extra checks to only merge reads that are
  135. * covered by the same extent map.
  136. * Just extent_map::start will be enough, as they are unique
  137. * inside the same inode.
  138. */
  139. u64 last_em_start;
  140. };
  141. /*
  142. * Helper to set the csum search commit root option for a bio_ctrl's bbio
  143. * before submitting the bio.
  144. *
  145. * Only for use by submit_one_bio().
  146. */
  147. static void bio_set_csum_search_commit_root(struct btrfs_bio_ctrl *bio_ctrl)
  148. {
  149. struct btrfs_bio *bbio = bio_ctrl->bbio;
  150. ASSERT(bbio);
  151. if (!(btrfs_op(&bbio->bio) == BTRFS_MAP_READ && is_data_inode(bbio->inode)))
  152. return;
  153. bio_ctrl->bbio->csum_search_commit_root =
  154. (bio_ctrl->generation &&
  155. bio_ctrl->generation < btrfs_get_fs_generation(bbio->inode->root->fs_info));
  156. }
  157. static void submit_one_bio(struct btrfs_bio_ctrl *bio_ctrl)
  158. {
  159. struct btrfs_bio *bbio = bio_ctrl->bbio;
  160. if (!bbio)
  161. return;
  162. /* Caller should ensure the bio has at least some range added */
  163. ASSERT(bbio->bio.bi_iter.bi_size);
  164. bio_set_csum_search_commit_root(bio_ctrl);
  165. if (btrfs_op(&bbio->bio) == BTRFS_MAP_READ &&
  166. bio_ctrl->compress_type != BTRFS_COMPRESS_NONE)
  167. btrfs_submit_compressed_read(bbio);
  168. else
  169. btrfs_submit_bbio(bbio, 0);
  170. /* The bbio is owned by the end_io handler now */
  171. bio_ctrl->bbio = NULL;
  172. /*
  173. * We used the generation to decide whether to lookup csums in the
  174. * commit_root or not when we called bio_set_csum_search_commit_root()
  175. * above. Now, reset the generation for the next bio.
  176. */
  177. bio_ctrl->generation = 0;
  178. }
  179. /*
  180. * Submit or fail the current bio in the bio_ctrl structure.
  181. */
  182. static void submit_write_bio(struct btrfs_bio_ctrl *bio_ctrl, int ret)
  183. {
  184. struct btrfs_bio *bbio = bio_ctrl->bbio;
  185. if (!bbio)
  186. return;
  187. if (ret) {
  188. ASSERT(ret < 0);
  189. btrfs_bio_end_io(bbio, errno_to_blk_status(ret));
  190. /* The bio is owned by the end_io handler now */
  191. bio_ctrl->bbio = NULL;
  192. } else {
  193. submit_one_bio(bio_ctrl);
  194. }
  195. }
  196. int __init extent_buffer_init_cachep(void)
  197. {
  198. extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
  199. sizeof(struct extent_buffer), 0, 0,
  200. NULL);
  201. if (!extent_buffer_cache)
  202. return -ENOMEM;
  203. return 0;
  204. }
  205. void __cold extent_buffer_free_cachep(void)
  206. {
  207. /*
  208. * Make sure all delayed rcu free are flushed before we
  209. * destroy caches.
  210. */
  211. rcu_barrier();
  212. kmem_cache_destroy(extent_buffer_cache);
  213. }
  214. static void process_one_folio(struct btrfs_fs_info *fs_info,
  215. struct folio *folio, const struct folio *locked_folio,
  216. unsigned long page_ops, u64 start, u64 end)
  217. {
  218. u32 len;
  219. ASSERT(end + 1 - start != 0 && end + 1 - start < U32_MAX);
  220. len = end + 1 - start;
  221. if (page_ops & PAGE_SET_ORDERED)
  222. btrfs_folio_clamp_set_ordered(fs_info, folio, start, len);
  223. if (page_ops & PAGE_START_WRITEBACK) {
  224. btrfs_folio_clamp_clear_dirty(fs_info, folio, start, len);
  225. btrfs_folio_clamp_set_writeback(fs_info, folio, start, len);
  226. }
  227. if (page_ops & PAGE_END_WRITEBACK)
  228. btrfs_folio_clamp_clear_writeback(fs_info, folio, start, len);
  229. if (folio != locked_folio && (page_ops & PAGE_UNLOCK))
  230. btrfs_folio_end_lock(fs_info, folio, start, len);
  231. }
  232. static void __process_folios_contig(struct address_space *mapping,
  233. const struct folio *locked_folio, u64 start,
  234. u64 end, unsigned long page_ops)
  235. {
  236. struct btrfs_fs_info *fs_info = inode_to_fs_info(mapping->host);
  237. pgoff_t index = start >> PAGE_SHIFT;
  238. pgoff_t end_index = end >> PAGE_SHIFT;
  239. struct folio_batch fbatch;
  240. int i;
  241. folio_batch_init(&fbatch);
  242. while (index <= end_index) {
  243. int found_folios;
  244. found_folios = filemap_get_folios_contig(mapping, &index,
  245. end_index, &fbatch);
  246. for (i = 0; i < found_folios; i++) {
  247. struct folio *folio = fbatch.folios[i];
  248. process_one_folio(fs_info, folio, locked_folio,
  249. page_ops, start, end);
  250. }
  251. folio_batch_release(&fbatch);
  252. cond_resched();
  253. }
  254. }
  255. static noinline void unlock_delalloc_folio(const struct inode *inode,
  256. struct folio *locked_folio,
  257. u64 start, u64 end)
  258. {
  259. ASSERT(locked_folio);
  260. __process_folios_contig(inode->i_mapping, locked_folio, start, end,
  261. PAGE_UNLOCK);
  262. }
  263. static noinline int lock_delalloc_folios(struct inode *inode,
  264. struct folio *locked_folio,
  265. u64 start, u64 end)
  266. {
  267. struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
  268. struct address_space *mapping = inode->i_mapping;
  269. pgoff_t index = start >> PAGE_SHIFT;
  270. pgoff_t end_index = end >> PAGE_SHIFT;
  271. u64 processed_end = start;
  272. struct folio_batch fbatch;
  273. folio_batch_init(&fbatch);
  274. while (index <= end_index) {
  275. unsigned int found_folios, i;
  276. found_folios = filemap_get_folios_contig(mapping, &index,
  277. end_index, &fbatch);
  278. if (found_folios == 0)
  279. goto out;
  280. for (i = 0; i < found_folios; i++) {
  281. struct folio *folio = fbatch.folios[i];
  282. u64 range_start;
  283. u32 range_len;
  284. if (folio == locked_folio)
  285. continue;
  286. folio_lock(folio);
  287. if (!folio_test_dirty(folio) || folio->mapping != mapping) {
  288. folio_unlock(folio);
  289. goto out;
  290. }
  291. range_start = max_t(u64, folio_pos(folio), start);
  292. range_len = min_t(u64, folio_next_pos(folio), end + 1) - range_start;
  293. btrfs_folio_set_lock(fs_info, folio, range_start, range_len);
  294. processed_end = range_start + range_len - 1;
  295. }
  296. folio_batch_release(&fbatch);
  297. cond_resched();
  298. }
  299. return 0;
  300. out:
  301. folio_batch_release(&fbatch);
  302. if (processed_end > start)
  303. unlock_delalloc_folio(inode, locked_folio, start, processed_end);
  304. return -EAGAIN;
  305. }
  306. /*
  307. * Find and lock a contiguous range of bytes in the file marked as delalloc, no
  308. * more than @max_bytes.
  309. *
  310. * @start: The original start bytenr to search.
  311. * Will store the extent range start bytenr.
  312. * @end: The original end bytenr of the search range
  313. * Will store the extent range end bytenr.
  314. *
  315. * Return true if we find a delalloc range which starts inside the original
  316. * range, and @start/@end will store the delalloc range start/end.
  317. *
  318. * Return false if we can't find any delalloc range which starts inside the
  319. * original range, and @start/@end will be the non-delalloc range start/end.
  320. */
  321. EXPORT_FOR_TESTS
  322. noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
  323. struct folio *locked_folio,
  324. u64 *start, u64 *end)
  325. {
  326. struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
  327. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  328. const u64 orig_start = *start;
  329. const u64 orig_end = *end;
  330. u64 max_bytes = fs_info->max_extent_size;
  331. u64 delalloc_start;
  332. u64 delalloc_end;
  333. bool found;
  334. struct extent_state *cached_state = NULL;
  335. int ret;
  336. int loops = 0;
  337. /* Caller should pass a valid @end to indicate the search range end */
  338. ASSERT(orig_end > orig_start);
  339. /* The range should at least cover part of the folio */
  340. ASSERT(!(orig_start >= folio_next_pos(locked_folio) ||
  341. orig_end <= folio_pos(locked_folio)));
  342. again:
  343. /* step one, find a bunch of delalloc bytes starting at start */
  344. delalloc_start = *start;
  345. delalloc_end = 0;
  346. /*
  347. * If @max_bytes is smaller than a block, btrfs_find_delalloc_range() can
  348. * return early without handling any dirty ranges.
  349. */
  350. ASSERT(max_bytes >= fs_info->sectorsize);
  351. found = btrfs_find_delalloc_range(tree, &delalloc_start, &delalloc_end,
  352. max_bytes, &cached_state);
  353. if (!found || delalloc_end <= *start || delalloc_start > orig_end) {
  354. *start = delalloc_start;
  355. /* @delalloc_end can be -1, never go beyond @orig_end */
  356. *end = min(delalloc_end, orig_end);
  357. btrfs_free_extent_state(cached_state);
  358. return false;
  359. }
  360. /*
  361. * start comes from the offset of locked_folio. We have to lock
  362. * folios in order, so we can't process delalloc bytes before
  363. * locked_folio
  364. */
  365. if (delalloc_start < *start)
  366. delalloc_start = *start;
  367. /*
  368. * make sure to limit the number of folios we try to lock down
  369. */
  370. if (delalloc_end + 1 - delalloc_start > max_bytes)
  371. delalloc_end = delalloc_start + max_bytes - 1;
  372. /* step two, lock all the folios after the folios that has start */
  373. ret = lock_delalloc_folios(inode, locked_folio, delalloc_start,
  374. delalloc_end);
  375. ASSERT(!ret || ret == -EAGAIN);
  376. if (ret == -EAGAIN) {
  377. /*
  378. * Some of the folios are gone, lets avoid looping by
  379. * shortening the size of the delalloc range we're searching.
  380. */
  381. btrfs_free_extent_state(cached_state);
  382. cached_state = NULL;
  383. if (!loops) {
  384. max_bytes = fs_info->sectorsize;
  385. loops = 1;
  386. goto again;
  387. } else {
  388. return false;
  389. }
  390. }
  391. /* step three, lock the state bits for the whole range */
  392. btrfs_lock_extent(tree, delalloc_start, delalloc_end, &cached_state);
  393. /* then test to make sure it is all still delalloc */
  394. ret = btrfs_test_range_bit(tree, delalloc_start, delalloc_end,
  395. EXTENT_DELALLOC, cached_state);
  396. btrfs_unlock_extent(tree, delalloc_start, delalloc_end, &cached_state);
  397. if (!ret) {
  398. unlock_delalloc_folio(inode, locked_folio, delalloc_start,
  399. delalloc_end);
  400. cond_resched();
  401. goto again;
  402. }
  403. *start = delalloc_start;
  404. *end = delalloc_end;
  405. return found;
  406. }
  407. void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
  408. const struct folio *locked_folio,
  409. struct extent_state **cached,
  410. u32 clear_bits, unsigned long page_ops)
  411. {
  412. btrfs_clear_extent_bit(&inode->io_tree, start, end, clear_bits, cached);
  413. __process_folios_contig(inode->vfs_inode.i_mapping, locked_folio, start,
  414. end, page_ops);
  415. }
  416. static bool btrfs_verify_folio(struct fsverity_info *vi, struct folio *folio,
  417. u64 start, u32 len)
  418. {
  419. struct btrfs_fs_info *fs_info = folio_to_fs_info(folio);
  420. if (!vi || btrfs_folio_test_uptodate(fs_info, folio, start, len))
  421. return true;
  422. return fsverity_verify_folio(vi, folio);
  423. }
  424. static void end_folio_read(struct fsverity_info *vi, struct folio *folio,
  425. bool uptodate, u64 start, u32 len)
  426. {
  427. struct btrfs_fs_info *fs_info = folio_to_fs_info(folio);
  428. ASSERT(folio_pos(folio) <= start &&
  429. start + len <= folio_next_pos(folio));
  430. if (uptodate && btrfs_verify_folio(vi, folio, start, len))
  431. btrfs_folio_set_uptodate(fs_info, folio, start, len);
  432. else
  433. btrfs_folio_clear_uptodate(fs_info, folio, start, len);
  434. if (!btrfs_is_subpage(fs_info, folio))
  435. folio_unlock(folio);
  436. else
  437. btrfs_folio_end_lock(fs_info, folio, start, len);
  438. }
  439. /*
  440. * After a write IO is done, we need to:
  441. *
  442. * - clear the uptodate bits on error
  443. * - clear the writeback bits in the extent tree for the range
  444. * - filio_end_writeback() if there is no more pending io for the folio
  445. *
  446. * Scheduling is not allowed, so the extent state tree is expected
  447. * to have one and only one object corresponding to this IO.
  448. */
  449. static void end_bbio_data_write(struct btrfs_bio *bbio)
  450. {
  451. struct btrfs_fs_info *fs_info = bbio->inode->root->fs_info;
  452. struct bio *bio = &bbio->bio;
  453. int error = blk_status_to_errno(bio->bi_status);
  454. struct folio_iter fi;
  455. const u32 sectorsize = fs_info->sectorsize;
  456. ASSERT(!bio_flagged(bio, BIO_CLONED));
  457. bio_for_each_folio_all(fi, bio) {
  458. struct folio *folio = fi.folio;
  459. u64 start = folio_pos(folio) + fi.offset;
  460. u32 len = fi.length;
  461. /* Our read/write should always be sector aligned. */
  462. if (!IS_ALIGNED(fi.offset, sectorsize))
  463. btrfs_err(fs_info,
  464. "partial page write in btrfs with offset %zu and length %zu",
  465. fi.offset, fi.length);
  466. else if (!IS_ALIGNED(fi.length, sectorsize))
  467. btrfs_info(fs_info,
  468. "incomplete page write with offset %zu and length %zu",
  469. fi.offset, fi.length);
  470. btrfs_finish_ordered_extent(bbio->ordered, folio, start, len,
  471. !error);
  472. if (error)
  473. mapping_set_error(folio->mapping, error);
  474. btrfs_folio_clear_writeback(fs_info, folio, start, len);
  475. }
  476. bio_put(bio);
  477. }
  478. static void begin_folio_read(struct btrfs_fs_info *fs_info, struct folio *folio)
  479. {
  480. ASSERT(folio_test_locked(folio));
  481. if (!btrfs_is_subpage(fs_info, folio))
  482. return;
  483. ASSERT(folio_test_private(folio));
  484. btrfs_folio_set_lock(fs_info, folio, folio_pos(folio), folio_size(folio));
  485. }
  486. /*
  487. * After a data read IO is done, we need to:
  488. *
  489. * - clear the uptodate bits on error
  490. * - set the uptodate bits if things worked
  491. * - set the folio up to date if all extents in the tree are uptodate
  492. * - clear the lock bit in the extent tree
  493. * - unlock the folio if there are no other extents locked for it
  494. *
  495. * Scheduling is not allowed, so the extent state tree is expected
  496. * to have one and only one object corresponding to this IO.
  497. */
  498. static void end_bbio_data_read(struct btrfs_bio *bbio)
  499. {
  500. struct btrfs_fs_info *fs_info = bbio->inode->root->fs_info;
  501. struct inode *inode = &bbio->inode->vfs_inode;
  502. struct bio *bio = &bbio->bio;
  503. struct fsverity_info *vi = NULL;
  504. struct folio_iter fi;
  505. ASSERT(!bio_flagged(bio, BIO_CLONED));
  506. if (bbio->file_offset < i_size_read(inode))
  507. vi = fsverity_get_info(inode);
  508. bio_for_each_folio_all(fi, &bbio->bio) {
  509. bool uptodate = !bio->bi_status;
  510. struct folio *folio = fi.folio;
  511. u64 start = folio_pos(folio) + fi.offset;
  512. btrfs_debug(fs_info,
  513. "%s: bi_sector=%llu, err=%d, mirror=%u",
  514. __func__, bio->bi_iter.bi_sector, bio->bi_status,
  515. bbio->mirror_num);
  516. if (likely(uptodate)) {
  517. u64 end = start + fi.length - 1;
  518. loff_t i_size = i_size_read(inode);
  519. /*
  520. * Zero out the remaining part if this range straddles
  521. * i_size.
  522. *
  523. * Here we should only zero the range inside the folio,
  524. * not touch anything else.
  525. *
  526. * NOTE: i_size is exclusive while end is inclusive and
  527. * folio_contains() takes PAGE_SIZE units.
  528. */
  529. if (folio_contains(folio, i_size >> PAGE_SHIFT) &&
  530. i_size <= end) {
  531. u32 zero_start = max(offset_in_folio(folio, i_size),
  532. offset_in_folio(folio, start));
  533. u32 zero_len = offset_in_folio(folio, end) + 1 -
  534. zero_start;
  535. folio_zero_range(folio, zero_start, zero_len);
  536. }
  537. }
  538. /* Update page status and unlock. */
  539. end_folio_read(vi, folio, uptodate, start, fi.length);
  540. }
  541. bio_put(bio);
  542. }
  543. /*
  544. * Populate every free slot in a provided array with folios using GFP_NOFS.
  545. *
  546. * @nr_folios: number of folios to allocate
  547. * @order: the order of the folios to be allocated
  548. * @folio_array: the array to fill with folios; any existing non-NULL entries in
  549. * the array will be skipped
  550. *
  551. * Return: 0 if all folios were able to be allocated;
  552. * -ENOMEM otherwise, the partially allocated folios would be freed and
  553. * the array slots zeroed
  554. */
  555. int btrfs_alloc_folio_array(unsigned int nr_folios, unsigned int order,
  556. struct folio **folio_array)
  557. {
  558. for (int i = 0; i < nr_folios; i++) {
  559. if (folio_array[i])
  560. continue;
  561. folio_array[i] = folio_alloc(GFP_NOFS, order);
  562. if (!folio_array[i])
  563. goto error;
  564. }
  565. return 0;
  566. error:
  567. for (int i = 0; i < nr_folios; i++) {
  568. if (folio_array[i])
  569. folio_put(folio_array[i]);
  570. folio_array[i] = NULL;
  571. }
  572. return -ENOMEM;
  573. }
  574. /*
  575. * Populate every free slot in a provided array with pages, using GFP_NOFS.
  576. *
  577. * @nr_pages: number of pages to allocate
  578. * @page_array: the array to fill with pages; any existing non-null entries in
  579. * the array will be skipped
  580. * @nofail: whether using __GFP_NOFAIL flag
  581. *
  582. * Return: 0 if all pages were able to be allocated;
  583. * -ENOMEM otherwise, the partially allocated pages would be freed and
  584. * the array slots zeroed
  585. */
  586. int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array,
  587. bool nofail)
  588. {
  589. const gfp_t gfp = nofail ? (GFP_NOFS | __GFP_NOFAIL) : GFP_NOFS;
  590. unsigned int allocated;
  591. for (allocated = 0; allocated < nr_pages;) {
  592. unsigned int last = allocated;
  593. allocated = alloc_pages_bulk(gfp, nr_pages, page_array);
  594. if (unlikely(allocated == last)) {
  595. /* No progress, fail and do cleanup. */
  596. for (int i = 0; i < allocated; i++) {
  597. __free_page(page_array[i]);
  598. page_array[i] = NULL;
  599. }
  600. return -ENOMEM;
  601. }
  602. }
  603. return 0;
  604. }
  605. /*
  606. * Populate needed folios for the extent buffer.
  607. *
  608. * For now, the folios populated are always in order 0 (aka, single page).
  609. */
  610. static int alloc_eb_folio_array(struct extent_buffer *eb, bool nofail)
  611. {
  612. struct page *page_array[INLINE_EXTENT_BUFFER_PAGES] = { 0 };
  613. int num_pages = num_extent_pages(eb);
  614. int ret;
  615. ret = btrfs_alloc_page_array(num_pages, page_array, nofail);
  616. if (ret < 0)
  617. return ret;
  618. for (int i = 0; i < num_pages; i++)
  619. eb->folios[i] = page_folio(page_array[i]);
  620. eb->folio_size = PAGE_SIZE;
  621. eb->folio_shift = PAGE_SHIFT;
  622. return 0;
  623. }
  624. static bool btrfs_bio_is_contig(struct btrfs_bio_ctrl *bio_ctrl,
  625. u64 disk_bytenr, loff_t file_offset)
  626. {
  627. struct bio *bio = &bio_ctrl->bbio->bio;
  628. const sector_t sector = disk_bytenr >> SECTOR_SHIFT;
  629. if (bio_ctrl->compress_type != BTRFS_COMPRESS_NONE) {
  630. /*
  631. * For compression, all IO should have its logical bytenr set
  632. * to the starting bytenr of the compressed extent.
  633. */
  634. return bio->bi_iter.bi_sector == sector;
  635. }
  636. /*
  637. * To merge into a bio both the disk sector and the logical offset in
  638. * the file need to be contiguous.
  639. */
  640. return bio_ctrl->next_file_offset == file_offset &&
  641. bio_end_sector(bio) == sector;
  642. }
  643. static void alloc_new_bio(struct btrfs_inode *inode,
  644. struct btrfs_bio_ctrl *bio_ctrl,
  645. u64 disk_bytenr, u64 file_offset)
  646. {
  647. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  648. struct btrfs_bio *bbio;
  649. bbio = btrfs_bio_alloc(BIO_MAX_VECS, bio_ctrl->opf, inode,
  650. file_offset, bio_ctrl->end_io_func, NULL);
  651. bbio->bio.bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT;
  652. bbio->bio.bi_write_hint = inode->vfs_inode.i_write_hint;
  653. bio_ctrl->bbio = bbio;
  654. bio_ctrl->len_to_oe_boundary = U32_MAX;
  655. bio_ctrl->next_file_offset = file_offset;
  656. /* Limit data write bios to the ordered boundary. */
  657. if (bio_ctrl->wbc) {
  658. struct btrfs_ordered_extent *ordered;
  659. ordered = btrfs_lookup_ordered_extent(inode, file_offset);
  660. if (ordered) {
  661. bio_ctrl->len_to_oe_boundary = min_t(u32, U32_MAX,
  662. ordered->file_offset +
  663. ordered->disk_num_bytes - file_offset);
  664. bbio->ordered = ordered;
  665. }
  666. /*
  667. * Pick the last added device to support cgroup writeback. For
  668. * multi-device file systems this means blk-cgroup policies have
  669. * to always be set on the last added/replaced device.
  670. * This is a bit odd but has been like that for a long time.
  671. */
  672. bio_set_dev(&bbio->bio, fs_info->fs_devices->latest_dev->bdev);
  673. wbc_init_bio(bio_ctrl->wbc, &bbio->bio);
  674. }
  675. }
  676. /*
  677. * @disk_bytenr: logical bytenr where the write will be
  678. * @page: page to add to the bio
  679. * @size: portion of page that we want to write to
  680. * @pg_offset: offset of the new bio or to check whether we are adding
  681. * a contiguous page to the previous one
  682. * @read_em_generation: generation of the extent_map we are submitting
  683. * (only used for read)
  684. *
  685. * The will either add the page into the existing @bio_ctrl->bbio, or allocate a
  686. * new one in @bio_ctrl->bbio.
  687. * The mirror number for this IO should already be initialized in
  688. * @bio_ctrl->mirror_num.
  689. */
  690. static void submit_extent_folio(struct btrfs_bio_ctrl *bio_ctrl,
  691. u64 disk_bytenr, struct folio *folio,
  692. size_t size, unsigned long pg_offset,
  693. u64 read_em_generation)
  694. {
  695. struct btrfs_inode *inode = folio_to_inode(folio);
  696. loff_t file_offset = folio_pos(folio) + pg_offset;
  697. ASSERT(pg_offset + size <= folio_size(folio));
  698. ASSERT(bio_ctrl->end_io_func);
  699. if (bio_ctrl->bbio &&
  700. !btrfs_bio_is_contig(bio_ctrl, disk_bytenr, file_offset))
  701. submit_one_bio(bio_ctrl);
  702. do {
  703. u32 len = size;
  704. /* Allocate new bio if needed */
  705. if (!bio_ctrl->bbio)
  706. alloc_new_bio(inode, bio_ctrl, disk_bytenr, file_offset);
  707. /* Cap to the current ordered extent boundary if there is one. */
  708. if (len > bio_ctrl->len_to_oe_boundary) {
  709. ASSERT(bio_ctrl->compress_type == BTRFS_COMPRESS_NONE);
  710. ASSERT(is_data_inode(inode));
  711. len = bio_ctrl->len_to_oe_boundary;
  712. }
  713. if (!bio_add_folio(&bio_ctrl->bbio->bio, folio, len, pg_offset)) {
  714. /* bio full: move on to a new one */
  715. submit_one_bio(bio_ctrl);
  716. continue;
  717. }
  718. /*
  719. * Now that the folio is definitely added to the bio, include its
  720. * generation in the max generation calculation.
  721. */
  722. bio_ctrl->generation = max(bio_ctrl->generation, read_em_generation);
  723. bio_ctrl->next_file_offset += len;
  724. if (bio_ctrl->wbc)
  725. wbc_account_cgroup_owner(bio_ctrl->wbc, folio, len);
  726. size -= len;
  727. pg_offset += len;
  728. disk_bytenr += len;
  729. file_offset += len;
  730. /*
  731. * len_to_oe_boundary defaults to U32_MAX, which isn't folio or
  732. * sector aligned. alloc_new_bio() then sets it to the end of
  733. * our ordered extent for writes into zoned devices.
  734. *
  735. * When len_to_oe_boundary is tracking an ordered extent, we
  736. * trust the ordered extent code to align things properly, and
  737. * the check above to cap our write to the ordered extent
  738. * boundary is correct.
  739. *
  740. * When len_to_oe_boundary is U32_MAX, the cap above would
  741. * result in a 4095 byte IO for the last folio right before
  742. * we hit the bio limit of UINT_MAX. bio_add_folio() has all
  743. * the checks required to make sure we don't overflow the bio,
  744. * and we should just ignore len_to_oe_boundary completely
  745. * unless we're using it to track an ordered extent.
  746. *
  747. * It's pretty hard to make a bio sized U32_MAX, but it can
  748. * happen when the page cache is able to feed us contiguous
  749. * folios for large extents.
  750. */
  751. if (bio_ctrl->len_to_oe_boundary != U32_MAX)
  752. bio_ctrl->len_to_oe_boundary -= len;
  753. /* Ordered extent boundary: move on to a new bio. */
  754. if (bio_ctrl->len_to_oe_boundary == 0)
  755. submit_one_bio(bio_ctrl);
  756. } while (size);
  757. }
  758. static int attach_extent_buffer_folio(struct extent_buffer *eb,
  759. struct folio *folio,
  760. struct btrfs_folio_state *prealloc)
  761. {
  762. struct btrfs_fs_info *fs_info = eb->fs_info;
  763. int ret = 0;
  764. /*
  765. * If the page is mapped to btree inode, we should hold the private
  766. * lock to prevent race.
  767. * For cloned or dummy extent buffers, their pages are not mapped and
  768. * will not race with any other ebs.
  769. */
  770. if (folio->mapping)
  771. lockdep_assert_held(&folio->mapping->i_private_lock);
  772. if (!btrfs_meta_is_subpage(fs_info)) {
  773. if (!folio_test_private(folio))
  774. folio_attach_private(folio, eb);
  775. else
  776. WARN_ON(folio_get_private(folio) != eb);
  777. return 0;
  778. }
  779. /* Already mapped, just free prealloc */
  780. if (folio_test_private(folio)) {
  781. btrfs_free_folio_state(prealloc);
  782. return 0;
  783. }
  784. if (prealloc)
  785. /* Has preallocated memory for subpage */
  786. folio_attach_private(folio, prealloc);
  787. else
  788. /* Do new allocation to attach subpage */
  789. ret = btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_METADATA);
  790. return ret;
  791. }
  792. int set_folio_extent_mapped(struct folio *folio)
  793. {
  794. struct btrfs_fs_info *fs_info;
  795. ASSERT(folio->mapping);
  796. if (folio_test_private(folio))
  797. return 0;
  798. fs_info = folio_to_fs_info(folio);
  799. if (btrfs_is_subpage(fs_info, folio))
  800. return btrfs_attach_folio_state(fs_info, folio, BTRFS_SUBPAGE_DATA);
  801. folio_attach_private(folio, (void *)EXTENT_FOLIO_PRIVATE);
  802. return 0;
  803. }
  804. void clear_folio_extent_mapped(struct folio *folio)
  805. {
  806. struct btrfs_fs_info *fs_info;
  807. ASSERT(folio->mapping);
  808. if (!folio_test_private(folio))
  809. return;
  810. fs_info = folio_to_fs_info(folio);
  811. if (btrfs_is_subpage(fs_info, folio))
  812. return btrfs_detach_folio_state(fs_info, folio, BTRFS_SUBPAGE_DATA);
  813. folio_detach_private(folio);
  814. }
  815. static struct extent_map *get_extent_map(struct btrfs_inode *inode,
  816. struct folio *folio, u64 start,
  817. u64 len, struct extent_map **em_cached)
  818. {
  819. struct extent_map *em;
  820. ASSERT(em_cached);
  821. if (*em_cached) {
  822. em = *em_cached;
  823. if (btrfs_extent_map_in_tree(em) && start >= em->start &&
  824. start < btrfs_extent_map_end(em)) {
  825. refcount_inc(&em->refs);
  826. return em;
  827. }
  828. btrfs_free_extent_map(em);
  829. *em_cached = NULL;
  830. }
  831. em = btrfs_get_extent(inode, folio, start, len);
  832. if (!IS_ERR(em)) {
  833. BUG_ON(*em_cached);
  834. refcount_inc(&em->refs);
  835. *em_cached = em;
  836. }
  837. return em;
  838. }
  839. static void btrfs_readahead_expand(struct readahead_control *ractl,
  840. const struct extent_map *em)
  841. {
  842. const u64 ra_pos = readahead_pos(ractl);
  843. const u64 ra_end = ra_pos + readahead_length(ractl);
  844. const u64 em_end = btrfs_extent_map_end(em);
  845. /* No expansion for holes and inline extents. */
  846. if (em->disk_bytenr > EXTENT_MAP_LAST_BYTE)
  847. return;
  848. ASSERT(em_end >= ra_pos,
  849. "extent_map %llu %llu ends before current readahead position %llu",
  850. em->start, em->len, ra_pos);
  851. if (em_end > ra_end)
  852. readahead_expand(ractl, ra_pos, em_end - ra_pos);
  853. }
  854. /*
  855. * basic readpage implementation. Locked extent state structs are inserted
  856. * into the tree that are removed when the IO is done (by the end_io
  857. * handlers)
  858. * XXX JDM: This needs looking at to ensure proper page locking
  859. * return 0 on success, otherwise return error
  860. */
  861. static int btrfs_do_readpage(struct folio *folio, struct extent_map **em_cached,
  862. struct btrfs_bio_ctrl *bio_ctrl,
  863. struct fsverity_info *vi)
  864. {
  865. struct inode *inode = folio->mapping->host;
  866. struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
  867. u64 start = folio_pos(folio);
  868. const u64 end = start + folio_size(folio) - 1;
  869. u64 extent_offset;
  870. u64 locked_end;
  871. u64 last_byte = i_size_read(inode);
  872. struct extent_map *em;
  873. int ret = 0;
  874. const size_t blocksize = fs_info->sectorsize;
  875. if (bio_ctrl->ractl)
  876. locked_end = readahead_pos(bio_ctrl->ractl) + readahead_length(bio_ctrl->ractl) - 1;
  877. else
  878. locked_end = end;
  879. ret = set_folio_extent_mapped(folio);
  880. if (ret < 0) {
  881. folio_unlock(folio);
  882. return ret;
  883. }
  884. if (folio_contains(folio, last_byte >> PAGE_SHIFT)) {
  885. size_t zero_offset = offset_in_folio(folio, last_byte);
  886. if (zero_offset)
  887. folio_zero_range(folio, zero_offset,
  888. folio_size(folio) - zero_offset);
  889. }
  890. bio_ctrl->end_io_func = end_bbio_data_read;
  891. begin_folio_read(fs_info, folio);
  892. for (u64 cur = start; cur <= end; cur += blocksize) {
  893. enum btrfs_compression_type compress_type = BTRFS_COMPRESS_NONE;
  894. unsigned long pg_offset = offset_in_folio(folio, cur);
  895. bool force_bio_submit = false;
  896. u64 disk_bytenr;
  897. u64 block_start;
  898. u64 em_gen;
  899. ASSERT(IS_ALIGNED(cur, fs_info->sectorsize));
  900. if (cur >= last_byte) {
  901. folio_zero_range(folio, pg_offset, end - cur + 1);
  902. end_folio_read(vi, folio, true, cur, end - cur + 1);
  903. break;
  904. }
  905. if (btrfs_folio_test_uptodate(fs_info, folio, cur, blocksize)) {
  906. end_folio_read(vi, folio, true, cur, blocksize);
  907. continue;
  908. }
  909. /*
  910. * Search extent map for the whole locked range.
  911. * This will allow btrfs_get_extent() to return a larger hole
  912. * when possible.
  913. * This can reduce duplicated btrfs_get_extent() calls for large
  914. * holes.
  915. */
  916. em = get_extent_map(BTRFS_I(inode), folio, cur, locked_end - cur + 1, em_cached);
  917. if (IS_ERR(em)) {
  918. end_folio_read(vi, folio, false, cur, end + 1 - cur);
  919. return PTR_ERR(em);
  920. }
  921. extent_offset = cur - em->start;
  922. BUG_ON(btrfs_extent_map_end(em) <= cur);
  923. BUG_ON(end < cur);
  924. compress_type = btrfs_extent_map_compression(em);
  925. /*
  926. * Only expand readahead for extents which are already creating
  927. * the pages anyway in add_ra_bio_pages, which is compressed
  928. * extents in the non subpage case.
  929. */
  930. if (bio_ctrl->ractl &&
  931. !btrfs_is_subpage(fs_info, folio) &&
  932. compress_type != BTRFS_COMPRESS_NONE)
  933. btrfs_readahead_expand(bio_ctrl->ractl, em);
  934. if (compress_type != BTRFS_COMPRESS_NONE)
  935. disk_bytenr = em->disk_bytenr;
  936. else
  937. disk_bytenr = btrfs_extent_map_block_start(em) + extent_offset;
  938. if (em->flags & EXTENT_FLAG_PREALLOC)
  939. block_start = EXTENT_MAP_HOLE;
  940. else
  941. block_start = btrfs_extent_map_block_start(em);
  942. /*
  943. * If we have a file range that points to a compressed extent
  944. * and it's followed by a consecutive file range that points
  945. * to the same compressed extent (possibly with a different
  946. * offset and/or length, so it either points to the whole extent
  947. * or only part of it), we must make sure we do not submit a
  948. * single bio to populate the folios for the 2 ranges because
  949. * this makes the compressed extent read zero out the folios
  950. * belonging to the 2nd range. Imagine the following scenario:
  951. *
  952. * File layout
  953. * [0 - 8K] [8K - 24K]
  954. * | |
  955. * | |
  956. * points to extent X, points to extent X,
  957. * offset 4K, length of 8K offset 0, length 16K
  958. *
  959. * [extent X, compressed length = 4K uncompressed length = 16K]
  960. *
  961. * If the bio to read the compressed extent covers both ranges,
  962. * it will decompress extent X into the folios belonging to the
  963. * first range and then it will stop, zeroing out the remaining
  964. * folios that belong to the other range that points to extent X.
  965. * So here we make sure we submit 2 bios, one for the first
  966. * range and another one for the third range. Both will target
  967. * the same physical extent from disk, but we can't currently
  968. * make the compressed bio endio callback populate the folios
  969. * for both ranges because each compressed bio is tightly
  970. * coupled with a single extent map, and each range can have
  971. * an extent map with a different offset value relative to the
  972. * uncompressed data of our extent and different lengths. This
  973. * is a corner case so we prioritize correctness over
  974. * non-optimal behavior (submitting 2 bios for the same extent).
  975. */
  976. if (compress_type != BTRFS_COMPRESS_NONE &&
  977. bio_ctrl->last_em_start != U64_MAX &&
  978. bio_ctrl->last_em_start != em->start)
  979. force_bio_submit = true;
  980. bio_ctrl->last_em_start = em->start;
  981. em_gen = em->generation;
  982. btrfs_free_extent_map(em);
  983. em = NULL;
  984. /* we've found a hole, just zero and go on */
  985. if (block_start == EXTENT_MAP_HOLE) {
  986. folio_zero_range(folio, pg_offset, blocksize);
  987. end_folio_read(vi, folio, true, cur, blocksize);
  988. continue;
  989. }
  990. /* the get_extent function already copied into the folio */
  991. if (block_start == EXTENT_MAP_INLINE) {
  992. end_folio_read(vi, folio, true, cur, blocksize);
  993. continue;
  994. }
  995. if (bio_ctrl->compress_type != compress_type) {
  996. submit_one_bio(bio_ctrl);
  997. bio_ctrl->compress_type = compress_type;
  998. }
  999. if (force_bio_submit)
  1000. submit_one_bio(bio_ctrl);
  1001. submit_extent_folio(bio_ctrl, disk_bytenr, folio, blocksize,
  1002. pg_offset, em_gen);
  1003. }
  1004. return 0;
  1005. }
  1006. /*
  1007. * Check if we can skip waiting the @ordered extent covering the block at @fileoff.
  1008. *
  1009. * @fileoff: Both input and output.
  1010. * Input as the file offset where the check should start at.
  1011. * Output as where the next check should start at,
  1012. * if the function returns true.
  1013. *
  1014. * Return true if we can skip to @fileoff. The caller needs to check the new
  1015. * @fileoff value to make sure it covers the full range, before skipping the
  1016. * full OE.
  1017. *
  1018. * Return false if we must wait for the ordered extent.
  1019. */
  1020. static bool can_skip_one_ordered_range(struct btrfs_inode *inode,
  1021. struct btrfs_ordered_extent *ordered,
  1022. u64 *fileoff)
  1023. {
  1024. const struct btrfs_fs_info *fs_info = inode->root->fs_info;
  1025. struct folio *folio;
  1026. const u32 blocksize = fs_info->sectorsize;
  1027. u64 cur = *fileoff;
  1028. bool ret;
  1029. folio = filemap_get_folio(inode->vfs_inode.i_mapping, cur >> PAGE_SHIFT);
  1030. /*
  1031. * We should have locked the folio(s) for range [start, end], thus
  1032. * there must be a folio and it must be locked.
  1033. */
  1034. ASSERT(!IS_ERR(folio));
  1035. ASSERT(folio_test_locked(folio));
  1036. /*
  1037. * There are several cases for the folio and OE combination:
  1038. *
  1039. * 1) Folio has no private flag
  1040. * The OE has all its IO done but not yet finished, and folio got
  1041. * invalidated.
  1042. *
  1043. * Have we have to wait for the OE to finish, as it may contain the
  1044. * to-be-inserted data checksum.
  1045. * Without the data checksum inserted into the csum tree, read will
  1046. * just fail with missing csum.
  1047. */
  1048. if (!folio_test_private(folio)) {
  1049. ret = false;
  1050. goto out;
  1051. }
  1052. /*
  1053. * 2) The first block is DIRTY.
  1054. *
  1055. * This means the OE is created by some other folios whose file pos is
  1056. * before this one. And since we are holding the folio lock, the writeback
  1057. * of this folio cannot start.
  1058. *
  1059. * We must skip the whole OE, because it will never start until we
  1060. * finished our folio read and unlocked the folio.
  1061. */
  1062. if (btrfs_folio_test_dirty(fs_info, folio, cur, blocksize)) {
  1063. u64 range_len = umin(folio_next_pos(folio),
  1064. ordered->file_offset + ordered->num_bytes) - cur;
  1065. ret = true;
  1066. /*
  1067. * At least inside the folio, all the remaining blocks should
  1068. * also be dirty.
  1069. */
  1070. ASSERT(btrfs_folio_test_dirty(fs_info, folio, cur, range_len));
  1071. *fileoff = ordered->file_offset + ordered->num_bytes;
  1072. goto out;
  1073. }
  1074. /*
  1075. * 3) The first block is uptodate.
  1076. *
  1077. * At least the first block can be skipped, but we are still not fully
  1078. * sure. E.g. if the OE has some other folios in the range that cannot
  1079. * be skipped.
  1080. * So we return true and update @next_ret to the OE/folio boundary.
  1081. */
  1082. if (btrfs_folio_test_uptodate(fs_info, folio, cur, blocksize)) {
  1083. u64 range_len = umin(folio_next_pos(folio),
  1084. ordered->file_offset + ordered->num_bytes) - cur;
  1085. /*
  1086. * The whole range to the OE end or folio boundary should also
  1087. * be uptodate.
  1088. */
  1089. ASSERT(btrfs_folio_test_uptodate(fs_info, folio, cur, range_len));
  1090. ret = true;
  1091. *fileoff = cur + range_len;
  1092. goto out;
  1093. }
  1094. /*
  1095. * 4) The first block is not uptodate.
  1096. *
  1097. * This means the folio is invalidated after the writeback was finished,
  1098. * but by some other operations (e.g. block aligned buffered write) the
  1099. * folio is inserted into filemap.
  1100. * Very much the same as case 1).
  1101. */
  1102. ret = false;
  1103. out:
  1104. folio_put(folio);
  1105. return ret;
  1106. }
  1107. static bool can_skip_ordered_extent(struct btrfs_inode *inode,
  1108. struct btrfs_ordered_extent *ordered,
  1109. u64 start, u64 end)
  1110. {
  1111. const u64 range_end = min(end, ordered->file_offset + ordered->num_bytes - 1);
  1112. u64 cur = max(start, ordered->file_offset);
  1113. while (cur < range_end) {
  1114. bool can_skip;
  1115. can_skip = can_skip_one_ordered_range(inode, ordered, &cur);
  1116. if (!can_skip)
  1117. return false;
  1118. }
  1119. return true;
  1120. }
  1121. /*
  1122. * Locking helper to make sure we get a stable view of extent maps for the
  1123. * involved range.
  1124. *
  1125. * This is for folio read paths (read and readahead), thus the involved range
  1126. * should have all the folios locked.
  1127. */
  1128. static void lock_extents_for_read(struct btrfs_inode *inode, u64 start, u64 end,
  1129. struct extent_state **cached_state)
  1130. {
  1131. u64 cur_pos;
  1132. /* Caller must provide a valid @cached_state. */
  1133. ASSERT(cached_state);
  1134. /* The range must at least be page aligned, as all read paths are folio based. */
  1135. ASSERT(IS_ALIGNED(start, PAGE_SIZE));
  1136. ASSERT(IS_ALIGNED(end + 1, PAGE_SIZE));
  1137. again:
  1138. btrfs_lock_extent(&inode->io_tree, start, end, cached_state);
  1139. cur_pos = start;
  1140. while (cur_pos < end) {
  1141. struct btrfs_ordered_extent *ordered;
  1142. ordered = btrfs_lookup_ordered_range(inode, cur_pos,
  1143. end - cur_pos + 1);
  1144. /*
  1145. * No ordered extents in the range, and we hold the extent lock,
  1146. * no one can modify the extent maps in the range, we're safe to return.
  1147. */
  1148. if (!ordered)
  1149. break;
  1150. /* Check if we can skip waiting for the whole OE. */
  1151. if (can_skip_ordered_extent(inode, ordered, start, end)) {
  1152. cur_pos = min(ordered->file_offset + ordered->num_bytes,
  1153. end + 1);
  1154. btrfs_put_ordered_extent(ordered);
  1155. continue;
  1156. }
  1157. /* Now wait for the OE to finish. */
  1158. btrfs_unlock_extent(&inode->io_tree, start, end, cached_state);
  1159. btrfs_start_ordered_extent_nowriteback(ordered, start, end + 1 - start);
  1160. btrfs_put_ordered_extent(ordered);
  1161. /* We have unlocked the whole range, restart from the beginning. */
  1162. goto again;
  1163. }
  1164. }
  1165. int btrfs_read_folio(struct file *file, struct folio *folio)
  1166. {
  1167. struct inode *vfs_inode = folio->mapping->host;
  1168. struct btrfs_inode *inode = BTRFS_I(vfs_inode);
  1169. const u64 start = folio_pos(folio);
  1170. const u64 end = start + folio_size(folio) - 1;
  1171. struct extent_state *cached_state = NULL;
  1172. struct btrfs_bio_ctrl bio_ctrl = {
  1173. .opf = REQ_OP_READ,
  1174. .last_em_start = U64_MAX,
  1175. };
  1176. struct extent_map *em_cached = NULL;
  1177. struct fsverity_info *vi = NULL;
  1178. int ret;
  1179. lock_extents_for_read(inode, start, end, &cached_state);
  1180. if (folio_pos(folio) < i_size_read(vfs_inode))
  1181. vi = fsverity_get_info(vfs_inode);
  1182. ret = btrfs_do_readpage(folio, &em_cached, &bio_ctrl, vi);
  1183. btrfs_unlock_extent(&inode->io_tree, start, end, &cached_state);
  1184. btrfs_free_extent_map(em_cached);
  1185. /*
  1186. * If btrfs_do_readpage() failed we will want to submit the assembled
  1187. * bio to do the cleanup.
  1188. */
  1189. submit_one_bio(&bio_ctrl);
  1190. return ret;
  1191. }
  1192. static void set_delalloc_bitmap(struct folio *folio, unsigned long *delalloc_bitmap,
  1193. u64 start, u32 len)
  1194. {
  1195. struct btrfs_fs_info *fs_info = folio_to_fs_info(folio);
  1196. const u64 folio_start = folio_pos(folio);
  1197. unsigned int start_bit;
  1198. unsigned int nbits;
  1199. ASSERT(start >= folio_start && start + len <= folio_start + folio_size(folio));
  1200. start_bit = (start - folio_start) >> fs_info->sectorsize_bits;
  1201. nbits = len >> fs_info->sectorsize_bits;
  1202. ASSERT(bitmap_test_range_all_zero(delalloc_bitmap, start_bit, nbits));
  1203. bitmap_set(delalloc_bitmap, start_bit, nbits);
  1204. }
  1205. static bool find_next_delalloc_bitmap(struct folio *folio,
  1206. unsigned long *delalloc_bitmap, u64 start,
  1207. u64 *found_start, u32 *found_len)
  1208. {
  1209. struct btrfs_fs_info *fs_info = folio_to_fs_info(folio);
  1210. const u64 folio_start = folio_pos(folio);
  1211. const unsigned int bitmap_size = btrfs_blocks_per_folio(fs_info, folio);
  1212. unsigned int start_bit;
  1213. unsigned int first_zero;
  1214. unsigned int first_set;
  1215. ASSERT(start >= folio_start && start < folio_start + folio_size(folio));
  1216. start_bit = (start - folio_start) >> fs_info->sectorsize_bits;
  1217. first_set = find_next_bit(delalloc_bitmap, bitmap_size, start_bit);
  1218. if (first_set >= bitmap_size)
  1219. return false;
  1220. *found_start = folio_start + (first_set << fs_info->sectorsize_bits);
  1221. first_zero = find_next_zero_bit(delalloc_bitmap, bitmap_size, first_set);
  1222. *found_len = (first_zero - first_set) << fs_info->sectorsize_bits;
  1223. return true;
  1224. }
  1225. /*
  1226. * Do all of the delayed allocation setup.
  1227. *
  1228. * Return >0 if all the dirty blocks are submitted async (compression) or inlined.
  1229. * The @folio should no longer be touched (treat it as already unlocked).
  1230. *
  1231. * Return 0 if there is still dirty block that needs to be submitted through
  1232. * extent_writepage_io().
  1233. * bio_ctrl->submit_bitmap will indicate which blocks of the folio should be
  1234. * submitted, and @folio is still kept locked.
  1235. *
  1236. * Return <0 if there is any error hit.
  1237. * Any allocated ordered extent range covering this folio will be marked
  1238. * finished (IOERR), and @folio is still kept locked.
  1239. */
  1240. static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode,
  1241. struct folio *folio,
  1242. struct btrfs_bio_ctrl *bio_ctrl)
  1243. {
  1244. struct btrfs_fs_info *fs_info = inode_to_fs_info(&inode->vfs_inode);
  1245. struct writeback_control *wbc = bio_ctrl->wbc;
  1246. const bool is_subpage = btrfs_is_subpage(fs_info, folio);
  1247. const u64 page_start = folio_pos(folio);
  1248. const u64 page_end = page_start + folio_size(folio) - 1;
  1249. const unsigned int blocks_per_folio = btrfs_blocks_per_folio(fs_info, folio);
  1250. unsigned long delalloc_bitmap = 0;
  1251. /*
  1252. * Save the last found delalloc end. As the delalloc end can go beyond
  1253. * page boundary, thus we cannot rely on subpage bitmap to locate the
  1254. * last delalloc end.
  1255. */
  1256. u64 last_delalloc_end = 0;
  1257. /*
  1258. * The range end (exclusive) of the last successfully finished delalloc
  1259. * range.
  1260. * Any range covered by ordered extent must either be manually marked
  1261. * finished (error handling), or has IO submitted (and finish the
  1262. * ordered extent normally).
  1263. *
  1264. * This records the end of ordered extent cleanup if we hit an error.
  1265. */
  1266. u64 last_finished_delalloc_end = page_start;
  1267. u64 delalloc_start = page_start;
  1268. u64 delalloc_end = page_end;
  1269. u64 delalloc_to_write = 0;
  1270. unsigned int start_bit;
  1271. unsigned int end_bit;
  1272. int ret = 0;
  1273. /* Save the dirty bitmap as our submission bitmap will be a subset of it. */
  1274. if (btrfs_is_subpage(fs_info, folio)) {
  1275. ASSERT(blocks_per_folio > 1);
  1276. btrfs_get_subpage_dirty_bitmap(fs_info, folio, &bio_ctrl->submit_bitmap);
  1277. } else {
  1278. bio_ctrl->submit_bitmap = 1;
  1279. }
  1280. for_each_set_bitrange(start_bit, end_bit, &bio_ctrl->submit_bitmap,
  1281. blocks_per_folio) {
  1282. u64 start = page_start + (start_bit << fs_info->sectorsize_bits);
  1283. u32 len = (end_bit - start_bit) << fs_info->sectorsize_bits;
  1284. btrfs_folio_set_lock(fs_info, folio, start, len);
  1285. }
  1286. /* Lock all (subpage) delalloc ranges inside the folio first. */
  1287. while (delalloc_start < page_end) {
  1288. delalloc_end = page_end;
  1289. if (!find_lock_delalloc_range(&inode->vfs_inode, folio,
  1290. &delalloc_start, &delalloc_end)) {
  1291. delalloc_start = delalloc_end + 1;
  1292. continue;
  1293. }
  1294. set_delalloc_bitmap(folio, &delalloc_bitmap, delalloc_start,
  1295. min(delalloc_end, page_end) + 1 - delalloc_start);
  1296. last_delalloc_end = delalloc_end;
  1297. delalloc_start = delalloc_end + 1;
  1298. }
  1299. delalloc_start = page_start;
  1300. if (!last_delalloc_end)
  1301. goto out;
  1302. /* Run the delalloc ranges for the above locked ranges. */
  1303. while (delalloc_start < page_end) {
  1304. u64 found_start;
  1305. u32 found_len;
  1306. bool found;
  1307. if (!is_subpage) {
  1308. /*
  1309. * For non-subpage case, the found delalloc range must
  1310. * cover this folio and there must be only one locked
  1311. * delalloc range.
  1312. */
  1313. found_start = page_start;
  1314. found_len = last_delalloc_end + 1 - found_start;
  1315. found = true;
  1316. } else {
  1317. found = find_next_delalloc_bitmap(folio, &delalloc_bitmap,
  1318. delalloc_start, &found_start, &found_len);
  1319. }
  1320. if (!found)
  1321. break;
  1322. /*
  1323. * The subpage range covers the last sector, the delalloc range may
  1324. * end beyond the folio boundary, use the saved delalloc_end
  1325. * instead.
  1326. */
  1327. if (found_start + found_len >= page_end)
  1328. found_len = last_delalloc_end + 1 - found_start;
  1329. if (ret >= 0) {
  1330. /*
  1331. * Some delalloc range may be created by previous folios.
  1332. * Thus we still need to clean up this range during error
  1333. * handling.
  1334. */
  1335. last_finished_delalloc_end = found_start;
  1336. /* No errors hit so far, run the current delalloc range. */
  1337. ret = btrfs_run_delalloc_range(inode, folio,
  1338. found_start,
  1339. found_start + found_len - 1,
  1340. wbc);
  1341. if (ret >= 0)
  1342. last_finished_delalloc_end = found_start + found_len;
  1343. if (unlikely(ret < 0))
  1344. btrfs_err_rl(fs_info,
  1345. "failed to run delalloc range, root=%lld ino=%llu folio=%llu submit_bitmap=%*pbl start=%llu len=%u: %d",
  1346. btrfs_root_id(inode->root),
  1347. btrfs_ino(inode),
  1348. folio_pos(folio),
  1349. blocks_per_folio,
  1350. &bio_ctrl->submit_bitmap,
  1351. found_start, found_len, ret);
  1352. } else {
  1353. /*
  1354. * We've hit an error during previous delalloc range,
  1355. * have to cleanup the remaining locked ranges.
  1356. */
  1357. btrfs_unlock_extent(&inode->io_tree, found_start,
  1358. found_start + found_len - 1, NULL);
  1359. unlock_delalloc_folio(&inode->vfs_inode, folio,
  1360. found_start,
  1361. found_start + found_len - 1);
  1362. }
  1363. /*
  1364. * We have some ranges that's going to be submitted asynchronously
  1365. * (compression or inline). These range have their own control
  1366. * on when to unlock the pages. We should not touch them
  1367. * anymore, so clear the range from the submission bitmap.
  1368. */
  1369. if (ret > 0) {
  1370. unsigned int start_bit = (found_start - page_start) >>
  1371. fs_info->sectorsize_bits;
  1372. unsigned int end_bit = (min(page_end + 1, found_start + found_len) -
  1373. page_start) >> fs_info->sectorsize_bits;
  1374. bitmap_clear(&bio_ctrl->submit_bitmap, start_bit, end_bit - start_bit);
  1375. }
  1376. /*
  1377. * Above btrfs_run_delalloc_range() may have unlocked the folio,
  1378. * thus for the last range, we cannot touch the folio anymore.
  1379. */
  1380. if (found_start + found_len >= last_delalloc_end + 1)
  1381. break;
  1382. delalloc_start = found_start + found_len;
  1383. }
  1384. /*
  1385. * It's possible we had some ordered extents created before we hit
  1386. * an error, cleanup non-async successfully created delalloc ranges.
  1387. */
  1388. if (unlikely(ret < 0)) {
  1389. unsigned int bitmap_size = min(
  1390. (last_finished_delalloc_end - page_start) >>
  1391. fs_info->sectorsize_bits,
  1392. blocks_per_folio);
  1393. for_each_set_bitrange(start_bit, end_bit, &bio_ctrl->submit_bitmap,
  1394. bitmap_size) {
  1395. u64 start = page_start + (start_bit << fs_info->sectorsize_bits);
  1396. u32 len = (end_bit - start_bit) << fs_info->sectorsize_bits;
  1397. btrfs_mark_ordered_io_finished(inode, folio, start, len, false);
  1398. }
  1399. return ret;
  1400. }
  1401. out:
  1402. if (last_delalloc_end)
  1403. delalloc_end = last_delalloc_end;
  1404. else
  1405. delalloc_end = page_end;
  1406. /*
  1407. * delalloc_end is already one less than the total length, so
  1408. * we don't subtract one from PAGE_SIZE.
  1409. */
  1410. delalloc_to_write +=
  1411. DIV_ROUND_UP(delalloc_end + 1 - page_start, PAGE_SIZE);
  1412. /*
  1413. * If all ranges are submitted asynchronously, we just need to account
  1414. * for them here.
  1415. */
  1416. if (bitmap_empty(&bio_ctrl->submit_bitmap, blocks_per_folio)) {
  1417. wbc->nr_to_write -= delalloc_to_write;
  1418. return 1;
  1419. }
  1420. if (wbc->nr_to_write < delalloc_to_write) {
  1421. int thresh = 8192;
  1422. if (delalloc_to_write < thresh * 2)
  1423. thresh = delalloc_to_write;
  1424. wbc->nr_to_write = min_t(u64, delalloc_to_write,
  1425. thresh);
  1426. }
  1427. return 0;
  1428. }
  1429. /*
  1430. * Return 0 if we have submitted or queued the sector for submission.
  1431. * Return <0 for critical errors, and the involved sector will be cleaned up.
  1432. *
  1433. * Caller should make sure filepos < i_size and handle filepos >= i_size case.
  1434. */
  1435. static int submit_one_sector(struct btrfs_inode *inode,
  1436. struct folio *folio,
  1437. u64 filepos, struct btrfs_bio_ctrl *bio_ctrl,
  1438. loff_t i_size)
  1439. {
  1440. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  1441. struct extent_map *em;
  1442. u64 block_start;
  1443. u64 disk_bytenr;
  1444. u64 extent_offset;
  1445. u64 em_end;
  1446. const u32 sectorsize = fs_info->sectorsize;
  1447. ASSERT(IS_ALIGNED(filepos, sectorsize));
  1448. /* @filepos >= i_size case should be handled by the caller. */
  1449. ASSERT(filepos < i_size);
  1450. em = btrfs_get_extent(inode, NULL, filepos, sectorsize);
  1451. if (IS_ERR(em)) {
  1452. /*
  1453. * bio_ctrl may contain a bio crossing several folios.
  1454. * Submit it immediately so that the bio has a chance
  1455. * to finish normally, other than marked as error.
  1456. */
  1457. submit_one_bio(bio_ctrl);
  1458. /*
  1459. * When submission failed, we should still clear the folio dirty.
  1460. * Or the folio will be written back again but without any
  1461. * ordered extent.
  1462. */
  1463. btrfs_folio_clear_dirty(fs_info, folio, filepos, sectorsize);
  1464. btrfs_folio_set_writeback(fs_info, folio, filepos, sectorsize);
  1465. btrfs_folio_clear_writeback(fs_info, folio, filepos, sectorsize);
  1466. /*
  1467. * Since there is no bio submitted to finish the ordered
  1468. * extent, we have to manually finish this sector.
  1469. */
  1470. btrfs_mark_ordered_io_finished(inode, folio, filepos,
  1471. fs_info->sectorsize, false);
  1472. return PTR_ERR(em);
  1473. }
  1474. extent_offset = filepos - em->start;
  1475. em_end = btrfs_extent_map_end(em);
  1476. ASSERT(filepos <= em_end);
  1477. ASSERT(IS_ALIGNED(em->start, sectorsize));
  1478. ASSERT(IS_ALIGNED(em->len, sectorsize));
  1479. block_start = btrfs_extent_map_block_start(em);
  1480. disk_bytenr = btrfs_extent_map_block_start(em) + extent_offset;
  1481. ASSERT(!btrfs_extent_map_is_compressed(em));
  1482. ASSERT(block_start != EXTENT_MAP_HOLE);
  1483. ASSERT(block_start != EXTENT_MAP_INLINE);
  1484. btrfs_free_extent_map(em);
  1485. em = NULL;
  1486. /*
  1487. * Although the PageDirty bit is cleared before entering this
  1488. * function, subpage dirty bit is not cleared.
  1489. * So clear subpage dirty bit here so next time we won't submit
  1490. * a folio for a range already written to disk.
  1491. */
  1492. btrfs_folio_clear_dirty(fs_info, folio, filepos, sectorsize);
  1493. btrfs_folio_set_writeback(fs_info, folio, filepos, sectorsize);
  1494. /*
  1495. * Above call should set the whole folio with writeback flag, even
  1496. * just for a single subpage sector.
  1497. * As long as the folio is properly locked and the range is correct,
  1498. * we should always get the folio with writeback flag.
  1499. */
  1500. ASSERT(folio_test_writeback(folio));
  1501. submit_extent_folio(bio_ctrl, disk_bytenr, folio,
  1502. sectorsize, filepos - folio_pos(folio), 0);
  1503. return 0;
  1504. }
  1505. /*
  1506. * Helper for extent_writepage(). This calls the writepage start hooks,
  1507. * and does the loop to map the page into extents and bios.
  1508. *
  1509. * We return 1 if the IO is started and the page is unlocked,
  1510. * 0 if all went well (page still locked)
  1511. * < 0 if there were errors (page still locked)
  1512. */
  1513. static noinline_for_stack int extent_writepage_io(struct btrfs_inode *inode,
  1514. struct folio *folio,
  1515. u64 start, u32 len,
  1516. struct btrfs_bio_ctrl *bio_ctrl,
  1517. loff_t i_size)
  1518. {
  1519. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  1520. unsigned long range_bitmap = 0;
  1521. bool submitted_io = false;
  1522. int found_error = 0;
  1523. const u64 end = start + len;
  1524. const u64 folio_start = folio_pos(folio);
  1525. const u64 folio_end = folio_start + folio_size(folio);
  1526. const unsigned int blocks_per_folio = btrfs_blocks_per_folio(fs_info, folio);
  1527. u64 cur;
  1528. int bit;
  1529. int ret = 0;
  1530. ASSERT(start >= folio_start, "start=%llu folio_start=%llu", start, folio_start);
  1531. ASSERT(end <= folio_end, "start=%llu len=%u folio_start=%llu folio_size=%zu",
  1532. start, len, folio_start, folio_size(folio));
  1533. ret = btrfs_writepage_cow_fixup(folio);
  1534. if (ret == -EAGAIN) {
  1535. /* Fixup worker will requeue */
  1536. folio_redirty_for_writepage(bio_ctrl->wbc, folio);
  1537. folio_unlock(folio);
  1538. return 1;
  1539. }
  1540. if (ret < 0) {
  1541. btrfs_folio_clear_dirty(fs_info, folio, start, len);
  1542. btrfs_folio_set_writeback(fs_info, folio, start, len);
  1543. btrfs_folio_clear_writeback(fs_info, folio, start, len);
  1544. return ret;
  1545. }
  1546. bitmap_set(&range_bitmap, (start - folio_pos(folio)) >> fs_info->sectorsize_bits,
  1547. len >> fs_info->sectorsize_bits);
  1548. bitmap_and(&bio_ctrl->submit_bitmap, &bio_ctrl->submit_bitmap, &range_bitmap,
  1549. blocks_per_folio);
  1550. bio_ctrl->end_io_func = end_bbio_data_write;
  1551. for_each_set_bit(bit, &bio_ctrl->submit_bitmap, blocks_per_folio) {
  1552. cur = folio_pos(folio) + (bit << fs_info->sectorsize_bits);
  1553. if (cur >= i_size) {
  1554. struct btrfs_ordered_extent *ordered;
  1555. ordered = btrfs_lookup_first_ordered_range(inode, cur,
  1556. fs_info->sectorsize);
  1557. /*
  1558. * We have just run delalloc before getting here, so
  1559. * there must be an ordered extent.
  1560. */
  1561. ASSERT(ordered != NULL);
  1562. spin_lock(&inode->ordered_tree_lock);
  1563. set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
  1564. ordered->truncated_len = min(ordered->truncated_len,
  1565. cur - ordered->file_offset);
  1566. spin_unlock(&inode->ordered_tree_lock);
  1567. btrfs_put_ordered_extent(ordered);
  1568. btrfs_mark_ordered_io_finished(inode, folio, cur,
  1569. fs_info->sectorsize, true);
  1570. /*
  1571. * This range is beyond i_size, thus we don't need to
  1572. * bother writing back.
  1573. * But we still need to clear the dirty subpage bit, or
  1574. * the next time the folio gets dirtied, we will try to
  1575. * writeback the sectors with subpage dirty bits,
  1576. * causing writeback without ordered extent.
  1577. */
  1578. btrfs_folio_clear_dirty(fs_info, folio, cur, fs_info->sectorsize);
  1579. continue;
  1580. }
  1581. ret = submit_one_sector(inode, folio, cur, bio_ctrl, i_size);
  1582. if (unlikely(ret < 0)) {
  1583. if (!found_error)
  1584. found_error = ret;
  1585. continue;
  1586. }
  1587. submitted_io = true;
  1588. }
  1589. /*
  1590. * If we didn't submitted any sector (>= i_size), folio dirty get
  1591. * cleared but PAGECACHE_TAG_DIRTY is not cleared (only cleared
  1592. * by folio_start_writeback() if the folio is not dirty).
  1593. *
  1594. * Here we set writeback and clear for the range. If the full folio
  1595. * is no longer dirty then we clear the PAGECACHE_TAG_DIRTY tag.
  1596. *
  1597. * If we hit any error, the corresponding sector will have its dirty
  1598. * flag cleared and writeback finished, thus no need to handle the error case.
  1599. */
  1600. if (!submitted_io && !found_error) {
  1601. btrfs_folio_set_writeback(fs_info, folio, start, len);
  1602. btrfs_folio_clear_writeback(fs_info, folio, start, len);
  1603. }
  1604. return found_error;
  1605. }
  1606. /*
  1607. * the writepage semantics are similar to regular writepage. extent
  1608. * records are inserted to lock ranges in the tree, and as dirty areas
  1609. * are found, they are marked writeback. Then the lock bits are removed
  1610. * and the end_io handler clears the writeback ranges
  1611. *
  1612. * Return 0 if everything goes well.
  1613. * Return <0 for error.
  1614. */
  1615. static int extent_writepage(struct folio *folio, struct btrfs_bio_ctrl *bio_ctrl)
  1616. {
  1617. struct btrfs_inode *inode = BTRFS_I(folio->mapping->host);
  1618. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  1619. int ret;
  1620. size_t pg_offset;
  1621. loff_t i_size = i_size_read(&inode->vfs_inode);
  1622. const pgoff_t end_index = i_size >> PAGE_SHIFT;
  1623. const unsigned int blocks_per_folio = btrfs_blocks_per_folio(fs_info, folio);
  1624. trace_extent_writepage(folio, &inode->vfs_inode, bio_ctrl->wbc);
  1625. WARN_ON(!folio_test_locked(folio));
  1626. pg_offset = offset_in_folio(folio, i_size);
  1627. if (folio->index > end_index ||
  1628. (folio->index == end_index && !pg_offset)) {
  1629. folio_invalidate(folio, 0, folio_size(folio));
  1630. folio_unlock(folio);
  1631. return 0;
  1632. }
  1633. if (folio_contains(folio, end_index))
  1634. folio_zero_range(folio, pg_offset, folio_size(folio) - pg_offset);
  1635. /*
  1636. * Default to unlock the whole folio.
  1637. * The proper bitmap can only be initialized until writepage_delalloc().
  1638. */
  1639. bio_ctrl->submit_bitmap = (unsigned long)-1;
  1640. /*
  1641. * If the page is dirty but without private set, it's marked dirty
  1642. * without informing the fs.
  1643. * Nowadays that is a bug, since the introduction of
  1644. * pin_user_pages*().
  1645. *
  1646. * So here we check if the page has private set to rule out such
  1647. * case.
  1648. * But we also have a long history of relying on the COW fixup,
  1649. * so here we only enable this check for experimental builds until
  1650. * we're sure it's safe.
  1651. */
  1652. if (IS_ENABLED(CONFIG_BTRFS_EXPERIMENTAL) &&
  1653. unlikely(!folio_test_private(folio))) {
  1654. WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
  1655. btrfs_err_rl(fs_info,
  1656. "root %lld ino %llu folio %llu is marked dirty without notifying the fs",
  1657. btrfs_root_id(inode->root),
  1658. btrfs_ino(inode), folio_pos(folio));
  1659. ret = -EUCLEAN;
  1660. goto done;
  1661. }
  1662. ret = set_folio_extent_mapped(folio);
  1663. if (ret < 0)
  1664. goto done;
  1665. ret = writepage_delalloc(inode, folio, bio_ctrl);
  1666. if (ret == 1)
  1667. return 0;
  1668. if (ret)
  1669. goto done;
  1670. ret = extent_writepage_io(inode, folio, folio_pos(folio),
  1671. folio_size(folio), bio_ctrl, i_size);
  1672. if (ret == 1)
  1673. return 0;
  1674. if (unlikely(ret < 0))
  1675. btrfs_err_rl(fs_info,
  1676. "failed to submit blocks, root=%lld inode=%llu folio=%llu submit_bitmap=%*pbl: %d",
  1677. btrfs_root_id(inode->root), btrfs_ino(inode),
  1678. folio_pos(folio), blocks_per_folio,
  1679. &bio_ctrl->submit_bitmap, ret);
  1680. bio_ctrl->wbc->nr_to_write--;
  1681. done:
  1682. if (ret < 0)
  1683. mapping_set_error(folio->mapping, ret);
  1684. /*
  1685. * Only unlock ranges that are submitted. As there can be some async
  1686. * submitted ranges inside the folio.
  1687. */
  1688. btrfs_folio_end_lock_bitmap(fs_info, folio, bio_ctrl->submit_bitmap);
  1689. ASSERT(ret <= 0);
  1690. return ret;
  1691. }
  1692. /*
  1693. * Lock extent buffer status and pages for writeback.
  1694. *
  1695. * Return %false if the extent buffer doesn't need to be submitted (e.g. the
  1696. * extent buffer is not dirty)
  1697. * Return %true is the extent buffer is submitted to bio.
  1698. */
  1699. static noinline_for_stack bool lock_extent_buffer_for_io(struct extent_buffer *eb,
  1700. struct writeback_control *wbc)
  1701. {
  1702. struct btrfs_fs_info *fs_info = eb->fs_info;
  1703. bool ret = false;
  1704. btrfs_tree_lock(eb);
  1705. while (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
  1706. btrfs_tree_unlock(eb);
  1707. if (wbc->sync_mode != WB_SYNC_ALL)
  1708. return false;
  1709. wait_on_extent_buffer_writeback(eb);
  1710. btrfs_tree_lock(eb);
  1711. }
  1712. /*
  1713. * We need to do this to prevent races in people who check if the eb is
  1714. * under IO since we can end up having no IO bits set for a short period
  1715. * of time.
  1716. */
  1717. spin_lock(&eb->refs_lock);
  1718. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
  1719. XA_STATE(xas, &fs_info->buffer_tree, eb->start >> fs_info->nodesize_bits);
  1720. unsigned long flags;
  1721. set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  1722. spin_unlock(&eb->refs_lock);
  1723. xas_lock_irqsave(&xas, flags);
  1724. xas_load(&xas);
  1725. xas_set_mark(&xas, PAGECACHE_TAG_WRITEBACK);
  1726. xas_clear_mark(&xas, PAGECACHE_TAG_DIRTY);
  1727. xas_clear_mark(&xas, PAGECACHE_TAG_TOWRITE);
  1728. xas_unlock_irqrestore(&xas, flags);
  1729. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  1730. percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
  1731. -eb->len,
  1732. fs_info->dirty_metadata_batch);
  1733. ret = true;
  1734. } else {
  1735. spin_unlock(&eb->refs_lock);
  1736. }
  1737. btrfs_tree_unlock(eb);
  1738. return ret;
  1739. }
  1740. static void set_btree_ioerr(struct extent_buffer *eb)
  1741. {
  1742. struct btrfs_fs_info *fs_info = eb->fs_info;
  1743. set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
  1744. /*
  1745. * A read may stumble upon this buffer later, make sure that it gets an
  1746. * error and knows there was an error.
  1747. */
  1748. clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  1749. /*
  1750. * We need to set the mapping with the io error as well because a write
  1751. * error will flip the file system readonly, and then syncfs() will
  1752. * return a 0 because we are readonly if we don't modify the err seq for
  1753. * the superblock.
  1754. */
  1755. mapping_set_error(eb->fs_info->btree_inode->i_mapping, -EIO);
  1756. /*
  1757. * If writeback for a btree extent that doesn't belong to a log tree
  1758. * failed, increment the counter transaction->eb_write_errors.
  1759. * We do this because while the transaction is running and before it's
  1760. * committing (when we call filemap_fdata[write|wait]_range against
  1761. * the btree inode), we might have
  1762. * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
  1763. * returns an error or an error happens during writeback, when we're
  1764. * committing the transaction we wouldn't know about it, since the pages
  1765. * can be no longer dirty nor marked anymore for writeback (if a
  1766. * subsequent modification to the extent buffer didn't happen before the
  1767. * transaction commit), which makes filemap_fdata[write|wait]_range not
  1768. * able to find the pages which contain errors at transaction
  1769. * commit time. So if this happens we must abort the transaction,
  1770. * otherwise we commit a super block with btree roots that point to
  1771. * btree nodes/leafs whose content on disk is invalid - either garbage
  1772. * or the content of some node/leaf from a past generation that got
  1773. * cowed or deleted and is no longer valid.
  1774. *
  1775. * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
  1776. * not be enough - we need to distinguish between log tree extents vs
  1777. * non-log tree extents, and the next filemap_fdatawait_range() call
  1778. * will catch and clear such errors in the mapping - and that call might
  1779. * be from a log sync and not from a transaction commit. Also, checking
  1780. * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
  1781. * not done and would not be reliable - the eb might have been released
  1782. * from memory and reading it back again means that flag would not be
  1783. * set (since it's a runtime flag, not persisted on disk).
  1784. *
  1785. * Using the flags below in the btree inode also makes us achieve the
  1786. * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
  1787. * writeback for all dirty pages and before filemap_fdatawait_range()
  1788. * is called, the writeback for all dirty pages had already finished
  1789. * with errors - because we were not using AS_EIO/AS_ENOSPC,
  1790. * filemap_fdatawait_range() would return success, as it could not know
  1791. * that writeback errors happened (the pages were no longer tagged for
  1792. * writeback).
  1793. */
  1794. switch (eb->log_index) {
  1795. case -1:
  1796. set_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags);
  1797. break;
  1798. case 0:
  1799. set_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
  1800. break;
  1801. case 1:
  1802. set_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
  1803. break;
  1804. default:
  1805. BUG(); /* unexpected, logic error */
  1806. }
  1807. }
  1808. static void buffer_tree_set_mark(const struct extent_buffer *eb, xa_mark_t mark)
  1809. {
  1810. struct btrfs_fs_info *fs_info = eb->fs_info;
  1811. XA_STATE(xas, &fs_info->buffer_tree, eb->start >> fs_info->nodesize_bits);
  1812. unsigned long flags;
  1813. xas_lock_irqsave(&xas, flags);
  1814. xas_load(&xas);
  1815. xas_set_mark(&xas, mark);
  1816. xas_unlock_irqrestore(&xas, flags);
  1817. }
  1818. static void buffer_tree_clear_mark(const struct extent_buffer *eb, xa_mark_t mark)
  1819. {
  1820. struct btrfs_fs_info *fs_info = eb->fs_info;
  1821. XA_STATE(xas, &fs_info->buffer_tree, eb->start >> fs_info->nodesize_bits);
  1822. unsigned long flags;
  1823. xas_lock_irqsave(&xas, flags);
  1824. xas_load(&xas);
  1825. xas_clear_mark(&xas, mark);
  1826. xas_unlock_irqrestore(&xas, flags);
  1827. }
  1828. static void buffer_tree_tag_for_writeback(struct btrfs_fs_info *fs_info,
  1829. unsigned long start, unsigned long end)
  1830. {
  1831. XA_STATE(xas, &fs_info->buffer_tree, start);
  1832. unsigned int tagged = 0;
  1833. void *eb;
  1834. xas_lock_irq(&xas);
  1835. xas_for_each_marked(&xas, eb, end, PAGECACHE_TAG_DIRTY) {
  1836. xas_set_mark(&xas, PAGECACHE_TAG_TOWRITE);
  1837. if (++tagged % XA_CHECK_SCHED)
  1838. continue;
  1839. xas_pause(&xas);
  1840. xas_unlock_irq(&xas);
  1841. cond_resched();
  1842. xas_lock_irq(&xas);
  1843. }
  1844. xas_unlock_irq(&xas);
  1845. }
  1846. struct eb_batch {
  1847. unsigned int nr;
  1848. unsigned int cur;
  1849. struct extent_buffer *ebs[PAGEVEC_SIZE];
  1850. };
  1851. static inline bool eb_batch_add(struct eb_batch *batch, struct extent_buffer *eb)
  1852. {
  1853. batch->ebs[batch->nr++] = eb;
  1854. return (batch->nr < PAGEVEC_SIZE);
  1855. }
  1856. static inline void eb_batch_init(struct eb_batch *batch)
  1857. {
  1858. batch->nr = 0;
  1859. batch->cur = 0;
  1860. }
  1861. static inline struct extent_buffer *eb_batch_next(struct eb_batch *batch)
  1862. {
  1863. if (batch->cur >= batch->nr)
  1864. return NULL;
  1865. return batch->ebs[batch->cur++];
  1866. }
  1867. static inline void eb_batch_release(struct eb_batch *batch)
  1868. {
  1869. for (unsigned int i = 0; i < batch->nr; i++)
  1870. free_extent_buffer(batch->ebs[i]);
  1871. eb_batch_init(batch);
  1872. }
  1873. static inline struct extent_buffer *find_get_eb(struct xa_state *xas, unsigned long max,
  1874. xa_mark_t mark)
  1875. {
  1876. struct extent_buffer *eb;
  1877. retry:
  1878. eb = xas_find_marked(xas, max, mark);
  1879. if (xas_retry(xas, eb))
  1880. goto retry;
  1881. if (!eb)
  1882. return NULL;
  1883. if (!refcount_inc_not_zero(&eb->refs)) {
  1884. xas_reset(xas);
  1885. goto retry;
  1886. }
  1887. if (unlikely(eb != xas_reload(xas))) {
  1888. free_extent_buffer(eb);
  1889. xas_reset(xas);
  1890. goto retry;
  1891. }
  1892. return eb;
  1893. }
  1894. static unsigned int buffer_tree_get_ebs_tag(struct btrfs_fs_info *fs_info,
  1895. unsigned long *start,
  1896. unsigned long end, xa_mark_t tag,
  1897. struct eb_batch *batch)
  1898. {
  1899. XA_STATE(xas, &fs_info->buffer_tree, *start);
  1900. struct extent_buffer *eb;
  1901. rcu_read_lock();
  1902. while ((eb = find_get_eb(&xas, end, tag)) != NULL) {
  1903. if (!eb_batch_add(batch, eb)) {
  1904. *start = ((eb->start + eb->len) >> fs_info->nodesize_bits);
  1905. goto out;
  1906. }
  1907. }
  1908. if (end == ULONG_MAX)
  1909. *start = ULONG_MAX;
  1910. else
  1911. *start = end + 1;
  1912. out:
  1913. rcu_read_unlock();
  1914. return batch->nr;
  1915. }
  1916. /*
  1917. * The endio specific version which won't touch any unsafe spinlock in endio
  1918. * context.
  1919. */
  1920. static struct extent_buffer *find_extent_buffer_nolock(
  1921. struct btrfs_fs_info *fs_info, u64 start)
  1922. {
  1923. struct extent_buffer *eb;
  1924. unsigned long index = (start >> fs_info->nodesize_bits);
  1925. rcu_read_lock();
  1926. eb = xa_load(&fs_info->buffer_tree, index);
  1927. if (eb && !refcount_inc_not_zero(&eb->refs))
  1928. eb = NULL;
  1929. rcu_read_unlock();
  1930. return eb;
  1931. }
  1932. static void end_bbio_meta_write(struct btrfs_bio *bbio)
  1933. {
  1934. struct extent_buffer *eb = bbio->private;
  1935. struct folio_iter fi;
  1936. if (bbio->bio.bi_status != BLK_STS_OK)
  1937. set_btree_ioerr(eb);
  1938. bio_for_each_folio_all(fi, &bbio->bio) {
  1939. btrfs_meta_folio_clear_writeback(fi.folio, eb);
  1940. }
  1941. buffer_tree_clear_mark(eb, PAGECACHE_TAG_WRITEBACK);
  1942. clear_and_wake_up_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  1943. bio_put(&bbio->bio);
  1944. }
  1945. static void prepare_eb_write(struct extent_buffer *eb)
  1946. {
  1947. u32 nritems;
  1948. unsigned long start;
  1949. unsigned long end;
  1950. clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
  1951. /* Set btree blocks beyond nritems with 0 to avoid stale content */
  1952. nritems = btrfs_header_nritems(eb);
  1953. if (btrfs_header_level(eb) > 0) {
  1954. end = btrfs_node_key_ptr_offset(eb, nritems);
  1955. memzero_extent_buffer(eb, end, eb->len - end);
  1956. } else {
  1957. /*
  1958. * Leaf:
  1959. * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
  1960. */
  1961. start = btrfs_item_nr_offset(eb, nritems);
  1962. end = btrfs_item_nr_offset(eb, 0);
  1963. if (nritems == 0)
  1964. end += BTRFS_LEAF_DATA_SIZE(eb->fs_info);
  1965. else
  1966. end += btrfs_item_offset(eb, nritems - 1);
  1967. memzero_extent_buffer(eb, start, end - start);
  1968. }
  1969. }
  1970. static noinline_for_stack void write_one_eb(struct extent_buffer *eb,
  1971. struct writeback_control *wbc)
  1972. {
  1973. struct btrfs_fs_info *fs_info = eb->fs_info;
  1974. struct btrfs_bio *bbio;
  1975. prepare_eb_write(eb);
  1976. bbio = btrfs_bio_alloc(INLINE_EXTENT_BUFFER_PAGES,
  1977. REQ_OP_WRITE | REQ_META | wbc_to_write_flags(wbc),
  1978. BTRFS_I(fs_info->btree_inode), eb->start,
  1979. end_bbio_meta_write, eb);
  1980. bbio->bio.bi_iter.bi_sector = eb->start >> SECTOR_SHIFT;
  1981. bio_set_dev(&bbio->bio, fs_info->fs_devices->latest_dev->bdev);
  1982. wbc_init_bio(wbc, &bbio->bio);
  1983. for (int i = 0; i < num_extent_folios(eb); i++) {
  1984. struct folio *folio = eb->folios[i];
  1985. u64 range_start = max_t(u64, eb->start, folio_pos(folio));
  1986. u32 range_len = min_t(u64, folio_next_pos(folio),
  1987. eb->start + eb->len) - range_start;
  1988. folio_lock(folio);
  1989. btrfs_meta_folio_clear_dirty(folio, eb);
  1990. btrfs_meta_folio_set_writeback(folio, eb);
  1991. if (!folio_test_dirty(folio))
  1992. wbc->nr_to_write -= folio_nr_pages(folio);
  1993. bio_add_folio_nofail(&bbio->bio, folio, range_len,
  1994. offset_in_folio(folio, range_start));
  1995. wbc_account_cgroup_owner(wbc, folio, range_len);
  1996. folio_unlock(folio);
  1997. }
  1998. /*
  1999. * If the fs is already in error status, do not submit any writeback
  2000. * but immediately finish it.
  2001. */
  2002. if (unlikely(BTRFS_FS_ERROR(fs_info))) {
  2003. btrfs_bio_end_io(bbio, errno_to_blk_status(BTRFS_FS_ERROR(fs_info)));
  2004. return;
  2005. }
  2006. btrfs_submit_bbio(bbio, 0);
  2007. }
  2008. /*
  2009. * Wait for all eb writeback in the given range to finish.
  2010. *
  2011. * @fs_info: The fs_info for this file system.
  2012. * @start: The offset of the range to start waiting on writeback.
  2013. * @end: The end of the range, inclusive. This is meant to be used in
  2014. * conjunction with wait_marked_extents, so this will usually be
  2015. * the_next_eb->start - 1.
  2016. */
  2017. void btrfs_btree_wait_writeback_range(struct btrfs_fs_info *fs_info, u64 start,
  2018. u64 end)
  2019. {
  2020. struct eb_batch batch;
  2021. unsigned long start_index = (start >> fs_info->nodesize_bits);
  2022. unsigned long end_index = (end >> fs_info->nodesize_bits);
  2023. eb_batch_init(&batch);
  2024. while (start_index <= end_index) {
  2025. struct extent_buffer *eb;
  2026. unsigned int nr_ebs;
  2027. nr_ebs = buffer_tree_get_ebs_tag(fs_info, &start_index, end_index,
  2028. PAGECACHE_TAG_WRITEBACK, &batch);
  2029. if (!nr_ebs)
  2030. break;
  2031. while ((eb = eb_batch_next(&batch)) != NULL)
  2032. wait_on_extent_buffer_writeback(eb);
  2033. eb_batch_release(&batch);
  2034. cond_resched();
  2035. }
  2036. }
  2037. int btree_writepages(struct address_space *mapping, struct writeback_control *wbc)
  2038. {
  2039. struct btrfs_eb_write_context ctx = { .wbc = wbc };
  2040. struct btrfs_fs_info *fs_info = inode_to_fs_info(mapping->host);
  2041. int ret = 0;
  2042. int done = 0;
  2043. int nr_to_write_done = 0;
  2044. struct eb_batch batch;
  2045. unsigned int nr_ebs;
  2046. unsigned long index;
  2047. unsigned long end;
  2048. int scanned = 0;
  2049. xa_mark_t tag;
  2050. eb_batch_init(&batch);
  2051. if (wbc->range_cyclic) {
  2052. index = ((mapping->writeback_index << PAGE_SHIFT) >> fs_info->nodesize_bits);
  2053. end = -1;
  2054. /*
  2055. * Start from the beginning does not need to cycle over the
  2056. * range, mark it as scanned.
  2057. */
  2058. scanned = (index == 0);
  2059. } else {
  2060. index = (wbc->range_start >> fs_info->nodesize_bits);
  2061. end = (wbc->range_end >> fs_info->nodesize_bits);
  2062. scanned = 1;
  2063. }
  2064. if (wbc->sync_mode == WB_SYNC_ALL)
  2065. tag = PAGECACHE_TAG_TOWRITE;
  2066. else
  2067. tag = PAGECACHE_TAG_DIRTY;
  2068. btrfs_zoned_meta_io_lock(fs_info);
  2069. retry:
  2070. if (wbc->sync_mode == WB_SYNC_ALL)
  2071. buffer_tree_tag_for_writeback(fs_info, index, end);
  2072. while (!done && !nr_to_write_done && (index <= end) &&
  2073. (nr_ebs = buffer_tree_get_ebs_tag(fs_info, &index, end, tag, &batch))) {
  2074. struct extent_buffer *eb;
  2075. while ((eb = eb_batch_next(&batch)) != NULL) {
  2076. ctx.eb = eb;
  2077. ret = btrfs_check_meta_write_pointer(eb->fs_info, &ctx);
  2078. if (ret) {
  2079. if (ret == -EBUSY)
  2080. ret = 0;
  2081. if (ret) {
  2082. done = 1;
  2083. break;
  2084. }
  2085. continue;
  2086. }
  2087. if (!lock_extent_buffer_for_io(eb, wbc))
  2088. continue;
  2089. /* Implies write in zoned mode. */
  2090. if (ctx.zoned_bg) {
  2091. /* Mark the last eb in the block group. */
  2092. btrfs_schedule_zone_finish_bg(ctx.zoned_bg, eb);
  2093. ctx.zoned_bg->meta_write_pointer += eb->len;
  2094. }
  2095. write_one_eb(eb, wbc);
  2096. }
  2097. nr_to_write_done = (wbc->nr_to_write <= 0);
  2098. eb_batch_release(&batch);
  2099. cond_resched();
  2100. }
  2101. if (!scanned && !done) {
  2102. /*
  2103. * We hit the last page and there is more work to be done: wrap
  2104. * back to the start of the file
  2105. */
  2106. scanned = 1;
  2107. index = 0;
  2108. goto retry;
  2109. }
  2110. /*
  2111. * If something went wrong, don't allow any metadata write bio to be
  2112. * submitted.
  2113. *
  2114. * This would prevent use-after-free if we had dirty pages not
  2115. * cleaned up, which can still happen by fuzzed images.
  2116. *
  2117. * - Bad extent tree
  2118. * Allowing existing tree block to be allocated for other trees.
  2119. *
  2120. * - Log tree operations
  2121. * Exiting tree blocks get allocated to log tree, bumps its
  2122. * generation, then get cleaned in tree re-balance.
  2123. * Such tree block will not be written back, since it's clean,
  2124. * thus no WRITTEN flag set.
  2125. * And after log writes back, this tree block is not traced by
  2126. * any dirty extent_io_tree.
  2127. *
  2128. * - Offending tree block gets re-dirtied from its original owner
  2129. * Since it has bumped generation, no WRITTEN flag, it can be
  2130. * reused without COWing. This tree block will not be traced
  2131. * by btrfs_transaction::dirty_pages.
  2132. *
  2133. * Now such dirty tree block will not be cleaned by any dirty
  2134. * extent io tree. Thus we don't want to submit such wild eb
  2135. * if the fs already has error.
  2136. *
  2137. * We can get ret > 0 from submit_extent_folio() indicating how many ebs
  2138. * were submitted. Reset it to 0 to avoid false alerts for the caller.
  2139. */
  2140. if (ret > 0)
  2141. ret = 0;
  2142. if (!ret && BTRFS_FS_ERROR(fs_info))
  2143. ret = -EROFS;
  2144. if (ctx.zoned_bg)
  2145. btrfs_put_block_group(ctx.zoned_bg);
  2146. btrfs_zoned_meta_io_unlock(fs_info);
  2147. return ret;
  2148. }
  2149. /*
  2150. * Walk the list of dirty pages of the given address space and write all of them.
  2151. *
  2152. * @mapping: address space structure to write
  2153. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  2154. * @bio_ctrl: holds context for the write, namely the bio
  2155. *
  2156. * If a page is already under I/O, write_cache_pages() skips it, even
  2157. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  2158. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  2159. * and msync() need to guarantee that all the data which was dirty at the time
  2160. * the call was made get new I/O started against them. If wbc->sync_mode is
  2161. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  2162. * existing IO to complete.
  2163. */
  2164. static int extent_write_cache_pages(struct address_space *mapping,
  2165. struct btrfs_bio_ctrl *bio_ctrl)
  2166. {
  2167. struct writeback_control *wbc = bio_ctrl->wbc;
  2168. struct inode *inode = mapping->host;
  2169. int ret = 0;
  2170. int done = 0;
  2171. int nr_to_write_done = 0;
  2172. struct folio_batch fbatch;
  2173. unsigned int nr_folios;
  2174. pgoff_t index;
  2175. pgoff_t end; /* Inclusive */
  2176. pgoff_t done_index;
  2177. int range_whole = 0;
  2178. int scanned = 0;
  2179. xa_mark_t tag;
  2180. /*
  2181. * We have to hold onto the inode so that ordered extents can do their
  2182. * work when the IO finishes. The alternative to this is failing to add
  2183. * an ordered extent if the igrab() fails there and that is a huge pain
  2184. * to deal with, so instead just hold onto the inode throughout the
  2185. * writepages operation. If it fails here we are freeing up the inode
  2186. * anyway and we'd rather not waste our time writing out stuff that is
  2187. * going to be truncated anyway.
  2188. */
  2189. if (!igrab(inode))
  2190. return 0;
  2191. folio_batch_init(&fbatch);
  2192. if (wbc->range_cyclic) {
  2193. index = mapping->writeback_index; /* Start from prev offset */
  2194. end = -1;
  2195. /*
  2196. * Start from the beginning does not need to cycle over the
  2197. * range, mark it as scanned.
  2198. */
  2199. scanned = (index == 0);
  2200. } else {
  2201. index = wbc->range_start >> PAGE_SHIFT;
  2202. end = wbc->range_end >> PAGE_SHIFT;
  2203. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  2204. range_whole = 1;
  2205. scanned = 1;
  2206. }
  2207. /*
  2208. * We do the tagged writepage as long as the snapshot flush bit is set
  2209. * and we are the first one who do the filemap_flush() on this inode.
  2210. *
  2211. * The nr_to_write == LONG_MAX is needed to make sure other flushers do
  2212. * not race in and drop the bit.
  2213. */
  2214. if (range_whole && wbc->nr_to_write == LONG_MAX &&
  2215. test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
  2216. &BTRFS_I(inode)->runtime_flags))
  2217. wbc->tagged_writepages = 1;
  2218. tag = wbc_to_tag(wbc);
  2219. retry:
  2220. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  2221. tag_pages_for_writeback(mapping, index, end);
  2222. done_index = index;
  2223. while (!done && !nr_to_write_done && (index <= end) &&
  2224. (nr_folios = filemap_get_folios_tag(mapping, &index,
  2225. end, tag, &fbatch))) {
  2226. unsigned i;
  2227. for (i = 0; i < nr_folios; i++) {
  2228. struct folio *folio = fbatch.folios[i];
  2229. done_index = folio_next_index(folio);
  2230. /*
  2231. * At this point we hold neither the i_pages lock nor
  2232. * the folio lock: the folio may be truncated or
  2233. * invalidated (changing folio->mapping to NULL).
  2234. */
  2235. if (!folio_trylock(folio)) {
  2236. submit_write_bio(bio_ctrl, 0);
  2237. folio_lock(folio);
  2238. }
  2239. if (unlikely(folio->mapping != mapping)) {
  2240. folio_unlock(folio);
  2241. continue;
  2242. }
  2243. if (!folio_test_dirty(folio)) {
  2244. /* Someone wrote it for us. */
  2245. folio_unlock(folio);
  2246. continue;
  2247. }
  2248. /*
  2249. * For subpage case, compression can lead to mixed
  2250. * writeback and dirty flags, e.g:
  2251. * 0 32K 64K 96K 128K
  2252. * | |//////||/////| |//|
  2253. *
  2254. * In above case, [32K, 96K) is asynchronously submitted
  2255. * for compression, and [124K, 128K) needs to be written back.
  2256. *
  2257. * If we didn't wait writeback for page 64K, [128K, 128K)
  2258. * won't be submitted as the page still has writeback flag
  2259. * and will be skipped in the next check.
  2260. *
  2261. * This mixed writeback and dirty case is only possible for
  2262. * subpage case.
  2263. *
  2264. * TODO: Remove this check after migrating compression to
  2265. * regular submission.
  2266. */
  2267. if (wbc->sync_mode != WB_SYNC_NONE ||
  2268. btrfs_is_subpage(inode_to_fs_info(inode), folio)) {
  2269. if (folio_test_writeback(folio))
  2270. submit_write_bio(bio_ctrl, 0);
  2271. folio_wait_writeback(folio);
  2272. }
  2273. if (folio_test_writeback(folio) ||
  2274. !folio_clear_dirty_for_io(folio)) {
  2275. folio_unlock(folio);
  2276. continue;
  2277. }
  2278. ret = extent_writepage(folio, bio_ctrl);
  2279. if (ret < 0) {
  2280. done = 1;
  2281. break;
  2282. }
  2283. /*
  2284. * The filesystem may choose to bump up nr_to_write.
  2285. * We have to make sure to honor the new nr_to_write
  2286. * at any time.
  2287. */
  2288. nr_to_write_done = (wbc->sync_mode == WB_SYNC_NONE &&
  2289. wbc->nr_to_write <= 0);
  2290. }
  2291. folio_batch_release(&fbatch);
  2292. cond_resched();
  2293. }
  2294. if (!scanned && !done) {
  2295. /*
  2296. * We hit the last page and there is more work to be done: wrap
  2297. * back to the start of the file
  2298. */
  2299. scanned = 1;
  2300. index = 0;
  2301. /*
  2302. * If we're looping we could run into a page that is locked by a
  2303. * writer and that writer could be waiting on writeback for a
  2304. * page in our current bio, and thus deadlock, so flush the
  2305. * write bio here.
  2306. */
  2307. submit_write_bio(bio_ctrl, 0);
  2308. goto retry;
  2309. }
  2310. if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
  2311. mapping->writeback_index = done_index;
  2312. btrfs_add_delayed_iput(BTRFS_I(inode));
  2313. return ret;
  2314. }
  2315. /*
  2316. * Submit the pages in the range to bio for call sites which delalloc range has
  2317. * already been ran (aka, ordered extent inserted) and all pages are still
  2318. * locked.
  2319. */
  2320. void extent_write_locked_range(struct inode *inode, const struct folio *locked_folio,
  2321. u64 start, u64 end, struct writeback_control *wbc,
  2322. bool pages_dirty)
  2323. {
  2324. bool found_error = false;
  2325. int ret = 0;
  2326. struct address_space *mapping = inode->i_mapping;
  2327. struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
  2328. const u32 sectorsize = fs_info->sectorsize;
  2329. loff_t i_size = i_size_read(inode);
  2330. u64 cur = start;
  2331. struct btrfs_bio_ctrl bio_ctrl = {
  2332. .wbc = wbc,
  2333. .opf = REQ_OP_WRITE | wbc_to_write_flags(wbc),
  2334. };
  2335. if (wbc->no_cgroup_owner)
  2336. bio_ctrl.opf |= REQ_BTRFS_CGROUP_PUNT;
  2337. ASSERT(IS_ALIGNED(start, sectorsize) && IS_ALIGNED(end + 1, sectorsize));
  2338. while (cur <= end) {
  2339. u64 cur_end;
  2340. u32 cur_len;
  2341. struct folio *folio;
  2342. folio = filemap_get_folio(mapping, cur >> PAGE_SHIFT);
  2343. /*
  2344. * This shouldn't happen, the pages are pinned and locked, this
  2345. * code is just in case, but shouldn't actually be run.
  2346. */
  2347. if (IS_ERR(folio)) {
  2348. cur_end = min(round_down(cur, PAGE_SIZE) + PAGE_SIZE - 1, end);
  2349. cur_len = cur_end + 1 - cur;
  2350. btrfs_mark_ordered_io_finished(BTRFS_I(inode), NULL,
  2351. cur, cur_len, false);
  2352. mapping_set_error(mapping, PTR_ERR(folio));
  2353. cur = cur_end;
  2354. continue;
  2355. }
  2356. cur_end = min_t(u64, folio_next_pos(folio) - 1, end);
  2357. cur_len = cur_end + 1 - cur;
  2358. ASSERT(folio_test_locked(folio));
  2359. if (pages_dirty && folio != locked_folio)
  2360. ASSERT(folio_test_dirty(folio));
  2361. /*
  2362. * Set the submission bitmap to submit all sectors.
  2363. * extent_writepage_io() will do the truncation correctly.
  2364. */
  2365. bio_ctrl.submit_bitmap = (unsigned long)-1;
  2366. ret = extent_writepage_io(BTRFS_I(inode), folio, cur, cur_len,
  2367. &bio_ctrl, i_size);
  2368. if (ret == 1)
  2369. goto next_page;
  2370. if (ret)
  2371. mapping_set_error(mapping, ret);
  2372. btrfs_folio_end_lock(fs_info, folio, cur, cur_len);
  2373. if (ret < 0)
  2374. found_error = true;
  2375. next_page:
  2376. folio_put(folio);
  2377. cur = cur_end + 1;
  2378. }
  2379. submit_write_bio(&bio_ctrl, found_error ? ret : 0);
  2380. }
  2381. int btrfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
  2382. {
  2383. struct inode *inode = mapping->host;
  2384. int ret = 0;
  2385. struct btrfs_bio_ctrl bio_ctrl = {
  2386. .wbc = wbc,
  2387. .opf = REQ_OP_WRITE | wbc_to_write_flags(wbc),
  2388. };
  2389. /*
  2390. * Allow only a single thread to do the reloc work in zoned mode to
  2391. * protect the write pointer updates.
  2392. */
  2393. btrfs_zoned_data_reloc_lock(BTRFS_I(inode));
  2394. ret = extent_write_cache_pages(mapping, &bio_ctrl);
  2395. submit_write_bio(&bio_ctrl, ret);
  2396. btrfs_zoned_data_reloc_unlock(BTRFS_I(inode));
  2397. return ret;
  2398. }
  2399. void btrfs_readahead(struct readahead_control *rac)
  2400. {
  2401. struct btrfs_bio_ctrl bio_ctrl = {
  2402. .opf = REQ_OP_READ | REQ_RAHEAD,
  2403. .ractl = rac,
  2404. .last_em_start = U64_MAX,
  2405. };
  2406. struct folio *folio;
  2407. struct inode *vfs_inode = rac->mapping->host;
  2408. struct btrfs_inode *inode = BTRFS_I(vfs_inode);
  2409. const u64 start = readahead_pos(rac);
  2410. const u64 end = start + readahead_length(rac) - 1;
  2411. struct extent_state *cached_state = NULL;
  2412. struct extent_map *em_cached = NULL;
  2413. struct fsverity_info *vi = NULL;
  2414. lock_extents_for_read(inode, start, end, &cached_state);
  2415. if (start < i_size_read(vfs_inode))
  2416. vi = fsverity_get_info(vfs_inode);
  2417. while ((folio = readahead_folio(rac)) != NULL)
  2418. btrfs_do_readpage(folio, &em_cached, &bio_ctrl, vi);
  2419. btrfs_unlock_extent(&inode->io_tree, start, end, &cached_state);
  2420. if (em_cached)
  2421. btrfs_free_extent_map(em_cached);
  2422. submit_one_bio(&bio_ctrl);
  2423. }
  2424. /*
  2425. * basic invalidate_folio code, this waits on any locked or writeback
  2426. * ranges corresponding to the folio, and then deletes any extent state
  2427. * records from the tree
  2428. */
  2429. int extent_invalidate_folio(struct extent_io_tree *tree,
  2430. struct folio *folio, size_t offset)
  2431. {
  2432. struct extent_state *cached_state = NULL;
  2433. u64 start = folio_pos(folio);
  2434. u64 end = start + folio_size(folio) - 1;
  2435. size_t blocksize = folio_to_fs_info(folio)->sectorsize;
  2436. /* This function is only called for the btree inode */
  2437. ASSERT(tree->owner == IO_TREE_BTREE_INODE_IO);
  2438. start += ALIGN(offset, blocksize);
  2439. if (start > end)
  2440. return 0;
  2441. btrfs_lock_extent(tree, start, end, &cached_state);
  2442. folio_wait_writeback(folio);
  2443. /*
  2444. * Currently for btree io tree, only EXTENT_LOCKED is utilized,
  2445. * so here we only need to unlock the extent range to free any
  2446. * existing extent state.
  2447. */
  2448. btrfs_unlock_extent(tree, start, end, &cached_state);
  2449. return 0;
  2450. }
  2451. /*
  2452. * A helper for struct address_space_operations::release_folio, this tests for
  2453. * areas of the folio that are locked or under IO and drops the related state
  2454. * bits if it is safe to drop the folio.
  2455. */
  2456. static bool try_release_extent_state(struct extent_io_tree *tree,
  2457. struct folio *folio)
  2458. {
  2459. struct extent_state *cached_state = NULL;
  2460. u64 start = folio_pos(folio);
  2461. u64 end = start + folio_size(folio) - 1;
  2462. u32 range_bits;
  2463. u32 clear_bits;
  2464. bool ret = false;
  2465. int ret2;
  2466. btrfs_get_range_bits(tree, start, end, &range_bits, &cached_state);
  2467. /*
  2468. * We can release the folio if it's locked only for ordered extent
  2469. * completion, since that doesn't require using the folio.
  2470. */
  2471. if ((range_bits & EXTENT_LOCKED) &&
  2472. !(range_bits & EXTENT_FINISHING_ORDERED))
  2473. goto out;
  2474. clear_bits = ~(EXTENT_LOCKED | EXTENT_NODATASUM | EXTENT_DELALLOC_NEW |
  2475. EXTENT_CTLBITS | EXTENT_QGROUP_RESERVED |
  2476. EXTENT_FINISHING_ORDERED);
  2477. /*
  2478. * At this point we can safely clear everything except the locked,
  2479. * nodatasum, delalloc new and finishing ordered bits. The delalloc new
  2480. * bit will be cleared by ordered extent completion.
  2481. */
  2482. ret2 = btrfs_clear_extent_bit(tree, start, end, clear_bits, &cached_state);
  2483. /*
  2484. * If clear_extent_bit failed for enomem reasons, we can't allow the
  2485. * release to continue.
  2486. */
  2487. if (ret2 == 0)
  2488. ret = true;
  2489. out:
  2490. btrfs_free_extent_state(cached_state);
  2491. return ret;
  2492. }
  2493. /*
  2494. * a helper for release_folio. As long as there are no locked extents
  2495. * in the range corresponding to the page, both state records and extent
  2496. * map records are removed
  2497. */
  2498. bool try_release_extent_mapping(struct folio *folio, gfp_t mask)
  2499. {
  2500. u64 start = folio_pos(folio);
  2501. u64 end = start + folio_size(folio) - 1;
  2502. struct btrfs_inode *inode = folio_to_inode(folio);
  2503. struct extent_io_tree *io_tree = &inode->io_tree;
  2504. while (start <= end) {
  2505. const u64 cur_gen = btrfs_get_fs_generation(inode->root->fs_info);
  2506. const u64 len = end - start + 1;
  2507. struct extent_map_tree *extent_tree = &inode->extent_tree;
  2508. struct extent_map *em;
  2509. write_lock(&extent_tree->lock);
  2510. em = btrfs_lookup_extent_mapping(extent_tree, start, len);
  2511. if (!em) {
  2512. write_unlock(&extent_tree->lock);
  2513. break;
  2514. }
  2515. if ((em->flags & EXTENT_FLAG_PINNED) || em->start != start) {
  2516. write_unlock(&extent_tree->lock);
  2517. btrfs_free_extent_map(em);
  2518. break;
  2519. }
  2520. if (btrfs_test_range_bit_exists(io_tree, em->start,
  2521. btrfs_extent_map_end(em) - 1,
  2522. EXTENT_LOCKED))
  2523. goto next;
  2524. /*
  2525. * If it's not in the list of modified extents, used by a fast
  2526. * fsync, we can remove it. If it's being logged we can safely
  2527. * remove it since fsync took an extra reference on the em.
  2528. */
  2529. if (list_empty(&em->list) || (em->flags & EXTENT_FLAG_LOGGING))
  2530. goto remove_em;
  2531. /*
  2532. * If it's in the list of modified extents, remove it only if
  2533. * its generation is older then the current one, in which case
  2534. * we don't need it for a fast fsync. Otherwise don't remove it,
  2535. * we could be racing with an ongoing fast fsync that could miss
  2536. * the new extent.
  2537. */
  2538. if (em->generation >= cur_gen)
  2539. goto next;
  2540. remove_em:
  2541. /*
  2542. * We only remove extent maps that are not in the list of
  2543. * modified extents or that are in the list but with a
  2544. * generation lower then the current generation, so there is no
  2545. * need to set the full fsync flag on the inode (it hurts the
  2546. * fsync performance for workloads with a data size that exceeds
  2547. * or is close to the system's memory).
  2548. */
  2549. btrfs_remove_extent_mapping(inode, em);
  2550. /* Once for the inode's extent map tree. */
  2551. btrfs_free_extent_map(em);
  2552. next:
  2553. start = btrfs_extent_map_end(em);
  2554. write_unlock(&extent_tree->lock);
  2555. /* Once for us, for the lookup_extent_mapping() reference. */
  2556. btrfs_free_extent_map(em);
  2557. if (need_resched()) {
  2558. /*
  2559. * If we need to resched but we can't block just exit
  2560. * and leave any remaining extent maps.
  2561. */
  2562. if (!gfpflags_allow_blocking(mask))
  2563. break;
  2564. cond_resched();
  2565. }
  2566. }
  2567. return try_release_extent_state(io_tree, folio);
  2568. }
  2569. static int extent_buffer_under_io(const struct extent_buffer *eb)
  2570. {
  2571. return (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
  2572. test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  2573. }
  2574. static bool folio_range_has_eb(struct folio *folio)
  2575. {
  2576. struct btrfs_folio_state *bfs;
  2577. lockdep_assert_held(&folio->mapping->i_private_lock);
  2578. if (folio_test_private(folio)) {
  2579. bfs = folio_get_private(folio);
  2580. if (atomic_read(&bfs->eb_refs))
  2581. return true;
  2582. }
  2583. return false;
  2584. }
  2585. static void detach_extent_buffer_folio(const struct extent_buffer *eb, struct folio *folio)
  2586. {
  2587. struct btrfs_fs_info *fs_info = eb->fs_info;
  2588. struct address_space *mapping = folio->mapping;
  2589. const bool mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
  2590. /*
  2591. * For mapped eb, we're going to change the folio private, which should
  2592. * be done under the i_private_lock.
  2593. */
  2594. if (mapped)
  2595. spin_lock(&mapping->i_private_lock);
  2596. if (!folio_test_private(folio)) {
  2597. if (mapped)
  2598. spin_unlock(&mapping->i_private_lock);
  2599. return;
  2600. }
  2601. if (!btrfs_meta_is_subpage(fs_info)) {
  2602. /*
  2603. * We do this since we'll remove the pages after we've removed
  2604. * the eb from the xarray, so we could race and have this page
  2605. * now attached to the new eb. So only clear folio if it's
  2606. * still connected to this eb.
  2607. */
  2608. if (folio_test_private(folio) && folio_get_private(folio) == eb) {
  2609. BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  2610. BUG_ON(folio_test_dirty(folio));
  2611. BUG_ON(folio_test_writeback(folio));
  2612. /* We need to make sure we haven't be attached to a new eb. */
  2613. folio_detach_private(folio);
  2614. }
  2615. if (mapped)
  2616. spin_unlock(&mapping->i_private_lock);
  2617. return;
  2618. }
  2619. /*
  2620. * For subpage, we can have dummy eb with folio private attached. In
  2621. * this case, we can directly detach the private as such folio is only
  2622. * attached to one dummy eb, no sharing.
  2623. */
  2624. if (!mapped) {
  2625. btrfs_detach_folio_state(fs_info, folio, BTRFS_SUBPAGE_METADATA);
  2626. return;
  2627. }
  2628. btrfs_folio_dec_eb_refs(fs_info, folio);
  2629. /*
  2630. * We can only detach the folio private if there are no other ebs in the
  2631. * page range and no unfinished IO.
  2632. */
  2633. if (!folio_range_has_eb(folio))
  2634. btrfs_detach_folio_state(fs_info, folio, BTRFS_SUBPAGE_METADATA);
  2635. spin_unlock(&mapping->i_private_lock);
  2636. }
  2637. /* Release all folios attached to the extent buffer */
  2638. static void btrfs_release_extent_buffer_folios(const struct extent_buffer *eb)
  2639. {
  2640. ASSERT(!extent_buffer_under_io(eb));
  2641. for (int i = 0; i < INLINE_EXTENT_BUFFER_PAGES; i++) {
  2642. struct folio *folio = eb->folios[i];
  2643. if (!folio)
  2644. continue;
  2645. detach_extent_buffer_folio(eb, folio);
  2646. }
  2647. }
  2648. /*
  2649. * Helper for releasing the extent buffer.
  2650. */
  2651. static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
  2652. {
  2653. btrfs_release_extent_buffer_folios(eb);
  2654. btrfs_leak_debug_del_eb(eb);
  2655. kmem_cache_free(extent_buffer_cache, eb);
  2656. }
  2657. static struct extent_buffer *__alloc_extent_buffer(struct btrfs_fs_info *fs_info,
  2658. u64 start)
  2659. {
  2660. struct extent_buffer *eb = NULL;
  2661. eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
  2662. eb->start = start;
  2663. eb->len = fs_info->nodesize;
  2664. eb->fs_info = fs_info;
  2665. init_rwsem(&eb->lock);
  2666. btrfs_leak_debug_add_eb(eb);
  2667. spin_lock_init(&eb->refs_lock);
  2668. refcount_set(&eb->refs, 1);
  2669. ASSERT(eb->len <= BTRFS_MAX_METADATA_BLOCKSIZE);
  2670. return eb;
  2671. }
  2672. /*
  2673. * For use in eb allocation error cleanup paths, as btrfs_release_extent_buffer()
  2674. * does not call folio_put(), and we need to set the folios to NULL so that
  2675. * btrfs_release_extent_buffer() will not detach them a second time.
  2676. */
  2677. static void cleanup_extent_buffer_folios(struct extent_buffer *eb)
  2678. {
  2679. const int num_folios = num_extent_folios(eb);
  2680. /* We cannot use num_extent_folios() as loop bound as eb->folios changes. */
  2681. for (int i = 0; i < num_folios; i++) {
  2682. ASSERT(eb->folios[i]);
  2683. detach_extent_buffer_folio(eb, eb->folios[i]);
  2684. folio_put(eb->folios[i]);
  2685. eb->folios[i] = NULL;
  2686. }
  2687. }
  2688. struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src)
  2689. {
  2690. struct extent_buffer *new;
  2691. int num_folios;
  2692. int ret;
  2693. new = __alloc_extent_buffer(src->fs_info, src->start);
  2694. if (new == NULL)
  2695. return NULL;
  2696. /*
  2697. * Set UNMAPPED before calling btrfs_release_extent_buffer(), as
  2698. * btrfs_release_extent_buffer() have different behavior for
  2699. * UNMAPPED subpage extent buffer.
  2700. */
  2701. set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags);
  2702. ret = alloc_eb_folio_array(new, false);
  2703. if (ret)
  2704. goto release_eb;
  2705. ASSERT(num_extent_folios(src) == num_extent_folios(new),
  2706. "%d != %d", num_extent_folios(src), num_extent_folios(new));
  2707. /* Explicitly use the cached num_extent value from now on. */
  2708. num_folios = num_extent_folios(src);
  2709. for (int i = 0; i < num_folios; i++) {
  2710. struct folio *folio = new->folios[i];
  2711. ret = attach_extent_buffer_folio(new, folio, NULL);
  2712. if (ret < 0)
  2713. goto cleanup_folios;
  2714. WARN_ON(folio_test_dirty(folio));
  2715. }
  2716. for (int i = 0; i < num_folios; i++)
  2717. folio_put(new->folios[i]);
  2718. copy_extent_buffer_full(new, src);
  2719. set_extent_buffer_uptodate(new);
  2720. return new;
  2721. cleanup_folios:
  2722. cleanup_extent_buffer_folios(new);
  2723. release_eb:
  2724. btrfs_release_extent_buffer(new);
  2725. return NULL;
  2726. }
  2727. struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
  2728. u64 start)
  2729. {
  2730. struct extent_buffer *eb;
  2731. int ret;
  2732. eb = __alloc_extent_buffer(fs_info, start);
  2733. if (!eb)
  2734. return NULL;
  2735. ret = alloc_eb_folio_array(eb, false);
  2736. if (ret)
  2737. goto release_eb;
  2738. for (int i = 0; i < num_extent_folios(eb); i++) {
  2739. ret = attach_extent_buffer_folio(eb, eb->folios[i], NULL);
  2740. if (ret < 0)
  2741. goto cleanup_folios;
  2742. }
  2743. for (int i = 0; i < num_extent_folios(eb); i++)
  2744. folio_put(eb->folios[i]);
  2745. set_extent_buffer_uptodate(eb);
  2746. btrfs_set_header_nritems(eb, 0);
  2747. set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
  2748. return eb;
  2749. cleanup_folios:
  2750. cleanup_extent_buffer_folios(eb);
  2751. release_eb:
  2752. btrfs_release_extent_buffer(eb);
  2753. return NULL;
  2754. }
  2755. static void check_buffer_tree_ref(struct extent_buffer *eb)
  2756. {
  2757. int refs;
  2758. /*
  2759. * The TREE_REF bit is first set when the extent_buffer is added to the
  2760. * xarray. It is also reset, if unset, when a new reference is created
  2761. * by find_extent_buffer.
  2762. *
  2763. * It is only cleared in two cases: freeing the last non-tree
  2764. * reference to the extent_buffer when its STALE bit is set or
  2765. * calling release_folio when the tree reference is the only reference.
  2766. *
  2767. * In both cases, care is taken to ensure that the extent_buffer's
  2768. * pages are not under io. However, release_folio can be concurrently
  2769. * called with creating new references, which is prone to race
  2770. * conditions between the calls to check_buffer_tree_ref in those
  2771. * codepaths and clearing TREE_REF in try_release_extent_buffer.
  2772. *
  2773. * The actual lifetime of the extent_buffer in the xarray is adequately
  2774. * protected by the refcount, but the TREE_REF bit and its corresponding
  2775. * reference are not. To protect against this class of races, we call
  2776. * check_buffer_tree_ref() from the code paths which trigger io. Note that
  2777. * once io is initiated, TREE_REF can no longer be cleared, so that is
  2778. * the moment at which any such race is best fixed.
  2779. */
  2780. refs = refcount_read(&eb->refs);
  2781. if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  2782. return;
  2783. spin_lock(&eb->refs_lock);
  2784. if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  2785. refcount_inc(&eb->refs);
  2786. spin_unlock(&eb->refs_lock);
  2787. }
  2788. static void mark_extent_buffer_accessed(struct extent_buffer *eb)
  2789. {
  2790. check_buffer_tree_ref(eb);
  2791. for (int i = 0; i < num_extent_folios(eb); i++)
  2792. folio_mark_accessed(eb->folios[i]);
  2793. }
  2794. struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
  2795. u64 start)
  2796. {
  2797. struct extent_buffer *eb;
  2798. eb = find_extent_buffer_nolock(fs_info, start);
  2799. if (!eb)
  2800. return NULL;
  2801. /*
  2802. * Lock our eb's refs_lock to avoid races with free_extent_buffer().
  2803. * When we get our eb it might be flagged with EXTENT_BUFFER_STALE and
  2804. * another task running free_extent_buffer() might have seen that flag
  2805. * set, eb->refs == 2, that the buffer isn't under IO (dirty and
  2806. * writeback flags not set) and it's still in the tree (flag
  2807. * EXTENT_BUFFER_TREE_REF set), therefore being in the process of
  2808. * decrementing the extent buffer's reference count twice. So here we
  2809. * could race and increment the eb's reference count, clear its stale
  2810. * flag, mark it as dirty and drop our reference before the other task
  2811. * finishes executing free_extent_buffer, which would later result in
  2812. * an attempt to free an extent buffer that is dirty.
  2813. */
  2814. if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
  2815. spin_lock(&eb->refs_lock);
  2816. spin_unlock(&eb->refs_lock);
  2817. }
  2818. mark_extent_buffer_accessed(eb);
  2819. return eb;
  2820. }
  2821. struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
  2822. u64 start)
  2823. {
  2824. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  2825. struct extent_buffer *eb, *exists = NULL;
  2826. int ret;
  2827. eb = find_extent_buffer(fs_info, start);
  2828. if (eb)
  2829. return eb;
  2830. eb = alloc_dummy_extent_buffer(fs_info, start);
  2831. if (!eb)
  2832. return ERR_PTR(-ENOMEM);
  2833. eb->fs_info = fs_info;
  2834. again:
  2835. xa_lock_irq(&fs_info->buffer_tree);
  2836. exists = __xa_cmpxchg(&fs_info->buffer_tree, start >> fs_info->nodesize_bits,
  2837. NULL, eb, GFP_NOFS);
  2838. if (xa_is_err(exists)) {
  2839. ret = xa_err(exists);
  2840. xa_unlock_irq(&fs_info->buffer_tree);
  2841. btrfs_release_extent_buffer(eb);
  2842. return ERR_PTR(ret);
  2843. }
  2844. if (exists) {
  2845. if (!refcount_inc_not_zero(&exists->refs)) {
  2846. /* The extent buffer is being freed, retry. */
  2847. xa_unlock_irq(&fs_info->buffer_tree);
  2848. goto again;
  2849. }
  2850. xa_unlock_irq(&fs_info->buffer_tree);
  2851. btrfs_release_extent_buffer(eb);
  2852. return exists;
  2853. }
  2854. xa_unlock_irq(&fs_info->buffer_tree);
  2855. check_buffer_tree_ref(eb);
  2856. return eb;
  2857. #else
  2858. /* Stub to avoid linker error when compiled with optimizations turned off. */
  2859. return NULL;
  2860. #endif
  2861. }
  2862. static struct extent_buffer *grab_extent_buffer(struct btrfs_fs_info *fs_info,
  2863. struct folio *folio)
  2864. {
  2865. struct extent_buffer *exists;
  2866. lockdep_assert_held(&folio->mapping->i_private_lock);
  2867. /*
  2868. * For subpage case, we completely rely on xarray to ensure we don't try
  2869. * to insert two ebs for the same bytenr. So here we always return NULL
  2870. * and just continue.
  2871. */
  2872. if (btrfs_meta_is_subpage(fs_info))
  2873. return NULL;
  2874. /* Page not yet attached to an extent buffer */
  2875. if (!folio_test_private(folio))
  2876. return NULL;
  2877. /*
  2878. * We could have already allocated an eb for this folio and attached one
  2879. * so lets see if we can get a ref on the existing eb, and if we can we
  2880. * know it's good and we can just return that one, else we know we can
  2881. * just overwrite folio private.
  2882. */
  2883. exists = folio_get_private(folio);
  2884. if (refcount_inc_not_zero(&exists->refs))
  2885. return exists;
  2886. WARN_ON(folio_test_dirty(folio));
  2887. folio_detach_private(folio);
  2888. return NULL;
  2889. }
  2890. /*
  2891. * Validate alignment constraints of eb at logical address @start.
  2892. */
  2893. static bool check_eb_alignment(struct btrfs_fs_info *fs_info, u64 start)
  2894. {
  2895. const u32 nodesize = fs_info->nodesize;
  2896. if (unlikely(!IS_ALIGNED(start, fs_info->sectorsize))) {
  2897. btrfs_err(fs_info, "bad tree block start %llu", start);
  2898. return true;
  2899. }
  2900. if (unlikely(nodesize < PAGE_SIZE && !IS_ALIGNED(start, nodesize))) {
  2901. btrfs_err(fs_info,
  2902. "tree block is not nodesize aligned, start %llu nodesize %u",
  2903. start, nodesize);
  2904. return true;
  2905. }
  2906. if (unlikely(nodesize >= PAGE_SIZE && !PAGE_ALIGNED(start))) {
  2907. btrfs_err(fs_info,
  2908. "tree block is not page aligned, start %llu nodesize %u",
  2909. start, nodesize);
  2910. return true;
  2911. }
  2912. if (unlikely(!IS_ALIGNED(start, nodesize) &&
  2913. !test_and_set_bit(BTRFS_FS_UNALIGNED_TREE_BLOCK, &fs_info->flags))) {
  2914. btrfs_warn(fs_info,
  2915. "tree block not nodesize aligned, start %llu nodesize %u, can be resolved by a full metadata balance",
  2916. start, nodesize);
  2917. }
  2918. return false;
  2919. }
  2920. /*
  2921. * Return 0 if eb->folios[i] is attached to btree inode successfully.
  2922. * Return >0 if there is already another extent buffer for the range,
  2923. * and @found_eb_ret would be updated.
  2924. * Return -EAGAIN if the filemap has an existing folio but with different size
  2925. * than @eb.
  2926. * The caller needs to free the existing folios and retry using the same order.
  2927. */
  2928. static int attach_eb_folio_to_filemap(struct extent_buffer *eb, int i,
  2929. struct btrfs_folio_state *prealloc,
  2930. struct extent_buffer **found_eb_ret)
  2931. {
  2932. struct btrfs_fs_info *fs_info = eb->fs_info;
  2933. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  2934. const pgoff_t index = eb->start >> PAGE_SHIFT;
  2935. struct folio *existing_folio;
  2936. int ret;
  2937. ASSERT(found_eb_ret);
  2938. /* Caller should ensure the folio exists. */
  2939. ASSERT(eb->folios[i]);
  2940. retry:
  2941. existing_folio = NULL;
  2942. ret = filemap_add_folio(mapping, eb->folios[i], index + i,
  2943. GFP_NOFS | __GFP_NOFAIL);
  2944. if (!ret)
  2945. goto finish;
  2946. existing_folio = filemap_lock_folio(mapping, index + i);
  2947. /* The page cache only exists for a very short time, just retry. */
  2948. if (IS_ERR(existing_folio))
  2949. goto retry;
  2950. /* For now, we should only have single-page folios for btree inode. */
  2951. ASSERT(folio_nr_pages(existing_folio) == 1);
  2952. if (folio_size(existing_folio) != eb->folio_size) {
  2953. folio_unlock(existing_folio);
  2954. folio_put(existing_folio);
  2955. return -EAGAIN;
  2956. }
  2957. finish:
  2958. spin_lock(&mapping->i_private_lock);
  2959. if (existing_folio && btrfs_meta_is_subpage(fs_info)) {
  2960. /* We're going to reuse the existing page, can drop our folio now. */
  2961. __free_page(folio_page(eb->folios[i], 0));
  2962. eb->folios[i] = existing_folio;
  2963. } else if (existing_folio) {
  2964. struct extent_buffer *existing_eb;
  2965. existing_eb = grab_extent_buffer(fs_info, existing_folio);
  2966. if (existing_eb) {
  2967. /* The extent buffer still exists, we can use it directly. */
  2968. *found_eb_ret = existing_eb;
  2969. spin_unlock(&mapping->i_private_lock);
  2970. folio_unlock(existing_folio);
  2971. folio_put(existing_folio);
  2972. return 1;
  2973. }
  2974. /* The extent buffer no longer exists, we can reuse the folio. */
  2975. __free_page(folio_page(eb->folios[i], 0));
  2976. eb->folios[i] = existing_folio;
  2977. }
  2978. eb->folio_size = folio_size(eb->folios[i]);
  2979. eb->folio_shift = folio_shift(eb->folios[i]);
  2980. /* Should not fail, as we have preallocated the memory. */
  2981. ret = attach_extent_buffer_folio(eb, eb->folios[i], prealloc);
  2982. ASSERT(!ret);
  2983. /*
  2984. * To inform we have an extra eb under allocation, so that
  2985. * detach_extent_buffer_page() won't release the folio private when the
  2986. * eb hasn't been inserted into the xarray yet.
  2987. *
  2988. * The ref will be decreased when the eb releases the page, in
  2989. * detach_extent_buffer_page(). Thus needs no special handling in the
  2990. * error path.
  2991. */
  2992. btrfs_folio_inc_eb_refs(fs_info, eb->folios[i]);
  2993. spin_unlock(&mapping->i_private_lock);
  2994. return 0;
  2995. }
  2996. struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
  2997. u64 start, u64 owner_root, int level)
  2998. {
  2999. int attached = 0;
  3000. struct extent_buffer *eb;
  3001. struct extent_buffer *existing_eb = NULL;
  3002. struct btrfs_folio_state *prealloc = NULL;
  3003. u64 lockdep_owner = owner_root;
  3004. bool page_contig = true;
  3005. int uptodate = 1;
  3006. int ret;
  3007. if (check_eb_alignment(fs_info, start))
  3008. return ERR_PTR(-EINVAL);
  3009. #if BITS_PER_LONG == 32
  3010. if (start >= MAX_LFS_FILESIZE) {
  3011. btrfs_err_rl(fs_info,
  3012. "extent buffer %llu is beyond 32bit page cache limit", start);
  3013. btrfs_err_32bit_limit(fs_info);
  3014. return ERR_PTR(-EOVERFLOW);
  3015. }
  3016. if (start >= BTRFS_32BIT_EARLY_WARN_THRESHOLD)
  3017. btrfs_warn_32bit_limit(fs_info);
  3018. #endif
  3019. eb = find_extent_buffer(fs_info, start);
  3020. if (eb)
  3021. return eb;
  3022. eb = __alloc_extent_buffer(fs_info, start);
  3023. if (!eb)
  3024. return ERR_PTR(-ENOMEM);
  3025. /*
  3026. * The reloc trees are just snapshots, so we need them to appear to be
  3027. * just like any other fs tree WRT lockdep.
  3028. */
  3029. if (lockdep_owner == BTRFS_TREE_RELOC_OBJECTID)
  3030. lockdep_owner = BTRFS_FS_TREE_OBJECTID;
  3031. btrfs_set_buffer_lockdep_class(lockdep_owner, eb, level);
  3032. /*
  3033. * Preallocate folio private for subpage case, so that we won't
  3034. * allocate memory with i_private_lock nor page lock hold.
  3035. *
  3036. * The memory will be freed by attach_extent_buffer_page() or freed
  3037. * manually if we exit earlier.
  3038. */
  3039. if (btrfs_meta_is_subpage(fs_info)) {
  3040. prealloc = btrfs_alloc_folio_state(fs_info, PAGE_SIZE, BTRFS_SUBPAGE_METADATA);
  3041. if (IS_ERR(prealloc)) {
  3042. ret = PTR_ERR(prealloc);
  3043. goto out;
  3044. }
  3045. }
  3046. reallocate:
  3047. /* Allocate all pages first. */
  3048. ret = alloc_eb_folio_array(eb, true);
  3049. if (ret < 0) {
  3050. btrfs_free_folio_state(prealloc);
  3051. goto out;
  3052. }
  3053. /* Attach all pages to the filemap. */
  3054. for (int i = 0; i < num_extent_folios(eb); i++) {
  3055. struct folio *folio;
  3056. ret = attach_eb_folio_to_filemap(eb, i, prealloc, &existing_eb);
  3057. if (ret > 0) {
  3058. ASSERT(existing_eb);
  3059. goto out;
  3060. }
  3061. /*
  3062. * TODO: Special handling for a corner case where the order of
  3063. * folios mismatch between the new eb and filemap.
  3064. *
  3065. * This happens when:
  3066. *
  3067. * - the new eb is using higher order folio
  3068. *
  3069. * - the filemap is still using 0-order folios for the range
  3070. * This can happen at the previous eb allocation, and we don't
  3071. * have higher order folio for the call.
  3072. *
  3073. * - the existing eb has already been freed
  3074. *
  3075. * In this case, we have to free the existing folios first, and
  3076. * re-allocate using the same order.
  3077. * Thankfully this is not going to happen yet, as we're still
  3078. * using 0-order folios.
  3079. */
  3080. if (unlikely(ret == -EAGAIN)) {
  3081. DEBUG_WARN("folio order mismatch between new eb and filemap");
  3082. goto reallocate;
  3083. }
  3084. attached++;
  3085. /*
  3086. * Only after attach_eb_folio_to_filemap(), eb->folios[] is
  3087. * reliable, as we may choose to reuse the existing page cache
  3088. * and free the allocated page.
  3089. */
  3090. folio = eb->folios[i];
  3091. WARN_ON(btrfs_meta_folio_test_dirty(folio, eb));
  3092. /*
  3093. * Check if the current page is physically contiguous with previous eb
  3094. * page.
  3095. * At this stage, either we allocated a large folio, thus @i
  3096. * would only be 0, or we fall back to per-page allocation.
  3097. */
  3098. if (i && folio_page(eb->folios[i - 1], 0) + 1 != folio_page(folio, 0))
  3099. page_contig = false;
  3100. if (!btrfs_meta_folio_test_uptodate(folio, eb))
  3101. uptodate = 0;
  3102. /*
  3103. * We can't unlock the pages just yet since the extent buffer
  3104. * hasn't been properly inserted into the xarray, this opens a
  3105. * race with btree_release_folio() which can free a page while we
  3106. * are still filling in all pages for the buffer and we could crash.
  3107. */
  3108. }
  3109. if (uptodate)
  3110. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  3111. /* All pages are physically contiguous, can skip cross page handling. */
  3112. if (page_contig)
  3113. eb->addr = folio_address(eb->folios[0]) + offset_in_page(eb->start);
  3114. again:
  3115. xa_lock_irq(&fs_info->buffer_tree);
  3116. existing_eb = __xa_cmpxchg(&fs_info->buffer_tree,
  3117. start >> fs_info->nodesize_bits, NULL, eb,
  3118. GFP_NOFS);
  3119. if (xa_is_err(existing_eb)) {
  3120. ret = xa_err(existing_eb);
  3121. xa_unlock_irq(&fs_info->buffer_tree);
  3122. goto out;
  3123. }
  3124. if (existing_eb) {
  3125. if (!refcount_inc_not_zero(&existing_eb->refs)) {
  3126. xa_unlock_irq(&fs_info->buffer_tree);
  3127. goto again;
  3128. }
  3129. xa_unlock_irq(&fs_info->buffer_tree);
  3130. goto out;
  3131. }
  3132. xa_unlock_irq(&fs_info->buffer_tree);
  3133. /* add one reference for the tree */
  3134. check_buffer_tree_ref(eb);
  3135. /*
  3136. * Now it's safe to unlock the pages because any calls to
  3137. * btree_release_folio will correctly detect that a page belongs to a
  3138. * live buffer and won't free them prematurely.
  3139. */
  3140. for (int i = 0; i < num_extent_folios(eb); i++) {
  3141. folio_unlock(eb->folios[i]);
  3142. /*
  3143. * A folio that has been added to an address_space mapping
  3144. * should not continue holding the refcount from its original
  3145. * allocation indefinitely.
  3146. */
  3147. folio_put(eb->folios[i]);
  3148. }
  3149. return eb;
  3150. out:
  3151. WARN_ON(!refcount_dec_and_test(&eb->refs));
  3152. /*
  3153. * Any attached folios need to be detached before we unlock them. This
  3154. * is because when we're inserting our new folios into the mapping, and
  3155. * then attaching our eb to that folio. If we fail to insert our folio
  3156. * we'll lookup the folio for that index, and grab that EB. We do not
  3157. * want that to grab this eb, as we're getting ready to free it. So we
  3158. * have to detach it first and then unlock it.
  3159. *
  3160. * Note: the bounds is num_extent_pages() as we need to go through all slots.
  3161. */
  3162. for (int i = 0; i < num_extent_pages(eb); i++) {
  3163. struct folio *folio = eb->folios[i];
  3164. if (i < attached) {
  3165. ASSERT(folio);
  3166. detach_extent_buffer_folio(eb, folio);
  3167. folio_unlock(folio);
  3168. } else if (!folio) {
  3169. continue;
  3170. }
  3171. folio_put(folio);
  3172. eb->folios[i] = NULL;
  3173. }
  3174. btrfs_release_extent_buffer(eb);
  3175. if (ret < 0)
  3176. return ERR_PTR(ret);
  3177. ASSERT(existing_eb);
  3178. return existing_eb;
  3179. }
  3180. static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
  3181. {
  3182. struct extent_buffer *eb =
  3183. container_of(head, struct extent_buffer, rcu_head);
  3184. kmem_cache_free(extent_buffer_cache, eb);
  3185. }
  3186. static int release_extent_buffer(struct extent_buffer *eb)
  3187. __releases(&eb->refs_lock)
  3188. {
  3189. lockdep_assert_held(&eb->refs_lock);
  3190. if (refcount_dec_and_test(&eb->refs)) {
  3191. struct btrfs_fs_info *fs_info = eb->fs_info;
  3192. spin_unlock(&eb->refs_lock);
  3193. /*
  3194. * We're erasing, theoretically there will be no allocations, so
  3195. * just use GFP_ATOMIC.
  3196. *
  3197. * We use cmpxchg instead of erase because we do not know if
  3198. * this eb is actually in the tree or not, we could be cleaning
  3199. * up an eb that we allocated but never inserted into the tree.
  3200. * Thus use cmpxchg to remove it from the tree if it is there,
  3201. * or leave the other entry if this isn't in the tree.
  3202. *
  3203. * The documentation says that putting a NULL value is the same
  3204. * as erase as long as XA_FLAGS_ALLOC is not set, which it isn't
  3205. * in this case.
  3206. */
  3207. xa_cmpxchg_irq(&fs_info->buffer_tree,
  3208. eb->start >> fs_info->nodesize_bits, eb, NULL,
  3209. GFP_ATOMIC);
  3210. btrfs_leak_debug_del_eb(eb);
  3211. /* Should be safe to release folios at this point. */
  3212. btrfs_release_extent_buffer_folios(eb);
  3213. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  3214. if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) {
  3215. kmem_cache_free(extent_buffer_cache, eb);
  3216. return 1;
  3217. }
  3218. #endif
  3219. call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
  3220. return 1;
  3221. }
  3222. spin_unlock(&eb->refs_lock);
  3223. return 0;
  3224. }
  3225. void free_extent_buffer(struct extent_buffer *eb)
  3226. {
  3227. int refs;
  3228. if (!eb)
  3229. return;
  3230. refs = refcount_read(&eb->refs);
  3231. while (1) {
  3232. if (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags)) {
  3233. if (refs == 1)
  3234. break;
  3235. } else if (refs <= 3) {
  3236. break;
  3237. }
  3238. /* Optimization to avoid locking eb->refs_lock. */
  3239. if (atomic_try_cmpxchg(&eb->refs.refs, &refs, refs - 1))
  3240. return;
  3241. }
  3242. spin_lock(&eb->refs_lock);
  3243. if (refcount_read(&eb->refs) == 2 &&
  3244. test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
  3245. !extent_buffer_under_io(eb) &&
  3246. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  3247. refcount_dec(&eb->refs);
  3248. /*
  3249. * I know this is terrible, but it's temporary until we stop tracking
  3250. * the uptodate bits and such for the extent buffers.
  3251. */
  3252. release_extent_buffer(eb);
  3253. }
  3254. void free_extent_buffer_stale(struct extent_buffer *eb)
  3255. {
  3256. if (!eb)
  3257. return;
  3258. spin_lock(&eb->refs_lock);
  3259. set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
  3260. if (refcount_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
  3261. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  3262. refcount_dec(&eb->refs);
  3263. release_extent_buffer(eb);
  3264. }
  3265. static void btree_clear_folio_dirty_tag(struct folio *folio)
  3266. {
  3267. ASSERT(!folio_test_dirty(folio));
  3268. ASSERT(folio_test_locked(folio));
  3269. xa_lock_irq(&folio->mapping->i_pages);
  3270. if (!folio_test_dirty(folio))
  3271. __xa_clear_mark(&folio->mapping->i_pages, folio->index,
  3272. PAGECACHE_TAG_DIRTY);
  3273. xa_unlock_irq(&folio->mapping->i_pages);
  3274. }
  3275. void btrfs_clear_buffer_dirty(struct btrfs_trans_handle *trans,
  3276. struct extent_buffer *eb)
  3277. {
  3278. struct btrfs_fs_info *fs_info = eb->fs_info;
  3279. btrfs_assert_tree_write_locked(eb);
  3280. if (trans && btrfs_header_generation(eb) != trans->transid)
  3281. return;
  3282. /*
  3283. * Instead of clearing the dirty flag off of the buffer, mark it as
  3284. * EXTENT_BUFFER_ZONED_ZEROOUT. This allows us to preserve
  3285. * write-ordering in zoned mode, without the need to later re-dirty
  3286. * the extent_buffer.
  3287. *
  3288. * The actual zeroout of the buffer will happen later in
  3289. * btree_csum_one_bio.
  3290. */
  3291. if (btrfs_is_zoned(fs_info) && test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
  3292. set_bit(EXTENT_BUFFER_ZONED_ZEROOUT, &eb->bflags);
  3293. return;
  3294. }
  3295. if (!test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags))
  3296. return;
  3297. buffer_tree_clear_mark(eb, PAGECACHE_TAG_DIRTY);
  3298. percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, -eb->len,
  3299. fs_info->dirty_metadata_batch);
  3300. for (int i = 0; i < num_extent_folios(eb); i++) {
  3301. struct folio *folio = eb->folios[i];
  3302. bool last;
  3303. if (!folio_test_dirty(folio))
  3304. continue;
  3305. folio_lock(folio);
  3306. last = btrfs_meta_folio_clear_and_test_dirty(folio, eb);
  3307. if (last)
  3308. btree_clear_folio_dirty_tag(folio);
  3309. folio_unlock(folio);
  3310. }
  3311. WARN_ON(refcount_read(&eb->refs) == 0);
  3312. }
  3313. void set_extent_buffer_dirty(struct extent_buffer *eb)
  3314. {
  3315. bool was_dirty;
  3316. check_buffer_tree_ref(eb);
  3317. was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
  3318. WARN_ON(refcount_read(&eb->refs) == 0);
  3319. WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
  3320. WARN_ON(test_bit(EXTENT_BUFFER_ZONED_ZEROOUT, &eb->bflags));
  3321. if (!was_dirty) {
  3322. bool subpage = btrfs_meta_is_subpage(eb->fs_info);
  3323. /*
  3324. * For subpage case, we can have other extent buffers in the
  3325. * same page, and in clear_extent_buffer_dirty() we
  3326. * have to clear page dirty without subpage lock held.
  3327. * This can cause race where our page gets dirty cleared after
  3328. * we just set it.
  3329. *
  3330. * Thankfully, clear_extent_buffer_dirty() has locked
  3331. * its page for other reasons, we can use page lock to prevent
  3332. * the above race.
  3333. */
  3334. if (subpage)
  3335. folio_lock(eb->folios[0]);
  3336. for (int i = 0; i < num_extent_folios(eb); i++)
  3337. btrfs_meta_folio_set_dirty(eb->folios[i], eb);
  3338. buffer_tree_set_mark(eb, PAGECACHE_TAG_DIRTY);
  3339. if (subpage)
  3340. folio_unlock(eb->folios[0]);
  3341. percpu_counter_add_batch(&eb->fs_info->dirty_metadata_bytes,
  3342. eb->len,
  3343. eb->fs_info->dirty_metadata_batch);
  3344. }
  3345. #ifdef CONFIG_BTRFS_DEBUG
  3346. for (int i = 0; i < num_extent_folios(eb); i++)
  3347. ASSERT(folio_test_dirty(eb->folios[i]));
  3348. #endif
  3349. }
  3350. void clear_extent_buffer_uptodate(struct extent_buffer *eb)
  3351. {
  3352. clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  3353. for (int i = 0; i < num_extent_folios(eb); i++) {
  3354. struct folio *folio = eb->folios[i];
  3355. if (!folio)
  3356. continue;
  3357. btrfs_meta_folio_clear_uptodate(folio, eb);
  3358. }
  3359. }
  3360. void set_extent_buffer_uptodate(struct extent_buffer *eb)
  3361. {
  3362. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  3363. for (int i = 0; i < num_extent_folios(eb); i++)
  3364. btrfs_meta_folio_set_uptodate(eb->folios[i], eb);
  3365. }
  3366. static void clear_extent_buffer_reading(struct extent_buffer *eb)
  3367. {
  3368. clear_and_wake_up_bit(EXTENT_BUFFER_READING, &eb->bflags);
  3369. }
  3370. static void end_bbio_meta_read(struct btrfs_bio *bbio)
  3371. {
  3372. struct extent_buffer *eb = bbio->private;
  3373. bool uptodate = !bbio->bio.bi_status;
  3374. /*
  3375. * If the extent buffer is marked UPTODATE before the read operation
  3376. * completes, other calls to read_extent_buffer_pages() will return
  3377. * early without waiting for the read to finish, causing data races.
  3378. */
  3379. WARN_ON(test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags));
  3380. eb->read_mirror = bbio->mirror_num;
  3381. if (uptodate &&
  3382. btrfs_validate_extent_buffer(eb, &bbio->parent_check) < 0)
  3383. uptodate = false;
  3384. if (uptodate)
  3385. set_extent_buffer_uptodate(eb);
  3386. else
  3387. clear_extent_buffer_uptodate(eb);
  3388. clear_extent_buffer_reading(eb);
  3389. free_extent_buffer(eb);
  3390. bio_put(&bbio->bio);
  3391. }
  3392. int read_extent_buffer_pages_nowait(struct extent_buffer *eb, int mirror_num,
  3393. const struct btrfs_tree_parent_check *check)
  3394. {
  3395. struct btrfs_fs_info *fs_info = eb->fs_info;
  3396. struct btrfs_bio *bbio;
  3397. if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
  3398. return 0;
  3399. /*
  3400. * We could have had EXTENT_BUFFER_UPTODATE cleared by the write
  3401. * operation, which could potentially still be in flight. In this case
  3402. * we simply want to return an error.
  3403. */
  3404. if (unlikely(test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)))
  3405. return -EIO;
  3406. /* Someone else is already reading the buffer, just wait for it. */
  3407. if (test_and_set_bit(EXTENT_BUFFER_READING, &eb->bflags))
  3408. return 0;
  3409. /*
  3410. * Between the initial test_bit(EXTENT_BUFFER_UPTODATE) and the above
  3411. * test_and_set_bit(EXTENT_BUFFER_READING), someone else could have
  3412. * started and finished reading the same eb. In this case, UPTODATE
  3413. * will now be set, and we shouldn't read it in again.
  3414. */
  3415. if (unlikely(test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))) {
  3416. clear_extent_buffer_reading(eb);
  3417. return 0;
  3418. }
  3419. eb->read_mirror = 0;
  3420. check_buffer_tree_ref(eb);
  3421. refcount_inc(&eb->refs);
  3422. bbio = btrfs_bio_alloc(INLINE_EXTENT_BUFFER_PAGES,
  3423. REQ_OP_READ | REQ_META, BTRFS_I(fs_info->btree_inode),
  3424. eb->start, end_bbio_meta_read, eb);
  3425. bbio->bio.bi_iter.bi_sector = eb->start >> SECTOR_SHIFT;
  3426. memcpy(&bbio->parent_check, check, sizeof(*check));
  3427. for (int i = 0; i < num_extent_folios(eb); i++) {
  3428. struct folio *folio = eb->folios[i];
  3429. u64 range_start = max_t(u64, eb->start, folio_pos(folio));
  3430. u32 range_len = min_t(u64, folio_next_pos(folio),
  3431. eb->start + eb->len) - range_start;
  3432. bio_add_folio_nofail(&bbio->bio, folio, range_len,
  3433. offset_in_folio(folio, range_start));
  3434. }
  3435. btrfs_submit_bbio(bbio, mirror_num);
  3436. return 0;
  3437. }
  3438. int read_extent_buffer_pages(struct extent_buffer *eb, int mirror_num,
  3439. const struct btrfs_tree_parent_check *check)
  3440. {
  3441. int ret;
  3442. ret = read_extent_buffer_pages_nowait(eb, mirror_num, check);
  3443. if (ret < 0)
  3444. return ret;
  3445. wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_READING, TASK_UNINTERRUPTIBLE);
  3446. if (unlikely(!test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)))
  3447. return -EIO;
  3448. return 0;
  3449. }
  3450. static bool report_eb_range(const struct extent_buffer *eb, unsigned long start,
  3451. unsigned long len)
  3452. {
  3453. btrfs_warn(eb->fs_info,
  3454. "access to eb bytenr %llu len %u out of range start %lu len %lu",
  3455. eb->start, eb->len, start, len);
  3456. DEBUG_WARN();
  3457. return true;
  3458. }
  3459. /*
  3460. * Check if the [start, start + len) range is valid before reading/writing
  3461. * the eb.
  3462. * NOTE: @start and @len are offset inside the eb, not logical address.
  3463. *
  3464. * Caller should not touch the dst/src memory if this function returns error.
  3465. */
  3466. static inline int check_eb_range(const struct extent_buffer *eb,
  3467. unsigned long start, unsigned long len)
  3468. {
  3469. unsigned long offset;
  3470. /* start, start + len should not go beyond eb->len nor overflow */
  3471. if (unlikely(check_add_overflow(start, len, &offset) || offset > eb->len))
  3472. return report_eb_range(eb, start, len);
  3473. return false;
  3474. }
  3475. void read_extent_buffer(const struct extent_buffer *eb, void *dstv,
  3476. unsigned long start, unsigned long len)
  3477. {
  3478. const int unit_size = eb->folio_size;
  3479. size_t cur;
  3480. size_t offset;
  3481. char *dst = (char *)dstv;
  3482. unsigned long i = get_eb_folio_index(eb, start);
  3483. if (check_eb_range(eb, start, len)) {
  3484. /*
  3485. * Invalid range hit, reset the memory, so callers won't get
  3486. * some random garbage for their uninitialized memory.
  3487. */
  3488. memset(dstv, 0, len);
  3489. return;
  3490. }
  3491. if (eb->addr) {
  3492. memcpy(dstv, eb->addr + start, len);
  3493. return;
  3494. }
  3495. offset = get_eb_offset_in_folio(eb, start);
  3496. while (len > 0) {
  3497. char *kaddr;
  3498. cur = min(len, unit_size - offset);
  3499. kaddr = folio_address(eb->folios[i]);
  3500. memcpy(dst, kaddr + offset, cur);
  3501. dst += cur;
  3502. len -= cur;
  3503. offset = 0;
  3504. i++;
  3505. }
  3506. }
  3507. int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb,
  3508. void __user *dstv,
  3509. unsigned long start, unsigned long len)
  3510. {
  3511. const int unit_size = eb->folio_size;
  3512. size_t cur;
  3513. size_t offset;
  3514. char __user *dst = (char __user *)dstv;
  3515. unsigned long i = get_eb_folio_index(eb, start);
  3516. int ret = 0;
  3517. WARN_ON(start > eb->len);
  3518. WARN_ON(start + len > eb->start + eb->len);
  3519. if (eb->addr) {
  3520. if (copy_to_user_nofault(dstv, eb->addr + start, len))
  3521. ret = -EFAULT;
  3522. return ret;
  3523. }
  3524. offset = get_eb_offset_in_folio(eb, start);
  3525. while (len > 0) {
  3526. char *kaddr;
  3527. cur = min(len, unit_size - offset);
  3528. kaddr = folio_address(eb->folios[i]);
  3529. if (copy_to_user_nofault(dst, kaddr + offset, cur)) {
  3530. ret = -EFAULT;
  3531. break;
  3532. }
  3533. dst += cur;
  3534. len -= cur;
  3535. offset = 0;
  3536. i++;
  3537. }
  3538. return ret;
  3539. }
  3540. int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
  3541. unsigned long start, unsigned long len)
  3542. {
  3543. const int unit_size = eb->folio_size;
  3544. size_t cur;
  3545. size_t offset;
  3546. char *kaddr;
  3547. char *ptr = (char *)ptrv;
  3548. unsigned long i = get_eb_folio_index(eb, start);
  3549. int ret = 0;
  3550. if (check_eb_range(eb, start, len))
  3551. return -EINVAL;
  3552. if (eb->addr)
  3553. return memcmp(ptrv, eb->addr + start, len);
  3554. offset = get_eb_offset_in_folio(eb, start);
  3555. while (len > 0) {
  3556. cur = min(len, unit_size - offset);
  3557. kaddr = folio_address(eb->folios[i]);
  3558. ret = memcmp(ptr, kaddr + offset, cur);
  3559. if (ret)
  3560. break;
  3561. ptr += cur;
  3562. len -= cur;
  3563. offset = 0;
  3564. i++;
  3565. }
  3566. return ret;
  3567. }
  3568. /*
  3569. * Check that the extent buffer is uptodate.
  3570. *
  3571. * For regular sector size == PAGE_SIZE case, check if @page is uptodate.
  3572. * For subpage case, check if the range covered by the eb has EXTENT_UPTODATE.
  3573. */
  3574. static void assert_eb_folio_uptodate(const struct extent_buffer *eb, int i)
  3575. {
  3576. struct btrfs_fs_info *fs_info = eb->fs_info;
  3577. struct folio *folio = eb->folios[i];
  3578. ASSERT(folio);
  3579. /*
  3580. * If we are using the commit root we could potentially clear a page
  3581. * Uptodate while we're using the extent buffer that we've previously
  3582. * looked up. We don't want to complain in this case, as the page was
  3583. * valid before, we just didn't write it out. Instead we want to catch
  3584. * the case where we didn't actually read the block properly, which
  3585. * would have !PageUptodate and !EXTENT_BUFFER_WRITE_ERR.
  3586. */
  3587. if (test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
  3588. return;
  3589. if (btrfs_meta_is_subpage(fs_info)) {
  3590. folio = eb->folios[0];
  3591. ASSERT(i == 0);
  3592. if (WARN_ON(!btrfs_subpage_test_uptodate(fs_info, folio,
  3593. eb->start, eb->len)))
  3594. btrfs_subpage_dump_bitmap(fs_info, folio, eb->start, eb->len);
  3595. } else {
  3596. WARN_ON(!folio_test_uptodate(folio));
  3597. }
  3598. }
  3599. static void __write_extent_buffer(const struct extent_buffer *eb,
  3600. const void *srcv, unsigned long start,
  3601. unsigned long len, bool use_memmove)
  3602. {
  3603. const int unit_size = eb->folio_size;
  3604. size_t cur;
  3605. size_t offset;
  3606. char *kaddr;
  3607. const char *src = (const char *)srcv;
  3608. unsigned long i = get_eb_folio_index(eb, start);
  3609. /* For unmapped (dummy) ebs, no need to check their uptodate status. */
  3610. const bool check_uptodate = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
  3611. if (check_eb_range(eb, start, len))
  3612. return;
  3613. if (eb->addr) {
  3614. if (use_memmove)
  3615. memmove(eb->addr + start, srcv, len);
  3616. else
  3617. memcpy(eb->addr + start, srcv, len);
  3618. return;
  3619. }
  3620. offset = get_eb_offset_in_folio(eb, start);
  3621. while (len > 0) {
  3622. if (check_uptodate)
  3623. assert_eb_folio_uptodate(eb, i);
  3624. cur = min(len, unit_size - offset);
  3625. kaddr = folio_address(eb->folios[i]);
  3626. if (use_memmove)
  3627. memmove(kaddr + offset, src, cur);
  3628. else
  3629. memcpy(kaddr + offset, src, cur);
  3630. src += cur;
  3631. len -= cur;
  3632. offset = 0;
  3633. i++;
  3634. }
  3635. }
  3636. void write_extent_buffer(const struct extent_buffer *eb, const void *srcv,
  3637. unsigned long start, unsigned long len)
  3638. {
  3639. return __write_extent_buffer(eb, srcv, start, len, false);
  3640. }
  3641. static void memset_extent_buffer(const struct extent_buffer *eb, int c,
  3642. unsigned long start, unsigned long len)
  3643. {
  3644. const int unit_size = eb->folio_size;
  3645. unsigned long cur = start;
  3646. if (eb->addr) {
  3647. memset(eb->addr + start, c, len);
  3648. return;
  3649. }
  3650. while (cur < start + len) {
  3651. unsigned long index = get_eb_folio_index(eb, cur);
  3652. unsigned int offset = get_eb_offset_in_folio(eb, cur);
  3653. unsigned int cur_len = min(start + len - cur, unit_size - offset);
  3654. assert_eb_folio_uptodate(eb, index);
  3655. memset(folio_address(eb->folios[index]) + offset, c, cur_len);
  3656. cur += cur_len;
  3657. }
  3658. }
  3659. void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start,
  3660. unsigned long len)
  3661. {
  3662. if (check_eb_range(eb, start, len))
  3663. return;
  3664. return memset_extent_buffer(eb, 0, start, len);
  3665. }
  3666. void copy_extent_buffer_full(const struct extent_buffer *dst,
  3667. const struct extent_buffer *src)
  3668. {
  3669. const int unit_size = src->folio_size;
  3670. unsigned long cur = 0;
  3671. ASSERT(dst->len == src->len);
  3672. while (cur < src->len) {
  3673. unsigned long index = get_eb_folio_index(src, cur);
  3674. unsigned long offset = get_eb_offset_in_folio(src, cur);
  3675. unsigned long cur_len = min(src->len, unit_size - offset);
  3676. void *addr = folio_address(src->folios[index]) + offset;
  3677. write_extent_buffer(dst, addr, cur, cur_len);
  3678. cur += cur_len;
  3679. }
  3680. }
  3681. void copy_extent_buffer(const struct extent_buffer *dst,
  3682. const struct extent_buffer *src,
  3683. unsigned long dst_offset, unsigned long src_offset,
  3684. unsigned long len)
  3685. {
  3686. const int unit_size = dst->folio_size;
  3687. u64 dst_len = dst->len;
  3688. size_t cur;
  3689. size_t offset;
  3690. char *kaddr;
  3691. unsigned long i = get_eb_folio_index(dst, dst_offset);
  3692. if (check_eb_range(dst, dst_offset, len) ||
  3693. check_eb_range(src, src_offset, len))
  3694. return;
  3695. WARN_ON(src->len != dst_len);
  3696. offset = get_eb_offset_in_folio(dst, dst_offset);
  3697. while (len > 0) {
  3698. assert_eb_folio_uptodate(dst, i);
  3699. cur = min(len, (unsigned long)(unit_size - offset));
  3700. kaddr = folio_address(dst->folios[i]);
  3701. read_extent_buffer(src, kaddr + offset, src_offset, cur);
  3702. src_offset += cur;
  3703. len -= cur;
  3704. offset = 0;
  3705. i++;
  3706. }
  3707. }
  3708. /*
  3709. * Calculate the folio and offset of the byte containing the given bit number.
  3710. *
  3711. * @eb: the extent buffer
  3712. * @start: offset of the bitmap item in the extent buffer
  3713. * @nr: bit number
  3714. * @folio_index: return index of the folio in the extent buffer that contains
  3715. * the given bit number
  3716. * @folio_offset: return offset into the folio given by folio_index
  3717. *
  3718. * This helper hides the ugliness of finding the byte in an extent buffer which
  3719. * contains a given bit.
  3720. */
  3721. static inline void eb_bitmap_offset(const struct extent_buffer *eb,
  3722. unsigned long start, unsigned long nr,
  3723. unsigned long *folio_index,
  3724. size_t *folio_offset)
  3725. {
  3726. size_t byte_offset = BIT_BYTE(nr);
  3727. size_t offset;
  3728. /*
  3729. * The byte we want is the offset of the extent buffer + the offset of
  3730. * the bitmap item in the extent buffer + the offset of the byte in the
  3731. * bitmap item.
  3732. */
  3733. offset = start + offset_in_eb_folio(eb, eb->start) + byte_offset;
  3734. *folio_index = offset >> eb->folio_shift;
  3735. *folio_offset = offset_in_eb_folio(eb, offset);
  3736. }
  3737. /*
  3738. * Determine whether a bit in a bitmap item is set.
  3739. *
  3740. * @eb: the extent buffer
  3741. * @start: offset of the bitmap item in the extent buffer
  3742. * @nr: bit number to test
  3743. */
  3744. bool extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start,
  3745. unsigned long nr)
  3746. {
  3747. unsigned long i;
  3748. size_t offset;
  3749. u8 *kaddr;
  3750. eb_bitmap_offset(eb, start, nr, &i, &offset);
  3751. assert_eb_folio_uptodate(eb, i);
  3752. kaddr = folio_address(eb->folios[i]);
  3753. return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
  3754. }
  3755. static u8 *extent_buffer_get_byte(const struct extent_buffer *eb, unsigned long bytenr)
  3756. {
  3757. unsigned long index = get_eb_folio_index(eb, bytenr);
  3758. if (check_eb_range(eb, bytenr, 1))
  3759. return NULL;
  3760. return folio_address(eb->folios[index]) + get_eb_offset_in_folio(eb, bytenr);
  3761. }
  3762. /*
  3763. * Set an area of a bitmap to 1.
  3764. *
  3765. * @eb: the extent buffer
  3766. * @start: offset of the bitmap item in the extent buffer
  3767. * @pos: bit number of the first bit
  3768. * @len: number of bits to set
  3769. */
  3770. void extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start,
  3771. unsigned long pos, unsigned long len)
  3772. {
  3773. unsigned int first_byte = start + BIT_BYTE(pos);
  3774. unsigned int last_byte = start + BIT_BYTE(pos + len - 1);
  3775. const bool same_byte = (first_byte == last_byte);
  3776. u8 mask = BITMAP_FIRST_BYTE_MASK(pos);
  3777. u8 *kaddr;
  3778. if (same_byte)
  3779. mask &= BITMAP_LAST_BYTE_MASK(pos + len);
  3780. /* Handle the first byte. */
  3781. kaddr = extent_buffer_get_byte(eb, first_byte);
  3782. *kaddr |= mask;
  3783. if (same_byte)
  3784. return;
  3785. /* Handle the byte aligned part. */
  3786. ASSERT(first_byte + 1 <= last_byte);
  3787. memset_extent_buffer(eb, 0xff, first_byte + 1, last_byte - first_byte - 1);
  3788. /* Handle the last byte. */
  3789. kaddr = extent_buffer_get_byte(eb, last_byte);
  3790. *kaddr |= BITMAP_LAST_BYTE_MASK(pos + len);
  3791. }
  3792. /*
  3793. * Clear an area of a bitmap.
  3794. *
  3795. * @eb: the extent buffer
  3796. * @start: offset of the bitmap item in the extent buffer
  3797. * @pos: bit number of the first bit
  3798. * @len: number of bits to clear
  3799. */
  3800. void extent_buffer_bitmap_clear(const struct extent_buffer *eb,
  3801. unsigned long start, unsigned long pos,
  3802. unsigned long len)
  3803. {
  3804. unsigned int first_byte = start + BIT_BYTE(pos);
  3805. unsigned int last_byte = start + BIT_BYTE(pos + len - 1);
  3806. const bool same_byte = (first_byte == last_byte);
  3807. u8 mask = BITMAP_FIRST_BYTE_MASK(pos);
  3808. u8 *kaddr;
  3809. if (same_byte)
  3810. mask &= BITMAP_LAST_BYTE_MASK(pos + len);
  3811. /* Handle the first byte. */
  3812. kaddr = extent_buffer_get_byte(eb, first_byte);
  3813. *kaddr &= ~mask;
  3814. if (same_byte)
  3815. return;
  3816. /* Handle the byte aligned part. */
  3817. ASSERT(first_byte + 1 <= last_byte);
  3818. memset_extent_buffer(eb, 0, first_byte + 1, last_byte - first_byte - 1);
  3819. /* Handle the last byte. */
  3820. kaddr = extent_buffer_get_byte(eb, last_byte);
  3821. *kaddr &= ~BITMAP_LAST_BYTE_MASK(pos + len);
  3822. }
  3823. static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
  3824. {
  3825. unsigned long distance = (src > dst) ? src - dst : dst - src;
  3826. return distance < len;
  3827. }
  3828. void memcpy_extent_buffer(const struct extent_buffer *dst,
  3829. unsigned long dst_offset, unsigned long src_offset,
  3830. unsigned long len)
  3831. {
  3832. const int unit_size = dst->folio_size;
  3833. unsigned long cur_off = 0;
  3834. if (check_eb_range(dst, dst_offset, len) ||
  3835. check_eb_range(dst, src_offset, len))
  3836. return;
  3837. if (dst->addr) {
  3838. const bool use_memmove = areas_overlap(src_offset, dst_offset, len);
  3839. if (use_memmove)
  3840. memmove(dst->addr + dst_offset, dst->addr + src_offset, len);
  3841. else
  3842. memcpy(dst->addr + dst_offset, dst->addr + src_offset, len);
  3843. return;
  3844. }
  3845. while (cur_off < len) {
  3846. unsigned long cur_src = cur_off + src_offset;
  3847. unsigned long folio_index = get_eb_folio_index(dst, cur_src);
  3848. unsigned long folio_off = get_eb_offset_in_folio(dst, cur_src);
  3849. unsigned long cur_len = min(src_offset + len - cur_src,
  3850. unit_size - folio_off);
  3851. void *src_addr = folio_address(dst->folios[folio_index]) + folio_off;
  3852. const bool use_memmove = areas_overlap(src_offset + cur_off,
  3853. dst_offset + cur_off, cur_len);
  3854. __write_extent_buffer(dst, src_addr, dst_offset + cur_off, cur_len,
  3855. use_memmove);
  3856. cur_off += cur_len;
  3857. }
  3858. }
  3859. void memmove_extent_buffer(const struct extent_buffer *dst,
  3860. unsigned long dst_offset, unsigned long src_offset,
  3861. unsigned long len)
  3862. {
  3863. unsigned long dst_end = dst_offset + len - 1;
  3864. unsigned long src_end = src_offset + len - 1;
  3865. if (check_eb_range(dst, dst_offset, len) ||
  3866. check_eb_range(dst, src_offset, len))
  3867. return;
  3868. if (dst_offset < src_offset) {
  3869. memcpy_extent_buffer(dst, dst_offset, src_offset, len);
  3870. return;
  3871. }
  3872. if (dst->addr) {
  3873. memmove(dst->addr + dst_offset, dst->addr + src_offset, len);
  3874. return;
  3875. }
  3876. while (len > 0) {
  3877. unsigned long src_i;
  3878. size_t cur;
  3879. size_t dst_off_in_folio;
  3880. size_t src_off_in_folio;
  3881. void *src_addr;
  3882. bool use_memmove;
  3883. src_i = get_eb_folio_index(dst, src_end);
  3884. dst_off_in_folio = get_eb_offset_in_folio(dst, dst_end);
  3885. src_off_in_folio = get_eb_offset_in_folio(dst, src_end);
  3886. cur = min_t(unsigned long, len, src_off_in_folio + 1);
  3887. cur = min(cur, dst_off_in_folio + 1);
  3888. src_addr = folio_address(dst->folios[src_i]) + src_off_in_folio -
  3889. cur + 1;
  3890. use_memmove = areas_overlap(src_end - cur + 1, dst_end - cur + 1,
  3891. cur);
  3892. __write_extent_buffer(dst, src_addr, dst_end - cur + 1, cur,
  3893. use_memmove);
  3894. dst_end -= cur;
  3895. src_end -= cur;
  3896. len -= cur;
  3897. }
  3898. }
  3899. static int try_release_subpage_extent_buffer(struct folio *folio)
  3900. {
  3901. struct btrfs_fs_info *fs_info = folio_to_fs_info(folio);
  3902. struct extent_buffer *eb;
  3903. unsigned long start = (folio_pos(folio) >> fs_info->nodesize_bits);
  3904. unsigned long index = start;
  3905. unsigned long end = index + (PAGE_SIZE >> fs_info->nodesize_bits) - 1;
  3906. int ret;
  3907. rcu_read_lock();
  3908. xa_for_each_range(&fs_info->buffer_tree, index, eb, start, end) {
  3909. /*
  3910. * The same as try_release_extent_buffer(), to ensure the eb
  3911. * won't disappear out from under us.
  3912. */
  3913. spin_lock(&eb->refs_lock);
  3914. rcu_read_unlock();
  3915. if (refcount_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
  3916. spin_unlock(&eb->refs_lock);
  3917. rcu_read_lock();
  3918. continue;
  3919. }
  3920. /*
  3921. * If tree ref isn't set then we know the ref on this eb is a
  3922. * real ref, so just return, this eb will likely be freed soon
  3923. * anyway.
  3924. */
  3925. if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
  3926. spin_unlock(&eb->refs_lock);
  3927. rcu_read_lock();
  3928. break;
  3929. }
  3930. /*
  3931. * Here we don't care about the return value, we will always
  3932. * check the folio private at the end. And
  3933. * release_extent_buffer() will release the refs_lock.
  3934. */
  3935. release_extent_buffer(eb);
  3936. rcu_read_lock();
  3937. }
  3938. rcu_read_unlock();
  3939. /*
  3940. * Finally to check if we have cleared folio private, as if we have
  3941. * released all ebs in the page, the folio private should be cleared now.
  3942. */
  3943. spin_lock(&folio->mapping->i_private_lock);
  3944. if (!folio_test_private(folio))
  3945. ret = 1;
  3946. else
  3947. ret = 0;
  3948. spin_unlock(&folio->mapping->i_private_lock);
  3949. return ret;
  3950. }
  3951. int try_release_extent_buffer(struct folio *folio)
  3952. {
  3953. struct extent_buffer *eb;
  3954. if (btrfs_meta_is_subpage(folio_to_fs_info(folio)))
  3955. return try_release_subpage_extent_buffer(folio);
  3956. /*
  3957. * We need to make sure nobody is changing folio private, as we rely on
  3958. * folio private as the pointer to extent buffer.
  3959. */
  3960. spin_lock(&folio->mapping->i_private_lock);
  3961. if (!folio_test_private(folio)) {
  3962. spin_unlock(&folio->mapping->i_private_lock);
  3963. return 1;
  3964. }
  3965. eb = folio_get_private(folio);
  3966. BUG_ON(!eb);
  3967. /*
  3968. * This is a little awful but should be ok, we need to make sure that
  3969. * the eb doesn't disappear out from under us while we're looking at
  3970. * this page.
  3971. */
  3972. spin_lock(&eb->refs_lock);
  3973. if (refcount_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
  3974. spin_unlock(&eb->refs_lock);
  3975. spin_unlock(&folio->mapping->i_private_lock);
  3976. return 0;
  3977. }
  3978. spin_unlock(&folio->mapping->i_private_lock);
  3979. /*
  3980. * If tree ref isn't set then we know the ref on this eb is a real ref,
  3981. * so just return, this page will likely be freed soon anyway.
  3982. */
  3983. if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
  3984. spin_unlock(&eb->refs_lock);
  3985. return 0;
  3986. }
  3987. return release_extent_buffer(eb);
  3988. }
  3989. /*
  3990. * Attempt to readahead a child block.
  3991. *
  3992. * @fs_info: the fs_info
  3993. * @bytenr: bytenr to read
  3994. * @owner_root: objectid of the root that owns this eb
  3995. * @gen: generation for the uptodate check, can be 0
  3996. * @level: level for the eb
  3997. *
  3998. * Attempt to readahead a tree block at @bytenr. If @gen is 0 then we do a
  3999. * normal uptodate check of the eb, without checking the generation. If we have
  4000. * to read the block we will not block on anything.
  4001. */
  4002. void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info,
  4003. u64 bytenr, u64 owner_root, u64 gen, int level)
  4004. {
  4005. struct btrfs_tree_parent_check check = {
  4006. .level = level,
  4007. .transid = gen
  4008. };
  4009. struct extent_buffer *eb;
  4010. int ret;
  4011. eb = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level);
  4012. if (IS_ERR(eb))
  4013. return;
  4014. if (btrfs_buffer_uptodate(eb, gen, true)) {
  4015. free_extent_buffer(eb);
  4016. return;
  4017. }
  4018. ret = read_extent_buffer_pages_nowait(eb, 0, &check);
  4019. if (ret < 0)
  4020. free_extent_buffer_stale(eb);
  4021. else
  4022. free_extent_buffer(eb);
  4023. }
  4024. /*
  4025. * Readahead a node's child block.
  4026. *
  4027. * @node: parent node we're reading from
  4028. * @slot: slot in the parent node for the child we want to read
  4029. *
  4030. * A helper for btrfs_readahead_tree_block, we simply read the bytenr pointed at
  4031. * the slot in the node provided.
  4032. */
  4033. void btrfs_readahead_node_child(struct extent_buffer *node, int slot)
  4034. {
  4035. btrfs_readahead_tree_block(node->fs_info,
  4036. btrfs_node_blockptr(node, slot),
  4037. btrfs_header_owner(node),
  4038. btrfs_node_ptr_generation(node, slot),
  4039. btrfs_header_level(node) - 1);
  4040. }