vserver 1.9.3
[linux-2.6.git] / fs / ext3 / ialloc.c
1 /*
2  *  linux/fs/ext3/ialloc.c
3  *
4  * Copyright (C) 1992, 1993, 1994, 1995
5  * Remy Card (card@masi.ibp.fr)
6  * Laboratoire MASI - Institut Blaise Pascal
7  * Universite Pierre et Marie Curie (Paris VI)
8  *
9  *  BSD ufs-inspired inode and directory allocation by
10  *  Stephen Tweedie (sct@redhat.com), 1993
11  *  Big-endian to little-endian byte-swapping/bitmaps by
12  *        David S. Miller (davem@caip.rutgers.edu), 1995
13  */
14
15 #include <linux/time.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/quotaops.h>
23 #include <linux/buffer_head.h>
24 #include <linux/random.h>
25 #include <linux/vs_dlimit.h>
26
27 #include <asm/bitops.h>
28 #include <asm/byteorder.h>
29
30 #include "xattr.h"
31 #include "acl.h"
32
33 /*
34  * ialloc.c contains the inodes allocation and deallocation routines
35  */
36
37 /*
38  * The free inodes are managed by bitmaps.  A file system contains several
39  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
40  * block for inodes, N blocks for the inode table and data blocks.
41  *
42  * The file system contains group descriptors which are located after the
43  * super block.  Each descriptor contains the number of the bitmap block and
44  * the free blocks count in the block.
45  */
46
47
48 /*
49  * Read the inode allocation bitmap for a given block_group, reading
50  * into the specified slot in the superblock's bitmap cache.
51  *
52  * Return buffer_head of bitmap on success or NULL.
53  */
54 static struct buffer_head *
55 read_inode_bitmap(struct super_block * sb, unsigned long block_group)
56 {
57         struct ext3_group_desc *desc;
58         struct buffer_head *bh = NULL;
59
60         desc = ext3_get_group_desc(sb, block_group, NULL);
61         if (!desc)
62                 goto error_out;
63
64         bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
65         if (!bh)
66                 ext3_error(sb, "read_inode_bitmap",
67                             "Cannot read inode bitmap - "
68                             "block_group = %lu, inode_bitmap = %u",
69                             block_group, le32_to_cpu(desc->bg_inode_bitmap));
70 error_out:
71         return bh;
72 }
73
74 /*
75  * NOTE! When we get the inode, we're the only people
76  * that have access to it, and as such there are no
77  * race conditions we have to worry about. The inode
78  * is not on the hash-lists, and it cannot be reached
79  * through the filesystem because the directory entry
80  * has been deleted earlier.
81  *
82  * HOWEVER: we must make sure that we get no aliases,
83  * which means that we have to call "clear_inode()"
84  * _before_ we mark the inode not in use in the inode
85  * bitmaps. Otherwise a newly created file might use
86  * the same inode number (not actually the same pointer
87  * though), and then we'd have two inodes sharing the
88  * same inode number and space on the harddisk.
89  */
90 void ext3_free_inode (handle_t *handle, struct inode * inode)
91 {
92         struct super_block * sb = inode->i_sb;
93         int is_directory;
94         unsigned long ino;
95         struct buffer_head *bitmap_bh = NULL;
96         struct buffer_head *bh2;
97         unsigned long block_group;
98         unsigned long bit;
99         struct ext3_group_desc * gdp;
100         struct ext3_super_block * es;
101         struct ext3_sb_info *sbi;
102         int fatal = 0, err;
103
104         if (atomic_read(&inode->i_count) > 1) {
105                 printk ("ext3_free_inode: inode has count=%d\n",
106                                         atomic_read(&inode->i_count));
107                 return;
108         }
109         if (inode->i_nlink) {
110                 printk ("ext3_free_inode: inode has nlink=%d\n",
111                         inode->i_nlink);
112                 return;
113         }
114         if (!sb) {
115                 printk("ext3_free_inode: inode on nonexistent device\n");
116                 return;
117         }
118         sbi = EXT3_SB(sb);
119
120         ino = inode->i_ino;
121         ext3_debug ("freeing inode %lu\n", ino);
122
123         /*
124          * Note: we must free any quota before locking the superblock,
125          * as writing the quota to disk may need the lock as well.
126          */
127         DQUOT_INIT(inode);
128         ext3_xattr_delete_inode(handle, inode);
129         DLIMIT_FREE_INODE(sb, inode->i_xid);
130         DQUOT_FREE_INODE(inode);
131         DQUOT_DROP(inode);
132
133         is_directory = S_ISDIR(inode->i_mode);
134
135         /* Do this BEFORE marking the inode not in use or returning an error */
136         clear_inode (inode);
137
138         es = EXT3_SB(sb)->s_es;
139         if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
140                 ext3_error (sb, "ext3_free_inode",
141                             "reserved or nonexistent inode %lu", ino);
142                 goto error_return;
143         }
144         block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
145         bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
146         bitmap_bh = read_inode_bitmap(sb, block_group);
147         if (!bitmap_bh)
148                 goto error_return;
149
150         BUFFER_TRACE(bitmap_bh, "get_write_access");
151         fatal = ext3_journal_get_write_access(handle, bitmap_bh);
152         if (fatal)
153                 goto error_return;
154
155         /* Ok, now we can actually update the inode bitmaps.. */
156         if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
157                                         bit, bitmap_bh->b_data))
158                 ext3_error (sb, "ext3_free_inode",
159                               "bit already cleared for inode %lu", ino);
160         else {
161                 gdp = ext3_get_group_desc (sb, block_group, &bh2);
162
163                 BUFFER_TRACE(bh2, "get_write_access");
164                 fatal = ext3_journal_get_write_access(handle, bh2);
165                 if (fatal) goto error_return;
166
167                 if (gdp) {
168                         spin_lock(sb_bgl_lock(sbi, block_group));
169                         gdp->bg_free_inodes_count = cpu_to_le16(
170                                 le16_to_cpu(gdp->bg_free_inodes_count) + 1);
171                         if (is_directory)
172                                 gdp->bg_used_dirs_count = cpu_to_le16(
173                                   le16_to_cpu(gdp->bg_used_dirs_count) - 1);
174                         spin_unlock(sb_bgl_lock(sbi, block_group));
175                         percpu_counter_inc(&sbi->s_freeinodes_counter);
176                         if (is_directory)
177                                 percpu_counter_dec(&sbi->s_dirs_counter);
178
179                 }
180                 BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
181                 err = ext3_journal_dirty_metadata(handle, bh2);
182                 if (!fatal) fatal = err;
183         }
184         BUFFER_TRACE(bitmap_bh, "call ext3_journal_dirty_metadata");
185         err = ext3_journal_dirty_metadata(handle, bitmap_bh);
186         if (!fatal)
187                 fatal = err;
188         sb->s_dirt = 1;
189 error_return:
190         brelse(bitmap_bh);
191         ext3_std_error(sb, fatal);
192 }
193
194 /*
195  * There are two policies for allocating an inode.  If the new inode is
196  * a directory, then a forward search is made for a block group with both
197  * free space and a low directory-to-inode ratio; if that fails, then of
198  * the groups with above-average free space, that group with the fewest
199  * directories already is chosen.
200  *
201  * For other inodes, search forward from the parent directory\'s block
202  * group to find a free inode.
203  */
204 static int find_group_dir(struct super_block *sb, struct inode *parent)
205 {
206         int ngroups = EXT3_SB(sb)->s_groups_count;
207         int freei, avefreei;
208         struct ext3_group_desc *desc, *best_desc = NULL;
209         struct buffer_head *bh;
210         int group, best_group = -1;
211
212         freei = percpu_counter_read_positive(&EXT3_SB(sb)->s_freeinodes_counter);
213         avefreei = freei / ngroups;
214
215         for (group = 0; group < ngroups; group++) {
216                 desc = ext3_get_group_desc (sb, group, &bh);
217                 if (!desc || !desc->bg_free_inodes_count)
218                         continue;
219                 if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
220                         continue;
221                 if (!best_desc || 
222                     (le16_to_cpu(desc->bg_free_blocks_count) >
223                      le16_to_cpu(best_desc->bg_free_blocks_count))) {
224                         best_group = group;
225                         best_desc = desc;
226                 }
227         }
228         return best_group;
229 }
230
231 /* 
232  * Orlov's allocator for directories. 
233  * 
234  * We always try to spread first-level directories.
235  *
236  * If there are blockgroups with both free inodes and free blocks counts 
237  * not worse than average we return one with smallest directory count. 
238  * Otherwise we simply return a random group. 
239  * 
240  * For the rest rules look so: 
241  * 
242  * It's OK to put directory into a group unless 
243  * it has too many directories already (max_dirs) or 
244  * it has too few free inodes left (min_inodes) or 
245  * it has too few free blocks left (min_blocks) or 
246  * it's already running too large debt (max_debt). 
247  * Parent's group is prefered, if it doesn't satisfy these 
248  * conditions we search cyclically through the rest. If none 
249  * of the groups look good we just look for a group with more 
250  * free inodes than average (starting at parent's group). 
251  * 
252  * Debt is incremented each time we allocate a directory and decremented 
253  * when we allocate an inode, within 0--255. 
254  */ 
255
256 #define INODE_COST 64
257 #define BLOCK_COST 256
258
259 static int find_group_orlov(struct super_block *sb, struct inode *parent)
260 {
261         int parent_group = EXT3_I(parent)->i_block_group;
262         struct ext3_sb_info *sbi = EXT3_SB(sb);
263         struct ext3_super_block *es = sbi->s_es;
264         int ngroups = sbi->s_groups_count;
265         int inodes_per_group = EXT3_INODES_PER_GROUP(sb);
266         int freei, avefreei;
267         int freeb, avefreeb;
268         int blocks_per_dir, ndirs;
269         int max_debt, max_dirs, min_blocks, min_inodes;
270         int group = -1, i;
271         struct ext3_group_desc *desc;
272         struct buffer_head *bh;
273
274         freei = percpu_counter_read_positive(&sbi->s_freeinodes_counter);
275         avefreei = freei / ngroups;
276         freeb = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
277         avefreeb = freeb / ngroups;
278         ndirs = percpu_counter_read_positive(&sbi->s_dirs_counter);
279
280         if ((parent == sb->s_root->d_inode) ||
281             (EXT3_I(parent)->i_flags & EXT3_TOPDIR_FL)) {
282                 int best_ndir = inodes_per_group;
283                 int best_group = -1;
284
285                 get_random_bytes(&group, sizeof(group));
286                 parent_group = (unsigned)group % ngroups;
287                 for (i = 0; i < ngroups; i++) {
288                         group = (parent_group + i) % ngroups;
289                         desc = ext3_get_group_desc (sb, group, &bh);
290                         if (!desc || !desc->bg_free_inodes_count)
291                                 continue;
292                         if (le16_to_cpu(desc->bg_used_dirs_count) >= best_ndir)
293                                 continue;
294                         if (le16_to_cpu(desc->bg_free_inodes_count) < avefreei)
295                                 continue;
296                         if (le16_to_cpu(desc->bg_free_blocks_count) < avefreeb)
297                                 continue;
298                         best_group = group;
299                         best_ndir = le16_to_cpu(desc->bg_used_dirs_count);
300                 }
301                 if (best_group >= 0)
302                         return best_group;
303                 goto fallback;
304         }
305
306         blocks_per_dir = (le32_to_cpu(es->s_blocks_count) - freeb) / ndirs;
307
308         max_dirs = ndirs / ngroups + inodes_per_group / 16;
309         min_inodes = avefreei - inodes_per_group / 4;
310         min_blocks = avefreeb - EXT3_BLOCKS_PER_GROUP(sb) / 4;
311
312         max_debt = EXT3_BLOCKS_PER_GROUP(sb) / max(blocks_per_dir, BLOCK_COST);
313         if (max_debt * INODE_COST > inodes_per_group)
314                 max_debt = inodes_per_group / INODE_COST;
315         if (max_debt > 255)
316                 max_debt = 255;
317         if (max_debt == 0)
318                 max_debt = 1;
319
320         for (i = 0; i < ngroups; i++) {
321                 group = (parent_group + i) % ngroups;
322                 desc = ext3_get_group_desc (sb, group, &bh);
323                 if (!desc || !desc->bg_free_inodes_count)
324                         continue;
325                 if (sbi->s_debts[group] >= max_debt)
326                         continue;
327                 if (le16_to_cpu(desc->bg_used_dirs_count) >= max_dirs)
328                         continue;
329                 if (le16_to_cpu(desc->bg_free_inodes_count) < min_inodes)
330                         continue;
331                 if (le16_to_cpu(desc->bg_free_blocks_count) < min_blocks)
332                         continue;
333                 return group;
334         }
335
336 fallback:
337         for (i = 0; i < ngroups; i++) {
338                 group = (parent_group + i) % ngroups;
339                 desc = ext3_get_group_desc (sb, group, &bh);
340                 if (!desc || !desc->bg_free_inodes_count)
341                         continue;
342                 if (le16_to_cpu(desc->bg_free_inodes_count) >= avefreei)
343                         return group;
344         }
345
346         if (avefreei) {
347                 /*
348                  * The free-inodes counter is approximate, and for really small
349                  * filesystems the above test can fail to find any blockgroups
350                  */
351                 avefreei = 0;
352                 goto fallback;
353         }
354
355         return -1;
356 }
357
358 static int find_group_other(struct super_block *sb, struct inode *parent)
359 {
360         int parent_group = EXT3_I(parent)->i_block_group;
361         int ngroups = EXT3_SB(sb)->s_groups_count;
362         struct ext3_group_desc *desc;
363         struct buffer_head *bh;
364         int group, i;
365
366         /*
367          * Try to place the inode in its parent directory
368          */
369         group = parent_group;
370         desc = ext3_get_group_desc (sb, group, &bh);
371         if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
372                         le16_to_cpu(desc->bg_free_blocks_count))
373                 return group;
374
375         /*
376          * We're going to place this inode in a different blockgroup from its
377          * parent.  We want to cause files in a common directory to all land in
378          * the same blockgroup.  But we want files which are in a different
379          * directory which shares a blockgroup with our parent to land in a
380          * different blockgroup.
381          *
382          * So add our directory's i_ino into the starting point for the hash.
383          */
384         group = (group + parent->i_ino) % ngroups;
385
386         /*
387          * Use a quadratic hash to find a group with a free inode and some free
388          * blocks.
389          */
390         for (i = 1; i < ngroups; i <<= 1) {
391                 group += i;
392                 if (group >= ngroups)
393                         group -= ngroups;
394                 desc = ext3_get_group_desc (sb, group, &bh);
395                 if (desc && le16_to_cpu(desc->bg_free_inodes_count) &&
396                                 le16_to_cpu(desc->bg_free_blocks_count))
397                         return group;
398         }
399
400         /*
401          * That failed: try linear search for a free inode, even if that group
402          * has no free blocks.
403          */
404         group = parent_group;
405         for (i = 0; i < ngroups; i++) {
406                 if (++group >= ngroups)
407                         group = 0;
408                 desc = ext3_get_group_desc (sb, group, &bh);
409                 if (desc && le16_to_cpu(desc->bg_free_inodes_count))
410                         return group;
411         }
412
413         return -1;
414 }
415
416 /*
417  * There are two policies for allocating an inode.  If the new inode is
418  * a directory, then a forward search is made for a block group with both
419  * free space and a low directory-to-inode ratio; if that fails, then of
420  * the groups with above-average free space, that group with the fewest
421  * directories already is chosen.
422  *
423  * For other inodes, search forward from the parent directory's block
424  * group to find a free inode.
425  */
426 struct inode *ext3_new_inode(handle_t *handle, struct inode * dir, int mode)
427 {
428         struct super_block *sb;
429         struct buffer_head *bitmap_bh = NULL;
430         struct buffer_head *bh2;
431         int group;
432         unsigned long ino = 0;
433         struct inode * inode;
434         struct ext3_group_desc * gdp = NULL;
435         struct ext3_super_block * es;
436         struct ext3_inode_info *ei;
437         struct ext3_sb_info *sbi;
438         int err = 0;
439         struct inode *ret;
440         int i;
441
442         /* Cannot create files in a deleted directory */
443         if (!dir || !dir->i_nlink)
444                 return ERR_PTR(-EPERM);
445
446         sb = dir->i_sb;
447         inode = new_inode(sb);
448         if (!inode)
449                 return ERR_PTR(-ENOMEM);
450
451         if (sb->s_flags & MS_TAGXID)
452                 inode->i_xid = current->xid;
453         else
454                 inode->i_xid = 0;
455
456         if (DLIMIT_ALLOC_INODE(sb, inode->i_xid)) {
457                 err = -ENOSPC;
458                 goto out;
459         }
460         ei = EXT3_I(inode);
461
462         sbi = EXT3_SB(sb);
463         es = sbi->s_es;
464         if (S_ISDIR(mode)) {
465                 if (test_opt (sb, OLDALLOC))
466                         group = find_group_dir(sb, dir);
467                 else
468                         group = find_group_orlov(sb, dir);
469         } else 
470                 group = find_group_other(sb, dir);
471
472         err = -ENOSPC;
473         if (group == -1)
474                 goto out;
475
476         for (i = 0; i < sbi->s_groups_count; i++) {
477                 gdp = ext3_get_group_desc(sb, group, &bh2);
478
479                 err = -EIO;
480                 brelse(bitmap_bh);
481                 bitmap_bh = read_inode_bitmap(sb, group);
482                 if (!bitmap_bh)
483                         goto fail;
484
485                 ino = 0;
486
487 repeat_in_this_group:
488                 ino = ext3_find_next_zero_bit((unsigned long *)
489                                 bitmap_bh->b_data, EXT3_INODES_PER_GROUP(sb), ino);
490                 if (ino < EXT3_INODES_PER_GROUP(sb)) {
491                         int credits = 0;
492
493                         BUFFER_TRACE(bitmap_bh, "get_write_access");
494                         err = ext3_journal_get_write_access_credits(handle,
495                                                         bitmap_bh, &credits);
496                         if (err)
497                                 goto fail;
498
499                         if (!ext3_set_bit_atomic(sb_bgl_lock(sbi, group),
500                                                 ino, bitmap_bh->b_data)) {
501                                 /* we won it */
502                                 BUFFER_TRACE(bitmap_bh,
503                                         "call ext3_journal_dirty_metadata");
504                                 err = ext3_journal_dirty_metadata(handle,
505                                                                 bitmap_bh);
506                                 if (err)
507                                         goto fail;
508                                 goto got;
509                         }
510                         /* we lost it */
511                         journal_release_buffer(handle, bitmap_bh, credits);
512
513                         if (++ino < EXT3_INODES_PER_GROUP(sb))
514                                 goto repeat_in_this_group;
515                 }
516
517                 /*
518                  * This case is possible in concurrent environment.  It is very
519                  * rare.  We cannot repeat the find_group_xxx() call because
520                  * that will simply return the same blockgroup, because the
521                  * group descriptor metadata has not yet been updated.
522                  * So we just go onto the next blockgroup.
523                  */
524                 if (++group == sbi->s_groups_count)
525                         group = 0;
526         }
527         err = -ENOSPC;
528         goto out;
529
530 got:
531         ino += group * EXT3_INODES_PER_GROUP(sb) + 1;
532         if (ino < EXT3_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
533                 ext3_error (sb, "ext3_new_inode",
534                             "reserved inode or inode > inodes count - "
535                             "block_group = %d, inode=%lu", group, ino);
536                 err = -EIO;
537                 goto fail;
538         }
539
540         BUFFER_TRACE(bh2, "get_write_access");
541         err = ext3_journal_get_write_access(handle, bh2);
542         if (err) goto fail;
543         spin_lock(sb_bgl_lock(sbi, group));
544         gdp->bg_free_inodes_count =
545                 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
546         if (S_ISDIR(mode)) {
547                 gdp->bg_used_dirs_count =
548                         cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
549         }
550         spin_unlock(sb_bgl_lock(sbi, group));
551         BUFFER_TRACE(bh2, "call ext3_journal_dirty_metadata");
552         err = ext3_journal_dirty_metadata(handle, bh2);
553         if (err) goto fail;
554
555         percpu_counter_dec(&sbi->s_freeinodes_counter);
556         if (S_ISDIR(mode))
557                 percpu_counter_inc(&sbi->s_dirs_counter);
558         sb->s_dirt = 1;
559
560         inode->i_uid = current->fsuid;
561         if (test_opt (sb, GRPID))
562                 inode->i_gid = dir->i_gid;
563         else if (dir->i_mode & S_ISGID) {
564                 inode->i_gid = dir->i_gid;
565                 if (S_ISDIR(mode))
566                         mode |= S_ISGID;
567         } else
568                 inode->i_gid = current->fsgid;
569         inode->i_mode = mode;
570
571         inode->i_ino = ino;
572         /* This is the optimal IO size (for stat), not the fs block size */
573         inode->i_blksize = PAGE_SIZE;
574         inode->i_blocks = 0;
575         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
576
577         memset(ei->i_data, 0, sizeof(ei->i_data));
578         ei->i_next_alloc_block = 0;
579         ei->i_next_alloc_goal = 0;
580         ei->i_dir_start_lookup = 0;
581         ei->i_disksize = 0;
582
583         ei->i_flags = EXT3_I(dir)->i_flags &
584                 ~(EXT3_INDEX_FL|EXT3_IUNLINK_FL|EXT3_BARRIER_FL);
585         if (S_ISLNK(mode))
586                 ei->i_flags &= ~(EXT3_IMMUTABLE_FL|EXT3_APPEND_FL);
587         /* dirsync only applies to directories */
588         if (!S_ISDIR(mode))
589                 ei->i_flags &= ~EXT3_DIRSYNC_FL;
590 #ifdef EXT3_FRAGMENTS
591         ei->i_faddr = 0;
592         ei->i_frag_no = 0;
593         ei->i_frag_size = 0;
594 #endif
595         ei->i_file_acl = 0;
596         ei->i_dir_acl = 0;
597         ei->i_dtime = 0;
598 #ifdef EXT3_PREALLOCATE
599         ei->i_prealloc_block = 0;
600         ei->i_prealloc_count = 0;
601 #endif
602         ei->i_block_group = group;
603
604         ext3_set_inode_flags(inode);
605         if (IS_DIRSYNC(inode))
606                 handle->h_sync = 1;
607         insert_inode_hash(inode);
608         spin_lock(&sbi->s_next_gen_lock);
609         inode->i_generation = sbi->s_next_generation++;
610         spin_unlock(&sbi->s_next_gen_lock);
611
612         ei->i_state = EXT3_STATE_NEW;
613
614         ret = inode;
615         if(DQUOT_ALLOC_INODE(inode)) {
616                 DQUOT_DROP(inode);
617                 err = -EDQUOT;
618                 goto fail2;
619         }
620         err = ext3_init_acl(handle, inode, dir);
621         if (err) {
622                 DQUOT_FREE_INODE(inode);
623                 goto fail2;
624         }
625         err = ext3_mark_inode_dirty(handle, inode);
626         if (err) {
627                 ext3_std_error(sb, err);
628                 DQUOT_FREE_INODE(inode);
629                 goto fail2;
630         }
631
632         ext3_debug("allocating inode %lu\n", inode->i_ino);
633         goto really_out;
634 fail:
635         DLIMIT_FREE_INODE(sb, inode->i_xid);
636         ext3_std_error(sb, err);
637 out:
638         iput(inode);
639         ret = ERR_PTR(err);
640 really_out:
641         brelse(bitmap_bh);
642         return ret;
643
644 fail2:
645         DLIMIT_FREE_INODE(sb, inode->i_xid);
646         inode->i_flags |= S_NOQUOTA;
647         inode->i_nlink = 0;
648         iput(inode);
649         brelse(bitmap_bh);
650         return ERR_PTR(err);
651 }
652
653 /* Verify that we are loading a valid orphan from disk */
654 struct inode *ext3_orphan_get(struct super_block *sb, unsigned long ino)
655 {
656         unsigned long max_ino = le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count);
657         unsigned long block_group;
658         int bit;
659         struct buffer_head *bitmap_bh = NULL;
660         struct inode *inode = NULL;
661
662         /* Error cases - e2fsck has already cleaned up for us */
663         if (ino > max_ino) {
664                 ext3_warning(sb, __FUNCTION__,
665                              "bad orphan ino %lu!  e2fsck was run?\n", ino);
666                 goto out;
667         }
668
669         block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
670         bit = (ino - 1) % EXT3_INODES_PER_GROUP(sb);
671         bitmap_bh = read_inode_bitmap(sb, block_group);
672         if (!bitmap_bh) {
673                 ext3_warning(sb, __FUNCTION__,
674                              "inode bitmap error for orphan %lu\n", ino);
675                 goto out;
676         }
677
678         /* Having the inode bit set should be a 100% indicator that this
679          * is a valid orphan (no e2fsck run on fs).  Orphans also include
680          * inodes that were being truncated, so we can't check i_nlink==0.
681          */
682         if (!ext3_test_bit(bit, bitmap_bh->b_data) ||
683                         !(inode = iget(sb, ino)) || is_bad_inode(inode) ||
684                         NEXT_ORPHAN(inode) > max_ino) {
685                 ext3_warning(sb, __FUNCTION__,
686                              "bad orphan inode %lu!  e2fsck was run?\n", ino);
687                 printk(KERN_NOTICE "ext3_test_bit(bit=%d, block=%llu) = %d\n",
688                        bit, (unsigned long long)bitmap_bh->b_blocknr,
689                        ext3_test_bit(bit, bitmap_bh->b_data));
690                 printk(KERN_NOTICE "inode=%p\n", inode);
691                 if (inode) {
692                         printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
693                                is_bad_inode(inode));
694                         printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
695                                NEXT_ORPHAN(inode));
696                         printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
697                 }
698                 /* Avoid freeing blocks if we got a bad deleted inode */
699                 if (inode && inode->i_nlink == 0)
700                         inode->i_blocks = 0;
701                 iput(inode);
702                 inode = NULL;
703         }
704 out:
705         brelse(bitmap_bh);
706         return inode;
707 }
708
709 unsigned long ext3_count_free_inodes (struct super_block * sb)
710 {
711         unsigned long desc_count;
712         struct ext3_group_desc *gdp;
713         int i;
714 #ifdef EXT3FS_DEBUG
715         struct ext3_super_block *es;
716         unsigned long bitmap_count, x;
717         struct buffer_head *bitmap_bh = NULL;
718
719         lock_super (sb);
720         es = EXT3_SB(sb)->s_es;
721         desc_count = 0;
722         bitmap_count = 0;
723         gdp = NULL;
724         for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
725                 gdp = ext3_get_group_desc (sb, i, NULL);
726                 if (!gdp)
727                         continue;
728                 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
729                 brelse(bitmap_bh);
730                 bitmap_bh = read_inode_bitmap(sb, i);
731                 if (!bitmap_bh)
732                         continue;
733
734                 x = ext3_count_free(bitmap_bh, EXT3_INODES_PER_GROUP(sb) / 8);
735                 printk("group %d: stored = %d, counted = %lu\n",
736                         i, le16_to_cpu(gdp->bg_free_inodes_count), x);
737                 bitmap_count += x;
738         }
739         brelse(bitmap_bh);
740         printk("ext3_count_free_inodes: stored = %u, computed = %lu, %lu\n",
741                 le32_to_cpu(es->s_free_inodes_count), desc_count, bitmap_count);
742         unlock_super(sb);
743         return desc_count;
744 #else
745         desc_count = 0;
746         for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
747                 gdp = ext3_get_group_desc (sb, i, NULL);
748                 if (!gdp)
749                         continue;
750                 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
751         }
752         return desc_count;
753 #endif
754 }
755
756 /* Called at mount-time, super-block is locked */
757 unsigned long ext3_count_dirs (struct super_block * sb)
758 {
759         unsigned long count = 0;
760         int i;
761
762         for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
763                 struct ext3_group_desc *gdp = ext3_get_group_desc (sb, i, NULL);
764                 if (!gdp)
765                         continue;
766                 count += le16_to_cpu(gdp->bg_used_dirs_count);
767         }
768         return count;
769 }
770
771 #ifdef CONFIG_EXT3_CHECK
772 /* Called at mount-time, super-block is locked */
773 void ext3_check_inodes_bitmap (struct super_block * sb)
774 {
775         struct ext3_super_block * es;
776         unsigned long desc_count, bitmap_count, x;
777         struct buffer_head *bitmap_bh = NULL;
778         struct ext3_group_desc * gdp;
779         int i;
780
781         es = EXT3_SB(sb)->s_es;
782         desc_count = 0;
783         bitmap_count = 0;
784         gdp = NULL;
785         for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
786                 gdp = ext3_get_group_desc (sb, i, NULL);
787                 if (!gdp)
788                         continue;
789                 desc_count += le16_to_cpu(gdp->bg_free_inodes_count);
790                 brelse(bitmap_bh);
791                 bitmap_bh = read_inode_bitmap(sb, i);
792                 if (!bitmap_bh)
793                         continue;
794
795                 x = ext3_count_free(bitmap_bh, EXT3_INODES_PER_GROUP(sb) / 8);
796                 if (le16_to_cpu(gdp->bg_free_inodes_count) != x)
797                         ext3_error (sb, "ext3_check_inodes_bitmap",
798                                     "Wrong free inodes count in group %d, "
799                                     "stored = %d, counted = %lu", i,
800                                     le16_to_cpu(gdp->bg_free_inodes_count), x);
801                 bitmap_count += x;
802         }
803         brelse(bitmap_bh);
804         if (le32_to_cpu(es->s_free_inodes_count) != bitmap_count)
805                 ext3_error (sb, "ext3_check_inodes_bitmap",
806                             "Wrong free inodes count in super block, "
807                             "stored = %lu, counted = %lu",
808                             (unsigned long)le32_to_cpu(es->s_free_inodes_count),
809                             bitmap_count);
810 }
811 #endif