ctree.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  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_key *ins_key,
  9. struct btrfs_path *path, int data_size);
  10. static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
  11. *root, struct buffer_head *dst, struct buffer_head
  12. *src);
  13. static int balance_node_right(struct btrfs_trans_handle *trans, struct
  14. btrfs_root *root, struct buffer_head *dst_buf,
  15. struct buffer_head *src_buf);
  16. static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  17. struct btrfs_path *path, int level, int slot);
  18. inline void btrfs_init_path(struct btrfs_path *p)
  19. {
  20. memset(p, 0, sizeof(*p));
  21. }
  22. struct btrfs_path *btrfs_alloc_path(void)
  23. {
  24. struct btrfs_path *path;
  25. path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
  26. if (path)
  27. btrfs_init_path(path);
  28. return path;
  29. }
  30. void btrfs_free_path(struct btrfs_path *p)
  31. {
  32. btrfs_release_path(NULL, p);
  33. kmem_cache_free(btrfs_path_cachep, p);
  34. }
  35. void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
  36. {
  37. int i;
  38. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  39. if (!p->nodes[i])
  40. break;
  41. btrfs_block_release(root, p->nodes[i]);
  42. }
  43. memset(p, 0, sizeof(*p));
  44. }
  45. static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
  46. *root, struct buffer_head *buf, struct buffer_head
  47. *parent, int parent_slot, struct buffer_head
  48. **cow_ret)
  49. {
  50. struct buffer_head *cow;
  51. struct btrfs_node *cow_node;
  52. if (btrfs_header_generation(btrfs_buffer_header(buf)) ==
  53. trans->transid) {
  54. *cow_ret = buf;
  55. return 0;
  56. }
  57. cow = btrfs_alloc_free_block(trans, root);
  58. cow_node = btrfs_buffer_node(cow);
  59. if (buf->b_size != root->blocksize || cow->b_size != root->blocksize)
  60. WARN_ON(1);
  61. memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize);
  62. btrfs_set_header_blocknr(&cow_node->header, cow->b_blocknr);
  63. btrfs_set_header_generation(&cow_node->header, trans->transid);
  64. btrfs_inc_ref(trans, root, buf);
  65. if (buf == root->node) {
  66. root->node = cow;
  67. get_bh(cow);
  68. if (buf != root->commit_root) {
  69. btrfs_free_extent(trans, root, buf->b_blocknr, 1, 1);
  70. }
  71. btrfs_block_release(root, buf);
  72. } else {
  73. btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot,
  74. cow->b_blocknr);
  75. btrfs_mark_buffer_dirty(parent);
  76. btrfs_free_extent(trans, root, buf->b_blocknr, 1, 1);
  77. }
  78. btrfs_block_release(root, buf);
  79. mark_buffer_dirty(cow);
  80. *cow_ret = cow;
  81. return 0;
  82. }
  83. /*
  84. * The leaf data grows from end-to-front in the node.
  85. * this returns the address of the start of the last item,
  86. * which is the stop of the leaf data stack
  87. */
  88. static inline unsigned int leaf_data_end(struct btrfs_root *root,
  89. struct btrfs_leaf *leaf)
  90. {
  91. u32 nr = btrfs_header_nritems(&leaf->header);
  92. if (nr == 0)
  93. return BTRFS_LEAF_DATA_SIZE(root);
  94. return btrfs_item_offset(leaf->items + nr - 1);
  95. }
  96. /*
  97. * compare two keys in a memcmp fashion
  98. */
  99. static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
  100. {
  101. struct btrfs_key k1;
  102. btrfs_disk_key_to_cpu(&k1, disk);
  103. if (k1.objectid > k2->objectid)
  104. return 1;
  105. if (k1.objectid < k2->objectid)
  106. return -1;
  107. if (k1.offset > k2->offset)
  108. return 1;
  109. if (k1.offset < k2->offset)
  110. return -1;
  111. if (k1.flags > k2->flags)
  112. return 1;
  113. if (k1.flags < k2->flags)
  114. return -1;
  115. return 0;
  116. }
  117. static int check_node(struct btrfs_root *root, struct btrfs_path *path,
  118. int level)
  119. {
  120. int i;
  121. struct btrfs_node *parent = NULL;
  122. struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
  123. int parent_slot;
  124. u32 nritems = btrfs_header_nritems(&node->header);
  125. if (path->nodes[level + 1])
  126. parent = btrfs_buffer_node(path->nodes[level + 1]);
  127. parent_slot = path->slots[level + 1];
  128. BUG_ON(nritems == 0);
  129. if (parent) {
  130. struct btrfs_disk_key *parent_key;
  131. parent_key = &parent->ptrs[parent_slot].key;
  132. BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
  133. sizeof(struct btrfs_disk_key)));
  134. BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
  135. btrfs_header_blocknr(&node->header));
  136. }
  137. BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
  138. for (i = 0; nritems > 1 && i < nritems - 2; i++) {
  139. struct btrfs_key cpukey;
  140. btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key);
  141. BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0);
  142. }
  143. return 0;
  144. }
  145. static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
  146. int level)
  147. {
  148. int i;
  149. struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]);
  150. struct btrfs_node *parent = NULL;
  151. int parent_slot;
  152. u32 nritems = btrfs_header_nritems(&leaf->header);
  153. if (path->nodes[level + 1])
  154. parent = btrfs_buffer_node(path->nodes[level + 1]);
  155. parent_slot = path->slots[level + 1];
  156. BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
  157. if (nritems == 0)
  158. return 0;
  159. if (parent) {
  160. struct btrfs_disk_key *parent_key;
  161. parent_key = &parent->ptrs[parent_slot].key;
  162. BUG_ON(memcmp(parent_key, &leaf->items[0].key,
  163. sizeof(struct btrfs_disk_key)));
  164. BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
  165. btrfs_header_blocknr(&leaf->header));
  166. }
  167. for (i = 0; nritems > 1 && i < nritems - 2; i++) {
  168. struct btrfs_key cpukey;
  169. btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
  170. BUG_ON(comp_keys(&leaf->items[i].key,
  171. &cpukey) >= 0);
  172. BUG_ON(btrfs_item_offset(leaf->items + i) !=
  173. btrfs_item_end(leaf->items + i + 1));
  174. if (i == 0) {
  175. BUG_ON(btrfs_item_offset(leaf->items + i) +
  176. btrfs_item_size(leaf->items + i) !=
  177. BTRFS_LEAF_DATA_SIZE(root));
  178. }
  179. }
  180. return 0;
  181. }
  182. static int check_block(struct btrfs_root *root, struct btrfs_path *path,
  183. int level)
  184. {
  185. struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
  186. if (memcmp(node->header.fsid, root->fs_info->disk_super->fsid,
  187. sizeof(node->header.fsid)))
  188. BUG();
  189. if (level == 0)
  190. return check_leaf(root, path, level);
  191. return check_node(root, path, level);
  192. }
  193. /*
  194. * search for key in the array p. items p are item_size apart
  195. * and there are 'max' items in p
  196. * the slot in the array is returned via slot, and it points to
  197. * the place where you would insert key if it is not found in
  198. * the array.
  199. *
  200. * slot may point to max if the key is bigger than all of the keys
  201. */
  202. static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
  203. int max, int *slot)
  204. {
  205. int low = 0;
  206. int high = max;
  207. int mid;
  208. int ret;
  209. struct btrfs_disk_key *tmp;
  210. while(low < high) {
  211. mid = (low + high) / 2;
  212. tmp = (struct btrfs_disk_key *)(p + mid * item_size);
  213. ret = comp_keys(tmp, key);
  214. if (ret < 0)
  215. low = mid + 1;
  216. else if (ret > 0)
  217. high = mid;
  218. else {
  219. *slot = mid;
  220. return 0;
  221. }
  222. }
  223. *slot = low;
  224. return 1;
  225. }
  226. /*
  227. * simple bin_search frontend that does the right thing for
  228. * leaves vs nodes
  229. */
  230. static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
  231. {
  232. if (btrfs_is_leaf(c)) {
  233. struct btrfs_leaf *l = (struct btrfs_leaf *)c;
  234. return generic_bin_search((void *)l->items,
  235. sizeof(struct btrfs_item),
  236. key, btrfs_header_nritems(&c->header),
  237. slot);
  238. } else {
  239. return generic_bin_search((void *)c->ptrs,
  240. sizeof(struct btrfs_key_ptr),
  241. key, btrfs_header_nritems(&c->header),
  242. slot);
  243. }
  244. return -1;
  245. }
  246. static struct buffer_head *read_node_slot(struct btrfs_root *root,
  247. struct buffer_head *parent_buf,
  248. int slot)
  249. {
  250. struct btrfs_node *node = btrfs_buffer_node(parent_buf);
  251. if (slot < 0)
  252. return NULL;
  253. if (slot >= btrfs_header_nritems(&node->header))
  254. return NULL;
  255. return read_tree_block(root, btrfs_node_blockptr(node, slot));
  256. }
  257. static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
  258. *root, struct btrfs_path *path, int level)
  259. {
  260. struct buffer_head *right_buf;
  261. struct buffer_head *mid_buf;
  262. struct buffer_head *left_buf;
  263. struct buffer_head *parent_buf = NULL;
  264. struct btrfs_node *right = NULL;
  265. struct btrfs_node *mid;
  266. struct btrfs_node *left = NULL;
  267. struct btrfs_node *parent = NULL;
  268. int ret = 0;
  269. int wret;
  270. int pslot;
  271. int orig_slot = path->slots[level];
  272. u64 orig_ptr;
  273. if (level == 0)
  274. return 0;
  275. mid_buf = path->nodes[level];
  276. mid = btrfs_buffer_node(mid_buf);
  277. orig_ptr = btrfs_node_blockptr(mid, orig_slot);
  278. if (level < BTRFS_MAX_LEVEL - 1)
  279. parent_buf = path->nodes[level + 1];
  280. pslot = path->slots[level + 1];
  281. /*
  282. * deal with the case where there is only one pointer in the root
  283. * by promoting the node below to a root
  284. */
  285. if (!parent_buf) {
  286. struct buffer_head *child;
  287. u64 blocknr = mid_buf->b_blocknr;
  288. if (btrfs_header_nritems(&mid->header) != 1)
  289. return 0;
  290. /* promote the child to a root */
  291. child = read_node_slot(root, mid_buf, 0);
  292. BUG_ON(!child);
  293. root->node = child;
  294. path->nodes[level] = NULL;
  295. clean_tree_block(trans, root, mid_buf);
  296. wait_on_buffer(mid_buf);
  297. /* once for the path */
  298. btrfs_block_release(root, mid_buf);
  299. /* once for the root ptr */
  300. btrfs_block_release(root, mid_buf);
  301. return btrfs_free_extent(trans, root, blocknr, 1, 1);
  302. }
  303. parent = btrfs_buffer_node(parent_buf);
  304. if (btrfs_header_nritems(&mid->header) >
  305. BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
  306. return 0;
  307. left_buf = read_node_slot(root, parent_buf, pslot - 1);
  308. right_buf = read_node_slot(root, parent_buf, pslot + 1);
  309. /* first, try to make some room in the middle buffer */
  310. if (left_buf) {
  311. btrfs_cow_block(trans, root, left_buf, parent_buf, pslot - 1,
  312. &left_buf);
  313. left = btrfs_buffer_node(left_buf);
  314. orig_slot += btrfs_header_nritems(&left->header);
  315. wret = push_node_left(trans, root, left_buf, mid_buf);
  316. if (wret < 0)
  317. ret = wret;
  318. }
  319. /*
  320. * then try to empty the right most buffer into the middle
  321. */
  322. if (right_buf) {
  323. btrfs_cow_block(trans, root, right_buf, parent_buf, pslot + 1,
  324. &right_buf);
  325. right = btrfs_buffer_node(right_buf);
  326. wret = push_node_left(trans, root, mid_buf, right_buf);
  327. if (wret < 0)
  328. ret = wret;
  329. if (btrfs_header_nritems(&right->header) == 0) {
  330. u64 blocknr = right_buf->b_blocknr;
  331. clean_tree_block(trans, root, right_buf);
  332. wait_on_buffer(right_buf);
  333. btrfs_block_release(root, right_buf);
  334. right_buf = NULL;
  335. right = NULL;
  336. wret = del_ptr(trans, root, path, level + 1, pslot +
  337. 1);
  338. if (wret)
  339. ret = wret;
  340. wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
  341. if (wret)
  342. ret = wret;
  343. } else {
  344. btrfs_memcpy(root, parent,
  345. &parent->ptrs[pslot + 1].key,
  346. &right->ptrs[0].key,
  347. sizeof(struct btrfs_disk_key));
  348. btrfs_mark_buffer_dirty(parent_buf);
  349. }
  350. }
  351. if (btrfs_header_nritems(&mid->header) == 1) {
  352. /*
  353. * we're not allowed to leave a node with one item in the
  354. * tree during a delete. A deletion from lower in the tree
  355. * could try to delete the only pointer in this node.
  356. * So, pull some keys from the left.
  357. * There has to be a left pointer at this point because
  358. * otherwise we would have pulled some pointers from the
  359. * right
  360. */
  361. BUG_ON(!left_buf);
  362. wret = balance_node_right(trans, root, mid_buf, left_buf);
  363. if (wret < 0)
  364. ret = wret;
  365. BUG_ON(wret == 1);
  366. }
  367. if (btrfs_header_nritems(&mid->header) == 0) {
  368. /* we've managed to empty the middle node, drop it */
  369. u64 blocknr = mid_buf->b_blocknr;
  370. clean_tree_block(trans, root, mid_buf);
  371. wait_on_buffer(mid_buf);
  372. btrfs_block_release(root, mid_buf);
  373. mid_buf = NULL;
  374. mid = NULL;
  375. wret = del_ptr(trans, root, path, level + 1, pslot);
  376. if (wret)
  377. ret = wret;
  378. wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
  379. if (wret)
  380. ret = wret;
  381. } else {
  382. /* update the parent key to reflect our changes */
  383. btrfs_memcpy(root, parent,
  384. &parent->ptrs[pslot].key, &mid->ptrs[0].key,
  385. sizeof(struct btrfs_disk_key));
  386. btrfs_mark_buffer_dirty(parent_buf);
  387. }
  388. /* update the path */
  389. if (left_buf) {
  390. if (btrfs_header_nritems(&left->header) > orig_slot) {
  391. get_bh(left_buf);
  392. path->nodes[level] = left_buf;
  393. path->slots[level + 1] -= 1;
  394. path->slots[level] = orig_slot;
  395. if (mid_buf)
  396. btrfs_block_release(root, mid_buf);
  397. } else {
  398. orig_slot -= btrfs_header_nritems(&left->header);
  399. path->slots[level] = orig_slot;
  400. }
  401. }
  402. /* double check we haven't messed things up */
  403. check_block(root, path, level);
  404. if (orig_ptr !=
  405. btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]),
  406. path->slots[level]))
  407. BUG();
  408. if (right_buf)
  409. btrfs_block_release(root, right_buf);
  410. if (left_buf)
  411. btrfs_block_release(root, left_buf);
  412. return ret;
  413. }
  414. /*
  415. * look for key in the tree. path is filled in with nodes along the way
  416. * if key is found, we return zero and you can find the item in the leaf
  417. * level of the path (level 0)
  418. *
  419. * If the key isn't found, the path points to the slot where it should
  420. * be inserted, and 1 is returned. If there are other errors during the
  421. * search a negative error number is returned.
  422. *
  423. * if ins_len > 0, nodes and leaves will be split as we walk down the
  424. * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
  425. * possible)
  426. */
  427. int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
  428. *root, struct btrfs_key *key, struct btrfs_path *p, int
  429. ins_len, int cow)
  430. {
  431. struct buffer_head *b;
  432. struct buffer_head *cow_buf;
  433. struct btrfs_node *c;
  434. int slot;
  435. int ret;
  436. int level;
  437. WARN_ON(p->nodes[0] != NULL);
  438. WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
  439. again:
  440. b = root->node;
  441. get_bh(b);
  442. while (b) {
  443. c = btrfs_buffer_node(b);
  444. level = btrfs_header_level(&c->header);
  445. if (cow) {
  446. int wret;
  447. wret = btrfs_cow_block(trans, root, b,
  448. p->nodes[level + 1],
  449. p->slots[level + 1],
  450. &cow_buf);
  451. b = cow_buf;
  452. c = btrfs_buffer_node(b);
  453. }
  454. BUG_ON(!cow && ins_len);
  455. if (level != btrfs_header_level(&c->header))
  456. WARN_ON(1);
  457. level = btrfs_header_level(&c->header);
  458. p->nodes[level] = b;
  459. ret = check_block(root, p, level);
  460. if (ret)
  461. return -1;
  462. ret = bin_search(c, key, &slot);
  463. if (!btrfs_is_leaf(c)) {
  464. if (ret && slot > 0)
  465. slot -= 1;
  466. p->slots[level] = slot;
  467. if (ins_len > 0 && btrfs_header_nritems(&c->header) >=
  468. BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
  469. int sret = split_node(trans, root, p, level);
  470. BUG_ON(sret > 0);
  471. if (sret)
  472. return sret;
  473. b = p->nodes[level];
  474. c = btrfs_buffer_node(b);
  475. slot = p->slots[level];
  476. } else if (ins_len < 0) {
  477. int sret = balance_level(trans, root, p,
  478. level);
  479. if (sret)
  480. return sret;
  481. b = p->nodes[level];
  482. if (!b)
  483. goto again;
  484. c = btrfs_buffer_node(b);
  485. slot = p->slots[level];
  486. BUG_ON(btrfs_header_nritems(&c->header) == 1);
  487. }
  488. b = read_tree_block(root, btrfs_node_blockptr(c, slot));
  489. } else {
  490. struct btrfs_leaf *l = (struct btrfs_leaf *)c;
  491. p->slots[level] = slot;
  492. if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
  493. sizeof(struct btrfs_item) + ins_len) {
  494. int sret = split_leaf(trans, root, key,
  495. 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. lower = btrfs_buffer_node(path->nodes[level-1]);
  639. memcpy(c->header.fsid, root->fs_info->disk_super->fsid,
  640. sizeof(c->header.fsid));
  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. memcpy(split->header.fsid, root->fs_info->disk_super->fsid,
  727. sizeof(split->header.fsid));
  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 nritems = btrfs_header_nritems(&l->header);
  760. int end = min(nritems, start + nr) - 1;
  761. if (!nr)
  762. return 0;
  763. data_len = btrfs_item_end(l->items + start);
  764. data_len = data_len - btrfs_item_offset(l->items + end);
  765. data_len += sizeof(struct btrfs_item) * nr;
  766. WARN_ON(data_len < 0);
  767. return data_len;
  768. }
  769. /*
  770. * The space between the end of the leaf items and
  771. * the start of the leaf data. IOW, how much room
  772. * the leaf has left for both items and data
  773. */
  774. int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
  775. {
  776. int nritems = btrfs_header_nritems(&leaf->header);
  777. return BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
  778. }
  779. /*
  780. * push some data in the path leaf to the right, trying to free up at
  781. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  782. *
  783. * returns 1 if the push failed because the other node didn't have enough
  784. * room, 0 if everything worked out and < 0 if there were major errors.
  785. */
  786. static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
  787. *root, struct btrfs_path *path, int data_size)
  788. {
  789. struct buffer_head *left_buf = path->nodes[0];
  790. struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf);
  791. struct btrfs_leaf *right;
  792. struct buffer_head *right_buf;
  793. struct buffer_head *upper;
  794. struct btrfs_node *upper_node;
  795. int slot;
  796. int i;
  797. int free_space;
  798. int push_space = 0;
  799. int push_items = 0;
  800. struct btrfs_item *item;
  801. u32 left_nritems;
  802. u32 right_nritems;
  803. slot = path->slots[1];
  804. if (!path->nodes[1]) {
  805. return 1;
  806. }
  807. upper = path->nodes[1];
  808. upper_node = btrfs_buffer_node(upper);
  809. if (slot >= btrfs_header_nritems(&upper_node->header) - 1) {
  810. return 1;
  811. }
  812. right_buf = read_tree_block(root,
  813. btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1));
  814. right = btrfs_buffer_leaf(right_buf);
  815. free_space = btrfs_leaf_free_space(root, right);
  816. if (free_space < data_size + sizeof(struct btrfs_item)) {
  817. btrfs_block_release(root, right_buf);
  818. return 1;
  819. }
  820. /* cow and double check */
  821. btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf);
  822. right = btrfs_buffer_leaf(right_buf);
  823. free_space = btrfs_leaf_free_space(root, right);
  824. if (free_space < data_size + sizeof(struct btrfs_item)) {
  825. btrfs_block_release(root, right_buf);
  826. return 1;
  827. }
  828. left_nritems = btrfs_header_nritems(&left->header);
  829. for (i = left_nritems - 1; i >= 0; i--) {
  830. item = left->items + i;
  831. if (path->slots[0] == i)
  832. push_space += data_size + sizeof(*item);
  833. if (btrfs_item_size(item) + sizeof(*item) + push_space >
  834. free_space)
  835. break;
  836. push_items++;
  837. push_space += btrfs_item_size(item) + sizeof(*item);
  838. }
  839. if (push_items == 0) {
  840. btrfs_block_release(root, right_buf);
  841. return 1;
  842. }
  843. right_nritems = btrfs_header_nritems(&right->header);
  844. /* push left to right */
  845. push_space = btrfs_item_end(left->items + left_nritems - push_items);
  846. push_space -= leaf_data_end(root, left);
  847. /* make room in the right data area */
  848. btrfs_memmove(root, right, btrfs_leaf_data(right) +
  849. leaf_data_end(root, right) - push_space,
  850. btrfs_leaf_data(right) +
  851. leaf_data_end(root, right), BTRFS_LEAF_DATA_SIZE(root) -
  852. leaf_data_end(root, right));
  853. /* copy from the left data area */
  854. btrfs_memcpy(root, right, btrfs_leaf_data(right) +
  855. BTRFS_LEAF_DATA_SIZE(root) - push_space,
  856. btrfs_leaf_data(left) + leaf_data_end(root, left),
  857. push_space);
  858. btrfs_memmove(root, right, right->items + push_items, right->items,
  859. right_nritems * sizeof(struct btrfs_item));
  860. /* copy the items from left to right */
  861. btrfs_memcpy(root, right, right->items, left->items +
  862. left_nritems - push_items,
  863. push_items * sizeof(struct btrfs_item));
  864. /* update the item pointers */
  865. right_nritems += push_items;
  866. btrfs_set_header_nritems(&right->header, right_nritems);
  867. push_space = BTRFS_LEAF_DATA_SIZE(root);
  868. for (i = 0; i < right_nritems; i++) {
  869. btrfs_set_item_offset(right->items + i, push_space -
  870. btrfs_item_size(right->items + i));
  871. push_space = btrfs_item_offset(right->items + i);
  872. }
  873. left_nritems -= push_items;
  874. btrfs_set_header_nritems(&left->header, left_nritems);
  875. btrfs_mark_buffer_dirty(left_buf);
  876. btrfs_mark_buffer_dirty(right_buf);
  877. btrfs_memcpy(root, upper_node, &upper_node->ptrs[slot + 1].key,
  878. &right->items[0].key, sizeof(struct btrfs_disk_key));
  879. btrfs_mark_buffer_dirty(upper);
  880. /* then fixup the leaf pointer in the path */
  881. if (path->slots[0] >= left_nritems) {
  882. path->slots[0] -= left_nritems;
  883. btrfs_block_release(root, path->nodes[0]);
  884. path->nodes[0] = right_buf;
  885. path->slots[1] += 1;
  886. } else {
  887. btrfs_block_release(root, right_buf);
  888. }
  889. return 0;
  890. }
  891. /*
  892. * push some data in the path leaf to the left, trying to free up at
  893. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  894. */
  895. static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
  896. *root, struct btrfs_path *path, int data_size)
  897. {
  898. struct buffer_head *right_buf = path->nodes[0];
  899. struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf);
  900. struct buffer_head *t;
  901. struct btrfs_leaf *left;
  902. int slot;
  903. int i;
  904. int free_space;
  905. int push_space = 0;
  906. int push_items = 0;
  907. struct btrfs_item *item;
  908. u32 old_left_nritems;
  909. int ret = 0;
  910. int wret;
  911. slot = path->slots[1];
  912. if (slot == 0) {
  913. return 1;
  914. }
  915. if (!path->nodes[1]) {
  916. return 1;
  917. }
  918. t = read_tree_block(root,
  919. btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1));
  920. left = btrfs_buffer_leaf(t);
  921. free_space = btrfs_leaf_free_space(root, left);
  922. if (free_space < data_size + sizeof(struct btrfs_item)) {
  923. btrfs_block_release(root, t);
  924. return 1;
  925. }
  926. /* cow and double check */
  927. btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
  928. left = btrfs_buffer_leaf(t);
  929. free_space = btrfs_leaf_free_space(root, left);
  930. if (free_space < data_size + sizeof(struct btrfs_item)) {
  931. btrfs_block_release(root, t);
  932. return 1;
  933. }
  934. for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
  935. item = right->items + i;
  936. if (path->slots[0] == i)
  937. push_space += data_size + sizeof(*item);
  938. if (btrfs_item_size(item) + sizeof(*item) + push_space >
  939. free_space)
  940. break;
  941. push_items++;
  942. push_space += btrfs_item_size(item) + sizeof(*item);
  943. }
  944. if (push_items == 0) {
  945. btrfs_block_release(root, t);
  946. return 1;
  947. }
  948. /* push data from right to left */
  949. btrfs_memcpy(root, left, left->items +
  950. btrfs_header_nritems(&left->header),
  951. right->items, push_items * sizeof(struct btrfs_item));
  952. push_space = BTRFS_LEAF_DATA_SIZE(root) -
  953. btrfs_item_offset(right->items + push_items -1);
  954. btrfs_memcpy(root, left, btrfs_leaf_data(left) +
  955. leaf_data_end(root, left) - push_space,
  956. btrfs_leaf_data(right) +
  957. btrfs_item_offset(right->items + push_items - 1),
  958. push_space);
  959. old_left_nritems = btrfs_header_nritems(&left->header);
  960. BUG_ON(old_left_nritems < 0);
  961. for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
  962. u32 ioff = btrfs_item_offset(left->items + i);
  963. btrfs_set_item_offset(left->items + i, ioff -
  964. (BTRFS_LEAF_DATA_SIZE(root) -
  965. btrfs_item_offset(left->items +
  966. old_left_nritems - 1)));
  967. }
  968. btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
  969. /* fixup right node */
  970. push_space = btrfs_item_offset(right->items + push_items - 1) -
  971. leaf_data_end(root, right);
  972. btrfs_memmove(root, right, btrfs_leaf_data(right) +
  973. BTRFS_LEAF_DATA_SIZE(root) - push_space,
  974. btrfs_leaf_data(right) +
  975. leaf_data_end(root, right), push_space);
  976. btrfs_memmove(root, right, right->items, right->items + push_items,
  977. (btrfs_header_nritems(&right->header) - push_items) *
  978. sizeof(struct btrfs_item));
  979. btrfs_set_header_nritems(&right->header,
  980. btrfs_header_nritems(&right->header) -
  981. push_items);
  982. push_space = BTRFS_LEAF_DATA_SIZE(root);
  983. for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
  984. btrfs_set_item_offset(right->items + i, push_space -
  985. btrfs_item_size(right->items + i));
  986. push_space = btrfs_item_offset(right->items + i);
  987. }
  988. btrfs_mark_buffer_dirty(t);
  989. btrfs_mark_buffer_dirty(right_buf);
  990. wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
  991. if (wret)
  992. ret = wret;
  993. /* then fixup the leaf pointer in the path */
  994. if (path->slots[0] < push_items) {
  995. path->slots[0] += old_left_nritems;
  996. btrfs_block_release(root, path->nodes[0]);
  997. path->nodes[0] = t;
  998. path->slots[1] -= 1;
  999. } else {
  1000. btrfs_block_release(root, t);
  1001. path->slots[0] -= push_items;
  1002. }
  1003. BUG_ON(path->slots[0] < 0);
  1004. return ret;
  1005. }
  1006. /*
  1007. * split the path's leaf in two, making sure there is at least data_size
  1008. * available for the resulting leaf level of the path.
  1009. *
  1010. * returns 0 if all went well and < 0 on failure.
  1011. */
  1012. static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
  1013. *root, struct btrfs_key *ins_key,
  1014. struct btrfs_path *path, int data_size)
  1015. {
  1016. struct buffer_head *l_buf;
  1017. struct btrfs_leaf *l;
  1018. u32 nritems;
  1019. int mid;
  1020. int slot;
  1021. struct btrfs_leaf *right;
  1022. struct buffer_head *right_buffer;
  1023. int space_needed = data_size + sizeof(struct btrfs_item);
  1024. int data_copy_size;
  1025. int rt_data_off;
  1026. int i;
  1027. int ret = 0;
  1028. int wret;
  1029. int double_split = 0;
  1030. struct btrfs_disk_key disk_key;
  1031. /* first try to make some room by pushing left and right */
  1032. wret = push_leaf_left(trans, root, path, data_size);
  1033. if (wret < 0)
  1034. return wret;
  1035. if (wret) {
  1036. wret = push_leaf_right(trans, root, path, data_size);
  1037. if (wret < 0)
  1038. return wret;
  1039. }
  1040. l_buf = path->nodes[0];
  1041. l = btrfs_buffer_leaf(l_buf);
  1042. /* did the pushes work? */
  1043. if (btrfs_leaf_free_space(root, l) >=
  1044. sizeof(struct btrfs_item) + data_size)
  1045. return 0;
  1046. if (!path->nodes[1]) {
  1047. ret = insert_new_root(trans, root, path, 1);
  1048. if (ret)
  1049. return ret;
  1050. }
  1051. slot = path->slots[0];
  1052. nritems = btrfs_header_nritems(&l->header);
  1053. mid = (nritems + 1)/ 2;
  1054. right_buffer = btrfs_alloc_free_block(trans, root);
  1055. BUG_ON(!right_buffer);
  1056. right = btrfs_buffer_leaf(right_buffer);
  1057. memset(&right->header, 0, sizeof(right->header));
  1058. btrfs_set_header_blocknr(&right->header, right_buffer->b_blocknr);
  1059. btrfs_set_header_generation(&right->header, trans->transid);
  1060. btrfs_set_header_level(&right->header, 0);
  1061. memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
  1062. sizeof(right->header.fsid));
  1063. if (mid <= slot) {
  1064. if (nritems == 1 ||
  1065. leaf_space_used(l, mid, nritems - mid) + space_needed >
  1066. BTRFS_LEAF_DATA_SIZE(root)) {
  1067. if (slot >= nritems) {
  1068. btrfs_cpu_key_to_disk(&disk_key, ins_key);
  1069. btrfs_set_header_nritems(&right->header, 0);
  1070. wret = insert_ptr(trans, root, path,
  1071. &disk_key,
  1072. right_buffer->b_blocknr,
  1073. path->slots[1] + 1, 1);
  1074. if (wret)
  1075. ret = wret;
  1076. btrfs_block_release(root, path->nodes[0]);
  1077. path->nodes[0] = right_buffer;
  1078. path->slots[0] = 0;
  1079. path->slots[1] += 1;
  1080. return ret;
  1081. }
  1082. mid = slot;
  1083. double_split = 1;
  1084. }
  1085. } else {
  1086. if (leaf_space_used(l, 0, mid + 1) + space_needed >
  1087. BTRFS_LEAF_DATA_SIZE(root)) {
  1088. if (slot == 0) {
  1089. btrfs_cpu_key_to_disk(&disk_key, ins_key);
  1090. btrfs_set_header_nritems(&right->header, 0);
  1091. wret = insert_ptr(trans, root, path,
  1092. &disk_key,
  1093. right_buffer->b_blocknr,
  1094. path->slots[1] - 1, 1);
  1095. if (wret)
  1096. ret = wret;
  1097. btrfs_block_release(root, path->nodes[0]);
  1098. path->nodes[0] = right_buffer;
  1099. path->slots[0] = 0;
  1100. path->slots[1] -= 1;
  1101. return ret;
  1102. }
  1103. mid = slot;
  1104. double_split = 1;
  1105. }
  1106. }
  1107. btrfs_set_header_nritems(&right->header, nritems - mid);
  1108. data_copy_size = btrfs_item_end(l->items + mid) -
  1109. leaf_data_end(root, l);
  1110. btrfs_memcpy(root, right, right->items, l->items + mid,
  1111. (nritems - mid) * sizeof(struct btrfs_item));
  1112. btrfs_memcpy(root, right,
  1113. btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
  1114. data_copy_size, btrfs_leaf_data(l) +
  1115. leaf_data_end(root, l), data_copy_size);
  1116. rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
  1117. btrfs_item_end(l->items + mid);
  1118. for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
  1119. u32 ioff = btrfs_item_offset(right->items + i);
  1120. btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
  1121. }
  1122. btrfs_set_header_nritems(&l->header, mid);
  1123. ret = 0;
  1124. wret = insert_ptr(trans, root, path, &right->items[0].key,
  1125. right_buffer->b_blocknr, path->slots[1] + 1, 1);
  1126. if (wret)
  1127. ret = wret;
  1128. btrfs_mark_buffer_dirty(right_buffer);
  1129. btrfs_mark_buffer_dirty(l_buf);
  1130. BUG_ON(path->slots[0] != slot);
  1131. if (mid <= slot) {
  1132. btrfs_block_release(root, path->nodes[0]);
  1133. path->nodes[0] = right_buffer;
  1134. path->slots[0] -= mid;
  1135. path->slots[1] += 1;
  1136. } else
  1137. btrfs_block_release(root, right_buffer);
  1138. BUG_ON(path->slots[0] < 0);
  1139. if (!double_split)
  1140. return ret;
  1141. right_buffer = btrfs_alloc_free_block(trans, root);
  1142. BUG_ON(!right_buffer);
  1143. right = btrfs_buffer_leaf(right_buffer);
  1144. memset(&right->header, 0, sizeof(right->header));
  1145. btrfs_set_header_blocknr(&right->header, right_buffer->b_blocknr);
  1146. btrfs_set_header_generation(&right->header, trans->transid);
  1147. btrfs_set_header_level(&right->header, 0);
  1148. memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
  1149. sizeof(right->header.fsid));
  1150. btrfs_cpu_key_to_disk(&disk_key, ins_key);
  1151. btrfs_set_header_nritems(&right->header, 0);
  1152. wret = insert_ptr(trans, root, path,
  1153. &disk_key,
  1154. right_buffer->b_blocknr,
  1155. path->slots[1], 1);
  1156. if (wret)
  1157. ret = wret;
  1158. btrfs_block_release(root, path->nodes[0]);
  1159. path->nodes[0] = right_buffer;
  1160. path->slots[0] = 0;
  1161. check_node(root, path, 1);
  1162. check_leaf(root, path, 0);
  1163. return ret;
  1164. }
  1165. /*
  1166. * Given a key and some data, insert an item into the tree.
  1167. * This does all the path init required, making room in the tree if needed.
  1168. */
  1169. int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
  1170. *root, struct btrfs_path *path, struct btrfs_key
  1171. *cpu_key, u32 data_size)
  1172. {
  1173. int ret = 0;
  1174. int slot;
  1175. int slot_orig;
  1176. struct btrfs_leaf *leaf;
  1177. struct buffer_head *leaf_buf;
  1178. u32 nritems;
  1179. unsigned int data_end;
  1180. struct btrfs_disk_key disk_key;
  1181. btrfs_cpu_key_to_disk(&disk_key, cpu_key);
  1182. /* create a root if there isn't one */
  1183. if (!root->node)
  1184. BUG();
  1185. ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
  1186. if (ret == 0) {
  1187. return -EEXIST;
  1188. }
  1189. if (ret < 0)
  1190. goto out;
  1191. slot_orig = path->slots[0];
  1192. leaf_buf = path->nodes[0];
  1193. leaf = btrfs_buffer_leaf(leaf_buf);
  1194. nritems = btrfs_header_nritems(&leaf->header);
  1195. data_end = leaf_data_end(root, leaf);
  1196. if (btrfs_leaf_free_space(root, leaf) <
  1197. sizeof(struct btrfs_item) + data_size) {
  1198. BUG();
  1199. }
  1200. slot = path->slots[0];
  1201. BUG_ON(slot < 0);
  1202. if (slot != nritems) {
  1203. int i;
  1204. unsigned int old_data = btrfs_item_end(leaf->items + slot);
  1205. /*
  1206. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  1207. */
  1208. /* first correct the data pointers */
  1209. for (i = slot; i < nritems; i++) {
  1210. u32 ioff = btrfs_item_offset(leaf->items + i);
  1211. btrfs_set_item_offset(leaf->items + i,
  1212. ioff - data_size);
  1213. }
  1214. /* shift the items */
  1215. btrfs_memmove(root, leaf, leaf->items + slot + 1,
  1216. leaf->items + slot,
  1217. (nritems - slot) * sizeof(struct btrfs_item));
  1218. /* shift the data */
  1219. btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
  1220. data_end - data_size, btrfs_leaf_data(leaf) +
  1221. data_end, old_data - data_end);
  1222. data_end = old_data;
  1223. }
  1224. /* setup the item for the new data */
  1225. btrfs_memcpy(root, leaf, &leaf->items[slot].key, &disk_key,
  1226. sizeof(struct btrfs_disk_key));
  1227. btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
  1228. btrfs_set_item_size(leaf->items + slot, data_size);
  1229. btrfs_set_header_nritems(&leaf->header, nritems + 1);
  1230. btrfs_mark_buffer_dirty(leaf_buf);
  1231. ret = 0;
  1232. if (slot == 0)
  1233. ret = fixup_low_keys(trans, root, path, &disk_key, 1);
  1234. if (btrfs_leaf_free_space(root, leaf) < 0)
  1235. BUG();
  1236. check_leaf(root, path, 0);
  1237. out:
  1238. return ret;
  1239. }
  1240. /*
  1241. * Given a key and some data, insert an item into the tree.
  1242. * This does all the path init required, making room in the tree if needed.
  1243. */
  1244. int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
  1245. *root, struct btrfs_key *cpu_key, void *data, u32
  1246. data_size)
  1247. {
  1248. int ret = 0;
  1249. struct btrfs_path *path;
  1250. u8 *ptr;
  1251. path = btrfs_alloc_path();
  1252. BUG_ON(!path);
  1253. btrfs_init_path(path);
  1254. ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
  1255. if (!ret) {
  1256. ptr = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1257. path->slots[0], u8);
  1258. btrfs_memcpy(root, path->nodes[0]->b_data,
  1259. ptr, data, data_size);
  1260. btrfs_mark_buffer_dirty(path->nodes[0]);
  1261. }
  1262. btrfs_release_path(root, path);
  1263. btrfs_free_path(path);
  1264. return ret;
  1265. }
  1266. /*
  1267. * delete the pointer from a given node.
  1268. *
  1269. * If the delete empties a node, the node is removed from the tree,
  1270. * continuing all the way the root if required. The root is converted into
  1271. * a leaf if all the nodes are emptied.
  1272. */
  1273. static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  1274. struct btrfs_path *path, int level, int slot)
  1275. {
  1276. struct btrfs_node *node;
  1277. struct buffer_head *parent = path->nodes[level];
  1278. u32 nritems;
  1279. int ret = 0;
  1280. int wret;
  1281. node = btrfs_buffer_node(parent);
  1282. nritems = btrfs_header_nritems(&node->header);
  1283. if (slot != nritems -1) {
  1284. btrfs_memmove(root, node, node->ptrs + slot,
  1285. node->ptrs + slot + 1,
  1286. sizeof(struct btrfs_key_ptr) *
  1287. (nritems - slot - 1));
  1288. }
  1289. nritems--;
  1290. btrfs_set_header_nritems(&node->header, nritems);
  1291. if (nritems == 0 && parent == root->node) {
  1292. struct btrfs_header *header = btrfs_buffer_header(root->node);
  1293. BUG_ON(btrfs_header_level(header) != 1);
  1294. /* just turn the root into a leaf and break */
  1295. btrfs_set_header_level(header, 0);
  1296. } else if (slot == 0) {
  1297. wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
  1298. level + 1);
  1299. if (wret)
  1300. ret = wret;
  1301. }
  1302. btrfs_mark_buffer_dirty(parent);
  1303. return ret;
  1304. }
  1305. /*
  1306. * delete the item at the leaf level in path. If that empties
  1307. * the leaf, remove it from the tree
  1308. */
  1309. int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  1310. struct btrfs_path *path)
  1311. {
  1312. int slot;
  1313. struct btrfs_leaf *leaf;
  1314. struct buffer_head *leaf_buf;
  1315. int doff;
  1316. int dsize;
  1317. int ret = 0;
  1318. int wret;
  1319. u32 nritems;
  1320. leaf_buf = path->nodes[0];
  1321. leaf = btrfs_buffer_leaf(leaf_buf);
  1322. slot = path->slots[0];
  1323. doff = btrfs_item_offset(leaf->items + slot);
  1324. dsize = btrfs_item_size(leaf->items + slot);
  1325. nritems = btrfs_header_nritems(&leaf->header);
  1326. if (slot != nritems - 1) {
  1327. int i;
  1328. int data_end = leaf_data_end(root, leaf);
  1329. btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
  1330. data_end + dsize,
  1331. btrfs_leaf_data(leaf) + data_end,
  1332. doff - data_end);
  1333. for (i = slot + 1; i < nritems; i++) {
  1334. u32 ioff = btrfs_item_offset(leaf->items + i);
  1335. btrfs_set_item_offset(leaf->items + i, ioff + dsize);
  1336. }
  1337. btrfs_memmove(root, leaf, leaf->items + slot,
  1338. leaf->items + slot + 1,
  1339. sizeof(struct btrfs_item) *
  1340. (nritems - slot - 1));
  1341. }
  1342. btrfs_set_header_nritems(&leaf->header, nritems - 1);
  1343. nritems--;
  1344. /* delete the leaf if we've emptied it */
  1345. if (nritems == 0) {
  1346. if (leaf_buf == root->node) {
  1347. btrfs_set_header_level(&leaf->header, 0);
  1348. } else {
  1349. clean_tree_block(trans, root, leaf_buf);
  1350. wait_on_buffer(leaf_buf);
  1351. wret = del_ptr(trans, root, path, 1, path->slots[1]);
  1352. if (wret)
  1353. ret = wret;
  1354. wret = btrfs_free_extent(trans, root,
  1355. leaf_buf->b_blocknr, 1, 1);
  1356. if (wret)
  1357. ret = wret;
  1358. }
  1359. } else {
  1360. int used = leaf_space_used(leaf, 0, nritems);
  1361. if (slot == 0) {
  1362. wret = fixup_low_keys(trans, root, path,
  1363. &leaf->items[0].key, 1);
  1364. if (wret)
  1365. ret = wret;
  1366. }
  1367. /* delete the leaf if it is mostly empty */
  1368. if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
  1369. /* push_leaf_left fixes the path.
  1370. * make sure the path still points to our leaf
  1371. * for possible call to del_ptr below
  1372. */
  1373. slot = path->slots[1];
  1374. get_bh(leaf_buf);
  1375. wret = push_leaf_left(trans, root, path, 1);
  1376. if (wret < 0)
  1377. ret = wret;
  1378. if (path->nodes[0] == leaf_buf &&
  1379. btrfs_header_nritems(&leaf->header)) {
  1380. wret = push_leaf_right(trans, root, path, 1);
  1381. if (wret < 0)
  1382. ret = wret;
  1383. }
  1384. if (btrfs_header_nritems(&leaf->header) == 0) {
  1385. u64 blocknr = leaf_buf->b_blocknr;
  1386. clean_tree_block(trans, root, leaf_buf);
  1387. wait_on_buffer(leaf_buf);
  1388. wret = del_ptr(trans, root, path, 1, slot);
  1389. if (wret)
  1390. ret = wret;
  1391. btrfs_block_release(root, leaf_buf);
  1392. wret = btrfs_free_extent(trans, root, blocknr,
  1393. 1, 1);
  1394. if (wret)
  1395. ret = wret;
  1396. } else {
  1397. btrfs_mark_buffer_dirty(leaf_buf);
  1398. btrfs_block_release(root, leaf_buf);
  1399. }
  1400. } else {
  1401. btrfs_mark_buffer_dirty(leaf_buf);
  1402. }
  1403. }
  1404. return ret;
  1405. }
  1406. /*
  1407. * walk up the tree as far as required to find the next leaf.
  1408. * returns 0 if it found something or 1 if there are no greater leaves.
  1409. * returns < 0 on io errors.
  1410. */
  1411. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
  1412. {
  1413. int slot;
  1414. int level = 1;
  1415. u64 blocknr;
  1416. struct buffer_head *c;
  1417. struct btrfs_node *c_node;
  1418. struct buffer_head *next = NULL;
  1419. while(level < BTRFS_MAX_LEVEL) {
  1420. if (!path->nodes[level])
  1421. return 1;
  1422. slot = path->slots[level] + 1;
  1423. c = path->nodes[level];
  1424. c_node = btrfs_buffer_node(c);
  1425. if (slot >= btrfs_header_nritems(&c_node->header)) {
  1426. level++;
  1427. continue;
  1428. }
  1429. blocknr = btrfs_node_blockptr(c_node, slot);
  1430. if (next)
  1431. btrfs_block_release(root, next);
  1432. next = read_tree_block(root, blocknr);
  1433. break;
  1434. }
  1435. path->slots[level] = slot;
  1436. while(1) {
  1437. level--;
  1438. c = path->nodes[level];
  1439. btrfs_block_release(root, c);
  1440. path->nodes[level] = next;
  1441. path->slots[level] = 0;
  1442. if (!level)
  1443. break;
  1444. next = read_tree_block(root,
  1445. btrfs_node_blockptr(btrfs_buffer_node(next), 0));
  1446. }
  1447. return 0;
  1448. }