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