inode.c 195 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext4/inode.c
  4. *
  5. * Copyright (C) 1992, 1993, 1994, 1995
  6. * Remy Card (card@masi.ibp.fr)
  7. * Laboratoire MASI - Institut Blaise Pascal
  8. * Universite Pierre et Marie Curie (Paris VI)
  9. *
  10. * from
  11. *
  12. * linux/fs/minix/inode.c
  13. *
  14. * Copyright (C) 1991, 1992 Linus Torvalds
  15. *
  16. * 64-bit file support on 64-bit platforms by Jakub Jelinek
  17. * (jj@sunsite.ms.mff.cuni.cz)
  18. *
  19. * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/mount.h>
  23. #include <linux/time.h>
  24. #include <linux/highuid.h>
  25. #include <linux/pagemap.h>
  26. #include <linux/dax.h>
  27. #include <linux/quotaops.h>
  28. #include <linux/string.h>
  29. #include <linux/buffer_head.h>
  30. #include <linux/writeback.h>
  31. #include <linux/pagevec.h>
  32. #include <linux/mpage.h>
  33. #include <linux/rmap.h>
  34. #include <linux/namei.h>
  35. #include <linux/uio.h>
  36. #include <linux/bio.h>
  37. #include <linux/workqueue.h>
  38. #include <linux/kernel.h>
  39. #include <linux/printk.h>
  40. #include <linux/slab.h>
  41. #include <linux/bitops.h>
  42. #include <linux/iomap.h>
  43. #include <linux/iversion.h>
  44. #include "ext4_jbd2.h"
  45. #include "xattr.h"
  46. #include "acl.h"
  47. #include "truncate.h"
  48. #include <kunit/static_stub.h>
  49. #include <trace/events/ext4.h>
  50. static void ext4_journalled_zero_new_buffers(handle_t *handle,
  51. struct inode *inode,
  52. struct folio *folio,
  53. unsigned from, unsigned to);
  54. static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
  55. struct ext4_inode_info *ei)
  56. {
  57. __u32 csum;
  58. __u16 dummy_csum = 0;
  59. int offset = offsetof(struct ext4_inode, i_checksum_lo);
  60. unsigned int csum_size = sizeof(dummy_csum);
  61. csum = ext4_chksum(ei->i_csum_seed, (__u8 *)raw, offset);
  62. csum = ext4_chksum(csum, (__u8 *)&dummy_csum, csum_size);
  63. offset += csum_size;
  64. csum = ext4_chksum(csum, (__u8 *)raw + offset,
  65. EXT4_GOOD_OLD_INODE_SIZE - offset);
  66. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  67. offset = offsetof(struct ext4_inode, i_checksum_hi);
  68. csum = ext4_chksum(csum, (__u8 *)raw + EXT4_GOOD_OLD_INODE_SIZE,
  69. offset - EXT4_GOOD_OLD_INODE_SIZE);
  70. if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
  71. csum = ext4_chksum(csum, (__u8 *)&dummy_csum,
  72. csum_size);
  73. offset += csum_size;
  74. }
  75. csum = ext4_chksum(csum, (__u8 *)raw + offset,
  76. EXT4_INODE_SIZE(inode->i_sb) - offset);
  77. }
  78. return csum;
  79. }
  80. static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
  81. struct ext4_inode_info *ei)
  82. {
  83. __u32 provided, calculated;
  84. if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
  85. cpu_to_le32(EXT4_OS_LINUX) ||
  86. !ext4_has_feature_metadata_csum(inode->i_sb))
  87. return 1;
  88. provided = le16_to_cpu(raw->i_checksum_lo);
  89. calculated = ext4_inode_csum(inode, raw, ei);
  90. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
  91. EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
  92. provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16;
  93. else
  94. calculated &= 0xFFFF;
  95. return provided == calculated;
  96. }
  97. void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
  98. struct ext4_inode_info *ei)
  99. {
  100. __u32 csum;
  101. if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
  102. cpu_to_le32(EXT4_OS_LINUX) ||
  103. !ext4_has_feature_metadata_csum(inode->i_sb))
  104. return;
  105. csum = ext4_inode_csum(inode, raw, ei);
  106. raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF);
  107. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
  108. EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
  109. raw->i_checksum_hi = cpu_to_le16(csum >> 16);
  110. }
  111. static inline int ext4_begin_ordered_truncate(struct inode *inode,
  112. loff_t new_size)
  113. {
  114. struct jbd2_inode *jinode = READ_ONCE(EXT4_I(inode)->jinode);
  115. trace_ext4_begin_ordered_truncate(inode, new_size);
  116. /*
  117. * If jinode is zero, then we never opened the file for
  118. * writing, so there's no need to call
  119. * jbd2_journal_begin_ordered_truncate() since there's no
  120. * outstanding writes we need to flush.
  121. */
  122. if (!jinode)
  123. return 0;
  124. return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
  125. jinode,
  126. new_size);
  127. }
  128. /*
  129. * Test whether an inode is a fast symlink.
  130. * A fast symlink has its symlink data stored in ext4_inode_info->i_data.
  131. */
  132. int ext4_inode_is_fast_symlink(struct inode *inode)
  133. {
  134. if (!ext4_has_feature_ea_inode(inode->i_sb)) {
  135. int ea_blocks = EXT4_I(inode)->i_file_acl ?
  136. EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0;
  137. if (ext4_has_inline_data(inode))
  138. return 0;
  139. return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
  140. }
  141. return S_ISLNK(inode->i_mode) && inode->i_size &&
  142. (inode->i_size < EXT4_N_BLOCKS * 4);
  143. }
  144. /*
  145. * Called at the last iput() if i_nlink is zero.
  146. */
  147. void ext4_evict_inode(struct inode *inode)
  148. {
  149. handle_t *handle;
  150. int err;
  151. /*
  152. * Credits for final inode cleanup and freeing:
  153. * sb + inode (ext4_orphan_del()), block bitmap, group descriptor
  154. * (xattr block freeing), bitmap, group descriptor (inode freeing)
  155. */
  156. int extra_credits = 6;
  157. struct ext4_xattr_inode_array *ea_inode_array = NULL;
  158. bool freeze_protected = false;
  159. trace_ext4_evict_inode(inode);
  160. dax_break_layout_final(inode);
  161. if (EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)
  162. ext4_evict_ea_inode(inode);
  163. if (inode->i_nlink) {
  164. /*
  165. * If there's dirty page will lead to data loss, user
  166. * could see stale data.
  167. */
  168. if (unlikely(!ext4_emergency_state(inode->i_sb) &&
  169. mapping_tagged(&inode->i_data, PAGECACHE_TAG_DIRTY)))
  170. ext4_warning_inode(inode, "data will be lost");
  171. truncate_inode_pages_final(&inode->i_data);
  172. goto no_delete;
  173. }
  174. if (is_bad_inode(inode))
  175. goto no_delete;
  176. dquot_initialize(inode);
  177. if (ext4_should_order_data(inode))
  178. ext4_begin_ordered_truncate(inode, 0);
  179. truncate_inode_pages_final(&inode->i_data);
  180. /*
  181. * For inodes with journalled data, transaction commit could have
  182. * dirtied the inode. And for inodes with dioread_nolock, unwritten
  183. * extents converting worker could merge extents and also have dirtied
  184. * the inode. Flush worker is ignoring it because of I_FREEING flag but
  185. * we still need to remove the inode from the writeback lists.
  186. */
  187. inode_io_list_del(inode);
  188. /*
  189. * Protect us against freezing - iput() caller didn't have to have any
  190. * protection against it. When we are in a running transaction though,
  191. * we are already protected against freezing and we cannot grab further
  192. * protection due to lock ordering constraints.
  193. */
  194. if (!ext4_journal_current_handle()) {
  195. sb_start_intwrite(inode->i_sb);
  196. freeze_protected = true;
  197. }
  198. if (!IS_NOQUOTA(inode))
  199. extra_credits += EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb);
  200. /*
  201. * Block bitmap, group descriptor, and inode are accounted in both
  202. * ext4_blocks_for_truncate() and extra_credits. So subtract 3.
  203. */
  204. handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
  205. ext4_blocks_for_truncate(inode) + extra_credits - 3);
  206. if (IS_ERR(handle)) {
  207. ext4_std_error(inode->i_sb, PTR_ERR(handle));
  208. /*
  209. * If we're going to skip the normal cleanup, we still need to
  210. * make sure that the in-core orphan linked list is properly
  211. * cleaned up.
  212. */
  213. ext4_orphan_del(NULL, inode);
  214. if (freeze_protected)
  215. sb_end_intwrite(inode->i_sb);
  216. goto no_delete;
  217. }
  218. if (IS_SYNC(inode))
  219. ext4_handle_sync(handle);
  220. /*
  221. * Set inode->i_size to 0 before calling ext4_truncate(). We need
  222. * special handling of symlinks here because i_size is used to
  223. * determine whether ext4_inode_info->i_data contains symlink data or
  224. * block mappings. Setting i_size to 0 will remove its fast symlink
  225. * status. Erase i_data so that it becomes a valid empty block map.
  226. */
  227. if (ext4_inode_is_fast_symlink(inode))
  228. memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data));
  229. inode->i_size = 0;
  230. err = ext4_mark_inode_dirty(handle, inode);
  231. if (err) {
  232. ext4_warning(inode->i_sb,
  233. "couldn't mark inode dirty (err %d)", err);
  234. goto stop_handle;
  235. }
  236. if (inode->i_blocks) {
  237. err = ext4_truncate(inode);
  238. if (err) {
  239. ext4_error_err(inode->i_sb, -err,
  240. "couldn't truncate inode %lu (err %d)",
  241. inode->i_ino, err);
  242. goto stop_handle;
  243. }
  244. }
  245. /* Remove xattr references. */
  246. err = ext4_xattr_delete_inode(handle, inode, &ea_inode_array,
  247. extra_credits);
  248. if (err) {
  249. ext4_warning(inode->i_sb, "xattr delete (err %d)", err);
  250. stop_handle:
  251. ext4_journal_stop(handle);
  252. ext4_orphan_del(NULL, inode);
  253. if (freeze_protected)
  254. sb_end_intwrite(inode->i_sb);
  255. ext4_xattr_inode_array_free(ea_inode_array);
  256. goto no_delete;
  257. }
  258. /*
  259. * Kill off the orphan record which ext4_truncate created.
  260. * AKPM: I think this can be inside the above `if'.
  261. * Note that ext4_orphan_del() has to be able to cope with the
  262. * deletion of a non-existent orphan - this is because we don't
  263. * know if ext4_truncate() actually created an orphan record.
  264. * (Well, we could do this if we need to, but heck - it works)
  265. */
  266. ext4_orphan_del(handle, inode);
  267. EXT4_I(inode)->i_dtime = (__u32)ktime_get_real_seconds();
  268. /*
  269. * One subtle ordering requirement: if anything has gone wrong
  270. * (transaction abort, IO errors, whatever), then we can still
  271. * do these next steps (the fs will already have been marked as
  272. * having errors), but we can't free the inode if the mark_dirty
  273. * fails.
  274. */
  275. if (ext4_mark_inode_dirty(handle, inode))
  276. /* If that failed, just do the required in-core inode clear. */
  277. ext4_clear_inode(inode);
  278. else
  279. ext4_free_inode(handle, inode);
  280. ext4_journal_stop(handle);
  281. if (freeze_protected)
  282. sb_end_intwrite(inode->i_sb);
  283. ext4_xattr_inode_array_free(ea_inode_array);
  284. return;
  285. no_delete:
  286. /*
  287. * Check out some where else accidentally dirty the evicting inode,
  288. * which may probably cause inode use-after-free issues later.
  289. */
  290. WARN_ON_ONCE(!list_empty_careful(&inode->i_io_list));
  291. if (!list_empty(&EXT4_I(inode)->i_fc_list))
  292. ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_NOMEM, NULL);
  293. ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
  294. }
  295. #ifdef CONFIG_QUOTA
  296. qsize_t *ext4_get_reserved_space(struct inode *inode)
  297. {
  298. return &EXT4_I(inode)->i_reserved_quota;
  299. }
  300. #endif
  301. /*
  302. * Called with i_data_sem down, which is important since we can call
  303. * ext4_discard_preallocations() from here.
  304. */
  305. void ext4_da_update_reserve_space(struct inode *inode,
  306. int used, int quota_claim)
  307. {
  308. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  309. struct ext4_inode_info *ei = EXT4_I(inode);
  310. spin_lock(&ei->i_block_reservation_lock);
  311. trace_ext4_da_update_reserve_space(inode, used, quota_claim);
  312. if (unlikely(used > ei->i_reserved_data_blocks)) {
  313. ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
  314. "with only %d reserved data blocks",
  315. __func__, inode->i_ino, used,
  316. ei->i_reserved_data_blocks);
  317. WARN_ON(1);
  318. used = ei->i_reserved_data_blocks;
  319. }
  320. /* Update per-inode reservations */
  321. ei->i_reserved_data_blocks -= used;
  322. percpu_counter_sub(&sbi->s_dirtyclusters_counter, used);
  323. spin_unlock(&ei->i_block_reservation_lock);
  324. /* Update quota subsystem for data blocks */
  325. if (quota_claim)
  326. dquot_claim_block(inode, EXT4_C2B(sbi, used));
  327. else {
  328. /*
  329. * We did fallocate with an offset that is already delayed
  330. * allocated. So on delayed allocated writeback we should
  331. * not re-claim the quota for fallocated blocks.
  332. */
  333. dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
  334. }
  335. /*
  336. * If we have done all the pending block allocations and if
  337. * there aren't any writers on the inode, we can discard the
  338. * inode's preallocations.
  339. */
  340. if ((ei->i_reserved_data_blocks == 0) &&
  341. !inode_is_open_for_write(inode))
  342. ext4_discard_preallocations(inode);
  343. }
  344. static int __check_block_validity(struct inode *inode, const char *func,
  345. unsigned int line,
  346. struct ext4_map_blocks *map)
  347. {
  348. journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
  349. if (journal && inode == journal->j_inode)
  350. return 0;
  351. if (!ext4_inode_block_valid(inode, map->m_pblk, map->m_len)) {
  352. ext4_error_inode(inode, func, line, map->m_pblk,
  353. "lblock %lu mapped to illegal pblock %llu "
  354. "(length %d)", (unsigned long) map->m_lblk,
  355. map->m_pblk, map->m_len);
  356. return -EFSCORRUPTED;
  357. }
  358. return 0;
  359. }
  360. int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk,
  361. ext4_lblk_t len)
  362. {
  363. int ret;
  364. KUNIT_STATIC_STUB_REDIRECT(ext4_issue_zeroout, inode, lblk, pblk, len);
  365. if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode))
  366. return fscrypt_zeroout_range(inode, lblk, pblk, len);
  367. ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS);
  368. if (ret > 0)
  369. ret = 0;
  370. return ret;
  371. }
  372. /*
  373. * For generic regular files, when updating the extent tree, Ext4 should
  374. * hold the i_rwsem and invalidate_lock exclusively. This ensures
  375. * exclusion against concurrent page faults, as well as reads and writes.
  376. */
  377. #ifdef CONFIG_EXT4_DEBUG
  378. void ext4_check_map_extents_env(struct inode *inode)
  379. {
  380. if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
  381. return;
  382. if (!S_ISREG(inode->i_mode) ||
  383. IS_NOQUOTA(inode) || IS_VERITY(inode) ||
  384. is_special_ino(inode->i_sb, inode->i_ino) ||
  385. (inode_state_read_once(inode) & (I_FREEING | I_WILL_FREE | I_NEW)) ||
  386. ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE) ||
  387. ext4_verity_in_progress(inode))
  388. return;
  389. WARN_ON_ONCE(!inode_is_locked(inode) &&
  390. !rwsem_is_locked(&inode->i_mapping->invalidate_lock));
  391. }
  392. #else
  393. void ext4_check_map_extents_env(struct inode *inode) {}
  394. #endif
  395. #define check_block_validity(inode, map) \
  396. __check_block_validity((inode), __func__, __LINE__, (map))
  397. #ifdef ES_AGGRESSIVE_TEST
  398. static void ext4_map_blocks_es_recheck(handle_t *handle,
  399. struct inode *inode,
  400. struct ext4_map_blocks *es_map,
  401. struct ext4_map_blocks *map,
  402. int flags)
  403. {
  404. int retval;
  405. map->m_flags = 0;
  406. /*
  407. * There is a race window that the result is not the same.
  408. * e.g. xfstests #223 when dioread_nolock enables. The reason
  409. * is that we lookup a block mapping in extent status tree with
  410. * out taking i_data_sem. So at the time the unwritten extent
  411. * could be converted.
  412. */
  413. down_read(&EXT4_I(inode)->i_data_sem);
  414. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  415. retval = ext4_ext_map_blocks(handle, inode, map, 0);
  416. } else {
  417. retval = ext4_ind_map_blocks(handle, inode, map, 0);
  418. }
  419. up_read((&EXT4_I(inode)->i_data_sem));
  420. /*
  421. * We don't check m_len because extent will be collpased in status
  422. * tree. So the m_len might not equal.
  423. */
  424. if (es_map->m_lblk != map->m_lblk ||
  425. es_map->m_flags != map->m_flags ||
  426. es_map->m_pblk != map->m_pblk) {
  427. printk("ES cache assertion failed for inode: %lu "
  428. "es_cached ex [%d/%d/%llu/%x] != "
  429. "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
  430. inode->i_ino, es_map->m_lblk, es_map->m_len,
  431. es_map->m_pblk, es_map->m_flags, map->m_lblk,
  432. map->m_len, map->m_pblk, map->m_flags,
  433. retval, flags);
  434. }
  435. }
  436. #endif /* ES_AGGRESSIVE_TEST */
  437. static int ext4_map_query_blocks_next_in_leaf(handle_t *handle,
  438. struct inode *inode, struct ext4_map_blocks *map,
  439. unsigned int orig_mlen)
  440. {
  441. struct ext4_map_blocks map2;
  442. unsigned int status, status2;
  443. int retval;
  444. status = map->m_flags & EXT4_MAP_UNWRITTEN ?
  445. EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
  446. WARN_ON_ONCE(!(map->m_flags & EXT4_MAP_QUERY_LAST_IN_LEAF));
  447. WARN_ON_ONCE(orig_mlen <= map->m_len);
  448. /* Prepare map2 for lookup in next leaf block */
  449. map2.m_lblk = map->m_lblk + map->m_len;
  450. map2.m_len = orig_mlen - map->m_len;
  451. map2.m_flags = 0;
  452. retval = ext4_ext_map_blocks(handle, inode, &map2, 0);
  453. if (retval <= 0) {
  454. ext4_es_cache_extent(inode, map->m_lblk, map->m_len,
  455. map->m_pblk, status);
  456. return map->m_len;
  457. }
  458. if (unlikely(retval != map2.m_len)) {
  459. ext4_warning(inode->i_sb,
  460. "ES len assertion failed for inode "
  461. "%lu: retval %d != map->m_len %d",
  462. inode->i_ino, retval, map2.m_len);
  463. WARN_ON(1);
  464. }
  465. status2 = map2.m_flags & EXT4_MAP_UNWRITTEN ?
  466. EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
  467. /*
  468. * If map2 is contiguous with map, then let's insert it as a single
  469. * extent in es cache and return the combined length of both the maps.
  470. */
  471. if (map->m_pblk + map->m_len == map2.m_pblk &&
  472. status == status2) {
  473. ext4_es_cache_extent(inode, map->m_lblk,
  474. map->m_len + map2.m_len, map->m_pblk,
  475. status);
  476. map->m_len += map2.m_len;
  477. } else {
  478. ext4_es_cache_extent(inode, map->m_lblk, map->m_len,
  479. map->m_pblk, status);
  480. }
  481. return map->m_len;
  482. }
  483. int ext4_map_query_blocks(handle_t *handle, struct inode *inode,
  484. struct ext4_map_blocks *map, int flags)
  485. {
  486. unsigned int status;
  487. int retval;
  488. unsigned int orig_mlen = map->m_len;
  489. flags &= EXT4_EX_QUERY_FILTER;
  490. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  491. retval = ext4_ext_map_blocks(handle, inode, map, flags);
  492. else
  493. retval = ext4_ind_map_blocks(handle, inode, map, flags);
  494. if (retval < 0)
  495. return retval;
  496. /* A hole? */
  497. if (retval == 0)
  498. goto out;
  499. if (unlikely(retval != map->m_len)) {
  500. ext4_warning(inode->i_sb,
  501. "ES len assertion failed for inode "
  502. "%lu: retval %d != map->m_len %d",
  503. inode->i_ino, retval, map->m_len);
  504. WARN_ON(1);
  505. }
  506. /*
  507. * No need to query next in leaf:
  508. * - if returned extent is not last in leaf or
  509. * - if the last in leaf is the full requested range
  510. */
  511. if (!(map->m_flags & EXT4_MAP_QUERY_LAST_IN_LEAF) ||
  512. map->m_len == orig_mlen) {
  513. status = map->m_flags & EXT4_MAP_UNWRITTEN ?
  514. EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
  515. ext4_es_cache_extent(inode, map->m_lblk, map->m_len,
  516. map->m_pblk, status);
  517. } else {
  518. retval = ext4_map_query_blocks_next_in_leaf(handle, inode, map,
  519. orig_mlen);
  520. }
  521. out:
  522. map->m_seq = READ_ONCE(EXT4_I(inode)->i_es_seq);
  523. return retval;
  524. }
  525. int ext4_map_create_blocks(handle_t *handle, struct inode *inode,
  526. struct ext4_map_blocks *map, int flags)
  527. {
  528. unsigned int status;
  529. int err, retval = 0;
  530. /*
  531. * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE
  532. * indicates that the blocks and quotas has already been
  533. * checked when the data was copied into the page cache.
  534. */
  535. if (map->m_flags & EXT4_MAP_DELAYED)
  536. flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
  537. /*
  538. * Here we clear m_flags because after allocating an new extent,
  539. * it will be set again.
  540. */
  541. map->m_flags &= ~EXT4_MAP_FLAGS;
  542. /*
  543. * We need to check for EXT4 here because migrate could have
  544. * changed the inode type in between.
  545. */
  546. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  547. retval = ext4_ext_map_blocks(handle, inode, map, flags);
  548. } else {
  549. retval = ext4_ind_map_blocks(handle, inode, map, flags);
  550. /*
  551. * We allocated new blocks which will result in i_data's
  552. * format changing. Force the migrate to fail by clearing
  553. * migrate flags.
  554. */
  555. if (retval > 0 && map->m_flags & EXT4_MAP_NEW)
  556. ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
  557. }
  558. if (retval <= 0)
  559. return retval;
  560. if (unlikely(retval != map->m_len)) {
  561. ext4_warning(inode->i_sb,
  562. "ES len assertion failed for inode %lu: "
  563. "retval %d != map->m_len %d",
  564. inode->i_ino, retval, map->m_len);
  565. WARN_ON(1);
  566. }
  567. /*
  568. * We have to zeroout blocks before inserting them into extent
  569. * status tree. Otherwise someone could look them up there and
  570. * use them before they are really zeroed. We also have to
  571. * unmap metadata before zeroing as otherwise writeback can
  572. * overwrite zeros with stale data from block device.
  573. */
  574. if (flags & EXT4_GET_BLOCKS_ZERO &&
  575. map->m_flags & EXT4_MAP_MAPPED && map->m_flags & EXT4_MAP_NEW) {
  576. err = ext4_issue_zeroout(inode, map->m_lblk, map->m_pblk,
  577. map->m_len);
  578. if (err)
  579. return err;
  580. }
  581. status = map->m_flags & EXT4_MAP_UNWRITTEN ?
  582. EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
  583. ext4_es_insert_extent(inode, map->m_lblk, map->m_len, map->m_pblk,
  584. status, flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE);
  585. map->m_seq = READ_ONCE(EXT4_I(inode)->i_es_seq);
  586. return retval;
  587. }
  588. /*
  589. * The ext4_map_blocks() function tries to look up the requested blocks,
  590. * and returns if the blocks are already mapped.
  591. *
  592. * Otherwise it takes the write lock of the i_data_sem and allocate blocks
  593. * and store the allocated blocks in the result buffer head and mark it
  594. * mapped.
  595. *
  596. * If file type is extents based, it will call ext4_ext_map_blocks(),
  597. * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
  598. * based files
  599. *
  600. * On success, it returns the number of blocks being mapped or allocated.
  601. * If flags doesn't contain EXT4_GET_BLOCKS_CREATE the blocks are
  602. * pre-allocated and unwritten, the resulting @map is marked as unwritten.
  603. * If the flags contain EXT4_GET_BLOCKS_CREATE, it will mark @map as mapped.
  604. *
  605. * It returns 0 if plain look up failed (blocks have not been allocated), in
  606. * that case, @map is returned as unmapped but we still do fill map->m_len to
  607. * indicate the length of a hole starting at map->m_lblk.
  608. *
  609. * It returns the error in case of allocation failure.
  610. */
  611. int ext4_map_blocks(handle_t *handle, struct inode *inode,
  612. struct ext4_map_blocks *map, int flags)
  613. {
  614. struct extent_status es;
  615. int retval;
  616. int ret = 0;
  617. unsigned int orig_mlen = map->m_len;
  618. #ifdef ES_AGGRESSIVE_TEST
  619. struct ext4_map_blocks orig_map;
  620. memcpy(&orig_map, map, sizeof(*map));
  621. #endif
  622. map->m_flags = 0;
  623. ext_debug(inode, "flag 0x%x, max_blocks %u, logical block %lu\n",
  624. flags, map->m_len, (unsigned long) map->m_lblk);
  625. /*
  626. * ext4_map_blocks returns an int, and m_len is an unsigned int
  627. */
  628. if (unlikely(map->m_len > INT_MAX))
  629. map->m_len = INT_MAX;
  630. /* We can handle the block number less than EXT_MAX_BLOCKS */
  631. if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS))
  632. return -EFSCORRUPTED;
  633. /*
  634. * Callers from the context of data submission are the only exceptions
  635. * for regular files that do not hold the i_rwsem or invalidate_lock.
  636. * However, caching unrelated ranges is not permitted.
  637. */
  638. if (flags & EXT4_GET_BLOCKS_IO_SUBMIT)
  639. WARN_ON_ONCE(!(flags & EXT4_EX_NOCACHE));
  640. else
  641. ext4_check_map_extents_env(inode);
  642. /* Lookup extent status tree firstly */
  643. if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es, &map->m_seq)) {
  644. if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
  645. map->m_pblk = ext4_es_pblock(&es) +
  646. map->m_lblk - es.es_lblk;
  647. map->m_flags |= ext4_es_is_written(&es) ?
  648. EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;
  649. retval = es.es_len - (map->m_lblk - es.es_lblk);
  650. if (retval > map->m_len)
  651. retval = map->m_len;
  652. map->m_len = retval;
  653. } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) {
  654. map->m_pblk = 0;
  655. map->m_flags |= ext4_es_is_delayed(&es) ?
  656. EXT4_MAP_DELAYED : 0;
  657. retval = es.es_len - (map->m_lblk - es.es_lblk);
  658. if (retval > map->m_len)
  659. retval = map->m_len;
  660. map->m_len = retval;
  661. retval = 0;
  662. } else {
  663. BUG();
  664. }
  665. if (flags & EXT4_GET_BLOCKS_CACHED_NOWAIT)
  666. return retval;
  667. #ifdef ES_AGGRESSIVE_TEST
  668. ext4_map_blocks_es_recheck(handle, inode, map,
  669. &orig_map, flags);
  670. #endif
  671. if (!(flags & EXT4_GET_BLOCKS_QUERY_LAST_IN_LEAF) ||
  672. orig_mlen == map->m_len)
  673. goto found;
  674. map->m_len = orig_mlen;
  675. }
  676. /*
  677. * In the query cache no-wait mode, nothing we can do more if we
  678. * cannot find extent in the cache.
  679. */
  680. if (flags & EXT4_GET_BLOCKS_CACHED_NOWAIT)
  681. return 0;
  682. /*
  683. * Try to see if we can get the block without requesting a new
  684. * file system block.
  685. */
  686. down_read(&EXT4_I(inode)->i_data_sem);
  687. retval = ext4_map_query_blocks(handle, inode, map, flags);
  688. up_read((&EXT4_I(inode)->i_data_sem));
  689. found:
  690. if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
  691. ret = check_block_validity(inode, map);
  692. if (ret != 0)
  693. return ret;
  694. }
  695. /* If it is only a block(s) look up */
  696. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
  697. return retval;
  698. /*
  699. * Returns if the blocks have already allocated
  700. *
  701. * Note that if blocks have been preallocated
  702. * ext4_ext_map_blocks() returns with buffer head unmapped
  703. */
  704. if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
  705. /*
  706. * If we need to convert extent to unwritten
  707. * we continue and do the actual work in
  708. * ext4_ext_map_blocks()
  709. */
  710. if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
  711. return retval;
  712. ext4_fc_track_inode(handle, inode);
  713. /*
  714. * New blocks allocate and/or writing to unwritten extent
  715. * will possibly result in updating i_data, so we take
  716. * the write lock of i_data_sem, and call get_block()
  717. * with create == 1 flag.
  718. */
  719. down_write(&EXT4_I(inode)->i_data_sem);
  720. retval = ext4_map_create_blocks(handle, inode, map, flags);
  721. up_write((&EXT4_I(inode)->i_data_sem));
  722. if (retval < 0)
  723. ext_debug(inode, "failed with err %d\n", retval);
  724. if (retval <= 0)
  725. return retval;
  726. if (map->m_flags & EXT4_MAP_MAPPED) {
  727. ret = check_block_validity(inode, map);
  728. if (ret != 0)
  729. return ret;
  730. /*
  731. * Inodes with freshly allocated blocks where contents will be
  732. * visible after transaction commit must be on transaction's
  733. * ordered data list.
  734. */
  735. if (map->m_flags & EXT4_MAP_NEW &&
  736. !(map->m_flags & EXT4_MAP_UNWRITTEN) &&
  737. !(flags & EXT4_GET_BLOCKS_ZERO) &&
  738. !ext4_is_quota_file(inode) &&
  739. ext4_should_order_data(inode)) {
  740. loff_t start_byte = EXT4_LBLK_TO_B(inode, map->m_lblk);
  741. loff_t length = EXT4_LBLK_TO_B(inode, map->m_len);
  742. if (flags & EXT4_GET_BLOCKS_IO_SUBMIT)
  743. ret = ext4_jbd2_inode_add_wait(handle, inode,
  744. start_byte, length);
  745. else
  746. ret = ext4_jbd2_inode_add_write(handle, inode,
  747. start_byte, length);
  748. if (ret)
  749. return ret;
  750. }
  751. }
  752. ext4_fc_track_range(handle, inode, map->m_lblk, map->m_lblk +
  753. map->m_len - 1);
  754. return retval;
  755. }
  756. /*
  757. * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages
  758. * we have to be careful as someone else may be manipulating b_state as well.
  759. */
  760. static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags)
  761. {
  762. unsigned long old_state;
  763. unsigned long new_state;
  764. flags &= EXT4_MAP_FLAGS;
  765. /* Dummy buffer_head? Set non-atomically. */
  766. if (!bh->b_folio) {
  767. bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags;
  768. return;
  769. }
  770. /*
  771. * Someone else may be modifying b_state. Be careful! This is ugly but
  772. * once we get rid of using bh as a container for mapping information
  773. * to pass to / from get_block functions, this can go away.
  774. */
  775. old_state = READ_ONCE(bh->b_state);
  776. do {
  777. new_state = (old_state & ~EXT4_MAP_FLAGS) | flags;
  778. } while (unlikely(!try_cmpxchg(&bh->b_state, &old_state, new_state)));
  779. }
  780. /*
  781. * Make sure that the current journal transaction has enough credits to map
  782. * one extent. Return -EAGAIN if it cannot extend the current running
  783. * transaction.
  784. */
  785. static inline int ext4_journal_ensure_extent_credits(handle_t *handle,
  786. struct inode *inode)
  787. {
  788. int credits;
  789. int ret;
  790. /* Called from ext4_da_write_begin() which has no handle started? */
  791. if (!handle)
  792. return 0;
  793. credits = ext4_chunk_trans_blocks(inode, 1);
  794. ret = __ext4_journal_ensure_credits(handle, credits, credits, 0);
  795. return ret <= 0 ? ret : -EAGAIN;
  796. }
  797. static int _ext4_get_block(struct inode *inode, sector_t iblock,
  798. struct buffer_head *bh, int flags)
  799. {
  800. struct ext4_map_blocks map;
  801. int ret = 0;
  802. if (ext4_has_inline_data(inode))
  803. return -ERANGE;
  804. map.m_lblk = iblock;
  805. map.m_len = bh->b_size >> inode->i_blkbits;
  806. ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map,
  807. flags);
  808. if (ret > 0) {
  809. map_bh(bh, inode->i_sb, map.m_pblk);
  810. ext4_update_bh_state(bh, map.m_flags);
  811. bh->b_size = inode->i_sb->s_blocksize * map.m_len;
  812. ret = 0;
  813. } else if (ret == 0) {
  814. /* hole case, need to fill in bh->b_size */
  815. bh->b_size = inode->i_sb->s_blocksize * map.m_len;
  816. }
  817. return ret;
  818. }
  819. int ext4_get_block(struct inode *inode, sector_t iblock,
  820. struct buffer_head *bh, int create)
  821. {
  822. return _ext4_get_block(inode, iblock, bh,
  823. create ? EXT4_GET_BLOCKS_CREATE : 0);
  824. }
  825. /*
  826. * Get block function used when preparing for buffered write if we require
  827. * creating an unwritten extent if blocks haven't been allocated. The extent
  828. * will be converted to written after the IO is complete.
  829. */
  830. int ext4_get_block_unwritten(struct inode *inode, sector_t iblock,
  831. struct buffer_head *bh_result, int create)
  832. {
  833. int ret = 0;
  834. ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n",
  835. inode->i_ino, create);
  836. ret = _ext4_get_block(inode, iblock, bh_result,
  837. EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT);
  838. /*
  839. * If the buffer is marked unwritten, mark it as new to make sure it is
  840. * zeroed out correctly in case of partial writes. Otherwise, there is
  841. * a chance of stale data getting exposed.
  842. */
  843. if (ret == 0 && buffer_unwritten(bh_result))
  844. set_buffer_new(bh_result);
  845. return ret;
  846. }
  847. /* Maximum number of blocks we map for direct IO at once. */
  848. #define DIO_MAX_BLOCKS 4096
  849. /*
  850. * `handle' can be NULL if create is zero
  851. */
  852. struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
  853. ext4_lblk_t block, int map_flags)
  854. {
  855. struct ext4_map_blocks map;
  856. struct buffer_head *bh;
  857. int create = map_flags & EXT4_GET_BLOCKS_CREATE;
  858. bool nowait = map_flags & EXT4_GET_BLOCKS_CACHED_NOWAIT;
  859. int err;
  860. ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
  861. || handle != NULL || create == 0);
  862. ASSERT(create == 0 || !nowait);
  863. map.m_lblk = block;
  864. map.m_len = 1;
  865. err = ext4_map_blocks(handle, inode, &map, map_flags);
  866. if (err == 0)
  867. return create ? ERR_PTR(-ENOSPC) : NULL;
  868. if (err < 0)
  869. return ERR_PTR(err);
  870. if (nowait)
  871. return sb_find_get_block(inode->i_sb, map.m_pblk);
  872. /*
  873. * Since bh could introduce extra ref count such as referred by
  874. * journal_head etc. Try to avoid using __GFP_MOVABLE here
  875. * as it may fail the migration when journal_head remains.
  876. */
  877. bh = getblk_unmovable(inode->i_sb->s_bdev, map.m_pblk,
  878. inode->i_sb->s_blocksize);
  879. if (unlikely(!bh))
  880. return ERR_PTR(-ENOMEM);
  881. if (map.m_flags & EXT4_MAP_NEW) {
  882. ASSERT(create != 0);
  883. ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
  884. || (handle != NULL));
  885. /*
  886. * Now that we do not always journal data, we should
  887. * keep in mind whether this should always journal the
  888. * new buffer as metadata. For now, regular file
  889. * writes use ext4_get_block instead, so it's not a
  890. * problem.
  891. */
  892. lock_buffer(bh);
  893. BUFFER_TRACE(bh, "call get_create_access");
  894. err = ext4_journal_get_create_access(handle, inode->i_sb, bh,
  895. EXT4_JTR_NONE);
  896. if (unlikely(err)) {
  897. unlock_buffer(bh);
  898. goto errout;
  899. }
  900. if (!buffer_uptodate(bh)) {
  901. memset(bh->b_data, 0, inode->i_sb->s_blocksize);
  902. set_buffer_uptodate(bh);
  903. }
  904. unlock_buffer(bh);
  905. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  906. err = ext4_handle_dirty_metadata(handle, inode, bh);
  907. if (unlikely(err))
  908. goto errout;
  909. } else
  910. BUFFER_TRACE(bh, "not a new buffer");
  911. return bh;
  912. errout:
  913. brelse(bh);
  914. return ERR_PTR(err);
  915. }
  916. struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
  917. ext4_lblk_t block, int map_flags)
  918. {
  919. struct buffer_head *bh;
  920. int ret;
  921. bh = ext4_getblk(handle, inode, block, map_flags);
  922. if (IS_ERR(bh))
  923. return bh;
  924. if (!bh || ext4_buffer_uptodate(bh))
  925. return bh;
  926. ret = ext4_read_bh_lock(bh, REQ_META | REQ_PRIO, true);
  927. if (ret) {
  928. put_bh(bh);
  929. return ERR_PTR(ret);
  930. }
  931. return bh;
  932. }
  933. /* Read a contiguous batch of blocks. */
  934. int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count,
  935. bool wait, struct buffer_head **bhs)
  936. {
  937. int i, err;
  938. for (i = 0; i < bh_count; i++) {
  939. bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */);
  940. if (IS_ERR(bhs[i])) {
  941. err = PTR_ERR(bhs[i]);
  942. bh_count = i;
  943. goto out_brelse;
  944. }
  945. }
  946. for (i = 0; i < bh_count; i++)
  947. /* Note that NULL bhs[i] is valid because of holes. */
  948. if (bhs[i] && !ext4_buffer_uptodate(bhs[i]))
  949. ext4_read_bh_lock(bhs[i], REQ_META | REQ_PRIO, false);
  950. if (!wait)
  951. return 0;
  952. for (i = 0; i < bh_count; i++)
  953. if (bhs[i])
  954. wait_on_buffer(bhs[i]);
  955. for (i = 0; i < bh_count; i++) {
  956. if (bhs[i] && !buffer_uptodate(bhs[i])) {
  957. err = -EIO;
  958. goto out_brelse;
  959. }
  960. }
  961. return 0;
  962. out_brelse:
  963. for (i = 0; i < bh_count; i++) {
  964. brelse(bhs[i]);
  965. bhs[i] = NULL;
  966. }
  967. return err;
  968. }
  969. int ext4_walk_page_buffers(handle_t *handle, struct inode *inode,
  970. struct buffer_head *head,
  971. unsigned from,
  972. unsigned to,
  973. int *partial,
  974. int (*fn)(handle_t *handle, struct inode *inode,
  975. struct buffer_head *bh))
  976. {
  977. struct buffer_head *bh;
  978. unsigned block_start, block_end;
  979. unsigned blocksize = head->b_size;
  980. int err, ret = 0;
  981. struct buffer_head *next;
  982. for (bh = head, block_start = 0;
  983. ret == 0 && (bh != head || !block_start);
  984. block_start = block_end, bh = next) {
  985. next = bh->b_this_page;
  986. block_end = block_start + blocksize;
  987. if (block_end <= from || block_start >= to) {
  988. if (partial && !buffer_uptodate(bh))
  989. *partial = 1;
  990. continue;
  991. }
  992. err = (*fn)(handle, inode, bh);
  993. if (!ret)
  994. ret = err;
  995. }
  996. return ret;
  997. }
  998. /*
  999. * Helper for handling dirtying of journalled data. We also mark the folio as
  1000. * dirty so that writeback code knows about this page (and inode) contains
  1001. * dirty data. ext4_writepages() then commits appropriate transaction to
  1002. * make data stable.
  1003. */
  1004. static int ext4_dirty_journalled_data(handle_t *handle, struct buffer_head *bh)
  1005. {
  1006. struct folio *folio = bh->b_folio;
  1007. struct inode *inode = folio->mapping->host;
  1008. /* only regular files have a_ops */
  1009. if (S_ISREG(inode->i_mode))
  1010. folio_mark_dirty(folio);
  1011. return ext4_handle_dirty_metadata(handle, NULL, bh);
  1012. }
  1013. int do_journal_get_write_access(handle_t *handle, struct inode *inode,
  1014. struct buffer_head *bh)
  1015. {
  1016. if (!buffer_mapped(bh) || buffer_freed(bh))
  1017. return 0;
  1018. BUFFER_TRACE(bh, "get write access");
  1019. return ext4_journal_get_write_access(handle, inode->i_sb, bh,
  1020. EXT4_JTR_NONE);
  1021. }
  1022. int ext4_block_write_begin(handle_t *handle, struct folio *folio,
  1023. loff_t pos, unsigned len,
  1024. get_block_t *get_block)
  1025. {
  1026. unsigned int from = offset_in_folio(folio, pos);
  1027. unsigned to = from + len;
  1028. struct inode *inode = folio->mapping->host;
  1029. unsigned block_start, block_end;
  1030. sector_t block;
  1031. int err = 0;
  1032. unsigned int blocksize = i_blocksize(inode);
  1033. struct buffer_head *bh, *head, *wait[2];
  1034. int nr_wait = 0;
  1035. int i;
  1036. bool should_journal_data = ext4_should_journal_data(inode);
  1037. BUG_ON(!folio_test_locked(folio));
  1038. BUG_ON(to > folio_size(folio));
  1039. BUG_ON(from > to);
  1040. WARN_ON_ONCE(blocksize > folio_size(folio));
  1041. head = folio_buffers(folio);
  1042. if (!head)
  1043. head = create_empty_buffers(folio, blocksize, 0);
  1044. block = EXT4_PG_TO_LBLK(inode, folio->index);
  1045. for (bh = head, block_start = 0; bh != head || !block_start;
  1046. block++, block_start = block_end, bh = bh->b_this_page) {
  1047. block_end = block_start + blocksize;
  1048. if (block_end <= from || block_start >= to) {
  1049. if (folio_test_uptodate(folio)) {
  1050. set_buffer_uptodate(bh);
  1051. }
  1052. continue;
  1053. }
  1054. if (WARN_ON_ONCE(buffer_new(bh)))
  1055. clear_buffer_new(bh);
  1056. if (!buffer_mapped(bh)) {
  1057. WARN_ON(bh->b_size != blocksize);
  1058. err = ext4_journal_ensure_extent_credits(handle, inode);
  1059. if (!err)
  1060. err = get_block(inode, block, bh, 1);
  1061. if (err)
  1062. break;
  1063. if (buffer_new(bh)) {
  1064. /*
  1065. * We may be zeroing partial buffers or all new
  1066. * buffers in case of failure. Prepare JBD2 for
  1067. * that.
  1068. */
  1069. if (should_journal_data)
  1070. do_journal_get_write_access(handle,
  1071. inode, bh);
  1072. if (folio_test_uptodate(folio)) {
  1073. /*
  1074. * Unlike __block_write_begin() we leave
  1075. * dirtying of new uptodate buffers to
  1076. * ->write_end() time or
  1077. * folio_zero_new_buffers().
  1078. */
  1079. set_buffer_uptodate(bh);
  1080. continue;
  1081. }
  1082. if (block_end > to || block_start < from)
  1083. folio_zero_segments(folio, to,
  1084. block_end,
  1085. block_start, from);
  1086. continue;
  1087. }
  1088. }
  1089. if (folio_test_uptodate(folio)) {
  1090. set_buffer_uptodate(bh);
  1091. continue;
  1092. }
  1093. if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
  1094. !buffer_unwritten(bh) &&
  1095. (block_start < from || block_end > to)) {
  1096. ext4_read_bh_lock(bh, 0, false);
  1097. wait[nr_wait++] = bh;
  1098. }
  1099. }
  1100. /*
  1101. * If we issued read requests, let them complete.
  1102. */
  1103. for (i = 0; i < nr_wait; i++) {
  1104. wait_on_buffer(wait[i]);
  1105. if (!buffer_uptodate(wait[i]))
  1106. err = -EIO;
  1107. }
  1108. if (unlikely(err)) {
  1109. if (should_journal_data)
  1110. ext4_journalled_zero_new_buffers(handle, inode, folio,
  1111. from, to);
  1112. else
  1113. folio_zero_new_buffers(folio, from, to);
  1114. } else if (fscrypt_inode_uses_fs_layer_crypto(inode)) {
  1115. for (i = 0; i < nr_wait; i++) {
  1116. int err2;
  1117. err2 = fscrypt_decrypt_pagecache_blocks(folio,
  1118. blocksize, bh_offset(wait[i]));
  1119. if (err2) {
  1120. clear_buffer_uptodate(wait[i]);
  1121. err = err2;
  1122. }
  1123. }
  1124. }
  1125. return err;
  1126. }
  1127. /*
  1128. * To preserve ordering, it is essential that the hole instantiation and
  1129. * the data write be encapsulated in a single transaction. We cannot
  1130. * close off a transaction and start a new one between the ext4_get_block()
  1131. * and the ext4_write_end(). So doing the jbd2_journal_start at the start of
  1132. * ext4_write_begin() is the right place.
  1133. */
  1134. static int ext4_write_begin(const struct kiocb *iocb,
  1135. struct address_space *mapping,
  1136. loff_t pos, unsigned len,
  1137. struct folio **foliop, void **fsdata)
  1138. {
  1139. struct inode *inode = mapping->host;
  1140. int ret, needed_blocks;
  1141. handle_t *handle;
  1142. int retries = 0;
  1143. struct folio *folio;
  1144. pgoff_t index;
  1145. unsigned from, to;
  1146. ret = ext4_emergency_state(inode->i_sb);
  1147. if (unlikely(ret))
  1148. return ret;
  1149. trace_ext4_write_begin(inode, pos, len);
  1150. /*
  1151. * Reserve one block more for addition to orphan list in case
  1152. * we allocate blocks but write fails for some reason
  1153. */
  1154. needed_blocks = ext4_chunk_trans_extent(inode,
  1155. ext4_journal_blocks_per_folio(inode)) + 1;
  1156. index = pos >> PAGE_SHIFT;
  1157. if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
  1158. ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
  1159. foliop);
  1160. if (ret < 0)
  1161. return ret;
  1162. if (ret == 1)
  1163. return 0;
  1164. }
  1165. /*
  1166. * write_begin_get_folio() can take a long time if the
  1167. * system is thrashing due to memory pressure, or if the folio
  1168. * is being written back. So grab it first before we start
  1169. * the transaction handle. This also allows us to allocate
  1170. * the folio (if needed) without using GFP_NOFS.
  1171. */
  1172. retry_grab:
  1173. folio = write_begin_get_folio(iocb, mapping, index, len);
  1174. if (IS_ERR(folio))
  1175. return PTR_ERR(folio);
  1176. if (len > folio_next_pos(folio) - pos)
  1177. len = folio_next_pos(folio) - pos;
  1178. from = offset_in_folio(folio, pos);
  1179. to = from + len;
  1180. /*
  1181. * The same as page allocation, we prealloc buffer heads before
  1182. * starting the handle.
  1183. */
  1184. if (!folio_buffers(folio))
  1185. create_empty_buffers(folio, inode->i_sb->s_blocksize, 0);
  1186. folio_unlock(folio);
  1187. retry_journal:
  1188. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
  1189. if (IS_ERR(handle)) {
  1190. folio_put(folio);
  1191. return PTR_ERR(handle);
  1192. }
  1193. folio_lock(folio);
  1194. if (folio->mapping != mapping) {
  1195. /* The folio got truncated from under us */
  1196. folio_unlock(folio);
  1197. folio_put(folio);
  1198. ext4_journal_stop(handle);
  1199. goto retry_grab;
  1200. }
  1201. /* In case writeback began while the folio was unlocked */
  1202. folio_wait_stable(folio);
  1203. if (ext4_should_dioread_nolock(inode))
  1204. ret = ext4_block_write_begin(handle, folio, pos, len,
  1205. ext4_get_block_unwritten);
  1206. else
  1207. ret = ext4_block_write_begin(handle, folio, pos, len,
  1208. ext4_get_block);
  1209. if (!ret && ext4_should_journal_data(inode)) {
  1210. ret = ext4_walk_page_buffers(handle, inode,
  1211. folio_buffers(folio), from, to,
  1212. NULL, do_journal_get_write_access);
  1213. }
  1214. if (ret) {
  1215. bool extended = (pos + len > inode->i_size) &&
  1216. !ext4_verity_in_progress(inode);
  1217. folio_unlock(folio);
  1218. /*
  1219. * ext4_block_write_begin may have instantiated a few blocks
  1220. * outside i_size. Trim these off again. Don't need
  1221. * i_size_read because we hold i_rwsem.
  1222. *
  1223. * Add inode to orphan list in case we crash before
  1224. * truncate finishes
  1225. */
  1226. if (extended && ext4_can_truncate(inode))
  1227. ext4_orphan_add(handle, inode);
  1228. ext4_journal_stop(handle);
  1229. if (extended) {
  1230. ext4_truncate_failed_write(inode);
  1231. /*
  1232. * If truncate failed early the inode might
  1233. * still be on the orphan list; we need to
  1234. * make sure the inode is removed from the
  1235. * orphan list in that case.
  1236. */
  1237. if (inode->i_nlink)
  1238. ext4_orphan_del(NULL, inode);
  1239. }
  1240. if (ret == -EAGAIN ||
  1241. (ret == -ENOSPC &&
  1242. ext4_should_retry_alloc(inode->i_sb, &retries)))
  1243. goto retry_journal;
  1244. folio_put(folio);
  1245. return ret;
  1246. }
  1247. *foliop = folio;
  1248. return ret;
  1249. }
  1250. /* For write_end() in data=journal mode */
  1251. static int write_end_fn(handle_t *handle, struct inode *inode,
  1252. struct buffer_head *bh)
  1253. {
  1254. int ret;
  1255. if (!buffer_mapped(bh) || buffer_freed(bh))
  1256. return 0;
  1257. set_buffer_uptodate(bh);
  1258. ret = ext4_dirty_journalled_data(handle, bh);
  1259. clear_buffer_meta(bh);
  1260. clear_buffer_prio(bh);
  1261. clear_buffer_new(bh);
  1262. return ret;
  1263. }
  1264. /*
  1265. * We need to pick up the new inode size which generic_commit_write gave us
  1266. * `iocb` can be NULL - eg, when called from page_symlink().
  1267. *
  1268. * ext4 never places buffers on inode->i_mapping->i_private_list. metadata
  1269. * buffers are managed internally.
  1270. */
  1271. static int ext4_write_end(const struct kiocb *iocb,
  1272. struct address_space *mapping,
  1273. loff_t pos, unsigned len, unsigned copied,
  1274. struct folio *folio, void *fsdata)
  1275. {
  1276. handle_t *handle = ext4_journal_current_handle();
  1277. struct inode *inode = mapping->host;
  1278. loff_t old_size = inode->i_size;
  1279. int ret = 0, ret2;
  1280. int i_size_changed = 0;
  1281. bool verity = ext4_verity_in_progress(inode);
  1282. trace_ext4_write_end(inode, pos, len, copied);
  1283. if (ext4_has_inline_data(inode) &&
  1284. ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
  1285. return ext4_write_inline_data_end(inode, pos, len, copied,
  1286. folio);
  1287. copied = block_write_end(pos, len, copied, folio);
  1288. /*
  1289. * it's important to update i_size while still holding folio lock:
  1290. * page writeout could otherwise come in and zero beyond i_size.
  1291. *
  1292. * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree
  1293. * blocks are being written past EOF, so skip the i_size update.
  1294. */
  1295. if (!verity)
  1296. i_size_changed = ext4_update_inode_size(inode, pos + copied);
  1297. folio_unlock(folio);
  1298. folio_put(folio);
  1299. if (old_size < pos && !verity) {
  1300. pagecache_isize_extended(inode, old_size, pos);
  1301. ext4_zero_partial_blocks(handle, inode, old_size, pos - old_size);
  1302. }
  1303. /*
  1304. * Don't mark the inode dirty under folio lock. First, it unnecessarily
  1305. * makes the holding time of folio lock longer. Second, it forces lock
  1306. * ordering of folio lock and transaction start for journaling
  1307. * filesystems.
  1308. */
  1309. if (i_size_changed)
  1310. ret = ext4_mark_inode_dirty(handle, inode);
  1311. if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
  1312. /* if we have allocated more blocks and copied
  1313. * less. We will have blocks allocated outside
  1314. * inode->i_size. So truncate them
  1315. */
  1316. ext4_orphan_add(handle, inode);
  1317. ret2 = ext4_journal_stop(handle);
  1318. if (!ret)
  1319. ret = ret2;
  1320. if (pos + len > inode->i_size && !verity) {
  1321. ext4_truncate_failed_write(inode);
  1322. /*
  1323. * If truncate failed early the inode might still be
  1324. * on the orphan list; we need to make sure the inode
  1325. * is removed from the orphan list in that case.
  1326. */
  1327. if (inode->i_nlink)
  1328. ext4_orphan_del(NULL, inode);
  1329. }
  1330. return ret ? ret : copied;
  1331. }
  1332. /*
  1333. * This is a private version of folio_zero_new_buffers() which doesn't
  1334. * set the buffer to be dirty, since in data=journalled mode we need
  1335. * to call ext4_dirty_journalled_data() instead.
  1336. */
  1337. static void ext4_journalled_zero_new_buffers(handle_t *handle,
  1338. struct inode *inode,
  1339. struct folio *folio,
  1340. unsigned from, unsigned to)
  1341. {
  1342. unsigned int block_start = 0, block_end;
  1343. struct buffer_head *head, *bh;
  1344. bh = head = folio_buffers(folio);
  1345. do {
  1346. block_end = block_start + bh->b_size;
  1347. if (buffer_new(bh)) {
  1348. if (block_end > from && block_start < to) {
  1349. if (!folio_test_uptodate(folio)) {
  1350. unsigned start, size;
  1351. start = max(from, block_start);
  1352. size = min(to, block_end) - start;
  1353. folio_zero_range(folio, start, size);
  1354. }
  1355. clear_buffer_new(bh);
  1356. write_end_fn(handle, inode, bh);
  1357. }
  1358. }
  1359. block_start = block_end;
  1360. bh = bh->b_this_page;
  1361. } while (bh != head);
  1362. }
  1363. static int ext4_journalled_write_end(const struct kiocb *iocb,
  1364. struct address_space *mapping,
  1365. loff_t pos, unsigned len, unsigned copied,
  1366. struct folio *folio, void *fsdata)
  1367. {
  1368. handle_t *handle = ext4_journal_current_handle();
  1369. struct inode *inode = mapping->host;
  1370. loff_t old_size = inode->i_size;
  1371. int ret = 0, ret2;
  1372. int partial = 0;
  1373. unsigned from, to;
  1374. int size_changed = 0;
  1375. bool verity = ext4_verity_in_progress(inode);
  1376. trace_ext4_journalled_write_end(inode, pos, len, copied);
  1377. from = pos & (PAGE_SIZE - 1);
  1378. to = from + len;
  1379. BUG_ON(!ext4_handle_valid(handle));
  1380. if (ext4_has_inline_data(inode))
  1381. return ext4_write_inline_data_end(inode, pos, len, copied,
  1382. folio);
  1383. if (unlikely(copied < len) && !folio_test_uptodate(folio)) {
  1384. copied = 0;
  1385. ext4_journalled_zero_new_buffers(handle, inode, folio,
  1386. from, to);
  1387. } else {
  1388. if (unlikely(copied < len))
  1389. ext4_journalled_zero_new_buffers(handle, inode, folio,
  1390. from + copied, to);
  1391. ret = ext4_walk_page_buffers(handle, inode,
  1392. folio_buffers(folio),
  1393. from, from + copied, &partial,
  1394. write_end_fn);
  1395. if (!partial)
  1396. folio_mark_uptodate(folio);
  1397. }
  1398. if (!verity)
  1399. size_changed = ext4_update_inode_size(inode, pos + copied);
  1400. EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
  1401. folio_unlock(folio);
  1402. folio_put(folio);
  1403. if (old_size < pos && !verity) {
  1404. pagecache_isize_extended(inode, old_size, pos);
  1405. ext4_zero_partial_blocks(handle, inode, old_size, pos - old_size);
  1406. }
  1407. if (size_changed) {
  1408. ret2 = ext4_mark_inode_dirty(handle, inode);
  1409. if (!ret)
  1410. ret = ret2;
  1411. }
  1412. if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
  1413. /* if we have allocated more blocks and copied
  1414. * less. We will have blocks allocated outside
  1415. * inode->i_size. So truncate them
  1416. */
  1417. ext4_orphan_add(handle, inode);
  1418. ret2 = ext4_journal_stop(handle);
  1419. if (!ret)
  1420. ret = ret2;
  1421. if (pos + len > inode->i_size && !verity) {
  1422. ext4_truncate_failed_write(inode);
  1423. /*
  1424. * If truncate failed early the inode might still be
  1425. * on the orphan list; we need to make sure the inode
  1426. * is removed from the orphan list in that case.
  1427. */
  1428. if (inode->i_nlink)
  1429. ext4_orphan_del(NULL, inode);
  1430. }
  1431. return ret ? ret : copied;
  1432. }
  1433. /*
  1434. * Reserve space for 'nr_resv' clusters
  1435. */
  1436. static int ext4_da_reserve_space(struct inode *inode, int nr_resv)
  1437. {
  1438. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1439. struct ext4_inode_info *ei = EXT4_I(inode);
  1440. int ret;
  1441. /*
  1442. * We will charge metadata quota at writeout time; this saves
  1443. * us from metadata over-estimation, though we may go over by
  1444. * a small amount in the end. Here we just reserve for data.
  1445. */
  1446. ret = dquot_reserve_block(inode, EXT4_C2B(sbi, nr_resv));
  1447. if (ret)
  1448. return ret;
  1449. spin_lock(&ei->i_block_reservation_lock);
  1450. if (ext4_claim_free_clusters(sbi, nr_resv, 0)) {
  1451. spin_unlock(&ei->i_block_reservation_lock);
  1452. dquot_release_reservation_block(inode, EXT4_C2B(sbi, nr_resv));
  1453. return -ENOSPC;
  1454. }
  1455. ei->i_reserved_data_blocks += nr_resv;
  1456. trace_ext4_da_reserve_space(inode, nr_resv);
  1457. spin_unlock(&ei->i_block_reservation_lock);
  1458. return 0; /* success */
  1459. }
  1460. void ext4_da_release_space(struct inode *inode, int to_free)
  1461. {
  1462. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1463. struct ext4_inode_info *ei = EXT4_I(inode);
  1464. if (!to_free)
  1465. return; /* Nothing to release, exit */
  1466. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  1467. trace_ext4_da_release_space(inode, to_free);
  1468. if (unlikely(to_free > ei->i_reserved_data_blocks)) {
  1469. /*
  1470. * if there aren't enough reserved blocks, then the
  1471. * counter is messed up somewhere. Since this
  1472. * function is called from invalidate page, it's
  1473. * harmless to return without any action.
  1474. */
  1475. ext4_warning(inode->i_sb, "ext4_da_release_space: "
  1476. "ino %lu, to_free %d with only %d reserved "
  1477. "data blocks", inode->i_ino, to_free,
  1478. ei->i_reserved_data_blocks);
  1479. WARN_ON(1);
  1480. to_free = ei->i_reserved_data_blocks;
  1481. }
  1482. ei->i_reserved_data_blocks -= to_free;
  1483. /* update fs dirty data blocks counter */
  1484. percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
  1485. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1486. dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
  1487. }
  1488. /*
  1489. * Delayed allocation stuff
  1490. */
  1491. struct mpage_da_data {
  1492. /* These are input fields for ext4_do_writepages() */
  1493. struct inode *inode;
  1494. struct writeback_control *wbc;
  1495. unsigned int can_map:1; /* Can writepages call map blocks? */
  1496. /* These are internal state of ext4_do_writepages() */
  1497. loff_t start_pos; /* The start pos to write */
  1498. loff_t next_pos; /* Current pos to examine */
  1499. loff_t end_pos; /* Last pos to examine */
  1500. /*
  1501. * Extent to map - this can be after start_pos because that can be
  1502. * fully mapped. We somewhat abuse m_flags to store whether the extent
  1503. * is delalloc or unwritten.
  1504. */
  1505. struct ext4_map_blocks map;
  1506. struct ext4_io_submit io_submit; /* IO submission data */
  1507. unsigned int do_map:1;
  1508. unsigned int scanned_until_end:1;
  1509. unsigned int journalled_more_data:1;
  1510. };
  1511. static void mpage_release_unused_pages(struct mpage_da_data *mpd,
  1512. bool invalidate)
  1513. {
  1514. unsigned nr, i;
  1515. pgoff_t index, end;
  1516. struct folio_batch fbatch;
  1517. struct inode *inode = mpd->inode;
  1518. struct address_space *mapping = inode->i_mapping;
  1519. /* This is necessary when next_pos == 0. */
  1520. if (mpd->start_pos >= mpd->next_pos)
  1521. return;
  1522. mpd->scanned_until_end = 0;
  1523. if (invalidate) {
  1524. ext4_lblk_t start, last;
  1525. start = EXT4_B_TO_LBLK(inode, mpd->start_pos);
  1526. last = mpd->next_pos >> inode->i_blkbits;
  1527. /*
  1528. * avoid racing with extent status tree scans made by
  1529. * ext4_insert_delayed_block()
  1530. */
  1531. down_write(&EXT4_I(inode)->i_data_sem);
  1532. ext4_es_remove_extent(inode, start, last - start);
  1533. up_write(&EXT4_I(inode)->i_data_sem);
  1534. }
  1535. folio_batch_init(&fbatch);
  1536. index = mpd->start_pos >> PAGE_SHIFT;
  1537. end = mpd->next_pos >> PAGE_SHIFT;
  1538. while (index < end) {
  1539. nr = filemap_get_folios(mapping, &index, end - 1, &fbatch);
  1540. if (nr == 0)
  1541. break;
  1542. for (i = 0; i < nr; i++) {
  1543. struct folio *folio = fbatch.folios[i];
  1544. if (folio_pos(folio) < mpd->start_pos)
  1545. continue;
  1546. if (folio_next_index(folio) > end)
  1547. continue;
  1548. BUG_ON(!folio_test_locked(folio));
  1549. BUG_ON(folio_test_writeback(folio));
  1550. if (invalidate) {
  1551. if (folio_mapped(folio))
  1552. folio_clear_dirty_for_io(folio);
  1553. block_invalidate_folio(folio, 0,
  1554. folio_size(folio));
  1555. folio_clear_uptodate(folio);
  1556. }
  1557. folio_unlock(folio);
  1558. }
  1559. folio_batch_release(&fbatch);
  1560. }
  1561. }
  1562. static void ext4_print_free_blocks(struct inode *inode)
  1563. {
  1564. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1565. struct super_block *sb = inode->i_sb;
  1566. struct ext4_inode_info *ei = EXT4_I(inode);
  1567. ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
  1568. EXT4_C2B(EXT4_SB(inode->i_sb),
  1569. ext4_count_free_clusters(sb)));
  1570. ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
  1571. ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
  1572. (long long) EXT4_C2B(EXT4_SB(sb),
  1573. percpu_counter_sum(&sbi->s_freeclusters_counter)));
  1574. ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
  1575. (long long) EXT4_C2B(EXT4_SB(sb),
  1576. percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
  1577. ext4_msg(sb, KERN_CRIT, "Block reservation details");
  1578. ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
  1579. ei->i_reserved_data_blocks);
  1580. return;
  1581. }
  1582. /*
  1583. * Check whether the cluster containing lblk has been allocated or has
  1584. * delalloc reservation.
  1585. *
  1586. * Returns 0 if the cluster doesn't have either, 1 if it has delalloc
  1587. * reservation, 2 if it's already been allocated, negative error code on
  1588. * failure.
  1589. */
  1590. static int ext4_clu_alloc_state(struct inode *inode, ext4_lblk_t lblk)
  1591. {
  1592. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1593. int ret;
  1594. /* Has delalloc reservation? */
  1595. if (ext4_es_scan_clu(inode, &ext4_es_is_delayed, lblk))
  1596. return 1;
  1597. /* Already been allocated? */
  1598. if (ext4_es_scan_clu(inode, &ext4_es_is_mapped, lblk))
  1599. return 2;
  1600. ret = ext4_clu_mapped(inode, EXT4_B2C(sbi, lblk));
  1601. if (ret < 0)
  1602. return ret;
  1603. if (ret > 0)
  1604. return 2;
  1605. return 0;
  1606. }
  1607. /*
  1608. * ext4_insert_delayed_blocks - adds a multiple delayed blocks to the extents
  1609. * status tree, incrementing the reserved
  1610. * cluster/block count or making pending
  1611. * reservations where needed
  1612. *
  1613. * @inode - file containing the newly added block
  1614. * @lblk - start logical block to be added
  1615. * @len - length of blocks to be added
  1616. *
  1617. * Returns 0 on success, negative error code on failure.
  1618. */
  1619. static int ext4_insert_delayed_blocks(struct inode *inode, ext4_lblk_t lblk,
  1620. ext4_lblk_t len)
  1621. {
  1622. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1623. int ret;
  1624. bool lclu_allocated = false;
  1625. bool end_allocated = false;
  1626. ext4_lblk_t resv_clu;
  1627. ext4_lblk_t end = lblk + len - 1;
  1628. /*
  1629. * If the cluster containing lblk or end is shared with a delayed,
  1630. * written, or unwritten extent in a bigalloc file system, it's
  1631. * already been accounted for and does not need to be reserved.
  1632. * A pending reservation must be made for the cluster if it's
  1633. * shared with a written or unwritten extent and doesn't already
  1634. * have one. Written and unwritten extents can be purged from the
  1635. * extents status tree if the system is under memory pressure, so
  1636. * it's necessary to examine the extent tree if a search of the
  1637. * extents status tree doesn't get a match.
  1638. */
  1639. if (sbi->s_cluster_ratio == 1) {
  1640. ret = ext4_da_reserve_space(inode, len);
  1641. if (ret != 0) /* ENOSPC */
  1642. return ret;
  1643. } else { /* bigalloc */
  1644. resv_clu = EXT4_B2C(sbi, end) - EXT4_B2C(sbi, lblk) + 1;
  1645. ret = ext4_clu_alloc_state(inode, lblk);
  1646. if (ret < 0)
  1647. return ret;
  1648. if (ret > 0) {
  1649. resv_clu--;
  1650. lclu_allocated = (ret == 2);
  1651. }
  1652. if (EXT4_B2C(sbi, lblk) != EXT4_B2C(sbi, end)) {
  1653. ret = ext4_clu_alloc_state(inode, end);
  1654. if (ret < 0)
  1655. return ret;
  1656. if (ret > 0) {
  1657. resv_clu--;
  1658. end_allocated = (ret == 2);
  1659. }
  1660. }
  1661. if (resv_clu) {
  1662. ret = ext4_da_reserve_space(inode, resv_clu);
  1663. if (ret != 0) /* ENOSPC */
  1664. return ret;
  1665. }
  1666. }
  1667. ext4_es_insert_delayed_extent(inode, lblk, len, lclu_allocated,
  1668. end_allocated);
  1669. return 0;
  1670. }
  1671. /*
  1672. * Looks up the requested blocks and sets the delalloc extent map.
  1673. * First try to look up for the extent entry that contains the requested
  1674. * blocks in the extent status tree without i_data_sem, then try to look
  1675. * up for the ondisk extent mapping with i_data_sem in read mode,
  1676. * finally hold i_data_sem in write mode, looks up again and add a
  1677. * delalloc extent entry if it still couldn't find any extent. Pass out
  1678. * the mapped extent through @map and return 0 on success.
  1679. */
  1680. static int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map)
  1681. {
  1682. struct extent_status es;
  1683. int retval;
  1684. #ifdef ES_AGGRESSIVE_TEST
  1685. struct ext4_map_blocks orig_map;
  1686. memcpy(&orig_map, map, sizeof(*map));
  1687. #endif
  1688. map->m_flags = 0;
  1689. ext_debug(inode, "max_blocks %u, logical block %lu\n", map->m_len,
  1690. (unsigned long) map->m_lblk);
  1691. ext4_check_map_extents_env(inode);
  1692. /* Lookup extent status tree firstly */
  1693. if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es, NULL)) {
  1694. map->m_len = min_t(unsigned int, map->m_len,
  1695. es.es_len - (map->m_lblk - es.es_lblk));
  1696. if (ext4_es_is_hole(&es))
  1697. goto add_delayed;
  1698. found:
  1699. /*
  1700. * Delayed extent could be allocated by fallocate.
  1701. * So we need to check it.
  1702. */
  1703. if (ext4_es_is_delayed(&es)) {
  1704. map->m_flags |= EXT4_MAP_DELAYED;
  1705. return 0;
  1706. }
  1707. map->m_pblk = ext4_es_pblock(&es) + map->m_lblk - es.es_lblk;
  1708. if (ext4_es_is_written(&es))
  1709. map->m_flags |= EXT4_MAP_MAPPED;
  1710. else if (ext4_es_is_unwritten(&es))
  1711. map->m_flags |= EXT4_MAP_UNWRITTEN;
  1712. else
  1713. BUG();
  1714. #ifdef ES_AGGRESSIVE_TEST
  1715. ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
  1716. #endif
  1717. return 0;
  1718. }
  1719. /*
  1720. * Try to see if we can get the block without requesting a new
  1721. * file system block.
  1722. */
  1723. down_read(&EXT4_I(inode)->i_data_sem);
  1724. if (ext4_has_inline_data(inode))
  1725. retval = 0;
  1726. else
  1727. retval = ext4_map_query_blocks(NULL, inode, map, 0);
  1728. up_read(&EXT4_I(inode)->i_data_sem);
  1729. if (retval)
  1730. return retval < 0 ? retval : 0;
  1731. add_delayed:
  1732. down_write(&EXT4_I(inode)->i_data_sem);
  1733. /*
  1734. * Page fault path (ext4_page_mkwrite does not take i_rwsem)
  1735. * and fallocate path (no folio lock) can race. Make sure we
  1736. * lookup the extent status tree here again while i_data_sem
  1737. * is held in write mode, before inserting a new da entry in
  1738. * the extent status tree.
  1739. */
  1740. if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es, NULL)) {
  1741. map->m_len = min_t(unsigned int, map->m_len,
  1742. es.es_len - (map->m_lblk - es.es_lblk));
  1743. if (!ext4_es_is_hole(&es)) {
  1744. up_write(&EXT4_I(inode)->i_data_sem);
  1745. goto found;
  1746. }
  1747. } else if (!ext4_has_inline_data(inode)) {
  1748. retval = ext4_map_query_blocks(NULL, inode, map, 0);
  1749. if (retval) {
  1750. up_write(&EXT4_I(inode)->i_data_sem);
  1751. return retval < 0 ? retval : 0;
  1752. }
  1753. }
  1754. map->m_flags |= EXT4_MAP_DELAYED;
  1755. retval = ext4_insert_delayed_blocks(inode, map->m_lblk, map->m_len);
  1756. if (!retval)
  1757. map->m_seq = READ_ONCE(EXT4_I(inode)->i_es_seq);
  1758. up_write(&EXT4_I(inode)->i_data_sem);
  1759. return retval;
  1760. }
  1761. /*
  1762. * This is a special get_block_t callback which is used by
  1763. * ext4_da_write_begin(). It will either return mapped block or
  1764. * reserve space for a single block.
  1765. *
  1766. * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
  1767. * We also have b_blocknr = -1 and b_bdev initialized properly
  1768. *
  1769. * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
  1770. * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
  1771. * initialized properly.
  1772. */
  1773. int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
  1774. struct buffer_head *bh, int create)
  1775. {
  1776. struct ext4_map_blocks map;
  1777. sector_t invalid_block = ~((sector_t) 0xffff);
  1778. int ret = 0;
  1779. BUG_ON(create == 0);
  1780. BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
  1781. if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
  1782. invalid_block = ~0;
  1783. map.m_lblk = iblock;
  1784. map.m_len = 1;
  1785. /*
  1786. * first, we need to know whether the block is allocated already
  1787. * preallocated blocks are unmapped but should treated
  1788. * the same as allocated blocks.
  1789. */
  1790. ret = ext4_da_map_blocks(inode, &map);
  1791. if (ret < 0)
  1792. return ret;
  1793. if (map.m_flags & EXT4_MAP_DELAYED) {
  1794. map_bh(bh, inode->i_sb, invalid_block);
  1795. set_buffer_new(bh);
  1796. set_buffer_delay(bh);
  1797. return 0;
  1798. }
  1799. map_bh(bh, inode->i_sb, map.m_pblk);
  1800. ext4_update_bh_state(bh, map.m_flags);
  1801. if (buffer_unwritten(bh)) {
  1802. /* A delayed write to unwritten bh should be marked
  1803. * new and mapped. Mapped ensures that we don't do
  1804. * get_block multiple times when we write to the same
  1805. * offset and new ensures that we do proper zero out
  1806. * for partial write.
  1807. */
  1808. set_buffer_new(bh);
  1809. set_buffer_mapped(bh);
  1810. }
  1811. return 0;
  1812. }
  1813. static void mpage_folio_done(struct mpage_da_data *mpd, struct folio *folio)
  1814. {
  1815. mpd->start_pos += folio_size(folio);
  1816. mpd->wbc->nr_to_write -= folio_nr_pages(folio);
  1817. folio_unlock(folio);
  1818. }
  1819. static int mpage_submit_folio(struct mpage_da_data *mpd, struct folio *folio)
  1820. {
  1821. size_t len;
  1822. loff_t size;
  1823. int err;
  1824. WARN_ON_ONCE(folio_pos(folio) != mpd->start_pos);
  1825. folio_clear_dirty_for_io(folio);
  1826. /*
  1827. * We have to be very careful here! Nothing protects writeback path
  1828. * against i_size changes and the page can be writeably mapped into
  1829. * page tables. So an application can be growing i_size and writing
  1830. * data through mmap while writeback runs. folio_clear_dirty_for_io()
  1831. * write-protects our page in page tables and the page cannot get
  1832. * written to again until we release folio lock. So only after
  1833. * folio_clear_dirty_for_io() we are safe to sample i_size for
  1834. * ext4_bio_write_folio() to zero-out tail of the written page. We rely
  1835. * on the barrier provided by folio_test_clear_dirty() in
  1836. * folio_clear_dirty_for_io() to make sure i_size is really sampled only
  1837. * after page tables are updated.
  1838. */
  1839. size = i_size_read(mpd->inode);
  1840. len = folio_size(folio);
  1841. if (folio_pos(folio) + len > size &&
  1842. !ext4_verity_in_progress(mpd->inode))
  1843. len = size & (len - 1);
  1844. err = ext4_bio_write_folio(&mpd->io_submit, folio, len);
  1845. return err;
  1846. }
  1847. #define BH_FLAGS (BIT(BH_Unwritten) | BIT(BH_Delay))
  1848. /*
  1849. * mballoc gives us at most this number of blocks...
  1850. * XXX: That seems to be only a limitation of ext4_mb_normalize_request().
  1851. * The rest of mballoc seems to handle chunks up to full group size.
  1852. */
  1853. #define MAX_WRITEPAGES_EXTENT_LEN 2048
  1854. /*
  1855. * mpage_add_bh_to_extent - try to add bh to extent of blocks to map
  1856. *
  1857. * @mpd - extent of blocks
  1858. * @lblk - logical number of the block in the file
  1859. * @bh - buffer head we want to add to the extent
  1860. *
  1861. * The function is used to collect contig. blocks in the same state. If the
  1862. * buffer doesn't require mapping for writeback and we haven't started the
  1863. * extent of buffers to map yet, the function returns 'true' immediately - the
  1864. * caller can write the buffer right away. Otherwise the function returns true
  1865. * if the block has been added to the extent, false if the block couldn't be
  1866. * added.
  1867. */
  1868. static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk,
  1869. struct buffer_head *bh)
  1870. {
  1871. struct ext4_map_blocks *map = &mpd->map;
  1872. /* Buffer that doesn't need mapping for writeback? */
  1873. if (!buffer_dirty(bh) || !buffer_mapped(bh) ||
  1874. (!buffer_delay(bh) && !buffer_unwritten(bh))) {
  1875. /* So far no extent to map => we write the buffer right away */
  1876. if (map->m_len == 0)
  1877. return true;
  1878. return false;
  1879. }
  1880. /* First block in the extent? */
  1881. if (map->m_len == 0) {
  1882. /* We cannot map unless handle is started... */
  1883. if (!mpd->do_map)
  1884. return false;
  1885. map->m_lblk = lblk;
  1886. map->m_len = 1;
  1887. map->m_flags = bh->b_state & BH_FLAGS;
  1888. return true;
  1889. }
  1890. /* Don't go larger than mballoc is willing to allocate */
  1891. if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN)
  1892. return false;
  1893. /* Can we merge the block to our big extent? */
  1894. if (lblk == map->m_lblk + map->m_len &&
  1895. (bh->b_state & BH_FLAGS) == map->m_flags) {
  1896. map->m_len++;
  1897. return true;
  1898. }
  1899. return false;
  1900. }
  1901. /*
  1902. * mpage_process_page_bufs - submit page buffers for IO or add them to extent
  1903. *
  1904. * @mpd - extent of blocks for mapping
  1905. * @head - the first buffer in the page
  1906. * @bh - buffer we should start processing from
  1907. * @lblk - logical number of the block in the file corresponding to @bh
  1908. *
  1909. * Walk through page buffers from @bh upto @head (exclusive) and either submit
  1910. * the page for IO if all buffers in this page were mapped and there's no
  1911. * accumulated extent of buffers to map or add buffers in the page to the
  1912. * extent of buffers to map. The function returns 1 if the caller can continue
  1913. * by processing the next page, 0 if it should stop adding buffers to the
  1914. * extent to map because we cannot extend it anymore. It can also return value
  1915. * < 0 in case of error during IO submission.
  1916. */
  1917. static int mpage_process_page_bufs(struct mpage_da_data *mpd,
  1918. struct buffer_head *head,
  1919. struct buffer_head *bh,
  1920. ext4_lblk_t lblk)
  1921. {
  1922. struct inode *inode = mpd->inode;
  1923. int err;
  1924. ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1)
  1925. >> inode->i_blkbits;
  1926. if (ext4_verity_in_progress(inode))
  1927. blocks = EXT_MAX_BLOCKS;
  1928. do {
  1929. BUG_ON(buffer_locked(bh));
  1930. if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) {
  1931. /* Found extent to map? */
  1932. if (mpd->map.m_len)
  1933. return 0;
  1934. /* Buffer needs mapping and handle is not started? */
  1935. if (!mpd->do_map)
  1936. return 0;
  1937. /* Everything mapped so far and we hit EOF */
  1938. break;
  1939. }
  1940. } while (lblk++, (bh = bh->b_this_page) != head);
  1941. /* So far everything mapped? Submit the page for IO. */
  1942. if (mpd->map.m_len == 0) {
  1943. err = mpage_submit_folio(mpd, head->b_folio);
  1944. if (err < 0)
  1945. return err;
  1946. mpage_folio_done(mpd, head->b_folio);
  1947. }
  1948. if (lblk >= blocks) {
  1949. mpd->scanned_until_end = 1;
  1950. return 0;
  1951. }
  1952. return 1;
  1953. }
  1954. /*
  1955. * mpage_process_folio - update folio buffers corresponding to changed extent
  1956. * and may submit fully mapped page for IO
  1957. * @mpd: description of extent to map, on return next extent to map
  1958. * @folio: Contains these buffers.
  1959. * @m_lblk: logical block mapping.
  1960. * @m_pblk: corresponding physical mapping.
  1961. * @map_bh: determines on return whether this page requires any further
  1962. * mapping or not.
  1963. *
  1964. * Scan given folio buffers corresponding to changed extent and update buffer
  1965. * state according to new extent state.
  1966. * We map delalloc buffers to their physical location, clear unwritten bits.
  1967. * If the given folio is not fully mapped, we update @mpd to the next extent in
  1968. * the given folio that needs mapping & return @map_bh as true.
  1969. */
  1970. static int mpage_process_folio(struct mpage_da_data *mpd, struct folio *folio,
  1971. ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk,
  1972. bool *map_bh)
  1973. {
  1974. struct buffer_head *head, *bh;
  1975. ext4_io_end_t *io_end = mpd->io_submit.io_end;
  1976. ext4_lblk_t lblk = *m_lblk;
  1977. ext4_fsblk_t pblock = *m_pblk;
  1978. int err = 0;
  1979. ssize_t io_end_size = 0;
  1980. struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end);
  1981. bh = head = folio_buffers(folio);
  1982. do {
  1983. if (lblk < mpd->map.m_lblk)
  1984. continue;
  1985. if (lblk >= mpd->map.m_lblk + mpd->map.m_len) {
  1986. /*
  1987. * Buffer after end of mapped extent.
  1988. * Find next buffer in the folio to map.
  1989. */
  1990. mpd->map.m_len = 0;
  1991. mpd->map.m_flags = 0;
  1992. io_end_vec->size += io_end_size;
  1993. err = mpage_process_page_bufs(mpd, head, bh, lblk);
  1994. if (err > 0)
  1995. err = 0;
  1996. if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) {
  1997. io_end_vec = ext4_alloc_io_end_vec(io_end);
  1998. if (IS_ERR(io_end_vec)) {
  1999. err = PTR_ERR(io_end_vec);
  2000. goto out;
  2001. }
  2002. io_end_vec->offset = EXT4_LBLK_TO_B(mpd->inode,
  2003. mpd->map.m_lblk);
  2004. }
  2005. *map_bh = true;
  2006. goto out;
  2007. }
  2008. if (buffer_delay(bh)) {
  2009. clear_buffer_delay(bh);
  2010. bh->b_blocknr = pblock++;
  2011. }
  2012. clear_buffer_unwritten(bh);
  2013. io_end_size += i_blocksize(mpd->inode);
  2014. } while (lblk++, (bh = bh->b_this_page) != head);
  2015. io_end_vec->size += io_end_size;
  2016. *map_bh = false;
  2017. out:
  2018. *m_lblk = lblk;
  2019. *m_pblk = pblock;
  2020. return err;
  2021. }
  2022. /*
  2023. * mpage_map_buffers - update buffers corresponding to changed extent and
  2024. * submit fully mapped pages for IO
  2025. *
  2026. * @mpd - description of extent to map, on return next extent to map
  2027. *
  2028. * Scan buffers corresponding to changed extent (we expect corresponding pages
  2029. * to be already locked) and update buffer state according to new extent state.
  2030. * We map delalloc buffers to their physical location, clear unwritten bits,
  2031. * and mark buffers as uninit when we perform writes to unwritten extents
  2032. * and do extent conversion after IO is finished. If the last page is not fully
  2033. * mapped, we update @map to the next extent in the last page that needs
  2034. * mapping. Otherwise we submit the page for IO.
  2035. */
  2036. static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd)
  2037. {
  2038. struct folio_batch fbatch;
  2039. unsigned nr, i;
  2040. struct inode *inode = mpd->inode;
  2041. pgoff_t start, end;
  2042. ext4_lblk_t lblk;
  2043. ext4_fsblk_t pblock;
  2044. int err;
  2045. bool map_bh = false;
  2046. start = EXT4_LBLK_TO_PG(inode, mpd->map.m_lblk);
  2047. end = EXT4_LBLK_TO_PG(inode, mpd->map.m_lblk + mpd->map.m_len - 1);
  2048. pblock = mpd->map.m_pblk;
  2049. folio_batch_init(&fbatch);
  2050. while (start <= end) {
  2051. nr = filemap_get_folios(inode->i_mapping, &start, end, &fbatch);
  2052. if (nr == 0)
  2053. break;
  2054. for (i = 0; i < nr; i++) {
  2055. struct folio *folio = fbatch.folios[i];
  2056. lblk = EXT4_PG_TO_LBLK(inode, folio->index);
  2057. err = mpage_process_folio(mpd, folio, &lblk, &pblock,
  2058. &map_bh);
  2059. /*
  2060. * If map_bh is true, means page may require further bh
  2061. * mapping, or maybe the page was submitted for IO.
  2062. * So we return to call further extent mapping.
  2063. */
  2064. if (err < 0 || map_bh)
  2065. goto out;
  2066. /* Page fully mapped - let IO run! */
  2067. err = mpage_submit_folio(mpd, folio);
  2068. if (err < 0)
  2069. goto out;
  2070. mpage_folio_done(mpd, folio);
  2071. }
  2072. folio_batch_release(&fbatch);
  2073. }
  2074. /* Extent fully mapped and matches with page boundary. We are done. */
  2075. mpd->map.m_len = 0;
  2076. mpd->map.m_flags = 0;
  2077. return 0;
  2078. out:
  2079. folio_batch_release(&fbatch);
  2080. return err;
  2081. }
  2082. static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
  2083. {
  2084. struct inode *inode = mpd->inode;
  2085. struct ext4_map_blocks *map = &mpd->map;
  2086. int get_blocks_flags;
  2087. int err, dioread_nolock;
  2088. /* Make sure transaction has enough credits for this extent */
  2089. err = ext4_journal_ensure_extent_credits(handle, inode);
  2090. if (err < 0)
  2091. return err;
  2092. trace_ext4_da_write_pages_extent(inode, map);
  2093. /*
  2094. * Call ext4_map_blocks() to allocate any delayed allocation blocks, or
  2095. * to convert an unwritten extent to be initialized (in the case
  2096. * where we have written into one or more preallocated blocks). It is
  2097. * possible that we're going to need more metadata blocks than
  2098. * previously reserved. However we must not fail because we're in
  2099. * writeback and there is nothing we can do about it so it might result
  2100. * in data loss. So use reserved blocks to allocate metadata if
  2101. * possible. In addition, do not cache any unrelated extents, as it
  2102. * only holds the folio lock but does not hold the i_rwsem or
  2103. * invalidate_lock, which could corrupt the extent status tree.
  2104. */
  2105. get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
  2106. EXT4_GET_BLOCKS_METADATA_NOFAIL |
  2107. EXT4_GET_BLOCKS_IO_SUBMIT |
  2108. EXT4_EX_NOCACHE;
  2109. dioread_nolock = ext4_should_dioread_nolock(inode);
  2110. if (dioread_nolock)
  2111. get_blocks_flags |= EXT4_GET_BLOCKS_UNWRIT_EXT;
  2112. err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
  2113. if (err < 0)
  2114. return err;
  2115. if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) {
  2116. if (!mpd->io_submit.io_end->handle &&
  2117. ext4_handle_valid(handle)) {
  2118. mpd->io_submit.io_end->handle = handle->h_rsv_handle;
  2119. handle->h_rsv_handle = NULL;
  2120. }
  2121. ext4_set_io_unwritten_flag(mpd->io_submit.io_end);
  2122. }
  2123. BUG_ON(map->m_len == 0);
  2124. return 0;
  2125. }
  2126. /*
  2127. * This is used to submit mapped buffers in a single folio that is not fully
  2128. * mapped for various reasons, such as insufficient space or journal credits.
  2129. */
  2130. static int mpage_submit_partial_folio(struct mpage_da_data *mpd)
  2131. {
  2132. struct inode *inode = mpd->inode;
  2133. struct folio *folio;
  2134. loff_t pos;
  2135. int ret;
  2136. folio = filemap_get_folio(inode->i_mapping,
  2137. mpd->start_pos >> PAGE_SHIFT);
  2138. if (IS_ERR(folio))
  2139. return PTR_ERR(folio);
  2140. /*
  2141. * The mapped position should be within the current processing folio
  2142. * but must not be the folio start position.
  2143. */
  2144. pos = ((loff_t)mpd->map.m_lblk) << inode->i_blkbits;
  2145. if (WARN_ON_ONCE((folio_pos(folio) == pos) ||
  2146. !folio_contains(folio, pos >> PAGE_SHIFT)))
  2147. return -EINVAL;
  2148. ret = mpage_submit_folio(mpd, folio);
  2149. if (ret)
  2150. goto out;
  2151. /*
  2152. * Update start_pos to prevent this folio from being released in
  2153. * mpage_release_unused_pages(), it will be reset to the aligned folio
  2154. * pos when this folio is written again in the next round. Additionally,
  2155. * do not update wbc->nr_to_write here, as it will be updated once the
  2156. * entire folio has finished processing.
  2157. */
  2158. mpd->start_pos = pos;
  2159. out:
  2160. folio_unlock(folio);
  2161. folio_put(folio);
  2162. return ret;
  2163. }
  2164. /*
  2165. * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length
  2166. * mpd->len and submit pages underlying it for IO
  2167. *
  2168. * @handle - handle for journal operations
  2169. * @mpd - extent to map
  2170. * @give_up_on_write - we set this to true iff there is a fatal error and there
  2171. * is no hope of writing the data. The caller should discard
  2172. * dirty pages to avoid infinite loops.
  2173. *
  2174. * The function maps extent starting at mpd->lblk of length mpd->len. If it is
  2175. * delayed, blocks are allocated, if it is unwritten, we may need to convert
  2176. * them to initialized or split the described range from larger unwritten
  2177. * extent. Note that we need not map all the described range since allocation
  2178. * can return less blocks or the range is covered by more unwritten extents. We
  2179. * cannot map more because we are limited by reserved transaction credits. On
  2180. * the other hand we always make sure that the last touched page is fully
  2181. * mapped so that it can be written out (and thus forward progress is
  2182. * guaranteed). After mapping we submit all mapped pages for IO.
  2183. */
  2184. static int mpage_map_and_submit_extent(handle_t *handle,
  2185. struct mpage_da_data *mpd,
  2186. bool *give_up_on_write)
  2187. {
  2188. struct inode *inode = mpd->inode;
  2189. struct ext4_map_blocks *map = &mpd->map;
  2190. int err;
  2191. loff_t disksize;
  2192. int progress = 0;
  2193. ext4_io_end_t *io_end = mpd->io_submit.io_end;
  2194. struct ext4_io_end_vec *io_end_vec;
  2195. io_end_vec = ext4_alloc_io_end_vec(io_end);
  2196. if (IS_ERR(io_end_vec))
  2197. return PTR_ERR(io_end_vec);
  2198. io_end_vec->offset = EXT4_LBLK_TO_B(inode, map->m_lblk);
  2199. do {
  2200. err = mpage_map_one_extent(handle, mpd);
  2201. if (err < 0) {
  2202. struct super_block *sb = inode->i_sb;
  2203. if (ext4_emergency_state(sb))
  2204. goto invalidate_dirty_pages;
  2205. /*
  2206. * Let the uper layers retry transient errors.
  2207. * In the case of ENOSPC, if ext4_count_free_blocks()
  2208. * is non-zero, a commit should free up blocks.
  2209. */
  2210. if ((err == -ENOMEM) || (err == -EAGAIN) ||
  2211. (err == -ENOSPC && ext4_count_free_clusters(sb))) {
  2212. /*
  2213. * We may have already allocated extents for
  2214. * some bhs inside the folio, issue the
  2215. * corresponding data to prevent stale data.
  2216. */
  2217. if (progress) {
  2218. if (mpage_submit_partial_folio(mpd))
  2219. goto invalidate_dirty_pages;
  2220. goto update_disksize;
  2221. }
  2222. return err;
  2223. }
  2224. ext4_msg(sb, KERN_CRIT,
  2225. "Delayed block allocation failed for "
  2226. "inode %lu at logical offset %llu with"
  2227. " max blocks %u with error %d",
  2228. inode->i_ino,
  2229. (unsigned long long)map->m_lblk,
  2230. (unsigned)map->m_len, -err);
  2231. ext4_msg(sb, KERN_CRIT,
  2232. "This should not happen!! Data will "
  2233. "be lost\n");
  2234. if (err == -ENOSPC)
  2235. ext4_print_free_blocks(inode);
  2236. invalidate_dirty_pages:
  2237. *give_up_on_write = true;
  2238. return err;
  2239. }
  2240. progress = 1;
  2241. /*
  2242. * Update buffer state, submit mapped pages, and get us new
  2243. * extent to map
  2244. */
  2245. err = mpage_map_and_submit_buffers(mpd);
  2246. if (err < 0)
  2247. goto update_disksize;
  2248. } while (map->m_len);
  2249. update_disksize:
  2250. /*
  2251. * Update on-disk size after IO is submitted. Races with
  2252. * truncate are avoided by checking i_size under i_data_sem.
  2253. */
  2254. disksize = mpd->start_pos;
  2255. if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) {
  2256. int err2;
  2257. loff_t i_size;
  2258. down_write(&EXT4_I(inode)->i_data_sem);
  2259. i_size = i_size_read(inode);
  2260. if (disksize > i_size)
  2261. disksize = i_size;
  2262. if (disksize > EXT4_I(inode)->i_disksize)
  2263. EXT4_I(inode)->i_disksize = disksize;
  2264. up_write(&EXT4_I(inode)->i_data_sem);
  2265. err2 = ext4_mark_inode_dirty(handle, inode);
  2266. if (err2) {
  2267. ext4_error_err(inode->i_sb, -err2,
  2268. "Failed to mark inode %lu dirty",
  2269. inode->i_ino);
  2270. }
  2271. if (!err)
  2272. err = err2;
  2273. }
  2274. return err;
  2275. }
  2276. static int ext4_journal_folio_buffers(handle_t *handle, struct folio *folio,
  2277. size_t len)
  2278. {
  2279. struct buffer_head *page_bufs = folio_buffers(folio);
  2280. struct inode *inode = folio->mapping->host;
  2281. int ret, err;
  2282. ret = ext4_walk_page_buffers(handle, inode, page_bufs, 0, len,
  2283. NULL, do_journal_get_write_access);
  2284. err = ext4_walk_page_buffers(handle, inode, page_bufs, 0, len,
  2285. NULL, write_end_fn);
  2286. if (ret == 0)
  2287. ret = err;
  2288. err = ext4_jbd2_inode_add_write(handle, inode, folio_pos(folio), len);
  2289. if (ret == 0)
  2290. ret = err;
  2291. EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
  2292. return ret;
  2293. }
  2294. static int mpage_journal_page_buffers(handle_t *handle,
  2295. struct mpage_da_data *mpd,
  2296. struct folio *folio)
  2297. {
  2298. struct inode *inode = mpd->inode;
  2299. loff_t size = i_size_read(inode);
  2300. size_t len = folio_size(folio);
  2301. folio_clear_checked(folio);
  2302. mpd->wbc->nr_to_write -= folio_nr_pages(folio);
  2303. if (folio_pos(folio) + len > size &&
  2304. !ext4_verity_in_progress(inode))
  2305. len = size & (len - 1);
  2306. return ext4_journal_folio_buffers(handle, folio, len);
  2307. }
  2308. /*
  2309. * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages
  2310. * needing mapping, submit mapped pages
  2311. *
  2312. * @mpd - where to look for pages
  2313. *
  2314. * Walk dirty pages in the mapping. If they are fully mapped, submit them for
  2315. * IO immediately. If we cannot map blocks, we submit just already mapped
  2316. * buffers in the page for IO and keep page dirty. When we can map blocks and
  2317. * we find a page which isn't mapped we start accumulating extent of buffers
  2318. * underlying these pages that needs mapping (formed by either delayed or
  2319. * unwritten buffers). We also lock the pages containing these buffers. The
  2320. * extent found is returned in @mpd structure (starting at mpd->lblk with
  2321. * length mpd->len blocks).
  2322. *
  2323. * Note that this function can attach bios to one io_end structure which are
  2324. * neither logically nor physically contiguous. Although it may seem as an
  2325. * unnecessary complication, it is actually inevitable in blocksize < pagesize
  2326. * case as we need to track IO to all buffers underlying a page in one io_end.
  2327. */
  2328. static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd)
  2329. {
  2330. struct address_space *mapping = mpd->inode->i_mapping;
  2331. struct folio_batch fbatch;
  2332. unsigned int nr_folios;
  2333. pgoff_t index = mpd->start_pos >> PAGE_SHIFT;
  2334. pgoff_t end = mpd->end_pos >> PAGE_SHIFT;
  2335. xa_mark_t tag;
  2336. int i, err = 0;
  2337. ext4_lblk_t lblk;
  2338. struct buffer_head *head;
  2339. handle_t *handle = NULL;
  2340. int bpp = ext4_journal_blocks_per_folio(mpd->inode);
  2341. tag = wbc_to_tag(mpd->wbc);
  2342. mpd->map.m_len = 0;
  2343. mpd->next_pos = mpd->start_pos;
  2344. if (ext4_should_journal_data(mpd->inode)) {
  2345. handle = ext4_journal_start(mpd->inode, EXT4_HT_WRITE_PAGE,
  2346. bpp);
  2347. if (IS_ERR(handle))
  2348. return PTR_ERR(handle);
  2349. }
  2350. folio_batch_init(&fbatch);
  2351. while (index <= end) {
  2352. nr_folios = filemap_get_folios_tag(mapping, &index, end,
  2353. tag, &fbatch);
  2354. if (nr_folios == 0)
  2355. break;
  2356. for (i = 0; i < nr_folios; i++) {
  2357. struct folio *folio = fbatch.folios[i];
  2358. /*
  2359. * Accumulated enough dirty pages? This doesn't apply
  2360. * to WB_SYNC_ALL mode. For integrity sync we have to
  2361. * keep going because someone may be concurrently
  2362. * dirtying pages, and we might have synced a lot of
  2363. * newly appeared dirty pages, but have not synced all
  2364. * of the old dirty pages.
  2365. */
  2366. if (mpd->wbc->sync_mode == WB_SYNC_NONE &&
  2367. mpd->wbc->nr_to_write <=
  2368. EXT4_LBLK_TO_PG(mpd->inode, mpd->map.m_len))
  2369. goto out;
  2370. /* If we can't merge this page, we are done. */
  2371. if (mpd->map.m_len > 0 &&
  2372. mpd->next_pos != folio_pos(folio))
  2373. goto out;
  2374. if (handle) {
  2375. err = ext4_journal_ensure_credits(handle, bpp,
  2376. 0);
  2377. if (err < 0)
  2378. goto out;
  2379. }
  2380. folio_lock(folio);
  2381. /*
  2382. * If the page is no longer dirty, or its mapping no
  2383. * longer corresponds to inode we are writing (which
  2384. * means it has been truncated or invalidated), or the
  2385. * page is already under writeback and we are not doing
  2386. * a data integrity writeback, skip the page
  2387. */
  2388. if (!folio_test_dirty(folio) ||
  2389. (folio_test_writeback(folio) &&
  2390. (mpd->wbc->sync_mode == WB_SYNC_NONE)) ||
  2391. unlikely(folio->mapping != mapping)) {
  2392. folio_unlock(folio);
  2393. continue;
  2394. }
  2395. folio_wait_writeback(folio);
  2396. BUG_ON(folio_test_writeback(folio));
  2397. /*
  2398. * Should never happen but for buggy code in
  2399. * other subsystems that call
  2400. * set_page_dirty() without properly warning
  2401. * the file system first. See [1] for more
  2402. * information.
  2403. *
  2404. * [1] https://lore.kernel.org/linux-mm/20180103100430.GE4911@quack2.suse.cz
  2405. */
  2406. if (!folio_buffers(folio)) {
  2407. ext4_warning_inode(mpd->inode, "page %lu does not have buffers attached", folio->index);
  2408. folio_clear_dirty(folio);
  2409. folio_unlock(folio);
  2410. continue;
  2411. }
  2412. if (mpd->map.m_len == 0)
  2413. mpd->start_pos = folio_pos(folio);
  2414. mpd->next_pos = folio_next_pos(folio);
  2415. /*
  2416. * Writeout when we cannot modify metadata is simple.
  2417. * Just submit the page. For data=journal mode we
  2418. * first handle writeout of the page for checkpoint and
  2419. * only after that handle delayed page dirtying. This
  2420. * makes sure current data is checkpointed to the final
  2421. * location before possibly journalling it again which
  2422. * is desirable when the page is frequently dirtied
  2423. * through a pin.
  2424. */
  2425. if (!mpd->can_map) {
  2426. err = mpage_submit_folio(mpd, folio);
  2427. if (err < 0)
  2428. goto out;
  2429. /* Pending dirtying of journalled data? */
  2430. if (folio_test_checked(folio)) {
  2431. err = mpage_journal_page_buffers(handle,
  2432. mpd, folio);
  2433. if (err < 0)
  2434. goto out;
  2435. mpd->journalled_more_data = 1;
  2436. }
  2437. mpage_folio_done(mpd, folio);
  2438. } else {
  2439. /* Add all dirty buffers to mpd */
  2440. lblk = EXT4_PG_TO_LBLK(mpd->inode, folio->index);
  2441. head = folio_buffers(folio);
  2442. err = mpage_process_page_bufs(mpd, head, head,
  2443. lblk);
  2444. if (err <= 0)
  2445. goto out;
  2446. err = 0;
  2447. }
  2448. }
  2449. folio_batch_release(&fbatch);
  2450. cond_resched();
  2451. }
  2452. mpd->scanned_until_end = 1;
  2453. if (handle)
  2454. ext4_journal_stop(handle);
  2455. return 0;
  2456. out:
  2457. folio_batch_release(&fbatch);
  2458. if (handle)
  2459. ext4_journal_stop(handle);
  2460. return err;
  2461. }
  2462. static int ext4_do_writepages(struct mpage_da_data *mpd)
  2463. {
  2464. struct writeback_control *wbc = mpd->wbc;
  2465. pgoff_t writeback_index = 0;
  2466. long nr_to_write = wbc->nr_to_write;
  2467. int range_whole = 0;
  2468. int cycled = 1;
  2469. handle_t *handle = NULL;
  2470. struct inode *inode = mpd->inode;
  2471. struct address_space *mapping = inode->i_mapping;
  2472. int needed_blocks, rsv_blocks = 0, ret = 0;
  2473. struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
  2474. struct blk_plug plug;
  2475. bool give_up_on_write = false;
  2476. trace_ext4_writepages(inode, wbc);
  2477. /*
  2478. * No pages to write? This is mainly a kludge to avoid starting
  2479. * a transaction for special inodes like journal inode on last iput()
  2480. * because that could violate lock ordering on umount
  2481. */
  2482. if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
  2483. goto out_writepages;
  2484. /*
  2485. * If the filesystem has aborted, it is read-only, so return
  2486. * right away instead of dumping stack traces later on that
  2487. * will obscure the real source of the problem. We test
  2488. * fs shutdown state instead of sb->s_flag's SB_RDONLY because
  2489. * the latter could be true if the filesystem is mounted
  2490. * read-only, and in that case, ext4_writepages should
  2491. * *never* be called, so if that ever happens, we would want
  2492. * the stack trace.
  2493. */
  2494. ret = ext4_emergency_state(mapping->host->i_sb);
  2495. if (unlikely(ret))
  2496. goto out_writepages;
  2497. /*
  2498. * If we have inline data and arrive here, it means that
  2499. * we will soon create the block for the 1st page, so
  2500. * we'd better clear the inline data here.
  2501. */
  2502. if (ext4_has_inline_data(inode)) {
  2503. /* Just inode will be modified... */
  2504. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  2505. if (IS_ERR(handle)) {
  2506. ret = PTR_ERR(handle);
  2507. goto out_writepages;
  2508. }
  2509. BUG_ON(ext4_test_inode_state(inode,
  2510. EXT4_STATE_MAY_INLINE_DATA));
  2511. ext4_destroy_inline_data(handle, inode);
  2512. ext4_journal_stop(handle);
  2513. }
  2514. /*
  2515. * data=journal mode does not do delalloc so we just need to writeout /
  2516. * journal already mapped buffers. On the other hand we need to commit
  2517. * transaction to make data stable. We expect all the data to be
  2518. * already in the journal (the only exception are DMA pinned pages
  2519. * dirtied behind our back) so we commit transaction here and run the
  2520. * writeback loop to checkpoint them. The checkpointing is not actually
  2521. * necessary to make data persistent *but* quite a few places (extent
  2522. * shifting operations, fsverity, ...) depend on being able to drop
  2523. * pagecache pages after calling filemap_write_and_wait() and for that
  2524. * checkpointing needs to happen.
  2525. */
  2526. if (ext4_should_journal_data(inode)) {
  2527. mpd->can_map = 0;
  2528. if (wbc->sync_mode == WB_SYNC_ALL)
  2529. ext4_fc_commit(sbi->s_journal,
  2530. EXT4_I(inode)->i_datasync_tid);
  2531. }
  2532. mpd->journalled_more_data = 0;
  2533. if (ext4_should_dioread_nolock(inode)) {
  2534. int bpf = ext4_journal_blocks_per_folio(inode);
  2535. /*
  2536. * We may need to convert up to one extent per block in
  2537. * the folio and we may dirty the inode.
  2538. */
  2539. rsv_blocks = 1 + ext4_ext_index_trans_blocks(inode, bpf);
  2540. }
  2541. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  2542. range_whole = 1;
  2543. if (wbc->range_cyclic) {
  2544. writeback_index = mapping->writeback_index;
  2545. if (writeback_index)
  2546. cycled = 0;
  2547. mpd->start_pos = writeback_index << PAGE_SHIFT;
  2548. mpd->end_pos = LLONG_MAX;
  2549. } else {
  2550. mpd->start_pos = wbc->range_start;
  2551. mpd->end_pos = wbc->range_end;
  2552. }
  2553. ext4_io_submit_init(&mpd->io_submit, wbc);
  2554. retry:
  2555. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  2556. tag_pages_for_writeback(mapping, mpd->start_pos >> PAGE_SHIFT,
  2557. mpd->end_pos >> PAGE_SHIFT);
  2558. blk_start_plug(&plug);
  2559. /*
  2560. * First writeback pages that don't need mapping - we can avoid
  2561. * starting a transaction unnecessarily and also avoid being blocked
  2562. * in the block layer on device congestion while having transaction
  2563. * started.
  2564. */
  2565. mpd->do_map = 0;
  2566. mpd->scanned_until_end = 0;
  2567. mpd->io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
  2568. if (!mpd->io_submit.io_end) {
  2569. ret = -ENOMEM;
  2570. goto unplug;
  2571. }
  2572. ret = mpage_prepare_extent_to_map(mpd);
  2573. /* Unlock pages we didn't use */
  2574. mpage_release_unused_pages(mpd, false);
  2575. /* Submit prepared bio */
  2576. ext4_io_submit(&mpd->io_submit);
  2577. ext4_put_io_end_defer(mpd->io_submit.io_end);
  2578. mpd->io_submit.io_end = NULL;
  2579. if (ret < 0)
  2580. goto unplug;
  2581. while (!mpd->scanned_until_end && wbc->nr_to_write > 0) {
  2582. /* For each extent of pages we use new io_end */
  2583. mpd->io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
  2584. if (!mpd->io_submit.io_end) {
  2585. ret = -ENOMEM;
  2586. break;
  2587. }
  2588. WARN_ON_ONCE(!mpd->can_map);
  2589. /*
  2590. * We have two constraints: We find one extent to map and we
  2591. * must always write out whole page (makes a difference when
  2592. * blocksize < pagesize) so that we don't block on IO when we
  2593. * try to write out the rest of the page. Journalled mode is
  2594. * not supported by delalloc.
  2595. */
  2596. BUG_ON(ext4_should_journal_data(inode));
  2597. /*
  2598. * Calculate the number of credits needed to reserve for one
  2599. * extent of up to MAX_WRITEPAGES_EXTENT_LEN blocks. It will
  2600. * attempt to extend the transaction or start a new iteration
  2601. * if the reserved credits are insufficient.
  2602. */
  2603. needed_blocks = ext4_chunk_trans_blocks(inode,
  2604. MAX_WRITEPAGES_EXTENT_LEN);
  2605. /* start a new transaction */
  2606. handle = ext4_journal_start_with_reserve(inode,
  2607. EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks);
  2608. if (IS_ERR(handle)) {
  2609. ret = PTR_ERR(handle);
  2610. ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
  2611. "%ld pages, ino %lu; err %d", __func__,
  2612. wbc->nr_to_write, inode->i_ino, ret);
  2613. /* Release allocated io_end */
  2614. ext4_put_io_end(mpd->io_submit.io_end);
  2615. mpd->io_submit.io_end = NULL;
  2616. break;
  2617. }
  2618. mpd->do_map = 1;
  2619. trace_ext4_da_write_folios_start(inode, mpd->start_pos,
  2620. mpd->next_pos, wbc);
  2621. ret = mpage_prepare_extent_to_map(mpd);
  2622. if (!ret && mpd->map.m_len)
  2623. ret = mpage_map_and_submit_extent(handle, mpd,
  2624. &give_up_on_write);
  2625. /*
  2626. * Caution: If the handle is synchronous,
  2627. * ext4_journal_stop() can wait for transaction commit
  2628. * to finish which may depend on writeback of pages to
  2629. * complete or on page lock to be released. In that
  2630. * case, we have to wait until after we have
  2631. * submitted all the IO, released page locks we hold,
  2632. * and dropped io_end reference (for extent conversion
  2633. * to be able to complete) before stopping the handle.
  2634. */
  2635. if (!ext4_handle_valid(handle) || handle->h_sync == 0) {
  2636. ext4_journal_stop(handle);
  2637. handle = NULL;
  2638. mpd->do_map = 0;
  2639. }
  2640. /* Unlock pages we didn't use */
  2641. mpage_release_unused_pages(mpd, give_up_on_write);
  2642. /* Submit prepared bio */
  2643. ext4_io_submit(&mpd->io_submit);
  2644. /*
  2645. * Drop our io_end reference we got from init. We have
  2646. * to be careful and use deferred io_end finishing if
  2647. * we are still holding the transaction as we can
  2648. * release the last reference to io_end which may end
  2649. * up doing unwritten extent conversion.
  2650. */
  2651. if (handle) {
  2652. ext4_put_io_end_defer(mpd->io_submit.io_end);
  2653. ext4_journal_stop(handle);
  2654. } else
  2655. ext4_put_io_end(mpd->io_submit.io_end);
  2656. mpd->io_submit.io_end = NULL;
  2657. trace_ext4_da_write_folios_end(inode, mpd->start_pos,
  2658. mpd->next_pos, wbc, ret);
  2659. if (ret == -ENOSPC && sbi->s_journal) {
  2660. /*
  2661. * Commit the transaction which would
  2662. * free blocks released in the transaction
  2663. * and try again
  2664. */
  2665. jbd2_journal_force_commit_nested(sbi->s_journal);
  2666. ret = 0;
  2667. continue;
  2668. }
  2669. if (ret == -EAGAIN)
  2670. ret = 0;
  2671. /* Fatal error - ENOMEM, EIO... */
  2672. if (ret)
  2673. break;
  2674. }
  2675. unplug:
  2676. blk_finish_plug(&plug);
  2677. if (!ret && !cycled && wbc->nr_to_write > 0) {
  2678. cycled = 1;
  2679. mpd->end_pos = (writeback_index << PAGE_SHIFT) - 1;
  2680. mpd->start_pos = 0;
  2681. goto retry;
  2682. }
  2683. /* Update index */
  2684. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  2685. /*
  2686. * Set the writeback_index so that range_cyclic
  2687. * mode will write it back later
  2688. */
  2689. mapping->writeback_index = mpd->start_pos >> PAGE_SHIFT;
  2690. out_writepages:
  2691. trace_ext4_writepages_result(inode, wbc, ret,
  2692. nr_to_write - wbc->nr_to_write);
  2693. return ret;
  2694. }
  2695. static int ext4_writepages(struct address_space *mapping,
  2696. struct writeback_control *wbc)
  2697. {
  2698. struct super_block *sb = mapping->host->i_sb;
  2699. struct mpage_da_data mpd = {
  2700. .inode = mapping->host,
  2701. .wbc = wbc,
  2702. .can_map = 1,
  2703. };
  2704. int ret;
  2705. int alloc_ctx;
  2706. ret = ext4_emergency_state(sb);
  2707. if (unlikely(ret))
  2708. return ret;
  2709. alloc_ctx = ext4_writepages_down_read(sb);
  2710. ret = ext4_do_writepages(&mpd);
  2711. /*
  2712. * For data=journal writeback we could have come across pages marked
  2713. * for delayed dirtying (PageChecked) which were just added to the
  2714. * running transaction. Try once more to get them to stable storage.
  2715. */
  2716. if (!ret && mpd.journalled_more_data)
  2717. ret = ext4_do_writepages(&mpd);
  2718. ext4_writepages_up_read(sb, alloc_ctx);
  2719. return ret;
  2720. }
  2721. int ext4_normal_submit_inode_data_buffers(struct jbd2_inode *jinode)
  2722. {
  2723. struct writeback_control wbc = {
  2724. .sync_mode = WB_SYNC_ALL,
  2725. .nr_to_write = LONG_MAX,
  2726. .range_start = jinode->i_dirty_start,
  2727. .range_end = jinode->i_dirty_end,
  2728. };
  2729. struct mpage_da_data mpd = {
  2730. .inode = jinode->i_vfs_inode,
  2731. .wbc = &wbc,
  2732. .can_map = 0,
  2733. };
  2734. return ext4_do_writepages(&mpd);
  2735. }
  2736. static int ext4_dax_writepages(struct address_space *mapping,
  2737. struct writeback_control *wbc)
  2738. {
  2739. int ret;
  2740. long nr_to_write = wbc->nr_to_write;
  2741. struct inode *inode = mapping->host;
  2742. int alloc_ctx;
  2743. ret = ext4_emergency_state(inode->i_sb);
  2744. if (unlikely(ret))
  2745. return ret;
  2746. alloc_ctx = ext4_writepages_down_read(inode->i_sb);
  2747. trace_ext4_writepages(inode, wbc);
  2748. ret = dax_writeback_mapping_range(mapping,
  2749. EXT4_SB(inode->i_sb)->s_daxdev, wbc);
  2750. trace_ext4_writepages_result(inode, wbc, ret,
  2751. nr_to_write - wbc->nr_to_write);
  2752. ext4_writepages_up_read(inode->i_sb, alloc_ctx);
  2753. return ret;
  2754. }
  2755. static int ext4_nonda_switch(struct super_block *sb)
  2756. {
  2757. s64 free_clusters, dirty_clusters;
  2758. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2759. /*
  2760. * switch to non delalloc mode if we are running low
  2761. * on free block. The free block accounting via percpu
  2762. * counters can get slightly wrong with percpu_counter_batch getting
  2763. * accumulated on each CPU without updating global counters
  2764. * Delalloc need an accurate free block accounting. So switch
  2765. * to non delalloc when we are near to error range.
  2766. */
  2767. free_clusters =
  2768. percpu_counter_read_positive(&sbi->s_freeclusters_counter);
  2769. dirty_clusters =
  2770. percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
  2771. /*
  2772. * Start pushing delalloc when 1/2 of free blocks are dirty.
  2773. */
  2774. if (dirty_clusters && (free_clusters < 2 * dirty_clusters))
  2775. try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
  2776. if (2 * free_clusters < 3 * dirty_clusters ||
  2777. free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) {
  2778. /*
  2779. * free block count is less than 150% of dirty blocks
  2780. * or free blocks is less than watermark
  2781. */
  2782. return 1;
  2783. }
  2784. return 0;
  2785. }
  2786. static int ext4_da_write_begin(const struct kiocb *iocb,
  2787. struct address_space *mapping,
  2788. loff_t pos, unsigned len,
  2789. struct folio **foliop, void **fsdata)
  2790. {
  2791. int ret, retries = 0;
  2792. struct folio *folio;
  2793. pgoff_t index;
  2794. struct inode *inode = mapping->host;
  2795. ret = ext4_emergency_state(inode->i_sb);
  2796. if (unlikely(ret))
  2797. return ret;
  2798. index = pos >> PAGE_SHIFT;
  2799. if (ext4_nonda_switch(inode->i_sb) || ext4_verity_in_progress(inode)) {
  2800. *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
  2801. return ext4_write_begin(iocb, mapping, pos,
  2802. len, foliop, fsdata);
  2803. }
  2804. *fsdata = (void *)0;
  2805. trace_ext4_da_write_begin(inode, pos, len);
  2806. if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
  2807. ret = ext4_generic_write_inline_data(mapping, inode, pos, len,
  2808. foliop, fsdata, true);
  2809. if (ret < 0)
  2810. return ret;
  2811. if (ret == 1)
  2812. return 0;
  2813. }
  2814. retry:
  2815. folio = write_begin_get_folio(iocb, mapping, index, len);
  2816. if (IS_ERR(folio))
  2817. return PTR_ERR(folio);
  2818. if (len > folio_next_pos(folio) - pos)
  2819. len = folio_next_pos(folio) - pos;
  2820. ret = ext4_block_write_begin(NULL, folio, pos, len,
  2821. ext4_da_get_block_prep);
  2822. if (ret < 0) {
  2823. folio_unlock(folio);
  2824. folio_put(folio);
  2825. /*
  2826. * ext4_block_write_begin may have instantiated a few blocks
  2827. * outside i_size. Trim these off again. Don't need
  2828. * i_size_read because we hold inode lock.
  2829. */
  2830. if (pos + len > inode->i_size)
  2831. ext4_truncate_failed_write(inode);
  2832. if (ret == -ENOSPC &&
  2833. ext4_should_retry_alloc(inode->i_sb, &retries))
  2834. goto retry;
  2835. return ret;
  2836. }
  2837. *foliop = folio;
  2838. return ret;
  2839. }
  2840. /*
  2841. * Check if we should update i_disksize
  2842. * when write to the end of file but not require block allocation
  2843. */
  2844. static int ext4_da_should_update_i_disksize(struct folio *folio,
  2845. unsigned long offset)
  2846. {
  2847. struct buffer_head *bh;
  2848. struct inode *inode = folio->mapping->host;
  2849. unsigned int idx;
  2850. int i;
  2851. bh = folio_buffers(folio);
  2852. idx = offset >> inode->i_blkbits;
  2853. for (i = 0; i < idx; i++)
  2854. bh = bh->b_this_page;
  2855. if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
  2856. return 0;
  2857. return 1;
  2858. }
  2859. static int ext4_da_do_write_end(struct address_space *mapping,
  2860. loff_t pos, unsigned len, unsigned copied,
  2861. struct folio *folio)
  2862. {
  2863. struct inode *inode = mapping->host;
  2864. loff_t old_size = inode->i_size;
  2865. bool disksize_changed = false;
  2866. loff_t new_i_size, zero_len = 0;
  2867. handle_t *handle;
  2868. if (unlikely(!folio_buffers(folio))) {
  2869. folio_unlock(folio);
  2870. folio_put(folio);
  2871. return -EIO;
  2872. }
  2873. /*
  2874. * block_write_end() will mark the inode as dirty with I_DIRTY_PAGES
  2875. * flag, which all that's needed to trigger page writeback.
  2876. */
  2877. copied = block_write_end(pos, len, copied, folio);
  2878. new_i_size = pos + copied;
  2879. /*
  2880. * It's important to update i_size while still holding folio lock,
  2881. * because folio writeout could otherwise come in and zero beyond
  2882. * i_size.
  2883. *
  2884. * Since we are holding inode lock, we are sure i_disksize <=
  2885. * i_size. We also know that if i_disksize < i_size, there are
  2886. * delalloc writes pending in the range up to i_size. If the end of
  2887. * the current write is <= i_size, there's no need to touch
  2888. * i_disksize since writeback will push i_disksize up to i_size
  2889. * eventually. If the end of the current write is > i_size and
  2890. * inside an allocated block which ext4_da_should_update_i_disksize()
  2891. * checked, we need to update i_disksize here as certain
  2892. * ext4_writepages() paths not allocating blocks and update i_disksize.
  2893. */
  2894. if (new_i_size > inode->i_size) {
  2895. unsigned long end;
  2896. i_size_write(inode, new_i_size);
  2897. end = offset_in_folio(folio, new_i_size - 1);
  2898. if (copied && ext4_da_should_update_i_disksize(folio, end)) {
  2899. ext4_update_i_disksize(inode, new_i_size);
  2900. disksize_changed = true;
  2901. }
  2902. }
  2903. folio_unlock(folio);
  2904. folio_put(folio);
  2905. if (pos > old_size) {
  2906. pagecache_isize_extended(inode, old_size, pos);
  2907. zero_len = pos - old_size;
  2908. }
  2909. if (!disksize_changed && !zero_len)
  2910. return copied;
  2911. handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
  2912. if (IS_ERR(handle))
  2913. return PTR_ERR(handle);
  2914. if (zero_len)
  2915. ext4_zero_partial_blocks(handle, inode, old_size, zero_len);
  2916. ext4_mark_inode_dirty(handle, inode);
  2917. ext4_journal_stop(handle);
  2918. return copied;
  2919. }
  2920. static int ext4_da_write_end(const struct kiocb *iocb,
  2921. struct address_space *mapping,
  2922. loff_t pos, unsigned len, unsigned copied,
  2923. struct folio *folio, void *fsdata)
  2924. {
  2925. struct inode *inode = mapping->host;
  2926. int write_mode = (int)(unsigned long)fsdata;
  2927. if (write_mode == FALL_BACK_TO_NONDELALLOC)
  2928. return ext4_write_end(iocb, mapping, pos,
  2929. len, copied, folio, fsdata);
  2930. trace_ext4_da_write_end(inode, pos, len, copied);
  2931. if (write_mode != CONVERT_INLINE_DATA &&
  2932. ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
  2933. ext4_has_inline_data(inode))
  2934. return ext4_write_inline_data_end(inode, pos, len, copied,
  2935. folio);
  2936. if (unlikely(copied < len) && !folio_test_uptodate(folio))
  2937. copied = 0;
  2938. return ext4_da_do_write_end(mapping, pos, len, copied, folio);
  2939. }
  2940. /*
  2941. * Force all delayed allocation blocks to be allocated for a given inode.
  2942. */
  2943. int ext4_alloc_da_blocks(struct inode *inode)
  2944. {
  2945. trace_ext4_alloc_da_blocks(inode);
  2946. if (!EXT4_I(inode)->i_reserved_data_blocks)
  2947. return 0;
  2948. /*
  2949. * We do something simple for now. The filemap_flush() will
  2950. * also start triggering a write of the data blocks, which is
  2951. * not strictly speaking necessary. However, to do otherwise
  2952. * would require replicating code paths in:
  2953. *
  2954. * ext4_writepages() ->
  2955. * write_cache_pages() ---> (via passed in callback function)
  2956. * __mpage_da_writepage() -->
  2957. * mpage_add_bh_to_extent()
  2958. * mpage_da_map_blocks()
  2959. *
  2960. * The problem is that write_cache_pages(), located in
  2961. * mm/page-writeback.c, marks pages clean in preparation for
  2962. * doing I/O, which is not desirable if we're not planning on
  2963. * doing I/O at all.
  2964. *
  2965. * We could call write_cache_pages(), and then redirty all of
  2966. * the pages by calling redirty_page_for_writepage() but that
  2967. * would be ugly in the extreme. So instead we would need to
  2968. * replicate parts of the code in the above functions,
  2969. * simplifying them because we wouldn't actually intend to
  2970. * write out the pages, but rather only collect contiguous
  2971. * logical block extents, call the multi-block allocator, and
  2972. * then update the buffer heads with the block allocations.
  2973. *
  2974. * For now, though, we'll cheat by calling filemap_flush(),
  2975. * which will map the blocks, and start the I/O, but not
  2976. * actually wait for the I/O to complete.
  2977. */
  2978. return filemap_flush(inode->i_mapping);
  2979. }
  2980. /*
  2981. * bmap() is special. It gets used by applications such as lilo and by
  2982. * the swapper to find the on-disk block of a specific piece of data.
  2983. *
  2984. * Naturally, this is dangerous if the block concerned is still in the
  2985. * journal. If somebody makes a swapfile on an ext4 data-journaling
  2986. * filesystem and enables swap, then they may get a nasty shock when the
  2987. * data getting swapped to that swapfile suddenly gets overwritten by
  2988. * the original zero's written out previously to the journal and
  2989. * awaiting writeback in the kernel's buffer cache.
  2990. *
  2991. * So, if we see any bmap calls here on a modified, data-journaled file,
  2992. * take extra steps to flush any blocks which might be in the cache.
  2993. */
  2994. static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
  2995. {
  2996. struct inode *inode = mapping->host;
  2997. sector_t ret = 0;
  2998. inode_lock_shared(inode);
  2999. /*
  3000. * We can get here for an inline file via the FIBMAP ioctl
  3001. */
  3002. if (ext4_has_inline_data(inode))
  3003. goto out;
  3004. if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
  3005. (test_opt(inode->i_sb, DELALLOC) ||
  3006. ext4_should_journal_data(inode))) {
  3007. /*
  3008. * With delalloc or journalled data we want to sync the file so
  3009. * that we can make sure we allocate blocks for file and data
  3010. * is in place for the user to see it
  3011. */
  3012. filemap_write_and_wait(mapping);
  3013. }
  3014. ret = iomap_bmap(mapping, block, &ext4_iomap_ops);
  3015. out:
  3016. inode_unlock_shared(inode);
  3017. return ret;
  3018. }
  3019. static void ext4_invalidate_folio(struct folio *folio, size_t offset,
  3020. size_t length)
  3021. {
  3022. trace_ext4_invalidate_folio(folio, offset, length);
  3023. /* No journalling happens on data buffers when this function is used */
  3024. WARN_ON(folio_buffers(folio) && buffer_jbd(folio_buffers(folio)));
  3025. block_invalidate_folio(folio, offset, length);
  3026. }
  3027. static int __ext4_journalled_invalidate_folio(struct folio *folio,
  3028. size_t offset, size_t length)
  3029. {
  3030. journal_t *journal = EXT4_JOURNAL(folio->mapping->host);
  3031. trace_ext4_journalled_invalidate_folio(folio, offset, length);
  3032. /*
  3033. * If it's a full truncate we just forget about the pending dirtying
  3034. */
  3035. if (offset == 0 && length == folio_size(folio))
  3036. folio_clear_checked(folio);
  3037. return jbd2_journal_invalidate_folio(journal, folio, offset, length);
  3038. }
  3039. /* Wrapper for aops... */
  3040. static void ext4_journalled_invalidate_folio(struct folio *folio,
  3041. size_t offset,
  3042. size_t length)
  3043. {
  3044. WARN_ON(__ext4_journalled_invalidate_folio(folio, offset, length) < 0);
  3045. }
  3046. static bool ext4_release_folio(struct folio *folio, gfp_t wait)
  3047. {
  3048. struct inode *inode = folio->mapping->host;
  3049. journal_t *journal = EXT4_JOURNAL(inode);
  3050. trace_ext4_release_folio(inode, folio);
  3051. /* Page has dirty journalled data -> cannot release */
  3052. if (folio_test_checked(folio))
  3053. return false;
  3054. if (journal)
  3055. return jbd2_journal_try_to_free_buffers(journal, folio);
  3056. else
  3057. return try_to_free_buffers(folio);
  3058. }
  3059. static bool ext4_inode_datasync_dirty(struct inode *inode)
  3060. {
  3061. journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
  3062. if (journal) {
  3063. if (jbd2_transaction_committed(journal,
  3064. EXT4_I(inode)->i_datasync_tid))
  3065. return false;
  3066. if (test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT))
  3067. return !list_empty(&EXT4_I(inode)->i_fc_list);
  3068. return true;
  3069. }
  3070. /* Any metadata buffers to write? */
  3071. if (!list_empty(&inode->i_mapping->i_private_list))
  3072. return true;
  3073. return inode_state_read_once(inode) & I_DIRTY_DATASYNC;
  3074. }
  3075. static void ext4_set_iomap(struct inode *inode, struct iomap *iomap,
  3076. struct ext4_map_blocks *map, loff_t offset,
  3077. loff_t length, unsigned int flags)
  3078. {
  3079. u8 blkbits = inode->i_blkbits;
  3080. /*
  3081. * Writes that span EOF might trigger an I/O size update on completion,
  3082. * so consider them to be dirty for the purpose of O_DSYNC, even if
  3083. * there is no other metadata changes being made or are pending.
  3084. */
  3085. iomap->flags = 0;
  3086. if (ext4_inode_datasync_dirty(inode) ||
  3087. offset + length > i_size_read(inode))
  3088. iomap->flags |= IOMAP_F_DIRTY;
  3089. if (map->m_flags & EXT4_MAP_NEW)
  3090. iomap->flags |= IOMAP_F_NEW;
  3091. /* HW-offload atomics are always used */
  3092. if (flags & IOMAP_ATOMIC)
  3093. iomap->flags |= IOMAP_F_ATOMIC_BIO;
  3094. if (flags & IOMAP_DAX)
  3095. iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev;
  3096. else
  3097. iomap->bdev = inode->i_sb->s_bdev;
  3098. iomap->offset = EXT4_LBLK_TO_B(inode, map->m_lblk);
  3099. iomap->length = EXT4_LBLK_TO_B(inode, map->m_len);
  3100. if ((map->m_flags & EXT4_MAP_MAPPED) &&
  3101. !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3102. iomap->flags |= IOMAP_F_MERGED;
  3103. /*
  3104. * Flags passed to ext4_map_blocks() for direct I/O writes can result
  3105. * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits
  3106. * set. In order for any allocated unwritten extents to be converted
  3107. * into written extents correctly within the ->end_io() handler, we
  3108. * need to ensure that the iomap->type is set appropriately. Hence, the
  3109. * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has
  3110. * been set first.
  3111. */
  3112. if (map->m_flags & EXT4_MAP_UNWRITTEN) {
  3113. iomap->type = IOMAP_UNWRITTEN;
  3114. iomap->addr = (u64) map->m_pblk << blkbits;
  3115. if (flags & IOMAP_DAX)
  3116. iomap->addr += EXT4_SB(inode->i_sb)->s_dax_part_off;
  3117. } else if (map->m_flags & EXT4_MAP_MAPPED) {
  3118. iomap->type = IOMAP_MAPPED;
  3119. iomap->addr = (u64) map->m_pblk << blkbits;
  3120. if (flags & IOMAP_DAX)
  3121. iomap->addr += EXT4_SB(inode->i_sb)->s_dax_part_off;
  3122. } else if (map->m_flags & EXT4_MAP_DELAYED) {
  3123. iomap->type = IOMAP_DELALLOC;
  3124. iomap->addr = IOMAP_NULL_ADDR;
  3125. } else {
  3126. iomap->type = IOMAP_HOLE;
  3127. iomap->addr = IOMAP_NULL_ADDR;
  3128. }
  3129. }
  3130. static int ext4_map_blocks_atomic_write_slow(handle_t *handle,
  3131. struct inode *inode, struct ext4_map_blocks *map)
  3132. {
  3133. ext4_lblk_t m_lblk = map->m_lblk;
  3134. unsigned int m_len = map->m_len;
  3135. unsigned int mapped_len = 0, m_flags = 0;
  3136. ext4_fsblk_t next_pblk = 0;
  3137. bool check_next_pblk = false;
  3138. int ret = 0;
  3139. WARN_ON_ONCE(!ext4_has_feature_bigalloc(inode->i_sb));
  3140. /*
  3141. * This is a slow path in case of mixed mapping. We use
  3142. * EXT4_GET_BLOCKS_CREATE_ZERO flag here to make sure we get a single
  3143. * contiguous mapped mapping. This will ensure any unwritten or hole
  3144. * regions within the requested range is zeroed out and we return
  3145. * a single contiguous mapped extent.
  3146. */
  3147. m_flags = EXT4_GET_BLOCKS_CREATE_ZERO;
  3148. do {
  3149. ret = ext4_map_blocks(handle, inode, map, m_flags);
  3150. if (ret < 0 && ret != -ENOSPC)
  3151. goto out_err;
  3152. /*
  3153. * This should never happen, but let's return an error code to
  3154. * avoid an infinite loop in here.
  3155. */
  3156. if (ret == 0) {
  3157. ret = -EFSCORRUPTED;
  3158. ext4_warning_inode(inode,
  3159. "ext4_map_blocks() couldn't allocate blocks m_flags: 0x%x, ret:%d",
  3160. m_flags, ret);
  3161. goto out_err;
  3162. }
  3163. /*
  3164. * With bigalloc we should never get ENOSPC nor discontiguous
  3165. * physical extents.
  3166. */
  3167. if ((check_next_pblk && next_pblk != map->m_pblk) ||
  3168. ret == -ENOSPC) {
  3169. ext4_warning_inode(inode,
  3170. "Non-contiguous allocation detected: expected %llu, got %llu, "
  3171. "or ext4_map_blocks() returned out of space ret: %d",
  3172. next_pblk, map->m_pblk, ret);
  3173. ret = -EFSCORRUPTED;
  3174. goto out_err;
  3175. }
  3176. next_pblk = map->m_pblk + map->m_len;
  3177. check_next_pblk = true;
  3178. mapped_len += map->m_len;
  3179. map->m_lblk += map->m_len;
  3180. map->m_len = m_len - mapped_len;
  3181. } while (mapped_len < m_len);
  3182. /*
  3183. * We might have done some work in above loop, so we need to query the
  3184. * start of the physical extent, based on the origin m_lblk and m_len.
  3185. * Let's also ensure we were able to allocate the required range for
  3186. * mixed mapping case.
  3187. */
  3188. map->m_lblk = m_lblk;
  3189. map->m_len = m_len;
  3190. map->m_flags = 0;
  3191. ret = ext4_map_blocks(handle, inode, map,
  3192. EXT4_GET_BLOCKS_QUERY_LAST_IN_LEAF);
  3193. if (ret != m_len) {
  3194. ext4_warning_inode(inode,
  3195. "allocation failed for atomic write request m_lblk:%u, m_len:%u, ret:%d\n",
  3196. m_lblk, m_len, ret);
  3197. ret = -EINVAL;
  3198. }
  3199. return ret;
  3200. out_err:
  3201. /* reset map before returning an error */
  3202. map->m_lblk = m_lblk;
  3203. map->m_len = m_len;
  3204. map->m_flags = 0;
  3205. return ret;
  3206. }
  3207. /*
  3208. * ext4_map_blocks_atomic: Helper routine to ensure the entire requested
  3209. * range in @map [lblk, lblk + len) is one single contiguous extent with no
  3210. * mixed mappings.
  3211. *
  3212. * We first use m_flags passed to us by our caller (ext4_iomap_alloc()).
  3213. * We only call EXT4_GET_BLOCKS_ZERO in the slow path, when the underlying
  3214. * physical extent for the requested range does not have a single contiguous
  3215. * mapping type i.e. (Hole, Mapped, or Unwritten) throughout.
  3216. * In that case we will loop over the requested range to allocate and zero out
  3217. * the unwritten / holes in between, to get a single mapped extent from
  3218. * [m_lblk, m_lblk + m_len). Note that this is only possible because we know
  3219. * this can be called only with bigalloc enabled filesystem where the underlying
  3220. * cluster is already allocated. This avoids allocating discontiguous extents
  3221. * in the slow path due to multiple calls to ext4_map_blocks().
  3222. * The slow path is mostly non-performance critical path, so it should be ok to
  3223. * loop using ext4_map_blocks() with appropriate flags to allocate & zero the
  3224. * underlying short holes/unwritten extents within the requested range.
  3225. */
  3226. static int ext4_map_blocks_atomic_write(handle_t *handle, struct inode *inode,
  3227. struct ext4_map_blocks *map, int m_flags,
  3228. bool *force_commit)
  3229. {
  3230. ext4_lblk_t m_lblk = map->m_lblk;
  3231. unsigned int m_len = map->m_len;
  3232. int ret = 0;
  3233. WARN_ON_ONCE(m_len > 1 && !ext4_has_feature_bigalloc(inode->i_sb));
  3234. ret = ext4_map_blocks(handle, inode, map, m_flags);
  3235. if (ret < 0 || ret == m_len)
  3236. goto out;
  3237. /*
  3238. * This is a mixed mapping case where we were not able to allocate
  3239. * a single contiguous extent. In that case let's reset requested
  3240. * mapping and call the slow path.
  3241. */
  3242. map->m_lblk = m_lblk;
  3243. map->m_len = m_len;
  3244. map->m_flags = 0;
  3245. /*
  3246. * slow path means we have mixed mapping, that means we will need
  3247. * to force txn commit.
  3248. */
  3249. *force_commit = true;
  3250. return ext4_map_blocks_atomic_write_slow(handle, inode, map);
  3251. out:
  3252. return ret;
  3253. }
  3254. static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map,
  3255. unsigned int flags)
  3256. {
  3257. handle_t *handle;
  3258. int ret, dio_credits, m_flags = 0, retries = 0;
  3259. bool force_commit = false;
  3260. /*
  3261. * Trim the mapping request to the maximum value that we can map at
  3262. * once for direct I/O.
  3263. */
  3264. if (map->m_len > DIO_MAX_BLOCKS)
  3265. map->m_len = DIO_MAX_BLOCKS;
  3266. /*
  3267. * journal credits estimation for atomic writes. We call
  3268. * ext4_map_blocks(), to find if there could be a mixed mapping. If yes,
  3269. * then let's assume the no. of pextents required can be m_len i.e.
  3270. * every alternate block can be unwritten and hole.
  3271. */
  3272. if (flags & IOMAP_ATOMIC) {
  3273. unsigned int orig_mlen = map->m_len;
  3274. ret = ext4_map_blocks(NULL, inode, map, 0);
  3275. if (ret < 0)
  3276. return ret;
  3277. if (map->m_len < orig_mlen) {
  3278. map->m_len = orig_mlen;
  3279. dio_credits = ext4_meta_trans_blocks(inode, orig_mlen,
  3280. map->m_len);
  3281. } else {
  3282. dio_credits = ext4_chunk_trans_blocks(inode,
  3283. map->m_len);
  3284. }
  3285. } else {
  3286. dio_credits = ext4_chunk_trans_blocks(inode, map->m_len);
  3287. }
  3288. retry:
  3289. /*
  3290. * Either we allocate blocks and then don't get an unwritten extent, so
  3291. * in that case we have reserved enough credits. Or, the blocks are
  3292. * already allocated and unwritten. In that case, the extent conversion
  3293. * fits into the credits as well.
  3294. */
  3295. handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits);
  3296. if (IS_ERR(handle))
  3297. return PTR_ERR(handle);
  3298. /*
  3299. * DAX and direct I/O are the only two operations that are currently
  3300. * supported with IOMAP_WRITE.
  3301. */
  3302. WARN_ON(!(flags & (IOMAP_DAX | IOMAP_DIRECT)));
  3303. if (flags & IOMAP_DAX)
  3304. m_flags = EXT4_GET_BLOCKS_CREATE_ZERO;
  3305. /*
  3306. * We use i_size instead of i_disksize here because delalloc writeback
  3307. * can complete at any point during the I/O and subsequently push the
  3308. * i_disksize out to i_size. This could be beyond where direct I/O is
  3309. * happening and thus expose allocated blocks to direct I/O reads.
  3310. */
  3311. else if (EXT4_LBLK_TO_B(inode, map->m_lblk) >= i_size_read(inode))
  3312. m_flags = EXT4_GET_BLOCKS_CREATE;
  3313. else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3314. m_flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
  3315. if (flags & IOMAP_ATOMIC)
  3316. ret = ext4_map_blocks_atomic_write(handle, inode, map, m_flags,
  3317. &force_commit);
  3318. else
  3319. ret = ext4_map_blocks(handle, inode, map, m_flags);
  3320. /*
  3321. * We cannot fill holes in indirect tree based inodes as that could
  3322. * expose stale data in the case of a crash. Use the magic error code
  3323. * to fallback to buffered I/O.
  3324. */
  3325. if (!m_flags && !ret)
  3326. ret = -ENOTBLK;
  3327. ext4_journal_stop(handle);
  3328. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  3329. goto retry;
  3330. /*
  3331. * Force commit the current transaction if the allocation spans a mixed
  3332. * mapping range. This ensures any pending metadata updates (like
  3333. * unwritten to written extents conversion) in this range are in
  3334. * consistent state with the file data blocks, before performing the
  3335. * actual write I/O. If the commit fails, the whole I/O must be aborted
  3336. * to prevent any possible torn writes.
  3337. */
  3338. if (ret > 0 && force_commit) {
  3339. int ret2;
  3340. ret2 = ext4_force_commit(inode->i_sb);
  3341. if (ret2)
  3342. return ret2;
  3343. }
  3344. return ret;
  3345. }
  3346. static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
  3347. unsigned flags, struct iomap *iomap, struct iomap *srcmap)
  3348. {
  3349. int ret;
  3350. struct ext4_map_blocks map;
  3351. u8 blkbits = inode->i_blkbits;
  3352. unsigned int orig_mlen;
  3353. if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
  3354. return -EINVAL;
  3355. if (WARN_ON_ONCE(ext4_has_inline_data(inode)))
  3356. return -ERANGE;
  3357. /*
  3358. * Calculate the first and last logical blocks respectively.
  3359. */
  3360. map.m_lblk = offset >> blkbits;
  3361. map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
  3362. EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
  3363. orig_mlen = map.m_len;
  3364. if (flags & IOMAP_WRITE) {
  3365. /*
  3366. * We check here if the blocks are already allocated, then we
  3367. * don't need to start a journal txn and we can directly return
  3368. * the mapping information. This could boost performance
  3369. * especially in multi-threaded overwrite requests.
  3370. */
  3371. if (offset + length <= i_size_read(inode)) {
  3372. ret = ext4_map_blocks(NULL, inode, &map, 0);
  3373. /*
  3374. * For DAX we convert extents to initialized ones before
  3375. * copying the data, otherwise we do it after I/O so
  3376. * there's no need to call into ext4_iomap_alloc().
  3377. */
  3378. if ((map.m_flags & EXT4_MAP_MAPPED) ||
  3379. (!(flags & IOMAP_DAX) &&
  3380. (map.m_flags & EXT4_MAP_UNWRITTEN))) {
  3381. /*
  3382. * For atomic writes the entire requested
  3383. * length should be mapped.
  3384. */
  3385. if (ret == orig_mlen ||
  3386. (!(flags & IOMAP_ATOMIC) && ret > 0))
  3387. goto out;
  3388. }
  3389. map.m_len = orig_mlen;
  3390. }
  3391. ret = ext4_iomap_alloc(inode, &map, flags);
  3392. } else {
  3393. ret = ext4_map_blocks(NULL, inode, &map, 0);
  3394. }
  3395. if (ret < 0)
  3396. return ret;
  3397. out:
  3398. /*
  3399. * When inline encryption is enabled, sometimes I/O to an encrypted file
  3400. * has to be broken up to guarantee DUN contiguity. Handle this by
  3401. * limiting the length of the mapping returned.
  3402. */
  3403. map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len);
  3404. /*
  3405. * Before returning to iomap, let's ensure the allocated mapping
  3406. * covers the entire requested length for atomic writes.
  3407. */
  3408. if (flags & IOMAP_ATOMIC) {
  3409. if (map.m_len < (length >> blkbits)) {
  3410. WARN_ON_ONCE(1);
  3411. return -EINVAL;
  3412. }
  3413. }
  3414. ext4_set_iomap(inode, iomap, &map, offset, length, flags);
  3415. return 0;
  3416. }
  3417. const struct iomap_ops ext4_iomap_ops = {
  3418. .iomap_begin = ext4_iomap_begin,
  3419. };
  3420. static int ext4_iomap_begin_report(struct inode *inode, loff_t offset,
  3421. loff_t length, unsigned int flags,
  3422. struct iomap *iomap, struct iomap *srcmap)
  3423. {
  3424. int ret;
  3425. struct ext4_map_blocks map;
  3426. u8 blkbits = inode->i_blkbits;
  3427. if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
  3428. return -EINVAL;
  3429. if (ext4_has_inline_data(inode)) {
  3430. ret = ext4_inline_data_iomap(inode, iomap);
  3431. if (ret != -EAGAIN) {
  3432. if (ret == 0 && offset >= iomap->length)
  3433. ret = -ENOENT;
  3434. return ret;
  3435. }
  3436. }
  3437. /*
  3438. * Calculate the first and last logical block respectively.
  3439. */
  3440. map.m_lblk = offset >> blkbits;
  3441. map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
  3442. EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
  3443. /*
  3444. * Fiemap callers may call for offset beyond s_bitmap_maxbytes.
  3445. * So handle it here itself instead of querying ext4_map_blocks().
  3446. * Since ext4_map_blocks() will warn about it and will return
  3447. * -EIO error.
  3448. */
  3449. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  3450. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3451. if (offset >= sbi->s_bitmap_maxbytes) {
  3452. map.m_flags = 0;
  3453. goto set_iomap;
  3454. }
  3455. }
  3456. ret = ext4_map_blocks(NULL, inode, &map, 0);
  3457. if (ret < 0)
  3458. return ret;
  3459. set_iomap:
  3460. ext4_set_iomap(inode, iomap, &map, offset, length, flags);
  3461. return 0;
  3462. }
  3463. const struct iomap_ops ext4_iomap_report_ops = {
  3464. .iomap_begin = ext4_iomap_begin_report,
  3465. };
  3466. /*
  3467. * For data=journal mode, folio should be marked dirty only when it was
  3468. * writeably mapped. When that happens, it was already attached to the
  3469. * transaction and marked as jbddirty (we take care of this in
  3470. * ext4_page_mkwrite()). On transaction commit, we writeprotect page mappings
  3471. * so we should have nothing to do here, except for the case when someone
  3472. * had the page pinned and dirtied the page through this pin (e.g. by doing
  3473. * direct IO to it). In that case we'd need to attach buffers here to the
  3474. * transaction but we cannot due to lock ordering. We cannot just dirty the
  3475. * folio and leave attached buffers clean, because the buffers' dirty state is
  3476. * "definitive". We cannot just set the buffers dirty or jbddirty because all
  3477. * the journalling code will explode. So what we do is to mark the folio
  3478. * "pending dirty" and next time ext4_writepages() is called, attach buffers
  3479. * to the transaction appropriately.
  3480. */
  3481. static bool ext4_journalled_dirty_folio(struct address_space *mapping,
  3482. struct folio *folio)
  3483. {
  3484. WARN_ON_ONCE(!folio_buffers(folio));
  3485. if (folio_maybe_dma_pinned(folio))
  3486. folio_set_checked(folio);
  3487. return filemap_dirty_folio(mapping, folio);
  3488. }
  3489. static bool ext4_dirty_folio(struct address_space *mapping, struct folio *folio)
  3490. {
  3491. WARN_ON_ONCE(!folio_test_locked(folio) && !folio_test_dirty(folio));
  3492. WARN_ON_ONCE(!folio_buffers(folio));
  3493. return block_dirty_folio(mapping, folio);
  3494. }
  3495. static int ext4_iomap_swap_activate(struct swap_info_struct *sis,
  3496. struct file *file, sector_t *span)
  3497. {
  3498. return iomap_swapfile_activate(sis, file, span,
  3499. &ext4_iomap_report_ops);
  3500. }
  3501. static const struct address_space_operations ext4_aops = {
  3502. .read_folio = ext4_read_folio,
  3503. .readahead = ext4_readahead,
  3504. .writepages = ext4_writepages,
  3505. .write_begin = ext4_write_begin,
  3506. .write_end = ext4_write_end,
  3507. .dirty_folio = ext4_dirty_folio,
  3508. .bmap = ext4_bmap,
  3509. .invalidate_folio = ext4_invalidate_folio,
  3510. .release_folio = ext4_release_folio,
  3511. .migrate_folio = buffer_migrate_folio,
  3512. .is_partially_uptodate = block_is_partially_uptodate,
  3513. .error_remove_folio = generic_error_remove_folio,
  3514. .swap_activate = ext4_iomap_swap_activate,
  3515. };
  3516. static const struct address_space_operations ext4_journalled_aops = {
  3517. .read_folio = ext4_read_folio,
  3518. .readahead = ext4_readahead,
  3519. .writepages = ext4_writepages,
  3520. .write_begin = ext4_write_begin,
  3521. .write_end = ext4_journalled_write_end,
  3522. .dirty_folio = ext4_journalled_dirty_folio,
  3523. .bmap = ext4_bmap,
  3524. .invalidate_folio = ext4_journalled_invalidate_folio,
  3525. .release_folio = ext4_release_folio,
  3526. .migrate_folio = buffer_migrate_folio_norefs,
  3527. .is_partially_uptodate = block_is_partially_uptodate,
  3528. .error_remove_folio = generic_error_remove_folio,
  3529. .swap_activate = ext4_iomap_swap_activate,
  3530. };
  3531. static const struct address_space_operations ext4_da_aops = {
  3532. .read_folio = ext4_read_folio,
  3533. .readahead = ext4_readahead,
  3534. .writepages = ext4_writepages,
  3535. .write_begin = ext4_da_write_begin,
  3536. .write_end = ext4_da_write_end,
  3537. .dirty_folio = ext4_dirty_folio,
  3538. .bmap = ext4_bmap,
  3539. .invalidate_folio = ext4_invalidate_folio,
  3540. .release_folio = ext4_release_folio,
  3541. .migrate_folio = buffer_migrate_folio,
  3542. .is_partially_uptodate = block_is_partially_uptodate,
  3543. .error_remove_folio = generic_error_remove_folio,
  3544. .swap_activate = ext4_iomap_swap_activate,
  3545. };
  3546. static const struct address_space_operations ext4_dax_aops = {
  3547. .writepages = ext4_dax_writepages,
  3548. .dirty_folio = noop_dirty_folio,
  3549. .bmap = ext4_bmap,
  3550. .swap_activate = ext4_iomap_swap_activate,
  3551. };
  3552. void ext4_set_aops(struct inode *inode)
  3553. {
  3554. switch (ext4_inode_journal_mode(inode)) {
  3555. case EXT4_INODE_ORDERED_DATA_MODE:
  3556. case EXT4_INODE_WRITEBACK_DATA_MODE:
  3557. break;
  3558. case EXT4_INODE_JOURNAL_DATA_MODE:
  3559. inode->i_mapping->a_ops = &ext4_journalled_aops;
  3560. return;
  3561. default:
  3562. BUG();
  3563. }
  3564. if (IS_DAX(inode))
  3565. inode->i_mapping->a_ops = &ext4_dax_aops;
  3566. else if (test_opt(inode->i_sb, DELALLOC))
  3567. inode->i_mapping->a_ops = &ext4_da_aops;
  3568. else
  3569. inode->i_mapping->a_ops = &ext4_aops;
  3570. }
  3571. /*
  3572. * Here we can't skip an unwritten buffer even though it usually reads zero
  3573. * because it might have data in pagecache (eg, if called from ext4_zero_range,
  3574. * ext4_punch_hole, etc) which needs to be properly zeroed out. Otherwise a
  3575. * racing writeback can come later and flush the stale pagecache to disk.
  3576. */
  3577. static int __ext4_block_zero_page_range(handle_t *handle,
  3578. struct address_space *mapping, loff_t from, loff_t length)
  3579. {
  3580. unsigned int offset, blocksize, pos;
  3581. ext4_lblk_t iblock;
  3582. struct inode *inode = mapping->host;
  3583. struct buffer_head *bh;
  3584. struct folio *folio;
  3585. int err = 0;
  3586. folio = __filemap_get_folio(mapping, from >> PAGE_SHIFT,
  3587. FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
  3588. mapping_gfp_constraint(mapping, ~__GFP_FS));
  3589. if (IS_ERR(folio))
  3590. return PTR_ERR(folio);
  3591. blocksize = inode->i_sb->s_blocksize;
  3592. iblock = EXT4_PG_TO_LBLK(inode, folio->index);
  3593. bh = folio_buffers(folio);
  3594. if (!bh)
  3595. bh = create_empty_buffers(folio, blocksize, 0);
  3596. /* Find the buffer that contains "offset" */
  3597. offset = offset_in_folio(folio, from);
  3598. pos = blocksize;
  3599. while (offset >= pos) {
  3600. bh = bh->b_this_page;
  3601. iblock++;
  3602. pos += blocksize;
  3603. }
  3604. if (buffer_freed(bh)) {
  3605. BUFFER_TRACE(bh, "freed: skip");
  3606. goto unlock;
  3607. }
  3608. if (!buffer_mapped(bh)) {
  3609. BUFFER_TRACE(bh, "unmapped");
  3610. ext4_get_block(inode, iblock, bh, 0);
  3611. /* unmapped? It's a hole - nothing to do */
  3612. if (!buffer_mapped(bh)) {
  3613. BUFFER_TRACE(bh, "still unmapped");
  3614. goto unlock;
  3615. }
  3616. }
  3617. /* Ok, it's mapped. Make sure it's up-to-date */
  3618. if (folio_test_uptodate(folio))
  3619. set_buffer_uptodate(bh);
  3620. if (!buffer_uptodate(bh)) {
  3621. err = ext4_read_bh_lock(bh, 0, true);
  3622. if (err)
  3623. goto unlock;
  3624. if (fscrypt_inode_uses_fs_layer_crypto(inode)) {
  3625. /* We expect the key to be set. */
  3626. BUG_ON(!fscrypt_has_encryption_key(inode));
  3627. err = fscrypt_decrypt_pagecache_blocks(folio,
  3628. blocksize,
  3629. bh_offset(bh));
  3630. if (err) {
  3631. clear_buffer_uptodate(bh);
  3632. goto unlock;
  3633. }
  3634. }
  3635. }
  3636. if (ext4_should_journal_data(inode)) {
  3637. BUFFER_TRACE(bh, "get write access");
  3638. err = ext4_journal_get_write_access(handle, inode->i_sb, bh,
  3639. EXT4_JTR_NONE);
  3640. if (err)
  3641. goto unlock;
  3642. }
  3643. folio_zero_range(folio, offset, length);
  3644. BUFFER_TRACE(bh, "zeroed end of block");
  3645. if (ext4_should_journal_data(inode)) {
  3646. err = ext4_dirty_journalled_data(handle, bh);
  3647. } else {
  3648. mark_buffer_dirty(bh);
  3649. /*
  3650. * Only the written block requires ordered data to prevent
  3651. * exposing stale data.
  3652. */
  3653. if (!buffer_unwritten(bh) && !buffer_delay(bh) &&
  3654. ext4_should_order_data(inode))
  3655. err = ext4_jbd2_inode_add_write(handle, inode, from,
  3656. length);
  3657. }
  3658. unlock:
  3659. folio_unlock(folio);
  3660. folio_put(folio);
  3661. return err;
  3662. }
  3663. /*
  3664. * ext4_block_zero_page_range() zeros out a mapping of length 'length'
  3665. * starting from file offset 'from'. The range to be zero'd must
  3666. * be contained with in one block. If the specified range exceeds
  3667. * the end of the block it will be shortened to end of the block
  3668. * that corresponds to 'from'
  3669. */
  3670. static int ext4_block_zero_page_range(handle_t *handle,
  3671. struct address_space *mapping, loff_t from, loff_t length)
  3672. {
  3673. struct inode *inode = mapping->host;
  3674. unsigned blocksize = inode->i_sb->s_blocksize;
  3675. unsigned int max = blocksize - (from & (blocksize - 1));
  3676. /*
  3677. * correct length if it does not fall between
  3678. * 'from' and the end of the block
  3679. */
  3680. if (length > max || length < 0)
  3681. length = max;
  3682. if (IS_DAX(inode)) {
  3683. return dax_zero_range(inode, from, length, NULL,
  3684. &ext4_iomap_ops);
  3685. }
  3686. return __ext4_block_zero_page_range(handle, mapping, from, length);
  3687. }
  3688. /*
  3689. * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
  3690. * up to the end of the block which corresponds to `from'.
  3691. * This required during truncate. We need to physically zero the tail end
  3692. * of that block so it doesn't yield old data if the file is later grown.
  3693. */
  3694. static int ext4_block_truncate_page(handle_t *handle,
  3695. struct address_space *mapping, loff_t from)
  3696. {
  3697. unsigned length;
  3698. unsigned blocksize;
  3699. struct inode *inode = mapping->host;
  3700. /* If we are processing an encrypted inode during orphan list handling */
  3701. if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode))
  3702. return 0;
  3703. blocksize = i_blocksize(inode);
  3704. length = blocksize - (from & (blocksize - 1));
  3705. return ext4_block_zero_page_range(handle, mapping, from, length);
  3706. }
  3707. int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
  3708. loff_t lstart, loff_t length)
  3709. {
  3710. struct super_block *sb = inode->i_sb;
  3711. struct address_space *mapping = inode->i_mapping;
  3712. unsigned partial_start, partial_end;
  3713. ext4_fsblk_t start, end;
  3714. loff_t byte_end = (lstart + length - 1);
  3715. int err = 0;
  3716. partial_start = lstart & (sb->s_blocksize - 1);
  3717. partial_end = byte_end & (sb->s_blocksize - 1);
  3718. start = lstart >> sb->s_blocksize_bits;
  3719. end = byte_end >> sb->s_blocksize_bits;
  3720. /* Handle partial zero within the single block */
  3721. if (start == end &&
  3722. (partial_start || (partial_end != sb->s_blocksize - 1))) {
  3723. err = ext4_block_zero_page_range(handle, mapping,
  3724. lstart, length);
  3725. return err;
  3726. }
  3727. /* Handle partial zero out on the start of the range */
  3728. if (partial_start) {
  3729. err = ext4_block_zero_page_range(handle, mapping,
  3730. lstart, sb->s_blocksize);
  3731. if (err)
  3732. return err;
  3733. }
  3734. /* Handle partial zero out on the end of the range */
  3735. if (partial_end != sb->s_blocksize - 1)
  3736. err = ext4_block_zero_page_range(handle, mapping,
  3737. byte_end - partial_end,
  3738. partial_end + 1);
  3739. return err;
  3740. }
  3741. int ext4_can_truncate(struct inode *inode)
  3742. {
  3743. if (S_ISREG(inode->i_mode))
  3744. return 1;
  3745. if (S_ISDIR(inode->i_mode))
  3746. return 1;
  3747. if (S_ISLNK(inode->i_mode))
  3748. return !ext4_inode_is_fast_symlink(inode);
  3749. return 0;
  3750. }
  3751. /*
  3752. * We have to make sure i_disksize gets properly updated before we truncate
  3753. * page cache due to hole punching or zero range. Otherwise i_disksize update
  3754. * can get lost as it may have been postponed to submission of writeback but
  3755. * that will never happen if we remove the folio containing i_size from the
  3756. * page cache. Also if we punch hole within i_size but above i_disksize,
  3757. * following ext4_page_mkwrite() may mistakenly allocate written blocks over
  3758. * the hole and thus introduce allocated blocks beyond i_disksize which is
  3759. * not allowed (e2fsck would complain in case of crash).
  3760. */
  3761. int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
  3762. loff_t len)
  3763. {
  3764. handle_t *handle;
  3765. int ret;
  3766. loff_t size = i_size_read(inode);
  3767. WARN_ON(!inode_is_locked(inode));
  3768. if (offset > size)
  3769. return 0;
  3770. if (offset + len < size)
  3771. size = offset + len;
  3772. if (EXT4_I(inode)->i_disksize >= size)
  3773. return 0;
  3774. handle = ext4_journal_start(inode, EXT4_HT_MISC, 1);
  3775. if (IS_ERR(handle))
  3776. return PTR_ERR(handle);
  3777. ext4_update_i_disksize(inode, size);
  3778. ret = ext4_mark_inode_dirty(handle, inode);
  3779. ext4_journal_stop(handle);
  3780. return ret;
  3781. }
  3782. static inline void ext4_truncate_folio(struct inode *inode,
  3783. loff_t start, loff_t end)
  3784. {
  3785. unsigned long blocksize = i_blocksize(inode);
  3786. struct folio *folio;
  3787. /* Nothing to be done if no complete block needs to be truncated. */
  3788. if (round_up(start, blocksize) >= round_down(end, blocksize))
  3789. return;
  3790. folio = filemap_lock_folio(inode->i_mapping, start >> PAGE_SHIFT);
  3791. if (IS_ERR(folio))
  3792. return;
  3793. if (folio_mkclean(folio))
  3794. folio_mark_dirty(folio);
  3795. folio_unlock(folio);
  3796. folio_put(folio);
  3797. }
  3798. int ext4_truncate_page_cache_block_range(struct inode *inode,
  3799. loff_t start, loff_t end)
  3800. {
  3801. unsigned long blocksize = i_blocksize(inode);
  3802. int ret;
  3803. /*
  3804. * For journalled data we need to write (and checkpoint) pages
  3805. * before discarding page cache to avoid inconsitent data on disk
  3806. * in case of crash before freeing or unwritten converting trans
  3807. * is committed.
  3808. */
  3809. if (ext4_should_journal_data(inode)) {
  3810. ret = filemap_write_and_wait_range(inode->i_mapping, start,
  3811. end - 1);
  3812. if (ret)
  3813. return ret;
  3814. goto truncate_pagecache;
  3815. }
  3816. /*
  3817. * If the block size is less than the page size, the file's mapped
  3818. * blocks within one page could be freed or converted to unwritten.
  3819. * So it's necessary to remove writable userspace mappings, and then
  3820. * ext4_page_mkwrite() can be called during subsequent write access
  3821. * to these partial folios.
  3822. */
  3823. if (!IS_ALIGNED(start | end, PAGE_SIZE) &&
  3824. blocksize < PAGE_SIZE && start < inode->i_size) {
  3825. loff_t page_boundary = round_up(start, PAGE_SIZE);
  3826. ext4_truncate_folio(inode, start, min(page_boundary, end));
  3827. if (end > page_boundary)
  3828. ext4_truncate_folio(inode,
  3829. round_down(end, PAGE_SIZE), end);
  3830. }
  3831. truncate_pagecache:
  3832. truncate_pagecache_range(inode, start, end - 1);
  3833. return 0;
  3834. }
  3835. static void ext4_wait_dax_page(struct inode *inode)
  3836. {
  3837. filemap_invalidate_unlock(inode->i_mapping);
  3838. schedule();
  3839. filemap_invalidate_lock(inode->i_mapping);
  3840. }
  3841. int ext4_break_layouts(struct inode *inode)
  3842. {
  3843. if (WARN_ON_ONCE(!rwsem_is_locked(&inode->i_mapping->invalidate_lock)))
  3844. return -EINVAL;
  3845. return dax_break_layout_inode(inode, ext4_wait_dax_page);
  3846. }
  3847. /*
  3848. * ext4_punch_hole: punches a hole in a file by releasing the blocks
  3849. * associated with the given offset and length
  3850. *
  3851. * @inode: File inode
  3852. * @offset: The offset where the hole will begin
  3853. * @len: The length of the hole
  3854. *
  3855. * Returns: 0 on success or negative on failure
  3856. */
  3857. int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
  3858. {
  3859. struct inode *inode = file_inode(file);
  3860. struct super_block *sb = inode->i_sb;
  3861. ext4_lblk_t start_lblk, end_lblk;
  3862. loff_t max_end = sb->s_maxbytes;
  3863. loff_t end = offset + length;
  3864. handle_t *handle;
  3865. unsigned int credits;
  3866. int ret;
  3867. trace_ext4_punch_hole(inode, offset, length, 0);
  3868. WARN_ON_ONCE(!inode_is_locked(inode));
  3869. /*
  3870. * For indirect-block based inodes, make sure that the hole within
  3871. * one block before last range.
  3872. */
  3873. if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3874. max_end = EXT4_SB(sb)->s_bitmap_maxbytes - sb->s_blocksize;
  3875. /* No need to punch hole beyond i_size */
  3876. if (offset >= inode->i_size || offset >= max_end)
  3877. return 0;
  3878. /*
  3879. * If the hole extends beyond i_size, set the hole to end after
  3880. * the block that contains i_size to save pointless tail block zeroing.
  3881. */
  3882. if (end >= inode->i_size)
  3883. end = round_up(inode->i_size, sb->s_blocksize);
  3884. if (end > max_end)
  3885. end = max_end;
  3886. length = end - offset;
  3887. /*
  3888. * Attach jinode to inode for jbd2 if we do any zeroing of partial
  3889. * block.
  3890. */
  3891. if (!IS_ALIGNED(offset | end, sb->s_blocksize)) {
  3892. ret = ext4_inode_attach_jinode(inode);
  3893. if (ret < 0)
  3894. return ret;
  3895. }
  3896. ret = ext4_update_disksize_before_punch(inode, offset, length);
  3897. if (ret)
  3898. return ret;
  3899. /* Now release the pages and zero block aligned part of pages*/
  3900. ret = ext4_truncate_page_cache_block_range(inode, offset, end);
  3901. if (ret)
  3902. return ret;
  3903. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3904. credits = ext4_chunk_trans_extent(inode, 2);
  3905. else
  3906. credits = ext4_blocks_for_truncate(inode);
  3907. handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
  3908. if (IS_ERR(handle)) {
  3909. ret = PTR_ERR(handle);
  3910. ext4_std_error(sb, ret);
  3911. return ret;
  3912. }
  3913. ret = ext4_zero_partial_blocks(handle, inode, offset, length);
  3914. if (ret)
  3915. goto out_handle;
  3916. /* If there are blocks to remove, do it */
  3917. start_lblk = EXT4_B_TO_LBLK(inode, offset);
  3918. end_lblk = end >> inode->i_blkbits;
  3919. if (end_lblk > start_lblk) {
  3920. ext4_lblk_t hole_len = end_lblk - start_lblk;
  3921. ext4_fc_track_inode(handle, inode);
  3922. ext4_check_map_extents_env(inode);
  3923. down_write(&EXT4_I(inode)->i_data_sem);
  3924. ext4_discard_preallocations(inode);
  3925. ext4_es_remove_extent(inode, start_lblk, hole_len);
  3926. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3927. ret = ext4_ext_remove_space(inode, start_lblk,
  3928. end_lblk - 1);
  3929. else
  3930. ret = ext4_ind_remove_space(handle, inode, start_lblk,
  3931. end_lblk);
  3932. if (ret) {
  3933. up_write(&EXT4_I(inode)->i_data_sem);
  3934. goto out_handle;
  3935. }
  3936. ext4_es_insert_extent(inode, start_lblk, hole_len, ~0,
  3937. EXTENT_STATUS_HOLE, 0);
  3938. up_write(&EXT4_I(inode)->i_data_sem);
  3939. }
  3940. ext4_fc_track_range(handle, inode, start_lblk, end_lblk);
  3941. ret = ext4_mark_inode_dirty(handle, inode);
  3942. if (unlikely(ret))
  3943. goto out_handle;
  3944. ext4_update_inode_fsync_trans(handle, inode, 1);
  3945. if (IS_SYNC(inode))
  3946. ext4_handle_sync(handle);
  3947. out_handle:
  3948. ext4_journal_stop(handle);
  3949. return ret;
  3950. }
  3951. int ext4_inode_attach_jinode(struct inode *inode)
  3952. {
  3953. struct ext4_inode_info *ei = EXT4_I(inode);
  3954. struct jbd2_inode *jinode;
  3955. if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal)
  3956. return 0;
  3957. jinode = jbd2_alloc_inode(GFP_KERNEL);
  3958. spin_lock(&inode->i_lock);
  3959. if (!ei->jinode) {
  3960. if (!jinode) {
  3961. spin_unlock(&inode->i_lock);
  3962. return -ENOMEM;
  3963. }
  3964. jbd2_journal_init_jbd_inode(jinode, inode);
  3965. /*
  3966. * Publish ->jinode only after it is fully initialized so that
  3967. * readers never observe a partially initialized jbd2_inode.
  3968. */
  3969. smp_wmb();
  3970. WRITE_ONCE(ei->jinode, jinode);
  3971. jinode = NULL;
  3972. }
  3973. spin_unlock(&inode->i_lock);
  3974. if (unlikely(jinode != NULL))
  3975. jbd2_free_inode(jinode);
  3976. return 0;
  3977. }
  3978. /*
  3979. * ext4_truncate()
  3980. *
  3981. * We block out ext4_get_block() block instantiations across the entire
  3982. * transaction, and VFS/VM ensures that ext4_truncate() cannot run
  3983. * simultaneously on behalf of the same inode.
  3984. *
  3985. * As we work through the truncate and commit bits of it to the journal there
  3986. * is one core, guiding principle: the file's tree must always be consistent on
  3987. * disk. We must be able to restart the truncate after a crash.
  3988. *
  3989. * The file's tree may be transiently inconsistent in memory (although it
  3990. * probably isn't), but whenever we close off and commit a journal transaction,
  3991. * the contents of (the filesystem + the journal) must be consistent and
  3992. * restartable. It's pretty simple, really: bottom up, right to left (although
  3993. * left-to-right works OK too).
  3994. *
  3995. * Note that at recovery time, journal replay occurs *before* the restart of
  3996. * truncate against the orphan inode list.
  3997. *
  3998. * The committed inode has the new, desired i_size (which is the same as
  3999. * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
  4000. * that this inode's truncate did not complete and it will again call
  4001. * ext4_truncate() to have another go. So there will be instantiated blocks
  4002. * to the right of the truncation point in a crashed ext4 filesystem. But
  4003. * that's fine - as long as they are linked from the inode, the post-crash
  4004. * ext4_truncate() run will find them and release them.
  4005. */
  4006. int ext4_truncate(struct inode *inode)
  4007. {
  4008. struct ext4_inode_info *ei = EXT4_I(inode);
  4009. unsigned int credits;
  4010. int err = 0, err2;
  4011. handle_t *handle;
  4012. struct address_space *mapping = inode->i_mapping;
  4013. /*
  4014. * There is a possibility that we're either freeing the inode
  4015. * or it's a completely new inode. In those cases we might not
  4016. * have i_rwsem locked because it's not necessary.
  4017. */
  4018. if (!(inode_state_read_once(inode) & (I_NEW | I_FREEING)))
  4019. WARN_ON(!inode_is_locked(inode));
  4020. trace_ext4_truncate_enter(inode);
  4021. if (!ext4_can_truncate(inode))
  4022. goto out_trace;
  4023. if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
  4024. ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
  4025. if (ext4_has_inline_data(inode)) {
  4026. int has_inline = 1;
  4027. err = ext4_inline_data_truncate(inode, &has_inline);
  4028. if (err || has_inline)
  4029. goto out_trace;
  4030. }
  4031. /* If we zero-out tail of the page, we have to create jinode for jbd2 */
  4032. if (inode->i_size & (inode->i_sb->s_blocksize - 1)) {
  4033. err = ext4_inode_attach_jinode(inode);
  4034. if (err)
  4035. goto out_trace;
  4036. }
  4037. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  4038. credits = ext4_chunk_trans_extent(inode, 1);
  4039. else
  4040. credits = ext4_blocks_for_truncate(inode);
  4041. handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
  4042. if (IS_ERR(handle)) {
  4043. err = PTR_ERR(handle);
  4044. goto out_trace;
  4045. }
  4046. if (inode->i_size & (inode->i_sb->s_blocksize - 1))
  4047. ext4_block_truncate_page(handle, mapping, inode->i_size);
  4048. /*
  4049. * We add the inode to the orphan list, so that if this
  4050. * truncate spans multiple transactions, and we crash, we will
  4051. * resume the truncate when the filesystem recovers. It also
  4052. * marks the inode dirty, to catch the new size.
  4053. *
  4054. * Implication: the file must always be in a sane, consistent
  4055. * truncatable state while each transaction commits.
  4056. */
  4057. err = ext4_orphan_add(handle, inode);
  4058. if (err)
  4059. goto out_stop;
  4060. ext4_fc_track_inode(handle, inode);
  4061. ext4_check_map_extents_env(inode);
  4062. down_write(&EXT4_I(inode)->i_data_sem);
  4063. ext4_discard_preallocations(inode);
  4064. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  4065. err = ext4_ext_truncate(handle, inode);
  4066. else
  4067. ext4_ind_truncate(handle, inode);
  4068. up_write(&ei->i_data_sem);
  4069. if (err)
  4070. goto out_stop;
  4071. if (IS_SYNC(inode))
  4072. ext4_handle_sync(handle);
  4073. out_stop:
  4074. /*
  4075. * If this was a simple ftruncate() and the file will remain alive,
  4076. * then we need to clear up the orphan record which we created above.
  4077. * However, if this was a real unlink then we were called by
  4078. * ext4_evict_inode(), and we allow that function to clean up the
  4079. * orphan info for us.
  4080. */
  4081. if (inode->i_nlink)
  4082. ext4_orphan_del(handle, inode);
  4083. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  4084. err2 = ext4_mark_inode_dirty(handle, inode);
  4085. if (unlikely(err2 && !err))
  4086. err = err2;
  4087. ext4_journal_stop(handle);
  4088. out_trace:
  4089. trace_ext4_truncate_exit(inode);
  4090. return err;
  4091. }
  4092. static inline u64 ext4_inode_peek_iversion(const struct inode *inode)
  4093. {
  4094. if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
  4095. return inode_peek_iversion_raw(inode);
  4096. else
  4097. return inode_peek_iversion(inode);
  4098. }
  4099. static int ext4_inode_blocks_set(struct ext4_inode *raw_inode,
  4100. struct ext4_inode_info *ei)
  4101. {
  4102. struct inode *inode = &(ei->vfs_inode);
  4103. u64 i_blocks = READ_ONCE(inode->i_blocks);
  4104. struct super_block *sb = inode->i_sb;
  4105. if (i_blocks <= ~0U) {
  4106. /*
  4107. * i_blocks can be represented in a 32 bit variable
  4108. * as multiple of 512 bytes
  4109. */
  4110. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  4111. raw_inode->i_blocks_high = 0;
  4112. ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
  4113. return 0;
  4114. }
  4115. /*
  4116. * This should never happen since sb->s_maxbytes should not have
  4117. * allowed this, sb->s_maxbytes was set according to the huge_file
  4118. * feature in ext4_fill_super().
  4119. */
  4120. if (!ext4_has_feature_huge_file(sb))
  4121. return -EFSCORRUPTED;
  4122. if (i_blocks <= 0xffffffffffffULL) {
  4123. /*
  4124. * i_blocks can be represented in a 48 bit variable
  4125. * as multiple of 512 bytes
  4126. */
  4127. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  4128. raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
  4129. ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
  4130. } else {
  4131. ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
  4132. /* i_block is stored in file system block size */
  4133. i_blocks = i_blocks >> (inode->i_blkbits - 9);
  4134. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  4135. raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
  4136. }
  4137. return 0;
  4138. }
  4139. static int ext4_fill_raw_inode(struct inode *inode, struct ext4_inode *raw_inode)
  4140. {
  4141. struct ext4_inode_info *ei = EXT4_I(inode);
  4142. uid_t i_uid;
  4143. gid_t i_gid;
  4144. projid_t i_projid;
  4145. int block;
  4146. int err;
  4147. err = ext4_inode_blocks_set(raw_inode, ei);
  4148. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  4149. i_uid = i_uid_read(inode);
  4150. i_gid = i_gid_read(inode);
  4151. i_projid = from_kprojid(&init_user_ns, ei->i_projid);
  4152. if (!(test_opt(inode->i_sb, NO_UID32))) {
  4153. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
  4154. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
  4155. /*
  4156. * Fix up interoperability with old kernels. Otherwise,
  4157. * old inodes get re-used with the upper 16 bits of the
  4158. * uid/gid intact.
  4159. */
  4160. if (ei->i_dtime && !ext4_inode_orphan_tracked(inode)) {
  4161. raw_inode->i_uid_high = 0;
  4162. raw_inode->i_gid_high = 0;
  4163. } else {
  4164. raw_inode->i_uid_high =
  4165. cpu_to_le16(high_16_bits(i_uid));
  4166. raw_inode->i_gid_high =
  4167. cpu_to_le16(high_16_bits(i_gid));
  4168. }
  4169. } else {
  4170. raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
  4171. raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
  4172. raw_inode->i_uid_high = 0;
  4173. raw_inode->i_gid_high = 0;
  4174. }
  4175. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  4176. EXT4_INODE_SET_CTIME(inode, raw_inode);
  4177. EXT4_INODE_SET_MTIME(inode, raw_inode);
  4178. EXT4_INODE_SET_ATIME(inode, raw_inode);
  4179. EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
  4180. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  4181. raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
  4182. if (likely(!test_opt2(inode->i_sb, HURD_COMPAT)))
  4183. raw_inode->i_file_acl_high =
  4184. cpu_to_le16(ei->i_file_acl >> 32);
  4185. raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
  4186. ext4_isize_set(raw_inode, ei->i_disksize);
  4187. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  4188. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  4189. if (old_valid_dev(inode->i_rdev)) {
  4190. raw_inode->i_block[0] =
  4191. cpu_to_le32(old_encode_dev(inode->i_rdev));
  4192. raw_inode->i_block[1] = 0;
  4193. } else {
  4194. raw_inode->i_block[0] = 0;
  4195. raw_inode->i_block[1] =
  4196. cpu_to_le32(new_encode_dev(inode->i_rdev));
  4197. raw_inode->i_block[2] = 0;
  4198. }
  4199. } else if (!ext4_has_inline_data(inode)) {
  4200. for (block = 0; block < EXT4_N_BLOCKS; block++)
  4201. raw_inode->i_block[block] = ei->i_data[block];
  4202. }
  4203. if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
  4204. u64 ivers = ext4_inode_peek_iversion(inode);
  4205. raw_inode->i_disk_version = cpu_to_le32(ivers);
  4206. if (ei->i_extra_isize) {
  4207. if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
  4208. raw_inode->i_version_hi =
  4209. cpu_to_le32(ivers >> 32);
  4210. raw_inode->i_extra_isize =
  4211. cpu_to_le16(ei->i_extra_isize);
  4212. }
  4213. }
  4214. if (i_projid != EXT4_DEF_PROJID &&
  4215. !ext4_has_feature_project(inode->i_sb))
  4216. err = err ?: -EFSCORRUPTED;
  4217. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
  4218. EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
  4219. raw_inode->i_projid = cpu_to_le32(i_projid);
  4220. ext4_inode_csum_set(inode, raw_inode, ei);
  4221. return err;
  4222. }
  4223. /*
  4224. * ext4_get_inode_loc returns with an extra refcount against the inode's
  4225. * underlying buffer_head on success. If we pass 'inode' and it does not
  4226. * have in-inode xattr, we have all inode data in memory that is needed
  4227. * to recreate the on-disk version of this inode.
  4228. */
  4229. static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino,
  4230. struct inode *inode, struct ext4_iloc *iloc,
  4231. ext4_fsblk_t *ret_block)
  4232. {
  4233. struct ext4_group_desc *gdp;
  4234. struct buffer_head *bh;
  4235. ext4_fsblk_t block;
  4236. struct blk_plug plug;
  4237. int inodes_per_block, inode_offset;
  4238. iloc->bh = NULL;
  4239. if (ino < EXT4_ROOT_INO ||
  4240. ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
  4241. return -EFSCORRUPTED;
  4242. iloc->block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  4243. gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
  4244. if (!gdp)
  4245. return -EIO;
  4246. /*
  4247. * Figure out the offset within the block group inode table
  4248. */
  4249. inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
  4250. inode_offset = ((ino - 1) %
  4251. EXT4_INODES_PER_GROUP(sb));
  4252. iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
  4253. block = ext4_inode_table(sb, gdp);
  4254. if ((block <= le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) ||
  4255. (block >= ext4_blocks_count(EXT4_SB(sb)->s_es))) {
  4256. ext4_error(sb, "Invalid inode table block %llu in "
  4257. "block_group %u", block, iloc->block_group);
  4258. return -EFSCORRUPTED;
  4259. }
  4260. block += (inode_offset / inodes_per_block);
  4261. bh = sb_getblk(sb, block);
  4262. if (unlikely(!bh))
  4263. return -ENOMEM;
  4264. if (ext4_buffer_uptodate(bh))
  4265. goto has_buffer;
  4266. lock_buffer(bh);
  4267. if (ext4_buffer_uptodate(bh)) {
  4268. /* Someone brought it uptodate while we waited */
  4269. unlock_buffer(bh);
  4270. goto has_buffer;
  4271. }
  4272. /*
  4273. * If we have all information of the inode in memory and this
  4274. * is the only valid inode in the block, we need not read the
  4275. * block.
  4276. */
  4277. if (inode && !ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  4278. struct buffer_head *bitmap_bh;
  4279. int i, start;
  4280. start = inode_offset & ~(inodes_per_block - 1);
  4281. /* Is the inode bitmap in cache? */
  4282. bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
  4283. if (unlikely(!bitmap_bh))
  4284. goto make_io;
  4285. /*
  4286. * If the inode bitmap isn't in cache then the
  4287. * optimisation may end up performing two reads instead
  4288. * of one, so skip it.
  4289. */
  4290. if (!buffer_uptodate(bitmap_bh)) {
  4291. brelse(bitmap_bh);
  4292. goto make_io;
  4293. }
  4294. for (i = start; i < start + inodes_per_block; i++) {
  4295. if (i == inode_offset)
  4296. continue;
  4297. if (ext4_test_bit(i, bitmap_bh->b_data))
  4298. break;
  4299. }
  4300. brelse(bitmap_bh);
  4301. if (i == start + inodes_per_block) {
  4302. struct ext4_inode *raw_inode =
  4303. (struct ext4_inode *) (bh->b_data + iloc->offset);
  4304. /* all other inodes are free, so skip I/O */
  4305. memset(bh->b_data, 0, bh->b_size);
  4306. if (!ext4_test_inode_state(inode, EXT4_STATE_NEW))
  4307. ext4_fill_raw_inode(inode, raw_inode);
  4308. set_buffer_uptodate(bh);
  4309. unlock_buffer(bh);
  4310. goto has_buffer;
  4311. }
  4312. }
  4313. make_io:
  4314. /*
  4315. * If we need to do any I/O, try to pre-readahead extra
  4316. * blocks from the inode table.
  4317. */
  4318. blk_start_plug(&plug);
  4319. if (EXT4_SB(sb)->s_inode_readahead_blks) {
  4320. ext4_fsblk_t b, end, table;
  4321. unsigned num;
  4322. __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks;
  4323. table = ext4_inode_table(sb, gdp);
  4324. /* s_inode_readahead_blks is always a power of 2 */
  4325. b = block & ~((ext4_fsblk_t) ra_blks - 1);
  4326. if (table > b)
  4327. b = table;
  4328. end = b + ra_blks;
  4329. num = EXT4_INODES_PER_GROUP(sb);
  4330. if (ext4_has_group_desc_csum(sb))
  4331. num -= ext4_itable_unused_count(sb, gdp);
  4332. table += num / inodes_per_block;
  4333. if (end > table)
  4334. end = table;
  4335. while (b <= end)
  4336. ext4_sb_breadahead_unmovable(sb, b++);
  4337. }
  4338. /*
  4339. * There are other valid inodes in the buffer, this inode
  4340. * has in-inode xattrs, or we don't have this inode in memory.
  4341. * Read the block from disk.
  4342. */
  4343. trace_ext4_load_inode(sb, ino);
  4344. ext4_read_bh_nowait(bh, REQ_META | REQ_PRIO, NULL,
  4345. ext4_simulate_fail(sb, EXT4_SIM_INODE_EIO));
  4346. blk_finish_plug(&plug);
  4347. wait_on_buffer(bh);
  4348. if (!buffer_uptodate(bh)) {
  4349. if (ret_block)
  4350. *ret_block = block;
  4351. brelse(bh);
  4352. return -EIO;
  4353. }
  4354. has_buffer:
  4355. iloc->bh = bh;
  4356. return 0;
  4357. }
  4358. static int __ext4_get_inode_loc_noinmem(struct inode *inode,
  4359. struct ext4_iloc *iloc)
  4360. {
  4361. ext4_fsblk_t err_blk = 0;
  4362. int ret;
  4363. ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, NULL, iloc,
  4364. &err_blk);
  4365. if (ret == -EIO)
  4366. ext4_error_inode_block(inode, err_blk, EIO,
  4367. "unable to read itable block");
  4368. return ret;
  4369. }
  4370. int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
  4371. {
  4372. ext4_fsblk_t err_blk = 0;
  4373. int ret;
  4374. ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, inode, iloc,
  4375. &err_blk);
  4376. if (ret == -EIO)
  4377. ext4_error_inode_block(inode, err_blk, EIO,
  4378. "unable to read itable block");
  4379. return ret;
  4380. }
  4381. int ext4_get_fc_inode_loc(struct super_block *sb, unsigned long ino,
  4382. struct ext4_iloc *iloc)
  4383. {
  4384. return __ext4_get_inode_loc(sb, ino, NULL, iloc, NULL);
  4385. }
  4386. static bool ext4_should_enable_dax(struct inode *inode)
  4387. {
  4388. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  4389. if (test_opt2(inode->i_sb, DAX_NEVER))
  4390. return false;
  4391. if (!S_ISREG(inode->i_mode))
  4392. return false;
  4393. if (ext4_should_journal_data(inode))
  4394. return false;
  4395. if (ext4_has_inline_data(inode))
  4396. return false;
  4397. if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT))
  4398. return false;
  4399. if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY))
  4400. return false;
  4401. if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags))
  4402. return false;
  4403. if (test_opt(inode->i_sb, DAX_ALWAYS))
  4404. return true;
  4405. return ext4_test_inode_flag(inode, EXT4_INODE_DAX);
  4406. }
  4407. void ext4_set_inode_flags(struct inode *inode, bool init)
  4408. {
  4409. unsigned int flags = EXT4_I(inode)->i_flags;
  4410. unsigned int new_fl = 0;
  4411. WARN_ON_ONCE(IS_DAX(inode) && init);
  4412. if (flags & EXT4_SYNC_FL)
  4413. new_fl |= S_SYNC;
  4414. if (flags & EXT4_APPEND_FL)
  4415. new_fl |= S_APPEND;
  4416. if (flags & EXT4_IMMUTABLE_FL)
  4417. new_fl |= S_IMMUTABLE;
  4418. if (flags & EXT4_NOATIME_FL)
  4419. new_fl |= S_NOATIME;
  4420. if (flags & EXT4_DIRSYNC_FL)
  4421. new_fl |= S_DIRSYNC;
  4422. /* Because of the way inode_set_flags() works we must preserve S_DAX
  4423. * here if already set. */
  4424. new_fl |= (inode->i_flags & S_DAX);
  4425. if (init && ext4_should_enable_dax(inode))
  4426. new_fl |= S_DAX;
  4427. if (flags & EXT4_ENCRYPT_FL)
  4428. new_fl |= S_ENCRYPTED;
  4429. if (flags & EXT4_CASEFOLD_FL)
  4430. new_fl |= S_CASEFOLD;
  4431. if (flags & EXT4_VERITY_FL)
  4432. new_fl |= S_VERITY;
  4433. inode_set_flags(inode, new_fl,
  4434. S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX|
  4435. S_ENCRYPTED|S_CASEFOLD|S_VERITY);
  4436. }
  4437. static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
  4438. struct ext4_inode_info *ei)
  4439. {
  4440. blkcnt_t i_blocks ;
  4441. struct inode *inode = &(ei->vfs_inode);
  4442. struct super_block *sb = inode->i_sb;
  4443. if (ext4_has_feature_huge_file(sb)) {
  4444. /* we are using combined 48 bit field */
  4445. i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
  4446. le32_to_cpu(raw_inode->i_blocks_lo);
  4447. if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
  4448. /* i_blocks represent file system block size */
  4449. return i_blocks << (inode->i_blkbits - 9);
  4450. } else {
  4451. return i_blocks;
  4452. }
  4453. } else {
  4454. return le32_to_cpu(raw_inode->i_blocks_lo);
  4455. }
  4456. }
  4457. static inline int ext4_iget_extra_inode(struct inode *inode,
  4458. struct ext4_inode *raw_inode,
  4459. struct ext4_inode_info *ei)
  4460. {
  4461. __le32 *magic = (void *)raw_inode +
  4462. EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
  4463. if (EXT4_INODE_HAS_XATTR_SPACE(inode) &&
  4464. *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
  4465. int err;
  4466. err = xattr_check_inode(inode, IHDR(inode, raw_inode),
  4467. ITAIL(inode, raw_inode));
  4468. if (err)
  4469. return err;
  4470. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  4471. err = ext4_find_inline_data_nolock(inode);
  4472. if (!err && ext4_has_inline_data(inode))
  4473. ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  4474. return err;
  4475. } else
  4476. EXT4_I(inode)->i_inline_off = 0;
  4477. return 0;
  4478. }
  4479. int ext4_get_projid(struct inode *inode, kprojid_t *projid)
  4480. {
  4481. if (!ext4_has_feature_project(inode->i_sb))
  4482. return -EOPNOTSUPP;
  4483. *projid = EXT4_I(inode)->i_projid;
  4484. return 0;
  4485. }
  4486. /*
  4487. * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of
  4488. * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag
  4489. * set.
  4490. */
  4491. static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val)
  4492. {
  4493. if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
  4494. inode_set_iversion_raw(inode, val);
  4495. else
  4496. inode_set_iversion_queried(inode, val);
  4497. }
  4498. static int check_igot_inode(struct inode *inode, ext4_iget_flags flags,
  4499. const char *function, unsigned int line)
  4500. {
  4501. const char *err_str;
  4502. if (flags & EXT4_IGET_EA_INODE) {
  4503. if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
  4504. err_str = "missing EA_INODE flag";
  4505. goto error;
  4506. }
  4507. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
  4508. EXT4_I(inode)->i_file_acl) {
  4509. err_str = "ea_inode with extended attributes";
  4510. goto error;
  4511. }
  4512. } else {
  4513. if ((EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
  4514. /*
  4515. * open_by_handle_at() could provide an old inode number
  4516. * that has since been reused for an ea_inode; this does
  4517. * not indicate filesystem corruption
  4518. */
  4519. if (flags & EXT4_IGET_HANDLE)
  4520. return -ESTALE;
  4521. err_str = "unexpected EA_INODE flag";
  4522. goto error;
  4523. }
  4524. }
  4525. if (is_bad_inode(inode) && !(flags & EXT4_IGET_BAD)) {
  4526. err_str = "unexpected bad inode w/o EXT4_IGET_BAD";
  4527. goto error;
  4528. }
  4529. return 0;
  4530. error:
  4531. ext4_error_inode(inode, function, line, 0, "%s", err_str);
  4532. return -EFSCORRUPTED;
  4533. }
  4534. void ext4_set_inode_mapping_order(struct inode *inode)
  4535. {
  4536. struct super_block *sb = inode->i_sb;
  4537. u16 min_order, max_order;
  4538. max_order = EXT4_SB(sb)->s_max_folio_order;
  4539. if (!max_order)
  4540. return;
  4541. min_order = EXT4_SB(sb)->s_min_folio_order;
  4542. if (!min_order && !S_ISREG(inode->i_mode))
  4543. return;
  4544. if (ext4_test_inode_flag(inode, EXT4_INODE_JOURNAL_DATA))
  4545. max_order = min_order;
  4546. mapping_set_folio_order_range(inode->i_mapping, min_order, max_order);
  4547. }
  4548. struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
  4549. ext4_iget_flags flags, const char *function,
  4550. unsigned int line)
  4551. {
  4552. struct ext4_iloc iloc;
  4553. struct ext4_inode *raw_inode;
  4554. struct ext4_inode_info *ei;
  4555. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  4556. struct inode *inode;
  4557. journal_t *journal = EXT4_SB(sb)->s_journal;
  4558. long ret;
  4559. loff_t size;
  4560. int block;
  4561. uid_t i_uid;
  4562. gid_t i_gid;
  4563. projid_t i_projid;
  4564. if ((!(flags & EXT4_IGET_SPECIAL) && is_special_ino(sb, ino)) ||
  4565. (ino < EXT4_ROOT_INO) ||
  4566. (ino > le32_to_cpu(es->s_inodes_count))) {
  4567. if (flags & EXT4_IGET_HANDLE)
  4568. return ERR_PTR(-ESTALE);
  4569. __ext4_error(sb, function, line, false, EFSCORRUPTED, 0,
  4570. "inode #%lu: comm %s: iget: illegal inode #",
  4571. ino, current->comm);
  4572. return ERR_PTR(-EFSCORRUPTED);
  4573. }
  4574. inode = iget_locked(sb, ino);
  4575. if (!inode)
  4576. return ERR_PTR(-ENOMEM);
  4577. if (!(inode_state_read_once(inode) & I_NEW)) {
  4578. ret = check_igot_inode(inode, flags, function, line);
  4579. if (ret) {
  4580. iput(inode);
  4581. return ERR_PTR(ret);
  4582. }
  4583. return inode;
  4584. }
  4585. ei = EXT4_I(inode);
  4586. iloc.bh = NULL;
  4587. ret = __ext4_get_inode_loc_noinmem(inode, &iloc);
  4588. if (ret < 0)
  4589. goto bad_inode;
  4590. raw_inode = ext4_raw_inode(&iloc);
  4591. if ((flags & EXT4_IGET_HANDLE) &&
  4592. (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) {
  4593. ret = -ESTALE;
  4594. goto bad_inode;
  4595. }
  4596. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  4597. ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
  4598. if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
  4599. EXT4_INODE_SIZE(inode->i_sb) ||
  4600. (ei->i_extra_isize & 3)) {
  4601. ext4_error_inode(inode, function, line, 0,
  4602. "iget: bad extra_isize %u "
  4603. "(inode size %u)",
  4604. ei->i_extra_isize,
  4605. EXT4_INODE_SIZE(inode->i_sb));
  4606. ret = -EFSCORRUPTED;
  4607. goto bad_inode;
  4608. }
  4609. } else
  4610. ei->i_extra_isize = 0;
  4611. /* Precompute checksum seed for inode metadata */
  4612. if (ext4_has_feature_metadata_csum(sb)) {
  4613. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  4614. __u32 csum;
  4615. __le32 inum = cpu_to_le32(inode->i_ino);
  4616. __le32 gen = raw_inode->i_generation;
  4617. csum = ext4_chksum(sbi->s_csum_seed, (__u8 *)&inum,
  4618. sizeof(inum));
  4619. ei->i_csum_seed = ext4_chksum(csum, (__u8 *)&gen, sizeof(gen));
  4620. }
  4621. if ((!ext4_inode_csum_verify(inode, raw_inode, ei) ||
  4622. ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) &&
  4623. (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))) {
  4624. ext4_error_inode_err(inode, function, line, 0,
  4625. EFSBADCRC, "iget: checksum invalid");
  4626. ret = -EFSBADCRC;
  4627. goto bad_inode;
  4628. }
  4629. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  4630. i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  4631. i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  4632. if (ext4_has_feature_project(sb) &&
  4633. EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE &&
  4634. EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
  4635. i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid);
  4636. else
  4637. i_projid = EXT4_DEF_PROJID;
  4638. if (!(test_opt(inode->i_sb, NO_UID32))) {
  4639. i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  4640. i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  4641. }
  4642. i_uid_write(inode, i_uid);
  4643. i_gid_write(inode, i_gid);
  4644. ei->i_projid = make_kprojid(&init_user_ns, i_projid);
  4645. set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
  4646. ei->i_inline_off = 0;
  4647. ei->i_dir_start_lookup = 0;
  4648. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  4649. /* We now have enough fields to check if the inode was active or not.
  4650. * This is needed because nfsd might try to access dead inodes
  4651. * the test is that same one that e2fsck uses
  4652. * NeilBrown 1999oct15
  4653. */
  4654. if (inode->i_nlink == 0) {
  4655. if ((inode->i_mode == 0 || flags & EXT4_IGET_SPECIAL ||
  4656. !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
  4657. ino != EXT4_BOOT_LOADER_INO) {
  4658. /* this inode is deleted or unallocated */
  4659. if (flags & EXT4_IGET_SPECIAL) {
  4660. ext4_error_inode(inode, function, line, 0,
  4661. "iget: special inode unallocated");
  4662. ret = -EFSCORRUPTED;
  4663. } else
  4664. ret = -ESTALE;
  4665. goto bad_inode;
  4666. }
  4667. /* The only unlinked inodes we let through here have
  4668. * valid i_mode and are being read by the orphan
  4669. * recovery code: that's fine, we're about to complete
  4670. * the process of deleting those.
  4671. * OR it is the EXT4_BOOT_LOADER_INO which is
  4672. * not initialized on a new filesystem. */
  4673. }
  4674. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  4675. ext4_set_inode_flags(inode, true);
  4676. /* Detect invalid flag combination - can't have both inline data and extents */
  4677. if (ext4_test_inode_flag(inode, EXT4_INODE_INLINE_DATA) &&
  4678. ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  4679. ext4_error_inode(inode, function, line, 0,
  4680. "inode has both inline data and extents flags");
  4681. ret = -EFSCORRUPTED;
  4682. goto bad_inode;
  4683. }
  4684. inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
  4685. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
  4686. if (ext4_has_feature_64bit(sb))
  4687. ei->i_file_acl |=
  4688. ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
  4689. inode->i_size = ext4_isize(sb, raw_inode);
  4690. size = i_size_read(inode);
  4691. if (size < 0 || size > ext4_get_maxbytes(inode)) {
  4692. ext4_error_inode(inode, function, line, 0,
  4693. "iget: bad i_size value: %lld", size);
  4694. ret = -EFSCORRUPTED;
  4695. goto bad_inode;
  4696. }
  4697. /*
  4698. * If dir_index is not enabled but there's dir with INDEX flag set,
  4699. * we'd normally treat htree data as empty space. But with metadata
  4700. * checksumming that corrupts checksums so forbid that.
  4701. */
  4702. if (!ext4_has_feature_dir_index(sb) &&
  4703. ext4_has_feature_metadata_csum(sb) &&
  4704. ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) {
  4705. ext4_error_inode(inode, function, line, 0,
  4706. "iget: Dir with htree data on filesystem without dir_index feature.");
  4707. ret = -EFSCORRUPTED;
  4708. goto bad_inode;
  4709. }
  4710. ei->i_disksize = inode->i_size;
  4711. #ifdef CONFIG_QUOTA
  4712. ei->i_reserved_quota = 0;
  4713. #endif
  4714. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  4715. ei->i_block_group = iloc.block_group;
  4716. ei->i_last_alloc_group = ~0;
  4717. /*
  4718. * NOTE! The in-memory inode i_data array is in little-endian order
  4719. * even on big-endian machines: we do NOT byteswap the block numbers!
  4720. */
  4721. for (block = 0; block < EXT4_N_BLOCKS; block++)
  4722. ei->i_data[block] = raw_inode->i_block[block];
  4723. INIT_LIST_HEAD(&ei->i_orphan);
  4724. ext4_fc_init_inode(&ei->vfs_inode);
  4725. /*
  4726. * Set transaction id's of transactions that have to be committed
  4727. * to finish f[data]sync. We set them to currently running transaction
  4728. * as we cannot be sure that the inode or some of its metadata isn't
  4729. * part of the transaction - the inode could have been reclaimed and
  4730. * now it is reread from disk.
  4731. */
  4732. if (journal) {
  4733. transaction_t *transaction;
  4734. tid_t tid;
  4735. read_lock(&journal->j_state_lock);
  4736. if (journal->j_running_transaction)
  4737. transaction = journal->j_running_transaction;
  4738. else
  4739. transaction = journal->j_committing_transaction;
  4740. if (transaction)
  4741. tid = transaction->t_tid;
  4742. else
  4743. tid = journal->j_commit_sequence;
  4744. read_unlock(&journal->j_state_lock);
  4745. ei->i_sync_tid = tid;
  4746. ei->i_datasync_tid = tid;
  4747. }
  4748. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  4749. if (ei->i_extra_isize == 0) {
  4750. /* The extra space is currently unused. Use it. */
  4751. BUILD_BUG_ON(sizeof(struct ext4_inode) & 3);
  4752. ei->i_extra_isize = sizeof(struct ext4_inode) -
  4753. EXT4_GOOD_OLD_INODE_SIZE;
  4754. } else {
  4755. ret = ext4_iget_extra_inode(inode, raw_inode, ei);
  4756. if (ret)
  4757. goto bad_inode;
  4758. }
  4759. }
  4760. EXT4_INODE_GET_CTIME(inode, raw_inode);
  4761. EXT4_INODE_GET_ATIME(inode, raw_inode);
  4762. EXT4_INODE_GET_MTIME(inode, raw_inode);
  4763. EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
  4764. if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
  4765. u64 ivers = le32_to_cpu(raw_inode->i_disk_version);
  4766. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  4767. if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
  4768. ivers |=
  4769. (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
  4770. }
  4771. ext4_inode_set_iversion_queried(inode, ivers);
  4772. }
  4773. ret = 0;
  4774. if (ei->i_file_acl &&
  4775. !ext4_inode_block_valid(inode, ei->i_file_acl, 1)) {
  4776. ext4_error_inode(inode, function, line, 0,
  4777. "iget: bad extended attribute block %llu",
  4778. ei->i_file_acl);
  4779. ret = -EFSCORRUPTED;
  4780. goto bad_inode;
  4781. } else if (!ext4_has_inline_data(inode)) {
  4782. /* validate the block references in the inode */
  4783. if (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) &&
  4784. (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  4785. (S_ISLNK(inode->i_mode) &&
  4786. !ext4_inode_is_fast_symlink(inode)))) {
  4787. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  4788. ret = ext4_ext_check_inode(inode);
  4789. else
  4790. ret = ext4_ind_check_inode(inode);
  4791. }
  4792. }
  4793. if (ret)
  4794. goto bad_inode;
  4795. if (S_ISREG(inode->i_mode)) {
  4796. inode->i_op = &ext4_file_inode_operations;
  4797. inode->i_fop = &ext4_file_operations;
  4798. ext4_set_aops(inode);
  4799. } else if (S_ISDIR(inode->i_mode)) {
  4800. inode->i_op = &ext4_dir_inode_operations;
  4801. inode->i_fop = &ext4_dir_operations;
  4802. } else if (S_ISLNK(inode->i_mode)) {
  4803. /* VFS does not allow setting these so must be corruption */
  4804. if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) {
  4805. ext4_error_inode(inode, function, line, 0,
  4806. "iget: immutable or append flags "
  4807. "not allowed on symlinks");
  4808. ret = -EFSCORRUPTED;
  4809. goto bad_inode;
  4810. }
  4811. if (IS_ENCRYPTED(inode)) {
  4812. inode->i_op = &ext4_encrypted_symlink_inode_operations;
  4813. } else if (ext4_inode_is_fast_symlink(inode)) {
  4814. inode->i_op = &ext4_fast_symlink_inode_operations;
  4815. /*
  4816. * Orphan cleanup can see inodes with i_size == 0
  4817. * and i_data uninitialized. Skip size checks in
  4818. * that case. This is safe because the first thing
  4819. * ext4_evict_inode() does for fast symlinks is
  4820. * clearing of i_data and i_size.
  4821. */
  4822. if ((EXT4_SB(sb)->s_mount_state & EXT4_ORPHAN_FS)) {
  4823. if (inode->i_nlink != 0) {
  4824. ext4_error_inode(inode, function, line, 0,
  4825. "invalid orphan symlink nlink %d",
  4826. inode->i_nlink);
  4827. ret = -EFSCORRUPTED;
  4828. goto bad_inode;
  4829. }
  4830. } else {
  4831. if (inode->i_size == 0 ||
  4832. inode->i_size >= sizeof(ei->i_data) ||
  4833. strnlen((char *)ei->i_data, inode->i_size + 1) !=
  4834. inode->i_size) {
  4835. ext4_error_inode(inode, function, line, 0,
  4836. "invalid fast symlink length %llu",
  4837. (unsigned long long)inode->i_size);
  4838. ret = -EFSCORRUPTED;
  4839. goto bad_inode;
  4840. }
  4841. inode_set_cached_link(inode, (char *)ei->i_data,
  4842. inode->i_size);
  4843. }
  4844. } else {
  4845. inode->i_op = &ext4_symlink_inode_operations;
  4846. }
  4847. } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  4848. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  4849. inode->i_op = &ext4_special_inode_operations;
  4850. if (raw_inode->i_block[0])
  4851. init_special_inode(inode, inode->i_mode,
  4852. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  4853. else
  4854. init_special_inode(inode, inode->i_mode,
  4855. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  4856. } else if (ino == EXT4_BOOT_LOADER_INO) {
  4857. make_bad_inode(inode);
  4858. } else {
  4859. ret = -EFSCORRUPTED;
  4860. ext4_error_inode(inode, function, line, 0,
  4861. "iget: bogus i_mode (%o)", inode->i_mode);
  4862. goto bad_inode;
  4863. }
  4864. if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb)) {
  4865. ext4_error_inode(inode, function, line, 0,
  4866. "casefold flag without casefold feature");
  4867. ret = -EFSCORRUPTED;
  4868. goto bad_inode;
  4869. }
  4870. ext4_set_inode_mapping_order(inode);
  4871. ret = check_igot_inode(inode, flags, function, line);
  4872. /*
  4873. * -ESTALE here means there is nothing inherently wrong with the inode,
  4874. * it's just not an inode we can return for an fhandle lookup.
  4875. */
  4876. if (ret == -ESTALE) {
  4877. brelse(iloc.bh);
  4878. unlock_new_inode(inode);
  4879. iput(inode);
  4880. return ERR_PTR(-ESTALE);
  4881. }
  4882. if (ret)
  4883. goto bad_inode;
  4884. brelse(iloc.bh);
  4885. /* Initialize the "no ACL's" state for the simple cases */
  4886. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) && !ei->i_file_acl)
  4887. cache_no_acl(inode);
  4888. unlock_new_inode(inode);
  4889. return inode;
  4890. bad_inode:
  4891. brelse(iloc.bh);
  4892. iget_failed(inode);
  4893. return ERR_PTR(ret);
  4894. }
  4895. static void __ext4_update_other_inode_time(struct super_block *sb,
  4896. unsigned long orig_ino,
  4897. unsigned long ino,
  4898. struct ext4_inode *raw_inode)
  4899. {
  4900. struct inode *inode;
  4901. inode = find_inode_by_ino_rcu(sb, ino);
  4902. if (!inode)
  4903. return;
  4904. if (!inode_is_dirtytime_only(inode))
  4905. return;
  4906. spin_lock(&inode->i_lock);
  4907. if (inode_is_dirtytime_only(inode)) {
  4908. struct ext4_inode_info *ei = EXT4_I(inode);
  4909. inode_state_clear(inode, I_DIRTY_TIME);
  4910. spin_unlock(&inode->i_lock);
  4911. spin_lock(&ei->i_raw_lock);
  4912. EXT4_INODE_SET_CTIME(inode, raw_inode);
  4913. EXT4_INODE_SET_MTIME(inode, raw_inode);
  4914. EXT4_INODE_SET_ATIME(inode, raw_inode);
  4915. ext4_inode_csum_set(inode, raw_inode, ei);
  4916. spin_unlock(&ei->i_raw_lock);
  4917. trace_ext4_other_inode_update_time(inode, orig_ino);
  4918. return;
  4919. }
  4920. spin_unlock(&inode->i_lock);
  4921. }
  4922. /*
  4923. * Opportunistically update the other time fields for other inodes in
  4924. * the same inode table block.
  4925. */
  4926. static void ext4_update_other_inodes_time(struct super_block *sb,
  4927. unsigned long orig_ino, char *buf)
  4928. {
  4929. unsigned long ino;
  4930. int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
  4931. int inode_size = EXT4_INODE_SIZE(sb);
  4932. /*
  4933. * Calculate the first inode in the inode table block. Inode
  4934. * numbers are one-based. That is, the first inode in a block
  4935. * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1).
  4936. */
  4937. ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1;
  4938. rcu_read_lock();
  4939. for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) {
  4940. if (ino == orig_ino)
  4941. continue;
  4942. __ext4_update_other_inode_time(sb, orig_ino, ino,
  4943. (struct ext4_inode *)buf);
  4944. }
  4945. rcu_read_unlock();
  4946. }
  4947. /*
  4948. * Post the struct inode info into an on-disk inode location in the
  4949. * buffer-cache. This gobbles the caller's reference to the
  4950. * buffer_head in the inode location struct.
  4951. *
  4952. * The caller must have write access to iloc->bh.
  4953. */
  4954. static int ext4_do_update_inode(handle_t *handle,
  4955. struct inode *inode,
  4956. struct ext4_iloc *iloc)
  4957. {
  4958. struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
  4959. struct ext4_inode_info *ei = EXT4_I(inode);
  4960. struct buffer_head *bh = iloc->bh;
  4961. struct super_block *sb = inode->i_sb;
  4962. int err;
  4963. int need_datasync = 0, set_large_file = 0;
  4964. spin_lock(&ei->i_raw_lock);
  4965. /*
  4966. * For fields not tracked in the in-memory inode, initialise them
  4967. * to zero for new inodes.
  4968. */
  4969. if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
  4970. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  4971. if (READ_ONCE(ei->i_disksize) != ext4_isize(inode->i_sb, raw_inode))
  4972. need_datasync = 1;
  4973. if (ei->i_disksize > 0x7fffffffULL) {
  4974. if (!ext4_has_feature_large_file(sb) ||
  4975. EXT4_SB(sb)->s_es->s_rev_level == cpu_to_le32(EXT4_GOOD_OLD_REV))
  4976. set_large_file = 1;
  4977. }
  4978. err = ext4_fill_raw_inode(inode, raw_inode);
  4979. spin_unlock(&ei->i_raw_lock);
  4980. if (err) {
  4981. EXT4_ERROR_INODE(inode, "corrupted inode contents");
  4982. goto out_brelse;
  4983. }
  4984. if (inode->i_sb->s_flags & SB_LAZYTIME)
  4985. ext4_update_other_inodes_time(inode->i_sb, inode->i_ino,
  4986. bh->b_data);
  4987. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  4988. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  4989. if (err)
  4990. goto out_error;
  4991. ext4_clear_inode_state(inode, EXT4_STATE_NEW);
  4992. if (set_large_file) {
  4993. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access");
  4994. err = ext4_journal_get_write_access(handle, sb,
  4995. EXT4_SB(sb)->s_sbh,
  4996. EXT4_JTR_NONE);
  4997. if (err)
  4998. goto out_error;
  4999. lock_buffer(EXT4_SB(sb)->s_sbh);
  5000. ext4_set_feature_large_file(sb);
  5001. ext4_superblock_csum_set(sb);
  5002. unlock_buffer(EXT4_SB(sb)->s_sbh);
  5003. ext4_handle_sync(handle);
  5004. err = ext4_handle_dirty_metadata(handle, NULL,
  5005. EXT4_SB(sb)->s_sbh);
  5006. }
  5007. ext4_update_inode_fsync_trans(handle, inode, need_datasync);
  5008. out_error:
  5009. ext4_std_error(inode->i_sb, err);
  5010. out_brelse:
  5011. brelse(bh);
  5012. return err;
  5013. }
  5014. /*
  5015. * ext4_write_inode()
  5016. *
  5017. * We are called from a few places:
  5018. *
  5019. * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files.
  5020. * Here, there will be no transaction running. We wait for any running
  5021. * transaction to commit.
  5022. *
  5023. * - Within flush work (sys_sync(), kupdate and such).
  5024. * We wait on commit, if told to.
  5025. *
  5026. * - Within iput_final() -> write_inode_now()
  5027. * We wait on commit, if told to.
  5028. *
  5029. * In all cases it is actually safe for us to return without doing anything,
  5030. * because the inode has been copied into a raw inode buffer in
  5031. * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL
  5032. * writeback.
  5033. *
  5034. * Note that we are absolutely dependent upon all inode dirtiers doing the
  5035. * right thing: they *must* call mark_inode_dirty() after dirtying info in
  5036. * which we are interested.
  5037. *
  5038. * It would be a bug for them to not do this. The code:
  5039. *
  5040. * mark_inode_dirty(inode)
  5041. * stuff();
  5042. * inode->i_size = expr;
  5043. *
  5044. * is in error because write_inode() could occur while `stuff()' is running,
  5045. * and the new i_size will be lost. Plus the inode will no longer be on the
  5046. * superblock's dirty inode list.
  5047. */
  5048. int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
  5049. {
  5050. int err;
  5051. if (WARN_ON_ONCE(current->flags & PF_MEMALLOC))
  5052. return 0;
  5053. err = ext4_emergency_state(inode->i_sb);
  5054. if (unlikely(err))
  5055. return err;
  5056. if (EXT4_SB(inode->i_sb)->s_journal) {
  5057. if (ext4_journal_current_handle()) {
  5058. ext4_debug("called recursively, non-PF_MEMALLOC!\n");
  5059. dump_stack();
  5060. return -EIO;
  5061. }
  5062. /*
  5063. * No need to force transaction in WB_SYNC_NONE mode. Also
  5064. * ext4_sync_fs() will force the commit after everything is
  5065. * written.
  5066. */
  5067. if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync)
  5068. return 0;
  5069. err = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal,
  5070. EXT4_I(inode)->i_sync_tid);
  5071. } else {
  5072. struct ext4_iloc iloc;
  5073. err = __ext4_get_inode_loc_noinmem(inode, &iloc);
  5074. if (err)
  5075. return err;
  5076. /*
  5077. * sync(2) will flush the whole buffer cache. No need to do
  5078. * it here separately for each inode.
  5079. */
  5080. if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
  5081. sync_dirty_buffer(iloc.bh);
  5082. if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
  5083. ext4_error_inode_block(inode, iloc.bh->b_blocknr, EIO,
  5084. "IO error syncing inode");
  5085. err = -EIO;
  5086. }
  5087. brelse(iloc.bh);
  5088. }
  5089. return err;
  5090. }
  5091. /*
  5092. * In data=journal mode ext4_journalled_invalidate_folio() may fail to invalidate
  5093. * buffers that are attached to a folio straddling i_size and are undergoing
  5094. * commit. In that case we have to wait for commit to finish and try again.
  5095. */
  5096. static void ext4_wait_for_tail_page_commit(struct inode *inode)
  5097. {
  5098. unsigned offset;
  5099. journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
  5100. tid_t commit_tid;
  5101. int ret;
  5102. bool has_transaction;
  5103. offset = inode->i_size & (PAGE_SIZE - 1);
  5104. /*
  5105. * If the folio is fully truncated, we don't need to wait for any commit
  5106. * (and we even should not as __ext4_journalled_invalidate_folio() may
  5107. * strip all buffers from the folio but keep the folio dirty which can then
  5108. * confuse e.g. concurrent ext4_writepages() seeing dirty folio without
  5109. * buffers). Also we don't need to wait for any commit if all buffers in
  5110. * the folio remain valid. This is most beneficial for the common case of
  5111. * blocksize == PAGESIZE.
  5112. */
  5113. if (!offset || offset > (PAGE_SIZE - i_blocksize(inode)))
  5114. return;
  5115. while (1) {
  5116. struct folio *folio = filemap_lock_folio(inode->i_mapping,
  5117. inode->i_size >> PAGE_SHIFT);
  5118. if (IS_ERR(folio))
  5119. return;
  5120. ret = __ext4_journalled_invalidate_folio(folio, offset,
  5121. folio_size(folio) - offset);
  5122. folio_unlock(folio);
  5123. folio_put(folio);
  5124. if (ret != -EBUSY)
  5125. return;
  5126. has_transaction = false;
  5127. read_lock(&journal->j_state_lock);
  5128. if (journal->j_committing_transaction) {
  5129. commit_tid = journal->j_committing_transaction->t_tid;
  5130. has_transaction = true;
  5131. }
  5132. read_unlock(&journal->j_state_lock);
  5133. if (has_transaction)
  5134. jbd2_log_wait_commit(journal, commit_tid);
  5135. }
  5136. }
  5137. /*
  5138. * ext4_setattr()
  5139. *
  5140. * Called from notify_change.
  5141. *
  5142. * We want to trap VFS attempts to truncate the file as soon as
  5143. * possible. In particular, we want to make sure that when the VFS
  5144. * shrinks i_size, we put the inode on the orphan list and modify
  5145. * i_disksize immediately, so that during the subsequent flushing of
  5146. * dirty pages and freeing of disk blocks, we can guarantee that any
  5147. * commit will leave the blocks being flushed in an unused state on
  5148. * disk. (On recovery, the inode will get truncated and the blocks will
  5149. * be freed, so we have a strong guarantee that no future commit will
  5150. * leave these blocks visible to the user.)
  5151. *
  5152. * Another thing we have to assure is that if we are in ordered mode
  5153. * and inode is still attached to the committing transaction, we must
  5154. * we start writeout of all the dirty pages which are being truncated.
  5155. * This way we are sure that all the data written in the previous
  5156. * transaction are already on disk (truncate waits for pages under
  5157. * writeback).
  5158. *
  5159. * Called with inode->i_rwsem down.
  5160. */
  5161. int ext4_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  5162. struct iattr *attr)
  5163. {
  5164. struct inode *inode = d_inode(dentry);
  5165. int error, rc = 0;
  5166. int orphan = 0;
  5167. const unsigned int ia_valid = attr->ia_valid;
  5168. bool inc_ivers = true;
  5169. error = ext4_emergency_state(inode->i_sb);
  5170. if (unlikely(error))
  5171. return error;
  5172. if (unlikely(IS_IMMUTABLE(inode)))
  5173. return -EPERM;
  5174. if (unlikely(IS_APPEND(inode) &&
  5175. (ia_valid & (ATTR_MODE | ATTR_UID |
  5176. ATTR_GID | ATTR_TIMES_SET))))
  5177. return -EPERM;
  5178. error = setattr_prepare(idmap, dentry, attr);
  5179. if (error)
  5180. return error;
  5181. error = fscrypt_prepare_setattr(dentry, attr);
  5182. if (error)
  5183. return error;
  5184. if (is_quota_modification(idmap, inode, attr)) {
  5185. error = dquot_initialize(inode);
  5186. if (error)
  5187. return error;
  5188. }
  5189. if (i_uid_needs_update(idmap, attr, inode) ||
  5190. i_gid_needs_update(idmap, attr, inode)) {
  5191. handle_t *handle;
  5192. /* (user+group)*(old+new) structure, inode write (sb,
  5193. * inode block, ? - but truncate inode update has it) */
  5194. handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
  5195. (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
  5196. EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
  5197. if (IS_ERR(handle)) {
  5198. error = PTR_ERR(handle);
  5199. goto err_out;
  5200. }
  5201. /* dquot_transfer() calls back ext4_get_inode_usage() which
  5202. * counts xattr inode references.
  5203. */
  5204. down_read(&EXT4_I(inode)->xattr_sem);
  5205. error = dquot_transfer(idmap, inode, attr);
  5206. up_read(&EXT4_I(inode)->xattr_sem);
  5207. if (error) {
  5208. ext4_journal_stop(handle);
  5209. return error;
  5210. }
  5211. /* Update corresponding info in inode so that everything is in
  5212. * one transaction */
  5213. i_uid_update(idmap, attr, inode);
  5214. i_gid_update(idmap, attr, inode);
  5215. error = ext4_mark_inode_dirty(handle, inode);
  5216. ext4_journal_stop(handle);
  5217. if (unlikely(error)) {
  5218. return error;
  5219. }
  5220. }
  5221. if (attr->ia_valid & ATTR_SIZE) {
  5222. handle_t *handle;
  5223. loff_t oldsize = inode->i_size;
  5224. loff_t old_disksize;
  5225. int shrink = (attr->ia_size < inode->i_size);
  5226. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  5227. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  5228. if (attr->ia_size > sbi->s_bitmap_maxbytes) {
  5229. return -EFBIG;
  5230. }
  5231. }
  5232. if (!S_ISREG(inode->i_mode)) {
  5233. return -EINVAL;
  5234. }
  5235. if (attr->ia_size == inode->i_size)
  5236. inc_ivers = false;
  5237. /*
  5238. * If file has inline data but new size exceeds inline capacity,
  5239. * convert to extent-based storage first to prevent inconsistent
  5240. * state (inline flag set but size exceeds inline capacity).
  5241. */
  5242. if (ext4_has_inline_data(inode) &&
  5243. attr->ia_size > EXT4_I(inode)->i_inline_size) {
  5244. error = ext4_convert_inline_data(inode);
  5245. if (error)
  5246. goto err_out;
  5247. }
  5248. if (shrink) {
  5249. if (ext4_should_order_data(inode)) {
  5250. error = ext4_begin_ordered_truncate(inode,
  5251. attr->ia_size);
  5252. if (error)
  5253. goto err_out;
  5254. }
  5255. /*
  5256. * Blocks are going to be removed from the inode. Wait
  5257. * for dio in flight.
  5258. */
  5259. inode_dio_wait(inode);
  5260. }
  5261. filemap_invalidate_lock(inode->i_mapping);
  5262. rc = ext4_break_layouts(inode);
  5263. if (rc) {
  5264. filemap_invalidate_unlock(inode->i_mapping);
  5265. goto err_out;
  5266. }
  5267. if (attr->ia_size != inode->i_size) {
  5268. /* attach jbd2 jinode for EOF folio tail zeroing */
  5269. if (attr->ia_size & (inode->i_sb->s_blocksize - 1) ||
  5270. oldsize & (inode->i_sb->s_blocksize - 1)) {
  5271. error = ext4_inode_attach_jinode(inode);
  5272. if (error)
  5273. goto out_mmap_sem;
  5274. }
  5275. handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
  5276. if (IS_ERR(handle)) {
  5277. error = PTR_ERR(handle);
  5278. goto out_mmap_sem;
  5279. }
  5280. if (ext4_handle_valid(handle) && shrink) {
  5281. error = ext4_orphan_add(handle, inode);
  5282. orphan = 1;
  5283. }
  5284. /*
  5285. * Update c/mtime and tail zero the EOF folio on
  5286. * truncate up. ext4_truncate() handles the shrink case
  5287. * below.
  5288. */
  5289. if (!shrink) {
  5290. inode_set_mtime_to_ts(inode,
  5291. inode_set_ctime_current(inode));
  5292. if (oldsize & (inode->i_sb->s_blocksize - 1))
  5293. ext4_block_truncate_page(handle,
  5294. inode->i_mapping, oldsize);
  5295. }
  5296. if (shrink)
  5297. ext4_fc_track_range(handle, inode,
  5298. (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >>
  5299. inode->i_sb->s_blocksize_bits,
  5300. EXT_MAX_BLOCKS - 1);
  5301. else
  5302. ext4_fc_track_range(
  5303. handle, inode,
  5304. (oldsize > 0 ? oldsize - 1 : oldsize) >>
  5305. inode->i_sb->s_blocksize_bits,
  5306. (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >>
  5307. inode->i_sb->s_blocksize_bits);
  5308. down_write(&EXT4_I(inode)->i_data_sem);
  5309. old_disksize = EXT4_I(inode)->i_disksize;
  5310. EXT4_I(inode)->i_disksize = attr->ia_size;
  5311. /*
  5312. * We have to update i_size under i_data_sem together
  5313. * with i_disksize to avoid races with writeback code
  5314. * running ext4_wb_update_i_disksize().
  5315. */
  5316. if (!error)
  5317. i_size_write(inode, attr->ia_size);
  5318. else
  5319. EXT4_I(inode)->i_disksize = old_disksize;
  5320. up_write(&EXT4_I(inode)->i_data_sem);
  5321. rc = ext4_mark_inode_dirty(handle, inode);
  5322. if (!error)
  5323. error = rc;
  5324. ext4_journal_stop(handle);
  5325. if (error)
  5326. goto out_mmap_sem;
  5327. if (!shrink) {
  5328. pagecache_isize_extended(inode, oldsize,
  5329. inode->i_size);
  5330. } else if (ext4_should_journal_data(inode)) {
  5331. ext4_wait_for_tail_page_commit(inode);
  5332. }
  5333. }
  5334. /*
  5335. * Truncate pagecache after we've waited for commit
  5336. * in data=journal mode to make pages freeable.
  5337. */
  5338. truncate_pagecache(inode, inode->i_size);
  5339. /*
  5340. * Call ext4_truncate() even if i_size didn't change to
  5341. * truncate possible preallocated blocks.
  5342. */
  5343. if (attr->ia_size <= oldsize) {
  5344. rc = ext4_truncate(inode);
  5345. if (rc)
  5346. error = rc;
  5347. }
  5348. out_mmap_sem:
  5349. filemap_invalidate_unlock(inode->i_mapping);
  5350. }
  5351. if (!error) {
  5352. if (inc_ivers)
  5353. inode_inc_iversion(inode);
  5354. setattr_copy(idmap, inode, attr);
  5355. mark_inode_dirty(inode);
  5356. }
  5357. /*
  5358. * If the call to ext4_truncate failed to get a transaction handle at
  5359. * all, we need to clean up the in-core orphan list manually.
  5360. */
  5361. if (orphan && inode->i_nlink)
  5362. ext4_orphan_del(NULL, inode);
  5363. if (!error && (ia_valid & ATTR_MODE))
  5364. rc = posix_acl_chmod(idmap, dentry, inode->i_mode);
  5365. err_out:
  5366. if (error)
  5367. ext4_std_error(inode->i_sb, error);
  5368. if (!error)
  5369. error = rc;
  5370. return error;
  5371. }
  5372. u32 ext4_dio_alignment(struct inode *inode)
  5373. {
  5374. if (fsverity_active(inode))
  5375. return 0;
  5376. if (ext4_should_journal_data(inode))
  5377. return 0;
  5378. if (ext4_has_inline_data(inode))
  5379. return 0;
  5380. if (IS_ENCRYPTED(inode)) {
  5381. if (!fscrypt_dio_supported(inode))
  5382. return 0;
  5383. return i_blocksize(inode);
  5384. }
  5385. return 1; /* use the iomap defaults */
  5386. }
  5387. int ext4_getattr(struct mnt_idmap *idmap, const struct path *path,
  5388. struct kstat *stat, u32 request_mask, unsigned int query_flags)
  5389. {
  5390. struct inode *inode = d_inode(path->dentry);
  5391. struct ext4_inode *raw_inode;
  5392. struct ext4_inode_info *ei = EXT4_I(inode);
  5393. unsigned int flags;
  5394. if ((request_mask & STATX_BTIME) &&
  5395. EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) {
  5396. stat->result_mask |= STATX_BTIME;
  5397. stat->btime.tv_sec = ei->i_crtime.tv_sec;
  5398. stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
  5399. }
  5400. /*
  5401. * Return the DIO alignment restrictions if requested. We only return
  5402. * this information when requested, since on encrypted files it might
  5403. * take a fair bit of work to get if the file wasn't opened recently.
  5404. */
  5405. if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) {
  5406. u32 dio_align = ext4_dio_alignment(inode);
  5407. stat->result_mask |= STATX_DIOALIGN;
  5408. if (dio_align == 1) {
  5409. struct block_device *bdev = inode->i_sb->s_bdev;
  5410. /* iomap defaults */
  5411. stat->dio_mem_align = bdev_dma_alignment(bdev) + 1;
  5412. stat->dio_offset_align = bdev_logical_block_size(bdev);
  5413. } else {
  5414. stat->dio_mem_align = dio_align;
  5415. stat->dio_offset_align = dio_align;
  5416. }
  5417. }
  5418. if ((request_mask & STATX_WRITE_ATOMIC) && S_ISREG(inode->i_mode)) {
  5419. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  5420. unsigned int awu_min = 0, awu_max = 0;
  5421. if (ext4_inode_can_atomic_write(inode)) {
  5422. awu_min = sbi->s_awu_min;
  5423. awu_max = sbi->s_awu_max;
  5424. }
  5425. generic_fill_statx_atomic_writes(stat, awu_min, awu_max, 0);
  5426. }
  5427. flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
  5428. if (flags & EXT4_APPEND_FL)
  5429. stat->attributes |= STATX_ATTR_APPEND;
  5430. if (flags & EXT4_COMPR_FL)
  5431. stat->attributes |= STATX_ATTR_COMPRESSED;
  5432. if (flags & EXT4_ENCRYPT_FL)
  5433. stat->attributes |= STATX_ATTR_ENCRYPTED;
  5434. if (flags & EXT4_IMMUTABLE_FL)
  5435. stat->attributes |= STATX_ATTR_IMMUTABLE;
  5436. if (flags & EXT4_NODUMP_FL)
  5437. stat->attributes |= STATX_ATTR_NODUMP;
  5438. if (flags & EXT4_VERITY_FL)
  5439. stat->attributes |= STATX_ATTR_VERITY;
  5440. stat->attributes_mask |= (STATX_ATTR_APPEND |
  5441. STATX_ATTR_COMPRESSED |
  5442. STATX_ATTR_ENCRYPTED |
  5443. STATX_ATTR_IMMUTABLE |
  5444. STATX_ATTR_NODUMP |
  5445. STATX_ATTR_VERITY);
  5446. generic_fillattr(idmap, request_mask, inode, stat);
  5447. return 0;
  5448. }
  5449. int ext4_file_getattr(struct mnt_idmap *idmap,
  5450. const struct path *path, struct kstat *stat,
  5451. u32 request_mask, unsigned int query_flags)
  5452. {
  5453. struct inode *inode = d_inode(path->dentry);
  5454. u64 delalloc_blocks;
  5455. ext4_getattr(idmap, path, stat, request_mask, query_flags);
  5456. /*
  5457. * If there is inline data in the inode, the inode will normally not
  5458. * have data blocks allocated (it may have an external xattr block).
  5459. * Report at least one sector for such files, so tools like tar, rsync,
  5460. * others don't incorrectly think the file is completely sparse.
  5461. */
  5462. if (unlikely(ext4_has_inline_data(inode)))
  5463. stat->blocks += (stat->size + 511) >> 9;
  5464. /*
  5465. * We can't update i_blocks if the block allocation is delayed
  5466. * otherwise in the case of system crash before the real block
  5467. * allocation is done, we will have i_blocks inconsistent with
  5468. * on-disk file blocks.
  5469. * We always keep i_blocks updated together with real
  5470. * allocation. But to not confuse with user, stat
  5471. * will return the blocks that include the delayed allocation
  5472. * blocks for this file.
  5473. */
  5474. delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
  5475. EXT4_I(inode)->i_reserved_data_blocks);
  5476. stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9);
  5477. return 0;
  5478. }
  5479. static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
  5480. int pextents)
  5481. {
  5482. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  5483. return ext4_ind_trans_blocks(inode, lblocks);
  5484. return ext4_ext_index_trans_blocks(inode, pextents);
  5485. }
  5486. /*
  5487. * Account for index blocks, block groups bitmaps and block group
  5488. * descriptor blocks if modify datablocks and index blocks
  5489. * worse case, the indexs blocks spread over different block groups
  5490. *
  5491. * If datablocks are discontiguous, they are possible to spread over
  5492. * different block groups too. If they are contiguous, with flexbg,
  5493. * they could still across block group boundary.
  5494. *
  5495. * Also account for superblock, inode, quota and xattr blocks
  5496. */
  5497. int ext4_meta_trans_blocks(struct inode *inode, int lblocks, int pextents)
  5498. {
  5499. ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
  5500. int gdpblocks;
  5501. int idxblocks;
  5502. int ret;
  5503. /*
  5504. * How many index and leaf blocks need to touch to map @lblocks
  5505. * logical blocks to @pextents physical extents?
  5506. */
  5507. idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents);
  5508. /*
  5509. * Now let's see how many group bitmaps and group descriptors need
  5510. * to account
  5511. */
  5512. groups = idxblocks + pextents;
  5513. gdpblocks = groups;
  5514. if (groups > ngroups)
  5515. groups = ngroups;
  5516. if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
  5517. gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
  5518. /* bitmaps and block group descriptor blocks */
  5519. ret = idxblocks + groups + gdpblocks;
  5520. /* Blocks for super block, inode, quota and xattr blocks */
  5521. ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
  5522. return ret;
  5523. }
  5524. /*
  5525. * Calculate the journal credits for modifying the number of blocks
  5526. * in a single extent within one transaction. 'nrblocks' is used only
  5527. * for non-extent inodes. For extent type inodes, 'nrblocks' can be
  5528. * zero if the exact number of blocks is unknown.
  5529. */
  5530. int ext4_chunk_trans_extent(struct inode *inode, int nrblocks)
  5531. {
  5532. int ret;
  5533. ret = ext4_meta_trans_blocks(inode, nrblocks, 1);
  5534. /* Account for data blocks for journalled mode */
  5535. if (ext4_should_journal_data(inode))
  5536. ret += nrblocks;
  5537. return ret;
  5538. }
  5539. /*
  5540. * Calculate the journal credits for a chunk of data modification.
  5541. *
  5542. * This is called from DIO, fallocate or whoever calling
  5543. * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
  5544. *
  5545. * journal buffers for data blocks are not included here, as DIO
  5546. * and fallocate do no need to journal data buffers.
  5547. */
  5548. int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
  5549. {
  5550. return ext4_meta_trans_blocks(inode, nrblocks, 1);
  5551. }
  5552. /*
  5553. * The caller must have previously called ext4_reserve_inode_write().
  5554. * Give this, we know that the caller already has write access to iloc->bh.
  5555. */
  5556. int ext4_mark_iloc_dirty(handle_t *handle,
  5557. struct inode *inode, struct ext4_iloc *iloc)
  5558. {
  5559. int err = 0;
  5560. err = ext4_emergency_state(inode->i_sb);
  5561. if (unlikely(err)) {
  5562. put_bh(iloc->bh);
  5563. return err;
  5564. }
  5565. ext4_fc_track_inode(handle, inode);
  5566. /* the do_update_inode consumes one bh->b_count */
  5567. get_bh(iloc->bh);
  5568. /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
  5569. err = ext4_do_update_inode(handle, inode, iloc);
  5570. put_bh(iloc->bh);
  5571. return err;
  5572. }
  5573. /*
  5574. * On success, We end up with an outstanding reference count against
  5575. * iloc->bh. This _must_ be cleaned up later.
  5576. */
  5577. int
  5578. ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
  5579. struct ext4_iloc *iloc)
  5580. {
  5581. int err;
  5582. err = ext4_emergency_state(inode->i_sb);
  5583. if (unlikely(err))
  5584. return err;
  5585. err = ext4_get_inode_loc(inode, iloc);
  5586. if (!err) {
  5587. BUFFER_TRACE(iloc->bh, "get_write_access");
  5588. err = ext4_journal_get_write_access(handle, inode->i_sb,
  5589. iloc->bh, EXT4_JTR_NONE);
  5590. if (err) {
  5591. brelse(iloc->bh);
  5592. iloc->bh = NULL;
  5593. }
  5594. ext4_fc_track_inode(handle, inode);
  5595. }
  5596. ext4_std_error(inode->i_sb, err);
  5597. return err;
  5598. }
  5599. static int __ext4_expand_extra_isize(struct inode *inode,
  5600. unsigned int new_extra_isize,
  5601. struct ext4_iloc *iloc,
  5602. handle_t *handle, int *no_expand)
  5603. {
  5604. struct ext4_inode *raw_inode;
  5605. struct ext4_xattr_ibody_header *header;
  5606. unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb);
  5607. struct ext4_inode_info *ei = EXT4_I(inode);
  5608. int error;
  5609. /* this was checked at iget time, but double check for good measure */
  5610. if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) ||
  5611. (ei->i_extra_isize & 3)) {
  5612. EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)",
  5613. ei->i_extra_isize,
  5614. EXT4_INODE_SIZE(inode->i_sb));
  5615. return -EFSCORRUPTED;
  5616. }
  5617. if ((new_extra_isize < ei->i_extra_isize) ||
  5618. (new_extra_isize < 4) ||
  5619. (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE))
  5620. return -EINVAL; /* Should never happen */
  5621. raw_inode = ext4_raw_inode(iloc);
  5622. header = IHDR(inode, raw_inode);
  5623. /* No extended attributes present */
  5624. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
  5625. header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
  5626. memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE +
  5627. EXT4_I(inode)->i_extra_isize, 0,
  5628. new_extra_isize - EXT4_I(inode)->i_extra_isize);
  5629. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  5630. return 0;
  5631. }
  5632. /*
  5633. * We may need to allocate external xattr block so we need quotas
  5634. * initialized. Here we can be called with various locks held so we
  5635. * cannot affort to initialize quotas ourselves. So just bail.
  5636. */
  5637. if (dquot_initialize_needed(inode))
  5638. return -EAGAIN;
  5639. /* try to expand with EAs present */
  5640. error = ext4_expand_extra_isize_ea(inode, new_extra_isize,
  5641. raw_inode, handle);
  5642. if (error) {
  5643. /*
  5644. * Inode size expansion failed; don't try again
  5645. */
  5646. *no_expand = 1;
  5647. }
  5648. return error;
  5649. }
  5650. /*
  5651. * Expand an inode by new_extra_isize bytes.
  5652. * Returns 0 on success or negative error number on failure.
  5653. */
  5654. static int ext4_try_to_expand_extra_isize(struct inode *inode,
  5655. unsigned int new_extra_isize,
  5656. struct ext4_iloc iloc,
  5657. handle_t *handle)
  5658. {
  5659. int no_expand;
  5660. int error;
  5661. if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND))
  5662. return -EOVERFLOW;
  5663. /*
  5664. * In nojournal mode, we can immediately attempt to expand
  5665. * the inode. When journaled, we first need to obtain extra
  5666. * buffer credits since we may write into the EA block
  5667. * with this same handle. If journal_extend fails, then it will
  5668. * only result in a minor loss of functionality for that inode.
  5669. * If this is felt to be critical, then e2fsck should be run to
  5670. * force a large enough s_min_extra_isize.
  5671. */
  5672. if (ext4_journal_extend(handle,
  5673. EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0)
  5674. return -ENOSPC;
  5675. if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
  5676. return -EBUSY;
  5677. error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc,
  5678. handle, &no_expand);
  5679. ext4_write_unlock_xattr(inode, &no_expand);
  5680. return error;
  5681. }
  5682. int ext4_expand_extra_isize(struct inode *inode,
  5683. unsigned int new_extra_isize,
  5684. struct ext4_iloc *iloc)
  5685. {
  5686. handle_t *handle;
  5687. int no_expand;
  5688. int error, rc;
  5689. if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
  5690. brelse(iloc->bh);
  5691. return -EOVERFLOW;
  5692. }
  5693. handle = ext4_journal_start(inode, EXT4_HT_INODE,
  5694. EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
  5695. if (IS_ERR(handle)) {
  5696. error = PTR_ERR(handle);
  5697. brelse(iloc->bh);
  5698. return error;
  5699. }
  5700. ext4_write_lock_xattr(inode, &no_expand);
  5701. BUFFER_TRACE(iloc->bh, "get_write_access");
  5702. error = ext4_journal_get_write_access(handle, inode->i_sb, iloc->bh,
  5703. EXT4_JTR_NONE);
  5704. if (error) {
  5705. brelse(iloc->bh);
  5706. goto out_unlock;
  5707. }
  5708. error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc,
  5709. handle, &no_expand);
  5710. rc = ext4_mark_iloc_dirty(handle, inode, iloc);
  5711. if (!error)
  5712. error = rc;
  5713. out_unlock:
  5714. ext4_write_unlock_xattr(inode, &no_expand);
  5715. ext4_journal_stop(handle);
  5716. return error;
  5717. }
  5718. /*
  5719. * What we do here is to mark the in-core inode as clean with respect to inode
  5720. * dirtiness (it may still be data-dirty).
  5721. * This means that the in-core inode may be reaped by prune_icache
  5722. * without having to perform any I/O. This is a very good thing,
  5723. * because *any* task may call prune_icache - even ones which
  5724. * have a transaction open against a different journal.
  5725. *
  5726. * Is this cheating? Not really. Sure, we haven't written the
  5727. * inode out, but prune_icache isn't a user-visible syncing function.
  5728. * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
  5729. * we start and wait on commits.
  5730. */
  5731. int __ext4_mark_inode_dirty(handle_t *handle, struct inode *inode,
  5732. const char *func, unsigned int line)
  5733. {
  5734. struct ext4_iloc iloc;
  5735. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  5736. int err;
  5737. might_sleep();
  5738. trace_ext4_mark_inode_dirty(inode, _RET_IP_);
  5739. err = ext4_reserve_inode_write(handle, inode, &iloc);
  5740. if (err)
  5741. goto out;
  5742. if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize)
  5743. ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize,
  5744. iloc, handle);
  5745. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  5746. out:
  5747. if (unlikely(err))
  5748. ext4_error_inode_err(inode, func, line, 0, err,
  5749. "mark_inode_dirty error");
  5750. return err;
  5751. }
  5752. /*
  5753. * ext4_dirty_inode() is called from __mark_inode_dirty()
  5754. *
  5755. * We're really interested in the case where a file is being extended.
  5756. * i_size has been changed by generic_commit_write() and we thus need
  5757. * to include the updated inode in the current transaction.
  5758. *
  5759. * Also, dquot_alloc_block() will always dirty the inode when blocks
  5760. * are allocated to the file.
  5761. *
  5762. * If the inode is marked synchronous, we don't honour that here - doing
  5763. * so would cause a commit on atime updates, which we don't bother doing.
  5764. * We handle synchronous inodes at the highest possible level.
  5765. */
  5766. void ext4_dirty_inode(struct inode *inode, int flags)
  5767. {
  5768. handle_t *handle;
  5769. handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
  5770. if (IS_ERR(handle))
  5771. return;
  5772. ext4_mark_inode_dirty(handle, inode);
  5773. ext4_journal_stop(handle);
  5774. }
  5775. int ext4_change_inode_journal_flag(struct inode *inode, int val)
  5776. {
  5777. journal_t *journal;
  5778. handle_t *handle;
  5779. int err;
  5780. int alloc_ctx;
  5781. /*
  5782. * We have to be very careful here: changing a data block's
  5783. * journaling status dynamically is dangerous. If we write a
  5784. * data block to the journal, change the status and then delete
  5785. * that block, we risk forgetting to revoke the old log record
  5786. * from the journal and so a subsequent replay can corrupt data.
  5787. * So, first we make sure that the journal is empty and that
  5788. * nobody is changing anything.
  5789. */
  5790. journal = EXT4_JOURNAL(inode);
  5791. if (!journal)
  5792. return 0;
  5793. if (is_journal_aborted(journal))
  5794. return -EROFS;
  5795. /* Wait for all existing dio workers */
  5796. inode_dio_wait(inode);
  5797. /*
  5798. * Before flushing the journal and switching inode's aops, we have
  5799. * to flush all dirty data the inode has. There can be outstanding
  5800. * delayed allocations, there can be unwritten extents created by
  5801. * fallocate or buffered writes in dioread_nolock mode covered by
  5802. * dirty data which can be converted only after flushing the dirty
  5803. * data (and journalled aops don't know how to handle these cases).
  5804. */
  5805. filemap_invalidate_lock(inode->i_mapping);
  5806. err = filemap_write_and_wait(inode->i_mapping);
  5807. if (err < 0) {
  5808. filemap_invalidate_unlock(inode->i_mapping);
  5809. return err;
  5810. }
  5811. /* Before switch the inode journalling mode evict all the page cache. */
  5812. truncate_pagecache(inode, 0);
  5813. alloc_ctx = ext4_writepages_down_write(inode->i_sb);
  5814. jbd2_journal_lock_updates(journal);
  5815. /*
  5816. * OK, there are no updates running now, and all cached data is
  5817. * synced to disk. We are now in a completely consistent state
  5818. * which doesn't have anything in the journal, and we know that
  5819. * no filesystem updates are running, so it is safe to modify
  5820. * the inode's in-core data-journaling state flag now.
  5821. */
  5822. if (val)
  5823. ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
  5824. else {
  5825. err = jbd2_journal_flush(journal, 0);
  5826. if (err < 0) {
  5827. jbd2_journal_unlock_updates(journal);
  5828. ext4_writepages_up_write(inode->i_sb, alloc_ctx);
  5829. filemap_invalidate_unlock(inode->i_mapping);
  5830. return err;
  5831. }
  5832. ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
  5833. }
  5834. ext4_set_aops(inode);
  5835. ext4_set_inode_mapping_order(inode);
  5836. jbd2_journal_unlock_updates(journal);
  5837. ext4_writepages_up_write(inode->i_sb, alloc_ctx);
  5838. filemap_invalidate_unlock(inode->i_mapping);
  5839. /* Finally we can mark the inode as dirty. */
  5840. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  5841. if (IS_ERR(handle))
  5842. return PTR_ERR(handle);
  5843. ext4_fc_mark_ineligible(inode->i_sb,
  5844. EXT4_FC_REASON_JOURNAL_FLAG_CHANGE, handle);
  5845. err = ext4_mark_inode_dirty(handle, inode);
  5846. ext4_handle_sync(handle);
  5847. ext4_journal_stop(handle);
  5848. ext4_std_error(inode->i_sb, err);
  5849. return err;
  5850. }
  5851. static int ext4_bh_unmapped(handle_t *handle, struct inode *inode,
  5852. struct buffer_head *bh)
  5853. {
  5854. return !buffer_mapped(bh);
  5855. }
  5856. static int ext4_block_page_mkwrite(struct inode *inode, struct folio *folio,
  5857. get_block_t get_block)
  5858. {
  5859. handle_t *handle;
  5860. loff_t size;
  5861. unsigned long len;
  5862. int credits;
  5863. int ret;
  5864. credits = ext4_chunk_trans_extent(inode,
  5865. ext4_journal_blocks_per_folio(inode));
  5866. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, credits);
  5867. if (IS_ERR(handle))
  5868. return PTR_ERR(handle);
  5869. folio_lock(folio);
  5870. size = i_size_read(inode);
  5871. /* Page got truncated from under us? */
  5872. if (folio->mapping != inode->i_mapping || folio_pos(folio) > size) {
  5873. ret = -EFAULT;
  5874. goto out_error;
  5875. }
  5876. len = folio_size(folio);
  5877. if (folio_pos(folio) + len > size)
  5878. len = size - folio_pos(folio);
  5879. ret = ext4_block_write_begin(handle, folio, 0, len, get_block);
  5880. if (ret)
  5881. goto out_error;
  5882. if (!ext4_should_journal_data(inode)) {
  5883. block_commit_write(folio, 0, len);
  5884. folio_mark_dirty(folio);
  5885. } else {
  5886. ret = ext4_journal_folio_buffers(handle, folio, len);
  5887. if (ret)
  5888. goto out_error;
  5889. }
  5890. ext4_journal_stop(handle);
  5891. folio_wait_stable(folio);
  5892. return ret;
  5893. out_error:
  5894. folio_unlock(folio);
  5895. ext4_journal_stop(handle);
  5896. return ret;
  5897. }
  5898. vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf)
  5899. {
  5900. struct vm_area_struct *vma = vmf->vma;
  5901. struct folio *folio = page_folio(vmf->page);
  5902. loff_t size;
  5903. unsigned long len;
  5904. int err;
  5905. vm_fault_t ret;
  5906. struct file *file = vma->vm_file;
  5907. struct inode *inode = file_inode(file);
  5908. struct address_space *mapping = inode->i_mapping;
  5909. get_block_t *get_block = ext4_get_block;
  5910. int retries = 0;
  5911. if (unlikely(IS_IMMUTABLE(inode)))
  5912. return VM_FAULT_SIGBUS;
  5913. sb_start_pagefault(inode->i_sb);
  5914. file_update_time(vma->vm_file);
  5915. filemap_invalidate_lock_shared(mapping);
  5916. err = ext4_convert_inline_data(inode);
  5917. if (err)
  5918. goto out_ret;
  5919. /*
  5920. * On data journalling we skip straight to the transaction handle:
  5921. * there's no delalloc; page truncated will be checked later; the
  5922. * early return w/ all buffers mapped (calculates size/len) can't
  5923. * be used; and there's no dioread_nolock, so only ext4_get_block.
  5924. */
  5925. if (ext4_should_journal_data(inode))
  5926. goto retry_alloc;
  5927. /* Delalloc case is easy... */
  5928. if (test_opt(inode->i_sb, DELALLOC) &&
  5929. !ext4_nonda_switch(inode->i_sb)) {
  5930. do {
  5931. err = block_page_mkwrite(vma, vmf,
  5932. ext4_da_get_block_prep);
  5933. } while (err == -ENOSPC &&
  5934. ext4_should_retry_alloc(inode->i_sb, &retries));
  5935. goto out_ret;
  5936. }
  5937. folio_lock(folio);
  5938. size = i_size_read(inode);
  5939. /* Page got truncated from under us? */
  5940. if (folio->mapping != mapping || folio_pos(folio) > size) {
  5941. folio_unlock(folio);
  5942. ret = VM_FAULT_NOPAGE;
  5943. goto out;
  5944. }
  5945. len = folio_size(folio);
  5946. if (folio_pos(folio) + len > size)
  5947. len = size - folio_pos(folio);
  5948. /*
  5949. * Return if we have all the buffers mapped. This avoids the need to do
  5950. * journal_start/journal_stop which can block and take a long time
  5951. *
  5952. * This cannot be done for data journalling, as we have to add the
  5953. * inode to the transaction's list to writeprotect pages on commit.
  5954. */
  5955. if (folio_buffers(folio)) {
  5956. if (!ext4_walk_page_buffers(NULL, inode, folio_buffers(folio),
  5957. 0, len, NULL,
  5958. ext4_bh_unmapped)) {
  5959. /* Wait so that we don't change page under IO */
  5960. folio_wait_stable(folio);
  5961. ret = VM_FAULT_LOCKED;
  5962. goto out;
  5963. }
  5964. }
  5965. folio_unlock(folio);
  5966. /* OK, we need to fill the hole... */
  5967. if (ext4_should_dioread_nolock(inode))
  5968. get_block = ext4_get_block_unwritten;
  5969. retry_alloc:
  5970. /* Start journal and allocate blocks */
  5971. err = ext4_block_page_mkwrite(inode, folio, get_block);
  5972. if (err == -EAGAIN ||
  5973. (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)))
  5974. goto retry_alloc;
  5975. out_ret:
  5976. ret = vmf_fs_error(err);
  5977. out:
  5978. filemap_invalidate_unlock_shared(mapping);
  5979. sb_end_pagefault(inode->i_sb);
  5980. return ret;
  5981. }