lbalance.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. /*
  2. * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
  3. */
  4. #include <linux/uaccess.h>
  5. #include <linux/string.h>
  6. #include <linux/time.h>
  7. #include "reiserfs.h"
  8. #include <linux/buffer_head.h>
  9. /*
  10. * copy copy_count entries from source directory item to dest buffer
  11. * (creating new item if needed)
  12. */
  13. static void leaf_copy_dir_entries(struct buffer_info *dest_bi,
  14. struct buffer_head *source, int last_first,
  15. int item_num, int from, int copy_count)
  16. {
  17. struct buffer_head *dest = dest_bi->bi_bh;
  18. /*
  19. * either the number of target item, or if we must create a
  20. * new item, the number of the item we will create it next to
  21. */
  22. int item_num_in_dest;
  23. struct item_head *ih;
  24. struct reiserfs_de_head *deh;
  25. int copy_records_len; /* length of all records in item to be copied */
  26. char *records;
  27. ih = item_head(source, item_num);
  28. RFALSE(!is_direntry_le_ih(ih), "vs-10000: item must be directory item");
  29. /*
  30. * length of all record to be copied and first byte of
  31. * the last of them
  32. */
  33. deh = B_I_DEH(source, ih);
  34. if (copy_count) {
  35. copy_records_len = (from ? deh_location(&deh[from - 1]) :
  36. ih_item_len(ih)) -
  37. deh_location(&deh[from + copy_count - 1]);
  38. records =
  39. source->b_data + ih_location(ih) +
  40. deh_location(&deh[from + copy_count - 1]);
  41. } else {
  42. copy_records_len = 0;
  43. records = NULL;
  44. }
  45. /* when copy last to first, dest buffer can contain 0 items */
  46. item_num_in_dest =
  47. (last_first ==
  48. LAST_TO_FIRST) ? ((B_NR_ITEMS(dest)) ? 0 : -1) : (B_NR_ITEMS(dest)
  49. - 1);
  50. /*
  51. * if there are no items in dest or the first/last item in
  52. * dest is not item of the same directory
  53. */
  54. if ((item_num_in_dest == -1) ||
  55. (last_first == FIRST_TO_LAST && le_ih_k_offset(ih) == DOT_OFFSET) ||
  56. (last_first == LAST_TO_FIRST
  57. && comp_short_le_keys /*COMP_SHORT_KEYS */ (&ih->ih_key,
  58. leaf_key(dest,
  59. item_num_in_dest))))
  60. {
  61. /* create new item in dest */
  62. struct item_head new_ih;
  63. /* form item header */
  64. memcpy(&new_ih.ih_key, &ih->ih_key, KEY_SIZE);
  65. put_ih_version(&new_ih, KEY_FORMAT_3_5);
  66. /* calculate item len */
  67. put_ih_item_len(&new_ih,
  68. DEH_SIZE * copy_count + copy_records_len);
  69. put_ih_entry_count(&new_ih, 0);
  70. if (last_first == LAST_TO_FIRST) {
  71. /* form key by the following way */
  72. if (from < ih_entry_count(ih)) {
  73. set_le_ih_k_offset(&new_ih,
  74. deh_offset(&deh[from]));
  75. } else {
  76. /*
  77. * no entries will be copied to this
  78. * item in this function
  79. */
  80. set_le_ih_k_offset(&new_ih, U32_MAX);
  81. /*
  82. * this item is not yet valid, but we
  83. * want I_IS_DIRECTORY_ITEM to return 1
  84. * for it, so we -1
  85. */
  86. }
  87. set_le_key_k_type(KEY_FORMAT_3_5, &new_ih.ih_key,
  88. TYPE_DIRENTRY);
  89. }
  90. /* insert item into dest buffer */
  91. leaf_insert_into_buf(dest_bi,
  92. (last_first ==
  93. LAST_TO_FIRST) ? 0 : B_NR_ITEMS(dest),
  94. &new_ih, NULL, 0);
  95. } else {
  96. /* prepare space for entries */
  97. leaf_paste_in_buffer(dest_bi,
  98. (last_first ==
  99. FIRST_TO_LAST) ? (B_NR_ITEMS(dest) -
  100. 1) : 0, MAX_US_INT,
  101. DEH_SIZE * copy_count + copy_records_len,
  102. records, 0);
  103. }
  104. item_num_in_dest =
  105. (last_first == FIRST_TO_LAST) ? (B_NR_ITEMS(dest) - 1) : 0;
  106. leaf_paste_entries(dest_bi, item_num_in_dest,
  107. (last_first ==
  108. FIRST_TO_LAST) ? ih_entry_count(item_head(dest,
  109. item_num_in_dest))
  110. : 0, copy_count, deh + from, records,
  111. DEH_SIZE * copy_count + copy_records_len);
  112. }
  113. /*
  114. * Copy the first (if last_first == FIRST_TO_LAST) or last
  115. * (last_first == LAST_TO_FIRST) item or part of it or nothing
  116. * (see the return 0 below) from SOURCE to the end (if last_first)
  117. * or beginning (!last_first) of the DEST
  118. */
  119. /* returns 1 if anything was copied, else 0 */
  120. static int leaf_copy_boundary_item(struct buffer_info *dest_bi,
  121. struct buffer_head *src, int last_first,
  122. int bytes_or_entries)
  123. {
  124. struct buffer_head *dest = dest_bi->bi_bh;
  125. /* number of items in the source and destination buffers */
  126. int dest_nr_item, src_nr_item;
  127. struct item_head *ih;
  128. struct item_head *dih;
  129. dest_nr_item = B_NR_ITEMS(dest);
  130. /*
  131. * if ( DEST is empty or first item of SOURCE and last item of
  132. * DEST are the items of different objects or of different types )
  133. * then there is no need to treat this item differently from the
  134. * other items that we copy, so we return
  135. */
  136. if (last_first == FIRST_TO_LAST) {
  137. ih = item_head(src, 0);
  138. dih = item_head(dest, dest_nr_item - 1);
  139. /* there is nothing to merge */
  140. if (!dest_nr_item
  141. || (!op_is_left_mergeable(&ih->ih_key, src->b_size)))
  142. return 0;
  143. RFALSE(!ih_item_len(ih),
  144. "vs-10010: item can not have empty length");
  145. if (is_direntry_le_ih(ih)) {
  146. if (bytes_or_entries == -1)
  147. /* copy all entries to dest */
  148. bytes_or_entries = ih_entry_count(ih);
  149. leaf_copy_dir_entries(dest_bi, src, FIRST_TO_LAST, 0, 0,
  150. bytes_or_entries);
  151. return 1;
  152. }
  153. /*
  154. * copy part of the body of the first item of SOURCE
  155. * to the end of the body of the last item of the DEST
  156. * part defined by 'bytes_or_entries'; if bytes_or_entries
  157. * == -1 copy whole body; don't create new item header
  158. */
  159. if (bytes_or_entries == -1)
  160. bytes_or_entries = ih_item_len(ih);
  161. #ifdef CONFIG_REISERFS_CHECK
  162. else {
  163. if (bytes_or_entries == ih_item_len(ih)
  164. && is_indirect_le_ih(ih))
  165. if (get_ih_free_space(ih))
  166. reiserfs_panic(sb_from_bi(dest_bi),
  167. "vs-10020",
  168. "last unformatted node "
  169. "must be filled "
  170. "entirely (%h)", ih);
  171. }
  172. #endif
  173. /*
  174. * merge first item (or its part) of src buffer with the last
  175. * item of dest buffer. Both are of the same file
  176. */
  177. leaf_paste_in_buffer(dest_bi,
  178. dest_nr_item - 1, ih_item_len(dih),
  179. bytes_or_entries, ih_item_body(src, ih), 0);
  180. if (is_indirect_le_ih(dih)) {
  181. RFALSE(get_ih_free_space(dih),
  182. "vs-10030: merge to left: last unformatted node of non-last indirect item %h must have zerto free space",
  183. ih);
  184. if (bytes_or_entries == ih_item_len(ih))
  185. set_ih_free_space(dih, get_ih_free_space(ih));
  186. }
  187. return 1;
  188. }
  189. /* copy boundary item to right (last_first == LAST_TO_FIRST) */
  190. /*
  191. * (DEST is empty or last item of SOURCE and first item of DEST
  192. * are the items of different object or of different types)
  193. */
  194. src_nr_item = B_NR_ITEMS(src);
  195. ih = item_head(src, src_nr_item - 1);
  196. dih = item_head(dest, 0);
  197. if (!dest_nr_item || !op_is_left_mergeable(&dih->ih_key, src->b_size))
  198. return 0;
  199. if (is_direntry_le_ih(ih)) {
  200. /*
  201. * bytes_or_entries = entries number in last
  202. * item body of SOURCE
  203. */
  204. if (bytes_or_entries == -1)
  205. bytes_or_entries = ih_entry_count(ih);
  206. leaf_copy_dir_entries(dest_bi, src, LAST_TO_FIRST,
  207. src_nr_item - 1,
  208. ih_entry_count(ih) - bytes_or_entries,
  209. bytes_or_entries);
  210. return 1;
  211. }
  212. /*
  213. * copy part of the body of the last item of SOURCE to the
  214. * begin of the body of the first item of the DEST; part defined
  215. * by 'bytes_or_entries'; if byte_or_entriess == -1 copy whole body;
  216. * change first item key of the DEST; don't create new item header
  217. */
  218. RFALSE(is_indirect_le_ih(ih) && get_ih_free_space(ih),
  219. "vs-10040: merge to right: last unformatted node of non-last indirect item must be filled entirely (%h)",
  220. ih);
  221. if (bytes_or_entries == -1) {
  222. /* bytes_or_entries = length of last item body of SOURCE */
  223. bytes_or_entries = ih_item_len(ih);
  224. RFALSE(le_ih_k_offset(dih) !=
  225. le_ih_k_offset(ih) + op_bytes_number(ih, src->b_size),
  226. "vs-10050: items %h and %h do not match", ih, dih);
  227. /* change first item key of the DEST */
  228. set_le_ih_k_offset(dih, le_ih_k_offset(ih));
  229. /* item becomes non-mergeable */
  230. /* or mergeable if left item was */
  231. set_le_ih_k_type(dih, le_ih_k_type(ih));
  232. } else {
  233. /* merge to right only part of item */
  234. RFALSE(ih_item_len(ih) <= bytes_or_entries,
  235. "vs-10060: no so much bytes %lu (needed %lu)",
  236. (unsigned long)ih_item_len(ih),
  237. (unsigned long)bytes_or_entries);
  238. /* change first item key of the DEST */
  239. if (is_direct_le_ih(dih)) {
  240. RFALSE(le_ih_k_offset(dih) <=
  241. (unsigned long)bytes_or_entries,
  242. "vs-10070: dih %h, bytes_or_entries(%d)", dih,
  243. bytes_or_entries);
  244. set_le_ih_k_offset(dih,
  245. le_ih_k_offset(dih) -
  246. bytes_or_entries);
  247. } else {
  248. RFALSE(le_ih_k_offset(dih) <=
  249. (bytes_or_entries / UNFM_P_SIZE) * dest->b_size,
  250. "vs-10080: dih %h, bytes_or_entries(%d)",
  251. dih,
  252. (bytes_or_entries / UNFM_P_SIZE) * dest->b_size);
  253. set_le_ih_k_offset(dih,
  254. le_ih_k_offset(dih) -
  255. ((bytes_or_entries / UNFM_P_SIZE) *
  256. dest->b_size));
  257. }
  258. }
  259. leaf_paste_in_buffer(dest_bi, 0, 0, bytes_or_entries,
  260. ih_item_body(src,
  261. ih) + ih_item_len(ih) - bytes_or_entries,
  262. 0);
  263. return 1;
  264. }
  265. /*
  266. * copy cpy_mun items from buffer src to buffer dest
  267. * last_first == FIRST_TO_LAST means, that we copy cpy_num items beginning
  268. * from first-th item in src to tail of dest
  269. * last_first == LAST_TO_FIRST means, that we copy cpy_num items beginning
  270. * from first-th item in src to head of dest
  271. */
  272. static void leaf_copy_items_entirely(struct buffer_info *dest_bi,
  273. struct buffer_head *src, int last_first,
  274. int first, int cpy_num)
  275. {
  276. struct buffer_head *dest;
  277. int nr, free_space;
  278. int dest_before;
  279. int last_loc, last_inserted_loc, location;
  280. int i, j;
  281. struct block_head *blkh;
  282. struct item_head *ih;
  283. RFALSE(last_first != LAST_TO_FIRST && last_first != FIRST_TO_LAST,
  284. "vs-10090: bad last_first parameter %d", last_first);
  285. RFALSE(B_NR_ITEMS(src) - first < cpy_num,
  286. "vs-10100: too few items in source %d, required %d from %d",
  287. B_NR_ITEMS(src), cpy_num, first);
  288. RFALSE(cpy_num < 0, "vs-10110: can not copy negative amount of items");
  289. RFALSE(!dest_bi, "vs-10120: can not copy negative amount of items");
  290. dest = dest_bi->bi_bh;
  291. RFALSE(!dest, "vs-10130: can not copy negative amount of items");
  292. if (cpy_num == 0)
  293. return;
  294. blkh = B_BLK_HEAD(dest);
  295. nr = blkh_nr_item(blkh);
  296. free_space = blkh_free_space(blkh);
  297. /*
  298. * we will insert items before 0-th or nr-th item in dest buffer.
  299. * It depends of last_first parameter
  300. */
  301. dest_before = (last_first == LAST_TO_FIRST) ? 0 : nr;
  302. /* location of head of first new item */
  303. ih = item_head(dest, dest_before);
  304. RFALSE(blkh_free_space(blkh) < cpy_num * IH_SIZE,
  305. "vs-10140: not enough free space for headers %d (needed %d)",
  306. B_FREE_SPACE(dest), cpy_num * IH_SIZE);
  307. /* prepare space for headers */
  308. memmove(ih + cpy_num, ih, (nr - dest_before) * IH_SIZE);
  309. /* copy item headers */
  310. memcpy(ih, item_head(src, first), cpy_num * IH_SIZE);
  311. free_space -= (IH_SIZE * cpy_num);
  312. set_blkh_free_space(blkh, free_space);
  313. /* location of unmovable item */
  314. j = location = (dest_before == 0) ? dest->b_size : ih_location(ih - 1);
  315. for (i = dest_before; i < nr + cpy_num; i++) {
  316. location -= ih_item_len(ih + i - dest_before);
  317. put_ih_location(ih + i - dest_before, location);
  318. }
  319. /* prepare space for items */
  320. last_loc = ih_location(&ih[nr + cpy_num - 1 - dest_before]);
  321. last_inserted_loc = ih_location(&ih[cpy_num - 1]);
  322. /* check free space */
  323. RFALSE(free_space < j - last_inserted_loc,
  324. "vs-10150: not enough free space for items %d (needed %d)",
  325. free_space, j - last_inserted_loc);
  326. memmove(dest->b_data + last_loc,
  327. dest->b_data + last_loc + j - last_inserted_loc,
  328. last_inserted_loc - last_loc);
  329. /* copy items */
  330. memcpy(dest->b_data + last_inserted_loc,
  331. item_body(src, (first + cpy_num - 1)),
  332. j - last_inserted_loc);
  333. /* sizes, item number */
  334. set_blkh_nr_item(blkh, nr + cpy_num);
  335. set_blkh_free_space(blkh, free_space - (j - last_inserted_loc));
  336. do_balance_mark_leaf_dirty(dest_bi->tb, dest, 0);
  337. if (dest_bi->bi_parent) {
  338. struct disk_child *t_dc;
  339. t_dc = B_N_CHILD(dest_bi->bi_parent, dest_bi->bi_position);
  340. RFALSE(dc_block_number(t_dc) != dest->b_blocknr,
  341. "vs-10160: block number in bh does not match to field in disk_child structure %lu and %lu",
  342. (long unsigned)dest->b_blocknr,
  343. (long unsigned)dc_block_number(t_dc));
  344. put_dc_size(t_dc,
  345. dc_size(t_dc) + (j - last_inserted_loc +
  346. IH_SIZE * cpy_num));
  347. do_balance_mark_internal_dirty(dest_bi->tb, dest_bi->bi_parent,
  348. 0);
  349. }
  350. }
  351. /*
  352. * This function splits the (liquid) item into two items (useful when
  353. * shifting part of an item into another node.)
  354. */
  355. static void leaf_item_bottle(struct buffer_info *dest_bi,
  356. struct buffer_head *src, int last_first,
  357. int item_num, int cpy_bytes)
  358. {
  359. struct buffer_head *dest = dest_bi->bi_bh;
  360. struct item_head *ih;
  361. RFALSE(cpy_bytes == -1,
  362. "vs-10170: bytes == - 1 means: do not split item");
  363. if (last_first == FIRST_TO_LAST) {
  364. /*
  365. * if ( if item in position item_num in buffer SOURCE
  366. * is directory item )
  367. */
  368. ih = item_head(src, item_num);
  369. if (is_direntry_le_ih(ih))
  370. leaf_copy_dir_entries(dest_bi, src, FIRST_TO_LAST,
  371. item_num, 0, cpy_bytes);
  372. else {
  373. struct item_head n_ih;
  374. /*
  375. * copy part of the body of the item number 'item_num'
  376. * of SOURCE to the end of the DEST part defined by
  377. * 'cpy_bytes'; create new item header; change old
  378. * item_header (????); n_ih = new item_header;
  379. */
  380. memcpy(&n_ih, ih, IH_SIZE);
  381. put_ih_item_len(&n_ih, cpy_bytes);
  382. if (is_indirect_le_ih(ih)) {
  383. RFALSE(cpy_bytes == ih_item_len(ih)
  384. && get_ih_free_space(ih),
  385. "vs-10180: when whole indirect item is bottle to left neighbor, it must have free_space==0 (not %lu)",
  386. (long unsigned)get_ih_free_space(ih));
  387. set_ih_free_space(&n_ih, 0);
  388. }
  389. RFALSE(op_is_left_mergeable(&ih->ih_key, src->b_size),
  390. "vs-10190: bad mergeability of item %h", ih);
  391. n_ih.ih_version = ih->ih_version; /* JDM Endian safe, both le */
  392. leaf_insert_into_buf(dest_bi, B_NR_ITEMS(dest), &n_ih,
  393. item_body(src, item_num), 0);
  394. }
  395. } else {
  396. /*
  397. * if ( if item in position item_num in buffer
  398. * SOURCE is directory item )
  399. */
  400. ih = item_head(src, item_num);
  401. if (is_direntry_le_ih(ih))
  402. leaf_copy_dir_entries(dest_bi, src, LAST_TO_FIRST,
  403. item_num,
  404. ih_entry_count(ih) - cpy_bytes,
  405. cpy_bytes);
  406. else {
  407. struct item_head n_ih;
  408. /*
  409. * copy part of the body of the item number 'item_num'
  410. * of SOURCE to the begin of the DEST part defined by
  411. * 'cpy_bytes'; create new item header;
  412. * n_ih = new item_header;
  413. */
  414. memcpy(&n_ih.ih_key, &ih->ih_key, KEY_SIZE);
  415. /* Endian safe, both le */
  416. n_ih.ih_version = ih->ih_version;
  417. if (is_direct_le_ih(ih)) {
  418. set_le_ih_k_offset(&n_ih,
  419. le_ih_k_offset(ih) +
  420. ih_item_len(ih) - cpy_bytes);
  421. set_le_ih_k_type(&n_ih, TYPE_DIRECT);
  422. set_ih_free_space(&n_ih, MAX_US_INT);
  423. } else {
  424. /* indirect item */
  425. RFALSE(!cpy_bytes && get_ih_free_space(ih),
  426. "vs-10200: ih->ih_free_space must be 0 when indirect item will be appended");
  427. set_le_ih_k_offset(&n_ih,
  428. le_ih_k_offset(ih) +
  429. (ih_item_len(ih) -
  430. cpy_bytes) / UNFM_P_SIZE *
  431. dest->b_size);
  432. set_le_ih_k_type(&n_ih, TYPE_INDIRECT);
  433. set_ih_free_space(&n_ih, get_ih_free_space(ih));
  434. }
  435. /* set item length */
  436. put_ih_item_len(&n_ih, cpy_bytes);
  437. /* Endian safe, both le */
  438. n_ih.ih_version = ih->ih_version;
  439. leaf_insert_into_buf(dest_bi, 0, &n_ih,
  440. item_body(src, item_num) +
  441. ih_item_len(ih) - cpy_bytes, 0);
  442. }
  443. }
  444. }
  445. /*
  446. * If cpy_bytes equals minus one than copy cpy_num whole items from SOURCE
  447. * to DEST. If cpy_bytes not equal to minus one than copy cpy_num-1 whole
  448. * items from SOURCE to DEST. From last item copy cpy_num bytes for regular
  449. * item and cpy_num directory entries for directory item.
  450. */
  451. static int leaf_copy_items(struct buffer_info *dest_bi, struct buffer_head *src,
  452. int last_first, int cpy_num, int cpy_bytes)
  453. {
  454. struct buffer_head *dest;
  455. int pos, i, src_nr_item, bytes;
  456. dest = dest_bi->bi_bh;
  457. RFALSE(!dest || !src, "vs-10210: !dest || !src");
  458. RFALSE(last_first != FIRST_TO_LAST && last_first != LAST_TO_FIRST,
  459. "vs-10220:last_first != FIRST_TO_LAST && last_first != LAST_TO_FIRST");
  460. RFALSE(B_NR_ITEMS(src) < cpy_num,
  461. "vs-10230: No enough items: %d, req. %d", B_NR_ITEMS(src),
  462. cpy_num);
  463. RFALSE(cpy_num < 0, "vs-10240: cpy_num < 0 (%d)", cpy_num);
  464. if (cpy_num == 0)
  465. return 0;
  466. if (last_first == FIRST_TO_LAST) {
  467. /* copy items to left */
  468. pos = 0;
  469. if (cpy_num == 1)
  470. bytes = cpy_bytes;
  471. else
  472. bytes = -1;
  473. /*
  474. * copy the first item or it part or nothing to the end of
  475. * the DEST (i = leaf_copy_boundary_item(DEST,SOURCE,0,bytes))
  476. */
  477. i = leaf_copy_boundary_item(dest_bi, src, FIRST_TO_LAST, bytes);
  478. cpy_num -= i;
  479. if (cpy_num == 0)
  480. return i;
  481. pos += i;
  482. if (cpy_bytes == -1)
  483. /*
  484. * copy first cpy_num items starting from position
  485. * 'pos' of SOURCE to end of DEST
  486. */
  487. leaf_copy_items_entirely(dest_bi, src, FIRST_TO_LAST,
  488. pos, cpy_num);
  489. else {
  490. /*
  491. * copy first cpy_num-1 items starting from position
  492. * 'pos-1' of the SOURCE to the end of the DEST
  493. */
  494. leaf_copy_items_entirely(dest_bi, src, FIRST_TO_LAST,
  495. pos, cpy_num - 1);
  496. /*
  497. * copy part of the item which number is
  498. * cpy_num+pos-1 to the end of the DEST
  499. */
  500. leaf_item_bottle(dest_bi, src, FIRST_TO_LAST,
  501. cpy_num + pos - 1, cpy_bytes);
  502. }
  503. } else {
  504. /* copy items to right */
  505. src_nr_item = B_NR_ITEMS(src);
  506. if (cpy_num == 1)
  507. bytes = cpy_bytes;
  508. else
  509. bytes = -1;
  510. /*
  511. * copy the last item or it part or nothing to the
  512. * begin of the DEST
  513. * (i = leaf_copy_boundary_item(DEST,SOURCE,1,bytes));
  514. */
  515. i = leaf_copy_boundary_item(dest_bi, src, LAST_TO_FIRST, bytes);
  516. cpy_num -= i;
  517. if (cpy_num == 0)
  518. return i;
  519. pos = src_nr_item - cpy_num - i;
  520. if (cpy_bytes == -1) {
  521. /*
  522. * starting from position 'pos' copy last cpy_num
  523. * items of SOURCE to begin of DEST
  524. */
  525. leaf_copy_items_entirely(dest_bi, src, LAST_TO_FIRST,
  526. pos, cpy_num);
  527. } else {
  528. /*
  529. * copy last cpy_num-1 items starting from position
  530. * 'pos+1' of the SOURCE to the begin of the DEST;
  531. */
  532. leaf_copy_items_entirely(dest_bi, src, LAST_TO_FIRST,
  533. pos + 1, cpy_num - 1);
  534. /*
  535. * copy part of the item which number is pos to
  536. * the begin of the DEST
  537. */
  538. leaf_item_bottle(dest_bi, src, LAST_TO_FIRST, pos,
  539. cpy_bytes);
  540. }
  541. }
  542. return i;
  543. }
  544. /*
  545. * there are types of coping: from S[0] to L[0], from S[0] to R[0],
  546. * from R[0] to L[0]. for each of these we have to define parent and
  547. * positions of destination and source buffers
  548. */
  549. static void leaf_define_dest_src_infos(int shift_mode, struct tree_balance *tb,
  550. struct buffer_info *dest_bi,
  551. struct buffer_info *src_bi,
  552. int *first_last,
  553. struct buffer_head *Snew)
  554. {
  555. memset(dest_bi, 0, sizeof(struct buffer_info));
  556. memset(src_bi, 0, sizeof(struct buffer_info));
  557. /* define dest, src, dest parent, dest position */
  558. switch (shift_mode) {
  559. case LEAF_FROM_S_TO_L: /* it is used in leaf_shift_left */
  560. src_bi->tb = tb;
  561. src_bi->bi_bh = PATH_PLAST_BUFFER(tb->tb_path);
  562. src_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, 0);
  563. /* src->b_item_order */
  564. src_bi->bi_position = PATH_H_B_ITEM_ORDER(tb->tb_path, 0);
  565. dest_bi->tb = tb;
  566. dest_bi->bi_bh = tb->L[0];
  567. dest_bi->bi_parent = tb->FL[0];
  568. dest_bi->bi_position = get_left_neighbor_position(tb, 0);
  569. *first_last = FIRST_TO_LAST;
  570. break;
  571. case LEAF_FROM_S_TO_R: /* it is used in leaf_shift_right */
  572. src_bi->tb = tb;
  573. src_bi->bi_bh = PATH_PLAST_BUFFER(tb->tb_path);
  574. src_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, 0);
  575. src_bi->bi_position = PATH_H_B_ITEM_ORDER(tb->tb_path, 0);
  576. dest_bi->tb = tb;
  577. dest_bi->bi_bh = tb->R[0];
  578. dest_bi->bi_parent = tb->FR[0];
  579. dest_bi->bi_position = get_right_neighbor_position(tb, 0);
  580. *first_last = LAST_TO_FIRST;
  581. break;
  582. case LEAF_FROM_R_TO_L: /* it is used in balance_leaf_when_delete */
  583. src_bi->tb = tb;
  584. src_bi->bi_bh = tb->R[0];
  585. src_bi->bi_parent = tb->FR[0];
  586. src_bi->bi_position = get_right_neighbor_position(tb, 0);
  587. dest_bi->tb = tb;
  588. dest_bi->bi_bh = tb->L[0];
  589. dest_bi->bi_parent = tb->FL[0];
  590. dest_bi->bi_position = get_left_neighbor_position(tb, 0);
  591. *first_last = FIRST_TO_LAST;
  592. break;
  593. case LEAF_FROM_L_TO_R: /* it is used in balance_leaf_when_delete */
  594. src_bi->tb = tb;
  595. src_bi->bi_bh = tb->L[0];
  596. src_bi->bi_parent = tb->FL[0];
  597. src_bi->bi_position = get_left_neighbor_position(tb, 0);
  598. dest_bi->tb = tb;
  599. dest_bi->bi_bh = tb->R[0];
  600. dest_bi->bi_parent = tb->FR[0];
  601. dest_bi->bi_position = get_right_neighbor_position(tb, 0);
  602. *first_last = LAST_TO_FIRST;
  603. break;
  604. case LEAF_FROM_S_TO_SNEW:
  605. src_bi->tb = tb;
  606. src_bi->bi_bh = PATH_PLAST_BUFFER(tb->tb_path);
  607. src_bi->bi_parent = PATH_H_PPARENT(tb->tb_path, 0);
  608. src_bi->bi_position = PATH_H_B_ITEM_ORDER(tb->tb_path, 0);
  609. dest_bi->tb = tb;
  610. dest_bi->bi_bh = Snew;
  611. dest_bi->bi_parent = NULL;
  612. dest_bi->bi_position = 0;
  613. *first_last = LAST_TO_FIRST;
  614. break;
  615. default:
  616. reiserfs_panic(sb_from_bi(src_bi), "vs-10250",
  617. "shift type is unknown (%d)", shift_mode);
  618. }
  619. RFALSE(!src_bi->bi_bh || !dest_bi->bi_bh,
  620. "vs-10260: mode==%d, source (%p) or dest (%p) buffer is initialized incorrectly",
  621. shift_mode, src_bi->bi_bh, dest_bi->bi_bh);
  622. }
  623. /*
  624. * copy mov_num items and mov_bytes of the (mov_num-1)th item to
  625. * neighbor. Delete them from source
  626. */
  627. int leaf_move_items(int shift_mode, struct tree_balance *tb, int mov_num,
  628. int mov_bytes, struct buffer_head *Snew)
  629. {
  630. int ret_value;
  631. struct buffer_info dest_bi, src_bi;
  632. int first_last;
  633. leaf_define_dest_src_infos(shift_mode, tb, &dest_bi, &src_bi,
  634. &first_last, Snew);
  635. ret_value =
  636. leaf_copy_items(&dest_bi, src_bi.bi_bh, first_last, mov_num,
  637. mov_bytes);
  638. leaf_delete_items(&src_bi, first_last,
  639. (first_last ==
  640. FIRST_TO_LAST) ? 0 : (B_NR_ITEMS(src_bi.bi_bh) -
  641. mov_num), mov_num, mov_bytes);
  642. return ret_value;
  643. }
  644. /*
  645. * Shift shift_num items (and shift_bytes of last shifted item if
  646. * shift_bytes != -1) from S[0] to L[0] and replace the delimiting key
  647. */
  648. int leaf_shift_left(struct tree_balance *tb, int shift_num, int shift_bytes)
  649. {
  650. struct buffer_head *S0 = PATH_PLAST_BUFFER(tb->tb_path);
  651. int i;
  652. /*
  653. * move shift_num (and shift_bytes bytes) items from S[0]
  654. * to left neighbor L[0]
  655. */
  656. i = leaf_move_items(LEAF_FROM_S_TO_L, tb, shift_num, shift_bytes, NULL);
  657. if (shift_num) {
  658. /* number of items in S[0] == 0 */
  659. if (B_NR_ITEMS(S0) == 0) {
  660. RFALSE(shift_bytes != -1,
  661. "vs-10270: S0 is empty now, but shift_bytes != -1 (%d)",
  662. shift_bytes);
  663. #ifdef CONFIG_REISERFS_CHECK
  664. if (tb->tb_mode == M_PASTE || tb->tb_mode == M_INSERT) {
  665. print_cur_tb("vs-10275");
  666. reiserfs_panic(tb->tb_sb, "vs-10275",
  667. "balance condition corrupted "
  668. "(%c)", tb->tb_mode);
  669. }
  670. #endif
  671. if (PATH_H_POSITION(tb->tb_path, 1) == 0)
  672. replace_key(tb, tb->CFL[0], tb->lkey[0],
  673. PATH_H_PPARENT(tb->tb_path, 0), 0);
  674. } else {
  675. /* replace lkey in CFL[0] by 0-th key from S[0]; */
  676. replace_key(tb, tb->CFL[0], tb->lkey[0], S0, 0);
  677. RFALSE((shift_bytes != -1 &&
  678. !(is_direntry_le_ih(item_head(S0, 0))
  679. && !ih_entry_count(item_head(S0, 0)))) &&
  680. (!op_is_left_mergeable
  681. (leaf_key(S0, 0), S0->b_size)),
  682. "vs-10280: item must be mergeable");
  683. }
  684. }
  685. return i;
  686. }
  687. /* CLEANING STOPPED HERE */
  688. /*
  689. * Shift shift_num (shift_bytes) items from S[0] to the right neighbor,
  690. * and replace the delimiting key
  691. */
  692. int leaf_shift_right(struct tree_balance *tb, int shift_num, int shift_bytes)
  693. {
  694. int ret_value;
  695. /*
  696. * move shift_num (and shift_bytes) items from S[0] to
  697. * right neighbor R[0]
  698. */
  699. ret_value =
  700. leaf_move_items(LEAF_FROM_S_TO_R, tb, shift_num, shift_bytes, NULL);
  701. /* replace rkey in CFR[0] by the 0-th key from R[0] */
  702. if (shift_num) {
  703. replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
  704. }
  705. return ret_value;
  706. }
  707. static void leaf_delete_items_entirely(struct buffer_info *bi,
  708. int first, int del_num);
  709. /*
  710. * If del_bytes == -1, starting from position 'first' delete del_num
  711. * items in whole in buffer CUR.
  712. * If not.
  713. * If last_first == 0. Starting from position 'first' delete del_num-1
  714. * items in whole. Delete part of body of the first item. Part defined by
  715. * del_bytes. Don't delete first item header
  716. * If last_first == 1. Starting from position 'first+1' delete del_num-1
  717. * items in whole. Delete part of body of the last item . Part defined by
  718. * del_bytes. Don't delete last item header.
  719. */
  720. void leaf_delete_items(struct buffer_info *cur_bi, int last_first,
  721. int first, int del_num, int del_bytes)
  722. {
  723. struct buffer_head *bh;
  724. int item_amount = B_NR_ITEMS(bh = cur_bi->bi_bh);
  725. RFALSE(!bh, "10155: bh is not defined");
  726. RFALSE(del_num < 0, "10160: del_num can not be < 0. del_num==%d",
  727. del_num);
  728. RFALSE(first < 0
  729. || first + del_num > item_amount,
  730. "10165: invalid number of first item to be deleted (%d) or "
  731. "no so much items (%d) to delete (only %d)", first,
  732. first + del_num, item_amount);
  733. if (del_num == 0)
  734. return;
  735. if (first == 0 && del_num == item_amount && del_bytes == -1) {
  736. make_empty_node(cur_bi);
  737. do_balance_mark_leaf_dirty(cur_bi->tb, bh, 0);
  738. return;
  739. }
  740. if (del_bytes == -1)
  741. /* delete del_num items beginning from item in position first */
  742. leaf_delete_items_entirely(cur_bi, first, del_num);
  743. else {
  744. if (last_first == FIRST_TO_LAST) {
  745. /*
  746. * delete del_num-1 items beginning from
  747. * item in position first
  748. */
  749. leaf_delete_items_entirely(cur_bi, first, del_num - 1);
  750. /*
  751. * delete the part of the first item of the bh
  752. * do not delete item header
  753. */
  754. leaf_cut_from_buffer(cur_bi, 0, 0, del_bytes);
  755. } else {
  756. struct item_head *ih;
  757. int len;
  758. /*
  759. * delete del_num-1 items beginning from
  760. * item in position first+1
  761. */
  762. leaf_delete_items_entirely(cur_bi, first + 1,
  763. del_num - 1);
  764. ih = item_head(bh, B_NR_ITEMS(bh) - 1);
  765. if (is_direntry_le_ih(ih))
  766. /* the last item is directory */
  767. /*
  768. * len = numbers of directory entries
  769. * in this item
  770. */
  771. len = ih_entry_count(ih);
  772. else
  773. /* len = body len of item */
  774. len = ih_item_len(ih);
  775. /*
  776. * delete the part of the last item of the bh
  777. * do not delete item header
  778. */
  779. leaf_cut_from_buffer(cur_bi, B_NR_ITEMS(bh) - 1,
  780. len - del_bytes, del_bytes);
  781. }
  782. }
  783. }
  784. /* insert item into the leaf node in position before */
  785. void leaf_insert_into_buf(struct buffer_info *bi, int before,
  786. struct item_head * const inserted_item_ih,
  787. const char * const inserted_item_body,
  788. int zeros_number)
  789. {
  790. struct buffer_head *bh = bi->bi_bh;
  791. int nr, free_space;
  792. struct block_head *blkh;
  793. struct item_head *ih;
  794. int i;
  795. int last_loc, unmoved_loc;
  796. char *to;
  797. blkh = B_BLK_HEAD(bh);
  798. nr = blkh_nr_item(blkh);
  799. free_space = blkh_free_space(blkh);
  800. /* check free space */
  801. RFALSE(free_space < ih_item_len(inserted_item_ih) + IH_SIZE,
  802. "vs-10170: not enough free space in block %z, new item %h",
  803. bh, inserted_item_ih);
  804. RFALSE(zeros_number > ih_item_len(inserted_item_ih),
  805. "vs-10172: zero number == %d, item length == %d",
  806. zeros_number, ih_item_len(inserted_item_ih));
  807. /* get item new item must be inserted before */
  808. ih = item_head(bh, before);
  809. /* prepare space for the body of new item */
  810. last_loc = nr ? ih_location(&ih[nr - before - 1]) : bh->b_size;
  811. unmoved_loc = before ? ih_location(ih - 1) : bh->b_size;
  812. memmove(bh->b_data + last_loc - ih_item_len(inserted_item_ih),
  813. bh->b_data + last_loc, unmoved_loc - last_loc);
  814. to = bh->b_data + unmoved_loc - ih_item_len(inserted_item_ih);
  815. memset(to, 0, zeros_number);
  816. to += zeros_number;
  817. /* copy body to prepared space */
  818. if (inserted_item_body)
  819. memmove(to, inserted_item_body,
  820. ih_item_len(inserted_item_ih) - zeros_number);
  821. else
  822. memset(to, '\0', ih_item_len(inserted_item_ih) - zeros_number);
  823. /* insert item header */
  824. memmove(ih + 1, ih, IH_SIZE * (nr - before));
  825. memmove(ih, inserted_item_ih, IH_SIZE);
  826. /* change locations */
  827. for (i = before; i < nr + 1; i++) {
  828. unmoved_loc -= ih_item_len(&ih[i - before]);
  829. put_ih_location(&ih[i - before], unmoved_loc);
  830. }
  831. /* sizes, free space, item number */
  832. set_blkh_nr_item(blkh, blkh_nr_item(blkh) + 1);
  833. set_blkh_free_space(blkh,
  834. free_space - (IH_SIZE +
  835. ih_item_len(inserted_item_ih)));
  836. do_balance_mark_leaf_dirty(bi->tb, bh, 1);
  837. if (bi->bi_parent) {
  838. struct disk_child *t_dc;
  839. t_dc = B_N_CHILD(bi->bi_parent, bi->bi_position);
  840. put_dc_size(t_dc,
  841. dc_size(t_dc) + (IH_SIZE +
  842. ih_item_len(inserted_item_ih)));
  843. do_balance_mark_internal_dirty(bi->tb, bi->bi_parent, 0);
  844. }
  845. }
  846. /*
  847. * paste paste_size bytes to affected_item_num-th item.
  848. * When item is a directory, this only prepare space for new entries
  849. */
  850. void leaf_paste_in_buffer(struct buffer_info *bi, int affected_item_num,
  851. int pos_in_item, int paste_size,
  852. const char *body, int zeros_number)
  853. {
  854. struct buffer_head *bh = bi->bi_bh;
  855. int nr, free_space;
  856. struct block_head *blkh;
  857. struct item_head *ih;
  858. int i;
  859. int last_loc, unmoved_loc;
  860. blkh = B_BLK_HEAD(bh);
  861. nr = blkh_nr_item(blkh);
  862. free_space = blkh_free_space(blkh);
  863. /* check free space */
  864. RFALSE(free_space < paste_size,
  865. "vs-10175: not enough free space: needed %d, available %d",
  866. paste_size, free_space);
  867. #ifdef CONFIG_REISERFS_CHECK
  868. if (zeros_number > paste_size) {
  869. struct super_block *sb = NULL;
  870. if (bi && bi->tb)
  871. sb = bi->tb->tb_sb;
  872. print_cur_tb("10177");
  873. reiserfs_panic(sb, "vs-10177",
  874. "zeros_number == %d, paste_size == %d",
  875. zeros_number, paste_size);
  876. }
  877. #endif /* CONFIG_REISERFS_CHECK */
  878. /* item to be appended */
  879. ih = item_head(bh, affected_item_num);
  880. last_loc = ih_location(&ih[nr - affected_item_num - 1]);
  881. unmoved_loc = affected_item_num ? ih_location(ih - 1) : bh->b_size;
  882. /* prepare space */
  883. memmove(bh->b_data + last_loc - paste_size, bh->b_data + last_loc,
  884. unmoved_loc - last_loc);
  885. /* change locations */
  886. for (i = affected_item_num; i < nr; i++)
  887. put_ih_location(&ih[i - affected_item_num],
  888. ih_location(&ih[i - affected_item_num]) -
  889. paste_size);
  890. if (body) {
  891. if (!is_direntry_le_ih(ih)) {
  892. if (!pos_in_item) {
  893. /* shift data to right */
  894. memmove(bh->b_data + ih_location(ih) +
  895. paste_size,
  896. bh->b_data + ih_location(ih),
  897. ih_item_len(ih));
  898. /* paste data in the head of item */
  899. memset(bh->b_data + ih_location(ih), 0,
  900. zeros_number);
  901. memcpy(bh->b_data + ih_location(ih) +
  902. zeros_number, body,
  903. paste_size - zeros_number);
  904. } else {
  905. memset(bh->b_data + unmoved_loc - paste_size, 0,
  906. zeros_number);
  907. memcpy(bh->b_data + unmoved_loc - paste_size +
  908. zeros_number, body,
  909. paste_size - zeros_number);
  910. }
  911. }
  912. } else
  913. memset(bh->b_data + unmoved_loc - paste_size, '\0', paste_size);
  914. put_ih_item_len(ih, ih_item_len(ih) + paste_size);
  915. /* change free space */
  916. set_blkh_free_space(blkh, free_space - paste_size);
  917. do_balance_mark_leaf_dirty(bi->tb, bh, 0);
  918. if (bi->bi_parent) {
  919. struct disk_child *t_dc =
  920. B_N_CHILD(bi->bi_parent, bi->bi_position);
  921. put_dc_size(t_dc, dc_size(t_dc) + paste_size);
  922. do_balance_mark_internal_dirty(bi->tb, bi->bi_parent, 0);
  923. }
  924. }
  925. /*
  926. * cuts DEL_COUNT entries beginning from FROM-th entry. Directory item
  927. * does not have free space, so it moves DEHs and remaining records as
  928. * necessary. Return value is size of removed part of directory item
  929. * in bytes.
  930. */
  931. static int leaf_cut_entries(struct buffer_head *bh,
  932. struct item_head *ih, int from, int del_count)
  933. {
  934. char *item;
  935. struct reiserfs_de_head *deh;
  936. int prev_record_offset; /* offset of record, that is (from-1)th */
  937. char *prev_record; /* */
  938. int cut_records_len; /* length of all removed records */
  939. int i;
  940. /*
  941. * make sure that item is directory and there are enough entries to
  942. * remove
  943. */
  944. RFALSE(!is_direntry_le_ih(ih), "10180: item is not directory item");
  945. RFALSE(ih_entry_count(ih) < from + del_count,
  946. "10185: item contains not enough entries: entry_count = %d, from = %d, to delete = %d",
  947. ih_entry_count(ih), from, del_count);
  948. if (del_count == 0)
  949. return 0;
  950. /* first byte of item */
  951. item = bh->b_data + ih_location(ih);
  952. /* entry head array */
  953. deh = B_I_DEH(bh, ih);
  954. /*
  955. * first byte of remaining entries, those are BEFORE cut entries
  956. * (prev_record) and length of all removed records (cut_records_len)
  957. */
  958. prev_record_offset =
  959. (from ? deh_location(&deh[from - 1]) : ih_item_len(ih));
  960. cut_records_len = prev_record_offset /*from_record */ -
  961. deh_location(&deh[from + del_count - 1]);
  962. prev_record = item + prev_record_offset;
  963. /* adjust locations of remaining entries */
  964. for (i = ih_entry_count(ih) - 1; i > from + del_count - 1; i--)
  965. put_deh_location(&deh[i],
  966. deh_location(&deh[i]) -
  967. (DEH_SIZE * del_count));
  968. for (i = 0; i < from; i++)
  969. put_deh_location(&deh[i],
  970. deh_location(&deh[i]) - (DEH_SIZE * del_count +
  971. cut_records_len));
  972. put_ih_entry_count(ih, ih_entry_count(ih) - del_count);
  973. /* shift entry head array and entries those are AFTER removed entries */
  974. memmove((char *)(deh + from),
  975. deh + from + del_count,
  976. prev_record - cut_records_len - (char *)(deh + from +
  977. del_count));
  978. /* shift records, those are BEFORE removed entries */
  979. memmove(prev_record - cut_records_len - DEH_SIZE * del_count,
  980. prev_record, item + ih_item_len(ih) - prev_record);
  981. return DEH_SIZE * del_count + cut_records_len;
  982. }
  983. /*
  984. * when cut item is part of regular file
  985. * pos_in_item - first byte that must be cut
  986. * cut_size - number of bytes to be cut beginning from pos_in_item
  987. *
  988. * when cut item is part of directory
  989. * pos_in_item - number of first deleted entry
  990. * cut_size - count of deleted entries
  991. */
  992. void leaf_cut_from_buffer(struct buffer_info *bi, int cut_item_num,
  993. int pos_in_item, int cut_size)
  994. {
  995. int nr;
  996. struct buffer_head *bh = bi->bi_bh;
  997. struct block_head *blkh;
  998. struct item_head *ih;
  999. int last_loc, unmoved_loc;
  1000. int i;
  1001. blkh = B_BLK_HEAD(bh);
  1002. nr = blkh_nr_item(blkh);
  1003. /* item head of truncated item */
  1004. ih = item_head(bh, cut_item_num);
  1005. if (is_direntry_le_ih(ih)) {
  1006. /* first cut entry () */
  1007. cut_size = leaf_cut_entries(bh, ih, pos_in_item, cut_size);
  1008. if (pos_in_item == 0) {
  1009. /* change key */
  1010. RFALSE(cut_item_num,
  1011. "when 0-th enrty of item is cut, that item must be first in the node, not %d-th",
  1012. cut_item_num);
  1013. /* change item key by key of first entry in the item */
  1014. set_le_ih_k_offset(ih, deh_offset(B_I_DEH(bh, ih)));
  1015. }
  1016. } else {
  1017. /* item is direct or indirect */
  1018. RFALSE(is_statdata_le_ih(ih), "10195: item is stat data");
  1019. RFALSE(pos_in_item && pos_in_item + cut_size != ih_item_len(ih),
  1020. "10200: invalid offset (%lu) or trunc_size (%lu) or ih_item_len (%lu)",
  1021. (long unsigned)pos_in_item, (long unsigned)cut_size,
  1022. (long unsigned)ih_item_len(ih));
  1023. /* shift item body to left if cut is from the head of item */
  1024. if (pos_in_item == 0) {
  1025. memmove(bh->b_data + ih_location(ih),
  1026. bh->b_data + ih_location(ih) + cut_size,
  1027. ih_item_len(ih) - cut_size);
  1028. /* change key of item */
  1029. if (is_direct_le_ih(ih))
  1030. set_le_ih_k_offset(ih,
  1031. le_ih_k_offset(ih) +
  1032. cut_size);
  1033. else {
  1034. set_le_ih_k_offset(ih,
  1035. le_ih_k_offset(ih) +
  1036. (cut_size / UNFM_P_SIZE) *
  1037. bh->b_size);
  1038. RFALSE(ih_item_len(ih) == cut_size
  1039. && get_ih_free_space(ih),
  1040. "10205: invalid ih_free_space (%h)", ih);
  1041. }
  1042. }
  1043. }
  1044. /* location of the last item */
  1045. last_loc = ih_location(&ih[nr - cut_item_num - 1]);
  1046. /* location of the item, which is remaining at the same place */
  1047. unmoved_loc = cut_item_num ? ih_location(ih - 1) : bh->b_size;
  1048. /* shift */
  1049. memmove(bh->b_data + last_loc + cut_size, bh->b_data + last_loc,
  1050. unmoved_loc - last_loc - cut_size);
  1051. /* change item length */
  1052. put_ih_item_len(ih, ih_item_len(ih) - cut_size);
  1053. if (is_indirect_le_ih(ih)) {
  1054. if (pos_in_item)
  1055. set_ih_free_space(ih, 0);
  1056. }
  1057. /* change locations */
  1058. for (i = cut_item_num; i < nr; i++)
  1059. put_ih_location(&ih[i - cut_item_num],
  1060. ih_location(&ih[i - cut_item_num]) + cut_size);
  1061. /* size, free space */
  1062. set_blkh_free_space(blkh, blkh_free_space(blkh) + cut_size);
  1063. do_balance_mark_leaf_dirty(bi->tb, bh, 0);
  1064. if (bi->bi_parent) {
  1065. struct disk_child *t_dc;
  1066. t_dc = B_N_CHILD(bi->bi_parent, bi->bi_position);
  1067. put_dc_size(t_dc, dc_size(t_dc) - cut_size);
  1068. do_balance_mark_internal_dirty(bi->tb, bi->bi_parent, 0);
  1069. }
  1070. }
  1071. /* delete del_num items from buffer starting from the first'th item */
  1072. static void leaf_delete_items_entirely(struct buffer_info *bi,
  1073. int first, int del_num)
  1074. {
  1075. struct buffer_head *bh = bi->bi_bh;
  1076. int nr;
  1077. int i, j;
  1078. int last_loc, last_removed_loc;
  1079. struct block_head *blkh;
  1080. struct item_head *ih;
  1081. RFALSE(bh == NULL, "10210: buffer is 0");
  1082. RFALSE(del_num < 0, "10215: del_num less than 0 (%d)", del_num);
  1083. if (del_num == 0)
  1084. return;
  1085. blkh = B_BLK_HEAD(bh);
  1086. nr = blkh_nr_item(blkh);
  1087. RFALSE(first < 0 || first + del_num > nr,
  1088. "10220: first=%d, number=%d, there is %d items", first, del_num,
  1089. nr);
  1090. if (first == 0 && del_num == nr) {
  1091. /* this does not work */
  1092. make_empty_node(bi);
  1093. do_balance_mark_leaf_dirty(bi->tb, bh, 0);
  1094. return;
  1095. }
  1096. ih = item_head(bh, first);
  1097. /* location of unmovable item */
  1098. j = (first == 0) ? bh->b_size : ih_location(ih - 1);
  1099. /* delete items */
  1100. last_loc = ih_location(&ih[nr - 1 - first]);
  1101. last_removed_loc = ih_location(&ih[del_num - 1]);
  1102. memmove(bh->b_data + last_loc + j - last_removed_loc,
  1103. bh->b_data + last_loc, last_removed_loc - last_loc);
  1104. /* delete item headers */
  1105. memmove(ih, ih + del_num, (nr - first - del_num) * IH_SIZE);
  1106. /* change item location */
  1107. for (i = first; i < nr - del_num; i++)
  1108. put_ih_location(&ih[i - first],
  1109. ih_location(&ih[i - first]) + (j -
  1110. last_removed_loc));
  1111. /* sizes, item number */
  1112. set_blkh_nr_item(blkh, blkh_nr_item(blkh) - del_num);
  1113. set_blkh_free_space(blkh,
  1114. blkh_free_space(blkh) + (j - last_removed_loc +
  1115. IH_SIZE * del_num));
  1116. do_balance_mark_leaf_dirty(bi->tb, bh, 0);
  1117. if (bi->bi_parent) {
  1118. struct disk_child *t_dc =
  1119. B_N_CHILD(bi->bi_parent, bi->bi_position);
  1120. put_dc_size(t_dc,
  1121. dc_size(t_dc) - (j - last_removed_loc +
  1122. IH_SIZE * del_num));
  1123. do_balance_mark_internal_dirty(bi->tb, bi->bi_parent, 0);
  1124. }
  1125. }
  1126. /*
  1127. * paste new_entry_count entries (new_dehs, records) into position
  1128. * before to item_num-th item
  1129. */
  1130. void leaf_paste_entries(struct buffer_info *bi,
  1131. int item_num,
  1132. int before,
  1133. int new_entry_count,
  1134. struct reiserfs_de_head *new_dehs,
  1135. const char *records, int paste_size)
  1136. {
  1137. struct item_head *ih;
  1138. char *item;
  1139. struct reiserfs_de_head *deh;
  1140. char *insert_point;
  1141. int i, old_entry_num;
  1142. struct buffer_head *bh = bi->bi_bh;
  1143. if (new_entry_count == 0)
  1144. return;
  1145. ih = item_head(bh, item_num);
  1146. /*
  1147. * make sure, that item is directory, and there are enough
  1148. * records in it
  1149. */
  1150. RFALSE(!is_direntry_le_ih(ih), "10225: item is not directory item");
  1151. RFALSE(ih_entry_count(ih) < before,
  1152. "10230: there are no entry we paste entries before. entry_count = %d, before = %d",
  1153. ih_entry_count(ih), before);
  1154. /* first byte of dest item */
  1155. item = bh->b_data + ih_location(ih);
  1156. /* entry head array */
  1157. deh = B_I_DEH(bh, ih);
  1158. /* new records will be pasted at this point */
  1159. insert_point =
  1160. item +
  1161. (before ? deh_location(&deh[before - 1])
  1162. : (ih_item_len(ih) - paste_size));
  1163. /* adjust locations of records that will be AFTER new records */
  1164. for (i = ih_entry_count(ih) - 1; i >= before; i--)
  1165. put_deh_location(&deh[i],
  1166. deh_location(&deh[i]) +
  1167. (DEH_SIZE * new_entry_count));
  1168. /* adjust locations of records that will be BEFORE new records */
  1169. for (i = 0; i < before; i++)
  1170. put_deh_location(&deh[i],
  1171. deh_location(&deh[i]) + paste_size);
  1172. old_entry_num = ih_entry_count(ih);
  1173. put_ih_entry_count(ih, ih_entry_count(ih) + new_entry_count);
  1174. /* prepare space for pasted records */
  1175. memmove(insert_point + paste_size, insert_point,
  1176. item + (ih_item_len(ih) - paste_size) - insert_point);
  1177. /* copy new records */
  1178. memcpy(insert_point + DEH_SIZE * new_entry_count, records,
  1179. paste_size - DEH_SIZE * new_entry_count);
  1180. /* prepare space for new entry heads */
  1181. deh += before;
  1182. memmove((char *)(deh + new_entry_count), deh,
  1183. insert_point - (char *)deh);
  1184. /* copy new entry heads */
  1185. deh = (struct reiserfs_de_head *)((char *)deh);
  1186. memcpy(deh, new_dehs, DEH_SIZE * new_entry_count);
  1187. /* set locations of new records */
  1188. for (i = 0; i < new_entry_count; i++) {
  1189. put_deh_location(&deh[i],
  1190. deh_location(&deh[i]) +
  1191. (-deh_location
  1192. (&new_dehs[new_entry_count - 1]) +
  1193. insert_point + DEH_SIZE * new_entry_count -
  1194. item));
  1195. }
  1196. /* change item key if necessary (when we paste before 0-th entry */
  1197. if (!before) {
  1198. set_le_ih_k_offset(ih, deh_offset(new_dehs));
  1199. }
  1200. #ifdef CONFIG_REISERFS_CHECK
  1201. {
  1202. int prev, next;
  1203. /* check record locations */
  1204. deh = B_I_DEH(bh, ih);
  1205. for (i = 0; i < ih_entry_count(ih); i++) {
  1206. next =
  1207. (i <
  1208. ih_entry_count(ih) -
  1209. 1) ? deh_location(&deh[i + 1]) : 0;
  1210. prev = (i != 0) ? deh_location(&deh[i - 1]) : 0;
  1211. if (prev && prev <= deh_location(&deh[i]))
  1212. reiserfs_error(sb_from_bi(bi), "vs-10240",
  1213. "directory item (%h) "
  1214. "corrupted (prev %a, "
  1215. "cur(%d) %a)",
  1216. ih, deh + i - 1, i, deh + i);
  1217. if (next && next >= deh_location(&deh[i]))
  1218. reiserfs_error(sb_from_bi(bi), "vs-10250",
  1219. "directory item (%h) "
  1220. "corrupted (cur(%d) %a, "
  1221. "next %a)",
  1222. ih, i, deh + i, deh + i + 1);
  1223. }
  1224. }
  1225. #endif
  1226. }