inode.c 133 KB

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