ctree.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/sched.h>
  19. #include "ctree.h"
  20. #include "disk-io.h"
  21. #include "transaction.h"
  22. #include "print-tree.h"
  23. static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
  24. *root, struct btrfs_path *path, int level);
  25. static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
  26. *root, struct btrfs_key *ins_key,
  27. struct btrfs_path *path, int data_size);
  28. static int push_node_left(struct btrfs_trans_handle *trans,
  29. struct btrfs_root *root, struct extent_buffer *dst,
  30. struct extent_buffer *src);
  31. static int balance_node_right(struct btrfs_trans_handle *trans,
  32. struct btrfs_root *root,
  33. struct extent_buffer *dst_buf,
  34. struct extent_buffer *src_buf);
  35. static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  36. struct btrfs_path *path, int level, int slot);
  37. inline void btrfs_init_path(struct btrfs_path *p)
  38. {
  39. memset(p, 0, sizeof(*p));
  40. }
  41. struct btrfs_path *btrfs_alloc_path(void)
  42. {
  43. struct btrfs_path *path;
  44. path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
  45. if (path) {
  46. btrfs_init_path(path);
  47. path->reada = 1;
  48. }
  49. return path;
  50. }
  51. void btrfs_free_path(struct btrfs_path *p)
  52. {
  53. btrfs_release_path(NULL, p);
  54. kmem_cache_free(btrfs_path_cachep, p);
  55. }
  56. void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
  57. {
  58. int i;
  59. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  60. if (!p->nodes[i])
  61. break;
  62. free_extent_buffer(p->nodes[i]);
  63. }
  64. memset(p, 0, sizeof(*p));
  65. }
  66. static int __btrfs_cow_block(struct btrfs_trans_handle *trans,
  67. struct btrfs_root *root,
  68. struct extent_buffer *buf,
  69. struct extent_buffer *parent, int parent_slot,
  70. struct extent_buffer **cow_ret,
  71. u64 search_start, u64 empty_size)
  72. {
  73. struct extent_buffer *cow;
  74. int ret = 0;
  75. int different_trans = 0;
  76. WARN_ON(root->ref_cows && trans->transid != root->last_trans);
  77. cow = btrfs_alloc_free_block(trans, root, buf->len,
  78. search_start, empty_size);
  79. if (IS_ERR(cow))
  80. return PTR_ERR(cow);
  81. copy_extent_buffer(cow, buf, 0, 0, cow->len);
  82. btrfs_set_header_bytenr(cow, cow->start);
  83. btrfs_set_header_generation(cow, trans->transid);
  84. btrfs_set_header_owner(cow, root->root_key.objectid);
  85. WARN_ON(btrfs_header_generation(buf) > trans->transid);
  86. if (btrfs_header_generation(buf) != trans->transid) {
  87. different_trans = 1;
  88. ret = btrfs_inc_ref(trans, root, buf);
  89. if (ret)
  90. return ret;
  91. } else {
  92. clean_tree_block(trans, root, buf);
  93. }
  94. if (buf == root->node) {
  95. root->node = cow;
  96. extent_buffer_get(cow);
  97. if (buf != root->commit_root) {
  98. btrfs_free_extent(trans, root, buf->start,
  99. buf->len, 1);
  100. }
  101. free_extent_buffer(buf);
  102. } else {
  103. btrfs_set_node_blockptr(parent, parent_slot,
  104. cow->start);
  105. btrfs_mark_buffer_dirty(parent);
  106. WARN_ON(btrfs_header_generation(parent) != trans->transid);
  107. btrfs_free_extent(trans, root, buf->start, buf->len, 1);
  108. }
  109. free_extent_buffer(buf);
  110. btrfs_mark_buffer_dirty(cow);
  111. *cow_ret = cow;
  112. return 0;
  113. }
  114. int btrfs_cow_block(struct btrfs_trans_handle *trans,
  115. struct btrfs_root *root, struct extent_buffer *buf,
  116. struct extent_buffer *parent, int parent_slot,
  117. struct extent_buffer **cow_ret)
  118. {
  119. u64 search_start;
  120. int ret;
  121. if (trans->transaction != root->fs_info->running_transaction) {
  122. printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
  123. root->fs_info->running_transaction->transid);
  124. WARN_ON(1);
  125. }
  126. if (trans->transid != root->fs_info->generation) {
  127. printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
  128. root->fs_info->generation);
  129. WARN_ON(1);
  130. }
  131. if (btrfs_header_generation(buf) == trans->transid) {
  132. *cow_ret = buf;
  133. return 0;
  134. }
  135. search_start = buf->start & ~((u64)BTRFS_BLOCK_GROUP_SIZE - 1);
  136. ret = __btrfs_cow_block(trans, root, buf, parent,
  137. parent_slot, cow_ret, search_start, 0);
  138. return ret;
  139. }
  140. static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
  141. {
  142. if (blocknr < other && other - (blocknr + blocksize) < 32768)
  143. return 1;
  144. if (blocknr > other && blocknr - (other + blocksize) < 32768)
  145. return 1;
  146. return 0;
  147. }
  148. static int should_defrag_leaf(struct extent_buffer *leaf)
  149. {
  150. struct btrfs_key key;
  151. u32 nritems;
  152. if (btrfs_buffer_defrag(leaf))
  153. return 1;
  154. nritems = btrfs_header_nritems(leaf);
  155. if (nritems == 0)
  156. return 0;
  157. btrfs_item_key_to_cpu(leaf, &key, 0);
  158. if (key.type == BTRFS_DIR_ITEM_KEY)
  159. return 1;
  160. btrfs_item_key_to_cpu(leaf, &key, nritems - 1);
  161. if (key.type == BTRFS_DIR_ITEM_KEY)
  162. return 1;
  163. if (nritems > 4) {
  164. btrfs_item_key_to_cpu(leaf, &key, nritems / 2);
  165. if (key.type == BTRFS_DIR_ITEM_KEY)
  166. return 1;
  167. }
  168. return 0;
  169. }
  170. int btrfs_realloc_node(struct btrfs_trans_handle *trans,
  171. struct btrfs_root *root, struct extent_buffer *parent,
  172. int start_slot, int cache_only, u64 *last_ret,
  173. struct btrfs_key *progress)
  174. {
  175. struct extent_buffer *cur;
  176. struct extent_buffer *tmp;
  177. u64 blocknr;
  178. u64 search_start = *last_ret;
  179. u64 last_block = 0;
  180. u64 other;
  181. u32 parent_nritems;
  182. int end_slot;
  183. int i;
  184. int err = 0;
  185. int parent_level;
  186. int uptodate;
  187. u32 blocksize;
  188. if (trans->transaction != root->fs_info->running_transaction) {
  189. printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
  190. root->fs_info->running_transaction->transid);
  191. WARN_ON(1);
  192. }
  193. if (trans->transid != root->fs_info->generation) {
  194. printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
  195. root->fs_info->generation);
  196. WARN_ON(1);
  197. }
  198. parent_level = btrfs_header_level(parent);
  199. parent_nritems = btrfs_header_nritems(parent);
  200. blocksize = btrfs_level_size(root, parent_level - 1);
  201. end_slot = parent_nritems;
  202. if (parent_nritems == 1)
  203. return 0;
  204. if (root != root->fs_info->extent_root) {
  205. struct btrfs_key first_key;
  206. struct btrfs_key last_key;
  207. btrfs_node_key_to_cpu(parent, &first_key, 0);
  208. btrfs_node_key_to_cpu(parent, &last_key, parent_nritems - 1);
  209. if (first_key.objectid != last_key.objectid)
  210. return 0;
  211. }
  212. for (i = start_slot; i < end_slot; i++) {
  213. int close = 1;
  214. blocknr = btrfs_node_blockptr(parent, i);
  215. if (last_block == 0)
  216. last_block = blocknr;
  217. if (i > 0) {
  218. other = btrfs_node_blockptr(parent, i - 1);
  219. close = close_blocks(blocknr, other, blocksize);
  220. }
  221. if (close && i < end_slot - 1) {
  222. other = btrfs_node_blockptr(parent, i + 1);
  223. close = close_blocks(blocknr, other, blocksize);
  224. }
  225. if (close) {
  226. last_block = blocknr;
  227. continue;
  228. }
  229. cur = btrfs_find_tree_block(root, blocknr, blocksize);
  230. if (cur)
  231. uptodate = btrfs_buffer_uptodate(cur);
  232. else
  233. uptodate = 0;
  234. if (!cur || !uptodate ||
  235. (parent_level != 1 && !btrfs_buffer_defrag(cur)) ||
  236. (parent_level == 1 && !should_defrag_leaf(cur))) {
  237. if (cache_only) {
  238. free_extent_buffer(cur);
  239. continue;
  240. }
  241. if (!cur) {
  242. cur = read_tree_block(root, blocknr,
  243. blocksize);
  244. } else if (!uptodate) {
  245. btrfs_read_buffer(cur);
  246. }
  247. }
  248. if (search_start == 0)
  249. search_start = last_block;
  250. err = __btrfs_cow_block(trans, root, cur, parent, i,
  251. &tmp, search_start,
  252. min(16 * blocksize,
  253. (end_slot - i) * blocksize));
  254. if (err) {
  255. free_extent_buffer(cur);
  256. break;
  257. }
  258. search_start = tmp->start;
  259. *last_ret = search_start;
  260. if (parent_level == 1)
  261. btrfs_clear_buffer_defrag(tmp);
  262. free_extent_buffer(tmp);
  263. }
  264. return err;
  265. }
  266. /*
  267. * The leaf data grows from end-to-front in the node.
  268. * this returns the address of the start of the last item,
  269. * which is the stop of the leaf data stack
  270. */
  271. static inline unsigned int leaf_data_end(struct btrfs_root *root,
  272. struct extent_buffer *leaf)
  273. {
  274. u32 nr = btrfs_header_nritems(leaf);
  275. if (nr == 0)
  276. return BTRFS_LEAF_DATA_SIZE(root);
  277. return btrfs_item_offset_nr(leaf, nr - 1);
  278. }
  279. /*
  280. * compare two keys in a memcmp fashion
  281. */
  282. static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
  283. {
  284. struct btrfs_key k1;
  285. btrfs_disk_key_to_cpu(&k1, disk);
  286. if (k1.objectid > k2->objectid)
  287. return 1;
  288. if (k1.objectid < k2->objectid)
  289. return -1;
  290. if (k1.type > k2->type)
  291. return 1;
  292. if (k1.type < k2->type)
  293. return -1;
  294. if (k1.offset > k2->offset)
  295. return 1;
  296. if (k1.offset < k2->offset)
  297. return -1;
  298. return 0;
  299. }
  300. static int check_node(struct btrfs_root *root, struct btrfs_path *path,
  301. int level)
  302. {
  303. struct extent_buffer *parent = NULL;
  304. struct extent_buffer *node = path->nodes[level];
  305. struct btrfs_disk_key parent_key;
  306. struct btrfs_disk_key node_key;
  307. int parent_slot;
  308. int slot;
  309. struct btrfs_key cpukey;
  310. u32 nritems = btrfs_header_nritems(node);
  311. if (path->nodes[level + 1])
  312. parent = path->nodes[level + 1];
  313. slot = path->slots[level];
  314. BUG_ON(nritems == 0);
  315. if (parent) {
  316. parent_slot = path->slots[level + 1];
  317. btrfs_node_key(parent, &parent_key, parent_slot);
  318. btrfs_node_key(node, &node_key, 0);
  319. BUG_ON(memcmp(&parent_key, &node_key,
  320. sizeof(struct btrfs_disk_key)));
  321. BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
  322. btrfs_header_bytenr(node));
  323. }
  324. BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
  325. if (slot != 0) {
  326. btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
  327. btrfs_node_key(node, &node_key, slot);
  328. BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
  329. }
  330. if (slot < nritems - 1) {
  331. btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
  332. btrfs_node_key(node, &node_key, slot);
  333. BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
  334. }
  335. return 0;
  336. }
  337. static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
  338. int level)
  339. {
  340. struct extent_buffer *leaf = path->nodes[level];
  341. struct extent_buffer *parent = NULL;
  342. int parent_slot;
  343. struct btrfs_key cpukey;
  344. struct btrfs_disk_key parent_key;
  345. struct btrfs_disk_key leaf_key;
  346. int slot = path->slots[0];
  347. u32 nritems = btrfs_header_nritems(leaf);
  348. if (path->nodes[level + 1])
  349. parent = path->nodes[level + 1];
  350. if (nritems == 0)
  351. return 0;
  352. if (parent) {
  353. parent_slot = path->slots[level + 1];
  354. btrfs_node_key(parent, &parent_key, parent_slot);
  355. btrfs_item_key(leaf, &leaf_key, 0);
  356. BUG_ON(memcmp(&parent_key, &leaf_key,
  357. sizeof(struct btrfs_disk_key)));
  358. BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
  359. btrfs_header_bytenr(leaf));
  360. }
  361. #if 0
  362. for (i = 0; nritems > 1 && i < nritems - 2; i++) {
  363. btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
  364. btrfs_item_key(leaf, &leaf_key, i);
  365. if (comp_keys(&leaf_key, &cpukey) >= 0) {
  366. btrfs_print_leaf(root, leaf);
  367. printk("slot %d offset bad key\n", i);
  368. BUG_ON(1);
  369. }
  370. if (btrfs_item_offset_nr(leaf, i) !=
  371. btrfs_item_end_nr(leaf, i + 1)) {
  372. btrfs_print_leaf(root, leaf);
  373. printk("slot %d offset bad\n", i);
  374. BUG_ON(1);
  375. }
  376. if (i == 0) {
  377. if (btrfs_item_offset_nr(leaf, i) +
  378. btrfs_item_size_nr(leaf, i) !=
  379. BTRFS_LEAF_DATA_SIZE(root)) {
  380. btrfs_print_leaf(root, leaf);
  381. printk("slot %d first offset bad\n", i);
  382. BUG_ON(1);
  383. }
  384. }
  385. }
  386. if (nritems > 0) {
  387. if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
  388. btrfs_print_leaf(root, leaf);
  389. printk("slot %d bad size \n", nritems - 1);
  390. BUG_ON(1);
  391. }
  392. }
  393. #endif
  394. if (slot != 0 && slot < nritems - 1) {
  395. btrfs_item_key(leaf, &leaf_key, slot);
  396. btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
  397. if (comp_keys(&leaf_key, &cpukey) <= 0) {
  398. btrfs_print_leaf(root, leaf);
  399. printk("slot %d offset bad key\n", slot);
  400. BUG_ON(1);
  401. }
  402. if (btrfs_item_offset_nr(leaf, slot - 1) !=
  403. btrfs_item_end_nr(leaf, slot)) {
  404. btrfs_print_leaf(root, leaf);
  405. printk("slot %d offset bad\n", slot);
  406. BUG_ON(1);
  407. }
  408. }
  409. if (slot < nritems - 1) {
  410. btrfs_item_key(leaf, &leaf_key, slot);
  411. btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
  412. BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
  413. if (btrfs_item_offset_nr(leaf, slot) !=
  414. btrfs_item_end_nr(leaf, slot + 1)) {
  415. btrfs_print_leaf(root, leaf);
  416. printk("slot %d offset bad\n", slot);
  417. BUG_ON(1);
  418. }
  419. }
  420. BUG_ON(btrfs_item_offset_nr(leaf, 0) +
  421. btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
  422. return 0;
  423. }
  424. static int check_block(struct btrfs_root *root, struct btrfs_path *path,
  425. int level)
  426. {
  427. return 0;
  428. #if 0
  429. struct extent_buffer *buf = path->nodes[level];
  430. if (memcmp_extent_buffer(buf, root->fs_info->fsid,
  431. (unsigned long)btrfs_header_fsid(buf),
  432. BTRFS_FSID_SIZE)) {
  433. printk("warning bad block %Lu\n", buf->start);
  434. return 1;
  435. }
  436. #endif
  437. if (level == 0)
  438. return check_leaf(root, path, level);
  439. return check_node(root, path, level);
  440. }
  441. /*
  442. * search for key in the extent_buffer. The items start at offset p,
  443. * and they are item_size apart. There are 'max' items in p.
  444. *
  445. * the slot in the array is returned via slot, and it points to
  446. * the place where you would insert key if it is not found in
  447. * the array.
  448. *
  449. * slot may point to max if the key is bigger than all of the keys
  450. */
  451. static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
  452. int item_size, struct btrfs_key *key,
  453. int max, int *slot)
  454. {
  455. int low = 0;
  456. int high = max;
  457. int mid;
  458. int ret;
  459. struct btrfs_disk_key *tmp = NULL;
  460. struct btrfs_disk_key unaligned;
  461. unsigned long offset;
  462. char *map_token = NULL;
  463. char *kaddr = NULL;
  464. unsigned long map_start = 0;
  465. unsigned long map_len = 0;
  466. int err;
  467. while(low < high) {
  468. mid = (low + high) / 2;
  469. offset = p + mid * item_size;
  470. if (!map_token || offset < map_start ||
  471. (offset + sizeof(struct btrfs_disk_key)) >
  472. map_start + map_len) {
  473. if (map_token) {
  474. unmap_extent_buffer(eb, map_token, KM_USER0);
  475. map_token = NULL;
  476. }
  477. err = map_extent_buffer(eb, offset,
  478. sizeof(struct btrfs_disk_key),
  479. &map_token, &kaddr,
  480. &map_start, &map_len, KM_USER0);
  481. if (!err) {
  482. tmp = (struct btrfs_disk_key *)(kaddr + offset -
  483. map_start);
  484. } else {
  485. read_extent_buffer(eb, &unaligned,
  486. offset, sizeof(unaligned));
  487. tmp = &unaligned;
  488. }
  489. } else {
  490. tmp = (struct btrfs_disk_key *)(kaddr + offset -
  491. map_start);
  492. }
  493. ret = comp_keys(tmp, key);
  494. if (ret < 0)
  495. low = mid + 1;
  496. else if (ret > 0)
  497. high = mid;
  498. else {
  499. *slot = mid;
  500. if (map_token)
  501. unmap_extent_buffer(eb, map_token, KM_USER0);
  502. return 0;
  503. }
  504. }
  505. *slot = low;
  506. if (map_token)
  507. unmap_extent_buffer(eb, map_token, KM_USER0);
  508. return 1;
  509. }
  510. /*
  511. * simple bin_search frontend that does the right thing for
  512. * leaves vs nodes
  513. */
  514. static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
  515. int level, int *slot)
  516. {
  517. if (level == 0) {
  518. return generic_bin_search(eb,
  519. offsetof(struct btrfs_leaf, items),
  520. sizeof(struct btrfs_item),
  521. key, btrfs_header_nritems(eb),
  522. slot);
  523. } else {
  524. return generic_bin_search(eb,
  525. offsetof(struct btrfs_node, ptrs),
  526. sizeof(struct btrfs_key_ptr),
  527. key, btrfs_header_nritems(eb),
  528. slot);
  529. }
  530. return -1;
  531. }
  532. static struct extent_buffer *read_node_slot(struct btrfs_root *root,
  533. struct extent_buffer *parent, int slot)
  534. {
  535. if (slot < 0)
  536. return NULL;
  537. if (slot >= btrfs_header_nritems(parent))
  538. return NULL;
  539. return read_tree_block(root, btrfs_node_blockptr(parent, slot),
  540. btrfs_level_size(root, btrfs_header_level(parent) - 1));
  541. }
  542. static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
  543. *root, struct btrfs_path *path, int level)
  544. {
  545. struct extent_buffer *right = NULL;
  546. struct extent_buffer *mid;
  547. struct extent_buffer *left = NULL;
  548. struct extent_buffer *parent = NULL;
  549. int ret = 0;
  550. int wret;
  551. int pslot;
  552. int orig_slot = path->slots[level];
  553. int err_on_enospc = 0;
  554. u64 orig_ptr;
  555. if (level == 0)
  556. return 0;
  557. mid = path->nodes[level];
  558. orig_ptr = btrfs_node_blockptr(mid, orig_slot);
  559. if (level < BTRFS_MAX_LEVEL - 1)
  560. parent = path->nodes[level + 1];
  561. pslot = path->slots[level + 1];
  562. /*
  563. * deal with the case where there is only one pointer in the root
  564. * by promoting the node below to a root
  565. */
  566. if (!parent) {
  567. struct extent_buffer *child;
  568. if (btrfs_header_nritems(mid) != 1)
  569. return 0;
  570. /* promote the child to a root */
  571. child = read_node_slot(root, mid, 0);
  572. BUG_ON(!child);
  573. root->node = child;
  574. path->nodes[level] = NULL;
  575. clean_tree_block(trans, root, mid);
  576. wait_on_tree_block_writeback(root, mid);
  577. /* once for the path */
  578. free_extent_buffer(mid);
  579. ret = btrfs_free_extent(trans, root, mid->start, mid->len, 1);
  580. /* once for the root ptr */
  581. free_extent_buffer(mid);
  582. return ret;
  583. }
  584. if (btrfs_header_nritems(mid) >
  585. BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
  586. return 0;
  587. if (btrfs_header_nritems(mid) < 2)
  588. err_on_enospc = 1;
  589. left = read_node_slot(root, parent, pslot - 1);
  590. if (left) {
  591. wret = btrfs_cow_block(trans, root, left,
  592. parent, pslot - 1, &left);
  593. if (wret) {
  594. ret = wret;
  595. goto enospc;
  596. }
  597. }
  598. right = read_node_slot(root, parent, pslot + 1);
  599. if (right) {
  600. wret = btrfs_cow_block(trans, root, right,
  601. parent, pslot + 1, &right);
  602. if (wret) {
  603. ret = wret;
  604. goto enospc;
  605. }
  606. }
  607. /* first, try to make some room in the middle buffer */
  608. if (left) {
  609. orig_slot += btrfs_header_nritems(left);
  610. wret = push_node_left(trans, root, left, mid);
  611. if (wret < 0)
  612. ret = wret;
  613. if (btrfs_header_nritems(mid) < 2)
  614. err_on_enospc = 1;
  615. }
  616. /*
  617. * then try to empty the right most buffer into the middle
  618. */
  619. if (right) {
  620. wret = push_node_left(trans, root, mid, right);
  621. if (wret < 0 && wret != -ENOSPC)
  622. ret = wret;
  623. if (btrfs_header_nritems(right) == 0) {
  624. u64 bytenr = right->start;
  625. u32 blocksize = right->len;
  626. clean_tree_block(trans, root, right);
  627. wait_on_tree_block_writeback(root, right);
  628. free_extent_buffer(right);
  629. right = NULL;
  630. wret = del_ptr(trans, root, path, level + 1, pslot +
  631. 1);
  632. if (wret)
  633. ret = wret;
  634. wret = btrfs_free_extent(trans, root, bytenr,
  635. blocksize, 1);
  636. if (wret)
  637. ret = wret;
  638. } else {
  639. struct btrfs_disk_key right_key;
  640. btrfs_node_key(right, &right_key, 0);
  641. btrfs_set_node_key(parent, &right_key, pslot + 1);
  642. btrfs_mark_buffer_dirty(parent);
  643. }
  644. }
  645. if (btrfs_header_nritems(mid) == 1) {
  646. /*
  647. * we're not allowed to leave a node with one item in the
  648. * tree during a delete. A deletion from lower in the tree
  649. * could try to delete the only pointer in this node.
  650. * So, pull some keys from the left.
  651. * There has to be a left pointer at this point because
  652. * otherwise we would have pulled some pointers from the
  653. * right
  654. */
  655. BUG_ON(!left);
  656. wret = balance_node_right(trans, root, mid, left);
  657. if (wret < 0) {
  658. ret = wret;
  659. goto enospc;
  660. }
  661. BUG_ON(wret == 1);
  662. }
  663. if (btrfs_header_nritems(mid) == 0) {
  664. /* we've managed to empty the middle node, drop it */
  665. u64 bytenr = mid->start;
  666. u32 blocksize = mid->len;
  667. clean_tree_block(trans, root, mid);
  668. wait_on_tree_block_writeback(root, mid);
  669. free_extent_buffer(mid);
  670. mid = NULL;
  671. wret = del_ptr(trans, root, path, level + 1, pslot);
  672. if (wret)
  673. ret = wret;
  674. wret = btrfs_free_extent(trans, root, bytenr, blocksize, 1);
  675. if (wret)
  676. ret = wret;
  677. } else {
  678. /* update the parent key to reflect our changes */
  679. struct btrfs_disk_key mid_key;
  680. btrfs_node_key(mid, &mid_key, 0);
  681. btrfs_set_node_key(parent, &mid_key, pslot);
  682. btrfs_mark_buffer_dirty(parent);
  683. }
  684. /* update the path */
  685. if (left) {
  686. if (btrfs_header_nritems(left) > orig_slot) {
  687. extent_buffer_get(left);
  688. path->nodes[level] = left;
  689. path->slots[level + 1] -= 1;
  690. path->slots[level] = orig_slot;
  691. if (mid)
  692. free_extent_buffer(mid);
  693. } else {
  694. orig_slot -= btrfs_header_nritems(left);
  695. path->slots[level] = orig_slot;
  696. }
  697. }
  698. /* double check we haven't messed things up */
  699. check_block(root, path, level);
  700. if (orig_ptr !=
  701. btrfs_node_blockptr(path->nodes[level], path->slots[level]))
  702. BUG();
  703. enospc:
  704. if (right)
  705. free_extent_buffer(right);
  706. if (left)
  707. free_extent_buffer(left);
  708. return ret;
  709. }
  710. /* returns zero if the push worked, non-zero otherwise */
  711. static int push_nodes_for_insert(struct btrfs_trans_handle *trans,
  712. struct btrfs_root *root,
  713. struct btrfs_path *path, int level)
  714. {
  715. struct extent_buffer *right = NULL;
  716. struct extent_buffer *mid;
  717. struct extent_buffer *left = NULL;
  718. struct extent_buffer *parent = NULL;
  719. int ret = 0;
  720. int wret;
  721. int pslot;
  722. int orig_slot = path->slots[level];
  723. u64 orig_ptr;
  724. if (level == 0)
  725. return 1;
  726. mid = path->nodes[level];
  727. orig_ptr = btrfs_node_blockptr(mid, orig_slot);
  728. if (level < BTRFS_MAX_LEVEL - 1)
  729. parent = path->nodes[level + 1];
  730. pslot = path->slots[level + 1];
  731. if (!parent)
  732. return 1;
  733. left = read_node_slot(root, parent, pslot - 1);
  734. /* first, try to make some room in the middle buffer */
  735. if (left) {
  736. u32 left_nr;
  737. left_nr = btrfs_header_nritems(left);
  738. if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
  739. wret = 1;
  740. } else {
  741. ret = btrfs_cow_block(trans, root, left, parent,
  742. pslot - 1, &left);
  743. if (ret)
  744. wret = 1;
  745. else {
  746. wret = push_node_left(trans, root,
  747. left, mid);
  748. }
  749. }
  750. if (wret < 0)
  751. ret = wret;
  752. if (wret == 0) {
  753. struct btrfs_disk_key disk_key;
  754. orig_slot += left_nr;
  755. btrfs_node_key(mid, &disk_key, 0);
  756. btrfs_set_node_key(parent, &disk_key, pslot);
  757. btrfs_mark_buffer_dirty(parent);
  758. if (btrfs_header_nritems(left) > orig_slot) {
  759. path->nodes[level] = left;
  760. path->slots[level + 1] -= 1;
  761. path->slots[level] = orig_slot;
  762. free_extent_buffer(mid);
  763. } else {
  764. orig_slot -=
  765. btrfs_header_nritems(left);
  766. path->slots[level] = orig_slot;
  767. free_extent_buffer(left);
  768. }
  769. return 0;
  770. }
  771. free_extent_buffer(left);
  772. }
  773. right= read_node_slot(root, parent, pslot + 1);
  774. /*
  775. * then try to empty the right most buffer into the middle
  776. */
  777. if (right) {
  778. u32 right_nr;
  779. right_nr = btrfs_header_nritems(right);
  780. if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
  781. wret = 1;
  782. } else {
  783. ret = btrfs_cow_block(trans, root, right,
  784. parent, pslot + 1,
  785. &right);
  786. if (ret)
  787. wret = 1;
  788. else {
  789. wret = balance_node_right(trans, root,
  790. right, mid);
  791. }
  792. }
  793. if (wret < 0)
  794. ret = wret;
  795. if (wret == 0) {
  796. struct btrfs_disk_key disk_key;
  797. btrfs_node_key(right, &disk_key, 0);
  798. btrfs_set_node_key(parent, &disk_key, pslot + 1);
  799. btrfs_mark_buffer_dirty(parent);
  800. if (btrfs_header_nritems(mid) <= orig_slot) {
  801. path->nodes[level] = right;
  802. path->slots[level + 1] += 1;
  803. path->slots[level] = orig_slot -
  804. btrfs_header_nritems(mid);
  805. free_extent_buffer(mid);
  806. } else {
  807. free_extent_buffer(right);
  808. }
  809. return 0;
  810. }
  811. free_extent_buffer(right);
  812. }
  813. return 1;
  814. }
  815. /*
  816. * readahead one full node of leaves
  817. */
  818. static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
  819. int level, int slot)
  820. {
  821. struct extent_buffer *node;
  822. u32 nritems;
  823. u64 search;
  824. u64 lowest_read;
  825. u64 highest_read;
  826. u64 nread = 0;
  827. int direction = path->reada;
  828. struct extent_buffer *eb;
  829. u32 nr;
  830. u32 blocksize;
  831. u32 nscan = 0;
  832. if (level != 1)
  833. return;
  834. if (!path->nodes[level])
  835. return;
  836. node = path->nodes[level];
  837. search = btrfs_node_blockptr(node, slot);
  838. blocksize = btrfs_level_size(root, level - 1);
  839. eb = btrfs_find_tree_block(root, search, blocksize);
  840. if (eb) {
  841. free_extent_buffer(eb);
  842. return;
  843. }
  844. highest_read = search;
  845. lowest_read = search;
  846. nritems = btrfs_header_nritems(node);
  847. nr = slot;
  848. while(1) {
  849. if (direction < 0) {
  850. if (nr == 0)
  851. break;
  852. nr--;
  853. } else if (direction > 0) {
  854. nr++;
  855. if (nr >= nritems)
  856. break;
  857. }
  858. search = btrfs_node_blockptr(node, nr);
  859. if ((search >= lowest_read && search <= highest_read) ||
  860. (search < lowest_read && lowest_read - search <= 32768) ||
  861. (search > highest_read && search - highest_read <= 32768)) {
  862. readahead_tree_block(root, search, blocksize);
  863. nread += blocksize;
  864. }
  865. nscan++;
  866. if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
  867. break;
  868. if(nread > (1024 * 1024) || nscan > 128)
  869. break;
  870. if (search < lowest_read)
  871. lowest_read = search;
  872. if (search > highest_read)
  873. highest_read = search;
  874. }
  875. }
  876. /*
  877. * look for key in the tree. path is filled in with nodes along the way
  878. * if key is found, we return zero and you can find the item in the leaf
  879. * level of the path (level 0)
  880. *
  881. * If the key isn't found, the path points to the slot where it should
  882. * be inserted, and 1 is returned. If there are other errors during the
  883. * search a negative error number is returned.
  884. *
  885. * if ins_len > 0, nodes and leaves will be split as we walk down the
  886. * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
  887. * possible)
  888. */
  889. int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
  890. *root, struct btrfs_key *key, struct btrfs_path *p, int
  891. ins_len, int cow)
  892. {
  893. struct extent_buffer *b;
  894. u64 bytenr;
  895. int slot;
  896. int ret;
  897. int level;
  898. int should_reada = p->reada;
  899. u8 lowest_level = 0;
  900. lowest_level = p->lowest_level;
  901. WARN_ON(lowest_level && ins_len);
  902. WARN_ON(p->nodes[0] != NULL);
  903. WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
  904. again:
  905. b = root->node;
  906. extent_buffer_get(b);
  907. while (b) {
  908. level = btrfs_header_level(b);
  909. if (cow) {
  910. int wret;
  911. wret = btrfs_cow_block(trans, root, b,
  912. p->nodes[level + 1],
  913. p->slots[level + 1],
  914. &b);
  915. if (wret) {
  916. free_extent_buffer(b);
  917. return wret;
  918. }
  919. }
  920. BUG_ON(!cow && ins_len);
  921. if (level != btrfs_header_level(b))
  922. WARN_ON(1);
  923. level = btrfs_header_level(b);
  924. p->nodes[level] = b;
  925. ret = check_block(root, p, level);
  926. if (ret)
  927. return -1;
  928. ret = bin_search(b, key, level, &slot);
  929. if (level != 0) {
  930. if (ret && slot > 0)
  931. slot -= 1;
  932. p->slots[level] = slot;
  933. if (ins_len > 0 && btrfs_header_nritems(b) >=
  934. BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
  935. int sret = split_node(trans, root, p, level);
  936. BUG_ON(sret > 0);
  937. if (sret)
  938. return sret;
  939. b = p->nodes[level];
  940. slot = p->slots[level];
  941. } else if (ins_len < 0) {
  942. int sret = balance_level(trans, root, p,
  943. level);
  944. if (sret)
  945. return sret;
  946. b = p->nodes[level];
  947. if (!b) {
  948. btrfs_release_path(NULL, p);
  949. goto again;
  950. }
  951. slot = p->slots[level];
  952. BUG_ON(btrfs_header_nritems(b) == 1);
  953. }
  954. /* this is only true while dropping a snapshot */
  955. if (level == lowest_level)
  956. break;
  957. bytenr = btrfs_node_blockptr(b, slot);
  958. if (should_reada)
  959. reada_for_search(root, p, level, slot);
  960. b = read_tree_block(root, bytenr,
  961. btrfs_level_size(root, level - 1));
  962. } else {
  963. p->slots[level] = slot;
  964. if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
  965. sizeof(struct btrfs_item) + ins_len) {
  966. int sret = split_leaf(trans, root, key,
  967. p, ins_len);
  968. BUG_ON(sret > 0);
  969. if (sret)
  970. return sret;
  971. }
  972. return ret;
  973. }
  974. }
  975. return 1;
  976. }
  977. /*
  978. * adjust the pointers going up the tree, starting at level
  979. * making sure the right key of each node is points to 'key'.
  980. * This is used after shifting pointers to the left, so it stops
  981. * fixing up pointers when a given leaf/node is not in slot 0 of the
  982. * higher levels
  983. *
  984. * If this fails to write a tree block, it returns -1, but continues
  985. * fixing up the blocks in ram so the tree is consistent.
  986. */
  987. static int fixup_low_keys(struct btrfs_trans_handle *trans,
  988. struct btrfs_root *root, struct btrfs_path *path,
  989. struct btrfs_disk_key *key, int level)
  990. {
  991. int i;
  992. int ret = 0;
  993. struct extent_buffer *t;
  994. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  995. int tslot = path->slots[i];
  996. if (!path->nodes[i])
  997. break;
  998. t = path->nodes[i];
  999. btrfs_set_node_key(t, key, tslot);
  1000. btrfs_mark_buffer_dirty(path->nodes[i]);
  1001. if (tslot != 0)
  1002. break;
  1003. }
  1004. return ret;
  1005. }
  1006. /*
  1007. * try to push data from one node into the next node left in the
  1008. * tree.
  1009. *
  1010. * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
  1011. * error, and > 0 if there was no room in the left hand block.
  1012. */
  1013. static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
  1014. *root, struct extent_buffer *dst,
  1015. struct extent_buffer *src)
  1016. {
  1017. int push_items = 0;
  1018. int src_nritems;
  1019. int dst_nritems;
  1020. int ret = 0;
  1021. src_nritems = btrfs_header_nritems(src);
  1022. dst_nritems = btrfs_header_nritems(dst);
  1023. push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
  1024. if (push_items <= 0) {
  1025. return 1;
  1026. }
  1027. if (src_nritems < push_items)
  1028. push_items = src_nritems;
  1029. copy_extent_buffer(dst, src,
  1030. btrfs_node_key_ptr_offset(dst_nritems),
  1031. btrfs_node_key_ptr_offset(0),
  1032. push_items * sizeof(struct btrfs_key_ptr));
  1033. if (push_items < src_nritems) {
  1034. memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
  1035. btrfs_node_key_ptr_offset(push_items),
  1036. (src_nritems - push_items) *
  1037. sizeof(struct btrfs_key_ptr));
  1038. }
  1039. btrfs_set_header_nritems(src, src_nritems - push_items);
  1040. btrfs_set_header_nritems(dst, dst_nritems + push_items);
  1041. btrfs_mark_buffer_dirty(src);
  1042. btrfs_mark_buffer_dirty(dst);
  1043. return ret;
  1044. }
  1045. /*
  1046. * try to push data from one node into the next node right in the
  1047. * tree.
  1048. *
  1049. * returns 0 if some ptrs were pushed, < 0 if there was some horrible
  1050. * error, and > 0 if there was no room in the right hand block.
  1051. *
  1052. * this will only push up to 1/2 the contents of the left node over
  1053. */
  1054. static int balance_node_right(struct btrfs_trans_handle *trans,
  1055. struct btrfs_root *root,
  1056. struct extent_buffer *dst,
  1057. struct extent_buffer *src)
  1058. {
  1059. int push_items = 0;
  1060. int max_push;
  1061. int src_nritems;
  1062. int dst_nritems;
  1063. int ret = 0;
  1064. src_nritems = btrfs_header_nritems(src);
  1065. dst_nritems = btrfs_header_nritems(dst);
  1066. push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
  1067. if (push_items <= 0)
  1068. return 1;
  1069. max_push = src_nritems / 2 + 1;
  1070. /* don't try to empty the node */
  1071. if (max_push >= src_nritems)
  1072. return 1;
  1073. if (max_push < push_items)
  1074. push_items = max_push;
  1075. memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
  1076. btrfs_node_key_ptr_offset(0),
  1077. (dst_nritems) *
  1078. sizeof(struct btrfs_key_ptr));
  1079. copy_extent_buffer(dst, src,
  1080. btrfs_node_key_ptr_offset(0),
  1081. btrfs_node_key_ptr_offset(src_nritems - push_items),
  1082. push_items * sizeof(struct btrfs_key_ptr));
  1083. btrfs_set_header_nritems(src, src_nritems - push_items);
  1084. btrfs_set_header_nritems(dst, dst_nritems + push_items);
  1085. btrfs_mark_buffer_dirty(src);
  1086. btrfs_mark_buffer_dirty(dst);
  1087. return ret;
  1088. }
  1089. /*
  1090. * helper function to insert a new root level in the tree.
  1091. * A new node is allocated, and a single item is inserted to
  1092. * point to the existing root
  1093. *
  1094. * returns zero on success or < 0 on failure.
  1095. */
  1096. static int insert_new_root(struct btrfs_trans_handle *trans,
  1097. struct btrfs_root *root,
  1098. struct btrfs_path *path, int level)
  1099. {
  1100. struct extent_buffer *lower;
  1101. struct extent_buffer *c;
  1102. struct btrfs_disk_key lower_key;
  1103. BUG_ON(path->nodes[level]);
  1104. BUG_ON(path->nodes[level-1] != root->node);
  1105. c = btrfs_alloc_free_block(trans, root, root->nodesize,
  1106. root->node->start, 0);
  1107. if (IS_ERR(c))
  1108. return PTR_ERR(c);
  1109. memset_extent_buffer(c, 0, 0, root->nodesize);
  1110. btrfs_set_header_nritems(c, 1);
  1111. btrfs_set_header_level(c, level);
  1112. btrfs_set_header_bytenr(c, c->start);
  1113. btrfs_set_header_generation(c, trans->transid);
  1114. btrfs_set_header_owner(c, root->root_key.objectid);
  1115. lower = path->nodes[level-1];
  1116. write_extent_buffer(c, root->fs_info->fsid,
  1117. (unsigned long)btrfs_header_fsid(c),
  1118. BTRFS_FSID_SIZE);
  1119. if (level == 1)
  1120. btrfs_item_key(lower, &lower_key, 0);
  1121. else
  1122. btrfs_node_key(lower, &lower_key, 0);
  1123. btrfs_set_node_key(c, &lower_key, 0);
  1124. btrfs_set_node_blockptr(c, 0, lower->start);
  1125. btrfs_mark_buffer_dirty(c);
  1126. /* the super has an extra ref to root->node */
  1127. free_extent_buffer(root->node);
  1128. root->node = c;
  1129. extent_buffer_get(c);
  1130. path->nodes[level] = c;
  1131. path->slots[level] = 0;
  1132. return 0;
  1133. }
  1134. /*
  1135. * worker function to insert a single pointer in a node.
  1136. * the node should have enough room for the pointer already
  1137. *
  1138. * slot and level indicate where you want the key to go, and
  1139. * blocknr is the block the key points to.
  1140. *
  1141. * returns zero on success and < 0 on any error
  1142. */
  1143. static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
  1144. *root, struct btrfs_path *path, struct btrfs_disk_key
  1145. *key, u64 bytenr, int slot, int level)
  1146. {
  1147. struct extent_buffer *lower;
  1148. int nritems;
  1149. BUG_ON(!path->nodes[level]);
  1150. lower = path->nodes[level];
  1151. nritems = btrfs_header_nritems(lower);
  1152. if (slot > nritems)
  1153. BUG();
  1154. if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
  1155. BUG();
  1156. if (slot != nritems) {
  1157. memmove_extent_buffer(lower,
  1158. btrfs_node_key_ptr_offset(slot + 1),
  1159. btrfs_node_key_ptr_offset(slot),
  1160. (nritems - slot) * sizeof(struct btrfs_key_ptr));
  1161. }
  1162. btrfs_set_node_key(lower, key, slot);
  1163. btrfs_set_node_blockptr(lower, slot, bytenr);
  1164. btrfs_set_header_nritems(lower, nritems + 1);
  1165. btrfs_mark_buffer_dirty(lower);
  1166. return 0;
  1167. }
  1168. /*
  1169. * split the node at the specified level in path in two.
  1170. * The path is corrected to point to the appropriate node after the split
  1171. *
  1172. * Before splitting this tries to make some room in the node by pushing
  1173. * left and right, if either one works, it returns right away.
  1174. *
  1175. * returns 0 on success and < 0 on failure
  1176. */
  1177. static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
  1178. *root, struct btrfs_path *path, int level)
  1179. {
  1180. struct extent_buffer *c;
  1181. struct extent_buffer *split;
  1182. struct btrfs_disk_key disk_key;
  1183. int mid;
  1184. int ret;
  1185. int wret;
  1186. u32 c_nritems;
  1187. c = path->nodes[level];
  1188. if (c == root->node) {
  1189. /* trying to split the root, lets make a new one */
  1190. ret = insert_new_root(trans, root, path, level + 1);
  1191. if (ret)
  1192. return ret;
  1193. } else {
  1194. ret = push_nodes_for_insert(trans, root, path, level);
  1195. c = path->nodes[level];
  1196. if (!ret && btrfs_header_nritems(c) <
  1197. BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
  1198. return 0;
  1199. if (ret < 0)
  1200. return ret;
  1201. }
  1202. c_nritems = btrfs_header_nritems(c);
  1203. split = btrfs_alloc_free_block(trans, root, root->nodesize,
  1204. c->start, 0);
  1205. if (IS_ERR(split))
  1206. return PTR_ERR(split);
  1207. btrfs_set_header_flags(split, btrfs_header_flags(c));
  1208. btrfs_set_header_level(split, btrfs_header_level(c));
  1209. btrfs_set_header_bytenr(split, split->start);
  1210. btrfs_set_header_generation(split, trans->transid);
  1211. btrfs_set_header_owner(split, root->root_key.objectid);
  1212. write_extent_buffer(split, root->fs_info->fsid,
  1213. (unsigned long)btrfs_header_fsid(split),
  1214. BTRFS_FSID_SIZE);
  1215. mid = (c_nritems + 1) / 2;
  1216. copy_extent_buffer(split, c,
  1217. btrfs_node_key_ptr_offset(0),
  1218. btrfs_node_key_ptr_offset(mid),
  1219. (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
  1220. btrfs_set_header_nritems(split, c_nritems - mid);
  1221. btrfs_set_header_nritems(c, mid);
  1222. ret = 0;
  1223. btrfs_mark_buffer_dirty(c);
  1224. btrfs_mark_buffer_dirty(split);
  1225. btrfs_node_key(split, &disk_key, 0);
  1226. wret = insert_ptr(trans, root, path, &disk_key, split->start,
  1227. path->slots[level + 1] + 1,
  1228. level + 1);
  1229. if (wret)
  1230. ret = wret;
  1231. if (path->slots[level] >= mid) {
  1232. path->slots[level] -= mid;
  1233. free_extent_buffer(c);
  1234. path->nodes[level] = split;
  1235. path->slots[level + 1] += 1;
  1236. } else {
  1237. free_extent_buffer(split);
  1238. }
  1239. return ret;
  1240. }
  1241. /*
  1242. * how many bytes are required to store the items in a leaf. start
  1243. * and nr indicate which items in the leaf to check. This totals up the
  1244. * space used both by the item structs and the item data
  1245. */
  1246. static int leaf_space_used(struct extent_buffer *l, int start, int nr)
  1247. {
  1248. int data_len;
  1249. int nritems = btrfs_header_nritems(l);
  1250. int end = min(nritems, start + nr) - 1;
  1251. if (!nr)
  1252. return 0;
  1253. data_len = btrfs_item_end_nr(l, start);
  1254. data_len = data_len - btrfs_item_offset_nr(l, end);
  1255. data_len += sizeof(struct btrfs_item) * nr;
  1256. WARN_ON(data_len < 0);
  1257. return data_len;
  1258. }
  1259. /*
  1260. * The space between the end of the leaf items and
  1261. * the start of the leaf data. IOW, how much room
  1262. * the leaf has left for both items and data
  1263. */
  1264. int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
  1265. {
  1266. int nritems = btrfs_header_nritems(leaf);
  1267. int ret;
  1268. ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
  1269. if (ret < 0) {
  1270. printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
  1271. ret, BTRFS_LEAF_DATA_SIZE(root),
  1272. leaf_space_used(leaf, 0, nritems), nritems);
  1273. }
  1274. return ret;
  1275. }
  1276. /*
  1277. * push some data in the path leaf to the right, trying to free up at
  1278. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  1279. *
  1280. * returns 1 if the push failed because the other node didn't have enough
  1281. * room, 0 if everything worked out and < 0 if there were major errors.
  1282. */
  1283. static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
  1284. *root, struct btrfs_path *path, int data_size)
  1285. {
  1286. struct extent_buffer *left = path->nodes[0];
  1287. struct extent_buffer *right;
  1288. struct extent_buffer *upper;
  1289. struct btrfs_disk_key disk_key;
  1290. int slot;
  1291. int i;
  1292. int free_space;
  1293. int push_space = 0;
  1294. int push_items = 0;
  1295. struct btrfs_item *item;
  1296. u32 left_nritems;
  1297. u32 right_nritems;
  1298. u32 data_end;
  1299. u32 this_item_size;
  1300. int ret;
  1301. slot = path->slots[1];
  1302. if (!path->nodes[1]) {
  1303. return 1;
  1304. }
  1305. upper = path->nodes[1];
  1306. if (slot >= btrfs_header_nritems(upper) - 1)
  1307. return 1;
  1308. right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1),
  1309. root->leafsize);
  1310. free_space = btrfs_leaf_free_space(root, right);
  1311. if (free_space < data_size + sizeof(struct btrfs_item)) {
  1312. free_extent_buffer(right);
  1313. return 1;
  1314. }
  1315. /* cow and double check */
  1316. ret = btrfs_cow_block(trans, root, right, upper,
  1317. slot + 1, &right);
  1318. if (ret) {
  1319. free_extent_buffer(right);
  1320. return 1;
  1321. }
  1322. free_space = btrfs_leaf_free_space(root, right);
  1323. if (free_space < data_size + sizeof(struct btrfs_item)) {
  1324. free_extent_buffer(right);
  1325. return 1;
  1326. }
  1327. left_nritems = btrfs_header_nritems(left);
  1328. if (left_nritems == 0) {
  1329. free_extent_buffer(right);
  1330. return 1;
  1331. }
  1332. for (i = left_nritems - 1; i >= 1; i--) {
  1333. item = btrfs_item_nr(left, i);
  1334. if (path->slots[0] == i)
  1335. push_space += data_size + sizeof(*item);
  1336. if (!left->map_token) {
  1337. map_extent_buffer(left, (unsigned long)item,
  1338. sizeof(struct btrfs_item),
  1339. &left->map_token, &left->kaddr,
  1340. &left->map_start, &left->map_len,
  1341. KM_USER1);
  1342. }
  1343. this_item_size = btrfs_item_size(left, item);
  1344. if (this_item_size + sizeof(*item) + push_space > free_space)
  1345. break;
  1346. push_items++;
  1347. push_space += this_item_size + sizeof(*item);
  1348. }
  1349. if (left->map_token) {
  1350. unmap_extent_buffer(left, left->map_token, KM_USER1);
  1351. left->map_token = NULL;
  1352. }
  1353. if (push_items == 0) {
  1354. free_extent_buffer(right);
  1355. return 1;
  1356. }
  1357. if (push_items == left_nritems)
  1358. WARN_ON(1);
  1359. /* push left to right */
  1360. right_nritems = btrfs_header_nritems(right);
  1361. push_space = btrfs_item_end_nr(left, left_nritems - push_items);
  1362. push_space -= leaf_data_end(root, left);
  1363. /* make room in the right data area */
  1364. data_end = leaf_data_end(root, right);
  1365. memmove_extent_buffer(right,
  1366. btrfs_leaf_data(right) + data_end - push_space,
  1367. btrfs_leaf_data(right) + data_end,
  1368. BTRFS_LEAF_DATA_SIZE(root) - data_end);
  1369. /* copy from the left data area */
  1370. copy_extent_buffer(right, left, btrfs_leaf_data(right) +
  1371. BTRFS_LEAF_DATA_SIZE(root) - push_space,
  1372. btrfs_leaf_data(left) + leaf_data_end(root, left),
  1373. push_space);
  1374. memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
  1375. btrfs_item_nr_offset(0),
  1376. right_nritems * sizeof(struct btrfs_item));
  1377. /* copy the items from left to right */
  1378. copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
  1379. btrfs_item_nr_offset(left_nritems - push_items),
  1380. push_items * sizeof(struct btrfs_item));
  1381. /* update the item pointers */
  1382. right_nritems += push_items;
  1383. btrfs_set_header_nritems(right, right_nritems);
  1384. push_space = BTRFS_LEAF_DATA_SIZE(root);
  1385. for (i = 0; i < right_nritems; i++) {
  1386. item = btrfs_item_nr(right, i);
  1387. if (!right->map_token) {
  1388. map_extent_buffer(right, (unsigned long)item,
  1389. sizeof(struct btrfs_item),
  1390. &right->map_token, &right->kaddr,
  1391. &right->map_start, &right->map_len,
  1392. KM_USER1);
  1393. }
  1394. push_space -= btrfs_item_size(right, item);
  1395. btrfs_set_item_offset(right, item, push_space);
  1396. }
  1397. if (right->map_token) {
  1398. unmap_extent_buffer(right, right->map_token, KM_USER1);
  1399. right->map_token = NULL;
  1400. }
  1401. left_nritems -= push_items;
  1402. btrfs_set_header_nritems(left, left_nritems);
  1403. btrfs_mark_buffer_dirty(left);
  1404. btrfs_mark_buffer_dirty(right);
  1405. btrfs_item_key(right, &disk_key, 0);
  1406. btrfs_set_node_key(upper, &disk_key, slot + 1);
  1407. btrfs_mark_buffer_dirty(upper);
  1408. /* then fixup the leaf pointer in the path */
  1409. if (path->slots[0] >= left_nritems) {
  1410. path->slots[0] -= left_nritems;
  1411. free_extent_buffer(path->nodes[0]);
  1412. path->nodes[0] = right;
  1413. path->slots[1] += 1;
  1414. } else {
  1415. free_extent_buffer(right);
  1416. }
  1417. return 0;
  1418. }
  1419. /*
  1420. * push some data in the path leaf to the left, trying to free up at
  1421. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  1422. */
  1423. static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
  1424. *root, struct btrfs_path *path, int data_size)
  1425. {
  1426. struct btrfs_disk_key disk_key;
  1427. struct extent_buffer *right = path->nodes[0];
  1428. struct extent_buffer *left;
  1429. int slot;
  1430. int i;
  1431. int free_space;
  1432. int push_space = 0;
  1433. int push_items = 0;
  1434. struct btrfs_item *item;
  1435. u32 old_left_nritems;
  1436. u32 right_nritems;
  1437. int ret = 0;
  1438. int wret;
  1439. u32 this_item_size;
  1440. u32 old_left_item_size;
  1441. slot = path->slots[1];
  1442. if (slot == 0)
  1443. return 1;
  1444. if (!path->nodes[1])
  1445. return 1;
  1446. left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1],
  1447. slot - 1), root->leafsize);
  1448. free_space = btrfs_leaf_free_space(root, left);
  1449. if (free_space < data_size + sizeof(struct btrfs_item)) {
  1450. free_extent_buffer(left);
  1451. return 1;
  1452. }
  1453. /* cow and double check */
  1454. ret = btrfs_cow_block(trans, root, left,
  1455. path->nodes[1], slot - 1, &left);
  1456. if (ret) {
  1457. /* we hit -ENOSPC, but it isn't fatal here */
  1458. free_extent_buffer(left);
  1459. return 1;
  1460. }
  1461. free_space = btrfs_leaf_free_space(root, left);
  1462. if (free_space < data_size + sizeof(struct btrfs_item)) {
  1463. free_extent_buffer(left);
  1464. return 1;
  1465. }
  1466. right_nritems = btrfs_header_nritems(right);
  1467. if (right_nritems == 0) {
  1468. free_extent_buffer(left);
  1469. return 1;
  1470. }
  1471. for (i = 0; i < right_nritems - 1; i++) {
  1472. item = btrfs_item_nr(right, i);
  1473. if (!right->map_token) {
  1474. map_extent_buffer(right, (unsigned long)item,
  1475. sizeof(struct btrfs_item),
  1476. &right->map_token, &right->kaddr,
  1477. &right->map_start, &right->map_len,
  1478. KM_USER1);
  1479. }
  1480. if (path->slots[0] == i)
  1481. push_space += data_size + sizeof(*item);
  1482. this_item_size = btrfs_item_size(right, item);
  1483. if (this_item_size + sizeof(*item) + push_space > free_space)
  1484. break;
  1485. push_items++;
  1486. push_space += this_item_size + sizeof(*item);
  1487. }
  1488. if (right->map_token) {
  1489. unmap_extent_buffer(right, right->map_token, KM_USER1);
  1490. right->map_token = NULL;
  1491. }
  1492. if (push_items == 0) {
  1493. free_extent_buffer(left);
  1494. return 1;
  1495. }
  1496. if (push_items == btrfs_header_nritems(right))
  1497. WARN_ON(1);
  1498. /* push data from right to left */
  1499. copy_extent_buffer(left, right,
  1500. btrfs_item_nr_offset(btrfs_header_nritems(left)),
  1501. btrfs_item_nr_offset(0),
  1502. push_items * sizeof(struct btrfs_item));
  1503. push_space = BTRFS_LEAF_DATA_SIZE(root) -
  1504. btrfs_item_offset_nr(right, push_items -1);
  1505. copy_extent_buffer(left, right, btrfs_leaf_data(left) +
  1506. leaf_data_end(root, left) - push_space,
  1507. btrfs_leaf_data(right) +
  1508. btrfs_item_offset_nr(right, push_items - 1),
  1509. push_space);
  1510. old_left_nritems = btrfs_header_nritems(left);
  1511. BUG_ON(old_left_nritems < 0);
  1512. old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
  1513. for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
  1514. u32 ioff;
  1515. item = btrfs_item_nr(left, i);
  1516. if (!left->map_token) {
  1517. map_extent_buffer(left, (unsigned long)item,
  1518. sizeof(struct btrfs_item),
  1519. &left->map_token, &left->kaddr,
  1520. &left->map_start, &left->map_len,
  1521. KM_USER1);
  1522. }
  1523. ioff = btrfs_item_offset(left, item);
  1524. btrfs_set_item_offset(left, item,
  1525. ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
  1526. }
  1527. btrfs_set_header_nritems(left, old_left_nritems + push_items);
  1528. if (left->map_token) {
  1529. unmap_extent_buffer(left, left->map_token, KM_USER1);
  1530. left->map_token = NULL;
  1531. }
  1532. /* fixup right node */
  1533. push_space = btrfs_item_offset_nr(right, push_items - 1) -
  1534. leaf_data_end(root, right);
  1535. memmove_extent_buffer(right, btrfs_leaf_data(right) +
  1536. BTRFS_LEAF_DATA_SIZE(root) - push_space,
  1537. btrfs_leaf_data(right) +
  1538. leaf_data_end(root, right), push_space);
  1539. memmove_extent_buffer(right, btrfs_item_nr_offset(0),
  1540. btrfs_item_nr_offset(push_items),
  1541. (btrfs_header_nritems(right) - push_items) *
  1542. sizeof(struct btrfs_item));
  1543. right_nritems = btrfs_header_nritems(right) - push_items;
  1544. btrfs_set_header_nritems(right, right_nritems);
  1545. push_space = BTRFS_LEAF_DATA_SIZE(root);
  1546. for (i = 0; i < right_nritems; i++) {
  1547. item = btrfs_item_nr(right, i);
  1548. if (!right->map_token) {
  1549. map_extent_buffer(right, (unsigned long)item,
  1550. sizeof(struct btrfs_item),
  1551. &right->map_token, &right->kaddr,
  1552. &right->map_start, &right->map_len,
  1553. KM_USER1);
  1554. }
  1555. push_space = push_space - btrfs_item_size(right, item);
  1556. btrfs_set_item_offset(right, item, push_space);
  1557. }
  1558. if (right->map_token) {
  1559. unmap_extent_buffer(right, right->map_token, KM_USER1);
  1560. right->map_token = NULL;
  1561. }
  1562. btrfs_mark_buffer_dirty(left);
  1563. btrfs_mark_buffer_dirty(right);
  1564. btrfs_item_key(right, &disk_key, 0);
  1565. wret = fixup_low_keys(trans, root, path, &disk_key, 1);
  1566. if (wret)
  1567. ret = wret;
  1568. /* then fixup the leaf pointer in the path */
  1569. if (path->slots[0] < push_items) {
  1570. path->slots[0] += old_left_nritems;
  1571. free_extent_buffer(path->nodes[0]);
  1572. path->nodes[0] = left;
  1573. path->slots[1] -= 1;
  1574. } else {
  1575. free_extent_buffer(left);
  1576. path->slots[0] -= push_items;
  1577. }
  1578. BUG_ON(path->slots[0] < 0);
  1579. return ret;
  1580. }
  1581. /*
  1582. * split the path's leaf in two, making sure there is at least data_size
  1583. * available for the resulting leaf level of the path.
  1584. *
  1585. * returns 0 if all went well and < 0 on failure.
  1586. */
  1587. static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
  1588. *root, struct btrfs_key *ins_key,
  1589. struct btrfs_path *path, int data_size)
  1590. {
  1591. struct extent_buffer *l;
  1592. u32 nritems;
  1593. int mid;
  1594. int slot;
  1595. struct extent_buffer *right;
  1596. int space_needed = data_size + sizeof(struct btrfs_item);
  1597. int data_copy_size;
  1598. int rt_data_off;
  1599. int i;
  1600. int ret = 0;
  1601. int wret;
  1602. int double_split = 0;
  1603. struct btrfs_disk_key disk_key;
  1604. /* first try to make some room by pushing left and right */
  1605. wret = push_leaf_left(trans, root, path, data_size);
  1606. if (wret < 0) {
  1607. return wret;
  1608. }
  1609. if (wret) {
  1610. wret = push_leaf_right(trans, root, path, data_size);
  1611. if (wret < 0)
  1612. return wret;
  1613. }
  1614. l = path->nodes[0];
  1615. /* did the pushes work? */
  1616. if (btrfs_leaf_free_space(root, l) >=
  1617. sizeof(struct btrfs_item) + data_size) {
  1618. return 0;
  1619. }
  1620. if (!path->nodes[1]) {
  1621. ret = insert_new_root(trans, root, path, 1);
  1622. if (ret)
  1623. return ret;
  1624. }
  1625. slot = path->slots[0];
  1626. nritems = btrfs_header_nritems(l);
  1627. mid = (nritems + 1)/ 2;
  1628. right = btrfs_alloc_free_block(trans, root, root->leafsize,
  1629. l->start, 0);
  1630. if (IS_ERR(right))
  1631. return PTR_ERR(right);
  1632. memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
  1633. btrfs_set_header_bytenr(right, right->start);
  1634. btrfs_set_header_generation(right, trans->transid);
  1635. btrfs_set_header_owner(right, root->root_key.objectid);
  1636. btrfs_set_header_level(right, 0);
  1637. write_extent_buffer(right, root->fs_info->fsid,
  1638. (unsigned long)btrfs_header_fsid(right),
  1639. BTRFS_FSID_SIZE);
  1640. if (mid <= slot) {
  1641. if (nritems == 1 ||
  1642. leaf_space_used(l, mid, nritems - mid) + space_needed >
  1643. BTRFS_LEAF_DATA_SIZE(root)) {
  1644. if (slot >= nritems) {
  1645. btrfs_cpu_key_to_disk(&disk_key, ins_key);
  1646. btrfs_set_header_nritems(right, 0);
  1647. wret = insert_ptr(trans, root, path,
  1648. &disk_key, right->start,
  1649. path->slots[1] + 1, 1);
  1650. if (wret)
  1651. ret = wret;
  1652. free_extent_buffer(path->nodes[0]);
  1653. path->nodes[0] = right;
  1654. path->slots[0] = 0;
  1655. path->slots[1] += 1;
  1656. return ret;
  1657. }
  1658. mid = slot;
  1659. if (mid != nritems &&
  1660. leaf_space_used(l, mid, nritems - mid) +
  1661. space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
  1662. double_split = 1;
  1663. }
  1664. }
  1665. } else {
  1666. if (leaf_space_used(l, 0, mid + 1) + space_needed >
  1667. BTRFS_LEAF_DATA_SIZE(root)) {
  1668. if (slot == 0) {
  1669. btrfs_cpu_key_to_disk(&disk_key, ins_key);
  1670. btrfs_set_header_nritems(right, 0);
  1671. wret = insert_ptr(trans, root, path,
  1672. &disk_key,
  1673. right->start,
  1674. path->slots[1], 1);
  1675. if (wret)
  1676. ret = wret;
  1677. free_extent_buffer(path->nodes[0]);
  1678. path->nodes[0] = right;
  1679. path->slots[0] = 0;
  1680. if (path->slots[1] == 0) {
  1681. wret = fixup_low_keys(trans, root,
  1682. path, &disk_key, 1);
  1683. if (wret)
  1684. ret = wret;
  1685. }
  1686. return ret;
  1687. }
  1688. mid = slot;
  1689. double_split = 1;
  1690. }
  1691. }
  1692. nritems = nritems - mid;
  1693. btrfs_set_header_nritems(right, nritems);
  1694. data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
  1695. copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
  1696. btrfs_item_nr_offset(mid),
  1697. nritems * sizeof(struct btrfs_item));
  1698. copy_extent_buffer(right, l,
  1699. btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
  1700. data_copy_size, btrfs_leaf_data(l) +
  1701. leaf_data_end(root, l), data_copy_size);
  1702. rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
  1703. btrfs_item_end_nr(l, mid);
  1704. for (i = 0; i < nritems; i++) {
  1705. struct btrfs_item *item = btrfs_item_nr(right, i);
  1706. u32 ioff;
  1707. if (!right->map_token) {
  1708. map_extent_buffer(right, (unsigned long)item,
  1709. sizeof(struct btrfs_item),
  1710. &right->map_token, &right->kaddr,
  1711. &right->map_start, &right->map_len,
  1712. KM_USER1);
  1713. }
  1714. ioff = btrfs_item_offset(right, item);
  1715. btrfs_set_item_offset(right, item, ioff + rt_data_off);
  1716. }
  1717. if (right->map_token) {
  1718. unmap_extent_buffer(right, right->map_token, KM_USER1);
  1719. right->map_token = NULL;
  1720. }
  1721. btrfs_set_header_nritems(l, mid);
  1722. ret = 0;
  1723. btrfs_item_key(right, &disk_key, 0);
  1724. wret = insert_ptr(trans, root, path, &disk_key, right->start,
  1725. path->slots[1] + 1, 1);
  1726. if (wret)
  1727. ret = wret;
  1728. btrfs_mark_buffer_dirty(right);
  1729. btrfs_mark_buffer_dirty(l);
  1730. BUG_ON(path->slots[0] != slot);
  1731. if (mid <= slot) {
  1732. free_extent_buffer(path->nodes[0]);
  1733. path->nodes[0] = right;
  1734. path->slots[0] -= mid;
  1735. path->slots[1] += 1;
  1736. } else
  1737. free_extent_buffer(right);
  1738. BUG_ON(path->slots[0] < 0);
  1739. if (!double_split) {
  1740. return ret;
  1741. }
  1742. right = btrfs_alloc_free_block(trans, root, root->leafsize,
  1743. l->start, 0);
  1744. if (IS_ERR(right))
  1745. return PTR_ERR(right);
  1746. memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
  1747. btrfs_set_header_bytenr(right, right->start);
  1748. btrfs_set_header_generation(right, trans->transid);
  1749. btrfs_set_header_owner(right, root->root_key.objectid);
  1750. btrfs_set_header_level(right, 0);
  1751. write_extent_buffer(right, root->fs_info->fsid,
  1752. (unsigned long)btrfs_header_fsid(right),
  1753. BTRFS_FSID_SIZE);
  1754. btrfs_cpu_key_to_disk(&disk_key, ins_key);
  1755. btrfs_set_header_nritems(right, 0);
  1756. wret = insert_ptr(trans, root, path,
  1757. &disk_key, right->start,
  1758. path->slots[1], 1);
  1759. if (wret)
  1760. ret = wret;
  1761. if (path->slots[1] == 0) {
  1762. wret = fixup_low_keys(trans, root, path, &disk_key, 1);
  1763. if (wret)
  1764. ret = wret;
  1765. }
  1766. free_extent_buffer(path->nodes[0]);
  1767. path->nodes[0] = right;
  1768. path->slots[0] = 0;
  1769. return ret;
  1770. }
  1771. int btrfs_truncate_item(struct btrfs_trans_handle *trans,
  1772. struct btrfs_root *root,
  1773. struct btrfs_path *path,
  1774. u32 new_size)
  1775. {
  1776. int ret = 0;
  1777. int slot;
  1778. int slot_orig;
  1779. struct extent_buffer *leaf;
  1780. struct btrfs_item *item;
  1781. u32 nritems;
  1782. unsigned int data_end;
  1783. unsigned int old_data_start;
  1784. unsigned int old_size;
  1785. unsigned int size_diff;
  1786. int i;
  1787. slot_orig = path->slots[0];
  1788. leaf = path->nodes[0];
  1789. nritems = btrfs_header_nritems(leaf);
  1790. data_end = leaf_data_end(root, leaf);
  1791. slot = path->slots[0];
  1792. old_data_start = btrfs_item_offset_nr(leaf, slot);
  1793. old_size = btrfs_item_size_nr(leaf, slot);
  1794. BUG_ON(old_size <= new_size);
  1795. size_diff = old_size - new_size;
  1796. BUG_ON(slot < 0);
  1797. BUG_ON(slot >= nritems);
  1798. /*
  1799. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  1800. */
  1801. /* first correct the data pointers */
  1802. for (i = slot; i < nritems; i++) {
  1803. u32 ioff;
  1804. item = btrfs_item_nr(leaf, i);
  1805. if (!leaf->map_token) {
  1806. map_extent_buffer(leaf, (unsigned long)item,
  1807. sizeof(struct btrfs_item),
  1808. &leaf->map_token, &leaf->kaddr,
  1809. &leaf->map_start, &leaf->map_len,
  1810. KM_USER1);
  1811. }
  1812. ioff = btrfs_item_offset(leaf, item);
  1813. btrfs_set_item_offset(leaf, item, ioff + size_diff);
  1814. }
  1815. if (leaf->map_token) {
  1816. unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
  1817. leaf->map_token = NULL;
  1818. }
  1819. /* shift the data */
  1820. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  1821. data_end + size_diff, btrfs_leaf_data(leaf) +
  1822. data_end, old_data_start + new_size - data_end);
  1823. item = btrfs_item_nr(leaf, slot);
  1824. btrfs_set_item_size(leaf, item, new_size);
  1825. btrfs_mark_buffer_dirty(leaf);
  1826. ret = 0;
  1827. if (btrfs_leaf_free_space(root, leaf) < 0) {
  1828. btrfs_print_leaf(root, leaf);
  1829. BUG();
  1830. }
  1831. return ret;
  1832. }
  1833. int btrfs_extend_item(struct btrfs_trans_handle *trans,
  1834. struct btrfs_root *root, struct btrfs_path *path,
  1835. u32 data_size)
  1836. {
  1837. int ret = 0;
  1838. int slot;
  1839. int slot_orig;
  1840. struct extent_buffer *leaf;
  1841. struct btrfs_item *item;
  1842. u32 nritems;
  1843. unsigned int data_end;
  1844. unsigned int old_data;
  1845. unsigned int old_size;
  1846. int i;
  1847. slot_orig = path->slots[0];
  1848. leaf = path->nodes[0];
  1849. nritems = btrfs_header_nritems(leaf);
  1850. data_end = leaf_data_end(root, leaf);
  1851. if (btrfs_leaf_free_space(root, leaf) < data_size) {
  1852. btrfs_print_leaf(root, leaf);
  1853. BUG();
  1854. }
  1855. slot = path->slots[0];
  1856. old_data = btrfs_item_end_nr(leaf, slot);
  1857. BUG_ON(slot < 0);
  1858. if (slot >= nritems) {
  1859. btrfs_print_leaf(root, leaf);
  1860. printk("slot %d too large, nritems %d\n", slot, nritems);
  1861. BUG_ON(1);
  1862. }
  1863. /*
  1864. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  1865. */
  1866. /* first correct the data pointers */
  1867. for (i = slot; i < nritems; i++) {
  1868. u32 ioff;
  1869. item = btrfs_item_nr(leaf, i);
  1870. if (!leaf->map_token) {
  1871. map_extent_buffer(leaf, (unsigned long)item,
  1872. sizeof(struct btrfs_item),
  1873. &leaf->map_token, &leaf->kaddr,
  1874. &leaf->map_start, &leaf->map_len,
  1875. KM_USER1);
  1876. }
  1877. ioff = btrfs_item_offset(leaf, item);
  1878. btrfs_set_item_offset(leaf, item, ioff - data_size);
  1879. }
  1880. if (leaf->map_token) {
  1881. unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
  1882. leaf->map_token = NULL;
  1883. }
  1884. /* shift the data */
  1885. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  1886. data_end - data_size, btrfs_leaf_data(leaf) +
  1887. data_end, old_data - data_end);
  1888. data_end = old_data;
  1889. old_size = btrfs_item_size_nr(leaf, slot);
  1890. item = btrfs_item_nr(leaf, slot);
  1891. btrfs_set_item_size(leaf, item, old_size + data_size);
  1892. btrfs_mark_buffer_dirty(leaf);
  1893. ret = 0;
  1894. if (btrfs_leaf_free_space(root, leaf) < 0) {
  1895. btrfs_print_leaf(root, leaf);
  1896. BUG();
  1897. }
  1898. return ret;
  1899. }
  1900. /*
  1901. * Given a key and some data, insert an item into the tree.
  1902. * This does all the path init required, making room in the tree if needed.
  1903. */
  1904. int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
  1905. struct btrfs_root *root,
  1906. struct btrfs_path *path,
  1907. struct btrfs_key *cpu_key, u32 data_size)
  1908. {
  1909. struct extent_buffer *leaf;
  1910. struct btrfs_item *item;
  1911. int ret = 0;
  1912. int slot;
  1913. int slot_orig;
  1914. u32 nritems;
  1915. unsigned int data_end;
  1916. struct btrfs_disk_key disk_key;
  1917. btrfs_cpu_key_to_disk(&disk_key, cpu_key);
  1918. /* create a root if there isn't one */
  1919. if (!root->node)
  1920. BUG();
  1921. ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
  1922. if (ret == 0) {
  1923. return -EEXIST;
  1924. }
  1925. if (ret < 0)
  1926. goto out;
  1927. slot_orig = path->slots[0];
  1928. leaf = path->nodes[0];
  1929. nritems = btrfs_header_nritems(leaf);
  1930. data_end = leaf_data_end(root, leaf);
  1931. if (btrfs_leaf_free_space(root, leaf) <
  1932. sizeof(struct btrfs_item) + data_size) {
  1933. btrfs_print_leaf(root, leaf);
  1934. printk("not enough freespace need %u have %d\n",
  1935. data_size, btrfs_leaf_free_space(root, leaf));
  1936. BUG();
  1937. }
  1938. slot = path->slots[0];
  1939. BUG_ON(slot < 0);
  1940. if (slot != nritems) {
  1941. int i;
  1942. unsigned int old_data = btrfs_item_end_nr(leaf, slot);
  1943. if (old_data < data_end) {
  1944. btrfs_print_leaf(root, leaf);
  1945. printk("slot %d old_data %d data_end %d\n",
  1946. slot, old_data, data_end);
  1947. BUG_ON(1);
  1948. }
  1949. /*
  1950. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  1951. */
  1952. /* first correct the data pointers */
  1953. WARN_ON(leaf->map_token);
  1954. for (i = slot; i < nritems; i++) {
  1955. u32 ioff;
  1956. item = btrfs_item_nr(leaf, i);
  1957. if (!leaf->map_token) {
  1958. map_extent_buffer(leaf, (unsigned long)item,
  1959. sizeof(struct btrfs_item),
  1960. &leaf->map_token, &leaf->kaddr,
  1961. &leaf->map_start, &leaf->map_len,
  1962. KM_USER1);
  1963. }
  1964. ioff = btrfs_item_offset(leaf, item);
  1965. btrfs_set_item_offset(leaf, item, ioff - data_size);
  1966. }
  1967. if (leaf->map_token) {
  1968. unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
  1969. leaf->map_token = NULL;
  1970. }
  1971. /* shift the items */
  1972. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
  1973. btrfs_item_nr_offset(slot),
  1974. (nritems - slot) * sizeof(struct btrfs_item));
  1975. /* shift the data */
  1976. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  1977. data_end - data_size, btrfs_leaf_data(leaf) +
  1978. data_end, old_data - data_end);
  1979. data_end = old_data;
  1980. }
  1981. /* setup the item for the new data */
  1982. btrfs_set_item_key(leaf, &disk_key, slot);
  1983. item = btrfs_item_nr(leaf, slot);
  1984. btrfs_set_item_offset(leaf, item, data_end - data_size);
  1985. btrfs_set_item_size(leaf, item, data_size);
  1986. btrfs_set_header_nritems(leaf, nritems + 1);
  1987. btrfs_mark_buffer_dirty(leaf);
  1988. ret = 0;
  1989. if (slot == 0)
  1990. ret = fixup_low_keys(trans, root, path, &disk_key, 1);
  1991. if (btrfs_leaf_free_space(root, leaf) < 0) {
  1992. btrfs_print_leaf(root, leaf);
  1993. BUG();
  1994. }
  1995. out:
  1996. return ret;
  1997. }
  1998. /*
  1999. * Given a key and some data, insert an item into the tree.
  2000. * This does all the path init required, making room in the tree if needed.
  2001. */
  2002. int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
  2003. *root, struct btrfs_key *cpu_key, void *data, u32
  2004. data_size)
  2005. {
  2006. int ret = 0;
  2007. struct btrfs_path *path;
  2008. struct extent_buffer *leaf;
  2009. unsigned long ptr;
  2010. path = btrfs_alloc_path();
  2011. BUG_ON(!path);
  2012. ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
  2013. if (!ret) {
  2014. leaf = path->nodes[0];
  2015. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  2016. write_extent_buffer(leaf, data, ptr, data_size);
  2017. btrfs_mark_buffer_dirty(leaf);
  2018. }
  2019. btrfs_free_path(path);
  2020. return ret;
  2021. }
  2022. /*
  2023. * delete the pointer from a given node.
  2024. *
  2025. * If the delete empties a node, the node is removed from the tree,
  2026. * continuing all the way the root if required. The root is converted into
  2027. * a leaf if all the nodes are emptied.
  2028. */
  2029. static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2030. struct btrfs_path *path, int level, int slot)
  2031. {
  2032. struct extent_buffer *parent = path->nodes[level];
  2033. u32 nritems;
  2034. int ret = 0;
  2035. int wret;
  2036. nritems = btrfs_header_nritems(parent);
  2037. if (slot != nritems -1) {
  2038. memmove_extent_buffer(parent,
  2039. btrfs_node_key_ptr_offset(slot),
  2040. btrfs_node_key_ptr_offset(slot + 1),
  2041. sizeof(struct btrfs_key_ptr) *
  2042. (nritems - slot - 1));
  2043. }
  2044. nritems--;
  2045. btrfs_set_header_nritems(parent, nritems);
  2046. if (nritems == 0 && parent == root->node) {
  2047. BUG_ON(btrfs_header_level(root->node) != 1);
  2048. /* just turn the root into a leaf and break */
  2049. btrfs_set_header_level(root->node, 0);
  2050. } else if (slot == 0) {
  2051. struct btrfs_disk_key disk_key;
  2052. btrfs_node_key(parent, &disk_key, 0);
  2053. wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
  2054. if (wret)
  2055. ret = wret;
  2056. }
  2057. btrfs_mark_buffer_dirty(parent);
  2058. return ret;
  2059. }
  2060. /*
  2061. * delete the item at the leaf level in path. If that empties
  2062. * the leaf, remove it from the tree
  2063. */
  2064. int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2065. struct btrfs_path *path)
  2066. {
  2067. int slot;
  2068. struct extent_buffer *leaf;
  2069. struct btrfs_item *item;
  2070. int doff;
  2071. int dsize;
  2072. int ret = 0;
  2073. int wret;
  2074. u32 nritems;
  2075. leaf = path->nodes[0];
  2076. slot = path->slots[0];
  2077. doff = btrfs_item_offset_nr(leaf, slot);
  2078. dsize = btrfs_item_size_nr(leaf, slot);
  2079. nritems = btrfs_header_nritems(leaf);
  2080. if (slot != nritems - 1) {
  2081. int i;
  2082. int data_end = leaf_data_end(root, leaf);
  2083. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  2084. data_end + dsize,
  2085. btrfs_leaf_data(leaf) + data_end,
  2086. doff - data_end);
  2087. for (i = slot + 1; i < nritems; i++) {
  2088. u32 ioff;
  2089. item = btrfs_item_nr(leaf, i);
  2090. if (!leaf->map_token) {
  2091. map_extent_buffer(leaf, (unsigned long)item,
  2092. sizeof(struct btrfs_item),
  2093. &leaf->map_token, &leaf->kaddr,
  2094. &leaf->map_start, &leaf->map_len,
  2095. KM_USER1);
  2096. }
  2097. ioff = btrfs_item_offset(leaf, item);
  2098. btrfs_set_item_offset(leaf, item, ioff + dsize);
  2099. }
  2100. if (leaf->map_token) {
  2101. unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
  2102. leaf->map_token = NULL;
  2103. }
  2104. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
  2105. btrfs_item_nr_offset(slot + 1),
  2106. sizeof(struct btrfs_item) *
  2107. (nritems - slot - 1));
  2108. }
  2109. btrfs_set_header_nritems(leaf, nritems - 1);
  2110. nritems--;
  2111. /* delete the leaf if we've emptied it */
  2112. if (nritems == 0) {
  2113. if (leaf == root->node) {
  2114. btrfs_set_header_level(leaf, 0);
  2115. } else {
  2116. clean_tree_block(trans, root, leaf);
  2117. wait_on_tree_block_writeback(root, leaf);
  2118. wret = del_ptr(trans, root, path, 1, path->slots[1]);
  2119. if (wret)
  2120. ret = wret;
  2121. wret = btrfs_free_extent(trans, root,
  2122. leaf->start, leaf->len, 1);
  2123. if (wret)
  2124. ret = wret;
  2125. }
  2126. } else {
  2127. int used = leaf_space_used(leaf, 0, nritems);
  2128. if (slot == 0) {
  2129. struct btrfs_disk_key disk_key;
  2130. btrfs_item_key(leaf, &disk_key, 0);
  2131. wret = fixup_low_keys(trans, root, path,
  2132. &disk_key, 1);
  2133. if (wret)
  2134. ret = wret;
  2135. }
  2136. /* delete the leaf if it is mostly empty */
  2137. if (0 && used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
  2138. /* push_leaf_left fixes the path.
  2139. * make sure the path still points to our leaf
  2140. * for possible call to del_ptr below
  2141. */
  2142. slot = path->slots[1];
  2143. extent_buffer_get(leaf);
  2144. wret = push_leaf_left(trans, root, path, 1);
  2145. if (wret < 0 && wret != -ENOSPC)
  2146. ret = wret;
  2147. if (path->nodes[0] == leaf &&
  2148. btrfs_header_nritems(leaf)) {
  2149. wret = push_leaf_right(trans, root, path, 1);
  2150. if (wret < 0 && wret != -ENOSPC)
  2151. ret = wret;
  2152. }
  2153. if (btrfs_header_nritems(leaf) == 0) {
  2154. u64 bytenr = leaf->start;
  2155. u32 blocksize = leaf->len;
  2156. clean_tree_block(trans, root, leaf);
  2157. wait_on_tree_block_writeback(root, leaf);
  2158. wret = del_ptr(trans, root, path, 1, slot);
  2159. if (wret)
  2160. ret = wret;
  2161. free_extent_buffer(leaf);
  2162. wret = btrfs_free_extent(trans, root, bytenr,
  2163. blocksize, 1);
  2164. if (wret)
  2165. ret = wret;
  2166. } else {
  2167. btrfs_mark_buffer_dirty(leaf);
  2168. free_extent_buffer(leaf);
  2169. }
  2170. } else {
  2171. btrfs_mark_buffer_dirty(leaf);
  2172. }
  2173. }
  2174. return ret;
  2175. }
  2176. /*
  2177. * walk up the tree as far as required to find the next leaf.
  2178. * returns 0 if it found something or 1 if there are no greater leaves.
  2179. * returns < 0 on io errors.
  2180. */
  2181. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
  2182. {
  2183. int slot;
  2184. int level = 1;
  2185. u64 bytenr;
  2186. struct extent_buffer *c;
  2187. struct extent_buffer *next = NULL;
  2188. while(level < BTRFS_MAX_LEVEL) {
  2189. if (!path->nodes[level])
  2190. return 1;
  2191. slot = path->slots[level] + 1;
  2192. c = path->nodes[level];
  2193. if (slot >= btrfs_header_nritems(c)) {
  2194. level++;
  2195. continue;
  2196. }
  2197. bytenr = btrfs_node_blockptr(c, slot);
  2198. if (next)
  2199. free_extent_buffer(next);
  2200. if (path->reada)
  2201. reada_for_search(root, path, level, slot);
  2202. next = read_tree_block(root, bytenr,
  2203. btrfs_level_size(root, level -1));
  2204. break;
  2205. }
  2206. path->slots[level] = slot;
  2207. while(1) {
  2208. level--;
  2209. c = path->nodes[level];
  2210. free_extent_buffer(c);
  2211. path->nodes[level] = next;
  2212. path->slots[level] = 0;
  2213. if (!level)
  2214. break;
  2215. if (path->reada)
  2216. reada_for_search(root, path, level, 0);
  2217. next = read_tree_block(root, btrfs_node_blockptr(next, 0),
  2218. btrfs_level_size(root, level - 1));
  2219. }
  2220. return 0;
  2221. }