inode.c 162 KB

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