ctree.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  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. 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. memcpy(c->header.fsid, root->fs_info->disk_super->fsid,
  642. sizeof(c->header.fsid));
  643. if (btrfs_is_leaf(lower))
  644. lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
  645. else
  646. lower_key = &lower->ptrs[0].key;
  647. btrfs_memcpy(root, c, &c->ptrs[0].key, lower_key,
  648. sizeof(struct btrfs_disk_key));
  649. btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->b_blocknr);
  650. btrfs_mark_buffer_dirty(t);
  651. /* the super has an extra ref to root->node */
  652. btrfs_block_release(root, root->node);
  653. root->node = t;
  654. get_bh(t);
  655. path->nodes[level] = t;
  656. path->slots[level] = 0;
  657. return 0;
  658. }
  659. /*
  660. * worker function to insert a single pointer in a node.
  661. * the node should have enough room for the pointer already
  662. *
  663. * slot and level indicate where you want the key to go, and
  664. * blocknr is the block the key points to.
  665. *
  666. * returns zero on success and < 0 on any error
  667. */
  668. static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
  669. *root, struct btrfs_path *path, struct btrfs_disk_key
  670. *key, u64 blocknr, int slot, int level)
  671. {
  672. struct btrfs_node *lower;
  673. int nritems;
  674. BUG_ON(!path->nodes[level]);
  675. lower = btrfs_buffer_node(path->nodes[level]);
  676. nritems = btrfs_header_nritems(&lower->header);
  677. if (slot > nritems)
  678. BUG();
  679. if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
  680. BUG();
  681. if (slot != nritems) {
  682. btrfs_memmove(root, lower, lower->ptrs + slot + 1,
  683. lower->ptrs + slot,
  684. (nritems - slot) * sizeof(struct btrfs_key_ptr));
  685. }
  686. btrfs_memcpy(root, lower, &lower->ptrs[slot].key,
  687. key, sizeof(struct btrfs_disk_key));
  688. btrfs_set_node_blockptr(lower, slot, blocknr);
  689. btrfs_set_header_nritems(&lower->header, nritems + 1);
  690. btrfs_mark_buffer_dirty(path->nodes[level]);
  691. return 0;
  692. }
  693. /*
  694. * split the node at the specified level in path in two.
  695. * The path is corrected to point to the appropriate node after the split
  696. *
  697. * Before splitting this tries to make some room in the node by pushing
  698. * left and right, if either one works, it returns right away.
  699. *
  700. * returns 0 on success and < 0 on failure
  701. */
  702. static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
  703. *root, struct btrfs_path *path, int level)
  704. {
  705. struct buffer_head *t;
  706. struct btrfs_node *c;
  707. struct buffer_head *split_buffer;
  708. struct btrfs_node *split;
  709. int mid;
  710. int ret;
  711. int wret;
  712. u32 c_nritems;
  713. t = path->nodes[level];
  714. c = btrfs_buffer_node(t);
  715. if (t == root->node) {
  716. /* trying to split the root, lets make a new one */
  717. ret = insert_new_root(trans, root, path, level + 1);
  718. if (ret)
  719. return ret;
  720. }
  721. c_nritems = btrfs_header_nritems(&c->header);
  722. split_buffer = btrfs_alloc_free_block(trans, root);
  723. split = btrfs_buffer_node(split_buffer);
  724. btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
  725. btrfs_set_header_level(&split->header, btrfs_header_level(&c->header));
  726. btrfs_set_header_blocknr(&split->header, split_buffer->b_blocknr);
  727. btrfs_set_header_generation(&split->header, trans->transid);
  728. btrfs_set_header_parentid(&split->header,
  729. btrfs_header_parentid(btrfs_buffer_header(root->node)));
  730. memcpy(split->header.fsid, root->fs_info->disk_super->fsid,
  731. sizeof(split->header.fsid));
  732. mid = (c_nritems + 1) / 2;
  733. btrfs_memcpy(root, split, split->ptrs, c->ptrs + mid,
  734. (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
  735. btrfs_set_header_nritems(&split->header, c_nritems - mid);
  736. btrfs_set_header_nritems(&c->header, mid);
  737. ret = 0;
  738. btrfs_mark_buffer_dirty(t);
  739. btrfs_mark_buffer_dirty(split_buffer);
  740. wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
  741. split_buffer->b_blocknr, path->slots[level + 1] + 1,
  742. level + 1);
  743. if (wret)
  744. ret = wret;
  745. if (path->slots[level] >= mid) {
  746. path->slots[level] -= mid;
  747. btrfs_block_release(root, t);
  748. path->nodes[level] = split_buffer;
  749. path->slots[level + 1] += 1;
  750. } else {
  751. btrfs_block_release(root, split_buffer);
  752. }
  753. return ret;
  754. }
  755. /*
  756. * how many bytes are required to store the items in a leaf. start
  757. * and nr indicate which items in the leaf to check. This totals up the
  758. * space used both by the item structs and the item data
  759. */
  760. static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
  761. {
  762. int data_len;
  763. int nritems = btrfs_header_nritems(&l->header);
  764. int end = min(nritems, start + nr) - 1;
  765. if (!nr)
  766. return 0;
  767. data_len = btrfs_item_end(l->items + start);
  768. data_len = data_len - btrfs_item_offset(l->items + end);
  769. data_len += sizeof(struct btrfs_item) * nr;
  770. WARN_ON(data_len < 0);
  771. return data_len;
  772. }
  773. /*
  774. * The space between the end of the leaf items and
  775. * the start of the leaf data. IOW, how much room
  776. * the leaf has left for both items and data
  777. */
  778. int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
  779. {
  780. int nritems = btrfs_header_nritems(&leaf->header);
  781. return BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
  782. }
  783. /*
  784. * push some data in the path leaf to the right, trying to free up at
  785. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  786. *
  787. * returns 1 if the push failed because the other node didn't have enough
  788. * room, 0 if everything worked out and < 0 if there were major errors.
  789. */
  790. static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
  791. *root, struct btrfs_path *path, int data_size)
  792. {
  793. struct buffer_head *left_buf = path->nodes[0];
  794. struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf);
  795. struct btrfs_leaf *right;
  796. struct buffer_head *right_buf;
  797. struct buffer_head *upper;
  798. struct btrfs_node *upper_node;
  799. int slot;
  800. int i;
  801. int free_space;
  802. int push_space = 0;
  803. int push_items = 0;
  804. struct btrfs_item *item;
  805. u32 left_nritems;
  806. u32 right_nritems;
  807. slot = path->slots[1];
  808. if (!path->nodes[1]) {
  809. return 1;
  810. }
  811. upper = path->nodes[1];
  812. upper_node = btrfs_buffer_node(upper);
  813. if (slot >= btrfs_header_nritems(&upper_node->header) - 1) {
  814. return 1;
  815. }
  816. right_buf = read_tree_block(root,
  817. btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1));
  818. right = btrfs_buffer_leaf(right_buf);
  819. free_space = btrfs_leaf_free_space(root, right);
  820. if (free_space < data_size + sizeof(struct btrfs_item)) {
  821. btrfs_block_release(root, right_buf);
  822. return 1;
  823. }
  824. /* cow and double check */
  825. btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf);
  826. right = btrfs_buffer_leaf(right_buf);
  827. free_space = btrfs_leaf_free_space(root, right);
  828. if (free_space < data_size + sizeof(struct btrfs_item)) {
  829. btrfs_block_release(root, right_buf);
  830. return 1;
  831. }
  832. left_nritems = btrfs_header_nritems(&left->header);
  833. for (i = left_nritems - 1; i >= 0; i--) {
  834. item = left->items + i;
  835. if (path->slots[0] == i)
  836. push_space += data_size + sizeof(*item);
  837. if (btrfs_item_size(item) + sizeof(*item) + push_space >
  838. free_space)
  839. break;
  840. push_items++;
  841. push_space += btrfs_item_size(item) + sizeof(*item);
  842. }
  843. if (push_items == 0) {
  844. btrfs_block_release(root, right_buf);
  845. return 1;
  846. }
  847. right_nritems = btrfs_header_nritems(&right->header);
  848. /* push left to right */
  849. push_space = btrfs_item_end(left->items + left_nritems - push_items);
  850. push_space -= leaf_data_end(root, left);
  851. /* make room in the right data area */
  852. btrfs_memmove(root, right, btrfs_leaf_data(right) +
  853. leaf_data_end(root, right) - push_space,
  854. btrfs_leaf_data(right) +
  855. leaf_data_end(root, right), BTRFS_LEAF_DATA_SIZE(root) -
  856. leaf_data_end(root, right));
  857. /* copy from the left data area */
  858. btrfs_memcpy(root, right, btrfs_leaf_data(right) +
  859. BTRFS_LEAF_DATA_SIZE(root) - push_space,
  860. btrfs_leaf_data(left) + leaf_data_end(root, left),
  861. push_space);
  862. btrfs_memmove(root, right, right->items + push_items, right->items,
  863. right_nritems * sizeof(struct btrfs_item));
  864. /* copy the items from left to right */
  865. btrfs_memcpy(root, right, right->items, left->items +
  866. left_nritems - push_items,
  867. push_items * sizeof(struct btrfs_item));
  868. /* update the item pointers */
  869. right_nritems += push_items;
  870. btrfs_set_header_nritems(&right->header, right_nritems);
  871. push_space = BTRFS_LEAF_DATA_SIZE(root);
  872. for (i = 0; i < right_nritems; i++) {
  873. btrfs_set_item_offset(right->items + i, push_space -
  874. btrfs_item_size(right->items + i));
  875. push_space = btrfs_item_offset(right->items + i);
  876. }
  877. left_nritems -= push_items;
  878. btrfs_set_header_nritems(&left->header, left_nritems);
  879. btrfs_mark_buffer_dirty(left_buf);
  880. btrfs_mark_buffer_dirty(right_buf);
  881. btrfs_memcpy(root, upper_node, &upper_node->ptrs[slot + 1].key,
  882. &right->items[0].key, sizeof(struct btrfs_disk_key));
  883. btrfs_mark_buffer_dirty(upper);
  884. /* then fixup the leaf pointer in the path */
  885. if (path->slots[0] >= left_nritems) {
  886. path->slots[0] -= left_nritems;
  887. btrfs_block_release(root, path->nodes[0]);
  888. path->nodes[0] = right_buf;
  889. path->slots[1] += 1;
  890. } else {
  891. btrfs_block_release(root, right_buf);
  892. }
  893. return 0;
  894. }
  895. /*
  896. * push some data in the path leaf to the left, trying to free up at
  897. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  898. */
  899. static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
  900. *root, struct btrfs_path *path, int data_size)
  901. {
  902. struct buffer_head *right_buf = path->nodes[0];
  903. struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf);
  904. struct buffer_head *t;
  905. struct btrfs_leaf *left;
  906. int slot;
  907. int i;
  908. int free_space;
  909. int push_space = 0;
  910. int push_items = 0;
  911. struct btrfs_item *item;
  912. u32 old_left_nritems;
  913. int ret = 0;
  914. int wret;
  915. slot = path->slots[1];
  916. if (slot == 0) {
  917. return 1;
  918. }
  919. if (!path->nodes[1]) {
  920. return 1;
  921. }
  922. t = read_tree_block(root,
  923. btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1));
  924. left = btrfs_buffer_leaf(t);
  925. free_space = btrfs_leaf_free_space(root, left);
  926. if (free_space < data_size + sizeof(struct btrfs_item)) {
  927. btrfs_block_release(root, t);
  928. return 1;
  929. }
  930. /* cow and double check */
  931. btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
  932. left = btrfs_buffer_leaf(t);
  933. free_space = btrfs_leaf_free_space(root, left);
  934. if (free_space < data_size + sizeof(struct btrfs_item)) {
  935. btrfs_block_release(root, t);
  936. return 1;
  937. }
  938. for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
  939. item = right->items + i;
  940. if (path->slots[0] == i)
  941. push_space += data_size + sizeof(*item);
  942. if (btrfs_item_size(item) + sizeof(*item) + push_space >
  943. free_space)
  944. break;
  945. push_items++;
  946. push_space += btrfs_item_size(item) + sizeof(*item);
  947. }
  948. if (push_items == 0) {
  949. btrfs_block_release(root, t);
  950. return 1;
  951. }
  952. /* push data from right to left */
  953. btrfs_memcpy(root, left, left->items +
  954. btrfs_header_nritems(&left->header),
  955. right->items, push_items * sizeof(struct btrfs_item));
  956. push_space = BTRFS_LEAF_DATA_SIZE(root) -
  957. btrfs_item_offset(right->items + push_items -1);
  958. btrfs_memcpy(root, left, btrfs_leaf_data(left) +
  959. leaf_data_end(root, left) - push_space,
  960. btrfs_leaf_data(right) +
  961. btrfs_item_offset(right->items + push_items - 1),
  962. push_space);
  963. old_left_nritems = btrfs_header_nritems(&left->header);
  964. BUG_ON(old_left_nritems < 0);
  965. for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
  966. u32 ioff = btrfs_item_offset(left->items + i);
  967. btrfs_set_item_offset(left->items + i, ioff -
  968. (BTRFS_LEAF_DATA_SIZE(root) -
  969. btrfs_item_offset(left->items +
  970. old_left_nritems - 1)));
  971. }
  972. btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
  973. /* fixup right node */
  974. push_space = btrfs_item_offset(right->items + push_items - 1) -
  975. leaf_data_end(root, right);
  976. btrfs_memmove(root, right, btrfs_leaf_data(right) +
  977. BTRFS_LEAF_DATA_SIZE(root) - push_space,
  978. btrfs_leaf_data(right) +
  979. leaf_data_end(root, right), push_space);
  980. btrfs_memmove(root, right, right->items, right->items + push_items,
  981. (btrfs_header_nritems(&right->header) - push_items) *
  982. sizeof(struct btrfs_item));
  983. btrfs_set_header_nritems(&right->header,
  984. btrfs_header_nritems(&right->header) -
  985. push_items);
  986. push_space = BTRFS_LEAF_DATA_SIZE(root);
  987. for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
  988. btrfs_set_item_offset(right->items + i, push_space -
  989. btrfs_item_size(right->items + i));
  990. push_space = btrfs_item_offset(right->items + i);
  991. }
  992. btrfs_mark_buffer_dirty(t);
  993. btrfs_mark_buffer_dirty(right_buf);
  994. wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
  995. if (wret)
  996. ret = wret;
  997. /* then fixup the leaf pointer in the path */
  998. if (path->slots[0] < push_items) {
  999. path->slots[0] += old_left_nritems;
  1000. btrfs_block_release(root, path->nodes[0]);
  1001. path->nodes[0] = t;
  1002. path->slots[1] -= 1;
  1003. } else {
  1004. btrfs_block_release(root, t);
  1005. path->slots[0] -= push_items;
  1006. }
  1007. BUG_ON(path->slots[0] < 0);
  1008. return ret;
  1009. }
  1010. /*
  1011. * split the path's leaf in two, making sure there is at least data_size
  1012. * available for the resulting leaf level of the path.
  1013. *
  1014. * returns 0 if all went well and < 0 on failure.
  1015. */
  1016. static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
  1017. *root, struct btrfs_key *ins_key,
  1018. struct btrfs_path *path, int data_size)
  1019. {
  1020. struct buffer_head *l_buf;
  1021. struct btrfs_leaf *l;
  1022. u32 nritems;
  1023. int mid;
  1024. int slot;
  1025. struct btrfs_leaf *right;
  1026. struct buffer_head *right_buffer;
  1027. int space_needed = data_size + sizeof(struct btrfs_item);
  1028. int data_copy_size;
  1029. int rt_data_off;
  1030. int i;
  1031. int ret = 0;
  1032. int wret;
  1033. int double_split = 0;
  1034. struct btrfs_disk_key disk_key;
  1035. /* first try to make some room by pushing left and right */
  1036. wret = push_leaf_left(trans, root, path, data_size);
  1037. if (wret < 0)
  1038. return wret;
  1039. if (wret) {
  1040. wret = push_leaf_right(trans, root, path, data_size);
  1041. if (wret < 0)
  1042. return wret;
  1043. }
  1044. l_buf = path->nodes[0];
  1045. l = btrfs_buffer_leaf(l_buf);
  1046. /* did the pushes work? */
  1047. if (btrfs_leaf_free_space(root, l) >=
  1048. sizeof(struct btrfs_item) + data_size)
  1049. return 0;
  1050. if (!path->nodes[1]) {
  1051. ret = insert_new_root(trans, root, path, 1);
  1052. if (ret)
  1053. return ret;
  1054. }
  1055. slot = path->slots[0];
  1056. nritems = btrfs_header_nritems(&l->header);
  1057. mid = (nritems + 1)/ 2;
  1058. right_buffer = btrfs_alloc_free_block(trans, root);
  1059. BUG_ON(!right_buffer);
  1060. right = btrfs_buffer_leaf(right_buffer);
  1061. memset(&right->header, 0, sizeof(right->header));
  1062. btrfs_set_header_blocknr(&right->header, right_buffer->b_blocknr);
  1063. btrfs_set_header_generation(&right->header, trans->transid);
  1064. btrfs_set_header_level(&right->header, 0);
  1065. btrfs_set_header_parentid(&right->header,
  1066. btrfs_header_parentid(btrfs_buffer_header(root->node)));
  1067. memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
  1068. sizeof(right->header.fsid));
  1069. if (mid <= slot) {
  1070. if (nritems == 1 ||
  1071. leaf_space_used(l, mid, nritems - mid) + space_needed >
  1072. BTRFS_LEAF_DATA_SIZE(root)) {
  1073. if (slot >= nritems) {
  1074. btrfs_cpu_key_to_disk(&disk_key, ins_key);
  1075. btrfs_set_header_nritems(&right->header, 0);
  1076. wret = insert_ptr(trans, root, path,
  1077. &disk_key,
  1078. right_buffer->b_blocknr,
  1079. path->slots[1] + 1, 1);
  1080. if (wret)
  1081. ret = wret;
  1082. btrfs_block_release(root, path->nodes[0]);
  1083. path->nodes[0] = right_buffer;
  1084. path->slots[0] = 0;
  1085. path->slots[1] += 1;
  1086. return ret;
  1087. }
  1088. mid = slot;
  1089. double_split = 1;
  1090. }
  1091. } else {
  1092. if (leaf_space_used(l, 0, mid + 1) + space_needed >
  1093. BTRFS_LEAF_DATA_SIZE(root)) {
  1094. if (slot == 0) {
  1095. btrfs_cpu_key_to_disk(&disk_key, ins_key);
  1096. btrfs_set_header_nritems(&right->header, 0);
  1097. wret = insert_ptr(trans, root, path,
  1098. &disk_key,
  1099. right_buffer->b_blocknr,
  1100. path->slots[1] - 1, 1);
  1101. if (wret)
  1102. ret = wret;
  1103. btrfs_block_release(root, path->nodes[0]);
  1104. path->nodes[0] = right_buffer;
  1105. path->slots[0] = 0;
  1106. path->slots[1] -= 1;
  1107. return ret;
  1108. }
  1109. mid = slot;
  1110. double_split = 1;
  1111. }
  1112. }
  1113. btrfs_set_header_nritems(&right->header, nritems - mid);
  1114. data_copy_size = btrfs_item_end(l->items + mid) -
  1115. leaf_data_end(root, l);
  1116. btrfs_memcpy(root, right, right->items, l->items + mid,
  1117. (nritems - mid) * sizeof(struct btrfs_item));
  1118. btrfs_memcpy(root, right,
  1119. btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
  1120. data_copy_size, btrfs_leaf_data(l) +
  1121. leaf_data_end(root, l), data_copy_size);
  1122. rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
  1123. btrfs_item_end(l->items + mid);
  1124. for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
  1125. u32 ioff = btrfs_item_offset(right->items + i);
  1126. btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
  1127. }
  1128. btrfs_set_header_nritems(&l->header, mid);
  1129. ret = 0;
  1130. wret = insert_ptr(trans, root, path, &right->items[0].key,
  1131. right_buffer->b_blocknr, path->slots[1] + 1, 1);
  1132. if (wret)
  1133. ret = wret;
  1134. btrfs_mark_buffer_dirty(right_buffer);
  1135. btrfs_mark_buffer_dirty(l_buf);
  1136. BUG_ON(path->slots[0] != slot);
  1137. if (mid <= slot) {
  1138. btrfs_block_release(root, path->nodes[0]);
  1139. path->nodes[0] = right_buffer;
  1140. path->slots[0] -= mid;
  1141. path->slots[1] += 1;
  1142. } else
  1143. btrfs_block_release(root, right_buffer);
  1144. BUG_ON(path->slots[0] < 0);
  1145. if (!double_split)
  1146. return ret;
  1147. right_buffer = btrfs_alloc_free_block(trans, root);
  1148. BUG_ON(!right_buffer);
  1149. right = btrfs_buffer_leaf(right_buffer);
  1150. memset(&right->header, 0, sizeof(right->header));
  1151. btrfs_set_header_blocknr(&right->header, right_buffer->b_blocknr);
  1152. btrfs_set_header_generation(&right->header, trans->transid);
  1153. btrfs_set_header_level(&right->header, 0);
  1154. btrfs_set_header_parentid(&right->header,
  1155. btrfs_header_parentid(btrfs_buffer_header(root->node)));
  1156. memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
  1157. sizeof(right->header.fsid));
  1158. btrfs_cpu_key_to_disk(&disk_key, ins_key);
  1159. btrfs_set_header_nritems(&right->header, 0);
  1160. wret = insert_ptr(trans, root, path,
  1161. &disk_key,
  1162. right_buffer->b_blocknr,
  1163. path->slots[1], 1);
  1164. if (wret)
  1165. ret = wret;
  1166. btrfs_block_release(root, path->nodes[0]);
  1167. path->nodes[0] = right_buffer;
  1168. path->slots[0] = 0;
  1169. check_node(root, path, 1);
  1170. check_leaf(root, path, 0);
  1171. return ret;
  1172. }
  1173. /*
  1174. * Given a key and some data, insert an item into the tree.
  1175. * This does all the path init required, making room in the tree if needed.
  1176. */
  1177. int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
  1178. *root, struct btrfs_path *path, struct btrfs_key
  1179. *cpu_key, u32 data_size)
  1180. {
  1181. int ret = 0;
  1182. int slot;
  1183. int slot_orig;
  1184. struct btrfs_leaf *leaf;
  1185. struct buffer_head *leaf_buf;
  1186. u32 nritems;
  1187. unsigned int data_end;
  1188. struct btrfs_disk_key disk_key;
  1189. btrfs_cpu_key_to_disk(&disk_key, cpu_key);
  1190. /* create a root if there isn't one */
  1191. if (!root->node)
  1192. BUG();
  1193. ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
  1194. if (ret == 0) {
  1195. return -EEXIST;
  1196. }
  1197. if (ret < 0)
  1198. goto out;
  1199. slot_orig = path->slots[0];
  1200. leaf_buf = path->nodes[0];
  1201. leaf = btrfs_buffer_leaf(leaf_buf);
  1202. nritems = btrfs_header_nritems(&leaf->header);
  1203. data_end = leaf_data_end(root, leaf);
  1204. if (btrfs_leaf_free_space(root, leaf) <
  1205. sizeof(struct btrfs_item) + data_size) {
  1206. BUG();
  1207. }
  1208. slot = path->slots[0];
  1209. BUG_ON(slot < 0);
  1210. if (slot != nritems) {
  1211. int i;
  1212. unsigned int old_data = btrfs_item_end(leaf->items + slot);
  1213. /*
  1214. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  1215. */
  1216. /* first correct the data pointers */
  1217. for (i = slot; i < nritems; i++) {
  1218. u32 ioff = btrfs_item_offset(leaf->items + i);
  1219. btrfs_set_item_offset(leaf->items + i,
  1220. ioff - data_size);
  1221. }
  1222. /* shift the items */
  1223. btrfs_memmove(root, leaf, leaf->items + slot + 1,
  1224. leaf->items + slot,
  1225. (nritems - slot) * sizeof(struct btrfs_item));
  1226. /* shift the data */
  1227. btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
  1228. data_end - data_size, btrfs_leaf_data(leaf) +
  1229. data_end, old_data - data_end);
  1230. data_end = old_data;
  1231. }
  1232. /* setup the item for the new data */
  1233. btrfs_memcpy(root, leaf, &leaf->items[slot].key, &disk_key,
  1234. sizeof(struct btrfs_disk_key));
  1235. btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
  1236. btrfs_set_item_size(leaf->items + slot, data_size);
  1237. btrfs_set_header_nritems(&leaf->header, nritems + 1);
  1238. btrfs_mark_buffer_dirty(leaf_buf);
  1239. ret = 0;
  1240. if (slot == 0)
  1241. ret = fixup_low_keys(trans, root, path, &disk_key, 1);
  1242. if (btrfs_leaf_free_space(root, leaf) < 0)
  1243. BUG();
  1244. check_leaf(root, path, 0);
  1245. out:
  1246. return ret;
  1247. }
  1248. /*
  1249. * Given a key and some data, insert an item into the tree.
  1250. * This does all the path init required, making room in the tree if needed.
  1251. */
  1252. int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
  1253. *root, struct btrfs_key *cpu_key, void *data, u32
  1254. data_size)
  1255. {
  1256. int ret = 0;
  1257. struct btrfs_path *path;
  1258. u8 *ptr;
  1259. path = btrfs_alloc_path();
  1260. BUG_ON(!path);
  1261. btrfs_init_path(path);
  1262. ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
  1263. if (!ret) {
  1264. ptr = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1265. path->slots[0], u8);
  1266. btrfs_memcpy(root, path->nodes[0]->b_data,
  1267. ptr, data, data_size);
  1268. btrfs_mark_buffer_dirty(path->nodes[0]);
  1269. }
  1270. btrfs_release_path(root, path);
  1271. btrfs_free_path(path);
  1272. return ret;
  1273. }
  1274. /*
  1275. * delete the pointer from a given node.
  1276. *
  1277. * If the delete empties a node, the node is removed from the tree,
  1278. * continuing all the way the root if required. The root is converted into
  1279. * a leaf if all the nodes are emptied.
  1280. */
  1281. static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  1282. struct btrfs_path *path, int level, int slot)
  1283. {
  1284. struct btrfs_node *node;
  1285. struct buffer_head *parent = path->nodes[level];
  1286. u32 nritems;
  1287. int ret = 0;
  1288. int wret;
  1289. node = btrfs_buffer_node(parent);
  1290. nritems = btrfs_header_nritems(&node->header);
  1291. if (slot != nritems -1) {
  1292. btrfs_memmove(root, node, node->ptrs + slot,
  1293. node->ptrs + slot + 1,
  1294. sizeof(struct btrfs_key_ptr) *
  1295. (nritems - slot - 1));
  1296. }
  1297. nritems--;
  1298. btrfs_set_header_nritems(&node->header, nritems);
  1299. if (nritems == 0 && parent == root->node) {
  1300. struct btrfs_header *header = btrfs_buffer_header(root->node);
  1301. BUG_ON(btrfs_header_level(header) != 1);
  1302. /* just turn the root into a leaf and break */
  1303. btrfs_set_header_level(header, 0);
  1304. } else if (slot == 0) {
  1305. wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
  1306. level + 1);
  1307. if (wret)
  1308. ret = wret;
  1309. }
  1310. btrfs_mark_buffer_dirty(parent);
  1311. return ret;
  1312. }
  1313. /*
  1314. * delete the item at the leaf level in path. If that empties
  1315. * the leaf, remove it from the tree
  1316. */
  1317. int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  1318. struct btrfs_path *path)
  1319. {
  1320. int slot;
  1321. struct btrfs_leaf *leaf;
  1322. struct buffer_head *leaf_buf;
  1323. int doff;
  1324. int dsize;
  1325. int ret = 0;
  1326. int wret;
  1327. u32 nritems;
  1328. leaf_buf = path->nodes[0];
  1329. leaf = btrfs_buffer_leaf(leaf_buf);
  1330. slot = path->slots[0];
  1331. doff = btrfs_item_offset(leaf->items + slot);
  1332. dsize = btrfs_item_size(leaf->items + slot);
  1333. nritems = btrfs_header_nritems(&leaf->header);
  1334. if (slot != nritems - 1) {
  1335. int i;
  1336. int data_end = leaf_data_end(root, leaf);
  1337. btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
  1338. data_end + dsize,
  1339. btrfs_leaf_data(leaf) + data_end,
  1340. doff - data_end);
  1341. for (i = slot + 1; i < nritems; i++) {
  1342. u32 ioff = btrfs_item_offset(leaf->items + i);
  1343. btrfs_set_item_offset(leaf->items + i, ioff + dsize);
  1344. }
  1345. btrfs_memmove(root, leaf, leaf->items + slot,
  1346. leaf->items + slot + 1,
  1347. sizeof(struct btrfs_item) *
  1348. (nritems - slot - 1));
  1349. }
  1350. btrfs_set_header_nritems(&leaf->header, nritems - 1);
  1351. nritems--;
  1352. /* delete the leaf if we've emptied it */
  1353. if (nritems == 0) {
  1354. if (leaf_buf == root->node) {
  1355. btrfs_set_header_level(&leaf->header, 0);
  1356. } else {
  1357. clean_tree_block(trans, root, leaf_buf);
  1358. wait_on_buffer(leaf_buf);
  1359. wret = del_ptr(trans, root, path, 1, path->slots[1]);
  1360. if (wret)
  1361. ret = wret;
  1362. wret = btrfs_free_extent(trans, root,
  1363. leaf_buf->b_blocknr, 1, 1);
  1364. if (wret)
  1365. ret = wret;
  1366. }
  1367. } else {
  1368. int used = leaf_space_used(leaf, 0, nritems);
  1369. if (slot == 0) {
  1370. wret = fixup_low_keys(trans, root, path,
  1371. &leaf->items[0].key, 1);
  1372. if (wret)
  1373. ret = wret;
  1374. }
  1375. /* delete the leaf if it is mostly empty */
  1376. if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
  1377. /* push_leaf_left fixes the path.
  1378. * make sure the path still points to our leaf
  1379. * for possible call to del_ptr below
  1380. */
  1381. slot = path->slots[1];
  1382. get_bh(leaf_buf);
  1383. wret = push_leaf_left(trans, root, path, 1);
  1384. if (wret < 0)
  1385. ret = wret;
  1386. if (path->nodes[0] == leaf_buf &&
  1387. btrfs_header_nritems(&leaf->header)) {
  1388. wret = push_leaf_right(trans, root, path, 1);
  1389. if (wret < 0)
  1390. ret = wret;
  1391. }
  1392. if (btrfs_header_nritems(&leaf->header) == 0) {
  1393. u64 blocknr = leaf_buf->b_blocknr;
  1394. clean_tree_block(trans, root, leaf_buf);
  1395. wait_on_buffer(leaf_buf);
  1396. wret = del_ptr(trans, root, path, 1, slot);
  1397. if (wret)
  1398. ret = wret;
  1399. btrfs_block_release(root, leaf_buf);
  1400. wret = btrfs_free_extent(trans, root, blocknr,
  1401. 1, 1);
  1402. if (wret)
  1403. ret = wret;
  1404. } else {
  1405. btrfs_mark_buffer_dirty(leaf_buf);
  1406. btrfs_block_release(root, leaf_buf);
  1407. }
  1408. } else {
  1409. btrfs_mark_buffer_dirty(leaf_buf);
  1410. }
  1411. }
  1412. return ret;
  1413. }
  1414. /*
  1415. * walk up the tree as far as required to find the next leaf.
  1416. * returns 0 if it found something or 1 if there are no greater leaves.
  1417. * returns < 0 on io errors.
  1418. */
  1419. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
  1420. {
  1421. int slot;
  1422. int level = 1;
  1423. u64 blocknr;
  1424. struct buffer_head *c;
  1425. struct btrfs_node *c_node;
  1426. struct buffer_head *next = NULL;
  1427. while(level < BTRFS_MAX_LEVEL) {
  1428. if (!path->nodes[level])
  1429. return 1;
  1430. slot = path->slots[level] + 1;
  1431. c = path->nodes[level];
  1432. c_node = btrfs_buffer_node(c);
  1433. if (slot >= btrfs_header_nritems(&c_node->header)) {
  1434. level++;
  1435. continue;
  1436. }
  1437. blocknr = btrfs_node_blockptr(c_node, slot);
  1438. if (next)
  1439. btrfs_block_release(root, next);
  1440. next = read_tree_block(root, blocknr);
  1441. break;
  1442. }
  1443. path->slots[level] = slot;
  1444. while(1) {
  1445. level--;
  1446. c = path->nodes[level];
  1447. btrfs_block_release(root, c);
  1448. path->nodes[level] = next;
  1449. path->slots[level] = 0;
  1450. if (!level)
  1451. break;
  1452. next = read_tree_block(root,
  1453. btrfs_node_blockptr(btrfs_buffer_node(next), 0));
  1454. }
  1455. return 0;
  1456. }