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 if (sb->dq_op)
256 sb->dq_op->free_inode (inode, 1);
257 sb->s_dirt = 1;
258 clear_inode (inode);
259 unlock_super (sb);
260 }
261
262
263
264
265
266
267 static void inc_inode_version (struct inode * inode,
268 struct ext2_group_desc *gdp,
269 int mode)
270 {
271 unsigned long inode_block;
272 struct buffer_head * bh;
273 struct ext2_inode * raw_inode;
274
275 inode_block = gdp->bg_inode_table + (((inode->i_ino - 1) %
276 EXT2_INODES_PER_GROUP(inode->i_sb)) /
277 EXT2_INODES_PER_BLOCK(inode->i_sb));
278 bh = bread (inode->i_sb->s_dev, inode_block, inode->i_sb->s_blocksize);
279 if (!bh) {
280 ext2_error (inode->i_sb, "inc_inode_version",
281 "Cannot load inode table block - "
282 "inode=%lu, inode_block=%lu\n",
283 inode->i_ino, inode_block);
284 inode->u.ext2_i.i_version = 1;
285 return;
286 }
287 raw_inode = ((struct ext2_inode *) bh->b_data) +
288 (((inode->i_ino - 1) %
289 EXT2_INODES_PER_GROUP(inode->i_sb)) %
290 EXT2_INODES_PER_BLOCK(inode->i_sb));
291 raw_inode->i_version++;
292 inode->u.ext2_i.i_version = raw_inode->i_version;
293 mark_buffer_dirty(bh, 1);
294 brelse (bh);
295 }
296
297
298
299
300
301
302
303
304
305
306
307 struct inode * ext2_new_inode (const struct inode * dir, int mode, int * err)
308 {
309 struct super_block * sb;
310 struct buffer_head * bh;
311 struct buffer_head * bh2;
312 int i, j, avefreei;
313 struct inode * inode;
314 int bitmap_nr;
315 struct ext2_group_desc * gdp;
316 struct ext2_group_desc * tmp;
317 struct ext2_super_block * es;
318
319 if (!dir || !(inode = get_empty_inode ()))
320 return NULL;
321 sb = dir->i_sb;
322 inode->i_sb = sb;
323 inode->i_flags = sb->s_flags;
324 lock_super (sb);
325 es = sb->u.ext2_sb.s_es;
326 repeat:
327 gdp = NULL; i=0;
328
329 *err = -ENOSPC;
330 if (S_ISDIR(mode)) {
331 avefreei = es->s_free_inodes_count /
332 sb->u.ext2_sb.s_groups_count;
333
334
335
336
337
338
339
340
341
342
343
344
345
346 if (!gdp) {
347 for (j = 0; j < sb->u.ext2_sb.s_groups_count; j++) {
348 tmp = get_group_desc (sb, j, &bh2);
349 if (tmp->bg_free_inodes_count &&
350 tmp->bg_free_inodes_count >= avefreei) {
351 if (!gdp ||
352 (tmp->bg_free_blocks_count >
353 gdp->bg_free_blocks_count)) {
354 i = j;
355 gdp = tmp;
356 }
357 }
358 }
359 }
360 }
361 else
362 {
363
364
365
366 i = dir->u.ext2_i.i_block_group;
367 tmp = get_group_desc (sb, i, &bh2);
368 if (tmp->bg_free_inodes_count)
369 gdp = tmp;
370 else
371 {
372
373
374
375
376 for (j = 1; j < sb->u.ext2_sb.s_groups_count; j <<= 1) {
377 i += j;
378 if (i >= sb->u.ext2_sb.s_groups_count)
379 i -= sb->u.ext2_sb.s_groups_count;
380 tmp = get_group_desc (sb, i, &bh2);
381 if (tmp->bg_free_inodes_count) {
382 gdp = tmp;
383 break;
384 }
385 }
386 }
387 if (!gdp) {
388
389
390
391 i = dir->u.ext2_i.i_block_group + 1;
392 for (j = 2; j < sb->u.ext2_sb.s_groups_count; j++) {
393 if (++i >= sb->u.ext2_sb.s_groups_count)
394 i = 0;
395 tmp = get_group_desc (sb, i, &bh2);
396 if (tmp->bg_free_inodes_count) {
397 gdp = tmp;
398 break;
399 }
400 }
401 }
402 }
403
404 if (!gdp) {
405 unlock_super (sb);
406 iput(inode);
407 return NULL;
408 }
409 bitmap_nr = load_inode_bitmap (sb, i);
410 bh = sb->u.ext2_sb.s_inode_bitmap[bitmap_nr];
411 if ((j = find_first_zero_bit ((unsigned long *) bh->b_data,
412 EXT2_INODES_PER_GROUP(sb))) <
413 EXT2_INODES_PER_GROUP(sb)) {
414 if (set_bit (j, bh->b_data)) {
415 ext2_warning (sb, "ext2_new_inode",
416 "bit already set for inode %d", j);
417 goto repeat;
418 }
419 mark_buffer_dirty(bh, 1);
420 if (sb->s_flags & MS_SYNCHRONOUS) {
421 ll_rw_block (WRITE, 1, &bh);
422 wait_on_buffer (bh);
423 }
424 } else {
425 if (gdp->bg_free_inodes_count != 0) {
426 ext2_error (sb, "ext2_new_inode",
427 "Free inodes count corrupted in group %d",
428 i);
429 unlock_super (sb);
430 iput (inode);
431 return NULL;
432 }
433 goto repeat;
434 }
435 j += i * EXT2_INODES_PER_GROUP(sb) + 1;
436 if (j < EXT2_FIRST_INO || j > es->s_inodes_count) {
437 ext2_error (sb, "ext2_new_inode",
438 "reserved inode or inode > inodes count - "
439 "block_group = %d,inode=%d", i, j);
440 unlock_super (sb);
441 iput (inode);
442 return NULL;
443 }
444 gdp->bg_free_inodes_count--;
445 if (S_ISDIR(mode))
446 gdp->bg_used_dirs_count++;
447 mark_buffer_dirty(bh2, 1);
448 es->s_free_inodes_count--;
449 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
450 sb->s_dirt = 1;
451 inode->i_mode = mode;
452 inode->i_sb = sb;
453 inode->i_count = 1;
454 inode->i_nlink = 1;
455 inode->i_dev = sb->s_dev;
456 inode->i_uid = current->fsuid;
457 if (test_opt (sb, GRPID))
458 inode->i_gid = dir->i_gid;
459 else if (dir->i_mode & S_ISGID) {
460 inode->i_gid = dir->i_gid;
461 if (S_ISDIR(mode))
462 mode |= S_ISGID;
463 } else
464 inode->i_gid = current->fsgid;
465 inode->i_dirt = 1;
466 inode->i_ino = j;
467 inode->i_blksize = PAGE_SIZE;
468 inode->i_blocks = 0;
469 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
470 inode->u.ext2_i.i_flags = dir->u.ext2_i.i_flags;
471 if (S_ISLNK(mode))
472 inode->u.ext2_i.i_flags &= ~(EXT2_IMMUTABLE_FL | EXT2_APPEND_FL);
473 inode->u.ext2_i.i_faddr = 0;
474 inode->u.ext2_i.i_frag_no = 0;
475 inode->u.ext2_i.i_frag_size = 0;
476 inode->u.ext2_i.i_file_acl = 0;
477 inode->u.ext2_i.i_dir_acl = 0;
478 inode->u.ext2_i.i_dtime = 0;
479 inode->u.ext2_i.i_block_group = i;
480 inode->i_op = NULL;
481 if (inode->u.ext2_i.i_flags & EXT2_SYNC_FL)
482 inode->i_flags |= MS_SYNCHRONOUS;
483 insert_inode_hash(inode);
484 inc_inode_version (inode, gdp, mode);
485
486 unlock_super (sb);
487 if (sb->dq_op) {
488 sb->dq_op->initialize (inode, -1);
489 if (sb->dq_op->alloc_inode (inode, 1)) {
490 sb->dq_op->drop (inode);
491 inode->i_nlink = 0;
492 iput (inode);
493 *err = -EDQUOT;
494 return NULL;
495 }
496 inode->i_flags |= S_WRITE;
497 }
498 ext2_debug ("allocating inode %lu\n", inode->i_ino);
499
500 *err = 0;
501 return inode;
502 }
503
504 unsigned long ext2_count_free_inodes (struct super_block * sb)
505 {
506 #ifdef EXT2FS_DEBUG
507 struct ext2_super_block * es;
508 unsigned long desc_count, bitmap_count, x;
509 int bitmap_nr;
510 struct ext2_group_desc * gdp;
511 int i;
512
513 lock_super (sb);
514 es = sb->u.ext2_sb.s_es;
515 desc_count = 0;
516 bitmap_count = 0;
517 gdp = NULL;
518 for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
519 gdp = get_group_desc (sb, i, NULL);
520 desc_count += gdp->bg_free_inodes_count;
521 bitmap_nr = load_inode_bitmap (sb, i);
522 x = ext2_count_free (sb->u.ext2_sb.s_inode_bitmap[bitmap_nr],
523 EXT2_INODES_PER_GROUP(sb) / 8);
524 printk ("group %d: stored = %d, counted = %lu\n",
525 i, gdp->bg_free_inodes_count, x);
526 bitmap_count += x;
527 }
528 printk("ext2_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
529 es->s_free_inodes_count, desc_count, bitmap_count);
530 unlock_super (sb);
531 return desc_count;
532 #else
533 return sb->u.ext2_sb.s_es->s_free_inodes_count;
534 #endif
535 }
536
537 void ext2_check_inodes_bitmap (struct super_block * sb)
538 {
539 struct ext2_super_block * es;
540 unsigned long desc_count, bitmap_count, x;
541 int bitmap_nr;
542 struct ext2_group_desc * gdp;
543 int i;
544
545 lock_super (sb);
546 es = sb->u.ext2_sb.s_es;
547 desc_count = 0;
548 bitmap_count = 0;
549 gdp = NULL;
550 for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
551 gdp = get_group_desc (sb, i, NULL);
552 desc_count += gdp->bg_free_inodes_count;
553 bitmap_nr = load_inode_bitmap (sb, i);
554 x = ext2_count_free (sb->u.ext2_sb.s_inode_bitmap[bitmap_nr],
555 EXT2_INODES_PER_GROUP(sb) / 8);
556 if (gdp->bg_free_inodes_count != x)
557 ext2_error (sb, "ext2_check_inodes_bitmap",
558 "Wrong free inodes count in group %d, "
559 "stored = %d, counted = %lu", i,
560 gdp->bg_free_inodes_count, x);
561 bitmap_count += x;
562 }
563 if (es->s_free_inodes_count != bitmap_count)
564 ext2_error (sb, "ext2_check_inodes_bitmap",
565 "Wrong free inodes count in super block, "
566 "stored = %lu, counted = %lu",
567 (unsigned long) es->s_free_inodes_count, bitmap_count);
568 unlock_super (sb);
569 }