tree-log.c 107 KB

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