inode.c 168 KB

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