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