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