1 /*
2 
3 /usr/src/ext2ed/inodebitmap_com.c
4 
5 A part of the extended file system 2 disk editor.
6 
7 -------------------------
8 Handles the inode bitmap.
9 -------------------------
10 
11 Please refer to the documentation in blockbitmap_com.c - Those two files are almost equal.
12 
13 First written on: July 25 1995
14 
15 Copyright (C) 1995 Gadi Oxman
16 
17 */
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 #include "ext2ed.h"
24 
25 
type_ext2_inode_bitmap___entry(char * command_line)26 void type_ext2_inode_bitmap___entry (char *command_line)
27 
28 {
29 	unsigned long entry_num;
30 	char *ptr,buffer [80];
31 
32 	ptr=parse_word (command_line,buffer);
33 	if (*ptr==0) {
34 		wprintw (command_win,"Error - No argument specified\n");refresh_command_win ();return;
35 	}
36 	ptr=parse_word (ptr,buffer);
37 
38 	entry_num=atol (buffer);
39 
40 	if (entry_num >= file_system_info.super_block.s_inodes_per_group) {
41 		wprintw (command_win,"Error - Entry number out of bounds\n");refresh_command_win ();return;
42 	}
43 
44 	inode_bitmap_info.entry_num=entry_num;
45 	strcpy (buffer,"show");dispatch (buffer);
46 }
47 
type_ext2_inode_bitmap___next(char * command_line)48 void type_ext2_inode_bitmap___next (char *command_line)
49 
50 {
51 	long entry_offset=1;
52 	char *ptr,buffer [80];
53 
54 	ptr=parse_word (command_line,buffer);
55 	if (*ptr!=0) {
56 		ptr=parse_word (ptr,buffer);
57 		entry_offset=atol (buffer);
58 	}
59 
60 	sprintf (buffer,"entry %ld",inode_bitmap_info.entry_num+entry_offset);
61 	dispatch (buffer);
62 }
63 
type_ext2_inode_bitmap___prev(char * command_line)64 void type_ext2_inode_bitmap___prev (char *command_line)
65 
66 {
67 	long entry_offset=1;
68 	char *ptr,buffer [80];
69 
70 	ptr=parse_word (command_line,buffer);
71 	if (*ptr!=0) {
72 		ptr=parse_word (ptr,buffer);
73 		entry_offset=atol (buffer);
74 	}
75 
76 	sprintf (buffer,"entry %ld",inode_bitmap_info.entry_num-entry_offset);
77 	dispatch (buffer);
78 }
79 
type_ext2_inode_bitmap___allocate(char * command_line)80 void type_ext2_inode_bitmap___allocate (char *command_line)
81 
82 {
83 	long entry_num,num=1;
84 	char *ptr,buffer [80];
85 
86 	ptr=parse_word (command_line,buffer);
87 	if (*ptr!=0) {
88 		ptr=parse_word (ptr,buffer);
89 		num=atol (buffer);
90 	}
91 
92 	entry_num=inode_bitmap_info.entry_num;
93 	if (num > file_system_info.super_block.s_inodes_per_group-entry_num) {
94 		wprintw (command_win,"Error - There aren't that much inodes in the group\n");
95 		refresh_command_win ();return;
96 	}
97 
98 	while (num) {
99 		allocate_inode (entry_num);
100 		num--;entry_num++;
101 	}
102 
103 	dispatch ("show");
104 }
105 
type_ext2_inode_bitmap___deallocate(char * command_line)106 void type_ext2_inode_bitmap___deallocate (char *command_line)
107 
108 {
109 	long entry_num,num=1;
110 	char *ptr,buffer [80];
111 
112 	ptr=parse_word (command_line,buffer);
113 	if (*ptr!=0) {
114 		ptr=parse_word (ptr,buffer);
115 		num=atol (buffer);
116 	}
117 
118 	entry_num=inode_bitmap_info.entry_num;
119 	if (num > file_system_info.super_block.s_inodes_per_group-entry_num) {
120 		wprintw (command_win,"Error - There aren't that much inodes in the group\n");
121 		refresh_command_win ();return;
122 	}
123 
124 	while (num) {
125 		deallocate_inode (entry_num);
126 		num--;entry_num++;
127 	}
128 
129 	dispatch ("show");
130 }
131 
132 
allocate_inode(long entry_num)133 void allocate_inode (long entry_num)
134 
135 {
136 	unsigned char bit_mask=1;
137 	int byte_offset,j;
138 
139 	byte_offset=entry_num/8;
140 	for (j=0;j<entry_num%8;j++)
141 		bit_mask*=2;
142 	type_data.u.buffer [byte_offset] |= bit_mask;
143 }
144 
deallocate_inode(long entry_num)145 void deallocate_inode (long entry_num)
146 
147 {
148 	unsigned char bit_mask=1;
149 	int byte_offset,j;
150 
151 	byte_offset=entry_num/8;
152 	for (j=0;j<entry_num%8;j++)
153 		bit_mask*=2;
154 	bit_mask^=0xff;
155 
156 	type_data.u.buffer [byte_offset] &= bit_mask;
157 }
158 
type_ext2_inode_bitmap___show(char * command_line)159 void type_ext2_inode_bitmap___show (char *command_line)
160 
161 {
162 	int i,j;
163 	unsigned char *ptr;
164 	unsigned long inode_num,entry_num;
165 
166 	ptr=type_data.u.buffer;
167 	show_pad_info.line=0;show_pad_info.max_line=-1;
168 
169 	wmove (show_pad,0,0);
170 	for (i=0,entry_num=0;i<file_system_info.super_block.s_inodes_per_group/8;i++,ptr++) {
171 		for (j=1;j<=128;j*=2) {
172 			if (entry_num==inode_bitmap_info.entry_num) {
173 				wattrset (show_pad,A_REVERSE);
174 				show_pad_info.line=show_pad_info.max_line-show_pad_info.display_lines/2;
175 			}
176 
177 			if ((*ptr) & j)
178 				wprintw (show_pad,"1");
179 			else
180 				wprintw (show_pad,"0");
181 
182 			if (entry_num==inode_bitmap_info.entry_num)
183 				wattrset (show_pad,A_NORMAL);
184 
185 			entry_num++;
186 		}
187 		wprintw (show_pad," ");
188 		if (i%8==7) {
189 			wprintw (show_pad,"\n");
190 			show_pad_info.max_line++;
191 		}
192 	}
193 
194 	if (i%8!=7) {
195 		wprintw (show_pad,"\n");
196 		show_pad_info.max_line++;
197 	}
198 
199 	refresh_show_pad ();
200 	show_info ();
201 	wmove (show_win,1,0);wprintw (show_win,"Inode bitmap of block group %ld\n",inode_bitmap_info.group_num);
202 
203 	inode_num=1+inode_bitmap_info.entry_num+inode_bitmap_info.group_num*file_system_info.super_block.s_inodes_per_group;
204 	wprintw (show_win,"Status of inode %ld - ",inode_num);
205 	ptr=type_data.u.buffer+inode_bitmap_info.entry_num/8;
206 	j=1;
207 	for (i=inode_bitmap_info.entry_num % 8;i>0;i--)
208 		j*=2;
209 	if ((*ptr) & j)
210 		wprintw (show_win,"Allocated\n");
211 	else
212 		wprintw (show_win,"Free\n");
213 	refresh_show_win ();
214 }
215