objectid.c 7.0 KB

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