tree-log.c 86 KB

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