tree-log.c 108 KB

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