inode.c 160 KB

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