tree-log.c 74 KB

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