inode.c 159 KB

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