inode.c 175 KB

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