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