tree-mod-log.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "messages.h"
  3. #include "tree-mod-log.h"
  4. #include "disk-io.h"
  5. #include "fs.h"
  6. #include "accessors.h"
  7. #include "tree-checker.h"
  8. struct tree_mod_root {
  9. u64 logical;
  10. u8 level;
  11. };
  12. struct tree_mod_elem {
  13. struct rb_node node;
  14. u64 logical;
  15. u64 seq;
  16. enum btrfs_mod_log_op op;
  17. /*
  18. * This is used for BTRFS_MOD_LOG_KEY_* and BTRFS_MOD_LOG_MOVE_KEYS
  19. * operations.
  20. */
  21. int slot;
  22. /* This is used for BTRFS_MOD_LOG_KEY* and BTRFS_MOD_LOG_ROOT_REPLACE. */
  23. u64 generation;
  24. union {
  25. /*
  26. * This is used for the following op types:
  27. *
  28. * BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING
  29. * BTRFS_MOD_LOG_KEY_REMOVE_WHILE_MOVING
  30. * BTRFS_MOD_LOG_KEY_REMOVE
  31. * BTRFS_MOD_LOG_KEY_REPLACE
  32. */
  33. struct {
  34. struct btrfs_disk_key key;
  35. u64 blockptr;
  36. } slot_change;
  37. /* This is used for op == BTRFS_MOD_LOG_MOVE_KEYS. */
  38. struct {
  39. int dst_slot;
  40. int nr_items;
  41. } move;
  42. /* This is used for op == BTRFS_MOD_LOG_ROOT_REPLACE. */
  43. struct tree_mod_root old_root;
  44. };
  45. };
  46. /*
  47. * Pull a new tree mod seq number for our operation.
  48. */
  49. static u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
  50. {
  51. return atomic64_inc_return(&fs_info->tree_mod_seq);
  52. }
  53. /*
  54. * This adds a new blocker to the tree mod log's blocker list if the @elem
  55. * passed does not already have a sequence number set. So when a caller expects
  56. * to record tree modifications, it should ensure to set elem->seq to zero
  57. * before calling btrfs_get_tree_mod_seq.
  58. * Returns a fresh, unused tree log modification sequence number, even if no new
  59. * blocker was added.
  60. */
  61. u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
  62. struct btrfs_seq_list *elem)
  63. {
  64. write_lock(&fs_info->tree_mod_log_lock);
  65. if (!elem->seq) {
  66. elem->seq = btrfs_inc_tree_mod_seq(fs_info);
  67. list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
  68. set_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags);
  69. }
  70. write_unlock(&fs_info->tree_mod_log_lock);
  71. return elem->seq;
  72. }
  73. void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
  74. struct btrfs_seq_list *elem)
  75. {
  76. struct rb_root *tm_root;
  77. struct rb_node *node;
  78. struct rb_node *next;
  79. struct tree_mod_elem *tm;
  80. u64 min_seq = BTRFS_SEQ_LAST;
  81. u64 seq_putting = elem->seq;
  82. if (!seq_putting)
  83. return;
  84. write_lock(&fs_info->tree_mod_log_lock);
  85. list_del(&elem->list);
  86. elem->seq = 0;
  87. if (list_empty(&fs_info->tree_mod_seq_list)) {
  88. clear_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags);
  89. } else {
  90. struct btrfs_seq_list *first;
  91. first = list_first_entry(&fs_info->tree_mod_seq_list,
  92. struct btrfs_seq_list, list);
  93. if (seq_putting > first->seq) {
  94. /*
  95. * Blocker with lower sequence number exists, we cannot
  96. * remove anything from the log.
  97. */
  98. write_unlock(&fs_info->tree_mod_log_lock);
  99. return;
  100. }
  101. min_seq = first->seq;
  102. }
  103. /*
  104. * Anything that's lower than the lowest existing (read: blocked)
  105. * sequence number can be removed from the tree.
  106. */
  107. tm_root = &fs_info->tree_mod_log;
  108. for (node = rb_first(tm_root); node; node = next) {
  109. next = rb_next(node);
  110. tm = rb_entry(node, struct tree_mod_elem, node);
  111. if (tm->seq >= min_seq)
  112. continue;
  113. rb_erase(node, tm_root);
  114. kfree(tm);
  115. }
  116. write_unlock(&fs_info->tree_mod_log_lock);
  117. }
  118. /*
  119. * Key order of the log:
  120. * node/leaf start address -> sequence
  121. *
  122. * The 'start address' is the logical address of the *new* root node for root
  123. * replace operations, or the logical address of the affected block for all
  124. * other operations.
  125. */
  126. static noinline int tree_mod_log_insert(struct btrfs_fs_info *fs_info,
  127. struct tree_mod_elem *tm)
  128. {
  129. struct rb_root *tm_root;
  130. struct rb_node **new;
  131. struct rb_node *parent = NULL;
  132. struct tree_mod_elem *cur;
  133. lockdep_assert_held_write(&fs_info->tree_mod_log_lock);
  134. tm->seq = btrfs_inc_tree_mod_seq(fs_info);
  135. tm_root = &fs_info->tree_mod_log;
  136. new = &tm_root->rb_node;
  137. while (*new) {
  138. cur = rb_entry(*new, struct tree_mod_elem, node);
  139. parent = *new;
  140. if (cur->logical < tm->logical)
  141. new = &((*new)->rb_left);
  142. else if (cur->logical > tm->logical)
  143. new = &((*new)->rb_right);
  144. else if (cur->seq < tm->seq)
  145. new = &((*new)->rb_left);
  146. else if (cur->seq > tm->seq)
  147. new = &((*new)->rb_right);
  148. else
  149. return -EEXIST;
  150. }
  151. rb_link_node(&tm->node, parent, new);
  152. rb_insert_color(&tm->node, tm_root);
  153. return 0;
  154. }
  155. static inline bool skip_eb_logging(const struct extent_buffer *eb)
  156. {
  157. const u64 owner = btrfs_header_owner(eb);
  158. if (btrfs_header_level(eb) == 0)
  159. return true;
  160. /*
  161. * Tree mod logging exists so that there's a consistent view of the
  162. * extents and backrefs of inodes even if while a task is iterating over
  163. * them other tasks are modifying subvolume trees and the extent tree
  164. * (including running delayed refs). So we only need to log extent
  165. * buffers from the extent tree and subvolume trees.
  166. */
  167. if (owner == BTRFS_EXTENT_TREE_OBJECTID)
  168. return false;
  169. if (btrfs_is_fstree(owner))
  170. return false;
  171. return true;
  172. }
  173. /*
  174. * Determines if logging can be omitted. Returns true if it can. Otherwise, it
  175. * returns false with the tree_mod_log_lock acquired. The caller must hold
  176. * this until all tree mod log insertions are recorded in the rb tree and then
  177. * write unlock fs_info::tree_mod_log_lock.
  178. */
  179. static bool tree_mod_dont_log(struct btrfs_fs_info *fs_info, const struct extent_buffer *eb)
  180. {
  181. if (!test_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags))
  182. return true;
  183. if (eb && skip_eb_logging(eb))
  184. return true;
  185. write_lock(&fs_info->tree_mod_log_lock);
  186. if (list_empty(&(fs_info)->tree_mod_seq_list)) {
  187. write_unlock(&fs_info->tree_mod_log_lock);
  188. return true;
  189. }
  190. return false;
  191. }
  192. /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
  193. static bool tree_mod_need_log(const struct btrfs_fs_info *fs_info,
  194. const struct extent_buffer *eb)
  195. {
  196. if (!test_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags))
  197. return false;
  198. if (eb && skip_eb_logging(eb))
  199. return false;
  200. return true;
  201. }
  202. static struct tree_mod_elem *alloc_tree_mod_elem(const struct extent_buffer *eb,
  203. int slot,
  204. enum btrfs_mod_log_op op)
  205. {
  206. struct tree_mod_elem *tm;
  207. /* Can't be one of these types, due to union in struct tree_mod_elem. */
  208. ASSERT(op != BTRFS_MOD_LOG_MOVE_KEYS);
  209. ASSERT(op != BTRFS_MOD_LOG_ROOT_REPLACE);
  210. tm = kzalloc_obj(*tm, GFP_NOFS);
  211. if (!tm)
  212. return NULL;
  213. tm->logical = eb->start;
  214. btrfs_node_key(eb, &tm->slot_change.key, slot);
  215. tm->slot_change.blockptr = btrfs_node_blockptr(eb, slot);
  216. tm->op = op;
  217. tm->slot = slot;
  218. tm->generation = btrfs_node_ptr_generation(eb, slot);
  219. RB_CLEAR_NODE(&tm->node);
  220. return tm;
  221. }
  222. int btrfs_tree_mod_log_insert_key(const struct extent_buffer *eb, int slot,
  223. enum btrfs_mod_log_op op)
  224. {
  225. struct tree_mod_elem *tm;
  226. int ret = 0;
  227. if (!tree_mod_need_log(eb->fs_info, eb))
  228. return 0;
  229. tm = alloc_tree_mod_elem(eb, slot, op);
  230. if (!tm)
  231. ret = -ENOMEM;
  232. if (tree_mod_dont_log(eb->fs_info, eb)) {
  233. kfree(tm);
  234. /*
  235. * Don't error if we failed to allocate memory because we don't
  236. * need to log.
  237. */
  238. return 0;
  239. } else if (ret != 0) {
  240. /*
  241. * We previously failed to allocate memory and we need to log,
  242. * so we have to fail.
  243. */
  244. goto out_unlock;
  245. }
  246. ret = tree_mod_log_insert(eb->fs_info, tm);
  247. out_unlock:
  248. write_unlock(&eb->fs_info->tree_mod_log_lock);
  249. if (ret)
  250. kfree(tm);
  251. return ret;
  252. }
  253. static struct tree_mod_elem *tree_mod_log_alloc_move(const struct extent_buffer *eb,
  254. int dst_slot, int src_slot,
  255. int nr_items)
  256. {
  257. struct tree_mod_elem *tm;
  258. tm = kzalloc_obj(*tm, GFP_NOFS);
  259. if (!tm)
  260. return ERR_PTR(-ENOMEM);
  261. tm->logical = eb->start;
  262. tm->slot = src_slot;
  263. tm->move.dst_slot = dst_slot;
  264. tm->move.nr_items = nr_items;
  265. tm->op = BTRFS_MOD_LOG_MOVE_KEYS;
  266. RB_CLEAR_NODE(&tm->node);
  267. return tm;
  268. }
  269. int btrfs_tree_mod_log_insert_move(const struct extent_buffer *eb,
  270. int dst_slot, int src_slot,
  271. int nr_items)
  272. {
  273. struct tree_mod_elem *tm = NULL;
  274. struct tree_mod_elem **tm_list = NULL;
  275. int ret = 0;
  276. int i;
  277. bool locked = false;
  278. if (!tree_mod_need_log(eb->fs_info, eb))
  279. return 0;
  280. tm_list = kzalloc_objs(struct tree_mod_elem *, nr_items, GFP_NOFS);
  281. if (!tm_list) {
  282. ret = -ENOMEM;
  283. goto lock;
  284. }
  285. tm = tree_mod_log_alloc_move(eb, dst_slot, src_slot, nr_items);
  286. if (IS_ERR(tm)) {
  287. ret = PTR_ERR(tm);
  288. tm = NULL;
  289. goto lock;
  290. }
  291. for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
  292. tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
  293. BTRFS_MOD_LOG_KEY_REMOVE_WHILE_MOVING);
  294. if (!tm_list[i]) {
  295. ret = -ENOMEM;
  296. goto lock;
  297. }
  298. }
  299. lock:
  300. if (tree_mod_dont_log(eb->fs_info, eb)) {
  301. /*
  302. * Don't error if we failed to allocate memory because we don't
  303. * need to log.
  304. */
  305. ret = 0;
  306. goto free_tms;
  307. }
  308. locked = true;
  309. /*
  310. * We previously failed to allocate memory and we need to log, so we
  311. * have to fail.
  312. */
  313. if (ret != 0)
  314. goto free_tms;
  315. /*
  316. * When we override something during the move, we log these removals.
  317. * This can only happen when we move towards the beginning of the
  318. * buffer, i.e. dst_slot < src_slot.
  319. */
  320. for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
  321. ret = tree_mod_log_insert(eb->fs_info, tm_list[i]);
  322. if (ret)
  323. goto free_tms;
  324. }
  325. ret = tree_mod_log_insert(eb->fs_info, tm);
  326. if (ret)
  327. goto free_tms;
  328. write_unlock(&eb->fs_info->tree_mod_log_lock);
  329. kfree(tm_list);
  330. return 0;
  331. free_tms:
  332. if (tm_list) {
  333. for (i = 0; i < nr_items; i++) {
  334. if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
  335. rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log);
  336. kfree(tm_list[i]);
  337. }
  338. }
  339. if (locked)
  340. write_unlock(&eb->fs_info->tree_mod_log_lock);
  341. kfree(tm_list);
  342. kfree(tm);
  343. return ret;
  344. }
  345. static int tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
  346. struct tree_mod_elem **tm_list,
  347. int nritems)
  348. {
  349. int i, j;
  350. int ret;
  351. for (i = nritems - 1; i >= 0; i--) {
  352. ret = tree_mod_log_insert(fs_info, tm_list[i]);
  353. if (ret) {
  354. for (j = nritems - 1; j > i; j--)
  355. rb_erase(&tm_list[j]->node,
  356. &fs_info->tree_mod_log);
  357. return ret;
  358. }
  359. }
  360. return 0;
  361. }
  362. int btrfs_tree_mod_log_insert_root(struct extent_buffer *old_root,
  363. struct extent_buffer *new_root,
  364. bool log_removal)
  365. {
  366. struct btrfs_fs_info *fs_info = old_root->fs_info;
  367. struct tree_mod_elem *tm = NULL;
  368. struct tree_mod_elem **tm_list = NULL;
  369. int nritems = 0;
  370. int ret = 0;
  371. int i;
  372. if (!tree_mod_need_log(fs_info, NULL))
  373. return 0;
  374. if (log_removal && btrfs_header_level(old_root) > 0) {
  375. nritems = btrfs_header_nritems(old_root);
  376. tm_list = kzalloc_objs(struct tree_mod_elem *, nritems,
  377. GFP_NOFS);
  378. if (!tm_list) {
  379. ret = -ENOMEM;
  380. goto lock;
  381. }
  382. for (i = 0; i < nritems; i++) {
  383. tm_list[i] = alloc_tree_mod_elem(old_root, i,
  384. BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING);
  385. if (!tm_list[i]) {
  386. ret = -ENOMEM;
  387. goto lock;
  388. }
  389. }
  390. }
  391. tm = kzalloc_obj(*tm, GFP_NOFS);
  392. if (!tm) {
  393. ret = -ENOMEM;
  394. goto lock;
  395. }
  396. tm->logical = new_root->start;
  397. tm->old_root.logical = old_root->start;
  398. tm->old_root.level = btrfs_header_level(old_root);
  399. tm->generation = btrfs_header_generation(old_root);
  400. tm->op = BTRFS_MOD_LOG_ROOT_REPLACE;
  401. lock:
  402. if (tree_mod_dont_log(fs_info, NULL)) {
  403. /*
  404. * Don't error if we failed to allocate memory because we don't
  405. * need to log.
  406. */
  407. ret = 0;
  408. goto free_tms;
  409. } else if (ret != 0) {
  410. /*
  411. * We previously failed to allocate memory and we need to log,
  412. * so we have to fail.
  413. */
  414. goto out_unlock;
  415. }
  416. if (tm_list)
  417. ret = tree_mod_log_free_eb(fs_info, tm_list, nritems);
  418. if (!ret)
  419. ret = tree_mod_log_insert(fs_info, tm);
  420. out_unlock:
  421. write_unlock(&fs_info->tree_mod_log_lock);
  422. if (ret)
  423. goto free_tms;
  424. kfree(tm_list);
  425. return ret;
  426. free_tms:
  427. if (tm_list) {
  428. for (i = 0; i < nritems; i++)
  429. kfree(tm_list[i]);
  430. kfree(tm_list);
  431. }
  432. kfree(tm);
  433. return ret;
  434. }
  435. static struct tree_mod_elem *__tree_mod_log_search(struct btrfs_fs_info *fs_info,
  436. u64 start, u64 min_seq,
  437. bool smallest)
  438. {
  439. struct rb_root *tm_root;
  440. struct rb_node *node;
  441. struct tree_mod_elem *cur = NULL;
  442. struct tree_mod_elem *found = NULL;
  443. read_lock(&fs_info->tree_mod_log_lock);
  444. tm_root = &fs_info->tree_mod_log;
  445. node = tm_root->rb_node;
  446. while (node) {
  447. cur = rb_entry(node, struct tree_mod_elem, node);
  448. if (cur->logical < start) {
  449. node = node->rb_left;
  450. } else if (cur->logical > start) {
  451. node = node->rb_right;
  452. } else if (cur->seq < min_seq) {
  453. node = node->rb_left;
  454. } else if (!smallest) {
  455. /* We want the node with the highest seq */
  456. if (found)
  457. BUG_ON(found->seq > cur->seq);
  458. found = cur;
  459. node = node->rb_left;
  460. } else if (cur->seq > min_seq) {
  461. /* We want the node with the smallest seq */
  462. if (found)
  463. BUG_ON(found->seq < cur->seq);
  464. found = cur;
  465. node = node->rb_right;
  466. } else {
  467. found = cur;
  468. break;
  469. }
  470. }
  471. read_unlock(&fs_info->tree_mod_log_lock);
  472. return found;
  473. }
  474. /*
  475. * This returns the element from the log with the smallest time sequence
  476. * value that's in the log (the oldest log item). Any element with a time
  477. * sequence lower than min_seq will be ignored.
  478. */
  479. static struct tree_mod_elem *tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info,
  480. u64 start, u64 min_seq)
  481. {
  482. return __tree_mod_log_search(fs_info, start, min_seq, true);
  483. }
  484. /*
  485. * This returns the element from the log with the largest time sequence
  486. * value that's in the log (the most recent log item). Any element with
  487. * a time sequence lower than min_seq will be ignored.
  488. */
  489. static struct tree_mod_elem *tree_mod_log_search(struct btrfs_fs_info *fs_info,
  490. u64 start, u64 min_seq)
  491. {
  492. return __tree_mod_log_search(fs_info, start, min_seq, false);
  493. }
  494. int btrfs_tree_mod_log_eb_copy(struct extent_buffer *dst,
  495. const struct extent_buffer *src,
  496. unsigned long dst_offset,
  497. unsigned long src_offset,
  498. int nr_items)
  499. {
  500. struct btrfs_fs_info *fs_info = dst->fs_info;
  501. int ret = 0;
  502. struct tree_mod_elem **tm_list = NULL;
  503. struct tree_mod_elem **tm_list_add = NULL;
  504. struct tree_mod_elem **tm_list_rem = NULL;
  505. int i;
  506. bool locked = false;
  507. struct tree_mod_elem *dst_move_tm = NULL;
  508. struct tree_mod_elem *src_move_tm = NULL;
  509. u32 dst_move_nr_items = btrfs_header_nritems(dst) - dst_offset;
  510. u32 src_move_nr_items = btrfs_header_nritems(src) - (src_offset + nr_items);
  511. if (!tree_mod_need_log(fs_info, NULL))
  512. return 0;
  513. if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
  514. return 0;
  515. tm_list = kzalloc_objs(struct tree_mod_elem *, nr_items * 2, GFP_NOFS);
  516. if (!tm_list) {
  517. ret = -ENOMEM;
  518. goto lock;
  519. }
  520. if (dst_move_nr_items) {
  521. dst_move_tm = tree_mod_log_alloc_move(dst, dst_offset + nr_items,
  522. dst_offset, dst_move_nr_items);
  523. if (IS_ERR(dst_move_tm)) {
  524. ret = PTR_ERR(dst_move_tm);
  525. dst_move_tm = NULL;
  526. goto lock;
  527. }
  528. }
  529. if (src_move_nr_items) {
  530. src_move_tm = tree_mod_log_alloc_move(src, src_offset,
  531. src_offset + nr_items,
  532. src_move_nr_items);
  533. if (IS_ERR(src_move_tm)) {
  534. ret = PTR_ERR(src_move_tm);
  535. src_move_tm = NULL;
  536. goto lock;
  537. }
  538. }
  539. tm_list_add = tm_list;
  540. tm_list_rem = tm_list + nr_items;
  541. for (i = 0; i < nr_items; i++) {
  542. tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
  543. BTRFS_MOD_LOG_KEY_REMOVE);
  544. if (!tm_list_rem[i]) {
  545. ret = -ENOMEM;
  546. goto lock;
  547. }
  548. tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
  549. BTRFS_MOD_LOG_KEY_ADD);
  550. if (!tm_list_add[i]) {
  551. ret = -ENOMEM;
  552. goto lock;
  553. }
  554. }
  555. lock:
  556. if (tree_mod_dont_log(fs_info, NULL)) {
  557. /*
  558. * Don't error if we failed to allocate memory because we don't
  559. * need to log.
  560. */
  561. ret = 0;
  562. goto free_tms;
  563. }
  564. locked = true;
  565. /*
  566. * We previously failed to allocate memory and we need to log, so we
  567. * have to fail.
  568. */
  569. if (ret != 0)
  570. goto free_tms;
  571. if (dst_move_tm) {
  572. ret = tree_mod_log_insert(fs_info, dst_move_tm);
  573. if (ret)
  574. goto free_tms;
  575. }
  576. for (i = 0; i < nr_items; i++) {
  577. ret = tree_mod_log_insert(fs_info, tm_list_rem[i]);
  578. if (ret)
  579. goto free_tms;
  580. ret = tree_mod_log_insert(fs_info, tm_list_add[i]);
  581. if (ret)
  582. goto free_tms;
  583. }
  584. if (src_move_tm) {
  585. ret = tree_mod_log_insert(fs_info, src_move_tm);
  586. if (ret)
  587. goto free_tms;
  588. }
  589. write_unlock(&fs_info->tree_mod_log_lock);
  590. kfree(tm_list);
  591. return 0;
  592. free_tms:
  593. if (dst_move_tm && !RB_EMPTY_NODE(&dst_move_tm->node))
  594. rb_erase(&dst_move_tm->node, &fs_info->tree_mod_log);
  595. kfree(dst_move_tm);
  596. if (src_move_tm && !RB_EMPTY_NODE(&src_move_tm->node))
  597. rb_erase(&src_move_tm->node, &fs_info->tree_mod_log);
  598. kfree(src_move_tm);
  599. if (tm_list) {
  600. for (i = 0; i < nr_items * 2; i++) {
  601. if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
  602. rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
  603. kfree(tm_list[i]);
  604. }
  605. }
  606. if (locked)
  607. write_unlock(&fs_info->tree_mod_log_lock);
  608. kfree(tm_list);
  609. return ret;
  610. }
  611. int btrfs_tree_mod_log_free_eb(struct extent_buffer *eb)
  612. {
  613. struct tree_mod_elem **tm_list = NULL;
  614. int nritems = 0;
  615. int i;
  616. int ret = 0;
  617. if (!tree_mod_need_log(eb->fs_info, eb))
  618. return 0;
  619. nritems = btrfs_header_nritems(eb);
  620. tm_list = kzalloc_objs(struct tree_mod_elem *, nritems, GFP_NOFS);
  621. if (!tm_list) {
  622. ret = -ENOMEM;
  623. goto lock;
  624. }
  625. for (i = 0; i < nritems; i++) {
  626. tm_list[i] = alloc_tree_mod_elem(eb, i,
  627. BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING);
  628. if (!tm_list[i]) {
  629. ret = -ENOMEM;
  630. goto lock;
  631. }
  632. }
  633. lock:
  634. if (tree_mod_dont_log(eb->fs_info, eb)) {
  635. /*
  636. * Don't error if we failed to allocate memory because we don't
  637. * need to log.
  638. */
  639. ret = 0;
  640. goto free_tms;
  641. } else if (ret != 0) {
  642. /*
  643. * We previously failed to allocate memory and we need to log,
  644. * so we have to fail.
  645. */
  646. goto out_unlock;
  647. }
  648. ret = tree_mod_log_free_eb(eb->fs_info, tm_list, nritems);
  649. out_unlock:
  650. write_unlock(&eb->fs_info->tree_mod_log_lock);
  651. if (ret)
  652. goto free_tms;
  653. kfree(tm_list);
  654. return 0;
  655. free_tms:
  656. if (tm_list) {
  657. for (i = 0; i < nritems; i++)
  658. kfree(tm_list[i]);
  659. kfree(tm_list);
  660. }
  661. return ret;
  662. }
  663. /*
  664. * Returns the logical address of the oldest predecessor of the given root.
  665. * Entries older than time_seq are ignored.
  666. */
  667. static struct tree_mod_elem *tree_mod_log_oldest_root(struct extent_buffer *eb_root,
  668. u64 time_seq)
  669. {
  670. struct tree_mod_elem *tm;
  671. struct tree_mod_elem *found = NULL;
  672. u64 root_logical = eb_root->start;
  673. bool looped = false;
  674. if (!time_seq)
  675. return NULL;
  676. /*
  677. * The very last operation that's logged for a root is the replacement
  678. * operation (if it is replaced at all). This has the logical address
  679. * of the *new* root, making it the very first operation that's logged
  680. * for this root.
  681. */
  682. while (1) {
  683. tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical,
  684. time_seq);
  685. if (!looped && !tm)
  686. return NULL;
  687. /*
  688. * If there are no tree operation for the oldest root, we simply
  689. * return it. This should only happen if that (old) root is at
  690. * level 0.
  691. */
  692. if (!tm)
  693. break;
  694. /*
  695. * If there's an operation that's not a root replacement, we
  696. * found the oldest version of our root. Normally, we'll find a
  697. * BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
  698. */
  699. if (tm->op != BTRFS_MOD_LOG_ROOT_REPLACE)
  700. break;
  701. found = tm;
  702. root_logical = tm->old_root.logical;
  703. looped = true;
  704. }
  705. /* If there's no old root to return, return what we found instead */
  706. if (!found)
  707. found = tm;
  708. return found;
  709. }
  710. /*
  711. * tm is a pointer to the first operation to rewind within eb. Then, all
  712. * previous operations will be rewound (until we reach something older than
  713. * time_seq).
  714. */
  715. static void tree_mod_log_rewind(struct btrfs_fs_info *fs_info,
  716. struct extent_buffer *eb,
  717. u64 time_seq,
  718. struct tree_mod_elem *first_tm)
  719. {
  720. u32 n;
  721. struct rb_node *next;
  722. struct tree_mod_elem *tm = first_tm;
  723. unsigned long o_dst;
  724. unsigned long o_src;
  725. unsigned long p_size = sizeof(struct btrfs_key_ptr);
  726. /*
  727. * max_slot tracks the maximum valid slot of the rewind eb at every
  728. * step of the rewind. This is in contrast with 'n' which eventually
  729. * matches the number of items, but can be wrong during moves or if
  730. * removes overlap on already valid slots (which is probably separately
  731. * a bug). We do this to validate the offsets of memmoves for rewinding
  732. * moves and detect invalid memmoves.
  733. *
  734. * Since a rewind eb can start empty, max_slot is a signed integer with
  735. * a special meaning for -1, which is that no slot is valid to move out
  736. * of. Any other negative value is invalid.
  737. */
  738. int max_slot;
  739. int move_src_end_slot;
  740. int move_dst_end_slot;
  741. n = btrfs_header_nritems(eb);
  742. max_slot = n - 1;
  743. read_lock(&fs_info->tree_mod_log_lock);
  744. while (tm && tm->seq >= time_seq) {
  745. ASSERT(max_slot >= -1);
  746. /*
  747. * All the operations are recorded with the operator used for
  748. * the modification. As we're going backwards, we do the
  749. * opposite of each operation here.
  750. */
  751. switch (tm->op) {
  752. case BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING:
  753. BUG_ON(tm->slot < n);
  754. fallthrough;
  755. case BTRFS_MOD_LOG_KEY_REMOVE_WHILE_MOVING:
  756. case BTRFS_MOD_LOG_KEY_REMOVE:
  757. btrfs_set_node_key(eb, &tm->slot_change.key, tm->slot);
  758. btrfs_set_node_blockptr(eb, tm->slot, tm->slot_change.blockptr);
  759. btrfs_set_node_ptr_generation(eb, tm->slot,
  760. tm->generation);
  761. n++;
  762. if (tm->slot > max_slot)
  763. max_slot = tm->slot;
  764. break;
  765. case BTRFS_MOD_LOG_KEY_REPLACE:
  766. BUG_ON(tm->slot >= n);
  767. btrfs_set_node_key(eb, &tm->slot_change.key, tm->slot);
  768. btrfs_set_node_blockptr(eb, tm->slot, tm->slot_change.blockptr);
  769. btrfs_set_node_ptr_generation(eb, tm->slot,
  770. tm->generation);
  771. break;
  772. case BTRFS_MOD_LOG_KEY_ADD:
  773. /*
  774. * It is possible we could have already removed keys
  775. * behind the known max slot, so this will be an
  776. * overestimate. In practice, the copy operation
  777. * inserts them in increasing order, and overestimating
  778. * just means we miss some warnings, so it's OK. It
  779. * isn't worth carefully tracking the full array of
  780. * valid slots to check against when moving.
  781. */
  782. if (tm->slot == max_slot)
  783. max_slot--;
  784. /* if a move operation is needed it's in the log */
  785. n--;
  786. break;
  787. case BTRFS_MOD_LOG_MOVE_KEYS:
  788. ASSERT(tm->move.nr_items > 0);
  789. move_src_end_slot = tm->move.dst_slot + tm->move.nr_items - 1;
  790. move_dst_end_slot = tm->slot + tm->move.nr_items - 1;
  791. o_dst = btrfs_node_key_ptr_offset(eb, tm->slot);
  792. o_src = btrfs_node_key_ptr_offset(eb, tm->move.dst_slot);
  793. if (WARN_ON(move_src_end_slot > max_slot ||
  794. tm->move.nr_items <= 0)) {
  795. btrfs_warn(fs_info,
  796. "move from invalid tree mod log slot eb %llu slot %d dst_slot %d nr_items %d seq %llu n %u max_slot %d",
  797. eb->start, tm->slot,
  798. tm->move.dst_slot, tm->move.nr_items,
  799. tm->seq, n, max_slot);
  800. }
  801. memmove_extent_buffer(eb, o_dst, o_src,
  802. tm->move.nr_items * p_size);
  803. max_slot = move_dst_end_slot;
  804. break;
  805. case BTRFS_MOD_LOG_ROOT_REPLACE:
  806. /*
  807. * This operation is special. For roots, this must be
  808. * handled explicitly before rewinding.
  809. * For non-roots, this operation may exist if the node
  810. * was a root: root A -> child B; then A gets empty and
  811. * B is promoted to the new root. In the mod log, we'll
  812. * have a root-replace operation for B, a tree block
  813. * that is no root. We simply ignore that operation.
  814. */
  815. break;
  816. }
  817. next = rb_next(&tm->node);
  818. if (!next)
  819. break;
  820. tm = rb_entry(next, struct tree_mod_elem, node);
  821. if (tm->logical != first_tm->logical)
  822. break;
  823. }
  824. read_unlock(&fs_info->tree_mod_log_lock);
  825. btrfs_set_header_nritems(eb, n);
  826. }
  827. /*
  828. * Called with eb read locked. If the buffer cannot be rewound, the same buffer
  829. * is returned. If rewind operations happen, a fresh buffer is returned. The
  830. * returned buffer is always read-locked. If the returned buffer is not the
  831. * input buffer, the lock on the input buffer is released and the input buffer
  832. * is freed (its refcount is decremented).
  833. */
  834. struct extent_buffer *btrfs_tree_mod_log_rewind(struct btrfs_fs_info *fs_info,
  835. struct extent_buffer *eb,
  836. u64 time_seq)
  837. {
  838. struct extent_buffer *eb_rewin;
  839. struct tree_mod_elem *tm;
  840. if (!time_seq)
  841. return eb;
  842. if (btrfs_header_level(eb) == 0)
  843. return eb;
  844. tm = tree_mod_log_search(fs_info, eb->start, time_seq);
  845. if (!tm)
  846. return eb;
  847. if (tm->op == BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
  848. BUG_ON(tm->slot != 0);
  849. eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
  850. if (!eb_rewin) {
  851. btrfs_tree_read_unlock(eb);
  852. free_extent_buffer(eb);
  853. return NULL;
  854. }
  855. btrfs_set_header_bytenr(eb_rewin, eb->start);
  856. btrfs_set_header_backref_rev(eb_rewin,
  857. btrfs_header_backref_rev(eb));
  858. btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
  859. btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
  860. } else {
  861. eb_rewin = btrfs_clone_extent_buffer(eb);
  862. if (!eb_rewin) {
  863. btrfs_tree_read_unlock(eb);
  864. free_extent_buffer(eb);
  865. return NULL;
  866. }
  867. }
  868. btrfs_tree_read_unlock(eb);
  869. free_extent_buffer(eb);
  870. btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb_rewin),
  871. eb_rewin, btrfs_header_level(eb_rewin));
  872. btrfs_tree_read_lock(eb_rewin);
  873. tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
  874. WARN_ON(btrfs_header_nritems(eb_rewin) >
  875. BTRFS_NODEPTRS_PER_BLOCK(fs_info));
  876. return eb_rewin;
  877. }
  878. /*
  879. * Rewind the state of @root's root node to the given @time_seq value.
  880. * If there are no changes, the current root->root_node is returned. If anything
  881. * changed in between, there's a fresh buffer allocated on which the rewind
  882. * operations are done. In any case, the returned buffer is read locked.
  883. * Returns NULL on error (with no locks held).
  884. */
  885. struct extent_buffer *btrfs_get_old_root(struct btrfs_root *root, u64 time_seq)
  886. {
  887. struct btrfs_fs_info *fs_info = root->fs_info;
  888. struct tree_mod_elem *tm;
  889. struct extent_buffer *eb = NULL;
  890. struct extent_buffer *eb_root;
  891. u64 eb_root_owner = 0;
  892. struct extent_buffer *old;
  893. struct tree_mod_root *old_root = NULL;
  894. u64 old_generation = 0;
  895. u64 logical;
  896. int level;
  897. eb_root = btrfs_read_lock_root_node(root);
  898. tm = tree_mod_log_oldest_root(eb_root, time_seq);
  899. if (!tm)
  900. return eb_root;
  901. if (tm->op == BTRFS_MOD_LOG_ROOT_REPLACE) {
  902. old_root = &tm->old_root;
  903. old_generation = tm->generation;
  904. logical = old_root->logical;
  905. level = old_root->level;
  906. } else {
  907. logical = eb_root->start;
  908. level = btrfs_header_level(eb_root);
  909. }
  910. tm = tree_mod_log_search(fs_info, logical, time_seq);
  911. if (old_root && tm && tm->op != BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
  912. struct btrfs_tree_parent_check check = { 0 };
  913. btrfs_tree_read_unlock(eb_root);
  914. free_extent_buffer(eb_root);
  915. check.level = level;
  916. check.owner_root = btrfs_root_id(root);
  917. old = read_tree_block(fs_info, logical, &check);
  918. if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
  919. if (!IS_ERR(old))
  920. free_extent_buffer(old);
  921. btrfs_warn(fs_info,
  922. "failed to read tree block %llu from get_old_root",
  923. logical);
  924. } else {
  925. struct tree_mod_elem *tm2;
  926. btrfs_tree_read_lock(old);
  927. eb = btrfs_clone_extent_buffer(old);
  928. /*
  929. * After the lookup for the most recent tree mod operation
  930. * above and before we locked and cloned the extent buffer
  931. * 'old', a new tree mod log operation may have been added.
  932. * So lookup for a more recent one to make sure the number
  933. * of mod log operations we replay is consistent with the
  934. * number of items we have in the cloned extent buffer,
  935. * otherwise we can hit a BUG_ON when rewinding the extent
  936. * buffer.
  937. */
  938. tm2 = tree_mod_log_search(fs_info, logical, time_seq);
  939. btrfs_tree_read_unlock(old);
  940. free_extent_buffer(old);
  941. ASSERT(tm2);
  942. ASSERT(tm2 == tm || tm2->seq > tm->seq);
  943. if (!tm2 || tm2->seq < tm->seq) {
  944. free_extent_buffer(eb);
  945. return NULL;
  946. }
  947. tm = tm2;
  948. }
  949. } else if (old_root) {
  950. eb_root_owner = btrfs_header_owner(eb_root);
  951. btrfs_tree_read_unlock(eb_root);
  952. free_extent_buffer(eb_root);
  953. eb = alloc_dummy_extent_buffer(fs_info, logical);
  954. } else {
  955. eb = btrfs_clone_extent_buffer(eb_root);
  956. btrfs_tree_read_unlock(eb_root);
  957. free_extent_buffer(eb_root);
  958. }
  959. if (!eb)
  960. return NULL;
  961. if (old_root) {
  962. btrfs_set_header_bytenr(eb, eb->start);
  963. btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
  964. btrfs_set_header_owner(eb, eb_root_owner);
  965. btrfs_set_header_level(eb, old_root->level);
  966. btrfs_set_header_generation(eb, old_generation);
  967. }
  968. btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), eb,
  969. btrfs_header_level(eb));
  970. btrfs_tree_read_lock(eb);
  971. if (tm)
  972. tree_mod_log_rewind(fs_info, eb, time_seq, tm);
  973. else
  974. WARN_ON(btrfs_header_level(eb) != 0);
  975. WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
  976. return eb;
  977. }
  978. int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
  979. {
  980. struct tree_mod_elem *tm;
  981. int level;
  982. struct extent_buffer *eb_root = btrfs_root_node(root);
  983. tm = tree_mod_log_oldest_root(eb_root, time_seq);
  984. if (tm && tm->op == BTRFS_MOD_LOG_ROOT_REPLACE)
  985. level = tm->old_root.level;
  986. else
  987. level = btrfs_header_level(eb_root);
  988. free_extent_buffer(eb_root);
  989. return level;
  990. }
  991. /*
  992. * Return the lowest sequence number in the tree modification log.
  993. *
  994. * Return the sequence number of the oldest tree modification log user, which
  995. * corresponds to the lowest sequence number of all existing users. If there are
  996. * no users it returns 0.
  997. */
  998. u64 btrfs_tree_mod_log_lowest_seq(struct btrfs_fs_info *fs_info)
  999. {
  1000. u64 ret = 0;
  1001. read_lock(&fs_info->tree_mod_log_lock);
  1002. if (!list_empty(&fs_info->tree_mod_seq_list)) {
  1003. struct btrfs_seq_list *elem;
  1004. elem = list_first_entry(&fs_info->tree_mod_seq_list,
  1005. struct btrfs_seq_list, list);
  1006. ret = elem->seq;
  1007. }
  1008. read_unlock(&fs_info->tree_mod_log_lock);
  1009. return ret;
  1010. }