inode.c 172 KB

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