inode.c 134 KB

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