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
27 The functions in this file use the flobal structure block_bitmap_info. This structure contains the current
28 position in the bitmap.
32 void type_ext2_block_bitmap___entry (char *command_line)
36 This function changes the current entry in the bitmap. It just changes the entry_num variable in block_bitmap_info
37 and dispatches a show command to show the new entry.
42 unsigned long entry_num;
43 char *ptr,buffer [80];
47 ptr=parse_word (command_line,buffer); /* Get the requested entry */
49 wprintw (command_win,"Error - No argument specified\n");
50 refresh_command_win (); return;
52 ptr=parse_word (ptr,buffer);
54 entry_num=atol (buffer);
57 if (entry_num >= file_system_info.super_block.s_blocks_per_group) { /* Check if it is a valid entry number */
59 wprintw (command_win,"Error - Entry number out of bounds\n");
60 refresh_command_win ();return;
65 block_bitmap_info.entry_num=entry_num; /* If it is, just change entry_num and */
66 strcpy (buffer,"show");dispatch (buffer); /* dispatch a show command */
69 void type_ext2_block_bitmap___next (char *command_line)
73 This function passes to the next entry in the bitmap. We just call the above entry command.
79 char *ptr,buffer [80];
81 ptr=parse_word (command_line,buffer);
83 ptr=parse_word (ptr,buffer);
84 entry_offset=atol (buffer);
87 sprintf (buffer,"entry %ld",block_bitmap_info.entry_num+entry_offset);
91 void type_ext2_block_bitmap___prev (char *command_line)
95 char *ptr,buffer [80];
97 ptr=parse_word (command_line,buffer);
99 ptr=parse_word (ptr,buffer);
100 entry_offset=atol (buffer);
103 sprintf (buffer,"entry %ld",block_bitmap_info.entry_num-entry_offset);
107 void type_ext2_block_bitmap___allocate (char *command_line)
111 This function starts allocating block from the current position. Allocating involves setting the correct bits
112 in the bitmap. This function is a vector version of allocate_block below - We just run on the blocks that
113 we need to allocate, and call allocate_block for each one.
118 long entry_num,num=1;
119 char *ptr,buffer [80];
121 ptr=parse_word (command_line,buffer); /* Get the number of blocks to allocate */
123 ptr=parse_word (ptr,buffer);
127 entry_num=block_bitmap_info.entry_num;
128 /* Check for limits */
129 if (num > file_system_info.super_block.s_blocks_per_group-entry_num) {
130 wprintw (command_win,"Error - There aren't that much blocks in the group\n");
131 refresh_command_win ();return;
134 while (num) { /* And call allocate_block */
135 allocate_block (entry_num); /* for each block */
139 dispatch ("show"); /* Show the result */
142 void type_ext2_block_bitmap___deallocate (char *command_line)
144 /* This is the opposite of the above function - We call deallocate_block instead of allocate_block */
147 long entry_num,num=1;
148 char *ptr,buffer [80];
150 ptr=parse_word (command_line,buffer);
152 ptr=parse_word (ptr,buffer);
156 entry_num=block_bitmap_info.entry_num;
157 if (num > file_system_info.super_block.s_blocks_per_group-entry_num) {
158 wprintw (command_win,"Error - There aren't that much blocks in the group\n");
159 refresh_command_win ();return;
163 deallocate_block (entry_num);
171 void allocate_block (long entry_num)
173 /* In this function we convert the bit number into the right byte and inner bit positions. */
176 unsigned char bit_mask=1;
179 byte_offset=entry_num/8; /* Find the correct byte - entry_num/8 */
180 /* The position inside the byte is entry_num %8 */
181 for (j=0;j<entry_num%8;j++)
182 bit_mask*=2; /* Generate the or mask - 1 at the right place */
183 type_data.u.buffer [byte_offset] |= bit_mask; /* And apply it */
186 void deallocate_block (long entry_num)
188 /* This is the opposite of allocate_block above. We use an and mask instead of an or mask. */
191 unsigned char bit_mask=1;
194 byte_offset=entry_num/8;
195 for (j=0;j<entry_num%8;j++)
199 type_data.u.buffer [byte_offset] &= bit_mask;
202 void type_ext2_block_bitmap___show (char *command_line)
206 We show the bitmap as a series of bits, grouped at 8-bit intervals. We display 8 such groups on each line.
207 The current position (as known from block_bitmap_info.entry_num) is highlighted.
214 unsigned long block_num,entry_num;
216 ptr=type_data.u.buffer;
217 show_pad_info.line=0;show_pad_info.max_line=-1;
219 wmove (show_pad,0,0);
220 for (i=0,entry_num=0;i<file_system_info.super_block.s_blocks_per_group/8;i++,ptr++) {
221 for (j=1;j<=128;j*=2) { /* j contains the and bit mask */
222 if (entry_num==block_bitmap_info.entry_num) { /* Highlight the current entry */
223 wattrset (show_pad,A_REVERSE);
224 show_pad_info.line=show_pad_info.max_line-show_pad_info.display_lines/2;
227 if ((*ptr) & j) /* Apply the mask */
228 wprintw (show_pad,"1");
230 wprintw (show_pad,"0");
232 if (entry_num==block_bitmap_info.entry_num)
233 wattrset (show_pad,A_NORMAL);
235 entry_num++; /* Pass to the next entry */
237 wprintw (show_pad," ");
238 if (i%8==7) { /* Display 8 groups in a row */
239 wprintw (show_pad,"\n");
240 show_pad_info.max_line++;
245 show_info (); /* Show the usual information */
247 /* Show the group number */
248 wmove (show_win,1,0);
249 wprintw (show_win,"Block bitmap of block group %ld\n",block_bitmap_info.group_num);
250 /* Show the block number */
252 block_num=block_bitmap_info.entry_num+block_bitmap_info.group_num*file_system_info.super_block.s_blocks_per_group;
253 block_num+=file_system_info.super_block.s_first_data_block;
255 wprintw (show_win,"Status of block %ld - ",block_num); /* and the allocation status */
256 ptr=type_data.u.buffer+block_bitmap_info.entry_num/8;
258 for (i=block_bitmap_info.entry_num % 8;i>0;i--)
261 wprintw (show_win,"Allocated\n");
263 wprintw (show_win,"Free\n");