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