tree-log.c 114 KB

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