How to Interpret and Change GAM/SGAM Bits

Below is the physical view of 1st GAM page, on page 2 or offset 0x4000.

gb1

The GAM allocation bits starts at offset 0xC2 onward relative to the start offset on current page (i.e. 0x40C2).

Viewing this page of these value yields :

DBCC TRACEON(3604)
DBCC PAGE (‘test’, 1, 2, 3)

GAM: Extent Alloc Status @0x4C1820C2
————————————
(1:0)        – (1:56)       =     ALLOCATED
(1:64)       – (1:120)      = NOT ALLOCATED

Now, if I change the value at offset 0xC2 from 0x00 to 0x01 :

GAM: Extent Alloc Status @0x4F4520C2
————————————
(1:0)        –              = NOT ALLOCATED
(1:8)        – (1:56)       =     ALLOCATED
(1:64)       – (1:120)      = NOT ALLOCATED
Again, if I change the value at offset 0xC2 from 0x00 to 0x10 :

GAM: Extent Alloc Status @0x4F41C0C2
————————————
(1:0)        – (1:24)       =     ALLOCATED
(1:32)       –              = NOT ALLOCATED
(1:40)       – (1:56)       =     ALLOCATED
(1:64)       – (1:120)      = NOT ALLOCATED

Again, if I change the value at offset 0xC2 from 0x00 to 0x11 :

GAM: Extent Alloc Status @0x4C7500C2
————————————
(1:0)        –              = NOT ALLOCATED
(1:8)        – (1:24)       =     ALLOCATED
(1:32)       –              = NOT ALLOCATED
(1:40)       – (1:56)       =     ALLOCATED
(1:64)       – (1:120)      = NOT ALLOCATED

From this pattern, we can derive some general pattern as follows :

Using conventions from SQL 2000 documentation, bit 0 means allocated, and bit 1 means Not Allocated, and each bit span one extent (8 pages) :

gb2

0x00 = 0000 0000 becomes 0000 0000
0x01 = 0000 0001 becomes 1000 0000
0x10 = 0001 0000 becomes 0000 1000
0x11 = 0001 0001 becomes 1000 1000

So, to determine the allocation bits, each byte is transposed, then each nibble in a transposed byte is is transposed again.

To prove it, let’s say I want to make the extent 16-23 is allocated and the rest of extend is not allocated, then the value would be :

1101 1111 transposed to 1111 1101 then to 1111 1011 = 0xFB.

Apply this value to the physical file and we have :

GAM: Extent Alloc Status @0x52BE60C2
————————————
(1:0)        – (1:8)        = NOT ALLOCATED        
(1:16)       –              =     ALLOCATED        
(1:24)       – (1:120)      = NOT ALLOCATED        

As I expected. Using this general pattern, then we can allocate/de-allocate any extent at will, but use this power with care, or else, it will damage / overwrite your existing data 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: