1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/sched.h>
16#include <linux/fs.h>
17#include <linux/jbd.h>
18#include <linux/ext3_fs.h>
19#include <linux/ext3_jbd.h>
20#include <linux/stat.h>
21#include <linux/string.h>
22#include <linux/locks.h>
23#include <linux/quotaops.h>
24
25#include <asm/bitops.h>
26#include <asm/byteorder.h>
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50static int read_inode_bitmap (struct super_block * sb,
51 unsigned long block_group,
52 unsigned int bitmap_nr)
53{
54 struct ext3_group_desc * gdp;
55 struct buffer_head * bh = NULL;
56 int retval = 0;
57
58 gdp = ext3_get_group_desc (sb, block_group, NULL);
59 if (!gdp) {
60 retval = -EIO;
61 goto error_out;
62 }
63 bh = sb_bread(sb, le32_to_cpu(gdp->bg_inode_bitmap));
64 if (!bh) {
65 ext3_error (sb, "read_inode_bitmap",
66 "Cannot read inode bitmap - "
67 "block_group = %lu, inode_bitmap = %u",
68 block_group, gdp->bg_inode_bitmap);
69 retval = -EIO;
70 }
71
72
73
74
75error_out:
76 sb->u.ext3_sb.s_inode_bitmap_number[bitmap_nr] = block_group;
77 sb->u.ext3_sb.s_inode_bitmap[bitmap_nr] = bh;
78 return retval;
79}
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94static int load_inode_bitmap (struct super_block * sb,
95 unsigned int block_group)
96{
97 struct ext3_sb_info *sbi = EXT3_SB(sb);
98 unsigned long inode_bitmap_number;
99 struct buffer_head * inode_bitmap;
100 int i, j, retval = 0;
101
102 if (block_group >= sbi->s_groups_count)
103 ext3_panic (sb, "load_inode_bitmap",
104 "block_group >= groups_count - "
105 "block_group = %d, groups_count = %lu",
106 block_group, sbi->s_groups_count);
107 if (sbi->s_loaded_inode_bitmaps > 0 &&
108 sbi->s_inode_bitmap_number[0] == block_group &&
109 sbi->s_inode_bitmap[0] != NULL)
110 return 0;
111 if (sbi->s_groups_count <= EXT3_MAX_GROUP_LOADED) {
112 if (sbi->s_inode_bitmap[block_group]) {
113 if (sbi->s_inode_bitmap_number[block_group] !=
114 block_group)
115 ext3_panic(sb, "load_inode_bitmap",
116 "block_group != inode_bitmap_number");
117 return block_group;
118 }
119 retval = read_inode_bitmap(sb, block_group, block_group);
120 if (retval < 0)
121 return retval;
122 return block_group;
123 }
124
125 for (i = 0; i < sbi->s_loaded_inode_bitmaps &&
126 sbi->s_inode_bitmap_number[i] != block_group; i++)
127 ;
128 if (i < sbi->s_loaded_inode_bitmaps &&
129 sbi->s_inode_bitmap_number[i] == block_group) {
130 inode_bitmap_number = sbi->s_inode_bitmap_number[i];
131 inode_bitmap = sbi->s_inode_bitmap[i];
132 for (j = i; j > 0; j--) {
133 sbi->s_inode_bitmap_number[j] =
134 sbi->s_inode_bitmap_number[j - 1];
135 sbi->s_inode_bitmap[j] = sbi->s_inode_bitmap[j - 1];
136 }
137 sbi->s_inode_bitmap_number[0] = inode_bitmap_number;
138 sbi->s_inode_bitmap[0] = inode_bitmap;
139
140
141
142
143
144
145 if (!inode_bitmap)
146 retval = read_inode_bitmap (sb, block_group, 0);
147 } else {
148 if (sbi->s_loaded_inode_bitmaps < EXT3_MAX_GROUP_LOADED)
149 sbi->s_loaded_inode_bitmaps++;
150 else
151 brelse(sbi->s_inode_bitmap[EXT3_MAX_GROUP_LOADED - 1]);
152 for (j = sbi->s_loaded_inode_bitmaps - 1; j > 0; j--) {
153 sbi->s_inode_bitmap_number[j] =
154 sbi->s_inode_bitmap_number[j - 1];
155 sbi->s_inode_bitmap[j] = sbi->s_inode_bitmap[j - 1];
156 }
157 retval = read_inode_bitmap (sb, block_group, 0);
158 }
159 return retval;
160}
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178void ext3_free_inode (handle_t *handle, struct inode * inode)
179{
180 struct super_block * sb = inode->i_sb;
181 int is_directory;
182 unsigned long ino;
183 struct buffer_head * bh;
184 struct buffer_head * bh2;
185 unsigned long block_group;
186 unsigned long bit;
187 int bitmap_nr;
188 struct ext3_group_desc * gdp;
189 struct ext3_super_block * es;
190 int fatal = 0, err;
191
192 if (!inode->i_dev) {
193 printk ("ext3_free_inode: inode has no device\n");
194 return;
195 }
196 if (atomic_read(&inode->i_count) > 1) {
197 printk ("ext3_free_inode: inode has count=%d\n",
198 atomic_read(&inode->i_count));
199 return;
200 }
201 if (inode->i_nlink) {
202 printk ("ext3_free_inode: inode has nlink=%d\n",
203 inode->i_nlink);
204 return;
205 }
206 if (!sb) {
207 printk("ext3_free_inode: inode on nonexistent device\n");
208 return;
209 }
210
211 ino = inode->i_ino;
212 ext3_debug ("freeing inode %lu\n", ino);
213
214
215
216
217
218 DQUOT_INIT(inode);
219 DQUOT_FREE_INODE(inode);
220 DQUOT_DROP(inode);
221
222 is_directory = S_ISDIR(inode->i_mode);
223
224
225 clear_inode (inode);
226
227 lock_super (sb);
228 es = sb->u.ext3_sb.s_es;
229 if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
230 ext3_error (sb, "ext3_free_inode",
231 "reserved or nonexistent inode %lu", ino);
232 goto error_return;
233 }
234 block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
235 bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
236 bitmap_nr = load_inode_bitmap (sb, block_group);
237 if (bitmap_nr < 0)
238 goto error_return;
239
240 bh = sb->u.ext3_sb.s_inode_bitmap[bitmap_nr];
241
242 BUFFER_TRACE(bh, "get_write_access");
243 fatal = ext3_journal_get_write_access(handle, bh);
244 if (fatal)
245 goto error_return;
246
247
248 if (!ext3_clear_bit (bit, bh->b_data))
249 ext3_error (sb, "ext3_free_inode",
250 "bit already cleared for inode %lu", ino);
251 else {
252 gdp = ext3_get_group_desc (sb, block_group, &bh2);
253
254 BUFFER_TRACE(bh2, "get_write_access");
255 fatal = ext3_journal_get_write_access(handle, bh2);
256 if (fatal) goto error_return;
257
258 BUFFER_TRACE(sb->u.ext3_sb.s_sbh, "get write access");
259 fatal = ext3_journal_get_write_access(handle, sb->u.ext3_sb.s_sbh);
260 if (fatal) goto error_return;
261
262 if (gdp) {
263 gdp->bg_free_inodes_count = cpu_to_le16(
264 le16_to_cpu(gdp->bg_free_inodes_count) + 1);
265 if (is_directory)
266 gdp->bg_used_dirs_count = cpu_to_le16(
267 le16_to_cpu(gdp->bg_used_dirs_count) - 1);
268 }
269 BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
270 err = ext3_journal_dirty_metadata(handle, bh2);
271 if (!fatal) fatal = err;
272 es->s_free_inodes_count =
273 cpu_to_le32(le32_to_cpu(es->s_free_inodes_count) + 1);
274 BUFFER_TRACE(sb->u.ext3_sb.s_sbh,
275 "call ext3_journal_dirty_metadata");
276 err = ext3_journal_dirty_metadata(handle, sb->u.ext3_sb.s_sbh);
277 if (!fatal) fatal = err;
278 }
279 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
280 err = ext3_journal_dirty_metadata(handle, bh);
281 if (!fatal)
282 fatal = err;
283 sb->s_dirt = 1;
284error_return:
285 ext3_std_error(sb, fatal);
286 unlock_super(sb);
287}
288
289
290
291
292
293
294
295
296
297
298
299struct inode * ext3_new_inode (handle_t *handle,
300 const struct inode * dir, int mode)
301{
302 struct super_block * sb;
303 struct buffer_head * bh;
304 struct buffer_head * bh2;
305 int i, j, avefreei;
306 struct inode * inode;
307 int bitmap_nr;
308 struct ext3_group_desc * gdp;
309 struct ext3_group_desc * tmp;
310 struct ext3_super_block * es;
311 int err = 0;
312
313
314 if (!dir || !dir->i_nlink)
315 return ERR_PTR(-EPERM);
316
317 sb = dir->i_sb;
318 inode = new_inode(sb);
319 if (!inode)
320 return ERR_PTR(-ENOMEM);
321 init_rwsem(&inode->u.ext3_i.truncate_sem);
322
323 lock_super (sb);
324 es = sb->u.ext3_sb.s_es;
325repeat:
326 gdp = NULL;
327 i = 0;
328
329 if (S_ISDIR(mode)) {
330 avefreei = le32_to_cpu(es->s_free_inodes_count) /
331 sb->u.ext3_sb.s_groups_count;
332 if (!gdp) {
333 for (j = 0; j < sb->u.ext3_sb.s_groups_count; j++) {
334 struct buffer_head *temp_buffer;
335 tmp = ext3_get_group_desc (sb, j, &temp_buffer);
336 if (tmp &&
337 le16_to_cpu(tmp->bg_free_inodes_count) &&
338 le16_to_cpu(tmp->bg_free_inodes_count) >=
339 avefreei) {
340 if (!gdp || (le16_to_cpu(tmp->bg_free_blocks_count) >
341 le16_to_cpu(gdp->bg_free_blocks_count))) {
342 i = j;
343 gdp = tmp;
344 bh2 = temp_buffer;
345 }
346 }
347 }
348 }
349 } else {
350
351
352
353 i = dir->u.ext3_i.i_block_group;
354 tmp = ext3_get_group_desc (sb, i, &bh2);
355 if (tmp && le16_to_cpu(tmp->bg_free_inodes_count))
356 gdp = tmp;
357 else
358 {
359
360
361
362
363 for (j = 1; j < sb->u.ext3_sb.s_groups_count; j <<= 1) {
364 i += j;
365 if (i >= sb->u.ext3_sb.s_groups_count)
366 i -= sb->u.ext3_sb.s_groups_count;
367 tmp = ext3_get_group_desc (sb, i, &bh2);
368 if (tmp &&
369 le16_to_cpu(tmp->bg_free_inodes_count)) {
370 gdp = tmp;
371 break;
372 }
373 }
374 }
375 if (!gdp) {
376
377
378
379 i = dir->u.ext3_i.i_block_group + 1;
380 for (j = 2; j < sb->u.ext3_sb.s_groups_count; j++) {
381 if (++i >= sb->u.ext3_sb.s_groups_count)
382 i = 0;
383 tmp = ext3_get_group_desc (sb, i, &bh2);
384 if (tmp &&
385 le16_to_cpu(tmp->bg_free_inodes_count)) {
386 gdp = tmp;
387 break;
388 }
389 }
390 }
391 }
392
393 err = -ENOSPC;
394 if (!gdp)
395 goto out;
396
397 err = -EIO;
398 bitmap_nr = load_inode_bitmap (sb, i);
399 if (bitmap_nr < 0)
400 goto fail;
401
402 bh = sb->u.ext3_sb.s_inode_bitmap[bitmap_nr];
403
404 if ((j = ext3_find_first_zero_bit ((unsigned long *) bh->b_data,
405 EXT3_INODES_PER_GROUP(sb))) <
406 EXT3_INODES_PER_GROUP(sb)) {
407 BUFFER_TRACE(bh, "get_write_access");
408 err = ext3_journal_get_write_access(handle, bh);
409 if (err) goto fail;
410
411 if (ext3_set_bit (j, bh->b_data)) {
412 ext3_error (sb, "ext3_new_inode",
413 "bit already set for inode %d", j);
414 goto repeat;
415 }
416 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
417 err = ext3_journal_dirty_metadata(handle, bh);
418 if (err) goto fail;
419 } else {
420 if (le16_to_cpu(gdp->bg_free_inodes_count) != 0) {
421 ext3_error (sb, "ext3_new_inode",
422 "Free inodes count corrupted in group %d",
423 i);
424
425 err = -ENOSPC;
426 if (sb->s_flags & MS_RDONLY)
427 goto fail;
428
429 BUFFER_TRACE(bh2, "get_write_access");
430 err = ext3_journal_get_write_access(handle, bh2);
431 if (err) goto fail;
432 gdp->bg_free_inodes_count = 0;
433 BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
434 err = ext3_journal_dirty_metadata(handle, bh2);
435 if (err) goto fail;
436 }
437 goto repeat;
438 }
439 j += i * EXT3_INODES_PER_GROUP(sb) + 1;
440 if (j < EXT3_FIRST_INO(sb) || j > le32_to_cpu(es->s_inodes_count)) {
441 ext3_error (sb, "ext3_new_inode",
442 "reserved inode or inode > inodes count - "
443 "block_group = %d,inode=%d", i, j);
444 err = -EIO;
445 goto fail;
446 }
447
448 BUFFER_TRACE(bh2, "get_write_access");
449 err = ext3_journal_get_write_access(handle, bh2);
450 if (err) goto fail;
451 gdp->bg_free_inodes_count =
452 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
453 if (S_ISDIR(mode))
454 gdp->bg_used_dirs_count =
455 cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
456 BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
457 err = ext3_journal_dirty_metadata(handle, bh2);
458 if (err) goto fail;
459
460 BUFFER_TRACE(sb->u.ext3_sb.s_sbh, "get_write_access");
461 err = ext3_journal_get_write_access(handle, sb->u.ext3_sb.s_sbh);
462 if (err) goto fail;
463 es->s_free_inodes_count =
464 cpu_to_le32(le32_to_cpu(es->s_free_inodes_count) - 1);
465 BUFFER_TRACE(sb->u.ext3_sb.s_sbh, "call ext3_journal_dirty_metadata");
466 err = ext3_journal_dirty_metadata(handle, sb->u.ext3_sb.s_sbh);
467 sb->s_dirt = 1;
468 if (err) goto fail;
469
470 inode->i_uid = current->fsuid;
471 if (test_opt (sb, GRPID))
472 inode->i_gid = dir->i_gid;
473 else if (dir->i_mode & S_ISGID) {
474 inode->i_gid = dir->i_gid;
475 if (S_ISDIR(mode))
476 mode |= S_ISGID;
477 } else
478 inode->i_gid = current->fsgid;
479 inode->i_mode = mode;
480
481 inode->i_ino = j;
482
483 inode->i_blksize = PAGE_SIZE;
484 inode->i_blocks = 0;
485 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
486 inode->u.ext3_i.i_flags = dir->u.ext3_i.i_flags & ~EXT3_INDEX_FL;
487 if (S_ISLNK(mode))
488 inode->u.ext3_i.i_flags &= ~(EXT3_IMMUTABLE_FL|EXT3_APPEND_FL);
489#ifdef EXT3_FRAGMENTS
490 inode->u.ext3_i.i_faddr = 0;
491 inode->u.ext3_i.i_frag_no = 0;
492 inode->u.ext3_i.i_frag_size = 0;
493#endif
494 inode->u.ext3_i.i_file_acl = 0;
495 inode->u.ext3_i.i_dir_acl = 0;
496 inode->u.ext3_i.i_dtime = 0;
497 INIT_LIST_HEAD(&inode->u.ext3_i.i_orphan);
498#ifdef EXT3_PREALLOCATE
499 inode->u.ext3_i.i_prealloc_count = 0;
500#endif
501 inode->u.ext3_i.i_block_group = i;
502
503 ext3_set_inode_flags(inode);
504 if (IS_SYNC(inode))
505 handle->h_sync = 1;
506 insert_inode_hash(inode);
507 inode->i_generation = sb->u.ext3_sb.s_next_generation++;
508
509 inode->u.ext3_i.i_state = EXT3_STATE_NEW;
510 err = ext3_mark_inode_dirty(handle, inode);
511 if (err) goto fail;
512
513 unlock_super (sb);
514 if(DQUOT_ALLOC_INODE(inode)) {
515 DQUOT_DROP(inode);
516 inode->i_flags |= S_NOQUOTA;
517 inode->i_nlink = 0;
518 iput(inode);
519 return ERR_PTR(-EDQUOT);
520 }
521 ext3_debug ("allocating inode %lu\n", inode->i_ino);
522 return inode;
523
524fail:
525 ext3_std_error(sb, err);
526out:
527 unlock_super(sb);
528 iput(inode);
529 return ERR_PTR(err);
530}
531
532
533struct inode *ext3_orphan_get(struct super_block *sb, unsigned long ino)
534{
535 unsigned long max_ino = le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count);
536 unsigned long block_group;
537 int bit;
538 int bitmap_nr;
539 struct buffer_head *bh;
540 struct inode *inode = NULL;
541
542
543 if (ino > max_ino) {
544 ext3_warning(sb, __FUNCTION__,
545 "bad orphan ino %lu! e2fsck was run?\n", ino);
546 return NULL;
547 }
548
549 block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
550 bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
551 if ((bitmap_nr = load_inode_bitmap(sb, block_group)) < 0 ||
552 !(bh = EXT3_SB(sb)->s_inode_bitmap[bitmap_nr])) {
553 ext3_warning(sb, __FUNCTION__,
554 "inode bitmap error for orphan %lu\n", ino);
555 return NULL;
556 }
557
558
559
560
561
562 if (!ext3_test_bit(bit, bh->b_data) || !(inode = iget(sb, ino)) ||
563 is_bad_inode(inode) || NEXT_ORPHAN(inode) > max_ino) {
564 ext3_warning(sb, __FUNCTION__,
565 "bad orphan inode %lu! e2fsck was run?\n", ino);
566 printk(KERN_NOTICE "ext3_test_bit(bit=%d, block=%ld) = %d\n",
567 bit, bh->b_blocknr, ext3_test_bit(bit, bh->b_data));
568 printk(KERN_NOTICE "inode=%p\n", inode);
569 if (inode) {
570 printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
571 is_bad_inode(inode));
572 printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
573 NEXT_ORPHAN(inode));
574 printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
575 }
576
577 if (inode && inode->i_nlink == 0)
578 inode->i_blocks = 0;
579 iput(inode);
580 return NULL;
581 }
582
583 return inode;
584}
585
586unsigned long ext3_count_free_inodes (struct super_block * sb)
587{
588#ifdef EXT3FS_DEBUG
589 struct ext3_super_block * es;
590 unsigned long desc_count, bitmap_count, x;
591 int bitmap_nr;
592 struct ext3_group_desc * gdp;
593 int i;
594
595 lock_super (sb);
596 es = sb->u.ext3_sb.s_es;
597 desc_count = 0;
598 bitmap_count = 0;
599 gdp = NULL;
600 for (i = 0; i < sb->u.ext3_sb.s_groups_count; i++) {
601 gdp = ext3_get_group_desc (sb, i, NULL);
602 if (!gdp)
603 continue;
604 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
605 bitmap_nr = load_inode_bitmap (sb, i);
606 if (bitmap_nr < 0)
607 continue;
608
609 x = ext3_count_free (sb->u.ext3_sb.s_inode_bitmap[bitmap_nr],
610 EXT3_INODES_PER_GROUP(sb) / 8);
611 printk ("group %d: stored = %d, counted = %lu\n",
612 i, le16_to_cpu(gdp->bg_free_inodes_count), x);
613 bitmap_count += x;
614 }
615 printk("ext3_count_free_inodes: stored = %lu, computed = %lu, %lu\n",
616 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
617 unlock_super (sb);
618 return desc_count;
619#else
620 return le32_to_cpu(sb->u.ext3_sb.s_es->s_free_inodes_count);
621#endif
622}
623
624#ifdef CONFIG_EXT3_CHECK
625
626void ext3_check_inodes_bitmap (struct super_block * sb)
627{
628 struct ext3_super_block * es;
629 unsigned long desc_count, bitmap_count, x;
630 int bitmap_nr;
631 struct ext3_group_desc * gdp;
632 int i;
633
634 es = sb->u.ext3_sb.s_es;
635 desc_count = 0;
636 bitmap_count = 0;
637 gdp = NULL;
638 for (i = 0; i < sb->u.ext3_sb.s_groups_count; i++) {
639 gdp = ext3_get_group_desc (sb, i, NULL);
640 if (!gdp)
641 continue;
642 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
643 bitmap_nr = load_inode_bitmap (sb, i);
644 if (bitmap_nr < 0)
645 continue;
646
647 x = ext3_count_free (sb->u.ext3_sb.s_inode_bitmap[bitmap_nr],
648 EXT3_INODES_PER_GROUP(sb) / 8);
649 if (le16_to_cpu(gdp->bg_free_inodes_count) != x)
650 ext3_error (sb, "ext3_check_inodes_bitmap",
651 "Wrong free inodes count in group %d, "
652 "stored = %d, counted = %lu", i,
653 le16_to_cpu(gdp->bg_free_inodes_count), x);
654 bitmap_count += x;
655 }
656 if (le32_to_cpu(es->s_free_inodes_count) != bitmap_count)
657 ext3_error (sb, "ext3_check_inodes_bitmap",
658 "Wrong free inodes count in super block, "
659 "stored = %lu, counted = %lu",
660 (unsigned long)le32_to_cpu(es->s_free_inodes_count),
661 bitmap_count);
662}
663#endif
664