tree-log.c 82 KB

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