inode.c 137 KB

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