inode.c 134 KB

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