inode.c 122 KB

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