ctree.c 70 KB

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