inode.c 161 KB

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