tree-log.c 79 KB

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