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