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