tree-log.c 107 KB

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