tree-log.c 110 KB

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