inode.c 173 KB

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