objectid.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
  3. */
  4. #include <linux/config.h>
  5. #include <linux/string.h>
  6. #include <linux/random.h>
  7. #include <linux/time.h>
  8. #include <linux/reiserfs_fs.h>
  9. #include <linux/reiserfs_fs_sb.h>
  10. // find where objectid map starts
  11. #define objectid_map(s,rs) (old_format_only (s) ? \
  12. (__le32 *)((struct reiserfs_super_block_v1 *)(rs) + 1) :\
  13. (__le32 *)((rs) + 1))
  14. #ifdef CONFIG_REISERFS_CHECK
  15. static void check_objectid_map(struct super_block *s, __le32 * map)
  16. {
  17. if (le32_to_cpu(map[0]) != 1)
  18. reiserfs_panic(s,
  19. "vs-15010: check_objectid_map: map corrupted: %lx",
  20. (long unsigned int)le32_to_cpu(map[0]));
  21. // FIXME: add something else here
  22. }
  23. #else
  24. static void check_objectid_map(struct super_block *s, __le32 * map)
  25. {;
  26. }
  27. #endif
  28. /* When we allocate objectids we allocate the first unused objectid.
  29. Each sequence of objectids in use (the odd sequences) is followed
  30. by a sequence of objectids not in use (the even sequences). We
  31. only need to record the last objectid in each of these sequences
  32. (both the odd and even sequences) in order to fully define the
  33. boundaries of the sequences. A consequence of allocating the first
  34. objectid not in use is that under most conditions this scheme is
  35. extremely compact. The exception is immediately after a sequence
  36. of operations which deletes a large number of objects of
  37. non-sequential objectids, and even then it will become compact
  38. again as soon as more objects are created. Note that many
  39. interesting optimizations of layout could result from complicating
  40. objectid assignment, but we have deferred making them for now. */
  41. /* get unique object identifier */
  42. __u32 reiserfs_get_unused_objectid(struct reiserfs_transaction_handle *th)
  43. {
  44. struct super_block *s = th->t_super;
  45. struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(s);
  46. __le32 *map = objectid_map(s, rs);
  47. __u32 unused_objectid;
  48. BUG_ON(!th->t_trans_id);
  49. check_objectid_map(s, map);
  50. reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
  51. /* comment needed -Hans */
  52. unused_objectid = le32_to_cpu(map[1]);
  53. if (unused_objectid == U32_MAX) {
  54. reiserfs_warning(s, "%s: no more object ids", __FUNCTION__);
  55. reiserfs_restore_prepared_buffer(s, SB_BUFFER_WITH_SB(s));
  56. return 0;
  57. }
  58. /* This incrementation allocates the first unused objectid. That
  59. is to say, the first entry on the objectid map is the first
  60. unused objectid, and by incrementing it we use it. See below
  61. where we check to see if we eliminated a sequence of unused
  62. objectids.... */
  63. map[1] = cpu_to_le32(unused_objectid + 1);
  64. /* Now we check to see if we eliminated the last remaining member of
  65. the first even sequence (and can eliminate the sequence by
  66. eliminating its last objectid from oids), and can collapse the
  67. first two odd sequences into one sequence. If so, then the net
  68. result is to eliminate a pair of objectids from oids. We do this
  69. by shifting the entire map to the left. */
  70. if (sb_oid_cursize(rs) > 2 && map[1] == map[2]) {
  71. memmove(map + 1, map + 3,
  72. (sb_oid_cursize(rs) - 3) * sizeof(__u32));
  73. set_sb_oid_cursize(rs, sb_oid_cursize(rs) - 2);
  74. }
  75. journal_mark_dirty(th, s, SB_BUFFER_WITH_SB(s));
  76. return unused_objectid;
  77. }
  78. /* makes object identifier unused */
  79. void reiserfs_release_objectid(struct reiserfs_transaction_handle *th,
  80. __u32 objectid_to_release)
  81. {
  82. struct super_block *s = th->t_super;
  83. struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(s);
  84. __le32 *map = objectid_map(s, rs);
  85. int i = 0;
  86. BUG_ON(!th->t_trans_id);
  87. //return;
  88. check_objectid_map(s, map);
  89. reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
  90. journal_mark_dirty(th, s, SB_BUFFER_WITH_SB(s));
  91. /* start at the beginning of the objectid map (i = 0) and go to
  92. the end of it (i = disk_sb->s_oid_cursize). Linear search is
  93. what we use, though it is possible that binary search would be
  94. more efficient after performing lots of deletions (which is
  95. when oids is large.) We only check even i's. */
  96. while (i < sb_oid_cursize(rs)) {
  97. if (objectid_to_release == le32_to_cpu(map[i])) {
  98. /* This incrementation unallocates the objectid. */
  99. //map[i]++;
  100. map[i] = cpu_to_le32(le32_to_cpu(map[i]) + 1);
  101. /* Did we unallocate the last member of an odd sequence, and can shrink oids? */
  102. if (map[i] == map[i + 1]) {
  103. /* shrink objectid map */
  104. memmove(map + i, map + i + 2,
  105. (sb_oid_cursize(rs) - i -
  106. 2) * sizeof(__u32));
  107. //disk_sb->s_oid_cursize -= 2;
  108. set_sb_oid_cursize(rs, sb_oid_cursize(rs) - 2);
  109. RFALSE(sb_oid_cursize(rs) < 2 ||
  110. sb_oid_cursize(rs) > sb_oid_maxsize(rs),
  111. "vs-15005: objectid map corrupted cur_size == %d (max == %d)",
  112. sb_oid_cursize(rs), sb_oid_maxsize(rs));
  113. }
  114. return;
  115. }
  116. if (objectid_to_release > le32_to_cpu(map[i]) &&
  117. objectid_to_release < le32_to_cpu(map[i + 1])) {
  118. /* size of objectid map is not changed */
  119. if (objectid_to_release + 1 == le32_to_cpu(map[i + 1])) {
  120. //objectid_map[i+1]--;
  121. map[i + 1] =
  122. cpu_to_le32(le32_to_cpu(map[i + 1]) - 1);
  123. return;
  124. }
  125. /* JDM comparing two little-endian values for equality -- safe */
  126. if (sb_oid_cursize(rs) == sb_oid_maxsize(rs)) {
  127. /* objectid map must be expanded, but there is no space */
  128. PROC_INFO_INC(s, leaked_oid);
  129. return;
  130. }
  131. /* expand the objectid map */
  132. memmove(map + i + 3, map + i + 1,
  133. (sb_oid_cursize(rs) - i - 1) * sizeof(__u32));
  134. map[i + 1] = cpu_to_le32(objectid_to_release);
  135. map[i + 2] = cpu_to_le32(objectid_to_release + 1);
  136. set_sb_oid_cursize(rs, sb_oid_cursize(rs) + 2);
  137. return;
  138. }
  139. i += 2;
  140. }
  141. reiserfs_warning(s,
  142. "vs-15011: reiserfs_release_objectid: tried to free free object id (%lu)",
  143. (long unsigned)objectid_to_release);
  144. }
  145. int reiserfs_convert_objectid_map_v1(struct super_block *s)
  146. {
  147. struct reiserfs_super_block *disk_sb = SB_DISK_SUPER_BLOCK(s);
  148. int cur_size = sb_oid_cursize(disk_sb);
  149. int new_size = (s->s_blocksize - SB_SIZE) / sizeof(__u32) / 2 * 2;
  150. int old_max = sb_oid_maxsize(disk_sb);
  151. struct reiserfs_super_block_v1 *disk_sb_v1;
  152. __le32 *objectid_map, *new_objectid_map;
  153. int i;
  154. disk_sb_v1 =
  155. (struct reiserfs_super_block_v1 *)(SB_BUFFER_WITH_SB(s)->b_data);
  156. objectid_map = (__le32 *) (disk_sb_v1 + 1);
  157. new_objectid_map = (__le32 *) (disk_sb + 1);
  158. if (cur_size > new_size) {
  159. /* mark everyone used that was listed as free at the end of the objectid
  160. ** map
  161. */
  162. objectid_map[new_size - 1] = objectid_map[cur_size - 1];
  163. set_sb_oid_cursize(disk_sb, new_size);
  164. }
  165. /* move the smaller objectid map past the end of the new super */
  166. for (i = new_size - 1; i >= 0; i--) {
  167. objectid_map[i + (old_max - new_size)] = objectid_map[i];
  168. }
  169. /* set the max size so we don't overflow later */
  170. set_sb_oid_maxsize(disk_sb, new_size);
  171. /* Zero out label and generate random UUID */
  172. memset(disk_sb->s_label, 0, sizeof(disk_sb->s_label));
  173. generate_random_uuid(disk_sb->s_uuid);
  174. /* finally, zero out the unused chunk of the new super */
  175. memset(disk_sb->s_unused, 0, sizeof(disk_sb->s_unused));
  176. return 0;
  177. }