tree-log.c 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641
  1. /*
  2. * Copyright (C) 2008 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/list_sort.h>
  22. #include "tree-log.h"
  23. #include "disk-io.h"
  24. #include "locking.h"
  25. #include "print-tree.h"
  26. #include "backref.h"
  27. #include "hash.h"
  28. /* magic values for the inode_only field in btrfs_log_inode:
  29. *
  30. * LOG_INODE_ALL means to log everything
  31. * LOG_INODE_EXISTS means to log just enough to recreate the inode
  32. * during log replay
  33. */
  34. #define LOG_INODE_ALL 0
  35. #define LOG_INODE_EXISTS 1
  36. /*
  37. * directory trouble cases
  38. *
  39. * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
  40. * log, we must force a full commit before doing an fsync of the directory
  41. * where the unlink was done.
  42. * ---> record transid of last unlink/rename per directory
  43. *
  44. * mkdir foo/some_dir
  45. * normal commit
  46. * rename foo/some_dir foo2/some_dir
  47. * mkdir foo/some_dir
  48. * fsync foo/some_dir/some_file
  49. *
  50. * The fsync above will unlink the original some_dir without recording
  51. * it in its new location (foo2). After a crash, some_dir will be gone
  52. * unless the fsync of some_file forces a full commit
  53. *
  54. * 2) we must log any new names for any file or dir that is in the fsync
  55. * log. ---> check inode while renaming/linking.
  56. *
  57. * 2a) we must log any new names for any file or dir during rename
  58. * when the directory they are being removed from was logged.
  59. * ---> check inode and old parent dir during rename
  60. *
  61. * 2a is actually the more important variant. With the extra logging
  62. * a crash might unlink the old name without recreating the new one
  63. *
  64. * 3) after a crash, we must go through any directories with a link count
  65. * of zero and redo the rm -rf
  66. *
  67. * mkdir f1/foo
  68. * normal commit
  69. * rm -rf f1/foo
  70. * fsync(f1)
  71. *
  72. * The directory f1 was fully removed from the FS, but fsync was never
  73. * called on f1, only its parent dir. After a crash the rm -rf must
  74. * be replayed. This must be able to recurse down the entire
  75. * directory tree. The inode link count fixup code takes care of the
  76. * ugly details.
  77. */
  78. /*
  79. * stages for the tree walking. The first
  80. * stage (0) is to only pin down the blocks we find
  81. * the second stage (1) is to make sure that all the inodes
  82. * we find in the log are created in the subvolume.
  83. *
  84. * The last stage is to deal with directories and links and extents
  85. * and all the other fun semantics
  86. */
  87. #define LOG_WALK_PIN_ONLY 0
  88. #define LOG_WALK_REPLAY_INODES 1
  89. #define LOG_WALK_REPLAY_DIR_INDEX 2
  90. #define LOG_WALK_REPLAY_ALL 3
  91. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  92. struct btrfs_root *root, struct inode *inode,
  93. int inode_only,
  94. const loff_t start,
  95. const loff_t end,
  96. struct btrfs_log_ctx *ctx);
  97. static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  98. struct btrfs_root *root,
  99. struct btrfs_path *path, u64 objectid);
  100. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  101. struct btrfs_root *root,
  102. struct btrfs_root *log,
  103. struct btrfs_path *path,
  104. u64 dirid, int del_all);
  105. /*
  106. * tree logging is a special write ahead log used to make sure that
  107. * fsyncs and O_SYNCs can happen without doing full tree commits.
  108. *
  109. * Full tree commits are expensive because they require commonly
  110. * modified blocks to be recowed, creating many dirty pages in the
  111. * extent tree an 4x-6x higher write load than ext3.
  112. *
  113. * Instead of doing a tree commit on every fsync, we use the
  114. * key ranges and transaction ids to find items for a given file or directory
  115. * that have changed in this transaction. Those items are copied into
  116. * a special tree (one per subvolume root), that tree is written to disk
  117. * and then the fsync is considered complete.
  118. *
  119. * After a crash, items are copied out of the log-tree back into the
  120. * subvolume tree. Any file data extents found are recorded in the extent
  121. * allocation tree, and the log-tree freed.
  122. *
  123. * The log tree is read three times, once to pin down all the extents it is
  124. * using in ram and once, once to create all the inodes logged in the tree
  125. * and once to do all the other items.
  126. */
  127. /*
  128. * start a sub transaction and setup the log tree
  129. * this increments the log tree writer count to make the people
  130. * syncing the tree wait for us to finish
  131. */
  132. static int start_log_trans(struct btrfs_trans_handle *trans,
  133. struct btrfs_root *root,
  134. struct btrfs_log_ctx *ctx)
  135. {
  136. int index;
  137. int ret;
  138. mutex_lock(&root->log_mutex);
  139. if (root->log_root) {
  140. if (btrfs_need_log_full_commit(root->fs_info, trans)) {
  141. ret = -EAGAIN;
  142. goto out;
  143. }
  144. if (!root->log_start_pid) {
  145. root->log_start_pid = current->pid;
  146. clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  147. } else if (root->log_start_pid != current->pid) {
  148. set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  149. }
  150. atomic_inc(&root->log_batch);
  151. atomic_inc(&root->log_writers);
  152. if (ctx) {
  153. index = root->log_transid % 2;
  154. list_add_tail(&ctx->list, &root->log_ctxs[index]);
  155. ctx->log_transid = root->log_transid;
  156. }
  157. mutex_unlock(&root->log_mutex);
  158. return 0;
  159. }
  160. ret = 0;
  161. mutex_lock(&root->fs_info->tree_log_mutex);
  162. if (!root->fs_info->log_root_tree)
  163. ret = btrfs_init_log_root_tree(trans, root->fs_info);
  164. mutex_unlock(&root->fs_info->tree_log_mutex);
  165. if (ret)
  166. goto out;
  167. if (!root->log_root) {
  168. ret = btrfs_add_log_tree(trans, root);
  169. if (ret)
  170. goto out;
  171. }
  172. clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  173. root->log_start_pid = current->pid;
  174. atomic_inc(&root->log_batch);
  175. atomic_inc(&root->log_writers);
  176. if (ctx) {
  177. index = root->log_transid % 2;
  178. list_add_tail(&ctx->list, &root->log_ctxs[index]);
  179. ctx->log_transid = root->log_transid;
  180. }
  181. out:
  182. mutex_unlock(&root->log_mutex);
  183. return ret;
  184. }
  185. /*
  186. * returns 0 if there was a log transaction running and we were able
  187. * to join, or returns -ENOENT if there were not transactions
  188. * in progress
  189. */
  190. static int join_running_log_trans(struct btrfs_root *root)
  191. {
  192. int ret = -ENOENT;
  193. smp_mb();
  194. if (!root->log_root)
  195. return -ENOENT;
  196. mutex_lock(&root->log_mutex);
  197. if (root->log_root) {
  198. ret = 0;
  199. atomic_inc(&root->log_writers);
  200. }
  201. mutex_unlock(&root->log_mutex);
  202. return ret;
  203. }
  204. /*
  205. * This either makes the current running log transaction wait
  206. * until you call btrfs_end_log_trans() or it makes any future
  207. * log transactions wait until you call btrfs_end_log_trans()
  208. */
  209. int btrfs_pin_log_trans(struct btrfs_root *root)
  210. {
  211. int ret = -ENOENT;
  212. mutex_lock(&root->log_mutex);
  213. atomic_inc(&root->log_writers);
  214. mutex_unlock(&root->log_mutex);
  215. return ret;
  216. }
  217. /*
  218. * indicate we're done making changes to the log tree
  219. * and wake up anyone waiting to do a sync
  220. */
  221. void btrfs_end_log_trans(struct btrfs_root *root)
  222. {
  223. if (atomic_dec_and_test(&root->log_writers)) {
  224. smp_mb();
  225. if (waitqueue_active(&root->log_writer_wait))
  226. wake_up(&root->log_writer_wait);
  227. }
  228. }
  229. /*
  230. * the walk control struct is used to pass state down the chain when
  231. * processing the log tree. The stage field tells us which part
  232. * of the log tree processing we are currently doing. The others
  233. * are state fields used for that specific part
  234. */
  235. struct walk_control {
  236. /* should we free the extent on disk when done? This is used
  237. * at transaction commit time while freeing a log tree
  238. */
  239. int free;
  240. /* should we write out the extent buffer? This is used
  241. * while flushing the log tree to disk during a sync
  242. */
  243. int write;
  244. /* should we wait for the extent buffer io to finish? Also used
  245. * while flushing the log tree to disk for a sync
  246. */
  247. int wait;
  248. /* pin only walk, we record which extents on disk belong to the
  249. * log trees
  250. */
  251. int pin;
  252. /* what stage of the replay code we're currently in */
  253. int stage;
  254. /* the root we are currently replaying */
  255. struct btrfs_root *replay_dest;
  256. /* the trans handle for the current replay */
  257. struct btrfs_trans_handle *trans;
  258. /* the function that gets used to process blocks we find in the
  259. * tree. Note the extent_buffer might not be up to date when it is
  260. * passed in, and it must be checked or read if you need the data
  261. * inside it
  262. */
  263. int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
  264. struct walk_control *wc, u64 gen);
  265. };
  266. /*
  267. * process_func used to pin down extents, write them or wait on them
  268. */
  269. static int process_one_buffer(struct btrfs_root *log,
  270. struct extent_buffer *eb,
  271. struct walk_control *wc, u64 gen)
  272. {
  273. int ret = 0;
  274. /*
  275. * If this fs is mixed then we need to be able to process the leaves to
  276. * pin down any logged extents, so we have to read the block.
  277. */
  278. if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
  279. ret = btrfs_read_buffer(eb, gen);
  280. if (ret)
  281. return ret;
  282. }
  283. if (wc->pin)
  284. ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
  285. eb->start, eb->len);
  286. if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
  287. if (wc->pin && btrfs_header_level(eb) == 0)
  288. ret = btrfs_exclude_logged_extents(log, eb);
  289. if (wc->write)
  290. btrfs_write_tree_block(eb);
  291. if (wc->wait)
  292. btrfs_wait_tree_block_writeback(eb);
  293. }
  294. return ret;
  295. }
  296. /*
  297. * Item overwrite used by replay and tree logging. eb, slot and key all refer
  298. * to the src data we are copying out.
  299. *
  300. * root is the tree we are copying into, and path is a scratch
  301. * path for use in this function (it should be released on entry and
  302. * will be released on exit).
  303. *
  304. * If the key is already in the destination tree the existing item is
  305. * overwritten. If the existing item isn't big enough, it is extended.
  306. * If it is too large, it is truncated.
  307. *
  308. * If the key isn't in the destination yet, a new item is inserted.
  309. */
  310. static noinline int overwrite_item(struct btrfs_trans_handle *trans,
  311. struct btrfs_root *root,
  312. struct btrfs_path *path,
  313. struct extent_buffer *eb, int slot,
  314. struct btrfs_key *key)
  315. {
  316. int ret;
  317. u32 item_size;
  318. u64 saved_i_size = 0;
  319. int save_old_i_size = 0;
  320. unsigned long src_ptr;
  321. unsigned long dst_ptr;
  322. int overwrite_root = 0;
  323. bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
  324. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  325. overwrite_root = 1;
  326. item_size = btrfs_item_size_nr(eb, slot);
  327. src_ptr = btrfs_item_ptr_offset(eb, slot);
  328. /* look for the key in the destination tree */
  329. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  330. if (ret < 0)
  331. return ret;
  332. if (ret == 0) {
  333. char *src_copy;
  334. char *dst_copy;
  335. u32 dst_size = btrfs_item_size_nr(path->nodes[0],
  336. path->slots[0]);
  337. if (dst_size != item_size)
  338. goto insert;
  339. if (item_size == 0) {
  340. btrfs_release_path(path);
  341. return 0;
  342. }
  343. dst_copy = kmalloc(item_size, GFP_NOFS);
  344. src_copy = kmalloc(item_size, GFP_NOFS);
  345. if (!dst_copy || !src_copy) {
  346. btrfs_release_path(path);
  347. kfree(dst_copy);
  348. kfree(src_copy);
  349. return -ENOMEM;
  350. }
  351. read_extent_buffer(eb, src_copy, src_ptr, item_size);
  352. dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  353. read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
  354. item_size);
  355. ret = memcmp(dst_copy, src_copy, item_size);
  356. kfree(dst_copy);
  357. kfree(src_copy);
  358. /*
  359. * they have the same contents, just return, this saves
  360. * us from cowing blocks in the destination tree and doing
  361. * extra writes that may not have been done by a previous
  362. * sync
  363. */
  364. if (ret == 0) {
  365. btrfs_release_path(path);
  366. return 0;
  367. }
  368. /*
  369. * We need to load the old nbytes into the inode so when we
  370. * replay the extents we've logged we get the right nbytes.
  371. */
  372. if (inode_item) {
  373. struct btrfs_inode_item *item;
  374. u64 nbytes;
  375. u32 mode;
  376. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  377. struct btrfs_inode_item);
  378. nbytes = btrfs_inode_nbytes(path->nodes[0], item);
  379. item = btrfs_item_ptr(eb, slot,
  380. struct btrfs_inode_item);
  381. btrfs_set_inode_nbytes(eb, item, nbytes);
  382. /*
  383. * If this is a directory we need to reset the i_size to
  384. * 0 so that we can set it up properly when replaying
  385. * the rest of the items in this log.
  386. */
  387. mode = btrfs_inode_mode(eb, item);
  388. if (S_ISDIR(mode))
  389. btrfs_set_inode_size(eb, item, 0);
  390. }
  391. } else if (inode_item) {
  392. struct btrfs_inode_item *item;
  393. u32 mode;
  394. /*
  395. * New inode, set nbytes to 0 so that the nbytes comes out
  396. * properly when we replay the extents.
  397. */
  398. item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
  399. btrfs_set_inode_nbytes(eb, item, 0);
  400. /*
  401. * If this is a directory we need to reset the i_size to 0 so
  402. * that we can set it up properly when replaying the rest of
  403. * the items in this log.
  404. */
  405. mode = btrfs_inode_mode(eb, item);
  406. if (S_ISDIR(mode))
  407. btrfs_set_inode_size(eb, item, 0);
  408. }
  409. insert:
  410. btrfs_release_path(path);
  411. /* try to insert the key into the destination tree */
  412. ret = btrfs_insert_empty_item(trans, root, path,
  413. key, item_size);
  414. /* make sure any existing item is the correct size */
  415. if (ret == -EEXIST) {
  416. u32 found_size;
  417. found_size = btrfs_item_size_nr(path->nodes[0],
  418. path->slots[0]);
  419. if (found_size > item_size)
  420. btrfs_truncate_item(root, path, item_size, 1);
  421. else if (found_size < item_size)
  422. btrfs_extend_item(root, path,
  423. item_size - found_size);
  424. } else if (ret) {
  425. return ret;
  426. }
  427. dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
  428. path->slots[0]);
  429. /* don't overwrite an existing inode if the generation number
  430. * was logged as zero. This is done when the tree logging code
  431. * is just logging an inode to make sure it exists after recovery.
  432. *
  433. * Also, don't overwrite i_size on directories during replay.
  434. * log replay inserts and removes directory items based on the
  435. * state of the tree found in the subvolume, and i_size is modified
  436. * as it goes
  437. */
  438. if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
  439. struct btrfs_inode_item *src_item;
  440. struct btrfs_inode_item *dst_item;
  441. src_item = (struct btrfs_inode_item *)src_ptr;
  442. dst_item = (struct btrfs_inode_item *)dst_ptr;
  443. if (btrfs_inode_generation(eb, src_item) == 0)
  444. goto no_copy;
  445. if (overwrite_root &&
  446. S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
  447. S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
  448. save_old_i_size = 1;
  449. saved_i_size = btrfs_inode_size(path->nodes[0],
  450. dst_item);
  451. }
  452. }
  453. copy_extent_buffer(path->nodes[0], eb, dst_ptr,
  454. src_ptr, item_size);
  455. if (save_old_i_size) {
  456. struct btrfs_inode_item *dst_item;
  457. dst_item = (struct btrfs_inode_item *)dst_ptr;
  458. btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
  459. }
  460. /* make sure the generation is filled in */
  461. if (key->type == BTRFS_INODE_ITEM_KEY) {
  462. struct btrfs_inode_item *dst_item;
  463. dst_item = (struct btrfs_inode_item *)dst_ptr;
  464. if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
  465. btrfs_set_inode_generation(path->nodes[0], dst_item,
  466. trans->transid);
  467. }
  468. }
  469. no_copy:
  470. btrfs_mark_buffer_dirty(path->nodes[0]);
  471. btrfs_release_path(path);
  472. return 0;
  473. }
  474. /*
  475. * simple helper to read an inode off the disk from a given root
  476. * This can only be called for subvolume roots and not for the log
  477. */
  478. static noinline struct inode *read_one_inode(struct btrfs_root *root,
  479. u64 objectid)
  480. {
  481. struct btrfs_key key;
  482. struct inode *inode;
  483. key.objectid = objectid;
  484. key.type = BTRFS_INODE_ITEM_KEY;
  485. key.offset = 0;
  486. inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
  487. if (IS_ERR(inode)) {
  488. inode = NULL;
  489. } else if (is_bad_inode(inode)) {
  490. iput(inode);
  491. inode = NULL;
  492. }
  493. return inode;
  494. }
  495. /* replays a single extent in 'eb' at 'slot' with 'key' into the
  496. * subvolume 'root'. path is released on entry and should be released
  497. * on exit.
  498. *
  499. * extents in the log tree have not been allocated out of the extent
  500. * tree yet. So, this completes the allocation, taking a reference
  501. * as required if the extent already exists or creating a new extent
  502. * if it isn't in the extent allocation tree yet.
  503. *
  504. * The extent is inserted into the file, dropping any existing extents
  505. * from the file that overlap the new one.
  506. */
  507. static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
  508. struct btrfs_root *root,
  509. struct btrfs_path *path,
  510. struct extent_buffer *eb, int slot,
  511. struct btrfs_key *key)
  512. {
  513. int found_type;
  514. u64 extent_end;
  515. u64 start = key->offset;
  516. u64 nbytes = 0;
  517. struct btrfs_file_extent_item *item;
  518. struct inode *inode = NULL;
  519. unsigned long size;
  520. int ret = 0;
  521. item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  522. found_type = btrfs_file_extent_type(eb, item);
  523. if (found_type == BTRFS_FILE_EXTENT_REG ||
  524. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  525. nbytes = btrfs_file_extent_num_bytes(eb, item);
  526. extent_end = start + nbytes;
  527. /*
  528. * We don't add to the inodes nbytes if we are prealloc or a
  529. * hole.
  530. */
  531. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  532. nbytes = 0;
  533. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  534. size = btrfs_file_extent_inline_len(eb, slot, item);
  535. nbytes = btrfs_file_extent_ram_bytes(eb, item);
  536. extent_end = ALIGN(start + size, root->sectorsize);
  537. } else {
  538. ret = 0;
  539. goto out;
  540. }
  541. inode = read_one_inode(root, key->objectid);
  542. if (!inode) {
  543. ret = -EIO;
  544. goto out;
  545. }
  546. /*
  547. * first check to see if we already have this extent in the
  548. * file. This must be done before the btrfs_drop_extents run
  549. * so we don't try to drop this extent.
  550. */
  551. ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
  552. start, 0);
  553. if (ret == 0 &&
  554. (found_type == BTRFS_FILE_EXTENT_REG ||
  555. found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
  556. struct btrfs_file_extent_item cmp1;
  557. struct btrfs_file_extent_item cmp2;
  558. struct btrfs_file_extent_item *existing;
  559. struct extent_buffer *leaf;
  560. leaf = path->nodes[0];
  561. existing = btrfs_item_ptr(leaf, path->slots[0],
  562. struct btrfs_file_extent_item);
  563. read_extent_buffer(eb, &cmp1, (unsigned long)item,
  564. sizeof(cmp1));
  565. read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
  566. sizeof(cmp2));
  567. /*
  568. * we already have a pointer to this exact extent,
  569. * we don't have to do anything
  570. */
  571. if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
  572. btrfs_release_path(path);
  573. goto out;
  574. }
  575. }
  576. btrfs_release_path(path);
  577. /* drop any overlapping extents */
  578. ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
  579. if (ret)
  580. goto out;
  581. if (found_type == BTRFS_FILE_EXTENT_REG ||
  582. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  583. u64 offset;
  584. unsigned long dest_offset;
  585. struct btrfs_key ins;
  586. ret = btrfs_insert_empty_item(trans, root, path, key,
  587. sizeof(*item));
  588. if (ret)
  589. goto out;
  590. dest_offset = btrfs_item_ptr_offset(path->nodes[0],
  591. path->slots[0]);
  592. copy_extent_buffer(path->nodes[0], eb, dest_offset,
  593. (unsigned long)item, sizeof(*item));
  594. ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  595. ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  596. ins.type = BTRFS_EXTENT_ITEM_KEY;
  597. offset = key->offset - btrfs_file_extent_offset(eb, item);
  598. if (ins.objectid > 0) {
  599. u64 csum_start;
  600. u64 csum_end;
  601. LIST_HEAD(ordered_sums);
  602. /*
  603. * is this extent already allocated in the extent
  604. * allocation tree? If so, just add a reference
  605. */
  606. ret = btrfs_lookup_data_extent(root, ins.objectid,
  607. ins.offset);
  608. if (ret == 0) {
  609. ret = btrfs_inc_extent_ref(trans, root,
  610. ins.objectid, ins.offset,
  611. 0, root->root_key.objectid,
  612. key->objectid, offset, 0);
  613. if (ret)
  614. goto out;
  615. } else {
  616. /*
  617. * insert the extent pointer in the extent
  618. * allocation tree
  619. */
  620. ret = btrfs_alloc_logged_file_extent(trans,
  621. root, root->root_key.objectid,
  622. key->objectid, offset, &ins);
  623. if (ret)
  624. goto out;
  625. }
  626. btrfs_release_path(path);
  627. if (btrfs_file_extent_compression(eb, item)) {
  628. csum_start = ins.objectid;
  629. csum_end = csum_start + ins.offset;
  630. } else {
  631. csum_start = ins.objectid +
  632. btrfs_file_extent_offset(eb, item);
  633. csum_end = csum_start +
  634. btrfs_file_extent_num_bytes(eb, item);
  635. }
  636. ret = btrfs_lookup_csums_range(root->log_root,
  637. csum_start, csum_end - 1,
  638. &ordered_sums, 0);
  639. if (ret)
  640. goto out;
  641. while (!list_empty(&ordered_sums)) {
  642. struct btrfs_ordered_sum *sums;
  643. sums = list_entry(ordered_sums.next,
  644. struct btrfs_ordered_sum,
  645. list);
  646. if (!ret)
  647. ret = btrfs_csum_file_blocks(trans,
  648. root->fs_info->csum_root,
  649. sums);
  650. list_del(&sums->list);
  651. kfree(sums);
  652. }
  653. if (ret)
  654. goto out;
  655. } else {
  656. btrfs_release_path(path);
  657. }
  658. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  659. /* inline extents are easy, we just overwrite them */
  660. ret = overwrite_item(trans, root, path, eb, slot, key);
  661. if (ret)
  662. goto out;
  663. }
  664. inode_add_bytes(inode, nbytes);
  665. ret = btrfs_update_inode(trans, root, inode);
  666. out:
  667. if (inode)
  668. iput(inode);
  669. return ret;
  670. }
  671. /*
  672. * when cleaning up conflicts between the directory names in the
  673. * subvolume, directory names in the log and directory names in the
  674. * inode back references, we may have to unlink inodes from directories.
  675. *
  676. * This is a helper function to do the unlink of a specific directory
  677. * item
  678. */
  679. static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
  680. struct btrfs_root *root,
  681. struct btrfs_path *path,
  682. struct inode *dir,
  683. struct btrfs_dir_item *di)
  684. {
  685. struct inode *inode;
  686. char *name;
  687. int name_len;
  688. struct extent_buffer *leaf;
  689. struct btrfs_key location;
  690. int ret;
  691. leaf = path->nodes[0];
  692. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  693. name_len = btrfs_dir_name_len(leaf, di);
  694. name = kmalloc(name_len, GFP_NOFS);
  695. if (!name)
  696. return -ENOMEM;
  697. read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
  698. btrfs_release_path(path);
  699. inode = read_one_inode(root, location.objectid);
  700. if (!inode) {
  701. ret = -EIO;
  702. goto out;
  703. }
  704. ret = link_to_fixup_dir(trans, root, path, location.objectid);
  705. if (ret)
  706. goto out;
  707. ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
  708. if (ret)
  709. goto out;
  710. else
  711. ret = btrfs_run_delayed_items(trans, root);
  712. out:
  713. kfree(name);
  714. iput(inode);
  715. return ret;
  716. }
  717. /*
  718. * helper function to see if a given name and sequence number found
  719. * in an inode back reference are already in a directory and correctly
  720. * point to this inode
  721. */
  722. static noinline int inode_in_dir(struct btrfs_root *root,
  723. struct btrfs_path *path,
  724. u64 dirid, u64 objectid, u64 index,
  725. const char *name, int name_len)
  726. {
  727. struct btrfs_dir_item *di;
  728. struct btrfs_key location;
  729. int match = 0;
  730. di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
  731. index, name, name_len, 0);
  732. if (di && !IS_ERR(di)) {
  733. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  734. if (location.objectid != objectid)
  735. goto out;
  736. } else
  737. goto out;
  738. btrfs_release_path(path);
  739. di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
  740. if (di && !IS_ERR(di)) {
  741. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  742. if (location.objectid != objectid)
  743. goto out;
  744. } else
  745. goto out;
  746. match = 1;
  747. out:
  748. btrfs_release_path(path);
  749. return match;
  750. }
  751. /*
  752. * helper function to check a log tree for a named back reference in
  753. * an inode. This is used to decide if a back reference that is
  754. * found in the subvolume conflicts with what we find in the log.
  755. *
  756. * inode backreferences may have multiple refs in a single item,
  757. * during replay we process one reference at a time, and we don't
  758. * want to delete valid links to a file from the subvolume if that
  759. * link is also in the log.
  760. */
  761. static noinline int backref_in_log(struct btrfs_root *log,
  762. struct btrfs_key *key,
  763. u64 ref_objectid,
  764. char *name, int namelen)
  765. {
  766. struct btrfs_path *path;
  767. struct btrfs_inode_ref *ref;
  768. unsigned long ptr;
  769. unsigned long ptr_end;
  770. unsigned long name_ptr;
  771. int found_name_len;
  772. int item_size;
  773. int ret;
  774. int match = 0;
  775. path = btrfs_alloc_path();
  776. if (!path)
  777. return -ENOMEM;
  778. ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
  779. if (ret != 0)
  780. goto out;
  781. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  782. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  783. if (btrfs_find_name_in_ext_backref(path, ref_objectid,
  784. name, namelen, NULL))
  785. match = 1;
  786. goto out;
  787. }
  788. item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
  789. ptr_end = ptr + item_size;
  790. while (ptr < ptr_end) {
  791. ref = (struct btrfs_inode_ref *)ptr;
  792. found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
  793. if (found_name_len == namelen) {
  794. name_ptr = (unsigned long)(ref + 1);
  795. ret = memcmp_extent_buffer(path->nodes[0], name,
  796. name_ptr, namelen);
  797. if (ret == 0) {
  798. match = 1;
  799. goto out;
  800. }
  801. }
  802. ptr = (unsigned long)(ref + 1) + found_name_len;
  803. }
  804. out:
  805. btrfs_free_path(path);
  806. return match;
  807. }
  808. static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
  809. struct btrfs_root *root,
  810. struct btrfs_path *path,
  811. struct btrfs_root *log_root,
  812. struct inode *dir, struct inode *inode,
  813. struct extent_buffer *eb,
  814. u64 inode_objectid, u64 parent_objectid,
  815. u64 ref_index, char *name, int namelen,
  816. int *search_done)
  817. {
  818. int ret;
  819. char *victim_name;
  820. int victim_name_len;
  821. struct extent_buffer *leaf;
  822. struct btrfs_dir_item *di;
  823. struct btrfs_key search_key;
  824. struct btrfs_inode_extref *extref;
  825. again:
  826. /* Search old style refs */
  827. search_key.objectid = inode_objectid;
  828. search_key.type = BTRFS_INODE_REF_KEY;
  829. search_key.offset = parent_objectid;
  830. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  831. if (ret == 0) {
  832. struct btrfs_inode_ref *victim_ref;
  833. unsigned long ptr;
  834. unsigned long ptr_end;
  835. leaf = path->nodes[0];
  836. /* are we trying to overwrite a back ref for the root directory
  837. * if so, just jump out, we're done
  838. */
  839. if (search_key.objectid == search_key.offset)
  840. return 1;
  841. /* check all the names in this back reference to see
  842. * if they are in the log. if so, we allow them to stay
  843. * otherwise they must be unlinked as a conflict
  844. */
  845. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  846. ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
  847. while (ptr < ptr_end) {
  848. victim_ref = (struct btrfs_inode_ref *)ptr;
  849. victim_name_len = btrfs_inode_ref_name_len(leaf,
  850. victim_ref);
  851. victim_name = kmalloc(victim_name_len, GFP_NOFS);
  852. if (!victim_name)
  853. return -ENOMEM;
  854. read_extent_buffer(leaf, victim_name,
  855. (unsigned long)(victim_ref + 1),
  856. victim_name_len);
  857. if (!backref_in_log(log_root, &search_key,
  858. parent_objectid,
  859. victim_name,
  860. victim_name_len)) {
  861. inc_nlink(inode);
  862. btrfs_release_path(path);
  863. ret = btrfs_unlink_inode(trans, root, dir,
  864. inode, victim_name,
  865. victim_name_len);
  866. kfree(victim_name);
  867. if (ret)
  868. return ret;
  869. ret = btrfs_run_delayed_items(trans, root);
  870. if (ret)
  871. return ret;
  872. *search_done = 1;
  873. goto again;
  874. }
  875. kfree(victim_name);
  876. ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
  877. }
  878. /*
  879. * NOTE: we have searched root tree and checked the
  880. * coresponding ref, it does not need to check again.
  881. */
  882. *search_done = 1;
  883. }
  884. btrfs_release_path(path);
  885. /* Same search but for extended refs */
  886. extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
  887. inode_objectid, parent_objectid, 0,
  888. 0);
  889. if (!IS_ERR_OR_NULL(extref)) {
  890. u32 item_size;
  891. u32 cur_offset = 0;
  892. unsigned long base;
  893. struct inode *victim_parent;
  894. leaf = path->nodes[0];
  895. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  896. base = btrfs_item_ptr_offset(leaf, path->slots[0]);
  897. while (cur_offset < item_size) {
  898. extref = (struct btrfs_inode_extref *)base + cur_offset;
  899. victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
  900. if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
  901. goto next;
  902. victim_name = kmalloc(victim_name_len, GFP_NOFS);
  903. if (!victim_name)
  904. return -ENOMEM;
  905. read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
  906. victim_name_len);
  907. search_key.objectid = inode_objectid;
  908. search_key.type = BTRFS_INODE_EXTREF_KEY;
  909. search_key.offset = btrfs_extref_hash(parent_objectid,
  910. victim_name,
  911. victim_name_len);
  912. ret = 0;
  913. if (!backref_in_log(log_root, &search_key,
  914. parent_objectid, victim_name,
  915. victim_name_len)) {
  916. ret = -ENOENT;
  917. victim_parent = read_one_inode(root,
  918. parent_objectid);
  919. if (victim_parent) {
  920. inc_nlink(inode);
  921. btrfs_release_path(path);
  922. ret = btrfs_unlink_inode(trans, root,
  923. victim_parent,
  924. inode,
  925. victim_name,
  926. victim_name_len);
  927. if (!ret)
  928. ret = btrfs_run_delayed_items(
  929. trans, root);
  930. }
  931. iput(victim_parent);
  932. kfree(victim_name);
  933. if (ret)
  934. return ret;
  935. *search_done = 1;
  936. goto again;
  937. }
  938. kfree(victim_name);
  939. if (ret)
  940. return ret;
  941. next:
  942. cur_offset += victim_name_len + sizeof(*extref);
  943. }
  944. *search_done = 1;
  945. }
  946. btrfs_release_path(path);
  947. /* look for a conflicting sequence number */
  948. di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
  949. ref_index, name, namelen, 0);
  950. if (di && !IS_ERR(di)) {
  951. ret = drop_one_dir_item(trans, root, path, dir, di);
  952. if (ret)
  953. return ret;
  954. }
  955. btrfs_release_path(path);
  956. /* look for a conflicing name */
  957. di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
  958. name, namelen, 0);
  959. if (di && !IS_ERR(di)) {
  960. ret = drop_one_dir_item(trans, root, path, dir, di);
  961. if (ret)
  962. return ret;
  963. }
  964. btrfs_release_path(path);
  965. return 0;
  966. }
  967. static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  968. u32 *namelen, char **name, u64 *index,
  969. u64 *parent_objectid)
  970. {
  971. struct btrfs_inode_extref *extref;
  972. extref = (struct btrfs_inode_extref *)ref_ptr;
  973. *namelen = btrfs_inode_extref_name_len(eb, extref);
  974. *name = kmalloc(*namelen, GFP_NOFS);
  975. if (*name == NULL)
  976. return -ENOMEM;
  977. read_extent_buffer(eb, *name, (unsigned long)&extref->name,
  978. *namelen);
  979. *index = btrfs_inode_extref_index(eb, extref);
  980. if (parent_objectid)
  981. *parent_objectid = btrfs_inode_extref_parent(eb, extref);
  982. return 0;
  983. }
  984. static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  985. u32 *namelen, char **name, u64 *index)
  986. {
  987. struct btrfs_inode_ref *ref;
  988. ref = (struct btrfs_inode_ref *)ref_ptr;
  989. *namelen = btrfs_inode_ref_name_len(eb, ref);
  990. *name = kmalloc(*namelen, GFP_NOFS);
  991. if (*name == NULL)
  992. return -ENOMEM;
  993. read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
  994. *index = btrfs_inode_ref_index(eb, ref);
  995. return 0;
  996. }
  997. /*
  998. * replay one inode back reference item found in the log tree.
  999. * eb, slot and key refer to the buffer and key found in the log tree.
  1000. * root is the destination we are replaying into, and path is for temp
  1001. * use by this function. (it should be released on return).
  1002. */
  1003. static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
  1004. struct btrfs_root *root,
  1005. struct btrfs_root *log,
  1006. struct btrfs_path *path,
  1007. struct extent_buffer *eb, int slot,
  1008. struct btrfs_key *key)
  1009. {
  1010. struct inode *dir = NULL;
  1011. struct inode *inode = NULL;
  1012. unsigned long ref_ptr;
  1013. unsigned long ref_end;
  1014. char *name = NULL;
  1015. int namelen;
  1016. int ret;
  1017. int search_done = 0;
  1018. int log_ref_ver = 0;
  1019. u64 parent_objectid;
  1020. u64 inode_objectid;
  1021. u64 ref_index = 0;
  1022. int ref_struct_size;
  1023. ref_ptr = btrfs_item_ptr_offset(eb, slot);
  1024. ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
  1025. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  1026. struct btrfs_inode_extref *r;
  1027. ref_struct_size = sizeof(struct btrfs_inode_extref);
  1028. log_ref_ver = 1;
  1029. r = (struct btrfs_inode_extref *)ref_ptr;
  1030. parent_objectid = btrfs_inode_extref_parent(eb, r);
  1031. } else {
  1032. ref_struct_size = sizeof(struct btrfs_inode_ref);
  1033. parent_objectid = key->offset;
  1034. }
  1035. inode_objectid = key->objectid;
  1036. /*
  1037. * it is possible that we didn't log all the parent directories
  1038. * for a given inode. If we don't find the dir, just don't
  1039. * copy the back ref in. The link count fixup code will take
  1040. * care of the rest
  1041. */
  1042. dir = read_one_inode(root, parent_objectid);
  1043. if (!dir) {
  1044. ret = -ENOENT;
  1045. goto out;
  1046. }
  1047. inode = read_one_inode(root, inode_objectid);
  1048. if (!inode) {
  1049. ret = -EIO;
  1050. goto out;
  1051. }
  1052. while (ref_ptr < ref_end) {
  1053. if (log_ref_ver) {
  1054. ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
  1055. &ref_index, &parent_objectid);
  1056. /*
  1057. * parent object can change from one array
  1058. * item to another.
  1059. */
  1060. if (!dir)
  1061. dir = read_one_inode(root, parent_objectid);
  1062. if (!dir) {
  1063. ret = -ENOENT;
  1064. goto out;
  1065. }
  1066. } else {
  1067. ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
  1068. &ref_index);
  1069. }
  1070. if (ret)
  1071. goto out;
  1072. /* if we already have a perfect match, we're done */
  1073. if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
  1074. ref_index, name, namelen)) {
  1075. /*
  1076. * look for a conflicting back reference in the
  1077. * metadata. if we find one we have to unlink that name
  1078. * of the file before we add our new link. Later on, we
  1079. * overwrite any existing back reference, and we don't
  1080. * want to create dangling pointers in the directory.
  1081. */
  1082. if (!search_done) {
  1083. ret = __add_inode_ref(trans, root, path, log,
  1084. dir, inode, eb,
  1085. inode_objectid,
  1086. parent_objectid,
  1087. ref_index, name, namelen,
  1088. &search_done);
  1089. if (ret) {
  1090. if (ret == 1)
  1091. ret = 0;
  1092. goto out;
  1093. }
  1094. }
  1095. /* insert our name */
  1096. ret = btrfs_add_link(trans, dir, inode, name, namelen,
  1097. 0, ref_index);
  1098. if (ret)
  1099. goto out;
  1100. btrfs_update_inode(trans, root, inode);
  1101. }
  1102. ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
  1103. kfree(name);
  1104. name = NULL;
  1105. if (log_ref_ver) {
  1106. iput(dir);
  1107. dir = NULL;
  1108. }
  1109. }
  1110. /* finally write the back reference in the inode */
  1111. ret = overwrite_item(trans, root, path, eb, slot, key);
  1112. out:
  1113. btrfs_release_path(path);
  1114. kfree(name);
  1115. iput(dir);
  1116. iput(inode);
  1117. return ret;
  1118. }
  1119. static int insert_orphan_item(struct btrfs_trans_handle *trans,
  1120. struct btrfs_root *root, u64 ino)
  1121. {
  1122. int ret;
  1123. ret = btrfs_insert_orphan_item(trans, root, ino);
  1124. if (ret == -EEXIST)
  1125. ret = 0;
  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);
  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. ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages,
  2303. mark);
  2304. btrfs_wait_logged_extents(trans, log, log_transid);
  2305. wait_log_commit(trans, log_root_tree,
  2306. root_log_ctx.log_transid);
  2307. mutex_unlock(&log_root_tree->log_mutex);
  2308. if (!ret)
  2309. ret = root_log_ctx.log_ret;
  2310. goto out;
  2311. }
  2312. ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
  2313. atomic_set(&log_root_tree->log_commit[index2], 1);
  2314. if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
  2315. wait_log_commit(trans, log_root_tree,
  2316. root_log_ctx.log_transid - 1);
  2317. }
  2318. wait_for_writer(trans, log_root_tree);
  2319. /*
  2320. * now that we've moved on to the tree of log tree roots,
  2321. * check the full commit flag again
  2322. */
  2323. if (btrfs_need_log_full_commit(root->fs_info, trans)) {
  2324. blk_finish_plug(&plug);
  2325. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2326. btrfs_free_logged_extents(log, log_transid);
  2327. mutex_unlock(&log_root_tree->log_mutex);
  2328. ret = -EAGAIN;
  2329. goto out_wake_log_root;
  2330. }
  2331. ret = btrfs_write_marked_extents(log_root_tree,
  2332. &log_root_tree->dirty_log_pages,
  2333. EXTENT_DIRTY | EXTENT_NEW);
  2334. blk_finish_plug(&plug);
  2335. if (ret) {
  2336. btrfs_set_log_full_commit(root->fs_info, trans);
  2337. btrfs_abort_transaction(trans, root, ret);
  2338. btrfs_free_logged_extents(log, log_transid);
  2339. mutex_unlock(&log_root_tree->log_mutex);
  2340. goto out_wake_log_root;
  2341. }
  2342. ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  2343. if (!ret)
  2344. ret = btrfs_wait_marked_extents(log_root_tree,
  2345. &log_root_tree->dirty_log_pages,
  2346. EXTENT_NEW | EXTENT_DIRTY);
  2347. if (ret) {
  2348. btrfs_set_log_full_commit(root->fs_info, trans);
  2349. btrfs_free_logged_extents(log, log_transid);
  2350. mutex_unlock(&log_root_tree->log_mutex);
  2351. goto out_wake_log_root;
  2352. }
  2353. btrfs_wait_logged_extents(trans, log, log_transid);
  2354. btrfs_set_super_log_root(root->fs_info->super_for_commit,
  2355. log_root_tree->node->start);
  2356. btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
  2357. btrfs_header_level(log_root_tree->node));
  2358. log_root_tree->log_transid++;
  2359. mutex_unlock(&log_root_tree->log_mutex);
  2360. /*
  2361. * nobody else is going to jump in and write the the ctree
  2362. * super here because the log_commit atomic below is protecting
  2363. * us. We must be called with a transaction handle pinning
  2364. * the running transaction open, so a full commit can't hop
  2365. * in and cause problems either.
  2366. */
  2367. ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
  2368. if (ret) {
  2369. btrfs_set_log_full_commit(root->fs_info, trans);
  2370. btrfs_abort_transaction(trans, root, ret);
  2371. goto out_wake_log_root;
  2372. }
  2373. mutex_lock(&root->log_mutex);
  2374. if (root->last_log_commit < log_transid)
  2375. root->last_log_commit = log_transid;
  2376. mutex_unlock(&root->log_mutex);
  2377. out_wake_log_root:
  2378. /*
  2379. * We needn't get log_mutex here because we are sure all
  2380. * the other tasks are blocked.
  2381. */
  2382. btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
  2383. mutex_lock(&log_root_tree->log_mutex);
  2384. log_root_tree->log_transid_committed++;
  2385. atomic_set(&log_root_tree->log_commit[index2], 0);
  2386. mutex_unlock(&log_root_tree->log_mutex);
  2387. if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
  2388. wake_up(&log_root_tree->log_commit_wait[index2]);
  2389. out:
  2390. /* See above. */
  2391. btrfs_remove_all_log_ctxs(root, index1, ret);
  2392. mutex_lock(&root->log_mutex);
  2393. root->log_transid_committed++;
  2394. atomic_set(&root->log_commit[index1], 0);
  2395. mutex_unlock(&root->log_mutex);
  2396. if (waitqueue_active(&root->log_commit_wait[index1]))
  2397. wake_up(&root->log_commit_wait[index1]);
  2398. return ret;
  2399. }
  2400. static void free_log_tree(struct btrfs_trans_handle *trans,
  2401. struct btrfs_root *log)
  2402. {
  2403. int ret;
  2404. u64 start;
  2405. u64 end;
  2406. struct walk_control wc = {
  2407. .free = 1,
  2408. .process_func = process_one_buffer
  2409. };
  2410. ret = walk_log_tree(trans, log, &wc);
  2411. /* I don't think this can happen but just in case */
  2412. if (ret)
  2413. btrfs_abort_transaction(trans, log, ret);
  2414. while (1) {
  2415. ret = find_first_extent_bit(&log->dirty_log_pages,
  2416. 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
  2417. NULL);
  2418. if (ret)
  2419. break;
  2420. clear_extent_bits(&log->dirty_log_pages, start, end,
  2421. EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
  2422. }
  2423. /*
  2424. * We may have short-circuited the log tree with the full commit logic
  2425. * and left ordered extents on our list, so clear these out to keep us
  2426. * from leaking inodes and memory.
  2427. */
  2428. btrfs_free_logged_extents(log, 0);
  2429. btrfs_free_logged_extents(log, 1);
  2430. free_extent_buffer(log->node);
  2431. kfree(log);
  2432. }
  2433. /*
  2434. * free all the extents used by the tree log. This should be called
  2435. * at commit time of the full transaction
  2436. */
  2437. int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
  2438. {
  2439. if (root->log_root) {
  2440. free_log_tree(trans, root->log_root);
  2441. root->log_root = NULL;
  2442. }
  2443. return 0;
  2444. }
  2445. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  2446. struct btrfs_fs_info *fs_info)
  2447. {
  2448. if (fs_info->log_root_tree) {
  2449. free_log_tree(trans, fs_info->log_root_tree);
  2450. fs_info->log_root_tree = NULL;
  2451. }
  2452. return 0;
  2453. }
  2454. /*
  2455. * If both a file and directory are logged, and unlinks or renames are
  2456. * mixed in, we have a few interesting corners:
  2457. *
  2458. * create file X in dir Y
  2459. * link file X to X.link in dir Y
  2460. * fsync file X
  2461. * unlink file X but leave X.link
  2462. * fsync dir Y
  2463. *
  2464. * After a crash we would expect only X.link to exist. But file X
  2465. * didn't get fsync'd again so the log has back refs for X and X.link.
  2466. *
  2467. * We solve this by removing directory entries and inode backrefs from the
  2468. * log when a file that was logged in the current transaction is
  2469. * unlinked. Any later fsync will include the updated log entries, and
  2470. * we'll be able to reconstruct the proper directory items from backrefs.
  2471. *
  2472. * This optimizations allows us to avoid relogging the entire inode
  2473. * or the entire directory.
  2474. */
  2475. int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
  2476. struct btrfs_root *root,
  2477. const char *name, int name_len,
  2478. struct inode *dir, u64 index)
  2479. {
  2480. struct btrfs_root *log;
  2481. struct btrfs_dir_item *di;
  2482. struct btrfs_path *path;
  2483. int ret;
  2484. int err = 0;
  2485. int bytes_del = 0;
  2486. u64 dir_ino = btrfs_ino(dir);
  2487. if (BTRFS_I(dir)->logged_trans < trans->transid)
  2488. return 0;
  2489. ret = join_running_log_trans(root);
  2490. if (ret)
  2491. return 0;
  2492. mutex_lock(&BTRFS_I(dir)->log_mutex);
  2493. log = root->log_root;
  2494. path = btrfs_alloc_path();
  2495. if (!path) {
  2496. err = -ENOMEM;
  2497. goto out_unlock;
  2498. }
  2499. di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
  2500. name, name_len, -1);
  2501. if (IS_ERR(di)) {
  2502. err = PTR_ERR(di);
  2503. goto fail;
  2504. }
  2505. if (di) {
  2506. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2507. bytes_del += name_len;
  2508. if (ret) {
  2509. err = ret;
  2510. goto fail;
  2511. }
  2512. }
  2513. btrfs_release_path(path);
  2514. di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
  2515. index, name, name_len, -1);
  2516. if (IS_ERR(di)) {
  2517. err = PTR_ERR(di);
  2518. goto fail;
  2519. }
  2520. if (di) {
  2521. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2522. bytes_del += name_len;
  2523. if (ret) {
  2524. err = ret;
  2525. goto fail;
  2526. }
  2527. }
  2528. /* update the directory size in the log to reflect the names
  2529. * we have removed
  2530. */
  2531. if (bytes_del) {
  2532. struct btrfs_key key;
  2533. key.objectid = dir_ino;
  2534. key.offset = 0;
  2535. key.type = BTRFS_INODE_ITEM_KEY;
  2536. btrfs_release_path(path);
  2537. ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
  2538. if (ret < 0) {
  2539. err = ret;
  2540. goto fail;
  2541. }
  2542. if (ret == 0) {
  2543. struct btrfs_inode_item *item;
  2544. u64 i_size;
  2545. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2546. struct btrfs_inode_item);
  2547. i_size = btrfs_inode_size(path->nodes[0], item);
  2548. if (i_size > bytes_del)
  2549. i_size -= bytes_del;
  2550. else
  2551. i_size = 0;
  2552. btrfs_set_inode_size(path->nodes[0], item, i_size);
  2553. btrfs_mark_buffer_dirty(path->nodes[0]);
  2554. } else
  2555. ret = 0;
  2556. btrfs_release_path(path);
  2557. }
  2558. fail:
  2559. btrfs_free_path(path);
  2560. out_unlock:
  2561. mutex_unlock(&BTRFS_I(dir)->log_mutex);
  2562. if (ret == -ENOSPC) {
  2563. btrfs_set_log_full_commit(root->fs_info, trans);
  2564. ret = 0;
  2565. } else if (ret < 0)
  2566. btrfs_abort_transaction(trans, root, ret);
  2567. btrfs_end_log_trans(root);
  2568. return err;
  2569. }
  2570. /* see comments for btrfs_del_dir_entries_in_log */
  2571. int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
  2572. struct btrfs_root *root,
  2573. const char *name, int name_len,
  2574. struct inode *inode, u64 dirid)
  2575. {
  2576. struct btrfs_root *log;
  2577. u64 index;
  2578. int ret;
  2579. if (BTRFS_I(inode)->logged_trans < trans->transid)
  2580. return 0;
  2581. ret = join_running_log_trans(root);
  2582. if (ret)
  2583. return 0;
  2584. log = root->log_root;
  2585. mutex_lock(&BTRFS_I(inode)->log_mutex);
  2586. ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
  2587. dirid, &index);
  2588. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  2589. if (ret == -ENOSPC) {
  2590. btrfs_set_log_full_commit(root->fs_info, trans);
  2591. ret = 0;
  2592. } else if (ret < 0 && ret != -ENOENT)
  2593. btrfs_abort_transaction(trans, root, ret);
  2594. btrfs_end_log_trans(root);
  2595. return ret;
  2596. }
  2597. /*
  2598. * creates a range item in the log for 'dirid'. first_offset and
  2599. * last_offset tell us which parts of the key space the log should
  2600. * be considered authoritative for.
  2601. */
  2602. static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
  2603. struct btrfs_root *log,
  2604. struct btrfs_path *path,
  2605. int key_type, u64 dirid,
  2606. u64 first_offset, u64 last_offset)
  2607. {
  2608. int ret;
  2609. struct btrfs_key key;
  2610. struct btrfs_dir_log_item *item;
  2611. key.objectid = dirid;
  2612. key.offset = first_offset;
  2613. if (key_type == BTRFS_DIR_ITEM_KEY)
  2614. key.type = BTRFS_DIR_LOG_ITEM_KEY;
  2615. else
  2616. key.type = BTRFS_DIR_LOG_INDEX_KEY;
  2617. ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
  2618. if (ret)
  2619. return ret;
  2620. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2621. struct btrfs_dir_log_item);
  2622. btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
  2623. btrfs_mark_buffer_dirty(path->nodes[0]);
  2624. btrfs_release_path(path);
  2625. return 0;
  2626. }
  2627. /*
  2628. * log all the items included in the current transaction for a given
  2629. * directory. This also creates the range items in the log tree required
  2630. * to replay anything deleted before the fsync
  2631. */
  2632. static noinline int log_dir_items(struct btrfs_trans_handle *trans,
  2633. struct btrfs_root *root, struct inode *inode,
  2634. struct btrfs_path *path,
  2635. struct btrfs_path *dst_path, int key_type,
  2636. u64 min_offset, u64 *last_offset_ret)
  2637. {
  2638. struct btrfs_key min_key;
  2639. struct btrfs_root *log = root->log_root;
  2640. struct extent_buffer *src;
  2641. int err = 0;
  2642. int ret;
  2643. int i;
  2644. int nritems;
  2645. u64 first_offset = min_offset;
  2646. u64 last_offset = (u64)-1;
  2647. u64 ino = btrfs_ino(inode);
  2648. log = root->log_root;
  2649. min_key.objectid = ino;
  2650. min_key.type = key_type;
  2651. min_key.offset = min_offset;
  2652. ret = btrfs_search_forward(root, &min_key, path, trans->transid);
  2653. /*
  2654. * we didn't find anything from this transaction, see if there
  2655. * is anything at all
  2656. */
  2657. if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
  2658. min_key.objectid = ino;
  2659. min_key.type = key_type;
  2660. min_key.offset = (u64)-1;
  2661. btrfs_release_path(path);
  2662. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2663. if (ret < 0) {
  2664. btrfs_release_path(path);
  2665. return ret;
  2666. }
  2667. ret = btrfs_previous_item(root, path, ino, key_type);
  2668. /* if ret == 0 there are items for this type,
  2669. * create a range to tell us the last key of this type.
  2670. * otherwise, there are no items in this directory after
  2671. * *min_offset, and we create a range to indicate that.
  2672. */
  2673. if (ret == 0) {
  2674. struct btrfs_key tmp;
  2675. btrfs_item_key_to_cpu(path->nodes[0], &tmp,
  2676. path->slots[0]);
  2677. if (key_type == tmp.type)
  2678. first_offset = max(min_offset, tmp.offset) + 1;
  2679. }
  2680. goto done;
  2681. }
  2682. /* go backward to find any previous key */
  2683. ret = btrfs_previous_item(root, path, ino, key_type);
  2684. if (ret == 0) {
  2685. struct btrfs_key tmp;
  2686. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  2687. if (key_type == tmp.type) {
  2688. first_offset = tmp.offset;
  2689. ret = overwrite_item(trans, log, dst_path,
  2690. path->nodes[0], path->slots[0],
  2691. &tmp);
  2692. if (ret) {
  2693. err = ret;
  2694. goto done;
  2695. }
  2696. }
  2697. }
  2698. btrfs_release_path(path);
  2699. /* find the first key from this transaction again */
  2700. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2701. if (WARN_ON(ret != 0))
  2702. goto done;
  2703. /*
  2704. * we have a block from this transaction, log every item in it
  2705. * from our directory
  2706. */
  2707. while (1) {
  2708. struct btrfs_key tmp;
  2709. src = path->nodes[0];
  2710. nritems = btrfs_header_nritems(src);
  2711. for (i = path->slots[0]; i < nritems; i++) {
  2712. btrfs_item_key_to_cpu(src, &min_key, i);
  2713. if (min_key.objectid != ino || min_key.type != key_type)
  2714. goto done;
  2715. ret = overwrite_item(trans, log, dst_path, src, i,
  2716. &min_key);
  2717. if (ret) {
  2718. err = ret;
  2719. goto done;
  2720. }
  2721. }
  2722. path->slots[0] = nritems;
  2723. /*
  2724. * look ahead to the next item and see if it is also
  2725. * from this directory and from this transaction
  2726. */
  2727. ret = btrfs_next_leaf(root, path);
  2728. if (ret == 1) {
  2729. last_offset = (u64)-1;
  2730. goto done;
  2731. }
  2732. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  2733. if (tmp.objectid != ino || tmp.type != key_type) {
  2734. last_offset = (u64)-1;
  2735. goto done;
  2736. }
  2737. if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
  2738. ret = overwrite_item(trans, log, dst_path,
  2739. path->nodes[0], path->slots[0],
  2740. &tmp);
  2741. if (ret)
  2742. err = ret;
  2743. else
  2744. last_offset = tmp.offset;
  2745. goto done;
  2746. }
  2747. }
  2748. done:
  2749. btrfs_release_path(path);
  2750. btrfs_release_path(dst_path);
  2751. if (err == 0) {
  2752. *last_offset_ret = last_offset;
  2753. /*
  2754. * insert the log range keys to indicate where the log
  2755. * is valid
  2756. */
  2757. ret = insert_dir_log_key(trans, log, path, key_type,
  2758. ino, first_offset, last_offset);
  2759. if (ret)
  2760. err = ret;
  2761. }
  2762. return err;
  2763. }
  2764. /*
  2765. * logging directories is very similar to logging inodes, We find all the items
  2766. * from the current transaction and write them to the log.
  2767. *
  2768. * The recovery code scans the directory in the subvolume, and if it finds a
  2769. * key in the range logged that is not present in the log tree, then it means
  2770. * that dir entry was unlinked during the transaction.
  2771. *
  2772. * In order for that scan to work, we must include one key smaller than
  2773. * the smallest logged by this transaction and one key larger than the largest
  2774. * key logged by this transaction.
  2775. */
  2776. static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
  2777. struct btrfs_root *root, struct inode *inode,
  2778. struct btrfs_path *path,
  2779. struct btrfs_path *dst_path)
  2780. {
  2781. u64 min_key;
  2782. u64 max_key;
  2783. int ret;
  2784. int key_type = BTRFS_DIR_ITEM_KEY;
  2785. again:
  2786. min_key = 0;
  2787. max_key = 0;
  2788. while (1) {
  2789. ret = log_dir_items(trans, root, inode, path,
  2790. dst_path, key_type, min_key,
  2791. &max_key);
  2792. if (ret)
  2793. return ret;
  2794. if (max_key == (u64)-1)
  2795. break;
  2796. min_key = max_key + 1;
  2797. }
  2798. if (key_type == BTRFS_DIR_ITEM_KEY) {
  2799. key_type = BTRFS_DIR_INDEX_KEY;
  2800. goto again;
  2801. }
  2802. return 0;
  2803. }
  2804. /*
  2805. * a helper function to drop items from the log before we relog an
  2806. * inode. max_key_type indicates the highest item type to remove.
  2807. * This cannot be run for file data extents because it does not
  2808. * free the extents they point to.
  2809. */
  2810. static int drop_objectid_items(struct btrfs_trans_handle *trans,
  2811. struct btrfs_root *log,
  2812. struct btrfs_path *path,
  2813. u64 objectid, int max_key_type)
  2814. {
  2815. int ret;
  2816. struct btrfs_key key;
  2817. struct btrfs_key found_key;
  2818. int start_slot;
  2819. key.objectid = objectid;
  2820. key.type = max_key_type;
  2821. key.offset = (u64)-1;
  2822. while (1) {
  2823. ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
  2824. BUG_ON(ret == 0); /* Logic error */
  2825. if (ret < 0)
  2826. break;
  2827. if (path->slots[0] == 0)
  2828. break;
  2829. path->slots[0]--;
  2830. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2831. path->slots[0]);
  2832. if (found_key.objectid != objectid)
  2833. break;
  2834. found_key.offset = 0;
  2835. found_key.type = 0;
  2836. ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
  2837. &start_slot);
  2838. ret = btrfs_del_items(trans, log, path, start_slot,
  2839. path->slots[0] - start_slot + 1);
  2840. /*
  2841. * If start slot isn't 0 then we don't need to re-search, we've
  2842. * found the last guy with the objectid in this tree.
  2843. */
  2844. if (ret || start_slot != 0)
  2845. break;
  2846. btrfs_release_path(path);
  2847. }
  2848. btrfs_release_path(path);
  2849. if (ret > 0)
  2850. ret = 0;
  2851. return ret;
  2852. }
  2853. static void fill_inode_item(struct btrfs_trans_handle *trans,
  2854. struct extent_buffer *leaf,
  2855. struct btrfs_inode_item *item,
  2856. struct inode *inode, int log_inode_only)
  2857. {
  2858. struct btrfs_map_token token;
  2859. btrfs_init_map_token(&token);
  2860. if (log_inode_only) {
  2861. /* set the generation to zero so the recover code
  2862. * can tell the difference between an logging
  2863. * just to say 'this inode exists' and a logging
  2864. * to say 'update this inode with these values'
  2865. */
  2866. btrfs_set_token_inode_generation(leaf, item, 0, &token);
  2867. btrfs_set_token_inode_size(leaf, item, 0, &token);
  2868. } else {
  2869. btrfs_set_token_inode_generation(leaf, item,
  2870. BTRFS_I(inode)->generation,
  2871. &token);
  2872. btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
  2873. }
  2874. btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
  2875. btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
  2876. btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
  2877. btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
  2878. btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
  2879. inode->i_atime.tv_sec, &token);
  2880. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
  2881. inode->i_atime.tv_nsec, &token);
  2882. btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
  2883. inode->i_mtime.tv_sec, &token);
  2884. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
  2885. inode->i_mtime.tv_nsec, &token);
  2886. btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
  2887. inode->i_ctime.tv_sec, &token);
  2888. btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
  2889. inode->i_ctime.tv_nsec, &token);
  2890. btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
  2891. &token);
  2892. btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
  2893. btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
  2894. btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
  2895. btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
  2896. btrfs_set_token_inode_block_group(leaf, item, 0, &token);
  2897. }
  2898. static int log_inode_item(struct btrfs_trans_handle *trans,
  2899. struct btrfs_root *log, struct btrfs_path *path,
  2900. struct inode *inode)
  2901. {
  2902. struct btrfs_inode_item *inode_item;
  2903. int ret;
  2904. ret = btrfs_insert_empty_item(trans, log, path,
  2905. &BTRFS_I(inode)->location,
  2906. sizeof(*inode_item));
  2907. if (ret && ret != -EEXIST)
  2908. return ret;
  2909. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2910. struct btrfs_inode_item);
  2911. fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
  2912. btrfs_release_path(path);
  2913. return 0;
  2914. }
  2915. static noinline int copy_items(struct btrfs_trans_handle *trans,
  2916. struct inode *inode,
  2917. struct btrfs_path *dst_path,
  2918. struct btrfs_path *src_path, u64 *last_extent,
  2919. int start_slot, int nr, int inode_only)
  2920. {
  2921. unsigned long src_offset;
  2922. unsigned long dst_offset;
  2923. struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
  2924. struct btrfs_file_extent_item *extent;
  2925. struct btrfs_inode_item *inode_item;
  2926. struct extent_buffer *src = src_path->nodes[0];
  2927. struct btrfs_key first_key, last_key, key;
  2928. int ret;
  2929. struct btrfs_key *ins_keys;
  2930. u32 *ins_sizes;
  2931. char *ins_data;
  2932. int i;
  2933. struct list_head ordered_sums;
  2934. int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  2935. bool has_extents = false;
  2936. bool need_find_last_extent = true;
  2937. bool done = false;
  2938. INIT_LIST_HEAD(&ordered_sums);
  2939. ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
  2940. nr * sizeof(u32), GFP_NOFS);
  2941. if (!ins_data)
  2942. return -ENOMEM;
  2943. first_key.objectid = (u64)-1;
  2944. ins_sizes = (u32 *)ins_data;
  2945. ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
  2946. for (i = 0; i < nr; i++) {
  2947. ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
  2948. btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
  2949. }
  2950. ret = btrfs_insert_empty_items(trans, log, dst_path,
  2951. ins_keys, ins_sizes, nr);
  2952. if (ret) {
  2953. kfree(ins_data);
  2954. return ret;
  2955. }
  2956. for (i = 0; i < nr; i++, dst_path->slots[0]++) {
  2957. dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
  2958. dst_path->slots[0]);
  2959. src_offset = btrfs_item_ptr_offset(src, start_slot + i);
  2960. if ((i == (nr - 1)))
  2961. last_key = ins_keys[i];
  2962. if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
  2963. inode_item = btrfs_item_ptr(dst_path->nodes[0],
  2964. dst_path->slots[0],
  2965. struct btrfs_inode_item);
  2966. fill_inode_item(trans, dst_path->nodes[0], inode_item,
  2967. inode, inode_only == LOG_INODE_EXISTS);
  2968. } else {
  2969. copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
  2970. src_offset, ins_sizes[i]);
  2971. }
  2972. /*
  2973. * We set need_find_last_extent here in case we know we were
  2974. * processing other items and then walk into the first extent in
  2975. * the inode. If we don't hit an extent then nothing changes,
  2976. * we'll do the last search the next time around.
  2977. */
  2978. if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
  2979. has_extents = true;
  2980. if (first_key.objectid == (u64)-1)
  2981. first_key = ins_keys[i];
  2982. } else {
  2983. need_find_last_extent = false;
  2984. }
  2985. /* take a reference on file data extents so that truncates
  2986. * or deletes of this inode don't have to relog the inode
  2987. * again
  2988. */
  2989. if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
  2990. !skip_csum) {
  2991. int found_type;
  2992. extent = btrfs_item_ptr(src, start_slot + i,
  2993. struct btrfs_file_extent_item);
  2994. if (btrfs_file_extent_generation(src, extent) < trans->transid)
  2995. continue;
  2996. found_type = btrfs_file_extent_type(src, extent);
  2997. if (found_type == BTRFS_FILE_EXTENT_REG) {
  2998. u64 ds, dl, cs, cl;
  2999. ds = btrfs_file_extent_disk_bytenr(src,
  3000. extent);
  3001. /* ds == 0 is a hole */
  3002. if (ds == 0)
  3003. continue;
  3004. dl = btrfs_file_extent_disk_num_bytes(src,
  3005. extent);
  3006. cs = btrfs_file_extent_offset(src, extent);
  3007. cl = btrfs_file_extent_num_bytes(src,
  3008. extent);
  3009. if (btrfs_file_extent_compression(src,
  3010. extent)) {
  3011. cs = 0;
  3012. cl = dl;
  3013. }
  3014. ret = btrfs_lookup_csums_range(
  3015. log->fs_info->csum_root,
  3016. ds + cs, ds + cs + cl - 1,
  3017. &ordered_sums, 0);
  3018. if (ret) {
  3019. btrfs_release_path(dst_path);
  3020. kfree(ins_data);
  3021. return ret;
  3022. }
  3023. }
  3024. }
  3025. }
  3026. btrfs_mark_buffer_dirty(dst_path->nodes[0]);
  3027. btrfs_release_path(dst_path);
  3028. kfree(ins_data);
  3029. /*
  3030. * we have to do this after the loop above to avoid changing the
  3031. * log tree while trying to change the log tree.
  3032. */
  3033. ret = 0;
  3034. while (!list_empty(&ordered_sums)) {
  3035. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3036. struct btrfs_ordered_sum,
  3037. list);
  3038. if (!ret)
  3039. ret = btrfs_csum_file_blocks(trans, log, sums);
  3040. list_del(&sums->list);
  3041. kfree(sums);
  3042. }
  3043. if (!has_extents)
  3044. return ret;
  3045. if (need_find_last_extent && *last_extent == first_key.offset) {
  3046. /*
  3047. * We don't have any leafs between our current one and the one
  3048. * we processed before that can have file extent items for our
  3049. * inode (and have a generation number smaller than our current
  3050. * transaction id).
  3051. */
  3052. need_find_last_extent = false;
  3053. }
  3054. /*
  3055. * Because we use btrfs_search_forward we could skip leaves that were
  3056. * not modified and then assume *last_extent is valid when it really
  3057. * isn't. So back up to the previous leaf and read the end of the last
  3058. * extent before we go and fill in holes.
  3059. */
  3060. if (need_find_last_extent) {
  3061. u64 len;
  3062. ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
  3063. if (ret < 0)
  3064. return ret;
  3065. if (ret)
  3066. goto fill_holes;
  3067. if (src_path->slots[0])
  3068. src_path->slots[0]--;
  3069. src = src_path->nodes[0];
  3070. btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
  3071. if (key.objectid != btrfs_ino(inode) ||
  3072. key.type != BTRFS_EXTENT_DATA_KEY)
  3073. goto fill_holes;
  3074. extent = btrfs_item_ptr(src, src_path->slots[0],
  3075. struct btrfs_file_extent_item);
  3076. if (btrfs_file_extent_type(src, extent) ==
  3077. BTRFS_FILE_EXTENT_INLINE) {
  3078. len = btrfs_file_extent_inline_len(src,
  3079. src_path->slots[0],
  3080. extent);
  3081. *last_extent = ALIGN(key.offset + len,
  3082. log->sectorsize);
  3083. } else {
  3084. len = btrfs_file_extent_num_bytes(src, extent);
  3085. *last_extent = key.offset + len;
  3086. }
  3087. }
  3088. fill_holes:
  3089. /* So we did prev_leaf, now we need to move to the next leaf, but a few
  3090. * things could have happened
  3091. *
  3092. * 1) A merge could have happened, so we could currently be on a leaf
  3093. * that holds what we were copying in the first place.
  3094. * 2) A split could have happened, and now not all of the items we want
  3095. * are on the same leaf.
  3096. *
  3097. * So we need to adjust how we search for holes, we need to drop the
  3098. * path and re-search for the first extent key we found, and then walk
  3099. * forward until we hit the last one we copied.
  3100. */
  3101. if (need_find_last_extent) {
  3102. /* btrfs_prev_leaf could return 1 without releasing the path */
  3103. btrfs_release_path(src_path);
  3104. ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
  3105. src_path, 0, 0);
  3106. if (ret < 0)
  3107. return ret;
  3108. ASSERT(ret == 0);
  3109. src = src_path->nodes[0];
  3110. i = src_path->slots[0];
  3111. } else {
  3112. i = start_slot;
  3113. }
  3114. /*
  3115. * Ok so here we need to go through and fill in any holes we may have
  3116. * to make sure that holes are punched for those areas in case they had
  3117. * extents previously.
  3118. */
  3119. while (!done) {
  3120. u64 offset, len;
  3121. u64 extent_end;
  3122. if (i >= btrfs_header_nritems(src_path->nodes[0])) {
  3123. ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
  3124. if (ret < 0)
  3125. return ret;
  3126. ASSERT(ret == 0);
  3127. src = src_path->nodes[0];
  3128. i = 0;
  3129. }
  3130. btrfs_item_key_to_cpu(src, &key, i);
  3131. if (!btrfs_comp_cpu_keys(&key, &last_key))
  3132. done = true;
  3133. if (key.objectid != btrfs_ino(inode) ||
  3134. key.type != BTRFS_EXTENT_DATA_KEY) {
  3135. i++;
  3136. continue;
  3137. }
  3138. extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
  3139. if (btrfs_file_extent_type(src, extent) ==
  3140. BTRFS_FILE_EXTENT_INLINE) {
  3141. len = btrfs_file_extent_inline_len(src, i, extent);
  3142. extent_end = ALIGN(key.offset + len, log->sectorsize);
  3143. } else {
  3144. len = btrfs_file_extent_num_bytes(src, extent);
  3145. extent_end = key.offset + len;
  3146. }
  3147. i++;
  3148. if (*last_extent == key.offset) {
  3149. *last_extent = extent_end;
  3150. continue;
  3151. }
  3152. offset = *last_extent;
  3153. len = key.offset - *last_extent;
  3154. ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
  3155. offset, 0, 0, len, 0, len, 0,
  3156. 0, 0);
  3157. if (ret)
  3158. break;
  3159. *last_extent = extent_end;
  3160. }
  3161. /*
  3162. * Need to let the callers know we dropped the path so they should
  3163. * re-search.
  3164. */
  3165. if (!ret && need_find_last_extent)
  3166. ret = 1;
  3167. return ret;
  3168. }
  3169. static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
  3170. {
  3171. struct extent_map *em1, *em2;
  3172. em1 = list_entry(a, struct extent_map, list);
  3173. em2 = list_entry(b, struct extent_map, list);
  3174. if (em1->start < em2->start)
  3175. return -1;
  3176. else if (em1->start > em2->start)
  3177. return 1;
  3178. return 0;
  3179. }
  3180. static int wait_ordered_extents(struct btrfs_trans_handle *trans,
  3181. struct inode *inode,
  3182. struct btrfs_root *root,
  3183. const struct extent_map *em,
  3184. const struct list_head *logged_list,
  3185. bool *ordered_io_error)
  3186. {
  3187. struct btrfs_ordered_extent *ordered;
  3188. struct btrfs_root *log = root->log_root;
  3189. u64 mod_start = em->mod_start;
  3190. u64 mod_len = em->mod_len;
  3191. const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  3192. u64 csum_offset;
  3193. u64 csum_len;
  3194. LIST_HEAD(ordered_sums);
  3195. int ret = 0;
  3196. *ordered_io_error = false;
  3197. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  3198. em->block_start == EXTENT_MAP_HOLE)
  3199. return 0;
  3200. /*
  3201. * Wait far any ordered extent that covers our extent map. If it
  3202. * finishes without an error, first check and see if our csums are on
  3203. * our outstanding ordered extents.
  3204. */
  3205. list_for_each_entry(ordered, logged_list, log_list) {
  3206. struct btrfs_ordered_sum *sum;
  3207. if (!mod_len)
  3208. break;
  3209. if (ordered->file_offset + ordered->len <= mod_start ||
  3210. mod_start + mod_len <= ordered->file_offset)
  3211. continue;
  3212. if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
  3213. !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
  3214. !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
  3215. const u64 start = ordered->file_offset;
  3216. const u64 end = ordered->file_offset + ordered->len - 1;
  3217. WARN_ON(ordered->inode != inode);
  3218. filemap_fdatawrite_range(inode->i_mapping, start, end);
  3219. }
  3220. wait_event(ordered->wait,
  3221. (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
  3222. test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
  3223. if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
  3224. /*
  3225. * Clear the AS_EIO/AS_ENOSPC flags from the inode's
  3226. * i_mapping flags, so that the next fsync won't get
  3227. * an outdated io error too.
  3228. */
  3229. btrfs_inode_check_errors(inode);
  3230. *ordered_io_error = true;
  3231. break;
  3232. }
  3233. /*
  3234. * We are going to copy all the csums on this ordered extent, so
  3235. * go ahead and adjust mod_start and mod_len in case this
  3236. * ordered extent has already been logged.
  3237. */
  3238. if (ordered->file_offset > mod_start) {
  3239. if (ordered->file_offset + ordered->len >=
  3240. mod_start + mod_len)
  3241. mod_len = ordered->file_offset - mod_start;
  3242. /*
  3243. * If we have this case
  3244. *
  3245. * |--------- logged extent ---------|
  3246. * |----- ordered extent ----|
  3247. *
  3248. * Just don't mess with mod_start and mod_len, we'll
  3249. * just end up logging more csums than we need and it
  3250. * will be ok.
  3251. */
  3252. } else {
  3253. if (ordered->file_offset + ordered->len <
  3254. mod_start + mod_len) {
  3255. mod_len = (mod_start + mod_len) -
  3256. (ordered->file_offset + ordered->len);
  3257. mod_start = ordered->file_offset +
  3258. ordered->len;
  3259. } else {
  3260. mod_len = 0;
  3261. }
  3262. }
  3263. if (skip_csum)
  3264. continue;
  3265. /*
  3266. * To keep us from looping for the above case of an ordered
  3267. * extent that falls inside of the logged extent.
  3268. */
  3269. if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
  3270. &ordered->flags))
  3271. continue;
  3272. if (ordered->csum_bytes_left) {
  3273. btrfs_start_ordered_extent(inode, ordered, 0);
  3274. wait_event(ordered->wait,
  3275. ordered->csum_bytes_left == 0);
  3276. }
  3277. list_for_each_entry(sum, &ordered->list, list) {
  3278. ret = btrfs_csum_file_blocks(trans, log, sum);
  3279. if (ret)
  3280. break;
  3281. }
  3282. }
  3283. if (*ordered_io_error || !mod_len || ret || skip_csum)
  3284. return ret;
  3285. if (em->compress_type) {
  3286. csum_offset = 0;
  3287. csum_len = max(em->block_len, em->orig_block_len);
  3288. } else {
  3289. csum_offset = mod_start - em->start;
  3290. csum_len = mod_len;
  3291. }
  3292. /* block start is already adjusted for the file extent offset. */
  3293. ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
  3294. em->block_start + csum_offset,
  3295. em->block_start + csum_offset +
  3296. csum_len - 1, &ordered_sums, 0);
  3297. if (ret)
  3298. return ret;
  3299. while (!list_empty(&ordered_sums)) {
  3300. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3301. struct btrfs_ordered_sum,
  3302. list);
  3303. if (!ret)
  3304. ret = btrfs_csum_file_blocks(trans, log, sums);
  3305. list_del(&sums->list);
  3306. kfree(sums);
  3307. }
  3308. return ret;
  3309. }
  3310. static int log_one_extent(struct btrfs_trans_handle *trans,
  3311. struct inode *inode, struct btrfs_root *root,
  3312. const struct extent_map *em,
  3313. struct btrfs_path *path,
  3314. const struct list_head *logged_list,
  3315. struct btrfs_log_ctx *ctx)
  3316. {
  3317. struct btrfs_root *log = root->log_root;
  3318. struct btrfs_file_extent_item *fi;
  3319. struct extent_buffer *leaf;
  3320. struct btrfs_map_token token;
  3321. struct btrfs_key key;
  3322. u64 extent_offset = em->start - em->orig_start;
  3323. u64 block_len;
  3324. int ret;
  3325. int extent_inserted = 0;
  3326. bool ordered_io_err = false;
  3327. ret = wait_ordered_extents(trans, inode, root, em, logged_list,
  3328. &ordered_io_err);
  3329. if (ret)
  3330. return ret;
  3331. if (ordered_io_err) {
  3332. ctx->io_err = -EIO;
  3333. return 0;
  3334. }
  3335. btrfs_init_map_token(&token);
  3336. ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
  3337. em->start + em->len, NULL, 0, 1,
  3338. sizeof(*fi), &extent_inserted);
  3339. if (ret)
  3340. return ret;
  3341. if (!extent_inserted) {
  3342. key.objectid = btrfs_ino(inode);
  3343. key.type = BTRFS_EXTENT_DATA_KEY;
  3344. key.offset = em->start;
  3345. ret = btrfs_insert_empty_item(trans, log, path, &key,
  3346. sizeof(*fi));
  3347. if (ret)
  3348. return ret;
  3349. }
  3350. leaf = path->nodes[0];
  3351. fi = btrfs_item_ptr(leaf, path->slots[0],
  3352. struct btrfs_file_extent_item);
  3353. btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
  3354. &token);
  3355. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  3356. btrfs_set_token_file_extent_type(leaf, fi,
  3357. BTRFS_FILE_EXTENT_PREALLOC,
  3358. &token);
  3359. else
  3360. btrfs_set_token_file_extent_type(leaf, fi,
  3361. BTRFS_FILE_EXTENT_REG,
  3362. &token);
  3363. block_len = max(em->block_len, em->orig_block_len);
  3364. if (em->compress_type != BTRFS_COMPRESS_NONE) {
  3365. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3366. em->block_start,
  3367. &token);
  3368. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3369. &token);
  3370. } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
  3371. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3372. em->block_start -
  3373. extent_offset, &token);
  3374. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3375. &token);
  3376. } else {
  3377. btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
  3378. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
  3379. &token);
  3380. }
  3381. btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
  3382. btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
  3383. btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
  3384. btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
  3385. &token);
  3386. btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
  3387. btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
  3388. btrfs_mark_buffer_dirty(leaf);
  3389. btrfs_release_path(path);
  3390. return ret;
  3391. }
  3392. static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
  3393. struct btrfs_root *root,
  3394. struct inode *inode,
  3395. struct btrfs_path *path,
  3396. struct list_head *logged_list,
  3397. struct btrfs_log_ctx *ctx)
  3398. {
  3399. struct extent_map *em, *n;
  3400. struct list_head extents;
  3401. struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
  3402. u64 test_gen;
  3403. int ret = 0;
  3404. int num = 0;
  3405. INIT_LIST_HEAD(&extents);
  3406. write_lock(&tree->lock);
  3407. test_gen = root->fs_info->last_trans_committed;
  3408. list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
  3409. list_del_init(&em->list);
  3410. /*
  3411. * Just an arbitrary number, this can be really CPU intensive
  3412. * once we start getting a lot of extents, and really once we
  3413. * have a bunch of extents we just want to commit since it will
  3414. * be faster.
  3415. */
  3416. if (++num > 32768) {
  3417. list_del_init(&tree->modified_extents);
  3418. ret = -EFBIG;
  3419. goto process;
  3420. }
  3421. if (em->generation <= test_gen)
  3422. continue;
  3423. /* Need a ref to keep it from getting evicted from cache */
  3424. atomic_inc(&em->refs);
  3425. set_bit(EXTENT_FLAG_LOGGING, &em->flags);
  3426. list_add_tail(&em->list, &extents);
  3427. num++;
  3428. }
  3429. list_sort(NULL, &extents, extent_cmp);
  3430. process:
  3431. while (!list_empty(&extents)) {
  3432. em = list_entry(extents.next, struct extent_map, list);
  3433. list_del_init(&em->list);
  3434. /*
  3435. * If we had an error we just need to delete everybody from our
  3436. * private list.
  3437. */
  3438. if (ret) {
  3439. clear_em_logging(tree, em);
  3440. free_extent_map(em);
  3441. continue;
  3442. }
  3443. write_unlock(&tree->lock);
  3444. ret = log_one_extent(trans, inode, root, em, path, logged_list,
  3445. ctx);
  3446. write_lock(&tree->lock);
  3447. clear_em_logging(tree, em);
  3448. free_extent_map(em);
  3449. }
  3450. WARN_ON(!list_empty(&extents));
  3451. write_unlock(&tree->lock);
  3452. btrfs_release_path(path);
  3453. return ret;
  3454. }
  3455. /* log a single inode in the tree log.
  3456. * At least one parent directory for this inode must exist in the tree
  3457. * or be logged already.
  3458. *
  3459. * Any items from this inode changed by the current transaction are copied
  3460. * to the log tree. An extra reference is taken on any extents in this
  3461. * file, allowing us to avoid a whole pile of corner cases around logging
  3462. * blocks that have been removed from the tree.
  3463. *
  3464. * See LOG_INODE_ALL and related defines for a description of what inode_only
  3465. * does.
  3466. *
  3467. * This handles both files and directories.
  3468. */
  3469. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  3470. struct btrfs_root *root, struct inode *inode,
  3471. int inode_only,
  3472. const loff_t start,
  3473. const loff_t end,
  3474. struct btrfs_log_ctx *ctx)
  3475. {
  3476. struct btrfs_path *path;
  3477. struct btrfs_path *dst_path;
  3478. struct btrfs_key min_key;
  3479. struct btrfs_key max_key;
  3480. struct btrfs_root *log = root->log_root;
  3481. struct extent_buffer *src = NULL;
  3482. LIST_HEAD(logged_list);
  3483. u64 last_extent = 0;
  3484. int err = 0;
  3485. int ret;
  3486. int nritems;
  3487. int ins_start_slot = 0;
  3488. int ins_nr;
  3489. bool fast_search = false;
  3490. u64 ino = btrfs_ino(inode);
  3491. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  3492. path = btrfs_alloc_path();
  3493. if (!path)
  3494. return -ENOMEM;
  3495. dst_path = btrfs_alloc_path();
  3496. if (!dst_path) {
  3497. btrfs_free_path(path);
  3498. return -ENOMEM;
  3499. }
  3500. min_key.objectid = ino;
  3501. min_key.type = BTRFS_INODE_ITEM_KEY;
  3502. min_key.offset = 0;
  3503. max_key.objectid = ino;
  3504. /* today the code can only do partial logging of directories */
  3505. if (S_ISDIR(inode->i_mode) ||
  3506. (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3507. &BTRFS_I(inode)->runtime_flags) &&
  3508. inode_only == LOG_INODE_EXISTS))
  3509. max_key.type = BTRFS_XATTR_ITEM_KEY;
  3510. else
  3511. max_key.type = (u8)-1;
  3512. max_key.offset = (u64)-1;
  3513. /* Only run delayed items if we are a dir or a new file */
  3514. if (S_ISDIR(inode->i_mode) ||
  3515. BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
  3516. ret = btrfs_commit_inode_delayed_items(trans, inode);
  3517. if (ret) {
  3518. btrfs_free_path(path);
  3519. btrfs_free_path(dst_path);
  3520. return ret;
  3521. }
  3522. }
  3523. mutex_lock(&BTRFS_I(inode)->log_mutex);
  3524. btrfs_get_logged_extents(inode, &logged_list, start, end);
  3525. /*
  3526. * a brute force approach to making sure we get the most uptodate
  3527. * copies of everything.
  3528. */
  3529. if (S_ISDIR(inode->i_mode)) {
  3530. int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
  3531. if (inode_only == LOG_INODE_EXISTS)
  3532. max_key_type = BTRFS_XATTR_ITEM_KEY;
  3533. ret = drop_objectid_items(trans, log, path, ino, max_key_type);
  3534. } else {
  3535. if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3536. &BTRFS_I(inode)->runtime_flags)) {
  3537. clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  3538. &BTRFS_I(inode)->runtime_flags);
  3539. ret = btrfs_truncate_inode_items(trans, log,
  3540. inode, 0, 0);
  3541. } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  3542. &BTRFS_I(inode)->runtime_flags) ||
  3543. inode_only == LOG_INODE_EXISTS) {
  3544. if (inode_only == LOG_INODE_ALL)
  3545. fast_search = true;
  3546. max_key.type = BTRFS_XATTR_ITEM_KEY;
  3547. ret = drop_objectid_items(trans, log, path, ino,
  3548. max_key.type);
  3549. } else {
  3550. if (inode_only == LOG_INODE_ALL)
  3551. fast_search = true;
  3552. ret = log_inode_item(trans, log, dst_path, inode);
  3553. if (ret) {
  3554. err = ret;
  3555. goto out_unlock;
  3556. }
  3557. goto log_extents;
  3558. }
  3559. }
  3560. if (ret) {
  3561. err = ret;
  3562. goto out_unlock;
  3563. }
  3564. while (1) {
  3565. ins_nr = 0;
  3566. ret = btrfs_search_forward(root, &min_key,
  3567. path, trans->transid);
  3568. if (ret != 0)
  3569. break;
  3570. again:
  3571. /* note, ins_nr might be > 0 here, cleanup outside the loop */
  3572. if (min_key.objectid != ino)
  3573. break;
  3574. if (min_key.type > max_key.type)
  3575. break;
  3576. src = path->nodes[0];
  3577. if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
  3578. ins_nr++;
  3579. goto next_slot;
  3580. } else if (!ins_nr) {
  3581. ins_start_slot = path->slots[0];
  3582. ins_nr = 1;
  3583. goto next_slot;
  3584. }
  3585. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  3586. ins_start_slot, ins_nr, inode_only);
  3587. if (ret < 0) {
  3588. err = ret;
  3589. goto out_unlock;
  3590. }
  3591. if (ret) {
  3592. ins_nr = 0;
  3593. btrfs_release_path(path);
  3594. continue;
  3595. }
  3596. ins_nr = 1;
  3597. ins_start_slot = path->slots[0];
  3598. next_slot:
  3599. nritems = btrfs_header_nritems(path->nodes[0]);
  3600. path->slots[0]++;
  3601. if (path->slots[0] < nritems) {
  3602. btrfs_item_key_to_cpu(path->nodes[0], &min_key,
  3603. path->slots[0]);
  3604. goto again;
  3605. }
  3606. if (ins_nr) {
  3607. ret = copy_items(trans, inode, dst_path, path,
  3608. &last_extent, ins_start_slot,
  3609. ins_nr, inode_only);
  3610. if (ret < 0) {
  3611. err = ret;
  3612. goto out_unlock;
  3613. }
  3614. ret = 0;
  3615. ins_nr = 0;
  3616. }
  3617. btrfs_release_path(path);
  3618. if (min_key.offset < (u64)-1) {
  3619. min_key.offset++;
  3620. } else if (min_key.type < max_key.type) {
  3621. min_key.type++;
  3622. min_key.offset = 0;
  3623. } else {
  3624. break;
  3625. }
  3626. }
  3627. if (ins_nr) {
  3628. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  3629. ins_start_slot, ins_nr, inode_only);
  3630. if (ret < 0) {
  3631. err = ret;
  3632. goto out_unlock;
  3633. }
  3634. ret = 0;
  3635. ins_nr = 0;
  3636. }
  3637. log_extents:
  3638. btrfs_release_path(path);
  3639. btrfs_release_path(dst_path);
  3640. if (fast_search) {
  3641. /*
  3642. * Some ordered extents started by fsync might have completed
  3643. * before we collected the ordered extents in logged_list, which
  3644. * means they're gone, not in our logged_list nor in the inode's
  3645. * ordered tree. We want the application/user space to know an
  3646. * error happened while attempting to persist file data so that
  3647. * it can take proper action. If such error happened, we leave
  3648. * without writing to the log tree and the fsync must report the
  3649. * file data write error and not commit the current transaction.
  3650. */
  3651. err = btrfs_inode_check_errors(inode);
  3652. if (err) {
  3653. ctx->io_err = err;
  3654. goto out_unlock;
  3655. }
  3656. ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
  3657. &logged_list, ctx);
  3658. if (ret) {
  3659. err = ret;
  3660. goto out_unlock;
  3661. }
  3662. } else if (inode_only == LOG_INODE_ALL) {
  3663. struct extent_map *em, *n;
  3664. write_lock(&em_tree->lock);
  3665. /*
  3666. * We can't just remove every em if we're called for a ranged
  3667. * fsync - that is, one that doesn't cover the whole possible
  3668. * file range (0 to LLONG_MAX). This is because we can have
  3669. * em's that fall outside the range we're logging and therefore
  3670. * their ordered operations haven't completed yet
  3671. * (btrfs_finish_ordered_io() not invoked yet). This means we
  3672. * didn't get their respective file extent item in the fs/subvol
  3673. * tree yet, and need to let the next fast fsync (one which
  3674. * consults the list of modified extent maps) find the em so
  3675. * that it logs a matching file extent item and waits for the
  3676. * respective ordered operation to complete (if it's still
  3677. * running).
  3678. *
  3679. * Removing every em outside the range we're logging would make
  3680. * the next fast fsync not log their matching file extent items,
  3681. * therefore making us lose data after a log replay.
  3682. */
  3683. list_for_each_entry_safe(em, n, &em_tree->modified_extents,
  3684. list) {
  3685. const u64 mod_end = em->mod_start + em->mod_len - 1;
  3686. if (em->mod_start >= start && mod_end <= end)
  3687. list_del_init(&em->list);
  3688. }
  3689. write_unlock(&em_tree->lock);
  3690. }
  3691. if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
  3692. ret = log_directory_changes(trans, root, inode, path, dst_path);
  3693. if (ret) {
  3694. err = ret;
  3695. goto out_unlock;
  3696. }
  3697. }
  3698. BTRFS_I(inode)->logged_trans = trans->transid;
  3699. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
  3700. out_unlock:
  3701. if (unlikely(err))
  3702. btrfs_put_logged_extents(&logged_list);
  3703. else
  3704. btrfs_submit_logged_extents(&logged_list, log);
  3705. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  3706. btrfs_free_path(path);
  3707. btrfs_free_path(dst_path);
  3708. return err;
  3709. }
  3710. /*
  3711. * follow the dentry parent pointers up the chain and see if any
  3712. * of the directories in it require a full commit before they can
  3713. * be logged. Returns zero if nothing special needs to be done or 1 if
  3714. * a full commit is required.
  3715. */
  3716. static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
  3717. struct inode *inode,
  3718. struct dentry *parent,
  3719. struct super_block *sb,
  3720. u64 last_committed)
  3721. {
  3722. int ret = 0;
  3723. struct btrfs_root *root;
  3724. struct dentry *old_parent = NULL;
  3725. struct inode *orig_inode = inode;
  3726. /*
  3727. * for regular files, if its inode is already on disk, we don't
  3728. * have to worry about the parents at all. This is because
  3729. * we can use the last_unlink_trans field to record renames
  3730. * and other fun in this file.
  3731. */
  3732. if (S_ISREG(inode->i_mode) &&
  3733. BTRFS_I(inode)->generation <= last_committed &&
  3734. BTRFS_I(inode)->last_unlink_trans <= last_committed)
  3735. goto out;
  3736. if (!S_ISDIR(inode->i_mode)) {
  3737. if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
  3738. goto out;
  3739. inode = parent->d_inode;
  3740. }
  3741. while (1) {
  3742. /*
  3743. * If we are logging a directory then we start with our inode,
  3744. * not our parents inode, so we need to skipp setting the
  3745. * logged_trans so that further down in the log code we don't
  3746. * think this inode has already been logged.
  3747. */
  3748. if (inode != orig_inode)
  3749. BTRFS_I(inode)->logged_trans = trans->transid;
  3750. smp_mb();
  3751. if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
  3752. root = BTRFS_I(inode)->root;
  3753. /*
  3754. * make sure any commits to the log are forced
  3755. * to be full commits
  3756. */
  3757. btrfs_set_log_full_commit(root->fs_info, trans);
  3758. ret = 1;
  3759. break;
  3760. }
  3761. if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
  3762. break;
  3763. if (IS_ROOT(parent))
  3764. break;
  3765. parent = dget_parent(parent);
  3766. dput(old_parent);
  3767. old_parent = parent;
  3768. inode = parent->d_inode;
  3769. }
  3770. dput(old_parent);
  3771. out:
  3772. return ret;
  3773. }
  3774. /*
  3775. * helper function around btrfs_log_inode to make sure newly created
  3776. * parent directories also end up in the log. A minimal inode and backref
  3777. * only logging is done of any parent directories that are older than
  3778. * the last committed transaction
  3779. */
  3780. static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
  3781. struct btrfs_root *root, struct inode *inode,
  3782. struct dentry *parent,
  3783. const loff_t start,
  3784. const loff_t end,
  3785. int exists_only,
  3786. struct btrfs_log_ctx *ctx)
  3787. {
  3788. int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
  3789. struct super_block *sb;
  3790. struct dentry *old_parent = NULL;
  3791. int ret = 0;
  3792. u64 last_committed = root->fs_info->last_trans_committed;
  3793. const struct dentry * const first_parent = parent;
  3794. const bool did_unlink = (BTRFS_I(inode)->last_unlink_trans >
  3795. last_committed);
  3796. sb = inode->i_sb;
  3797. if (btrfs_test_opt(root, NOTREELOG)) {
  3798. ret = 1;
  3799. goto end_no_trans;
  3800. }
  3801. /*
  3802. * The prev transaction commit doesn't complete, we need do
  3803. * full commit by ourselves.
  3804. */
  3805. if (root->fs_info->last_trans_log_full_commit >
  3806. root->fs_info->last_trans_committed) {
  3807. ret = 1;
  3808. goto end_no_trans;
  3809. }
  3810. if (root != BTRFS_I(inode)->root ||
  3811. btrfs_root_refs(&root->root_item) == 0) {
  3812. ret = 1;
  3813. goto end_no_trans;
  3814. }
  3815. ret = check_parent_dirs_for_sync(trans, inode, parent,
  3816. sb, last_committed);
  3817. if (ret)
  3818. goto end_no_trans;
  3819. if (btrfs_inode_in_log(inode, trans->transid)) {
  3820. ret = BTRFS_NO_LOG_SYNC;
  3821. goto end_no_trans;
  3822. }
  3823. ret = start_log_trans(trans, root, ctx);
  3824. if (ret)
  3825. goto end_no_trans;
  3826. ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
  3827. if (ret)
  3828. goto end_trans;
  3829. /*
  3830. * for regular files, if its inode is already on disk, we don't
  3831. * have to worry about the parents at all. This is because
  3832. * we can use the last_unlink_trans field to record renames
  3833. * and other fun in this file.
  3834. */
  3835. if (S_ISREG(inode->i_mode) &&
  3836. BTRFS_I(inode)->generation <= last_committed &&
  3837. BTRFS_I(inode)->last_unlink_trans <= last_committed) {
  3838. ret = 0;
  3839. goto end_trans;
  3840. }
  3841. while (1) {
  3842. if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
  3843. break;
  3844. inode = parent->d_inode;
  3845. if (root != BTRFS_I(inode)->root)
  3846. break;
  3847. /*
  3848. * On unlink we must make sure our immediate parent directory
  3849. * inode is fully logged. This is to prevent leaving dangling
  3850. * directory index entries and a wrong directory inode's i_size.
  3851. * Not doing so can result in a directory being impossible to
  3852. * delete after log replay (rmdir will always fail with error
  3853. * -ENOTEMPTY).
  3854. */
  3855. if (did_unlink && parent == first_parent)
  3856. inode_only = LOG_INODE_ALL;
  3857. else
  3858. inode_only = LOG_INODE_EXISTS;
  3859. if (BTRFS_I(inode)->generation >
  3860. root->fs_info->last_trans_committed ||
  3861. inode_only == LOG_INODE_ALL) {
  3862. ret = btrfs_log_inode(trans, root, inode, inode_only,
  3863. 0, LLONG_MAX, ctx);
  3864. if (ret)
  3865. goto end_trans;
  3866. }
  3867. if (IS_ROOT(parent))
  3868. break;
  3869. parent = dget_parent(parent);
  3870. dput(old_parent);
  3871. old_parent = parent;
  3872. }
  3873. ret = 0;
  3874. end_trans:
  3875. dput(old_parent);
  3876. if (ret < 0) {
  3877. btrfs_set_log_full_commit(root->fs_info, trans);
  3878. ret = 1;
  3879. }
  3880. if (ret)
  3881. btrfs_remove_log_ctx(root, ctx);
  3882. btrfs_end_log_trans(root);
  3883. end_no_trans:
  3884. return ret;
  3885. }
  3886. /*
  3887. * it is not safe to log dentry if the chunk root has added new
  3888. * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
  3889. * If this returns 1, you must commit the transaction to safely get your
  3890. * data on disk.
  3891. */
  3892. int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
  3893. struct btrfs_root *root, struct dentry *dentry,
  3894. const loff_t start,
  3895. const loff_t end,
  3896. struct btrfs_log_ctx *ctx)
  3897. {
  3898. struct dentry *parent = dget_parent(dentry);
  3899. int ret;
  3900. ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent,
  3901. start, end, 0, ctx);
  3902. dput(parent);
  3903. return ret;
  3904. }
  3905. /*
  3906. * should be called during mount to recover any replay any log trees
  3907. * from the FS
  3908. */
  3909. int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
  3910. {
  3911. int ret;
  3912. struct btrfs_path *path;
  3913. struct btrfs_trans_handle *trans;
  3914. struct btrfs_key key;
  3915. struct btrfs_key found_key;
  3916. struct btrfs_key tmp_key;
  3917. struct btrfs_root *log;
  3918. struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
  3919. struct walk_control wc = {
  3920. .process_func = process_one_buffer,
  3921. .stage = 0,
  3922. };
  3923. path = btrfs_alloc_path();
  3924. if (!path)
  3925. return -ENOMEM;
  3926. fs_info->log_root_recovering = 1;
  3927. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  3928. if (IS_ERR(trans)) {
  3929. ret = PTR_ERR(trans);
  3930. goto error;
  3931. }
  3932. wc.trans = trans;
  3933. wc.pin = 1;
  3934. ret = walk_log_tree(trans, log_root_tree, &wc);
  3935. if (ret) {
  3936. btrfs_error(fs_info, ret, "Failed to pin buffers while "
  3937. "recovering log root tree.");
  3938. goto error;
  3939. }
  3940. again:
  3941. key.objectid = BTRFS_TREE_LOG_OBJECTID;
  3942. key.offset = (u64)-1;
  3943. key.type = BTRFS_ROOT_ITEM_KEY;
  3944. while (1) {
  3945. ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
  3946. if (ret < 0) {
  3947. btrfs_error(fs_info, ret,
  3948. "Couldn't find tree log root.");
  3949. goto error;
  3950. }
  3951. if (ret > 0) {
  3952. if (path->slots[0] == 0)
  3953. break;
  3954. path->slots[0]--;
  3955. }
  3956. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  3957. path->slots[0]);
  3958. btrfs_release_path(path);
  3959. if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  3960. break;
  3961. log = btrfs_read_fs_root(log_root_tree, &found_key);
  3962. if (IS_ERR(log)) {
  3963. ret = PTR_ERR(log);
  3964. btrfs_error(fs_info, ret,
  3965. "Couldn't read tree log root.");
  3966. goto error;
  3967. }
  3968. tmp_key.objectid = found_key.offset;
  3969. tmp_key.type = BTRFS_ROOT_ITEM_KEY;
  3970. tmp_key.offset = (u64)-1;
  3971. wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
  3972. if (IS_ERR(wc.replay_dest)) {
  3973. ret = PTR_ERR(wc.replay_dest);
  3974. free_extent_buffer(log->node);
  3975. free_extent_buffer(log->commit_root);
  3976. kfree(log);
  3977. btrfs_error(fs_info, ret, "Couldn't read target root "
  3978. "for tree log recovery.");
  3979. goto error;
  3980. }
  3981. wc.replay_dest->log_root = log;
  3982. btrfs_record_root_in_trans(trans, wc.replay_dest);
  3983. ret = walk_log_tree(trans, log, &wc);
  3984. if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
  3985. ret = fixup_inode_link_counts(trans, wc.replay_dest,
  3986. path);
  3987. }
  3988. key.offset = found_key.offset - 1;
  3989. wc.replay_dest->log_root = NULL;
  3990. free_extent_buffer(log->node);
  3991. free_extent_buffer(log->commit_root);
  3992. kfree(log);
  3993. if (ret)
  3994. goto error;
  3995. if (found_key.offset == 0)
  3996. break;
  3997. }
  3998. btrfs_release_path(path);
  3999. /* step one is to pin it all, step two is to replay just inodes */
  4000. if (wc.pin) {
  4001. wc.pin = 0;
  4002. wc.process_func = replay_one_buffer;
  4003. wc.stage = LOG_WALK_REPLAY_INODES;
  4004. goto again;
  4005. }
  4006. /* step three is to replay everything */
  4007. if (wc.stage < LOG_WALK_REPLAY_ALL) {
  4008. wc.stage++;
  4009. goto again;
  4010. }
  4011. btrfs_free_path(path);
  4012. /* step 4: commit the transaction, which also unpins the blocks */
  4013. ret = btrfs_commit_transaction(trans, fs_info->tree_root);
  4014. if (ret)
  4015. return ret;
  4016. free_extent_buffer(log_root_tree->node);
  4017. log_root_tree->log_root = NULL;
  4018. fs_info->log_root_recovering = 0;
  4019. kfree(log_root_tree);
  4020. return 0;
  4021. error:
  4022. if (wc.trans)
  4023. btrfs_end_transaction(wc.trans, fs_info->tree_root);
  4024. btrfs_free_path(path);
  4025. return ret;
  4026. }
  4027. /*
  4028. * there are some corner cases where we want to force a full
  4029. * commit instead of allowing a directory to be logged.
  4030. *
  4031. * They revolve around files there were unlinked from the directory, and
  4032. * this function updates the parent directory so that a full commit is
  4033. * properly done if it is fsync'd later after the unlinks are done.
  4034. */
  4035. void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
  4036. struct inode *dir, struct inode *inode,
  4037. int for_rename)
  4038. {
  4039. /*
  4040. * when we're logging a file, if it hasn't been renamed
  4041. * or unlinked, and its inode is fully committed on disk,
  4042. * we don't have to worry about walking up the directory chain
  4043. * to log its parents.
  4044. *
  4045. * So, we use the last_unlink_trans field to put this transid
  4046. * into the file. When the file is logged we check it and
  4047. * don't log the parents if the file is fully on disk.
  4048. */
  4049. if (S_ISREG(inode->i_mode))
  4050. BTRFS_I(inode)->last_unlink_trans = trans->transid;
  4051. /*
  4052. * if this directory was already logged any new
  4053. * names for this file/dir will get recorded
  4054. */
  4055. smp_mb();
  4056. if (BTRFS_I(dir)->logged_trans == trans->transid)
  4057. return;
  4058. /*
  4059. * if the inode we're about to unlink was logged,
  4060. * the log will be properly updated for any new names
  4061. */
  4062. if (BTRFS_I(inode)->logged_trans == trans->transid)
  4063. return;
  4064. /*
  4065. * when renaming files across directories, if the directory
  4066. * there we're unlinking from gets fsync'd later on, there's
  4067. * no way to find the destination directory later and fsync it
  4068. * properly. So, we have to be conservative and force commits
  4069. * so the new name gets discovered.
  4070. */
  4071. if (for_rename)
  4072. goto record;
  4073. /* we can safely do the unlink without any special recording */
  4074. return;
  4075. record:
  4076. BTRFS_I(dir)->last_unlink_trans = trans->transid;
  4077. }
  4078. /*
  4079. * Call this after adding a new name for a file and it will properly
  4080. * update the log to reflect the new name.
  4081. *
  4082. * It will return zero if all goes well, and it will return 1 if a
  4083. * full transaction commit is required.
  4084. */
  4085. int btrfs_log_new_name(struct btrfs_trans_handle *trans,
  4086. struct inode *inode, struct inode *old_dir,
  4087. struct dentry *parent)
  4088. {
  4089. struct btrfs_root * root = BTRFS_I(inode)->root;
  4090. /*
  4091. * this will force the logging code to walk the dentry chain
  4092. * up for the file
  4093. */
  4094. if (S_ISREG(inode->i_mode))
  4095. BTRFS_I(inode)->last_unlink_trans = trans->transid;
  4096. /*
  4097. * if this inode hasn't been logged and directory we're renaming it
  4098. * from hasn't been logged, we don't need to log it
  4099. */
  4100. if (BTRFS_I(inode)->logged_trans <=
  4101. root->fs_info->last_trans_committed &&
  4102. (!old_dir || BTRFS_I(old_dir)->logged_trans <=
  4103. root->fs_info->last_trans_committed))
  4104. return 0;
  4105. return btrfs_log_inode_parent(trans, root, inode, parent, 0,
  4106. LLONG_MAX, 1, NULL);
  4107. }