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