inode.c 151 KB

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