tree-log.c 86 KB

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