ctree.c 43 KB

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