inode.c 162 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/bio.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/file.h>
  22. #include <linux/fs.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/highmem.h>
  25. #include <linux/time.h>
  26. #include <linux/init.h>
  27. #include <linux/string.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/mpage.h>
  30. #include <linux/swap.h>
  31. #include <linux/writeback.h>
  32. #include <linux/statfs.h>
  33. #include <linux/compat.h>
  34. #include <linux/bit_spinlock.h>
  35. #include <linux/xattr.h>
  36. #include <linux/posix_acl.h>
  37. #include <linux/falloc.h>
  38. #include "compat.h"
  39. #include "ctree.h"
  40. #include "disk-io.h"
  41. #include "transaction.h"
  42. #include "btrfs_inode.h"
  43. #include "ioctl.h"
  44. #include "print-tree.h"
  45. #include "volumes.h"
  46. #include "ordered-data.h"
  47. #include "xattr.h"
  48. #include "tree-log.h"
  49. #include "compression.h"
  50. #include "locking.h"
  51. struct btrfs_iget_args {
  52. u64 ino;
  53. struct btrfs_root *root;
  54. };
  55. static const struct inode_operations btrfs_dir_inode_operations;
  56. static const struct inode_operations btrfs_symlink_inode_operations;
  57. static const struct inode_operations btrfs_dir_ro_inode_operations;
  58. static const struct inode_operations btrfs_special_inode_operations;
  59. static const struct inode_operations btrfs_file_inode_operations;
  60. static const struct address_space_operations btrfs_aops;
  61. static const struct address_space_operations btrfs_symlink_aops;
  62. static const struct file_operations btrfs_dir_file_operations;
  63. static struct extent_io_ops btrfs_extent_io_ops;
  64. static struct kmem_cache *btrfs_inode_cachep;
  65. struct kmem_cache *btrfs_trans_handle_cachep;
  66. struct kmem_cache *btrfs_transaction_cachep;
  67. struct kmem_cache *btrfs_path_cachep;
  68. #define S_SHIFT 12
  69. static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
  70. [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
  71. [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
  72. [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
  73. [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
  74. [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
  75. [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
  76. [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
  77. };
  78. static void btrfs_truncate(struct inode *inode);
  79. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
  80. static noinline int cow_file_range(struct inode *inode,
  81. struct page *locked_page,
  82. u64 start, u64 end, int *page_started,
  83. unsigned long *nr_written, int unlock);
  84. static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
  85. struct inode *inode, struct inode *dir)
  86. {
  87. int err;
  88. err = btrfs_init_acl(trans, inode, dir);
  89. if (!err)
  90. err = btrfs_xattr_security_init(trans, inode, dir);
  91. return err;
  92. }
  93. /*
  94. * this does all the hard work for inserting an inline extent into
  95. * the btree. The caller should have done a btrfs_drop_extents so that
  96. * no overlapping inline items exist in the btree
  97. */
  98. static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
  99. struct btrfs_root *root, struct inode *inode,
  100. u64 start, size_t size, size_t compressed_size,
  101. struct page **compressed_pages)
  102. {
  103. struct btrfs_key key;
  104. struct btrfs_path *path;
  105. struct extent_buffer *leaf;
  106. struct page *page = NULL;
  107. char *kaddr;
  108. unsigned long ptr;
  109. struct btrfs_file_extent_item *ei;
  110. int err = 0;
  111. int ret;
  112. size_t cur_size = size;
  113. size_t datasize;
  114. unsigned long offset;
  115. int use_compress = 0;
  116. if (compressed_size && compressed_pages) {
  117. use_compress = 1;
  118. cur_size = compressed_size;
  119. }
  120. path = btrfs_alloc_path();
  121. if (!path)
  122. return -ENOMEM;
  123. path->leave_spinning = 1;
  124. btrfs_set_trans_block_group(trans, inode);
  125. key.objectid = inode->i_ino;
  126. key.offset = start;
  127. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  128. datasize = btrfs_file_extent_calc_inline_size(cur_size);
  129. inode_add_bytes(inode, size);
  130. ret = btrfs_insert_empty_item(trans, root, path, &key,
  131. datasize);
  132. BUG_ON(ret);
  133. if (ret) {
  134. err = ret;
  135. goto fail;
  136. }
  137. leaf = path->nodes[0];
  138. ei = btrfs_item_ptr(leaf, path->slots[0],
  139. struct btrfs_file_extent_item);
  140. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  141. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  142. btrfs_set_file_extent_encryption(leaf, ei, 0);
  143. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  144. btrfs_set_file_extent_ram_bytes(leaf, ei, size);
  145. ptr = btrfs_file_extent_inline_start(ei);
  146. if (use_compress) {
  147. struct page *cpage;
  148. int i = 0;
  149. while (compressed_size > 0) {
  150. cpage = compressed_pages[i];
  151. cur_size = min_t(unsigned long, compressed_size,
  152. PAGE_CACHE_SIZE);
  153. kaddr = kmap_atomic(cpage, KM_USER0);
  154. write_extent_buffer(leaf, kaddr, ptr, cur_size);
  155. kunmap_atomic(kaddr, KM_USER0);
  156. i++;
  157. ptr += cur_size;
  158. compressed_size -= cur_size;
  159. }
  160. btrfs_set_file_extent_compression(leaf, ei,
  161. BTRFS_COMPRESS_ZLIB);
  162. } else {
  163. page = find_get_page(inode->i_mapping,
  164. start >> PAGE_CACHE_SHIFT);
  165. btrfs_set_file_extent_compression(leaf, ei, 0);
  166. kaddr = kmap_atomic(page, KM_USER0);
  167. offset = start & (PAGE_CACHE_SIZE - 1);
  168. write_extent_buffer(leaf, kaddr + offset, ptr, size);
  169. kunmap_atomic(kaddr, KM_USER0);
  170. page_cache_release(page);
  171. }
  172. btrfs_mark_buffer_dirty(leaf);
  173. btrfs_free_path(path);
  174. /*
  175. * we're an inline extent, so nobody can
  176. * extend the file past i_size without locking
  177. * a page we already have locked.
  178. *
  179. * We must do any isize and inode updates
  180. * before we unlock the pages. Otherwise we
  181. * could end up racing with unlink.
  182. */
  183. BTRFS_I(inode)->disk_i_size = inode->i_size;
  184. btrfs_update_inode(trans, root, inode);
  185. return 0;
  186. fail:
  187. btrfs_free_path(path);
  188. return err;
  189. }
  190. /*
  191. * conditionally insert an inline extent into the file. This
  192. * does the checks required to make sure the data is small enough
  193. * to fit as an inline extent.
  194. */
  195. static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
  196. struct btrfs_root *root,
  197. struct inode *inode, u64 start, u64 end,
  198. size_t compressed_size,
  199. struct page **compressed_pages)
  200. {
  201. u64 isize = i_size_read(inode);
  202. u64 actual_end = min(end + 1, isize);
  203. u64 inline_len = actual_end - start;
  204. u64 aligned_end = (end + root->sectorsize - 1) &
  205. ~((u64)root->sectorsize - 1);
  206. u64 hint_byte;
  207. u64 data_len = inline_len;
  208. int ret;
  209. if (compressed_size)
  210. data_len = compressed_size;
  211. if (start > 0 ||
  212. actual_end >= PAGE_CACHE_SIZE ||
  213. data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
  214. (!compressed_size &&
  215. (actual_end & (root->sectorsize - 1)) == 0) ||
  216. end + 1 < isize ||
  217. data_len > root->fs_info->max_inline) {
  218. return 1;
  219. }
  220. ret = btrfs_drop_extents(trans, inode, start, aligned_end,
  221. &hint_byte, 1);
  222. BUG_ON(ret);
  223. if (isize > actual_end)
  224. inline_len = min_t(u64, isize, actual_end);
  225. ret = insert_inline_extent(trans, root, inode, start,
  226. inline_len, compressed_size,
  227. compressed_pages);
  228. BUG_ON(ret);
  229. btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
  230. return 0;
  231. }
  232. struct async_extent {
  233. u64 start;
  234. u64 ram_size;
  235. u64 compressed_size;
  236. struct page **pages;
  237. unsigned long nr_pages;
  238. struct list_head list;
  239. };
  240. struct async_cow {
  241. struct inode *inode;
  242. struct btrfs_root *root;
  243. struct page *locked_page;
  244. u64 start;
  245. u64 end;
  246. struct list_head extents;
  247. struct btrfs_work work;
  248. };
  249. static noinline int add_async_extent(struct async_cow *cow,
  250. u64 start, u64 ram_size,
  251. u64 compressed_size,
  252. struct page **pages,
  253. unsigned long nr_pages)
  254. {
  255. struct async_extent *async_extent;
  256. async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
  257. async_extent->start = start;
  258. async_extent->ram_size = ram_size;
  259. async_extent->compressed_size = compressed_size;
  260. async_extent->pages = pages;
  261. async_extent->nr_pages = nr_pages;
  262. list_add_tail(&async_extent->list, &cow->extents);
  263. return 0;
  264. }
  265. /*
  266. * we create compressed extents in two phases. The first
  267. * phase compresses a range of pages that have already been
  268. * locked (both pages and state bits are locked).
  269. *
  270. * This is done inside an ordered work queue, and the compression
  271. * is spread across many cpus. The actual IO submission is step
  272. * two, and the ordered work queue takes care of making sure that
  273. * happens in the same order things were put onto the queue by
  274. * writepages and friends.
  275. *
  276. * If this code finds it can't get good compression, it puts an
  277. * entry onto the work queue to write the uncompressed bytes. This
  278. * makes sure that both compressed inodes and uncompressed inodes
  279. * are written in the same order that pdflush sent them down.
  280. */
  281. static noinline int compress_file_range(struct inode *inode,
  282. struct page *locked_page,
  283. u64 start, u64 end,
  284. struct async_cow *async_cow,
  285. int *num_added)
  286. {
  287. struct btrfs_root *root = BTRFS_I(inode)->root;
  288. struct btrfs_trans_handle *trans;
  289. u64 num_bytes;
  290. u64 orig_start;
  291. u64 disk_num_bytes;
  292. u64 blocksize = root->sectorsize;
  293. u64 actual_end;
  294. u64 isize = i_size_read(inode);
  295. int ret = 0;
  296. struct page **pages = NULL;
  297. unsigned long nr_pages;
  298. unsigned long nr_pages_ret = 0;
  299. unsigned long total_compressed = 0;
  300. unsigned long total_in = 0;
  301. unsigned long max_compressed = 128 * 1024;
  302. unsigned long max_uncompressed = 128 * 1024;
  303. int i;
  304. int will_compress;
  305. orig_start = start;
  306. actual_end = min_t(u64, isize, end + 1);
  307. again:
  308. will_compress = 0;
  309. nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
  310. nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
  311. /*
  312. * we don't want to send crud past the end of i_size through
  313. * compression, that's just a waste of CPU time. So, if the
  314. * end of the file is before the start of our current
  315. * requested range of bytes, we bail out to the uncompressed
  316. * cleanup code that can deal with all of this.
  317. *
  318. * It isn't really the fastest way to fix things, but this is a
  319. * very uncommon corner.
  320. */
  321. if (actual_end <= start)
  322. goto cleanup_and_bail_uncompressed;
  323. total_compressed = actual_end - start;
  324. /* we want to make sure that amount of ram required to uncompress
  325. * an extent is reasonable, so we limit the total size in ram
  326. * of a compressed extent to 128k. This is a crucial number
  327. * because it also controls how easily we can spread reads across
  328. * cpus for decompression.
  329. *
  330. * We also want to make sure the amount of IO required to do
  331. * a random read is reasonably small, so we limit the size of
  332. * a compressed extent to 128k.
  333. */
  334. total_compressed = min(total_compressed, max_uncompressed);
  335. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  336. num_bytes = max(blocksize, num_bytes);
  337. disk_num_bytes = num_bytes;
  338. total_in = 0;
  339. ret = 0;
  340. /*
  341. * we do compression for mount -o compress and when the
  342. * inode has not been flagged as nocompress. This flag can
  343. * change at any time if we discover bad compression ratios.
  344. */
  345. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
  346. btrfs_test_opt(root, COMPRESS)) {
  347. WARN_ON(pages);
  348. pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
  349. ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
  350. total_compressed, pages,
  351. nr_pages, &nr_pages_ret,
  352. &total_in,
  353. &total_compressed,
  354. max_compressed);
  355. if (!ret) {
  356. unsigned long offset = total_compressed &
  357. (PAGE_CACHE_SIZE - 1);
  358. struct page *page = pages[nr_pages_ret - 1];
  359. char *kaddr;
  360. /* zero the tail end of the last page, we might be
  361. * sending it down to disk
  362. */
  363. if (offset) {
  364. kaddr = kmap_atomic(page, KM_USER0);
  365. memset(kaddr + offset, 0,
  366. PAGE_CACHE_SIZE - offset);
  367. kunmap_atomic(kaddr, KM_USER0);
  368. }
  369. will_compress = 1;
  370. }
  371. }
  372. if (start == 0) {
  373. trans = btrfs_join_transaction(root, 1);
  374. BUG_ON(!trans);
  375. btrfs_set_trans_block_group(trans, inode);
  376. /* lets try to make an inline extent */
  377. if (ret || total_in < (actual_end - start)) {
  378. /* we didn't compress the entire range, try
  379. * to make an uncompressed inline extent.
  380. */
  381. ret = cow_file_range_inline(trans, root, inode,
  382. start, end, 0, NULL);
  383. } else {
  384. /* try making a compressed inline extent */
  385. ret = cow_file_range_inline(trans, root, inode,
  386. start, end,
  387. total_compressed, pages);
  388. }
  389. if (ret == 0) {
  390. /*
  391. * inline extent creation worked, we don't need
  392. * to create any more async work items. Unlock
  393. * and free up our temp pages.
  394. */
  395. extent_clear_unlock_delalloc(inode,
  396. &BTRFS_I(inode)->io_tree,
  397. start, end, NULL,
  398. EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
  399. EXTENT_CLEAR_DELALLOC |
  400. EXTENT_CLEAR_ACCOUNTING |
  401. EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
  402. btrfs_end_transaction(trans, root);
  403. goto free_pages_out;
  404. }
  405. btrfs_end_transaction(trans, root);
  406. }
  407. if (will_compress) {
  408. /*
  409. * we aren't doing an inline extent round the compressed size
  410. * up to a block size boundary so the allocator does sane
  411. * things
  412. */
  413. total_compressed = (total_compressed + blocksize - 1) &
  414. ~(blocksize - 1);
  415. /*
  416. * one last check to make sure the compression is really a
  417. * win, compare the page count read with the blocks on disk
  418. */
  419. total_in = (total_in + PAGE_CACHE_SIZE - 1) &
  420. ~(PAGE_CACHE_SIZE - 1);
  421. if (total_compressed >= total_in) {
  422. will_compress = 0;
  423. } else {
  424. disk_num_bytes = total_compressed;
  425. num_bytes = total_in;
  426. }
  427. }
  428. if (!will_compress && pages) {
  429. /*
  430. * the compression code ran but failed to make things smaller,
  431. * free any pages it allocated and our page pointer array
  432. */
  433. for (i = 0; i < nr_pages_ret; i++) {
  434. WARN_ON(pages[i]->mapping);
  435. page_cache_release(pages[i]);
  436. }
  437. kfree(pages);
  438. pages = NULL;
  439. total_compressed = 0;
  440. nr_pages_ret = 0;
  441. /* flag the file so we don't compress in the future */
  442. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  443. }
  444. if (will_compress) {
  445. *num_added += 1;
  446. /* the async work queues will take care of doing actual
  447. * allocation on disk for these compressed pages,
  448. * and will submit them to the elevator.
  449. */
  450. add_async_extent(async_cow, start, num_bytes,
  451. total_compressed, pages, nr_pages_ret);
  452. if (start + num_bytes < end && start + num_bytes < actual_end) {
  453. start += num_bytes;
  454. pages = NULL;
  455. cond_resched();
  456. goto again;
  457. }
  458. } else {
  459. cleanup_and_bail_uncompressed:
  460. /*
  461. * No compression, but we still need to write the pages in
  462. * the file we've been given so far. redirty the locked
  463. * page if it corresponds to our extent and set things up
  464. * for the async work queue to run cow_file_range to do
  465. * the normal delalloc dance
  466. */
  467. if (page_offset(locked_page) >= start &&
  468. page_offset(locked_page) <= end) {
  469. __set_page_dirty_nobuffers(locked_page);
  470. /* unlocked later on in the async handlers */
  471. }
  472. add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
  473. *num_added += 1;
  474. }
  475. out:
  476. return 0;
  477. free_pages_out:
  478. for (i = 0; i < nr_pages_ret; i++) {
  479. WARN_ON(pages[i]->mapping);
  480. page_cache_release(pages[i]);
  481. }
  482. kfree(pages);
  483. goto out;
  484. }
  485. /*
  486. * phase two of compressed writeback. This is the ordered portion
  487. * of the code, which only gets called in the order the work was
  488. * queued. We walk all the async extents created by compress_file_range
  489. * and send them down to the disk.
  490. */
  491. static noinline int submit_compressed_extents(struct inode *inode,
  492. struct async_cow *async_cow)
  493. {
  494. struct async_extent *async_extent;
  495. u64 alloc_hint = 0;
  496. struct btrfs_trans_handle *trans;
  497. struct btrfs_key ins;
  498. struct extent_map *em;
  499. struct btrfs_root *root = BTRFS_I(inode)->root;
  500. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  501. struct extent_io_tree *io_tree;
  502. int ret = 0;
  503. if (list_empty(&async_cow->extents))
  504. return 0;
  505. while (!list_empty(&async_cow->extents)) {
  506. async_extent = list_entry(async_cow->extents.next,
  507. struct async_extent, list);
  508. list_del(&async_extent->list);
  509. io_tree = &BTRFS_I(inode)->io_tree;
  510. retry:
  511. /* did the compression code fall back to uncompressed IO? */
  512. if (!async_extent->pages) {
  513. int page_started = 0;
  514. unsigned long nr_written = 0;
  515. lock_extent(io_tree, async_extent->start,
  516. async_extent->start +
  517. async_extent->ram_size - 1, GFP_NOFS);
  518. /* allocate blocks */
  519. ret = cow_file_range(inode, async_cow->locked_page,
  520. async_extent->start,
  521. async_extent->start +
  522. async_extent->ram_size - 1,
  523. &page_started, &nr_written, 0);
  524. /*
  525. * if page_started, cow_file_range inserted an
  526. * inline extent and took care of all the unlocking
  527. * and IO for us. Otherwise, we need to submit
  528. * all those pages down to the drive.
  529. */
  530. if (!page_started && !ret)
  531. extent_write_locked_range(io_tree,
  532. inode, async_extent->start,
  533. async_extent->start +
  534. async_extent->ram_size - 1,
  535. btrfs_get_extent,
  536. WB_SYNC_ALL);
  537. kfree(async_extent);
  538. cond_resched();
  539. continue;
  540. }
  541. lock_extent(io_tree, async_extent->start,
  542. async_extent->start + async_extent->ram_size - 1,
  543. GFP_NOFS);
  544. trans = btrfs_join_transaction(root, 1);
  545. ret = btrfs_reserve_extent(trans, root,
  546. async_extent->compressed_size,
  547. async_extent->compressed_size,
  548. 0, alloc_hint,
  549. (u64)-1, &ins, 1);
  550. btrfs_end_transaction(trans, root);
  551. if (ret) {
  552. int i;
  553. for (i = 0; i < async_extent->nr_pages; i++) {
  554. WARN_ON(async_extent->pages[i]->mapping);
  555. page_cache_release(async_extent->pages[i]);
  556. }
  557. kfree(async_extent->pages);
  558. async_extent->nr_pages = 0;
  559. async_extent->pages = NULL;
  560. unlock_extent(io_tree, async_extent->start,
  561. async_extent->start +
  562. async_extent->ram_size - 1, GFP_NOFS);
  563. goto retry;
  564. }
  565. /*
  566. * here we're doing allocation and writeback of the
  567. * compressed pages
  568. */
  569. btrfs_drop_extent_cache(inode, async_extent->start,
  570. async_extent->start +
  571. async_extent->ram_size - 1, 0);
  572. em = alloc_extent_map(GFP_NOFS);
  573. em->start = async_extent->start;
  574. em->len = async_extent->ram_size;
  575. em->orig_start = em->start;
  576. em->block_start = ins.objectid;
  577. em->block_len = ins.offset;
  578. em->bdev = root->fs_info->fs_devices->latest_bdev;
  579. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  580. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  581. while (1) {
  582. write_lock(&em_tree->lock);
  583. ret = add_extent_mapping(em_tree, em);
  584. write_unlock(&em_tree->lock);
  585. if (ret != -EEXIST) {
  586. free_extent_map(em);
  587. break;
  588. }
  589. btrfs_drop_extent_cache(inode, async_extent->start,
  590. async_extent->start +
  591. async_extent->ram_size - 1, 0);
  592. }
  593. ret = btrfs_add_ordered_extent(inode, async_extent->start,
  594. ins.objectid,
  595. async_extent->ram_size,
  596. ins.offset,
  597. BTRFS_ORDERED_COMPRESSED);
  598. BUG_ON(ret);
  599. /*
  600. * clear dirty, set writeback and unlock the pages.
  601. */
  602. extent_clear_unlock_delalloc(inode,
  603. &BTRFS_I(inode)->io_tree,
  604. async_extent->start,
  605. async_extent->start +
  606. async_extent->ram_size - 1,
  607. NULL, EXTENT_CLEAR_UNLOCK_PAGE |
  608. EXTENT_CLEAR_UNLOCK |
  609. EXTENT_CLEAR_DELALLOC |
  610. EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
  611. ret = btrfs_submit_compressed_write(inode,
  612. async_extent->start,
  613. async_extent->ram_size,
  614. ins.objectid,
  615. ins.offset, async_extent->pages,
  616. async_extent->nr_pages);
  617. BUG_ON(ret);
  618. alloc_hint = ins.objectid + ins.offset;
  619. kfree(async_extent);
  620. cond_resched();
  621. }
  622. return 0;
  623. }
  624. /*
  625. * when extent_io.c finds a delayed allocation range in the file,
  626. * the call backs end up in this code. The basic idea is to
  627. * allocate extents on disk for the range, and create ordered data structs
  628. * in ram to track those extents.
  629. *
  630. * locked_page is the page that writepage had locked already. We use
  631. * it to make sure we don't do extra locks or unlocks.
  632. *
  633. * *page_started is set to one if we unlock locked_page and do everything
  634. * required to start IO on it. It may be clean and already done with
  635. * IO when we return.
  636. */
  637. static noinline int cow_file_range(struct inode *inode,
  638. struct page *locked_page,
  639. u64 start, u64 end, int *page_started,
  640. unsigned long *nr_written,
  641. int unlock)
  642. {
  643. struct btrfs_root *root = BTRFS_I(inode)->root;
  644. struct btrfs_trans_handle *trans;
  645. u64 alloc_hint = 0;
  646. u64 num_bytes;
  647. unsigned long ram_size;
  648. u64 disk_num_bytes;
  649. u64 cur_alloc_size;
  650. u64 blocksize = root->sectorsize;
  651. u64 actual_end;
  652. u64 isize = i_size_read(inode);
  653. struct btrfs_key ins;
  654. struct extent_map *em;
  655. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  656. int ret = 0;
  657. trans = btrfs_join_transaction(root, 1);
  658. BUG_ON(!trans);
  659. btrfs_set_trans_block_group(trans, inode);
  660. actual_end = min_t(u64, isize, end + 1);
  661. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  662. num_bytes = max(blocksize, num_bytes);
  663. disk_num_bytes = num_bytes;
  664. ret = 0;
  665. if (start == 0) {
  666. /* lets try to make an inline extent */
  667. ret = cow_file_range_inline(trans, root, inode,
  668. start, end, 0, NULL);
  669. if (ret == 0) {
  670. extent_clear_unlock_delalloc(inode,
  671. &BTRFS_I(inode)->io_tree,
  672. start, end, NULL,
  673. EXTENT_CLEAR_UNLOCK_PAGE |
  674. EXTENT_CLEAR_UNLOCK |
  675. EXTENT_CLEAR_DELALLOC |
  676. EXTENT_CLEAR_ACCOUNTING |
  677. EXTENT_CLEAR_DIRTY |
  678. EXTENT_SET_WRITEBACK |
  679. EXTENT_END_WRITEBACK);
  680. *nr_written = *nr_written +
  681. (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
  682. *page_started = 1;
  683. ret = 0;
  684. goto out;
  685. }
  686. }
  687. BUG_ON(disk_num_bytes >
  688. btrfs_super_total_bytes(&root->fs_info->super_copy));
  689. read_lock(&BTRFS_I(inode)->extent_tree.lock);
  690. em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
  691. start, num_bytes);
  692. if (em) {
  693. /*
  694. * if block start isn't an actual block number then find the
  695. * first block in this inode and use that as a hint. If that
  696. * block is also bogus then just don't worry about it.
  697. */
  698. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  699. free_extent_map(em);
  700. em = search_extent_mapping(em_tree, 0, 0);
  701. if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
  702. alloc_hint = em->block_start;
  703. if (em)
  704. free_extent_map(em);
  705. } else {
  706. alloc_hint = em->block_start;
  707. free_extent_map(em);
  708. }
  709. }
  710. read_unlock(&BTRFS_I(inode)->extent_tree.lock);
  711. btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
  712. while (disk_num_bytes > 0) {
  713. unsigned long op;
  714. cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
  715. ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
  716. root->sectorsize, 0, alloc_hint,
  717. (u64)-1, &ins, 1);
  718. BUG_ON(ret);
  719. em = alloc_extent_map(GFP_NOFS);
  720. em->start = start;
  721. em->orig_start = em->start;
  722. ram_size = ins.offset;
  723. em->len = ins.offset;
  724. em->block_start = ins.objectid;
  725. em->block_len = ins.offset;
  726. em->bdev = root->fs_info->fs_devices->latest_bdev;
  727. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  728. while (1) {
  729. write_lock(&em_tree->lock);
  730. ret = add_extent_mapping(em_tree, em);
  731. write_unlock(&em_tree->lock);
  732. if (ret != -EEXIST) {
  733. free_extent_map(em);
  734. break;
  735. }
  736. btrfs_drop_extent_cache(inode, start,
  737. start + ram_size - 1, 0);
  738. }
  739. cur_alloc_size = ins.offset;
  740. ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
  741. ram_size, cur_alloc_size, 0);
  742. BUG_ON(ret);
  743. if (root->root_key.objectid ==
  744. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  745. ret = btrfs_reloc_clone_csums(inode, start,
  746. cur_alloc_size);
  747. BUG_ON(ret);
  748. }
  749. if (disk_num_bytes < cur_alloc_size)
  750. break;
  751. /* we're not doing compressed IO, don't unlock the first
  752. * page (which the caller expects to stay locked), don't
  753. * clear any dirty bits and don't set any writeback bits
  754. *
  755. * Do set the Private2 bit so we know this page was properly
  756. * setup for writepage
  757. */
  758. op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
  759. op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
  760. EXTENT_SET_PRIVATE2;
  761. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  762. start, start + ram_size - 1,
  763. locked_page, op);
  764. disk_num_bytes -= cur_alloc_size;
  765. num_bytes -= cur_alloc_size;
  766. alloc_hint = ins.objectid + ins.offset;
  767. start += cur_alloc_size;
  768. }
  769. out:
  770. ret = 0;
  771. btrfs_end_transaction(trans, root);
  772. return ret;
  773. }
  774. /*
  775. * work queue call back to started compression on a file and pages
  776. */
  777. static noinline void async_cow_start(struct btrfs_work *work)
  778. {
  779. struct async_cow *async_cow;
  780. int num_added = 0;
  781. async_cow = container_of(work, struct async_cow, work);
  782. compress_file_range(async_cow->inode, async_cow->locked_page,
  783. async_cow->start, async_cow->end, async_cow,
  784. &num_added);
  785. if (num_added == 0)
  786. async_cow->inode = NULL;
  787. }
  788. /*
  789. * work queue call back to submit previously compressed pages
  790. */
  791. static noinline void async_cow_submit(struct btrfs_work *work)
  792. {
  793. struct async_cow *async_cow;
  794. struct btrfs_root *root;
  795. unsigned long nr_pages;
  796. async_cow = container_of(work, struct async_cow, work);
  797. root = async_cow->root;
  798. nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
  799. PAGE_CACHE_SHIFT;
  800. atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
  801. if (atomic_read(&root->fs_info->async_delalloc_pages) <
  802. 5 * 1042 * 1024 &&
  803. waitqueue_active(&root->fs_info->async_submit_wait))
  804. wake_up(&root->fs_info->async_submit_wait);
  805. if (async_cow->inode)
  806. submit_compressed_extents(async_cow->inode, async_cow);
  807. }
  808. static noinline void async_cow_free(struct btrfs_work *work)
  809. {
  810. struct async_cow *async_cow;
  811. async_cow = container_of(work, struct async_cow, work);
  812. kfree(async_cow);
  813. }
  814. static int cow_file_range_async(struct inode *inode, struct page *locked_page,
  815. u64 start, u64 end, int *page_started,
  816. unsigned long *nr_written)
  817. {
  818. struct async_cow *async_cow;
  819. struct btrfs_root *root = BTRFS_I(inode)->root;
  820. unsigned long nr_pages;
  821. u64 cur_end;
  822. int limit = 10 * 1024 * 1042;
  823. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
  824. 1, 0, NULL, GFP_NOFS);
  825. while (start < end) {
  826. async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
  827. async_cow->inode = inode;
  828. async_cow->root = root;
  829. async_cow->locked_page = locked_page;
  830. async_cow->start = start;
  831. if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
  832. cur_end = end;
  833. else
  834. cur_end = min(end, start + 512 * 1024 - 1);
  835. async_cow->end = cur_end;
  836. INIT_LIST_HEAD(&async_cow->extents);
  837. async_cow->work.func = async_cow_start;
  838. async_cow->work.ordered_func = async_cow_submit;
  839. async_cow->work.ordered_free = async_cow_free;
  840. async_cow->work.flags = 0;
  841. nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
  842. PAGE_CACHE_SHIFT;
  843. atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
  844. btrfs_queue_worker(&root->fs_info->delalloc_workers,
  845. &async_cow->work);
  846. if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
  847. wait_event(root->fs_info->async_submit_wait,
  848. (atomic_read(&root->fs_info->async_delalloc_pages) <
  849. limit));
  850. }
  851. while (atomic_read(&root->fs_info->async_submit_draining) &&
  852. atomic_read(&root->fs_info->async_delalloc_pages)) {
  853. wait_event(root->fs_info->async_submit_wait,
  854. (atomic_read(&root->fs_info->async_delalloc_pages) ==
  855. 0));
  856. }
  857. *nr_written += nr_pages;
  858. start = cur_end + 1;
  859. }
  860. *page_started = 1;
  861. return 0;
  862. }
  863. static noinline int csum_exist_in_range(struct btrfs_root *root,
  864. u64 bytenr, u64 num_bytes)
  865. {
  866. int ret;
  867. struct btrfs_ordered_sum *sums;
  868. LIST_HEAD(list);
  869. ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
  870. bytenr + num_bytes - 1, &list);
  871. if (ret == 0 && list_empty(&list))
  872. return 0;
  873. while (!list_empty(&list)) {
  874. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  875. list_del(&sums->list);
  876. kfree(sums);
  877. }
  878. return 1;
  879. }
  880. /*
  881. * when nowcow writeback call back. This checks for snapshots or COW copies
  882. * of the extents that exist in the file, and COWs the file as required.
  883. *
  884. * If no cow copies or snapshots exist, we write directly to the existing
  885. * blocks on disk
  886. */
  887. static noinline int run_delalloc_nocow(struct inode *inode,
  888. struct page *locked_page,
  889. u64 start, u64 end, int *page_started, int force,
  890. unsigned long *nr_written)
  891. {
  892. struct btrfs_root *root = BTRFS_I(inode)->root;
  893. struct btrfs_trans_handle *trans;
  894. struct extent_buffer *leaf;
  895. struct btrfs_path *path;
  896. struct btrfs_file_extent_item *fi;
  897. struct btrfs_key found_key;
  898. u64 cow_start;
  899. u64 cur_offset;
  900. u64 extent_end;
  901. u64 extent_offset;
  902. u64 disk_bytenr;
  903. u64 num_bytes;
  904. int extent_type;
  905. int ret;
  906. int type;
  907. int nocow;
  908. int check_prev = 1;
  909. path = btrfs_alloc_path();
  910. BUG_ON(!path);
  911. trans = btrfs_join_transaction(root, 1);
  912. BUG_ON(!trans);
  913. cow_start = (u64)-1;
  914. cur_offset = start;
  915. while (1) {
  916. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  917. cur_offset, 0);
  918. BUG_ON(ret < 0);
  919. if (ret > 0 && path->slots[0] > 0 && check_prev) {
  920. leaf = path->nodes[0];
  921. btrfs_item_key_to_cpu(leaf, &found_key,
  922. path->slots[0] - 1);
  923. if (found_key.objectid == inode->i_ino &&
  924. found_key.type == BTRFS_EXTENT_DATA_KEY)
  925. path->slots[0]--;
  926. }
  927. check_prev = 0;
  928. next_slot:
  929. leaf = path->nodes[0];
  930. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  931. ret = btrfs_next_leaf(root, path);
  932. if (ret < 0)
  933. BUG_ON(1);
  934. if (ret > 0)
  935. break;
  936. leaf = path->nodes[0];
  937. }
  938. nocow = 0;
  939. disk_bytenr = 0;
  940. num_bytes = 0;
  941. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  942. if (found_key.objectid > inode->i_ino ||
  943. found_key.type > BTRFS_EXTENT_DATA_KEY ||
  944. found_key.offset > end)
  945. break;
  946. if (found_key.offset > cur_offset) {
  947. extent_end = found_key.offset;
  948. extent_type = 0;
  949. goto out_check;
  950. }
  951. fi = btrfs_item_ptr(leaf, path->slots[0],
  952. struct btrfs_file_extent_item);
  953. extent_type = btrfs_file_extent_type(leaf, fi);
  954. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  955. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  956. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  957. extent_offset = btrfs_file_extent_offset(leaf, fi);
  958. extent_end = found_key.offset +
  959. btrfs_file_extent_num_bytes(leaf, fi);
  960. if (extent_end <= start) {
  961. path->slots[0]++;
  962. goto next_slot;
  963. }
  964. if (disk_bytenr == 0)
  965. goto out_check;
  966. if (btrfs_file_extent_compression(leaf, fi) ||
  967. btrfs_file_extent_encryption(leaf, fi) ||
  968. btrfs_file_extent_other_encoding(leaf, fi))
  969. goto out_check;
  970. if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
  971. goto out_check;
  972. if (btrfs_extent_readonly(root, disk_bytenr))
  973. goto out_check;
  974. if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
  975. found_key.offset -
  976. extent_offset, disk_bytenr))
  977. goto out_check;
  978. disk_bytenr += extent_offset;
  979. disk_bytenr += cur_offset - found_key.offset;
  980. num_bytes = min(end + 1, extent_end) - cur_offset;
  981. /*
  982. * force cow if csum exists in the range.
  983. * this ensure that csum for a given extent are
  984. * either valid or do not exist.
  985. */
  986. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  987. goto out_check;
  988. nocow = 1;
  989. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  990. extent_end = found_key.offset +
  991. btrfs_file_extent_inline_len(leaf, fi);
  992. extent_end = ALIGN(extent_end, root->sectorsize);
  993. } else {
  994. BUG_ON(1);
  995. }
  996. out_check:
  997. if (extent_end <= start) {
  998. path->slots[0]++;
  999. goto next_slot;
  1000. }
  1001. if (!nocow) {
  1002. if (cow_start == (u64)-1)
  1003. cow_start = cur_offset;
  1004. cur_offset = extent_end;
  1005. if (cur_offset > end)
  1006. break;
  1007. path->slots[0]++;
  1008. goto next_slot;
  1009. }
  1010. btrfs_release_path(root, path);
  1011. if (cow_start != (u64)-1) {
  1012. ret = cow_file_range(inode, locked_page, cow_start,
  1013. found_key.offset - 1, page_started,
  1014. nr_written, 1);
  1015. BUG_ON(ret);
  1016. cow_start = (u64)-1;
  1017. }
  1018. if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  1019. struct extent_map *em;
  1020. struct extent_map_tree *em_tree;
  1021. em_tree = &BTRFS_I(inode)->extent_tree;
  1022. em = alloc_extent_map(GFP_NOFS);
  1023. em->start = cur_offset;
  1024. em->orig_start = em->start;
  1025. em->len = num_bytes;
  1026. em->block_len = num_bytes;
  1027. em->block_start = disk_bytenr;
  1028. em->bdev = root->fs_info->fs_devices->latest_bdev;
  1029. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  1030. while (1) {
  1031. write_lock(&em_tree->lock);
  1032. ret = add_extent_mapping(em_tree, em);
  1033. write_unlock(&em_tree->lock);
  1034. if (ret != -EEXIST) {
  1035. free_extent_map(em);
  1036. break;
  1037. }
  1038. btrfs_drop_extent_cache(inode, em->start,
  1039. em->start + em->len - 1, 0);
  1040. }
  1041. type = BTRFS_ORDERED_PREALLOC;
  1042. } else {
  1043. type = BTRFS_ORDERED_NOCOW;
  1044. }
  1045. ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
  1046. num_bytes, num_bytes, type);
  1047. BUG_ON(ret);
  1048. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  1049. cur_offset, cur_offset + num_bytes - 1,
  1050. locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
  1051. EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
  1052. EXTENT_SET_PRIVATE2);
  1053. cur_offset = extent_end;
  1054. if (cur_offset > end)
  1055. break;
  1056. }
  1057. btrfs_release_path(root, path);
  1058. if (cur_offset <= end && cow_start == (u64)-1)
  1059. cow_start = cur_offset;
  1060. if (cow_start != (u64)-1) {
  1061. ret = cow_file_range(inode, locked_page, cow_start, end,
  1062. page_started, nr_written, 1);
  1063. BUG_ON(ret);
  1064. }
  1065. ret = btrfs_end_transaction(trans, root);
  1066. BUG_ON(ret);
  1067. btrfs_free_path(path);
  1068. return 0;
  1069. }
  1070. /*
  1071. * extent_io.c call back to do delayed allocation processing
  1072. */
  1073. static int run_delalloc_range(struct inode *inode, struct page *locked_page,
  1074. u64 start, u64 end, int *page_started,
  1075. unsigned long *nr_written)
  1076. {
  1077. int ret;
  1078. struct btrfs_root *root = BTRFS_I(inode)->root;
  1079. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
  1080. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1081. page_started, 1, nr_written);
  1082. else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
  1083. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1084. page_started, 0, nr_written);
  1085. else if (!btrfs_test_opt(root, COMPRESS))
  1086. ret = cow_file_range(inode, locked_page, start, end,
  1087. page_started, nr_written, 1);
  1088. else
  1089. ret = cow_file_range_async(inode, locked_page, start, end,
  1090. page_started, nr_written);
  1091. return ret;
  1092. }
  1093. static int btrfs_split_extent_hook(struct inode *inode,
  1094. struct extent_state *orig, u64 split)
  1095. {
  1096. struct btrfs_root *root = BTRFS_I(inode)->root;
  1097. u64 size;
  1098. if (!(orig->state & EXTENT_DELALLOC))
  1099. return 0;
  1100. size = orig->end - orig->start + 1;
  1101. if (size > root->fs_info->max_extent) {
  1102. u64 num_extents;
  1103. u64 new_size;
  1104. new_size = orig->end - split + 1;
  1105. num_extents = div64_u64(size + root->fs_info->max_extent - 1,
  1106. root->fs_info->max_extent);
  1107. /*
  1108. * if we break a large extent up then leave oustanding_extents
  1109. * be, since we've already accounted for the large extent.
  1110. */
  1111. if (div64_u64(new_size + root->fs_info->max_extent - 1,
  1112. root->fs_info->max_extent) < num_extents)
  1113. return 0;
  1114. }
  1115. spin_lock(&BTRFS_I(inode)->accounting_lock);
  1116. BTRFS_I(inode)->outstanding_extents++;
  1117. spin_unlock(&BTRFS_I(inode)->accounting_lock);
  1118. return 0;
  1119. }
  1120. /*
  1121. * extent_io.c merge_extent_hook, used to track merged delayed allocation
  1122. * extents so we can keep track of new extents that are just merged onto old
  1123. * extents, such as when we are doing sequential writes, so we can properly
  1124. * account for the metadata space we'll need.
  1125. */
  1126. static int btrfs_merge_extent_hook(struct inode *inode,
  1127. struct extent_state *new,
  1128. struct extent_state *other)
  1129. {
  1130. struct btrfs_root *root = BTRFS_I(inode)->root;
  1131. u64 new_size, old_size;
  1132. u64 num_extents;
  1133. /* not delalloc, ignore it */
  1134. if (!(other->state & EXTENT_DELALLOC))
  1135. return 0;
  1136. old_size = other->end - other->start + 1;
  1137. if (new->start < other->start)
  1138. new_size = other->end - new->start + 1;
  1139. else
  1140. new_size = new->end - other->start + 1;
  1141. /* we're not bigger than the max, unreserve the space and go */
  1142. if (new_size <= root->fs_info->max_extent) {
  1143. spin_lock(&BTRFS_I(inode)->accounting_lock);
  1144. BTRFS_I(inode)->outstanding_extents--;
  1145. spin_unlock(&BTRFS_I(inode)->accounting_lock);
  1146. return 0;
  1147. }
  1148. /*
  1149. * If we grew by another max_extent, just return, we want to keep that
  1150. * reserved amount.
  1151. */
  1152. num_extents = div64_u64(old_size + root->fs_info->max_extent - 1,
  1153. root->fs_info->max_extent);
  1154. if (div64_u64(new_size + root->fs_info->max_extent - 1,
  1155. root->fs_info->max_extent) > num_extents)
  1156. return 0;
  1157. spin_lock(&BTRFS_I(inode)->accounting_lock);
  1158. BTRFS_I(inode)->outstanding_extents--;
  1159. spin_unlock(&BTRFS_I(inode)->accounting_lock);
  1160. return 0;
  1161. }
  1162. /*
  1163. * extent_io.c set_bit_hook, used to track delayed allocation
  1164. * bytes in this file, and to maintain the list of inodes that
  1165. * have pending delalloc work to be done.
  1166. */
  1167. static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
  1168. unsigned long old, unsigned long bits)
  1169. {
  1170. /*
  1171. * set_bit and clear bit hooks normally require _irqsave/restore
  1172. * but in this case, we are only testeing for the DELALLOC
  1173. * bit, which is only set or cleared with irqs on
  1174. */
  1175. if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
  1176. struct btrfs_root *root = BTRFS_I(inode)->root;
  1177. spin_lock(&BTRFS_I(inode)->accounting_lock);
  1178. BTRFS_I(inode)->outstanding_extents++;
  1179. spin_unlock(&BTRFS_I(inode)->accounting_lock);
  1180. btrfs_delalloc_reserve_space(root, inode, end - start + 1);
  1181. spin_lock(&root->fs_info->delalloc_lock);
  1182. BTRFS_I(inode)->delalloc_bytes += end - start + 1;
  1183. root->fs_info->delalloc_bytes += end - start + 1;
  1184. if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1185. list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
  1186. &root->fs_info->delalloc_inodes);
  1187. }
  1188. spin_unlock(&root->fs_info->delalloc_lock);
  1189. }
  1190. return 0;
  1191. }
  1192. /*
  1193. * extent_io.c clear_bit_hook, see set_bit_hook for why
  1194. */
  1195. static int btrfs_clear_bit_hook(struct inode *inode,
  1196. struct extent_state *state, unsigned long bits)
  1197. {
  1198. /*
  1199. * set_bit and clear bit hooks normally require _irqsave/restore
  1200. * but in this case, we are only testeing for the DELALLOC
  1201. * bit, which is only set or cleared with irqs on
  1202. */
  1203. if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
  1204. struct btrfs_root *root = BTRFS_I(inode)->root;
  1205. if (bits & EXTENT_DO_ACCOUNTING) {
  1206. spin_lock(&BTRFS_I(inode)->accounting_lock);
  1207. BTRFS_I(inode)->outstanding_extents--;
  1208. spin_unlock(&BTRFS_I(inode)->accounting_lock);
  1209. btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
  1210. }
  1211. spin_lock(&root->fs_info->delalloc_lock);
  1212. if (state->end - state->start + 1 >
  1213. root->fs_info->delalloc_bytes) {
  1214. printk(KERN_INFO "btrfs warning: delalloc account "
  1215. "%llu %llu\n",
  1216. (unsigned long long)
  1217. state->end - state->start + 1,
  1218. (unsigned long long)
  1219. root->fs_info->delalloc_bytes);
  1220. btrfs_delalloc_free_space(root, inode, (u64)-1);
  1221. root->fs_info->delalloc_bytes = 0;
  1222. BTRFS_I(inode)->delalloc_bytes = 0;
  1223. } else {
  1224. btrfs_delalloc_free_space(root, inode,
  1225. state->end -
  1226. state->start + 1);
  1227. root->fs_info->delalloc_bytes -= state->end -
  1228. state->start + 1;
  1229. BTRFS_I(inode)->delalloc_bytes -= state->end -
  1230. state->start + 1;
  1231. }
  1232. if (BTRFS_I(inode)->delalloc_bytes == 0 &&
  1233. !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1234. list_del_init(&BTRFS_I(inode)->delalloc_inodes);
  1235. }
  1236. spin_unlock(&root->fs_info->delalloc_lock);
  1237. }
  1238. return 0;
  1239. }
  1240. /*
  1241. * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
  1242. * we don't create bios that span stripes or chunks
  1243. */
  1244. int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
  1245. size_t size, struct bio *bio,
  1246. unsigned long bio_flags)
  1247. {
  1248. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  1249. struct btrfs_mapping_tree *map_tree;
  1250. u64 logical = (u64)bio->bi_sector << 9;
  1251. u64 length = 0;
  1252. u64 map_length;
  1253. int ret;
  1254. if (bio_flags & EXTENT_BIO_COMPRESSED)
  1255. return 0;
  1256. length = bio->bi_size;
  1257. map_tree = &root->fs_info->mapping_tree;
  1258. map_length = length;
  1259. ret = btrfs_map_block(map_tree, READ, logical,
  1260. &map_length, NULL, 0);
  1261. if (map_length < length + size)
  1262. return 1;
  1263. return 0;
  1264. }
  1265. /*
  1266. * in order to insert checksums into the metadata in large chunks,
  1267. * we wait until bio submission time. All the pages in the bio are
  1268. * checksummed and sums are attached onto the ordered extent record.
  1269. *
  1270. * At IO completion time the cums attached on the ordered extent record
  1271. * are inserted into the btree
  1272. */
  1273. static int __btrfs_submit_bio_start(struct inode *inode, int rw,
  1274. struct bio *bio, int mirror_num,
  1275. unsigned long bio_flags)
  1276. {
  1277. struct btrfs_root *root = BTRFS_I(inode)->root;
  1278. int ret = 0;
  1279. ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
  1280. BUG_ON(ret);
  1281. return 0;
  1282. }
  1283. /*
  1284. * in order to insert checksums into the metadata in large chunks,
  1285. * we wait until bio submission time. All the pages in the bio are
  1286. * checksummed and sums are attached onto the ordered extent record.
  1287. *
  1288. * At IO completion time the cums attached on the ordered extent record
  1289. * are inserted into the btree
  1290. */
  1291. static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
  1292. int mirror_num, unsigned long bio_flags)
  1293. {
  1294. struct btrfs_root *root = BTRFS_I(inode)->root;
  1295. return btrfs_map_bio(root, rw, bio, mirror_num, 1);
  1296. }
  1297. /*
  1298. * extent_io.c submission hook. This does the right thing for csum calculation
  1299. * on write, or reading the csums from the tree before a read
  1300. */
  1301. static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  1302. int mirror_num, unsigned long bio_flags)
  1303. {
  1304. struct btrfs_root *root = BTRFS_I(inode)->root;
  1305. int ret = 0;
  1306. int skip_sum;
  1307. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  1308. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  1309. BUG_ON(ret);
  1310. if (!(rw & (1 << BIO_RW))) {
  1311. if (bio_flags & EXTENT_BIO_COMPRESSED) {
  1312. return btrfs_submit_compressed_read(inode, bio,
  1313. mirror_num, bio_flags);
  1314. } else if (!skip_sum)
  1315. btrfs_lookup_bio_sums(root, inode, bio, NULL);
  1316. goto mapit;
  1317. } else if (!skip_sum) {
  1318. /* csum items have already been cloned */
  1319. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  1320. goto mapit;
  1321. /* we're doing a write, do the async checksumming */
  1322. return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  1323. inode, rw, bio, mirror_num,
  1324. bio_flags, __btrfs_submit_bio_start,
  1325. __btrfs_submit_bio_done);
  1326. }
  1327. mapit:
  1328. return btrfs_map_bio(root, rw, bio, mirror_num, 0);
  1329. }
  1330. /*
  1331. * given a list of ordered sums record them in the inode. This happens
  1332. * at IO completion time based on sums calculated at bio submission time.
  1333. */
  1334. static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
  1335. struct inode *inode, u64 file_offset,
  1336. struct list_head *list)
  1337. {
  1338. struct btrfs_ordered_sum *sum;
  1339. btrfs_set_trans_block_group(trans, inode);
  1340. list_for_each_entry(sum, list, list) {
  1341. btrfs_csum_file_blocks(trans,
  1342. BTRFS_I(inode)->root->fs_info->csum_root, sum);
  1343. }
  1344. return 0;
  1345. }
  1346. int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
  1347. {
  1348. if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
  1349. WARN_ON(1);
  1350. return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
  1351. GFP_NOFS);
  1352. }
  1353. /* see btrfs_writepage_start_hook for details on why this is required */
  1354. struct btrfs_writepage_fixup {
  1355. struct page *page;
  1356. struct btrfs_work work;
  1357. };
  1358. static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
  1359. {
  1360. struct btrfs_writepage_fixup *fixup;
  1361. struct btrfs_ordered_extent *ordered;
  1362. struct page *page;
  1363. struct inode *inode;
  1364. u64 page_start;
  1365. u64 page_end;
  1366. fixup = container_of(work, struct btrfs_writepage_fixup, work);
  1367. page = fixup->page;
  1368. again:
  1369. lock_page(page);
  1370. if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
  1371. ClearPageChecked(page);
  1372. goto out_page;
  1373. }
  1374. inode = page->mapping->host;
  1375. page_start = page_offset(page);
  1376. page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
  1377. lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
  1378. /* already ordered? We're done */
  1379. if (PagePrivate2(page))
  1380. goto out;
  1381. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  1382. if (ordered) {
  1383. unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
  1384. page_end, GFP_NOFS);
  1385. unlock_page(page);
  1386. btrfs_start_ordered_extent(inode, ordered, 1);
  1387. goto again;
  1388. }
  1389. btrfs_set_extent_delalloc(inode, page_start, page_end);
  1390. ClearPageChecked(page);
  1391. out:
  1392. unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
  1393. out_page:
  1394. unlock_page(page);
  1395. page_cache_release(page);
  1396. }
  1397. /*
  1398. * There are a few paths in the higher layers of the kernel that directly
  1399. * set the page dirty bit without asking the filesystem if it is a
  1400. * good idea. This causes problems because we want to make sure COW
  1401. * properly happens and the data=ordered rules are followed.
  1402. *
  1403. * In our case any range that doesn't have the ORDERED bit set
  1404. * hasn't been properly setup for IO. We kick off an async process
  1405. * to fix it up. The async helper will wait for ordered extents, set
  1406. * the delalloc bit and make it safe to write the page.
  1407. */
  1408. static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
  1409. {
  1410. struct inode *inode = page->mapping->host;
  1411. struct btrfs_writepage_fixup *fixup;
  1412. struct btrfs_root *root = BTRFS_I(inode)->root;
  1413. /* this page is properly in the ordered list */
  1414. if (TestClearPagePrivate2(page))
  1415. return 0;
  1416. if (PageChecked(page))
  1417. return -EAGAIN;
  1418. fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
  1419. if (!fixup)
  1420. return -EAGAIN;
  1421. SetPageChecked(page);
  1422. page_cache_get(page);
  1423. fixup->work.func = btrfs_writepage_fixup_worker;
  1424. fixup->page = page;
  1425. btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
  1426. return -EAGAIN;
  1427. }
  1428. static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
  1429. struct inode *inode, u64 file_pos,
  1430. u64 disk_bytenr, u64 disk_num_bytes,
  1431. u64 num_bytes, u64 ram_bytes,
  1432. u8 compression, u8 encryption,
  1433. u16 other_encoding, int extent_type)
  1434. {
  1435. struct btrfs_root *root = BTRFS_I(inode)->root;
  1436. struct btrfs_file_extent_item *fi;
  1437. struct btrfs_path *path;
  1438. struct extent_buffer *leaf;
  1439. struct btrfs_key ins;
  1440. u64 hint;
  1441. int ret;
  1442. path = btrfs_alloc_path();
  1443. BUG_ON(!path);
  1444. path->leave_spinning = 1;
  1445. /*
  1446. * we may be replacing one extent in the tree with another.
  1447. * The new extent is pinned in the extent map, and we don't want
  1448. * to drop it from the cache until it is completely in the btree.
  1449. *
  1450. * So, tell btrfs_drop_extents to leave this extent in the cache.
  1451. * the caller is expected to unpin it and allow it to be merged
  1452. * with the others.
  1453. */
  1454. ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
  1455. &hint, 0);
  1456. BUG_ON(ret);
  1457. ins.objectid = inode->i_ino;
  1458. ins.offset = file_pos;
  1459. ins.type = BTRFS_EXTENT_DATA_KEY;
  1460. ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
  1461. BUG_ON(ret);
  1462. leaf = path->nodes[0];
  1463. fi = btrfs_item_ptr(leaf, path->slots[0],
  1464. struct btrfs_file_extent_item);
  1465. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1466. btrfs_set_file_extent_type(leaf, fi, extent_type);
  1467. btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
  1468. btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
  1469. btrfs_set_file_extent_offset(leaf, fi, 0);
  1470. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1471. btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
  1472. btrfs_set_file_extent_compression(leaf, fi, compression);
  1473. btrfs_set_file_extent_encryption(leaf, fi, encryption);
  1474. btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
  1475. btrfs_unlock_up_safe(path, 1);
  1476. btrfs_set_lock_blocking(leaf);
  1477. btrfs_mark_buffer_dirty(leaf);
  1478. inode_add_bytes(inode, num_bytes);
  1479. ins.objectid = disk_bytenr;
  1480. ins.offset = disk_num_bytes;
  1481. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1482. ret = btrfs_alloc_reserved_file_extent(trans, root,
  1483. root->root_key.objectid,
  1484. inode->i_ino, file_pos, &ins);
  1485. BUG_ON(ret);
  1486. btrfs_free_path(path);
  1487. return 0;
  1488. }
  1489. /*
  1490. * helper function for btrfs_finish_ordered_io, this
  1491. * just reads in some of the csum leaves to prime them into ram
  1492. * before we start the transaction. It limits the amount of btree
  1493. * reads required while inside the transaction.
  1494. */
  1495. static noinline void reada_csum(struct btrfs_root *root,
  1496. struct btrfs_path *path,
  1497. struct btrfs_ordered_extent *ordered_extent)
  1498. {
  1499. struct btrfs_ordered_sum *sum;
  1500. u64 bytenr;
  1501. sum = list_entry(ordered_extent->list.next, struct btrfs_ordered_sum,
  1502. list);
  1503. bytenr = sum->sums[0].bytenr;
  1504. /*
  1505. * we don't care about the results, the point of this search is
  1506. * just to get the btree leaves into ram
  1507. */
  1508. btrfs_lookup_csum(NULL, root->fs_info->csum_root, path, bytenr, 0);
  1509. }
  1510. /* as ordered data IO finishes, this gets called so we can finish
  1511. * an ordered extent if the range of bytes in the file it covers are
  1512. * fully written.
  1513. */
  1514. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
  1515. {
  1516. struct btrfs_root *root = BTRFS_I(inode)->root;
  1517. struct btrfs_trans_handle *trans;
  1518. struct btrfs_ordered_extent *ordered_extent = NULL;
  1519. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1520. struct btrfs_path *path;
  1521. int compressed = 0;
  1522. int ret;
  1523. ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
  1524. if (!ret)
  1525. return 0;
  1526. /*
  1527. * before we join the transaction, try to do some of our IO.
  1528. * This will limit the amount of IO that we have to do with
  1529. * the transaction running. We're unlikely to need to do any
  1530. * IO if the file extents are new, the disk_i_size checks
  1531. * covers the most common case.
  1532. */
  1533. if (start < BTRFS_I(inode)->disk_i_size) {
  1534. path = btrfs_alloc_path();
  1535. if (path) {
  1536. ret = btrfs_lookup_file_extent(NULL, root, path,
  1537. inode->i_ino,
  1538. start, 0);
  1539. ordered_extent = btrfs_lookup_ordered_extent(inode,
  1540. start);
  1541. if (!list_empty(&ordered_extent->list)) {
  1542. btrfs_release_path(root, path);
  1543. reada_csum(root, path, ordered_extent);
  1544. }
  1545. btrfs_free_path(path);
  1546. }
  1547. }
  1548. if (!ordered_extent)
  1549. ordered_extent = btrfs_lookup_ordered_extent(inode, start);
  1550. BUG_ON(!ordered_extent);
  1551. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
  1552. BUG_ON(!list_empty(&ordered_extent->list));
  1553. ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
  1554. if (!ret) {
  1555. trans = btrfs_join_transaction(root, 1);
  1556. ret = btrfs_update_inode(trans, root, inode);
  1557. BUG_ON(ret);
  1558. btrfs_end_transaction(trans, root);
  1559. }
  1560. goto out;
  1561. }
  1562. lock_extent(io_tree, ordered_extent->file_offset,
  1563. ordered_extent->file_offset + ordered_extent->len - 1,
  1564. GFP_NOFS);
  1565. trans = btrfs_join_transaction(root, 1);
  1566. if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
  1567. compressed = 1;
  1568. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
  1569. BUG_ON(compressed);
  1570. ret = btrfs_mark_extent_written(trans, inode,
  1571. ordered_extent->file_offset,
  1572. ordered_extent->file_offset +
  1573. ordered_extent->len);
  1574. BUG_ON(ret);
  1575. } else {
  1576. ret = insert_reserved_file_extent(trans, inode,
  1577. ordered_extent->file_offset,
  1578. ordered_extent->start,
  1579. ordered_extent->disk_len,
  1580. ordered_extent->len,
  1581. ordered_extent->len,
  1582. compressed, 0, 0,
  1583. BTRFS_FILE_EXTENT_REG);
  1584. unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
  1585. ordered_extent->file_offset,
  1586. ordered_extent->len);
  1587. BUG_ON(ret);
  1588. }
  1589. unlock_extent(io_tree, ordered_extent->file_offset,
  1590. ordered_extent->file_offset + ordered_extent->len - 1,
  1591. GFP_NOFS);
  1592. add_pending_csums(trans, inode, ordered_extent->file_offset,
  1593. &ordered_extent->list);
  1594. /* this also removes the ordered extent from the tree */
  1595. btrfs_ordered_update_i_size(inode, 0, ordered_extent);
  1596. ret = btrfs_update_inode(trans, root, inode);
  1597. BUG_ON(ret);
  1598. btrfs_end_transaction(trans, root);
  1599. out:
  1600. /* once for us */
  1601. btrfs_put_ordered_extent(ordered_extent);
  1602. /* once for the tree */
  1603. btrfs_put_ordered_extent(ordered_extent);
  1604. return 0;
  1605. }
  1606. static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
  1607. struct extent_state *state, int uptodate)
  1608. {
  1609. ClearPagePrivate2(page);
  1610. return btrfs_finish_ordered_io(page->mapping->host, start, end);
  1611. }
  1612. /*
  1613. * When IO fails, either with EIO or csum verification fails, we
  1614. * try other mirrors that might have a good copy of the data. This
  1615. * io_failure_record is used to record state as we go through all the
  1616. * mirrors. If another mirror has good data, the page is set up to date
  1617. * and things continue. If a good mirror can't be found, the original
  1618. * bio end_io callback is called to indicate things have failed.
  1619. */
  1620. struct io_failure_record {
  1621. struct page *page;
  1622. u64 start;
  1623. u64 len;
  1624. u64 logical;
  1625. unsigned long bio_flags;
  1626. int last_mirror;
  1627. };
  1628. static int btrfs_io_failed_hook(struct bio *failed_bio,
  1629. struct page *page, u64 start, u64 end,
  1630. struct extent_state *state)
  1631. {
  1632. struct io_failure_record *failrec = NULL;
  1633. u64 private;
  1634. struct extent_map *em;
  1635. struct inode *inode = page->mapping->host;
  1636. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1637. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1638. struct bio *bio;
  1639. int num_copies;
  1640. int ret;
  1641. int rw;
  1642. u64 logical;
  1643. ret = get_state_private(failure_tree, start, &private);
  1644. if (ret) {
  1645. failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
  1646. if (!failrec)
  1647. return -ENOMEM;
  1648. failrec->start = start;
  1649. failrec->len = end - start + 1;
  1650. failrec->last_mirror = 0;
  1651. failrec->bio_flags = 0;
  1652. read_lock(&em_tree->lock);
  1653. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1654. if (em->start > start || em->start + em->len < start) {
  1655. free_extent_map(em);
  1656. em = NULL;
  1657. }
  1658. read_unlock(&em_tree->lock);
  1659. if (!em || IS_ERR(em)) {
  1660. kfree(failrec);
  1661. return -EIO;
  1662. }
  1663. logical = start - em->start;
  1664. logical = em->block_start + logical;
  1665. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1666. logical = em->block_start;
  1667. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  1668. }
  1669. failrec->logical = logical;
  1670. free_extent_map(em);
  1671. set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
  1672. EXTENT_DIRTY, GFP_NOFS);
  1673. set_state_private(failure_tree, start,
  1674. (u64)(unsigned long)failrec);
  1675. } else {
  1676. failrec = (struct io_failure_record *)(unsigned long)private;
  1677. }
  1678. num_copies = btrfs_num_copies(
  1679. &BTRFS_I(inode)->root->fs_info->mapping_tree,
  1680. failrec->logical, failrec->len);
  1681. failrec->last_mirror++;
  1682. if (!state) {
  1683. spin_lock(&BTRFS_I(inode)->io_tree.lock);
  1684. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  1685. failrec->start,
  1686. EXTENT_LOCKED);
  1687. if (state && state->start != failrec->start)
  1688. state = NULL;
  1689. spin_unlock(&BTRFS_I(inode)->io_tree.lock);
  1690. }
  1691. if (!state || failrec->last_mirror > num_copies) {
  1692. set_state_private(failure_tree, failrec->start, 0);
  1693. clear_extent_bits(failure_tree, failrec->start,
  1694. failrec->start + failrec->len - 1,
  1695. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  1696. kfree(failrec);
  1697. return -EIO;
  1698. }
  1699. bio = bio_alloc(GFP_NOFS, 1);
  1700. bio->bi_private = state;
  1701. bio->bi_end_io = failed_bio->bi_end_io;
  1702. bio->bi_sector = failrec->logical >> 9;
  1703. bio->bi_bdev = failed_bio->bi_bdev;
  1704. bio->bi_size = 0;
  1705. bio_add_page(bio, page, failrec->len, start - page_offset(page));
  1706. if (failed_bio->bi_rw & (1 << BIO_RW))
  1707. rw = WRITE;
  1708. else
  1709. rw = READ;
  1710. BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
  1711. failrec->last_mirror,
  1712. failrec->bio_flags);
  1713. return 0;
  1714. }
  1715. /*
  1716. * each time an IO finishes, we do a fast check in the IO failure tree
  1717. * to see if we need to process or clean up an io_failure_record
  1718. */
  1719. static int btrfs_clean_io_failures(struct inode *inode, u64 start)
  1720. {
  1721. u64 private;
  1722. u64 private_failure;
  1723. struct io_failure_record *failure;
  1724. int ret;
  1725. private = 0;
  1726. if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  1727. (u64)-1, 1, EXTENT_DIRTY)) {
  1728. ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
  1729. start, &private_failure);
  1730. if (ret == 0) {
  1731. failure = (struct io_failure_record *)(unsigned long)
  1732. private_failure;
  1733. set_state_private(&BTRFS_I(inode)->io_failure_tree,
  1734. failure->start, 0);
  1735. clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
  1736. failure->start,
  1737. failure->start + failure->len - 1,
  1738. EXTENT_DIRTY | EXTENT_LOCKED,
  1739. GFP_NOFS);
  1740. kfree(failure);
  1741. }
  1742. }
  1743. return 0;
  1744. }
  1745. /*
  1746. * when reads are done, we need to check csums to verify the data is correct
  1747. * if there's a match, we allow the bio to finish. If not, we go through
  1748. * the io_failure_record routines to find good copies
  1749. */
  1750. static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  1751. struct extent_state *state)
  1752. {
  1753. size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
  1754. struct inode *inode = page->mapping->host;
  1755. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1756. char *kaddr;
  1757. u64 private = ~(u32)0;
  1758. int ret;
  1759. struct btrfs_root *root = BTRFS_I(inode)->root;
  1760. u32 csum = ~(u32)0;
  1761. if (PageChecked(page)) {
  1762. ClearPageChecked(page);
  1763. goto good;
  1764. }
  1765. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
  1766. return 0;
  1767. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
  1768. test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
  1769. clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
  1770. GFP_NOFS);
  1771. return 0;
  1772. }
  1773. if (state && state->start == start) {
  1774. private = state->private;
  1775. ret = 0;
  1776. } else {
  1777. ret = get_state_private(io_tree, start, &private);
  1778. }
  1779. kaddr = kmap_atomic(page, KM_USER0);
  1780. if (ret)
  1781. goto zeroit;
  1782. csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
  1783. btrfs_csum_final(csum, (char *)&csum);
  1784. if (csum != private)
  1785. goto zeroit;
  1786. kunmap_atomic(kaddr, KM_USER0);
  1787. good:
  1788. /* if the io failure tree for this inode is non-empty,
  1789. * check to see if we've recovered from a failed IO
  1790. */
  1791. btrfs_clean_io_failures(inode, start);
  1792. return 0;
  1793. zeroit:
  1794. if (printk_ratelimit()) {
  1795. printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
  1796. "private %llu\n", page->mapping->host->i_ino,
  1797. (unsigned long long)start, csum,
  1798. (unsigned long long)private);
  1799. }
  1800. memset(kaddr + offset, 1, end - start + 1);
  1801. flush_dcache_page(page);
  1802. kunmap_atomic(kaddr, KM_USER0);
  1803. if (private == 0)
  1804. return 0;
  1805. return -EIO;
  1806. }
  1807. struct delayed_iput {
  1808. struct list_head list;
  1809. struct inode *inode;
  1810. };
  1811. void btrfs_add_delayed_iput(struct inode *inode)
  1812. {
  1813. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  1814. struct delayed_iput *delayed;
  1815. if (atomic_add_unless(&inode->i_count, -1, 1))
  1816. return;
  1817. delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
  1818. delayed->inode = inode;
  1819. spin_lock(&fs_info->delayed_iput_lock);
  1820. list_add_tail(&delayed->list, &fs_info->delayed_iputs);
  1821. spin_unlock(&fs_info->delayed_iput_lock);
  1822. }
  1823. void btrfs_run_delayed_iputs(struct btrfs_root *root)
  1824. {
  1825. LIST_HEAD(list);
  1826. struct btrfs_fs_info *fs_info = root->fs_info;
  1827. struct delayed_iput *delayed;
  1828. int empty;
  1829. spin_lock(&fs_info->delayed_iput_lock);
  1830. empty = list_empty(&fs_info->delayed_iputs);
  1831. spin_unlock(&fs_info->delayed_iput_lock);
  1832. if (empty)
  1833. return;
  1834. down_read(&root->fs_info->cleanup_work_sem);
  1835. spin_lock(&fs_info->delayed_iput_lock);
  1836. list_splice_init(&fs_info->delayed_iputs, &list);
  1837. spin_unlock(&fs_info->delayed_iput_lock);
  1838. while (!list_empty(&list)) {
  1839. delayed = list_entry(list.next, struct delayed_iput, list);
  1840. list_del(&delayed->list);
  1841. iput(delayed->inode);
  1842. kfree(delayed);
  1843. }
  1844. up_read(&root->fs_info->cleanup_work_sem);
  1845. }
  1846. /*
  1847. * This creates an orphan entry for the given inode in case something goes
  1848. * wrong in the middle of an unlink/truncate.
  1849. */
  1850. int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
  1851. {
  1852. struct btrfs_root *root = BTRFS_I(inode)->root;
  1853. int ret = 0;
  1854. spin_lock(&root->list_lock);
  1855. /* already on the orphan list, we're good */
  1856. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  1857. spin_unlock(&root->list_lock);
  1858. return 0;
  1859. }
  1860. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1861. spin_unlock(&root->list_lock);
  1862. /*
  1863. * insert an orphan item to track this unlinked/truncated file
  1864. */
  1865. ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
  1866. return ret;
  1867. }
  1868. /*
  1869. * We have done the truncate/delete so we can go ahead and remove the orphan
  1870. * item for this particular inode.
  1871. */
  1872. int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
  1873. {
  1874. struct btrfs_root *root = BTRFS_I(inode)->root;
  1875. int ret = 0;
  1876. spin_lock(&root->list_lock);
  1877. if (list_empty(&BTRFS_I(inode)->i_orphan)) {
  1878. spin_unlock(&root->list_lock);
  1879. return 0;
  1880. }
  1881. list_del_init(&BTRFS_I(inode)->i_orphan);
  1882. if (!trans) {
  1883. spin_unlock(&root->list_lock);
  1884. return 0;
  1885. }
  1886. spin_unlock(&root->list_lock);
  1887. ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
  1888. return ret;
  1889. }
  1890. /*
  1891. * this cleans up any orphans that may be left on the list from the last use
  1892. * of this root.
  1893. */
  1894. void btrfs_orphan_cleanup(struct btrfs_root *root)
  1895. {
  1896. struct btrfs_path *path;
  1897. struct extent_buffer *leaf;
  1898. struct btrfs_item *item;
  1899. struct btrfs_key key, found_key;
  1900. struct btrfs_trans_handle *trans;
  1901. struct inode *inode;
  1902. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  1903. if (!xchg(&root->clean_orphans, 0))
  1904. return;
  1905. path = btrfs_alloc_path();
  1906. BUG_ON(!path);
  1907. path->reada = -1;
  1908. key.objectid = BTRFS_ORPHAN_OBJECTID;
  1909. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  1910. key.offset = (u64)-1;
  1911. while (1) {
  1912. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1913. if (ret < 0) {
  1914. printk(KERN_ERR "Error searching slot for orphan: %d"
  1915. "\n", ret);
  1916. break;
  1917. }
  1918. /*
  1919. * if ret == 0 means we found what we were searching for, which
  1920. * is weird, but possible, so only screw with path if we didnt
  1921. * find the key and see if we have stuff that matches
  1922. */
  1923. if (ret > 0) {
  1924. if (path->slots[0] == 0)
  1925. break;
  1926. path->slots[0]--;
  1927. }
  1928. /* pull out the item */
  1929. leaf = path->nodes[0];
  1930. item = btrfs_item_nr(leaf, path->slots[0]);
  1931. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1932. /* make sure the item matches what we want */
  1933. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  1934. break;
  1935. if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
  1936. break;
  1937. /* release the path since we're done with it */
  1938. btrfs_release_path(root, path);
  1939. /*
  1940. * this is where we are basically btrfs_lookup, without the
  1941. * crossing root thing. we store the inode number in the
  1942. * offset of the orphan item.
  1943. */
  1944. found_key.objectid = found_key.offset;
  1945. found_key.type = BTRFS_INODE_ITEM_KEY;
  1946. found_key.offset = 0;
  1947. inode = btrfs_iget(root->fs_info->sb, &found_key, root);
  1948. if (IS_ERR(inode))
  1949. break;
  1950. /*
  1951. * add this inode to the orphan list so btrfs_orphan_del does
  1952. * the proper thing when we hit it
  1953. */
  1954. spin_lock(&root->list_lock);
  1955. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1956. spin_unlock(&root->list_lock);
  1957. /*
  1958. * if this is a bad inode, means we actually succeeded in
  1959. * removing the inode, but not the orphan record, which means
  1960. * we need to manually delete the orphan since iput will just
  1961. * do a destroy_inode
  1962. */
  1963. if (is_bad_inode(inode)) {
  1964. trans = btrfs_start_transaction(root, 1);
  1965. btrfs_orphan_del(trans, inode);
  1966. btrfs_end_transaction(trans, root);
  1967. iput(inode);
  1968. continue;
  1969. }
  1970. /* if we have links, this was a truncate, lets do that */
  1971. if (inode->i_nlink) {
  1972. nr_truncate++;
  1973. btrfs_truncate(inode);
  1974. } else {
  1975. nr_unlink++;
  1976. }
  1977. /* this will do delete_inode and everything for us */
  1978. iput(inode);
  1979. }
  1980. if (nr_unlink)
  1981. printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
  1982. if (nr_truncate)
  1983. printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
  1984. btrfs_free_path(path);
  1985. }
  1986. /*
  1987. * very simple check to peek ahead in the leaf looking for xattrs. If we
  1988. * don't find any xattrs, we know there can't be any acls.
  1989. *
  1990. * slot is the slot the inode is in, objectid is the objectid of the inode
  1991. */
  1992. static noinline int acls_after_inode_item(struct extent_buffer *leaf,
  1993. int slot, u64 objectid)
  1994. {
  1995. u32 nritems = btrfs_header_nritems(leaf);
  1996. struct btrfs_key found_key;
  1997. int scanned = 0;
  1998. slot++;
  1999. while (slot < nritems) {
  2000. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2001. /* we found a different objectid, there must not be acls */
  2002. if (found_key.objectid != objectid)
  2003. return 0;
  2004. /* we found an xattr, assume we've got an acl */
  2005. if (found_key.type == BTRFS_XATTR_ITEM_KEY)
  2006. return 1;
  2007. /*
  2008. * we found a key greater than an xattr key, there can't
  2009. * be any acls later on
  2010. */
  2011. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  2012. return 0;
  2013. slot++;
  2014. scanned++;
  2015. /*
  2016. * it goes inode, inode backrefs, xattrs, extents,
  2017. * so if there are a ton of hard links to an inode there can
  2018. * be a lot of backrefs. Don't waste time searching too hard,
  2019. * this is just an optimization
  2020. */
  2021. if (scanned >= 8)
  2022. break;
  2023. }
  2024. /* we hit the end of the leaf before we found an xattr or
  2025. * something larger than an xattr. We have to assume the inode
  2026. * has acls
  2027. */
  2028. return 1;
  2029. }
  2030. /*
  2031. * read an inode from the btree into the in-memory inode
  2032. */
  2033. static void btrfs_read_locked_inode(struct inode *inode)
  2034. {
  2035. struct btrfs_path *path;
  2036. struct extent_buffer *leaf;
  2037. struct btrfs_inode_item *inode_item;
  2038. struct btrfs_timespec *tspec;
  2039. struct btrfs_root *root = BTRFS_I(inode)->root;
  2040. struct btrfs_key location;
  2041. int maybe_acls;
  2042. u64 alloc_group_block;
  2043. u32 rdev;
  2044. int ret;
  2045. path = btrfs_alloc_path();
  2046. BUG_ON(!path);
  2047. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  2048. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  2049. if (ret)
  2050. goto make_bad;
  2051. leaf = path->nodes[0];
  2052. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  2053. struct btrfs_inode_item);
  2054. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  2055. inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
  2056. inode->i_uid = btrfs_inode_uid(leaf, inode_item);
  2057. inode->i_gid = btrfs_inode_gid(leaf, inode_item);
  2058. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  2059. tspec = btrfs_inode_atime(inode_item);
  2060. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2061. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2062. tspec = btrfs_inode_mtime(inode_item);
  2063. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2064. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2065. tspec = btrfs_inode_ctime(inode_item);
  2066. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2067. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2068. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  2069. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  2070. BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
  2071. inode->i_generation = BTRFS_I(inode)->generation;
  2072. inode->i_rdev = 0;
  2073. rdev = btrfs_inode_rdev(leaf, inode_item);
  2074. BTRFS_I(inode)->index_cnt = (u64)-1;
  2075. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  2076. alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
  2077. /*
  2078. * try to precache a NULL acl entry for files that don't have
  2079. * any xattrs or acls
  2080. */
  2081. maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
  2082. if (!maybe_acls)
  2083. cache_no_acl(inode);
  2084. BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
  2085. alloc_group_block, 0);
  2086. btrfs_free_path(path);
  2087. inode_item = NULL;
  2088. switch (inode->i_mode & S_IFMT) {
  2089. case S_IFREG:
  2090. inode->i_mapping->a_ops = &btrfs_aops;
  2091. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2092. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  2093. inode->i_fop = &btrfs_file_operations;
  2094. inode->i_op = &btrfs_file_inode_operations;
  2095. break;
  2096. case S_IFDIR:
  2097. inode->i_fop = &btrfs_dir_file_operations;
  2098. if (root == root->fs_info->tree_root)
  2099. inode->i_op = &btrfs_dir_ro_inode_operations;
  2100. else
  2101. inode->i_op = &btrfs_dir_inode_operations;
  2102. break;
  2103. case S_IFLNK:
  2104. inode->i_op = &btrfs_symlink_inode_operations;
  2105. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  2106. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2107. break;
  2108. default:
  2109. inode->i_op = &btrfs_special_inode_operations;
  2110. init_special_inode(inode, inode->i_mode, rdev);
  2111. break;
  2112. }
  2113. btrfs_update_iflags(inode);
  2114. return;
  2115. make_bad:
  2116. btrfs_free_path(path);
  2117. make_bad_inode(inode);
  2118. }
  2119. /*
  2120. * given a leaf and an inode, copy the inode fields into the leaf
  2121. */
  2122. static void fill_inode_item(struct btrfs_trans_handle *trans,
  2123. struct extent_buffer *leaf,
  2124. struct btrfs_inode_item *item,
  2125. struct inode *inode)
  2126. {
  2127. btrfs_set_inode_uid(leaf, item, inode->i_uid);
  2128. btrfs_set_inode_gid(leaf, item, inode->i_gid);
  2129. btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
  2130. btrfs_set_inode_mode(leaf, item, inode->i_mode);
  2131. btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
  2132. btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
  2133. inode->i_atime.tv_sec);
  2134. btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
  2135. inode->i_atime.tv_nsec);
  2136. btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
  2137. inode->i_mtime.tv_sec);
  2138. btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
  2139. inode->i_mtime.tv_nsec);
  2140. btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
  2141. inode->i_ctime.tv_sec);
  2142. btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
  2143. inode->i_ctime.tv_nsec);
  2144. btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
  2145. btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
  2146. btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
  2147. btrfs_set_inode_transid(leaf, item, trans->transid);
  2148. btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
  2149. btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
  2150. btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
  2151. }
  2152. /*
  2153. * copy everything in the in-memory inode into the btree.
  2154. */
  2155. noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
  2156. struct btrfs_root *root, struct inode *inode)
  2157. {
  2158. struct btrfs_inode_item *inode_item;
  2159. struct btrfs_path *path;
  2160. struct extent_buffer *leaf;
  2161. int ret;
  2162. path = btrfs_alloc_path();
  2163. BUG_ON(!path);
  2164. path->leave_spinning = 1;
  2165. ret = btrfs_lookup_inode(trans, root, path,
  2166. &BTRFS_I(inode)->location, 1);
  2167. if (ret) {
  2168. if (ret > 0)
  2169. ret = -ENOENT;
  2170. goto failed;
  2171. }
  2172. btrfs_unlock_up_safe(path, 1);
  2173. leaf = path->nodes[0];
  2174. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  2175. struct btrfs_inode_item);
  2176. fill_inode_item(trans, leaf, inode_item, inode);
  2177. btrfs_mark_buffer_dirty(leaf);
  2178. btrfs_set_inode_last_trans(trans, inode);
  2179. ret = 0;
  2180. failed:
  2181. btrfs_free_path(path);
  2182. return ret;
  2183. }
  2184. /*
  2185. * unlink helper that gets used here in inode.c and in the tree logging
  2186. * recovery code. It remove a link in a directory with a given name, and
  2187. * also drops the back refs in the inode to the directory
  2188. */
  2189. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  2190. struct btrfs_root *root,
  2191. struct inode *dir, struct inode *inode,
  2192. const char *name, int name_len)
  2193. {
  2194. struct btrfs_path *path;
  2195. int ret = 0;
  2196. struct extent_buffer *leaf;
  2197. struct btrfs_dir_item *di;
  2198. struct btrfs_key key;
  2199. u64 index;
  2200. path = btrfs_alloc_path();
  2201. if (!path) {
  2202. ret = -ENOMEM;
  2203. goto err;
  2204. }
  2205. path->leave_spinning = 1;
  2206. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2207. name, name_len, -1);
  2208. if (IS_ERR(di)) {
  2209. ret = PTR_ERR(di);
  2210. goto err;
  2211. }
  2212. if (!di) {
  2213. ret = -ENOENT;
  2214. goto err;
  2215. }
  2216. leaf = path->nodes[0];
  2217. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2218. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2219. if (ret)
  2220. goto err;
  2221. btrfs_release_path(root, path);
  2222. ret = btrfs_del_inode_ref(trans, root, name, name_len,
  2223. inode->i_ino,
  2224. dir->i_ino, &index);
  2225. if (ret) {
  2226. printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
  2227. "inode %lu parent %lu\n", name_len, name,
  2228. inode->i_ino, dir->i_ino);
  2229. goto err;
  2230. }
  2231. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  2232. index, name, name_len, -1);
  2233. if (IS_ERR(di)) {
  2234. ret = PTR_ERR(di);
  2235. goto err;
  2236. }
  2237. if (!di) {
  2238. ret = -ENOENT;
  2239. goto err;
  2240. }
  2241. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2242. btrfs_release_path(root, path);
  2243. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  2244. inode, dir->i_ino);
  2245. BUG_ON(ret != 0 && ret != -ENOENT);
  2246. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  2247. dir, index);
  2248. BUG_ON(ret);
  2249. err:
  2250. btrfs_free_path(path);
  2251. if (ret)
  2252. goto out;
  2253. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2254. inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2255. btrfs_update_inode(trans, root, dir);
  2256. btrfs_drop_nlink(inode);
  2257. ret = btrfs_update_inode(trans, root, inode);
  2258. out:
  2259. return ret;
  2260. }
  2261. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  2262. {
  2263. struct btrfs_root *root;
  2264. struct btrfs_trans_handle *trans;
  2265. struct inode *inode = dentry->d_inode;
  2266. int ret;
  2267. unsigned long nr = 0;
  2268. root = BTRFS_I(dir)->root;
  2269. /*
  2270. * 5 items for unlink inode
  2271. * 1 for orphan
  2272. */
  2273. ret = btrfs_reserve_metadata_space(root, 6);
  2274. if (ret)
  2275. return ret;
  2276. trans = btrfs_start_transaction(root, 1);
  2277. if (IS_ERR(trans)) {
  2278. btrfs_unreserve_metadata_space(root, 6);
  2279. return PTR_ERR(trans);
  2280. }
  2281. btrfs_set_trans_block_group(trans, dir);
  2282. btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
  2283. ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2284. dentry->d_name.name, dentry->d_name.len);
  2285. if (inode->i_nlink == 0)
  2286. ret = btrfs_orphan_add(trans, inode);
  2287. nr = trans->blocks_used;
  2288. btrfs_end_transaction_throttle(trans, root);
  2289. btrfs_unreserve_metadata_space(root, 6);
  2290. btrfs_btree_balance_dirty(root, nr);
  2291. return ret;
  2292. }
  2293. int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
  2294. struct btrfs_root *root,
  2295. struct inode *dir, u64 objectid,
  2296. const char *name, int name_len)
  2297. {
  2298. struct btrfs_path *path;
  2299. struct extent_buffer *leaf;
  2300. struct btrfs_dir_item *di;
  2301. struct btrfs_key key;
  2302. u64 index;
  2303. int ret;
  2304. path = btrfs_alloc_path();
  2305. if (!path)
  2306. return -ENOMEM;
  2307. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2308. name, name_len, -1);
  2309. BUG_ON(!di || IS_ERR(di));
  2310. leaf = path->nodes[0];
  2311. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2312. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  2313. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2314. BUG_ON(ret);
  2315. btrfs_release_path(root, path);
  2316. ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  2317. objectid, root->root_key.objectid,
  2318. dir->i_ino, &index, name, name_len);
  2319. if (ret < 0) {
  2320. BUG_ON(ret != -ENOENT);
  2321. di = btrfs_search_dir_index_item(root, path, dir->i_ino,
  2322. name, name_len);
  2323. BUG_ON(!di || IS_ERR(di));
  2324. leaf = path->nodes[0];
  2325. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2326. btrfs_release_path(root, path);
  2327. index = key.offset;
  2328. }
  2329. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  2330. index, name, name_len, -1);
  2331. BUG_ON(!di || IS_ERR(di));
  2332. leaf = path->nodes[0];
  2333. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2334. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  2335. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2336. BUG_ON(ret);
  2337. btrfs_release_path(root, path);
  2338. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2339. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2340. ret = btrfs_update_inode(trans, root, dir);
  2341. BUG_ON(ret);
  2342. dir->i_sb->s_dirt = 1;
  2343. btrfs_free_path(path);
  2344. return 0;
  2345. }
  2346. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  2347. {
  2348. struct inode *inode = dentry->d_inode;
  2349. int err = 0;
  2350. int ret;
  2351. struct btrfs_root *root = BTRFS_I(dir)->root;
  2352. struct btrfs_trans_handle *trans;
  2353. unsigned long nr = 0;
  2354. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
  2355. inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  2356. return -ENOTEMPTY;
  2357. ret = btrfs_reserve_metadata_space(root, 5);
  2358. if (ret)
  2359. return ret;
  2360. trans = btrfs_start_transaction(root, 1);
  2361. if (IS_ERR(trans)) {
  2362. btrfs_unreserve_metadata_space(root, 5);
  2363. return PTR_ERR(trans);
  2364. }
  2365. btrfs_set_trans_block_group(trans, dir);
  2366. if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  2367. err = btrfs_unlink_subvol(trans, root, dir,
  2368. BTRFS_I(inode)->location.objectid,
  2369. dentry->d_name.name,
  2370. dentry->d_name.len);
  2371. goto out;
  2372. }
  2373. err = btrfs_orphan_add(trans, inode);
  2374. if (err)
  2375. goto out;
  2376. /* now the directory is empty */
  2377. err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2378. dentry->d_name.name, dentry->d_name.len);
  2379. if (!err)
  2380. btrfs_i_size_write(inode, 0);
  2381. out:
  2382. nr = trans->blocks_used;
  2383. ret = btrfs_end_transaction_throttle(trans, root);
  2384. btrfs_unreserve_metadata_space(root, 5);
  2385. btrfs_btree_balance_dirty(root, nr);
  2386. if (ret && !err)
  2387. err = ret;
  2388. return err;
  2389. }
  2390. #if 0
  2391. /*
  2392. * when truncating bytes in a file, it is possible to avoid reading
  2393. * the leaves that contain only checksum items. This can be the
  2394. * majority of the IO required to delete a large file, but it must
  2395. * be done carefully.
  2396. *
  2397. * The keys in the level just above the leaves are checked to make sure
  2398. * the lowest key in a given leaf is a csum key, and starts at an offset
  2399. * after the new size.
  2400. *
  2401. * Then the key for the next leaf is checked to make sure it also has
  2402. * a checksum item for the same file. If it does, we know our target leaf
  2403. * contains only checksum items, and it can be safely freed without reading
  2404. * it.
  2405. *
  2406. * This is just an optimization targeted at large files. It may do
  2407. * nothing. It will return 0 unless things went badly.
  2408. */
  2409. static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
  2410. struct btrfs_root *root,
  2411. struct btrfs_path *path,
  2412. struct inode *inode, u64 new_size)
  2413. {
  2414. struct btrfs_key key;
  2415. int ret;
  2416. int nritems;
  2417. struct btrfs_key found_key;
  2418. struct btrfs_key other_key;
  2419. struct btrfs_leaf_ref *ref;
  2420. u64 leaf_gen;
  2421. u64 leaf_start;
  2422. path->lowest_level = 1;
  2423. key.objectid = inode->i_ino;
  2424. key.type = BTRFS_CSUM_ITEM_KEY;
  2425. key.offset = new_size;
  2426. again:
  2427. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2428. if (ret < 0)
  2429. goto out;
  2430. if (path->nodes[1] == NULL) {
  2431. ret = 0;
  2432. goto out;
  2433. }
  2434. ret = 0;
  2435. btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
  2436. nritems = btrfs_header_nritems(path->nodes[1]);
  2437. if (!nritems)
  2438. goto out;
  2439. if (path->slots[1] >= nritems)
  2440. goto next_node;
  2441. /* did we find a key greater than anything we want to delete? */
  2442. if (found_key.objectid > inode->i_ino ||
  2443. (found_key.objectid == inode->i_ino && found_key.type > key.type))
  2444. goto out;
  2445. /* we check the next key in the node to make sure the leave contains
  2446. * only checksum items. This comparison doesn't work if our
  2447. * leaf is the last one in the node
  2448. */
  2449. if (path->slots[1] + 1 >= nritems) {
  2450. next_node:
  2451. /* search forward from the last key in the node, this
  2452. * will bring us into the next node in the tree
  2453. */
  2454. btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
  2455. /* unlikely, but we inc below, so check to be safe */
  2456. if (found_key.offset == (u64)-1)
  2457. goto out;
  2458. /* search_forward needs a path with locks held, do the
  2459. * search again for the original key. It is possible
  2460. * this will race with a balance and return a path that
  2461. * we could modify, but this drop is just an optimization
  2462. * and is allowed to miss some leaves.
  2463. */
  2464. btrfs_release_path(root, path);
  2465. found_key.offset++;
  2466. /* setup a max key for search_forward */
  2467. other_key.offset = (u64)-1;
  2468. other_key.type = key.type;
  2469. other_key.objectid = key.objectid;
  2470. path->keep_locks = 1;
  2471. ret = btrfs_search_forward(root, &found_key, &other_key,
  2472. path, 0, 0);
  2473. path->keep_locks = 0;
  2474. if (ret || found_key.objectid != key.objectid ||
  2475. found_key.type != key.type) {
  2476. ret = 0;
  2477. goto out;
  2478. }
  2479. key.offset = found_key.offset;
  2480. btrfs_release_path(root, path);
  2481. cond_resched();
  2482. goto again;
  2483. }
  2484. /* we know there's one more slot after us in the tree,
  2485. * read that key so we can verify it is also a checksum item
  2486. */
  2487. btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
  2488. if (found_key.objectid < inode->i_ino)
  2489. goto next_key;
  2490. if (found_key.type != key.type || found_key.offset < new_size)
  2491. goto next_key;
  2492. /*
  2493. * if the key for the next leaf isn't a csum key from this objectid,
  2494. * we can't be sure there aren't good items inside this leaf.
  2495. * Bail out
  2496. */
  2497. if (other_key.objectid != inode->i_ino || other_key.type != key.type)
  2498. goto out;
  2499. leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
  2500. leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
  2501. /*
  2502. * it is safe to delete this leaf, it contains only
  2503. * csum items from this inode at an offset >= new_size
  2504. */
  2505. ret = btrfs_del_leaf(trans, root, path, leaf_start);
  2506. BUG_ON(ret);
  2507. if (root->ref_cows && leaf_gen < trans->transid) {
  2508. ref = btrfs_alloc_leaf_ref(root, 0);
  2509. if (ref) {
  2510. ref->root_gen = root->root_key.offset;
  2511. ref->bytenr = leaf_start;
  2512. ref->owner = 0;
  2513. ref->generation = leaf_gen;
  2514. ref->nritems = 0;
  2515. btrfs_sort_leaf_ref(ref);
  2516. ret = btrfs_add_leaf_ref(root, ref, 0);
  2517. WARN_ON(ret);
  2518. btrfs_free_leaf_ref(root, ref);
  2519. } else {
  2520. WARN_ON(1);
  2521. }
  2522. }
  2523. next_key:
  2524. btrfs_release_path(root, path);
  2525. if (other_key.objectid == inode->i_ino &&
  2526. other_key.type == key.type && other_key.offset > key.offset) {
  2527. key.offset = other_key.offset;
  2528. cond_resched();
  2529. goto again;
  2530. }
  2531. ret = 0;
  2532. out:
  2533. /* fixup any changes we've made to the path */
  2534. path->lowest_level = 0;
  2535. path->keep_locks = 0;
  2536. btrfs_release_path(root, path);
  2537. return ret;
  2538. }
  2539. #endif
  2540. /*
  2541. * this can truncate away extent items, csum items and directory items.
  2542. * It starts at a high offset and removes keys until it can't find
  2543. * any higher than new_size
  2544. *
  2545. * csum items that cross the new i_size are truncated to the new size
  2546. * as well.
  2547. *
  2548. * min_type is the minimum key type to truncate down to. If set to 0, this
  2549. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  2550. */
  2551. int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  2552. struct btrfs_root *root,
  2553. struct inode *inode,
  2554. u64 new_size, u32 min_type)
  2555. {
  2556. struct btrfs_path *path;
  2557. struct extent_buffer *leaf;
  2558. struct btrfs_file_extent_item *fi;
  2559. struct btrfs_key key;
  2560. struct btrfs_key found_key;
  2561. u64 extent_start = 0;
  2562. u64 extent_num_bytes = 0;
  2563. u64 extent_offset = 0;
  2564. u64 item_end = 0;
  2565. u64 mask = root->sectorsize - 1;
  2566. u32 found_type = (u8)-1;
  2567. int found_extent;
  2568. int del_item;
  2569. int pending_del_nr = 0;
  2570. int pending_del_slot = 0;
  2571. int extent_type = -1;
  2572. int encoding;
  2573. int ret;
  2574. int err = 0;
  2575. BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
  2576. if (root->ref_cows)
  2577. btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
  2578. path = btrfs_alloc_path();
  2579. BUG_ON(!path);
  2580. path->reada = -1;
  2581. key.objectid = inode->i_ino;
  2582. key.offset = (u64)-1;
  2583. key.type = (u8)-1;
  2584. search_again:
  2585. path->leave_spinning = 1;
  2586. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2587. if (ret < 0) {
  2588. err = ret;
  2589. goto out;
  2590. }
  2591. if (ret > 0) {
  2592. /* there are no items in the tree for us to truncate, we're
  2593. * done
  2594. */
  2595. if (path->slots[0] == 0)
  2596. goto out;
  2597. path->slots[0]--;
  2598. }
  2599. while (1) {
  2600. fi = NULL;
  2601. leaf = path->nodes[0];
  2602. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2603. found_type = btrfs_key_type(&found_key);
  2604. encoding = 0;
  2605. if (found_key.objectid != inode->i_ino)
  2606. break;
  2607. if (found_type < min_type)
  2608. break;
  2609. item_end = found_key.offset;
  2610. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  2611. fi = btrfs_item_ptr(leaf, path->slots[0],
  2612. struct btrfs_file_extent_item);
  2613. extent_type = btrfs_file_extent_type(leaf, fi);
  2614. encoding = btrfs_file_extent_compression(leaf, fi);
  2615. encoding |= btrfs_file_extent_encryption(leaf, fi);
  2616. encoding |= btrfs_file_extent_other_encoding(leaf, fi);
  2617. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2618. item_end +=
  2619. btrfs_file_extent_num_bytes(leaf, fi);
  2620. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2621. item_end += btrfs_file_extent_inline_len(leaf,
  2622. fi);
  2623. }
  2624. item_end--;
  2625. }
  2626. if (found_type > min_type) {
  2627. del_item = 1;
  2628. } else {
  2629. if (item_end < new_size)
  2630. break;
  2631. if (found_key.offset >= new_size)
  2632. del_item = 1;
  2633. else
  2634. del_item = 0;
  2635. }
  2636. found_extent = 0;
  2637. /* FIXME, shrink the extent if the ref count is only 1 */
  2638. if (found_type != BTRFS_EXTENT_DATA_KEY)
  2639. goto delete;
  2640. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2641. u64 num_dec;
  2642. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  2643. if (!del_item && !encoding) {
  2644. u64 orig_num_bytes =
  2645. btrfs_file_extent_num_bytes(leaf, fi);
  2646. extent_num_bytes = new_size -
  2647. found_key.offset + root->sectorsize - 1;
  2648. extent_num_bytes = extent_num_bytes &
  2649. ~((u64)root->sectorsize - 1);
  2650. btrfs_set_file_extent_num_bytes(leaf, fi,
  2651. extent_num_bytes);
  2652. num_dec = (orig_num_bytes -
  2653. extent_num_bytes);
  2654. if (root->ref_cows && extent_start != 0)
  2655. inode_sub_bytes(inode, num_dec);
  2656. btrfs_mark_buffer_dirty(leaf);
  2657. } else {
  2658. extent_num_bytes =
  2659. btrfs_file_extent_disk_num_bytes(leaf,
  2660. fi);
  2661. extent_offset = found_key.offset -
  2662. btrfs_file_extent_offset(leaf, fi);
  2663. /* FIXME blocksize != 4096 */
  2664. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  2665. if (extent_start != 0) {
  2666. found_extent = 1;
  2667. if (root->ref_cows)
  2668. inode_sub_bytes(inode, num_dec);
  2669. }
  2670. }
  2671. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2672. /*
  2673. * we can't truncate inline items that have had
  2674. * special encodings
  2675. */
  2676. if (!del_item &&
  2677. btrfs_file_extent_compression(leaf, fi) == 0 &&
  2678. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  2679. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  2680. u32 size = new_size - found_key.offset;
  2681. if (root->ref_cows) {
  2682. inode_sub_bytes(inode, item_end + 1 -
  2683. new_size);
  2684. }
  2685. size =
  2686. btrfs_file_extent_calc_inline_size(size);
  2687. ret = btrfs_truncate_item(trans, root, path,
  2688. size, 1);
  2689. BUG_ON(ret);
  2690. } else if (root->ref_cows) {
  2691. inode_sub_bytes(inode, item_end + 1 -
  2692. found_key.offset);
  2693. }
  2694. }
  2695. delete:
  2696. if (del_item) {
  2697. if (!pending_del_nr) {
  2698. /* no pending yet, add ourselves */
  2699. pending_del_slot = path->slots[0];
  2700. pending_del_nr = 1;
  2701. } else if (pending_del_nr &&
  2702. path->slots[0] + 1 == pending_del_slot) {
  2703. /* hop on the pending chunk */
  2704. pending_del_nr++;
  2705. pending_del_slot = path->slots[0];
  2706. } else {
  2707. BUG();
  2708. }
  2709. } else {
  2710. break;
  2711. }
  2712. if (found_extent && root->ref_cows) {
  2713. btrfs_set_path_blocking(path);
  2714. ret = btrfs_free_extent(trans, root, extent_start,
  2715. extent_num_bytes, 0,
  2716. btrfs_header_owner(leaf),
  2717. inode->i_ino, extent_offset);
  2718. BUG_ON(ret);
  2719. }
  2720. if (found_type == BTRFS_INODE_ITEM_KEY)
  2721. break;
  2722. if (path->slots[0] == 0 ||
  2723. path->slots[0] != pending_del_slot) {
  2724. if (root->ref_cows) {
  2725. err = -EAGAIN;
  2726. goto out;
  2727. }
  2728. if (pending_del_nr) {
  2729. ret = btrfs_del_items(trans, root, path,
  2730. pending_del_slot,
  2731. pending_del_nr);
  2732. BUG_ON(ret);
  2733. pending_del_nr = 0;
  2734. }
  2735. btrfs_release_path(root, path);
  2736. goto search_again;
  2737. } else {
  2738. path->slots[0]--;
  2739. }
  2740. }
  2741. out:
  2742. if (pending_del_nr) {
  2743. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  2744. pending_del_nr);
  2745. }
  2746. btrfs_free_path(path);
  2747. return err;
  2748. }
  2749. /*
  2750. * taken from block_truncate_page, but does cow as it zeros out
  2751. * any bytes left in the last page in the file.
  2752. */
  2753. static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
  2754. {
  2755. struct inode *inode = mapping->host;
  2756. struct btrfs_root *root = BTRFS_I(inode)->root;
  2757. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2758. struct btrfs_ordered_extent *ordered;
  2759. char *kaddr;
  2760. u32 blocksize = root->sectorsize;
  2761. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  2762. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  2763. struct page *page;
  2764. int ret = 0;
  2765. u64 page_start;
  2766. u64 page_end;
  2767. if ((offset & (blocksize - 1)) == 0)
  2768. goto out;
  2769. ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
  2770. if (ret)
  2771. goto out;
  2772. ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
  2773. if (ret)
  2774. goto out;
  2775. ret = -ENOMEM;
  2776. again:
  2777. page = grab_cache_page(mapping, index);
  2778. if (!page) {
  2779. btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
  2780. btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
  2781. goto out;
  2782. }
  2783. page_start = page_offset(page);
  2784. page_end = page_start + PAGE_CACHE_SIZE - 1;
  2785. if (!PageUptodate(page)) {
  2786. ret = btrfs_readpage(NULL, page);
  2787. lock_page(page);
  2788. if (page->mapping != mapping) {
  2789. unlock_page(page);
  2790. page_cache_release(page);
  2791. goto again;
  2792. }
  2793. if (!PageUptodate(page)) {
  2794. ret = -EIO;
  2795. goto out_unlock;
  2796. }
  2797. }
  2798. wait_on_page_writeback(page);
  2799. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2800. set_page_extent_mapped(page);
  2801. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  2802. if (ordered) {
  2803. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2804. unlock_page(page);
  2805. page_cache_release(page);
  2806. btrfs_start_ordered_extent(inode, ordered, 1);
  2807. btrfs_put_ordered_extent(ordered);
  2808. goto again;
  2809. }
  2810. clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
  2811. EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
  2812. GFP_NOFS);
  2813. ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
  2814. if (ret) {
  2815. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2816. goto out_unlock;
  2817. }
  2818. ret = 0;
  2819. if (offset != PAGE_CACHE_SIZE) {
  2820. kaddr = kmap(page);
  2821. memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
  2822. flush_dcache_page(page);
  2823. kunmap(page);
  2824. }
  2825. ClearPageChecked(page);
  2826. set_page_dirty(page);
  2827. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2828. out_unlock:
  2829. if (ret)
  2830. btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
  2831. btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
  2832. unlock_page(page);
  2833. page_cache_release(page);
  2834. out:
  2835. return ret;
  2836. }
  2837. int btrfs_cont_expand(struct inode *inode, loff_t size)
  2838. {
  2839. struct btrfs_trans_handle *trans;
  2840. struct btrfs_root *root = BTRFS_I(inode)->root;
  2841. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2842. struct extent_map *em;
  2843. u64 mask = root->sectorsize - 1;
  2844. u64 hole_start = (inode->i_size + mask) & ~mask;
  2845. u64 block_end = (size + mask) & ~mask;
  2846. u64 last_byte;
  2847. u64 cur_offset;
  2848. u64 hole_size;
  2849. int err = 0;
  2850. if (size <= hole_start)
  2851. return 0;
  2852. while (1) {
  2853. struct btrfs_ordered_extent *ordered;
  2854. btrfs_wait_ordered_range(inode, hole_start,
  2855. block_end - hole_start);
  2856. lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2857. ordered = btrfs_lookup_ordered_extent(inode, hole_start);
  2858. if (!ordered)
  2859. break;
  2860. unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2861. btrfs_put_ordered_extent(ordered);
  2862. }
  2863. cur_offset = hole_start;
  2864. while (1) {
  2865. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  2866. block_end - cur_offset, 0);
  2867. BUG_ON(IS_ERR(em) || !em);
  2868. last_byte = min(extent_map_end(em), block_end);
  2869. last_byte = (last_byte + mask) & ~mask;
  2870. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  2871. u64 hint_byte = 0;
  2872. hole_size = last_byte - cur_offset;
  2873. err = btrfs_reserve_metadata_space(root, 2);
  2874. if (err)
  2875. break;
  2876. trans = btrfs_start_transaction(root, 1);
  2877. btrfs_set_trans_block_group(trans, inode);
  2878. err = btrfs_drop_extents(trans, inode, cur_offset,
  2879. cur_offset + hole_size,
  2880. &hint_byte, 1);
  2881. BUG_ON(err);
  2882. err = btrfs_insert_file_extent(trans, root,
  2883. inode->i_ino, cur_offset, 0,
  2884. 0, hole_size, 0, hole_size,
  2885. 0, 0, 0);
  2886. BUG_ON(err);
  2887. btrfs_drop_extent_cache(inode, hole_start,
  2888. last_byte - 1, 0);
  2889. btrfs_end_transaction(trans, root);
  2890. btrfs_unreserve_metadata_space(root, 2);
  2891. }
  2892. free_extent_map(em);
  2893. cur_offset = last_byte;
  2894. if (cur_offset >= block_end)
  2895. break;
  2896. }
  2897. unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2898. return err;
  2899. }
  2900. static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
  2901. {
  2902. struct btrfs_root *root = BTRFS_I(inode)->root;
  2903. struct btrfs_trans_handle *trans;
  2904. unsigned long nr;
  2905. int ret;
  2906. if (attr->ia_size == inode->i_size)
  2907. return 0;
  2908. if (attr->ia_size > inode->i_size) {
  2909. unsigned long limit;
  2910. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  2911. if (attr->ia_size > inode->i_sb->s_maxbytes)
  2912. return -EFBIG;
  2913. if (limit != RLIM_INFINITY && attr->ia_size > limit) {
  2914. send_sig(SIGXFSZ, current, 0);
  2915. return -EFBIG;
  2916. }
  2917. }
  2918. ret = btrfs_reserve_metadata_space(root, 1);
  2919. if (ret)
  2920. return ret;
  2921. trans = btrfs_start_transaction(root, 1);
  2922. btrfs_set_trans_block_group(trans, inode);
  2923. ret = btrfs_orphan_add(trans, inode);
  2924. BUG_ON(ret);
  2925. nr = trans->blocks_used;
  2926. btrfs_end_transaction(trans, root);
  2927. btrfs_unreserve_metadata_space(root, 1);
  2928. btrfs_btree_balance_dirty(root, nr);
  2929. if (attr->ia_size > inode->i_size) {
  2930. ret = btrfs_cont_expand(inode, attr->ia_size);
  2931. if (ret) {
  2932. btrfs_truncate(inode);
  2933. return ret;
  2934. }
  2935. i_size_write(inode, attr->ia_size);
  2936. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  2937. trans = btrfs_start_transaction(root, 1);
  2938. btrfs_set_trans_block_group(trans, inode);
  2939. ret = btrfs_update_inode(trans, root, inode);
  2940. BUG_ON(ret);
  2941. if (inode->i_nlink > 0) {
  2942. ret = btrfs_orphan_del(trans, inode);
  2943. BUG_ON(ret);
  2944. }
  2945. nr = trans->blocks_used;
  2946. btrfs_end_transaction(trans, root);
  2947. btrfs_btree_balance_dirty(root, nr);
  2948. return 0;
  2949. }
  2950. /*
  2951. * We're truncating a file that used to have good data down to
  2952. * zero. Make sure it gets into the ordered flush list so that
  2953. * any new writes get down to disk quickly.
  2954. */
  2955. if (attr->ia_size == 0)
  2956. BTRFS_I(inode)->ordered_data_close = 1;
  2957. /* we don't support swapfiles, so vmtruncate shouldn't fail */
  2958. ret = vmtruncate(inode, attr->ia_size);
  2959. BUG_ON(ret);
  2960. return 0;
  2961. }
  2962. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  2963. {
  2964. struct inode *inode = dentry->d_inode;
  2965. int err;
  2966. err = inode_change_ok(inode, attr);
  2967. if (err)
  2968. return err;
  2969. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  2970. err = btrfs_setattr_size(inode, attr);
  2971. if (err)
  2972. return err;
  2973. }
  2974. attr->ia_valid &= ~ATTR_SIZE;
  2975. if (attr->ia_valid)
  2976. err = inode_setattr(inode, attr);
  2977. if (!err && ((attr->ia_valid & ATTR_MODE)))
  2978. err = btrfs_acl_chmod(inode);
  2979. return err;
  2980. }
  2981. void btrfs_delete_inode(struct inode *inode)
  2982. {
  2983. struct btrfs_trans_handle *trans;
  2984. struct btrfs_root *root = BTRFS_I(inode)->root;
  2985. unsigned long nr;
  2986. int ret;
  2987. truncate_inode_pages(&inode->i_data, 0);
  2988. if (is_bad_inode(inode)) {
  2989. btrfs_orphan_del(NULL, inode);
  2990. goto no_delete;
  2991. }
  2992. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  2993. if (root->fs_info->log_root_recovering) {
  2994. BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
  2995. goto no_delete;
  2996. }
  2997. if (inode->i_nlink > 0) {
  2998. BUG_ON(btrfs_root_refs(&root->root_item) != 0);
  2999. goto no_delete;
  3000. }
  3001. btrfs_i_size_write(inode, 0);
  3002. while (1) {
  3003. trans = btrfs_start_transaction(root, 1);
  3004. btrfs_set_trans_block_group(trans, inode);
  3005. ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
  3006. if (ret != -EAGAIN)
  3007. break;
  3008. nr = trans->blocks_used;
  3009. btrfs_end_transaction(trans, root);
  3010. trans = NULL;
  3011. btrfs_btree_balance_dirty(root, nr);
  3012. }
  3013. if (ret == 0) {
  3014. ret = btrfs_orphan_del(trans, inode);
  3015. BUG_ON(ret);
  3016. }
  3017. nr = trans->blocks_used;
  3018. btrfs_end_transaction(trans, root);
  3019. btrfs_btree_balance_dirty(root, nr);
  3020. no_delete:
  3021. clear_inode(inode);
  3022. return;
  3023. }
  3024. /*
  3025. * this returns the key found in the dir entry in the location pointer.
  3026. * If no dir entries were found, location->objectid is 0.
  3027. */
  3028. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  3029. struct btrfs_key *location)
  3030. {
  3031. const char *name = dentry->d_name.name;
  3032. int namelen = dentry->d_name.len;
  3033. struct btrfs_dir_item *di;
  3034. struct btrfs_path *path;
  3035. struct btrfs_root *root = BTRFS_I(dir)->root;
  3036. int ret = 0;
  3037. path = btrfs_alloc_path();
  3038. BUG_ON(!path);
  3039. di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  3040. namelen, 0);
  3041. if (IS_ERR(di))
  3042. ret = PTR_ERR(di);
  3043. if (!di || IS_ERR(di))
  3044. goto out_err;
  3045. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  3046. out:
  3047. btrfs_free_path(path);
  3048. return ret;
  3049. out_err:
  3050. location->objectid = 0;
  3051. goto out;
  3052. }
  3053. /*
  3054. * when we hit a tree root in a directory, the btrfs part of the inode
  3055. * needs to be changed to reflect the root directory of the tree root. This
  3056. * is kind of like crossing a mount point.
  3057. */
  3058. static int fixup_tree_root_location(struct btrfs_root *root,
  3059. struct inode *dir,
  3060. struct dentry *dentry,
  3061. struct btrfs_key *location,
  3062. struct btrfs_root **sub_root)
  3063. {
  3064. struct btrfs_path *path;
  3065. struct btrfs_root *new_root;
  3066. struct btrfs_root_ref *ref;
  3067. struct extent_buffer *leaf;
  3068. int ret;
  3069. int err = 0;
  3070. path = btrfs_alloc_path();
  3071. if (!path) {
  3072. err = -ENOMEM;
  3073. goto out;
  3074. }
  3075. err = -ENOENT;
  3076. ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
  3077. BTRFS_I(dir)->root->root_key.objectid,
  3078. location->objectid);
  3079. if (ret) {
  3080. if (ret < 0)
  3081. err = ret;
  3082. goto out;
  3083. }
  3084. leaf = path->nodes[0];
  3085. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  3086. if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
  3087. btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
  3088. goto out;
  3089. ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
  3090. (unsigned long)(ref + 1),
  3091. dentry->d_name.len);
  3092. if (ret)
  3093. goto out;
  3094. btrfs_release_path(root->fs_info->tree_root, path);
  3095. new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
  3096. if (IS_ERR(new_root)) {
  3097. err = PTR_ERR(new_root);
  3098. goto out;
  3099. }
  3100. if (btrfs_root_refs(&new_root->root_item) == 0) {
  3101. err = -ENOENT;
  3102. goto out;
  3103. }
  3104. *sub_root = new_root;
  3105. location->objectid = btrfs_root_dirid(&new_root->root_item);
  3106. location->type = BTRFS_INODE_ITEM_KEY;
  3107. location->offset = 0;
  3108. err = 0;
  3109. out:
  3110. btrfs_free_path(path);
  3111. return err;
  3112. }
  3113. static void inode_tree_add(struct inode *inode)
  3114. {
  3115. struct btrfs_root *root = BTRFS_I(inode)->root;
  3116. struct btrfs_inode *entry;
  3117. struct rb_node **p;
  3118. struct rb_node *parent;
  3119. again:
  3120. p = &root->inode_tree.rb_node;
  3121. parent = NULL;
  3122. if (hlist_unhashed(&inode->i_hash))
  3123. return;
  3124. spin_lock(&root->inode_lock);
  3125. while (*p) {
  3126. parent = *p;
  3127. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  3128. if (inode->i_ino < entry->vfs_inode.i_ino)
  3129. p = &parent->rb_left;
  3130. else if (inode->i_ino > entry->vfs_inode.i_ino)
  3131. p = &parent->rb_right;
  3132. else {
  3133. WARN_ON(!(entry->vfs_inode.i_state &
  3134. (I_WILL_FREE | I_FREEING | I_CLEAR)));
  3135. rb_erase(parent, &root->inode_tree);
  3136. RB_CLEAR_NODE(parent);
  3137. spin_unlock(&root->inode_lock);
  3138. goto again;
  3139. }
  3140. }
  3141. rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
  3142. rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3143. spin_unlock(&root->inode_lock);
  3144. }
  3145. static void inode_tree_del(struct inode *inode)
  3146. {
  3147. struct btrfs_root *root = BTRFS_I(inode)->root;
  3148. int empty = 0;
  3149. spin_lock(&root->inode_lock);
  3150. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  3151. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3152. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  3153. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3154. }
  3155. spin_unlock(&root->inode_lock);
  3156. if (empty && btrfs_root_refs(&root->root_item) == 0) {
  3157. synchronize_srcu(&root->fs_info->subvol_srcu);
  3158. spin_lock(&root->inode_lock);
  3159. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3160. spin_unlock(&root->inode_lock);
  3161. if (empty)
  3162. btrfs_add_dead_root(root);
  3163. }
  3164. }
  3165. int btrfs_invalidate_inodes(struct btrfs_root *root)
  3166. {
  3167. struct rb_node *node;
  3168. struct rb_node *prev;
  3169. struct btrfs_inode *entry;
  3170. struct inode *inode;
  3171. u64 objectid = 0;
  3172. WARN_ON(btrfs_root_refs(&root->root_item) != 0);
  3173. spin_lock(&root->inode_lock);
  3174. again:
  3175. node = root->inode_tree.rb_node;
  3176. prev = NULL;
  3177. while (node) {
  3178. prev = node;
  3179. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3180. if (objectid < entry->vfs_inode.i_ino)
  3181. node = node->rb_left;
  3182. else if (objectid > entry->vfs_inode.i_ino)
  3183. node = node->rb_right;
  3184. else
  3185. break;
  3186. }
  3187. if (!node) {
  3188. while (prev) {
  3189. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  3190. if (objectid <= entry->vfs_inode.i_ino) {
  3191. node = prev;
  3192. break;
  3193. }
  3194. prev = rb_next(prev);
  3195. }
  3196. }
  3197. while (node) {
  3198. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3199. objectid = entry->vfs_inode.i_ino + 1;
  3200. inode = igrab(&entry->vfs_inode);
  3201. if (inode) {
  3202. spin_unlock(&root->inode_lock);
  3203. if (atomic_read(&inode->i_count) > 1)
  3204. d_prune_aliases(inode);
  3205. /*
  3206. * btrfs_drop_inode will remove it from
  3207. * the inode cache when its usage count
  3208. * hits zero.
  3209. */
  3210. iput(inode);
  3211. cond_resched();
  3212. spin_lock(&root->inode_lock);
  3213. goto again;
  3214. }
  3215. if (cond_resched_lock(&root->inode_lock))
  3216. goto again;
  3217. node = rb_next(node);
  3218. }
  3219. spin_unlock(&root->inode_lock);
  3220. return 0;
  3221. }
  3222. static noinline void init_btrfs_i(struct inode *inode)
  3223. {
  3224. struct btrfs_inode *bi = BTRFS_I(inode);
  3225. bi->generation = 0;
  3226. bi->sequence = 0;
  3227. bi->last_trans = 0;
  3228. bi->last_sub_trans = 0;
  3229. bi->logged_trans = 0;
  3230. bi->delalloc_bytes = 0;
  3231. bi->reserved_bytes = 0;
  3232. bi->disk_i_size = 0;
  3233. bi->flags = 0;
  3234. bi->index_cnt = (u64)-1;
  3235. bi->last_unlink_trans = 0;
  3236. bi->ordered_data_close = 0;
  3237. extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
  3238. extent_io_tree_init(&BTRFS_I(inode)->io_tree,
  3239. inode->i_mapping, GFP_NOFS);
  3240. extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
  3241. inode->i_mapping, GFP_NOFS);
  3242. INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
  3243. INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
  3244. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  3245. btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
  3246. mutex_init(&BTRFS_I(inode)->log_mutex);
  3247. }
  3248. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  3249. {
  3250. struct btrfs_iget_args *args = p;
  3251. inode->i_ino = args->ino;
  3252. init_btrfs_i(inode);
  3253. BTRFS_I(inode)->root = args->root;
  3254. btrfs_set_inode_space_info(args->root, inode);
  3255. return 0;
  3256. }
  3257. static int btrfs_find_actor(struct inode *inode, void *opaque)
  3258. {
  3259. struct btrfs_iget_args *args = opaque;
  3260. return args->ino == inode->i_ino &&
  3261. args->root == BTRFS_I(inode)->root;
  3262. }
  3263. static struct inode *btrfs_iget_locked(struct super_block *s,
  3264. u64 objectid,
  3265. struct btrfs_root *root)
  3266. {
  3267. struct inode *inode;
  3268. struct btrfs_iget_args args;
  3269. args.ino = objectid;
  3270. args.root = root;
  3271. inode = iget5_locked(s, objectid, btrfs_find_actor,
  3272. btrfs_init_locked_inode,
  3273. (void *)&args);
  3274. return inode;
  3275. }
  3276. /* Get an inode object given its location and corresponding root.
  3277. * Returns in *is_new if the inode was read from disk
  3278. */
  3279. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  3280. struct btrfs_root *root)
  3281. {
  3282. struct inode *inode;
  3283. inode = btrfs_iget_locked(s, location->objectid, root);
  3284. if (!inode)
  3285. return ERR_PTR(-ENOMEM);
  3286. if (inode->i_state & I_NEW) {
  3287. BTRFS_I(inode)->root = root;
  3288. memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
  3289. btrfs_read_locked_inode(inode);
  3290. inode_tree_add(inode);
  3291. unlock_new_inode(inode);
  3292. }
  3293. return inode;
  3294. }
  3295. static struct inode *new_simple_dir(struct super_block *s,
  3296. struct btrfs_key *key,
  3297. struct btrfs_root *root)
  3298. {
  3299. struct inode *inode = new_inode(s);
  3300. if (!inode)
  3301. return ERR_PTR(-ENOMEM);
  3302. init_btrfs_i(inode);
  3303. BTRFS_I(inode)->root = root;
  3304. memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
  3305. BTRFS_I(inode)->dummy_inode = 1;
  3306. inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
  3307. inode->i_op = &simple_dir_inode_operations;
  3308. inode->i_fop = &simple_dir_operations;
  3309. inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
  3310. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3311. return inode;
  3312. }
  3313. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  3314. {
  3315. struct inode *inode;
  3316. struct btrfs_root *root = BTRFS_I(dir)->root;
  3317. struct btrfs_root *sub_root = root;
  3318. struct btrfs_key location;
  3319. int index;
  3320. int ret;
  3321. dentry->d_op = &btrfs_dentry_operations;
  3322. if (dentry->d_name.len > BTRFS_NAME_LEN)
  3323. return ERR_PTR(-ENAMETOOLONG);
  3324. ret = btrfs_inode_by_name(dir, dentry, &location);
  3325. if (ret < 0)
  3326. return ERR_PTR(ret);
  3327. if (location.objectid == 0)
  3328. return NULL;
  3329. if (location.type == BTRFS_INODE_ITEM_KEY) {
  3330. inode = btrfs_iget(dir->i_sb, &location, root);
  3331. return inode;
  3332. }
  3333. BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
  3334. index = srcu_read_lock(&root->fs_info->subvol_srcu);
  3335. ret = fixup_tree_root_location(root, dir, dentry,
  3336. &location, &sub_root);
  3337. if (ret < 0) {
  3338. if (ret != -ENOENT)
  3339. inode = ERR_PTR(ret);
  3340. else
  3341. inode = new_simple_dir(dir->i_sb, &location, sub_root);
  3342. } else {
  3343. inode = btrfs_iget(dir->i_sb, &location, sub_root);
  3344. }
  3345. srcu_read_unlock(&root->fs_info->subvol_srcu, index);
  3346. if (root != sub_root) {
  3347. down_read(&root->fs_info->cleanup_work_sem);
  3348. if (!(inode->i_sb->s_flags & MS_RDONLY))
  3349. btrfs_orphan_cleanup(sub_root);
  3350. up_read(&root->fs_info->cleanup_work_sem);
  3351. }
  3352. return inode;
  3353. }
  3354. static int btrfs_dentry_delete(struct dentry *dentry)
  3355. {
  3356. struct btrfs_root *root;
  3357. if (!dentry->d_inode && !IS_ROOT(dentry))
  3358. dentry = dentry->d_parent;
  3359. if (dentry->d_inode) {
  3360. root = BTRFS_I(dentry->d_inode)->root;
  3361. if (btrfs_root_refs(&root->root_item) == 0)
  3362. return 1;
  3363. }
  3364. return 0;
  3365. }
  3366. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  3367. struct nameidata *nd)
  3368. {
  3369. struct inode *inode;
  3370. inode = btrfs_lookup_dentry(dir, dentry);
  3371. if (IS_ERR(inode))
  3372. return ERR_CAST(inode);
  3373. return d_splice_alias(inode, dentry);
  3374. }
  3375. static unsigned char btrfs_filetype_table[] = {
  3376. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  3377. };
  3378. static int btrfs_real_readdir(struct file *filp, void *dirent,
  3379. filldir_t filldir)
  3380. {
  3381. struct inode *inode = filp->f_dentry->d_inode;
  3382. struct btrfs_root *root = BTRFS_I(inode)->root;
  3383. struct btrfs_item *item;
  3384. struct btrfs_dir_item *di;
  3385. struct btrfs_key key;
  3386. struct btrfs_key found_key;
  3387. struct btrfs_path *path;
  3388. int ret;
  3389. u32 nritems;
  3390. struct extent_buffer *leaf;
  3391. int slot;
  3392. int advance;
  3393. unsigned char d_type;
  3394. int over = 0;
  3395. u32 di_cur;
  3396. u32 di_total;
  3397. u32 di_len;
  3398. int key_type = BTRFS_DIR_INDEX_KEY;
  3399. char tmp_name[32];
  3400. char *name_ptr;
  3401. int name_len;
  3402. /* FIXME, use a real flag for deciding about the key type */
  3403. if (root->fs_info->tree_root == root)
  3404. key_type = BTRFS_DIR_ITEM_KEY;
  3405. /* special case for "." */
  3406. if (filp->f_pos == 0) {
  3407. over = filldir(dirent, ".", 1,
  3408. 1, inode->i_ino,
  3409. DT_DIR);
  3410. if (over)
  3411. return 0;
  3412. filp->f_pos = 1;
  3413. }
  3414. /* special case for .., just use the back ref */
  3415. if (filp->f_pos == 1) {
  3416. u64 pino = parent_ino(filp->f_path.dentry);
  3417. over = filldir(dirent, "..", 2,
  3418. 2, pino, DT_DIR);
  3419. if (over)
  3420. return 0;
  3421. filp->f_pos = 2;
  3422. }
  3423. path = btrfs_alloc_path();
  3424. path->reada = 2;
  3425. btrfs_set_key_type(&key, key_type);
  3426. key.offset = filp->f_pos;
  3427. key.objectid = inode->i_ino;
  3428. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3429. if (ret < 0)
  3430. goto err;
  3431. advance = 0;
  3432. while (1) {
  3433. leaf = path->nodes[0];
  3434. nritems = btrfs_header_nritems(leaf);
  3435. slot = path->slots[0];
  3436. if (advance || slot >= nritems) {
  3437. if (slot >= nritems - 1) {
  3438. ret = btrfs_next_leaf(root, path);
  3439. if (ret)
  3440. break;
  3441. leaf = path->nodes[0];
  3442. nritems = btrfs_header_nritems(leaf);
  3443. slot = path->slots[0];
  3444. } else {
  3445. slot++;
  3446. path->slots[0]++;
  3447. }
  3448. }
  3449. advance = 1;
  3450. item = btrfs_item_nr(leaf, slot);
  3451. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3452. if (found_key.objectid != key.objectid)
  3453. break;
  3454. if (btrfs_key_type(&found_key) != key_type)
  3455. break;
  3456. if (found_key.offset < filp->f_pos)
  3457. continue;
  3458. filp->f_pos = found_key.offset;
  3459. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  3460. di_cur = 0;
  3461. di_total = btrfs_item_size(leaf, item);
  3462. while (di_cur < di_total) {
  3463. struct btrfs_key location;
  3464. name_len = btrfs_dir_name_len(leaf, di);
  3465. if (name_len <= sizeof(tmp_name)) {
  3466. name_ptr = tmp_name;
  3467. } else {
  3468. name_ptr = kmalloc(name_len, GFP_NOFS);
  3469. if (!name_ptr) {
  3470. ret = -ENOMEM;
  3471. goto err;
  3472. }
  3473. }
  3474. read_extent_buffer(leaf, name_ptr,
  3475. (unsigned long)(di + 1), name_len);
  3476. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  3477. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  3478. /* is this a reference to our own snapshot? If so
  3479. * skip it
  3480. */
  3481. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  3482. location.objectid == root->root_key.objectid) {
  3483. over = 0;
  3484. goto skip;
  3485. }
  3486. over = filldir(dirent, name_ptr, name_len,
  3487. found_key.offset, location.objectid,
  3488. d_type);
  3489. skip:
  3490. if (name_ptr != tmp_name)
  3491. kfree(name_ptr);
  3492. if (over)
  3493. goto nopos;
  3494. di_len = btrfs_dir_name_len(leaf, di) +
  3495. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  3496. di_cur += di_len;
  3497. di = (struct btrfs_dir_item *)((char *)di + di_len);
  3498. }
  3499. }
  3500. /* Reached end of directory/root. Bump pos past the last item. */
  3501. if (key_type == BTRFS_DIR_INDEX_KEY)
  3502. filp->f_pos = INT_LIMIT(off_t);
  3503. else
  3504. filp->f_pos++;
  3505. nopos:
  3506. ret = 0;
  3507. err:
  3508. btrfs_free_path(path);
  3509. return ret;
  3510. }
  3511. int btrfs_write_inode(struct inode *inode, int wait)
  3512. {
  3513. struct btrfs_root *root = BTRFS_I(inode)->root;
  3514. struct btrfs_trans_handle *trans;
  3515. int ret = 0;
  3516. if (root->fs_info->btree_inode == inode)
  3517. return 0;
  3518. if (wait) {
  3519. trans = btrfs_join_transaction(root, 1);
  3520. btrfs_set_trans_block_group(trans, inode);
  3521. ret = btrfs_commit_transaction(trans, root);
  3522. }
  3523. return ret;
  3524. }
  3525. /*
  3526. * This is somewhat expensive, updating the tree every time the
  3527. * inode changes. But, it is most likely to find the inode in cache.
  3528. * FIXME, needs more benchmarking...there are no reasons other than performance
  3529. * to keep or drop this code.
  3530. */
  3531. void btrfs_dirty_inode(struct inode *inode)
  3532. {
  3533. struct btrfs_root *root = BTRFS_I(inode)->root;
  3534. struct btrfs_trans_handle *trans;
  3535. trans = btrfs_join_transaction(root, 1);
  3536. btrfs_set_trans_block_group(trans, inode);
  3537. btrfs_update_inode(trans, root, inode);
  3538. btrfs_end_transaction(trans, root);
  3539. }
  3540. /*
  3541. * find the highest existing sequence number in a directory
  3542. * and then set the in-memory index_cnt variable to reflect
  3543. * free sequence numbers
  3544. */
  3545. static int btrfs_set_inode_index_count(struct inode *inode)
  3546. {
  3547. struct btrfs_root *root = BTRFS_I(inode)->root;
  3548. struct btrfs_key key, found_key;
  3549. struct btrfs_path *path;
  3550. struct extent_buffer *leaf;
  3551. int ret;
  3552. key.objectid = inode->i_ino;
  3553. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  3554. key.offset = (u64)-1;
  3555. path = btrfs_alloc_path();
  3556. if (!path)
  3557. return -ENOMEM;
  3558. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3559. if (ret < 0)
  3560. goto out;
  3561. /* FIXME: we should be able to handle this */
  3562. if (ret == 0)
  3563. goto out;
  3564. ret = 0;
  3565. /*
  3566. * MAGIC NUMBER EXPLANATION:
  3567. * since we search a directory based on f_pos we have to start at 2
  3568. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  3569. * else has to start at 2
  3570. */
  3571. if (path->slots[0] == 0) {
  3572. BTRFS_I(inode)->index_cnt = 2;
  3573. goto out;
  3574. }
  3575. path->slots[0]--;
  3576. leaf = path->nodes[0];
  3577. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3578. if (found_key.objectid != inode->i_ino ||
  3579. btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
  3580. BTRFS_I(inode)->index_cnt = 2;
  3581. goto out;
  3582. }
  3583. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  3584. out:
  3585. btrfs_free_path(path);
  3586. return ret;
  3587. }
  3588. /*
  3589. * helper to find a free sequence number in a given directory. This current
  3590. * code is very simple, later versions will do smarter things in the btree
  3591. */
  3592. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  3593. {
  3594. int ret = 0;
  3595. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  3596. ret = btrfs_set_inode_index_count(dir);
  3597. if (ret)
  3598. return ret;
  3599. }
  3600. *index = BTRFS_I(dir)->index_cnt;
  3601. BTRFS_I(dir)->index_cnt++;
  3602. return ret;
  3603. }
  3604. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  3605. struct btrfs_root *root,
  3606. struct inode *dir,
  3607. const char *name, int name_len,
  3608. u64 ref_objectid, u64 objectid,
  3609. u64 alloc_hint, int mode, u64 *index)
  3610. {
  3611. struct inode *inode;
  3612. struct btrfs_inode_item *inode_item;
  3613. struct btrfs_key *location;
  3614. struct btrfs_path *path;
  3615. struct btrfs_inode_ref *ref;
  3616. struct btrfs_key key[2];
  3617. u32 sizes[2];
  3618. unsigned long ptr;
  3619. int ret;
  3620. int owner;
  3621. path = btrfs_alloc_path();
  3622. BUG_ON(!path);
  3623. inode = new_inode(root->fs_info->sb);
  3624. if (!inode)
  3625. return ERR_PTR(-ENOMEM);
  3626. if (dir) {
  3627. ret = btrfs_set_inode_index(dir, index);
  3628. if (ret) {
  3629. iput(inode);
  3630. return ERR_PTR(ret);
  3631. }
  3632. }
  3633. /*
  3634. * index_cnt is ignored for everything but a dir,
  3635. * btrfs_get_inode_index_count has an explanation for the magic
  3636. * number
  3637. */
  3638. init_btrfs_i(inode);
  3639. BTRFS_I(inode)->index_cnt = 2;
  3640. BTRFS_I(inode)->root = root;
  3641. BTRFS_I(inode)->generation = trans->transid;
  3642. btrfs_set_inode_space_info(root, inode);
  3643. if (mode & S_IFDIR)
  3644. owner = 0;
  3645. else
  3646. owner = 1;
  3647. BTRFS_I(inode)->block_group =
  3648. btrfs_find_block_group(root, 0, alloc_hint, owner);
  3649. key[0].objectid = objectid;
  3650. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  3651. key[0].offset = 0;
  3652. key[1].objectid = objectid;
  3653. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  3654. key[1].offset = ref_objectid;
  3655. sizes[0] = sizeof(struct btrfs_inode_item);
  3656. sizes[1] = name_len + sizeof(*ref);
  3657. path->leave_spinning = 1;
  3658. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  3659. if (ret != 0)
  3660. goto fail;
  3661. inode->i_uid = current_fsuid();
  3662. if (dir && (dir->i_mode & S_ISGID)) {
  3663. inode->i_gid = dir->i_gid;
  3664. if (S_ISDIR(mode))
  3665. mode |= S_ISGID;
  3666. } else
  3667. inode->i_gid = current_fsgid();
  3668. inode->i_mode = mode;
  3669. inode->i_ino = objectid;
  3670. inode_set_bytes(inode, 0);
  3671. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3672. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3673. struct btrfs_inode_item);
  3674. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  3675. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  3676. struct btrfs_inode_ref);
  3677. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  3678. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  3679. ptr = (unsigned long)(ref + 1);
  3680. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  3681. btrfs_mark_buffer_dirty(path->nodes[0]);
  3682. btrfs_free_path(path);
  3683. location = &BTRFS_I(inode)->location;
  3684. location->objectid = objectid;
  3685. location->offset = 0;
  3686. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  3687. btrfs_inherit_iflags(inode, dir);
  3688. if ((mode & S_IFREG)) {
  3689. if (btrfs_test_opt(root, NODATASUM))
  3690. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  3691. if (btrfs_test_opt(root, NODATACOW))
  3692. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  3693. }
  3694. insert_inode_hash(inode);
  3695. inode_tree_add(inode);
  3696. return inode;
  3697. fail:
  3698. if (dir)
  3699. BTRFS_I(dir)->index_cnt--;
  3700. btrfs_free_path(path);
  3701. iput(inode);
  3702. return ERR_PTR(ret);
  3703. }
  3704. static inline u8 btrfs_inode_type(struct inode *inode)
  3705. {
  3706. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  3707. }
  3708. /*
  3709. * utility function to add 'inode' into 'parent_inode' with
  3710. * a give name and a given sequence number.
  3711. * if 'add_backref' is true, also insert a backref from the
  3712. * inode to the parent directory.
  3713. */
  3714. int btrfs_add_link(struct btrfs_trans_handle *trans,
  3715. struct inode *parent_inode, struct inode *inode,
  3716. const char *name, int name_len, int add_backref, u64 index)
  3717. {
  3718. int ret = 0;
  3719. struct btrfs_key key;
  3720. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  3721. if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  3722. memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
  3723. } else {
  3724. key.objectid = inode->i_ino;
  3725. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  3726. key.offset = 0;
  3727. }
  3728. if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  3729. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  3730. key.objectid, root->root_key.objectid,
  3731. parent_inode->i_ino,
  3732. index, name, name_len);
  3733. } else if (add_backref) {
  3734. ret = btrfs_insert_inode_ref(trans, root,
  3735. name, name_len, inode->i_ino,
  3736. parent_inode->i_ino, index);
  3737. }
  3738. if (ret == 0) {
  3739. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  3740. parent_inode->i_ino, &key,
  3741. btrfs_inode_type(inode), index);
  3742. BUG_ON(ret);
  3743. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  3744. name_len * 2);
  3745. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  3746. ret = btrfs_update_inode(trans, root, parent_inode);
  3747. }
  3748. return ret;
  3749. }
  3750. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  3751. struct dentry *dentry, struct inode *inode,
  3752. int backref, u64 index)
  3753. {
  3754. int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  3755. inode, dentry->d_name.name,
  3756. dentry->d_name.len, backref, index);
  3757. if (!err) {
  3758. d_instantiate(dentry, inode);
  3759. return 0;
  3760. }
  3761. if (err > 0)
  3762. err = -EEXIST;
  3763. return err;
  3764. }
  3765. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  3766. int mode, dev_t rdev)
  3767. {
  3768. struct btrfs_trans_handle *trans;
  3769. struct btrfs_root *root = BTRFS_I(dir)->root;
  3770. struct inode *inode = NULL;
  3771. int err;
  3772. int drop_inode = 0;
  3773. u64 objectid;
  3774. unsigned long nr = 0;
  3775. u64 index = 0;
  3776. if (!new_valid_dev(rdev))
  3777. return -EINVAL;
  3778. /*
  3779. * 2 for inode item and ref
  3780. * 2 for dir items
  3781. * 1 for xattr if selinux is on
  3782. */
  3783. err = btrfs_reserve_metadata_space(root, 5);
  3784. if (err)
  3785. return err;
  3786. trans = btrfs_start_transaction(root, 1);
  3787. if (!trans)
  3788. goto fail;
  3789. btrfs_set_trans_block_group(trans, dir);
  3790. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3791. if (err) {
  3792. err = -ENOSPC;
  3793. goto out_unlock;
  3794. }
  3795. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3796. dentry->d_name.len,
  3797. dentry->d_parent->d_inode->i_ino, objectid,
  3798. BTRFS_I(dir)->block_group, mode, &index);
  3799. err = PTR_ERR(inode);
  3800. if (IS_ERR(inode))
  3801. goto out_unlock;
  3802. err = btrfs_init_inode_security(trans, inode, dir);
  3803. if (err) {
  3804. drop_inode = 1;
  3805. goto out_unlock;
  3806. }
  3807. btrfs_set_trans_block_group(trans, inode);
  3808. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  3809. if (err)
  3810. drop_inode = 1;
  3811. else {
  3812. inode->i_op = &btrfs_special_inode_operations;
  3813. init_special_inode(inode, inode->i_mode, rdev);
  3814. btrfs_update_inode(trans, root, inode);
  3815. }
  3816. btrfs_update_inode_block_group(trans, inode);
  3817. btrfs_update_inode_block_group(trans, dir);
  3818. out_unlock:
  3819. nr = trans->blocks_used;
  3820. btrfs_end_transaction_throttle(trans, root);
  3821. fail:
  3822. btrfs_unreserve_metadata_space(root, 5);
  3823. if (drop_inode) {
  3824. inode_dec_link_count(inode);
  3825. iput(inode);
  3826. }
  3827. btrfs_btree_balance_dirty(root, nr);
  3828. return err;
  3829. }
  3830. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  3831. int mode, struct nameidata *nd)
  3832. {
  3833. struct btrfs_trans_handle *trans;
  3834. struct btrfs_root *root = BTRFS_I(dir)->root;
  3835. struct inode *inode = NULL;
  3836. int err;
  3837. int drop_inode = 0;
  3838. unsigned long nr = 0;
  3839. u64 objectid;
  3840. u64 index = 0;
  3841. /*
  3842. * 2 for inode item and ref
  3843. * 2 for dir items
  3844. * 1 for xattr if selinux is on
  3845. */
  3846. err = btrfs_reserve_metadata_space(root, 5);
  3847. if (err)
  3848. return err;
  3849. trans = btrfs_start_transaction(root, 1);
  3850. if (!trans)
  3851. goto fail;
  3852. btrfs_set_trans_block_group(trans, dir);
  3853. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3854. if (err) {
  3855. err = -ENOSPC;
  3856. goto out_unlock;
  3857. }
  3858. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3859. dentry->d_name.len,
  3860. dentry->d_parent->d_inode->i_ino,
  3861. objectid, BTRFS_I(dir)->block_group, mode,
  3862. &index);
  3863. err = PTR_ERR(inode);
  3864. if (IS_ERR(inode))
  3865. goto out_unlock;
  3866. err = btrfs_init_inode_security(trans, inode, dir);
  3867. if (err) {
  3868. drop_inode = 1;
  3869. goto out_unlock;
  3870. }
  3871. btrfs_set_trans_block_group(trans, inode);
  3872. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  3873. if (err)
  3874. drop_inode = 1;
  3875. else {
  3876. inode->i_mapping->a_ops = &btrfs_aops;
  3877. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  3878. inode->i_fop = &btrfs_file_operations;
  3879. inode->i_op = &btrfs_file_inode_operations;
  3880. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  3881. }
  3882. btrfs_update_inode_block_group(trans, inode);
  3883. btrfs_update_inode_block_group(trans, dir);
  3884. out_unlock:
  3885. nr = trans->blocks_used;
  3886. btrfs_end_transaction_throttle(trans, root);
  3887. fail:
  3888. btrfs_unreserve_metadata_space(root, 5);
  3889. if (drop_inode) {
  3890. inode_dec_link_count(inode);
  3891. iput(inode);
  3892. }
  3893. btrfs_btree_balance_dirty(root, nr);
  3894. return err;
  3895. }
  3896. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  3897. struct dentry *dentry)
  3898. {
  3899. struct btrfs_trans_handle *trans;
  3900. struct btrfs_root *root = BTRFS_I(dir)->root;
  3901. struct inode *inode = old_dentry->d_inode;
  3902. u64 index;
  3903. unsigned long nr = 0;
  3904. int err;
  3905. int drop_inode = 0;
  3906. if (inode->i_nlink == 0)
  3907. return -ENOENT;
  3908. /* do not allow sys_link's with other subvols of the same device */
  3909. if (root->objectid != BTRFS_I(inode)->root->objectid)
  3910. return -EPERM;
  3911. /*
  3912. * 1 item for inode ref
  3913. * 2 items for dir items
  3914. */
  3915. err = btrfs_reserve_metadata_space(root, 3);
  3916. if (err)
  3917. return err;
  3918. btrfs_inc_nlink(inode);
  3919. err = btrfs_set_inode_index(dir, &index);
  3920. if (err)
  3921. goto fail;
  3922. trans = btrfs_start_transaction(root, 1);
  3923. btrfs_set_trans_block_group(trans, dir);
  3924. atomic_inc(&inode->i_count);
  3925. err = btrfs_add_nondir(trans, dentry, inode, 1, index);
  3926. if (err) {
  3927. drop_inode = 1;
  3928. } else {
  3929. btrfs_update_inode_block_group(trans, dir);
  3930. err = btrfs_update_inode(trans, root, inode);
  3931. BUG_ON(err);
  3932. btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
  3933. }
  3934. nr = trans->blocks_used;
  3935. btrfs_end_transaction_throttle(trans, root);
  3936. fail:
  3937. btrfs_unreserve_metadata_space(root, 3);
  3938. if (drop_inode) {
  3939. inode_dec_link_count(inode);
  3940. iput(inode);
  3941. }
  3942. btrfs_btree_balance_dirty(root, nr);
  3943. return err;
  3944. }
  3945. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  3946. {
  3947. struct inode *inode = NULL;
  3948. struct btrfs_trans_handle *trans;
  3949. struct btrfs_root *root = BTRFS_I(dir)->root;
  3950. int err = 0;
  3951. int drop_on_err = 0;
  3952. u64 objectid = 0;
  3953. u64 index = 0;
  3954. unsigned long nr = 1;
  3955. /*
  3956. * 2 items for inode and ref
  3957. * 2 items for dir items
  3958. * 1 for xattr if selinux is on
  3959. */
  3960. err = btrfs_reserve_metadata_space(root, 5);
  3961. if (err)
  3962. return err;
  3963. trans = btrfs_start_transaction(root, 1);
  3964. if (!trans) {
  3965. err = -ENOMEM;
  3966. goto out_unlock;
  3967. }
  3968. btrfs_set_trans_block_group(trans, dir);
  3969. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3970. if (err) {
  3971. err = -ENOSPC;
  3972. goto out_unlock;
  3973. }
  3974. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3975. dentry->d_name.len,
  3976. dentry->d_parent->d_inode->i_ino, objectid,
  3977. BTRFS_I(dir)->block_group, S_IFDIR | mode,
  3978. &index);
  3979. if (IS_ERR(inode)) {
  3980. err = PTR_ERR(inode);
  3981. goto out_fail;
  3982. }
  3983. drop_on_err = 1;
  3984. err = btrfs_init_inode_security(trans, inode, dir);
  3985. if (err)
  3986. goto out_fail;
  3987. inode->i_op = &btrfs_dir_inode_operations;
  3988. inode->i_fop = &btrfs_dir_file_operations;
  3989. btrfs_set_trans_block_group(trans, inode);
  3990. btrfs_i_size_write(inode, 0);
  3991. err = btrfs_update_inode(trans, root, inode);
  3992. if (err)
  3993. goto out_fail;
  3994. err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  3995. inode, dentry->d_name.name,
  3996. dentry->d_name.len, 0, index);
  3997. if (err)
  3998. goto out_fail;
  3999. d_instantiate(dentry, inode);
  4000. drop_on_err = 0;
  4001. btrfs_update_inode_block_group(trans, inode);
  4002. btrfs_update_inode_block_group(trans, dir);
  4003. out_fail:
  4004. nr = trans->blocks_used;
  4005. btrfs_end_transaction_throttle(trans, root);
  4006. out_unlock:
  4007. btrfs_unreserve_metadata_space(root, 5);
  4008. if (drop_on_err)
  4009. iput(inode);
  4010. btrfs_btree_balance_dirty(root, nr);
  4011. return err;
  4012. }
  4013. /* helper for btfs_get_extent. Given an existing extent in the tree,
  4014. * and an extent that you want to insert, deal with overlap and insert
  4015. * the new extent into the tree.
  4016. */
  4017. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  4018. struct extent_map *existing,
  4019. struct extent_map *em,
  4020. u64 map_start, u64 map_len)
  4021. {
  4022. u64 start_diff;
  4023. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  4024. start_diff = map_start - em->start;
  4025. em->start = map_start;
  4026. em->len = map_len;
  4027. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  4028. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  4029. em->block_start += start_diff;
  4030. em->block_len -= start_diff;
  4031. }
  4032. return add_extent_mapping(em_tree, em);
  4033. }
  4034. static noinline int uncompress_inline(struct btrfs_path *path,
  4035. struct inode *inode, struct page *page,
  4036. size_t pg_offset, u64 extent_offset,
  4037. struct btrfs_file_extent_item *item)
  4038. {
  4039. int ret;
  4040. struct extent_buffer *leaf = path->nodes[0];
  4041. char *tmp;
  4042. size_t max_size;
  4043. unsigned long inline_size;
  4044. unsigned long ptr;
  4045. WARN_ON(pg_offset != 0);
  4046. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  4047. inline_size = btrfs_file_extent_inline_item_len(leaf,
  4048. btrfs_item_nr(leaf, path->slots[0]));
  4049. tmp = kmalloc(inline_size, GFP_NOFS);
  4050. ptr = btrfs_file_extent_inline_start(item);
  4051. read_extent_buffer(leaf, tmp, ptr, inline_size);
  4052. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  4053. ret = btrfs_zlib_decompress(tmp, page, extent_offset,
  4054. inline_size, max_size);
  4055. if (ret) {
  4056. char *kaddr = kmap_atomic(page, KM_USER0);
  4057. unsigned long copy_size = min_t(u64,
  4058. PAGE_CACHE_SIZE - pg_offset,
  4059. max_size - extent_offset);
  4060. memset(kaddr + pg_offset, 0, copy_size);
  4061. kunmap_atomic(kaddr, KM_USER0);
  4062. }
  4063. kfree(tmp);
  4064. return 0;
  4065. }
  4066. /*
  4067. * a bit scary, this does extent mapping from logical file offset to the disk.
  4068. * the ugly parts come from merging extents from the disk with the in-ram
  4069. * representation. This gets more complex because of the data=ordered code,
  4070. * where the in-ram extents might be locked pending data=ordered completion.
  4071. *
  4072. * This also copies inline extents directly into the page.
  4073. */
  4074. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  4075. size_t pg_offset, u64 start, u64 len,
  4076. int create)
  4077. {
  4078. int ret;
  4079. int err = 0;
  4080. u64 bytenr;
  4081. u64 extent_start = 0;
  4082. u64 extent_end = 0;
  4083. u64 objectid = inode->i_ino;
  4084. u32 found_type;
  4085. struct btrfs_path *path = NULL;
  4086. struct btrfs_root *root = BTRFS_I(inode)->root;
  4087. struct btrfs_file_extent_item *item;
  4088. struct extent_buffer *leaf;
  4089. struct btrfs_key found_key;
  4090. struct extent_map *em = NULL;
  4091. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4092. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4093. struct btrfs_trans_handle *trans = NULL;
  4094. int compressed;
  4095. again:
  4096. read_lock(&em_tree->lock);
  4097. em = lookup_extent_mapping(em_tree, start, len);
  4098. if (em)
  4099. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4100. read_unlock(&em_tree->lock);
  4101. if (em) {
  4102. if (em->start > start || em->start + em->len <= start)
  4103. free_extent_map(em);
  4104. else if (em->block_start == EXTENT_MAP_INLINE && page)
  4105. free_extent_map(em);
  4106. else
  4107. goto out;
  4108. }
  4109. em = alloc_extent_map(GFP_NOFS);
  4110. if (!em) {
  4111. err = -ENOMEM;
  4112. goto out;
  4113. }
  4114. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4115. em->start = EXTENT_MAP_HOLE;
  4116. em->orig_start = EXTENT_MAP_HOLE;
  4117. em->len = (u64)-1;
  4118. em->block_len = (u64)-1;
  4119. if (!path) {
  4120. path = btrfs_alloc_path();
  4121. BUG_ON(!path);
  4122. }
  4123. ret = btrfs_lookup_file_extent(trans, root, path,
  4124. objectid, start, trans != NULL);
  4125. if (ret < 0) {
  4126. err = ret;
  4127. goto out;
  4128. }
  4129. if (ret != 0) {
  4130. if (path->slots[0] == 0)
  4131. goto not_found;
  4132. path->slots[0]--;
  4133. }
  4134. leaf = path->nodes[0];
  4135. item = btrfs_item_ptr(leaf, path->slots[0],
  4136. struct btrfs_file_extent_item);
  4137. /* are we inside the extent that was found? */
  4138. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4139. found_type = btrfs_key_type(&found_key);
  4140. if (found_key.objectid != objectid ||
  4141. found_type != BTRFS_EXTENT_DATA_KEY) {
  4142. goto not_found;
  4143. }
  4144. found_type = btrfs_file_extent_type(leaf, item);
  4145. extent_start = found_key.offset;
  4146. compressed = btrfs_file_extent_compression(leaf, item);
  4147. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4148. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4149. extent_end = extent_start +
  4150. btrfs_file_extent_num_bytes(leaf, item);
  4151. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4152. size_t size;
  4153. size = btrfs_file_extent_inline_len(leaf, item);
  4154. extent_end = (extent_start + size + root->sectorsize - 1) &
  4155. ~((u64)root->sectorsize - 1);
  4156. }
  4157. if (start >= extent_end) {
  4158. path->slots[0]++;
  4159. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  4160. ret = btrfs_next_leaf(root, path);
  4161. if (ret < 0) {
  4162. err = ret;
  4163. goto out;
  4164. }
  4165. if (ret > 0)
  4166. goto not_found;
  4167. leaf = path->nodes[0];
  4168. }
  4169. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4170. if (found_key.objectid != objectid ||
  4171. found_key.type != BTRFS_EXTENT_DATA_KEY)
  4172. goto not_found;
  4173. if (start + len <= found_key.offset)
  4174. goto not_found;
  4175. em->start = start;
  4176. em->len = found_key.offset - start;
  4177. goto not_found_em;
  4178. }
  4179. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4180. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4181. em->start = extent_start;
  4182. em->len = extent_end - extent_start;
  4183. em->orig_start = extent_start -
  4184. btrfs_file_extent_offset(leaf, item);
  4185. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  4186. if (bytenr == 0) {
  4187. em->block_start = EXTENT_MAP_HOLE;
  4188. goto insert;
  4189. }
  4190. if (compressed) {
  4191. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4192. em->block_start = bytenr;
  4193. em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
  4194. item);
  4195. } else {
  4196. bytenr += btrfs_file_extent_offset(leaf, item);
  4197. em->block_start = bytenr;
  4198. em->block_len = em->len;
  4199. if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
  4200. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  4201. }
  4202. goto insert;
  4203. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4204. unsigned long ptr;
  4205. char *map;
  4206. size_t size;
  4207. size_t extent_offset;
  4208. size_t copy_size;
  4209. em->block_start = EXTENT_MAP_INLINE;
  4210. if (!page || create) {
  4211. em->start = extent_start;
  4212. em->len = extent_end - extent_start;
  4213. goto out;
  4214. }
  4215. size = btrfs_file_extent_inline_len(leaf, item);
  4216. extent_offset = page_offset(page) + pg_offset - extent_start;
  4217. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  4218. size - extent_offset);
  4219. em->start = extent_start + extent_offset;
  4220. em->len = (copy_size + root->sectorsize - 1) &
  4221. ~((u64)root->sectorsize - 1);
  4222. em->orig_start = EXTENT_MAP_INLINE;
  4223. if (compressed)
  4224. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4225. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  4226. if (create == 0 && !PageUptodate(page)) {
  4227. if (btrfs_file_extent_compression(leaf, item) ==
  4228. BTRFS_COMPRESS_ZLIB) {
  4229. ret = uncompress_inline(path, inode, page,
  4230. pg_offset,
  4231. extent_offset, item);
  4232. BUG_ON(ret);
  4233. } else {
  4234. map = kmap(page);
  4235. read_extent_buffer(leaf, map + pg_offset, ptr,
  4236. copy_size);
  4237. if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
  4238. memset(map + pg_offset + copy_size, 0,
  4239. PAGE_CACHE_SIZE - pg_offset -
  4240. copy_size);
  4241. }
  4242. kunmap(page);
  4243. }
  4244. flush_dcache_page(page);
  4245. } else if (create && PageUptodate(page)) {
  4246. if (!trans) {
  4247. kunmap(page);
  4248. free_extent_map(em);
  4249. em = NULL;
  4250. btrfs_release_path(root, path);
  4251. trans = btrfs_join_transaction(root, 1);
  4252. goto again;
  4253. }
  4254. map = kmap(page);
  4255. write_extent_buffer(leaf, map + pg_offset, ptr,
  4256. copy_size);
  4257. kunmap(page);
  4258. btrfs_mark_buffer_dirty(leaf);
  4259. }
  4260. set_extent_uptodate(io_tree, em->start,
  4261. extent_map_end(em) - 1, GFP_NOFS);
  4262. goto insert;
  4263. } else {
  4264. printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
  4265. WARN_ON(1);
  4266. }
  4267. not_found:
  4268. em->start = start;
  4269. em->len = len;
  4270. not_found_em:
  4271. em->block_start = EXTENT_MAP_HOLE;
  4272. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  4273. insert:
  4274. btrfs_release_path(root, path);
  4275. if (em->start > start || extent_map_end(em) <= start) {
  4276. printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
  4277. "[%llu %llu]\n", (unsigned long long)em->start,
  4278. (unsigned long long)em->len,
  4279. (unsigned long long)start,
  4280. (unsigned long long)len);
  4281. err = -EIO;
  4282. goto out;
  4283. }
  4284. err = 0;
  4285. write_lock(&em_tree->lock);
  4286. ret = add_extent_mapping(em_tree, em);
  4287. /* it is possible that someone inserted the extent into the tree
  4288. * while we had the lock dropped. It is also possible that
  4289. * an overlapping map exists in the tree
  4290. */
  4291. if (ret == -EEXIST) {
  4292. struct extent_map *existing;
  4293. ret = 0;
  4294. existing = lookup_extent_mapping(em_tree, start, len);
  4295. if (existing && (existing->start > start ||
  4296. existing->start + existing->len <= start)) {
  4297. free_extent_map(existing);
  4298. existing = NULL;
  4299. }
  4300. if (!existing) {
  4301. existing = lookup_extent_mapping(em_tree, em->start,
  4302. em->len);
  4303. if (existing) {
  4304. err = merge_extent_mapping(em_tree, existing,
  4305. em, start,
  4306. root->sectorsize);
  4307. free_extent_map(existing);
  4308. if (err) {
  4309. free_extent_map(em);
  4310. em = NULL;
  4311. }
  4312. } else {
  4313. err = -EIO;
  4314. free_extent_map(em);
  4315. em = NULL;
  4316. }
  4317. } else {
  4318. free_extent_map(em);
  4319. em = existing;
  4320. err = 0;
  4321. }
  4322. }
  4323. write_unlock(&em_tree->lock);
  4324. out:
  4325. if (path)
  4326. btrfs_free_path(path);
  4327. if (trans) {
  4328. ret = btrfs_end_transaction(trans, root);
  4329. if (!err)
  4330. err = ret;
  4331. }
  4332. if (err) {
  4333. free_extent_map(em);
  4334. return ERR_PTR(err);
  4335. }
  4336. return em;
  4337. }
  4338. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  4339. const struct iovec *iov, loff_t offset,
  4340. unsigned long nr_segs)
  4341. {
  4342. return -EINVAL;
  4343. }
  4344. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  4345. __u64 start, __u64 len)
  4346. {
  4347. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
  4348. }
  4349. int btrfs_readpage(struct file *file, struct page *page)
  4350. {
  4351. struct extent_io_tree *tree;
  4352. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4353. return extent_read_full_page(tree, page, btrfs_get_extent);
  4354. }
  4355. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  4356. {
  4357. struct extent_io_tree *tree;
  4358. if (current->flags & PF_MEMALLOC) {
  4359. redirty_page_for_writepage(wbc, page);
  4360. unlock_page(page);
  4361. return 0;
  4362. }
  4363. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4364. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  4365. }
  4366. int btrfs_writepages(struct address_space *mapping,
  4367. struct writeback_control *wbc)
  4368. {
  4369. struct extent_io_tree *tree;
  4370. tree = &BTRFS_I(mapping->host)->io_tree;
  4371. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  4372. }
  4373. static int
  4374. btrfs_readpages(struct file *file, struct address_space *mapping,
  4375. struct list_head *pages, unsigned nr_pages)
  4376. {
  4377. struct extent_io_tree *tree;
  4378. tree = &BTRFS_I(mapping->host)->io_tree;
  4379. return extent_readpages(tree, mapping, pages, nr_pages,
  4380. btrfs_get_extent);
  4381. }
  4382. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  4383. {
  4384. struct extent_io_tree *tree;
  4385. struct extent_map_tree *map;
  4386. int ret;
  4387. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4388. map = &BTRFS_I(page->mapping->host)->extent_tree;
  4389. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  4390. if (ret == 1) {
  4391. ClearPagePrivate(page);
  4392. set_page_private(page, 0);
  4393. page_cache_release(page);
  4394. }
  4395. return ret;
  4396. }
  4397. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  4398. {
  4399. if (PageWriteback(page) || PageDirty(page))
  4400. return 0;
  4401. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  4402. }
  4403. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  4404. {
  4405. struct extent_io_tree *tree;
  4406. struct btrfs_ordered_extent *ordered;
  4407. u64 page_start = page_offset(page);
  4408. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  4409. /*
  4410. * we have the page locked, so new writeback can't start,
  4411. * and the dirty bit won't be cleared while we are here.
  4412. *
  4413. * Wait for IO on this page so that we can safely clear
  4414. * the PagePrivate2 bit and do ordered accounting
  4415. */
  4416. wait_on_page_writeback(page);
  4417. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4418. if (offset) {
  4419. btrfs_releasepage(page, GFP_NOFS);
  4420. return;
  4421. }
  4422. lock_extent(tree, page_start, page_end, GFP_NOFS);
  4423. ordered = btrfs_lookup_ordered_extent(page->mapping->host,
  4424. page_offset(page));
  4425. if (ordered) {
  4426. /*
  4427. * IO on this page will never be started, so we need
  4428. * to account for any ordered extents now
  4429. */
  4430. clear_extent_bit(tree, page_start, page_end,
  4431. EXTENT_DIRTY | EXTENT_DELALLOC |
  4432. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
  4433. NULL, GFP_NOFS);
  4434. /*
  4435. * whoever cleared the private bit is responsible
  4436. * for the finish_ordered_io
  4437. */
  4438. if (TestClearPagePrivate2(page)) {
  4439. btrfs_finish_ordered_io(page->mapping->host,
  4440. page_start, page_end);
  4441. }
  4442. btrfs_put_ordered_extent(ordered);
  4443. lock_extent(tree, page_start, page_end, GFP_NOFS);
  4444. }
  4445. clear_extent_bit(tree, page_start, page_end,
  4446. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  4447. EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS);
  4448. __btrfs_releasepage(page, GFP_NOFS);
  4449. ClearPageChecked(page);
  4450. if (PagePrivate(page)) {
  4451. ClearPagePrivate(page);
  4452. set_page_private(page, 0);
  4453. page_cache_release(page);
  4454. }
  4455. }
  4456. /*
  4457. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  4458. * called from a page fault handler when a page is first dirtied. Hence we must
  4459. * be careful to check for EOF conditions here. We set the page up correctly
  4460. * for a written page which means we get ENOSPC checking when writing into
  4461. * holes and correct delalloc and unwritten extent mapping on filesystems that
  4462. * support these features.
  4463. *
  4464. * We are not allowed to take the i_mutex here so we have to play games to
  4465. * protect against truncate races as the page could now be beyond EOF. Because
  4466. * vmtruncate() writes the inode size before removing pages, once we have the
  4467. * page lock we can determine safely if the page is beyond EOF. If it is not
  4468. * beyond EOF, then the page is guaranteed safe against truncation until we
  4469. * unlock the page.
  4470. */
  4471. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  4472. {
  4473. struct page *page = vmf->page;
  4474. struct inode *inode = fdentry(vma->vm_file)->d_inode;
  4475. struct btrfs_root *root = BTRFS_I(inode)->root;
  4476. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4477. struct btrfs_ordered_extent *ordered;
  4478. char *kaddr;
  4479. unsigned long zero_start;
  4480. loff_t size;
  4481. int ret;
  4482. u64 page_start;
  4483. u64 page_end;
  4484. ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
  4485. if (ret) {
  4486. if (ret == -ENOMEM)
  4487. ret = VM_FAULT_OOM;
  4488. else /* -ENOSPC, -EIO, etc */
  4489. ret = VM_FAULT_SIGBUS;
  4490. goto out;
  4491. }
  4492. ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
  4493. if (ret) {
  4494. btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
  4495. ret = VM_FAULT_SIGBUS;
  4496. goto out;
  4497. }
  4498. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  4499. again:
  4500. lock_page(page);
  4501. size = i_size_read(inode);
  4502. page_start = page_offset(page);
  4503. page_end = page_start + PAGE_CACHE_SIZE - 1;
  4504. if ((page->mapping != inode->i_mapping) ||
  4505. (page_start >= size)) {
  4506. btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
  4507. /* page got truncated out from underneath us */
  4508. goto out_unlock;
  4509. }
  4510. wait_on_page_writeback(page);
  4511. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  4512. set_page_extent_mapped(page);
  4513. /*
  4514. * we can't set the delalloc bits if there are pending ordered
  4515. * extents. Drop our locks and wait for them to finish
  4516. */
  4517. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  4518. if (ordered) {
  4519. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  4520. unlock_page(page);
  4521. btrfs_start_ordered_extent(inode, ordered, 1);
  4522. btrfs_put_ordered_extent(ordered);
  4523. goto again;
  4524. }
  4525. /*
  4526. * XXX - page_mkwrite gets called every time the page is dirtied, even
  4527. * if it was already dirty, so for space accounting reasons we need to
  4528. * clear any delalloc bits for the range we are fixing to save. There
  4529. * is probably a better way to do this, but for now keep consistent with
  4530. * prepare_pages in the normal write path.
  4531. */
  4532. clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
  4533. EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
  4534. GFP_NOFS);
  4535. ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
  4536. if (ret) {
  4537. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  4538. ret = VM_FAULT_SIGBUS;
  4539. btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
  4540. goto out_unlock;
  4541. }
  4542. ret = 0;
  4543. /* page is wholly or partially inside EOF */
  4544. if (page_start + PAGE_CACHE_SIZE > size)
  4545. zero_start = size & ~PAGE_CACHE_MASK;
  4546. else
  4547. zero_start = PAGE_CACHE_SIZE;
  4548. if (zero_start != PAGE_CACHE_SIZE) {
  4549. kaddr = kmap(page);
  4550. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  4551. flush_dcache_page(page);
  4552. kunmap(page);
  4553. }
  4554. ClearPageChecked(page);
  4555. set_page_dirty(page);
  4556. SetPageUptodate(page);
  4557. BTRFS_I(inode)->last_trans = root->fs_info->generation;
  4558. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  4559. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  4560. out_unlock:
  4561. btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
  4562. if (!ret)
  4563. return VM_FAULT_LOCKED;
  4564. unlock_page(page);
  4565. out:
  4566. return ret;
  4567. }
  4568. static void btrfs_truncate(struct inode *inode)
  4569. {
  4570. struct btrfs_root *root = BTRFS_I(inode)->root;
  4571. int ret;
  4572. struct btrfs_trans_handle *trans;
  4573. unsigned long nr;
  4574. u64 mask = root->sectorsize - 1;
  4575. if (!S_ISREG(inode->i_mode)) {
  4576. WARN_ON(1);
  4577. return;
  4578. }
  4579. ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
  4580. if (ret)
  4581. return;
  4582. btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
  4583. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  4584. trans = btrfs_start_transaction(root, 1);
  4585. btrfs_set_trans_block_group(trans, inode);
  4586. /*
  4587. * setattr is responsible for setting the ordered_data_close flag,
  4588. * but that is only tested during the last file release. That
  4589. * could happen well after the next commit, leaving a great big
  4590. * window where new writes may get lost if someone chooses to write
  4591. * to this file after truncating to zero
  4592. *
  4593. * The inode doesn't have any dirty data here, and so if we commit
  4594. * this is a noop. If someone immediately starts writing to the inode
  4595. * it is very likely we'll catch some of their writes in this
  4596. * transaction, and the commit will find this file on the ordered
  4597. * data list with good things to send down.
  4598. *
  4599. * This is a best effort solution, there is still a window where
  4600. * using truncate to replace the contents of the file will
  4601. * end up with a zero length file after a crash.
  4602. */
  4603. if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
  4604. btrfs_add_ordered_operation(trans, root, inode);
  4605. while (1) {
  4606. ret = btrfs_truncate_inode_items(trans, root, inode,
  4607. inode->i_size,
  4608. BTRFS_EXTENT_DATA_KEY);
  4609. if (ret != -EAGAIN)
  4610. break;
  4611. ret = btrfs_update_inode(trans, root, inode);
  4612. BUG_ON(ret);
  4613. nr = trans->blocks_used;
  4614. btrfs_end_transaction(trans, root);
  4615. btrfs_btree_balance_dirty(root, nr);
  4616. trans = btrfs_start_transaction(root, 1);
  4617. btrfs_set_trans_block_group(trans, inode);
  4618. }
  4619. if (ret == 0 && inode->i_nlink > 0) {
  4620. ret = btrfs_orphan_del(trans, inode);
  4621. BUG_ON(ret);
  4622. }
  4623. ret = btrfs_update_inode(trans, root, inode);
  4624. BUG_ON(ret);
  4625. nr = trans->blocks_used;
  4626. ret = btrfs_end_transaction_throttle(trans, root);
  4627. BUG_ON(ret);
  4628. btrfs_btree_balance_dirty(root, nr);
  4629. }
  4630. /*
  4631. * create a new subvolume directory/inode (helper for the ioctl).
  4632. */
  4633. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  4634. struct btrfs_root *new_root,
  4635. u64 new_dirid, u64 alloc_hint)
  4636. {
  4637. struct inode *inode;
  4638. int err;
  4639. u64 index = 0;
  4640. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
  4641. new_dirid, alloc_hint, S_IFDIR | 0700, &index);
  4642. if (IS_ERR(inode))
  4643. return PTR_ERR(inode);
  4644. inode->i_op = &btrfs_dir_inode_operations;
  4645. inode->i_fop = &btrfs_dir_file_operations;
  4646. inode->i_nlink = 1;
  4647. btrfs_i_size_write(inode, 0);
  4648. err = btrfs_update_inode(trans, new_root, inode);
  4649. BUG_ON(err);
  4650. iput(inode);
  4651. return 0;
  4652. }
  4653. /* helper function for file defrag and space balancing. This
  4654. * forces readahead on a given range of bytes in an inode
  4655. */
  4656. unsigned long btrfs_force_ra(struct address_space *mapping,
  4657. struct file_ra_state *ra, struct file *file,
  4658. pgoff_t offset, pgoff_t last_index)
  4659. {
  4660. pgoff_t req_size = last_index - offset + 1;
  4661. page_cache_sync_readahead(mapping, ra, file, offset, req_size);
  4662. return offset + req_size;
  4663. }
  4664. struct inode *btrfs_alloc_inode(struct super_block *sb)
  4665. {
  4666. struct btrfs_inode *ei;
  4667. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  4668. if (!ei)
  4669. return NULL;
  4670. ei->last_trans = 0;
  4671. ei->last_sub_trans = 0;
  4672. ei->logged_trans = 0;
  4673. ei->outstanding_extents = 0;
  4674. ei->reserved_extents = 0;
  4675. ei->root = NULL;
  4676. spin_lock_init(&ei->accounting_lock);
  4677. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  4678. INIT_LIST_HEAD(&ei->i_orphan);
  4679. INIT_LIST_HEAD(&ei->ordered_operations);
  4680. return &ei->vfs_inode;
  4681. }
  4682. void btrfs_destroy_inode(struct inode *inode)
  4683. {
  4684. struct btrfs_ordered_extent *ordered;
  4685. struct btrfs_root *root = BTRFS_I(inode)->root;
  4686. WARN_ON(!list_empty(&inode->i_dentry));
  4687. WARN_ON(inode->i_data.nrpages);
  4688. /*
  4689. * This can happen where we create an inode, but somebody else also
  4690. * created the same inode and we need to destroy the one we already
  4691. * created.
  4692. */
  4693. if (!root)
  4694. goto free;
  4695. /*
  4696. * Make sure we're properly removed from the ordered operation
  4697. * lists.
  4698. */
  4699. smp_mb();
  4700. if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
  4701. spin_lock(&root->fs_info->ordered_extent_lock);
  4702. list_del_init(&BTRFS_I(inode)->ordered_operations);
  4703. spin_unlock(&root->fs_info->ordered_extent_lock);
  4704. }
  4705. spin_lock(&root->list_lock);
  4706. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  4707. printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
  4708. inode->i_ino);
  4709. list_del_init(&BTRFS_I(inode)->i_orphan);
  4710. }
  4711. spin_unlock(&root->list_lock);
  4712. while (1) {
  4713. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  4714. if (!ordered)
  4715. break;
  4716. else {
  4717. printk(KERN_ERR "btrfs found ordered "
  4718. "extent %llu %llu on inode cleanup\n",
  4719. (unsigned long long)ordered->file_offset,
  4720. (unsigned long long)ordered->len);
  4721. btrfs_remove_ordered_extent(inode, ordered);
  4722. btrfs_put_ordered_extent(ordered);
  4723. btrfs_put_ordered_extent(ordered);
  4724. }
  4725. }
  4726. inode_tree_del(inode);
  4727. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  4728. free:
  4729. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  4730. }
  4731. void btrfs_drop_inode(struct inode *inode)
  4732. {
  4733. struct btrfs_root *root = BTRFS_I(inode)->root;
  4734. if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
  4735. generic_delete_inode(inode);
  4736. else
  4737. generic_drop_inode(inode);
  4738. }
  4739. static void init_once(void *foo)
  4740. {
  4741. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  4742. inode_init_once(&ei->vfs_inode);
  4743. }
  4744. void btrfs_destroy_cachep(void)
  4745. {
  4746. if (btrfs_inode_cachep)
  4747. kmem_cache_destroy(btrfs_inode_cachep);
  4748. if (btrfs_trans_handle_cachep)
  4749. kmem_cache_destroy(btrfs_trans_handle_cachep);
  4750. if (btrfs_transaction_cachep)
  4751. kmem_cache_destroy(btrfs_transaction_cachep);
  4752. if (btrfs_path_cachep)
  4753. kmem_cache_destroy(btrfs_path_cachep);
  4754. }
  4755. int btrfs_init_cachep(void)
  4756. {
  4757. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  4758. sizeof(struct btrfs_inode), 0,
  4759. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
  4760. if (!btrfs_inode_cachep)
  4761. goto fail;
  4762. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  4763. sizeof(struct btrfs_trans_handle), 0,
  4764. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  4765. if (!btrfs_trans_handle_cachep)
  4766. goto fail;
  4767. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  4768. sizeof(struct btrfs_transaction), 0,
  4769. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  4770. if (!btrfs_transaction_cachep)
  4771. goto fail;
  4772. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  4773. sizeof(struct btrfs_path), 0,
  4774. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  4775. if (!btrfs_path_cachep)
  4776. goto fail;
  4777. return 0;
  4778. fail:
  4779. btrfs_destroy_cachep();
  4780. return -ENOMEM;
  4781. }
  4782. static int btrfs_getattr(struct vfsmount *mnt,
  4783. struct dentry *dentry, struct kstat *stat)
  4784. {
  4785. struct inode *inode = dentry->d_inode;
  4786. generic_fillattr(inode, stat);
  4787. stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
  4788. stat->blksize = PAGE_CACHE_SIZE;
  4789. stat->blocks = (inode_get_bytes(inode) +
  4790. BTRFS_I(inode)->delalloc_bytes) >> 9;
  4791. return 0;
  4792. }
  4793. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  4794. struct inode *new_dir, struct dentry *new_dentry)
  4795. {
  4796. struct btrfs_trans_handle *trans;
  4797. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  4798. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  4799. struct inode *new_inode = new_dentry->d_inode;
  4800. struct inode *old_inode = old_dentry->d_inode;
  4801. struct timespec ctime = CURRENT_TIME;
  4802. u64 index = 0;
  4803. u64 root_objectid;
  4804. int ret;
  4805. if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  4806. return -EPERM;
  4807. /* we only allow rename subvolume link between subvolumes */
  4808. if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  4809. return -EXDEV;
  4810. if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
  4811. (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
  4812. return -ENOTEMPTY;
  4813. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  4814. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  4815. return -ENOTEMPTY;
  4816. /*
  4817. * We want to reserve the absolute worst case amount of items. So if
  4818. * both inodes are subvols and we need to unlink them then that would
  4819. * require 4 item modifications, but if they are both normal inodes it
  4820. * would require 5 item modifications, so we'll assume their normal
  4821. * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
  4822. * should cover the worst case number of items we'll modify.
  4823. */
  4824. ret = btrfs_reserve_metadata_space(root, 11);
  4825. if (ret)
  4826. return ret;
  4827. /*
  4828. * we're using rename to replace one file with another.
  4829. * and the replacement file is large. Start IO on it now so
  4830. * we don't add too much work to the end of the transaction
  4831. */
  4832. if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
  4833. old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  4834. filemap_flush(old_inode->i_mapping);
  4835. /* close the racy window with snapshot create/destroy ioctl */
  4836. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  4837. down_read(&root->fs_info->subvol_sem);
  4838. trans = btrfs_start_transaction(root, 1);
  4839. btrfs_set_trans_block_group(trans, new_dir);
  4840. if (dest != root)
  4841. btrfs_record_root_in_trans(trans, dest);
  4842. ret = btrfs_set_inode_index(new_dir, &index);
  4843. if (ret)
  4844. goto out_fail;
  4845. if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4846. /* force full log commit if subvolume involved. */
  4847. root->fs_info->last_trans_log_full_commit = trans->transid;
  4848. } else {
  4849. ret = btrfs_insert_inode_ref(trans, dest,
  4850. new_dentry->d_name.name,
  4851. new_dentry->d_name.len,
  4852. old_inode->i_ino,
  4853. new_dir->i_ino, index);
  4854. if (ret)
  4855. goto out_fail;
  4856. /*
  4857. * this is an ugly little race, but the rename is required
  4858. * to make sure that if we crash, the inode is either at the
  4859. * old name or the new one. pinning the log transaction lets
  4860. * us make sure we don't allow a log commit to come in after
  4861. * we unlink the name but before we add the new name back in.
  4862. */
  4863. btrfs_pin_log_trans(root);
  4864. }
  4865. /*
  4866. * make sure the inode gets flushed if it is replacing
  4867. * something.
  4868. */
  4869. if (new_inode && new_inode->i_size &&
  4870. old_inode && S_ISREG(old_inode->i_mode)) {
  4871. btrfs_add_ordered_operation(trans, root, old_inode);
  4872. }
  4873. old_dir->i_ctime = old_dir->i_mtime = ctime;
  4874. new_dir->i_ctime = new_dir->i_mtime = ctime;
  4875. old_inode->i_ctime = ctime;
  4876. if (old_dentry->d_parent != new_dentry->d_parent)
  4877. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  4878. if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4879. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  4880. ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
  4881. old_dentry->d_name.name,
  4882. old_dentry->d_name.len);
  4883. } else {
  4884. btrfs_inc_nlink(old_dentry->d_inode);
  4885. ret = btrfs_unlink_inode(trans, root, old_dir,
  4886. old_dentry->d_inode,
  4887. old_dentry->d_name.name,
  4888. old_dentry->d_name.len);
  4889. }
  4890. BUG_ON(ret);
  4891. if (new_inode) {
  4892. new_inode->i_ctime = CURRENT_TIME;
  4893. if (unlikely(new_inode->i_ino ==
  4894. BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  4895. root_objectid = BTRFS_I(new_inode)->location.objectid;
  4896. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  4897. root_objectid,
  4898. new_dentry->d_name.name,
  4899. new_dentry->d_name.len);
  4900. BUG_ON(new_inode->i_nlink == 0);
  4901. } else {
  4902. ret = btrfs_unlink_inode(trans, dest, new_dir,
  4903. new_dentry->d_inode,
  4904. new_dentry->d_name.name,
  4905. new_dentry->d_name.len);
  4906. }
  4907. BUG_ON(ret);
  4908. if (new_inode->i_nlink == 0) {
  4909. ret = btrfs_orphan_add(trans, new_dentry->d_inode);
  4910. BUG_ON(ret);
  4911. }
  4912. }
  4913. ret = btrfs_add_link(trans, new_dir, old_inode,
  4914. new_dentry->d_name.name,
  4915. new_dentry->d_name.len, 0, index);
  4916. BUG_ON(ret);
  4917. if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  4918. btrfs_log_new_name(trans, old_inode, old_dir,
  4919. new_dentry->d_parent);
  4920. btrfs_end_log_trans(root);
  4921. }
  4922. out_fail:
  4923. btrfs_end_transaction_throttle(trans, root);
  4924. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  4925. up_read(&root->fs_info->subvol_sem);
  4926. btrfs_unreserve_metadata_space(root, 11);
  4927. return ret;
  4928. }
  4929. /*
  4930. * some fairly slow code that needs optimization. This walks the list
  4931. * of all the inodes with pending delalloc and forces them to disk.
  4932. */
  4933. int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
  4934. {
  4935. struct list_head *head = &root->fs_info->delalloc_inodes;
  4936. struct btrfs_inode *binode;
  4937. struct inode *inode;
  4938. if (root->fs_info->sb->s_flags & MS_RDONLY)
  4939. return -EROFS;
  4940. spin_lock(&root->fs_info->delalloc_lock);
  4941. while (!list_empty(head)) {
  4942. binode = list_entry(head->next, struct btrfs_inode,
  4943. delalloc_inodes);
  4944. inode = igrab(&binode->vfs_inode);
  4945. if (!inode)
  4946. list_del_init(&binode->delalloc_inodes);
  4947. spin_unlock(&root->fs_info->delalloc_lock);
  4948. if (inode) {
  4949. filemap_flush(inode->i_mapping);
  4950. if (delay_iput)
  4951. btrfs_add_delayed_iput(inode);
  4952. else
  4953. iput(inode);
  4954. }
  4955. cond_resched();
  4956. spin_lock(&root->fs_info->delalloc_lock);
  4957. }
  4958. spin_unlock(&root->fs_info->delalloc_lock);
  4959. /* the filemap_flush will queue IO into the worker threads, but
  4960. * we have to make sure the IO is actually started and that
  4961. * ordered extents get created before we return
  4962. */
  4963. atomic_inc(&root->fs_info->async_submit_draining);
  4964. while (atomic_read(&root->fs_info->nr_async_submits) ||
  4965. atomic_read(&root->fs_info->async_delalloc_pages)) {
  4966. wait_event(root->fs_info->async_submit_wait,
  4967. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  4968. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  4969. }
  4970. atomic_dec(&root->fs_info->async_submit_draining);
  4971. return 0;
  4972. }
  4973. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  4974. const char *symname)
  4975. {
  4976. struct btrfs_trans_handle *trans;
  4977. struct btrfs_root *root = BTRFS_I(dir)->root;
  4978. struct btrfs_path *path;
  4979. struct btrfs_key key;
  4980. struct inode *inode = NULL;
  4981. int err;
  4982. int drop_inode = 0;
  4983. u64 objectid;
  4984. u64 index = 0 ;
  4985. int name_len;
  4986. int datasize;
  4987. unsigned long ptr;
  4988. struct btrfs_file_extent_item *ei;
  4989. struct extent_buffer *leaf;
  4990. unsigned long nr = 0;
  4991. name_len = strlen(symname) + 1;
  4992. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  4993. return -ENAMETOOLONG;
  4994. /*
  4995. * 2 items for inode item and ref
  4996. * 2 items for dir items
  4997. * 1 item for xattr if selinux is on
  4998. */
  4999. err = btrfs_reserve_metadata_space(root, 5);
  5000. if (err)
  5001. return err;
  5002. trans = btrfs_start_transaction(root, 1);
  5003. if (!trans)
  5004. goto out_fail;
  5005. btrfs_set_trans_block_group(trans, dir);
  5006. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  5007. if (err) {
  5008. err = -ENOSPC;
  5009. goto out_unlock;
  5010. }
  5011. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  5012. dentry->d_name.len,
  5013. dentry->d_parent->d_inode->i_ino, objectid,
  5014. BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
  5015. &index);
  5016. err = PTR_ERR(inode);
  5017. if (IS_ERR(inode))
  5018. goto out_unlock;
  5019. err = btrfs_init_inode_security(trans, inode, dir);
  5020. if (err) {
  5021. drop_inode = 1;
  5022. goto out_unlock;
  5023. }
  5024. btrfs_set_trans_block_group(trans, inode);
  5025. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  5026. if (err)
  5027. drop_inode = 1;
  5028. else {
  5029. inode->i_mapping->a_ops = &btrfs_aops;
  5030. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  5031. inode->i_fop = &btrfs_file_operations;
  5032. inode->i_op = &btrfs_file_inode_operations;
  5033. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  5034. }
  5035. btrfs_update_inode_block_group(trans, inode);
  5036. btrfs_update_inode_block_group(trans, dir);
  5037. if (drop_inode)
  5038. goto out_unlock;
  5039. path = btrfs_alloc_path();
  5040. BUG_ON(!path);
  5041. key.objectid = inode->i_ino;
  5042. key.offset = 0;
  5043. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  5044. datasize = btrfs_file_extent_calc_inline_size(name_len);
  5045. err = btrfs_insert_empty_item(trans, root, path, &key,
  5046. datasize);
  5047. if (err) {
  5048. drop_inode = 1;
  5049. goto out_unlock;
  5050. }
  5051. leaf = path->nodes[0];
  5052. ei = btrfs_item_ptr(leaf, path->slots[0],
  5053. struct btrfs_file_extent_item);
  5054. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  5055. btrfs_set_file_extent_type(leaf, ei,
  5056. BTRFS_FILE_EXTENT_INLINE);
  5057. btrfs_set_file_extent_encryption(leaf, ei, 0);
  5058. btrfs_set_file_extent_compression(leaf, ei, 0);
  5059. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  5060. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  5061. ptr = btrfs_file_extent_inline_start(ei);
  5062. write_extent_buffer(leaf, symname, ptr, name_len);
  5063. btrfs_mark_buffer_dirty(leaf);
  5064. btrfs_free_path(path);
  5065. inode->i_op = &btrfs_symlink_inode_operations;
  5066. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  5067. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  5068. inode_set_bytes(inode, name_len);
  5069. btrfs_i_size_write(inode, name_len - 1);
  5070. err = btrfs_update_inode(trans, root, inode);
  5071. if (err)
  5072. drop_inode = 1;
  5073. out_unlock:
  5074. nr = trans->blocks_used;
  5075. btrfs_end_transaction_throttle(trans, root);
  5076. out_fail:
  5077. btrfs_unreserve_metadata_space(root, 5);
  5078. if (drop_inode) {
  5079. inode_dec_link_count(inode);
  5080. iput(inode);
  5081. }
  5082. btrfs_btree_balance_dirty(root, nr);
  5083. return err;
  5084. }
  5085. static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
  5086. u64 alloc_hint, int mode)
  5087. {
  5088. struct btrfs_trans_handle *trans;
  5089. struct btrfs_root *root = BTRFS_I(inode)->root;
  5090. struct btrfs_key ins;
  5091. u64 alloc_size;
  5092. u64 cur_offset = start;
  5093. u64 num_bytes = end - start;
  5094. int ret = 0;
  5095. while (num_bytes > 0) {
  5096. alloc_size = min(num_bytes, root->fs_info->max_extent);
  5097. ret = btrfs_reserve_extent(trans, root, alloc_size,
  5098. root->sectorsize, 0, alloc_hint,
  5099. (u64)-1, &ins, 1);
  5100. if (ret) {
  5101. WARN_ON(1);
  5102. break;
  5103. }
  5104. ret = btrfs_reserve_metadata_space(root, 3);
  5105. if (ret) {
  5106. btrfs_free_reserved_extent(root, ins.objectid,
  5107. ins.offset);
  5108. break;
  5109. }
  5110. trans = btrfs_start_transaction(root, 1);
  5111. ret = insert_reserved_file_extent(trans, inode,
  5112. cur_offset, ins.objectid,
  5113. ins.offset, ins.offset,
  5114. ins.offset, 0, 0, 0,
  5115. BTRFS_FILE_EXTENT_PREALLOC);
  5116. BUG_ON(ret);
  5117. btrfs_drop_extent_cache(inode, cur_offset,
  5118. cur_offset + ins.offset -1, 0);
  5119. num_bytes -= ins.offset;
  5120. cur_offset += ins.offset;
  5121. alloc_hint = ins.objectid + ins.offset;
  5122. inode->i_ctime = CURRENT_TIME;
  5123. BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
  5124. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  5125. cur_offset > inode->i_size) {
  5126. i_size_write(inode, cur_offset);
  5127. btrfs_ordered_update_i_size(inode, cur_offset, NULL);
  5128. }
  5129. ret = btrfs_update_inode(trans, root, inode);
  5130. BUG_ON(ret);
  5131. btrfs_end_transaction(trans, root);
  5132. btrfs_unreserve_metadata_space(root, 3);
  5133. }
  5134. return ret;
  5135. }
  5136. static long btrfs_fallocate(struct inode *inode, int mode,
  5137. loff_t offset, loff_t len)
  5138. {
  5139. u64 cur_offset;
  5140. u64 last_byte;
  5141. u64 alloc_start;
  5142. u64 alloc_end;
  5143. u64 alloc_hint = 0;
  5144. u64 locked_end;
  5145. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  5146. struct extent_map *em;
  5147. int ret;
  5148. alloc_start = offset & ~mask;
  5149. alloc_end = (offset + len + mask) & ~mask;
  5150. /*
  5151. * wait for ordered IO before we have any locks. We'll loop again
  5152. * below with the locks held.
  5153. */
  5154. btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
  5155. mutex_lock(&inode->i_mutex);
  5156. if (alloc_start > inode->i_size) {
  5157. ret = btrfs_cont_expand(inode, alloc_start);
  5158. if (ret)
  5159. goto out;
  5160. }
  5161. ret = btrfs_check_data_free_space(BTRFS_I(inode)->root, inode,
  5162. alloc_end - alloc_start);
  5163. if (ret)
  5164. goto out;
  5165. locked_end = alloc_end - 1;
  5166. while (1) {
  5167. struct btrfs_ordered_extent *ordered;
  5168. /* the extent lock is ordered inside the running
  5169. * transaction
  5170. */
  5171. lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  5172. GFP_NOFS);
  5173. ordered = btrfs_lookup_first_ordered_extent(inode,
  5174. alloc_end - 1);
  5175. if (ordered &&
  5176. ordered->file_offset + ordered->len > alloc_start &&
  5177. ordered->file_offset < alloc_end) {
  5178. btrfs_put_ordered_extent(ordered);
  5179. unlock_extent(&BTRFS_I(inode)->io_tree,
  5180. alloc_start, locked_end, GFP_NOFS);
  5181. /*
  5182. * we can't wait on the range with the transaction
  5183. * running or with the extent lock held
  5184. */
  5185. btrfs_wait_ordered_range(inode, alloc_start,
  5186. alloc_end - alloc_start);
  5187. } else {
  5188. if (ordered)
  5189. btrfs_put_ordered_extent(ordered);
  5190. break;
  5191. }
  5192. }
  5193. cur_offset = alloc_start;
  5194. while (1) {
  5195. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  5196. alloc_end - cur_offset, 0);
  5197. BUG_ON(IS_ERR(em) || !em);
  5198. last_byte = min(extent_map_end(em), alloc_end);
  5199. last_byte = (last_byte + mask) & ~mask;
  5200. if (em->block_start == EXTENT_MAP_HOLE ||
  5201. (cur_offset >= inode->i_size &&
  5202. !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  5203. ret = prealloc_file_range(inode,
  5204. cur_offset, last_byte,
  5205. alloc_hint, mode);
  5206. if (ret < 0) {
  5207. free_extent_map(em);
  5208. break;
  5209. }
  5210. }
  5211. if (em->block_start <= EXTENT_MAP_LAST_BYTE)
  5212. alloc_hint = em->block_start;
  5213. free_extent_map(em);
  5214. cur_offset = last_byte;
  5215. if (cur_offset >= alloc_end) {
  5216. ret = 0;
  5217. break;
  5218. }
  5219. }
  5220. unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  5221. GFP_NOFS);
  5222. btrfs_free_reserved_data_space(BTRFS_I(inode)->root, inode,
  5223. alloc_end - alloc_start);
  5224. out:
  5225. mutex_unlock(&inode->i_mutex);
  5226. return ret;
  5227. }
  5228. static int btrfs_set_page_dirty(struct page *page)
  5229. {
  5230. return __set_page_dirty_nobuffers(page);
  5231. }
  5232. static int btrfs_permission(struct inode *inode, int mask)
  5233. {
  5234. if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
  5235. return -EACCES;
  5236. return generic_permission(inode, mask, btrfs_check_acl);
  5237. }
  5238. static const struct inode_operations btrfs_dir_inode_operations = {
  5239. .getattr = btrfs_getattr,
  5240. .lookup = btrfs_lookup,
  5241. .create = btrfs_create,
  5242. .unlink = btrfs_unlink,
  5243. .link = btrfs_link,
  5244. .mkdir = btrfs_mkdir,
  5245. .rmdir = btrfs_rmdir,
  5246. .rename = btrfs_rename,
  5247. .symlink = btrfs_symlink,
  5248. .setattr = btrfs_setattr,
  5249. .mknod = btrfs_mknod,
  5250. .setxattr = btrfs_setxattr,
  5251. .getxattr = btrfs_getxattr,
  5252. .listxattr = btrfs_listxattr,
  5253. .removexattr = btrfs_removexattr,
  5254. .permission = btrfs_permission,
  5255. };
  5256. static const struct inode_operations btrfs_dir_ro_inode_operations = {
  5257. .lookup = btrfs_lookup,
  5258. .permission = btrfs_permission,
  5259. };
  5260. static const struct file_operations btrfs_dir_file_operations = {
  5261. .llseek = generic_file_llseek,
  5262. .read = generic_read_dir,
  5263. .readdir = btrfs_real_readdir,
  5264. .unlocked_ioctl = btrfs_ioctl,
  5265. #ifdef CONFIG_COMPAT
  5266. .compat_ioctl = btrfs_ioctl,
  5267. #endif
  5268. .release = btrfs_release_file,
  5269. .fsync = btrfs_sync_file,
  5270. };
  5271. static struct extent_io_ops btrfs_extent_io_ops = {
  5272. .fill_delalloc = run_delalloc_range,
  5273. .submit_bio_hook = btrfs_submit_bio_hook,
  5274. .merge_bio_hook = btrfs_merge_bio_hook,
  5275. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  5276. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  5277. .writepage_start_hook = btrfs_writepage_start_hook,
  5278. .readpage_io_failed_hook = btrfs_io_failed_hook,
  5279. .set_bit_hook = btrfs_set_bit_hook,
  5280. .clear_bit_hook = btrfs_clear_bit_hook,
  5281. .merge_extent_hook = btrfs_merge_extent_hook,
  5282. .split_extent_hook = btrfs_split_extent_hook,
  5283. };
  5284. /*
  5285. * btrfs doesn't support the bmap operation because swapfiles
  5286. * use bmap to make a mapping of extents in the file. They assume
  5287. * these extents won't change over the life of the file and they
  5288. * use the bmap result to do IO directly to the drive.
  5289. *
  5290. * the btrfs bmap call would return logical addresses that aren't
  5291. * suitable for IO and they also will change frequently as COW
  5292. * operations happen. So, swapfile + btrfs == corruption.
  5293. *
  5294. * For now we're avoiding this by dropping bmap.
  5295. */
  5296. static const struct address_space_operations btrfs_aops = {
  5297. .readpage = btrfs_readpage,
  5298. .writepage = btrfs_writepage,
  5299. .writepages = btrfs_writepages,
  5300. .readpages = btrfs_readpages,
  5301. .sync_page = block_sync_page,
  5302. .direct_IO = btrfs_direct_IO,
  5303. .invalidatepage = btrfs_invalidatepage,
  5304. .releasepage = btrfs_releasepage,
  5305. .set_page_dirty = btrfs_set_page_dirty,
  5306. .error_remove_page = generic_error_remove_page,
  5307. };
  5308. static const struct address_space_operations btrfs_symlink_aops = {
  5309. .readpage = btrfs_readpage,
  5310. .writepage = btrfs_writepage,
  5311. .invalidatepage = btrfs_invalidatepage,
  5312. .releasepage = btrfs_releasepage,
  5313. };
  5314. static const struct inode_operations btrfs_file_inode_operations = {
  5315. .truncate = btrfs_truncate,
  5316. .getattr = btrfs_getattr,
  5317. .setattr = btrfs_setattr,
  5318. .setxattr = btrfs_setxattr,
  5319. .getxattr = btrfs_getxattr,
  5320. .listxattr = btrfs_listxattr,
  5321. .removexattr = btrfs_removexattr,
  5322. .permission = btrfs_permission,
  5323. .fallocate = btrfs_fallocate,
  5324. .fiemap = btrfs_fiemap,
  5325. };
  5326. static const struct inode_operations btrfs_special_inode_operations = {
  5327. .getattr = btrfs_getattr,
  5328. .setattr = btrfs_setattr,
  5329. .permission = btrfs_permission,
  5330. .setxattr = btrfs_setxattr,
  5331. .getxattr = btrfs_getxattr,
  5332. .listxattr = btrfs_listxattr,
  5333. .removexattr = btrfs_removexattr,
  5334. };
  5335. static const struct inode_operations btrfs_symlink_inode_operations = {
  5336. .readlink = generic_readlink,
  5337. .follow_link = page_follow_link_light,
  5338. .put_link = page_put_link,
  5339. .permission = btrfs_permission,
  5340. .setxattr = btrfs_setxattr,
  5341. .getxattr = btrfs_getxattr,
  5342. .listxattr = btrfs_listxattr,
  5343. .removexattr = btrfs_removexattr,
  5344. };
  5345. const struct dentry_operations btrfs_dentry_operations = {
  5346. .d_delete = btrfs_dentry_delete,
  5347. };