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