tree-log.c 85 KB

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