3 /usr/src/ext2ed/blockbitmap_com.c
5 A part of the extended file system 2 disk editor.
7 -------------------------
8 Handles the block bitmap.
9 -------------------------
11 This file implements the commands which are specific to the blockbitmap type.
13 First written on: July 5 1995
15 Copyright (C) 1995 Gadi Oxman
28 The functions in this file use the flobal structure block_bitmap_info. This structure contains the current
29 position in the bitmap.
33 void type_ext2_block_bitmap___entry (char *command_line
)
37 This function changes the current entry in the bitmap. It just changes the entry_num variable in block_bitmap_info
38 and dispatches a show command to show the new entry.
43 unsigned long entry_num
;
44 char *ptr
,buffer
[80];
48 ptr
=parse_word (command_line
,buffer
); /* Get the requested entry */
50 wprintw (command_win
,"Error - No argument specified\n");
51 refresh_command_win (); return;
53 ptr
=parse_word (ptr
,buffer
);
55 entry_num
=atol (buffer
);
58 if (entry_num
>= file_system_info
.super_block
.s_blocks_per_group
) { /* Check if it is a valid entry number */
60 wprintw (command_win
,"Error - Entry number out of bounds\n");
61 refresh_command_win ();return;
66 block_bitmap_info
.entry_num
=entry_num
; /* If it is, just change entry_num and */
67 strcpy (buffer
,"show");dispatch (buffer
); /* dispatch a show command */
70 void type_ext2_block_bitmap___next (char *command_line
)
74 This function passes to the next entry in the bitmap. We just call the above entry command.
80 char *ptr
,buffer
[80];
82 ptr
=parse_word (command_line
,buffer
);
84 ptr
=parse_word (ptr
,buffer
);
85 entry_offset
=atol (buffer
);
88 sprintf (buffer
,"entry %ld",block_bitmap_info
.entry_num
+entry_offset
);
92 void type_ext2_block_bitmap___prev (char *command_line
)
96 char *ptr
,buffer
[80];
98 ptr
=parse_word (command_line
,buffer
);
100 ptr
=parse_word (ptr
,buffer
);
101 entry_offset
=atol (buffer
);
104 sprintf (buffer
,"entry %ld",block_bitmap_info
.entry_num
-entry_offset
);
108 void type_ext2_block_bitmap___allocate (char *command_line
)
112 This function starts allocating block from the current position. Allocating involves setting the correct bits
113 in the bitmap. This function is a vector version of allocate_block below - We just run on the blocks that
114 we need to allocate, and call allocate_block for each one.
119 long entry_num
,num
=1;
120 char *ptr
,buffer
[80];
122 ptr
=parse_word (command_line
,buffer
); /* Get the number of blocks to allocate */
124 ptr
=parse_word (ptr
,buffer
);
128 entry_num
=block_bitmap_info
.entry_num
;
129 /* Check for limits */
130 if (num
> file_system_info
.super_block
.s_blocks_per_group
-entry_num
) {
131 wprintw (command_win
,"Error - There aren't that much blocks in the group\n");
132 refresh_command_win ();return;
135 while (num
) { /* And call allocate_block */
136 allocate_block (entry_num
); /* for each block */
140 dispatch ("show"); /* Show the result */
143 void type_ext2_block_bitmap___deallocate (char *command_line
)
145 /* This is the opposite of the above function - We call deallocate_block instead of allocate_block */
148 long entry_num
,num
=1;
149 char *ptr
,buffer
[80];
151 ptr
=parse_word (command_line
,buffer
);
153 ptr
=parse_word (ptr
,buffer
);
157 entry_num
=block_bitmap_info
.entry_num
;
158 if (num
> file_system_info
.super_block
.s_blocks_per_group
-entry_num
) {
159 wprintw (command_win
,"Error - There aren't that much blocks in the group\n");
160 refresh_command_win ();return;
164 deallocate_block (entry_num
);
172 void allocate_block (long entry_num
)
174 /* In this function we convert the bit number into the right byte and inner bit positions. */
177 unsigned char bit_mask
=1;
180 byte_offset
=entry_num
/8; /* Find the correct byte - entry_num/8 */
181 /* The position inside the byte is entry_num %8 */
182 for (j
=0;j
<entry_num
%8;j
++)
183 bit_mask
*=2; /* Generate the or mask - 1 at the right place */
184 type_data
.u
.buffer
[byte_offset
] |= bit_mask
; /* And apply it */
187 void deallocate_block (long entry_num
)
189 /* This is the opposite of allocate_block above. We use an and mask instead of an or mask. */
192 unsigned char bit_mask
=1;
195 byte_offset
=entry_num
/8;
196 for (j
=0;j
<entry_num
%8;j
++)
200 type_data
.u
.buffer
[byte_offset
] &= bit_mask
;
203 void type_ext2_block_bitmap___show (char *command_line
)
207 We show the bitmap as a series of bits, grouped at 8-bit intervals. We display 8 such groups on each line.
208 The current position (as known from block_bitmap_info.entry_num) is highlighted.
215 unsigned long block_num
,entry_num
;
217 ptr
=type_data
.u
.buffer
;
218 show_pad_info
.line
=0;show_pad_info
.max_line
=-1;
220 wmove (show_pad
,0,0);
221 for (i
=0,entry_num
=0;i
<file_system_info
.super_block
.s_blocks_per_group
/8;i
++,ptr
++) {
222 for (j
=1;j
<=128;j
*=2) { /* j contains the and bit mask */
223 if (entry_num
==block_bitmap_info
.entry_num
) { /* Highlight the current entry */
224 wattrset (show_pad
,A_REVERSE
);
225 show_pad_info
.line
=show_pad_info
.max_line
-show_pad_info
.display_lines
/2;
228 if ((*ptr
) & j
) /* Apply the mask */
229 wprintw (show_pad
,"1");
231 wprintw (show_pad
,"0");
233 if (entry_num
==block_bitmap_info
.entry_num
)
234 wattrset (show_pad
,A_NORMAL
);
236 entry_num
++; /* Pass to the next entry */
238 wprintw (show_pad
," ");
239 if (i
%8==7) { /* Display 8 groups in a row */
240 wprintw (show_pad
,"\n");
241 show_pad_info
.max_line
++;
246 show_info (); /* Show the usual information */
248 /* Show the group number */
249 wmove (show_win
,1,0);
250 wprintw (show_win
,"Block bitmap of block group %ld\n",block_bitmap_info
.group_num
);
251 /* Show the block number */
253 block_num
=block_bitmap_info
.entry_num
+block_bitmap_info
.group_num
*file_system_info
.super_block
.s_blocks_per_group
;
254 block_num
+=file_system_info
.super_block
.s_first_data_block
;
256 wprintw (show_win
,"Status of block %ld - ",block_num
); /* and the allocation status */
257 ptr
=type_data
.u
.buffer
+block_bitmap_info
.entry_num
/8;
259 for (i
=block_bitmap_info
.entry_num
% 8;i
>0;i
--)
262 wprintw (show_win
,"Allocated\n");
264 wprintw (show_win
,"Free\n");