objectid.c 6.9 KB

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