fix_node.c 77 KB

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