inode.c 167 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772
  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. * Goal-directed block allocation by Stephen Tweedie
  16. * (sct@redhat.com), 1993, 1998
  17. * Big-endian to little-endian byte-swapping/bitmaps by
  18. * David S. Miller (davem@caip.rutgers.edu), 1995
  19. * 64-bit file support on 64-bit platforms by Jakub Jelinek
  20. * (jj@sunsite.ms.mff.cuni.cz)
  21. *
  22. * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
  23. */
  24. #include <linux/module.h>
  25. #include <linux/fs.h>
  26. #include <linux/time.h>
  27. #include <linux/jbd2.h>
  28. #include <linux/highuid.h>
  29. #include <linux/pagemap.h>
  30. #include <linux/quotaops.h>
  31. #include <linux/string.h>
  32. #include <linux/buffer_head.h>
  33. #include <linux/writeback.h>
  34. #include <linux/pagevec.h>
  35. #include <linux/mpage.h>
  36. #include <linux/namei.h>
  37. #include <linux/uio.h>
  38. #include <linux/bio.h>
  39. #include <linux/workqueue.h>
  40. #include "ext4_jbd2.h"
  41. #include "xattr.h"
  42. #include "acl.h"
  43. #include "ext4_extents.h"
  44. #include <trace/events/ext4.h>
  45. #define MPAGE_DA_EXTENT_TAIL 0x01
  46. static inline int ext4_begin_ordered_truncate(struct inode *inode,
  47. loff_t new_size)
  48. {
  49. return jbd2_journal_begin_ordered_truncate(
  50. EXT4_SB(inode->i_sb)->s_journal,
  51. &EXT4_I(inode)->jinode,
  52. new_size);
  53. }
  54. static void ext4_invalidatepage(struct page *page, unsigned long offset);
  55. /*
  56. * Test whether an inode is a fast symlink.
  57. */
  58. static int ext4_inode_is_fast_symlink(struct inode *inode)
  59. {
  60. int ea_blocks = EXT4_I(inode)->i_file_acl ?
  61. (inode->i_sb->s_blocksize >> 9) : 0;
  62. return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
  63. }
  64. /*
  65. * Work out how many blocks we need to proceed with the next chunk of a
  66. * truncate transaction.
  67. */
  68. static unsigned long blocks_for_truncate(struct inode *inode)
  69. {
  70. ext4_lblk_t needed;
  71. needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
  72. /* Give ourselves just enough room to cope with inodes in which
  73. * i_blocks is corrupt: we've seen disk corruptions in the past
  74. * which resulted in random data in an inode which looked enough
  75. * like a regular file for ext4 to try to delete it. Things
  76. * will go a bit crazy if that happens, but at least we should
  77. * try not to panic the whole kernel. */
  78. if (needed < 2)
  79. needed = 2;
  80. /* But we need to bound the transaction so we don't overflow the
  81. * journal. */
  82. if (needed > EXT4_MAX_TRANS_DATA)
  83. needed = EXT4_MAX_TRANS_DATA;
  84. return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
  85. }
  86. /*
  87. * Truncate transactions can be complex and absolutely huge. So we need to
  88. * be able to restart the transaction at a conventient checkpoint to make
  89. * sure we don't overflow the journal.
  90. *
  91. * start_transaction gets us a new handle for a truncate transaction,
  92. * and extend_transaction tries to extend the existing one a bit. If
  93. * extend fails, we need to propagate the failure up and restart the
  94. * transaction in the top-level truncate loop. --sct
  95. */
  96. static handle_t *start_transaction(struct inode *inode)
  97. {
  98. handle_t *result;
  99. result = ext4_journal_start(inode, blocks_for_truncate(inode));
  100. if (!IS_ERR(result))
  101. return result;
  102. ext4_std_error(inode->i_sb, PTR_ERR(result));
  103. return result;
  104. }
  105. /*
  106. * Try to extend this transaction for the purposes of truncation.
  107. *
  108. * Returns 0 if we managed to create more room. If we can't create more
  109. * room, and the transaction must be restarted we return 1.
  110. */
  111. static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
  112. {
  113. if (!ext4_handle_valid(handle))
  114. return 0;
  115. if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1))
  116. return 0;
  117. if (!ext4_journal_extend(handle, blocks_for_truncate(inode)))
  118. return 0;
  119. return 1;
  120. }
  121. /*
  122. * Restart the transaction associated with *handle. This does a commit,
  123. * so before we call here everything must be consistently dirtied against
  124. * this transaction.
  125. */
  126. int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
  127. int nblocks)
  128. {
  129. int ret;
  130. /*
  131. * Drop i_data_sem to avoid deadlock with ext4_get_blocks At this
  132. * moment, get_block can be called only for blocks inside i_size since
  133. * page cache has been already dropped and writes are blocked by
  134. * i_mutex. So we can safely drop the i_data_sem here.
  135. */
  136. BUG_ON(EXT4_JOURNAL(inode) == NULL);
  137. jbd_debug(2, "restarting handle %p\n", handle);
  138. up_write(&EXT4_I(inode)->i_data_sem);
  139. ret = ext4_journal_restart(handle, blocks_for_truncate(inode));
  140. down_write(&EXT4_I(inode)->i_data_sem);
  141. ext4_discard_preallocations(inode);
  142. return ret;
  143. }
  144. /*
  145. * Called at the last iput() if i_nlink is zero.
  146. */
  147. void ext4_delete_inode(struct inode *inode)
  148. {
  149. handle_t *handle;
  150. int err;
  151. if (ext4_should_order_data(inode))
  152. ext4_begin_ordered_truncate(inode, 0);
  153. truncate_inode_pages(&inode->i_data, 0);
  154. if (is_bad_inode(inode))
  155. goto no_delete;
  156. handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3);
  157. if (IS_ERR(handle)) {
  158. ext4_std_error(inode->i_sb, PTR_ERR(handle));
  159. /*
  160. * If we're going to skip the normal cleanup, we still need to
  161. * make sure that the in-core orphan linked list is properly
  162. * cleaned up.
  163. */
  164. ext4_orphan_del(NULL, inode);
  165. goto no_delete;
  166. }
  167. if (IS_SYNC(inode))
  168. ext4_handle_sync(handle);
  169. inode->i_size = 0;
  170. err = ext4_mark_inode_dirty(handle, inode);
  171. if (err) {
  172. ext4_warning(inode->i_sb, __func__,
  173. "couldn't mark inode dirty (err %d)", err);
  174. goto stop_handle;
  175. }
  176. if (inode->i_blocks)
  177. ext4_truncate(inode);
  178. /*
  179. * ext4_ext_truncate() doesn't reserve any slop when it
  180. * restarts journal transactions; therefore there may not be
  181. * enough credits left in the handle to remove the inode from
  182. * the orphan list and set the dtime field.
  183. */
  184. if (!ext4_handle_has_enough_credits(handle, 3)) {
  185. err = ext4_journal_extend(handle, 3);
  186. if (err > 0)
  187. err = ext4_journal_restart(handle, 3);
  188. if (err != 0) {
  189. ext4_warning(inode->i_sb, __func__,
  190. "couldn't extend journal (err %d)", err);
  191. stop_handle:
  192. ext4_journal_stop(handle);
  193. goto no_delete;
  194. }
  195. }
  196. /*
  197. * Kill off the orphan record which ext4_truncate created.
  198. * AKPM: I think this can be inside the above `if'.
  199. * Note that ext4_orphan_del() has to be able to cope with the
  200. * deletion of a non-existent orphan - this is because we don't
  201. * know if ext4_truncate() actually created an orphan record.
  202. * (Well, we could do this if we need to, but heck - it works)
  203. */
  204. ext4_orphan_del(handle, inode);
  205. EXT4_I(inode)->i_dtime = get_seconds();
  206. /*
  207. * One subtle ordering requirement: if anything has gone wrong
  208. * (transaction abort, IO errors, whatever), then we can still
  209. * do these next steps (the fs will already have been marked as
  210. * having errors), but we can't free the inode if the mark_dirty
  211. * fails.
  212. */
  213. if (ext4_mark_inode_dirty(handle, inode))
  214. /* If that failed, just do the required in-core inode clear. */
  215. clear_inode(inode);
  216. else
  217. ext4_free_inode(handle, inode);
  218. ext4_journal_stop(handle);
  219. return;
  220. no_delete:
  221. clear_inode(inode); /* We must guarantee clearing of inode... */
  222. }
  223. typedef struct {
  224. __le32 *p;
  225. __le32 key;
  226. struct buffer_head *bh;
  227. } Indirect;
  228. static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
  229. {
  230. p->key = *(p->p = v);
  231. p->bh = bh;
  232. }
  233. /**
  234. * ext4_block_to_path - parse the block number into array of offsets
  235. * @inode: inode in question (we are only interested in its superblock)
  236. * @i_block: block number to be parsed
  237. * @offsets: array to store the offsets in
  238. * @boundary: set this non-zero if the referred-to block is likely to be
  239. * followed (on disk) by an indirect block.
  240. *
  241. * To store the locations of file's data ext4 uses a data structure common
  242. * for UNIX filesystems - tree of pointers anchored in the inode, with
  243. * data blocks at leaves and indirect blocks in intermediate nodes.
  244. * This function translates the block number into path in that tree -
  245. * return value is the path length and @offsets[n] is the offset of
  246. * pointer to (n+1)th node in the nth one. If @block is out of range
  247. * (negative or too large) warning is printed and zero returned.
  248. *
  249. * Note: function doesn't find node addresses, so no IO is needed. All
  250. * we need to know is the capacity of indirect blocks (taken from the
  251. * inode->i_sb).
  252. */
  253. /*
  254. * Portability note: the last comparison (check that we fit into triple
  255. * indirect block) is spelled differently, because otherwise on an
  256. * architecture with 32-bit longs and 8Kb pages we might get into trouble
  257. * if our filesystem had 8Kb blocks. We might use long long, but that would
  258. * kill us on x86. Oh, well, at least the sign propagation does not matter -
  259. * i_block would have to be negative in the very beginning, so we would not
  260. * get there at all.
  261. */
  262. static int ext4_block_to_path(struct inode *inode,
  263. ext4_lblk_t i_block,
  264. ext4_lblk_t offsets[4], int *boundary)
  265. {
  266. int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  267. int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
  268. const long direct_blocks = EXT4_NDIR_BLOCKS,
  269. indirect_blocks = ptrs,
  270. double_blocks = (1 << (ptrs_bits * 2));
  271. int n = 0;
  272. int final = 0;
  273. if (i_block < direct_blocks) {
  274. offsets[n++] = i_block;
  275. final = direct_blocks;
  276. } else if ((i_block -= direct_blocks) < indirect_blocks) {
  277. offsets[n++] = EXT4_IND_BLOCK;
  278. offsets[n++] = i_block;
  279. final = ptrs;
  280. } else if ((i_block -= indirect_blocks) < double_blocks) {
  281. offsets[n++] = EXT4_DIND_BLOCK;
  282. offsets[n++] = i_block >> ptrs_bits;
  283. offsets[n++] = i_block & (ptrs - 1);
  284. final = ptrs;
  285. } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
  286. offsets[n++] = EXT4_TIND_BLOCK;
  287. offsets[n++] = i_block >> (ptrs_bits * 2);
  288. offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
  289. offsets[n++] = i_block & (ptrs - 1);
  290. final = ptrs;
  291. } else {
  292. ext4_warning(inode->i_sb, "ext4_block_to_path",
  293. "block %lu > max in inode %lu",
  294. i_block + direct_blocks +
  295. indirect_blocks + double_blocks, inode->i_ino);
  296. }
  297. if (boundary)
  298. *boundary = final - 1 - (i_block & (ptrs - 1));
  299. return n;
  300. }
  301. static int __ext4_check_blockref(const char *function, struct inode *inode,
  302. __le32 *p, unsigned int max)
  303. {
  304. __le32 *bref = p;
  305. unsigned int blk;
  306. while (bref < p+max) {
  307. blk = le32_to_cpu(*bref++);
  308. if (blk &&
  309. unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb),
  310. blk, 1))) {
  311. ext4_error(inode->i_sb, function,
  312. "invalid block reference %u "
  313. "in inode #%lu", blk, inode->i_ino);
  314. return -EIO;
  315. }
  316. }
  317. return 0;
  318. }
  319. #define ext4_check_indirect_blockref(inode, bh) \
  320. __ext4_check_blockref(__func__, inode, (__le32 *)(bh)->b_data, \
  321. EXT4_ADDR_PER_BLOCK((inode)->i_sb))
  322. #define ext4_check_inode_blockref(inode) \
  323. __ext4_check_blockref(__func__, inode, EXT4_I(inode)->i_data, \
  324. EXT4_NDIR_BLOCKS)
  325. /**
  326. * ext4_get_branch - read the chain of indirect blocks leading to data
  327. * @inode: inode in question
  328. * @depth: depth of the chain (1 - direct pointer, etc.)
  329. * @offsets: offsets of pointers in inode/indirect blocks
  330. * @chain: place to store the result
  331. * @err: here we store the error value
  332. *
  333. * Function fills the array of triples <key, p, bh> and returns %NULL
  334. * if everything went OK or the pointer to the last filled triple
  335. * (incomplete one) otherwise. Upon the return chain[i].key contains
  336. * the number of (i+1)-th block in the chain (as it is stored in memory,
  337. * i.e. little-endian 32-bit), chain[i].p contains the address of that
  338. * number (it points into struct inode for i==0 and into the bh->b_data
  339. * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
  340. * block for i>0 and NULL for i==0. In other words, it holds the block
  341. * numbers of the chain, addresses they were taken from (and where we can
  342. * verify that chain did not change) and buffer_heads hosting these
  343. * numbers.
  344. *
  345. * Function stops when it stumbles upon zero pointer (absent block)
  346. * (pointer to last triple returned, *@err == 0)
  347. * or when it gets an IO error reading an indirect block
  348. * (ditto, *@err == -EIO)
  349. * or when it reads all @depth-1 indirect blocks successfully and finds
  350. * the whole chain, all way to the data (returns %NULL, *err == 0).
  351. *
  352. * Need to be called with
  353. * down_read(&EXT4_I(inode)->i_data_sem)
  354. */
  355. static Indirect *ext4_get_branch(struct inode *inode, int depth,
  356. ext4_lblk_t *offsets,
  357. Indirect chain[4], int *err)
  358. {
  359. struct super_block *sb = inode->i_sb;
  360. Indirect *p = chain;
  361. struct buffer_head *bh;
  362. *err = 0;
  363. /* i_data is not going away, no lock needed */
  364. add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
  365. if (!p->key)
  366. goto no_block;
  367. while (--depth) {
  368. bh = sb_getblk(sb, le32_to_cpu(p->key));
  369. if (unlikely(!bh))
  370. goto failure;
  371. if (!bh_uptodate_or_lock(bh)) {
  372. if (bh_submit_read(bh) < 0) {
  373. put_bh(bh);
  374. goto failure;
  375. }
  376. /* validate block references */
  377. if (ext4_check_indirect_blockref(inode, bh)) {
  378. put_bh(bh);
  379. goto failure;
  380. }
  381. }
  382. add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
  383. /* Reader: end */
  384. if (!p->key)
  385. goto no_block;
  386. }
  387. return NULL;
  388. failure:
  389. *err = -EIO;
  390. no_block:
  391. return p;
  392. }
  393. /**
  394. * ext4_find_near - find a place for allocation with sufficient locality
  395. * @inode: owner
  396. * @ind: descriptor of indirect block.
  397. *
  398. * This function returns the preferred place for block allocation.
  399. * It is used when heuristic for sequential allocation fails.
  400. * Rules are:
  401. * + if there is a block to the left of our position - allocate near it.
  402. * + if pointer will live in indirect block - allocate near that block.
  403. * + if pointer will live in inode - allocate in the same
  404. * cylinder group.
  405. *
  406. * In the latter case we colour the starting block by the callers PID to
  407. * prevent it from clashing with concurrent allocations for a different inode
  408. * in the same block group. The PID is used here so that functionally related
  409. * files will be close-by on-disk.
  410. *
  411. * Caller must make sure that @ind is valid and will stay that way.
  412. */
  413. static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
  414. {
  415. struct ext4_inode_info *ei = EXT4_I(inode);
  416. __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
  417. __le32 *p;
  418. ext4_fsblk_t bg_start;
  419. ext4_fsblk_t last_block;
  420. ext4_grpblk_t colour;
  421. ext4_group_t block_group;
  422. int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
  423. /* Try to find previous block */
  424. for (p = ind->p - 1; p >= start; p--) {
  425. if (*p)
  426. return le32_to_cpu(*p);
  427. }
  428. /* No such thing, so let's try location of indirect block */
  429. if (ind->bh)
  430. return ind->bh->b_blocknr;
  431. /*
  432. * It is going to be referred to from the inode itself? OK, just put it
  433. * into the same cylinder group then.
  434. */
  435. block_group = ei->i_block_group;
  436. if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
  437. block_group &= ~(flex_size-1);
  438. if (S_ISREG(inode->i_mode))
  439. block_group++;
  440. }
  441. bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
  442. last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
  443. /*
  444. * If we are doing delayed allocation, we don't need take
  445. * colour into account.
  446. */
  447. if (test_opt(inode->i_sb, DELALLOC))
  448. return bg_start;
  449. if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
  450. colour = (current->pid % 16) *
  451. (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
  452. else
  453. colour = (current->pid % 16) * ((last_block - bg_start) / 16);
  454. return bg_start + colour;
  455. }
  456. /**
  457. * ext4_find_goal - find a preferred place for allocation.
  458. * @inode: owner
  459. * @block: block we want
  460. * @partial: pointer to the last triple within a chain
  461. *
  462. * Normally this function find the preferred place for block allocation,
  463. * returns it.
  464. * Because this is only used for non-extent files, we limit the block nr
  465. * to 32 bits.
  466. */
  467. static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
  468. Indirect *partial)
  469. {
  470. ext4_fsblk_t goal;
  471. /*
  472. * XXX need to get goal block from mballoc's data structures
  473. */
  474. goal = ext4_find_near(inode, partial);
  475. goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
  476. return goal;
  477. }
  478. /**
  479. * ext4_blks_to_allocate: Look up the block map and count the number
  480. * of direct blocks need to be allocated for the given branch.
  481. *
  482. * @branch: chain of indirect blocks
  483. * @k: number of blocks need for indirect blocks
  484. * @blks: number of data blocks to be mapped.
  485. * @blocks_to_boundary: the offset in the indirect block
  486. *
  487. * return the total number of blocks to be allocate, including the
  488. * direct and indirect blocks.
  489. */
  490. static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks,
  491. int blocks_to_boundary)
  492. {
  493. unsigned int count = 0;
  494. /*
  495. * Simple case, [t,d]Indirect block(s) has not allocated yet
  496. * then it's clear blocks on that path have not allocated
  497. */
  498. if (k > 0) {
  499. /* right now we don't handle cross boundary allocation */
  500. if (blks < blocks_to_boundary + 1)
  501. count += blks;
  502. else
  503. count += blocks_to_boundary + 1;
  504. return count;
  505. }
  506. count++;
  507. while (count < blks && count <= blocks_to_boundary &&
  508. le32_to_cpu(*(branch[0].p + count)) == 0) {
  509. count++;
  510. }
  511. return count;
  512. }
  513. /**
  514. * ext4_alloc_blocks: multiple allocate blocks needed for a branch
  515. * @indirect_blks: the number of blocks need to allocate for indirect
  516. * blocks
  517. *
  518. * @new_blocks: on return it will store the new block numbers for
  519. * the indirect blocks(if needed) and the first direct block,
  520. * @blks: on return it will store the total number of allocated
  521. * direct blocks
  522. */
  523. static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
  524. ext4_lblk_t iblock, ext4_fsblk_t goal,
  525. int indirect_blks, int blks,
  526. ext4_fsblk_t new_blocks[4], int *err)
  527. {
  528. struct ext4_allocation_request ar;
  529. int target, i;
  530. unsigned long count = 0, blk_allocated = 0;
  531. int index = 0;
  532. ext4_fsblk_t current_block = 0;
  533. int ret = 0;
  534. /*
  535. * Here we try to allocate the requested multiple blocks at once,
  536. * on a best-effort basis.
  537. * To build a branch, we should allocate blocks for
  538. * the indirect blocks(if not allocated yet), and at least
  539. * the first direct block of this branch. That's the
  540. * minimum number of blocks need to allocate(required)
  541. */
  542. /* first we try to allocate the indirect blocks */
  543. target = indirect_blks;
  544. while (target > 0) {
  545. count = target;
  546. /* allocating blocks for indirect blocks and direct blocks */
  547. current_block = ext4_new_meta_blocks(handle, inode,
  548. goal, &count, err);
  549. if (*err)
  550. goto failed_out;
  551. BUG_ON(current_block + count > EXT4_MAX_BLOCK_FILE_PHYS);
  552. target -= count;
  553. /* allocate blocks for indirect blocks */
  554. while (index < indirect_blks && count) {
  555. new_blocks[index++] = current_block++;
  556. count--;
  557. }
  558. if (count > 0) {
  559. /*
  560. * save the new block number
  561. * for the first direct block
  562. */
  563. new_blocks[index] = current_block;
  564. printk(KERN_INFO "%s returned more blocks than "
  565. "requested\n", __func__);
  566. WARN_ON(1);
  567. break;
  568. }
  569. }
  570. target = blks - count ;
  571. blk_allocated = count;
  572. if (!target)
  573. goto allocated;
  574. /* Now allocate data blocks */
  575. memset(&ar, 0, sizeof(ar));
  576. ar.inode = inode;
  577. ar.goal = goal;
  578. ar.len = target;
  579. ar.logical = iblock;
  580. if (S_ISREG(inode->i_mode))
  581. /* enable in-core preallocation only for regular files */
  582. ar.flags = EXT4_MB_HINT_DATA;
  583. current_block = ext4_mb_new_blocks(handle, &ar, err);
  584. BUG_ON(current_block + ar.len > EXT4_MAX_BLOCK_FILE_PHYS);
  585. if (*err && (target == blks)) {
  586. /*
  587. * if the allocation failed and we didn't allocate
  588. * any blocks before
  589. */
  590. goto failed_out;
  591. }
  592. if (!*err) {
  593. if (target == blks) {
  594. /*
  595. * save the new block number
  596. * for the first direct block
  597. */
  598. new_blocks[index] = current_block;
  599. }
  600. blk_allocated += ar.len;
  601. }
  602. allocated:
  603. /* total number of blocks allocated for direct blocks */
  604. ret = blk_allocated;
  605. *err = 0;
  606. return ret;
  607. failed_out:
  608. for (i = 0; i < index; i++)
  609. ext4_free_blocks(handle, inode, 0, new_blocks[i], 1, 0);
  610. return ret;
  611. }
  612. /**
  613. * ext4_alloc_branch - allocate and set up a chain of blocks.
  614. * @inode: owner
  615. * @indirect_blks: number of allocated indirect blocks
  616. * @blks: number of allocated direct blocks
  617. * @offsets: offsets (in the blocks) to store the pointers to next.
  618. * @branch: place to store the chain in.
  619. *
  620. * This function allocates blocks, zeroes out all but the last one,
  621. * links them into chain and (if we are synchronous) writes them to disk.
  622. * In other words, it prepares a branch that can be spliced onto the
  623. * inode. It stores the information about that chain in the branch[], in
  624. * the same format as ext4_get_branch() would do. We are calling it after
  625. * we had read the existing part of chain and partial points to the last
  626. * triple of that (one with zero ->key). Upon the exit we have the same
  627. * picture as after the successful ext4_get_block(), except that in one
  628. * place chain is disconnected - *branch->p is still zero (we did not
  629. * set the last link), but branch->key contains the number that should
  630. * be placed into *branch->p to fill that gap.
  631. *
  632. * If allocation fails we free all blocks we've allocated (and forget
  633. * their buffer_heads) and return the error value the from failed
  634. * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
  635. * as described above and return 0.
  636. */
  637. static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
  638. ext4_lblk_t iblock, int indirect_blks,
  639. int *blks, ext4_fsblk_t goal,
  640. ext4_lblk_t *offsets, Indirect *branch)
  641. {
  642. int blocksize = inode->i_sb->s_blocksize;
  643. int i, n = 0;
  644. int err = 0;
  645. struct buffer_head *bh;
  646. int num;
  647. ext4_fsblk_t new_blocks[4];
  648. ext4_fsblk_t current_block;
  649. num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks,
  650. *blks, new_blocks, &err);
  651. if (err)
  652. return err;
  653. branch[0].key = cpu_to_le32(new_blocks[0]);
  654. /*
  655. * metadata blocks and data blocks are allocated.
  656. */
  657. for (n = 1; n <= indirect_blks; n++) {
  658. /*
  659. * Get buffer_head for parent block, zero it out
  660. * and set the pointer to new one, then send
  661. * parent to disk.
  662. */
  663. bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
  664. branch[n].bh = bh;
  665. lock_buffer(bh);
  666. BUFFER_TRACE(bh, "call get_create_access");
  667. err = ext4_journal_get_create_access(handle, bh);
  668. if (err) {
  669. /* Don't brelse(bh) here; it's done in
  670. * ext4_journal_forget() below */
  671. unlock_buffer(bh);
  672. goto failed;
  673. }
  674. memset(bh->b_data, 0, blocksize);
  675. branch[n].p = (__le32 *) bh->b_data + offsets[n];
  676. branch[n].key = cpu_to_le32(new_blocks[n]);
  677. *branch[n].p = branch[n].key;
  678. if (n == indirect_blks) {
  679. current_block = new_blocks[n];
  680. /*
  681. * End of chain, update the last new metablock of
  682. * the chain to point to the new allocated
  683. * data blocks numbers
  684. */
  685. for (i = 1; i < num; i++)
  686. *(branch[n].p + i) = cpu_to_le32(++current_block);
  687. }
  688. BUFFER_TRACE(bh, "marking uptodate");
  689. set_buffer_uptodate(bh);
  690. unlock_buffer(bh);
  691. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  692. err = ext4_handle_dirty_metadata(handle, inode, bh);
  693. if (err)
  694. goto failed;
  695. }
  696. *blks = num;
  697. return err;
  698. failed:
  699. /* Allocation failed, free what we already allocated */
  700. ext4_free_blocks(handle, inode, 0, new_blocks[0], 1, 0);
  701. for (i = 1; i <= n ; i++) {
  702. /*
  703. * branch[i].bh is newly allocated, so there is no
  704. * need to revoke the block, which is why we don't
  705. * need to set EXT4_FREE_BLOCKS_METADATA.
  706. */
  707. ext4_free_blocks(handle, inode, 0, new_blocks[i], 1,
  708. EXT4_FREE_BLOCKS_FORGET);
  709. }
  710. for (i = n+1; i < indirect_blks; i++)
  711. ext4_free_blocks(handle, inode, 0, new_blocks[i], 1, 0);
  712. ext4_free_blocks(handle, inode, 0, new_blocks[i], num, 0);
  713. return err;
  714. }
  715. /**
  716. * ext4_splice_branch - splice the allocated branch onto inode.
  717. * @inode: owner
  718. * @block: (logical) number of block we are adding
  719. * @chain: chain of indirect blocks (with a missing link - see
  720. * ext4_alloc_branch)
  721. * @where: location of missing link
  722. * @num: number of indirect blocks we are adding
  723. * @blks: number of direct blocks we are adding
  724. *
  725. * This function fills the missing link and does all housekeeping needed in
  726. * inode (->i_blocks, etc.). In case of success we end up with the full
  727. * chain to new block and return 0.
  728. */
  729. static int ext4_splice_branch(handle_t *handle, struct inode *inode,
  730. ext4_lblk_t block, Indirect *where, int num,
  731. int blks)
  732. {
  733. int i;
  734. int err = 0;
  735. ext4_fsblk_t current_block;
  736. /*
  737. * If we're splicing into a [td]indirect block (as opposed to the
  738. * inode) then we need to get write access to the [td]indirect block
  739. * before the splice.
  740. */
  741. if (where->bh) {
  742. BUFFER_TRACE(where->bh, "get_write_access");
  743. err = ext4_journal_get_write_access(handle, where->bh);
  744. if (err)
  745. goto err_out;
  746. }
  747. /* That's it */
  748. *where->p = where->key;
  749. /*
  750. * Update the host buffer_head or inode to point to more just allocated
  751. * direct blocks blocks
  752. */
  753. if (num == 0 && blks > 1) {
  754. current_block = le32_to_cpu(where->key) + 1;
  755. for (i = 1; i < blks; i++)
  756. *(where->p + i) = cpu_to_le32(current_block++);
  757. }
  758. /* We are done with atomic stuff, now do the rest of housekeeping */
  759. /* had we spliced it onto indirect block? */
  760. if (where->bh) {
  761. /*
  762. * If we spliced it onto an indirect block, we haven't
  763. * altered the inode. Note however that if it is being spliced
  764. * onto an indirect block at the very end of the file (the
  765. * file is growing) then we *will* alter the inode to reflect
  766. * the new i_size. But that is not done here - it is done in
  767. * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
  768. */
  769. jbd_debug(5, "splicing indirect only\n");
  770. BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
  771. err = ext4_handle_dirty_metadata(handle, inode, where->bh);
  772. if (err)
  773. goto err_out;
  774. } else {
  775. /*
  776. * OK, we spliced it into the inode itself on a direct block.
  777. */
  778. ext4_mark_inode_dirty(handle, inode);
  779. jbd_debug(5, "splicing direct\n");
  780. }
  781. return err;
  782. err_out:
  783. for (i = 1; i <= num; i++) {
  784. /*
  785. * branch[i].bh is newly allocated, so there is no
  786. * need to revoke the block, which is why we don't
  787. * need to set EXT4_FREE_BLOCKS_METADATA.
  788. */
  789. ext4_free_blocks(handle, inode, where[i].bh, 0, 1,
  790. EXT4_FREE_BLOCKS_FORGET);
  791. }
  792. ext4_free_blocks(handle, inode, 0, le32_to_cpu(where[num].key),
  793. blks, 0);
  794. return err;
  795. }
  796. /*
  797. * The ext4_ind_get_blocks() function handles non-extents inodes
  798. * (i.e., using the traditional indirect/double-indirect i_blocks
  799. * scheme) for ext4_get_blocks().
  800. *
  801. * Allocation strategy is simple: if we have to allocate something, we will
  802. * have to go the whole way to leaf. So let's do it before attaching anything
  803. * to tree, set linkage between the newborn blocks, write them if sync is
  804. * required, recheck the path, free and repeat if check fails, otherwise
  805. * set the last missing link (that will protect us from any truncate-generated
  806. * removals - all blocks on the path are immune now) and possibly force the
  807. * write on the parent block.
  808. * That has a nice additional property: no special recovery from the failed
  809. * allocations is needed - we simply release blocks and do not touch anything
  810. * reachable from inode.
  811. *
  812. * `handle' can be NULL if create == 0.
  813. *
  814. * return > 0, # of blocks mapped or allocated.
  815. * return = 0, if plain lookup failed.
  816. * return < 0, error case.
  817. *
  818. * The ext4_ind_get_blocks() function should be called with
  819. * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem
  820. * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or
  821. * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
  822. * blocks.
  823. */
  824. static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode,
  825. ext4_lblk_t iblock, unsigned int maxblocks,
  826. struct buffer_head *bh_result,
  827. int flags)
  828. {
  829. int err = -EIO;
  830. ext4_lblk_t offsets[4];
  831. Indirect chain[4];
  832. Indirect *partial;
  833. ext4_fsblk_t goal;
  834. int indirect_blks;
  835. int blocks_to_boundary = 0;
  836. int depth;
  837. int count = 0;
  838. ext4_fsblk_t first_block = 0;
  839. J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL));
  840. J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
  841. depth = ext4_block_to_path(inode, iblock, offsets,
  842. &blocks_to_boundary);
  843. if (depth == 0)
  844. goto out;
  845. partial = ext4_get_branch(inode, depth, offsets, chain, &err);
  846. /* Simplest case - block found, no allocation needed */
  847. if (!partial) {
  848. first_block = le32_to_cpu(chain[depth - 1].key);
  849. clear_buffer_new(bh_result);
  850. count++;
  851. /*map more blocks*/
  852. while (count < maxblocks && count <= blocks_to_boundary) {
  853. ext4_fsblk_t blk;
  854. blk = le32_to_cpu(*(chain[depth-1].p + count));
  855. if (blk == first_block + count)
  856. count++;
  857. else
  858. break;
  859. }
  860. goto got_it;
  861. }
  862. /* Next simple case - plain lookup or failed read of indirect block */
  863. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO)
  864. goto cleanup;
  865. /*
  866. * Okay, we need to do block allocation.
  867. */
  868. goal = ext4_find_goal(inode, iblock, partial);
  869. /* the number of blocks need to allocate for [d,t]indirect blocks */
  870. indirect_blks = (chain + depth) - partial - 1;
  871. /*
  872. * Next look up the indirect map to count the totoal number of
  873. * direct blocks to allocate for this branch.
  874. */
  875. count = ext4_blks_to_allocate(partial, indirect_blks,
  876. maxblocks, blocks_to_boundary);
  877. /*
  878. * Block out ext4_truncate while we alter the tree
  879. */
  880. err = ext4_alloc_branch(handle, inode, iblock, indirect_blks,
  881. &count, goal,
  882. offsets + (partial - chain), partial);
  883. /*
  884. * The ext4_splice_branch call will free and forget any buffers
  885. * on the new chain if there is a failure, but that risks using
  886. * up transaction credits, especially for bitmaps where the
  887. * credits cannot be returned. Can we handle this somehow? We
  888. * may need to return -EAGAIN upwards in the worst case. --sct
  889. */
  890. if (!err)
  891. err = ext4_splice_branch(handle, inode, iblock,
  892. partial, indirect_blks, count);
  893. if (err)
  894. goto cleanup;
  895. set_buffer_new(bh_result);
  896. ext4_update_inode_fsync_trans(handle, inode, 1);
  897. got_it:
  898. map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
  899. if (count > blocks_to_boundary)
  900. set_buffer_boundary(bh_result);
  901. err = count;
  902. /* Clean up and exit */
  903. partial = chain + depth - 1; /* the whole chain */
  904. cleanup:
  905. while (partial > chain) {
  906. BUFFER_TRACE(partial->bh, "call brelse");
  907. brelse(partial->bh);
  908. partial--;
  909. }
  910. BUFFER_TRACE(bh_result, "returned");
  911. out:
  912. return err;
  913. }
  914. qsize_t ext4_get_reserved_space(struct inode *inode)
  915. {
  916. unsigned long long total;
  917. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  918. total = EXT4_I(inode)->i_reserved_data_blocks +
  919. EXT4_I(inode)->i_reserved_meta_blocks;
  920. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  921. return (total << inode->i_blkbits);
  922. }
  923. /*
  924. * Calculate the number of metadata blocks need to reserve
  925. * to allocate @blocks for non extent file based file
  926. */
  927. static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks)
  928. {
  929. int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  930. int ind_blks, dind_blks, tind_blks;
  931. /* number of new indirect blocks needed */
  932. ind_blks = (blocks + icap - 1) / icap;
  933. dind_blks = (ind_blks + icap - 1) / icap;
  934. tind_blks = 1;
  935. return ind_blks + dind_blks + tind_blks;
  936. }
  937. /*
  938. * Calculate the number of metadata blocks need to reserve
  939. * to allocate given number of blocks
  940. */
  941. static int ext4_calc_metadata_amount(struct inode *inode, int blocks)
  942. {
  943. if (!blocks)
  944. return 0;
  945. if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)
  946. return ext4_ext_calc_metadata_amount(inode, blocks);
  947. return ext4_indirect_calc_metadata_amount(inode, blocks);
  948. }
  949. static void ext4_da_update_reserve_space(struct inode *inode, int used)
  950. {
  951. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  952. int total, mdb, mdb_free;
  953. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  954. /* recalculate the number of metablocks still need to be reserved */
  955. total = EXT4_I(inode)->i_reserved_data_blocks - used;
  956. mdb = ext4_calc_metadata_amount(inode, total);
  957. /* figure out how many metablocks to release */
  958. BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
  959. mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
  960. if (mdb_free) {
  961. /* Account for allocated meta_blocks */
  962. mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks;
  963. /* update fs dirty blocks counter */
  964. percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free);
  965. EXT4_I(inode)->i_allocated_meta_blocks = 0;
  966. EXT4_I(inode)->i_reserved_meta_blocks = mdb;
  967. }
  968. /* update per-inode reservations */
  969. BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks);
  970. EXT4_I(inode)->i_reserved_data_blocks -= used;
  971. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  972. /*
  973. * free those over-booking quota for metadata blocks
  974. */
  975. if (mdb_free)
  976. vfs_dq_release_reservation_block(inode, mdb_free);
  977. /*
  978. * If we have done all the pending block allocations and if
  979. * there aren't any writers on the inode, we can discard the
  980. * inode's preallocations.
  981. */
  982. if (!total && (atomic_read(&inode->i_writecount) == 0))
  983. ext4_discard_preallocations(inode);
  984. }
  985. static int check_block_validity(struct inode *inode, const char *msg,
  986. sector_t logical, sector_t phys, int len)
  987. {
  988. if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), phys, len)) {
  989. ext4_error(inode->i_sb, msg,
  990. "inode #%lu logical block %llu mapped to %llu "
  991. "(size %d)", inode->i_ino,
  992. (unsigned long long) logical,
  993. (unsigned long long) phys, len);
  994. return -EIO;
  995. }
  996. return 0;
  997. }
  998. /*
  999. * Return the number of contiguous dirty pages in a given inode
  1000. * starting at page frame idx.
  1001. */
  1002. static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx,
  1003. unsigned int max_pages)
  1004. {
  1005. struct address_space *mapping = inode->i_mapping;
  1006. pgoff_t index;
  1007. struct pagevec pvec;
  1008. pgoff_t num = 0;
  1009. int i, nr_pages, done = 0;
  1010. if (max_pages == 0)
  1011. return 0;
  1012. pagevec_init(&pvec, 0);
  1013. while (!done) {
  1014. index = idx;
  1015. nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  1016. PAGECACHE_TAG_DIRTY,
  1017. (pgoff_t)PAGEVEC_SIZE);
  1018. if (nr_pages == 0)
  1019. break;
  1020. for (i = 0; i < nr_pages; i++) {
  1021. struct page *page = pvec.pages[i];
  1022. struct buffer_head *bh, *head;
  1023. lock_page(page);
  1024. if (unlikely(page->mapping != mapping) ||
  1025. !PageDirty(page) ||
  1026. PageWriteback(page) ||
  1027. page->index != idx) {
  1028. done = 1;
  1029. unlock_page(page);
  1030. break;
  1031. }
  1032. if (page_has_buffers(page)) {
  1033. bh = head = page_buffers(page);
  1034. do {
  1035. if (!buffer_delay(bh) &&
  1036. !buffer_unwritten(bh))
  1037. done = 1;
  1038. bh = bh->b_this_page;
  1039. } while (!done && (bh != head));
  1040. }
  1041. unlock_page(page);
  1042. if (done)
  1043. break;
  1044. idx++;
  1045. num++;
  1046. if (num >= max_pages)
  1047. break;
  1048. }
  1049. pagevec_release(&pvec);
  1050. }
  1051. return num;
  1052. }
  1053. /*
  1054. * The ext4_get_blocks() function tries to look up the requested blocks,
  1055. * and returns if the blocks are already mapped.
  1056. *
  1057. * Otherwise it takes the write lock of the i_data_sem and allocate blocks
  1058. * and store the allocated blocks in the result buffer head and mark it
  1059. * mapped.
  1060. *
  1061. * If file type is extents based, it will call ext4_ext_get_blocks(),
  1062. * Otherwise, call with ext4_ind_get_blocks() to handle indirect mapping
  1063. * based files
  1064. *
  1065. * On success, it returns the number of blocks being mapped or allocate.
  1066. * if create==0 and the blocks are pre-allocated and uninitialized block,
  1067. * the result buffer head is unmapped. If the create ==1, it will make sure
  1068. * the buffer head is mapped.
  1069. *
  1070. * It returns 0 if plain look up failed (blocks have not been allocated), in
  1071. * that casem, buffer head is unmapped
  1072. *
  1073. * It returns the error in case of allocation failure.
  1074. */
  1075. int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block,
  1076. unsigned int max_blocks, struct buffer_head *bh,
  1077. int flags)
  1078. {
  1079. int retval;
  1080. clear_buffer_mapped(bh);
  1081. clear_buffer_unwritten(bh);
  1082. ext_debug("ext4_get_blocks(): inode %lu, flag %d, max_blocks %u,"
  1083. "logical block %lu\n", inode->i_ino, flags, max_blocks,
  1084. (unsigned long)block);
  1085. /*
  1086. * Try to see if we can get the block without requesting a new
  1087. * file system block.
  1088. */
  1089. down_read((&EXT4_I(inode)->i_data_sem));
  1090. if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
  1091. retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
  1092. bh, 0);
  1093. } else {
  1094. retval = ext4_ind_get_blocks(handle, inode, block, max_blocks,
  1095. bh, 0);
  1096. }
  1097. up_read((&EXT4_I(inode)->i_data_sem));
  1098. if (retval > 0 && buffer_mapped(bh)) {
  1099. int ret = check_block_validity(inode, "file system corruption",
  1100. block, bh->b_blocknr, retval);
  1101. if (ret != 0)
  1102. return ret;
  1103. }
  1104. /* If it is only a block(s) look up */
  1105. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
  1106. return retval;
  1107. /*
  1108. * Returns if the blocks have already allocated
  1109. *
  1110. * Note that if blocks have been preallocated
  1111. * ext4_ext_get_block() returns th create = 0
  1112. * with buffer head unmapped.
  1113. */
  1114. if (retval > 0 && buffer_mapped(bh))
  1115. return retval;
  1116. /*
  1117. * When we call get_blocks without the create flag, the
  1118. * BH_Unwritten flag could have gotten set if the blocks
  1119. * requested were part of a uninitialized extent. We need to
  1120. * clear this flag now that we are committed to convert all or
  1121. * part of the uninitialized extent to be an initialized
  1122. * extent. This is because we need to avoid the combination
  1123. * of BH_Unwritten and BH_Mapped flags being simultaneously
  1124. * set on the buffer_head.
  1125. */
  1126. clear_buffer_unwritten(bh);
  1127. /*
  1128. * New blocks allocate and/or writing to uninitialized extent
  1129. * will possibly result in updating i_data, so we take
  1130. * the write lock of i_data_sem, and call get_blocks()
  1131. * with create == 1 flag.
  1132. */
  1133. down_write((&EXT4_I(inode)->i_data_sem));
  1134. /*
  1135. * if the caller is from delayed allocation writeout path
  1136. * we have already reserved fs blocks for allocation
  1137. * let the underlying get_block() function know to
  1138. * avoid double accounting
  1139. */
  1140. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
  1141. EXT4_I(inode)->i_delalloc_reserved_flag = 1;
  1142. /*
  1143. * We need to check for EXT4 here because migrate
  1144. * could have changed the inode type in between
  1145. */
  1146. if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
  1147. retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
  1148. bh, flags);
  1149. } else {
  1150. retval = ext4_ind_get_blocks(handle, inode, block,
  1151. max_blocks, bh, flags);
  1152. if (retval > 0 && buffer_new(bh)) {
  1153. /*
  1154. * We allocated new blocks which will result in
  1155. * i_data's format changing. Force the migrate
  1156. * to fail by clearing migrate flags
  1157. */
  1158. EXT4_I(inode)->i_state &= ~EXT4_STATE_EXT_MIGRATE;
  1159. }
  1160. }
  1161. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
  1162. EXT4_I(inode)->i_delalloc_reserved_flag = 0;
  1163. /*
  1164. * Update reserved blocks/metadata blocks after successful
  1165. * block allocation which had been deferred till now.
  1166. */
  1167. if ((retval > 0) && (flags & EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE))
  1168. ext4_da_update_reserve_space(inode, retval);
  1169. up_write((&EXT4_I(inode)->i_data_sem));
  1170. if (retval > 0 && buffer_mapped(bh)) {
  1171. int ret = check_block_validity(inode, "file system "
  1172. "corruption after allocation",
  1173. block, bh->b_blocknr, retval);
  1174. if (ret != 0)
  1175. return ret;
  1176. }
  1177. return retval;
  1178. }
  1179. /* Maximum number of blocks we map for direct IO at once. */
  1180. #define DIO_MAX_BLOCKS 4096
  1181. int ext4_get_block(struct inode *inode, sector_t iblock,
  1182. struct buffer_head *bh_result, int create)
  1183. {
  1184. handle_t *handle = ext4_journal_current_handle();
  1185. int ret = 0, started = 0;
  1186. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  1187. int dio_credits;
  1188. if (create && !handle) {
  1189. /* Direct IO write... */
  1190. if (max_blocks > DIO_MAX_BLOCKS)
  1191. max_blocks = DIO_MAX_BLOCKS;
  1192. dio_credits = ext4_chunk_trans_blocks(inode, max_blocks);
  1193. handle = ext4_journal_start(inode, dio_credits);
  1194. if (IS_ERR(handle)) {
  1195. ret = PTR_ERR(handle);
  1196. goto out;
  1197. }
  1198. started = 1;
  1199. }
  1200. ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result,
  1201. create ? EXT4_GET_BLOCKS_CREATE : 0);
  1202. if (ret > 0) {
  1203. bh_result->b_size = (ret << inode->i_blkbits);
  1204. ret = 0;
  1205. }
  1206. if (started)
  1207. ext4_journal_stop(handle);
  1208. out:
  1209. return ret;
  1210. }
  1211. /*
  1212. * `handle' can be NULL if create is zero
  1213. */
  1214. struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
  1215. ext4_lblk_t block, int create, int *errp)
  1216. {
  1217. struct buffer_head dummy;
  1218. int fatal = 0, err;
  1219. int flags = 0;
  1220. J_ASSERT(handle != NULL || create == 0);
  1221. dummy.b_state = 0;
  1222. dummy.b_blocknr = -1000;
  1223. buffer_trace_init(&dummy.b_history);
  1224. if (create)
  1225. flags |= EXT4_GET_BLOCKS_CREATE;
  1226. err = ext4_get_blocks(handle, inode, block, 1, &dummy, flags);
  1227. /*
  1228. * ext4_get_blocks() returns number of blocks mapped. 0 in
  1229. * case of a HOLE.
  1230. */
  1231. if (err > 0) {
  1232. if (err > 1)
  1233. WARN_ON(1);
  1234. err = 0;
  1235. }
  1236. *errp = err;
  1237. if (!err && buffer_mapped(&dummy)) {
  1238. struct buffer_head *bh;
  1239. bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
  1240. if (!bh) {
  1241. *errp = -EIO;
  1242. goto err;
  1243. }
  1244. if (buffer_new(&dummy)) {
  1245. J_ASSERT(create != 0);
  1246. J_ASSERT(handle != NULL);
  1247. /*
  1248. * Now that we do not always journal data, we should
  1249. * keep in mind whether this should always journal the
  1250. * new buffer as metadata. For now, regular file
  1251. * writes use ext4_get_block instead, so it's not a
  1252. * problem.
  1253. */
  1254. lock_buffer(bh);
  1255. BUFFER_TRACE(bh, "call get_create_access");
  1256. fatal = ext4_journal_get_create_access(handle, bh);
  1257. if (!fatal && !buffer_uptodate(bh)) {
  1258. memset(bh->b_data, 0, inode->i_sb->s_blocksize);
  1259. set_buffer_uptodate(bh);
  1260. }
  1261. unlock_buffer(bh);
  1262. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1263. err = ext4_handle_dirty_metadata(handle, inode, bh);
  1264. if (!fatal)
  1265. fatal = err;
  1266. } else {
  1267. BUFFER_TRACE(bh, "not a new buffer");
  1268. }
  1269. if (fatal) {
  1270. *errp = fatal;
  1271. brelse(bh);
  1272. bh = NULL;
  1273. }
  1274. return bh;
  1275. }
  1276. err:
  1277. return NULL;
  1278. }
  1279. struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
  1280. ext4_lblk_t block, int create, int *err)
  1281. {
  1282. struct buffer_head *bh;
  1283. bh = ext4_getblk(handle, inode, block, create, err);
  1284. if (!bh)
  1285. return bh;
  1286. if (buffer_uptodate(bh))
  1287. return bh;
  1288. ll_rw_block(READ_META, 1, &bh);
  1289. wait_on_buffer(bh);
  1290. if (buffer_uptodate(bh))
  1291. return bh;
  1292. put_bh(bh);
  1293. *err = -EIO;
  1294. return NULL;
  1295. }
  1296. static int walk_page_buffers(handle_t *handle,
  1297. struct buffer_head *head,
  1298. unsigned from,
  1299. unsigned to,
  1300. int *partial,
  1301. int (*fn)(handle_t *handle,
  1302. struct buffer_head *bh))
  1303. {
  1304. struct buffer_head *bh;
  1305. unsigned block_start, block_end;
  1306. unsigned blocksize = head->b_size;
  1307. int err, ret = 0;
  1308. struct buffer_head *next;
  1309. for (bh = head, block_start = 0;
  1310. ret == 0 && (bh != head || !block_start);
  1311. block_start = block_end, bh = next) {
  1312. next = bh->b_this_page;
  1313. block_end = block_start + blocksize;
  1314. if (block_end <= from || block_start >= to) {
  1315. if (partial && !buffer_uptodate(bh))
  1316. *partial = 1;
  1317. continue;
  1318. }
  1319. err = (*fn)(handle, bh);
  1320. if (!ret)
  1321. ret = err;
  1322. }
  1323. return ret;
  1324. }
  1325. /*
  1326. * To preserve ordering, it is essential that the hole instantiation and
  1327. * the data write be encapsulated in a single transaction. We cannot
  1328. * close off a transaction and start a new one between the ext4_get_block()
  1329. * and the commit_write(). So doing the jbd2_journal_start at the start of
  1330. * prepare_write() is the right place.
  1331. *
  1332. * Also, this function can nest inside ext4_writepage() ->
  1333. * block_write_full_page(). In that case, we *know* that ext4_writepage()
  1334. * has generated enough buffer credits to do the whole page. So we won't
  1335. * block on the journal in that case, which is good, because the caller may
  1336. * be PF_MEMALLOC.
  1337. *
  1338. * By accident, ext4 can be reentered when a transaction is open via
  1339. * quota file writes. If we were to commit the transaction while thus
  1340. * reentered, there can be a deadlock - we would be holding a quota
  1341. * lock, and the commit would never complete if another thread had a
  1342. * transaction open and was blocking on the quota lock - a ranking
  1343. * violation.
  1344. *
  1345. * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
  1346. * will _not_ run commit under these circumstances because handle->h_ref
  1347. * is elevated. We'll still have enough credits for the tiny quotafile
  1348. * write.
  1349. */
  1350. static int do_journal_get_write_access(handle_t *handle,
  1351. struct buffer_head *bh)
  1352. {
  1353. if (!buffer_mapped(bh) || buffer_freed(bh))
  1354. return 0;
  1355. return ext4_journal_get_write_access(handle, bh);
  1356. }
  1357. /*
  1358. * Truncate blocks that were not used by write. We have to truncate the
  1359. * pagecache as well so that corresponding buffers get properly unmapped.
  1360. */
  1361. static void ext4_truncate_failed_write(struct inode *inode)
  1362. {
  1363. truncate_inode_pages(inode->i_mapping, inode->i_size);
  1364. ext4_truncate(inode);
  1365. }
  1366. static int ext4_write_begin(struct file *file, struct address_space *mapping,
  1367. loff_t pos, unsigned len, unsigned flags,
  1368. struct page **pagep, void **fsdata)
  1369. {
  1370. struct inode *inode = mapping->host;
  1371. int ret, needed_blocks;
  1372. handle_t *handle;
  1373. int retries = 0;
  1374. struct page *page;
  1375. pgoff_t index;
  1376. unsigned from, to;
  1377. trace_ext4_write_begin(inode, pos, len, flags);
  1378. /*
  1379. * Reserve one block more for addition to orphan list in case
  1380. * we allocate blocks but write fails for some reason
  1381. */
  1382. needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
  1383. index = pos >> PAGE_CACHE_SHIFT;
  1384. from = pos & (PAGE_CACHE_SIZE - 1);
  1385. to = from + len;
  1386. retry:
  1387. handle = ext4_journal_start(inode, needed_blocks);
  1388. if (IS_ERR(handle)) {
  1389. ret = PTR_ERR(handle);
  1390. goto out;
  1391. }
  1392. /* We cannot recurse into the filesystem as the transaction is already
  1393. * started */
  1394. flags |= AOP_FLAG_NOFS;
  1395. page = grab_cache_page_write_begin(mapping, index, flags);
  1396. if (!page) {
  1397. ext4_journal_stop(handle);
  1398. ret = -ENOMEM;
  1399. goto out;
  1400. }
  1401. *pagep = page;
  1402. ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  1403. ext4_get_block);
  1404. if (!ret && ext4_should_journal_data(inode)) {
  1405. ret = walk_page_buffers(handle, page_buffers(page),
  1406. from, to, NULL, do_journal_get_write_access);
  1407. }
  1408. if (ret) {
  1409. unlock_page(page);
  1410. page_cache_release(page);
  1411. /*
  1412. * block_write_begin may have instantiated a few blocks
  1413. * outside i_size. Trim these off again. Don't need
  1414. * i_size_read because we hold i_mutex.
  1415. *
  1416. * Add inode to orphan list in case we crash before
  1417. * truncate finishes
  1418. */
  1419. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  1420. ext4_orphan_add(handle, inode);
  1421. ext4_journal_stop(handle);
  1422. if (pos + len > inode->i_size) {
  1423. ext4_truncate_failed_write(inode);
  1424. /*
  1425. * If truncate failed early the inode might
  1426. * still be on the orphan list; we need to
  1427. * make sure the inode is removed from the
  1428. * orphan list in that case.
  1429. */
  1430. if (inode->i_nlink)
  1431. ext4_orphan_del(NULL, inode);
  1432. }
  1433. }
  1434. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  1435. goto retry;
  1436. out:
  1437. return ret;
  1438. }
  1439. /* For write_end() in data=journal mode */
  1440. static int write_end_fn(handle_t *handle, struct buffer_head *bh)
  1441. {
  1442. if (!buffer_mapped(bh) || buffer_freed(bh))
  1443. return 0;
  1444. set_buffer_uptodate(bh);
  1445. return ext4_handle_dirty_metadata(handle, NULL, bh);
  1446. }
  1447. static int ext4_generic_write_end(struct file *file,
  1448. struct address_space *mapping,
  1449. loff_t pos, unsigned len, unsigned copied,
  1450. struct page *page, void *fsdata)
  1451. {
  1452. int i_size_changed = 0;
  1453. struct inode *inode = mapping->host;
  1454. handle_t *handle = ext4_journal_current_handle();
  1455. copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
  1456. /*
  1457. * No need to use i_size_read() here, the i_size
  1458. * cannot change under us because we hold i_mutex.
  1459. *
  1460. * But it's important to update i_size while still holding page lock:
  1461. * page writeout could otherwise come in and zero beyond i_size.
  1462. */
  1463. if (pos + copied > inode->i_size) {
  1464. i_size_write(inode, pos + copied);
  1465. i_size_changed = 1;
  1466. }
  1467. if (pos + copied > EXT4_I(inode)->i_disksize) {
  1468. /* We need to mark inode dirty even if
  1469. * new_i_size is less that inode->i_size
  1470. * bu greater than i_disksize.(hint delalloc)
  1471. */
  1472. ext4_update_i_disksize(inode, (pos + copied));
  1473. i_size_changed = 1;
  1474. }
  1475. unlock_page(page);
  1476. page_cache_release(page);
  1477. /*
  1478. * Don't mark the inode dirty under page lock. First, it unnecessarily
  1479. * makes the holding time of page lock longer. Second, it forces lock
  1480. * ordering of page lock and transaction start for journaling
  1481. * filesystems.
  1482. */
  1483. if (i_size_changed)
  1484. ext4_mark_inode_dirty(handle, inode);
  1485. return copied;
  1486. }
  1487. /*
  1488. * We need to pick up the new inode size which generic_commit_write gave us
  1489. * `file' can be NULL - eg, when called from page_symlink().
  1490. *
  1491. * ext4 never places buffers on inode->i_mapping->private_list. metadata
  1492. * buffers are managed internally.
  1493. */
  1494. static int ext4_ordered_write_end(struct file *file,
  1495. struct address_space *mapping,
  1496. loff_t pos, unsigned len, unsigned copied,
  1497. struct page *page, void *fsdata)
  1498. {
  1499. handle_t *handle = ext4_journal_current_handle();
  1500. struct inode *inode = mapping->host;
  1501. int ret = 0, ret2;
  1502. trace_ext4_ordered_write_end(inode, pos, len, copied);
  1503. ret = ext4_jbd2_file_inode(handle, inode);
  1504. if (ret == 0) {
  1505. ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
  1506. page, fsdata);
  1507. copied = ret2;
  1508. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  1509. /* if we have allocated more blocks and copied
  1510. * less. We will have blocks allocated outside
  1511. * inode->i_size. So truncate them
  1512. */
  1513. ext4_orphan_add(handle, inode);
  1514. if (ret2 < 0)
  1515. ret = ret2;
  1516. }
  1517. ret2 = ext4_journal_stop(handle);
  1518. if (!ret)
  1519. ret = ret2;
  1520. if (pos + len > inode->i_size) {
  1521. ext4_truncate_failed_write(inode);
  1522. /*
  1523. * If truncate failed early the inode might still be
  1524. * on the orphan list; we need to make sure the inode
  1525. * is removed from the orphan list in that case.
  1526. */
  1527. if (inode->i_nlink)
  1528. ext4_orphan_del(NULL, inode);
  1529. }
  1530. return ret ? ret : copied;
  1531. }
  1532. static int ext4_writeback_write_end(struct file *file,
  1533. struct address_space *mapping,
  1534. loff_t pos, unsigned len, unsigned copied,
  1535. struct page *page, void *fsdata)
  1536. {
  1537. handle_t *handle = ext4_journal_current_handle();
  1538. struct inode *inode = mapping->host;
  1539. int ret = 0, ret2;
  1540. trace_ext4_writeback_write_end(inode, pos, len, copied);
  1541. ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
  1542. page, fsdata);
  1543. copied = ret2;
  1544. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  1545. /* if we have allocated more blocks and copied
  1546. * less. We will have blocks allocated outside
  1547. * inode->i_size. So truncate them
  1548. */
  1549. ext4_orphan_add(handle, inode);
  1550. if (ret2 < 0)
  1551. ret = ret2;
  1552. ret2 = ext4_journal_stop(handle);
  1553. if (!ret)
  1554. ret = ret2;
  1555. if (pos + len > inode->i_size) {
  1556. ext4_truncate_failed_write(inode);
  1557. /*
  1558. * If truncate failed early the inode might still be
  1559. * on the orphan list; we need to make sure the inode
  1560. * is removed from the orphan list in that case.
  1561. */
  1562. if (inode->i_nlink)
  1563. ext4_orphan_del(NULL, inode);
  1564. }
  1565. return ret ? ret : copied;
  1566. }
  1567. static int ext4_journalled_write_end(struct file *file,
  1568. struct address_space *mapping,
  1569. loff_t pos, unsigned len, unsigned copied,
  1570. struct page *page, void *fsdata)
  1571. {
  1572. handle_t *handle = ext4_journal_current_handle();
  1573. struct inode *inode = mapping->host;
  1574. int ret = 0, ret2;
  1575. int partial = 0;
  1576. unsigned from, to;
  1577. loff_t new_i_size;
  1578. trace_ext4_journalled_write_end(inode, pos, len, copied);
  1579. from = pos & (PAGE_CACHE_SIZE - 1);
  1580. to = from + len;
  1581. if (copied < len) {
  1582. if (!PageUptodate(page))
  1583. copied = 0;
  1584. page_zero_new_buffers(page, from+copied, to);
  1585. }
  1586. ret = walk_page_buffers(handle, page_buffers(page), from,
  1587. to, &partial, write_end_fn);
  1588. if (!partial)
  1589. SetPageUptodate(page);
  1590. new_i_size = pos + copied;
  1591. if (new_i_size > inode->i_size)
  1592. i_size_write(inode, pos+copied);
  1593. EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
  1594. if (new_i_size > EXT4_I(inode)->i_disksize) {
  1595. ext4_update_i_disksize(inode, new_i_size);
  1596. ret2 = ext4_mark_inode_dirty(handle, inode);
  1597. if (!ret)
  1598. ret = ret2;
  1599. }
  1600. unlock_page(page);
  1601. page_cache_release(page);
  1602. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  1603. /* if we have allocated more blocks and copied
  1604. * less. We will have blocks allocated outside
  1605. * inode->i_size. So truncate them
  1606. */
  1607. ext4_orphan_add(handle, inode);
  1608. ret2 = ext4_journal_stop(handle);
  1609. if (!ret)
  1610. ret = ret2;
  1611. if (pos + len > inode->i_size) {
  1612. ext4_truncate_failed_write(inode);
  1613. /*
  1614. * If truncate failed early the inode might still be
  1615. * on the orphan list; we need to make sure the inode
  1616. * is removed from the orphan list in that case.
  1617. */
  1618. if (inode->i_nlink)
  1619. ext4_orphan_del(NULL, inode);
  1620. }
  1621. return ret ? ret : copied;
  1622. }
  1623. static int ext4_da_reserve_space(struct inode *inode, int nrblocks)
  1624. {
  1625. int retries = 0;
  1626. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1627. unsigned long md_needed, mdblocks, total = 0;
  1628. /*
  1629. * recalculate the amount of metadata blocks to reserve
  1630. * in order to allocate nrblocks
  1631. * worse case is one extent per block
  1632. */
  1633. repeat:
  1634. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  1635. total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks;
  1636. mdblocks = ext4_calc_metadata_amount(inode, total);
  1637. BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks);
  1638. md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks;
  1639. total = md_needed + nrblocks;
  1640. /*
  1641. * Make quota reservation here to prevent quota overflow
  1642. * later. Real quota accounting is done at pages writeout
  1643. * time.
  1644. */
  1645. if (vfs_dq_reserve_block(inode, total)) {
  1646. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1647. return -EDQUOT;
  1648. }
  1649. if (ext4_claim_free_blocks(sbi, total)) {
  1650. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1651. vfs_dq_release_reservation_block(inode, total);
  1652. if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
  1653. yield();
  1654. goto repeat;
  1655. }
  1656. return -ENOSPC;
  1657. }
  1658. EXT4_I(inode)->i_reserved_data_blocks += nrblocks;
  1659. EXT4_I(inode)->i_reserved_meta_blocks = mdblocks;
  1660. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1661. return 0; /* success */
  1662. }
  1663. static void ext4_da_release_space(struct inode *inode, int to_free)
  1664. {
  1665. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1666. int total, mdb, mdb_free, release;
  1667. if (!to_free)
  1668. return; /* Nothing to release, exit */
  1669. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  1670. if (!EXT4_I(inode)->i_reserved_data_blocks) {
  1671. /*
  1672. * if there is no reserved blocks, but we try to free some
  1673. * then the counter is messed up somewhere.
  1674. * but since this function is called from invalidate
  1675. * page, it's harmless to return without any action
  1676. */
  1677. printk(KERN_INFO "ext4 delalloc try to release %d reserved "
  1678. "blocks for inode %lu, but there is no reserved "
  1679. "data blocks\n", to_free, inode->i_ino);
  1680. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1681. return;
  1682. }
  1683. /* recalculate the number of metablocks still need to be reserved */
  1684. total = EXT4_I(inode)->i_reserved_data_blocks - to_free;
  1685. mdb = ext4_calc_metadata_amount(inode, total);
  1686. /* figure out how many metablocks to release */
  1687. BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
  1688. mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
  1689. release = to_free + mdb_free;
  1690. /* update fs dirty blocks counter for truncate case */
  1691. percpu_counter_sub(&sbi->s_dirtyblocks_counter, release);
  1692. /* update per-inode reservations */
  1693. BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks);
  1694. EXT4_I(inode)->i_reserved_data_blocks -= to_free;
  1695. BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
  1696. EXT4_I(inode)->i_reserved_meta_blocks = mdb;
  1697. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1698. vfs_dq_release_reservation_block(inode, release);
  1699. }
  1700. static void ext4_da_page_release_reservation(struct page *page,
  1701. unsigned long offset)
  1702. {
  1703. int to_release = 0;
  1704. struct buffer_head *head, *bh;
  1705. unsigned int curr_off = 0;
  1706. head = page_buffers(page);
  1707. bh = head;
  1708. do {
  1709. unsigned int next_off = curr_off + bh->b_size;
  1710. if ((offset <= curr_off) && (buffer_delay(bh))) {
  1711. to_release++;
  1712. clear_buffer_delay(bh);
  1713. }
  1714. curr_off = next_off;
  1715. } while ((bh = bh->b_this_page) != head);
  1716. ext4_da_release_space(page->mapping->host, to_release);
  1717. }
  1718. /*
  1719. * Delayed allocation stuff
  1720. */
  1721. /*
  1722. * mpage_da_submit_io - walks through extent of pages and try to write
  1723. * them with writepage() call back
  1724. *
  1725. * @mpd->inode: inode
  1726. * @mpd->first_page: first page of the extent
  1727. * @mpd->next_page: page after the last page of the extent
  1728. *
  1729. * By the time mpage_da_submit_io() is called we expect all blocks
  1730. * to be allocated. this may be wrong if allocation failed.
  1731. *
  1732. * As pages are already locked by write_cache_pages(), we can't use it
  1733. */
  1734. static int mpage_da_submit_io(struct mpage_da_data *mpd)
  1735. {
  1736. long pages_skipped;
  1737. struct pagevec pvec;
  1738. unsigned long index, end;
  1739. int ret = 0, err, nr_pages, i;
  1740. struct inode *inode = mpd->inode;
  1741. struct address_space *mapping = inode->i_mapping;
  1742. BUG_ON(mpd->next_page <= mpd->first_page);
  1743. /*
  1744. * We need to start from the first_page to the next_page - 1
  1745. * to make sure we also write the mapped dirty buffer_heads.
  1746. * If we look at mpd->b_blocknr we would only be looking
  1747. * at the currently mapped buffer_heads.
  1748. */
  1749. index = mpd->first_page;
  1750. end = mpd->next_page - 1;
  1751. pagevec_init(&pvec, 0);
  1752. while (index <= end) {
  1753. nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
  1754. if (nr_pages == 0)
  1755. break;
  1756. for (i = 0; i < nr_pages; i++) {
  1757. struct page *page = pvec.pages[i];
  1758. index = page->index;
  1759. if (index > end)
  1760. break;
  1761. index++;
  1762. BUG_ON(!PageLocked(page));
  1763. BUG_ON(PageWriteback(page));
  1764. pages_skipped = mpd->wbc->pages_skipped;
  1765. err = mapping->a_ops->writepage(page, mpd->wbc);
  1766. if (!err && (pages_skipped == mpd->wbc->pages_skipped))
  1767. /*
  1768. * have successfully written the page
  1769. * without skipping the same
  1770. */
  1771. mpd->pages_written++;
  1772. /*
  1773. * In error case, we have to continue because
  1774. * remaining pages are still locked
  1775. * XXX: unlock and re-dirty them?
  1776. */
  1777. if (ret == 0)
  1778. ret = err;
  1779. }
  1780. pagevec_release(&pvec);
  1781. }
  1782. return ret;
  1783. }
  1784. /*
  1785. * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers
  1786. *
  1787. * @mpd->inode - inode to walk through
  1788. * @exbh->b_blocknr - first block on a disk
  1789. * @exbh->b_size - amount of space in bytes
  1790. * @logical - first logical block to start assignment with
  1791. *
  1792. * the function goes through all passed space and put actual disk
  1793. * block numbers into buffer heads, dropping BH_Delay and BH_Unwritten
  1794. */
  1795. static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical,
  1796. struct buffer_head *exbh)
  1797. {
  1798. struct inode *inode = mpd->inode;
  1799. struct address_space *mapping = inode->i_mapping;
  1800. int blocks = exbh->b_size >> inode->i_blkbits;
  1801. sector_t pblock = exbh->b_blocknr, cur_logical;
  1802. struct buffer_head *head, *bh;
  1803. pgoff_t index, end;
  1804. struct pagevec pvec;
  1805. int nr_pages, i;
  1806. index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1807. end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1808. cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1809. pagevec_init(&pvec, 0);
  1810. while (index <= end) {
  1811. /* XXX: optimize tail */
  1812. nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
  1813. if (nr_pages == 0)
  1814. break;
  1815. for (i = 0; i < nr_pages; i++) {
  1816. struct page *page = pvec.pages[i];
  1817. index = page->index;
  1818. if (index > end)
  1819. break;
  1820. index++;
  1821. BUG_ON(!PageLocked(page));
  1822. BUG_ON(PageWriteback(page));
  1823. BUG_ON(!page_has_buffers(page));
  1824. bh = page_buffers(page);
  1825. head = bh;
  1826. /* skip blocks out of the range */
  1827. do {
  1828. if (cur_logical >= logical)
  1829. break;
  1830. cur_logical++;
  1831. } while ((bh = bh->b_this_page) != head);
  1832. do {
  1833. if (cur_logical >= logical + blocks)
  1834. break;
  1835. if (buffer_delay(bh) ||
  1836. buffer_unwritten(bh)) {
  1837. BUG_ON(bh->b_bdev != inode->i_sb->s_bdev);
  1838. if (buffer_delay(bh)) {
  1839. clear_buffer_delay(bh);
  1840. bh->b_blocknr = pblock;
  1841. } else {
  1842. /*
  1843. * unwritten already should have
  1844. * blocknr assigned. Verify that
  1845. */
  1846. clear_buffer_unwritten(bh);
  1847. BUG_ON(bh->b_blocknr != pblock);
  1848. }
  1849. } else if (buffer_mapped(bh))
  1850. BUG_ON(bh->b_blocknr != pblock);
  1851. cur_logical++;
  1852. pblock++;
  1853. } while ((bh = bh->b_this_page) != head);
  1854. }
  1855. pagevec_release(&pvec);
  1856. }
  1857. }
  1858. /*
  1859. * __unmap_underlying_blocks - just a helper function to unmap
  1860. * set of blocks described by @bh
  1861. */
  1862. static inline void __unmap_underlying_blocks(struct inode *inode,
  1863. struct buffer_head *bh)
  1864. {
  1865. struct block_device *bdev = inode->i_sb->s_bdev;
  1866. int blocks, i;
  1867. blocks = bh->b_size >> inode->i_blkbits;
  1868. for (i = 0; i < blocks; i++)
  1869. unmap_underlying_metadata(bdev, bh->b_blocknr + i);
  1870. }
  1871. static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd,
  1872. sector_t logical, long blk_cnt)
  1873. {
  1874. int nr_pages, i;
  1875. pgoff_t index, end;
  1876. struct pagevec pvec;
  1877. struct inode *inode = mpd->inode;
  1878. struct address_space *mapping = inode->i_mapping;
  1879. index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1880. end = (logical + blk_cnt - 1) >>
  1881. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1882. while (index <= end) {
  1883. nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
  1884. if (nr_pages == 0)
  1885. break;
  1886. for (i = 0; i < nr_pages; i++) {
  1887. struct page *page = pvec.pages[i];
  1888. index = page->index;
  1889. if (index > end)
  1890. break;
  1891. index++;
  1892. BUG_ON(!PageLocked(page));
  1893. BUG_ON(PageWriteback(page));
  1894. block_invalidatepage(page, 0);
  1895. ClearPageUptodate(page);
  1896. unlock_page(page);
  1897. }
  1898. }
  1899. return;
  1900. }
  1901. static void ext4_print_free_blocks(struct inode *inode)
  1902. {
  1903. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1904. printk(KERN_CRIT "Total free blocks count %lld\n",
  1905. ext4_count_free_blocks(inode->i_sb));
  1906. printk(KERN_CRIT "Free/Dirty block details\n");
  1907. printk(KERN_CRIT "free_blocks=%lld\n",
  1908. (long long) percpu_counter_sum(&sbi->s_freeblocks_counter));
  1909. printk(KERN_CRIT "dirty_blocks=%lld\n",
  1910. (long long) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
  1911. printk(KERN_CRIT "Block reservation details\n");
  1912. printk(KERN_CRIT "i_reserved_data_blocks=%u\n",
  1913. EXT4_I(inode)->i_reserved_data_blocks);
  1914. printk(KERN_CRIT "i_reserved_meta_blocks=%u\n",
  1915. EXT4_I(inode)->i_reserved_meta_blocks);
  1916. return;
  1917. }
  1918. /*
  1919. * mpage_da_map_blocks - go through given space
  1920. *
  1921. * @mpd - bh describing space
  1922. *
  1923. * The function skips space we know is already mapped to disk blocks.
  1924. *
  1925. */
  1926. static int mpage_da_map_blocks(struct mpage_da_data *mpd)
  1927. {
  1928. int err, blks, get_blocks_flags;
  1929. struct buffer_head new;
  1930. sector_t next = mpd->b_blocknr;
  1931. unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
  1932. loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
  1933. handle_t *handle = NULL;
  1934. /*
  1935. * We consider only non-mapped and non-allocated blocks
  1936. */
  1937. if ((mpd->b_state & (1 << BH_Mapped)) &&
  1938. !(mpd->b_state & (1 << BH_Delay)) &&
  1939. !(mpd->b_state & (1 << BH_Unwritten)))
  1940. return 0;
  1941. /*
  1942. * If we didn't accumulate anything to write simply return
  1943. */
  1944. if (!mpd->b_size)
  1945. return 0;
  1946. handle = ext4_journal_current_handle();
  1947. BUG_ON(!handle);
  1948. /*
  1949. * Call ext4_get_blocks() to allocate any delayed allocation
  1950. * blocks, or to convert an uninitialized extent to be
  1951. * initialized (in the case where we have written into
  1952. * one or more preallocated blocks).
  1953. *
  1954. * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
  1955. * indicate that we are on the delayed allocation path. This
  1956. * affects functions in many different parts of the allocation
  1957. * call path. This flag exists primarily because we don't
  1958. * want to change *many* call functions, so ext4_get_blocks()
  1959. * will set the magic i_delalloc_reserved_flag once the
  1960. * inode's allocation semaphore is taken.
  1961. *
  1962. * If the blocks in questions were delalloc blocks, set
  1963. * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
  1964. * variables are updated after the blocks have been allocated.
  1965. */
  1966. new.b_state = 0;
  1967. get_blocks_flags = (EXT4_GET_BLOCKS_CREATE |
  1968. EXT4_GET_BLOCKS_DELALLOC_RESERVE);
  1969. if (mpd->b_state & (1 << BH_Delay))
  1970. get_blocks_flags |= EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE;
  1971. blks = ext4_get_blocks(handle, mpd->inode, next, max_blocks,
  1972. &new, get_blocks_flags);
  1973. if (blks < 0) {
  1974. err = blks;
  1975. /*
  1976. * If get block returns with error we simply
  1977. * return. Later writepage will redirty the page and
  1978. * writepages will find the dirty page again
  1979. */
  1980. if (err == -EAGAIN)
  1981. return 0;
  1982. if (err == -ENOSPC &&
  1983. ext4_count_free_blocks(mpd->inode->i_sb)) {
  1984. mpd->retval = err;
  1985. return 0;
  1986. }
  1987. /*
  1988. * get block failure will cause us to loop in
  1989. * writepages, because a_ops->writepage won't be able
  1990. * to make progress. The page will be redirtied by
  1991. * writepage and writepages will again try to write
  1992. * the same.
  1993. */
  1994. ext4_msg(mpd->inode->i_sb, KERN_CRIT,
  1995. "delayed block allocation failed for inode %lu at "
  1996. "logical offset %llu with max blocks %zd with "
  1997. "error %d\n", mpd->inode->i_ino,
  1998. (unsigned long long) next,
  1999. mpd->b_size >> mpd->inode->i_blkbits, err);
  2000. printk(KERN_CRIT "This should not happen!! "
  2001. "Data will be lost\n");
  2002. if (err == -ENOSPC) {
  2003. ext4_print_free_blocks(mpd->inode);
  2004. }
  2005. /* invalidate all the pages */
  2006. ext4_da_block_invalidatepages(mpd, next,
  2007. mpd->b_size >> mpd->inode->i_blkbits);
  2008. return err;
  2009. }
  2010. BUG_ON(blks == 0);
  2011. new.b_size = (blks << mpd->inode->i_blkbits);
  2012. if (buffer_new(&new))
  2013. __unmap_underlying_blocks(mpd->inode, &new);
  2014. /*
  2015. * If blocks are delayed marked, we need to
  2016. * put actual blocknr and drop delayed bit
  2017. */
  2018. if ((mpd->b_state & (1 << BH_Delay)) ||
  2019. (mpd->b_state & (1 << BH_Unwritten)))
  2020. mpage_put_bnr_to_bhs(mpd, next, &new);
  2021. if (ext4_should_order_data(mpd->inode)) {
  2022. err = ext4_jbd2_file_inode(handle, mpd->inode);
  2023. if (err)
  2024. return err;
  2025. }
  2026. /*
  2027. * Update on-disk size along with block allocation.
  2028. */
  2029. disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
  2030. if (disksize > i_size_read(mpd->inode))
  2031. disksize = i_size_read(mpd->inode);
  2032. if (disksize > EXT4_I(mpd->inode)->i_disksize) {
  2033. ext4_update_i_disksize(mpd->inode, disksize);
  2034. return ext4_mark_inode_dirty(handle, mpd->inode);
  2035. }
  2036. return 0;
  2037. }
  2038. #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
  2039. (1 << BH_Delay) | (1 << BH_Unwritten))
  2040. /*
  2041. * mpage_add_bh_to_extent - try to add one more block to extent of blocks
  2042. *
  2043. * @mpd->lbh - extent of blocks
  2044. * @logical - logical number of the block in the file
  2045. * @bh - bh of the block (used to access block's state)
  2046. *
  2047. * the function is used to collect contig. blocks in same state
  2048. */
  2049. static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
  2050. sector_t logical, size_t b_size,
  2051. unsigned long b_state)
  2052. {
  2053. sector_t next;
  2054. int nrblocks = mpd->b_size >> mpd->inode->i_blkbits;
  2055. /* check if thereserved journal credits might overflow */
  2056. if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) {
  2057. if (nrblocks >= EXT4_MAX_TRANS_DATA) {
  2058. /*
  2059. * With non-extent format we are limited by the journal
  2060. * credit available. Total credit needed to insert
  2061. * nrblocks contiguous blocks is dependent on the
  2062. * nrblocks. So limit nrblocks.
  2063. */
  2064. goto flush_it;
  2065. } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
  2066. EXT4_MAX_TRANS_DATA) {
  2067. /*
  2068. * Adding the new buffer_head would make it cross the
  2069. * allowed limit for which we have journal credit
  2070. * reserved. So limit the new bh->b_size
  2071. */
  2072. b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
  2073. mpd->inode->i_blkbits;
  2074. /* we will do mpage_da_submit_io in the next loop */
  2075. }
  2076. }
  2077. /*
  2078. * First block in the extent
  2079. */
  2080. if (mpd->b_size == 0) {
  2081. mpd->b_blocknr = logical;
  2082. mpd->b_size = b_size;
  2083. mpd->b_state = b_state & BH_FLAGS;
  2084. return;
  2085. }
  2086. next = mpd->b_blocknr + nrblocks;
  2087. /*
  2088. * Can we merge the block to our big extent?
  2089. */
  2090. if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
  2091. mpd->b_size += b_size;
  2092. return;
  2093. }
  2094. flush_it:
  2095. /*
  2096. * We couldn't merge the block to our extent, so we
  2097. * need to flush current extent and start new one
  2098. */
  2099. if (mpage_da_map_blocks(mpd) == 0)
  2100. mpage_da_submit_io(mpd);
  2101. mpd->io_done = 1;
  2102. return;
  2103. }
  2104. static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
  2105. {
  2106. return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
  2107. }
  2108. /*
  2109. * __mpage_da_writepage - finds extent of pages and blocks
  2110. *
  2111. * @page: page to consider
  2112. * @wbc: not used, we just follow rules
  2113. * @data: context
  2114. *
  2115. * The function finds extents of pages and scan them for all blocks.
  2116. */
  2117. static int __mpage_da_writepage(struct page *page,
  2118. struct writeback_control *wbc, void *data)
  2119. {
  2120. struct mpage_da_data *mpd = data;
  2121. struct inode *inode = mpd->inode;
  2122. struct buffer_head *bh, *head;
  2123. sector_t logical;
  2124. if (mpd->io_done) {
  2125. /*
  2126. * Rest of the page in the page_vec
  2127. * redirty then and skip then. We will
  2128. * try to write them again after
  2129. * starting a new transaction
  2130. */
  2131. redirty_page_for_writepage(wbc, page);
  2132. unlock_page(page);
  2133. return MPAGE_DA_EXTENT_TAIL;
  2134. }
  2135. /*
  2136. * Can we merge this page to current extent?
  2137. */
  2138. if (mpd->next_page != page->index) {
  2139. /*
  2140. * Nope, we can't. So, we map non-allocated blocks
  2141. * and start IO on them using writepage()
  2142. */
  2143. if (mpd->next_page != mpd->first_page) {
  2144. if (mpage_da_map_blocks(mpd) == 0)
  2145. mpage_da_submit_io(mpd);
  2146. /*
  2147. * skip rest of the page in the page_vec
  2148. */
  2149. mpd->io_done = 1;
  2150. redirty_page_for_writepage(wbc, page);
  2151. unlock_page(page);
  2152. return MPAGE_DA_EXTENT_TAIL;
  2153. }
  2154. /*
  2155. * Start next extent of pages ...
  2156. */
  2157. mpd->first_page = page->index;
  2158. /*
  2159. * ... and blocks
  2160. */
  2161. mpd->b_size = 0;
  2162. mpd->b_state = 0;
  2163. mpd->b_blocknr = 0;
  2164. }
  2165. mpd->next_page = page->index + 1;
  2166. logical = (sector_t) page->index <<
  2167. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  2168. if (!page_has_buffers(page)) {
  2169. mpage_add_bh_to_extent(mpd, logical, PAGE_CACHE_SIZE,
  2170. (1 << BH_Dirty) | (1 << BH_Uptodate));
  2171. if (mpd->io_done)
  2172. return MPAGE_DA_EXTENT_TAIL;
  2173. } else {
  2174. /*
  2175. * Page with regular buffer heads, just add all dirty ones
  2176. */
  2177. head = page_buffers(page);
  2178. bh = head;
  2179. do {
  2180. BUG_ON(buffer_locked(bh));
  2181. /*
  2182. * We need to try to allocate
  2183. * unmapped blocks in the same page.
  2184. * Otherwise we won't make progress
  2185. * with the page in ext4_writepage
  2186. */
  2187. if (ext4_bh_delay_or_unwritten(NULL, bh)) {
  2188. mpage_add_bh_to_extent(mpd, logical,
  2189. bh->b_size,
  2190. bh->b_state);
  2191. if (mpd->io_done)
  2192. return MPAGE_DA_EXTENT_TAIL;
  2193. } else if (buffer_dirty(bh) && (buffer_mapped(bh))) {
  2194. /*
  2195. * mapped dirty buffer. We need to update
  2196. * the b_state because we look at
  2197. * b_state in mpage_da_map_blocks. We don't
  2198. * update b_size because if we find an
  2199. * unmapped buffer_head later we need to
  2200. * use the b_state flag of that buffer_head.
  2201. */
  2202. if (mpd->b_size == 0)
  2203. mpd->b_state = bh->b_state & BH_FLAGS;
  2204. }
  2205. logical++;
  2206. } while ((bh = bh->b_this_page) != head);
  2207. }
  2208. return 0;
  2209. }
  2210. /*
  2211. * This is a special get_blocks_t callback which is used by
  2212. * ext4_da_write_begin(). It will either return mapped block or
  2213. * reserve space for a single block.
  2214. *
  2215. * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
  2216. * We also have b_blocknr = -1 and b_bdev initialized properly
  2217. *
  2218. * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
  2219. * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
  2220. * initialized properly.
  2221. */
  2222. static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
  2223. struct buffer_head *bh_result, int create)
  2224. {
  2225. int ret = 0;
  2226. sector_t invalid_block = ~((sector_t) 0xffff);
  2227. if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
  2228. invalid_block = ~0;
  2229. BUG_ON(create == 0);
  2230. BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
  2231. /*
  2232. * first, we need to know whether the block is allocated already
  2233. * preallocated blocks are unmapped but should treated
  2234. * the same as allocated blocks.
  2235. */
  2236. ret = ext4_get_blocks(NULL, inode, iblock, 1, bh_result, 0);
  2237. if ((ret == 0) && !buffer_delay(bh_result)) {
  2238. /* the block isn't (pre)allocated yet, let's reserve space */
  2239. /*
  2240. * XXX: __block_prepare_write() unmaps passed block,
  2241. * is it OK?
  2242. */
  2243. ret = ext4_da_reserve_space(inode, 1);
  2244. if (ret)
  2245. /* not enough space to reserve */
  2246. return ret;
  2247. map_bh(bh_result, inode->i_sb, invalid_block);
  2248. set_buffer_new(bh_result);
  2249. set_buffer_delay(bh_result);
  2250. } else if (ret > 0) {
  2251. bh_result->b_size = (ret << inode->i_blkbits);
  2252. if (buffer_unwritten(bh_result)) {
  2253. /* A delayed write to unwritten bh should
  2254. * be marked new and mapped. Mapped ensures
  2255. * that we don't do get_block multiple times
  2256. * when we write to the same offset and new
  2257. * ensures that we do proper zero out for
  2258. * partial write.
  2259. */
  2260. set_buffer_new(bh_result);
  2261. set_buffer_mapped(bh_result);
  2262. }
  2263. ret = 0;
  2264. }
  2265. return ret;
  2266. }
  2267. /*
  2268. * This function is used as a standard get_block_t calback function
  2269. * when there is no desire to allocate any blocks. It is used as a
  2270. * callback function for block_prepare_write(), nobh_writepage(), and
  2271. * block_write_full_page(). These functions should only try to map a
  2272. * single block at a time.
  2273. *
  2274. * Since this function doesn't do block allocations even if the caller
  2275. * requests it by passing in create=1, it is critically important that
  2276. * any caller checks to make sure that any buffer heads are returned
  2277. * by this function are either all already mapped or marked for
  2278. * delayed allocation before calling nobh_writepage() or
  2279. * block_write_full_page(). Otherwise, b_blocknr could be left
  2280. * unitialized, and the page write functions will be taken by
  2281. * surprise.
  2282. */
  2283. static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
  2284. struct buffer_head *bh_result, int create)
  2285. {
  2286. int ret = 0;
  2287. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  2288. BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
  2289. /*
  2290. * we don't want to do block allocation in writepage
  2291. * so call get_block_wrap with create = 0
  2292. */
  2293. ret = ext4_get_blocks(NULL, inode, iblock, max_blocks, bh_result, 0);
  2294. if (ret > 0) {
  2295. bh_result->b_size = (ret << inode->i_blkbits);
  2296. ret = 0;
  2297. }
  2298. return ret;
  2299. }
  2300. static int bget_one(handle_t *handle, struct buffer_head *bh)
  2301. {
  2302. get_bh(bh);
  2303. return 0;
  2304. }
  2305. static int bput_one(handle_t *handle, struct buffer_head *bh)
  2306. {
  2307. put_bh(bh);
  2308. return 0;
  2309. }
  2310. static int __ext4_journalled_writepage(struct page *page,
  2311. unsigned int len)
  2312. {
  2313. struct address_space *mapping = page->mapping;
  2314. struct inode *inode = mapping->host;
  2315. struct buffer_head *page_bufs;
  2316. handle_t *handle = NULL;
  2317. int ret = 0;
  2318. int err;
  2319. page_bufs = page_buffers(page);
  2320. BUG_ON(!page_bufs);
  2321. walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one);
  2322. /* As soon as we unlock the page, it can go away, but we have
  2323. * references to buffers so we are safe */
  2324. unlock_page(page);
  2325. handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
  2326. if (IS_ERR(handle)) {
  2327. ret = PTR_ERR(handle);
  2328. goto out;
  2329. }
  2330. ret = walk_page_buffers(handle, page_bufs, 0, len, NULL,
  2331. do_journal_get_write_access);
  2332. err = walk_page_buffers(handle, page_bufs, 0, len, NULL,
  2333. write_end_fn);
  2334. if (ret == 0)
  2335. ret = err;
  2336. err = ext4_journal_stop(handle);
  2337. if (!ret)
  2338. ret = err;
  2339. walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one);
  2340. EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
  2341. out:
  2342. return ret;
  2343. }
  2344. /*
  2345. * Note that we don't need to start a transaction unless we're journaling data
  2346. * because we should have holes filled from ext4_page_mkwrite(). We even don't
  2347. * need to file the inode to the transaction's list in ordered mode because if
  2348. * we are writing back data added by write(), the inode is already there and if
  2349. * we are writing back data modified via mmap(), noone guarantees in which
  2350. * transaction the data will hit the disk. In case we are journaling data, we
  2351. * cannot start transaction directly because transaction start ranks above page
  2352. * lock so we have to do some magic.
  2353. *
  2354. * This function can get called via...
  2355. * - ext4_da_writepages after taking page lock (have journal handle)
  2356. * - journal_submit_inode_data_buffers (no journal handle)
  2357. * - shrink_page_list via pdflush (no journal handle)
  2358. * - grab_page_cache when doing write_begin (have journal handle)
  2359. *
  2360. * We don't do any block allocation in this function. If we have page with
  2361. * multiple blocks we need to write those buffer_heads that are mapped. This
  2362. * is important for mmaped based write. So if we do with blocksize 1K
  2363. * truncate(f, 1024);
  2364. * a = mmap(f, 0, 4096);
  2365. * a[0] = 'a';
  2366. * truncate(f, 4096);
  2367. * we have in the page first buffer_head mapped via page_mkwrite call back
  2368. * but other bufer_heads would be unmapped but dirty(dirty done via the
  2369. * do_wp_page). So writepage should write the first block. If we modify
  2370. * the mmap area beyond 1024 we will again get a page_fault and the
  2371. * page_mkwrite callback will do the block allocation and mark the
  2372. * buffer_heads mapped.
  2373. *
  2374. * We redirty the page if we have any buffer_heads that is either delay or
  2375. * unwritten in the page.
  2376. *
  2377. * We can get recursively called as show below.
  2378. *
  2379. * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
  2380. * ext4_writepage()
  2381. *
  2382. * But since we don't do any block allocation we should not deadlock.
  2383. * Page also have the dirty flag cleared so we don't get recurive page_lock.
  2384. */
  2385. static int ext4_writepage(struct page *page,
  2386. struct writeback_control *wbc)
  2387. {
  2388. int ret = 0;
  2389. loff_t size;
  2390. unsigned int len;
  2391. struct buffer_head *page_bufs;
  2392. struct inode *inode = page->mapping->host;
  2393. trace_ext4_writepage(inode, page);
  2394. size = i_size_read(inode);
  2395. if (page->index == size >> PAGE_CACHE_SHIFT)
  2396. len = size & ~PAGE_CACHE_MASK;
  2397. else
  2398. len = PAGE_CACHE_SIZE;
  2399. if (page_has_buffers(page)) {
  2400. page_bufs = page_buffers(page);
  2401. if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
  2402. ext4_bh_delay_or_unwritten)) {
  2403. /*
  2404. * We don't want to do block allocation
  2405. * So redirty the page and return
  2406. * We may reach here when we do a journal commit
  2407. * via journal_submit_inode_data_buffers.
  2408. * If we don't have mapping block we just ignore
  2409. * them. We can also reach here via shrink_page_list
  2410. */
  2411. redirty_page_for_writepage(wbc, page);
  2412. unlock_page(page);
  2413. return 0;
  2414. }
  2415. } else {
  2416. /*
  2417. * The test for page_has_buffers() is subtle:
  2418. * We know the page is dirty but it lost buffers. That means
  2419. * that at some moment in time after write_begin()/write_end()
  2420. * has been called all buffers have been clean and thus they
  2421. * must have been written at least once. So they are all
  2422. * mapped and we can happily proceed with mapping them
  2423. * and writing the page.
  2424. *
  2425. * Try to initialize the buffer_heads and check whether
  2426. * all are mapped and non delay. We don't want to
  2427. * do block allocation here.
  2428. */
  2429. ret = block_prepare_write(page, 0, len,
  2430. noalloc_get_block_write);
  2431. if (!ret) {
  2432. page_bufs = page_buffers(page);
  2433. /* check whether all are mapped and non delay */
  2434. if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
  2435. ext4_bh_delay_or_unwritten)) {
  2436. redirty_page_for_writepage(wbc, page);
  2437. unlock_page(page);
  2438. return 0;
  2439. }
  2440. } else {
  2441. /*
  2442. * We can't do block allocation here
  2443. * so just redity the page and unlock
  2444. * and return
  2445. */
  2446. redirty_page_for_writepage(wbc, page);
  2447. unlock_page(page);
  2448. return 0;
  2449. }
  2450. /* now mark the buffer_heads as dirty and uptodate */
  2451. block_commit_write(page, 0, len);
  2452. }
  2453. if (PageChecked(page) && ext4_should_journal_data(inode)) {
  2454. /*
  2455. * It's mmapped pagecache. Add buffers and journal it. There
  2456. * doesn't seem much point in redirtying the page here.
  2457. */
  2458. ClearPageChecked(page);
  2459. return __ext4_journalled_writepage(page, len);
  2460. }
  2461. if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode))
  2462. ret = nobh_writepage(page, noalloc_get_block_write, wbc);
  2463. else
  2464. ret = block_write_full_page(page, noalloc_get_block_write,
  2465. wbc);
  2466. return ret;
  2467. }
  2468. /*
  2469. * This is called via ext4_da_writepages() to
  2470. * calulate the total number of credits to reserve to fit
  2471. * a single extent allocation into a single transaction,
  2472. * ext4_da_writpeages() will loop calling this before
  2473. * the block allocation.
  2474. */
  2475. static int ext4_da_writepages_trans_blocks(struct inode *inode)
  2476. {
  2477. int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
  2478. /*
  2479. * With non-extent format the journal credit needed to
  2480. * insert nrblocks contiguous block is dependent on
  2481. * number of contiguous block. So we will limit
  2482. * number of contiguous block to a sane value
  2483. */
  2484. if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) &&
  2485. (max_blocks > EXT4_MAX_TRANS_DATA))
  2486. max_blocks = EXT4_MAX_TRANS_DATA;
  2487. return ext4_chunk_trans_blocks(inode, max_blocks);
  2488. }
  2489. static int ext4_da_writepages(struct address_space *mapping,
  2490. struct writeback_control *wbc)
  2491. {
  2492. pgoff_t index;
  2493. int range_whole = 0;
  2494. handle_t *handle = NULL;
  2495. struct mpage_da_data mpd;
  2496. struct inode *inode = mapping->host;
  2497. int no_nrwrite_index_update;
  2498. int pages_written = 0;
  2499. long pages_skipped;
  2500. unsigned int max_pages;
  2501. int range_cyclic, cycled = 1, io_done = 0;
  2502. int needed_blocks, ret = 0;
  2503. long desired_nr_to_write, nr_to_writebump = 0;
  2504. loff_t range_start = wbc->range_start;
  2505. struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
  2506. trace_ext4_da_writepages(inode, wbc);
  2507. /*
  2508. * No pages to write? This is mainly a kludge to avoid starting
  2509. * a transaction for special inodes like journal inode on last iput()
  2510. * because that could violate lock ordering on umount
  2511. */
  2512. if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
  2513. return 0;
  2514. /*
  2515. * If the filesystem has aborted, it is read-only, so return
  2516. * right away instead of dumping stack traces later on that
  2517. * will obscure the real source of the problem. We test
  2518. * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
  2519. * the latter could be true if the filesystem is mounted
  2520. * read-only, and in that case, ext4_da_writepages should
  2521. * *never* be called, so if that ever happens, we would want
  2522. * the stack trace.
  2523. */
  2524. if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
  2525. return -EROFS;
  2526. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  2527. range_whole = 1;
  2528. range_cyclic = wbc->range_cyclic;
  2529. if (wbc->range_cyclic) {
  2530. index = mapping->writeback_index;
  2531. if (index)
  2532. cycled = 0;
  2533. wbc->range_start = index << PAGE_CACHE_SHIFT;
  2534. wbc->range_end = LLONG_MAX;
  2535. wbc->range_cyclic = 0;
  2536. } else
  2537. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  2538. /*
  2539. * This works around two forms of stupidity. The first is in
  2540. * the writeback code, which caps the maximum number of pages
  2541. * written to be 1024 pages. This is wrong on multiple
  2542. * levels; different architectues have a different page size,
  2543. * which changes the maximum amount of data which gets
  2544. * written. Secondly, 4 megabytes is way too small. XFS
  2545. * forces this value to be 16 megabytes by multiplying
  2546. * nr_to_write parameter by four, and then relies on its
  2547. * allocator to allocate larger extents to make them
  2548. * contiguous. Unfortunately this brings us to the second
  2549. * stupidity, which is that ext4's mballoc code only allocates
  2550. * at most 2048 blocks. So we force contiguous writes up to
  2551. * the number of dirty blocks in the inode, or
  2552. * sbi->max_writeback_mb_bump whichever is smaller.
  2553. */
  2554. max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT);
  2555. if (!range_cyclic && range_whole)
  2556. desired_nr_to_write = wbc->nr_to_write * 8;
  2557. else
  2558. desired_nr_to_write = ext4_num_dirty_pages(inode, index,
  2559. max_pages);
  2560. if (desired_nr_to_write > max_pages)
  2561. desired_nr_to_write = max_pages;
  2562. if (wbc->nr_to_write < desired_nr_to_write) {
  2563. nr_to_writebump = desired_nr_to_write - wbc->nr_to_write;
  2564. wbc->nr_to_write = desired_nr_to_write;
  2565. }
  2566. mpd.wbc = wbc;
  2567. mpd.inode = mapping->host;
  2568. /*
  2569. * we don't want write_cache_pages to update
  2570. * nr_to_write and writeback_index
  2571. */
  2572. no_nrwrite_index_update = wbc->no_nrwrite_index_update;
  2573. wbc->no_nrwrite_index_update = 1;
  2574. pages_skipped = wbc->pages_skipped;
  2575. retry:
  2576. while (!ret && wbc->nr_to_write > 0) {
  2577. /*
  2578. * we insert one extent at a time. So we need
  2579. * credit needed for single extent allocation.
  2580. * journalled mode is currently not supported
  2581. * by delalloc
  2582. */
  2583. BUG_ON(ext4_should_journal_data(inode));
  2584. needed_blocks = ext4_da_writepages_trans_blocks(inode);
  2585. /* start a new transaction*/
  2586. handle = ext4_journal_start(inode, needed_blocks);
  2587. if (IS_ERR(handle)) {
  2588. ret = PTR_ERR(handle);
  2589. ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
  2590. "%ld pages, ino %lu; err %d\n", __func__,
  2591. wbc->nr_to_write, inode->i_ino, ret);
  2592. goto out_writepages;
  2593. }
  2594. /*
  2595. * Now call __mpage_da_writepage to find the next
  2596. * contiguous region of logical blocks that need
  2597. * blocks to be allocated by ext4. We don't actually
  2598. * submit the blocks for I/O here, even though
  2599. * write_cache_pages thinks it will, and will set the
  2600. * pages as clean for write before calling
  2601. * __mpage_da_writepage().
  2602. */
  2603. mpd.b_size = 0;
  2604. mpd.b_state = 0;
  2605. mpd.b_blocknr = 0;
  2606. mpd.first_page = 0;
  2607. mpd.next_page = 0;
  2608. mpd.io_done = 0;
  2609. mpd.pages_written = 0;
  2610. mpd.retval = 0;
  2611. ret = write_cache_pages(mapping, wbc, __mpage_da_writepage,
  2612. &mpd);
  2613. /*
  2614. * If we have a contiguous extent of pages and we
  2615. * haven't done the I/O yet, map the blocks and submit
  2616. * them for I/O.
  2617. */
  2618. if (!mpd.io_done && mpd.next_page != mpd.first_page) {
  2619. if (mpage_da_map_blocks(&mpd) == 0)
  2620. mpage_da_submit_io(&mpd);
  2621. mpd.io_done = 1;
  2622. ret = MPAGE_DA_EXTENT_TAIL;
  2623. }
  2624. trace_ext4_da_write_pages(inode, &mpd);
  2625. wbc->nr_to_write -= mpd.pages_written;
  2626. ext4_journal_stop(handle);
  2627. if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
  2628. /* commit the transaction which would
  2629. * free blocks released in the transaction
  2630. * and try again
  2631. */
  2632. jbd2_journal_force_commit_nested(sbi->s_journal);
  2633. wbc->pages_skipped = pages_skipped;
  2634. ret = 0;
  2635. } else if (ret == MPAGE_DA_EXTENT_TAIL) {
  2636. /*
  2637. * got one extent now try with
  2638. * rest of the pages
  2639. */
  2640. pages_written += mpd.pages_written;
  2641. wbc->pages_skipped = pages_skipped;
  2642. ret = 0;
  2643. io_done = 1;
  2644. } else if (wbc->nr_to_write)
  2645. /*
  2646. * There is no more writeout needed
  2647. * or we requested for a noblocking writeout
  2648. * and we found the device congested
  2649. */
  2650. break;
  2651. }
  2652. if (!io_done && !cycled) {
  2653. cycled = 1;
  2654. index = 0;
  2655. wbc->range_start = index << PAGE_CACHE_SHIFT;
  2656. wbc->range_end = mapping->writeback_index - 1;
  2657. goto retry;
  2658. }
  2659. if (pages_skipped != wbc->pages_skipped)
  2660. ext4_msg(inode->i_sb, KERN_CRIT,
  2661. "This should not happen leaving %s "
  2662. "with nr_to_write = %ld ret = %d\n",
  2663. __func__, wbc->nr_to_write, ret);
  2664. /* Update index */
  2665. index += pages_written;
  2666. wbc->range_cyclic = range_cyclic;
  2667. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  2668. /*
  2669. * set the writeback_index so that range_cyclic
  2670. * mode will write it back later
  2671. */
  2672. mapping->writeback_index = index;
  2673. out_writepages:
  2674. if (!no_nrwrite_index_update)
  2675. wbc->no_nrwrite_index_update = 0;
  2676. if (wbc->nr_to_write > nr_to_writebump)
  2677. wbc->nr_to_write -= nr_to_writebump;
  2678. wbc->range_start = range_start;
  2679. trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
  2680. return ret;
  2681. }
  2682. #define FALL_BACK_TO_NONDELALLOC 1
  2683. static int ext4_nonda_switch(struct super_block *sb)
  2684. {
  2685. s64 free_blocks, dirty_blocks;
  2686. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2687. /*
  2688. * switch to non delalloc mode if we are running low
  2689. * on free block. The free block accounting via percpu
  2690. * counters can get slightly wrong with percpu_counter_batch getting
  2691. * accumulated on each CPU without updating global counters
  2692. * Delalloc need an accurate free block accounting. So switch
  2693. * to non delalloc when we are near to error range.
  2694. */
  2695. free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  2696. dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter);
  2697. if (2 * free_blocks < 3 * dirty_blocks ||
  2698. free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
  2699. /*
  2700. * free block count is less that 150% of dirty blocks
  2701. * or free blocks is less that watermark
  2702. */
  2703. return 1;
  2704. }
  2705. return 0;
  2706. }
  2707. static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
  2708. loff_t pos, unsigned len, unsigned flags,
  2709. struct page **pagep, void **fsdata)
  2710. {
  2711. int ret, retries = 0;
  2712. struct page *page;
  2713. pgoff_t index;
  2714. unsigned from, to;
  2715. struct inode *inode = mapping->host;
  2716. handle_t *handle;
  2717. index = pos >> PAGE_CACHE_SHIFT;
  2718. from = pos & (PAGE_CACHE_SIZE - 1);
  2719. to = from + len;
  2720. if (ext4_nonda_switch(inode->i_sb)) {
  2721. *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
  2722. return ext4_write_begin(file, mapping, pos,
  2723. len, flags, pagep, fsdata);
  2724. }
  2725. *fsdata = (void *)0;
  2726. trace_ext4_da_write_begin(inode, pos, len, flags);
  2727. retry:
  2728. /*
  2729. * With delayed allocation, we don't log the i_disksize update
  2730. * if there is delayed block allocation. But we still need
  2731. * to journalling the i_disksize update if writes to the end
  2732. * of file which has an already mapped buffer.
  2733. */
  2734. handle = ext4_journal_start(inode, 1);
  2735. if (IS_ERR(handle)) {
  2736. ret = PTR_ERR(handle);
  2737. goto out;
  2738. }
  2739. /* We cannot recurse into the filesystem as the transaction is already
  2740. * started */
  2741. flags |= AOP_FLAG_NOFS;
  2742. page = grab_cache_page_write_begin(mapping, index, flags);
  2743. if (!page) {
  2744. ext4_journal_stop(handle);
  2745. ret = -ENOMEM;
  2746. goto out;
  2747. }
  2748. *pagep = page;
  2749. ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  2750. ext4_da_get_block_prep);
  2751. if (ret < 0) {
  2752. unlock_page(page);
  2753. ext4_journal_stop(handle);
  2754. page_cache_release(page);
  2755. /*
  2756. * block_write_begin may have instantiated a few blocks
  2757. * outside i_size. Trim these off again. Don't need
  2758. * i_size_read because we hold i_mutex.
  2759. */
  2760. if (pos + len > inode->i_size)
  2761. ext4_truncate_failed_write(inode);
  2762. }
  2763. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  2764. goto retry;
  2765. out:
  2766. return ret;
  2767. }
  2768. /*
  2769. * Check if we should update i_disksize
  2770. * when write to the end of file but not require block allocation
  2771. */
  2772. static int ext4_da_should_update_i_disksize(struct page *page,
  2773. unsigned long offset)
  2774. {
  2775. struct buffer_head *bh;
  2776. struct inode *inode = page->mapping->host;
  2777. unsigned int idx;
  2778. int i;
  2779. bh = page_buffers(page);
  2780. idx = offset >> inode->i_blkbits;
  2781. for (i = 0; i < idx; i++)
  2782. bh = bh->b_this_page;
  2783. if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
  2784. return 0;
  2785. return 1;
  2786. }
  2787. static int ext4_da_write_end(struct file *file,
  2788. struct address_space *mapping,
  2789. loff_t pos, unsigned len, unsigned copied,
  2790. struct page *page, void *fsdata)
  2791. {
  2792. struct inode *inode = mapping->host;
  2793. int ret = 0, ret2;
  2794. handle_t *handle = ext4_journal_current_handle();
  2795. loff_t new_i_size;
  2796. unsigned long start, end;
  2797. int write_mode = (int)(unsigned long)fsdata;
  2798. if (write_mode == FALL_BACK_TO_NONDELALLOC) {
  2799. if (ext4_should_order_data(inode)) {
  2800. return ext4_ordered_write_end(file, mapping, pos,
  2801. len, copied, page, fsdata);
  2802. } else if (ext4_should_writeback_data(inode)) {
  2803. return ext4_writeback_write_end(file, mapping, pos,
  2804. len, copied, page, fsdata);
  2805. } else {
  2806. BUG();
  2807. }
  2808. }
  2809. trace_ext4_da_write_end(inode, pos, len, copied);
  2810. start = pos & (PAGE_CACHE_SIZE - 1);
  2811. end = start + copied - 1;
  2812. /*
  2813. * generic_write_end() will run mark_inode_dirty() if i_size
  2814. * changes. So let's piggyback the i_disksize mark_inode_dirty
  2815. * into that.
  2816. */
  2817. new_i_size = pos + copied;
  2818. if (new_i_size > EXT4_I(inode)->i_disksize) {
  2819. if (ext4_da_should_update_i_disksize(page, end)) {
  2820. down_write(&EXT4_I(inode)->i_data_sem);
  2821. if (new_i_size > EXT4_I(inode)->i_disksize) {
  2822. /*
  2823. * Updating i_disksize when extending file
  2824. * without needing block allocation
  2825. */
  2826. if (ext4_should_order_data(inode))
  2827. ret = ext4_jbd2_file_inode(handle,
  2828. inode);
  2829. EXT4_I(inode)->i_disksize = new_i_size;
  2830. }
  2831. up_write(&EXT4_I(inode)->i_data_sem);
  2832. /* We need to mark inode dirty even if
  2833. * new_i_size is less that inode->i_size
  2834. * bu greater than i_disksize.(hint delalloc)
  2835. */
  2836. ext4_mark_inode_dirty(handle, inode);
  2837. }
  2838. }
  2839. ret2 = generic_write_end(file, mapping, pos, len, copied,
  2840. page, fsdata);
  2841. copied = ret2;
  2842. if (ret2 < 0)
  2843. ret = ret2;
  2844. ret2 = ext4_journal_stop(handle);
  2845. if (!ret)
  2846. ret = ret2;
  2847. return ret ? ret : copied;
  2848. }
  2849. static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
  2850. {
  2851. /*
  2852. * Drop reserved blocks
  2853. */
  2854. BUG_ON(!PageLocked(page));
  2855. if (!page_has_buffers(page))
  2856. goto out;
  2857. ext4_da_page_release_reservation(page, offset);
  2858. out:
  2859. ext4_invalidatepage(page, offset);
  2860. return;
  2861. }
  2862. /*
  2863. * Force all delayed allocation blocks to be allocated for a given inode.
  2864. */
  2865. int ext4_alloc_da_blocks(struct inode *inode)
  2866. {
  2867. trace_ext4_alloc_da_blocks(inode);
  2868. if (!EXT4_I(inode)->i_reserved_data_blocks &&
  2869. !EXT4_I(inode)->i_reserved_meta_blocks)
  2870. return 0;
  2871. /*
  2872. * We do something simple for now. The filemap_flush() will
  2873. * also start triggering a write of the data blocks, which is
  2874. * not strictly speaking necessary (and for users of
  2875. * laptop_mode, not even desirable). However, to do otherwise
  2876. * would require replicating code paths in:
  2877. *
  2878. * ext4_da_writepages() ->
  2879. * write_cache_pages() ---> (via passed in callback function)
  2880. * __mpage_da_writepage() -->
  2881. * mpage_add_bh_to_extent()
  2882. * mpage_da_map_blocks()
  2883. *
  2884. * The problem is that write_cache_pages(), located in
  2885. * mm/page-writeback.c, marks pages clean in preparation for
  2886. * doing I/O, which is not desirable if we're not planning on
  2887. * doing I/O at all.
  2888. *
  2889. * We could call write_cache_pages(), and then redirty all of
  2890. * the pages by calling redirty_page_for_writeback() but that
  2891. * would be ugly in the extreme. So instead we would need to
  2892. * replicate parts of the code in the above functions,
  2893. * simplifying them becuase we wouldn't actually intend to
  2894. * write out the pages, but rather only collect contiguous
  2895. * logical block extents, call the multi-block allocator, and
  2896. * then update the buffer heads with the block allocations.
  2897. *
  2898. * For now, though, we'll cheat by calling filemap_flush(),
  2899. * which will map the blocks, and start the I/O, but not
  2900. * actually wait for the I/O to complete.
  2901. */
  2902. return filemap_flush(inode->i_mapping);
  2903. }
  2904. /*
  2905. * bmap() is special. It gets used by applications such as lilo and by
  2906. * the swapper to find the on-disk block of a specific piece of data.
  2907. *
  2908. * Naturally, this is dangerous if the block concerned is still in the
  2909. * journal. If somebody makes a swapfile on an ext4 data-journaling
  2910. * filesystem and enables swap, then they may get a nasty shock when the
  2911. * data getting swapped to that swapfile suddenly gets overwritten by
  2912. * the original zero's written out previously to the journal and
  2913. * awaiting writeback in the kernel's buffer cache.
  2914. *
  2915. * So, if we see any bmap calls here on a modified, data-journaled file,
  2916. * take extra steps to flush any blocks which might be in the cache.
  2917. */
  2918. static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
  2919. {
  2920. struct inode *inode = mapping->host;
  2921. journal_t *journal;
  2922. int err;
  2923. if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
  2924. test_opt(inode->i_sb, DELALLOC)) {
  2925. /*
  2926. * With delalloc we want to sync the file
  2927. * so that we can make sure we allocate
  2928. * blocks for file
  2929. */
  2930. filemap_write_and_wait(mapping);
  2931. }
  2932. if (EXT4_JOURNAL(inode) && EXT4_I(inode)->i_state & EXT4_STATE_JDATA) {
  2933. /*
  2934. * This is a REALLY heavyweight approach, but the use of
  2935. * bmap on dirty files is expected to be extremely rare:
  2936. * only if we run lilo or swapon on a freshly made file
  2937. * do we expect this to happen.
  2938. *
  2939. * (bmap requires CAP_SYS_RAWIO so this does not
  2940. * represent an unprivileged user DOS attack --- we'd be
  2941. * in trouble if mortal users could trigger this path at
  2942. * will.)
  2943. *
  2944. * NB. EXT4_STATE_JDATA is not set on files other than
  2945. * regular files. If somebody wants to bmap a directory
  2946. * or symlink and gets confused because the buffer
  2947. * hasn't yet been flushed to disk, they deserve
  2948. * everything they get.
  2949. */
  2950. EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA;
  2951. journal = EXT4_JOURNAL(inode);
  2952. jbd2_journal_lock_updates(journal);
  2953. err = jbd2_journal_flush(journal);
  2954. jbd2_journal_unlock_updates(journal);
  2955. if (err)
  2956. return 0;
  2957. }
  2958. return generic_block_bmap(mapping, block, ext4_get_block);
  2959. }
  2960. static int ext4_readpage(struct file *file, struct page *page)
  2961. {
  2962. return mpage_readpage(page, ext4_get_block);
  2963. }
  2964. static int
  2965. ext4_readpages(struct file *file, struct address_space *mapping,
  2966. struct list_head *pages, unsigned nr_pages)
  2967. {
  2968. return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
  2969. }
  2970. static void ext4_invalidatepage(struct page *page, unsigned long offset)
  2971. {
  2972. journal_t *journal = EXT4_JOURNAL(page->mapping->host);
  2973. /*
  2974. * If it's a full truncate we just forget about the pending dirtying
  2975. */
  2976. if (offset == 0)
  2977. ClearPageChecked(page);
  2978. if (journal)
  2979. jbd2_journal_invalidatepage(journal, page, offset);
  2980. else
  2981. block_invalidatepage(page, offset);
  2982. }
  2983. static int ext4_releasepage(struct page *page, gfp_t wait)
  2984. {
  2985. journal_t *journal = EXT4_JOURNAL(page->mapping->host);
  2986. WARN_ON(PageChecked(page));
  2987. if (!page_has_buffers(page))
  2988. return 0;
  2989. if (journal)
  2990. return jbd2_journal_try_to_free_buffers(journal, page, wait);
  2991. else
  2992. return try_to_free_buffers(page);
  2993. }
  2994. /*
  2995. * O_DIRECT for ext3 (or indirect map) based files
  2996. *
  2997. * If the O_DIRECT write will extend the file then add this inode to the
  2998. * orphan list. So recovery will truncate it back to the original size
  2999. * if the machine crashes during the write.
  3000. *
  3001. * If the O_DIRECT write is intantiating holes inside i_size and the machine
  3002. * crashes then stale disk data _may_ be exposed inside the file. But current
  3003. * VFS code falls back into buffered path in that case so we are safe.
  3004. */
  3005. static ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb,
  3006. const struct iovec *iov, loff_t offset,
  3007. unsigned long nr_segs)
  3008. {
  3009. struct file *file = iocb->ki_filp;
  3010. struct inode *inode = file->f_mapping->host;
  3011. struct ext4_inode_info *ei = EXT4_I(inode);
  3012. handle_t *handle;
  3013. ssize_t ret;
  3014. int orphan = 0;
  3015. size_t count = iov_length(iov, nr_segs);
  3016. int retries = 0;
  3017. if (rw == WRITE) {
  3018. loff_t final_size = offset + count;
  3019. if (final_size > inode->i_size) {
  3020. /* Credits for sb + inode write */
  3021. handle = ext4_journal_start(inode, 2);
  3022. if (IS_ERR(handle)) {
  3023. ret = PTR_ERR(handle);
  3024. goto out;
  3025. }
  3026. ret = ext4_orphan_add(handle, inode);
  3027. if (ret) {
  3028. ext4_journal_stop(handle);
  3029. goto out;
  3030. }
  3031. orphan = 1;
  3032. ei->i_disksize = inode->i_size;
  3033. ext4_journal_stop(handle);
  3034. }
  3035. }
  3036. retry:
  3037. ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
  3038. offset, nr_segs,
  3039. ext4_get_block, NULL);
  3040. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  3041. goto retry;
  3042. if (orphan) {
  3043. int err;
  3044. /* Credits for sb + inode write */
  3045. handle = ext4_journal_start(inode, 2);
  3046. if (IS_ERR(handle)) {
  3047. /* This is really bad luck. We've written the data
  3048. * but cannot extend i_size. Bail out and pretend
  3049. * the write failed... */
  3050. ret = PTR_ERR(handle);
  3051. goto out;
  3052. }
  3053. if (inode->i_nlink)
  3054. ext4_orphan_del(handle, inode);
  3055. if (ret > 0) {
  3056. loff_t end = offset + ret;
  3057. if (end > inode->i_size) {
  3058. ei->i_disksize = end;
  3059. i_size_write(inode, end);
  3060. /*
  3061. * We're going to return a positive `ret'
  3062. * here due to non-zero-length I/O, so there's
  3063. * no way of reporting error returns from
  3064. * ext4_mark_inode_dirty() to userspace. So
  3065. * ignore it.
  3066. */
  3067. ext4_mark_inode_dirty(handle, inode);
  3068. }
  3069. }
  3070. err = ext4_journal_stop(handle);
  3071. if (ret == 0)
  3072. ret = err;
  3073. }
  3074. out:
  3075. return ret;
  3076. }
  3077. static int ext4_get_block_dio_write(struct inode *inode, sector_t iblock,
  3078. struct buffer_head *bh_result, int create)
  3079. {
  3080. handle_t *handle = NULL;
  3081. int ret = 0;
  3082. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  3083. int dio_credits;
  3084. ext4_debug("ext4_get_block_dio_write: inode %lu, create flag %d\n",
  3085. inode->i_ino, create);
  3086. /*
  3087. * DIO VFS code passes create = 0 flag for write to
  3088. * the middle of file. It does this to avoid block
  3089. * allocation for holes, to prevent expose stale data
  3090. * out when there is parallel buffered read (which does
  3091. * not hold the i_mutex lock) while direct IO write has
  3092. * not completed. DIO request on holes finally falls back
  3093. * to buffered IO for this reason.
  3094. *
  3095. * For ext4 extent based file, since we support fallocate,
  3096. * new allocated extent as uninitialized, for holes, we
  3097. * could fallocate blocks for holes, thus parallel
  3098. * buffered IO read will zero out the page when read on
  3099. * a hole while parallel DIO write to the hole has not completed.
  3100. *
  3101. * when we come here, we know it's a direct IO write to
  3102. * to the middle of file (<i_size)
  3103. * so it's safe to override the create flag from VFS.
  3104. */
  3105. create = EXT4_GET_BLOCKS_DIO_CREATE_EXT;
  3106. if (max_blocks > DIO_MAX_BLOCKS)
  3107. max_blocks = DIO_MAX_BLOCKS;
  3108. dio_credits = ext4_chunk_trans_blocks(inode, max_blocks);
  3109. handle = ext4_journal_start(inode, dio_credits);
  3110. if (IS_ERR(handle)) {
  3111. ret = PTR_ERR(handle);
  3112. goto out;
  3113. }
  3114. ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result,
  3115. create);
  3116. if (ret > 0) {
  3117. bh_result->b_size = (ret << inode->i_blkbits);
  3118. ret = 0;
  3119. }
  3120. ext4_journal_stop(handle);
  3121. out:
  3122. return ret;
  3123. }
  3124. static void ext4_free_io_end(ext4_io_end_t *io)
  3125. {
  3126. BUG_ON(!io);
  3127. iput(io->inode);
  3128. kfree(io);
  3129. }
  3130. static void dump_aio_dio_list(struct inode * inode)
  3131. {
  3132. #ifdef EXT4_DEBUG
  3133. struct list_head *cur, *before, *after;
  3134. ext4_io_end_t *io, *io0, *io1;
  3135. if (list_empty(&EXT4_I(inode)->i_aio_dio_complete_list)){
  3136. ext4_debug("inode %lu aio dio list is empty\n", inode->i_ino);
  3137. return;
  3138. }
  3139. ext4_debug("Dump inode %lu aio_dio_completed_IO list \n", inode->i_ino);
  3140. list_for_each_entry(io, &EXT4_I(inode)->i_aio_dio_complete_list, list){
  3141. cur = &io->list;
  3142. before = cur->prev;
  3143. io0 = container_of(before, ext4_io_end_t, list);
  3144. after = cur->next;
  3145. io1 = container_of(after, ext4_io_end_t, list);
  3146. ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
  3147. io, inode->i_ino, io0, io1);
  3148. }
  3149. #endif
  3150. }
  3151. /*
  3152. * check a range of space and convert unwritten extents to written.
  3153. */
  3154. static int ext4_end_aio_dio_nolock(ext4_io_end_t *io)
  3155. {
  3156. struct inode *inode = io->inode;
  3157. loff_t offset = io->offset;
  3158. size_t size = io->size;
  3159. int ret = 0;
  3160. ext4_debug("end_aio_dio_onlock: io 0x%p from inode %lu,list->next 0x%p,"
  3161. "list->prev 0x%p\n",
  3162. io, inode->i_ino, io->list.next, io->list.prev);
  3163. if (list_empty(&io->list))
  3164. return ret;
  3165. if (io->flag != DIO_AIO_UNWRITTEN)
  3166. return ret;
  3167. if (offset + size <= i_size_read(inode))
  3168. ret = ext4_convert_unwritten_extents(inode, offset, size);
  3169. if (ret < 0) {
  3170. printk(KERN_EMERG "%s: failed to convert unwritten"
  3171. "extents to written extents, error is %d"
  3172. " io is still on inode %lu aio dio list\n",
  3173. __func__, ret, inode->i_ino);
  3174. return ret;
  3175. }
  3176. /* clear the DIO AIO unwritten flag */
  3177. io->flag = 0;
  3178. return ret;
  3179. }
  3180. /*
  3181. * work on completed aio dio IO, to convert unwritten extents to extents
  3182. */
  3183. static void ext4_end_aio_dio_work(struct work_struct *work)
  3184. {
  3185. ext4_io_end_t *io = container_of(work, ext4_io_end_t, work);
  3186. struct inode *inode = io->inode;
  3187. int ret = 0;
  3188. mutex_lock(&inode->i_mutex);
  3189. ret = ext4_end_aio_dio_nolock(io);
  3190. if (ret >= 0) {
  3191. if (!list_empty(&io->list))
  3192. list_del_init(&io->list);
  3193. ext4_free_io_end(io);
  3194. }
  3195. mutex_unlock(&inode->i_mutex);
  3196. }
  3197. /*
  3198. * This function is called from ext4_sync_file().
  3199. *
  3200. * When AIO DIO IO is completed, the work to convert unwritten
  3201. * extents to written is queued on workqueue but may not get immediately
  3202. * scheduled. When fsync is called, we need to ensure the
  3203. * conversion is complete before fsync returns.
  3204. * The inode keeps track of a list of completed AIO from DIO path
  3205. * that might needs to do the conversion. This function walks through
  3206. * the list and convert the related unwritten extents to written.
  3207. */
  3208. int flush_aio_dio_completed_IO(struct inode *inode)
  3209. {
  3210. ext4_io_end_t *io;
  3211. int ret = 0;
  3212. int ret2 = 0;
  3213. if (list_empty(&EXT4_I(inode)->i_aio_dio_complete_list))
  3214. return ret;
  3215. dump_aio_dio_list(inode);
  3216. while (!list_empty(&EXT4_I(inode)->i_aio_dio_complete_list)){
  3217. io = list_entry(EXT4_I(inode)->i_aio_dio_complete_list.next,
  3218. ext4_io_end_t, list);
  3219. /*
  3220. * Calling ext4_end_aio_dio_nolock() to convert completed
  3221. * IO to written.
  3222. *
  3223. * When ext4_sync_file() is called, run_queue() may already
  3224. * about to flush the work corresponding to this io structure.
  3225. * It will be upset if it founds the io structure related
  3226. * to the work-to-be schedule is freed.
  3227. *
  3228. * Thus we need to keep the io structure still valid here after
  3229. * convertion finished. The io structure has a flag to
  3230. * avoid double converting from both fsync and background work
  3231. * queue work.
  3232. */
  3233. ret = ext4_end_aio_dio_nolock(io);
  3234. if (ret < 0)
  3235. ret2 = ret;
  3236. else
  3237. list_del_init(&io->list);
  3238. }
  3239. return (ret2 < 0) ? ret2 : 0;
  3240. }
  3241. static ext4_io_end_t *ext4_init_io_end (struct inode *inode)
  3242. {
  3243. ext4_io_end_t *io = NULL;
  3244. io = kmalloc(sizeof(*io), GFP_NOFS);
  3245. if (io) {
  3246. igrab(inode);
  3247. io->inode = inode;
  3248. io->flag = 0;
  3249. io->offset = 0;
  3250. io->size = 0;
  3251. io->error = 0;
  3252. INIT_WORK(&io->work, ext4_end_aio_dio_work);
  3253. INIT_LIST_HEAD(&io->list);
  3254. }
  3255. return io;
  3256. }
  3257. static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
  3258. ssize_t size, void *private)
  3259. {
  3260. ext4_io_end_t *io_end = iocb->private;
  3261. struct workqueue_struct *wq;
  3262. /* if not async direct IO or dio with 0 bytes write, just return */
  3263. if (!io_end || !size)
  3264. return;
  3265. ext_debug("ext4_end_io_dio(): io_end 0x%p"
  3266. "for inode %lu, iocb 0x%p, offset %llu, size %llu\n",
  3267. iocb->private, io_end->inode->i_ino, iocb, offset,
  3268. size);
  3269. /* if not aio dio with unwritten extents, just free io and return */
  3270. if (io_end->flag != DIO_AIO_UNWRITTEN){
  3271. ext4_free_io_end(io_end);
  3272. iocb->private = NULL;
  3273. return;
  3274. }
  3275. io_end->offset = offset;
  3276. io_end->size = size;
  3277. wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
  3278. /* queue the work to convert unwritten extents to written */
  3279. queue_work(wq, &io_end->work);
  3280. /* Add the io_end to per-inode completed aio dio list*/
  3281. list_add_tail(&io_end->list,
  3282. &EXT4_I(io_end->inode)->i_aio_dio_complete_list);
  3283. iocb->private = NULL;
  3284. }
  3285. /*
  3286. * For ext4 extent files, ext4 will do direct-io write to holes,
  3287. * preallocated extents, and those write extend the file, no need to
  3288. * fall back to buffered IO.
  3289. *
  3290. * For holes, we fallocate those blocks, mark them as unintialized
  3291. * If those blocks were preallocated, we mark sure they are splited, but
  3292. * still keep the range to write as unintialized.
  3293. *
  3294. * The unwrritten extents will be converted to written when DIO is completed.
  3295. * For async direct IO, since the IO may still pending when return, we
  3296. * set up an end_io call back function, which will do the convertion
  3297. * when async direct IO completed.
  3298. *
  3299. * If the O_DIRECT write will extend the file then add this inode to the
  3300. * orphan list. So recovery will truncate it back to the original size
  3301. * if the machine crashes during the write.
  3302. *
  3303. */
  3304. static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
  3305. const struct iovec *iov, loff_t offset,
  3306. unsigned long nr_segs)
  3307. {
  3308. struct file *file = iocb->ki_filp;
  3309. struct inode *inode = file->f_mapping->host;
  3310. ssize_t ret;
  3311. size_t count = iov_length(iov, nr_segs);
  3312. loff_t final_size = offset + count;
  3313. if (rw == WRITE && final_size <= inode->i_size) {
  3314. /*
  3315. * We could direct write to holes and fallocate.
  3316. *
  3317. * Allocated blocks to fill the hole are marked as uninitialized
  3318. * to prevent paralel buffered read to expose the stale data
  3319. * before DIO complete the data IO.
  3320. *
  3321. * As to previously fallocated extents, ext4 get_block
  3322. * will just simply mark the buffer mapped but still
  3323. * keep the extents uninitialized.
  3324. *
  3325. * for non AIO case, we will convert those unwritten extents
  3326. * to written after return back from blockdev_direct_IO.
  3327. *
  3328. * for async DIO, the conversion needs to be defered when
  3329. * the IO is completed. The ext4 end_io callback function
  3330. * will be called to take care of the conversion work.
  3331. * Here for async case, we allocate an io_end structure to
  3332. * hook to the iocb.
  3333. */
  3334. iocb->private = NULL;
  3335. EXT4_I(inode)->cur_aio_dio = NULL;
  3336. if (!is_sync_kiocb(iocb)) {
  3337. iocb->private = ext4_init_io_end(inode);
  3338. if (!iocb->private)
  3339. return -ENOMEM;
  3340. /*
  3341. * we save the io structure for current async
  3342. * direct IO, so that later ext4_get_blocks()
  3343. * could flag the io structure whether there
  3344. * is a unwritten extents needs to be converted
  3345. * when IO is completed.
  3346. */
  3347. EXT4_I(inode)->cur_aio_dio = iocb->private;
  3348. }
  3349. ret = blockdev_direct_IO(rw, iocb, inode,
  3350. inode->i_sb->s_bdev, iov,
  3351. offset, nr_segs,
  3352. ext4_get_block_dio_write,
  3353. ext4_end_io_dio);
  3354. if (iocb->private)
  3355. EXT4_I(inode)->cur_aio_dio = NULL;
  3356. /*
  3357. * The io_end structure takes a reference to the inode,
  3358. * that structure needs to be destroyed and the
  3359. * reference to the inode need to be dropped, when IO is
  3360. * complete, even with 0 byte write, or failed.
  3361. *
  3362. * In the successful AIO DIO case, the io_end structure will be
  3363. * desctroyed and the reference to the inode will be dropped
  3364. * after the end_io call back function is called.
  3365. *
  3366. * In the case there is 0 byte write, or error case, since
  3367. * VFS direct IO won't invoke the end_io call back function,
  3368. * we need to free the end_io structure here.
  3369. */
  3370. if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) {
  3371. ext4_free_io_end(iocb->private);
  3372. iocb->private = NULL;
  3373. } else if (ret > 0 && (EXT4_I(inode)->i_state &
  3374. EXT4_STATE_DIO_UNWRITTEN)) {
  3375. int err;
  3376. /*
  3377. * for non AIO case, since the IO is already
  3378. * completed, we could do the convertion right here
  3379. */
  3380. err = ext4_convert_unwritten_extents(inode,
  3381. offset, ret);
  3382. if (err < 0)
  3383. ret = err;
  3384. EXT4_I(inode)->i_state &= ~EXT4_STATE_DIO_UNWRITTEN;
  3385. }
  3386. return ret;
  3387. }
  3388. /* for write the the end of file case, we fall back to old way */
  3389. return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
  3390. }
  3391. static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
  3392. const struct iovec *iov, loff_t offset,
  3393. unsigned long nr_segs)
  3394. {
  3395. struct file *file = iocb->ki_filp;
  3396. struct inode *inode = file->f_mapping->host;
  3397. if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)
  3398. return ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs);
  3399. return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
  3400. }
  3401. /*
  3402. * Pages can be marked dirty completely asynchronously from ext4's journalling
  3403. * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
  3404. * much here because ->set_page_dirty is called under VFS locks. The page is
  3405. * not necessarily locked.
  3406. *
  3407. * We cannot just dirty the page and leave attached buffers clean, because the
  3408. * buffers' dirty state is "definitive". We cannot just set the buffers dirty
  3409. * or jbddirty because all the journalling code will explode.
  3410. *
  3411. * So what we do is to mark the page "pending dirty" and next time writepage
  3412. * is called, propagate that into the buffers appropriately.
  3413. */
  3414. static int ext4_journalled_set_page_dirty(struct page *page)
  3415. {
  3416. SetPageChecked(page);
  3417. return __set_page_dirty_nobuffers(page);
  3418. }
  3419. static const struct address_space_operations ext4_ordered_aops = {
  3420. .readpage = ext4_readpage,
  3421. .readpages = ext4_readpages,
  3422. .writepage = ext4_writepage,
  3423. .sync_page = block_sync_page,
  3424. .write_begin = ext4_write_begin,
  3425. .write_end = ext4_ordered_write_end,
  3426. .bmap = ext4_bmap,
  3427. .invalidatepage = ext4_invalidatepage,
  3428. .releasepage = ext4_releasepage,
  3429. .direct_IO = ext4_direct_IO,
  3430. .migratepage = buffer_migrate_page,
  3431. .is_partially_uptodate = block_is_partially_uptodate,
  3432. .error_remove_page = generic_error_remove_page,
  3433. };
  3434. static const struct address_space_operations ext4_writeback_aops = {
  3435. .readpage = ext4_readpage,
  3436. .readpages = ext4_readpages,
  3437. .writepage = ext4_writepage,
  3438. .sync_page = block_sync_page,
  3439. .write_begin = ext4_write_begin,
  3440. .write_end = ext4_writeback_write_end,
  3441. .bmap = ext4_bmap,
  3442. .invalidatepage = ext4_invalidatepage,
  3443. .releasepage = ext4_releasepage,
  3444. .direct_IO = ext4_direct_IO,
  3445. .migratepage = buffer_migrate_page,
  3446. .is_partially_uptodate = block_is_partially_uptodate,
  3447. .error_remove_page = generic_error_remove_page,
  3448. };
  3449. static const struct address_space_operations ext4_journalled_aops = {
  3450. .readpage = ext4_readpage,
  3451. .readpages = ext4_readpages,
  3452. .writepage = ext4_writepage,
  3453. .sync_page = block_sync_page,
  3454. .write_begin = ext4_write_begin,
  3455. .write_end = ext4_journalled_write_end,
  3456. .set_page_dirty = ext4_journalled_set_page_dirty,
  3457. .bmap = ext4_bmap,
  3458. .invalidatepage = ext4_invalidatepage,
  3459. .releasepage = ext4_releasepage,
  3460. .is_partially_uptodate = block_is_partially_uptodate,
  3461. .error_remove_page = generic_error_remove_page,
  3462. };
  3463. static const struct address_space_operations ext4_da_aops = {
  3464. .readpage = ext4_readpage,
  3465. .readpages = ext4_readpages,
  3466. .writepage = ext4_writepage,
  3467. .writepages = ext4_da_writepages,
  3468. .sync_page = block_sync_page,
  3469. .write_begin = ext4_da_write_begin,
  3470. .write_end = ext4_da_write_end,
  3471. .bmap = ext4_bmap,
  3472. .invalidatepage = ext4_da_invalidatepage,
  3473. .releasepage = ext4_releasepage,
  3474. .direct_IO = ext4_direct_IO,
  3475. .migratepage = buffer_migrate_page,
  3476. .is_partially_uptodate = block_is_partially_uptodate,
  3477. .error_remove_page = generic_error_remove_page,
  3478. };
  3479. void ext4_set_aops(struct inode *inode)
  3480. {
  3481. if (ext4_should_order_data(inode) &&
  3482. test_opt(inode->i_sb, DELALLOC))
  3483. inode->i_mapping->a_ops = &ext4_da_aops;
  3484. else if (ext4_should_order_data(inode))
  3485. inode->i_mapping->a_ops = &ext4_ordered_aops;
  3486. else if (ext4_should_writeback_data(inode) &&
  3487. test_opt(inode->i_sb, DELALLOC))
  3488. inode->i_mapping->a_ops = &ext4_da_aops;
  3489. else if (ext4_should_writeback_data(inode))
  3490. inode->i_mapping->a_ops = &ext4_writeback_aops;
  3491. else
  3492. inode->i_mapping->a_ops = &ext4_journalled_aops;
  3493. }
  3494. /*
  3495. * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
  3496. * up to the end of the block which corresponds to `from'.
  3497. * This required during truncate. We need to physically zero the tail end
  3498. * of that block so it doesn't yield old data if the file is later grown.
  3499. */
  3500. int ext4_block_truncate_page(handle_t *handle,
  3501. struct address_space *mapping, loff_t from)
  3502. {
  3503. ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
  3504. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  3505. unsigned blocksize, length, pos;
  3506. ext4_lblk_t iblock;
  3507. struct inode *inode = mapping->host;
  3508. struct buffer_head *bh;
  3509. struct page *page;
  3510. int err = 0;
  3511. page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
  3512. mapping_gfp_mask(mapping) & ~__GFP_FS);
  3513. if (!page)
  3514. return -EINVAL;
  3515. blocksize = inode->i_sb->s_blocksize;
  3516. length = blocksize - (offset & (blocksize - 1));
  3517. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  3518. /*
  3519. * For "nobh" option, we can only work if we don't need to
  3520. * read-in the page - otherwise we create buffers to do the IO.
  3521. */
  3522. if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
  3523. ext4_should_writeback_data(inode) && PageUptodate(page)) {
  3524. zero_user(page, offset, length);
  3525. set_page_dirty(page);
  3526. goto unlock;
  3527. }
  3528. if (!page_has_buffers(page))
  3529. create_empty_buffers(page, blocksize, 0);
  3530. /* Find the buffer that contains "offset" */
  3531. bh = page_buffers(page);
  3532. pos = blocksize;
  3533. while (offset >= pos) {
  3534. bh = bh->b_this_page;
  3535. iblock++;
  3536. pos += blocksize;
  3537. }
  3538. err = 0;
  3539. if (buffer_freed(bh)) {
  3540. BUFFER_TRACE(bh, "freed: skip");
  3541. goto unlock;
  3542. }
  3543. if (!buffer_mapped(bh)) {
  3544. BUFFER_TRACE(bh, "unmapped");
  3545. ext4_get_block(inode, iblock, bh, 0);
  3546. /* unmapped? It's a hole - nothing to do */
  3547. if (!buffer_mapped(bh)) {
  3548. BUFFER_TRACE(bh, "still unmapped");
  3549. goto unlock;
  3550. }
  3551. }
  3552. /* Ok, it's mapped. Make sure it's up-to-date */
  3553. if (PageUptodate(page))
  3554. set_buffer_uptodate(bh);
  3555. if (!buffer_uptodate(bh)) {
  3556. err = -EIO;
  3557. ll_rw_block(READ, 1, &bh);
  3558. wait_on_buffer(bh);
  3559. /* Uhhuh. Read error. Complain and punt. */
  3560. if (!buffer_uptodate(bh))
  3561. goto unlock;
  3562. }
  3563. if (ext4_should_journal_data(inode)) {
  3564. BUFFER_TRACE(bh, "get write access");
  3565. err = ext4_journal_get_write_access(handle, bh);
  3566. if (err)
  3567. goto unlock;
  3568. }
  3569. zero_user(page, offset, length);
  3570. BUFFER_TRACE(bh, "zeroed end of block");
  3571. err = 0;
  3572. if (ext4_should_journal_data(inode)) {
  3573. err = ext4_handle_dirty_metadata(handle, inode, bh);
  3574. } else {
  3575. if (ext4_should_order_data(inode))
  3576. err = ext4_jbd2_file_inode(handle, inode);
  3577. mark_buffer_dirty(bh);
  3578. }
  3579. unlock:
  3580. unlock_page(page);
  3581. page_cache_release(page);
  3582. return err;
  3583. }
  3584. /*
  3585. * Probably it should be a library function... search for first non-zero word
  3586. * or memcmp with zero_page, whatever is better for particular architecture.
  3587. * Linus?
  3588. */
  3589. static inline int all_zeroes(__le32 *p, __le32 *q)
  3590. {
  3591. while (p < q)
  3592. if (*p++)
  3593. return 0;
  3594. return 1;
  3595. }
  3596. /**
  3597. * ext4_find_shared - find the indirect blocks for partial truncation.
  3598. * @inode: inode in question
  3599. * @depth: depth of the affected branch
  3600. * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
  3601. * @chain: place to store the pointers to partial indirect blocks
  3602. * @top: place to the (detached) top of branch
  3603. *
  3604. * This is a helper function used by ext4_truncate().
  3605. *
  3606. * When we do truncate() we may have to clean the ends of several
  3607. * indirect blocks but leave the blocks themselves alive. Block is
  3608. * partially truncated if some data below the new i_size is refered
  3609. * from it (and it is on the path to the first completely truncated
  3610. * data block, indeed). We have to free the top of that path along
  3611. * with everything to the right of the path. Since no allocation
  3612. * past the truncation point is possible until ext4_truncate()
  3613. * finishes, we may safely do the latter, but top of branch may
  3614. * require special attention - pageout below the truncation point
  3615. * might try to populate it.
  3616. *
  3617. * We atomically detach the top of branch from the tree, store the
  3618. * block number of its root in *@top, pointers to buffer_heads of
  3619. * partially truncated blocks - in @chain[].bh and pointers to
  3620. * their last elements that should not be removed - in
  3621. * @chain[].p. Return value is the pointer to last filled element
  3622. * of @chain.
  3623. *
  3624. * The work left to caller to do the actual freeing of subtrees:
  3625. * a) free the subtree starting from *@top
  3626. * b) free the subtrees whose roots are stored in
  3627. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  3628. * c) free the subtrees growing from the inode past the @chain[0].
  3629. * (no partially truncated stuff there). */
  3630. static Indirect *ext4_find_shared(struct inode *inode, int depth,
  3631. ext4_lblk_t offsets[4], Indirect chain[4],
  3632. __le32 *top)
  3633. {
  3634. Indirect *partial, *p;
  3635. int k, err;
  3636. *top = 0;
  3637. /* Make k index the deepest non-null offset + 1 */
  3638. for (k = depth; k > 1 && !offsets[k-1]; k--)
  3639. ;
  3640. partial = ext4_get_branch(inode, k, offsets, chain, &err);
  3641. /* Writer: pointers */
  3642. if (!partial)
  3643. partial = chain + k-1;
  3644. /*
  3645. * If the branch acquired continuation since we've looked at it -
  3646. * fine, it should all survive and (new) top doesn't belong to us.
  3647. */
  3648. if (!partial->key && *partial->p)
  3649. /* Writer: end */
  3650. goto no_top;
  3651. for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
  3652. ;
  3653. /*
  3654. * OK, we've found the last block that must survive. The rest of our
  3655. * branch should be detached before unlocking. However, if that rest
  3656. * of branch is all ours and does not grow immediately from the inode
  3657. * it's easier to cheat and just decrement partial->p.
  3658. */
  3659. if (p == chain + k - 1 && p > chain) {
  3660. p->p--;
  3661. } else {
  3662. *top = *p->p;
  3663. /* Nope, don't do this in ext4. Must leave the tree intact */
  3664. #if 0
  3665. *p->p = 0;
  3666. #endif
  3667. }
  3668. /* Writer: end */
  3669. while (partial > p) {
  3670. brelse(partial->bh);
  3671. partial--;
  3672. }
  3673. no_top:
  3674. return partial;
  3675. }
  3676. /*
  3677. * Zero a number of block pointers in either an inode or an indirect block.
  3678. * If we restart the transaction we must again get write access to the
  3679. * indirect block for further modification.
  3680. *
  3681. * We release `count' blocks on disk, but (last - first) may be greater
  3682. * than `count' because there can be holes in there.
  3683. */
  3684. static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
  3685. struct buffer_head *bh,
  3686. ext4_fsblk_t block_to_free,
  3687. unsigned long count, __le32 *first,
  3688. __le32 *last)
  3689. {
  3690. __le32 *p;
  3691. int flags = EXT4_FREE_BLOCKS_FORGET;
  3692. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  3693. flags |= EXT4_FREE_BLOCKS_METADATA;
  3694. if (try_to_extend_transaction(handle, inode)) {
  3695. if (bh) {
  3696. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  3697. ext4_handle_dirty_metadata(handle, inode, bh);
  3698. }
  3699. ext4_mark_inode_dirty(handle, inode);
  3700. ext4_truncate_restart_trans(handle, inode,
  3701. blocks_for_truncate(inode));
  3702. if (bh) {
  3703. BUFFER_TRACE(bh, "retaking write access");
  3704. ext4_journal_get_write_access(handle, bh);
  3705. }
  3706. }
  3707. for (p = first; p < last; p++)
  3708. *p = 0;
  3709. ext4_free_blocks(handle, inode, 0, block_to_free, count, flags);
  3710. }
  3711. /**
  3712. * ext4_free_data - free a list of data blocks
  3713. * @handle: handle for this transaction
  3714. * @inode: inode we are dealing with
  3715. * @this_bh: indirect buffer_head which contains *@first and *@last
  3716. * @first: array of block numbers
  3717. * @last: points immediately past the end of array
  3718. *
  3719. * We are freeing all blocks refered from that array (numbers are stored as
  3720. * little-endian 32-bit) and updating @inode->i_blocks appropriately.
  3721. *
  3722. * We accumulate contiguous runs of blocks to free. Conveniently, if these
  3723. * blocks are contiguous then releasing them at one time will only affect one
  3724. * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
  3725. * actually use a lot of journal space.
  3726. *
  3727. * @this_bh will be %NULL if @first and @last point into the inode's direct
  3728. * block pointers.
  3729. */
  3730. static void ext4_free_data(handle_t *handle, struct inode *inode,
  3731. struct buffer_head *this_bh,
  3732. __le32 *first, __le32 *last)
  3733. {
  3734. ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
  3735. unsigned long count = 0; /* Number of blocks in the run */
  3736. __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
  3737. corresponding to
  3738. block_to_free */
  3739. ext4_fsblk_t nr; /* Current block # */
  3740. __le32 *p; /* Pointer into inode/ind
  3741. for current block */
  3742. int err;
  3743. if (this_bh) { /* For indirect block */
  3744. BUFFER_TRACE(this_bh, "get_write_access");
  3745. err = ext4_journal_get_write_access(handle, this_bh);
  3746. /* Important: if we can't update the indirect pointers
  3747. * to the blocks, we can't free them. */
  3748. if (err)
  3749. return;
  3750. }
  3751. for (p = first; p < last; p++) {
  3752. nr = le32_to_cpu(*p);
  3753. if (nr) {
  3754. /* accumulate blocks to free if they're contiguous */
  3755. if (count == 0) {
  3756. block_to_free = nr;
  3757. block_to_free_p = p;
  3758. count = 1;
  3759. } else if (nr == block_to_free + count) {
  3760. count++;
  3761. } else {
  3762. ext4_clear_blocks(handle, inode, this_bh,
  3763. block_to_free,
  3764. count, block_to_free_p, p);
  3765. block_to_free = nr;
  3766. block_to_free_p = p;
  3767. count = 1;
  3768. }
  3769. }
  3770. }
  3771. if (count > 0)
  3772. ext4_clear_blocks(handle, inode, this_bh, block_to_free,
  3773. count, block_to_free_p, p);
  3774. if (this_bh) {
  3775. BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
  3776. /*
  3777. * The buffer head should have an attached journal head at this
  3778. * point. However, if the data is corrupted and an indirect
  3779. * block pointed to itself, it would have been detached when
  3780. * the block was cleared. Check for this instead of OOPSing.
  3781. */
  3782. if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
  3783. ext4_handle_dirty_metadata(handle, inode, this_bh);
  3784. else
  3785. ext4_error(inode->i_sb, __func__,
  3786. "circular indirect block detected, "
  3787. "inode=%lu, block=%llu",
  3788. inode->i_ino,
  3789. (unsigned long long) this_bh->b_blocknr);
  3790. }
  3791. }
  3792. /**
  3793. * ext4_free_branches - free an array of branches
  3794. * @handle: JBD handle for this transaction
  3795. * @inode: inode we are dealing with
  3796. * @parent_bh: the buffer_head which contains *@first and *@last
  3797. * @first: array of block numbers
  3798. * @last: pointer immediately past the end of array
  3799. * @depth: depth of the branches to free
  3800. *
  3801. * We are freeing all blocks refered from these branches (numbers are
  3802. * stored as little-endian 32-bit) and updating @inode->i_blocks
  3803. * appropriately.
  3804. */
  3805. static void ext4_free_branches(handle_t *handle, struct inode *inode,
  3806. struct buffer_head *parent_bh,
  3807. __le32 *first, __le32 *last, int depth)
  3808. {
  3809. ext4_fsblk_t nr;
  3810. __le32 *p;
  3811. if (ext4_handle_is_aborted(handle))
  3812. return;
  3813. if (depth--) {
  3814. struct buffer_head *bh;
  3815. int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  3816. p = last;
  3817. while (--p >= first) {
  3818. nr = le32_to_cpu(*p);
  3819. if (!nr)
  3820. continue; /* A hole */
  3821. /* Go read the buffer for the next level down */
  3822. bh = sb_bread(inode->i_sb, nr);
  3823. /*
  3824. * A read failure? Report error and clear slot
  3825. * (should be rare).
  3826. */
  3827. if (!bh) {
  3828. ext4_error(inode->i_sb, "ext4_free_branches",
  3829. "Read failure, inode=%lu, block=%llu",
  3830. inode->i_ino, nr);
  3831. continue;
  3832. }
  3833. /* This zaps the entire block. Bottom up. */
  3834. BUFFER_TRACE(bh, "free child branches");
  3835. ext4_free_branches(handle, inode, bh,
  3836. (__le32 *) bh->b_data,
  3837. (__le32 *) bh->b_data + addr_per_block,
  3838. depth);
  3839. /*
  3840. * We've probably journalled the indirect block several
  3841. * times during the truncate. But it's no longer
  3842. * needed and we now drop it from the transaction via
  3843. * jbd2_journal_revoke().
  3844. *
  3845. * That's easy if it's exclusively part of this
  3846. * transaction. But if it's part of the committing
  3847. * transaction then jbd2_journal_forget() will simply
  3848. * brelse() it. That means that if the underlying
  3849. * block is reallocated in ext4_get_block(),
  3850. * unmap_underlying_metadata() will find this block
  3851. * and will try to get rid of it. damn, damn.
  3852. *
  3853. * If this block has already been committed to the
  3854. * journal, a revoke record will be written. And
  3855. * revoke records must be emitted *before* clearing
  3856. * this block's bit in the bitmaps.
  3857. */
  3858. ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
  3859. /*
  3860. * Everything below this this pointer has been
  3861. * released. Now let this top-of-subtree go.
  3862. *
  3863. * We want the freeing of this indirect block to be
  3864. * atomic in the journal with the updating of the
  3865. * bitmap block which owns it. So make some room in
  3866. * the journal.
  3867. *
  3868. * We zero the parent pointer *after* freeing its
  3869. * pointee in the bitmaps, so if extend_transaction()
  3870. * for some reason fails to put the bitmap changes and
  3871. * the release into the same transaction, recovery
  3872. * will merely complain about releasing a free block,
  3873. * rather than leaking blocks.
  3874. */
  3875. if (ext4_handle_is_aborted(handle))
  3876. return;
  3877. if (try_to_extend_transaction(handle, inode)) {
  3878. ext4_mark_inode_dirty(handle, inode);
  3879. ext4_truncate_restart_trans(handle, inode,
  3880. blocks_for_truncate(inode));
  3881. }
  3882. ext4_free_blocks(handle, inode, 0, nr, 1,
  3883. EXT4_FREE_BLOCKS_METADATA);
  3884. if (parent_bh) {
  3885. /*
  3886. * The block which we have just freed is
  3887. * pointed to by an indirect block: journal it
  3888. */
  3889. BUFFER_TRACE(parent_bh, "get_write_access");
  3890. if (!ext4_journal_get_write_access(handle,
  3891. parent_bh)){
  3892. *p = 0;
  3893. BUFFER_TRACE(parent_bh,
  3894. "call ext4_handle_dirty_metadata");
  3895. ext4_handle_dirty_metadata(handle,
  3896. inode,
  3897. parent_bh);
  3898. }
  3899. }
  3900. }
  3901. } else {
  3902. /* We have reached the bottom of the tree. */
  3903. BUFFER_TRACE(parent_bh, "free data blocks");
  3904. ext4_free_data(handle, inode, parent_bh, first, last);
  3905. }
  3906. }
  3907. int ext4_can_truncate(struct inode *inode)
  3908. {
  3909. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  3910. return 0;
  3911. if (S_ISREG(inode->i_mode))
  3912. return 1;
  3913. if (S_ISDIR(inode->i_mode))
  3914. return 1;
  3915. if (S_ISLNK(inode->i_mode))
  3916. return !ext4_inode_is_fast_symlink(inode);
  3917. return 0;
  3918. }
  3919. /*
  3920. * ext4_truncate()
  3921. *
  3922. * We block out ext4_get_block() block instantiations across the entire
  3923. * transaction, and VFS/VM ensures that ext4_truncate() cannot run
  3924. * simultaneously on behalf of the same inode.
  3925. *
  3926. * As we work through the truncate and commmit bits of it to the journal there
  3927. * is one core, guiding principle: the file's tree must always be consistent on
  3928. * disk. We must be able to restart the truncate after a crash.
  3929. *
  3930. * The file's tree may be transiently inconsistent in memory (although it
  3931. * probably isn't), but whenever we close off and commit a journal transaction,
  3932. * the contents of (the filesystem + the journal) must be consistent and
  3933. * restartable. It's pretty simple, really: bottom up, right to left (although
  3934. * left-to-right works OK too).
  3935. *
  3936. * Note that at recovery time, journal replay occurs *before* the restart of
  3937. * truncate against the orphan inode list.
  3938. *
  3939. * The committed inode has the new, desired i_size (which is the same as
  3940. * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
  3941. * that this inode's truncate did not complete and it will again call
  3942. * ext4_truncate() to have another go. So there will be instantiated blocks
  3943. * to the right of the truncation point in a crashed ext4 filesystem. But
  3944. * that's fine - as long as they are linked from the inode, the post-crash
  3945. * ext4_truncate() run will find them and release them.
  3946. */
  3947. void ext4_truncate(struct inode *inode)
  3948. {
  3949. handle_t *handle;
  3950. struct ext4_inode_info *ei = EXT4_I(inode);
  3951. __le32 *i_data = ei->i_data;
  3952. int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  3953. struct address_space *mapping = inode->i_mapping;
  3954. ext4_lblk_t offsets[4];
  3955. Indirect chain[4];
  3956. Indirect *partial;
  3957. __le32 nr = 0;
  3958. int n;
  3959. ext4_lblk_t last_block;
  3960. unsigned blocksize = inode->i_sb->s_blocksize;
  3961. if (!ext4_can_truncate(inode))
  3962. return;
  3963. if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
  3964. ei->i_state |= EXT4_STATE_DA_ALLOC_CLOSE;
  3965. if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
  3966. ext4_ext_truncate(inode);
  3967. return;
  3968. }
  3969. handle = start_transaction(inode);
  3970. if (IS_ERR(handle))
  3971. return; /* AKPM: return what? */
  3972. last_block = (inode->i_size + blocksize-1)
  3973. >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
  3974. if (inode->i_size & (blocksize - 1))
  3975. if (ext4_block_truncate_page(handle, mapping, inode->i_size))
  3976. goto out_stop;
  3977. n = ext4_block_to_path(inode, last_block, offsets, NULL);
  3978. if (n == 0)
  3979. goto out_stop; /* error */
  3980. /*
  3981. * OK. This truncate is going to happen. We add the inode to the
  3982. * orphan list, so that if this truncate spans multiple transactions,
  3983. * and we crash, we will resume the truncate when the filesystem
  3984. * recovers. It also marks the inode dirty, to catch the new size.
  3985. *
  3986. * Implication: the file must always be in a sane, consistent
  3987. * truncatable state while each transaction commits.
  3988. */
  3989. if (ext4_orphan_add(handle, inode))
  3990. goto out_stop;
  3991. /*
  3992. * From here we block out all ext4_get_block() callers who want to
  3993. * modify the block allocation tree.
  3994. */
  3995. down_write(&ei->i_data_sem);
  3996. ext4_discard_preallocations(inode);
  3997. /*
  3998. * The orphan list entry will now protect us from any crash which
  3999. * occurs before the truncate completes, so it is now safe to propagate
  4000. * the new, shorter inode size (held for now in i_size) into the
  4001. * on-disk inode. We do this via i_disksize, which is the value which
  4002. * ext4 *really* writes onto the disk inode.
  4003. */
  4004. ei->i_disksize = inode->i_size;
  4005. if (n == 1) { /* direct blocks */
  4006. ext4_free_data(handle, inode, NULL, i_data+offsets[0],
  4007. i_data + EXT4_NDIR_BLOCKS);
  4008. goto do_indirects;
  4009. }
  4010. partial = ext4_find_shared(inode, n, offsets, chain, &nr);
  4011. /* Kill the top of shared branch (not detached) */
  4012. if (nr) {
  4013. if (partial == chain) {
  4014. /* Shared branch grows from the inode */
  4015. ext4_free_branches(handle, inode, NULL,
  4016. &nr, &nr+1, (chain+n-1) - partial);
  4017. *partial->p = 0;
  4018. /*
  4019. * We mark the inode dirty prior to restart,
  4020. * and prior to stop. No need for it here.
  4021. */
  4022. } else {
  4023. /* Shared branch grows from an indirect block */
  4024. BUFFER_TRACE(partial->bh, "get_write_access");
  4025. ext4_free_branches(handle, inode, partial->bh,
  4026. partial->p,
  4027. partial->p+1, (chain+n-1) - partial);
  4028. }
  4029. }
  4030. /* Clear the ends of indirect blocks on the shared branch */
  4031. while (partial > chain) {
  4032. ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
  4033. (__le32*)partial->bh->b_data+addr_per_block,
  4034. (chain+n-1) - partial);
  4035. BUFFER_TRACE(partial->bh, "call brelse");
  4036. brelse(partial->bh);
  4037. partial--;
  4038. }
  4039. do_indirects:
  4040. /* Kill the remaining (whole) subtrees */
  4041. switch (offsets[0]) {
  4042. default:
  4043. nr = i_data[EXT4_IND_BLOCK];
  4044. if (nr) {
  4045. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
  4046. i_data[EXT4_IND_BLOCK] = 0;
  4047. }
  4048. case EXT4_IND_BLOCK:
  4049. nr = i_data[EXT4_DIND_BLOCK];
  4050. if (nr) {
  4051. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
  4052. i_data[EXT4_DIND_BLOCK] = 0;
  4053. }
  4054. case EXT4_DIND_BLOCK:
  4055. nr = i_data[EXT4_TIND_BLOCK];
  4056. if (nr) {
  4057. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
  4058. i_data[EXT4_TIND_BLOCK] = 0;
  4059. }
  4060. case EXT4_TIND_BLOCK:
  4061. ;
  4062. }
  4063. up_write(&ei->i_data_sem);
  4064. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  4065. ext4_mark_inode_dirty(handle, inode);
  4066. /*
  4067. * In a multi-transaction truncate, we only make the final transaction
  4068. * synchronous
  4069. */
  4070. if (IS_SYNC(inode))
  4071. ext4_handle_sync(handle);
  4072. out_stop:
  4073. /*
  4074. * If this was a simple ftruncate(), and the file will remain alive
  4075. * then we need to clear up the orphan record which we created above.
  4076. * However, if this was a real unlink then we were called by
  4077. * ext4_delete_inode(), and we allow that function to clean up the
  4078. * orphan info for us.
  4079. */
  4080. if (inode->i_nlink)
  4081. ext4_orphan_del(handle, inode);
  4082. ext4_journal_stop(handle);
  4083. }
  4084. /*
  4085. * ext4_get_inode_loc returns with an extra refcount against the inode's
  4086. * underlying buffer_head on success. If 'in_mem' is true, we have all
  4087. * data in memory that is needed to recreate the on-disk version of this
  4088. * inode.
  4089. */
  4090. static int __ext4_get_inode_loc(struct inode *inode,
  4091. struct ext4_iloc *iloc, int in_mem)
  4092. {
  4093. struct ext4_group_desc *gdp;
  4094. struct buffer_head *bh;
  4095. struct super_block *sb = inode->i_sb;
  4096. ext4_fsblk_t block;
  4097. int inodes_per_block, inode_offset;
  4098. iloc->bh = NULL;
  4099. if (!ext4_valid_inum(sb, inode->i_ino))
  4100. return -EIO;
  4101. iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
  4102. gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
  4103. if (!gdp)
  4104. return -EIO;
  4105. /*
  4106. * Figure out the offset within the block group inode table
  4107. */
  4108. inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb));
  4109. inode_offset = ((inode->i_ino - 1) %
  4110. EXT4_INODES_PER_GROUP(sb));
  4111. block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
  4112. iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
  4113. bh = sb_getblk(sb, block);
  4114. if (!bh) {
  4115. ext4_error(sb, "ext4_get_inode_loc", "unable to read "
  4116. "inode block - inode=%lu, block=%llu",
  4117. inode->i_ino, block);
  4118. return -EIO;
  4119. }
  4120. if (!buffer_uptodate(bh)) {
  4121. lock_buffer(bh);
  4122. /*
  4123. * If the buffer has the write error flag, we have failed
  4124. * to write out another inode in the same block. In this
  4125. * case, we don't have to read the block because we may
  4126. * read the old inode data successfully.
  4127. */
  4128. if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
  4129. set_buffer_uptodate(bh);
  4130. if (buffer_uptodate(bh)) {
  4131. /* someone brought it uptodate while we waited */
  4132. unlock_buffer(bh);
  4133. goto has_buffer;
  4134. }
  4135. /*
  4136. * If we have all information of the inode in memory and this
  4137. * is the only valid inode in the block, we need not read the
  4138. * block.
  4139. */
  4140. if (in_mem) {
  4141. struct buffer_head *bitmap_bh;
  4142. int i, start;
  4143. start = inode_offset & ~(inodes_per_block - 1);
  4144. /* Is the inode bitmap in cache? */
  4145. bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
  4146. if (!bitmap_bh)
  4147. goto make_io;
  4148. /*
  4149. * If the inode bitmap isn't in cache then the
  4150. * optimisation may end up performing two reads instead
  4151. * of one, so skip it.
  4152. */
  4153. if (!buffer_uptodate(bitmap_bh)) {
  4154. brelse(bitmap_bh);
  4155. goto make_io;
  4156. }
  4157. for (i = start; i < start + inodes_per_block; i++) {
  4158. if (i == inode_offset)
  4159. continue;
  4160. if (ext4_test_bit(i, bitmap_bh->b_data))
  4161. break;
  4162. }
  4163. brelse(bitmap_bh);
  4164. if (i == start + inodes_per_block) {
  4165. /* all other inodes are free, so skip I/O */
  4166. memset(bh->b_data, 0, bh->b_size);
  4167. set_buffer_uptodate(bh);
  4168. unlock_buffer(bh);
  4169. goto has_buffer;
  4170. }
  4171. }
  4172. make_io:
  4173. /*
  4174. * If we need to do any I/O, try to pre-readahead extra
  4175. * blocks from the inode table.
  4176. */
  4177. if (EXT4_SB(sb)->s_inode_readahead_blks) {
  4178. ext4_fsblk_t b, end, table;
  4179. unsigned num;
  4180. table = ext4_inode_table(sb, gdp);
  4181. /* s_inode_readahead_blks is always a power of 2 */
  4182. b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
  4183. if (table > b)
  4184. b = table;
  4185. end = b + EXT4_SB(sb)->s_inode_readahead_blks;
  4186. num = EXT4_INODES_PER_GROUP(sb);
  4187. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  4188. EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
  4189. num -= ext4_itable_unused_count(sb, gdp);
  4190. table += num / inodes_per_block;
  4191. if (end > table)
  4192. end = table;
  4193. while (b <= end)
  4194. sb_breadahead(sb, b++);
  4195. }
  4196. /*
  4197. * There are other valid inodes in the buffer, this inode
  4198. * has in-inode xattrs, or we don't have this inode in memory.
  4199. * Read the block from disk.
  4200. */
  4201. get_bh(bh);
  4202. bh->b_end_io = end_buffer_read_sync;
  4203. submit_bh(READ_META, bh);
  4204. wait_on_buffer(bh);
  4205. if (!buffer_uptodate(bh)) {
  4206. ext4_error(sb, __func__,
  4207. "unable to read inode block - inode=%lu, "
  4208. "block=%llu", inode->i_ino, block);
  4209. brelse(bh);
  4210. return -EIO;
  4211. }
  4212. }
  4213. has_buffer:
  4214. iloc->bh = bh;
  4215. return 0;
  4216. }
  4217. int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
  4218. {
  4219. /* We have all inode data except xattrs in memory here. */
  4220. return __ext4_get_inode_loc(inode, iloc,
  4221. !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR));
  4222. }
  4223. void ext4_set_inode_flags(struct inode *inode)
  4224. {
  4225. unsigned int flags = EXT4_I(inode)->i_flags;
  4226. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  4227. if (flags & EXT4_SYNC_FL)
  4228. inode->i_flags |= S_SYNC;
  4229. if (flags & EXT4_APPEND_FL)
  4230. inode->i_flags |= S_APPEND;
  4231. if (flags & EXT4_IMMUTABLE_FL)
  4232. inode->i_flags |= S_IMMUTABLE;
  4233. if (flags & EXT4_NOATIME_FL)
  4234. inode->i_flags |= S_NOATIME;
  4235. if (flags & EXT4_DIRSYNC_FL)
  4236. inode->i_flags |= S_DIRSYNC;
  4237. }
  4238. /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
  4239. void ext4_get_inode_flags(struct ext4_inode_info *ei)
  4240. {
  4241. unsigned int flags = ei->vfs_inode.i_flags;
  4242. ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
  4243. EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL);
  4244. if (flags & S_SYNC)
  4245. ei->i_flags |= EXT4_SYNC_FL;
  4246. if (flags & S_APPEND)
  4247. ei->i_flags |= EXT4_APPEND_FL;
  4248. if (flags & S_IMMUTABLE)
  4249. ei->i_flags |= EXT4_IMMUTABLE_FL;
  4250. if (flags & S_NOATIME)
  4251. ei->i_flags |= EXT4_NOATIME_FL;
  4252. if (flags & S_DIRSYNC)
  4253. ei->i_flags |= EXT4_DIRSYNC_FL;
  4254. }
  4255. static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
  4256. struct ext4_inode_info *ei)
  4257. {
  4258. blkcnt_t i_blocks ;
  4259. struct inode *inode = &(ei->vfs_inode);
  4260. struct super_block *sb = inode->i_sb;
  4261. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  4262. EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
  4263. /* we are using combined 48 bit field */
  4264. i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
  4265. le32_to_cpu(raw_inode->i_blocks_lo);
  4266. if (ei->i_flags & EXT4_HUGE_FILE_FL) {
  4267. /* i_blocks represent file system block size */
  4268. return i_blocks << (inode->i_blkbits - 9);
  4269. } else {
  4270. return i_blocks;
  4271. }
  4272. } else {
  4273. return le32_to_cpu(raw_inode->i_blocks_lo);
  4274. }
  4275. }
  4276. struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
  4277. {
  4278. struct ext4_iloc iloc;
  4279. struct ext4_inode *raw_inode;
  4280. struct ext4_inode_info *ei;
  4281. struct inode *inode;
  4282. journal_t *journal = EXT4_SB(sb)->s_journal;
  4283. long ret;
  4284. int block;
  4285. inode = iget_locked(sb, ino);
  4286. if (!inode)
  4287. return ERR_PTR(-ENOMEM);
  4288. if (!(inode->i_state & I_NEW))
  4289. return inode;
  4290. ei = EXT4_I(inode);
  4291. iloc.bh = 0;
  4292. ret = __ext4_get_inode_loc(inode, &iloc, 0);
  4293. if (ret < 0)
  4294. goto bad_inode;
  4295. raw_inode = ext4_raw_inode(&iloc);
  4296. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  4297. inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  4298. inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  4299. if (!(test_opt(inode->i_sb, NO_UID32))) {
  4300. inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  4301. inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  4302. }
  4303. inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
  4304. ei->i_state = 0;
  4305. ei->i_dir_start_lookup = 0;
  4306. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  4307. /* We now have enough fields to check if the inode was active or not.
  4308. * This is needed because nfsd might try to access dead inodes
  4309. * the test is that same one that e2fsck uses
  4310. * NeilBrown 1999oct15
  4311. */
  4312. if (inode->i_nlink == 0) {
  4313. if (inode->i_mode == 0 ||
  4314. !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
  4315. /* this inode is deleted */
  4316. ret = -ESTALE;
  4317. goto bad_inode;
  4318. }
  4319. /* The only unlinked inodes we let through here have
  4320. * valid i_mode and are being read by the orphan
  4321. * recovery code: that's fine, we're about to complete
  4322. * the process of deleting those. */
  4323. }
  4324. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  4325. inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
  4326. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
  4327. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
  4328. ei->i_file_acl |=
  4329. ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
  4330. inode->i_size = ext4_isize(raw_inode);
  4331. ei->i_disksize = inode->i_size;
  4332. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  4333. ei->i_block_group = iloc.block_group;
  4334. ei->i_last_alloc_group = ~0;
  4335. /*
  4336. * NOTE! The in-memory inode i_data array is in little-endian order
  4337. * even on big-endian machines: we do NOT byteswap the block numbers!
  4338. */
  4339. for (block = 0; block < EXT4_N_BLOCKS; block++)
  4340. ei->i_data[block] = raw_inode->i_block[block];
  4341. INIT_LIST_HEAD(&ei->i_orphan);
  4342. /*
  4343. * Set transaction id's of transactions that have to be committed
  4344. * to finish f[data]sync. We set them to currently running transaction
  4345. * as we cannot be sure that the inode or some of its metadata isn't
  4346. * part of the transaction - the inode could have been reclaimed and
  4347. * now it is reread from disk.
  4348. */
  4349. if (journal) {
  4350. transaction_t *transaction;
  4351. tid_t tid;
  4352. spin_lock(&journal->j_state_lock);
  4353. if (journal->j_running_transaction)
  4354. transaction = journal->j_running_transaction;
  4355. else
  4356. transaction = journal->j_committing_transaction;
  4357. if (transaction)
  4358. tid = transaction->t_tid;
  4359. else
  4360. tid = journal->j_commit_sequence;
  4361. spin_unlock(&journal->j_state_lock);
  4362. ei->i_sync_tid = tid;
  4363. ei->i_datasync_tid = tid;
  4364. }
  4365. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  4366. ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
  4367. if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
  4368. EXT4_INODE_SIZE(inode->i_sb)) {
  4369. ret = -EIO;
  4370. goto bad_inode;
  4371. }
  4372. if (ei->i_extra_isize == 0) {
  4373. /* The extra space is currently unused. Use it. */
  4374. ei->i_extra_isize = sizeof(struct ext4_inode) -
  4375. EXT4_GOOD_OLD_INODE_SIZE;
  4376. } else {
  4377. __le32 *magic = (void *)raw_inode +
  4378. EXT4_GOOD_OLD_INODE_SIZE +
  4379. ei->i_extra_isize;
  4380. if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC))
  4381. ei->i_state |= EXT4_STATE_XATTR;
  4382. }
  4383. } else
  4384. ei->i_extra_isize = 0;
  4385. EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
  4386. EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
  4387. EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
  4388. EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
  4389. inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
  4390. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  4391. if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
  4392. inode->i_version |=
  4393. (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
  4394. }
  4395. ret = 0;
  4396. if (ei->i_file_acl &&
  4397. !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
  4398. ext4_error(sb, __func__,
  4399. "bad extended attribute block %llu in inode #%lu",
  4400. ei->i_file_acl, inode->i_ino);
  4401. ret = -EIO;
  4402. goto bad_inode;
  4403. } else if (ei->i_flags & EXT4_EXTENTS_FL) {
  4404. if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  4405. (S_ISLNK(inode->i_mode) &&
  4406. !ext4_inode_is_fast_symlink(inode)))
  4407. /* Validate extent which is part of inode */
  4408. ret = ext4_ext_check_inode(inode);
  4409. } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  4410. (S_ISLNK(inode->i_mode) &&
  4411. !ext4_inode_is_fast_symlink(inode))) {
  4412. /* Validate block references which are part of inode */
  4413. ret = ext4_check_inode_blockref(inode);
  4414. }
  4415. if (ret)
  4416. goto bad_inode;
  4417. if (S_ISREG(inode->i_mode)) {
  4418. inode->i_op = &ext4_file_inode_operations;
  4419. inode->i_fop = &ext4_file_operations;
  4420. ext4_set_aops(inode);
  4421. } else if (S_ISDIR(inode->i_mode)) {
  4422. inode->i_op = &ext4_dir_inode_operations;
  4423. inode->i_fop = &ext4_dir_operations;
  4424. } else if (S_ISLNK(inode->i_mode)) {
  4425. if (ext4_inode_is_fast_symlink(inode)) {
  4426. inode->i_op = &ext4_fast_symlink_inode_operations;
  4427. nd_terminate_link(ei->i_data, inode->i_size,
  4428. sizeof(ei->i_data) - 1);
  4429. } else {
  4430. inode->i_op = &ext4_symlink_inode_operations;
  4431. ext4_set_aops(inode);
  4432. }
  4433. } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  4434. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  4435. inode->i_op = &ext4_special_inode_operations;
  4436. if (raw_inode->i_block[0])
  4437. init_special_inode(inode, inode->i_mode,
  4438. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  4439. else
  4440. init_special_inode(inode, inode->i_mode,
  4441. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  4442. } else {
  4443. ret = -EIO;
  4444. ext4_error(inode->i_sb, __func__,
  4445. "bogus i_mode (%o) for inode=%lu",
  4446. inode->i_mode, inode->i_ino);
  4447. goto bad_inode;
  4448. }
  4449. brelse(iloc.bh);
  4450. ext4_set_inode_flags(inode);
  4451. unlock_new_inode(inode);
  4452. return inode;
  4453. bad_inode:
  4454. brelse(iloc.bh);
  4455. iget_failed(inode);
  4456. return ERR_PTR(ret);
  4457. }
  4458. static int ext4_inode_blocks_set(handle_t *handle,
  4459. struct ext4_inode *raw_inode,
  4460. struct ext4_inode_info *ei)
  4461. {
  4462. struct inode *inode = &(ei->vfs_inode);
  4463. u64 i_blocks = inode->i_blocks;
  4464. struct super_block *sb = inode->i_sb;
  4465. if (i_blocks <= ~0U) {
  4466. /*
  4467. * i_blocks can be represnted in a 32 bit variable
  4468. * as multiple of 512 bytes
  4469. */
  4470. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  4471. raw_inode->i_blocks_high = 0;
  4472. ei->i_flags &= ~EXT4_HUGE_FILE_FL;
  4473. return 0;
  4474. }
  4475. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
  4476. return -EFBIG;
  4477. if (i_blocks <= 0xffffffffffffULL) {
  4478. /*
  4479. * i_blocks can be represented in a 48 bit variable
  4480. * as multiple of 512 bytes
  4481. */
  4482. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  4483. raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
  4484. ei->i_flags &= ~EXT4_HUGE_FILE_FL;
  4485. } else {
  4486. ei->i_flags |= EXT4_HUGE_FILE_FL;
  4487. /* i_block is stored in file system block size */
  4488. i_blocks = i_blocks >> (inode->i_blkbits - 9);
  4489. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  4490. raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
  4491. }
  4492. return 0;
  4493. }
  4494. /*
  4495. * Post the struct inode info into an on-disk inode location in the
  4496. * buffer-cache. This gobbles the caller's reference to the
  4497. * buffer_head in the inode location struct.
  4498. *
  4499. * The caller must have write access to iloc->bh.
  4500. */
  4501. static int ext4_do_update_inode(handle_t *handle,
  4502. struct inode *inode,
  4503. struct ext4_iloc *iloc)
  4504. {
  4505. struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
  4506. struct ext4_inode_info *ei = EXT4_I(inode);
  4507. struct buffer_head *bh = iloc->bh;
  4508. int err = 0, rc, block;
  4509. /* For fields not not tracking in the in-memory inode,
  4510. * initialise them to zero for new inodes. */
  4511. if (ei->i_state & EXT4_STATE_NEW)
  4512. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  4513. ext4_get_inode_flags(ei);
  4514. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  4515. if (!(test_opt(inode->i_sb, NO_UID32))) {
  4516. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
  4517. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
  4518. /*
  4519. * Fix up interoperability with old kernels. Otherwise, old inodes get
  4520. * re-used with the upper 16 bits of the uid/gid intact
  4521. */
  4522. if (!ei->i_dtime) {
  4523. raw_inode->i_uid_high =
  4524. cpu_to_le16(high_16_bits(inode->i_uid));
  4525. raw_inode->i_gid_high =
  4526. cpu_to_le16(high_16_bits(inode->i_gid));
  4527. } else {
  4528. raw_inode->i_uid_high = 0;
  4529. raw_inode->i_gid_high = 0;
  4530. }
  4531. } else {
  4532. raw_inode->i_uid_low =
  4533. cpu_to_le16(fs_high2lowuid(inode->i_uid));
  4534. raw_inode->i_gid_low =
  4535. cpu_to_le16(fs_high2lowgid(inode->i_gid));
  4536. raw_inode->i_uid_high = 0;
  4537. raw_inode->i_gid_high = 0;
  4538. }
  4539. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  4540. EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
  4541. EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
  4542. EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
  4543. EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
  4544. if (ext4_inode_blocks_set(handle, raw_inode, ei))
  4545. goto out_brelse;
  4546. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  4547. raw_inode->i_flags = cpu_to_le32(ei->i_flags);
  4548. if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
  4549. cpu_to_le32(EXT4_OS_HURD))
  4550. raw_inode->i_file_acl_high =
  4551. cpu_to_le16(ei->i_file_acl >> 32);
  4552. raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
  4553. ext4_isize_set(raw_inode, ei->i_disksize);
  4554. if (ei->i_disksize > 0x7fffffffULL) {
  4555. struct super_block *sb = inode->i_sb;
  4556. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
  4557. EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
  4558. EXT4_SB(sb)->s_es->s_rev_level ==
  4559. cpu_to_le32(EXT4_GOOD_OLD_REV)) {
  4560. /* If this is the first large file
  4561. * created, add a flag to the superblock.
  4562. */
  4563. err = ext4_journal_get_write_access(handle,
  4564. EXT4_SB(sb)->s_sbh);
  4565. if (err)
  4566. goto out_brelse;
  4567. ext4_update_dynamic_rev(sb);
  4568. EXT4_SET_RO_COMPAT_FEATURE(sb,
  4569. EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
  4570. sb->s_dirt = 1;
  4571. ext4_handle_sync(handle);
  4572. err = ext4_handle_dirty_metadata(handle, inode,
  4573. EXT4_SB(sb)->s_sbh);
  4574. }
  4575. }
  4576. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  4577. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  4578. if (old_valid_dev(inode->i_rdev)) {
  4579. raw_inode->i_block[0] =
  4580. cpu_to_le32(old_encode_dev(inode->i_rdev));
  4581. raw_inode->i_block[1] = 0;
  4582. } else {
  4583. raw_inode->i_block[0] = 0;
  4584. raw_inode->i_block[1] =
  4585. cpu_to_le32(new_encode_dev(inode->i_rdev));
  4586. raw_inode->i_block[2] = 0;
  4587. }
  4588. } else
  4589. for (block = 0; block < EXT4_N_BLOCKS; block++)
  4590. raw_inode->i_block[block] = ei->i_data[block];
  4591. raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
  4592. if (ei->i_extra_isize) {
  4593. if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
  4594. raw_inode->i_version_hi =
  4595. cpu_to_le32(inode->i_version >> 32);
  4596. raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
  4597. }
  4598. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  4599. rc = ext4_handle_dirty_metadata(handle, inode, bh);
  4600. if (!err)
  4601. err = rc;
  4602. ei->i_state &= ~EXT4_STATE_NEW;
  4603. ext4_update_inode_fsync_trans(handle, inode, 0);
  4604. out_brelse:
  4605. brelse(bh);
  4606. ext4_std_error(inode->i_sb, err);
  4607. return err;
  4608. }
  4609. /*
  4610. * ext4_write_inode()
  4611. *
  4612. * We are called from a few places:
  4613. *
  4614. * - Within generic_file_write() for O_SYNC files.
  4615. * Here, there will be no transaction running. We wait for any running
  4616. * trasnaction to commit.
  4617. *
  4618. * - Within sys_sync(), kupdate and such.
  4619. * We wait on commit, if tol to.
  4620. *
  4621. * - Within prune_icache() (PF_MEMALLOC == true)
  4622. * Here we simply return. We can't afford to block kswapd on the
  4623. * journal commit.
  4624. *
  4625. * In all cases it is actually safe for us to return without doing anything,
  4626. * because the inode has been copied into a raw inode buffer in
  4627. * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
  4628. * knfsd.
  4629. *
  4630. * Note that we are absolutely dependent upon all inode dirtiers doing the
  4631. * right thing: they *must* call mark_inode_dirty() after dirtying info in
  4632. * which we are interested.
  4633. *
  4634. * It would be a bug for them to not do this. The code:
  4635. *
  4636. * mark_inode_dirty(inode)
  4637. * stuff();
  4638. * inode->i_size = expr;
  4639. *
  4640. * is in error because a kswapd-driven write_inode() could occur while
  4641. * `stuff()' is running, and the new i_size will be lost. Plus the inode
  4642. * will no longer be on the superblock's dirty inode list.
  4643. */
  4644. int ext4_write_inode(struct inode *inode, int wait)
  4645. {
  4646. int err;
  4647. if (current->flags & PF_MEMALLOC)
  4648. return 0;
  4649. if (EXT4_SB(inode->i_sb)->s_journal) {
  4650. if (ext4_journal_current_handle()) {
  4651. jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
  4652. dump_stack();
  4653. return -EIO;
  4654. }
  4655. if (!wait)
  4656. return 0;
  4657. err = ext4_force_commit(inode->i_sb);
  4658. } else {
  4659. struct ext4_iloc iloc;
  4660. err = ext4_get_inode_loc(inode, &iloc);
  4661. if (err)
  4662. return err;
  4663. if (wait)
  4664. sync_dirty_buffer(iloc.bh);
  4665. if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
  4666. ext4_error(inode->i_sb, __func__,
  4667. "IO error syncing inode, "
  4668. "inode=%lu, block=%llu",
  4669. inode->i_ino,
  4670. (unsigned long long)iloc.bh->b_blocknr);
  4671. err = -EIO;
  4672. }
  4673. }
  4674. return err;
  4675. }
  4676. /*
  4677. * ext4_setattr()
  4678. *
  4679. * Called from notify_change.
  4680. *
  4681. * We want to trap VFS attempts to truncate the file as soon as
  4682. * possible. In particular, we want to make sure that when the VFS
  4683. * shrinks i_size, we put the inode on the orphan list and modify
  4684. * i_disksize immediately, so that during the subsequent flushing of
  4685. * dirty pages and freeing of disk blocks, we can guarantee that any
  4686. * commit will leave the blocks being flushed in an unused state on
  4687. * disk. (On recovery, the inode will get truncated and the blocks will
  4688. * be freed, so we have a strong guarantee that no future commit will
  4689. * leave these blocks visible to the user.)
  4690. *
  4691. * Another thing we have to assure is that if we are in ordered mode
  4692. * and inode is still attached to the committing transaction, we must
  4693. * we start writeout of all the dirty pages which are being truncated.
  4694. * This way we are sure that all the data written in the previous
  4695. * transaction are already on disk (truncate waits for pages under
  4696. * writeback).
  4697. *
  4698. * Called with inode->i_mutex down.
  4699. */
  4700. int ext4_setattr(struct dentry *dentry, struct iattr *attr)
  4701. {
  4702. struct inode *inode = dentry->d_inode;
  4703. int error, rc = 0;
  4704. const unsigned int ia_valid = attr->ia_valid;
  4705. error = inode_change_ok(inode, attr);
  4706. if (error)
  4707. return error;
  4708. if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
  4709. (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
  4710. handle_t *handle;
  4711. /* (user+group)*(old+new) structure, inode write (sb,
  4712. * inode block, ? - but truncate inode update has it) */
  4713. handle = ext4_journal_start(inode, (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+
  4714. EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb))+3);
  4715. if (IS_ERR(handle)) {
  4716. error = PTR_ERR(handle);
  4717. goto err_out;
  4718. }
  4719. error = vfs_dq_transfer(inode, attr) ? -EDQUOT : 0;
  4720. if (error) {
  4721. ext4_journal_stop(handle);
  4722. return error;
  4723. }
  4724. /* Update corresponding info in inode so that everything is in
  4725. * one transaction */
  4726. if (attr->ia_valid & ATTR_UID)
  4727. inode->i_uid = attr->ia_uid;
  4728. if (attr->ia_valid & ATTR_GID)
  4729. inode->i_gid = attr->ia_gid;
  4730. error = ext4_mark_inode_dirty(handle, inode);
  4731. ext4_journal_stop(handle);
  4732. }
  4733. if (attr->ia_valid & ATTR_SIZE) {
  4734. if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) {
  4735. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  4736. if (attr->ia_size > sbi->s_bitmap_maxbytes) {
  4737. error = -EFBIG;
  4738. goto err_out;
  4739. }
  4740. }
  4741. }
  4742. if (S_ISREG(inode->i_mode) &&
  4743. attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
  4744. handle_t *handle;
  4745. handle = ext4_journal_start(inode, 3);
  4746. if (IS_ERR(handle)) {
  4747. error = PTR_ERR(handle);
  4748. goto err_out;
  4749. }
  4750. error = ext4_orphan_add(handle, inode);
  4751. EXT4_I(inode)->i_disksize = attr->ia_size;
  4752. rc = ext4_mark_inode_dirty(handle, inode);
  4753. if (!error)
  4754. error = rc;
  4755. ext4_journal_stop(handle);
  4756. if (ext4_should_order_data(inode)) {
  4757. error = ext4_begin_ordered_truncate(inode,
  4758. attr->ia_size);
  4759. if (error) {
  4760. /* Do as much error cleanup as possible */
  4761. handle = ext4_journal_start(inode, 3);
  4762. if (IS_ERR(handle)) {
  4763. ext4_orphan_del(NULL, inode);
  4764. goto err_out;
  4765. }
  4766. ext4_orphan_del(handle, inode);
  4767. ext4_journal_stop(handle);
  4768. goto err_out;
  4769. }
  4770. }
  4771. }
  4772. rc = inode_setattr(inode, attr);
  4773. /* If inode_setattr's call to ext4_truncate failed to get a
  4774. * transaction handle at all, we need to clean up the in-core
  4775. * orphan list manually. */
  4776. if (inode->i_nlink)
  4777. ext4_orphan_del(NULL, inode);
  4778. if (!rc && (ia_valid & ATTR_MODE))
  4779. rc = ext4_acl_chmod(inode);
  4780. err_out:
  4781. ext4_std_error(inode->i_sb, error);
  4782. if (!error)
  4783. error = rc;
  4784. return error;
  4785. }
  4786. int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
  4787. struct kstat *stat)
  4788. {
  4789. struct inode *inode;
  4790. unsigned long delalloc_blocks;
  4791. inode = dentry->d_inode;
  4792. generic_fillattr(inode, stat);
  4793. /*
  4794. * We can't update i_blocks if the block allocation is delayed
  4795. * otherwise in the case of system crash before the real block
  4796. * allocation is done, we will have i_blocks inconsistent with
  4797. * on-disk file blocks.
  4798. * We always keep i_blocks updated together with real
  4799. * allocation. But to not confuse with user, stat
  4800. * will return the blocks that include the delayed allocation
  4801. * blocks for this file.
  4802. */
  4803. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  4804. delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks;
  4805. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  4806. stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
  4807. return 0;
  4808. }
  4809. static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks,
  4810. int chunk)
  4811. {
  4812. int indirects;
  4813. /* if nrblocks are contiguous */
  4814. if (chunk) {
  4815. /*
  4816. * With N contiguous data blocks, it need at most
  4817. * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks
  4818. * 2 dindirect blocks
  4819. * 1 tindirect block
  4820. */
  4821. indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb);
  4822. return indirects + 3;
  4823. }
  4824. /*
  4825. * if nrblocks are not contiguous, worse case, each block touch
  4826. * a indirect block, and each indirect block touch a double indirect
  4827. * block, plus a triple indirect block
  4828. */
  4829. indirects = nrblocks * 2 + 1;
  4830. return indirects;
  4831. }
  4832. static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
  4833. {
  4834. if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
  4835. return ext4_indirect_trans_blocks(inode, nrblocks, chunk);
  4836. return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
  4837. }
  4838. /*
  4839. * Account for index blocks, block groups bitmaps and block group
  4840. * descriptor blocks if modify datablocks and index blocks
  4841. * worse case, the indexs blocks spread over different block groups
  4842. *
  4843. * If datablocks are discontiguous, they are possible to spread over
  4844. * different block groups too. If they are contiuguous, with flexbg,
  4845. * they could still across block group boundary.
  4846. *
  4847. * Also account for superblock, inode, quota and xattr blocks
  4848. */
  4849. int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
  4850. {
  4851. ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
  4852. int gdpblocks;
  4853. int idxblocks;
  4854. int ret = 0;
  4855. /*
  4856. * How many index blocks need to touch to modify nrblocks?
  4857. * The "Chunk" flag indicating whether the nrblocks is
  4858. * physically contiguous on disk
  4859. *
  4860. * For Direct IO and fallocate, they calls get_block to allocate
  4861. * one single extent at a time, so they could set the "Chunk" flag
  4862. */
  4863. idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
  4864. ret = idxblocks;
  4865. /*
  4866. * Now let's see how many group bitmaps and group descriptors need
  4867. * to account
  4868. */
  4869. groups = idxblocks;
  4870. if (chunk)
  4871. groups += 1;
  4872. else
  4873. groups += nrblocks;
  4874. gdpblocks = groups;
  4875. if (groups > ngroups)
  4876. groups = ngroups;
  4877. if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
  4878. gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
  4879. /* bitmaps and block group descriptor blocks */
  4880. ret += groups + gdpblocks;
  4881. /* Blocks for super block, inode, quota and xattr blocks */
  4882. ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
  4883. return ret;
  4884. }
  4885. /*
  4886. * Calulate the total number of credits to reserve to fit
  4887. * the modification of a single pages into a single transaction,
  4888. * which may include multiple chunks of block allocations.
  4889. *
  4890. * This could be called via ext4_write_begin()
  4891. *
  4892. * We need to consider the worse case, when
  4893. * one new block per extent.
  4894. */
  4895. int ext4_writepage_trans_blocks(struct inode *inode)
  4896. {
  4897. int bpp = ext4_journal_blocks_per_page(inode);
  4898. int ret;
  4899. ret = ext4_meta_trans_blocks(inode, bpp, 0);
  4900. /* Account for data blocks for journalled mode */
  4901. if (ext4_should_journal_data(inode))
  4902. ret += bpp;
  4903. return ret;
  4904. }
  4905. /*
  4906. * Calculate the journal credits for a chunk of data modification.
  4907. *
  4908. * This is called from DIO, fallocate or whoever calling
  4909. * ext4_get_blocks() to map/allocate a chunk of contiguous disk blocks.
  4910. *
  4911. * journal buffers for data blocks are not included here, as DIO
  4912. * and fallocate do no need to journal data buffers.
  4913. */
  4914. int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
  4915. {
  4916. return ext4_meta_trans_blocks(inode, nrblocks, 1);
  4917. }
  4918. /*
  4919. * The caller must have previously called ext4_reserve_inode_write().
  4920. * Give this, we know that the caller already has write access to iloc->bh.
  4921. */
  4922. int ext4_mark_iloc_dirty(handle_t *handle,
  4923. struct inode *inode, struct ext4_iloc *iloc)
  4924. {
  4925. int err = 0;
  4926. if (test_opt(inode->i_sb, I_VERSION))
  4927. inode_inc_iversion(inode);
  4928. /* the do_update_inode consumes one bh->b_count */
  4929. get_bh(iloc->bh);
  4930. /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
  4931. err = ext4_do_update_inode(handle, inode, iloc);
  4932. put_bh(iloc->bh);
  4933. return err;
  4934. }
  4935. /*
  4936. * On success, We end up with an outstanding reference count against
  4937. * iloc->bh. This _must_ be cleaned up later.
  4938. */
  4939. int
  4940. ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
  4941. struct ext4_iloc *iloc)
  4942. {
  4943. int err;
  4944. err = ext4_get_inode_loc(inode, iloc);
  4945. if (!err) {
  4946. BUFFER_TRACE(iloc->bh, "get_write_access");
  4947. err = ext4_journal_get_write_access(handle, iloc->bh);
  4948. if (err) {
  4949. brelse(iloc->bh);
  4950. iloc->bh = NULL;
  4951. }
  4952. }
  4953. ext4_std_error(inode->i_sb, err);
  4954. return err;
  4955. }
  4956. /*
  4957. * Expand an inode by new_extra_isize bytes.
  4958. * Returns 0 on success or negative error number on failure.
  4959. */
  4960. static int ext4_expand_extra_isize(struct inode *inode,
  4961. unsigned int new_extra_isize,
  4962. struct ext4_iloc iloc,
  4963. handle_t *handle)
  4964. {
  4965. struct ext4_inode *raw_inode;
  4966. struct ext4_xattr_ibody_header *header;
  4967. struct ext4_xattr_entry *entry;
  4968. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
  4969. return 0;
  4970. raw_inode = ext4_raw_inode(&iloc);
  4971. header = IHDR(inode, raw_inode);
  4972. entry = IFIRST(header);
  4973. /* No extended attributes present */
  4974. if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) ||
  4975. header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
  4976. memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
  4977. new_extra_isize);
  4978. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  4979. return 0;
  4980. }
  4981. /* try to expand with EAs present */
  4982. return ext4_expand_extra_isize_ea(inode, new_extra_isize,
  4983. raw_inode, handle);
  4984. }
  4985. /*
  4986. * What we do here is to mark the in-core inode as clean with respect to inode
  4987. * dirtiness (it may still be data-dirty).
  4988. * This means that the in-core inode may be reaped by prune_icache
  4989. * without having to perform any I/O. This is a very good thing,
  4990. * because *any* task may call prune_icache - even ones which
  4991. * have a transaction open against a different journal.
  4992. *
  4993. * Is this cheating? Not really. Sure, we haven't written the
  4994. * inode out, but prune_icache isn't a user-visible syncing function.
  4995. * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
  4996. * we start and wait on commits.
  4997. *
  4998. * Is this efficient/effective? Well, we're being nice to the system
  4999. * by cleaning up our inodes proactively so they can be reaped
  5000. * without I/O. But we are potentially leaving up to five seconds'
  5001. * worth of inodes floating about which prune_icache wants us to
  5002. * write out. One way to fix that would be to get prune_icache()
  5003. * to do a write_super() to free up some memory. It has the desired
  5004. * effect.
  5005. */
  5006. int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
  5007. {
  5008. struct ext4_iloc iloc;
  5009. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  5010. static unsigned int mnt_count;
  5011. int err, ret;
  5012. might_sleep();
  5013. err = ext4_reserve_inode_write(handle, inode, &iloc);
  5014. if (ext4_handle_valid(handle) &&
  5015. EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
  5016. !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) {
  5017. /*
  5018. * We need extra buffer credits since we may write into EA block
  5019. * with this same handle. If journal_extend fails, then it will
  5020. * only result in a minor loss of functionality for that inode.
  5021. * If this is felt to be critical, then e2fsck should be run to
  5022. * force a large enough s_min_extra_isize.
  5023. */
  5024. if ((jbd2_journal_extend(handle,
  5025. EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
  5026. ret = ext4_expand_extra_isize(inode,
  5027. sbi->s_want_extra_isize,
  5028. iloc, handle);
  5029. if (ret) {
  5030. EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND;
  5031. if (mnt_count !=
  5032. le16_to_cpu(sbi->s_es->s_mnt_count)) {
  5033. ext4_warning(inode->i_sb, __func__,
  5034. "Unable to expand inode %lu. Delete"
  5035. " some EAs or run e2fsck.",
  5036. inode->i_ino);
  5037. mnt_count =
  5038. le16_to_cpu(sbi->s_es->s_mnt_count);
  5039. }
  5040. }
  5041. }
  5042. }
  5043. if (!err)
  5044. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  5045. return err;
  5046. }
  5047. /*
  5048. * ext4_dirty_inode() is called from __mark_inode_dirty()
  5049. *
  5050. * We're really interested in the case where a file is being extended.
  5051. * i_size has been changed by generic_commit_write() and we thus need
  5052. * to include the updated inode in the current transaction.
  5053. *
  5054. * Also, vfs_dq_alloc_block() will always dirty the inode when blocks
  5055. * are allocated to the file.
  5056. *
  5057. * If the inode is marked synchronous, we don't honour that here - doing
  5058. * so would cause a commit on atime updates, which we don't bother doing.
  5059. * We handle synchronous inodes at the highest possible level.
  5060. */
  5061. void ext4_dirty_inode(struct inode *inode)
  5062. {
  5063. handle_t *handle;
  5064. handle = ext4_journal_start(inode, 2);
  5065. if (IS_ERR(handle))
  5066. goto out;
  5067. ext4_mark_inode_dirty(handle, inode);
  5068. ext4_journal_stop(handle);
  5069. out:
  5070. return;
  5071. }
  5072. #if 0
  5073. /*
  5074. * Bind an inode's backing buffer_head into this transaction, to prevent
  5075. * it from being flushed to disk early. Unlike
  5076. * ext4_reserve_inode_write, this leaves behind no bh reference and
  5077. * returns no iloc structure, so the caller needs to repeat the iloc
  5078. * lookup to mark the inode dirty later.
  5079. */
  5080. static int ext4_pin_inode(handle_t *handle, struct inode *inode)
  5081. {
  5082. struct ext4_iloc iloc;
  5083. int err = 0;
  5084. if (handle) {
  5085. err = ext4_get_inode_loc(inode, &iloc);
  5086. if (!err) {
  5087. BUFFER_TRACE(iloc.bh, "get_write_access");
  5088. err = jbd2_journal_get_write_access(handle, iloc.bh);
  5089. if (!err)
  5090. err = ext4_handle_dirty_metadata(handle,
  5091. inode,
  5092. iloc.bh);
  5093. brelse(iloc.bh);
  5094. }
  5095. }
  5096. ext4_std_error(inode->i_sb, err);
  5097. return err;
  5098. }
  5099. #endif
  5100. int ext4_change_inode_journal_flag(struct inode *inode, int val)
  5101. {
  5102. journal_t *journal;
  5103. handle_t *handle;
  5104. int err;
  5105. /*
  5106. * We have to be very careful here: changing a data block's
  5107. * journaling status dynamically is dangerous. If we write a
  5108. * data block to the journal, change the status and then delete
  5109. * that block, we risk forgetting to revoke the old log record
  5110. * from the journal and so a subsequent replay can corrupt data.
  5111. * So, first we make sure that the journal is empty and that
  5112. * nobody is changing anything.
  5113. */
  5114. journal = EXT4_JOURNAL(inode);
  5115. if (!journal)
  5116. return 0;
  5117. if (is_journal_aborted(journal))
  5118. return -EROFS;
  5119. jbd2_journal_lock_updates(journal);
  5120. jbd2_journal_flush(journal);
  5121. /*
  5122. * OK, there are no updates running now, and all cached data is
  5123. * synced to disk. We are now in a completely consistent state
  5124. * which doesn't have anything in the journal, and we know that
  5125. * no filesystem updates are running, so it is safe to modify
  5126. * the inode's in-core data-journaling state flag now.
  5127. */
  5128. if (val)
  5129. EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL;
  5130. else
  5131. EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL;
  5132. ext4_set_aops(inode);
  5133. jbd2_journal_unlock_updates(journal);
  5134. /* Finally we can mark the inode as dirty. */
  5135. handle = ext4_journal_start(inode, 1);
  5136. if (IS_ERR(handle))
  5137. return PTR_ERR(handle);
  5138. err = ext4_mark_inode_dirty(handle, inode);
  5139. ext4_handle_sync(handle);
  5140. ext4_journal_stop(handle);
  5141. ext4_std_error(inode->i_sb, err);
  5142. return err;
  5143. }
  5144. static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
  5145. {
  5146. return !buffer_mapped(bh);
  5147. }
  5148. int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  5149. {
  5150. struct page *page = vmf->page;
  5151. loff_t size;
  5152. unsigned long len;
  5153. int ret = -EINVAL;
  5154. void *fsdata;
  5155. struct file *file = vma->vm_file;
  5156. struct inode *inode = file->f_path.dentry->d_inode;
  5157. struct address_space *mapping = inode->i_mapping;
  5158. /*
  5159. * Get i_alloc_sem to stop truncates messing with the inode. We cannot
  5160. * get i_mutex because we are already holding mmap_sem.
  5161. */
  5162. down_read(&inode->i_alloc_sem);
  5163. size = i_size_read(inode);
  5164. if (page->mapping != mapping || size <= page_offset(page)
  5165. || !PageUptodate(page)) {
  5166. /* page got truncated from under us? */
  5167. goto out_unlock;
  5168. }
  5169. ret = 0;
  5170. if (PageMappedToDisk(page))
  5171. goto out_unlock;
  5172. if (page->index == size >> PAGE_CACHE_SHIFT)
  5173. len = size & ~PAGE_CACHE_MASK;
  5174. else
  5175. len = PAGE_CACHE_SIZE;
  5176. lock_page(page);
  5177. /*
  5178. * return if we have all the buffers mapped. This avoid
  5179. * the need to call write_begin/write_end which does a
  5180. * journal_start/journal_stop which can block and take
  5181. * long time
  5182. */
  5183. if (page_has_buffers(page)) {
  5184. if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
  5185. ext4_bh_unmapped)) {
  5186. unlock_page(page);
  5187. goto out_unlock;
  5188. }
  5189. }
  5190. unlock_page(page);
  5191. /*
  5192. * OK, we need to fill the hole... Do write_begin write_end
  5193. * to do block allocation/reservation.We are not holding
  5194. * inode.i__mutex here. That allow * parallel write_begin,
  5195. * write_end call. lock_page prevent this from happening
  5196. * on the same page though
  5197. */
  5198. ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
  5199. len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
  5200. if (ret < 0)
  5201. goto out_unlock;
  5202. ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
  5203. len, len, page, fsdata);
  5204. if (ret < 0)
  5205. goto out_unlock;
  5206. ret = 0;
  5207. out_unlock:
  5208. if (ret)
  5209. ret = VM_FAULT_SIGBUS;
  5210. up_read(&inode->i_alloc_sem);
  5211. return ret;
  5212. }