tree-log.c 85 KB

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