inode.c 134 KB

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