xref: /linux/fs/reiserfs/objectid.c (revision 3e8962be915bacc1d70e4849a075041838d60a3f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
31da177e4SLinus Torvalds  */
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <linux/config.h>
61da177e4SLinus Torvalds #include <linux/string.h>
71da177e4SLinus Torvalds #include <linux/random.h>
81da177e4SLinus Torvalds #include <linux/time.h>
91da177e4SLinus Torvalds #include <linux/reiserfs_fs.h>
101da177e4SLinus Torvalds #include <linux/reiserfs_fs_sb.h>
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds // find where objectid map starts
131da177e4SLinus Torvalds #define objectid_map(s,rs) (old_format_only (s) ? \
14*3e8962beSAl Viro                          (__le32 *)((struct reiserfs_super_block_v1 *)(rs) + 1) :\
15*3e8962beSAl Viro 			 (__le32 *)((rs) + 1))
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #ifdef CONFIG_REISERFS_CHECK
191da177e4SLinus Torvalds 
20*3e8962beSAl Viro static void check_objectid_map (struct super_block * s, __le32 * map)
211da177e4SLinus Torvalds {
221da177e4SLinus Torvalds     if (le32_to_cpu (map[0]) != 1)
231da177e4SLinus Torvalds 	reiserfs_panic (s, "vs-15010: check_objectid_map: map corrupted: %lx",
241da177e4SLinus Torvalds 			( long unsigned int ) le32_to_cpu (map[0]));
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds     // FIXME: add something else here
271da177e4SLinus Torvalds }
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #else
30*3e8962beSAl Viro static void check_objectid_map (struct super_block * s, __le32 * map)
311da177e4SLinus Torvalds {;}
321da177e4SLinus Torvalds #endif
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds /* When we allocate objectids we allocate the first unused objectid.
361da177e4SLinus Torvalds    Each sequence of objectids in use (the odd sequences) is followed
371da177e4SLinus Torvalds    by a sequence of objectids not in use (the even sequences).  We
381da177e4SLinus Torvalds    only need to record the last objectid in each of these sequences
391da177e4SLinus Torvalds    (both the odd and even sequences) in order to fully define the
401da177e4SLinus Torvalds    boundaries of the sequences.  A consequence of allocating the first
411da177e4SLinus Torvalds    objectid not in use is that under most conditions this scheme is
421da177e4SLinus Torvalds    extremely compact.  The exception is immediately after a sequence
431da177e4SLinus Torvalds    of operations which deletes a large number of objects of
441da177e4SLinus Torvalds    non-sequential objectids, and even then it will become compact
451da177e4SLinus Torvalds    again as soon as more objects are created.  Note that many
461da177e4SLinus Torvalds    interesting optimizations of layout could result from complicating
471da177e4SLinus Torvalds    objectid assignment, but we have deferred making them for now. */
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds /* get unique object identifier */
511da177e4SLinus Torvalds __u32 reiserfs_get_unused_objectid (struct reiserfs_transaction_handle *th)
521da177e4SLinus Torvalds {
531da177e4SLinus Torvalds     struct super_block * s = th->t_super;
541da177e4SLinus Torvalds     struct reiserfs_super_block * rs = SB_DISK_SUPER_BLOCK (s);
55*3e8962beSAl Viro     __le32 * map = objectid_map (s, rs);
561da177e4SLinus Torvalds     __u32 unused_objectid;
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds     BUG_ON (!th->t_trans_id);
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds     check_objectid_map (s, map);
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds     reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
631da177e4SLinus Torvalds                                 /* comment needed -Hans */
641da177e4SLinus Torvalds     unused_objectid = le32_to_cpu (map[1]);
651da177e4SLinus Torvalds     if (unused_objectid == U32_MAX) {
661da177e4SLinus Torvalds 	reiserfs_warning (s, "%s: no more object ids", __FUNCTION__);
671da177e4SLinus Torvalds 	reiserfs_restore_prepared_buffer(s, SB_BUFFER_WITH_SB(s)) ;
681da177e4SLinus Torvalds 	return 0;
691da177e4SLinus Torvalds     }
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds     /* This incrementation allocates the first unused objectid. That
721da177e4SLinus Torvalds        is to say, the first entry on the objectid map is the first
731da177e4SLinus Torvalds        unused objectid, and by incrementing it we use it.  See below
741da177e4SLinus Torvalds        where we check to see if we eliminated a sequence of unused
751da177e4SLinus Torvalds        objectids.... */
761da177e4SLinus Torvalds     map[1] = cpu_to_le32 (unused_objectid + 1);
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds     /* Now we check to see if we eliminated the last remaining member of
791da177e4SLinus Torvalds        the first even sequence (and can eliminate the sequence by
801da177e4SLinus Torvalds        eliminating its last objectid from oids), and can collapse the
811da177e4SLinus Torvalds        first two odd sequences into one sequence.  If so, then the net
821da177e4SLinus Torvalds        result is to eliminate a pair of objectids from oids.  We do this
831da177e4SLinus Torvalds        by shifting the entire map to the left. */
841da177e4SLinus Torvalds     if (sb_oid_cursize(rs) > 2 && map[1] == map[2]) {
851da177e4SLinus Torvalds 	memmove (map + 1, map + 3, (sb_oid_cursize(rs) - 3) * sizeof(__u32));
861da177e4SLinus Torvalds         set_sb_oid_cursize( rs, sb_oid_cursize(rs) - 2 );
871da177e4SLinus Torvalds     }
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds     journal_mark_dirty(th, s, SB_BUFFER_WITH_SB (s));
901da177e4SLinus Torvalds     return unused_objectid;
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /* makes object identifier unused */
951da177e4SLinus Torvalds void reiserfs_release_objectid (struct reiserfs_transaction_handle *th,
961da177e4SLinus Torvalds 				__u32 objectid_to_release)
971da177e4SLinus Torvalds {
981da177e4SLinus Torvalds     struct super_block * s = th->t_super;
991da177e4SLinus Torvalds     struct reiserfs_super_block * rs = SB_DISK_SUPER_BLOCK (s);
100*3e8962beSAl Viro     __le32 * map = objectid_map (s, rs);
1011da177e4SLinus Torvalds     int i = 0;
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds     BUG_ON (!th->t_trans_id);
1041da177e4SLinus Torvalds     //return;
1051da177e4SLinus Torvalds     check_objectid_map (s, map);
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds     reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
1081da177e4SLinus Torvalds     journal_mark_dirty(th, s, SB_BUFFER_WITH_SB (s));
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds     /* start at the beginning of the objectid map (i = 0) and go to
1111da177e4SLinus Torvalds        the end of it (i = disk_sb->s_oid_cursize).  Linear search is
1121da177e4SLinus Torvalds        what we use, though it is possible that binary search would be
1131da177e4SLinus Torvalds        more efficient after performing lots of deletions (which is
1141da177e4SLinus Torvalds        when oids is large.)  We only check even i's. */
1151da177e4SLinus Torvalds     while (i < sb_oid_cursize(rs)) {
1161da177e4SLinus Torvalds 	if (objectid_to_release == le32_to_cpu (map[i])) {
1171da177e4SLinus Torvalds 	    /* This incrementation unallocates the objectid. */
1181da177e4SLinus Torvalds 	    //map[i]++;
1191da177e4SLinus Torvalds 	    map[i] = cpu_to_le32 (le32_to_cpu (map[i]) + 1);
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	    /* Did we unallocate the last member of an odd sequence, and can shrink oids? */
1221da177e4SLinus Torvalds 	    if (map[i] == map[i+1]) {
1231da177e4SLinus Torvalds 		/* shrink objectid map */
1241da177e4SLinus Torvalds 		memmove (map + i, map + i + 2,
1251da177e4SLinus Torvalds 			 (sb_oid_cursize(rs) - i - 2) * sizeof (__u32));
1261da177e4SLinus Torvalds 		//disk_sb->s_oid_cursize -= 2;
1271da177e4SLinus Torvalds                 set_sb_oid_cursize( rs, sb_oid_cursize(rs) - 2 );
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 		RFALSE( sb_oid_cursize(rs) < 2 ||
1301da177e4SLinus Torvalds 		        sb_oid_cursize(rs) > sb_oid_maxsize(rs),
1311da177e4SLinus Torvalds 		        "vs-15005: objectid map corrupted cur_size == %d (max == %d)",
1321da177e4SLinus Torvalds                         sb_oid_cursize(rs), sb_oid_maxsize(rs));
1331da177e4SLinus Torvalds 	    }
1341da177e4SLinus Torvalds 	    return;
1351da177e4SLinus Torvalds 	}
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	if (objectid_to_release > le32_to_cpu (map[i]) &&
1381da177e4SLinus Torvalds 	    objectid_to_release < le32_to_cpu (map[i + 1])) {
1391da177e4SLinus Torvalds 	    /* size of objectid map is not changed */
1401da177e4SLinus Torvalds 	    if (objectid_to_release + 1 == le32_to_cpu (map[i + 1])) {
1411da177e4SLinus Torvalds 		//objectid_map[i+1]--;
1421da177e4SLinus Torvalds 		map[i + 1] = cpu_to_le32 (le32_to_cpu (map[i + 1]) - 1);
1431da177e4SLinus Torvalds 		return;
1441da177e4SLinus Torvalds 	    }
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds             /* JDM comparing two little-endian values for equality -- safe */
1471da177e4SLinus Torvalds 	if (sb_oid_cursize(rs) == sb_oid_maxsize(rs)) {
1481da177e4SLinus Torvalds 		/* objectid map must be expanded, but there is no space */
1491da177e4SLinus Torvalds 		PROC_INFO_INC( s, leaked_oid );
1501da177e4SLinus Torvalds 		return;
1511da177e4SLinus Torvalds 	}
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	    /* expand the objectid map*/
1541da177e4SLinus Torvalds 	    memmove (map + i + 3, map + i + 1,
1551da177e4SLinus Torvalds 		     (sb_oid_cursize(rs) - i - 1) * sizeof(__u32));
1561da177e4SLinus Torvalds 	    map[i + 1] = cpu_to_le32 (objectid_to_release);
1571da177e4SLinus Torvalds 	    map[i + 2] = cpu_to_le32 (objectid_to_release + 1);
1581da177e4SLinus Torvalds             set_sb_oid_cursize( rs, sb_oid_cursize(rs) + 2 );
1591da177e4SLinus Torvalds 	    return;
1601da177e4SLinus Torvalds 	}
1611da177e4SLinus Torvalds 	i += 2;
1621da177e4SLinus Torvalds     }
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds     reiserfs_warning (s, "vs-15011: reiserfs_release_objectid: tried to free free object id (%lu)",
1651da177e4SLinus Torvalds 		      ( long unsigned ) objectid_to_release);
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds int reiserfs_convert_objectid_map_v1(struct super_block *s) {
1701da177e4SLinus Torvalds     struct reiserfs_super_block *disk_sb = SB_DISK_SUPER_BLOCK (s);
1711da177e4SLinus Torvalds     int cur_size = sb_oid_cursize(disk_sb);
1721da177e4SLinus Torvalds     int new_size = (s->s_blocksize - SB_SIZE) / sizeof(__u32) / 2 * 2 ;
1731da177e4SLinus Torvalds     int old_max = sb_oid_maxsize(disk_sb);
1741da177e4SLinus Torvalds     struct reiserfs_super_block_v1 *disk_sb_v1 ;
175*3e8962beSAl Viro     __le32 *objectid_map, *new_objectid_map ;
1761da177e4SLinus Torvalds     int i ;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds     disk_sb_v1=(struct reiserfs_super_block_v1 *)(SB_BUFFER_WITH_SB(s)->b_data);
179*3e8962beSAl Viro     objectid_map = (__le32 *)(disk_sb_v1 + 1) ;
180*3e8962beSAl Viro     new_objectid_map = (__le32 *)(disk_sb + 1) ;
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds     if (cur_size > new_size) {
1831da177e4SLinus Torvalds 	/* mark everyone used that was listed as free at the end of the objectid
1841da177e4SLinus Torvalds 	** map
1851da177e4SLinus Torvalds 	*/
1861da177e4SLinus Torvalds 	objectid_map[new_size - 1] = objectid_map[cur_size - 1] ;
1871da177e4SLinus Torvalds 	set_sb_oid_cursize(disk_sb,new_size) ;
1881da177e4SLinus Torvalds     }
1891da177e4SLinus Torvalds     /* move the smaller objectid map past the end of the new super */
1901da177e4SLinus Torvalds     for (i = new_size - 1 ; i >= 0 ; i--) {
1911da177e4SLinus Torvalds         objectid_map[i + (old_max - new_size)] = objectid_map[i] ;
1921da177e4SLinus Torvalds     }
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds     /* set the max size so we don't overflow later */
1961da177e4SLinus Torvalds     set_sb_oid_maxsize(disk_sb,new_size) ;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds     /* Zero out label and generate random UUID */
1991da177e4SLinus Torvalds     memset(disk_sb->s_label, 0, sizeof(disk_sb->s_label)) ;
2001da177e4SLinus Torvalds     generate_random_uuid(disk_sb->s_uuid);
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds     /* finally, zero out the unused chunk of the new super */
2031da177e4SLinus Torvalds     memset(disk_sb->s_unused, 0, sizeof(disk_sb->s_unused)) ;
2041da177e4SLinus Torvalds     return 0 ;
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
207