ctree.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "kerncompat.h"
  4. #include "radix-tree.h"
  5. #include "ctree.h"
  6. #include "disk-io.h"
  7. #include "print-tree.h"
  8. static int split_node(struct btrfs_root *root, struct btrfs_path *path,
  9. int level);
  10. static int split_leaf(struct btrfs_root *root, struct btrfs_path *path,
  11. int data_size);
  12. static int push_node_left(struct btrfs_root *root, struct btrfs_buffer *dst,
  13. struct btrfs_buffer *src);
  14. static int balance_node_right(struct btrfs_root *root,
  15. struct btrfs_buffer *dst_buf,
  16. struct btrfs_buffer *src_buf);
  17. static int del_ptr(struct btrfs_root *root, struct btrfs_path *path, int level,
  18. int slot);
  19. inline void btrfs_init_path(struct btrfs_path *p)
  20. {
  21. memset(p, 0, sizeof(*p));
  22. }
  23. void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
  24. {
  25. int i;
  26. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  27. if (!p->nodes[i])
  28. break;
  29. btrfs_block_release(root, p->nodes[i]);
  30. }
  31. memset(p, 0, sizeof(*p));
  32. }
  33. static int btrfs_cow_block(struct btrfs_root *root,
  34. struct btrfs_buffer *buf,
  35. struct btrfs_buffer *parent,
  36. int parent_slot,
  37. struct btrfs_buffer **cow_ret)
  38. {
  39. struct btrfs_buffer *cow;
  40. if (!list_empty(&buf->dirty)) {
  41. *cow_ret = buf;
  42. return 0;
  43. }
  44. cow = btrfs_alloc_free_block(root);
  45. memcpy(&cow->node, &buf->node, root->blocksize);
  46. btrfs_set_header_blocknr(&cow->node.header, cow->blocknr);
  47. *cow_ret = cow;
  48. btrfs_inc_ref(root, buf);
  49. if (buf == root->node) {
  50. root->node = cow;
  51. cow->count++;
  52. if (buf != root->commit_root)
  53. btrfs_free_extent(root, buf->blocknr, 1, 1);
  54. btrfs_block_release(root, buf);
  55. } else {
  56. btrfs_set_node_blockptr(&parent->node, parent_slot,
  57. cow->blocknr);
  58. BUG_ON(list_empty(&parent->dirty));
  59. btrfs_free_extent(root, buf->blocknr, 1, 1);
  60. }
  61. btrfs_block_release(root, buf);
  62. return 0;
  63. }
  64. /*
  65. * The leaf data grows from end-to-front in the node.
  66. * this returns the address of the start of the last item,
  67. * which is the stop of the leaf data stack
  68. */
  69. static inline unsigned int leaf_data_end(struct btrfs_root *root,
  70. struct btrfs_leaf *leaf)
  71. {
  72. u32 nr = btrfs_header_nritems(&leaf->header);
  73. if (nr == 0)
  74. return BTRFS_LEAF_DATA_SIZE(root);
  75. return btrfs_item_offset(leaf->items + nr - 1);
  76. }
  77. /*
  78. * The space between the end of the leaf items and
  79. * the start of the leaf data. IOW, how much room
  80. * the leaf has left for both items and data
  81. */
  82. int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
  83. {
  84. int data_end = leaf_data_end(root, leaf);
  85. int nritems = btrfs_header_nritems(&leaf->header);
  86. char *items_end = (char *)(leaf->items + nritems + 1);
  87. return (char *)(btrfs_leaf_data(leaf) + data_end) - (char *)items_end;
  88. }
  89. /*
  90. * compare two keys in a memcmp fashion
  91. */
  92. static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
  93. {
  94. struct btrfs_key k1;
  95. btrfs_disk_key_to_cpu(&k1, disk);
  96. if (k1.objectid > k2->objectid)
  97. return 1;
  98. if (k1.objectid < k2->objectid)
  99. return -1;
  100. if (k1.flags > k2->flags)
  101. return 1;
  102. if (k1.flags < k2->flags)
  103. return -1;
  104. if (k1.offset > k2->offset)
  105. return 1;
  106. if (k1.offset < k2->offset)
  107. return -1;
  108. return 0;
  109. }
  110. static int check_node(struct btrfs_root *root, struct btrfs_path *path,
  111. int level)
  112. {
  113. int i;
  114. struct btrfs_node *parent = NULL;
  115. struct btrfs_node *node = &path->nodes[level]->node;
  116. int parent_slot;
  117. u32 nritems = btrfs_header_nritems(&node->header);
  118. if (path->nodes[level + 1])
  119. parent = &path->nodes[level + 1]->node;
  120. parent_slot = path->slots[level + 1];
  121. BUG_ON(nritems == 0);
  122. if (parent) {
  123. struct btrfs_disk_key *parent_key;
  124. parent_key = &parent->ptrs[parent_slot].key;
  125. BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
  126. sizeof(struct btrfs_disk_key)));
  127. BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
  128. btrfs_header_blocknr(&node->header));
  129. }
  130. BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
  131. for (i = 0; nritems > 1 && i < nritems - 2; i++) {
  132. struct btrfs_key cpukey;
  133. btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key);
  134. BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0);
  135. }
  136. return 0;
  137. }
  138. static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
  139. int level)
  140. {
  141. int i;
  142. struct btrfs_leaf *leaf = &path->nodes[level]->leaf;
  143. struct btrfs_node *parent = NULL;
  144. int parent_slot;
  145. u32 nritems = btrfs_header_nritems(&leaf->header);
  146. if (path->nodes[level + 1])
  147. parent = &path->nodes[level + 1]->node;
  148. parent_slot = path->slots[level + 1];
  149. BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
  150. if (nritems == 0)
  151. return 0;
  152. if (parent) {
  153. struct btrfs_disk_key *parent_key;
  154. parent_key = &parent->ptrs[parent_slot].key;
  155. BUG_ON(memcmp(parent_key, &leaf->items[0].key,
  156. sizeof(struct btrfs_disk_key)));
  157. BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
  158. btrfs_header_blocknr(&leaf->header));
  159. }
  160. for (i = 0; nritems > 1 && i < nritems - 2; i++) {
  161. struct btrfs_key cpukey;
  162. btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
  163. BUG_ON(comp_keys(&leaf->items[i].key,
  164. &cpukey) >= 0);
  165. BUG_ON(btrfs_item_offset(leaf->items + i) !=
  166. btrfs_item_end(leaf->items + i + 1));
  167. if (i == 0) {
  168. BUG_ON(btrfs_item_offset(leaf->items + i) +
  169. btrfs_item_size(leaf->items + i) !=
  170. BTRFS_LEAF_DATA_SIZE(root));
  171. }
  172. }
  173. return 0;
  174. }
  175. static int check_block(struct btrfs_root *root, struct btrfs_path *path,
  176. int level)
  177. {
  178. if (level == 0)
  179. return check_leaf(root, path, level);
  180. return check_node(root, path, level);
  181. }
  182. /*
  183. * search for key in the array p. items p are item_size apart
  184. * and there are 'max' items in p
  185. * the slot in the array is returned via slot, and it points to
  186. * the place where you would insert key if it is not found in
  187. * the array.
  188. *
  189. * slot may point to max if the key is bigger than all of the keys
  190. */
  191. static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
  192. int max, int *slot)
  193. {
  194. int low = 0;
  195. int high = max;
  196. int mid;
  197. int ret;
  198. struct btrfs_disk_key *tmp;
  199. while(low < high) {
  200. mid = (low + high) / 2;
  201. tmp = (struct btrfs_disk_key *)(p + mid * item_size);
  202. ret = comp_keys(tmp, key);
  203. if (ret < 0)
  204. low = mid + 1;
  205. else if (ret > 0)
  206. high = mid;
  207. else {
  208. *slot = mid;
  209. return 0;
  210. }
  211. }
  212. *slot = low;
  213. return 1;
  214. }
  215. /*
  216. * simple bin_search frontend that does the right thing for
  217. * leaves vs nodes
  218. */
  219. static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
  220. {
  221. if (btrfs_is_leaf(c)) {
  222. struct btrfs_leaf *l = (struct btrfs_leaf *)c;
  223. return generic_bin_search((void *)l->items,
  224. sizeof(struct btrfs_item),
  225. key, btrfs_header_nritems(&c->header),
  226. slot);
  227. } else {
  228. return generic_bin_search((void *)c->ptrs,
  229. sizeof(struct btrfs_key_ptr),
  230. key, btrfs_header_nritems(&c->header),
  231. slot);
  232. }
  233. return -1;
  234. }
  235. static struct btrfs_buffer *read_node_slot(struct btrfs_root *root,
  236. struct btrfs_buffer *parent_buf,
  237. int slot)
  238. {
  239. struct btrfs_node *node = &parent_buf->node;
  240. if (slot < 0)
  241. return NULL;
  242. if (slot >= btrfs_header_nritems(&node->header))
  243. return NULL;
  244. return read_tree_block(root, btrfs_node_blockptr(node, slot));
  245. }
  246. static int balance_level(struct btrfs_root *root, struct btrfs_path *path,
  247. int level)
  248. {
  249. struct btrfs_buffer *right_buf;
  250. struct btrfs_buffer *mid_buf;
  251. struct btrfs_buffer *left_buf;
  252. struct btrfs_buffer *parent_buf = NULL;
  253. struct btrfs_node *right = NULL;
  254. struct btrfs_node *mid;
  255. struct btrfs_node *left = NULL;
  256. struct btrfs_node *parent = NULL;
  257. int ret = 0;
  258. int wret;
  259. int pslot;
  260. int orig_slot = path->slots[level];
  261. u64 orig_ptr;
  262. if (level == 0)
  263. return 0;
  264. mid_buf = path->nodes[level];
  265. mid = &mid_buf->node;
  266. orig_ptr = btrfs_node_blockptr(mid, orig_slot);
  267. if (level < BTRFS_MAX_LEVEL - 1)
  268. parent_buf = path->nodes[level + 1];
  269. pslot = path->slots[level + 1];
  270. if (!parent_buf) {
  271. struct btrfs_buffer *child;
  272. u64 blocknr = mid_buf->blocknr;
  273. if (btrfs_header_nritems(&mid->header) != 1)
  274. return 0;
  275. /* promote the child to a root */
  276. child = read_node_slot(root, mid_buf, 0);
  277. BUG_ON(!child);
  278. root->node = child;
  279. path->nodes[level] = NULL;
  280. /* once for the path */
  281. btrfs_block_release(root, mid_buf);
  282. /* once for the root ptr */
  283. btrfs_block_release(root, mid_buf);
  284. clean_tree_block(root, mid_buf);
  285. return btrfs_free_extent(root, blocknr, 1, 1);
  286. }
  287. parent = &parent_buf->node;
  288. if (btrfs_header_nritems(&mid->header) >
  289. BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
  290. return 0;
  291. left_buf = read_node_slot(root, parent_buf, pslot - 1);
  292. right_buf = read_node_slot(root, parent_buf, pslot + 1);
  293. /* first, try to make some room in the middle buffer */
  294. if (left_buf) {
  295. btrfs_cow_block(root, left_buf, parent_buf,
  296. pslot - 1, &left_buf);
  297. left = &left_buf->node;
  298. orig_slot += btrfs_header_nritems(&left->header);
  299. wret = push_node_left(root, left_buf, mid_buf);
  300. if (wret < 0)
  301. ret = wret;
  302. }
  303. /*
  304. * then try to empty the right most buffer into the middle
  305. */
  306. if (right_buf) {
  307. btrfs_cow_block(root, right_buf, parent_buf,
  308. pslot + 1, &right_buf);
  309. right = &right_buf->node;
  310. wret = push_node_left(root, mid_buf, right_buf);
  311. if (wret < 0)
  312. ret = wret;
  313. if (btrfs_header_nritems(&right->header) == 0) {
  314. u64 blocknr = right_buf->blocknr;
  315. btrfs_block_release(root, right_buf);
  316. clean_tree_block(root, right_buf);
  317. right_buf = NULL;
  318. right = NULL;
  319. wret = del_ptr(root, path, level + 1, pslot + 1);
  320. if (wret)
  321. ret = wret;
  322. wret = btrfs_free_extent(root, blocknr, 1, 1);
  323. if (wret)
  324. ret = wret;
  325. } else {
  326. memcpy(&parent->ptrs[pslot + 1].key,
  327. &right->ptrs[0].key,
  328. sizeof(struct btrfs_disk_key));
  329. BUG_ON(list_empty(&parent_buf->dirty));
  330. }
  331. }
  332. if (btrfs_header_nritems(&mid->header) == 1) {
  333. /*
  334. * we're not allowed to leave a node with one item in the
  335. * tree during a delete. A deletion from lower in the tree
  336. * could try to delete the only pointer in this node.
  337. * So, pull some keys from the left.
  338. * There has to be a left pointer at this point because
  339. * otherwise we would have pulled some pointers from the
  340. * right
  341. */
  342. BUG_ON(!left_buf);
  343. wret = balance_node_right(root, mid_buf, left_buf);
  344. if (wret < 0)
  345. ret = wret;
  346. BUG_ON(wret == 1);
  347. }
  348. if (btrfs_header_nritems(&mid->header) == 0) {
  349. /* we've managed to empty the middle node, drop it */
  350. u64 blocknr = mid_buf->blocknr;
  351. btrfs_block_release(root, mid_buf);
  352. clean_tree_block(root, mid_buf);
  353. mid_buf = NULL;
  354. mid = NULL;
  355. wret = del_ptr(root, path, level + 1, pslot);
  356. if (wret)
  357. ret = wret;
  358. wret = btrfs_free_extent(root, blocknr, 1, 1);
  359. if (wret)
  360. ret = wret;
  361. } else {
  362. /* update the parent key to reflect our changes */
  363. memcpy(&parent->ptrs[pslot].key, &mid->ptrs[0].key,
  364. sizeof(struct btrfs_disk_key));
  365. BUG_ON(list_empty(&parent_buf->dirty));
  366. }
  367. /* update the path */
  368. if (left_buf) {
  369. if (btrfs_header_nritems(&left->header) > orig_slot) {
  370. left_buf->count++; // released below
  371. path->nodes[level] = left_buf;
  372. path->slots[level + 1] -= 1;
  373. path->slots[level] = orig_slot;
  374. if (mid_buf)
  375. btrfs_block_release(root, mid_buf);
  376. } else {
  377. orig_slot -= btrfs_header_nritems(&left->header);
  378. path->slots[level] = orig_slot;
  379. }
  380. }
  381. /* double check we haven't messed things up */
  382. check_block(root, path, level);
  383. if (orig_ptr != btrfs_node_blockptr(&path->nodes[level]->node,
  384. path->slots[level]))
  385. BUG();
  386. if (right_buf)
  387. btrfs_block_release(root, right_buf);
  388. if (left_buf)
  389. btrfs_block_release(root, left_buf);
  390. return ret;
  391. }
  392. /*
  393. * look for key in the tree. path is filled in with nodes along the way
  394. * if key is found, we return zero and you can find the item in the leaf
  395. * level of the path (level 0)
  396. *
  397. * If the key isn't found, the path points to the slot where it should
  398. * be inserted, and 1 is returned. If there are other errors during the
  399. * search a negative error number is returned.
  400. *
  401. * if ins_len > 0, nodes and leaves will be split as we walk down the
  402. * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
  403. * possible)
  404. */
  405. int btrfs_search_slot(struct btrfs_root *root, struct btrfs_key *key,
  406. struct btrfs_path *p, int ins_len, int cow)
  407. {
  408. struct btrfs_buffer *b;
  409. struct btrfs_buffer *cow_buf;
  410. struct btrfs_node *c;
  411. int slot;
  412. int ret;
  413. int level;
  414. again:
  415. b = root->node;
  416. b->count++;
  417. while (b) {
  418. level = btrfs_header_level(&b->node.header);
  419. if (cow) {
  420. int wret;
  421. wret = btrfs_cow_block(root, b, p->nodes[level + 1],
  422. p->slots[level + 1], &cow_buf);
  423. b = cow_buf;
  424. }
  425. BUG_ON(!cow && ins_len);
  426. c = &b->node;
  427. p->nodes[level] = b;
  428. ret = check_block(root, p, level);
  429. if (ret)
  430. return -1;
  431. ret = bin_search(c, key, &slot);
  432. if (!btrfs_is_leaf(c)) {
  433. if (ret && slot > 0)
  434. slot -= 1;
  435. p->slots[level] = slot;
  436. if (ins_len > 0 && btrfs_header_nritems(&c->header) ==
  437. BTRFS_NODEPTRS_PER_BLOCK(root)) {
  438. int sret = split_node(root, p, level);
  439. BUG_ON(sret > 0);
  440. if (sret)
  441. return sret;
  442. b = p->nodes[level];
  443. c = &b->node;
  444. slot = p->slots[level];
  445. } else if (ins_len < 0) {
  446. int sret = balance_level(root, p, level);
  447. if (sret)
  448. return sret;
  449. b = p->nodes[level];
  450. if (!b)
  451. goto again;
  452. c = &b->node;
  453. slot = p->slots[level];
  454. BUG_ON(btrfs_header_nritems(&c->header) == 1);
  455. }
  456. b = read_tree_block(root, btrfs_node_blockptr(c, slot));
  457. } else {
  458. struct btrfs_leaf *l = (struct btrfs_leaf *)c;
  459. p->slots[level] = slot;
  460. if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
  461. sizeof(struct btrfs_item) + ins_len) {
  462. int sret = split_leaf(root, p, ins_len);
  463. BUG_ON(sret > 0);
  464. if (sret)
  465. return sret;
  466. }
  467. BUG_ON(root->node->count == 1);
  468. return ret;
  469. }
  470. }
  471. BUG_ON(root->node->count == 1);
  472. return 1;
  473. }
  474. /*
  475. * adjust the pointers going up the tree, starting at level
  476. * making sure the right key of each node is points to 'key'.
  477. * This is used after shifting pointers to the left, so it stops
  478. * fixing up pointers when a given leaf/node is not in slot 0 of the
  479. * higher levels
  480. *
  481. * If this fails to write a tree block, it returns -1, but continues
  482. * fixing up the blocks in ram so the tree is consistent.
  483. */
  484. static int fixup_low_keys(struct btrfs_root *root,
  485. struct btrfs_path *path, struct btrfs_disk_key *key,
  486. int level)
  487. {
  488. int i;
  489. int ret = 0;
  490. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  491. struct btrfs_node *t;
  492. int tslot = path->slots[i];
  493. if (!path->nodes[i])
  494. break;
  495. t = &path->nodes[i]->node;
  496. memcpy(&t->ptrs[tslot].key, key, sizeof(*key));
  497. BUG_ON(list_empty(&path->nodes[i]->dirty));
  498. if (tslot != 0)
  499. break;
  500. }
  501. return ret;
  502. }
  503. /*
  504. * try to push data from one node into the next node left in the
  505. * tree.
  506. *
  507. * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
  508. * error, and > 0 if there was no room in the left hand block.
  509. */
  510. static int push_node_left(struct btrfs_root *root, struct btrfs_buffer *dst_buf,
  511. struct btrfs_buffer *src_buf)
  512. {
  513. struct btrfs_node *src = &src_buf->node;
  514. struct btrfs_node *dst = &dst_buf->node;
  515. int push_items = 0;
  516. int src_nritems;
  517. int dst_nritems;
  518. int ret = 0;
  519. src_nritems = btrfs_header_nritems(&src->header);
  520. dst_nritems = btrfs_header_nritems(&dst->header);
  521. push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
  522. if (push_items <= 0) {
  523. return 1;
  524. }
  525. if (src_nritems < push_items)
  526. push_items = src_nritems;
  527. memcpy(dst->ptrs + dst_nritems, src->ptrs,
  528. push_items * sizeof(struct btrfs_key_ptr));
  529. if (push_items < src_nritems) {
  530. memmove(src->ptrs, src->ptrs + push_items,
  531. (src_nritems - push_items) *
  532. sizeof(struct btrfs_key_ptr));
  533. }
  534. btrfs_set_header_nritems(&src->header, src_nritems - push_items);
  535. btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
  536. BUG_ON(list_empty(&src_buf->dirty));
  537. BUG_ON(list_empty(&dst_buf->dirty));
  538. return ret;
  539. }
  540. /*
  541. * try to push data from one node into the next node right in the
  542. * tree.
  543. *
  544. * returns 0 if some ptrs were pushed, < 0 if there was some horrible
  545. * error, and > 0 if there was no room in the right hand block.
  546. *
  547. * this will only push up to 1/2 the contents of the left node over
  548. */
  549. static int balance_node_right(struct btrfs_root *root,
  550. struct btrfs_buffer *dst_buf,
  551. struct btrfs_buffer *src_buf)
  552. {
  553. struct btrfs_node *src = &src_buf->node;
  554. struct btrfs_node *dst = &dst_buf->node;
  555. int push_items = 0;
  556. int max_push;
  557. int src_nritems;
  558. int dst_nritems;
  559. int ret = 0;
  560. src_nritems = btrfs_header_nritems(&src->header);
  561. dst_nritems = btrfs_header_nritems(&dst->header);
  562. push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
  563. if (push_items <= 0) {
  564. return 1;
  565. }
  566. max_push = src_nritems / 2 + 1;
  567. /* don't try to empty the node */
  568. if (max_push > src_nritems)
  569. return 1;
  570. if (max_push < push_items)
  571. push_items = max_push;
  572. memmove(dst->ptrs + push_items, dst->ptrs,
  573. dst_nritems * sizeof(struct btrfs_key_ptr));
  574. memcpy(dst->ptrs, src->ptrs + src_nritems - push_items,
  575. push_items * sizeof(struct btrfs_key_ptr));
  576. btrfs_set_header_nritems(&src->header, src_nritems - push_items);
  577. btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
  578. BUG_ON(list_empty(&src_buf->dirty));
  579. BUG_ON(list_empty(&dst_buf->dirty));
  580. return ret;
  581. }
  582. /*
  583. * helper function to insert a new root level in the tree.
  584. * A new node is allocated, and a single item is inserted to
  585. * point to the existing root
  586. *
  587. * returns zero on success or < 0 on failure.
  588. */
  589. static int insert_new_root(struct btrfs_root *root,
  590. struct btrfs_path *path, int level)
  591. {
  592. struct btrfs_buffer *t;
  593. struct btrfs_node *lower;
  594. struct btrfs_node *c;
  595. struct btrfs_disk_key *lower_key;
  596. BUG_ON(path->nodes[level]);
  597. BUG_ON(path->nodes[level-1] != root->node);
  598. t = btrfs_alloc_free_block(root);
  599. c = &t->node;
  600. memset(c, 0, root->blocksize);
  601. btrfs_set_header_nritems(&c->header, 1);
  602. btrfs_set_header_level(&c->header, level);
  603. btrfs_set_header_blocknr(&c->header, t->blocknr);
  604. btrfs_set_header_parentid(&c->header,
  605. btrfs_header_parentid(&root->node->node.header));
  606. lower = &path->nodes[level-1]->node;
  607. if (btrfs_is_leaf(lower))
  608. lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
  609. else
  610. lower_key = &lower->ptrs[0].key;
  611. memcpy(&c->ptrs[0].key, lower_key, sizeof(struct btrfs_disk_key));
  612. btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->blocknr);
  613. /* the super has an extra ref to root->node */
  614. btrfs_block_release(root, root->node);
  615. root->node = t;
  616. t->count++;
  617. path->nodes[level] = t;
  618. path->slots[level] = 0;
  619. return 0;
  620. }
  621. /*
  622. * worker function to insert a single pointer in a node.
  623. * the node should have enough room for the pointer already
  624. *
  625. * slot and level indicate where you want the key to go, and
  626. * blocknr is the block the key points to.
  627. *
  628. * returns zero on success and < 0 on any error
  629. */
  630. static int insert_ptr(struct btrfs_root *root,
  631. struct btrfs_path *path, struct btrfs_disk_key *key,
  632. u64 blocknr, int slot, int level)
  633. {
  634. struct btrfs_node *lower;
  635. int nritems;
  636. BUG_ON(!path->nodes[level]);
  637. lower = &path->nodes[level]->node;
  638. nritems = btrfs_header_nritems(&lower->header);
  639. if (slot > nritems)
  640. BUG();
  641. if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
  642. BUG();
  643. if (slot != nritems) {
  644. memmove(lower->ptrs + slot + 1, lower->ptrs + slot,
  645. (nritems - slot) * sizeof(struct btrfs_key_ptr));
  646. }
  647. memcpy(&lower->ptrs[slot].key, key, sizeof(struct btrfs_disk_key));
  648. btrfs_set_node_blockptr(lower, slot, blocknr);
  649. btrfs_set_header_nritems(&lower->header, nritems + 1);
  650. BUG_ON(list_empty(&path->nodes[level]->dirty));
  651. return 0;
  652. }
  653. /*
  654. * split the node at the specified level in path in two.
  655. * The path is corrected to point to the appropriate node after the split
  656. *
  657. * Before splitting this tries to make some room in the node by pushing
  658. * left and right, if either one works, it returns right away.
  659. *
  660. * returns 0 on success and < 0 on failure
  661. */
  662. static int split_node(struct btrfs_root *root, struct btrfs_path *path,
  663. int level)
  664. {
  665. struct btrfs_buffer *t;
  666. struct btrfs_node *c;
  667. struct btrfs_buffer *split_buffer;
  668. struct btrfs_node *split;
  669. int mid;
  670. int ret;
  671. int wret;
  672. u32 c_nritems;
  673. t = path->nodes[level];
  674. c = &t->node;
  675. if (t == root->node) {
  676. /* trying to split the root, lets make a new one */
  677. ret = insert_new_root(root, path, level + 1);
  678. if (ret)
  679. return ret;
  680. }
  681. c_nritems = btrfs_header_nritems(&c->header);
  682. split_buffer = btrfs_alloc_free_block(root);
  683. split = &split_buffer->node;
  684. btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
  685. btrfs_set_header_blocknr(&split->header, split_buffer->blocknr);
  686. btrfs_set_header_parentid(&split->header,
  687. btrfs_header_parentid(&root->node->node.header));
  688. mid = (c_nritems + 1) / 2;
  689. memcpy(split->ptrs, c->ptrs + mid,
  690. (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
  691. btrfs_set_header_nritems(&split->header, c_nritems - mid);
  692. btrfs_set_header_nritems(&c->header, mid);
  693. ret = 0;
  694. BUG_ON(list_empty(&t->dirty));
  695. wret = insert_ptr(root, path, &split->ptrs[0].key,
  696. split_buffer->blocknr, path->slots[level + 1] + 1,
  697. level + 1);
  698. if (wret)
  699. ret = wret;
  700. if (path->slots[level] >= mid) {
  701. path->slots[level] -= mid;
  702. btrfs_block_release(root, t);
  703. path->nodes[level] = split_buffer;
  704. path->slots[level + 1] += 1;
  705. } else {
  706. btrfs_block_release(root, split_buffer);
  707. }
  708. return ret;
  709. }
  710. /*
  711. * how many bytes are required to store the items in a leaf. start
  712. * and nr indicate which items in the leaf to check. This totals up the
  713. * space used both by the item structs and the item data
  714. */
  715. static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
  716. {
  717. int data_len;
  718. int end = start + nr - 1;
  719. if (!nr)
  720. return 0;
  721. data_len = btrfs_item_end(l->items + start);
  722. data_len = data_len - btrfs_item_offset(l->items + end);
  723. data_len += sizeof(struct btrfs_item) * nr;
  724. return data_len;
  725. }
  726. /*
  727. * push some data in the path leaf to the right, trying to free up at
  728. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  729. *
  730. * returns 1 if the push failed because the other node didn't have enough
  731. * room, 0 if everything worked out and < 0 if there were major errors.
  732. */
  733. static int push_leaf_right(struct btrfs_root *root, struct btrfs_path *path,
  734. int data_size)
  735. {
  736. struct btrfs_buffer *left_buf = path->nodes[0];
  737. struct btrfs_leaf *left = &left_buf->leaf;
  738. struct btrfs_leaf *right;
  739. struct btrfs_buffer *right_buf;
  740. struct btrfs_buffer *upper;
  741. int slot;
  742. int i;
  743. int free_space;
  744. int push_space = 0;
  745. int push_items = 0;
  746. struct btrfs_item *item;
  747. u32 left_nritems;
  748. u32 right_nritems;
  749. slot = path->slots[1];
  750. if (!path->nodes[1]) {
  751. return 1;
  752. }
  753. upper = path->nodes[1];
  754. if (slot >= btrfs_header_nritems(&upper->node.header) - 1) {
  755. return 1;
  756. }
  757. right_buf = read_tree_block(root, btrfs_node_blockptr(&upper->node,
  758. slot + 1));
  759. right = &right_buf->leaf;
  760. free_space = btrfs_leaf_free_space(root, right);
  761. if (free_space < data_size + sizeof(struct btrfs_item)) {
  762. btrfs_block_release(root, right_buf);
  763. return 1;
  764. }
  765. /* cow and double check */
  766. btrfs_cow_block(root, right_buf, upper, slot + 1, &right_buf);
  767. right = &right_buf->leaf;
  768. free_space = btrfs_leaf_free_space(root, right);
  769. if (free_space < data_size + sizeof(struct btrfs_item)) {
  770. btrfs_block_release(root, right_buf);
  771. return 1;
  772. }
  773. left_nritems = btrfs_header_nritems(&left->header);
  774. for (i = left_nritems - 1; i >= 0; i--) {
  775. item = left->items + i;
  776. if (path->slots[0] == i)
  777. push_space += data_size + sizeof(*item);
  778. if (btrfs_item_size(item) + sizeof(*item) + push_space >
  779. free_space)
  780. break;
  781. push_items++;
  782. push_space += btrfs_item_size(item) + sizeof(*item);
  783. }
  784. if (push_items == 0) {
  785. btrfs_block_release(root, right_buf);
  786. return 1;
  787. }
  788. right_nritems = btrfs_header_nritems(&right->header);
  789. /* push left to right */
  790. push_space = btrfs_item_end(left->items + left_nritems - push_items);
  791. push_space -= leaf_data_end(root, left);
  792. /* make room in the right data area */
  793. memmove(btrfs_leaf_data(right) + leaf_data_end(root, right) -
  794. push_space, btrfs_leaf_data(right) + leaf_data_end(root, right),
  795. BTRFS_LEAF_DATA_SIZE(root) - leaf_data_end(root, right));
  796. /* copy from the left data area */
  797. memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - push_space,
  798. btrfs_leaf_data(left) + leaf_data_end(root, left), push_space);
  799. memmove(right->items + push_items, right->items,
  800. right_nritems * sizeof(struct btrfs_item));
  801. /* copy the items from left to right */
  802. memcpy(right->items, left->items + left_nritems - push_items,
  803. push_items * sizeof(struct btrfs_item));
  804. /* update the item pointers */
  805. right_nritems += push_items;
  806. btrfs_set_header_nritems(&right->header, right_nritems);
  807. push_space = BTRFS_LEAF_DATA_SIZE(root);
  808. for (i = 0; i < right_nritems; i++) {
  809. btrfs_set_item_offset(right->items + i, push_space -
  810. btrfs_item_size(right->items + i));
  811. push_space = btrfs_item_offset(right->items + i);
  812. }
  813. left_nritems -= push_items;
  814. btrfs_set_header_nritems(&left->header, left_nritems);
  815. BUG_ON(list_empty(&left_buf->dirty));
  816. BUG_ON(list_empty(&right_buf->dirty));
  817. memcpy(&upper->node.ptrs[slot + 1].key,
  818. &right->items[0].key, sizeof(struct btrfs_disk_key));
  819. BUG_ON(list_empty(&upper->dirty));
  820. /* then fixup the leaf pointer in the path */
  821. if (path->slots[0] >= left_nritems) {
  822. path->slots[0] -= left_nritems;
  823. btrfs_block_release(root, path->nodes[0]);
  824. path->nodes[0] = right_buf;
  825. path->slots[1] += 1;
  826. } else {
  827. btrfs_block_release(root, right_buf);
  828. }
  829. return 0;
  830. }
  831. /*
  832. * push some data in the path leaf to the left, trying to free up at
  833. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  834. */
  835. static int push_leaf_left(struct btrfs_root *root, struct btrfs_path *path,
  836. int data_size)
  837. {
  838. struct btrfs_buffer *right_buf = path->nodes[0];
  839. struct btrfs_leaf *right = &right_buf->leaf;
  840. struct btrfs_buffer *t;
  841. struct btrfs_leaf *left;
  842. int slot;
  843. int i;
  844. int free_space;
  845. int push_space = 0;
  846. int push_items = 0;
  847. struct btrfs_item *item;
  848. u32 old_left_nritems;
  849. int ret = 0;
  850. int wret;
  851. slot = path->slots[1];
  852. if (slot == 0) {
  853. return 1;
  854. }
  855. if (!path->nodes[1]) {
  856. return 1;
  857. }
  858. t = read_tree_block(root, btrfs_node_blockptr(&path->nodes[1]->node,
  859. slot - 1));
  860. left = &t->leaf;
  861. free_space = btrfs_leaf_free_space(root, left);
  862. if (free_space < data_size + sizeof(struct btrfs_item)) {
  863. btrfs_block_release(root, t);
  864. return 1;
  865. }
  866. /* cow and double check */
  867. btrfs_cow_block(root, t, path->nodes[1], slot - 1, &t);
  868. left = &t->leaf;
  869. free_space = btrfs_leaf_free_space(root, left);
  870. if (free_space < data_size + sizeof(struct btrfs_item)) {
  871. btrfs_block_release(root, t);
  872. return 1;
  873. }
  874. for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
  875. item = right->items + i;
  876. if (path->slots[0] == i)
  877. push_space += data_size + sizeof(*item);
  878. if (btrfs_item_size(item) + sizeof(*item) + push_space >
  879. free_space)
  880. break;
  881. push_items++;
  882. push_space += btrfs_item_size(item) + sizeof(*item);
  883. }
  884. if (push_items == 0) {
  885. btrfs_block_release(root, t);
  886. return 1;
  887. }
  888. /* push data from right to left */
  889. memcpy(left->items + btrfs_header_nritems(&left->header),
  890. right->items, push_items * sizeof(struct btrfs_item));
  891. push_space = BTRFS_LEAF_DATA_SIZE(root) -
  892. btrfs_item_offset(right->items + push_items -1);
  893. memcpy(btrfs_leaf_data(left) + leaf_data_end(root, left) - push_space,
  894. btrfs_leaf_data(right) +
  895. btrfs_item_offset(right->items + push_items - 1),
  896. push_space);
  897. old_left_nritems = btrfs_header_nritems(&left->header);
  898. BUG_ON(old_left_nritems < 0);
  899. for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
  900. u32 ioff = btrfs_item_offset(left->items + i);
  901. btrfs_set_item_offset(left->items + i, ioff -
  902. (BTRFS_LEAF_DATA_SIZE(root) -
  903. btrfs_item_offset(left->items +
  904. old_left_nritems - 1)));
  905. }
  906. btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
  907. /* fixup right node */
  908. push_space = btrfs_item_offset(right->items + push_items - 1) -
  909. leaf_data_end(root, right);
  910. memmove(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
  911. push_space, btrfs_leaf_data(right) +
  912. leaf_data_end(root, right), push_space);
  913. memmove(right->items, right->items + push_items,
  914. (btrfs_header_nritems(&right->header) - push_items) *
  915. sizeof(struct btrfs_item));
  916. btrfs_set_header_nritems(&right->header,
  917. btrfs_header_nritems(&right->header) -
  918. push_items);
  919. push_space = BTRFS_LEAF_DATA_SIZE(root);
  920. for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
  921. btrfs_set_item_offset(right->items + i, push_space -
  922. btrfs_item_size(right->items + i));
  923. push_space = btrfs_item_offset(right->items + i);
  924. }
  925. BUG_ON(list_empty(&t->dirty));
  926. BUG_ON(list_empty(&right_buf->dirty));
  927. wret = fixup_low_keys(root, path, &right->items[0].key, 1);
  928. if (wret)
  929. ret = wret;
  930. /* then fixup the leaf pointer in the path */
  931. if (path->slots[0] < push_items) {
  932. path->slots[0] += old_left_nritems;
  933. btrfs_block_release(root, path->nodes[0]);
  934. path->nodes[0] = t;
  935. path->slots[1] -= 1;
  936. } else {
  937. btrfs_block_release(root, t);
  938. path->slots[0] -= push_items;
  939. }
  940. BUG_ON(path->slots[0] < 0);
  941. return ret;
  942. }
  943. /*
  944. * split the path's leaf in two, making sure there is at least data_size
  945. * available for the resulting leaf level of the path.
  946. *
  947. * returns 0 if all went well and < 0 on failure.
  948. */
  949. static int split_leaf(struct btrfs_root *root, struct btrfs_path *path,
  950. int data_size)
  951. {
  952. struct btrfs_buffer *l_buf;
  953. struct btrfs_leaf *l;
  954. u32 nritems;
  955. int mid;
  956. int slot;
  957. struct btrfs_leaf *right;
  958. struct btrfs_buffer *right_buffer;
  959. int space_needed = data_size + sizeof(struct btrfs_item);
  960. int data_copy_size;
  961. int rt_data_off;
  962. int i;
  963. int ret;
  964. int wret;
  965. wret = push_leaf_left(root, path, data_size);
  966. if (wret < 0)
  967. return wret;
  968. if (wret) {
  969. wret = push_leaf_right(root, path, data_size);
  970. if (wret < 0)
  971. return wret;
  972. }
  973. l_buf = path->nodes[0];
  974. l = &l_buf->leaf;
  975. /* did the pushes work? */
  976. if (btrfs_leaf_free_space(root, l) >=
  977. sizeof(struct btrfs_item) + data_size)
  978. return 0;
  979. if (!path->nodes[1]) {
  980. ret = insert_new_root(root, path, 1);
  981. if (ret)
  982. return ret;
  983. }
  984. slot = path->slots[0];
  985. nritems = btrfs_header_nritems(&l->header);
  986. mid = (nritems + 1)/ 2;
  987. right_buffer = btrfs_alloc_free_block(root);
  988. BUG_ON(!right_buffer);
  989. BUG_ON(mid == nritems);
  990. right = &right_buffer->leaf;
  991. memset(&right->header, 0, sizeof(right->header));
  992. if (mid <= slot) {
  993. /* FIXME, just alloc a new leaf here */
  994. if (leaf_space_used(l, mid, nritems - mid) + space_needed >
  995. BTRFS_LEAF_DATA_SIZE(root))
  996. BUG();
  997. } else {
  998. /* FIXME, just alloc a new leaf here */
  999. if (leaf_space_used(l, 0, mid + 1) + space_needed >
  1000. BTRFS_LEAF_DATA_SIZE(root))
  1001. BUG();
  1002. }
  1003. btrfs_set_header_nritems(&right->header, nritems - mid);
  1004. btrfs_set_header_blocknr(&right->header, right_buffer->blocknr);
  1005. btrfs_set_header_level(&right->header, 0);
  1006. btrfs_set_header_parentid(&right->header,
  1007. btrfs_header_parentid(&root->node->node.header));
  1008. data_copy_size = btrfs_item_end(l->items + mid) -
  1009. leaf_data_end(root, l);
  1010. memcpy(right->items, l->items + mid,
  1011. (nritems - mid) * sizeof(struct btrfs_item));
  1012. memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
  1013. data_copy_size, btrfs_leaf_data(l) +
  1014. leaf_data_end(root, l), data_copy_size);
  1015. rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
  1016. btrfs_item_end(l->items + mid);
  1017. for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
  1018. u32 ioff = btrfs_item_offset(right->items + i);
  1019. btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
  1020. }
  1021. btrfs_set_header_nritems(&l->header, mid);
  1022. ret = 0;
  1023. wret = insert_ptr(root, path, &right->items[0].key,
  1024. right_buffer->blocknr, path->slots[1] + 1, 1);
  1025. if (wret)
  1026. ret = wret;
  1027. BUG_ON(list_empty(&right_buffer->dirty));
  1028. BUG_ON(list_empty(&l_buf->dirty));
  1029. BUG_ON(path->slots[0] != slot);
  1030. if (mid <= slot) {
  1031. btrfs_block_release(root, path->nodes[0]);
  1032. path->nodes[0] = right_buffer;
  1033. path->slots[0] -= mid;
  1034. path->slots[1] += 1;
  1035. } else
  1036. btrfs_block_release(root, right_buffer);
  1037. BUG_ON(path->slots[0] < 0);
  1038. return ret;
  1039. }
  1040. /*
  1041. * Given a key and some data, insert an item into the tree.
  1042. * This does all the path init required, making room in the tree if needed.
  1043. */
  1044. int btrfs_insert_empty_item(struct btrfs_root *root, struct btrfs_path *path,
  1045. struct btrfs_key *cpu_key, u32 data_size)
  1046. {
  1047. int ret = 0;
  1048. int slot;
  1049. int slot_orig;
  1050. struct btrfs_leaf *leaf;
  1051. struct btrfs_buffer *leaf_buf;
  1052. u32 nritems;
  1053. unsigned int data_end;
  1054. struct btrfs_disk_key disk_key;
  1055. btrfs_cpu_key_to_disk(&disk_key, cpu_key);
  1056. /* create a root if there isn't one */
  1057. if (!root->node)
  1058. BUG();
  1059. ret = btrfs_search_slot(root, cpu_key, path, data_size, 1);
  1060. if (ret == 0) {
  1061. btrfs_release_path(root, path);
  1062. return -EEXIST;
  1063. }
  1064. if (ret < 0)
  1065. goto out;
  1066. slot_orig = path->slots[0];
  1067. leaf_buf = path->nodes[0];
  1068. leaf = &leaf_buf->leaf;
  1069. nritems = btrfs_header_nritems(&leaf->header);
  1070. data_end = leaf_data_end(root, leaf);
  1071. if (btrfs_leaf_free_space(root, leaf) <
  1072. sizeof(struct btrfs_item) + data_size)
  1073. BUG();
  1074. slot = path->slots[0];
  1075. BUG_ON(slot < 0);
  1076. if (slot != nritems) {
  1077. int i;
  1078. unsigned int old_data = btrfs_item_end(leaf->items + slot);
  1079. /*
  1080. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  1081. */
  1082. /* first correct the data pointers */
  1083. for (i = slot; i < nritems; i++) {
  1084. u32 ioff = btrfs_item_offset(leaf->items + i);
  1085. btrfs_set_item_offset(leaf->items + i,
  1086. ioff - data_size);
  1087. }
  1088. /* shift the items */
  1089. memmove(leaf->items + slot + 1, leaf->items + slot,
  1090. (nritems - slot) * sizeof(struct btrfs_item));
  1091. /* shift the data */
  1092. memmove(btrfs_leaf_data(leaf) + data_end - data_size,
  1093. btrfs_leaf_data(leaf) +
  1094. data_end, old_data - data_end);
  1095. data_end = old_data;
  1096. }
  1097. /* setup the item for the new data */
  1098. memcpy(&leaf->items[slot].key, &disk_key,
  1099. sizeof(struct btrfs_disk_key));
  1100. btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
  1101. btrfs_set_item_size(leaf->items + slot, data_size);
  1102. btrfs_set_header_nritems(&leaf->header, nritems + 1);
  1103. ret = 0;
  1104. if (slot == 0)
  1105. ret = fixup_low_keys(root, path, &disk_key, 1);
  1106. BUG_ON(list_empty(&leaf_buf->dirty));
  1107. if (btrfs_leaf_free_space(root, leaf) < 0)
  1108. BUG();
  1109. check_leaf(root, path, 0);
  1110. out:
  1111. return ret;
  1112. }
  1113. /*
  1114. * Given a key and some data, insert an item into the tree.
  1115. * This does all the path init required, making room in the tree if needed.
  1116. */
  1117. int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *cpu_key,
  1118. void *data, u32 data_size)
  1119. {
  1120. int ret = 0;
  1121. struct btrfs_path path;
  1122. u8 *ptr;
  1123. btrfs_init_path(&path);
  1124. ret = btrfs_insert_empty_item(root, &path, cpu_key, data_size);
  1125. if (!ret) {
  1126. ptr = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0], u8);
  1127. memcpy(ptr, data, data_size);
  1128. }
  1129. btrfs_release_path(root, &path);
  1130. return ret;
  1131. }
  1132. /*
  1133. * delete the pointer from a given node.
  1134. *
  1135. * If the delete empties a node, the node is removed from the tree,
  1136. * continuing all the way the root if required. The root is converted into
  1137. * a leaf if all the nodes are emptied.
  1138. */
  1139. static int del_ptr(struct btrfs_root *root, struct btrfs_path *path, int level,
  1140. int slot)
  1141. {
  1142. struct btrfs_node *node;
  1143. struct btrfs_buffer *parent = path->nodes[level];
  1144. u32 nritems;
  1145. int ret = 0;
  1146. int wret;
  1147. node = &parent->node;
  1148. nritems = btrfs_header_nritems(&node->header);
  1149. if (slot != nritems -1) {
  1150. memmove(node->ptrs + slot, node->ptrs + slot + 1,
  1151. sizeof(struct btrfs_key_ptr) * (nritems - slot - 1));
  1152. }
  1153. nritems--;
  1154. btrfs_set_header_nritems(&node->header, nritems);
  1155. if (nritems == 0 && parent == root->node) {
  1156. BUG_ON(btrfs_header_level(&root->node->node.header) != 1);
  1157. /* just turn the root into a leaf and break */
  1158. btrfs_set_header_level(&root->node->node.header, 0);
  1159. } else if (slot == 0) {
  1160. wret = fixup_low_keys(root, path, &node->ptrs[0].key,
  1161. level + 1);
  1162. if (wret)
  1163. ret = wret;
  1164. }
  1165. BUG_ON(list_empty(&parent->dirty));
  1166. return ret;
  1167. }
  1168. /*
  1169. * delete the item at the leaf level in path. If that empties
  1170. * the leaf, remove it from the tree
  1171. */
  1172. int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path)
  1173. {
  1174. int slot;
  1175. struct btrfs_leaf *leaf;
  1176. struct btrfs_buffer *leaf_buf;
  1177. int doff;
  1178. int dsize;
  1179. int ret = 0;
  1180. int wret;
  1181. u32 nritems;
  1182. leaf_buf = path->nodes[0];
  1183. leaf = &leaf_buf->leaf;
  1184. slot = path->slots[0];
  1185. doff = btrfs_item_offset(leaf->items + slot);
  1186. dsize = btrfs_item_size(leaf->items + slot);
  1187. nritems = btrfs_header_nritems(&leaf->header);
  1188. if (slot != nritems - 1) {
  1189. int i;
  1190. int data_end = leaf_data_end(root, leaf);
  1191. memmove(btrfs_leaf_data(leaf) + data_end + dsize,
  1192. btrfs_leaf_data(leaf) + data_end,
  1193. doff - data_end);
  1194. for (i = slot + 1; i < nritems; i++) {
  1195. u32 ioff = btrfs_item_offset(leaf->items + i);
  1196. btrfs_set_item_offset(leaf->items + i, ioff + dsize);
  1197. }
  1198. memmove(leaf->items + slot, leaf->items + slot + 1,
  1199. sizeof(struct btrfs_item) *
  1200. (nritems - slot - 1));
  1201. }
  1202. btrfs_set_header_nritems(&leaf->header, nritems - 1);
  1203. nritems--;
  1204. /* delete the leaf if we've emptied it */
  1205. if (nritems == 0) {
  1206. if (leaf_buf == root->node) {
  1207. btrfs_set_header_level(&leaf->header, 0);
  1208. BUG_ON(list_empty(&leaf_buf->dirty));
  1209. } else {
  1210. clean_tree_block(root, leaf_buf);
  1211. wret = del_ptr(root, path, 1, path->slots[1]);
  1212. if (wret)
  1213. ret = wret;
  1214. wret = btrfs_free_extent(root, leaf_buf->blocknr, 1, 1);
  1215. if (wret)
  1216. ret = wret;
  1217. }
  1218. } else {
  1219. int used = leaf_space_used(leaf, 0, nritems);
  1220. if (slot == 0) {
  1221. wret = fixup_low_keys(root, path,
  1222. &leaf->items[0].key, 1);
  1223. if (wret)
  1224. ret = wret;
  1225. }
  1226. BUG_ON(list_empty(&leaf_buf->dirty));
  1227. /* delete the leaf if it is mostly empty */
  1228. if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
  1229. /* push_leaf_left fixes the path.
  1230. * make sure the path still points to our leaf
  1231. * for possible call to del_ptr below
  1232. */
  1233. slot = path->slots[1];
  1234. leaf_buf->count++;
  1235. wret = push_leaf_left(root, path, 1);
  1236. if (wret < 0)
  1237. ret = wret;
  1238. if (path->nodes[0] == leaf_buf &&
  1239. btrfs_header_nritems(&leaf->header)) {
  1240. wret = push_leaf_right(root, path, 1);
  1241. if (wret < 0)
  1242. ret = wret;
  1243. }
  1244. if (btrfs_header_nritems(&leaf->header) == 0) {
  1245. u64 blocknr = leaf_buf->blocknr;
  1246. clean_tree_block(root, leaf_buf);
  1247. wret = del_ptr(root, path, 1, slot);
  1248. if (wret)
  1249. ret = wret;
  1250. btrfs_block_release(root, leaf_buf);
  1251. wret = btrfs_free_extent(root, blocknr, 1, 1);
  1252. if (wret)
  1253. ret = wret;
  1254. } else {
  1255. btrfs_block_release(root, leaf_buf);
  1256. }
  1257. }
  1258. }
  1259. return ret;
  1260. }
  1261. /*
  1262. * walk up the tree as far as required to find the next leaf.
  1263. * returns 0 if it found something or 1 if there are no greater leaves.
  1264. * returns < 0 on io errors.
  1265. */
  1266. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
  1267. {
  1268. int slot;
  1269. int level = 1;
  1270. u64 blocknr;
  1271. struct btrfs_buffer *c;
  1272. struct btrfs_buffer *next = NULL;
  1273. while(level < BTRFS_MAX_LEVEL) {
  1274. if (!path->nodes[level])
  1275. return 1;
  1276. slot = path->slots[level] + 1;
  1277. c = path->nodes[level];
  1278. if (slot >= btrfs_header_nritems(&c->node.header)) {
  1279. level++;
  1280. continue;
  1281. }
  1282. blocknr = btrfs_node_blockptr(&c->node, slot);
  1283. if (next)
  1284. btrfs_block_release(root, next);
  1285. next = read_tree_block(root, blocknr);
  1286. break;
  1287. }
  1288. path->slots[level] = slot;
  1289. while(1) {
  1290. level--;
  1291. c = path->nodes[level];
  1292. btrfs_block_release(root, c);
  1293. path->nodes[level] = next;
  1294. path->slots[level] = 0;
  1295. if (!level)
  1296. break;
  1297. next = read_tree_block(root,
  1298. btrfs_node_blockptr(&next->node, 0));
  1299. }
  1300. return 0;
  1301. }