inode.c 135 KB

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