ctree.c 65 KB

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