This source file includes following definitions.
- get_group_desc
- read_inode_bitmap
- load_inode_bitmap
- set_inode_dtime
- ext2_free_inode
- inc_inode_version
- ext2_new_inode
- ext2_count_free_inodes
- ext2_check_inodes_bitmap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 #include <linux/fs.h>
28 #include <linux/ext2_fs.h>
29 #include <linux/sched.h>
30 #include <linux/stat.h>
31 #include <linux/string.h>
32 #include <linux/locks.h>
33
34 #include <asm/bitops.h>
35
36 static struct ext2_group_desc * get_group_desc (struct super_block * sb,
37 unsigned int block_group,
38 struct buffer_head ** bh)
39 {
40 unsigned long group_desc;
41 unsigned long desc;
42 struct ext2_group_desc * gdp;
43
44 if (block_group >= sb->u.ext2_sb.s_groups_count)
45 ext2_panic (sb, "get_group_desc",
46 "block_group >= groups_count\n"
47 "block_group = %d, groups_count = %lu",
48 block_group, sb->u.ext2_sb.s_groups_count);
49
50 group_desc = block_group / EXT2_DESC_PER_BLOCK(sb);
51 desc = block_group % EXT2_DESC_PER_BLOCK(sb);
52 if (!sb->u.ext2_sb.s_group_desc[group_desc])
53 ext2_panic (sb, "get_group_desc",
54 "Group descriptor not loaded\n"
55 "block_group = %d, group_desc = %lu, desc = %lu",
56 block_group, group_desc, desc);
57 gdp = (struct ext2_group_desc *)
58 sb->u.ext2_sb.s_group_desc[group_desc]->b_data;
59 if (bh)
60 *bh = sb->u.ext2_sb.s_group_desc[group_desc];
61 return gdp + desc;
62 }
63
64 static void read_inode_bitmap (struct super_block * sb,
65 unsigned long block_group,
66 unsigned int bitmap_nr)
67 {
68 struct ext2_group_desc * gdp;
69 struct buffer_head * bh;
70
71 gdp = get_group_desc (sb, block_group, NULL);
72 bh = bread (sb->s_dev, gdp->bg_inode_bitmap, sb->s_blocksize);
73 if (!bh)
74 ext2_panic (sb, "read_inode_bitmap", "Cannot read inode bitmap\n"
75 "block_group = %lu, inode_bitmap = %lu",
76 block_group, gdp->bg_inode_bitmap);
77 sb->u.ext2_sb.s_inode_bitmap_number[bitmap_nr] = block_group;
78 sb->u.ext2_sb.s_inode_bitmap[bitmap_nr] = bh;
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92 static int load_inode_bitmap (struct super_block * sb,
93 unsigned int block_group)
94 {
95 int i, j;
96 unsigned long inode_bitmap_number;
97 struct buffer_head * inode_bitmap;
98
99 if (block_group >= sb->u.ext2_sb.s_groups_count)
100 ext2_panic (sb, "load_inode_bitmap",
101 "block_group >= groups_count\n"
102 "block_group = %d, groups_count = %lu",
103 block_group, sb->u.ext2_sb.s_groups_count);
104 if (sb->u.ext2_sb.s_loaded_inode_bitmaps > 0 &&
105 sb->u.ext2_sb.s_inode_bitmap_number[0] == block_group)
106 return 0;
107 if (sb->u.ext2_sb.s_groups_count <= EXT2_MAX_GROUP_LOADED) {
108 if (sb->u.ext2_sb.s_inode_bitmap[block_group]) {
109 if (sb->u.ext2_sb.s_inode_bitmap_number[block_group] != block_group)
110 ext2_panic (sb, "load_inode_bitmap",
111 "block_group != inode_bitmap_number");
112 else
113 return block_group;
114 } else {
115 read_inode_bitmap (sb, block_group, block_group);
116 return block_group;
117 }
118 }
119
120 for (i = 0; i < sb->u.ext2_sb.s_loaded_inode_bitmaps &&
121 sb->u.ext2_sb.s_inode_bitmap_number[i] != block_group;
122 i++)
123 ;
124 if (i < sb->u.ext2_sb.s_loaded_inode_bitmaps &&
125 sb->u.ext2_sb.s_inode_bitmap_number[i] == block_group) {
126 inode_bitmap_number = sb->u.ext2_sb.s_inode_bitmap_number[i];
127 inode_bitmap = sb->u.ext2_sb.s_inode_bitmap[i];
128 for (j = i; j > 0; j--) {
129 sb->u.ext2_sb.s_inode_bitmap_number[j] =
130 sb->u.ext2_sb.s_inode_bitmap_number[j - 1];
131 sb->u.ext2_sb.s_inode_bitmap[j] =
132 sb->u.ext2_sb.s_inode_bitmap[j - 1];
133 }
134 sb->u.ext2_sb.s_inode_bitmap_number[0] = inode_bitmap_number;
135 sb->u.ext2_sb.s_inode_bitmap[0] = inode_bitmap;
136 } else {
137 if (sb->u.ext2_sb.s_loaded_inode_bitmaps < EXT2_MAX_GROUP_LOADED)
138 sb->u.ext2_sb.s_loaded_inode_bitmaps++;
139 else
140 brelse (sb->u.ext2_sb.s_inode_bitmap[EXT2_MAX_GROUP_LOADED - 1]);
141 for (j = sb->u.ext2_sb.s_loaded_inode_bitmaps - 1; j > 0; j--) {
142 sb->u.ext2_sb.s_inode_bitmap_number[j] =
143 sb->u.ext2_sb.s_inode_bitmap_number[j - 1];
144 sb->u.ext2_sb.s_inode_bitmap[j] =
145 sb->u.ext2_sb.s_inode_bitmap[j - 1];
146 }
147 read_inode_bitmap (sb, block_group, 0);
148 }
149 return 0;
150 }
151
152
153
154
155
156
157 static void set_inode_dtime (struct inode * inode,
158 struct ext2_group_desc * gdp)
159 {
160 unsigned long inode_block;
161 struct buffer_head * bh;
162 struct ext2_inode * raw_inode;
163
164 inode_block = gdp->bg_inode_table + (((inode->i_ino - 1) %
165 EXT2_INODES_PER_GROUP(inode->i_sb)) /
166 EXT2_INODES_PER_BLOCK(inode->i_sb));
167 bh = bread (inode->i_sb->s_dev, inode_block, inode->i_sb->s_blocksize);
168 if (!bh)
169 ext2_panic (inode->i_sb, "set_inode_dtime",
170 "Cannot load inode table block\n"
171 "inode=%lu, inode_block=%lu",
172 inode->i_ino, inode_block);
173 raw_inode = ((struct ext2_inode *) bh->b_data) +
174 (((inode->i_ino - 1) %
175 EXT2_INODES_PER_GROUP(inode->i_sb)) %
176 EXT2_INODES_PER_BLOCK(inode->i_sb));
177 raw_inode->i_links_count = 0;
178 raw_inode->i_dtime = CURRENT_TIME;
179 mark_buffer_dirty(bh, 1);
180 if (IS_SYNC(inode)) {
181 ll_rw_block (WRITE, 1, &bh);
182 wait_on_buffer (bh);
183 }
184 brelse (bh);
185 }
186
187 void ext2_free_inode (struct inode * inode)
188 {
189 struct super_block * sb;
190 struct buffer_head * bh;
191 struct buffer_head * bh2;
192 unsigned long block_group;
193 unsigned long bit;
194 int bitmap_nr;
195 struct ext2_group_desc * gdp;
196 struct ext2_super_block * es;
197
198 if (!inode)
199 return;
200 if (!inode->i_dev) {
201 printk ("ext2_free_inode: inode has no device\n");
202 return;
203 }
204 if (inode->i_count > 1) {
205 printk ("ext2_free_inode: inode has count=%d\n",
206 inode->i_count);
207 return;
208 }
209 if (inode->i_nlink) {
210 printk ("ext2_free_inode: inode has nlink=%d\n",
211 inode->i_nlink);
212 return;
213 }
214 if (!inode->i_sb) {
215 printk("ext2_free_inode: inode on nonexistent device\n");
216 return;
217 }
218
219 ext2_debug ("freeing inode %lu\n", inode->i_ino);
220
221 sb = inode->i_sb;
222 lock_super (sb);
223 if (inode->i_ino < EXT2_FIRST_INO ||
224 inode->i_ino > sb->u.ext2_sb.s_es->s_inodes_count) {
225 ext2_error (sb, "free_inode",
226 "reserved inode or nonexistent inode");
227 unlock_super (sb);
228 return;
229 }
230 es = sb->u.ext2_sb.s_es;
231 block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(sb);
232 bit = (inode->i_ino - 1) % EXT2_INODES_PER_GROUP(sb);
233 bitmap_nr = load_inode_bitmap (sb, block_group);
234 bh = sb->u.ext2_sb.s_inode_bitmap[bitmap_nr];
235 if (!clear_bit (bit, bh->b_data))
236 ext2_warning (sb, "ext2_free_inode",
237 "bit already cleared for inode %lu", inode->i_ino);
238 else {
239 gdp = get_group_desc (sb, block_group, &bh2);
240 gdp->bg_free_inodes_count++;
241 if (S_ISDIR(inode->i_mode))
242 gdp->bg_used_dirs_count--;
243 mark_buffer_dirty(bh2, 1);
244 es->s_free_inodes_count++;
245 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
246 set_inode_dtime (inode, gdp);
247 }
248 mark_buffer_dirty(bh, 1);
249 if (sb->s_flags & MS_SYNC) {
250 ll_rw_block (WRITE, 1, &bh);
251 wait_on_buffer (bh);
252 }
253
254 sb->s_dirt = 1;
255 clear_inode (inode);
256 unlock_super (sb);
257 }
258
259
260
261
262
263
264 static void inc_inode_version (struct inode * inode,
265 struct ext2_group_desc *gdp,
266 int mode)
267 {
268 unsigned long inode_block;
269 struct buffer_head * bh;
270 struct ext2_inode * raw_inode;
271
272 inode_block = gdp->bg_inode_table + (((inode->i_ino - 1) %
273 EXT2_INODES_PER_GROUP(inode->i_sb)) /
274 EXT2_INODES_PER_BLOCK(inode->i_sb));
275 bh = bread (inode->i_sb->s_dev, inode_block, inode->i_sb->s_blocksize);
276 if (!bh) {
277 ext2_error (inode->i_sb, "inc_inode_version",
278 "Cannot load inode table block"
279 "inode=%lu, inode_block=%lu\n",
280 inode->i_ino, inode_block);
281 inode->u.ext2_i.i_version = 1;
282 return;
283 }
284 raw_inode = ((struct ext2_inode *) bh->b_data) +
285 (((inode->i_ino - 1) %
286 EXT2_INODES_PER_GROUP(inode->i_sb)) %
287 EXT2_INODES_PER_BLOCK(inode->i_sb));
288 raw_inode->i_version++;
289 inode->u.ext2_i.i_version = raw_inode->i_version;
290 mark_buffer_dirty(bh, 1);
291 brelse (bh);
292 }
293
294
295
296
297
298
299
300
301
302
303
304 struct inode * ext2_new_inode (const struct inode * dir, int mode)
305 {
306 struct super_block * sb;
307 struct buffer_head * bh;
308 struct buffer_head * bh2;
309 int i, j, avefreei;
310 struct inode * inode;
311 int bitmap_nr;
312 struct ext2_group_desc * gdp;
313 struct ext2_group_desc * tmp;
314 struct ext2_super_block * es;
315
316 if (!dir || !(inode = get_empty_inode ()))
317 return NULL;
318 sb = dir->i_sb;
319 inode->i_sb = sb;
320 inode->i_flags = sb->s_flags;
321 lock_super (sb);
322 es = sb->u.ext2_sb.s_es;
323 repeat:
324 gdp = NULL; i=0;
325
326 if (S_ISDIR(mode)) {
327 avefreei = es->s_free_inodes_count /
328 sb->u.ext2_sb.s_groups_count;
329
330
331
332
333
334
335
336
337
338
339
340
341
342 if (!gdp) {
343 for (j = 0; j < sb->u.ext2_sb.s_groups_count; j++) {
344 tmp = get_group_desc (sb, j, &bh2);
345 if (tmp->bg_free_inodes_count &&
346 tmp->bg_free_inodes_count >= avefreei) {
347 if (!gdp ||
348 (tmp->bg_free_blocks_count >
349 gdp->bg_free_blocks_count)) {
350 i = j;
351 gdp = tmp;
352 }
353 }
354 }
355 }
356 }
357 else
358 {
359
360
361
362 i = dir->u.ext2_i.i_block_group;
363 tmp = get_group_desc (sb, i, &bh2);
364 if (tmp->bg_free_inodes_count)
365 gdp = tmp;
366 else
367 {
368
369
370
371
372 for (j = 1; j < sb->u.ext2_sb.s_groups_count; j <<= 1) {
373 i += j;
374 if (i >= sb->u.ext2_sb.s_groups_count)
375 i -= sb->u.ext2_sb.s_groups_count;
376 tmp = get_group_desc (sb, i, &bh2);
377 if (tmp->bg_free_inodes_count) {
378 gdp = tmp;
379 break;
380 }
381 }
382 }
383 if (!gdp) {
384
385
386
387 i = dir->u.ext2_i.i_block_group + 1;
388 for (j = 2; j < sb->u.ext2_sb.s_groups_count; j++) {
389 if (++i >= sb->u.ext2_sb.s_groups_count)
390 i = 0;
391 tmp = get_group_desc (sb, i, &bh2);
392 if (tmp->bg_free_inodes_count) {
393 gdp = tmp;
394 break;
395 }
396 }
397 }
398 }
399
400 if (!gdp) {
401 unlock_super (sb);
402 iput(inode);
403 return NULL;
404 }
405 bitmap_nr = load_inode_bitmap (sb, i);
406 bh = sb->u.ext2_sb.s_inode_bitmap[bitmap_nr];
407 if ((j = find_first_zero_bit ((unsigned long *) bh->b_data,
408 EXT2_INODES_PER_GROUP(sb))) <
409 EXT2_INODES_PER_GROUP(sb)) {
410 if (set_bit (j, bh->b_data)) {
411 ext2_warning (sb, "ext2_new_inode",
412 "bit already set for inode %d", j);
413 goto repeat;
414 }
415 mark_buffer_dirty(bh, 1);
416 if (sb->s_flags & MS_SYNC) {
417 ll_rw_block (WRITE, 1, &bh);
418 wait_on_buffer (bh);
419 }
420 } else {
421 if (gdp->bg_free_inodes_count != 0) {
422 ext2_error (sb, "ext2_new_inode",
423 "Free inodes count corrupted in group %d",
424 i);
425 unlock_super (sb);
426 iput (inode);
427 return NULL;
428 }
429 goto repeat;
430 }
431 j += i * EXT2_INODES_PER_GROUP(sb) + 1;
432 if (j < EXT2_FIRST_INO || j > es->s_inodes_count) {
433 ext2_error (sb, "ext2_new_inode",
434 "reserved inode or inode > inodes count\n"
435 "block_group = %d,inode=%d", i, j);
436 unlock_super (sb);
437 iput (inode);
438 return NULL;
439 }
440 gdp->bg_free_inodes_count--;
441 if (S_ISDIR(mode))
442 gdp->bg_used_dirs_count++;
443 mark_buffer_dirty(bh2, 1);
444 es->s_free_inodes_count--;
445 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
446 sb->s_dirt = 1;
447 inode->i_mode = mode;
448 inode->i_sb = sb;
449 inode->i_count = 1;
450 inode->i_nlink = 1;
451 inode->i_dev = sb->s_dev;
452 inode->i_uid = current->fsuid;
453 if (test_opt (sb, GRPID))
454 inode->i_gid = dir->i_gid;
455 else if (dir->i_mode & S_ISGID) {
456 inode->i_gid = dir->i_gid;
457 if (S_ISDIR(mode))
458 mode |= S_ISGID;
459 } else
460 inode->i_gid = current->fsgid;
461 inode->i_dirt = 1;
462 inode->i_ino = j;
463 inode->i_blksize = sb->s_blocksize;
464 inode->i_blocks = 0;
465 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
466 inode->u.ext2_i.i_flags = dir->u.ext2_i.i_flags;
467 inode->u.ext2_i.i_faddr = 0;
468 inode->u.ext2_i.i_frag_no = 0;
469 inode->u.ext2_i.i_frag_size = 0;
470 inode->u.ext2_i.i_file_acl = 0;
471 inode->u.ext2_i.i_dir_acl = 0;
472 inode->u.ext2_i.i_dtime = 0;
473 inode->u.ext2_i.i_block_group = i;
474 inode->i_op = NULL;
475 if (inode->u.ext2_i.i_flags & EXT2_SYNC_FL)
476 inode->i_flags |= MS_SYNC;
477 insert_inode_hash(inode);
478 inc_inode_version (inode, gdp, mode);
479
480 ext2_debug ("allocating inode %lu\n", inode->i_ino);
481
482 unlock_super (sb);
483 return inode;
484 }
485
486 unsigned long ext2_count_free_inodes (struct super_block * sb)
487 {
488 #ifdef EXT2FS_DEBUG
489 struct ext2_super_block * es;
490 unsigned long desc_count, bitmap_count, x;
491 int bitmap_nr;
492 struct ext2_group_desc * gdp;
493 int i;
494
495 lock_super (sb);
496 es = sb->u.ext2_sb.s_es;
497 desc_count = 0;
498 bitmap_count = 0;
499 gdp = NULL;
500 for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
501 gdp = get_group_desc (sb, i, NULL);
502 desc_count += gdp->bg_free_inodes_count;
503 bitmap_nr = load_inode_bitmap (sb, i);
504 x = ext2_count_free (sb->u.ext2_sb.s_inode_bitmap[bitmap_nr],
505 EXT2_INODES_PER_GROUP(sb) / 8);
506 printk ("group %d: stored = %d, counted = %lu\n",
507 i, gdp->bg_free_inodes_count, x);
508 bitmap_count += x;
509 }
510 printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
511 es->s_free_inodes_count, desc_count, bitmap_count);
512 unlock_super (sb);
513 return desc_count;
514 #else
515 return sb->u.ext2_sb.s_es->s_free_inodes_count;
516 #endif
517 }
518
519 void ext2_check_inodes_bitmap (struct super_block * sb)
520 {
521 struct ext2_super_block * es;
522 unsigned long desc_count, bitmap_count, x;
523 int bitmap_nr;
524 struct ext2_group_desc * gdp;
525 int i;
526
527 lock_super (sb);
528 es = sb->u.ext2_sb.s_es;
529 desc_count = 0;
530 bitmap_count = 0;
531 gdp = NULL;
532 for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
533 gdp = get_group_desc (sb, i, NULL);
534 desc_count += gdp->bg_free_inodes_count;
535 bitmap_nr = load_inode_bitmap (sb, i);
536 x = ext2_count_free (sb->u.ext2_sb.s_inode_bitmap[bitmap_nr],
537 EXT2_INODES_PER_GROUP(sb) / 8);
538 if (gdp->bg_free_inodes_count != x)
539 ext2_error (sb, "ext2_check_inodes_bitmap",
540 "Wrong free inodes count in group %d, "
541 "stored = %d, counted = %lu", i,
542 gdp->bg_free_inodes_count, x);
543 bitmap_count += x;
544 }
545 if (es->s_free_inodes_count != bitmap_count)
546 ext2_error (sb, "ext2_check_inodes_bitmap",
547 "Wrong free inodes count in super block, "
548 "stored = %lu, counted = %lu",
549 es->s_free_inodes_count, bitmap_count);
550 unlock_super (sb);
551 }