inode.c 116 KB

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