inode.c 156 KB

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