fix_node.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821
  1. /*
  2. * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
  3. */
  4. #include <linux/time.h>
  5. #include <linux/slab.h>
  6. #include <linux/string.h>
  7. #include "reiserfs.h"
  8. #include <linux/buffer_head.h>
  9. /*
  10. * To make any changes in the tree we find a node that contains item
  11. * to be changed/deleted or position in the node we insert a new item
  12. * to. We call this node S. To do balancing we need to decide what we
  13. * will shift to left/right neighbor, or to a new node, where new item
  14. * will be etc. To make this analysis simpler we build virtual
  15. * node. Virtual node is an array of items, that will replace items of
  16. * node S. (For instance if we are going to delete an item, virtual
  17. * node does not contain it). Virtual node keeps information about
  18. * item sizes and types, mergeability of first and last items, sizes
  19. * of all entries in directory item. We use this array of items when
  20. * calculating what we can shift to neighbors and how many nodes we
  21. * have to have if we do not any shiftings, if we shift to left/right
  22. * neighbor or to both.
  23. */
  24. /*
  25. * Takes item number in virtual node, returns number of item
  26. * that it has in source buffer
  27. */
  28. static inline int old_item_num(int new_num, int affected_item_num, int mode)
  29. {
  30. if (mode == M_PASTE || mode == M_CUT || new_num < affected_item_num)
  31. return new_num;
  32. if (mode == M_INSERT) {
  33. RFALSE(new_num == 0,
  34. "vs-8005: for INSERT mode and item number of inserted item");
  35. return new_num - 1;
  36. }
  37. RFALSE(mode != M_DELETE,
  38. "vs-8010: old_item_num: mode must be M_DELETE (mode = \'%c\'",
  39. mode);
  40. /* delete mode */
  41. return new_num + 1;
  42. }
  43. static void create_virtual_node(struct tree_balance *tb, int h)
  44. {
  45. struct item_head *ih;
  46. struct virtual_node *vn = tb->tb_vn;
  47. int new_num;
  48. struct buffer_head *Sh; /* this comes from tb->S[h] */
  49. Sh = PATH_H_PBUFFER(tb->tb_path, h);
  50. /* size of changed node */
  51. vn->vn_size =
  52. MAX_CHILD_SIZE(Sh) - B_FREE_SPACE(Sh) + tb->insert_size[h];
  53. /* for internal nodes array if virtual items is not created */
  54. if (h) {
  55. vn->vn_nr_item = (vn->vn_size - DC_SIZE) / (DC_SIZE + KEY_SIZE);
  56. return;
  57. }
  58. /* number of items in virtual node */
  59. vn->vn_nr_item =
  60. B_NR_ITEMS(Sh) + ((vn->vn_mode == M_INSERT) ? 1 : 0) -
  61. ((vn->vn_mode == M_DELETE) ? 1 : 0);
  62. /* first virtual item */
  63. vn->vn_vi = (struct virtual_item *)(tb->tb_vn + 1);
  64. memset(vn->vn_vi, 0, vn->vn_nr_item * sizeof(struct virtual_item));
  65. vn->vn_free_ptr += vn->vn_nr_item * sizeof(struct virtual_item);
  66. /* first item in the node */
  67. ih = item_head(Sh, 0);
  68. /* define the mergeability for 0-th item (if it is not being deleted) */
  69. if (op_is_left_mergeable(&ih->ih_key, Sh->b_size)
  70. && (vn->vn_mode != M_DELETE || vn->vn_affected_item_num))
  71. vn->vn_vi[0].vi_type |= VI_TYPE_LEFT_MERGEABLE;
  72. /*
  73. * go through all items that remain in the virtual
  74. * node (except for the new (inserted) one)
  75. */
  76. for (new_num = 0; new_num < vn->vn_nr_item; new_num++) {
  77. int j;
  78. struct virtual_item *vi = vn->vn_vi + new_num;
  79. int is_affected =
  80. ((new_num != vn->vn_affected_item_num) ? 0 : 1);
  81. if (is_affected && vn->vn_mode == M_INSERT)
  82. continue;
  83. /* get item number in source node */
  84. j = old_item_num(new_num, vn->vn_affected_item_num,
  85. vn->vn_mode);
  86. vi->vi_item_len += ih_item_len(ih + j) + IH_SIZE;
  87. vi->vi_ih = ih + j;
  88. vi->vi_item = ih_item_body(Sh, ih + j);
  89. vi->vi_uarea = vn->vn_free_ptr;
  90. /*
  91. * FIXME: there is no check that item operation did not
  92. * consume too much memory
  93. */
  94. vn->vn_free_ptr +=
  95. op_create_vi(vn, vi, is_affected, tb->insert_size[0]);
  96. if (tb->vn_buf + tb->vn_buf_size < vn->vn_free_ptr)
  97. reiserfs_panic(tb->tb_sb, "vs-8030",
  98. "virtual node space consumed");
  99. if (!is_affected)
  100. /* this is not being changed */
  101. continue;
  102. if (vn->vn_mode == M_PASTE || vn->vn_mode == M_CUT) {
  103. vn->vn_vi[new_num].vi_item_len += tb->insert_size[0];
  104. /* pointer to data which is going to be pasted */
  105. vi->vi_new_data = vn->vn_data;
  106. }
  107. }
  108. /* virtual inserted item is not defined yet */
  109. if (vn->vn_mode == M_INSERT) {
  110. struct virtual_item *vi = vn->vn_vi + vn->vn_affected_item_num;
  111. RFALSE(vn->vn_ins_ih == NULL,
  112. "vs-8040: item header of inserted item is not specified");
  113. vi->vi_item_len = tb->insert_size[0];
  114. vi->vi_ih = vn->vn_ins_ih;
  115. vi->vi_item = vn->vn_data;
  116. vi->vi_uarea = vn->vn_free_ptr;
  117. op_create_vi(vn, vi, 0 /*not pasted or cut */ ,
  118. tb->insert_size[0]);
  119. }
  120. /*
  121. * set right merge flag we take right delimiting key and
  122. * check whether it is a mergeable item
  123. */
  124. if (tb->CFR[0]) {
  125. struct reiserfs_key *key;
  126. key = internal_key(tb->CFR[0], tb->rkey[0]);
  127. if (op_is_left_mergeable(key, Sh->b_size)
  128. && (vn->vn_mode != M_DELETE
  129. || vn->vn_affected_item_num != B_NR_ITEMS(Sh) - 1))
  130. vn->vn_vi[vn->vn_nr_item - 1].vi_type |=
  131. VI_TYPE_RIGHT_MERGEABLE;
  132. #ifdef CONFIG_REISERFS_CHECK
  133. if (op_is_left_mergeable(key, Sh->b_size) &&
  134. !(vn->vn_mode != M_DELETE
  135. || vn->vn_affected_item_num != B_NR_ITEMS(Sh) - 1)) {
  136. /*
  137. * we delete last item and it could be merged
  138. * with right neighbor's first item
  139. */
  140. if (!
  141. (B_NR_ITEMS(Sh) == 1
  142. && is_direntry_le_ih(item_head(Sh, 0))
  143. && ih_entry_count(item_head(Sh, 0)) == 1)) {
  144. /*
  145. * node contains more than 1 item, or item
  146. * is not directory item, or this item
  147. * contains more than 1 entry
  148. */
  149. print_block(Sh, 0, -1, -1);
  150. reiserfs_panic(tb->tb_sb, "vs-8045",
  151. "rdkey %k, affected item==%d "
  152. "(mode==%c) Must be %c",
  153. key, vn->vn_affected_item_num,
  154. vn->vn_mode, M_DELETE);
  155. }
  156. }
  157. #endif
  158. }
  159. }
  160. /*
  161. * Using virtual node check, how many items can be
  162. * shifted to left neighbor
  163. */
  164. static void check_left(struct tree_balance *tb, int h, int cur_free)
  165. {
  166. int i;
  167. struct virtual_node *vn = tb->tb_vn;
  168. struct virtual_item *vi;
  169. int d_size, ih_size;
  170. RFALSE(cur_free < 0, "vs-8050: cur_free (%d) < 0", cur_free);
  171. /* internal level */
  172. if (h > 0) {
  173. tb->lnum[h] = cur_free / (DC_SIZE + KEY_SIZE);
  174. return;
  175. }
  176. /* leaf level */
  177. if (!cur_free || !vn->vn_nr_item) {
  178. /* no free space or nothing to move */
  179. tb->lnum[h] = 0;
  180. tb->lbytes = -1;
  181. return;
  182. }
  183. RFALSE(!PATH_H_PPARENT(tb->tb_path, 0),
  184. "vs-8055: parent does not exist or invalid");
  185. vi = vn->vn_vi;
  186. if ((unsigned int)cur_free >=
  187. (vn->vn_size -
  188. ((vi->vi_type & VI_TYPE_LEFT_MERGEABLE) ? IH_SIZE : 0))) {
  189. /* all contents of S[0] fits into L[0] */
  190. RFALSE(vn->vn_mode == M_INSERT || vn->vn_mode == M_PASTE,
  191. "vs-8055: invalid mode or balance condition failed");
  192. tb->lnum[0] = vn->vn_nr_item;
  193. tb->lbytes = -1;
  194. return;
  195. }
  196. d_size = 0, ih_size = IH_SIZE;
  197. /* first item may be merge with last item in left neighbor */
  198. if (vi->vi_type & VI_TYPE_LEFT_MERGEABLE)
  199. d_size = -((int)IH_SIZE), ih_size = 0;
  200. tb->lnum[0] = 0;
  201. for (i = 0; i < vn->vn_nr_item;
  202. i++, ih_size = IH_SIZE, d_size = 0, vi++) {
  203. d_size += vi->vi_item_len;
  204. if (cur_free >= d_size) {
  205. /* the item can be shifted entirely */
  206. cur_free -= d_size;
  207. tb->lnum[0]++;
  208. continue;
  209. }
  210. /* the item cannot be shifted entirely, try to split it */
  211. /*
  212. * check whether L[0] can hold ih and at least one byte
  213. * of the item body
  214. */
  215. /* cannot shift even a part of the current item */
  216. if (cur_free <= ih_size) {
  217. tb->lbytes = -1;
  218. return;
  219. }
  220. cur_free -= ih_size;
  221. tb->lbytes = op_check_left(vi, cur_free, 0, 0);
  222. if (tb->lbytes != -1)
  223. /* count partially shifted item */
  224. tb->lnum[0]++;
  225. break;
  226. }
  227. return;
  228. }
  229. /*
  230. * Using virtual node check, how many items can be
  231. * shifted to right neighbor
  232. */
  233. static void check_right(struct tree_balance *tb, int h, int cur_free)
  234. {
  235. int i;
  236. struct virtual_node *vn = tb->tb_vn;
  237. struct virtual_item *vi;
  238. int d_size, ih_size;
  239. RFALSE(cur_free < 0, "vs-8070: cur_free < 0");
  240. /* internal level */
  241. if (h > 0) {
  242. tb->rnum[h] = cur_free / (DC_SIZE + KEY_SIZE);
  243. return;
  244. }
  245. /* leaf level */
  246. if (!cur_free || !vn->vn_nr_item) {
  247. /* no free space */
  248. tb->rnum[h] = 0;
  249. tb->rbytes = -1;
  250. return;
  251. }
  252. RFALSE(!PATH_H_PPARENT(tb->tb_path, 0),
  253. "vs-8075: parent does not exist or invalid");
  254. vi = vn->vn_vi + vn->vn_nr_item - 1;
  255. if ((unsigned int)cur_free >=
  256. (vn->vn_size -
  257. ((vi->vi_type & VI_TYPE_RIGHT_MERGEABLE) ? IH_SIZE : 0))) {
  258. /* all contents of S[0] fits into R[0] */
  259. RFALSE(vn->vn_mode == M_INSERT || vn->vn_mode == M_PASTE,
  260. "vs-8080: invalid mode or balance condition failed");
  261. tb->rnum[h] = vn->vn_nr_item;
  262. tb->rbytes = -1;
  263. return;
  264. }
  265. d_size = 0, ih_size = IH_SIZE;
  266. /* last item may be merge with first item in right neighbor */
  267. if (vi->vi_type & VI_TYPE_RIGHT_MERGEABLE)
  268. d_size = -(int)IH_SIZE, ih_size = 0;
  269. tb->rnum[0] = 0;
  270. for (i = vn->vn_nr_item - 1; i >= 0;
  271. i--, d_size = 0, ih_size = IH_SIZE, vi--) {
  272. d_size += vi->vi_item_len;
  273. if (cur_free >= d_size) {
  274. /* the item can be shifted entirely */
  275. cur_free -= d_size;
  276. tb->rnum[0]++;
  277. continue;
  278. }
  279. /*
  280. * check whether R[0] can hold ih and at least one
  281. * byte of the item body
  282. */
  283. /* cannot shift even a part of the current item */
  284. if (cur_free <= ih_size) {
  285. tb->rbytes = -1;
  286. return;
  287. }
  288. /*
  289. * R[0] can hold the header of the item and at least
  290. * one byte of its body
  291. */
  292. cur_free -= ih_size; /* cur_free is still > 0 */
  293. tb->rbytes = op_check_right(vi, cur_free);
  294. if (tb->rbytes != -1)
  295. /* count partially shifted item */
  296. tb->rnum[0]++;
  297. break;
  298. }
  299. return;
  300. }
  301. /*
  302. * from - number of items, which are shifted to left neighbor entirely
  303. * to - number of item, which are shifted to right neighbor entirely
  304. * from_bytes - number of bytes of boundary item (or directory entries)
  305. * which are shifted to left neighbor
  306. * to_bytes - number of bytes of boundary item (or directory entries)
  307. * which are shifted to right neighbor
  308. */
  309. static int get_num_ver(int mode, struct tree_balance *tb, int h,
  310. int from, int from_bytes,
  311. int to, int to_bytes, short *snum012, int flow)
  312. {
  313. int i;
  314. int units;
  315. struct virtual_node *vn = tb->tb_vn;
  316. int total_node_size, max_node_size, current_item_size;
  317. int needed_nodes;
  318. /* position of item we start filling node from */
  319. int start_item;
  320. /* position of item we finish filling node by */
  321. int end_item;
  322. /*
  323. * number of first bytes (entries for directory) of start_item-th item
  324. * we do not include into node that is being filled
  325. */
  326. int start_bytes;
  327. /*
  328. * number of last bytes (entries for directory) of end_item-th item
  329. * we do node include into node that is being filled
  330. */
  331. int end_bytes;
  332. /*
  333. * these are positions in virtual item of items, that are split
  334. * between S[0] and S1new and S1new and S2new
  335. */
  336. int split_item_positions[2];
  337. split_item_positions[0] = -1;
  338. split_item_positions[1] = -1;
  339. /*
  340. * We only create additional nodes if we are in insert or paste mode
  341. * or we are in replace mode at the internal level. If h is 0 and
  342. * the mode is M_REPLACE then in fix_nodes we change the mode to
  343. * paste or insert before we get here in the code.
  344. */
  345. RFALSE(tb->insert_size[h] < 0 || (mode != M_INSERT && mode != M_PASTE),
  346. "vs-8100: insert_size < 0 in overflow");
  347. max_node_size = MAX_CHILD_SIZE(PATH_H_PBUFFER(tb->tb_path, h));
  348. /*
  349. * snum012 [0-2] - number of items, that lay
  350. * to S[0], first new node and second new node
  351. */
  352. snum012[3] = -1; /* s1bytes */
  353. snum012[4] = -1; /* s2bytes */
  354. /* internal level */
  355. if (h > 0) {
  356. i = ((to - from) * (KEY_SIZE + DC_SIZE) + DC_SIZE);
  357. if (i == max_node_size)
  358. return 1;
  359. return (i / max_node_size + 1);
  360. }
  361. /* leaf level */
  362. needed_nodes = 1;
  363. total_node_size = 0;
  364. /* start from 'from'-th item */
  365. start_item = from;
  366. /* skip its first 'start_bytes' units */
  367. start_bytes = ((from_bytes != -1) ? from_bytes : 0);
  368. /* last included item is the 'end_item'-th one */
  369. end_item = vn->vn_nr_item - to - 1;
  370. /* do not count last 'end_bytes' units of 'end_item'-th item */
  371. end_bytes = (to_bytes != -1) ? to_bytes : 0;
  372. /*
  373. * go through all item beginning from the start_item-th item
  374. * and ending by the end_item-th item. Do not count first
  375. * 'start_bytes' units of 'start_item'-th item and last
  376. * 'end_bytes' of 'end_item'-th item
  377. */
  378. for (i = start_item; i <= end_item; i++) {
  379. struct virtual_item *vi = vn->vn_vi + i;
  380. int skip_from_end = ((i == end_item) ? end_bytes : 0);
  381. RFALSE(needed_nodes > 3, "vs-8105: too many nodes are needed");
  382. /* get size of current item */
  383. current_item_size = vi->vi_item_len;
  384. /*
  385. * do not take in calculation head part (from_bytes)
  386. * of from-th item
  387. */
  388. current_item_size -=
  389. op_part_size(vi, 0 /*from start */ , start_bytes);
  390. /* do not take in calculation tail part of last item */
  391. current_item_size -=
  392. op_part_size(vi, 1 /*from end */ , skip_from_end);
  393. /* if item fits into current node entierly */
  394. if (total_node_size + current_item_size <= max_node_size) {
  395. snum012[needed_nodes - 1]++;
  396. total_node_size += current_item_size;
  397. start_bytes = 0;
  398. continue;
  399. }
  400. /*
  401. * virtual item length is longer, than max size of item in
  402. * a node. It is impossible for direct item
  403. */
  404. if (current_item_size > max_node_size) {
  405. RFALSE(is_direct_le_ih(vi->vi_ih),
  406. "vs-8110: "
  407. "direct item length is %d. It can not be longer than %d",
  408. current_item_size, max_node_size);
  409. /* we will try to split it */
  410. flow = 1;
  411. }
  412. /* as we do not split items, take new node and continue */
  413. if (!flow) {
  414. needed_nodes++;
  415. i--;
  416. total_node_size = 0;
  417. continue;
  418. }
  419. /*
  420. * calculate number of item units which fit into node being
  421. * filled
  422. */
  423. {
  424. int free_space;
  425. free_space = max_node_size - total_node_size - IH_SIZE;
  426. units =
  427. op_check_left(vi, free_space, start_bytes,
  428. skip_from_end);
  429. /*
  430. * nothing fits into current node, take new
  431. * node and continue
  432. */
  433. if (units == -1) {
  434. needed_nodes++, i--, total_node_size = 0;
  435. continue;
  436. }
  437. }
  438. /* something fits into the current node */
  439. start_bytes += units;
  440. snum012[needed_nodes - 1 + 3] = units;
  441. if (needed_nodes > 2)
  442. reiserfs_warning(tb->tb_sb, "vs-8111",
  443. "split_item_position is out of range");
  444. snum012[needed_nodes - 1]++;
  445. split_item_positions[needed_nodes - 1] = i;
  446. needed_nodes++;
  447. /* continue from the same item with start_bytes != -1 */
  448. start_item = i;
  449. i--;
  450. total_node_size = 0;
  451. }
  452. /*
  453. * sum012[4] (if it is not -1) contains number of units of which
  454. * are to be in S1new, snum012[3] - to be in S0. They are supposed
  455. * to be S1bytes and S2bytes correspondingly, so recalculate
  456. */
  457. if (snum012[4] > 0) {
  458. int split_item_num;
  459. int bytes_to_r, bytes_to_l;
  460. int bytes_to_S1new;
  461. split_item_num = split_item_positions[1];
  462. bytes_to_l =
  463. ((from == split_item_num
  464. && from_bytes != -1) ? from_bytes : 0);
  465. bytes_to_r =
  466. ((end_item == split_item_num
  467. && end_bytes != -1) ? end_bytes : 0);
  468. bytes_to_S1new =
  469. ((split_item_positions[0] ==
  470. split_item_positions[1]) ? snum012[3] : 0);
  471. /* s2bytes */
  472. snum012[4] =
  473. op_unit_num(&vn->vn_vi[split_item_num]) - snum012[4] -
  474. bytes_to_r - bytes_to_l - bytes_to_S1new;
  475. if (vn->vn_vi[split_item_num].vi_index != TYPE_DIRENTRY &&
  476. vn->vn_vi[split_item_num].vi_index != TYPE_INDIRECT)
  477. reiserfs_warning(tb->tb_sb, "vs-8115",
  478. "not directory or indirect item");
  479. }
  480. /* now we know S2bytes, calculate S1bytes */
  481. if (snum012[3] > 0) {
  482. int split_item_num;
  483. int bytes_to_r, bytes_to_l;
  484. int bytes_to_S2new;
  485. split_item_num = split_item_positions[0];
  486. bytes_to_l =
  487. ((from == split_item_num
  488. && from_bytes != -1) ? from_bytes : 0);
  489. bytes_to_r =
  490. ((end_item == split_item_num
  491. && end_bytes != -1) ? end_bytes : 0);
  492. bytes_to_S2new =
  493. ((split_item_positions[0] == split_item_positions[1]
  494. && snum012[4] != -1) ? snum012[4] : 0);
  495. /* s1bytes */
  496. snum012[3] =
  497. op_unit_num(&vn->vn_vi[split_item_num]) - snum012[3] -
  498. bytes_to_r - bytes_to_l - bytes_to_S2new;
  499. }
  500. return needed_nodes;
  501. }
  502. /*
  503. * Set parameters for balancing.
  504. * Performs write of results of analysis of balancing into structure tb,
  505. * where it will later be used by the functions that actually do the balancing.
  506. * Parameters:
  507. * tb tree_balance structure;
  508. * h current level of the node;
  509. * lnum number of items from S[h] that must be shifted to L[h];
  510. * rnum number of items from S[h] that must be shifted to R[h];
  511. * blk_num number of blocks that S[h] will be splitted into;
  512. * s012 number of items that fall into splitted nodes.
  513. * lbytes number of bytes which flow to the left neighbor from the
  514. * item that is not shifted entirely
  515. * rbytes number of bytes which flow to the right neighbor from the
  516. * item that is not shifted entirely
  517. * s1bytes number of bytes which flow to the first new node when
  518. * S[0] splits (this number is contained in s012 array)
  519. */
  520. static void set_parameters(struct tree_balance *tb, int h, int lnum,
  521. int rnum, int blk_num, short *s012, int lb, int rb)
  522. {
  523. tb->lnum[h] = lnum;
  524. tb->rnum[h] = rnum;
  525. tb->blknum[h] = blk_num;
  526. /* only for leaf level */
  527. if (h == 0) {
  528. if (s012 != NULL) {
  529. tb->s0num = *s012++;
  530. tb->snum[0] = *s012++;
  531. tb->snum[1] = *s012++;
  532. tb->sbytes[0] = *s012++;
  533. tb->sbytes[1] = *s012;
  534. }
  535. tb->lbytes = lb;
  536. tb->rbytes = rb;
  537. }
  538. PROC_INFO_ADD(tb->tb_sb, lnum[h], lnum);
  539. PROC_INFO_ADD(tb->tb_sb, rnum[h], rnum);
  540. PROC_INFO_ADD(tb->tb_sb, lbytes[h], lb);
  541. PROC_INFO_ADD(tb->tb_sb, rbytes[h], rb);
  542. }
  543. /*
  544. * check if node disappears if we shift tb->lnum[0] items to left
  545. * neighbor and tb->rnum[0] to the right one.
  546. */
  547. static int is_leaf_removable(struct tree_balance *tb)
  548. {
  549. struct virtual_node *vn = tb->tb_vn;
  550. int to_left, to_right;
  551. int size;
  552. int remain_items;
  553. /*
  554. * number of items that will be shifted to left (right) neighbor
  555. * entirely
  556. */
  557. to_left = tb->lnum[0] - ((tb->lbytes != -1) ? 1 : 0);
  558. to_right = tb->rnum[0] - ((tb->rbytes != -1) ? 1 : 0);
  559. remain_items = vn->vn_nr_item;
  560. /* how many items remain in S[0] after shiftings to neighbors */
  561. remain_items -= (to_left + to_right);
  562. /* all content of node can be shifted to neighbors */
  563. if (remain_items < 1) {
  564. set_parameters(tb, 0, to_left, vn->vn_nr_item - to_left, 0,
  565. NULL, -1, -1);
  566. return 1;
  567. }
  568. /* S[0] is not removable */
  569. if (remain_items > 1 || tb->lbytes == -1 || tb->rbytes == -1)
  570. return 0;
  571. /* check whether we can divide 1 remaining item between neighbors */
  572. /* get size of remaining item (in item units) */
  573. size = op_unit_num(&vn->vn_vi[to_left]);
  574. if (tb->lbytes + tb->rbytes >= size) {
  575. set_parameters(tb, 0, to_left + 1, to_right + 1, 0, NULL,
  576. tb->lbytes, -1);
  577. return 1;
  578. }
  579. return 0;
  580. }
  581. /* check whether L, S, R can be joined in one node */
  582. static int are_leaves_removable(struct tree_balance *tb, int lfree, int rfree)
  583. {
  584. struct virtual_node *vn = tb->tb_vn;
  585. int ih_size;
  586. struct buffer_head *S0;
  587. S0 = PATH_H_PBUFFER(tb->tb_path, 0);
  588. ih_size = 0;
  589. if (vn->vn_nr_item) {
  590. if (vn->vn_vi[0].vi_type & VI_TYPE_LEFT_MERGEABLE)
  591. ih_size += IH_SIZE;
  592. if (vn->vn_vi[vn->vn_nr_item - 1].
  593. vi_type & VI_TYPE_RIGHT_MERGEABLE)
  594. ih_size += IH_SIZE;
  595. } else {
  596. /* there was only one item and it will be deleted */
  597. struct item_head *ih;
  598. RFALSE(B_NR_ITEMS(S0) != 1,
  599. "vs-8125: item number must be 1: it is %d",
  600. B_NR_ITEMS(S0));
  601. ih = item_head(S0, 0);
  602. if (tb->CFR[0]
  603. && !comp_short_le_keys(&ih->ih_key,
  604. internal_key(tb->CFR[0],
  605. tb->rkey[0])))
  606. /*
  607. * Directory must be in correct state here: that is
  608. * somewhere at the left side should exist first
  609. * directory item. But the item being deleted can
  610. * not be that first one because its right neighbor
  611. * is item of the same directory. (But first item
  612. * always gets deleted in last turn). So, neighbors
  613. * of deleted item can be merged, so we can save
  614. * ih_size
  615. */
  616. if (is_direntry_le_ih(ih)) {
  617. ih_size = IH_SIZE;
  618. /*
  619. * we might check that left neighbor exists
  620. * and is of the same directory
  621. */
  622. RFALSE(le_ih_k_offset(ih) == DOT_OFFSET,
  623. "vs-8130: first directory item can not be removed until directory is not empty");
  624. }
  625. }
  626. if (MAX_CHILD_SIZE(S0) + vn->vn_size <= rfree + lfree + ih_size) {
  627. set_parameters(tb, 0, -1, -1, -1, NULL, -1, -1);
  628. PROC_INFO_INC(tb->tb_sb, leaves_removable);
  629. return 1;
  630. }
  631. return 0;
  632. }
  633. /* when we do not split item, lnum and rnum are numbers of entire items */
  634. #define SET_PAR_SHIFT_LEFT \
  635. if (h)\
  636. {\
  637. int to_l;\
  638. \
  639. to_l = (MAX_NR_KEY(Sh)+1 - lpar + vn->vn_nr_item + 1) / 2 -\
  640. (MAX_NR_KEY(Sh) + 1 - lpar);\
  641. \
  642. set_parameters (tb, h, to_l, 0, lnver, NULL, -1, -1);\
  643. }\
  644. else \
  645. {\
  646. if (lset==LEFT_SHIFT_FLOW)\
  647. set_parameters (tb, h, lpar, 0, lnver, snum012+lset,\
  648. tb->lbytes, -1);\
  649. else\
  650. set_parameters (tb, h, lpar - (tb->lbytes!=-1), 0, lnver, snum012+lset,\
  651. -1, -1);\
  652. }
  653. #define SET_PAR_SHIFT_RIGHT \
  654. if (h)\
  655. {\
  656. int to_r;\
  657. \
  658. to_r = (MAX_NR_KEY(Sh)+1 - rpar + vn->vn_nr_item + 1) / 2 - (MAX_NR_KEY(Sh) + 1 - rpar);\
  659. \
  660. set_parameters (tb, h, 0, to_r, rnver, NULL, -1, -1);\
  661. }\
  662. else \
  663. {\
  664. if (rset==RIGHT_SHIFT_FLOW)\
  665. set_parameters (tb, h, 0, rpar, rnver, snum012+rset,\
  666. -1, tb->rbytes);\
  667. else\
  668. set_parameters (tb, h, 0, rpar - (tb->rbytes!=-1), rnver, snum012+rset,\
  669. -1, -1);\
  670. }
  671. static void free_buffers_in_tb(struct tree_balance *tb)
  672. {
  673. int i;
  674. pathrelse(tb->tb_path);
  675. for (i = 0; i < MAX_HEIGHT; i++) {
  676. brelse(tb->L[i]);
  677. brelse(tb->R[i]);
  678. brelse(tb->FL[i]);
  679. brelse(tb->FR[i]);
  680. brelse(tb->CFL[i]);
  681. brelse(tb->CFR[i]);
  682. tb->L[i] = NULL;
  683. tb->R[i] = NULL;
  684. tb->FL[i] = NULL;
  685. tb->FR[i] = NULL;
  686. tb->CFL[i] = NULL;
  687. tb->CFR[i] = NULL;
  688. }
  689. }
  690. /*
  691. * Get new buffers for storing new nodes that are created while balancing.
  692. * Returns: SCHEDULE_OCCURRED - schedule occurred while the function worked;
  693. * CARRY_ON - schedule didn't occur while the function worked;
  694. * NO_DISK_SPACE - no disk space.
  695. */
  696. /* The function is NOT SCHEDULE-SAFE! */
  697. static int get_empty_nodes(struct tree_balance *tb, int h)
  698. {
  699. struct buffer_head *new_bh, *Sh = PATH_H_PBUFFER(tb->tb_path, h);
  700. b_blocknr_t *blocknr, blocknrs[MAX_AMOUNT_NEEDED] = { 0, };
  701. int counter, number_of_freeblk;
  702. int amount_needed; /* number of needed empty blocks */
  703. int retval = CARRY_ON;
  704. struct super_block *sb = tb->tb_sb;
  705. /*
  706. * number_of_freeblk is the number of empty blocks which have been
  707. * acquired for use by the balancing algorithm minus the number of
  708. * empty blocks used in the previous levels of the analysis,
  709. * number_of_freeblk = tb->cur_blknum can be non-zero if a schedule
  710. * occurs after empty blocks are acquired, and the balancing analysis
  711. * is then restarted, amount_needed is the number needed by this
  712. * level (h) of the balancing analysis.
  713. *
  714. * Note that for systems with many processes writing, it would be
  715. * more layout optimal to calculate the total number needed by all
  716. * levels and then to run reiserfs_new_blocks to get all of them at
  717. * once.
  718. */
  719. /*
  720. * Initiate number_of_freeblk to the amount acquired prior to the
  721. * restart of the analysis or 0 if not restarted, then subtract the
  722. * amount needed by all of the levels of the tree below h.
  723. */
  724. /* blknum includes S[h], so we subtract 1 in this calculation */
  725. for (counter = 0, number_of_freeblk = tb->cur_blknum;
  726. counter < h; counter++)
  727. number_of_freeblk -=
  728. (tb->blknum[counter]) ? (tb->blknum[counter] -
  729. 1) : 0;
  730. /* Allocate missing empty blocks. */
  731. /* if Sh == 0 then we are getting a new root */
  732. amount_needed = (Sh) ? (tb->blknum[h] - 1) : 1;
  733. /*
  734. * Amount_needed = the amount that we need more than the
  735. * amount that we have.
  736. */
  737. if (amount_needed > number_of_freeblk)
  738. amount_needed -= number_of_freeblk;
  739. else /* If we have enough already then there is nothing to do. */
  740. return CARRY_ON;
  741. /*
  742. * No need to check quota - is not allocated for blocks used
  743. * for formatted nodes
  744. */
  745. if (reiserfs_new_form_blocknrs(tb, blocknrs,
  746. amount_needed) == NO_DISK_SPACE)
  747. return NO_DISK_SPACE;
  748. /* for each blocknumber we just got, get a buffer and stick it on FEB */
  749. for (blocknr = blocknrs, counter = 0;
  750. counter < amount_needed; blocknr++, counter++) {
  751. RFALSE(!*blocknr,
  752. "PAP-8135: reiserfs_new_blocknrs failed when got new blocks");
  753. new_bh = sb_getblk(sb, *blocknr);
  754. RFALSE(buffer_dirty(new_bh) ||
  755. buffer_journaled(new_bh) ||
  756. buffer_journal_dirty(new_bh),
  757. "PAP-8140: journaled or dirty buffer %b for the new block",
  758. new_bh);
  759. /* Put empty buffers into the array. */
  760. RFALSE(tb->FEB[tb->cur_blknum],
  761. "PAP-8141: busy slot for new buffer");
  762. set_buffer_journal_new(new_bh);
  763. tb->FEB[tb->cur_blknum++] = new_bh;
  764. }
  765. if (retval == CARRY_ON && FILESYSTEM_CHANGED_TB(tb))
  766. retval = REPEAT_SEARCH;
  767. return retval;
  768. }
  769. /*
  770. * Get free space of the left neighbor, which is stored in the parent
  771. * node of the left neighbor.
  772. */
  773. static int get_lfree(struct tree_balance *tb, int h)
  774. {
  775. struct buffer_head *l, *f;
  776. int order;
  777. if ((f = PATH_H_PPARENT(tb->tb_path, h)) == NULL ||
  778. (l = tb->FL[h]) == NULL)
  779. return 0;
  780. if (f == l)
  781. order = PATH_H_B_ITEM_ORDER(tb->tb_path, h) - 1;
  782. else {
  783. order = B_NR_ITEMS(l);
  784. f = l;
  785. }
  786. return (MAX_CHILD_SIZE(f) - dc_size(B_N_CHILD(f, order)));
  787. }
  788. /*
  789. * Get free space of the right neighbor,
  790. * which is stored in the parent node of the right neighbor.
  791. */
  792. static int get_rfree(struct tree_balance *tb, int h)
  793. {
  794. struct buffer_head *r, *f;
  795. int order;
  796. if ((f = PATH_H_PPARENT(tb->tb_path, h)) == NULL ||
  797. (r = tb->FR[h]) == NULL)
  798. return 0;
  799. if (f == r)
  800. order = PATH_H_B_ITEM_ORDER(tb->tb_path, h) + 1;
  801. else {
  802. order = 0;
  803. f = r;
  804. }
  805. return (MAX_CHILD_SIZE(f) - dc_size(B_N_CHILD(f, order)));
  806. }
  807. /* Check whether left neighbor is in memory. */
  808. static int is_left_neighbor_in_cache(struct tree_balance *tb, int h)
  809. {
  810. struct buffer_head *father, *left;
  811. struct super_block *sb = tb->tb_sb;
  812. b_blocknr_t left_neighbor_blocknr;
  813. int left_neighbor_position;
  814. /* Father of the left neighbor does not exist. */
  815. if (!tb->FL[h])
  816. return 0;
  817. /* Calculate father of the node to be balanced. */
  818. father = PATH_H_PBUFFER(tb->tb_path, h + 1);
  819. RFALSE(!father ||
  820. !B_IS_IN_TREE(father) ||
  821. !B_IS_IN_TREE(tb->FL[h]) ||
  822. !buffer_uptodate(father) ||
  823. !buffer_uptodate(tb->FL[h]),
  824. "vs-8165: F[h] (%b) or FL[h] (%b) is invalid",
  825. father, tb->FL[h]);
  826. /*
  827. * Get position of the pointer to the left neighbor
  828. * into the left father.
  829. */
  830. left_neighbor_position = (father == tb->FL[h]) ?
  831. tb->lkey[h] : B_NR_ITEMS(tb->FL[h]);
  832. /* Get left neighbor block number. */
  833. left_neighbor_blocknr =
  834. B_N_CHILD_NUM(tb->FL[h], left_neighbor_position);
  835. /* Look for the left neighbor in the cache. */
  836. if ((left = sb_find_get_block(sb, left_neighbor_blocknr))) {
  837. RFALSE(buffer_uptodate(left) && !B_IS_IN_TREE(left),
  838. "vs-8170: left neighbor (%b %z) is not in the tree",
  839. left, left);
  840. put_bh(left);
  841. return 1;
  842. }
  843. return 0;
  844. }
  845. #define LEFT_PARENTS 'l'
  846. #define RIGHT_PARENTS 'r'
  847. static void decrement_key(struct cpu_key *key)
  848. {
  849. /* call item specific function for this key */
  850. item_ops[cpu_key_k_type(key)]->decrement_key(key);
  851. }
  852. /*
  853. * Calculate far left/right parent of the left/right neighbor of the
  854. * current node, that is calculate the left/right (FL[h]/FR[h]) neighbor
  855. * of the parent F[h].
  856. * Calculate left/right common parent of the current node and L[h]/R[h].
  857. * Calculate left/right delimiting key position.
  858. * Returns: PATH_INCORRECT - path in the tree is not correct
  859. * SCHEDULE_OCCURRED - schedule occurred while the function worked
  860. * CARRY_ON - schedule didn't occur while the function
  861. * worked
  862. */
  863. static int get_far_parent(struct tree_balance *tb,
  864. int h,
  865. struct buffer_head **pfather,
  866. struct buffer_head **pcom_father, char c_lr_par)
  867. {
  868. struct buffer_head *parent;
  869. INITIALIZE_PATH(s_path_to_neighbor_father);
  870. struct treepath *path = tb->tb_path;
  871. struct cpu_key s_lr_father_key;
  872. int counter,
  873. position = INT_MAX,
  874. first_last_position = 0,
  875. path_offset = PATH_H_PATH_OFFSET(path, h);
  876. /*
  877. * Starting from F[h] go upwards in the tree, and look for the common
  878. * ancestor of F[h], and its neighbor l/r, that should be obtained.
  879. */
  880. counter = path_offset;
  881. RFALSE(counter < FIRST_PATH_ELEMENT_OFFSET,
  882. "PAP-8180: invalid path length");
  883. for (; counter > FIRST_PATH_ELEMENT_OFFSET; counter--) {
  884. /*
  885. * Check whether parent of the current buffer in the path
  886. * is really parent in the tree.
  887. */
  888. if (!B_IS_IN_TREE
  889. (parent = PATH_OFFSET_PBUFFER(path, counter - 1)))
  890. return REPEAT_SEARCH;
  891. /* Check whether position in the parent is correct. */
  892. if ((position =
  893. PATH_OFFSET_POSITION(path,
  894. counter - 1)) >
  895. B_NR_ITEMS(parent))
  896. return REPEAT_SEARCH;
  897. /*
  898. * Check whether parent at the path really points
  899. * to the child.
  900. */
  901. if (B_N_CHILD_NUM(parent, position) !=
  902. PATH_OFFSET_PBUFFER(path, counter)->b_blocknr)
  903. return REPEAT_SEARCH;
  904. /*
  905. * Return delimiting key if position in the parent is not
  906. * equal to first/last one.
  907. */
  908. if (c_lr_par == RIGHT_PARENTS)
  909. first_last_position = B_NR_ITEMS(parent);
  910. if (position != first_last_position) {
  911. *pcom_father = parent;
  912. get_bh(*pcom_father);
  913. /*(*pcom_father = parent)->b_count++; */
  914. break;
  915. }
  916. }
  917. /* if we are in the root of the tree, then there is no common father */
  918. if (counter == FIRST_PATH_ELEMENT_OFFSET) {
  919. /*
  920. * Check whether first buffer in the path is the
  921. * root of the tree.
  922. */
  923. if (PATH_OFFSET_PBUFFER
  924. (tb->tb_path,
  925. FIRST_PATH_ELEMENT_OFFSET)->b_blocknr ==
  926. SB_ROOT_BLOCK(tb->tb_sb)) {
  927. *pfather = *pcom_father = NULL;
  928. return CARRY_ON;
  929. }
  930. return REPEAT_SEARCH;
  931. }
  932. RFALSE(B_LEVEL(*pcom_father) <= DISK_LEAF_NODE_LEVEL,
  933. "PAP-8185: (%b %z) level too small",
  934. *pcom_father, *pcom_father);
  935. /* Check whether the common parent is locked. */
  936. if (buffer_locked(*pcom_father)) {
  937. /* Release the write lock while the buffer is busy */
  938. int depth = reiserfs_write_unlock_nested(tb->tb_sb);
  939. __wait_on_buffer(*pcom_father);
  940. reiserfs_write_lock_nested(tb->tb_sb, depth);
  941. if (FILESYSTEM_CHANGED_TB(tb)) {
  942. brelse(*pcom_father);
  943. return REPEAT_SEARCH;
  944. }
  945. }
  946. /*
  947. * So, we got common parent of the current node and its
  948. * left/right neighbor. Now we are getting the parent of the
  949. * left/right neighbor.
  950. */
  951. /* Form key to get parent of the left/right neighbor. */
  952. le_key2cpu_key(&s_lr_father_key,
  953. internal_key(*pcom_father,
  954. (c_lr_par ==
  955. LEFT_PARENTS) ? (tb->lkey[h - 1] =
  956. position -
  957. 1) : (tb->rkey[h -
  958. 1] =
  959. position)));
  960. if (c_lr_par == LEFT_PARENTS)
  961. decrement_key(&s_lr_father_key);
  962. if (search_by_key
  963. (tb->tb_sb, &s_lr_father_key, &s_path_to_neighbor_father,
  964. h + 1) == IO_ERROR)
  965. /* path is released */
  966. return IO_ERROR;
  967. if (FILESYSTEM_CHANGED_TB(tb)) {
  968. pathrelse(&s_path_to_neighbor_father);
  969. brelse(*pcom_father);
  970. return REPEAT_SEARCH;
  971. }
  972. *pfather = PATH_PLAST_BUFFER(&s_path_to_neighbor_father);
  973. RFALSE(B_LEVEL(*pfather) != h + 1,
  974. "PAP-8190: (%b %z) level too small", *pfather, *pfather);
  975. RFALSE(s_path_to_neighbor_father.path_length <
  976. FIRST_PATH_ELEMENT_OFFSET, "PAP-8192: path length is too small");
  977. s_path_to_neighbor_father.path_length--;
  978. pathrelse(&s_path_to_neighbor_father);
  979. return CARRY_ON;
  980. }
  981. /*
  982. * Get parents of neighbors of node in the path(S[path_offset]) and
  983. * common parents of S[path_offset] and L[path_offset]/R[path_offset]:
  984. * F[path_offset], FL[path_offset], FR[path_offset], CFL[path_offset],
  985. * CFR[path_offset].
  986. * Calculate numbers of left and right delimiting keys position:
  987. * lkey[path_offset], rkey[path_offset].
  988. * Returns: SCHEDULE_OCCURRED - schedule occurred while the function worked
  989. * CARRY_ON - schedule didn't occur while the function worked
  990. */
  991. static int get_parents(struct tree_balance *tb, int h)
  992. {
  993. struct treepath *path = tb->tb_path;
  994. int position,
  995. ret,
  996. path_offset = PATH_H_PATH_OFFSET(tb->tb_path, h);
  997. struct buffer_head *curf, *curcf;
  998. /* Current node is the root of the tree or will be root of the tree */
  999. if (path_offset <= FIRST_PATH_ELEMENT_OFFSET) {
  1000. /*
  1001. * The root can not have parents.
  1002. * Release nodes which previously were obtained as
  1003. * parents of the current node neighbors.
  1004. */
  1005. brelse(tb->FL[h]);
  1006. brelse(tb->CFL[h]);
  1007. brelse(tb->FR[h]);
  1008. brelse(tb->CFR[h]);
  1009. tb->FL[h] = NULL;
  1010. tb->CFL[h] = NULL;
  1011. tb->FR[h] = NULL;
  1012. tb->CFR[h] = NULL;
  1013. return CARRY_ON;
  1014. }
  1015. /* Get parent FL[path_offset] of L[path_offset]. */
  1016. position = PATH_OFFSET_POSITION(path, path_offset - 1);
  1017. if (position) {
  1018. /* Current node is not the first child of its parent. */
  1019. curf = PATH_OFFSET_PBUFFER(path, path_offset - 1);
  1020. curcf = PATH_OFFSET_PBUFFER(path, path_offset - 1);
  1021. get_bh(curf);
  1022. get_bh(curf);
  1023. tb->lkey[h] = position - 1;
  1024. } else {
  1025. /*
  1026. * Calculate current parent of L[path_offset], which is the
  1027. * left neighbor of the current node. Calculate current
  1028. * common parent of L[path_offset] and the current node.
  1029. * Note that CFL[path_offset] not equal FL[path_offset] and
  1030. * CFL[path_offset] not equal F[path_offset].
  1031. * Calculate lkey[path_offset].
  1032. */
  1033. if ((ret = get_far_parent(tb, h + 1, &curf,
  1034. &curcf,
  1035. LEFT_PARENTS)) != CARRY_ON)
  1036. return ret;
  1037. }
  1038. brelse(tb->FL[h]);
  1039. tb->FL[h] = curf; /* New initialization of FL[h]. */
  1040. brelse(tb->CFL[h]);
  1041. tb->CFL[h] = curcf; /* New initialization of CFL[h]. */
  1042. RFALSE((curf && !B_IS_IN_TREE(curf)) ||
  1043. (curcf && !B_IS_IN_TREE(curcf)),
  1044. "PAP-8195: FL (%b) or CFL (%b) is invalid", curf, curcf);
  1045. /* Get parent FR[h] of R[h]. */
  1046. /* Current node is the last child of F[h]. FR[h] != F[h]. */
  1047. if (position == B_NR_ITEMS(PATH_H_PBUFFER(path, h + 1))) {
  1048. /*
  1049. * Calculate current parent of R[h], which is the right
  1050. * neighbor of F[h]. Calculate current common parent of
  1051. * R[h] and current node. Note that CFR[h] not equal
  1052. * FR[path_offset] and CFR[h] not equal F[h].
  1053. */
  1054. if ((ret =
  1055. get_far_parent(tb, h + 1, &curf, &curcf,
  1056. RIGHT_PARENTS)) != CARRY_ON)
  1057. return ret;
  1058. } else {
  1059. /* Current node is not the last child of its parent F[h]. */
  1060. curf = PATH_OFFSET_PBUFFER(path, path_offset - 1);
  1061. curcf = PATH_OFFSET_PBUFFER(path, path_offset - 1);
  1062. get_bh(curf);
  1063. get_bh(curf);
  1064. tb->rkey[h] = position;
  1065. }
  1066. brelse(tb->FR[h]);
  1067. /* New initialization of FR[path_offset]. */
  1068. tb->FR[h] = curf;
  1069. brelse(tb->CFR[h]);
  1070. /* New initialization of CFR[path_offset]. */
  1071. tb->CFR[h] = curcf;
  1072. RFALSE((curf && !B_IS_IN_TREE(curf)) ||
  1073. (curcf && !B_IS_IN_TREE(curcf)),
  1074. "PAP-8205: FR (%b) or CFR (%b) is invalid", curf, curcf);
  1075. return CARRY_ON;
  1076. }
  1077. /*
  1078. * it is possible to remove node as result of shiftings to
  1079. * neighbors even when we insert or paste item.
  1080. */
  1081. static inline int can_node_be_removed(int mode, int lfree, int sfree, int rfree,
  1082. struct tree_balance *tb, int h)
  1083. {
  1084. struct buffer_head *Sh = PATH_H_PBUFFER(tb->tb_path, h);
  1085. int levbytes = tb->insert_size[h];
  1086. struct item_head *ih;
  1087. struct reiserfs_key *r_key = NULL;
  1088. ih = item_head(Sh, 0);
  1089. if (tb->CFR[h])
  1090. r_key = internal_key(tb->CFR[h], tb->rkey[h]);
  1091. if (lfree + rfree + sfree < MAX_CHILD_SIZE(Sh) + levbytes
  1092. /* shifting may merge items which might save space */
  1093. -
  1094. ((!h
  1095. && op_is_left_mergeable(&ih->ih_key, Sh->b_size)) ? IH_SIZE : 0)
  1096. -
  1097. ((!h && r_key
  1098. && op_is_left_mergeable(r_key, Sh->b_size)) ? IH_SIZE : 0)
  1099. + ((h) ? KEY_SIZE : 0)) {
  1100. /* node can not be removed */
  1101. if (sfree >= levbytes) {
  1102. /* new item fits into node S[h] without any shifting */
  1103. if (!h)
  1104. tb->s0num =
  1105. B_NR_ITEMS(Sh) +
  1106. ((mode == M_INSERT) ? 1 : 0);
  1107. set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
  1108. return NO_BALANCING_NEEDED;
  1109. }
  1110. }
  1111. PROC_INFO_INC(tb->tb_sb, can_node_be_removed[h]);
  1112. return !NO_BALANCING_NEEDED;
  1113. }
  1114. /*
  1115. * Check whether current node S[h] is balanced when increasing its size by
  1116. * Inserting or Pasting.
  1117. * Calculate parameters for balancing for current level h.
  1118. * Parameters:
  1119. * tb tree_balance structure;
  1120. * h current level of the node;
  1121. * inum item number in S[h];
  1122. * mode i - insert, p - paste;
  1123. * Returns: 1 - schedule occurred;
  1124. * 0 - balancing for higher levels needed;
  1125. * -1 - no balancing for higher levels needed;
  1126. * -2 - no disk space.
  1127. */
  1128. /* ip means Inserting or Pasting */
  1129. static int ip_check_balance(struct tree_balance *tb, int h)
  1130. {
  1131. struct virtual_node *vn = tb->tb_vn;
  1132. /*
  1133. * Number of bytes that must be inserted into (value is negative
  1134. * if bytes are deleted) buffer which contains node being balanced.
  1135. * The mnemonic is that the attempted change in node space used
  1136. * level is levbytes bytes.
  1137. */
  1138. int levbytes;
  1139. int ret;
  1140. int lfree, sfree, rfree /* free space in L, S and R */ ;
  1141. /*
  1142. * nver is short for number of vertixes, and lnver is the number if
  1143. * we shift to the left, rnver is the number if we shift to the
  1144. * right, and lrnver is the number if we shift in both directions.
  1145. * The goal is to minimize first the number of vertixes, and second,
  1146. * the number of vertixes whose contents are changed by shifting,
  1147. * and third the number of uncached vertixes whose contents are
  1148. * changed by shifting and must be read from disk.
  1149. */
  1150. int nver, lnver, rnver, lrnver;
  1151. /*
  1152. * used at leaf level only, S0 = S[0] is the node being balanced,
  1153. * sInum [ I = 0,1,2 ] is the number of items that will
  1154. * remain in node SI after balancing. S1 and S2 are new
  1155. * nodes that might be created.
  1156. */
  1157. /*
  1158. * we perform 8 calls to get_num_ver(). For each call we
  1159. * calculate five parameters. where 4th parameter is s1bytes
  1160. * and 5th - s2bytes
  1161. *
  1162. * s0num, s1num, s2num for 8 cases
  1163. * 0,1 - do not shift and do not shift but bottle
  1164. * 2 - shift only whole item to left
  1165. * 3 - shift to left and bottle as much as possible
  1166. * 4,5 - shift to right (whole items and as much as possible
  1167. * 6,7 - shift to both directions (whole items and as much as possible)
  1168. */
  1169. short snum012[40] = { 0, };
  1170. /* Sh is the node whose balance is currently being checked */
  1171. struct buffer_head *Sh;
  1172. Sh = PATH_H_PBUFFER(tb->tb_path, h);
  1173. levbytes = tb->insert_size[h];
  1174. /* Calculate balance parameters for creating new root. */
  1175. if (!Sh) {
  1176. if (!h)
  1177. reiserfs_panic(tb->tb_sb, "vs-8210",
  1178. "S[0] can not be 0");
  1179. switch (ret = get_empty_nodes(tb, h)) {
  1180. /* no balancing for higher levels needed */
  1181. case CARRY_ON:
  1182. set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
  1183. return NO_BALANCING_NEEDED;
  1184. case NO_DISK_SPACE:
  1185. case REPEAT_SEARCH:
  1186. return ret;
  1187. default:
  1188. reiserfs_panic(tb->tb_sb, "vs-8215", "incorrect "
  1189. "return value of get_empty_nodes");
  1190. }
  1191. }
  1192. /* get parents of S[h] neighbors. */
  1193. ret = get_parents(tb, h);
  1194. if (ret != CARRY_ON)
  1195. return ret;
  1196. sfree = B_FREE_SPACE(Sh);
  1197. /* get free space of neighbors */
  1198. rfree = get_rfree(tb, h);
  1199. lfree = get_lfree(tb, h);
  1200. /* and new item fits into node S[h] without any shifting */
  1201. if (can_node_be_removed(vn->vn_mode, lfree, sfree, rfree, tb, h) ==
  1202. NO_BALANCING_NEEDED)
  1203. return NO_BALANCING_NEEDED;
  1204. create_virtual_node(tb, h);
  1205. /*
  1206. * determine maximal number of items we can shift to the left
  1207. * neighbor (in tb structure) and the maximal number of bytes
  1208. * that can flow to the left neighbor from the left most liquid
  1209. * item that cannot be shifted from S[0] entirely (returned value)
  1210. */
  1211. check_left(tb, h, lfree);
  1212. /*
  1213. * determine maximal number of items we can shift to the right
  1214. * neighbor (in tb structure) and the maximal number of bytes
  1215. * that can flow to the right neighbor from the right most liquid
  1216. * item that cannot be shifted from S[0] entirely (returned value)
  1217. */
  1218. check_right(tb, h, rfree);
  1219. /*
  1220. * all contents of internal node S[h] can be moved into its
  1221. * neighbors, S[h] will be removed after balancing
  1222. */
  1223. if (h && (tb->rnum[h] + tb->lnum[h] >= vn->vn_nr_item + 1)) {
  1224. int to_r;
  1225. /*
  1226. * Since we are working on internal nodes, and our internal
  1227. * nodes have fixed size entries, then we can balance by the
  1228. * number of items rather than the space they consume. In this
  1229. * routine we set the left node equal to the right node,
  1230. * allowing a difference of less than or equal to 1 child
  1231. * pointer.
  1232. */
  1233. to_r =
  1234. ((MAX_NR_KEY(Sh) << 1) + 2 - tb->lnum[h] - tb->rnum[h] +
  1235. vn->vn_nr_item + 1) / 2 - (MAX_NR_KEY(Sh) + 1 -
  1236. tb->rnum[h]);
  1237. set_parameters(tb, h, vn->vn_nr_item + 1 - to_r, to_r, 0, NULL,
  1238. -1, -1);
  1239. return CARRY_ON;
  1240. }
  1241. /*
  1242. * this checks balance condition, that any two neighboring nodes
  1243. * can not fit in one node
  1244. */
  1245. RFALSE(h &&
  1246. (tb->lnum[h] >= vn->vn_nr_item + 1 ||
  1247. tb->rnum[h] >= vn->vn_nr_item + 1),
  1248. "vs-8220: tree is not balanced on internal level");
  1249. RFALSE(!h && ((tb->lnum[h] >= vn->vn_nr_item && (tb->lbytes == -1)) ||
  1250. (tb->rnum[h] >= vn->vn_nr_item && (tb->rbytes == -1))),
  1251. "vs-8225: tree is not balanced on leaf level");
  1252. /*
  1253. * all contents of S[0] can be moved into its neighbors
  1254. * S[0] will be removed after balancing.
  1255. */
  1256. if (!h && is_leaf_removable(tb))
  1257. return CARRY_ON;
  1258. /*
  1259. * why do we perform this check here rather than earlier??
  1260. * Answer: we can win 1 node in some cases above. Moreover we
  1261. * checked it above, when we checked, that S[0] is not removable
  1262. * in principle
  1263. */
  1264. /* new item fits into node S[h] without any shifting */
  1265. if (sfree >= levbytes) {
  1266. if (!h)
  1267. tb->s0num = vn->vn_nr_item;
  1268. set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
  1269. return NO_BALANCING_NEEDED;
  1270. }
  1271. {
  1272. int lpar, rpar, nset, lset, rset, lrset;
  1273. /* regular overflowing of the node */
  1274. /*
  1275. * get_num_ver works in 2 modes (FLOW & NO_FLOW)
  1276. * lpar, rpar - number of items we can shift to left/right
  1277. * neighbor (including splitting item)
  1278. * nset, lset, rset, lrset - shows, whether flowing items
  1279. * give better packing
  1280. */
  1281. #define FLOW 1
  1282. #define NO_FLOW 0 /* do not any splitting */
  1283. /* we choose one of the following */
  1284. #define NOTHING_SHIFT_NO_FLOW 0
  1285. #define NOTHING_SHIFT_FLOW 5
  1286. #define LEFT_SHIFT_NO_FLOW 10
  1287. #define LEFT_SHIFT_FLOW 15
  1288. #define RIGHT_SHIFT_NO_FLOW 20
  1289. #define RIGHT_SHIFT_FLOW 25
  1290. #define LR_SHIFT_NO_FLOW 30
  1291. #define LR_SHIFT_FLOW 35
  1292. lpar = tb->lnum[h];
  1293. rpar = tb->rnum[h];
  1294. /*
  1295. * calculate number of blocks S[h] must be split into when
  1296. * nothing is shifted to the neighbors, as well as number of
  1297. * items in each part of the split node (s012 numbers),
  1298. * and number of bytes (s1bytes) of the shared drop which
  1299. * flow to S1 if any
  1300. */
  1301. nset = NOTHING_SHIFT_NO_FLOW;
  1302. nver = get_num_ver(vn->vn_mode, tb, h,
  1303. 0, -1, h ? vn->vn_nr_item : 0, -1,
  1304. snum012, NO_FLOW);
  1305. if (!h) {
  1306. int nver1;
  1307. /*
  1308. * note, that in this case we try to bottle
  1309. * between S[0] and S1 (S1 - the first new node)
  1310. */
  1311. nver1 = get_num_ver(vn->vn_mode, tb, h,
  1312. 0, -1, 0, -1,
  1313. snum012 + NOTHING_SHIFT_FLOW, FLOW);
  1314. if (nver > nver1)
  1315. nset = NOTHING_SHIFT_FLOW, nver = nver1;
  1316. }
  1317. /*
  1318. * calculate number of blocks S[h] must be split into when
  1319. * l_shift_num first items and l_shift_bytes of the right
  1320. * most liquid item to be shifted are shifted to the left
  1321. * neighbor, as well as number of items in each part of the
  1322. * splitted node (s012 numbers), and number of bytes
  1323. * (s1bytes) of the shared drop which flow to S1 if any
  1324. */
  1325. lset = LEFT_SHIFT_NO_FLOW;
  1326. lnver = get_num_ver(vn->vn_mode, tb, h,
  1327. lpar - ((h || tb->lbytes == -1) ? 0 : 1),
  1328. -1, h ? vn->vn_nr_item : 0, -1,
  1329. snum012 + LEFT_SHIFT_NO_FLOW, NO_FLOW);
  1330. if (!h) {
  1331. int lnver1;
  1332. lnver1 = get_num_ver(vn->vn_mode, tb, h,
  1333. lpar -
  1334. ((tb->lbytes != -1) ? 1 : 0),
  1335. tb->lbytes, 0, -1,
  1336. snum012 + LEFT_SHIFT_FLOW, FLOW);
  1337. if (lnver > lnver1)
  1338. lset = LEFT_SHIFT_FLOW, lnver = lnver1;
  1339. }
  1340. /*
  1341. * calculate number of blocks S[h] must be split into when
  1342. * r_shift_num first items and r_shift_bytes of the left most
  1343. * liquid item to be shifted are shifted to the right neighbor,
  1344. * as well as number of items in each part of the splitted
  1345. * node (s012 numbers), and number of bytes (s1bytes) of the
  1346. * shared drop which flow to S1 if any
  1347. */
  1348. rset = RIGHT_SHIFT_NO_FLOW;
  1349. rnver = get_num_ver(vn->vn_mode, tb, h,
  1350. 0, -1,
  1351. h ? (vn->vn_nr_item - rpar) : (rpar -
  1352. ((tb->
  1353. rbytes !=
  1354. -1) ? 1 :
  1355. 0)), -1,
  1356. snum012 + RIGHT_SHIFT_NO_FLOW, NO_FLOW);
  1357. if (!h) {
  1358. int rnver1;
  1359. rnver1 = get_num_ver(vn->vn_mode, tb, h,
  1360. 0, -1,
  1361. (rpar -
  1362. ((tb->rbytes != -1) ? 1 : 0)),
  1363. tb->rbytes,
  1364. snum012 + RIGHT_SHIFT_FLOW, FLOW);
  1365. if (rnver > rnver1)
  1366. rset = RIGHT_SHIFT_FLOW, rnver = rnver1;
  1367. }
  1368. /*
  1369. * calculate number of blocks S[h] must be split into when
  1370. * items are shifted in both directions, as well as number
  1371. * of items in each part of the splitted node (s012 numbers),
  1372. * and number of bytes (s1bytes) of the shared drop which
  1373. * flow to S1 if any
  1374. */
  1375. lrset = LR_SHIFT_NO_FLOW;
  1376. lrnver = get_num_ver(vn->vn_mode, tb, h,
  1377. lpar - ((h || tb->lbytes == -1) ? 0 : 1),
  1378. -1,
  1379. h ? (vn->vn_nr_item - rpar) : (rpar -
  1380. ((tb->
  1381. rbytes !=
  1382. -1) ? 1 :
  1383. 0)), -1,
  1384. snum012 + LR_SHIFT_NO_FLOW, NO_FLOW);
  1385. if (!h) {
  1386. int lrnver1;
  1387. lrnver1 = get_num_ver(vn->vn_mode, tb, h,
  1388. lpar -
  1389. ((tb->lbytes != -1) ? 1 : 0),
  1390. tb->lbytes,
  1391. (rpar -
  1392. ((tb->rbytes != -1) ? 1 : 0)),
  1393. tb->rbytes,
  1394. snum012 + LR_SHIFT_FLOW, FLOW);
  1395. if (lrnver > lrnver1)
  1396. lrset = LR_SHIFT_FLOW, lrnver = lrnver1;
  1397. }
  1398. /*
  1399. * Our general shifting strategy is:
  1400. * 1) to minimized number of new nodes;
  1401. * 2) to minimized number of neighbors involved in shifting;
  1402. * 3) to minimized number of disk reads;
  1403. */
  1404. /* we can win TWO or ONE nodes by shifting in both directions */
  1405. if (lrnver < lnver && lrnver < rnver) {
  1406. RFALSE(h &&
  1407. (tb->lnum[h] != 1 ||
  1408. tb->rnum[h] != 1 ||
  1409. lrnver != 1 || rnver != 2 || lnver != 2
  1410. || h != 1), "vs-8230: bad h");
  1411. if (lrset == LR_SHIFT_FLOW)
  1412. set_parameters(tb, h, tb->lnum[h], tb->rnum[h],
  1413. lrnver, snum012 + lrset,
  1414. tb->lbytes, tb->rbytes);
  1415. else
  1416. set_parameters(tb, h,
  1417. tb->lnum[h] -
  1418. ((tb->lbytes == -1) ? 0 : 1),
  1419. tb->rnum[h] -
  1420. ((tb->rbytes == -1) ? 0 : 1),
  1421. lrnver, snum012 + lrset, -1, -1);
  1422. return CARRY_ON;
  1423. }
  1424. /*
  1425. * if shifting doesn't lead to better packing
  1426. * then don't shift
  1427. */
  1428. if (nver == lrnver) {
  1429. set_parameters(tb, h, 0, 0, nver, snum012 + nset, -1,
  1430. -1);
  1431. return CARRY_ON;
  1432. }
  1433. /*
  1434. * now we know that for better packing shifting in only one
  1435. * direction either to the left or to the right is required
  1436. */
  1437. /*
  1438. * if shifting to the left is better than
  1439. * shifting to the right
  1440. */
  1441. if (lnver < rnver) {
  1442. SET_PAR_SHIFT_LEFT;
  1443. return CARRY_ON;
  1444. }
  1445. /*
  1446. * if shifting to the right is better than
  1447. * shifting to the left
  1448. */
  1449. if (lnver > rnver) {
  1450. SET_PAR_SHIFT_RIGHT;
  1451. return CARRY_ON;
  1452. }
  1453. /*
  1454. * now shifting in either direction gives the same number
  1455. * of nodes and we can make use of the cached neighbors
  1456. */
  1457. if (is_left_neighbor_in_cache(tb, h)) {
  1458. SET_PAR_SHIFT_LEFT;
  1459. return CARRY_ON;
  1460. }
  1461. /*
  1462. * shift to the right independently on whether the
  1463. * right neighbor in cache or not
  1464. */
  1465. SET_PAR_SHIFT_RIGHT;
  1466. return CARRY_ON;
  1467. }
  1468. }
  1469. /*
  1470. * Check whether current node S[h] is balanced when Decreasing its size by
  1471. * Deleting or Cutting for INTERNAL node of S+tree.
  1472. * Calculate parameters for balancing for current level h.
  1473. * Parameters:
  1474. * tb tree_balance structure;
  1475. * h current level of the node;
  1476. * inum item number in S[h];
  1477. * mode i - insert, p - paste;
  1478. * Returns: 1 - schedule occurred;
  1479. * 0 - balancing for higher levels needed;
  1480. * -1 - no balancing for higher levels needed;
  1481. * -2 - no disk space.
  1482. *
  1483. * Note: Items of internal nodes have fixed size, so the balance condition for
  1484. * the internal part of S+tree is as for the B-trees.
  1485. */
  1486. static int dc_check_balance_internal(struct tree_balance *tb, int h)
  1487. {
  1488. struct virtual_node *vn = tb->tb_vn;
  1489. /*
  1490. * Sh is the node whose balance is currently being checked,
  1491. * and Fh is its father.
  1492. */
  1493. struct buffer_head *Sh, *Fh;
  1494. int ret;
  1495. int lfree, rfree /* free space in L and R */ ;
  1496. Sh = PATH_H_PBUFFER(tb->tb_path, h);
  1497. Fh = PATH_H_PPARENT(tb->tb_path, h);
  1498. /*
  1499. * using tb->insert_size[h], which is negative in this case,
  1500. * create_virtual_node calculates:
  1501. * new_nr_item = number of items node would have if operation is
  1502. * performed without balancing (new_nr_item);
  1503. */
  1504. create_virtual_node(tb, h);
  1505. if (!Fh) { /* S[h] is the root. */
  1506. /* no balancing for higher levels needed */
  1507. if (vn->vn_nr_item > 0) {
  1508. set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
  1509. return NO_BALANCING_NEEDED;
  1510. }
  1511. /*
  1512. * new_nr_item == 0.
  1513. * Current root will be deleted resulting in
  1514. * decrementing the tree height.
  1515. */
  1516. set_parameters(tb, h, 0, 0, 0, NULL, -1, -1);
  1517. return CARRY_ON;
  1518. }
  1519. if ((ret = get_parents(tb, h)) != CARRY_ON)
  1520. return ret;
  1521. /* get free space of neighbors */
  1522. rfree = get_rfree(tb, h);
  1523. lfree = get_lfree(tb, h);
  1524. /* determine maximal number of items we can fit into neighbors */
  1525. check_left(tb, h, lfree);
  1526. check_right(tb, h, rfree);
  1527. /*
  1528. * Balance condition for the internal node is valid.
  1529. * In this case we balance only if it leads to better packing.
  1530. */
  1531. if (vn->vn_nr_item >= MIN_NR_KEY(Sh)) {
  1532. /*
  1533. * Here we join S[h] with one of its neighbors,
  1534. * which is impossible with greater values of new_nr_item.
  1535. */
  1536. if (vn->vn_nr_item == MIN_NR_KEY(Sh)) {
  1537. /* All contents of S[h] can be moved to L[h]. */
  1538. if (tb->lnum[h] >= vn->vn_nr_item + 1) {
  1539. int n;
  1540. int order_L;
  1541. order_L =
  1542. ((n =
  1543. PATH_H_B_ITEM_ORDER(tb->tb_path,
  1544. h)) ==
  1545. 0) ? B_NR_ITEMS(tb->FL[h]) : n - 1;
  1546. n = dc_size(B_N_CHILD(tb->FL[h], order_L)) /
  1547. (DC_SIZE + KEY_SIZE);
  1548. set_parameters(tb, h, -n - 1, 0, 0, NULL, -1,
  1549. -1);
  1550. return CARRY_ON;
  1551. }
  1552. /* All contents of S[h] can be moved to R[h]. */
  1553. if (tb->rnum[h] >= vn->vn_nr_item + 1) {
  1554. int n;
  1555. int order_R;
  1556. order_R =
  1557. ((n =
  1558. PATH_H_B_ITEM_ORDER(tb->tb_path,
  1559. h)) ==
  1560. B_NR_ITEMS(Fh)) ? 0 : n + 1;
  1561. n = dc_size(B_N_CHILD(tb->FR[h], order_R)) /
  1562. (DC_SIZE + KEY_SIZE);
  1563. set_parameters(tb, h, 0, -n - 1, 0, NULL, -1,
  1564. -1);
  1565. return CARRY_ON;
  1566. }
  1567. }
  1568. /*
  1569. * All contents of S[h] can be moved to the neighbors
  1570. * (L[h] & R[h]).
  1571. */
  1572. if (tb->rnum[h] + tb->lnum[h] >= vn->vn_nr_item + 1) {
  1573. int to_r;
  1574. to_r =
  1575. ((MAX_NR_KEY(Sh) << 1) + 2 - tb->lnum[h] -
  1576. tb->rnum[h] + vn->vn_nr_item + 1) / 2 -
  1577. (MAX_NR_KEY(Sh) + 1 - tb->rnum[h]);
  1578. set_parameters(tb, h, vn->vn_nr_item + 1 - to_r, to_r,
  1579. 0, NULL, -1, -1);
  1580. return CARRY_ON;
  1581. }
  1582. /* Balancing does not lead to better packing. */
  1583. set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
  1584. return NO_BALANCING_NEEDED;
  1585. }
  1586. /*
  1587. * Current node contain insufficient number of items.
  1588. * Balancing is required.
  1589. */
  1590. /* Check whether we can merge S[h] with left neighbor. */
  1591. if (tb->lnum[h] >= vn->vn_nr_item + 1)
  1592. if (is_left_neighbor_in_cache(tb, h)
  1593. || tb->rnum[h] < vn->vn_nr_item + 1 || !tb->FR[h]) {
  1594. int n;
  1595. int order_L;
  1596. order_L =
  1597. ((n =
  1598. PATH_H_B_ITEM_ORDER(tb->tb_path,
  1599. h)) ==
  1600. 0) ? B_NR_ITEMS(tb->FL[h]) : n - 1;
  1601. n = dc_size(B_N_CHILD(tb->FL[h], order_L)) / (DC_SIZE +
  1602. KEY_SIZE);
  1603. set_parameters(tb, h, -n - 1, 0, 0, NULL, -1, -1);
  1604. return CARRY_ON;
  1605. }
  1606. /* Check whether we can merge S[h] with right neighbor. */
  1607. if (tb->rnum[h] >= vn->vn_nr_item + 1) {
  1608. int n;
  1609. int order_R;
  1610. order_R =
  1611. ((n =
  1612. PATH_H_B_ITEM_ORDER(tb->tb_path,
  1613. h)) == B_NR_ITEMS(Fh)) ? 0 : (n + 1);
  1614. n = dc_size(B_N_CHILD(tb->FR[h], order_R)) / (DC_SIZE +
  1615. KEY_SIZE);
  1616. set_parameters(tb, h, 0, -n - 1, 0, NULL, -1, -1);
  1617. return CARRY_ON;
  1618. }
  1619. /* All contents of S[h] can be moved to the neighbors (L[h] & R[h]). */
  1620. if (tb->rnum[h] + tb->lnum[h] >= vn->vn_nr_item + 1) {
  1621. int to_r;
  1622. to_r =
  1623. ((MAX_NR_KEY(Sh) << 1) + 2 - tb->lnum[h] - tb->rnum[h] +
  1624. vn->vn_nr_item + 1) / 2 - (MAX_NR_KEY(Sh) + 1 -
  1625. tb->rnum[h]);
  1626. set_parameters(tb, h, vn->vn_nr_item + 1 - to_r, to_r, 0, NULL,
  1627. -1, -1);
  1628. return CARRY_ON;
  1629. }
  1630. /* For internal nodes try to borrow item from a neighbor */
  1631. RFALSE(!tb->FL[h] && !tb->FR[h], "vs-8235: trying to borrow for root");
  1632. /* Borrow one or two items from caching neighbor */
  1633. if (is_left_neighbor_in_cache(tb, h) || !tb->FR[h]) {
  1634. int from_l;
  1635. from_l =
  1636. (MAX_NR_KEY(Sh) + 1 - tb->lnum[h] + vn->vn_nr_item +
  1637. 1) / 2 - (vn->vn_nr_item + 1);
  1638. set_parameters(tb, h, -from_l, 0, 1, NULL, -1, -1);
  1639. return CARRY_ON;
  1640. }
  1641. set_parameters(tb, h, 0,
  1642. -((MAX_NR_KEY(Sh) + 1 - tb->rnum[h] + vn->vn_nr_item +
  1643. 1) / 2 - (vn->vn_nr_item + 1)), 1, NULL, -1, -1);
  1644. return CARRY_ON;
  1645. }
  1646. /*
  1647. * Check whether current node S[h] is balanced when Decreasing its size by
  1648. * Deleting or Truncating for LEAF node of S+tree.
  1649. * Calculate parameters for balancing for current level h.
  1650. * Parameters:
  1651. * tb tree_balance structure;
  1652. * h current level of the node;
  1653. * inum item number in S[h];
  1654. * mode i - insert, p - paste;
  1655. * Returns: 1 - schedule occurred;
  1656. * 0 - balancing for higher levels needed;
  1657. * -1 - no balancing for higher levels needed;
  1658. * -2 - no disk space.
  1659. */
  1660. static int dc_check_balance_leaf(struct tree_balance *tb, int h)
  1661. {
  1662. struct virtual_node *vn = tb->tb_vn;
  1663. /*
  1664. * Number of bytes that must be deleted from
  1665. * (value is negative if bytes are deleted) buffer which
  1666. * contains node being balanced. The mnemonic is that the
  1667. * attempted change in node space used level is levbytes bytes.
  1668. */
  1669. int levbytes;
  1670. /* the maximal item size */
  1671. int maxsize, ret;
  1672. /*
  1673. * S0 is the node whose balance is currently being checked,
  1674. * and F0 is its father.
  1675. */
  1676. struct buffer_head *S0, *F0;
  1677. int lfree, rfree /* free space in L and R */ ;
  1678. S0 = PATH_H_PBUFFER(tb->tb_path, 0);
  1679. F0 = PATH_H_PPARENT(tb->tb_path, 0);
  1680. levbytes = tb->insert_size[h];
  1681. maxsize = MAX_CHILD_SIZE(S0); /* maximal possible size of an item */
  1682. if (!F0) { /* S[0] is the root now. */
  1683. RFALSE(-levbytes >= maxsize - B_FREE_SPACE(S0),
  1684. "vs-8240: attempt to create empty buffer tree");
  1685. set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
  1686. return NO_BALANCING_NEEDED;
  1687. }
  1688. if ((ret = get_parents(tb, h)) != CARRY_ON)
  1689. return ret;
  1690. /* get free space of neighbors */
  1691. rfree = get_rfree(tb, h);
  1692. lfree = get_lfree(tb, h);
  1693. create_virtual_node(tb, h);
  1694. /* if 3 leaves can be merge to one, set parameters and return */
  1695. if (are_leaves_removable(tb, lfree, rfree))
  1696. return CARRY_ON;
  1697. /*
  1698. * determine maximal number of items we can shift to the left/right
  1699. * neighbor and the maximal number of bytes that can flow to the
  1700. * left/right neighbor from the left/right most liquid item that
  1701. * cannot be shifted from S[0] entirely
  1702. */
  1703. check_left(tb, h, lfree);
  1704. check_right(tb, h, rfree);
  1705. /* check whether we can merge S with left neighbor. */
  1706. if (tb->lnum[0] >= vn->vn_nr_item && tb->lbytes == -1)
  1707. if (is_left_neighbor_in_cache(tb, h) || ((tb->rnum[0] - ((tb->rbytes == -1) ? 0 : 1)) < vn->vn_nr_item) || /* S can not be merged with R */
  1708. !tb->FR[h]) {
  1709. RFALSE(!tb->FL[h],
  1710. "vs-8245: dc_check_balance_leaf: FL[h] must exist");
  1711. /* set parameter to merge S[0] with its left neighbor */
  1712. set_parameters(tb, h, -1, 0, 0, NULL, -1, -1);
  1713. return CARRY_ON;
  1714. }
  1715. /* check whether we can merge S[0] with right neighbor. */
  1716. if (tb->rnum[0] >= vn->vn_nr_item && tb->rbytes == -1) {
  1717. set_parameters(tb, h, 0, -1, 0, NULL, -1, -1);
  1718. return CARRY_ON;
  1719. }
  1720. /*
  1721. * All contents of S[0] can be moved to the neighbors (L[0] & R[0]).
  1722. * Set parameters and return
  1723. */
  1724. if (is_leaf_removable(tb))
  1725. return CARRY_ON;
  1726. /* Balancing is not required. */
  1727. tb->s0num = vn->vn_nr_item;
  1728. set_parameters(tb, h, 0, 0, 1, NULL, -1, -1);
  1729. return NO_BALANCING_NEEDED;
  1730. }
  1731. /*
  1732. * Check whether current node S[h] is balanced when Decreasing its size by
  1733. * Deleting or Cutting.
  1734. * Calculate parameters for balancing for current level h.
  1735. * Parameters:
  1736. * tb tree_balance structure;
  1737. * h current level of the node;
  1738. * inum item number in S[h];
  1739. * mode d - delete, c - cut.
  1740. * Returns: 1 - schedule occurred;
  1741. * 0 - balancing for higher levels needed;
  1742. * -1 - no balancing for higher levels needed;
  1743. * -2 - no disk space.
  1744. */
  1745. static int dc_check_balance(struct tree_balance *tb, int h)
  1746. {
  1747. RFALSE(!(PATH_H_PBUFFER(tb->tb_path, h)),
  1748. "vs-8250: S is not initialized");
  1749. if (h)
  1750. return dc_check_balance_internal(tb, h);
  1751. else
  1752. return dc_check_balance_leaf(tb, h);
  1753. }
  1754. /*
  1755. * Check whether current node S[h] is balanced.
  1756. * Calculate parameters for balancing for current level h.
  1757. * Parameters:
  1758. *
  1759. * tb tree_balance structure:
  1760. *
  1761. * tb is a large structure that must be read about in the header
  1762. * file at the same time as this procedure if the reader is
  1763. * to successfully understand this procedure
  1764. *
  1765. * h current level of the node;
  1766. * inum item number in S[h];
  1767. * mode i - insert, p - paste, d - delete, c - cut.
  1768. * Returns: 1 - schedule occurred;
  1769. * 0 - balancing for higher levels needed;
  1770. * -1 - no balancing for higher levels needed;
  1771. * -2 - no disk space.
  1772. */
  1773. static int check_balance(int mode,
  1774. struct tree_balance *tb,
  1775. int h,
  1776. int inum,
  1777. int pos_in_item,
  1778. struct item_head *ins_ih, const void *data)
  1779. {
  1780. struct virtual_node *vn;
  1781. vn = tb->tb_vn = (struct virtual_node *)(tb->vn_buf);
  1782. vn->vn_free_ptr = (char *)(tb->tb_vn + 1);
  1783. vn->vn_mode = mode;
  1784. vn->vn_affected_item_num = inum;
  1785. vn->vn_pos_in_item = pos_in_item;
  1786. vn->vn_ins_ih = ins_ih;
  1787. vn->vn_data = data;
  1788. RFALSE(mode == M_INSERT && !vn->vn_ins_ih,
  1789. "vs-8255: ins_ih can not be 0 in insert mode");
  1790. /* Calculate balance parameters when size of node is increasing. */
  1791. if (tb->insert_size[h] > 0)
  1792. return ip_check_balance(tb, h);
  1793. /* Calculate balance parameters when size of node is decreasing. */
  1794. return dc_check_balance(tb, h);
  1795. }
  1796. /* Check whether parent at the path is the really parent of the current node.*/
  1797. static int get_direct_parent(struct tree_balance *tb, int h)
  1798. {
  1799. struct buffer_head *bh;
  1800. struct treepath *path = tb->tb_path;
  1801. int position,
  1802. path_offset = PATH_H_PATH_OFFSET(tb->tb_path, h);
  1803. /* We are in the root or in the new root. */
  1804. if (path_offset <= FIRST_PATH_ELEMENT_OFFSET) {
  1805. RFALSE(path_offset < FIRST_PATH_ELEMENT_OFFSET - 1,
  1806. "PAP-8260: invalid offset in the path");
  1807. if (PATH_OFFSET_PBUFFER(path, FIRST_PATH_ELEMENT_OFFSET)->
  1808. b_blocknr == SB_ROOT_BLOCK(tb->tb_sb)) {
  1809. /* Root is not changed. */
  1810. PATH_OFFSET_PBUFFER(path, path_offset - 1) = NULL;
  1811. PATH_OFFSET_POSITION(path, path_offset - 1) = 0;
  1812. return CARRY_ON;
  1813. }
  1814. /* Root is changed and we must recalculate the path. */
  1815. return REPEAT_SEARCH;
  1816. }
  1817. /* Parent in the path is not in the tree. */
  1818. if (!B_IS_IN_TREE
  1819. (bh = PATH_OFFSET_PBUFFER(path, path_offset - 1)))
  1820. return REPEAT_SEARCH;
  1821. if ((position =
  1822. PATH_OFFSET_POSITION(path,
  1823. path_offset - 1)) > B_NR_ITEMS(bh))
  1824. return REPEAT_SEARCH;
  1825. /* Parent in the path is not parent of the current node in the tree. */
  1826. if (B_N_CHILD_NUM(bh, position) !=
  1827. PATH_OFFSET_PBUFFER(path, path_offset)->b_blocknr)
  1828. return REPEAT_SEARCH;
  1829. if (buffer_locked(bh)) {
  1830. int depth = reiserfs_write_unlock_nested(tb->tb_sb);
  1831. __wait_on_buffer(bh);
  1832. reiserfs_write_lock_nested(tb->tb_sb, depth);
  1833. if (FILESYSTEM_CHANGED_TB(tb))
  1834. return REPEAT_SEARCH;
  1835. }
  1836. /*
  1837. * Parent in the path is unlocked and really parent
  1838. * of the current node.
  1839. */
  1840. return CARRY_ON;
  1841. }
  1842. /*
  1843. * Using lnum[h] and rnum[h] we should determine what neighbors
  1844. * of S[h] we
  1845. * need in order to balance S[h], and get them if necessary.
  1846. * Returns: SCHEDULE_OCCURRED - schedule occurred while the function worked;
  1847. * CARRY_ON - schedule didn't occur while the function worked;
  1848. */
  1849. static int get_neighbors(struct tree_balance *tb, int h)
  1850. {
  1851. int child_position,
  1852. path_offset = PATH_H_PATH_OFFSET(tb->tb_path, h + 1);
  1853. unsigned long son_number;
  1854. struct super_block *sb = tb->tb_sb;
  1855. struct buffer_head *bh;
  1856. int depth;
  1857. PROC_INFO_INC(sb, get_neighbors[h]);
  1858. if (tb->lnum[h]) {
  1859. /* We need left neighbor to balance S[h]. */
  1860. PROC_INFO_INC(sb, need_l_neighbor[h]);
  1861. bh = PATH_OFFSET_PBUFFER(tb->tb_path, path_offset);
  1862. RFALSE(bh == tb->FL[h] &&
  1863. !PATH_OFFSET_POSITION(tb->tb_path, path_offset),
  1864. "PAP-8270: invalid position in the parent");
  1865. child_position =
  1866. (bh ==
  1867. tb->FL[h]) ? tb->lkey[h] : B_NR_ITEMS(tb->
  1868. FL[h]);
  1869. son_number = B_N_CHILD_NUM(tb->FL[h], child_position);
  1870. depth = reiserfs_write_unlock_nested(tb->tb_sb);
  1871. bh = sb_bread(sb, son_number);
  1872. reiserfs_write_lock_nested(tb->tb_sb, depth);
  1873. if (!bh)
  1874. return IO_ERROR;
  1875. if (FILESYSTEM_CHANGED_TB(tb)) {
  1876. brelse(bh);
  1877. PROC_INFO_INC(sb, get_neighbors_restart[h]);
  1878. return REPEAT_SEARCH;
  1879. }
  1880. RFALSE(!B_IS_IN_TREE(tb->FL[h]) ||
  1881. child_position > B_NR_ITEMS(tb->FL[h]) ||
  1882. B_N_CHILD_NUM(tb->FL[h], child_position) !=
  1883. bh->b_blocknr, "PAP-8275: invalid parent");
  1884. RFALSE(!B_IS_IN_TREE(bh), "PAP-8280: invalid child");
  1885. RFALSE(!h &&
  1886. B_FREE_SPACE(bh) !=
  1887. MAX_CHILD_SIZE(bh) -
  1888. dc_size(B_N_CHILD(tb->FL[0], child_position)),
  1889. "PAP-8290: invalid child size of left neighbor");
  1890. brelse(tb->L[h]);
  1891. tb->L[h] = bh;
  1892. }
  1893. /* We need right neighbor to balance S[path_offset]. */
  1894. if (tb->rnum[h]) {
  1895. PROC_INFO_INC(sb, need_r_neighbor[h]);
  1896. bh = PATH_OFFSET_PBUFFER(tb->tb_path, path_offset);
  1897. RFALSE(bh == tb->FR[h] &&
  1898. PATH_OFFSET_POSITION(tb->tb_path,
  1899. path_offset) >=
  1900. B_NR_ITEMS(bh),
  1901. "PAP-8295: invalid position in the parent");
  1902. child_position =
  1903. (bh == tb->FR[h]) ? tb->rkey[h] + 1 : 0;
  1904. son_number = B_N_CHILD_NUM(tb->FR[h], child_position);
  1905. depth = reiserfs_write_unlock_nested(tb->tb_sb);
  1906. bh = sb_bread(sb, son_number);
  1907. reiserfs_write_lock_nested(tb->tb_sb, depth);
  1908. if (!bh)
  1909. return IO_ERROR;
  1910. if (FILESYSTEM_CHANGED_TB(tb)) {
  1911. brelse(bh);
  1912. PROC_INFO_INC(sb, get_neighbors_restart[h]);
  1913. return REPEAT_SEARCH;
  1914. }
  1915. brelse(tb->R[h]);
  1916. tb->R[h] = bh;
  1917. RFALSE(!h
  1918. && B_FREE_SPACE(bh) !=
  1919. MAX_CHILD_SIZE(bh) -
  1920. dc_size(B_N_CHILD(tb->FR[0], child_position)),
  1921. "PAP-8300: invalid child size of right neighbor (%d != %d - %d)",
  1922. B_FREE_SPACE(bh), MAX_CHILD_SIZE(bh),
  1923. dc_size(B_N_CHILD(tb->FR[0], child_position)));
  1924. }
  1925. return CARRY_ON;
  1926. }
  1927. static int get_virtual_node_size(struct super_block *sb, struct buffer_head *bh)
  1928. {
  1929. int max_num_of_items;
  1930. int max_num_of_entries;
  1931. unsigned long blocksize = sb->s_blocksize;
  1932. #define MIN_NAME_LEN 1
  1933. max_num_of_items = (blocksize - BLKH_SIZE) / (IH_SIZE + MIN_ITEM_LEN);
  1934. max_num_of_entries = (blocksize - BLKH_SIZE - IH_SIZE) /
  1935. (DEH_SIZE + MIN_NAME_LEN);
  1936. return sizeof(struct virtual_node) +
  1937. max(max_num_of_items * sizeof(struct virtual_item),
  1938. sizeof(struct virtual_item) + sizeof(struct direntry_uarea) +
  1939. (max_num_of_entries - 1) * sizeof(__u16));
  1940. }
  1941. /*
  1942. * maybe we should fail balancing we are going to perform when kmalloc
  1943. * fails several times. But now it will loop until kmalloc gets
  1944. * required memory
  1945. */
  1946. static int get_mem_for_virtual_node(struct tree_balance *tb)
  1947. {
  1948. int check_fs = 0;
  1949. int size;
  1950. char *buf;
  1951. size = get_virtual_node_size(tb->tb_sb, PATH_PLAST_BUFFER(tb->tb_path));
  1952. /* we have to allocate more memory for virtual node */
  1953. if (size > tb->vn_buf_size) {
  1954. if (tb->vn_buf) {
  1955. /* free memory allocated before */
  1956. kfree(tb->vn_buf);
  1957. /* this is not needed if kfree is atomic */
  1958. check_fs = 1;
  1959. }
  1960. /* virtual node requires now more memory */
  1961. tb->vn_buf_size = size;
  1962. /* get memory for virtual item */
  1963. buf = kmalloc(size, GFP_ATOMIC | __GFP_NOWARN);
  1964. if (!buf) {
  1965. /*
  1966. * getting memory with GFP_KERNEL priority may involve
  1967. * balancing now (due to indirect_to_direct conversion
  1968. * on dcache shrinking). So, release path and collected
  1969. * resources here
  1970. */
  1971. free_buffers_in_tb(tb);
  1972. buf = kmalloc(size, GFP_NOFS);
  1973. if (!buf) {
  1974. tb->vn_buf_size = 0;
  1975. }
  1976. tb->vn_buf = buf;
  1977. schedule();
  1978. return REPEAT_SEARCH;
  1979. }
  1980. tb->vn_buf = buf;
  1981. }
  1982. if (check_fs && FILESYSTEM_CHANGED_TB(tb))
  1983. return REPEAT_SEARCH;
  1984. return CARRY_ON;
  1985. }
  1986. #ifdef CONFIG_REISERFS_CHECK
  1987. static void tb_buffer_sanity_check(struct super_block *sb,
  1988. struct buffer_head *bh,
  1989. const char *descr, int level)
  1990. {
  1991. if (bh) {
  1992. if (atomic_read(&(bh->b_count)) <= 0)
  1993. reiserfs_panic(sb, "jmacd-1", "negative or zero "
  1994. "reference counter for buffer %s[%d] "
  1995. "(%b)", descr, level, bh);
  1996. if (!buffer_uptodate(bh))
  1997. reiserfs_panic(sb, "jmacd-2", "buffer is not up "
  1998. "to date %s[%d] (%b)",
  1999. descr, level, bh);
  2000. if (!B_IS_IN_TREE(bh))
  2001. reiserfs_panic(sb, "jmacd-3", "buffer is not "
  2002. "in tree %s[%d] (%b)",
  2003. descr, level, bh);
  2004. if (bh->b_bdev != sb->s_bdev)
  2005. reiserfs_panic(sb, "jmacd-4", "buffer has wrong "
  2006. "device %s[%d] (%b)",
  2007. descr, level, bh);
  2008. if (bh->b_size != sb->s_blocksize)
  2009. reiserfs_panic(sb, "jmacd-5", "buffer has wrong "
  2010. "blocksize %s[%d] (%b)",
  2011. descr, level, bh);
  2012. if (bh->b_blocknr > SB_BLOCK_COUNT(sb))
  2013. reiserfs_panic(sb, "jmacd-6", "buffer block "
  2014. "number too high %s[%d] (%b)",
  2015. descr, level, bh);
  2016. }
  2017. }
  2018. #else
  2019. static void tb_buffer_sanity_check(struct super_block *sb,
  2020. struct buffer_head *bh,
  2021. const char *descr, int level)
  2022. {;
  2023. }
  2024. #endif
  2025. static int clear_all_dirty_bits(struct super_block *s, struct buffer_head *bh)
  2026. {
  2027. return reiserfs_prepare_for_journal(s, bh, 0);
  2028. }
  2029. static int wait_tb_buffers_until_unlocked(struct tree_balance *tb)
  2030. {
  2031. struct buffer_head *locked;
  2032. #ifdef CONFIG_REISERFS_CHECK
  2033. int repeat_counter = 0;
  2034. #endif
  2035. int i;
  2036. do {
  2037. locked = NULL;
  2038. for (i = tb->tb_path->path_length;
  2039. !locked && i > ILLEGAL_PATH_ELEMENT_OFFSET; i--) {
  2040. if (PATH_OFFSET_PBUFFER(tb->tb_path, i)) {
  2041. /*
  2042. * if I understand correctly, we can only
  2043. * be sure the last buffer in the path is
  2044. * in the tree --clm
  2045. */
  2046. #ifdef CONFIG_REISERFS_CHECK
  2047. if (PATH_PLAST_BUFFER(tb->tb_path) ==
  2048. PATH_OFFSET_PBUFFER(tb->tb_path, i))
  2049. tb_buffer_sanity_check(tb->tb_sb,
  2050. PATH_OFFSET_PBUFFER
  2051. (tb->tb_path,
  2052. i), "S",
  2053. tb->tb_path->
  2054. path_length - i);
  2055. #endif
  2056. if (!clear_all_dirty_bits(tb->tb_sb,
  2057. PATH_OFFSET_PBUFFER
  2058. (tb->tb_path,
  2059. i))) {
  2060. locked =
  2061. PATH_OFFSET_PBUFFER(tb->tb_path,
  2062. i);
  2063. }
  2064. }
  2065. }
  2066. for (i = 0; !locked && i < MAX_HEIGHT && tb->insert_size[i];
  2067. i++) {
  2068. if (tb->lnum[i]) {
  2069. if (tb->L[i]) {
  2070. tb_buffer_sanity_check(tb->tb_sb,
  2071. tb->L[i],
  2072. "L", i);
  2073. if (!clear_all_dirty_bits
  2074. (tb->tb_sb, tb->L[i]))
  2075. locked = tb->L[i];
  2076. }
  2077. if (!locked && tb->FL[i]) {
  2078. tb_buffer_sanity_check(tb->tb_sb,
  2079. tb->FL[i],
  2080. "FL", i);
  2081. if (!clear_all_dirty_bits
  2082. (tb->tb_sb, tb->FL[i]))
  2083. locked = tb->FL[i];
  2084. }
  2085. if (!locked && tb->CFL[i]) {
  2086. tb_buffer_sanity_check(tb->tb_sb,
  2087. tb->CFL[i],
  2088. "CFL", i);
  2089. if (!clear_all_dirty_bits
  2090. (tb->tb_sb, tb->CFL[i]))
  2091. locked = tb->CFL[i];
  2092. }
  2093. }
  2094. if (!locked && (tb->rnum[i])) {
  2095. if (tb->R[i]) {
  2096. tb_buffer_sanity_check(tb->tb_sb,
  2097. tb->R[i],
  2098. "R", i);
  2099. if (!clear_all_dirty_bits
  2100. (tb->tb_sb, tb->R[i]))
  2101. locked = tb->R[i];
  2102. }
  2103. if (!locked && tb->FR[i]) {
  2104. tb_buffer_sanity_check(tb->tb_sb,
  2105. tb->FR[i],
  2106. "FR", i);
  2107. if (!clear_all_dirty_bits
  2108. (tb->tb_sb, tb->FR[i]))
  2109. locked = tb->FR[i];
  2110. }
  2111. if (!locked && tb->CFR[i]) {
  2112. tb_buffer_sanity_check(tb->tb_sb,
  2113. tb->CFR[i],
  2114. "CFR", i);
  2115. if (!clear_all_dirty_bits
  2116. (tb->tb_sb, tb->CFR[i]))
  2117. locked = tb->CFR[i];
  2118. }
  2119. }
  2120. }
  2121. /*
  2122. * as far as I can tell, this is not required. The FEB list
  2123. * seems to be full of newly allocated nodes, which will
  2124. * never be locked, dirty, or anything else.
  2125. * To be safe, I'm putting in the checks and waits in.
  2126. * For the moment, they are needed to keep the code in
  2127. * journal.c from complaining about the buffer.
  2128. * That code is inside CONFIG_REISERFS_CHECK as well. --clm
  2129. */
  2130. for (i = 0; !locked && i < MAX_FEB_SIZE; i++) {
  2131. if (tb->FEB[i]) {
  2132. if (!clear_all_dirty_bits
  2133. (tb->tb_sb, tb->FEB[i]))
  2134. locked = tb->FEB[i];
  2135. }
  2136. }
  2137. if (locked) {
  2138. int depth;
  2139. #ifdef CONFIG_REISERFS_CHECK
  2140. repeat_counter++;
  2141. if ((repeat_counter % 10000) == 0) {
  2142. reiserfs_warning(tb->tb_sb, "reiserfs-8200",
  2143. "too many iterations waiting "
  2144. "for buffer to unlock "
  2145. "(%b)", locked);
  2146. /* Don't loop forever. Try to recover from possible error. */
  2147. return (FILESYSTEM_CHANGED_TB(tb)) ?
  2148. REPEAT_SEARCH : CARRY_ON;
  2149. }
  2150. #endif
  2151. depth = reiserfs_write_unlock_nested(tb->tb_sb);
  2152. __wait_on_buffer(locked);
  2153. reiserfs_write_lock_nested(tb->tb_sb, depth);
  2154. if (FILESYSTEM_CHANGED_TB(tb))
  2155. return REPEAT_SEARCH;
  2156. }
  2157. } while (locked);
  2158. return CARRY_ON;
  2159. }
  2160. /*
  2161. * Prepare for balancing, that is
  2162. * get all necessary parents, and neighbors;
  2163. * analyze what and where should be moved;
  2164. * get sufficient number of new nodes;
  2165. * Balancing will start only after all resources will be collected at a time.
  2166. *
  2167. * When ported to SMP kernels, only at the last moment after all needed nodes
  2168. * are collected in cache, will the resources be locked using the usual
  2169. * textbook ordered lock acquisition algorithms. Note that ensuring that
  2170. * this code neither write locks what it does not need to write lock nor locks
  2171. * out of order will be a pain in the butt that could have been avoided.
  2172. * Grumble grumble. -Hans
  2173. *
  2174. * fix is meant in the sense of render unchanging
  2175. *
  2176. * Latency might be improved by first gathering a list of what buffers
  2177. * are needed and then getting as many of them in parallel as possible? -Hans
  2178. *
  2179. * Parameters:
  2180. * op_mode i - insert, d - delete, c - cut (truncate), p - paste (append)
  2181. * tb tree_balance structure;
  2182. * inum item number in S[h];
  2183. * pos_in_item - comment this if you can
  2184. * ins_ih item head of item being inserted
  2185. * data inserted item or data to be pasted
  2186. * Returns: 1 - schedule occurred while the function worked;
  2187. * 0 - schedule didn't occur while the function worked;
  2188. * -1 - if no_disk_space
  2189. */
  2190. int fix_nodes(int op_mode, struct tree_balance *tb,
  2191. struct item_head *ins_ih, const void *data)
  2192. {
  2193. int ret, h, item_num = PATH_LAST_POSITION(tb->tb_path);
  2194. int pos_in_item;
  2195. /*
  2196. * we set wait_tb_buffers_run when we have to restore any dirty
  2197. * bits cleared during wait_tb_buffers_run
  2198. */
  2199. int wait_tb_buffers_run = 0;
  2200. struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
  2201. ++REISERFS_SB(tb->tb_sb)->s_fix_nodes;
  2202. pos_in_item = tb->tb_path->pos_in_item;
  2203. tb->fs_gen = get_generation(tb->tb_sb);
  2204. /*
  2205. * we prepare and log the super here so it will already be in the
  2206. * transaction when do_balance needs to change it.
  2207. * This way do_balance won't have to schedule when trying to prepare
  2208. * the super for logging
  2209. */
  2210. reiserfs_prepare_for_journal(tb->tb_sb,
  2211. SB_BUFFER_WITH_SB(tb->tb_sb), 1);
  2212. journal_mark_dirty(tb->transaction_handle,
  2213. SB_BUFFER_WITH_SB(tb->tb_sb));
  2214. if (FILESYSTEM_CHANGED_TB(tb))
  2215. return REPEAT_SEARCH;
  2216. /* if it possible in indirect_to_direct conversion */
  2217. if (buffer_locked(tbS0)) {
  2218. int depth = reiserfs_write_unlock_nested(tb->tb_sb);
  2219. __wait_on_buffer(tbS0);
  2220. reiserfs_write_lock_nested(tb->tb_sb, depth);
  2221. if (FILESYSTEM_CHANGED_TB(tb))
  2222. return REPEAT_SEARCH;
  2223. }
  2224. #ifdef CONFIG_REISERFS_CHECK
  2225. if (REISERFS_SB(tb->tb_sb)->cur_tb) {
  2226. print_cur_tb("fix_nodes");
  2227. reiserfs_panic(tb->tb_sb, "PAP-8305",
  2228. "there is pending do_balance");
  2229. }
  2230. if (!buffer_uptodate(tbS0) || !B_IS_IN_TREE(tbS0))
  2231. reiserfs_panic(tb->tb_sb, "PAP-8320", "S[0] (%b %z) is "
  2232. "not uptodate at the beginning of fix_nodes "
  2233. "or not in tree (mode %c)",
  2234. tbS0, tbS0, op_mode);
  2235. /* Check parameters. */
  2236. switch (op_mode) {
  2237. case M_INSERT:
  2238. if (item_num <= 0 || item_num > B_NR_ITEMS(tbS0))
  2239. reiserfs_panic(tb->tb_sb, "PAP-8330", "Incorrect "
  2240. "item number %d (in S0 - %d) in case "
  2241. "of insert", item_num,
  2242. B_NR_ITEMS(tbS0));
  2243. break;
  2244. case M_PASTE:
  2245. case M_DELETE:
  2246. case M_CUT:
  2247. if (item_num < 0 || item_num >= B_NR_ITEMS(tbS0)) {
  2248. print_block(tbS0, 0, -1, -1);
  2249. reiserfs_panic(tb->tb_sb, "PAP-8335", "Incorrect "
  2250. "item number(%d); mode = %c "
  2251. "insert_size = %d",
  2252. item_num, op_mode,
  2253. tb->insert_size[0]);
  2254. }
  2255. break;
  2256. default:
  2257. reiserfs_panic(tb->tb_sb, "PAP-8340", "Incorrect mode "
  2258. "of operation");
  2259. }
  2260. #endif
  2261. if (get_mem_for_virtual_node(tb) == REPEAT_SEARCH)
  2262. /* FIXME: maybe -ENOMEM when tb->vn_buf == 0? Now just repeat */
  2263. return REPEAT_SEARCH;
  2264. /* Starting from the leaf level; for all levels h of the tree. */
  2265. for (h = 0; h < MAX_HEIGHT && tb->insert_size[h]; h++) {
  2266. ret = get_direct_parent(tb, h);
  2267. if (ret != CARRY_ON)
  2268. goto repeat;
  2269. ret = check_balance(op_mode, tb, h, item_num,
  2270. pos_in_item, ins_ih, data);
  2271. if (ret != CARRY_ON) {
  2272. if (ret == NO_BALANCING_NEEDED) {
  2273. /* No balancing for higher levels needed. */
  2274. ret = get_neighbors(tb, h);
  2275. if (ret != CARRY_ON)
  2276. goto repeat;
  2277. if (h != MAX_HEIGHT - 1)
  2278. tb->insert_size[h + 1] = 0;
  2279. /*
  2280. * ok, analysis and resource gathering
  2281. * are complete
  2282. */
  2283. break;
  2284. }
  2285. goto repeat;
  2286. }
  2287. ret = get_neighbors(tb, h);
  2288. if (ret != CARRY_ON)
  2289. goto repeat;
  2290. /*
  2291. * No disk space, or schedule occurred and analysis may be
  2292. * invalid and needs to be redone.
  2293. */
  2294. ret = get_empty_nodes(tb, h);
  2295. if (ret != CARRY_ON)
  2296. goto repeat;
  2297. /*
  2298. * We have a positive insert size but no nodes exist on this
  2299. * level, this means that we are creating a new root.
  2300. */
  2301. if (!PATH_H_PBUFFER(tb->tb_path, h)) {
  2302. RFALSE(tb->blknum[h] != 1,
  2303. "PAP-8350: creating new empty root");
  2304. if (h < MAX_HEIGHT - 1)
  2305. tb->insert_size[h + 1] = 0;
  2306. } else if (!PATH_H_PBUFFER(tb->tb_path, h + 1)) {
  2307. /*
  2308. * The tree needs to be grown, so this node S[h]
  2309. * which is the root node is split into two nodes,
  2310. * and a new node (S[h+1]) will be created to
  2311. * become the root node.
  2312. */
  2313. if (tb->blknum[h] > 1) {
  2314. RFALSE(h == MAX_HEIGHT - 1,
  2315. "PAP-8355: attempt to create too high of a tree");
  2316. tb->insert_size[h + 1] =
  2317. (DC_SIZE +
  2318. KEY_SIZE) * (tb->blknum[h] - 1) +
  2319. DC_SIZE;
  2320. } else if (h < MAX_HEIGHT - 1)
  2321. tb->insert_size[h + 1] = 0;
  2322. } else
  2323. tb->insert_size[h + 1] =
  2324. (DC_SIZE + KEY_SIZE) * (tb->blknum[h] - 1);
  2325. }
  2326. ret = wait_tb_buffers_until_unlocked(tb);
  2327. if (ret == CARRY_ON) {
  2328. if (FILESYSTEM_CHANGED_TB(tb)) {
  2329. wait_tb_buffers_run = 1;
  2330. ret = REPEAT_SEARCH;
  2331. goto repeat;
  2332. } else {
  2333. return CARRY_ON;
  2334. }
  2335. } else {
  2336. wait_tb_buffers_run = 1;
  2337. goto repeat;
  2338. }
  2339. repeat:
  2340. /*
  2341. * fix_nodes was unable to perform its calculation due to
  2342. * filesystem got changed under us, lack of free disk space or i/o
  2343. * failure. If the first is the case - the search will be
  2344. * repeated. For now - free all resources acquired so far except
  2345. * for the new allocated nodes
  2346. */
  2347. {
  2348. int i;
  2349. /* Release path buffers. */
  2350. if (wait_tb_buffers_run) {
  2351. pathrelse_and_restore(tb->tb_sb, tb->tb_path);
  2352. } else {
  2353. pathrelse(tb->tb_path);
  2354. }
  2355. /* brelse all resources collected for balancing */
  2356. for (i = 0; i < MAX_HEIGHT; i++) {
  2357. if (wait_tb_buffers_run) {
  2358. reiserfs_restore_prepared_buffer(tb->tb_sb,
  2359. tb->L[i]);
  2360. reiserfs_restore_prepared_buffer(tb->tb_sb,
  2361. tb->R[i]);
  2362. reiserfs_restore_prepared_buffer(tb->tb_sb,
  2363. tb->FL[i]);
  2364. reiserfs_restore_prepared_buffer(tb->tb_sb,
  2365. tb->FR[i]);
  2366. reiserfs_restore_prepared_buffer(tb->tb_sb,
  2367. tb->
  2368. CFL[i]);
  2369. reiserfs_restore_prepared_buffer(tb->tb_sb,
  2370. tb->
  2371. CFR[i]);
  2372. }
  2373. brelse(tb->L[i]);
  2374. brelse(tb->R[i]);
  2375. brelse(tb->FL[i]);
  2376. brelse(tb->FR[i]);
  2377. brelse(tb->CFL[i]);
  2378. brelse(tb->CFR[i]);
  2379. tb->L[i] = NULL;
  2380. tb->R[i] = NULL;
  2381. tb->FL[i] = NULL;
  2382. tb->FR[i] = NULL;
  2383. tb->CFL[i] = NULL;
  2384. tb->CFR[i] = NULL;
  2385. }
  2386. if (wait_tb_buffers_run) {
  2387. for (i = 0; i < MAX_FEB_SIZE; i++) {
  2388. if (tb->FEB[i])
  2389. reiserfs_restore_prepared_buffer
  2390. (tb->tb_sb, tb->FEB[i]);
  2391. }
  2392. }
  2393. return ret;
  2394. }
  2395. }
  2396. void unfix_nodes(struct tree_balance *tb)
  2397. {
  2398. int i;
  2399. /* Release path buffers. */
  2400. pathrelse_and_restore(tb->tb_sb, tb->tb_path);
  2401. /* brelse all resources collected for balancing */
  2402. for (i = 0; i < MAX_HEIGHT; i++) {
  2403. reiserfs_restore_prepared_buffer(tb->tb_sb, tb->L[i]);
  2404. reiserfs_restore_prepared_buffer(tb->tb_sb, tb->R[i]);
  2405. reiserfs_restore_prepared_buffer(tb->tb_sb, tb->FL[i]);
  2406. reiserfs_restore_prepared_buffer(tb->tb_sb, tb->FR[i]);
  2407. reiserfs_restore_prepared_buffer(tb->tb_sb, tb->CFL[i]);
  2408. reiserfs_restore_prepared_buffer(tb->tb_sb, tb->CFR[i]);
  2409. brelse(tb->L[i]);
  2410. brelse(tb->R[i]);
  2411. brelse(tb->FL[i]);
  2412. brelse(tb->FR[i]);
  2413. brelse(tb->CFL[i]);
  2414. brelse(tb->CFR[i]);
  2415. }
  2416. /* deal with list of allocated (used and unused) nodes */
  2417. for (i = 0; i < MAX_FEB_SIZE; i++) {
  2418. if (tb->FEB[i]) {
  2419. b_blocknr_t blocknr = tb->FEB[i]->b_blocknr;
  2420. /*
  2421. * de-allocated block which was not used by
  2422. * balancing and bforget about buffer for it
  2423. */
  2424. brelse(tb->FEB[i]);
  2425. reiserfs_free_block(tb->transaction_handle, NULL,
  2426. blocknr, 0);
  2427. }
  2428. if (tb->used[i]) {
  2429. /* release used as new nodes including a new root */
  2430. brelse(tb->used[i]);
  2431. }
  2432. }
  2433. kfree(tb->vn_buf);
  2434. }