tree-log.c 109 KB

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