tree-log.c 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659
  1. /*
  2. * Copyright (C) 2008 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/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/list_sort.h>
  22. #include "tree-log.h"
  23. #include "disk-io.h"
  24. #include "locking.h"
  25. #include "print-tree.h"
  26. #include "backref.h"
  27. #include "hash.h"
  28. /* magic values for the inode_only field in btrfs_log_inode:
  29. *
  30. * LOG_INODE_ALL means to log everything
  31. * LOG_INODE_EXISTS means to log just enough to recreate the inode
  32. * during log replay
  33. */
  34. #define LOG_INODE_ALL 0
  35. #define LOG_INODE_EXISTS 1
  36. /*
  37. * directory trouble cases
  38. *
  39. * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
  40. * log, we must force a full commit before doing an fsync of the directory
  41. * where the unlink was done.
  42. * ---> record transid of last unlink/rename per directory
  43. *
  44. * mkdir foo/some_dir
  45. * normal commit
  46. * rename foo/some_dir foo2/some_dir
  47. * mkdir foo/some_dir
  48. * fsync foo/some_dir/some_file
  49. *
  50. * The fsync above will unlink the original some_dir without recording
  51. * it in its new location (foo2). After a crash, some_dir will be gone
  52. * unless the fsync of some_file forces a full commit
  53. *
  54. * 2) we must log any new names for any file or dir that is in the fsync
  55. * log. ---> check inode while renaming/linking.
  56. *
  57. * 2a) we must log any new names for any file or dir during rename
  58. * when the directory they are being removed from was logged.
  59. * ---> check inode and old parent dir during rename
  60. *
  61. * 2a is actually the more important variant. With the extra logging
  62. * a crash might unlink the old name without recreating the new one
  63. *
  64. * 3) after a crash, we must go through any directories with a link count
  65. * of zero and redo the rm -rf
  66. *
  67. * mkdir f1/foo
  68. * normal commit
  69. * rm -rf f1/foo
  70. * fsync(f1)
  71. *
  72. * The directory f1 was fully removed from the FS, but fsync was never
  73. * called on f1, only its parent dir. After a crash the rm -rf must
  74. * be replayed. This must be able to recurse down the entire
  75. * directory tree. The inode link count fixup code takes care of the
  76. * ugly details.
  77. */
  78. /*
  79. * stages for the tree walking. The first
  80. * stage (0) is to only pin down the blocks we find
  81. * the second stage (1) is to make sure that all the inodes
  82. * we find in the log are created in the subvolume.
  83. *
  84. * The last stage is to deal with directories and links and extents
  85. * and all the other fun semantics
  86. */
  87. #define LOG_WALK_PIN_ONLY 0
  88. #define LOG_WALK_REPLAY_INODES 1
  89. #define LOG_WALK_REPLAY_DIR_INDEX 2
  90. #define LOG_WALK_REPLAY_ALL 3
  91. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  92. struct btrfs_root *root, struct inode *inode,
  93. int inode_only,
  94. const loff_t start,
  95. const loff_t end,
  96. struct btrfs_log_ctx *ctx);
  97. static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  98. struct btrfs_root *root,
  99. struct btrfs_path *path, u64 objectid);
  100. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  101. struct btrfs_root *root,
  102. struct btrfs_root *log,
  103. struct btrfs_path *path,
  104. u64 dirid, int del_all);
  105. /*
  106. * tree logging is a special write ahead log used to make sure that
  107. * fsyncs and O_SYNCs can happen without doing full tree commits.
  108. *
  109. * Full tree commits are expensive because they require commonly
  110. * modified blocks to be recowed, creating many dirty pages in the
  111. * extent tree an 4x-6x higher write load than ext3.
  112. *
  113. * Instead of doing a tree commit on every fsync, we use the
  114. * key ranges and transaction ids to find items for a given file or directory
  115. * that have changed in this transaction. Those items are copied into
  116. * a special tree (one per subvolume root), that tree is written to disk
  117. * and then the fsync is considered complete.
  118. *
  119. * After a crash, items are copied out of the log-tree back into the
  120. * subvolume tree. Any file data extents found are recorded in the extent
  121. * allocation tree, and the log-tree freed.
  122. *
  123. * The log tree is read three times, once to pin down all the extents it is
  124. * using in ram and once, once to create all the inodes logged in the tree
  125. * and once to do all the other items.
  126. */
  127. /*
  128. * start a sub transaction and setup the log tree
  129. * this increments the log tree writer count to make the people
  130. * syncing the tree wait for us to finish
  131. */
  132. static int start_log_trans(struct btrfs_trans_handle *trans,
  133. struct btrfs_root *root,
  134. struct btrfs_log_ctx *ctx)
  135. {
  136. int index;
  137. int ret;
  138. mutex_lock(&root->log_mutex);
  139. if (root->log_root) {
  140. if (btrfs_need_log_full_commit(root->fs_info, trans)) {
  141. ret = -EAGAIN;
  142. goto out;
  143. }
  144. if (!root->log_start_pid) {
  145. root->log_start_pid = current->pid;
  146. clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  147. } else if (root->log_start_pid != current->pid) {
  148. set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  149. }
  150. atomic_inc(&root->log_batch);
  151. atomic_inc(&root->log_writers);
  152. if (ctx) {
  153. index = root->log_transid % 2;
  154. list_add_tail(&ctx->list, &root->log_ctxs[index]);
  155. ctx->log_transid = root->log_transid;
  156. }
  157. mutex_unlock(&root->log_mutex);
  158. return 0;
  159. }
  160. ret = 0;
  161. mutex_lock(&root->fs_info->tree_log_mutex);
  162. if (!root->fs_info->log_root_tree)
  163. ret = btrfs_init_log_root_tree(trans, root->fs_info);
  164. mutex_unlock(&root->fs_info->tree_log_mutex);
  165. if (ret)
  166. goto out;
  167. if (!root->log_root) {
  168. ret = btrfs_add_log_tree(trans, root);
  169. if (ret)
  170. goto out;
  171. }
  172. clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  173. root->log_start_pid = current->pid;
  174. atomic_inc(&root->log_batch);
  175. atomic_inc(&root->log_writers);
  176. if (ctx) {
  177. index = root->log_transid % 2;
  178. list_add_tail(&ctx->list, &root->log_ctxs[index]);
  179. ctx->log_transid = root->log_transid;
  180. }
  181. out:
  182. mutex_unlock(&root->log_mutex);
  183. return ret;
  184. }
  185. /*
  186. * returns 0 if there was a log transaction running and we were able
  187. * to join, or returns -ENOENT if there were not transactions
  188. * in progress
  189. */
  190. static int join_running_log_trans(struct btrfs_root *root)
  191. {
  192. int ret = -ENOENT;
  193. smp_mb();
  194. if (!root->log_root)
  195. return -ENOENT;
  196. mutex_lock(&root->log_mutex);
  197. if (root->log_root) {
  198. ret = 0;
  199. atomic_inc(&root->log_writers);
  200. }
  201. mutex_unlock(&root->log_mutex);
  202. return ret;
  203. }
  204. /*
  205. * This either makes the current running log transaction wait
  206. * until you call btrfs_end_log_trans() or it makes any future
  207. * log transactions wait until you call btrfs_end_log_trans()
  208. */
  209. int btrfs_pin_log_trans(struct btrfs_root *root)
  210. {
  211. int ret = -ENOENT;
  212. mutex_lock(&root->log_mutex);
  213. atomic_inc(&root->log_writers);
  214. mutex_unlock(&root->log_mutex);
  215. return ret;
  216. }
  217. /*
  218. * indicate we're done making changes to the log tree
  219. * and wake up anyone waiting to do a sync
  220. */
  221. void btrfs_end_log_trans(struct btrfs_root *root)
  222. {
  223. if (atomic_dec_and_test(&root->log_writers)) {
  224. smp_mb();
  225. if (waitqueue_active(&root->log_writer_wait))
  226. wake_up(&root->log_writer_wait);
  227. }
  228. }
  229. /*
  230. * the walk control struct is used to pass state down the chain when
  231. * processing the log tree. The stage field tells us which part
  232. * of the log tree processing we are currently doing. The others
  233. * are state fields used for that specific part
  234. */
  235. struct walk_control {
  236. /* should we free the extent on disk when done? This is used
  237. * at transaction commit time while freeing a log tree
  238. */
  239. int free;
  240. /* should we write out the extent buffer? This is used
  241. * while flushing the log tree to disk during a sync
  242. */
  243. int write;
  244. /* should we wait for the extent buffer io to finish? Also used
  245. * while flushing the log tree to disk for a sync
  246. */
  247. int wait;
  248. /* pin only walk, we record which extents on disk belong to the
  249. * log trees
  250. */
  251. int pin;
  252. /* what stage of the replay code we're currently in */
  253. int stage;
  254. /* the root we are currently replaying */
  255. struct btrfs_root *replay_dest;
  256. /* the trans handle for the current replay */
  257. struct btrfs_trans_handle *trans;
  258. /* the function that gets used to process blocks we find in the
  259. * tree. Note the extent_buffer might not be up to date when it is
  260. * passed in, and it must be checked or read if you need the data
  261. * inside it
  262. */
  263. int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
  264. struct walk_control *wc, u64 gen);
  265. };
  266. /*
  267. * process_func used to pin down extents, write them or wait on them
  268. */
  269. static int process_one_buffer(struct btrfs_root *log,
  270. struct extent_buffer *eb,
  271. struct walk_control *wc, u64 gen)
  272. {
  273. int ret = 0;
  274. /*
  275. * If this fs is mixed then we need to be able to process the leaves to
  276. * pin down any logged extents, so we have to read the block.
  277. */
  278. if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
  279. ret = btrfs_read_buffer(eb, gen);
  280. if (ret)
  281. return ret;
  282. }
  283. if (wc->pin)
  284. ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
  285. eb->start, eb->len);
  286. if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
  287. if (wc->pin && btrfs_header_level(eb) == 0)
  288. ret = btrfs_exclude_logged_extents(log, eb);
  289. if (wc->write)
  290. btrfs_write_tree_block(eb);
  291. if (wc->wait)
  292. btrfs_wait_tree_block_writeback(eb);
  293. }
  294. return ret;
  295. }
  296. /*
  297. * Item overwrite used by replay and tree logging. eb, slot and key all refer
  298. * to the src data we are copying out.
  299. *
  300. * root is the tree we are copying into, and path is a scratch
  301. * path for use in this function (it should be released on entry and
  302. * will be released on exit).
  303. *
  304. * If the key is already in the destination tree the existing item is
  305. * overwritten. If the existing item isn't big enough, it is extended.
  306. * If it is too large, it is truncated.
  307. *
  308. * If the key isn't in the destination yet, a new item is inserted.
  309. */
  310. static noinline int overwrite_item(struct btrfs_trans_handle *trans,
  311. struct btrfs_root *root,
  312. struct btrfs_path *path,
  313. struct extent_buffer *eb, int slot,
  314. struct btrfs_key *key)
  315. {
  316. int ret;
  317. u32 item_size;
  318. u64 saved_i_size = 0;
  319. int save_old_i_size = 0;
  320. unsigned long src_ptr;
  321. unsigned long dst_ptr;
  322. int overwrite_root = 0;
  323. bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
  324. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  325. overwrite_root = 1;
  326. item_size = btrfs_item_size_nr(eb, slot);
  327. src_ptr = btrfs_item_ptr_offset(eb, slot);
  328. /* look for the key in the destination tree */
  329. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  330. if (ret < 0)
  331. return ret;
  332. if (ret == 0) {
  333. char *src_copy;
  334. char *dst_copy;
  335. u32 dst_size = btrfs_item_size_nr(path->nodes[0],
  336. path->slots[0]);
  337. if (dst_size != item_size)
  338. goto insert;
  339. if (item_size == 0) {
  340. btrfs_release_path(path);
  341. return 0;
  342. }
  343. dst_copy = kmalloc(item_size, GFP_NOFS);
  344. src_copy = kmalloc(item_size, GFP_NOFS);
  345. if (!dst_copy || !src_copy) {
  346. btrfs_release_path(path);
  347. kfree(dst_copy);
  348. kfree(src_copy);
  349. return -ENOMEM;
  350. }
  351. read_extent_buffer(eb, src_copy, src_ptr, item_size);
  352. dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  353. read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
  354. item_size);
  355. ret = memcmp(dst_copy, src_copy, item_size);
  356. kfree(dst_copy);
  357. kfree(src_copy);
  358. /*
  359. * they have the same contents, just return, this saves
  360. * us from cowing blocks in the destination tree and doing
  361. * extra writes that may not have been done by a previous
  362. * sync
  363. */
  364. if (ret == 0) {
  365. btrfs_release_path(path);
  366. return 0;
  367. }
  368. /*
  369. * We need to load the old nbytes into the inode so when we
  370. * replay the extents we've logged we get the right nbytes.
  371. */
  372. if (inode_item) {
  373. struct btrfs_inode_item *item;
  374. u64 nbytes;
  375. u32 mode;
  376. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  377. struct btrfs_inode_item);
  378. nbytes = btrfs_inode_nbytes(path->nodes[0], item);
  379. item = btrfs_item_ptr(eb, slot,
  380. struct btrfs_inode_item);
  381. btrfs_set_inode_nbytes(eb, item, nbytes);
  382. /*
  383. * If this is a directory we need to reset the i_size to
  384. * 0 so that we can set it up properly when replaying
  385. * the rest of the items in this log.
  386. */
  387. mode = btrfs_inode_mode(eb, item);
  388. if (S_ISDIR(mode))
  389. btrfs_set_inode_size(eb, item, 0);
  390. }
  391. } else if (inode_item) {
  392. struct btrfs_inode_item *item;
  393. u32 mode;
  394. /*
  395. * New inode, set nbytes to 0 so that the nbytes comes out
  396. * properly when we replay the extents.
  397. */
  398. item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
  399. btrfs_set_inode_nbytes(eb, item, 0);
  400. /*
  401. * If this is a directory we need to reset the i_size to 0 so
  402. * that we can set it up properly when replaying the rest of
  403. * the items in this log.
  404. */
  405. mode = btrfs_inode_mode(eb, item);
  406. if (S_ISDIR(mode))
  407. btrfs_set_inode_size(eb, item, 0);
  408. }
  409. insert:
  410. btrfs_release_path(path);
  411. /* try to insert the key into the destination tree */
  412. ret = btrfs_insert_empty_item(trans, root, path,
  413. key, item_size);
  414. /* make sure any existing item is the correct size */
  415. if (ret == -EEXIST) {
  416. u32 found_size;
  417. found_size = btrfs_item_size_nr(path->nodes[0],
  418. path->slots[0]);
  419. if (found_size > item_size)
  420. btrfs_truncate_item(root, path, item_size, 1);
  421. else if (found_size < item_size)
  422. btrfs_extend_item(root, path,
  423. item_size - found_size);
  424. } else if (ret) {
  425. return ret;
  426. }
  427. dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
  428. path->slots[0]);
  429. /* don't overwrite an existing inode if the generation number
  430. * was logged as zero. This is done when the tree logging code
  431. * is just logging an inode to make sure it exists after recovery.
  432. *
  433. * Also, don't overwrite i_size on directories during replay.
  434. * log replay inserts and removes directory items based on the
  435. * state of the tree found in the subvolume, and i_size is modified
  436. * as it goes
  437. */
  438. if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
  439. struct btrfs_inode_item *src_item;
  440. struct btrfs_inode_item *dst_item;
  441. src_item = (struct btrfs_inode_item *)src_ptr;
  442. dst_item = (struct btrfs_inode_item *)dst_ptr;
  443. if (btrfs_inode_generation(eb, src_item) == 0) {
  444. struct extent_buffer *dst_eb = path->nodes[0];
  445. if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
  446. S_ISREG(btrfs_inode_mode(dst_eb, dst_item))) {
  447. struct btrfs_map_token token;
  448. u64 ino_size = btrfs_inode_size(eb, src_item);
  449. btrfs_init_map_token(&token);
  450. btrfs_set_token_inode_size(dst_eb, dst_item,
  451. ino_size, &token);
  452. }
  453. goto no_copy;
  454. }
  455. if (overwrite_root &&
  456. S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
  457. S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
  458. save_old_i_size = 1;
  459. saved_i_size = btrfs_inode_size(path->nodes[0],
  460. dst_item);
  461. }
  462. }
  463. copy_extent_buffer(path->nodes[0], eb, dst_ptr,
  464. src_ptr, item_size);
  465. if (save_old_i_size) {
  466. struct btrfs_inode_item *dst_item;
  467. dst_item = (struct btrfs_inode_item *)dst_ptr;
  468. btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
  469. }
  470. /* make sure the generation is filled in */
  471. if (key->type == BTRFS_INODE_ITEM_KEY) {
  472. struct btrfs_inode_item *dst_item;
  473. dst_item = (struct btrfs_inode_item *)dst_ptr;
  474. if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
  475. btrfs_set_inode_generation(path->nodes[0], dst_item,
  476. trans->transid);
  477. }
  478. }
  479. no_copy:
  480. btrfs_mark_buffer_dirty(path->nodes[0]);
  481. btrfs_release_path(path);
  482. return 0;
  483. }
  484. /*
  485. * simple helper to read an inode off the disk from a given root
  486. * This can only be called for subvolume roots and not for the log
  487. */
  488. static noinline struct inode *read_one_inode(struct btrfs_root *root,
  489. u64 objectid)
  490. {
  491. struct btrfs_key key;
  492. struct inode *inode;
  493. key.objectid = objectid;
  494. key.type = BTRFS_INODE_ITEM_KEY;
  495. key.offset = 0;
  496. inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
  497. if (IS_ERR(inode)) {
  498. inode = NULL;
  499. } else if (is_bad_inode(inode)) {
  500. iput(inode);
  501. inode = NULL;
  502. }
  503. return inode;
  504. }
  505. /* replays a single extent in 'eb' at 'slot' with 'key' into the
  506. * subvolume 'root'. path is released on entry and should be released
  507. * on exit.
  508. *
  509. * extents in the log tree have not been allocated out of the extent
  510. * tree yet. So, this completes the allocation, taking a reference
  511. * as required if the extent already exists or creating a new extent
  512. * if it isn't in the extent allocation tree yet.
  513. *
  514. * The extent is inserted into the file, dropping any existing extents
  515. * from the file that overlap the new one.
  516. */
  517. static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
  518. struct btrfs_root *root,
  519. struct btrfs_path *path,
  520. struct extent_buffer *eb, int slot,
  521. struct btrfs_key *key)
  522. {
  523. int found_type;
  524. u64 extent_end;
  525. u64 start = key->offset;
  526. u64 nbytes = 0;
  527. struct btrfs_file_extent_item *item;
  528. struct inode *inode = NULL;
  529. unsigned long size;
  530. int ret = 0;
  531. item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  532. found_type = btrfs_file_extent_type(eb, item);
  533. if (found_type == BTRFS_FILE_EXTENT_REG ||
  534. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  535. nbytes = btrfs_file_extent_num_bytes(eb, item);
  536. extent_end = start + nbytes;
  537. /*
  538. * We don't add to the inodes nbytes if we are prealloc or a
  539. * hole.
  540. */
  541. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  542. nbytes = 0;
  543. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  544. size = btrfs_file_extent_inline_len(eb, slot, item);
  545. nbytes = btrfs_file_extent_ram_bytes(eb, item);
  546. extent_end = ALIGN(start + size, root->sectorsize);
  547. } else {
  548. ret = 0;
  549. goto out;
  550. }
  551. inode = read_one_inode(root, key->objectid);
  552. if (!inode) {
  553. ret = -EIO;
  554. goto out;
  555. }
  556. /*
  557. * first check to see if we already have this extent in the
  558. * file. This must be done before the btrfs_drop_extents run
  559. * so we don't try to drop this extent.
  560. */
  561. ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
  562. start, 0);
  563. if (ret == 0 &&
  564. (found_type == BTRFS_FILE_EXTENT_REG ||
  565. found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
  566. struct btrfs_file_extent_item cmp1;
  567. struct btrfs_file_extent_item cmp2;
  568. struct btrfs_file_extent_item *existing;
  569. struct extent_buffer *leaf;
  570. leaf = path->nodes[0];
  571. existing = btrfs_item_ptr(leaf, path->slots[0],
  572. struct btrfs_file_extent_item);
  573. read_extent_buffer(eb, &cmp1, (unsigned long)item,
  574. sizeof(cmp1));
  575. read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
  576. sizeof(cmp2));
  577. /*
  578. * we already have a pointer to this exact extent,
  579. * we don't have to do anything
  580. */
  581. if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
  582. btrfs_release_path(path);
  583. goto out;
  584. }
  585. }
  586. btrfs_release_path(path);
  587. /* drop any overlapping extents */
  588. ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
  589. if (ret)
  590. goto out;
  591. if (found_type == BTRFS_FILE_EXTENT_REG ||
  592. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  593. u64 offset;
  594. unsigned long dest_offset;
  595. struct btrfs_key ins;
  596. ret = btrfs_insert_empty_item(trans, root, path, key,
  597. sizeof(*item));
  598. if (ret)
  599. goto out;
  600. dest_offset = btrfs_item_ptr_offset(path->nodes[0],
  601. path->slots[0]);
  602. copy_extent_buffer(path->nodes[0], eb, dest_offset,
  603. (unsigned long)item, sizeof(*item));
  604. ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  605. ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  606. ins.type = BTRFS_EXTENT_ITEM_KEY;
  607. offset = key->offset - btrfs_file_extent_offset(eb, item);
  608. if (ins.objectid > 0) {
  609. u64 csum_start;
  610. u64 csum_end;
  611. LIST_HEAD(ordered_sums);
  612. /*
  613. * is this extent already allocated in the extent
  614. * allocation tree? If so, just add a reference
  615. */
  616. ret = btrfs_lookup_data_extent(root, ins.objectid,
  617. ins.offset);
  618. if (ret == 0) {
  619. ret = btrfs_inc_extent_ref(trans, root,
  620. ins.objectid, ins.offset,
  621. 0, root->root_key.objectid,
  622. key->objectid, offset, 0);
  623. if (ret)
  624. goto out;
  625. } else {
  626. /*
  627. * insert the extent pointer in the extent
  628. * allocation tree
  629. */
  630. ret = btrfs_alloc_logged_file_extent(trans,
  631. root, root->root_key.objectid,
  632. key->objectid, offset, &ins);
  633. if (ret)
  634. goto out;
  635. }
  636. btrfs_release_path(path);
  637. if (btrfs_file_extent_compression(eb, item)) {
  638. csum_start = ins.objectid;
  639. csum_end = csum_start + ins.offset;
  640. } else {
  641. csum_start = ins.objectid +
  642. btrfs_file_extent_offset(eb, item);
  643. csum_end = csum_start +
  644. btrfs_file_extent_num_bytes(eb, item);
  645. }
  646. ret = btrfs_lookup_csums_range(root->log_root,
  647. csum_start, csum_end - 1,
  648. &ordered_sums, 0);
  649. if (ret)
  650. goto out;
  651. while (!list_empty(&ordered_sums)) {
  652. struct btrfs_ordered_sum *sums;
  653. sums = list_entry(ordered_sums.next,
  654. struct btrfs_ordered_sum,
  655. list);
  656. if (!ret)
  657. ret = btrfs_csum_file_blocks(trans,
  658. root->fs_info->csum_root,
  659. sums);
  660. list_del(&sums->list);
  661. kfree(sums);
  662. }
  663. if (ret)
  664. goto out;
  665. } else {
  666. btrfs_release_path(path);
  667. }
  668. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  669. /* inline extents are easy, we just overwrite them */
  670. ret = overwrite_item(trans, root, path, eb, slot, key);
  671. if (ret)
  672. goto out;
  673. }
  674. inode_add_bytes(inode, nbytes);
  675. ret = btrfs_update_inode(trans, root, inode);
  676. out:
  677. if (inode)
  678. iput(inode);
  679. return ret;
  680. }
  681. /*
  682. * when cleaning up conflicts between the directory names in the
  683. * subvolume, directory names in the log and directory names in the
  684. * inode back references, we may have to unlink inodes from directories.
  685. *
  686. * This is a helper function to do the unlink of a specific directory
  687. * item
  688. */
  689. static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
  690. struct btrfs_root *root,
  691. struct btrfs_path *path,
  692. struct inode *dir,
  693. struct btrfs_dir_item *di)
  694. {
  695. struct inode *inode;
  696. char *name;
  697. int name_len;
  698. struct extent_buffer *leaf;
  699. struct btrfs_key location;
  700. int ret;
  701. leaf = path->nodes[0];
  702. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  703. name_len = btrfs_dir_name_len(leaf, di);
  704. name = kmalloc(name_len, GFP_NOFS);
  705. if (!name)
  706. return -ENOMEM;
  707. read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
  708. btrfs_release_path(path);
  709. inode = read_one_inode(root, location.objectid);
  710. if (!inode) {
  711. ret = -EIO;
  712. goto out;
  713. }
  714. ret = link_to_fixup_dir(trans, root, path, location.objectid);
  715. if (ret)
  716. goto out;
  717. ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
  718. if (ret)
  719. goto out;
  720. else
  721. ret = btrfs_run_delayed_items(trans, root);
  722. out:
  723. kfree(name);
  724. iput(inode);
  725. return ret;
  726. }
  727. /*
  728. * helper function to see if a given name and sequence number found
  729. * in an inode back reference are already in a directory and correctly
  730. * point to this inode
  731. */
  732. static noinline int inode_in_dir(struct btrfs_root *root,
  733. struct btrfs_path *path,
  734. u64 dirid, u64 objectid, u64 index,
  735. const char *name, int name_len)
  736. {
  737. struct btrfs_dir_item *di;
  738. struct btrfs_key location;
  739. int match = 0;
  740. di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
  741. index, name, name_len, 0);
  742. if (di && !IS_ERR(di)) {
  743. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  744. if (location.objectid != objectid)
  745. goto out;
  746. } else
  747. goto out;
  748. btrfs_release_path(path);
  749. di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
  750. if (di && !IS_ERR(di)) {
  751. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  752. if (location.objectid != objectid)
  753. goto out;
  754. } else
  755. goto out;
  756. match = 1;
  757. out:
  758. btrfs_release_path(path);
  759. return match;
  760. }
  761. /*
  762. * helper function to check a log tree for a named back reference in
  763. * an inode. This is used to decide if a back reference that is
  764. * found in the subvolume conflicts with what we find in the log.
  765. *
  766. * inode backreferences may have multiple refs in a single item,
  767. * during replay we process one reference at a time, and we don't
  768. * want to delete valid links to a file from the subvolume if that
  769. * link is also in the log.
  770. */
  771. static noinline int backref_in_log(struct btrfs_root *log,
  772. struct btrfs_key *key,
  773. u64 ref_objectid,
  774. char *name, int namelen)
  775. {
  776. struct btrfs_path *path;
  777. struct btrfs_inode_ref *ref;
  778. unsigned long ptr;
  779. unsigned long ptr_end;
  780. unsigned long name_ptr;
  781. int found_name_len;
  782. int item_size;
  783. int ret;
  784. int match = 0;
  785. path = btrfs_alloc_path();
  786. if (!path)
  787. return -ENOMEM;
  788. ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
  789. if (ret != 0)
  790. goto out;
  791. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  792. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  793. if (btrfs_find_name_in_ext_backref(path, ref_objectid,
  794. name, namelen, NULL))
  795. match = 1;
  796. goto out;
  797. }
  798. item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
  799. ptr_end = ptr + item_size;
  800. while (ptr < ptr_end) {
  801. ref = (struct btrfs_inode_ref *)ptr;
  802. found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
  803. if (found_name_len == namelen) {
  804. name_ptr = (unsigned long)(ref + 1);
  805. ret = memcmp_extent_buffer(path->nodes[0], name,
  806. name_ptr, namelen);
  807. if (ret == 0) {
  808. match = 1;
  809. goto out;
  810. }
  811. }
  812. ptr = (unsigned long)(ref + 1) + found_name_len;
  813. }
  814. out:
  815. btrfs_free_path(path);
  816. return match;
  817. }
  818. static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
  819. struct btrfs_root *root,
  820. struct btrfs_path *path,
  821. struct btrfs_root *log_root,
  822. struct inode *dir, struct inode *inode,
  823. struct extent_buffer *eb,
  824. u64 inode_objectid, u64 parent_objectid,
  825. u64 ref_index, char *name, int namelen,
  826. int *search_done)
  827. {
  828. int ret;
  829. char *victim_name;
  830. int victim_name_len;
  831. struct extent_buffer *leaf;
  832. struct btrfs_dir_item *di;
  833. struct btrfs_key search_key;
  834. struct btrfs_inode_extref *extref;
  835. again:
  836. /* Search old style refs */
  837. search_key.objectid = inode_objectid;
  838. search_key.type = BTRFS_INODE_REF_KEY;
  839. search_key.offset = parent_objectid;
  840. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  841. if (ret == 0) {
  842. struct btrfs_inode_ref *victim_ref;
  843. unsigned long ptr;
  844. unsigned long ptr_end;
  845. leaf = path->nodes[0];
  846. /* are we trying to overwrite a back ref for the root directory
  847. * if so, just jump out, we're done
  848. */
  849. if (search_key.objectid == search_key.offset)
  850. return 1;
  851. /* check all the names in this back reference to see
  852. * if they are in the log. if so, we allow them to stay
  853. * otherwise they must be unlinked as a conflict
  854. */
  855. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  856. ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
  857. while (ptr < ptr_end) {
  858. victim_ref = (struct btrfs_inode_ref *)ptr;
  859. victim_name_len = btrfs_inode_ref_name_len(leaf,
  860. victim_ref);
  861. victim_name = kmalloc(victim_name_len, GFP_NOFS);
  862. if (!victim_name)
  863. return -ENOMEM;
  864. read_extent_buffer(leaf, victim_name,
  865. (unsigned long)(victim_ref + 1),
  866. victim_name_len);
  867. if (!backref_in_log(log_root, &search_key,
  868. parent_objectid,
  869. victim_name,
  870. victim_name_len)) {
  871. inc_nlink(inode);
  872. btrfs_release_path(path);
  873. ret = btrfs_unlink_inode(trans, root, dir,
  874. inode, victim_name,
  875. victim_name_len);
  876. kfree(victim_name);
  877. if (ret)
  878. return ret;
  879. ret = btrfs_run_delayed_items(trans, root);
  880. if (ret)
  881. return ret;
  882. *search_done = 1;
  883. goto again;
  884. }
  885. kfree(victim_name);
  886. ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
  887. }
  888. /*
  889. * NOTE: we have searched root tree and checked the
  890. * coresponding ref, it does not need to check again.
  891. */
  892. *search_done = 1;
  893. }
  894. btrfs_release_path(path);
  895. /* Same search but for extended refs */
  896. extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
  897. inode_objectid, parent_objectid, 0,
  898. 0);
  899. if (!IS_ERR_OR_NULL(extref)) {
  900. u32 item_size;
  901. u32 cur_offset = 0;
  902. unsigned long base;
  903. struct inode *victim_parent;
  904. leaf = path->nodes[0];
  905. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  906. base = btrfs_item_ptr_offset(leaf, path->slots[0]);
  907. while (cur_offset < item_size) {
  908. extref = (struct btrfs_inode_extref *)(base + cur_offset);
  909. victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
  910. if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
  911. goto next;
  912. victim_name = kmalloc(victim_name_len, GFP_NOFS);
  913. if (!victim_name)
  914. return -ENOMEM;
  915. read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
  916. victim_name_len);
  917. search_key.objectid = inode_objectid;
  918. search_key.type = BTRFS_INODE_EXTREF_KEY;
  919. search_key.offset = btrfs_extref_hash(parent_objectid,
  920. victim_name,
  921. victim_name_len);
  922. ret = 0;
  923. if (!backref_in_log(log_root, &search_key,
  924. parent_objectid, victim_name,
  925. victim_name_len)) {
  926. ret = -ENOENT;
  927. victim_parent = read_one_inode(root,
  928. parent_objectid);
  929. if (victim_parent) {
  930. inc_nlink(inode);
  931. btrfs_release_path(path);
  932. ret = btrfs_unlink_inode(trans, root,
  933. victim_parent,
  934. inode,
  935. victim_name,
  936. victim_name_len);
  937. if (!ret)
  938. ret = btrfs_run_delayed_items(
  939. trans, root);
  940. }
  941. iput(victim_parent);
  942. kfree(victim_name);
  943. if (ret)
  944. return ret;
  945. *search_done = 1;
  946. goto again;
  947. }
  948. kfree(victim_name);
  949. if (ret)
  950. return ret;
  951. next:
  952. cur_offset += victim_name_len + sizeof(*extref);
  953. }
  954. *search_done = 1;
  955. }
  956. btrfs_release_path(path);
  957. /* look for a conflicting sequence number */
  958. di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
  959. ref_index, name, namelen, 0);
  960. if (di && !IS_ERR(di)) {
  961. ret = drop_one_dir_item(trans, root, path, dir, di);
  962. if (ret)
  963. return ret;
  964. }
  965. btrfs_release_path(path);
  966. /* look for a conflicing name */
  967. di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
  968. name, namelen, 0);
  969. if (di && !IS_ERR(di)) {
  970. ret = drop_one_dir_item(trans, root, path, dir, di);
  971. if (ret)
  972. return ret;
  973. }
  974. btrfs_release_path(path);
  975. return 0;
  976. }
  977. static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  978. u32 *namelen, char **name, u64 *index,
  979. u64 *parent_objectid)
  980. {
  981. struct btrfs_inode_extref *extref;
  982. extref = (struct btrfs_inode_extref *)ref_ptr;
  983. *namelen = btrfs_inode_extref_name_len(eb, extref);
  984. *name = kmalloc(*namelen, GFP_NOFS);
  985. if (*name == NULL)
  986. return -ENOMEM;
  987. read_extent_buffer(eb, *name, (unsigned long)&extref->name,
  988. *namelen);
  989. *index = btrfs_inode_extref_index(eb, extref);
  990. if (parent_objectid)
  991. *parent_objectid = btrfs_inode_extref_parent(eb, extref);
  992. return 0;
  993. }
  994. static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  995. u32 *namelen, char **name, u64 *index)
  996. {
  997. struct btrfs_inode_ref *ref;
  998. ref = (struct btrfs_inode_ref *)ref_ptr;
  999. *namelen = btrfs_inode_ref_name_len(eb, ref);
  1000. *name = kmalloc(*namelen, GFP_NOFS);
  1001. if (*name == NULL)
  1002. return -ENOMEM;
  1003. read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
  1004. *index = btrfs_inode_ref_index(eb, ref);
  1005. return 0;
  1006. }
  1007. /*
  1008. * replay one inode back reference item found in the log tree.
  1009. * eb, slot and key refer to the buffer and key found in the log tree.
  1010. * root is the destination we are replaying into, and path is for temp
  1011. * use by this function. (it should be released on return).
  1012. */
  1013. static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
  1014. struct btrfs_root *root,
  1015. struct btrfs_root *log,
  1016. struct btrfs_path *path,
  1017. struct extent_buffer *eb, int slot,
  1018. struct btrfs_key *key)
  1019. {
  1020. struct inode *dir = NULL;
  1021. struct inode *inode = NULL;
  1022. unsigned long ref_ptr;
  1023. unsigned long ref_end;
  1024. char *name = NULL;
  1025. int namelen;
  1026. int ret;
  1027. int search_done = 0;
  1028. int log_ref_ver = 0;
  1029. u64 parent_objectid;
  1030. u64 inode_objectid;
  1031. u64 ref_index = 0;
  1032. int ref_struct_size;
  1033. ref_ptr = btrfs_item_ptr_offset(eb, slot);
  1034. ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
  1035. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  1036. struct btrfs_inode_extref *r;
  1037. ref_struct_size = sizeof(struct btrfs_inode_extref);
  1038. log_ref_ver = 1;
  1039. r = (struct btrfs_inode_extref *)ref_ptr;
  1040. parent_objectid = btrfs_inode_extref_parent(eb, r);
  1041. } else {
  1042. ref_struct_size = sizeof(struct btrfs_inode_ref);
  1043. parent_objectid = key->offset;
  1044. }
  1045. inode_objectid = key->objectid;
  1046. /*
  1047. * it is possible that we didn't log all the parent directories
  1048. * for a given inode. If we don't find the dir, just don't
  1049. * copy the back ref in. The link count fixup code will take
  1050. * care of the rest
  1051. */
  1052. dir = read_one_inode(root, parent_objectid);
  1053. if (!dir) {
  1054. ret = -ENOENT;
  1055. goto out;
  1056. }
  1057. inode = read_one_inode(root, inode_objectid);
  1058. if (!inode) {
  1059. ret = -EIO;
  1060. goto out;
  1061. }
  1062. while (ref_ptr < ref_end) {
  1063. if (log_ref_ver) {
  1064. ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
  1065. &ref_index, &parent_objectid);
  1066. /*
  1067. * parent object can change from one array
  1068. * item to another.
  1069. */
  1070. if (!dir)
  1071. dir = read_one_inode(root, parent_objectid);
  1072. if (!dir) {
  1073. ret = -ENOENT;
  1074. goto out;
  1075. }
  1076. } else {
  1077. ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
  1078. &ref_index);
  1079. }
  1080. if (ret)
  1081. goto out;
  1082. /* if we already have a perfect match, we're done */
  1083. if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
  1084. ref_index, name, namelen)) {
  1085. /*
  1086. * look for a conflicting back reference in the
  1087. * metadata. if we find one we have to unlink that name
  1088. * of the file before we add our new link. Later on, we
  1089. * overwrite any existing back reference, and we don't
  1090. * want to create dangling pointers in the directory.
  1091. */
  1092. if (!search_done) {
  1093. ret = __add_inode_ref(trans, root, path, log,
  1094. dir, inode, eb,
  1095. inode_objectid,
  1096. parent_objectid,
  1097. ref_index, name, namelen,
  1098. &search_done);
  1099. if (ret) {
  1100. if (ret == 1)
  1101. ret = 0;
  1102. goto out;
  1103. }
  1104. }
  1105. /* insert our name */
  1106. ret = btrfs_add_link(trans, dir, inode, name, namelen,
  1107. 0, ref_index);
  1108. if (ret)
  1109. goto out;
  1110. btrfs_update_inode(trans, root, inode);
  1111. }
  1112. ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
  1113. kfree(name);
  1114. name = NULL;
  1115. if (log_ref_ver) {
  1116. iput(dir);
  1117. dir = NULL;
  1118. }
  1119. }
  1120. /* finally write the back reference in the inode */
  1121. ret = overwrite_item(trans, root, path, eb, slot, key);
  1122. out:
  1123. btrfs_release_path(path);
  1124. kfree(name);
  1125. iput(dir);
  1126. iput(inode);
  1127. return ret;
  1128. }
  1129. static int insert_orphan_item(struct btrfs_trans_handle *trans,
  1130. struct btrfs_root *root, u64 ino)
  1131. {
  1132. int ret;
  1133. ret = btrfs_insert_orphan_item(trans, root, ino);
  1134. if (ret == -EEXIST)
  1135. ret = 0;
  1136. return ret;
  1137. }
  1138. static int count_inode_extrefs(struct btrfs_root *root,
  1139. struct inode *inode, struct btrfs_path *path)
  1140. {
  1141. int ret = 0;
  1142. int name_len;
  1143. unsigned int nlink = 0;
  1144. u32 item_size;
  1145. u32 cur_offset = 0;
  1146. u64 inode_objectid = btrfs_ino(inode);
  1147. u64 offset = 0;
  1148. unsigned long ptr;
  1149. struct btrfs_inode_extref *extref;
  1150. struct extent_buffer *leaf;
  1151. while (1) {
  1152. ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
  1153. &extref, &offset);
  1154. if (ret)
  1155. break;
  1156. leaf = path->nodes[0];
  1157. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1158. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1159. while (cur_offset < item_size) {
  1160. extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
  1161. name_len = btrfs_inode_extref_name_len(leaf, extref);
  1162. nlink++;
  1163. cur_offset += name_len + sizeof(*extref);
  1164. }
  1165. offset++;
  1166. btrfs_release_path(path);
  1167. }
  1168. btrfs_release_path(path);
  1169. if (ret < 0)
  1170. return ret;
  1171. return nlink;
  1172. }
  1173. static int count_inode_refs(struct btrfs_root *root,
  1174. struct inode *inode, struct btrfs_path *path)
  1175. {
  1176. int ret;
  1177. struct btrfs_key key;
  1178. unsigned int nlink = 0;
  1179. unsigned long ptr;
  1180. unsigned long ptr_end;
  1181. int name_len;
  1182. u64 ino = btrfs_ino(inode);
  1183. key.objectid = ino;
  1184. key.type = BTRFS_INODE_REF_KEY;
  1185. key.offset = (u64)-1;
  1186. while (1) {
  1187. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1188. if (ret < 0)
  1189. break;
  1190. if (ret > 0) {
  1191. if (path->slots[0] == 0)
  1192. break;
  1193. path->slots[0]--;
  1194. }
  1195. process_slot:
  1196. btrfs_item_key_to_cpu(path->nodes[0], &key,
  1197. path->slots[0]);
  1198. if (key.objectid != ino ||
  1199. key.type != BTRFS_INODE_REF_KEY)
  1200. break;
  1201. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  1202. ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
  1203. path->slots[0]);
  1204. while (ptr < ptr_end) {
  1205. struct btrfs_inode_ref *ref;
  1206. ref = (struct btrfs_inode_ref *)ptr;
  1207. name_len = btrfs_inode_ref_name_len(path->nodes[0],
  1208. ref);
  1209. ptr = (unsigned long)(ref + 1) + name_len;
  1210. nlink++;
  1211. }
  1212. if (key.offset == 0)
  1213. break;
  1214. if (path->slots[0] > 0) {
  1215. path->slots[0]--;
  1216. goto process_slot;
  1217. }
  1218. key.offset--;
  1219. btrfs_release_path(path);
  1220. }
  1221. btrfs_release_path(path);
  1222. return nlink;
  1223. }
  1224. /*
  1225. * There are a few corners where the link count of the file can't
  1226. * be properly maintained during replay. So, instead of adding
  1227. * lots of complexity to the log code, we just scan the backrefs
  1228. * for any file that has been through replay.
  1229. *
  1230. * The scan will update the link count on the inode to reflect the
  1231. * number of back refs found. If it goes down to zero, the iput
  1232. * will free the inode.
  1233. */
  1234. static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
  1235. struct btrfs_root *root,
  1236. struct inode *inode)
  1237. {
  1238. struct btrfs_path *path;
  1239. int ret;
  1240. u64 nlink = 0;
  1241. u64 ino = btrfs_ino(inode);
  1242. path = btrfs_alloc_path();
  1243. if (!path)
  1244. return -ENOMEM;
  1245. ret = count_inode_refs(root, inode, path);
  1246. if (ret < 0)
  1247. goto out;
  1248. nlink = ret;
  1249. ret = count_inode_extrefs(root, inode, path);
  1250. if (ret == -ENOENT)
  1251. ret = 0;
  1252. if (ret < 0)
  1253. goto out;
  1254. nlink += ret;
  1255. ret = 0;
  1256. if (nlink != inode->i_nlink) {
  1257. set_nlink(inode, nlink);
  1258. btrfs_update_inode(trans, root, inode);
  1259. }
  1260. BTRFS_I(inode)->index_cnt = (u64)-1;
  1261. if (inode->i_nlink == 0) {
  1262. if (S_ISDIR(inode->i_mode)) {
  1263. ret = replay_dir_deletes(trans, root, NULL, path,
  1264. ino, 1);
  1265. if (ret)
  1266. goto out;
  1267. }
  1268. ret = insert_orphan_item(trans, root, ino);
  1269. }
  1270. out:
  1271. btrfs_free_path(path);
  1272. return ret;
  1273. }
  1274. static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
  1275. struct btrfs_root *root,
  1276. struct btrfs_path *path)
  1277. {
  1278. int ret;
  1279. struct btrfs_key key;
  1280. struct inode *inode;
  1281. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  1282. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1283. key.offset = (u64)-1;
  1284. while (1) {
  1285. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1286. if (ret < 0)
  1287. break;
  1288. if (ret == 1) {
  1289. if (path->slots[0] == 0)
  1290. break;
  1291. path->slots[0]--;
  1292. }
  1293. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1294. if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
  1295. key.type != BTRFS_ORPHAN_ITEM_KEY)
  1296. break;
  1297. ret = btrfs_del_item(trans, root, path);
  1298. if (ret)
  1299. goto out;
  1300. btrfs_release_path(path);
  1301. inode = read_one_inode(root, key.offset);
  1302. if (!inode)
  1303. return -EIO;
  1304. ret = fixup_inode_link_count(trans, root, inode);
  1305. iput(inode);
  1306. if (ret)
  1307. goto out;
  1308. /*
  1309. * fixup on a directory may create new entries,
  1310. * make sure we always look for the highset possible
  1311. * offset
  1312. */
  1313. key.offset = (u64)-1;
  1314. }
  1315. ret = 0;
  1316. out:
  1317. btrfs_release_path(path);
  1318. return ret;
  1319. }
  1320. /*
  1321. * record a given inode in the fixup dir so we can check its link
  1322. * count when replay is done. The link count is incremented here
  1323. * so the inode won't go away until we check it
  1324. */
  1325. static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  1326. struct btrfs_root *root,
  1327. struct btrfs_path *path,
  1328. u64 objectid)
  1329. {
  1330. struct btrfs_key key;
  1331. int ret = 0;
  1332. struct inode *inode;
  1333. inode = read_one_inode(root, objectid);
  1334. if (!inode)
  1335. return -EIO;
  1336. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  1337. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1338. key.offset = objectid;
  1339. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  1340. btrfs_release_path(path);
  1341. if (ret == 0) {
  1342. if (!inode->i_nlink)
  1343. set_nlink(inode, 1);
  1344. else
  1345. inc_nlink(inode);
  1346. ret = btrfs_update_inode(trans, root, inode);
  1347. } else if (ret == -EEXIST) {
  1348. ret = 0;
  1349. } else {
  1350. BUG(); /* Logic Error */
  1351. }
  1352. iput(inode);
  1353. return ret;
  1354. }
  1355. /*
  1356. * when replaying the log for a directory, we only insert names
  1357. * for inodes that actually exist. This means an fsync on a directory
  1358. * does not implicitly fsync all the new files in it
  1359. */
  1360. static noinline int insert_one_name(struct btrfs_trans_handle *trans,
  1361. struct btrfs_root *root,
  1362. struct btrfs_path *path,
  1363. u64 dirid, u64 index,
  1364. char *name, int name_len, u8 type,
  1365. struct btrfs_key *location)
  1366. {
  1367. struct inode *inode;
  1368. struct inode *dir;
  1369. int ret;
  1370. inode = read_one_inode(root, location->objectid);
  1371. if (!inode)
  1372. return -ENOENT;
  1373. dir = read_one_inode(root, dirid);
  1374. if (!dir) {
  1375. iput(inode);
  1376. return -EIO;
  1377. }
  1378. ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
  1379. /* FIXME, put inode into FIXUP list */
  1380. iput(inode);
  1381. iput(dir);
  1382. return ret;
  1383. }
  1384. /*
  1385. * take a single entry in a log directory item and replay it into
  1386. * the subvolume.
  1387. *
  1388. * if a conflicting item exists in the subdirectory already,
  1389. * the inode it points to is unlinked and put into the link count
  1390. * fix up tree.
  1391. *
  1392. * If a name from the log points to a file or directory that does
  1393. * not exist in the FS, it is skipped. fsyncs on directories
  1394. * do not force down inodes inside that directory, just changes to the
  1395. * names or unlinks in a directory.
  1396. */
  1397. static noinline int replay_one_name(struct btrfs_trans_handle *trans,
  1398. struct btrfs_root *root,
  1399. struct btrfs_path *path,
  1400. struct extent_buffer *eb,
  1401. struct btrfs_dir_item *di,
  1402. struct btrfs_key *key)
  1403. {
  1404. char *name;
  1405. int name_len;
  1406. struct btrfs_dir_item *dst_di;
  1407. struct btrfs_key found_key;
  1408. struct btrfs_key log_key;
  1409. struct inode *dir;
  1410. u8 log_type;
  1411. int exists;
  1412. int ret = 0;
  1413. bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
  1414. dir = read_one_inode(root, key->objectid);
  1415. if (!dir)
  1416. return -EIO;
  1417. name_len = btrfs_dir_name_len(eb, di);
  1418. name = kmalloc(name_len, GFP_NOFS);
  1419. if (!name) {
  1420. ret = -ENOMEM;
  1421. goto out;
  1422. }
  1423. log_type = btrfs_dir_type(eb, di);
  1424. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1425. name_len);
  1426. btrfs_dir_item_key_to_cpu(eb, di, &log_key);
  1427. exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
  1428. if (exists == 0)
  1429. exists = 1;
  1430. else
  1431. exists = 0;
  1432. btrfs_release_path(path);
  1433. if (key->type == BTRFS_DIR_ITEM_KEY) {
  1434. dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
  1435. name, name_len, 1);
  1436. } else if (key->type == BTRFS_DIR_INDEX_KEY) {
  1437. dst_di = btrfs_lookup_dir_index_item(trans, root, path,
  1438. key->objectid,
  1439. key->offset, name,
  1440. name_len, 1);
  1441. } else {
  1442. /* Corruption */
  1443. ret = -EINVAL;
  1444. goto out;
  1445. }
  1446. if (IS_ERR_OR_NULL(dst_di)) {
  1447. /* we need a sequence number to insert, so we only
  1448. * do inserts for the BTRFS_DIR_INDEX_KEY types
  1449. */
  1450. if (key->type != BTRFS_DIR_INDEX_KEY)
  1451. goto out;
  1452. goto insert;
  1453. }
  1454. btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
  1455. /* the existing item matches the logged item */
  1456. if (found_key.objectid == log_key.objectid &&
  1457. found_key.type == log_key.type &&
  1458. found_key.offset == log_key.offset &&
  1459. btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
  1460. update_size = false;
  1461. goto out;
  1462. }
  1463. /*
  1464. * don't drop the conflicting directory entry if the inode
  1465. * for the new entry doesn't exist
  1466. */
  1467. if (!exists)
  1468. goto out;
  1469. ret = drop_one_dir_item(trans, root, path, dir, dst_di);
  1470. if (ret)
  1471. goto out;
  1472. if (key->type == BTRFS_DIR_INDEX_KEY)
  1473. goto insert;
  1474. out:
  1475. btrfs_release_path(path);
  1476. if (!ret && update_size) {
  1477. btrfs_i_size_write(dir, dir->i_size + name_len * 2);
  1478. ret = btrfs_update_inode(trans, root, dir);
  1479. }
  1480. kfree(name);
  1481. iput(dir);
  1482. return ret;
  1483. insert:
  1484. btrfs_release_path(path);
  1485. ret = insert_one_name(trans, root, path, key->objectid, key->offset,
  1486. name, name_len, log_type, &log_key);
  1487. if (ret && ret != -ENOENT)
  1488. goto out;
  1489. update_size = false;
  1490. ret = 0;
  1491. goto out;
  1492. }
  1493. /*
  1494. * find all the names in a directory item and reconcile them into
  1495. * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
  1496. * one name in a directory item, but the same code gets used for
  1497. * both directory index types
  1498. */
  1499. static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
  1500. struct btrfs_root *root,
  1501. struct btrfs_path *path,
  1502. struct extent_buffer *eb, int slot,
  1503. struct btrfs_key *key)
  1504. {
  1505. int ret;
  1506. u32 item_size = btrfs_item_size_nr(eb, slot);
  1507. struct btrfs_dir_item *di;
  1508. int name_len;
  1509. unsigned long ptr;
  1510. unsigned long ptr_end;
  1511. ptr = btrfs_item_ptr_offset(eb, slot);
  1512. ptr_end = ptr + item_size;
  1513. while (ptr < ptr_end) {
  1514. di = (struct btrfs_dir_item *)ptr;
  1515. if (verify_dir_item(root, eb, di))
  1516. return -EIO;
  1517. name_len = btrfs_dir_name_len(eb, di);
  1518. ret = replay_one_name(trans, root, path, eb, di, key);
  1519. if (ret)
  1520. return ret;
  1521. ptr = (unsigned long)(di + 1);
  1522. ptr += name_len;
  1523. }
  1524. return 0;
  1525. }
  1526. /*
  1527. * directory replay has two parts. There are the standard directory
  1528. * items in the log copied from the subvolume, and range items
  1529. * created in the log while the subvolume was logged.
  1530. *
  1531. * The range items tell us which parts of the key space the log
  1532. * is authoritative for. During replay, if a key in the subvolume
  1533. * directory is in a logged range item, but not actually in the log
  1534. * that means it was deleted from the directory before the fsync
  1535. * and should be removed.
  1536. */
  1537. static noinline int find_dir_range(struct btrfs_root *root,
  1538. struct btrfs_path *path,
  1539. u64 dirid, int key_type,
  1540. u64 *start_ret, u64 *end_ret)
  1541. {
  1542. struct btrfs_key key;
  1543. u64 found_end;
  1544. struct btrfs_dir_log_item *item;
  1545. int ret;
  1546. int nritems;
  1547. if (*start_ret == (u64)-1)
  1548. return 1;
  1549. key.objectid = dirid;
  1550. key.type = key_type;
  1551. key.offset = *start_ret;
  1552. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1553. if (ret < 0)
  1554. goto out;
  1555. if (ret > 0) {
  1556. if (path->slots[0] == 0)
  1557. goto out;
  1558. path->slots[0]--;
  1559. }
  1560. if (ret != 0)
  1561. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1562. if (key.type != key_type || key.objectid != dirid) {
  1563. ret = 1;
  1564. goto next;
  1565. }
  1566. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1567. struct btrfs_dir_log_item);
  1568. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1569. if (*start_ret >= key.offset && *start_ret <= found_end) {
  1570. ret = 0;
  1571. *start_ret = key.offset;
  1572. *end_ret = found_end;
  1573. goto out;
  1574. }
  1575. ret = 1;
  1576. next:
  1577. /* check the next slot in the tree to see if it is a valid item */
  1578. nritems = btrfs_header_nritems(path->nodes[0]);
  1579. if (path->slots[0] >= nritems) {
  1580. ret = btrfs_next_leaf(root, path);
  1581. if (ret)
  1582. goto out;
  1583. } else {
  1584. path->slots[0]++;
  1585. }
  1586. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1587. if (key.type != key_type || key.objectid != dirid) {
  1588. ret = 1;
  1589. goto out;
  1590. }
  1591. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1592. struct btrfs_dir_log_item);
  1593. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1594. *start_ret = key.offset;
  1595. *end_ret = found_end;
  1596. ret = 0;
  1597. out:
  1598. btrfs_release_path(path);
  1599. return ret;
  1600. }
  1601. /*
  1602. * this looks for a given directory item in the log. If the directory
  1603. * item is not in the log, the item is removed and the inode it points
  1604. * to is unlinked
  1605. */
  1606. static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
  1607. struct btrfs_root *root,
  1608. struct btrfs_root *log,
  1609. struct btrfs_path *path,
  1610. struct btrfs_path *log_path,
  1611. struct inode *dir,
  1612. struct btrfs_key *dir_key)
  1613. {
  1614. int ret;
  1615. struct extent_buffer *eb;
  1616. int slot;
  1617. u32 item_size;
  1618. struct btrfs_dir_item *di;
  1619. struct btrfs_dir_item *log_di;
  1620. int name_len;
  1621. unsigned long ptr;
  1622. unsigned long ptr_end;
  1623. char *name;
  1624. struct inode *inode;
  1625. struct btrfs_key location;
  1626. again:
  1627. eb = path->nodes[0];
  1628. slot = path->slots[0];
  1629. item_size = btrfs_item_size_nr(eb, slot);
  1630. ptr = btrfs_item_ptr_offset(eb, slot);
  1631. ptr_end = ptr + item_size;
  1632. while (ptr < ptr_end) {
  1633. di = (struct btrfs_dir_item *)ptr;
  1634. if (verify_dir_item(root, eb, di)) {
  1635. ret = -EIO;
  1636. goto out;
  1637. }
  1638. name_len = btrfs_dir_name_len(eb, di);
  1639. name = kmalloc(name_len, GFP_NOFS);
  1640. if (!name) {
  1641. ret = -ENOMEM;
  1642. goto out;
  1643. }
  1644. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1645. name_len);
  1646. log_di = NULL;
  1647. if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
  1648. log_di = btrfs_lookup_dir_item(trans, log, log_path,
  1649. dir_key->objectid,
  1650. name, name_len, 0);
  1651. } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
  1652. log_di = btrfs_lookup_dir_index_item(trans, log,
  1653. log_path,
  1654. dir_key->objectid,
  1655. dir_key->offset,
  1656. name, name_len, 0);
  1657. }
  1658. if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
  1659. btrfs_dir_item_key_to_cpu(eb, di, &location);
  1660. btrfs_release_path(path);
  1661. btrfs_release_path(log_path);
  1662. inode = read_one_inode(root, location.objectid);
  1663. if (!inode) {
  1664. kfree(name);
  1665. return -EIO;
  1666. }
  1667. ret = link_to_fixup_dir(trans, root,
  1668. path, location.objectid);
  1669. if (ret) {
  1670. kfree(name);
  1671. iput(inode);
  1672. goto out;
  1673. }
  1674. inc_nlink(inode);
  1675. ret = btrfs_unlink_inode(trans, root, dir, inode,
  1676. name, name_len);
  1677. if (!ret)
  1678. ret = btrfs_run_delayed_items(trans, root);
  1679. kfree(name);
  1680. iput(inode);
  1681. if (ret)
  1682. goto out;
  1683. /* there might still be more names under this key
  1684. * check and repeat if required
  1685. */
  1686. ret = btrfs_search_slot(NULL, root, dir_key, path,
  1687. 0, 0);
  1688. if (ret == 0)
  1689. goto again;
  1690. ret = 0;
  1691. goto out;
  1692. } else if (IS_ERR(log_di)) {
  1693. kfree(name);
  1694. return PTR_ERR(log_di);
  1695. }
  1696. btrfs_release_path(log_path);
  1697. kfree(name);
  1698. ptr = (unsigned long)(di + 1);
  1699. ptr += name_len;
  1700. }
  1701. ret = 0;
  1702. out:
  1703. btrfs_release_path(path);
  1704. btrfs_release_path(log_path);
  1705. return ret;
  1706. }
  1707. /*
  1708. * deletion replay happens before we copy any new directory items
  1709. * out of the log or out of backreferences from inodes. It
  1710. * scans the log to find ranges of keys that log is authoritative for,
  1711. * and then scans the directory to find items in those ranges that are
  1712. * not present in the log.
  1713. *
  1714. * Anything we don't find in the log is unlinked and removed from the
  1715. * directory.
  1716. */
  1717. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  1718. struct btrfs_root *root,
  1719. struct btrfs_root *log,
  1720. struct btrfs_path *path,
  1721. u64 dirid, int del_all)
  1722. {
  1723. u64 range_start;
  1724. u64 range_end;
  1725. int key_type = BTRFS_DIR_LOG_ITEM_KEY;
  1726. int ret = 0;
  1727. struct btrfs_key dir_key;
  1728. struct btrfs_key found_key;
  1729. struct btrfs_path *log_path;
  1730. struct inode *dir;
  1731. dir_key.objectid = dirid;
  1732. dir_key.type = BTRFS_DIR_ITEM_KEY;
  1733. log_path = btrfs_alloc_path();
  1734. if (!log_path)
  1735. return -ENOMEM;
  1736. dir = read_one_inode(root, dirid);
  1737. /* it isn't an error if the inode isn't there, that can happen
  1738. * because we replay the deletes before we copy in the inode item
  1739. * from the log
  1740. */
  1741. if (!dir) {
  1742. btrfs_free_path(log_path);
  1743. return 0;
  1744. }
  1745. again:
  1746. range_start = 0;
  1747. range_end = 0;
  1748. while (1) {
  1749. if (del_all)
  1750. range_end = (u64)-1;
  1751. else {
  1752. ret = find_dir_range(log, path, dirid, key_type,
  1753. &range_start, &range_end);
  1754. if (ret != 0)
  1755. break;
  1756. }
  1757. dir_key.offset = range_start;
  1758. while (1) {
  1759. int nritems;
  1760. ret = btrfs_search_slot(NULL, root, &dir_key, path,
  1761. 0, 0);
  1762. if (ret < 0)
  1763. goto out;
  1764. nritems = btrfs_header_nritems(path->nodes[0]);
  1765. if (path->slots[0] >= nritems) {
  1766. ret = btrfs_next_leaf(root, path);
  1767. if (ret)
  1768. break;
  1769. }
  1770. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1771. path->slots[0]);
  1772. if (found_key.objectid != dirid ||
  1773. found_key.type != dir_key.type)
  1774. goto next_type;
  1775. if (found_key.offset > range_end)
  1776. break;
  1777. ret = check_item_in_log(trans, root, log, path,
  1778. log_path, dir,
  1779. &found_key);
  1780. if (ret)
  1781. goto out;
  1782. if (found_key.offset == (u64)-1)
  1783. break;
  1784. dir_key.offset = found_key.offset + 1;
  1785. }
  1786. btrfs_release_path(path);
  1787. if (range_end == (u64)-1)
  1788. break;
  1789. range_start = range_end + 1;
  1790. }
  1791. next_type:
  1792. ret = 0;
  1793. if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
  1794. key_type = BTRFS_DIR_LOG_INDEX_KEY;
  1795. dir_key.type = BTRFS_DIR_INDEX_KEY;
  1796. btrfs_release_path(path);
  1797. goto again;
  1798. }
  1799. out:
  1800. btrfs_release_path(path);
  1801. btrfs_free_path(log_path);
  1802. iput(dir);
  1803. return ret;
  1804. }
  1805. /*
  1806. * the process_func used to replay items from the log tree. This
  1807. * gets called in two different stages. The first stage just looks
  1808. * for inodes and makes sure they are all copied into the subvolume.
  1809. *
  1810. * The second stage copies all the other item types from the log into
  1811. * the subvolume. The two stage approach is slower, but gets rid of
  1812. * lots of complexity around inodes referencing other inodes that exist
  1813. * only in the log (references come from either directory items or inode
  1814. * back refs).
  1815. */
  1816. static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
  1817. struct walk_control *wc, u64 gen)
  1818. {
  1819. int nritems;
  1820. struct btrfs_path *path;
  1821. struct btrfs_root *root = wc->replay_dest;
  1822. struct btrfs_key key;
  1823. int level;
  1824. int i;
  1825. int ret;
  1826. ret = btrfs_read_buffer(eb, gen);
  1827. if (ret)
  1828. return ret;
  1829. level = btrfs_header_level(eb);
  1830. if (level != 0)
  1831. return 0;
  1832. path = btrfs_alloc_path();
  1833. if (!path)
  1834. return -ENOMEM;
  1835. nritems = btrfs_header_nritems(eb);
  1836. for (i = 0; i < nritems; i++) {
  1837. btrfs_item_key_to_cpu(eb, &key, i);
  1838. /* inode keys are done during the first stage */
  1839. if (key.type == BTRFS_INODE_ITEM_KEY &&
  1840. wc->stage == LOG_WALK_REPLAY_INODES) {
  1841. struct btrfs_inode_item *inode_item;
  1842. u32 mode;
  1843. inode_item = btrfs_item_ptr(eb, i,
  1844. struct btrfs_inode_item);
  1845. mode = btrfs_inode_mode(eb, inode_item);
  1846. if (S_ISDIR(mode)) {
  1847. ret = replay_dir_deletes(wc->trans,
  1848. root, log, path, key.objectid, 0);
  1849. if (ret)
  1850. break;
  1851. }
  1852. ret = overwrite_item(wc->trans, root, path,
  1853. eb, i, &key);
  1854. if (ret)
  1855. break;
  1856. /* for regular files, make sure corresponding
  1857. * orhpan item exist. extents past the new EOF
  1858. * will be truncated later by orphan cleanup.
  1859. */
  1860. if (S_ISREG(mode)) {
  1861. ret = insert_orphan_item(wc->trans, root,
  1862. key.objectid);
  1863. if (ret)
  1864. break;
  1865. }
  1866. ret = link_to_fixup_dir(wc->trans, root,
  1867. path, key.objectid);
  1868. if (ret)
  1869. break;
  1870. }
  1871. if (key.type == BTRFS_DIR_INDEX_KEY &&
  1872. wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
  1873. ret = replay_one_dir_item(wc->trans, root, path,
  1874. eb, i, &key);
  1875. if (ret)
  1876. break;
  1877. }
  1878. if (wc->stage < LOG_WALK_REPLAY_ALL)
  1879. continue;
  1880. /* these keys are simply copied */
  1881. if (key.type == BTRFS_XATTR_ITEM_KEY) {
  1882. ret = overwrite_item(wc->trans, root, path,
  1883. eb, i, &key);
  1884. if (ret)
  1885. break;
  1886. } else if (key.type == BTRFS_INODE_REF_KEY ||
  1887. key.type == BTRFS_INODE_EXTREF_KEY) {
  1888. ret = add_inode_ref(wc->trans, root, log, path,
  1889. eb, i, &key);
  1890. if (ret && ret != -ENOENT)
  1891. break;
  1892. ret = 0;
  1893. } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
  1894. ret = replay_one_extent(wc->trans, root, path,
  1895. eb, i, &key);
  1896. if (ret)
  1897. break;
  1898. } else if (key.type == BTRFS_DIR_ITEM_KEY) {
  1899. ret = replay_one_dir_item(wc->trans, root, path,
  1900. eb, i, &key);
  1901. if (ret)
  1902. break;
  1903. }
  1904. }
  1905. btrfs_free_path(path);
  1906. return ret;
  1907. }
  1908. static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
  1909. struct btrfs_root *root,
  1910. struct btrfs_path *path, int *level,
  1911. struct walk_control *wc)
  1912. {
  1913. u64 root_owner;
  1914. u64 bytenr;
  1915. u64 ptr_gen;
  1916. struct extent_buffer *next;
  1917. struct extent_buffer *cur;
  1918. struct extent_buffer *parent;
  1919. u32 blocksize;
  1920. int ret = 0;
  1921. WARN_ON(*level < 0);
  1922. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1923. while (*level > 0) {
  1924. WARN_ON(*level < 0);
  1925. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1926. cur = path->nodes[*level];
  1927. WARN_ON(btrfs_header_level(cur) != *level);
  1928. if (path->slots[*level] >=
  1929. btrfs_header_nritems(cur))
  1930. break;
  1931. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  1932. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  1933. blocksize = root->nodesize;
  1934. parent = path->nodes[*level];
  1935. root_owner = btrfs_header_owner(parent);
  1936. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  1937. if (!next)
  1938. return -ENOMEM;
  1939. if (*level == 1) {
  1940. ret = wc->process_func(root, next, wc, ptr_gen);
  1941. if (ret) {
  1942. free_extent_buffer(next);
  1943. return ret;
  1944. }
  1945. path->slots[*level]++;
  1946. if (wc->free) {
  1947. ret = btrfs_read_buffer(next, ptr_gen);
  1948. if (ret) {
  1949. free_extent_buffer(next);
  1950. return ret;
  1951. }
  1952. if (trans) {
  1953. btrfs_tree_lock(next);
  1954. btrfs_set_lock_blocking(next);
  1955. clean_tree_block(trans, root, next);
  1956. btrfs_wait_tree_block_writeback(next);
  1957. btrfs_tree_unlock(next);
  1958. }
  1959. WARN_ON(root_owner !=
  1960. BTRFS_TREE_LOG_OBJECTID);
  1961. ret = btrfs_free_and_pin_reserved_extent(root,
  1962. bytenr, blocksize);
  1963. if (ret) {
  1964. free_extent_buffer(next);
  1965. return ret;
  1966. }
  1967. }
  1968. free_extent_buffer(next);
  1969. continue;
  1970. }
  1971. ret = btrfs_read_buffer(next, ptr_gen);
  1972. if (ret) {
  1973. free_extent_buffer(next);
  1974. return ret;
  1975. }
  1976. WARN_ON(*level <= 0);
  1977. if (path->nodes[*level-1])
  1978. free_extent_buffer(path->nodes[*level-1]);
  1979. path->nodes[*level-1] = next;
  1980. *level = btrfs_header_level(next);
  1981. path->slots[*level] = 0;
  1982. cond_resched();
  1983. }
  1984. WARN_ON(*level < 0);
  1985. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1986. path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
  1987. cond_resched();
  1988. return 0;
  1989. }
  1990. static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
  1991. struct btrfs_root *root,
  1992. struct btrfs_path *path, int *level,
  1993. struct walk_control *wc)
  1994. {
  1995. u64 root_owner;
  1996. int i;
  1997. int slot;
  1998. int ret;
  1999. for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  2000. slot = path->slots[i];
  2001. if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
  2002. path->slots[i]++;
  2003. *level = i;
  2004. WARN_ON(*level == 0);
  2005. return 0;
  2006. } else {
  2007. struct extent_buffer *parent;
  2008. if (path->nodes[*level] == root->node)
  2009. parent = path->nodes[*level];
  2010. else
  2011. parent = path->nodes[*level + 1];
  2012. root_owner = btrfs_header_owner(parent);
  2013. ret = wc->process_func(root, path->nodes[*level], wc,
  2014. btrfs_header_generation(path->nodes[*level]));
  2015. if (ret)
  2016. return ret;
  2017. if (wc->free) {
  2018. struct extent_buffer *next;
  2019. next = path->nodes[*level];
  2020. if (trans) {
  2021. btrfs_tree_lock(next);
  2022. btrfs_set_lock_blocking(next);
  2023. clean_tree_block(trans, root, next);
  2024. btrfs_wait_tree_block_writeback(next);
  2025. btrfs_tree_unlock(next);
  2026. }
  2027. WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
  2028. ret = btrfs_free_and_pin_reserved_extent(root,
  2029. path->nodes[*level]->start,
  2030. path->nodes[*level]->len);
  2031. if (ret)
  2032. return ret;
  2033. }
  2034. free_extent_buffer(path->nodes[*level]);
  2035. path->nodes[*level] = NULL;
  2036. *level = i + 1;
  2037. }
  2038. }
  2039. return 1;
  2040. }
  2041. /*
  2042. * drop the reference count on the tree rooted at 'snap'. This traverses
  2043. * the tree freeing any blocks that have a ref count of zero after being
  2044. * decremented.
  2045. */
  2046. static int walk_log_tree(struct btrfs_trans_handle *trans,
  2047. struct btrfs_root *log, struct walk_control *wc)
  2048. {
  2049. int ret = 0;
  2050. int wret;
  2051. int level;
  2052. struct btrfs_path *path;
  2053. int orig_level;
  2054. path = btrfs_alloc_path();
  2055. if (!path)
  2056. return -ENOMEM;
  2057. level = btrfs_header_level(log->node);
  2058. orig_level = level;
  2059. path->nodes[level] = log->node;
  2060. extent_buffer_get(log->node);
  2061. path->slots[level] = 0;
  2062. while (1) {
  2063. wret = walk_down_log_tree(trans, log, path, &level, wc);
  2064. if (wret > 0)
  2065. break;
  2066. if (wret < 0) {
  2067. ret = wret;
  2068. goto out;
  2069. }
  2070. wret = walk_up_log_tree(trans, log, path, &level, wc);
  2071. if (wret > 0)
  2072. break;
  2073. if (wret < 0) {
  2074. ret = wret;
  2075. goto out;
  2076. }
  2077. }
  2078. /* was the root node processed? if not, catch it here */
  2079. if (path->nodes[orig_level]) {
  2080. ret = wc->process_func(log, path->nodes[orig_level], wc,
  2081. btrfs_header_generation(path->nodes[orig_level]));
  2082. if (ret)
  2083. goto out;
  2084. if (wc->free) {
  2085. struct extent_buffer *next;
  2086. next = path->nodes[orig_level];
  2087. if (trans) {
  2088. btrfs_tree_lock(next);
  2089. btrfs_set_lock_blocking(next);
  2090. clean_tree_block(trans, log, next);
  2091. btrfs_wait_tree_block_writeback(next);
  2092. btrfs_tree_unlock(next);
  2093. }
  2094. WARN_ON(log->root_key.objectid !=
  2095. BTRFS_TREE_LOG_OBJECTID);
  2096. ret = btrfs_free_and_pin_reserved_extent(log, next->start,
  2097. next->len);
  2098. if (ret)
  2099. goto out;
  2100. }
  2101. }
  2102. out:
  2103. btrfs_free_path(path);
  2104. return ret;
  2105. }
  2106. /*
  2107. * helper function to update the item for a given subvolumes log root
  2108. * in the tree of log roots
  2109. */
  2110. static int update_log_root(struct btrfs_trans_handle *trans,
  2111. struct btrfs_root *log)
  2112. {
  2113. int ret;
  2114. if (log->log_transid == 1) {
  2115. /* insert root item on the first sync */
  2116. ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
  2117. &log->root_key, &log->root_item);
  2118. } else {
  2119. ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
  2120. &log->root_key, &log->root_item);
  2121. }
  2122. return ret;
  2123. }
  2124. static void wait_log_commit(struct btrfs_trans_handle *trans,
  2125. struct btrfs_root *root, int transid)
  2126. {
  2127. DEFINE_WAIT(wait);
  2128. int index = transid % 2;
  2129. /*
  2130. * we only allow two pending log transactions at a time,
  2131. * so we know that if ours is more than 2 older than the
  2132. * current transaction, we're done
  2133. */
  2134. do {
  2135. prepare_to_wait(&root->log_commit_wait[index],
  2136. &wait, TASK_UNINTERRUPTIBLE);
  2137. mutex_unlock(&root->log_mutex);
  2138. if (root->log_transid_committed < transid &&
  2139. atomic_read(&root->log_commit[index]))
  2140. schedule();
  2141. finish_wait(&root->log_commit_wait[index], &wait);
  2142. mutex_lock(&root->log_mutex);
  2143. } while (root->log_transid_committed < transid &&
  2144. atomic_read(&root->log_commit[index]));
  2145. }
  2146. static void wait_for_writer(struct btrfs_trans_handle *trans,
  2147. struct btrfs_root *root)
  2148. {
  2149. DEFINE_WAIT(wait);
  2150. while (atomic_read(&root->log_writers)) {
  2151. prepare_to_wait(&root->log_writer_wait,
  2152. &wait, TASK_UNINTERRUPTIBLE);
  2153. mutex_unlock(&root->log_mutex);
  2154. if (atomic_read(&root->log_writers))
  2155. schedule();
  2156. mutex_lock(&root->log_mutex);
  2157. finish_wait(&root->log_writer_wait, &wait);
  2158. }
  2159. }
  2160. static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
  2161. struct btrfs_log_ctx *ctx)
  2162. {
  2163. if (!ctx)
  2164. return;
  2165. mutex_lock(&root->log_mutex);
  2166. list_del_init(&ctx->list);
  2167. mutex_unlock(&root->log_mutex);
  2168. }
  2169. /*
  2170. * Invoked in log mutex context, or be sure there is no other task which
  2171. * can access the list.
  2172. */
  2173. static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
  2174. int index, int error)
  2175. {
  2176. struct btrfs_log_ctx *ctx;
  2177. if (!error) {
  2178. INIT_LIST_HEAD(&root->log_ctxs[index]);
  2179. return;
  2180. }
  2181. list_for_each_entry(ctx, &root->log_ctxs[index], list)
  2182. ctx->log_ret = error;
  2183. INIT_LIST_HEAD(&root->log_ctxs[index]);
  2184. }
  2185. /*
  2186. * btrfs_sync_log does sends a given tree log down to the disk and
  2187. * updates the super blocks to record it. When this call is done,
  2188. * you know that any inodes previously logged are safely on disk only
  2189. * if it returns 0.
  2190. *
  2191. * Any other return value means you need to call btrfs_commit_transaction.
  2192. * Some of the edge cases for fsyncing directories that have had unlinks
  2193. * or renames done in the past mean that sometimes the only safe
  2194. * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
  2195. * that has happened.
  2196. */
  2197. int btrfs_sync_log(struct btrfs_trans_handle *trans,
  2198. struct btrfs_root *root, struct btrfs_log_ctx *ctx)
  2199. {
  2200. int index1;
  2201. int index2;
  2202. int mark;
  2203. int ret;
  2204. struct btrfs_root *log = root->log_root;
  2205. struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
  2206. int log_transid = 0;
  2207. struct btrfs_log_ctx root_log_ctx;
  2208. struct blk_plug plug;
  2209. mutex_lock(&root->log_mutex);
  2210. log_transid = ctx->log_transid;
  2211. if (root->log_transid_committed >= log_transid) {
  2212. mutex_unlock(&root->log_mutex);
  2213. return ctx->log_ret;
  2214. }
  2215. index1 = log_transid % 2;
  2216. if (atomic_read(&root->log_commit[index1])) {
  2217. wait_log_commit(trans, root, log_transid);
  2218. mutex_unlock(&root->log_mutex);
  2219. return ctx->log_ret;
  2220. }
  2221. ASSERT(log_transid == root->log_transid);
  2222. atomic_set(&root->log_commit[index1], 1);
  2223. /* wait for previous tree log sync to complete */
  2224. if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
  2225. wait_log_commit(trans, root, log_transid - 1);
  2226. while (1) {
  2227. int batch = atomic_read(&root->log_batch);
  2228. /* when we're on an ssd, just kick the log commit out */
  2229. if (!btrfs_test_opt(root, SSD) &&
  2230. test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
  2231. mutex_unlock(&root->log_mutex);
  2232. schedule_timeout_uninterruptible(1);
  2233. mutex_lock(&root->log_mutex);
  2234. }
  2235. wait_for_writer(trans, root);
  2236. if (batch == atomic_read(&root->log_batch))
  2237. break;
  2238. }
  2239. /* bail out if we need to do a full commit */
  2240. if (btrfs_need_log_full_commit(root->fs_info, trans)) {
  2241. ret = -EAGAIN;
  2242. btrfs_free_logged_extents(log, log_transid);
  2243. mutex_unlock(&root->log_mutex);
  2244. goto out;
  2245. }
  2246. if (log_transid % 2 == 0)
  2247. mark = EXTENT_DIRTY;
  2248. else
  2249. mark = EXTENT_NEW;
  2250. /* we start IO on all the marked extents here, but we don't actually
  2251. * wait for them until later.
  2252. */
  2253. blk_start_plug(&plug);
  2254. ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
  2255. if (ret) {
  2256. blk_finish_plug(&plug);
  2257. btrfs_abort_transaction(trans, root, ret);
  2258. btrfs_free_logged_extents(log, log_transid);
  2259. btrfs_set_log_full_commit(root->fs_info, trans);
  2260. mutex_unlock(&root->log_mutex);
  2261. goto out;
  2262. }
  2263. btrfs_set_root_node(&log->root_item, log->node);
  2264. root->log_transid++;
  2265. log->log_transid = root->log_transid;
  2266. root->log_start_pid = 0;
  2267. /*
  2268. * IO has been started, blocks of the log tree have WRITTEN flag set
  2269. * in their headers. new modifications of the log will be written to
  2270. * new positions. so it's safe to allow log writers to go in.
  2271. */
  2272. mutex_unlock(&root->log_mutex);
  2273. btrfs_init_log_ctx(&root_log_ctx);
  2274. mutex_lock(&log_root_tree->log_mutex);
  2275. atomic_inc(&log_root_tree->log_batch);
  2276. atomic_inc(&log_root_tree->log_writers);
  2277. index2 = log_root_tree->log_transid % 2;
  2278. list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
  2279. root_log_ctx.log_transid = log_root_tree->log_transid;
  2280. mutex_unlock(&log_root_tree->log_mutex);
  2281. ret = update_log_root(trans, log);
  2282. mutex_lock(&log_root_tree->log_mutex);
  2283. if (atomic_dec_and_test(&log_root_tree->log_writers)) {
  2284. smp_mb();
  2285. if (waitqueue_active(&log_root_tree->log_writer_wait))
  2286. wake_up(&log_root_tree->log_writer_wait);
  2287. }
  2288. if (ret) {
  2289. if (!list_empty(&root_log_ctx.list))
  2290. list_del_init(&root_log_ctx.list);
  2291. blk_finish_plug(&plug);
  2292. btrfs_set_log_full_commit(root->fs_info, trans);
  2293. if (ret != -ENOSPC) {
  2294. btrfs_abort_transaction(trans, root, ret);
  2295. mutex_unlock(&log_root_tree->log_mutex);
  2296. goto out;
  2297. }
  2298. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2299. btrfs_free_logged_extents(log, log_transid);
  2300. mutex_unlock(&log_root_tree->log_mutex);
  2301. ret = -EAGAIN;
  2302. goto out;
  2303. }
  2304. if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
  2305. mutex_unlock(&log_root_tree->log_mutex);
  2306. ret = root_log_ctx.log_ret;
  2307. goto out;
  2308. }
  2309. index2 = root_log_ctx.log_transid % 2;
  2310. if (atomic_read(&log_root_tree->log_commit[index2])) {
  2311. blk_finish_plug(&plug);
  2312. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2313. btrfs_wait_logged_extents(trans, log, log_transid);
  2314. wait_log_commit(trans, log_root_tree,
  2315. root_log_ctx.log_transid);
  2316. mutex_unlock(&log_root_tree->log_mutex);
  2317. ret = root_log_ctx.log_ret;
  2318. goto out;
  2319. }
  2320. ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
  2321. atomic_set(&log_root_tree->log_commit[index2], 1);
  2322. if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
  2323. wait_log_commit(trans, log_root_tree,
  2324. root_log_ctx.log_transid - 1);
  2325. }
  2326. wait_for_writer(trans, log_root_tree);
  2327. /*
  2328. * now that we've moved on to the tree of log tree roots,
  2329. * check the full commit flag again
  2330. */
  2331. if (btrfs_need_log_full_commit(root->fs_info, trans)) {
  2332. blk_finish_plug(&plug);
  2333. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2334. btrfs_free_logged_extents(log, log_transid);
  2335. mutex_unlock(&log_root_tree->log_mutex);
  2336. ret = -EAGAIN;
  2337. goto out_wake_log_root;
  2338. }
  2339. ret = btrfs_write_marked_extents(log_root_tree,
  2340. &log_root_tree->dirty_log_pages,
  2341. EXTENT_DIRTY | EXTENT_NEW);
  2342. blk_finish_plug(&plug);
  2343. if (ret) {
  2344. btrfs_set_log_full_commit(root->fs_info, trans);
  2345. btrfs_abort_transaction(trans, root, ret);
  2346. btrfs_free_logged_extents(log, log_transid);
  2347. mutex_unlock(&log_root_tree->log_mutex);
  2348. goto out_wake_log_root;
  2349. }
  2350. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2351. btrfs_wait_marked_extents(log_root_tree,
  2352. &log_root_tree->dirty_log_pages,
  2353. EXTENT_NEW | EXTENT_DIRTY);
  2354. btrfs_wait_logged_extents(trans, log, log_transid);
  2355. btrfs_set_super_log_root(root->fs_info->super_for_commit,
  2356. log_root_tree->node->start);
  2357. btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
  2358. btrfs_header_level(log_root_tree->node));
  2359. log_root_tree->log_transid++;
  2360. mutex_unlock(&log_root_tree->log_mutex);
  2361. /*
  2362. * nobody else is going to jump in and write the the ctree
  2363. * super here because the log_commit atomic below is protecting
  2364. * us. We must be called with a transaction handle pinning
  2365. * the running transaction open, so a full commit can't hop
  2366. * in and cause problems either.
  2367. */
  2368. ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
  2369. if (ret) {
  2370. btrfs_set_log_full_commit(root->fs_info, trans);
  2371. btrfs_abort_transaction(trans, root, ret);
  2372. goto out_wake_log_root;
  2373. }
  2374. mutex_lock(&root->log_mutex);
  2375. if (root->last_log_commit < log_transid)
  2376. root->last_log_commit = log_transid;
  2377. mutex_unlock(&root->log_mutex);
  2378. out_wake_log_root:
  2379. /*
  2380. * We needn't get log_mutex here because we are sure all
  2381. * the other tasks are blocked.
  2382. */
  2383. btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
  2384. mutex_lock(&log_root_tree->log_mutex);
  2385. log_root_tree->log_transid_committed++;
  2386. atomic_set(&log_root_tree->log_commit[index2], 0);
  2387. mutex_unlock(&log_root_tree->log_mutex);
  2388. if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
  2389. wake_up(&log_root_tree->log_commit_wait[index2]);
  2390. out:
  2391. /* See above. */
  2392. btrfs_remove_all_log_ctxs(root, index1, ret);
  2393. mutex_lock(&root->log_mutex);
  2394. root->log_transid_committed++;
  2395. atomic_set(&root->log_commit[index1], 0);
  2396. mutex_unlock(&root->log_mutex);
  2397. if (waitqueue_active(&root->log_commit_wait[index1]))
  2398. wake_up(&root->log_commit_wait[index1]);
  2399. return ret;
  2400. }
  2401. static void free_log_tree(struct btrfs_trans_handle *trans,
  2402. struct btrfs_root *log)
  2403. {
  2404. int ret;
  2405. u64 start;
  2406. u64 end;
  2407. struct walk_control wc = {
  2408. .free = 1,
  2409. .process_func = process_one_buffer
  2410. };
  2411. ret = walk_log_tree(trans, log, &wc);
  2412. /* I don't think this can happen but just in case */
  2413. if (ret)
  2414. btrfs_abort_transaction(trans, log, ret);
  2415. while (1) {
  2416. ret = find_first_extent_bit(&log->dirty_log_pages,
  2417. 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
  2418. NULL);
  2419. if (ret)
  2420. break;
  2421. clear_extent_bits(&log->dirty_log_pages, start, end,
  2422. EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
  2423. }
  2424. /*
  2425. * We may have short-circuited the log tree with the full commit logic
  2426. * and left ordered extents on our list, so clear these out to keep us
  2427. * from leaking inodes and memory.
  2428. */
  2429. btrfs_free_logged_extents(log, 0);
  2430. btrfs_free_logged_extents(log, 1);
  2431. free_extent_buffer(log->node);
  2432. kfree(log);
  2433. }
  2434. /*
  2435. * free all the extents used by the tree log. This should be called
  2436. * at commit time of the full transaction
  2437. */
  2438. int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
  2439. {
  2440. if (root->log_root) {
  2441. free_log_tree(trans, root->log_root);
  2442. root->log_root = NULL;
  2443. }
  2444. return 0;
  2445. }
  2446. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  2447. struct btrfs_fs_info *fs_info)
  2448. {
  2449. if (fs_info->log_root_tree) {
  2450. free_log_tree(trans, fs_info->log_root_tree);
  2451. fs_info->log_root_tree = NULL;
  2452. }
  2453. return 0;
  2454. }
  2455. /*
  2456. * If both a file and directory are logged, and unlinks or renames are
  2457. * mixed in, we have a few interesting corners:
  2458. *
  2459. * create file X in dir Y
  2460. * link file X to X.link in dir Y
  2461. * fsync file X
  2462. * unlink file X but leave X.link
  2463. * fsync dir Y
  2464. *
  2465. * After a crash we would expect only X.link to exist. But file X
  2466. * didn't get fsync'd again so the log has back refs for X and X.link.
  2467. *
  2468. * We solve this by removing directory entries and inode backrefs from the
  2469. * log when a file that was logged in the current transaction is
  2470. * unlinked. Any later fsync will include the updated log entries, and
  2471. * we'll be able to reconstruct the proper directory items from backrefs.
  2472. *
  2473. * This optimizations allows us to avoid relogging the entire inode
  2474. * or the entire directory.
  2475. */
  2476. int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
  2477. struct btrfs_root *root,
  2478. const char *name, int name_len,
  2479. struct inode *dir, u64 index)
  2480. {
  2481. struct btrfs_root *log;
  2482. struct btrfs_dir_item *di;
  2483. struct btrfs_path *path;
  2484. int ret;
  2485. int err = 0;
  2486. int bytes_del = 0;
  2487. u64 dir_ino = btrfs_ino(dir);
  2488. if (BTRFS_I(dir)->logged_trans < trans->transid)
  2489. return 0;
  2490. ret = join_running_log_trans(root);
  2491. if (ret)
  2492. return 0;
  2493. mutex_lock(&BTRFS_I(dir)->log_mutex);
  2494. log = root->log_root;
  2495. path = btrfs_alloc_path();
  2496. if (!path) {
  2497. err = -ENOMEM;
  2498. goto out_unlock;
  2499. }
  2500. di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
  2501. name, name_len, -1);
  2502. if (IS_ERR(di)) {
  2503. err = PTR_ERR(di);
  2504. goto fail;
  2505. }
  2506. if (di) {
  2507. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2508. bytes_del += name_len;
  2509. if (ret) {
  2510. err = ret;
  2511. goto fail;
  2512. }
  2513. }
  2514. btrfs_release_path(path);
  2515. di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
  2516. index, name, name_len, -1);
  2517. if (IS_ERR(di)) {
  2518. err = PTR_ERR(di);
  2519. goto fail;
  2520. }
  2521. if (di) {
  2522. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2523. bytes_del += name_len;
  2524. if (ret) {
  2525. err = ret;
  2526. goto fail;
  2527. }
  2528. }
  2529. /* update the directory size in the log to reflect the names
  2530. * we have removed
  2531. */
  2532. if (bytes_del) {
  2533. struct btrfs_key key;
  2534. key.objectid = dir_ino;
  2535. key.offset = 0;
  2536. key.type = BTRFS_INODE_ITEM_KEY;
  2537. btrfs_release_path(path);
  2538. ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
  2539. if (ret < 0) {
  2540. err = ret;
  2541. goto fail;
  2542. }
  2543. if (ret == 0) {
  2544. struct btrfs_inode_item *item;
  2545. u64 i_size;
  2546. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2547. struct btrfs_inode_item);
  2548. i_size = btrfs_inode_size(path->nodes[0], item);
  2549. if (i_size > bytes_del)
  2550. i_size -= bytes_del;
  2551. else
  2552. i_size = 0;
  2553. btrfs_set_inode_size(path->nodes[0], item, i_size);
  2554. btrfs_mark_buffer_dirty(path->nodes[0]);
  2555. } else
  2556. ret = 0;
  2557. btrfs_release_path(path);
  2558. }
  2559. fail:
  2560. btrfs_free_path(path);
  2561. out_unlock:
  2562. mutex_unlock(&BTRFS_I(dir)->log_mutex);
  2563. if (ret == -ENOSPC) {
  2564. btrfs_set_log_full_commit(root->fs_info, trans);
  2565. ret = 0;
  2566. } else if (ret < 0)
  2567. btrfs_abort_transaction(trans, root, ret);
  2568. btrfs_end_log_trans(root);
  2569. return err;
  2570. }
  2571. /* see comments for btrfs_del_dir_entries_in_log */
  2572. int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
  2573. struct btrfs_root *root,
  2574. const char *name, int name_len,
  2575. struct inode *inode, u64 dirid)
  2576. {
  2577. struct btrfs_root *log;
  2578. u64 index;
  2579. int ret;
  2580. if (BTRFS_I(inode)->logged_trans < trans->transid)
  2581. return 0;
  2582. ret = join_running_log_trans(root);
  2583. if (ret)
  2584. return 0;
  2585. log = root->log_root;
  2586. mutex_lock(&BTRFS_I(inode)->log_mutex);
  2587. ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
  2588. dirid, &index);
  2589. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  2590. if (ret == -ENOSPC) {
  2591. btrfs_set_log_full_commit(root->fs_info, trans);
  2592. ret = 0;
  2593. } else if (ret < 0 && ret != -ENOENT)
  2594. btrfs_abort_transaction(trans, root, ret);
  2595. btrfs_end_log_trans(root);
  2596. return ret;
  2597. }
  2598. /*
  2599. * creates a range item in the log for 'dirid'. first_offset and
  2600. * last_offset tell us which parts of the key space the log should
  2601. * be considered authoritative for.
  2602. */
  2603. static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
  2604. struct btrfs_root *log,
  2605. struct btrfs_path *path,
  2606. int key_type, u64 dirid,
  2607. u64 first_offset, u64 last_offset)
  2608. {
  2609. int ret;
  2610. struct btrfs_key key;
  2611. struct btrfs_dir_log_item *item;
  2612. key.objectid = dirid;
  2613. key.offset = first_offset;
  2614. if (key_type == BTRFS_DIR_ITEM_KEY)
  2615. key.type = BTRFS_DIR_LOG_ITEM_KEY;
  2616. else
  2617. key.type = BTRFS_DIR_LOG_INDEX_KEY;
  2618. ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
  2619. if (ret)
  2620. return ret;
  2621. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2622. struct btrfs_dir_log_item);
  2623. btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
  2624. btrfs_mark_buffer_dirty(path->nodes[0]);
  2625. btrfs_release_path(path);
  2626. return 0;
  2627. }
  2628. /*
  2629. * log all the items included in the current transaction for a given
  2630. * directory. This also creates the range items in the log tree required
  2631. * to replay anything deleted before the fsync
  2632. */
  2633. static noinline int log_dir_items(struct btrfs_trans_handle *trans,
  2634. struct btrfs_root *root, struct inode *inode,
  2635. struct btrfs_path *path,
  2636. struct btrfs_path *dst_path, int key_type,
  2637. u64 min_offset, u64 *last_offset_ret)
  2638. {
  2639. struct btrfs_key min_key;
  2640. struct btrfs_root *log = root->log_root;
  2641. struct extent_buffer *src;
  2642. int err = 0;
  2643. int ret;
  2644. int i;
  2645. int nritems;
  2646. u64 first_offset = min_offset;
  2647. u64 last_offset = (u64)-1;
  2648. u64 ino = btrfs_ino(inode);
  2649. log = root->log_root;
  2650. min_key.objectid = ino;
  2651. min_key.type = key_type;
  2652. min_key.offset = min_offset;
  2653. ret = btrfs_search_forward(root, &min_key, path, trans->transid);
  2654. /*
  2655. * we didn't find anything from this transaction, see if there
  2656. * is anything at all
  2657. */
  2658. if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
  2659. min_key.objectid = ino;
  2660. min_key.type = key_type;
  2661. min_key.offset = (u64)-1;
  2662. btrfs_release_path(path);
  2663. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2664. if (ret < 0) {
  2665. btrfs_release_path(path);
  2666. return ret;
  2667. }
  2668. ret = btrfs_previous_item(root, path, ino, key_type);
  2669. /* if ret == 0 there are items for this type,
  2670. * create a range to tell us the last key of this type.
  2671. * otherwise, there are no items in this directory after
  2672. * *min_offset, and we create a range to indicate that.
  2673. */
  2674. if (ret == 0) {
  2675. struct btrfs_key tmp;
  2676. btrfs_item_key_to_cpu(path->nodes[0], &tmp,
  2677. path->slots[0]);
  2678. if (key_type == tmp.type)
  2679. first_offset = max(min_offset, tmp.offset) + 1;
  2680. }
  2681. goto done;
  2682. }
  2683. /* go backward to find any previous key */
  2684. ret = btrfs_previous_item(root, path, ino, key_type);
  2685. if (ret == 0) {
  2686. struct btrfs_key tmp;
  2687. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  2688. if (key_type == tmp.type) {
  2689. first_offset = tmp.offset;
  2690. ret = overwrite_item(trans, log, dst_path,
  2691. path->nodes[0], path->slots[0],
  2692. &tmp);
  2693. if (ret) {
  2694. err = ret;
  2695. goto done;
  2696. }
  2697. }
  2698. }
  2699. btrfs_release_path(path);
  2700. /* find the first key from this transaction again */
  2701. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2702. if (WARN_ON(ret != 0))
  2703. goto done;
  2704. /*
  2705. * we have a block from this transaction, log every item in it
  2706. * from our directory
  2707. */
  2708. while (1) {
  2709. struct btrfs_key tmp;
  2710. src = path->nodes[0];
  2711. nritems = btrfs_header_nritems(src);
  2712. for (i = path->slots[0]; i < nritems; i++) {
  2713. btrfs_item_key_to_cpu(src, &min_key, i);
  2714. if (min_key.objectid != ino || min_key.type != key_type)
  2715. goto done;
  2716. ret = overwrite_item(trans, log, dst_path, src, i,
  2717. &min_key);
  2718. if (ret) {
  2719. err = ret;
  2720. goto done;
  2721. }
  2722. }
  2723. path->slots[0] = nritems;
  2724. /*
  2725. * look ahead to the next item and see if it is also
  2726. * from this directory and from this transaction
  2727. */
  2728. ret = btrfs_next_leaf(root, path);
  2729. if (ret == 1) {
  2730. last_offset = (u64)-1;
  2731. goto done;
  2732. }
  2733. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  2734. if (tmp.objectid != ino || tmp.type != key_type) {
  2735. last_offset = (u64)-1;
  2736. goto done;
  2737. }
  2738. if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
  2739. ret = overwrite_item(trans, log, dst_path,
  2740. path->nodes[0], path->slots[0],
  2741. &tmp);
  2742. if (ret)
  2743. err = ret;
  2744. else
  2745. last_offset = tmp.offset;
  2746. goto done;
  2747. }
  2748. }
  2749. done:
  2750. btrfs_release_path(path);
  2751. btrfs_release_path(dst_path);
  2752. if (err == 0) {
  2753. *last_offset_ret = last_offset;
  2754. /*
  2755. * insert the log range keys to indicate where the log
  2756. * is valid
  2757. */
  2758. ret = insert_dir_log_key(trans, log, path, key_type,
  2759. ino, first_offset, last_offset);
  2760. if (ret)
  2761. err = ret;
  2762. }
  2763. return err;
  2764. }
  2765. /*
  2766. * logging directories is very similar to logging inodes, We find all the items
  2767. * from the current transaction and write them to the log.
  2768. *
  2769. * The recovery code scans the directory in the subvolume, and if it finds a
  2770. * key in the range logged that is not present in the log tree, then it means
  2771. * that dir entry was unlinked during the transaction.
  2772. *
  2773. * In order for that scan to work, we must include one key smaller than
  2774. * the smallest logged by this transaction and one key larger than the largest
  2775. * key logged by this transaction.
  2776. */
  2777. static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
  2778. struct btrfs_root *root, struct inode *inode,
  2779. struct btrfs_path *path,
  2780. struct btrfs_path *dst_path)
  2781. {
  2782. u64 min_key;
  2783. u64 max_key;
  2784. int ret;
  2785. int key_type = BTRFS_DIR_ITEM_KEY;
  2786. again:
  2787. min_key = 0;
  2788. max_key = 0;
  2789. while (1) {
  2790. ret = log_dir_items(trans, root, inode, path,
  2791. dst_path, key_type, min_key,
  2792. &max_key);
  2793. if (ret)
  2794. return ret;
  2795. if (max_key == (u64)-1)
  2796. break;
  2797. min_key = max_key + 1;
  2798. }
  2799. if (key_type == BTRFS_DIR_ITEM_KEY) {
  2800. key_type = BTRFS_DIR_INDEX_KEY;
  2801. goto again;
  2802. }
  2803. return 0;
  2804. }
  2805. /*
  2806. * a helper function to drop items from the log before we relog an
  2807. * inode. max_key_type indicates the highest item type to remove.
  2808. * This cannot be run for file data extents because it does not
  2809. * free the extents they point to.
  2810. */
  2811. static int drop_objectid_items(struct btrfs_trans_handle *trans,
  2812. struct btrfs_root *log,
  2813. struct btrfs_path *path,
  2814. u64 objectid, int max_key_type)
  2815. {
  2816. int ret;
  2817. struct btrfs_key key;
  2818. struct btrfs_key found_key;
  2819. int start_slot;
  2820. key.objectid = objectid;
  2821. key.type = max_key_type;
  2822. key.offset = (u64)-1;
  2823. while (1) {
  2824. ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
  2825. BUG_ON(ret == 0); /* Logic error */
  2826. if (ret < 0)
  2827. break;
  2828. if (path->slots[0] == 0)
  2829. break;
  2830. path->slots[0]--;
  2831. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2832. path->slots[0]);
  2833. if (found_key.objectid != objectid)
  2834. break;
  2835. found_key.offset = 0;
  2836. found_key.type = 0;
  2837. ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
  2838. &start_slot);
  2839. ret = btrfs_del_items(trans, log, path, start_slot,
  2840. path->slots[0] - start_slot + 1);
  2841. /*
  2842. * If start slot isn't 0 then we don't need to re-search, we've
  2843. * found the last guy with the objectid in this tree.
  2844. */
  2845. if (ret || start_slot != 0)
  2846. break;
  2847. btrfs_release_path(path);
  2848. }
  2849. btrfs_release_path(path);
  2850. if (ret > 0)
  2851. ret = 0;
  2852. return ret;
  2853. }
  2854. static void fill_inode_item(struct btrfs_trans_handle *trans,
  2855. struct extent_buffer *leaf,
  2856. struct btrfs_inode_item *item,
  2857. struct inode *inode, int log_inode_only,
  2858. u64 logged_isize)
  2859. {
  2860. struct btrfs_map_token token;
  2861. btrfs_init_map_token(&token);
  2862. if (log_inode_only) {
  2863. /* set the generation to zero so the recover code
  2864. * can tell the difference between an logging
  2865. * just to say 'this inode exists' and a logging
  2866. * to say 'update this inode with these values'
  2867. */
  2868. btrfs_set_token_inode_generation(leaf, item, 0, &token);
  2869. btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
  2870. } else {
  2871. btrfs_set_token_inode_generation(leaf, item,
  2872. BTRFS_I(inode)->generation,
  2873. &token);
  2874. btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
  2875. }
  2876. btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
  2877. btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
  2878. btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
  2879. btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
  2880. btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
  2881. inode->i_atime.tv_sec, &token);
  2882. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
  2883. inode->i_atime.tv_nsec, &token);
  2884. btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
  2885. inode->i_mtime.tv_sec, &token);
  2886. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
  2887. inode->i_mtime.tv_nsec, &token);
  2888. btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
  2889. inode->i_ctime.tv_sec, &token);
  2890. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
  2891. inode->i_ctime.tv_nsec, &token);
  2892. btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
  2893. &token);
  2894. btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
  2895. btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
  2896. btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
  2897. btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
  2898. btrfs_set_token_inode_block_group(leaf, item, 0, &token);
  2899. }
  2900. static int log_inode_item(struct btrfs_trans_handle *trans,
  2901. struct btrfs_root *log, struct btrfs_path *path,
  2902. struct inode *inode)
  2903. {
  2904. struct btrfs_inode_item *inode_item;
  2905. int ret;
  2906. ret = btrfs_insert_empty_item(trans, log, path,
  2907. &BTRFS_I(inode)->location,
  2908. sizeof(*inode_item));
  2909. if (ret && ret != -EEXIST)
  2910. return ret;
  2911. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2912. struct btrfs_inode_item);
  2913. fill_inode_item(trans, path->nodes[0], inode_item, inode, 0, 0);
  2914. btrfs_release_path(path);
  2915. return 0;
  2916. }
  2917. static noinline int copy_items(struct btrfs_trans_handle *trans,
  2918. struct inode *inode,
  2919. struct btrfs_path *dst_path,
  2920. struct btrfs_path *src_path, u64 *last_extent,
  2921. int start_slot, int nr, int inode_only,
  2922. u64 logged_isize)
  2923. {
  2924. unsigned long src_offset;
  2925. unsigned long dst_offset;
  2926. struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
  2927. struct btrfs_file_extent_item *extent;
  2928. struct btrfs_inode_item *inode_item;
  2929. struct extent_buffer *src = src_path->nodes[0];
  2930. struct btrfs_key first_key, last_key, key;
  2931. int ret;
  2932. struct btrfs_key *ins_keys;
  2933. u32 *ins_sizes;
  2934. char *ins_data;
  2935. int i;
  2936. struct list_head ordered_sums;
  2937. int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  2938. bool has_extents = false;
  2939. bool need_find_last_extent = true;
  2940. bool done = false;
  2941. INIT_LIST_HEAD(&ordered_sums);
  2942. ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
  2943. nr * sizeof(u32), GFP_NOFS);
  2944. if (!ins_data)
  2945. return -ENOMEM;
  2946. first_key.objectid = (u64)-1;
  2947. ins_sizes = (u32 *)ins_data;
  2948. ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
  2949. for (i = 0; i < nr; i++) {
  2950. ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
  2951. btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
  2952. }
  2953. ret = btrfs_insert_empty_items(trans, log, dst_path,
  2954. ins_keys, ins_sizes, nr);
  2955. if (ret) {
  2956. kfree(ins_data);
  2957. return ret;
  2958. }
  2959. for (i = 0; i < nr; i++, dst_path->slots[0]++) {
  2960. dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
  2961. dst_path->slots[0]);
  2962. src_offset = btrfs_item_ptr_offset(src, start_slot + i);
  2963. if ((i == (nr - 1)))
  2964. last_key = ins_keys[i];
  2965. if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
  2966. inode_item = btrfs_item_ptr(dst_path->nodes[0],
  2967. dst_path->slots[0],
  2968. struct btrfs_inode_item);
  2969. fill_inode_item(trans, dst_path->nodes[0], inode_item,
  2970. inode, inode_only == LOG_INODE_EXISTS,
  2971. logged_isize);
  2972. } else {
  2973. copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
  2974. src_offset, ins_sizes[i]);
  2975. }
  2976. /*
  2977. * We set need_find_last_extent here in case we know we were
  2978. * processing other items and then walk into the first extent in
  2979. * the inode. If we don't hit an extent then nothing changes,
  2980. * we'll do the last search the next time around.
  2981. */
  2982. if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
  2983. has_extents = true;
  2984. if (first_key.objectid == (u64)-1)
  2985. first_key = ins_keys[i];
  2986. } else {
  2987. need_find_last_extent = false;
  2988. }
  2989. /* take a reference on file data extents so that truncates
  2990. * or deletes of this inode don't have to relog the inode
  2991. * again
  2992. */
  2993. if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
  2994. !skip_csum) {
  2995. int found_type;
  2996. extent = btrfs_item_ptr(src, start_slot + i,
  2997. struct btrfs_file_extent_item);
  2998. if (btrfs_file_extent_generation(src, extent) < trans->transid)
  2999. continue;
  3000. found_type = btrfs_file_extent_type(src, extent);
  3001. if (found_type == BTRFS_FILE_EXTENT_REG) {
  3002. u64 ds, dl, cs, cl;
  3003. ds = btrfs_file_extent_disk_bytenr(src,
  3004. extent);
  3005. /* ds == 0 is a hole */
  3006. if (ds == 0)
  3007. continue;
  3008. dl = btrfs_file_extent_disk_num_bytes(src,
  3009. extent);
  3010. cs = btrfs_file_extent_offset(src, extent);
  3011. cl = btrfs_file_extent_num_bytes(src,
  3012. extent);
  3013. if (btrfs_file_extent_compression(src,
  3014. extent)) {
  3015. cs = 0;
  3016. cl = dl;
  3017. }
  3018. ret = btrfs_lookup_csums_range(
  3019. log->fs_info->csum_root,
  3020. ds + cs, ds + cs + cl - 1,
  3021. &ordered_sums, 0);
  3022. if (ret) {
  3023. btrfs_release_path(dst_path);
  3024. kfree(ins_data);
  3025. return ret;
  3026. }
  3027. }
  3028. }
  3029. }
  3030. btrfs_mark_buffer_dirty(dst_path->nodes[0]);
  3031. btrfs_release_path(dst_path);
  3032. kfree(ins_data);
  3033. /*
  3034. * we have to do this after the loop above to avoid changing the
  3035. * log tree while trying to change the log tree.
  3036. */
  3037. ret = 0;
  3038. while (!list_empty(&ordered_sums)) {
  3039. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3040. struct btrfs_ordered_sum,
  3041. list);
  3042. if (!ret)
  3043. ret = btrfs_csum_file_blocks(trans, log, sums);
  3044. list_del(&sums->list);
  3045. kfree(sums);
  3046. }
  3047. if (!has_extents)
  3048. return ret;
  3049. if (need_find_last_extent && *last_extent == first_key.offset) {
  3050. /*
  3051. * We don't have any leafs between our current one and the one
  3052. * we processed before that can have file extent items for our
  3053. * inode (and have a generation number smaller than our current
  3054. * transaction id).
  3055. */
  3056. need_find_last_extent = false;
  3057. }
  3058. /*
  3059. * Because we use btrfs_search_forward we could skip leaves that were
  3060. * not modified and then assume *last_extent is valid when it really
  3061. * isn't. So back up to the previous leaf and read the end of the last
  3062. * extent before we go and fill in holes.
  3063. */
  3064. if (need_find_last_extent) {
  3065. u64 len;
  3066. ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
  3067. if (ret < 0)
  3068. return ret;
  3069. if (ret)
  3070. goto fill_holes;
  3071. if (src_path->slots[0])
  3072. src_path->slots[0]--;
  3073. src = src_path->nodes[0];
  3074. btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
  3075. if (key.objectid != btrfs_ino(inode) ||
  3076. key.type != BTRFS_EXTENT_DATA_KEY)
  3077. goto fill_holes;
  3078. extent = btrfs_item_ptr(src, src_path->slots[0],
  3079. struct btrfs_file_extent_item);
  3080. if (btrfs_file_extent_type(src, extent) ==
  3081. BTRFS_FILE_EXTENT_INLINE) {
  3082. len = btrfs_file_extent_inline_len(src,
  3083. src_path->slots[0],
  3084. extent);
  3085. *last_extent = ALIGN(key.offset + len,
  3086. log->sectorsize);
  3087. } else {
  3088. len = btrfs_file_extent_num_bytes(src, extent);
  3089. *last_extent = key.offset + len;
  3090. }
  3091. }
  3092. fill_holes:
  3093. /* So we did prev_leaf, now we need to move to the next leaf, but a few
  3094. * things could have happened
  3095. *
  3096. * 1) A merge could have happened, so we could currently be on a leaf
  3097. * that holds what we were copying in the first place.
  3098. * 2) A split could have happened, and now not all of the items we want
  3099. * are on the same leaf.
  3100. *
  3101. * So we need to adjust how we search for holes, we need to drop the
  3102. * path and re-search for the first extent key we found, and then walk
  3103. * forward until we hit the last one we copied.
  3104. */
  3105. if (need_find_last_extent) {
  3106. /* btrfs_prev_leaf could return 1 without releasing the path */
  3107. btrfs_release_path(src_path);
  3108. ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
  3109. src_path, 0, 0);
  3110. if (ret < 0)
  3111. return ret;
  3112. ASSERT(ret == 0);
  3113. src = src_path->nodes[0];
  3114. i = src_path->slots[0];
  3115. } else {
  3116. i = start_slot;
  3117. }
  3118. /*
  3119. * Ok so here we need to go through and fill in any holes we may have
  3120. * to make sure that holes are punched for those areas in case they had
  3121. * extents previously.
  3122. */
  3123. while (!done) {
  3124. u64 offset, len;
  3125. u64 extent_end;
  3126. if (i >= btrfs_header_nritems(src_path->nodes[0])) {
  3127. ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
  3128. if (ret < 0)
  3129. return ret;
  3130. ASSERT(ret == 0);
  3131. src = src_path->nodes[0];
  3132. i = 0;
  3133. }
  3134. btrfs_item_key_to_cpu(src, &key, i);
  3135. if (!btrfs_comp_cpu_keys(&key, &last_key))
  3136. done = true;
  3137. if (key.objectid != btrfs_ino(inode) ||
  3138. key.type != BTRFS_EXTENT_DATA_KEY) {
  3139. i++;
  3140. continue;
  3141. }
  3142. extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
  3143. if (btrfs_file_extent_type(src, extent) ==
  3144. BTRFS_FILE_EXTENT_INLINE) {
  3145. len = btrfs_file_extent_inline_len(src, i, extent);
  3146. extent_end = ALIGN(key.offset + len, log->sectorsize);
  3147. } else {
  3148. len = btrfs_file_extent_num_bytes(src, extent);
  3149. extent_end = key.offset + len;
  3150. }
  3151. i++;
  3152. if (*last_extent == key.offset) {
  3153. *last_extent = extent_end;
  3154. continue;
  3155. }
  3156. offset = *last_extent;
  3157. len = key.offset - *last_extent;
  3158. ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
  3159. offset, 0, 0, len, 0, len, 0,
  3160. 0, 0);
  3161. if (ret)
  3162. break;
  3163. *last_extent = extent_end;
  3164. }
  3165. /*
  3166. * Need to let the callers know we dropped the path so they should
  3167. * re-search.
  3168. */
  3169. if (!ret && need_find_last_extent)
  3170. ret = 1;
  3171. return ret;
  3172. }
  3173. static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
  3174. {
  3175. struct extent_map *em1, *em2;
  3176. em1 = list_entry(a, struct extent_map, list);
  3177. em2 = list_entry(b, struct extent_map, list);
  3178. if (em1->start < em2->start)
  3179. return -1;
  3180. else if (em1->start > em2->start)
  3181. return 1;
  3182. return 0;
  3183. }
  3184. static int wait_ordered_extents(struct btrfs_trans_handle *trans,
  3185. struct inode *inode,
  3186. struct btrfs_root *root,
  3187. const struct extent_map *em,
  3188. const struct list_head *logged_list,
  3189. bool *ordered_io_error)
  3190. {
  3191. struct btrfs_ordered_extent *ordered;
  3192. struct btrfs_root *log = root->log_root;
  3193. u64 mod_start = em->mod_start;
  3194. u64 mod_len = em->mod_len;
  3195. const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  3196. u64 csum_offset;
  3197. u64 csum_len;
  3198. LIST_HEAD(ordered_sums);
  3199. int ret = 0;
  3200. *ordered_io_error = false;
  3201. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  3202. em->block_start == EXTENT_MAP_HOLE)
  3203. return 0;
  3204. /*
  3205. * Wait far any ordered extent that covers our extent map. If it
  3206. * finishes without an error, first check and see if our csums are on
  3207. * our outstanding ordered extents.
  3208. */
  3209. list_for_each_entry(ordered, logged_list, log_list) {
  3210. struct btrfs_ordered_sum *sum;
  3211. if (!mod_len)
  3212. break;
  3213. if (ordered->file_offset + ordered->len <= mod_start ||
  3214. mod_start + mod_len <= ordered->file_offset)
  3215. continue;
  3216. if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
  3217. !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
  3218. !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
  3219. const u64 start = ordered->file_offset;
  3220. const u64 end = ordered->file_offset + ordered->len - 1;
  3221. WARN_ON(ordered->inode != inode);
  3222. filemap_fdatawrite_range(inode->i_mapping, start, end);
  3223. }
  3224. wait_event(ordered->wait,
  3225. (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
  3226. test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
  3227. if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
  3228. *ordered_io_error = true;
  3229. break;
  3230. }
  3231. /*
  3232. * We are going to copy all the csums on this ordered extent, so
  3233. * go ahead and adjust mod_start and mod_len in case this
  3234. * ordered extent has already been logged.
  3235. */
  3236. if (ordered->file_offset > mod_start) {
  3237. if (ordered->file_offset + ordered->len >=
  3238. mod_start + mod_len)
  3239. mod_len = ordered->file_offset - mod_start;
  3240. /*
  3241. * If we have this case
  3242. *
  3243. * |--------- logged extent ---------|
  3244. * |----- ordered extent ----|
  3245. *
  3246. * Just don't mess with mod_start and mod_len, we'll
  3247. * just end up logging more csums than we need and it
  3248. * will be ok.
  3249. */
  3250. } else {
  3251. if (ordered->file_offset + ordered->len <
  3252. mod_start + mod_len) {
  3253. mod_len = (mod_start + mod_len) -
  3254. (ordered->file_offset + ordered->len);
  3255. mod_start = ordered->file_offset +
  3256. ordered->len;
  3257. } else {
  3258. mod_len = 0;
  3259. }
  3260. }
  3261. if (skip_csum)
  3262. continue;
  3263. /*
  3264. * To keep us from looping for the above case of an ordered
  3265. * extent that falls inside of the logged extent.
  3266. */
  3267. if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
  3268. &ordered->flags))
  3269. continue;
  3270. if (ordered->csum_bytes_left) {
  3271. btrfs_start_ordered_extent(inode, ordered, 0);
  3272. wait_event(ordered->wait,
  3273. ordered->csum_bytes_left == 0);
  3274. }
  3275. list_for_each_entry(sum, &ordered->list, list) {
  3276. ret = btrfs_csum_file_blocks(trans, log, sum);
  3277. if (ret)
  3278. break;
  3279. }
  3280. }
  3281. if (*ordered_io_error || !mod_len || ret || skip_csum)
  3282. return ret;
  3283. if (em->compress_type) {
  3284. csum_offset = 0;
  3285. csum_len = max(em->block_len, em->orig_block_len);
  3286. } else {
  3287. csum_offset = mod_start - em->start;
  3288. csum_len = mod_len;
  3289. }
  3290. /* block start is already adjusted for the file extent offset. */
  3291. ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
  3292. em->block_start + csum_offset,
  3293. em->block_start + csum_offset +
  3294. csum_len - 1, &ordered_sums, 0);
  3295. if (ret)
  3296. return ret;
  3297. while (!list_empty(&ordered_sums)) {
  3298. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3299. struct btrfs_ordered_sum,
  3300. list);
  3301. if (!ret)
  3302. ret = btrfs_csum_file_blocks(trans, log, sums);
  3303. list_del(&sums->list);
  3304. kfree(sums);
  3305. }
  3306. return ret;
  3307. }
  3308. static int log_one_extent(struct btrfs_trans_handle *trans,
  3309. struct inode *inode, struct btrfs_root *root,
  3310. const struct extent_map *em,
  3311. struct btrfs_path *path,
  3312. const struct list_head *logged_list,
  3313. struct btrfs_log_ctx *ctx)
  3314. {
  3315. struct btrfs_root *log = root->log_root;
  3316. struct btrfs_file_extent_item *fi;
  3317. struct extent_buffer *leaf;
  3318. struct btrfs_map_token token;
  3319. struct btrfs_key key;
  3320. u64 extent_offset = em->start - em->orig_start;
  3321. u64 block_len;
  3322. int ret;
  3323. int extent_inserted = 0;
  3324. bool ordered_io_err = false;
  3325. ret = wait_ordered_extents(trans, inode, root, em, logged_list,
  3326. &ordered_io_err);
  3327. if (ret)
  3328. return ret;
  3329. if (ordered_io_err) {
  3330. ctx->io_err = -EIO;
  3331. return 0;
  3332. }
  3333. btrfs_init_map_token(&token);
  3334. ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
  3335. em->start + em->len, NULL, 0, 1,
  3336. sizeof(*fi), &extent_inserted);
  3337. if (ret)
  3338. return ret;
  3339. if (!extent_inserted) {
  3340. key.objectid = btrfs_ino(inode);
  3341. key.type = BTRFS_EXTENT_DATA_KEY;
  3342. key.offset = em->start;
  3343. ret = btrfs_insert_empty_item(trans, log, path, &key,
  3344. sizeof(*fi));
  3345. if (ret)
  3346. return ret;
  3347. }
  3348. leaf = path->nodes[0];
  3349. fi = btrfs_item_ptr(leaf, path->slots[0],
  3350. struct btrfs_file_extent_item);
  3351. btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
  3352. &token);
  3353. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  3354. btrfs_set_token_file_extent_type(leaf, fi,
  3355. BTRFS_FILE_EXTENT_PREALLOC,
  3356. &token);
  3357. else
  3358. btrfs_set_token_file_extent_type(leaf, fi,
  3359. BTRFS_FILE_EXTENT_REG,
  3360. &token);
  3361. block_len = max(em->block_len, em->orig_block_len);
  3362. if (em->compress_type != BTRFS_COMPRESS_NONE) {
  3363. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3364. em->block_start,
  3365. &token);
  3366. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3367. &token);
  3368. } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
  3369. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3370. em->block_start -
  3371. extent_offset, &token);
  3372. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3373. &token);
  3374. } else {
  3375. btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
  3376. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
  3377. &token);
  3378. }
  3379. btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
  3380. btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
  3381. btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
  3382. btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
  3383. &token);
  3384. btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
  3385. btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
  3386. btrfs_mark_buffer_dirty(leaf);
  3387. btrfs_release_path(path);
  3388. return ret;
  3389. }
  3390. static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
  3391. struct btrfs_root *root,
  3392. struct inode *inode,
  3393. struct btrfs_path *path,
  3394. struct list_head *logged_list,
  3395. struct btrfs_log_ctx *ctx)
  3396. {
  3397. struct extent_map *em, *n;
  3398. struct list_head extents;
  3399. struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
  3400. u64 test_gen;
  3401. int ret = 0;
  3402. int num = 0;
  3403. INIT_LIST_HEAD(&extents);
  3404. write_lock(&tree->lock);
  3405. test_gen = root->fs_info->last_trans_committed;
  3406. list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
  3407. list_del_init(&em->list);
  3408. /*
  3409. * Just an arbitrary number, this can be really CPU intensive
  3410. * once we start getting a lot of extents, and really once we
  3411. * have a bunch of extents we just want to commit since it will
  3412. * be faster.
  3413. */
  3414. if (++num > 32768) {
  3415. list_del_init(&tree->modified_extents);
  3416. ret = -EFBIG;
  3417. goto process;
  3418. }
  3419. if (em->generation <= test_gen)
  3420. continue;
  3421. /* Need a ref to keep it from getting evicted from cache */
  3422. atomic_inc(&em->refs);
  3423. set_bit(EXTENT_FLAG_LOGGING, &em->flags);
  3424. list_add_tail(&em->list, &extents);
  3425. num++;
  3426. }
  3427. list_sort(NULL, &extents, extent_cmp);
  3428. process:
  3429. while (!list_empty(&extents)) {
  3430. em = list_entry(extents.next, struct extent_map, list);
  3431. list_del_init(&em->list);
  3432. /*
  3433. * If we had an error we just need to delete everybody from our
  3434. * private list.
  3435. */
  3436. if (ret) {
  3437. clear_em_logging(tree, em);
  3438. free_extent_map(em);
  3439. continue;
  3440. }
  3441. write_unlock(&tree->lock);
  3442. ret = log_one_extent(trans, inode, root, em, path, logged_list,
  3443. ctx);
  3444. write_lock(&tree->lock);
  3445. clear_em_logging(tree, em);
  3446. free_extent_map(em);
  3447. }
  3448. WARN_ON(!list_empty(&extents));
  3449. write_unlock(&tree->lock);
  3450. btrfs_release_path(path);
  3451. return ret;
  3452. }
  3453. static int logged_inode_size(struct btrfs_root *log, struct inode *inode,
  3454. struct btrfs_path *path, u64 *size_ret)
  3455. {
  3456. struct btrfs_key key;
  3457. int ret;
  3458. key.objectid = btrfs_ino(inode);
  3459. key.type = BTRFS_INODE_ITEM_KEY;
  3460. key.offset = 0;
  3461. ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
  3462. if (ret < 0) {
  3463. return ret;
  3464. } else if (ret > 0) {
  3465. *size_ret = i_size_read(inode);
  3466. } else {
  3467. struct btrfs_inode_item *item;
  3468. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3469. struct btrfs_inode_item);
  3470. *size_ret = btrfs_inode_size(path->nodes[0], item);
  3471. }
  3472. btrfs_release_path(path);
  3473. return 0;
  3474. }
  3475. /* log a single inode in the tree log.
  3476. * At least one parent directory for this inode must exist in the tree
  3477. * or be logged already.
  3478. *
  3479. * Any items from this inode changed by the current transaction are copied
  3480. * to the log tree. An extra reference is taken on any extents in this
  3481. * file, allowing us to avoid a whole pile of corner cases around logging
  3482. * blocks that have been removed from the tree.
  3483. *
  3484. * See LOG_INODE_ALL and related defines for a description of what inode_only
  3485. * does.
  3486. *
  3487. * This handles both files and directories.
  3488. */
  3489. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  3490. struct btrfs_root *root, struct inode *inode,
  3491. int inode_only,
  3492. const loff_t start,
  3493. const loff_t end,
  3494. struct btrfs_log_ctx *ctx)
  3495. {
  3496. struct btrfs_path *path;
  3497. struct btrfs_path *dst_path;
  3498. struct btrfs_key min_key;
  3499. struct btrfs_key max_key;
  3500. struct btrfs_root *log = root->log_root;
  3501. struct extent_buffer *src = NULL;
  3502. LIST_HEAD(logged_list);
  3503. u64 last_extent = 0;
  3504. int err = 0;
  3505. int ret;
  3506. int nritems;
  3507. int ins_start_slot = 0;
  3508. int ins_nr;
  3509. bool fast_search = false;
  3510. u64 ino = btrfs_ino(inode);
  3511. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  3512. u64 logged_isize = 0;
  3513. path = btrfs_alloc_path();
  3514. if (!path)
  3515. return -ENOMEM;
  3516. dst_path = btrfs_alloc_path();
  3517. if (!dst_path) {
  3518. btrfs_free_path(path);
  3519. return -ENOMEM;
  3520. }
  3521. min_key.objectid = ino;
  3522. min_key.type = BTRFS_INODE_ITEM_KEY;
  3523. min_key.offset = 0;
  3524. max_key.objectid = ino;
  3525. /* today the code can only do partial logging of directories */
  3526. if (S_ISDIR(inode->i_mode) ||
  3527. (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3528. &BTRFS_I(inode)->runtime_flags) &&
  3529. inode_only == LOG_INODE_EXISTS))
  3530. max_key.type = BTRFS_XATTR_ITEM_KEY;
  3531. else
  3532. max_key.type = (u8)-1;
  3533. max_key.offset = (u64)-1;
  3534. /* Only run delayed items if we are a dir or a new file */
  3535. if (S_ISDIR(inode->i_mode) ||
  3536. BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
  3537. ret = btrfs_commit_inode_delayed_items(trans, inode);
  3538. if (ret) {
  3539. btrfs_free_path(path);
  3540. btrfs_free_path(dst_path);
  3541. return ret;
  3542. }
  3543. }
  3544. mutex_lock(&BTRFS_I(inode)->log_mutex);
  3545. btrfs_get_logged_extents(inode, &logged_list);
  3546. /*
  3547. * a brute force approach to making sure we get the most uptodate
  3548. * copies of everything.
  3549. */
  3550. if (S_ISDIR(inode->i_mode)) {
  3551. int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
  3552. if (inode_only == LOG_INODE_EXISTS)
  3553. max_key_type = BTRFS_XATTR_ITEM_KEY;
  3554. ret = drop_objectid_items(trans, log, path, ino, max_key_type);
  3555. } else {
  3556. if (inode_only == LOG_INODE_EXISTS) {
  3557. /*
  3558. * Make sure the new inode item we write to the log has
  3559. * the same isize as the current one (if it exists).
  3560. * This is necessary to prevent data loss after log
  3561. * replay, and also to prevent doing a wrong expanding
  3562. * truncate - for e.g. create file, write 4K into offset
  3563. * 0, fsync, write 4K into offset 4096, add hard link,
  3564. * fsync some other file (to sync log), power fail - if
  3565. * we use the inode's current i_size, after log replay
  3566. * we get a 8Kb file, with the last 4Kb extent as a hole
  3567. * (zeroes), as if an expanding truncate happened,
  3568. * instead of getting a file of 4Kb only.
  3569. */
  3570. err = logged_inode_size(log, inode, path,
  3571. &logged_isize);
  3572. if (err)
  3573. goto out_unlock;
  3574. }
  3575. if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3576. &BTRFS_I(inode)->runtime_flags)) {
  3577. clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  3578. &BTRFS_I(inode)->runtime_flags);
  3579. ret = btrfs_truncate_inode_items(trans, log,
  3580. inode, 0, 0);
  3581. } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  3582. &BTRFS_I(inode)->runtime_flags) ||
  3583. inode_only == LOG_INODE_EXISTS) {
  3584. if (inode_only == LOG_INODE_ALL)
  3585. fast_search = true;
  3586. max_key.type = BTRFS_XATTR_ITEM_KEY;
  3587. ret = drop_objectid_items(trans, log, path, ino,
  3588. max_key.type);
  3589. } else {
  3590. if (inode_only == LOG_INODE_ALL)
  3591. fast_search = true;
  3592. ret = log_inode_item(trans, log, dst_path, inode);
  3593. if (ret) {
  3594. err = ret;
  3595. goto out_unlock;
  3596. }
  3597. goto log_extents;
  3598. }
  3599. }
  3600. if (ret) {
  3601. err = ret;
  3602. goto out_unlock;
  3603. }
  3604. while (1) {
  3605. ins_nr = 0;
  3606. ret = btrfs_search_forward(root, &min_key,
  3607. path, trans->transid);
  3608. if (ret != 0)
  3609. break;
  3610. again:
  3611. /* note, ins_nr might be > 0 here, cleanup outside the loop */
  3612. if (min_key.objectid != ino)
  3613. break;
  3614. if (min_key.type > max_key.type)
  3615. break;
  3616. src = path->nodes[0];
  3617. if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
  3618. ins_nr++;
  3619. goto next_slot;
  3620. } else if (!ins_nr) {
  3621. ins_start_slot = path->slots[0];
  3622. ins_nr = 1;
  3623. goto next_slot;
  3624. }
  3625. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  3626. ins_start_slot, ins_nr, inode_only,
  3627. logged_isize);
  3628. if (ret < 0) {
  3629. err = ret;
  3630. goto out_unlock;
  3631. }
  3632. if (ret) {
  3633. ins_nr = 0;
  3634. btrfs_release_path(path);
  3635. continue;
  3636. }
  3637. ins_nr = 1;
  3638. ins_start_slot = path->slots[0];
  3639. next_slot:
  3640. nritems = btrfs_header_nritems(path->nodes[0]);
  3641. path->slots[0]++;
  3642. if (path->slots[0] < nritems) {
  3643. btrfs_item_key_to_cpu(path->nodes[0], &min_key,
  3644. path->slots[0]);
  3645. goto again;
  3646. }
  3647. if (ins_nr) {
  3648. ret = copy_items(trans, inode, dst_path, path,
  3649. &last_extent, ins_start_slot,
  3650. ins_nr, inode_only, logged_isize);
  3651. if (ret < 0) {
  3652. err = ret;
  3653. goto out_unlock;
  3654. }
  3655. ret = 0;
  3656. ins_nr = 0;
  3657. }
  3658. btrfs_release_path(path);
  3659. if (min_key.offset < (u64)-1) {
  3660. min_key.offset++;
  3661. } else if (min_key.type < max_key.type) {
  3662. min_key.type++;
  3663. min_key.offset = 0;
  3664. } else {
  3665. break;
  3666. }
  3667. }
  3668. if (ins_nr) {
  3669. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  3670. ins_start_slot, ins_nr, inode_only,
  3671. logged_isize);
  3672. if (ret < 0) {
  3673. err = ret;
  3674. goto out_unlock;
  3675. }
  3676. ret = 0;
  3677. ins_nr = 0;
  3678. }
  3679. log_extents:
  3680. btrfs_release_path(path);
  3681. btrfs_release_path(dst_path);
  3682. if (fast_search) {
  3683. ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
  3684. &logged_list, ctx);
  3685. if (ret) {
  3686. err = ret;
  3687. goto out_unlock;
  3688. }
  3689. } else if (inode_only == LOG_INODE_ALL) {
  3690. struct extent_map *em, *n;
  3691. write_lock(&em_tree->lock);
  3692. /*
  3693. * We can't just remove every em if we're called for a ranged
  3694. * fsync - that is, one that doesn't cover the whole possible
  3695. * file range (0 to LLONG_MAX). This is because we can have
  3696. * em's that fall outside the range we're logging and therefore
  3697. * their ordered operations haven't completed yet
  3698. * (btrfs_finish_ordered_io() not invoked yet). This means we
  3699. * didn't get their respective file extent item in the fs/subvol
  3700. * tree yet, and need to let the next fast fsync (one which
  3701. * consults the list of modified extent maps) find the em so
  3702. * that it logs a matching file extent item and waits for the
  3703. * respective ordered operation to complete (if it's still
  3704. * running).
  3705. *
  3706. * Removing every em outside the range we're logging would make
  3707. * the next fast fsync not log their matching file extent items,
  3708. * therefore making us lose data after a log replay.
  3709. */
  3710. list_for_each_entry_safe(em, n, &em_tree->modified_extents,
  3711. list) {
  3712. const u64 mod_end = em->mod_start + em->mod_len - 1;
  3713. if (em->mod_start >= start && mod_end <= end)
  3714. list_del_init(&em->list);
  3715. }
  3716. write_unlock(&em_tree->lock);
  3717. }
  3718. if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
  3719. ret = log_directory_changes(trans, root, inode, path, dst_path);
  3720. if (ret) {
  3721. err = ret;
  3722. goto out_unlock;
  3723. }
  3724. }
  3725. BTRFS_I(inode)->logged_trans = trans->transid;
  3726. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
  3727. out_unlock:
  3728. if (unlikely(err))
  3729. btrfs_put_logged_extents(&logged_list);
  3730. else
  3731. btrfs_submit_logged_extents(&logged_list, log);
  3732. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  3733. btrfs_free_path(path);
  3734. btrfs_free_path(dst_path);
  3735. return err;
  3736. }
  3737. /*
  3738. * follow the dentry parent pointers up the chain and see if any
  3739. * of the directories in it require a full commit before they can
  3740. * be logged. Returns zero if nothing special needs to be done or 1 if
  3741. * a full commit is required.
  3742. */
  3743. static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
  3744. struct inode *inode,
  3745. struct dentry *parent,
  3746. struct super_block *sb,
  3747. u64 last_committed)
  3748. {
  3749. int ret = 0;
  3750. struct btrfs_root *root;
  3751. struct dentry *old_parent = NULL;
  3752. struct inode *orig_inode = inode;
  3753. /*
  3754. * for regular files, if its inode is already on disk, we don't
  3755. * have to worry about the parents at all. This is because
  3756. * we can use the last_unlink_trans field to record renames
  3757. * and other fun in this file.
  3758. */
  3759. if (S_ISREG(inode->i_mode) &&
  3760. BTRFS_I(inode)->generation <= last_committed &&
  3761. BTRFS_I(inode)->last_unlink_trans <= last_committed)
  3762. goto out;
  3763. if (!S_ISDIR(inode->i_mode)) {
  3764. if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
  3765. goto out;
  3766. inode = parent->d_inode;
  3767. }
  3768. while (1) {
  3769. /*
  3770. * If we are logging a directory then we start with our inode,
  3771. * not our parents inode, so we need to skipp setting the
  3772. * logged_trans so that further down in the log code we don't
  3773. * think this inode has already been logged.
  3774. */
  3775. if (inode != orig_inode)
  3776. BTRFS_I(inode)->logged_trans = trans->transid;
  3777. smp_mb();
  3778. if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
  3779. root = BTRFS_I(inode)->root;
  3780. /*
  3781. * make sure any commits to the log are forced
  3782. * to be full commits
  3783. */
  3784. btrfs_set_log_full_commit(root->fs_info, trans);
  3785. ret = 1;
  3786. break;
  3787. }
  3788. if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
  3789. break;
  3790. if (IS_ROOT(parent))
  3791. break;
  3792. parent = dget_parent(parent);
  3793. dput(old_parent);
  3794. old_parent = parent;
  3795. inode = parent->d_inode;
  3796. }
  3797. dput(old_parent);
  3798. out:
  3799. return ret;
  3800. }
  3801. /*
  3802. * helper function around btrfs_log_inode to make sure newly created
  3803. * parent directories also end up in the log. A minimal inode and backref
  3804. * only logging is done of any parent directories that are older than
  3805. * the last committed transaction
  3806. */
  3807. static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
  3808. struct btrfs_root *root, struct inode *inode,
  3809. struct dentry *parent,
  3810. const loff_t start,
  3811. const loff_t end,
  3812. int exists_only,
  3813. struct btrfs_log_ctx *ctx)
  3814. {
  3815. int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
  3816. struct super_block *sb;
  3817. struct dentry *old_parent = NULL;
  3818. int ret = 0;
  3819. u64 last_committed = root->fs_info->last_trans_committed;
  3820. sb = inode->i_sb;
  3821. if (btrfs_test_opt(root, NOTREELOG)) {
  3822. ret = 1;
  3823. goto end_no_trans;
  3824. }
  3825. /*
  3826. * The prev transaction commit doesn't complete, we need do
  3827. * full commit by ourselves.
  3828. */
  3829. if (root->fs_info->last_trans_log_full_commit >
  3830. root->fs_info->last_trans_committed) {
  3831. ret = 1;
  3832. goto end_no_trans;
  3833. }
  3834. if (root != BTRFS_I(inode)->root ||
  3835. btrfs_root_refs(&root->root_item) == 0) {
  3836. ret = 1;
  3837. goto end_no_trans;
  3838. }
  3839. ret = check_parent_dirs_for_sync(trans, inode, parent,
  3840. sb, last_committed);
  3841. if (ret)
  3842. goto end_no_trans;
  3843. if (btrfs_inode_in_log(inode, trans->transid)) {
  3844. ret = BTRFS_NO_LOG_SYNC;
  3845. goto end_no_trans;
  3846. }
  3847. ret = start_log_trans(trans, root, ctx);
  3848. if (ret)
  3849. goto end_no_trans;
  3850. ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
  3851. if (ret)
  3852. goto end_trans;
  3853. /*
  3854. * for regular files, if its inode is already on disk, we don't
  3855. * have to worry about the parents at all. This is because
  3856. * we can use the last_unlink_trans field to record renames
  3857. * and other fun in this file.
  3858. */
  3859. if (S_ISREG(inode->i_mode) &&
  3860. BTRFS_I(inode)->generation <= last_committed &&
  3861. BTRFS_I(inode)->last_unlink_trans <= last_committed) {
  3862. ret = 0;
  3863. goto end_trans;
  3864. }
  3865. inode_only = LOG_INODE_EXISTS;
  3866. while (1) {
  3867. if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
  3868. break;
  3869. inode = parent->d_inode;
  3870. if (root != BTRFS_I(inode)->root)
  3871. break;
  3872. if (BTRFS_I(inode)->generation >
  3873. root->fs_info->last_trans_committed) {
  3874. ret = btrfs_log_inode(trans, root, inode, inode_only,
  3875. 0, LLONG_MAX, ctx);
  3876. if (ret)
  3877. goto end_trans;
  3878. }
  3879. if (IS_ROOT(parent))
  3880. break;
  3881. parent = dget_parent(parent);
  3882. dput(old_parent);
  3883. old_parent = parent;
  3884. }
  3885. ret = 0;
  3886. end_trans:
  3887. dput(old_parent);
  3888. if (ret < 0) {
  3889. btrfs_set_log_full_commit(root->fs_info, trans);
  3890. ret = 1;
  3891. }
  3892. if (ret)
  3893. btrfs_remove_log_ctx(root, ctx);
  3894. btrfs_end_log_trans(root);
  3895. end_no_trans:
  3896. return ret;
  3897. }
  3898. /*
  3899. * it is not safe to log dentry if the chunk root has added new
  3900. * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
  3901. * If this returns 1, you must commit the transaction to safely get your
  3902. * data on disk.
  3903. */
  3904. int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
  3905. struct btrfs_root *root, struct dentry *dentry,
  3906. const loff_t start,
  3907. const loff_t end,
  3908. struct btrfs_log_ctx *ctx)
  3909. {
  3910. struct dentry *parent = dget_parent(dentry);
  3911. int ret;
  3912. ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent,
  3913. start, end, 0, ctx);
  3914. dput(parent);
  3915. return ret;
  3916. }
  3917. /*
  3918. * should be called during mount to recover any replay any log trees
  3919. * from the FS
  3920. */
  3921. int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
  3922. {
  3923. int ret;
  3924. struct btrfs_path *path;
  3925. struct btrfs_trans_handle *trans;
  3926. struct btrfs_key key;
  3927. struct btrfs_key found_key;
  3928. struct btrfs_key tmp_key;
  3929. struct btrfs_root *log;
  3930. struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
  3931. struct walk_control wc = {
  3932. .process_func = process_one_buffer,
  3933. .stage = 0,
  3934. };
  3935. path = btrfs_alloc_path();
  3936. if (!path)
  3937. return -ENOMEM;
  3938. fs_info->log_root_recovering = 1;
  3939. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  3940. if (IS_ERR(trans)) {
  3941. ret = PTR_ERR(trans);
  3942. goto error;
  3943. }
  3944. wc.trans = trans;
  3945. wc.pin = 1;
  3946. ret = walk_log_tree(trans, log_root_tree, &wc);
  3947. if (ret) {
  3948. btrfs_error(fs_info, ret, "Failed to pin buffers while "
  3949. "recovering log root tree.");
  3950. goto error;
  3951. }
  3952. again:
  3953. key.objectid = BTRFS_TREE_LOG_OBJECTID;
  3954. key.offset = (u64)-1;
  3955. key.type = BTRFS_ROOT_ITEM_KEY;
  3956. while (1) {
  3957. ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
  3958. if (ret < 0) {
  3959. btrfs_error(fs_info, ret,
  3960. "Couldn't find tree log root.");
  3961. goto error;
  3962. }
  3963. if (ret > 0) {
  3964. if (path->slots[0] == 0)
  3965. break;
  3966. path->slots[0]--;
  3967. }
  3968. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  3969. path->slots[0]);
  3970. btrfs_release_path(path);
  3971. if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  3972. break;
  3973. log = btrfs_read_fs_root(log_root_tree, &found_key);
  3974. if (IS_ERR(log)) {
  3975. ret = PTR_ERR(log);
  3976. btrfs_error(fs_info, ret,
  3977. "Couldn't read tree log root.");
  3978. goto error;
  3979. }
  3980. tmp_key.objectid = found_key.offset;
  3981. tmp_key.type = BTRFS_ROOT_ITEM_KEY;
  3982. tmp_key.offset = (u64)-1;
  3983. wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
  3984. if (IS_ERR(wc.replay_dest)) {
  3985. ret = PTR_ERR(wc.replay_dest);
  3986. free_extent_buffer(log->node);
  3987. free_extent_buffer(log->commit_root);
  3988. kfree(log);
  3989. btrfs_error(fs_info, ret, "Couldn't read target root "
  3990. "for tree log recovery.");
  3991. goto error;
  3992. }
  3993. wc.replay_dest->log_root = log;
  3994. btrfs_record_root_in_trans(trans, wc.replay_dest);
  3995. ret = walk_log_tree(trans, log, &wc);
  3996. if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
  3997. ret = fixup_inode_link_counts(trans, wc.replay_dest,
  3998. path);
  3999. }
  4000. key.offset = found_key.offset - 1;
  4001. wc.replay_dest->log_root = NULL;
  4002. free_extent_buffer(log->node);
  4003. free_extent_buffer(log->commit_root);
  4004. kfree(log);
  4005. if (ret)
  4006. goto error;
  4007. if (found_key.offset == 0)
  4008. break;
  4009. }
  4010. btrfs_release_path(path);
  4011. /* step one is to pin it all, step two is to replay just inodes */
  4012. if (wc.pin) {
  4013. wc.pin = 0;
  4014. wc.process_func = replay_one_buffer;
  4015. wc.stage = LOG_WALK_REPLAY_INODES;
  4016. goto again;
  4017. }
  4018. /* step three is to replay everything */
  4019. if (wc.stage < LOG_WALK_REPLAY_ALL) {
  4020. wc.stage++;
  4021. goto again;
  4022. }
  4023. btrfs_free_path(path);
  4024. /* step 4: commit the transaction, which also unpins the blocks */
  4025. ret = btrfs_commit_transaction(trans, fs_info->tree_root);
  4026. if (ret)
  4027. return ret;
  4028. free_extent_buffer(log_root_tree->node);
  4029. log_root_tree->log_root = NULL;
  4030. fs_info->log_root_recovering = 0;
  4031. kfree(log_root_tree);
  4032. return 0;
  4033. error:
  4034. if (wc.trans)
  4035. btrfs_end_transaction(wc.trans, fs_info->tree_root);
  4036. btrfs_free_path(path);
  4037. return ret;
  4038. }
  4039. /*
  4040. * there are some corner cases where we want to force a full
  4041. * commit instead of allowing a directory to be logged.
  4042. *
  4043. * They revolve around files there were unlinked from the directory, and
  4044. * this function updates the parent directory so that a full commit is
  4045. * properly done if it is fsync'd later after the unlinks are done.
  4046. */
  4047. void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
  4048. struct inode *dir, struct inode *inode,
  4049. int for_rename)
  4050. {
  4051. /*
  4052. * when we're logging a file, if it hasn't been renamed
  4053. * or unlinked, and its inode is fully committed on disk,
  4054. * we don't have to worry about walking up the directory chain
  4055. * to log its parents.
  4056. *
  4057. * So, we use the last_unlink_trans field to put this transid
  4058. * into the file. When the file is logged we check it and
  4059. * don't log the parents if the file is fully on disk.
  4060. */
  4061. if (S_ISREG(inode->i_mode))
  4062. BTRFS_I(inode)->last_unlink_trans = trans->transid;
  4063. /*
  4064. * if this directory was already logged any new
  4065. * names for this file/dir will get recorded
  4066. */
  4067. smp_mb();
  4068. if (BTRFS_I(dir)->logged_trans == trans->transid)
  4069. return;
  4070. /*
  4071. * if the inode we're about to unlink was logged,
  4072. * the log will be properly updated for any new names
  4073. */
  4074. if (BTRFS_I(inode)->logged_trans == trans->transid)
  4075. return;
  4076. /*
  4077. * when renaming files across directories, if the directory
  4078. * there we're unlinking from gets fsync'd later on, there's
  4079. * no way to find the destination directory later and fsync it
  4080. * properly. So, we have to be conservative and force commits
  4081. * so the new name gets discovered.
  4082. */
  4083. if (for_rename)
  4084. goto record;
  4085. /* we can safely do the unlink without any special recording */
  4086. return;
  4087. record:
  4088. BTRFS_I(dir)->last_unlink_trans = trans->transid;
  4089. }
  4090. /*
  4091. * Call this after adding a new name for a file and it will properly
  4092. * update the log to reflect the new name.
  4093. *
  4094. * It will return zero if all goes well, and it will return 1 if a
  4095. * full transaction commit is required.
  4096. */
  4097. int btrfs_log_new_name(struct btrfs_trans_handle *trans,
  4098. struct inode *inode, struct inode *old_dir,
  4099. struct dentry *parent)
  4100. {
  4101. struct btrfs_root * root = BTRFS_I(inode)->root;
  4102. /*
  4103. * this will force the logging code to walk the dentry chain
  4104. * up for the file
  4105. */
  4106. if (S_ISREG(inode->i_mode))
  4107. BTRFS_I(inode)->last_unlink_trans = trans->transid;
  4108. /*
  4109. * if this inode hasn't been logged and directory we're renaming it
  4110. * from hasn't been logged, we don't need to log it
  4111. */
  4112. if (BTRFS_I(inode)->logged_trans <=
  4113. root->fs_info->last_trans_committed &&
  4114. (!old_dir || BTRFS_I(old_dir)->logged_trans <=
  4115. root->fs_info->last_trans_committed))
  4116. return 0;
  4117. return btrfs_log_inode_parent(trans, root, inode, parent, 0,
  4118. LLONG_MAX, 1, NULL);
  4119. }