tree-log.c 118 KB

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