inode.c 157 KB

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